| 1 |
// Copyright (c) 2008 The OpenSSL Project. All rights reserved. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
#include <openssl/type_check.h> |
| 5 |
|
| 6 |
#include <assert.h> |
| 7 |
#include <string.h> |
| 8 |
|
| 9 |
#include "internal.h" |
| 10 |
#include "../../internal.h" |
| 11 |
|
| 12 |
|
| 13 |
// NOTE: the IV/counter CTR mode is big-endian. The code itself |
| 14 |
// is endian-neutral. |
| 15 |
|
| 16 |
// increment counter (128-bit int) by 1 |
| 17 |
static void ctr128_inc(uint8_t *counter) { |
| 18 |
uint32_t n = 16, c = 1; |
| 19 |
|
| 20 |
do { |
| 21 |
--n; |
| 22 |
c += counter[n]; |
| 23 |
counter[n] = (uint8_t) c; |
| 24 |
c >>= 8; |
| 25 |
} while (n); |
| 26 |
} |
| 27 |
|
| 28 |
OPENSSL_STATIC_ASSERT(16 % sizeof(crypto_word_t) == 0, |
| 29 |
ctr_block_cannot_be_divided_into_crypto_word_t) |
| 30 |
|
| 31 |
// The input encrypted as though 128bit counter mode is being used. The extra |
| 32 |
// state information to record how much of the 128bit block we have used is |
| 33 |
// contained in *num, and the encrypted counter is kept in ecount_buf. Both |
| 34 |
// *num and ecount_buf must be initialised with zeros before the first call to |
| 35 |
// CRYPTO_ctr128_encrypt(). |
| 36 |
// |
| 37 |
// This algorithm assumes that the counter is in the x lower bits of the IV |
| 38 |
// (ivec), and that the application has full control over overflow and the rest |
| 39 |
// of the IV. This implementation takes NO responsibility for checking that |
| 40 |
// the counter doesn't overflow into the rest of the IV when incremented. |
| 41 |
void CRYPTO_ctr128_encrypt(const uint8_t *in, uint8_t *out, size_t len, |
| 42 |
const AES_KEY *key, uint8_t ivec[16], |
| 43 |
uint8_t ecount_buf[16], unsigned int *num, |
| 44 |
block128_f block) { |
| 45 |
unsigned int n; |
| 46 |
|
| 47 |
assert(key && ecount_buf && num); |
| 48 |
assert(len == 0 || (in && out)); |
| 49 |
assert(*num < 16); |
| 50 |
|
| 51 |
n = *num; |
| 52 |
|
| 53 |
while (n && len) { |
| 54 |
*(out++) = *(in++) ^ ecount_buf[n]; |
| 55 |
--len; |
| 56 |
n = (n + 1) % 16; |
| 57 |
} |
| 58 |
while (len >= 16) { |
| 59 |
(*block)(ivec, ecount_buf, key); |
| 60 |
ctr128_inc(ivec); |
| 61 |
CRYPTO_xor16(out, in, ecount_buf); |
| 62 |
len -= 16; |
| 63 |
out += 16; |
| 64 |
in += 16; |
| 65 |
n = 0; |
| 66 |
} |
| 67 |
if (len) { |
| 68 |
(*block)(ivec, ecount_buf, key); |
| 69 |
ctr128_inc(ivec); |
| 70 |
while (len--) { |
| 71 |
out[n] = in[n] ^ ecount_buf[n]; |
| 72 |
++n; |
| 73 |
} |
| 74 |
} |
| 75 |
*num = n; |
| 76 |
} |
| 77 |
|
| 78 |
// increment upper 96 bits of 128-bit counter by 1 |
| 79 |
static void ctr96_inc(uint8_t *counter) { |
| 80 |
uint32_t n = 12, c = 1; |
| 81 |
|
| 82 |
do { |
| 83 |
--n; |
| 84 |
c += counter[n]; |
| 85 |
counter[n] = (uint8_t) c; |
| 86 |
c >>= 8; |
| 87 |
} while (n); |
| 88 |
} |
| 89 |
|
| 90 |
void CRYPTO_ctr128_encrypt_ctr32(const uint8_t *in, uint8_t *out, size_t len, |
| 91 |
const AES_KEY *key, uint8_t ivec[16], |
| 92 |
uint8_t ecount_buf[16], unsigned int *num, |
| 93 |
ctr128_f func) { |
| 94 |
unsigned int n, ctr32; |
| 95 |
|
| 96 |
assert(key && ecount_buf && num); |
| 97 |
assert(len == 0 || (in && out)); |
| 98 |
assert(*num < 16); |
| 99 |
|
| 100 |
n = *num; |
| 101 |
|
| 102 |
while (n && len) { |
| 103 |
*(out++) = *(in++) ^ ecount_buf[n]; |
| 104 |
--len; |
| 105 |
n = (n + 1) % 16; |
| 106 |
} |
| 107 |
|
| 108 |
ctr32 = CRYPTO_load_u32_be(ivec + 12); |
| 109 |
while (len >= 16) { |
| 110 |
size_t blocks = len / 16; |
| 111 |
// 1<<28 is just a not-so-small yet not-so-large number... |
| 112 |
// Below condition is practically never met, but it has to |
| 113 |
// be checked for code correctness. |
| 114 |
if (sizeof(size_t) > sizeof(unsigned int) && blocks > (1U << 28)) { |
| 115 |
blocks = (1U << 28); |
| 116 |
} |
| 117 |
// As (*func) operates on 32-bit counter, caller |
| 118 |
// has to handle overflow. 'if' below detects the |
| 119 |
// overflow, which is then handled by limiting the |
| 120 |
// amount of blocks to the exact overflow point... |
| 121 |
ctr32 += (uint32_t)blocks; |
| 122 |
if (ctr32 < blocks) { |
| 123 |
blocks -= ctr32; |
| 124 |
ctr32 = 0; |
| 125 |
} |
| 126 |
(*func)(in, out, blocks, key, ivec); |
| 127 |
// (*func) does not update ivec, caller does: |
| 128 |
CRYPTO_store_u32_be(ivec + 12, ctr32); |
| 129 |
// ... overflow was detected, propogate carry. |
| 130 |
if (ctr32 == 0) { |
| 131 |
ctr96_inc(ivec); |
| 132 |
} |
| 133 |
blocks *= 16; |
| 134 |
len -= blocks; |
| 135 |
out += blocks; |
| 136 |
in += blocks; |
| 137 |
} |
| 138 |
if (len) { |
| 139 |
OPENSSL_memset(ecount_buf, 0, 16); |
| 140 |
(*func)(ecount_buf, ecount_buf, 1, key, ivec); |
| 141 |
++ctr32; |
| 142 |
CRYPTO_store_u32_be(ivec + 12, ctr32); |
| 143 |
if (ctr32 == 0) { |
| 144 |
ctr96_inc(ivec); |
| 145 |
} |
| 146 |
while (len--) { |
| 147 |
out[n] = in[n] ^ ecount_buf[n]; |
| 148 |
++n; |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
*num = n; |
| 153 |
} |