This is an automated email from the ASF dual-hosted git repository. mgorecki pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/mynewt-core.git
commit f69738b3baaaf658362fea05c54189c1f0c4e411 Author: Michal Gorecki <[email protected]> AuthorDate: Wed Oct 8 11:43:15 2025 +0200 crypto/mbedtls: Fix gcm_mynewt coding style This adjusts codings style for the latest clang-format requirements --- crypto/mbedtls/include/mbedtls/gcm_mynewt.h | 10 ++- .../selftest/src/testcases/gcm_mynewt_test.c | 51 ++++++++------- crypto/mbedtls/src/gcm_mynewt.c | 73 +++++++++++----------- 3 files changed, 66 insertions(+), 68 deletions(-) diff --git a/crypto/mbedtls/include/mbedtls/gcm_mynewt.h b/crypto/mbedtls/include/mbedtls/gcm_mynewt.h index 40945bd48..b1b02ba19 100644 --- a/crypto/mbedtls/include/mbedtls/gcm_mynewt.h +++ b/crypto/mbedtls/include/mbedtls/gcm_mynewt.h @@ -26,11 +26,9 @@ /** * Same as mbedtls_gcm_setkey, but with preallocated memory for cipher algorithm context */ -int mbedtls_gcm_setkey_noalloc( mbedtls_gcm_context *ctx, - const mbedtls_cipher_info_t *cipher_info, - const unsigned char *key, - unsigned int keybits, - void *cipher_ctx); - +int mbedtls_gcm_setkey_noalloc(mbedtls_gcm_context *ctx, + const mbedtls_cipher_info_t *cipher_info, + const unsigned char *key, unsigned int keybits, + void *cipher_ctx); #endif /* _GCM_MYNEWT_H_ */ diff --git a/crypto/mbedtls/selftest/src/testcases/gcm_mynewt_test.c b/crypto/mbedtls/selftest/src/testcases/gcm_mynewt_test.c index f062f2b4e..46c69af7d 100644 --- a/crypto/mbedtls/selftest/src/testcases/gcm_mynewt_test.c +++ b/crypto/mbedtls/selftest/src/testcases/gcm_mynewt_test.c @@ -24,32 +24,33 @@ static const mbedtls_cipher_info_t *rsm_ucast_cipher; /* This contains both ADD and plaintext for encryption */ static const uint8_t initial_data[110] = { - 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, - 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, - 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, - 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, - 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, - 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, - 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, - 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, - 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, - 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A, - 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8, 0xA9, 0xAA + 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x11, + 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x21, 0x22, + 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x31, 0x32, 0x33, + 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x41, 0x42, 0x43, 0x44, + 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x51, 0x52, 0x53, 0x54, 0x55, + 0x56, 0x57, 0x58, 0x59, 0x5A, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, + 0x67, 0x68, 0x69, 0x6A, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, + 0x78, 0x79, 0x7A, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, + 0x89, 0x8A, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, + 0x9A, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8, 0xA9, 0xAA }; -static const uint8_t key[32] = { - 0xC0, 0xCA, 0xC0, 0x1A, 0xC0, 0xCA, 0xC0, 0x1A, - 0xC0, 0xCA, 0xC0, 0x1A, 0xC0, 0xCA, 0xC0, 0x1A, - 0xC0, 0xCA, 0xC0, 0x1A, 0xC0, 0xCA, 0xC0, 0x1A, - 0xC0, 0xCA, 0xC0, 0x1A, 0xC0, 0xCA, 0xC0, 0x1A -}; -static const uint8_t iv[12] = { 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xA, 0xB }; -static const uint8_t expected_tag[16] = { 0x05, 0x5D, 0x8E, 0xD4, 0xF9, 0x2A, 0x87, 0x87, - 0x6F, 0x23, 0xF2, 0xE6, 0xF0, 0x1D, 0x6D, 0x5C }; +static const uint8_t key[32] = { 0xC0, 0xCA, 0xC0, 0x1A, 0xC0, 0xCA, 0xC0, + 0x1A, 0xC0, 0xCA, 0xC0, 0x1A, 0xC0, 0xCA, + 0xC0, 0x1A, 0xC0, 0xCA, 0xC0, 0x1A, 0xC0, + 0xCA, 0xC0, 0x1A, 0xC0, 0xCA, 0xC0, 0x1A, + 0xC0, 0xCA, 0xC0, 0x1A }; +static const uint8_t iv[12] = { 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, + 0x6, 0x7, 0x8, 0x9, 0xA, 0xB }; +static const uint8_t expected_tag[16] = { 0x05, 0x5D, 0x8E, 0xD4, 0xF9, 0x2A, + 0x87, 0x87, 0x6F, 0x23, 0xF2, 0xE6, + 0xF0, 0x1D, 0x6D, 0x5C }; static uint8_t test_tag[16]; static uint8_t test_buf[110]; -static int mbedtls_gcm_mynewt_test_crypt(uint8_t enc) +static int +mbedtls_gcm_mynewt_test_crypt(uint8_t enc) { int add_len = 40; mbedtls_gcm_context ctx; @@ -63,9 +64,8 @@ static int mbedtls_gcm_mynewt_test_crypt(uint8_t enc) int rc; if (rsm_ucast_cipher == NULL) { - rsm_ucast_cipher = - mbedtls_cipher_info_from_values(MBEDTLS_CIPHER_ID_AES, 256, - MBEDTLS_MODE_ECB); + rsm_ucast_cipher = mbedtls_cipher_info_from_values(MBEDTLS_CIPHER_ID_AES, + 256, MBEDTLS_MODE_ECB); } memset(&ctx, 0, sizeof(ctx)); @@ -75,8 +75,7 @@ static int mbedtls_gcm_mynewt_test_crypt(uint8_t enc) goto out; } - rc = mbedtls_gcm_starts(&ctx, - enc == 1 ? MBEDTLS_GCM_ENCRYPT : MBEDTLS_GCM_DECRYPT, + rc = mbedtls_gcm_starts(&ctx, enc == 1 ? MBEDTLS_GCM_ENCRYPT : MBEDTLS_GCM_DECRYPT, iv, sizeof(iv)); if (rc) { goto out; diff --git a/crypto/mbedtls/src/gcm_mynewt.c b/crypto/mbedtls/src/gcm_mynewt.c index f84612463..fbb480078 100644 --- a/crypto/mbedtls/src/gcm_mynewt.c +++ b/crypto/mbedtls/src/gcm_mynewt.c @@ -69,12 +69,13 @@ #if !defined(MBEDTLS_GCM_ALT) /* Used to select the acceleration mechanism */ -#define MBEDTLS_GCM_ACC_SMALLTABLE 0 -#define MBEDTLS_GCM_ACC_LARGETABLE 1 -#define MBEDTLS_GCM_ACC_AESNI 2 -#define MBEDTLS_GCM_ACC_AESCE 3 +#define MBEDTLS_GCM_ACC_SMALLTABLE 0 +#define MBEDTLS_GCM_ACC_LARGETABLE 1 +#define MBEDTLS_GCM_ACC_AESNI 2 +#define MBEDTLS_GCM_ACC_AESCE 3 -static inline void gcm_set_acceleration(mbedtls_gcm_context *ctx) +static inline void +gcm_set_acceleration(mbedtls_gcm_context *ctx) { #if defined(MBEDTLS_GCM_LARGE_TABLE) ctx->acceleration = MBEDTLS_GCM_ACC_LARGETABLE; @@ -96,10 +97,11 @@ static inline void gcm_set_acceleration(mbedtls_gcm_context *ctx) #endif } -static inline void gcm_gen_table_rightshift(uint64_t dst[2], const uint64_t src[2]) +static inline void +gcm_gen_table_rightshift(uint64_t dst[2], const uint64_t src[2]) { - uint8_t *u8Dst = (uint8_t *) dst; - uint8_t *u8Src = (uint8_t *) src; + uint8_t *u8Dst = (uint8_t *)dst; + uint8_t *u8Src = (uint8_t *)src; MBEDTLS_PUT_UINT64_BE(MBEDTLS_GET_UINT64_BE(&src[1], 0) >> 1, &dst[1], 0); u8Dst[8] |= (u8Src[7] & 0x01) << 7; @@ -115,11 +117,12 @@ static inline void gcm_gen_table_rightshift(uint64_t dst[2], const uint64_t src[ * is the high-order bit of HH corresponds to P^0 and the low-order bit of HL * corresponds to P^127. */ -static int gcm_gen_table(mbedtls_gcm_context *ctx) +static int +gcm_gen_table(mbedtls_gcm_context *ctx) { int ret, i, j; uint64_t u64h[2] = { 0 }; - uint8_t *h = (uint8_t *) u64h; + uint8_t *h = (uint8_t *)u64h; #if defined(MBEDTLS_BLOCK_CIPHER_C) ret = mbedtls_block_cipher_encrypt(&ctx->block_cipher_ctx, h, h); @@ -134,18 +137,18 @@ static int gcm_gen_table(mbedtls_gcm_context *ctx) gcm_set_acceleration(ctx); /* MBEDTLS_GCM_HTABLE_SIZE/2 = 1000 corresponds to 1 in GF(2^128) */ - ctx->H[MBEDTLS_GCM_HTABLE_SIZE/2][0] = u64h[0]; - ctx->H[MBEDTLS_GCM_HTABLE_SIZE/2][1] = u64h[1]; + ctx->H[MBEDTLS_GCM_HTABLE_SIZE / 2][0] = u64h[0]; + ctx->H[MBEDTLS_GCM_HTABLE_SIZE / 2][1] = u64h[1]; switch (ctx->acceleration) { #if defined(MBEDTLS_AESNI_HAVE_CODE) - case MBEDTLS_GCM_ACC_AESNI: - return 0; + case MBEDTLS_GCM_ACC_AESNI: + return 0; #endif #if defined(MBEDTLS_AESCE_HAVE_CODE) - case MBEDTLS_GCM_ACC_AESCE: - return 0; + case MBEDTLS_GCM_ACC_AESCE: + return 0; #endif default: @@ -153,13 +156,13 @@ static int gcm_gen_table(mbedtls_gcm_context *ctx) ctx->H[0][0] = 0; ctx->H[0][1] = 0; - for (i = MBEDTLS_GCM_HTABLE_SIZE/4; i > 0; i >>= 1) { - gcm_gen_table_rightshift(ctx->H[i], ctx->H[i*2]); + for (i = MBEDTLS_GCM_HTABLE_SIZE / 4; i > 0; i >>= 1) { + gcm_gen_table_rightshift(ctx->H[i], ctx->H[i * 2]); } #if !defined(MBEDTLS_GCM_LARGE_TABLE) /* pack elements of H as 64-bits ints, big-endian */ - for (i = MBEDTLS_GCM_HTABLE_SIZE/2; i > 0; i >>= 1) { + for (i = MBEDTLS_GCM_HTABLE_SIZE / 2; i > 0; i >>= 1) { MBEDTLS_PUT_UINT64_BE(ctx->H[i][0], &ctx->H[i][0], 0); MBEDTLS_PUT_UINT64_BE(ctx->H[i][1], &ctx->H[i][1], 0); } @@ -167,10 +170,9 @@ static int gcm_gen_table(mbedtls_gcm_context *ctx) for (i = 2; i < MBEDTLS_GCM_HTABLE_SIZE; i <<= 1) { for (j = 1; j < i; j++) { - mbedtls_xor_no_simd((unsigned char *) ctx->H[i+j], - (unsigned char *) ctx->H[i], - (unsigned char *) ctx->H[j], - 16); + mbedtls_xor_no_simd((unsigned char *)ctx->H[i + j], + (unsigned char *)ctx->H[i], + (unsigned char *)ctx->H[j], 16); } } } @@ -178,11 +180,11 @@ static int gcm_gen_table(mbedtls_gcm_context *ctx) return 0; } -int mbedtls_gcm_setkey_noalloc( mbedtls_gcm_context *ctx, - const mbedtls_cipher_info_t *cipher_info, - const unsigned char *key, - unsigned int keybits, - void *cipher_ctx) +int +mbedtls_gcm_setkey_noalloc(mbedtls_gcm_context *ctx, + const mbedtls_cipher_info_t *cipher_info, + const unsigned char *key, unsigned int keybits, + void *cipher_ctx) { int ret; @@ -205,17 +207,16 @@ int mbedtls_gcm_setkey_noalloc( mbedtls_gcm_context *ctx, #endif #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */ - if( ( ret = mbedtls_cipher_setkey( &ctx->cipher_ctx, key, - keybits, - MBEDTLS_ENCRYPT ) ) != 0 ) - { - return( ret ); + if ((ret = mbedtls_cipher_setkey(&ctx->cipher_ctx, key, keybits, + MBEDTLS_ENCRYPT)) != 0) { + return ret; } - if( ( ret = gcm_gen_table( ctx ) ) != 0 ) - return( ret ); + if ((ret = gcm_gen_table(ctx)) != 0) { + return ret; + } - return( 0 ); + return 0; } #endif /* !MBEDTLS_GCM_ALT */
