You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1273 lines
32 KiB

  1. /* ====================================================================
  2. * Copyright (c) 2008 The OpenSSL Project. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in
  13. * the documentation and/or other materials provided with the
  14. * distribution.
  15. *
  16. * 3. All advertising materials mentioning features or use of this
  17. * software must display the following acknowledgment:
  18. * "This product includes software developed by the OpenSSL Project
  19. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  20. *
  21. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  22. * endorse or promote products derived from this software without
  23. * prior written permission. For written permission, please contact
  24. * openssl-core@openssl.org.
  25. *
  26. * 5. Products derived from this software may not be called "OpenSSL"
  27. * nor may "OpenSSL" appear in their names without prior written
  28. * permission of the OpenSSL Project.
  29. *
  30. * 6. Redistributions of any form whatsoever must retain the following
  31. * acknowledgment:
  32. * "This product includes software developed by the OpenSSL Project
  33. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  34. *
  35. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  36. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  37. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  38. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  41. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  42. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  43. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  44. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  45. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  46. * OF THE POSSIBILITY OF SUCH DAMAGE.
  47. * ==================================================================== */
  48. #include <openssl/base.h>
  49. #include <assert.h>
  50. #include <string.h>
  51. #include <openssl/mem.h>
  52. #include <openssl/cpu.h>
  53. #include "internal.h"
  54. #include "../internal.h"
  55. #if !defined(OPENSSL_NO_ASM) && \
  56. (defined(OPENSSL_X86) || defined(OPENSSL_X86_64) || \
  57. defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64))
  58. #define GHASH_ASM
  59. #endif
  60. #if defined(BSWAP4) && STRICT_ALIGNMENT == 1
  61. /* redefine, because alignment is ensured */
  62. #undef GETU32
  63. #define GETU32(p) BSWAP4(*(const uint32_t *)(p))
  64. #undef PUTU32
  65. #define PUTU32(p, v) *(uint32_t *)(p) = BSWAP4(v)
  66. #endif
  67. #define PACK(s) ((size_t)(s) << (sizeof(size_t) * 8 - 16))
  68. #define REDUCE1BIT(V) \
  69. do { \
  70. if (sizeof(size_t) == 8) { \
  71. uint64_t T = UINT64_C(0xe100000000000000) & (0 - (V.lo & 1)); \
  72. V.lo = (V.hi << 63) | (V.lo >> 1); \
  73. V.hi = (V.hi >> 1) ^ T; \
  74. } else { \
  75. uint32_t T = 0xe1000000U & (0 - (uint32_t)(V.lo & 1)); \
  76. V.lo = (V.hi << 63) | (V.lo >> 1); \
  77. V.hi = (V.hi >> 1) ^ ((uint64_t)T << 32); \
  78. } \
  79. } while (0)
  80. // kSizeTWithoutLower4Bits is a mask that can be used to zero the lower four
  81. // bits of a |size_t|.
  82. static const size_t kSizeTWithoutLower4Bits = (size_t) -16;
  83. static void gcm_init_4bit(u128 Htable[16], uint64_t H[2]) {
  84. u128 V;
  85. Htable[0].hi = 0;
  86. Htable[0].lo = 0;
  87. V.hi = H[0];
  88. V.lo = H[1];
  89. Htable[8] = V;
  90. REDUCE1BIT(V);
  91. Htable[4] = V;
  92. REDUCE1BIT(V);
  93. Htable[2] = V;
  94. REDUCE1BIT(V);
  95. Htable[1] = V;
  96. Htable[3].hi = V.hi ^ Htable[2].hi, Htable[3].lo = V.lo ^ Htable[2].lo;
  97. V = Htable[4];
  98. Htable[5].hi = V.hi ^ Htable[1].hi, Htable[5].lo = V.lo ^ Htable[1].lo;
  99. Htable[6].hi = V.hi ^ Htable[2].hi, Htable[6].lo = V.lo ^ Htable[2].lo;
  100. Htable[7].hi = V.hi ^ Htable[3].hi, Htable[7].lo = V.lo ^ Htable[3].lo;
  101. V = Htable[8];
  102. Htable[9].hi = V.hi ^ Htable[1].hi, Htable[9].lo = V.lo ^ Htable[1].lo;
  103. Htable[10].hi = V.hi ^ Htable[2].hi, Htable[10].lo = V.lo ^ Htable[2].lo;
  104. Htable[11].hi = V.hi ^ Htable[3].hi, Htable[11].lo = V.lo ^ Htable[3].lo;
  105. Htable[12].hi = V.hi ^ Htable[4].hi, Htable[12].lo = V.lo ^ Htable[4].lo;
  106. Htable[13].hi = V.hi ^ Htable[5].hi, Htable[13].lo = V.lo ^ Htable[5].lo;
  107. Htable[14].hi = V.hi ^ Htable[6].hi, Htable[14].lo = V.lo ^ Htable[6].lo;
  108. Htable[15].hi = V.hi ^ Htable[7].hi, Htable[15].lo = V.lo ^ Htable[7].lo;
  109. #if defined(GHASH_ASM) && defined(OPENSSL_ARM)
  110. /* ARM assembler expects specific dword order in Htable. */
  111. {
  112. int j;
  113. const union {
  114. long one;
  115. char little;
  116. } is_endian = {1};
  117. if (is_endian.little) {
  118. for (j = 0; j < 16; ++j) {
  119. V = Htable[j];
  120. Htable[j].hi = V.lo;
  121. Htable[j].lo = V.hi;
  122. }
  123. } else {
  124. for (j = 0; j < 16; ++j) {
  125. V = Htable[j];
  126. Htable[j].hi = V.lo << 32 | V.lo >> 32;
  127. Htable[j].lo = V.hi << 32 | V.hi >> 32;
  128. }
  129. }
  130. }
  131. #endif
  132. }
  133. #if !defined(GHASH_ASM) || defined(OPENSSL_AARCH64)
  134. static const size_t rem_4bit[16] = {
  135. PACK(0x0000), PACK(0x1C20), PACK(0x3840), PACK(0x2460),
  136. PACK(0x7080), PACK(0x6CA0), PACK(0x48C0), PACK(0x54E0),
  137. PACK(0xE100), PACK(0xFD20), PACK(0xD940), PACK(0xC560),
  138. PACK(0x9180), PACK(0x8DA0), PACK(0xA9C0), PACK(0xB5E0)};
  139. static void gcm_gmult_4bit(uint64_t Xi[2], const u128 Htable[16]) {
  140. u128 Z;
  141. int cnt = 15;
  142. size_t rem, nlo, nhi;
  143. const union {
  144. long one;
  145. char little;
  146. } is_endian = {1};
  147. nlo = ((const uint8_t *)Xi)[15];
  148. nhi = nlo >> 4;
  149. nlo &= 0xf;
  150. Z.hi = Htable[nlo].hi;
  151. Z.lo = Htable[nlo].lo;
  152. while (1) {
  153. rem = (size_t)Z.lo & 0xf;
  154. Z.lo = (Z.hi << 60) | (Z.lo >> 4);
  155. Z.hi = (Z.hi >> 4);
  156. if (sizeof(size_t) == 8) {
  157. Z.hi ^= rem_4bit[rem];
  158. } else {
  159. Z.hi ^= (uint64_t)rem_4bit[rem] << 32;
  160. }
  161. Z.hi ^= Htable[nhi].hi;
  162. Z.lo ^= Htable[nhi].lo;
  163. if (--cnt < 0) {
  164. break;
  165. }
  166. nlo = ((const uint8_t *)Xi)[cnt];
  167. nhi = nlo >> 4;
  168. nlo &= 0xf;
  169. rem = (size_t)Z.lo & 0xf;
  170. Z.lo = (Z.hi << 60) | (Z.lo >> 4);
  171. Z.hi = (Z.hi >> 4);
  172. if (sizeof(size_t) == 8) {
  173. Z.hi ^= rem_4bit[rem];
  174. } else {
  175. Z.hi ^= (uint64_t)rem_4bit[rem] << 32;
  176. }
  177. Z.hi ^= Htable[nlo].hi;
  178. Z.lo ^= Htable[nlo].lo;
  179. }
  180. if (is_endian.little) {
  181. #ifdef BSWAP8
  182. Xi[0] = BSWAP8(Z.hi);
  183. Xi[1] = BSWAP8(Z.lo);
  184. #else
  185. uint8_t *p = (uint8_t *)Xi;
  186. uint32_t v;
  187. v = (uint32_t)(Z.hi >> 32);
  188. PUTU32(p, v);
  189. v = (uint32_t)(Z.hi);
  190. PUTU32(p + 4, v);
  191. v = (uint32_t)(Z.lo >> 32);
  192. PUTU32(p + 8, v);
  193. v = (uint32_t)(Z.lo);
  194. PUTU32(p + 12, v);
  195. #endif
  196. } else {
  197. Xi[0] = Z.hi;
  198. Xi[1] = Z.lo;
  199. }
  200. }
  201. /* Streamed gcm_mult_4bit, see CRYPTO_gcm128_[en|de]crypt for
  202. * details... Compiler-generated code doesn't seem to give any
  203. * performance improvement, at least not on x86[_64]. It's here
  204. * mostly as reference and a placeholder for possible future
  205. * non-trivial optimization[s]... */
  206. static void gcm_ghash_4bit(uint64_t Xi[2], const u128 Htable[16], const uint8_t *inp,
  207. size_t len) {
  208. u128 Z;
  209. int cnt;
  210. size_t rem, nlo, nhi;
  211. const union {
  212. long one;
  213. char little;
  214. } is_endian = {1};
  215. do {
  216. cnt = 15;
  217. nlo = ((const uint8_t *)Xi)[15];
  218. nlo ^= inp[15];
  219. nhi = nlo >> 4;
  220. nlo &= 0xf;
  221. Z.hi = Htable[nlo].hi;
  222. Z.lo = Htable[nlo].lo;
  223. while (1) {
  224. rem = (size_t)Z.lo & 0xf;
  225. Z.lo = (Z.hi << 60) | (Z.lo >> 4);
  226. Z.hi = (Z.hi >> 4);
  227. if (sizeof(size_t) == 8) {
  228. Z.hi ^= rem_4bit[rem];
  229. } else {
  230. Z.hi ^= (uint64_t)rem_4bit[rem] << 32;
  231. }
  232. Z.hi ^= Htable[nhi].hi;
  233. Z.lo ^= Htable[nhi].lo;
  234. if (--cnt < 0) {
  235. break;
  236. }
  237. nlo = ((const uint8_t *)Xi)[cnt];
  238. nlo ^= inp[cnt];
  239. nhi = nlo >> 4;
  240. nlo &= 0xf;
  241. rem = (size_t)Z.lo & 0xf;
  242. Z.lo = (Z.hi << 60) | (Z.lo >> 4);
  243. Z.hi = (Z.hi >> 4);
  244. if (sizeof(size_t) == 8) {
  245. Z.hi ^= rem_4bit[rem];
  246. } else {
  247. Z.hi ^= (uint64_t)rem_4bit[rem] << 32;
  248. }
  249. Z.hi ^= Htable[nlo].hi;
  250. Z.lo ^= Htable[nlo].lo;
  251. }
  252. if (is_endian.little) {
  253. #ifdef BSWAP8
  254. Xi[0] = BSWAP8(Z.hi);
  255. Xi[1] = BSWAP8(Z.lo);
  256. #else
  257. uint8_t *p = (uint8_t *)Xi;
  258. uint32_t v;
  259. v = (uint32_t)(Z.hi >> 32);
  260. PUTU32(p, v);
  261. v = (uint32_t)(Z.hi);
  262. PUTU32(p + 4, v);
  263. v = (uint32_t)(Z.lo >> 32);
  264. PUTU32(p + 8, v);
  265. v = (uint32_t)(Z.lo);
  266. PUTU32(p + 12, v);
  267. #endif
  268. } else {
  269. Xi[0] = Z.hi;
  270. Xi[1] = Z.lo;
  271. }
  272. } while (inp += 16, len -= 16);
  273. }
  274. #else /* GHASH_ASM */
  275. void gcm_gmult_4bit(uint64_t Xi[2], const u128 Htable[16]);
  276. void gcm_ghash_4bit(uint64_t Xi[2], const u128 Htable[16], const uint8_t *inp,
  277. size_t len);
  278. #endif
  279. #define GCM_MUL(ctx, Xi) gcm_gmult_4bit(ctx->Xi.u, ctx->Htable)
  280. #if defined(GHASH_ASM)
  281. #define GHASH(ctx, in, len) gcm_ghash_4bit((ctx)->Xi.u, (ctx)->Htable, in, len)
  282. /* GHASH_CHUNK is "stride parameter" missioned to mitigate cache
  283. * trashing effect. In other words idea is to hash data while it's
  284. * still in L1 cache after encryption pass... */
  285. #define GHASH_CHUNK (3 * 1024)
  286. #endif
  287. #if defined(GHASH_ASM)
  288. #if defined(OPENSSL_X86) || defined(OPENSSL_X86_64)
  289. #define GHASH_ASM_X86_OR_64
  290. #define GCM_FUNCREF_4BIT
  291. void gcm_init_clmul(u128 Htable[16], const uint64_t Xi[2]);
  292. void gcm_gmult_clmul(uint64_t Xi[2], const u128 Htable[16]);
  293. void gcm_ghash_clmul(uint64_t Xi[2], const u128 Htable[16], const uint8_t *inp,
  294. size_t len);
  295. #if defined(OPENSSL_X86)
  296. #define gcm_init_avx gcm_init_clmul
  297. #define gcm_gmult_avx gcm_gmult_clmul
  298. #define gcm_ghash_avx gcm_ghash_clmul
  299. #else
  300. void gcm_init_avx(u128 Htable[16], const uint64_t Xi[2]);
  301. void gcm_gmult_avx(uint64_t Xi[2], const u128 Htable[16]);
  302. void gcm_ghash_avx(uint64_t Xi[2], const u128 Htable[16], const uint8_t *in,
  303. size_t len);
  304. #define AESNI_GCM
  305. static int aesni_gcm_enabled(GCM128_CONTEXT *ctx, ctr128_f stream) {
  306. return stream == aesni_ctr32_encrypt_blocks &&
  307. ctx->ghash == gcm_ghash_avx;
  308. }
  309. size_t aesni_gcm_encrypt(const uint8_t *in, uint8_t *out, size_t len,
  310. const void *key, uint8_t ivec[16], uint64_t *Xi);
  311. size_t aesni_gcm_decrypt(const uint8_t *in, uint8_t *out, size_t len,
  312. const void *key, uint8_t ivec[16], uint64_t *Xi);
  313. #endif
  314. #if defined(OPENSSL_X86)
  315. #define GHASH_ASM_X86
  316. void gcm_gmult_4bit_mmx(uint64_t Xi[2], const u128 Htable[16]);
  317. void gcm_ghash_4bit_mmx(uint64_t Xi[2], const u128 Htable[16], const uint8_t *inp,
  318. size_t len);
  319. void gcm_gmult_4bit_x86(uint64_t Xi[2], const u128 Htable[16]);
  320. void gcm_ghash_4bit_x86(uint64_t Xi[2], const u128 Htable[16], const uint8_t *inp,
  321. size_t len);
  322. #endif
  323. #elif defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64)
  324. #include <openssl/arm_arch.h>
  325. #if __ARM_ARCH__ >= 7
  326. #define GHASH_ASM_ARM
  327. #define GCM_FUNCREF_4BIT
  328. static int pmull_capable(void) {
  329. return CRYPTO_is_ARMv8_PMULL_capable();
  330. }
  331. void gcm_init_v8(u128 Htable[16], const uint64_t Xi[2]);
  332. void gcm_gmult_v8(uint64_t Xi[2], const u128 Htable[16]);
  333. void gcm_ghash_v8(uint64_t Xi[2], const u128 Htable[16], const uint8_t *inp,
  334. size_t len);
  335. #if defined(OPENSSL_ARM)
  336. /* 32-bit ARM also has support for doing GCM with NEON instructions. */
  337. static int neon_capable(void) {
  338. return CRYPTO_is_NEON_capable();
  339. }
  340. void gcm_init_neon(u128 Htable[16], const uint64_t Xi[2]);
  341. void gcm_gmult_neon(uint64_t Xi[2], const u128 Htable[16]);
  342. void gcm_ghash_neon(uint64_t Xi[2], const u128 Htable[16], const uint8_t *inp,
  343. size_t len);
  344. #else
  345. /* AArch64 only has the ARMv8 versions of functions. */
  346. static int neon_capable(void) {
  347. return 0;
  348. }
  349. static void gcm_init_neon(u128 Htable[16], const uint64_t Xi[2]) {
  350. abort();
  351. }
  352. static void gcm_gmult_neon(uint64_t Xi[2], const u128 Htable[16]) {
  353. abort();
  354. }
  355. static void gcm_ghash_neon(uint64_t Xi[2], const u128 Htable[16],
  356. const uint8_t *inp, size_t len) {
  357. abort();
  358. }
  359. #endif
  360. #endif
  361. #endif
  362. #endif
  363. #ifdef GCM_FUNCREF_4BIT
  364. #undef GCM_MUL
  365. #define GCM_MUL(ctx, Xi) (*gcm_gmult_p)(ctx->Xi.u, ctx->Htable)
  366. #ifdef GHASH
  367. #undef GHASH
  368. #define GHASH(ctx, in, len) (*gcm_ghash_p)(ctx->Xi.u, ctx->Htable, in, len)
  369. #endif
  370. #endif
  371. void CRYPTO_gcm128_init(GCM128_CONTEXT *ctx, const void *key,
  372. block128_f block) {
  373. const union {
  374. long one;
  375. char little;
  376. } is_endian = {1};
  377. memset(ctx, 0, sizeof(*ctx));
  378. ctx->block = block;
  379. (*block)(ctx->H.c, ctx->H.c, key);
  380. if (is_endian.little) {
  381. /* H is stored in host byte order */
  382. #ifdef BSWAP8
  383. ctx->H.u[0] = BSWAP8(ctx->H.u[0]);
  384. ctx->H.u[1] = BSWAP8(ctx->H.u[1]);
  385. #else
  386. uint8_t *p = ctx->H.c;
  387. uint64_t hi, lo;
  388. hi = (uint64_t)GETU32(p) << 32 | GETU32(p + 4);
  389. lo = (uint64_t)GETU32(p + 8) << 32 | GETU32(p + 12);
  390. ctx->H.u[0] = hi;
  391. ctx->H.u[1] = lo;
  392. #endif
  393. }
  394. #if defined(GHASH_ASM_X86_OR_64)
  395. if (crypto_gcm_clmul_enabled()) {
  396. if (((OPENSSL_ia32cap_P[1] >> 22) & 0x41) == 0x41) { /* AVX+MOVBE */
  397. gcm_init_avx(ctx->Htable, ctx->H.u);
  398. ctx->gmult = gcm_gmult_avx;
  399. ctx->ghash = gcm_ghash_avx;
  400. } else {
  401. gcm_init_clmul(ctx->Htable, ctx->H.u);
  402. ctx->gmult = gcm_gmult_clmul;
  403. ctx->ghash = gcm_ghash_clmul;
  404. }
  405. return;
  406. }
  407. gcm_init_4bit(ctx->Htable, ctx->H.u);
  408. #if defined(GHASH_ASM_X86) /* x86 only */
  409. if (OPENSSL_ia32cap_P[0] & (1 << 25)) { /* check SSE bit */
  410. ctx->gmult = gcm_gmult_4bit_mmx;
  411. ctx->ghash = gcm_ghash_4bit_mmx;
  412. } else {
  413. ctx->gmult = gcm_gmult_4bit_x86;
  414. ctx->ghash = gcm_ghash_4bit_x86;
  415. }
  416. #else
  417. ctx->gmult = gcm_gmult_4bit;
  418. ctx->ghash = gcm_ghash_4bit;
  419. #endif
  420. #elif defined(GHASH_ASM_ARM)
  421. if (pmull_capable()) {
  422. gcm_init_v8(ctx->Htable, ctx->H.u);
  423. ctx->gmult = gcm_gmult_v8;
  424. ctx->ghash = gcm_ghash_v8;
  425. } else if (neon_capable()) {
  426. gcm_init_neon(ctx->Htable,ctx->H.u);
  427. ctx->gmult = gcm_gmult_neon;
  428. ctx->ghash = gcm_ghash_neon;
  429. } else {
  430. gcm_init_4bit(ctx->Htable, ctx->H.u);
  431. ctx->gmult = gcm_gmult_4bit;
  432. ctx->ghash = gcm_ghash_4bit;
  433. }
  434. #else
  435. gcm_init_4bit(ctx->Htable, ctx->H.u);
  436. ctx->gmult = gcm_gmult_4bit;
  437. ctx->ghash = gcm_ghash_4bit;
  438. #endif
  439. }
  440. void CRYPTO_gcm128_setiv(GCM128_CONTEXT *ctx, const void *key,
  441. const uint8_t *iv, size_t len) {
  442. const union {
  443. long one;
  444. char little;
  445. } is_endian = {1};
  446. unsigned int ctr;
  447. #ifdef GCM_FUNCREF_4BIT
  448. void (*gcm_gmult_p)(uint64_t Xi[2], const u128 Htable[16]) = ctx->gmult;
  449. #endif
  450. ctx->Yi.u[0] = 0;
  451. ctx->Yi.u[1] = 0;
  452. ctx->Xi.u[0] = 0;
  453. ctx->Xi.u[1] = 0;
  454. ctx->len.u[0] = 0; /* AAD length */
  455. ctx->len.u[1] = 0; /* message length */
  456. ctx->ares = 0;
  457. ctx->mres = 0;
  458. if (len == 12) {
  459. memcpy(ctx->Yi.c, iv, 12);
  460. ctx->Yi.c[15] = 1;
  461. ctr = 1;
  462. } else {
  463. size_t i;
  464. uint64_t len0 = len;
  465. while (len >= 16) {
  466. for (i = 0; i < 16; ++i) {
  467. ctx->Yi.c[i] ^= iv[i];
  468. }
  469. GCM_MUL(ctx, Yi);
  470. iv += 16;
  471. len -= 16;
  472. }
  473. if (len) {
  474. for (i = 0; i < len; ++i) {
  475. ctx->Yi.c[i] ^= iv[i];
  476. }
  477. GCM_MUL(ctx, Yi);
  478. }
  479. len0 <<= 3;
  480. if (is_endian.little) {
  481. #ifdef BSWAP8
  482. ctx->Yi.u[1] ^= BSWAP8(len0);
  483. #else
  484. ctx->Yi.c[8] ^= (uint8_t)(len0 >> 56);
  485. ctx->Yi.c[9] ^= (uint8_t)(len0 >> 48);
  486. ctx->Yi.c[10] ^= (uint8_t)(len0 >> 40);
  487. ctx->Yi.c[11] ^= (uint8_t)(len0 >> 32);
  488. ctx->Yi.c[12] ^= (uint8_t)(len0 >> 24);
  489. ctx->Yi.c[13] ^= (uint8_t)(len0 >> 16);
  490. ctx->Yi.c[14] ^= (uint8_t)(len0 >> 8);
  491. ctx->Yi.c[15] ^= (uint8_t)(len0);
  492. #endif
  493. } else {
  494. ctx->Yi.u[1] ^= len0;
  495. }
  496. GCM_MUL(ctx, Yi);
  497. if (is_endian.little) {
  498. ctr = GETU32(ctx->Yi.c + 12);
  499. } else {
  500. ctr = ctx->Yi.d[3];
  501. }
  502. }
  503. (*ctx->block)(ctx->Yi.c, ctx->EK0.c, key);
  504. ++ctr;
  505. if (is_endian.little) {
  506. PUTU32(ctx->Yi.c + 12, ctr);
  507. } else {
  508. ctx->Yi.d[3] = ctr;
  509. }
  510. }
  511. int CRYPTO_gcm128_aad(GCM128_CONTEXT *ctx, const uint8_t *aad, size_t len) {
  512. size_t i;
  513. unsigned int n;
  514. uint64_t alen = ctx->len.u[0];
  515. #ifdef GCM_FUNCREF_4BIT
  516. void (*gcm_gmult_p)(uint64_t Xi[2], const u128 Htable[16]) = ctx->gmult;
  517. #ifdef GHASH
  518. void (*gcm_ghash_p)(uint64_t Xi[2], const u128 Htable[16], const uint8_t *inp,
  519. size_t len) = ctx->ghash;
  520. #endif
  521. #endif
  522. if (ctx->len.u[1]) {
  523. return 0;
  524. }
  525. alen += len;
  526. if (alen > (UINT64_C(1) << 61) || (sizeof(len) == 8 && alen < len)) {
  527. return 0;
  528. }
  529. ctx->len.u[0] = alen;
  530. n = ctx->ares;
  531. if (n) {
  532. while (n && len) {
  533. ctx->Xi.c[n] ^= *(aad++);
  534. --len;
  535. n = (n + 1) % 16;
  536. }
  537. if (n == 0) {
  538. GCM_MUL(ctx, Xi);
  539. } else {
  540. ctx->ares = n;
  541. return 1;
  542. }
  543. }
  544. #ifdef GHASH
  545. i = len & kSizeTWithoutLower4Bits;
  546. if (i != 0) {
  547. GHASH(ctx, aad, i);
  548. aad += i;
  549. len -= i;
  550. }
  551. #else
  552. while (len >= 16) {
  553. for (i = 0; i < 16; ++i) {
  554. ctx->Xi.c[i] ^= aad[i];
  555. }
  556. GCM_MUL(ctx, Xi);
  557. aad += 16;
  558. len -= 16;
  559. }
  560. #endif
  561. if (len) {
  562. n = (unsigned int)len;
  563. for (i = 0; i < len; ++i) {
  564. ctx->Xi.c[i] ^= aad[i];
  565. }
  566. }
  567. ctx->ares = n;
  568. return 1;
  569. }
  570. int CRYPTO_gcm128_encrypt(GCM128_CONTEXT *ctx, const void *key,
  571. const unsigned char *in, unsigned char *out,
  572. size_t len) {
  573. const union {
  574. long one;
  575. char little;
  576. } is_endian = {1};
  577. unsigned int n, ctr;
  578. size_t i;
  579. uint64_t mlen = ctx->len.u[1];
  580. block128_f block = ctx->block;
  581. #ifdef GCM_FUNCREF_4BIT
  582. void (*gcm_gmult_p)(uint64_t Xi[2], const u128 Htable[16]) = ctx->gmult;
  583. #ifdef GHASH
  584. void (*gcm_ghash_p)(uint64_t Xi[2], const u128 Htable[16], const uint8_t *inp,
  585. size_t len) = ctx->ghash;
  586. #endif
  587. #endif
  588. mlen += len;
  589. if (mlen > ((UINT64_C(1) << 36) - 32) ||
  590. (sizeof(len) == 8 && mlen < len)) {
  591. return 0;
  592. }
  593. ctx->len.u[1] = mlen;
  594. if (ctx->ares) {
  595. /* First call to encrypt finalizes GHASH(AAD) */
  596. GCM_MUL(ctx, Xi);
  597. ctx->ares = 0;
  598. }
  599. if (is_endian.little) {
  600. ctr = GETU32(ctx->Yi.c + 12);
  601. } else {
  602. ctr = ctx->Yi.d[3];
  603. }
  604. n = ctx->mres;
  605. if (n) {
  606. while (n && len) {
  607. ctx->Xi.c[n] ^= *(out++) = *(in++) ^ ctx->EKi.c[n];
  608. --len;
  609. n = (n + 1) % 16;
  610. }
  611. if (n == 0) {
  612. GCM_MUL(ctx, Xi);
  613. } else {
  614. ctx->mres = n;
  615. return 1;
  616. }
  617. }
  618. if (STRICT_ALIGNMENT && ((size_t)in | (size_t)out) % sizeof(size_t) != 0) {
  619. for (i = 0; i < len; ++i) {
  620. if (n == 0) {
  621. (*block)(ctx->Yi.c, ctx->EKi.c, key);
  622. ++ctr;
  623. if (is_endian.little) {
  624. PUTU32(ctx->Yi.c + 12, ctr);
  625. } else {
  626. ctx->Yi.d[3] = ctr;
  627. }
  628. }
  629. ctx->Xi.c[n] ^= out[i] = in[i] ^ ctx->EKi.c[n];
  630. n = (n + 1) % 16;
  631. if (n == 0) {
  632. GCM_MUL(ctx, Xi);
  633. }
  634. }
  635. ctx->mres = n;
  636. return 1;
  637. }
  638. #if defined(GHASH) && defined(GHASH_CHUNK)
  639. while (len >= GHASH_CHUNK) {
  640. size_t j = GHASH_CHUNK;
  641. while (j) {
  642. size_t *out_t = (size_t *)out;
  643. const size_t *in_t = (const size_t *)in;
  644. (*block)(ctx->Yi.c, ctx->EKi.c, key);
  645. ++ctr;
  646. if (is_endian.little) {
  647. PUTU32(ctx->Yi.c + 12, ctr);
  648. } else {
  649. ctx->Yi.d[3] = ctr;
  650. }
  651. for (i = 0; i < 16 / sizeof(size_t); ++i) {
  652. out_t[i] = in_t[i] ^ ctx->EKi.t[i];
  653. }
  654. out += 16;
  655. in += 16;
  656. j -= 16;
  657. }
  658. GHASH(ctx, out - GHASH_CHUNK, GHASH_CHUNK);
  659. len -= GHASH_CHUNK;
  660. }
  661. if ((i = (len & (size_t) - 16))) {
  662. size_t j = i;
  663. while (len >= 16) {
  664. size_t *out_t = (size_t *)out;
  665. const size_t *in_t = (const size_t *)in;
  666. (*block)(ctx->Yi.c, ctx->EKi.c, key);
  667. ++ctr;
  668. if (is_endian.little) {
  669. PUTU32(ctx->Yi.c + 12, ctr);
  670. } else {
  671. ctx->Yi.d[3] = ctr;
  672. }
  673. for (i = 0; i < 16 / sizeof(size_t); ++i) {
  674. out_t[i] = in_t[i] ^ ctx->EKi.t[i];
  675. }
  676. out += 16;
  677. in += 16;
  678. len -= 16;
  679. }
  680. GHASH(ctx, out - j, j);
  681. }
  682. #else
  683. while (len >= 16) {
  684. size_t *out_t = (size_t *)out;
  685. const size_t *in_t = (const size_t *)in;
  686. (*block)(ctx->Yi.c, ctx->EKi.c, key);
  687. ++ctr;
  688. if (is_endian.little) {
  689. PUTU32(ctx->Yi.c + 12, ctr);
  690. } else {
  691. ctx->Yi.d[3] = ctr;
  692. }
  693. for (i = 0; i < 16 / sizeof(size_t); ++i) {
  694. ctx->Xi.t[i] ^= out_t[i] = in_t[i] ^ ctx->EKi.t[i];
  695. }
  696. GCM_MUL(ctx, Xi);
  697. out += 16;
  698. in += 16;
  699. len -= 16;
  700. }
  701. #endif
  702. if (len) {
  703. (*block)(ctx->Yi.c, ctx->EKi.c, key);
  704. ++ctr;
  705. if (is_endian.little) {
  706. PUTU32(ctx->Yi.c + 12, ctr);
  707. } else {
  708. ctx->Yi.d[3] = ctr;
  709. }
  710. while (len--) {
  711. ctx->Xi.c[n] ^= out[n] = in[n] ^ ctx->EKi.c[n];
  712. ++n;
  713. }
  714. }
  715. ctx->mres = n;
  716. return 1;
  717. }
  718. int CRYPTO_gcm128_decrypt(GCM128_CONTEXT *ctx, const void *key,
  719. const unsigned char *in, unsigned char *out,
  720. size_t len) {
  721. const union {
  722. long one;
  723. char little;
  724. } is_endian = {1};
  725. unsigned int n, ctr;
  726. size_t i;
  727. uint64_t mlen = ctx->len.u[1];
  728. block128_f block = ctx->block;
  729. #ifdef GCM_FUNCREF_4BIT
  730. void (*gcm_gmult_p)(uint64_t Xi[2], const u128 Htable[16]) = ctx->gmult;
  731. #ifdef GHASH
  732. void (*gcm_ghash_p)(uint64_t Xi[2], const u128 Htable[16], const uint8_t *inp,
  733. size_t len) = ctx->ghash;
  734. #endif
  735. #endif
  736. mlen += len;
  737. if (mlen > ((UINT64_C(1) << 36) - 32) ||
  738. (sizeof(len) == 8 && mlen < len)) {
  739. return 0;
  740. }
  741. ctx->len.u[1] = mlen;
  742. if (ctx->ares) {
  743. /* First call to decrypt finalizes GHASH(AAD) */
  744. GCM_MUL(ctx, Xi);
  745. ctx->ares = 0;
  746. }
  747. if (is_endian.little) {
  748. ctr = GETU32(ctx->Yi.c + 12);
  749. } else {
  750. ctr = ctx->Yi.d[3];
  751. }
  752. n = ctx->mres;
  753. if (n) {
  754. while (n && len) {
  755. uint8_t c = *(in++);
  756. *(out++) = c ^ ctx->EKi.c[n];
  757. ctx->Xi.c[n] ^= c;
  758. --len;
  759. n = (n + 1) % 16;
  760. }
  761. if (n == 0) {
  762. GCM_MUL(ctx, Xi);
  763. } else {
  764. ctx->mres = n;
  765. return 1;
  766. }
  767. }
  768. if (STRICT_ALIGNMENT && ((size_t)in | (size_t)out) % sizeof(size_t) != 0) {
  769. for (i = 0; i < len; ++i) {
  770. uint8_t c;
  771. if (n == 0) {
  772. (*block)(ctx->Yi.c, ctx->EKi.c, key);
  773. ++ctr;
  774. if (is_endian.little) {
  775. PUTU32(ctx->Yi.c + 12, ctr);
  776. } else {
  777. ctx->Yi.d[3] = ctr;
  778. }
  779. }
  780. c = in[i];
  781. out[i] = c ^ ctx->EKi.c[n];
  782. ctx->Xi.c[n] ^= c;
  783. n = (n + 1) % 16;
  784. if (n == 0) {
  785. GCM_MUL(ctx, Xi);
  786. }
  787. }
  788. ctx->mres = n;
  789. return 1;
  790. }
  791. #if defined(GHASH) && defined(GHASH_CHUNK)
  792. while (len >= GHASH_CHUNK) {
  793. size_t j = GHASH_CHUNK;
  794. GHASH(ctx, in, GHASH_CHUNK);
  795. while (j) {
  796. size_t *out_t = (size_t *)out;
  797. const size_t *in_t = (const size_t *)in;
  798. (*block)(ctx->Yi.c, ctx->EKi.c, key);
  799. ++ctr;
  800. if (is_endian.little) {
  801. PUTU32(ctx->Yi.c + 12, ctr);
  802. } else {
  803. ctx->Yi.d[3] = ctr;
  804. }
  805. for (i = 0; i < 16 / sizeof(size_t); ++i) {
  806. out_t[i] = in_t[i] ^ ctx->EKi.t[i];
  807. }
  808. out += 16;
  809. in += 16;
  810. j -= 16;
  811. }
  812. len -= GHASH_CHUNK;
  813. }
  814. i = len & kSizeTWithoutLower4Bits;
  815. if (i != 0) {
  816. GHASH(ctx, in, i);
  817. while (len >= 16) {
  818. size_t *out_t = (size_t *)out;
  819. const size_t *in_t = (const size_t *)in;
  820. (*block)(ctx->Yi.c, ctx->EKi.c, key);
  821. ++ctr;
  822. if (is_endian.little) {
  823. PUTU32(ctx->Yi.c + 12, ctr);
  824. } else {
  825. ctx->Yi.d[3] = ctr;
  826. }
  827. for (i = 0; i < 16 / sizeof(size_t); ++i) {
  828. out_t[i] = in_t[i] ^ ctx->EKi.t[i];
  829. }
  830. out += 16;
  831. in += 16;
  832. len -= 16;
  833. }
  834. }
  835. #else
  836. while (len >= 16) {
  837. size_t *out_t = (size_t *)out;
  838. const size_t *in_t = (const size_t *)in;
  839. (*block)(ctx->Yi.c, ctx->EKi.c, key);
  840. ++ctr;
  841. if (is_endian.little) {
  842. PUTU32(ctx->Yi.c + 12, ctr);
  843. } else {
  844. ctx->Yi.d[3] = ctr;
  845. }
  846. for (i = 0; i < 16 / sizeof(size_t); ++i) {
  847. size_t c = in_t[i];
  848. out_t[i] = c ^ ctx->EKi.t[i];
  849. ctx->Xi.t[i] ^= c;
  850. }
  851. GCM_MUL(ctx, Xi);
  852. out += 16;
  853. in += 16;
  854. len -= 16;
  855. }
  856. #endif
  857. if (len) {
  858. (*block)(ctx->Yi.c, ctx->EKi.c, key);
  859. ++ctr;
  860. if (is_endian.little) {
  861. PUTU32(ctx->Yi.c + 12, ctr);
  862. } else {
  863. ctx->Yi.d[3] = ctr;
  864. }
  865. while (len--) {
  866. uint8_t c = in[n];
  867. ctx->Xi.c[n] ^= c;
  868. out[n] = c ^ ctx->EKi.c[n];
  869. ++n;
  870. }
  871. }
  872. ctx->mres = n;
  873. return 1;
  874. }
  875. int CRYPTO_gcm128_encrypt_ctr32(GCM128_CONTEXT *ctx, const void *key,
  876. const uint8_t *in, uint8_t *out, size_t len,
  877. ctr128_f stream) {
  878. const union {
  879. long one;
  880. char little;
  881. } is_endian = {1};
  882. unsigned int n, ctr;
  883. uint64_t mlen = ctx->len.u[1];
  884. #ifdef GCM_FUNCREF_4BIT
  885. void (*gcm_gmult_p)(uint64_t Xi[2], const u128 Htable[16]) = ctx->gmult;
  886. #ifdef GHASH
  887. void (*gcm_ghash_p)(uint64_t Xi[2], const u128 Htable[16], const uint8_t *inp,
  888. size_t len) = ctx->ghash;
  889. #endif
  890. #endif
  891. mlen += len;
  892. if (mlen > ((UINT64_C(1) << 36) - 32) ||
  893. (sizeof(len) == 8 && mlen < len)) {
  894. return 0;
  895. }
  896. ctx->len.u[1] = mlen;
  897. if (ctx->ares) {
  898. /* First call to encrypt finalizes GHASH(AAD) */
  899. GCM_MUL(ctx, Xi);
  900. ctx->ares = 0;
  901. }
  902. n = ctx->mres;
  903. if (n) {
  904. while (n && len) {
  905. ctx->Xi.c[n] ^= *(out++) = *(in++) ^ ctx->EKi.c[n];
  906. --len;
  907. n = (n + 1) % 16;
  908. }
  909. if (n == 0) {
  910. GCM_MUL(ctx, Xi);
  911. } else {
  912. ctx->mres = n;
  913. return 1;
  914. }
  915. }
  916. #if defined(AESNI_GCM)
  917. if (aesni_gcm_enabled(ctx, stream)) {
  918. /* |aesni_gcm_encrypt| may not process all the input given to it. It may
  919. * not process *any* of its input if it is deemed too small. */
  920. size_t bulk = aesni_gcm_encrypt(in, out, len, key, ctx->Yi.c, ctx->Xi.u);
  921. in += bulk;
  922. out += bulk;
  923. len -= bulk;
  924. }
  925. #endif
  926. if (is_endian.little) {
  927. ctr = GETU32(ctx->Yi.c + 12);
  928. } else {
  929. ctr = ctx->Yi.d[3];
  930. }
  931. #if defined(GHASH)
  932. while (len >= GHASH_CHUNK) {
  933. (*stream)(in, out, GHASH_CHUNK / 16, key, ctx->Yi.c);
  934. ctr += GHASH_CHUNK / 16;
  935. if (is_endian.little) {
  936. PUTU32(ctx->Yi.c + 12, ctr);
  937. } else {
  938. ctx->Yi.d[3] = ctr;
  939. }
  940. GHASH(ctx, out, GHASH_CHUNK);
  941. out += GHASH_CHUNK;
  942. in += GHASH_CHUNK;
  943. len -= GHASH_CHUNK;
  944. }
  945. #endif
  946. size_t i = len & kSizeTWithoutLower4Bits;
  947. if (i != 0) {
  948. size_t j = i / 16;
  949. (*stream)(in, out, j, key, ctx->Yi.c);
  950. ctr += (unsigned int)j;
  951. if (is_endian.little) {
  952. PUTU32(ctx->Yi.c + 12, ctr);
  953. } else {
  954. ctx->Yi.d[3] = ctr;
  955. }
  956. in += i;
  957. len -= i;
  958. #if defined(GHASH)
  959. GHASH(ctx, out, i);
  960. out += i;
  961. #else
  962. while (j--) {
  963. for (i = 0; i < 16; ++i) {
  964. ctx->Xi.c[i] ^= out[i];
  965. }
  966. GCM_MUL(ctx, Xi);
  967. out += 16;
  968. }
  969. #endif
  970. }
  971. if (len) {
  972. (*ctx->block)(ctx->Yi.c, ctx->EKi.c, key);
  973. ++ctr;
  974. if (is_endian.little) {
  975. PUTU32(ctx->Yi.c + 12, ctr);
  976. } else {
  977. ctx->Yi.d[3] = ctr;
  978. }
  979. while (len--) {
  980. ctx->Xi.c[n] ^= out[n] = in[n] ^ ctx->EKi.c[n];
  981. ++n;
  982. }
  983. }
  984. ctx->mres = n;
  985. return 1;
  986. }
  987. int CRYPTO_gcm128_decrypt_ctr32(GCM128_CONTEXT *ctx, const void *key,
  988. const uint8_t *in, uint8_t *out, size_t len,
  989. ctr128_f stream) {
  990. const union {
  991. long one;
  992. char little;
  993. } is_endian = {1};
  994. unsigned int n, ctr;
  995. uint64_t mlen = ctx->len.u[1];
  996. #ifdef GCM_FUNCREF_4BIT
  997. void (*gcm_gmult_p)(uint64_t Xi[2], const u128 Htable[16]) = ctx->gmult;
  998. #ifdef GHASH
  999. void (*gcm_ghash_p)(uint64_t Xi[2], const u128 Htable[16], const uint8_t *inp,
  1000. size_t len) = ctx->ghash;
  1001. #endif
  1002. #endif
  1003. mlen += len;
  1004. if (mlen > ((UINT64_C(1) << 36) - 32) ||
  1005. (sizeof(len) == 8 && mlen < len)) {
  1006. return 0;
  1007. }
  1008. ctx->len.u[1] = mlen;
  1009. if (ctx->ares) {
  1010. /* First call to decrypt finalizes GHASH(AAD) */
  1011. GCM_MUL(ctx, Xi);
  1012. ctx->ares = 0;
  1013. }
  1014. n = ctx->mres;
  1015. if (n) {
  1016. while (n && len) {
  1017. uint8_t c = *(in++);
  1018. *(out++) = c ^ ctx->EKi.c[n];
  1019. ctx->Xi.c[n] ^= c;
  1020. --len;
  1021. n = (n + 1) % 16;
  1022. }
  1023. if (n == 0) {
  1024. GCM_MUL(ctx, Xi);
  1025. } else {
  1026. ctx->mres = n;
  1027. return 1;
  1028. }
  1029. }
  1030. #if defined(AESNI_GCM)
  1031. if (aesni_gcm_enabled(ctx, stream)) {
  1032. /* |aesni_gcm_decrypt| may not process all the input given to it. It may
  1033. * not process *any* of its input if it is deemed too small. */
  1034. size_t bulk = aesni_gcm_decrypt(in, out, len, key, ctx->Yi.c, ctx->Xi.u);
  1035. in += bulk;
  1036. out += bulk;
  1037. len -= bulk;
  1038. }
  1039. #endif
  1040. if (is_endian.little) {
  1041. ctr = GETU32(ctx->Yi.c + 12);
  1042. } else {
  1043. ctr = ctx->Yi.d[3];
  1044. }
  1045. #if defined(GHASH)
  1046. while (len >= GHASH_CHUNK) {
  1047. GHASH(ctx, in, GHASH_CHUNK);
  1048. (*stream)(in, out, GHASH_CHUNK / 16, key, ctx->Yi.c);
  1049. ctr += GHASH_CHUNK / 16;
  1050. if (is_endian.little) {
  1051. PUTU32(ctx->Yi.c + 12, ctr);
  1052. } else {
  1053. ctx->Yi.d[3] = ctr;
  1054. }
  1055. out += GHASH_CHUNK;
  1056. in += GHASH_CHUNK;
  1057. len -= GHASH_CHUNK;
  1058. }
  1059. #endif
  1060. size_t i = len & kSizeTWithoutLower4Bits;
  1061. if (i != 0) {
  1062. size_t j = i / 16;
  1063. #if defined(GHASH)
  1064. GHASH(ctx, in, i);
  1065. #else
  1066. while (j--) {
  1067. size_t k;
  1068. for (k = 0; k < 16; ++k) {
  1069. ctx->Xi.c[k] ^= in[k];
  1070. }
  1071. GCM_MUL(ctx, Xi);
  1072. in += 16;
  1073. }
  1074. j = i / 16;
  1075. in -= i;
  1076. #endif
  1077. (*stream)(in, out, j, key, ctx->Yi.c);
  1078. ctr += (unsigned int)j;
  1079. if (is_endian.little) {
  1080. PUTU32(ctx->Yi.c + 12, ctr);
  1081. } else {
  1082. ctx->Yi.d[3] = ctr;
  1083. }
  1084. out += i;
  1085. in += i;
  1086. len -= i;
  1087. }
  1088. if (len) {
  1089. (*ctx->block)(ctx->Yi.c, ctx->EKi.c, key);
  1090. ++ctr;
  1091. if (is_endian.little) {
  1092. PUTU32(ctx->Yi.c + 12, ctr);
  1093. } else {
  1094. ctx->Yi.d[3] = ctr;
  1095. }
  1096. while (len--) {
  1097. uint8_t c = in[n];
  1098. ctx->Xi.c[n] ^= c;
  1099. out[n] = c ^ ctx->EKi.c[n];
  1100. ++n;
  1101. }
  1102. }
  1103. ctx->mres = n;
  1104. return 1;
  1105. }
  1106. int CRYPTO_gcm128_finish(GCM128_CONTEXT *ctx, const uint8_t *tag, size_t len) {
  1107. const union {
  1108. long one;
  1109. char little;
  1110. } is_endian = {1};
  1111. uint64_t alen = ctx->len.u[0] << 3;
  1112. uint64_t clen = ctx->len.u[1] << 3;
  1113. #ifdef GCM_FUNCREF_4BIT
  1114. void (*gcm_gmult_p)(uint64_t Xi[2], const u128 Htable[16]) = ctx->gmult;
  1115. #endif
  1116. if (ctx->mres || ctx->ares) {
  1117. GCM_MUL(ctx, Xi);
  1118. }
  1119. if (is_endian.little) {
  1120. #ifdef BSWAP8
  1121. alen = BSWAP8(alen);
  1122. clen = BSWAP8(clen);
  1123. #else
  1124. uint8_t *p = ctx->len.c;
  1125. ctx->len.u[0] = alen;
  1126. ctx->len.u[1] = clen;
  1127. alen = (uint64_t)GETU32(p) << 32 | GETU32(p + 4);
  1128. clen = (uint64_t)GETU32(p + 8) << 32 | GETU32(p + 12);
  1129. #endif
  1130. }
  1131. ctx->Xi.u[0] ^= alen;
  1132. ctx->Xi.u[1] ^= clen;
  1133. GCM_MUL(ctx, Xi);
  1134. ctx->Xi.u[0] ^= ctx->EK0.u[0];
  1135. ctx->Xi.u[1] ^= ctx->EK0.u[1];
  1136. if (tag && len <= sizeof(ctx->Xi)) {
  1137. return CRYPTO_memcmp(ctx->Xi.c, tag, len) == 0;
  1138. } else {
  1139. return 0;
  1140. }
  1141. }
  1142. void CRYPTO_gcm128_tag(GCM128_CONTEXT *ctx, unsigned char *tag, size_t len) {
  1143. CRYPTO_gcm128_finish(ctx, NULL, 0);
  1144. memcpy(tag, ctx->Xi.c, len <= sizeof(ctx->Xi.c) ? len : sizeof(ctx->Xi.c));
  1145. }
  1146. #if defined(OPENSSL_X86) || defined(OPENSSL_X86_64)
  1147. int crypto_gcm_clmul_enabled(void) {
  1148. #ifdef GHASH_ASM
  1149. return OPENSSL_ia32cap_P[0] & (1 << 24) && /* check FXSR bit */
  1150. OPENSSL_ia32cap_P[1] & (1 << 1); /* check PCLMULQDQ bit */
  1151. #else
  1152. return 0;
  1153. #endif
  1154. }
  1155. #endif