gcm128.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607
  1. /*
  2. * Copyright 2010-2023 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. /* RISC-V uses C implementation as a fallback. */
  27. #if defined(__riscv)
  28. # define INCLUDE_C_GMULT_4BIT
  29. # define INCLUDE_C_GHASH_4BIT
  30. #endif
  31. #define PACK(s) ((size_t)(s)<<(sizeof(size_t)*8-16))
  32. #define REDUCE1BIT(V) do { \
  33. if (sizeof(size_t)==8) { \
  34. u64 T = U64(0xe100000000000000) & (0-(V.lo&1)); \
  35. V.lo = (V.hi<<63)|(V.lo>>1); \
  36. V.hi = (V.hi>>1 )^T; \
  37. } \
  38. else { \
  39. u32 T = 0xe1000000U & (0-(u32)(V.lo&1)); \
  40. V.lo = (V.hi<<63)|(V.lo>>1); \
  41. V.hi = (V.hi>>1 )^((u64)T<<32); \
  42. } \
  43. } while(0)
  44. /*-
  45. *
  46. * NOTE: TABLE_BITS and all non-4bit implementations have been removed in 3.1.
  47. *
  48. * Even though permitted values for TABLE_BITS are 8, 4 and 1, it should
  49. * never be set to 8. 8 is effectively reserved for testing purposes.
  50. * TABLE_BITS>1 are lookup-table-driven implementations referred to as
  51. * "Shoup's" in GCM specification. In other words OpenSSL does not cover
  52. * whole spectrum of possible table driven implementations. Why? In
  53. * non-"Shoup's" case memory access pattern is segmented in such manner,
  54. * that it's trivial to see that cache timing information can reveal
  55. * fair portion of intermediate hash value. Given that ciphertext is
  56. * always available to attacker, it's possible for him to attempt to
  57. * deduce secret parameter H and if successful, tamper with messages
  58. * [which is nothing but trivial in CTR mode]. In "Shoup's" case it's
  59. * not as trivial, but there is no reason to believe that it's resistant
  60. * to cache-timing attack. And the thing about "8-bit" implementation is
  61. * that it consumes 16 (sixteen) times more memory, 4KB per individual
  62. * key + 1KB shared. Well, on pros side it should be twice as fast as
  63. * "4-bit" version. And for gcc-generated x86[_64] code, "8-bit" version
  64. * was observed to run ~75% faster, closer to 100% for commercial
  65. * compilers... Yet "4-bit" procedure is preferred, because it's
  66. * believed to provide better security-performance balance and adequate
  67. * all-round performance. "All-round" refers to things like:
  68. *
  69. * - shorter setup time effectively improves overall timing for
  70. * handling short messages;
  71. * - larger table allocation can become unbearable because of VM
  72. * subsystem penalties (for example on Windows large enough free
  73. * results in VM working set trimming, meaning that consequent
  74. * malloc would immediately incur working set expansion);
  75. * - larger table has larger cache footprint, which can affect
  76. * performance of other code paths (not necessarily even from same
  77. * thread in Hyper-Threading world);
  78. *
  79. * Value of 1 is not appropriate for performance reasons.
  80. */
  81. static void gcm_init_4bit(u128 Htable[16], const u64 H[2])
  82. {
  83. u128 V;
  84. # if defined(OPENSSL_SMALL_FOOTPRINT)
  85. int i;
  86. # endif
  87. Htable[0].hi = 0;
  88. Htable[0].lo = 0;
  89. V.hi = H[0];
  90. V.lo = H[1];
  91. # if defined(OPENSSL_SMALL_FOOTPRINT)
  92. for (Htable[8] = V, i = 4; i > 0; i >>= 1) {
  93. REDUCE1BIT(V);
  94. Htable[i] = V;
  95. }
  96. for (i = 2; i < 16; i <<= 1) {
  97. u128 *Hi = Htable + i;
  98. int j;
  99. for (V = *Hi, j = 1; j < i; ++j) {
  100. Hi[j].hi = V.hi ^ Htable[j].hi;
  101. Hi[j].lo = V.lo ^ Htable[j].lo;
  102. }
  103. }
  104. # else
  105. Htable[8] = V;
  106. REDUCE1BIT(V);
  107. Htable[4] = V;
  108. REDUCE1BIT(V);
  109. Htable[2] = V;
  110. REDUCE1BIT(V);
  111. Htable[1] = V;
  112. Htable[3].hi = V.hi ^ Htable[2].hi, Htable[3].lo = V.lo ^ Htable[2].lo;
  113. V = Htable[4];
  114. Htable[5].hi = V.hi ^ Htable[1].hi, Htable[5].lo = V.lo ^ Htable[1].lo;
  115. Htable[6].hi = V.hi ^ Htable[2].hi, Htable[6].lo = V.lo ^ Htable[2].lo;
  116. Htable[7].hi = V.hi ^ Htable[3].hi, Htable[7].lo = V.lo ^ Htable[3].lo;
  117. V = Htable[8];
  118. Htable[9].hi = V.hi ^ Htable[1].hi, Htable[9].lo = V.lo ^ Htable[1].lo;
  119. Htable[10].hi = V.hi ^ Htable[2].hi, Htable[10].lo = V.lo ^ Htable[2].lo;
  120. Htable[11].hi = V.hi ^ Htable[3].hi, Htable[11].lo = V.lo ^ Htable[3].lo;
  121. Htable[12].hi = V.hi ^ Htable[4].hi, Htable[12].lo = V.lo ^ Htable[4].lo;
  122. Htable[13].hi = V.hi ^ Htable[5].hi, Htable[13].lo = V.lo ^ Htable[5].lo;
  123. Htable[14].hi = V.hi ^ Htable[6].hi, Htable[14].lo = V.lo ^ Htable[6].lo;
  124. Htable[15].hi = V.hi ^ Htable[7].hi, Htable[15].lo = V.lo ^ Htable[7].lo;
  125. # endif
  126. # if defined(GHASH_ASM) && (defined(__arm__) || defined(__arm))
  127. /*
  128. * ARM assembler expects specific dword order in Htable.
  129. */
  130. {
  131. int j;
  132. DECLARE_IS_ENDIAN;
  133. if (IS_LITTLE_ENDIAN)
  134. for (j = 0; j < 16; ++j) {
  135. V = Htable[j];
  136. Htable[j].hi = V.lo;
  137. Htable[j].lo = V.hi;
  138. } else
  139. for (j = 0; j < 16; ++j) {
  140. V = Htable[j];
  141. Htable[j].hi = V.lo << 32 | V.lo >> 32;
  142. Htable[j].lo = V.hi << 32 | V.hi >> 32;
  143. }
  144. }
  145. # endif
  146. }
  147. # if !defined(GHASH_ASM) || defined(INCLUDE_C_GMULT_4BIT)
  148. static const size_t rem_4bit[16] = {
  149. PACK(0x0000), PACK(0x1C20), PACK(0x3840), PACK(0x2460),
  150. PACK(0x7080), PACK(0x6CA0), PACK(0x48C0), PACK(0x54E0),
  151. PACK(0xE100), PACK(0xFD20), PACK(0xD940), PACK(0xC560),
  152. PACK(0x9180), PACK(0x8DA0), PACK(0xA9C0), PACK(0xB5E0)
  153. };
  154. static void gcm_gmult_4bit(u64 Xi[2], const u128 Htable[16])
  155. {
  156. u128 Z;
  157. int cnt = 15;
  158. size_t rem, nlo, nhi;
  159. DECLARE_IS_ENDIAN;
  160. nlo = ((const u8 *)Xi)[15];
  161. nhi = nlo >> 4;
  162. nlo &= 0xf;
  163. Z.hi = Htable[nlo].hi;
  164. Z.lo = Htable[nlo].lo;
  165. while (1) {
  166. rem = (size_t)Z.lo & 0xf;
  167. Z.lo = (Z.hi << 60) | (Z.lo >> 4);
  168. Z.hi = (Z.hi >> 4);
  169. if (sizeof(size_t) == 8)
  170. Z.hi ^= rem_4bit[rem];
  171. else
  172. Z.hi ^= (u64)rem_4bit[rem] << 32;
  173. Z.hi ^= Htable[nhi].hi;
  174. Z.lo ^= Htable[nhi].lo;
  175. if (--cnt < 0)
  176. break;
  177. nlo = ((const u8 *)Xi)[cnt];
  178. nhi = nlo >> 4;
  179. nlo &= 0xf;
  180. rem = (size_t)Z.lo & 0xf;
  181. Z.lo = (Z.hi << 60) | (Z.lo >> 4);
  182. Z.hi = (Z.hi >> 4);
  183. if (sizeof(size_t) == 8)
  184. Z.hi ^= rem_4bit[rem];
  185. else
  186. Z.hi ^= (u64)rem_4bit[rem] << 32;
  187. Z.hi ^= Htable[nlo].hi;
  188. Z.lo ^= Htable[nlo].lo;
  189. }
  190. if (IS_LITTLE_ENDIAN) {
  191. # ifdef BSWAP8
  192. Xi[0] = BSWAP8(Z.hi);
  193. Xi[1] = BSWAP8(Z.lo);
  194. # else
  195. u8 *p = (u8 *)Xi;
  196. u32 v;
  197. v = (u32)(Z.hi >> 32);
  198. PUTU32(p, v);
  199. v = (u32)(Z.hi);
  200. PUTU32(p + 4, v);
  201. v = (u32)(Z.lo >> 32);
  202. PUTU32(p + 8, v);
  203. v = (u32)(Z.lo);
  204. PUTU32(p + 12, v);
  205. # endif
  206. } else {
  207. Xi[0] = Z.hi;
  208. Xi[1] = Z.lo;
  209. }
  210. }
  211. # endif
  212. # if !defined(GHASH_ASM) || defined(INCLUDE_C_GHASH_4BIT)
  213. # if !defined(OPENSSL_SMALL_FOOTPRINT)
  214. /*
  215. * Streamed gcm_mult_4bit, see CRYPTO_gcm128_[en|de]crypt for
  216. * details... Compiler-generated code doesn't seem to give any
  217. * performance improvement, at least not on x86[_64]. It's here
  218. * mostly as reference and a placeholder for possible future
  219. * non-trivial optimization[s]...
  220. */
  221. static void gcm_ghash_4bit(u64 Xi[2], const u128 Htable[16],
  222. const u8 *inp, size_t len)
  223. {
  224. u128 Z;
  225. int cnt;
  226. size_t rem, nlo, nhi;
  227. DECLARE_IS_ENDIAN;
  228. do {
  229. cnt = 15;
  230. nlo = ((const u8 *)Xi)[15];
  231. nlo ^= inp[15];
  232. nhi = nlo >> 4;
  233. nlo &= 0xf;
  234. Z.hi = Htable[nlo].hi;
  235. Z.lo = Htable[nlo].lo;
  236. while (1) {
  237. rem = (size_t)Z.lo & 0xf;
  238. Z.lo = (Z.hi << 60) | (Z.lo >> 4);
  239. Z.hi = (Z.hi >> 4);
  240. if (sizeof(size_t) == 8)
  241. Z.hi ^= rem_4bit[rem];
  242. else
  243. Z.hi ^= (u64)rem_4bit[rem] << 32;
  244. Z.hi ^= Htable[nhi].hi;
  245. Z.lo ^= Htable[nhi].lo;
  246. if (--cnt < 0)
  247. break;
  248. nlo = ((const u8 *)Xi)[cnt];
  249. nlo ^= inp[cnt];
  250. nhi = nlo >> 4;
  251. nlo &= 0xf;
  252. rem = (size_t)Z.lo & 0xf;
  253. Z.lo = (Z.hi << 60) | (Z.lo >> 4);
  254. Z.hi = (Z.hi >> 4);
  255. if (sizeof(size_t) == 8)
  256. Z.hi ^= rem_4bit[rem];
  257. else
  258. Z.hi ^= (u64)rem_4bit[rem] << 32;
  259. Z.hi ^= Htable[nlo].hi;
  260. Z.lo ^= Htable[nlo].lo;
  261. }
  262. if (IS_LITTLE_ENDIAN) {
  263. # ifdef BSWAP8
  264. Xi[0] = BSWAP8(Z.hi);
  265. Xi[1] = BSWAP8(Z.lo);
  266. # else
  267. u8 *p = (u8 *)Xi;
  268. u32 v;
  269. v = (u32)(Z.hi >> 32);
  270. PUTU32(p, v);
  271. v = (u32)(Z.hi);
  272. PUTU32(p + 4, v);
  273. v = (u32)(Z.lo >> 32);
  274. PUTU32(p + 8, v);
  275. v = (u32)(Z.lo);
  276. PUTU32(p + 12, v);
  277. # endif
  278. } else {
  279. Xi[0] = Z.hi;
  280. Xi[1] = Z.lo;
  281. }
  282. inp += 16;
  283. /* Block size is 128 bits so len is a multiple of 16 */
  284. len -= 16;
  285. } while (len > 0);
  286. }
  287. # endif
  288. # else
  289. void gcm_gmult_4bit(u64 Xi[2], const u128 Htable[16]);
  290. void gcm_ghash_4bit(u64 Xi[2], const u128 Htable[16], const u8 *inp,
  291. size_t len);
  292. # endif
  293. # define GCM_MUL(ctx) ctx->funcs.gmult(ctx->Xi.u,ctx->Htable)
  294. # if defined(GHASH_ASM) || !defined(OPENSSL_SMALL_FOOTPRINT)
  295. # define GHASH(ctx,in,len) ctx->funcs.ghash((ctx)->Xi.u,(ctx)->Htable,in,len)
  296. /*
  297. * GHASH_CHUNK is "stride parameter" missioned to mitigate cache trashing
  298. * effect. In other words idea is to hash data while it's still in L1 cache
  299. * after encryption pass...
  300. */
  301. # define GHASH_CHUNK (3*1024)
  302. # endif
  303. #if (defined(GHASH_ASM) || defined(OPENSSL_CPUID_OBJ))
  304. # if !defined(I386_ONLY) && \
  305. (defined(__i386) || defined(__i386__) || \
  306. defined(__x86_64) || defined(__x86_64__) || \
  307. defined(_M_IX86) || defined(_M_AMD64) || defined(_M_X64))
  308. # define GHASH_ASM_X86_OR_64
  309. void gcm_init_clmul(u128 Htable[16], const u64 Xi[2]);
  310. void gcm_gmult_clmul(u64 Xi[2], const u128 Htable[16]);
  311. void gcm_ghash_clmul(u64 Xi[2], const u128 Htable[16], const u8 *inp,
  312. size_t len);
  313. # if defined(__i386) || defined(__i386__) || defined(_M_IX86)
  314. # define gcm_init_avx gcm_init_clmul
  315. # define gcm_gmult_avx gcm_gmult_clmul
  316. # define gcm_ghash_avx gcm_ghash_clmul
  317. # else
  318. void gcm_init_avx(u128 Htable[16], const u64 Xi[2]);
  319. void gcm_gmult_avx(u64 Xi[2], const u128 Htable[16]);
  320. void gcm_ghash_avx(u64 Xi[2], const u128 Htable[16], const u8 *inp,
  321. size_t len);
  322. # endif
  323. # if defined(__i386) || defined(__i386__) || defined(_M_IX86)
  324. # define GHASH_ASM_X86
  325. void gcm_gmult_4bit_mmx(u64 Xi[2], const u128 Htable[16]);
  326. void gcm_ghash_4bit_mmx(u64 Xi[2], const u128 Htable[16], const u8 *inp,
  327. size_t len);
  328. void gcm_gmult_4bit_x86(u64 Xi[2], const u128 Htable[16]);
  329. void gcm_ghash_4bit_x86(u64 Xi[2], const u128 Htable[16], const u8 *inp,
  330. size_t len);
  331. # endif
  332. # elif defined(__arm__) || defined(__arm) || defined(__aarch64__) || defined(_M_ARM64)
  333. # include "arm_arch.h"
  334. # if __ARM_MAX_ARCH__>=7
  335. # define GHASH_ASM_ARM
  336. # define PMULL_CAPABLE (OPENSSL_armcap_P & ARMV8_PMULL)
  337. # if defined(__arm__) || defined(__arm)
  338. # define NEON_CAPABLE (OPENSSL_armcap_P & ARMV7_NEON)
  339. # endif
  340. void gcm_init_neon(u128 Htable[16], const u64 Xi[2]);
  341. void gcm_gmult_neon(u64 Xi[2], const u128 Htable[16]);
  342. void gcm_ghash_neon(u64 Xi[2], const u128 Htable[16], const u8 *inp,
  343. size_t len);
  344. void gcm_init_v8(u128 Htable[16], const u64 Xi[2]);
  345. void gcm_gmult_v8(u64 Xi[2], const u128 Htable[16]);
  346. void gcm_ghash_v8(u64 Xi[2], const u128 Htable[16], const u8 *inp,
  347. size_t len);
  348. # endif
  349. # elif defined(__sparc__) || defined(__sparc)
  350. # include "crypto/sparc_arch.h"
  351. # define GHASH_ASM_SPARC
  352. void gcm_init_vis3(u128 Htable[16], const u64 Xi[2]);
  353. void gcm_gmult_vis3(u64 Xi[2], const u128 Htable[16]);
  354. void gcm_ghash_vis3(u64 Xi[2], const u128 Htable[16], const u8 *inp,
  355. size_t len);
  356. # elif defined(OPENSSL_CPUID_OBJ) && (defined(__powerpc__) || defined(__ppc__) || defined(_ARCH_PPC))
  357. # include "crypto/ppc_arch.h"
  358. # define GHASH_ASM_PPC
  359. void gcm_init_p8(u128 Htable[16], const u64 Xi[2]);
  360. void gcm_gmult_p8(u64 Xi[2], const u128 Htable[16]);
  361. void gcm_ghash_p8(u64 Xi[2], const u128 Htable[16], const u8 *inp,
  362. size_t len);
  363. # elif defined(OPENSSL_CPUID_OBJ) && defined(__riscv) && __riscv_xlen == 64
  364. # include "crypto/riscv_arch.h"
  365. # define GHASH_ASM_RV64I
  366. /* Zbc/Zbkc (scalar crypto with clmul) based routines. */
  367. void gcm_init_rv64i_zbc(u128 Htable[16], const u64 Xi[2]);
  368. void gcm_init_rv64i_zbc__zbb(u128 Htable[16], const u64 Xi[2]);
  369. void gcm_init_rv64i_zbc__zbkb(u128 Htable[16], const u64 Xi[2]);
  370. void gcm_gmult_rv64i_zbc(u64 Xi[2], const u128 Htable[16]);
  371. void gcm_gmult_rv64i_zbc__zbkb(u64 Xi[2], const u128 Htable[16]);
  372. void gcm_ghash_rv64i_zbc(u64 Xi[2], const u128 Htable[16],
  373. const u8 *inp, size_t len);
  374. void gcm_ghash_rv64i_zbc__zbkb(u64 Xi[2], const u128 Htable[16],
  375. const u8 *inp, size_t len);
  376. # endif
  377. #endif
  378. static void gcm_get_funcs(struct gcm_funcs_st *ctx)
  379. {
  380. /* set defaults -- overridden below as needed */
  381. ctx->ginit = gcm_init_4bit;
  382. #if !defined(GHASH_ASM)
  383. ctx->gmult = gcm_gmult_4bit;
  384. #else
  385. ctx->gmult = NULL;
  386. #endif
  387. #if !defined(GHASH_ASM) && !defined(OPENSSL_SMALL_FOOTPRINT)
  388. ctx->ghash = gcm_ghash_4bit;
  389. #else
  390. ctx->ghash = NULL;
  391. #endif
  392. #if defined(GHASH_ASM_X86_OR_64)
  393. # if !defined(GHASH_ASM_X86) || defined(OPENSSL_IA32_SSE2)
  394. /* x86_64 */
  395. if (OPENSSL_ia32cap_P[1] & (1 << 1)) { /* check PCLMULQDQ bit */
  396. if (((OPENSSL_ia32cap_P[1] >> 22) & 0x41) == 0x41) { /* AVX+MOVBE */
  397. ctx->ginit = gcm_init_avx;
  398. ctx->gmult = gcm_gmult_avx;
  399. ctx->ghash = gcm_ghash_avx;
  400. } else {
  401. ctx->ginit = gcm_init_clmul;
  402. ctx->gmult = gcm_gmult_clmul;
  403. ctx->ghash = gcm_ghash_clmul;
  404. }
  405. return;
  406. }
  407. # endif
  408. # if defined(GHASH_ASM_X86)
  409. /* x86 only */
  410. # if defined(OPENSSL_IA32_SSE2)
  411. if (OPENSSL_ia32cap_P[0] & (1 << 25)) { /* check SSE bit */
  412. ctx->gmult = gcm_gmult_4bit_mmx;
  413. ctx->ghash = gcm_ghash_4bit_mmx;
  414. return;
  415. }
  416. # else
  417. if (OPENSSL_ia32cap_P[0] & (1 << 23)) { /* check MMX bit */
  418. ctx->gmult = gcm_gmult_4bit_mmx;
  419. ctx->ghash = gcm_ghash_4bit_mmx;
  420. return;
  421. }
  422. # endif
  423. ctx->gmult = gcm_gmult_4bit_x86;
  424. ctx->ghash = gcm_ghash_4bit_x86;
  425. return;
  426. # else
  427. /* x86_64 fallback defaults */
  428. ctx->gmult = gcm_gmult_4bit;
  429. ctx->ghash = gcm_ghash_4bit;
  430. return;
  431. # endif
  432. #elif defined(GHASH_ASM_ARM)
  433. /* ARM defaults */
  434. ctx->gmult = gcm_gmult_4bit;
  435. # if !defined(OPENSSL_SMALL_FOOTPRINT)
  436. ctx->ghash = gcm_ghash_4bit;
  437. # else
  438. ctx->ghash = NULL;
  439. # endif
  440. # ifdef PMULL_CAPABLE
  441. if (PMULL_CAPABLE) {
  442. ctx->ginit = (gcm_init_fn)gcm_init_v8;
  443. ctx->gmult = gcm_gmult_v8;
  444. ctx->ghash = gcm_ghash_v8;
  445. }
  446. # elif defined(NEON_CAPABLE)
  447. if (NEON_CAPABLE) {
  448. ctx->ginit = gcm_init_neon;
  449. ctx->gmult = gcm_gmult_neon;
  450. ctx->ghash = gcm_ghash_neon;
  451. }
  452. # endif
  453. return;
  454. #elif defined(GHASH_ASM_SPARC)
  455. /* SPARC defaults */
  456. ctx->gmult = gcm_gmult_4bit;
  457. ctx->ghash = gcm_ghash_4bit;
  458. if (OPENSSL_sparcv9cap_P[0] & SPARCV9_VIS3) {
  459. ctx->ginit = gcm_init_vis3;
  460. ctx->gmult = gcm_gmult_vis3;
  461. ctx->ghash = gcm_ghash_vis3;
  462. }
  463. return;
  464. #elif defined(GHASH_ASM_PPC)
  465. /* PowerPC does not define GHASH_ASM; defaults set above */
  466. if (OPENSSL_ppccap_P & PPC_CRYPTO207) {
  467. ctx->ginit = gcm_init_p8;
  468. ctx->gmult = gcm_gmult_p8;
  469. ctx->ghash = gcm_ghash_p8;
  470. }
  471. return;
  472. #elif defined(GHASH_ASM_RV64I)
  473. /* RISCV defaults */
  474. ctx->gmult = gcm_gmult_4bit;
  475. ctx->ghash = gcm_ghash_4bit;
  476. if (RISCV_HAS_ZBC()) {
  477. if (RISCV_HAS_ZBKB()) {
  478. ctx->ginit = gcm_init_rv64i_zbc__zbkb;
  479. ctx->gmult = gcm_gmult_rv64i_zbc__zbkb;
  480. ctx->ghash = gcm_ghash_rv64i_zbc__zbkb;
  481. } else if (RISCV_HAS_ZBB()) {
  482. ctx->ginit = gcm_init_rv64i_zbc__zbb;
  483. ctx->gmult = gcm_gmult_rv64i_zbc;
  484. ctx->ghash = gcm_ghash_rv64i_zbc;
  485. } else {
  486. ctx->ginit = gcm_init_rv64i_zbc;
  487. ctx->gmult = gcm_gmult_rv64i_zbc;
  488. ctx->ghash = gcm_ghash_rv64i_zbc;
  489. }
  490. }
  491. return;
  492. #elif defined(GHASH_ASM)
  493. /* all other architectures use the generic names */
  494. ctx->gmult = gcm_gmult_4bit;
  495. ctx->ghash = gcm_ghash_4bit;
  496. return;
  497. #endif
  498. }
  499. void ossl_gcm_init_4bit(u128 Htable[16], const u64 H[2])
  500. {
  501. struct gcm_funcs_st funcs;
  502. gcm_get_funcs(&funcs);
  503. funcs.ginit(Htable, H);
  504. }
  505. void ossl_gcm_gmult_4bit(u64 Xi[2], const u128 Htable[16])
  506. {
  507. struct gcm_funcs_st funcs;
  508. gcm_get_funcs(&funcs);
  509. funcs.gmult(Xi, Htable);
  510. }
  511. void ossl_gcm_ghash_4bit(u64 Xi[2], const u128 Htable[16],
  512. const u8 *inp, size_t len)
  513. {
  514. struct gcm_funcs_st funcs;
  515. u64 tmp[2];
  516. size_t i;
  517. gcm_get_funcs(&funcs);
  518. if (funcs.ghash != NULL) {
  519. funcs.ghash(Xi, Htable, inp, len);
  520. } else {
  521. /* Emulate ghash if needed */
  522. for (i = 0; i < len; i += 16) {
  523. memcpy(tmp, &inp[i], sizeof(tmp));
  524. Xi[0] ^= tmp[0];
  525. Xi[1] ^= tmp[1];
  526. funcs.gmult(Xi, Htable);
  527. }
  528. }
  529. }
  530. void CRYPTO_gcm128_init(GCM128_CONTEXT *ctx, void *key, block128_f block)
  531. {
  532. DECLARE_IS_ENDIAN;
  533. memset(ctx, 0, sizeof(*ctx));
  534. ctx->block = block;
  535. ctx->key = key;
  536. (*block) (ctx->H.c, ctx->H.c, key);
  537. if (IS_LITTLE_ENDIAN) {
  538. /* H is stored in host byte order */
  539. #ifdef BSWAP8
  540. ctx->H.u[0] = BSWAP8(ctx->H.u[0]);
  541. ctx->H.u[1] = BSWAP8(ctx->H.u[1]);
  542. #else
  543. u8 *p = ctx->H.c;
  544. u64 hi, lo;
  545. hi = (u64)GETU32(p) << 32 | GETU32(p + 4);
  546. lo = (u64)GETU32(p + 8) << 32 | GETU32(p + 12);
  547. ctx->H.u[0] = hi;
  548. ctx->H.u[1] = lo;
  549. #endif
  550. }
  551. gcm_get_funcs(&ctx->funcs);
  552. ctx->funcs.ginit(ctx->Htable, ctx->H.u);
  553. }
  554. void CRYPTO_gcm128_setiv(GCM128_CONTEXT *ctx, const unsigned char *iv,
  555. size_t len)
  556. {
  557. DECLARE_IS_ENDIAN;
  558. unsigned int ctr;
  559. ctx->len.u[0] = 0; /* AAD length */
  560. ctx->len.u[1] = 0; /* message length */
  561. ctx->ares = 0;
  562. ctx->mres = 0;
  563. if (len == 12) {
  564. memcpy(ctx->Yi.c, iv, 12);
  565. ctx->Yi.c[12] = 0;
  566. ctx->Yi.c[13] = 0;
  567. ctx->Yi.c[14] = 0;
  568. ctx->Yi.c[15] = 1;
  569. ctr = 1;
  570. } else {
  571. size_t i;
  572. u64 len0 = len;
  573. /* Borrow ctx->Xi to calculate initial Yi */
  574. ctx->Xi.u[0] = 0;
  575. ctx->Xi.u[1] = 0;
  576. while (len >= 16) {
  577. for (i = 0; i < 16; ++i)
  578. ctx->Xi.c[i] ^= iv[i];
  579. GCM_MUL(ctx);
  580. iv += 16;
  581. len -= 16;
  582. }
  583. if (len) {
  584. for (i = 0; i < len; ++i)
  585. ctx->Xi.c[i] ^= iv[i];
  586. GCM_MUL(ctx);
  587. }
  588. len0 <<= 3;
  589. if (IS_LITTLE_ENDIAN) {
  590. #ifdef BSWAP8
  591. ctx->Xi.u[1] ^= BSWAP8(len0);
  592. #else
  593. ctx->Xi.c[8] ^= (u8)(len0 >> 56);
  594. ctx->Xi.c[9] ^= (u8)(len0 >> 48);
  595. ctx->Xi.c[10] ^= (u8)(len0 >> 40);
  596. ctx->Xi.c[11] ^= (u8)(len0 >> 32);
  597. ctx->Xi.c[12] ^= (u8)(len0 >> 24);
  598. ctx->Xi.c[13] ^= (u8)(len0 >> 16);
  599. ctx->Xi.c[14] ^= (u8)(len0 >> 8);
  600. ctx->Xi.c[15] ^= (u8)(len0);
  601. #endif
  602. } else {
  603. ctx->Xi.u[1] ^= len0;
  604. }
  605. GCM_MUL(ctx);
  606. if (IS_LITTLE_ENDIAN)
  607. #ifdef BSWAP4
  608. ctr = BSWAP4(ctx->Xi.d[3]);
  609. #else
  610. ctr = GETU32(ctx->Xi.c + 12);
  611. #endif
  612. else
  613. ctr = ctx->Xi.d[3];
  614. /* Copy borrowed Xi to Yi */
  615. ctx->Yi.u[0] = ctx->Xi.u[0];
  616. ctx->Yi.u[1] = ctx->Xi.u[1];
  617. }
  618. ctx->Xi.u[0] = 0;
  619. ctx->Xi.u[1] = 0;
  620. (*ctx->block) (ctx->Yi.c, ctx->EK0.c, ctx->key);
  621. ++ctr;
  622. if (IS_LITTLE_ENDIAN)
  623. #ifdef BSWAP4
  624. ctx->Yi.d[3] = BSWAP4(ctr);
  625. #else
  626. PUTU32(ctx->Yi.c + 12, ctr);
  627. #endif
  628. else
  629. ctx->Yi.d[3] = ctr;
  630. }
  631. int CRYPTO_gcm128_aad(GCM128_CONTEXT *ctx, const unsigned char *aad,
  632. size_t len)
  633. {
  634. size_t i;
  635. unsigned int n;
  636. u64 alen = ctx->len.u[0];
  637. if (ctx->len.u[1])
  638. return -2;
  639. alen += len;
  640. if (alen > (U64(1) << 61) || (sizeof(len) == 8 && alen < len))
  641. return -1;
  642. ctx->len.u[0] = alen;
  643. n = ctx->ares;
  644. if (n) {
  645. while (n && len) {
  646. ctx->Xi.c[n] ^= *(aad++);
  647. --len;
  648. n = (n + 1) % 16;
  649. }
  650. if (n == 0)
  651. GCM_MUL(ctx);
  652. else {
  653. ctx->ares = n;
  654. return 0;
  655. }
  656. }
  657. #ifdef GHASH
  658. if ((i = (len & (size_t)-16))) {
  659. GHASH(ctx, aad, i);
  660. aad += i;
  661. len -= i;
  662. }
  663. #else
  664. while (len >= 16) {
  665. for (i = 0; i < 16; ++i)
  666. ctx->Xi.c[i] ^= aad[i];
  667. GCM_MUL(ctx);
  668. aad += 16;
  669. len -= 16;
  670. }
  671. #endif
  672. if (len) {
  673. n = (unsigned int)len;
  674. for (i = 0; i < len; ++i)
  675. ctx->Xi.c[i] ^= aad[i];
  676. }
  677. ctx->ares = n;
  678. return 0;
  679. }
  680. int CRYPTO_gcm128_encrypt(GCM128_CONTEXT *ctx,
  681. const unsigned char *in, unsigned char *out,
  682. size_t len)
  683. {
  684. DECLARE_IS_ENDIAN;
  685. unsigned int n, ctr, mres;
  686. size_t i;
  687. u64 mlen = ctx->len.u[1];
  688. block128_f block = ctx->block;
  689. void *key = ctx->key;
  690. mlen += len;
  691. if (mlen > ((U64(1) << 36) - 32) || (sizeof(len) == 8 && mlen < len))
  692. return -1;
  693. ctx->len.u[1] = mlen;
  694. mres = ctx->mres;
  695. if (ctx->ares) {
  696. /* First call to encrypt finalizes GHASH(AAD) */
  697. #if defined(GHASH) && !defined(OPENSSL_SMALL_FOOTPRINT)
  698. if (len == 0) {
  699. GCM_MUL(ctx);
  700. ctx->ares = 0;
  701. return 0;
  702. }
  703. memcpy(ctx->Xn, ctx->Xi.c, sizeof(ctx->Xi));
  704. ctx->Xi.u[0] = 0;
  705. ctx->Xi.u[1] = 0;
  706. mres = sizeof(ctx->Xi);
  707. #else
  708. GCM_MUL(ctx);
  709. #endif
  710. ctx->ares = 0;
  711. }
  712. if (IS_LITTLE_ENDIAN)
  713. #ifdef BSWAP4
  714. ctr = BSWAP4(ctx->Yi.d[3]);
  715. #else
  716. ctr = GETU32(ctx->Yi.c + 12);
  717. #endif
  718. else
  719. ctr = ctx->Yi.d[3];
  720. n = mres % 16;
  721. #if !defined(OPENSSL_SMALL_FOOTPRINT)
  722. if (16 % sizeof(size_t) == 0) { /* always true actually */
  723. do {
  724. if (n) {
  725. # if defined(GHASH)
  726. while (n && len) {
  727. ctx->Xn[mres++] = *(out++) = *(in++) ^ ctx->EKi.c[n];
  728. --len;
  729. n = (n + 1) % 16;
  730. }
  731. if (n == 0) {
  732. GHASH(ctx, ctx->Xn, mres);
  733. mres = 0;
  734. } else {
  735. ctx->mres = mres;
  736. return 0;
  737. }
  738. # else
  739. while (n && len) {
  740. ctx->Xi.c[n] ^= *(out++) = *(in++) ^ ctx->EKi.c[n];
  741. --len;
  742. n = (n + 1) % 16;
  743. }
  744. if (n == 0) {
  745. GCM_MUL(ctx);
  746. mres = 0;
  747. } else {
  748. ctx->mres = n;
  749. return 0;
  750. }
  751. # endif
  752. }
  753. # if defined(STRICT_ALIGNMENT)
  754. if (((size_t)in | (size_t)out) % sizeof(size_t) != 0)
  755. break;
  756. # endif
  757. # if defined(GHASH)
  758. if (len >= 16 && mres) {
  759. GHASH(ctx, ctx->Xn, mres);
  760. mres = 0;
  761. }
  762. # if defined(GHASH_CHUNK)
  763. while (len >= GHASH_CHUNK) {
  764. size_t j = GHASH_CHUNK;
  765. while (j) {
  766. size_t_aX *out_t = (size_t_aX *)out;
  767. const size_t_aX *in_t = (const size_t_aX *)in;
  768. (*block) (ctx->Yi.c, ctx->EKi.c, key);
  769. ++ctr;
  770. if (IS_LITTLE_ENDIAN)
  771. # ifdef BSWAP4
  772. ctx->Yi.d[3] = BSWAP4(ctr);
  773. # else
  774. PUTU32(ctx->Yi.c + 12, ctr);
  775. # endif
  776. else
  777. ctx->Yi.d[3] = ctr;
  778. for (i = 0; i < 16 / sizeof(size_t); ++i)
  779. out_t[i] = in_t[i] ^ ctx->EKi.t[i];
  780. out += 16;
  781. in += 16;
  782. j -= 16;
  783. }
  784. GHASH(ctx, out - GHASH_CHUNK, GHASH_CHUNK);
  785. len -= GHASH_CHUNK;
  786. }
  787. # endif
  788. if ((i = (len & (size_t)-16))) {
  789. size_t j = i;
  790. while (len >= 16) {
  791. size_t_aX *out_t = (size_t_aX *)out;
  792. const size_t_aX *in_t = (const size_t_aX *)in;
  793. (*block) (ctx->Yi.c, ctx->EKi.c, key);
  794. ++ctr;
  795. if (IS_LITTLE_ENDIAN)
  796. # ifdef BSWAP4
  797. ctx->Yi.d[3] = BSWAP4(ctr);
  798. # else
  799. PUTU32(ctx->Yi.c + 12, ctr);
  800. # endif
  801. else
  802. ctx->Yi.d[3] = ctr;
  803. for (i = 0; i < 16 / sizeof(size_t); ++i)
  804. out_t[i] = in_t[i] ^ ctx->EKi.t[i];
  805. out += 16;
  806. in += 16;
  807. len -= 16;
  808. }
  809. GHASH(ctx, out - j, j);
  810. }
  811. # else
  812. while (len >= 16) {
  813. size_t *out_t = (size_t *)out;
  814. const size_t *in_t = (const size_t *)in;
  815. (*block) (ctx->Yi.c, ctx->EKi.c, key);
  816. ++ctr;
  817. if (IS_LITTLE_ENDIAN)
  818. # ifdef BSWAP4
  819. ctx->Yi.d[3] = BSWAP4(ctr);
  820. # else
  821. PUTU32(ctx->Yi.c + 12, ctr);
  822. # endif
  823. else
  824. ctx->Yi.d[3] = ctr;
  825. for (i = 0; i < 16 / sizeof(size_t); ++i)
  826. ctx->Xi.t[i] ^= out_t[i] = in_t[i] ^ ctx->EKi.t[i];
  827. GCM_MUL(ctx);
  828. out += 16;
  829. in += 16;
  830. len -= 16;
  831. }
  832. # endif
  833. if (len) {
  834. (*block) (ctx->Yi.c, ctx->EKi.c, key);
  835. ++ctr;
  836. if (IS_LITTLE_ENDIAN)
  837. # ifdef BSWAP4
  838. ctx->Yi.d[3] = BSWAP4(ctr);
  839. # else
  840. PUTU32(ctx->Yi.c + 12, ctr);
  841. # endif
  842. else
  843. ctx->Yi.d[3] = ctr;
  844. # if defined(GHASH)
  845. while (len--) {
  846. ctx->Xn[mres++] = out[n] = in[n] ^ ctx->EKi.c[n];
  847. ++n;
  848. }
  849. # else
  850. while (len--) {
  851. ctx->Xi.c[n] ^= out[n] = in[n] ^ ctx->EKi.c[n];
  852. ++n;
  853. }
  854. mres = n;
  855. # endif
  856. }
  857. ctx->mres = mres;
  858. return 0;
  859. } while (0);
  860. }
  861. #endif
  862. for (i = 0; i < len; ++i) {
  863. if (n == 0) {
  864. (*block) (ctx->Yi.c, ctx->EKi.c, key);
  865. ++ctr;
  866. if (IS_LITTLE_ENDIAN)
  867. #ifdef BSWAP4
  868. ctx->Yi.d[3] = BSWAP4(ctr);
  869. #else
  870. PUTU32(ctx->Yi.c + 12, ctr);
  871. #endif
  872. else
  873. ctx->Yi.d[3] = ctr;
  874. }
  875. #if defined(GHASH) && !defined(OPENSSL_SMALL_FOOTPRINT)
  876. ctx->Xn[mres++] = out[i] = in[i] ^ ctx->EKi.c[n];
  877. n = (n + 1) % 16;
  878. if (mres == sizeof(ctx->Xn)) {
  879. GHASH(ctx,ctx->Xn,sizeof(ctx->Xn));
  880. mres = 0;
  881. }
  882. #else
  883. ctx->Xi.c[n] ^= out[i] = in[i] ^ ctx->EKi.c[n];
  884. mres = n = (n + 1) % 16;
  885. if (n == 0)
  886. GCM_MUL(ctx);
  887. #endif
  888. }
  889. ctx->mres = mres;
  890. return 0;
  891. }
  892. int CRYPTO_gcm128_decrypt(GCM128_CONTEXT *ctx,
  893. const unsigned char *in, unsigned char *out,
  894. size_t len)
  895. {
  896. DECLARE_IS_ENDIAN;
  897. unsigned int n, ctr, mres;
  898. size_t i;
  899. u64 mlen = ctx->len.u[1];
  900. block128_f block = ctx->block;
  901. void *key = ctx->key;
  902. mlen += len;
  903. if (mlen > ((U64(1) << 36) - 32) || (sizeof(len) == 8 && mlen < len))
  904. return -1;
  905. ctx->len.u[1] = mlen;
  906. mres = ctx->mres;
  907. if (ctx->ares) {
  908. /* First call to decrypt finalizes GHASH(AAD) */
  909. #if defined(GHASH) && !defined(OPENSSL_SMALL_FOOTPRINT)
  910. if (len == 0) {
  911. GCM_MUL(ctx);
  912. ctx->ares = 0;
  913. return 0;
  914. }
  915. memcpy(ctx->Xn, ctx->Xi.c, sizeof(ctx->Xi));
  916. ctx->Xi.u[0] = 0;
  917. ctx->Xi.u[1] = 0;
  918. mres = sizeof(ctx->Xi);
  919. #else
  920. GCM_MUL(ctx);
  921. #endif
  922. ctx->ares = 0;
  923. }
  924. if (IS_LITTLE_ENDIAN)
  925. #ifdef BSWAP4
  926. ctr = BSWAP4(ctx->Yi.d[3]);
  927. #else
  928. ctr = GETU32(ctx->Yi.c + 12);
  929. #endif
  930. else
  931. ctr = ctx->Yi.d[3];
  932. n = mres % 16;
  933. #if !defined(OPENSSL_SMALL_FOOTPRINT)
  934. if (16 % sizeof(size_t) == 0) { /* always true actually */
  935. do {
  936. if (n) {
  937. # if defined(GHASH)
  938. while (n && len) {
  939. *(out++) = (ctx->Xn[mres++] = *(in++)) ^ ctx->EKi.c[n];
  940. --len;
  941. n = (n + 1) % 16;
  942. }
  943. if (n == 0) {
  944. GHASH(ctx, ctx->Xn, mres);
  945. mres = 0;
  946. } else {
  947. ctx->mres = mres;
  948. return 0;
  949. }
  950. # else
  951. while (n && len) {
  952. u8 c = *(in++);
  953. *(out++) = c ^ ctx->EKi.c[n];
  954. ctx->Xi.c[n] ^= c;
  955. --len;
  956. n = (n + 1) % 16;
  957. }
  958. if (n == 0) {
  959. GCM_MUL(ctx);
  960. mres = 0;
  961. } else {
  962. ctx->mres = n;
  963. return 0;
  964. }
  965. # endif
  966. }
  967. # if defined(STRICT_ALIGNMENT)
  968. if (((size_t)in | (size_t)out) % sizeof(size_t) != 0)
  969. break;
  970. # endif
  971. # if defined(GHASH)
  972. if (len >= 16 && mres) {
  973. GHASH(ctx, ctx->Xn, mres);
  974. mres = 0;
  975. }
  976. # if defined(GHASH_CHUNK)
  977. while (len >= GHASH_CHUNK) {
  978. size_t j = GHASH_CHUNK;
  979. GHASH(ctx, in, GHASH_CHUNK);
  980. while (j) {
  981. size_t_aX *out_t = (size_t_aX *)out;
  982. const size_t_aX *in_t = (const size_t_aX *)in;
  983. (*block) (ctx->Yi.c, ctx->EKi.c, key);
  984. ++ctr;
  985. if (IS_LITTLE_ENDIAN)
  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. out_t[i] = in_t[i] ^ ctx->EKi.t[i];
  995. out += 16;
  996. in += 16;
  997. j -= 16;
  998. }
  999. len -= GHASH_CHUNK;
  1000. }
  1001. # endif
  1002. if ((i = (len & (size_t)-16))) {
  1003. GHASH(ctx, in, i);
  1004. while (len >= 16) {
  1005. size_t_aX *out_t = (size_t_aX *)out;
  1006. const size_t_aX *in_t = (const size_t_aX *)in;
  1007. (*block) (ctx->Yi.c, ctx->EKi.c, key);
  1008. ++ctr;
  1009. if (IS_LITTLE_ENDIAN)
  1010. # ifdef BSWAP4
  1011. ctx->Yi.d[3] = BSWAP4(ctr);
  1012. # else
  1013. PUTU32(ctx->Yi.c + 12, ctr);
  1014. # endif
  1015. else
  1016. ctx->Yi.d[3] = ctr;
  1017. for (i = 0; i < 16 / sizeof(size_t); ++i)
  1018. out_t[i] = in_t[i] ^ ctx->EKi.t[i];
  1019. out += 16;
  1020. in += 16;
  1021. len -= 16;
  1022. }
  1023. }
  1024. # else
  1025. while (len >= 16) {
  1026. size_t *out_t = (size_t *)out;
  1027. const size_t *in_t = (const size_t *)in;
  1028. (*block) (ctx->Yi.c, ctx->EKi.c, key);
  1029. ++ctr;
  1030. if (IS_LITTLE_ENDIAN)
  1031. # ifdef BSWAP4
  1032. ctx->Yi.d[3] = BSWAP4(ctr);
  1033. # else
  1034. PUTU32(ctx->Yi.c + 12, ctr);
  1035. # endif
  1036. else
  1037. ctx->Yi.d[3] = ctr;
  1038. for (i = 0; i < 16 / sizeof(size_t); ++i) {
  1039. size_t c = in_t[i];
  1040. out_t[i] = c ^ ctx->EKi.t[i];
  1041. ctx->Xi.t[i] ^= c;
  1042. }
  1043. GCM_MUL(ctx);
  1044. out += 16;
  1045. in += 16;
  1046. len -= 16;
  1047. }
  1048. # endif
  1049. if (len) {
  1050. (*block) (ctx->Yi.c, ctx->EKi.c, key);
  1051. ++ctr;
  1052. if (IS_LITTLE_ENDIAN)
  1053. # ifdef BSWAP4
  1054. ctx->Yi.d[3] = BSWAP4(ctr);
  1055. # else
  1056. PUTU32(ctx->Yi.c + 12, ctr);
  1057. # endif
  1058. else
  1059. ctx->Yi.d[3] = ctr;
  1060. # if defined(GHASH)
  1061. while (len--) {
  1062. out[n] = (ctx->Xn[mres++] = in[n]) ^ ctx->EKi.c[n];
  1063. ++n;
  1064. }
  1065. # else
  1066. while (len--) {
  1067. u8 c = in[n];
  1068. ctx->Xi.c[n] ^= c;
  1069. out[n] = c ^ ctx->EKi.c[n];
  1070. ++n;
  1071. }
  1072. mres = n;
  1073. # endif
  1074. }
  1075. ctx->mres = mres;
  1076. return 0;
  1077. } while (0);
  1078. }
  1079. #endif
  1080. for (i = 0; i < len; ++i) {
  1081. u8 c;
  1082. if (n == 0) {
  1083. (*block) (ctx->Yi.c, ctx->EKi.c, key);
  1084. ++ctr;
  1085. if (IS_LITTLE_ENDIAN)
  1086. #ifdef BSWAP4
  1087. ctx->Yi.d[3] = BSWAP4(ctr);
  1088. #else
  1089. PUTU32(ctx->Yi.c + 12, ctr);
  1090. #endif
  1091. else
  1092. ctx->Yi.d[3] = ctr;
  1093. }
  1094. #if defined(GHASH) && !defined(OPENSSL_SMALL_FOOTPRINT)
  1095. out[i] = (ctx->Xn[mres++] = c = in[i]) ^ ctx->EKi.c[n];
  1096. n = (n + 1) % 16;
  1097. if (mres == sizeof(ctx->Xn)) {
  1098. GHASH(ctx,ctx->Xn,sizeof(ctx->Xn));
  1099. mres = 0;
  1100. }
  1101. #else
  1102. c = in[i];
  1103. out[i] = c ^ ctx->EKi.c[n];
  1104. ctx->Xi.c[n] ^= c;
  1105. mres = n = (n + 1) % 16;
  1106. if (n == 0)
  1107. GCM_MUL(ctx);
  1108. #endif
  1109. }
  1110. ctx->mres = mres;
  1111. return 0;
  1112. }
  1113. int CRYPTO_gcm128_encrypt_ctr32(GCM128_CONTEXT *ctx,
  1114. const unsigned char *in, unsigned char *out,
  1115. size_t len, ctr128_f stream)
  1116. {
  1117. #if defined(OPENSSL_SMALL_FOOTPRINT)
  1118. return CRYPTO_gcm128_encrypt(ctx, in, out, len);
  1119. #else
  1120. DECLARE_IS_ENDIAN;
  1121. unsigned int n, ctr, mres;
  1122. size_t i;
  1123. u64 mlen = ctx->len.u[1];
  1124. void *key = ctx->key;
  1125. mlen += len;
  1126. if (mlen > ((U64(1) << 36) - 32) || (sizeof(len) == 8 && mlen < len))
  1127. return -1;
  1128. ctx->len.u[1] = mlen;
  1129. mres = ctx->mres;
  1130. if (ctx->ares) {
  1131. /* First call to encrypt finalizes GHASH(AAD) */
  1132. #if defined(GHASH)
  1133. if (len == 0) {
  1134. GCM_MUL(ctx);
  1135. ctx->ares = 0;
  1136. return 0;
  1137. }
  1138. memcpy(ctx->Xn, ctx->Xi.c, sizeof(ctx->Xi));
  1139. ctx->Xi.u[0] = 0;
  1140. ctx->Xi.u[1] = 0;
  1141. mres = sizeof(ctx->Xi);
  1142. #else
  1143. GCM_MUL(ctx);
  1144. #endif
  1145. ctx->ares = 0;
  1146. }
  1147. if (IS_LITTLE_ENDIAN)
  1148. # ifdef BSWAP4
  1149. ctr = BSWAP4(ctx->Yi.d[3]);
  1150. # else
  1151. ctr = GETU32(ctx->Yi.c + 12);
  1152. # endif
  1153. else
  1154. ctr = ctx->Yi.d[3];
  1155. n = mres % 16;
  1156. if (n) {
  1157. # if defined(GHASH)
  1158. while (n && len) {
  1159. ctx->Xn[mres++] = *(out++) = *(in++) ^ ctx->EKi.c[n];
  1160. --len;
  1161. n = (n + 1) % 16;
  1162. }
  1163. if (n == 0) {
  1164. GHASH(ctx, ctx->Xn, mres);
  1165. mres = 0;
  1166. } else {
  1167. ctx->mres = mres;
  1168. return 0;
  1169. }
  1170. # else
  1171. while (n && len) {
  1172. ctx->Xi.c[n] ^= *(out++) = *(in++) ^ ctx->EKi.c[n];
  1173. --len;
  1174. n = (n + 1) % 16;
  1175. }
  1176. if (n == 0) {
  1177. GCM_MUL(ctx);
  1178. mres = 0;
  1179. } else {
  1180. ctx->mres = n;
  1181. return 0;
  1182. }
  1183. # endif
  1184. }
  1185. # if defined(GHASH)
  1186. if (len >= 16 && mres) {
  1187. GHASH(ctx, ctx->Xn, mres);
  1188. mres = 0;
  1189. }
  1190. # if defined(GHASH_CHUNK)
  1191. while (len >= GHASH_CHUNK) {
  1192. (*stream) (in, out, GHASH_CHUNK / 16, key, ctx->Yi.c);
  1193. ctr += GHASH_CHUNK / 16;
  1194. if (IS_LITTLE_ENDIAN)
  1195. # ifdef BSWAP4
  1196. ctx->Yi.d[3] = BSWAP4(ctr);
  1197. # else
  1198. PUTU32(ctx->Yi.c + 12, ctr);
  1199. # endif
  1200. else
  1201. ctx->Yi.d[3] = ctr;
  1202. GHASH(ctx, out, GHASH_CHUNK);
  1203. out += GHASH_CHUNK;
  1204. in += GHASH_CHUNK;
  1205. len -= GHASH_CHUNK;
  1206. }
  1207. # endif
  1208. # endif
  1209. if ((i = (len & (size_t)-16))) {
  1210. size_t j = i / 16;
  1211. (*stream) (in, out, j, key, ctx->Yi.c);
  1212. ctr += (unsigned int)j;
  1213. if (IS_LITTLE_ENDIAN)
  1214. # ifdef BSWAP4
  1215. ctx->Yi.d[3] = BSWAP4(ctr);
  1216. # else
  1217. PUTU32(ctx->Yi.c + 12, ctr);
  1218. # endif
  1219. else
  1220. ctx->Yi.d[3] = ctr;
  1221. in += i;
  1222. len -= i;
  1223. # if defined(GHASH)
  1224. GHASH(ctx, out, i);
  1225. out += i;
  1226. # else
  1227. while (j--) {
  1228. for (i = 0; i < 16; ++i)
  1229. ctx->Xi.c[i] ^= out[i];
  1230. GCM_MUL(ctx);
  1231. out += 16;
  1232. }
  1233. # endif
  1234. }
  1235. if (len) {
  1236. (*ctx->block) (ctx->Yi.c, ctx->EKi.c, key);
  1237. ++ctr;
  1238. if (IS_LITTLE_ENDIAN)
  1239. # ifdef BSWAP4
  1240. ctx->Yi.d[3] = BSWAP4(ctr);
  1241. # else
  1242. PUTU32(ctx->Yi.c + 12, ctr);
  1243. # endif
  1244. else
  1245. ctx->Yi.d[3] = ctr;
  1246. while (len--) {
  1247. # if defined(GHASH)
  1248. ctx->Xn[mres++] = out[n] = in[n] ^ ctx->EKi.c[n];
  1249. # else
  1250. ctx->Xi.c[mres++] ^= out[n] = in[n] ^ ctx->EKi.c[n];
  1251. # endif
  1252. ++n;
  1253. }
  1254. }
  1255. ctx->mres = mres;
  1256. return 0;
  1257. #endif
  1258. }
  1259. int CRYPTO_gcm128_decrypt_ctr32(GCM128_CONTEXT *ctx,
  1260. const unsigned char *in, unsigned char *out,
  1261. size_t len, ctr128_f stream)
  1262. {
  1263. #if defined(OPENSSL_SMALL_FOOTPRINT)
  1264. return CRYPTO_gcm128_decrypt(ctx, in, out, len);
  1265. #else
  1266. DECLARE_IS_ENDIAN;
  1267. unsigned int n, ctr, mres;
  1268. size_t i;
  1269. u64 mlen = ctx->len.u[1];
  1270. void *key = ctx->key;
  1271. mlen += len;
  1272. if (mlen > ((U64(1) << 36) - 32) || (sizeof(len) == 8 && mlen < len))
  1273. return -1;
  1274. ctx->len.u[1] = mlen;
  1275. mres = ctx->mres;
  1276. if (ctx->ares) {
  1277. /* First call to decrypt finalizes GHASH(AAD) */
  1278. # if defined(GHASH)
  1279. if (len == 0) {
  1280. GCM_MUL(ctx);
  1281. ctx->ares = 0;
  1282. return 0;
  1283. }
  1284. memcpy(ctx->Xn, ctx->Xi.c, sizeof(ctx->Xi));
  1285. ctx->Xi.u[0] = 0;
  1286. ctx->Xi.u[1] = 0;
  1287. mres = sizeof(ctx->Xi);
  1288. # else
  1289. GCM_MUL(ctx);
  1290. # endif
  1291. ctx->ares = 0;
  1292. }
  1293. if (IS_LITTLE_ENDIAN)
  1294. # ifdef BSWAP4
  1295. ctr = BSWAP4(ctx->Yi.d[3]);
  1296. # else
  1297. ctr = GETU32(ctx->Yi.c + 12);
  1298. # endif
  1299. else
  1300. ctr = ctx->Yi.d[3];
  1301. n = mres % 16;
  1302. if (n) {
  1303. # if defined(GHASH)
  1304. while (n && len) {
  1305. *(out++) = (ctx->Xn[mres++] = *(in++)) ^ ctx->EKi.c[n];
  1306. --len;
  1307. n = (n + 1) % 16;
  1308. }
  1309. if (n == 0) {
  1310. GHASH(ctx, ctx->Xn, mres);
  1311. mres = 0;
  1312. } else {
  1313. ctx->mres = mres;
  1314. return 0;
  1315. }
  1316. # else
  1317. while (n && len) {
  1318. u8 c = *(in++);
  1319. *(out++) = c ^ ctx->EKi.c[n];
  1320. ctx->Xi.c[n] ^= c;
  1321. --len;
  1322. n = (n + 1) % 16;
  1323. }
  1324. if (n == 0) {
  1325. GCM_MUL(ctx);
  1326. mres = 0;
  1327. } else {
  1328. ctx->mres = n;
  1329. return 0;
  1330. }
  1331. # endif
  1332. }
  1333. # if defined(GHASH)
  1334. if (len >= 16 && mres) {
  1335. GHASH(ctx, ctx->Xn, mres);
  1336. mres = 0;
  1337. }
  1338. # if defined(GHASH_CHUNK)
  1339. while (len >= GHASH_CHUNK) {
  1340. GHASH(ctx, in, GHASH_CHUNK);
  1341. (*stream) (in, out, GHASH_CHUNK / 16, key, ctx->Yi.c);
  1342. ctr += GHASH_CHUNK / 16;
  1343. if (IS_LITTLE_ENDIAN)
  1344. # ifdef BSWAP4
  1345. ctx->Yi.d[3] = BSWAP4(ctr);
  1346. # else
  1347. PUTU32(ctx->Yi.c + 12, ctr);
  1348. # endif
  1349. else
  1350. ctx->Yi.d[3] = ctr;
  1351. out += GHASH_CHUNK;
  1352. in += GHASH_CHUNK;
  1353. len -= GHASH_CHUNK;
  1354. }
  1355. # endif
  1356. # endif
  1357. if ((i = (len & (size_t)-16))) {
  1358. size_t j = i / 16;
  1359. # if defined(GHASH)
  1360. GHASH(ctx, in, i);
  1361. # else
  1362. while (j--) {
  1363. size_t k;
  1364. for (k = 0; k < 16; ++k)
  1365. ctx->Xi.c[k] ^= in[k];
  1366. GCM_MUL(ctx);
  1367. in += 16;
  1368. }
  1369. j = i / 16;
  1370. in -= i;
  1371. # endif
  1372. (*stream) (in, out, j, key, ctx->Yi.c);
  1373. ctr += (unsigned int)j;
  1374. if (IS_LITTLE_ENDIAN)
  1375. # ifdef BSWAP4
  1376. ctx->Yi.d[3] = BSWAP4(ctr);
  1377. # else
  1378. PUTU32(ctx->Yi.c + 12, ctr);
  1379. # endif
  1380. else
  1381. ctx->Yi.d[3] = ctr;
  1382. out += i;
  1383. in += i;
  1384. len -= i;
  1385. }
  1386. if (len) {
  1387. (*ctx->block) (ctx->Yi.c, ctx->EKi.c, key);
  1388. ++ctr;
  1389. if (IS_LITTLE_ENDIAN)
  1390. # ifdef BSWAP4
  1391. ctx->Yi.d[3] = BSWAP4(ctr);
  1392. # else
  1393. PUTU32(ctx->Yi.c + 12, ctr);
  1394. # endif
  1395. else
  1396. ctx->Yi.d[3] = ctr;
  1397. while (len--) {
  1398. # if defined(GHASH)
  1399. out[n] = (ctx->Xn[mres++] = in[n]) ^ ctx->EKi.c[n];
  1400. # else
  1401. u8 c = in[n];
  1402. ctx->Xi.c[mres++] ^= c;
  1403. out[n] = c ^ ctx->EKi.c[n];
  1404. # endif
  1405. ++n;
  1406. }
  1407. }
  1408. ctx->mres = mres;
  1409. return 0;
  1410. #endif
  1411. }
  1412. int CRYPTO_gcm128_finish(GCM128_CONTEXT *ctx, const unsigned char *tag,
  1413. size_t len)
  1414. {
  1415. DECLARE_IS_ENDIAN;
  1416. u64 alen = ctx->len.u[0] << 3;
  1417. u64 clen = ctx->len.u[1] << 3;
  1418. #if defined(GHASH) && !defined(OPENSSL_SMALL_FOOTPRINT)
  1419. u128 bitlen;
  1420. unsigned int mres = ctx->mres;
  1421. if (mres) {
  1422. unsigned blocks = (mres + 15) & -16;
  1423. memset(ctx->Xn + mres, 0, blocks - mres);
  1424. mres = blocks;
  1425. if (mres == sizeof(ctx->Xn)) {
  1426. GHASH(ctx, ctx->Xn, mres);
  1427. mres = 0;
  1428. }
  1429. } else if (ctx->ares) {
  1430. GCM_MUL(ctx);
  1431. }
  1432. #else
  1433. if (ctx->mres || ctx->ares)
  1434. GCM_MUL(ctx);
  1435. #endif
  1436. if (IS_LITTLE_ENDIAN) {
  1437. #ifdef BSWAP8
  1438. alen = BSWAP8(alen);
  1439. clen = BSWAP8(clen);
  1440. #else
  1441. u8 *p = ctx->len.c;
  1442. ctx->len.u[0] = alen;
  1443. ctx->len.u[1] = clen;
  1444. alen = (u64)GETU32(p) << 32 | GETU32(p + 4);
  1445. clen = (u64)GETU32(p + 8) << 32 | GETU32(p + 12);
  1446. #endif
  1447. }
  1448. #if defined(GHASH) && !defined(OPENSSL_SMALL_FOOTPRINT)
  1449. bitlen.hi = alen;
  1450. bitlen.lo = clen;
  1451. memcpy(ctx->Xn + mres, &bitlen, sizeof(bitlen));
  1452. mres += sizeof(bitlen);
  1453. GHASH(ctx, ctx->Xn, mres);
  1454. #else
  1455. ctx->Xi.u[0] ^= alen;
  1456. ctx->Xi.u[1] ^= clen;
  1457. GCM_MUL(ctx);
  1458. #endif
  1459. ctx->Xi.u[0] ^= ctx->EK0.u[0];
  1460. ctx->Xi.u[1] ^= ctx->EK0.u[1];
  1461. if (tag && len <= sizeof(ctx->Xi))
  1462. return CRYPTO_memcmp(ctx->Xi.c, tag, len);
  1463. else
  1464. return -1;
  1465. }
  1466. void CRYPTO_gcm128_tag(GCM128_CONTEXT *ctx, unsigned char *tag, size_t len)
  1467. {
  1468. CRYPTO_gcm128_finish(ctx, NULL, 0);
  1469. memcpy(tag, ctx->Xi.c,
  1470. len <= sizeof(ctx->Xi.c) ? len : sizeof(ctx->Xi.c));
  1471. }
  1472. GCM128_CONTEXT *CRYPTO_gcm128_new(void *key, block128_f block)
  1473. {
  1474. GCM128_CONTEXT *ret;
  1475. if ((ret = OPENSSL_malloc(sizeof(*ret))) != NULL)
  1476. CRYPTO_gcm128_init(ret, key, block);
  1477. return ret;
  1478. }
  1479. void CRYPTO_gcm128_release(GCM128_CONTEXT *ctx)
  1480. {
  1481. OPENSSL_clear_free(ctx, sizeof(*ctx));
  1482. }