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,75 @@
1
+ #if defined(__aarch64__) || defined(_M_ARM64)
2
+
3
+ # include <stddef.h>
4
+ # include <stdint.h>
5
+
6
+ # include "../common/common.h"
7
+ # include "aegis256.h"
8
+ # include "aegis256_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
+ aegis256_update(aes_block_t *const state, const aes_block_t d)
39
+ {
40
+ aes_block_t tmp;
41
+
42
+ tmp = state[5];
43
+ state[5] = AES_ENC(state[4], state[5]);
44
+ state[4] = AES_ENC(state[3], state[4]);
45
+ state[3] = AES_ENC(state[2], state[3]);
46
+ state[2] = AES_ENC(state[1], state[2]);
47
+ state[1] = AES_ENC(state[0], state[1]);
48
+ state[0] = AES_BLOCK_XOR(AES_ENC(tmp, state[0]), d);
49
+ }
50
+
51
+ # include "aegis256_common.h"
52
+
53
+ struct aegis256_implementation aegis256_neon_aes_implementation = {
54
+ .encrypt_detached = encrypt_detached,
55
+ .decrypt_detached = decrypt_detached,
56
+ .encrypt_unauthenticated = encrypt_unauthenticated,
57
+ .decrypt_unauthenticated = decrypt_unauthenticated,
58
+ .stream = stream,
59
+ .state_init = state_init,
60
+ .state_encrypt_update = state_encrypt_update,
61
+ .state_encrypt_final = state_encrypt_final,
62
+ .state_decrypt_update = state_decrypt_update,
63
+ .state_decrypt_final = state_decrypt_final,
64
+ .state_mac_init = state_mac_init,
65
+ .state_mac_update = state_mac_update,
66
+ .state_mac_final = state_mac_final,
67
+ .state_mac_reset = state_mac_reset,
68
+ .state_mac_clone = state_mac_clone,
69
+ };
70
+
71
+ # ifdef __clang__
72
+ # pragma clang attribute pop
73
+ # endif
74
+
75
+ #endif
@@ -0,0 +1,9 @@
1
+ #ifndef aegis256_neon_aes_H
2
+ #define aegis256_neon_aes_H
3
+
4
+ #include "../common/common.h"
5
+ #include "implementations.h"
6
+
7
+ extern struct aegis256_implementation aegis256_neon_aes_implementation;
8
+
9
+ #endif
@@ -0,0 +1,58 @@
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 "aegis256.h"
11
+ # include "aegis256_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
+ aegis256_update(aes_block_t *const state, const aes_block_t d)
26
+ {
27
+ aes_block_t tmp;
28
+
29
+ tmp = state[5];
30
+ state[5] = AES_ENC(state[4], state[5]);
31
+ state[4] = AES_ENC(state[3], state[4]);
32
+ state[3] = AES_ENC(state[2], state[3]);
33
+ state[2] = AES_ENC(state[1], state[2]);
34
+ state[1] = AES_ENC(state[0], state[1]);
35
+ state[0] = AES_BLOCK_XOR(AES_ENC(tmp, state[0]), d);
36
+ }
37
+
38
+ # include "aegis256_common.h"
39
+
40
+ struct aegis256_implementation aegis256_soft_implementation = {
41
+ .encrypt_detached = encrypt_detached,
42
+ .decrypt_detached = decrypt_detached,
43
+ .encrypt_unauthenticated = encrypt_unauthenticated,
44
+ .decrypt_unauthenticated = decrypt_unauthenticated,
45
+ .stream = stream,
46
+ .state_init = state_init,
47
+ .state_encrypt_update = state_encrypt_update,
48
+ .state_encrypt_final = state_encrypt_final,
49
+ .state_decrypt_update = state_decrypt_update,
50
+ .state_decrypt_final = state_decrypt_final,
51
+ .state_mac_init = state_mac_init,
52
+ .state_mac_update = state_mac_update,
53
+ .state_mac_final = state_mac_final,
54
+ .state_mac_reset = state_mac_reset,
55
+ .state_mac_clone = state_mac_clone,
56
+ };
57
+
58
+ #endif
@@ -0,0 +1,9 @@
1
+ #ifndef aegis256_soft_H
2
+ #define aegis256_soft_H
3
+
4
+ #include "../common/common.h"
5
+ #include "implementations.h"
6
+
7
+ extern struct aegis256_implementation aegis256_soft_implementation;
8
+
9
+ #endif
@@ -0,0 +1,39 @@
1
+ #ifndef aegis256_implementations_H
2
+ #define aegis256_implementations_H
3
+
4
+ #include <stddef.h>
5
+ #include <stdint.h>
6
+
7
+ #include "aegis256.h"
8
+
9
+ /* Namespacing to avoid conflicts with libsodium */
10
+ #define aegis256_soft_implementation libaegis_aegis256_soft_implementation
11
+ #define aegis256_aesni_implementation libaegis_aegis256_aesni_implementation
12
+ #define aegis256_neon_aes_implementation libaegis_aegis256_neon_aes_implementation
13
+ #define aegis256_altivec_implementation libaegis_aegis256_altivec_implementation
14
+
15
+ typedef struct aegis256_implementation {
16
+ int (*encrypt_detached)(uint8_t *c, uint8_t *mac, size_t maclen, const uint8_t *m, size_t mlen,
17
+ const uint8_t *ad, size_t adlen, const uint8_t *npub, const uint8_t *k);
18
+ int (*decrypt_detached)(uint8_t *m, const uint8_t *c, size_t clen, const uint8_t *mac,
19
+ size_t maclen, const uint8_t *ad, size_t adlen, const uint8_t *npub,
20
+ const uint8_t *k);
21
+ void (*stream)(uint8_t *out, size_t len, const uint8_t *npub, const uint8_t *k);
22
+ void (*encrypt_unauthenticated)(uint8_t *c, const uint8_t *m, size_t mlen, const uint8_t *npub,
23
+ const uint8_t *k);
24
+ void (*decrypt_unauthenticated)(uint8_t *m, const uint8_t *c, size_t clen, const uint8_t *npub,
25
+ const uint8_t *k);
26
+ void (*state_init)(aegis256_state *st_, const uint8_t *ad, size_t adlen, const uint8_t *npub,
27
+ const uint8_t *k);
28
+ int (*state_encrypt_update)(aegis256_state *st_, uint8_t *c, const uint8_t *m, size_t mlen);
29
+ int (*state_encrypt_final)(aegis256_state *st_, uint8_t *mac, size_t maclen);
30
+ int (*state_decrypt_update)(aegis256_state *st_, uint8_t *m, const uint8_t *c, size_t clen);
31
+ int (*state_decrypt_final)(aegis256_state *st_, const uint8_t *mac, size_t maclen);
32
+ void (*state_mac_init)(aegis256_mac_state *st_, const uint8_t *npub, const uint8_t *k);
33
+ int (*state_mac_update)(aegis256_mac_state *st_, const uint8_t *ad, size_t adlen);
34
+ int (*state_mac_final)(aegis256_mac_state *st_, uint8_t *mac, size_t maclen);
35
+ void (*state_mac_reset)(aegis256_mac_state *st);
36
+ void (*state_mac_clone)(aegis256_mac_state *dst, const aegis256_mac_state *src);
37
+ } aegis256_implementation;
38
+
39
+ #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 "aegis256x2.h"
7
+ #include "aegis256x2_aesni.h"
8
+ #include "aegis256x2_altivec.h"
9
+ #include "aegis256x2_avx2.h"
10
+ #include "aegis256x2_neon_aes.h"
11
+
12
+ #ifndef HAS_HW_AES
13
+ # include "aegis256x2_soft.h"
14
+ static const aegis256x2_implementation *implementation = &aegis256x2_soft_implementation;
15
+ #else
16
+ # if defined(__aarch64__) || defined(_M_ARM64)
17
+ static const aegis256x2_implementation *implementation = &aegis256x2_neon_aes_implementation;
18
+ # elif defined(__x86_64__) || defined(__i386__)
19
+ static const aegis256x2_implementation *implementation = &aegis256x2_aesni_implementation;
20
+ # elif defined(__ALTIVEC__) && defined(__CRYPTO__)
21
+ static const aegis256x2_implementation *implementation = &aegis256x2_altivec_implementation;
22
+ # else
23
+ # error "Unsupported architecture"
24
+ # endif
25
+ #endif
26
+
27
+ size_t
28
+ aegis256x2_keybytes(void)
29
+ {
30
+ return aegis256x2_KEYBYTES;
31
+ }
32
+
33
+ size_t
34
+ aegis256x2_npubbytes(void)
35
+ {
36
+ return aegis256x2_NPUBBYTES;
37
+ }
38
+
39
+ size_t
40
+ aegis256x2_abytes_min(void)
41
+ {
42
+ return aegis256x2_ABYTES_MIN;
43
+ }
44
+
45
+ size_t
46
+ aegis256x2_abytes_max(void)
47
+ {
48
+ return aegis256x2_ABYTES_MAX;
49
+ }
50
+
51
+ size_t
52
+ aegis256x2_tailbytes_max(void)
53
+ {
54
+ return aegis256x2_TAILBYTES_MAX;
55
+ }
56
+
57
+ int
58
+ aegis256x2_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
+ aegis256x2_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
+ aegis256x2_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 aegis256x2_encrypt_detached(c, c + mlen, maclen, m, mlen, ad, adlen, npub, k);
85
+ }
86
+
87
+ int
88
+ aegis256x2_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 = aegis256x2_decrypt_detached(m, c, clen - maclen, c + clen - maclen, maclen, ad, adlen,
95
+ npub, k);
96
+ }
97
+ return ret;
98
+ }
99
+
100
+ void
101
+ aegis256x2_state_init(aegis256x2_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
+ aegis256x2_state_encrypt_update(aegis256x2_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
+ aegis256x2_state_encrypt_final(aegis256x2_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
+ aegis256x2_state_decrypt_update(aegis256x2_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
+ aegis256x2_state_decrypt_final(aegis256x2_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
+ aegis256x2_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
+ aegis256x2_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
+ aegis256x2_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
+ aegis256x2_mac_init(aegis256x2_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
+ aegis256x2_mac_update(aegis256x2_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
+ aegis256x2_mac_final(aegis256x2_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
+ aegis256x2_mac_verify(aegis256x2_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
+ aegis256x2_mac_reset(aegis256x2_mac_state *st_)
202
+ {
203
+ implementation->state_mac_reset(st_);
204
+ }
205
+
206
+ void
207
+ aegis256x2_mac_state_clone(aegis256x2_mac_state *dst, const aegis256x2_mac_state *src)
208
+ {
209
+ implementation->state_mac_clone(dst, src);
210
+ }
211
+
212
+ int
213
+ aegis256x2_pick_best_implementation(void)
214
+ {
215
+ #ifndef HAS_HW_AES
216
+ implementation = &aegis256x2_soft_implementation;
217
+ #endif
218
+
219
+ #if defined(__aarch64__) || defined(_M_ARM64)
220
+ if (aegis_runtime_has_neon_aes()) {
221
+ implementation = &aegis256x2_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 = &aegis256x2_avx2_implementation;
230
+ return 0;
231
+ }
232
+ # endif
233
+ if (aegis_runtime_has_aesni() && aegis_runtime_has_avx()) {
234
+ implementation = &aegis256x2_aesni_implementation;
235
+ return 0;
236
+ }
237
+ #endif
238
+
239
+ #if defined(__ALTIVEC__) && defined(__CRYPTO__)
240
+ if (aegis_runtime_has_altivec()) {
241
+ implementation = &aegis256x2_altivec_implementation;
242
+ return 0;
243
+ }
244
+ #endif
245
+
246
+ return 0; /* LCOV_EXCL_LINE */
247
+ }
@@ -0,0 +1,103 @@
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 "aegis256x2.h"
8
+ # include "aegis256x2_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
+ aegis256x2_update(aes_block_t *const state, const aes_block_t d)
67
+ {
68
+ aes_block_t tmp;
69
+
70
+ tmp = state[5];
71
+ state[5] = AES_ENC(state[4], state[5]);
72
+ state[4] = AES_ENC(state[3], state[4]);
73
+ state[3] = AES_ENC(state[2], state[3]);
74
+ state[2] = AES_ENC(state[1], state[2]);
75
+ state[1] = AES_ENC(state[0], state[1]);
76
+ state[0] = AES_BLOCK_XOR(AES_ENC(tmp, state[0]), d);
77
+ }
78
+
79
+ # include "aegis256x2_common.h"
80
+
81
+ struct aegis256x2_implementation aegis256x2_aesni_implementation = {
82
+ .encrypt_detached = encrypt_detached,
83
+ .decrypt_detached = decrypt_detached,
84
+ .encrypt_unauthenticated = encrypt_unauthenticated,
85
+ .decrypt_unauthenticated = decrypt_unauthenticated,
86
+ .stream = stream,
87
+ .state_init = state_init,
88
+ .state_encrypt_update = state_encrypt_update,
89
+ .state_encrypt_final = state_encrypt_final,
90
+ .state_decrypt_update = state_decrypt_update,
91
+ .state_decrypt_final = state_decrypt_final,
92
+ .state_mac_init = state_mac_init,
93
+ .state_mac_update = state_mac_update,
94
+ .state_mac_final = state_mac_final,
95
+ .state_mac_reset = state_mac_reset,
96
+ .state_mac_clone = state_mac_clone,
97
+ };
98
+
99
+ # ifdef __clang__
100
+ # pragma clang attribute pop
101
+ # endif
102
+
103
+ #endif
@@ -0,0 +1,9 @@
1
+ #ifndef aegis256x2_aesni_H
2
+ #define aegis256x2_aesni_H
3
+
4
+ #include "../common/common.h"
5
+ #include "implementations.h"
6
+
7
+ extern struct aegis256x2_implementation aegis256x2_aesni_implementation;
8
+
9
+ #endif
@@ -0,0 +1,101 @@
1
+ #if defined(__ALTIVEC__) && defined(__CRYPTO__)
2
+
3
+ # include <stddef.h>
4
+ # include <stdint.h>
5
+
6
+ # include "../common/common.h"
7
+ # include "aegis256x2.h"
8
+ # include "aegis256x2_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 32
19
+
20
+ typedef struct {
21
+ vector unsigned char b0;
22
+ vector unsigned char b1;
23
+ } aes_block_t;
24
+
25
+ static inline aes_block_t
26
+ AES_BLOCK_XOR(const aes_block_t a, const aes_block_t b)
27
+ {
28
+ return (aes_block_t) { vec_xor(a.b0, b.b0), vec_xor(a.b1, b.b1) };
29
+ }
30
+
31
+ static inline aes_block_t
32
+ AES_BLOCK_AND(const aes_block_t a, const aes_block_t b)
33
+ {
34
+ return (aes_block_t) { vec_and(a.b0, b.b0), vec_and(a.b1, b.b1) };
35
+ }
36
+
37
+ static inline aes_block_t
38
+ AES_BLOCK_LOAD(const uint8_t *a)
39
+ {
40
+ return (aes_block_t) { vec_xl_be(0, a), vec_xl_be(0, a + 16) };
41
+ }
42
+
43
+ static inline aes_block_t
44
+ AES_BLOCK_LOAD_64x2(uint64_t a, uint64_t b)
45
+ {
46
+ const vector unsigned char t = ((vector unsigned char) vec_revb(
47
+ vec_insert(a, vec_promote((unsigned long long) (b), 1), 0)));
48
+ return (aes_block_t) { t, t };
49
+ }
50
+ static inline void
51
+ AES_BLOCK_STORE(uint8_t *a, const aes_block_t b)
52
+ {
53
+ vec_xst_be(b.b0, 0, a);
54
+ vec_xst_be(b.b1, 0, a + 16);
55
+ }
56
+
57
+ static inline aes_block_t
58
+ AES_ENC(const aes_block_t a, const aes_block_t b)
59
+ {
60
+ return (aes_block_t) { vec_cipher_be(a.b0, b.b0), vec_cipher_be(a.b1, b.b1) };
61
+ }
62
+
63
+ static inline void
64
+ aegis256x2_update(aes_block_t *const state, const aes_block_t d)
65
+ {
66
+ aes_block_t tmp;
67
+
68
+ tmp = state[5];
69
+ state[5] = AES_ENC(state[4], state[5]);
70
+ state[4] = AES_ENC(state[3], state[4]);
71
+ state[3] = AES_ENC(state[2], state[3]);
72
+ state[2] = AES_ENC(state[1], state[2]);
73
+ state[1] = AES_ENC(state[0], state[1]);
74
+ state[0] = AES_BLOCK_XOR(AES_ENC(tmp, state[0]), d);
75
+ }
76
+
77
+ # include "aegis256x2_common.h"
78
+
79
+ struct aegis256x2_implementation aegis256x2_altivec_implementation = {
80
+ .encrypt_detached = encrypt_detached,
81
+ .decrypt_detached = decrypt_detached,
82
+ .encrypt_unauthenticated = encrypt_unauthenticated,
83
+ .decrypt_unauthenticated = decrypt_unauthenticated,
84
+ .stream = stream,
85
+ .state_init = state_init,
86
+ .state_encrypt_update = state_encrypt_update,
87
+ .state_encrypt_final = state_encrypt_final,
88
+ .state_decrypt_update = state_decrypt_update,
89
+ .state_decrypt_final = state_decrypt_final,
90
+ .state_mac_init = state_mac_init,
91
+ .state_mac_update = state_mac_update,
92
+ .state_mac_final = state_mac_final,
93
+ .state_mac_reset = state_mac_reset,
94
+ .state_mac_clone = state_mac_clone,
95
+ };
96
+
97
+ # ifdef __clang__
98
+ # pragma clang attribute pop
99
+ # endif
100
+
101
+ #endif
@@ -0,0 +1,9 @@
1
+ #ifndef aegis256x2_altivec_H
2
+ #define aegis256x2_altivec_H
3
+
4
+ #include "../common/common.h"
5
+ #include "implementations.h"
6
+
7
+ extern struct aegis256x2_implementation aegis256x2_altivec_implementation;
8
+
9
+ #endif