Formatting changes

Signed-off-by: Andrzej Kurek <andrzej.kurek@arm.com>
This commit is contained in:
Andrzej Kurek 2020-07-06 15:28:59 -04:00
parent 84bde419e1
commit 0919b142b6
No known key found for this signature in database
GPG key ID: 89A90840DC388527
4 changed files with 142 additions and 143 deletions

View file

@ -2470,7 +2470,6 @@ static int ssl_cid_build_inner_plaintext( unsigned char *content,
volatile size_t *content_size_dup = content_size;
volatile size_t remaining_dup = remaining;
/* Write real content type */
if( remaining == 0 )
return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
@ -4888,6 +4887,7 @@ static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
memset( mask + offset / 8, 0xFF, len / 8 );
}
#define BITMASK_CHECK_FAILED 0x75555555
/*
* Check that bitmask is full
*/
@ -4897,11 +4897,11 @@ static int ssl_bitmask_check( unsigned char *mask, size_t len )
for( i = 0; i < len / 8; i++ )
if( mask[i] != 0xFF )
return( 0x75555555 );
return( BITMASK_CHECK_FAILED );
for( i = 0; i < len % 8; i++ )
if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
return( 0x75555555 );
return( BITMASK_CHECK_FAILED );
return( 0 );
}
@ -7129,6 +7129,7 @@ write_msg:
#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
#define PEER_CRT_CHANGED 0x75555555
static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
unsigned char *crt_buf,
size_t crt_buf_len )
@ -7136,14 +7137,15 @@ static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
if( peer_crt == NULL )
return( 0x75555555 );
return( PEER_CRT_CHANGED );
if( peer_crt->raw.len != crt_buf_len )
return( 0x75555555 );
return( PEER_CRT_CHANGED );
return( mbedtls_platform_memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
}
#elif defined(MBEDTLS_SSL_RENEGOTIATION)
#define PEER_CRT_CHANGED 0x75555555
static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
unsigned char *crt_buf,
size_t crt_buf_len )
@ -7161,7 +7163,7 @@ static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
if( peer_cert_digest == NULL ||
digest_info == MBEDTLS_MD_INVALID_HANDLE )
{
return( 0x75555555 );
return( PEER_CRT_CHANGED );
}
digest_len = mbedtls_md_get_size( digest_info );
@ -7170,7 +7172,7 @@ static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
if( ret != 0 )
return( 0x75555555 );
return( PEER_CRT_CHANGED );
return( mbedtls_platform_memcmp( tmp_digest, peer_cert_digest, digest_len ) );
}

View file

