Repository navigation
Expand file tree
/
Copy pathkeyManagement.c
More file actions
154 lines (124 loc) · 4.98 KB
/
Copy pathkeyManagement.c
File metadata and controls
154 lines (124 loc) · 4.98 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include "keyManagement.h"
#include "encryption.h"
#include "main.h"
bool has_symbol(char c)
{
char* symbols = "!@#$%^&*()-_=+[{]};:'\",<.>/?`~";
for (int i=0;symbols[i] != '\0'; i++)
{
if(c==symbols[i])return true;
}
return false;
}
bool validate_password(char* pass)
{
int len=strlen(pass);
if (len<8)return false;
bool is_symbol=0;bool is_digit=0;bool is_capital=0;
for(int i=0;i<strlen(pass);i++)
{
if(isupper(pass[i]))is_capital=1;
if(isdigit(pass[i]))is_digit=1;
if(has_symbol(pass[i]))is_symbol=1;
if(is_capital&&is_digit&&is_symbol)break;
}
return is_capital && is_digit && is_symbol;
}
void get_hash_password(char* pass,unsigned char* hash,unsigned int* hash_len_actual)
{
const EVP_MD *md = EVP_sha256();
EVP_MD_CTX *mdctx = EVP_MD_CTX_new();
EVP_DigestInit_ex(mdctx, md,NULL);
EVP_DigestUpdate(mdctx, pass, strlen(pass));
EVP_DigestFinal_ex(mdctx, hash,hash_len_actual);
EVP_MD_CTX_free(mdctx);
}
char* set_password()
{
printf("Please enter a password:\n(Must be at least 8 characters, including at least one capital letter, one symbol, and one number)\n");
char* pass=getpass("");
while (validate_password(pass)==0)
{
printf("Invalid password! Try again!\n");
pass=getpass("");
}
printf("Password set successfully!\n");
return pass;
}
void set_key()
{
RAND_bytes(key,KEY_SIZE);
unsigned char iv[AES_BLOCK_SIZE];
RAND_bytes(iv,AES_BLOCK_SIZE);
char* file_path = malloc(sizeof(char)*MAX_PATH_SIZE);
getcwd(file_path,sizeof(char)*MAX_PATH_SIZE);
unsigned char salt[AES_BLOCK_SIZE];
RAND_bytes(salt,AES_BLOCK_SIZE);
char* pass=set_password();
//should also write the hash of the password to make sure the user entered the correct password
unsigned char* hash = malloc(sizeof(unsigned char)*HASH_SIZE);
unsigned int hash_len_actual;
get_hash_password(pass,hash,&hash_len_actual);
int digest=100000;
unsigned char wrapping_key[KEY_SIZE];
PKCS5_PBKDF2_HMAC(pass,strlen(pass),salt,AES_BLOCK_SIZE,digest,EVP_sha256(),KEY_SIZE,wrapping_key);
char* secret_path=malloc(MAX_PATH_SIZE);
snprintf(secret_path, MAX_PATH_SIZE, "%s/secret.key", file_path);
unsigned char encrypted_secret_key[KEY_SIZE + AES_BLOCK_SIZE];
encrypt_secret_key(key,iv,encrypted_secret_key);
FILE* out_file = fopen(secret_path,"wb");
fwrite(salt, sizeof(unsigned char),AES_BLOCK_SIZE, out_file); //writing salt
fwrite(&digest, sizeof(int), 1, out_file);//writing iteration count
fwrite(iv, sizeof(unsigned char),AES_BLOCK_SIZE, out_file); //writing iv
fwrite(encrypted_secret_key, sizeof(char), sizeof(encrypted_secret_key), out_file); //writing encrypted key
fwrite(hash,sizeof(unsigned char),HASH_SIZE, out_file); //writing the hash of the password
printf("Key generated successflly, and is stored at this directory:\n%s\n",getcwd(file_path,MAX_PATH_SIZE));
free(file_path);
free(hash);
free(secret_path);
fclose(out_file);
printf("Press any key to continue...\n");
getchar();
getchar();
}
void retrieve_key()
{
printf("Please enter your password:\n");
char* pass=getpass("");
unsigned char* hash = malloc(sizeof(unsigned char)*HASH_SIZE);
unsigned int hash_actual_len;
get_hash_password(pass,hash,&hash_actual_len);
char* file_path = malloc(sizeof(char)*MAX_PATH_SIZE);
getcwd(file_path, MAX_PATH_SIZE);
char* secret_path=malloc(MAX_PATH_SIZE);
snprintf(secret_path, MAX_PATH_SIZE, "%s/secret.key", file_path);
FILE* out_file = fopen(secret_path,"rb");
unsigned char salt[AES_BLOCK_SIZE];
unsigned char iv[AES_BLOCK_SIZE];
unsigned char wrapping_key[KEY_SIZE];
int digest;
char* encrypted_secret_key = malloc(KEY_SIZE + AES_BLOCK_SIZE);
unsigned char* retreived_hash = malloc(sizeof(unsigned char)*HASH_SIZE);
fread(salt, sizeof(unsigned char),AES_BLOCK_SIZE, out_file); //reading salt
fread(&digest, sizeof(int), 1, out_file); //reading iteration count
fread(iv, sizeof(unsigned char),AES_BLOCK_SIZE, out_file); //reading iv
fread(encrypted_secret_key,sizeof(char),sizeof(char)*(KEY_SIZE + AES_BLOCK_SIZE), out_file); //reading encrypted key
fread(retreived_hash,sizeof(unsigned char),sizeof(unsigned char)*HASH_SIZE, out_file); //reading the hash of the password
while(memcmp(hash,retreived_hash,HASH_SIZE)!=0){
printf("Incorrect password! Please try again!\n");
pass=getpass("");
int hash_len;
get_hash_password(pass,hash,&hash_len);
}
PKCS5_PBKDF2_HMAC(pass,strlen(pass),salt,AES_BLOCK_SIZE,digest,EVP_sha256(),KEY_SIZE,wrapping_key);
char decreypted_secret_key[KEY_SIZE];
decrypt_secret_key(wrapping_key,iv,encrypted_secret_key,decreypted_secret_key);
strcpy(key,decreypted_secret_key);
key_initialized=1;
free(hash);
free(file_path);
free(secret_path);
free(encrypted_secret_key);
free(retreived_hash);
fclose(out_file);
}