e_padlock.c 23 KB

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