aes_gcm_mbedtls.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. srtp_aes_gcm_ctx_t *c = (srtp_aes_gcm_ctx_t *)cv;
  233. debug_print(srtp_mod_aes_gcm, "setting AAD: %s",
  234. srtp_octet_string_hex_string(aad, aad_len));
  235. if (aad_len + c->aad_size > MAX_AD_SIZE) {
  236. return srtp_err_status_bad_param;
  237. }
  238. memcpy(c->aad + c->aad_size, aad, aad_len);
  239. c->aad_size += aad_len;
  240. return (srtp_err_status_ok);
  241. }
  242. /*
  243. * This function encrypts a buffer using AES GCM mode
  244. *
  245. * Parameters:
  246. * c Crypto context
  247. * buf data to encrypt
  248. * enc_len length of encrypt buffer
  249. */
  250. static srtp_err_status_t srtp_aes_gcm_mbedtls_encrypt(void *cv,
  251. unsigned char *buf,
  252. unsigned int *enc_len)
  253. {
  254. FUNC_ENTRY();
  255. srtp_aes_gcm_ctx_t *c = (srtp_aes_gcm_ctx_t *)cv;
  256. int errCode = 0;
  257. if (c->dir != srtp_direction_encrypt && c->dir != srtp_direction_decrypt) {
  258. return (srtp_err_status_bad_param);
  259. }
  260. errCode = mbedtls_gcm_crypt_and_tag(c->ctx, MBEDTLS_GCM_ENCRYPT, *enc_len,
  261. c->iv, c->iv_len, c->aad, c->aad_size,
  262. buf, buf, c->tag_len, c->tag);
  263. c->aad_size = 0;
  264. if (errCode != 0) {
  265. debug_print(srtp_mod_aes_gcm, "mbedtls error code: %d", errCode);
  266. return srtp_err_status_bad_param;
  267. }
  268. return (srtp_err_status_ok);
  269. }
  270. /*
  271. * This function calculates and returns the GCM tag for a given context.
  272. * This should be called after encrypting the data. The *len value
  273. * is increased by the tag size. The caller must ensure that *buf has
  274. * enough room to accept the appended tag.
  275. *
  276. * Parameters:
  277. * c Crypto context
  278. * buf data to encrypt
  279. * len length of encrypt buffer
  280. */
  281. static srtp_err_status_t srtp_aes_gcm_mbedtls_get_tag(void *cv,
  282. uint8_t *buf,
  283. uint32_t *len)
  284. {
  285. FUNC_ENTRY();
  286. srtp_aes_gcm_ctx_t *c = (srtp_aes_gcm_ctx_t *)cv;
  287. debug_print(srtp_mod_aes_gcm, "appended tag size: %d", c->tag_len);
  288. *len = c->tag_len;
  289. memcpy(buf, c->tag, c->tag_len);
  290. return (srtp_err_status_ok);
  291. }
  292. /*
  293. * This function decrypts a buffer using AES GCM mode
  294. *
  295. * Parameters:
  296. * c Crypto context
  297. * buf data to encrypt
  298. * enc_len length of encrypt buffer
  299. */
  300. static srtp_err_status_t srtp_aes_gcm_mbedtls_decrypt(void *cv,
  301. unsigned char *buf,
  302. unsigned int *enc_len)
  303. {
  304. FUNC_ENTRY();
  305. srtp_aes_gcm_ctx_t *c = (srtp_aes_gcm_ctx_t *)cv;
  306. int errCode = 0;
  307. if (c->dir != srtp_direction_encrypt && c->dir != srtp_direction_decrypt) {
  308. return (srtp_err_status_bad_param);
  309. }
  310. debug_print(srtp_mod_aes_gcm, "AAD: %s",
  311. srtp_octet_string_hex_string(c->aad, c->aad_size));
  312. errCode = mbedtls_gcm_auth_decrypt(
  313. c->ctx, (*enc_len - c->tag_len), c->iv, c->iv_len, c->aad, c->aad_size,
  314. buf + (*enc_len - c->tag_len), c->tag_len, buf, buf);
  315. c->aad_size = 0;
  316. if (errCode != 0) {
  317. return (srtp_err_status_auth_fail);
  318. }
  319. /*
  320. * Reduce the buffer size by the tag length since the tag
  321. * is not part of the original payload
  322. */
  323. *enc_len -= c->tag_len;
  324. return (srtp_err_status_ok);
  325. }
  326. /*
  327. * Name of this crypto engine
  328. */
  329. static const char srtp_aes_gcm_128_mbedtls_description[] =
  330. "AES-128 GCM using mbedtls";
  331. static const char srtp_aes_gcm_256_mbedtls_description[] =
  332. "AES-256 GCM using mbedtls";
  333. /*
  334. * This is the vector function table for this crypto engine.
  335. */
  336. const srtp_cipher_type_t srtp_aes_gcm_128 = {
  337. srtp_aes_gcm_mbedtls_alloc,
  338. srtp_aes_gcm_mbedtls_dealloc,
  339. srtp_aes_gcm_mbedtls_context_init,
  340. srtp_aes_gcm_mbedtls_set_aad,
  341. srtp_aes_gcm_mbedtls_encrypt,
  342. srtp_aes_gcm_mbedtls_decrypt,
  343. srtp_aes_gcm_mbedtls_set_iv,
  344. srtp_aes_gcm_mbedtls_get_tag,
  345. srtp_aes_gcm_128_mbedtls_description,
  346. &srtp_aes_gcm_128_test_case_0,
  347. SRTP_AES_GCM_128
  348. };
  349. /*
  350. * This is the vector function table for this crypto engine.
  351. */
  352. const srtp_cipher_type_t srtp_aes_gcm_256 = {
  353. srtp_aes_gcm_mbedtls_alloc,
  354. srtp_aes_gcm_mbedtls_dealloc,
  355. srtp_aes_gcm_mbedtls_context_init,
  356. srtp_aes_gcm_mbedtls_set_aad,
  357. srtp_aes_gcm_mbedtls_encrypt,
  358. srtp_aes_gcm_mbedtls_decrypt,
  359. srtp_aes_gcm_mbedtls_set_iv,
  360. srtp_aes_gcm_mbedtls_get_tag,
  361. srtp_aes_gcm_256_mbedtls_description,
  362. &srtp_aes_gcm_256_test_case_0,
  363. SRTP_AES_GCM_256
  364. };