Skip to content

Commit 0a8ea9a

Browse files
committed
Fix for SIGILL crash under Linux without AVX support
In wxSQLite3 issue utelle/wxsqlite3#126 a crash under Linux without AVX support was reported. The GCC compiler generated invalid instructions for the software implementation of the AEGIS algorithm. The issue could be solved by re-arranging the AEGIS code blocks, so that blocks using the same hardware feature are now grouped together. Test databases for the ciphers AEGIS and Ascon128 were added.
1 parent 7aab546 commit 0a8ea9a

File tree

7 files changed

+78
-43
lines changed

7 files changed

+78
-43
lines changed

.github/workflows/ci4sqlite3mc.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ jobs:
3636
run: |
3737
./sqlite3shell test1.db3 ".read test/test1.sql"
3838
./sqlite3shell test2.db3 ".read test/test2.sql"
39+
./sqlite3shell test/persons-aegis-testkey.db3 ".read test/test3.sql"
40+
./sqlite3shell test/persons-ascon128-testkey.db3 ".read test/test4.sql"
3941
./sqlite3shell dummy.db3 ".read test/sqlciphertest.sql"
4042
4143
host_qemu:
@@ -65,4 +67,6 @@ jobs:
6567
make
6668
./sqlite3shell test1.db3 ".read test/test1.sql"
6769
./sqlite3shell test2.db3 ".read test/test2.sql"
70+
./sqlite3shell test/persons-aegis-testkey.db3 ".read test/test3.sql"
71+
./sqlite3shell test/persons-ascon128-testkey.db3 ".read test/test4.sql"
6872
./sqlite3shell dummy.db3 ".read test/sqlciphertest.sql"

src/aegis/libaegis.c

Lines changed: 45 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
** Name: libaegis.c
33
** Purpose: Amalgamation of the AEGIS library
4-
** Copyright: (c) 2024-2024 Ulrich Telle
4+
** Copyright: (c) 2024-2025 Ulrich Telle
55
** SPDX-License-Identifier: MIT
66
*/
77

@@ -27,58 +27,62 @@
2727
# pragma GCC push_options
2828
#endif
2929

30-
/* AEGIS 128 L */
30+
/* Variants of implementation headers */
3131
#include "aegis128l/implementations.h"
32-
#include "aegis128l/aegis128l_aesni.c"
33-
#include "aegis128l/aegis128l_altivec.c"
34-
#include "aegis128l/aegis128l_armcrypto.c"
35-
#include "aegis128l/aegis128l_soft.c"
36-
#include "aegis128l/aegis128l.c"
37-
38-
/* AEGIS 128 x2 */
3932
#include "aegis128x2/implementations.h"
40-
#include "aegis128x2/aegis128x2_aesni.c"
41-
#include "aegis128x2/aegis128x2_altivec.c"
42-
#include "aegis128x2/aegis128x2_armcrypto.c"
43-
#include "aegis128x2/aegis128x2_avx2.c"
33+
#include "aegis128x4/implementations.h"
34+
#include "aegis256/implementations.h"
35+
#include "aegis256x2/implementations.h"
36+
#include "aegis256x4/implementations.h"
37+
38+
/* Variants without hardware acceleration */
39+
#include "aegis128l/aegis128l_soft.c"
4440
#include "aegis128x2/aegis128x2_soft.c"
45-
#include "aegis128x2/aegis128x2.c"
41+
#include "aegis128x4/aegis128x4_soft.c"
42+
#include "aegis256/aegis256_soft.c"
43+
#include "aegis256x2/aegis256x2_soft.c"
44+
#include "aegis256x4/aegis256x4_soft.c"
4645

47-
/* AEGIS 128 x4 */
48-
#include "aegis128x4/implementations.h"
46+
/* Variants with support for AES and AVX instruction sets */
47+
#include "aegis128l/aegis128l_aesni.c"
48+
#include "aegis128x2/aegis128x2_aesni.c"
4949
#include "aegis128x4/aegis128x4_aesni.c"
50-
#include "aegis128x4/aegis128x4_altivec.c"
51-
#include "aegis128x4/aegis128x4_armcrypto.c"
50+
#include "aegis256/aegis256_aesni.c"
51+
#include "aegis256x2/aegis256x2_aesni.c"
52+
#include "aegis256x4/aegis256x4_aesni.c"
53+
54+
/* Variants with support for VAES and AVX2 instruction sets */
55+
#include "aegis128x2/aegis128x2_avx2.c"
5256
#include "aegis128x4/aegis128x4_avx2.c"
57+
#include "aegis256x2/aegis256x2_avx2.c"
58+
#include "aegis256x4/aegis256x4_avx2.c"
59+
60+
/* Variants with support for AVX512F instruction sets */
5361
#include "aegis128x4/aegis128x4_avx512.c"
54-
#include "aegis128x4/aegis128x4_soft.c"
55-
#include "aegis128x4/aegis128x4.c"
62+
#include "aegis256x4/aegis256x4_avx512.c"
5663

