gcm128.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894
  1. /*
  2. * Copyright 2010-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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 <openssl/crypto.h>
  10. #include "modes_local.h"
  11. #include <string.h>
  12. #if defined(__GNUC__) && !defined(STRICT_ALIGNMENT)
  13. typedef size_t size_t_aX __attribute((__aligned__(1)));
  14. #else
  15. typedef size_t size_t_aX;
  16. #endif
  17. #if defined(BSWAP4) && defined(STRICT_ALIGNMENT)
  18. /* redefine, because alignment is ensured */
  19. # undef GETU32
  20. # define GETU32(p) BSWAP4(*(const u32 *)(p))
  21. # undef PUTU32
  22. # define PUTU32(p,v) *(u32 *)(p) = BSWAP4(v)
  23. #endif
  24. #define PACK(s) ((size_t)(s)<<(sizeof(size_t)*8-16))
  25. #define REDUCE1BIT(V) do { \
  26. if (sizeof(size_t)==8) { \
  27. u64 T = U64(0xe100000000000000) & (0-(V.lo&1)); \
  28. V.lo = (V.hi<<63)|(V.lo>>1); \
  29. V.hi = (V.hi>>1 )^T; \
  30. } \
  31. else { \
  32. u32 T = 0xe1000000U & (0-(u32)(V.lo&1)); \
  33. V.lo = (V.hi<<63)|(V.lo>>1); \
  34. V.hi = (V.hi>>1 )^((u64)T<<32); \
  35. } \
  36. } while(0)
  37. /*-
  38. * Even though permitted values for TABLE_BITS are 8, 4 and 1, it should
  39. * never be set to 8. 8 is effectively reserved for testing purposes.
  40. * TABLE_BITS>1 are lookup-table-driven implementations referred to as
  41. * "Shoup's" in GCM specification. In other words OpenSSL does not cover
  42. * whole spectrum of possible table driven implementations. Why? In
  43. * non-"Shoup's" case memory access pattern is segmented in such manner,
  44. * that it's trivial to see that cache timing information can reveal
  45. * fair portion of intermediate hash value. Given that ciphertext is
  46. * always available to attacker, it's possible for him to attempt to
  47. * deduce secret parameter H and if successful, tamper with messages
  48. * [which is nothing but trivial in CTR mode]. In "Shoup's" case it's
  49. * not as trivial, but there is no reason to believe that it's resistant
  50. * to cache-timing attack. And the thing about "8-bit" implementation is
  51. * that it consumes 16 (sixteen) times more memory, 4KB per individual
  52. * key + 1KB shared. Well, on pros side it should be twice as fast as
  53. * "4-bit" version. And for gcc-generated x86[_64] code, "8-bit" version
  54. * was observed to run ~75% faster, closer to 100% for commercial
  55. * compilers... Yet "4-bit" procedure is preferred, because it's
  56. * believed to provide better security-performance balance and adequate
  57. * all-round performance. "All-round" refers to things like:
  58. *
  59. * - shorter setup time effectively improves overall timing for
  60. * handling short messages;
  61. * - larger table allocation can become unbearable because of VM
  62. * subsystem penalties (for example on Windows large enough free
  63. * results in VM working set trimming, meaning that consequent
  64. * malloc would immediately incur working set expansion);
  65. * - larger table has larger cache footprint, which can affect
  66. * performance of other code paths (not necessarily even from same
  67. * thread in Hyper-Threading world);
  68. *
  69. * Value of 1 is not appropriate for performance reasons.
  70. */
  71. #if TABLE_BITS==8
  72. static void gcm_init_8bit(u128 Htable[256], u64 H[2])
  73. {
  74. int i, j;
  75. u128 V;
  76. Htable[0].hi = 0;
  77. Htable[0].lo = 0;
  78. V.hi = H[0];
  79. V.lo = H[1];
  80. for (Htable[128] = V, i = 64; i > 0; i >>= 1) {
  81. REDUCE1BIT(V);
  82. Htable[i] = V;
  83. }
  84. for (i = 2; i < 256; i <<= 1) {
  85. u128 *Hi = Htable + i, H0 = *Hi;
  86. for (j = 1; j < i; ++j) {
  87. Hi[j].hi = H0.hi ^ Htable[j].hi;
  88. Hi[j].lo = H0.lo ^ Htable[j].lo;
  89. }
  90. }
  91. }
  92. static void gcm_gmult_8bit(u64 Xi[2], const u128 Htable[256])
  93. {
  94. u128 Z = { 0, 0 };
  95. const u8 *xi = (const u8 *)Xi + 15;
  96. size_t rem, n = *xi;
  97. const union {
  98. long one;
  99. char little;
  100. } is_endian = { 1 };
  101. static const size_t rem_8bit[256] = {
  102. PACK(0x0000), PACK(0x01C2), PACK(0x0384), PACK(0x0246),
  103. PACK(0x0708), PACK(0x06CA), PACK(0x048C), PACK(0x054E),
  104. PACK(0x0E10), PACK(0x0FD2), PACK(0x0D94), PACK(0x0C56),
  105. PACK(0x0918), PACK(0x08DA), PACK(0x0A9C), PACK(0x0B5E),
  106. PACK(0x1C20), PACK(0x1DE2), PACK(0x1FA4), PACK(0x1E66),
  107. PACK(0x1B28), PACK(0x1AEA), PACK(0x18AC), PACK(0x196E),
  108. PACK(0x1230), PACK(0x13F2), PACK(0x11B4), PACK(0x1076),
  109. PACK(0x1538), PACK(0x14FA), PACK(0x16BC), PACK(0x177E),
  110. PACK(0x3840), PACK(0x3982), PACK(0x3BC4), PACK(0x3A06),
  111. PACK(0x3F48), PACK(0x3E8A), PACK(0x3CCC), PACK(0x3D0E),
  112. PACK(0x3650), PACK(0x3792), PACK(0x35D4), PACK(0x3416),
  113. PACK(0x3158), PACK(0x309A), PACK(0x32DC), PACK(0x331E),
  114. PACK(0x2460), PACK(0x25A2), PACK(0x27E4), PACK(0x2626),
  115. PACK(0x2368), PACK(0x22AA), PACK(0x20EC), PACK(0x212E),
  116. PACK(0x2A70), PACK(0x2BB2), PACK(0x29F4), PACK(0x2836),
  117. PACK(0x2D78), PACK(0x2CBA), PACK(0x2EFC), PACK(0x2F3E),
  118. PACK(0x7080), PACK(0x7142), PACK(0x7304), PACK(0x72C6),
  119. PACK(0x7788), PACK(0x764A), PACK(0x740C), PACK(0x75CE),
  120. PACK(0x7E90), PACK(0x7F52), PACK(0x7D14), PACK(0x7CD6),
  121. PACK(0x7998), PACK(0x785A), PACK(0x7A1C), PACK(0x7BDE),
  122. PACK(0x6CA0), PACK(0x6D62), PACK(0x6F24), PACK(0x6EE6),
  123. PACK(0x6BA8), PACK(0x6A6A), PACK(0x682C), PACK(0x69EE),
  124. PACK(0x62B0), PACK(0x6372), PACK(0x6134), PACK(0x60F6),
  125. PACK(0x65B8), PACK(0x647A), PACK(0x663C), PACK(0x67FE),
  126. PACK(0x48C0), PACK(0x4902), PACK(0x4B44), PACK(0x4A86),
  127. PACK(0x4FC8), PACK(0x4E0A), PACK(0x4C4C), PACK(0x4D8E),
  128. PACK(0x46D0), PACK(0x4712), PACK(0x4554), PACK(0x4496),
  129. PACK(0x41D8), PACK(0x401A), PACK(0x425C), PACK(0x439E),
  130. PACK(0x54E0), PACK(0x5522), PACK(0x5764), PACK(0x56A6),
  131. PACK(0x53E8), PACK(0x522A), PACK(0x506C), PACK(0x51AE),
  132. PACK(0x5AF0), PACK(0x5B32), PACK(0x5974), PACK(0x58B6),
  133. PACK(0x5DF8), PACK(0x5C3A), PACK(0x5E7C), PACK(0x5FBE),
  134. PACK(0xE100), PACK(0xE0C2), PACK(0xE284), PACK(0xE346),
  135. PACK(0xE608), PACK(0xE7CA), PACK(0xE58C), PACK(0xE44E),
  136. PACK(0xEF10), PACK(0xEED2), PACK(0xEC94), PACK(0xED56),
  137. PACK(0xE818), PACK(0xE9DA), PACK(0xEB9C), PACK(0xEA5E),
  138. PACK(0xFD20), PACK(0xFCE2), PACK(0xFEA4), PACK(0xFF66),
  139. PACK(0xFA28), PACK(0xFBEA), PACK(0xF9AC), PACK(0xF86E),
  140. PACK(0xF330), PACK(0xF2F2), PACK(0xF0B4), PACK(0xF176),
  141. PACK(0xF438), PACK(0xF5FA), PACK(0xF7BC), PACK(0xF67E),
  142. PACK(0xD940), PACK(0xD882), PACK(0xDAC4), PACK(0xDB06),
  143. PACK(0xDE48), PACK(0xDF8A), PACK(0xDDCC), PACK(0xDC0E),
  144. PACK(0xD750), PACK(0xD692), PACK(0xD4D4), PACK(0xD516),
  145. PACK(0xD058), PACK(0xD19A), PACK(0xD3DC), PACK(0xD21E),
  146. PACK(0xC560), PACK(0xC4A2), PACK(0xC6E4), PACK(0xC726),
  147. PACK(0xC268), PACK(0xC3AA), PACK(0xC1EC), PACK(0xC02E),
  148. PACK(0xCB70), PACK(0xCAB2), PACK(0xC8F4), PACK(0xC936),
  149. PACK(0xCC78), PACK(0xCDBA), PACK(0xCFFC), PACK(0xCE3E),
  150. PACK(0x9180), PACK(0x9042), PACK(0x9204), PACK(0x93C6),
  151. PACK(0x9688), PACK(0x974A), PACK(0x950C), PACK(0x94CE),
  152. PACK(0x9F90), PACK(0x9E52), PACK(0x9C14), PACK(0x9DD6),
  153. PACK(0x9898), PACK(0x995A), PACK(0x9B1C), PACK(0x9ADE),
  154. PACK(0x8DA0), PACK(0x8C62), PACK(0x8E24), PACK(0x8FE6),
  155. PACK(0x8AA8), PACK(0x8B6A), PACK(0x892C), PACK(0x88EE),
  156. PACK(0x83B0), PACK(0x8272), PACK(0x8034), PACK(0x81F6),
  157. PACK(0x84B8), PACK(0x857A), PACK(0x873C), PACK(0x86FE),
  158. PACK(0xA9C0), PACK(0xA802), PACK(0xAA44), PACK(0xAB86),
  159. PACK(0xAEC8), PACK(0xAF0A), PACK(0xAD4C), PACK(0xAC8E),
  160. PACK(0xA7D0), PACK(0xA612), PACK(0xA454), PACK(0xA596),
  161. PACK(0xA0D8), PACK(0xA11A), PACK(0xA35C), PACK(0xA29E),
  162. PACK(0xB5E0), PACK(0xB422), PACK(0xB664), PACK(0xB7A6),
  163. PACK(0xB2E8), PACK(0xB32A), PACK(0xB16C), PACK(0xB0AE),
  164. PACK(0xBBF0), PACK(0xBA32), PACK(0xB874), PACK(0xB9B6),
  165. PACK(0xBCF8), PACK(0xBD3A), PACK(0xBF7C), PACK(0xBEBE)
  166. };
  167. while (1) {
  168. Z.hi ^= Htable[n].hi;
  169. Z.lo ^= Htable[n].lo;
  170. if ((u8 *)Xi == xi)
  171. break;
  172. n = *(--xi);
  173. rem = (size_t)Z.lo & 0xff;
  174. Z.lo = (Z.hi << 56) | (Z.lo >> 8);
  175. Z.hi = (Z.hi >> 8);
  176. if (sizeof(size_t) == 8)
  177. Z.hi ^= rem_8bit[rem];
  178. else
  179. Z.hi ^= (u64)rem_8bit[rem] << 32;
  180. }
  181. if (is_endian.little) {
  182. # ifdef BSWAP8
  183. Xi[0] = BSWAP8(Z.hi);
  184. Xi[1] = BSWAP8(Z.lo);
  185. # else
  186. u8 *p = (u8 *)Xi;
  187. u32 v;
  188. v = (u32)(Z.hi >> 32);
  189. PUTU32(p, v);
  190. v = (u32)(Z.hi);
  191. PUTU32(p + 4, v);
  192. v = (u32)(Z.lo >> 32);
  193. PUTU32(p + 8, v);
  194. v = (u32)(Z.lo);
  195. PUTU32(p + 12, v);
  196. # endif
  197. } else {
  198. Xi[0] = Z.hi;
  199. Xi[1] = Z.lo;
  200. }
  201. }
  202. # define GCM_MUL(ctx) gcm_gmult_8bit(ctx->Xi.u,ctx->Htable)
  203. #elif TABLE_BITS==4
  204. static void gcm_init_4bit(u128 Htable[16], u64 H[2])
  205. {
  206. u128 V;
  207. # if defined(OPENSSL_SMALL_FOOTPRINT)
  208. int i;
  209. # endif
  210. Htable[0].hi = 0;
  211. Htable[0].lo = 0;
  212. V.hi = H[0];
  213. V.lo = H[1];
  214. # if defined(OPENSSL_SMALL_FOOTPRINT)
  215. for (Htable[8] = V, i = 4; i > 0; i >>= 1) {
  216. REDUCE1BIT(V);
  217. Htable[i] = V;
  218. }
  219. for (i = 2; i < 16; i <<= 1) {
  220. u128 *Hi = Htable + i;
  221. int j;
  222. for (V = *Hi, j = 1; j < i; ++j) {
  223. Hi[j].hi = V.hi ^ Htable[j].hi;
  224. Hi[j].lo = V.lo ^ Htable[j].lo;
  225. }
  226. }
  227. # else
  228. Htable[8] = V;
  229. REDUCE1BIT(V);
  230. Htable[4] = V;
  231. REDUCE1BIT(V);
  232. Htable[2] = V;
  233. REDUCE1BIT(V);
  234. Htable[1] = V;
  235. Htable[3].hi = V.hi ^ Htable[2].hi, Htable[3].lo = V.lo ^ Htable[2].lo;
  236. V = Htable[4];
  237. Htable[5].hi = V.hi ^ Htable[1].hi, Htable[5].lo = V.lo ^ Htable[1].lo;
  238. Htable[6].hi = V.hi ^ Htable[2].hi, Htable[6].lo = V.lo ^ Htable[2].lo;
  239. Htable[7].hi = V.hi ^ Htable[3].hi, Htable[7].lo = V.lo ^ Htable[3].lo;
  240. V = Htable[8];
  241. Htable[9].hi = V.hi ^ Htable[1].hi, Htable[9].lo = V.lo ^ Htable[1].lo;
  242. Htable[10].hi = V.hi ^ Htable[2].hi, Htable[10].lo = V.lo ^ Htable[2].lo;
  243. Htable[11].hi = V.hi ^ Htable[3].hi, Htable[11].lo = V.lo ^ Htable[3].lo;
  244. Htable[12].hi = V.hi ^ Htable[4].hi, Htable[12].lo = V.lo ^ Htable[4].lo;
  245. Htable[13].hi = V.hi ^ Htable[5].hi, Htable[13].lo = V.lo ^ Htable[5].lo;
  246. Htable[14].hi = V.hi ^ Htable[6].hi, Htable[14].lo = V.lo ^ Htable[6].lo;
  247. Htable[15].hi = V.hi ^ Htable[7].hi, Htable[15].lo = V.lo ^ Htable[7].lo;
  248. # endif
  249. # if defined(GHASH_ASM) && (defined(__arm__) || defined(__arm))
  250. /*
  251. * ARM assembler expects specific dword order in Htable.
  252. */
  253. {
  254. int j;
  255. const union {
  256. long one;
  257. char little;
  258. } is_endian = { 1 };
  259. if (is_endian.little)
  260. for (j = 0; j < 16; ++j) {
  261. V = Htable[j];
  262. Htable[j].hi = V.lo;
  263. Htable[j].lo = V.hi;
  264. } else
  265. for (j = 0; j < 16; ++j) {
  266. V = Htable[j];
  267. Htable[j].hi = V.lo << 32 | V.lo >> 32;
  268. Htable[j].lo = V.hi << 32 | V.hi >> 32;
  269. }
  270. }
  271. # endif
  272. }
  273. # ifndef GHASH_ASM
  274. static const size_t rem_4bit[16] = {
  275. PACK(0x0000), PACK(0x1C20), PACK(0x3840), PACK(0x2460),
  276. PACK(0x7080), PACK(0x6CA0), PACK(0x48C0), PACK(0x54E0),
  277. PACK(0xE100), PACK(0xFD20), PACK(0xD940), PACK(0xC560),
  278. PACK(0x9180), PACK(0x8DA0), PACK(0xA9C0), PACK(0xB5E0)
  279. };
  280. static void gcm_gmult_4bit(u64 Xi[2], const u128 Htable[16])
  281. {
  282. u128 Z;
  283. int cnt = 15;
  284. size_t rem, nlo, nhi;
  285. const union {
  286. long one;
  287. char little;
  288. } is_endian = { 1 };
  289. nlo = ((const u8 *)Xi)[15];
  290. nhi = nlo >> 4;
  291. nlo &= 0xf;
  292. Z.hi = Htable[nlo].hi;
  293. Z.lo = Htable[nlo].lo;
  294. while (1) {
  295. rem = (size_t)Z.lo & 0xf;
  296. Z.lo = (Z.hi << 60) | (Z.lo >> 4);
  297. Z.hi = (Z.hi >> 4);
  298. if (sizeof(size_t) == 8)
  299. Z.hi ^= rem_4bit[rem];
  300. else
  301. Z.hi ^= (u64)rem_4bit[rem] << 32;
  302. Z.hi ^= Htable[nhi].hi;
  303. Z.lo ^= Htable[nhi].lo;
  304. if (--cnt < 0)
  305. break;
  306. nlo = ((const u8 *)Xi)[cnt];
  307. nhi = nlo >> 4;
  308. nlo &= 0xf;
  309. rem = (size_t)Z.lo & 0xf;
  310. Z.lo = (Z.hi << 60) | (Z.lo >> 4);
  311. Z.hi = (Z.hi >> 4);
  312. if (sizeof(size_t) == 8)
  313. Z.hi ^= rem_4bit[rem];
  314. else
  315. Z.hi ^= (u64)rem_4bit[rem] << 32;
  316. Z.hi ^= Htable[nlo].hi;
  317. Z.lo ^= Htable[nlo].lo;
  318. }
  319. if (is_endian.little) {
  320. # ifdef BSWAP8
  321. Xi[0] = BSWAP8(Z.hi);
  322. Xi[1] = BSWAP8(Z.lo);
  323. # else
  324. u8 *p = (u8 *)Xi;
  325. u32 v;
  326. v = (u32)(Z.hi >> 32);
  327. PUTU32(p, v);
  328. v = (u32)(Z.hi);
  329. PUTU32(p + 4, v);
  330. v = (u32)(Z.lo >> 32);
  331. PUTU32(p + 8, v);
  332. v = (u32)(Z.lo);
  333. PUTU32(p + 12, v);
  334. # endif
  335. } else {
  336. Xi[0] = Z.hi;
  337. Xi[1] = Z.lo;
  338. }
  339. }
  340. # if !defined(OPENSSL_SMALL_FOOTPRINT)
  341. /*
  342. * Streamed gcm_mult_4bit, see CRYPTO_gcm128_[en|de]crypt for
  343. * details... Compiler-generated code doesn't seem to give any
  344. * performance improvement, at least not on x86[_64]. It's here
  345. * mostly as reference and a placeholder for possible future
  346. * non-trivial optimization[s]...
  347. */
  348. static void gcm_ghash_4bit(u64 Xi[2], const u128 Htable[16],
  349. const u8 *inp, size_t len)
  350. {
  351. u128 Z;
  352. int cnt;
  353. size_t rem, nlo, nhi;
  354. const union {
  355. long one;
  356. char little;
  357. } is_endian = { 1 };
  358. # if 1
  359. do {
  360. cnt = 15;
  361. nlo = ((const u8 *)Xi)[15];
  362. nlo ^= inp[15];
  363. nhi = nlo >> 4;
  364. nlo &= 0xf;
  365. Z.hi = Htable[nlo].hi;
  366. Z.lo = Htable[nlo].lo;
  367. while (1) {
  368. rem = (size_t)Z.lo & 0xf;
  369. Z.lo = (Z.hi << 60) | (Z.lo >> 4);
  370. Z.hi = (Z.hi >> 4);
  371. if (sizeof(size_t) == 8)
  372. Z.hi ^= rem_4bit[rem];
  373. else
  374. Z.hi ^= (u64)rem_4bit[rem] << 32;
  375. Z.hi ^= Htable[nhi].hi;
  376. Z.lo ^= Htable[nhi].lo;
  377. if (--cnt < 0)
  378. break;
  379. nlo = ((const u8 *)Xi)[cnt];
  380. nlo ^= inp[cnt];
  381. nhi = nlo >> 4;
  382. nlo &= 0xf;
  383. rem = (size_t)Z.lo & 0xf;
  384. Z.lo = (Z.hi << 60) | (Z.lo >> 4);
  385. Z.hi = (Z.hi >> 4);
  386. if (sizeof(size_t) == 8)
  387. Z.hi ^= rem_4bit[rem];
  388. else
  389. Z.hi ^= (u64)rem_4bit[rem] << 32;
  390. Z.hi ^= Htable[nlo].hi;
  391. Z.lo ^= Htable[nlo].lo;
  392. }
  393. # else
  394. /*
  395. * Extra 256+16 bytes per-key plus 512 bytes shared tables
  396. * [should] give ~50% improvement... One could have PACK()-ed
  397. * the rem_8bit even here, but the priority is to minimize
  398. * cache footprint...
  399. */
  400. u128 Hshr4[16]; /* Htable shifted right by 4 bits */
  401. u8 Hshl4[16]; /* Htable shifted left by 4 bits */
  402. static const unsigned short rem_8bit[256] = {
  403. 0x0000, 0x01C2, 0x0384, 0x0246, 0x0708, 0x06CA, 0x048C, 0x054E,
  404. 0x0E10, 0x0FD2, 0x0D94, 0x0C56, 0x0918, 0x08DA, 0x0A9C, 0x0B5E,
  405. 0x1C20, 0x1DE2, 0x1FA4, 0x1E66, 0x1B28, 0x1AEA, 0x18AC, 0x196E,
  406. 0x1230, 0x13F2, 0x11B4, 0x1076, 0x1538, 0x14FA, 0x16BC, 0x177E,
  407. 0x3840, 0x3982, 0x3BC4, 0x3A06, 0x3F48, 0x3E8A, 0x3CCC, 0x3D0E,
  408. 0x3650, 0x3792, 0x35D4, 0x3416, 0x3158, 0x309A, 0x32DC, 0x331E,
  409. 0x2460, 0x25A2, 0x27E4, 0x2626, 0x2368, 0x22AA, 0x20EC, 0x212E,
  410. 0x2A70, 0x2BB2, 0x29F4, 0x2836, 0x2D78, 0x2CBA, 0x2EFC, 0x2F3E,
  411. 0x7080, 0x7142, 0x7304, 0x72C6, 0x7788, 0x764A, 0x740C, 0x75CE,
  412. 0x7E90, 0x7F52, 0x7D14, 0x7CD6, 0x7998, 0x785A, 0x7A1C, 0x7BDE,
  413. 0x6CA0, 0x6D62, 0x6F24, 0x6EE6, 0x6BA8, 0x6A6A, 0x682C, 0x69EE,
  414. 0x62B0, 0x6372, 0x6134, 0x60F6, 0x65B8, 0x647A, 0x663C, 0x67FE,
  415. 0x48C0, 0x4902, 0x4B44, 0x4A86, 0x4FC8, 0x4E0A, 0x4C4C, 0x4D8E,
  416. 0x46D0, 0x4712, 0x4554, 0x4496, 0x41D8, 0x401A, 0x425C, 0x439E,
  417. 0x54E0, 0x5522, 0x5764, 0x56A6, 0x53E8, 0x522A, 0x506C, 0x51AE,
  418. 0x5AF0, 0x5B32, 0x5974, 0x58B6, 0x5DF8, 0x5C3A, 0x5E7C, 0x5FBE,
  419. 0xE100, 0xE0C2, 0xE284, 0xE346, 0xE608, 0xE7CA, 0xE58C, 0xE44E,
  420. 0xEF10, 0xEED2, 0xEC94, 0xED56, 0xE818, 0xE9DA, 0xEB9C, 0xEA5E,
  421. 0xFD20, 0xFCE2, 0xFEA4, 0xFF66, 0xFA28, 0xFBEA, 0xF9AC, 0xF86E,
  422. 0xF330, 0xF2F2, 0xF0B4, 0xF176, 0xF438, 0xF5FA, 0xF7BC, 0xF67E,
  423. 0xD940, 0xD882, 0xDAC4, 0xDB06, 0xDE48, 0xDF8A, 0xDDCC, 0xDC0E,
  424. 0xD750, 0xD692, 0xD4D4, 0xD516, 0xD058, 0xD19A, 0xD3DC, 0xD21E,
  425. 0xC560, 0xC4A2, 0xC6E4, 0xC726, 0xC268, 0xC3AA, 0xC1EC, 0xC02E,
  426. 0xCB70, 0xCAB2, 0xC8F4, 0xC936, 0xCC78, 0xCDBA, 0xCFFC, 0xCE3E,
  427. 0x9180, 0x9042, 0x9204, 0x93C6, 0x9688, 0x974A, 0x950C, 0x94CE,
  428. 0x9F90, 0x9E52, 0x9C14, 0x9DD6, 0x9898, 0x995A, 0x9B1C, 0x9ADE,
  429. 0x8DA0, 0x8C62, 0x8E24, 0x8FE6, 0x8AA8, 0x8B6A, 0x892C, 0x88EE,
  430. 0x83B0, 0x8272, 0x8034, 0x81F6, 0x84B8, 0x857A, 0x873C, 0x86FE,
  431. 0xA9C0, 0xA802, 0xAA44, 0xAB86, 0xAEC8, 0xAF0A, 0xAD4C, 0xAC8E,
  432. 0xA7D0, 0xA612, 0xA454, 0xA596, 0xA0D8, 0xA11A, 0xA35C, 0xA29E,
  433. 0xB5E0, 0xB422, 0xB664, 0xB7A6, 0xB2E8, 0xB32A, 0xB16C, 0xB0AE,
  434. 0xBBF0, 0xBA32, 0xB874, 0xB9B6, 0xBCF8, 0xBD3A, 0xBF7C, 0xBEBE
  435. };
  436. /*
  437. * This pre-processing phase slows down procedure by approximately
  438. * same time as it makes each loop spin faster. In other words
  439. * single block performance is approximately same as straightforward
  440. * "4-bit" implementation, and then it goes only faster...
  441. */
  442. for (cnt = 0; cnt < 16; ++cnt) {
  443. Z.hi = Htable[cnt].hi;
  444. Z.lo = Htable[cnt].lo;
  445. Hshr4[cnt].lo = (Z.hi << 60) | (Z.lo >> 4);
  446. Hshr4[cnt].hi = (Z.hi >> 4);
  447. Hshl4[cnt] = (u8)(Z.lo << 4);
  448. }
  449. do {
  450. for (Z.lo = 0, Z.hi = 0, cnt = 15; cnt; --cnt) {
  451. nlo = ((const u8 *)Xi)[cnt];
  452. nlo ^= inp[cnt];
  453. nhi = nlo >> 4;
  454. nlo &= 0xf;
  455. Z.hi ^= Htable[nlo].hi;
  456. Z.lo ^= Htable[nlo].lo;
  457. rem = (size_t)Z.lo & 0xff;
  458. Z.lo = (Z.hi << 56) | (Z.lo >> 8);
  459. Z.hi = (Z.hi >> 8);
  460. Z.hi ^= Hshr4[nhi].hi;
  461. Z.lo ^= Hshr4[nhi].lo;
  462. Z.hi ^= (u64)rem_8bit[rem ^ Hshl4[nhi]] << 48;
  463. }
  464. nlo = ((const u8 *)Xi)[0];
  465. nlo ^= inp[0];
  466. nhi = nlo >> 4;
  467. nlo &= 0xf;
  468. Z.hi ^= Htable[nlo].hi;
  469. Z.lo ^= Htable[nlo].lo;
  470. rem = (size_t)Z.lo & 0xf;
  471. Z.lo = (Z.hi << 60) | (Z.lo >> 4);
  472. Z.hi = (Z.hi >> 4);
  473. Z.hi ^= Htable[nhi].hi;
  474. Z.lo ^= Htable[nhi].lo;
  475. Z.hi ^= ((u64)rem_8bit[rem << 4]) << 48;
  476. # endif
  477. if (is_endian.little) {
  478. # ifdef BSWAP8
  479. Xi[0] = BSWAP8(Z.hi);
  480. Xi[1] = BSWAP8(Z.lo);
  481. # else
  482. u8 *p = (u8 *)Xi;
  483. u32 v;
  484. v = (u32)(Z.hi >> 32);
  485. PUTU32(p, v);
  486. v = (u32)(Z.hi);
  487. PUTU32(p + 4, v);
  488. v = (u32)(Z.lo >> 32);
  489. PUTU32(p + 8, v);
  490. v = (u32)(Z.lo);
  491. PUTU32(p + 12, v);
  492. # endif
  493. } else {
  494. Xi[0] = Z.hi;
  495. Xi[1] = Z.lo;
  496. }
  497. } while (inp += 16, len -= 16);
  498. }
  499. # endif
  500. # else
  501. void gcm_gmult_4bit(u64 Xi[2], const u128 Htable[16]);
  502. void gcm_ghash_4bit(u64 Xi[2], const u128 Htable[16], const u8 *inp,
  503. size_t len);
  504. # endif
  505. # define GCM_MUL(ctx) gcm_gmult_4bit(ctx->Xi.u,ctx->Htable)
  506. # if defined(GHASH_ASM) || !defined(OPENSSL_SMALL_FOOTPRINT)
  507. # define GHASH(ctx,in,len) gcm_ghash_4bit((ctx)->Xi.u,(ctx)->Htable,in,len)
  508. /*
  509. * GHASH_CHUNK is "stride parameter" missioned to mitigate cache trashing
  510. * effect. In other words idea is to hash data while it's still in L1 cache
  511. * after encryption pass...
  512. */
  513. # define GHASH_CHUNK (3*1024)
  514. # endif
  515. #else /* TABLE_BITS */
  516. static void gcm_gmult_1bit(u64 Xi[2], const u64 H[2])
  517. {
  518. u128 V, Z = { 0, 0 };
  519. long X;
  520. int i, j;
  521. const long *xi = (const long *)Xi;
  522. const union {
  523. long one;
  524. char little;
  525. } is_endian = { 1 };
  526. V.hi = H[0]; /* H is in host byte order, no byte swapping */
  527. V.lo = H[1];
  528. for (j = 0; j < 16 / sizeof(long); ++j) {
  529. if (is_endian.little) {
  530. if (sizeof(long) == 8) {
  531. # ifdef BSWAP8
  532. X = (long)(BSWAP8(xi[j]));
  533. # else
  534. const u8 *p = (const u8 *)(xi + j);
  535. X = (long)((u64)GETU32(p) << 32 | GETU32(p + 4));
  536. # endif
  537. } else {
  538. const u8 *p = (const u8 *)(xi + j);
  539. X = (long)GETU32(p);
  540. }
  541. } else
  542. X = xi[j];
  543. for (i = 0; i < 8 * sizeof(long); ++i, X <<= 1) {
  544. u64 M = (u64)(X >> (8 * sizeof(long) - 1));
  545. Z.hi ^= V.hi & M;
  546. Z.lo ^= V.lo & M;
  547. REDUCE1BIT(V);
  548. }
  549. }
  550. if (is_endian.little) {
  551. # ifdef BSWAP8
  552. Xi[0] = BSWAP8(Z.hi);
  553. Xi[1] = BSWAP8(Z.lo);
  554. # else
  555. u8 *p = (u8 *)Xi;
  556. u32 v;
  557. v = (u32)(Z.hi >> 32);
  558. PUTU32(p, v);
  559. v = (u32)(Z.hi);
  560. PUTU32(p + 4, v);
  561. v = (u32)(Z.lo >> 32);
  562. PUTU32(p + 8, v);
  563. v = (u32)(Z.lo);
  564. PUTU32(p + 12, v);
  565. # endif
  566. } else {
  567. Xi[0] = Z.hi;
  568. Xi[1] = Z.lo;
  569. }
  570. }
  571. # define GCM_MUL(ctx) gcm_gmult_1bit(ctx->Xi.u,ctx->H.u)
  572. #endif
  573. #if TABLE_BITS==4 && (defined(GHASH_ASM) || defined(OPENSSL_CPUID_OBJ))
  574. # if !defined(I386_ONLY) && \
  575. (defined(__i386) || defined(__i386__) || \
  576. defined(__x86_64) || defined(__x86_64__) || \
  577. defined(_M_IX86) || defined(_M_AMD64) || defined(_M_X64))
  578. # define GHASH_ASM_X86_OR_64
  579. # define GCM_FUNCREF_4BIT
  580. extern unsigned int OPENSSL_ia32cap_P[];
  581. void gcm_init_clmul(u128 Htable[16], const u64 Xi[2]);
  582. void gcm_gmult_clmul(u64 Xi[2], const u128 Htable[16]);
  583. void gcm_ghash_clmul(u64 Xi[2], const u128 Htable[16], const u8 *inp,
  584. size_t len);
  585. # if defined(__i386) || defined(__i386__) || defined(_M_IX86)
  586. # define gcm_init_avx gcm_init_clmul
  587. # define gcm_gmult_avx gcm_gmult_clmul
  588. # define gcm_ghash_avx gcm_ghash_clmul
  589. # else
  590. void gcm_init_avx(u128 Htable[16], const u64 Xi[2]);
  591. void gcm_gmult_avx(u64 Xi[2], const u128 Htable[16]);
  592. void gcm_ghash_avx(u64 Xi[2], const u128 Htable[16], const u8 *inp,
  593. size_t len);
  594. # endif
  595. # if defined(__i386) || defined(__i386__) || defined(_M_IX86)
  596. # define GHASH_ASM_X86
  597. void gcm_gmult_4bit_mmx(u64 Xi[2], const u128 Htable[16]);
  598. void gcm_ghash_4bit_mmx(u64 Xi[2], const u128 Htable[16], const u8 *inp,
  599. size_t len);
  600. void gcm_gmult_4bit_x86(u64 Xi[2], const u128 Htable[16]);
  601. void gcm_ghash_4bit_x86(u64 Xi[2], const u128 Htable[16], const u8 *inp,
  602. size_t len);
  603. # endif
  604. # elif defined(__arm__) || defined(__arm) || defined(__aarch64__)
  605. # include "arm_arch.h"
  606. # if __ARM_MAX_ARCH__>=7
  607. # define GHASH_ASM_ARM
  608. # define GCM_FUNCREF_4BIT
  609. # define PMULL_CAPABLE (OPENSSL_armcap_P & ARMV8_PMULL)
  610. # if defined(__arm__) || defined(__arm)
  611. # define NEON_CAPABLE (OPENSSL_armcap_P & ARMV7_NEON)
  612. # endif
  613. void gcm_init_neon(u128 Htable[16], const u64 Xi[2]);
  614. void gcm_gmult_neon(u64 Xi[2], const u128 Htable[16]);
  615. void gcm_ghash_neon(u64 Xi[2], const u128 Htable[16], const u8 *inp,
  616. size_t len);
  617. void gcm_init_v8(u128 Htable[16], const u64 Xi[2]);
  618. void gcm_gmult_v8(u64 Xi[2], const u128 Htable[16]);
  619. void gcm_ghash_v8(u64 Xi[2], const u128 Htable[16], const u8 *inp,
  620. size_t len);
  621. # endif
  622. # elif defined(__sparc__) || defined(__sparc)
  623. # include "sparc_arch.h"
  624. # define GHASH_ASM_SPARC
  625. # define GCM_FUNCREF_4BIT
  626. extern unsigned int OPENSSL_sparcv9cap_P[];
  627. void gcm_init_vis3(u128 Htable[16], const u64 Xi[2]);
  628. void gcm_gmult_vis3(u64 Xi[2], const u128 Htable[16]);
  629. void gcm_ghash_vis3(u64 Xi[2], const u128 Htable[16], const u8 *inp,
  630. size_t len);
  631. # elif defined(OPENSSL_CPUID_OBJ) && (defined(__powerpc__) || defined(__ppc__) || defined(_ARCH_PPC))
  632. # include "ppc_arch.h"
  633. # define GHASH_ASM_PPC
  634. # define GCM_FUNCREF_4BIT
  635. void gcm_init_p8(u128 Htable[16], const u64 Xi[2]);
  636. void gcm_gmult_p8(u64 Xi[2], const u128 Htable[16]);
  637. void gcm_ghash_p8(u64 Xi[2], const u128 Htable[16], const u8 *inp,
  638. size_t len);
  639. # endif
  640. #endif
  641. #ifdef GCM_FUNCREF_4BIT
  642. # undef GCM_MUL
  643. # define GCM_MUL(ctx) (*gcm_gmult_p)(ctx->Xi.u,ctx->Htable)
  644. # ifdef GHASH
  645. # undef GHASH
  646. # define GHASH(ctx,in,len) (*gcm_ghash_p)(ctx->Xi.u,ctx->Htable,in,len)
  647. # endif
  648. #endif
  649. void CRYPTO_gcm128_init(GCM128_CONTEXT *ctx, void *key, block128_f block)
  650. {
  651. const union {
  652. long one;
  653. char little;
  654. } is_endian = { 1 };
  655. memset(ctx, 0, sizeof(*ctx));
  656. ctx->block = block;
  657. ctx->key = key;
  658. (*block) (ctx->H.c, ctx->H.c, key);
  659. if (is_endian.little) {
  660. /* H is stored in host byte order */
  661. #ifdef BSWAP8
  662. ctx->H.u[0] = BSWAP8(ctx->H.u[0]);
  663. ctx->H.u[1] = BSWAP8(ctx->H.u[1]);
  664. #else
  665. u8 *p = ctx->H.c;
  666. u64 hi, lo;
  667. hi = (u64)GETU32(p) << 32 | GETU32(p + 4);
  668. lo = (u64)GETU32(p + 8) << 32 | GETU32(p + 12);
  669. ctx->H.u[0] = hi;
  670. ctx->H.u[1] = lo;
  671. #endif
  672. }
  673. #if TABLE_BITS==8
  674. gcm_init_8bit(ctx->Htable, ctx->H.u);
  675. #elif TABLE_BITS==4
  676. # if defined(GHASH)
  677. # define CTX__GHASH(f) (ctx->ghash = (f))
  678. # else
  679. # define CTX__GHASH(f) (ctx->ghash = NULL)
  680. # endif
  681. # if defined(GHASH_ASM_X86_OR_64)
  682. # if !defined(GHASH_ASM_X86) || defined(OPENSSL_IA32_SSE2)
  683. if (OPENSSL_ia32cap_P[1] & (1 << 1)) { /* check PCLMULQDQ bit */
  684. if (((OPENSSL_ia32cap_P[1] >> 22) & 0x41) == 0x41) { /* AVX+MOVBE */
  685. gcm_init_avx(ctx->Htable, ctx->H.u);
  686. ctx->gmult = gcm_gmult_avx;
  687. CTX__GHASH(gcm_ghash_avx);
  688. } else {
  689. gcm_init_clmul(ctx->Htable, ctx->H.u);
  690. ctx->gmult = gcm_gmult_clmul;
  691. CTX__GHASH(gcm_ghash_clmul);
  692. }
  693. return;
  694. }
  695. # endif
  696. gcm_init_4bit(ctx->Htable, ctx->H.u);
  697. # if defined(GHASH_ASM_X86) /* x86 only */
  698. # if defined(OPENSSL_IA32_SSE2)
  699. if (OPENSSL_ia32cap_P[0] & (1 << 25)) { /* check SSE bit */
  700. # else
  701. if (OPENSSL_ia32cap_P[0] & (1 << 23)) { /* check MMX bit */
  702. # endif
  703. ctx->gmult = gcm_gmult_4bit_mmx;
  704. CTX__GHASH(gcm_ghash_4bit_mmx);
  705. } else {
  706. ctx->gmult = gcm_gmult_4bit_x86;
  707. CTX__GHASH(gcm_ghash_4bit_x86);
  708. }
  709. # else
  710. ctx->gmult = gcm_gmult_4bit;
  711. CTX__GHASH(gcm_ghash_4bit);
  712. # endif
  713. # elif defined(GHASH_ASM_ARM)
  714. # ifdef PMULL_CAPABLE
  715. if (PMULL_CAPABLE) {
  716. gcm_init_v8(ctx->Htable, ctx->H.u);
  717. ctx->gmult = gcm_gmult_v8;
  718. CTX__GHASH(gcm_ghash_v8);
  719. } else
  720. # endif
  721. # ifdef NEON_CAPABLE
  722. if (NEON_CAPABLE) {
  723. gcm_init_neon(ctx->Htable, ctx->H.u);
  724. ctx->gmult = gcm_gmult_neon;
  725. CTX__GHASH(gcm_ghash_neon);
  726. } else
  727. # endif
  728. {
  729. gcm_init_4bit(ctx->Htable, ctx->H.u);
  730. ctx->gmult = gcm_gmult_4bit;
  731. CTX__GHASH(gcm_ghash_4bit);
  732. }
  733. # elif defined(GHASH_ASM_SPARC)
  734. if (OPENSSL_sparcv9cap_P[0] & SPARCV9_VIS3) {
  735. gcm_init_vis3(ctx->Htable, ctx->H.u);
  736. ctx->gmult = gcm_gmult_vis3;
  737. CTX__GHASH(gcm_ghash_vis3);
  738. } else {
  739. gcm_init_4bit(ctx->Htable, ctx->H.u);
  740. ctx->gmult = gcm_gmult_4bit;
  741. CTX__GHASH(gcm_ghash_4bit);
  742. }
  743. # elif defined(GHASH_ASM_PPC)
  744. if (OPENSSL_ppccap_P & PPC_CRYPTO207) {
  745. gcm_init_p8(ctx->Htable, ctx->H.u);
  746. ctx->gmult = gcm_gmult_p8;
  747. CTX__GHASH(gcm_ghash_p8);
  748. } else {
  749. gcm_init_4bit(ctx->Htable, ctx->H.u);
  750. ctx->gmult = gcm_gmult_4bit;
  751. CTX__GHASH(gcm_ghash_4bit);
  752. }
  753. # else
  754. gcm_init_4bit(ctx->Htable, ctx->H.u);
  755. # endif
  756. # undef CTX__GHASH
  757. #endif
  758. }
  759. void CRYPTO_gcm128_setiv(GCM128_CONTEXT *ctx, const unsigned char *iv,
  760. size_t len)
  761. {
  762. const union {
  763. long one;
  764. char little;
  765. } is_endian = { 1 };
  766. unsigned int ctr;
  767. #ifdef GCM_FUNCREF_4BIT
  768. void (*gcm_gmult_p) (u64 Xi[2], const u128 Htable[16]) = ctx->gmult;
  769. #endif
  770. ctx->len.u[0] = 0; /* AAD length */
  771. ctx->len.u[1] = 0; /* message length */
  772. ctx->ares = 0;
  773. ctx->mres = 0;
  774. if (len == 12) {
  775. memcpy(ctx->Yi.c, iv, 12);
  776. ctx->Yi.c[12] = 0;
  777. ctx->Yi.c[13] = 0;
  778. ctx->Yi.c[14] = 0;
  779. ctx->Yi.c[15] = 1;
  780. ctr = 1;
  781. } else {
  782. size_t i;
  783. u64 len0 = len;
  784. /* Borrow ctx->Xi to calculate initial Yi */
  785. ctx->Xi.u[0] = 0;
  786. ctx->Xi.u[1] = 0;
  787. while (len >= 16) {
  788. for (i = 0; i < 16; ++i)
  789. ctx->Xi.c[i] ^= iv[i];
  790. GCM_MUL(ctx);
  791. iv += 16;
  792. len -= 16;
  793. }
  794. if (len) {
  795. for (i = 0; i < len; ++i)
  796. ctx->Xi.c[i] ^= iv[i];
  797. GCM_MUL(ctx);
  798. }
  799. len0 <<= 3;
  800. if (is_endian.little) {
  801. #ifdef BSWAP8
  802. ctx->Xi.u[1] ^= BSWAP8(len0);
  803. #else
  804. ctx->Xi.c[8] ^= (u8)(len0 >> 56);
  805. ctx->Xi.c[9] ^= (u8)(len0 >> 48);
  806. ctx->Xi.c[10] ^= (u8)(len0 >> 40);
  807. ctx->Xi.c[11] ^= (u8)(len0 >> 32);
  808. ctx->Xi.c[12] ^= (u8)(len0 >> 24);
  809. ctx->Xi.c[13] ^= (u8)(len0 >> 16);
  810. ctx->Xi.c[14] ^= (u8)(len0 >> 8);
  811. ctx->Xi.c[15] ^= (u8)(len0);
  812. #endif
  813. } else {
  814. ctx->Xi.u[1] ^= len0;
  815. }
  816. GCM_MUL(ctx);
  817. if (is_endian.little)
  818. #ifdef BSWAP4
  819. ctr = BSWAP4(ctx->Xi.d[3]);
  820. #else
  821. ctr = GETU32(ctx->Xi.c + 12);
  822. #endif
  823. else
  824. ctr = ctx->Xi.d[3];
  825. /* Copy borrowed Xi to Yi */
  826. ctx->Yi.u[0] = ctx->Xi.u[0];
  827. ctx->Yi.u[1] = ctx->Xi.u[1];
  828. }
  829. ctx->Xi.u[0] = 0;
  830. ctx->Xi.u[1] = 0;
  831. (*ctx->block) (ctx->Yi.c, ctx->EK0.c, ctx->key);
  832. ++ctr;
  833. if (is_endian.little)
  834. #ifdef BSWAP4
  835. ctx->Yi.d[3] = BSWAP4(ctr);
  836. #else
  837. PUTU32(ctx->Yi.c + 12, ctr);
  838. #endif
  839. else
  840. ctx->Yi.d[3] = ctr;
  841. }
  842. int CRYPTO_gcm128_aad(GCM128_CONTEXT *ctx, const unsigned char *aad,
  843. size_t len)
  844. {
  845. size_t i;
  846. unsigned int n;
  847. u64 alen = ctx->len.u[0];
  848. #ifdef GCM_FUNCREF_4BIT
  849. void (*gcm_gmult_p) (u64 Xi[2], const u128 Htable[16]) = ctx->gmult;
  850. # ifdef GHASH
  851. void (*gcm_ghash_p) (u64 Xi[2], const u128 Htable[16],
  852. const u8 *inp, size_t len) = ctx->ghash;
  853. # endif
  854. #endif
  855. if (ctx->len.u[1])
  856. return -2;
  857. alen += len;
  858. if (alen > (U64(1) << 61) || (sizeof(len) == 8 && alen < len))
  859. return -1;
  860. ctx->len.u[0] = alen;
  861. n = ctx->ares;
  862. if (n) {
  863. while (n && len) {
  864. ctx->Xi.c[n] ^= *(aad++);
  865. --len;
  866. n = (n + 1) % 16;
  867. }
  868. if (n == 0)
  869. GCM_MUL(ctx);
  870. else {
  871. ctx->ares = n;
  872. return 0;
  873. }
  874. }
  875. #ifdef GHASH
  876. if ((i = (len & (size_t)-16))) {
  877. GHASH(ctx, aad, i);
  878. aad += i;
  879. len -= i;
  880. }
  881. #else
  882. while (len >= 16) {
  883. for (i = 0; i < 16; ++i)
  884. ctx->Xi.c[i] ^= aad[i];
  885. GCM_MUL(ctx);
  886. aad += 16;
  887. len -= 16;
  888. }
  889. #endif
  890. if (len) {
  891. n = (unsigned int)len;
  892. for (i = 0; i < len; ++i)
  893. ctx->Xi.c[i] ^= aad[i];
  894. }
  895. ctx->ares = n;
  896. return 0;
  897. }
  898. int CRYPTO_gcm128_encrypt(GCM128_CONTEXT *ctx,
  899. const unsigned char *in, unsigned char *out,
  900. size_t len)
  901. {
  902. const union {
  903. long one;
  904. char little;
  905. } is_endian = { 1 };
  906. unsigned int n, ctr, mres;
  907. size_t i;
  908. u64 mlen = ctx->len.u[1];
  909. block128_f block = ctx->block;
  910. void *key = ctx->key;
  911. #ifdef GCM_FUNCREF_4BIT
  912. void (*gcm_gmult_p) (u64 Xi[2], const u128 Htable[16]) = ctx->gmult;
  913. # if defined(GHASH) && !defined(OPENSSL_SMALL_FOOTPRINT)
  914. void (*gcm_ghash_p) (u64 Xi[2], const u128 Htable[16],
  915. const u8 *inp, size_t len) = ctx->ghash;
  916. # endif
  917. #endif
  918. mlen += len;
  919. if (mlen > ((U64(1) << 36) - 32) || (sizeof(len) == 8 && mlen < len))
  920. return -1;
  921. ctx->len.u[1] = mlen;
  922. mres = ctx->mres;
  923. if (ctx->ares) {
  924. /* First call to encrypt finalizes GHASH(AAD) */
  925. #if defined(GHASH) && !defined(OPENSSL_SMALL_FOOTPRINT)
  926. if (len == 0) {
  927. GCM_MUL(ctx);
  928. ctx->ares = 0;
  929. return 0;
  930. }
  931. memcpy(ctx->Xn, ctx->Xi.c, sizeof(ctx->Xi));
  932. ctx->Xi.u[0] = 0;
  933. ctx->Xi.u[1] = 0;
  934. mres = sizeof(ctx->Xi);
  935. #else
  936. GCM_MUL(ctx);
  937. #endif
  938. ctx->ares = 0;
  939. }
  940. if (is_endian.little)
  941. #ifdef BSWAP4
  942. ctr = BSWAP4(ctx->Yi.d[3]);
  943. #else
  944. ctr = GETU32(ctx->Yi.c + 12);
  945. #endif
  946. else
  947. ctr = ctx->Yi.d[3];
  948. n = mres % 16;
  949. #if !defined(OPENSSL_SMALL_FOOTPRINT)
  950. if (16 % sizeof(size_t) == 0) { /* always true actually */
  951. do {
  952. if (n) {
  953. # if defined(GHASH)
  954. while (n && len) {
  955. ctx->Xn[mres++] = *(out++) = *(in++) ^ ctx->EKi.c[n];
  956. --len;
  957. n = (n + 1) % 16;
  958. }
  959. if (n == 0) {
  960. GHASH(ctx, ctx->Xn, mres);
  961. mres = 0;
  962. } else {
  963. ctx->mres = mres;
  964. return 0;
  965. }
  966. # else
  967. while (n && len) {
  968. ctx->Xi.c[n] ^= *(out++) = *(in++) ^ ctx->EKi.c[n];
  969. --len;
  970. n = (n + 1) % 16;
  971. }
  972. if (n == 0) {
  973. GCM_MUL(ctx);
  974. mres = 0;
  975. } else {
  976. ctx->mres = n;
  977. return 0;
  978. }
  979. # endif
  980. }
  981. # if defined(STRICT_ALIGNMENT)
  982. if (((size_t)in | (size_t)out) % sizeof(size_t) != 0)
  983. break;
  984. # endif
  985. # if defined(GHASH)
  986. if (len >= 16 && mres) {
  987. GHASH(ctx, ctx->Xn, mres);
  988. mres = 0;
  989. }
  990. # if defined(GHASH_CHUNK)
  991. while (len >= GHASH_CHUNK) {
  992. size_t j = GHASH_CHUNK;
  993. while (j) {
  994. size_t_aX *out_t = (size_t_aX *)out;
  995. const size_t_aX *in_t = (const size_t_aX *)in;
  996. (*block) (ctx->Yi.c, ctx->EKi.c, key);
  997. ++ctr;
  998. if (is_endian.little)
  999. # ifdef BSWAP4
  1000. ctx->Yi.d[3] = BSWAP4(ctr);
  1001. # else
  1002. PUTU32(ctx->Yi.c + 12, ctr);
  1003. # endif
  1004. else
  1005. ctx->Yi.d[3] = ctr;
  1006. for (i = 0; i < 16 / sizeof(size_t); ++i)
  1007. out_t[i] = in_t[i] ^ ctx->EKi.t[i];
  1008. out += 16;
  1009. in += 16;
  1010. j -= 16;
  1011. }
  1012. GHASH(ctx, out - GHASH_CHUNK, GHASH_CHUNK);
  1013. len -= GHASH_CHUNK;
  1014. }
  1015. # endif
  1016. if ((i = (len & (size_t)-16))) {
  1017. size_t j = i;
  1018. while (len >= 16) {
  1019. size_t_aX *out_t = (size_t_aX *)out;
  1020. const size_t_aX *in_t = (const size_t_aX *)in;
  1021. (*block) (ctx->Yi.c, ctx->EKi.c, key);
  1022. ++ctr;
  1023. if (is_endian.little)
  1024. # ifdef BSWAP4
  1025. ctx->Yi.d[3] = BSWAP4(ctr);
  1026. # else
  1027. PUTU32(ctx->Yi.c + 12, ctr);
  1028. # endif
  1029. else
  1030. ctx->Yi.d[3] = ctr;
  1031. for (i = 0; i < 16 / sizeof(size_t); ++i)
  1032. out_t[i] = in_t[i] ^ ctx->EKi.t[i];
  1033. out += 16;
  1034. in += 16;
  1035. len -= 16;
  1036. }
  1037. GHASH(ctx, out - j, j);
  1038. }
  1039. # else
  1040. while (len >= 16) {
  1041. size_t *out_t = (size_t *)out;
  1042. const size_t *in_t = (const size_t *)in;
  1043. (*block) (ctx->Yi.c, ctx->EKi.c, key);
  1044. ++ctr;
  1045. if (is_endian.little)
  1046. # ifdef BSWAP4
  1047. ctx->Yi.d[3] = BSWAP4(ctr);
  1048. # else
  1049. PUTU32(ctx->Yi.c + 12, ctr);
  1050. # endif
  1051. else
  1052. ctx->Yi.d[3] = ctr;
  1053. for (i = 0; i < 16 / sizeof(size_t); ++i)
  1054. ctx->Xi.t[i] ^= out_t[i] = in_t[i] ^ ctx->EKi.t[i];
  1055. GCM_MUL(ctx);
  1056. out += 16;
  1057. in += 16;
  1058. len -= 16;
  1059. }
  1060. # endif
  1061. if (len) {
  1062. (*block) (ctx->Yi.c, ctx->EKi.c, key);
  1063. ++ctr;
  1064. if (is_endian.little)
  1065. # ifdef BSWAP4
  1066. ctx->Yi.d[3] = BSWAP4(ctr);
  1067. # else
  1068. PUTU32(ctx->Yi.c + 12, ctr);
  1069. # endif
  1070. else
  1071. ctx->Yi.d[3] = ctr;
  1072. # if defined(GHASH)
  1073. while (len--) {
  1074. ctx->Xn[mres++] = out[n] = in[n] ^ ctx->EKi.c[n];
  1075. ++n;
  1076. }
  1077. # else
  1078. while (len--) {
  1079. ctx->Xi.c[n] ^= out[n] = in[n] ^ ctx->EKi.c[n];
  1080. ++n;
  1081. }
  1082. mres = n;
  1083. # endif
  1084. }
  1085. ctx->mres = mres;
  1086. return 0;
  1087. } while (0);
  1088. }
  1089. #endif
  1090. for (i = 0; i < len; ++i) {
  1091. if (n == 0) {
  1092. (*block) (ctx->Yi.c, ctx->EKi.c, key);
  1093. ++ctr;
  1094. if (is_endian.little)
  1095. #ifdef BSWAP4
  1096. ctx->Yi.d[3] = BSWAP4(ctr);
  1097. #else
  1098. PUTU32(ctx->Yi.c + 12, ctr);
  1099. #endif
  1100. else
  1101. ctx->Yi.d[3] = ctr;
  1102. }
  1103. #if defined(GHASH) && !defined(OPENSSL_SMALL_FOOTPRINT)
  1104. ctx->Xn[mres++] = out[i] = in[i] ^ ctx->EKi.c[n];
  1105. n = (n + 1) % 16;
  1106. if (mres == sizeof(ctx->Xn)) {
  1107. GHASH(ctx,ctx->Xn,sizeof(ctx->Xn));
  1108. mres = 0;
  1109. }
  1110. #else
  1111. ctx->Xi.c[n] ^= out[i] = in[i] ^ ctx->EKi.c[n];
  1112. mres = n = (n + 1) % 16;
  1113. if (n == 0)
  1114. GCM_MUL(ctx);
  1115. #endif
  1116. }
  1117. ctx->mres = mres;
  1118. return 0;
  1119. }
  1120. int CRYPTO_gcm128_decrypt(GCM128_CONTEXT *ctx,
  1121. const unsigned char *in, unsigned char *out,
  1122. size_t len)
  1123. {
  1124. const union {
  1125. long one;
  1126. char little;
  1127. } is_endian = { 1 };
  1128. unsigned int n, ctr, mres;
  1129. size_t i;
  1130. u64 mlen = ctx->len.u[1];
  1131. block128_f block = ctx->block;
  1132. void *key = ctx->key;
  1133. #ifdef GCM_FUNCREF_4BIT
  1134. void (*gcm_gmult_p) (u64 Xi[2], const u128 Htable[16]) = ctx->gmult;
  1135. # if defined(GHASH) && !defined(OPENSSL_SMALL_FOOTPRINT)
  1136. void (*gcm_ghash_p) (u64 Xi[2], const u128 Htable[16],
  1137. const u8 *inp, size_t len) = ctx->ghash;
  1138. # endif
  1139. #endif
  1140. mlen += len;
  1141. if (mlen > ((U64(1) << 36) - 32) || (sizeof(len) == 8 && mlen < len))
  1142. return -1;
  1143. ctx->len.u[1] = mlen;
  1144. mres = ctx->mres;
  1145. if (ctx->ares) {
  1146. /* First call to decrypt finalizes GHASH(AAD) */
  1147. #if defined(GHASH) && !defined(OPENSSL_SMALL_FOOTPRINT)
  1148. if (len == 0) {
  1149. GCM_MUL(ctx);
  1150. ctx->ares = 0;
  1151. return 0;
  1152. }
  1153. memcpy(ctx->Xn, ctx->Xi.c, sizeof(ctx->Xi));
  1154. ctx->Xi.u[0] = 0;
  1155. ctx->Xi.u[1] = 0;
  1156. mres = sizeof(ctx->Xi);
  1157. #else
  1158. GCM_MUL(ctx);
  1159. #endif
  1160. ctx->ares = 0;
  1161. }
  1162. if (is_endian.little)
  1163. #ifdef BSWAP4
  1164. ctr = BSWAP4(ctx->Yi.d[3]);
  1165. #else
  1166. ctr = GETU32(ctx->Yi.c + 12);
  1167. #endif
  1168. else
  1169. ctr = ctx->Yi.d[3];
  1170. n = mres % 16;
  1171. #if !defined(OPENSSL_SMALL_FOOTPRINT)
  1172. if (16 % sizeof(size_t) == 0) { /* always true actually */
  1173. do {
  1174. if (n) {
  1175. # if defined(GHASH)
  1176. while (n && len) {
  1177. *(out++) = (ctx->Xn[mres++] = *(in++)) ^ ctx->EKi.c[n];
  1178. --len;
  1179. n = (n + 1) % 16;
  1180. }
  1181. if (n == 0) {
  1182. GHASH(ctx, ctx->Xn, mres);
  1183. mres = 0;
  1184. } else {
  1185. ctx->mres = mres;
  1186. return 0;
  1187. }
  1188. # else
  1189. while (n && len) {
  1190. u8 c = *(in++);
  1191. *(out++) = c ^ ctx->EKi.c[n];
  1192. ctx->Xi.c[n] ^= c;
  1193. --len;
  1194. n = (n + 1) % 16;
  1195. }
  1196. if (n == 0) {
  1197. GCM_MUL(ctx);
  1198. mres = 0;
  1199. } else {
  1200. ctx->mres = n;
  1201. return 0;
  1202. }
  1203. # endif
  1204. }
  1205. # if defined(STRICT_ALIGNMENT)
  1206. if (((size_t)in | (size_t)out) % sizeof(size_t) != 0)
  1207. break;
  1208. # endif
  1209. # if defined(GHASH)
  1210. if (len >= 16 && mres) {
  1211. GHASH(ctx, ctx->Xn, mres);
  1212. mres = 0;
  1213. }
  1214. # if defined(GHASH_CHUNK)
  1215. while (len >= GHASH_CHUNK) {
  1216. size_t j = GHASH_CHUNK;
  1217. GHASH(ctx, in, GHASH_CHUNK);
  1218. while (j) {
  1219. size_t_aX *out_t = (size_t_aX *)out;
  1220. const size_t_aX *in_t = (const size_t_aX *)in;
  1221. (*block) (ctx->Yi.c, ctx->EKi.c, key);
  1222. ++ctr;
  1223. if (is_endian.little)
  1224. # ifdef BSWAP4
  1225. ctx->Yi.d[3] = BSWAP4(ctr);
  1226. # else
  1227. PUTU32(ctx->Yi.c + 12, ctr);
  1228. # endif
  1229. else
  1230. ctx->Yi.d[3] = ctr;
  1231. for (i = 0; i < 16 / sizeof(size_t); ++i)
  1232. out_t[i] = in_t[i] ^ ctx->EKi.t[i];
  1233. out += 16;
  1234. in += 16;
  1235. j -= 16;
  1236. }
  1237. len -= GHASH_CHUNK;
  1238. }
  1239. # endif
  1240. if ((i = (len & (size_t)-16))) {
  1241. GHASH(ctx, in, i);
  1242. while (len >= 16) {
  1243. size_t_aX *out_t = (size_t_aX *)out;
  1244. const size_t_aX *in_t = (const size_t_aX *)in;
  1245. (*block) (ctx->Yi.c, ctx->EKi.c, key);
  1246. ++ctr;
  1247. if (is_endian.little)
  1248. # ifdef BSWAP4
  1249. ctx->Yi.d[3] = BSWAP4(ctr);
  1250. # else
  1251. PUTU32(ctx->Yi.c + 12, ctr);
  1252. # endif
  1253. else
  1254. ctx->Yi.d[3] = ctr;
  1255. for (i = 0; i < 16 / sizeof(size_t); ++i)
  1256. out_t[i] = in_t[i] ^ ctx->EKi.t[i];
  1257. out += 16;
  1258. in += 16;
  1259. len -= 16;
  1260. }
  1261. }
  1262. # else
  1263. while (len >= 16) {
  1264. size_t *out_t = (size_t *)out;
  1265. const size_t *in_t = (const size_t *)in;
  1266. (*block) (ctx->Yi.c, ctx->EKi.c, key);
  1267. ++ctr;
  1268. if (is_endian.little)
  1269. # ifdef BSWAP4
  1270. ctx->Yi.d[3] = BSWAP4(ctr);
  1271. # else
  1272. PUTU32(ctx->Yi.c + 12, ctr);
  1273. # endif
  1274. else
  1275. ctx->Yi.d[3] = ctr;
  1276. for (i = 0; i < 16 / sizeof(size_t); ++i) {
  1277. size_t c = in_t[i];
  1278. out_t[i] = c ^ ctx->EKi.t[i];
  1279. ctx->Xi.t[i] ^= c;
  1280. }
  1281. GCM_MUL(ctx);
  1282. out += 16;
  1283. in += 16;
  1284. len -= 16;
  1285. }
  1286. # endif
  1287. if (len) {
  1288. (*block) (ctx->Yi.c, ctx->EKi.c, key);
  1289. ++ctr;
  1290. if (is_endian.little)
  1291. # ifdef BSWAP4
  1292. ctx->Yi.d[3] = BSWAP4(ctr);
  1293. # else
  1294. PUTU32(ctx->Yi.c + 12, ctr);
  1295. # endif
  1296. else
  1297. ctx->Yi.d[3] = ctr;
  1298. # if defined(GHASH)
  1299. while (len--) {
  1300. out[n] = (ctx->Xn[mres++] = in[n]) ^ ctx->EKi.c[n];
  1301. ++n;
  1302. }
  1303. # else
  1304. while (len--) {
  1305. u8 c = in[n];
  1306. ctx->Xi.c[n] ^= c;
  1307. out[n] = c ^ ctx->EKi.c[n];
  1308. ++n;
  1309. }
  1310. mres = n;
  1311. # endif
  1312. }
  1313. ctx->mres = mres;
  1314. return 0;
  1315. } while (0);
  1316. }
  1317. #endif
  1318. for (i = 0; i < len; ++i) {
  1319. u8 c;
  1320. if (n == 0) {
  1321. (*block) (ctx->Yi.c, ctx->EKi.c, key);
  1322. ++ctr;
  1323. if (is_endian.little)
  1324. #ifdef BSWAP4
  1325. ctx->Yi.d[3] = BSWAP4(ctr);
  1326. #else
  1327. PUTU32(ctx->Yi.c + 12, ctr);
  1328. #endif
  1329. else
  1330. ctx->Yi.d[3] = ctr;
  1331. }
  1332. #if defined(GHASH) && !defined(OPENSSL_SMALL_FOOTPRINT)
  1333. out[i] = (ctx->Xn[mres++] = c = in[i]) ^ ctx->EKi.c[n];
  1334. n = (n + 1) % 16;
  1335. if (mres == sizeof(ctx->Xn)) {
  1336. GHASH(ctx,ctx->Xn,sizeof(ctx->Xn));
  1337. mres = 0;
  1338. }
  1339. #else
  1340. c = in[i];
  1341. out[i] = c ^ ctx->EKi.c[n];
  1342. ctx->Xi.c[n] ^= c;
  1343. mres = n = (n + 1) % 16;
  1344. if (n == 0)
  1345. GCM_MUL(ctx);
  1346. #endif
  1347. }
  1348. ctx->mres = mres;
  1349. return 0;
  1350. }
  1351. int CRYPTO_gcm128_encrypt_ctr32(GCM128_CONTEXT *ctx,
  1352. const unsigned char *in, unsigned char *out,
  1353. size_t len, ctr128_f stream)
  1354. {
  1355. #if defined(OPENSSL_SMALL_FOOTPRINT)
  1356. return CRYPTO_gcm128_encrypt(ctx, in, out, len);
  1357. #else
  1358. const union {
  1359. long one;
  1360. char little;
  1361. } is_endian = { 1 };
  1362. unsigned int n, ctr, mres;
  1363. size_t i;
  1364. u64 mlen = ctx->len.u[1];
  1365. void *key = ctx->key;
  1366. # ifdef GCM_FUNCREF_4BIT
  1367. void (*gcm_gmult_p) (u64 Xi[2], const u128 Htable[16]) = ctx->gmult;
  1368. # ifdef GHASH
  1369. void (*gcm_ghash_p) (u64 Xi[2], const u128 Htable[16],
  1370. const u8 *inp, size_t len) = ctx->ghash;
  1371. # endif
  1372. # endif
  1373. mlen += len;
  1374. if (mlen > ((U64(1) << 36) - 32) || (sizeof(len) == 8 && mlen < len))
  1375. return -1;
  1376. ctx->len.u[1] = mlen;
  1377. mres = ctx->mres;
  1378. if (ctx->ares) {
  1379. /* First call to encrypt finalizes GHASH(AAD) */
  1380. #if defined(GHASH)
  1381. if (len == 0) {
  1382. GCM_MUL(ctx);
  1383. ctx->ares = 0;
  1384. return 0;
  1385. }
  1386. memcpy(ctx->Xn, ctx->Xi.c, sizeof(ctx->Xi));
  1387. ctx->Xi.u[0] = 0;
  1388. ctx->Xi.u[1] = 0;
  1389. mres = sizeof(ctx->Xi);
  1390. #else
  1391. GCM_MUL(ctx);
  1392. #endif
  1393. ctx->ares = 0;
  1394. }
  1395. if (is_endian.little)
  1396. # ifdef BSWAP4
  1397. ctr = BSWAP4(ctx->Yi.d[3]);
  1398. # else
  1399. ctr = GETU32(ctx->Yi.c + 12);
  1400. # endif
  1401. else
  1402. ctr = ctx->Yi.d[3];
  1403. n = mres % 16;
  1404. if (n) {
  1405. # if defined(GHASH)
  1406. while (n && len) {
  1407. ctx->Xn[mres++] = *(out++) = *(in++) ^ ctx->EKi.c[n];
  1408. --len;
  1409. n = (n + 1) % 16;
  1410. }
  1411. if (n == 0) {
  1412. GHASH(ctx, ctx->Xn, mres);
  1413. mres = 0;
  1414. } else {
  1415. ctx->mres = mres;
  1416. return 0;
  1417. }
  1418. # else
  1419. while (n && len) {
  1420. ctx->Xi.c[n] ^= *(out++) = *(in++) ^ ctx->EKi.c[n];
  1421. --len;
  1422. n = (n + 1) % 16;
  1423. }
  1424. if (n == 0) {
  1425. GCM_MUL(ctx);
  1426. mres = 0;
  1427. } else {
  1428. ctx->mres = n;
  1429. return 0;
  1430. }
  1431. # endif
  1432. }
  1433. # if defined(GHASH)
  1434. if (len >= 16 && mres) {
  1435. GHASH(ctx, ctx->Xn, mres);
  1436. mres = 0;
  1437. }
  1438. # if defined(GHASH_CHUNK)
  1439. while (len >= GHASH_CHUNK) {
  1440. (*stream) (in, out, GHASH_CHUNK / 16, key, ctx->Yi.c);
  1441. ctr += GHASH_CHUNK / 16;
  1442. if (is_endian.little)
  1443. # ifdef BSWAP4
  1444. ctx->Yi.d[3] = BSWAP4(ctr);
  1445. # else
  1446. PUTU32(ctx->Yi.c + 12, ctr);
  1447. # endif
  1448. else
  1449. ctx->Yi.d[3] = ctr;
  1450. GHASH(ctx, out, GHASH_CHUNK);
  1451. out += GHASH_CHUNK;
  1452. in += GHASH_CHUNK;
  1453. len -= GHASH_CHUNK;
  1454. }
  1455. # endif
  1456. # endif
  1457. if ((i = (len & (size_t)-16))) {
  1458. size_t j = i / 16;
  1459. (*stream) (in, out, j, key, ctx->Yi.c);
  1460. ctr += (unsigned int)j;
  1461. if (is_endian.little)
  1462. # ifdef BSWAP4
  1463. ctx->Yi.d[3] = BSWAP4(ctr);
  1464. # else
  1465. PUTU32(ctx->Yi.c + 12, ctr);
  1466. # endif
  1467. else
  1468. ctx->Yi.d[3] = ctr;
  1469. in += i;
  1470. len -= i;
  1471. # if defined(GHASH)
  1472. GHASH(ctx, out, i);
  1473. out += i;
  1474. # else
  1475. while (j--) {
  1476. for (i = 0; i < 16; ++i)
  1477. ctx->Xi.c[i] ^= out[i];
  1478. GCM_MUL(ctx);
  1479. out += 16;
  1480. }
  1481. # endif
  1482. }
  1483. if (len) {
  1484. (*ctx->block) (ctx->Yi.c, ctx->EKi.c, key);
  1485. ++ctr;
  1486. if (is_endian.little)
  1487. # ifdef BSWAP4
  1488. ctx->Yi.d[3] = BSWAP4(ctr);
  1489. # else
  1490. PUTU32(ctx->Yi.c + 12, ctr);
  1491. # endif
  1492. else
  1493. ctx->Yi.d[3] = ctr;
  1494. while (len--) {
  1495. # if defined(GHASH)
  1496. ctx->Xn[mres++] = out[n] = in[n] ^ ctx->EKi.c[n];
  1497. # else
  1498. ctx->Xi.c[mres++] ^= out[n] = in[n] ^ ctx->EKi.c[n];
  1499. # endif
  1500. ++n;
  1501. }
  1502. }
  1503. ctx->mres = mres;
  1504. return 0;
  1505. #endif
  1506. }
  1507. int CRYPTO_gcm128_decrypt_ctr32(GCM128_CONTEXT *ctx,
  1508. const unsigned char *in, unsigned char *out,
  1509. size_t len, ctr128_f stream)
  1510. {
  1511. #if defined(OPENSSL_SMALL_FOOTPRINT)
  1512. return CRYPTO_gcm128_decrypt(ctx, in, out, len);
  1513. #else
  1514. const union {
  1515. long one;
  1516. char little;
  1517. } is_endian = { 1 };
  1518. unsigned int n, ctr, mres;
  1519. size_t i;
  1520. u64 mlen = ctx->len.u[1];
  1521. void *key = ctx->key;
  1522. # ifdef GCM_FUNCREF_4BIT
  1523. void (*gcm_gmult_p) (u64 Xi[2], const u128 Htable[16]) = ctx->gmult;
  1524. # ifdef GHASH
  1525. void (*gcm_ghash_p) (u64 Xi[2], const u128 Htable[16],
  1526. const u8 *inp, size_t len) = ctx->ghash;
  1527. # endif
  1528. # endif
  1529. mlen += len;
  1530. if (mlen > ((U64(1) << 36) - 32) || (sizeof(len) == 8 && mlen < len))
  1531. return -1;
  1532. ctx->len.u[1] = mlen;
  1533. mres = ctx->mres;
  1534. if (ctx->ares) {
  1535. /* First call to decrypt finalizes GHASH(AAD) */
  1536. # if defined(GHASH)
  1537. if (len == 0) {
  1538. GCM_MUL(ctx);
  1539. ctx->ares = 0;
  1540. return 0;
  1541. }
  1542. memcpy(ctx->Xn, ctx->Xi.c, sizeof(ctx->Xi));
  1543. ctx->Xi.u[0] = 0;
  1544. ctx->Xi.u[1] = 0;
  1545. mres = sizeof(ctx->Xi);
  1546. # else
  1547. GCM_MUL(ctx);
  1548. # endif
  1549. ctx->ares = 0;
  1550. }
  1551. if (is_endian.little)
  1552. # ifdef BSWAP4
  1553. ctr = BSWAP4(ctx->Yi.d[3]);
  1554. # else
  1555. ctr = GETU32(ctx->Yi.c + 12);
  1556. # endif
  1557. else
  1558. ctr = ctx->Yi.d[3];
  1559. n = mres % 16;
  1560. if (n) {
  1561. # if defined(GHASH)
  1562. while (n && len) {
  1563. *(out++) = (ctx->Xn[mres++] = *(in++)) ^ ctx->EKi.c[n];
  1564. --len;
  1565. n = (n + 1) % 16;
  1566. }
  1567. if (n == 0) {
  1568. GHASH(ctx, ctx->Xn, mres);
  1569. mres = 0;
  1570. } else {
  1571. ctx->mres = mres;
  1572. return 0;
  1573. }
  1574. # else
  1575. while (n && len) {
  1576. u8 c = *(in++);
  1577. *(out++) = c ^ ctx->EKi.c[n];
  1578. ctx->Xi.c[n] ^= c;
  1579. --len;
  1580. n = (n + 1) % 16;
  1581. }
  1582. if (n == 0) {
  1583. GCM_MUL(ctx);
  1584. mres = 0;
  1585. } else {
  1586. ctx->mres = n;
  1587. return 0;
  1588. }
  1589. # endif
  1590. }
  1591. # if defined(GHASH)
  1592. if (len >= 16 && mres) {
  1593. GHASH(ctx, ctx->Xn, mres);
  1594. mres = 0;
  1595. }
  1596. # if defined(GHASH_CHUNK)
  1597. while (len >= GHASH_CHUNK) {
  1598. GHASH(ctx, in, GHASH_CHUNK);
  1599. (*stream) (in, out, GHASH_CHUNK / 16, key, ctx->Yi.c);
  1600. ctr += GHASH_CHUNK / 16;
  1601. if (is_endian.little)
  1602. # ifdef BSWAP4
  1603. ctx->Yi.d[3] = BSWAP4(ctr);
  1604. # else
  1605. PUTU32(ctx->Yi.c + 12, ctr);
  1606. # endif
  1607. else
  1608. ctx->Yi.d[3] = ctr;
  1609. out += GHASH_CHUNK;
  1610. in += GHASH_CHUNK;
  1611. len -= GHASH_CHUNK;
  1612. }
  1613. # endif
  1614. # endif
  1615. if ((i = (len & (size_t)-16))) {
  1616. size_t j = i / 16;
  1617. # if defined(GHASH)
  1618. GHASH(ctx, in, i);
  1619. # else
  1620. while (j--) {
  1621. size_t k;
  1622. for (k = 0; k < 16; ++k)
  1623. ctx->Xi.c[k] ^= in[k];
  1624. GCM_MUL(ctx);
  1625. in += 16;
  1626. }
  1627. j = i / 16;
  1628. in -= i;
  1629. # endif
  1630. (*stream) (in, out, j, key, ctx->Yi.c);
  1631. ctr += (unsigned int)j;
  1632. if (is_endian.little)
  1633. # ifdef BSWAP4
  1634. ctx->Yi.d[3] = BSWAP4(ctr);
  1635. # else
  1636. PUTU32(ctx->Yi.c + 12, ctr);
  1637. # endif
  1638. else
  1639. ctx->Yi.d[3] = ctr;
  1640. out += i;
  1641. in += i;
  1642. len -= i;
  1643. }
  1644. if (len) {
  1645. (*ctx->block) (ctx->Yi.c, ctx->EKi.c, key);
  1646. ++ctr;
  1647. if (is_endian.little)
  1648. # ifdef BSWAP4
  1649. ctx->Yi.d[3] = BSWAP4(ctr);
  1650. # else
  1651. PUTU32(ctx->Yi.c + 12, ctr);
  1652. # endif
  1653. else
  1654. ctx->Yi.d[3] = ctr;
  1655. while (len--) {
  1656. # if defined(GHASH)
  1657. out[n] = (ctx->Xn[mres++] = in[n]) ^ ctx->EKi.c[n];
  1658. # else
  1659. u8 c = in[n];
  1660. ctx->Xi.c[mres++] ^= c;
  1661. out[n] = c ^ ctx->EKi.c[n];
  1662. # endif
  1663. ++n;
  1664. }
  1665. }
  1666. ctx->mres = mres;
  1667. return 0;
  1668. #endif
  1669. }
  1670. int CRYPTO_gcm128_finish(GCM128_CONTEXT *ctx, const unsigned char *tag,
  1671. size_t len)
  1672. {
  1673. const union {
  1674. long one;
  1675. char little;
  1676. } is_endian = { 1 };
  1677. u64 alen = ctx->len.u[0] << 3;
  1678. u64 clen = ctx->len.u[1] << 3;
  1679. #ifdef GCM_FUNCREF_4BIT
  1680. void (*gcm_gmult_p) (u64 Xi[2], const u128 Htable[16]) = ctx->gmult;
  1681. # if defined(GHASH) && !defined(OPENSSL_SMALL_FOOTPRINT)
  1682. void (*gcm_ghash_p) (u64 Xi[2], const u128 Htable[16],
  1683. const u8 *inp, size_t len) = ctx->ghash;
  1684. # endif
  1685. #endif
  1686. #if defined(GHASH) && !defined(OPENSSL_SMALL_FOOTPRINT)
  1687. u128 bitlen;
  1688. unsigned int mres = ctx->mres;
  1689. if (mres) {
  1690. unsigned blocks = (mres + 15) & -16;
  1691. memset(ctx->Xn + mres, 0, blocks - mres);
  1692. mres = blocks;
  1693. if (mres == sizeof(ctx->Xn)) {
  1694. GHASH(ctx, ctx->Xn, mres);
  1695. mres = 0;
  1696. }
  1697. } else if (ctx->ares) {
  1698. GCM_MUL(ctx);
  1699. }
  1700. #else
  1701. if (ctx->mres || ctx->ares)
  1702. GCM_MUL(ctx);
  1703. #endif
  1704. if (is_endian.little) {
  1705. #ifdef BSWAP8
  1706. alen = BSWAP8(alen);
  1707. clen = BSWAP8(clen);
  1708. #else
  1709. u8 *p = ctx->len.c;
  1710. ctx->len.u[0] = alen;
  1711. ctx->len.u[1] = clen;
  1712. alen = (u64)GETU32(p) << 32 | GETU32(p + 4);
  1713. clen = (u64)GETU32(p + 8) << 32 | GETU32(p + 12);
  1714. #endif
  1715. }
  1716. #if defined(GHASH) && !defined(OPENSSL_SMALL_FOOTPRINT)
  1717. bitlen.hi = alen;
  1718. bitlen.lo = clen;
  1719. memcpy(ctx->Xn + mres, &bitlen, sizeof(bitlen));
  1720. mres += sizeof(bitlen);
  1721. GHASH(ctx, ctx->Xn, mres);
  1722. #else
  1723. ctx->Xi.u[0] ^= alen;
  1724. ctx->Xi.u[1] ^= clen;
  1725. GCM_MUL(ctx);
  1726. #endif
  1727. ctx->Xi.u[0] ^= ctx->EK0.u[0];
  1728. ctx->Xi.u[1] ^= ctx->EK0.u[1];
  1729. if (tag && len <= sizeof(ctx->Xi))
  1730. return CRYPTO_memcmp(ctx->Xi.c, tag, len);
  1731. else
  1732. return -1;
  1733. }
  1734. void CRYPTO_gcm128_tag(GCM128_CONTEXT *ctx, unsigned char *tag, size_t len)
  1735. {
  1736. CRYPTO_gcm128_finish(ctx, NULL, 0);
  1737. memcpy(tag, ctx->Xi.c,
  1738. len <= sizeof(ctx->Xi.c) ? len : sizeof(ctx->Xi.c));
  1739. }
  1740. GCM128_CONTEXT *CRYPTO_gcm128_new(void *key, block128_f block)
  1741. {
  1742. GCM128_CONTEXT *ret;
  1743. if ((ret = OPENSSL_malloc(sizeof(*ret))) != NULL)
  1744. CRYPTO_gcm128_init(ret, key, block);
  1745. return ret;
  1746. }
  1747. void CRYPTO_gcm128_release(GCM128_CONTEXT *ctx)
  1748. {
  1749. OPENSSL_clear_free(ctx, sizeof(*ctx));
  1750. }