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
|
int
aes256_gcm_encrypt (const uint8_t *plaintext, int plaintext_len,
const uint8_t *aad, int aad_len,
const uint8_t key[AES_256_KEY_SIZE],
const uint8_t *nonce, int nonce_len,
uint8_t *ciphertext,
uint8_t tag[GCM_TAG_SIZE])
{
int len = 0;
int ciphertext_len = 0;
int ok = 0;
if (!EVP_CIPHER_CTX_reset (cipher_ctx)) {
ERR_print_errors_fp (stderr);
return -1;
}
if (EVP_EncryptInit_ex2 (cipher_ctx, cipher_aes_gcm, NULL, NULL, NULL) != 1) {
ERR_print_errors_fp (stderr);
return -1;
}
/*
Set nonce length before setting key/nonce if using a non-default length.
For GCM, 12 bytes is the common default and recommended size.
*/
if (EVP_CIPHER_CTX_ctrl (cipher_ctx, EVP_CTRL_GCM_SET_IVLEN,
nonce_len, NULL) != 1) {
ERR_print_errors_fp (stderr);
return -1;
}
if (EVP_EncryptInit_ex (cipher_ctx, NULL, NULL, key, nonce) != 1) {
ERR_print_errors_fp (stderr);
return -1;
}
/*
AAD is authenticated but not encrypted.
Pass out=NULL for AAD.
*/
if (aad != NULL && aad_len > 0) {
if (EVP_EncryptUpdate (cipher_ctx, NULL, &len, aad, aad_len) != 1) {
ERR_print_errors_fp (stderr);
return -1;
}
}
if (EVP_EncryptUpdate (cipher_ctx, ciphertext, &len,
plaintext, plaintext_len) != 1) {
ERR_print_errors_fp (stderr);
return -1;
}
ciphertext_len = len;
/*
Final for GCM typically produces no extra ciphertext bytes, but it must
still be called.
*/
if (EVP_EncryptFinal_ex (cipher_ctx, ciphertext + ciphertext_len, &len) != 1) {
ERR_print_errors_fp (stderr);
return -1;
}
ciphertext_len += len;
/*
Get the authentication tag.
*/
if (EVP_CIPHER_CTX_ctrl (cipher_ctx, EVP_CTRL_GCM_GET_TAG,
GCM_TAG_SIZE, tag) != 1) {
ERR_print_errors_fp (stderr);
return -1;
}
return ciphertext_len;
}
int aes256_gcm_decrypt (const uint8_t *ciphertext, int ciphertext_len,
const uint8_t *aad, int aad_len,
const uint8_t tag[GCM_TAG_SIZE],
const uint8_t key[AES_256_KEY_SIZE],
const uint8_t *nonce, int nonce_len,
uint8_t *plaintext)
{
int len = 0;
int plaintext_len = 0;
int ret = -1;
if (EVP_CIPHER_CTX_reset (cipher_ctx)) {
ERR_print_errors_fp (stderr);
return -1;
}
if (EVP_DecryptInit_ex2 (cipher_ctx, cipher_aes_gcm, NULL, NULL, NULL) != 1) {
ERR_print_errors_fp (stderr);
return -1;
}
if (EVP_CIPHER_CTX_ctrl (cipher_ctx, EVP_CTRL_GCM_SET_IVLEN,
nonce_len, NULL) != 1) {
ERR_print_errors_fp (stderr);
return -1;
}
if (EVP_DecryptInit_ex2 (cipher_ctx, NULL, key, nonce, NULL) != 1) {
ERR_print_errors_fp (stderr);
return -1;
}
if (aad != NULL && aad_len > 0) {
if (EVP_DecryptUpdate (cipher_ctx, NULL, &len, aad, aad_len) != 1) {
ERR_print_errors_fp (stderr);
return -1;
}
}
if (EVP_DecryptUpdate (cipher_ctx, plaintext, &len,
ciphertext, ciphertext_len) != 1) {
ERR_print_errors_fp (stderr);
return -1;
}
plaintext_len = len;
/*
Set expected tag before finalizing.
*/
if (EVP_CIPHER_CTX_ctrl (cipher_ctx, EVP_CTRL_GCM_SET_TAG,
GCM_TAG_SIZE, (void *)tag) != 1) {
ERR_print_errors_fp (stderr);
return -1;
}
/*
Success only if tag verification passes.
*/
if (EVP_DecryptFinal_ex (cipher_ctx, plaintext + plaintext_len, &len) != 1) {
ERR_print_errors_fp (stderr);
return -1;
}
plaintext_len += len;
return plaintext_len;
}
|