2
0

eng_padlock.c 37 KB

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