-
Notifications
You must be signed in to change notification settings - Fork 0
/
genKey.c
70 lines (61 loc) · 1.97 KB
/
genKey.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
Generate encryption key / IV and save to binary files
*/
#include <stdint.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
/* OpenSSL headers */
#include <openssl/bio.h>
#include <openssl/evp.h>
#include <openssl/rand.h>
void main()
{
uint8_t amal_key[EVP_MAX_KEY_LENGTH] ,
basim_key[EVP_MAX_KEY_LENGTH] ,
amal_iv[EVP_MAX_IV_LENGTH] ,
basim_iv[EVP_MAX_IV_LENGTH] ;
unsigned amal_key_len = EVP_MAX_KEY_LENGTH ;
unsigned basim_key_len = EVP_MAX_KEY_LENGTH ;
unsigned amal_iv_len = EVP_MAX_IV_LENGTH ;
unsigned basim_iv_len = EVP_MAX_IV_LENGTH ;
int amal_fd_key, amal_fd_iv, basim_fd_key, basim_fd_iv ;
amal_fd_key = open("kdc/amal_key.bin", O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR) ;
if( amal_fd_key == -1 )
{
fprintf(stderr, "Unable to open file for key\n");
exit(-1) ;
}
amal_fd_iv = open("kdc/amal_iv.bin", O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR) ;
if( amal_fd_iv == -1 )
{
fprintf(stderr, "Unable to open file for IV\n");
exit(-1) ;
}
basim_fd_key = open("kdc/basim_key.bin", O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR) ;
if( basim_fd_key == -1 )
{
fprintf(stderr, "Unable to open file for key\n");
exit(-1) ;
}
basim_fd_iv = open("kdc/basim_iv.bin", O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR) ;
if( basim_fd_iv == -1 )
{
fprintf(stderr, "Unable to open file for IV\n");
exit(-1) ;
}
// Genrate Amal random key & IV
RAND_bytes( amal_key , amal_key_len );
RAND_bytes( amal_iv , amal_iv_len );
// Generate Basim random key & IV
RAND_bytes( basim_key , basim_key_len );
RAND_bytes( basim_iv , basim_iv_len );
write( amal_fd_key , amal_key , amal_key_len );
write( amal_fd_iv , amal_iv , amal_iv_len );
write( basim_fd_key , basim_key , basim_key_len );
write( basim_fd_iv , basim_iv , basim_iv_len );
close( amal_fd_key ) ;
close( amal_fd_iv ) ;
close( basim_fd_key ) ;
close( basim_fd_iv ) ;
}