gcm128.c 43 KB

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