2
0

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. /* should be init to zeros. */
  16. static char prompt_string[80];
  17. void EVP_set_pw_prompt(const char *prompt)
  18. {
  19. if (prompt == NULL)
  20. prompt_string[0] = '\0';
  21. else {
  22. strncpy(prompt_string, prompt, 79);
  23. prompt_string[79] = '\0';
  24. }
  25. }
  26. char *EVP_get_pw_prompt(void)
  27. {
  28. if (prompt_string[0] == '\0')
  29. return NULL;
  30. else
  31. return prompt_string;
  32. }
  33. /*
  34. * For historical reasons, the standard function for reading passwords is in
  35. * the DES library -- if someone ever wants to disable DES, this function
  36. * will fail
  37. */
  38. int EVP_read_pw_string(char *buf, int len, const char *prompt, int verify)
  39. {
  40. return EVP_read_pw_string_min(buf, 0, len, prompt, verify);
  41. }
  42. int EVP_read_pw_string_min(char *buf, int min, int len, const char *prompt,
  43. int verify)
  44. {
  45. int ret = -1;
  46. char buff[BUFSIZ];
  47. UI *ui;
  48. if ((prompt == NULL) && (prompt_string[0] != '\0'))
  49. prompt = prompt_string;
  50. ui = UI_new();
  51. if (ui == NULL)
  52. return ret;
  53. if (UI_add_input_string(ui, prompt, 0, buf, min,
  54. (len >= BUFSIZ) ? BUFSIZ - 1 : len) < 0
  55. || (verify
  56. && UI_add_verify_string(ui, prompt, 0, buff, min,
  57. (len >= BUFSIZ) ? BUFSIZ - 1 : len,
  58. buf) < 0))
  59. goto end;
  60. ret = UI_process(ui);
  61. OPENSSL_cleanse(buff, BUFSIZ);
  62. end:
  63. UI_free(ui);
  64. return ret;
  65. }
  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. }