Skip to content

Commit 9e0a131

Browse files
committed
introduce separate aes_desc
`aes_desc` and `aes_enc_desc` now do auto-detection of the best suitable AES implementation for the platform. Signed-off-by: Steffen Jaeckel <[email protected]>
1 parent 2314444 commit 9e0a131

16 files changed

+306
-100
lines changed

doc/crypt.tex

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,10 @@ \subsection{Simple Encryption Demonstration}
614614
\hline SAFER+ & saferp\_desc &16 & 16, 24, 32 & 8, 12, 16 & 4 \\
615615
\hline AES & aes\_desc & 16 & 16, 24, 32 & 10, 12, 14 & 6 \\
616616
& aes\_enc\_desc & 16 & 16, 24, 32 & 10, 12, 14 & 6 \\
617-
& aesni\_desc & 16 & 16, 24, 32 & 10, 12, 14 & 6 \\
617+
& rijndael\_desc & 16 & 16, 24, 32 & 10, 12, 14 & 6 \\
618+
& rijndael\_enc\_desc & 16 & 16, 24, 32 & 10, 12, 14 & 6 \\
619+
\hline AES & aesni\_desc & 16 & 16, 24, 32 & 10, 12, 14 & 6 \\
620+
(only on x86 with SSE4.1) &&&&& \\
618621
\hline Twofish & twofish\_desc & 16 & 16, 24, 32 & 16 & 7 \\
619622
\hline DES & des\_desc & 8 & 8 & 16 & 13 \\
620623
\hline 3DES (EDE mode) & des3\_desc & 8 & 16, 24 & 16 & 14 \\
@@ -640,24 +643,30 @@ \subsection{Notes}
640643
\begin{small}
641644
\begin{enumerate}
642645
\item
643-
For AES, (also known as Rijndael) there are four descriptors which complicate issues a little. The descriptors
644-
rijndael\_desc and rijndael\_enc\_desc provide the cipher named \textit{rijndael}. The descriptors aes\_desc and
645-
aes\_enc\_desc provide the cipher name \textit{aes}. Functionally both \textit{rijndael} and \textit{aes} are the same cipher. The
646+
For AES, (also known as Rijndael) there are multiple descriptors which complicate issues a little. As of FIXME-version-next
647+
the library also integrates hardware-accelerated AES operations (e.g. AES-NI on amd64 with SSE4.1). Therefor the functionality of
648+
the descriptors for the AES algorithm has changed. The rijndael\_desc and rijndael\_enc\_desc descriptors provide the cipher
649+
named \textit{rijndael} which contains the software implementation of the algorithm. The descriptors aes\_desc and aes\_enc\_desc
650+
provide the cipher named \textit{aes} and implement an auto-detection mechanism that chooses between the software-only \textit{rijndael}
651+
and the hardware-accelerated implementation.
652+
653+
Functionally both \textit{rijndael} and \textit{aes} are the same cipher. The
646654
only difference is when you call find\_cipher() you have to pass the correct name. The cipher descriptors with \textit{enc}
647655
in the middle (e.g. rijndael\_enc\_desc) are related to an implementation of Rijndael with only the encryption routine
648656
and tables. The decryption and self--test function pointers of both \textit{encrypt only} descriptors are set to \textbf{NULL} and
649657
should not be called.
650658

651659
The \textit{encrypt only} descriptors are useful for applications that only use the encryption function of the cipher. Algorithms such
652-
as EAX, PMAC and OMAC only require the encryption function. So far this \textit{encrypt only} functionality has only been implemented for
653-
Rijndael as it makes the most sense for this cipher.
660+
as EAX, PMAC and OMAC or the CTR mode only require the encryption function. So far this \textit{encrypt only} functionality has only
661+
been implemented for Rijndael as it makes the most sense for this cipher.
654662

655663
\item
656664
Note that for \textit{DES} and \textit{3DES} they use 8 and 24 byte keys but only 7 and 21 [respectively] bytes of the keys are in
657665
fact used for the purposes of encryption. My suggestion is just to use random 8/24 byte keys instead of trying to make a 8/24
658666
byte string from the real 7/21 byte key.
659667

