Skip to content

Commit 1e629e6

Browse files
authored
Merge pull request #557 from libtom/add-aesni
Add AES-NI
2 parents 0173cac + d026c63 commit 1e629e6

22 files changed

+714
-45
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ jobs:
3838
- { BUILDNAME: 'VALGRIND', BUILDOPTIONS: '', BUILDSCRIPT: '.ci/valgrind.sh' }
3939
- { BUILDNAME: 'STOCK', BUILDOPTIONS: '', BUILDSCRIPT: '.ci/run.sh' }
4040
- { BUILDNAME: 'STOCK-MPI', BUILDOPTIONS: '-ULTM_DESC -UTFM_DESC -UUSE_LTM -UUSE_TFM', BUILDSCRIPT: '.ci/run.sh' }
41+
- { BUILDNAME: 'STOCK+AESNI', BUILDOPTIONS: '-msse4.1 -maes', BUILDSCRIPT: '.ci/run.sh' }
4142
- { BUILDNAME: 'EASY', BUILDOPTIONS: '-DLTC_EASY', BUILDSCRIPT: '.ci/run.sh' }
4243
- { BUILDNAME: 'SMALL', BUILDOPTIONS: '-DLTC_SMALL_CODE', BUILDSCRIPT: '.ci/run.sh' }
4344
- { BUILDNAME: 'NOTABLES', BUILDOPTIONS: '-DLTC_NO_TABLES', BUILDSCRIPT: '.ci/run.sh' }

doc/crypt.tex

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +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+
& 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) &&&&& \\
617621
\hline Twofish & twofish\_desc & 16 & 16, 24, 32 & 16 & 7 \\
618622
\hline DES & des\_desc & 8 & 8 & 16 & 13 \\
619623
\hline 3DES (EDE mode) & des3\_desc & 8 & 16, 24 & 16 & 14 \\
@@ -639,24 +643,30 @@ \subsection{Notes}
639643
\begin{small}
640644
\begin{enumerate}
641645
\item
642-
For AES, (also known as Rijndael) there are four descriptors which complicate issues a little. The descriptors
643-
rijndael\_desc and rijndael\_enc\_desc provide the cipher named \textit{rijndael}. The descriptors aes\_desc and
644-
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
645654
only difference is when you call find\_cipher() you have to pass the correct name. The cipher descriptors with \textit{enc}
646655
in the middle (e.g. rijndael\_enc\_desc) are related to an implementation of Rijndael with only the encryption routine
647656
and tables. The decryption and self--test function pointers of both \textit{encrypt only} descriptors are set to \textbf{NULL} and
648657
should not be called.
649658

650659
The \textit{encrypt only} descriptors are useful for applications that only use the encryption function of the cipher. Algorithms such
651-
as EAX, PMAC and OMAC only require the encryption function. So far this \textit{encrypt only} functionality has only been implemented for
652-
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.
653662

654663
\item
655664
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
656665
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
657666
byte string from the real 7/21 byte key.
658667

659-
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.
660670

661671
\item
662672
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;

libtomcrypt_VS2008.vcproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,10 @@
410410
RelativePath="src\ciphers\aes\aes.c"
411411
>
412412
</File>
413+
<File
414+
RelativePath="src\ciphers\aes\aes_desc.c"
415+
>
416+
</File>
413417
<File
414418
RelativePath="src\ciphers\aes\aes_tab.c"
415419
>
@@ -454,6 +458,10 @@
454458
/>
455459
</FileConfiguration>
456460
</File>
461+
<File
462+
RelativePath="src\ciphers\aes\aesni.c"
463+
>
464+
</File>
457465
</Filter>
458466
<Filter
459467
Name="safer"

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: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ LIBMAIN_I =libtomcrypt.dll.a
3535
LIBMAIN_D =libtomcrypt.dll
3636

