-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathffi_openssl.c
51 lines (48 loc) · 1.1 KB
/
ffi_openssl.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
#include <stdint.h>
#include <openssl/aes.h>
#include <openssl/modes.h>
void aesIgeDecryptRaw(
const unsigned char *src,
unsigned char *dst,
size_t len,
const unsigned char *key,
unsigned char *iv) {
AES_KEY aes;
AES_set_decrypt_key(key, 256, &aes);
AES_ige_encrypt(src, dst, len, &aes, iv, AES_DECRYPT);
}
static void incrementIv(unsigned char iv[16], uint64_t blockIndex)
{
if (!blockIndex) {
return;
}
int digits = 16;
uint64_t increment = blockIndex;
do {
--digits;
increment += iv[digits];
iv[digits] = increment & 0xFFULL;
increment >>= 8;
} while (digits != 0 && increment != 0);
}
void my_CRYPTO_ctr128_encrypt(
const unsigned char *src,
unsigned char *dst,
size_t len,
const unsigned char *key,
int keysize,
uint64_t block_index,
unsigned char *iv) {
AES_KEY aes;
AES_set_encrypt_key(key, keysize, &aes);
unsigned char ecount_buf[16] = {0};
unsigned int num = 0;
incrementIv(iv, block_index);
CRYPTO_ctr128_encrypt(
src, dst, len,
&aes, iv,
ecount_buf,
&num,
(block128_f)AES_encrypt
);
}