eng_padlock.c 31 KB

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