lws-genaes.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  1. /*
  2. * lws-api-test-gencrypto - lws-genaes
  3. *
  4. * Written in 2010-2018 by Andy Green <[email protected]>
  5. *
  6. * This file is made available under the Creative Commons CC0 1.0
  7. * Universal Public Domain Dedication.
  8. */
  9. #include <libwebsockets.h>
  10. static const uint8_t
  11. /*
  12. * produced with (plaintext.txt contains "test plaintext\0\0")
  13. *
  14. * openssl enc -aes256 \
  15. * -K "0123456789abcdeffedcba98765432100123456789abcdeffedcba9876543210" \
  16. * -iv "0123456789abcdeffedcba9876543210"
  17. * -in plaintext.txt -out out.enc
  18. *
  19. */
  20. *cbc256 = (uint8_t *)"test plaintext\0\0",
  21. cbc256_enc[] = {
  22. 0x2b, 0x5d, 0xb2, 0xa8, 0x5a, 0x5a, 0xf4, 0x2e,
  23. 0xf7, 0xf9, 0xc5, 0x3c, 0x73, 0xef, 0x40, 0x88,
  24. }, cbc256_iv[] = {
  25. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  26. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  27. }, cbc256_key[] = {
  28. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  29. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  30. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  31. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  32. }
  33. ;
  34. static int
  35. test_genaes_cbc(void)
  36. {
  37. struct lws_genaes_ctx ctx;
  38. struct lws_gencrypto_keyelem e;
  39. uint8_t res[32], res1[32];
  40. /*
  41. * As part of a jwk, these are allocated. But here we just use one as
  42. * a wrapper on a static binary key.
  43. */
  44. e.buf = (uint8_t *)cbc256_key;
  45. e.len = sizeof(cbc256_key);
  46. if (lws_genaes_create(&ctx, LWS_GAESO_ENC, LWS_GAESM_CBC, &e, 0, NULL)) {
  47. lwsl_err("%s: lws_genaes_create failed\n", __func__);
  48. return 1;
  49. }
  50. if (lws_genaes_crypt(&ctx, cbc256, 16, res, (uint8_t *)cbc256_iv,
  51. NULL, NULL, 0)) {
  52. lwsl_err("%s: lws_genaes_crypt failed\n", __func__);
  53. goto bail;
  54. }
  55. if (lws_genaes_destroy(&ctx, NULL, 0)) {
  56. lwsl_err("%s: lws_genaes_destroy enc failed\n", __func__);
  57. return -1;
  58. }
  59. if (lws_timingsafe_bcmp(cbc256_enc, res, 16)) {
  60. lwsl_err("%s: lws_genaes_crypt encoding mismatch\n", __func__);
  61. lwsl_hexdump_notice(res, 16);
  62. return -1;
  63. }
  64. if (lws_genaes_create(&ctx, LWS_GAESO_DEC, LWS_GAESM_CBC, &e, 0, NULL)) {
  65. lwsl_err("%s: lws_genaes_create dec failed\n", __func__);
  66. return -1;
  67. }
  68. if (lws_genaes_crypt(&ctx, res, 16, res1, (uint8_t *)cbc256_iv,
  69. NULL, NULL, 0)) {
  70. lwsl_err("%s: lws_genaes_crypt dec failed\n", __func__);
  71. goto bail;
  72. }
  73. if (lws_genaes_destroy(&ctx, NULL, 0)) {
  74. lwsl_err("%s: lws_genaes_destroy dec failed\n", __func__);
  75. lwsl_hexdump_notice(res1, 16);
  76. return -1;
  77. }
  78. if (lws_timingsafe_bcmp(cbc256, res1, 16)) {
  79. lwsl_err("%s: lws_genaes_crypt decoding mismatch\n", __func__);
  80. lwsl_hexdump_notice(res, 16);
  81. return -1;
  82. }
  83. return 0;
  84. bail:
  85. lws_genaes_destroy(&ctx, NULL, 0);
  86. return -1;
  87. }
  88. static const uint8_t
  89. /*
  90. * produced with (plaintext.txt contains "test plaintext\0\0")
  91. *
  92. * openssl enc -aes-128-cfb \
  93. * -K "0123456789abcdeffedcba9876543210" \
  94. * -iv "0123456789abcdeffedcba9876543210"
  95. * -in plaintext.txt -out out.enc
  96. *
  97. */
  98. *cfb128 = (uint8_t *)"test plaintext\0\0",
  99. cfb128_enc[] = {
  100. 0xd2, 0x11, 0x86, 0xd7, 0xa9, 0x55, 0x59, 0x04,
  101. 0x4f, 0x63, 0x7c, 0xb9, 0xc6, 0xa1, 0xc9, 0x71
  102. }, cfb128_iv[] = {
  103. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  104. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  105. }, cfb128_key[] = {
  106. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  107. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  108. };
  109. static int
  110. test_genaes_cfb128(void)
  111. {
  112. struct lws_genaes_ctx ctx;
  113. struct lws_gencrypto_keyelem e;
  114. uint8_t res[32], res1[32];
  115. size_t iv_off = 0;
  116. e.buf = (uint8_t *)cfb128_key;
  117. e.len = sizeof(cfb128_key);
  118. if (lws_genaes_create(&ctx, LWS_GAESO_ENC, LWS_GAESM_CFB128, &e, 0, NULL)) {
  119. lwsl_err("%s: lws_genaes_create failed\n", __func__);
  120. return 1;
  121. }
  122. if (lws_genaes_crypt(&ctx, cfb128, 16, res, (uint8_t *)cfb128_iv,
  123. NULL, &iv_off, 0)) {
  124. lwsl_err("%s: lws_genaes_crypt failed\n", __func__);
  125. goto bail;
  126. }
  127. if (lws_genaes_destroy(&ctx, NULL, 0)) {
  128. lwsl_err("%s: lws_genaes_destroy failed\n", __func__);
  129. return -1;
  130. }
  131. if (lws_timingsafe_bcmp(cfb128_enc, res, 16)) {
  132. lwsl_err("%s: lws_genaes_crypt encoding mismatch\n", __func__);
  133. lwsl_hexdump_notice(res, 16);
  134. return -1;
  135. }
  136. iv_off = 0;
  137. if (lws_genaes_create(&ctx, LWS_GAESO_DEC, LWS_GAESM_CFB128, &e, 0, NULL)) {
  138. lwsl_err("%s: lws_genaes_create dec failed\n", __func__);
  139. return -1;
  140. }
  141. if (lws_genaes_crypt(&ctx, res, 16, res1, (uint8_t *)cfb128_iv,
  142. NULL, &iv_off, 0)) {
  143. lwsl_err("%s: lws_genaes_crypt dec failed\n", __func__);
  144. goto bail;
  145. }
  146. if (lws_genaes_destroy(&ctx, NULL, 0)) {
  147. lwsl_err("%s: lws_genaes_destroy failed\n", __func__);
  148. return -1;
  149. }
  150. if (lws_timingsafe_bcmp(cfb128, res1, 16)) {
  151. lwsl_err("%s: lws_genaes_crypt decoding mismatch\n", __func__);
  152. lwsl_hexdump_notice(res1, 16);
  153. return -1;
  154. }
  155. return 0;
  156. bail:
  157. lws_genaes_destroy(&ctx, NULL, 0);
  158. return -1;
  159. }
  160. static const uint8_t
  161. /*
  162. * produced with (plaintext.txt contains "test plaintext\0\0")
  163. *
  164. * openssl enc -aes-128-cfb8 \
  165. * -K "0123456789abcdeffedcba9876543210" \
  166. * -iv "0123456789abcdeffedcba9876543210"
  167. * -in plaintext.txt -out out.enc
  168. *
  169. */
  170. *cfb8 = (uint8_t *)"test plaintext\0\0",
  171. cfb8_enc[] = {
  172. 0xd2, 0x91, 0x06, 0x2d, 0x1b, 0x1e, 0x9b, 0x39,
  173. 0xa6, 0x65, 0x8e, 0xbe, 0x68, 0x32, 0x3d, 0xab
  174. }, cfb8_iv[] = {
  175. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  176. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  177. }, cfb8_key[] = {
  178. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  179. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  180. };
  181. static int
  182. test_genaes_cfb8(void)
  183. {
  184. struct lws_genaes_ctx ctx;
  185. struct lws_gencrypto_keyelem e;
  186. uint8_t res[32], res1[32];
  187. e.buf = (uint8_t *)cfb8_key;
  188. e.len = sizeof(cfb8_key);
  189. if (lws_genaes_create(&ctx, LWS_GAESO_ENC, LWS_GAESM_CFB8, &e, 0, NULL)) {
  190. lwsl_err("%s: lws_genaes_create failed\n", __func__);
  191. return 1;
  192. }
  193. if (lws_genaes_crypt(&ctx, cfb8, 16, res, (uint8_t *)cfb8_iv,
  194. NULL, NULL, 0)) {
  195. lwsl_err("%s: lws_genaes_crypt failed\n", __func__);
  196. goto bail;
  197. }
  198. if (lws_genaes_destroy(&ctx, NULL, 0)) {
  199. lwsl_err("%s: lws_genaes_destroy failed\n", __func__);
  200. return -1;
  201. }
  202. if (lws_timingsafe_bcmp(cfb8_enc, res, 16)) {
  203. lwsl_err("%s: lws_genaes_crypt encoding mismatch\n", __func__);
  204. lwsl_hexdump_notice(res, 16);
  205. return -1;
  206. }
  207. if (lws_genaes_create(&ctx, LWS_GAESO_DEC, LWS_GAESM_CFB8, &e, 0, NULL)) {
  208. lwsl_err("%s: lws_genaes_create dec failed\n", __func__);
  209. return -1;
  210. }
  211. if (lws_genaes_crypt(&ctx, res, 16, res1, (uint8_t *)cfb8_iv,
  212. NULL, NULL, 0)) {
  213. lwsl_err("%s: lws_genaes_crypt dec failed\n", __func__);
  214. goto bail;
  215. }
  216. if (lws_genaes_destroy(&ctx, NULL, 0)) {
  217. lwsl_err("%s: lws_genaes_destroy failed\n", __func__);
  218. return -1;
  219. }
  220. if (lws_timingsafe_bcmp(cfb8, res1, 16)) {
  221. lwsl_err("%s: lws_genaes_crypt decoding mismatch\n", __func__);
  222. lwsl_hexdump_notice(res1, 16);
  223. return -1;
  224. }
  225. return 0;
  226. bail:
  227. lws_genaes_destroy(&ctx, NULL, 0);
  228. return -1;
  229. }
  230. static const uint8_t
  231. /*
  232. * produced with (plaintext.txt contains "test plaintext\0\0")
  233. *
  234. * openssl enc -aes-128-ctr \
  235. * -K "0123456789abcdeffedcba9876543210" \
  236. * -iv "0123456789abcdeffedcba9876543210"
  237. * -in plaintext.txt -out out.enc
  238. *
  239. */
  240. *ctr = (uint8_t *)"test plaintext\0\0",
  241. ctr_enc[] = {
  242. 0xd2, 0x11, 0x86, 0xd7, 0xa9, 0x55, 0x59, 0x04,
  243. 0x4f, 0x63, 0x7c, 0xb9, 0xc6, 0xa1, 0xc9, 0x71
  244. }, ctr_iv[] = {
  245. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  246. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  247. }, ctr_key[] = {
  248. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  249. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  250. };
  251. static int
  252. test_genaes_ctr(void)
  253. {
  254. uint8_t nonce_counter[16], sb[16];
  255. struct lws_genaes_ctx ctx;
  256. struct lws_gencrypto_keyelem e;
  257. uint8_t res[32], res1[32];
  258. size_t nc_off = 0;
  259. e.buf = (uint8_t *)ctr_key;
  260. e.len = sizeof(ctr_key);
  261. memset(sb, 0, sizeof(nonce_counter));
  262. memcpy(nonce_counter, ctr_iv, sizeof(ctr_iv));
  263. if (lws_genaes_create(&ctx, LWS_GAESO_ENC, LWS_GAESM_CTR, &e, 0, NULL)) {
  264. lwsl_err("%s: lws_genaes_create failed\n", __func__);
  265. return 1;
  266. }
  267. if (lws_genaes_crypt(&ctx, ctr, 16, res, nonce_counter, sb, &nc_off, 0)) {
  268. lwsl_err("%s: lws_genaes_crypt failed\n", __func__);
  269. goto bail;
  270. }
  271. if (lws_genaes_destroy(&ctx, NULL, 0)) {
  272. lwsl_err("%s: lws_genaes_destroy failed\n", __func__);
  273. return -1;
  274. }
  275. if (lws_timingsafe_bcmp(ctr_enc, res, 16)) {
  276. lwsl_err("%s: lws_genaes_crypt encoding mismatch\n", __func__);
  277. lwsl_hexdump_notice(res, 16);
  278. return -1;
  279. }
  280. nc_off = 0;
  281. memset(sb , 0, sizeof(nonce_counter));
  282. memcpy(nonce_counter, ctr_iv, sizeof(ctr_iv));
  283. if (lws_genaes_create(&ctx, LWS_GAESO_DEC, LWS_GAESM_CTR, &e, 0, NULL)) {
  284. lwsl_err("%s: lws_genaes_create dec failed\n", __func__);
  285. return -1;
  286. }
  287. if (lws_genaes_crypt(&ctx, res, 16, res1, nonce_counter, sb, &nc_off, 0)) {
  288. lwsl_err("%s: lws_genaes_crypt dec failed\n", __func__);
  289. goto bail;
  290. }
  291. if (lws_genaes_destroy(&ctx, NULL, 0)) {
  292. lwsl_err("%s: lws_genaes_destroy failed\n", __func__);
  293. return -1;
  294. }
  295. if (lws_timingsafe_bcmp(ctr, res1, 16)) {
  296. lwsl_err("%s: lws_genaes_crypt decoding mismatch\n", __func__);
  297. lwsl_hexdump_notice(res1, 16);
  298. return -1;
  299. }
  300. lws_explicit_bzero(sb, sizeof(sb));
  301. return 0;
  302. bail:
  303. lws_genaes_destroy(&ctx, NULL, 0);
  304. return -1;
  305. }
  306. static const uint8_t
  307. /*
  308. * produced with (plaintext.txt contains "test plaintext\0\0")
  309. *
  310. * openssl enc -aes-128-ecb \
  311. * -K "0123456789abcdeffedcba9876543210" \
  312. * -in plaintext.txt -out out.enc
  313. *
  314. */
  315. *ecb = (uint8_t *)"test plaintext\0\0",
  316. ecb_enc[] = {
  317. 0xf3, 0xe5, 0x6c, 0x80, 0x3a, 0xf1, 0xc4, 0xa0,
  318. 0x7e, 0xdf, 0x86, 0x0f, 0x6d, 0xca, 0x5d, 0x36,
  319. 0x17, 0x22, 0x37, 0x42, 0x47, 0x41, 0x67, 0x7d,
  320. 0x99, 0x25, 0x02, 0x6b, 0x6b, 0x8f, 0x9c, 0x7f
  321. }, ecb_key[] = {
  322. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  323. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  324. };
  325. static int
  326. test_genaes_ecb(void)
  327. {
  328. struct lws_genaes_ctx ctx;
  329. struct lws_gencrypto_keyelem e;
  330. uint8_t res[32], res1[32];
  331. /*
  332. * As part of a jwk, these are allocated. But here we just use one as
  333. * a wrapper on a static binary key.
  334. */
  335. e.buf = (uint8_t *)ecb_key;
  336. e.len = sizeof(ecb_key);
  337. if (lws_genaes_create(&ctx, LWS_GAESO_ENC, LWS_GAESM_ECB, &e, 0, NULL)) {
  338. lwsl_err("%s: lws_genaes_create failed\n", __func__);
  339. return 1;
  340. }
  341. if (lws_genaes_crypt(&ctx, ecb, 16, res, NULL, NULL, NULL, 0)) {
  342. lwsl_err("%s: lws_genaes_crypt failed\n", __func__);
  343. goto bail;
  344. }
  345. if (lws_genaes_destroy(&ctx, NULL, 0)) {
  346. lwsl_err("%s: lws_genaes_destroy failed\n", __func__);
  347. return -1;
  348. }
  349. if (lws_timingsafe_bcmp(ecb_enc, res, 16)) {
  350. lwsl_err("%s: lws_genaes_crypt encoding mismatch\n", __func__);
  351. lwsl_hexdump_notice(res, 16);
  352. return -1;
  353. }
  354. if (lws_genaes_create(&ctx, LWS_GAESO_DEC, LWS_GAESM_ECB, &e, 0, NULL)) {
  355. lwsl_err("%s: lws_genaes_create dec failed\n", __func__);
  356. return -1;
  357. }
  358. if (lws_genaes_crypt(&ctx, res, 16, res1, NULL, NULL, NULL, 0)) {
  359. lwsl_err("%s: lws_genaes_crypt dec failed\n", __func__);
  360. goto bail;
  361. }
  362. if (lws_genaes_destroy(&ctx, NULL, 0)) {
  363. lwsl_err("%s: lws_genaes_destroy failed\n", __func__);
  364. return -1;
  365. }
  366. if (lws_timingsafe_bcmp(ecb, res1, 16)) {
  367. lwsl_err("%s: lws_genaes_crypt decoding mismatch\n", __func__);
  368. lwsl_hexdump_notice(res, 16);
  369. return -1;
  370. }
  371. return 0;
  372. bail:
  373. lws_genaes_destroy(&ctx, NULL, 0);
  374. return -1;
  375. }
  376. #if defined(MBEDTLS_CONFIG_H) && !defined(MBEDTLS_CIPHER_MODE_OFB)
  377. #else
  378. static const uint8_t
  379. /*
  380. * produced with (plaintext.txt contains "test plaintext\0\0")
  381. *
  382. * openssl enc -aes-128-ofb \
  383. * -K "0123456789abcdeffedcba98765432100123456789abcdeffedcba9876543210" \
  384. * -iv "0123456789abcdeffedcba9876543210"
  385. * -in plaintext.txt -out out.enc
  386. *
  387. */
  388. *ofb = (uint8_t *)"test plaintext\0\0",
  389. ofb_enc[] = {
  390. /* !!! ugh... openssl app produces this... */
  391. // 0xd2, 0x11, 0x86, 0xd7, 0xa9, 0x55, 0x59, 0x04,
  392. // 0x4f, 0x63, 0x7c, 0xb9, 0xc6, 0xa1, 0xc9, 0x71,
  393. /* but both OpenSSL and mbedTLS produce this */
  394. 0x11, 0x33, 0x6D, 0xFC, 0x88, 0x4C, 0x28, 0xBA,
  395. 0xD0, 0xF2, 0x6C, 0xBC, 0xDE, 0x4A, 0x56, 0x20
  396. }, ofb_iv[] = {
  397. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  398. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  399. }, ofb_key[] = {
  400. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  401. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  402. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  403. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  404. }
  405. ;
  406. static int
  407. test_genaes_ofb(void)
  408. {
  409. struct lws_genaes_ctx ctx;
  410. struct lws_gencrypto_keyelem e;
  411. uint8_t res[32], res1[32];
  412. size_t iv_off = 0;
  413. e.buf = (uint8_t *)ofb_key;
  414. e.len = sizeof(ofb_key);
  415. if (lws_genaes_create(&ctx, LWS_GAESO_ENC, LWS_GAESM_OFB, &e, 0, NULL)) {
  416. lwsl_err("%s: lws_genaes_create failed\n", __func__);
  417. return 1;
  418. }
  419. if (lws_genaes_crypt(&ctx, ofb, 16, res, (uint8_t *)ofb_iv, NULL,
  420. &iv_off, 0)) {
  421. lwsl_err("%s: lws_genaes_crypt failed\n", __func__);
  422. goto bail;
  423. }
  424. if (lws_genaes_destroy(&ctx, NULL, 0)) {
  425. lwsl_err("%s: lws_genaes_destroy failed\n", __func__);
  426. return -1;
  427. }
  428. if (lws_timingsafe_bcmp(ofb_enc, res, 16)) {
  429. lwsl_err("%s: lws_genaes_crypt encoding mismatch\n", __func__);
  430. lwsl_hexdump_notice(res, 16);
  431. return -1;
  432. }
  433. iv_off = 0;
  434. if (lws_genaes_create(&ctx, LWS_GAESO_DEC, LWS_GAESM_OFB, &e, 0, NULL)) {
  435. lwsl_err("%s: lws_genaes_create dec failed\n", __func__);
  436. return -1;
  437. }
  438. if (lws_genaes_crypt(&ctx, res, 16, res1, (uint8_t *)ofb_iv, NULL,
  439. &iv_off, 0)) {
  440. lwsl_err("%s: lws_genaes_crypt dec failed\n", __func__);
  441. goto bail;
  442. }
  443. if (lws_genaes_destroy(&ctx, NULL, 0)) {
  444. lwsl_err("%s: lws_genaes_destroy failed\n", __func__);
  445. return -1;
  446. }
  447. if (lws_timingsafe_bcmp(ofb, res1, 16)) {
  448. lwsl_err("%s: lws_genaes_crypt decoding mismatch\n", __func__);
  449. lwsl_hexdump_notice(res, 16);
  450. return -1;
  451. }
  452. return 0;
  453. bail:
  454. lws_genaes_destroy(&ctx, NULL, 0);
  455. return -1;
  456. }
  457. #endif
  458. #if defined(MBEDTLS_CONFIG_H) && !defined(MBEDTLS_CIPHER_MODE_XTS)
  459. #else
  460. static const uint8_t
  461. /*
  462. * Fedora openssl tool doesn't support xts... this data produced
  463. * by testing on mbedtls + OpenSSL and getting the same result
  464. *
  465. * NOTICE that xts requires a double-length key... OpenSSL now checks
  466. * the key for duplication so we use a random key
  467. */
  468. *xts = (uint8_t *)"test plaintext\0\0",
  469. xts_enc[] = {
  470. 0x87, 0x83, 0x20, 0x8B, 0x15, 0x89, 0xA1, 0x13,
  471. 0xDC, 0xEA, 0x82, 0xB6, 0xFF, 0x8D, 0x76, 0x3A
  472. }, xts_key[] = {
  473. 0xa4, 0xd6, 0xa2, 0x1a, 0x3b, 0x34, 0x34, 0x43,
  474. 0x9a, 0xe2, 0x6a, 0x01, 0x1c, 0x73, 0x80, 0x3b,
  475. 0xdd, 0xf6, 0xd4, 0x37, 0x5e, 0x0e, 0x1c, 0x72,
  476. 0x8e, 0xe5, 0x18, 0x69, 0xfd, 0x08, 0x40, 0x2b,
  477. 0x98, 0xf9, 0x75, 0xa8, 0x36, 0xd5, 0x0f, 0xa2,
  478. 0x20, 0x04, 0x43, 0xa7, 0x3a, 0xa6, 0x4a, 0xdc,
  479. 0xe9, 0x54, 0x50, 0xfa, 0x38, 0xad, 0x6d, 0x96,
  480. 0x5f, 0x31, 0x9e, 0xcd, 0x33, 0x08, 0xa0, 0x44
  481. }
  482. ;
  483. static int
  484. test_genaes_xts(void)
  485. {
  486. struct lws_genaes_ctx ctx;
  487. struct lws_gencrypto_keyelem e;
  488. uint8_t res[32], res1[32], data_unit[16];
  489. memset(data_unit, 0, sizeof(data_unit));
  490. e.buf = (uint8_t *)xts_key;
  491. e.len = sizeof(xts_key);
  492. if (lws_genaes_create(&ctx, LWS_GAESO_ENC, LWS_GAESM_XTS, &e, 0, NULL)) {
  493. lwsl_err("%s: lws_genaes_create failed\n", __func__);
  494. return 1;
  495. }
  496. if (lws_genaes_crypt(&ctx, xts, 16, res, data_unit, NULL, NULL, 0)) {
  497. lwsl_err("%s: lws_genaes_crypt failed\n", __func__);
  498. goto bail;
  499. }
  500. if (lws_genaes_destroy(&ctx, NULL, 0)) {
  501. lwsl_err("%s: lws_genaes_destroy failed\n", __func__);
  502. return -1;
  503. }
  504. if (lws_timingsafe_bcmp(xts_enc, res, 16)) {
  505. lwsl_err("%s: lws_genaes_crypt encoding mismatch\n", __func__);
  506. lwsl_hexdump_notice(res, 16);
  507. return -1;
  508. }
  509. if (lws_genaes_create(&ctx, LWS_GAESO_DEC, LWS_GAESM_XTS, &e, 0, NULL)) {
  510. lwsl_err("%s: lws_genaes_create dec failed\n", __func__);
  511. return -1;
  512. }
  513. if (lws_genaes_crypt(&ctx, res, 16, res1, data_unit, NULL, NULL, 0)) {
  514. lwsl_err("%s: lws_genaes_crypt dec failed\n", __func__);
  515. goto bail;
  516. }
  517. if (lws_genaes_destroy(&ctx, NULL, 0)) {
  518. lwsl_err("%s: lws_genaes_destroy failed\n", __func__);
  519. return -1;
  520. }
  521. if (lws_timingsafe_bcmp(xts, res1, 16)) {
  522. lwsl_err("%s: lws_genaes_crypt decoding mismatch\n", __func__);
  523. lwsl_hexdump_notice(res, 16);
  524. return -1;
  525. }
  526. return 0;
  527. bail:
  528. lws_genaes_destroy(&ctx, NULL, 0);
  529. return -1;
  530. }
  531. #endif
  532. static const uint8_t
  533. /*
  534. * https://csrc.nist.gov/CSRC/media/Projects/
  535. * Cryptographic-Algorithm-Validation-Program/
  536. * documents/mac/gcmtestvectors.zip
  537. */
  538. gcm_ct[] = {
  539. 0xf7, 0x26, 0x44, 0x13, 0xa8, 0x4c, 0x0e, 0x7c,
  540. 0xd5, 0x36, 0x86, 0x7e, 0xb9, 0xf2, 0x17, 0x36
  541. }, gcm_iv[] = {
  542. 0x99, 0xaa, 0x3e, 0x68, 0xed, 0x81, 0x73, 0xa0,
  543. 0xee, 0xd0, 0x66, 0x84
  544. }, gcm_key[] = {
  545. 0xee, 0xbc, 0x1f, 0x57, 0x48, 0x7f, 0x51, 0x92,
  546. 0x1c, 0x04, 0x65, 0x66, 0x5f, 0x8a, 0xe6, 0xd1,
  547. 0x65, 0x8b, 0xb2, 0x6d, 0xe6, 0xf8, 0xa0, 0x69,
  548. 0xa3, 0x52, 0x02, 0x93, 0xa5, 0x72, 0x07, 0x8f
  549. }, gcm_pt[] = {
  550. 0xf5, 0x6e, 0x87, 0x05, 0x5b, 0xc3, 0x2d, 0x0e,
  551. 0xeb, 0x31, 0xb2, 0xea, 0xcc, 0x2b, 0xf2, 0xa5
  552. }, gcm_aad[] = {
  553. 0x4d, 0x23, 0xc3, 0xce, 0xc3, 0x34, 0xb4, 0x9b,
  554. 0xdb, 0x37, 0x0c, 0x43, 0x7f, 0xec, 0x78, 0xde
  555. }, gcm_tag[] = {
  556. 0x67, 0xba, 0x05, 0x10, 0x26, 0x2a, 0xe4, 0x87,
  557. 0xd7, 0x37, 0xee, 0x62, 0x98, 0xf7, 0x7e, 0x0c
  558. };
  559. static int
  560. test_genaes_gcm(void)
  561. {
  562. uint8_t res[sizeof(gcm_ct)], tag[sizeof(gcm_tag)];
  563. struct lws_genaes_ctx ctx;
  564. struct lws_gencrypto_keyelem e;
  565. size_t iv_off = 0;
  566. e.buf = (uint8_t *)gcm_key;
  567. e.len = sizeof(gcm_key);
  568. /* Encrypt */
  569. if (lws_genaes_create(&ctx, LWS_GAESO_ENC, LWS_GAESM_GCM, &e, 0, NULL)) {
  570. lwsl_err("%s: lws_genaes_create failed\n", __func__);
  571. return 1;
  572. }
  573. /* first we set the iv and aad */
  574. iv_off = sizeof(gcm_iv);
  575. if (lws_genaes_crypt(&ctx, gcm_aad, sizeof(gcm_aad), NULL,
  576. (uint8_t *)gcm_iv, (uint8_t *)gcm_tag,
  577. &iv_off, sizeof(gcm_tag))) {
  578. lwsl_err("%s: lws_genaes_crypt 1 failed\n", __func__);
  579. goto bail;
  580. }
  581. if (lws_genaes_crypt(&ctx, gcm_pt, sizeof(gcm_pt), res,
  582. NULL, NULL, NULL, 0)) {
  583. lwsl_err("%s: lws_genaes_crypt 2 failed\n", __func__);
  584. goto bail;
  585. }
  586. if (lws_genaes_destroy(&ctx, tag, sizeof(tag))) {
  587. lwsl_err("%s: lws_genaes_destroy enc failed\n", __func__);
  588. return -1;
  589. }
  590. if (lws_timingsafe_bcmp(gcm_ct, res, sizeof(gcm_ct))) {
  591. lwsl_err("%s: lws_genaes_crypt encoding mismatch\n", __func__);
  592. lwsl_hexdump_notice(res, sizeof(gcm_ct));
  593. return -1;
  594. }
  595. /* Decrypt */
  596. if (lws_genaes_create(&ctx, LWS_GAESO_DEC, LWS_GAESM_GCM, &e, 0, NULL)) {
  597. lwsl_err("%s: lws_genaes_create failed\n", __func__);
  598. return 1;
  599. }
  600. iv_off = sizeof(gcm_iv); /* initial call sets iv + aad + tag */
  601. if (lws_genaes_crypt(&ctx, gcm_aad, sizeof(gcm_aad), NULL,
  602. (uint8_t *)gcm_iv, (uint8_t *)gcm_tag,
  603. &iv_off, sizeof(gcm_tag))) {
  604. lwsl_err("%s: lws_genaes_crypt 1 failed\n", __func__);
  605. goto bail;
  606. }
  607. if (lws_genaes_crypt(&ctx, gcm_ct, sizeof(gcm_ct), res,
  608. NULL, NULL, NULL, 0)) {
  609. lwsl_err("%s: lws_genaes_crypt 2 failed\n", __func__);
  610. goto bail;
  611. }
  612. if (lws_genaes_destroy(&ctx, tag, sizeof(tag))) {
  613. lwsl_err("%s: lws_genaes_destroy dec failed\n", __func__);
  614. return -1;
  615. }
  616. if (lws_timingsafe_bcmp(gcm_pt, res, sizeof(gcm_pt))) {
  617. lwsl_err("%s: lws_genaes_crypt decoding mismatch\n", __func__);
  618. lwsl_hexdump_notice(res, sizeof(gcm_ct));
  619. return -1;
  620. }
  621. return 0;
  622. bail:
  623. lws_genaes_destroy(&ctx, NULL, 0);
  624. return -1;
  625. }
  626. int
  627. test_genaes(struct lws_context *context)
  628. {
  629. if (test_genaes_cbc())
  630. goto bail;
  631. if (test_genaes_cfb128())
  632. goto bail;
  633. if (test_genaes_cfb8())
  634. goto bail;
  635. if (test_genaes_ctr())
  636. goto bail;
  637. if (test_genaes_ecb())
  638. goto bail;
  639. #if defined(MBEDTLS_CONFIG_H) && !defined(MBEDTLS_CIPHER_MODE_OFB)
  640. #else
  641. if (test_genaes_ofb())
  642. goto bail;
  643. #endif
  644. #if defined(MBEDTLS_CONFIG_H) && !defined(MBEDTLS_CIPHER_MODE_XTS)
  645. #else
  646. if (test_genaes_xts())
  647. goto bail;
  648. #endif
  649. if (test_genaes_gcm())
  650. goto bail;
  651. /* end */
  652. lwsl_notice("%s: selftest OK\n", __func__);
  653. return 0;
  654. bail:
  655. lwsl_err("%s: selftest failed ++++++++++++++++++++\n", __func__);
  656. return 1;
  657. }