Procházet zdrojové kódy

RSA generator

master
root před 5 roky
rodič
revize
3579e62dd7
6 změnil soubory, kde provedl 153 přidání a 9 odebrání
  1. 18
    9
      src/Makefile
  2. 56
    0
      src/lc6_ssl.c
  3. 8
    0
      src/lc6_ssl.h
  4. 31
    0
      src/libchat6.c
  5. 19
    0
      src/libchat6.h
  6. 21
    0
      src/test.c

+ 18
- 9
src/Makefile Zobrazit soubor

@@ -15,14 +15,16 @@
###############################################################################

CC := gcc
#CCFLAGS := -Wall -Werror -pedantic -O2 -fPIC
CCFLAGS := -Wall -Werror -pedantic -g -fPIC
LDFLAGS := -lopenssl -lpthread
#CCFLAGS := -fPIC -Wall -Werror -std=c11 -pedantic -O2
CCFLAGS := -fPIC -Wall -Werror -std=c11 -pedantic -g
LDFLAGS :=
LIBS := -lchat6 -lssl -lpthread -lcrypto

TARGETS:= libchat6.so
TARGETS:= libchat6.so libchat6.a
MAINS := $(.o, $(TARGETS) )
OBJ := \
sockets.o \
lc6_ssl.o \
libchat6.o \
$(MAINS)
DEPS :=

@@ -31,11 +33,18 @@ DEPS :=
all: $(TARGETS)

clean:
rm -f $(TARGETS) $(OBJ)
rm -f $(TARGETS) $(OBJ) test test.o

$(OBJ): %.o : %.c $(DEPS)
$(CC) -c -o $@ $< $(CCFLAGS)
$(CC) $(CCFLAGS) $< -c -o $@

$(TARGETS): $(OBJ)
$(CC) -o $@ $(LIBS) $^ $(CCFLAGS) $(LDFLAGS)
libchat6.so: $(OBJ)
$(CC) $(LDFLAGS) -shared $^ $(LIBS) -o $@

libchat6.a: $(OBJ)
$(AR) rcs $@ $^
ranlib $@

test: test.o
$(CC) $(LDFLAGS) $^ -o $@ $(LIBS)


+ 56
- 0
src/lc6_ssl.c Zobrazit soubor

@@ -0,0 +1,56 @@
#include <openssl/rsa.h>
#include <openssl/pem.h>
#include <string.h>
#include <assert.h>

#include "lc6_ssl.h"

libchat_user* ssl_create_keypair(libchat_user *user) {

BIGNUM *bn;
RSA *rsa;
BIO *pub;
BIO *priv;
size_t publen;
size_t privlen;

if ( user == NULL ) {
user = malloc(sizeof(&user));
assert(user);
memset(user, 0, sizeof(&user));
}

bn = BN_new();
BN_set_word(bn, RSA_F4);

rsa = RSA_new();
RSA_generate_key_ex(rsa, 2048, bn, NULL);

priv = BIO_new(BIO_s_mem());
pub = BIO_new(BIO_s_mem());

PEM_write_bio_RSAPrivateKey(priv, rsa, NULL, NULL, 0, NULL, NULL);
PEM_write_bio_RSAPublicKey(pub, rsa);

RSA_free(rsa);

privlen = BIO_pending(priv);
publen = BIO_pending(pub);

user->priv_key = malloc(privlen+1);
user->pub_key = malloc(publen+1);


BIO_read(priv, user->priv_key, privlen);
BIO_read(pub, user->pub_key, publen);

user->priv_key[privlen] = '\0';
user->pub_key[publen] = '\0';

BIO_free_all(priv);
BIO_free_all(pub);
BN_free(bn);
// RSA_free(rsa);

return user;
}

+ 8
- 0
src/lc6_ssl.h Zobrazit soubor

@@ -0,0 +1,8 @@
#ifndef LC6_SSL_H
#define LC6_SSL_H

#include "libchat6.h"

libchat_user* ssl_create_keypair(libchat_user*);

#endif

+ 31
- 0
src/libchat6.c Zobrazit soubor

@@ -0,0 +1,31 @@
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

#include "lc6_ssl.h"


int libchat_init(libchat_user *user) {

if ( user == NULL )
return LIBCHAT_ERROR_INPUT_NULL;

return 0;
}

libchat_user* libchat_create_user(libchat_user *user) {
return ssl_create_keypair(user);
}

void libchat_free_user(libchat_user *user) {
if ( !user )
return;

if ( user->pub_key )
free(user->pub_key);

if ( user->priv_key )
free(user->priv_key);

free(user);
}

+ 19
- 0
src/libchat6.h Zobrazit soubor

@@ -0,0 +1,19 @@
#ifndef LC6_LIBCHAT6_H
#define LC6_LIBCHAT6_H

typedef struct libchat_user {
char nickname[256];
char *pub_key;
char *priv_key;
char icon[32768];
} libchat_user;

enum LIBCHAT_ERRORS {
LIBCHAT_ERROR_INPUT_NULL = 1
};

int libchat_init(libchat_user*);
libchat_user* libchat_create_user(libchat_user*);
void libchat_free_user(libchat_user*);

#endif

+ 21
- 0
src/test.c Zobrazit soubor

@@ -0,0 +1,21 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "libchat6.h"


int main(int argc, char **argv) {
libchat_user *user;
user = libchat_create_user(NULL);

strcpy(user->nickname,"Päscu");

printf("pub\n%s\n", user->pub_key);
printf("priv\n%s\n", user->priv_key);

libchat_free_user(user);
user = NULL;

return 0;
}

Načítá se…
Zrušit
Uložit