e_padlock.c 22 KB

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