1 /*
2  * aes_icm_ossl.c
3  *
4  * AES Integer Counter Mode
5  *
6  * John A. Foley
7  * Cisco Systems, Inc.
8  *
9  * 2/24/2012:  This module was modified to use CiscoSSL for AES counter
10  *             mode.  Eddy Lem contributed the code to allow this.
11  *
12  * 12/20/2012: Added support for AES-192 and AES-256.
13  */
14 
15 /*
16  *
17  * Copyright (c) 2013-2017, Cisco Systems, Inc.
18  * All rights reserved.
19  *
20  * Redistribution and use in source and binary forms, with or without
21  * modification, are permitted provided that the following conditions
22  * are met:
23  *
24  *   Redistributions of source code must retain the above copyright
25  *   notice, this list of conditions and the following disclaimer.
26  *
27  *   Redistributions in binary form must reproduce the above
28  *   copyright notice, this list of conditions and the following
29  *   disclaimer in the documentation and/or other materials provided
30  *   with the distribution.
31  *
32  *   Neither the name of the Cisco Systems, Inc. nor the names of its
33  *   contributors may be used to endorse or promote products derived
34  *   from this software without specific prior written permission.
35  *
36  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
37  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
38  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
39  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
40  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
41  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
42  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
43  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47  * OF THE POSSIBILITY OF SUCH DAMAGE.
48  *
49  */
50 
51 #ifdef HAVE_CONFIG_H
52 #include <config.h>
53 #endif
54 
55 #include <openssl/evp.h>
56 #include "aes_icm_ext.h"
57 #include "crypto_types.h"
58 #include "err.h" /* for srtp_debug */
59 #include "alloc.h"
60 #include "cipher_types.h"
61 #include "cipher_test_cases.h"
62 
63 srtp_debug_module_t srtp_mod_aes_icm = {
64     0,             /* debugging is off by default */
65     "aes icm ossl" /* printable module name       */
66 };
67 
68 /*
69  * integer counter mode works as follows:
70  *
71  * 16 bits
72  * <----->
73  * +------+------+------+------+------+------+------+------+
74  * |           nonce           |    packet index    |  ctr |---+
75  * +------+------+------+------+------+------+------+------+   |
76  *                                                             |
77  * +------+------+------+------+------+------+------+------+   v
78  * |                      salt                      |000000|->(+)
79  * +------+------+------+------+------+------+------+------+   |
80  *                                                             |
81  *                                                        +---------+
82  *                                                        | encrypt |
83  *                                                        +---------+
84  *                                                             |
85  * +------+------+------+------+------+------+------+------+   |
86  * |                    keystream block                    |<--+
87  * +------+------+------+------+------+------+------+------+
88  *
89  * All fields are big-endian
90  *
91  * ctr is the block counter, which increments from zero for
92  * each packet (16 bits wide)
93  *
94  * packet index is distinct for each packet (48 bits wide)
95  *
96  * nonce can be distinct across many uses of the same key, or
97  * can be a fixed value per key, or can be per-packet randomness
98  * (64 bits)
99  *
100  */
101 
102 /*
103  * This function allocates a new instance of this crypto engine.
104  * The key_len parameter should be one of 30, 38, or 46 for
105  * AES-128, AES-192, and AES-256 respectively.  Note, this key_len
106  * value is inflated, as it also accounts for the 112 bit salt
107  * value.  The tlen argument is for the AEAD tag length, which
108  * isn't used in counter mode.
109  */
srtp_aes_icm_openssl_alloc(srtp_cipher_t ** c,int key_len,int tlen)110 static srtp_err_status_t srtp_aes_icm_openssl_alloc(srtp_cipher_t **c,
111                                                     int key_len,
112                                                     int tlen)
113 {
114     srtp_aes_icm_ctx_t *icm;
115 
116     debug_print(srtp_mod_aes_icm, "allocating cipher with key length %d",
117                 key_len);
118 
119     /*
120      * Verify the key_len is valid for one of: AES-128/192/256
121      */
122     if (key_len != SRTP_AES_ICM_128_KEY_LEN_WSALT &&
123         key_len != SRTP_AES_ICM_192_KEY_LEN_WSALT &&
124         key_len != SRTP_AES_ICM_256_KEY_LEN_WSALT) {
125         return srtp_err_status_bad_param;
126     }
127 
128     /* allocate memory a cipher of type aes_icm */
129     *c = (srtp_cipher_t *)srtp_crypto_alloc(sizeof(srtp_cipher_t));
130     if (*c == NULL) {
131         return srtp_err_status_alloc_fail;
132     }
133 
134     icm = (srtp_aes_icm_ctx_t *)srtp_crypto_alloc(sizeof(srtp_aes_icm_ctx_t));
135     if (icm == NULL) {
136         srtp_crypto_free(*c);
137         *c = NULL;
138         return srtp_err_status_alloc_fail;
139     }
140 
141     icm->ctx = EVP_CIPHER_CTX_new();
142     if (icm->ctx == NULL) {
143         srtp_crypto_free(icm);
144         srtp_crypto_free(*c);
145         *c = NULL;
146         return srtp_err_status_alloc_fail;
147     }
148 
149     /* set pointers */
150     (*c)->state = icm;
151 
152     /* setup cipher parameters */
153     switch (key_len) {
154     case SRTP_AES_ICM_128_KEY_LEN_WSALT:
155         (*c)->algorithm = SRTP_AES_ICM_128;
156         (*c)->type = &srtp_aes_icm_128;
157         icm->key_size = SRTP_AES_128_KEY_LEN;
158         break;
159     case SRTP_AES_ICM_192_KEY_LEN_WSALT:
160         (*c)->algorithm = SRTP_AES_ICM_192;
161         (*c)->type = &srtp_aes_icm_192;
162         icm->key_size = SRTP_AES_192_KEY_LEN;
163         break;
164     case SRTP_AES_ICM_256_KEY_LEN_WSALT:
165         (*c)->algorithm = SRTP_AES_ICM_256;
166         (*c)->type = &srtp_aes_icm_256;
167         icm->key_size = SRTP_AES_256_KEY_LEN;
168         break;
169     }
170 
171     /* set key size        */
172     (*c)->key_len = key_len;
173 
174     return srtp_err_status_ok;
175 }
176 
177 /*
178  * This function deallocates an instance of this engine
179  */
srtp_aes_icm_openssl_dealloc(srtp_cipher_t * c)180 static srtp_err_status_t srtp_aes_icm_openssl_dealloc(srtp_cipher_t *c)
181 {
182     srtp_aes_icm_ctx_t *ctx;
183 
184     if (c == NULL) {
185         return srtp_err_status_bad_param;
186     }
187 
188     /*
189      * Free the EVP context
190      */
191     ctx = (srtp_aes_icm_ctx_t *)c->state;
192     if (ctx != NULL) {
193         EVP_CIPHER_CTX_free(ctx->ctx);
194         /* zeroize the key material */
195         octet_string_set_to_zero(ctx, sizeof(srtp_aes_icm_ctx_t));
196         srtp_crypto_free(ctx);
197     }
198 
199     /* free memory */
200     srtp_crypto_free(c);
201 
202     return srtp_err_status_ok;
203 }
204 
205 /*
206  * aes_icm_openssl_context_init(...) initializes the aes_icm_context
207  * using the value in key[].
208  *
209  * the key is the secret key
210  *
211  * the salt is unpredictable (but not necessarily secret) data which
212  * randomizes the starting point in the keystream
213  */
srtp_aes_icm_openssl_context_init(void * cv,const uint8_t * key)214 static srtp_err_status_t srtp_aes_icm_openssl_context_init(void *cv,
215                                                            const uint8_t *key)
216 {
217     srtp_aes_icm_ctx_t *c = (srtp_aes_icm_ctx_t *)cv;
218     const EVP_CIPHER *evp;
219 
220     /*
221      * set counter and initial values to 'offset' value, being careful not to
222      * go past the end of the key buffer
223      */
224     v128_set_to_zero(&c->counter);
225     v128_set_to_zero(&c->offset);
226     memcpy(&c->counter, key + c->key_size, SRTP_SALT_LEN);
227     memcpy(&c->offset, key + c->key_size, SRTP_SALT_LEN);
228 
229     /* force last two octets of the offset to zero (for srtp compatibility) */
230     c->offset.v8[SRTP_SALT_LEN] = c->offset.v8[SRTP_SALT_LEN + 1] = 0;
231     c->counter.v8[SRTP_SALT_LEN] = c->counter.v8[SRTP_SALT_LEN + 1] = 0;
232 
233     debug_print(srtp_mod_aes_icm, "key:  %s",
234                 srtp_octet_string_hex_string(key, c->key_size));
235     debug_print(srtp_mod_aes_icm, "offset: %s", v128_hex_string(&c->offset));
236 
237     switch (c->key_size) {
238     case SRTP_AES_256_KEY_LEN:
239         evp = EVP_aes_256_ctr();
240         break;
241     case SRTP_AES_192_KEY_LEN:
242         evp = EVP_aes_192_ctr();
243         break;
244     case SRTP_AES_128_KEY_LEN:
245         evp = EVP_aes_128_ctr();
246         break;
247     default:
248         return srtp_err_status_bad_param;
249         break;
250     }
251 
252     EVP_CIPHER_CTX_cleanup(c->ctx);
253     if (!EVP_EncryptInit_ex(c->ctx, evp, NULL, key, NULL)) {
254         return srtp_err_status_fail;
255     } else {
256         return srtp_err_status_ok;
257     }
258 
259     return srtp_err_status_ok;
260 }
261 
262 /*
263  * aes_icm_set_iv(c, iv) sets the counter value to the exor of iv with
264  * the offset
265  */
srtp_aes_icm_openssl_set_iv(void * cv,uint8_t * iv,srtp_cipher_direction_t dir)266 static srtp_err_status_t srtp_aes_icm_openssl_set_iv(
267     void *cv,
268     uint8_t *iv,
269     srtp_cipher_direction_t dir)
270 {
271     srtp_aes_icm_ctx_t *c = (srtp_aes_icm_ctx_t *)cv;
272     v128_t nonce;
273 
274     /* set nonce (for alignment) */
275     v128_copy_octet_string(&nonce, iv);
276 
277     debug_print(srtp_mod_aes_icm, "setting iv: %s", v128_hex_string(&nonce));
278 
279     v128_xor(&c->counter, &c->offset, &nonce);
280 
281     debug_print(srtp_mod_aes_icm, "set_counter: %s",
282                 v128_hex_string(&c->counter));
283 
284     if (!EVP_EncryptInit_ex(c->ctx, NULL, NULL, NULL, c->counter.v8)) {
285         return srtp_err_status_fail;
286     } else {
287         return srtp_err_status_ok;
288     }
289 }
290 
291 /*
292  * This function encrypts a buffer using AES CTR mode
293  *
294  * Parameters:
295  *	c	Crypto context
296  *	buf	data to encrypt
297  *	enc_len	length of encrypt buffer
298  */
srtp_aes_icm_openssl_encrypt(void * cv,unsigned char * buf,unsigned int * enc_len)299 static srtp_err_status_t srtp_aes_icm_openssl_encrypt(void *cv,
300                                                       unsigned char *buf,
301                                                       unsigned int *enc_len)
302 {
303     srtp_aes_icm_ctx_t *c = (srtp_aes_icm_ctx_t *)cv;
304     int len = 0;
305 
306     debug_print(srtp_mod_aes_icm, "rs0: %s", v128_hex_string(&c->counter));
307 
308     if (!EVP_EncryptUpdate(c->ctx, buf, &len, buf, *enc_len)) {
309         return srtp_err_status_cipher_fail;
310     }
311     *enc_len = len;
312 
313     if (!EVP_EncryptFinal_ex(c->ctx, buf + len, &len)) {
314         return srtp_err_status_cipher_fail;
315     }
316     *enc_len += len;
317 
318     return srtp_err_status_ok;
319 }
320 
321 /*
322  * Name of this crypto engine
323  */
324 static const char srtp_aes_icm_128_openssl_description[] =
325     "AES-128 counter mode using openssl";
326 static const char srtp_aes_icm_192_openssl_description[] =
327     "AES-192 counter mode using openssl";
328 static const char srtp_aes_icm_256_openssl_description[] =
329     "AES-256 counter mode using openssl";
330 
331 /*
332  * This is the function table for this crypto engine.
333  * note: the encrypt function is identical to the decrypt function
334  */
335 const srtp_cipher_type_t srtp_aes_icm_128 = {
336     srtp_aes_icm_openssl_alloc,           /* */
337     srtp_aes_icm_openssl_dealloc,         /* */
338     srtp_aes_icm_openssl_context_init,    /* */
339     0,                                    /* set_aad */
340     srtp_aes_icm_openssl_encrypt,         /* */
341     srtp_aes_icm_openssl_encrypt,         /* */
342     srtp_aes_icm_openssl_set_iv,          /* */
343     0,                                    /* get_tag */
344     srtp_aes_icm_128_openssl_description, /* */
345     &srtp_aes_icm_128_test_case_0,        /* */
346     SRTP_AES_ICM_128                      /* */
347 };
348 
349 /*
350  * This is the function table for this crypto engine.
351  * note: the encrypt function is identical to the decrypt function
352  */
353 const srtp_cipher_type_t srtp_aes_icm_192 = {
354     srtp_aes_icm_openssl_alloc,           /* */
355     srtp_aes_icm_openssl_dealloc,         /* */
356     srtp_aes_icm_openssl_context_init,    /* */
357     0,                                    /* set_aad */
358     srtp_aes_icm_openssl_encrypt,         /* */
359     srtp_aes_icm_openssl_encrypt,         /* */
360     srtp_aes_icm_openssl_set_iv,          /* */
361     0,                                    /* get_tag */
362     srtp_aes_icm_192_openssl_description, /* */
363     &srtp_aes_icm_192_test_case_0,        /* */
364     SRTP_AES_ICM_192                      /* */
365 };
366 
367 /*
368  * This is the function table for this crypto engine.
369  * note: the encrypt function is identical to the decrypt function
370  */
371 const srtp_cipher_type_t srtp_aes_icm_256 = {
372     srtp_aes_icm_openssl_alloc,           /* */
373     srtp_aes_icm_openssl_dealloc,         /* */
374     srtp_aes_icm_openssl_context_init,    /* */
375     0,                                    /* set_aad */
376     srtp_aes_icm_openssl_encrypt,         /* */
377     srtp_aes_icm_openssl_encrypt,         /* */
378     srtp_aes_icm_openssl_set_iv,          /* */
379     0,                                    /* get_tag */
380     srtp_aes_icm_256_openssl_description, /* */
381     &srtp_aes_icm_256_test_case_0,        /* */
382     SRTP_AES_ICM_256                      /* */
383 };
384