Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
dependencies ▾

Modern-Apps.git / apple / FindFamily / FindFamilyiOS / Crypto / Keccak.swift

Keccak.swift175 lines · 6.1 KiBRaw
1import Foundation
2
3// Keccak-f[1600] and the SHA-3 / SHAKE functions that FIPS 203 (ML-KEM) and
4// FIPS 204 (ML-DSA) are built on.
5//
6// CryptoKit exposes neither SHA-3 nor SHAKE below iOS 26, and the deployment
7// target here is iOS 18, so the post-quantum code brings its own. This is a
8// straight transcription of FIPS 202 — no shortcuts, no platform assumptions.
9
10enum Keccak {
11 // MARK: - Keccak-f[1600] permutation
12
13 private static let roundConstants: [UInt64] = [
14 0x0000000000000001, 0x0000000000008082, 0x800000000000808a, 0x8000000080008000,
15 0x000000000000808b, 0x0000000080000001, 0x8000000080008081, 0x8000000000008009,
16 0x000000000000008a, 0x0000000000000088, 0x0000000080008009, 0x000000008000000a,
17 0x000000008000808b, 0x800000000000008b, 0x8000000000008089, 0x8000000000008003,
18 0x8000000000008002, 0x8000000000000080, 0x000000000000800a, 0x800000008000000a,
19 0x8000000080008081, 0x8000000000008080, 0x0000000080000001, 0x8000000080008008,
20 ]
21
22 /// Rotation offsets ρ, indexed as `x + 5*y`.
23 private static let rotationOffsets: [UInt64] = [
24 0, 1, 62, 28, 27,
25 36, 44, 6, 55, 20,
26 3, 10, 43, 25, 39,
27 41, 45, 15, 21, 8,
28 18, 2, 61, 56, 14,
29 ]
30
31 @inline(__always)
32 private static func rotl(_ x: UInt64, _ n: UInt64) -> UInt64 {
33 n == 0 ? x : (x << n) | (x >> (64 - n))
34 }
35
36 /// In-place Keccak-f[1600] on a 25-lane state.
37 static func permute(_ a: inout [UInt64]) {
38 var b = [UInt64](repeating: 0, count: 25)
39 var c = [UInt64](repeating: 0, count: 5)
40 var d = [UInt64](repeating: 0, count: 5)
41
42 for round in 0..<24 {
43 // θ
44 for x in 0..<5 {
45 c[x] = a[x] ^ a[x + 5] ^ a[x + 10] ^ a[x + 15] ^ a[x + 20]
46 }
47 for x in 0..<5 {
48 d[x] = c[(x + 4) % 5] ^ rotl(c[(x + 1) % 5], 1)
49 }
50 for y in 0..<5 {
51 for x in 0..<5 { a[x + 5 * y] ^= d[x] }
52 }
53 // ρ and π: B[y, 2x+3y] = rotl(A[x, y], r[x, y])
54 for y in 0..<5 {
55 for x in 0..<5 {
56 b[y + 5 * ((2 * x + 3 * y) % 5)] = rotl(a[x + 5 * y], rotationOffsets[x + 5 * y])
57 }
58 }
59 // χ
60 for y in 0..<5 {
61 for x in 0..<5 {
62 a[x + 5 * y] = b[x + 5 * y] ^ (~b[(x + 1) % 5 + 5 * y] & b[(x + 2) % 5 + 5 * y])
63 }
64 }
65 // ι
66 a[0] ^= roundConstants[round]
67 }
68 }
69
70 // MARK: - Sponge
71
72 /// Absorbs a complete message, then squeezes on demand. Every use here knows its
73 /// input up front, so there's no incremental-absorb path to get wrong.
74 struct Sponge {
75 private var state = [UInt64](repeating: 0, count: 25)
76 private let rate: Int // bytes
77 private var block: [UInt8] // current squeeze block
78 private var offset: Int // read position within `block`
79
80 init(rate: Int, padByte: UInt8, input: [UInt8]) {
81 self.rate = rate
82 self.block = []
83 self.offset = 0
84
85 // Absorb full blocks, then the padded remainder.
86 var padded = input
87 let remainder = padded.count % rate
88 var tail = [UInt8](repeating: 0, count: rate - remainder)
89 tail[0] = padByte
90 tail[tail.count - 1] |= 0x80
91 padded.append(contentsOf: tail)
92
93 var i = 0
94 while i < padded.count {
95 absorbBlock(Array(padded[i..<(i + rate)]))
96 i += rate
97 }
98 squeezeBlock()
99 }
100
101 private mutating func absorbBlock(_ bytes: [UInt8]) {
102 for j in 0..<(rate / 8) {
103 var lane: UInt64 = 0
104 for b in 0..<8 {
105 lane |= UInt64(bytes[j * 8 + b]) << UInt64(8 * b)
106 }
107 state[j] ^= lane
108 }
109 Keccak.permute(&state)
110 }
111
112 private mutating func squeezeBlock() {
113 var out = [UInt8](repeating: 0, count: rate)
114 for j in 0..<(rate / 8) {
115 let lane = state[j]
116 for b in 0..<8 {
117 out[j * 8 + b] = UInt8(truncatingIfNeeded: lane >> UInt64(8 * b))
118 }
119 }
120 block = out
121 offset = 0
122 }
123
124 /// Squeezes exactly `count` bytes, permuting as needed.
125 mutating func squeeze(_ count: Int) -> [UInt8] {
126 var out = [UInt8]()
127 out.reserveCapacity(count)
128 var remaining = count
129 while remaining > 0 {
130 if offset == rate {
131 Keccak.permute(&state)
132 squeezeBlock()
133 }
134 let take = min(remaining, rate - offset)
135 out.append(contentsOf: block[offset..<(offset + take)])
136 offset += take
137 remaining -= take
138 }
139 return out
140 }
141 }
142
143 // MARK: - Named functions
144
145 static func shake128(_ input: [UInt8], outputCount: Int) -> [UInt8] {
146 var s = Sponge(rate: 168, padByte: 0x1F, input: input)
147 return s.squeeze(outputCount)
148 }
149
150 static func shake256(_ input: [UInt8], outputCount: Int) -> [UInt8] {
151 var s = Sponge(rate: 136, padByte: 0x1F, input: input)
152 return s.squeeze(outputCount)
153 }
154
155 /// Incremental SHAKE128 reader — ML-KEM's matrix sampling rejects candidates,
156 /// so it can't know its output length in advance.
157 static func shake128Reader(_ input: [UInt8]) -> Sponge {
158 Sponge(rate: 168, padByte: 0x1F, input: input)
159 }
160
161 /// Incremental SHAKE256 reader — used by ML-DSA rejection sampling.
162 static func shake256Reader(_ input: [UInt8]) -> Sponge {
163 Sponge(rate: 136, padByte: 0x1F, input: input)
164 }
165
166 static func sha3_256(_ input: [UInt8]) -> [UInt8] {
167 var s = Sponge(rate: 136, padByte: 0x06, input: input)
168 return s.squeeze(32)
169 }
170
171 static func sha3_512(_ input: [UInt8]) -> [UInt8] {
172 var s = Sponge(rate: 72, padByte: 0x06, input: input)
173 return s.squeeze(64)
174 }
175}