25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

1271 satır
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. uint64_t len0 = len;
  464. while (len >= 16) {
  465. for (size_t i = 0; i < 16; ++i) {
  466. ctx->Yi.c[i] ^= iv[i];
  467. }
  468. GCM_MUL(ctx, Yi);
  469. iv += 16;
  470. len -= 16;
  471. }
  472. if (len) {
  473. for (size_t i = 0; i < len; ++i) {
  474. ctx->Yi.c[i] ^= iv[i];
  475. }
  476. GCM_MUL(ctx, Yi);
  477. }
  478. len0 <<= 3;
  479. if (is_endian.little) {
  480. #ifdef BSWAP8
  481. ctx->Yi.u[1] ^= BSWAP8(len0);
  482. #else
  483. ctx->Yi.c[8] ^= (uint8_t)(len0 >> 56);
  484. ctx->Yi.c[9] ^= (uint8_t)(len0 >> 48);
  485. ctx->Yi.c[10] ^= (uint8_t)(len0 >> 40);
  486. ctx->Yi.c[11] ^= (uint8_t)(len0 >> 32);
  487. ctx->Yi.c[12] ^= (uint8_t)(len0 >> 24);
  488. ctx->Yi.c[13] ^= (uint8_t)(len0 >> 16);
  489. ctx->Yi.c[14] ^= (uint8_t)(len0 >> 8);
  490. ctx->Yi.c[15] ^= (uint8_t)(len0);
  491. #endif
  492. } else {
  493. ctx->Yi.u[1] ^= len0;
  494. }
  495. GCM_MUL(ctx, Yi);
  496. if (is_endian.little) {
  497. ctr = GETU32(ctx->Yi.c + 12);
  498. } else {
  499. ctr = ctx->Yi.d[3];
  500. }
  501. }
  502. (*ctx->block)(ctx->Yi.c, ctx->EK0.c, key);
  503. ++ctr;
  504. if (is_endian.little) {
  505. PUTU32(ctx->Yi.c + 12, ctr);
  506. } else {
  507. ctx->Yi.d[3] = ctr;
  508. }
  509. }
  510. int CRYPTO_gcm128_aad(GCM128_CONTEXT *ctx, const uint8_t *aad, size_t len) {
  511. unsigned int n;
  512. uint64_t alen = ctx->len.u[0];
  513. #ifdef GCM_FUNCREF_4BIT
  514. void (*gcm_gmult_p)(uint64_t Xi[2], const u128 Htable[16]) = ctx->gmult;
  515. #ifdef GHASH
  516. void (*gcm_ghash_p)(uint64_t Xi[2], const u128 Htable[16], const uint8_t *inp,
  517. size_t len) = ctx->ghash;
  518. #endif
  519. #endif
  520. if (ctx->len.u[1]) {
  521. return 0;
  522. }
  523. alen += len;
  524. if (alen > (UINT64_C(1) << 61) || (sizeof(len) == 8 && alen < len)) {
  525. return 0;
  526. }
  527. ctx->len.u[0] = alen;
  528. n = ctx->ares;
  529. if (n) {
  530. while (n && len) {
  531. ctx->Xi.c[n] ^= *(aad++);
  532. --len;
  533. n = (n + 1) % 16;
  534. }
  535. if (n == 0) {
  536. GCM_MUL(ctx, Xi);
  537. } else {
  538. ctx->ares = n;
  539. return 1;
  540. }
  541. }
  542. /* Process a whole number of blocks. */
  543. #ifdef GHASH
  544. size_t len_blocks = len & kSizeTWithoutLower4Bits;
  545. if (len_blocks != 0) {
  546. GHASH(ctx, aad, len_blocks);
  547. aad += len_blocks;
  548. len -= len_blocks;
  549. }
  550. #else
  551. while (len >= 16) {
  552. for (size_t i = 0; i < 16; ++i) {
  553. ctx->Xi.c[i] ^= aad[i];
  554. }
  555. GCM_MUL(ctx, Xi);
  556. aad += 16;
  557. len -= 16;
  558. }
  559. #endif
  560. /* Process the remainder. */
  561. if (len != 0) {
  562. n = (unsigned int)len;
  563. for (size_t 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. uint64_t mlen = ctx->len.u[1];
  579. block128_f block = ctx->block;
  580. #ifdef GCM_FUNCREF_4BIT
  581. void (*gcm_gmult_p)(uint64_t Xi[2], const u128 Htable[16]) = ctx->gmult;
  582. #ifdef GHASH
  583. void (*gcm_ghash_p)(uint64_t Xi[2], const u128 Htable[16], const uint8_t *inp,
  584. size_t len) = ctx->ghash;
  585. #endif
  586. #endif
  587. mlen += len;
  588. if (mlen > ((UINT64_C(1) << 36) - 32) ||
  589. (sizeof(len) == 8 && mlen < len)) {
  590. return 0;
  591. }
  592. ctx->len.u[1] = mlen;
  593. if (ctx->ares) {
  594. /* First call to encrypt finalizes GHASH(AAD) */
  595. GCM_MUL(ctx, Xi);
  596. ctx->ares = 0;
  597. }
  598. if (is_endian.little) {
  599. ctr = GETU32(ctx->Yi.c + 12);
  600. } else {
  601. ctr = ctx->Yi.d[3];
  602. }
  603. n = ctx->mres;
  604. if (n) {
  605. while (n && len) {
  606. ctx->Xi.c[n] ^= *(out++) = *(in++) ^ ctx->EKi.c[n];
  607. --len;
  608. n = (n + 1) % 16;
  609. }
  610. if (n == 0) {
  611. GCM_MUL(ctx, Xi);
  612. } else {
  613. ctx->mres = n;
  614. return 1;
  615. }
  616. }
  617. if (STRICT_ALIGNMENT && ((size_t)in | (size_t)out) % sizeof(size_t) != 0) {
  618. for (size_t i = 0; i < len; ++i) {
  619. if (n == 0) {
  620. (*block)(ctx->Yi.c, ctx->EKi.c, key);
  621. ++ctr;
  622. if (is_endian.little) {
  623. PUTU32(ctx->Yi.c + 12, ctr);
  624. } else {
  625. ctx->Yi.d[3] = ctr;
  626. }
  627. }
  628. ctx->Xi.c[n] ^= out[i] = in[i] ^ ctx->EKi.c[n];
  629. n = (n + 1) % 16;
  630. if (n == 0) {
  631. GCM_MUL(ctx, Xi);
  632. }
  633. }
  634. ctx->mres = n;
  635. return 1;
  636. }
  637. #if defined(GHASH) && defined(GHASH_CHUNK)
  638. while (len >= GHASH_CHUNK) {
  639. size_t j = GHASH_CHUNK;
  640. while (j) {
  641. size_t *out_t = (size_t *)out;
  642. const size_t *in_t = (const size_t *)in;
  643. (*block)(ctx->Yi.c, ctx->EKi.c, key);
  644. ++ctr;
  645. if (is_endian.little) {
  646. PUTU32(ctx->Yi.c + 12, ctr);
  647. } else {
  648. ctx->Yi.d[3] = ctr;
  649. }
  650. for (size_t i = 0; i < 16 / sizeof(size_t); ++i) {
  651. out_t[i] = in_t[i] ^ ctx->EKi.t[i];
  652. }
  653. out += 16;
  654. in += 16;
  655. j -= 16;
  656. }
  657. GHASH(ctx, out - GHASH_CHUNK, GHASH_CHUNK);
  658. len -= GHASH_CHUNK;
  659. }
  660. size_t len_blocks = len & kSizeTWithoutLower4Bits;
  661. if (len_blocks != 0) {
  662. while (len >= 16) {
  663. size_t *out_t = (size_t *)out;
  664. const size_t *in_t = (const size_t *)in;
  665. (*block)(ctx->Yi.c, ctx->EKi.c, key);
  666. ++ctr;
  667. if (is_endian.little) {
  668. PUTU32(ctx->Yi.c + 12, ctr);
  669. } else {
  670. ctx->Yi.d[3] = ctr;
  671. }
  672. for (size_t i = 0; i < 16 / sizeof(size_t); ++i) {
  673. out_t[i] = in_t[i] ^ ctx->EKi.t[i];
  674. }
  675. out += 16;
  676. in += 16;
  677. len -= 16;
  678. }
  679. GHASH(ctx, out - len_blocks, len_blocks);
  680. }
  681. #else
  682. while (len >= 16) {
  683. size_t *out_t = (size_t *)out;
  684. const size_t *in_t = (const size_t *)in;
  685. (*block)(ctx->Yi.c, ctx->EKi.c, key);
  686. ++ctr;
  687. if (is_endian.little) {
  688. PUTU32(ctx->Yi.c + 12, ctr);
  689. } else {
  690. ctx->Yi.d[3] = ctr;
  691. }
  692. for (size_t i = 0; i < 16 / sizeof(size_t); ++i) {
  693. ctx->Xi.t[i] ^= out_t[i] = in_t[i] ^ ctx->EKi.t[i];
  694. }
  695. GCM_MUL(ctx, Xi);
  696. out += 16;
  697. in += 16;
  698. len -= 16;
  699. }
  700. #endif
  701. if (len) {
  702. (*block)(ctx->Yi.c, ctx->EKi.c, key);
  703. ++ctr;
  704. if (is_endian.little) {
  705. PUTU32(ctx->Yi.c + 12, ctr);
  706. } else {
  707. ctx->Yi.d[3] = ctr;
  708. }
  709. while (len--) {
  710. ctx->Xi.c[n] ^= out[n] = in[n] ^ ctx->EKi.c[n];
  711. ++n;
  712. }
  713. }
  714. ctx->mres = n;
  715. return 1;
  716. }
  717. int CRYPTO_gcm128_decrypt(GCM128_CONTEXT *ctx, const void *key,
  718. const unsigned char *in, unsigned char *out,
  719. size_t len) {
  720. const union {
  721. long one;
  722. char little;
  723. } is_endian = {1};
  724. unsigned int n, ctr;
  725. uint64_t mlen = ctx->len.u[1];
  726. block128_f block = ctx->block;
  727. #ifdef GCM_FUNCREF_4BIT
  728. void (*gcm_gmult_p)(uint64_t Xi[2], const u128 Htable[16]) = ctx->gmult;
  729. #ifdef GHASH
  730. void (*gcm_ghash_p)(uint64_t Xi[2], const u128 Htable[16], const uint8_t *inp,
  731. size_t len) = ctx->ghash;
  732. #endif
  733. #endif
  734. mlen += len;
  735. if (mlen > ((UINT64_C(1) << 36) - 32) ||
  736. (sizeof(len) == 8 && mlen < len)) {
  737. return 0;
  738. }
  739. ctx->len.u[1] = mlen;
  740. if (ctx->ares) {
  741. /* First call to decrypt finalizes GHASH(AAD) */
  742. GCM_MUL(ctx, Xi);
  743. ctx->ares = 0;
  744. }
  745. if (is_endian.little) {
  746. ctr = GETU32(ctx->Yi.c + 12);
  747. } else {
  748. ctr = ctx->Yi.d[3];
  749. }
  750. n = ctx->mres;
  751. if (n) {
  752. while (n && len) {
  753. uint8_t c = *(in++);
  754. *(out++) = c ^ ctx->EKi.c[n];
  755. ctx->Xi.c[n] ^= c;
  756. --len;
  757. n = (n + 1) % 16;
  758. }
  759. if (n == 0) {
  760. GCM_MUL(ctx, Xi);
  761. } else {
  762. ctx->mres = n;
  763. return 1;
  764. }
  765. }
  766. if (STRICT_ALIGNMENT && ((size_t)in | (size_t)out) % sizeof(size_t) != 0) {
  767. for (size_t i = 0; i < len; ++i) {
  768. uint8_t c;
  769. if (n == 0) {
  770. (*block)(ctx->Yi.c, ctx->EKi.c, key);
  771. ++ctr;
  772. if (is_endian.little) {
  773. PUTU32(ctx->Yi.c + 12, ctr);
  774. } else {
  775. ctx->Yi.d[3] = ctr;
  776. }
  777. }
  778. c = in[i];
  779. out[i] = c ^ ctx->EKi.c[n];
  780. ctx->Xi.c[n] ^= c;
  781. n = (n + 1) % 16;
  782. if (n == 0) {
  783. GCM_MUL(ctx, Xi);
  784. }
  785. }
  786. ctx->mres = n;
  787. return 1;
  788. }
  789. #if defined(GHASH) && defined(GHASH_CHUNK)
  790. while (len >= GHASH_CHUNK) {
  791. size_t j = GHASH_CHUNK;
  792. GHASH(ctx, in, GHASH_CHUNK);
  793. while (j) {
  794. size_t *out_t = (size_t *)out;
  795. const size_t *in_t = (const size_t *)in;
  796. (*block)(ctx->Yi.c, ctx->EKi.c, key);
  797. ++ctr;
  798. if (is_endian.little) {
  799. PUTU32(ctx->Yi.c + 12, ctr);
  800. } else {
  801. ctx->Yi.d[3] = ctr;
  802. }
  803. for (size_t i = 0; i < 16 / sizeof(size_t); ++i) {
  804. out_t[i] = in_t[i] ^ ctx->EKi.t[i];
  805. }
  806. out += 16;
  807. in += 16;
  808. j -= 16;
  809. }
  810. len -= GHASH_CHUNK;
  811. }
  812. size_t len_blocks = len & kSizeTWithoutLower4Bits;
  813. if (len_blocks != 0) {
  814. GHASH(ctx, in, len_blocks);
  815. while (len >= 16) {
  816. size_t *out_t = (size_t *)out;
  817. const size_t *in_t = (const size_t *)in;
  818. (*block)(ctx->Yi.c, ctx->EKi.c, key);
  819. ++ctr;
  820. if (is_endian.little) {
  821. PUTU32(ctx->Yi.c + 12, ctr);
  822. } else {
  823. ctx->Yi.d[3] = ctr;
  824. }
  825. for (size_t i = 0; i < 16 / sizeof(size_t); ++i) {
  826. out_t[i] = in_t[i] ^ ctx->EKi.t[i];
  827. }
  828. out += 16;
  829. in += 16;
  830. len -= 16;
  831. }
  832. }
  833. #else
  834. while (len >= 16) {
  835. size_t *out_t = (size_t *)out;
  836. const size_t *in_t = (const size_t *)in;
  837. (*block)(ctx->Yi.c, ctx->EKi.c, key);
  838. ++ctr;
  839. if (is_endian.little) {
  840. PUTU32(ctx->Yi.c + 12, ctr);
  841. } else {
  842. ctx->Yi.d[3] = ctr;
  843. }
  844. for (size_t i = 0; i < 16 / sizeof(size_t); ++i) {
  845. size_t c = in_t[i];
  846. out_t[i] = c ^ ctx->EKi.t[i];
  847. ctx->Xi.t[i] ^= c;
  848. }
  849. GCM_MUL(ctx, Xi);
  850. out += 16;
  851. in += 16;
  852. len -= 16;
  853. }
  854. #endif
  855. if (len) {
  856. (*block)(ctx->Yi.c, ctx->EKi.c, key);
  857. ++ctr;
  858. if (is_endian.little) {
  859. PUTU32(ctx->Yi.c + 12, ctr);
  860. } else {
  861. ctx->Yi.d[3] = ctr;
  862. }
  863. while (len--) {
  864. uint8_t c = in[n];
  865. ctx->Xi.c[n] ^= c;
  866. out[n] = c ^ ctx->EKi.c[n];
  867. ++n;
  868. }
  869. }
  870. ctx->mres = n;
  871. return 1;
  872. }
  873. int CRYPTO_gcm128_encrypt_ctr32(GCM128_CONTEXT *ctx, const void *key,
  874. const uint8_t *in, uint8_t *out, size_t len,
  875. ctr128_f stream) {
  876. const union {
  877. long one;
  878. char little;
  879. } is_endian = {1};
  880. unsigned int n, ctr;
  881. uint64_t mlen = ctx->len.u[1];
  882. #ifdef GCM_FUNCREF_4BIT
  883. void (*gcm_gmult_p)(uint64_t Xi[2], const u128 Htable[16]) = ctx->gmult;
  884. #ifdef GHASH
  885. void (*gcm_ghash_p)(uint64_t Xi[2], const u128 Htable[16], const uint8_t *inp,
  886. size_t len) = ctx->ghash;
  887. #endif
  888. #endif
  889. mlen += len;
  890. if (mlen > ((UINT64_C(1) << 36) - 32) ||
  891. (sizeof(len) == 8 && mlen < len)) {
  892. return 0;
  893. }
  894. ctx->len.u[1] = mlen;
  895. if (ctx->ares) {
  896. /* First call to encrypt finalizes GHASH(AAD) */
  897. GCM_MUL(ctx, Xi);
  898. ctx->ares = 0;
  899. }
  900. n = ctx->mres;
  901. if (n) {
  902. while (n && len) {
  903. ctx->Xi.c[n] ^= *(out++) = *(in++) ^ ctx->EKi.c[n];
  904. --len;
  905. n = (n + 1) % 16;
  906. }
  907. if (n == 0) {
  908. GCM_MUL(ctx, Xi);
  909. } else {
  910. ctx->mres = n;
  911. return 1;
  912. }
  913. }
  914. #if defined(AESNI_GCM)
  915. if (aesni_gcm_enabled(ctx, stream)) {
  916. /* |aesni_gcm_encrypt| may not process all the input given to it. It may
  917. * not process *any* of its input if it is deemed too small. */
  918. size_t bulk = aesni_gcm_encrypt(in, out, len, key, ctx->Yi.c, ctx->Xi.u);
  919. in += bulk;
  920. out += bulk;
  921. len -= bulk;
  922. }
  923. #endif
  924. if (is_endian.little) {
  925. ctr = GETU32(ctx->Yi.c + 12);
  926. } else {
  927. ctr = ctx->Yi.d[3];
  928. }
  929. #if defined(GHASH)
  930. while (len >= GHASH_CHUNK) {
  931. (*stream)(in, out, GHASH_CHUNK / 16, key, ctx->Yi.c);
  932. ctr += GHASH_CHUNK / 16;
  933. if (is_endian.little) {
  934. PUTU32(ctx->Yi.c + 12, ctr);
  935. } else {
  936. ctx->Yi.d[3] = ctr;
  937. }
  938. GHASH(ctx, out, GHASH_CHUNK);
  939. out += GHASH_CHUNK;
  940. in += GHASH_CHUNK;
  941. len -= GHASH_CHUNK;
  942. }
  943. #endif
  944. size_t i = len & kSizeTWithoutLower4Bits;
  945. if (i != 0) {
  946. size_t j = i / 16;
  947. (*stream)(in, out, j, key, ctx->Yi.c);
  948. ctr += (unsigned int)j;
  949. if (is_endian.little) {
  950. PUTU32(ctx->Yi.c + 12, ctr);
  951. } else {
  952. ctx->Yi.d[3] = ctr;
  953. }
  954. in += i;
  955. len -= i;
  956. #if defined(GHASH)
  957. GHASH(ctx, out, i);
  958. out += i;
  959. #else
  960. while (j--) {
  961. for (i = 0; i < 16; ++i) {
  962. ctx->Xi.c[i] ^= out[i];
  963. }
  964. GCM_MUL(ctx, Xi);
  965. out += 16;
  966. }
  967. #endif
  968. }
  969. if (len) {
  970. (*ctx->block)(ctx->Yi.c, ctx->EKi.c, key);
  971. ++ctr;
  972. if (is_endian.little) {
  973. PUTU32(ctx->Yi.c + 12, ctr);
  974. } else {
  975. ctx->Yi.d[3] = ctr;
  976. }
  977. while (len--) {
  978. ctx->Xi.c[n] ^= out[n] = in[n] ^ ctx->EKi.c[n];
  979. ++n;
  980. }
  981. }
  982. ctx->mres = n;
  983. return 1;
  984. }
  985. int CRYPTO_gcm128_decrypt_ctr32(GCM128_CONTEXT *ctx, const void *key,
  986. const uint8_t *in, uint8_t *out, size_t len,
  987. ctr128_f stream) {
  988. const union {
  989. long one;
  990. char little;
  991. } is_endian = {1};
  992. unsigned int n, ctr;
  993. uint64_t mlen = ctx->len.u[1];
  994. #ifdef GCM_FUNCREF_4BIT
  995. void (*gcm_gmult_p)(uint64_t Xi[2], const u128 Htable[16]) = ctx->gmult;
  996. #ifdef GHASH
  997. void (*gcm_ghash_p)(uint64_t Xi[2], const u128 Htable[16], const uint8_t *inp,
  998. size_t len) = ctx->ghash;
  999. #endif
  1000. #endif
  1001. mlen += len;
  1002. if (mlen > ((UINT64_C(1) << 36) - 32) ||
  1003. (sizeof(len) == 8 && mlen < len)) {
  1004. return 0;
  1005. }
  1006. ctx->len.u[1] = mlen;
  1007. if (ctx->ares) {
  1008. /* First call to decrypt finalizes GHASH(AAD) */
  1009. GCM_MUL(ctx, Xi);
  1010. ctx->ares = 0;
  1011. }
  1012. n = ctx->mres;
  1013. if (n) {
  1014. while (n && len) {
  1015. uint8_t c = *(in++);
  1016. *(out++) = c ^ ctx->EKi.c[n];
  1017. ctx->Xi.c[n] ^= c;
  1018. --len;
  1019. n = (n + 1) % 16;
  1020. }
  1021. if (n == 0) {
  1022. GCM_MUL(ctx, Xi);
  1023. } else {
  1024. ctx->mres = n;
  1025. return 1;
  1026. }
  1027. }
  1028. #if defined(AESNI_GCM)
  1029. if (aesni_gcm_enabled(ctx, stream)) {
  1030. /* |aesni_gcm_decrypt| may not process all the input given to it. It may
  1031. * not process *any* of its input if it is deemed too small. */
  1032. size_t bulk = aesni_gcm_decrypt(in, out, len, key, ctx->Yi.c, ctx->Xi.u);
  1033. in += bulk;
  1034. out += bulk;
  1035. len -= bulk;
  1036. }
  1037. #endif
  1038. if (is_endian.little) {
  1039. ctr = GETU32(ctx->Yi.c + 12);
  1040. } else {
  1041. ctr = ctx->Yi.d[3];
  1042. }
  1043. #if defined(GHASH)
  1044. while (len >= GHASH_CHUNK) {
  1045. GHASH(ctx, in, GHASH_CHUNK);
  1046. (*stream)(in, out, GHASH_CHUNK / 16, key, ctx->Yi.c);
  1047. ctr += GHASH_CHUNK / 16;
  1048. if (is_endian.little) {
  1049. PUTU32(ctx->Yi.c + 12, ctr);
  1050. } else {
  1051. ctx->Yi.d[3] = ctr;
  1052. }
  1053. out += GHASH_CHUNK;
  1054. in += GHASH_CHUNK;
  1055. len -= GHASH_CHUNK;
  1056. }
  1057. #endif
  1058. size_t i = len & kSizeTWithoutLower4Bits;
  1059. if (i != 0) {
  1060. size_t j = i / 16;
  1061. #if defined(GHASH)
  1062. GHASH(ctx, in, i);
  1063. #else
  1064. while (j--) {
  1065. size_t k;
  1066. for (k = 0; k < 16; ++k) {
  1067. ctx->Xi.c[k] ^= in[k];
  1068. }
  1069. GCM_MUL(ctx, Xi);
  1070. in += 16;
  1071. }
  1072. j = i / 16;
  1073. in -= i;
  1074. #endif
  1075. (*stream)(in, out, j, key, ctx->Yi.c);
  1076. ctr += (unsigned int)j;
  1077. if (is_endian.little) {
  1078. PUTU32(ctx->Yi.c + 12, ctr);
  1079. } else {
  1080. ctx->Yi.d[3] = ctr;
  1081. }
  1082. out += i;
  1083. in += i;
  1084. len -= i;
  1085. }
  1086. if (len) {
  1087. (*ctx->block)(ctx->Yi.c, ctx->EKi.c, key);
  1088. ++ctr;
  1089. if (is_endian.little) {
  1090. PUTU32(ctx->Yi.c + 12, ctr);
  1091. } else {
  1092. ctx->Yi.d[3] = ctr;
  1093. }
  1094. while (len--) {
  1095. uint8_t c = in[n];
  1096. ctx->Xi.c[n] ^= c;
  1097. out[n] = c ^ ctx->EKi.c[n];
  1098. ++n;
  1099. }
  1100. }
  1101. ctx->mres = n;
  1102. return 1;
  1103. }
  1104. int CRYPTO_gcm128_finish(GCM128_CONTEXT *ctx, const uint8_t *tag, size_t len) {
  1105. const union {
  1106. long one;
  1107. char little;
  1108. } is_endian = {1};
  1109. uint64_t alen = ctx->len.u[0] << 3;
  1110. uint64_t clen = ctx->len.u[1] << 3;
  1111. #ifdef GCM_FUNCREF_4BIT
  1112. void (*gcm_gmult_p)(uint64_t Xi[2], const u128 Htable[16]) = ctx->gmult;
  1113. #endif
  1114. if (ctx->mres || ctx->ares) {
  1115. GCM_MUL(ctx, Xi);
  1116. }
  1117. if (is_endian.little) {
  1118. #ifdef BSWAP8
  1119. alen = BSWAP8(alen);
  1120. clen = BSWAP8(clen);
  1121. #else
  1122. uint8_t *p = ctx->len.c;
  1123. ctx->len.u[0] = alen;
  1124. ctx->len.u[1] = clen;
  1125. alen = (uint64_t)GETU32(p) << 32 | GETU32(p + 4);
  1126. clen = (uint64_t)GETU32(p + 8) << 32 | GETU32(p + 12);
  1127. #endif
  1128. }
  1129. ctx->Xi.u[0] ^= alen;
  1130. ctx->Xi.u[1] ^= clen;
  1131. GCM_MUL(ctx, Xi);
  1132. ctx->Xi.u[0] ^= ctx->EK0.u[0];
  1133. ctx->Xi.u[1] ^= ctx->EK0.u[1];
  1134. if (tag && len <= sizeof(ctx->Xi)) {
  1135. return CRYPTO_memcmp(ctx->Xi.c, tag, len) == 0;
  1136. } else {
  1137. return 0;
  1138. }
  1139. }
  1140. void CRYPTO_gcm128_tag(GCM128_CONTEXT *ctx, unsigned char *tag, size_t len) {
  1141. CRYPTO_gcm128_finish(ctx, NULL, 0);
  1142. memcpy(tag, ctx->Xi.c, len <= sizeof(ctx->Xi.c) ? len : sizeof(ctx->Xi.c));
  1143. }
  1144. #if defined(OPENSSL_X86) || defined(OPENSSL_X86_64)
  1145. int crypto_gcm_clmul_enabled(void) {
  1146. #ifdef GHASH_ASM
  1147. return OPENSSL_ia32cap_P[0] & (1 << 24) && /* check FXSR bit */
  1148. OPENSSL_ia32cap_P[1] & (1 << 1); /* check PCLMULQDQ bit */
  1149. #else
  1150. return 0;
  1151. #endif
  1152. }
  1153. #endif