57-
/* AEGIS 256 */
58-
#include "aegis256/implementations.h"
59-
#include "aegis256/aegis256_aesni.c"
64+
/* Variants with support for AltiVec instruction sets */
65+
#include "aegis128l/aegis128l_altivec.c"
66+
#include "aegis128x2/aegis128x2_altivec.c"
67+
#include "aegis128x4/aegis128x4_altivec.c"
6068
#include "aegis256/aegis256_altivec.c"
61-
#include "aegis256/aegis256_armcrypto.c"
62-
#include "aegis256/aegis256_soft.c"
63-
#include "aegis256/aegis256.c"
64-
65-
/* AEGIS 256 x2 */
66-
#include "aegis256x2/implementations.h"
67-
#include "aegis256x2/aegis256x2_aesni.c"
6869
#include "aegis256x2/aegis256x2_altivec.c"
69-
#include "aegis256x2/aegis256x2_armcrypto.c"
70-
#include "aegis256x2/aegis256x2_avx2.c"
71-
#include "aegis256x2/aegis256x2_soft.c"
72-
#include "aegis256x2/aegis256x2.c"
73-
74-
/* AEGIS 256 x4 */
75-
#include "aegis256x4/implementations.h"
76-
#include "aegis256x4/aegis256x4_aesni.c"
7770
#include "aegis256x4/aegis256x4_altivec.c"
71+
72+
/* Variants with support for ARM Neon instruction sets */
73+
#include "aegis128l/aegis128l_armcrypto.c"
74+
#include "aegis128x2/aegis128x2_armcrypto.c"
75+
#include "aegis128x4/aegis128x4_armcrypto.c"
76+
#include "aegis256/aegis256_armcrypto.c"
77+
#include "aegis256x2/aegis256x2_armcrypto.c"
7878
#include "aegis256x4/aegis256x4_armcrypto.c"
79-
#include "aegis256x4/aegis256x4_avx2.c"
80-
#include "aegis256x4/aegis256x4_avx512.c"
81-
#include "aegis256x4/aegis256x4_soft.c"
79+
80+
/* Control functions for the AEGIS variants */
81+
#include "aegis128l/aegis128l.c"
82+
#include "aegis128x2/aegis128x2.c"
83+
#include "aegis128x4/aegis128x4.c"
84+
#include "aegis256/aegis256.c"
85+
#include "aegis256x2/aegis256x2.c"
8286
#include "aegis256x4/aegis256x4.c"
8387

8488
#if defined(__GNUC__)

src/aes_hardware.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@
8181

8282
#endif /* SQLITE3MC_OMIT_AES_HARDWARE_SUPPORT */
8383

84+
#if defined(__GNUC__)
85+
#pragma GCC push_options
86+
#endif
8487

8588
#if HAS_AES_HARDWARE != AES_HARDWARE_NONE
8689
/* --- Implementation of common data and functions for any AES hardware --- */
@@ -126,9 +129,9 @@ toUint32FromLE(const void* buffer)
126129

127130
/* Define SQLITE3MC_FUNC_ISA */
128131
#if SQLITE3MC_COMPILER_HAS_ATTRIBUTE(target)
129-
#define SQLITE3MC_FUNC_ISA(isa) SQLITE3MC_COMPILER_ATTRIBUTE(target(isa))
132+
#define SQLITE3MC_FUNC_ISA(isa) SQLITE3MC_COMPILER_ATTRIBUTE(target(isa))
130133
#else
131-
#define SQLITE3MC_FUNC_ISA(isa)
134+
#define SQLITE3MC_FUNC_ISA(isa)
132135
#endif
133136

134137
/* Define SQLITE3MC_FUNC_ISA_INLINE */
@@ -168,9 +171,17 @@ aesHardwareCheck()
168171

169172
#endif /* defined(__clang__) || defined(__GNUC__) */
170173

174+
#if defined(__GNUC__)
175+
#pragma GCC push_options
176+
#endif
177+
171178
#include <wmmintrin.h>
172179
#include <smmintrin.h>
173180

181+
#if defined(__GNUC__)
182+
#pragma GCC pop_options
183+
#endif
184+
174185
SQLITE3MC_FUNC_ISA("sse4.2,aes")
175186
static int
176187
aesGenKeyEncryptInternal(const unsigned char* userKey, const int bits, __m128i* keyData)
@@ -800,6 +811,10 @@ aesHardwareCheck()
800811

801812
#endif
802813

814+
#if defined(__GNUC__)
815+
#pragma GCC pop_options
816+
#endif
817+
803818
/*
804819
** The top-level selection function, caching the results of
805820
** aesHardwareCheck() so it only has to run once.

test/persons-aegis-testkey.db3

32 KB
Binary file not shown.

test/persons-ascon128-testkey.db3

32 KB
Binary file not shown.

test/test3.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.echo on
2+
PRAGMA cipher='aegis';
3+
PRAGMA key='testkey';
4+
SELECT COUNT(*) FROM persons;
5+
SELECT DISTINCT lastname, firstname FROM persons WHERE city='Rom';
6+
.q

test/test4.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.echo on
2+
PRAGMA cipher='ascon128';
3+
PRAGMA key='testkey';
4+
SELECT COUNT(*) FROM persons;
5+
SELECT DISTINCT lastname, firstname FROM persons WHERE city='Rom';
6+
.q

0 commit comments

Comments
 (0)