mirror of
https://github.com/Mbed-TLS/mbedtls.git
synced 2025-03-28 19:21:08 +00:00
Add support for key inversion using AES-NI
This commit is contained in:
parent
80637c7520
commit
01e31bbffb
@ -81,6 +81,16 @@ int aesni_gcm_mult( unsigned char c[16],
|
|||||||
const unsigned char a[16],
|
const unsigned char a[16],
|
||||||
const unsigned char b[16] );
|
const unsigned char b[16] );
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief Compute decryption round keys from encryption round keys
|
||||||
|
*
|
||||||
|
* \param invkey Round keys for the equivalent inverse cipher
|
||||||
|
* \param fwdkey Original round keys (for encryption)
|
||||||
|
* \param nr Number of rounds (that is, number of round keys minus one)
|
||||||
|
*/
|
||||||
|
void aesni_inverse_key( unsigned char *invkey,
|
||||||
|
const unsigned char *fwdkey, int nr );
|
||||||
|
|
||||||
#endif /* POLARSSL_HAVE_X86_64 */
|
#endif /* POLARSSL_HAVE_X86_64 */
|
||||||
|
|
||||||
#endif /* POLARSSL_AESNI_H */
|
#endif /* POLARSSL_AESNI_H */
|
||||||
|
@ -591,6 +591,15 @@ int aes_setkey_dec( aes_context *ctx, const unsigned char *key, unsigned int key
|
|||||||
if( ret != 0 )
|
if( ret != 0 )
|
||||||
return( ret );
|
return( ret );
|
||||||
|
|
||||||
|
#if defined(POLARSSL_AESNI_C) && defined(POLARSSL_HAVE_X86_64)
|
||||||
|
if( aesni_supports( POLARSSL_AESNI_AES ) )
|
||||||
|
{
|
||||||
|
aesni_inverse_key( (unsigned char *) ctx->rk,
|
||||||
|
(const unsigned char *) cty.rk, ctx->nr );
|
||||||
|
goto done;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
SK = cty.rk + cty.nr * 4;
|
SK = cty.rk + cty.nr * 4;
|
||||||
|
|
||||||
*RK++ = *SK++;
|
*RK++ = *SK++;
|
||||||
@ -614,6 +623,7 @@ int aes_setkey_dec( aes_context *ctx, const unsigned char *key, unsigned int key
|
|||||||
*RK++ = *SK++;
|
*RK++ = *SK++;
|
||||||
*RK++ = *SK++;
|
*RK++ = *SK++;
|
||||||
|
|
||||||
|
done:
|
||||||
memset( &cty, 0, sizeof( aes_context ) );
|
memset( &cty, 0, sizeof( aes_context ) );
|
||||||
|
|
||||||
return( 0 );
|
return( 0 );
|
||||||
|
@ -215,6 +215,28 @@ int aesni_gcm_mult( unsigned char c[16],
|
|||||||
return( 0 );
|
return( 0 );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Compute decryption round keys from encryption round keys
|
||||||
|
*/
|
||||||
|
void aesni_inverse_key( unsigned char *invkey,
|
||||||
|
const unsigned char *fwdkey, int nr )
|
||||||
|
{
|
||||||
|
unsigned char *ik = invkey;
|
||||||
|
const unsigned char *fk = fwdkey + 16 * nr;
|
||||||
|
|
||||||
|
memcpy( ik, fk, 16 );
|
||||||
|
|
||||||
|
for( fk -= 16, ik += 16; fk > fwdkey; fk -= 16, ik += 16 )
|
||||||
|
asm( "movdqu (%0), %%xmm0 \n"
|
||||||
|
"aesimc %%xmm0, %%xmm0 \n"
|
||||||
|
"movdqu %%xmm0, (%1) \n"
|
||||||
|
:
|
||||||
|
: "r" (fk), "r" (ik)
|
||||||
|
: "memory", "xmm0" );
|
||||||
|
|
||||||
|
memcpy( ik, fk, 16 );
|
||||||
|
}
|
||||||
|
|
||||||
#endif /* POLARSSL_HAVE_X86_64 */
|
#endif /* POLARSSL_HAVE_X86_64 */
|
||||||
|
|
||||||
#endif /* POLARSSL_AESNI_C */
|
#endif /* POLARSSL_AESNI_C */
|
||||||
|
Loading…
x
Reference in New Issue
Block a user