Skip to content

Commit 145c36d

Browse files
committed
Add fips202x2 support
1 parent 0ae709c commit 145c36d

File tree

5 files changed

+928
-2
lines changed

5 files changed

+928
-2
lines changed

pqcrypto-internals/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "pqcrypto-internals"
3-
version = "0.2.5"
3+
version = "0.2.6"
44
edition = "2021"
55
description = "bindings to common cryptography"
66
license = "MIT OR Apache-2.0"

pqcrypto-internals/build.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ fn main() {
3333
println!("cargo:rustc-link-lib=pqclean_common");
3434

3535
let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap_or_default();
36+
let target_env = env::var("CARGO_CFG_TARGET_ENV").unwrap_or_default();
3637
if target_arch == "x86" || target_arch == "x86_64" {
3738
let mut builder = cc::Build::new();
3839

@@ -42,7 +43,6 @@ fn main() {
4243
builder.flag(format!("--sysroot={wasi_sdk_path}").as_str());
4344
}
4445

45-
let target_env = env::var("CARGO_CFG_TARGET_ENV").unwrap();
4646
if target_env == "msvc" {
4747
builder.flag("/arch:AVX2");
4848
} else {
@@ -56,5 +56,19 @@ fn main() {
5656
)
5757
.compile("keccak4x");
5858
println!("cargo:rustc-link-lib=keccak4x")
59+
} else if target_arch == "aarch64" && target_env != "msvc" {
60+
let mut builder = cc::Build::new();
61+
62+
if target_os == "wasi" {
63+
let wasi_sdk_path =
64+
&std::env::var("WASI_SDK_DIR").expect("missing environment variable: WASI_SDK_DIR");
65+
builder.flag(format!("--sysroot={wasi_sdk_path}").as_str());
66+
}
67+
68+
builder
69+
.file(cfiledir.join("keccak2x").join("fips202x2.c"))
70+
.file(cfiledir.join("keccak2x").join("feat.S"))
71+
.compile("keccak2x");
72+
println!("cargo:rustc-link-lib=keccak2x")
5973
}
6074
}
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
2+
/*
3+
MIT License
4+
5+
Copyright (c) 2020 Bas Westerbaan
6+
Copyright (c) 2023: Hanno Becker, Vincent Hwang, Matthias J. Kannwischer, Bo-Yin Yang, and Shang-Yi Yang
7+
8+
Permission is hereby granted, free of charge, to any person obtaining a copy
9+
of this software and associated documentation files (the "Software"), to deal
10+
in the Software without restriction, including without limitation the rights
11+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
copies of the Software, and to permit persons to whom the Software is
13+
furnished to do so, subject to the following conditions:
14+
15+
The above copyright notice and this permission notice shall be included in all
16+
copies or substantial portions of the Software.
17+
18+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24+
SOFTWARE.
25+
*/
26+
27+
#if (__APPLE__ && __ARM_FEATURE_CRYPTO) || (__ARM_FEATURE_SHA3)
28+
29+
.macro round
30+
; Execute theta, but without xoring into the state yet.
31+
; Compute parities p[i] = a[i] ^ a[5+i] ^ ... ^ a[20+i].
32+
eor3.16b v25, v0, v5, v10
33+
eor3.16b v26, v1, v6, v11
34+
eor3.16b v27, v2, v7, v12
35+
eor3.16b v28, v3, v8, v13
36+
eor3.16b v29, v4, v9, v14
37+
38+
eor3.16b v25, v25, v15, v20
39+
eor3.16b v26, v26, v16, v21
40+
eor3.16b v27, v27, v17, v22
41+
eor3.16b v28, v28, v18, v23
42+
eor3.16b v29, v29, v19, v24
43+
44+
rax1.2d v30, v29, v26 ; d[0] = rotl(p[1], 1) ^ p[4]
45+
rax1.2d v29, v27, v29 ; d[3] = rotl(p[4], 1) ^ p[2]
46+
rax1.2d v27, v25, v27 ; d[1] = rotl(p[2], 1) ^ p[0]
47+
rax1.2d v25, v28, v25 ; d[4] = rotl(p[0], 1) ^ p[3]
48+
rax1.2d v28, v26, v28 ; d[2] = rotl(p[3], 1) ^ p[1]
49+
50+
; Xor parities from step theta into the state at the same time
51+
; as executing rho and pi.
52+
eor.16b v0, v0, v30
53+
mov.16b v31, v1
54+
xar.2d v1, v6, v27, 20
55+
xar.2d v6, v9, v25, 44
56+
xar.2d v9, v22, v28, 3
57+
xar.2d v22, v14, v25, 25
58+
xar.2d v14, v20, v30, 46
59+
xar.2d v20, v2, v28, 2
60+
xar.2d v2, v12, v28, 21
61+
xar.2d v12, v13, v29, 39
62+
xar.2d v13, v19, v25, 56
63+
xar.2d v19, v23, v29, 8
64+
xar.2d v23, v15, v30, 23
65+
xar.2d v15, v4, v25, 37
66+
xar.2d v4, v24, v25, 50
67+
xar.2d v24, v21, v27, 62
68+
xar.2d v21, v8, v29, 9
69+
xar.2d v8, v16, v27, 19
70+
xar.2d v16, v5, v30, 28
71+
xar.2d v5, v3, v29, 36
72+
xar.2d v3, v18, v29, 43
73+
xar.2d v18, v17, v28, 49
74+
xar.2d v17, v11, v27, 54
75+
xar.2d v11, v7, v28, 58
76+
xar.2d v7, v10, v30, 61
77+
xar.2d v10, v31, v27, 63
78+
79+
; Chi
80+
bcax.16b v25, v0, v2, v1
81+
bcax.16b v26, v1, v3, v2
82+
bcax.16b v2, v2, v4, v3
83+
bcax.16b v3, v3, v0, v4
84+
bcax.16b v4, v4, v1, v0
85+
mov.16b v0, v25
86+
mov.16b v1, v26
87+
88+
bcax.16b v25, v5, v7, v6
89+
bcax.16b v26, v6, v8, v7
90+
bcax.16b v7, v7, v9, v8
91+
bcax.16b v8, v8, v5, v9
92+
bcax.16b v9, v9, v6, v5
93+
mov.16b v5, v25
94+
mov.16b v6, v26
95+
96+
bcax.16b v25, v10, v12, v11
97+
bcax.16b v26, v11, v13, v12
98+
bcax.16b v12, v12, v14, v13
99+
bcax.16b v13, v13, v10, v14
100+
bcax.16b v14, v14, v11, v10
101+
mov.16b v10, v25
102+
mov.16b v11, v26
103+
104+
bcax.16b v25, v15, v17, v16
105+
bcax.16b v26, v16, v18, v17
106+
bcax.16b v17, v17, v19, v18
107+
bcax.16b v18, v18, v15, v19
108+
bcax.16b v19, v19, v16, v15
109+
mov.16b v15, v25
110+
mov.16b v16, v26
111+
112+
bcax.16b v25, v20, v22, v21
113+
bcax.16b v26, v21, v23, v22
114+
bcax.16b v22, v22, v24, v23
115+
bcax.16b v23, v23, v20, v24
116+
bcax.16b v24, v24, v21, v20
117+
mov.16b v20, v25
118+
mov.16b v21, v26
119+
120+
; iota
121+
ld1r {v25.2d}, [x1], #8
122+
eor.16b v0, v0, v25
123+
.endm
124+
125+
.align 4
126+
.global f1600x2
127+
.global _f1600x2
128+
f1600x2:
129+
_f1600x2:
130+
stp d8, d9, [sp,#-16]!
131+
stp d10, d11, [sp,#-16]!
132+
stp d12, d13, [sp,#-16]!
133+
stp d14, d15, [sp,#-16]!
134+
135+
mov x2, x0
136+
mov x3, #24
137+
138+
ld1.2d {v0, v1, v2, v3}, [x0], #64
139+
ld1.2d {v4, v5, v6, v7}, [x0], #64
140+
ld1.2d {v8, v9, v10, v11}, [x0], #64
141+
ld1.2d {v12, v13, v14, v15}, [x0], #64
142+
ld1.2d {v16, v17, v18, v19}, [x0], #64
143+
ld1.2d {v20, v21, v22, v23}, [x0], #64
144+
ld1.2d {v24}, [x0]
145+
146+
loop:
147+
round
148+
149+
subs x3, x3, #1
150+
cbnz x3, loop
151+
152+
mov x0, x2
153+
st1.2d {v0, v1, v2, v3}, [x0], #64
154+
st1.2d {v4, v5, v6, v7}, [x0], #64
155+
st1.2d {v8, v9, v10, v11}, [x0], #64
156+
st1.2d {v12, v13, v14, v15}, [x0], #64
157+
st1.2d {v16, v17, v18, v19}, [x0], #64
158+
st1.2d {v20, v21, v22, v23}, [x0], #64
159+
st1.2d {v24}, [x0]
160+
161+
ldp d14, d15, [sp], #16
162+
ldp d12, d13, [sp], #16
163+
ldp d10, d11, [sp], #16
164+
ldp d8, d9, [sp], #16
165+
166+
ret lr
167+
168+
#endif

0 commit comments

Comments
 (0)