p5_scrypt.c 8.2 KB

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