@ -33,16 +33,16 @@
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of Intel Corporation nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
* - Neither the name of Intel Corporation nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
@ -99,7 +99,7 @@ const uECC_word_t curve_b[NUM_ECC_WORDS] = {
};
static int uECC_update_param_sha256(mbedtls_sha256_context *ctx,
const uECC_word_t val[NUM_ECC_WORDS])
const uECC_word_t val[NUM_ECC_WORDS])
{
uint8_t bytes[NUM_ECC_BYTES];
@ -119,10 +119,10 @@ static int uECC_compute_param_sha256(unsigned char output[32])
}
if (uECC_update_param_sha256(&ctx, curve_p) != 0 ||
uECC_update_param_sha256(&ctx, curve_n) != 0 ||
uECC_update_param_sha256(&ctx, curve_G) != 0 ||
uECC_update_param_sha256(&ctx, curve_G + NUM_ECC_WORDS) != 0 ||
uECC_update_param_sha256(&ctx, curve_b) != 0)
uECC_update_param_sha256(&ctx, curve_n) != 0 ||
uECC_update_param_sha256(&ctx, curve_G) != 0 ||
uECC_update_param_sha256(&ctx, curve_G + NUM_ECC_WORDS) != 0 ||
uECC_update_param_sha256(&ctx, curve_b) != 0)
{
goto exit;
}
@ -449,7 +449,7 @@ void ecc_wait_state_reset(ecc_wait_state_t *ws)
* know it's always 8. This saves a bit of code size and execution speed.
*/
static void uECC_vli_mult_rnd(uECC_word_t *result, const uECC_word_t *left,
const uECC_word_t *right, ecc_wait_state_t *s)
const uECC_word_t *right, ecc_wait_state_t *s)
{
uECC_word_t r0 = 0;
@ -546,7 +546,7 @@ static void uECC_vli_mult_rnd(uECC_word_t *result, const uECC_word_t *left,
}
void uECC_vli_modAdd(uECC_word_t *result, const uECC_word_t *left,
const uECC_word_t *right, const uECC_word_t *mod)
const uECC_word_t *right, const uECC_word_t *mod)
{
uECC_word_t carry = uECC_vli_add(result, left, right);
if (carry || uECC_vli_cmp_unsafe(mod, result) != 1) {
@ -557,7 +557,7 @@ void uECC_vli_modAdd(uECC_word_t *result, const uECC_word_t *left,
}
void uECC_vli_modSub(uECC_word_t *result, const uECC_word_t *left,
const uECC_word_t *right, const uECC_word_t *mod)
const uECC_word_t *right, const uECC_word_t *mod)
{
uECC_word_t l_borrow = uECC_vli_sub(result, left, right);
if (l_borrow) {
@ -570,7 +570,7 @@ void uECC_vli_modSub(uECC_word_t *result, const uECC_word_t *left,
/* Computes result = product % mod, where product is 2N words long. */
/* Currently only designed to work for curve_p or curve_n. */
void uECC_vli_mmod(uECC_word_t *result, uECC_word_t *product,
const uECC_word_t *mod)
const uECC_word_t *mod)
{
uECC_word_t mod_multiple[2 * NUM_ECC_WORDS];
uECC_word_t tmp[2 * NUM_ECC_WORDS];
@ -608,14 +608,14 @@ void uECC_vli_mmod(uECC_word_t *result, uECC_word_t *product,
index = !(index ^ borrow);
uECC_vli_rshift1(mod_multiple);
mod_multiple[num_words - 1] |= mod_multiple[num_words] <<
(uECC_WORD_BITS - 1);
(uECC_WORD_BITS - 1);
uECC_vli_rshift1(mod_multiple + num_words);
}
uECC_vli_set(result, v[index]);
}
void uECC_vli_modMult(uECC_word_t *result, const uECC_word_t *left,
const uECC_word_t *right, const uECC_word_t *mod)
const uECC_word_t *right, const uECC_word_t *mod)
{
uECC_word_t product[2 * NUM_ECC_WORDS];
uECC_vli_mult_rnd(product, left, right, NULL);
@ -640,7 +640,7 @@ void uECC_vli_modMult_fast(uECC_word_t *result, const uECC_word_t *left,
#define EVEN(vli) (!(vli[0] & 1))
static void vli_modInv_update(uECC_word_t *uv,
const uECC_word_t *mod)
const uECC_word_t *mod)
{
uECC_word_t carry = 0;
@ -655,7 +655,7 @@ static void vli_modInv_update(uECC_word_t *uv,
}
void uECC_vli_modInv(uECC_word_t *result, const uECC_word_t *input,
const uECC_word_t *mod)
const uECC_word_t *mod)
{
uECC_word_t a[NUM_ECC_WORDS], b[NUM_ECC_WORDS];
uECC_word_t u[NUM_ECC_WORDS], v[NUM_ECC_WORDS];
@ -674,27 +674,27 @@ void uECC_vli_modInv(uECC_word_t *result, const uECC_word_t *input,
while ((cmpResult = uECC_vli_cmp_unsafe(a, b)) != 0) {
if (EVEN(a)) {
uECC_vli_rshift1(a);
vli_modInv_update(u, mod);
} else if (EVEN(b)) {
vli_modInv_update(u, mod);
} else if (EVEN(b)) {
uECC_vli_rshift1(b);
vli_modInv_update(v, mod);
} else if (cmpResult > 0) {
uECC_vli_sub(a, a, b);
uECC_vli_rshift1(a);
if (uECC_vli_cmp_unsafe(u, v) < 0) {
uECC_vli_add(u, u, mod);
}
uECC_vli_sub(u, u, v);
vli_modInv_update(u, mod);
} else {
uECC_vli_sub(b, b, a);
uECC_vli_rshift1(b);
if (uECC_vli_cmp_unsafe(v, u) < 0) {
uECC_vli_add(v, v, mod);
}
uECC_vli_sub(v, v, u);
vli_modInv_update(v, mod);
}
uECC_vli_add(u, u, mod);
}
uECC_vli_sub(u, u, v);
vli_modInv_update(u, mod);
} else {
uECC_vli_sub(b, b, a);
uECC_vli_rshift1(b);
if (uECC_vli_cmp_unsafe(v, u) < 0) {
uECC_vli_add(v, v, mod);
}
uECC_vli_sub(v, v, u);
vli_modInv_update(v, mod);
}
}
uECC_vli_set(result, u);
}
@ -702,7 +702,7 @@ void uECC_vli_modInv(uECC_word_t *result, const uECC_word_t *input,
/* ------ Point operations ------ */
void double_jacobian_default(uECC_word_t * X1, uECC_word_t * Y1,
uECC_word_t * Z1)
uECC_word_t * Z1)
{
/* t1 = X, t2 = Y, t3 = Z */
uECC_word_t t4[NUM_ECC_WORDS];
@ -755,7 +755,7 @@ void double_jacobian_default(uECC_word_t * X1, uECC_word_t * Y1,
* @param curve IN -- elliptic curve
*/
static void x_side_default(uECC_word_t *result,
const uECC_word_t *x)
const uECC_word_t *x)
{
uECC_word_t _3[NUM_ECC_WORDS] = {3}; /* -a = 3 */
@ -861,7 +861,7 @@ void vli_mmod_fast_secp256r1(unsigned int *result, unsigned int*product)
while (carry < 0);
} else {
while (carry ||
uECC_vli_cmp_unsafe(curve_p, result) != 1) {
uECC_vli_cmp_unsafe(curve_p, result) != 1) {
carry -= uECC_vli_sub(result, result, curve_p);
}
}
@ -876,7 +876,7 @@ void apply_z(uECC_word_t * X1, uECC_word_t * Y1, const uECC_word_t * const Z)
{
uECC_word_t t1[NUM_ECC_WORDS];
uECC_vli_modMult_fast(t1, Z, Z); /* z^2 */
uECC_vli_modMult_fast(t1, Z, Z); /* z^2 */
uECC_vli_modMult_fast(X1, X1, t1); /* x1 * z^2 */
uECC_vli_modMult_fast(t1, t1, Z); /* z^3 */
uECC_vli_modMult_fast(Y1, Y1, t1); /* y1 * z^3 */
@ -929,7 +929,7 @@ static void XYcZ_add_rnd(uECC_word_t * X1, uECC_word_t * Y1,
}
void XYcZ_add(uECC_word_t * X1, uECC_word_t * Y1,
uECC_word_t * X2, uECC_word_t * Y2)
uECC_word_t * X2, uECC_word_t * Y2)
{
XYcZ_add_rnd(X1, Y1, X2, Y2, NULL);
}
@ -1030,7 +1030,7 @@ static uECC_word_t regularize_k(const uECC_word_t * const k, uECC_word_t *k0,
bitcount_t num_n_bits = NUM_ECC_BITS;
uECC_word_t carry = uECC_vli_add(k0, k, curve_n) ||
uECC_vli_testBit(k0, num_n_bits);
uECC_vli_testBit(k0, num_n_bits);
uECC_vli_add(k1, k0, curve_n);
@ -1078,7 +1078,7 @@ int EccPoint_mult_safer(uECC_word_t * result, const uECC_word_t * point,
carry = regularize_k(scalar, tmp, s);
/* If an RNG function was specified, get a random initial Z value to
* protect against side-channel attacks such as Template SPA */
* protect against side-channel attacks such as Template SPA */
if (g_rng_function) {
if (uECC_generate_random_int(k2[carry], curve_p, num_words) != UECC_SUCCESS) {
r = UECC_FAILURE;
@ -1135,7 +1135,7 @@ uECC_word_t EccPoint_compute_public_key(uECC_word_t *result,
/* Converts an integer in uECC native format to big-endian bytes. */
void uECC_vli_nativeToBytes(uint8_t *bytes, int num_bytes,
const unsigned int *native)
const unsigned int *native)
{
wordcount_t i;
for (i = 0; i < num_bytes; ++i) {
@ -1146,7 +1146,7 @@ void uECC_vli_nativeToBytes(uint8_t *bytes, int num_bytes,
/* Converts big-endian bytes to an integer in uECC native format. */
void uECC_vli_bytesToNative(unsigned int *native, const uint8_t *bytes,
int num_bytes)
int num_bytes)
{
wordcount_t i;
uECC_vli_clear(native);
@ -1158,7 +1158,7 @@ void uECC_vli_bytesToNative(unsigned int *native, const uint8_t *bytes,
}
int uECC_generate_random_int(uECC_word_t *random, const uECC_word_t *top,
wordcount_t num_words)
wordcount_t num_words)
{
uECC_word_t mask = (uECC_word_t)-1;
uECC_word_t tries;
@ -1170,10 +1170,10 @@ int uECC_generate_random_int(uECC_word_t *random, const uECC_word_t *top,
for (tries = 0; tries < uECC_RNG_MAX_TRIES; ++tries) {
if (g_rng_function((uint8_t *)random, num_words * uECC_WORD_SIZE) != num_words * uECC_WORD_SIZE) {
return UECC_FAILURE;
}
return UECC_FAILURE;
}
random[num_words - 1] &=
mask >> ((bitcount_t)(num_words * uECC_WORD_SIZE * 8 - num_bits));
mask >> ((bitcount_t)(num_words * uECC_WORD_SIZE * 8 - num_bits));
if (!uECC_vli_isZero(random) &&
uECC_vli_cmp(top, random) == 1) {
return UECC_SUCCESS;
@ -1207,7 +1207,7 @@ int uECC_valid_point(const uECC_word_t *point)
/* Make sure that y^2 == x^3 + ax + b */
diff = uECC_vli_equal(tmp1, tmp2);
if (diff == 0) {
mbedtls_platform_random_delay();
mbedtls_platform_random_delay();
if (diff == 0) {
return 0;
}
@ -1234,13 +1234,13 @@ int uECC_valid_public_key(const uint8_t *public_key)
return uECC_valid_point(_public);
}
int uECC_compute_public_key(const uint8_t * private_key, uint8_t * public_key)
int uECC_compute_public_key(const uint8_t *private_key, uint8_t *public_key)
{
int ret = UECC_FAULT_DETECTED;
uECC_word_t _private[NUM_ECC_WORDS];
uECC_word_t _public[NUM_ECC_WORDS * 2];
volatile const uint8_t * private_key_dup = private_key;
volatile const uint8_t * public_key_dup = public_key;
volatile const uint8_t *private_key_dup = private_key;
volatile const uint8_t *public_key_dup = public_key;
uECC_vli_bytesToNative(
_private,
@ -1266,8 +1266,8 @@ int uECC_compute_public_key(const uint8_t * private_key, uint8_t * public_key)
uECC_vli_nativeToBytes(
public_key +
NUM_ECC_BYTES, NUM_ECC_BYTES, _public + NUM_ECC_WORDS);
if(private_key_dup != private_key || public_key_dup != public_key){
return UECC_FAULT_DETECTED;
if (private_key_dup != private_key || public_key_dup != public_key){
return UECC_FAULT_DETECTED;
}
return ret;
}

View file

@ -12,10 +12,10 @@
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
@ -36,16 +36,16 @@
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of Intel Corporation nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
* - Neither the name of Intel Corporation nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
@ -91,14 +91,14 @@ int uECC_make_key_with_d(uint8_t *public_key, uint8_t *private_key,
/* Converting buffers to correct bit order: */
uECC_vli_nativeToBytes(private_key,
BITS_TO_BYTES(NUM_ECC_BITS),
_private);
BITS_TO_BYTES(NUM_ECC_BITS),
_private);
uECC_vli_nativeToBytes(public_key,
NUM_ECC_BYTES,
_public);
NUM_ECC_BYTES,
_public);
uECC_vli_nativeToBytes(public_key + NUM_ECC_BYTES,
NUM_ECC_BYTES,
_public + NUM_ECC_WORDS);
NUM_ECC_BYTES,
_public + NUM_ECC_WORDS);
exit:
/* erasing temporary buffer used to store secret: */
@ -122,7 +122,7 @@ int uECC_make_key(uint8_t *public_key, uint8_t *private_key)
uECC_RNG_Function rng_function = uECC_get_rng();
if (!rng_function ||
rng_function((uint8_t *)_random, 2 * NUM_ECC_WORDS*uECC_WORD_SIZE) != 2 * NUM_ECC_WORDS*uECC_WORD_SIZE) {
return UECC_FAILURE;
return UECC_FAILURE;
}
/* computing modular reduction of _random (see FIPS 186.4 B.4.1): */
@ -138,30 +138,29 @@ int uECC_make_key(uint8_t *public_key, uint8_t *private_key)
/* Converting buffers to correct bit order: */
uECC_vli_nativeToBytes(private_key,
BITS_TO_BYTES(NUM_ECC_BITS),
_private);
BITS_TO_BYTES(NUM_ECC_BITS),
_private);
uECC_vli_nativeToBytes(public_key,
NUM_ECC_BYTES,
_public);
NUM_ECC_BYTES,
_public);
uECC_vli_nativeToBytes(public_key + NUM_ECC_BYTES,
NUM_ECC_BYTES,
_public + NUM_ECC_WORDS);
NUM_ECC_BYTES,
_public + NUM_ECC_WORDS);
/* erasing temporary buffer that stored secret: */
mbedtls_platform_memset(_private, 0, NUM_ECC_BYTES);
if(private_key == private_key_dup &&
public_key == public_key_dup){
return UECC_SUCCESS;
if (private_key == private_key_dup && public_key == public_key_dup) {
return UECC_SUCCESS;
}
return UECC_FAULT_DETECTED;
}
}
}
return UECC_FAILURE;
}
int uECC_shared_secret(const uint8_t *public_key, const uint8_t *private_key,
uint8_t *secret)
uint8_t *secret)
{
uECC_word_t _public[NUM_ECC_WORDS * 2];
@ -173,27 +172,25 @@ int uECC_shared_secret(const uint8_t *public_key, const uint8_t *private_key,
volatile const uint8_t *private_key_dup = private_key;
volatile const uint8_t *secret_dup = secret;
/* Converting buffers to correct bit order: */
uECC_vli_bytesToNative(_private,
private_key,
BITS_TO_BYTES(NUM_ECC_BITS));
private_key,
BITS_TO_BYTES(NUM_ECC_BITS));
uECC_vli_bytesToNative(_public,
public_key,
num_bytes);
public_key,
num_bytes);
uECC_vli_bytesToNative(_public + num_words,
public_key + num_bytes,
num_bytes);
public_key + num_bytes,
num_bytes);
r = EccPoint_mult_safer(_public, _public, _private);
uECC_vli_nativeToBytes(secret, num_bytes, _public);
/* erasing temporary buffer used to store secret: */
mbedtls_platform_zeroize(_private, sizeof(_private));
if(public_key_dup != public_key || private_key_dup != private_key ||
secret_dup != secret){
return UECC_FAULT_DETECTED;
}
if (public_key_dup != public_key || private_key_dup != private_key || secret_dup != secret) {
return UECC_FAULT_DETECTED;
}
return r;
}

View file

@ -11,10 +11,10 @@
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
@ -34,16 +34,16 @@
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of Intel Corporation nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
* - Neither the name of Intel Corporation nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
@ -69,7 +69,7 @@
#include "mbedtls/platform_util.h"
static void bits2int(uECC_word_t *native, const uint8_t *bits,
unsigned bits_size)
unsigned bits_size)
{
unsigned num_n_bytes = BITS_TO_BYTES(NUM_ECC_BITS);
@ -82,7 +82,7 @@ static void bits2int(uECC_word_t *native, const uint8_t *bits,
}
int uECC_sign_with_k(const uint8_t *private_key, const uint8_t *message_hash,
unsigned hash_size, uECC_word_t *k, uint8_t *signature)
unsigned hash_size, uECC_word_t *k, uint8_t *signature)
{
uECC_word_t tmp[NUM_ECC_WORDS];
@ -94,12 +94,12 @@ int uECC_sign_with_k(const uint8_t *private_key, const uint8_t *message_hash,
/* Make sure 0 < k < curve_n */
if (uECC_vli_isZero(k) ||
uECC_vli_cmp(curve_n, k) != 1) {
uECC_vli_cmp(curve_n, k) != 1) {
return UECC_FAILURE;
}
r = EccPoint_mult_safer(p, curve_G, k);
if (r != UECC_SUCCESS) {
if (r != UECC_SUCCESS) {
return r;
}
@ -116,7 +116,7 @@ int uECC_sign_with_k(const uint8_t *private_key, const uint8_t *message_hash,
/* Prevent side channel analysis of uECC_vli_modInv() to determine
bits of k / the private key by premultiplying by a random number */
uECC_vli_modMult(k, k, tmp, curve_n); /* k' = rand * k */
uECC_vli_modInv(k, k, curve_n); /* k = 1 / k' */
uECC_vli_modInv(k, k, curve_n); /* k = 1 / k' */
uECC_vli_modMult(k, k, tmp, curve_n); /* k = 1 / k */
uECC_vli_nativeToBytes(signature, NUM_ECC_BYTES, p); /* store r */
@ -140,7 +140,7 @@ int uECC_sign_with_k(const uint8_t *private_key, const uint8_t *message_hash,
}
int uECC_sign(const uint8_t *private_key, const uint8_t *message_hash,
unsigned hash_size, uint8_t *signature)
unsigned hash_size, uint8_t *signature)
{
int r;
uECC_word_t _random[2*NUM_ECC_WORDS];
@ -148,14 +148,14 @@ int uECC_sign(const uint8_t *private_key, const uint8_t *message_hash,
uECC_word_t tries;
volatile const uint8_t *private_key_dup = private_key;
volatile const uint8_t *message_hash_dup = message_hash;
volatile unsigned hash_size_dup = hash_size;
volatile uint8_t *signature_dup = signature;
volatile unsigned hash_size_dup = hash_size;
volatile uint8_t *signature_dup = signature;
for (tries = 0; tries < uECC_RNG_MAX_TRIES; ++tries) {
/* Generating _random uniformly at random: */
uECC_RNG_Function rng_function = uECC_get_rng();
if (!rng_function ||
rng_function((uint8_t *)_random, 2*NUM_ECC_WORDS*uECC_WORD_SIZE) != 2*NUM_ECC_WORDS*uECC_WORD_SIZE) {
rng_function((uint8_t *)_random, 2*NUM_ECC_WORDS*uECC_WORD_SIZE) != 2*NUM_ECC_WORDS*uECC_WORD_SIZE) {
return UECC_FAILURE;
}
@ -168,10 +168,10 @@ int uECC_sign(const uint8_t *private_key, const uint8_t *message_hash,
return r;
}
if (r == UECC_SUCCESS) {
if(private_key_dup != private_key || message_hash_dup != message_hash ||
hash_size_dup != hash_size || signature_dup != signature){
return UECC_FAULT_DETECTED;
}
if (private_key_dup != private_key || message_hash_dup != message_hash ||
hash_size_dup != hash_size || signature_dup != signature) {
return UECC_FAULT_DETECTED;
}
return UECC_SUCCESS;
}
/* else keep trying */
@ -202,10 +202,10 @@ int uECC_verify(const uint8_t *public_key, const uint8_t *message_hash,
bitcount_t i;
bitcount_t flow_control;
volatile uECC_word_t diff;
volatile const uint8_t *public_key_dup = public_key;
volatile const uint8_t *message_hash_dup = message_hash;
volatile unsigned hash_size_dup = hash_size;
volatile const uint8_t *signature_dup = signature;
volatile const uint8_t *public_key_dup = public_key;
volatile const uint8_t *message_hash_dup = message_hash;
volatile unsigned hash_size_dup = hash_size;
volatile const uint8_t *signature_dup = signature;
uECC_word_t _public[NUM_ECC_WORDS * 2];
@ -220,7 +220,7 @@ int uECC_verify(const uint8_t *public_key, const uint8_t *message_hash,
uECC_vli_bytesToNative(_public, public_key, NUM_ECC_BYTES);
uECC_vli_bytesToNative(_public + num_words, public_key + NUM_ECC_BYTES,
NUM_ECC_BYTES);
NUM_ECC_BYTES);
uECC_vli_bytesToNative(r, signature, NUM_ECC_BYTES);
uECC_vli_bytesToNative(s, signature + NUM_ECC_BYTES, NUM_ECC_BYTES);
@ -231,7 +231,7 @@ int uECC_verify(const uint8_t *public_key, const uint8_t *message_hash,
/* r, s must be < n. */
if (uECC_vli_cmp_unsafe(curve_n, r) != 1 ||
uECC_vli_cmp_unsafe(curve_n, s) != 1) {
uECC_vli_cmp_unsafe(curve_n, s) != 1) {
return UECC_FAILURE;
}
@ -265,7 +265,7 @@ int uECC_verify(const uint8_t *public_key, const uint8_t *message_hash,
uECC_vli_numBits(u2));
point = points[(!!uECC_vli_testBit(u1, num_bits - 1)) |
((!!uECC_vli_testBit(u2, num_bits - 1)) << 1)];
((!!uECC_vli_testBit(u2, num_bits - 1)) << 1)];
uECC_vli_set(rx, point);
uECC_vli_set(ry, point + num_words);
uECC_vli_clear(z);
@ -301,17 +301,17 @@ int uECC_verify(const uint8_t *public_key, const uint8_t *message_hash,
/* Accept only if v == r. */
diff = uECC_vli_equal(rx, r);
if (diff == 0) {
flow_control++;
mbedtls_platform_random_delay();
/* Re-check the condition and test if the control flow is as expected.
* 1 (base value) + num_bits - 1 (from the loop) + 5 incrementations.
*/
flow_control++;
mbedtls_platform_random_delay();
/* Re-check the condition and test if the control flow is as expected.
* 1 (base value) + num_bits - 1 (from the loop) + 5 incrementations.
*/
if (diff == 0 && flow_control == (num_bits + 5)) {
if(public_key_dup != public_key || message_hash_dup != message_hash ||
hash_size_dup != hash_size || signature_dup != signature){
return UECC_FAULT_DETECTED;
}
if (public_key_dup != public_key || message_hash_dup != message_hash ||
hash_size_dup != hash_size || signature_dup != signature) {
return UECC_FAULT_DETECTED;
}
return UECC_SUCCESS;
}
else {