aeskw.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. * RFC3394 Key Wrap uses a 128-bit key, and bloats what it is wrapping by
  28. * one 8-byte block. So, if you had a 32 byte plaintext CEK to wrap, after
  29. * wrapping it becomes a 40 byte wrapped, enciphered, key.
  30. *
  31. * The CEK comes in from and goes out in LJWE_EKEY. So LJWE_EKEY length
  32. * increases by 8 from calling this.
  33. */
  34. int
  35. lws_jwe_encrypt_aeskw_cbc_hs(struct lws_jwe *jwe, char *temp, int *temp_len)
  36. {
  37. struct lws_genaes_ctx aesctx;
  38. /* we are wrapping a key, so size for the worst case after wrap */
  39. uint8_t enc_cek[LWS_JWE_LIMIT_KEY_ELEMENT_BYTES +
  40. LWS_JWE_RFC3394_OVERHEAD_BYTES];
  41. int n, m, hlen = (int)lws_genhmac_size(jwe->jose.enc_alg->hmac_type),
  42. ot = *temp_len;
  43. if (jwe->jws.jwk->kty != LWS_GENCRYPTO_KTY_OCT) {
  44. lwsl_err("%s: unexpected kty %d\n", __func__, jwe->jws.jwk->kty);
  45. return -1;
  46. }
  47. /* create a b64 version of the JOSE header, needed for hashing */
  48. if (lws_jws_encode_b64_element(&jwe->jws.map_b64, LJWE_JOSE,
  49. temp, temp_len,
  50. jwe->jws.map.buf[LJWE_JOSE],
  51. jwe->jws.map.len[LJWE_JOSE]))
  52. return -1;
  53. /* Allocate temp space for ATAG and IV */
  54. if (lws_jws_alloc_element(&jwe->jws.map, LJWE_ATAG, temp + (ot - *temp_len),
  55. temp_len, hlen / 2, 0))
  56. return -1;
  57. if (lws_jws_alloc_element(&jwe->jws.map, LJWE_IV, temp + (ot - *temp_len),
  58. temp_len, LWS_JWE_AES_IV_BYTES, 0))
  59. return -1;
  60. /* 1) Encrypt the payload... */
  61. /* the CEK is 256-bit in the example encrypted with a 128-bit key */
  62. n = lws_jwe_encrypt_cbc_hs(jwe, (uint8_t *)jwe->jws.map.buf[LJWE_EKEY],
  63. (uint8_t *)jwe->jws.map_b64.buf[LJWE_JOSE],
  64. jwe->jws.map_b64.len[LJWE_JOSE]);
  65. if (n < 0) {
  66. lwsl_err("%s: lws_jwe_encrypt_cbc_hs failed\n", __func__);
  67. return -1;
  68. }
  69. /* 2) Encrypt the JWE Encrypted Key: RFC3394 Key Wrap uses 64 bit blocks
  70. * and 128-bit input key*/
  71. if (lws_genaes_create(&aesctx, LWS_GAESO_ENC, LWS_GAESM_KW,
  72. jwe->jws.jwk->e, 1, NULL)) {
  73. lwsl_notice("%s: lws_genaes_create\n", __func__);
  74. return -1;
  75. }
  76. /* tag size is determined by enc cipher key length */
  77. n = lws_genaes_crypt(&aesctx, (uint8_t *)jwe->jws.map.buf[LJWE_EKEY],
  78. jwe->jws.map.len[LJWE_EKEY], enc_cek, NULL, NULL, NULL,
  79. lws_gencrypto_bits_to_bytes(
  80. jwe->jose.enc_alg->keybits_fixed));
  81. m = lws_genaes_destroy(&aesctx, NULL, 0);
  82. if (n < 0) {
  83. lwsl_err("%s: encrypt cek fail\n", __func__);
  84. return -1;
  85. }
  86. if (m < 0) {
  87. lwsl_err("%s: lws_genaes_destroy fail\n", __func__);
  88. return -1;
  89. }
  90. jwe->jws.map.len[LJWE_EKEY] += LWS_JWE_RFC3394_OVERHEAD_BYTES;
  91. memcpy((uint8_t *)jwe->jws.map.buf[LJWE_EKEY], enc_cek,
  92. jwe->jws.map.len[LJWE_EKEY]);
  93. return jwe->jws.map.len[LJWE_CTXT];
  94. }
  95. int
  96. lws_jwe_auth_and_decrypt_aeskw_cbc_hs(struct lws_jwe *jwe)
  97. {
  98. struct lws_genaes_ctx aesctx;
  99. uint8_t enc_cek[LWS_JWE_LIMIT_KEY_ELEMENT_BYTES +
  100. LWS_JWE_RFC3394_OVERHEAD_BYTES];
  101. int n, m;
  102. if (jwe->jws.jwk->kty != LWS_GENCRYPTO_KTY_OCT) {
  103. lwsl_err("%s: unexpected kty %d\n", __func__, jwe->jws.jwk->kty);
  104. return -1;
  105. }
  106. /* the CEK is 256-bit in the example encrypted with a 128-bit key */
  107. if (jwe->jws.map.len[LJWE_EKEY] > sizeof(enc_cek))
  108. return -1;
  109. /* 1) Decrypt the JWE Encrypted Key to get the raw MAC / CEK */
  110. if (lws_genaes_create(&aesctx, LWS_GAESO_DEC, LWS_GAESM_KW,
  111. jwe->jws.jwk->e, 1, NULL)) {
  112. lwsl_notice("%s: lws_genaes_create\n", __func__);
  113. return -1;
  114. }
  115. /*
  116. * Decrypt the CEK into enc_cek
  117. * tag size is determined by enc cipher key length */
  118. n = lws_genaes_crypt(&aesctx, (uint8_t *)jwe->jws.map.buf[LJWE_EKEY],
  119. jwe->jws.map.len[LJWE_EKEY], enc_cek, NULL, NULL, NULL,
  120. lws_gencrypto_bits_to_bytes(
  121. jwe->jose.enc_alg->keybits_fixed));
  122. m = lws_genaes_destroy(&aesctx, NULL, 0);
  123. if (n < 0) {
  124. lwsl_err("%s: decrypt CEK fail\n", __func__);
  125. return -1;
  126. }
  127. if (m < 0) {
  128. lwsl_err("%s: lws_genaes_destroy fail\n", __func__);
  129. return -1;
  130. }
  131. /* 2) Decrypt the payload */
  132. n = lws_jwe_auth_and_decrypt_cbc_hs(jwe, enc_cek,
  133. (uint8_t *)jwe->jws.map_b64.buf[LJWE_JOSE],
  134. jwe->jws.map_b64.len[LJWE_JOSE]);
  135. if (n < 0) {
  136. lwsl_err("%s: lws_jwe_auth_and_decrypt_cbc_hs failed\n",
  137. __func__);
  138. return -1;
  139. }
  140. return jwe->jws.map.len[LJWE_CTXT];
  141. }