2
0

gcm128.c 44 KB

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