p5_crpt2.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * Copyright 1999-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 <stdlib.h>
  11. #include "internal/cryptlib.h"
  12. #include <openssl/x509.h>
  13. #include <openssl/evp.h>
  14. #include <openssl/kdf.h>
  15. #include <openssl/hmac.h>
  16. #include <openssl/trace.h>
  17. #include <openssl/core_names.h>
  18. #include "crypto/evp.h"
  19. #include "evp_local.h"
  20. int ossl_pkcs5_pbkdf2_hmac_ex(const char *pass, int passlen,
  21. const unsigned char *salt, int saltlen, int iter,
  22. const EVP_MD *digest, int keylen,
  23. unsigned char *out,
  24. OSSL_LIB_CTX *libctx, const char *propq)
  25. {
  26. const char *empty = "";
  27. int rv = 1, mode = 1;
  28. EVP_KDF *kdf;
  29. EVP_KDF_CTX *kctx;
  30. const char *mdname = EVP_MD_name(digest);
  31. OSSL_PARAM params[6], *p = params;
  32. /* Keep documented behaviour. */
  33. if (pass == NULL) {
  34. pass = empty;
  35. passlen = 0;
  36. } else if (passlen == -1) {
  37. passlen = strlen(pass);
  38. }
  39. if (salt == NULL && saltlen == 0)
  40. salt = (unsigned char *)empty;
  41. kdf = EVP_KDF_fetch(libctx, OSSL_KDF_NAME_PBKDF2, propq);
  42. kctx = EVP_KDF_CTX_new(kdf);
  43. EVP_KDF_free(kdf);
  44. if (kctx == NULL)
  45. return 0;
  46. *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_PASSWORD,
  47. (char *)pass, (size_t)passlen);
  48. *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_PKCS5, &mode);
  49. *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT,
  50. (unsigned char *)salt, saltlen);
  51. *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_ITER, &iter);
  52. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
  53. (char *)mdname, 0);
  54. *p = OSSL_PARAM_construct_end();
  55. if (EVP_KDF_derive(kctx, out, keylen, params) != 1)
  56. rv = 0;
  57. EVP_KDF_CTX_free(kctx);
  58. OSSL_TRACE_BEGIN(PKCS5V2) {
  59. BIO_printf(trc_out, "Password:\n");
  60. BIO_hex_string(trc_out,
  61. 0, passlen, pass, passlen);
  62. BIO_printf(trc_out, "\n");
  63. BIO_printf(trc_out, "Salt:\n");
  64. BIO_hex_string(trc_out,
  65. 0, saltlen, salt, saltlen);
  66. BIO_printf(trc_out, "\n");
  67. BIO_printf(trc_out, "Iteration count %d\n", iter);
  68. BIO_printf(trc_out, "Key:\n");
  69. BIO_hex_string(trc_out,
  70. 0, keylen, out, keylen);
  71. BIO_printf(trc_out, "\n");
  72. } OSSL_TRACE_END(PKCS5V2);
  73. return rv;
  74. }
  75. int PKCS5_PBKDF2_HMAC(const char *pass, int passlen, const unsigned char *salt,
  76. int saltlen, int iter, const EVP_MD *digest, int keylen,
  77. unsigned char *out)
  78. {
  79. return ossl_pkcs5_pbkdf2_hmac_ex(pass, passlen, salt, saltlen, iter, digest,
  80. keylen, out, NULL, NULL);
  81. }
  82. int PKCS5_PBKDF2_HMAC_SHA1(const char *pass, int passlen,
  83. const unsigned char *salt, int saltlen, int iter,
  84. int keylen, unsigned char *out)
  85. {
  86. return PKCS5_PBKDF2_HMAC(pass, passlen, salt, saltlen, iter, EVP_sha1(),
  87. keylen, out);
  88. }
  89. /*
  90. * Now the key derivation function itself. This is a bit evil because it has
  91. * to check the ASN1 parameters are valid: and there are quite a few of
  92. * them...
  93. */
  94. int PKCS5_v2_PBE_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass, int passlen,
  95. ASN1_TYPE *param, const EVP_CIPHER *c,
  96. const EVP_MD *md, int en_de)
  97. {
  98. PBE2PARAM *pbe2 = NULL;
  99. const EVP_CIPHER *cipher;
  100. EVP_PBE_KEYGEN *kdf;
  101. int rv = 0;
  102. pbe2 = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(PBE2PARAM), param);
  103. if (pbe2 == NULL) {
  104. ERR_raise(ERR_LIB_EVP, EVP_R_DECODE_ERROR);
  105. goto err;
  106. }
  107. /* See if we recognise the key derivation function */
  108. if (!EVP_PBE_find(EVP_PBE_TYPE_KDF, OBJ_obj2nid(pbe2->keyfunc->algorithm),
  109. NULL, NULL, &kdf)) {
  110. ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_KEY_DERIVATION_FUNCTION);
  111. goto err;
  112. }
  113. /*
  114. * lets see if we recognise the encryption algorithm.
  115. */
  116. cipher = EVP_get_cipherbyobj(pbe2->encryption->algorithm);
  117. if (!cipher) {
  118. ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_CIPHER);
  119. goto err;
  120. }
  121. /* Fixup cipher based on AlgorithmIdentifier */
  122. if (!EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, en_de))
  123. goto err;
  124. if (EVP_CIPHER_asn1_to_param(ctx, pbe2->encryption->parameter) < 0) {
  125. ERR_raise(ERR_LIB_EVP, EVP_R_CIPHER_PARAMETER_ERROR);
  126. goto err;
  127. }
  128. rv = kdf(ctx, pass, passlen, pbe2->keyfunc->parameter, NULL, NULL, en_de);
  129. err:
  130. PBE2PARAM_free(pbe2);
  131. return rv;
  132. }
  133. int PKCS5_v2_PBKDF2_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass,
  134. int passlen, ASN1_TYPE *param,
  135. const EVP_CIPHER *c, const EVP_MD *md, int en_de)
  136. {
  137. unsigned char *salt, key[EVP_MAX_KEY_LENGTH];
  138. int saltlen, iter, t;
  139. int rv = 0;
  140. unsigned int keylen = 0;
  141. int prf_nid, hmac_md_nid;
  142. PBKDF2PARAM *kdf = NULL;
  143. const EVP_MD *prfmd;
  144. if (EVP_CIPHER_CTX_cipher(ctx) == NULL) {
  145. ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
  146. goto err;
  147. }
  148. keylen = EVP_CIPHER_CTX_key_length(ctx);
  149. OPENSSL_assert(keylen <= sizeof(key));
  150. /* Decode parameter */
  151. kdf = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(PBKDF2PARAM), param);
  152. if (kdf == NULL) {
  153. ERR_raise(ERR_LIB_EVP, EVP_R_DECODE_ERROR);
  154. goto err;
  155. }
  156. t = EVP_CIPHER_CTX_key_length(ctx);
  157. if (t < 0) {
  158. ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY_LENGTH);
  159. goto err;
  160. }
  161. keylen = t;
  162. /* Now check the parameters of the kdf */
  163. if (kdf->keylength && (ASN1_INTEGER_get(kdf->keylength) != (int)keylen)) {
  164. ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_KEYLENGTH);
  165. goto err;
  166. }
  167. if (kdf->prf)
  168. prf_nid = OBJ_obj2nid(kdf->prf->algorithm);
  169. else
  170. prf_nid = NID_hmacWithSHA1;
  171. if (!EVP_PBE_find(EVP_PBE_TYPE_PRF, prf_nid, NULL, &hmac_md_nid, 0)) {
  172. ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_PRF);
  173. goto err;
  174. }
  175. prfmd = EVP_get_digestbynid(hmac_md_nid);
  176. if (prfmd == NULL) {
  177. ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_PRF);
  178. goto err;
  179. }
  180. if (kdf->salt->type != V_ASN1_OCTET_STRING) {
  181. ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_SALT_TYPE);
  182. goto err;
  183. }
  184. /* it seems that its all OK */
  185. salt = kdf->salt->value.octet_string->data;
  186. saltlen = kdf->salt->value.octet_string->length;
  187. iter = ASN1_INTEGER_get(kdf->iter);
  188. if (!PKCS5_PBKDF2_HMAC(pass, passlen, salt, saltlen, iter, prfmd,
  189. keylen, key))
  190. goto err;
  191. rv = EVP_CipherInit_ex(ctx, NULL, NULL, key, NULL, en_de);
  192. err:
  193. OPENSSL_cleanse(key, keylen);
  194. PBKDF2PARAM_free(kdf);
  195. return rv;
  196. }