p5_scrypt.c 7.7 KB

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