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,77 @@
1
+ #if defined(__aarch64__) || defined(_M_ARM64)
2
+
3
+ # include <stddef.h>
4
+ # include <stdint.h>
5
+
6
+ # include "../common/common.h"
7
+ # include "aegis128l.h"
8
+ # include "aegis128l_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 16
27
+
28
+ typedef uint8x16_t aes_block_t;
29
+
30
+ # define AES_BLOCK_XOR(A, B) veorq_u8((A), (B))
31
+ # define AES_BLOCK_AND(A, B) vandq_u8((A), (B))
32
+ # define AES_BLOCK_LOAD(A) vld1q_u8(A)
33
+ # define AES_BLOCK_LOAD_64x2(A, B) vreinterpretq_u8_u64(vsetq_lane_u64((A), vmovq_n_u64(B), 1))
34
+ # define AES_BLOCK_STORE(A, B) vst1q_u8((A), (B))
35
+ # define AES_ENC(A, B) veorq_u8(vaesmcq_u8(vaeseq_u8(vmovq_n_u8(0), (A))), (B))
36
+
37
+ static inline void
38
+ aegis128l_update(aes_block_t *const state, const aes_block_t d1, const aes_block_t d2)
39
+ {
40
+ aes_block_t tmp;
41
+
42
+ tmp = state[7];
43
+ state[7] = AES_ENC(state[6], state[7]);
44
+ state[6] = AES_ENC(state[5], state[6]);
45
+ state[5] = AES_ENC(state[4], state[5]);
46
+ state[4] = AES_BLOCK_XOR(AES_ENC(state[3], state[4]), d2);
47
+ state[3] = AES_ENC(state[2], state[3]);
48
+ state[2] = AES_ENC(state[1], state[2]);
49
+ state[1] = AES_ENC(state[0], state[1]);
50
+ state[0] = AES_BLOCK_XOR(AES_ENC(tmp, state[0]), d1);
51
+ }
52
+
53
+ # include "aegis128l_common.h"
54
+
55
+ struct aegis128l_implementation aegis128l_neon_aes_implementation = {
56
+ .encrypt_detached = encrypt_detached,
57
+ .decrypt_detached = decrypt_detached,
58
+ .encrypt_unauthenticated = encrypt_unauthenticated,
59
+ .decrypt_unauthenticated = decrypt_unauthenticated,
60
+ .stream = stream,
61
+ .state_init = state_init,
62
+ .state_encrypt_update = state_encrypt_update,
63
+ .state_encrypt_final = state_encrypt_final,
64
+ .state_decrypt_update = state_decrypt_update,
65
+ .state_decrypt_final = state_decrypt_final,
66
+ .state_mac_init = state_mac_init,
67
+ .state_mac_update = state_mac_update,
68
+ .state_mac_final = state_mac_final,
69
+ .state_mac_reset = state_mac_reset,
70
+ .state_mac_clone = state_mac_clone,
71
+ };
72
+
73
+ # ifdef __clang__
74
+ # pragma clang attribute pop
75
+ # endif
76
+
77
+ #endif
@@ -0,0 +1,9 @@
1
+ #ifndef aegis128l_neon_aes_H
2
+ #define aegis128l_neon_aes_H
3
+
4
+ #include "../common/common.h"
5
+ #include "implementations.h"
6
+
7
+ extern struct aegis128l_implementation aegis128l_neon_aes_implementation;
8
+
9
+ #endif
@@ -0,0 +1,86 @@
1
+ #if defined(__aarch64__) || defined(_M_ARM64)
2
+
3
+ # include <stddef.h>
4
+ # include <stdint.h>
5
+
6
+ # include "../common/common.h"
7
+ # include "aegis128l.h"
8
+ # include "aegis128l_neon_sha3.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
+ # ifndef __ARM_FEATURE_SHA3
17
+ # define __ARM_FEATURE_SHA3 1
18
+ # endif
19
+
20
+ # include <arm_neon.h>
21
+
22
+ # ifdef __clang__
23
+ # pragma clang attribute push(__attribute__((target("neon,crypto,aes,sha3"))), \
24
+ apply_to = function)
25
+ # elif defined(__GNUC__)
26
+ # if __GNUC__ < 14
27
+ # pragma GCC target("arch=armv8.2-a+simd+crypto+sha3")
28
+ # else
29
+ # pragma GCC target("+simd+crypto+sha3")
30
+ # endif
31
+ # endif
32
+
33
+ # define AES_BLOCK_LENGTH 16
34
+
35
+ typedef uint8x16_t aes_block_t;
36
+
37
+ # define AES_BLOCK_XOR(A, B) veorq_u8((A), (B))
38
+ # define AES_BLOCK_XOR3(A, B, C) veor3q_u8((A), (B), (C))
39
+ # define AES_BLOCK_AND(A, B) vandq_u8((A), (B))
40
+ # define AES_BLOCK_LOAD(A) vld1q_u8(A)
41
+ # define AES_BLOCK_LOAD_64x2(A, B) vreinterpretq_u8_u64(vsetq_lane_u64((A), vmovq_n_u64(B), 1))
42
+ # define AES_BLOCK_STORE(A, B) vst1q_u8((A), (B))
43
+ # define AES_ENC0(A) vaesmcq_u8(vaeseq_u8(vmovq_n_u8(0), (A)))
44
+ # define AES_ENC(A, B) AES_BLOCK_XOR(AES_ENC0(A), (B))
45
+
46
+ static inline void
47
+ aegis128l_update(aes_block_t *const state, const aes_block_t d1, const aes_block_t d2)
48
+ {
49
+ aes_block_t tmp;
50
+
51
+ tmp = state[7];
52
+ state[7] = AES_ENC(state[6], state[7]);
53
+ state[6] = AES_ENC(state[5], state[6]);
54
+ state[5] = AES_ENC(state[4], state[5]);
55
+ state[4] = AES_BLOCK_XOR3(state[4], AES_ENC0(state[3]), d2);
56
+ state[3] = AES_ENC(state[2], state[3]);
57
+ state[2] = AES_ENC(state[1], state[2]);
58
+ state[1] = AES_ENC(state[0], state[1]);
59
+ state[0] = AES_BLOCK_XOR3(state[0], AES_ENC0(tmp), d1);
60
+ }
61
+
62
+ # include "aegis128l_common.h"
63
+
64
+ struct aegis128l_implementation aegis128l_neon_sha3_implementation = {
65
+ .encrypt_detached = encrypt_detached,
66
+ .decrypt_detached = decrypt_detached,
67
+ .encrypt_unauthenticated = encrypt_unauthenticated,
68
+ .decrypt_unauthenticated = decrypt_unauthenticated,
69
+ .stream = stream,
70
+ .state_init = state_init,
71
+ .state_encrypt_update = state_encrypt_update,
72
+ .state_encrypt_final = state_encrypt_final,
73
+ .state_decrypt_update = state_decrypt_update,
74
+ .state_decrypt_final = state_decrypt_final,
75
+ .state_mac_init = state_mac_init,
76
+ .state_mac_update = state_mac_update,
77
+ .state_mac_final = state_mac_final,
78
+ .state_mac_reset = state_mac_reset,
79
+ .state_mac_clone = state_mac_clone,
80
+ };
81
+
82
+ # ifdef __clang__
83
+ # pragma clang attribute pop
84
+ # endif
85
+
86
+ #endif
@@ -0,0 +1,9 @@
1
+ #ifndef aegis128l_neon_sha3_H
2
+ #define aegis128l_neon_sha3_H
3
+
4
+ #include "../common/common.h"
5
+ #include "implementations.h"
6
+
7
+ extern struct aegis128l_implementation aegis128l_neon_sha3_implementation;
8
+
9
+ #endif
@@ -0,0 +1,63 @@
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 "aegis128l.h"
11
+ # include "aegis128l_soft.h"
12
+
13
+ # define AES_BLOCK_LENGTH 16
14
+
15
+ typedef SoftAesBlock aes_block_t;
16
+
17
+ # define AES_BLOCK_XOR(A, B) softaes_block_xor((A), (B))
18
+ # define AES_BLOCK_AND(A, B) softaes_block_and((A), (B))
19
+ # define AES_BLOCK_LOAD(A) softaes_block_load(A)
20
+ # define AES_BLOCK_LOAD_64x2(A, B) softaes_block_load64x2((A), (B))
21
+ # define AES_BLOCK_STORE(A, B) softaes_block_store((A), (B))
22
+ # define AES_ENC(A, B) softaes_block_encrypt((A), (B))
23
+
24
+ static inline void
25
+ aegis128l_update(aes_block_t *const state, const aes_block_t d1, const aes_block_t d2)
26
+ {
27
+ aes_block_t tmp;
28
+
29
+ tmp = state[7];
30
+ state[7] = AES_ENC(state[6], state[7]);
31
+ state[6] = AES_ENC(state[5], state[6]);
32
+ state[5] = AES_ENC(state[4], state[5]);
33
+ state[4] = AES_ENC(state[3], state[4]);
34
+ state[3] = AES_ENC(state[2], state[3]);
35
+ state[2] = AES_ENC(state[1], state[2]);
36
+ state[1] = AES_ENC(state[0], state[1]);
37
+ state[0] = AES_ENC(tmp, state[0]);
38
+
39
+ state[0] = AES_BLOCK_XOR(state[0], d1);
40
+ state[4] = AES_BLOCK_XOR(state[4], d2);
41
+ }
42
+
43
+ # include "aegis128l_common.h"
44
+
45
+ struct aegis128l_implementation aegis128l_soft_implementation = {
46
+ .encrypt_detached = encrypt_detached,
47
+ .decrypt_detached = decrypt_detached,
48
+ .encrypt_unauthenticated = encrypt_unauthenticated,
49
+ .decrypt_unauthenticated = decrypt_unauthenticated,
50
+ .stream = stream,
51
+ .state_init = state_init,
52
+ .state_encrypt_update = state_encrypt_update,
53
+ .state_encrypt_final = state_encrypt_final,
54
+ .state_decrypt_update = state_decrypt_update,
55
+ .state_decrypt_final = state_decrypt_final,
56
+ .state_mac_init = state_mac_init,
57
+ .state_mac_update = state_mac_update,
58
+ .state_mac_final = state_mac_final,
59
+ .state_mac_reset = state_mac_reset,
60
+ .state_mac_clone = state_mac_clone,
61
+ };
62
+
63
+ #endif
@@ -0,0 +1,9 @@
1
+ #ifndef aegis128l_soft_H
2
+ #define aegis128l_soft_H
3
+
4
+ #include "../common/common.h"
5
+ #include "implementations.h"
6
+
7
+ extern struct aegis128l_implementation aegis128l_soft_implementation;
8
+
9
+ #endif
@@ -0,0 +1,40 @@
1
+ #ifndef aegis128l_implementations_H
2
+ #define aegis128l_implementations_H
3
+
4
+ #include <stddef.h>
5
+ #include <stdint.h>
6
+
7
+ #include "aegis128l.h"
8
+
9
+ /* Namespacing to avoid conflicts with libsodium */
10
+ #define aegis128l_soft_implementation libaegis_aegis128l_soft_implementation
11
+ #define aegis128l_aesni_implementation libaegis_aegis128l_aesni_implementation
12
+ #define aegis128l_neon_aes_implementation libaegis_aegis128l_neon_aes_implementation
13
+ #define aegis128l_neon_sha3_implementation libaegis_aegis128l_neon_sha3_implementation
14
+ #define aegis128l_altivec_implementation libaegis_aegis128l_altivec_implementation
15
+
16
+ typedef struct aegis128l_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)(aegis128l_state *st_, const uint8_t *ad, size_t adlen, const uint8_t *npub,
28
+ const uint8_t *k);
29
+ int (*state_encrypt_update)(aegis128l_state *st_, uint8_t *c, const uint8_t *m, size_t mlen);
30
+ int (*state_encrypt_final)(aegis128l_state *st_, uint8_t *mac, size_t maclen);
31
+ int (*state_decrypt_update)(aegis128l_state *st_, uint8_t *m, const uint8_t *c, size_t clen);
32
+ int (*state_decrypt_final)(aegis128l_state *st_, const uint8_t *mac, size_t maclen);
33
+ void (*state_mac_init)(aegis128l_mac_state *st_, const uint8_t *npub, const uint8_t *k);
34
+ int (*state_mac_update)(aegis128l_mac_state *st_, const uint8_t *ad, size_t adlen);
35
+ int (*state_mac_final)(aegis128l_mac_state *st_, uint8_t *mac, size_t maclen);
36
+ void (*state_mac_reset)(aegis128l_mac_state *st);
37
+ void (*state_mac_clone)(aegis128l_mac_state *dst, const aegis128l_mac_state *src);
38
+ } aegis128l_implementation;
39
+
40
+ #endif
@@ -0,0 +1,247 @@
1
+ #include <stddef.h>
2
+ #include <stdint.h>
3
+
4
+ #include "../common/common.h"
5
+ #include "../common/cpu.h"
6
+ #include "aegis128x2.h"
7
+ #include "aegis128x2_aesni.h"
8
+ #include "aegis128x2_altivec.h"
9
+ #include "aegis128x2_avx2.h"
10
+ #include "aegis128x2_neon_aes.h"
11
+
12
+ #ifndef HAS_HW_AES
13
+ # include "aegis128x2_soft.h"
14
+ static const aegis128x2_implementation *implementation = &aegis128x2_soft_implementation;
15
+ #else
16
+ # if defined(__aarch64__) || defined(_M_ARM64)
17
+ static const aegis128x2_implementation *implementation = &aegis128x2_neon_aes_implementation;
18
+ # elif defined(__x86_64__) || defined(__i386__)
19
+ static const aegis128x2_implementation *implementation = &aegis128x2_aesni_implementation;
20
+ # elif defined(__ALTIVEC__) && defined(__CRYPTO__)
21
+ static const aegis128x2_implementation *implementation = &aegis128x2_altivec_implementation;
22
+ # else
23
+ # error "Unsupported architecture"
24
+ # endif
25
+ #endif
26
+
27
+ size_t
28
+ aegis128x2_keybytes(void)
29
+ {
30
+ return aegis128x2_KEYBYTES;
31
+ }
32
+
33
+ size_t
34
+ aegis128x2_npubbytes(void)
35
+ {
36
+ return aegis128x2_NPUBBYTES;
37
+ }
38
+
39
+ size_t
40
+ aegis128x2_abytes_min(void)
41
+ {
42
+ return aegis128x2_ABYTES_MIN;
43
+ }
44
+
45
+ size_t
46
+ aegis128x2_abytes_max(void)
47
+ {
48
+ return aegis128x2_ABYTES_MAX;
49
+ }
50
+
51
+ size_t
52
+ aegis128x2_tailbytes_max(void)
53
+ {
54
+ return aegis128x2_TAILBYTES_MAX;
55
+ }
56
+
57
+ int
58
+ aegis128x2_encrypt_detached(uint8_t *c, uint8_t *mac, size_t maclen, const uint8_t *m, size_t mlen,
59
+ const uint8_t *ad, size_t adlen, const uint8_t *npub, const uint8_t *k)
60
+ {
61
+ if (maclen != 16 && maclen != 32) {
62
+ errno = EINVAL;
63
+ return -1;
64
+ }
65
+ return implementation->encrypt_detached(c, mac, maclen, m, mlen, ad, adlen, npub, k);
66
+ }
67
+
68
+ int
69
+ aegis128x2_decrypt_detached(uint8_t *m, const uint8_t *c, size_t clen, const uint8_t *mac,
70
+ size_t maclen, const uint8_t *ad, size_t adlen, const uint8_t *npub,
71
+ const uint8_t *k)
72
+ {
73
+ if (maclen != 16 && maclen != 32) {
74
+ errno = EINVAL;
75
+ return -1;
76
+ }
77
+ return implementation->decrypt_detached(m, c, clen, mac, maclen, ad, adlen, npub, k);
78
+ }
79
+
80
+ int
81
+ aegis128x2_encrypt(uint8_t *c, size_t maclen, const uint8_t *m, size_t mlen, const uint8_t *ad,
82
+ size_t adlen, const uint8_t *npub, const uint8_t *k)
83
+ {
84
+ return aegis128x2_encrypt_detached(c, c + mlen, maclen, m, mlen, ad, adlen, npub, k);
85
+ }
86
+
87
+ int
88
+ aegis128x2_decrypt(uint8_t *m, const uint8_t *c, size_t clen, size_t maclen, const uint8_t *ad,
89
+ size_t adlen, const uint8_t *npub, const uint8_t *k)
90
+ {
91
+ int ret = -1;
92
+
93
+ if (clen >= maclen) {
94
+ ret = aegis128x2_decrypt_detached(m, c, clen - maclen, c + clen - maclen, maclen, ad, adlen,
95
+ npub, k);
96
+ }
97
+ return ret;
98
+ }
99
+
100
+ void
101
+ aegis128x2_state_init(aegis128x2_state *st_, const uint8_t *ad, size_t adlen, const uint8_t *npub,
102
+ const uint8_t *k)
103
+ {
104
+ memset(st_, 0, sizeof *st_);
105
+ implementation->state_init(st_, ad, adlen, npub, k);
106
+ }
107
+
108
+ int
109
+ aegis128x2_state_encrypt_update(aegis128x2_state *st_, uint8_t *c, const uint8_t *m, size_t mlen)
110
+ {
111
+ return implementation->state_encrypt_update(st_, c, m, mlen);
112
+ }
113
+
114
+ int
115
+ aegis128x2_state_encrypt_final(aegis128x2_state *st_, uint8_t *mac, size_t maclen)
116
+ {
117
+ if (maclen != 16 && maclen != 32) {
118
+ errno = EINVAL;
119
+ return -1;
120
+ }
121
+ return implementation->state_encrypt_final(st_, mac, maclen);
122
+ }
123
+
124
+ int
125
+ aegis128x2_state_decrypt_update(aegis128x2_state *st_, uint8_t *m, const uint8_t *c, size_t clen)
126
+ {
127
+ return implementation->state_decrypt_update(st_, m, c, clen);
128
+ }
129
+
130
+ int
131
+ aegis128x2_state_decrypt_final(aegis128x2_state *st_, const uint8_t *mac, size_t maclen)
132
+ {
133
+ if (maclen != 16 && maclen != 32) {
134
+ errno = EINVAL;
135
+ return -1;
136
+ }
137
+ return implementation->state_decrypt_final(st_, mac, maclen);
138
+ }
139
+
140
+ void
141
+ aegis128x2_stream(uint8_t *out, size_t len, const uint8_t *npub, const uint8_t *k)
142
+ {
143
+ implementation->stream(out, len, npub, k);
144
+ }
145
+
146
+ void
147
+ aegis128x2_encrypt_unauthenticated(uint8_t *c, const uint8_t *m, size_t mlen, const uint8_t *npub,
148
+ const uint8_t *k)
149
+ {
150
+ implementation->encrypt_unauthenticated(c, m, mlen, npub, k);
151
+ }
152
+
153
+ void
154
+ aegis128x2_decrypt_unauthenticated(uint8_t *m, const uint8_t *c, size_t clen, const uint8_t *npub,
155
+ const uint8_t *k)
156
+ {
157
+ implementation->decrypt_unauthenticated(m, c, clen, npub, k);
158
+ }
159
+
160
+ void
161
+ aegis128x2_mac_init(aegis128x2_mac_state *st_, const uint8_t *k, const uint8_t *npub)
162
+ {
163
+ implementation->state_mac_init(st_, npub, k);
164
+ }
165
+
166
+ int
167
+ aegis128x2_mac_update(aegis128x2_mac_state *st_, const uint8_t *m, size_t mlen)
168
+ {
169
+ return implementation->state_mac_update(st_, m, mlen);
170
+ }
171
+
172
+ int
173
+ aegis128x2_mac_final(aegis128x2_mac_state *st_, uint8_t *mac, size_t maclen)
174
+ {
175
+ if (maclen != 16 && maclen != 32) {
176
+ errno = EINVAL;
177
+ return -1;
178
+ }
179
+ return implementation->state_mac_final(st_, mac, maclen);
180
+ }
181
+
182
+ int
183
+ aegis128x2_mac_verify(aegis128x2_mac_state *st_, const uint8_t *mac, size_t maclen)
184
+ {
185
+ uint8_t expected_mac[32];
186
+
187
+ switch (maclen) {
188
+ case 16:
189
+ implementation->state_mac_final(st_, expected_mac, maclen);
190
+ return aegis_verify_16(expected_mac, mac);
191
+ case 32:
192
+ implementation->state_mac_final(st_, expected_mac, maclen);
193
+ return aegis_verify_32(expected_mac, mac);
194
+ default:
195
+ errno = EINVAL;
196
+ return -1;
197
+ }
198
+ }
199
+
200
+ void
201
+ aegis128x2_mac_reset(aegis128x2_mac_state *st_)
202
+ {
203
+ implementation->state_mac_reset(st_);
204
+ }
205
+
206
+ void
207
+ aegis128x2_mac_state_clone(aegis128x2_mac_state *dst, const aegis128x2_mac_state *src)
208
+ {
209
+ implementation->state_mac_clone(dst, src);
210
+ }
211
+
212
+ int
213
+ aegis128x2_pick_best_implementation(void)
214
+ {
215
+ #ifndef HAS_HW_AES
216
+ implementation = &aegis128x2_soft_implementation;
217
+ #endif
218
+
219
+ #if defined(__aarch64__) || defined(_M_ARM64)
220
+ if (aegis_runtime_has_neon_aes()) {
221
+ implementation = &aegis128x2_neon_aes_implementation;
222
+ return 0;
223
+ }
224
+ #endif
225
+
226
+ #if defined(__x86_64__) || defined(_M_AMD64) || defined(__i386__) || defined(_M_IX86)
227
+ # ifdef HAVE_VAESINTRIN_H
228
+ if (aegis_runtime_has_vaes() && aegis_runtime_has_avx2()) {
229
+ implementation = &aegis128x2_avx2_implementation;
230
+ return 0;
231
+ }
232
+ # endif
233
+ if (aegis_runtime_has_aesni() && aegis_runtime_has_avx()) {
234
+ implementation = &aegis128x2_aesni_implementation;
235
+ return 0;
236
+ }
237
+ #endif
238
+
239
+ #if defined(__ALTIVEC__) && defined(__CRYPTO__)
240
+ if (aegis_runtime_has_altivec()) {
241
+ implementation = &aegis128x2_altivec_implementation;
242
+ return 0;
243
+ }
244
+ #endif
245
+
246
+ return 0; /* LCOV_EXCL_LINE */
247
+ }
@@ -0,0 +1,108 @@
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 "aegis128x2.h"
8
+ # include "aegis128x2_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 32
20
+
21
+ typedef struct {
22
+ __m128i b0;
23
+ __m128i b1;
24
+ } aes_block_t;
25
+
26
+ static inline aes_block_t
27
+ AES_BLOCK_XOR(const aes_block_t a, const aes_block_t b)
28
+ {
29
+ return (aes_block_t) { _mm_xor_si128(a.b0, b.b0), _mm_xor_si128(a.b1, b.b1) };
30
+ }
31
+
32
+ static inline aes_block_t
33
+ AES_BLOCK_AND(const aes_block_t a, const aes_block_t b)
34
+ {
35
+ return (aes_block_t) { _mm_and_si128(a.b0, b.b0), _mm_and_si128(a.b1, b.b1) };
36
+ }
37
+
38
+ static inline aes_block_t
39
+ AES_BLOCK_LOAD(const uint8_t *a)
40
+ {
41
+ return (aes_block_t) { _mm_loadu_si128((const __m128i *) (const void *) a),
42
+ _mm_loadu_si128((const __m128i *) (const void *) (a + 16)) };
43
+ }
44
+
45
+ static inline aes_block_t
46
+ AES_BLOCK_LOAD_64x2(uint64_t a, uint64_t b)
47
+ {
48
+ const __m128i t = _mm_set_epi64x((long long) a, (long long) b);
49
+ return (aes_block_t) { t, t };
50
+ }
51
+
52
+ static inline void
53
+ AES_BLOCK_STORE(uint8_t *a, const aes_block_t b)
54
+ {
55
+ _mm_storeu_si128((__m128i *) (void *) a, b.b0);
56
+ _mm_storeu_si128((__m128i *) (void *) (a + 16), b.b1);
57
+ }
58
+
59
+ static inline aes_block_t
60
+ AES_ENC(const aes_block_t a, const aes_block_t b)
61
+ {
62
+ return (aes_block_t) { _mm_aesenc_si128(a.b0, b.b0), _mm_aesenc_si128(a.b1, b.b1) };
63
+ }
64
+
65
+ static inline void
66
+ aegis128x2_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 "aegis128x2_common.h"
85
+
86
+ struct aegis128x2_implementation aegis128x2_aesni_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
+ # ifdef __clang__
105
+ # pragma clang attribute pop
106
+ # endif
107
+
108
+ #endif
@@ -0,0 +1,9 @@
1
+ #ifndef aegis128x2_aesni_H
2
+ #define aegis128x2_aesni_H
3
+
4
+ #include "../common/common.h"
5
+ #include "implementations.h"
6
+
7
+ extern struct aegis128x2_implementation aegis128x2_aesni_implementation;
8
+
9
+ #endif