|
| 1 | +/* LibTomCrypt, modular cryptographic library -- Tom St Denis */ |
| 2 | +/* SPDX-License-Identifier: Unlicense */ |
| 3 | + |
| 4 | +/* Auto-detection of AES implementation by Steffen Jaeckel */ |
| 5 | +/** |
| 6 | + @file aes_desc.c |
| 7 | + Run-time detection of correct AES implementation |
| 8 | +*/ |
| 9 | + |
| 10 | +#include "tomcrypt_private.h" |
| 11 | + |
| 12 | +#if defined(LTC_RIJNDAEL) |
| 13 | + |
| 14 | +#ifndef ENCRYPT_ONLY |
| 15 | + |
| 16 | +#define AES_SETUP aes_setup |
| 17 | +#define AES_ENC aes_ecb_encrypt |
| 18 | +#define AES_DEC aes_ecb_decrypt |
| 19 | +#define AES_DONE aes_done |
| 20 | +#define AES_TEST aes_test |
| 21 | +#define AES_KS aes_keysize |
| 22 | + |
| 23 | +const struct ltc_cipher_descriptor aes_desc = |
| 24 | +{ |
| 25 | + "aes", |
| 26 | + 6, |
| 27 | + 16, 32, 16, 10, |
| 28 | + AES_SETUP, AES_ENC, AES_DEC, AES_TEST, AES_DONE, AES_KS, |
| 29 | + NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL |
| 30 | +}; |
| 31 | + |
| 32 | +#else |
| 33 | + |
| 34 | +#define AES_SETUP aes_enc_setup |
| 35 | +#define AES_ENC aes_enc_ecb_encrypt |
| 36 | +#define AES_DONE aes_enc_done |
| 37 | +#define AES_KS aes_enc_keysize |
| 38 | + |
| 39 | +const struct ltc_cipher_descriptor aes_enc_desc = |
| 40 | +{ |
| 41 | + "aes", |
| 42 | + 6, |
| 43 | + 16, 32, 16, 10, |
| 44 | + AES_SETUP, AES_ENC, NULL, NULL, AES_DONE, AES_KS, |
| 45 | + NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL |
| 46 | +}; |
| 47 | + |
| 48 | +#endif |
| 49 | + |
| 50 | +/* Code partially borrowed from https://software.intel.com/content/www/us/en/develop/articles/intel-sha-extensions.html */ |
| 51 | +#if defined(LTC_HAS_AES_NI) |
| 52 | +static LTC_INLINE int s_aesni_is_supported(void) |
| 53 | +{ |
| 54 | + static int initialized = 0, is_supported = 0; |
| 55 | + |
| 56 | + if (initialized == 0) { |
| 57 | + int a, b, c, d; |
| 58 | + |
| 59 | + /* Look for CPUID.1.0.ECX[25] |
| 60 | + * EAX = 1, ECX = 0 |
| 61 | + */ |
| 62 | + a = 1; |
| 63 | + c = 0; |
| 64 | + |
| 65 | + asm volatile ("cpuid" |
| 66 | + :"=a"(a), "=b"(b), "=c"(c), "=d"(d) |
| 67 | + :"a"(a), "c"(c) |
| 68 | + ); |
| 69 | + |
| 70 | + is_supported = ((c >> 25) & 1); |
| 71 | + initialized = 1; |
| 72 | + } |
| 73 | + |
| 74 | + return is_supported; |
| 75 | +} |
| 76 | + |
| 77 | +#ifndef ENCRYPT_ONLY |
| 78 | +int aesni_is_supported(void) |
| 79 | +{ |
| 80 | + return s_aesni_is_supported(); |
| 81 | +} |
| 82 | +#endif |
| 83 | +#endif |
| 84 | + |
| 85 | + /** |
| 86 | + Initialize the AES (Rijndael) block cipher |
| 87 | + @param key The symmetric key you wish to pass |
| 88 | + @param keylen The key length in bytes |
| 89 | + @param num_rounds The number of rounds desired (0 for default) |
| 90 | + @param skey The key in as scheduled by this function. |
| 91 | + @return CRYPT_OK if successful |
| 92 | + */ |
| 93 | +int AES_SETUP(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey) |
| 94 | +{ |
| 95 | +#ifdef LTC_HAS_AES_NI |
| 96 | + if (s_aesni_is_supported()) { |
| 97 | + return aesni_setup(key, keylen, num_rounds, skey); |
| 98 | + } |
| 99 | +#endif |
| 100 | + /* Last resort, software AES */ |
| 101 | + return rijndael_setup(key, keylen, num_rounds, skey); |
| 102 | +} |
| 103 | + |
| 104 | +/** |
| 105 | + Encrypts a block of text with AES |
| 106 | + @param pt The input plaintext (16 bytes) |
| 107 | + @param ct The output ciphertext (16 bytes) |
| 108 | + @param skey The key as scheduled |
| 109 | + @return CRYPT_OK if successful |
| 110 | +*/ |
| 111 | +int AES_ENC(const unsigned char *pt, unsigned char *ct, const symmetric_key *skey) |
| 112 | +{ |
| 113 | +#ifdef LTC_HAS_AES_NI |
| 114 | + if (s_aesni_is_supported()) { |
| 115 | + return aesni_ecb_encrypt(pt, ct, skey); |
| 116 | + } |
| 117 | +#endif |
| 118 | + return rijndael_ecb_encrypt(pt, ct, skey); |
| 119 | +} |
| 120 | + |
| 121 | + |
| 122 | +/** |
| 123 | + Decrypts a block of text with AES |
| 124 | + @param ct The input ciphertext (16 bytes) |
| 125 | + @param pt The output plaintext (16 bytes) |
| 126 | + @param skey The key as scheduled |
| 127 | + @return CRYPT_OK if successful |
| 128 | +*/ |
| 129 | +int AES_DEC(const unsigned char *ct, unsigned char *pt, const symmetric_key *skey) |
| 130 | +{ |
| 131 | +#ifdef LTC_HAS_AES_NI |
| 132 | + if (s_aesni_is_supported()) { |
| 133 | + return aesni_ecb_decrypt(ct, pt, skey); |
| 134 | + } |
| 135 | +#endif |
| 136 | + return rijndael_ecb_decrypt(ct, pt, skey); |
| 137 | +} |
| 138 | + |
| 139 | +/** |
| 140 | + Performs a self-test of the AES block cipher |
| 141 | + @return CRYPT_OK if functional, CRYPT_NOP if self-test has been disabled |
| 142 | +*/ |
| 143 | +int AES_TEST(void) |
| 144 | +{ |
| 145 | + #ifndef LTC_TEST |
| 146 | + return CRYPT_NOP; |
| 147 | + #else |
| 148 | + int err; |
| 149 | + static const struct { |
| 150 | + int keylen; |
| 151 | + unsigned char key[32], pt[16], ct[16]; |
| 152 | + } tests[] = { |
| 153 | + { 16, |
| 154 | + { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 155 | + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }, |
| 156 | + { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, |
| 157 | + 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff }, |
| 158 | + { 0x69, 0xc4, 0xe0, 0xd8, 0x6a, 0x7b, 0x04, 0x30, |
| 159 | + 0xd8, 0xcd, 0xb7, 0x80, 0x70, 0xb4, 0xc5, 0x5a } |
| 160 | + }, { |
| 161 | + 24, |
| 162 | + { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 163 | + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
| 164 | + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17 }, |
| 165 | + { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, |
| 166 | + 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff }, |
| 167 | + { 0xdd, 0xa9, 0x7c, 0xa4, 0x86, 0x4c, 0xdf, 0xe0, |
| 168 | + 0x6e, 0xaf, 0x70, 0xa0, 0xec, 0x0d, 0x71, 0x91 } |
| 169 | + }, { |
| 170 | + 32, |
| 171 | + { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 172 | + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
| 173 | + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, |
| 174 | + 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f }, |
| 175 | + { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, |
| 176 | + 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff }, |
| 177 | + { 0x8e, 0xa2, 0xb7, 0xca, 0x51, 0x67, 0x45, 0xbf, |
| 178 | + 0xea, 0xfc, 0x49, 0x90, 0x4b, 0x49, 0x60, 0x89 } |
| 179 | + } |
| 180 | + }; |
| 181 | + |
| 182 | + symmetric_key key; |
| 183 | + unsigned char tmp[2][16]; |
| 184 | + int i, y; |
| 185 | + |
| 186 | + for (i = 0; i < (int)(sizeof(tests)/sizeof(tests[0])); i++) { |
| 187 | + zeromem(&key, sizeof(key)); |
| 188 | + if ((err = aes_setup(tests[i].key, tests[i].keylen, 0, &key)) != CRYPT_OK) { |
| 189 | + return err; |
| 190 | + } |
| 191 | + |
| 192 | + aes_ecb_encrypt(tests[i].pt, tmp[0], &key); |
| 193 | + aes_ecb_decrypt(tmp[0], tmp[1], &key); |
| 194 | + if (compare_testvector(tmp[0], 16, tests[i].ct, 16, "AES Encrypt", i) || |
| 195 | + compare_testvector(tmp[1], 16, tests[i].pt, 16, "AES Decrypt", i)) { |
| 196 | + return CRYPT_FAIL_TESTVECTOR; |
| 197 | + } |
| 198 | + |
| 199 | + /* now see if we can encrypt all zero bytes 1000 times, decrypt and come back where we started */ |
| 200 | + for (y = 0; y < 16; y++) tmp[0][y] = 0; |
| 201 | + for (y = 0; y < 1000; y++) aes_ecb_encrypt(tmp[0], tmp[0], &key); |
| 202 | + for (y = 0; y < 1000; y++) aes_ecb_decrypt(tmp[0], tmp[0], &key); |
| 203 | + for (y = 0; y < 16; y++) if (tmp[0][y] != 0) return CRYPT_FAIL_TESTVECTOR; |
| 204 | + } |
| 205 | + return CRYPT_OK; |
| 206 | + #endif |
| 207 | +} |
| 208 | + |
| 209 | + |
| 210 | +/** Terminate the context |
| 211 | + @param skey The scheduled key |
| 212 | +*/ |
| 213 | +void AES_DONE(symmetric_key *skey) |
| 214 | +{ |
| 215 | + LTC_UNUSED_PARAM(skey); |
| 216 | +} |
| 217 | + |
| 218 | + |
| 219 | +/** |
| 220 | + Gets suitable key size |
| 221 | + @param keysize [in/out] The length of the recommended key (in bytes). This function will store the suitable size back in this variable. |
| 222 | + @return CRYPT_OK if the input key size is acceptable. |
| 223 | +*/ |
| 224 | +int AES_KS(int *keysize) |
| 225 | +{ |
| 226 | + LTC_ARGCHK(keysize != NULL); |
| 227 | + |
| 228 | + if (*keysize < 16) { |
| 229 | + return CRYPT_INVALID_KEYSIZE; |
| 230 | + } |
| 231 | + if (*keysize < 24) { |
| 232 | + *keysize = 16; |
| 233 | + return CRYPT_OK; |
| 234 | + } |
| 235 | + if (*keysize < 32) { |
| 236 | + *keysize = 24; |
| 237 | + return CRYPT_OK; |
| 238 | + } |
| 239 | + *keysize = 32; |
| 240 | + return CRYPT_OK; |
| 241 | +} |
| 242 | + |
| 243 | +#endif |
| 244 | + |
0 commit comments