e_padlock.c 34 KB

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