pcl_gcm128.c 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477
  1. /*
  2. * Copyright (C) 2011-2018 Intel Corporation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in
  12. * the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Intel Corporation nor the names of its
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. */
  31. /* ====================================================================
  32. * Copyright (c) 1998-2017 The OpenSSL Project. All rights reserved.
  33. *
  34. * Redistribution and use in source and binary forms, with or without
  35. * modification, are permitted provided that the following conditions
  36. * are met:
  37. *
  38. * 1. Redistributions of source code must retain the above copyright
  39. * notice, this list of conditions and the following disclaimer.
  40. *
  41. * 2. Redistributions in binary form must reproduce the above copyright
  42. * notice, this list of conditions and the following disclaimer in
  43. * the documentation and/or other materials provided with the
  44. * distribution.
  45. *
  46. * 3. All advertising materials mentioning features or use of this
  47. * software must display the following acknowledgment:
  48. * "This product includes software developed by the OpenSSL Project
  49. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  50. *
  51. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  52. * endorse or promote products derived from this software without
  53. * prior written permission. For written permission, please contact
  54. * openssl-core@openssl.org.
  55. *
  56. * 5. Products derived from this software may not be called "OpenSSL"
  57. * nor may "OpenSSL" appear in their names without prior written
  58. * permission of the OpenSSL Project.
  59. *
  60. * 6. Redistributions of any form whatsoever must retain the following
  61. * acknowledgment:
  62. * "This product includes software developed by the OpenSSL Project
  63. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  64. *
  65. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  66. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  67. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  68. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  69. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  70. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  71. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  72. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  73. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  74. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  75. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  76. * OF THE POSSIBILITY OF SUCH DAMAGE.
  77. * ====================================================================
  78. *
  79. * This product includes cryptographic software written by Eric Young
  80. * (eay@cryptsoft.com). This product includes software written by Tim
  81. * Hudson (tjh@cryptsoft.com).
  82. *
  83. */
  84. /* Content from openssl-1.1.0e/crypto/modes/gcm128.c */
  85. #include <openssl/aes.h>
  86. #include <openssl/sha.h>
  87. #include <openssl/modes.h>
  88. #include <sgx_tseal.h>
  89. #include <pcl_common.h>
  90. #include <pcl_internal.h>
  91. #include <pcl_crypto_internal.h>
  92. #include <pcl_modes_lcl.h>
  93. /* PCL UNUSED START *
  94. #if defined(BSWAP4) && defined(STRICT_ALIGNMENT)
  95. /* redefine, because alignment is ensured * /
  96. # undef GETU32
  97. # define GETU32(p) BSWAP4(*(const u32 *)(p))
  98. # undef PUTU32
  99. # define PUTU32(p,v) *(u32 *)(p) = BSWAP4(v)
  100. #endif
  101. #define PACK(s) ((size_t)(s)<<(sizeof(size_t)*8-16))
  102. #define REDUCE1BIT(V) do { \
  103. if (sizeof(size_t)==8) { \
  104. u64 T = U64(0xe100000000000000) & (0-(V.lo&1)); \
  105. V.lo = (V.hi<<63)|(V.lo>>1); \
  106. V.hi = (V.hi>>1 )^T; \
  107. } \
  108. else { \
  109. u32 T = 0xe1000000U & (0-(u32)(V.lo&1)); \
  110. V.lo = (V.hi<<63)|(V.lo>>1); \
  111. V.hi = (V.hi>>1 )^((u64)T<<32); \
  112. } \
  113. } while(0)
  114. /*-
  115. * Even though permitted values for TABLE_BITS are 8, 4 and 1, it should
  116. * never be set to 8. 8 is effectively reserved for testing purposes.
  117. * TABLE_BITS>1 are lookup-table-driven implementations referred to as
  118. * "Shoup's" in GCM specification. In other words OpenSSL does not cover
  119. * whole spectrum of possible table driven implementations. Why? In
  120. * non-"Shoup's" case memory access pattern is segmented in such manner,
  121. * that it's trivial to see that cache timing information can reveal
  122. * fair portion of intermediate hash value. Given that ciphertext is
  123. * always available to attacker, it's possible for him to attempt to
  124. * deduce secret parameter H and if successful, tamper with messages
  125. * [which is nothing but trivial in CTR mode]. In "Shoup's" case it's
  126. * not as trivial, but there is no reason to believe that it's resistant
  127. * to cache-timing attack. And the thing about "8-bit" implementation is
  128. * that it consumes 16 (sixteen) times more memory, 4KB per individual
  129. * key + 1KB shared. Well, on pros side it should be twice as fast as
  130. * "4-bit" version. And for gcc-generated x86[_64] code, "8-bit" version
  131. * was observed to run ~75% faster, closer to 100% for commercial
  132. * compilers... Yet "4-bit" procedure is preferred, because it's
  133. * believed to provide better security-performance balance and adequate
  134. * all-round performance. "All-round" refers to things like:
  135. *
  136. * - shorter setup time effectively improves overall timing for
  137. * handling short messages;
  138. * - larger table allocation can become unbearable because of VM
  139. * subsystem penalties (for example on Windows large enough free
  140. * results in VM working set trimming, meaning that consequent
  141. * malloc would immediately incur working set expansion);
  142. * - larger table has larger cache footprint, which can affect
  143. * performance of other code paths (not necessarily even from same
  144. * thread in Hyper-Threading world);
  145. *
  146. * Value of 1 is not appropriate for performance reasons.
  147. * /
  148. #if TABLE_BITS==8
  149. static void gcm_init_8bit(u128 Htable[256], u64 H[2])
  150. {
  151. int i, j;
  152. u128 V;
  153. Htable[0].hi = 0;
  154. Htable[0].lo = 0;
  155. V.hi = H[0];
  156. V.lo = H[1];
  157. for (Htable[128] = V, i = 64; i > 0; i >>= 1) {
  158. REDUCE1BIT(V);
  159. Htable[i] = V;
  160. }
  161. for (i = 2; i < 256; i <<= 1) {
  162. u128 *Hi = Htable + i, H0 = *Hi;
  163. for (j = 1; j < i; ++j) {
  164. Hi[j].hi = H0.hi ^ Htable[j].hi;
  165. Hi[j].lo = H0.lo ^ Htable[j].lo;
  166. }
  167. }
  168. }
  169. static void gcm_gmult_8bit(u64 Xi[2], const u128 Htable[256])
  170. {
  171. u128 Z = { 0, 0 };
  172. const u8 *xi = (const u8 *)Xi + 15;
  173. size_t rem, n = *xi;
  174. const union {
  175. long one;
  176. char little;
  177. } is_endian = { 1 };
  178. static const size_t rem_8bit[256] = {
  179. PACK(0x0000), PACK(0x01C2), PACK(0x0384), PACK(0x0246),
  180. PACK(0x0708), PACK(0x06CA), PACK(0x048C), PACK(0x054E),
  181. PACK(0x0E10), PACK(0x0FD2), PACK(0x0D94), PACK(0x0C56),
  182. PACK(0x0918), PACK(0x08DA), PACK(0x0A9C), PACK(0x0B5E),
  183. PACK(0x1C20), PACK(0x1DE2), PACK(0x1FA4), PACK(0x1E66),
  184. PACK(0x1B28), PACK(0x1AEA), PACK(0x18AC), PACK(0x196E),
  185. PACK(0x1230), PACK(0x13F2), PACK(0x11B4), PACK(0x1076),
  186. PACK(0x1538), PACK(0x14FA), PACK(0x16BC), PACK(0x177E),
  187. PACK(0x3840), PACK(0x3982), PACK(0x3BC4), PACK(0x3A06),
  188. PACK(0x3F48), PACK(0x3E8A), PACK(0x3CCC), PACK(0x3D0E),
  189. PACK(0x3650), PACK(0x3792), PACK(0x35D4), PACK(0x3416),
  190. PACK(0x3158), PACK(0x309A), PACK(0x32DC), PACK(0x331E),
  191. PACK(0x2460), PACK(0x25A2), PACK(0x27E4), PACK(0x2626),
  192. PACK(0x2368), PACK(0x22AA), PACK(0x20EC), PACK(0x212E),
  193. PACK(0x2A70), PACK(0x2BB2), PACK(0x29F4), PACK(0x2836),
  194. PACK(0x2D78), PACK(0x2CBA), PACK(0x2EFC), PACK(0x2F3E),
  195. PACK(0x7080), PACK(0x7142), PACK(0x7304), PACK(0x72C6),
  196. PACK(0x7788), PACK(0x764A), PACK(0x740C), PACK(0x75CE),
  197. PACK(0x7E90), PACK(0x7F52), PACK(0x7D14), PACK(0x7CD6),
  198. PACK(0x7998), PACK(0x785A), PACK(0x7A1C), PACK(0x7BDE),
  199. PACK(0x6CA0), PACK(0x6D62), PACK(0x6F24), PACK(0x6EE6),
  200. PACK(0x6BA8), PACK(0x6A6A), PACK(0x682C), PACK(0x69EE),
  201. PACK(0x62B0), PACK(0x6372), PACK(0x6134), PACK(0x60F6),
  202. PACK(0x65B8), PACK(0x647A), PACK(0x663C), PACK(0x67FE),
  203. PACK(0x48C0), PACK(0x4902), PACK(0x4B44), PACK(0x4A86),
  204. PACK(0x4FC8), PACK(0x4E0A), PACK(0x4C4C), PACK(0x4D8E),
  205. PACK(0x46D0), PACK(0x4712), PACK(0x4554), PACK(0x4496),
  206. PACK(0x41D8), PACK(0x401A), PACK(0x425C), PACK(0x439E),
  207. PACK(0x54E0), PACK(0x5522), PACK(0x5764), PACK(0x56A6),
  208. PACK(0x53E8), PACK(0x522A), PACK(0x506C), PACK(0x51AE),
  209. PACK(0x5AF0), PACK(0x5B32), PACK(0x5974), PACK(0x58B6),
  210. PACK(0x5DF8), PACK(0x5C3A), PACK(0x5E7C), PACK(0x5FBE),
  211. PACK(0xE100), PACK(0xE0C2), PACK(0xE284), PACK(0xE346),
  212. PACK(0xE608), PACK(0xE7CA), PACK(0xE58C), PACK(0xE44E),
  213. PACK(0xEF10), PACK(0xEED2), PACK(0xEC94), PACK(0xED56),
  214. PACK(0xE818), PACK(0xE9DA), PACK(0xEB9C), PACK(0xEA5E),
  215. PACK(0xFD20), PACK(0xFCE2), PACK(0xFEA4), PACK(0xFF66),
  216. PACK(0xFA28), PACK(0xFBEA), PACK(0xF9AC), PACK(0xF86E),
  217. PACK(0xF330), PACK(0xF2F2), PACK(0xF0B4), PACK(0xF176),
  218. PACK(0xF438), PACK(0xF5FA), PACK(0xF7BC), PACK(0xF67E),
  219. PACK(0xD940), PACK(0xD882), PACK(0xDAC4), PACK(0xDB06),
  220. PACK(0xDE48), PACK(0xDF8A), PACK(0xDDCC), PACK(0xDC0E),
  221. PACK(0xD750), PACK(0xD692), PACK(0xD4D4), PACK(0xD516),
  222. PACK(0xD058), PACK(0xD19A), PACK(0xD3DC), PACK(0xD21E),
  223. PACK(0xC560), PACK(0xC4A2), PACK(0xC6E4), PACK(0xC726),
  224. PACK(0xC268), PACK(0xC3AA), PACK(0xC1EC), PACK(0xC02E),
  225. PACK(0xCB70), PACK(0xCAB2), PACK(0xC8F4), PACK(0xC936),
  226. PACK(0xCC78), PACK(0xCDBA), PACK(0xCFFC), PACK(0xCE3E),
  227. PACK(0x9180), PACK(0x9042), PACK(0x9204), PACK(0x93C6),
  228. PACK(0x9688), PACK(0x974A), PACK(0x950C), PACK(0x94CE),
  229. PACK(0x9F90), PACK(0x9E52), PACK(0x9C14), PACK(0x9DD6),
  230. PACK(0x9898), PACK(0x995A), PACK(0x9B1C), PACK(0x9ADE),
  231. PACK(0x8DA0), PACK(0x8C62), PACK(0x8E24), PACK(0x8FE6),
  232. PACK(0x8AA8), PACK(0x8B6A), PACK(0x892C), PACK(0x88EE),
  233. PACK(0x83B0), PACK(0x8272), PACK(0x8034), PACK(0x81F6),
  234. PACK(0x84B8), PACK(0x857A), PACK(0x873C), PACK(0x86FE),
  235. PACK(0xA9C0), PACK(0xA802), PACK(0xAA44), PACK(0xAB86),
  236. PACK(0xAEC8), PACK(0xAF0A), PACK(0xAD4C), PACK(0xAC8E),
  237. PACK(0xA7D0), PACK(0xA612), PACK(0xA454), PACK(0xA596),
  238. PACK(0xA0D8), PACK(0xA11A), PACK(0xA35C), PACK(0xA29E),
  239. PACK(0xB5E0), PACK(0xB422), PACK(0xB664), PACK(0xB7A6),
  240. PACK(0xB2E8), PACK(0xB32A), PACK(0xB16C), PACK(0xB0AE),
  241. PACK(0xBBF0), PACK(0xBA32), PACK(0xB874), PACK(0xB9B6),
  242. PACK(0xBCF8), PACK(0xBD3A), PACK(0xBF7C), PACK(0xBEBE)
  243. };
  244. while (1) {
  245. Z.hi ^= Htable[n].hi;
  246. Z.lo ^= Htable[n].lo;
  247. if ((u8 *)Xi == xi)
  248. break;
  249. n = *(--xi);
  250. rem = (size_t)Z.lo & 0xff;
  251. Z.lo = (Z.hi << 56) | (Z.lo >> 8);
  252. Z.hi = (Z.hi >> 8);
  253. if (sizeof(size_t) == 8)
  254. Z.hi ^= rem_8bit[rem];
  255. else
  256. Z.hi ^= (u64)rem_8bit[rem] << 32;
  257. }
  258. if (is_endian.little) {
  259. # ifdef BSWAP8
  260. Xi[0] = BSWAP8(Z.hi);
  261. Xi[1] = BSWAP8(Z.lo);
  262. # else
  263. u8 *p = (u8 *)Xi;
  264. u32 v;
  265. v = (u32)(Z.hi >> 32);
  266. PUTU32(p, v);
  267. v = (u32)(Z.hi);
  268. PUTU32(p + 4, v);
  269. v = (u32)(Z.lo >> 32);
  270. PUTU32(p + 8, v);
  271. v = (u32)(Z.lo);
  272. PUTU32(p + 12, v);
  273. # endif
  274. } else {
  275. Xi[0] = Z.hi;
  276. Xi[1] = Z.lo;
  277. }
  278. }
  279. # define GCM_MUL(ctx,Xi) gcm_gmult_8bit(ctx->Xi.u,ctx->Htable)
  280. #elif TABLE_BITS==4
  281. static void pcl_gcm_init_4bit(u128 Htable[16], u64 H[2])
  282. {
  283. u128 V;
  284. # if defined(OPENSSL_SMALL_FOOTPRINT)
  285. int i;
  286. # endif
  287. Htable[0].hi = 0;
  288. Htable[0].lo = 0;
  289. V.hi = H[0];
  290. V.lo = H[1];
  291. # if defined(OPENSSL_SMALL_FOOTPRINT)
  292. for (Htable[8] = V, i = 4; i > 0; i >>= 1) {
  293. REDUCE1BIT(V);
  294. Htable[i] = V;
  295. }
  296. for (i = 2; i < 16; i <<= 1) {
  297. u128 *Hi = Htable + i;
  298. int j;
  299. for (V = *Hi, j = 1; j < i; ++j) {
  300. Hi[j].hi = V.hi ^ Htable[j].hi;
  301. Hi[j].lo = V.lo ^ Htable[j].lo;
  302. }
  303. }
  304. # else
  305. Htable[8] = V;
  306. REDUCE1BIT(V);
  307. Htable[4] = V;
  308. REDUCE1BIT(V);
  309. Htable[2] = V;
  310. REDUCE1BIT(V);
  311. Htable[1] = V;
  312. Htable[3].hi = V.hi ^ Htable[2].hi, Htable[3].lo = V.lo ^ Htable[2].lo;
  313. V = Htable[4];
  314. Htable[5].hi = V.hi ^ Htable[1].hi, Htable[5].lo = V.lo ^ Htable[1].lo;
  315. Htable[6].hi = V.hi ^ Htable[2].hi, Htable[6].lo = V.lo ^ Htable[2].lo;
  316. Htable[7].hi = V.hi ^ Htable[3].hi, Htable[7].lo = V.lo ^ Htable[3].lo;
  317. V = Htable[8];
  318. Htable[9].hi = V.hi ^ Htable[1].hi, Htable[9].lo = V.lo ^ Htable[1].lo;
  319. Htable[10].hi = V.hi ^ Htable[2].hi, Htable[10].lo = V.lo ^ Htable[2].lo;
  320. Htable[11].hi = V.hi ^ Htable[3].hi, Htable[11].lo = V.lo ^ Htable[3].lo;
  321. Htable[12].hi = V.hi ^ Htable[4].hi, Htable[12].lo = V.lo ^ Htable[4].lo;
  322. Htable[13].hi = V.hi ^ Htable[5].hi, Htable[13].lo = V.lo ^ Htable[5].lo;
  323. Htable[14].hi = V.hi ^ Htable[6].hi, Htable[14].lo = V.lo ^ Htable[6].lo;
  324. Htable[15].hi = V.hi ^ Htable[7].hi, Htable[15].lo = V.lo ^ Htable[7].lo;
  325. # endif
  326. # if defined(GHASH_ASM) && (defined(__arm__) || defined(__arm))
  327. /*
  328. * ARM assembler expects specific dword order in Htable.
  329. * /
  330. {
  331. int j;
  332. const union {
  333. long one;
  334. char little;
  335. } is_endian = { 1 };
  336. if (is_endian.little)
  337. for (j = 0; j < 16; ++j) {
  338. V = Htable[j];
  339. Htable[j].hi = V.lo;
  340. Htable[j].lo = V.hi;
  341. } else
  342. for (j = 0; j < 16; ++j) {
  343. V = Htable[j];
  344. Htable[j].hi = V.lo << 32 | V.lo >> 32;
  345. Htable[j].lo = V.hi << 32 | V.hi >> 32;
  346. }
  347. }
  348. # endif
  349. }
  350. static const size_t rem_4bit[16] = {
  351. PACK(0x0000), PACK(0x1C20), PACK(0x3840), PACK(0x2460),
  352. PACK(0x7080), PACK(0x6CA0), PACK(0x48C0), PACK(0x54E0),
  353. PACK(0xE100), PACK(0xFD20), PACK(0xD940), PACK(0xC560),
  354. PACK(0x9180), PACK(0x8DA0), PACK(0xA9C0), PACK(0xB5E0)
  355. };
  356. static void pcl_gcm_gmult_4bit(u64 Xi[2], const u128 Htable[16])
  357. {
  358. u128 Z;
  359. int cnt = 15;
  360. size_t rem, nlo, nhi;
  361. const union {
  362. long one;
  363. char little;
  364. } is_endian = { 1 };
  365. nlo = ((const u8 *)Xi)[15];
  366. nhi = nlo >> 4;
  367. nlo &= 0xf;
  368. Z.hi = Htable[nlo].hi;
  369. Z.lo = Htable[nlo].lo;
  370. while (1) {
  371. rem = (size_t)Z.lo & 0xf;
  372. Z.lo = (Z.hi << 60) | (Z.lo >> 4);
  373. Z.hi = (Z.hi >> 4);
  374. if (sizeof(size_t) == 8)
  375. Z.hi ^= rem_4bit[rem];
  376. else
  377. Z.hi ^= (u64)rem_4bit[rem] << 32;
  378. Z.hi ^= Htable[nhi].hi;
  379. Z.lo ^= Htable[nhi].lo;
  380. if (--cnt < 0)
  381. break;
  382. nlo = ((const u8 *)Xi)[cnt];
  383. nhi = nlo >> 4;
  384. nlo &= 0xf;
  385. rem = (size_t)Z.lo & 0xf;
  386. Z.lo = (Z.hi << 60) | (Z.lo >> 4);
  387. Z.hi = (Z.hi >> 4);
  388. if (sizeof(size_t) == 8)
  389. Z.hi ^= rem_4bit[rem];
  390. else
  391. Z.hi ^= (u64)rem_4bit[rem] << 32;
  392. Z.hi ^= Htable[nlo].hi;
  393. Z.lo ^= Htable[nlo].lo;
  394. }
  395. if (is_endian.little) {
  396. # ifdef BSWAP8
  397. Xi[0] = BSWAP8(Z.hi);
  398. Xi[1] = BSWAP8(Z.lo);
  399. # else
  400. u8 *p = (u8 *)Xi;
  401. u32 v;
  402. v = (u32)(Z.hi >> 32);
  403. PUTU32(p, v);
  404. v = (u32)(Z.hi);
  405. PUTU32(p + 4, v);
  406. v = (u32)(Z.lo >> 32);
  407. PUTU32(p + 8, v);
  408. v = (u32)(Z.lo);
  409. PUTU32(p + 12, v);
  410. # endif
  411. } else {
  412. Xi[0] = Z.hi;
  413. Xi[1] = Z.lo;
  414. }
  415. }
  416. # if !defined(OPENSSL_SMALL_FOOTPRINT)
  417. /*
  418. * Streamed gcm_mult_4bit, see CRYPTO_gcm128_[en|de]crypt for
  419. * details... Compiler-generated code doesn't seem to give any
  420. * performance improvement, at least not on x86[_64]. It's here
  421. * mostly as reference and a placeholder for possible future
  422. * non-trivial optimization[s]...
  423. * /
  424. static void gcm_ghash_4bit(u64 Xi[2], const u128 Htable[16],
  425. const u8 *inp, size_t len)
  426. {
  427. u128 Z;
  428. int cnt;
  429. size_t rem, nlo, nhi;
  430. const union {
  431. long one;
  432. char little;
  433. } is_endian = { 1 };
  434. # if 1
  435. do {
  436. cnt = 15;
  437. nlo = ((const u8 *)Xi)[15];
  438. nlo ^= inp[15];
  439. nhi = nlo >> 4;
  440. nlo &= 0xf;
  441. Z.hi = Htable[nlo].hi;
  442. Z.lo = Htable[nlo].lo;
  443. while (1) {
  444. rem = (size_t)Z.lo & 0xf;
  445. Z.lo = (Z.hi << 60) | (Z.lo >> 4);
  446. Z.hi = (Z.hi >> 4);
  447. if (sizeof(size_t) == 8)
  448. Z.hi ^= rem_4bit[rem];
  449. else
  450. Z.hi ^= (u64)rem_4bit[rem] << 32;
  451. Z.hi ^= Htable[nhi].hi;
  452. Z.lo ^= Htable[nhi].lo;
  453. if (--cnt < 0)
  454. break;
  455. nlo = ((const u8 *)Xi)[cnt];
  456. nlo ^= inp[cnt];
  457. nhi = nlo >> 4;
  458. nlo &= 0xf;
  459. rem = (size_t)Z.lo & 0xf;
  460. Z.lo = (Z.hi << 60) | (Z.lo >> 4);
  461. Z.hi = (Z.hi >> 4);
  462. if (sizeof(size_t) == 8)
  463. Z.hi ^= rem_4bit[rem];
  464. else
  465. Z.hi ^= (u64)rem_4bit[rem] << 32;
  466. Z.hi ^= Htable[nlo].hi;
  467. Z.lo ^= Htable[nlo].lo;
  468. }
  469. # else
  470. /*
  471. * Extra 256+16 bytes per-key plus 512 bytes shared tables
  472. * [should] give ~50% improvement... One could have PACK()-ed
  473. * the rem_8bit even here, but the priority is to minimize
  474. * cache footprint...
  475. * /
  476. u128 Hshr4[16]; /* Htable shifted right by 4 bits * /
  477. u8 Hshl4[16]; /* Htable shifted left by 4 bits * /
  478. static const unsigned short rem_8bit[256] = {
  479. 0x0000, 0x01C2, 0x0384, 0x0246, 0x0708, 0x06CA, 0x048C, 0x054E,
  480. 0x0E10, 0x0FD2, 0x0D94, 0x0C56, 0x0918, 0x08DA, 0x0A9C, 0x0B5E,
  481. 0x1C20, 0x1DE2, 0x1FA4, 0x1E66, 0x1B28, 0x1AEA, 0x18AC, 0x196E,
  482. 0x1230, 0x13F2, 0x11B4, 0x1076, 0x1538, 0x14FA, 0x16BC, 0x177E,
  483. 0x3840, 0x3982, 0x3BC4, 0x3A06, 0x3F48, 0x3E8A, 0x3CCC, 0x3D0E,
  484. 0x3650, 0x3792, 0x35D4, 0x3416, 0x3158, 0x309A, 0x32DC, 0x331E,
  485. 0x2460, 0x25A2, 0x27E4, 0x2626, 0x2368, 0x22AA, 0x20EC, 0x212E,
  486. 0x2A70, 0x2BB2, 0x29F4, 0x2836, 0x2D78, 0x2CBA, 0x2EFC, 0x2F3E,
  487. 0x7080, 0x7142, 0x7304, 0x72C6, 0x7788, 0x764A, 0x740C, 0x75CE,
  488. 0x7E90, 0x7F52, 0x7D14, 0x7CD6, 0x7998, 0x785A, 0x7A1C, 0x7BDE,
  489. 0x6CA0, 0x6D62, 0x6F24, 0x6EE6, 0x6BA8, 0x6A6A, 0x682C, 0x69EE,
  490. 0x62B0, 0x6372, 0x6134, 0x60F6, 0x65B8, 0x647A, 0x663C, 0x67FE,
  491. 0x48C0, 0x4902, 0x4B44, 0x4A86, 0x4FC8, 0x4E0A, 0x4C4C, 0x4D8E,
  492. 0x46D0, 0x4712, 0x4554, 0x4496, 0x41D8, 0x401A, 0x425C, 0x439E,
  493. 0x54E0, 0x5522, 0x5764, 0x56A6, 0x53E8, 0x522A, 0x506C, 0x51AE,
  494. 0x5AF0, 0x5B32, 0x5974, 0x58B6, 0x5DF8, 0x5C3A, 0x5E7C, 0x5FBE,
  495. 0xE100, 0xE0C2, 0xE284, 0xE346, 0xE608, 0xE7CA, 0xE58C, 0xE44E,
  496. 0xEF10, 0xEED2, 0xEC94, 0xED56, 0xE818, 0xE9DA, 0xEB9C, 0xEA5E,
  497. 0xFD20, 0xFCE2, 0xFEA4, 0xFF66, 0xFA28, 0xFBEA, 0xF9AC, 0xF86E,
  498. 0xF330, 0xF2F2, 0xF0B4, 0xF176, 0xF438, 0xF5FA, 0xF7BC, 0xF67E,
  499. 0xD940, 0xD882, 0xDAC4, 0xDB06, 0xDE48, 0xDF8A, 0xDDCC, 0xDC0E,
  500. 0xD750, 0xD692, 0xD4D4, 0xD516, 0xD058, 0xD19A, 0xD3DC, 0xD21E,
  501. 0xC560, 0xC4A2, 0xC6E4, 0xC726, 0xC268, 0xC3AA, 0xC1EC, 0xC02E,
  502. 0xCB70, 0xCAB2, 0xC8F4, 0xC936, 0xCC78, 0xCDBA, 0xCFFC, 0xCE3E,
  503. 0x9180, 0x9042, 0x9204, 0x93C6, 0x9688, 0x974A, 0x950C, 0x94CE,
  504. 0x9F90, 0x9E52, 0x9C14, 0x9DD6, 0x9898, 0x995A, 0x9B1C, 0x9ADE,
  505. 0x8DA0, 0x8C62, 0x8E24, 0x8FE6, 0x8AA8, 0x8B6A, 0x892C, 0x88EE,
  506. 0x83B0, 0x8272, 0x8034, 0x81F6, 0x84B8, 0x857A, 0x873C, 0x86FE,
  507. 0xA9C0, 0xA802, 0xAA44, 0xAB86, 0xAEC8, 0xAF0A, 0xAD4C, 0xAC8E,
  508. 0xA7D0, 0xA612, 0xA454, 0xA596, 0xA0D8, 0xA11A, 0xA35C, 0xA29E,
  509. 0xB5E0, 0xB422, 0xB664, 0xB7A6, 0xB2E8, 0xB32A, 0xB16C, 0xB0AE,
  510. 0xBBF0, 0xBA32, 0xB874, 0xB9B6, 0xBCF8, 0xBD3A, 0xBF7C, 0xBEBE
  511. };
  512. /*
  513. * This pre-processing phase slows down procedure by approximately
  514. * same time as it makes each loop spin faster. In other words
  515. * single block performance is approximately same as straightforward
  516. * "4-bit" implementation, and then it goes only faster...
  517. * /
  518. for (cnt = 0; cnt < 16; ++cnt) {
  519. Z.hi = Htable[cnt].hi;
  520. Z.lo = Htable[cnt].lo;
  521. Hshr4[cnt].lo = (Z.hi << 60) | (Z.lo >> 4);
  522. Hshr4[cnt].hi = (Z.hi >> 4);
  523. Hshl4[cnt] = (u8)(Z.lo << 4);
  524. }
  525. do {
  526. for (Z.lo = 0, Z.hi = 0, cnt = 15; cnt; --cnt) {
  527. nlo = ((const u8 *)Xi)[cnt];
  528. nlo ^= inp[cnt];
  529. nhi = nlo >> 4;
  530. nlo &= 0xf;
  531. Z.hi ^= Htable[nlo].hi;
  532. Z.lo ^= Htable[nlo].lo;
  533. rem = (size_t)Z.lo & 0xff;
  534. Z.lo = (Z.hi << 56) | (Z.lo >> 8);
  535. Z.hi = (Z.hi >> 8);
  536. Z.hi ^= Hshr4[nhi].hi;
  537. Z.lo ^= Hshr4[nhi].lo;
  538. Z.hi ^= (u64)rem_8bit[rem ^ Hshl4[nhi]] << 48;
  539. }
  540. nlo = ((const u8 *)Xi)[0];
  541. nlo ^= inp[0];
  542. nhi = nlo >> 4;
  543. nlo &= 0xf;
  544. Z.hi ^= Htable[nlo].hi;
  545. Z.lo ^= Htable[nlo].lo;
  546. rem = (size_t)Z.lo & 0xf;
  547. Z.lo = (Z.hi << 60) | (Z.lo >> 4);
  548. Z.hi = (Z.hi >> 4);
  549. Z.hi ^= Htable[nhi].hi;
  550. Z.lo ^= Htable[nhi].lo;
  551. Z.hi ^= ((u64)rem_8bit[rem << 4]) << 48;
  552. # endif
  553. if (is_endian.little) {
  554. # ifdef BSWAP8
  555. Xi[0] = BSWAP8(Z.hi);
  556. Xi[1] = BSWAP8(Z.lo);
  557. # else
  558. u8 *p = (u8 *)Xi;
  559. u32 v;
  560. v = (u32)(Z.hi >> 32);
  561. PUTU32(p, v);
  562. v = (u32)(Z.hi);
  563. PUTU32(p + 4, v);
  564. v = (u32)(Z.lo >> 32);
  565. PUTU32(p + 8, v);
  566. v = (u32)(Z.lo);
  567. PUTU32(p + 12, v);
  568. # endif
  569. } else {
  570. Xi[0] = Z.hi;
  571. Xi[1] = Z.lo;
  572. }
  573. } while (inp += 16, len -= 16);
  574. }
  575. # endif
  576. # else
  577. void gcm_gmult_4bit(u64 Xi[2], const u128 Htable[16]);
  578. void gcm_ghash_4bit(u64 Xi[2], const u128 Htable[16], const u8 *inp,
  579. size_t len);
  580. # endif
  581. # define GCM_MUL(ctx,Xi) gcm_gmult_4bit(ctx->Xi.u,ctx->Htable)
  582. # if defined(GHASH_ASM) || !defined(OPENSSL_SMALL_FOOTPRINT)
  583. # define GHASH(ctx,in,len) gcm_ghash_4bit((ctx)->Xi.u,(ctx)->Htable,in,len)
  584. /* PCL UNUSED END */
  585. /*
  586. * GHASH_CHUNK is "stride parameter" missioned to mitigate cache trashing
  587. * effect. In other words idea is to hash data while it's still in L1 cache
  588. * after encryption pass...
  589. */
  590. # define GHASH_CHUNK (3*1024)
  591. /* PCL UNUSED START *
  592. # endif
  593. #else /* TABLE_BITS * /
  594. static void gcm_gmult_1bit(u64 Xi[2], const u64 H[2])
  595. {
  596. u128 V, Z = { 0, 0 };
  597. long X;
  598. int i, j;
  599. const long *xi = (const long *)Xi;
  600. const union {
  601. long one;
  602. char little;
  603. } is_endian = { 1 };
  604. V.hi = H[0]; /* H is in host byte order, no byte swapping * /
  605. V.lo = H[1];
  606. for (j = 0; j < 16 / sizeof(long); ++j) {
  607. if (is_endian.little) {
  608. if (sizeof(long) == 8) {
  609. # ifdef BSWAP8
  610. X = (long)(BSWAP8(xi[j]));
  611. # else
  612. const u8 *p = (const u8 *)(xi + j);
  613. X = (long)((u64)GETU32(p) << 32 | GETU32(p + 4));
  614. # endif
  615. } else {
  616. const u8 *p = (const u8 *)(xi + j);
  617. X = (long)GETU32(p);
  618. }
  619. } else
  620. X = xi[j];
  621. for (i = 0; i < 8 * sizeof(long); ++i, X <<= 1) {
  622. u64 M = (u64)(X >> (8 * sizeof(long) - 1));
  623. Z.hi ^= V.hi & M;
  624. Z.lo ^= V.lo & M;
  625. REDUCE1BIT(V);
  626. }
  627. }
  628. if (is_endian.little) {
  629. # ifdef BSWAP8
  630. Xi[0] = BSWAP8(Z.hi);
  631. Xi[1] = BSWAP8(Z.lo);
  632. # else
  633. u8 *p = (u8 *)Xi;
  634. u32 v;
  635. v = (u32)(Z.hi >> 32);
  636. PUTU32(p, v);
  637. v = (u32)(Z.hi);
  638. PUTU32(p + 4, v);
  639. v = (u32)(Z.lo >> 32);
  640. PUTU32(p + 8, v);
  641. v = (u32)(Z.lo);
  642. PUTU32(p + 12, v);
  643. # endif
  644. } else {
  645. Xi[0] = Z.hi;
  646. Xi[1] = Z.lo;
  647. }
  648. }
  649. # define GCM_MUL(ctx,Xi) gcm_gmult_1bit(ctx->Xi.u,ctx->H.u)
  650. #endif
  651. #if TABLE_BITS==4 && (defined(GHASH_ASM) || defined(OPENSSL_CPUID_OBJ))
  652. # if !defined(I386_ONLY) && \
  653. (defined(__i386) || defined(__i386__) || \
  654. defined(__x86_64) || defined(__x86_64__) || \
  655. defined(_M_IX86) || defined(_M_AMD64) || defined(_M_X64))
  656. # define GHASH_ASM_X86_OR_64
  657. # define GCM_FUNCREF_4BIT
  658. extern unsigned int OPENSSL_ia32cap_P[];
  659. void gcm_init_clmul(u128 Htable[16], const u64 Xi[2]);
  660. void gcm_gmult_clmul(u64 Xi[2], const u128 Htable[16]);
  661. void gcm_ghash_clmul(u64 Xi[2], const u128 Htable[16], const u8 *inp,
  662. size_t len);
  663. # if defined(__i386) || defined(__i386__) || defined(_M_IX86)
  664. # define gcm_init_avx gcm_init_clmul
  665. # define gcm_gmult_avx gcm_gmult_clmul
  666. # define gcm_ghash_avx gcm_ghash_clmul
  667. # else
  668. void gcm_init_avx(u128 Htable[16], const u64 Xi[2]);
  669. void gcm_gmult_avx(u64 Xi[2], const u128 Htable[16]);
  670. void gcm_ghash_avx(u64 Xi[2], const u128 Htable[16], const u8 *inp,
  671. size_t len);
  672. # endif
  673. # if defined(__i386) || defined(__i386__) || defined(_M_IX86)
  674. # define GHASH_ASM_X86
  675. void gcm_gmult_4bit_mmx(u64 Xi[2], const u128 Htable[16]);
  676. void gcm_ghash_4bit_mmx(u64 Xi[2], const u128 Htable[16], const u8 *inp,
  677. size_t len);
  678. void gcm_gmult_4bit_x86(u64 Xi[2], const u128 Htable[16]);
  679. void gcm_ghash_4bit_x86(u64 Xi[2], const u128 Htable[16], const u8 *inp,
  680. size_t len);
  681. # endif
  682. # elif defined(__arm__) || defined(__arm) || defined(__aarch64__)
  683. # include "arm_arch.h"
  684. # if __ARM_MAX_ARCH__>=7
  685. # define GHASH_ASM_ARM
  686. # define GCM_FUNCREF_4BIT
  687. # define PMULL_CAPABLE (OPENSSL_armcap_P & ARMV8_PMULL)
  688. # if defined(__arm__) || defined(__arm)
  689. # define NEON_CAPABLE (OPENSSL_armcap_P & ARMV7_NEON)
  690. # endif
  691. void gcm_init_neon(u128 Htable[16], const u64 Xi[2]);
  692. void gcm_gmult_neon(u64 Xi[2], const u128 Htable[16]);
  693. void gcm_ghash_neon(u64 Xi[2], const u128 Htable[16], const u8 *inp,
  694. size_t len);
  695. void gcm_init_v8(u128 Htable[16], const u64 Xi[2]);
  696. void gcm_gmult_v8(u64 Xi[2], const u128 Htable[16]);
  697. void gcm_ghash_v8(u64 Xi[2], const u128 Htable[16], const u8 *inp,
  698. size_t len);
  699. # endif
  700. # elif defined(__sparc__) || defined(__sparc)
  701. # include "sparc_arch.h"
  702. # define GHASH_ASM_SPARC
  703. # define GCM_FUNCREF_4BIT
  704. extern unsigned int OPENSSL_sparcv9cap_P[];
  705. void gcm_init_vis3(u128 Htable[16], const u64 Xi[2]);
  706. void gcm_gmult_vis3(u64 Xi[2], const u128 Htable[16]);
  707. void gcm_ghash_vis3(u64 Xi[2], const u128 Htable[16], const u8 *inp,
  708. size_t len);
  709. # elif defined(OPENSSL_CPUID_OBJ) && (defined(__powerpc__) || defined(__ppc__) || defined(_ARCH_PPC))
  710. # include "ppc_arch.h"
  711. # define GHASH_ASM_PPC
  712. # define GCM_FUNCREF_4BIT
  713. void gcm_init_p8(u128 Htable[16], const u64 Xi[2]);
  714. void gcm_gmult_p8(u64 Xi[2], const u128 Htable[16]);
  715. void gcm_ghash_p8(u64 Xi[2], const u128 Htable[16], const u8 *inp,
  716. size_t len);
  717. # endif
  718. #endif
  719. #ifdef GCM_FUNCREF_4BIT
  720. # undef GCM_MUL
  721. # define GCM_MUL(ctx,Xi) (*gcm_gmult_p)(ctx->Xi.u,ctx->Htable)
  722. # ifdef GHASH
  723. # undef GHASH
  724. /* PCL UNUSED END */
  725. extern "C" void pcl_gcm_init_clmul(u128 Htable[16], u64 H[2]);
  726. extern "C" void pcl_gcm_gmult_clmul(u64 Xi[2], const u128 Htable[16]);
  727. extern "C" void pcl_gcm_ghash_clmul(u64 Xi[2], const u128 Htable[16],
  728. const u8 *inp, size_t len);
  729. # define GCM_MUL(ctx,Xi) pcl_gcm_gmult_clmul(ctx->Xi.u,ctx->Htable)
  730. # define GHASH(ctx,in,len) (*gcm_ghash_p)(ctx->Xi.u,ctx->Htable,in,len)
  731. /* PCL UNUSED START *
  732. # endif
  733. #endif
  734. /* PCL UNUSED END */
  735. void pcl_CRYPTO_gcm128_init(GCM128_CONTEXT *ctx, void *key, block128_f block)
  736. {
  737. /* PCL UNUSED START *
  738. const union {
  739. long one;
  740. char little;
  741. } is_endian = { 1 };
  742. /* PCL UNUSED END */
  743. pcl_memset(ctx, 0, sizeof(*ctx));
  744. ctx->block = block;
  745. ctx->key = key;
  746. (*block) (ctx->H.c, ctx->H.c, key);
  747. /* PCL UNUSED START *
  748. if (is_endian.little) {
  749. /* H is stored in host byte order *
  750. #ifdef BSWAP8
  751. /* PCL UNUSED END */
  752. ctx->H.u[0] = BSWAP8(ctx->H.u[0]);
  753. ctx->H.u[1] = BSWAP8(ctx->H.u[1]);
  754. /* PCL UNUSED START *
  755. #else
  756. u8 *p = ctx->H.c;
  757. u64 hi, lo;
  758. hi = (u64)GETU32(p) << 32 | GETU32(p + 4);
  759. lo = (u64)GETU32(p + 8) << 32 | GETU32(p + 12);
  760. ctx->H.u[0] = hi;
  761. ctx->H.u[1] = lo;
  762. #endif
  763. }
  764. #if TABLE_BITS==8
  765. gcm_init_8bit(ctx->Htable, ctx->H.u);
  766. #elif TABLE_BITS==4
  767. # if defined(GHASH)
  768. # define CTX__GHASH(f) (ctx->ghash = (f))
  769. # else
  770. # define CTX__GHASH(f) (ctx->ghash = NULL)
  771. # endif
  772. # if defined(GHASH_ASM_X86_OR_64)
  773. # if !defined(GHASH_ASM_X86) || defined(OPENSSL_IA32_SSE2)
  774. if (OPENSSL_ia32cap_P[0] & (1 << 24) && // check FXSR bit
  775. OPENSSL_ia32cap_P[1] & (1 << 1)) { // check PCLMULQDQ bit
  776. if (((OPENSSL_ia32cap_P[1] >> 22) & 0x41) == 0x41) { // AVX+MOVBE
  777. gcm_init_avx(ctx->Htable, ctx->H.u);
  778. ctx->gmult = gcm_gmult_avx;
  779. CTX__GHASH(gcm_ghash_avx);
  780. } else {
  781. /* PCL UNUSED END */
  782. pcl_gcm_init_clmul(ctx->Htable, ctx->H.u);
  783. ctx->gmult = pcl_gcm_gmult_clmul;
  784. ctx->ghash = pcl_gcm_ghash_clmul;
  785. /* PCL UNUSED START *
  786. CTX__GHASH(gcm_ghash_clmul);
  787. }
  788. /* PCL UNUSED END */
  789. return;
  790. /* PCL UNUSED START *
  791. }
  792. # endif
  793. pcl_gcm_init_4bit(ctx->Htable, ctx->H.u);
  794. # if defined(GHASH_ASM_X86) // x86 only
  795. # if defined(OPENSSL_IA32_SSE2)
  796. if (OPENSSL_ia32cap_P[0] & (1 << 25)) { // check SSE bit
  797. # else
  798. if (OPENSSL_ia32cap_P[0] & (1 << 23)) { // check MMX bit
  799. # endif
  800. ctx->gmult = gcm_gmult_4bit_mmx;
  801. CTX__GHASH(gcm_ghash_4bit_mmx);
  802. } else {
  803. ctx->gmult = gcm_gmult_4bit_x86;
  804. CTX__GHASH(gcm_ghash_4bit_x86);
  805. }
  806. # else
  807. ctx->gmult = pcl_gcm_gmult_4bit;
  808. CTX__GHASH(pcl_gcm_ghash_4bit);
  809. ctx->ghash = pcl_gcm_ghash_4bit;
  810. # endif
  811. # elif defined(GHASH_ASM_ARM)
  812. # ifdef PMULL_CAPABLE
  813. if (PMULL_CAPABLE) {
  814. gcm_init_v8(ctx->Htable, ctx->H.u);
  815. ctx->gmult = gcm_gmult_v8;
  816. CTX__GHASH(gcm_ghash_v8);
  817. } else
  818. # endif
  819. # ifdef NEON_CAPABLE
  820. if (NEON_CAPABLE) {
  821. gcm_init_neon(ctx->Htable, ctx->H.u);
  822. ctx->gmult = gcm_gmult_neon;
  823. CTX__GHASH(gcm_ghash_neon);
  824. } else
  825. # endif
  826. {
  827. gcm_init_4bit(ctx->Htable, ctx->H.u);
  828. ctx->gmult = gcm_gmult_4bit;
  829. CTX__GHASH(gcm_ghash_4bit);
  830. }
  831. # elif defined(GHASH_ASM_SPARC)
  832. if (OPENSSL_sparcv9cap_P[0] & SPARCV9_VIS3) {
  833. gcm_init_vis3(ctx->Htable, ctx->H.u);
  834. ctx->gmult = gcm_gmult_vis3;
  835. CTX__GHASH(gcm_ghash_vis3);
  836. } else {
  837. gcm_init_4bit(ctx->Htable, ctx->H.u);
  838. ctx->gmult = gcm_gmult_4bit;
  839. CTX__GHASH(gcm_ghash_4bit);
  840. }
  841. # elif defined(GHASH_ASM_PPC)
  842. if (OPENSSL_ppccap_P & PPC_CRYPTO207) {
  843. gcm_init_p8(ctx->Htable, ctx->H.u);
  844. ctx->gmult = gcm_gmult_p8;
  845. CTX__GHASH(gcm_ghash_p8);
  846. } else {
  847. gcm_init_4bit(ctx->Htable, ctx->H.u);
  848. ctx->gmult = gcm_gmult_4bit;
  849. CTX__GHASH(gcm_ghash_4bit);
  850. }
  851. # else
  852. gcm_init_4bit(ctx->Htable, ctx->H.u);
  853. # endif
  854. # undef CTX__GHASH
  855. #endif
  856. /* PCL UNUSED END */
  857. }
  858. void pcl_CRYPTO_gcm128_setiv(GCM128_CONTEXT *ctx, const unsigned char *iv,
  859. size_t len)
  860. {
  861. /* PCL UNUSED START *
  862. const union {
  863. long one;
  864. char little;
  865. } is_endian = { 1 };
  866. /* PCL UNUSED END */
  867. unsigned int ctr;
  868. /* PCL UNUSED START *
  869. #ifdef GCM_FUNCREF_4BIT
  870. /* PCL UNUSED END */
  871. void (*gcm_gmult_p) (u64 Xi[2], const u128 Htable[16]) = ctx->gmult;
  872. /* PCL UNUSED START *
  873. #endif
  874. /* PCL UNUSED END */
  875. ctx->Yi.u[0] = 0;
  876. ctx->Yi.u[1] = 0;
  877. ctx->Xi.u[0] = 0;
  878. ctx->Xi.u[1] = 0;
  879. ctx->len.u[0] = 0; /* AAD length */
  880. ctx->len.u[1] = 0; /* message length */
  881. ctx->ares = 0;
  882. ctx->mres = 0;
  883. /* PCL UNUSED START *
  884. if (len == 12) {
  885. /* PCL UNUSED END */
  886. pcl_memcpy(ctx->Yi.c, (void*)iv, 12);
  887. ctx->Yi.c[15] = 1;
  888. ctr = 1;
  889. /* PCL UNUSED START *
  890. } else {
  891. size_t i;
  892. u64 len0 = len;
  893. while (len >= 16) {
  894. for (i = 0; i < 16; ++i)
  895. ctx->Yi.c[i] ^= iv[i];
  896. GCM_MUL(ctx, Yi);
  897. iv += 16;
  898. len -= 16;
  899. }
  900. if (len) {
  901. for (i = 0; i < len; ++i)
  902. ctx->Yi.c[i] ^= iv[i];
  903. GCM_MUL(ctx, Yi);
  904. }
  905. len0 <<= 3;
  906. if (is_endian.little) {
  907. #ifdef BSWAP8
  908. ctx->Yi.u[1] ^= BSWAP8(len0);
  909. #else
  910. ctx->Yi.c[8] ^= (u8)(len0 >> 56);
  911. ctx->Yi.c[9] ^= (u8)(len0 >> 48);
  912. ctx->Yi.c[10] ^= (u8)(len0 >> 40);
  913. ctx->Yi.c[11] ^= (u8)(len0 >> 32);
  914. ctx->Yi.c[12] ^= (u8)(len0 >> 24);
  915. ctx->Yi.c[13] ^= (u8)(len0 >> 16);
  916. ctx->Yi.c[14] ^= (u8)(len0 >> 8);
  917. ctx->Yi.c[15] ^= (u8)(len0);
  918. #endif
  919. } else
  920. ctx->Yi.u[1] ^= len0;
  921. GCM_MUL(ctx, Yi);
  922. if (is_endian.little)
  923. #ifdef BSWAP4
  924. ctr = BSWAP4(ctx->Yi.d[3]);
  925. #else
  926. ctr = GETU32(ctx->Yi.c + 12);
  927. #endif
  928. else
  929. ctr = ctx->Yi.d[3];
  930. }
  931. /* PCL UNUSED END */
  932. (*ctx->block) (ctx->Yi.c, ctx->EK0.c, ctx->key);
  933. ++ctr;
  934. /* PCL UNUSED START *
  935. if (is_endian.little)
  936. #ifdef BSWAP4
  937. /* PCL UNUSED END */
  938. ctx->Yi.d[3] = pcl_bswap32(ctr);
  939. /* PCL UNUSED START *
  940. #else
  941. PUTU32(ctx->Yi.c + 12, ctr);
  942. #endif
  943. else
  944. ctx->Yi.d[3] = ctr;
  945. /* PCL UNUSED END */
  946. }
  947. int pcl_CRYPTO_gcm128_aad(
  948. GCM128_CONTEXT *ctx,
  949. const unsigned char *aad,
  950. size_t len)
  951. {
  952. size_t i;
  953. unsigned int n;
  954. u64 alen = ctx->len.u[0];
  955. /* PCL UNUSED START *
  956. #ifdef GCM_FUNCREF_4BIT
  957. /* PCL UNUSED END */
  958. void (*gcm_gmult_p) (u64 Xi[2], const u128 Htable[16]) = ctx->gmult;
  959. /* PCL UNUSED START *
  960. # ifdef GHASH
  961. /* PCL UNUSED END */
  962. void (*gcm_ghash_p) (u64 Xi[2], const u128 Htable[16],
  963. const u8 *inp, size_t len) = ctx->ghash;
  964. /* PCL UNUSED START *
  965. # endif
  966. #endif
  967. /* PCL UNUSED END */
  968. if (ctx->len.u[1])
  969. return -2;
  970. alen += len;
  971. if (alen > (U64(1) << 61) || (sizeof(len) == 8 && alen < len))
  972. return -1;
  973. ctx->len.u[0] = alen;
  974. n = ctx->ares;
  975. if (n) {
  976. while (n && len) {
  977. ctx->Xi.c[n] ^= *(aad++);
  978. --len;
  979. n = (n + 1) % 16;
  980. }
  981. if (n == 0)
  982. GCM_MUL(ctx, Xi);
  983. else {
  984. ctx->ares = n;
  985. return 0;
  986. }
  987. }
  988. /* PCL UNUSED START *
  989. #ifdef GHASH
  990. /* PCL UNUSED END */
  991. if ((i = (len & (size_t)-16))) {
  992. GHASH(ctx, aad, i);
  993. aad += i;
  994. len -= i;
  995. }
  996. /* PCL UNUSED START *
  997. #else
  998. while (len >= 16) {
  999. for (i = 0; i < 16; ++i)
  1000. ctx->Xi.c[i] ^= aad[i];
  1001. GCM_MUL(ctx, Xi);
  1002. aad += 16;
  1003. len -= 16;
  1004. }
  1005. #endif
  1006. /* PCL UNUSED END */
  1007. if (len) {
  1008. n = (unsigned int)len;
  1009. for (i = 0; i < len; ++i)
  1010. ctx->Xi.c[i] ^= aad[i];
  1011. }
  1012. ctx->ares = n;
  1013. return 0;
  1014. }
  1015. /* PCL UNUSED START *
  1016. int CRYPTO_gcm128_encrypt(GCM128_CONTEXT *ctx,
  1017. const unsigned char *in, unsigned char *out,
  1018. size_t len)
  1019. {
  1020. const union {
  1021. long one;
  1022. char little;
  1023. } is_endian = { 1 };
  1024. unsigned int n, ctr;
  1025. size_t i;
  1026. u64 mlen = ctx->len.u[1];
  1027. block128_f block = ctx->block;
  1028. void *key = ctx->key;
  1029. #ifdef GCM_FUNCREF_4BIT
  1030. void (*gcm_gmult_p) (u64 Xi[2], const u128 Htable[16]) = ctx->gmult;
  1031. # if defined(GHASH) && !defined(OPENSSL_SMALL_FOOTPRINT)
  1032. void (*gcm_ghash_p) (u64 Xi[2], const u128 Htable[16],
  1033. const u8 *inp, size_t len) = ctx->ghash;
  1034. # endif
  1035. #endif
  1036. mlen += len;
  1037. if (mlen > ((U64(1) << 36) - 32) || (sizeof(len) == 8 && mlen < len))
  1038. return -1;
  1039. ctx->len.u[1] = mlen;
  1040. if (ctx->ares) {
  1041. /* First call to encrypt finalizes GHASH(AAD) * /
  1042. GCM_MUL(ctx, Xi);
  1043. ctx->ares = 0;
  1044. }
  1045. if (is_endian.little)
  1046. #ifdef BSWAP4
  1047. ctr = BSWAP4(ctx->Yi.d[3]);
  1048. #else
  1049. ctr = GETU32(ctx->Yi.c + 12);
  1050. #endif
  1051. else
  1052. ctr = ctx->Yi.d[3];
  1053. n = ctx->mres;
  1054. #if !defined(OPENSSL_SMALL_FOOTPRINT)
  1055. if (16 % sizeof(size_t) == 0) { /* always true actually * /
  1056. do {
  1057. if (n) {
  1058. while (n && len) {
  1059. ctx->Xi.c[n] ^= *(out++) = *(in++) ^ ctx->EKi.c[n];
  1060. --len;
  1061. n = (n + 1) % 16;
  1062. }
  1063. if (n == 0)
  1064. GCM_MUL(ctx, Xi);
  1065. else {
  1066. ctx->mres = n;
  1067. return 0;
  1068. }
  1069. }
  1070. # if defined(STRICT_ALIGNMENT)
  1071. if (((size_t)in | (size_t)out) % sizeof(size_t) != 0)
  1072. break;
  1073. # endif
  1074. # if defined(GHASH)
  1075. # if defined(GHASH_CHUNK)
  1076. while (len >= GHASH_CHUNK) {
  1077. size_t j = GHASH_CHUNK;
  1078. while (j) {
  1079. size_t *out_t = (size_t *)out;
  1080. const size_t *in_t = (const size_t *)in;
  1081. (*block) (ctx->Yi.c, ctx->EKi.c, key);
  1082. ++ctr;
  1083. if (is_endian.little)
  1084. # ifdef BSWAP4
  1085. ctx->Yi.d[3] = BSWAP4(ctr);
  1086. # else
  1087. PUTU32(ctx->Yi.c + 12, ctr);
  1088. # endif
  1089. else
  1090. ctx->Yi.d[3] = ctr;
  1091. for (i = 0; i < 16 / sizeof(size_t); ++i)
  1092. out_t[i] = in_t[i] ^ ctx->EKi.t[i];
  1093. out += 16;
  1094. in += 16;
  1095. j -= 16;
  1096. }
  1097. GHASH(ctx, out - GHASH_CHUNK, GHASH_CHUNK);
  1098. len -= GHASH_CHUNK;
  1099. }
  1100. # endif
  1101. if ((i = (len & (size_t)-16))) {
  1102. size_t j = i;
  1103. while (len >= 16) {
  1104. size_t *out_t = (size_t *)out;
  1105. const size_t *in_t = (const size_t *)in;
  1106. (*block) (ctx->Yi.c, ctx->EKi.c, key);
  1107. ++ctr;
  1108. if (is_endian.little)
  1109. # ifdef BSWAP4
  1110. ctx->Yi.d[3] = BSWAP4(ctr);
  1111. # else
  1112. PUTU32(ctx->Yi.c + 12, ctr);
  1113. # endif
  1114. else
  1115. ctx->Yi.d[3] = ctr;
  1116. for (i = 0; i < 16 / sizeof(size_t); ++i)
  1117. out_t[i] = in_t[i] ^ ctx->EKi.t[i];
  1118. out += 16;
  1119. in += 16;
  1120. len -= 16;
  1121. }
  1122. GHASH(ctx, out - j, j);
  1123. }
  1124. # else
  1125. while (len >= 16) {
  1126. size_t *out_t = (size_t *)out;
  1127. const size_t *in_t = (const size_t *)in;
  1128. (*block) (ctx->Yi.c, ctx->EKi.c, key);
  1129. ++ctr;
  1130. if (is_endian.little)
  1131. # ifdef BSWAP4
  1132. ctx->Yi.d[3] = BSWAP4(ctr);
  1133. # else
  1134. PUTU32(ctx->Yi.c + 12, ctr);
  1135. # endif
  1136. else
  1137. ctx->Yi.d[3] = ctr;
  1138. for (i = 0; i < 16 / sizeof(size_t); ++i)
  1139. ctx->Xi.t[i] ^= out_t[i] = in_t[i] ^ ctx->EKi.t[i];
  1140. GCM_MUL(ctx, Xi);
  1141. out += 16;
  1142. in += 16;
  1143. len -= 16;
  1144. }
  1145. # endif
  1146. if (len) {
  1147. (*block) (ctx->Yi.c, ctx->EKi.c, key);
  1148. ++ctr;
  1149. if (is_endian.little)
  1150. # ifdef BSWAP4
  1151. ctx->Yi.d[3] = BSWAP4(ctr);
  1152. # else
  1153. PUTU32(ctx->Yi.c + 12, ctr);
  1154. # endif
  1155. else
  1156. ctx->Yi.d[3] = ctr;
  1157. while (len--) {
  1158. ctx->Xi.c[n] ^= out[n] = in[n] ^ ctx->EKi.c[n];
  1159. ++n;
  1160. }
  1161. }
  1162. ctx->mres = n;
  1163. return 0;
  1164. } while (0);
  1165. }
  1166. #endif
  1167. for (i = 0; i < len; ++i) {
  1168. if (n == 0) {
  1169. (*block) (ctx->Yi.c, ctx->EKi.c, key);
  1170. ++ctr;
  1171. if (is_endian.little)
  1172. #ifdef BSWAP4
  1173. ctx->Yi.d[3] = BSWAP4(ctr);
  1174. #else
  1175. PUTU32(ctx->Yi.c + 12, ctr);
  1176. #endif
  1177. else
  1178. ctx->Yi.d[3] = ctr;
  1179. }
  1180. ctx->Xi.c[n] ^= out[i] = in[i] ^ ctx->EKi.c[n];
  1181. n = (n + 1) % 16;
  1182. if (n == 0)
  1183. GCM_MUL(ctx, Xi);
  1184. }
  1185. ctx->mres = n;
  1186. return 0;
  1187. }
  1188. /* PCL UNUSED END */
  1189. int pcl_CRYPTO_gcm128_decrypt(GCM128_CONTEXT *ctx,
  1190. const unsigned char *in, unsigned char *out,
  1191. size_t len)
  1192. {
  1193. /* PCL UNUSED START *
  1194. const union {
  1195. long one;
  1196. char little;
  1197. } is_endian = { 1 };
  1198. /* PCL UNUSED END */
  1199. unsigned int n, ctr;
  1200. size_t i;
  1201. u64 mlen = ctx->len.u[1];
  1202. block128_f block = ctx->block;
  1203. void *key = ctx->key;
  1204. /* PCL UNUSED START *
  1205. #ifdef GCM_FUNCREF_4BIT
  1206. /* PCL UNUSED END */
  1207. void (*gcm_gmult_p) (u64 Xi[2], const u128 Htable[16]) = ctx->gmult;
  1208. /* PCL UNUSED START *
  1209. # if defined(GHASH) && !defined(OPENSSL_SMALL_FOOTPRINT)
  1210. /* PCL UNUSED END */
  1211. void (*gcm_ghash_p) (u64 Xi[2], const u128 Htable[16],
  1212. const u8 *inp, size_t len) = ctx->ghash;
  1213. /* PCL UNUSED START *
  1214. # endif
  1215. #endif
  1216. /* PCL UNUSED END */
  1217. mlen += len;
  1218. if (mlen > ((U64(1) << 36) - 32) || (sizeof(len) == 8 && mlen < len))
  1219. return -1;
  1220. ctx->len.u[1] = mlen;
  1221. if (ctx->ares) {
  1222. /* First call to decrypt finalizes GHASH(AAD) */
  1223. GCM_MUL(ctx, Xi);
  1224. ctx->ares = 0;
  1225. }
  1226. /* PCL UNUSED START *
  1227. if (is_endian.little)
  1228. #ifdef BSWAP4
  1229. /* PCL UNUSED END */
  1230. ctr = pcl_bswap32(ctx->Yi.d[3]);
  1231. /* PCL UNUSED START *
  1232. #else
  1233. ctr = GETU32(ctx->Yi.c + 12);
  1234. #endif
  1235. else
  1236. ctr = ctx->Yi.d[3];
  1237. /* PCL UNUSED END */
  1238. n = ctx->mres;
  1239. /* PCL UNUSED START *
  1240. #if !defined(OPENSSL_SMALL_FOOTPRINT)
  1241. if (16 % sizeof(size_t) == 0) { /* always true actually * /
  1242. do {
  1243. /* PCL UNUSED END */
  1244. if (n) {
  1245. while (n && len) {
  1246. u8 c = *(in++);
  1247. *(out++) = c ^ ctx->EKi.c[n];
  1248. ctx->Xi.c[n] ^= c;
  1249. --len;
  1250. n = (n + 1) % 16;
  1251. }
  1252. if (n == 0)
  1253. GCM_MUL(ctx, Xi);
  1254. else {
  1255. ctx->mres = n;
  1256. return 0;
  1257. }
  1258. }
  1259. /* PCL UNUSED START *
  1260. # if defined(STRICT_ALIGNMENT)
  1261. if (((size_t)in | (size_t)out) % sizeof(size_t) != 0)
  1262. break;
  1263. # endif
  1264. # if defined(GHASH)
  1265. # if defined(GHASH_CHUNK)
  1266. /* PCL UNUSED END */
  1267. while (len >= GHASH_CHUNK) {
  1268. size_t j = GHASH_CHUNK;
  1269. GHASH(ctx, in, GHASH_CHUNK);
  1270. while (j) {
  1271. size_t *out_t = (size_t *)out;
  1272. const size_t *in_t = (const size_t *)in;
  1273. (*block) (ctx->Yi.c, ctx->EKi.c, key);
  1274. ++ctr;
  1275. /* PCL UNUSED START *
  1276. if (is_endian.little)
  1277. # ifdef BSWAP4
  1278. /* PCL UNUSED END */
  1279. ctx->Yi.d[3] = pcl_bswap32(ctr);
  1280. /* PCL UNUSED START *
  1281. # else
  1282. PUTU32(ctx->Yi.c + 12, ctr);
  1283. # endif
  1284. else
  1285. ctx->Yi.d[3] = ctr;
  1286. /* PCL UNUSED END */
  1287. for (i = 0; i < 16 / sizeof(size_t); ++i)
  1288. out_t[i] = in_t[i] ^ ctx->EKi.t[i];
  1289. out += 16;
  1290. in += 16;
  1291. j -= 16;
  1292. }
  1293. len -= GHASH_CHUNK;
  1294. }
  1295. /* PCL UNUSED START *
  1296. # endif
  1297. /* PCL UNUSED END */
  1298. if ((i = (len & (size_t)-16))) {
  1299. GHASH(ctx, in, i);
  1300. while (len >= 16) {
  1301. size_t *out_t = (size_t *)out;
  1302. const size_t *in_t = (const size_t *)in;
  1303. (*block) (ctx->Yi.c, ctx->EKi.c, key);
  1304. ++ctr;
  1305. /* PCL UNUSED START *
  1306. if (is_endian.little)
  1307. # ifdef BSWAP4
  1308. /* PCL UNUSED END */
  1309. ctx->Yi.d[3] = pcl_bswap32(ctr);
  1310. /* PCL UNUSED START *
  1311. # else
  1312. PUTU32(ctx->Yi.c + 12, ctr);
  1313. # endif
  1314. else
  1315. ctx->Yi.d[3] = ctr;
  1316. /* PCL UNUSED END */
  1317. for (i = 0; i < 16 / sizeof(size_t); ++i)
  1318. out_t[i] = in_t[i] ^ ctx->EKi.t[i];
  1319. out += 16;
  1320. in += 16;
  1321. len -= 16;
  1322. }
  1323. }
  1324. /* PCL UNUSED START *
  1325. # else
  1326. while (len >= 16) {
  1327. size_t *out_t = (size_t *)out;
  1328. const size_t *in_t = (const size_t *)in;
  1329. (*block) (ctx->Yi.c, ctx->EKi.c, key);
  1330. ++ctr;
  1331. if (is_endian.little)
  1332. # ifdef BSWAP4
  1333. ctx->Yi.d[3] = BSWAP4(ctr);
  1334. # else
  1335. PUTU32(ctx->Yi.c + 12, ctr);
  1336. # endif
  1337. else
  1338. ctx->Yi.d[3] = ctr;
  1339. for (i = 0; i < 16 / sizeof(size_t); ++i) {
  1340. size_t c = in[i];
  1341. out[i] = c ^ ctx->EKi.t[i];
  1342. ctx->Xi.t[i] ^= c;
  1343. }
  1344. GCM_MUL(ctx, Xi);
  1345. out += 16;
  1346. in += 16;
  1347. len -= 16;
  1348. }
  1349. # endif
  1350. /* PCL UNUSED END */
  1351. if (len) {
  1352. (*block) (ctx->Yi.c, ctx->EKi.c, key);
  1353. ++ctr;
  1354. /* PCL UNUSED START *
  1355. if (is_endian.little)
  1356. # ifdef BSWAP4
  1357. /* PCL UNUSED END */
  1358. ctx->Yi.d[3] = pcl_bswap32(ctr);
  1359. /* PCL UNUSED START *
  1360. # else
  1361. PUTU32(ctx->Yi.c + 12, ctr);
  1362. # endif
  1363. else
  1364. ctx->Yi.d[3] = ctr;
  1365. /* PCL UNUSED END */
  1366. while (len--) {
  1367. u8 c = in[n];
  1368. ctx->Xi.c[n] ^= c;
  1369. out[n] = c ^ ctx->EKi.c[n];
  1370. ++n;
  1371. }
  1372. }
  1373. ctx->mres = n;
  1374. return 0;
  1375. /* PCL UNUSED START *
  1376. } while (0);
  1377. }
  1378. #endif
  1379. for (i = 0; i < len; ++i) {
  1380. u8 c;
  1381. if (n == 0) {
  1382. (*block) (ctx->Yi.c, ctx->EKi.c, key);
  1383. ++ctr;
  1384. if (is_endian.little)
  1385. #ifdef BSWAP4
  1386. ctx->Yi.d[3] = pcl_bswap32(ctr);
  1387. #else
  1388. PUTU32(ctx->Yi.c + 12, ctr);
  1389. #endif
  1390. else
  1391. ctx->Yi.d[3] = ctr;
  1392. }
  1393. c = in[i];
  1394. out[i] = c ^ ctx->EKi.c[n];
  1395. ctx->Xi.c[n] ^= c;
  1396. n = (n + 1) % 16;
  1397. if (n == 0)
  1398. GCM_MUL(ctx, Xi);
  1399. }
  1400. ctx->mres = n;
  1401. return 0;
  1402. /* PCL UNUSED END */
  1403. }
  1404. /* PCL UNUSED START *
  1405. int CRYPTO_gcm128_encrypt_ctr32(GCM128_CONTEXT *ctx,
  1406. const unsigned char *in, unsigned char *out,
  1407. size_t len, ctr128_f stream)
  1408. {
  1409. #if defined(OPENSSL_SMALL_FOOTPRINT)
  1410. return CRYPTO_gcm128_encrypt(ctx, in, out, len);
  1411. #else
  1412. const union {
  1413. long one;
  1414. char little;
  1415. } is_endian = { 1 };
  1416. unsigned int n, ctr;
  1417. size_t i;
  1418. u64 mlen = ctx->len.u[1];
  1419. void *key = ctx->key;
  1420. # ifdef GCM_FUNCREF_4BIT
  1421. void (*gcm_gmult_p) (u64 Xi[2], const u128 Htable[16]) = ctx->gmult;
  1422. # ifdef GHASH
  1423. void (*gcm_ghash_p) (u64 Xi[2], const u128 Htable[16],
  1424. const u8 *inp, size_t len) = ctx->ghash;
  1425. # endif
  1426. # endif
  1427. mlen += len;
  1428. if (mlen > ((U64(1) << 36) - 32) || (sizeof(len) == 8 && mlen < len))
  1429. return -1;
  1430. ctx->len.u[1] = mlen;
  1431. if (ctx->ares) {
  1432. /* First call to encrypt finalizes GHASH(AAD) * /
  1433. GCM_MUL(ctx, Xi);
  1434. ctx->ares = 0;
  1435. }
  1436. if (is_endian.little)
  1437. # ifdef BSWAP4
  1438. ctr = BSWAP4(ctx->Yi.d[3]);
  1439. # else
  1440. ctr = GETU32(ctx->Yi.c + 12);
  1441. # endif
  1442. else
  1443. ctr = ctx->Yi.d[3];
  1444. n = ctx->mres;
  1445. if (n) {
  1446. while (n && len) {
  1447. ctx->Xi.c[n] ^= *(out++) = *(in++) ^ ctx->EKi.c[n];
  1448. --len;
  1449. n = (n + 1) % 16;
  1450. }
  1451. if (n == 0)
  1452. GCM_MUL(ctx, Xi);
  1453. else {
  1454. ctx->mres = n;
  1455. return 0;
  1456. }
  1457. }
  1458. # if defined(GHASH) && defined(GHASH_CHUNK)
  1459. while (len >= GHASH_CHUNK) {
  1460. (*stream) (in, out, GHASH_CHUNK / 16, key, ctx->Yi.c);
  1461. ctr += GHASH_CHUNK / 16;
  1462. if (is_endian.little)
  1463. # ifdef BSWAP4
  1464. ctx->Yi.d[3] = BSWAP4(ctr);
  1465. # else
  1466. PUTU32(ctx->Yi.c + 12, ctr);
  1467. # endif
  1468. else
  1469. ctx->Yi.d[3] = ctr;
  1470. GHASH(ctx, out, GHASH_CHUNK);
  1471. out += GHASH_CHUNK;
  1472. in += GHASH_CHUNK;
  1473. len -= GHASH_CHUNK;
  1474. }
  1475. # endif
  1476. if ((i = (len & (size_t)-16))) {
  1477. size_t j = i / 16;
  1478. (*stream) (in, out, j, key, ctx->Yi.c);
  1479. ctr += (unsigned int)j;
  1480. if (is_endian.little)
  1481. # ifdef BSWAP4
  1482. ctx->Yi.d[3] = BSWAP4(ctr);
  1483. # else
  1484. PUTU32(ctx->Yi.c + 12, ctr);
  1485. # endif
  1486. else
  1487. ctx->Yi.d[3] = ctr;
  1488. in += i;
  1489. len -= i;
  1490. # if defined(GHASH)
  1491. GHASH(ctx, out, i);
  1492. out += i;
  1493. # else
  1494. while (j--) {
  1495. for (i = 0; i < 16; ++i)
  1496. ctx->Xi.c[i] ^= out[i];
  1497. GCM_MUL(ctx, Xi);
  1498. out += 16;
  1499. }
  1500. # endif
  1501. }
  1502. if (len) {
  1503. (*ctx->block) (ctx->Yi.c, ctx->EKi.c, key);
  1504. ++ctr;
  1505. if (is_endian.little)
  1506. # ifdef BSWAP4
  1507. ctx->Yi.d[3] = BSWAP4(ctr);
  1508. # else
  1509. PUTU32(ctx->Yi.c + 12, ctr);
  1510. # endif
  1511. else
  1512. ctx->Yi.d[3] = ctr;
  1513. while (len--) {
  1514. ctx->Xi.c[n] ^= out[n] = in[n] ^ ctx->EKi.c[n];
  1515. ++n;
  1516. }
  1517. }
  1518. ctx->mres = n;
  1519. return 0;
  1520. #endif
  1521. }
  1522. int CRYPTO_gcm128_decrypt_ctr32(GCM128_CONTEXT *ctx,
  1523. const unsigned char *in, unsigned char *out,
  1524. size_t len, ctr128_f stream)
  1525. {
  1526. #if defined(OPENSSL_SMALL_FOOTPRINT)
  1527. return CRYPTO_gcm128_decrypt(ctx, in, out, len);
  1528. #else
  1529. const union {
  1530. long one;
  1531. char little;
  1532. } is_endian = { 1 };
  1533. unsigned int n, ctr;
  1534. size_t i;
  1535. u64 mlen = ctx->len.u[1];
  1536. void *key = ctx->key;
  1537. # ifdef GCM_FUNCREF_4BIT
  1538. void (*gcm_gmult_p) (u64 Xi[2], const u128 Htable[16]) = ctx->gmult;
  1539. # ifdef GHASH
  1540. void (*gcm_ghash_p) (u64 Xi[2], const u128 Htable[16],
  1541. const u8 *inp, size_t len) = ctx->ghash;
  1542. # endif
  1543. # endif
  1544. mlen += len;
  1545. if (mlen > ((U64(1) << 36) - 32) || (sizeof(len) == 8 && mlen < len))
  1546. return -1;
  1547. ctx->len.u[1] = mlen;
  1548. if (ctx->ares) {
  1549. /* First call to decrypt finalizes GHASH(AAD) * /
  1550. GCM_MUL(ctx, Xi);
  1551. ctx->ares = 0;
  1552. }
  1553. if (is_endian.little)
  1554. # ifdef BSWAP4
  1555. ctr = BSWAP4(ctx->Yi.d[3]);
  1556. # else
  1557. ctr = GETU32(ctx->Yi.c + 12);
  1558. # endif
  1559. else
  1560. ctr = ctx->Yi.d[3];
  1561. n = ctx->mres;
  1562. if (n) {
  1563. while (n && len) {
  1564. u8 c = *(in++);
  1565. *(out++) = c ^ ctx->EKi.c[n];
  1566. ctx->Xi.c[n] ^= c;
  1567. --len;
  1568. n = (n + 1) % 16;
  1569. }
  1570. if (n == 0)
  1571. GCM_MUL(ctx, Xi);
  1572. else {
  1573. ctx->mres = n;
  1574. return 0;
  1575. }
  1576. }
  1577. # if defined(GHASH) && defined(GHASH_CHUNK)
  1578. while (len >= GHASH_CHUNK) {
  1579. GHASH(ctx, in, GHASH_CHUNK);
  1580. (*stream) (in, out, GHASH_CHUNK / 16, key, ctx->Yi.c);
  1581. ctr += GHASH_CHUNK / 16;
  1582. if (is_endian.little)
  1583. # ifdef BSWAP4
  1584. ctx->Yi.d[3] = BSWAP4(ctr);
  1585. # else
  1586. PUTU32(ctx->Yi.c + 12, ctr);
  1587. # endif
  1588. else
  1589. ctx->Yi.d[3] = ctr;
  1590. out += GHASH_CHUNK;
  1591. in += GHASH_CHUNK;
  1592. len -= GHASH_CHUNK;
  1593. }
  1594. # endif
  1595. if ((i = (len & (size_t)-16))) {
  1596. size_t j = i / 16;
  1597. # if defined(GHASH)
  1598. GHASH(ctx, in, i);
  1599. # else
  1600. while (j--) {
  1601. size_t k;
  1602. for (k = 0; k < 16; ++k)
  1603. ctx->Xi.c[k] ^= in[k];
  1604. GCM_MUL(ctx, Xi);
  1605. in += 16;
  1606. }
  1607. j = i / 16;
  1608. in -= i;
  1609. # endif
  1610. (*stream) (in, out, j, key, ctx->Yi.c);
  1611. ctr += (unsigned int)j;
  1612. if (is_endian.little)
  1613. # ifdef BSWAP4
  1614. ctx->Yi.d[3] = BSWAP4(ctr);
  1615. # else
  1616. PUTU32(ctx->Yi.c + 12, ctr);
  1617. # endif
  1618. else
  1619. ctx->Yi.d[3] = ctr;
  1620. out += i;
  1621. in += i;
  1622. len -= i;
  1623. }
  1624. if (len) {
  1625. (*ctx->block) (ctx->Yi.c, ctx->EKi.c, key);
  1626. ++ctr;
  1627. if (is_endian.little)
  1628. # ifdef BSWAP4
  1629. ctx->Yi.d[3] = BSWAP4(ctr);
  1630. # else
  1631. PUTU32(ctx->Yi.c + 12, ctr);
  1632. # endif
  1633. else
  1634. ctx->Yi.d[3] = ctr;
  1635. while (len--) {
  1636. u8 c = in[n];
  1637. ctx->Xi.c[n] ^= c;
  1638. out[n] = c ^ ctx->EKi.c[n];
  1639. ++n;
  1640. }
  1641. }
  1642. ctx->mres = n;
  1643. return 0;
  1644. #endif
  1645. }
  1646. /* PCL UNUSED END */
  1647. int pcl_CRYPTO_gcm128_finish(GCM128_CONTEXT *ctx, const unsigned char *tag,
  1648. size_t len)
  1649. {
  1650. /* PCL UNUSED START *
  1651. const union {
  1652. long one;
  1653. char little;
  1654. } is_endian = { 1 };
  1655. /* PCL UNUSED END */
  1656. u64 alen = ctx->len.u[0] << 3;
  1657. u64 clen = ctx->len.u[1] << 3;
  1658. /* PCL UNUSED START *
  1659. #ifdef GCM_FUNCREF_4BIT
  1660. /* PCL UNUSED END */
  1661. void (*gcm_gmult_p) (u64 Xi[2], const u128 Htable[16]) = ctx->gmult;
  1662. /* PCL UNUSED START *
  1663. #endif
  1664. /* PCL UNUSED END */
  1665. if (ctx->mres || ctx->ares)
  1666. GCM_MUL(ctx, Xi);
  1667. /* PCL UNUSED START *
  1668. if (is_endian.little) {
  1669. #ifdef BSWAP8
  1670. /* PCL UNUSED END */
  1671. alen = BSWAP8(alen);
  1672. clen = BSWAP8(clen);
  1673. /* PCL UNUSED START *
  1674. #else
  1675. u8 *p = ctx->len.c;
  1676. ctx->len.u[0] = alen;
  1677. ctx->len.u[1] = clen;
  1678. alen = (u64)GETU32(p) << 32 | GETU32(p + 4);
  1679. clen = (u64)GETU32(p + 8) << 32 | GETU32(p + 12);
  1680. #endif
  1681. }
  1682. /* PCL UNUSED END */
  1683. ctx->Xi.u[0] ^= alen;
  1684. ctx->Xi.u[1] ^= clen;
  1685. GCM_MUL(ctx, Xi);
  1686. ctx->Xi.u[0] ^= ctx->EK0.u[0];
  1687. ctx->Xi.u[1] ^= ctx->EK0.u[1];
  1688. if (tag && len <= sizeof(ctx->Xi))
  1689. return !pcl_consttime_memequal(ctx->Xi.c, (void*)tag, len);
  1690. else
  1691. return -1;
  1692. }
  1693. /* PCL UNUSED START *
  1694. void CRYPTO_gcm128_tag(GCM128_CONTEXT *ctx, unsigned char *tag, size_t len)
  1695. {
  1696. CRYPTO_gcm128_finish(ctx, NULL, 0);
  1697. memcpy(tag, ctx->Xi.c,
  1698. len <= sizeof(ctx->Xi.c) ? len : sizeof(ctx->Xi.c));
  1699. }
  1700. GCM128_CONTEXT *CRYPTO_gcm128_new(void *key, block128_f block)
  1701. {
  1702. GCM128_CONTEXT *ret;
  1703. if ((ret = OPENSSL_malloc(sizeof(*ret))) != NULL)
  1704. CRYPTO_gcm128_init(ret, key, block);
  1705. return ret;
  1706. }
  1707. void CRYPTO_gcm128_release(GCM128_CONTEXT *ctx)
  1708. {
  1709. OPENSSL_clear_free(ctx, sizeof(*ctx));
  1710. }
  1711. #if defined(SELFTEST)
  1712. # include <stdio.h>
  1713. # include <openssl/aes.h>
  1714. /* Test Case 1 * /
  1715. static const u8 K1[16], *P1 = NULL, *A1 = NULL, IV1[12], *C1 = NULL;
  1716. static const u8 T1[] = {
  1717. 0x58, 0xe2, 0xfc, 0xce, 0xfa, 0x7e, 0x30, 0x61,
  1718. 0x36, 0x7f, 0x1d, 0x57, 0xa4, 0xe7, 0x45, 0x5a
  1719. };
  1720. /* Test Case 2 * /
  1721. # define K2 K1
  1722. # define A2 A1
  1723. # define IV2 IV1
  1724. static const u8 P2[16];
  1725. static const u8 C2[] = {
  1726. 0x03, 0x88, 0xda, 0xce, 0x60, 0xb6, 0xa3, 0x92,
  1727. 0xf3, 0x28, 0xc2, 0xb9, 0x71, 0xb2, 0xfe, 0x78
  1728. };
  1729. static const u8 T2[] = {
  1730. 0xab, 0x6e, 0x47, 0xd4, 0x2c, 0xec, 0x13, 0xbd,
  1731. 0xf5, 0x3a, 0x67, 0xb2, 0x12, 0x57, 0xbd, 0xdf
  1732. };
  1733. /* Test Case 3 * /
  1734. # define A3 A2
  1735. static const u8 K3[] = {
  1736. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  1737. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08
  1738. };
  1739. static const u8 P3[] = {
  1740. 0xd9, 0x31, 0x32, 0x25, 0xf8, 0x84, 0x06, 0xe5,
  1741. 0xa5, 0x59, 0x09, 0xc5, 0xaf, 0xf5, 0x26, 0x9a,
  1742. 0x86, 0xa7, 0xa9, 0x53, 0x15, 0x34, 0xf7, 0xda,
  1743. 0x2e, 0x4c, 0x30, 0x3d, 0x8a, 0x31, 0x8a, 0x72,
  1744. 0x1c, 0x3c, 0x0c, 0x95, 0x95, 0x68, 0x09, 0x53,
  1745. 0x2f, 0xcf, 0x0e, 0x24, 0x49, 0xa6, 0xb5, 0x25,
  1746. 0xb1, 0x6a, 0xed, 0xf5, 0xaa, 0x0d, 0xe6, 0x57,
  1747. 0xba, 0x63, 0x7b, 0x39, 0x1a, 0xaf, 0xd2, 0x55
  1748. };
  1749. static const u8 IV3[] = {
  1750. 0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce, 0xdb, 0xad,
  1751. 0xde, 0xca, 0xf8, 0x88
  1752. };
  1753. static const u8 C3[] = {
  1754. 0x42, 0x83, 0x1e, 0xc2, 0x21, 0x77, 0x74, 0x24,
  1755. 0x4b, 0x72, 0x21, 0xb7, 0x84, 0xd0, 0xd4, 0x9c,
  1756. 0xe3, 0xaa, 0x21, 0x2f, 0x2c, 0x02, 0xa4, 0xe0,
  1757. 0x35, 0xc1, 0x7e, 0x23, 0x29, 0xac, 0xa1, 0x2e,
  1758. 0x21, 0xd5, 0x14, 0xb2, 0x54, 0x66, 0x93, 0x1c,
  1759. 0x7d, 0x8f, 0x6a, 0x5a, 0xac, 0x84, 0xaa, 0x05,
  1760. 0x1b, 0xa3, 0x0b, 0x39, 0x6a, 0x0a, 0xac, 0x97,
  1761. 0x3d, 0x58, 0xe0, 0x91, 0x47, 0x3f, 0x59, 0x85
  1762. };
  1763. static const u8 T3[] = {
  1764. 0x4d, 0x5c, 0x2a, 0xf3, 0x27, 0xcd, 0x64, 0xa6,
  1765. 0x2c, 0xf3, 0x5a, 0xbd, 0x2b, 0xa6, 0xfa, 0xb4
  1766. };
  1767. /* Test Case 4 * /
  1768. # define K4 K3
  1769. # define IV4 IV3
  1770. static const u8 P4[] = {
  1771. 0xd9, 0x31, 0x32, 0x25, 0xf8, 0x84, 0x06, 0xe5,
  1772. 0xa5, 0x59, 0x09, 0xc5, 0xaf, 0xf5, 0x26, 0x9a,
  1773. 0x86, 0xa7, 0xa9, 0x53, 0x15, 0x34, 0xf7, 0xda,
  1774. 0x2e, 0x4c, 0x30, 0x3d, 0x8a, 0x31, 0x8a, 0x72,
  1775. 0x1c, 0x3c, 0x0c, 0x95, 0x95, 0x68, 0x09, 0x53,
  1776. 0x2f, 0xcf, 0x0e, 0x24, 0x49, 0xa6, 0xb5, 0x25,
  1777. 0xb1, 0x6a, 0xed, 0xf5, 0xaa, 0x0d, 0xe6, 0x57,
  1778. 0xba, 0x63, 0x7b, 0x39
  1779. };
  1780. static const u8 A4[] = {
  1781. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  1782. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  1783. 0xab, 0xad, 0xda, 0xd2
  1784. };
  1785. static const u8 C4[] = {
  1786. 0x42, 0x83, 0x1e, 0xc2, 0x21, 0x77, 0x74, 0x24,
  1787. 0x4b, 0x72, 0x21, 0xb7, 0x84, 0xd0, 0xd4, 0x9c,
  1788. 0xe3, 0xaa, 0x21, 0x2f, 0x2c, 0x02, 0xa4, 0xe0,
  1789. 0x35, 0xc1, 0x7e, 0x23, 0x29, 0xac, 0xa1, 0x2e,
  1790. 0x21, 0xd5, 0x14, 0xb2, 0x54, 0x66, 0x93, 0x1c,
  1791. 0x7d, 0x8f, 0x6a, 0x5a, 0xac, 0x84, 0xaa, 0x05,
  1792. 0x1b, 0xa3, 0x0b, 0x39, 0x6a, 0x0a, 0xac, 0x97,
  1793. 0x3d, 0x58, 0xe0, 0x91
  1794. };
  1795. static const u8 T4[] = {
  1796. 0x5b, 0xc9, 0x4f, 0xbc, 0x32, 0x21, 0xa5, 0xdb,
  1797. 0x94, 0xfa, 0xe9, 0x5a, 0xe7, 0x12, 0x1a, 0x47
  1798. };
  1799. /* Test Case 5 * /
  1800. # define K5 K4
  1801. # define P5 P4
  1802. # define A5 A4
  1803. static const u8 IV5[] = {
  1804. 0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce, 0xdb, 0xad
  1805. };
  1806. static const u8 C5[] = {
  1807. 0x61, 0x35, 0x3b, 0x4c, 0x28, 0x06, 0x93, 0x4a,
  1808. 0x77, 0x7f, 0xf5, 0x1f, 0xa2, 0x2a, 0x47, 0x55,
  1809. 0x69, 0x9b, 0x2a, 0x71, 0x4f, 0xcd, 0xc6, 0xf8,
  1810. 0x37, 0x66, 0xe5, 0xf9, 0x7b, 0x6c, 0x74, 0x23,
  1811. 0x73, 0x80, 0x69, 0x00, 0xe4, 0x9f, 0x24, 0xb2,
  1812. 0x2b, 0x09, 0x75, 0x44, 0xd4, 0x89, 0x6b, 0x42,
  1813. 0x49, 0x89, 0xb5, 0xe1, 0xeb, 0xac, 0x0f, 0x07,
  1814. 0xc2, 0x3f, 0x45, 0x98
  1815. };
  1816. static const u8 T5[] = {
  1817. 0x36, 0x12, 0xd2, 0xe7, 0x9e, 0x3b, 0x07, 0x85,
  1818. 0x56, 0x1b, 0xe1, 0x4a, 0xac, 0xa2, 0xfc, 0xcb
  1819. };
  1820. /* Test Case 6 * /
  1821. # define K6 K5
  1822. # define P6 P5
  1823. # define A6 A5
  1824. static const u8 IV6[] = {
  1825. 0x93, 0x13, 0x22, 0x5d, 0xf8, 0x84, 0x06, 0xe5,
  1826. 0x55, 0x90, 0x9c, 0x5a, 0xff, 0x52, 0x69, 0xaa,
  1827. 0x6a, 0x7a, 0x95, 0x38, 0x53, 0x4f, 0x7d, 0xa1,
  1828. 0xe4, 0xc3, 0x03, 0xd2, 0xa3, 0x18, 0xa7, 0x28,
  1829. 0xc3, 0xc0, 0xc9, 0x51, 0x56, 0x80, 0x95, 0x39,
  1830. 0xfc, 0xf0, 0xe2, 0x42, 0x9a, 0x6b, 0x52, 0x54,
  1831. 0x16, 0xae, 0xdb, 0xf5, 0xa0, 0xde, 0x6a, 0x57,
  1832. 0xa6, 0x37, 0xb3, 0x9b
  1833. };
  1834. static const u8 C6[] = {
  1835. 0x8c, 0xe2, 0x49, 0x98, 0x62, 0x56, 0x15, 0xb6,
  1836. 0x03, 0xa0, 0x33, 0xac, 0xa1, 0x3f, 0xb8, 0x94,
  1837. 0xbe, 0x91, 0x12, 0xa5, 0xc3, 0xa2, 0x11, 0xa8,
  1838. 0xba, 0x26, 0x2a, 0x3c, 0xca, 0x7e, 0x2c, 0xa7,
  1839. 0x01, 0xe4, 0xa9, 0xa4, 0xfb, 0xa4, 0x3c, 0x90,
  1840. 0xcc, 0xdc, 0xb2, 0x81, 0xd4, 0x8c, 0x7c, 0x6f,
  1841. 0xd6, 0x28, 0x75, 0xd2, 0xac, 0xa4, 0x17, 0x03,
  1842. 0x4c, 0x34, 0xae, 0xe5
  1843. };
  1844. static const u8 T6[] = {
  1845. 0x61, 0x9c, 0xc5, 0xae, 0xff, 0xfe, 0x0b, 0xfa,
  1846. 0x46, 0x2a, 0xf4, 0x3c, 0x16, 0x99, 0xd0, 0x50
  1847. };
  1848. /* Test Case 7 * /
  1849. static const u8 K7[24], *P7 = NULL, *A7 = NULL, IV7[12], *C7 = NULL;
  1850. static const u8 T7[] = {
  1851. 0xcd, 0x33, 0xb2, 0x8a, 0xc7, 0x73, 0xf7, 0x4b,
  1852. 0xa0, 0x0e, 0xd1, 0xf3, 0x12, 0x57, 0x24, 0x35
  1853. };
  1854. /* Test Case 8 * /
  1855. # define K8 K7
  1856. # define IV8 IV7
  1857. # define A8 A7
  1858. static const u8 P8[16];
  1859. static const u8 C8[] = {
  1860. 0x98, 0xe7, 0x24, 0x7c, 0x07, 0xf0, 0xfe, 0x41,
  1861. 0x1c, 0x26, 0x7e, 0x43, 0x84, 0xb0, 0xf6, 0x00
  1862. };
  1863. static const u8 T8[] = {
  1864. 0x2f, 0xf5, 0x8d, 0x80, 0x03, 0x39, 0x27, 0xab,
  1865. 0x8e, 0xf4, 0xd4, 0x58, 0x75, 0x14, 0xf0, 0xfb
  1866. };
  1867. /* Test Case 9 * /
  1868. # define A9 A8
  1869. static const u8 K9[] = {
  1870. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  1871. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08,
  1872. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c
  1873. };
  1874. static const u8 P9[] = {
  1875. 0xd9, 0x31, 0x32, 0x25, 0xf8, 0x84, 0x06, 0xe5,
  1876. 0xa5, 0x59, 0x09, 0xc5, 0xaf, 0xf5, 0x26, 0x9a,
  1877. 0x86, 0xa7, 0xa9, 0x53, 0x15, 0x34, 0xf7, 0xda,
  1878. 0x2e, 0x4c, 0x30, 0x3d, 0x8a, 0x31, 0x8a, 0x72,
  1879. 0x1c, 0x3c, 0x0c, 0x95, 0x95, 0x68, 0x09, 0x53,
  1880. 0x2f, 0xcf, 0x0e, 0x24, 0x49, 0xa6, 0xb5, 0x25,
  1881. 0xb1, 0x6a, 0xed, 0xf5, 0xaa, 0x0d, 0xe6, 0x57,
  1882. 0xba, 0x63, 0x7b, 0x39, 0x1a, 0xaf, 0xd2, 0x55
  1883. };
  1884. static const u8 IV9[] = {
  1885. 0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce, 0xdb, 0xad,
  1886. 0xde, 0xca, 0xf8, 0x88
  1887. };
  1888. static const u8 C9[] = {
  1889. 0x39, 0x80, 0xca, 0x0b, 0x3c, 0x00, 0xe8, 0x41,
  1890. 0xeb, 0x06, 0xfa, 0xc4, 0x87, 0x2a, 0x27, 0x57,
  1891. 0x85, 0x9e, 0x1c, 0xea, 0xa6, 0xef, 0xd9, 0x84,
  1892. 0x62, 0x85, 0x93, 0xb4, 0x0c, 0xa1, 0xe1, 0x9c,
  1893. 0x7d, 0x77, 0x3d, 0x00, 0xc1, 0x44, 0xc5, 0x25,
  1894. 0xac, 0x61, 0x9d, 0x18, 0xc8, 0x4a, 0x3f, 0x47,
  1895. 0x18, 0xe2, 0x44, 0x8b, 0x2f, 0xe3, 0x24, 0xd9,
  1896. 0xcc, 0xda, 0x27, 0x10, 0xac, 0xad, 0xe2, 0x56
  1897. };
  1898. static const u8 T9[] = {
  1899. 0x99, 0x24, 0xa7, 0xc8, 0x58, 0x73, 0x36, 0xbf,
  1900. 0xb1, 0x18, 0x02, 0x4d, 0xb8, 0x67, 0x4a, 0x14
  1901. };
  1902. /* Test Case 10 * /
  1903. # define K10 K9
  1904. # define IV10 IV9
  1905. static const u8 P10[] = {
  1906. 0xd9, 0x31, 0x32, 0x25, 0xf8, 0x84, 0x06, 0xe5,
  1907. 0xa5, 0x59, 0x09, 0xc5, 0xaf, 0xf5, 0x26, 0x9a,
  1908. 0x86, 0xa7, 0xa9, 0x53, 0x15, 0x34, 0xf7, 0xda,
  1909. 0x2e, 0x4c, 0x30, 0x3d, 0x8a, 0x31, 0x8a, 0x72,
  1910. 0x1c, 0x3c, 0x0c, 0x95, 0x95, 0x68, 0x09, 0x53,
  1911. 0x2f, 0xcf, 0x0e, 0x24, 0x49, 0xa6, 0xb5, 0x25,
  1912. 0xb1, 0x6a, 0xed, 0xf5, 0xaa, 0x0d, 0xe6, 0x57,
  1913. 0xba, 0x63, 0x7b, 0x39
  1914. };
  1915. static const u8 A10[] = {
  1916. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  1917. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  1918. 0xab, 0xad, 0xda, 0xd2
  1919. };
  1920. static const u8 C10[] = {
  1921. 0x39, 0x80, 0xca, 0x0b, 0x3c, 0x00, 0xe8, 0x41,
  1922. 0xeb, 0x06, 0xfa, 0xc4, 0x87, 0x2a, 0x27, 0x57,
  1923. 0x85, 0x9e, 0x1c, 0xea, 0xa6, 0xef, 0xd9, 0x84,
  1924. 0x62, 0x85, 0x93, 0xb4, 0x0c, 0xa1, 0xe1, 0x9c,
  1925. 0x7d, 0x77, 0x3d, 0x00, 0xc1, 0x44, 0xc5, 0x25,
  1926. 0xac, 0x61, 0x9d, 0x18, 0xc8, 0x4a, 0x3f, 0x47,
  1927. 0x18, 0xe2, 0x44, 0x8b, 0x2f, 0xe3, 0x24, 0xd9,
  1928. 0xcc, 0xda, 0x27, 0x10
  1929. };
  1930. static const u8 T10[] = {
  1931. 0x25, 0x19, 0x49, 0x8e, 0x80, 0xf1, 0x47, 0x8f,
  1932. 0x37, 0xba, 0x55, 0xbd, 0x6d, 0x27, 0x61, 0x8c
  1933. };
  1934. /* Test Case 11 * /
  1935. # define K11 K10
  1936. # define P11 P10
  1937. # define A11 A10
  1938. static const u8 IV11[] = { 0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce, 0xdb, 0xad };
  1939. static const u8 C11[] = {
  1940. 0x0f, 0x10, 0xf5, 0x99, 0xae, 0x14, 0xa1, 0x54,
  1941. 0xed, 0x24, 0xb3, 0x6e, 0x25, 0x32, 0x4d, 0xb8,
  1942. 0xc5, 0x66, 0x63, 0x2e, 0xf2, 0xbb, 0xb3, 0x4f,
  1943. 0x83, 0x47, 0x28, 0x0f, 0xc4, 0x50, 0x70, 0x57,
  1944. 0xfd, 0xdc, 0x29, 0xdf, 0x9a, 0x47, 0x1f, 0x75,
  1945. 0xc6, 0x65, 0x41, 0xd4, 0xd4, 0xda, 0xd1, 0xc9,
  1946. 0xe9, 0x3a, 0x19, 0xa5, 0x8e, 0x8b, 0x47, 0x3f,
  1947. 0xa0, 0xf0, 0x62, 0xf7
  1948. };
  1949. static const u8 T11[] = {
  1950. 0x65, 0xdc, 0xc5, 0x7f, 0xcf, 0x62, 0x3a, 0x24,
  1951. 0x09, 0x4f, 0xcc, 0xa4, 0x0d, 0x35, 0x33, 0xf8
  1952. };
  1953. /* Test Case 12 * /
  1954. # define K12 K11
  1955. # define P12 P11
  1956. # define A12 A11
  1957. static const u8 IV12[] = {
  1958. 0x93, 0x13, 0x22, 0x5d, 0xf8, 0x84, 0x06, 0xe5,
  1959. 0x55, 0x90, 0x9c, 0x5a, 0xff, 0x52, 0x69, 0xaa,
  1960. 0x6a, 0x7a, 0x95, 0x38, 0x53, 0x4f, 0x7d, 0xa1,
  1961. 0xe4, 0xc3, 0x03, 0xd2, 0xa3, 0x18, 0xa7, 0x28,
  1962. 0xc3, 0xc0, 0xc9, 0x51, 0x56, 0x80, 0x95, 0x39,
  1963. 0xfc, 0xf0, 0xe2, 0x42, 0x9a, 0x6b, 0x52, 0x54,
  1964. 0x16, 0xae, 0xdb, 0xf5, 0xa0, 0xde, 0x6a, 0x57,
  1965. 0xa6, 0x37, 0xb3, 0x9b
  1966. };
  1967. static const u8 C12[] = {
  1968. 0xd2, 0x7e, 0x88, 0x68, 0x1c, 0xe3, 0x24, 0x3c,
  1969. 0x48, 0x30, 0x16, 0x5a, 0x8f, 0xdc, 0xf9, 0xff,
  1970. 0x1d, 0xe9, 0xa1, 0xd8, 0xe6, 0xb4, 0x47, 0xef,
  1971. 0x6e, 0xf7, 0xb7, 0x98, 0x28, 0x66, 0x6e, 0x45,
  1972. 0x81, 0xe7, 0x90, 0x12, 0xaf, 0x34, 0xdd, 0xd9,
  1973. 0xe2, 0xf0, 0x37, 0x58, 0x9b, 0x29, 0x2d, 0xb3,
  1974. 0xe6, 0x7c, 0x03, 0x67, 0x45, 0xfa, 0x22, 0xe7,
  1975. 0xe9, 0xb7, 0x37, 0x3b
  1976. };
  1977. static const u8 T12[] = {
  1978. 0xdc, 0xf5, 0x66, 0xff, 0x29, 0x1c, 0x25, 0xbb,
  1979. 0xb8, 0x56, 0x8f, 0xc3, 0xd3, 0x76, 0xa6, 0xd9
  1980. };
  1981. /* Test Case 13 * /
  1982. static const u8 K13[32], *P13 = NULL, *A13 = NULL, IV13[12], *C13 = NULL;
  1983. static const u8 T13[] = {
  1984. 0x53, 0x0f, 0x8a, 0xfb, 0xc7, 0x45, 0x36, 0xb9,
  1985. 0xa9, 0x63, 0xb4, 0xf1, 0xc4, 0xcb, 0x73, 0x8b
  1986. };
  1987. /* Test Case 14 * /
  1988. # define K14 K13
  1989. # define A14 A13
  1990. static const u8 P14[16], IV14[12];
  1991. static const u8 C14[] = {
  1992. 0xce, 0xa7, 0x40, 0x3d, 0x4d, 0x60, 0x6b, 0x6e,
  1993. 0x07, 0x4e, 0xc5, 0xd3, 0xba, 0xf3, 0x9d, 0x18
  1994. };
  1995. static const u8 T14[] = {
  1996. 0xd0, 0xd1, 0xc8, 0xa7, 0x99, 0x99, 0x6b, 0xf0,
  1997. 0x26, 0x5b, 0x98, 0xb5, 0xd4, 0x8a, 0xb9, 0x19
  1998. };
  1999. /* Test Case 15 * /
  2000. # define A15 A14
  2001. static const u8 K15[] = {
  2002. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  2003. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08,
  2004. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  2005. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08
  2006. };
  2007. static const u8 P15[] = {
  2008. 0xd9, 0x31, 0x32, 0x25, 0xf8, 0x84, 0x06, 0xe5,
  2009. 0xa5, 0x59, 0x09, 0xc5, 0xaf, 0xf5, 0x26, 0x9a,
  2010. 0x86, 0xa7, 0xa9, 0x53, 0x15, 0x34, 0xf7, 0xda,
  2011. 0x2e, 0x4c, 0x30, 0x3d, 0x8a, 0x31, 0x8a, 0x72,
  2012. 0x1c, 0x3c, 0x0c, 0x95, 0x95, 0x68, 0x09, 0x53,
  2013. 0x2f, 0xcf, 0x0e, 0x24, 0x49, 0xa6, 0xb5, 0x25,
  2014. 0xb1, 0x6a, 0xed, 0xf5, 0xaa, 0x0d, 0xe6, 0x57,
  2015. 0xba, 0x63, 0x7b, 0x39, 0x1a, 0xaf, 0xd2, 0x55
  2016. };
  2017. static const u8 IV15[] = {
  2018. 0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce, 0xdb, 0xad,
  2019. 0xde, 0xca, 0xf8, 0x88
  2020. };
  2021. static const u8 C15[] = {
  2022. 0x52, 0x2d, 0xc1, 0xf0, 0x99, 0x56, 0x7d, 0x07,
  2023. 0xf4, 0x7f, 0x37, 0xa3, 0x2a, 0x84, 0x42, 0x7d,
  2024. 0x64, 0x3a, 0x8c, 0xdc, 0xbf, 0xe5, 0xc0, 0xc9,
  2025. 0x75, 0x98, 0xa2, 0xbd, 0x25, 0x55, 0xd1, 0xaa,
  2026. 0x8c, 0xb0, 0x8e, 0x48, 0x59, 0x0d, 0xbb, 0x3d,
  2027. 0xa7, 0xb0, 0x8b, 0x10, 0x56, 0x82, 0x88, 0x38,
  2028. 0xc5, 0xf6, 0x1e, 0x63, 0x93, 0xba, 0x7a, 0x0a,
  2029. 0xbc, 0xc9, 0xf6, 0x62, 0x89, 0x80, 0x15, 0xad
  2030. };
  2031. static const u8 T15[] = {
  2032. 0xb0, 0x94, 0xda, 0xc5, 0xd9, 0x34, 0x71, 0xbd,
  2033. 0xec, 0x1a, 0x50, 0x22, 0x70, 0xe3, 0xcc, 0x6c
  2034. };
  2035. /* Test Case 16 * /
  2036. # define K16 K15
  2037. # define IV16 IV15
  2038. static const u8 P16[] = {
  2039. 0xd9, 0x31, 0x32, 0x25, 0xf8, 0x84, 0x06, 0xe5,
  2040. 0xa5, 0x59, 0x09, 0xc5, 0xaf, 0xf5, 0x26, 0x9a,
  2041. 0x86, 0xa7, 0xa9, 0x53, 0x15, 0x34, 0xf7, 0xda,
  2042. 0x2e, 0x4c, 0x30, 0x3d, 0x8a, 0x31, 0x8a, 0x72,
  2043. 0x1c, 0x3c, 0x0c, 0x95, 0x95, 0x68, 0x09, 0x53,
  2044. 0x2f, 0xcf, 0x0e, 0x24, 0x49, 0xa6, 0xb5, 0x25,
  2045. 0xb1, 0x6a, 0xed, 0xf5, 0xaa, 0x0d, 0xe6, 0x57,
  2046. 0xba, 0x63, 0x7b, 0x39
  2047. };
  2048. static const u8 A16[] = {
  2049. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  2050. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  2051. 0xab, 0xad, 0xda, 0xd2
  2052. };
  2053. static const u8 C16[] = {
  2054. 0x52, 0x2d, 0xc1, 0xf0, 0x99, 0x56, 0x7d, 0x07,
  2055. 0xf4, 0x7f, 0x37, 0xa3, 0x2a, 0x84, 0x42, 0x7d,
  2056. 0x64, 0x3a, 0x8c, 0xdc, 0xbf, 0xe5, 0xc0, 0xc9,
  2057. 0x75, 0x98, 0xa2, 0xbd, 0x25, 0x55, 0xd1, 0xaa,
  2058. 0x8c, 0xb0, 0x8e, 0x48, 0x59, 0x0d, 0xbb, 0x3d,
  2059. 0xa7, 0xb0, 0x8b, 0x10, 0x56, 0x82, 0x88, 0x38,
  2060. 0xc5, 0xf6, 0x1e, 0x63, 0x93, 0xba, 0x7a, 0x0a,
  2061. 0xbc, 0xc9, 0xf6, 0x62
  2062. };
  2063. static const u8 T16[] = {
  2064. 0x76, 0xfc, 0x6e, 0xce, 0x0f, 0x4e, 0x17, 0x68,
  2065. 0xcd, 0xdf, 0x88, 0x53, 0xbb, 0x2d, 0x55, 0x1b
  2066. };
  2067. /* Test Case 17 * /
  2068. # define K17 K16
  2069. # define P17 P16
  2070. # define A17 A16
  2071. static const u8 IV17[] = { 0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce, 0xdb, 0xad };
  2072. static const u8 C17[] = {
  2073. 0xc3, 0x76, 0x2d, 0xf1, 0xca, 0x78, 0x7d, 0x32,
  2074. 0xae, 0x47, 0xc1, 0x3b, 0xf1, 0x98, 0x44, 0xcb,
  2075. 0xaf, 0x1a, 0xe1, 0x4d, 0x0b, 0x97, 0x6a, 0xfa,
  2076. 0xc5, 0x2f, 0xf7, 0xd7, 0x9b, 0xba, 0x9d, 0xe0,
  2077. 0xfe, 0xb5, 0x82, 0xd3, 0x39, 0x34, 0xa4, 0xf0,
  2078. 0x95, 0x4c, 0xc2, 0x36, 0x3b, 0xc7, 0x3f, 0x78,
  2079. 0x62, 0xac, 0x43, 0x0e, 0x64, 0xab, 0xe4, 0x99,
  2080. 0xf4, 0x7c, 0x9b, 0x1f
  2081. };
  2082. static const u8 T17[] = {
  2083. 0x3a, 0x33, 0x7d, 0xbf, 0x46, 0xa7, 0x92, 0xc4,
  2084. 0x5e, 0x45, 0x49, 0x13, 0xfe, 0x2e, 0xa8, 0xf2
  2085. };
  2086. /* Test Case 18 * /
  2087. # define K18 K17
  2088. # define P18 P17
  2089. # define A18 A17
  2090. static const u8 IV18[] = {
  2091. 0x93, 0x13, 0x22, 0x5d, 0xf8, 0x84, 0x06, 0xe5,
  2092. 0x55, 0x90, 0x9c, 0x5a, 0xff, 0x52, 0x69, 0xaa,
  2093. 0x6a, 0x7a, 0x95, 0x38, 0x53, 0x4f, 0x7d, 0xa1,
  2094. 0xe4, 0xc3, 0x03, 0xd2, 0xa3, 0x18, 0xa7, 0x28,
  2095. 0xc3, 0xc0, 0xc9, 0x51, 0x56, 0x80, 0x95, 0x39,
  2096. 0xfc, 0xf0, 0xe2, 0x42, 0x9a, 0x6b, 0x52, 0x54,
  2097. 0x16, 0xae, 0xdb, 0xf5, 0xa0, 0xde, 0x6a, 0x57,
  2098. 0xa6, 0x37, 0xb3, 0x9b
  2099. };
  2100. static const u8 C18[] = {
  2101. 0x5a, 0x8d, 0xef, 0x2f, 0x0c, 0x9e, 0x53, 0xf1,
  2102. 0xf7, 0x5d, 0x78, 0x53, 0x65, 0x9e, 0x2a, 0x20,
  2103. 0xee, 0xb2, 0xb2, 0x2a, 0xaf, 0xde, 0x64, 0x19,
  2104. 0xa0, 0x58, 0xab, 0x4f, 0x6f, 0x74, 0x6b, 0xf4,
  2105. 0x0f, 0xc0, 0xc3, 0xb7, 0x80, 0xf2, 0x44, 0x45,
  2106. 0x2d, 0xa3, 0xeb, 0xf1, 0xc5, 0xd8, 0x2c, 0xde,
  2107. 0xa2, 0x41, 0x89, 0x97, 0x20, 0x0e, 0xf8, 0x2e,
  2108. 0x44, 0xae, 0x7e, 0x3f
  2109. };
  2110. static const u8 T18[] = {
  2111. 0xa4, 0x4a, 0x82, 0x66, 0xee, 0x1c, 0x8e, 0xb0,
  2112. 0xc8, 0xb5, 0xd4, 0xcf, 0x5a, 0xe9, 0xf1, 0x9a
  2113. };
  2114. /* Test Case 19 * /
  2115. # define K19 K1
  2116. # define P19 P1
  2117. # define IV19 IV1
  2118. # define C19 C1
  2119. static const u8 A19[] = {
  2120. 0xd9, 0x31, 0x32, 0x25, 0xf8, 0x84, 0x06, 0xe5,
  2121. 0xa5, 0x59, 0x09, 0xc5, 0xaf, 0xf5, 0x26, 0x9a,
  2122. 0x86, 0xa7, 0xa9, 0x53, 0x15, 0x34, 0xf7, 0xda,
  2123. 0x2e, 0x4c, 0x30, 0x3d, 0x8a, 0x31, 0x8a, 0x72,
  2124. 0x1c, 0x3c, 0x0c, 0x95, 0x95, 0x68, 0x09, 0x53,
  2125. 0x2f, 0xcf, 0x0e, 0x24, 0x49, 0xa6, 0xb5, 0x25,
  2126. 0xb1, 0x6a, 0xed, 0xf5, 0xaa, 0x0d, 0xe6, 0x57,
  2127. 0xba, 0x63, 0x7b, 0x39, 0x1a, 0xaf, 0xd2, 0x55,
  2128. 0x52, 0x2d, 0xc1, 0xf0, 0x99, 0x56, 0x7d, 0x07,
  2129. 0xf4, 0x7f, 0x37, 0xa3, 0x2a, 0x84, 0x42, 0x7d,
  2130. 0x64, 0x3a, 0x8c, 0xdc, 0xbf, 0xe5, 0xc0, 0xc9,
  2131. 0x75, 0x98, 0xa2, 0xbd, 0x25, 0x55, 0xd1, 0xaa,
  2132. 0x8c, 0xb0, 0x8e, 0x48, 0x59, 0x0d, 0xbb, 0x3d,
  2133. 0xa7, 0xb0, 0x8b, 0x10, 0x56, 0x82, 0x88, 0x38,
  2134. 0xc5, 0xf6, 0x1e, 0x63, 0x93, 0xba, 0x7a, 0x0a,
  2135. 0xbc, 0xc9, 0xf6, 0x62, 0x89, 0x80, 0x15, 0xad
  2136. };
  2137. static const u8 T19[] = {
  2138. 0x5f, 0xea, 0x79, 0x3a, 0x2d, 0x6f, 0x97, 0x4d,
  2139. 0x37, 0xe6, 0x8e, 0x0c, 0xb8, 0xff, 0x94, 0x92
  2140. };
  2141. /* Test Case 20 * /
  2142. # define K20 K1
  2143. # define A20 A1
  2144. /* this results in 0xff in counter LSB * /
  2145. static const u8 IV20[64] = { 0xff, 0xff, 0xff, 0xff };
  2146. static const u8 P20[288];
  2147. static const u8 C20[] = {
  2148. 0x56, 0xb3, 0x37, 0x3c, 0xa9, 0xef, 0x6e, 0x4a,
  2149. 0x2b, 0x64, 0xfe, 0x1e, 0x9a, 0x17, 0xb6, 0x14,
  2150. 0x25, 0xf1, 0x0d, 0x47, 0xa7, 0x5a, 0x5f, 0xce,
  2151. 0x13, 0xef, 0xc6, 0xbc, 0x78, 0x4a, 0xf2, 0x4f,
  2152. 0x41, 0x41, 0xbd, 0xd4, 0x8c, 0xf7, 0xc7, 0x70,
  2153. 0x88, 0x7a, 0xfd, 0x57, 0x3c, 0xca, 0x54, 0x18,
  2154. 0xa9, 0xae, 0xff, 0xcd, 0x7c, 0x5c, 0xed, 0xdf,
  2155. 0xc6, 0xa7, 0x83, 0x97, 0xb9, 0xa8, 0x5b, 0x49,
  2156. 0x9d, 0xa5, 0x58, 0x25, 0x72, 0x67, 0xca, 0xab,
  2157. 0x2a, 0xd0, 0xb2, 0x3c, 0xa4, 0x76, 0xa5, 0x3c,
  2158. 0xb1, 0x7f, 0xb4, 0x1c, 0x4b, 0x8b, 0x47, 0x5c,
  2159. 0xb4, 0xf3, 0xf7, 0x16, 0x50, 0x94, 0xc2, 0x29,
  2160. 0xc9, 0xe8, 0xc4, 0xdc, 0x0a, 0x2a, 0x5f, 0xf1,
  2161. 0x90, 0x3e, 0x50, 0x15, 0x11, 0x22, 0x13, 0x76,
  2162. 0xa1, 0xcd, 0xb8, 0x36, 0x4c, 0x50, 0x61, 0xa2,
  2163. 0x0c, 0xae, 0x74, 0xbc, 0x4a, 0xcd, 0x76, 0xce,
  2164. 0xb0, 0xab, 0xc9, 0xfd, 0x32, 0x17, 0xef, 0x9f,
  2165. 0x8c, 0x90, 0xbe, 0x40, 0x2d, 0xdf, 0x6d, 0x86,
  2166. 0x97, 0xf4, 0xf8, 0x80, 0xdf, 0xf1, 0x5b, 0xfb,
  2167. 0x7a, 0x6b, 0x28, 0x24, 0x1e, 0xc8, 0xfe, 0x18,
  2168. 0x3c, 0x2d, 0x59, 0xe3, 0xf9, 0xdf, 0xff, 0x65,
  2169. 0x3c, 0x71, 0x26, 0xf0, 0xac, 0xb9, 0xe6, 0x42,
  2170. 0x11, 0xf4, 0x2b, 0xae, 0x12, 0xaf, 0x46, 0x2b,
  2171. 0x10, 0x70, 0xbe, 0xf1, 0xab, 0x5e, 0x36, 0x06,
  2172. 0x87, 0x2c, 0xa1, 0x0d, 0xee, 0x15, 0xb3, 0x24,
  2173. 0x9b, 0x1a, 0x1b, 0x95, 0x8f, 0x23, 0x13, 0x4c,
  2174. 0x4b, 0xcc, 0xb7, 0xd0, 0x32, 0x00, 0xbc, 0xe4,
  2175. 0x20, 0xa2, 0xf8, 0xeb, 0x66, 0xdc, 0xf3, 0x64,
  2176. 0x4d, 0x14, 0x23, 0xc1, 0xb5, 0x69, 0x90, 0x03,
  2177. 0xc1, 0x3e, 0xce, 0xf4, 0xbf, 0x38, 0xa3, 0xb6,
  2178. 0x0e, 0xed, 0xc3, 0x40, 0x33, 0xba, 0xc1, 0x90,
  2179. 0x27, 0x83, 0xdc, 0x6d, 0x89, 0xe2, 0xe7, 0x74,
  2180. 0x18, 0x8a, 0x43, 0x9c, 0x7e, 0xbc, 0xc0, 0x67,
  2181. 0x2d, 0xbd, 0xa4, 0xdd, 0xcf, 0xb2, 0x79, 0x46,
  2182. 0x13, 0xb0, 0xbe, 0x41, 0x31, 0x5e, 0xf7, 0x78,
  2183. 0x70, 0x8a, 0x70, 0xee, 0x7d, 0x75, 0x16, 0x5c
  2184. };
  2185. static const u8 T20[] = {
  2186. 0x8b, 0x30, 0x7f, 0x6b, 0x33, 0x28, 0x6d, 0x0a,
  2187. 0xb0, 0x26, 0xa9, 0xed, 0x3f, 0xe1, 0xe8, 0x5f
  2188. };
  2189. # define TEST_CASE(n) do { \
  2190. u8 out[sizeof(P##n)]; \
  2191. AES_set_encrypt_key(K##n,sizeof(K##n)*8,&key); \
  2192. CRYPTO_gcm128_init(&ctx,&key,(block128_f)AES_encrypt); \
  2193. CRYPTO_gcm128_setiv(&ctx,IV##n,sizeof(IV##n)); \
  2194. memset(out,0,sizeof(out)); \
  2195. if (A##n) CRYPTO_gcm128_aad(&ctx,A##n,sizeof(A##n)); \
  2196. if (P##n) CRYPTO_gcm128_encrypt(&ctx,P##n,out,sizeof(out)); \
  2197. if (CRYPTO_gcm128_finish(&ctx,T##n,16) || \
  2198. (C##n && memcmp(out,C##n,sizeof(out)))) \
  2199. ret++, printf ("encrypt test#%d failed.\n",n); \
  2200. CRYPTO_gcm128_setiv(&ctx,IV##n,sizeof(IV##n)); \
  2201. memset(out,0,sizeof(out)); \
  2202. if (A##n) CRYPTO_gcm128_aad(&ctx,A##n,sizeof(A##n)); \
  2203. if (C##n) CRYPTO_gcm128_decrypt(&ctx,C##n,out,sizeof(out)); \
  2204. if (CRYPTO_gcm128_finish(&ctx,T##n,16) || \
  2205. (P##n && memcmp(out,P##n,sizeof(out)))) \
  2206. ret++, printf ("decrypt test#%d failed.\n",n); \
  2207. } while(0)
  2208. int main()
  2209. {
  2210. GCM128_CONTEXT ctx;
  2211. AES_KEY key;
  2212. int ret = 0;
  2213. TEST_CASE(1);
  2214. TEST_CASE(2);
  2215. TEST_CASE(3);
  2216. TEST_CASE(4);
  2217. TEST_CASE(5);
  2218. TEST_CASE(6);
  2219. TEST_CASE(7);
  2220. TEST_CASE(8);
  2221. TEST_CASE(9);
  2222. TEST_CASE(10);
  2223. TEST_CASE(11);
  2224. TEST_CASE(12);
  2225. TEST_CASE(13);
  2226. TEST_CASE(14);
  2227. TEST_CASE(15);
  2228. TEST_CASE(16);
  2229. TEST_CASE(17);
  2230. TEST_CASE(18);
  2231. TEST_CASE(19);
  2232. TEST_CASE(20);
  2233. # ifdef OPENSSL_CPUID_OBJ
  2234. {
  2235. size_t start, stop, gcm_t, ctr_t, OPENSSL_rdtsc();
  2236. union {
  2237. u64 u;
  2238. u8 c[1024];
  2239. } buf;
  2240. int i;
  2241. AES_set_encrypt_key(K1, sizeof(K1) * 8, &key);
  2242. CRYPTO_gcm128_init(&ctx, &key, (block128_f) AES_encrypt);
  2243. CRYPTO_gcm128_setiv(&ctx, IV1, sizeof(IV1));
  2244. CRYPTO_gcm128_encrypt(&ctx, buf.c, buf.c, sizeof(buf));
  2245. start = OPENSSL_rdtsc();
  2246. CRYPTO_gcm128_encrypt(&ctx, buf.c, buf.c, sizeof(buf));
  2247. gcm_t = OPENSSL_rdtsc() - start;
  2248. CRYPTO_ctr128_encrypt(buf.c, buf.c, sizeof(buf),
  2249. &key, ctx.Yi.c, ctx.EKi.c, &ctx.mres,
  2250. (block128_f) AES_encrypt);
  2251. start = OPENSSL_rdtsc();
  2252. CRYPTO_ctr128_encrypt(buf.c, buf.c, sizeof(buf),
  2253. &key, ctx.Yi.c, ctx.EKi.c, &ctx.mres,
  2254. (block128_f) AES_encrypt);
  2255. ctr_t = OPENSSL_rdtsc() - start;
  2256. printf("%.2f-%.2f=%.2f\n",
  2257. gcm_t / (double)sizeof(buf),
  2258. ctr_t / (double)sizeof(buf),
  2259. (gcm_t - ctr_t) / (double)sizeof(buf));
  2260. # ifdef GHASH
  2261. {
  2262. void (*gcm_ghash_p) (u64 Xi[2], const u128 Htable[16],
  2263. const u8 *inp, size_t len) = ctx.ghash;
  2264. GHASH((&ctx), buf.c, sizeof(buf));
  2265. start = OPENSSL_rdtsc();
  2266. for (i = 0; i < 100; ++i)
  2267. GHASH((&ctx), buf.c, sizeof(buf));
  2268. gcm_t = OPENSSL_rdtsc() - start;
  2269. printf("%.2f\n", gcm_t / (double)sizeof(buf) / (double)i);
  2270. }
  2271. # endif
  2272. }
  2273. # endif
  2274. return ret;
  2275. }
  2276. #endif
  2277. /* PCL UNUSED END */