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,111 @@
1
+ #if defined(__aarch64__) || defined(_M_ARM64)
2
+
3
+ # include <stddef.h>
4
+ # include <stdint.h>
5
+
6
+ # include "../common/common.h"
7
+ # include "aegis128x2.h"
8
+ # include "aegis128x2_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 32
27
+
28
+ typedef struct {
29
+ uint8x16_t b0;
30
+ uint8x16_t b1;
31
+ } aes_block_t;
32
+
33
+ static inline aes_block_t
34
+ AES_BLOCK_XOR(const aes_block_t a, const aes_block_t b)
35
+ {
36
+ return (aes_block_t) { veorq_u8(a.b0, b.b0), veorq_u8(a.b1, b.b1) };
37
+ }
38
+
39
+ static inline aes_block_t
40
+ AES_BLOCK_AND(const aes_block_t a, const aes_block_t b)
41
+ {
42
+ return (aes_block_t) { vandq_u8(a.b0, b.b0), vandq_u8(a.b1, b.b1) };
43
+ }
44
+
45
+ static inline aes_block_t
46
+ AES_BLOCK_LOAD(const uint8_t *a)
47
+ {
48
+ return (aes_block_t) { vld1q_u8(a), vld1q_u8(a + 16) };
49
+ }
50
+
51
+ static inline aes_block_t
52
+ AES_BLOCK_LOAD_64x2(uint64_t a, uint64_t b)
53
+ {
54
+ const uint8x16_t t = vreinterpretq_u8_u64(vsetq_lane_u64((a), vmovq_n_u64(b), 1));
55
+ return (aes_block_t) { t, t };
56
+ }
57
+ static inline void
58
+ AES_BLOCK_STORE(uint8_t *a, const aes_block_t b)
59
+ {
60
+ vst1q_u8(a, b.b0);
61
+ vst1q_u8(a + 16, b.b1);
62
+ }
63
+
64
+ static inline aes_block_t
65
+ AES_ENC(const aes_block_t a, const aes_block_t b)
66
+ {
67
+ return (aes_block_t) { veorq_u8(vaesmcq_u8(vaeseq_u8(vmovq_n_u8(0), a.b0)), b.b0),
68
+ veorq_u8(vaesmcq_u8(vaeseq_u8(vmovq_n_u8(0), a.b1)), b.b1) };
69
+ }
70
+
71
+ static inline void
72
+ aegis128x2_update(aes_block_t *const state, const aes_block_t d1, const aes_block_t d2)
73
+ {
74
+ aes_block_t tmp;
75
+
76
+ tmp = state[7];
77
+ state[7] = AES_ENC(state[6], state[7]);
78
+ state[6] = AES_ENC(state[5], state[6]);
79
+ state[5] = AES_ENC(state[4], state[5]);
80
+ state[4] = AES_BLOCK_XOR(AES_ENC(state[3], state[4]), d2);
81
+ state[3] = AES_ENC(state[2], state[3]);
82
+ state[2] = AES_ENC(state[1], state[2]);
83
+ state[1] = AES_ENC(state[0], state[1]);
84
+ state[0] = AES_BLOCK_XOR(AES_ENC(tmp, state[0]), d1);
85
+ }
86
+
87
+ # include "aegis128x2_common.h"
88
+
89
+ struct aegis128x2_implementation aegis128x2_neon_aes_implementation = {
90
+ .encrypt_detached = encrypt_detached,
91
+ .decrypt_detached = decrypt_detached,
92
+ .encrypt_unauthenticated = encrypt_unauthenticated,
93
+ .decrypt_unauthenticated = decrypt_unauthenticated,
94
+ .stream = stream,
95
+ .state_init = state_init,
96
+ .state_encrypt_update = state_encrypt_update,
97
+ .state_encrypt_final = state_encrypt_final,
98
+ .state_decrypt_update = state_decrypt_update,
99
+ .state_decrypt_final = state_decrypt_final,
100
+ .state_mac_init = state_mac_init,
101
+ .state_mac_update = state_mac_update,
102
+ .state_mac_final = state_mac_final,
103
+ .state_mac_reset = state_mac_reset,
104
+ .state_mac_clone = state_mac_clone,
105
+ };
106
+
107
+ # ifdef __clang__
108
+ # pragma clang attribute pop
109
+ # endif
110
+
111
+ #endif
@@ -0,0 +1,9 @@
1
+ #ifndef aegis128x2_neon_aes_H
2
+ #define aegis128x2_neon_aes_H
3
+
4
+ #include "../common/common.h"
5
+ #include "implementations.h"
6
+
7
+ extern struct aegis128x2_implementation aegis128x2_neon_aes_implementation;
8
+
9
+ #endif
@@ -0,0 +1,96 @@
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 "aegis128x2.h"
11
+ # include "aegis128x2_soft.h"
12
+
13
+ # define AES_BLOCK_LENGTH 32
14
+
15
+ typedef struct {
16
+ SoftAesBlock b0;
17
+ SoftAesBlock b1;
18
+ } aes_block_t;
19
+
20
+ static inline aes_block_t
21
+ AES_BLOCK_XOR(const aes_block_t a, const aes_block_t b)
22
+ {
23
+ return (aes_block_t) { softaes_block_xor(a.b0, b.b0), softaes_block_xor(a.b1, b.b1) };
24
+ }
25
+
26
+ static inline aes_block_t
27
+ AES_BLOCK_AND(const aes_block_t a, const aes_block_t b)
28
+ {
29
+ return (aes_block_t) { softaes_block_and(a.b0, b.b0), softaes_block_and(a.b1, b.b1) };
30
+ }
31
+
32
+ static inline aes_block_t
33
+ AES_BLOCK_LOAD(const uint8_t *a)
34
+ {
35
+ return (aes_block_t) { softaes_block_load(a), softaes_block_load(a + 16) };
36
+ }
37
+
38
+ static inline aes_block_t
39
+ AES_BLOCK_LOAD_64x2(uint64_t a, uint64_t b)
40
+ {
41
+ const SoftAesBlock t = softaes_block_load64x2(a, b);
42
+ return (aes_block_t) { t, t };
43
+ }
44
+ static inline void
45
+ AES_BLOCK_STORE(uint8_t *a, const aes_block_t b)
46
+ {
47
+ softaes_block_store(a, b.b0);
48
+ softaes_block_store(a + 16, b.b1);
49
+ }
50
+
51
+ static inline aes_block_t
52
+ AES_ENC(const aes_block_t a, const aes_block_t b)
53
+ {
54
+ return (aes_block_t) { softaes_block_encrypt(a.b0, b.b0), softaes_block_encrypt(a.b1, b.b1) };
55
+ }
56
+
57
+ static inline void
58
+ aegis128x2_update(aes_block_t *const state, const aes_block_t d1, const aes_block_t d2)
59
+ {
60
+ aes_block_t tmp;
61
+
62
+ tmp = state[7];
63
+ state[7] = AES_ENC(state[6], state[7]);
64
+ state[6] = AES_ENC(state[5], state[6]);
65
+ state[5] = AES_ENC(state[4], state[5]);
66
+ state[4] = AES_ENC(state[3], state[4]);
67
+ state[3] = AES_ENC(state[2], state[3]);
68
+ state[2] = AES_ENC(state[1], state[2]);
69
+ state[1] = AES_ENC(state[0], state[1]);
70
+ state[0] = AES_ENC(tmp, state[0]);
71
+
72
+ state[0] = AES_BLOCK_XOR(state[0], d1);
73
+ state[4] = AES_BLOCK_XOR(state[4], d2);
74
+ }
75
+
76
+ # include "aegis128x2_common.h"
77
+
78
+ struct aegis128x2_implementation aegis128x2_soft_implementation = {
79
+ .encrypt_detached = encrypt_detached,
80
+ .decrypt_detached = decrypt_detached,
81
+ .encrypt_unauthenticated = encrypt_unauthenticated,
82
+ .decrypt_unauthenticated = decrypt_unauthenticated,
83
+ .stream = stream,
84
+ .state_init = state_init,
85
+ .state_encrypt_update = state_encrypt_update,
86
+ .state_encrypt_final = state_encrypt_final,
87
+ .state_decrypt_update = state_decrypt_update,
88
+ .state_decrypt_final = state_decrypt_final,
89
+ .state_mac_init = state_mac_init,
90
+ .state_mac_update = state_mac_update,
91
+ .state_mac_final = state_mac_final,
92
+ .state_mac_reset = state_mac_reset,
93
+ .state_mac_clone = state_mac_clone,
94
+ };
95
+
96
+ #endif
@@ -0,0 +1,9 @@
1
+ #ifndef aegis128x2_soft_H
2
+ #define aegis128x2_soft_H
3
+
4
+ #include "../common/common.h"
5
+ #include "implementations.h"
6
+
7
+ extern struct aegis128x2_implementation aegis128x2_soft_implementation;
8
+
9
+ #endif
@@ -0,0 +1,40 @@
1
+ #ifndef aegis128x2_implementations_H
2
+ #define aegis128x2_implementations_H
3
+
4
+ #include <stddef.h>
5
+ #include <stdint.h>
6
+
7
+ #include "aegis128x2.h"
8
+
9
+ /* Namespacing to avoid conflicts with libsodium */
10
+ #define aegis128x2_soft_implementation libaegis_aegis128x2_soft_implementation
11
+ #define aegis128x2_aesni_implementation libaegis_aegis128x2_aesni_implementation
12
+ #define aegis128x2_neon_aes_implementation libaegis_aegis128x2_neon_aes_implementation
13
+ #define aegis128x2_altivec_implementation libaegis_aegis128x2_altivec_implementation
14
+ #define aegis128x2_avx2_implementation libaegis_aegis128x2_avx2_implementation
15
+
16
+ typedef struct aegis128x2_implementation {
17
+ int (*encrypt_detached)(uint8_t *c, uint8_t *mac, size_t maclen, const uint8_t *m, size_t mlen,
18
+ const uint8_t *ad, size_t adlen, const uint8_t *npub, const uint8_t *k);
19
+ int (*decrypt_detached)(uint8_t *m, const uint8_t *c, size_t clen, const uint8_t *mac,
20
+ size_t maclen, const uint8_t *ad, size_t adlen, const uint8_t *npub,
21
+ const uint8_t *k);
22
+ void (*stream)(uint8_t *out, size_t len, const uint8_t *npub, const uint8_t *k);
23
+ void (*encrypt_unauthenticated)(uint8_t *c, const uint8_t *m, size_t mlen, const uint8_t *npub,
24
+ const uint8_t *k);
25
+ void (*decrypt_unauthenticated)(uint8_t *m, const uint8_t *c, size_t clen, const uint8_t *npub,
26
+ const uint8_t *k);
27
+ void (*state_init)(aegis128x2_state *st_, const uint8_t *ad, size_t adlen, const uint8_t *npub,
28
+ const uint8_t *k);
29
+ int (*state_encrypt_update)(aegis128x2_state *st_, uint8_t *c, const uint8_t *m, size_t mlen);
30
+ int (*state_encrypt_final)(aegis128x2_state *st_, uint8_t *mac, size_t maclen);
31
+ int (*state_decrypt_update)(aegis128x2_state *st_, uint8_t *m, const uint8_t *c, size_t clen);
32
+ int (*state_decrypt_final)(aegis128x2_state *st_, const uint8_t *mac, size_t maclen);
33
+ void (*state_mac_init)(aegis128x2_mac_state *st_, const uint8_t *npub, const uint8_t *k);
34
+ int (*state_mac_update)(aegis128x2_mac_state *st_, const uint8_t *ad, size_t adlen);
35
+ int (*state_mac_final)(aegis128x2_mac_state *st_, uint8_t *mac, size_t maclen);
36
+ void (*state_mac_reset)(aegis128x2_mac_state *st);
37
+ void (*state_mac_clone)(aegis128x2_mac_state *dst, const aegis128x2_mac_state *src);
38
+ } aegis128x2_implementation;
39
+
40
+ #endif
@@ -0,0 +1,252 @@
1
+ #include <stddef.h>
2
+ #include <stdint.h>
3
+
4
+ #include "../common/common.h"
5
+ #include "../common/cpu.h"
6
+ #include "aegis128x4.h"
7
+ #include "aegis128x4_aesni.h"
8
+ #include "aegis128x4_altivec.h"
9
+ #include "aegis128x4_avx2.h"
10
+ #include "aegis128x4_avx512.h"
11
+ #include "aegis128x4_neon_aes.h"
12
+
13
+ #ifndef HAS_HW_AES
14
+ # include "aegis128x4_soft.h"
15
+ static const aegis128x4_implementation *implementation = &aegis128x4_soft_implementation;
16
+ #else
17
+ # if defined(__aarch64__) || defined(_M_ARM64)
18
+ static const aegis128x4_implementation *implementation = &aegis128x4_neon_aes_implementation;
19
+ # elif defined(__x86_64__) || defined(__i386__)
20
+ static const aegis128x4_implementation *implementation = &aegis128x4_aesni_implementation;
21
+ # elif defined(__ALTIVEC__) && defined(__CRYPTO__)
22
+ static const aegis128x4_implementation *implementation = &aegis128x4_altivec_implementation;
23
+ # else
24
+ # error "Unsupported architecture"
25
+ # endif
26
+ #endif
27
+
28
+ size_t
29
+ aegis128x4_keybytes(void)
30
+ {
31
+ return aegis128x4_KEYBYTES;
32
+ }
33
+
34
+ size_t
35
+ aegis128x4_npubbytes(void)
36
+ {
37
+ return aegis128x4_NPUBBYTES;
38
+ }
39
+
40
+ size_t
41
+ aegis128x4_abytes_min(void)
42
+ {
43
+ return aegis128x4_ABYTES_MIN;
44
+ }
45
+
46
+ size_t
47
+ aegis128x4_abytes_max(void)
48
+ {
49
+ return aegis128x4_ABYTES_MAX;
50
+ }
51
+
52
+ size_t
53
+ aegis128x4_tailbytes_max(void)
54
+ {
55
+ return aegis128x4_TAILBYTES_MAX;
56
+ }
57
+
58
+ int
59
+ aegis128x4_encrypt_detached(uint8_t *c, uint8_t *mac, size_t maclen, const uint8_t *m, size_t mlen,
60
+ const uint8_t *ad, size_t adlen, const uint8_t *npub, const uint8_t *k)
61
+ {
62
+ if (maclen != 16 && maclen != 32) {
63
+ errno = EINVAL;
64
+ return -1;
65
+ }
66
+ return implementation->encrypt_detached(c, mac, maclen, m, mlen, ad, adlen, npub, k);
67
+ }
68
+
69
+ int
70
+ aegis128x4_decrypt_detached(uint8_t *m, const uint8_t *c, size_t clen, const uint8_t *mac,
71
+ size_t maclen, const uint8_t *ad, size_t adlen, const uint8_t *npub,
72
+ const uint8_t *k)
73
+ {
74
+ if (maclen != 16 && maclen != 32) {
75
+ errno = EINVAL;
76
+ return -1;
77
+ }
78
+ return implementation->decrypt_detached(m, c, clen, mac, maclen, ad, adlen, npub, k);
79
+ }
80
+
81
+ int
82
+ aegis128x4_encrypt(uint8_t *c, size_t maclen, const uint8_t *m, size_t mlen, const uint8_t *ad,
83
+ size_t adlen, const uint8_t *npub, const uint8_t *k)
84
+ {
85
+ return aegis128x4_encrypt_detached(c, c + mlen, maclen, m, mlen, ad, adlen, npub, k);
86
+ }
87
+
88
+ int
89
+ aegis128x4_decrypt(uint8_t *m, const uint8_t *c, size_t clen, size_t maclen, const uint8_t *ad,
90
+ size_t adlen, const uint8_t *npub, const uint8_t *k)
91
+ {
92
+ int ret = -1;
93
+
94
+ if (clen >= maclen) {
95
+ ret = aegis128x4_decrypt_detached(m, c, clen - maclen, c + clen - maclen, maclen, ad, adlen,
96
+ npub, k);
97
+ }
98
+ return ret;
99
+ }
100
+
101
+ void
102
+ aegis128x4_state_init(aegis128x4_state *st_, const uint8_t *ad, size_t adlen, const uint8_t *npub,
103
+ const uint8_t *k)
104
+ {
105
+ memset(st_, 0, sizeof *st_);
106
+ implementation->state_init(st_, ad, adlen, npub, k);
107
+ }
108
+
109
+ int
110
+ aegis128x4_state_encrypt_update(aegis128x4_state *st_, uint8_t *c, const uint8_t *m, size_t mlen)
111
+ {
112
+ return implementation->state_encrypt_update(st_, c, m, mlen);
113
+ }
114
+
115
+ int
116
+ aegis128x4_state_encrypt_final(aegis128x4_state *st_, uint8_t *mac, size_t maclen)
117
+ {
118
+ if (maclen != 16 && maclen != 32) {
119
+ errno = EINVAL;
120
+ return -1;
121
+ }
122
+ return implementation->state_encrypt_final(st_, mac, maclen);
123
+ }
124
+
125
+ int
126
+ aegis128x4_state_decrypt_update(aegis128x4_state *st_, uint8_t *m, const uint8_t *c, size_t clen)
127
+ {
128
+ return implementation->state_decrypt_update(st_, m, c, clen);
129
+ }
130
+
131
+ int
132
+ aegis128x4_state_decrypt_final(aegis128x4_state *st_, const uint8_t *mac, size_t maclen)
133
+ {
134
+ if (maclen != 16 && maclen != 32) {
135
+ errno = EINVAL;
136
+ return -1;
137
+ }
138
+ return implementation->state_decrypt_final(st_, mac, maclen);
139
+ }
140
+
141
+ void
142
+ aegis128x4_stream(uint8_t *out, size_t len, const uint8_t *npub, const uint8_t *k)
143
+ {
144
+ implementation->stream(out, len, npub, k);
145
+ }
146
+
147
+ void
148
+ aegis128x4_encrypt_unauthenticated(uint8_t *c, const uint8_t *m, size_t mlen, const uint8_t *npub,
149
+ const uint8_t *k)
150
+ {
151
+ implementation->encrypt_unauthenticated(c, m, mlen, npub, k);
152
+ }
153
+
154
+ void
155
+ aegis128x4_decrypt_unauthenticated(uint8_t *m, const uint8_t *c, size_t clen, const uint8_t *npub,
156
+ const uint8_t *k)
157
+ {
158
+ implementation->decrypt_unauthenticated(m, c, clen, npub, k);
159
+ }
160
+
161
+ void
162
+ aegis128x4_mac_init(aegis128x4_mac_state *st_, const uint8_t *k, const uint8_t *npub)
163
+ {
164
+ implementation->state_mac_init(st_, npub, k);
165
+ }
166
+
167
+ int
168
+ aegis128x4_mac_update(aegis128x4_mac_state *st_, const uint8_t *m, size_t mlen)
169
+ {
170
+ return implementation->state_mac_update(st_, m, mlen);
171
+ }
172
+
173
+ int
174
+ aegis128x4_mac_final(aegis128x4_mac_state *st_, uint8_t *mac, size_t maclen)
175
+ {
176
+ if (maclen != 16 && maclen != 32) {
177
+ errno = EINVAL;
178
+ return -1;
179
+ }
180
+ return implementation->state_mac_final(st_, mac, maclen);
181
+ }
182
+
183
+ int
184
+ aegis128x4_mac_verify(aegis128x4_mac_state *st_, const uint8_t *mac, size_t maclen)
185
+ {
186
+ uint8_t expected_mac[32];
187
+
188
+ switch (maclen) {
189
+ case 16:
190
+ implementation->state_mac_final(st_, expected_mac, maclen);
191
+ return aegis_verify_16(expected_mac, mac);
192
+ case 32:
193
+ implementation->state_mac_final(st_, expected_mac, maclen);
194
+ return aegis_verify_32(expected_mac, mac);
195
+ default:
196
+ errno = EINVAL;
197
+ return -1;
198
+ }
199
+ }
200
+
201
+ void
202
+ aegis128x4_mac_reset(aegis128x4_mac_state *st_)
203
+ {
204
+ implementation->state_mac_reset(st_);
205
+ }
206
+
207
+ void
208
+ aegis128x4_mac_state_clone(aegis128x4_mac_state *dst, const aegis128x4_mac_state *src)
209
+ {
210
+ implementation->state_mac_clone(dst, src);
211
+ }
212
+
213
+ int
214
+ aegis128x4_pick_best_implementation(void)
215
+ {
216
+ #ifndef HAS_HW_AES
217
+ implementation = &aegis128x4_soft_implementation;
218
+ #endif
219
+
220
+ #if defined(__aarch64__) || defined(_M_ARM64)
221
+ if (aegis_runtime_has_neon_aes()) {
222
+ implementation = &aegis128x4_neon_aes_implementation;
223
+ return 0;
224
+ }
225
+ #endif
226
+
227
+ #if defined(__x86_64__) || defined(_M_AMD64) || defined(__i386__) || defined(_M_IX86)
228
+ # ifdef HAVE_VAESINTRIN_H
229
+ if (aegis_runtime_has_vaes() && aegis_runtime_has_avx512f()) {
230
+ implementation = &aegis128x4_avx512_implementation;
231
+ return 0;
232
+ }
233
+ if (aegis_runtime_has_vaes() && aegis_runtime_has_avx2()) {
234
+ implementation = &aegis128x4_avx2_implementation;
235
+ return 0;
236
+ }
237
+ # endif
238
+ if (aegis_runtime_has_aesni() && aegis_runtime_has_avx()) {
239
+ implementation = &aegis128x4_aesni_implementation;
240
+ return 0;
241
+ }
242
+ #endif
243
+
244
+ #if defined(__ALTIVEC__) && defined(__CRYPTO__)
245
+ if (aegis_runtime_has_altivec()) {
246
+ implementation = &aegis128x4_altivec_implementation;
247
+ return 0;
248
+ }
249
+ #endif
250
+
251
+ return 0; /* LCOV_EXCL_LINE */
252
+ }
@@ -0,0 +1,117 @@
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 "aegis128x4.h"
8
+ # include "aegis128x4_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 64
20
+
21
+ typedef struct {
22
+ __m128i b0;
23
+ __m128i b1;
24
+ __m128i b2;
25
+ __m128i b3;
26
+ } aes_block_t;
27
+
28
+ static inline aes_block_t
29
+ AES_BLOCK_XOR(const aes_block_t a, const aes_block_t b)
30
+ {
31
+ return (aes_block_t) { _mm_xor_si128(a.b0, b.b0), _mm_xor_si128(a.b1, b.b1),
32
+ _mm_xor_si128(a.b2, b.b2), _mm_xor_si128(a.b3, b.b3) };
33
+ }
34
+
35
+ static inline aes_block_t
36
+ AES_BLOCK_AND(const aes_block_t a, const aes_block_t b)
37
+ {
38
+ return (aes_block_t) { _mm_and_si128(a.b0, b.b0), _mm_and_si128(a.b1, b.b1),
39
+ _mm_and_si128(a.b2, b.b2), _mm_and_si128(a.b3, b.b3) };
40
+ }
41
+
42
+ static inline aes_block_t
43
+ AES_BLOCK_LOAD(const uint8_t *a)
44
+ {
45
+ return (aes_block_t) { _mm_loadu_si128((const __m128i *) (const void *) a),
46
+ _mm_loadu_si128((const __m128i *) (const void *) (a + 16)),
47
+ _mm_loadu_si128((const __m128i *) (const void *) (a + 32)),
48
+ _mm_loadu_si128((const __m128i *) (const void *) (a + 48)) };
49
+ }
50
+
51
+ static inline aes_block_t
52
+ AES_BLOCK_LOAD_64x2(uint64_t a, uint64_t b)
53
+ {
54
+ const __m128i t = _mm_set_epi64x((long long) a, (long long) b);
55
+ return (aes_block_t) { t, t, t, t };
56
+ }
57
+
58
+ static inline void
59
+ AES_BLOCK_STORE(uint8_t *a, const aes_block_t b)
60
+ {
61
+ _mm_storeu_si128((__m128i *) (void *) a, b.b0);
62
+ _mm_storeu_si128((__m128i *) (void *) (a + 16), b.b1);
63
+ _mm_storeu_si128((__m128i *) (void *) (a + 32), b.b2);
64
+ _mm_storeu_si128((__m128i *) (void *) (a + 48), b.b3);
65
+ }
66
+
67
+ static inline aes_block_t
68
+ AES_ENC(const aes_block_t a, const aes_block_t b)
69
+ {
70
+ return (aes_block_t) { _mm_aesenc_si128(a.b0, b.b0), _mm_aesenc_si128(a.b1, b.b1),
71
+ _mm_aesenc_si128(a.b2, b.b2), _mm_aesenc_si128(a.b3, b.b3) };
72
+ }
73
+
74
+ static inline void
75
+ aegis128x4_update(aes_block_t *const state, const aes_block_t d1, const aes_block_t d2)
76
+ {
77
+ aes_block_t tmp;
78
+
79
+ tmp = state[7];
80
+ state[7] = AES_ENC(state[6], state[7]);
81
+ state[6] = AES_ENC(state[5], state[6]);
82
+ state[5] = AES_ENC(state[4], state[5]);
83
+ state[4] = AES_ENC(state[3], state[4]);
84
+ state[3] = AES_ENC(state[2], state[3]);
85
+ state[2] = AES_ENC(state[1], state[2]);
86
+ state[1] = AES_ENC(state[0], state[1]);
87
+ state[0] = AES_ENC(tmp, state[0]);
88
+
89
+ state[0] = AES_BLOCK_XOR(state[0], d1);
90
+ state[4] = AES_BLOCK_XOR(state[4], d2);
91
+ }
92
+
93
+ # include "aegis128x4_common.h"
94
+
95
+ struct aegis128x4_implementation aegis128x4_aesni_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,9 @@
1
+ #ifndef aegis128x4_aesni_H
2
+ #define aegis128x4_aesni_H
3
+
4
+ #include "../common/common.h"
5
+ #include "implementations.h"
6
+
7
+ extern struct aegis128x4_implementation aegis128x4_aesni_implementation;
8
+
9
+ #endif