2018-09-24 18:07:34 +00:00
|
|
|
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
|
|
|
*
|
|
|
|
* LibTomCrypt is a library that provides various cryptographic
|
|
|
|
* algorithms in a highly modular and flexible manner.
|
|
|
|
*
|
|
|
|
* The library is free for all purposes without any express
|
|
|
|
* guarantee it works.
|
|
|
|
*
|
|
|
|
* Tom St Denis, tomstdenis@gmail.com, http://libtom.org
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
@file aes.c
|
|
|
|
Implementation of AES
|
2023-06-07 05:58:01 +00:00
|
|
|
*/
|
2018-09-24 18:07:34 +00:00
|
|
|
|
2023-06-07 05:58:01 +00:00
|
|
|
#include "tomcrypt_private.h"
|
2018-09-24 18:07:34 +00:00
|
|
|
|
|
|
|
#ifdef LTC_RIJNDAEL
|
|
|
|
|
2023-05-25 09:30:11 +00:00
|
|
|
#include "mbtls_aes.h"
|
|
|
|
|
|
|
|
static __thread mbedtls_aes_context ctx_encrypt;
|
|
|
|
|
|
|
|
#ifndef ENCRYPT_ONLY
|
|
|
|
static __thread mbedtls_aes_context ctx_decrypt;
|
2018-09-24 18:07:34 +00:00
|
|
|
|
|
|
|
#define SETUP rijndael_setup
|
|
|
|
#define ECB_ENC rijndael_ecb_encrypt
|
|
|
|
#define ECB_DEC rijndael_ecb_decrypt
|
|
|
|
#define ECB_DONE rijndael_done
|
|
|
|
#define ECB_TEST rijndael_test
|
|
|
|
#define ECB_KS rijndael_keysize
|
|
|
|
|
|
|
|
const struct ltc_cipher_descriptor rijndael_desc =
|
|
|
|
{
|
|
|
|
"rijndael",
|
|
|
|
6,
|
|
|
|
16, 32, 16, 10,
|
|
|
|
SETUP, ECB_ENC, ECB_DEC, ECB_TEST, ECB_DONE, ECB_KS,
|
2023-06-07 05:58:01 +00:00
|
|
|
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
|
2018-09-24 18:07:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const struct ltc_cipher_descriptor aes_desc =
|
|
|
|
{
|
|
|
|
"aes",
|
|
|
|
6,
|
|
|
|
16, 32, 16, 10,
|
|
|
|
SETUP, ECB_ENC, ECB_DEC, ECB_TEST, ECB_DONE, ECB_KS,
|
2023-06-07 05:58:01 +00:00
|
|
|
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
|
2018-09-24 18:07:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#define SETUP rijndael_enc_setup
|
|
|
|
#define ECB_ENC rijndael_enc_ecb_encrypt
|
|
|
|
#define ECB_KS rijndael_enc_keysize
|
|
|
|
#define ECB_DONE rijndael_enc_done
|
|
|
|
|
|
|
|
const struct ltc_cipher_descriptor rijndael_enc_desc =
|
|
|
|
{
|
|
|
|
"rijndael",
|
|
|
|
6,
|
|
|
|
16, 32, 16, 10,
|
|
|
|
SETUP, ECB_ENC, NULL, NULL, ECB_DONE, ECB_KS,
|
2023-06-07 05:58:01 +00:00
|
|
|
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
|
2018-09-24 18:07:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const struct ltc_cipher_descriptor aes_enc_desc =
|
|
|
|
{
|
|
|
|
"aes",
|
|
|
|
6,
|
|
|
|
16, 32, 16, 10,
|
|
|
|
SETUP, ECB_ENC, NULL, NULL, ECB_DONE, ECB_KS,
|
2023-06-07 05:58:01 +00:00
|
|
|
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
|
2018-09-24 18:07:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
|
|
|
Initialize the AES (Rijndael) block cipher
|
|
|
|
@param key The symmetric key you wish to pass
|
|
|
|
@param keylen The key length in bytes
|
|
|
|
@param num_rounds The number of rounds desired (0 for default)
|
|
|
|
@param skey The key in as scheduled by this function.
|
|
|
|
@return CRYPT_OK if successful
|
|
|
|
*/
|
|
|
|
int SETUP(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
|
|
|
|
{
|
|
|
|
LTC_ARGCHK(key != NULL);
|
|
|
|
LTC_ARGCHK(skey != NULL);
|
2023-06-07 05:58:01 +00:00
|
|
|
|
2018-09-24 18:07:34 +00:00
|
|
|
if (keylen != 16 && keylen != 24 && keylen != 32) {
|
|
|
|
return CRYPT_INVALID_KEYSIZE;
|
|
|
|
}
|
2023-06-07 05:58:01 +00:00
|
|
|
|
2018-09-24 18:07:34 +00:00
|
|
|
if (num_rounds != 0 && num_rounds != (10 + ((keylen/8)-2)*2)) {
|
|
|
|
return CRYPT_INVALID_ROUNDS;
|
|
|
|
}
|
|
|
|
|
2023-05-25 09:30:11 +00:00
|
|
|
mbedtls_aes_init(&ctx_encrypt);
|
|
|
|
if (mbedtls_aes_setkey_enc(&ctx_encrypt, key, keylen*8) != 0)
|
|
|
|
return CRYPT_INVALID_KEYSIZE;
|
|
|
|
memcpy(skey->rijndael.eK, ctx_encrypt.buf, sizeof(skey->rijndael.eK));
|
2018-09-24 18:07:34 +00:00
|
|
|
|
2023-05-25 09:30:11 +00:00
|
|
|
#ifndef ENCRYPT_ONLY
|
|
|
|
mbedtls_aes_init(&ctx_decrypt);
|
|
|
|
if (mbedtls_aes_setkey_dec(&ctx_decrypt, key, keylen*8) != 0)
|
|
|
|
return CRYPT_INVALID_KEYSIZE;
|
|
|
|
memcpy(skey->rijndael.dK, ctx_decrypt.buf, sizeof(skey->rijndael.dK));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
skey->rijndael.Nr = ctx_encrypt.nr;
|
2018-09-24 18:07:34 +00:00
|
|
|
|
2023-06-07 05:58:01 +00:00
|
|
|
return CRYPT_OK;
|
2018-09-24 18:07:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Encrypts a block of text with AES
|
|
|
|
@param pt The input plaintext (16 bytes)
|
|
|
|
@param ct The output ciphertext (16 bytes)
|
|
|
|
@param skey The key as scheduled
|
|
|
|
@return CRYPT_OK if successful
|
|
|
|
*/
|
|
|
|
#ifdef LTC_CLEAN_STACK
|
2023-06-07 05:58:01 +00:00
|
|
|
static int s_rijndael_ecb_encrypt(const unsigned char *pt, unsigned char *ct, const symmetric_key *skey)
|
2018-09-24 18:07:34 +00:00
|
|
|
#else
|
2023-06-07 05:58:01 +00:00
|
|
|
int ECB_ENC(const unsigned char *pt, unsigned char *ct, const symmetric_key *skey)
|
2018-09-24 18:07:34 +00:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
LTC_ARGCHK(pt != NULL);
|
|
|
|
LTC_ARGCHK(ct != NULL);
|
|
|
|
LTC_ARGCHK(skey != NULL);
|
|
|
|
|
2023-05-25 09:30:11 +00:00
|
|
|
ctx_encrypt.nr = skey->rijndael.Nr;
|
|
|
|
memset(ctx_encrypt.buf, 0, sizeof(ctx_encrypt.buf));
|
|
|
|
memcpy(ctx_encrypt.buf, skey->rijndael.eK, sizeof(skey->rijndael.eK));
|
2018-09-24 18:07:34 +00:00
|
|
|
|
2023-05-25 09:30:11 +00:00
|
|
|
return mbedtls_aes_crypt_ecb(&ctx_encrypt, MBEDTLS_AES_ENCRYPT, pt, ct) == 0 ? CRYPT_OK : CRYPT_ERROR;
|
2018-09-24 18:07:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef LTC_CLEAN_STACK
|
2023-06-07 05:58:01 +00:00
|
|
|
int ECB_ENC(const unsigned char *pt, unsigned char *ct, const symmetric_key *skey)
|
2018-09-24 18:07:34 +00:00
|
|
|
{
|
2023-06-07 05:58:01 +00:00
|
|
|
return s_rijndael_ecb_encrypt(pt, ct, skey);
|
2018-09-24 18:07:34 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-06-07 05:58:01 +00:00
|
|
|
#ifndef ENCRYPT_ONLY
|
2018-09-24 18:07:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
Decrypts a block of text with AES
|
|
|
|
@param ct The input ciphertext (16 bytes)
|
|
|
|
@param pt The output plaintext (16 bytes)
|
2023-06-07 05:58:01 +00:00
|
|
|
@param skey The key as scheduled
|
2018-09-24 18:07:34 +00:00
|
|
|
@return CRYPT_OK if successful
|
|
|
|
*/
|
|
|
|
#ifdef LTC_CLEAN_STACK
|
2023-06-07 05:58:01 +00:00
|
|
|
static int s_rijndael_ecb_decrypt(const unsigned char *ct, unsigned char *pt, const symmetric_key *skey)
|
2018-09-24 18:07:34 +00:00
|
|
|
#else
|
2023-06-07 05:58:01 +00:00
|
|
|
int ECB_DEC(const unsigned char *ct, unsigned char *pt, const symmetric_key *skey)
|
2018-09-24 18:07:34 +00:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
LTC_ARGCHK(pt != NULL);
|
|
|
|
LTC_ARGCHK(ct != NULL);
|
|
|
|
LTC_ARGCHK(skey != NULL);
|
|
|
|
|
2023-05-25 09:30:11 +00:00
|
|
|
ctx_decrypt.nr = skey->rijndael.Nr;
|
|
|
|
memset(ctx_decrypt.buf, 0, sizeof(ctx_decrypt.buf));
|
|
|
|
memcpy(ctx_decrypt.buf, skey->rijndael.dK, sizeof(skey->rijndael.dK));
|
|
|
|
|
|
|
|
return mbedtls_aes_crypt_ecb(&ctx_decrypt, MBEDTLS_AES_DECRYPT, ct, pt) == 0 ? CRYPT_OK : CRYPT_ERROR;
|
2018-09-24 18:07:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef LTC_CLEAN_STACK
|
2023-06-07 05:58:01 +00:00
|
|
|
int ECB_DEC(const unsigned char *ct, unsigned char *pt, const symmetric_key *skey)
|
2018-09-24 18:07:34 +00:00
|
|
|
{
|
2023-06-07 05:58:01 +00:00
|
|
|
return s_rijndael_ecb_decrypt(ct, pt, skey);
|
2018-09-24 18:07:34 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
|
|
|
Performs a self-test of the AES block cipher
|
|
|
|
@return CRYPT_OK if functional, CRYPT_NOP if self-test has been disabled
|
|
|
|
*/
|
|
|
|
int ECB_TEST(void)
|
|
|
|
{
|
|
|
|
#ifndef LTC_TEST
|
|
|
|
return CRYPT_NOP;
|
2023-06-07 05:58:01 +00:00
|
|
|
#else
|
2018-09-24 18:07:34 +00:00
|
|
|
int err;
|
|
|
|
static const struct {
|
|
|
|
int keylen;
|
|
|
|
unsigned char key[32], pt[16], ct[16];
|
|
|
|
} tests[] = {
|
|
|
|
{ 16,
|
2023-06-07 05:58:01 +00:00
|
|
|
{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
|
|
|
|
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
|
2018-09-24 18:07:34 +00:00
|
|
|
{ 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
|
|
|
|
0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff },
|
2023-06-07 05:58:01 +00:00
|
|
|
{ 0x69, 0xc4, 0xe0, 0xd8, 0x6a, 0x7b, 0x04, 0x30,
|
2018-09-24 18:07:34 +00:00
|
|
|
0xd8, 0xcd, 0xb7, 0x80, 0x70, 0xb4, 0xc5, 0x5a }
|
2023-06-07 05:58:01 +00:00
|
|
|
}, {
|
2018-09-24 18:07:34 +00:00
|
|
|
24,
|
2023-06-07 05:58:01 +00:00
|
|
|
{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
|
2018-09-24 18:07:34 +00:00
|
|
|
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
|
|
|
|
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17 },
|
|
|
|
{ 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
|
|
|
|
0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff },
|
2023-06-07 05:58:01 +00:00
|
|
|
{ 0xdd, 0xa9, 0x7c, 0xa4, 0x86, 0x4c, 0xdf, 0xe0,
|
2018-09-24 18:07:34 +00:00
|
|
|
0x6e, 0xaf, 0x70, 0xa0, 0xec, 0x0d, 0x71, 0x91 }
|
|
|
|
}, {
|
|
|
|
32,
|
2023-06-07 05:58:01 +00:00
|
|
|
{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
|
2018-09-24 18:07:34 +00:00
|
|
|
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
|
2023-06-07 05:58:01 +00:00
|
|
|
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
|
2018-09-24 18:07:34 +00:00
|
|
|
0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f },
|
|
|
|
{ 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
|
|
|
|
0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff },
|
2023-06-07 05:58:01 +00:00
|
|
|
{ 0x8e, 0xa2, 0xb7, 0xca, 0x51, 0x67, 0x45, 0xbf,
|
2018-09-24 18:07:34 +00:00
|
|
|
0xea, 0xfc, 0x49, 0x90, 0x4b, 0x49, 0x60, 0x89 }
|
|
|
|
}
|
|
|
|
};
|
2023-06-07 05:58:01 +00:00
|
|
|
|
|
|
|
symmetric_key key;
|
|
|
|
unsigned char tmp[2][16];
|
|
|
|
int i, y;
|
|
|
|
|
|
|
|
for (i = 0; i < (int)(sizeof(tests)/sizeof(tests[0])); i++) {
|
2018-09-24 18:07:34 +00:00
|
|
|
zeromem(&key, sizeof(key));
|
2023-06-07 05:58:01 +00:00
|
|
|
if ((err = rijndael_setup(tests[i].key, tests[i].keylen, 0, &key)) != CRYPT_OK) {
|
2018-09-24 18:07:34 +00:00
|
|
|
return err;
|
|
|
|
}
|
2023-06-07 05:58:01 +00:00
|
|
|
|
2018-09-24 18:07:34 +00:00
|
|
|
rijndael_ecb_encrypt(tests[i].pt, tmp[0], &key);
|
|
|
|
rijndael_ecb_decrypt(tmp[0], tmp[1], &key);
|
2023-06-07 05:58:01 +00:00
|
|
|
if (compare_testvector(tmp[0], 16, tests[i].ct, 16, "AES Encrypt", i) ||
|
|
|
|
compare_testvector(tmp[1], 16, tests[i].pt, 16, "AES Decrypt", i)) {
|
2018-09-24 18:07:34 +00:00
|
|
|
return CRYPT_FAIL_TESTVECTOR;
|
|
|
|
}
|
|
|
|
|
2023-06-07 05:58:01 +00:00
|
|
|
/* now see if we can encrypt all zero bytes 1000 times, decrypt and come back where we started */
|
|
|
|
for (y = 0; y < 16; y++) tmp[0][y] = 0;
|
|
|
|
for (y = 0; y < 1000; y++) rijndael_ecb_encrypt(tmp[0], tmp[0], &key);
|
|
|
|
for (y = 0; y < 1000; y++) rijndael_ecb_decrypt(tmp[0], tmp[0], &key);
|
|
|
|
for (y = 0; y < 16; y++) if (tmp[0][y] != 0) return CRYPT_FAIL_TESTVECTOR;
|
|
|
|
}
|
|
|
|
return CRYPT_OK;
|
2018-09-24 18:07:34 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* ENCRYPT_ONLY */
|
|
|
|
|
|
|
|
|
2023-06-07 05:58:01 +00:00
|
|
|
/** Terminate the context
|
2018-09-24 18:07:34 +00:00
|
|
|
@param skey The scheduled key
|
|
|
|
*/
|
|
|
|
void ECB_DONE(symmetric_key *skey)
|
|
|
|
{
|
2023-05-25 09:30:11 +00:00
|
|
|
mbedtls_aes_free(&ctx_encrypt);
|
|
|
|
#ifndef ENCRYPT_ONLY
|
|
|
|
mbedtls_aes_free(&ctx_decrypt);
|
|
|
|
#endif
|
2018-09-24 18:07:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Gets suitable key size
|
|
|
|
@param keysize [in/out] The length of the recommended key (in bytes). This function will store the suitable size back in this variable.
|
|
|
|
@return CRYPT_OK if the input key size is acceptable.
|
|
|
|
*/
|
|
|
|
int ECB_KS(int *keysize)
|
|
|
|
{
|
|
|
|
LTC_ARGCHK(keysize != NULL);
|
|
|
|
|
2023-06-07 05:58:01 +00:00
|
|
|
if (*keysize < 16) {
|
2018-09-24 18:07:34 +00:00
|
|
|
return CRYPT_INVALID_KEYSIZE;
|
2023-06-07 05:58:01 +00:00
|
|
|
}
|
2018-09-24 18:07:34 +00:00
|
|
|
if (*keysize < 24) {
|
|
|
|
*keysize = 16;
|
|
|
|
return CRYPT_OK;
|
2023-06-07 05:58:01 +00:00
|
|
|
}
|
|
|
|
if (*keysize < 32) {
|
2018-09-24 18:07:34 +00:00
|
|
|
*keysize = 24;
|
|
|
|
return CRYPT_OK;
|
|
|
|
}
|
2023-06-07 05:58:01 +00:00
|
|
|
*keysize = 32;
|
|
|
|
return CRYPT_OK;
|
2018-09-24 18:07:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|