20
20
static uint64_t ngx_http_encrypted_session_ntohll (uint64_t n );
21
21
static uint64_t ngx_http_encrypted_session_htonll (uint64_t n );
22
22
23
+ const EVP_CIPHER *
24
+ ngx_http_encrypted_session_get_cipher (enum ngx_http_encrypted_session_mode mode )
25
+ {
26
+ if (mode == ngx_http_encrypted_session_mode_cbc )
27
+ {
28
+ return EVP_aes_256_cbc ();
29
+ }
30
+ else if (mode == ngx_http_encrypted_session_mode_gcm )
31
+ {
32
+ return EVP_aes_256_gcm ();
33
+ }
34
+
35
+ return NULL ;
36
+ }
23
37
24
38
ngx_int_t
25
39
ngx_http_encrypted_session_aes_mac_encrypt (
26
40
ngx_http_encrypted_session_main_conf_t * emcf , ngx_pool_t * pool ,
27
41
ngx_log_t * log , const u_char * iv , size_t iv_len , const u_char * key ,
28
42
size_t key_len , const u_char * in , size_t in_len , ngx_uint_t expires ,
29
- u_char * * dst , size_t * dst_len )
43
+ enum ngx_http_encrypted_session_mode mode ,
44
+ u_char * * dst , size_t * dst_len , u_char * * tag )
30
45
{
31
46
const EVP_CIPHER * cipher ;
32
47
u_char * p , * data ;
@@ -50,7 +65,10 @@ ngx_http_encrypted_session_aes_mac_encrypt(
50
65
}
51
66
}
52
67
53
- cipher = EVP_aes_256_cbc ();
68
+ cipher = ngx_http_encrypted_session_get_cipher (mode );
69
+ if (!cipher ) {
70
+ goto evp_error ;
71
+ }
54
72
55
73
block_size = EVP_CIPHER_block_size (cipher );
56
74
@@ -107,6 +125,15 @@ ngx_http_encrypted_session_aes_mac_encrypt(
107
125
p += len ;
108
126
109
127
ret = EVP_EncryptFinal (emcf -> session_ctx , p , & len );
128
+ if (!ret ) {
129
+ goto evp_error ;
130
+ }
131
+
132
+ if (mode == ngx_http_encrypted_session_mode_gcm ) {
133
+ * tag = (u_char * )ngx_pcalloc (pool , ngx_http_encrypted_session_aes_tag_size );
134
+ ret = EVP_CIPHER_CTX_ctrl (emcf -> session_ctx , EVP_CTRL_GCM_GET_TAG ,
135
+ ngx_http_encrypted_session_aes_tag_size , * tag );
136
+ }
110
137
111
138
emcf -> reset_cipher_ctx (emcf -> session_ctx );
112
139
@@ -139,8 +166,10 @@ ngx_int_t
139
166
ngx_http_encrypted_session_aes_mac_decrypt (
140
167
ngx_http_encrypted_session_main_conf_t * emcf , ngx_pool_t * pool ,
141
168
ngx_log_t * log , const u_char * iv , size_t iv_len , const u_char * key ,
142
- size_t key_len , const u_char * in , size_t in_len , u_char * * dst ,
143
- size_t * dst_len )
169
+ size_t key_len , const u_char * in , size_t in_len ,
170
+ enum ngx_http_encrypted_session_mode mode ,
171
+ u_char * tag ,
172
+ u_char * * dst , size_t * dst_len )
144
173
{
145
174
const EVP_CIPHER * cipher ;
146
175
int ret ;
@@ -171,7 +200,10 @@ ngx_http_encrypted_session_aes_mac_decrypt(
171
200
}
172
201
}
173
202
174
- cipher = EVP_aes_256_cbc ();
203
+ cipher = ngx_http_encrypted_session_get_cipher (mode );
204
+ if (!cipher ) {
205
+ goto evp_error ;
206
+ }
175
207
176
208
ret = EVP_DecryptInit (emcf -> session_ctx , cipher , key , iv );
177
209
if (!ret ) {
@@ -200,6 +232,14 @@ ngx_http_encrypted_session_aes_mac_decrypt(
200
232
201
233
p += len ;
202
234
235
+ if (mode == ngx_http_encrypted_session_mode_gcm ) {
236
+ ret = EVP_CIPHER_CTX_ctrl (emcf -> session_ctx , EVP_CTRL_GCM_SET_TAG ,
237
+ ngx_http_encrypted_session_aes_tag_size , tag );
238
+ if (!ret ) {
239
+ goto evp_error ;
240
+ }
241
+ }
242
+
203
243
ret = EVP_DecryptFinal (emcf -> session_ctx , p , & len );
204
244
205
245
emcf -> reset_cipher_ctx (emcf -> session_ctx );
0 commit comments