C-style cast cleanup (partial)

Replace C-style casts with C++ casts.
This commit is contained in:
Nekotekina 2019-11-28 21:18:37 +03:00
parent 8bd52c9843
commit 5b9df53c13
20 changed files with 280 additions and 334 deletions

View file

@ -38,20 +38,20 @@
#ifndef GET_UINT32_LE
#define GET_UINT32_LE(n,b,i) \
{ \
(n) = ( (uint32_t) (b)[(i) ] ) \
| ( (uint32_t) (b)[(i) + 1] << 8 ) \
| ( (uint32_t) (b)[(i) + 2] << 16 ) \
| ( (uint32_t) (b)[(i) + 3] << 24 ); \
(n) = ( static_cast<uint32_t>((b)[(i) ]) ) \
| ( static_cast<uint32_t>((b)[(i) + 1]) << 8 ) \
| ( static_cast<uint32_t>((b)[(i) + 2]) << 16 ) \
| ( static_cast<uint32_t>((b)[(i) + 3]) << 24 );\
}
#endif
#ifndef PUT_UINT32_LE
#define PUT_UINT32_LE(n,b,i) \
{ \
(b)[(i) ] = (unsigned char) ( (n) ); \
(b)[(i) + 1] = (unsigned char) ( (n) >> 8 ); \
(b)[(i) + 2] = (unsigned char) ( (n) >> 16 ); \
(b)[(i) + 3] = (unsigned char) ( (n) >> 24 ); \
(b)[(i) ] = static_cast<unsigned char> ( (n) ); \
(b)[(i) + 1] = static_cast<unsigned char> ( (n) >> 8 ); \
(b)[(i) + 2] = static_cast<unsigned char> ( (n) >> 16 ); \
(b)[(i) + 3] = static_cast<unsigned char> ( (n) >> 24 ); \
}
#endif
@ -326,10 +326,10 @@ static const uint32_t RCON[10] =
* Forward S-box & tables
*/
static unsigned char FSb[256];
static uint32_t FT0[256];
static uint32_t FT1[256];
static uint32_t FT2[256];
static uint32_t FT3[256];
static uint32_t FT0[256];
static uint32_t FT1[256];
static uint32_t FT2[256];
static uint32_t FT3[256];
/*
* Reverse S-box & tables
@ -375,7 +375,7 @@ static void aes_gen_tables( void )
*/
for( i = 0, x = 1; i < 10; i++ )
{
RCON[i] = (uint32_t) x;
RCON[i] = static_cast<uint32_t>(x;
x = XTIME( x ) & 0xFF;
}
@ -408,10 +408,10 @@ static void aes_gen_tables( void )
y = XTIME( x ) & 0xFF;
z = ( y ^ x ) & 0xFF;
FT0[i] = ( (uint32_t) y ) ^
( (uint32_t) x << 8 ) ^
( (uint32_t) x << 16 ) ^
( (uint32_t) z << 24 );
FT0[i] = ( static_cast<uint32_t>(y ) ^
( static_cast<uint32_t>(x << 8 ) ^
( static_cast<uint32_t>(x << 16 ) ^
( static_cast<uint32_t>(z << 24 );
FT1[i] = ROTL8( FT0[i] );
FT2[i] = ROTL8( FT1[i] );
@ -419,10 +419,10 @@ static void aes_gen_tables( void )
x = RSb[i];
RT0[i] = ( (uint32_t) MUL( 0x0E, x ) ) ^
( (uint32_t) MUL( 0x09, x ) << 8 ) ^
( (uint32_t) MUL( 0x0D, x ) << 16 ) ^
( (uint32_t) MUL( 0x0B, x ) << 24 );
RT0[i] = ( static_cast<uint32_t>(MUL( 0x0E, x ) ) ^
( static_cast<uint32_t>(MUL( 0x09, x ) << 8 ) ^
( static_cast<uint32_t>(MUL( 0x0D, x ) << 16 ) ^
( static_cast<uint32_t>(MUL( 0x0B, x ) << 24 );
RT1[i] = ROTL8( RT0[i] );
RT2[i] = ROTL8( RT1[i] );
@ -460,7 +460,7 @@ int aes_setkey_enc( aes_context *ctx, const unsigned char *key, unsigned int key
ctx->rk = RK = ctx->buf;
if( aesni_supports( POLARSSL_AESNI_AES ) )
return( aesni_setkey_enc( (unsigned char *) ctx->rk, key, keysize ) );
return( aesni_setkey_enc( reinterpret_cast<unsigned char*>(ctx->rk), key, keysize ) );
for( i = 0; i < (keysize >> 5); i++ )
{
@ -474,10 +474,10 @@ int aes_setkey_enc( aes_context *ctx, const unsigned char *key, unsigned int key
for( i = 0; i < 10; i++, RK += 4 )
{
RK[4] = RK[0] ^ RCON[i] ^
( (uint32_t) FSb[ ( RK[3] >> 8 ) & 0xFF ] ) ^
( (uint32_t) FSb[ ( RK[3] >> 16 ) & 0xFF ] << 8 ) ^
( (uint32_t) FSb[ ( RK[3] >> 24 ) & 0xFF ] << 16 ) ^
( (uint32_t) FSb[ ( RK[3] ) & 0xFF ] << 24 );
( static_cast<uint32_t>(FSb[ ( RK[3] >> 8 ) & 0xFF ]) ) ^
( static_cast<uint32_t>(FSb[ ( RK[3] >> 16 ) & 0xFF ]) << 8 ) ^
( static_cast<uint32_t>(FSb[ ( RK[3] >> 24 ) & 0xFF ]) << 16 ) ^
( static_cast<uint32_t>(FSb[ ( RK[3] ) & 0xFF ]) << 24 );
RK[5] = RK[1] ^ RK[4];
RK[6] = RK[2] ^ RK[5];
@ -490,10 +490,10 @@ int aes_setkey_enc( aes_context *ctx, const unsigned char *key, unsigned int key
for( i = 0; i < 8; i++, RK += 6 )
{
RK[6] = RK[0] ^ RCON[i] ^
( (uint32_t) FSb[ ( RK[5] >> 8 ) & 0xFF ] ) ^
( (uint32_t) FSb[ ( RK[5] >> 16 ) & 0xFF ] << 8 ) ^
( (uint32_t) FSb[ ( RK[5] >> 24 ) & 0xFF ] << 16 ) ^
( (uint32_t) FSb[ ( RK[5] ) & 0xFF ] << 24 );
( static_cast<uint32_t>(FSb[ ( RK[5] >> 8 ) & 0xFF ]) ) ^
( static_cast<uint32_t>(FSb[ ( RK[5] >> 16 ) & 0xFF ]) << 8 ) ^
( static_cast<uint32_t>(FSb[ ( RK[5] >> 24 ) & 0xFF ]) << 16 ) ^
( static_cast<uint32_t>(FSb[ ( RK[5] ) & 0xFF ]) << 24 );
RK[7] = RK[1] ^ RK[6];
RK[8] = RK[2] ^ RK[7];
@ -508,20 +508,20 @@ int aes_setkey_enc( aes_context *ctx, const unsigned char *key, unsigned int key
for( i = 0; i < 7; i++, RK += 8 )
{
RK[8] = RK[0] ^ RCON[i] ^
( (uint32_t) FSb[ ( RK[7] >> 8 ) & 0xFF ] ) ^
( (uint32_t) FSb[ ( RK[7] >> 16 ) & 0xFF ] << 8 ) ^
( (uint32_t) FSb[ ( RK[7] >> 24 ) & 0xFF ] << 16 ) ^
( (uint32_t) FSb[ ( RK[7] ) & 0xFF ] << 24 );
( static_cast<uint32_t>(FSb[ ( RK[7] >> 8 ) & 0xFF ]) ) ^
( static_cast<uint32_t>(FSb[ ( RK[7] >> 16 ) & 0xFF ]) << 8 ) ^
( static_cast<uint32_t>(FSb[ ( RK[7] >> 24 ) & 0xFF ]) << 16 ) ^
( static_cast<uint32_t>(FSb[ ( RK[7] ) & 0xFF ]) << 24 );
RK[9] = RK[1] ^ RK[8];
RK[10] = RK[2] ^ RK[9];
RK[11] = RK[3] ^ RK[10];
RK[12] = RK[4] ^
( (uint32_t) FSb[ ( RK[11] ) & 0xFF ] ) ^
( (uint32_t) FSb[ ( RK[11] >> 8 ) & 0xFF ] << 8 ) ^
( (uint32_t) FSb[ ( RK[11] >> 16 ) & 0xFF ] << 16 ) ^
( (uint32_t) FSb[ ( RK[11] >> 24 ) & 0xFF ] << 24 );
( static_cast<uint32_t>(FSb[ ( RK[11] ) & 0xFF ]) ) ^
( static_cast<uint32_t>(FSb[ ( RK[11] >> 8 ) & 0xFF ]) << 8 ) ^
( static_cast<uint32_t>(FSb[ ( RK[11] >> 16 ) & 0xFF ]) << 16 ) ^
( static_cast<uint32_t>(FSb[ ( RK[11] >> 24 ) & 0xFF ]) << 24 );
RK[13] = RK[5] ^ RK[12];
RK[14] = RK[6] ^ RK[13];
@ -564,8 +564,8 @@ int aes_setkey_dec( aes_context *ctx, const unsigned char *key, unsigned int key
if( aesni_supports( POLARSSL_AESNI_AES ) )
{
aesni_inverse_key( (unsigned char *) ctx->rk,
(const unsigned char *) cty.rk, ctx->nr );
aesni_inverse_key( reinterpret_cast<unsigned char*>(ctx->rk),
reinterpret_cast<const unsigned char*>(cty.rk), ctx->nr );
goto done;
}
@ -676,28 +676,28 @@ int aes_crypt_ecb( aes_context *ctx,
AES_RROUND( Y0, Y1, Y2, Y3, X0, X1, X2, X3 );
X0 = *RK++ ^ \
( (uint32_t) RSb[ ( Y0 ) & 0xFF ] ) ^
( (uint32_t) RSb[ ( Y3 >> 8 ) & 0xFF ] << 8 ) ^
( (uint32_t) RSb[ ( Y2 >> 16 ) & 0xFF ] << 16 ) ^
( (uint32_t) RSb[ ( Y1 >> 24 ) & 0xFF ] << 24 );
( static_cast<uint32_t>(RSb[ ( Y0 ) & 0xFF ]) ) ^
( static_cast<uint32_t>(RSb[ ( Y3 >> 8 ) & 0xFF ]) << 8 ) ^
( static_cast<uint32_t>(RSb[ ( Y2 >> 16 ) & 0xFF ]) << 16 ) ^
( static_cast<uint32_t>(RSb[ ( Y1 >> 24 ) & 0xFF ]) << 24 );
X1 = *RK++ ^ \
( (uint32_t) RSb[ ( Y1 ) & 0xFF ] ) ^
( (uint32_t) RSb[ ( Y0 >> 8 ) & 0xFF ] << 8 ) ^
( (uint32_t) RSb[ ( Y3 >> 16 ) & 0xFF ] << 16 ) ^
( (uint32_t) RSb[ ( Y2 >> 24 ) & 0xFF ] << 24 );
( static_cast<uint32_t>(RSb[ ( Y1 ) & 0xFF ]) ) ^
( static_cast<uint32_t>(RSb[ ( Y0 >> 8 ) & 0xFF ]) << 8 ) ^
( static_cast<uint32_t>(RSb[ ( Y3 >> 16 ) & 0xFF ]) << 16 ) ^
( static_cast<uint32_t>(RSb[ ( Y2 >> 24 ) & 0xFF ]) << 24 );
X2 = *RK++ ^ \
( (uint32_t) RSb[ ( Y2 ) & 0xFF ] ) ^
( (uint32_t) RSb[ ( Y1 >> 8 ) & 0xFF ] << 8 ) ^
( (uint32_t) RSb[ ( Y0 >> 16 ) & 0xFF ] << 16 ) ^
( (uint32_t) RSb[ ( Y3 >> 24 ) & 0xFF ] << 24 );
( static_cast<uint32_t>(RSb[ ( Y2 ) & 0xFF ]) ) ^
( static_cast<uint32_t>(RSb[ ( Y1 >> 8 ) & 0xFF ]) << 8 ) ^
( static_cast<uint32_t>(RSb[ ( Y0 >> 16 ) & 0xFF ]) << 16 ) ^
( static_cast<uint32_t>(RSb[ ( Y3 >> 24 ) & 0xFF ]) << 24 );
X3 = *RK++ ^ \
( (uint32_t) RSb[ ( Y3 ) & 0xFF ] ) ^
( (uint32_t) RSb[ ( Y2 >> 8 ) & 0xFF ] << 8 ) ^
( (uint32_t) RSb[ ( Y1 >> 16 ) & 0xFF ] << 16 ) ^
( (uint32_t) RSb[ ( Y0 >> 24 ) & 0xFF ] << 24 );
( static_cast<uint32_t>(RSb[ ( Y3 ) & 0xFF ]) ) ^
( static_cast<uint32_t>(RSb[ ( Y2 >> 8 ) & 0xFF ]) << 8 ) ^
( static_cast<uint32_t>(RSb[ ( Y1 >> 16 ) & 0xFF ]) << 16 ) ^
( static_cast<uint32_t>(RSb[ ( Y0 >> 24 ) & 0xFF ]) << 24 );
}
else /* AES_ENCRYPT */
{
@ -710,28 +710,28 @@ int aes_crypt_ecb( aes_context *ctx,
AES_FROUND( Y0, Y1, Y2, Y3, X0, X1, X2, X3 );
X0 = *RK++ ^ \
( (uint32_t) FSb[ ( Y0 ) & 0xFF ] ) ^
( (uint32_t) FSb[ ( Y1 >> 8 ) & 0xFF ] << 8 ) ^
( (uint32_t) FSb[ ( Y2 >> 16 ) & 0xFF ] << 16 ) ^
( (uint32_t) FSb[ ( Y3 >> 24 ) & 0xFF ] << 24 );
( static_cast<uint32_t>(FSb[ ( Y0 ) & 0xFF ]) ) ^
( static_cast<uint32_t>(FSb[ ( Y1 >> 8 ) & 0xFF ]) << 8 ) ^
( static_cast<uint32_t>(FSb[ ( Y2 >> 16 ) & 0xFF ]) << 16 ) ^
( static_cast<uint32_t>(FSb[ ( Y3 >> 24 ) & 0xFF ]) << 24 );
X1 = *RK++ ^ \
( (uint32_t) FSb[ ( Y1 ) & 0xFF ] ) ^
( (uint32_t) FSb[ ( Y2 >> 8 ) & 0xFF ] << 8 ) ^
( (uint32_t) FSb[ ( Y3 >> 16 ) & 0xFF ] << 16 ) ^
( (uint32_t) FSb[ ( Y0 >> 24 ) & 0xFF ] << 24 );
( static_cast<uint32_t>(FSb[ ( Y1 ) & 0xFF ]) ) ^
( static_cast<uint32_t>(FSb[ ( Y2 >> 8 ) & 0xFF ]) << 8 ) ^
( static_cast<uint32_t>(FSb[ ( Y3 >> 16 ) & 0xFF ]) << 16 ) ^
( static_cast<uint32_t>(FSb[ ( Y0 >> 24 ) & 0xFF ]) << 24 );
X2 = *RK++ ^ \
( (uint32_t) FSb[ ( Y2 ) & 0xFF ] ) ^
( (uint32_t) FSb[ ( Y3 >> 8 ) & 0xFF ] << 8 ) ^
( (uint32_t) FSb[ ( Y0 >> 16 ) & 0xFF ] << 16 ) ^
( (uint32_t) FSb[ ( Y1 >> 24 ) & 0xFF ] << 24 );
( static_cast<uint32_t>(FSb[ ( Y2 ) & 0xFF ]) ) ^
( static_cast<uint32_t>(FSb[ ( Y3 >> 8 ) & 0xFF ]) << 8 ) ^
( static_cast<uint32_t>(FSb[ ( Y0 >> 16 ) & 0xFF ]) << 16 ) ^
( static_cast<uint32_t>(FSb[ ( Y1 >> 24 ) & 0xFF ]) << 24 );
X3 = *RK++ ^ \
( (uint32_t) FSb[ ( Y3 ) & 0xFF ] ) ^
( (uint32_t) FSb[ ( Y0 >> 8 ) & 0xFF ] << 8 ) ^
( (uint32_t) FSb[ ( Y1 >> 16 ) & 0xFF ] << 16 ) ^
( (uint32_t) FSb[ ( Y2 >> 24 ) & 0xFF ] << 24 );
( static_cast<uint32_t>(FSb[ ( Y3 ) & 0xFF ]) ) ^
( static_cast<uint32_t>(FSb[ ( Y0 >> 8 ) & 0xFF ]) << 8 ) ^
( static_cast<uint32_t>(FSb[ ( Y1 >> 16 ) & 0xFF ]) << 16 ) ^
( static_cast<uint32_t>(FSb[ ( Y2 >> 24 ) & 0xFF ]) << 24 );
}
PUT_UINT32_LE( X0, output, 0 );
@ -766,7 +766,7 @@ int aes_crypt_cbc( aes_context *ctx,
aes_crypt_ecb( ctx, mode, input, output );
for( i = 0; i < 16; i++ )
output[i] = (unsigned char)( output[i] ^ iv[i] );
output[i] ^= iv[i];
memcpy( iv, temp, 16 );
@ -780,7 +780,7 @@ int aes_crypt_cbc( aes_context *ctx,
while( length > 0 )
{
for( i = 0; i < 16; i++ )
output[i] = (unsigned char)( input[i] ^ iv[i] );
output[i] = input[i] ^ iv[i];
aes_crypt_ecb( ctx, mode, output, output );
memcpy( iv, output, 16 );
@ -816,8 +816,8 @@ int aes_crypt_cfb128( aes_context *ctx,
aes_crypt_ecb( ctx, AES_ENCRYPT, iv, iv );
c = *input++;
*output++ = (unsigned char)( c ^ iv[n] );
iv[n] = (unsigned char) c;
*output++ = static_cast<unsigned char>( c ^ iv[n] );
iv[n] = static_cast<unsigned char>(c);
n = (n + 1) & 0x0F;
}
@ -829,7 +829,7 @@ int aes_crypt_cfb128( aes_context *ctx,
if( n == 0 )
aes_crypt_ecb( ctx, AES_ENCRYPT, iv, iv );
iv[n] = *output++ = (unsigned char)( iv[n] ^ *input++ );
iv[n] = *output++ = static_cast<unsigned char>( iv[n] ^ *input++ );
n = (n + 1) & 0x0F;
}
@ -864,7 +864,7 @@ int aes_crypt_ctr( aes_context *ctx,
break;
}
c = *input++;
*output++ = (unsigned char)( c ^ stream_block[n] );
*output++ = static_cast<unsigned char>( c ^ stream_block[n] );
n = (n + 1) & 0x0F;
}
@ -890,7 +890,7 @@ void leftshift_onebit(unsigned char *input, unsigned char *output)
int i;
unsigned char overflow = 0;
for (i = 15; i >= 0; i--)
for (i = 15; i >= 0; i--)
{
output[i] = input[i] << 1;
output[i] |= overflow;
@ -901,7 +901,7 @@ void leftshift_onebit(unsigned char *input, unsigned char *output)
void xor_128(unsigned char *a, unsigned char *b, unsigned char *out)
{
int i;
for (i = 0; i < 16; i++)
for (i = 0; i < 16; i++)
out[i] = a[i] ^ b[i];
}
@ -910,7 +910,7 @@ void generate_subkey(aes_context *ctx, unsigned char *K1, unsigned char *K2)
unsigned char L[16];
unsigned char Z[16];
unsigned char tmp[16];
int i;
for (i = 0; i < 16; i++) Z[i] = 0;
@ -924,7 +924,7 @@ void generate_subkey(aes_context *ctx, unsigned char *K1, unsigned char *K2)
xor_128(tmp,const_Rb,K1);
}
if ((K1[0] & 0x80) == 0)
if ((K1[0] & 0x80) == 0)
{
leftshift_onebit(K1,K2);
} else {
@ -936,9 +936,9 @@ void generate_subkey(aes_context *ctx, unsigned char *K1, unsigned char *K2)
void padding (unsigned char *lastb, unsigned char *pad, int length)
{
int i;
for (i = 0; i < 16; i++)
for (i = 0; i < 16; i++)
{
if (i < length)
if (i < length)
pad[i] = lastb[i];
else if (i == length)
pad[i] = 0x80;
@ -955,7 +955,7 @@ void aes_cmac(aes_context *ctx, int length, unsigned char *input, unsigned char
generate_subkey(ctx, K1, K2);
n = (length + 15) / 16;
if (n == 0)
if (n == 0)
{
n = 1;
flag = 0;
@ -966,7 +966,7 @@ void aes_cmac(aes_context *ctx, int length, unsigned char *input, unsigned char
flag = 0;
}
if (flag)
if (flag)
{
xor_128(&input[16 * (n - 1)], K1, M_last);
} else {
@ -975,10 +975,10 @@ void aes_cmac(aes_context *ctx, int length, unsigned char *input, unsigned char
}
for (i = 0; i < 16; i++) X[i] = 0;
for (i = 0; i < n - 1; i++)
for (i = 0; i < n - 1; i++)
{
xor_128(X, &input[16*i], Y);
aes_crypt_ecb(ctx, AES_ENCRYPT, Y, X);
aes_crypt_ecb(ctx, AES_ENCRYPT, Y, X);
}
xor_128(X,M_last,Y);