diff options
Diffstat (limited to 'src/lib/libcrypto/evp/evp_key.c')
-rw-r--r-- | src/lib/libcrypto/evp/evp_key.c | 152 |
1 files changed, 79 insertions, 73 deletions
diff --git a/src/lib/libcrypto/evp/evp_key.c b/src/lib/libcrypto/evp/evp_key.c index b3cb0638fa..445456d3a7 100644 --- a/src/lib/libcrypto/evp/evp_key.c +++ b/src/lib/libcrypto/evp/evp_key.c | |||
@@ -5,21 +5,21 @@ | |||
5 | * This package is an SSL implementation written | 5 | * This package is an SSL implementation written |
6 | * by Eric Young (eay@cryptsoft.com). | 6 | * by Eric Young (eay@cryptsoft.com). |
7 | * The implementation was written so as to conform with Netscapes SSL. | 7 | * The implementation was written so as to conform with Netscapes SSL. |
8 | * | 8 | * |
9 | * This library is free for commercial and non-commercial use as long as | 9 | * This library is free for commercial and non-commercial use as long as |
10 | * the following conditions are aheared to. The following conditions | 10 | * the following conditions are aheared to. The following conditions |
11 | * apply to all code found in this distribution, be it the RC4, RSA, | 11 | * apply to all code found in this distribution, be it the RC4, RSA, |
12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | 12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
13 | * included with this distribution is covered by the same copyright terms | 13 | * included with this distribution is covered by the same copyright terms |
14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | 14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
15 | * | 15 | * |
16 | * Copyright remains Eric Young's, and as such any Copyright notices in | 16 | * Copyright remains Eric Young's, and as such any Copyright notices in |
17 | * the code are not to be removed. | 17 | * the code are not to be removed. |
18 | * If this package is used in a product, Eric Young should be given attribution | 18 | * If this package is used in a product, Eric Young should be given attribution |
19 | * as the author of the parts of the library used. | 19 | * as the author of the parts of the library used. |
20 | * This can be in the form of a textual message at program startup or | 20 | * This can be in the form of a textual message at program startup or |
21 | * in documentation (online or textual) provided with the package. | 21 | * in documentation (online or textual) provided with the package. |
22 | * | 22 | * |
23 | * Redistribution and use in source and binary forms, with or without | 23 | * Redistribution and use in source and binary forms, with or without |
24 | * modification, are permitted provided that the following conditions | 24 | * modification, are permitted provided that the following conditions |
25 | * are met: | 25 | * are met: |
@@ -34,10 +34,10 @@ | |||
34 | * Eric Young (eay@cryptsoft.com)" | 34 | * Eric Young (eay@cryptsoft.com)" |
35 | * The word 'cryptographic' can be left out if the rouines from the library | 35 | * The word 'cryptographic' can be left out if the rouines from the library |
36 | * being used are not cryptographic related :-). | 36 | * being used are not cryptographic related :-). |
37 | * 4. If you include any Windows specific code (or a derivative thereof) from | 37 | * 4. If you include any Windows specific code (or a derivative thereof) from |
38 | * the apps directory (application code) you must include an acknowledgement: | 38 | * the apps directory (application code) you must include an acknowledgement: |
39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | 39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
40 | * | 40 | * |
41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | 41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | 43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
@@ -49,7 +49,7 @@ | |||
49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | 49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
51 | * SUCH DAMAGE. | 51 | * SUCH DAMAGE. |
52 | * | 52 | * |
53 | * The licence and distribution terms for any publically available version or | 53 | * The licence and distribution terms for any publically available version or |
54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | 54 | * derivative of this code cannot be changed. i.e. this code cannot simply be |
55 | * copied and put under another distribution licence | 55 | * copied and put under another distribution licence |
@@ -66,123 +66,129 @@ | |||
66 | /* should be init to zeros. */ | 66 | /* should be init to zeros. */ |
67 | static char prompt_string[80]; | 67 | static char prompt_string[80]; |
68 | 68 | ||
69 | void EVP_set_pw_prompt(const char *prompt) | 69 | void |
70 | { | 70 | EVP_set_pw_prompt(const char *prompt) |
71 | { | ||
71 | if (prompt == NULL) | 72 | if (prompt == NULL) |
72 | prompt_string[0]='\0'; | 73 | prompt_string[0] = '\0'; |
73 | else | 74 | else { |
74 | { | 75 | strlcpy(prompt_string, prompt, sizeof(prompt_string)); |
75 | strlcpy(prompt_string,prompt,sizeof(prompt_string)); | ||
76 | } | ||
77 | } | 76 | } |
77 | } | ||
78 | 78 | ||
79 | char *EVP_get_pw_prompt(void) | 79 | char * |
80 | { | 80 | EVP_get_pw_prompt(void) |
81 | { | ||
81 | if (prompt_string[0] == '\0') | 82 | if (prompt_string[0] == '\0') |
82 | return(NULL); | 83 | return (NULL); |
83 | else | 84 | else |
84 | return(prompt_string); | 85 | return (prompt_string); |
85 | } | 86 | } |
86 | 87 | ||
87 | /* For historical reasons, the standard function for reading passwords is | 88 | /* For historical reasons, the standard function for reading passwords is |
88 | * in the DES library -- if someone ever wants to disable DES, | 89 | * in the DES library -- if someone ever wants to disable DES, |
89 | * this function will fail */ | 90 | * this function will fail */ |
90 | int EVP_read_pw_string(char *buf, int len, const char *prompt, int verify) | 91 | int |
91 | { | 92 | EVP_read_pw_string(char *buf, int len, const char *prompt, int verify) |
93 | { | ||
92 | return EVP_read_pw_string_min(buf, 0, len, prompt, verify); | 94 | return EVP_read_pw_string_min(buf, 0, len, prompt, verify); |
93 | } | 95 | } |
94 | 96 | ||
95 | int EVP_read_pw_string_min(char *buf, int min, int len, const char *prompt, int verify) | 97 | int |
96 | { | 98 | EVP_read_pw_string_min(char *buf, int min, int len, const char *prompt, |
99 | int verify) | ||
100 | { | ||
97 | int ret; | 101 | int ret; |
98 | char buff[BUFSIZ]; | 102 | char buff[BUFSIZ]; |
99 | UI *ui; | 103 | UI *ui; |
100 | 104 | ||
101 | if ((prompt == NULL) && (prompt_string[0] != '\0')) | 105 | if ((prompt == NULL) && (prompt_string[0] != '\0')) |
102 | prompt=prompt_string; | 106 | prompt = prompt_string; |
103 | ui = UI_new(); | 107 | ui = UI_new(); |
104 | UI_add_input_string(ui,prompt,0,buf,min,(len>=BUFSIZ)?BUFSIZ-1:len); | 108 | UI_add_input_string(ui, prompt, 0,buf, min, |
109 | (len >= BUFSIZ) ? BUFSIZ - 1 : len); | ||
105 | if (verify) | 110 | if (verify) |
106 | UI_add_verify_string(ui,prompt,0, | 111 | UI_add_verify_string(ui, prompt, 0, buff, min, |
107 | buff,min,(len>=BUFSIZ)?BUFSIZ-1:len,buf); | 112 | (len >= BUFSIZ) ? BUFSIZ - 1 : len, buf); |
108 | ret = UI_process(ui); | 113 | ret = UI_process(ui); |
109 | UI_free(ui); | 114 | UI_free(ui); |
110 | OPENSSL_cleanse(buff,BUFSIZ); | 115 | OPENSSL_cleanse(buff, BUFSIZ); |
111 | return ret; | 116 | return ret; |
112 | } | 117 | } |
113 | 118 | ||
114 | int EVP_BytesToKey(const EVP_CIPHER *type, const EVP_MD *md, | 119 | int |
115 | const unsigned char *salt, const unsigned char *data, int datal, | 120 | EVP_BytesToKey(const EVP_CIPHER *type, const EVP_MD *md, |
116 | int count, unsigned char *key, unsigned char *iv) | 121 | const unsigned char *salt, const unsigned char *data, int datal, |
117 | { | 122 | int count, unsigned char *key, unsigned char *iv) |
123 | { | ||
118 | EVP_MD_CTX c; | 124 | EVP_MD_CTX c; |
119 | unsigned char md_buf[EVP_MAX_MD_SIZE]; | 125 | unsigned char md_buf[EVP_MAX_MD_SIZE]; |
120 | int niv,nkey,addmd=0; | 126 | int niv, nkey, addmd = 0; |
121 | unsigned int mds=0,i; | 127 | unsigned int mds = 0, i; |
122 | int rv = 0; | 128 | int rv = 0; |
123 | nkey=type->key_len; | 129 | nkey = type->key_len; |
124 | niv=type->iv_len; | 130 | niv = type->iv_len; |
125 | OPENSSL_assert(nkey <= EVP_MAX_KEY_LENGTH); | 131 | OPENSSL_assert(nkey <= EVP_MAX_KEY_LENGTH); |
126 | OPENSSL_assert(niv <= EVP_MAX_IV_LENGTH); | 132 | OPENSSL_assert(niv <= EVP_MAX_IV_LENGTH); |
127 | 133 | ||
128 | if (data == NULL) return(nkey); | 134 | if (data == NULL) |
135 | return (nkey); | ||
129 | 136 | ||
130 | EVP_MD_CTX_init(&c); | 137 | EVP_MD_CTX_init(&c); |
131 | for (;;) | 138 | for (;;) { |
132 | { | 139 | if (!EVP_DigestInit_ex(&c, md, NULL)) |
133 | if (!EVP_DigestInit_ex(&c,md, NULL)) | ||
134 | return 0; | 140 | return 0; |
135 | if (addmd++) | 141 | if (addmd++) |
136 | if (!EVP_DigestUpdate(&c,&(md_buf[0]),mds)) | 142 | if (!EVP_DigestUpdate(&c, &(md_buf[0]), mds)) |
137 | goto err; | 143 | goto err; |
138 | if (!EVP_DigestUpdate(&c,data,datal)) | 144 | if (!EVP_DigestUpdate(&c, data, datal)) |
139 | goto err; | 145 | goto err; |
140 | if (salt != NULL) | 146 | if (salt != NULL) |
141 | if (!EVP_DigestUpdate(&c,salt,PKCS5_SALT_LEN)) | 147 | if (!EVP_DigestUpdate(&c, salt, PKCS5_SALT_LEN)) |
142 | goto err; | 148 | goto err; |
143 | if (!EVP_DigestFinal_ex(&c,&(md_buf[0]),&mds)) | 149 | if (!EVP_DigestFinal_ex(&c, &(md_buf[0]), &mds)) |
144 | goto err; | 150 | goto err; |
145 | 151 | ||
146 | for (i=1; i<(unsigned int)count; i++) | 152 | for (i = 1; i < (unsigned int)count; i++) { |
147 | { | 153 | if (!EVP_DigestInit_ex(&c, md, NULL)) |
148 | if (!EVP_DigestInit_ex(&c,md, NULL)) | ||
149 | goto err; | 154 | goto err; |
150 | if (!EVP_DigestUpdate(&c,&(md_buf[0]),mds)) | 155 | if (!EVP_DigestUpdate(&c, &(md_buf[0]), mds)) |
151 | goto err; | 156 | goto err; |
152 | if (!EVP_DigestFinal_ex(&c,&(md_buf[0]),&mds)) | 157 | if (!EVP_DigestFinal_ex(&c, &(md_buf[0]), &mds)) |
153 | goto err; | 158 | goto err; |
154 | } | 159 | } |
155 | i=0; | 160 | i = 0; |
156 | if (nkey) | 161 | if (nkey) { |
157 | { | 162 | for (;;) { |
158 | for (;;) | 163 | if (nkey == 0) |
159 | { | 164 | break; |
160 | if (nkey == 0) break; | 165 | if (i == mds) |
161 | if (i == mds) break; | 166 | break; |
162 | if (key != NULL) | 167 | if (key != NULL) |
163 | *(key++)=md_buf[i]; | 168 | *(key++) = md_buf[i]; |
164 | nkey--; | 169 | nkey--; |
165 | i++; | 170 | i++; |
166 | } | ||
167 | } | 171 | } |
168 | if (niv && (i != mds)) | 172 | } |
169 | { | 173 | if (niv && (i != mds)) { |
170 | for (;;) | 174 | for (;;) { |
171 | { | 175 | if (niv == 0) |
172 | if (niv == 0) break; | 176 | break; |
173 | if (i == mds) break; | 177 | if (i == mds) |
178 | break; | ||
174 | if (iv != NULL) | 179 | if (iv != NULL) |
175 | *(iv++)=md_buf[i]; | 180 | *(iv++) = md_buf[i]; |
176 | niv--; | 181 | niv--; |
177 | i++; | 182 | i++; |
178 | } | ||
179 | } | 183 | } |
180 | if ((nkey == 0) && (niv == 0)) break; | ||
181 | } | 184 | } |
185 | if ((nkey == 0) && (niv == 0)) | ||
186 | break; | ||
187 | } | ||
182 | rv = type->key_len; | 188 | rv = type->key_len; |
183 | err: | 189 | |
190 | err: | ||
184 | EVP_MD_CTX_cleanup(&c); | 191 | EVP_MD_CTX_cleanup(&c); |
185 | OPENSSL_cleanse(&(md_buf[0]),EVP_MAX_MD_SIZE); | 192 | OPENSSL_cleanse(&(md_buf[0]), EVP_MAX_MD_SIZE); |
186 | return rv; | 193 | return rv; |
187 | } | 194 | } |
188 | |||