2 * Copyright 2008-2021 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 #include <openssl/crypto.h>
12 #include "internal/endian.h"
13 #include "crypto/modes.h"
15 #if defined(__GNUC__) && !defined(STRICT_ALIGNMENT)
16 typedef size_t size_t_aX __attribute((__aligned__(1)));
18 typedef size_t size_t_aX;
22 * NOTE: the IV/counter CTR mode is big-endian. The code itself is
26 /* increment counter (128-bit int) by 1 */
27 static void ctr128_inc(unsigned char *counter)
39 #if !defined(OPENSSL_SMALL_FOOTPRINT)
40 static void ctr128_inc_aligned(unsigned char *counter)
42 size_t *data, c, d, n;
45 if (IS_LITTLE_ENDIAN || ((size_t)counter % sizeof(size_t)) != 0) {
50 data = (size_t *)counter;
52 n = 16 / sizeof(size_t);
56 /* did addition carry? */
57 c = ((d - c) & ~d) >> (sizeof(size_t) * 8 - 1);
63 * The input encrypted as though 128bit counter mode is being used. The
64 * extra state information to record how much of the 128bit block we have
65 * used is contained in *num, and the encrypted counter is kept in
66 * ecount_buf. Both *num and ecount_buf must be initialised with zeros
67 * before the first call to CRYPTO_ctr128_encrypt(). This algorithm assumes
68 * that the counter is in the x lower bits of the IV (ivec), and that the
69 * application has full control over overflow and the rest of the IV. This
70 * implementation takes NO responsibility for checking that the counter
71 * doesn't overflow into the rest of the IV when incremented.
73 void CRYPTO_ctr128_encrypt(const unsigned char *in, unsigned char *out,
74 size_t len, const void *key,
75 unsigned char ivec[16],
76 unsigned char ecount_buf[16], unsigned int *num,
84 #if !defined(OPENSSL_SMALL_FOOTPRINT)
85 if (16 % sizeof(size_t) == 0) { /* always true actually */
88 *(out++) = *(in++) ^ ecount_buf[n];
93 # if defined(STRICT_ALIGNMENT)
94 if (((size_t)in | (size_t)out | (size_t)ecount_buf)
95 % sizeof(size_t) != 0)
99 (*block) (ivec, ecount_buf, key);
100 ctr128_inc_aligned(ivec);
101 for (n = 0; n < 16; n += sizeof(size_t))
102 *(size_t_aX *)(out + n) =
103 *(size_t_aX *)(in + n)
104 ^ *(size_t_aX *)(ecount_buf + n);
111 (*block) (ivec, ecount_buf, key);
112 ctr128_inc_aligned(ivec);
114 out[n] = in[n] ^ ecount_buf[n];
122 /* the rest would be commonly eliminated by x86* compiler */
126 (*block) (ivec, ecount_buf, key);
129 out[l] = in[l] ^ ecount_buf[n];
137 /* increment upper 96 bits of 128-bit counter by 1 */
138 static void ctr96_inc(unsigned char *counter)
150 void CRYPTO_ctr128_encrypt_ctr32(const unsigned char *in, unsigned char *out,
151 size_t len, const void *key,
152 unsigned char ivec[16],
153 unsigned char ecount_buf[16],
154 unsigned int *num, ctr128_f func)
156 unsigned int n, ctr32;
161 *(out++) = *(in++) ^ ecount_buf[n];
166 ctr32 = GETU32(ivec + 12);
168 size_t blocks = len / 16;
170 * 1<<28 is just a not-so-small yet not-so-large number...
171 * Below condition is practically never met, but it has to
172 * be checked for code correctness.
174 if (sizeof(size_t) > sizeof(unsigned int) && blocks > (1U << 28))
177 * As (*func) operates on 32-bit counter, caller
178 * has to handle overflow. 'if' below detects the
179 * overflow, which is then handled by limiting the
180 * amount of blocks to the exact overflow point...
182 ctr32 += (u32)blocks;
183 if (ctr32 < blocks) {
187 (*func) (in, out, blocks, key, ivec);
188 /* (*ctr) does not update ivec, caller does: */
189 PUTU32(ivec + 12, ctr32);
190 /* ... overflow was detected, propagate carry. */
199 memset(ecount_buf, 0, 16);
200 (*func) (ecount_buf, ecount_buf, 1, key, ivec);
202 PUTU32(ivec + 12, ctr32);
206 out[n] = in[n] ^ ecount_buf[n];