gcm128.c 45 KB

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