-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrypto.c
More file actions
146 lines (121 loc) · 3.59 KB
/
crypto.c
File metadata and controls
146 lines (121 loc) · 3.59 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
#include "crypto.h"
#include <openssl/aes.h>
#include <openssl/evp.h>
#include <openssl/rand.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
bool crypto_init_done = false;
void Crypto_Init(void) {
if (!crypto_init_done) {
crypto_init_done = true;
}
}
void Crypto_Cleanup(void) { crypto_init_done = false; }
unsigned char *Crypto_EncryptAES256(const unsigned char *plaintext,
int plaintext_len, const unsigned char *key,
int *out_len) {
EVP_CIPHER_CTX *ctx = NULL;
int len;
int ciphertext_len;
unsigned char iv[AES_BLOCK_SIZE];
// Generate random IV
if (!RAND_bytes(iv, sizeof(iv))) {
return NULL;
}
// Max encrypted size is plaintext_len + block_size (padding) + IV size
unsigned char *ciphertext =
malloc(plaintext_len + AES_BLOCK_SIZE + sizeof(iv));
if (!ciphertext)
return NULL;
// Prepend IV
memcpy(ciphertext, iv, sizeof(iv));
if (!(ctx = EVP_CIPHER_CTX_new())) {
free(ciphertext);
return NULL;
}
if (1 != EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv)) {
EVP_CIPHER_CTX_free(ctx);
free(ciphertext);
return NULL;
}
if (1 != EVP_EncryptUpdate(ctx, ciphertext + sizeof(iv), &len, plaintext,
plaintext_len)) {
EVP_CIPHER_CTX_free(ctx);
free(ciphertext);
return NULL;
}
ciphertext_len = len;
if (1 != EVP_EncryptFinal_ex(ctx, ciphertext + sizeof(iv) + len, &len)) {
EVP_CIPHER_CTX_free(ctx);
free(ciphertext);
return NULL;
}
ciphertext_len += len;
EVP_CIPHER_CTX_free(ctx);
*out_len = ciphertext_len + sizeof(iv);
return ciphertext;
}
unsigned char *Crypto_DecryptAES256(const unsigned char *ciphertext_with_iv,
int ciphertext_len,
const unsigned char *key, int *out_len) {
EVP_CIPHER_CTX *ctx = NULL;
int len;
int plaintext_len;
unsigned char iv[AES_BLOCK_SIZE];
if (ciphertext_len < (int)sizeof(iv)) {
return NULL; // Too short to even contain IV
}
// Extract IV
memcpy(iv, ciphertext_with_iv, sizeof(iv));
const unsigned char *actual_ciphertext = ciphertext_with_iv + sizeof(iv);
int actual_ciphertext_len = ciphertext_len - sizeof(iv);
unsigned char *plaintext = malloc(actual_ciphertext_len + AES_BLOCK_SIZE);
if (!plaintext)
return NULL;
if (!(ctx = EVP_CIPHER_CTX_new())) {
free(plaintext);
return NULL;
}
if (1 != EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv)) {
EVP_CIPHER_CTX_free(ctx);
free(plaintext);
return NULL;
}
if (1 != EVP_DecryptUpdate(ctx, plaintext, &len, actual_ciphertext,
actual_ciphertext_len)) {
EVP_CIPHER_CTX_free(ctx);
free(plaintext);
return NULL;
}
plaintext_len = len;
if (1 != EVP_DecryptFinal_ex(ctx, plaintext + len, &len)) {
EVP_CIPHER_CTX_free(ctx);
free(plaintext);
return NULL;
}
plaintext_len += len;
EVP_CIPHER_CTX_free(ctx);
plaintext[plaintext_len] = '\0';
*out_len = plaintext_len;
return plaintext;
}
void Crypto_XOR(unsigned char *data, size_t data_len, const unsigned char *key,
size_t key_len) {
if (key_len == 0)
return;
for (size_t i = 0; i < data_len; ++i) {
data[i] ^= key[i % key_len];
}
}
uint32_t Crypto_CRC32(const unsigned char *data, size_t length) {
uint32_t crc = 0xFFFFFFFF;
for (size_t i = 0; i < length; i++) {
crc ^= data[i];
for (int j = 0; j < 8; j++) {
if (crc & 1) crc = (crc >> 1) ^ 0xEDB88320;
else crc >>= 1;
}
}
return ~crc;
}