e_padlock.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258
  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. #ifndef OPENSSL_NO_HW
  76. # ifndef OPENSSL_NO_HW_PADLOCK
  77. /* Attempt to have a single source for both 0.9.7 and 0.9.8 :-) */
  78. # if (OPENSSL_VERSION_NUMBER >= 0x00908000L)
  79. # ifndef OPENSSL_NO_DYNAMIC_ENGINE
  80. # define DYNAMIC_ENGINE
  81. # endif
  82. # elif (OPENSSL_VERSION_NUMBER >= 0x00907000L)
  83. # ifdef ENGINE_DYNAMIC_SUPPORT
  84. # define DYNAMIC_ENGINE
  85. # endif
  86. # else
  87. # error "Only OpenSSL >= 0.9.7 is supported"
  88. # endif
  89. /*
  90. * VIA PadLock AES is available *ONLY* on some x86 CPUs. Not only that it
  91. * doesn't exist elsewhere, but it even can't be compiled on other platforms!
  92. *
  93. * In addition, because of the heavy use of inline assembler, compiler choice
  94. * is limited to GCC and Microsoft C.
  95. */
  96. # undef COMPILE_HW_PADLOCK
  97. # if !defined(I386_ONLY) && !defined(OPENSSL_NO_INLINE_ASM)
  98. # if (defined(__GNUC__) && (defined(__i386__) || defined(__i386))) || \
  99. (defined(_MSC_VER) && defined(_M_IX86))
  100. # define COMPILE_HW_PADLOCK
  101. static ENGINE *ENGINE_padlock(void);
  102. # endif
  103. # endif
  104. # ifdef OPENSSL_NO_DYNAMIC_ENGINE
  105. void ENGINE_load_padlock(void)
  106. {
  107. /* On non-x86 CPUs it just returns. */
  108. # ifdef COMPILE_HW_PADLOCK
  109. ENGINE *toadd = ENGINE_padlock();
  110. if (!toadd)
  111. return;
  112. ENGINE_add(toadd);
  113. ENGINE_free(toadd);
  114. ERR_clear_error();
  115. # endif
  116. }
  117. # endif
  118. # ifdef COMPILE_HW_PADLOCK
  119. /*
  120. * We do these includes here to avoid header problems on platforms that do
  121. * not have the VIA padlock anyway...
  122. */
  123. # include <stdlib.h>
  124. # ifdef _WIN32
  125. # include <malloc.h>
  126. # ifndef alloca
  127. # define alloca _alloca
  128. # endif
  129. # elif defined(__GNUC__)
  130. # ifndef alloca
  131. # define alloca(s) __builtin_alloca(s)
  132. # endif
  133. # endif
  134. /* Function for ENGINE detection and control */
  135. static int padlock_available(void);
  136. static int padlock_init(ENGINE *e);
  137. /* RNG Stuff */
  138. static RAND_METHOD padlock_rand;
  139. /* Cipher Stuff */
  140. # ifndef OPENSSL_NO_AES
  141. static int padlock_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
  142. const int **nids, int nid);
  143. # endif
  144. /* Engine names */
  145. static const char *padlock_id = "padlock";
  146. static char padlock_name[100];
  147. /* Available features */
  148. static int padlock_use_ace = 0; /* Advanced Cryptography Engine */
  149. static int padlock_use_rng = 0; /* Random Number Generator */
  150. # ifndef OPENSSL_NO_AES
  151. static int padlock_aes_align_required = 1;
  152. # endif
  153. /* ===== Engine "management" functions ===== */
  154. /* Prepare the ENGINE structure for registration */
  155. static int padlock_bind_helper(ENGINE *e)
  156. {
  157. /* Check available features */
  158. padlock_available();
  159. # if 1 /* disable RNG for now, see commentary in
  160. * vicinity of RNG code */
  161. padlock_use_rng = 0;
  162. # endif
  163. /* Generate a nice engine name with available features */
  164. BIO_snprintf(padlock_name, sizeof(padlock_name),
  165. "VIA PadLock (%s, %s)",
  166. padlock_use_rng ? "RNG" : "no-RNG",
  167. padlock_use_ace ? "ACE" : "no-ACE");
  168. /* Register everything or return with an error */
  169. if (!ENGINE_set_id(e, padlock_id) ||
  170. !ENGINE_set_name(e, padlock_name) ||
  171. !ENGINE_set_init_function(e, padlock_init) ||
  172. # ifndef OPENSSL_NO_AES
  173. (padlock_use_ace && !ENGINE_set_ciphers(e, padlock_ciphers)) ||
  174. # endif
  175. (padlock_use_rng && !ENGINE_set_RAND(e, &padlock_rand))) {
  176. return 0;
  177. }
  178. /* Everything looks good */
  179. return 1;
  180. }
  181. /* Constructor */
  182. static ENGINE *ENGINE_padlock(void)
  183. {
  184. ENGINE *eng = ENGINE_new();
  185. if (!eng) {
  186. return NULL;
  187. }
  188. if (!padlock_bind_helper(eng)) {
  189. ENGINE_free(eng);
  190. return NULL;
  191. }
  192. return eng;
  193. }
  194. /* Check availability of the engine */
  195. static int padlock_init(ENGINE *e)
  196. {
  197. return (padlock_use_rng || padlock_use_ace);
  198. }
  199. /*
  200. * This stuff is needed if this ENGINE is being compiled into a
  201. * self-contained shared-library.
  202. */
  203. # ifdef DYNAMIC_ENGINE
  204. static int padlock_bind_fn(ENGINE *e, const char *id)
  205. {
  206. if (id && (strcmp(id, padlock_id) != 0)) {
  207. return 0;
  208. }
  209. if (!padlock_bind_helper(e)) {
  210. return 0;
  211. }
  212. return 1;
  213. }
  214. IMPLEMENT_DYNAMIC_CHECK_FN()
  215. IMPLEMENT_DYNAMIC_BIND_FN(padlock_bind_fn)
  216. # endif /* DYNAMIC_ENGINE */
  217. /* ===== Here comes the "real" engine ===== */
  218. # ifndef OPENSSL_NO_AES
  219. /* Some AES-related constants */
  220. # define AES_BLOCK_SIZE 16
  221. # define AES_KEY_SIZE_128 16
  222. # define AES_KEY_SIZE_192 24
  223. # define AES_KEY_SIZE_256 32
  224. /*
  225. * Here we store the status information relevant to the current context.
  226. */
  227. /*
  228. * BIG FAT WARNING: Inline assembler in PADLOCK_XCRYPT_ASM() depends on
  229. * the order of items in this structure. Don't blindly modify, reorder,
  230. * etc!
  231. */
  232. struct padlock_cipher_data {
  233. unsigned char iv[AES_BLOCK_SIZE]; /* Initialization vector */
  234. union {
  235. unsigned int pad[4];
  236. struct {
  237. int rounds:4;
  238. int dgst:1; /* n/a in C3 */
  239. int align:1; /* n/a in C3 */
  240. int ciphr:1; /* n/a in C3 */
  241. unsigned int keygen:1;
  242. int interm:1;
  243. unsigned int encdec:1;
  244. int ksize:2;
  245. } b;
  246. } cword; /* Control word */
  247. AES_KEY ks; /* Encryption key */
  248. };
  249. /*
  250. * Essentially this variable belongs in thread local storage.
  251. * Having this variable global on the other hand can only cause
  252. * few bogus key reloads [if any at all on single-CPU system],
  253. * so we accept the penatly...
  254. */
  255. static volatile struct padlock_cipher_data *padlock_saved_context;
  256. # endif
  257. /*-
  258. * =======================================================
  259. * Inline assembler section(s).
  260. * =======================================================
  261. * Order of arguments is chosen to facilitate Windows port
  262. * using __fastcall calling convention. If you wish to add
  263. * more routines, keep in mind that first __fastcall
  264. * argument is passed in %ecx and second - in %edx.
  265. * =======================================================
  266. */
  267. # if defined(__GNUC__) && __GNUC__>=2
  268. /*
  269. * As for excessive "push %ebx"/"pop %ebx" found all over.
  270. * When generating position-independent code GCC won't let
  271. * us use "b" in assembler templates nor even respect "ebx"
  272. * in "clobber description." Therefore the trouble...
  273. */
  274. /*
  275. * Helper function - check if a CPUID instruction is available on this CPU
  276. */
  277. static int padlock_insn_cpuid_available(void)
  278. {
  279. int result = -1;
  280. /*
  281. * We're checking if the bit #21 of EFLAGS can be toggled. If yes =
  282. * CPUID is available.
  283. */
  284. asm volatile ("pushf\n"
  285. "popl %%eax\n"
  286. "xorl $0x200000, %%eax\n"
  287. "movl %%eax, %%ecx\n"
  288. "andl $0x200000, %%ecx\n"
  289. "pushl %%eax\n"
  290. "popf\n"
  291. "pushf\n"
  292. "popl %%eax\n"
  293. "andl $0x200000, %%eax\n"
  294. "xorl %%eax, %%ecx\n"
  295. "movl %%ecx, %0\n":"=r" (result)::"eax", "ecx");
  296. return (result == 0);
  297. }
  298. /*
  299. * Load supported features of the CPU to see if the PadLock is available.
  300. */
  301. static int padlock_available(void)
  302. {
  303. char vendor_string[16];
  304. unsigned int eax, edx;
  305. /* First check if the CPUID instruction is available at all... */
  306. if (!padlock_insn_cpuid_available())
  307. return 0;
  308. /* Are we running on the Centaur (VIA) CPU? */
  309. eax = 0x00000000;
  310. vendor_string[12] = 0;
  311. asm volatile ("pushl %%ebx\n"
  312. "cpuid\n"
  313. "movl %%ebx,(%%edi)\n"
  314. "movl %%edx,4(%%edi)\n"
  315. "movl %%ecx,8(%%edi)\n"
  316. "popl %%ebx":"+a" (eax):"D"(vendor_string):"ecx", "edx");
  317. if (strcmp(vendor_string, "CentaurHauls") != 0)
  318. return 0;
  319. /* Check for Centaur Extended Feature Flags presence */
  320. eax = 0xC0000000;
  321. asm volatile ("pushl %%ebx; cpuid; popl %%ebx":"+a" (eax)::"ecx", "edx");
  322. if (eax < 0xC0000001)
  323. return 0;
  324. /* Read the Centaur Extended Feature Flags */
  325. eax = 0xC0000001;
  326. asm volatile ("pushl %%ebx; cpuid; popl %%ebx":"+a" (eax),
  327. "=d"(edx)::"ecx");
  328. /* Fill up some flags */
  329. padlock_use_ace = ((edx & (0x3 << 6)) == (0x3 << 6));
  330. padlock_use_rng = ((edx & (0x3 << 2)) == (0x3 << 2));
  331. return padlock_use_ace + padlock_use_rng;
  332. }
  333. # ifndef OPENSSL_NO_AES
  334. /* Our own htonl()/ntohl() */
  335. static inline void padlock_bswapl(AES_KEY *ks)
  336. {
  337. size_t i = sizeof(ks->rd_key) / sizeof(ks->rd_key[0]);
  338. unsigned int *key = ks->rd_key;
  339. while (i--) {
  340. asm volatile ("bswapl %0":"+r" (*key));
  341. key++;
  342. }
  343. }
  344. # endif
  345. /*
  346. * Force key reload from memory to the CPU microcode. Loading EFLAGS from the
  347. * stack clears EFLAGS[30] which does the trick.
  348. */
  349. static inline void padlock_reload_key(void)
  350. {
  351. asm volatile ("pushfl; popfl");
  352. }
  353. # ifndef OPENSSL_NO_AES
  354. /*
  355. * This is heuristic key context tracing. At first one
  356. * believes that one should use atomic swap instructions,
  357. * but it's not actually necessary. Point is that if
  358. * padlock_saved_context was changed by another thread
  359. * after we've read it and before we compare it with cdata,
  360. * our key *shall* be reloaded upon thread context switch
  361. * and we are therefore set in either case...
  362. */
  363. static inline void padlock_verify_context(struct padlock_cipher_data *cdata)
  364. {
  365. asm volatile ("pushfl\n"
  366. " btl $30,(%%esp)\n"
  367. " jnc 1f\n"
  368. " cmpl %2,%1\n"
  369. " je 1f\n"
  370. " popfl\n"
  371. " subl $4,%%esp\n"
  372. "1: addl $4,%%esp\n"
  373. " movl %2,%0":"+m" (padlock_saved_context)
  374. :"r"(padlock_saved_context), "r"(cdata):"cc");
  375. }
  376. /* Template for padlock_xcrypt_* modes */
  377. /*
  378. * BIG FAT WARNING: The offsets used with 'leal' instructions describe items
  379. * of the 'padlock_cipher_data' structure.
  380. */
  381. # define PADLOCK_XCRYPT_ASM(name,rep_xcrypt) \
  382. static inline void *name(size_t cnt, \
  383. struct padlock_cipher_data *cdata, \
  384. void *out, const void *inp) \
  385. { void *iv; \
  386. asm volatile ( "pushl %%ebx\n" \
  387. " leal 16(%0),%%edx\n" \
  388. " leal 32(%0),%%ebx\n" \
  389. rep_xcrypt "\n" \
  390. " popl %%ebx" \
  391. : "=a"(iv), "=c"(cnt), "=D"(out), "=S"(inp) \
  392. : "0"(cdata), "1"(cnt), "2"(out), "3"(inp) \
  393. : "edx", "cc", "memory"); \
  394. return iv; \
  395. }
  396. /* Generate all functions with appropriate opcodes */
  397. /* rep xcryptecb */
  398. PADLOCK_XCRYPT_ASM(padlock_xcrypt_ecb, ".byte 0xf3,0x0f,0xa7,0xc8")
  399. /* rep xcryptcbc */
  400. PADLOCK_XCRYPT_ASM(padlock_xcrypt_cbc, ".byte 0xf3,0x0f,0xa7,0xd0")
  401. /* rep xcryptcfb */
  402. PADLOCK_XCRYPT_ASM(padlock_xcrypt_cfb, ".byte 0xf3,0x0f,0xa7,0xe0")
  403. /* rep xcryptofb */
  404. PADLOCK_XCRYPT_ASM(padlock_xcrypt_ofb, ".byte 0xf3,0x0f,0xa7,0xe8")
  405. # endif
  406. /* The RNG call itself */
  407. static inline unsigned int padlock_xstore(void *addr, unsigned int edx_in)
  408. {
  409. unsigned int eax_out;
  410. asm volatile (".byte 0x0f,0xa7,0xc0" /* xstore */
  411. :"=a" (eax_out), "=m"(*(unsigned *)addr)
  412. :"D"(addr), "d"(edx_in)
  413. );
  414. return eax_out;
  415. }
  416. /*
  417. * Why not inline 'rep movsd'? I failed to find information on what value in
  418. * Direction Flag one can expect and consequently have to apply
  419. * "better-safe-than-sorry" approach and assume "undefined." I could
  420. * explicitly clear it and restore the original value upon return from
  421. * padlock_aes_cipher, but it's presumably too much trouble for too little
  422. * gain... In case you wonder 'rep xcrypt*' instructions above are *not*
  423. * affected by the Direction Flag and pointers advance toward larger
  424. * addresses unconditionally.
  425. */
  426. static inline unsigned char *padlock_memcpy(void *dst, const void *src,
  427. size_t n)
  428. {
  429. long *d = dst;
  430. const long *s = src;
  431. n /= sizeof(*d);
  432. do {
  433. *d++ = *s++;
  434. } while (--n);
  435. return dst;
  436. }
  437. # elif defined(_MSC_VER)
  438. /*
  439. * Unlike GCC these are real functions. In order to minimize impact
  440. * on performance we adhere to __fastcall calling convention in
  441. * order to get two first arguments passed through %ecx and %edx.
  442. * Which kind of suits very well, as instructions in question use
  443. * both %ecx and %edx as input:-)
  444. */
  445. # define REP_XCRYPT(code) \
  446. _asm _emit 0xf3 \
  447. _asm _emit 0x0f _asm _emit 0xa7 \
  448. _asm _emit code
  449. /*
  450. * BIG FAT WARNING: The offsets used with 'lea' instructions describe items
  451. * of the 'padlock_cipher_data' structure.
  452. */
  453. # define PADLOCK_XCRYPT_ASM(name,code) \
  454. static void * __fastcall \
  455. name (size_t cnt, void *cdata, \
  456. void *outp, const void *inp) \
  457. { _asm mov eax,edx \
  458. _asm lea edx,[eax+16] \
  459. _asm lea ebx,[eax+32] \
  460. _asm mov edi,outp \
  461. _asm mov esi,inp \
  462. REP_XCRYPT(code) \
  463. }
  464. PADLOCK_XCRYPT_ASM(padlock_xcrypt_ecb,0xc8)
  465. PADLOCK_XCRYPT_ASM(padlock_xcrypt_cbc,0xd0)
  466. PADLOCK_XCRYPT_ASM(padlock_xcrypt_cfb,0xe0)
  467. PADLOCK_XCRYPT_ASM(padlock_xcrypt_ofb,0xe8)
  468. static int __fastcall padlock_xstore(void *outp, unsigned int code)
  469. {
  470. _asm mov edi,ecx
  471. _asm _emit 0x0f _asm _emit 0xa7 _asm _emit 0xc0
  472. }
  473. static void __fastcall padlock_reload_key(void)
  474. {
  475. _asm pushfd
  476. _asm popfd
  477. }
  478. static void __fastcall padlock_verify_context(void *cdata)
  479. {
  480. _asm {
  481. pushfd
  482. bt DWORD PTR[esp],30
  483. jnc skip
  484. cmp ecx,padlock_saved_context
  485. je skip
  486. popfd
  487. sub esp,4
  488. skip: add esp,4
  489. mov padlock_saved_context,ecx
  490. }
  491. }
  492. static int
  493. padlock_available(void)
  494. {
  495. _asm {
  496. pushfd
  497. pop eax
  498. mov ecx,eax
  499. xor eax,1<<21
  500. push eax
  501. popfd
  502. pushfd
  503. pop eax
  504. xor eax,ecx
  505. bt eax,21
  506. jnc noluck
  507. mov eax,0
  508. cpuid
  509. xor eax,eax
  510. cmp ebx,'tneC'
  511. jne noluck
  512. cmp edx,'Hrua'
  513. jne noluck
  514. cmp ecx,'slua'
  515. jne noluck
  516. mov eax,0xC0000000
  517. cpuid
  518. mov edx,eax
  519. xor eax,eax
  520. cmp edx,0xC0000001
  521. jb noluck
  522. mov eax,0xC0000001
  523. cpuid
  524. xor eax,eax
  525. bt edx,6
  526. jnc skip_a
  527. bt edx,7
  528. jnc skip_a
  529. mov padlock_use_ace,1
  530. inc eax
  531. skip_a: bt edx,2
  532. jnc skip_r
  533. bt edx,3
  534. jnc skip_r
  535. mov padlock_use_rng,1
  536. inc eax
  537. skip_r:
  538. noluck:
  539. }
  540. }
  541. static void __fastcall padlock_bswapl(void *key)
  542. {
  543. _asm {
  544. pushfd
  545. cld
  546. mov esi,ecx
  547. mov edi,ecx
  548. mov ecx,60
  549. up: lodsd
  550. bswap eax
  551. stosd
  552. loop up
  553. popfd
  554. }
  555. }
  556. /*
  557. * MS actually specifies status of Direction Flag and compiler even manages
  558. * to compile following as 'rep movsd' all by itself...
  559. */
  560. # define padlock_memcpy(o,i,n) ((unsigned char *)memcpy((o),(i),(n)&~3U))
  561. # endif
  562. /* ===== AES encryption/decryption ===== */
  563. # ifndef OPENSSL_NO_AES
  564. # if defined(NID_aes_128_cfb128) && ! defined (NID_aes_128_cfb)
  565. # define NID_aes_128_cfb NID_aes_128_cfb128
  566. # endif
  567. # if defined(NID_aes_128_ofb128) && ! defined (NID_aes_128_ofb)
  568. # define NID_aes_128_ofb NID_aes_128_ofb128
  569. # endif
  570. # if defined(NID_aes_192_cfb128) && ! defined (NID_aes_192_cfb)
  571. # define NID_aes_192_cfb NID_aes_192_cfb128
  572. # endif
  573. # if defined(NID_aes_192_ofb128) && ! defined (NID_aes_192_ofb)
  574. # define NID_aes_192_ofb NID_aes_192_ofb128
  575. # endif
  576. # if defined(NID_aes_256_cfb128) && ! defined (NID_aes_256_cfb)
  577. # define NID_aes_256_cfb NID_aes_256_cfb128
  578. # endif
  579. # if defined(NID_aes_256_ofb128) && ! defined (NID_aes_256_ofb)
  580. # define NID_aes_256_ofb NID_aes_256_ofb128
  581. # endif
  582. /*
  583. * List of supported ciphers.
  584. */ static int padlock_cipher_nids[] = {
  585. NID_aes_128_ecb,
  586. NID_aes_128_cbc,
  587. NID_aes_128_cfb,
  588. NID_aes_128_ofb,
  589. NID_aes_192_ecb,
  590. NID_aes_192_cbc,
  591. NID_aes_192_cfb,
  592. NID_aes_192_ofb,
  593. NID_aes_256_ecb,
  594. NID_aes_256_cbc,
  595. NID_aes_256_cfb,
  596. NID_aes_256_ofb,
  597. };
  598. static int padlock_cipher_nids_num = (sizeof(padlock_cipher_nids) /
  599. sizeof(padlock_cipher_nids[0]));
  600. /* Function prototypes ... */
  601. static int padlock_aes_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  602. const unsigned char *iv, int enc);
  603. static int padlock_aes_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  604. const unsigned char *in, size_t nbytes);
  605. # define NEAREST_ALIGNED(ptr) ( (unsigned char *)(ptr) + \
  606. ( (0x10 - ((size_t)(ptr) & 0x0F)) & 0x0F ) )
  607. # define ALIGNED_CIPHER_DATA(ctx) ((struct padlock_cipher_data *)\
  608. NEAREST_ALIGNED(ctx->cipher_data))
  609. # define EVP_CIPHER_block_size_ECB AES_BLOCK_SIZE
  610. # define EVP_CIPHER_block_size_CBC AES_BLOCK_SIZE
  611. # define EVP_CIPHER_block_size_OFB 1
  612. # define EVP_CIPHER_block_size_CFB 1
  613. /*
  614. * Declaring so many ciphers by hand would be a pain. Instead introduce a bit
  615. * of preprocessor magic :-)
  616. */
  617. # define DECLARE_AES_EVP(ksize,lmode,umode) \
  618. static const EVP_CIPHER padlock_aes_##ksize##_##lmode = { \
  619. NID_aes_##ksize##_##lmode, \
  620. EVP_CIPHER_block_size_##umode, \
  621. AES_KEY_SIZE_##ksize, \
  622. AES_BLOCK_SIZE, \
  623. 0 | EVP_CIPH_##umode##_MODE, \
  624. padlock_aes_init_key, \
  625. padlock_aes_cipher, \
  626. NULL, \
  627. sizeof(struct padlock_cipher_data) + 16, \
  628. EVP_CIPHER_set_asn1_iv, \
  629. EVP_CIPHER_get_asn1_iv, \
  630. NULL, \
  631. NULL \
  632. }
  633. DECLARE_AES_EVP(128, ecb, ECB);
  634. DECLARE_AES_EVP(128, cbc, CBC);
  635. DECLARE_AES_EVP(128, cfb, CFB);
  636. DECLARE_AES_EVP(128, ofb, OFB);
  637. DECLARE_AES_EVP(192, ecb, ECB);
  638. DECLARE_AES_EVP(192, cbc, CBC);
  639. DECLARE_AES_EVP(192, cfb, CFB);
  640. DECLARE_AES_EVP(192, ofb, OFB);
  641. DECLARE_AES_EVP(256, ecb, ECB);
  642. DECLARE_AES_EVP(256, cbc, CBC);
  643. DECLARE_AES_EVP(256, cfb, CFB);
  644. DECLARE_AES_EVP(256, ofb, OFB);
  645. static int
  646. padlock_ciphers(ENGINE *e, const EVP_CIPHER **cipher, const int **nids,
  647. int nid)
  648. {
  649. /* No specific cipher => return a list of supported nids ... */
  650. if (!cipher) {
  651. *nids = padlock_cipher_nids;
  652. return padlock_cipher_nids_num;
  653. }
  654. /* ... or the requested "cipher" otherwise */
  655. switch (nid) {
  656. case NID_aes_128_ecb:
  657. *cipher = &padlock_aes_128_ecb;
  658. break;
  659. case NID_aes_128_cbc:
  660. *cipher = &padlock_aes_128_cbc;
  661. break;
  662. case NID_aes_128_cfb:
  663. *cipher = &padlock_aes_128_cfb;
  664. break;
  665. case NID_aes_128_ofb:
  666. *cipher = &padlock_aes_128_ofb;
  667. break;
  668. case NID_aes_192_ecb:
  669. *cipher = &padlock_aes_192_ecb;
  670. break;
  671. case NID_aes_192_cbc:
  672. *cipher = &padlock_aes_192_cbc;
  673. break;
  674. case NID_aes_192_cfb:
  675. *cipher = &padlock_aes_192_cfb;
  676. break;
  677. case NID_aes_192_ofb:
  678. *cipher = &padlock_aes_192_ofb;
  679. break;
  680. case NID_aes_256_ecb:
  681. *cipher = &padlock_aes_256_ecb;
  682. break;
  683. case NID_aes_256_cbc:
  684. *cipher = &padlock_aes_256_cbc;
  685. break;
  686. case NID_aes_256_cfb:
  687. *cipher = &padlock_aes_256_cfb;
  688. break;
  689. case NID_aes_256_ofb:
  690. *cipher = &padlock_aes_256_ofb;
  691. break;
  692. default:
  693. /* Sorry, we don't support this NID */
  694. *cipher = NULL;
  695. return 0;
  696. }
  697. return 1;
  698. }
  699. /* Prepare the encryption key for PadLock usage */
  700. static int
  701. padlock_aes_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  702. const unsigned char *iv, int enc)
  703. {
  704. struct padlock_cipher_data *cdata;
  705. int key_len = EVP_CIPHER_CTX_key_length(ctx) * 8;
  706. if (key == NULL)
  707. return 0; /* ERROR */
  708. cdata = ALIGNED_CIPHER_DATA(ctx);
  709. memset(cdata, 0, sizeof(struct padlock_cipher_data));
  710. /* Prepare Control word. */
  711. if (EVP_CIPHER_CTX_mode(ctx) == EVP_CIPH_OFB_MODE)
  712. cdata->cword.b.encdec = 0;
  713. else
  714. cdata->cword.b.encdec = (ctx->encrypt == 0);
  715. cdata->cword.b.rounds = 10 + (key_len - 128) / 32;
  716. cdata->cword.b.ksize = (key_len - 128) / 64;
  717. switch (key_len) {
  718. case 128:
  719. /*
  720. * PadLock can generate an extended key for AES128 in hardware
  721. */
  722. memcpy(cdata->ks.rd_key, key, AES_KEY_SIZE_128);
  723. cdata->cword.b.keygen = 0;
  724. break;
  725. case 192:
  726. case 256:
  727. /*
  728. * Generate an extended AES key in software. Needed for AES192/AES256
  729. */
  730. /*
  731. * Well, the above applies to Stepping 8 CPUs and is listed as
  732. * hardware errata. They most likely will fix it at some point and
  733. * then a check for stepping would be due here.
  734. */
  735. if (EVP_CIPHER_CTX_mode(ctx) == EVP_CIPH_CFB_MODE ||
  736. EVP_CIPHER_CTX_mode(ctx) == EVP_CIPH_OFB_MODE || enc)
  737. AES_set_encrypt_key(key, key_len, &cdata->ks);
  738. else
  739. AES_set_decrypt_key(key, key_len, &cdata->ks);
  740. # ifndef AES_ASM
  741. /*
  742. * OpenSSL C functions use byte-swapped extended key.
  743. */
  744. padlock_bswapl(&cdata->ks);
  745. # endif
  746. cdata->cword.b.keygen = 1;
  747. break;
  748. default:
  749. /* ERROR */
  750. return 0;
  751. }
  752. /*
  753. * This is done to cover for cases when user reuses the
  754. * context for new key. The catch is that if we don't do
  755. * this, padlock_eas_cipher might proceed with old key...
  756. */
  757. padlock_reload_key();
  758. return 1;
  759. }
  760. /*-
  761. * Simplified version of padlock_aes_cipher() used when
  762. * 1) both input and output buffers are at aligned addresses.
  763. * or when
  764. * 2) running on a newer CPU that doesn't require aligned buffers.
  765. */
  766. static int
  767. padlock_aes_cipher_omnivorous(EVP_CIPHER_CTX *ctx, unsigned char *out_arg,
  768. const unsigned char *in_arg, size_t nbytes)
  769. {
  770. struct padlock_cipher_data *cdata;
  771. void *iv;
  772. cdata = ALIGNED_CIPHER_DATA(ctx);
  773. padlock_verify_context(cdata);
  774. switch (EVP_CIPHER_CTX_mode(ctx)) {
  775. case EVP_CIPH_ECB_MODE:
  776. padlock_xcrypt_ecb(nbytes / AES_BLOCK_SIZE, cdata, out_arg, in_arg);
  777. break;
  778. case EVP_CIPH_CBC_MODE:
  779. memcpy(cdata->iv, ctx->iv, AES_BLOCK_SIZE);
  780. iv = padlock_xcrypt_cbc(nbytes / AES_BLOCK_SIZE, cdata, out_arg,
  781. in_arg);
  782. memcpy(ctx->iv, iv, AES_BLOCK_SIZE);
  783. break;
  784. case EVP_CIPH_CFB_MODE:
  785. memcpy(cdata->iv, ctx->iv, AES_BLOCK_SIZE);
  786. iv = padlock_xcrypt_cfb(nbytes / AES_BLOCK_SIZE, cdata, out_arg,
  787. in_arg);
  788. memcpy(ctx->iv, iv, AES_BLOCK_SIZE);
  789. break;
  790. case EVP_CIPH_OFB_MODE:
  791. memcpy(cdata->iv, ctx->iv, AES_BLOCK_SIZE);
  792. padlock_xcrypt_ofb(nbytes / AES_BLOCK_SIZE, cdata, out_arg, in_arg);
  793. memcpy(ctx->iv, cdata->iv, AES_BLOCK_SIZE);
  794. break;
  795. default:
  796. return 0;
  797. }
  798. memset(cdata->iv, 0, AES_BLOCK_SIZE);
  799. return 1;
  800. }
  801. # ifndef PADLOCK_CHUNK
  802. # define PADLOCK_CHUNK 512 /* Must be a power of 2 larger than 16 */
  803. # endif
  804. # if PADLOCK_CHUNK<16 || PADLOCK_CHUNK&(PADLOCK_CHUNK-1)
  805. # error "insane PADLOCK_CHUNK..."
  806. # endif
  807. /*
  808. * Re-align the arguments to 16-Bytes boundaries and run the encryption
  809. * function itself. This function is not AES-specific.
  810. */
  811. static int
  812. padlock_aes_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out_arg,
  813. const unsigned char *in_arg, size_t nbytes)
  814. {
  815. struct padlock_cipher_data *cdata;
  816. const void *inp;
  817. unsigned char *out;
  818. void *iv;
  819. int inp_misaligned, out_misaligned, realign_in_loop;
  820. size_t chunk, allocated = 0;
  821. /*
  822. * ctx->num is maintained in byte-oriented modes, such as CFB and OFB...
  823. */
  824. if ((chunk = ctx->num)) { /* borrow chunk variable */
  825. unsigned char *ivp = ctx->iv;
  826. switch (EVP_CIPHER_CTX_mode(ctx)) {
  827. case EVP_CIPH_CFB_MODE:
  828. if (chunk >= AES_BLOCK_SIZE)
  829. return 0; /* bogus value */
  830. if (ctx->encrypt)
  831. while (chunk < AES_BLOCK_SIZE && nbytes != 0) {
  832. ivp[chunk] = *(out_arg++) = *(in_arg++) ^ ivp[chunk];
  833. chunk++, nbytes--;
  834. } else
  835. while (chunk < AES_BLOCK_SIZE && nbytes != 0) {
  836. unsigned char c = *(in_arg++);
  837. *(out_arg++) = c ^ ivp[chunk];
  838. ivp[chunk++] = c, nbytes--;
  839. }
  840. ctx->num = chunk % AES_BLOCK_SIZE;
  841. break;
  842. case EVP_CIPH_OFB_MODE:
  843. if (chunk >= AES_BLOCK_SIZE)
  844. return 0; /* bogus value */
  845. while (chunk < AES_BLOCK_SIZE && nbytes != 0) {
  846. *(out_arg++) = *(in_arg++) ^ ivp[chunk];
  847. chunk++, nbytes--;
  848. }
  849. ctx->num = chunk % AES_BLOCK_SIZE;
  850. break;
  851. }
  852. }
  853. if (nbytes == 0)
  854. return 1;
  855. # if 0
  856. if (nbytes % AES_BLOCK_SIZE)
  857. return 0; /* are we expected to do tail processing? */
  858. # else
  859. /*
  860. * nbytes is always multiple of AES_BLOCK_SIZE in ECB and CBC modes and
  861. * arbitrary value in byte-oriented modes, such as CFB and OFB...
  862. */
  863. # endif
  864. /*
  865. * VIA promises CPUs that won't require alignment in the future. For now
  866. * padlock_aes_align_required is initialized to 1 and the condition is
  867. * never met...
  868. */
  869. /*
  870. * C7 core is capable to manage unaligned input in non-ECB[!] mode, but
  871. * performance penalties appear to be approximately same as for software
  872. * alignment below or ~3x. They promise to improve it in the future, but
  873. * for now we can just as well pretend that it can only handle aligned
  874. * input...
  875. */
  876. if (!padlock_aes_align_required && (nbytes % AES_BLOCK_SIZE) == 0)
  877. return padlock_aes_cipher_omnivorous(ctx, out_arg, in_arg, nbytes);
  878. inp_misaligned = (((size_t)in_arg) & 0x0F);
  879. out_misaligned = (((size_t)out_arg) & 0x0F);
  880. /*
  881. * Note that even if output is aligned and input not, I still prefer to
  882. * loop instead of copy the whole input and then encrypt in one stroke.
  883. * This is done in order to improve L1 cache utilization...
  884. */
  885. realign_in_loop = out_misaligned | inp_misaligned;
  886. if (!realign_in_loop && (nbytes % AES_BLOCK_SIZE) == 0)
  887. return padlock_aes_cipher_omnivorous(ctx, out_arg, in_arg, nbytes);
  888. /* this takes one "if" out of the loops */
  889. chunk = nbytes;
  890. chunk %= PADLOCK_CHUNK;
  891. if (chunk == 0)
  892. chunk = PADLOCK_CHUNK;
  893. if (out_misaligned) {
  894. /* optmize for small input */
  895. allocated = (chunk < nbytes ? PADLOCK_CHUNK : nbytes);
  896. out = alloca(0x10 + allocated);
  897. out = NEAREST_ALIGNED(out);
  898. } else
  899. out = out_arg;
  900. cdata = ALIGNED_CIPHER_DATA(ctx);
  901. padlock_verify_context(cdata);
  902. switch (EVP_CIPHER_CTX_mode(ctx)) {
  903. case EVP_CIPH_ECB_MODE:
  904. do {
  905. if (inp_misaligned)
  906. inp = padlock_memcpy(out, in_arg, chunk);
  907. else
  908. inp = in_arg;
  909. in_arg += chunk;
  910. padlock_xcrypt_ecb(chunk / AES_BLOCK_SIZE, cdata, out, inp);
  911. if (out_misaligned)
  912. out_arg = padlock_memcpy(out_arg, out, chunk) + chunk;
  913. else
  914. out = out_arg += chunk;
  915. nbytes -= chunk;
  916. chunk = PADLOCK_CHUNK;
  917. } while (nbytes);
  918. break;
  919. case EVP_CIPH_CBC_MODE:
  920. memcpy(cdata->iv, ctx->iv, AES_BLOCK_SIZE);
  921. goto cbc_shortcut;
  922. do {
  923. if (iv != cdata->iv)
  924. memcpy(cdata->iv, iv, AES_BLOCK_SIZE);
  925. chunk = PADLOCK_CHUNK;
  926. cbc_shortcut: /* optimize for small input */
  927. if (inp_misaligned)
  928. inp = padlock_memcpy(out, in_arg, chunk);
  929. else
  930. inp = in_arg;
  931. in_arg += chunk;
  932. iv = padlock_xcrypt_cbc(chunk / AES_BLOCK_SIZE, cdata, out, inp);
  933. if (out_misaligned)
  934. out_arg = padlock_memcpy(out_arg, out, chunk) + chunk;
  935. else
  936. out = out_arg += chunk;
  937. } while (nbytes -= chunk);
  938. memcpy(ctx->iv, iv, AES_BLOCK_SIZE);
  939. break;
  940. case EVP_CIPH_CFB_MODE:
  941. memcpy(iv = cdata->iv, ctx->iv, AES_BLOCK_SIZE);
  942. chunk &= ~(AES_BLOCK_SIZE - 1);
  943. if (chunk)
  944. goto cfb_shortcut;
  945. else
  946. goto cfb_skiploop;
  947. do {
  948. if (iv != cdata->iv)
  949. memcpy(cdata->iv, iv, AES_BLOCK_SIZE);
  950. chunk = PADLOCK_CHUNK;
  951. cfb_shortcut: /* optimize for small input */
  952. if (inp_misaligned)
  953. inp = padlock_memcpy(out, in_arg, chunk);
  954. else
  955. inp = in_arg;
  956. in_arg += chunk;
  957. iv = padlock_xcrypt_cfb(chunk / AES_BLOCK_SIZE, cdata, out, inp);
  958. if (out_misaligned)
  959. out_arg = padlock_memcpy(out_arg, out, chunk) + chunk;
  960. else
  961. out = out_arg += chunk;
  962. nbytes -= chunk;
  963. } while (nbytes >= AES_BLOCK_SIZE);
  964. cfb_skiploop:
  965. if (nbytes) {
  966. unsigned char *ivp = cdata->iv;
  967. if (iv != ivp) {
  968. memcpy(ivp, iv, AES_BLOCK_SIZE);
  969. iv = ivp;
  970. }
  971. ctx->num = nbytes;
  972. if (cdata->cword.b.encdec) {
  973. cdata->cword.b.encdec = 0;
  974. padlock_reload_key();
  975. padlock_xcrypt_ecb(1, cdata, ivp, ivp);
  976. cdata->cword.b.encdec = 1;
  977. padlock_reload_key();
  978. while (nbytes) {
  979. unsigned char c = *(in_arg++);
  980. *(out_arg++) = c ^ *ivp;
  981. *(ivp++) = c, nbytes--;
  982. }
  983. } else {
  984. padlock_reload_key();
  985. padlock_xcrypt_ecb(1, cdata, ivp, ivp);
  986. padlock_reload_key();
  987. while (nbytes) {
  988. *ivp = *(out_arg++) = *(in_arg++) ^ *ivp;
  989. ivp++, nbytes--;
  990. }
  991. }
  992. }
  993. memcpy(ctx->iv, iv, AES_BLOCK_SIZE);
  994. break;
  995. case EVP_CIPH_OFB_MODE:
  996. memcpy(cdata->iv, ctx->iv, AES_BLOCK_SIZE);
  997. chunk &= ~(AES_BLOCK_SIZE - 1);
  998. if (chunk)
  999. do {
  1000. if (inp_misaligned)
  1001. inp = padlock_memcpy(out, in_arg, chunk);
  1002. else
  1003. inp = in_arg;
  1004. in_arg += chunk;
  1005. padlock_xcrypt_ofb(chunk / AES_BLOCK_SIZE, cdata, out, inp);
  1006. if (out_misaligned)
  1007. out_arg = padlock_memcpy(out_arg, out, chunk) + chunk;
  1008. else
  1009. out = out_arg += chunk;
  1010. nbytes -= chunk;
  1011. chunk = PADLOCK_CHUNK;
  1012. } while (nbytes >= AES_BLOCK_SIZE);
  1013. if (nbytes) {
  1014. unsigned char *ivp = cdata->iv;
  1015. ctx->num = nbytes;
  1016. padlock_reload_key(); /* empirically found */
  1017. padlock_xcrypt_ecb(1, cdata, ivp, ivp);
  1018. padlock_reload_key(); /* empirically found */
  1019. while (nbytes) {
  1020. *(out_arg++) = *(in_arg++) ^ *ivp;
  1021. ivp++, nbytes--;
  1022. }
  1023. }
  1024. memcpy(ctx->iv, cdata->iv, AES_BLOCK_SIZE);
  1025. break;
  1026. default:
  1027. return 0;
  1028. }
  1029. /* Clean the realign buffer if it was used */
  1030. if (out_misaligned) {
  1031. volatile unsigned long *p = (void *)out;
  1032. size_t n = allocated / sizeof(*p);
  1033. while (n--)
  1034. *p++ = 0;
  1035. }
  1036. memset(cdata->iv, 0, AES_BLOCK_SIZE);
  1037. return 1;
  1038. }
  1039. # endif /* OPENSSL_NO_AES */
  1040. /* ===== Random Number Generator ===== */
  1041. /*
  1042. * This code is not engaged. The reason is that it does not comply
  1043. * with recommendations for VIA RNG usage for secure applications
  1044. * (posted at http://www.via.com.tw/en/viac3/c3.jsp) nor does it
  1045. * provide meaningful error control...
  1046. */
  1047. /*
  1048. * Wrapper that provides an interface between the API and the raw PadLock
  1049. * RNG
  1050. */
  1051. static int padlock_rand_bytes(unsigned char *output, int count)
  1052. {
  1053. unsigned int eax, buf;
  1054. while (count >= 8) {
  1055. eax = padlock_xstore(output, 0);
  1056. if (!(eax & (1 << 6)))
  1057. return 0; /* RNG disabled */
  1058. /* this ---vv--- covers DC bias, Raw Bits and String Filter */
  1059. if (eax & (0x1F << 10))
  1060. return 0;
  1061. if ((eax & 0x1F) == 0)
  1062. continue; /* no data, retry... */
  1063. if ((eax & 0x1F) != 8)
  1064. return 0; /* fatal failure... */
  1065. output += 8;
  1066. count -= 8;
  1067. }
  1068. while (count > 0) {
  1069. eax = padlock_xstore(&buf, 3);
  1070. if (!(eax & (1 << 6)))
  1071. return 0; /* RNG disabled */
  1072. /* this ---vv--- covers DC bias, Raw Bits and String Filter */
  1073. if (eax & (0x1F << 10))
  1074. return 0;
  1075. if ((eax & 0x1F) == 0)
  1076. continue; /* no data, retry... */
  1077. if ((eax & 0x1F) != 1)
  1078. return 0; /* fatal failure... */
  1079. *output++ = (unsigned char)buf;
  1080. count--;
  1081. }
  1082. *(volatile unsigned int *)&buf = 0;
  1083. return 1;
  1084. }
  1085. /* Dummy but necessary function */
  1086. static int padlock_rand_status(void)
  1087. {
  1088. return 1;
  1089. }
  1090. /* Prepare structure for registration */
  1091. static RAND_METHOD padlock_rand = {
  1092. NULL, /* seed */
  1093. padlock_rand_bytes, /* bytes */
  1094. NULL, /* cleanup */
  1095. NULL, /* add */
  1096. padlock_rand_bytes, /* pseudorand */
  1097. padlock_rand_status, /* rand status */
  1098. };
  1099. # else /* !COMPILE_HW_PADLOCK */
  1100. # ifndef OPENSSL_NO_DYNAMIC_ENGINE
  1101. OPENSSL_EXPORT
  1102. int bind_engine(ENGINE *e, const char *id, const dynamic_fns *fns);
  1103. OPENSSL_EXPORT
  1104. int bind_engine(ENGINE *e, const char *id, const dynamic_fns *fns)
  1105. {
  1106. return 0;
  1107. }
  1108. IMPLEMENT_DYNAMIC_CHECK_FN()
  1109. # endif
  1110. # endif /* COMPILE_HW_PADLOCK */
  1111. # endif /* !OPENSSL_NO_HW_PADLOCK */
  1112. #endif /* !OPENSSL_NO_HW */