3737
#List of objects to compile (all goes to libtomcrypt.a)
38-
OBJECTS=src/ciphers/aes/aes.o src/ciphers/aes/aes_enc.o src/ciphers/anubis.o src/ciphers/blowfish.o \
38+
OBJECTS=src/ciphers/aes/aes.o src/ciphers/aes/aes_desc.o src/ciphers/aes/aes_enc.o \
39+
src/ciphers/aes/aes_enc_desc.o src/ciphers/aes/aesni.o src/ciphers/anubis.o src/ciphers/blowfish.o \
3940
src/ciphers/camellia.o src/ciphers/cast5.o src/ciphers/des.o src/ciphers/idea.o src/ciphers/kasumi.o \
4041
src/ciphers/khazad.o src/ciphers/kseed.o src/ciphers/multi2.o src/ciphers/noekeon.o src/ciphers/rc2.o \
4142
src/ciphers/rc5.o src/ciphers/rc6.o src/ciphers/safer/safer.o src/ciphers/safer/saferp.o \
@@ -246,6 +247,8 @@ default: $(LIBMAIN_S)
246247
#SPECIAL: AES comes in two flavours - enc+dec and enc-only
247248
src/ciphers/aes/aes_enc.o: src/ciphers/aes/aes.c src/ciphers/aes/aes_tab.c
248249
$(CC) $(LTC_CFLAGS) -DENCRYPT_ONLY -c src/ciphers/aes/aes.c -o src/ciphers/aes/aes_enc.o
250+
src/ciphers/aes/aes_enc_desc.o: src/ciphers/aes/aes.c
251+
$(CC) $(LTC_CFLAGS) -DENCRYPT_ONLY -c src/ciphers/aes/aes_desc.c -o src/ciphers/aes/aes_enc_desc.o
249252

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

makefile.msvc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ VERSION=1.18.2-develop
2828
LIBMAIN_S =tomcrypt.lib
2929

3030
#List of objects to compile (all goes to tomcrypt.lib)
31-
OBJECTS=src/ciphers/aes/aes.obj src/ciphers/aes/aes_enc.obj src/ciphers/anubis.obj src/ciphers/blowfish.obj \
31+
OBJECTS=src/ciphers/aes/aes.obj src/ciphers/aes/aes_desc.obj src/ciphers/aes/aes_enc.obj \
32+
src/ciphers/aes/aes_enc_desc.obj src/ciphers/aes/aesni.obj src/ciphers/anubis.obj src/ciphers/blowfish.obj \
3233
src/ciphers/camellia.obj src/ciphers/cast5.obj src/ciphers/des.obj src/ciphers/idea.obj src/ciphers/kasumi.obj \
3334
src/ciphers/khazad.obj src/ciphers/kseed.obj src/ciphers/multi2.obj src/ciphers/noekeon.obj src/ciphers/rc2.obj \
3435
src/ciphers/rc5.obj src/ciphers/rc6.obj src/ciphers/safer/safer.obj src/ciphers/safer/saferp.obj \
@@ -239,6 +240,8 @@ default: $(LIBMAIN_S)
239240
#SPECIAL: AES comes in two flavours - enc+dec and enc-only
240241
src/ciphers/aes/aes_enc.obj: src/ciphers/aes/aes.c src/ciphers/aes/aes_tab.c
241242
$(CC) $(LTC_CFLAGS) /DENCRYPT_ONLY /c src/ciphers/aes/aes.c /Fosrc/ciphers/aes/aes_enc.obj
243+
src/ciphers/aes/aes_enc_desc.obj: src/ciphers/aes/aes_desc.c
244+
$(CC) $(LTC_CFLAGS) /DENCRYPT_ONLY /c src/ciphers/aes/aes_desc.c /Fosrc/ciphers/aes/aes_enc_desc.obj
242245

243246
#SPECIAL: these are the rules to make certain object files
244247
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: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ VERSION=1.18.2-develop
4545
LIBMAIN_S =libtomcrypt.a
4646

