2
0

e_padlock.c 38 KB

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