gcm128.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625
  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. /* zvkb/Zvbc (vector crypto with vclmul) based routines. */
  377. void gcm_init_rv64i_zvkb_zvbc(u128 Htable[16], const u64 Xi[2]);
  378. void gcm_gmult_rv64i_zvkb_zvbc(u64 Xi[2], const u128 Htable[16]);
  379. void gcm_ghash_rv64i_zvkb_zvbc(u64 Xi[2], const u128 Htable[16],
  380. const u8 *inp, size_t len);
  381. /* Zvkg (vector crypto with vgmul.vv and vghsh.vv). */
  382. void gcm_init_rv64i_zvkg(u128 Htable[16], const u64 Xi[2]);
  383. void gcm_init_rv64i_zvkg_zvkb(u128 Htable[16], const u64 Xi[2]);
  384. void gcm_gmult_rv64i_zvkg(u64 Xi[2], const u128 Htable[16]);
  385. void gcm_ghash_rv64i_zvkg(u64 Xi[2], const u128 Htable[16],
  386. const u8 *inp, size_t len);
  387. # endif
  388. #endif
  389. static void gcm_get_funcs(struct gcm_funcs_st *ctx)
  390. {
  391. /* set defaults -- overridden below as needed */
  392. ctx->ginit = gcm_init_4bit;
  393. #if !defined(GHASH_ASM)
  394. ctx->gmult = gcm_gmult_4bit;
  395. #else
  396. ctx->gmult = NULL;
  397. #endif
  398. #if !defined(GHASH_ASM) && !defined(OPENSSL_SMALL_FOOTPRINT)
  399. ctx->ghash = gcm_ghash_4bit;
  400. #else
  401. ctx->ghash = NULL;
  402. #endif
  403. #if defined(GHASH_ASM_X86_OR_64)
  404. # if !defined(GHASH_ASM_X86) || defined(OPENSSL_IA32_SSE2)
  405. /* x86_64 */
  406. if (OPENSSL_ia32cap_P[1] & (1 << 1)) { /* check PCLMULQDQ bit */
  407. if (((OPENSSL_ia32cap_P[1] >> 22) & 0x41) == 0x41) { /* AVX+MOVBE */
  408. ctx->ginit = gcm_init_avx;
  409. ctx->gmult = gcm_gmult_avx;
  410. ctx->ghash = gcm_ghash_avx;
  411. } else {
  412. ctx->ginit = gcm_init_clmul;
  413. ctx->gmult = gcm_gmult_clmul;
  414. ctx->ghash = gcm_ghash_clmul;
  415. }
  416. return;
  417. }
  418. # endif
  419. # if defined(GHASH_ASM_X86)
  420. /* x86 only */
  421. # if defined(OPENSSL_IA32_SSE2)
  422. if (OPENSSL_ia32cap_P[0] & (1 << 25)) { /* check SSE bit */
  423. ctx->gmult = gcm_gmult_4bit_mmx;
  424. ctx->ghash = gcm_ghash_4bit_mmx;
  425. return;
  426. }
  427. # else
  428. if (OPENSSL_ia32cap_P[0] & (1 << 23)) { /* check MMX bit */
  429. ctx->gmult = gcm_gmult_4bit_mmx;
  430. ctx->ghash = gcm_ghash_4bit_mmx;
  431. return;
  432. }
  433. # endif
  434. ctx->gmult = gcm_gmult_4bit_x86;
  435. ctx->ghash = gcm_ghash_4bit_x86;
  436. return;
  437. # else
  438. /* x86_64 fallback defaults */
  439. ctx->gmult = gcm_gmult_4bit;
  440. ctx->ghash = gcm_ghash_4bit;
  441. return;
  442. # endif
  443. #elif defined(GHASH_ASM_ARM)
  444. /* ARM defaults */
  445. ctx->gmult = gcm_gmult_4bit;
  446. ctx->ghash = gcm_ghash_4bit;
  447. # ifdef PMULL_CAPABLE
  448. if (PMULL_CAPABLE) {
  449. ctx->ginit = (gcm_init_fn)gcm_init_v8;
  450. ctx->gmult = gcm_gmult_v8;
  451. ctx->ghash = gcm_ghash_v8;
  452. }
  453. # elif defined(NEON_CAPABLE)
  454. if (NEON_CAPABLE) {
  455. ctx->ginit = gcm_init_neon;
  456. ctx->gmult = gcm_gmult_neon;
  457. ctx->ghash = gcm_ghash_neon;
  458. }
  459. # endif
  460. return;
  461. #elif defined(GHASH_ASM_SPARC)
  462. /* SPARC defaults */
  463. ctx->gmult = gcm_gmult_4bit;
  464. ctx->ghash = gcm_ghash_4bit;
  465. if (OPENSSL_sparcv9cap_P[0] & SPARCV9_VIS3) {
  466. ctx->ginit = gcm_init_vis3;
  467. ctx->gmult = gcm_gmult_vis3;
  468. ctx->ghash = gcm_ghash_vis3;
  469. }
  470. return;
  471. #elif defined(GHASH_ASM_PPC)
  472. /* PowerPC does not define GHASH_ASM; defaults set above */
  473. if (OPENSSL_ppccap_P & PPC_CRYPTO207) {
  474. ctx->ginit = gcm_init_p8;
  475. ctx->gmult = gcm_gmult_p8;
  476. ctx->ghash = gcm_ghash_p8;
  477. }
  478. return;
  479. #elif defined(GHASH_ASM_RV64I)
  480. /* RISCV defaults */
  481. ctx->gmult = gcm_gmult_4bit;
  482. ctx->ghash = gcm_ghash_4bit;
  483. if (RISCV_HAS_ZVKG() && riscv_vlen() >= 128) {
  484. if (RISCV_HAS_ZVKB())
  485. ctx->ginit = gcm_init_rv64i_zvkg_zvkb;
  486. else
  487. ctx->ginit = gcm_init_rv64i_zvkg;
  488. ctx->gmult = gcm_gmult_rv64i_zvkg;
  489. ctx->ghash = gcm_ghash_rv64i_zvkg;
  490. } else if (RISCV_HAS_ZVKB() && RISCV_HAS_ZVBC() && riscv_vlen() >= 128) {
  491. ctx->ginit = gcm_init_rv64i_zvkb_zvbc;
  492. ctx->gmult = gcm_gmult_rv64i_zvkb_zvbc;
  493. ctx->ghash = gcm_ghash_rv64i_zvkb_zvbc;
  494. } else if (RISCV_HAS_ZBC()) {
  495. if (RISCV_HAS_ZBKB()) {
  496. ctx->ginit = gcm_init_rv64i_zbc__zbkb;
  497. ctx->gmult = gcm_gmult_rv64i_zbc__zbkb;
  498. ctx->ghash = gcm_ghash_rv64i_zbc__zbkb;
  499. } else if (RISCV_HAS_ZBB()) {
  500. ctx->ginit = gcm_init_rv64i_zbc__zbb;
  501. ctx->gmult = gcm_gmult_rv64i_zbc;
  502. ctx->ghash = gcm_ghash_rv64i_zbc;
  503. } else {
  504. ctx->ginit = gcm_init_rv64i_zbc;
  505. ctx->gmult = gcm_gmult_rv64i_zbc;
  506. ctx->ghash = gcm_ghash_rv64i_zbc;
  507. }
  508. }
  509. return;
  510. #elif defined(GHASH_ASM)
  511. /* all other architectures use the generic names */
  512. ctx->gmult = gcm_gmult_4bit;
  513. ctx->ghash = gcm_ghash_4bit;
  514. return;
  515. #endif
  516. }
  517. void ossl_gcm_init_4bit(u128 Htable[16], const u64 H[2])
  518. {
  519. struct gcm_funcs_st funcs;
  520. gcm_get_funcs(&funcs);
  521. funcs.ginit(Htable, H);
  522. }
  523. void ossl_gcm_gmult_4bit(u64 Xi[2], const u128 Htable[16])
  524. {
  525. struct gcm_funcs_st funcs;
  526. gcm_get_funcs(&funcs);
  527. funcs.gmult(Xi, Htable);
  528. }
  529. void ossl_gcm_ghash_4bit(u64 Xi[2], const u128 Htable[16],
  530. const u8 *inp, size_t len)
  531. {
  532. struct gcm_funcs_st funcs;
  533. u64 tmp[2];
  534. size_t i;
  535. gcm_get_funcs(&funcs);
  536. if (funcs.ghash != NULL) {
  537. funcs.ghash(Xi, Htable, inp, len);
  538. } else {
  539. /* Emulate ghash if needed */
  540. for (i = 0; i < len; i += 16) {
  541. memcpy(tmp, &inp[i], sizeof(tmp));
  542. Xi[0] ^= tmp[0];
  543. Xi[1] ^= tmp[1];
  544. funcs.gmult(Xi, Htable);
  545. }
  546. }
  547. }
  548. void CRYPTO_gcm128_init(GCM128_CONTEXT *ctx, void *key, block128_f block)
  549. {
  550. DECLARE_IS_ENDIAN;
  551. memset(ctx, 0, sizeof(*ctx));
  552. ctx->block = block;
  553. ctx->key = key;
  554. (*block) (ctx->H.c, ctx->H.c, key);
  555. if (IS_LITTLE_ENDIAN) {
  556. /* H is stored in host byte order */
  557. #ifdef BSWAP8
  558. ctx->H.u[0] = BSWAP8(ctx->H.u[0]);
  559. ctx->H.u[1] = BSWAP8(ctx->H.u[1]);
  560. #else
  561. u8 *p = ctx->H.c;
  562. u64 hi, lo;
  563. hi = (u64)GETU32(p) << 32 | GETU32(p + 4);
  564. lo = (u64)GETU32(p + 8) << 32 | GETU32(p + 12);
  565. ctx->H.u[0] = hi;
  566. ctx->H.u[1] = lo;
  567. #endif
  568. }
  569. gcm_get_funcs(&ctx->funcs);
  570. ctx->funcs.ginit(ctx->Htable, ctx->H.u);
  571. }
  572. void CRYPTO_gcm128_setiv(GCM128_CONTEXT *ctx, const unsigned char *iv,
  573. size_t len)
  574. {
  575. DECLARE_IS_ENDIAN;
  576. unsigned int ctr;
  577. ctx->len.u[0] = 0; /* AAD length */
  578. ctx->len.u[1] = 0; /* message length */
  579. ctx->ares = 0;
  580. ctx->mres = 0;
  581. if (len == 12) {
  582. memcpy(ctx->Yi.c, iv, 12);
  583. ctx->Yi.c[12] = 0;
  584. ctx->Yi.c[13] = 0;
  585. ctx->Yi.c[14] = 0;
  586. ctx->Yi.c[15] = 1;
  587. ctr = 1;
  588. } else {
  589. size_t i;
  590. u64 len0 = len;
  591. /* Borrow ctx->Xi to calculate initial Yi */
  592. ctx->Xi.u[0] = 0;
  593. ctx->Xi.u[1] = 0;
  594. while (len >= 16) {
  595. for (i = 0; i < 16; ++i)
  596. ctx->Xi.c[i] ^= iv[i];
  597. GCM_MUL(ctx);
  598. iv += 16;
  599. len -= 16;
  600. }
  601. if (len) {
  602. for (i = 0; i < len; ++i)
  603. ctx->Xi.c[i] ^= iv[i];
  604. GCM_MUL(ctx);
  605. }
  606. len0 <<= 3;
  607. if (IS_LITTLE_ENDIAN) {
  608. #ifdef BSWAP8
  609. ctx->Xi.u[1] ^= BSWAP8(len0);
  610. #else
  611. ctx->Xi.c[8] ^= (u8)(len0 >> 56);
  612. ctx->Xi.c[9] ^= (u8)(len0 >> 48);
  613. ctx->Xi.c[10] ^= (u8)(len0 >> 40);
  614. ctx->Xi.c[11] ^= (u8)(len0 >> 32);
  615. ctx->Xi.c[12] ^= (u8)(len0 >> 24);
  616. ctx->Xi.c[13] ^= (u8)(len0 >> 16);
  617. ctx->Xi.c[14] ^= (u8)(len0 >> 8);
  618. ctx->Xi.c[15] ^= (u8)(len0);
  619. #endif
  620. } else {
  621. ctx->Xi.u[1] ^= len0;
  622. }
  623. GCM_MUL(ctx);
  624. if (IS_LITTLE_ENDIAN)
  625. #ifdef BSWAP4
  626. ctr = BSWAP4(ctx->Xi.d[3]);
  627. #else
  628. ctr = GETU32(ctx->Xi.c + 12);
  629. #endif
  630. else
  631. ctr = ctx->Xi.d[3];
  632. /* Copy borrowed Xi to Yi */
  633. ctx->Yi.u[0] = ctx->Xi.u[0];
  634. ctx->Yi.u[1] = ctx->Xi.u[1];
  635. }
  636. ctx->Xi.u[0] = 0;
  637. ctx->Xi.u[1] = 0;
  638. (*ctx->block) (ctx->Yi.c, ctx->EK0.c, ctx->key);
  639. ++ctr;
  640. if (IS_LITTLE_ENDIAN)
  641. #ifdef BSWAP4
  642. ctx->Yi.d[3] = BSWAP4(ctr);
  643. #else
  644. PUTU32(ctx->Yi.c + 12, ctr);
  645. #endif
  646. else
  647. ctx->Yi.d[3] = ctr;
  648. }
  649. int CRYPTO_gcm128_aad(GCM128_CONTEXT *ctx, const unsigned char *aad,
  650. size_t len)
  651. {
  652. size_t i;
  653. unsigned int n;
  654. u64 alen = ctx->len.u[0];
  655. if (ctx->len.u[1])
  656. return -2;
  657. alen += len;
  658. if (alen > (U64(1) << 61) || (sizeof(len) == 8 && alen < len))
  659. return -1;
  660. ctx->len.u[0] = alen;
  661. n = ctx->ares;
  662. if (n) {
  663. while (n && len) {
  664. ctx->Xi.c[n] ^= *(aad++);
  665. --len;
  666. n = (n + 1) % 16;
  667. }
  668. if (n == 0)
  669. GCM_MUL(ctx);
  670. else {
  671. ctx->ares = n;
  672. return 0;
  673. }
  674. }
  675. #ifdef GHASH
  676. if ((i = (len & (size_t)-16))) {
  677. GHASH(ctx, aad, i);
  678. aad += i;
  679. len -= i;
  680. }
  681. #else
  682. while (len >= 16) {
  683. for (i = 0; i < 16; ++i)
  684. ctx->Xi.c[i] ^= aad[i];
  685. GCM_MUL(ctx);
  686. aad += 16;
  687. len -= 16;
  688. }
  689. #endif
  690. if (len) {
  691. n = (unsigned int)len;
  692. for (i = 0; i < len; ++i)
  693. ctx->Xi.c[i] ^= aad[i];
  694. }
  695. ctx->ares = n;
  696. return 0;
  697. }
  698. int CRYPTO_gcm128_encrypt(GCM128_CONTEXT *ctx,
  699. const unsigned char *in, unsigned char *out,
  700. size_t len)
  701. {
  702. DECLARE_IS_ENDIAN;
  703. unsigned int n, ctr, mres;
  704. size_t i;
  705. u64 mlen = ctx->len.u[1];
  706. block128_f block = ctx->block;
  707. void *key = ctx->key;
  708. mlen += len;
  709. if (mlen > ((U64(1) << 36) - 32) || (sizeof(len) == 8 && mlen < len))
  710. return -1;
  711. ctx->len.u[1] = mlen;
  712. mres = ctx->mres;
  713. if (ctx->ares) {
  714. /* First call to encrypt finalizes GHASH(AAD) */
  715. #if defined(GHASH) && !defined(OPENSSL_SMALL_FOOTPRINT)
  716. if (len == 0) {
  717. GCM_MUL(ctx);
  718. ctx->ares = 0;
  719. return 0;
  720. }
  721. memcpy(ctx->Xn, ctx->Xi.c, sizeof(ctx->Xi));
  722. ctx->Xi.u[0] = 0;
  723. ctx->Xi.u[1] = 0;
  724. mres = sizeof(ctx->Xi);
  725. #else
  726. GCM_MUL(ctx);
  727. #endif
  728. ctx->ares = 0;
  729. }
  730. if (IS_LITTLE_ENDIAN)
  731. #ifdef BSWAP4
  732. ctr = BSWAP4(ctx->Yi.d[3]);
  733. #else
  734. ctr = GETU32(ctx->Yi.c + 12);
  735. #endif
  736. else
  737. ctr = ctx->Yi.d[3];
  738. n = mres % 16;
  739. #if !defined(OPENSSL_SMALL_FOOTPRINT)
  740. if (16 % sizeof(size_t) == 0) { /* always true actually */
  741. do {
  742. if (n) {
  743. # if defined(GHASH)
  744. while (n && len) {
  745. ctx->Xn[mres++] = *(out++) = *(in++) ^ ctx->EKi.c[n];
  746. --len;
  747. n = (n + 1) % 16;
  748. }
  749. if (n == 0) {
  750. GHASH(ctx, ctx->Xn, mres);
  751. mres = 0;
  752. } else {
  753. ctx->mres = mres;
  754. return 0;
  755. }
  756. # else
  757. while (n && len) {
  758. ctx->Xi.c[n] ^= *(out++) = *(in++) ^ ctx->EKi.c[n];
  759. --len;
  760. n = (n + 1) % 16;
  761. }
  762. if (n == 0) {
  763. GCM_MUL(ctx);
  764. mres = 0;
  765. } else {
  766. ctx->mres = n;
  767. return 0;
  768. }
  769. # endif
  770. }
  771. # if defined(STRICT_ALIGNMENT)
  772. if (((size_t)in | (size_t)out) % sizeof(size_t) != 0)
  773. break;
  774. # endif
  775. # if defined(GHASH)
  776. if (len >= 16 && mres) {
  777. GHASH(ctx, ctx->Xn, mres);
  778. mres = 0;
  779. }
  780. # if defined(GHASH_CHUNK)
  781. while (len >= GHASH_CHUNK) {
  782. size_t j = GHASH_CHUNK;
  783. while (j) {
  784. size_t_aX *out_t = (size_t_aX *)out;
  785. const size_t_aX *in_t = (const size_t_aX *)in;
  786. (*block) (ctx->Yi.c, ctx->EKi.c, key);
  787. ++ctr;
  788. if (IS_LITTLE_ENDIAN)
  789. # ifdef BSWAP4
  790. ctx->Yi.d[3] = BSWAP4(ctr);
  791. # else
  792. PUTU32(ctx->Yi.c + 12, ctr);
  793. # endif
  794. else
  795. ctx->Yi.d[3] = ctr;
  796. for (i = 0; i < 16 / sizeof(size_t); ++i)
  797. out_t[i] = in_t[i] ^ ctx->EKi.t[i];
  798. out += 16;
  799. in += 16;
  800. j -= 16;
  801. }
  802. GHASH(ctx, out - GHASH_CHUNK, GHASH_CHUNK);
  803. len -= GHASH_CHUNK;
  804. }
  805. # endif
  806. if ((i = (len & (size_t)-16))) {
  807. size_t j = i;
  808. while (len >= 16) {
  809. size_t_aX *out_t = (size_t_aX *)out;
  810. const size_t_aX *in_t = (const size_t_aX *)in;
  811. (*block) (ctx->Yi.c, ctx->EKi.c, 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. for (i = 0; i < 16 / sizeof(size_t); ++i)
  822. out_t[i] = in_t[i] ^ ctx->EKi.t[i];
  823. out += 16;
  824. in += 16;
  825. len -= 16;
  826. }
  827. GHASH(ctx, out - j, j);
  828. }
  829. # else
  830. while (len >= 16) {
  831. size_t *out_t = (size_t *)out;
  832. const size_t *in_t = (const size_t *)in;
  833. (*block) (ctx->Yi.c, ctx->EKi.c, key);
  834. ++ctr;
  835. if (IS_LITTLE_ENDIAN)
  836. # ifdef BSWAP4
  837. ctx->Yi.d[3] = BSWAP4(ctr);
  838. # else
  839. PUTU32(ctx->Yi.c + 12, ctr);
  840. # endif
  841. else
  842. ctx->Yi.d[3] = ctr;
  843. for (i = 0; i < 16 / sizeof(size_t); ++i)
  844. ctx->Xi.t[i] ^= out_t[i] = in_t[i] ^ ctx->EKi.t[i];
  845. GCM_MUL(ctx);
  846. out += 16;
  847. in += 16;
  848. len -= 16;
  849. }
  850. # endif
  851. if (len) {
  852. (*block) (ctx->Yi.c, ctx->EKi.c, key);
  853. ++ctr;
  854. if (IS_LITTLE_ENDIAN)
  855. # ifdef BSWAP4
  856. ctx->Yi.d[3] = BSWAP4(ctr);
  857. # else
  858. PUTU32(ctx->Yi.c + 12, ctr);
  859. # endif
  860. else
  861. ctx->Yi.d[3] = ctr;
  862. # if defined(GHASH)
  863. while (len--) {
  864. ctx->Xn[mres++] = out[n] = in[n] ^ ctx->EKi.c[n];
  865. ++n;
  866. }
  867. # else
  868. while (len--) {
  869. ctx->Xi.c[n] ^= out[n] = in[n] ^ ctx->EKi.c[n];
  870. ++n;
  871. }
  872. mres = n;
  873. # endif
  874. }
  875. ctx->mres = mres;
  876. return 0;
  877. } while (0);
  878. }
  879. #endif
  880. for (i = 0; i < len; ++i) {
  881. if (n == 0) {
  882. (*block) (ctx->Yi.c, ctx->EKi.c, key);
  883. ++ctr;
  884. if (IS_LITTLE_ENDIAN)
  885. #ifdef BSWAP4
  886. ctx->Yi.d[3] = BSWAP4(ctr);
  887. #else
  888. PUTU32(ctx->Yi.c + 12, ctr);
  889. #endif
  890. else
  891. ctx->Yi.d[3] = ctr;
  892. }
  893. #if defined(GHASH) && !defined(OPENSSL_SMALL_FOOTPRINT)
  894. ctx->Xn[mres++] = out[i] = in[i] ^ ctx->EKi.c[n];
  895. n = (n + 1) % 16;
  896. if (mres == sizeof(ctx->Xn)) {
  897. GHASH(ctx,ctx->Xn,sizeof(ctx->Xn));
  898. mres = 0;
  899. }
  900. #else
  901. ctx->Xi.c[n] ^= out[i] = in[i] ^ ctx->EKi.c[n];
  902. mres = n = (n + 1) % 16;
  903. if (n == 0)
  904. GCM_MUL(ctx);
  905. #endif
  906. }
  907. ctx->mres = mres;
  908. return 0;
  909. }
  910. int CRYPTO_gcm128_decrypt(GCM128_CONTEXT *ctx,
  911. const unsigned char *in, unsigned char *out,
  912. size_t len)
  913. {
  914. DECLARE_IS_ENDIAN;
  915. unsigned int n, ctr, mres;
  916. size_t i;
  917. u64 mlen = ctx->len.u[1];
  918. block128_f block = ctx->block;
  919. void *key = ctx->key;
  920. mlen += len;
  921. if (mlen > ((U64(1) << 36) - 32) || (sizeof(len) == 8 && mlen < len))
  922. return -1;
  923. ctx->len.u[1] = mlen;
  924. mres = ctx->mres;
  925. if (ctx->ares) {
  926. /* First call to decrypt finalizes GHASH(AAD) */
  927. #if defined(GHASH) && !defined(OPENSSL_SMALL_FOOTPRINT)
  928. if (len == 0) {
  929. GCM_MUL(ctx);
  930. ctx->ares = 0;
  931. return 0;
  932. }
  933. memcpy(ctx->Xn, ctx->Xi.c, sizeof(ctx->Xi));
  934. ctx->Xi.u[0] = 0;
  935. ctx->Xi.u[1] = 0;
  936. mres = sizeof(ctx->Xi);
  937. #else
  938. GCM_MUL(ctx);
  939. #endif
  940. ctx->ares = 0;
  941. }
  942. if (IS_LITTLE_ENDIAN)
  943. #ifdef BSWAP4
  944. ctr = BSWAP4(ctx->Yi.d[3]);
  945. #else
  946. ctr = GETU32(ctx->Yi.c + 12);
  947. #endif
  948. else
  949. ctr = ctx->Yi.d[3];
  950. n = mres % 16;
  951. #if !defined(OPENSSL_SMALL_FOOTPRINT)
  952. if (16 % sizeof(size_t) == 0) { /* always true actually */
  953. do {
  954. if (n) {
  955. # if defined(GHASH)
  956. while (n && len) {
  957. *(out++) = (ctx->Xn[mres++] = *(in++)) ^ ctx->EKi.c[n];
  958. --len;
  959. n = (n + 1) % 16;
  960. }
  961. if (n == 0) {
  962. GHASH(ctx, ctx->Xn, mres);
  963. mres = 0;
  964. } else {
  965. ctx->mres = mres;
  966. return 0;
  967. }
  968. # else
  969. while (n && len) {
  970. u8 c = *(in++);
  971. *(out++) = c ^ ctx->EKi.c[n];
  972. ctx->Xi.c[n] ^= c;
  973. --len;
  974. n = (n + 1) % 16;
  975. }
  976. if (n == 0) {
  977. GCM_MUL(ctx);
  978. mres = 0;
  979. } else {
  980. ctx->mres = n;
  981. return 0;
  982. }
  983. # endif
  984. }
  985. # if defined(STRICT_ALIGNMENT)
  986. if (((size_t)in | (size_t)out) % sizeof(size_t) != 0)
  987. break;
  988. # endif
  989. # if defined(GHASH)
  990. if (len >= 16 && mres) {
  991. GHASH(ctx, ctx->Xn, mres);
  992. mres = 0;
  993. }
  994. # if defined(GHASH_CHUNK)
  995. while (len >= GHASH_CHUNK) {
  996. size_t j = GHASH_CHUNK;
  997. GHASH(ctx, in, GHASH_CHUNK);
  998. while (j) {
  999. size_t_aX *out_t = (size_t_aX *)out;
  1000. const size_t_aX *in_t = (const size_t_aX *)in;
  1001. (*block) (ctx->Yi.c, ctx->EKi.c, key);
  1002. ++ctr;
  1003. if (IS_LITTLE_ENDIAN)
  1004. # ifdef BSWAP4
  1005. ctx->Yi.d[3] = BSWAP4(ctr);
  1006. # else
  1007. PUTU32(ctx->Yi.c + 12, ctr);
  1008. # endif
  1009. else
  1010. ctx->Yi.d[3] = ctr;
  1011. for (i = 0; i < 16 / sizeof(size_t); ++i)
  1012. out_t[i] = in_t[i] ^ ctx->EKi.t[i];
  1013. out += 16;
  1014. in += 16;
  1015. j -= 16;
  1016. }
  1017. len -= GHASH_CHUNK;
  1018. }
  1019. # endif
  1020. if ((i = (len & (size_t)-16))) {
  1021. GHASH(ctx, in, i);
  1022. while (len >= 16) {
  1023. size_t_aX *out_t = (size_t_aX *)out;
  1024. const size_t_aX *in_t = (const size_t_aX *)in;
  1025. (*block) (ctx->Yi.c, ctx->EKi.c, key);
  1026. ++ctr;
  1027. if (IS_LITTLE_ENDIAN)
  1028. # ifdef BSWAP4
  1029. ctx->Yi.d[3] = BSWAP4(ctr);
  1030. # else
  1031. PUTU32(ctx->Yi.c + 12, ctr);
  1032. # endif
  1033. else
  1034. ctx->Yi.d[3] = ctr;
  1035. for (i = 0; i < 16 / sizeof(size_t); ++i)
  1036. out_t[i] = in_t[i] ^ ctx->EKi.t[i];
  1037. out += 16;
  1038. in += 16;
  1039. len -= 16;
  1040. }
  1041. }
  1042. # else
  1043. while (len >= 16) {
  1044. size_t *out_t = (size_t *)out;
  1045. const size_t *in_t = (const size_t *)in;
  1046. (*block) (ctx->Yi.c, ctx->EKi.c, key);
  1047. ++ctr;
  1048. if (IS_LITTLE_ENDIAN)
  1049. # ifdef BSWAP4
  1050. ctx->Yi.d[3] = BSWAP4(ctr);
  1051. # else
  1052. PUTU32(ctx->Yi.c + 12, ctr);
  1053. # endif
  1054. else
  1055. ctx->Yi.d[3] = ctr;
  1056. for (i = 0; i < 16 / sizeof(size_t); ++i) {
  1057. size_t c = in_t[i];
  1058. out_t[i] = c ^ ctx->EKi.t[i];
  1059. ctx->Xi.t[i] ^= c;
  1060. }
  1061. GCM_MUL(ctx);
  1062. out += 16;
  1063. in += 16;
  1064. len -= 16;
  1065. }
  1066. # endif
  1067. if (len) {
  1068. (*block) (ctx->Yi.c, ctx->EKi.c, key);
  1069. ++ctr;
  1070. if (IS_LITTLE_ENDIAN)
  1071. # ifdef BSWAP4
  1072. ctx->Yi.d[3] = BSWAP4(ctr);
  1073. # else
  1074. PUTU32(ctx->Yi.c + 12, ctr);
  1075. # endif
  1076. else
  1077. ctx->Yi.d[3] = ctr;
  1078. # if defined(GHASH)
  1079. while (len--) {
  1080. out[n] = (ctx->Xn[mres++] = in[n]) ^ ctx->EKi.c[n];
  1081. ++n;
  1082. }
  1083. # else
  1084. while (len--) {
  1085. u8 c = in[n];
  1086. ctx->Xi.c[n] ^= c;
  1087. out[n] = c ^ ctx->EKi.c[n];
  1088. ++n;
  1089. }
  1090. mres = n;
  1091. # endif
  1092. }
  1093. ctx->mres = mres;
  1094. return 0;
  1095. } while (0);
  1096. }
  1097. #endif
  1098. for (i = 0; i < len; ++i) {
  1099. u8 c;
  1100. if (n == 0) {
  1101. (*block) (ctx->Yi.c, ctx->EKi.c, key);
  1102. ++ctr;
  1103. if (IS_LITTLE_ENDIAN)
  1104. #ifdef BSWAP4
  1105. ctx->Yi.d[3] = BSWAP4(ctr);
  1106. #else
  1107. PUTU32(ctx->Yi.c + 12, ctr);
  1108. #endif
  1109. else
  1110. ctx->Yi.d[3] = ctr;
  1111. }
  1112. #if defined(GHASH) && !defined(OPENSSL_SMALL_FOOTPRINT)
  1113. out[i] = (ctx->Xn[mres++] = c = in[i]) ^ ctx->EKi.c[n];
  1114. n = (n + 1) % 16;
  1115. if (mres == sizeof(ctx->Xn)) {
  1116. GHASH(ctx,ctx->Xn,sizeof(ctx->Xn));
  1117. mres = 0;
  1118. }
  1119. #else
  1120. c = in[i];
  1121. out[i] = c ^ ctx->EKi.c[n];
  1122. ctx->Xi.c[n] ^= c;
  1123. mres = n = (n + 1) % 16;
  1124. if (n == 0)
  1125. GCM_MUL(ctx);
  1126. #endif
  1127. }
  1128. ctx->mres = mres;
  1129. return 0;
  1130. }
  1131. int CRYPTO_gcm128_encrypt_ctr32(GCM128_CONTEXT *ctx,
  1132. const unsigned char *in, unsigned char *out,
  1133. size_t len, ctr128_f stream)
  1134. {
  1135. #if defined(OPENSSL_SMALL_FOOTPRINT)
  1136. return CRYPTO_gcm128_encrypt(ctx, in, out, len);
  1137. #else
  1138. DECLARE_IS_ENDIAN;
  1139. unsigned int n, ctr, mres;
  1140. size_t i;
  1141. u64 mlen = ctx->len.u[1];
  1142. void *key = ctx->key;
  1143. mlen += len;
  1144. if (mlen > ((U64(1) << 36) - 32) || (sizeof(len) == 8 && mlen < len))
  1145. return -1;
  1146. ctx->len.u[1] = mlen;
  1147. mres = ctx->mres;
  1148. if (ctx->ares) {
  1149. /* First call to encrypt finalizes GHASH(AAD) */
  1150. #if defined(GHASH)
  1151. if (len == 0) {
  1152. GCM_MUL(ctx);
  1153. ctx->ares = 0;
  1154. return 0;
  1155. }
  1156. memcpy(ctx->Xn, ctx->Xi.c, sizeof(ctx->Xi));
  1157. ctx->Xi.u[0] = 0;
  1158. ctx->Xi.u[1] = 0;
  1159. mres = sizeof(ctx->Xi);
  1160. #else
  1161. GCM_MUL(ctx);
  1162. #endif
  1163. ctx->ares = 0;
  1164. }
  1165. if (IS_LITTLE_ENDIAN)
  1166. # ifdef BSWAP4
  1167. ctr = BSWAP4(ctx->Yi.d[3]);
  1168. # else
  1169. ctr = GETU32(ctx->Yi.c + 12);
  1170. # endif
  1171. else
  1172. ctr = ctx->Yi.d[3];
  1173. n = mres % 16;
  1174. if (n) {
  1175. # if defined(GHASH)
  1176. while (n && len) {
  1177. ctx->Xn[mres++] = *(out++) = *(in++) ^ ctx->EKi.c[n];
  1178. --len;
  1179. n = (n + 1) % 16;
  1180. }
  1181. if (n == 0) {
  1182. GHASH(ctx, ctx->Xn, mres);
  1183. mres = 0;
  1184. } else {
  1185. ctx->mres = mres;
  1186. return 0;
  1187. }
  1188. # else
  1189. while (n && len) {
  1190. ctx->Xi.c[n] ^= *(out++) = *(in++) ^ ctx->EKi.c[n];
  1191. --len;
  1192. n = (n + 1) % 16;
  1193. }
  1194. if (n == 0) {
  1195. GCM_MUL(ctx);
  1196. mres = 0;
  1197. } else {
  1198. ctx->mres = n;
  1199. return 0;
  1200. }
  1201. # endif
  1202. }
  1203. # if defined(GHASH)
  1204. if (len >= 16 && mres) {
  1205. GHASH(ctx, ctx->Xn, mres);
  1206. mres = 0;
  1207. }
  1208. # if defined(GHASH_CHUNK)
  1209. while (len >= GHASH_CHUNK) {
  1210. (*stream) (in, out, GHASH_CHUNK / 16, key, ctx->Yi.c);
  1211. ctr += GHASH_CHUNK / 16;
  1212. if (IS_LITTLE_ENDIAN)
  1213. # ifdef BSWAP4
  1214. ctx->Yi.d[3] = BSWAP4(ctr);
  1215. # else
  1216. PUTU32(ctx->Yi.c + 12, ctr);
  1217. # endif
  1218. else
  1219. ctx->Yi.d[3] = ctr;
  1220. GHASH(ctx, out, GHASH_CHUNK);
  1221. out += GHASH_CHUNK;
  1222. in += GHASH_CHUNK;
  1223. len -= GHASH_CHUNK;
  1224. }
  1225. # endif
  1226. # endif
  1227. if ((i = (len & (size_t)-16))) {
  1228. size_t j = i / 16;
  1229. (*stream) (in, out, j, key, ctx->Yi.c);
  1230. ctr += (unsigned int)j;
  1231. if (IS_LITTLE_ENDIAN)
  1232. # ifdef BSWAP4
  1233. ctx->Yi.d[3] = BSWAP4(ctr);
  1234. # else
  1235. PUTU32(ctx->Yi.c + 12, ctr);
  1236. # endif
  1237. else
  1238. ctx->Yi.d[3] = ctr;
  1239. in += i;
  1240. len -= i;
  1241. # if defined(GHASH)
  1242. GHASH(ctx, out, i);
  1243. out += i;
  1244. # else
  1245. while (j--) {
  1246. for (i = 0; i < 16; ++i)
  1247. ctx->Xi.c[i] ^= out[i];
  1248. GCM_MUL(ctx);
  1249. out += 16;
  1250. }
  1251. # endif
  1252. }
  1253. if (len) {
  1254. (*ctx->block) (ctx->Yi.c, ctx->EKi.c, key);
  1255. ++ctr;
  1256. if (IS_LITTLE_ENDIAN)
  1257. # ifdef BSWAP4
  1258. ctx->Yi.d[3] = BSWAP4(ctr);
  1259. # else
  1260. PUTU32(ctx->Yi.c + 12, ctr);
  1261. # endif
  1262. else
  1263. ctx->Yi.d[3] = ctr;
  1264. while (len--) {
  1265. # if defined(GHASH)
  1266. ctx->Xn[mres++] = out[n] = in[n] ^ ctx->EKi.c[n];
  1267. # else
  1268. ctx->Xi.c[mres++] ^= out[n] = in[n] ^ ctx->EKi.c[n];
  1269. # endif
  1270. ++n;
  1271. }
  1272. }
  1273. ctx->mres = mres;
  1274. return 0;
  1275. #endif
  1276. }
  1277. int CRYPTO_gcm128_decrypt_ctr32(GCM128_CONTEXT *ctx,
  1278. const unsigned char *in, unsigned char *out,
  1279. size_t len, ctr128_f stream)
  1280. {
  1281. #if defined(OPENSSL_SMALL_FOOTPRINT)
  1282. return CRYPTO_gcm128_decrypt(ctx, in, out, len);
  1283. #else
  1284. DECLARE_IS_ENDIAN;
  1285. unsigned int n, ctr, mres;
  1286. size_t i;
  1287. u64 mlen = ctx->len.u[1];
  1288. void *key = ctx->key;
  1289. mlen += len;
  1290. if (mlen > ((U64(1) << 36) - 32) || (sizeof(len) == 8 && mlen < len))
  1291. return -1;
  1292. ctx->len.u[1] = mlen;
  1293. mres = ctx->mres;
  1294. if (ctx->ares) {
  1295. /* First call to decrypt finalizes GHASH(AAD) */
  1296. # if defined(GHASH)
  1297. if (len == 0) {
  1298. GCM_MUL(ctx);
  1299. ctx->ares = 0;
  1300. return 0;
  1301. }
  1302. memcpy(ctx->Xn, ctx->Xi.c, sizeof(ctx->Xi));
  1303. ctx->Xi.u[0] = 0;
  1304. ctx->Xi.u[1] = 0;
  1305. mres = sizeof(ctx->Xi);
  1306. # else
  1307. GCM_MUL(ctx);
  1308. # endif
  1309. ctx->ares = 0;
  1310. }
  1311. if (IS_LITTLE_ENDIAN)
  1312. # ifdef BSWAP4
  1313. ctr = BSWAP4(ctx->Yi.d[3]);
  1314. # else
  1315. ctr = GETU32(ctx->Yi.c + 12);
  1316. # endif
  1317. else
  1318. ctr = ctx->Yi.d[3];
  1319. n = mres % 16;
  1320. if (n) {
  1321. # if defined(GHASH)
  1322. while (n && len) {
  1323. *(out++) = (ctx->Xn[mres++] = *(in++)) ^ ctx->EKi.c[n];
  1324. --len;
  1325. n = (n + 1) % 16;
  1326. }
  1327. if (n == 0) {
  1328. GHASH(ctx, ctx->Xn, mres);
  1329. mres = 0;
  1330. } else {
  1331. ctx->mres = mres;
  1332. return 0;
  1333. }
  1334. # else
  1335. while (n && len) {
  1336. u8 c = *(in++);
  1337. *(out++) = c ^ ctx->EKi.c[n];
  1338. ctx->Xi.c[n] ^= c;
  1339. --len;
  1340. n = (n + 1) % 16;
  1341. }
  1342. if (n == 0) {
  1343. GCM_MUL(ctx);
  1344. mres = 0;
  1345. } else {
  1346. ctx->mres = n;
  1347. return 0;
  1348. }
  1349. # endif
  1350. }
  1351. # if defined(GHASH)
  1352. if (len >= 16 && mres) {
  1353. GHASH(ctx, ctx->Xn, mres);
  1354. mres = 0;
  1355. }
  1356. # if defined(GHASH_CHUNK)
  1357. while (len >= GHASH_CHUNK) {
  1358. GHASH(ctx, in, GHASH_CHUNK);
  1359. (*stream) (in, out, GHASH_CHUNK / 16, key, ctx->Yi.c);
  1360. ctr += GHASH_CHUNK / 16;
  1361. if (IS_LITTLE_ENDIAN)
  1362. # ifdef BSWAP4
  1363. ctx->Yi.d[3] = BSWAP4(ctr);
  1364. # else
  1365. PUTU32(ctx->Yi.c + 12, ctr);
  1366. # endif
  1367. else
  1368. ctx->Yi.d[3] = ctr;
  1369. out += GHASH_CHUNK;
  1370. in += GHASH_CHUNK;
  1371. len -= GHASH_CHUNK;
  1372. }
  1373. # endif
  1374. # endif
  1375. if ((i = (len & (size_t)-16))) {
  1376. size_t j = i / 16;
  1377. # if defined(GHASH)
  1378. GHASH(ctx, in, i);
  1379. # else
  1380. while (j--) {
  1381. size_t k;
  1382. for (k = 0; k < 16; ++k)
  1383. ctx->Xi.c[k] ^= in[k];
  1384. GCM_MUL(ctx);
  1385. in += 16;
  1386. }
  1387. j = i / 16;
  1388. in -= i;
  1389. # endif
  1390. (*stream) (in, out, j, key, ctx->Yi.c);
  1391. ctr += (unsigned int)j;
  1392. if (IS_LITTLE_ENDIAN)
  1393. # ifdef BSWAP4
  1394. ctx->Yi.d[3] = BSWAP4(ctr);
  1395. # else
  1396. PUTU32(ctx->Yi.c + 12, ctr);
  1397. # endif
  1398. else
  1399. ctx->Yi.d[3] = ctr;
  1400. out += i;
  1401. in += i;
  1402. len -= i;
  1403. }
  1404. if (len) {
  1405. (*ctx->block) (ctx->Yi.c, ctx->EKi.c, key);
  1406. ++ctr;
  1407. if (IS_LITTLE_ENDIAN)
  1408. # ifdef BSWAP4
  1409. ctx->Yi.d[3] = BSWAP4(ctr);
  1410. # else
  1411. PUTU32(ctx->Yi.c + 12, ctr);
  1412. # endif
  1413. else
  1414. ctx->Yi.d[3] = ctr;
  1415. while (len--) {
  1416. # if defined(GHASH)
  1417. out[n] = (ctx->Xn[mres++] = in[n]) ^ ctx->EKi.c[n];
  1418. # else
  1419. u8 c = in[n];
  1420. ctx->Xi.c[mres++] ^= c;
  1421. out[n] = c ^ ctx->EKi.c[n];
  1422. # endif
  1423. ++n;
  1424. }
  1425. }
  1426. ctx->mres = mres;
  1427. return 0;
  1428. #endif
  1429. }
  1430. int CRYPTO_gcm128_finish(GCM128_CONTEXT *ctx, const unsigned char *tag,
  1431. size_t len)
  1432. {
  1433. DECLARE_IS_ENDIAN;
  1434. u64 alen = ctx->len.u[0] << 3;
  1435. u64 clen = ctx->len.u[1] << 3;
  1436. #if defined(GHASH) && !defined(OPENSSL_SMALL_FOOTPRINT)
  1437. u128 bitlen;
  1438. unsigned int mres = ctx->mres;
  1439. if (mres) {
  1440. unsigned blocks = (mres + 15) & -16;
  1441. memset(ctx->Xn + mres, 0, blocks - mres);
  1442. mres = blocks;
  1443. if (mres == sizeof(ctx->Xn)) {
  1444. GHASH(ctx, ctx->Xn, mres);
  1445. mres = 0;
  1446. }
  1447. } else if (ctx->ares) {
  1448. GCM_MUL(ctx);
  1449. }
  1450. #else
  1451. if (ctx->mres || ctx->ares)
  1452. GCM_MUL(ctx);
  1453. #endif
  1454. if (IS_LITTLE_ENDIAN) {
  1455. #ifdef BSWAP8
  1456. alen = BSWAP8(alen);
  1457. clen = BSWAP8(clen);
  1458. #else
  1459. u8 *p = ctx->len.c;
  1460. ctx->len.u[0] = alen;
  1461. ctx->len.u[1] = clen;
  1462. alen = (u64)GETU32(p) << 32 | GETU32(p + 4);
  1463. clen = (u64)GETU32(p + 8) << 32 | GETU32(p + 12);
  1464. #endif
  1465. }
  1466. #if defined(GHASH) && !defined(OPENSSL_SMALL_FOOTPRINT)
  1467. bitlen.hi = alen;
  1468. bitlen.lo = clen;
  1469. memcpy(ctx->Xn + mres, &bitlen, sizeof(bitlen));
  1470. mres += sizeof(bitlen);
  1471. GHASH(ctx, ctx->Xn, mres);
  1472. #else
  1473. ctx->Xi.u[0] ^= alen;
  1474. ctx->Xi.u[1] ^= clen;
  1475. GCM_MUL(ctx);
  1476. #endif
  1477. ctx->Xi.u[0] ^= ctx->EK0.u[0];
  1478. ctx->Xi.u[1] ^= ctx->EK0.u[1];
  1479. if (tag && len <= sizeof(ctx->Xi))
  1480. return CRYPTO_memcmp(ctx->Xi.c, tag, len);
  1481. else
  1482. return -1;
  1483. }
  1484. void CRYPTO_gcm128_tag(GCM128_CONTEXT *ctx, unsigned char *tag, size_t len)
  1485. {
  1486. CRYPTO_gcm128_finish(ctx, NULL, 0);
  1487. memcpy(tag, ctx->Xi.c,
  1488. len <= sizeof(ctx->Xi.c) ? len : sizeof(ctx->Xi.c));
  1489. }
  1490. GCM128_CONTEXT *CRYPTO_gcm128_new(void *key, block128_f block)
  1491. {
  1492. GCM128_CONTEXT *ret;
  1493. if ((ret = OPENSSL_malloc(sizeof(*ret))) != NULL)
  1494. CRYPTO_gcm128_init(ret, key, block);
  1495. return ret;
  1496. }
  1497. void CRYPTO_gcm128_release(GCM128_CONTEXT *ctx)
  1498. {
  1499. OPENSSL_clear_free(ctx, sizeof(*ctx));
  1500. }