6
6
#include <stdbool.h>
7
7
#include "crypto.h"
8
8
9
+ #if OPENSSL_VERSION_NUMBER < 0x10100000L
10
+ HMAC_CTX * HMAC_CTX_new (void )
11
+ {
12
+ HMAC_CTX * ctx ;
13
+
14
+ ctx = OPENSSL_malloc (sizeof (HMAC_CTX ));
15
+ if (!ctx ) return NULL ;
16
+
17
+ HMAC_CTX_init (ctx );
18
+
19
+ return ctx ;
20
+ }
21
+
22
+ void HMAC_CTX_free (HMAC_CTX * ctx )
23
+ {
24
+ if (ctx == NULL ) return ;
25
+
26
+ HMAC_CTX_cleanup (ctx );
27
+ OPENSSL_free (ctx );
28
+ }
29
+
30
+ EVP_CIPHER_CTX * EVP_CIPHER_CTX_new (void )
31
+ {
32
+ EVP_CIPHER_CTX * ctx ;
33
+
34
+ ctx = OPENSSL_malloc (sizeof (EVP_CIPHER_CTX ));
35
+ if (!ctx ) return NULL ;
36
+
37
+ EVP_CIPHER_CTX_init (ctx );
38
+
39
+ return ctx ;
40
+ }
41
+
42
+ void EVP_CIPHER_CTX_free (EVP_CIPHER_CTX * ctx )
43
+ {
44
+ if (ctx == NULL ) return ;
45
+
46
+ EVP_CIPHER_CTX_cleanup (ctx );
47
+ OPENSSL_free (ctx );
48
+ }
49
+ #endif
50
+
9
51
struct seal_key {
10
52
const EVP_CIPHER * cipher ;
11
53
const EVP_MD * md ;
@@ -29,7 +71,7 @@ apr_status_t SEAL_KEY_CREATE(apr_pool_t *p, struct seal_key **skey,
29
71
goto done ;
30
72
}
31
73
32
- keylen = n -> cipher -> key_len ;
74
+ keylen = EVP_CIPHER_key_length ( n -> cipher ) ;
33
75
34
76
n -> md = EVP_sha256 ();
35
77
if (!n -> md ) {
@@ -81,24 +123,25 @@ apr_status_t SEAL_KEY_CREATE(apr_pool_t *p, struct seal_key **skey,
81
123
apr_status_t HMAC_BUFFER (struct seal_key * skey , struct databuf * buffer ,
82
124
struct databuf * result )
83
125
{
84
- HMAC_CTX hmac_ctx = { 0 } ;
126
+ HMAC_CTX * hmac_ctx ;
85
127
unsigned int len ;
86
- int ret ;
128
+ int ret = 0 ;
87
129
88
130
/* now MAC the buffer */
89
- HMAC_CTX_init (& hmac_ctx );
131
+ hmac_ctx = HMAC_CTX_new ();
132
+ if (!hmac_ctx ) goto done ;
90
133
91
- ret = HMAC_Init_ex (& hmac_ctx , skey -> hkey ,
92
- skey -> cipher -> key_len , skey -> md , NULL );
134
+ ret = HMAC_Init_ex (hmac_ctx , skey -> hkey ,
135
+ EVP_CIPHER_key_length ( skey -> cipher ) , skey -> md , NULL );
93
136
if (ret == 0 ) goto done ;
94
137
95
- ret = HMAC_Update (& hmac_ctx , buffer -> value , buffer -> length );
138
+ ret = HMAC_Update (hmac_ctx , buffer -> value , buffer -> length );
96
139
if (ret == 0 ) goto done ;
97
140
98
- ret = HMAC_Final (& hmac_ctx , result -> value , & len );
141
+ ret = HMAC_Final (hmac_ctx , result -> value , & len );
99
142
100
143
done :
101
- HMAC_CTX_cleanup ( & hmac_ctx );
144
+ HMAC_CTX_free ( hmac_ctx );
102
145
if (ret == 0 ) return EFAULT ;
103
146
104
147
result -> length = len ;
@@ -108,15 +151,15 @@ apr_status_t HMAC_BUFFER(struct seal_key *skey, struct databuf *buffer,
108
151
apr_status_t SEAL_BUFFER (apr_pool_t * p , struct seal_key * skey ,
109
152
struct databuf * plain , struct databuf * cipher )
110
153
{
111
- int blksz = skey -> cipher -> block_size ;
154
+ int blksz = EVP_CIPHER_block_size ( skey -> cipher ) ;
112
155
apr_status_t err = EFAULT ;
113
- EVP_CIPHER_CTX ctx = { 0 } ;
156
+ EVP_CIPHER_CTX * ctx ;
114
157
uint8_t rbuf [blksz ];
115
158
struct databuf hmacbuf ;
116
159
int outlen , totlen ;
117
160
int ret ;
118
161
119
- EVP_CIPHER_CTX_init ( & ctx );
162
+ ctx = EVP_CIPHER_CTX_new ( );
120
163
121
164
/* confounder to avoid exposing random numbers directly to clients
122
165
* as IVs */
@@ -126,30 +169,30 @@ apr_status_t SEAL_BUFFER(apr_pool_t *p, struct seal_key *skey,
126
169
if (cipher -> length == 0 ) {
127
170
/* add space for confounder and padding and MAC */
128
171
cipher -> length = (plain -> length / blksz + 2 ) * blksz ;
129
- cipher -> value = apr_palloc (p , cipher -> length + skey -> md -> md_size );
172
+ cipher -> value = apr_palloc (p , cipher -> length + EVP_MD_size ( skey -> md ) );
130
173
if (!cipher -> value ) {
131
174
err = ENOMEM ;
132
175
goto done ;
133
176
}
134
177
}
135
178
136
- ret = EVP_EncryptInit_ex (& ctx , skey -> cipher , NULL , skey -> ekey , NULL );
179
+ ret = EVP_EncryptInit_ex (ctx , skey -> cipher , NULL , skey -> ekey , NULL );
137
180
if (ret == 0 ) goto done ;
138
181
totlen = 0 ;
139
182
140
183
outlen = cipher -> length ;
141
- ret = EVP_EncryptUpdate (& ctx , cipher -> value , & outlen , rbuf , sizeof (rbuf ));
184
+ ret = EVP_EncryptUpdate (ctx , cipher -> value , & outlen , rbuf , sizeof (rbuf ));
142
185
if (ret == 0 ) goto done ;
143
186
totlen += outlen ;
144
187
145
188
outlen = cipher -> length - totlen ;
146
- ret = EVP_EncryptUpdate (& ctx , & cipher -> value [totlen ], & outlen ,
189
+ ret = EVP_EncryptUpdate (ctx , & cipher -> value [totlen ], & outlen ,
147
190
plain -> value , plain -> length );
148
191
if (ret == 0 ) goto done ;
149
192
totlen += outlen ;
150
193
151
194
outlen = cipher -> length - totlen ;
152
- ret = EVP_EncryptFinal_ex (& ctx , & cipher -> value [totlen ], & outlen );
195
+ ret = EVP_EncryptFinal_ex (ctx , & cipher -> value [totlen ], & outlen );
153
196
if (ret == 0 ) goto done ;
154
197
totlen += outlen ;
155
198
@@ -163,36 +206,38 @@ apr_status_t SEAL_BUFFER(apr_pool_t *p, struct seal_key *skey,
163
206
err = 0 ;
164
207
165
208
done :
166
- EVP_CIPHER_CTX_cleanup ( & ctx );
209
+ EVP_CIPHER_CTX_free ( ctx );
167
210
return err ;
168
211
}
169
212
170
213
apr_status_t UNSEAL_BUFFER (apr_pool_t * p , struct seal_key * skey ,
171
214
struct databuf * cipher , struct databuf * plain )
172
215
{
173
216
apr_status_t err = EFAULT ;
174
- EVP_CIPHER_CTX ctx = { 0 };
175
- unsigned char mac [skey -> md -> md_size ];
217
+ EVP_CIPHER_CTX * ctx = NULL ;
218
+ int blksz = EVP_CIPHER_block_size (skey -> cipher );
219
+ int md_size = EVP_MD_size (skey -> md );
220
+ unsigned char mac [md_size ];
176
221
struct databuf hmacbuf ;
177
222
int outlen , totlen ;
178
223
volatile bool equal = true;
179
224
int ret , i ;
180
225
181
226
/* check MAC first */
182
- cipher -> length -= skey -> md -> md_size ;
227
+ cipher -> length -= md_size ;
183
228
hmacbuf .value = mac ;
184
229
ret = HMAC_BUFFER (skey , cipher , & hmacbuf );
185
230
if (ret != 0 ) goto done ;
186
231
187
- if (hmacbuf .length != skey -> md -> md_size ) goto done ;
188
- for (i = 0 ; i < skey -> md -> md_size ; i ++ ) {
232
+ if (hmacbuf .length != md_size ) goto done ;
233
+ for (i = 0 ; i < md_size ; i ++ ) {
189
234
if (cipher -> value [cipher -> length + i ] != mac [i ]) equal = false;
190
235
/* not breaking intentionally,
191
236
* or we would allow an oracle attack */
192
237
}
193
238
if (!equal ) goto done ;
194
239
195
- EVP_CIPHER_CTX_init ( & ctx );
240
+ ctx = EVP_CIPHER_CTX_new ( );
196
241
197
242
if (plain -> length == 0 ) {
198
243
plain -> length = cipher -> length ;
@@ -203,37 +248,37 @@ apr_status_t UNSEAL_BUFFER(apr_pool_t *p, struct seal_key *skey,
203
248
}
204
249
}
205
250
206
- ret = EVP_DecryptInit_ex (& ctx , skey -> cipher , NULL , skey -> ekey , NULL );
251
+ ret = EVP_DecryptInit_ex (ctx , skey -> cipher , NULL , skey -> ekey , NULL );
207
252
if (ret == 0 ) goto done ;
208
253
209
254
totlen = 0 ;
210
255
outlen = plain -> length ;
211
- ret = EVP_DecryptUpdate (& ctx , plain -> value , & outlen ,
256
+ ret = EVP_DecryptUpdate (ctx , plain -> value , & outlen ,
212
257
cipher -> value , cipher -> length );
213
258
if (ret == 0 ) goto done ;
214
259
215
260
totlen += outlen ;
216
261
outlen = plain -> length - totlen ;
217
- ret = EVP_DecryptFinal_ex (& ctx , plain -> value , & outlen );
262
+ ret = EVP_DecryptFinal_ex (ctx , plain -> value , & outlen );
218
263
if (ret == 0 ) goto done ;
219
264
220
265
totlen += outlen ;
221
266
/* now remove the confounder */
222
- totlen -= skey -> cipher -> block_size ;
223
- memmove (plain -> value , plain -> value + skey -> cipher -> block_size , totlen );
267
+ totlen -= blksz ;
268
+ memmove (plain -> value , plain -> value + blksz , totlen );
224
269
225
270
plain -> length = totlen ;
226
271
err = 0 ;
227
272
228
273
done :
229
- EVP_CIPHER_CTX_cleanup ( & ctx );
274
+ EVP_CIPHER_CTX_free ( ctx );
230
275
return err ;
231
276
}
232
277
233
278
int get_mac_size (struct seal_key * skey )
234
279
{
235
280
if (skey ) {
236
- return skey -> md -> md_size ;
281
+ return EVP_MD_size ( skey -> md ) ;
237
282
} else {
238
283
return 0 ;
239
284
}
0 commit comments