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.
Files changed (143) hide show
  1. checksums.yaml +7 -0
  2. data/.github/workflows/ci.yml +66 -0
  3. data/CHANGELOG.md +44 -0
  4. data/FORMAT.md +139 -0
  5. data/GET_STARTED.md +30 -0
  6. data/Gemfile +7 -0
  7. data/LICENSE.txt +21 -0
  8. data/README.md +215 -0
  9. data/RELEASING.md +34 -0
  10. data/Rakefile +19 -0
  11. data/SECURITY.md +85 -0
  12. data/VENDORING.md +14 -0
  13. data/ext/pq_crypto_seal/aegis_unused_stubs.c +20 -0
  14. data/ext/pq_crypto_seal/extconf.rb +296 -0
  15. data/ext/pq_crypto_seal/pq_crypto_seal.c +390 -0
  16. data/ext/pq_crypto_seal/vendor/libaegis/LICENSE +21 -0
  17. data/ext/pq_crypto_seal/vendor/libaegis/TREE_SHA256 +1 -0
  18. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128l/aegis128l.c +246 -0
  19. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128l/aegis128l_aesni.c +73 -0
  20. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128l/aegis128l_aesni.h +9 -0
  21. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128l/aegis128l_altivec.c +70 -0
  22. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128l/aegis128l_altivec.h +9 -0
  23. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128l/aegis128l_common.h +691 -0
  24. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128l/aegis128l_neon_aes.c +77 -0
  25. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128l/aegis128l_neon_aes.h +9 -0
  26. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128l/aegis128l_neon_sha3.c +86 -0
  27. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128l/aegis128l_neon_sha3.h +9 -0
  28. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128l/aegis128l_soft.c +63 -0
  29. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128l/aegis128l_soft.h +9 -0
  30. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128l/implementations.h +40 -0
  31. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x2/aegis128x2.c +247 -0
  32. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x2/aegis128x2_aesni.c +108 -0
  33. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x2/aegis128x2_aesni.h +9 -0
  34. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x2/aegis128x2_altivec.c +103 -0
  35. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x2/aegis128x2_altivec.h +9 -0
  36. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x2/aegis128x2_avx2.c +79 -0
  37. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x2/aegis128x2_avx2.h +11 -0
  38. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x2/aegis128x2_common.h +795 -0
  39. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x2/aegis128x2_neon_aes.c +111 -0
  40. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x2/aegis128x2_neon_aes.h +9 -0
  41. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x2/aegis128x2_soft.c +96 -0
  42. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x2/aegis128x2_soft.h +9 -0
  43. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x2/implementations.h +40 -0
  44. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x4/aegis128x4.c +252 -0
  45. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x4/aegis128x4_aesni.c +117 -0
  46. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x4/aegis128x4_aesni.h +9 -0
  47. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x4/aegis128x4_altivec.c +111 -0
  48. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x4/aegis128x4_altivec.h +9 -0
  49. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x4/aegis128x4_avx2.c +112 -0
  50. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x4/aegis128x4_avx2.h +11 -0
  51. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x4/aegis128x4_avx512.c +85 -0
  52. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x4/aegis128x4_avx512.h +11 -0
  53. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x4/aegis128x4_common.h +808 -0
  54. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x4/aegis128x4_neon_aes.c +119 -0
  55. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x4/aegis128x4_neon_aes.h +9 -0
  56. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x4/aegis128x4_soft.c +104 -0
  57. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x4/aegis128x4_soft.h +9 -0
  58. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis128x4/implementations.h +41 -0
  59. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256/aegis256.c +240 -0
  60. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256/aegis256_aesni.c +68 -0
  61. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256/aegis256_aesni.h +9 -0
  62. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256/aegis256_altivec.c +68 -0
  63. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256/aegis256_altivec.h +9 -0
  64. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256/aegis256_common.h +673 -0
  65. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256/aegis256_neon_aes.c +75 -0
  66. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256/aegis256_neon_aes.h +9 -0
  67. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256/aegis256_soft.c +58 -0
  68. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256/aegis256_soft.h +9 -0
  69. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256/implementations.h +39 -0
  70. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x2/aegis256x2.c +247 -0
  71. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x2/aegis256x2_aesni.c +103 -0
  72. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x2/aegis256x2_aesni.h +9 -0
  73. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x2/aegis256x2_altivec.c +101 -0
  74. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x2/aegis256x2_altivec.h +9 -0
  75. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x2/aegis256x2_avx2.c +74 -0
  76. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x2/aegis256x2_avx2.h +11 -0
  77. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x2/aegis256x2_common.h +790 -0
  78. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x2/aegis256x2_neon_aes.c +109 -0
  79. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x2/aegis256x2_neon_aes.h +9 -0
  80. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x2/aegis256x2_soft.c +91 -0
  81. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x2/aegis256x2_soft.h +9 -0
  82. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x2/implementations.h +41 -0
  83. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x4/aegis256x4.c +252 -0
  84. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x4/aegis256x4_aesni.c +112 -0
  85. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x4/aegis256x4_aesni.h +9 -0
  86. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x4/aegis256x4_altivec.c +109 -0
  87. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x4/aegis256x4_altivec.h +9 -0
  88. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x4/aegis256x4_avx2.c +107 -0
  89. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x4/aegis256x4_avx2.h +11 -0
  90. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x4/aegis256x4_avx512.c +79 -0
  91. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x4/aegis256x4_avx512.h +11 -0
  92. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x4/aegis256x4_common.h +805 -0
  93. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x4/aegis256x4_neon_aes.c +117 -0
  94. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x4/aegis256x4_neon_aes.h +9 -0
  95. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x4/aegis256x4_soft.c +99 -0
  96. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x4/aegis256x4_soft.h +9 -0
  97. data/ext/pq_crypto_seal/vendor/libaegis/src/aegis256x4/implementations.h +42 -0
  98. data/ext/pq_crypto_seal/vendor/libaegis/src/common/common.c +81 -0
  99. data/ext/pq_crypto_seal/vendor/libaegis/src/common/common.h +144 -0
  100. data/ext/pq_crypto_seal/vendor/libaegis/src/common/cpu.c +378 -0
  101. data/ext/pq_crypto_seal/vendor/libaegis/src/common/cpu.h +34 -0
  102. data/ext/pq_crypto_seal/vendor/libaegis/src/common/keccak.c +187 -0
  103. data/ext/pq_crypto_seal/vendor/libaegis/src/common/keccak.h +13 -0
  104. data/ext/pq_crypto_seal/vendor/libaegis/src/common/softaes.c +338 -0
  105. data/ext/pq_crypto_seal/vendor/libaegis/src/common/softaes.h +69 -0
  106. data/ext/pq_crypto_seal/vendor/libaegis/src/include/aegis.h +77 -0
  107. data/ext/pq_crypto_seal/vendor/libaegis/src/include/aegis128l.h +311 -0
  108. data/ext/pq_crypto_seal/vendor/libaegis/src/include/aegis128x2.h +313 -0
  109. data/ext/pq_crypto_seal/vendor/libaegis/src/include/aegis128x4.h +313 -0
  110. data/ext/pq_crypto_seal/vendor/libaegis/src/include/aegis256.h +311 -0
  111. data/ext/pq_crypto_seal/vendor/libaegis/src/include/aegis256x2.h +313 -0
  112. data/ext/pq_crypto_seal/vendor/libaegis/src/include/aegis256x4.h +313 -0
  113. data/ext/pq_crypto_seal/vendor/libaegis/src/include/aegis_raf.h +580 -0
  114. data/ext/pq_crypto_seal/vendor/libaegis/src/raf/raf.c +87 -0
  115. data/ext/pq_crypto_seal/vendor/libaegis/src/raf/raf_aegis128l.c +20 -0
  116. data/ext/pq_crypto_seal/vendor/libaegis/src/raf/raf_aegis128x2.c +20 -0
  117. data/ext/pq_crypto_seal/vendor/libaegis/src/raf/raf_aegis128x4.c +20 -0
  118. data/ext/pq_crypto_seal/vendor/libaegis/src/raf/raf_aegis256.c +20 -0
  119. data/ext/pq_crypto_seal/vendor/libaegis/src/raf/raf_aegis256x2.c +20 -0
  120. data/ext/pq_crypto_seal/vendor/libaegis/src/raf/raf_aegis256x4.c +20 -0
  121. data/ext/pq_crypto_seal/vendor/libaegis/src/raf/raf_internal.h +152 -0
  122. data/ext/pq_crypto_seal/vendor/libaegis/src/raf/raf_merkle.c +268 -0
  123. data/ext/pq_crypto_seal/vendor/libaegis/src/raf/raf_merkle.h +165 -0
  124. data/ext/pq_crypto_seal/vendor/libaegis/src/raf/raf_variant.h +1119 -0
  125. data/fuzz/decrypt_fuzz.rb +119 -0
  126. data/lib/pq_crypto/seal/binary.rb +62 -0
  127. data/lib/pq_crypto/seal/core.rb +398 -0
  128. data/lib/pq_crypto/seal/errors.rb +15 -0
  129. data/lib/pq_crypto/seal/format.rb +275 -0
  130. data/lib/pq_crypto/seal/io.rb +336 -0
  131. data/lib/pq_crypto/seal/native.rb +13 -0
  132. data/lib/pq_crypto/seal/padding.rb +47 -0
  133. data/lib/pq_crypto/seal/version.rb +6 -0
  134. data/lib/pq_crypto/seal.rb +32 -0
  135. data/pq_crypto-seal.gemspec +37 -0
  136. data/script/vendor_libs.rb +79 -0
  137. data/test/aegis_vectors_test.rb +54 -0
  138. data/test/golden_vectors_test.rb +183 -0
  139. data/test/io_test.rb +125 -0
  140. data/test/native_test.rb +36 -0
  141. data/test/seal_test.rb +238 -0
  142. data/test/test_helper.rb +6 -0
  143. metadata +244 -0
