hkdf.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Implementation of HKDF ("HMAC-based Extract-and-Expand Key Derivation
  4. * Function"), aka RFC 5869. See also the original paper (Krawczyk 2010):
  5. * "Cryptographic Extraction and Key Derivation: The HKDF Scheme".
  6. *
  7. * Copyright 2019 Google LLC
  8. */
  9. #include <crypto/internal/hash.h>
  10. #include <crypto/sha2.h>
  11. #include <crypto/hkdf.h>
  12. #include <linux/module.h>
  13. /*
  14. * HKDF consists of two steps:
  15. *
  16. * 1. HKDF-Extract: extract a pseudorandom key from the input keying material
  17. * and optional salt.
  18. * 2. HKDF-Expand: expand the pseudorandom key into output keying material of
  19. * any length, parameterized by an application-specific info string.
  20. *
  21. */
  22. /**
  23. * hkdf_extract - HKDF-Extract (RFC 5869 section 2.2)
  24. * @hmac_tfm: an HMAC transform using the hash function desired for HKDF. The
  25. * caller is responsible for setting the @prk afterwards.
  26. * @ikm: input keying material
  27. * @ikmlen: length of @ikm
  28. * @salt: input salt value
  29. * @saltlen: length of @salt
  30. * @prk: resulting pseudorandom key
  31. *
  32. * Extracts a pseudorandom key @prk from the input keying material
  33. * @ikm with length @ikmlen and salt @salt with length @saltlen.
  34. * The length of @prk is given by the digest size of @hmac_tfm.
  35. * For an 'unsalted' version of HKDF-Extract @salt must be set
  36. * to all zeroes and @saltlen must be set to the length of @prk.
  37. *
  38. * Returns 0 on success with the pseudorandom key stored in @prk,
  39. * or a negative errno value otherwise.
  40. */
  41. int hkdf_extract(struct crypto_shash *hmac_tfm, const u8 *ikm,
  42. unsigned int ikmlen, const u8 *salt, unsigned int saltlen,
  43. u8 *prk)
  44. {
  45. int err;
  46. err = crypto_shash_setkey(hmac_tfm, salt, saltlen);
  47. if (!err)
  48. err = crypto_shash_tfm_digest(hmac_tfm, ikm, ikmlen, prk);
  49. return err;
  50. }
  51. EXPORT_SYMBOL_GPL(hkdf_extract);
  52. /**
  53. * hkdf_expand - HKDF-Expand (RFC 5869 section 2.3)
  54. * @hmac_tfm: hash context keyed with pseudorandom key
  55. * @info: application-specific information
  56. * @infolen: length of @info
  57. * @okm: output keying material
  58. * @okmlen: length of @okm
  59. *
  60. * This expands the pseudorandom key, which was already keyed into @hmac_tfm,
  61. * into @okmlen bytes of output keying material parameterized by the
  62. * application-specific @info of length @infolen bytes.
  63. * This is thread-safe and may be called by multiple threads in parallel.
  64. *
  65. * Returns 0 on success with output keying material stored in @okm,
  66. * or a negative errno value otherwise.
  67. */
  68. int hkdf_expand(struct crypto_shash *hmac_tfm,
  69. const u8 *info, unsigned int infolen,
  70. u8 *okm, unsigned int okmlen)
  71. {
  72. SHASH_DESC_ON_STACK(desc, hmac_tfm);
  73. unsigned int i, hashlen = crypto_shash_digestsize(hmac_tfm);
  74. int err;
  75. const u8 *prev = NULL;
  76. u8 counter = 1;
  77. u8 tmp[HASH_MAX_DIGESTSIZE] = {};
  78. if (WARN_ON(okmlen > 255 * hashlen))
  79. return -EINVAL;
  80. desc->tfm = hmac_tfm;
  81. for (i = 0; i < okmlen; i += hashlen) {
  82. err = crypto_shash_init(desc);
  83. if (err)
  84. goto out;
  85. if (prev) {
  86. err = crypto_shash_update(desc, prev, hashlen);
  87. if (err)
  88. goto out;
  89. }
  90. if (infolen) {
  91. err = crypto_shash_update(desc, info, infolen);
  92. if (err)
  93. goto out;
  94. }
  95. BUILD_BUG_ON(sizeof(counter) != 1);
  96. if (okmlen - i < hashlen) {
  97. err = crypto_shash_finup(desc, &counter, 1, tmp);
  98. if (err)
  99. goto out;
  100. memcpy(&okm[i], tmp, okmlen - i);
  101. memzero_explicit(tmp, sizeof(tmp));
  102. } else {
  103. err = crypto_shash_finup(desc, &counter, 1, &okm[i]);
  104. if (err)
  105. goto out;
  106. }
  107. counter++;
  108. prev = &okm[i];
  109. }
  110. err = 0;
  111. out:
  112. if (unlikely(err))
  113. memzero_explicit(okm, okmlen); /* so caller doesn't need to */
  114. shash_desc_zero(desc);
  115. memzero_explicit(tmp, HASH_MAX_DIGESTSIZE);
  116. return err;
  117. }
  118. EXPORT_SYMBOL_GPL(hkdf_expand);
  119. struct hkdf_testvec {
  120. const char *test;
  121. const u8 *ikm;
  122. const u8 *salt;
  123. const u8 *info;
  124. const u8 *prk;
  125. const u8 *okm;
  126. u16 ikm_size;
  127. u16 salt_size;
  128. u16 info_size;
  129. u16 prk_size;
  130. u16 okm_size;
  131. };
  132. /*
  133. * HKDF test vectors from RFC5869
  134. *
  135. * Additional HKDF test vectors from
  136. * https://github.com/brycx/Test-Vector-Generation/blob/master/HKDF/hkdf-hmac-sha2-test-vectors.md
  137. */
  138. static const struct hkdf_testvec hkdf_sha256_tv[] = {
  139. {
  140. .test = "basic hdkf test",
  141. .ikm = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  142. "\x0b\x0b\x0b\x0b\x0b\x0b",
  143. .ikm_size = 22,
  144. .salt = "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c",
  145. .salt_size = 13,
  146. .info = "\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9",
  147. .info_size = 10,
  148. .prk = "\x07\x77\x09\x36\x2c\x2e\x32\xdf\x0d\xdc\x3f\x0d\xc4\x7b\xba\x63"
  149. "\x90\xb6\xc7\x3b\xb5\x0f\x9c\x31\x22\xec\x84\x4a\xd7\xc2\xb3\xe5",
  150. .prk_size = 32,
  151. .okm = "\x3c\xb2\x5f\x25\xfa\xac\xd5\x7a\x90\x43\x4f\x64\xd0\x36\x2f\x2a"
  152. "\x2d\x2d\x0a\x90\xcf\x1a\x5a\x4c\x5d\xb0\x2d\x56\xec\xc4\xc5\xbf"
  153. "\x34\x00\x72\x08\xd5\xb8\x87\x18\x58\x65",
  154. .okm_size = 42,
  155. }, {
  156. .test = "hkdf test with long input",
  157. .ikm = "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"
  158. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"
  159. "\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f"
  160. "\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f"
  161. "\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f",
  162. .ikm_size = 80,
  163. .salt = "\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f"
  164. "\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x7b\x7c\x7d\x7e\x7f"
  165. "\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f"
  166. "\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f"
  167. "\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf",
  168. .salt_size = 80,
  169. .info = "\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf"
  170. "\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf"
  171. "\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf"
  172. "\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef"
  173. "\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff",
  174. .info_size = 80,
  175. .prk = "\x06\xa6\xb8\x8c\x58\x53\x36\x1a\x06\x10\x4c\x9c\xeb\x35\xb4\x5c"
  176. "\xef\x76\x00\x14\x90\x46\x71\x01\x4a\x19\x3f\x40\xc1\x5f\xc2\x44",
  177. .prk_size = 32,
  178. .okm = "\xb1\x1e\x39\x8d\xc8\x03\x27\xa1\xc8\xe7\xf7\x8c\x59\x6a\x49\x34"
  179. "\x4f\x01\x2e\xda\x2d\x4e\xfa\xd8\xa0\x50\xcc\x4c\x19\xaf\xa9\x7c"
  180. "\x59\x04\x5a\x99\xca\xc7\x82\x72\x71\xcb\x41\xc6\x5e\x59\x0e\x09"
  181. "\xda\x32\x75\x60\x0c\x2f\x09\xb8\x36\x77\x93\xa9\xac\xa3\xdb\x71"
  182. "\xcc\x30\xc5\x81\x79\xec\x3e\x87\xc1\x4c\x01\xd5\xc1\xf3\x43\x4f"
  183. "\x1d\x87",
  184. .okm_size = 82,
  185. }, {
  186. .test = "hkdf test with zero salt and info",
  187. .ikm = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  188. "\x0b\x0b\x0b\x0b\x0b\x0b",
  189. .ikm_size = 22,
  190. .salt = NULL,
  191. .salt_size = 0,
  192. .info = NULL,
  193. .info_size = 0,
  194. .prk = "\x19\xef\x24\xa3\x2c\x71\x7b\x16\x7f\x33\xa9\x1d\x6f\x64\x8b\xdf"
  195. "\x96\x59\x67\x76\xaf\xdb\x63\x77\xac\x43\x4c\x1c\x29\x3c\xcb\x04",
  196. .prk_size = 32,
  197. .okm = "\x8d\xa4\xe7\x75\xa5\x63\xc1\x8f\x71\x5f\x80\x2a\x06\x3c\x5a\x31"
  198. "\xb8\xa1\x1f\x5c\x5e\xe1\x87\x9e\xc3\x45\x4e\x5f\x3c\x73\x8d\x2d"
  199. "\x9d\x20\x13\x95\xfa\xa4\xb6\x1a\x96\xc8",
  200. .okm_size = 42,
  201. }, {
  202. .test = "hkdf test with short input",
  203. .ikm = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  204. .ikm_size = 11,
  205. .salt = "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c",
  206. .salt_size = 13,
  207. .info = "\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9",
  208. .info_size = 10,
  209. .prk = "\x82\x65\xf6\x9d\x7f\xf7\xe5\x01\x37\x93\x01\x5c\xa0\xef\x92\x0c"
  210. "\xb1\x68\x21\x99\xc8\xbc\x3a\x00\xda\x0c\xab\x47\xb7\xb0\x0f\xdf",
  211. .prk_size = 32,
  212. .okm = "\x58\xdc\xe1\x0d\x58\x01\xcd\xfd\xa8\x31\x72\x6b\xfe\xbc\xb7\x43"
  213. "\xd1\x4a\x7e\xe8\x3a\xa0\x57\xa9\x3d\x59\xb0\xa1\x31\x7f\xf0\x9d"
  214. "\x10\x5c\xce\xcf\x53\x56\x92\xb1\x4d\xd5",
  215. .okm_size = 42,
  216. }, {
  217. .test = "unsalted hkdf test with zero info",
  218. .ikm = "\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c"
  219. "\x0c\x0c\x0c\x0c\x0c\x0c",
  220. .ikm_size = 22,
  221. .salt = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  222. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  223. .salt_size = 32,
  224. .info = NULL,
  225. .info_size = 0,
  226. .prk = "\xaa\x84\x1e\x1f\x35\x74\xf3\x2d\x13\xfb\xa8\x00\x5f\xcd\x9b\x8d"
  227. "\x77\x67\x82\xa5\xdf\xa1\x92\x38\x92\xfd\x8b\x63\x5d\x3a\x89\xdf",
  228. .prk_size = 32,
  229. .okm = "\x59\x68\x99\x17\x9a\xb1\xbc\x00\xa7\xc0\x37\x86\xff\x43\xee\x53"
  230. "\x50\x04\xbe\x2b\xb9\xbe\x68\xbc\x14\x06\x63\x6f\x54\xbd\x33\x8a"
  231. "\x66\xa2\x37\xba\x2a\xcb\xce\xe3\xc9\xa7",
  232. .okm_size = 42,
  233. }
  234. };
  235. static const struct hkdf_testvec hkdf_sha384_tv[] = {
  236. {
  237. .test = "basic hkdf test",
  238. .ikm = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  239. "\x0b\x0b\x0b\x0b\x0b\x0b",
  240. .ikm_size = 22,
  241. .salt = "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c",
  242. .salt_size = 13,
  243. .info = "\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9",
  244. .info_size = 10,
  245. .prk = "\x70\x4b\x39\x99\x07\x79\xce\x1d\xc5\x48\x05\x2c\x7d\xc3\x9f\x30"
  246. "\x35\x70\xdd\x13\xfb\x39\xf7\xac\xc5\x64\x68\x0b\xef\x80\xe8\xde"
  247. "\xc7\x0e\xe9\xa7\xe1\xf3\xe2\x93\xef\x68\xec\xeb\x07\x2a\x5a\xde",
  248. .prk_size = 48,
  249. .okm = "\x9b\x50\x97\xa8\x60\x38\xb8\x05\x30\x90\x76\xa4\x4b\x3a\x9f\x38"
  250. "\x06\x3e\x25\xb5\x16\xdc\xbf\x36\x9f\x39\x4c\xfa\xb4\x36\x85\xf7"
  251. "\x48\xb6\x45\x77\x63\xe4\xf0\x20\x4f\xc5",
  252. .okm_size = 42,
  253. }, {
  254. .test = "hkdf test with long input",
  255. .ikm = "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"
  256. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"
  257. "\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f"
  258. "\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f"
  259. "\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f",
  260. .ikm_size = 80,
  261. .salt = "\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f"
  262. "\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x7b\x7c\x7d\x7e\x7f"
  263. "\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f"
  264. "\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f"
  265. "\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf",
  266. .salt_size = 80,
  267. .info = "\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf"
  268. "\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf"
  269. "\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf"
  270. "\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef"
  271. "\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff",
  272. .info_size = 80,
  273. .prk = "\xb3\x19\xf6\x83\x1d\xff\x93\x14\xef\xb6\x43\xba\xa2\x92\x63\xb3"
  274. "\x0e\x4a\x8d\x77\x9f\xe3\x1e\x9c\x90\x1e\xfd\x7d\xe7\x37\xc8\x5b"
  275. "\x62\xe6\x76\xd4\xdc\x87\xb0\x89\x5c\x6a\x7d\xc9\x7b\x52\xce\xbb",
  276. .prk_size = 48,
  277. .okm = "\x48\x4c\xa0\x52\xb8\xcc\x72\x4f\xd1\xc4\xec\x64\xd5\x7b\x4e\x81"
  278. "\x8c\x7e\x25\xa8\xe0\xf4\x56\x9e\xd7\x2a\x6a\x05\xfe\x06\x49\xee"
  279. "\xbf\x69\xf8\xd5\xc8\x32\x85\x6b\xf4\xe4\xfb\xc1\x79\x67\xd5\x49"
  280. "\x75\x32\x4a\x94\x98\x7f\x7f\x41\x83\x58\x17\xd8\x99\x4f\xdb\xd6"
  281. "\xf4\xc0\x9c\x55\x00\xdc\xa2\x4a\x56\x22\x2f\xea\x53\xd8\x96\x7a"
  282. "\x8b\x2e",
  283. .okm_size = 82,
  284. }, {
  285. .test = "hkdf test with zero salt and info",
  286. .ikm = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  287. "\x0b\x0b\x0b\x0b\x0b\x0b",
  288. .ikm_size = 22,
  289. .salt = NULL,
  290. .salt_size = 0,
  291. .info = NULL,
  292. .info_size = 0,
  293. .prk = "\x10\xe4\x0c\xf0\x72\xa4\xc5\x62\x6e\x43\xdd\x22\xc1\xcf\x72\x7d"
  294. "\x4b\xb1\x40\x97\x5c\x9a\xd0\xcb\xc8\xe4\x5b\x40\x06\x8f\x8f\x0b"
  295. "\xa5\x7c\xdb\x59\x8a\xf9\xdf\xa6\x96\x3a\x96\x89\x9a\xf0\x47\xe5",
  296. .prk_size = 48,
  297. .okm = "\xc8\xc9\x6e\x71\x0f\x89\xb0\xd7\x99\x0b\xca\x68\xbc\xde\xc8\xcf"
  298. "\x85\x40\x62\xe5\x4c\x73\xa7\xab\xc7\x43\xfa\xde\x9b\x24\x2d\xaa"
  299. "\xcc\x1c\xea\x56\x70\x41\x5b\x52\x84\x9c",
  300. .okm_size = 42,
  301. }, {
  302. .test = "hkdf test with short input",
  303. .ikm = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  304. .ikm_size = 11,
  305. .salt = "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c",
  306. .salt_size = 13,
  307. .info = "\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9",
  308. .info_size = 10,
  309. .prk = "\x6d\x31\x69\x98\x28\x79\x80\x88\xb3\x59\xda\xd5\x0b\x8f\x01\xb0"
  310. "\x15\xf1\x7a\xa3\xbd\x4e\x27\xa6\xe9\xf8\x73\xb7\x15\x85\xca\x6a"
  311. "\x00\xd1\xf0\x82\x12\x8a\xdb\x3c\xf0\x53\x0b\x57\xc0\xf9\xac\x72",
  312. .prk_size = 48,
  313. .okm = "\xfb\x7e\x67\x43\xeb\x42\xcd\xe9\x6f\x1b\x70\x77\x89\x52\xab\x75"
  314. "\x48\xca\xfe\x53\x24\x9f\x7f\xfe\x14\x97\xa1\x63\x5b\x20\x1f\xf1"
  315. "\x85\xb9\x3e\x95\x19\x92\xd8\x58\xf1\x1a",
  316. .okm_size = 42,
  317. }, {
  318. .test = "unsalted hkdf test with zero info",
  319. .ikm = "\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c"
  320. "\x0c\x0c\x0c\x0c\x0c\x0c",
  321. .ikm_size = 22,
  322. .salt = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  323. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  324. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  325. .salt_size = 48,
  326. .info = NULL,
  327. .info_size = 0,
  328. .prk = "\x9d\x2d\xa5\x06\x6f\x05\xd1\x6c\x59\xfe\xdf\x6c\x5f\x32\xc7\x5e"
  329. "\xda\x9a\x47\xa7\x9c\x93\x6a\xa4\x4c\xb7\x63\xa8\xe2\x2f\xfb\xfc"
  330. "\xd8\xfe\x55\x43\x58\x53\x47\x21\x90\x39\xd1\x68\x28\x36\x33\xf5",
  331. .prk_size = 48,
  332. .okm = "\x6a\xd7\xc7\x26\xc8\x40\x09\x54\x6a\x76\xe0\x54\x5d\xf2\x66\x78"
  333. "\x7e\x2b\x2c\xd6\xca\x43\x73\xa1\xf3\x14\x50\xa7\xbd\xf9\x48\x2b"
  334. "\xfa\xb8\x11\xf5\x54\x20\x0e\xad\x8f\x53",
  335. .okm_size = 42,
  336. }
  337. };
  338. static const struct hkdf_testvec hkdf_sha512_tv[] = {
  339. {
  340. .test = "basic hkdf test",
  341. .ikm = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  342. "\x0b\x0b\x0b\x0b\x0b\x0b",
  343. .ikm_size = 22,
  344. .salt = "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c",
  345. .salt_size = 13,
  346. .info = "\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9",
  347. .info_size = 10,
  348. .prk = "\x66\x57\x99\x82\x37\x37\xde\xd0\x4a\x88\xe4\x7e\x54\xa5\x89\x0b"
  349. "\xb2\xc3\xd2\x47\xc7\xa4\x25\x4a\x8e\x61\x35\x07\x23\x59\x0a\x26"
  350. "\xc3\x62\x38\x12\x7d\x86\x61\xb8\x8c\xf8\x0e\xf8\x02\xd5\x7e\x2f"
  351. "\x7c\xeb\xcf\x1e\x00\xe0\x83\x84\x8b\xe1\x99\x29\xc6\x1b\x42\x37",
  352. .prk_size = 64,
  353. .okm = "\x83\x23\x90\x08\x6c\xda\x71\xfb\x47\x62\x5b\xb5\xce\xb1\x68\xe4"
  354. "\xc8\xe2\x6a\x1a\x16\xed\x34\xd9\xfc\x7f\xe9\x2c\x14\x81\x57\x93"
  355. "\x38\xda\x36\x2c\xb8\xd9\xf9\x25\xd7\xcb",
  356. .okm_size = 42,
  357. }, {
  358. .test = "hkdf test with long input",
  359. .ikm = "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"
  360. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"
  361. "\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f"
  362. "\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f"
  363. "\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f",
  364. .ikm_size = 80,
  365. .salt = "\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f"
  366. "\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x7b\x7c\x7d\x7e\x7f"
  367. "\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f"
  368. "\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f"
  369. "\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf",
  370. .salt_size = 80,
  371. .info = "\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf"
  372. "\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf"
  373. "\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf"
  374. "\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef"
  375. "\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff",
  376. .info_size = 80,
  377. .prk = "\x35\x67\x25\x42\x90\x7d\x4e\x14\x2c\x00\xe8\x44\x99\xe7\x4e\x1d"
  378. "\xe0\x8b\xe8\x65\x35\xf9\x24\xe0\x22\x80\x4a\xd7\x75\xdd\xe2\x7e"
  379. "\xc8\x6c\xd1\xe5\xb7\xd1\x78\xc7\x44\x89\xbd\xbe\xb3\x07\x12\xbe"
  380. "\xb8\x2d\x4f\x97\x41\x6c\x5a\x94\xea\x81\xeb\xdf\x3e\x62\x9e\x4a",
  381. .prk_size = 64,
  382. .okm = "\xce\x6c\x97\x19\x28\x05\xb3\x46\xe6\x16\x1e\x82\x1e\xd1\x65\x67"
  383. "\x3b\x84\xf4\x00\xa2\xb5\x14\xb2\xfe\x23\xd8\x4c\xd1\x89\xdd\xf1"
  384. "\xb6\x95\xb4\x8c\xbd\x1c\x83\x88\x44\x11\x37\xb3\xce\x28\xf1\x6a"
  385. "\xa6\x4b\xa3\x3b\xa4\x66\xb2\x4d\xf6\xcf\xcb\x02\x1e\xcf\xf2\x35"
  386. "\xf6\xa2\x05\x6c\xe3\xaf\x1d\xe4\x4d\x57\x20\x97\xa8\x50\x5d\x9e"
  387. "\x7a\x93",
  388. .okm_size = 82,
  389. }, {
  390. .test = "hkdf test with zero salt and info",
  391. .ikm = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  392. "\x0b\x0b\x0b\x0b\x0b\x0b",
  393. .ikm_size = 22,
  394. .salt = NULL,
  395. .salt_size = 0,
  396. .info = NULL,
  397. .info_size = 0,
  398. .prk = "\xfd\x20\x0c\x49\x87\xac\x49\x13\x13\xbd\x4a\x2a\x13\x28\x71\x21"
  399. "\x24\x72\x39\xe1\x1c\x9e\xf8\x28\x02\x04\x4b\x66\xef\x35\x7e\x5b"
  400. "\x19\x44\x98\xd0\x68\x26\x11\x38\x23\x48\x57\x2a\x7b\x16\x11\xde"
  401. "\x54\x76\x40\x94\x28\x63\x20\x57\x8a\x86\x3f\x36\x56\x2b\x0d\xf6",
  402. .prk_size = 64,
  403. .okm = "\xf5\xfa\x02\xb1\x82\x98\xa7\x2a\x8c\x23\x89\x8a\x87\x03\x47\x2c"
  404. "\x6e\xb1\x79\xdc\x20\x4c\x03\x42\x5c\x97\x0e\x3b\x16\x4b\xf9\x0f"
  405. "\xff\x22\xd0\x48\x36\xd0\xe2\x34\x3b\xac",
  406. .okm_size = 42,
  407. }, {
  408. .test = "hkdf test with short input",
  409. .ikm = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  410. .ikm_size = 11,
  411. .salt = "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c",
  412. .salt_size = 13,
  413. .info = "\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9",
  414. .info_size = 10,
  415. .prk = "\x67\x40\x9c\x9c\xac\x28\xb5\x2e\xe9\xfa\xd9\x1c\x2f\xda\x99\x9f"
  416. "\x7c\xa2\x2e\x34\x34\xf0\xae\x77\x28\x63\x83\x65\x68\xad\x6a\x7f"
  417. "\x10\xcf\x11\x3b\xfd\xdd\x56\x01\x29\xa5\x94\xa8\xf5\x23\x85\xc2"
  418. "\xd6\x61\xd7\x85\xd2\x9c\xe9\x3a\x11\x40\x0c\x92\x06\x83\x18\x1d",
  419. .prk_size = 64,
  420. .okm = "\x74\x13\xe8\x99\x7e\x02\x06\x10\xfb\xf6\x82\x3f\x2c\xe1\x4b\xff"
  421. "\x01\x87\x5d\xb1\xca\x55\xf6\x8c\xfc\xf3\x95\x4d\xc8\xaf\xf5\x35"
  422. "\x59\xbd\x5e\x30\x28\xb0\x80\xf7\xc0\x68",
  423. .okm_size = 42,
  424. }, {
  425. .test = "unsalted hkdf test with zero info",
  426. .ikm = "\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c\x0c"
  427. "\x0c\x0c\x0c\x0c\x0c\x0c",
  428. .ikm_size = 22,
  429. .salt = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  430. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  431. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  432. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  433. .salt_size = 64,
  434. .info = NULL,
  435. .info_size = 0,
  436. .prk = "\x53\x46\xb3\x76\xbf\x3a\xa9\xf8\x4f\x8f\x6e\xd5\xb1\xc4\xf4\x89"
  437. "\x17\x2e\x24\x4d\xac\x30\x3d\x12\xf6\x8e\xcc\x76\x6e\xa6\x00\xaa"
  438. "\x88\x49\x5e\x7f\xb6\x05\x80\x31\x22\xfa\x13\x69\x24\xa8\x40\xb1"
  439. "\xf0\x71\x9d\x2d\x5f\x68\xe2\x9b\x24\x22\x99\xd7\x58\xed\x68\x0c",
  440. .prk_size = 64,
  441. .okm = "\x14\x07\xd4\x60\x13\xd9\x8b\xc6\xde\xce\xfc\xfe\xe5\x5f\x0f\x90"
  442. "\xb0\xc7\xf6\x3d\x68\xeb\x1a\x80\xea\xf0\x7e\x95\x3c\xfc\x0a\x3a"
  443. "\x52\x40\xa1\x55\xd6\xe4\xda\xa9\x65\xbb",
  444. .okm_size = 42,
  445. }
  446. };
  447. static int hkdf_test(const char *shash, const struct hkdf_testvec *tv)
  448. { struct crypto_shash *tfm = NULL;
  449. u8 *prk = NULL, *okm = NULL;
  450. unsigned int prk_size;
  451. const char *driver;
  452. int err;
  453. tfm = crypto_alloc_shash(shash, 0, 0);
  454. if (IS_ERR(tfm)) {
  455. pr_err("%s(%s): failed to allocate transform: %ld\n",
  456. tv->test, shash, PTR_ERR(tfm));
  457. return PTR_ERR(tfm);
  458. }
  459. driver = crypto_shash_driver_name(tfm);
  460. prk_size = crypto_shash_digestsize(tfm);
  461. prk = kzalloc(prk_size, GFP_KERNEL);
  462. if (!prk) {
  463. err = -ENOMEM;
  464. goto out_free;
  465. }
  466. if (tv->prk_size != prk_size) {
  467. pr_err("%s(%s): prk size mismatch (vec %u, digest %u\n",
  468. tv->test, driver, tv->prk_size, prk_size);
  469. err = -EINVAL;
  470. goto out_free;
  471. }
  472. err = hkdf_extract(tfm, tv->ikm, tv->ikm_size,
  473. tv->salt, tv->salt_size, prk);
  474. if (err) {
  475. pr_err("%s(%s): hkdf_extract failed with %d\n",
  476. tv->test, driver, err);
  477. goto out_free;
  478. }
  479. if (memcmp(prk, tv->prk, tv->prk_size)) {
  480. pr_err("%s(%s): hkdf_extract prk mismatch\n",
  481. tv->test, driver);
  482. print_hex_dump(KERN_ERR, "prk: ", DUMP_PREFIX_NONE,
  483. 16, 1, prk, tv->prk_size, false);
  484. err = -EINVAL;
  485. goto out_free;
  486. }
  487. okm = kzalloc(tv->okm_size, GFP_KERNEL);
  488. if (!okm) {
  489. err = -ENOMEM;
  490. goto out_free;
  491. }
  492. err = crypto_shash_setkey(tfm, tv->prk, tv->prk_size);
  493. if (err) {
  494. pr_err("%s(%s): failed to set prk, error %d\n",
  495. tv->test, driver, err);
  496. goto out_free;
  497. }
  498. err = hkdf_expand(tfm, tv->info, tv->info_size,
  499. okm, tv->okm_size);
  500. if (err) {
  501. pr_err("%s(%s): hkdf_expand() failed with %d\n",
  502. tv->test, driver, err);
  503. } else if (memcmp(okm, tv->okm, tv->okm_size)) {
  504. pr_err("%s(%s): hkdf_expand() okm mismatch\n",
  505. tv->test, driver);
  506. print_hex_dump(KERN_ERR, "okm: ", DUMP_PREFIX_NONE,
  507. 16, 1, okm, tv->okm_size, false);
  508. err = -EINVAL;
  509. }
  510. out_free:
  511. kfree(okm);
  512. kfree(prk);
  513. crypto_free_shash(tfm);
  514. return err;
  515. }
  516. static int __init crypto_hkdf_module_init(void)
  517. {
  518. int ret = 0, i;
  519. if (!IS_ENABLED(CONFIG_CRYPTO_SELFTESTS))
  520. return 0;
  521. for (i = 0; i < ARRAY_SIZE(hkdf_sha256_tv); i++) {
  522. ret = hkdf_test("hmac(sha256)", &hkdf_sha256_tv[i]);
  523. if (ret)
  524. return ret;
  525. }
  526. for (i = 0; i < ARRAY_SIZE(hkdf_sha384_tv); i++) {
  527. ret = hkdf_test("hmac(sha384)", &hkdf_sha384_tv[i]);
  528. if (ret)
  529. return ret;
  530. }
  531. for (i = 0; i < ARRAY_SIZE(hkdf_sha512_tv); i++) {
  532. ret = hkdf_test("hmac(sha512)", &hkdf_sha512_tv[i]);
  533. if (ret)
  534. return ret;
  535. }
  536. return 0;
  537. }
  538. static void __exit crypto_hkdf_module_exit(void) {}
  539. late_initcall(crypto_hkdf_module_init);
  540. module_exit(crypto_hkdf_module_exit);
  541. MODULE_LICENSE("GPL");
  542. MODULE_DESCRIPTION("HMAC-based Key Derivation Function (HKDF)");