VirtualBox

source: vbox/trunk/src/libs/openssl-1.1.0g/crypto/evp/evp_key.c@ 69890

Last change on this file since 69890 was 69890, checked in by vboxsync, 7 years ago

Added OpenSSL 1.1.0g with unneeded files removed, otherwise unmodified.
bugref:8070: src/libs maintenance

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette