/src/openssl30/engines/e_padlock.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2004-2023 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
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 |
8 | | */ |
9 | | |
10 | | /* |
11 | | * This file uses the low level AES and engine functions (which are deprecated |
12 | | * for non-internal use) in order to implement the padlock engine AES ciphers. |
13 | | */ |
14 | | #define OPENSSL_SUPPRESS_DEPRECATED |
15 | | |
16 | | #include <stdio.h> |
17 | | #include <string.h> |
18 | | |
19 | | #include <openssl/opensslconf.h> |
20 | | #include <openssl/crypto.h> |
21 | | #include <openssl/engine.h> |
22 | | #include <openssl/evp.h> |
23 | | #include <openssl/aes.h> |
24 | | #include <openssl/rand.h> |
25 | | #include <openssl/err.h> |
26 | | #include <openssl/modes.h> |
27 | | |
28 | | #ifndef OPENSSL_NO_PADLOCKENG |
29 | | |
30 | | /* |
31 | | * VIA PadLock AES is available *ONLY* on some x86 CPUs. Not only that it |
32 | | * doesn't exist elsewhere, but it even can't be compiled on other platforms! |
33 | | */ |
34 | | |
35 | | #undef COMPILE_PADLOCKENG |
36 | | #if defined(PADLOCK_ASM) |
37 | | #define COMPILE_PADLOCKENG |
38 | | #ifdef OPENSSL_NO_DYNAMIC_ENGINE |
39 | | static ENGINE *ENGINE_padlock(void); |
40 | | #endif |
41 | | #endif |
42 | | |
43 | | #ifdef OPENSSL_NO_DYNAMIC_ENGINE |
44 | | void engine_load_padlock_int(void); |
45 | | void engine_load_padlock_int(void) |
46 | 0 | { |
47 | | /* On non-x86 CPUs it just returns. */ |
48 | 0 | #ifdef COMPILE_PADLOCKENG |
49 | 0 | ENGINE *toadd = ENGINE_padlock(); |
50 | 0 | if (!toadd) |
51 | 0 | return; |
52 | 0 | ERR_set_mark(); |
53 | 0 | ENGINE_add(toadd); |
54 | | /* |
55 | | * If the "add" worked, it gets a structural reference. So either way, we |
56 | | * release our just-created reference. |
57 | | */ |
58 | 0 | ENGINE_free(toadd); |
59 | | /* |
60 | | * If the "add" didn't work, it was probably a conflict because it was |
61 | | * already added (eg. someone calling ENGINE_load_blah then calling |
62 | | * ENGINE_load_builtin_engines() perhaps). |
63 | | */ |
64 | 0 | ERR_pop_to_mark(); |
65 | 0 | #endif |
66 | 0 | } |
67 | | |
68 | | #endif |
69 | | |
70 | | #ifdef COMPILE_PADLOCKENG |
71 | | |
72 | | /* Function for ENGINE detection and control */ |
73 | | static int padlock_available(void); |
74 | | static int padlock_init(ENGINE *e); |
75 | | |
76 | | /* RNG Stuff */ |
77 | | static RAND_METHOD padlock_rand; |
78 | | |
79 | | /* Cipher Stuff */ |
80 | | static int padlock_ciphers(ENGINE *e, const EVP_CIPHER **cipher, |
81 | | const int **nids, int nid); |
82 | | |
83 | | /* Engine names */ |
84 | | static const char *padlock_id = "padlock"; |
85 | | static char padlock_name[100]; |
86 | | |
87 | | /* Available features */ |
88 | | static int padlock_use_ace = 0; /* Advanced Cryptography Engine */ |
89 | | static int padlock_use_rng = 0; /* Random Number Generator */ |
90 | | |
91 | | /* ===== Engine "management" functions ===== */ |
92 | | |
93 | | /* Prepare the ENGINE structure for registration */ |
94 | | static int padlock_bind_helper(ENGINE *e) |
95 | 0 | { |
96 | | /* Check available features */ |
97 | 0 | padlock_available(); |
98 | | |
99 | | /* |
100 | | * RNG is currently disabled for reasons discussed in commentary just |
101 | | * before padlock_rand_bytes function. |
102 | | */ |
103 | 0 | padlock_use_rng = 0; |
104 | | |
105 | | /* Generate a nice engine name with available features */ |
106 | 0 | BIO_snprintf(padlock_name, sizeof(padlock_name), |
107 | 0 | "VIA PadLock (%s, %s)", |
108 | 0 | padlock_use_rng ? "RNG" : "no-RNG", |
109 | 0 | padlock_use_ace ? "ACE" : "no-ACE"); |
110 | | |
111 | | /* Register everything or return with an error */ |
112 | 0 | if (!ENGINE_set_id(e, padlock_id) || !ENGINE_set_name(e, padlock_name) || !ENGINE_set_init_function(e, padlock_init) || (padlock_use_ace && !ENGINE_set_ciphers(e, padlock_ciphers)) || (padlock_use_rng && !ENGINE_set_RAND(e, &padlock_rand))) { |
113 | 0 | return 0; |
114 | 0 | } |
115 | | |
116 | | /* Everything looks good */ |
117 | 0 | return 1; |
118 | 0 | } |
119 | | |
120 | | #ifdef OPENSSL_NO_DYNAMIC_ENGINE |
121 | | /* Constructor */ |
122 | | static ENGINE *ENGINE_padlock(void) |
123 | 0 | { |
124 | 0 | ENGINE *eng = ENGINE_new(); |
125 | |
|
126 | 0 | if (eng == NULL) { |
127 | 0 | return NULL; |
128 | 0 | } |
129 | | |
130 | 0 | if (!padlock_bind_helper(eng)) { |
131 | 0 | ENGINE_free(eng); |
132 | 0 | return NULL; |
133 | 0 | } |
134 | | |
135 | 0 | return eng; |
136 | 0 | } |
137 | | #endif |
138 | | |
139 | | /* Check availability of the engine */ |
140 | | static int padlock_init(ENGINE *e) |
141 | 0 | { |
142 | 0 | return (padlock_use_rng || padlock_use_ace); |
143 | 0 | } |
144 | | |
145 | | #ifndef AES_ASM |
146 | | static int padlock_aes_set_encrypt_key(const unsigned char *userKey, |
147 | | const int bits, |
148 | | AES_KEY *key); |
149 | | static int padlock_aes_set_decrypt_key(const unsigned char *userKey, |
150 | | const int bits, |
151 | | AES_KEY *key); |
152 | | #define AES_ASM |
153 | | #define AES_set_encrypt_key padlock_aes_set_encrypt_key |
154 | | #define AES_set_decrypt_key padlock_aes_set_decrypt_key |
155 | | /* clang-format off */ |
156 | | # include "../crypto/aes/aes_core.c" |
157 | | /* clang-format on */ |
158 | | #endif |
159 | | |
160 | | /* |
161 | | * This stuff is needed if this ENGINE is being compiled into a |
162 | | * self-contained shared-library. |
163 | | */ |
164 | | #ifndef OPENSSL_NO_DYNAMIC_ENGINE |
165 | | static int padlock_bind_fn(ENGINE *e, const char *id) |
166 | | { |
167 | | if (id && (strcmp(id, padlock_id) != 0)) { |
168 | | return 0; |
169 | | } |
170 | | |
171 | | if (!padlock_bind_helper(e)) { |
172 | | return 0; |
173 | | } |
174 | | |
175 | | return 1; |
176 | | } |
177 | | |
178 | | IMPLEMENT_DYNAMIC_CHECK_FN() |
179 | | IMPLEMENT_DYNAMIC_BIND_FN(padlock_bind_fn) |
180 | | #endif /* !OPENSSL_NO_DYNAMIC_ENGINE */ |
181 | | /* ===== Here comes the "real" engine ===== */ |
182 | | |
183 | | /* Some AES-related constants */ |
184 | 0 | #define AES_BLOCK_SIZE 16 |
185 | 0 | #define AES_KEY_SIZE_128 16 |
186 | 0 | #define AES_KEY_SIZE_192 24 |
187 | 0 | #define AES_KEY_SIZE_256 32 |
188 | | /* |
189 | | * Here we store the status information relevant to the current context. |
190 | | */ |
191 | | /* |
192 | | * BIG FAT WARNING: Inline assembler in PADLOCK_XCRYPT_ASM() depends on |
193 | | * the order of items in this structure. Don't blindly modify, reorder, |
194 | | * etc! |
195 | | */ |
196 | | struct padlock_cipher_data { |
197 | | unsigned char iv[AES_BLOCK_SIZE]; /* Initialization vector */ |
198 | | union { |
199 | | unsigned int pad[4]; |
200 | | struct { |
201 | | int rounds : 4; |
202 | | int dgst : 1; /* n/a in C3 */ |
203 | | int align : 1; /* n/a in C3 */ |
204 | | int ciphr : 1; /* n/a in C3 */ |
205 | | unsigned int keygen : 1; |
206 | | int interm : 1; |
207 | | unsigned int encdec : 1; |
208 | | int ksize : 2; |
209 | | } b; |
210 | | } cword; /* Control word */ |
211 | | AES_KEY ks; /* Encryption key */ |
212 | | }; |
213 | | |
214 | | /* Interface to assembler module */ |
215 | | unsigned int padlock_capability(void); |
216 | | void padlock_key_bswap(AES_KEY *key); |
217 | | void padlock_verify_context(struct padlock_cipher_data *ctx); |
218 | | void padlock_reload_key(void); |
219 | | void padlock_aes_block(void *out, const void *inp, |
220 | | struct padlock_cipher_data *ctx); |
221 | | int padlock_ecb_encrypt(void *out, const void *inp, |
222 | | struct padlock_cipher_data *ctx, size_t len); |
223 | | int padlock_cbc_encrypt(void *out, const void *inp, |
224 | | struct padlock_cipher_data *ctx, size_t len); |
225 | | int padlock_cfb_encrypt(void *out, const void *inp, |
226 | | struct padlock_cipher_data *ctx, size_t len); |
227 | | int padlock_ofb_encrypt(void *out, const void *inp, |
228 | | struct padlock_cipher_data *ctx, size_t len); |
229 | | int padlock_ctr32_encrypt(void *out, const void *inp, |
230 | | struct padlock_cipher_data *ctx, size_t len); |
231 | | int padlock_xstore(void *out, int edx); |
232 | | void padlock_sha1_oneshot(void *ctx, const void *inp, size_t len); |
233 | | void padlock_sha1(void *ctx, const void *inp, size_t len); |
234 | | void padlock_sha256_oneshot(void *ctx, const void *inp, size_t len); |
235 | | void padlock_sha256(void *ctx, const void *inp, size_t len); |
236 | | |
237 | | /* |
238 | | * Load supported features of the CPU to see if the PadLock is available. |
239 | | */ |
240 | | static int padlock_available(void) |
241 | 0 | { |
242 | 0 | unsigned int edx = padlock_capability(); |
243 | | |
244 | | /* Fill up some flags */ |
245 | 0 | padlock_use_ace = ((edx & (0x3 << 6)) == (0x3 << 6)); |
246 | 0 | padlock_use_rng = ((edx & (0x3 << 2)) == (0x3 << 2)); |
247 | |
|
248 | 0 | return padlock_use_ace + padlock_use_rng; |
249 | 0 | } |
250 | | |
251 | | /* ===== AES encryption/decryption ===== */ |
252 | | |
253 | | #if defined(NID_aes_128_cfb128) && !defined(NID_aes_128_cfb) |
254 | 0 | #define NID_aes_128_cfb NID_aes_128_cfb128 |
255 | | #endif |
256 | | |
257 | | #if defined(NID_aes_128_ofb128) && !defined(NID_aes_128_ofb) |
258 | 0 | #define NID_aes_128_ofb NID_aes_128_ofb128 |
259 | | #endif |
260 | | |
261 | | #if defined(NID_aes_192_cfb128) && !defined(NID_aes_192_cfb) |
262 | 0 | #define NID_aes_192_cfb NID_aes_192_cfb128 |
263 | | #endif |
264 | | |
265 | | #if defined(NID_aes_192_ofb128) && !defined(NID_aes_192_ofb) |
266 | 0 | #define NID_aes_192_ofb NID_aes_192_ofb128 |
267 | | #endif |
268 | | |
269 | | #if defined(NID_aes_256_cfb128) && !defined(NID_aes_256_cfb) |
270 | 0 | #define NID_aes_256_cfb NID_aes_256_cfb128 |
271 | | #endif |
272 | | |
273 | | #if defined(NID_aes_256_ofb128) && !defined(NID_aes_256_ofb) |
274 | 0 | #define NID_aes_256_ofb NID_aes_256_ofb128 |
275 | | #endif |
276 | | |
277 | | /* List of supported ciphers. */ |
278 | | static const int padlock_cipher_nids[] = { |
279 | | NID_aes_128_ecb, |
280 | | NID_aes_128_cbc, |
281 | | NID_aes_128_cfb, |
282 | | NID_aes_128_ofb, |
283 | | NID_aes_128_ctr, |
284 | | |
285 | | NID_aes_192_ecb, |
286 | | NID_aes_192_cbc, |
287 | | NID_aes_192_cfb, |
288 | | NID_aes_192_ofb, |
289 | | NID_aes_192_ctr, |
290 | | |
291 | | NID_aes_256_ecb, |
292 | | NID_aes_256_cbc, |
293 | | NID_aes_256_cfb, |
294 | | NID_aes_256_ofb, |
295 | | NID_aes_256_ctr |
296 | | }; |
297 | | |
298 | | static int padlock_cipher_nids_num = (sizeof(padlock_cipher_nids) / sizeof(padlock_cipher_nids[0])); |
299 | | |
300 | | /* Function prototypes ... */ |
301 | | static int padlock_aes_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, |
302 | | const unsigned char *iv, int enc); |
303 | | |
304 | 0 | #define NEAREST_ALIGNED(ptr) ((unsigned char *)(ptr) + ((0x10 - ((size_t)(ptr) & 0x0F)) & 0x0F)) |
305 | 0 | #define ALIGNED_CIPHER_DATA(ctx) ((struct padlock_cipher_data *) \ |
306 | 0 | NEAREST_ALIGNED(EVP_CIPHER_CTX_get_cipher_data(ctx))) |
307 | | |
308 | | static int |
309 | | padlock_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out_arg, |
310 | | const unsigned char *in_arg, size_t nbytes) |
311 | 0 | { |
312 | 0 | return padlock_ecb_encrypt(out_arg, in_arg, |
313 | 0 | ALIGNED_CIPHER_DATA(ctx), nbytes); |
314 | 0 | } |
315 | | |
316 | | static int |
317 | | padlock_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out_arg, |
318 | | const unsigned char *in_arg, size_t nbytes) |
319 | 0 | { |
320 | 0 | struct padlock_cipher_data *cdata = ALIGNED_CIPHER_DATA(ctx); |
321 | 0 | int ret; |
322 | |
|
323 | 0 | memcpy(cdata->iv, EVP_CIPHER_CTX_iv(ctx), AES_BLOCK_SIZE); |
324 | 0 | if ((ret = padlock_cbc_encrypt(out_arg, in_arg, cdata, nbytes))) |
325 | 0 | memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), cdata->iv, AES_BLOCK_SIZE); |
326 | 0 | return ret; |
327 | 0 | } |
328 | | |
329 | | static int |
330 | | padlock_cfb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out_arg, |
331 | | const unsigned char *in_arg, size_t nbytes) |
332 | 0 | { |
333 | 0 | struct padlock_cipher_data *cdata = ALIGNED_CIPHER_DATA(ctx); |
334 | 0 | size_t chunk; |
335 | |
|
336 | 0 | if ((chunk = EVP_CIPHER_CTX_get_num(ctx))) { /* borrow chunk variable */ |
337 | 0 | unsigned char *ivp = EVP_CIPHER_CTX_iv_noconst(ctx); |
338 | |
|
339 | 0 | if (chunk >= AES_BLOCK_SIZE) |
340 | 0 | return 0; /* bogus value */ |
341 | | |
342 | 0 | if (EVP_CIPHER_CTX_is_encrypting(ctx)) |
343 | 0 | while (chunk < AES_BLOCK_SIZE && nbytes != 0) { |
344 | 0 | ivp[chunk] = *(out_arg++) = *(in_arg++) ^ ivp[chunk]; |
345 | 0 | chunk++, nbytes--; |
346 | 0 | } |
347 | 0 | else |
348 | 0 | while (chunk < AES_BLOCK_SIZE && nbytes != 0) { |
349 | 0 | unsigned char c = *(in_arg++); |
350 | 0 | *(out_arg++) = c ^ ivp[chunk]; |
351 | 0 | ivp[chunk++] = c, nbytes--; |
352 | 0 | } |
353 | |
|
354 | 0 | EVP_CIPHER_CTX_set_num(ctx, chunk % AES_BLOCK_SIZE); |
355 | 0 | } |
356 | | |
357 | 0 | if (nbytes == 0) |
358 | 0 | return 1; |
359 | | |
360 | 0 | memcpy(cdata->iv, EVP_CIPHER_CTX_iv(ctx), AES_BLOCK_SIZE); |
361 | |
|
362 | 0 | if ((chunk = nbytes & ~(AES_BLOCK_SIZE - 1))) { |
363 | 0 | if (!padlock_cfb_encrypt(out_arg, in_arg, cdata, chunk)) |
364 | 0 | return 0; |
365 | 0 | nbytes -= chunk; |
366 | 0 | } |
367 | | |
368 | 0 | if (nbytes) { |
369 | 0 | unsigned char *ivp = cdata->iv; |
370 | |
|
371 | 0 | out_arg += chunk; |
372 | 0 | in_arg += chunk; |
373 | 0 | EVP_CIPHER_CTX_set_num(ctx, nbytes); |
374 | 0 | if (cdata->cword.b.encdec) { |
375 | 0 | cdata->cword.b.encdec = 0; |
376 | 0 | padlock_reload_key(); |
377 | 0 | padlock_aes_block(ivp, ivp, cdata); |
378 | 0 | cdata->cword.b.encdec = 1; |
379 | 0 | padlock_reload_key(); |
380 | 0 | while (nbytes) { |
381 | 0 | unsigned char c = *(in_arg++); |
382 | 0 | *(out_arg++) = c ^ *ivp; |
383 | 0 | *(ivp++) = c, nbytes--; |
384 | 0 | } |
385 | 0 | } else { |
386 | 0 | padlock_reload_key(); |
387 | 0 | padlock_aes_block(ivp, ivp, cdata); |
388 | 0 | padlock_reload_key(); |
389 | 0 | while (nbytes) { |
390 | 0 | *ivp = *(out_arg++) = *(in_arg++) ^ *ivp; |
391 | 0 | ivp++, nbytes--; |
392 | 0 | } |
393 | 0 | } |
394 | 0 | } |
395 | |
|
396 | 0 | memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), cdata->iv, AES_BLOCK_SIZE); |
397 | |
|
398 | 0 | return 1; |
399 | 0 | } |
400 | | |
401 | | static int |
402 | | padlock_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out_arg, |
403 | | const unsigned char *in_arg, size_t nbytes) |
404 | 0 | { |
405 | 0 | struct padlock_cipher_data *cdata = ALIGNED_CIPHER_DATA(ctx); |
406 | 0 | size_t chunk; |
407 | | |
408 | | /* |
409 | | * ctx->num is maintained in byte-oriented modes, such as CFB and OFB... |
410 | | */ |
411 | 0 | if ((chunk = EVP_CIPHER_CTX_get_num(ctx))) { /* borrow chunk variable */ |
412 | 0 | unsigned char *ivp = EVP_CIPHER_CTX_iv_noconst(ctx); |
413 | |
|
414 | 0 | if (chunk >= AES_BLOCK_SIZE) |
415 | 0 | return 0; /* bogus value */ |
416 | | |
417 | 0 | while (chunk < AES_BLOCK_SIZE && nbytes != 0) { |
418 | 0 | *(out_arg++) = *(in_arg++) ^ ivp[chunk]; |
419 | 0 | chunk++, nbytes--; |
420 | 0 | } |
421 | |
|
422 | 0 | EVP_CIPHER_CTX_set_num(ctx, chunk % AES_BLOCK_SIZE); |
423 | 0 | } |
424 | | |
425 | 0 | if (nbytes == 0) |
426 | 0 | return 1; |
427 | | |
428 | 0 | memcpy(cdata->iv, EVP_CIPHER_CTX_iv(ctx), AES_BLOCK_SIZE); |
429 | |
|
430 | 0 | if ((chunk = nbytes & ~(AES_BLOCK_SIZE - 1))) { |
431 | 0 | if (!padlock_ofb_encrypt(out_arg, in_arg, cdata, chunk)) |
432 | 0 | return 0; |
433 | 0 | nbytes -= chunk; |
434 | 0 | } |
435 | | |
436 | 0 | if (nbytes) { |
437 | 0 | unsigned char *ivp = cdata->iv; |
438 | |
|
439 | 0 | out_arg += chunk; |
440 | 0 | in_arg += chunk; |
441 | 0 | EVP_CIPHER_CTX_set_num(ctx, nbytes); |
442 | 0 | padlock_reload_key(); /* empirically found */ |
443 | 0 | padlock_aes_block(ivp, ivp, cdata); |
444 | 0 | padlock_reload_key(); /* empirically found */ |
445 | 0 | while (nbytes) { |
446 | 0 | *(out_arg++) = *(in_arg++) ^ *ivp; |
447 | 0 | ivp++, nbytes--; |
448 | 0 | } |
449 | 0 | } |
450 | |
|
451 | 0 | memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), cdata->iv, AES_BLOCK_SIZE); |
452 | |
|
453 | 0 | return 1; |
454 | 0 | } |
455 | | |
456 | | static void padlock_ctr32_encrypt_glue(const unsigned char *in, |
457 | | unsigned char *out, size_t blocks, |
458 | | struct padlock_cipher_data *ctx, |
459 | | const unsigned char *ivec) |
460 | 0 | { |
461 | 0 | memcpy(ctx->iv, ivec, AES_BLOCK_SIZE); |
462 | 0 | padlock_ctr32_encrypt(out, in, ctx, AES_BLOCK_SIZE * blocks); |
463 | 0 | } |
464 | | |
465 | | static int |
466 | | padlock_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out_arg, |
467 | | const unsigned char *in_arg, size_t nbytes) |
468 | 0 | { |
469 | 0 | struct padlock_cipher_data *cdata = ALIGNED_CIPHER_DATA(ctx); |
470 | 0 | int n = EVP_CIPHER_CTX_get_num(ctx); |
471 | 0 | unsigned int num; |
472 | |
|
473 | 0 | if (n < 0) |
474 | 0 | return 0; |
475 | 0 | num = (unsigned int)n; |
476 | |
|
477 | 0 | CRYPTO_ctr128_encrypt_ctr32(in_arg, out_arg, nbytes, |
478 | 0 | cdata, EVP_CIPHER_CTX_iv_noconst(ctx), |
479 | 0 | EVP_CIPHER_CTX_buf_noconst(ctx), &num, |
480 | 0 | (ctr128_f)padlock_ctr32_encrypt_glue); |
481 | |
|
482 | 0 | EVP_CIPHER_CTX_set_num(ctx, (size_t)num); |
483 | 0 | return 1; |
484 | 0 | } |
485 | | |
486 | 0 | #define EVP_CIPHER_block_size_ECB AES_BLOCK_SIZE |
487 | 0 | #define EVP_CIPHER_block_size_CBC AES_BLOCK_SIZE |
488 | 0 | #define EVP_CIPHER_block_size_OFB 1 |
489 | 0 | #define EVP_CIPHER_block_size_CFB 1 |
490 | 0 | #define EVP_CIPHER_block_size_CTR 1 |
491 | | |
492 | | /* |
493 | | * Declaring so many ciphers by hand would be a pain. Instead introduce a bit |
494 | | * of preprocessor magic :-) |
495 | | */ |
496 | | #define DECLARE_AES_EVP(ksize, lmode, umode) \ |
497 | | static EVP_CIPHER *_hidden_aes_##ksize##_##lmode = NULL; \ |
498 | | static const EVP_CIPHER *padlock_aes_##ksize##_##lmode(void) \ |
499 | 0 | { \ |
500 | 0 | if (_hidden_aes_##ksize##_##lmode == NULL \ |
501 | 0 | && ((_hidden_aes_##ksize##_##lmode = EVP_CIPHER_meth_new(NID_aes_##ksize##_##lmode, \ |
502 | 0 | EVP_CIPHER_block_size_##umode, \ |
503 | 0 | AES_KEY_SIZE_##ksize)) \ |
504 | 0 | == NULL \ |
505 | 0 | || !EVP_CIPHER_meth_set_iv_length(_hidden_aes_##ksize##_##lmode, \ |
506 | 0 | AES_BLOCK_SIZE) \ |
507 | 0 | || !EVP_CIPHER_meth_set_flags(_hidden_aes_##ksize##_##lmode, \ |
508 | 0 | 0 | EVP_CIPH_##umode##_MODE) \ |
509 | 0 | || !EVP_CIPHER_meth_set_init(_hidden_aes_##ksize##_##lmode, \ |
510 | 0 | padlock_aes_init_key) \ |
511 | 0 | || !EVP_CIPHER_meth_set_do_cipher(_hidden_aes_##ksize##_##lmode, \ |
512 | 0 | padlock_##lmode##_cipher) \ |
513 | 0 | || !EVP_CIPHER_meth_set_impl_ctx_size(_hidden_aes_##ksize##_##lmode, \ |
514 | 0 | sizeof(struct padlock_cipher_data) + 16) \ |
515 | 0 | || !EVP_CIPHER_meth_set_set_asn1_params(_hidden_aes_##ksize##_##lmode, \ |
516 | 0 | EVP_CIPHER_set_asn1_iv) \ |
517 | 0 | || !EVP_CIPHER_meth_set_get_asn1_params(_hidden_aes_##ksize##_##lmode, \ |
518 | 0 | EVP_CIPHER_get_asn1_iv))) { \ |
519 | 0 | EVP_CIPHER_meth_free(_hidden_aes_##ksize##_##lmode); \ |
520 | 0 | _hidden_aes_##ksize##_##lmode = NULL; \ |
521 | 0 | } \ |
522 | 0 | return _hidden_aes_##ksize##_##lmode; \ |
523 | 0 | } |
524 | | |
525 | 0 | DECLARE_AES_EVP(128, ecb, ECB) |
526 | 0 | DECLARE_AES_EVP(128, cbc, CBC) |
527 | 0 | DECLARE_AES_EVP(128, cfb, CFB) |
528 | 0 | DECLARE_AES_EVP(128, ofb, OFB) |
529 | 0 | DECLARE_AES_EVP(128, ctr, CTR) |
530 | | |
531 | 0 | DECLARE_AES_EVP(192, ecb, ECB) |
532 | 0 | DECLARE_AES_EVP(192, cbc, CBC) |
533 | 0 | DECLARE_AES_EVP(192, cfb, CFB) |
534 | 0 | DECLARE_AES_EVP(192, ofb, OFB) |
535 | 0 | DECLARE_AES_EVP(192, ctr, CTR) |
536 | | |
537 | 0 | DECLARE_AES_EVP(256, ecb, ECB) |
538 | 0 | DECLARE_AES_EVP(256, cbc, CBC) |
539 | 0 | DECLARE_AES_EVP(256, cfb, CFB) |
540 | 0 | DECLARE_AES_EVP(256, ofb, OFB) |
541 | 0 | DECLARE_AES_EVP(256, ctr, CTR) |
542 | | |
543 | | static int |
544 | | padlock_ciphers(ENGINE *e, const EVP_CIPHER **cipher, const int **nids, |
545 | | int nid) |
546 | 0 | { |
547 | | /* No specific cipher => return a list of supported nids ... */ |
548 | 0 | if (!cipher) { |
549 | 0 | *nids = padlock_cipher_nids; |
550 | 0 | return padlock_cipher_nids_num; |
551 | 0 | } |
552 | | |
553 | | /* ... or the requested "cipher" otherwise */ |
554 | 0 | switch (nid) { |
555 | 0 | case NID_aes_128_ecb: |
556 | 0 | *cipher = padlock_aes_128_ecb(); |
557 | 0 | break; |
558 | 0 | case NID_aes_128_cbc: |
559 | 0 | *cipher = padlock_aes_128_cbc(); |
560 | 0 | break; |
561 | 0 | case NID_aes_128_cfb: |
562 | 0 | *cipher = padlock_aes_128_cfb(); |
563 | 0 | break; |
564 | 0 | case NID_aes_128_ofb: |
565 | 0 | *cipher = padlock_aes_128_ofb(); |
566 | 0 | break; |
567 | 0 | case NID_aes_128_ctr: |
568 | 0 | *cipher = padlock_aes_128_ctr(); |
569 | 0 | break; |
570 | | |
571 | 0 | case NID_aes_192_ecb: |
572 | 0 | *cipher = padlock_aes_192_ecb(); |
573 | 0 | break; |
574 | 0 | case NID_aes_192_cbc: |
575 | 0 | *cipher = padlock_aes_192_cbc(); |
576 | 0 | break; |
577 | 0 | case NID_aes_192_cfb: |
578 | 0 | *cipher = padlock_aes_192_cfb(); |
579 | 0 | break; |
580 | 0 | case NID_aes_192_ofb: |
581 | 0 | *cipher = padlock_aes_192_ofb(); |
582 | 0 | break; |
583 | 0 | case NID_aes_192_ctr: |
584 | 0 | *cipher = padlock_aes_192_ctr(); |
585 | 0 | break; |
586 | | |
587 | 0 | case NID_aes_256_ecb: |
588 | 0 | *cipher = padlock_aes_256_ecb(); |
589 | 0 | break; |
590 | 0 | case NID_aes_256_cbc: |
591 | 0 | *cipher = padlock_aes_256_cbc(); |
592 | 0 | break; |
593 | 0 | case NID_aes_256_cfb: |
594 | 0 | *cipher = padlock_aes_256_cfb(); |
595 | 0 | break; |
596 | 0 | case NID_aes_256_ofb: |
597 | 0 | *cipher = padlock_aes_256_ofb(); |
598 | 0 | break; |
599 | 0 | case NID_aes_256_ctr: |
600 | 0 | *cipher = padlock_aes_256_ctr(); |
601 | 0 | break; |
602 | | |
603 | 0 | default: |
604 | | /* Sorry, we don't support this NID */ |
605 | 0 | *cipher = NULL; |
606 | 0 | return 0; |
607 | 0 | } |
608 | | |
609 | 0 | return 1; |
610 | 0 | } |
611 | | |
612 | | /* Prepare the encryption key for PadLock usage */ |
613 | | static int |
614 | | padlock_aes_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, |
615 | | const unsigned char *iv, int enc) |
616 | 0 | { |
617 | 0 | struct padlock_cipher_data *cdata; |
618 | 0 | int key_len = EVP_CIPHER_CTX_get_key_length(ctx) * 8; |
619 | 0 | unsigned long mode = EVP_CIPHER_CTX_get_mode(ctx); |
620 | |
|
621 | 0 | if (key == NULL) |
622 | 0 | return 0; /* ERROR */ |
623 | | |
624 | 0 | cdata = ALIGNED_CIPHER_DATA(ctx); |
625 | 0 | memset(cdata, 0, sizeof(*cdata)); |
626 | | |
627 | | /* Prepare Control word. */ |
628 | 0 | if (mode == EVP_CIPH_OFB_MODE || mode == EVP_CIPH_CTR_MODE) |
629 | 0 | cdata->cword.b.encdec = 0; |
630 | 0 | else |
631 | 0 | cdata->cword.b.encdec = (EVP_CIPHER_CTX_is_encrypting(ctx) == 0); |
632 | 0 | cdata->cword.b.rounds = 10 + (key_len - 128) / 32; |
633 | 0 | cdata->cword.b.ksize = (key_len - 128) / 64; |
634 | |
|
635 | 0 | switch (key_len) { |
636 | 0 | case 128: |
637 | | /* |
638 | | * PadLock can generate an extended key for AES128 in hardware |
639 | | */ |
640 | 0 | memcpy(cdata->ks.rd_key, key, AES_KEY_SIZE_128); |
641 | 0 | cdata->cword.b.keygen = 0; |
642 | 0 | break; |
643 | | |
644 | 0 | case 192: |
645 | 0 | case 256: |
646 | | /* |
647 | | * Generate an extended AES key in software. Needed for AES192/AES256 |
648 | | */ |
649 | | /* |
650 | | * Well, the above applies to Stepping 8 CPUs and is listed as |
651 | | * hardware errata. They most likely will fix it at some point and |
652 | | * then a check for stepping would be due here. |
653 | | */ |
654 | 0 | if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE) |
655 | 0 | && !enc) |
656 | 0 | AES_set_decrypt_key(key, key_len, &cdata->ks); |
657 | 0 | else |
658 | 0 | AES_set_encrypt_key(key, key_len, &cdata->ks); |
659 | | /* |
660 | | * OpenSSL C functions use byte-swapped extended key. |
661 | | */ |
662 | 0 | padlock_key_bswap(&cdata->ks); |
663 | 0 | cdata->cword.b.keygen = 1; |
664 | 0 | break; |
665 | | |
666 | 0 | default: |
667 | | /* ERROR */ |
668 | 0 | return 0; |
669 | 0 | } |
670 | | |
671 | | /* |
672 | | * This is done to cover for cases when user reuses the |
673 | | * context for new key. The catch is that if we don't do |
674 | | * this, padlock_eas_cipher might proceed with old key... |
675 | | */ |
676 | 0 | padlock_reload_key(); |
677 | |
|
678 | 0 | return 1; |
679 | 0 | } |
680 | | |
681 | | /* ===== Random Number Generator ===== */ |
682 | | /* |
683 | | * This code is not engaged. The reason is that it does not comply |
684 | | * with recommendations for VIA RNG usage for secure applications |
685 | | * (posted at http://www.via.com.tw/en/viac3/c3.jsp) nor does it |
686 | | * provide meaningful error control... |
687 | | */ |
688 | | /* |
689 | | * Wrapper that provides an interface between the API and the raw PadLock |
690 | | * RNG |
691 | | */ |
692 | | static int padlock_rand_bytes(unsigned char *output, int count) |
693 | 0 | { |
694 | 0 | unsigned int eax, buf; |
695 | |
|
696 | 0 | while (count >= 8) { |
697 | 0 | eax = padlock_xstore(output, 0); |
698 | 0 | if (!(eax & (1 << 6))) |
699 | 0 | return 0; /* RNG disabled */ |
700 | | /* this ---vv--- covers DC bias, Raw Bits and String Filter */ |
701 | 0 | if (eax & (0x1F << 10)) |
702 | 0 | return 0; |
703 | 0 | if ((eax & 0x1F) == 0) |
704 | 0 | continue; /* no data, retry... */ |
705 | 0 | if ((eax & 0x1F) != 8) |
706 | 0 | return 0; /* fatal failure... */ |
707 | 0 | output += 8; |
708 | 0 | count -= 8; |
709 | 0 | } |
710 | 0 | while (count > 0) { |
711 | 0 | eax = padlock_xstore(&buf, 3); |
712 | 0 | if (!(eax & (1 << 6))) |
713 | 0 | return 0; /* RNG disabled */ |
714 | | /* this ---vv--- covers DC bias, Raw Bits and String Filter */ |
715 | 0 | if (eax & (0x1F << 10)) |
716 | 0 | return 0; |
717 | 0 | if ((eax & 0x1F) == 0) |
718 | 0 | continue; /* no data, retry... */ |
719 | 0 | if ((eax & 0x1F) != 1) |
720 | 0 | return 0; /* fatal failure... */ |
721 | 0 | *output++ = (unsigned char)buf; |
722 | 0 | count--; |
723 | 0 | } |
724 | 0 | OPENSSL_cleanse(&buf, sizeof(buf)); |
725 | |
|
726 | 0 | return 1; |
727 | 0 | } |
728 | | |
729 | | /* Dummy but necessary function */ |
730 | | static int padlock_rand_status(void) |
731 | 0 | { |
732 | 0 | return 1; |
733 | 0 | } |
734 | | |
735 | | /* Prepare structure for registration */ |
736 | | static RAND_METHOD padlock_rand = { |
737 | | NULL, /* seed */ |
738 | | padlock_rand_bytes, /* bytes */ |
739 | | NULL, /* cleanup */ |
740 | | NULL, /* add */ |
741 | | padlock_rand_bytes, /* pseudorand */ |
742 | | padlock_rand_status, /* rand status */ |
743 | | }; |
744 | | |
745 | | #endif /* COMPILE_PADLOCKENG */ |
746 | | #endif /* !OPENSSL_NO_PADLOCKENG */ |
747 | | |
748 | | #if defined(OPENSSL_NO_PADLOCKENG) || !defined(COMPILE_PADLOCKENG) |
749 | | #ifndef OPENSSL_NO_DYNAMIC_ENGINE |
750 | | OPENSSL_EXPORT |
751 | | int bind_engine(ENGINE *e, const char *id, const dynamic_fns *fns); |
752 | | OPENSSL_EXPORT |
753 | | int bind_engine(ENGINE *e, const char *id, const dynamic_fns *fns) |
754 | | { |
755 | | return 0; |
756 | | } |
757 | | |
758 | | IMPLEMENT_DYNAMIC_CHECK_FN() |
759 | | #endif |
760 | | #endif |