gcm128.c 72 KB

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