-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrypto.c
More file actions
78 lines (62 loc) · 2.05 KB
/
crypto.c
File metadata and controls
78 lines (62 loc) · 2.05 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
#include "crypto.h"
#define NUM_ITERS 65536
#define KEY_SIZE_BYTES 16
#define IV_SIZE_BYTES 16
#define SALT "l1pWK8SAkDR4UtE1uk9pPIc1qEMlxq7pwIJaoV1W4dspCYlnbs"
void crypto_init(){
ERR_load_crypto_strings();
OpenSSL_add_all_algorithms();
OPENSSL_config(NULL);
}
void crypto_deinit(){
EVP_cleanup();
ERR_free_strings();
}
int generateKey(const char *pass, const char *salt, char *out){
return PKCS5_PBKDF2_HMAC_SHA1(pass, -1,
(unsigned char *) salt, strlen(SALT) + 1, NUM_ITERS,
KEY_SIZE_BYTES, (unsigned char *) out);
}
char *getEncryptedBody(char *message){
return message + IV_SIZE_BYTES;
}
int decrypt(char *message, uint32_t message_size, char *password, char *plaintext){
EVP_CIPHER_CTX *ctx;
int len;
int plaintext_len;
char key[KEY_SIZE_BYTES];
int status = generateKey(password, SALT, key);
if(!status){
return -1;
}
const char *ciphertext = getEncryptedBody(message);
/* Create and initialise the context */
if(!(ctx = EVP_CIPHER_CTX_new())){
return -1;
}
/* Initialise the decryption operation. IMPORTANT - ensure you use a key
* and IV size appropriate for your cipher
* In this example we are using 256 bit AES (i.e. a 256 bit key). The
* IV size for *most* modes is the same as the block size. For AES this
* is 128 bits */
if(1 != EVP_DecryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, (unsigned char *) key, (unsigned char *) message)){
return -1;
}
/* Provide the message to be decrypted, and obtain the plaintext output.
* EVP_DecryptUpdate can be called multiple times if necessary
*/
if(1 != EVP_DecryptUpdate(ctx, (unsigned char *) plaintext, &len, (unsigned char *) ciphertext, message_size - IV_SIZE_BYTES)){
return -1;
}
plaintext_len = len;
/* Finalise the decryption. Further plaintext bytes may be written at
* this stage.
*/
if(1 != EVP_DecryptFinal_ex(ctx, (unsigned char *) plaintext + len, &len)){
return -1;
}
plaintext_len += len;
/* Clean up */
EVP_CIPHER_CTX_free(ctx);
return plaintext_len;
}