gcm128.c 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289
  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. * [email protected].
  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)
  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[2];
  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 GHASH_ASM_X86
  639. void gcm_gmult_4bit_mmx(u64 Xi[2], const u128 Htable[16]);
  640. void gcm_ghash_4bit_mmx(u64 Xi[2], const u128 Htable[16], const u8 *inp,
  641. size_t len);
  642. void gcm_gmult_4bit_x86(u64 Xi[2], const u128 Htable[16]);
  643. void gcm_ghash_4bit_x86(u64 Xi[2], const u128 Htable[16], const u8 *inp,
  644. size_t len);
  645. # endif
  646. # elif defined(__arm__) || defined(__arm)
  647. # include "arm_arch.h"
  648. # if __ARM_ARCH__>=7
  649. # define GHASH_ASM_ARM
  650. # define GCM_FUNCREF_4BIT
  651. void gcm_gmult_neon(u64 Xi[2], const u128 Htable[16]);
  652. void gcm_ghash_neon(u64 Xi[2], const u128 Htable[16], const u8 *inp,
  653. size_t len);
  654. # endif
  655. # endif
  656. #endif
  657. #ifdef GCM_FUNCREF_4BIT
  658. # undef GCM_MUL
  659. # define GCM_MUL(ctx,Xi) (*gcm_gmult_p)(ctx->Xi.u,ctx->Htable)
  660. # ifdef GHASH
  661. # undef GHASH
  662. # define GHASH(ctx,in,len) (*gcm_ghash_p)(ctx->Xi.u,ctx->Htable,in,len)
  663. # endif
  664. #endif
  665. void CRYPTO_gcm128_init(GCM128_CONTEXT *ctx, void *key, block128_f block)
  666. {
  667. const union {
  668. long one;
  669. char little;
  670. } is_endian = {
  671. 1
  672. };
  673. memset(ctx, 0, sizeof(*ctx));
  674. ctx->block = block;
  675. ctx->key = key;
  676. (*block) (ctx->H.c, ctx->H.c, key);
  677. if (is_endian.little) {
  678. /* H is stored in host byte order */
  679. #ifdef BSWAP8
  680. ctx->H.u[0] = BSWAP8(ctx->H.u[0]);
  681. ctx->H.u[1] = BSWAP8(ctx->H.u[1]);
  682. #else
  683. u8 *p = ctx->H.c;
  684. u64 hi, lo;
  685. hi = (u64)GETU32(p) << 32 | GETU32(p + 4);
  686. lo = (u64)GETU32(p + 8) << 32 | GETU32(p + 12);
  687. ctx->H.u[0] = hi;
  688. ctx->H.u[1] = lo;
  689. #endif
  690. }
  691. #if TABLE_BITS==8
  692. gcm_init_8bit(ctx->Htable, ctx->H.u);
  693. #elif TABLE_BITS==4
  694. # if defined(GHASH_ASM_X86_OR_64)
  695. # if !defined(GHASH_ASM_X86) || defined(OPENSSL_IA32_SSE2)
  696. if (OPENSSL_ia32cap_P[0] & (1 << 24) && /* check FXSR bit */
  697. OPENSSL_ia32cap_P[1] & (1 << 1)) { /* check PCLMULQDQ bit */
  698. gcm_init_clmul(ctx->Htable, ctx->H.u);
  699. ctx->gmult = gcm_gmult_clmul;
  700. ctx->ghash = gcm_ghash_clmul;
  701. return;
  702. }
  703. # endif
  704. gcm_init_4bit(ctx->Htable, ctx->H.u);
  705. # if defined(GHASH_ASM_X86) /* x86 only */
  706. # if defined(OPENSSL_IA32_SSE2)
  707. if (OPENSSL_ia32cap_P[0] & (1 << 25)) { /* check SSE bit */
  708. # else
  709. if (OPENSSL_ia32cap_P[0] & (1 << 23)) { /* check MMX bit */
  710. # endif
  711. ctx->gmult = gcm_gmult_4bit_mmx;
  712. ctx->ghash = gcm_ghash_4bit_mmx;
  713. } else {
  714. ctx->gmult = gcm_gmult_4bit_x86;
  715. ctx->ghash = gcm_ghash_4bit_x86;
  716. }
  717. # else
  718. ctx->gmult = gcm_gmult_4bit;
  719. ctx->ghash = gcm_ghash_4bit;
  720. # endif
  721. # elif defined(GHASH_ASM_ARM)
  722. if (OPENSSL_armcap_P & ARMV7_NEON) {
  723. ctx->gmult = gcm_gmult_neon;
  724. ctx->ghash = gcm_ghash_neon;
  725. } else {
  726. gcm_init_4bit(ctx->Htable, ctx->H.u);
  727. ctx->gmult = gcm_gmult_4bit;
  728. ctx->ghash = gcm_ghash_4bit;
  729. }
  730. # else
  731. gcm_init_4bit(ctx->Htable, ctx->H.u);
  732. # endif
  733. #endif
  734. }
  735. void CRYPTO_gcm128_setiv(GCM128_CONTEXT *ctx, const unsigned char *iv,
  736. size_t len)
  737. {
  738. const union {
  739. long one;
  740. char little;
  741. } is_endian = {
  742. 1
  743. };
  744. unsigned int ctr;
  745. #ifdef GCM_FUNCREF_4BIT
  746. void (*gcm_gmult_p) (u64 Xi[2], const u128 Htable[16]) = ctx->gmult;
  747. #endif
  748. ctx->Yi.u[0] = 0;
  749. ctx->Yi.u[1] = 0;
  750. ctx->Xi.u[0] = 0;
  751. ctx->Xi.u[1] = 0;
  752. ctx->len.u[0] = 0; /* AAD length */
  753. ctx->len.u[1] = 0; /* message length */
  754. ctx->ares = 0;
  755. ctx->mres = 0;
  756. if (len == 12) {
  757. memcpy(ctx->Yi.c, iv, 12);
  758. ctx->Yi.c[15] = 1;
  759. ctr = 1;
  760. } else {
  761. size_t i;
  762. u64 len0 = len;
  763. while (len >= 16) {
  764. for (i = 0; i < 16; ++i)
  765. ctx->Yi.c[i] ^= iv[i];
  766. GCM_MUL(ctx, Yi);
  767. iv += 16;
  768. len -= 16;
  769. }
  770. if (len) {
  771. for (i = 0; i < len; ++i)
  772. ctx->Yi.c[i] ^= iv[i];
  773. GCM_MUL(ctx, Yi);
  774. }
  775. len0 <<= 3;
  776. if (is_endian.little) {
  777. #ifdef BSWAP8
  778. ctx->Yi.u[1] ^= BSWAP8(len0);
  779. #else
  780. ctx->Yi.c[8] ^= (u8)(len0 >> 56);
  781. ctx->Yi.c[9] ^= (u8)(len0 >> 48);
  782. ctx->Yi.c[10] ^= (u8)(len0 >> 40);
  783. ctx->Yi.c[11] ^= (u8)(len0 >> 32);
  784. ctx->Yi.c[12] ^= (u8)(len0 >> 24);
  785. ctx->Yi.c[13] ^= (u8)(len0 >> 16);
  786. ctx->Yi.c[14] ^= (u8)(len0 >> 8);
  787. ctx->Yi.c[15] ^= (u8)(len0);
  788. #endif
  789. } else
  790. ctx->Yi.u[1] ^= len0;
  791. GCM_MUL(ctx, Yi);
  792. if (is_endian.little)
  793. #ifdef BSWAP4
  794. ctr = BSWAP4(ctx->Yi.d[3]);
  795. #else
  796. ctr = GETU32(ctx->Yi.c + 12);
  797. #endif
  798. else
  799. ctr = ctx->Yi.d[3];
  800. }
  801. (*ctx->block) (ctx->Yi.c, ctx->EK0.c, ctx->key);
  802. ++ctr;
  803. if (is_endian.little)
  804. #ifdef BSWAP4
  805. ctx->Yi.d[3] = BSWAP4(ctr);
  806. #else
  807. PUTU32(ctx->Yi.c + 12, ctr);
  808. #endif
  809. else
  810. ctx->Yi.d[3] = ctr;
  811. }
  812. int CRYPTO_gcm128_aad(GCM128_CONTEXT *ctx, const unsigned char *aad,
  813. size_t len)
  814. {
  815. size_t i;
  816. unsigned int n;
  817. u64 alen = ctx->len.u[0];
  818. #ifdef GCM_FUNCREF_4BIT
  819. void (*gcm_gmult_p) (u64 Xi[2], const u128 Htable[16]) = ctx->gmult;
  820. # ifdef GHASH
  821. void (*gcm_ghash_p) (u64 Xi[2], const u128 Htable[16],
  822. const u8 *inp, size_t len) = ctx->ghash;
  823. # endif
  824. #endif
  825. if (ctx->len.u[1])
  826. return -2;
  827. alen += len;
  828. if (alen > (U64(1) << 61) || (sizeof(len) == 8 && alen < len))
  829. return -1;
  830. ctx->len.u[0] = alen;
  831. n = ctx->ares;
  832. if (n) {
  833. while (n && len) {
  834. ctx->Xi.c[n] ^= *(aad++);
  835. --len;
  836. n = (n + 1) % 16;
  837. }
  838. if (n == 0)
  839. GCM_MUL(ctx, Xi);
  840. else {
  841. ctx->ares = n;
  842. return 0;
  843. }
  844. }
  845. #ifdef GHASH
  846. if ((i = (len & (size_t)-16))) {
  847. GHASH(ctx, aad, i);
  848. aad += i;
  849. len -= i;
  850. }
  851. #else
  852. while (len >= 16) {
  853. for (i = 0; i < 16; ++i)
  854. ctx->Xi.c[i] ^= aad[i];
  855. GCM_MUL(ctx, Xi);
  856. aad += 16;
  857. len -= 16;
  858. }
  859. #endif
  860. if (len) {
  861. n = (unsigned int)len;
  862. for (i = 0; i < len; ++i)
  863. ctx->Xi.c[i] ^= aad[i];
  864. }
  865. ctx->ares = n;
  866. return 0;
  867. }
  868. int CRYPTO_gcm128_encrypt(GCM128_CONTEXT *ctx,
  869. const unsigned char *in, unsigned char *out,
  870. size_t len)
  871. {
  872. const union {
  873. long one;
  874. char little;
  875. } is_endian = {
  876. 1
  877. };
  878. unsigned int n, ctr;
  879. size_t i;
  880. u64 mlen = ctx->len.u[1];
  881. block128_f block = ctx->block;
  882. void *key = ctx->key;
  883. #ifdef GCM_FUNCREF_4BIT
  884. void (*gcm_gmult_p) (u64 Xi[2], const u128 Htable[16]) = ctx->gmult;
  885. # ifdef GHASH
  886. void (*gcm_ghash_p) (u64 Xi[2], const u128 Htable[16],
  887. const u8 *inp, size_t len) = ctx->ghash;
  888. # endif
  889. #endif
  890. #if 0
  891. n = (unsigned int)mlen % 16; /* alternative to ctx->mres */
  892. #endif
  893. mlen += len;
  894. if (mlen > ((U64(1) << 36) - 32) || (sizeof(len) == 8 && mlen < len))
  895. return -1;
  896. ctx->len.u[1] = mlen;
  897. if (ctx->ares) {
  898. /* First call to encrypt finalizes GHASH(AAD) */
  899. GCM_MUL(ctx, Xi);
  900. ctx->ares = 0;
  901. }
  902. if (is_endian.little)
  903. #ifdef BSWAP4
  904. ctr = BSWAP4(ctx->Yi.d[3]);
  905. #else
  906. ctr = GETU32(ctx->Yi.c + 12);
  907. #endif
  908. else
  909. ctr = ctx->Yi.d[3];
  910. n = ctx->mres;
  911. #if !defined(OPENSSL_SMALL_FOOTPRINT)
  912. if (16 % sizeof(size_t) == 0) { /* always true actually */
  913. do {
  914. if (n) {
  915. while (n && len) {
  916. ctx->Xi.c[n] ^= *(out++) = *(in++) ^ ctx->EKi.c[n];
  917. --len;
  918. n = (n + 1) % 16;
  919. }
  920. if (n == 0)
  921. GCM_MUL(ctx, Xi);
  922. else {
  923. ctx->mres = n;
  924. return 0;
  925. }
  926. }
  927. # if defined(STRICT_ALIGNMENT)
  928. if (((size_t)in | (size_t)out) % sizeof(size_t) != 0)
  929. break;
  930. # endif
  931. # if defined(GHASH) && defined(GHASH_CHUNK)
  932. while (len >= GHASH_CHUNK) {
  933. size_t j = GHASH_CHUNK;
  934. while (j) {
  935. size_t *out_t = (size_t *)out;
  936. const size_t *in_t = (const size_t *)in;
  937. (*block) (ctx->Yi.c, ctx->EKi.c, key);
  938. ++ctr;
  939. if (is_endian.little)
  940. # ifdef BSWAP4
  941. ctx->Yi.d[3] = BSWAP4(ctr);
  942. # else
  943. PUTU32(ctx->Yi.c + 12, ctr);
  944. # endif
  945. else
  946. ctx->Yi.d[3] = ctr;
  947. for (i = 0; i < 16 / sizeof(size_t); ++i)
  948. out_t[i] = in_t[i] ^ ctx->EKi.t[i];
  949. out += 16;
  950. in += 16;
  951. j -= 16;
  952. }
  953. GHASH(ctx, out - GHASH_CHUNK, GHASH_CHUNK);
  954. len -= GHASH_CHUNK;
  955. }
  956. if ((i = (len & (size_t)-16))) {
  957. size_t j = i;
  958. while (len >= 16) {
  959. size_t *out_t = (size_t *)out;
  960. const size_t *in_t = (const size_t *)in;
  961. (*block) (ctx->Yi.c, ctx->EKi.c, key);
  962. ++ctr;
  963. if (is_endian.little)
  964. # ifdef BSWAP4
  965. ctx->Yi.d[3] = BSWAP4(ctr);
  966. # else
  967. PUTU32(ctx->Yi.c + 12, ctr);
  968. # endif
  969. else
  970. ctx->Yi.d[3] = ctr;
  971. for (i = 0; i < 16 / sizeof(size_t); ++i)
  972. out_t[i] = in_t[i] ^ ctx->EKi.t[i];
  973. out += 16;
  974. in += 16;
  975. len -= 16;
  976. }
  977. GHASH(ctx, out - j, j);
  978. }
  979. # else
  980. while (len >= 16) {
  981. size_t *out_t = (size_t *)out;
  982. const size_t *in_t = (const size_t *)in;
  983. (*block) (ctx->Yi.c, ctx->EKi.c, key);
  984. ++ctr;
  985. if (is_endian.little)
  986. # ifdef BSWAP4
  987. ctx->Yi.d[3] = BSWAP4(ctr);
  988. # else
  989. PUTU32(ctx->Yi.c + 12, ctr);
  990. # endif
  991. else
  992. ctx->Yi.d[3] = ctr;
  993. for (i = 0; i < 16 / sizeof(size_t); ++i)
  994. ctx->Xi.t[i] ^= out_t[i] = in_t[i] ^ ctx->EKi.t[i];
  995. GCM_MUL(ctx, Xi);
  996. out += 16;
  997. in += 16;
  998. len -= 16;
  999. }
  1000. # endif
  1001. if (len) {
  1002. (*block) (ctx->Yi.c, ctx->EKi.c, key);
  1003. ++ctr;
  1004. if (is_endian.little)
  1005. # ifdef BSWAP4
  1006. ctx->Yi.d[3] = BSWAP4(ctr);
  1007. # else
  1008. PUTU32(ctx->Yi.c + 12, ctr);
  1009. # endif
  1010. else
  1011. ctx->Yi.d[3] = ctr;
  1012. while (len--) {
  1013. ctx->Xi.c[n] ^= out[n] = in[n] ^ ctx->EKi.c[n];
  1014. ++n;
  1015. }
  1016. }
  1017. ctx->mres = n;
  1018. return 0;
  1019. } while (0);
  1020. }
  1021. #endif
  1022. for (i = 0; i < len; ++i) {
  1023. if (n == 0) {
  1024. (*block) (ctx->Yi.c, ctx->EKi.c, key);
  1025. ++ctr;
  1026. if (is_endian.little)
  1027. #ifdef BSWAP4
  1028. ctx->Yi.d[3] = BSWAP4(ctr);
  1029. #else
  1030. PUTU32(ctx->Yi.c + 12, ctr);
  1031. #endif
  1032. else
  1033. ctx->Yi.d[3] = ctr;
  1034. }
  1035. ctx->Xi.c[n] ^= out[i] = in[i] ^ ctx->EKi.c[n];
  1036. n = (n + 1) % 16;
  1037. if (n == 0)
  1038. GCM_MUL(ctx, Xi);
  1039. }
  1040. ctx->mres = n;
  1041. return 0;
  1042. }
  1043. int CRYPTO_gcm128_decrypt(GCM128_CONTEXT *ctx,
  1044. const unsigned char *in, unsigned char *out,
  1045. size_t len)
  1046. {
  1047. const union {
  1048. long one;
  1049. char little;
  1050. } is_endian = {
  1051. 1
  1052. };
  1053. unsigned int n, ctr;
  1054. size_t i;
  1055. u64 mlen = ctx->len.u[1];
  1056. block128_f block = ctx->block;
  1057. void *key = ctx->key;
  1058. #ifdef GCM_FUNCREF_4BIT
  1059. void (*gcm_gmult_p) (u64 Xi[2], const u128 Htable[16]) = ctx->gmult;
  1060. # ifdef GHASH
  1061. void (*gcm_ghash_p) (u64 Xi[2], const u128 Htable[16],
  1062. const u8 *inp, size_t len) = ctx->ghash;
  1063. # endif
  1064. #endif
  1065. mlen += len;
  1066. if (mlen > ((U64(1) << 36) - 32) || (sizeof(len) == 8 && mlen < len))
  1067. return -1;
  1068. ctx->len.u[1] = mlen;
  1069. if (ctx->ares) {
  1070. /* First call to decrypt finalizes GHASH(AAD) */
  1071. GCM_MUL(ctx, Xi);
  1072. ctx->ares = 0;
  1073. }
  1074. if (is_endian.little)
  1075. #ifdef BSWAP4
  1076. ctr = BSWAP4(ctx->Yi.d[3]);
  1077. #else
  1078. ctr = GETU32(ctx->Yi.c + 12);
  1079. #endif
  1080. else
  1081. ctr = ctx->Yi.d[3];
  1082. n = ctx->mres;
  1083. #if !defined(OPENSSL_SMALL_FOOTPRINT)
  1084. if (16 % sizeof(size_t) == 0) { /* always true actually */
  1085. do {
  1086. if (n) {
  1087. while (n && len) {
  1088. u8 c = *(in++);
  1089. *(out++) = c ^ ctx->EKi.c[n];
  1090. ctx->Xi.c[n] ^= c;
  1091. --len;
  1092. n = (n + 1) % 16;
  1093. }
  1094. if (n == 0)
  1095. GCM_MUL(ctx, Xi);
  1096. else {
  1097. ctx->mres = n;
  1098. return 0;
  1099. }
  1100. }
  1101. # if defined(STRICT_ALIGNMENT)
  1102. if (((size_t)in | (size_t)out) % sizeof(size_t) != 0)
  1103. break;
  1104. # endif
  1105. # if defined(GHASH) && defined(GHASH_CHUNK)
  1106. while (len >= GHASH_CHUNK) {
  1107. size_t j = GHASH_CHUNK;
  1108. GHASH(ctx, in, GHASH_CHUNK);
  1109. while (j) {
  1110. size_t *out_t = (size_t *)out;
  1111. const size_t *in_t = (const size_t *)in;
  1112. (*block) (ctx->Yi.c, ctx->EKi.c, key);
  1113. ++ctr;
  1114. if (is_endian.little)
  1115. # ifdef BSWAP4
  1116. ctx->Yi.d[3] = BSWAP4(ctr);
  1117. # else
  1118. PUTU32(ctx->Yi.c + 12, ctr);
  1119. # endif
  1120. else
  1121. ctx->Yi.d[3] = ctr;
  1122. for (i = 0; i < 16 / sizeof(size_t); ++i)
  1123. out_t[i] = in_t[i] ^ ctx->EKi.t[i];
  1124. out += 16;
  1125. in += 16;
  1126. j -= 16;
  1127. }
  1128. len -= GHASH_CHUNK;
  1129. }
  1130. if ((i = (len & (size_t)-16))) {
  1131. GHASH(ctx, in, i);
  1132. while (len >= 16) {
  1133. size_t *out_t = (size_t *)out;
  1134. const size_t *in_t = (const size_t *)in;
  1135. (*block) (ctx->Yi.c, ctx->EKi.c, key);
  1136. ++ctr;
  1137. if (is_endian.little)
  1138. # ifdef BSWAP4
  1139. ctx->Yi.d[3] = BSWAP4(ctr);
  1140. # else
  1141. PUTU32(ctx->Yi.c + 12, ctr);
  1142. # endif
  1143. else
  1144. ctx->Yi.d[3] = ctr;
  1145. for (i = 0; i < 16 / sizeof(size_t); ++i)
  1146. out_t[i] = in_t[i] ^ ctx->EKi.t[i];
  1147. out += 16;
  1148. in += 16;
  1149. len -= 16;
  1150. }
  1151. }
  1152. # else
  1153. while (len >= 16) {
  1154. size_t *out_t = (size_t *)out;
  1155. const size_t *in_t = (const size_t *)in;
  1156. (*block) (ctx->Yi.c, ctx->EKi.c, key);
  1157. ++ctr;
  1158. if (is_endian.little)
  1159. # ifdef BSWAP4
  1160. ctx->Yi.d[3] = BSWAP4(ctr);
  1161. # else
  1162. PUTU32(ctx->Yi.c + 12, ctr);
  1163. # endif
  1164. else
  1165. ctx->Yi.d[3] = ctr;
  1166. for (i = 0; i < 16 / sizeof(size_t); ++i) {
  1167. size_t c = in[i];
  1168. out[i] = c ^ ctx->EKi.t[i];
  1169. ctx->Xi.t[i] ^= c;
  1170. }
  1171. GCM_MUL(ctx, Xi);
  1172. out += 16;
  1173. in += 16;
  1174. len -= 16;
  1175. }
  1176. # endif
  1177. if (len) {
  1178. (*block) (ctx->Yi.c, ctx->EKi.c, key);
  1179. ++ctr;
  1180. if (is_endian.little)
  1181. # ifdef BSWAP4
  1182. ctx->Yi.d[3] = BSWAP4(ctr);
  1183. # else
  1184. PUTU32(ctx->Yi.c + 12, ctr);
  1185. # endif
  1186. else
  1187. ctx->Yi.d[3] = ctr;
  1188. while (len--) {
  1189. u8 c = in[n];
  1190. ctx->Xi.c[n] ^= c;
  1191. out[n] = c ^ ctx->EKi.c[n];
  1192. ++n;
  1193. }
  1194. }
  1195. ctx->mres = n;
  1196. return 0;
  1197. } while (0);
  1198. }
  1199. #endif
  1200. for (i = 0; i < len; ++i) {
  1201. u8 c;
  1202. if (n == 0) {
  1203. (*block) (ctx->Yi.c, ctx->EKi.c, key);
  1204. ++ctr;
  1205. if (is_endian.little)
  1206. #ifdef BSWAP4
  1207. ctx->Yi.d[3] = BSWAP4(ctr);
  1208. #else
  1209. PUTU32(ctx->Yi.c + 12, ctr);
  1210. #endif
  1211. else
  1212. ctx->Yi.d[3] = ctr;
  1213. }
  1214. c = in[i];
  1215. out[i] = c ^ ctx->EKi.c[n];
  1216. ctx->Xi.c[n] ^= c;
  1217. n = (n + 1) % 16;
  1218. if (n == 0)
  1219. GCM_MUL(ctx, Xi);
  1220. }
  1221. ctx->mres = n;
  1222. return 0;
  1223. }
  1224. int CRYPTO_gcm128_encrypt_ctr32(GCM128_CONTEXT *ctx,
  1225. const unsigned char *in, unsigned char *out,
  1226. size_t len, ctr128_f stream)
  1227. {
  1228. const union {
  1229. long one;
  1230. char little;
  1231. } is_endian = {
  1232. 1
  1233. };
  1234. unsigned int n, ctr;
  1235. size_t i;
  1236. u64 mlen = ctx->len.u[1];
  1237. void *key = ctx->key;
  1238. #ifdef GCM_FUNCREF_4BIT
  1239. void (*gcm_gmult_p) (u64 Xi[2], const u128 Htable[16]) = ctx->gmult;
  1240. # ifdef GHASH
  1241. void (*gcm_ghash_p) (u64 Xi[2], const u128 Htable[16],
  1242. const u8 *inp, size_t len) = ctx->ghash;
  1243. # endif
  1244. #endif
  1245. mlen += len;
  1246. if (mlen > ((U64(1) << 36) - 32) || (sizeof(len) == 8 && mlen < len))
  1247. return -1;
  1248. ctx->len.u[1] = mlen;
  1249. if (ctx->ares) {
  1250. /* First call to encrypt finalizes GHASH(AAD) */
  1251. GCM_MUL(ctx, Xi);
  1252. ctx->ares = 0;
  1253. }
  1254. if (is_endian.little)
  1255. #ifdef BSWAP4
  1256. ctr = BSWAP4(ctx->Yi.d[3]);
  1257. #else
  1258. ctr = GETU32(ctx->Yi.c + 12);
  1259. #endif
  1260. else
  1261. ctr = ctx->Yi.d[3];
  1262. n = ctx->mres;
  1263. if (n) {
  1264. while (n && len) {
  1265. ctx->Xi.c[n] ^= *(out++) = *(in++) ^ ctx->EKi.c[n];
  1266. --len;
  1267. n = (n + 1) % 16;
  1268. }
  1269. if (n == 0)
  1270. GCM_MUL(ctx, Xi);
  1271. else {
  1272. ctx->mres = n;
  1273. return 0;
  1274. }
  1275. }
  1276. #if defined(GHASH) && !defined(OPENSSL_SMALL_FOOTPRINT)
  1277. while (len >= GHASH_CHUNK) {
  1278. (*stream) (in, out, GHASH_CHUNK / 16, key, ctx->Yi.c);
  1279. ctr += GHASH_CHUNK / 16;
  1280. if (is_endian.little)
  1281. # ifdef BSWAP4
  1282. ctx->Yi.d[3] = BSWAP4(ctr);
  1283. # else
  1284. PUTU32(ctx->Yi.c + 12, ctr);
  1285. # endif
  1286. else
  1287. ctx->Yi.d[3] = ctr;
  1288. GHASH(ctx, out, GHASH_CHUNK);
  1289. out += GHASH_CHUNK;
  1290. in += GHASH_CHUNK;
  1291. len -= GHASH_CHUNK;
  1292. }
  1293. #endif
  1294. if ((i = (len & (size_t)-16))) {
  1295. size_t j = i / 16;
  1296. (*stream) (in, out, j, key, ctx->Yi.c);
  1297. ctr += (unsigned int)j;
  1298. if (is_endian.little)
  1299. #ifdef BSWAP4
  1300. ctx->Yi.d[3] = BSWAP4(ctr);
  1301. #else
  1302. PUTU32(ctx->Yi.c + 12, ctr);
  1303. #endif
  1304. else
  1305. ctx->Yi.d[3] = ctr;
  1306. in += i;
  1307. len -= i;
  1308. #if defined(GHASH)
  1309. GHASH(ctx, out, i);
  1310. out += i;
  1311. #else
  1312. while (j--) {
  1313. for (i = 0; i < 16; ++i)
  1314. ctx->Xi.c[i] ^= out[i];
  1315. GCM_MUL(ctx, Xi);
  1316. out += 16;
  1317. }
  1318. #endif
  1319. }
  1320. if (len) {
  1321. (*ctx->block) (ctx->Yi.c, ctx->EKi.c, key);
  1322. ++ctr;
  1323. if (is_endian.little)
  1324. #ifdef BSWAP4
  1325. ctx->Yi.d[3] = BSWAP4(ctr);
  1326. #else
  1327. PUTU32(ctx->Yi.c + 12, ctr);
  1328. #endif
  1329. else
  1330. ctx->Yi.d[3] = ctr;
  1331. while (len--) {
  1332. ctx->Xi.c[n] ^= out[n] = in[n] ^ ctx->EKi.c[n];
  1333. ++n;
  1334. }
  1335. }
  1336. ctx->mres = n;
  1337. return 0;
  1338. }
  1339. int CRYPTO_gcm128_decrypt_ctr32(GCM128_CONTEXT *ctx,
  1340. const unsigned char *in, unsigned char *out,
  1341. size_t len, ctr128_f stream)
  1342. {
  1343. const union {
  1344. long one;
  1345. char little;
  1346. } is_endian = {
  1347. 1
  1348. };
  1349. unsigned int n, ctr;
  1350. size_t i;
  1351. u64 mlen = ctx->len.u[1];
  1352. void *key = ctx->key;
  1353. #ifdef GCM_FUNCREF_4BIT
  1354. void (*gcm_gmult_p) (u64 Xi[2], const u128 Htable[16]) = ctx->gmult;
  1355. # ifdef GHASH
  1356. void (*gcm_ghash_p) (u64 Xi[2], const u128 Htable[16],
  1357. const u8 *inp, size_t len) = ctx->ghash;
  1358. # endif
  1359. #endif
  1360. mlen += len;
  1361. if (mlen > ((U64(1) << 36) - 32) || (sizeof(len) == 8 && mlen < len))
  1362. return -1;
  1363. ctx->len.u[1] = mlen;
  1364. if (ctx->ares) {
  1365. /* First call to decrypt finalizes GHASH(AAD) */
  1366. GCM_MUL(ctx, Xi);
  1367. ctx->ares = 0;
  1368. }
  1369. if (is_endian.little)
  1370. #ifdef BSWAP4
  1371. ctr = BSWAP4(ctx->Yi.d[3]);
  1372. #else
  1373. ctr = GETU32(ctx->Yi.c + 12);
  1374. #endif
  1375. else
  1376. ctr = ctx->Yi.d[3];
  1377. n = ctx->mres;
  1378. if (n) {
  1379. while (n && len) {
  1380. u8 c = *(in++);
  1381. *(out++) = c ^ ctx->EKi.c[n];
  1382. ctx->Xi.c[n] ^= c;
  1383. --len;
  1384. n = (n + 1) % 16;
  1385. }
  1386. if (n == 0)
  1387. GCM_MUL(ctx, Xi);
  1388. else {
  1389. ctx->mres = n;
  1390. return 0;
  1391. }
  1392. }
  1393. #if defined(GHASH) && !defined(OPENSSL_SMALL_FOOTPRINT)
  1394. while (len >= GHASH_CHUNK) {
  1395. GHASH(ctx, in, GHASH_CHUNK);
  1396. (*stream) (in, out, GHASH_CHUNK / 16, key, ctx->Yi.c);
  1397. ctr += GHASH_CHUNK / 16;
  1398. if (is_endian.little)
  1399. # ifdef BSWAP4
  1400. ctx->Yi.d[3] = BSWAP4(ctr);
  1401. # else
  1402. PUTU32(ctx->Yi.c + 12, ctr);
  1403. # endif
  1404. else
  1405. ctx->Yi.d[3] = ctr;
  1406. out += GHASH_CHUNK;
  1407. in += GHASH_CHUNK;
  1408. len -= GHASH_CHUNK;
  1409. }
  1410. #endif
  1411. if ((i = (len & (size_t)-16))) {
  1412. size_t j = i / 16;
  1413. #if defined(GHASH)
  1414. GHASH(ctx, in, i);
  1415. #else
  1416. while (j--) {
  1417. size_t k;
  1418. for (k = 0; k < 16; ++k)
  1419. ctx->Xi.c[k] ^= in[k];
  1420. GCM_MUL(ctx, Xi);
  1421. in += 16;
  1422. }
  1423. j = i / 16;
  1424. in -= i;
  1425. #endif
  1426. (*stream) (in, out, j, key, ctx->Yi.c);
  1427. ctr += (unsigned int)j;
  1428. if (is_endian.little)
  1429. #ifdef BSWAP4
  1430. ctx->Yi.d[3] = BSWAP4(ctr);
  1431. #else
  1432. PUTU32(ctx->Yi.c + 12, ctr);
  1433. #endif
  1434. else
  1435. ctx->Yi.d[3] = ctr;
  1436. out += i;
  1437. in += i;
  1438. len -= i;
  1439. }
  1440. if (len) {
  1441. (*ctx->block) (ctx->Yi.c, ctx->EKi.c, key);
  1442. ++ctr;
  1443. if (is_endian.little)
  1444. #ifdef BSWAP4
  1445. ctx->Yi.d[3] = BSWAP4(ctr);
  1446. #else
  1447. PUTU32(ctx->Yi.c + 12, ctr);
  1448. #endif
  1449. else
  1450. ctx->Yi.d[3] = ctr;
  1451. while (len--) {
  1452. u8 c = in[n];
  1453. ctx->Xi.c[n] ^= c;
  1454. out[n] = c ^ ctx->EKi.c[n];
  1455. ++n;
  1456. }
  1457. }
  1458. ctx->mres = n;
  1459. return 0;
  1460. }
  1461. int CRYPTO_gcm128_finish(GCM128_CONTEXT *ctx, const unsigned char *tag,
  1462. size_t len)
  1463. {
  1464. const union {
  1465. long one;
  1466. char little;
  1467. } is_endian = {
  1468. 1
  1469. };
  1470. u64 alen = ctx->len.u[0] << 3;
  1471. u64 clen = ctx->len.u[1] << 3;
  1472. #ifdef GCM_FUNCREF_4BIT
  1473. void (*gcm_gmult_p) (u64 Xi[2], const u128 Htable[16]) = ctx->gmult;
  1474. #endif
  1475. if (ctx->mres || ctx->ares)
  1476. GCM_MUL(ctx, Xi);
  1477. if (is_endian.little) {
  1478. #ifdef BSWAP8
  1479. alen = BSWAP8(alen);
  1480. clen = BSWAP8(clen);
  1481. #else
  1482. u8 *p = ctx->len.c;
  1483. ctx->len.u[0] = alen;
  1484. ctx->len.u[1] = clen;
  1485. alen = (u64)GETU32(p) << 32 | GETU32(p + 4);
  1486. clen = (u64)GETU32(p + 8) << 32 | GETU32(p + 12);
  1487. #endif
  1488. }
  1489. ctx->Xi.u[0] ^= alen;
  1490. ctx->Xi.u[1] ^= clen;
  1491. GCM_MUL(ctx, Xi);
  1492. ctx->Xi.u[0] ^= ctx->EK0.u[0];
  1493. ctx->Xi.u[1] ^= ctx->EK0.u[1];
  1494. if (tag && len <= sizeof(ctx->Xi))
  1495. return CRYPTO_memcmp(ctx->Xi.c, tag, len);
  1496. else
  1497. return -1;
  1498. }
  1499. void CRYPTO_gcm128_tag(GCM128_CONTEXT *ctx, unsigned char *tag, size_t len)
  1500. {
  1501. CRYPTO_gcm128_finish(ctx, NULL, 0);
  1502. memcpy(tag, ctx->Xi.c,
  1503. len <= sizeof(ctx->Xi.c) ? len : sizeof(ctx->Xi.c));
  1504. }
  1505. GCM128_CONTEXT *CRYPTO_gcm128_new(void *key, block128_f block)
  1506. {
  1507. GCM128_CONTEXT *ret;
  1508. if ((ret = (GCM128_CONTEXT *)OPENSSL_malloc(sizeof(GCM128_CONTEXT))))
  1509. CRYPTO_gcm128_init(ret, key, block);
  1510. return ret;
  1511. }
  1512. void CRYPTO_gcm128_release(GCM128_CONTEXT *ctx)
  1513. {
  1514. if (ctx) {
  1515. OPENSSL_cleanse(ctx, sizeof(*ctx));
  1516. OPENSSL_free(ctx);
  1517. }
  1518. }
  1519. #if defined(SELFTEST)
  1520. # include <stdio.h>
  1521. # include <openssl/aes.h>
  1522. /* Test Case 1 */
  1523. static const u8 K1[16], *P1 = NULL, *A1 = NULL, IV1[12], *C1 = NULL;
  1524. static const u8 T1[] = {
  1525. 0x58, 0xe2, 0xfc, 0xce, 0xfa, 0x7e, 0x30, 0x61,
  1526. 0x36, 0x7f, 0x1d, 0x57, 0xa4, 0xe7, 0x45, 0x5a
  1527. };
  1528. /* Test Case 2 */
  1529. # define K2 K1
  1530. # define A2 A1
  1531. # define IV2 IV1
  1532. static const u8 P2[16];
  1533. static const u8 C2[] = {
  1534. 0x03, 0x88, 0xda, 0xce, 0x60, 0xb6, 0xa3, 0x92,
  1535. 0xf3, 0x28, 0xc2, 0xb9, 0x71, 0xb2, 0xfe, 0x78
  1536. };
  1537. static const u8 T2[] = {
  1538. 0xab, 0x6e, 0x47, 0xd4, 0x2c, 0xec, 0x13, 0xbd,
  1539. 0xf5, 0x3a, 0x67, 0xb2, 0x12, 0x57, 0xbd, 0xdf
  1540. };
  1541. /* Test Case 3 */
  1542. # define A3 A2
  1543. static const u8 K3[] = {
  1544. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  1545. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08
  1546. };
  1547. static const u8 P3[] = {
  1548. 0xd9, 0x31, 0x32, 0x25, 0xf8, 0x84, 0x06, 0xe5,
  1549. 0xa5, 0x59, 0x09, 0xc5, 0xaf, 0xf5, 0x26, 0x9a,
  1550. 0x86, 0xa7, 0xa9, 0x53, 0x15, 0x34, 0xf7, 0xda,
  1551. 0x2e, 0x4c, 0x30, 0x3d, 0x8a, 0x31, 0x8a, 0x72,
  1552. 0x1c, 0x3c, 0x0c, 0x95, 0x95, 0x68, 0x09, 0x53,
  1553. 0x2f, 0xcf, 0x0e, 0x24, 0x49, 0xa6, 0xb5, 0x25,
  1554. 0xb1, 0x6a, 0xed, 0xf5, 0xaa, 0x0d, 0xe6, 0x57,
  1555. 0xba, 0x63, 0x7b, 0x39, 0x1a, 0xaf, 0xd2, 0x55
  1556. };
  1557. static const u8 IV3[] = {
  1558. 0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce, 0xdb, 0xad,
  1559. 0xde, 0xca, 0xf8, 0x88
  1560. };
  1561. static const u8 C3[] = {
  1562. 0x42, 0x83, 0x1e, 0xc2, 0x21, 0x77, 0x74, 0x24,
  1563. 0x4b, 0x72, 0x21, 0xb7, 0x84, 0xd0, 0xd4, 0x9c,
  1564. 0xe3, 0xaa, 0x21, 0x2f, 0x2c, 0x02, 0xa4, 0xe0,
  1565. 0x35, 0xc1, 0x7e, 0x23, 0x29, 0xac, 0xa1, 0x2e,
  1566. 0x21, 0xd5, 0x14, 0xb2, 0x54, 0x66, 0x93, 0x1c,
  1567. 0x7d, 0x8f, 0x6a, 0x5a, 0xac, 0x84, 0xaa, 0x05,
  1568. 0x1b, 0xa3, 0x0b, 0x39, 0x6a, 0x0a, 0xac, 0x97,
  1569. 0x3d, 0x58, 0xe0, 0x91, 0x47, 0x3f, 0x59, 0x85
  1570. };
  1571. static const u8 T3[] = {
  1572. 0x4d, 0x5c, 0x2a, 0xf3, 0x27, 0xcd, 0x64, 0xa6,
  1573. 0x2c, 0xf3, 0x5a, 0xbd, 0x2b, 0xa6, 0xfa, 0xb4
  1574. };
  1575. /* Test Case 4 */
  1576. # define K4 K3
  1577. # define IV4 IV3
  1578. static const u8 P4[] = {
  1579. 0xd9, 0x31, 0x32, 0x25, 0xf8, 0x84, 0x06, 0xe5,
  1580. 0xa5, 0x59, 0x09, 0xc5, 0xaf, 0xf5, 0x26, 0x9a,
  1581. 0x86, 0xa7, 0xa9, 0x53, 0x15, 0x34, 0xf7, 0xda,
  1582. 0x2e, 0x4c, 0x30, 0x3d, 0x8a, 0x31, 0x8a, 0x72,
  1583. 0x1c, 0x3c, 0x0c, 0x95, 0x95, 0x68, 0x09, 0x53,
  1584. 0x2f, 0xcf, 0x0e, 0x24, 0x49, 0xa6, 0xb5, 0x25,
  1585. 0xb1, 0x6a, 0xed, 0xf5, 0xaa, 0x0d, 0xe6, 0x57,
  1586. 0xba, 0x63, 0x7b, 0x39
  1587. };
  1588. static const u8 A4[] = {
  1589. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  1590. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  1591. 0xab, 0xad, 0xda, 0xd2
  1592. };
  1593. static const u8 C4[] = {
  1594. 0x42, 0x83, 0x1e, 0xc2, 0x21, 0x77, 0x74, 0x24,
  1595. 0x4b, 0x72, 0x21, 0xb7, 0x84, 0xd0, 0xd4, 0x9c,
  1596. 0xe3, 0xaa, 0x21, 0x2f, 0x2c, 0x02, 0xa4, 0xe0,
  1597. 0x35, 0xc1, 0x7e, 0x23, 0x29, 0xac, 0xa1, 0x2e,
  1598. 0x21, 0xd5, 0x14, 0xb2, 0x54, 0x66, 0x93, 0x1c,
  1599. 0x7d, 0x8f, 0x6a, 0x5a, 0xac, 0x84, 0xaa, 0x05,
  1600. 0x1b, 0xa3, 0x0b, 0x39, 0x6a, 0x0a, 0xac, 0x97,
  1601. 0x3d, 0x58, 0xe0, 0x91
  1602. };
  1603. static const u8 T4[] = {
  1604. 0x5b, 0xc9, 0x4f, 0xbc, 0x32, 0x21, 0xa5, 0xdb,
  1605. 0x94, 0xfa, 0xe9, 0x5a, 0xe7, 0x12, 0x1a, 0x47
  1606. };
  1607. /* Test Case 5 */
  1608. # define K5 K4
  1609. # define P5 P4
  1610. # define A5 A4
  1611. static const u8 IV5[] = {
  1612. 0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce, 0xdb, 0xad
  1613. };
  1614. static const u8 C5[] = {
  1615. 0x61, 0x35, 0x3b, 0x4c, 0x28, 0x06, 0x93, 0x4a,
  1616. 0x77, 0x7f, 0xf5, 0x1f, 0xa2, 0x2a, 0x47, 0x55,
  1617. 0x69, 0x9b, 0x2a, 0x71, 0x4f, 0xcd, 0xc6, 0xf8,
  1618. 0x37, 0x66, 0xe5, 0xf9, 0x7b, 0x6c, 0x74, 0x23,
  1619. 0x73, 0x80, 0x69, 0x00, 0xe4, 0x9f, 0x24, 0xb2,
  1620. 0x2b, 0x09, 0x75, 0x44, 0xd4, 0x89, 0x6b, 0x42,
  1621. 0x49, 0x89, 0xb5, 0xe1, 0xeb, 0xac, 0x0f, 0x07,
  1622. 0xc2, 0x3f, 0x45, 0x98
  1623. };
  1624. static const u8 T5[] = {
  1625. 0x36, 0x12, 0xd2, 0xe7, 0x9e, 0x3b, 0x07, 0x85,
  1626. 0x56, 0x1b, 0xe1, 0x4a, 0xac, 0xa2, 0xfc, 0xcb
  1627. };
  1628. /* Test Case 6 */
  1629. # define K6 K5
  1630. # define P6 P5
  1631. # define A6 A5
  1632. static const u8 IV6[] = {
  1633. 0x93, 0x13, 0x22, 0x5d, 0xf8, 0x84, 0x06, 0xe5,
  1634. 0x55, 0x90, 0x9c, 0x5a, 0xff, 0x52, 0x69, 0xaa,
  1635. 0x6a, 0x7a, 0x95, 0x38, 0x53, 0x4f, 0x7d, 0xa1,
  1636. 0xe4, 0xc3, 0x03, 0xd2, 0xa3, 0x18, 0xa7, 0x28,
  1637. 0xc3, 0xc0, 0xc9, 0x51, 0x56, 0x80, 0x95, 0x39,
  1638. 0xfc, 0xf0, 0xe2, 0x42, 0x9a, 0x6b, 0x52, 0x54,
  1639. 0x16, 0xae, 0xdb, 0xf5, 0xa0, 0xde, 0x6a, 0x57,
  1640. 0xa6, 0x37, 0xb3, 0x9b
  1641. };
  1642. static const u8 C6[] = {
  1643. 0x8c, 0xe2, 0x49, 0x98, 0x62, 0x56, 0x15, 0xb6,
  1644. 0x03, 0xa0, 0x33, 0xac, 0xa1, 0x3f, 0xb8, 0x94,
  1645. 0xbe, 0x91, 0x12, 0xa5, 0xc3, 0xa2, 0x11, 0xa8,
  1646. 0xba, 0x26, 0x2a, 0x3c, 0xca, 0x7e, 0x2c, 0xa7,
  1647. 0x01, 0xe4, 0xa9, 0xa4, 0xfb, 0xa4, 0x3c, 0x90,
  1648. 0xcc, 0xdc, 0xb2, 0x81, 0xd4, 0x8c, 0x7c, 0x6f,
  1649. 0xd6, 0x28, 0x75, 0xd2, 0xac, 0xa4, 0x17, 0x03,
  1650. 0x4c, 0x34, 0xae, 0xe5
  1651. };
  1652. static const u8 T6[] = {
  1653. 0x61, 0x9c, 0xc5, 0xae, 0xff, 0xfe, 0x0b, 0xfa,
  1654. 0x46, 0x2a, 0xf4, 0x3c, 0x16, 0x99, 0xd0, 0x50
  1655. };
  1656. /* Test Case 7 */
  1657. static const u8 K7[24], *P7 = NULL, *A7 = NULL, IV7[12], *C7 = NULL;
  1658. static const u8 T7[] = {
  1659. 0xcd, 0x33, 0xb2, 0x8a, 0xc7, 0x73, 0xf7, 0x4b,
  1660. 0xa0, 0x0e, 0xd1, 0xf3, 0x12, 0x57, 0x24, 0x35
  1661. };
  1662. /* Test Case 8 */
  1663. # define K8 K7
  1664. # define IV8 IV7
  1665. # define A8 A7
  1666. static const u8 P8[16];
  1667. static const u8 C8[] = {
  1668. 0x98, 0xe7, 0x24, 0x7c, 0x07, 0xf0, 0xfe, 0x41,
  1669. 0x1c, 0x26, 0x7e, 0x43, 0x84, 0xb0, 0xf6, 0x00
  1670. };
  1671. static const u8 T8[] = {
  1672. 0x2f, 0xf5, 0x8d, 0x80, 0x03, 0x39, 0x27, 0xab,
  1673. 0x8e, 0xf4, 0xd4, 0x58, 0x75, 0x14, 0xf0, 0xfb
  1674. };
  1675. /* Test Case 9 */
  1676. # define A9 A8
  1677. static const u8 K9[] = {
  1678. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  1679. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08,
  1680. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c
  1681. };
  1682. static const u8 P9[] = {
  1683. 0xd9, 0x31, 0x32, 0x25, 0xf8, 0x84, 0x06, 0xe5,
  1684. 0xa5, 0x59, 0x09, 0xc5, 0xaf, 0xf5, 0x26, 0x9a,
  1685. 0x86, 0xa7, 0xa9, 0x53, 0x15, 0x34, 0xf7, 0xda,
  1686. 0x2e, 0x4c, 0x30, 0x3d, 0x8a, 0x31, 0x8a, 0x72,
  1687. 0x1c, 0x3c, 0x0c, 0x95, 0x95, 0x68, 0x09, 0x53,
  1688. 0x2f, 0xcf, 0x0e, 0x24, 0x49, 0xa6, 0xb5, 0x25,
  1689. 0xb1, 0x6a, 0xed, 0xf5, 0xaa, 0x0d, 0xe6, 0x57,
  1690. 0xba, 0x63, 0x7b, 0x39, 0x1a, 0xaf, 0xd2, 0x55
  1691. };
  1692. static const u8 IV9[] = {
  1693. 0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce, 0xdb, 0xad,
  1694. 0xde, 0xca, 0xf8, 0x88
  1695. };
  1696. static const u8 C9[] = {
  1697. 0x39, 0x80, 0xca, 0x0b, 0x3c, 0x00, 0xe8, 0x41,
  1698. 0xeb, 0x06, 0xfa, 0xc4, 0x87, 0x2a, 0x27, 0x57,
  1699. 0x85, 0x9e, 0x1c, 0xea, 0xa6, 0xef, 0xd9, 0x84,
  1700. 0x62, 0x85, 0x93, 0xb4, 0x0c, 0xa1, 0xe1, 0x9c,
  1701. 0x7d, 0x77, 0x3d, 0x00, 0xc1, 0x44, 0xc5, 0x25,
  1702. 0xac, 0x61, 0x9d, 0x18, 0xc8, 0x4a, 0x3f, 0x47,
  1703. 0x18, 0xe2, 0x44, 0x8b, 0x2f, 0xe3, 0x24, 0xd9,
  1704. 0xcc, 0xda, 0x27, 0x10, 0xac, 0xad, 0xe2, 0x56
  1705. };
  1706. static const u8 T9[] = {
  1707. 0x99, 0x24, 0xa7, 0xc8, 0x58, 0x73, 0x36, 0xbf,
  1708. 0xb1, 0x18, 0x02, 0x4d, 0xb8, 0x67, 0x4a, 0x14
  1709. };
  1710. /* Test Case 10 */
  1711. # define K10 K9
  1712. # define IV10 IV9
  1713. static const u8 P10[] = {
  1714. 0xd9, 0x31, 0x32, 0x25, 0xf8, 0x84, 0x06, 0xe5,
  1715. 0xa5, 0x59, 0x09, 0xc5, 0xaf, 0xf5, 0x26, 0x9a,
  1716. 0x86, 0xa7, 0xa9, 0x53, 0x15, 0x34, 0xf7, 0xda,
  1717. 0x2e, 0x4c, 0x30, 0x3d, 0x8a, 0x31, 0x8a, 0x72,
  1718. 0x1c, 0x3c, 0x0c, 0x95, 0x95, 0x68, 0x09, 0x53,
  1719. 0x2f, 0xcf, 0x0e, 0x24, 0x49, 0xa6, 0xb5, 0x25,
  1720. 0xb1, 0x6a, 0xed, 0xf5, 0xaa, 0x0d, 0xe6, 0x57,
  1721. 0xba, 0x63, 0x7b, 0x39
  1722. };
  1723. static const u8 A10[] = {
  1724. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  1725. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  1726. 0xab, 0xad, 0xda, 0xd2
  1727. };
  1728. static const u8 C10[] = {
  1729. 0x39, 0x80, 0xca, 0x0b, 0x3c, 0x00, 0xe8, 0x41,
  1730. 0xeb, 0x06, 0xfa, 0xc4, 0x87, 0x2a, 0x27, 0x57,
  1731. 0x85, 0x9e, 0x1c, 0xea, 0xa6, 0xef, 0xd9, 0x84,
  1732. 0x62, 0x85, 0x93, 0xb4, 0x0c, 0xa1, 0xe1, 0x9c,
  1733. 0x7d, 0x77, 0x3d, 0x00, 0xc1, 0x44, 0xc5, 0x25,
  1734. 0xac, 0x61, 0x9d, 0x18, 0xc8, 0x4a, 0x3f, 0x47,
  1735. 0x18, 0xe2, 0x44, 0x8b, 0x2f, 0xe3, 0x24, 0xd9,
  1736. 0xcc, 0xda, 0x27, 0x10
  1737. };
  1738. static const u8 T10[] = {
  1739. 0x25, 0x19, 0x49, 0x8e, 0x80, 0xf1, 0x47, 0x8f,
  1740. 0x37, 0xba, 0x55, 0xbd, 0x6d, 0x27, 0x61, 0x8c
  1741. };
  1742. /* Test Case 11 */
  1743. # define K11 K10
  1744. # define P11 P10
  1745. # define A11 A10
  1746. static const u8 IV11[] = { 0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce, 0xdb, 0xad };
  1747. static const u8 C11[] = {
  1748. 0x0f, 0x10, 0xf5, 0x99, 0xae, 0x14, 0xa1, 0x54,
  1749. 0xed, 0x24, 0xb3, 0x6e, 0x25, 0x32, 0x4d, 0xb8,
  1750. 0xc5, 0x66, 0x63, 0x2e, 0xf2, 0xbb, 0xb3, 0x4f,
  1751. 0x83, 0x47, 0x28, 0x0f, 0xc4, 0x50, 0x70, 0x57,
  1752. 0xfd, 0xdc, 0x29, 0xdf, 0x9a, 0x47, 0x1f, 0x75,
  1753. 0xc6, 0x65, 0x41, 0xd4, 0xd4, 0xda, 0xd1, 0xc9,
  1754. 0xe9, 0x3a, 0x19, 0xa5, 0x8e, 0x8b, 0x47, 0x3f,
  1755. 0xa0, 0xf0, 0x62, 0xf7
  1756. };
  1757. static const u8 T11[] = {
  1758. 0x65, 0xdc, 0xc5, 0x7f, 0xcf, 0x62, 0x3a, 0x24,
  1759. 0x09, 0x4f, 0xcc, 0xa4, 0x0d, 0x35, 0x33, 0xf8
  1760. };
  1761. /* Test Case 12 */
  1762. # define K12 K11
  1763. # define P12 P11
  1764. # define A12 A11
  1765. static const u8 IV12[] = {
  1766. 0x93, 0x13, 0x22, 0x5d, 0xf8, 0x84, 0x06, 0xe5,
  1767. 0x55, 0x90, 0x9c, 0x5a, 0xff, 0x52, 0x69, 0xaa,
  1768. 0x6a, 0x7a, 0x95, 0x38, 0x53, 0x4f, 0x7d, 0xa1,
  1769. 0xe4, 0xc3, 0x03, 0xd2, 0xa3, 0x18, 0xa7, 0x28,
  1770. 0xc3, 0xc0, 0xc9, 0x51, 0x56, 0x80, 0x95, 0x39,
  1771. 0xfc, 0xf0, 0xe2, 0x42, 0x9a, 0x6b, 0x52, 0x54,
  1772. 0x16, 0xae, 0xdb, 0xf5, 0xa0, 0xde, 0x6a, 0x57,
  1773. 0xa6, 0x37, 0xb3, 0x9b
  1774. };
  1775. static const u8 C12[] = {
  1776. 0xd2, 0x7e, 0x88, 0x68, 0x1c, 0xe3, 0x24, 0x3c,
  1777. 0x48, 0x30, 0x16, 0x5a, 0x8f, 0xdc, 0xf9, 0xff,
  1778. 0x1d, 0xe9, 0xa1, 0xd8, 0xe6, 0xb4, 0x47, 0xef,
  1779. 0x6e, 0xf7, 0xb7, 0x98, 0x28, 0x66, 0x6e, 0x45,
  1780. 0x81, 0xe7, 0x90, 0x12, 0xaf, 0x34, 0xdd, 0xd9,
  1781. 0xe2, 0xf0, 0x37, 0x58, 0x9b, 0x29, 0x2d, 0xb3,
  1782. 0xe6, 0x7c, 0x03, 0x67, 0x45, 0xfa, 0x22, 0xe7,
  1783. 0xe9, 0xb7, 0x37, 0x3b
  1784. };
  1785. static const u8 T12[] = {
  1786. 0xdc, 0xf5, 0x66, 0xff, 0x29, 0x1c, 0x25, 0xbb,
  1787. 0xb8, 0x56, 0x8f, 0xc3, 0xd3, 0x76, 0xa6, 0xd9
  1788. };
  1789. /* Test Case 13 */
  1790. static const u8 K13[32], *P13 = NULL, *A13 = NULL, IV13[12], *C13 = NULL;
  1791. static const u8 T13[] = {
  1792. 0x53, 0x0f, 0x8a, 0xfb, 0xc7, 0x45, 0x36, 0xb9,
  1793. 0xa9, 0x63, 0xb4, 0xf1, 0xc4, 0xcb, 0x73, 0x8b
  1794. };
  1795. /* Test Case 14 */
  1796. # define K14 K13
  1797. # define A14 A13
  1798. static const u8 P14[16], IV14[12];
  1799. static const u8 C14[] = {
  1800. 0xce, 0xa7, 0x40, 0x3d, 0x4d, 0x60, 0x6b, 0x6e,
  1801. 0x07, 0x4e, 0xc5, 0xd3, 0xba, 0xf3, 0x9d, 0x18
  1802. };
  1803. static const u8 T14[] = {
  1804. 0xd0, 0xd1, 0xc8, 0xa7, 0x99, 0x99, 0x6b, 0xf0,
  1805. 0x26, 0x5b, 0x98, 0xb5, 0xd4, 0x8a, 0xb9, 0x19
  1806. };
  1807. /* Test Case 15 */
  1808. # define A15 A14
  1809. static const u8 K15[] = {
  1810. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  1811. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08,
  1812. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  1813. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08
  1814. };
  1815. static const u8 P15[] = {
  1816. 0xd9, 0x31, 0x32, 0x25, 0xf8, 0x84, 0x06, 0xe5,
  1817. 0xa5, 0x59, 0x09, 0xc5, 0xaf, 0xf5, 0x26, 0x9a,
  1818. 0x86, 0xa7, 0xa9, 0x53, 0x15, 0x34, 0xf7, 0xda,
  1819. 0x2e, 0x4c, 0x30, 0x3d, 0x8a, 0x31, 0x8a, 0x72,
  1820. 0x1c, 0x3c, 0x0c, 0x95, 0x95, 0x68, 0x09, 0x53,
  1821. 0x2f, 0xcf, 0x0e, 0x24, 0x49, 0xa6, 0xb5, 0x25,
  1822. 0xb1, 0x6a, 0xed, 0xf5, 0xaa, 0x0d, 0xe6, 0x57,
  1823. 0xba, 0x63, 0x7b, 0x39, 0x1a, 0xaf, 0xd2, 0x55
  1824. };
  1825. static const u8 IV15[] = {
  1826. 0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce, 0xdb, 0xad,
  1827. 0xde, 0xca, 0xf8, 0x88
  1828. };
  1829. static const u8 C15[] = {
  1830. 0x52, 0x2d, 0xc1, 0xf0, 0x99, 0x56, 0x7d, 0x07,
  1831. 0xf4, 0x7f, 0x37, 0xa3, 0x2a, 0x84, 0x42, 0x7d,
  1832. 0x64, 0x3a, 0x8c, 0xdc, 0xbf, 0xe5, 0xc0, 0xc9,
  1833. 0x75, 0x98, 0xa2, 0xbd, 0x25, 0x55, 0xd1, 0xaa,
  1834. 0x8c, 0xb0, 0x8e, 0x48, 0x59, 0x0d, 0xbb, 0x3d,
  1835. 0xa7, 0xb0, 0x8b, 0x10, 0x56, 0x82, 0x88, 0x38,
  1836. 0xc5, 0xf6, 0x1e, 0x63, 0x93, 0xba, 0x7a, 0x0a,
  1837. 0xbc, 0xc9, 0xf6, 0x62, 0x89, 0x80, 0x15, 0xad
  1838. };
  1839. static const u8 T15[] = {
  1840. 0xb0, 0x94, 0xda, 0xc5, 0xd9, 0x34, 0x71, 0xbd,
  1841. 0xec, 0x1a, 0x50, 0x22, 0x70, 0xe3, 0xcc, 0x6c
  1842. };
  1843. /* Test Case 16 */
  1844. # define K16 K15
  1845. # define IV16 IV15
  1846. static const u8 P16[] = {
  1847. 0xd9, 0x31, 0x32, 0x25, 0xf8, 0x84, 0x06, 0xe5,
  1848. 0xa5, 0x59, 0x09, 0xc5, 0xaf, 0xf5, 0x26, 0x9a,
  1849. 0x86, 0xa7, 0xa9, 0x53, 0x15, 0x34, 0xf7, 0xda,
  1850. 0x2e, 0x4c, 0x30, 0x3d, 0x8a, 0x31, 0x8a, 0x72,
  1851. 0x1c, 0x3c, 0x0c, 0x95, 0x95, 0x68, 0x09, 0x53,
  1852. 0x2f, 0xcf, 0x0e, 0x24, 0x49, 0xa6, 0xb5, 0x25,
  1853. 0xb1, 0x6a, 0xed, 0xf5, 0xaa, 0x0d, 0xe6, 0x57,
  1854. 0xba, 0x63, 0x7b, 0x39
  1855. };
  1856. static const u8 A16[] = {
  1857. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  1858. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  1859. 0xab, 0xad, 0xda, 0xd2
  1860. };
  1861. static const u8 C16[] = {
  1862. 0x52, 0x2d, 0xc1, 0xf0, 0x99, 0x56, 0x7d, 0x07,
  1863. 0xf4, 0x7f, 0x37, 0xa3, 0x2a, 0x84, 0x42, 0x7d,
  1864. 0x64, 0x3a, 0x8c, 0xdc, 0xbf, 0xe5, 0xc0, 0xc9,
  1865. 0x75, 0x98, 0xa2, 0xbd, 0x25, 0x55, 0xd1, 0xaa,
  1866. 0x8c, 0xb0, 0x8e, 0x48, 0x59, 0x0d, 0xbb, 0x3d,
  1867. 0xa7, 0xb0, 0x8b, 0x10, 0x56, 0x82, 0x88, 0x38,
  1868. 0xc5, 0xf6, 0x1e, 0x63, 0x93, 0xba, 0x7a, 0x0a,
  1869. 0xbc, 0xc9, 0xf6, 0x62
  1870. };
  1871. static const u8 T16[] = {
  1872. 0x76, 0xfc, 0x6e, 0xce, 0x0f, 0x4e, 0x17, 0x68,
  1873. 0xcd, 0xdf, 0x88, 0x53, 0xbb, 0x2d, 0x55, 0x1b
  1874. };
  1875. /* Test Case 17 */
  1876. # define K17 K16
  1877. # define P17 P16
  1878. # define A17 A16
  1879. static const u8 IV17[] = { 0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce, 0xdb, 0xad };
  1880. static const u8 C17[] = {
  1881. 0xc3, 0x76, 0x2d, 0xf1, 0xca, 0x78, 0x7d, 0x32,
  1882. 0xae, 0x47, 0xc1, 0x3b, 0xf1, 0x98, 0x44, 0xcb,
  1883. 0xaf, 0x1a, 0xe1, 0x4d, 0x0b, 0x97, 0x6a, 0xfa,
  1884. 0xc5, 0x2f, 0xf7, 0xd7, 0x9b, 0xba, 0x9d, 0xe0,
  1885. 0xfe, 0xb5, 0x82, 0xd3, 0x39, 0x34, 0xa4, 0xf0,
  1886. 0x95, 0x4c, 0xc2, 0x36, 0x3b, 0xc7, 0x3f, 0x78,
  1887. 0x62, 0xac, 0x43, 0x0e, 0x64, 0xab, 0xe4, 0x99,
  1888. 0xf4, 0x7c, 0x9b, 0x1f
  1889. };
  1890. static const u8 T17[] = {
  1891. 0x3a, 0x33, 0x7d, 0xbf, 0x46, 0xa7, 0x92, 0xc4,
  1892. 0x5e, 0x45, 0x49, 0x13, 0xfe, 0x2e, 0xa8, 0xf2
  1893. };
  1894. /* Test Case 18 */
  1895. # define K18 K17
  1896. # define P18 P17
  1897. # define A18 A17
  1898. static const u8 IV18[] = {
  1899. 0x93, 0x13, 0x22, 0x5d, 0xf8, 0x84, 0x06, 0xe5,
  1900. 0x55, 0x90, 0x9c, 0x5a, 0xff, 0x52, 0x69, 0xaa,
  1901. 0x6a, 0x7a, 0x95, 0x38, 0x53, 0x4f, 0x7d, 0xa1,
  1902. 0xe4, 0xc3, 0x03, 0xd2, 0xa3, 0x18, 0xa7, 0x28,
  1903. 0xc3, 0xc0, 0xc9, 0x51, 0x56, 0x80, 0x95, 0x39,
  1904. 0xfc, 0xf0, 0xe2, 0x42, 0x9a, 0x6b, 0x52, 0x54,
  1905. 0x16, 0xae, 0xdb, 0xf5, 0xa0, 0xde, 0x6a, 0x57,
  1906. 0xa6, 0x37, 0xb3, 0x9b
  1907. };
  1908. static const u8 C18[] = {
  1909. 0x5a, 0x8d, 0xef, 0x2f, 0x0c, 0x9e, 0x53, 0xf1,
  1910. 0xf7, 0x5d, 0x78, 0x53, 0x65, 0x9e, 0x2a, 0x20,
  1911. 0xee, 0xb2, 0xb2, 0x2a, 0xaf, 0xde, 0x64, 0x19,
  1912. 0xa0, 0x58, 0xab, 0x4f, 0x6f, 0x74, 0x6b, 0xf4,
  1913. 0x0f, 0xc0, 0xc3, 0xb7, 0x80, 0xf2, 0x44, 0x45,
  1914. 0x2d, 0xa3, 0xeb, 0xf1, 0xc5, 0xd8, 0x2c, 0xde,
  1915. 0xa2, 0x41, 0x89, 0x97, 0x20, 0x0e, 0xf8, 0x2e,
  1916. 0x44, 0xae, 0x7e, 0x3f
  1917. };
  1918. static const u8 T18[] = {
  1919. 0xa4, 0x4a, 0x82, 0x66, 0xee, 0x1c, 0x8e, 0xb0,
  1920. 0xc8, 0xb5, 0xd4, 0xcf, 0x5a, 0xe9, 0xf1, 0x9a
  1921. };
  1922. /* Test Case 19 */
  1923. # define K19 K1
  1924. # define P19 P1
  1925. # define IV19 IV1
  1926. # define C19 C1
  1927. static const u8 A19[] = {
  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, 0x1a, 0xaf, 0xd2, 0x55,
  1936. 0x52, 0x2d, 0xc1, 0xf0, 0x99, 0x56, 0x7d, 0x07,
  1937. 0xf4, 0x7f, 0x37, 0xa3, 0x2a, 0x84, 0x42, 0x7d,
  1938. 0x64, 0x3a, 0x8c, 0xdc, 0xbf, 0xe5, 0xc0, 0xc9,
  1939. 0x75, 0x98, 0xa2, 0xbd, 0x25, 0x55, 0xd1, 0xaa,
  1940. 0x8c, 0xb0, 0x8e, 0x48, 0x59, 0x0d, 0xbb, 0x3d,
  1941. 0xa7, 0xb0, 0x8b, 0x10, 0x56, 0x82, 0x88, 0x38,
  1942. 0xc5, 0xf6, 0x1e, 0x63, 0x93, 0xba, 0x7a, 0x0a,
  1943. 0xbc, 0xc9, 0xf6, 0x62, 0x89, 0x80, 0x15, 0xad
  1944. };
  1945. static const u8 T19[] = {
  1946. 0x5f, 0xea, 0x79, 0x3a, 0x2d, 0x6f, 0x97, 0x4d,
  1947. 0x37, 0xe6, 0x8e, 0x0c, 0xb8, 0xff, 0x94, 0x92
  1948. };
  1949. /* Test Case 20 */
  1950. # define K20 K1
  1951. # define A20 A1
  1952. /* this results in 0xff in counter LSB */
  1953. static const u8 IV20[64] = { 0xff, 0xff, 0xff, 0xff };
  1954. static const u8 P20[288];
  1955. static const u8 C20[] = {
  1956. 0x56, 0xb3, 0x37, 0x3c, 0xa9, 0xef, 0x6e, 0x4a,
  1957. 0x2b, 0x64, 0xfe, 0x1e, 0x9a, 0x17, 0xb6, 0x14,
  1958. 0x25, 0xf1, 0x0d, 0x47, 0xa7, 0x5a, 0x5f, 0xce,
  1959. 0x13, 0xef, 0xc6, 0xbc, 0x78, 0x4a, 0xf2, 0x4f,
  1960. 0x41, 0x41, 0xbd, 0xd4, 0x8c, 0xf7, 0xc7, 0x70,
  1961. 0x88, 0x7a, 0xfd, 0x57, 0x3c, 0xca, 0x54, 0x18,
  1962. 0xa9, 0xae, 0xff, 0xcd, 0x7c, 0x5c, 0xed, 0xdf,
  1963. 0xc6, 0xa7, 0x83, 0x97, 0xb9, 0xa8, 0x5b, 0x49,
  1964. 0x9d, 0xa5, 0x58, 0x25, 0x72, 0x67, 0xca, 0xab,
  1965. 0x2a, 0xd0, 0xb2, 0x3c, 0xa4, 0x76, 0xa5, 0x3c,
  1966. 0xb1, 0x7f, 0xb4, 0x1c, 0x4b, 0x8b, 0x47, 0x5c,
  1967. 0xb4, 0xf3, 0xf7, 0x16, 0x50, 0x94, 0xc2, 0x29,
  1968. 0xc9, 0xe8, 0xc4, 0xdc, 0x0a, 0x2a, 0x5f, 0xf1,
  1969. 0x90, 0x3e, 0x50, 0x15, 0x11, 0x22, 0x13, 0x76,
  1970. 0xa1, 0xcd, 0xb8, 0x36, 0x4c, 0x50, 0x61, 0xa2,
  1971. 0x0c, 0xae, 0x74, 0xbc, 0x4a, 0xcd, 0x76, 0xce,
  1972. 0xb0, 0xab, 0xc9, 0xfd, 0x32, 0x17, 0xef, 0x9f,
  1973. 0x8c, 0x90, 0xbe, 0x40, 0x2d, 0xdf, 0x6d, 0x86,
  1974. 0x97, 0xf4, 0xf8, 0x80, 0xdf, 0xf1, 0x5b, 0xfb,
  1975. 0x7a, 0x6b, 0x28, 0x24, 0x1e, 0xc8, 0xfe, 0x18,
  1976. 0x3c, 0x2d, 0x59, 0xe3, 0xf9, 0xdf, 0xff, 0x65,
  1977. 0x3c, 0x71, 0x26, 0xf0, 0xac, 0xb9, 0xe6, 0x42,
  1978. 0x11, 0xf4, 0x2b, 0xae, 0x12, 0xaf, 0x46, 0x2b,
  1979. 0x10, 0x70, 0xbe, 0xf1, 0xab, 0x5e, 0x36, 0x06,
  1980. 0x87, 0x2c, 0xa1, 0x0d, 0xee, 0x15, 0xb3, 0x24,
  1981. 0x9b, 0x1a, 0x1b, 0x95, 0x8f, 0x23, 0x13, 0x4c,
  1982. 0x4b, 0xcc, 0xb7, 0xd0, 0x32, 0x00, 0xbc, 0xe4,
  1983. 0x20, 0xa2, 0xf8, 0xeb, 0x66, 0xdc, 0xf3, 0x64,
  1984. 0x4d, 0x14, 0x23, 0xc1, 0xb5, 0x69, 0x90, 0x03,
  1985. 0xc1, 0x3e, 0xce, 0xf4, 0xbf, 0x38, 0xa3, 0xb6,
  1986. 0x0e, 0xed, 0xc3, 0x40, 0x33, 0xba, 0xc1, 0x90,
  1987. 0x27, 0x83, 0xdc, 0x6d, 0x89, 0xe2, 0xe7, 0x74,
  1988. 0x18, 0x8a, 0x43, 0x9c, 0x7e, 0xbc, 0xc0, 0x67,
  1989. 0x2d, 0xbd, 0xa4, 0xdd, 0xcf, 0xb2, 0x79, 0x46,
  1990. 0x13, 0xb0, 0xbe, 0x41, 0x31, 0x5e, 0xf7, 0x78,
  1991. 0x70, 0x8a, 0x70, 0xee, 0x7d, 0x75, 0x16, 0x5c
  1992. };
  1993. static const u8 T20[] = {
  1994. 0x8b, 0x30, 0x7f, 0x6b, 0x33, 0x28, 0x6d, 0x0a,
  1995. 0xb0, 0x26, 0xa9, 0xed, 0x3f, 0xe1, 0xe8, 0x5f
  1996. };
  1997. # define TEST_CASE(n) do { \
  1998. u8 out[sizeof(P##n)]; \
  1999. AES_set_encrypt_key(K##n,sizeof(K##n)*8,&key); \
  2000. CRYPTO_gcm128_init(&ctx,&key,(block128_f)AES_encrypt); \
  2001. CRYPTO_gcm128_setiv(&ctx,IV##n,sizeof(IV##n)); \
  2002. memset(out,0,sizeof(out)); \
  2003. if (A##n) CRYPTO_gcm128_aad(&ctx,A##n,sizeof(A##n)); \
  2004. if (P##n) CRYPTO_gcm128_encrypt(&ctx,P##n,out,sizeof(out)); \
  2005. if (CRYPTO_gcm128_finish(&ctx,T##n,16) || \
  2006. (C##n && memcmp(out,C##n,sizeof(out)))) \
  2007. ret++, printf ("encrypt test#%d failed.\n",n); \
  2008. CRYPTO_gcm128_setiv(&ctx,IV##n,sizeof(IV##n)); \
  2009. memset(out,0,sizeof(out)); \
  2010. if (A##n) CRYPTO_gcm128_aad(&ctx,A##n,sizeof(A##n)); \
  2011. if (C##n) CRYPTO_gcm128_decrypt(&ctx,C##n,out,sizeof(out)); \
  2012. if (CRYPTO_gcm128_finish(&ctx,T##n,16) || \
  2013. (P##n && memcmp(out,P##n,sizeof(out)))) \
  2014. ret++, printf ("decrypt test#%d failed.\n",n); \
  2015. } while(0)
  2016. int main()
  2017. {
  2018. GCM128_CONTEXT ctx;
  2019. AES_KEY key;
  2020. int ret = 0;
  2021. TEST_CASE(1);
  2022. TEST_CASE(2);
  2023. TEST_CASE(3);
  2024. TEST_CASE(4);
  2025. TEST_CASE(5);
  2026. TEST_CASE(6);
  2027. TEST_CASE(7);
  2028. TEST_CASE(8);
  2029. TEST_CASE(9);
  2030. TEST_CASE(10);
  2031. TEST_CASE(11);
  2032. TEST_CASE(12);
  2033. TEST_CASE(13);
  2034. TEST_CASE(14);
  2035. TEST_CASE(15);
  2036. TEST_CASE(16);
  2037. TEST_CASE(17);
  2038. TEST_CASE(18);
  2039. TEST_CASE(19);
  2040. TEST_CASE(20);
  2041. # ifdef OPENSSL_CPUID_OBJ
  2042. {
  2043. size_t start, stop, gcm_t, ctr_t, OPENSSL_rdtsc();
  2044. union {
  2045. u64 u;
  2046. u8 c[1024];
  2047. } buf;
  2048. int i;
  2049. AES_set_encrypt_key(K1, sizeof(K1) * 8, &key);
  2050. CRYPTO_gcm128_init(&ctx, &key, (block128_f) AES_encrypt);
  2051. CRYPTO_gcm128_setiv(&ctx, IV1, sizeof(IV1));
  2052. CRYPTO_gcm128_encrypt(&ctx, buf.c, buf.c, sizeof(buf));
  2053. start = OPENSSL_rdtsc();
  2054. CRYPTO_gcm128_encrypt(&ctx, buf.c, buf.c, sizeof(buf));
  2055. gcm_t = OPENSSL_rdtsc() - start;
  2056. CRYPTO_ctr128_encrypt(buf.c, buf.c, sizeof(buf),
  2057. &key, ctx.Yi.c, ctx.EKi.c, &ctx.mres,
  2058. (block128_f) AES_encrypt);
  2059. start = OPENSSL_rdtsc();
  2060. CRYPTO_ctr128_encrypt(buf.c, buf.c, sizeof(buf),
  2061. &key, ctx.Yi.c, ctx.EKi.c, &ctx.mres,
  2062. (block128_f) AES_encrypt);
  2063. ctr_t = OPENSSL_rdtsc() - start;
  2064. printf("%.2f-%.2f=%.2f\n",
  2065. gcm_t / (double)sizeof(buf),
  2066. ctr_t / (double)sizeof(buf),
  2067. (gcm_t - ctr_t) / (double)sizeof(buf));
  2068. # ifdef GHASH
  2069. {
  2070. void (*gcm_ghash_p) (u64 Xi[2], const u128 Htable[16],
  2071. const u8 *inp, size_t len) = ctx.ghash;
  2072. GHASH((&ctx), buf.c, sizeof(buf));
  2073. start = OPENSSL_rdtsc();
  2074. for (i = 0; i < 100; ++i)
  2075. GHASH((&ctx), buf.c, sizeof(buf));
  2076. gcm_t = OPENSSL_rdtsc() - start;
  2077. printf("%.2f\n", gcm_t / (double)sizeof(buf) / (double)i);
  2078. }
  2079. # endif
  2080. }
  2081. # endif
  2082. return ret;
  2083. }
  2084. #endif