gcm128.c 72 KB

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