2
0

e_padlock.c 26 KB

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