]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/evp/evp_key.c
Following the license change, modify the boilerplates in crypto/evp/
[thirdparty/openssl.git] / crypto / evp / evp_key.c
CommitLineData
62867571
RS
1/*
2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
0f113f3e 3 *
4a8b0c55 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
62867571
RS
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
d02b48c6
RE
8 */
9
10#include <stdio.h>
b39fc560 11#include "internal/cryptlib.h"
ec577822
BM
12#include <openssl/x509.h>
13#include <openssl/objects.h>
14#include <openssl/evp.h>
a63d5eaa 15#include <openssl/ui.h>
d02b48c6
RE
16
17/* should be init to zeros. */
18static char prompt_string[80];
19
875a644a 20void EVP_set_pw_prompt(const char *prompt)
0f113f3e
MC
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}
d02b48c6 29
6b691a5c 30char *EVP_get_pw_prompt(void)
0f113f3e
MC
31{
32 if (prompt_string[0] == '\0')
26a7d938 33 return NULL;
0f113f3e 34 else
26a7d938 35 return prompt_string;
0f113f3e 36}
d02b48c6 37
0f113f3e
MC
38/*
39 * For historical reasons, the standard function for reading passwords is in
40 * the DES library -- if someone ever wants to disable DES, this function
41 * will fail
42 */
6b691a5c 43int EVP_read_pw_string(char *buf, int len, const char *prompt, int verify)
0f113f3e
MC
44{
45 return EVP_read_pw_string_min(buf, 0, len, prompt, verify);
46}
a63d5eaa 47
0f113f3e
MC
48int EVP_read_pw_string_min(char *buf, int min, int len, const char *prompt,
49 int verify)
50{
b96dba9e 51 int ret = -1;
0f113f3e
MC
52 char buff[BUFSIZ];
53 UI *ui;
d02b48c6 54
0f113f3e
MC
55 if ((prompt == NULL) && (prompt_string[0] != '\0'))
56 prompt = prompt_string;
57 ui = UI_new();
90945fa3 58 if (ui == NULL)
b96dba9e
RL
59 return ret;
60 if (UI_add_input_string(ui, prompt, 0, buf, min,
61 (len >= BUFSIZ) ? BUFSIZ - 1 : len) < 0
62 || (verify
63 && UI_add_verify_string(ui, prompt, 0, buff, min,
64 (len >= BUFSIZ) ? BUFSIZ - 1 : len,
65 buf) < 0))
66 goto end;
0f113f3e 67 ret = UI_process(ui);
0f113f3e 68 OPENSSL_cleanse(buff, BUFSIZ);
b96dba9e
RL
69 end:
70 UI_free(ui);
0f113f3e
MC
71 return ret;
72}
d02b48c6 73
0f113f3e
MC
74int EVP_BytesToKey(const EVP_CIPHER *type, const EVP_MD *md,
75 const unsigned char *salt, const unsigned char *data,
76 int datal, int count, unsigned char *key,
77 unsigned char *iv)
78{
77a01145 79 EVP_MD_CTX *c;
0f113f3e
MC
80 unsigned char md_buf[EVP_MAX_MD_SIZE];
81 int niv, nkey, addmd = 0;
82 unsigned int mds = 0, i;
83 int rv = 0;
135727ab
RL
84 nkey = EVP_CIPHER_key_length(type);
85 niv = EVP_CIPHER_iv_length(type);
0f113f3e
MC
86 OPENSSL_assert(nkey <= EVP_MAX_KEY_LENGTH);
87 OPENSSL_assert(niv <= EVP_MAX_IV_LENGTH);
d02b48c6 88
0f113f3e 89 if (data == NULL)
26a7d938 90 return nkey;
d02b48c6 91
bfb0641f 92 c = EVP_MD_CTX_new();
77a01145
RL
93 if (c == NULL)
94 goto err;
0f113f3e 95 for (;;) {
77a01145 96 if (!EVP_DigestInit_ex(c, md, NULL))
3f6c7691 97 goto err;
0f113f3e 98 if (addmd++)
77a01145 99 if (!EVP_DigestUpdate(c, &(md_buf[0]), mds))
0f113f3e 100 goto err;
77a01145 101 if (!EVP_DigestUpdate(c, data, datal))
0f113f3e
MC
102 goto err;
103 if (salt != NULL)
77a01145 104 if (!EVP_DigestUpdate(c, salt, PKCS5_SALT_LEN))
0f113f3e 105 goto err;
77a01145 106 if (!EVP_DigestFinal_ex(c, &(md_buf[0]), &mds))
0f113f3e 107 goto err;
d02b48c6 108
0f113f3e 109 for (i = 1; i < (unsigned int)count; i++) {
77a01145 110 if (!EVP_DigestInit_ex(c, md, NULL))
0f113f3e 111 goto err;
77a01145 112 if (!EVP_DigestUpdate(c, &(md_buf[0]), mds))
0f113f3e 113 goto err;
77a01145 114 if (!EVP_DigestFinal_ex(c, &(md_buf[0]), &mds))
0f113f3e
MC
115 goto err;
116 }
117 i = 0;
118 if (nkey) {
119 for (;;) {
120 if (nkey == 0)
121 break;
122 if (i == mds)
123 break;
124 if (key != NULL)
125 *(key++) = md_buf[i];
126 nkey--;
127 i++;
128 }
129 }
130 if (niv && (i != mds)) {
131 for (;;) {
132 if (niv == 0)
133 break;
134 if (i == mds)
135 break;
136 if (iv != NULL)
137 *(iv++) = md_buf[i];
138 niv--;
139 i++;
140 }
141 }
142 if ((nkey == 0) && (niv == 0))
143 break;
144 }
135727ab 145 rv = EVP_CIPHER_key_length(type);
0f113f3e 146 err:
bfb0641f 147 EVP_MD_CTX_free(c);
3f6c7691 148 OPENSSL_cleanse(md_buf, sizeof(md_buf));
0f113f3e
MC
149 return rv;
150}