2
0

kmac.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. /*
  2. * Copyright 2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /*
  10. * See SP800-185 "Appendix A - KMAC, .... in Terms of Keccak[c]"
  11. *
  12. * Inputs are:
  13. * K = Key (len(K) < 2^2040 bits)
  14. * X = Input
  15. * L = Output length (0 <= L < 2^2040 bits)
  16. * S = Customization String Default="" (len(S) < 2^2040 bits)
  17. *
  18. * KMAC128(K, X, L, S)
  19. * {
  20. * newX = bytepad(encode_string(K), 168) || X || right_encode(L).
  21. * T = bytepad(encode_string("KMAC") || encode_string(S), 168).
  22. * return KECCAK[256](T || newX || 00, L).
  23. * }
  24. *
  25. * KMAC256(K, X, L, S)
  26. * {
  27. * newX = bytepad(encode_string(K), 136) || X || right_encode(L).
  28. * T = bytepad(encode_string("KMAC") || encode_string(S), 136).
  29. * return KECCAK[512](T || newX || 00, L).
  30. * }
  31. *
  32. * KMAC128XOF(K, X, L, S)
  33. * {
  34. * newX = bytepad(encode_string(K), 168) || X || right_encode(0).
  35. * T = bytepad(encode_string("KMAC") || encode_string(S), 168).
  36. * return KECCAK[256](T || newX || 00, L).
  37. * }
  38. *
  39. * KMAC256XOF(K, X, L, S)
  40. * {
  41. * newX = bytepad(encode_string(K), 136) || X || right_encode(0).
  42. * T = bytepad(encode_string("KMAC") || encode_string(S), 136).
  43. * return KECCAK[512](T || newX || 00, L).
  44. * }
  45. *
  46. */
  47. #include <stdlib.h>
  48. #include <openssl/evp.h>
  49. #include "internal/cryptlib.h"
  50. #include "internal/evp_int.h"
  51. #define KMAC_MAX_BLOCKSIZE ((1600 - 128*2) / 8) /* 168 */
  52. #define KMAC_MIN_BLOCKSIZE ((1600 - 256*2) / 8) /* 136 */
  53. /* Length encoding will be a 1 byte size + length in bits (2 bytes max) */
  54. #define KMAC_MAX_ENCODED_HEADER_LEN 3
  55. /*
  56. * Custom string max size is chosen such that:
  57. * len(encoded_string(custom) + len(kmac_encoded_string) <= KMAC_MIN_BLOCKSIZE
  58. * i.e: (KMAC_MAX_CUSTOM + KMAC_MAX_ENCODED_LEN) + 6 <= 136
  59. */
  60. #define KMAC_MAX_CUSTOM 127
  61. /* Maximum size of encoded custom string */
  62. #define KMAC_MAX_CUSTOM_ENCODED (KMAC_MAX_CUSTOM + KMAC_MAX_ENCODED_HEADER_LEN)
  63. /* Maximum key size in bytes = 2040 / 8 */
  64. #define KMAC_MAX_KEY 255
  65. /*
  66. * Maximum Encoded Key size will be padded to a multiple of the blocksize
  67. * i.e KMAC_MAX_KEY + KMAC_MAX_ENCODED_LEN = 258
  68. * Padded to a multiple of KMAC_MAX_BLOCKSIZE
  69. */
  70. #define KMAC_MAX_KEY_ENCODED (KMAC_MAX_BLOCKSIZE * 2)
  71. /* Fixed value of encode_string("KMAC") */
  72. static const unsigned char kmac_string[] = {
  73. 0x01, 0x20, 0x4B, 0x4D, 0x41, 0x43
  74. };
  75. #define KMAC_FLAG_XOF_MODE 1
  76. /* typedef EVP_MAC_IMPL */
  77. struct evp_mac_impl_st {
  78. EVP_MD_CTX *ctx;
  79. const EVP_MD *md;
  80. size_t out_len;
  81. int key_len;
  82. int custom_len;
  83. /* If xof_mode = 1 then we use right_encode(0) */
  84. int xof_mode;
  85. /* key and custom are stored in encoded form */
  86. unsigned char key[KMAC_MAX_KEY_ENCODED];
  87. unsigned char custom[KMAC_MAX_CUSTOM_ENCODED];
  88. };
  89. static int encode_string(unsigned char *out, int *out_len,
  90. const unsigned char *in, int in_len);
  91. static int right_encode(unsigned char *out, int *out_len, size_t bits);
  92. static int bytepad(unsigned char *out, int *out_len,
  93. const unsigned char *in1, int in1_len,
  94. const unsigned char *in2, int in2_len,
  95. int w);
  96. static int kmac_bytepad_encode_key(unsigned char *out, int *out_len,
  97. const unsigned char *in, int in_len,
  98. int w);
  99. static int kmac_ctrl_str(EVP_MAC_IMPL *kctx, const char *type,
  100. const char *value);
  101. static void kmac_free(EVP_MAC_IMPL *kctx)
  102. {
  103. if (kctx != NULL) {
  104. EVP_MD_CTX_free(kctx->ctx);
  105. OPENSSL_cleanse(kctx->key, kctx->key_len);
  106. OPENSSL_cleanse(kctx->custom, kctx->custom_len);
  107. OPENSSL_free(kctx);
  108. }
  109. }
  110. static EVP_MAC_IMPL *kmac_new(const EVP_MD *md)
  111. {
  112. EVP_MAC_IMPL *kctx = NULL;
  113. if ((kctx = OPENSSL_zalloc(sizeof(*kctx))) == NULL
  114. || (kctx->ctx = EVP_MD_CTX_new()) == NULL) {
  115. kmac_free(kctx);
  116. return NULL;
  117. }
  118. kctx->md = md;
  119. kctx->out_len = md->md_size;
  120. return kctx;
  121. }
  122. static EVP_MAC_IMPL *kmac128_new(void)
  123. {
  124. return kmac_new(evp_keccak_kmac128());
  125. }
  126. static EVP_MAC_IMPL *kmac256_new(void)
  127. {
  128. return kmac_new(evp_keccak_kmac256());
  129. }
  130. static EVP_MAC_IMPL *kmac_dup(const EVP_MAC_IMPL *gsrc)
  131. {
  132. EVP_MAC_IMPL *gdst;
  133. gdst = kmac_new(gsrc->md);
  134. if (gdst == NULL)
  135. return NULL;
  136. if (!EVP_MD_CTX_copy(gdst->ctx, gsrc->ctx)) {
  137. kmac_free(gdst);
  138. return NULL;
  139. }
  140. gdst->md = gsrc->md;
  141. gdst->out_len = gsrc->out_len;
  142. gdst->key_len = gsrc->key_len;
  143. gdst->custom_len = gsrc->custom_len;
  144. gdst->xof_mode = gsrc->xof_mode;
  145. memcpy(gdst->key, gsrc->key, gsrc->key_len);
  146. memcpy(gdst->custom, gsrc->custom, gdst->custom_len);
  147. return gdst;
  148. }
  149. /*
  150. * The init() assumes that any ctrl methods are set beforehand for
  151. * md, key and custom. Setting the fields afterwards will have no
  152. * effect on the output mac.
  153. */
  154. static int kmac_init(EVP_MAC_IMPL *kctx)
  155. {
  156. EVP_MD_CTX *ctx = kctx->ctx;
  157. unsigned char out[KMAC_MAX_BLOCKSIZE];
  158. int out_len, block_len;
  159. /* Check key has been set */
  160. if (kctx->key_len == 0) {
  161. EVPerr(EVP_F_KMAC_INIT, EVP_R_NO_KEY_SET);
  162. return 0;
  163. }
  164. if (!EVP_DigestInit_ex(kctx->ctx, kctx->md, NULL))
  165. return 0;
  166. block_len = EVP_MD_block_size(kctx->md);
  167. /* Set default custom string if it is not already set */
  168. if (kctx->custom_len == 0)
  169. (void)kmac_ctrl_str(kctx, "custom", "");
  170. return bytepad(out, &out_len, kmac_string, sizeof(kmac_string),
  171. kctx->custom, kctx->custom_len, block_len)
  172. && EVP_DigestUpdate(ctx, out, out_len)
  173. && EVP_DigestUpdate(ctx, kctx->key, kctx->key_len);
  174. }
  175. static size_t kmac_size(EVP_MAC_IMPL *kctx)
  176. {
  177. return kctx->out_len;
  178. }
  179. static int kmac_update(EVP_MAC_IMPL *kctx, const unsigned char *data,
  180. size_t datalen)
  181. {
  182. return EVP_DigestUpdate(kctx->ctx, data, datalen);
  183. }
  184. static int kmac_final(EVP_MAC_IMPL *kctx, unsigned char *out)
  185. {
  186. EVP_MD_CTX *ctx = kctx->ctx;
  187. int lbits, len;
  188. unsigned char encoded_outlen[KMAC_MAX_ENCODED_HEADER_LEN];
  189. /* KMAC XOF mode sets the encoded length to 0 */
  190. lbits = (kctx->xof_mode ? 0 : (kctx->out_len * 8));
  191. return right_encode(encoded_outlen, &len, lbits)
  192. && EVP_DigestUpdate(ctx, encoded_outlen, len)
  193. && EVP_DigestFinalXOF(ctx, out, kctx->out_len);
  194. }
  195. /*
  196. * The following Ctrl functions can be set any time before final():
  197. * - EVP_MAC_CTRL_SET_SIZE: The requested output length.
  198. * - EVP_MAC_CTRL_SET_XOF: If set, this indicates that right_encoded(0) is
  199. * part of the digested data, otherwise it uses
  200. * right_encoded(requested output length).
  201. * All other Ctrl functions should be set before init().
  202. */
  203. static int kmac_ctrl(EVP_MAC_IMPL *kctx, int cmd, va_list args)
  204. {
  205. const unsigned char *p;
  206. size_t len;
  207. size_t size;
  208. switch (cmd) {
  209. case EVP_MAC_CTRL_SET_XOF:
  210. kctx->xof_mode = va_arg(args, int);
  211. return 1;
  212. case EVP_MAC_CTRL_SET_SIZE:
  213. size = va_arg(args, size_t);
  214. kctx->out_len = size;
  215. return 1;
  216. case EVP_MAC_CTRL_SET_KEY:
  217. p = va_arg(args, const unsigned char *);
  218. len = va_arg(args, size_t);
  219. if (len < 4 || len > KMAC_MAX_KEY) {
  220. EVPerr(EVP_F_KMAC_CTRL, EVP_R_INVALID_KEY_LENGTH);
  221. return 0;
  222. }
  223. return kmac_bytepad_encode_key(kctx->key, &kctx->key_len, p, len,
  224. EVP_MD_block_size(kctx->md));
  225. case EVP_MAC_CTRL_SET_CUSTOM:
  226. p = va_arg(args, const unsigned char *);
  227. len = va_arg(args, size_t);
  228. if (len > KMAC_MAX_CUSTOM) {
  229. EVPerr(EVP_F_KMAC_CTRL, EVP_R_INVALID_CUSTOM_LENGTH);
  230. return 0;
  231. }
  232. return encode_string(kctx->custom, &kctx->custom_len, p, len);
  233. default:
  234. return -2;
  235. }
  236. }
  237. static int kmac_ctrl_int(EVP_MAC_IMPL *kctx, int cmd, ...)
  238. {
  239. int rv;
  240. va_list args;
  241. va_start(args, cmd);
  242. rv = kmac_ctrl(kctx, cmd, args);
  243. va_end(args);
  244. return rv;
  245. }
  246. static int kmac_ctrl_str_cb(void *kctx, int cmd, void *buf, size_t buflen)
  247. {
  248. return kmac_ctrl_int(kctx, cmd, buf, buflen);
  249. }
  250. static int kmac_ctrl_str(EVP_MAC_IMPL *kctx, const char *type,
  251. const char *value)
  252. {
  253. if (value == NULL)
  254. return 0;
  255. if (strcmp(type, "outlen") == 0)
  256. return kmac_ctrl_int(kctx, EVP_MAC_CTRL_SET_SIZE, (size_t)atoi(value));
  257. if (strcmp(type, "xof") == 0)
  258. return kmac_ctrl_int(kctx, EVP_MAC_CTRL_SET_XOF, atoi(value));
  259. if (strcmp(type, "key") == 0)
  260. return EVP_str2ctrl(kmac_ctrl_str_cb, kctx, EVP_MAC_CTRL_SET_KEY,
  261. value);
  262. if (strcmp(type, "hexkey") == 0)
  263. return EVP_hex2ctrl(kmac_ctrl_str_cb, kctx, EVP_MAC_CTRL_SET_KEY,
  264. value);
  265. if (strcmp(type, "custom") == 0)
  266. return EVP_str2ctrl(kmac_ctrl_str_cb, kctx, EVP_MAC_CTRL_SET_CUSTOM,
  267. value);
  268. if (strcmp(type, "hexcustom") == 0)
  269. return EVP_hex2ctrl(kmac_ctrl_str_cb, kctx, EVP_MAC_CTRL_SET_CUSTOM,
  270. value);
  271. return -2;
  272. }
  273. /*
  274. * Encoding/Padding Methods.
  275. */
  276. /* Returns the number of bytes required to store 'bits' into a byte array */
  277. static unsigned int get_encode_size(size_t bits)
  278. {
  279. unsigned int cnt = 0, sz = sizeof(size_t);
  280. while (bits && (cnt < sz)) {
  281. ++cnt;
  282. bits >>= 8;
  283. }
  284. /* If bits is zero 1 byte is required */
  285. if (cnt == 0)
  286. cnt = 1;
  287. return cnt;
  288. }
  289. /*
  290. * Convert an integer into bytes . The number of bytes is appended
  291. * to the end of the buffer. Returns an array of bytes 'out' of size
  292. * *out_len.
  293. *
  294. * e.g if bits = 32, out[2] = { 0x20, 0x01 }
  295. *
  296. */
  297. static int right_encode(unsigned char *out, int *out_len, size_t bits)
  298. {
  299. unsigned int len = get_encode_size(bits);
  300. int i;
  301. /* The length is constrained to a single byte: 2040/8 = 255 */
  302. if (len > 0xFF)
  303. return 0;
  304. /* MSB's are at the start of the bytes array */
  305. for (i = len - 1; i >= 0; --i) {
  306. out[i] = (unsigned char)(bits & 0xFF);
  307. bits >>= 8;
  308. }
  309. /* Tack the length onto the end */
  310. out[len] = (unsigned char)len;
  311. /* The Returned length includes the tacked on byte */
  312. *out_len = len + 1;
  313. return 1;
  314. }
  315. /*
  316. * Encodes a string with a left encoded length added. Note that the
  317. * in_len is converted to bits (*8).
  318. *
  319. * e.g- in="KMAC" gives out[6] = { 0x01, 0x20, 0x4B, 0x4D, 0x41, 0x43 }
  320. * len bits K M A C
  321. */
  322. static int encode_string(unsigned char *out, int *out_len,
  323. const unsigned char *in, int in_len)
  324. {
  325. if (in == NULL) {
  326. *out_len = 0;
  327. } else {
  328. int i, bits, len;
  329. bits = 8 * in_len;
  330. len = get_encode_size(bits);
  331. if (len > 0xFF)
  332. return 0;
  333. out[0] = len;
  334. for (i = len; i > 0; --i) {
  335. out[i] = (bits & 0xFF);
  336. bits >>= 8;
  337. }
  338. memcpy(out + len + 1, in, in_len);
  339. *out_len = (1 + len + in_len);
  340. }
  341. return 1;
  342. }
  343. /*
  344. * Returns a zero padded encoding of the inputs in1 and an optional
  345. * in2 (can be NULL). The padded output must be a multiple of the blocksize 'w'.
  346. * The value of w is in bytes (< 256).
  347. *
  348. * The returned output is:
  349. * zero_padded(multiple of w, (left_encode(w) || in1 [|| in2])
  350. */
  351. static int bytepad(unsigned char *out, int *out_len,
  352. const unsigned char *in1, int in1_len,
  353. const unsigned char *in2, int in2_len, int w)
  354. {
  355. int len;
  356. unsigned char *p = out;
  357. int sz = w;
  358. /* Left encoded w */
  359. *p++ = 1;
  360. *p++ = w;
  361. /* || in1 */
  362. memcpy(p, in1, in1_len);
  363. p += in1_len;
  364. /* [ || in2 ] */
  365. if (in2 != NULL && in2_len > 0) {
  366. memcpy(p, in2, in2_len);
  367. p += in2_len;
  368. }
  369. /* Figure out the pad size (divisible by w) */
  370. len = p - out;
  371. while (len > sz) {
  372. sz += w;
  373. }
  374. /* zero pad the end of the buffer */
  375. memset(p, 0, sz - len);
  376. *out_len = sz;
  377. return 1;
  378. }
  379. /*
  380. * Returns out = bytepad(encode_string(in), w)
  381. */
  382. static int kmac_bytepad_encode_key(unsigned char *out, int *out_len,
  383. const unsigned char *in, int in_len,
  384. int w)
  385. {
  386. unsigned char tmp[KMAC_MAX_KEY + KMAC_MAX_ENCODED_HEADER_LEN];
  387. int tmp_len;
  388. if (!encode_string(tmp, &tmp_len, in, in_len))
  389. return 0;
  390. return bytepad(out, out_len, tmp, tmp_len, NULL, 0, w);
  391. }
  392. const EVP_MAC kmac128_meth = {
  393. EVP_MAC_KMAC128,
  394. kmac128_new,
  395. kmac_dup,
  396. kmac_free,
  397. kmac_size,
  398. kmac_init,
  399. kmac_update,
  400. kmac_final,
  401. kmac_ctrl,
  402. kmac_ctrl_str
  403. };
  404. const EVP_MAC kmac256_meth = {
  405. EVP_MAC_KMAC256,
  406. kmac256_new,
  407. kmac_dup,
  408. kmac_free,
  409. kmac_size,
  410. kmac_init,
  411. kmac_update,
  412. kmac_final,
  413. kmac_ctrl,
  414. kmac_ctrl_str
  415. };