Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

1072 Zeilen
28 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. defined(OPENSSL_PPC64LE))
  59. #define GHASH_ASM
  60. #endif
  61. #define PACK(s) ((size_t)(s) << (sizeof(size_t) * 8 - 16))
  62. #define REDUCE1BIT(V) \
  63. do { \
  64. if (sizeof(size_t) == 8) { \
  65. uint64_t T = UINT64_C(0xe100000000000000) & (0 - ((V).lo & 1)); \
  66. (V).lo = ((V).hi << 63) | ((V).lo >> 1); \
  67. (V).hi = ((V).hi >> 1) ^ T; \
  68. } else { \
  69. uint32_t T = 0xe1000000U & (0 - (uint32_t)((V).lo & 1)); \
  70. (V).lo = ((V).hi << 63) | ((V).lo >> 1); \
  71. (V).hi = ((V).hi >> 1) ^ ((uint64_t)T << 32); \
  72. } \
  73. } while (0)
  74. // kSizeTWithoutLower4Bits is a mask that can be used to zero the lower four
  75. // bits of a |size_t|.
  76. static const size_t kSizeTWithoutLower4Bits = (size_t) -16;
  77. static void gcm_init_4bit(u128 Htable[16], uint64_t H[2]) {
  78. u128 V;
  79. Htable[0].hi = 0;
  80. Htable[0].lo = 0;
  81. V.hi = H[0];
  82. V.lo = H[1];
  83. Htable[8] = V;
  84. REDUCE1BIT(V);
  85. Htable[4] = V;
  86. REDUCE1BIT(V);
  87. Htable[2] = V;
  88. REDUCE1BIT(V);
  89. Htable[1] = V;
  90. Htable[3].hi = V.hi ^ Htable[2].hi, Htable[3].lo = V.lo ^ Htable[2].lo;
  91. V = Htable[4];
  92. Htable[5].hi = V.hi ^ Htable[1].hi, Htable[5].lo = V.lo ^ Htable[1].lo;
  93. Htable[6].hi = V.hi ^ Htable[2].hi, Htable[6].lo = V.lo ^ Htable[2].lo;
  94. Htable[7].hi = V.hi ^ Htable[3].hi, Htable[7].lo = V.lo ^ Htable[3].lo;
  95. V = Htable[8];
  96. Htable[9].hi = V.hi ^ Htable[1].hi, Htable[9].lo = V.lo ^ Htable[1].lo;
  97. Htable[10].hi = V.hi ^ Htable[2].hi, Htable[10].lo = V.lo ^ Htable[2].lo;
  98. Htable[11].hi = V.hi ^ Htable[3].hi, Htable[11].lo = V.lo ^ Htable[3].lo;
  99. Htable[12].hi = V.hi ^ Htable[4].hi, Htable[12].lo = V.lo ^ Htable[4].lo;
  100. Htable[13].hi = V.hi ^ Htable[5].hi, Htable[13].lo = V.lo ^ Htable[5].lo;
  101. Htable[14].hi = V.hi ^ Htable[6].hi, Htable[14].lo = V.lo ^ Htable[6].lo;
  102. Htable[15].hi = V.hi ^ Htable[7].hi, Htable[15].lo = V.lo ^ Htable[7].lo;
  103. #if defined(GHASH_ASM) && defined(OPENSSL_ARM)
  104. for (int j = 0; j < 16; ++j) {
  105. V = Htable[j];
  106. Htable[j].hi = V.lo;
  107. Htable[j].lo = V.hi;
  108. }
  109. #endif
  110. }
  111. #if !defined(GHASH_ASM) || defined(OPENSSL_AARCH64) || defined(OPENSSL_PPC64LE)
  112. static const size_t rem_4bit[16] = {
  113. PACK(0x0000), PACK(0x1C20), PACK(0x3840), PACK(0x2460),
  114. PACK(0x7080), PACK(0x6CA0), PACK(0x48C0), PACK(0x54E0),
  115. PACK(0xE100), PACK(0xFD20), PACK(0xD940), PACK(0xC560),
  116. PACK(0x9180), PACK(0x8DA0), PACK(0xA9C0), PACK(0xB5E0)};
  117. static void gcm_gmult_4bit(uint64_t Xi[2], const u128 Htable[16]) {
  118. u128 Z;
  119. int cnt = 15;
  120. size_t rem, nlo, nhi;
  121. nlo = ((const uint8_t *)Xi)[15];
  122. nhi = nlo >> 4;
  123. nlo &= 0xf;
  124. Z.hi = Htable[nlo].hi;
  125. Z.lo = Htable[nlo].lo;
  126. while (1) {
  127. rem = (size_t)Z.lo & 0xf;
  128. Z.lo = (Z.hi << 60) | (Z.lo >> 4);
  129. Z.hi = (Z.hi >> 4);
  130. if (sizeof(size_t) == 8) {
  131. Z.hi ^= rem_4bit[rem];
  132. } else {
  133. Z.hi ^= (uint64_t)rem_4bit[rem] << 32;
  134. }
  135. Z.hi ^= Htable[nhi].hi;
  136. Z.lo ^= Htable[nhi].lo;
  137. if (--cnt < 0) {
  138. break;
  139. }
  140. nlo = ((const uint8_t *)Xi)[cnt];
  141. nhi = nlo >> 4;
  142. nlo &= 0xf;
  143. rem = (size_t)Z.lo & 0xf;
  144. Z.lo = (Z.hi << 60) | (Z.lo >> 4);
  145. Z.hi = (Z.hi >> 4);
  146. if (sizeof(size_t) == 8) {
  147. Z.hi ^= rem_4bit[rem];
  148. } else {
  149. Z.hi ^= (uint64_t)rem_4bit[rem] << 32;
  150. }
  151. Z.hi ^= Htable[nlo].hi;
  152. Z.lo ^= Htable[nlo].lo;
  153. }
  154. Xi[0] = CRYPTO_bswap8(Z.hi);
  155. Xi[1] = CRYPTO_bswap8(Z.lo);
  156. }
  157. /* Streamed gcm_mult_4bit, see CRYPTO_gcm128_[en|de]crypt for
  158. * details... Compiler-generated code doesn't seem to give any
  159. * performance improvement, at least not on x86[_64]. It's here
  160. * mostly as reference and a placeholder for possible future
  161. * non-trivial optimization[s]... */
  162. static void gcm_ghash_4bit(uint64_t Xi[2], const u128 Htable[16],
  163. const uint8_t *inp, size_t len) {
  164. u128 Z;
  165. int cnt;
  166. size_t rem, nlo, nhi;
  167. do {
  168. cnt = 15;
  169. nlo = ((const uint8_t *)Xi)[15];
  170. nlo ^= inp[15];
  171. nhi = nlo >> 4;
  172. nlo &= 0xf;
  173. Z.hi = Htable[nlo].hi;
  174. Z.lo = Htable[nlo].lo;
  175. while (1) {
  176. rem = (size_t)Z.lo & 0xf;
  177. Z.lo = (Z.hi << 60) | (Z.lo >> 4);
  178. Z.hi = (Z.hi >> 4);
  179. if (sizeof(size_t) == 8) {
  180. Z.hi ^= rem_4bit[rem];
  181. } else {
  182. Z.hi ^= (uint64_t)rem_4bit[rem] << 32;
  183. }
  184. Z.hi ^= Htable[nhi].hi;
  185. Z.lo ^= Htable[nhi].lo;
  186. if (--cnt < 0) {
  187. break;
  188. }
  189. nlo = ((const uint8_t *)Xi)[cnt];
  190. nlo ^= inp[cnt];
  191. nhi = nlo >> 4;
  192. nlo &= 0xf;
  193. rem = (size_t)Z.lo & 0xf;
  194. Z.lo = (Z.hi << 60) | (Z.lo >> 4);
  195. Z.hi = (Z.hi >> 4);
  196. if (sizeof(size_t) == 8) {
  197. Z.hi ^= rem_4bit[rem];
  198. } else {
  199. Z.hi ^= (uint64_t)rem_4bit[rem] << 32;
  200. }
  201. Z.hi ^= Htable[nlo].hi;
  202. Z.lo ^= Htable[nlo].lo;
  203. }
  204. Xi[0] = CRYPTO_bswap8(Z.hi);
  205. Xi[1] = CRYPTO_bswap8(Z.lo);
  206. } while (inp += 16, len -= 16);
  207. }
  208. #else /* GHASH_ASM */
  209. void gcm_gmult_4bit(uint64_t Xi[2], const u128 Htable[16]);
  210. void gcm_ghash_4bit(uint64_t Xi[2], const u128 Htable[16], const uint8_t *inp,
  211. size_t len);
  212. #endif
  213. #define GCM_MUL(ctx, Xi) gcm_gmult_4bit((ctx)->Xi.u, (ctx)->Htable)
  214. #if defined(GHASH_ASM)
  215. #define GHASH(ctx, in, len) gcm_ghash_4bit((ctx)->Xi.u, (ctx)->Htable, in, len)
  216. /* GHASH_CHUNK is "stride parameter" missioned to mitigate cache
  217. * trashing effect. In other words idea is to hash data while it's
  218. * still in L1 cache after encryption pass... */
  219. #define GHASH_CHUNK (3 * 1024)
  220. #endif
  221. #if defined(GHASH_ASM)
  222. #if defined(OPENSSL_X86) || defined(OPENSSL_X86_64)
  223. #define GCM_FUNCREF_4BIT
  224. void gcm_init_clmul(u128 Htable[16], const uint64_t Xi[2]);
  225. void gcm_gmult_clmul(uint64_t Xi[2], const u128 Htable[16]);
  226. void gcm_ghash_clmul(uint64_t Xi[2], const u128 Htable[16], const uint8_t *inp,
  227. size_t len);
  228. #if defined(OPENSSL_X86_64)
  229. #define GHASH_ASM_X86_64
  230. void gcm_init_avx(u128 Htable[16], const uint64_t Xi[2]);
  231. void gcm_gmult_avx(uint64_t Xi[2], const u128 Htable[16]);
  232. void gcm_ghash_avx(uint64_t Xi[2], const u128 Htable[16], const uint8_t *in,
  233. size_t len);
  234. #define AESNI_GCM
  235. static int aesni_gcm_enabled(GCM128_CONTEXT *ctx, ctr128_f stream) {
  236. return stream == aesni_ctr32_encrypt_blocks &&
  237. ctx->ghash == gcm_ghash_avx;
  238. }
  239. size_t aesni_gcm_encrypt(const uint8_t *in, uint8_t *out, size_t len,
  240. const void *key, uint8_t ivec[16], uint64_t *Xi);
  241. size_t aesni_gcm_decrypt(const uint8_t *in, uint8_t *out, size_t len,
  242. const void *key, uint8_t ivec[16], uint64_t *Xi);
  243. #endif
  244. #if defined(OPENSSL_X86)
  245. #define GHASH_ASM_X86
  246. void gcm_gmult_4bit_mmx(uint64_t Xi[2], const u128 Htable[16]);
  247. void gcm_ghash_4bit_mmx(uint64_t Xi[2], const u128 Htable[16], const uint8_t *inp,
  248. size_t len);
  249. #endif
  250. #elif defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64)
  251. #include <openssl/arm_arch.h>
  252. #if __ARM_ARCH__ >= 7
  253. #define GHASH_ASM_ARM
  254. #define GCM_FUNCREF_4BIT
  255. static int pmull_capable(void) {
  256. return CRYPTO_is_ARMv8_PMULL_capable();
  257. }
  258. void gcm_init_v8(u128 Htable[16], const uint64_t Xi[2]);
  259. void gcm_gmult_v8(uint64_t Xi[2], const u128 Htable[16]);
  260. void gcm_ghash_v8(uint64_t Xi[2], const u128 Htable[16], const uint8_t *inp,
  261. size_t len);
  262. #if defined(OPENSSL_ARM)
  263. /* 32-bit ARM also has support for doing GCM with NEON instructions. */
  264. static int neon_capable(void) {
  265. return CRYPTO_is_NEON_capable();
  266. }
  267. void gcm_init_neon(u128 Htable[16], const uint64_t Xi[2]);
  268. void gcm_gmult_neon(uint64_t Xi[2], const u128 Htable[16]);
  269. void gcm_ghash_neon(uint64_t Xi[2], const u128 Htable[16], const uint8_t *inp,
  270. size_t len);
  271. #else
  272. /* AArch64 only has the ARMv8 versions of functions. */
  273. static int neon_capable(void) {
  274. return 0;
  275. }
  276. static void gcm_init_neon(u128 Htable[16], const uint64_t Xi[2]) {
  277. abort();
  278. }
  279. static void gcm_gmult_neon(uint64_t Xi[2], const u128 Htable[16]) {
  280. abort();
  281. }
  282. static void gcm_ghash_neon(uint64_t Xi[2], const u128 Htable[16],
  283. const uint8_t *inp, size_t len) {
  284. abort();
  285. }
  286. #endif
  287. #endif
  288. #elif defined(OPENSSL_PPC64LE)
  289. #define GHASH_ASM_PPC64LE
  290. #define GCM_FUNCREF_4BIT
  291. void gcm_init_p8(u128 Htable[16], const uint64_t Xi[2]);
  292. void gcm_gmult_p8(uint64_t Xi[2], const u128 Htable[16]);
  293. void gcm_ghash_p8(uint64_t Xi[2], const u128 Htable[16], const uint8_t *inp,
  294. size_t len);
  295. #endif
  296. #endif
  297. #ifdef GCM_FUNCREF_4BIT
  298. #undef GCM_MUL
  299. #define GCM_MUL(ctx, Xi) (*gcm_gmult_p)((ctx)->Xi.u, (ctx)->Htable)
  300. #ifdef GHASH
  301. #undef GHASH
  302. #define GHASH(ctx, in, len) (*gcm_ghash_p)((ctx)->Xi.u, (ctx)->Htable, in, len)
  303. #endif
  304. #endif
  305. void CRYPTO_ghash_init(gmult_func *out_mult, ghash_func *out_hash,
  306. u128 *out_key, u128 out_table[16],
  307. const uint8_t *gcm_key) {
  308. union {
  309. uint64_t u[2];
  310. uint8_t c[16];
  311. } H;
  312. OPENSSL_memcpy(H.c, gcm_key, 16);
  313. /* H is stored in host byte order */
  314. H.u[0] = CRYPTO_bswap8(H.u[0]);
  315. H.u[1] = CRYPTO_bswap8(H.u[1]);
  316. OPENSSL_memcpy(out_key, H.c, 16);
  317. #if defined(GHASH_ASM_X86_64)
  318. if (crypto_gcm_clmul_enabled()) {
  319. if (((OPENSSL_ia32cap_P[1] >> 22) & 0x41) == 0x41) { /* AVX+MOVBE */
  320. gcm_init_avx(out_table, H.u);
  321. *out_mult = gcm_gmult_avx;
  322. *out_hash = gcm_ghash_avx;
  323. return;
  324. }
  325. gcm_init_clmul(out_table, H.u);
  326. *out_mult = gcm_gmult_clmul;
  327. *out_hash = gcm_ghash_clmul;
  328. return;
  329. }
  330. #elif defined(GHASH_ASM_X86)
  331. if (crypto_gcm_clmul_enabled()) {
  332. gcm_init_clmul(out_table, H.u);
  333. *out_mult = gcm_gmult_clmul;
  334. *out_hash = gcm_ghash_clmul;
  335. return;
  336. }
  337. #elif defined(GHASH_ASM_ARM)
  338. if (pmull_capable()) {
  339. gcm_init_v8(out_table, H.u);
  340. *out_mult = gcm_gmult_v8;
  341. *out_hash = gcm_ghash_v8;
  342. return;
  343. }
  344. if (neon_capable()) {
  345. gcm_init_neon(out_table, H.u);
  346. *out_mult = gcm_gmult_neon;
  347. *out_hash = gcm_ghash_neon;
  348. return;
  349. }
  350. #elif defined(GHASH_ASM_PPC64LE)
  351. if (CRYPTO_is_PPC64LE_vcrypto_capable()) {
  352. gcm_init_p8(out_table, H.u);
  353. *out_mult = gcm_gmult_p8;
  354. *out_hash = gcm_ghash_p8;
  355. return;
  356. }
  357. #endif
  358. gcm_init_4bit(out_table, H.u);
  359. #if defined(GHASH_ASM_X86)
  360. *out_mult = gcm_gmult_4bit_mmx;
  361. *out_hash = gcm_ghash_4bit_mmx;
  362. #else
  363. *out_mult = gcm_gmult_4bit;
  364. *out_hash = gcm_ghash_4bit;
  365. #endif
  366. }
  367. void CRYPTO_gcm128_init(GCM128_CONTEXT *ctx, const void *aes_key,
  368. block128_f block) {
  369. OPENSSL_memset(ctx, 0, sizeof(*ctx));
  370. ctx->block = block;
  371. uint8_t gcm_key[16];
  372. OPENSSL_memset(gcm_key, 0, sizeof(gcm_key));
  373. (*block)(gcm_key, gcm_key, aes_key);
  374. CRYPTO_ghash_init(&ctx->gmult, &ctx->ghash, &ctx->H, ctx->Htable, gcm_key);
  375. }
  376. void CRYPTO_gcm128_setiv(GCM128_CONTEXT *ctx, const void *key,
  377. const uint8_t *iv, size_t len) {
  378. unsigned int ctr;
  379. #ifdef GCM_FUNCREF_4BIT
  380. void (*gcm_gmult_p)(uint64_t Xi[2], const u128 Htable[16]) = ctx->gmult;
  381. #endif
  382. ctx->Yi.u[0] = 0;
  383. ctx->Yi.u[1] = 0;
  384. ctx->Xi.u[0] = 0;
  385. ctx->Xi.u[1] = 0;
  386. ctx->len.u[0] = 0; /* AAD length */
  387. ctx->len.u[1] = 0; /* message length */
  388. ctx->ares = 0;
  389. ctx->mres = 0;
  390. if (len == 12) {
  391. OPENSSL_memcpy(ctx->Yi.c, iv, 12);
  392. ctx->Yi.c[15] = 1;
  393. ctr = 1;
  394. } else {
  395. uint64_t len0 = len;
  396. while (len >= 16) {
  397. for (size_t i = 0; i < 16; ++i) {
  398. ctx->Yi.c[i] ^= iv[i];
  399. }
  400. GCM_MUL(ctx, Yi);
  401. iv += 16;
  402. len -= 16;
  403. }
  404. if (len) {
  405. for (size_t i = 0; i < len; ++i) {
  406. ctx->Yi.c[i] ^= iv[i];
  407. }
  408. GCM_MUL(ctx, Yi);
  409. }
  410. len0 <<= 3;
  411. ctx->Yi.u[1] ^= CRYPTO_bswap8(len0);
  412. GCM_MUL(ctx, Yi);
  413. ctr = GETU32_aligned(ctx->Yi.c + 12);
  414. }
  415. (*ctx->block)(ctx->Yi.c, ctx->EK0.c, key);
  416. ++ctr;
  417. PUTU32_aligned(ctx->Yi.c + 12, ctr);
  418. }
  419. int CRYPTO_gcm128_aad(GCM128_CONTEXT *ctx, const uint8_t *aad, size_t len) {
  420. unsigned int n;
  421. uint64_t alen = ctx->len.u[0];
  422. #ifdef GCM_FUNCREF_4BIT
  423. void (*gcm_gmult_p)(uint64_t Xi[2], const u128 Htable[16]) = ctx->gmult;
  424. #ifdef GHASH
  425. void (*gcm_ghash_p)(uint64_t Xi[2], const u128 Htable[16], const uint8_t *inp,
  426. size_t len) = ctx->ghash;
  427. #endif
  428. #endif
  429. if (ctx->len.u[1]) {
  430. return 0;
  431. }
  432. alen += len;
  433. if (alen > (UINT64_C(1) << 61) || (sizeof(len) == 8 && alen < len)) {
  434. return 0;
  435. }
  436. ctx->len.u[0] = alen;
  437. n = ctx->ares;
  438. if (n) {
  439. while (n && len) {
  440. ctx->Xi.c[n] ^= *(aad++);
  441. --len;
  442. n = (n + 1) % 16;
  443. }
  444. if (n == 0) {
  445. GCM_MUL(ctx, Xi);
  446. } else {
  447. ctx->ares = n;
  448. return 1;
  449. }
  450. }
  451. /* Process a whole number of blocks. */
  452. #ifdef GHASH
  453. size_t len_blocks = len & kSizeTWithoutLower4Bits;
  454. if (len_blocks != 0) {
  455. GHASH(ctx, aad, len_blocks);
  456. aad += len_blocks;
  457. len -= len_blocks;
  458. }
  459. #else
  460. while (len >= 16) {
  461. for (size_t i = 0; i < 16; ++i) {
  462. ctx->Xi.c[i] ^= aad[i];
  463. }
  464. GCM_MUL(ctx, Xi);
  465. aad += 16;
  466. len -= 16;
  467. }
  468. #endif
  469. /* Process the remainder. */
  470. if (len != 0) {
  471. n = (unsigned int)len;
  472. for (size_t i = 0; i < len; ++i) {
  473. ctx->Xi.c[i] ^= aad[i];
  474. }
  475. }
  476. ctx->ares = n;
  477. return 1;
  478. }
  479. int CRYPTO_gcm128_encrypt(GCM128_CONTEXT *ctx, const void *key,
  480. const unsigned char *in, unsigned char *out,
  481. size_t len) {
  482. unsigned int n, ctr;
  483. uint64_t mlen = ctx->len.u[1];
  484. block128_f block = ctx->block;
  485. #ifdef GCM_FUNCREF_4BIT
  486. void (*gcm_gmult_p)(uint64_t Xi[2], const u128 Htable[16]) = ctx->gmult;
  487. #ifdef GHASH
  488. void (*gcm_ghash_p)(uint64_t Xi[2], const u128 Htable[16], const uint8_t *inp,
  489. size_t len) = ctx->ghash;
  490. #endif
  491. #endif
  492. mlen += len;
  493. if (mlen > ((UINT64_C(1) << 36) - 32) ||
  494. (sizeof(len) == 8 && mlen < len)) {
  495. return 0;
  496. }
  497. ctx->len.u[1] = mlen;
  498. if (ctx->ares) {
  499. /* First call to encrypt finalizes GHASH(AAD) */
  500. GCM_MUL(ctx, Xi);
  501. ctx->ares = 0;
  502. }
  503. ctr = GETU32_aligned(ctx->Yi.c + 12);
  504. n = ctx->mres;
  505. if (n) {
  506. while (n && len) {
  507. ctx->Xi.c[n] ^= *(out++) = *(in++) ^ ctx->EKi.c[n];
  508. --len;
  509. n = (n + 1) % 16;
  510. }
  511. if (n == 0) {
  512. GCM_MUL(ctx, Xi);
  513. } else {
  514. ctx->mres = n;
  515. return 1;
  516. }
  517. }
  518. if (STRICT_ALIGNMENT && ((size_t)in | (size_t)out) % sizeof(size_t) != 0) {
  519. for (size_t i = 0; i < len; ++i) {
  520. if (n == 0) {
  521. (*block)(ctx->Yi.c, ctx->EKi.c, key);
  522. ++ctr;
  523. PUTU32_aligned(ctx->Yi.c + 12, ctr);
  524. }
  525. ctx->Xi.c[n] ^= out[i] = in[i] ^ ctx->EKi.c[n];
  526. n = (n + 1) % 16;
  527. if (n == 0) {
  528. GCM_MUL(ctx, Xi);
  529. }
  530. }
  531. ctx->mres = n;
  532. return 1;
  533. }
  534. #if defined(GHASH) && defined(GHASH_CHUNK)
  535. while (len >= GHASH_CHUNK) {
  536. size_t j = GHASH_CHUNK;
  537. while (j) {
  538. size_t *out_t = (size_t *)out;
  539. const size_t *in_t = (const size_t *)in;
  540. (*block)(ctx->Yi.c, ctx->EKi.c, key);
  541. ++ctr;
  542. PUTU32_aligned(ctx->Yi.c + 12, ctr);
  543. for (size_t i = 0; i < 16 / sizeof(size_t); ++i) {
  544. out_t[i] = in_t[i] ^ ctx->EKi.t[i];
  545. }
  546. out += 16;
  547. in += 16;
  548. j -= 16;
  549. }
  550. GHASH(ctx, out - GHASH_CHUNK, GHASH_CHUNK);
  551. len -= GHASH_CHUNK;
  552. }
  553. size_t len_blocks = len & kSizeTWithoutLower4Bits;
  554. if (len_blocks != 0) {
  555. while (len >= 16) {
  556. size_t *out_t = (size_t *)out;
  557. const size_t *in_t = (const size_t *)in;
  558. (*block)(ctx->Yi.c, ctx->EKi.c, key);
  559. ++ctr;
  560. PUTU32_aligned(ctx->Yi.c + 12, ctr);
  561. for (size_t i = 0; i < 16 / sizeof(size_t); ++i) {
  562. out_t[i] = in_t[i] ^ ctx->EKi.t[i];
  563. }
  564. out += 16;
  565. in += 16;
  566. len -= 16;
  567. }
  568. GHASH(ctx, out - len_blocks, len_blocks);
  569. }
  570. #else
  571. while (len >= 16) {
  572. size_t *out_t = (size_t *)out;
  573. const size_t *in_t = (const size_t *)in;
  574. (*block)(ctx->Yi.c, ctx->EKi.c, key);
  575. ++ctr;
  576. PUTU32_aligned(ctx->Yi.c + 12, ctr);
  577. for (size_t i = 0; i < 16 / sizeof(size_t); ++i) {
  578. ctx->Xi.t[i] ^= out_t[i] = in_t[i] ^ ctx->EKi.t[i];
  579. }
  580. GCM_MUL(ctx, Xi);
  581. out += 16;
  582. in += 16;
  583. len -= 16;
  584. }
  585. #endif
  586. if (len) {
  587. (*block)(ctx->Yi.c, ctx->EKi.c, key);
  588. ++ctr;
  589. PUTU32_aligned(ctx->Yi.c + 12, ctr);
  590. while (len--) {
  591. ctx->Xi.c[n] ^= out[n] = in[n] ^ ctx->EKi.c[n];
  592. ++n;
  593. }
  594. }
  595. ctx->mres = n;
  596. return 1;
  597. }
  598. int CRYPTO_gcm128_decrypt(GCM128_CONTEXT *ctx, const void *key,
  599. const unsigned char *in, unsigned char *out,
  600. size_t len) {
  601. unsigned int n, ctr;
  602. uint64_t mlen = ctx->len.u[1];
  603. block128_f block = ctx->block;
  604. #ifdef GCM_FUNCREF_4BIT
  605. void (*gcm_gmult_p)(uint64_t Xi[2], const u128 Htable[16]) = ctx->gmult;
  606. #ifdef GHASH
  607. void (*gcm_ghash_p)(uint64_t Xi[2], const u128 Htable[16], const uint8_t *inp,
  608. size_t len) = ctx->ghash;
  609. #endif
  610. #endif
  611. mlen += len;
  612. if (mlen > ((UINT64_C(1) << 36) - 32) ||
  613. (sizeof(len) == 8 && mlen < len)) {
  614. return 0;
  615. }
  616. ctx->len.u[1] = mlen;
  617. if (ctx->ares) {
  618. /* First call to decrypt finalizes GHASH(AAD) */
  619. GCM_MUL(ctx, Xi);
  620. ctx->ares = 0;
  621. }
  622. ctr = GETU32_aligned(ctx->Yi.c + 12);
  623. n = ctx->mres;
  624. if (n) {
  625. while (n && len) {
  626. uint8_t c = *(in++);
  627. *(out++) = c ^ ctx->EKi.c[n];
  628. ctx->Xi.c[n] ^= c;
  629. --len;
  630. n = (n + 1) % 16;
  631. }
  632. if (n == 0) {
  633. GCM_MUL(ctx, Xi);
  634. } else {
  635. ctx->mres = n;
  636. return 1;
  637. }
  638. }
  639. if (STRICT_ALIGNMENT && ((size_t)in | (size_t)out) % sizeof(size_t) != 0) {
  640. for (size_t i = 0; i < len; ++i) {
  641. uint8_t c;
  642. if (n == 0) {
  643. (*block)(ctx->Yi.c, ctx->EKi.c, key);
  644. ++ctr;
  645. PUTU32_aligned(ctx->Yi.c + 12, ctr);
  646. }
  647. c = in[i];
  648. out[i] = c ^ ctx->EKi.c[n];
  649. ctx->Xi.c[n] ^= c;
  650. n = (n + 1) % 16;
  651. if (n == 0) {
  652. GCM_MUL(ctx, Xi);
  653. }
  654. }
  655. ctx->mres = n;
  656. return 1;
  657. }
  658. #if defined(GHASH) && defined(GHASH_CHUNK)
  659. while (len >= GHASH_CHUNK) {
  660. size_t j = GHASH_CHUNK;
  661. GHASH(ctx, in, GHASH_CHUNK);
  662. while (j) {
  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. PUTU32_aligned(ctx->Yi.c + 12, ctr);
  668. for (size_t i = 0; i < 16 / sizeof(size_t); ++i) {
  669. out_t[i] = in_t[i] ^ ctx->EKi.t[i];
  670. }
  671. out += 16;
  672. in += 16;
  673. j -= 16;
  674. }
  675. len -= GHASH_CHUNK;
  676. }
  677. size_t len_blocks = len & kSizeTWithoutLower4Bits;
  678. if (len_blocks != 0) {
  679. GHASH(ctx, in, len_blocks);
  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. PUTU32_aligned(ctx->Yi.c + 12, ctr);
  686. for (size_t i = 0; i < 16 / sizeof(size_t); ++i) {
  687. out_t[i] = in_t[i] ^ ctx->EKi.t[i];
  688. }
  689. out += 16;
  690. in += 16;
  691. len -= 16;
  692. }
  693. }
  694. #else
  695. while (len >= 16) {
  696. size_t *out_t = (size_t *)out;
  697. const size_t *in_t = (const size_t *)in;
  698. (*block)(ctx->Yi.c, ctx->EKi.c, key);
  699. ++ctr;
  700. PUTU32_aligned(ctx->Yi.c + 12, ctr);
  701. for (size_t i = 0; i < 16 / sizeof(size_t); ++i) {
  702. size_t c = in_t[i];
  703. out_t[i] = c ^ ctx->EKi.t[i];
  704. ctx->Xi.t[i] ^= c;
  705. }
  706. GCM_MUL(ctx, Xi);
  707. out += 16;
  708. in += 16;
  709. len -= 16;
  710. }
  711. #endif
  712. if (len) {
  713. (*block)(ctx->Yi.c, ctx->EKi.c, key);
  714. ++ctr;
  715. PUTU32_aligned(ctx->Yi.c + 12, ctr);
  716. while (len--) {
  717. uint8_t c = in[n];
  718. ctx->Xi.c[n] ^= c;
  719. out[n] = c ^ ctx->EKi.c[n];
  720. ++n;
  721. }
  722. }
  723. ctx->mres = n;
  724. return 1;
  725. }
  726. int CRYPTO_gcm128_encrypt_ctr32(GCM128_CONTEXT *ctx, const void *key,
  727. const uint8_t *in, uint8_t *out, size_t len,
  728. ctr128_f stream) {
  729. unsigned int n, ctr;
  730. uint64_t mlen = ctx->len.u[1];
  731. #ifdef GCM_FUNCREF_4BIT
  732. void (*gcm_gmult_p)(uint64_t Xi[2], const u128 Htable[16]) = ctx->gmult;
  733. #ifdef GHASH
  734. void (*gcm_ghash_p)(uint64_t Xi[2], const u128 Htable[16], const uint8_t *inp,
  735. size_t len) = ctx->ghash;
  736. #endif
  737. #endif
  738. mlen += len;
  739. if (mlen > ((UINT64_C(1) << 36) - 32) ||
  740. (sizeof(len) == 8 && mlen < len)) {
  741. return 0;
  742. }
  743. ctx->len.u[1] = mlen;
  744. if (ctx->ares) {
  745. /* First call to encrypt finalizes GHASH(AAD) */
  746. GCM_MUL(ctx, Xi);
  747. ctx->ares = 0;
  748. }
  749. n = ctx->mres;
  750. if (n) {
  751. while (n && len) {
  752. ctx->Xi.c[n] ^= *(out++) = *(in++) ^ ctx->EKi.c[n];
  753. --len;
  754. n = (n + 1) % 16;
  755. }
  756. if (n == 0) {
  757. GCM_MUL(ctx, Xi);
  758. } else {
  759. ctx->mres = n;
  760. return 1;
  761. }
  762. }
  763. #if defined(AESNI_GCM)
  764. if (aesni_gcm_enabled(ctx, stream)) {
  765. /* |aesni_gcm_encrypt| may not process all the input given to it. It may
  766. * not process *any* of its input if it is deemed too small. */
  767. size_t bulk = aesni_gcm_encrypt(in, out, len, key, ctx->Yi.c, ctx->Xi.u);
  768. in += bulk;
  769. out += bulk;
  770. len -= bulk;
  771. }
  772. #endif
  773. ctr = GETU32_aligned(ctx->Yi.c + 12);
  774. #if defined(GHASH)
  775. while (len >= GHASH_CHUNK) {
  776. (*stream)(in, out, GHASH_CHUNK / 16, key, ctx->Yi.c);
  777. ctr += GHASH_CHUNK / 16;
  778. PUTU32_aligned(ctx->Yi.c + 12, ctr);
  779. GHASH(ctx, out, GHASH_CHUNK);
  780. out += GHASH_CHUNK;
  781. in += GHASH_CHUNK;
  782. len -= GHASH_CHUNK;
  783. }
  784. #endif
  785. size_t i = len & kSizeTWithoutLower4Bits;
  786. if (i != 0) {
  787. size_t j = i / 16;
  788. (*stream)(in, out, j, key, ctx->Yi.c);
  789. ctr += (unsigned int)j;
  790. PUTU32_aligned(ctx->Yi.c + 12, ctr);
  791. in += i;
  792. len -= i;
  793. #if defined(GHASH)
  794. GHASH(ctx, out, i);
  795. out += i;
  796. #else
  797. while (j--) {
  798. for (i = 0; i < 16; ++i) {
  799. ctx->Xi.c[i] ^= out[i];
  800. }
  801. GCM_MUL(ctx, Xi);
  802. out += 16;
  803. }
  804. #endif
  805. }
  806. if (len) {
  807. (*ctx->block)(ctx->Yi.c, ctx->EKi.c, key);
  808. ++ctr;
  809. PUTU32_aligned(ctx->Yi.c + 12, ctr);
  810. while (len--) {
  811. ctx->Xi.c[n] ^= out[n] = in[n] ^ ctx->EKi.c[n];
  812. ++n;
  813. }
  814. }
  815. ctx->mres = n;
  816. return 1;
  817. }
  818. int CRYPTO_gcm128_decrypt_ctr32(GCM128_CONTEXT *ctx, const void *key,
  819. const uint8_t *in, uint8_t *out, size_t len,
  820. ctr128_f stream) {
  821. unsigned int n, ctr;
  822. uint64_t mlen = ctx->len.u[1];
  823. #ifdef GCM_FUNCREF_4BIT
  824. void (*gcm_gmult_p)(uint64_t Xi[2], const u128 Htable[16]) = ctx->gmult;
  825. #ifdef GHASH
  826. void (*gcm_ghash_p)(uint64_t Xi[2], const u128 Htable[16], const uint8_t *inp,
  827. size_t len) = ctx->ghash;
  828. #endif
  829. #endif
  830. mlen += len;
  831. if (mlen > ((UINT64_C(1) << 36) - 32) ||
  832. (sizeof(len) == 8 && mlen < len)) {
  833. return 0;
  834. }
  835. ctx->len.u[1] = mlen;
  836. if (ctx->ares) {
  837. /* First call to decrypt finalizes GHASH(AAD) */
  838. GCM_MUL(ctx, Xi);
  839. ctx->ares = 0;
  840. }
  841. n = ctx->mres;
  842. if (n) {
  843. while (n && len) {
  844. uint8_t c = *(in++);
  845. *(out++) = c ^ ctx->EKi.c[n];
  846. ctx->Xi.c[n] ^= c;
  847. --len;
  848. n = (n + 1) % 16;
  849. }
  850. if (n == 0) {
  851. GCM_MUL(ctx, Xi);
  852. } else {
  853. ctx->mres = n;
  854. return 1;
  855. }
  856. }
  857. #if defined(AESNI_GCM)
  858. if (aesni_gcm_enabled(ctx, stream)) {
  859. /* |aesni_gcm_decrypt| may not process all the input given to it. It may
  860. * not process *any* of its input if it is deemed too small. */
  861. size_t bulk = aesni_gcm_decrypt(in, out, len, key, ctx->Yi.c, ctx->Xi.u);
  862. in += bulk;
  863. out += bulk;
  864. len -= bulk;
  865. }
  866. #endif
  867. ctr = GETU32_aligned(ctx->Yi.c + 12);
  868. #if defined(GHASH)
  869. while (len >= GHASH_CHUNK) {
  870. GHASH(ctx, in, GHASH_CHUNK);
  871. (*stream)(in, out, GHASH_CHUNK / 16, key, ctx->Yi.c);
  872. ctr += GHASH_CHUNK / 16;
  873. PUTU32_aligned(ctx->Yi.c + 12, ctr);
  874. out += GHASH_CHUNK;
  875. in += GHASH_CHUNK;
  876. len -= GHASH_CHUNK;
  877. }
  878. #endif
  879. size_t i = len & kSizeTWithoutLower4Bits;
  880. if (i != 0) {
  881. size_t j = i / 16;
  882. #if defined(GHASH)
  883. GHASH(ctx, in, i);
  884. #else
  885. while (j--) {
  886. size_t k;
  887. for (k = 0; k < 16; ++k) {
  888. ctx->Xi.c[k] ^= in[k];
  889. }
  890. GCM_MUL(ctx, Xi);
  891. in += 16;
  892. }
  893. j = i / 16;
  894. in -= i;
  895. #endif
  896. (*stream)(in, out, j, key, ctx->Yi.c);
  897. ctr += (unsigned int)j;
  898. PUTU32_aligned(ctx->Yi.c + 12, ctr);
  899. out += i;
  900. in += i;
  901. len -= i;
  902. }
  903. if (len) {
  904. (*ctx->block)(ctx->Yi.c, ctx->EKi.c, key);
  905. ++ctr;
  906. PUTU32_aligned(ctx->Yi.c + 12, ctr);
  907. while (len--) {
  908. uint8_t c = in[n];
  909. ctx->Xi.c[n] ^= c;
  910. out[n] = c ^ ctx->EKi.c[n];
  911. ++n;
  912. }
  913. }
  914. ctx->mres = n;
  915. return 1;
  916. }
  917. int CRYPTO_gcm128_finish(GCM128_CONTEXT *ctx, const uint8_t *tag, size_t len) {
  918. uint64_t alen = ctx->len.u[0] << 3;
  919. uint64_t clen = ctx->len.u[1] << 3;
  920. #ifdef GCM_FUNCREF_4BIT
  921. void (*gcm_gmult_p)(uint64_t Xi[2], const u128 Htable[16]) = ctx->gmult;
  922. #endif
  923. if (ctx->mres || ctx->ares) {
  924. GCM_MUL(ctx, Xi);
  925. }
  926. alen = CRYPTO_bswap8(alen);
  927. clen = CRYPTO_bswap8(clen);
  928. ctx->Xi.u[0] ^= alen;
  929. ctx->Xi.u[1] ^= clen;
  930. GCM_MUL(ctx, Xi);
  931. ctx->Xi.u[0] ^= ctx->EK0.u[0];
  932. ctx->Xi.u[1] ^= ctx->EK0.u[1];
  933. if (tag && len <= sizeof(ctx->Xi)) {
  934. return CRYPTO_memcmp(ctx->Xi.c, tag, len) == 0;
  935. } else {
  936. return 0;
  937. }
  938. }
  939. void CRYPTO_gcm128_tag(GCM128_CONTEXT *ctx, unsigned char *tag, size_t len) {
  940. CRYPTO_gcm128_finish(ctx, NULL, 0);
  941. OPENSSL_memcpy(tag, ctx->Xi.c,
  942. len <= sizeof(ctx->Xi.c) ? len : sizeof(ctx->Xi.c));
  943. }
  944. #if defined(OPENSSL_X86) || defined(OPENSSL_X86_64)
  945. int crypto_gcm_clmul_enabled(void) {
  946. #ifdef GHASH_ASM
  947. return OPENSSL_ia32cap_P[0] & (1 << 24) && /* check FXSR bit */
  948. OPENSSL_ia32cap_P[1] & (1 << 1); /* check PCLMULQDQ bit */
  949. #else
  950. return 0;
  951. #endif
  952. }
  953. #endif