2
0

aes_gcm_mbedtls.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /*
  2. * aes_gcm_mbedtls.c
  3. *
  4. * AES Galois Counter Mode
  5. *
  6. * YongCheng Yang
  7. *
  8. */
  9. /*
  10. *
  11. * Copyright (c) 2013-2017, Cisco Systems, Inc.
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions
  16. * are met:
  17. *
  18. * Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. *
  21. * Redistributions in binary form must reproduce the above
  22. * copyright notice, this list of conditions and the following
  23. * disclaimer in the documentation and/or other materials provided
  24. * with the distribution.
  25. *
  26. * Neither the name of the Cisco Systems, Inc. nor the names of its
  27. * contributors may be used to endorse or promote products derived
  28. * from this software without specific prior written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  31. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  32. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  33. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  34. * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  35. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  36. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  37. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  38. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  41. * OF THE POSSIBILITY OF SUCH DAMAGE.
  42. *
  43. */
  44. #ifdef HAVE_CONFIG_H
  45. #include <config.h>
  46. #endif
  47. #include <mbedtls/gcm.h>
  48. #include "aes_gcm.h"
  49. #include "alloc.h"
  50. #include "err.h" /* for srtp_debug */
  51. #include "crypto_types.h"
  52. #include "cipher_types.h"
  53. #include "cipher_test_cases.h"
  54. srtp_debug_module_t srtp_mod_aes_gcm = {
  55. 0, /* debugging is off by default */
  56. "aes gcm mbedtls" /* printable module name */
  57. };
  58. /**
  59. * SRTP IV Formation for AES-GCM
  60. * https://tools.ietf.org/html/rfc7714#section-8.1
  61. * 0 0 0 0 0 0 0 0 0 0 1 1
  62. * 0 1 2 3 4 5 6 7 8 9 0 1
  63. * +--+--+--+--+--+--+--+--+--+--+--+--+
  64. * |00|00| SSRC | ROC | SEQ |---+
  65. * +--+--+--+--+--+--+--+--+--+--+--+--+ |
  66. * |
  67. * +--+--+--+--+--+--+--+--+--+--+--+--+ |
  68. * | Encryption Salt |->(+)
  69. * +--+--+--+--+--+--+--+--+--+--+--+--+ |
  70. * |
  71. * +--+--+--+--+--+--+--+--+--+--+--+--+ |
  72. * | Initialization Vector |<--+
  73. * +--+--+--+--+--+--+--+--+--+--+--+--+
  74. *
  75. * SRTCP IV Formation for AES-GCM
  76. * https://tools.ietf.org/html/rfc7714#section-9.1
  77. *
  78. */
  79. /*
  80. * For now we only support 8 and 16 octet tags. The spec allows for
  81. * optional 12 byte tag, which may be supported in the future.
  82. */
  83. #define GCM_IV_LEN 12
  84. #define GCM_AUTH_TAG_LEN 16
  85. #define GCM_AUTH_TAG_LEN_8 8
  86. #define FUNC_ENTRY() debug_print(srtp_mod_aes_gcm, "%s entry", __func__);
  87. /*
  88. * This function allocates a new instance of this crypto engine.
  89. * The key_len parameter should be one of 28 or 44 for
  90. * AES-128-GCM or AES-256-GCM respectively. Note that the
  91. * key length includes the 14 byte salt value that is used when
  92. * initializing the KDF.
  93. */
  94. static srtp_err_status_t srtp_aes_gcm_mbedtls_alloc(srtp_cipher_t **c,
  95. int key_len,
  96. int tlen)
  97. {
  98. FUNC_ENTRY();
  99. srtp_aes_gcm_ctx_t *gcm;
  100. debug_print(srtp_mod_aes_gcm, "allocating cipher with key length %d",
  101. key_len);
  102. debug_print(srtp_mod_aes_gcm, "allocating cipher with tag length %d", tlen);
  103. /*
  104. * Verify the key_len is valid for one of: AES-128/256
  105. */
  106. if (key_len != SRTP_AES_GCM_128_KEY_LEN_WSALT &&
  107. key_len != SRTP_AES_GCM_256_KEY_LEN_WSALT) {
  108. return (srtp_err_status_bad_param);
  109. }
  110. if (tlen != GCM_AUTH_TAG_LEN && tlen != GCM_AUTH_TAG_LEN_8) {
  111. return (srtp_err_status_bad_param);
  112. }
  113. /* allocate memory a cipher of type aes_gcm */
  114. *c = (srtp_cipher_t *)srtp_crypto_alloc(sizeof(srtp_cipher_t));
  115. if (*c == NULL) {
  116. return (srtp_err_status_alloc_fail);
  117. }
  118. gcm = (srtp_aes_gcm_ctx_t *)srtp_crypto_alloc(sizeof(srtp_aes_gcm_ctx_t));
  119. if (gcm == NULL) {
  120. srtp_crypto_free(*c);
  121. *c = NULL;
  122. return (srtp_err_status_alloc_fail);
  123. }
  124. gcm->ctx =
  125. (mbedtls_gcm_context *)srtp_crypto_alloc(sizeof(mbedtls_gcm_context));
  126. if (gcm->ctx == NULL) {
  127. srtp_crypto_free(gcm);
  128. srtp_crypto_free(*c);
  129. *c = NULL;
  130. return srtp_err_status_alloc_fail;
  131. }
  132. mbedtls_gcm_init(gcm->ctx);
  133. /* set pointers */
  134. (*c)->state = gcm;
  135. /* setup cipher attributes */
  136. switch (key_len) {
  137. case SRTP_AES_GCM_128_KEY_LEN_WSALT:
  138. (*c)->type = &srtp_aes_gcm_128;
  139. (*c)->algorithm = SRTP_AES_GCM_128;
  140. gcm->key_size = SRTP_AES_128_KEY_LEN;
  141. gcm->tag_len = tlen;
  142. break;
  143. case SRTP_AES_GCM_256_KEY_LEN_WSALT:
  144. (*c)->type = &srtp_aes_gcm_256;
  145. (*c)->algorithm = SRTP_AES_GCM_256;
  146. gcm->key_size = SRTP_AES_256_KEY_LEN;
  147. gcm->tag_len = tlen;
  148. break;
  149. }
  150. /* set key size */
  151. (*c)->key_len = key_len;
  152. return (srtp_err_status_ok);
  153. }
  154. /*
  155. * This function deallocates a GCM session
  156. */
  157. static srtp_err_status_t srtp_aes_gcm_mbedtls_dealloc(srtp_cipher_t *c)
  158. {
  159. srtp_aes_gcm_ctx_t *ctx;
  160. FUNC_ENTRY();
  161. ctx = (srtp_aes_gcm_ctx_t *)c->state;
  162. if (ctx) {
  163. mbedtls_gcm_free(ctx->ctx);
  164. srtp_crypto_free(ctx->ctx);
  165. /* zeroize the key material */
  166. octet_string_set_to_zero(ctx, sizeof(srtp_aes_gcm_ctx_t));
  167. srtp_crypto_free(ctx);
  168. }
  169. /* free memory */
  170. srtp_crypto_free(c);
  171. return (srtp_err_status_ok);
  172. }
  173. static srtp_err_status_t srtp_aes_gcm_mbedtls_context_init(void *cv,
  174. const uint8_t *key)
  175. {
  176. FUNC_ENTRY();
  177. srtp_aes_gcm_ctx_t *c = (srtp_aes_gcm_ctx_t *)cv;
  178. uint32_t key_len_in_bits;
  179. int errCode = 0;
  180. c->dir = srtp_direction_any;
  181. c->aad_size = 0;
  182. debug_print(srtp_mod_aes_gcm, "key: %s",
  183. srtp_octet_string_hex_string(key, c->key_size));
  184. key_len_in_bits = (c->key_size << 3);
  185. switch (c->key_size) {
  186. case SRTP_AES_256_KEY_LEN:
  187. case SRTP_AES_128_KEY_LEN:
  188. break;
  189. default:
  190. return (srtp_err_status_bad_param);
  191. break;
  192. }
  193. errCode = mbedtls_gcm_setkey(c->ctx, MBEDTLS_CIPHER_ID_AES,
  194. (const unsigned char *)key, key_len_in_bits);
  195. if (errCode != 0) {
  196. debug_print(srtp_mod_aes_gcm, "mbedtls error code: %d", errCode);
  197. return srtp_err_status_init_fail;
  198. }
  199. return (srtp_err_status_ok);
  200. }
  201. static srtp_err_status_t srtp_aes_gcm_mbedtls_set_iv(
  202. void *cv,
  203. uint8_t *iv,
  204. srtp_cipher_direction_t direction)
  205. {
  206. FUNC_ENTRY();
  207. srtp_aes_gcm_ctx_t *c = (srtp_aes_gcm_ctx_t *)cv;
  208. if (direction != srtp_direction_encrypt &&
  209. direction != srtp_direction_decrypt) {
  210. return (srtp_err_status_bad_param);
  211. }
  212. c->dir = direction;
  213. debug_print(srtp_mod_aes_gcm, "setting iv: %s",
  214. srtp_octet_string_hex_string(iv, GCM_IV_LEN));
  215. c->iv_len = GCM_IV_LEN;
  216. memcpy(c->iv, iv, c->iv_len);
  217. return (srtp_err_status_ok);
  218. }
  219. /*
  220. * This function processes the AAD
  221. *
  222. * Parameters:
  223. * c Crypto context
  224. * aad Additional data to process for AEAD cipher suites
  225. * aad_len length of aad buffer
  226. */
  227. static srtp_err_status_t srtp_aes_gcm_mbedtls_set_aad(void *cv,
  228. const uint8_t *aad,
  229. uint32_t aad_len)
  230. {
  231. FUNC_ENTRY();
  232. int errCode = 0;
  233. srtp_aes_gcm_ctx_t *c = (srtp_aes_gcm_ctx_t *)cv;
  234. debug_print(srtp_mod_aes_gcm, "setting AAD: %s",
  235. srtp_octet_string_hex_string(aad, aad_len));
  236. if (aad_len + c->aad_size > MAX_AD_SIZE) {
  237. return srtp_err_status_bad_param;
  238. }
  239. memcpy(c->aad + c->aad_size, aad, aad_len);
  240. c->aad_size += aad_len;
  241. return (srtp_err_status_ok);
  242. }
  243. /*
  244. * This function encrypts a buffer using AES GCM mode
  245. *
  246. * Parameters:
  247. * c Crypto context
  248. * buf data to encrypt
  249. * enc_len length of encrypt buffer
  250. */
  251. static srtp_err_status_t srtp_aes_gcm_mbedtls_encrypt(void *cv,
  252. unsigned char *buf,
  253. unsigned int *enc_len)
  254. {
  255. FUNC_ENTRY();
  256. srtp_aes_gcm_ctx_t *c = (srtp_aes_gcm_ctx_t *)cv;
  257. int errCode = 0;
  258. if (c->dir != srtp_direction_encrypt && c->dir != srtp_direction_decrypt) {
  259. return (srtp_err_status_bad_param);
  260. }
  261. errCode = mbedtls_gcm_crypt_and_tag(c->ctx, MBEDTLS_GCM_ENCRYPT, *enc_len,
  262. c->iv, c->iv_len, c->aad, c->aad_size,
  263. buf, buf, c->tag_len, c->tag);
  264. c->aad_size = 0;
  265. if (errCode != 0) {
  266. debug_print(srtp_mod_aes_gcm, "mbedtls error code: %d", errCode);
  267. return srtp_err_status_bad_param;
  268. }
  269. return (srtp_err_status_ok);
  270. }
  271. /*
  272. * This function calculates and returns the GCM tag for a given context.
  273. * This should be called after encrypting the data. The *len value
  274. * is increased by the tag size. The caller must ensure that *buf has
  275. * enough room to accept the appended tag.
  276. *
  277. * Parameters:
  278. * c Crypto context
  279. * buf data to encrypt
  280. * len length of encrypt buffer
  281. */
  282. static srtp_err_status_t srtp_aes_gcm_mbedtls_get_tag(void *cv,
  283. uint8_t *buf,
  284. uint32_t *len)
  285. {
  286. FUNC_ENTRY();
  287. srtp_aes_gcm_ctx_t *c = (srtp_aes_gcm_ctx_t *)cv;
  288. debug_print(srtp_mod_aes_gcm, "appended tag size: %d", c->tag_len);
  289. *len = c->tag_len;
  290. memcpy(buf, c->tag, c->tag_len);
  291. return (srtp_err_status_ok);
  292. }
  293. /*
  294. * This function decrypts a buffer using AES GCM mode
  295. *
  296. * Parameters:
  297. * c Crypto context
  298. * buf data to encrypt
  299. * enc_len length of encrypt buffer
  300. */
  301. static srtp_err_status_t srtp_aes_gcm_mbedtls_decrypt(void *cv,
  302. unsigned char *buf,
  303. unsigned int *enc_len)
  304. {
  305. FUNC_ENTRY();
  306. srtp_aes_gcm_ctx_t *c = (srtp_aes_gcm_ctx_t *)cv;
  307. int errCode = 0;
  308. int len = *enc_len;
  309. if (c->dir != srtp_direction_encrypt && c->dir != srtp_direction_decrypt) {
  310. return (srtp_err_status_bad_param);
  311. }
  312. debug_print(srtp_mod_aes_gcm, "AAD: %s",
  313. srtp_octet_string_hex_string(c->aad, c->aad_size));
  314. errCode = mbedtls_gcm_auth_decrypt(
  315. c->ctx, (*enc_len - c->tag_len), c->iv, c->iv_len, c->aad, c->aad_size,
  316. buf + (*enc_len - c->tag_len), c->tag_len, buf, buf);
  317. c->aad_size = 0;
  318. if (errCode != 0) {
  319. return (srtp_err_status_auth_fail);
  320. }
  321. /*
  322. * Reduce the buffer size by the tag length since the tag
  323. * is not part of the original payload
  324. */
  325. *enc_len -= c->tag_len;
  326. return (srtp_err_status_ok);
  327. }
  328. /*
  329. * Name of this crypto engine
  330. */
  331. static const char srtp_aes_gcm_128_mbedtls_description[] =
  332. "AES-128 GCM using mbedtls";
  333. static const char srtp_aes_gcm_256_mbedtls_description[] =
  334. "AES-256 GCM using mbedtls";
  335. /*
  336. * This is the vector function table for this crypto engine.
  337. */
  338. const srtp_cipher_type_t srtp_aes_gcm_128 = {
  339. srtp_aes_gcm_mbedtls_alloc,
  340. srtp_aes_gcm_mbedtls_dealloc,
  341. srtp_aes_gcm_mbedtls_context_init,
  342. srtp_aes_gcm_mbedtls_set_aad,
  343. srtp_aes_gcm_mbedtls_encrypt,
  344. srtp_aes_gcm_mbedtls_decrypt,
  345. srtp_aes_gcm_mbedtls_set_iv,
  346. srtp_aes_gcm_mbedtls_get_tag,
  347. srtp_aes_gcm_128_mbedtls_description,
  348. &srtp_aes_gcm_128_test_case_0,
  349. SRTP_AES_GCM_128
  350. };
  351. /*
  352. * This is the vector function table for this crypto engine.
  353. */
  354. const srtp_cipher_type_t srtp_aes_gcm_256 = {
  355. srtp_aes_gcm_mbedtls_alloc,
  356. srtp_aes_gcm_mbedtls_dealloc,
  357. srtp_aes_gcm_mbedtls_context_init,
  358. srtp_aes_gcm_mbedtls_set_aad,
  359. srtp_aes_gcm_mbedtls_encrypt,
  360. srtp_aes_gcm_mbedtls_decrypt,
  361. srtp_aes_gcm_mbedtls_set_iv,
  362. srtp_aes_gcm_mbedtls_get_tag,
  363. srtp_aes_gcm_256_mbedtls_description,
  364. &srtp_aes_gcm_256_test_case_0,
  365. SRTP_AES_GCM_256
  366. };