summaryrefslogtreecommitdiff
blob: 5d17cb426e887936969d56cc215fa5033ecefc56 (plain)
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
Backport of https://github.com/cisco/libsrtp/commit/0b45423678ddc46d702f3a51614f20bfbd112ddd

--- a/crypto/cipher/aes_gcm_ossl.c	2018-06-11 07:46:09 UTC
+++ b/crypto/cipher/aes_gcm_ossl.c
@@ -117,6 +117,14 @@ err_status_t aes_gcm_openssl_alloc (cipher_t **c, int 
     }
     memset(gcm, 0x0, sizeof(aes_gcm_ctx_t));
 
+    gcm->ctx = EVP_CIPHER_CTX_new();
+    if (gcm->ctx == NULL) {
+        crypto_free(gcm);
+        crypto_free(*c);
+        *c = NULL;
+        return err_status_alloc_fail;
+    }
+
     /* set pointers */
     (*c)->state = gcm;
 
@@ -140,7 +148,6 @@ err_status_t aes_gcm_openssl_alloc (cipher_t **c, int 
 
     /* set key size        */
     (*c)->key_len = key_len;
-    EVP_CIPHER_CTX_init(&gcm->ctx);
 
     return (err_status_ok);
 }
@@ -155,7 +162,7 @@ err_status_t aes_gcm_openssl_dealloc (cipher_t *c)
 
     ctx = (aes_gcm_ctx_t*)c->state;
     if (ctx) {
-	EVP_CIPHER_CTX_cleanup(&ctx->ctx);
+        EVP_CIPHER_CTX_free(ctx->ctx);
         /* decrement ref_count for the appropriate engine */
         switch (ctx->key_size) {
         case AES_256_KEYSIZE:
@@ -205,7 +212,7 @@ err_status_t aes_gcm_openssl_context_init (aes_gcm_ctx
         break;
     }
 
-    if (!EVP_CipherInit_ex(&c->ctx, evp, NULL, key, NULL, 0)) {
+    if (!EVP_CipherInit_ex(c->ctx, evp, NULL, key, NULL, 0)) {
         return (err_status_init_fail);
     }
 
@@ -227,19 +234,19 @@ err_status_t aes_gcm_openssl_set_iv (aes_gcm_ctx_t *c,
 
     debug_print(mod_aes_gcm, "setting iv: %s", v128_hex_string(iv));
 
-    if (!EVP_CipherInit_ex(&c->ctx, NULL, NULL, NULL,
+    if (!EVP_CipherInit_ex(c->ctx, NULL, NULL, NULL,
                            NULL, (c->dir == direction_encrypt ? 1 : 0))) {
         return (err_status_init_fail);
     }
 
     /* set IV len  and the IV value, the followiong 3 calls are required */
-    if (!EVP_CIPHER_CTX_ctrl(&c->ctx, EVP_CTRL_GCM_SET_IVLEN, 12, 0)) {
+    if (!EVP_CIPHER_CTX_ctrl(c->ctx, EVP_CTRL_GCM_SET_IVLEN, 12, 0)) {
         return (err_status_init_fail);
     }
-    if (!EVP_CIPHER_CTX_ctrl(&c->ctx, EVP_CTRL_GCM_SET_IV_FIXED, -1, iv)) {
+    if (!EVP_CIPHER_CTX_ctrl(c->ctx, EVP_CTRL_GCM_SET_IV_FIXED, -1, iv)) {
         return (err_status_init_fail);
     }
-    if (!EVP_CIPHER_CTX_ctrl(&c->ctx, EVP_CTRL_GCM_IV_GEN, 0, iv)) {
+    if (!EVP_CIPHER_CTX_ctrl(c->ctx, EVP_CTRL_GCM_IV_GEN, 0, iv)) {
         return (err_status_init_fail);
     }
 
@@ -263,9 +270,9 @@ err_status_t aes_gcm_openssl_set_aad (aes_gcm_ctx_t *c
      * Set dummy tag, OpenSSL requires the Tag to be set before
      * processing AAD
      */
-    EVP_CIPHER_CTX_ctrl(&c->ctx, EVP_CTRL_GCM_SET_TAG, c->tag_len, aad);
+    EVP_CIPHER_CTX_ctrl(c->ctx, EVP_CTRL_GCM_SET_TAG, c->tag_len, aad);
 
-    rv = EVP_Cipher(&c->ctx, NULL, aad, aad_len);
+    rv = EVP_Cipher(c->ctx, NULL, aad, aad_len);
     if (rv != aad_len) {
         return (err_status_algo_fail);
     } else {
@@ -291,7 +298,7 @@ err_status_t aes_gcm_openssl_encrypt (aes_gcm_ctx_t *c
     /*
      * Encrypt the data
      */
-    EVP_Cipher(&c->ctx, buf, buf, *enc_len);
+    EVP_Cipher(c->ctx, buf, buf, *enc_len);
 
     return (err_status_ok);
 }
@@ -313,12 +320,12 @@ err_status_t aes_gcm_openssl_get_tag (aes_gcm_ctx_t *c
     /*
      * Calculate the tag
      */
-    EVP_Cipher(&c->ctx, NULL, NULL, 0);
+    EVP_Cipher(c->ctx, NULL, NULL, 0);
 
     /*
      * Retreive the tag
      */
-    EVP_CIPHER_CTX_ctrl(&c->ctx, EVP_CTRL_GCM_GET_TAG, c->tag_len, buf);
+    EVP_CIPHER_CTX_ctrl(c->ctx, EVP_CTRL_GCM_GET_TAG, c->tag_len, buf);
 
     /*
      * Increase encryption length by desired tag size
@@ -347,14 +354,14 @@ err_status_t aes_gcm_openssl_decrypt (aes_gcm_ctx_t *c
     /*
      * Set the tag before decrypting
      */
-    EVP_CIPHER_CTX_ctrl(&c->ctx, EVP_CTRL_GCM_SET_TAG, c->tag_len, 
+    EVP_CIPHER_CTX_ctrl(c->ctx, EVP_CTRL_GCM_SET_TAG, c->tag_len,
 	                buf + (*enc_len - c->tag_len));
-    EVP_Cipher(&c->ctx, buf, buf, *enc_len - c->tag_len);
+    EVP_Cipher(c->ctx, buf, buf, *enc_len - c->tag_len);
 
     /*
      * Check the tag
      */
-    if (EVP_Cipher(&c->ctx, NULL, NULL, 0)) {
+    if (EVP_Cipher(c->ctx, NULL, NULL, 0)) {
         return (err_status_auth_fail);
     }
 
--- a/crypto/cipher/aes_icm_ossl.c	2018-06-11 07:46:00 UTC
+++ b/crypto/cipher/aes_icm_ossl.c
@@ -144,6 +144,14 @@ err_status_t aes_icm_openssl_alloc (cipher_t **c, int 
     }
     memset(icm, 0x0, sizeof(aes_icm_ctx_t));
 
+    icm->ctx = EVP_CIPHER_CTX_new();
+    if (icm->ctx == NULL) {
+        crypto_free(icm);
+        crypto_free(*c);
+        *c = NULL;
+        return err_status_alloc_fail;
+    }
+
     /* set pointers */
     (*c)->state = icm;
 
@@ -173,7 +181,6 @@ err_status_t aes_icm_openssl_alloc (cipher_t **c, int 
 
     /* set key size        */
     (*c)->key_len = key_len;
-    EVP_CIPHER_CTX_init(&icm->ctx);
 
     return err_status_ok;
 }
@@ -195,7 +202,7 @@ err_status_t aes_icm_openssl_dealloc (cipher_t *c)
      */
     ctx = (aes_icm_ctx_t*)c->state;
     if (ctx != NULL) {
-        EVP_CIPHER_CTX_cleanup(&ctx->ctx);
+        EVP_CIPHER_CTX_free(ctx->ctx);
         /* decrement ref_count for the appropriate engine */
         switch (ctx->key_size) {
         case AES_256_KEYSIZE:
@@ -257,8 +264,6 @@ err_status_t aes_icm_openssl_context_init (aes_icm_ctx
     debug_print(mod_aes_icm, "key:  %s", octet_string_hex_string(key, c->key_size));
     debug_print(mod_aes_icm, "offset: %s", v128_hex_string(&c->offset));
 
-    EVP_CIPHER_CTX_init(&c->ctx);
-
     switch (c->key_size) {
     case AES_256_KEYSIZE:
         evp = EVP_aes_256_ctr();
@@ -276,7 +281,7 @@ err_status_t aes_icm_openssl_context_init (aes_icm_ctx
         break;
     }
 
-    if (!EVP_EncryptInit_ex(&c->ctx, evp,
+    if (!EVP_EncryptInit_ex(c->ctx, evp,
                             NULL, key, NULL)) {
         return err_status_fail;
     } else {
@@ -304,7 +309,7 @@ err_status_t aes_icm_openssl_set_iv (aes_icm_ctx_t *c,
 
     debug_print(mod_aes_icm, "set_counter: %s", v128_hex_string(&c->counter));
 
-    if (!EVP_EncryptInit_ex(&c->ctx, NULL,
+    if (!EVP_EncryptInit_ex(c->ctx, NULL,
                             NULL, NULL, c->counter.v8)) {
         return err_status_fail;
     } else {
@@ -326,12 +331,12 @@ err_status_t aes_icm_openssl_encrypt (aes_icm_ctx_t *c
 
     debug_print(mod_aes_icm, "rs0: %s", v128_hex_string(&c->counter));
 
-    if (!EVP_EncryptUpdate(&c->ctx, buf, &len, buf, *enc_len)) {
+    if (!EVP_EncryptUpdate(c->ctx, buf, &len, buf, *enc_len)) {
         return err_status_cipher_fail;
     }
     *enc_len = len;
 
-    if (!EVP_EncryptFinal_ex(&c->ctx, buf, &len)) {
+    if (!EVP_EncryptFinal_ex(c->ctx, buf, &len)) {
         return err_status_cipher_fail;
     }
     *enc_len += len;
--- a/crypto/hash/hmac_ossl.c	2018-06-11 07:45:39 UTC
+++ b/crypto/hash/hmac_ossl.c
@@ -65,8 +65,6 @@ err_status_t
 hmac_alloc (auth_t **a, int key_len, int out_len)
 {
     extern auth_type_t hmac;
-    uint8_t *pointer;
-    HMAC_CTX *new_hmac_ctx;
 
     debug_print(mod_hmac, "allocating auth func with key length %d", key_len);
     debug_print(mod_hmac, "                          tag length %d", out_len);
@@ -76,21 +74,43 @@ hmac_alloc (auth_t **a, int key_len, int out_len)
         return err_status_bad_param;
     }
 
-    /* allocate memory for auth and HMAC_CTX structures */
-    pointer = (uint8_t*)crypto_alloc(sizeof(HMAC_CTX) + sizeof(auth_t));
-    if (pointer == NULL) {
+/* OpenSSL 1.1.0 made HMAC_CTX an opaque structure, which must be allocated
+   using HMAC_CTX_new.  But this function doesn't exist in OpenSSL 1.0.x. */
+#if OPENSSL_VERSION_NUMBER < 0x10100000L || (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x2070000fL)
+    {
+        /* allocate memory for auth and HMAC_CTX structures */
+        uint8_t* pointer;
+        HMAC_CTX *new_hmac_ctx;
+        pointer = (uint8_t*)crypto_alloc(sizeof(HMAC_CTX) + sizeof(auth_t));
+        if (pointer == NULL) {
+            return err_status_alloc_fail;
+        }
+        *a = (auth_t*)pointer;
+        (*a)->state = pointer + sizeof(auth_t);
+        new_hmac_ctx = (HMAC_CTX*)((*a)->state);
+
+        HMAC_CTX_init(new_hmac_ctx);
+    }
+
+#else
+    *a = (auth_t*)crypto_alloc(sizeof(auth_t));
+    if (*a == NULL) {
         return err_status_alloc_fail;
     }
 
+    (*a)->state = HMAC_CTX_new();
+    if ((*a)->state == NULL) {
+        crypto_free(*a);
+        *a = NULL;
+        return err_status_alloc_fail;
+    }
+#endif
+
     /* set pointers */
-    *a = (auth_t*)pointer;
     (*a)->type = &hmac;
-    (*a)->state = pointer + sizeof(auth_t);
     (*a)->out_len = out_len;
     (*a)->key_len = key_len;
     (*a)->prefix_len = 0;
-    new_hmac_ctx = (HMAC_CTX*)((*a)->state);
-    HMAC_CTX_init(new_hmac_ctx);
 
     /* increment global count of all hmac uses */
     hmac.ref_count++;
@@ -106,11 +126,19 @@ hmac_dealloc (auth_t *a)
 
     hmac_ctx = (HMAC_CTX*)a->state;
 
+#if OPENSSL_VERSION_NUMBER < 0x10100000L || (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x2070000fL)
     HMAC_CTX_cleanup(hmac_ctx);
 
     /* zeroize entire state*/
     octet_string_set_to_zero((uint8_t*)a,
                              sizeof(HMAC_CTX) + sizeof(auth_t));
+
+#else
+    HMAC_CTX_free(hmac_ctx);
+
+    /* zeroize entire state*/
+    octet_string_set_to_zero((uint8_t*)a, sizeof(auth_t));
+#endif
 
     /* free memory */
     crypto_free(a);
--- a/crypto/include/aes_gcm_ossl.h	2018-06-11 07:46:09 UTC
+++ b/crypto/include/aes_gcm_ossl.h
@@ -54,7 +54,7 @@
 typedef struct {
   int      key_size;
   int      tag_len;
-  EVP_CIPHER_CTX ctx;
+  EVP_CIPHER_CTX* ctx;
   cipher_direction_t dir;
 } aes_gcm_ctx_t;
 
--- a/crypto/include/aes_icm_ossl.h	2018-06-11 07:46:00 UTC
+++ b/crypto/include/aes_icm_ossl.h
@@ -71,7 +71,7 @@ typedef struct {
     v128_t counter;                /* holds the counter value          */
     v128_t offset;                 /* initial offset value             */
     int key_size;
-    EVP_CIPHER_CTX ctx;
+    EVP_CIPHER_CTX* ctx;
 } aes_icm_ctx_t;
 
 err_status_t aes_icm_openssl_set_iv(aes_icm_ctx_t *c, void *iv, int dir);
--- a/crypto/include/sha1.h	2017-08-01 11:57:38 UTC
+++ b/crypto/include/sha1.h
@@ -56,8 +56,6 @@
 #include <openssl/evp.h>
 #include <stdint.h>
 
-typedef EVP_MD_CTX sha1_ctx_t;
-
 /*
  * sha1_init(&ctx) initializes the SHA1 context ctx
  *
@@ -72,6 +70,12 @@ typedef EVP_MD_CTX sha1_ctx_t;
  *
  */
 
+/* OpenSSL 1.1.0 made EVP_MD_CTX an opaque structure, which must be allocated
+   using EVP_MD_CTX_new. But this function doesn't exist in OpenSSL 1.0.x. */
+#if OPENSSL_VERSION_NUMBER < 0x10100000L || (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x2070000fL)
+
+typedef EVP_MD_CTX sha1_ctx_t;
+
 static inline void sha1_init (sha1_ctx_t *ctx)
 {
     EVP_MD_CTX_init(ctx);
@@ -88,7 +92,33 @@ static inline void sha1_final (sha1_ctx_t *ctx, uint32
     unsigned int len = 0;
 
     EVP_DigestFinal(ctx, (unsigned char*)output, &len);
+    EVP_MD_CTX_cleanup(ctx);
 }
+
+#else
+
+typedef EVP_MD_CTX* sha1_ctx_t;
+
+static inline void sha1_init (sha1_ctx_t *ctx)
+{
+    *ctx = EVP_MD_CTX_new();
+    EVP_DigestInit(*ctx, EVP_sha1());
+}
+
+static inline void sha1_update (sha1_ctx_t *ctx, const uint8_t *M, int octets_in_msg)
+{
+    EVP_DigestUpdate(*ctx, M, octets_in_msg);
+}
+
+static inline void sha1_final (sha1_ctx_t *ctx, uint32_t *output)
+{
+    unsigned int len = 0;
+
+    EVP_DigestFinal(*ctx, (unsigned char*)output, &len);
+    EVP_MD_CTX_free(*ctx);
+}
+#endif
+
 #else
 #include "datatypes.h"