4747
#List of objects to compile (all goes to libtomcrypt.a)
48-
OBJECTS=src/ciphers/aes/aes.o src/ciphers/aes/aes_enc.o src/ciphers/anubis.o src/ciphers/blowfish.o \
48+
OBJECTS=src/ciphers/aes/aes.o src/ciphers/aes/aes_desc.o src/ciphers/aes/aes_enc.o \
49+
src/ciphers/aes/aes_enc_desc.o src/ciphers/aes/aesni.o src/ciphers/anubis.o src/ciphers/blowfish.o \
4950
src/ciphers/camellia.o src/ciphers/cast5.o src/ciphers/des.o src/ciphers/idea.o src/ciphers/kasumi.o \
5051
src/ciphers/khazad.o src/ciphers/kseed.o src/ciphers/multi2.o src/ciphers/noekeon.o src/ciphers/rc2.o \
5152
src/ciphers/rc5.o src/ciphers/rc6.o src/ciphers/safer/safer.o src/ciphers/safer/saferp.o \
@@ -256,6 +257,8 @@ default: $(LIBMAIN_S)
256257
#SPECIAL: AES comes in two flavours - enc+dec and enc-only
257258
src/ciphers/aes/aes_enc.o: src/ciphers/aes/aes.c src/ciphers/aes/aes_tab.c
258259
$(CC) $(LTC_CFLAGS) -DENCRYPT_ONLY -c src/ciphers/aes/aes.c -o src/ciphers/aes/aes_enc.o
260+
src/ciphers/aes/aes_enc_desc.o: src/ciphers/aes/aes_desc.c
261+
$(CC) $(LTC_CFLAGS) -DENCRYPT_ONLY -c src/ciphers/aes/aes_desc.c -o src/ciphers/aes/aes_enc_desc.o
259262

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

makefile_include.mk

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ endif # LTC_SMALL
126126

127127
ifneq ($(findstring clang,$(CC)),)
128128
LTC_CFLAGS += -Wno-typedef-redefinition -Wno-tautological-compare -Wno-builtin-requires-header
129-
LTC_CFLAGS += -Wno-missing-field-initializers -Wno-missing-braces -Wno-incomplete-setjmp-declaration
129+
LTC_CFLAGS += -Wno-missing-field-initializers -Wno-missing-braces -Wno-incomplete-setjmp-declaration -Wno-cast-align
130+
LTC_CFLAGS += -Wno-declaration-after-statement
130131
endif
131132
ifneq ($(findstring mingw,$(CC)),)
132133
LTC_CFLAGS += -Wno-shadow -Wno-attributes
@@ -212,7 +213,8 @@ library: $(call print-help,library,Builds the library) $(LIBNAME)
212213

213214

214215
# List of objects to compile (all goes to libtomcrypt.a)
215-
OBJECTS=src/ciphers/aes/aes.o src/ciphers/aes/aes_enc.o src/ciphers/anubis.o src/ciphers/blowfish.o \
216+
OBJECTS=src/ciphers/aes/aes.o src/ciphers/aes/aes_desc.o src/ciphers/aes/aes_enc.o \
217+
src/ciphers/aes/aes_enc_desc.o src/ciphers/aes/aesni.o src/ciphers/anubis.o src/ciphers/blowfish.o \
216218
src/ciphers/camellia.o src/ciphers/cast5.o src/ciphers/des.o src/ciphers/idea.o src/ciphers/kasumi.o \
217219
src/ciphers/khazad.o src/ciphers/kseed.o src/ciphers/multi2.o src/ciphers/noekeon.o src/ciphers/rc2.o \
218220
src/ciphers/rc5.o src/ciphers/rc6.o src/ciphers/safer/safer.o src/ciphers/safer/saferp.o \
@@ -426,6 +428,7 @@ src/hashes/sha2/sha512_224.o: src/hashes/sha2/sha512.c src/hashes/sha2/sha512_22
426428
src/hashes/sha2/sha512_256.o: src/hashes/sha2/sha512.c src/hashes/sha2/sha512_256.c
427429
src/hashes/whirl/whirl.o: src/hashes/whirl/whirl.c src/hashes/whirl/whirltab.c
428430

431+
429432
$(DOBJECTS): LTC_CFLAGS := -Itests $(LTC_CFLAGS)
430433
$(TOBJECTS): LTC_CFLAGS := -Itests $(LTC_CFLAGS)
431434

0 commit comments

Comments
 (0)