Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

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