kdf.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /* kdf.c - Key Derivation Functions
  2. * Copyright (C) 1998, 2011 Free Software Foundation, Inc.
  3. *
  4. * This file is part of Libgcrypt.
  5. *
  6. * Libgcrypt is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser general Public License as
  8. * published by the Free Software Foundation; either version 2.1 of
  9. * the License, or (at your option) any later version.
  10. *
  11. * Libgcrypt is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this program; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <config.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <errno.h>
  24. #include "g10lib.h"
  25. #include "cipher.h"
  26. #include "ath.h"
  27. /* Transform a passphrase into a suitable key of length KEYSIZE and
  28. store this key in the caller provided buffer KEYBUFFER. The caller
  29. must provide an HASHALGO, a valid ALGO and depending on that algo a
  30. SALT of 8 bytes and the number of ITERATIONS. Code taken from
  31. gnupg/agent/protect.c:hash_passphrase. */
  32. gpg_err_code_t
  33. openpgp_s2k (const void *passphrase, size_t passphraselen,
  34. int algo, int hashalgo,
  35. const void *salt, size_t saltlen,
  36. unsigned long iterations,
  37. size_t keysize, void *keybuffer)
  38. {
  39. gpg_err_code_t ec;
  40. gcry_md_hd_t md;
  41. char *key = keybuffer;
  42. int pass, i;
  43. int used = 0;
  44. int secmode;
  45. if ((algo == GCRY_KDF_SALTED_S2K || algo == GCRY_KDF_ITERSALTED_S2K)
  46. && (!salt || saltlen != 8))
  47. return GPG_ERR_INV_VALUE;
  48. secmode = gcry_is_secure (passphrase) || gcry_is_secure (keybuffer);
  49. ec = gpg_err_code (gcry_md_open (&md, hashalgo,
  50. secmode? GCRY_MD_FLAG_SECURE : 0));
  51. if (ec)
  52. return ec;
  53. for (pass=0; used < keysize; pass++)
  54. {
  55. if (pass)
  56. {
  57. gcry_md_reset (md);
  58. for (i=0; i < pass; i++) /* Preset the hash context. */
  59. gcry_md_putc (md, 0);
  60. }
  61. if (algo == GCRY_KDF_SALTED_S2K || algo == GCRY_KDF_ITERSALTED_S2K)
  62. {
  63. int len2 = passphraselen + 8;
  64. unsigned long count = len2;
  65. if (algo == GCRY_KDF_ITERSALTED_S2K)
  66. {
  67. count = iterations;
  68. if (count < len2)
  69. count = len2;
  70. }
  71. while (count > len2)
  72. {
  73. gcry_md_write (md, salt, saltlen);
  74. gcry_md_write (md, passphrase, passphraselen);
  75. count -= len2;
  76. }
  77. if (count < saltlen)
  78. gcry_md_write (md, salt, count);
  79. else
  80. {
  81. gcry_md_write (md, salt, saltlen);
  82. count -= saltlen;
  83. gcry_md_write (md, passphrase, count);
  84. }
  85. }
  86. else
  87. gcry_md_write (md, passphrase, passphraselen);
  88. gcry_md_final (md);
  89. i = gcry_md_get_algo_dlen (hashalgo);
  90. if (i > keysize - used)
  91. i = keysize - used;
  92. memcpy (key+used, gcry_md_read (md, hashalgo), i);
  93. used += i;
  94. }
  95. gcry_md_close (md);
  96. return 0;
  97. }
  98. /* Transform a passphrase into a suitable key of length KEYSIZE and
  99. store this key in the caller provided buffer KEYBUFFER. The caller
  100. must provide PRFALGO which indicates the pseudorandom function to
  101. use: This shall be the algorithms id of a hash algorithm; it is
  102. used in HMAC mode. SALT is a salt of length SALTLEN and ITERATIONS
  103. gives the number of iterations. */
  104. gpg_err_code_t
  105. pkdf2 (const void *passphrase, size_t passphraselen,
  106. int hashalgo,
  107. const void *salt, size_t saltlen,
  108. unsigned long iterations,
  109. size_t keysize, void *keybuffer)
  110. {
  111. gpg_err_code_t ec;
  112. gcry_md_hd_t md;
  113. int secmode;
  114. unsigned int dklen = keysize;
  115. char *dk = keybuffer;
  116. unsigned int hlen; /* Output length of the digest function. */
  117. unsigned int l; /* Rounded up number of blocks. */
  118. unsigned int r; /* Number of octets in the last block. */
  119. char *sbuf; /* Malloced buffer to concatenate salt and iter
  120. as well as space to hold TBUF and UBUF. */
  121. char *tbuf; /* Buffer for T; ptr into SBUF, size is HLEN. */
  122. char *ubuf; /* Buffer for U; ptr into SBUF, size is HLEN. */
  123. unsigned int lidx; /* Current block number. */
  124. unsigned long iter; /* Current iteration number. */
  125. unsigned int i;
  126. if (!salt || !saltlen || !iterations || !dklen)
  127. return GPG_ERR_INV_VALUE;
  128. hlen = gcry_md_get_algo_dlen (hashalgo);
  129. if (!hlen)
  130. return GPG_ERR_DIGEST_ALGO;
  131. secmode = gcry_is_secure (passphrase) || gcry_is_secure (keybuffer);
  132. /* We ignore step 1 from pksc5v2.1 which demands a check that dklen
  133. is not larger that 0xffffffff * hlen. */
  134. /* Step 2 */
  135. l = ((dklen - 1)/ hlen) + 1;
  136. r = dklen - (l - 1) * hlen;
  137. /* Setup buffers and prepare a hash context. */
  138. sbuf = (secmode
  139. ? gcry_malloc_secure (saltlen + 4 + hlen + hlen)
  140. : gcry_malloc (saltlen + 4 + hlen + hlen));
  141. if (!sbuf)
  142. return gpg_err_code_from_syserror ();
  143. tbuf = sbuf + saltlen + 4;
  144. ubuf = tbuf + hlen;
  145. ec = gpg_err_code (gcry_md_open (&md, hashalgo,
  146. (GCRY_MD_FLAG_HMAC
  147. | (secmode?GCRY_MD_FLAG_SECURE:0))));
  148. if (ec)
  149. {
  150. gcry_free (sbuf);
  151. return ec;
  152. }
  153. /* Step 3 and 4. */
  154. memcpy (sbuf, salt, saltlen);
  155. for (lidx = 1; lidx <= l; lidx++)
  156. {
  157. for (iter = 0; iter < iterations; iter++)
  158. {
  159. ec = gpg_err_code (gcry_md_setkey (md, passphrase, passphraselen));
  160. if (ec)
  161. {
  162. gcry_md_close (md);
  163. gcry_free (sbuf);
  164. return ec;
  165. }
  166. if (!iter) /* Compute U_1: */
  167. {
  168. sbuf[saltlen] = (lidx >> 24);
  169. sbuf[saltlen + 1] = (lidx >> 16);
  170. sbuf[saltlen + 2] = (lidx >> 8);
  171. sbuf[saltlen + 3] = lidx;
  172. gcry_md_write (md, sbuf, saltlen + 4);
  173. memcpy (ubuf, gcry_md_read (md, 0), hlen);
  174. memcpy (tbuf, ubuf, hlen);
  175. }
  176. else /* Compute U_(2..c): */
  177. {
  178. gcry_md_write (md, ubuf, hlen);
  179. memcpy (ubuf, gcry_md_read (md, 0), hlen);
  180. for (i=0; i < hlen; i++)
  181. tbuf[i] ^= ubuf[i];
  182. }
  183. }
  184. if (lidx == l) /* Last block. */
  185. memcpy (dk, tbuf, r);
  186. else
  187. {
  188. memcpy (dk, tbuf, hlen);
  189. dk += hlen;
  190. }
  191. }
  192. gcry_md_close (md);
  193. gcry_free (sbuf);
  194. return 0;
  195. }
  196. /* Derive a key from a passphrase. KEYSIZE gives the requested size
  197. of the keys in octets. KEYBUFFER is a caller provided buffer
  198. filled on success with the derived key. The input passphrase is
  199. taken from (PASSPHRASE,PASSPHRASELEN) which is an arbitrary memory
  200. buffer. ALGO specifies the KDF algorithm to use; these are the
  201. constants GCRY_KDF_*. SUBALGO specifies an algorithm used
  202. internally by the KDF algorithms; this is usually a hash algorithm
  203. but certain KDF algorithm may use it differently. {SALT,SALTLEN}
  204. is a salt as needed by most KDF algorithms. ITERATIONS is a
  205. positive integer parameter to most KDFs. 0 is returned on success,
  206. or an error code on failure. */
  207. gpg_error_t
  208. gcry_kdf_derive (const void *passphrase, size_t passphraselen,
  209. int algo, int subalgo,
  210. const void *salt, size_t saltlen,
  211. unsigned long iterations,
  212. size_t keysize, void *keybuffer)
  213. {
  214. gpg_err_code_t ec;
  215. if (!passphrase || (!passphraselen && algo != GCRY_KDF_PBKDF2))
  216. {
  217. ec = GPG_ERR_INV_DATA;
  218. goto leave;
  219. }
  220. if (!keybuffer || !keysize)
  221. {
  222. ec = GPG_ERR_INV_VALUE;
  223. goto leave;
  224. }
  225. switch (algo)
  226. {
  227. case GCRY_KDF_SIMPLE_S2K:
  228. case GCRY_KDF_SALTED_S2K:
  229. case GCRY_KDF_ITERSALTED_S2K:
  230. ec = openpgp_s2k (passphrase, passphraselen, algo, subalgo,
  231. salt, saltlen, iterations, keysize, keybuffer);
  232. break;
  233. case GCRY_KDF_PBKDF1:
  234. ec = GPG_ERR_UNSUPPORTED_ALGORITHM;
  235. break;
  236. case GCRY_KDF_PBKDF2:
  237. ec = pkdf2 (passphrase, passphraselen, subalgo,
  238. salt, saltlen, iterations, keysize, keybuffer);
  239. break;
  240. default:
  241. ec = GPG_ERR_UNKNOWN_ALGORITHM;
  242. break;
  243. }
  244. leave:
  245. return gpg_error (ec);
  246. }