gcm128.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859
  1. /*
  2. * Copyright 2010-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <string.h>
  10. #include <openssl/crypto.h>
  11. #include "internal/cryptlib.h"
  12. #include "internal/endian.h"
  13. #include "crypto/modes.h"
  14. #if defined(__GNUC__) && !defined(STRICT_ALIGNMENT)
  15. typedef size_t size_t_aX __attribute((__aligned__(1)));
  16. #else
  17. typedef size_t size_t_aX;
  18. #endif
  19. #if defined(BSWAP4) && defined(STRICT_ALIGNMENT)
  20. /* redefine, because alignment is ensured */
  21. # undef GETU32
  22. # define GETU32(p) BSWAP4(*(const u32 *)(p))
  23. # undef PUTU32
  24. # define PUTU32(p,v) *(u32 *)(p) = BSWAP4(v)
  25. #endif
  26. #define PACK(s) ((size_t)(s)<<(sizeof(size_t)*8-16))
  27. #define REDUCE1BIT(V) do { \
  28. if (sizeof(size_t)==8) { \
  29. u64 T = U64(0xe100000000000000) & (0-(V.lo&1)); \
  30. V.lo = (V.hi<<63)|(V.lo>>1); \
  31. V.hi = (V.hi>>1 )^T; \
  32. } \
  33. else { \
  34. u32 T = 0xe1000000U & (0-(u32)(V.lo&1)); \
  35. V.lo = (V.hi<<63)|(V.lo>>1); \
  36. V.hi = (V.hi>>1 )^((u64)T<<32); \
  37. } \
  38. } while(0)
  39. /*-
  40. * Even though permitted values for TABLE_BITS are 8, 4 and 1, it should
  41. * never be set to 8. 8 is effectively reserved for testing purposes.
  42. * TABLE_BITS>1 are lookup-table-driven implementations referred to as
  43. * "Shoup's" in GCM specification. In other words OpenSSL does not cover
  44. * whole spectrum of possible table driven implementations. Why? In
  45. * non-"Shoup's" case memory access pattern is segmented in such manner,
  46. * that it's trivial to see that cache timing information can reveal
  47. * fair portion of intermediate hash value. Given that ciphertext is
  48. * always available to attacker, it's possible for him to attempt to
  49. * deduce secret parameter H and if successful, tamper with messages
  50. * [which is nothing but trivial in CTR mode]. In "Shoup's" case it's
  51. * not as trivial, but there is no reason to believe that it's resistant
  52. * to cache-timing attack. And the thing about "8-bit" implementation is
  53. * that it consumes 16 (sixteen) times more memory, 4KB per individual
  54. * key + 1KB shared. Well, on pros side it should be twice as fast as
  55. * "4-bit" version. And for gcc-generated x86[_64] code, "8-bit" version
  56. * was observed to run ~75% faster, closer to 100% for commercial
  57. * compilers... Yet "4-bit" procedure is preferred, because it's
  58. * believed to provide better security-performance balance and adequate
  59. * all-round performance. "All-round" refers to things like:
  60. *
  61. * - shorter setup time effectively improves overall timing for
  62. * handling short messages;
  63. * - larger table allocation can become unbearable because of VM
  64. * subsystem penalties (for example on Windows large enough free
  65. * results in VM working set trimming, meaning that consequent
  66. * malloc would immediately incur working set expansion);
  67. * - larger table has larger cache footprint, which can affect
  68. * performance of other code paths (not necessarily even from same
  69. * thread in Hyper-Threading world);
  70. *
  71. * Value of 1 is not appropriate for performance reasons.
  72. */
  73. #if TABLE_BITS==8
  74. static void gcm_init_8bit(u128 Htable[256], u64 H[2])
  75. {
  76. int i, j;
  77. u128 V;
  78. Htable[0].hi = 0;
  79. Htable[0].lo = 0;
  80. V.hi = H[0];
  81. V.lo = H[1];
  82. for (Htable[128] = V, i = 64; i > 0; i >>= 1) {
  83. REDUCE1BIT(V);
  84. Htable[i] = V;
  85. }
  86. for (i = 2; i < 256; i <<= 1) {
  87. u128 *Hi = Htable + i, H0 = *Hi;
  88. for (j = 1; j < i; ++j) {
  89. Hi[j].hi = H0.hi ^ Htable[j].hi;
  90. Hi[j].lo = H0.lo ^ Htable[j].lo;
  91. }
  92. }
  93. }
  94. static void gcm_gmult_8bit(u64 Xi[2], const u128 Htable[256])
  95. {
  96. u128 Z = { 0, 0 };
  97. const u8 *xi = (const u8 *)Xi + 15;
  98. size_t rem, n = *xi;
  99. DECLARE_IS_ENDIAN;
  100. static const size_t rem_8bit[256] = {
  101. PACK(0x0000), PACK(0x01C2), PACK(0x0384), PACK(0x0246),
  102. PACK(0x0708), PACK(0x06CA), PACK(0x048C), PACK(0x054E),
  103. PACK(0x0E10), PACK(0x0FD2), PACK(0x0D94), PACK(0x0C56),
  104. PACK(0x0918), PACK(0x08DA), PACK(0x0A9C), PACK(0x0B5E),
  105. PACK(0x1C20), PACK(0x1DE2), PACK(0x1FA4), PACK(0x1E66),
  106. PACK(0x1B28), PACK(0x1AEA), PACK(0x18AC), PACK(0x196E),
  107. PACK(0x1230), PACK(0x13F2), PACK(0x11B4), PACK(0x1076),
  108. PACK(0x1538), PACK(0x14FA), PACK(0x16BC), PACK(0x177E),
  109. PACK(0x3840), PACK(0x3982), PACK(0x3BC4), PACK(0x3A06),
  110. PACK(0x3F48), PACK(0x3E8A), PACK(0x3CCC), PACK(0x3D0E),
  111. PACK(0x3650), PACK(0x3792), PACK(0x35D4), PACK(0x3416),
  112. PACK(0x3158), PACK(0x309A), PACK(0x32DC), PACK(0x331E),
  113. PACK(0x2460), PACK(0x25A2), PACK(0x27E4), PACK(0x2626),
  114. PACK(0x2368), PACK(0x22AA), PACK(0x20EC), PACK(0x212E),
  115. PACK(0x2A70), PACK(0x2BB2), PACK(0x29F4), PACK(0x2836),
  116. PACK(0x2D78), PACK(0x2CBA), PACK(0x2EFC), PACK(0x2F3E),
  117. PACK(0x7080), PACK(0x7142), PACK(0x7304), PACK(0x72C6),
  118. PACK(0x7788), PACK(0x764A), PACK(0x740C), PACK(0x75CE),
  119. PACK(0x7E90), PACK(0x7F52), PACK(0x7D14), PACK(0x7CD6),
  120. PACK(0x7998), PACK(0x785A), PACK(0x7A1C), PACK(0x7BDE),
  121. PACK(0x6CA0), PACK(0x6D62), PACK(0x6F24), PACK(0x6EE6),
  122. PACK(0x6BA8), PACK(0x6A6A), PACK(0x682C), PACK(0x69EE),
  123. PACK(0x62B0), PACK(0x6372), PACK(0x6134), PACK(0x60F6),
  124. PACK(0x65B8), PACK(0x647A), PACK(0x663C), PACK(0x67FE),
  125. PACK(0x48C0), PACK(0x4902), PACK(0x4B44), PACK(0x4A86),
  126. PACK(0x4FC8), PACK(0x4E0A), PACK(0x4C4C), PACK(0x4D8E),
  127. PACK(0x46D0), PACK(0x4712), PACK(0x4554), PACK(0x4496),
  128. PACK(0x41D8), PACK(0x401A), PACK(0x425C), PACK(0x439E),
  129. PACK(0x54E0), PACK(0x5522), PACK(0x5764), PACK(0x56A6),
  130. PACK(0x53E8), PACK(0x522A), PACK(0x506C), PACK(0x51AE),
  131. PACK(0x5AF0), PACK(0x5B32), PACK(0x5974), PACK(0x58B6),
  132. PACK(0x5DF8), PACK(0x5C3A), PACK(0x5E7C), PACK(0x5FBE),
  133. PACK(0xE100), PACK(0xE0C2), PACK(0xE284), PACK(0xE346),
  134. PACK(0xE608), PACK(0xE7CA), PACK(0xE58C), PACK(0xE44E),
  135. PACK(0xEF10), PACK(0xEED2), PACK(0xEC94), PACK(0xED56),
  136. PACK(0xE818), PACK(0xE9DA), PACK(0xEB9C), PACK(0xEA5E),
  137. PACK(0xFD20), PACK(0xFCE2), PACK(0xFEA4), PACK(0xFF66),
  138. PACK(0xFA28), PACK(0xFBEA), PACK(0xF9AC), PACK(0xF86E),
  139. PACK(0xF330), PACK(0xF2F2), PACK(0xF0B4), PACK(0xF176),
  140. PACK(0xF438), PACK(0xF5FA), PACK(0xF7BC), PACK(0xF67E),
  141. PACK(0xD940), PACK(0xD882), PACK(0xDAC4), PACK(0xDB06),
  142. PACK(0xDE48), PACK(0xDF8A), PACK(0xDDCC), PACK(0xDC0E),
  143. PACK(0xD750), PACK(0xD692), PACK(0xD4D4), PACK(0xD516),
  144. PACK(0xD058), PACK(0xD19A), PACK(0xD3DC), PACK(0xD21E),
  145. PACK(0xC560), PACK(0xC4A2), PACK(0xC6E4), PACK(0xC726),
  146. PACK(0xC268), PACK(0xC3AA), PACK(0xC1EC), PACK(0xC02E),
  147. PACK(0xCB70), PACK(0xCAB2), PACK(0xC8F4), PACK(0xC936),
  148. PACK(0xCC78), PACK(0xCDBA), PACK(0xCFFC), PACK(0xCE3E),
  149. PACK(0x9180), PACK(0x9042), PACK(0x9204), PACK(0x93C6),
  150. PACK(0x9688), PACK(0x974A), PACK(0x950C), PACK(0x94CE),
  151. PACK(0x9F90), PACK(0x9E52), PACK(0x9C14), PACK(0x9DD6),
  152. PACK(0x9898), PACK(0x995A), PACK(0x9B1C), PACK(0x9ADE),
  153. PACK(0x8DA0), PACK(0x8C62), PACK(0x8E24), PACK(0x8FE6),
  154. PACK(0x8AA8), PACK(0x8B6A), PACK(0x892C), PACK(0x88EE),
  155. PACK(0x83B0), PACK(0x8272), PACK(0x8034), PACK(0x81F6),
  156. PACK(0x84B8), PACK(0x857A), PACK(0x873C), PACK(0x86FE),
  157. PACK(0xA9C0), PACK(0xA802), PACK(0xAA44), PACK(0xAB86),
  158. PACK(0xAEC8), PACK(0xAF0A), PACK(0xAD4C), PACK(0xAC8E),
  159. PACK(0xA7D0), PACK(0xA612), PACK(0xA454), PACK(0xA596),
  160. PACK(0xA0D8), PACK(0xA11A), PACK(0xA35C), PACK(0xA29E),
  161. PACK(0xB5E0), PACK(0xB422), PACK(0xB664), PACK(0xB7A6),
  162. PACK(0xB2E8), PACK(0xB32A), PACK(0xB16C), PACK(0xB0AE),
  163. PACK(0xBBF0), PACK(0xBA32), PACK(0xB874), PACK(0xB9B6),
  164. PACK(0xBCF8), PACK(0xBD3A), PACK(0xBF7C), PACK(0xBEBE)
  165. };
  166. while (1) {
  167. Z.hi ^= Htable[n].hi;
  168. Z.lo ^= Htable[n].lo;
  169. if ((u8 *)Xi == xi)
  170. break;
  171. n = *(--xi);
  172. rem = (size_t)Z.lo & 0xff;
  173. Z.lo = (Z.hi << 56) | (Z.lo >> 8);
  174. Z.hi = (Z.hi >> 8);
  175. if (sizeof(size_t) == 8)
  176. Z.hi ^= rem_8bit[rem];
  177. else
  178. Z.hi ^= (u64)rem_8bit[rem] << 32;
  179. }
  180. if (IS_LITTLE_ENDIAN) {
  181. # ifdef BSWAP8
  182. Xi[0] = BSWAP8(Z.hi);
  183. Xi[1] = BSWAP8(Z.lo);
  184. # else
  185. u8 *p = (u8 *)Xi;
  186. u32 v;
  187. v = (u32)(Z.hi >> 32);
  188. PUTU32(p, v);
  189. v = (u32)(Z.hi);
  190. PUTU32(p + 4, v);
  191. v = (u32)(Z.lo >> 32);
  192. PUTU32(p + 8, v);
  193. v = (u32)(Z.lo);
  194. PUTU32(p + 12, v);
  195. # endif
  196. } else {
  197. Xi[0] = Z.hi;
  198. Xi[1] = Z.lo;
  199. }
  200. }
  201. # define GCM_MUL(ctx) gcm_gmult_8bit(ctx->Xi.u,ctx->Htable)
  202. #elif TABLE_BITS==4
  203. static void gcm_init_4bit(u128 Htable[16], u64 H[2])
  204. {
  205. u128 V;
  206. # if defined(OPENSSL_SMALL_FOOTPRINT)
  207. int i;
  208. # endif
  209. Htable[0].hi = 0;
  210. Htable[0].lo = 0;
  211. V.hi = H[0];
  212. V.lo = H[1];
  213. # if defined(OPENSSL_SMALL_FOOTPRINT)
  214. for (Htable[8] = V, i = 4; i > 0; i >>= 1) {
  215. REDUCE1BIT(V);
  216. Htable[i] = V;
  217. }
  218. for (i = 2; i < 16; i <<= 1) {
  219. u128 *Hi = Htable + i;
  220. int j;
  221. for (V = *Hi, j = 1; j < i; ++j) {
  222. Hi[j].hi = V.hi ^ Htable[j].hi;
  223. Hi[j].lo = V.lo ^ Htable[j].lo;
  224. }
  225. }
  226. # else
  227. Htable[8] = V;
  228. REDUCE1BIT(V);
  229. Htable[4] = V;
  230. REDUCE1BIT(V);
  231. Htable[2] = V;
  232. REDUCE1BIT(V);
  233. Htable[1] = V;
  234. Htable[3].hi = V.hi ^ Htable[2].hi, Htable[3].lo = V.lo ^ Htable[2].lo;
  235. V = Htable[4];
  236. Htable[5].hi = V.hi ^ Htable[1].hi, Htable[5].lo = V.lo ^ Htable[1].lo;
  237. Htable[6].hi = V.hi ^ Htable[2].hi, Htable[6].lo = V.lo ^ Htable[2].lo;
  238. Htable[7].hi = V.hi ^ Htable[3].hi, Htable[7].lo = V.lo ^ Htable[3].lo;
  239. V = Htable[8];
  240. Htable[9].hi = V.hi ^ Htable[1].hi, Htable[9].lo = V.lo ^ Htable[1].lo;
  241. Htable[10].hi = V.hi ^ Htable[2].hi, Htable[10].lo = V.lo ^ Htable[2].lo;
  242. Htable[11].hi = V.hi ^ Htable[3].hi, Htable[11].lo = V.lo ^ Htable[3].lo;
  243. Htable[12].hi = V.hi ^ Htable[4].hi, Htable[12].lo = V.lo ^ Htable[4].lo;
  244. Htable[13].hi = V.hi ^ Htable[5].hi, Htable[13].lo = V.lo ^ Htable[5].lo;
  245. Htable[14].hi = V.hi ^ Htable[6].hi, Htable[14].lo = V.lo ^ Htable[6].lo;
  246. Htable[15].hi = V.hi ^ Htable[7].hi, Htable[15].lo = V.lo ^ Htable[7].lo;
  247. # endif
  248. # if defined(GHASH_ASM) && (defined(__arm__) || defined(__arm))
  249. /*
  250. * ARM assembler expects specific dword order in Htable.
  251. */
  252. {
  253. int j;
  254. DECLARE_IS_ENDIAN;
  255. if (IS_LITTLE_ENDIAN)
  256. for (j = 0; j < 16; ++j) {
  257. V = Htable[j];
  258. Htable[j].hi = V.lo;
  259. Htable[j].lo = V.hi;
  260. } else
  261. for (j = 0; j < 16; ++j) {
  262. V = Htable[j];
  263. Htable[j].hi = V.lo << 32 | V.lo >> 32;
  264. Htable[j].lo = V.hi << 32 | V.hi >> 32;
  265. }
  266. }
  267. # endif
  268. }
  269. # ifndef GHASH_ASM
  270. static const size_t rem_4bit[16] = {
  271. PACK(0x0000), PACK(0x1C20), PACK(0x3840), PACK(0x2460),
  272. PACK(0x7080), PACK(0x6CA0), PACK(0x48C0), PACK(0x54E0),
  273. PACK(0xE100), PACK(0xFD20), PACK(0xD940), PACK(0xC560),
  274. PACK(0x9180), PACK(0x8DA0), PACK(0xA9C0), PACK(0xB5E0)
  275. };
  276. static void gcm_gmult_4bit(u64 Xi[2], const u128 Htable[16])
  277. {
  278. u128 Z;
  279. int cnt = 15;
  280. size_t rem, nlo, nhi;
  281. DECLARE_IS_ENDIAN;
  282. nlo = ((const u8 *)Xi)[15];
  283. nhi = nlo >> 4;
  284. nlo &= 0xf;
  285. Z.hi = Htable[nlo].hi;
  286. Z.lo = Htable[nlo].lo;
  287. while (1) {
  288. rem = (size_t)Z.lo & 0xf;
  289. Z.lo = (Z.hi << 60) | (Z.lo >> 4);
  290. Z.hi = (Z.hi >> 4);
  291. if (sizeof(size_t) == 8)
  292. Z.hi ^= rem_4bit[rem];
  293. else
  294. Z.hi ^= (u64)rem_4bit[rem] << 32;
  295. Z.hi ^= Htable[nhi].hi;
  296. Z.lo ^= Htable[nhi].lo;
  297. if (--cnt < 0)
  298. break;
  299. nlo = ((const u8 *)Xi)[cnt];
  300. nhi = nlo >> 4;
  301. nlo &= 0xf;
  302. rem = (size_t)Z.lo & 0xf;
  303. Z.lo = (Z.hi << 60) | (Z.lo >> 4);
  304. Z.hi = (Z.hi >> 4);
  305. if (sizeof(size_t) == 8)
  306. Z.hi ^= rem_4bit[rem];
  307. else
  308. Z.hi ^= (u64)rem_4bit[rem] << 32;
  309. Z.hi ^= Htable[nlo].hi;
  310. Z.lo ^= Htable[nlo].lo;
  311. }
  312. if (IS_LITTLE_ENDIAN) {
  313. # ifdef BSWAP8
  314. Xi[0] = BSWAP8(Z.hi);
  315. Xi[1] = BSWAP8(Z.lo);
  316. # else
  317. u8 *p = (u8 *)Xi;
  318. u32 v;
  319. v = (u32)(Z.hi >> 32);
  320. PUTU32(p, v);
  321. v = (u32)(Z.hi);
  322. PUTU32(p + 4, v);
  323. v = (u32)(Z.lo >> 32);
  324. PUTU32(p + 8, v);
  325. v = (u32)(Z.lo);
  326. PUTU32(p + 12, v);
  327. # endif
  328. } else {
  329. Xi[0] = Z.hi;
  330. Xi[1] = Z.lo;
  331. }
  332. }
  333. # if !defined(OPENSSL_SMALL_FOOTPRINT)
  334. /*
  335. * Streamed gcm_mult_4bit, see CRYPTO_gcm128_[en|de]crypt for
  336. * details... Compiler-generated code doesn't seem to give any
  337. * performance improvement, at least not on x86[_64]. It's here
  338. * mostly as reference and a placeholder for possible future
  339. * non-trivial optimization[s]...
  340. */
  341. static void gcm_ghash_4bit(u64 Xi[2], const u128 Htable[16],
  342. const u8 *inp, size_t len)
  343. {
  344. u128 Z;
  345. int cnt;
  346. size_t rem, nlo, nhi;
  347. DECLARE_IS_ENDIAN;
  348. # if 1
  349. do {
  350. cnt = 15;
  351. nlo = ((const u8 *)Xi)[15];
  352. nlo ^= inp[15];
  353. nhi = nlo >> 4;
  354. nlo &= 0xf;
  355. Z.hi = Htable[nlo].hi;
  356. Z.lo = Htable[nlo].lo;
  357. while (1) {
  358. rem = (size_t)Z.lo & 0xf;
  359. Z.lo = (Z.hi << 60) | (Z.lo >> 4);
  360. Z.hi = (Z.hi >> 4);
  361. if (sizeof(size_t) == 8)
  362. Z.hi ^= rem_4bit[rem];
  363. else
  364. Z.hi ^= (u64)rem_4bit[rem] << 32;
  365. Z.hi ^= Htable[nhi].hi;
  366. Z.lo ^= Htable[nhi].lo;
  367. if (--cnt < 0)
  368. break;
  369. nlo = ((const u8 *)Xi)[cnt];
  370. nlo ^= inp[cnt];
  371. nhi = nlo >> 4;
  372. nlo &= 0xf;
  373. rem = (size_t)Z.lo & 0xf;
  374. Z.lo = (Z.hi << 60) | (Z.lo >> 4);
  375. Z.hi = (Z.hi >> 4);
  376. if (sizeof(size_t) == 8)
  377. Z.hi ^= rem_4bit[rem];
  378. else
  379. Z.hi ^= (u64)rem_4bit[rem] << 32;
  380. Z.hi ^= Htable[nlo].hi;
  381. Z.lo ^= Htable[nlo].lo;
  382. }
  383. # else
  384. /*
  385. * Extra 256+16 bytes per-key plus 512 bytes shared tables
  386. * [should] give ~50% improvement... One could have PACK()-ed
  387. * the rem_8bit even here, but the priority is to minimize
  388. * cache footprint...
  389. */
  390. u128 Hshr4[16]; /* Htable shifted right by 4 bits */
  391. u8 Hshl4[16]; /* Htable shifted left by 4 bits */
  392. static const unsigned short rem_8bit[256] = {
  393. 0x0000, 0x01C2, 0x0384, 0x0246, 0x0708, 0x06CA, 0x048C, 0x054E,
  394. 0x0E10, 0x0FD2, 0x0D94, 0x0C56, 0x0918, 0x08DA, 0x0A9C, 0x0B5E,
  395. 0x1C20, 0x1DE2, 0x1FA4, 0x1E66, 0x1B28, 0x1AEA, 0x18AC, 0x196E,
  396. 0x1230, 0x13F2, 0x11B4, 0x1076, 0x1538, 0x14FA, 0x16BC, 0x177E,
  397. 0x3840, 0x3982, 0x3BC4, 0x3A06, 0x3F48, 0x3E8A, 0x3CCC, 0x3D0E,
  398. 0x3650, 0x3792, 0x35D4, 0x3416, 0x3158, 0x309A, 0x32DC, 0x331E,
  399. 0x2460, 0x25A2, 0x27E4, 0x2626, 0x2368, 0x22AA, 0x20EC, 0x212E,
  400. 0x2A70, 0x2BB2, 0x29F4, 0x2836, 0x2D78, 0x2CBA, 0x2EFC, 0x2F3E,
  401. 0x7080, 0x7142, 0x7304, 0x72C6, 0x7788, 0x764A, 0x740C, 0x75CE,
  402. 0x7E90, 0x7F52, 0x7D14, 0x7CD6, 0x7998, 0x785A, 0x7A1C, 0x7BDE,
  403. 0x6CA0, 0x6D62, 0x6F24, 0x6EE6, 0x6BA8, 0x6A6A, 0x682C, 0x69EE,
  404. 0x62B0, 0x6372, 0x6134, 0x60F6, 0x65B8, 0x647A, 0x663C, 0x67FE,
  405. 0x48C0, 0x4902, 0x4B44, 0x4A86, 0x4FC8, 0x4E0A, 0x4C4C, 0x4D8E,
  406. 0x46D0, 0x4712, 0x4554, 0x4496, 0x41D8, 0x401A, 0x425C, 0x439E,
  407. 0x54E0, 0x5522, 0x5764, 0x56A6, 0x53E8, 0x522A, 0x506C, 0x51AE,
  408. 0x5AF0, 0x5B32, 0x5974, 0x58B6, 0x5DF8, 0x5C3A, 0x5E7C, 0x5FBE,
  409. 0xE100, 0xE0C2, 0xE284, 0xE346, 0xE608, 0xE7CA, 0xE58C, 0xE44E,
  410. 0xEF10, 0xEED2, 0xEC94, 0xED56, 0xE818, 0xE9DA, 0xEB9C, 0xEA5E,
  411. 0xFD20, 0xFCE2, 0xFEA4, 0xFF66, 0xFA28, 0xFBEA, 0xF9AC, 0xF86E,
  412. 0xF330, 0xF2F2, 0xF0B4, 0xF176, 0xF438, 0xF5FA, 0xF7BC, 0xF67E,
  413. 0xD940, 0xD882, 0xDAC4, 0xDB06, 0xDE48, 0xDF8A, 0xDDCC, 0xDC0E,
  414. 0xD750, 0xD692, 0xD4D4, 0xD516, 0xD058, 0xD19A, 0xD3DC, 0xD21E,
  415. 0xC560, 0xC4A2, 0xC6E4, 0xC726, 0xC268, 0xC3AA, 0xC1EC, 0xC02E,
  416. 0xCB70, 0xCAB2, 0xC8F4, 0xC936, 0xCC78, 0xCDBA, 0xCFFC, 0xCE3E,
  417. 0x9180, 0x9042, 0x9204, 0x93C6, 0x9688, 0x974A, 0x950C, 0x94CE,
  418. 0x9F90, 0x9E52, 0x9C14, 0x9DD6, 0x9898, 0x995A, 0x9B1C, 0x9ADE,
  419. 0x8DA0, 0x8C62, 0x8E24, 0x8FE6, 0x8AA8, 0x8B6A, 0x892C, 0x88EE,
  420. 0x83B0, 0x8272, 0x8034, 0x81F6, 0x84B8, 0x857A, 0x873C, 0x86FE,
  421. 0xA9C0, 0xA802, 0xAA44, 0xAB86, 0xAEC8, 0xAF0A, 0xAD4C, 0xAC8E,
  422. 0xA7D0, 0xA612, 0xA454, 0xA596, 0xA0D8, 0xA11A, 0xA35C, 0xA29E,
  423. 0xB5E0, 0xB422, 0xB664, 0xB7A6, 0xB2E8, 0xB32A, 0xB16C, 0xB0AE,
  424. 0xBBF0, 0xBA32, 0xB874, 0xB9B6, 0xBCF8, 0xBD3A, 0xBF7C, 0xBEBE
  425. };
  426. /*
  427. * This pre-processing phase slows down procedure by approximately
  428. * same time as it makes each loop spin faster. In other words
  429. * single block performance is approximately same as straightforward
  430. * "4-bit" implementation, and then it goes only faster...
  431. */
  432. for (cnt = 0; cnt < 16; ++cnt) {
  433. Z.hi = Htable[cnt].hi;
  434. Z.lo = Htable[cnt].lo;
  435. Hshr4[cnt].lo = (Z.hi << 60) | (Z.lo >> 4);
  436. Hshr4[cnt].hi = (Z.hi >> 4);
  437. Hshl4[cnt] = (u8)(Z.lo << 4);
  438. }
  439. do {
  440. for (Z.lo = 0, Z.hi = 0, cnt = 15; cnt; --cnt) {
  441. nlo = ((const u8 *)Xi)[cnt];
  442. nlo ^= inp[cnt];
  443. nhi = nlo >> 4;
  444. nlo &= 0xf;
  445. Z.hi ^= Htable[nlo].hi;
  446. Z.lo ^= Htable[nlo].lo;
  447. rem = (size_t)Z.lo & 0xff;
  448. Z.lo = (Z.hi << 56) | (Z.lo >> 8);
  449. Z.hi = (Z.hi >> 8);
  450. Z.hi ^= Hshr4[nhi].hi;
  451. Z.lo ^= Hshr4[nhi].lo;
  452. Z.hi ^= (u64)rem_8bit[rem ^ Hshl4[nhi]] << 48;
  453. }
  454. nlo = ((const u8 *)Xi)[0];
  455. nlo ^= inp[0];
  456. nhi = nlo >> 4;
  457. nlo &= 0xf;
  458. Z.hi ^= Htable[nlo].hi;
  459. Z.lo ^= Htable[nlo].lo;
  460. rem = (size_t)Z.lo & 0xf;
  461. Z.lo = (Z.hi << 60) | (Z.lo >> 4);
  462. Z.hi = (Z.hi >> 4);
  463. Z.hi ^= Htable[nhi].hi;
  464. Z.lo ^= Htable[nhi].lo;
  465. Z.hi ^= ((u64)rem_8bit[rem << 4]) << 48;
  466. # endif
  467. if (IS_LITTLE_ENDIAN) {
  468. # ifdef BSWAP8
  469. Xi[0] = BSWAP8(Z.hi);
  470. Xi[1] = BSWAP8(Z.lo);
  471. # else
  472. u8 *p = (u8 *)Xi;
  473. u32 v;
  474. v = (u32)(Z.hi >> 32);
  475. PUTU32(p, v);
  476. v = (u32)(Z.hi);
  477. PUTU32(p + 4, v);
  478. v = (u32)(Z.lo >> 32);
  479. PUTU32(p + 8, v);
  480. v = (u32)(Z.lo);
  481. PUTU32(p + 12, v);
  482. # endif
  483. } else {
  484. Xi[0] = Z.hi;
  485. Xi[1] = Z.lo;
  486. }
  487. } while (inp += 16, len -= 16);
  488. }
  489. # endif
  490. # else
  491. void gcm_gmult_4bit(u64 Xi[2], const u128 Htable[16]);
  492. void gcm_ghash_4bit(u64 Xi[2], const u128 Htable[16], const u8 *inp,
  493. size_t len);
  494. # endif
  495. # define GCM_MUL(ctx) gcm_gmult_4bit(ctx->Xi.u,ctx->Htable)
  496. # if defined(GHASH_ASM) || !defined(OPENSSL_SMALL_FOOTPRINT)
  497. # define GHASH(ctx,in,len) gcm_ghash_4bit((ctx)->Xi.u,(ctx)->Htable,in,len)
  498. /*
  499. * GHASH_CHUNK is "stride parameter" missioned to mitigate cache trashing
  500. * effect. In other words idea is to hash data while it's still in L1 cache
  501. * after encryption pass...
  502. */
  503. # define GHASH_CHUNK (3*1024)
  504. # endif
  505. #else /* TABLE_BITS */
  506. static void gcm_gmult_1bit(u64 Xi[2], const u64 H[2])
  507. {
  508. u128 V, Z = { 0, 0 };
  509. long X;
  510. int i, j;
  511. const long *xi = (const long *)Xi;
  512. DECLARE_IS_ENDIAN;
  513. V.hi = H[0]; /* H is in host byte order, no byte swapping */
  514. V.lo = H[1];
  515. for (j = 0; j < 16 / sizeof(long); ++j) {
  516. if (IS_LITTLE_ENDIAN) {
  517. if (sizeof(long) == 8) {
  518. # ifdef BSWAP8
  519. X = (long)(BSWAP8(xi[j]));
  520. # else
  521. const u8 *p = (const u8 *)(xi + j);
  522. X = (long)((u64)GETU32(p) << 32 | GETU32(p + 4));
  523. # endif
  524. } else {
  525. const u8 *p = (const u8 *)(xi + j);
  526. X = (long)GETU32(p);
  527. }
  528. } else
  529. X = xi[j];
  530. for (i = 0; i < 8 * sizeof(long); ++i, X <<= 1) {
  531. u64 M = (u64)(X >> (8 * sizeof(long) - 1));
  532. Z.hi ^= V.hi & M;
  533. Z.lo ^= V.lo & M;
  534. REDUCE1BIT(V);
  535. }
  536. }
  537. if (IS_LITTLE_ENDIAN) {
  538. # ifdef BSWAP8
  539. Xi[0] = BSWAP8(Z.hi);
  540. Xi[1] = BSWAP8(Z.lo);
  541. # else
  542. u8 *p = (u8 *)Xi;
  543. u32 v;
  544. v = (u32)(Z.hi >> 32);
  545. PUTU32(p, v);
  546. v = (u32)(Z.hi);
  547. PUTU32(p + 4, v);
  548. v = (u32)(Z.lo >> 32);
  549. PUTU32(p + 8, v);
  550. v = (u32)(Z.lo);
  551. PUTU32(p + 12, v);
  552. # endif
  553. } else {
  554. Xi[0] = Z.hi;
  555. Xi[1] = Z.lo;
  556. }
  557. }
  558. # define GCM_MUL(ctx) gcm_gmult_1bit(ctx->Xi.u,ctx->H.u)
  559. #endif
  560. #if TABLE_BITS==4 && (defined(GHASH_ASM) || defined(OPENSSL_CPUID_OBJ))
  561. # if !defined(I386_ONLY) && \
  562. (defined(__i386) || defined(__i386__) || \
  563. defined(__x86_64) || defined(__x86_64__) || \
  564. defined(_M_IX86) || defined(_M_AMD64) || defined(_M_X64))
  565. # define GHASH_ASM_X86_OR_64
  566. # define GCM_FUNCREF_4BIT
  567. void gcm_init_clmul(u128 Htable[16], const u64 Xi[2]);
  568. void gcm_gmult_clmul(u64 Xi[2], const u128 Htable[16]);
  569. void gcm_ghash_clmul(u64 Xi[2], const u128 Htable[16], const u8 *inp,
  570. size_t len);
  571. # if defined(__i386) || defined(__i386__) || defined(_M_IX86)
  572. # define gcm_init_avx gcm_init_clmul
  573. # define gcm_gmult_avx gcm_gmult_clmul
  574. # define gcm_ghash_avx gcm_ghash_clmul
  575. # else
  576. void gcm_init_avx(u128 Htable[16], const u64 Xi[2]);
  577. void gcm_gmult_avx(u64 Xi[2], const u128 Htable[16]);
  578. void gcm_ghash_avx(u64 Xi[2], const u128 Htable[16], const u8 *inp,
  579. size_t len);
  580. # endif
  581. # if defined(__i386) || defined(__i386__) || defined(_M_IX86)
  582. # define GHASH_ASM_X86
  583. void gcm_gmult_4bit_mmx(u64 Xi[2], const u128 Htable[16]);
  584. void gcm_ghash_4bit_mmx(u64 Xi[2], const u128 Htable[16], const u8 *inp,
  585. size_t len);
  586. void gcm_gmult_4bit_x86(u64 Xi[2], const u128 Htable[16]);
  587. void gcm_ghash_4bit_x86(u64 Xi[2], const u128 Htable[16], const u8 *inp,
  588. size_t len);
  589. # endif
  590. # elif defined(__arm__) || defined(__arm) || defined(__aarch64__)
  591. # include "arm_arch.h"
  592. # if __ARM_MAX_ARCH__>=7
  593. # define GHASH_ASM_ARM
  594. # define GCM_FUNCREF_4BIT
  595. # define PMULL_CAPABLE (OPENSSL_armcap_P & ARMV8_PMULL)
  596. # if defined(__arm__) || defined(__arm)
  597. # define NEON_CAPABLE (OPENSSL_armcap_P & ARMV7_NEON)
  598. # endif
  599. void gcm_init_neon(u128 Htable[16], const u64 Xi[2]);
  600. void gcm_gmult_neon(u64 Xi[2], const u128 Htable[16]);
  601. void gcm_ghash_neon(u64 Xi[2], const u128 Htable[16], const u8 *inp,
  602. size_t len);
  603. void gcm_init_v8(u128 Htable[16], const u64 Xi[2]);
  604. void gcm_gmult_v8(u64 Xi[2], const u128 Htable[16]);
  605. void gcm_ghash_v8(u64 Xi[2], const u128 Htable[16], const u8 *inp,
  606. size_t len);
  607. # endif
  608. # elif defined(__sparc__) || defined(__sparc)
  609. # include "sparc_arch.h"
  610. # define GHASH_ASM_SPARC
  611. # define GCM_FUNCREF_4BIT
  612. extern unsigned int OPENSSL_sparcv9cap_P[];
  613. void gcm_init_vis3(u128 Htable[16], const u64 Xi[2]);
  614. void gcm_gmult_vis3(u64 Xi[2], const u128 Htable[16]);
  615. void gcm_ghash_vis3(u64 Xi[2], const u128 Htable[16], const u8 *inp,
  616. size_t len);
  617. # elif defined(OPENSSL_CPUID_OBJ) && (defined(__powerpc__) || defined(__ppc__) || defined(_ARCH_PPC))
  618. # include "ppc_arch.h"
  619. # define GHASH_ASM_PPC
  620. # define GCM_FUNCREF_4BIT
  621. void gcm_init_p8(u128 Htable[16], const u64 Xi[2]);
  622. void gcm_gmult_p8(u64 Xi[2], const u128 Htable[16]);
  623. void gcm_ghash_p8(u64 Xi[2], const u128 Htable[16], const u8 *inp,
  624. size_t len);
  625. # endif
  626. #endif
  627. #ifdef GCM_FUNCREF_4BIT
  628. # undef GCM_MUL
  629. # define GCM_MUL(ctx) (*gcm_gmult_p)(ctx->Xi.u,ctx->Htable)
  630. # ifdef GHASH
  631. # undef GHASH
  632. # define GHASH(ctx,in,len) (*gcm_ghash_p)(ctx->Xi.u,ctx->Htable,in,len)
  633. # endif
  634. #endif
  635. void CRYPTO_gcm128_init(GCM128_CONTEXT *ctx, void *key, block128_f block)
  636. {
  637. DECLARE_IS_ENDIAN;
  638. memset(ctx, 0, sizeof(*ctx));
  639. ctx->block = block;
  640. ctx->key = key;
  641. (*block) (ctx->H.c, ctx->H.c, key);
  642. if (IS_LITTLE_ENDIAN) {
  643. /* H is stored in host byte order */
  644. #ifdef BSWAP8
  645. ctx->H.u[0] = BSWAP8(ctx->H.u[0]);
  646. ctx->H.u[1] = BSWAP8(ctx->H.u[1]);
  647. #else
  648. u8 *p = ctx->H.c;
  649. u64 hi, lo;
  650. hi = (u64)GETU32(p) << 32 | GETU32(p + 4);
  651. lo = (u64)GETU32(p + 8) << 32 | GETU32(p + 12);
  652. ctx->H.u[0] = hi;
  653. ctx->H.u[1] = lo;
  654. #endif
  655. }
  656. #if TABLE_BITS==8
  657. gcm_init_8bit(ctx->Htable, ctx->H.u);
  658. #elif TABLE_BITS==4
  659. # if defined(GHASH)
  660. # define CTX__GHASH(f) (ctx->ghash = (f))
  661. # else
  662. # define CTX__GHASH(f) (ctx->ghash = NULL)
  663. # endif
  664. # if defined(GHASH_ASM_X86_OR_64)
  665. # if !defined(GHASH_ASM_X86) || defined(OPENSSL_IA32_SSE2)
  666. if (OPENSSL_ia32cap_P[1] & (1 << 1)) { /* check PCLMULQDQ bit */
  667. if (((OPENSSL_ia32cap_P[1] >> 22) & 0x41) == 0x41) { /* AVX+MOVBE */
  668. gcm_init_avx(ctx->Htable, ctx->H.u);
  669. ctx->gmult = gcm_gmult_avx;
  670. CTX__GHASH(gcm_ghash_avx);
  671. } else {
  672. gcm_init_clmul(ctx->Htable, ctx->H.u);
  673. ctx->gmult = gcm_gmult_clmul;
  674. CTX__GHASH(gcm_ghash_clmul);
  675. }
  676. return;
  677. }
  678. # endif
  679. gcm_init_4bit(ctx->Htable, ctx->H.u);
  680. # if defined(GHASH_ASM_X86) /* x86 only */
  681. # if defined(OPENSSL_IA32_SSE2)
  682. if (OPENSSL_ia32cap_P[0] & (1 << 25)) { /* check SSE bit */
  683. # else
  684. if (OPENSSL_ia32cap_P[0] & (1 << 23)) { /* check MMX bit */
  685. # endif
  686. ctx->gmult = gcm_gmult_4bit_mmx;
  687. CTX__GHASH(gcm_ghash_4bit_mmx);
  688. } else {
  689. ctx->gmult = gcm_gmult_4bit_x86;
  690. CTX__GHASH(gcm_ghash_4bit_x86);
  691. }
  692. # else
  693. ctx->gmult = gcm_gmult_4bit;
  694. CTX__GHASH(gcm_ghash_4bit);
  695. # endif
  696. # elif defined(GHASH_ASM_ARM)
  697. # ifdef PMULL_CAPABLE
  698. if (PMULL_CAPABLE) {
  699. gcm_init_v8(ctx->Htable, ctx->H.u);
  700. ctx->gmult = gcm_gmult_v8;
  701. CTX__GHASH(gcm_ghash_v8);
  702. } else
  703. # endif
  704. # ifdef NEON_CAPABLE
  705. if (NEON_CAPABLE) {
  706. gcm_init_neon(ctx->Htable, ctx->H.u);
  707. ctx->gmult = gcm_gmult_neon;
  708. CTX__GHASH(gcm_ghash_neon);
  709. } else
  710. # endif
  711. {
  712. gcm_init_4bit(ctx->Htable, ctx->H.u);
  713. ctx->gmult = gcm_gmult_4bit;
  714. CTX__GHASH(gcm_ghash_4bit);
  715. }
  716. # elif defined(GHASH_ASM_SPARC)
  717. if (OPENSSL_sparcv9cap_P[0] & SPARCV9_VIS3) {
  718. gcm_init_vis3(ctx->Htable, ctx->H.u);
  719. ctx->gmult = gcm_gmult_vis3;
  720. CTX__GHASH(gcm_ghash_vis3);
  721. } else {
  722. gcm_init_4bit(ctx->Htable, ctx->H.u);
  723. ctx->gmult = gcm_gmult_4bit;
  724. CTX__GHASH(gcm_ghash_4bit);
  725. }
  726. # elif defined(GHASH_ASM_PPC)
  727. if (OPENSSL_ppccap_P & PPC_CRYPTO207) {
  728. gcm_init_p8(ctx->Htable, ctx->H.u);
  729. ctx->gmult = gcm_gmult_p8;
  730. CTX__GHASH(gcm_ghash_p8);
  731. } else {
  732. gcm_init_4bit(ctx->Htable, ctx->H.u);
  733. ctx->gmult = gcm_gmult_4bit;
  734. CTX__GHASH(gcm_ghash_4bit);
  735. }
  736. # else
  737. gcm_init_4bit(ctx->Htable, ctx->H.u);
  738. # endif
  739. # undef CTX__GHASH
  740. #endif
  741. }
  742. void CRYPTO_gcm128_setiv(GCM128_CONTEXT *ctx, const unsigned char *iv,
  743. size_t len)
  744. {
  745. DECLARE_IS_ENDIAN;
  746. unsigned int ctr;
  747. #ifdef GCM_FUNCREF_4BIT
  748. void (*gcm_gmult_p) (u64 Xi[2], const u128 Htable[16]) = ctx->gmult;
  749. #endif
  750. ctx->len.u[0] = 0; /* AAD length */
  751. ctx->len.u[1] = 0; /* message length */
  752. ctx->ares = 0;
  753. ctx->mres = 0;
  754. if (len == 12) {
  755. memcpy(ctx->Yi.c, iv, 12);
  756. ctx->Yi.c[12] = 0;
  757. ctx->Yi.c[13] = 0;
  758. ctx->Yi.c[14] = 0;
  759. ctx->Yi.c[15] = 1;
  760. ctr = 1;
  761. } else {
  762. size_t i;
  763. u64 len0 = len;
  764. /* Borrow ctx->Xi to calculate initial Yi */
  765. ctx->Xi.u[0] = 0;
  766. ctx->Xi.u[1] = 0;
  767. while (len >= 16) {
  768. for (i = 0; i < 16; ++i)
  769. ctx->Xi.c[i] ^= iv[i];
  770. GCM_MUL(ctx);
  771. iv += 16;
  772. len -= 16;
  773. }
  774. if (len) {
  775. for (i = 0; i < len; ++i)
  776. ctx->Xi.c[i] ^= iv[i];
  777. GCM_MUL(ctx);
  778. }
  779. len0 <<= 3;
  780. if (IS_LITTLE_ENDIAN) {
  781. #ifdef BSWAP8
  782. ctx->Xi.u[1] ^= BSWAP8(len0);
  783. #else
  784. ctx->Xi.c[8] ^= (u8)(len0 >> 56);
  785. ctx->Xi.c[9] ^= (u8)(len0 >> 48);
  786. ctx->Xi.c[10] ^= (u8)(len0 >> 40);
  787. ctx->Xi.c[11] ^= (u8)(len0 >> 32);
  788. ctx->Xi.c[12] ^= (u8)(len0 >> 24);
  789. ctx->Xi.c[13] ^= (u8)(len0 >> 16);
  790. ctx->Xi.c[14] ^= (u8)(len0 >> 8);
  791. ctx->Xi.c[15] ^= (u8)(len0);
  792. #endif
  793. } else {
  794. ctx->Xi.u[1] ^= len0;
  795. }
  796. GCM_MUL(ctx);
  797. if (IS_LITTLE_ENDIAN)
  798. #ifdef BSWAP4
  799. ctr = BSWAP4(ctx->Xi.d[3]);
  800. #else
  801. ctr = GETU32(ctx->Xi.c + 12);
  802. #endif
  803. else
  804. ctr = ctx->Xi.d[3];
  805. /* Copy borrowed Xi to Yi */
  806. ctx->Yi.u[0] = ctx->Xi.u[0];
  807. ctx->Yi.u[1] = ctx->Xi.u[1];
  808. }
  809. ctx->Xi.u[0] = 0;
  810. ctx->Xi.u[1] = 0;
  811. (*ctx->block) (ctx->Yi.c, ctx->EK0.c, ctx->key);
  812. ++ctr;
  813. if (IS_LITTLE_ENDIAN)
  814. #ifdef BSWAP4
  815. ctx->Yi.d[3] = BSWAP4(ctr);
  816. #else
  817. PUTU32(ctx->Yi.c + 12, ctr);
  818. #endif
  819. else
  820. ctx->Yi.d[3] = ctr;
  821. }
  822. int CRYPTO_gcm128_aad(GCM128_CONTEXT *ctx, const unsigned char *aad,
  823. size_t len)
  824. {
  825. size_t i;
  826. unsigned int n;
  827. u64 alen = ctx->len.u[0];
  828. #ifdef GCM_FUNCREF_4BIT
  829. void (*gcm_gmult_p) (u64 Xi[2], const u128 Htable[16]) = ctx->gmult;
  830. # ifdef GHASH
  831. void (*gcm_ghash_p) (u64 Xi[2], const u128 Htable[16],
  832. const u8 *inp, size_t len) = ctx->ghash;
  833. # endif
  834. #endif
  835. if (ctx->len.u[1])
  836. return -2;
  837. alen += len;
  838. if (alen > (U64(1) << 61) || (sizeof(len) == 8 && alen < len))
  839. return -1;
  840. ctx->len.u[0] = alen;
  841. n = ctx->ares;
  842. if (n) {
  843. while (n && len) {
  844. ctx->Xi.c[n] ^= *(aad++);
  845. --len;
  846. n = (n + 1) % 16;
  847. }
  848. if (n == 0)
  849. GCM_MUL(ctx);
  850. else {
  851. ctx->ares = n;
  852. return 0;
  853. }
  854. }
  855. #ifdef GHASH
  856. if ((i = (len & (size_t)-16))) {
  857. GHASH(ctx, aad, i);
  858. aad += i;
  859. len -= i;
  860. }
  861. #else
  862. while (len >= 16) {
  863. for (i = 0; i < 16; ++i)
  864. ctx->Xi.c[i] ^= aad[i];
  865. GCM_MUL(ctx);
  866. aad += 16;
  867. len -= 16;
  868. }
  869. #endif
  870. if (len) {
  871. n = (unsigned int)len;
  872. for (i = 0; i < len; ++i)
  873. ctx->Xi.c[i] ^= aad[i];
  874. }
  875. ctx->ares = n;
  876. return 0;
  877. }
  878. int CRYPTO_gcm128_encrypt(GCM128_CONTEXT *ctx,
  879. const unsigned char *in, unsigned char *out,
  880. size_t len)
  881. {
  882. DECLARE_IS_ENDIAN;
  883. unsigned int n, ctr, mres;
  884. size_t i;
  885. u64 mlen = ctx->len.u[1];
  886. block128_f block = ctx->block;
  887. void *key = ctx->key;
  888. #ifdef GCM_FUNCREF_4BIT
  889. void (*gcm_gmult_p) (u64 Xi[2], const u128 Htable[16]) = ctx->gmult;
  890. # if defined(GHASH) && !defined(OPENSSL_SMALL_FOOTPRINT)
  891. void (*gcm_ghash_p) (u64 Xi[2], const u128 Htable[16],
  892. const u8 *inp, size_t len) = ctx->ghash;
  893. # endif
  894. #endif
  895. mlen += len;
  896. if (mlen > ((U64(1) << 36) - 32) || (sizeof(len) == 8 && mlen < len))
  897. return -1;
  898. ctx->len.u[1] = mlen;
  899. mres = ctx->mres;
  900. if (ctx->ares) {
  901. /* First call to encrypt finalizes GHASH(AAD) */
  902. #if defined(GHASH) && !defined(OPENSSL_SMALL_FOOTPRINT)
  903. if (len == 0) {
  904. GCM_MUL(ctx);
  905. ctx->ares = 0;
  906. return 0;
  907. }
  908. memcpy(ctx->Xn, ctx->Xi.c, sizeof(ctx->Xi));
  909. ctx->Xi.u[0] = 0;
  910. ctx->Xi.u[1] = 0;
  911. mres = sizeof(ctx->Xi);
  912. #else
  913. GCM_MUL(ctx);
  914. #endif
  915. ctx->ares = 0;
  916. }
  917. if (IS_LITTLE_ENDIAN)
  918. #ifdef BSWAP4
  919. ctr = BSWAP4(ctx->Yi.d[3]);
  920. #else
  921. ctr = GETU32(ctx->Yi.c + 12);
  922. #endif
  923. else
  924. ctr = ctx->Yi.d[3];
  925. n = mres % 16;
  926. #if !defined(OPENSSL_SMALL_FOOTPRINT)
  927. if (16 % sizeof(size_t) == 0) { /* always true actually */
  928. do {
  929. if (n) {
  930. # if defined(GHASH)
  931. while (n && len) {
  932. ctx->Xn[mres++] = *(out++) = *(in++) ^ ctx->EKi.c[n];
  933. --len;
  934. n = (n + 1) % 16;
  935. }
  936. if (n == 0) {
  937. GHASH(ctx, ctx->Xn, mres);
  938. mres = 0;
  939. } else {
  940. ctx->mres = mres;
  941. return 0;
  942. }
  943. # else
  944. while (n && len) {
  945. ctx->Xi.c[n] ^= *(out++) = *(in++) ^ ctx->EKi.c[n];
  946. --len;
  947. n = (n + 1) % 16;
  948. }
  949. if (n == 0) {
  950. GCM_MUL(ctx);
  951. mres = 0;
  952. } else {
  953. ctx->mres = n;
  954. return 0;
  955. }
  956. # endif
  957. }
  958. # if defined(STRICT_ALIGNMENT)
  959. if (((size_t)in | (size_t)out) % sizeof(size_t) != 0)
  960. break;
  961. # endif
  962. # if defined(GHASH)
  963. if (len >= 16 && mres) {
  964. GHASH(ctx, ctx->Xn, mres);
  965. mres = 0;
  966. }
  967. # if defined(GHASH_CHUNK)
  968. while (len >= GHASH_CHUNK) {
  969. size_t j = GHASH_CHUNK;
  970. while (j) {
  971. size_t_aX *out_t = (size_t_aX *)out;
  972. const size_t_aX *in_t = (const size_t_aX *)in;
  973. (*block) (ctx->Yi.c, ctx->EKi.c, key);
  974. ++ctr;
  975. if (IS_LITTLE_ENDIAN)
  976. # ifdef BSWAP4
  977. ctx->Yi.d[3] = BSWAP4(ctr);
  978. # else
  979. PUTU32(ctx->Yi.c + 12, ctr);
  980. # endif
  981. else
  982. ctx->Yi.d[3] = ctr;
  983. for (i = 0; i < 16 / sizeof(size_t); ++i)
  984. out_t[i] = in_t[i] ^ ctx->EKi.t[i];
  985. out += 16;
  986. in += 16;
  987. j -= 16;
  988. }
  989. GHASH(ctx, out - GHASH_CHUNK, GHASH_CHUNK);
  990. len -= GHASH_CHUNK;
  991. }
  992. # endif
  993. if ((i = (len & (size_t)-16))) {
  994. size_t j = i;
  995. while (len >= 16) {
  996. size_t_aX *out_t = (size_t_aX *)out;
  997. const size_t_aX *in_t = (const size_t_aX *)in;
  998. (*block) (ctx->Yi.c, ctx->EKi.c, key);
  999. ++ctr;
  1000. if (IS_LITTLE_ENDIAN)
  1001. # ifdef BSWAP4
  1002. ctx->Yi.d[3] = BSWAP4(ctr);
  1003. # else
  1004. PUTU32(ctx->Yi.c + 12, ctr);
  1005. # endif
  1006. else
  1007. ctx->Yi.d[3] = ctr;
  1008. for (i = 0; i < 16 / sizeof(size_t); ++i)
  1009. out_t[i] = in_t[i] ^ ctx->EKi.t[i];
  1010. out += 16;
  1011. in += 16;
  1012. len -= 16;
  1013. }
  1014. GHASH(ctx, out - j, j);
  1015. }
  1016. # else
  1017. while (len >= 16) {
  1018. size_t *out_t = (size_t *)out;
  1019. const size_t *in_t = (const size_t *)in;
  1020. (*block) (ctx->Yi.c, ctx->EKi.c, key);
  1021. ++ctr;
  1022. if (IS_LITTLE_ENDIAN)
  1023. # ifdef BSWAP4
  1024. ctx->Yi.d[3] = BSWAP4(ctr);
  1025. # else
  1026. PUTU32(ctx->Yi.c + 12, ctr);
  1027. # endif
  1028. else
  1029. ctx->Yi.d[3] = ctr;
  1030. for (i = 0; i < 16 / sizeof(size_t); ++i)
  1031. ctx->Xi.t[i] ^= out_t[i] = in_t[i] ^ ctx->EKi.t[i];
  1032. GCM_MUL(ctx);
  1033. out += 16;
  1034. in += 16;
  1035. len -= 16;
  1036. }
  1037. # endif
  1038. if (len) {
  1039. (*block) (ctx->Yi.c, ctx->EKi.c, key);
  1040. ++ctr;
  1041. if (IS_LITTLE_ENDIAN)
  1042. # ifdef BSWAP4
  1043. ctx->Yi.d[3] = BSWAP4(ctr);
  1044. # else
  1045. PUTU32(ctx->Yi.c + 12, ctr);
  1046. # endif
  1047. else
  1048. ctx->Yi.d[3] = ctr;
  1049. # if defined(GHASH)
  1050. while (len--) {
  1051. ctx->Xn[mres++] = out[n] = in[n] ^ ctx->EKi.c[n];
  1052. ++n;
  1053. }
  1054. # else
  1055. while (len--) {
  1056. ctx->Xi.c[n] ^= out[n] = in[n] ^ ctx->EKi.c[n];
  1057. ++n;
  1058. }
  1059. mres = n;
  1060. # endif
  1061. }
  1062. ctx->mres = mres;
  1063. return 0;
  1064. } while (0);
  1065. }
  1066. #endif
  1067. for (i = 0; i < len; ++i) {
  1068. if (n == 0) {
  1069. (*block) (ctx->Yi.c, ctx->EKi.c, key);
  1070. ++ctr;
  1071. if (IS_LITTLE_ENDIAN)
  1072. #ifdef BSWAP4
  1073. ctx->Yi.d[3] = BSWAP4(ctr);
  1074. #else
  1075. PUTU32(ctx->Yi.c + 12, ctr);
  1076. #endif
  1077. else
  1078. ctx->Yi.d[3] = ctr;
  1079. }
  1080. #if defined(GHASH) && !defined(OPENSSL_SMALL_FOOTPRINT)
  1081. ctx->Xn[mres++] = out[i] = in[i] ^ ctx->EKi.c[n];
  1082. n = (n + 1) % 16;
  1083. if (mres == sizeof(ctx->Xn)) {
  1084. GHASH(ctx,ctx->Xn,sizeof(ctx->Xn));
  1085. mres = 0;
  1086. }
  1087. #else
  1088. ctx->Xi.c[n] ^= out[i] = in[i] ^ ctx->EKi.c[n];
  1089. mres = n = (n + 1) % 16;
  1090. if (n == 0)
  1091. GCM_MUL(ctx);
  1092. #endif
  1093. }
  1094. ctx->mres = mres;
  1095. return 0;
  1096. }
  1097. int CRYPTO_gcm128_decrypt(GCM128_CONTEXT *ctx,
  1098. const unsigned char *in, unsigned char *out,
  1099. size_t len)
  1100. {
  1101. DECLARE_IS_ENDIAN;
  1102. unsigned int n, ctr, mres;
  1103. size_t i;
  1104. u64 mlen = ctx->len.u[1];
  1105. block128_f block = ctx->block;
  1106. void *key = ctx->key;
  1107. #ifdef GCM_FUNCREF_4BIT
  1108. void (*gcm_gmult_p) (u64 Xi[2], const u128 Htable[16]) = ctx->gmult;
  1109. # if defined(GHASH) && !defined(OPENSSL_SMALL_FOOTPRINT)
  1110. void (*gcm_ghash_p) (u64 Xi[2], const u128 Htable[16],
  1111. const u8 *inp, size_t len) = ctx->ghash;
  1112. # endif
  1113. #endif
  1114. mlen += len;
  1115. if (mlen > ((U64(1) << 36) - 32) || (sizeof(len) == 8 && mlen < len))
  1116. return -1;
  1117. ctx->len.u[1] = mlen;
  1118. mres = ctx->mres;
  1119. if (ctx->ares) {
  1120. /* First call to decrypt finalizes GHASH(AAD) */
  1121. #if defined(GHASH) && !defined(OPENSSL_SMALL_FOOTPRINT)
  1122. if (len == 0) {
  1123. GCM_MUL(ctx);
  1124. ctx->ares = 0;
  1125. return 0;
  1126. }
  1127. memcpy(ctx->Xn, ctx->Xi.c, sizeof(ctx->Xi));
  1128. ctx->Xi.u[0] = 0;
  1129. ctx->Xi.u[1] = 0;
  1130. mres = sizeof(ctx->Xi);
  1131. #else
  1132. GCM_MUL(ctx);
  1133. #endif
  1134. ctx->ares = 0;
  1135. }
  1136. if (IS_LITTLE_ENDIAN)
  1137. #ifdef BSWAP4
  1138. ctr = BSWAP4(ctx->Yi.d[3]);
  1139. #else
  1140. ctr = GETU32(ctx->Yi.c + 12);
  1141. #endif
  1142. else
  1143. ctr = ctx->Yi.d[3];
  1144. n = mres % 16;
  1145. #if !defined(OPENSSL_SMALL_FOOTPRINT)
  1146. if (16 % sizeof(size_t) == 0) { /* always true actually */
  1147. do {
  1148. if (n) {
  1149. # if defined(GHASH)
  1150. while (n && len) {
  1151. *(out++) = (ctx->Xn[mres++] = *(in++)) ^ ctx->EKi.c[n];
  1152. --len;
  1153. n = (n + 1) % 16;
  1154. }
  1155. if (n == 0) {
  1156. GHASH(ctx, ctx->Xn, mres);
  1157. mres = 0;
  1158. } else {
  1159. ctx->mres = mres;
  1160. return 0;
  1161. }
  1162. # else
  1163. while (n && len) {
  1164. u8 c = *(in++);
  1165. *(out++) = c ^ ctx->EKi.c[n];
  1166. ctx->Xi.c[n] ^= c;
  1167. --len;
  1168. n = (n + 1) % 16;
  1169. }
  1170. if (n == 0) {
  1171. GCM_MUL(ctx);
  1172. mres = 0;
  1173. } else {
  1174. ctx->mres = n;
  1175. return 0;
  1176. }
  1177. # endif
  1178. }
  1179. # if defined(STRICT_ALIGNMENT)
  1180. if (((size_t)in | (size_t)out) % sizeof(size_t) != 0)
  1181. break;
  1182. # endif
  1183. # if defined(GHASH)
  1184. if (len >= 16 && mres) {
  1185. GHASH(ctx, ctx->Xn, mres);
  1186. mres = 0;
  1187. }
  1188. # if defined(GHASH_CHUNK)
  1189. while (len >= GHASH_CHUNK) {
  1190. size_t j = GHASH_CHUNK;
  1191. GHASH(ctx, in, GHASH_CHUNK);
  1192. while (j) {
  1193. size_t_aX *out_t = (size_t_aX *)out;
  1194. const size_t_aX *in_t = (const size_t_aX *)in;
  1195. (*block) (ctx->Yi.c, ctx->EKi.c, key);
  1196. ++ctr;
  1197. if (IS_LITTLE_ENDIAN)
  1198. # ifdef BSWAP4
  1199. ctx->Yi.d[3] = BSWAP4(ctr);
  1200. # else
  1201. PUTU32(ctx->Yi.c + 12, ctr);
  1202. # endif
  1203. else
  1204. ctx->Yi.d[3] = ctr;
  1205. for (i = 0; i < 16 / sizeof(size_t); ++i)
  1206. out_t[i] = in_t[i] ^ ctx->EKi.t[i];
  1207. out += 16;
  1208. in += 16;
  1209. j -= 16;
  1210. }
  1211. len -= GHASH_CHUNK;
  1212. }
  1213. # endif
  1214. if ((i = (len & (size_t)-16))) {
  1215. GHASH(ctx, in, i);
  1216. while (len >= 16) {
  1217. size_t_aX *out_t = (size_t_aX *)out;
  1218. const size_t_aX *in_t = (const size_t_aX *)in;
  1219. (*block) (ctx->Yi.c, ctx->EKi.c, key);
  1220. ++ctr;
  1221. if (IS_LITTLE_ENDIAN)
  1222. # ifdef BSWAP4
  1223. ctx->Yi.d[3] = BSWAP4(ctr);
  1224. # else
  1225. PUTU32(ctx->Yi.c + 12, ctr);
  1226. # endif
  1227. else
  1228. ctx->Yi.d[3] = ctr;
  1229. for (i = 0; i < 16 / sizeof(size_t); ++i)
  1230. out_t[i] = in_t[i] ^ ctx->EKi.t[i];
  1231. out += 16;
  1232. in += 16;
  1233. len -= 16;
  1234. }
  1235. }
  1236. # else
  1237. while (len >= 16) {
  1238. size_t *out_t = (size_t *)out;
  1239. const size_t *in_t = (const size_t *)in;
  1240. (*block) (ctx->Yi.c, ctx->EKi.c, key);
  1241. ++ctr;
  1242. if (IS_LITTLE_ENDIAN)
  1243. # ifdef BSWAP4
  1244. ctx->Yi.d[3] = BSWAP4(ctr);
  1245. # else
  1246. PUTU32(ctx->Yi.c + 12, ctr);
  1247. # endif
  1248. else
  1249. ctx->Yi.d[3] = ctr;
  1250. for (i = 0; i < 16 / sizeof(size_t); ++i) {
  1251. size_t c = in_t[i];
  1252. out_t[i] = c ^ ctx->EKi.t[i];
  1253. ctx->Xi.t[i] ^= c;
  1254. }
  1255. GCM_MUL(ctx);
  1256. out += 16;
  1257. in += 16;
  1258. len -= 16;
  1259. }
  1260. # endif
  1261. if (len) {
  1262. (*block) (ctx->Yi.c, ctx->EKi.c, key);
  1263. ++ctr;
  1264. if (IS_LITTLE_ENDIAN)
  1265. # ifdef BSWAP4
  1266. ctx->Yi.d[3] = BSWAP4(ctr);
  1267. # else
  1268. PUTU32(ctx->Yi.c + 12, ctr);
  1269. # endif
  1270. else
  1271. ctx->Yi.d[3] = ctr;
  1272. # if defined(GHASH)
  1273. while (len--) {
  1274. out[n] = (ctx->Xn[mres++] = in[n]) ^ ctx->EKi.c[n];
  1275. ++n;
  1276. }
  1277. # else
  1278. while (len--) {
  1279. u8 c = in[n];
  1280. ctx->Xi.c[n] ^= c;
  1281. out[n] = c ^ ctx->EKi.c[n];
  1282. ++n;
  1283. }
  1284. mres = n;
  1285. # endif
  1286. }
  1287. ctx->mres = mres;
  1288. return 0;
  1289. } while (0);
  1290. }
  1291. #endif
  1292. for (i = 0; i < len; ++i) {
  1293. u8 c;
  1294. if (n == 0) {
  1295. (*block) (ctx->Yi.c, ctx->EKi.c, key);
  1296. ++ctr;
  1297. if (IS_LITTLE_ENDIAN)
  1298. #ifdef BSWAP4
  1299. ctx->Yi.d[3] = BSWAP4(ctr);
  1300. #else
  1301. PUTU32(ctx->Yi.c + 12, ctr);
  1302. #endif
  1303. else
  1304. ctx->Yi.d[3] = ctr;
  1305. }
  1306. #if defined(GHASH) && !defined(OPENSSL_SMALL_FOOTPRINT)
  1307. out[i] = (ctx->Xn[mres++] = c = in[i]) ^ ctx->EKi.c[n];
  1308. n = (n + 1) % 16;
  1309. if (mres == sizeof(ctx->Xn)) {
  1310. GHASH(ctx,ctx->Xn,sizeof(ctx->Xn));
  1311. mres = 0;
  1312. }
  1313. #else
  1314. c = in[i];
  1315. out[i] = c ^ ctx->EKi.c[n];
  1316. ctx->Xi.c[n] ^= c;
  1317. mres = n = (n + 1) % 16;
  1318. if (n == 0)
  1319. GCM_MUL(ctx);
  1320. #endif
  1321. }
  1322. ctx->mres = mres;
  1323. return 0;
  1324. }
  1325. int CRYPTO_gcm128_encrypt_ctr32(GCM128_CONTEXT *ctx,
  1326. const unsigned char *in, unsigned char *out,
  1327. size_t len, ctr128_f stream)
  1328. {
  1329. #if defined(OPENSSL_SMALL_FOOTPRINT)
  1330. return CRYPTO_gcm128_encrypt(ctx, in, out, len);
  1331. #else
  1332. DECLARE_IS_ENDIAN;
  1333. unsigned int n, ctr, mres;
  1334. size_t i;
  1335. u64 mlen = ctx->len.u[1];
  1336. void *key = ctx->key;
  1337. # ifdef GCM_FUNCREF_4BIT
  1338. void (*gcm_gmult_p) (u64 Xi[2], const u128 Htable[16]) = ctx->gmult;
  1339. # ifdef GHASH
  1340. void (*gcm_ghash_p) (u64 Xi[2], const u128 Htable[16],
  1341. const u8 *inp, size_t len) = ctx->ghash;
  1342. # endif
  1343. # endif
  1344. mlen += len;
  1345. if (mlen > ((U64(1) << 36) - 32) || (sizeof(len) == 8 && mlen < len))
  1346. return -1;
  1347. ctx->len.u[1] = mlen;
  1348. mres = ctx->mres;
  1349. if (ctx->ares) {
  1350. /* First call to encrypt finalizes GHASH(AAD) */
  1351. #if defined(GHASH)
  1352. if (len == 0) {
  1353. GCM_MUL(ctx);
  1354. ctx->ares = 0;
  1355. return 0;
  1356. }
  1357. memcpy(ctx->Xn, ctx->Xi.c, sizeof(ctx->Xi));
  1358. ctx->Xi.u[0] = 0;
  1359. ctx->Xi.u[1] = 0;
  1360. mres = sizeof(ctx->Xi);
  1361. #else
  1362. GCM_MUL(ctx);
  1363. #endif
  1364. ctx->ares = 0;
  1365. }
  1366. if (IS_LITTLE_ENDIAN)
  1367. # ifdef BSWAP4
  1368. ctr = BSWAP4(ctx->Yi.d[3]);
  1369. # else
  1370. ctr = GETU32(ctx->Yi.c + 12);
  1371. # endif
  1372. else
  1373. ctr = ctx->Yi.d[3];
  1374. n = mres % 16;
  1375. if (n) {
  1376. # if defined(GHASH)
  1377. while (n && len) {
  1378. ctx->Xn[mres++] = *(out++) = *(in++) ^ ctx->EKi.c[n];
  1379. --len;
  1380. n = (n + 1) % 16;
  1381. }
  1382. if (n == 0) {
  1383. GHASH(ctx, ctx->Xn, mres);
  1384. mres = 0;
  1385. } else {
  1386. ctx->mres = mres;
  1387. return 0;
  1388. }
  1389. # else
  1390. while (n && len) {
  1391. ctx->Xi.c[n] ^= *(out++) = *(in++) ^ ctx->EKi.c[n];
  1392. --len;
  1393. n = (n + 1) % 16;
  1394. }
  1395. if (n == 0) {
  1396. GCM_MUL(ctx);
  1397. mres = 0;
  1398. } else {
  1399. ctx->mres = n;
  1400. return 0;
  1401. }
  1402. # endif
  1403. }
  1404. # if defined(GHASH)
  1405. if (len >= 16 && mres) {
  1406. GHASH(ctx, ctx->Xn, mres);
  1407. mres = 0;
  1408. }
  1409. # if defined(GHASH_CHUNK)
  1410. while (len >= GHASH_CHUNK) {
  1411. (*stream) (in, out, GHASH_CHUNK / 16, key, ctx->Yi.c);
  1412. ctr += GHASH_CHUNK / 16;
  1413. if (IS_LITTLE_ENDIAN)
  1414. # ifdef BSWAP4
  1415. ctx->Yi.d[3] = BSWAP4(ctr);
  1416. # else
  1417. PUTU32(ctx->Yi.c + 12, ctr);
  1418. # endif
  1419. else
  1420. ctx->Yi.d[3] = ctr;
  1421. GHASH(ctx, out, GHASH_CHUNK);
  1422. out += GHASH_CHUNK;
  1423. in += GHASH_CHUNK;
  1424. len -= GHASH_CHUNK;
  1425. }
  1426. # endif
  1427. # endif
  1428. if ((i = (len & (size_t)-16))) {
  1429. size_t j = i / 16;
  1430. (*stream) (in, out, j, key, ctx->Yi.c);
  1431. ctr += (unsigned int)j;
  1432. if (IS_LITTLE_ENDIAN)
  1433. # ifdef BSWAP4
  1434. ctx->Yi.d[3] = BSWAP4(ctr);
  1435. # else
  1436. PUTU32(ctx->Yi.c + 12, ctr);
  1437. # endif
  1438. else
  1439. ctx->Yi.d[3] = ctr;
  1440. in += i;
  1441. len -= i;
  1442. # if defined(GHASH)
  1443. GHASH(ctx, out, i);
  1444. out += i;
  1445. # else
  1446. while (j--) {
  1447. for (i = 0; i < 16; ++i)
  1448. ctx->Xi.c[i] ^= out[i];
  1449. GCM_MUL(ctx);
  1450. out += 16;
  1451. }
  1452. # endif
  1453. }
  1454. if (len) {
  1455. (*ctx->block) (ctx->Yi.c, ctx->EKi.c, key);
  1456. ++ctr;
  1457. if (IS_LITTLE_ENDIAN)
  1458. # ifdef BSWAP4
  1459. ctx->Yi.d[3] = BSWAP4(ctr);
  1460. # else
  1461. PUTU32(ctx->Yi.c + 12, ctr);
  1462. # endif
  1463. else
  1464. ctx->Yi.d[3] = ctr;
  1465. while (len--) {
  1466. # if defined(GHASH)
  1467. ctx->Xn[mres++] = out[n] = in[n] ^ ctx->EKi.c[n];
  1468. # else
  1469. ctx->Xi.c[mres++] ^= out[n] = in[n] ^ ctx->EKi.c[n];
  1470. # endif
  1471. ++n;
  1472. }
  1473. }
  1474. ctx->mres = mres;
  1475. return 0;
  1476. #endif
  1477. }
  1478. int CRYPTO_gcm128_decrypt_ctr32(GCM128_CONTEXT *ctx,
  1479. const unsigned char *in, unsigned char *out,
  1480. size_t len, ctr128_f stream)
  1481. {
  1482. #if defined(OPENSSL_SMALL_FOOTPRINT)
  1483. return CRYPTO_gcm128_decrypt(ctx, in, out, len);
  1484. #else
  1485. DECLARE_IS_ENDIAN;
  1486. unsigned int n, ctr, mres;
  1487. size_t i;
  1488. u64 mlen = ctx->len.u[1];
  1489. void *key = ctx->key;
  1490. # ifdef GCM_FUNCREF_4BIT
  1491. void (*gcm_gmult_p) (u64 Xi[2], const u128 Htable[16]) = ctx->gmult;
  1492. # ifdef GHASH
  1493. void (*gcm_ghash_p) (u64 Xi[2], const u128 Htable[16],
  1494. const u8 *inp, size_t len) = ctx->ghash;
  1495. # endif
  1496. # endif
  1497. mlen += len;
  1498. if (mlen > ((U64(1) << 36) - 32) || (sizeof(len) == 8 && mlen < len))
  1499. return -1;
  1500. ctx->len.u[1] = mlen;
  1501. mres = ctx->mres;
  1502. if (ctx->ares) {
  1503. /* First call to decrypt finalizes GHASH(AAD) */
  1504. # if defined(GHASH)
  1505. if (len == 0) {
  1506. GCM_MUL(ctx);
  1507. ctx->ares = 0;
  1508. return 0;
  1509. }
  1510. memcpy(ctx->Xn, ctx->Xi.c, sizeof(ctx->Xi));
  1511. ctx->Xi.u[0] = 0;
  1512. ctx->Xi.u[1] = 0;
  1513. mres = sizeof(ctx->Xi);
  1514. # else
  1515. GCM_MUL(ctx);
  1516. # endif
  1517. ctx->ares = 0;
  1518. }
  1519. if (IS_LITTLE_ENDIAN)
  1520. # ifdef BSWAP4
  1521. ctr = BSWAP4(ctx->Yi.d[3]);
  1522. # else
  1523. ctr = GETU32(ctx->Yi.c + 12);
  1524. # endif
  1525. else
  1526. ctr = ctx->Yi.d[3];
  1527. n = mres % 16;
  1528. if (n) {
  1529. # if defined(GHASH)
  1530. while (n && len) {
  1531. *(out++) = (ctx->Xn[mres++] = *(in++)) ^ ctx->EKi.c[n];
  1532. --len;
  1533. n = (n + 1) % 16;
  1534. }
  1535. if (n == 0) {
  1536. GHASH(ctx, ctx->Xn, mres);
  1537. mres = 0;
  1538. } else {
  1539. ctx->mres = mres;
  1540. return 0;
  1541. }
  1542. # else
  1543. while (n && len) {
  1544. u8 c = *(in++);
  1545. *(out++) = c ^ ctx->EKi.c[n];
  1546. ctx->Xi.c[n] ^= c;
  1547. --len;
  1548. n = (n + 1) % 16;
  1549. }
  1550. if (n == 0) {
  1551. GCM_MUL(ctx);
  1552. mres = 0;
  1553. } else {
  1554. ctx->mres = n;
  1555. return 0;
  1556. }
  1557. # endif
  1558. }
  1559. # if defined(GHASH)
  1560. if (len >= 16 && mres) {
  1561. GHASH(ctx, ctx->Xn, mres);
  1562. mres = 0;
  1563. }
  1564. # if defined(GHASH_CHUNK)
  1565. while (len >= GHASH_CHUNK) {
  1566. GHASH(ctx, in, GHASH_CHUNK);
  1567. (*stream) (in, out, GHASH_CHUNK / 16, key, ctx->Yi.c);
  1568. ctr += GHASH_CHUNK / 16;
  1569. if (IS_LITTLE_ENDIAN)
  1570. # ifdef BSWAP4
  1571. ctx->Yi.d[3] = BSWAP4(ctr);
  1572. # else
  1573. PUTU32(ctx->Yi.c + 12, ctr);
  1574. # endif
  1575. else
  1576. ctx->Yi.d[3] = ctr;
  1577. out += GHASH_CHUNK;
  1578. in += GHASH_CHUNK;
  1579. len -= GHASH_CHUNK;
  1580. }
  1581. # endif
  1582. # endif
  1583. if ((i = (len & (size_t)-16))) {
  1584. size_t j = i / 16;
  1585. # if defined(GHASH)
  1586. GHASH(ctx, in, i);
  1587. # else
  1588. while (j--) {
  1589. size_t k;
  1590. for (k = 0; k < 16; ++k)
  1591. ctx->Xi.c[k] ^= in[k];
  1592. GCM_MUL(ctx);
  1593. in += 16;
  1594. }
  1595. j = i / 16;
  1596. in -= i;
  1597. # endif
  1598. (*stream) (in, out, j, key, ctx->Yi.c);
  1599. ctr += (unsigned int)j;
  1600. if (IS_LITTLE_ENDIAN)
  1601. # ifdef BSWAP4
  1602. ctx->Yi.d[3] = BSWAP4(ctr);
  1603. # else
  1604. PUTU32(ctx->Yi.c + 12, ctr);
  1605. # endif
  1606. else
  1607. ctx->Yi.d[3] = ctr;
  1608. out += i;
  1609. in += i;
  1610. len -= i;
  1611. }
  1612. if (len) {
  1613. (*ctx->block) (ctx->Yi.c, ctx->EKi.c, key);
  1614. ++ctr;
  1615. if (IS_LITTLE_ENDIAN)
  1616. # ifdef BSWAP4
  1617. ctx->Yi.d[3] = BSWAP4(ctr);
  1618. # else
  1619. PUTU32(ctx->Yi.c + 12, ctr);
  1620. # endif
  1621. else
  1622. ctx->Yi.d[3] = ctr;
  1623. while (len--) {
  1624. # if defined(GHASH)
  1625. out[n] = (ctx->Xn[mres++] = in[n]) ^ ctx->EKi.c[n];
  1626. # else
  1627. u8 c = in[n];
  1628. ctx->Xi.c[mres++] ^= c;
  1629. out[n] = c ^ ctx->EKi.c[n];
  1630. # endif
  1631. ++n;
  1632. }
  1633. }
  1634. ctx->mres = mres;
  1635. return 0;
  1636. #endif
  1637. }
  1638. int CRYPTO_gcm128_finish(GCM128_CONTEXT *ctx, const unsigned char *tag,
  1639. size_t len)
  1640. {
  1641. DECLARE_IS_ENDIAN;
  1642. u64 alen = ctx->len.u[0] << 3;
  1643. u64 clen = ctx->len.u[1] << 3;
  1644. #ifdef GCM_FUNCREF_4BIT
  1645. void (*gcm_gmult_p) (u64 Xi[2], const u128 Htable[16]) = ctx->gmult;
  1646. # if defined(GHASH) && !defined(OPENSSL_SMALL_FOOTPRINT)
  1647. void (*gcm_ghash_p) (u64 Xi[2], const u128 Htable[16],
  1648. const u8 *inp, size_t len) = ctx->ghash;
  1649. # endif
  1650. #endif
  1651. #if defined(GHASH) && !defined(OPENSSL_SMALL_FOOTPRINT)
  1652. u128 bitlen;
  1653. unsigned int mres = ctx->mres;
  1654. if (mres) {
  1655. unsigned blocks = (mres + 15) & -16;
  1656. memset(ctx->Xn + mres, 0, blocks - mres);
  1657. mres = blocks;
  1658. if (mres == sizeof(ctx->Xn)) {
  1659. GHASH(ctx, ctx->Xn, mres);
  1660. mres = 0;
  1661. }
  1662. } else if (ctx->ares) {
  1663. GCM_MUL(ctx);
  1664. }
  1665. #else
  1666. if (ctx->mres || ctx->ares)
  1667. GCM_MUL(ctx);
  1668. #endif
  1669. if (IS_LITTLE_ENDIAN) {
  1670. #ifdef BSWAP8
  1671. alen = BSWAP8(alen);
  1672. clen = BSWAP8(clen);
  1673. #else
  1674. u8 *p = ctx->len.c;
  1675. ctx->len.u[0] = alen;
  1676. ctx->len.u[1] = clen;
  1677. alen = (u64)GETU32(p) << 32 | GETU32(p + 4);
  1678. clen = (u64)GETU32(p + 8) << 32 | GETU32(p + 12);
  1679. #endif
  1680. }
  1681. #if defined(GHASH) && !defined(OPENSSL_SMALL_FOOTPRINT)
  1682. bitlen.hi = alen;
  1683. bitlen.lo = clen;
  1684. memcpy(ctx->Xn + mres, &bitlen, sizeof(bitlen));
  1685. mres += sizeof(bitlen);
  1686. GHASH(ctx, ctx->Xn, mres);
  1687. #else
  1688. ctx->Xi.u[0] ^= alen;
  1689. ctx->Xi.u[1] ^= clen;
  1690. GCM_MUL(ctx);
  1691. #endif
  1692. ctx->Xi.u[0] ^= ctx->EK0.u[0];
  1693. ctx->Xi.u[1] ^= ctx->EK0.u[1];
  1694. if (tag && len <= sizeof(ctx->Xi))
  1695. return CRYPTO_memcmp(ctx->Xi.c, tag, len);
  1696. else
  1697. return -1;
  1698. }
  1699. void CRYPTO_gcm128_tag(GCM128_CONTEXT *ctx, unsigned char *tag, size_t len)
  1700. {
  1701. CRYPTO_gcm128_finish(ctx, NULL, 0);
  1702. memcpy(tag, ctx->Xi.c,
  1703. len <= sizeof(ctx->Xi.c) ? len : sizeof(ctx->Xi.c));
  1704. }
  1705. GCM128_CONTEXT *CRYPTO_gcm128_new(void *key, block128_f block)
  1706. {
  1707. GCM128_CONTEXT *ret;
  1708. if ((ret = OPENSSL_malloc(sizeof(*ret))) != NULL)
  1709. CRYPTO_gcm128_init(ret, key, block);
  1710. return ret;
  1711. }
  1712. void CRYPTO_gcm128_release(GCM128_CONTEXT *ctx)
  1713. {
  1714. OPENSSL_clear_free(ctx, sizeof(*ctx));
  1715. }