summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/modes
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/lib/libcrypto/modes/cbc128.c16
-rw-r--r--src/lib/libcrypto/modes/ccm128.c12
-rw-r--r--src/lib/libcrypto/modes/cfb128.c16
-rw-r--r--src/lib/libcrypto/modes/ctr128.c38
-rw-r--r--src/lib/libcrypto/modes/gcm128.c92
-rw-r--r--src/lib/libcrypto/modes/modes_local.h40
-rw-r--r--src/lib/libcrypto/modes/ofb128.c14
-rw-r--r--src/lib/libcrypto/modes/xts128.c13
8 files changed, 48 insertions, 193 deletions
diff --git a/src/lib/libcrypto/modes/cbc128.c b/src/lib/libcrypto/modes/cbc128.c
index f8ebf79a87..1b6858ee25 100644
--- a/src/lib/libcrypto/modes/cbc128.c
+++ b/src/lib/libcrypto/modes/cbc128.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: cbc128.c,v 1.8 2023/07/08 14:56:54 beck Exp $ */ 1/* $OpenBSD: cbc128.c,v 1.11 2025/04/23 10:09:08 jsing Exp $ */
2/* ==================================================================== 2/* ====================================================================
3 * Copyright (c) 2008 The OpenSSL Project. All rights reserved. 3 * Copyright (c) 2008 The OpenSSL Project. All rights reserved.
4 * 4 *
@@ -49,15 +49,11 @@
49 * 49 *
50 */ 50 */
51 51
52#include <openssl/crypto.h>
53#include "modes_local.h"
54#include <string.h> 52#include <string.h>
55 53
56#ifndef MODES_DEBUG 54#include <openssl/crypto.h>
57# ifndef NDEBUG 55
58# define NDEBUG 56#include "modes_local.h"
59# endif
60#endif
61 57
62#undef STRICT_ALIGNMENT 58#undef STRICT_ALIGNMENT
63#ifdef __STRICT_ALIGNMENT 59#ifdef __STRICT_ALIGNMENT
@@ -74,7 +70,6 @@ CRYPTO_cbc128_encrypt(const unsigned char *in, unsigned char *out,
74 size_t n; 70 size_t n;
75 const unsigned char *iv = ivec; 71 const unsigned char *iv = ivec;
76 72
77#if !defined(OPENSSL_SMALL_FOOTPRINT)
78 if (STRICT_ALIGNMENT && 73 if (STRICT_ALIGNMENT &&
79 ((size_t)in|(size_t)out|(size_t)ivec) % sizeof(size_t) != 0) { 74 ((size_t)in|(size_t)out|(size_t)ivec) % sizeof(size_t) != 0) {
80 while (len >= 16) { 75 while (len >= 16) {
@@ -98,7 +93,6 @@ CRYPTO_cbc128_encrypt(const unsigned char *in, unsigned char *out,
98 out += 16; 93 out += 16;
99 } 94 }
100 } 95 }
101#endif
102 while (len) { 96 while (len) {
103 for (n = 0; n < 16 && n < len; ++n) 97 for (n = 0; n < 16 && n < len; ++n)
104 out[n] = in[n] ^ iv[n]; 98 out[n] = in[n] ^ iv[n];
@@ -127,7 +121,6 @@ CRYPTO_cbc128_decrypt(const unsigned char *in, unsigned char *out,
127 unsigned char c[16]; 121 unsigned char c[16];
128 } tmp; 122 } tmp;
129 123
130#if !defined(OPENSSL_SMALL_FOOTPRINT)
131 if (in != out) { 124 if (in != out) {
132 const unsigned char *iv = ivec; 125 const unsigned char *iv = ivec;
133 126
@@ -192,7 +185,6 @@ CRYPTO_cbc128_decrypt(const unsigned char *in, unsigned char *out,
192 } 185 }
193 } 186 }
194 } 187 }
195#endif
196 while (len) { 188 while (len) {
197 unsigned char c; 189 unsigned char c;
198 (*block)(in, tmp.c, key); 190 (*block)(in, tmp.c, key);
diff --git a/src/lib/libcrypto/modes/ccm128.c b/src/lib/libcrypto/modes/ccm128.c
index 68c5cce5da..0f592dd9e5 100644
--- a/src/lib/libcrypto/modes/ccm128.c
+++ b/src/lib/libcrypto/modes/ccm128.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: ccm128.c,v 1.8 2023/07/08 14:56:54 beck Exp $ */ 1/* $OpenBSD: ccm128.c,v 1.10 2025/04/21 16:01:18 jsing Exp $ */
2/* ==================================================================== 2/* ====================================================================
3 * Copyright (c) 2011 The OpenSSL Project. All rights reserved. 3 * Copyright (c) 2011 The OpenSSL Project. All rights reserved.
4 * 4 *
@@ -48,15 +48,11 @@
48 * ==================================================================== 48 * ====================================================================
49 */ 49 */
50 50
51#include <openssl/crypto.h>
52#include "modes_local.h"
53#include <string.h> 51#include <string.h>
54 52
55#ifndef MODES_DEBUG 53#include <openssl/crypto.h>
56# ifndef NDEBUG 54
57# define NDEBUG 55#include "modes_local.h"
58# endif
59#endif
60 56
61/* First you setup M and L parameters and pass the key schedule. 57/* First you setup M and L parameters and pass the key schedule.
62 * This is called once per session setup... */ 58 * This is called once per session setup... */
diff --git a/src/lib/libcrypto/modes/cfb128.c b/src/lib/libcrypto/modes/cfb128.c
index 931353a620..9a63a46724 100644
--- a/src/lib/libcrypto/modes/cfb128.c
+++ b/src/lib/libcrypto/modes/cfb128.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: cfb128.c,v 1.7 2023/07/08 14:56:54 beck Exp $ */ 1/* $OpenBSD: cfb128.c,v 1.10 2025/04/23 10:09:08 jsing Exp $ */
2/* ==================================================================== 2/* ====================================================================
3 * Copyright (c) 2008 The OpenSSL Project. All rights reserved. 3 * Copyright (c) 2008 The OpenSSL Project. All rights reserved.
4 * 4 *
@@ -49,15 +49,11 @@
49 * 49 *
50 */ 50 */
51 51
52#include <openssl/crypto.h>
53#include "modes_local.h"
54#include <string.h> 52#include <string.h>
55 53
56#ifndef MODES_DEBUG 54#include <openssl/crypto.h>
57# ifndef NDEBUG 55
58# define NDEBUG 56#include "modes_local.h"
59# endif
60#endif
61 57
62/* The input and output encrypted as though 128bit cfb mode is being 58/* The input and output encrypted as though 128bit cfb mode is being
63 * used. The extra state information to record how much of the 59 * used. The extra state information to record how much of the
@@ -75,7 +71,6 @@ CRYPTO_cfb128_encrypt(const unsigned char *in, unsigned char *out,
75 n = *num; 71 n = *num;
76 72
77 if (enc) { 73 if (enc) {
78#if !defined(OPENSSL_SMALL_FOOTPRINT)
79 if (16 % sizeof(size_t) == 0) 74 if (16 % sizeof(size_t) == 0)
80 do { /* always true actually */ 75 do { /* always true actually */
81 while (n && len) { 76 while (n && len) {
@@ -111,7 +106,6 @@ CRYPTO_cfb128_encrypt(const unsigned char *in, unsigned char *out,
111 return; 106 return;
112 } while (0); 107 } while (0);
113 /* the rest would be commonly eliminated by x86* compiler */ 108 /* the rest would be commonly eliminated by x86* compiler */
114#endif
115 while (l < len) { 109 while (l < len) {
116 if (n == 0) { 110 if (n == 0) {
117 (*block)(ivec, ivec, key); 111 (*block)(ivec, ivec, key);
@@ -122,7 +116,6 @@ CRYPTO_cfb128_encrypt(const unsigned char *in, unsigned char *out,
122 } 116 }
123 *num = n; 117 *num = n;
124 } else { 118 } else {
125#if !defined(OPENSSL_SMALL_FOOTPRINT)
126 if (16 % sizeof(size_t) == 0) 119 if (16 % sizeof(size_t) == 0)
127 do { /* always true actually */ 120 do { /* always true actually */
128 while (n && len) { 121 while (n && len) {
@@ -163,7 +156,6 @@ CRYPTO_cfb128_encrypt(const unsigned char *in, unsigned char *out,
163 return; 156 return;
164 } while (0); 157 } while (0);
165 /* the rest would be commonly eliminated by x86* compiler */ 158 /* the rest would be commonly eliminated by x86* compiler */
166#endif
167 while (l < len) { 159 while (l < len) {
168 unsigned char c; 160 unsigned char c;
169 if (n == 0) { 161 if (n == 0) {
diff --git a/src/lib/libcrypto/modes/ctr128.c b/src/lib/libcrypto/modes/ctr128.c
index 6d507dfc3a..30563ed6e3 100644
--- a/src/lib/libcrypto/modes/ctr128.c
+++ b/src/lib/libcrypto/modes/ctr128.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: ctr128.c,v 1.11 2023/07/08 14:56:54 beck Exp $ */ 1/* $OpenBSD: ctr128.c,v 1.17 2025/04/23 10:09:08 jsing Exp $ */
2/* ==================================================================== 2/* ====================================================================
3 * Copyright (c) 2008 The OpenSSL Project. All rights reserved. 3 * Copyright (c) 2008 The OpenSSL Project. All rights reserved.
4 * 4 *
@@ -49,16 +49,12 @@
49 * 49 *
50 */ 50 */
51 51
52#include <openssl/crypto.h>
53#include "modes_local.h"
54#include <string.h> 52#include <string.h>
55 53
56#ifndef MODES_DEBUG 54#include <openssl/crypto.h>
57# ifndef NDEBUG 55
58# define NDEBUG 56#include "crypto_internal.h"
59# endif 57#include "modes_local.h"
60#endif
61#include <assert.h>
62 58
63/* NOTE: the IV/counter CTR mode is big-endian. The code itself 59/* NOTE: the IV/counter CTR mode is big-endian. The code itself
64 * is endian-neutral. */ 60 * is endian-neutral. */
@@ -80,7 +76,6 @@ ctr128_inc(unsigned char *counter)
80 } while (n); 76 } while (n);
81} 77}
82 78
83#if !defined(OPENSSL_SMALL_FOOTPRINT)
84static void 79static void
85ctr128_inc_aligned(unsigned char *counter) 80ctr128_inc_aligned(unsigned char *counter)
86{ 81{
@@ -100,7 +95,6 @@ ctr128_inc_aligned(unsigned char *counter)
100 } while (n); 95 } while (n);
101#endif 96#endif
102} 97}
103#endif
104 98
105/* The input encrypted as though 128bit counter mode is being 99/* The input encrypted as though 128bit counter mode is being
106 * used. The extra state information to record how much of the 100 * used. The extra state information to record how much of the
@@ -121,14 +115,11 @@ CRYPTO_ctr128_encrypt(const unsigned char *in, unsigned char *out,
121 unsigned char ivec[16], unsigned char ecount_buf[16], 115 unsigned char ivec[16], unsigned char ecount_buf[16],
122 unsigned int *num, block128_f block) 116 unsigned int *num, block128_f block)
123{ 117{
124 unsigned int n; 118 unsigned int n = *num;
125 size_t l = 0; 119 size_t l = 0;
126 120
127 assert(*num < 16); 121 OPENSSL_assert(n < 16);
128
129 n = *num;
130 122
131#if !defined(OPENSSL_SMALL_FOOTPRINT)
132 if (16 % sizeof(size_t) == 0) 123 if (16 % sizeof(size_t) == 0)
133 do { /* always true actually */ 124 do { /* always true actually */
134 while (n && len) { 125 while (n && len) {
@@ -166,7 +157,6 @@ CRYPTO_ctr128_encrypt(const unsigned char *in, unsigned char *out,
166 return; 157 return;
167 } while (0); 158 } while (0);
168 /* the rest would be commonly eliminated by x86* compiler */ 159 /* the rest would be commonly eliminated by x86* compiler */
169#endif
170 while (l < len) { 160 while (l < len) {
171 if (n == 0) { 161 if (n == 0) {
172 (*block)(ivec, ecount_buf, key); 162 (*block)(ivec, ecount_buf, key);
@@ -204,11 +194,10 @@ CRYPTO_ctr128_encrypt_ctr32(const unsigned char *in, unsigned char *out,
204 unsigned char ivec[16], unsigned char ecount_buf[16], 194 unsigned char ivec[16], unsigned char ecount_buf[16],
205 unsigned int *num, ctr128_f func) 195 unsigned int *num, ctr128_f func)
206{ 196{
207 unsigned int n, ctr32; 197 unsigned int n = *num;
198 unsigned int ctr32;
208 199
209 assert(*num < 16); 200 OPENSSL_assert(n < 16);
210
211 n = *num;
212 201
213 while (n && len) { 202 while (n && len) {
214 *(out++) = *(in++) ^ ecount_buf[n]; 203 *(out++) = *(in++) ^ ecount_buf[n];
@@ -216,7 +205,8 @@ CRYPTO_ctr128_encrypt_ctr32(const unsigned char *in, unsigned char *out,
216 n = (n + 1) % 16; 205 n = (n + 1) % 16;
217 } 206 }
218 207
219 ctr32 = GETU32(ivec + 12); 208 ctr32 = crypto_load_be32toh(&ivec[12]);
209
220 while (len >= 16) { 210 while (len >= 16) {
221 size_t blocks = len/16; 211 size_t blocks = len/16;
222 /* 212 /*
@@ -240,7 +230,7 @@ CRYPTO_ctr128_encrypt_ctr32(const unsigned char *in, unsigned char *out,
240 } 230 }
241 (*func)(in, out, blocks, key, ivec); 231 (*func)(in, out, blocks, key, ivec);
242 /* (*ctr) does not update ivec, caller does: */ 232 /* (*ctr) does not update ivec, caller does: */
243 PUTU32(ivec + 12, ctr32); 233 crypto_store_htobe32(&ivec[12], ctr32);
244 /* ... overflow was detected, propagate carry. */ 234 /* ... overflow was detected, propagate carry. */
245 if (ctr32 == 0) 235 if (ctr32 == 0)
246 ctr96_inc(ivec); 236 ctr96_inc(ivec);
@@ -253,7 +243,7 @@ CRYPTO_ctr128_encrypt_ctr32(const unsigned char *in, unsigned char *out,
253 memset(ecount_buf, 0, 16); 243 memset(ecount_buf, 0, 16);
254 (*func)(ecount_buf, ecount_buf, 1, key, ivec); 244 (*func)(ecount_buf, ecount_buf, 1, key, ivec);
255 ++ctr32; 245 ++ctr32;
256 PUTU32(ivec + 12, ctr32); 246 crypto_store_htobe32(&ivec[12], ctr32);
257 if (ctr32 == 0) 247 if (ctr32 == 0)
258 ctr96_inc(ivec); 248 ctr96_inc(ivec);
259 while (len--) { 249 while (len--) {
diff --git a/src/lib/libcrypto/modes/gcm128.c b/src/lib/libcrypto/modes/gcm128.c
index 6c89bd44b7..21ba9eef57 100644
--- a/src/lib/libcrypto/modes/gcm128.c
+++ b/src/lib/libcrypto/modes/gcm128.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: gcm128.c,v 1.27 2024/09/06 09:57:32 tb Exp $ */ 1/* $OpenBSD: gcm128.c,v 1.35 2025/04/25 12:08:53 jsing Exp $ */
2/* ==================================================================== 2/* ====================================================================
3 * Copyright (c) 2010 The OpenSSL Project. All rights reserved. 3 * Copyright (c) 2010 The OpenSSL Project. All rights reserved.
4 * 4 *
@@ -48,8 +48,6 @@
48 * ==================================================================== 48 * ====================================================================
49 */ 49 */
50 50
51#define OPENSSL_FIPSAPI
52
53#include <string.h> 51#include <string.h>
54 52
55#include <openssl/crypto.h> 53#include <openssl/crypto.h>
@@ -57,18 +55,6 @@
57#include "crypto_internal.h" 55#include "crypto_internal.h"
58#include "modes_local.h" 56#include "modes_local.h"
59 57
60#ifndef MODES_DEBUG
61# ifndef NDEBUG
62# define NDEBUG
63# endif
64#endif
65
66#if defined(BSWAP4) && defined(__STRICT_ALIGNMENT)
67/* redefine, because alignment is ensured */
68#undef GETU32
69#define GETU32(p) BSWAP4(*(const u32 *)(p))
70#endif
71
72#define PACK(s) ((size_t)(s)<<(sizeof(size_t)*8-16)) 58#define PACK(s) ((size_t)(s)<<(sizeof(size_t)*8-16))
73#define REDUCE1BIT(V) \ 59#define REDUCE1BIT(V) \
74 do { \ 60 do { \
@@ -246,16 +232,13 @@ static void
246gcm_init_4bit(u128 Htable[16], u64 H[2]) 232gcm_init_4bit(u128 Htable[16], u64 H[2])
247{ 233{
248 u128 V; 234 u128 V;
249#if defined(OPENSSL_SMALL_FOOTPRINT)
250 int i; 235 int i;
251#endif
252 236
253 Htable[0].hi = 0; 237 Htable[0].hi = 0;
254 Htable[0].lo = 0; 238 Htable[0].lo = 0;
255 V.hi = H[0]; 239 V.hi = H[0];
256 V.lo = H[1]; 240 V.lo = H[1];
257 241
258#if defined(OPENSSL_SMALL_FOOTPRINT)
259 for (Htable[8] = V, i = 4; i > 0; i >>= 1) { 242 for (Htable[8] = V, i = 4; i > 0; i >>= 1) {
260 REDUCE1BIT(V); 243 REDUCE1BIT(V);
261 Htable[i] = V; 244 Htable[i] = V;
@@ -269,34 +252,7 @@ gcm_init_4bit(u128 Htable[16], u64 H[2])
269 Hi[j].lo = V.lo ^ Htable[j].lo; 252 Hi[j].lo = V.lo ^ Htable[j].lo;
270 } 253 }
271 } 254 }
272#else 255
273 Htable[8] = V;
274 REDUCE1BIT(V);
275 Htable[4] = V;
276 REDUCE1BIT(V);
277 Htable[2] = V;
278 REDUCE1BIT(V);
279 Htable[1] = V;
280 Htable[3].hi = V.hi ^ Htable[2].hi, Htable[3].lo = V.lo ^ Htable[2].lo;
281 V = Htable[4];
282 Htable[5].hi = V.hi ^ Htable[1].hi, Htable[5].lo = V.lo ^ Htable[1].lo;
283 Htable[6].hi = V.hi ^ Htable[2].hi, Htable[6].lo = V.lo ^ Htable[2].lo;
284 Htable[7].hi = V.hi ^ Htable[3].hi, Htable[7].lo = V.lo ^ Htable[3].lo;
285 V = Htable[8];
286 Htable[9].hi = V.hi ^ Htable[1].hi, Htable[9].lo = V.lo ^ Htable[1].lo;
287 Htable[10].hi = V.hi ^ Htable[2].hi,
288 Htable[10].lo = V.lo ^ Htable[2].lo;
289 Htable[11].hi = V.hi ^ Htable[3].hi,
290 Htable[11].lo = V.lo ^ Htable[3].lo;
291 Htable[12].hi = V.hi ^ Htable[4].hi,
292 Htable[12].lo = V.lo ^ Htable[4].lo;
293 Htable[13].hi = V.hi ^ Htable[5].hi,
294 Htable[13].lo = V.lo ^ Htable[5].lo;
295 Htable[14].hi = V.hi ^ Htable[6].hi,
296 Htable[14].lo = V.lo ^ Htable[6].lo;
297 Htable[15].hi = V.hi ^ Htable[7].hi,
298 Htable[15].lo = V.lo ^ Htable[7].lo;
299#endif
300#if defined(GHASH_ASM) && (defined(__arm__) || defined(__arm)) 256#if defined(GHASH_ASM) && (defined(__arm__) || defined(__arm))
301 /* 257 /*
302 * ARM assembler expects specific dword order in Htable. 258 * ARM assembler expects specific dword order in Htable.
@@ -376,7 +332,6 @@ gcm_gmult_4bit(u64 Xi[2], const u128 Htable[16])
376 Xi[1] = htobe64(Z.lo); 332 Xi[1] = htobe64(Z.lo);
377} 333}
378 334
379#if !defined(OPENSSL_SMALL_FOOTPRINT)
380/* 335/*
381 * Streamed gcm_mult_4bit, see CRYPTO_gcm128_[en|de]crypt for 336 * Streamed gcm_mult_4bit, see CRYPTO_gcm128_[en|de]crypt for
382 * details... Compiler-generated code doesn't seem to give any 337 * details... Compiler-generated code doesn't seem to give any
@@ -532,7 +487,6 @@ gcm_ghash_4bit(u64 Xi[2], const u128 Htable[16],
532 Xi[1] = htobe64(Z.lo); 487 Xi[1] = htobe64(Z.lo);
533 } while (inp += 16, len -= 16); 488 } while (inp += 16, len -= 16);
534} 489}
535#endif
536#else 490#else
537void gcm_gmult_4bit(u64 Xi[2], const u128 Htable[16]); 491void gcm_gmult_4bit(u64 Xi[2], const u128 Htable[16]);
538void gcm_ghash_4bit(u64 Xi[2], const u128 Htable[16], const u8 *inp, 492void gcm_ghash_4bit(u64 Xi[2], const u128 Htable[16], const u8 *inp,
@@ -540,48 +494,32 @@ void gcm_ghash_4bit(u64 Xi[2], const u128 Htable[16], const u8 *inp,
540#endif 494#endif
541 495
542#define GCM_MUL(ctx,Xi) gcm_gmult_4bit(ctx->Xi.u,ctx->Htable) 496#define GCM_MUL(ctx,Xi) gcm_gmult_4bit(ctx->Xi.u,ctx->Htable)
543#if defined(GHASH_ASM) || !defined(OPENSSL_SMALL_FOOTPRINT)
544#define GHASH(ctx,in,len) gcm_ghash_4bit((ctx)->Xi.u,(ctx)->Htable,in,len) 497#define GHASH(ctx,in,len) gcm_ghash_4bit((ctx)->Xi.u,(ctx)->Htable,in,len)
545/* GHASH_CHUNK is "stride parameter" missioned to mitigate cache 498/* GHASH_CHUNK is "stride parameter" missioned to mitigate cache
546 * trashing effect. In other words idea is to hash data while it's 499 * trashing effect. In other words idea is to hash data while it's
547 * still in L1 cache after encryption pass... */ 500 * still in L1 cache after encryption pass... */
548#define GHASH_CHUNK (3*1024) 501#define GHASH_CHUNK (3*1024)
549#endif
550 502
551#else /* TABLE_BITS */ 503#else /* TABLE_BITS */
552 504
553static void 505static void
554gcm_gmult_1bit(u64 Xi[2], const u64 H[2]) 506gcm_gmult_1bit(u64 Xi[2], const u64 H[2])
555{ 507{
556 u128 V, Z = { 0,0 }; 508 u128 V, Z = { 0, 0 };
557 long X; 509 u64 X;
558 int i, j; 510 int i, j;
559 const long *xi = (const long *)Xi;
560 511
561 V.hi = H[0]; /* H is in host byte order, no byte swapping */ 512 V.hi = H[0]; /* H is in host byte order, no byte swapping */
562 V.lo = H[1]; 513 V.lo = H[1];
563 514
564 for (j = 0; j < 16/sizeof(long); ++j) { 515 for (j = 0; j < 2; j++) {
565#if BYTE_ORDER == LITTLE_ENDIAN 516 X = be64toh(Xi[j]);
566#if SIZE_MAX == 0xffffffffffffffff
567#ifdef BSWAP8
568 X = (long)(BSWAP8(xi[j]));
569#else
570 const u8 *p = (const u8 *)(xi + j);
571 X = (long)((u64)GETU32(p) << 32|GETU32(p + 4));
572#endif
573#else
574 const u8 *p = (const u8 *)(xi + j);
575 X = (long)GETU32(p);
576#endif
577#else /* BIG_ENDIAN */
578 X = xi[j];
579#endif
580 517
581 for (i = 0; i < 8*sizeof(long); ++i, X <<= 1) { 518 for (i = 0; i < 64; i++) {
582 u64 M = (u64)(X >> (8*sizeof(long) - 1)); 519 u64 M = 0 - (X >> 63);
583 Z.hi ^= V.hi & M; 520 Z.hi ^= V.hi & M;
584 Z.lo ^= V.lo & M; 521 Z.lo ^= V.lo & M;
522 X <<= 1;
585 523
586 REDUCE1BIT(V); 524 REDUCE1BIT(V);
587 } 525 }
@@ -850,7 +788,6 @@ CRYPTO_gcm128_encrypt(GCM128_CONTEXT *ctx,
850 ctr = be32toh(ctx->Yi.d[3]); 788 ctr = be32toh(ctx->Yi.d[3]);
851 789
852 n = ctx->mres; 790 n = ctx->mres;
853#if !defined(OPENSSL_SMALL_FOOTPRINT)
854 if (16 % sizeof(size_t) == 0) 791 if (16 % sizeof(size_t) == 0)
855 do { /* always true actually */ 792 do { /* always true actually */
856 if (n) { 793 if (n) {
@@ -946,7 +883,6 @@ CRYPTO_gcm128_encrypt(GCM128_CONTEXT *ctx,
946 ctx->mres = n; 883 ctx->mres = n;
947 return 0; 884 return 0;
948 } while (0); 885 } while (0);
949#endif
950 for (i = 0; i < len; ++i) { 886 for (i = 0; i < len; ++i) {
951 if (n == 0) { 887 if (n == 0) {
952 (*block)(ctx->Yi.c, ctx->EKi.c, key); 888 (*block)(ctx->Yi.c, ctx->EKi.c, key);
@@ -996,7 +932,6 @@ CRYPTO_gcm128_decrypt(GCM128_CONTEXT *ctx,
996 ctr = be32toh(ctx->Yi.d[3]); 932 ctr = be32toh(ctx->Yi.d[3]);
997 933
998 n = ctx->mres; 934 n = ctx->mres;
999#if !defined(OPENSSL_SMALL_FOOTPRINT)
1000 if (16 % sizeof(size_t) == 0) 935 if (16 % sizeof(size_t) == 0)
1001 do { /* always true actually */ 936 do { /* always true actually */
1002 if (n) { 937 if (n) {
@@ -1068,8 +1003,8 @@ CRYPTO_gcm128_decrypt(GCM128_CONTEXT *ctx,
1068 ctx->Yi.d[3] = htobe32(ctr); 1003 ctx->Yi.d[3] = htobe32(ctr);
1069 1004
1070 for (i = 0; i < 16/sizeof(size_t); ++i) { 1005 for (i = 0; i < 16/sizeof(size_t); ++i) {
1071 size_t c = in[i]; 1006 size_t c = in_t[i];
1072 out[i] = c ^ ctx->EKi.t[i]; 1007 out_t[i] = c ^ ctx->EKi.t[i];
1073 ctx->Xi.t[i] ^= c; 1008 ctx->Xi.t[i] ^= c;
1074 } 1009 }
1075 GCM_MUL(ctx, Xi); 1010 GCM_MUL(ctx, Xi);
@@ -1094,7 +1029,6 @@ CRYPTO_gcm128_decrypt(GCM128_CONTEXT *ctx,
1094 ctx->mres = n; 1029 ctx->mres = n;
1095 return 0; 1030 return 0;
1096 } while (0); 1031 } while (0);
1097#endif
1098 for (i = 0; i < len; ++i) { 1032 for (i = 0; i < len; ++i) {
1099 u8 c; 1033 u8 c;
1100 if (n == 0) { 1034 if (n == 0) {
@@ -1159,7 +1093,7 @@ CRYPTO_gcm128_encrypt_ctr32(GCM128_CONTEXT *ctx,
1159 return 0; 1093 return 0;
1160 } 1094 }
1161 } 1095 }
1162#if defined(GHASH) && !defined(OPENSSL_SMALL_FOOTPRINT) 1096#if defined(GHASH) && defined(GHASH_CHUNK)
1163 while (len >= GHASH_CHUNK) { 1097 while (len >= GHASH_CHUNK) {
1164 (*stream)(in, out, GHASH_CHUNK/16, key, ctx->Yi.c); 1098 (*stream)(in, out, GHASH_CHUNK/16, key, ctx->Yi.c);
1165 ctr += GHASH_CHUNK/16; 1099 ctr += GHASH_CHUNK/16;
@@ -1251,7 +1185,7 @@ CRYPTO_gcm128_decrypt_ctr32(GCM128_CONTEXT *ctx,
1251 return 0; 1185 return 0;
1252 } 1186 }
1253 } 1187 }
1254#if defined(GHASH) && !defined(OPENSSL_SMALL_FOOTPRINT) 1188#if defined(GHASH) && defined(GHASH_CHUNK)
1255 while (len >= GHASH_CHUNK) { 1189 while (len >= GHASH_CHUNK) {
1256 GHASH(ctx, in, GHASH_CHUNK); 1190 GHASH(ctx, in, GHASH_CHUNK);
1257 (*stream)(in, out, GHASH_CHUNK/16, key, ctx->Yi.c); 1191 (*stream)(in, out, GHASH_CHUNK/16, key, ctx->Yi.c);
diff --git a/src/lib/libcrypto/modes/modes_local.h b/src/lib/libcrypto/modes/modes_local.h
index 511855f2e0..c04db034d0 100644
--- a/src/lib/libcrypto/modes/modes_local.h
+++ b/src/lib/libcrypto/modes/modes_local.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: modes_local.h,v 1.2 2023/07/08 14:55:36 beck Exp $ */ 1/* $OpenBSD: modes_local.h,v 1.4 2025/04/23 14:15:19 jsing Exp $ */
2/* ==================================================================== 2/* ====================================================================
3 * Copyright (c) 2010 The OpenSSL Project. All rights reserved. 3 * Copyright (c) 2010 The OpenSSL Project. All rights reserved.
4 * 4 *
@@ -27,44 +27,6 @@ typedef unsigned long long u64;
27typedef unsigned int u32; 27typedef unsigned int u32;
28typedef unsigned char u8; 28typedef unsigned char u8;
29 29
30#if !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM)
31#if defined(__GNUC__) && __GNUC__>=2
32# if defined(__x86_64) || defined(__x86_64__)
33# define BSWAP8(x) ({ u64 ret=(x); \
34 asm ("bswapq %0" \
35 : "+r"(ret)); ret; })
36# define BSWAP4(x) ({ u32 ret=(x); \
37 asm ("bswapl %0" \
38 : "+r"(ret)); ret; })
39# elif (defined(__i386) || defined(__i386__))
40# define BSWAP8(x) ({ u32 lo=(u64)(x)>>32,hi=(x); \
41 asm ("bswapl %0; bswapl %1" \
42 : "+r"(hi),"+r"(lo)); \
43 (u64)hi<<32|lo; })
44# define BSWAP4(x) ({ u32 ret=(x); \
45 asm ("bswapl %0" \
46 : "+r"(ret)); ret; })
47# elif (defined(__arm__) || defined(__arm)) && !defined(__STRICT_ALIGNMENT)
48# define BSWAP8(x) ({ u32 lo=(u64)(x)>>32,hi=(x); \
49 asm ("rev %0,%0; rev %1,%1" \
50 : "+r"(hi),"+r"(lo)); \
51 (u64)hi<<32|lo; })
52# define BSWAP4(x) ({ u32 ret; \
53 asm ("rev %0,%1" \
54 : "=r"(ret) : "r"((u32)(x))); \
55 ret; })
56# endif
57#endif
58#endif
59
60#if defined(BSWAP4) && !defined(__STRICT_ALIGNMENT)
61#define GETU32(p) BSWAP4(*(const u32 *)(p))
62#define PUTU32(p,v) *(u32 *)(p) = BSWAP4(v)
63#else
64#define GETU32(p) ((u32)(p)[0]<<24|(u32)(p)[1]<<16|(u32)(p)[2]<<8|(u32)(p)[3])
65#define PUTU32(p,v) ((p)[0]=(u8)((v)>>24),(p)[1]=(u8)((v)>>16),(p)[2]=(u8)((v)>>8),(p)[3]=(u8)(v))
66#endif
67
68/* GCM definitions */ 30/* GCM definitions */
69 31
70typedef struct { 32typedef struct {
diff --git a/src/lib/libcrypto/modes/ofb128.c b/src/lib/libcrypto/modes/ofb128.c
index 42afd29d58..8440e7f583 100644
--- a/src/lib/libcrypto/modes/ofb128.c
+++ b/src/lib/libcrypto/modes/ofb128.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: ofb128.c,v 1.7 2023/07/08 14:56:54 beck Exp $ */ 1/* $OpenBSD: ofb128.c,v 1.10 2025/04/23 10:09:08 jsing Exp $ */
2/* ==================================================================== 2/* ====================================================================
3 * Copyright (c) 2008 The OpenSSL Project. All rights reserved. 3 * Copyright (c) 2008 The OpenSSL Project. All rights reserved.
4 * 4 *
@@ -49,15 +49,11 @@
49 * 49 *
50 */ 50 */
51 51
52#include <openssl/crypto.h>
53#include "modes_local.h"
54#include <string.h> 52#include <string.h>
55 53
56#ifndef MODES_DEBUG 54#include <openssl/crypto.h>
57# ifndef NDEBUG 55
58# define NDEBUG 56#include "modes_local.h"
59# endif
60#endif
61 57
62/* The input and output encrypted as though 128bit ofb mode is being 58/* The input and output encrypted as though 128bit ofb mode is being
63 * used. The extra state information to record how much of the 59 * used. The extra state information to record how much of the
@@ -74,7 +70,6 @@ CRYPTO_ofb128_encrypt(const unsigned char *in, unsigned char *out,
74 70
75 n = *num; 71 n = *num;
76 72
77#if !defined(OPENSSL_SMALL_FOOTPRINT)
78 if (16 % sizeof(size_t) == 0) 73 if (16 % sizeof(size_t) == 0)
79 do { /* always true actually */ 74 do { /* always true actually */
80 while (n && len) { 75 while (n && len) {
@@ -109,7 +104,6 @@ CRYPTO_ofb128_encrypt(const unsigned char *in, unsigned char *out,
109 return; 104 return;
110 } while (0); 105 } while (0);
111 /* the rest would be commonly eliminated by x86* compiler */ 106 /* the rest would be commonly eliminated by x86* compiler */
112#endif
113 while (l < len) { 107 while (l < len) {
114 if (n == 0) { 108 if (n == 0) {
115 (*block)(ivec, ivec, key); 109 (*block)(ivec, ivec, key);
diff --git a/src/lib/libcrypto/modes/xts128.c b/src/lib/libcrypto/modes/xts128.c
index 7516acf850..789af9ef65 100644
--- a/src/lib/libcrypto/modes/xts128.c
+++ b/src/lib/libcrypto/modes/xts128.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: xts128.c,v 1.12 2023/07/08 14:56:54 beck Exp $ */ 1/* $OpenBSD: xts128.c,v 1.14 2025/04/21 16:01:18 jsing Exp $ */
2/* ==================================================================== 2/* ====================================================================
3 * Copyright (c) 2011 The OpenSSL Project. All rights reserved. 3 * Copyright (c) 2011 The OpenSSL Project. All rights reserved.
4 * 4 *
@@ -48,17 +48,12 @@
48 * ==================================================================== 48 * ====================================================================
49 */ 49 */
50 50
51#include <openssl/crypto.h>
52#include "modes_local.h"
53
54#include <endian.h> 51#include <endian.h>
55#include <string.h> 52#include <string.h>
56 53
57#ifndef MODES_DEBUG 54#include <openssl/crypto.h>
58# ifndef NDEBUG 55
59# define NDEBUG 56#include "modes_local.h"
60# endif
61#endif
62 57
63int 58int
64CRYPTO_xts128_encrypt(const XTS128_CONTEXT *ctx, const unsigned char iv[16], 59CRYPTO_xts128_encrypt(const XTS128_CONTEXT *ctx, const unsigned char iv[16],