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.
 
 
 
 
 
 

1254 lines
31 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 = OPENSSL_U64(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 *inp, size_t len);
  303. #endif
  304. #if defined(OPENSSL_X86)
  305. #define GHASH_ASM_X86
  306. void gcm_gmult_4bit_mmx(uint64_t Xi[2], const u128 Htable[16]);
  307. void gcm_ghash_4bit_mmx(uint64_t Xi[2], const u128 Htable[16], const uint8_t *inp,
  308. size_t len);
  309. void gcm_gmult_4bit_x86(uint64_t Xi[2], const u128 Htable[16]);
  310. void gcm_ghash_4bit_x86(uint64_t Xi[2], const u128 Htable[16], const uint8_t *inp,
  311. size_t len);
  312. #endif
  313. #elif defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64)
  314. #include <openssl/arm_arch.h>
  315. #if __ARM_ARCH__ >= 7
  316. #define GHASH_ASM_ARM
  317. #define GCM_FUNCREF_4BIT
  318. static int pmull_capable(void) {
  319. return CRYPTO_is_ARMv8_PMULL_capable();
  320. }
  321. void gcm_init_v8(u128 Htable[16], const uint64_t Xi[2]);
  322. void gcm_gmult_v8(uint64_t Xi[2], const u128 Htable[16]);
  323. void gcm_ghash_v8(uint64_t Xi[2], const u128 Htable[16], const uint8_t *inp,
  324. size_t len);
  325. #if defined(OPENSSL_ARM)
  326. /* 32-bit ARM also has support for doing GCM with NEON instructions. */
  327. static int neon_capable(void) {
  328. return CRYPTO_is_NEON_capable();
  329. }
  330. void gcm_init_neon(u128 Htable[16], const uint64_t Xi[2]);
  331. void gcm_gmult_neon(uint64_t Xi[2], const u128 Htable[16]);
  332. void gcm_ghash_neon(uint64_t Xi[2], const u128 Htable[16], const uint8_t *inp,
  333. size_t len);
  334. #else
  335. /* AArch64 only has the ARMv8 versions of functions. */
  336. static int neon_capable(void) {
  337. return 0;
  338. }
  339. void gcm_init_neon(u128 Htable[16], const uint64_t Xi[2]) {
  340. abort();
  341. }
  342. void gcm_gmult_neon(uint64_t Xi[2], const u128 Htable[16]) {
  343. abort();
  344. }
  345. void gcm_ghash_neon(uint64_t Xi[2], const u128 Htable[16], const uint8_t *inp,
  346. size_t len) {
  347. abort();
  348. }
  349. #endif
  350. #endif
  351. #endif
  352. #endif
  353. #ifdef GCM_FUNCREF_4BIT
  354. #undef GCM_MUL
  355. #define GCM_MUL(ctx, Xi) (*gcm_gmult_p)(ctx->Xi.u, ctx->Htable)
  356. #ifdef GHASH
  357. #undef GHASH
  358. #define GHASH(ctx, in, len) (*gcm_ghash_p)(ctx->Xi.u, ctx->Htable, in, len)
  359. #endif
  360. #endif
  361. GCM128_CONTEXT *CRYPTO_gcm128_new(const void *key, block128_f block) {
  362. GCM128_CONTEXT *ret;
  363. ret = (GCM128_CONTEXT *)OPENSSL_malloc(sizeof(GCM128_CONTEXT));
  364. if (ret != NULL) {
  365. CRYPTO_gcm128_init(ret, key, block);
  366. }
  367. return ret;
  368. }
  369. void CRYPTO_gcm128_init(GCM128_CONTEXT *ctx, const void *key,
  370. block128_f block) {
  371. const union {
  372. long one;
  373. char little;
  374. } is_endian = {1};
  375. memset(ctx, 0, sizeof(*ctx));
  376. ctx->block = block;
  377. (*block)(ctx->H.c, ctx->H.c, key);
  378. if (is_endian.little) {
  379. /* H is stored in host byte order */
  380. #ifdef BSWAP8
  381. ctx->H.u[0] = BSWAP8(ctx->H.u[0]);
  382. ctx->H.u[1] = BSWAP8(ctx->H.u[1]);
  383. #else
  384. uint8_t *p = ctx->H.c;
  385. uint64_t hi, lo;
  386. hi = (uint64_t)GETU32(p) << 32 | GETU32(p + 4);
  387. lo = (uint64_t)GETU32(p + 8) << 32 | GETU32(p + 12);
  388. ctx->H.u[0] = hi;
  389. ctx->H.u[1] = lo;
  390. #endif
  391. }
  392. #if defined(GHASH_ASM_X86_OR_64)
  393. if (crypto_gcm_clmul_enabled()) {
  394. if (((OPENSSL_ia32cap_P[1] >> 22) & 0x41) == 0x41) { /* AVX+MOVBE */
  395. gcm_init_avx(ctx->Htable, ctx->H.u);
  396. ctx->gmult = gcm_gmult_avx;
  397. ctx->ghash = gcm_ghash_avx;
  398. } else {
  399. gcm_init_clmul(ctx->Htable, ctx->H.u);
  400. ctx->gmult = gcm_gmult_clmul;
  401. ctx->ghash = gcm_ghash_clmul;
  402. }
  403. return;
  404. }
  405. gcm_init_4bit(ctx->Htable, ctx->H.u);
  406. #if defined(GHASH_ASM_X86) /* x86 only */
  407. if (OPENSSL_ia32cap_P[0] & (1 << 25)) { /* check SSE bit */
  408. ctx->gmult = gcm_gmult_4bit_mmx;
  409. ctx->ghash = gcm_ghash_4bit_mmx;
  410. } else {
  411. ctx->gmult = gcm_gmult_4bit_x86;
  412. ctx->ghash = gcm_ghash_4bit_x86;
  413. }
  414. #else
  415. ctx->gmult = gcm_gmult_4bit;
  416. ctx->ghash = gcm_ghash_4bit;
  417. #endif
  418. #elif defined(GHASH_ASM_ARM)
  419. if (pmull_capable()) {
  420. gcm_init_v8(ctx->Htable, ctx->H.u);
  421. ctx->gmult = gcm_gmult_v8;
  422. ctx->ghash = gcm_ghash_v8;
  423. } else if (neon_capable()) {
  424. gcm_init_neon(ctx->Htable,ctx->H.u);
  425. ctx->gmult = gcm_gmult_neon;
  426. ctx->ghash = gcm_ghash_neon;
  427. } else {
  428. gcm_init_4bit(ctx->Htable, ctx->H.u);
  429. ctx->gmult = gcm_gmult_4bit;
  430. ctx->ghash = gcm_ghash_4bit;
  431. }
  432. #else
  433. gcm_init_4bit(ctx->Htable, ctx->H.u);
  434. ctx->gmult = gcm_gmult_4bit;
  435. ctx->ghash = gcm_ghash_4bit;
  436. #endif
  437. }
  438. void CRYPTO_gcm128_setiv(GCM128_CONTEXT *ctx, const void *key,
  439. const uint8_t *iv, size_t len) {
  440. const union {
  441. long one;
  442. char little;
  443. } is_endian = {1};
  444. unsigned int ctr;
  445. #ifdef GCM_FUNCREF_4BIT
  446. void (*gcm_gmult_p)(uint64_t Xi[2], const u128 Htable[16]) = ctx->gmult;
  447. #endif
  448. ctx->Yi.u[0] = 0;
  449. ctx->Yi.u[1] = 0;
  450. ctx->Xi.u[0] = 0;
  451. ctx->Xi.u[1] = 0;
  452. ctx->len.u[0] = 0; /* AAD length */
  453. ctx->len.u[1] = 0; /* message length */
  454. ctx->ares = 0;
  455. ctx->mres = 0;
  456. if (len == 12) {
  457. memcpy(ctx->Yi.c, iv, 12);
  458. ctx->Yi.c[15] = 1;
  459. ctr = 1;
  460. } else {
  461. size_t i;
  462. uint64_t len0 = len;
  463. while (len >= 16) {
  464. for (i = 0; i < 16; ++i) {
  465. ctx->Yi.c[i] ^= iv[i];
  466. }
  467. GCM_MUL(ctx, Yi);
  468. iv += 16;
  469. len -= 16;
  470. }
  471. if (len) {
  472. for (i = 0; i < len; ++i) {
  473. ctx->Yi.c[i] ^= iv[i];
  474. }
  475. GCM_MUL(ctx, Yi);
  476. }
  477. len0 <<= 3;
  478. if (is_endian.little) {
  479. #ifdef BSWAP8
  480. ctx->Yi.u[1] ^= BSWAP8(len0);
  481. #else
  482. ctx->Yi.c[8] ^= (uint8_t)(len0 >> 56);
  483. ctx->Yi.c[9] ^= (uint8_t)(len0 >> 48);
  484. ctx->Yi.c[10] ^= (uint8_t)(len0 >> 40);
  485. ctx->Yi.c[11] ^= (uint8_t)(len0 >> 32);
  486. ctx->Yi.c[12] ^= (uint8_t)(len0 >> 24);
  487. ctx->Yi.c[13] ^= (uint8_t)(len0 >> 16);
  488. ctx->Yi.c[14] ^= (uint8_t)(len0 >> 8);
  489. ctx->Yi.c[15] ^= (uint8_t)(len0);
  490. #endif
  491. } else {
  492. ctx->Yi.u[1] ^= len0;
  493. }
  494. GCM_MUL(ctx, Yi);
  495. if (is_endian.little) {
  496. ctr = GETU32(ctx->Yi.c + 12);
  497. } else {
  498. ctr = ctx->Yi.d[3];
  499. }
  500. }
  501. (*ctx->block)(ctx->Yi.c, ctx->EK0.c, key);
  502. ++ctr;
  503. if (is_endian.little) {
  504. PUTU32(ctx->Yi.c + 12, ctr);
  505. } else {
  506. ctx->Yi.d[3] = ctr;
  507. }
  508. }
  509. int CRYPTO_gcm128_aad(GCM128_CONTEXT *ctx, const uint8_t *aad, size_t len) {
  510. size_t i;
  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 > (OPENSSL_U64(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. #ifdef GHASH
  543. if ((i = (len & (size_t) - 16))) {
  544. GHASH(ctx, aad, i);
  545. aad += i;
  546. len -= i;
  547. }
  548. #else
  549. while (len >= 16) {
  550. for (i = 0; i < 16; ++i) {
  551. ctx->Xi.c[i] ^= aad[i];
  552. }
  553. GCM_MUL(ctx, Xi);
  554. aad += 16;
  555. len -= 16;
  556. }
  557. #endif
  558. if (len) {
  559. n = (unsigned int)len;
  560. for (i = 0; i < len; ++i) {
  561. ctx->Xi.c[i] ^= aad[i];
  562. }
  563. }
  564. ctx->ares = n;
  565. return 1;
  566. }
  567. int CRYPTO_gcm128_encrypt(GCM128_CONTEXT *ctx, const void *key,
  568. const unsigned char *in, unsigned char *out,
  569. size_t len) {
  570. const union {
  571. long one;
  572. char little;
  573. } is_endian = {1};
  574. unsigned int n, ctr;
  575. size_t i;
  576. uint64_t mlen = ctx->len.u[1];
  577. block128_f block = ctx->block;
  578. #ifdef GCM_FUNCREF_4BIT
  579. void (*gcm_gmult_p)(uint64_t Xi[2], const u128 Htable[16]) = ctx->gmult;
  580. #ifdef GHASH
  581. void (*gcm_ghash_p)(uint64_t Xi[2], const u128 Htable[16], const uint8_t *inp,
  582. size_t len) = ctx->ghash;
  583. #endif
  584. #endif
  585. mlen += len;
  586. if (mlen > ((OPENSSL_U64(1) << 36) - 32) ||
  587. (sizeof(len) == 8 && mlen < len)) {
  588. return 0;
  589. }
  590. ctx->len.u[1] = mlen;
  591. if (ctx->ares) {
  592. /* First call to encrypt finalizes GHASH(AAD) */
  593. GCM_MUL(ctx, Xi);
  594. ctx->ares = 0;
  595. }
  596. if (is_endian.little) {
  597. ctr = GETU32(ctx->Yi.c + 12);
  598. } else {
  599. ctr = ctx->Yi.d[3];
  600. }
  601. n = ctx->mres;
  602. if (n) {
  603. while (n && len) {
  604. ctx->Xi.c[n] ^= *(out++) = *(in++) ^ ctx->EKi.c[n];
  605. --len;
  606. n = (n + 1) % 16;
  607. }
  608. if (n == 0) {
  609. GCM_MUL(ctx, Xi);
  610. } else {
  611. ctx->mres = n;
  612. return 1;
  613. }
  614. }
  615. if (STRICT_ALIGNMENT && ((size_t)in | (size_t)out) % sizeof(size_t) != 0) {
  616. for (i = 0; i < len; ++i) {
  617. if (n == 0) {
  618. (*block)(ctx->Yi.c, ctx->EKi.c, key);
  619. ++ctr;
  620. if (is_endian.little) {
  621. PUTU32(ctx->Yi.c + 12, ctr);
  622. } else {
  623. ctx->Yi.d[3] = ctr;
  624. }
  625. }
  626. ctx->Xi.c[n] ^= out[i] = in[i] ^ ctx->EKi.c[n];
  627. n = (n + 1) % 16;
  628. if (n == 0) {
  629. GCM_MUL(ctx, Xi);
  630. }
  631. }
  632. ctx->mres = n;
  633. return 1;
  634. }
  635. #if defined(GHASH) && defined(GHASH_CHUNK)
  636. while (len >= GHASH_CHUNK) {
  637. size_t j = GHASH_CHUNK;
  638. while (j) {
  639. size_t *out_t = (size_t *)out;
  640. const size_t *in_t = (const size_t *)in;
  641. (*block)(ctx->Yi.c, ctx->EKi.c, key);
  642. ++ctr;
  643. if (is_endian.little) {
  644. PUTU32(ctx->Yi.c + 12, ctr);
  645. } else {
  646. ctx->Yi.d[3] = ctr;
  647. }
  648. for (i = 0; i < 16 / sizeof(size_t); ++i) {
  649. out_t[i] = in_t[i] ^ ctx->EKi.t[i];
  650. }
  651. out += 16;
  652. in += 16;
  653. j -= 16;
  654. }
  655. GHASH(ctx, out - GHASH_CHUNK, GHASH_CHUNK);
  656. len -= GHASH_CHUNK;
  657. }
  658. if ((i = (len & (size_t) - 16))) {
  659. size_t j = i;
  660. while (len >= 16) {
  661. size_t *out_t = (size_t *)out;
  662. const size_t *in_t = (const size_t *)in;
  663. (*block)(ctx->Yi.c, ctx->EKi.c, key);
  664. ++ctr;
  665. if (is_endian.little) {
  666. PUTU32(ctx->Yi.c + 12, ctr);
  667. } else {
  668. ctx->Yi.d[3] = ctr;
  669. }
  670. for (i = 0; i < 16 / sizeof(size_t); ++i) {
  671. out_t[i] = in_t[i] ^ ctx->EKi.t[i];
  672. }
  673. out += 16;
  674. in += 16;
  675. len -= 16;
  676. }
  677. GHASH(ctx, out - j, j);
  678. }
  679. #else
  680. while (len >= 16) {
  681. size_t *out_t = (size_t *)out;
  682. const size_t *in_t = (const size_t *)in;
  683. (*block)(ctx->Yi.c, ctx->EKi.c, key);
  684. ++ctr;
  685. if (is_endian.little) {
  686. PUTU32(ctx->Yi.c + 12, ctr);
  687. } else {
  688. ctx->Yi.d[3] = ctr;
  689. }
  690. for (i = 0; i < 16 / sizeof(size_t); ++i) {
  691. ctx->Xi.t[i] ^= out_t[i] = in_t[i] ^ ctx->EKi.t[i];
  692. }
  693. GCM_MUL(ctx, Xi);
  694. out += 16;
  695. in += 16;
  696. len -= 16;
  697. }
  698. #endif
  699. if (len) {
  700. (*block)(ctx->Yi.c, ctx->EKi.c, key);
  701. ++ctr;
  702. if (is_endian.little) {
  703. PUTU32(ctx->Yi.c + 12, ctr);
  704. } else {
  705. ctx->Yi.d[3] = ctr;
  706. }
  707. while (len--) {
  708. ctx->Xi.c[n] ^= out[n] = in[n] ^ ctx->EKi.c[n];
  709. ++n;
  710. }
  711. }
  712. ctx->mres = n;
  713. return 1;
  714. }
  715. int CRYPTO_gcm128_decrypt(GCM128_CONTEXT *ctx, const void *key,
  716. const unsigned char *in, unsigned char *out,
  717. size_t len) {
  718. const union {
  719. long one;
  720. char little;
  721. } is_endian = {1};
  722. unsigned int n, ctr;
  723. size_t i;
  724. uint64_t mlen = ctx->len.u[1];
  725. block128_f block = ctx->block;
  726. #ifdef GCM_FUNCREF_4BIT
  727. void (*gcm_gmult_p)(uint64_t Xi[2], const u128 Htable[16]) = ctx->gmult;
  728. #ifdef GHASH
  729. void (*gcm_ghash_p)(uint64_t Xi[2], const u128 Htable[16], const uint8_t *inp,
  730. size_t len) = ctx->ghash;
  731. #endif
  732. #endif
  733. mlen += len;
  734. if (mlen > ((OPENSSL_U64(1) << 36) - 32) ||
  735. (sizeof(len) == 8 && mlen < len)) {
  736. return 0;
  737. }
  738. ctx->len.u[1] = mlen;
  739. if (ctx->ares) {
  740. /* First call to decrypt finalizes GHASH(AAD) */
  741. GCM_MUL(ctx, Xi);
  742. ctx->ares = 0;
  743. }
  744. if (is_endian.little) {
  745. ctr = GETU32(ctx->Yi.c + 12);
  746. } else {
  747. ctr = ctx->Yi.d[3];
  748. }
  749. n = ctx->mres;
  750. if (n) {
  751. while (n && len) {
  752. uint8_t c = *(in++);
  753. *(out++) = c ^ ctx->EKi.c[n];
  754. ctx->Xi.c[n] ^= c;
  755. --len;
  756. n = (n + 1) % 16;
  757. }
  758. if (n == 0) {
  759. GCM_MUL(ctx, Xi);
  760. } else {
  761. ctx->mres = n;
  762. return 1;
  763. }
  764. }
  765. if (STRICT_ALIGNMENT && ((size_t)in | (size_t)out) % sizeof(size_t) != 0) {
  766. for (i = 0; i < len; ++i) {
  767. uint8_t c;
  768. if (n == 0) {
  769. (*block)(ctx->Yi.c, ctx->EKi.c, key);
  770. ++ctr;
  771. if (is_endian.little) {
  772. PUTU32(ctx->Yi.c + 12, ctr);
  773. } else {
  774. ctx->Yi.d[3] = ctr;
  775. }
  776. }
  777. c = in[i];
  778. out[i] = c ^ ctx->EKi.c[n];
  779. ctx->Xi.c[n] ^= c;
  780. n = (n + 1) % 16;
  781. if (n == 0) {
  782. GCM_MUL(ctx, Xi);
  783. }
  784. }
  785. ctx->mres = n;
  786. return 1;
  787. }
  788. #if defined(GHASH) && defined(GHASH_CHUNK)
  789. while (len >= GHASH_CHUNK) {
  790. size_t j = GHASH_CHUNK;
  791. GHASH(ctx, in, GHASH_CHUNK);
  792. while (j) {
  793. size_t *out_t = (size_t *)out;
  794. const size_t *in_t = (const size_t *)in;
  795. (*block)(ctx->Yi.c, ctx->EKi.c, key);
  796. ++ctr;
  797. if (is_endian.little) {
  798. PUTU32(ctx->Yi.c + 12, ctr);
  799. } else {
  800. ctx->Yi.d[3] = ctr;
  801. }
  802. for (i = 0; i < 16 / sizeof(size_t); ++i) {
  803. out_t[i] = in_t[i] ^ ctx->EKi.t[i];
  804. }
  805. out += 16;
  806. in += 16;
  807. j -= 16;
  808. }
  809. len -= GHASH_CHUNK;
  810. }
  811. if ((i = (len & (size_t) - 16))) {
  812. GHASH(ctx, in, i);
  813. while (len >= 16) {
  814. size_t *out_t = (size_t *)out;
  815. const size_t *in_t = (const size_t *)in;
  816. (*block)(ctx->Yi.c, ctx->EKi.c, key);
  817. ++ctr;
  818. if (is_endian.little) {
  819. PUTU32(ctx->Yi.c + 12, ctr);
  820. } else {
  821. ctx->Yi.d[3] = ctr;
  822. }
  823. for (i = 0; i < 16 / sizeof(size_t); ++i) {
  824. out_t[i] = in_t[i] ^ ctx->EKi.t[i];
  825. }
  826. out += 16;
  827. in += 16;
  828. len -= 16;
  829. }
  830. }
  831. #else
  832. while (len >= 16) {
  833. size_t *out_t = (size_t *)out;
  834. const size_t *in_t = (const size_t *)in;
  835. (*block)(ctx->Yi.c, ctx->EKi.c, key);
  836. ++ctr;
  837. if (is_endian.little) {
  838. PUTU32(ctx->Yi.c + 12, ctr);
  839. } else {
  840. ctx->Yi.d[3] = ctr;
  841. }
  842. for (i = 0; i < 16 / sizeof(size_t); ++i) {
  843. size_t c = in_t[i];
  844. out_t[i] = c ^ ctx->EKi.t[i];
  845. ctx->Xi.t[i] ^= c;
  846. }
  847. GCM_MUL(ctx, Xi);
  848. out += 16;
  849. in += 16;
  850. len -= 16;
  851. }
  852. #endif
  853. if (len) {
  854. (*block)(ctx->Yi.c, ctx->EKi.c, key);
  855. ++ctr;
  856. if (is_endian.little) {
  857. PUTU32(ctx->Yi.c + 12, ctr);
  858. } else {
  859. ctx->Yi.d[3] = ctr;
  860. }
  861. while (len--) {
  862. uint8_t c = in[n];
  863. ctx->Xi.c[n] ^= c;
  864. out[n] = c ^ ctx->EKi.c[n];
  865. ++n;
  866. }
  867. }
  868. ctx->mres = n;
  869. return 1;
  870. }
  871. int CRYPTO_gcm128_encrypt_ctr32(GCM128_CONTEXT *ctx, const void *key,
  872. const uint8_t *in, uint8_t *out, size_t len,
  873. ctr128_f stream) {
  874. const union {
  875. long one;
  876. char little;
  877. } is_endian = {1};
  878. unsigned int n, ctr;
  879. uint64_t mlen = ctx->len.u[1];
  880. #ifdef GCM_FUNCREF_4BIT
  881. void (*gcm_gmult_p)(uint64_t Xi[2], const u128 Htable[16]) = ctx->gmult;
  882. #ifdef GHASH
  883. void (*gcm_ghash_p)(uint64_t Xi[2], const u128 Htable[16], const uint8_t *inp,
  884. size_t len) = ctx->ghash;
  885. #endif
  886. #endif
  887. mlen += len;
  888. if (mlen > ((OPENSSL_U64(1) << 36) - 32) ||
  889. (sizeof(len) == 8 && mlen < len)) {
  890. return 0;
  891. }
  892. ctx->len.u[1] = mlen;
  893. if (ctx->ares) {
  894. /* First call to encrypt finalizes GHASH(AAD) */
  895. GCM_MUL(ctx, Xi);
  896. ctx->ares = 0;
  897. }
  898. if (is_endian.little) {
  899. ctr = GETU32(ctx->Yi.c + 12);
  900. } else {
  901. ctr = ctx->Yi.d[3];
  902. }
  903. n = ctx->mres;
  904. if (n) {
  905. while (n && len) {
  906. ctx->Xi.c[n] ^= *(out++) = *(in++) ^ ctx->EKi.c[n];
  907. --len;
  908. n = (n + 1) % 16;
  909. }
  910. if (n == 0) {
  911. GCM_MUL(ctx, Xi);
  912. } else {
  913. ctx->mres = n;
  914. return 1;
  915. }
  916. }
  917. #if defined(GHASH)
  918. while (len >= GHASH_CHUNK) {
  919. (*stream)(in, out, GHASH_CHUNK / 16, key, ctx->Yi.c);
  920. ctr += GHASH_CHUNK / 16;
  921. if (is_endian.little) {
  922. PUTU32(ctx->Yi.c + 12, ctr);
  923. } else {
  924. ctx->Yi.d[3] = ctr;
  925. }
  926. GHASH(ctx, out, GHASH_CHUNK);
  927. out += GHASH_CHUNK;
  928. in += GHASH_CHUNK;
  929. len -= GHASH_CHUNK;
  930. }
  931. #endif
  932. size_t i = len & kSizeTWithoutLower4Bits;
  933. if (i != 0) {
  934. size_t j = i / 16;
  935. (*stream)(in, out, j, key, ctx->Yi.c);
  936. ctr += (unsigned int)j;
  937. if (is_endian.little) {
  938. PUTU32(ctx->Yi.c + 12, ctr);
  939. } else {
  940. ctx->Yi.d[3] = ctr;
  941. }
  942. in += i;
  943. len -= i;
  944. #if defined(GHASH)
  945. GHASH(ctx, out, i);
  946. out += i;
  947. #else
  948. while (j--) {
  949. for (i = 0; i < 16; ++i) {
  950. ctx->Xi.c[i] ^= out[i];
  951. }
  952. GCM_MUL(ctx, Xi);
  953. out += 16;
  954. }
  955. #endif
  956. }
  957. if (len) {
  958. (*ctx->block)(ctx->Yi.c, ctx->EKi.c, key);
  959. ++ctr;
  960. if (is_endian.little) {
  961. PUTU32(ctx->Yi.c + 12, ctr);
  962. } else {
  963. ctx->Yi.d[3] = ctr;
  964. }
  965. while (len--) {
  966. ctx->Xi.c[n] ^= out[n] = in[n] ^ ctx->EKi.c[n];
  967. ++n;
  968. }
  969. }
  970. ctx->mres = n;
  971. return 1;
  972. }
  973. int CRYPTO_gcm128_decrypt_ctr32(GCM128_CONTEXT *ctx, const void *key,
  974. const uint8_t *in, uint8_t *out, size_t len,
  975. ctr128_f stream) {
  976. const union {
  977. long one;
  978. char little;
  979. } is_endian = {1};
  980. unsigned int n, ctr;
  981. uint64_t mlen = ctx->len.u[1];
  982. #ifdef GCM_FUNCREF_4BIT
  983. void (*gcm_gmult_p)(uint64_t Xi[2], const u128 Htable[16]) = ctx->gmult;
  984. #ifdef GHASH
  985. void (*gcm_ghash_p)(uint64_t Xi[2], const u128 Htable[16], const uint8_t *inp,
  986. size_t len) = ctx->ghash;
  987. #endif
  988. #endif
  989. mlen += len;
  990. if (mlen > ((OPENSSL_U64(1) << 36) - 32) ||
  991. (sizeof(len) == 8 && mlen < len)) {
  992. return 0;
  993. }
  994. ctx->len.u[1] = mlen;
  995. if (ctx->ares) {
  996. /* First call to decrypt finalizes GHASH(AAD) */
  997. GCM_MUL(ctx, Xi);
  998. ctx->ares = 0;
  999. }
  1000. if (is_endian.little) {
  1001. ctr = GETU32(ctx->Yi.c + 12);
  1002. } else {
  1003. ctr = ctx->Yi.d[3];
  1004. }
  1005. n = ctx->mres;
  1006. if (n) {
  1007. while (n && len) {
  1008. uint8_t c = *(in++);
  1009. *(out++) = c ^ ctx->EKi.c[n];
  1010. ctx->Xi.c[n] ^= c;
  1011. --len;
  1012. n = (n + 1) % 16;
  1013. }
  1014. if (n == 0) {
  1015. GCM_MUL(ctx, Xi);
  1016. } else {
  1017. ctx->mres = n;
  1018. return 1;
  1019. }
  1020. }
  1021. #if defined(GHASH)
  1022. while (len >= GHASH_CHUNK) {
  1023. GHASH(ctx, in, GHASH_CHUNK);
  1024. (*stream)(in, out, GHASH_CHUNK / 16, key, ctx->Yi.c);
  1025. ctr += GHASH_CHUNK / 16;
  1026. if (is_endian.little) {
  1027. PUTU32(ctx->Yi.c + 12, ctr);
  1028. } else {
  1029. ctx->Yi.d[3] = ctr;
  1030. }
  1031. out += GHASH_CHUNK;
  1032. in += GHASH_CHUNK;
  1033. len -= GHASH_CHUNK;
  1034. }
  1035. #endif
  1036. size_t i = len & kSizeTWithoutLower4Bits;
  1037. if (i != 0) {
  1038. size_t j = i / 16;
  1039. #if defined(GHASH)
  1040. GHASH(ctx, in, i);
  1041. #else
  1042. while (j--) {
  1043. size_t k;
  1044. for (k = 0; k < 16; ++k) {
  1045. ctx->Xi.c[k] ^= in[k];
  1046. }
  1047. GCM_MUL(ctx, Xi);
  1048. in += 16;
  1049. }
  1050. j = i / 16;
  1051. in -= i;
  1052. #endif
  1053. (*stream)(in, out, j, key, ctx->Yi.c);
  1054. ctr += (unsigned int)j;
  1055. if (is_endian.little) {
  1056. PUTU32(ctx->Yi.c + 12, ctr);
  1057. } else {
  1058. ctx->Yi.d[3] = ctr;
  1059. }
  1060. out += i;
  1061. in += i;
  1062. len -= i;
  1063. }
  1064. if (len) {
  1065. (*ctx->block)(ctx->Yi.c, ctx->EKi.c, key);
  1066. ++ctr;
  1067. if (is_endian.little) {
  1068. PUTU32(ctx->Yi.c + 12, ctr);
  1069. } else {
  1070. ctx->Yi.d[3] = ctr;
  1071. }
  1072. while (len--) {
  1073. uint8_t c = in[n];
  1074. ctx->Xi.c[n] ^= c;
  1075. out[n] = c ^ ctx->EKi.c[n];
  1076. ++n;
  1077. }
  1078. }
  1079. ctx->mres = n;
  1080. return 1;
  1081. }
  1082. int CRYPTO_gcm128_finish(GCM128_CONTEXT *ctx, const uint8_t *tag, size_t len) {
  1083. const union {
  1084. long one;
  1085. char little;
  1086. } is_endian = {1};
  1087. uint64_t alen = ctx->len.u[0] << 3;
  1088. uint64_t clen = ctx->len.u[1] << 3;
  1089. #ifdef GCM_FUNCREF_4BIT
  1090. void (*gcm_gmult_p)(uint64_t Xi[2], const u128 Htable[16]) = ctx->gmult;
  1091. #endif
  1092. if (ctx->mres || ctx->ares) {
  1093. GCM_MUL(ctx, Xi);
  1094. }
  1095. if (is_endian.little) {
  1096. #ifdef BSWAP8
  1097. alen = BSWAP8(alen);
  1098. clen = BSWAP8(clen);
  1099. #else
  1100. uint8_t *p = ctx->len.c;
  1101. ctx->len.u[0] = alen;
  1102. ctx->len.u[1] = clen;
  1103. alen = (uint64_t)GETU32(p) << 32 | GETU32(p + 4);
  1104. clen = (uint64_t)GETU32(p + 8) << 32 | GETU32(p + 12);
  1105. #endif
  1106. }
  1107. ctx->Xi.u[0] ^= alen;
  1108. ctx->Xi.u[1] ^= clen;
  1109. GCM_MUL(ctx, Xi);
  1110. ctx->Xi.u[0] ^= ctx->EK0.u[0];
  1111. ctx->Xi.u[1] ^= ctx->EK0.u[1];
  1112. if (tag && len <= sizeof(ctx->Xi)) {
  1113. return CRYPTO_memcmp(ctx->Xi.c, tag, len) == 0;
  1114. } else {
  1115. return 0;
  1116. }
  1117. }
  1118. void CRYPTO_gcm128_tag(GCM128_CONTEXT *ctx, unsigned char *tag, size_t len) {
  1119. CRYPTO_gcm128_finish(ctx, NULL, 0);
  1120. memcpy(tag, ctx->Xi.c, len <= sizeof(ctx->Xi.c) ? len : sizeof(ctx->Xi.c));
  1121. }
  1122. void CRYPTO_gcm128_release(GCM128_CONTEXT *ctx) {
  1123. if (ctx) {
  1124. OPENSSL_cleanse(ctx, sizeof(*ctx));
  1125. OPENSSL_free(ctx);
  1126. }
  1127. }
  1128. #if defined(OPENSSL_X86) || defined(OPENSSL_X86_64)
  1129. int crypto_gcm_clmul_enabled(void) {
  1130. #ifdef GHASH_ASM
  1131. return OPENSSL_ia32cap_P[0] & (1 << 24) && /* check FXSR bit */
  1132. OPENSSL_ia32cap_P[1] & (1 << 1); /* check PCLMULQDQ bit */
  1133. #else
  1134. return 0;
  1135. #endif
  1136. }
  1137. #endif