blob: a6b84cdeb5638fd83ae7a99d4f2830037eb5e7ee [file] [log] [blame]
Dave Rodgmanb49cf102024-01-13 16:40:58 +00001/**
2 * \file ctr.h
3 *
4 * \brief This file contains common functionality for counter algorithms.
5 *
6 * Copyright The Mbed TLS Contributors
7 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
8 */
9
10#include "common.h"
11
12/**
13 * \brief Increment a big-endian 16-byte value.
14 * This is quite performance-sensitive for AES-CTR and CTR-DRBG.
15 *
16 * \param n A 16-byte value to be incremented.
17 */
18static inline void mbedtls_ctr_increment_counter(uint8_t n[16])
19{
20 // The 32-bit version seems to perform about the same as a 64-bit version
21 // on 64-bit architectures, so no need to define a 64-bit version.
22 for (int i = 3;; i--) {
23 uint32_t x = MBEDTLS_GET_UINT32_BE(n, i << 2);
24 x += 1;
25 MBEDTLS_PUT_UINT32_BE(x, n, i << 2);
26 if (x != 0 || i == 0) {
27 break;
28 }
29 }
30}