aescbc.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. int
  27. lws_jwe_encrypt_cbc_hs(struct lws_jwe *jwe, uint8_t *cek,
  28. uint8_t *aad, int aad_len)
  29. {
  30. int n, hlen = (int)lws_genhmac_size(jwe->jose.enc_alg->hmac_type);
  31. uint8_t digest[LWS_GENHASH_LARGEST];
  32. struct lws_gencrypto_keyelem el;
  33. struct lws_genhmac_ctx hmacctx;
  34. struct lws_genaes_ctx aesctx;
  35. size_t paddedlen;
  36. uint8_t al[8];
  37. /* Caller must have prepared space for the results */
  38. if (jwe->jws.map.len[LJWE_ATAG] != (unsigned int)hlen / 2) {
  39. lwsl_notice("%s: expected tag len %d, got %d\n", __func__,
  40. hlen / 2, jwe->jws.map.len[LJWE_ATAG]);
  41. return -1;
  42. }
  43. if (jwe->jws.map.len[LJWE_IV] != 16) {
  44. lwsl_notice("expected iv len %d, got %d\n", 16,
  45. jwe->jws.map.len[LJWE_IV]);
  46. return -1;
  47. }
  48. /* first create the authentication hmac */
  49. /* JWA Section 5.2.2.1
  50. *
  51. * 1. The secondary keys MAC_KEY and ENC_KEY are generated from the
  52. * input key K as follows. Each of these two keys is an octet
  53. * string.
  54. *
  55. * MAC_KEY consists of the initial MAC_KEY_LEN octets of K, in
  56. * order.
  57. * ENC_KEY consists of the final ENC_KEY_LEN octets of K, in
  58. * order.
  59. */
  60. /*
  61. * 2. The IV used is a 128-bit value generated randomly or
  62. * pseudorandomly for use in the cipher.
  63. */
  64. lws_get_random(jwe->jws.context, (void *)jwe->jws.map.buf[LJWE_IV], 16);
  65. /*
  66. * 3. The plaintext is CBC encrypted using PKCS #7 padding using
  67. * ENC_KEY as the key and the IV. We denote the ciphertext output
  68. * from this step as E.
  69. */
  70. /* second half is the AES ENC_KEY */
  71. el.buf = cek + (hlen / 2);
  72. el.len = hlen / 2;
  73. if (lws_genaes_create(&aesctx, LWS_GAESO_ENC, LWS_GAESM_CBC, &el,
  74. LWS_GAESP_WITH_PADDING, NULL)) {
  75. lwsl_err("%s: lws_genaes_create failed\n", __func__);
  76. return -1;
  77. }
  78. /*
  79. * the plaintext gets delivered to us in LJWE_CTXT, this replaces the
  80. * plaintext there with the ciphertext, which will be larger by some
  81. * padding bytes
  82. */
  83. n = lws_genaes_crypt(&aesctx, (uint8_t *)jwe->jws.map.buf[LJWE_CTXT],
  84. jwe->jws.map.len[LJWE_CTXT],
  85. (uint8_t *)jwe->jws.map.buf[LJWE_CTXT],
  86. (uint8_t *)jwe->jws.map.buf[LJWE_IV],
  87. NULL, NULL, LWS_AES_CBC_BLOCKLEN);
  88. paddedlen = lws_gencrypto_padded_length(LWS_AES_CBC_BLOCKLEN,
  89. jwe->jws.map.len[LJWE_CTXT]);
  90. jwe->jws.map.len[LJWE_CTXT] = (uint32_t)paddedlen;
  91. lws_genaes_destroy(&aesctx, (uint8_t *)jwe->jws.map.buf[LJWE_CTXT] +
  92. paddedlen - LWS_AES_CBC_BLOCKLEN, LWS_AES_CBC_BLOCKLEN);
  93. if (n) {
  94. lwsl_err("%s: lws_genaes_crypt failed\n", __func__);
  95. return -1;
  96. }
  97. /*
  98. * 4. The octet string AL is equal to the number of bits in the
  99. * Additional Authenticated Data A expressed as a 64-bit unsigned
  100. * big-endian integer.
  101. */
  102. lws_jwe_be64(aad_len * 8, al);
  103. /* first half of the CEK is the MAC key */
  104. if (lws_genhmac_init(&hmacctx, jwe->jose.enc_alg->hmac_type,
  105. cek, hlen / 2))
  106. return -1;
  107. /*
  108. * 5. A message Authentication Tag T is computed by applying HMAC
  109. * [RFC2104] to the following data, in order:
  110. *
  111. * - the Additional Authenticated Data A,
  112. * - the Initialization Vector IV,
  113. * - the ciphertext E computed in the previous step, and
  114. * - the octet string AL defined above.
  115. *
  116. * The string MAC_KEY is used as the MAC key. We denote the output
  117. * of the MAC computed in this step as M. The first T_LEN octets of
  118. * M are used as T.
  119. */
  120. if (lws_genhmac_update(&hmacctx, aad, aad_len) ||
  121. lws_genhmac_update(&hmacctx, jwe->jws.map.buf[LJWE_IV],
  122. LWS_JWE_AES_IV_BYTES) ||
  123. /* since we encrypted it, this is the ciphertext */
  124. lws_genhmac_update(&hmacctx,
  125. (uint8_t *)jwe->jws.map.buf[LJWE_CTXT],
  126. jwe->jws.map.len[LJWE_CTXT]) ||
  127. lws_genhmac_update(&hmacctx, al, 8)) {
  128. lwsl_err("%s: hmac computation failed\n", __func__);
  129. lws_genhmac_destroy(&hmacctx, NULL);
  130. return -1;
  131. }
  132. if (lws_genhmac_destroy(&hmacctx, digest)) {
  133. lwsl_err("%s: problem destroying hmac\n", __func__);
  134. return -1;
  135. }
  136. /* create tag */
  137. memcpy((void *)jwe->jws.map.buf[LJWE_ATAG], digest, hlen / 2);
  138. return jwe->jws.map.len[LJWE_CTXT];
  139. }
  140. int
  141. lws_jwe_auth_and_decrypt_cbc_hs(struct lws_jwe *jwe, uint8_t *enc_cek,
  142. uint8_t *aad, int aad_len)
  143. {
  144. int n, hlen = (int)lws_genhmac_size(jwe->jose.enc_alg->hmac_type);
  145. uint8_t digest[LWS_GENHASH_LARGEST];
  146. struct lws_gencrypto_keyelem el;
  147. struct lws_genhmac_ctx hmacctx;
  148. struct lws_genaes_ctx aesctx;
  149. uint8_t al[8];
  150. /* Some sanity checks on what came in */
  151. if (jwe->jws.map.len[LJWE_ATAG] != (unsigned int)hlen / 2) {
  152. lwsl_notice("%s: expected tag len %d, got %d\n", __func__,
  153. hlen / 2, jwe->jws.map.len[LJWE_ATAG]);
  154. return -1;
  155. }
  156. if (jwe->jws.map.len[LJWE_IV] != 16) {
  157. lwsl_notice("expected iv len %d, got %d\n", 16,
  158. jwe->jws.map.len[LJWE_IV]);
  159. return -1;
  160. }
  161. /* Prepare to check authentication
  162. *
  163. * AAD is the b64 JOSE header.
  164. *
  165. * The octet string AL, which is the number of bits in AAD expressed as
  166. * a big-endian 64-bit unsigned integer is:
  167. *
  168. * [0, 0, 0, 0, 0, 0, 1, 152]
  169. *
  170. * Concatenate the AAD, the Initialization Vector, the ciphertext, and
  171. * the AL value.
  172. *
  173. */
  174. lws_jwe_be64(aad_len * 8, al);
  175. /* first half of enc_cek is the MAC key */
  176. if (lws_genhmac_init(&hmacctx, jwe->jose.enc_alg->hmac_type, enc_cek,
  177. hlen / 2)) {
  178. lwsl_err("%s: lws_genhmac_init fail\n", __func__);
  179. return -1;
  180. }
  181. if (lws_genhmac_update(&hmacctx, aad, aad_len) ||
  182. lws_genhmac_update(&hmacctx, (uint8_t *)jwe->jws.map.buf[LJWE_IV],
  183. jwe->jws.map.len[LJWE_IV]) ||
  184. lws_genhmac_update(&hmacctx, (uint8_t *)jwe->jws.map.buf[LJWE_CTXT],
  185. jwe->jws.map.len[LJWE_CTXT]) ||
  186. lws_genhmac_update(&hmacctx, al, 8)) {
  187. lwsl_err("%s: hmac computation failed\n", __func__);
  188. lws_genhmac_destroy(&hmacctx, NULL);
  189. return -1;
  190. }
  191. if (lws_genhmac_destroy(&hmacctx, digest)) {
  192. lwsl_err("%s: problem destroying hmac\n", __func__);
  193. return -1;
  194. }
  195. /* first half of digest is the auth tag */
  196. if (lws_timingsafe_bcmp(digest, jwe->jws.map.buf[LJWE_ATAG], hlen / 2)) {
  197. lwsl_err("%s: auth failed: hmac tag (%d) != ATAG (%d)\n",
  198. __func__, hlen / 2, jwe->jws.map.len[LJWE_ATAG]);
  199. lwsl_hexdump_notice(jwe->jws.map.buf[LJWE_ATAG], hlen / 2);
  200. lwsl_hexdump_notice(digest, hlen / 2);
  201. return -1;
  202. }
  203. /* second half of enc cek is the CEK KEY */
  204. el.buf = enc_cek + (hlen / 2);
  205. el.len = hlen / 2;
  206. if (lws_genaes_create(&aesctx, LWS_GAESO_DEC, LWS_GAESM_CBC,
  207. &el, LWS_GAESP_NO_PADDING, NULL)) {
  208. lwsl_err("%s: lws_genaes_create failed\n", __func__);
  209. return -1;
  210. }
  211. n = lws_genaes_crypt(&aesctx, (uint8_t *)jwe->jws.map.buf[LJWE_CTXT],
  212. jwe->jws.map.len[LJWE_CTXT],
  213. (uint8_t *)jwe->jws.map.buf[LJWE_CTXT],
  214. (uint8_t *)jwe->jws.map.buf[LJWE_IV], NULL, NULL, 16);
  215. /* Strip the PKCS #7 padding */
  216. if (jwe->jws.map.len[LJWE_CTXT] < LWS_AES_CBC_BLOCKLEN ||
  217. jwe->jws.map.len[LJWE_CTXT] <= (unsigned char)jwe->jws.map.buf[LJWE_CTXT]
  218. [jwe->jws.map.len[LJWE_CTXT] - 1]) {
  219. lwsl_err("%s: invalid padded ciphertext length: %d. Corrupt data?\n",
  220. __func__, jwe->jws.map.len[LJWE_CTXT]);
  221. return -1;
  222. }
  223. jwe->jws.map.len[LJWE_CTXT] -= jwe->jws.map.buf[LJWE_CTXT][
  224. jwe->jws.map.len[LJWE_CTXT] - 1];
  225. n |= lws_genaes_destroy(&aesctx, NULL, 0);
  226. if (n) {
  227. lwsl_err("%s: lws_genaes_crypt failed\n", __func__);
  228. return -1;
  229. }
  230. return jwe->jws.map.len[LJWE_CTXT];
  231. }