p5_scrypt.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * Copyright 2015-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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. #include <stdio.h>
  10. #include "internal/cryptlib.h"
  11. #include <openssl/asn1t.h>
  12. #include <openssl/err.h>
  13. #include <openssl/evp.h>
  14. #include <openssl/x509.h>
  15. #include <openssl/rand.h>
  16. #ifndef OPENSSL_NO_SCRYPT
  17. /* PKCS#5 scrypt password based encryption structures */
  18. typedef struct {
  19. ASN1_OCTET_STRING *salt;
  20. ASN1_INTEGER *costParameter;
  21. ASN1_INTEGER *blockSize;
  22. ASN1_INTEGER *parallelizationParameter;
  23. ASN1_INTEGER *keyLength;
  24. } SCRYPT_PARAMS;
  25. ASN1_SEQUENCE(SCRYPT_PARAMS) = {
  26. ASN1_SIMPLE(SCRYPT_PARAMS, salt, ASN1_OCTET_STRING),
  27. ASN1_SIMPLE(SCRYPT_PARAMS, costParameter, ASN1_INTEGER),
  28. ASN1_SIMPLE(SCRYPT_PARAMS, blockSize, ASN1_INTEGER),
  29. ASN1_SIMPLE(SCRYPT_PARAMS, parallelizationParameter, ASN1_INTEGER),
  30. ASN1_OPT(SCRYPT_PARAMS, keyLength, ASN1_INTEGER),
  31. } static_ASN1_SEQUENCE_END(SCRYPT_PARAMS)
  32. DECLARE_ASN1_ALLOC_FUNCTIONS(SCRYPT_PARAMS)
  33. IMPLEMENT_ASN1_ALLOC_FUNCTIONS(SCRYPT_PARAMS)
  34. static X509_ALGOR *pkcs5_scrypt_set(const unsigned char *salt, size_t saltlen,
  35. size_t keylen, uint64_t N, uint64_t r,
  36. uint64_t p);
  37. /*
  38. * Return an algorithm identifier for a PKCS#5 v2.0 PBE algorithm using scrypt
  39. */
  40. X509_ALGOR *PKCS5_pbe2_set_scrypt(const EVP_CIPHER *cipher,
  41. const unsigned char *salt, int saltlen,
  42. unsigned char *aiv, uint64_t N, uint64_t r,
  43. uint64_t p)
  44. {
  45. X509_ALGOR *scheme = NULL, *ret = NULL;
  46. int alg_nid;
  47. size_t keylen = 0;
  48. EVP_CIPHER_CTX *ctx = NULL;
  49. unsigned char iv[EVP_MAX_IV_LENGTH];
  50. PBE2PARAM *pbe2 = NULL;
  51. if (!cipher) {
  52. ASN1err(ASN1_F_PKCS5_PBE2_SET_SCRYPT, ERR_R_PASSED_NULL_PARAMETER);
  53. goto err;
  54. }
  55. if (EVP_PBE_scrypt(NULL, 0, NULL, 0, N, r, p, 0, NULL, 0) == 0) {
  56. ASN1err(ASN1_F_PKCS5_PBE2_SET_SCRYPT,
  57. ASN1_R_INVALID_SCRYPT_PARAMETERS);
  58. goto err;
  59. }
  60. alg_nid = EVP_CIPHER_type(cipher);
  61. if (alg_nid == NID_undef) {
  62. ASN1err(ASN1_F_PKCS5_PBE2_SET_SCRYPT,
  63. ASN1_R_CIPHER_HAS_NO_OBJECT_IDENTIFIER);
  64. goto err;
  65. }
  66. pbe2 = PBE2PARAM_new();
  67. if (pbe2 == NULL)
  68. goto merr;
  69. /* Setup the AlgorithmIdentifier for the encryption scheme */
  70. scheme = pbe2->encryption;
  71. scheme->algorithm = OBJ_nid2obj(alg_nid);
  72. scheme->parameter = ASN1_TYPE_new();
  73. if (scheme->parameter == NULL)
  74. goto merr;
  75. /* Create random IV */
  76. if (EVP_CIPHER_iv_length(cipher)) {
  77. if (aiv)
  78. memcpy(iv, aiv, EVP_CIPHER_iv_length(cipher));
  79. else if (RAND_bytes(iv, EVP_CIPHER_iv_length(cipher)) < 0)
  80. goto err;
  81. }
  82. ctx = EVP_CIPHER_CTX_new();
  83. if (ctx == NULL)
  84. goto merr;
  85. /* Dummy cipherinit to just setup the IV */
  86. if (EVP_CipherInit_ex(ctx, cipher, NULL, NULL, iv, 0) == 0)
  87. goto err;
  88. if (EVP_CIPHER_param_to_asn1(ctx, scheme->parameter) < 0) {
  89. ASN1err(ASN1_F_PKCS5_PBE2_SET_SCRYPT,
  90. ASN1_R_ERROR_SETTING_CIPHER_PARAMS);
  91. goto err;
  92. }
  93. EVP_CIPHER_CTX_free(ctx);
  94. ctx = NULL;
  95. /* If its RC2 then we'd better setup the key length */
  96. if (alg_nid == NID_rc2_cbc)
  97. keylen = EVP_CIPHER_key_length(cipher);
  98. /* Setup keyfunc */
  99. X509_ALGOR_free(pbe2->keyfunc);
  100. pbe2->keyfunc = pkcs5_scrypt_set(salt, saltlen, keylen, N, r, p);
  101. if (pbe2->keyfunc == NULL)
  102. goto merr;
  103. /* Now set up top level AlgorithmIdentifier */
  104. ret = X509_ALGOR_new();
  105. if (ret == NULL)
  106. goto merr;
  107. ret->algorithm = OBJ_nid2obj(NID_pbes2);
  108. /* Encode PBE2PARAM into parameter */
  109. if (ASN1_TYPE_pack_sequence(ASN1_ITEM_rptr(PBE2PARAM), pbe2,
  110. &ret->parameter) == NULL)
  111. goto merr;
  112. PBE2PARAM_free(pbe2);
  113. pbe2 = NULL;
  114. return ret;
  115. merr:
  116. ASN1err(ASN1_F_PKCS5_PBE2_SET_SCRYPT, ERR_R_MALLOC_FAILURE);
  117. err:
  118. PBE2PARAM_free(pbe2);
  119. X509_ALGOR_free(ret);
  120. EVP_CIPHER_CTX_free(ctx);
  121. return NULL;
  122. }
  123. static X509_ALGOR *pkcs5_scrypt_set(const unsigned char *salt, size_t saltlen,
  124. size_t keylen, uint64_t N, uint64_t r,
  125. uint64_t p)
  126. {
  127. X509_ALGOR *keyfunc = NULL;
  128. SCRYPT_PARAMS *sparam = SCRYPT_PARAMS_new();
  129. if (sparam == NULL)
  130. goto merr;
  131. if (!saltlen)
  132. saltlen = PKCS5_SALT_LEN;
  133. /* This will either copy salt or grow the buffer */
  134. if (ASN1_STRING_set(sparam->salt, salt, saltlen) == 0)
  135. goto merr;
  136. if (salt == NULL && RAND_bytes(sparam->salt->data, saltlen) <= 0)
  137. goto err;
  138. if (ASN1_INTEGER_set_uint64(sparam->costParameter, N) == 0)
  139. goto merr;
  140. if (ASN1_INTEGER_set_uint64(sparam->blockSize, r) == 0)
  141. goto merr;
  142. if (ASN1_INTEGER_set_uint64(sparam->parallelizationParameter, p) == 0)
  143. goto merr;
  144. /* If have a key len set it up */
  145. if (keylen > 0) {
  146. sparam->keyLength = ASN1_INTEGER_new();
  147. if (sparam->keyLength == NULL)
  148. goto merr;
  149. if (ASN1_INTEGER_set_int64(sparam->keyLength, keylen) == 0)
  150. goto merr;
  151. }
  152. /* Finally setup the keyfunc structure */
  153. keyfunc = X509_ALGOR_new();
  154. if (keyfunc == NULL)
  155. goto merr;
  156. keyfunc->algorithm = OBJ_nid2obj(NID_id_scrypt);
  157. /* Encode SCRYPT_PARAMS into parameter of pbe2 */
  158. if (ASN1_TYPE_pack_sequence(ASN1_ITEM_rptr(SCRYPT_PARAMS), sparam,
  159. &keyfunc->parameter) == NULL)
  160. goto merr;
  161. SCRYPT_PARAMS_free(sparam);
  162. return keyfunc;
  163. merr:
  164. ASN1err(ASN1_F_PKCS5_SCRYPT_SET, ERR_R_MALLOC_FAILURE);
  165. err:
  166. SCRYPT_PARAMS_free(sparam);
  167. X509_ALGOR_free(keyfunc);
  168. return NULL;
  169. }
  170. int PKCS5_v2_scrypt_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass,
  171. int passlen, ASN1_TYPE *param,
  172. const EVP_CIPHER *c, const EVP_MD *md, int en_de)
  173. {
  174. unsigned char *salt, key[EVP_MAX_KEY_LENGTH];
  175. uint64_t p, r, N;
  176. size_t saltlen;
  177. size_t keylen = 0;
  178. int rv = 0;
  179. SCRYPT_PARAMS *sparam = NULL;
  180. if (EVP_CIPHER_CTX_cipher(ctx) == NULL) {
  181. EVPerr(EVP_F_PKCS5_V2_SCRYPT_KEYIVGEN, EVP_R_NO_CIPHER_SET);
  182. goto err;
  183. }
  184. /* Decode parameter */
  185. sparam = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(SCRYPT_PARAMS), param);
  186. if (sparam == NULL) {
  187. EVPerr(EVP_F_PKCS5_V2_SCRYPT_KEYIVGEN, EVP_R_DECODE_ERROR);
  188. goto err;
  189. }
  190. keylen = EVP_CIPHER_CTX_key_length(ctx);
  191. /* Now check the parameters of sparam */
  192. if (sparam->keyLength) {
  193. uint64_t spkeylen;
  194. if ((ASN1_INTEGER_get_uint64(&spkeylen, sparam->keyLength) == 0)
  195. || (spkeylen != keylen)) {
  196. EVPerr(EVP_F_PKCS5_V2_SCRYPT_KEYIVGEN,
  197. EVP_R_UNSUPPORTED_KEYLENGTH);
  198. goto err;
  199. }
  200. }
  201. /* Check all parameters fit in uint64_t and are acceptable to scrypt */
  202. if (ASN1_INTEGER_get_uint64(&N, sparam->costParameter) == 0
  203. || ASN1_INTEGER_get_uint64(&r, sparam->blockSize) == 0
  204. || ASN1_INTEGER_get_uint64(&p, sparam->parallelizationParameter) == 0
  205. || EVP_PBE_scrypt(NULL, 0, NULL, 0, N, r, p, 0, NULL, 0) == 0) {
  206. EVPerr(EVP_F_PKCS5_V2_SCRYPT_KEYIVGEN,
  207. EVP_R_ILLEGAL_SCRYPT_PARAMETERS);
  208. goto err;
  209. }
  210. /* it seems that its all OK */
  211. salt = sparam->salt->data;
  212. saltlen = sparam->salt->length;
  213. if (EVP_PBE_scrypt(pass, passlen, salt, saltlen, N, r, p, 0, key, keylen)
  214. == 0)
  215. goto err;
  216. rv = EVP_CipherInit_ex(ctx, NULL, NULL, key, NULL, en_de);
  217. err:
  218. if (keylen)
  219. OPENSSL_cleanse(key, keylen);
  220. SCRYPT_PARAMS_free(sparam);
  221. return rv;
  222. }
  223. #endif /* OPENSSL_NO_SCRYPT */