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