p5_crpt.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Copyright 1999-2016 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. /*
  15. * Doesn't do anything now: Builtin PBE algorithms in static table.
  16. */
  17. void PKCS5_PBE_add(void)
  18. {
  19. }
  20. int PKCS5_PBE_keyivgen(EVP_CIPHER_CTX *cctx, const char *pass, int passlen,
  21. ASN1_TYPE *param, const EVP_CIPHER *cipher,
  22. const EVP_MD *md, int en_de)
  23. {
  24. EVP_MD_CTX *ctx;
  25. unsigned char md_tmp[EVP_MAX_MD_SIZE];
  26. unsigned char key[EVP_MAX_KEY_LENGTH], iv[EVP_MAX_IV_LENGTH];
  27. int i;
  28. PBEPARAM *pbe;
  29. int saltlen, iter;
  30. unsigned char *salt;
  31. int mdsize;
  32. int rv = 0;
  33. /* Extract useful info from parameter */
  34. if (param == NULL || param->type != V_ASN1_SEQUENCE ||
  35. param->value.sequence == NULL) {
  36. EVPerr(EVP_F_PKCS5_PBE_KEYIVGEN, EVP_R_DECODE_ERROR);
  37. return 0;
  38. }
  39. pbe = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(PBEPARAM), param);
  40. if (pbe == NULL) {
  41. EVPerr(EVP_F_PKCS5_PBE_KEYIVGEN, EVP_R_DECODE_ERROR);
  42. return 0;
  43. }
  44. if (!pbe->iter)
  45. iter = 1;
  46. else
  47. iter = ASN1_INTEGER_get(pbe->iter);
  48. salt = pbe->salt->data;
  49. saltlen = pbe->salt->length;
  50. if (!pass)
  51. passlen = 0;
  52. else if (passlen == -1)
  53. passlen = strlen(pass);
  54. ctx = EVP_MD_CTX_new();
  55. if (ctx == NULL) {
  56. EVPerr(EVP_F_PKCS5_PBE_KEYIVGEN, ERR_R_MALLOC_FAILURE);
  57. goto err;
  58. }
  59. if (!EVP_DigestInit_ex(ctx, md, NULL))
  60. goto err;
  61. if (!EVP_DigestUpdate(ctx, pass, passlen))
  62. goto err;
  63. if (!EVP_DigestUpdate(ctx, salt, saltlen))
  64. goto err;
  65. PBEPARAM_free(pbe);
  66. if (!EVP_DigestFinal_ex(ctx, md_tmp, NULL))
  67. goto err;
  68. mdsize = EVP_MD_size(md);
  69. if (mdsize < 0)
  70. return 0;
  71. for (i = 1; i < iter; i++) {
  72. if (!EVP_DigestInit_ex(ctx, md, NULL))
  73. goto err;
  74. if (!EVP_DigestUpdate(ctx, md_tmp, mdsize))
  75. goto err;
  76. if (!EVP_DigestFinal_ex(ctx, md_tmp, NULL))
  77. goto err;
  78. }
  79. OPENSSL_assert(EVP_CIPHER_key_length(cipher) <= (int)sizeof(md_tmp));
  80. memcpy(key, md_tmp, EVP_CIPHER_key_length(cipher));
  81. OPENSSL_assert(EVP_CIPHER_iv_length(cipher) <= 16);
  82. memcpy(iv, md_tmp + (16 - EVP_CIPHER_iv_length(cipher)),
  83. EVP_CIPHER_iv_length(cipher));
  84. if (!EVP_CipherInit_ex(cctx, cipher, NULL, key, iv, en_de))
  85. goto err;
  86. OPENSSL_cleanse(md_tmp, EVP_MAX_MD_SIZE);
  87. OPENSSL_cleanse(key, EVP_MAX_KEY_LENGTH);
  88. OPENSSL_cleanse(iv, EVP_MAX_IV_LENGTH);
  89. rv = 1;
  90. err:
  91. EVP_MD_CTX_free(ctx);
  92. return rv;
  93. }