p5_scrypt.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*
  2. * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
  3. * 2015.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 2015 The OpenSSL Project. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. All advertising materials mentioning features or use of this
  21. * software must display the following acknowledgment:
  22. * "This product includes software developed by the OpenSSL Project
  23. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  24. *
  25. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  26. * endorse or promote products derived from this software without
  27. * prior written permission. For written permission, please contact
  28. * licensing@OpenSSL.org.
  29. *
  30. * 5. Products derived from this software may not be called "OpenSSL"
  31. * nor may "OpenSSL" appear in their names without prior written
  32. * permission of the OpenSSL Project.
  33. *
  34. * 6. Redistributions of any form whatsoever must retain the following
  35. * acknowledgment:
  36. * "This product includes software developed by the OpenSSL Project
  37. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  38. *
  39. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  40. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  42. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  43. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  44. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  45. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  46. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  48. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  49. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  50. * OF THE POSSIBILITY OF SUCH DAMAGE.
  51. * ====================================================================
  52. *
  53. * This product includes cryptographic software written by Eric Young
  54. * (eay@cryptsoft.com). This product includes software written by Tim
  55. * Hudson (tjh@cryptsoft.com).
  56. *
  57. */
  58. #include <stdio.h>
  59. #include "internal/cryptlib.h"
  60. #include <openssl/asn1t.h>
  61. #include <openssl/err.h>
  62. #include <openssl/evp.h>
  63. #include <openssl/x509.h>
  64. #include <openssl/rand.h>
  65. #ifndef OPENSSL_NO_SCRYPT
  66. /* PKCS#5 scrypt password based encryption structures */
  67. typedef struct {
  68. ASN1_OCTET_STRING *salt;
  69. ASN1_INTEGER *costParameter;
  70. ASN1_INTEGER *blockSize;
  71. ASN1_INTEGER *parallelizationParameter;
  72. ASN1_INTEGER *keyLength;
  73. } SCRYPT_PARAMS;
  74. ASN1_SEQUENCE(SCRYPT_PARAMS) = {
  75. ASN1_SIMPLE(SCRYPT_PARAMS, salt, ASN1_OCTET_STRING),
  76. ASN1_SIMPLE(SCRYPT_PARAMS, costParameter, ASN1_INTEGER),
  77. ASN1_SIMPLE(SCRYPT_PARAMS, blockSize, ASN1_INTEGER),
  78. ASN1_SIMPLE(SCRYPT_PARAMS, parallelizationParameter, ASN1_INTEGER),
  79. ASN1_OPT(SCRYPT_PARAMS, keyLength, ASN1_INTEGER),
  80. } static_ASN1_SEQUENCE_END(SCRYPT_PARAMS)
  81. DECLARE_ASN1_ALLOC_FUNCTIONS(SCRYPT_PARAMS)
  82. IMPLEMENT_ASN1_ALLOC_FUNCTIONS(SCRYPT_PARAMS)
  83. static X509_ALGOR *pkcs5_scrypt_set(const unsigned char *salt, size_t saltlen,
  84. size_t keylen, uint64_t N, uint64_t r,
  85. uint64_t p);
  86. /*
  87. * Return an algorithm identifier for a PKCS#5 v2.0 PBE algorithm using scrypt
  88. */
  89. X509_ALGOR *PKCS5_pbe2_set_scrypt(const EVP_CIPHER *cipher,
  90. const unsigned char *salt, int saltlen,
  91. unsigned char *aiv, uint64_t N, uint64_t r,
  92. uint64_t p)
  93. {
  94. X509_ALGOR *scheme = NULL, *kalg = NULL, *ret = NULL;
  95. int alg_nid;
  96. size_t keylen = 0;
  97. EVP_CIPHER_CTX *ctx = NULL;
  98. unsigned char iv[EVP_MAX_IV_LENGTH];
  99. PBE2PARAM *pbe2 = NULL;
  100. ASN1_OBJECT *obj;
  101. if (!cipher) {
  102. ASN1err(ASN1_F_PKCS5_PBE2_SET_SCRYPT, ERR_R_PASSED_NULL_PARAMETER);
  103. goto err;
  104. }
  105. if (EVP_PBE_scrypt(NULL, 0, NULL, 0, N, r, p, 0, NULL, 0) == 0) {
  106. ASN1err(ASN1_F_PKCS5_PBE2_SET_SCRYPT,
  107. ASN1_R_INVALID_SCRYPT_PARAMETERS);
  108. goto err;
  109. }
  110. alg_nid = EVP_CIPHER_type(cipher);
  111. if (alg_nid == NID_undef) {
  112. ASN1err(ASN1_F_PKCS5_PBE2_SET_SCRYPT,
  113. ASN1_R_CIPHER_HAS_NO_OBJECT_IDENTIFIER);
  114. goto err;
  115. }
  116. obj = OBJ_nid2obj(alg_nid);
  117. pbe2 = PBE2PARAM_new();
  118. if (pbe2 == NULL)
  119. goto merr;
  120. /* Setup the AlgorithmIdentifier for the encryption scheme */
  121. scheme = pbe2->encryption;
  122. scheme->algorithm = obj;
  123. scheme->parameter = ASN1_TYPE_new();
  124. if (scheme->parameter == NULL)
  125. goto merr;
  126. /* Create random IV */
  127. if (EVP_CIPHER_iv_length(cipher)) {
  128. if (aiv)
  129. memcpy(iv, aiv, EVP_CIPHER_iv_length(cipher));
  130. else if (RAND_bytes(iv, EVP_CIPHER_iv_length(cipher)) < 0)
  131. goto err;
  132. }
  133. ctx = EVP_CIPHER_CTX_new();
  134. if (ctx == NULL)
  135. goto merr;
  136. /* Dummy cipherinit to just setup the IV */
  137. if (EVP_CipherInit_ex(ctx, cipher, NULL, NULL, iv, 0) == 0)
  138. goto err;
  139. if (EVP_CIPHER_param_to_asn1(ctx, scheme->parameter) < 0) {
  140. ASN1err(ASN1_F_PKCS5_PBE2_SET_SCRYPT,
  141. ASN1_R_ERROR_SETTING_CIPHER_PARAMS);
  142. goto err;
  143. }
  144. EVP_CIPHER_CTX_free(ctx);
  145. ctx = NULL;
  146. /* If its RC2 then we'd better setup the key length */
  147. if (alg_nid == NID_rc2_cbc)
  148. keylen = EVP_CIPHER_key_length(cipher);
  149. /* Setup keyfunc */
  150. X509_ALGOR_free(pbe2->keyfunc);
  151. pbe2->keyfunc = pkcs5_scrypt_set(salt, saltlen, keylen, N, r, p);
  152. if (pbe2->keyfunc == NULL)
  153. goto merr;
  154. /* Now set up top level AlgorithmIdentifier */
  155. ret = X509_ALGOR_new();
  156. if (ret == NULL)
  157. goto merr;
  158. ret->algorithm = OBJ_nid2obj(NID_pbes2);
  159. /* Encode PBE2PARAM into parameter */
  160. if (ASN1_TYPE_pack_sequence(ASN1_ITEM_rptr(PBE2PARAM), pbe2,
  161. &ret->parameter) == NULL)
  162. goto merr;
  163. PBE2PARAM_free(pbe2);
  164. pbe2 = NULL;
  165. return ret;
  166. merr:
  167. ASN1err(ASN1_F_PKCS5_PBE2_SET_SCRYPT, ERR_R_MALLOC_FAILURE);
  168. err:
  169. PBE2PARAM_free(pbe2);
  170. X509_ALGOR_free(kalg);
  171. X509_ALGOR_free(ret);
  172. EVP_CIPHER_CTX_free(ctx);
  173. return NULL;
  174. }
  175. static X509_ALGOR *pkcs5_scrypt_set(const unsigned char *salt, size_t saltlen,
  176. size_t keylen, uint64_t N, uint64_t r,
  177. uint64_t p)
  178. {
  179. X509_ALGOR *keyfunc = NULL;
  180. SCRYPT_PARAMS *sparam = NULL;
  181. sparam = SCRYPT_PARAMS_new();
  182. if (sparam == NULL)
  183. goto merr;
  184. if (!saltlen)
  185. saltlen = PKCS5_SALT_LEN;
  186. /* This will either copy salt or grow the buffer */
  187. if (ASN1_STRING_set(sparam->salt, salt, saltlen) == 0)
  188. goto merr;
  189. if (salt == NULL && RAND_bytes(sparam->salt->data, saltlen) <= 0)
  190. goto err;
  191. if (ASN1_INTEGER_set_uint64(sparam->costParameter, N) == 0)
  192. goto merr;
  193. if (ASN1_INTEGER_set_uint64(sparam->blockSize, r) == 0)
  194. goto merr;
  195. if (ASN1_INTEGER_set_uint64(sparam->parallelizationParameter, p) == 0)
  196. goto merr;
  197. /* If have a key len set it up */
  198. if (keylen > 0) {
  199. sparam->keyLength = ASN1_INTEGER_new();
  200. if (sparam->keyLength == NULL)
  201. goto merr;
  202. if (ASN1_INTEGER_set_int64(sparam->keyLength, keylen) == 0)
  203. goto merr;
  204. }
  205. /* Finally setup the keyfunc structure */
  206. keyfunc = X509_ALGOR_new();
  207. if (keyfunc == NULL)
  208. goto merr;
  209. keyfunc->algorithm = OBJ_nid2obj(NID_id_scrypt);
  210. /* Encode SCRYPT_PARAMS into parameter of pbe2 */
  211. if (ASN1_TYPE_pack_sequence(ASN1_ITEM_rptr(SCRYPT_PARAMS), sparam,
  212. &keyfunc->parameter) == NULL)
  213. goto merr;
  214. SCRYPT_PARAMS_free(sparam);
  215. return keyfunc;
  216. merr:
  217. ASN1err(ASN1_F_PKCS5_SCRYPT_SET, ERR_R_MALLOC_FAILURE);
  218. err:
  219. SCRYPT_PARAMS_free(sparam);
  220. X509_ALGOR_free(keyfunc);
  221. return NULL;
  222. }
  223. int PKCS5_v2_scrypt_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass,
  224. int passlen, ASN1_TYPE *param,
  225. const EVP_CIPHER *c, const EVP_MD *md, int en_de)
  226. {
  227. unsigned char *salt, key[EVP_MAX_KEY_LENGTH];
  228. uint64_t p, r, N;
  229. size_t saltlen;
  230. size_t keylen = 0;
  231. int rv = 0;
  232. SCRYPT_PARAMS *sparam = NULL;
  233. if (EVP_CIPHER_CTX_cipher(ctx) == NULL) {
  234. EVPerr(EVP_F_PKCS5_V2_SCRYPT_KEYIVGEN, EVP_R_NO_CIPHER_SET);
  235. goto err;
  236. }
  237. /* Decode parameter */
  238. sparam = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(SCRYPT_PARAMS), param);
  239. if (sparam == NULL) {
  240. EVPerr(EVP_F_PKCS5_V2_SCRYPT_KEYIVGEN, EVP_R_DECODE_ERROR);
  241. goto err;
  242. }
  243. keylen = EVP_CIPHER_CTX_key_length(ctx);
  244. /* Now check the parameters of sparam */
  245. if (sparam->keyLength) {
  246. uint64_t spkeylen;
  247. if ((ASN1_INTEGER_get_uint64(&spkeylen, sparam->keyLength) == 0)
  248. || (spkeylen != keylen)) {
  249. EVPerr(EVP_F_PKCS5_V2_SCRYPT_KEYIVGEN,
  250. EVP_R_UNSUPPORTED_KEYLENGTH);
  251. goto err;
  252. }
  253. }
  254. /* Check all parameters fit in uint64_t and are acceptable to scrypt */
  255. if (ASN1_INTEGER_get_uint64(&N, sparam->costParameter) == 0
  256. || ASN1_INTEGER_get_uint64(&r, sparam->blockSize) == 0
  257. || ASN1_INTEGER_get_uint64(&p, sparam->parallelizationParameter) == 0
  258. || EVP_PBE_scrypt(NULL, 0, NULL, 0, N, r, p, 0, NULL, 0) == 0) {
  259. EVPerr(EVP_F_PKCS5_V2_SCRYPT_KEYIVGEN,
  260. EVP_R_ILLEGAL_SCRYPT_PARAMETERS);
  261. goto err;
  262. }
  263. /* it seems that its all OK */
  264. salt = sparam->salt->data;
  265. saltlen = sparam->salt->length;
  266. if (EVP_PBE_scrypt(pass, passlen, salt, saltlen, N, r, p, 0, key, keylen)
  267. == 0)
  268. goto err;
  269. rv = EVP_CipherInit_ex(ctx, NULL, NULL, key, NULL, en_de);
  270. err:
  271. if (keylen)
  272. OPENSSL_cleanse(key, keylen);
  273. SCRYPT_PARAMS_free(sparam);
  274. return rv;
  275. }
  276. #endif /* OPENSSL_NO_SCRYPT */