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