lws-genaes.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. * lws_genaes provides an AES abstraction api in lws that works the
  25. * same whether you are using openssl or mbedtls hash functions underneath.
  26. */
  27. #include "private-lib-core.h"
  28. #if defined(LWS_WITH_JOSE)
  29. #include "private-lib-jose.h"
  30. #endif
  31. /*
  32. * Care: many openssl apis return 1 for success. These are translated to the
  33. * lws convention of 0 for success.
  34. */
  35. int
  36. lws_genaes_create(struct lws_genaes_ctx *ctx, enum enum_aes_operation op,
  37. enum enum_aes_modes mode, struct lws_gencrypto_keyelem *el,
  38. enum enum_aes_padding padding, void *engine)
  39. {
  40. int n = 0;
  41. ctx->ctx = EVP_CIPHER_CTX_new();
  42. if (!ctx->ctx)
  43. return -1;
  44. ctx->mode = mode;
  45. ctx->k = el;
  46. ctx->engine = engine;
  47. ctx->init = 0;
  48. ctx->op = op;
  49. ctx->padding = padding;
  50. switch (ctx->k->len) {
  51. case 128 / 8:
  52. switch (mode) {
  53. case LWS_GAESM_KW:
  54. #if defined(LWS_HAVE_EVP_aes_128_wrap)
  55. EVP_CIPHER_CTX_set_flags(ctx->ctx,
  56. EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
  57. ctx->cipher = EVP_aes_128_wrap();
  58. break;
  59. #else
  60. lwsl_err("%s: your OpenSSL lacks AES wrap apis, update it\n",
  61. __func__);
  62. return -1;
  63. #endif
  64. case LWS_GAESM_CBC:
  65. ctx->cipher = EVP_aes_128_cbc();
  66. break;
  67. #if defined(LWS_HAVE_EVP_aes_128_cfb128)
  68. case LWS_GAESM_CFB128:
  69. ctx->cipher = EVP_aes_128_cfb128();
  70. break;
  71. #endif
  72. #if defined(LWS_HAVE_EVP_aes_128_cfb8)
  73. case LWS_GAESM_CFB8:
  74. ctx->cipher = EVP_aes_128_cfb8();
  75. break;
  76. #endif
  77. case LWS_GAESM_CTR:
  78. ctx->cipher = EVP_aes_128_ctr();
  79. break;
  80. case LWS_GAESM_ECB:
  81. ctx->cipher = EVP_aes_128_ecb();
  82. break;
  83. case LWS_GAESM_OFB:
  84. ctx->cipher = EVP_aes_128_ofb();
  85. break;
  86. case LWS_GAESM_XTS:
  87. lwsl_err("%s: AES XTS requires double-length key\n",
  88. __func__);
  89. break;
  90. case LWS_GAESM_GCM:
  91. ctx->cipher = EVP_aes_128_gcm();
  92. break;
  93. default:
  94. goto bail;
  95. }
  96. break;
  97. case 192 / 8:
  98. switch (mode) {
  99. case LWS_GAESM_KW:
  100. #if defined(LWS_HAVE_EVP_aes_128_wrap)
  101. EVP_CIPHER_CTX_set_flags(ctx->ctx,
  102. EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
  103. ctx->cipher = EVP_aes_192_wrap();
  104. break;
  105. #else
  106. lwsl_err("%s: your OpenSSL lacks AES wrap apis, update it\n",
  107. __func__);
  108. return -1;
  109. #endif
  110. case LWS_GAESM_CBC:
  111. ctx->cipher = EVP_aes_192_cbc();
  112. break;
  113. #if defined(LWS_HAVE_EVP_aes_192_cfb128)
  114. case LWS_GAESM_CFB128:
  115. ctx->cipher = EVP_aes_192_cfb128();
  116. break;
  117. #endif
  118. #if defined(LWS_HAVE_EVP_aes_192_cfb8)
  119. case LWS_GAESM_CFB8:
  120. ctx->cipher = EVP_aes_192_cfb8();
  121. break;
  122. #endif
  123. case LWS_GAESM_CTR:
  124. ctx->cipher = EVP_aes_192_ctr();
  125. break;
  126. case LWS_GAESM_ECB:
  127. ctx->cipher = EVP_aes_192_ecb();
  128. break;
  129. case LWS_GAESM_OFB:
  130. ctx->cipher = EVP_aes_192_ofb();
  131. break;
  132. case LWS_GAESM_XTS:
  133. lwsl_err("%s: AES XTS 192 invalid\n", __func__);
  134. goto bail;
  135. case LWS_GAESM_GCM:
  136. ctx->cipher = EVP_aes_192_gcm();
  137. break;
  138. default:
  139. goto bail;
  140. }
  141. break;
  142. case 256 / 8:
  143. switch (mode) {
  144. case LWS_GAESM_KW:
  145. #if defined(LWS_HAVE_EVP_aes_128_wrap)
  146. EVP_CIPHER_CTX_set_flags(ctx->ctx,
  147. EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
  148. ctx->cipher = EVP_aes_256_wrap();
  149. break;
  150. #else
  151. lwsl_err("%s: your OpenSSL lacks AES wrap apis, update it\n",
  152. __func__);
  153. return -1;
  154. #endif
  155. case LWS_GAESM_CBC:
  156. ctx->cipher = EVP_aes_256_cbc();
  157. break;
  158. #if defined(LWS_HAVE_EVP_aes_256_cfb128)
  159. case LWS_GAESM_CFB128:
  160. ctx->cipher = EVP_aes_256_cfb128();
  161. break;
  162. #endif
  163. #if defined(LWS_HAVE_EVP_aes_256_cfb8)
  164. case LWS_GAESM_CFB8:
  165. ctx->cipher = EVP_aes_256_cfb8();
  166. break;
  167. #endif
  168. case LWS_GAESM_CTR:
  169. ctx->cipher = EVP_aes_256_ctr();
  170. break;
  171. case LWS_GAESM_ECB:
  172. ctx->cipher = EVP_aes_256_ecb();
  173. break;
  174. case LWS_GAESM_OFB:
  175. ctx->cipher = EVP_aes_256_ofb();
  176. break;
  177. #if defined(LWS_HAVE_EVP_aes_128_xts)
  178. case LWS_GAESM_XTS:
  179. ctx->cipher = EVP_aes_128_xts();
  180. break;
  181. #endif
  182. case LWS_GAESM_GCM:
  183. ctx->cipher = EVP_aes_256_gcm();
  184. break;
  185. default:
  186. goto bail;
  187. }
  188. break;
  189. case 512 / 8:
  190. switch (mode) {
  191. case LWS_GAESM_XTS:
  192. ctx->cipher = EVP_aes_256_xts();
  193. break;
  194. default:
  195. goto bail;
  196. }
  197. break;
  198. default:
  199. lwsl_err("%s: unsupported AES size %d bits\n", __func__,
  200. ctx->k->len * 8);
  201. goto bail;
  202. }
  203. switch (ctx->op) {
  204. case LWS_GAESO_ENC:
  205. n = EVP_EncryptInit_ex(ctx->ctx, ctx->cipher, ctx->engine,
  206. NULL, NULL);
  207. EVP_CIPHER_CTX_set_padding(ctx->ctx, padding);
  208. break;
  209. case LWS_GAESO_DEC:
  210. n = EVP_DecryptInit_ex(ctx->ctx, ctx->cipher, ctx->engine,
  211. NULL, NULL);
  212. EVP_CIPHER_CTX_set_padding(ctx->ctx, padding);
  213. break;
  214. }
  215. if (!n) {
  216. lwsl_err("%s: cipher init failed (cipher %p)\n", __func__,
  217. ctx->cipher);
  218. lws_tls_err_describe_clear();
  219. goto bail;
  220. }
  221. return 0;
  222. bail:
  223. EVP_CIPHER_CTX_free(ctx->ctx);
  224. ctx->ctx = NULL;
  225. return -1;
  226. }
  227. int
  228. lws_genaes_destroy(struct lws_genaes_ctx *ctx, unsigned char *tag, size_t tlen)
  229. {
  230. uint8_t buf[256];
  231. int outl = sizeof(buf), n = 0;
  232. if (!ctx->ctx)
  233. return 0;
  234. if (ctx->init) {
  235. switch (ctx->op) {
  236. case LWS_GAESO_ENC:
  237. if (EVP_EncryptFinal_ex(ctx->ctx, buf, &outl) != 1) {
  238. lwsl_err("%s: enc final failed\n", __func__);
  239. n = -1;
  240. }
  241. if (ctx->mode == LWS_GAESM_GCM) {
  242. if (EVP_CIPHER_CTX_ctrl(ctx->ctx,
  243. EVP_CTRL_GCM_GET_TAG,
  244. ctx->taglen, tag) != 1) {
  245. lwsl_err("get tag ctrl failed\n");
  246. //lws_tls_err_describe_clear();
  247. n = 1;
  248. }
  249. }
  250. if (ctx->mode == LWS_GAESM_CBC)
  251. memcpy(tag, buf, outl);
  252. break;
  253. case LWS_GAESO_DEC:
  254. if (EVP_DecryptFinal_ex(ctx->ctx, buf, &outl) != 1) {
  255. lwsl_err("%s: dec final failed\n", __func__);
  256. lws_tls_err_describe_clear();
  257. n = -1;
  258. }
  259. break;
  260. }
  261. if (outl)
  262. lwsl_debug("%s: final len %d\n", __func__, outl);
  263. }
  264. ctx->k = NULL;
  265. EVP_CIPHER_CTX_free(ctx->ctx);
  266. ctx->ctx = NULL;
  267. return n;
  268. }
  269. int
  270. lws_genaes_crypt(struct lws_genaes_ctx *ctx,
  271. const uint8_t *in, size_t len, uint8_t *out,
  272. uint8_t *iv_or_nonce_ctr_or_data_unit_16,
  273. uint8_t *stream_block_16, size_t *nc_or_iv_off, int taglen)
  274. {
  275. int n = 0, outl, olen;
  276. if (!ctx->init) {
  277. EVP_CIPHER_CTX_set_key_length(ctx->ctx, ctx->k->len);
  278. if (ctx->mode == LWS_GAESM_GCM) {
  279. n = EVP_CIPHER_CTX_ctrl(ctx->ctx, EVP_CTRL_GCM_SET_IVLEN,
  280. (int)*nc_or_iv_off, NULL);
  281. if (n != 1) {
  282. lwsl_err("%s: SET_IVLEN failed\n", __func__);
  283. return -1;
  284. }
  285. memcpy(ctx->tag, stream_block_16, taglen);
  286. ctx->taglen = taglen;
  287. }
  288. switch (ctx->op) {
  289. case LWS_GAESO_ENC:
  290. n = EVP_EncryptInit_ex(ctx->ctx, NULL, NULL,
  291. ctx->k->buf,
  292. iv_or_nonce_ctr_or_data_unit_16);
  293. break;
  294. case LWS_GAESO_DEC:
  295. if (ctx->mode == LWS_GAESM_GCM)
  296. EVP_CIPHER_CTX_ctrl(ctx->ctx,
  297. EVP_CTRL_GCM_SET_TAG,
  298. ctx->taglen, ctx->tag);
  299. n = EVP_DecryptInit_ex(ctx->ctx, NULL, NULL,
  300. ctx->k->buf,
  301. iv_or_nonce_ctr_or_data_unit_16);
  302. break;
  303. }
  304. if (!n) {
  305. lws_tls_err_describe_clear();
  306. lwsl_err("%s: init failed (cipher %p)\n",
  307. __func__, ctx->cipher);
  308. return -1;
  309. }
  310. ctx->init = 1;
  311. }
  312. if (ctx->mode == LWS_GAESM_GCM && !out) {
  313. /* AAD */
  314. if (!len)
  315. return 0;
  316. switch (ctx->op) {
  317. case LWS_GAESO_ENC:
  318. n = EVP_EncryptUpdate(ctx->ctx, NULL, &olen, in, (int)len);
  319. break;
  320. case LWS_GAESO_DEC:
  321. n = EVP_DecryptUpdate(ctx->ctx, NULL, &olen, in, (int)len);
  322. break;
  323. default:
  324. return -1;
  325. }
  326. if (n != 1) {
  327. lwsl_err("%s: set AAD failed\n", __func__);
  328. lws_tls_err_describe_clear();
  329. lwsl_hexdump_err(in, len);
  330. return -1;
  331. }
  332. return 0;
  333. }
  334. switch (ctx->op) {
  335. case LWS_GAESO_ENC:
  336. n = EVP_EncryptUpdate(ctx->ctx, out, &outl, in, (int)len);
  337. break;
  338. case LWS_GAESO_DEC:
  339. n = EVP_DecryptUpdate(ctx->ctx, out, &outl, in, (int)len);
  340. break;
  341. default:
  342. return -1;
  343. }
  344. // lwsl_notice("discarding outl %d\n", (int)outl);
  345. if (!n) {
  346. lwsl_notice("%s: update failed\n", __func__);
  347. lws_tls_err_describe_clear();
  348. return -1;
  349. }
  350. return 0;
  351. }