aesgcm.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * libwebsockets - small server side websockets and web server implementation
  3. *
  4. * Copyright (C) 2010 - 2019 Andy Green <[email protected]>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to
  8. * deal in the Software without restriction, including without limitation the
  9. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  10. * sell copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  22. * IN THE SOFTWARE.
  23. */
  24. #include "private-lib-core.h"
  25. #include "private-lib-jose-jwe.h"
  26. /*
  27. * NOTICE this is AESGCM content encryption, it's not AES GCM key wrapping
  28. *
  29. *
  30. * This section defines the specifics of performing authenticated
  31. * encryption with AES in Galois/Counter Mode (GCM) ([AES] and
  32. * [NIST.800-38D]).
  33. *
  34. * The CEK is used as the encryption key.
  35. *
  36. * Use of an IV of size 96 bits is REQUIRED with this algorithm.
  37. *
  38. * The requested size of the Authentication Tag output MUST be 128 bits,
  39. * regardless of the key size.
  40. *
  41. * For decrypt: decrypt the KEK, then decrypt the payload
  42. *
  43. * For encrypt: encrypt the payload, then encrypt the KEK
  44. */
  45. /*
  46. * encrypting... enc_cek is unencrypted
  47. */
  48. int
  49. lws_jwe_encrypt_gcm(struct lws_jwe *jwe,
  50. uint8_t *enc_cek, uint8_t *aad, int aad_len)
  51. {
  52. struct lws_gencrypto_keyelem el;
  53. struct lws_genaes_ctx aesctx;
  54. size_t ivs = LWS_AESGCM_IV;
  55. int n;
  56. /* Some sanity checks on what came in */
  57. /* MUST be 128-bit for all sizes */
  58. if (jwe->jws.map.len[LJWE_ATAG] != LWS_AESGCM_TAG) {
  59. lwsl_notice("%s: AESGCM tag size must be 128b, got %d\n",
  60. __func__, jwe->jws.map.len[LJWE_ATAG]);
  61. return -1;
  62. }
  63. if (jwe->jws.map.len[LJWE_IV] != LWS_AESGCM_IV) { /* MUST be 96-bit */
  64. lwsl_notice("%s: AESGCM IV must be 128b, got %d\n", __func__,
  65. jwe->jws.map.len[LJWE_IV]);
  66. return -1;
  67. }
  68. /* EKEY is directly the CEK KEY */
  69. el.buf = enc_cek;
  70. el.len = jwe->jose.enc_alg->keybits_fixed / 8;
  71. if (lws_genaes_create(&aesctx, LWS_GAESO_ENC, LWS_GAESM_GCM,
  72. &el, LWS_GAESP_NO_PADDING, NULL)) {
  73. lwsl_err("%s: lws_genaes_create failed\n", __func__);
  74. return -1;
  75. }
  76. /* aad */
  77. n = lws_genaes_crypt(&aesctx, aad, aad_len, NULL,
  78. (uint8_t *)jwe->jws.map.buf[LJWE_IV],
  79. (uint8_t *)jwe->jws.map.buf[LJWE_ATAG], &ivs,
  80. LWS_AESGCM_TAG);
  81. if (n) {
  82. lwsl_err("%s: lws_genaes_crypt aad failed\n", __func__);
  83. return -1;
  84. }
  85. /* payload */
  86. n = lws_genaes_crypt(&aesctx, (uint8_t *)jwe->jws.map.buf[LJWE_CTXT],
  87. jwe->jws.map.len[LJWE_CTXT],
  88. (uint8_t *)jwe->jws.map.buf[LJWE_CTXT],
  89. (uint8_t *)jwe->jws.map.buf[LJWE_IV],
  90. NULL, &ivs,
  91. LWS_AESGCM_TAG);
  92. n |= lws_genaes_destroy(&aesctx, (uint8_t *)jwe->jws.map.buf[LJWE_ATAG],
  93. LWS_AESGCM_TAG);
  94. if (n) {
  95. lwsl_err("%s: lws_genaes_crypt failed\n", __func__);
  96. return -1;
  97. }
  98. return jwe->jws.map.len[LJWE_CTXT];
  99. }
  100. int
  101. lws_jwe_auth_and_decrypt_gcm(struct lws_jwe *jwe,
  102. uint8_t *enc_cek, uint8_t *aad, int aad_len)
  103. {
  104. struct lws_gencrypto_keyelem el;
  105. struct lws_genaes_ctx aesctx;
  106. size_t ivs = LWS_AESGCM_IV;
  107. uint8_t tag[LWS_AESGCM_TAG];
  108. int n;
  109. /* Some sanity checks on what came in */
  110. /* Tag MUST be 128-bit for all sizes */
  111. if (jwe->jws.map.len[LJWE_ATAG] != LWS_AESGCM_TAG) {
  112. lwsl_notice("%s: AESGCM tag size must be 128b, got %d\n",
  113. __func__, jwe->jws.map.len[LJWE_ATAG]);
  114. return -1;
  115. }
  116. if (jwe->jws.map.len[LJWE_IV] != LWS_AESGCM_IV) { /* MUST be 96-bit */
  117. lwsl_notice("%s: AESGCM IV must be 128b, got %d\n", __func__,
  118. jwe->jws.map.len[LJWE_IV]);
  119. return -1;
  120. }
  121. /* EKEY is directly the CEK KEY */
  122. el.buf = enc_cek;
  123. el.len = jwe->jose.enc_alg->keybits_fixed / 8;
  124. if (lws_genaes_create(&aesctx, LWS_GAESO_DEC, LWS_GAESM_GCM,
  125. &el, LWS_GAESP_NO_PADDING, NULL)) {
  126. lwsl_err("%s: lws_genaes_create failed\n", __func__);
  127. return -1;
  128. }
  129. n = lws_genaes_crypt(&aesctx, aad, aad_len,
  130. NULL,
  131. (uint8_t *)jwe->jws.map.buf[LJWE_IV],
  132. (uint8_t *)jwe->jws.map.buf[LJWE_ATAG], &ivs, 16);
  133. if (n) {
  134. lwsl_err("%s: lws_genaes_crypt aad failed\n", __func__);
  135. return -1;
  136. }
  137. n = lws_genaes_crypt(&aesctx, (uint8_t *)jwe->jws.map.buf[LJWE_CTXT],
  138. jwe->jws.map.len[LJWE_CTXT],
  139. (uint8_t *)jwe->jws.map.buf[LJWE_CTXT],
  140. (uint8_t *)jwe->jws.map.buf[LJWE_IV],
  141. (uint8_t *)jwe->jws.map.buf[LJWE_ATAG], &ivs, 16);
  142. n |= lws_genaes_destroy(&aesctx, tag, sizeof(tag));
  143. if (n) {
  144. lwsl_err("%s: lws_genaes_crypt failed\n", __func__);
  145. return -1;
  146. }
  147. return jwe->jws.map.len[LJWE_CTXT];
  148. }