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