evp_key.c 4.1 KB

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