Skip to content

Commit 50d7539

Browse files
Ron EldorRon Eldor
authored andcommitted
Code improvements
1. Make common function for setting key, which receives the direction as parameter. 2. Remove rediundant extra lines.
1 parent a58457d commit 50d7539

File tree

1 file changed

+10
-29
lines changed
  • features/cryptocell/FEATURE_CRYPTOCELL310

1 file changed

+10
-29
lines changed

features/cryptocell/FEATURE_CRYPTOCELL310/aes_alt.c

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,10 @@ void mbedtls_aes_xts_init( mbedtls_aes_xts_context *ctx ){}
111111
void mbedtls_aes_xts_free( mbedtls_aes_xts_context *ctx ){}
112112
#endif /* MBEDTLS_CIPHER_MODE_XTS */
113113

114-
int mbedtls_aes_setkey_enc( mbedtls_aes_context *ctx, const unsigned char *key,
115-
unsigned int keybits )
114+
static int CC_aes_setkey( mbedtls_aes_context *ctx, const unsigned char *key,
115+
unsigned int keybits, SaSiAesEncryptMode_t cipher_flag )
116116
{
117+
117118
int ret = 0;
118119
if ( ctx == NULL )
119120
return( MBEDTLS_ERR_AES_BAD_INPUT_DATA );
@@ -122,7 +123,7 @@ int mbedtls_aes_setkey_enc( mbedtls_aes_context *ctx, const unsigned char *key,
122123
{
123124
case 128:
124125
{
125-
ctx->CC_cipherFlag = SASI_AES_ENCRYPT;
126+
ctx->CC_cipherFlag = cipher_flag;
126127
ctx->CC_keySizeInBytes = ( keybits / 8 );
127128
memcpy( ctx->CC_Key, key, ctx->CC_keySizeInBytes );
128129
}
@@ -137,32 +138,16 @@ int mbedtls_aes_setkey_enc( mbedtls_aes_context *ctx, const unsigned char *key,
137138
return ( 0 );
138139

139140
}
141+
int mbedtls_aes_setkey_enc( mbedtls_aes_context *ctx, const unsigned char *key,
142+
unsigned int keybits )
143+
{
144+
return( CC_aes_setkey( ctx, key, keybits, SASI_AES_ENCRYPT ) );
145+
}
140146

141147
int mbedtls_aes_setkey_dec( mbedtls_aes_context *ctx, const unsigned char *key,
142148
unsigned int keybits )
143149
{
144-
int ret = 0;
145-
if( ctx == NULL )
146-
return( MBEDTLS_ERR_AES_BAD_INPUT_DATA );
147-
148-
switch( keybits )
149-
{
150-
case 128:
151-
{
152-
ctx->CC_cipherFlag = SASI_AES_DECRYPT;
153-
ctx->CC_keySizeInBytes = ( keybits / 8 );
154-
memcpy( ctx->CC_Key, key, ctx->CC_keySizeInBytes );
155-
}
156-
break;
157-
case 192:
158-
case 256:
159-
return( MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED );
160-
default:
161-
return( MBEDTLS_ERR_AES_INVALID_KEY_LENGTH );
162-
}
163-
164-
return( 0 );
165-
150+
return( CC_aes_setkey( ctx, key, keybits, SASI_AES_DECRYPT ) );
166151
}
167152

168153
static int CC_aes_cipher( mbedtls_aes_context *ctx,
@@ -237,7 +222,6 @@ int mbedtls_aes_crypt_ecb( mbedtls_aes_context *ctx,
237222
return( MBEDTLS_ERR_AES_BAD_INPUT_DATA );
238223

239224
return( CC_aes_cipher( ctx, mode, SASI_AES_MODE_ECB, 16, NULL, 0, input, output ) );
240-
241225
}
242226

243227
#if defined(MBEDTLS_CIPHER_MODE_CBC)
@@ -251,7 +235,6 @@ int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx,
251235
if( ctx == NULL )
252236
return ( MBEDTLS_ERR_AES_BAD_INPUT_DATA );
253237

254-
255238
if( length % SASI_AES_BLOCK_SIZE_IN_BYTES )
256239
return( MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH );
257240

@@ -317,7 +300,6 @@ int mbedtls_aes_crypt_ctr( mbedtls_aes_context *ctx,
317300
{
318301
goto exit;
319302
}
320-
321303
}
322304
*nc_off = ( length % SASI_AES_BLOCK_SIZE_IN_BYTES );
323305

@@ -326,4 +308,3 @@ int mbedtls_aes_crypt_ctr( mbedtls_aes_context *ctx,
326308
}
327309
#endif /* MBEDTLS_CIPHER_MODE_CTR */
328310
#endif/* MBEDTLS_AES_ALT */
329-

0 commit comments

Comments
 (0)