@@ -0,0 +1,119 @@
1
+ #if defined(__aarch64__) || defined(_M_ARM64)
2
+
3
+ # include <stddef.h>
4
+ # include <stdint.h>
5
+
6
+ # include "../common/common.h"
7
+ # include "aegis128x4.h"
8
+ # include "aegis128x4_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
+ aegis128x4_update(aes_block_t *const state, const aes_block_t d1, const aes_block_t d2)
81
+ {
82
+ aes_block_t tmp;
83
+
84
+ tmp = state[7];
85
+ state[7] = AES_ENC(state[6], state[7]);
86
+ state[6] = AES_ENC(state[5], state[6]);
87
+ state[5] = AES_ENC(state[4], state[5]);
88
+ state[4] = AES_BLOCK_XOR(AES_ENC(state[3], state[4]), d2);
89
+ state[3] = AES_ENC(state[2], state[3]);
90
+ state[2] = AES_ENC(state[1], state[2]);
91
+ state[1] = AES_ENC(state[0], state[1]);
92
+ state[0] = AES_BLOCK_XOR(AES_ENC(tmp, state[0]), d1);
93
+ }
94
+
95
+ # include "aegis128x4_common.h"
96
+
97
+ struct aegis128x4_implementation aegis128x4_neon_aes_implementation = {
98
+ .encrypt_detached = encrypt_detached,
99
+ .decrypt_detached = decrypt_detached,
100
+ .encrypt_unauthenticated = encrypt_unauthenticated,
101
+ .decrypt_unauthenticated = decrypt_unauthenticated,
102
+ .stream = stream,
103
+ .state_init = state_init,
104
+ .state_encrypt_update = state_encrypt_update,
105
+ .state_encrypt_final = state_encrypt_final,
106
+ .state_decrypt_update = state_decrypt_update,
107
+ .state_decrypt_final = state_decrypt_final,
108
+ .state_mac_init = state_mac_init,
109
+ .state_mac_update = state_mac_update,
110
+ .state_mac_final = state_mac_final,
111
+ .state_mac_reset = state_mac_reset,
112
+ .state_mac_clone = state_mac_clone,
113
+ };
114
+
115
+ # ifdef __clang__
116
+ # pragma clang attribute pop
117
+ # endif
118
+
119
+ #endif
@@ -0,0 +1,9 @@
1
+ #ifndef aegis128x4_neon_aes_H
2
+ #define aegis128x4_neon_aes_H
3
+
4
+ #include "../common/common.h"
5
+ #include "implementations.h"
6
+
7
+ extern struct aegis128x4_implementation aegis128x4_neon_aes_implementation;
8
+
9
+ #endif
@@ -0,0 +1,104 @@
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 "aegis128x4.h"
11
+ # include "aegis128x4_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
+ aegis128x4_update(aes_block_t *const state, const aes_block_t d1, const aes_block_t d2)
67
+ {
68
+ aes_block_t tmp;
69
+
70
+ tmp = state[7];
71
+ state[7] = AES_ENC(state[6], state[7]);
72
+ state[6] = AES_ENC(state[5], state[6]);
73
+ state[5] = AES_ENC(state[4], state[5]);
74
+ state[4] = AES_ENC(state[3], state[4]);
75
+ state[3] = AES_ENC(state[2], state[3]);
76
+ state[2] = AES_ENC(state[1], state[2]);
77
+ state[1] = AES_ENC(state[0], state[1]);
78
+ state[0] = AES_ENC(tmp, state[0]);
79
+
80
+ state[0] = AES_BLOCK_XOR(state[0], d1);
81
+ state[4] = AES_BLOCK_XOR(state[4], d2);
82
+ }
83
+
84
+ # include "aegis128x4_common.h"
85
+
86
+ struct aegis128x4_implementation aegis128x4_soft_implementation = {
87
+ .encrypt_detached = encrypt_detached,
88
+ .decrypt_detached = decrypt_detached,
89
+ .encrypt_unauthenticated = encrypt_unauthenticated,
90
+ .decrypt_unauthenticated = decrypt_unauthenticated,
91
+ .stream = stream,
92
+ .state_init = state_init,
93
+ .state_encrypt_update = state_encrypt_update,
94
+ .state_encrypt_final = state_encrypt_final,
95
+ .state_decrypt_update = state_decrypt_update,
96
+ .state_decrypt_final = state_decrypt_final,
97
+ .state_mac_init = state_mac_init,
98
+ .state_mac_update = state_mac_update,
99
+ .state_mac_final = state_mac_final,
100
+ .state_mac_reset = state_mac_reset,
101
+ .state_mac_clone = state_mac_clone,
102
+ };
103
+
104
+ #endif
@@ -0,0 +1,9 @@
1
+ #ifndef aegis128x4_soft_H
2
+ #define aegis128x4_soft_H
3
+
4
+ #include "../common/common.h"
5
+ #include "implementations.h"
6
+
7
+ extern struct aegis128x4_implementation aegis128x4_soft_implementation;
8
+
9
+ #endif
@@ -0,0 +1,41 @@
1
+ #ifndef aegis128x4_implementations_H
2
+ #define aegis128x4_implementations_H
3
+
4
+ #include <stddef.h>
5
+ #include <stdint.h>
6
+
7
+ #include "aegis128x4.h"
8
+
9
+ /* Namespacing to avoid conflicts with libsodium */
10
+ #define aegis128x4_soft_implementation libaegis_aegis128x4_soft_implementation
11
+ #define aegis128x4_aesni_implementation libaegis_aegis128x4_aesni_implementation
12
+ #define aegis128x4_neon_aes_implementation libaegis_aegis128x4_neon_aes_implementation
13
+ #define aegis128x4_altivec_implementation libaegis_aegis128x4_altivec_implementation
14
+ #define aegis128x4_avx2_implementation libaegis_aegis128x4_avx2_implementation
15
+ #define aegis128x4_avx512_implementation libaegis_aegis128x4_avx512_implementation
16
+
17
+ typedef struct aegis128x4_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)(aegis128x4_state *st_, const uint8_t *ad, size_t adlen, const uint8_t *npub,
29
+ const uint8_t *k);
30
+ int (*state_encrypt_update)(aegis128x4_state *st_, uint8_t *c, const uint8_t *m, size_t mlen);
31
+ int (*state_encrypt_final)(aegis128x4_state *st_, uint8_t *mac, size_t maclen);
32
+ int (*state_decrypt_update)(aegis128x4_state *st_, uint8_t *m, const uint8_t *c, size_t clen);
33
+ int (*state_decrypt_final)(aegis128x4_state *st_, const uint8_t *mac, size_t maclen);
34
+ void (*state_mac_init)(aegis128x4_mac_state *st_, const uint8_t *npub, const uint8_t *k);
35
+ int (*state_mac_update)(aegis128x4_mac_state *st_, const uint8_t *ad, size_t adlen);
36
+ int (*state_mac_final)(aegis128x4_mac_state *st_, uint8_t *mac, size_t maclen);
37
+ void (*state_mac_reset)(aegis128x4_mac_state *st);
38
+ void (*state_mac_clone)(aegis128x4_mac_state *dst, const aegis128x4_mac_state *src);
39
+ } aegis128x4_implementation;
40
+
41
+ #endif
@@ -0,0 +1,240 @@
1
+ #include <stddef.h>
2
+ #include <stdint.h>
3
+
4
+ #include "../common/common.h"
5
+ #include "../common/cpu.h"
6
+ #include "aegis256.h"
7
+ #include "aegis256_aesni.h"
8
+ #include "aegis256_altivec.h"
9
+ #include "aegis256_neon_aes.h"
10
+
11
+ #ifndef HAS_HW_AES
12
+ # include "aegis256_soft.h"
13
+ static const aegis256_implementation *implementation = &aegis256_soft_implementation;
14
+ #else
15
+ # if defined(__aarch64__) || defined(_M_ARM64)
16
+ static const aegis256_implementation *implementation = &aegis256_neon_aes_implementation;
17
+ # elif defined(__x86_64__) || defined(__i386__)
18
+ static const aegis256_implementation *implementation = &aegis256_aesni_implementation;
19
+ # elif defined(__ALTIVEC__) && defined(__CRYPTO__)
20
+ static const aegis256_implementation *implementation = &aegis256_altivec_implementation;
21
+ # else
22
+ # error "Unsupported architecture"
23
+ # endif
24
+ #endif
25
+
26
+ size_t
27
+ aegis256_keybytes(void)
28
+ {
29
+ return aegis256_KEYBYTES;
30
+ }
31
+
32
+ size_t
33
+ aegis256_npubbytes(void)
34
+ {
35
+ return aegis256_NPUBBYTES;
36
+ }
37
+
38
+ size_t
39
+ aegis256_abytes_min(void)
40
+ {
41
+ return aegis256_ABYTES_MIN;
42
+ }
43
+
44
+ size_t
45
+ aegis256_abytes_max(void)
46
+ {
47
+ return aegis256_ABYTES_MAX;
48
+ }
49
+
50
+ size_t
51
+ aegis256_tailbytes_max(void)
52
+ {
53
+ return aegis256_TAILBYTES_MAX;
54
+ }
55
+
56
+ int
57
+ aegis256_encrypt_detached(uint8_t *c, uint8_t *mac, size_t maclen, const uint8_t *m, size_t mlen,
58
+ const uint8_t *ad, size_t adlen, const uint8_t *npub, const uint8_t *k)
59
+ {
60
+ if (maclen != 16 && maclen != 32) {
61
+ errno = EINVAL;
62
+ return -1;
63
+ }
64
+ return implementation->encrypt_detached(c, mac, maclen, m, mlen, ad, adlen, npub, k);
65
+ }
66
+
67
+ int
68
+ aegis256_decrypt_detached(uint8_t *m, const uint8_t *c, size_t clen, const uint8_t *mac,
69
+ size_t maclen, const uint8_t *ad, size_t adlen, const uint8_t *npub,
70
+ const uint8_t *k)
71
+ {
72
+ if (maclen != 16 && maclen != 32) {
73
+ errno = EINVAL;
74
+ return -1;
75
+ }
76
+ return implementation->decrypt_detached(m, c, clen, mac, maclen, ad, adlen, npub, k);
77
+ }
78
+
79
+ int
80
+ aegis256_encrypt(uint8_t *c, size_t maclen, const uint8_t *m, size_t mlen, const uint8_t *ad,
81
+ size_t adlen, const uint8_t *npub, const uint8_t *k)
82
+ {
83
+ return aegis256_encrypt_detached(c, c + mlen, maclen, m, mlen, ad, adlen, npub, k);
84
+ }
85
+
86
+ int
87
+ aegis256_decrypt(uint8_t *m, const uint8_t *c, size_t clen, size_t maclen, const uint8_t *ad,
88
+ size_t adlen, const uint8_t *npub, const uint8_t *k)
89
+ {
90
+ int ret = -1;
91
+
92
+ if (clen >= maclen) {
93
+ ret = aegis256_decrypt_detached(m, c, clen - maclen, c + clen - maclen, maclen, ad, adlen,
94
+ npub, k);
95
+ }
96
+ return ret;
97
+ }
98
+
99
+ void
100
+ aegis256_state_init(aegis256_state *st_, const uint8_t *ad, size_t adlen, const uint8_t *npub,
101
+ const uint8_t *k)
102
+ {
103
+ memset(st_, 0, sizeof *st_);
104
+ implementation->state_init(st_, ad, adlen, npub, k);
105
+ }
106
+
107
+ int
108
+ aegis256_state_encrypt_update(aegis256_state *st_, uint8_t *c, const uint8_t *m, size_t mlen)
109
+ {
110
+ return implementation->state_encrypt_update(st_, c, m, mlen);
111
+ }
112
+
113
+ int
114
+ aegis256_state_encrypt_final(aegis256_state *st_, uint8_t *mac, size_t maclen)
115
+ {
116
+ if (maclen != 16 && maclen != 32) {
117
+ errno = EINVAL;
118
+ return -1;
119
+ }
120
+ return implementation->state_encrypt_final(st_, mac, maclen);
121
+ }
122
+
123
+ int
124
+ aegis256_state_decrypt_update(aegis256_state *st_, uint8_t *m, const uint8_t *c, size_t clen)
125
+ {
126
+ return implementation->state_decrypt_update(st_, m, c, clen);
127
+ }
128
+
129
+ int
130
+ aegis256_state_decrypt_final(aegis256_state *st_, const uint8_t *mac, size_t maclen)
131
+ {
132
+ if (maclen != 16 && maclen != 32) {
133
+ errno = EINVAL;
134
+ return -1;
135
+ }
136
+ return implementation->state_decrypt_final(st_, mac, maclen);
137
+ }
138
+
139
+ void
140
+ aegis256_stream(uint8_t *out, size_t len, const uint8_t *npub, const uint8_t *k)
141
+ {
142
+ implementation->stream(out, len, npub, k);
143
+ }
144
+
145
+ void
146
+ aegis256_encrypt_unauthenticated(uint8_t *c, const uint8_t *m, size_t mlen, const uint8_t *npub,
147
+ const uint8_t *k)
148
+ {
149
+ implementation->encrypt_unauthenticated(c, m, mlen, npub, k);
150
+ }
151
+
152
+ void
153
+ aegis256_decrypt_unauthenticated(uint8_t *m, const uint8_t *c, size_t clen, const uint8_t *npub,
154
+ const uint8_t *k)
155
+ {
156
+ implementation->decrypt_unauthenticated(m, c, clen, npub, k);
157
+ }
158
+
159
+ void
160
+ aegis256_mac_init(aegis256_mac_state *st_, const uint8_t *k, const uint8_t *npub)
161
+ {
162
+ implementation->state_mac_init(st_, npub, k);
163
+ }
164
+
165
+ int
166
+ aegis256_mac_update(aegis256_mac_state *st_, const uint8_t *m, size_t mlen)
167
+ {
168
+ return implementation->state_mac_update(st_, m, mlen);
169
+ }
170
+
171
+ int
172
+ aegis256_mac_final(aegis256_mac_state *st_, uint8_t *mac, size_t maclen)
173
+ {
174
+ if (maclen != 16 && maclen != 32) {
175
+ errno = EINVAL;
176
+ return -1;
177
+ }
178
+ return implementation->state_mac_final(st_, mac, maclen);
179
+ }
180
+
181
+ int
182
+ aegis256_mac_verify(aegis256_mac_state *st_, const uint8_t *mac, size_t maclen)
183
+ {
184
+ uint8_t expected_mac[32];
185
+
186
+ switch (maclen) {
187
+ case 16:
188
+ implementation->state_mac_final(st_, expected_mac, maclen);
189
+ return aegis_verify_16(expected_mac, mac);
190
+ case 32:
191
+ implementation->state_mac_final(st_, expected_mac, maclen);
192
+ return aegis_verify_32(expected_mac, mac);
193
+ default:
194
+ errno = EINVAL;
195
+ return -1;
196
+ }
197
+ }
198
+
199
+ void
200
+ aegis256_mac_reset(aegis256_mac_state *st_)
201
+ {
202
+ implementation->state_mac_reset(st_);
203
+ }
204
+
205
+ void
206
+ aegis256_mac_state_clone(aegis256_mac_state *dst, const aegis256_mac_state *src)
207
+ {
208
+ implementation->state_mac_clone(dst, src);
209
+ }
210
+
211
+ int
212
+ aegis256_pick_best_implementation(void)
213
+ {
214
+ #ifndef HAS_HW_AES
215
+ implementation = &aegis256_soft_implementation;
216
+ #endif
217
+
218
+ #if defined(__aarch64__) || defined(_M_ARM64)
219
+ if (aegis_runtime_has_neon_aes()) {
220
+ implementation = &aegis256_neon_aes_implementation;
221
+ return 0;
222
+ }
223
+ #endif
224
+
225
+ #if defined(__x86_64__) || defined(_M_AMD64) || defined(__i386__) || defined(_M_IX86)
226
+ if (aegis_runtime_has_aesni() && aegis_runtime_has_avx()) {
227
+ implementation = &aegis256_aesni_implementation;
228
+ return 0;
229
+ }
230
+ #endif
231
+
232
+ #if defined(__ALTIVEC__) && defined(__CRYPTO__)
233
+ if (aegis_runtime_has_altivec()) {
234
+ implementation = &aegis256_altivec_implementation;
235
+ return 0;
236
+ }
237
+ #endif
238
+
239
+ return 0; /* LCOV_EXCL_LINE */
240
+ }
@@ -0,0 +1,68 @@
1
+ #if defined(__i386__) || defined(_M_IX86) || defined(__x86_64__) || defined(_M_AMD64)
2
+
3
+ # include <stddef.h>
4
+ # include <stdint.h>
5
+
6
+ # include "../common/common.h"
7
+ # include "aegis256.h"
8
+ # include "aegis256_aesni.h"
9
+
10
+ # ifdef __clang__
11
+ # pragma clang attribute push(__attribute__((target("aes,avx"))), apply_to = function)
12
+ # elif defined(__GNUC__)
13
+ # pragma GCC target("aes,avx")
14
+ # endif
15
+
16
+ # include <immintrin.h>
17
+ # include <wmmintrin.h>
18
+
19
+ # define AES_BLOCK_LENGTH 16
20
+
21
+ typedef __m128i aes_block_t;
22
+
23
+ # define AES_BLOCK_XOR(A, B) _mm_xor_si128((A), (B))
24
+ # define AES_BLOCK_AND(A, B) _mm_and_si128((A), (B))
25
+ # define AES_BLOCK_LOAD(A) _mm_loadu_si128((const aes_block_t *) (const void *) (A))
26
+ # define AES_BLOCK_LOAD_64x2(A, B) _mm_set_epi64x((long long) (A), (long long) (B))
27
+ # define AES_BLOCK_STORE(A, B) _mm_storeu_si128((aes_block_t *) (void *) (A), (B))
28
+ # define AES_ENC(A, B) _mm_aesenc_si128((A), (B))
29
+
30
+ static inline void
31
+ aegis256_update(aes_block_t *const state, const aes_block_t d)
32
+ {
33
+ aes_block_t tmp;
34
+
35
+ tmp = state[5];
36
+ state[5] = AES_ENC(state[4], state[5]);
37
+ state[4] = AES_ENC(state[3], state[4]);
38
+ state[3] = AES_ENC(state[2], state[3]);
39
+ state[2] = AES_ENC(state[1], state[2]);
40
+ state[1] = AES_ENC(state[0], state[1]);
41
+ state[0] = AES_BLOCK_XOR(AES_ENC(tmp, state[0]), d);
42
+ }
43
+
44
+ # include "aegis256_common.h"
45
+
46
+ struct aegis256_implementation aegis256_aesni_implementation = {
47
+ .encrypt_detached = encrypt_detached,
48
+ .decrypt_detached = decrypt_detached,
49
+ .encrypt_unauthenticated = encrypt_unauthenticated,
50
+ .decrypt_unauthenticated = decrypt_unauthenticated,
51
+ .stream = stream,
52
+ .state_init = state_init,
53
+ .state_encrypt_update = state_encrypt_update,
54
+ .state_encrypt_final = state_encrypt_final,
55
+ .state_decrypt_update = state_decrypt_update,
56
+ .state_decrypt_final = state_decrypt_final,
57
+ .state_mac_init = state_mac_init,
58
+ .state_mac_update = state_mac_update,
59
+ .state_mac_final = state_mac_final,
60
+ .state_mac_reset = state_mac_reset,
61
+ .state_mac_clone = state_mac_clone,
62
+ };
63
+
64
+ # ifdef __clang__
65
+ # pragma clang attribute pop
66
+ # endif
67
+
68
+ #endif
@@ -0,0 +1,9 @@
1
+ #ifndef aegis256_aesni_H
2
+ #define aegis256_aesni_H
3
+
4
+ #include "../common/common.h"
5
+ #include "implementations.h"
6
+
7
+ extern struct aegis256_implementation aegis256_aesni_implementation;
8
+
9
+ #endif
@@ -0,0 +1,68 @@
1
+ #if defined(__ALTIVEC__) && defined(__CRYPTO__)
2
+
3
+ # include <stddef.h>
4
+ # include <stdint.h>
5
+
6
+ # include "../common/common.h"
7
+ # include "aegis256.h"
8
+ # include "aegis256_altivec.h"
9
+
10
+ # include <altivec.h>
11
+
12
+ # ifdef __clang__
13
+ # pragma clang attribute push(__attribute__((target("altivec,crypto"))), apply_to = function)
14
+ # elif defined(__GNUC__)
15
+ # pragma GCC target("altivec,crypto")
16
+ # endif
17
+
18
+ # define AES_BLOCK_LENGTH 16
19
+
20
+ typedef vector unsigned char aes_block_t;
21
+
22
+ # define AES_BLOCK_XOR(A, B) vec_xor((A), (B))
23
+ # define AES_BLOCK_AND(A, B) vec_and((A), (B))
24
+ # define AES_BLOCK_LOAD(A) vec_xl_be(0, (const unsigned char *) (A))
25
+ # define AES_BLOCK_LOAD_64x2(A, B) \
26
+ ((aes_block_t) vec_revb(vec_insert((A), vec_promote((unsigned long long) (B), 1), 0)))
27
+ # define AES_BLOCK_STORE(A, B) vec_xst_be((B), 0, (unsigned char *) (A))
28
+ # define AES_ENC(A, B) ((aes_block_t) vec_cipher_be((A), (B)))
29
+
30
+ static inline void
31
+ aegis256_update(aes_block_t *const state, const aes_block_t d)
32
+ {
33
+ aes_block_t tmp;
34
+
35
+ tmp = state[5];
36
+ state[5] = AES_ENC(state[4], state[5]);
37
+ state[4] = AES_ENC(state[3], state[4]);
38
+ state[3] = AES_ENC(state[2], state[3]);
39
+ state[2] = AES_ENC(state[1], state[2]);
40
+ state[1] = AES_ENC(state[0], state[1]);
41
+ state[0] = AES_BLOCK_XOR(AES_ENC(tmp, state[0]), d);
42
+ }
43
+
44
+ # include "aegis256_common.h"
45
+
46
+ struct aegis256_implementation aegis256_altivec_implementation = {
47
+ .encrypt_detached = encrypt_detached,
48
+ .decrypt_detached = decrypt_detached,
49
+ .encrypt_unauthenticated = encrypt_unauthenticated,
50
+ .decrypt_unauthenticated = decrypt_unauthenticated,
51
+ .stream = stream,
52
+ .state_init = state_init,
53
+ .state_encrypt_update = state_encrypt_update,
54
+ .state_encrypt_final = state_encrypt_final,
55
+ .state_decrypt_update = state_decrypt_update,
56
+ .state_decrypt_final = state_decrypt_final,
57
+ .state_mac_init = state_mac_init,
58
+ .state_mac_update = state_mac_update,
59
+ .state_mac_final = state_mac_final,
60
+ .state_mac_reset = state_mac_reset,
61
+ .state_mac_clone = state_mac_clone,
62
+ };
63
+
64
+ # ifdef __clang__
65
+ # pragma clang attribute pop
66
+ # endif
67
+
68
+ #endif
@@ -0,0 +1,9 @@
1
+ #ifndef aegis256_altivec_H
2
+ #define aegis256_altivec_H
3
+
4
+ #include "../common/common.h"
5
+ #include "implementations.h"
6
+
7
+ extern struct aegis256_implementation aegis256_altivec_implementation;
8
+
9
+ #endif