pq_crypto-seal 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.github/workflows/ci.yml +66 -0
- data/CHANGELOG.md +44 -0
- data/FORMAT.md +139 -0
- data/GET_STARTED.md +30 -0
- data/Gemfile +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +215 -0
- data/RELEASING.md +34 -0
- data/Rakefile +19 -0
- data/SECURITY.md +85 -0
- data/VENDORING.md +14 -0
- data/ext/pq_crypto_seal/aegis_unused_stubs.c +20 -0
- data/ext/pq_crypto_seal/extconf.rb +296 -0
- data/ext/pq_crypto_seal/pq_crypto_seal.c +390 -0
- data/ext/pq_crypto_seal/vendor/libaegis/LICENSE +21 -0
- data/ext/pq_crypto_seal/vendor/libaegis/TREE_SHA256 +1 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128l/aegis128l.c +246 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128l/aegis128l_aesni.c +73 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128l/aegis128l_aesni.h +9 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128l/aegis128l_altivec.c +70 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128l/aegis128l_altivec.h +9 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128l/aegis128l_common.h +691 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128l/aegis128l_neon_aes.c +77 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128l/aegis128l_neon_aes.h +9 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128l/aegis128l_neon_sha3.c +86 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128l/aegis128l_neon_sha3.h +9 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128l/aegis128l_soft.c +63 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128l/aegis128l_soft.h +9 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128l/implementations.h +40 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x2/aegis128x2.c +247 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x2/aegis128x2_aesni.c +108 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x2/aegis128x2_aesni.h +9 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x2/aegis128x2_altivec.c +103 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x2/aegis128x2_altivec.h +9 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x2/aegis128x2_avx2.c +79 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x2/aegis128x2_avx2.h +11 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x2/aegis128x2_common.h +795 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x2/aegis128x2_neon_aes.c +111 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x2/aegis128x2_neon_aes.h +9 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x2/aegis128x2_soft.c +96 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x2/aegis128x2_soft.h +9 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x2/implementations.h +40 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x4/aegis128x4.c +252 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x4/aegis128x4_aesni.c +117 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x4/aegis128x4_aesni.h +9 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x4/aegis128x4_altivec.c +111 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x4/aegis128x4_altivec.h +9 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x4/aegis128x4_avx2.c +112 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x4/aegis128x4_avx2.h +11 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x4/aegis128x4_avx512.c +85 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x4/aegis128x4_avx512.h +11 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x4/aegis128x4_common.h +808 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x4/aegis128x4_neon_aes.c +119 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x4/aegis128x4_neon_aes.h +9 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x4/aegis128x4_soft.c +104 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x4/aegis128x4_soft.h +9 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x4/implementations.h +41 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256/aegis256.c +240 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256/aegis256_aesni.c +68 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256/aegis256_aesni.h +9 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256/aegis256_altivec.c +68 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256/aegis256_altivec.h +9 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256/aegis256_common.h +673 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256/aegis256_neon_aes.c +75 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256/aegis256_neon_aes.h +9 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256/aegis256_soft.c +58 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256/aegis256_soft.h +9 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256/implementations.h +39 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x2/aegis256x2.c +247 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x2/aegis256x2_aesni.c +103 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x2/aegis256x2_aesni.h +9 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x2/aegis256x2_altivec.c +101 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x2/aegis256x2_altivec.h +9 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x2/aegis256x2_avx2.c +74 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x2/aegis256x2_avx2.h +11 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x2/aegis256x2_common.h +790 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x2/aegis256x2_neon_aes.c +109 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x2/aegis256x2_neon_aes.h +9 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x2/aegis256x2_soft.c +91 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x2/aegis256x2_soft.h +9 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x2/implementations.h +41 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x4/aegis256x4.c +252 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x4/aegis256x4_aesni.c +112 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x4/aegis256x4_aesni.h +9 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x4/aegis256x4_altivec.c +109 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x4/aegis256x4_altivec.h +9 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x4/aegis256x4_avx2.c +107 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x4/aegis256x4_avx2.h +11 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x4/aegis256x4_avx512.c +79 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x4/aegis256x4_avx512.h +11 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x4/aegis256x4_common.h +805 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x4/aegis256x4_neon_aes.c +117 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x4/aegis256x4_neon_aes.h +9 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x4/aegis256x4_soft.c +99 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x4/aegis256x4_soft.h +9 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x4/implementations.h +42 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/common/common.c +81 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/common/common.h +144 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/common/cpu.c +378 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/common/cpu.h +34 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/common/keccak.c +187 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/common/keccak.h +13 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/common/softaes.c +338 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/common/softaes.h +69 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/include/aegis.h +77 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/include/aegis128l.h +311 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/include/aegis128x2.h +313 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/include/aegis128x4.h +313 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/include/aegis256.h +311 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/include/aegis256x2.h +313 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/include/aegis256x4.h +313 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/include/aegis_raf.h +580 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/raf/raf.c +87 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/raf/raf_aegis128l.c +20 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/raf/raf_aegis128x2.c +20 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/raf/raf_aegis128x4.c +20 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/raf/raf_aegis256.c +20 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/raf/raf_aegis256x2.c +20 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/raf/raf_aegis256x4.c +20 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/raf/raf_internal.h +152 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/raf/raf_merkle.c +268 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/raf/raf_merkle.h +165 -0
- data/ext/pq_crypto_seal/vendor/libaegis/src/raf/raf_variant.h +1119 -0
- data/fuzz/decrypt_fuzz.rb +119 -0
- data/lib/pq_crypto/seal/binary.rb +62 -0
- data/lib/pq_crypto/seal/core.rb +398 -0
- data/lib/pq_crypto/seal/errors.rb +15 -0
- data/lib/pq_crypto/seal/format.rb +275 -0
- data/lib/pq_crypto/seal/io.rb +336 -0
- data/lib/pq_crypto/seal/native.rb +13 -0
- data/lib/pq_crypto/seal/padding.rb +47 -0
- data/lib/pq_crypto/seal/version.rb +6 -0
- data/lib/pq_crypto/seal.rb +32 -0
- data/pq_crypto-seal.gemspec +37 -0
- data/script/vendor_libs.rb +79 -0
- data/test/aegis_vectors_test.rb +54 -0
- data/test/golden_vectors_test.rb +183 -0
- data/test/io_test.rb +125 -0
- data/test/native_test.rb +36 -0
- data/test/seal_test.rb +238 -0
- data/test/test_helper.rb +6 -0
- metadata +244 -0
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
#if defined(__aarch64__) || defined(_M_ARM64)
|
|
2
|
+
|
|
3
|
+
# include <stddef.h>
|
|
4
|
+
# include <stdint.h>
|
|
5
|
+
|
|
6
|
+
# include "../common/common.h"
|
|
7
|
+
# include "aegis256x4.h"
|
|
8
|
+
# include "aegis256x4_neon_aes.h"
|
|
9
|
+
|
|
10
|
+
# ifndef __ARM_FEATURE_CRYPTO
|
|
11
|
+
# define __ARM_FEATURE_CRYPTO 1
|
|
12
|
+
# endif
|
|
13
|
+
# ifndef __ARM_FEATURE_AES
|
|
14
|
+
# define __ARM_FEATURE_AES 1
|
|
15
|
+
# endif
|
|
16
|
+
|
|
17
|
+
# include <arm_neon.h>
|
|
18
|
+
|
|
19
|
+
# ifdef __clang__
|
|
20
|
+
# pragma clang attribute push(__attribute__((target("neon,crypto,aes"))), \
|
|
21
|
+
apply_to = function)
|
|
22
|
+
# elif defined(__GNUC__)
|
|
23
|
+
# pragma GCC target("+simd+crypto")
|
|
24
|
+
# endif
|
|
25
|
+
|
|
26
|
+
# define AES_BLOCK_LENGTH 64
|
|
27
|
+
|
|
28
|
+
typedef struct {
|
|
29
|
+
uint8x16_t b0;
|
|
30
|
+
uint8x16_t b1;
|
|
31
|
+
uint8x16_t b2;
|
|
32
|
+
uint8x16_t b3;
|
|
33
|
+
} aes_block_t;
|
|
34
|
+
|
|
35
|
+
static inline aes_block_t
|
|
36
|
+
AES_BLOCK_XOR(const aes_block_t a, const aes_block_t b)
|
|
37
|
+
{
|
|
38
|
+
return (aes_block_t) { veorq_u8(a.b0, b.b0), veorq_u8(a.b1, b.b1), veorq_u8(a.b2, b.b2),
|
|
39
|
+
veorq_u8(a.b3, b.b3) };
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
static inline aes_block_t
|
|
43
|
+
AES_BLOCK_AND(const aes_block_t a, const aes_block_t b)
|
|
44
|
+
{
|
|
45
|
+
return (aes_block_t) { vandq_u8(a.b0, b.b0), vandq_u8(a.b1, b.b1), vandq_u8(a.b2, b.b2),
|
|
46
|
+
vandq_u8(a.b3, b.b3) };
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
static inline aes_block_t
|
|
50
|
+
AES_BLOCK_LOAD(const uint8_t *a)
|
|
51
|
+
{
|
|
52
|
+
return (aes_block_t) { vld1q_u8(a), vld1q_u8(a + 16), vld1q_u8(a + 32), vld1q_u8(a + 48) };
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
static inline aes_block_t
|
|
56
|
+
AES_BLOCK_LOAD_64x2(uint64_t a, uint64_t b)
|
|
57
|
+
{
|
|
58
|
+
const uint8x16_t t = vreinterpretq_u8_u64(vsetq_lane_u64((a), vmovq_n_u64(b), 1));
|
|
59
|
+
return (aes_block_t) { t, t, t, t };
|
|
60
|
+
}
|
|
61
|
+
static inline void
|
|
62
|
+
AES_BLOCK_STORE(uint8_t *a, const aes_block_t b)
|
|
63
|
+
{
|
|
64
|
+
vst1q_u8(a, b.b0);
|
|
65
|
+
vst1q_u8(a + 16, b.b1);
|
|
66
|
+
vst1q_u8(a + 32, b.b2);
|
|
67
|
+
vst1q_u8(a + 48, b.b3);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
static inline aes_block_t
|
|
71
|
+
AES_ENC(const aes_block_t a, const aes_block_t b)
|
|
72
|
+
{
|
|
73
|
+
return (aes_block_t) { veorq_u8(vaesmcq_u8(vaeseq_u8(vmovq_n_u8(0), a.b0)), b.b0),
|
|
74
|
+
veorq_u8(vaesmcq_u8(vaeseq_u8(vmovq_n_u8(0), a.b1)), b.b1),
|
|
75
|
+
veorq_u8(vaesmcq_u8(vaeseq_u8(vmovq_n_u8(0), a.b2)), b.b2),
|
|
76
|
+
veorq_u8(vaesmcq_u8(vaeseq_u8(vmovq_n_u8(0), a.b3)), b.b3) };
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
static inline void
|
|
80
|
+
aegis256x4_update(aes_block_t *const state, const aes_block_t d)
|
|
81
|
+
{
|
|
82
|
+
aes_block_t tmp;
|
|
83
|
+
|
|
84
|
+
tmp = state[5];
|
|
85
|
+
state[5] = AES_ENC(state[4], state[5]);
|
|
86
|
+
state[4] = AES_ENC(state[3], state[4]);
|
|
87
|
+
state[3] = AES_ENC(state[2], state[3]);
|
|
88
|
+
state[2] = AES_ENC(state[1], state[2]);
|
|
89
|
+
state[1] = AES_ENC(state[0], state[1]);
|
|
90
|
+
state[0] = AES_BLOCK_XOR(AES_ENC(tmp, state[0]), d);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
# include "aegis256x4_common.h"
|
|
94
|
+
|
|
95
|
+
struct aegis256x4_implementation aegis256x4_neon_aes_implementation = {
|
|
96
|
+
.encrypt_detached = encrypt_detached,
|
|
97
|
+
.decrypt_detached = decrypt_detached,
|
|
98
|
+
.encrypt_unauthenticated = encrypt_unauthenticated,
|
|
99
|
+
.decrypt_unauthenticated = decrypt_unauthenticated,
|
|
100
|
+
.stream = stream,
|
|
101
|
+
.state_init = state_init,
|
|
102
|
+
.state_encrypt_update = state_encrypt_update,
|
|
103
|
+
.state_encrypt_final = state_encrypt_final,
|
|
104
|
+
.state_decrypt_update = state_decrypt_update,
|
|
105
|
+
.state_decrypt_final = state_decrypt_final,
|
|
106
|
+
.state_mac_init = state_mac_init,
|
|
107
|
+
.state_mac_update = state_mac_update,
|
|
108
|
+
.state_mac_final = state_mac_final,
|
|
109
|
+
.state_mac_reset = state_mac_reset,
|
|
110
|
+
.state_mac_clone = state_mac_clone,
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
# ifdef __clang__
|
|
114
|
+
# pragma clang attribute pop
|
|
115
|
+
# endif
|
|
116
|
+
|
|
117
|
+
#endif
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
#include <stddef.h>
|
|
2
|
+
#include <stdint.h>
|
|
3
|
+
|
|
4
|
+
#include "../common/common.h"
|
|
5
|
+
#include "../common/cpu.h"
|
|
6
|
+
|
|
7
|
+
#ifndef HAS_HW_AES
|
|
8
|
+
|
|
9
|
+
# include "../common/softaes.h"
|
|
10
|
+
# include "aegis256x4.h"
|
|
11
|
+
# include "aegis256x4_soft.h"
|
|
12
|
+
|
|
13
|
+
# define AES_BLOCK_LENGTH 64
|
|
14
|
+
|
|
15
|
+
typedef struct {
|
|
16
|
+
SoftAesBlock b0;
|
|
17
|
+
SoftAesBlock b1;
|
|
18
|
+
SoftAesBlock b2;
|
|
19
|
+
SoftAesBlock b3;
|
|
20
|
+
} aes_block_t;
|
|
21
|
+
|
|
22
|
+
static inline aes_block_t
|
|
23
|
+
AES_BLOCK_XOR(const aes_block_t a, const aes_block_t b)
|
|
24
|
+
{
|
|
25
|
+
return (aes_block_t) { softaes_block_xor(a.b0, b.b0), softaes_block_xor(a.b1, b.b1),
|
|
26
|
+
softaes_block_xor(a.b2, b.b2), softaes_block_xor(a.b3, b.b3) };
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
static inline aes_block_t
|
|
30
|
+
AES_BLOCK_AND(const aes_block_t a, const aes_block_t b)
|
|
31
|
+
{
|
|
32
|
+
return (aes_block_t) { softaes_block_and(a.b0, b.b0), softaes_block_and(a.b1, b.b1),
|
|
33
|
+
softaes_block_and(a.b2, b.b2), softaes_block_and(a.b3, b.b3) };
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
static inline aes_block_t
|
|
37
|
+
AES_BLOCK_LOAD(const uint8_t *a)
|
|
38
|
+
{
|
|
39
|
+
return (aes_block_t) { softaes_block_load(a), softaes_block_load(a + 16),
|
|
40
|
+
softaes_block_load(a + 32), softaes_block_load(a + 48) };
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
static inline aes_block_t
|
|
44
|
+
AES_BLOCK_LOAD_64x2(uint64_t a, uint64_t b)
|
|
45
|
+
{
|
|
46
|
+
const SoftAesBlock t = softaes_block_load64x2(a, b);
|
|
47
|
+
return (aes_block_t) { t, t, t, t };
|
|
48
|
+
}
|
|
49
|
+
static inline void
|
|
50
|
+
AES_BLOCK_STORE(uint8_t *a, const aes_block_t b)
|
|
51
|
+
{
|
|
52
|
+
softaes_block_store(a, b.b0);
|
|
53
|
+
softaes_block_store(a + 16, b.b1);
|
|
54
|
+
softaes_block_store(a + 32, b.b2);
|
|
55
|
+
softaes_block_store(a + 48, b.b3);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
static inline aes_block_t
|
|
59
|
+
AES_ENC(const aes_block_t a, const aes_block_t b)
|
|
60
|
+
{
|
|
61
|
+
return (aes_block_t) { softaes_block_encrypt(a.b0, b.b0), softaes_block_encrypt(a.b1, b.b1),
|
|
62
|
+
softaes_block_encrypt(a.b2, b.b2), softaes_block_encrypt(a.b3, b.b3) };
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
static inline void
|
|
66
|
+
aegis256x4_update(aes_block_t *const state, const aes_block_t d)
|
|
67
|
+
{
|
|
68
|
+
aes_block_t tmp;
|
|
69
|
+
|
|
70
|
+
tmp = state[5];
|
|
71
|
+
state[5] = AES_ENC(state[4], state[5]);
|
|
72
|
+
state[4] = AES_ENC(state[3], state[4]);
|
|
73
|
+
state[3] = AES_ENC(state[2], state[3]);
|
|
74
|
+
state[2] = AES_ENC(state[1], state[2]);
|
|
75
|
+
state[1] = AES_ENC(state[0], state[1]);
|
|
76
|
+
state[0] = AES_BLOCK_XOR(AES_ENC(tmp, state[0]), d);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
# include "aegis256x4_common.h"
|
|
80
|
+
|
|
81
|
+
struct aegis256x4_implementation aegis256x4_soft_implementation = {
|
|
82
|
+
.encrypt_detached = encrypt_detached,
|
|
83
|
+
.decrypt_detached = decrypt_detached,
|
|
84
|
+
.encrypt_unauthenticated = encrypt_unauthenticated,
|
|
85
|
+
.decrypt_unauthenticated = decrypt_unauthenticated,
|
|
86
|
+
.stream = stream,
|
|
87
|
+
.state_init = state_init,
|
|
88
|
+
.state_encrypt_update = state_encrypt_update,
|
|
89
|
+
.state_encrypt_final = state_encrypt_final,
|
|
90
|
+
.state_decrypt_update = state_decrypt_update,
|
|
91
|
+
.state_decrypt_final = state_decrypt_final,
|
|
92
|
+
.state_mac_init = state_mac_init,
|
|
93
|
+
.state_mac_update = state_mac_update,
|
|
94
|
+
.state_mac_final = state_mac_final,
|
|
95
|
+
.state_mac_reset = state_mac_reset,
|
|
96
|
+
.state_mac_clone = state_mac_clone,
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
#endif
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
#ifndef aegis256x4_implementations_H
|
|
2
|
+
#define aegis256x4_implementations_H
|
|
3
|
+
|
|
4
|
+
#include <stddef.h>
|
|
5
|
+
#include <stdint.h>
|
|
6
|
+
|
|
7
|
+
#include "aegis256x4.h"
|
|
8
|
+
|
|
9
|
+
/* Namespacing to avoid conflicts with libsodium */
|
|
10
|
+
#define aegis256x4_soft_implementation libaegis_aegis256x4_soft_implementation
|
|
11
|
+
#define aegis256x4_aesni_implementation libaegis_aegis256x4_aesni_implementation
|
|
12
|
+
#define aegis256x4_neon_aes_implementation libaegis_aegis256x4_neon_aes_implementation
|
|
13
|
+
#define aegis256x4_altivec_implementation libaegis_aegis256x4_altivec_implementation
|
|
14
|
+
#define aegis256x4_avx2_implementation libaegis_aegis256x4_avx2_implementation
|
|
15
|
+
#define aegis256x4_avx512_implementation libaegis_aegis256x4_avx512_implementation
|
|
16
|
+
|
|
17
|
+
typedef struct aegis256x4_implementation {
|
|
18
|
+
int (*encrypt_detached)(uint8_t *c, uint8_t *mac, size_t maclen, const uint8_t *m, size_t mlen,
|
|
19
|
+
const uint8_t *ad, size_t adlen, const uint8_t *npub, const uint8_t *k);
|
|
20
|
+
int (*decrypt_detached)(uint8_t *m, const uint8_t *c, size_t clen, const uint8_t *mac,
|
|
21
|
+
size_t maclen, const uint8_t *ad, size_t adlen, const uint8_t *npub,
|
|
22
|
+
const uint8_t *k);
|
|
23
|
+
void (*stream)(uint8_t *out, size_t len, const uint8_t *npub, const uint8_t *k);
|
|
24
|
+
void (*encrypt_unauthenticated)(uint8_t *c, const uint8_t *m, size_t mlen, const uint8_t *npub,
|
|
25
|
+
const uint8_t *k);
|
|
26
|
+
void (*decrypt_unauthenticated)(uint8_t *m, const uint8_t *c, size_t clen, const uint8_t *npub,
|
|
27
|
+
const uint8_t *k);
|
|
28
|
+
void (*state_init)(aegis256x4_state *st_, const uint8_t *ad, size_t adlen, const uint8_t *npub,
|
|
29
|
+
const uint8_t *k);
|
|
30
|
+
int (*state_encrypt_update)(aegis256x4_state *st_, uint8_t *c, const uint8_t *m, size_t mlen);
|
|
31
|
+
int (*state_encrypt_final)(aegis256x4_state *st_, uint8_t *mac, size_t maclen);
|
|
32
|
+
int (*state_decrypt_update)(aegis256x4_state *st_, uint8_t *m, const uint8_t *c, size_t clen);
|
|
33
|
+
int (*state_decrypt_final)(aegis256x4_state *st_, const uint8_t *mac, size_t maclen);
|
|
34
|
+
void (*state_mac_init)(aegis256x4_mac_state *st_, const uint8_t *npub, const uint8_t *k);
|
|
35
|
+
int (*state_mac_update)(aegis256x4_mac_state *st_, const uint8_t *ad, size_t adlen);
|
|
36
|
+
int (*state_mac_final)(aegis256x4_mac_state *st_, uint8_t *mac, size_t maclen);
|
|
37
|
+
void (*state_mac_reset)(aegis256x4_mac_state *st);
|
|
38
|
+
void (*state_mac_clone)(aegis256x4_mac_state *dst, const aegis256x4_mac_state *src);
|
|
39
|
+
|
|
40
|
+
} aegis256x4_implementation;
|
|
41
|
+
|
|
42
|
+
#endif
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
#include <stddef.h>
|
|
2
|
+
#include <stdint.h>
|
|
3
|
+
|
|
4
|
+
#include "common.h"
|
|
5
|
+
#include "cpu.h"
|
|
6
|
+
|
|
7
|
+
static volatile uint16_t optblocker_u16;
|
|
8
|
+
|
|
9
|
+
static inline int
|
|
10
|
+
aegis_verify_n(const uint8_t *x_, const uint8_t *y_, const int n)
|
|
11
|
+
{
|
|
12
|
+
const volatile uint8_t *volatile x = (const volatile uint8_t *volatile) x_;
|
|
13
|
+
const volatile uint8_t *volatile y = (const volatile uint8_t *volatile) y_;
|
|
14
|
+
volatile uint16_t d = 0U;
|
|
15
|
+
int i;
|
|
16
|
+
|
|
17
|
+
for (i = 0; i < n; i++) {
|
|
18
|
+
d |= x[i] ^ y[i];
|
|
19
|
+
}
|
|
20
|
+
#if defined(__GNUC__) || defined(__clang__)
|
|
21
|
+
__asm__("" : "+r"(d) :);
|
|
22
|
+
#endif
|
|
23
|
+
d--;
|
|
24
|
+
d = ((d >> 13) ^ optblocker_u16) >> 2;
|
|
25
|
+
|
|
26
|
+
return (int) d - 1;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
int
|
|
30
|
+
aegis_verify_16(const uint8_t *x, const uint8_t *y)
|
|
31
|
+
{
|
|
32
|
+
return aegis_verify_n(x, y, 16);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
int
|
|
36
|
+
aegis_verify_32(const uint8_t *x, const uint8_t *y)
|
|
37
|
+
{
|
|
38
|
+
return aegis_verify_n(x, y, 32);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
extern int aegis128l_pick_best_implementation(void);
|
|
42
|
+
extern int aegis128x2_pick_best_implementation(void);
|
|
43
|
+
extern int aegis128x4_pick_best_implementation(void);
|
|
44
|
+
extern int aegis256_pick_best_implementation(void);
|
|
45
|
+
extern int aegis256x2_pick_best_implementation(void);
|
|
46
|
+
extern int aegis256x4_pick_best_implementation(void);
|
|
47
|
+
|
|
48
|
+
int
|
|
49
|
+
aegis_init(void)
|
|
50
|
+
{
|
|
51
|
+
static int initialized = 0;
|
|
52
|
+
|
|
53
|
+
if (initialized) {
|
|
54
|
+
return 0;
|
|
55
|
+
}
|
|
56
|
+
if (aegis_runtime_get_cpu_features() != 0) {
|
|
57
|
+
return 0;
|
|
58
|
+
}
|
|
59
|
+
if (aegis128l_pick_best_implementation() != 0 || aegis128x2_pick_best_implementation() != 0 ||
|
|
60
|
+
aegis128x4_pick_best_implementation() != 0 || aegis256_pick_best_implementation() != 0 ||
|
|
61
|
+
aegis256x2_pick_best_implementation() != 0 || aegis256x4_pick_best_implementation() != 0) {
|
|
62
|
+
return -1;
|
|
63
|
+
}
|
|
64
|
+
initialized = 1;
|
|
65
|
+
|
|
66
|
+
return 0;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
#if defined(_MSC_VER)
|
|
70
|
+
# pragma section(".CRT$XCU", read)
|
|
71
|
+
static void __cdecl _do_aegis_init(void);
|
|
72
|
+
__declspec(allocate(".CRT$XCU")) void (*aegis_init_constructor)(void) = _do_aegis_init;
|
|
73
|
+
#else
|
|
74
|
+
static void _do_aegis_init(void) __attribute__((constructor));
|
|
75
|
+
#endif
|
|
76
|
+
|
|
77
|
+
static void
|
|
78
|
+
_do_aegis_init(void)
|
|
79
|
+
{
|
|
80
|
+
(void) aegis_init();
|
|
81
|
+
}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
#ifndef common_H
|
|
2
|
+
#define common_H
|
|
3
|
+
|
|
4
|
+
#include <stddef.h>
|
|
5
|
+
#include <stdint.h>
|
|
6
|
+
|
|
7
|
+
#if defined(__wasm__) && !defined(__wasi__)
|
|
8
|
+
static int errno;
|
|
9
|
+
# define memcpy(A, B, C) __builtin_memcpy((A), (B), (C))
|
|
10
|
+
# define memset(A, B, C) __builtin_memset((A), (B), (C))
|
|
11
|
+
# define memcmp(A, B, C) __builtin_memcmp((A), (B), (C))
|
|
12
|
+
#else
|
|
13
|
+
# include <errno.h>
|
|
14
|
+
# include <stdlib.h>
|
|
15
|
+
# include <string.h>
|
|
16
|
+
#endif
|
|
17
|
+
|
|
18
|
+
#include "aegis.h"
|
|
19
|
+
|
|
20
|
+
#ifdef __linux__
|
|
21
|
+
# define HAVE_SYS_AUXV_H
|
|
22
|
+
# define HAVE_GETAUXVAL
|
|
23
|
+
#endif
|
|
24
|
+
#ifdef __ANDROID_API__
|
|
25
|
+
# if __ANDROID_API__ < 18
|
|
26
|
+
# undef HAVE_GETAUXVAL
|
|
27
|
+
# endif
|
|
28
|
+
# define HAVE_ANDROID_GETCPUFEATURES
|
|
29
|
+
#endif
|
|
30
|
+
#if defined(__i386__) || defined(_M_IX86) || defined(__x86_64__) || defined(_M_AMD64)
|
|
31
|
+
# define HAVE_CPUID
|
|
32
|
+
# define NATIVE_LITTLE_ENDIAN
|
|
33
|
+
# if defined(__clang__) || defined(__GNUC__)
|
|
34
|
+
# define HAVE_AVX_ASM
|
|
35
|
+
# endif
|
|
36
|
+
# define HAVE_AVXINTRIN_H
|
|
37
|
+
# define HAVE_AVX2INTRIN_H
|
|
38
|
+
# define HAVE_AVX512FINTRIN_H
|
|
39
|
+
# define HAVE_TMMINTRIN_H
|
|
40
|
+
# define HAVE_WMMINTRIN_H
|
|
41
|
+
# define HAVE_VAESINTRIN_H
|
|
42
|
+
# ifdef __GNUC__
|
|
43
|
+
# if !__has_include(<vaesintrin.h>)
|
|
44
|
+
# undef HAVE_VAESINTRIN_H
|
|
45
|
+
# endif
|
|
46
|
+
# endif
|
|
47
|
+
|
|
48
|
+
/* target pragmas don't define these flags on clang-cl (an alternative clang driver for Windows) */
|
|
49
|
+
# if defined(__clang__) && defined(_MSC_BUILD) && defined(_MSC_VER) && \
|
|
50
|
+
(defined(_M_IX86) || defined(_M_AMD64)) && !defined(__SSE3__)
|
|
51
|
+
# undef __SSE3__
|
|
52
|
+
# undef __SSSE3__
|
|
53
|
+
# undef __SSE4_1__
|
|
54
|
+
# undef __AVX__
|
|
55
|
+
# undef __AVX2__
|
|
56
|
+
# undef __AVX512F__
|
|
57
|
+
# undef __AES__
|
|
58
|
+
# undef __VAES__
|
|
59
|
+
|
|
60
|
+
# define __SSE3__ 1
|
|
61
|
+
# define __SSSE3__ 1
|
|
62
|
+
# define __SSE4_1__ 1
|
|
63
|
+
# define __AVX__ 1
|
|
64
|
+
# define __AVX2__ 1
|
|
65
|
+
# define __AVX512F__ 1
|
|
66
|
+
# define __AES__ 1
|
|
67
|
+
# define __VAES__ 1
|
|
68
|
+
# endif
|
|
69
|
+
|
|
70
|
+
#endif
|
|
71
|
+
|
|
72
|
+
#ifdef DISABLE_AVX2
|
|
73
|
+
# undef HAVE_AVXINTRIN_H
|
|
74
|
+
# undef HAVE_AVX2INTRIN_H
|
|
75
|
+
# undef HAVE_AVX512FINTRIN_H
|
|
76
|
+
# undef HAVE_VAESINTRIN_H
|
|
77
|
+
#endif
|
|
78
|
+
#ifdef DISABLE_AVX512
|
|
79
|
+
# undef HAVE_AVX512FINTRIN_H
|
|
80
|
+
#endif
|
|
81
|
+
|
|
82
|
+
#if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
|
|
83
|
+
# ifndef NATIVE_LITTLE_ENDIAN
|
|
84
|
+
# define NATIVE_LITTLE_ENDIAN
|
|
85
|
+
# endif
|
|
86
|
+
#endif
|
|
87
|
+
|
|
88
|
+
#if defined(__INTEL_COMPILER) || defined(_MSC_VER)
|
|
89
|
+
# define CRYPTO_ALIGN(x) __declspec(align(x))
|
|
90
|
+
#else
|
|
91
|
+
# define CRYPTO_ALIGN(x) __attribute__((aligned(x)))
|
|
92
|
+
#endif
|
|
93
|
+
|
|
94
|
+
#define LOAD32_LE(SRC) load32_le(SRC)
|
|
95
|
+
static inline uint32_t
|
|
96
|
+
load32_le(const uint8_t src[4])
|
|
97
|
+
{
|
|
98
|
+
#ifdef NATIVE_LITTLE_ENDIAN
|
|
99
|
+
uint32_t w;
|
|
100
|
+
memcpy(&w, src, sizeof w);
|
|
101
|
+
return w;
|
|
102
|
+
#else
|
|
103
|
+
uint32_t w = (uint32_t) src[0];
|
|
104
|
+
w |= (uint32_t) src[1] << 8;
|
|
105
|
+
w |= (uint32_t) src[2] << 16;
|
|
106
|
+
w |= (uint32_t) src[3] << 24;
|
|
107
|
+
return w;
|
|
108
|
+
#endif
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
#define STORE32_LE(DST, W) store32_le((DST), (W))
|
|
112
|
+
static inline void
|
|
113
|
+
store32_le(uint8_t dst[4], uint32_t w)
|
|
114
|
+
{
|
|
115
|
+
#ifdef NATIVE_LITTLE_ENDIAN
|
|
116
|
+
memcpy(dst, &w, sizeof w);
|
|
117
|
+
#else
|
|
118
|
+
dst[0] = (uint8_t) w;
|
|
119
|
+
w >>= 8;
|
|
120
|
+
dst[1] = (uint8_t) w;
|
|
121
|
+
w >>= 8;
|
|
122
|
+
dst[2] = (uint8_t) w;
|
|
123
|
+
w >>= 8;
|
|
124
|
+
dst[3] = (uint8_t) w;
|
|
125
|
+
#endif
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
#define ROTL32(X, B) rotl32((X), (B))
|
|
129
|
+
static inline uint32_t
|
|
130
|
+
rotl32(const uint32_t x, const int b)
|
|
131
|
+
{
|
|
132
|
+
return (x << b) | (x >> (32 - b));
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
#define COMPILER_ASSERT(X) (void) sizeof(char[(X) ? 1 : -1])
|
|
136
|
+
|
|
137
|
+
#ifndef ERANGE
|
|
138
|
+
# define ERANGE 34
|
|
139
|
+
#endif
|
|
140
|
+
#ifndef EINVAL
|
|
141
|
+
# define EINVAL 22
|
|
142
|
+
#endif
|
|
143
|
+
|
|
144
|
+
#endif
|