From 3c27356c4047c5869d9365e12ad90ccbdbb882ab Mon Sep 17 00:00:00 2001 From: beck <> Date: Sat, 8 Jul 2023 14:55:36 +0000 Subject: Hit modes with the loving mallet of knfmt ok tb@ --- src/lib/libcrypto/modes/ctr128.c | 148 +++++++++++++++++++++------------------ 1 file changed, 81 insertions(+), 67 deletions(-) (limited to 'src/lib/libcrypto/modes/ctr128.c') diff --git a/src/lib/libcrypto/modes/ctr128.c b/src/lib/libcrypto/modes/ctr128.c index eadb80449c..7ba68a9c4c 100644 --- a/src/lib/libcrypto/modes/ctr128.c +++ b/src/lib/libcrypto/modes/ctr128.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ctr128.c,v 1.9 2022/12/26 07:18:52 jmc Exp $ */ +/* $OpenBSD: ctr128.c,v 1.10 2023/07/08 14:55:36 beck Exp $ */ /* ==================================================================== * Copyright (c) 2008 The OpenSSL Project. All rights reserved. * @@ -7,7 +7,7 @@ * are met: * * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. + * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in @@ -64,8 +64,10 @@ * is endian-neutral. */ /* increment counter (128-bit int) by 1 */ -static void ctr128_inc(unsigned char *counter) { - u32 n=16; +static void +ctr128_inc(unsigned char *counter) +{ + u32 n = 16; u8 c; do { @@ -73,7 +75,8 @@ static void ctr128_inc(unsigned char *counter) { c = counter[n]; ++c; counter[n] = c; - if (c) return; + if (c) + return; } while (n); } @@ -112,70 +115,76 @@ ctr128_inc_aligned(unsigned char *counter) * responsibility for checking that the counter doesn't overflow * into the rest of the IV when incremented. */ -void CRYPTO_ctr128_encrypt(const unsigned char *in, unsigned char *out, - size_t len, const void *key, - unsigned char ivec[16], unsigned char ecount_buf[16], - unsigned int *num, block128_f block) +void +CRYPTO_ctr128_encrypt(const unsigned char *in, unsigned char *out, + size_t len, const void *key, + unsigned char ivec[16], unsigned char ecount_buf[16], + unsigned int *num, block128_f block) { unsigned int n; - size_t l=0; + size_t l = 0; assert(*num < 16); n = *num; #if !defined(OPENSSL_SMALL_FOOTPRINT) - if (16%sizeof(size_t) == 0) do { /* always true actually */ - while (n && len) { - *(out++) = *(in++) ^ ecount_buf[n]; - --len; - n = (n+1) % 16; - } + if (16 % sizeof(size_t) == 0) + do { /* always true actually */ + while (n && len) { + *(out++) = *(in++) ^ ecount_buf[n]; + --len; + n = (n + 1) % 16; + } #ifdef __STRICT_ALIGNMENT - if (((size_t)in|(size_t)out|(size_t)ivec)%sizeof(size_t) != 0) - break; + if (((size_t)in|(size_t)out|(size_t)ivec) % + sizeof(size_t) != 0) + break; #endif - while (len>=16) { - (*block)(ivec, ecount_buf, key); - ctr128_inc_aligned(ivec); - for (; n<16; n+=sizeof(size_t)) - *(size_t *)(out+n) = - *(size_t *)(in+n) ^ *(size_t *)(ecount_buf+n); - len -= 16; - out += 16; - in += 16; - n = 0; - } - if (len) { - (*block)(ivec, ecount_buf, key); - ctr128_inc_aligned(ivec); - while (len--) { - out[n] = in[n] ^ ecount_buf[n]; - ++n; + while (len >= 16) { + (*block)(ivec, ecount_buf, key); + ctr128_inc_aligned(ivec); + for (; n < 16; n += sizeof(size_t)) + *(size_t *)(out + n) = + *(size_t *)(in + n) ^ *(size_t *)(ecount_buf + + n); + len -= 16; + out += 16; + in += 16; + n = 0; } - } - *num = n; - return; - } while(0); + if (len) { + (*block)(ivec, ecount_buf, key); + ctr128_inc_aligned(ivec); + while (len--) { + out[n] = in[n] ^ ecount_buf[n]; + ++n; + } + } + *num = n; + return; + } while (0); /* the rest would be commonly eliminated by x86* compiler */ #endif - while (l=16) { + ctr32 = GETU32(ivec + 12); + while (len >= 16) { size_t blocks = len/16; /* * 1<<28 is just a not-so-small yet not-so-large number... * Below condition is practically never met, but it has to * be checked for code correctness. */ - if (sizeof(size_t)>sizeof(unsigned int) && blocks>(1U<<28)) - blocks = (1U<<28); + if (sizeof(size_t) > sizeof(unsigned int) && + blocks > (1U << 28)) + blocks = (1U << 28); /* * As (*func) operates on 32-bit counter, caller * has to handle overflow. 'if' below detects the @@ -223,29 +235,31 @@ void CRYPTO_ctr128_encrypt_ctr32(const unsigned char *in, unsigned char *out, ctr32 += (u32)blocks; if (ctr32 < blocks) { blocks -= ctr32; - ctr32 = 0; + ctr32 = 0; } - (*func)(in,out,blocks,key,ivec); + (*func)(in, out, blocks, key, ivec); /* (*ctr) does not update ivec, caller does: */ - PUTU32(ivec+12,ctr32); + PUTU32(ivec + 12, ctr32); /* ... overflow was detected, propagate carry. */ - if (ctr32 == 0) ctr96_inc(ivec); + if (ctr32 == 0) + ctr96_inc(ivec); blocks *= 16; len -= blocks; out += blocks; - in += blocks; + in += blocks; } if (len) { - memset(ecount_buf,0,16); - (*func)(ecount_buf,ecount_buf,1,key,ivec); + memset(ecount_buf, 0, 16); + (*func)(ecount_buf, ecount_buf, 1, key, ivec); ++ctr32; - PUTU32(ivec+12,ctr32); - if (ctr32 == 0) ctr96_inc(ivec); + PUTU32(ivec + 12, ctr32); + if (ctr32 == 0) + ctr96_inc(ivec); while (len--) { out[n] = in[n] ^ ecount_buf[n]; ++n; } } - *num=n; + *num = n; } -- cgit v1.2.3-55-g6feb