]>
git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/evp/evp_key.c
2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
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
11 #include "internal/cryptlib.h"
12 #include <openssl/x509.h>
13 #include <openssl/objects.h>
14 #include <openssl/evp.h>
15 #include <openssl/ui.h>
21 /* should be init to zeros. */
22 static char prompt_string
[80];
24 void EVP_set_pw_prompt(const char *prompt
)
27 prompt_string
[0] = '\0';
29 strncpy(prompt_string
, prompt
, 79);
30 prompt_string
[79] = '\0';
34 char *EVP_get_pw_prompt(void)
36 if (prompt_string
[0] == '\0')
43 * For historical reasons, the standard function for reading passwords is in
44 * the DES library -- if someone ever wants to disable DES, this function
47 int EVP_read_pw_string(char *buf
, int len
, const char *prompt
, int verify
)
49 return EVP_read_pw_string_min(buf
, 0, len
, prompt
, verify
);
52 int EVP_read_pw_string_min(char *buf
, int min
, int len
, const char *prompt
,
59 if ((prompt
== NULL
) && (prompt_string
[0] != '\0'))
60 prompt
= prompt_string
;
64 if (UI_add_input_string(ui
, prompt
, 0, buf
, min
,
65 (len
>= BUFSIZ
) ? BUFSIZ
- 1 : len
) < 0
67 && UI_add_verify_string(ui
, prompt
, 0, buff
, min
,
68 (len
>= BUFSIZ
) ? BUFSIZ
- 1 : len
,
72 OPENSSL_cleanse(buff
, BUFSIZ
);
78 int EVP_BytesToKey(const EVP_CIPHER
*type
, const EVP_MD
*md
,
79 const unsigned char *salt
, const unsigned char *data
,
80 int datal
, int count
, unsigned char *key
,
84 unsigned char md_buf
[EVP_MAX_MD_SIZE
];
85 int niv
, nkey
, addmd
= 0;
86 unsigned int mds
= 0, i
;
88 nkey
= EVP_CIPHER_key_length(type
);
89 niv
= EVP_CIPHER_iv_length(type
);
90 OPENSSL_assert(nkey
<= EVP_MAX_KEY_LENGTH
);
91 OPENSSL_assert(niv
<= EVP_MAX_IV_LENGTH
);
100 if (!EVP_DigestInit_ex(c
, md
, NULL
))
103 if (!EVP_DigestUpdate(c
, &(md_buf
[0]), mds
))
105 if (!EVP_DigestUpdate(c
, data
, datal
))
108 if (!EVP_DigestUpdate(c
, salt
, PKCS5_SALT_LEN
))
110 if (!EVP_DigestFinal_ex(c
, &(md_buf
[0]), &mds
))
113 for (i
= 1; i
< (unsigned int)count
; i
++) {
114 if (!EVP_DigestInit_ex(c
, md
, NULL
))
116 if (!EVP_DigestUpdate(c
, &(md_buf
[0]), mds
))
118 if (!EVP_DigestFinal_ex(c
, &(md_buf
[0]), &mds
))
129 *(key
++) = md_buf
[i
];
134 if (niv
&& (i
!= mds
)) {
146 if ((nkey
== 0) && (niv
== 0))
149 rv
= EVP_CIPHER_key_length(type
);
152 OPENSSL_cleanse(md_buf
, sizeof(md_buf
));