-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_aes.c
71 lines (57 loc) · 1.83 KB
/
my_aes.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
71
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "com_h.h"
// context of encrypt & decrypt
EVP_CIPHER_CTX *en, *de;
// For SHA256, block_size = 256-bits, so size(key) = size(iv) = 32 Bytes
unsigned char *key, *iv;
void init_key_iv(){
key = (unsigned char *) malloc(32);
iv = (unsigned char *) malloc(32);
memset(key, 0, 32);
memset(iv, 0, 32);
}
void set_key(const unsigned char *val_pt){
memcpy(key, val_pt, 32);
}
void set_iv(const unsigned char *val_pt){
memcpy(iv, val_pt, 32);
}
void init_AES(){
en = EVP_CIPHER_CTX_new();
de = EVP_CIPHER_CTX_new();
EVP_CIPHER_CTX_init(en);
EVP_CIPHER_CTX_init(de);
EVP_EncryptInit_ex(en, EVP_aes_256_cbc(), NULL, key, iv);
EVP_DecryptInit_ex(de, EVP_aes_256_cbc(), NULL, key, iv);
}
void aes_encrypt(unsigned char *plaintext, int *len){
// ensure the upperbound_length
// AES_BLOCK_SIZE = 32 (Bytes)
int cipher_len = *len + AES_BLOCK_SIZE, additional_len = 0;
unsigned char *ciphertext = (unsigned char *) malloc(cipher_len);
/* re_init */
EVP_EncryptInit_ex(en, NULL, NULL, NULL, NULL);
EVP_EncryptUpdate(en, ciphertext, &cipher_len, plaintext, *len);
EVP_EncryptFinal_ex(en, ciphertext + cipher_len, &additional_len);
*len = cipher_len + additional_len;
memcpy(plaintext, ciphertext, *len);
free(ciphertext);
}
void aes_decrypt(unsigned char *ciphertext, int *len){
// ensure the upperbound_length
int plain_len = *len, additional_len = 0;
unsigned char *plaintext = (unsigned char *) malloc(plain_len);
/* re_init */
EVP_DecryptInit_ex(de, NULL, NULL, NULL, NULL);
EVP_DecryptUpdate(de, plaintext, &plain_len, ciphertext, *len);
EVP_DecryptFinal_ex(de, plaintext + plain_len, &additional_len);
*len = plain_len + additional_len;
memcpy(ciphertext, plaintext, *len);
free(plaintext);
}
void end_AES(){
free(key);
free(iv);
}