2
0

evp_key.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * Copyright 1995-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/x509.h>
  12. #include <openssl/objects.h>
  13. #include <openssl/evp.h>
  14. #include <openssl/ui.h>
  15. #ifndef BUFSIZ
  16. # define BUFSIZ 256
  17. #endif
  18. /* should be init to zeros. */
  19. static char prompt_string[80];
  20. void EVP_set_pw_prompt(const char *prompt)
  21. {
  22. if (prompt == NULL)
  23. prompt_string[0] = '\0';
  24. else {
  25. strncpy(prompt_string, prompt, 79);
  26. prompt_string[79] = '\0';
  27. }
  28. }
  29. char *EVP_get_pw_prompt(void)
  30. {
  31. if (prompt_string[0] == '\0')
  32. return NULL;
  33. else
  34. return prompt_string;
  35. }
  36. /*
  37. * For historical reasons, the standard function for reading passwords is in
  38. * the DES library -- if someone ever wants to disable DES, this function
  39. * will fail
  40. */
  41. int EVP_read_pw_string(char *buf, int len, const char *prompt, int verify)
  42. {
  43. return EVP_read_pw_string_min(buf, 0, len, prompt, verify);
  44. }
  45. int EVP_read_pw_string_min(char *buf, int min, int len, const char *prompt,
  46. int verify)
  47. {
  48. int ret = -1;
  49. char buff[BUFSIZ];
  50. UI *ui;
  51. if ((prompt == NULL) && (prompt_string[0] != '\0'))
  52. prompt = prompt_string;
  53. ui = UI_new();
  54. if (ui == NULL)
  55. return ret;
  56. if (UI_add_input_string(ui, prompt, 0, buf, min,
  57. (len >= BUFSIZ) ? BUFSIZ - 1 : len) < 0
  58. || (verify
  59. && UI_add_verify_string(ui, prompt, 0, buff, min,
  60. (len >= BUFSIZ) ? BUFSIZ - 1 : len,
  61. buf) < 0))
  62. goto end;
  63. ret = UI_process(ui);
  64. OPENSSL_cleanse(buff, BUFSIZ);
  65. end:
  66. UI_free(ui);
  67. return ret;
  68. }
  69. int EVP_BytesToKey(const EVP_CIPHER *type, const EVP_MD *md,
  70. const unsigned char *salt, const unsigned char *data,
  71. int datal, int count, unsigned char *key,
  72. unsigned char *iv)
  73. {
  74. EVP_MD_CTX *c;
  75. unsigned char md_buf[EVP_MAX_MD_SIZE];
  76. int niv, nkey, addmd = 0;
  77. unsigned int mds = 0, i;
  78. int rv = 0;
  79. nkey = EVP_CIPHER_get_key_length(type);
  80. niv = EVP_CIPHER_get_iv_length(type);
  81. OPENSSL_assert(nkey <= EVP_MAX_KEY_LENGTH);
  82. OPENSSL_assert(niv >= 0 && niv <= EVP_MAX_IV_LENGTH);
  83. if (data == NULL)
  84. return nkey;
  85. c = EVP_MD_CTX_new();
  86. if (c == NULL)
  87. goto err;
  88. for (;;) {
  89. if (!EVP_DigestInit_ex(c, md, NULL))
  90. goto err;
  91. if (addmd++)
  92. if (!EVP_DigestUpdate(c, &(md_buf[0]), mds))
  93. goto err;
  94. if (!EVP_DigestUpdate(c, data, datal))
  95. goto err;
  96. if (salt != NULL)
  97. if (!EVP_DigestUpdate(c, salt, PKCS5_SALT_LEN))
  98. goto err;
  99. if (!EVP_DigestFinal_ex(c, &(md_buf[0]), &mds))
  100. goto err;
  101. for (i = 1; i < (unsigned int)count; i++) {
  102. if (!EVP_DigestInit_ex(c, md, NULL))
  103. goto err;
  104. if (!EVP_DigestUpdate(c, &(md_buf[0]), mds))
  105. goto err;
  106. if (!EVP_DigestFinal_ex(c, &(md_buf[0]), &mds))
  107. goto err;
  108. }
  109. i = 0;
  110. if (nkey) {
  111. for (;;) {
  112. if (nkey == 0)
  113. break;
  114. if (i == mds)
  115. break;
  116. if (key != NULL)
  117. *(key++) = md_buf[i];
  118. nkey--;
  119. i++;
  120. }
  121. }
  122. if (niv && (i != mds)) {
  123. for (;;) {
  124. if (niv == 0)
  125. break;
  126. if (i == mds)
  127. break;
  128. if (iv != NULL)
  129. *(iv++) = md_buf[i];
  130. niv--;
  131. i++;
  132. }
  133. }
  134. if ((nkey == 0) && (niv == 0))
  135. break;
  136. }
  137. rv = EVP_CIPHER_get_key_length(type);
  138. err:
  139. EVP_MD_CTX_free(c);
  140. OPENSSL_cleanse(md_buf, sizeof(md_buf));
  141. return rv;
  142. }