2 * Copyright 2008-2020 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 "crypto/modes.h"
14 #if defined(__GNUC__) && !defined(STRICT_ALIGNMENT)
15 typedef size_t size_t_aX __attribute((__aligned__(1)));
17 typedef size_t size_t_aX;
21 * NOTE: the IV/counter CTR mode is big-endian. The code itself is
25 /* increment counter (128-bit int) by 1 */
26 static void ctr128_inc(unsigned char *counter)
38 #if !defined(OPENSSL_SMALL_FOOTPRINT)
39 static void ctr128_inc_aligned(unsigned char *counter)
41 size_t *data, c, d, n;
49 if (is_endian.little || ((size_t)counter % sizeof(size_t)) != 0) {
54 data = (size_t *)counter;
56 n = 16 / sizeof(size_t);
60 /* did addition carry? */
61 c = ((d - c) & ~d) >> (sizeof(size_t) * 8 - 1);
67 * The input encrypted as though 128bit counter mode is being used. The
68 * extra state information to record how much of the 128bit block we have
69 * used is contained in *num, and the encrypted counter is kept in
70 * ecount_buf. Both *num and ecount_buf must be initialised with zeros
71 * before the first call to CRYPTO_ctr128_encrypt(). This algorithm assumes
72 * that the counter is in the x lower bits of the IV (ivec), and that the
73 * application has full control over overflow and the rest of the IV. This
74 * implementation takes NO responsibility for checking that the counter
75 * doesn't overflow into the rest of the IV when incremented.
77 void CRYPTO_ctr128_encrypt(const unsigned char *in, unsigned char *out,
78 size_t len, const void *key,
79 unsigned char ivec[16],
80 unsigned char ecount_buf[16], unsigned int *num,
88 #if !defined(OPENSSL_SMALL_FOOTPRINT)
89 if (16 % sizeof(size_t) == 0) { /* always true actually */
92 *(out++) = *(in++) ^ ecount_buf[n];
97 # if defined(STRICT_ALIGNMENT)
98 if (((size_t)in | (size_t)out | (size_t)ecount_buf)
99 % sizeof(size_t) != 0)
103 (*block) (ivec, ecount_buf, key);
104 ctr128_inc_aligned(ivec);
105 for (n = 0; n < 16; n += sizeof(size_t))
106 *(size_t_aX *)(out + n) =
107 *(size_t_aX *)(in + n)
108 ^ *(size_t_aX *)(ecount_buf + n);
115 (*block) (ivec, ecount_buf, key);
116 ctr128_inc_aligned(ivec);
118 out[n] = in[n] ^ ecount_buf[n];
126 /* the rest would be commonly eliminated by x86* compiler */
130 (*block) (ivec, ecount_buf, key);
133 out[l] = in[l] ^ ecount_buf[n];
141 /* increment upper 96 bits of 128-bit counter by 1 */
142 static void ctr96_inc(unsigned char *counter)
154 void CRYPTO_ctr128_encrypt_ctr32(const unsigned char *in, unsigned char *out,
155 size_t len, const void *key,
156 unsigned char ivec[16],
157 unsigned char ecount_buf[16],
158 unsigned int *num, ctr128_f func)
160 unsigned int n, ctr32;
165 *(out++) = *(in++) ^ ecount_buf[n];
170 ctr32 = GETU32(ivec + 12);
172 size_t blocks = len / 16;
174 * 1<<28 is just a not-so-small yet not-so-large number...
175 * Below condition is practically never met, but it has to
176 * be checked for code correctness.
178 if (sizeof(size_t) > sizeof(unsigned int) && blocks > (1U << 28))
181 * As (*func) operates on 32-bit counter, caller
182 * has to handle overflow. 'if' below detects the
183 * overflow, which is then handled by limiting the
184 * amount of blocks to the exact overflow point...
186 ctr32 += (u32)blocks;
187 if (ctr32 < blocks) {
191 (*func) (in, out, blocks, key, ivec);
192 /* (*ctr) does not update ivec, caller does: */
193 PUTU32(ivec + 12, ctr32);
194 /* ... overflow was detected, propagate carry. */
203 memset(ecount_buf, 0, 16);
204 (*func) (ecount_buf, ecount_buf, 1, key, ivec);
206 PUTU32(ivec + 12, ctr32);
210 out[n] = in[n] ^ ecount_buf[n];