660-
For \textit{3DES} exists a two-key mode, that can be initialized by calling the setup function with a \textit{keylen} of 16. This results in the re-usage of key \textit{K1} as key \textit{K3}. This mode has been specified as \textit{Keying Option 2} in FIPS 46-3.
668+
For \textit{3DES} exists a two-key mode, that can be initialized by calling the setup function with a \textit{keylen} of 16.
669+
This results in the re-usage of key \textit{K1} as key \textit{K3}. This mode has been specified as \textit{Keying Option 2} in FIPS 46-3.
661670

662671
\item
663672
Note that \textit{Twofish} has additional configuration options (Figure \ref{fig:twofishopts}) that take place at build time. These options are found in

helper.pl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ sub process_makefiles {
316316
my @t = qw();
317317
find({ no_chdir => 1, wanted => sub { push @t, $_ if $_ =~ /(common|no_prng|_tests?|test).c$/ } }, 'tests');
318318

319-
my @o = sort ('src/ciphers/aes/aes_enc.o', map { my $x = $_; $x =~ s/\.c$/.o/; $x } @c);
319+
my @o = sort ('src/ciphers/aes/aes_enc.o', 'src/ciphers/aes/aes_enc_desc.o', map { my $x = $_; $x =~ s/\.c$/.o/; $x } @c);
320320
my $var_o = prepare_variable("OBJECTS", @o);
321321
my $var_h = prepare_variable("HEADERS_PUB", (sort @h));
322322
(my $var_obj = $var_o) =~ s/\.o\b/.obj/sg;

makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ LTC_EXTRALIBS += $(EXTRALIBS)
4646

4747
#AES comes in two flavours... enc+dec and enc
4848
src/ciphers/aes/aes_enc.o: src/ciphers/aes/aes.c src/ciphers/aes/aes_tab.c
49+
ifneq ($V,1)
50+
@echo " * ${CC} $@" ${silent_echo}
51+
endif
52+
${silent} ${CC} ${LTC_CFLAGS} -DENCRYPT_ONLY -c $< -o $@
53+
src/ciphers/aes/aes_enc_desc.o: src/ciphers/aes/aes_desc.c
4954
ifneq ($V,1)
5055
@echo " * ${CC} $@" ${silent_echo}
5156
endif

makefile.mingw

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,8 @@ default: $(LIBMAIN_S)
246246
#SPECIAL: AES comes in two flavours - enc+dec and enc-only
247247
src/ciphers/aes/aes_enc.o: src/ciphers/aes/aes.c src/ciphers/aes/aes_tab.c
248248
$(CC) $(LTC_CFLAGS) -DENCRYPT_ONLY -c src/ciphers/aes/aes.c -o src/ciphers/aes/aes_enc.o
249+
src/ciphers/aes/aes_enc_desc.o: src/ciphers/aes/aes.c
250+
$(CC) $(LTC_CFLAGS) -DENCRYPT_ONLY -c src/ciphers/aes/aes_desc.c -o src/ciphers/aes/aes_enc_desc.o
249251

250252
#SPECIAL: these are the rules to make certain object files
251253
src/ciphers/aes/aes.o: src/ciphers/aes/aes.c src/ciphers/aes/aes_tab.c

makefile.msvc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,8 @@ default: $(LIBMAIN_S)
239239
#SPECIAL: AES comes in two flavours - enc+dec and enc-only
240240
src/ciphers/aes/aes_enc.obj: src/ciphers/aes/aes.c src/ciphers/aes/aes_tab.c
241241
$(CC) $(LTC_CFLAGS) /DENCRYPT_ONLY /c src/ciphers/aes/aes.c /Fosrc/ciphers/aes/aes_enc.obj
242+
src/ciphers/aes/aes_enc_desc.obj: src/ciphers/aes/aes_desc.c
243+
$(CC) $(LTC_CFLAGS) /DENCRYPT_ONLY /c src/ciphers/aes/aes_desc.c /Fosrc/ciphers/aes/aes_enc_desc.obj
242244

243245
#SPECIAL: these are the rules to make certain object files
244246
src/ciphers/aes/aes.obj: src/ciphers/aes/aes.c src/ciphers/aes/aes_tab.c

makefile.shared

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ endif
7070
#ciphers come in two flavours... enc+dec and enc
7171
src/ciphers/aes/aes_enc.o: src/ciphers/aes/aes.c src/ciphers/aes/aes_tab.c
7272
$(LTCOMPILE) $(LTC_CFLAGS) $(CPPFLAGS) $(LTC_LDFLAGS) -DENCRYPT_ONLY -c src/ciphers/aes/aes.c -o src/ciphers/aes/aes_enc.o
73+
src/ciphers/aes/aes_enc_desc.o: src/ciphers/aes/aes_desc.c
74+
$(LTCOMPILE) $(LTC_CFLAGS) $(CPPFLAGS) $(LTC_LDFLAGS) -DENCRYPT_ONLY -c src/ciphers/aes/aes_desc.c -o src/ciphers/aes/aes_enc_desc.o
7375

7476
.c.o:
7577
$(LTCOMPILE) $(LTC_CFLAGS) $(CPPFLAGS) $(LTC_LDFLAGS) -o $@ -c $<

makefile.unix

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,8 @@ default: $(LIBMAIN_S)
256256
#SPECIAL: AES comes in two flavours - enc+dec and enc-only
257257
src/ciphers/aes/aes_enc.o: src/ciphers/aes/aes.c src/ciphers/aes/aes_tab.c
258258
$(CC) $(LTC_CFLAGS) -DENCRYPT_ONLY -c src/ciphers/aes/aes.c -o src/ciphers/aes/aes_enc.o
259+
src/ciphers/aes/aes_enc_desc.o: src/ciphers/aes/aes_desc.c
260+
$(CC) $(LTC_CFLAGS) -DENCRYPT_ONLY -c src/ciphers/aes/aes_desc.c -o src/ciphers/aes/aes_enc_desc.o
259261

260262
#SPECIAL: these are the rules to make certain object files
261263
src/ciphers/aes/aes.o: src/ciphers/aes/aes.c src/ciphers/aes/aes_tab.c

makefile_include.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,7 @@ src/hashes/sha2/sha512_224.o: src/hashes/sha2/sha512.c src/hashes/sha2/sha512_22
427427
src/hashes/sha2/sha512_256.o: src/hashes/sha2/sha512.c src/hashes/sha2/sha512_256.c
428428
src/hashes/whirl/whirl.o: src/hashes/whirl/whirl.c src/hashes/whirl/whirltab.c
429429

430+
430431
$(DOBJECTS): LTC_CFLAGS := -Itests $(LTC_CFLAGS)
431432
$(TOBJECTS): LTC_CFLAGS := -Itests $(LTC_CFLAGS)
432433

src/ciphers/aes/aes.c

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,6 @@ const struct ltc_cipher_descriptor rijndael_desc =
4444
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
4545
};
4646

47-
const struct ltc_cipher_descriptor aes_desc =
48-
{
49-
"aes",
50-
6,
51-
16, 32, 16, 10,
52-
SETUP, ECB_ENC, ECB_DEC, ECB_TEST, ECB_DONE, ECB_KS,
53-
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
54-
};
55-
5647
#else
5748

5849
#define SETUP rijndael_enc_setup
@@ -69,15 +60,6 @@ const struct ltc_cipher_descriptor rijndael_enc_desc =
6960
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
7061
};
7162

72-
const struct ltc_cipher_descriptor aes_enc_desc =
73-
{
74-
"aes",
75-
6,
76-
16, 32, 16, 10,
77-
SETUP, ECB_ENC, NULL, NULL, ECB_DONE, ECB_KS,
78-
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
79-
};
80-
8163
#endif
8264

8365
#define LTC_AES_TAB_C

src/ciphers/aes/aes_desc.c

Lines changed: 244 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
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

Comments
 (0)