check.bend
bend-osdev-crypto@0.1.0.0 · 675 lines · 65 definitions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20import Base
21import bend-osdev-bytes@0.1.0.0/bytes.bend as B
22import bend-osdev-word64@0.1.0.0/word64.bend as W
23import bend-osdev-cipher-modes@0.1.0.0/cipher_modes.bend as M
24import bend-osdev-cipher-modes@0.1.0.0/aes.bend as X
25import bend-osdev-legacy-ciphers@0.1.0.0/legacy_ciphers.bend as L
26import bend-osdev-legacy-ciphers@0.1.0.0/des.bend as D
27import bend-osdev-legacy-ciphers@0.1.0.0/tdes.bend as T
28import bend-osdev-legacy-ciphers@0.1.0.0/sm4.bend as S
29import bend-osdev-legacy-ciphers@0.1.0.0/camellia.bend as Ca
30import bend-osdev-legacy-ciphers@0.1.0.0/serpent.bend as Sp
31import bend-osdev-legacy-ciphers@0.1.0.0/twofish.bend as Tf
32import bend-osdev-legacy-ciphers@0.1.0.0/rc4.bend as R
33import bend-osdev-crc@0.1.0.0/crc.bend as C
34import bend-osdev-crypto@0.1.0.0/crypto.bend as Cr
35import bend-osdev-crypto@0.1.0.0/stream.bend as St
36import bend-osdev-crypto@0.1.0.0/block.bend as Bk
37import bend-osdev-crypto@0.1.0.0/modes.bend as Md
38import bend-osdev-crypto@0.1.0.0/hash.bend as H
39import bend-osdev-crypto@0.1.0.0/mac.bend as Mc
40import bend-osdev-crypto@0.1.0.0/hmac.bend as Hm
41import bend-osdev-crypto@0.1.0.0/hkdf.bend as Hk
42import bend-osdev-crypto@0.1.0.0/aead.bend as Ae
43import bend-osdev-crypto@0.1.0.0/inst_cm.bend as CM
44import bend-osdev-crypto@0.1.0.0/inst_lc.bend as LC
45import bend-osdev-crypto@0.1.0.0/inst_aes.bend as A
46import bend-osdev-crypto@0.1.0.0/inst_rc4.bend as RC
47import bend-osdev-crypto@0.1.0.0/inst_ccm.bend as CC
48import bend-osdev-crypto@0.1.0.0/inst_cmac.bend as CMI
49import bend-osdev-crypto@0.1.0.0/inst_crc.bend as CR
50import bend-osdev-crypto@0.1.0.0/inst_sha2.bend as SH
51import bend-osdev-crypto@0.1.0.0/inst_sha3.bend as S3
52import bend-osdev-crypto@0.1.0.0/pcbc.bend as Pc
53import bend-osdev-crypto@0.1.0.0/lrw.bend as Lw
54import bend-osdev-crypto@0.1.0.0/cts.bend as Ct
55import bend-osdev-crypto@0.1.0.0/xcbc.bend as Xc
56import bend-osdev-crypto@0.1.0.0/vmac.bend as Vm
57import bend-osdev-legacy-ciphers@0.1.0.0/aria.bend as Ar
58import bend-osdev-legacy-ciphers@0.1.0.0/cast6.bend as C6
59import bend-osdev-legacy-ciphers@0.1.0.0/blowfish.bend as Bf
60import bend-osdev-legacy-ciphers@0.1.0.0/cast5.bend as C5
61import bend-osdev-legacy-ciphers@0.1.0.0/tea.bend as Te
62import bend-osdev-legacy-ciphers@0.1.0.0/xtea.bend as Xt
63import bend-osdev-legacy-ciphers@0.1.0.0/fcrypt.bend as Fc
64import bend-osdev-crypto@0.1.0.0/inst_michael.bend as MIC
65import bend-osdev-crypto@0.1.0.0/pbkdf2.bend as Pb
66import bend-osdev-crypto@0.1.0.0/inst_pbkdf2.bend as PbI
67import bend-osdev-crypto@0.1.0.0/kbkdf.bend as Kb
68import bend-osdev-crypto@0.1.0.0/inst_kbkdf.bend as KbI
69import bend-osdev-hashes@0.1.0.0/hashes.bend as Hs
70import bend-osdev-hashes@0.1.0.0/md4.bend as HM4
71import bend-osdev-hashes@0.1.0.0/md5.bend as HM5
72import bend-osdev-hashes@0.1.0.0/sha1.bend as HS1
73import bend-osdev-hashes@0.1.0.0/ripemd160.bend as HRM
74import bend-osdev-hashes@0.1.0.0/sm3.bend as HSM
75import bend-osdev-hashes@0.1.0.0/streebog.bend as HST
76import bend-osdev-hashes@0.1.0.0/michael.bend as HMI
77
78def cat(a: String, b: String) -> String:
79 a ++ b
80
81def err(e: Cr.CrError) -> String:
82 match e:
83 case Cr.CrBadKeyLength{n}:
84 cat("bad key length ", Nat.show(n))
85 case Cr.CrBadIvLength{n}:
86 cat("bad iv length ", Nat.show(n))
87 case Cr.CrNotBlockMultiple{n}:
88 cat("not whole blocks ", Nat.show(n))
89 case Cr.CrBadBlockSize{n}:
90 cat("bad block size ", Nat.show(n))
91 case Cr.CrTagMismatch{}:
92 "tag mismatch"
93 case Cr.CrBadOutputLength{n}:
94 cat("bad output length ", Nat.show(n))
95 case Cr.CrTooShort{n}:
96 cat("too short ", Nat.show(n))
97 case _:
98 "other error"
99
100def show(r: Result<&2, &2, Cr.CrError, B.Octets>) -> String:
101 match r:
102 case Done{o}:
103 B.Octets.hex(o)
104 case Fail{e}:
105 err(e)
106
107def cr.check.base(digit: Bool) -> U32:
108 match digit:
109 case True{}:
110 48
111 case False{}:
112 87
113
114def cr.check.nib(+c: U32) -> U32:
115 U32.sub(c, cr.check.base(U32.is_lt(c, 58)))
116
117def cr.check.hex(cs: List<&2, U32>, acc: List<&2, U32>) -> List<&2, U32>:
118 match cs:
119 case Con{a, Con{b, t}}:
120 cr.check.hex(t, Con{U32.add(U32.mul(cr.check.nib(a), 16), cr.check.nib(b)), acc})
121 case _:
122 B.rev(acc)
123
124def yes_no(b: Bool) -> String:
125 match b:
126 case True{}:
127 "accepted"
128 case False{}:
129 "refused"
130
131def cr.check.two.go(~S: Data, ~update: S -> B.Octets -> St.CrXorOut<S>, o: St.CrXorOut<S>, b: B.Octets) -> B.Octets:
132 match o:
133 case St.CrXorOut{s, d}:
134 B.Octets.append(d, St.CrXor.out_data(~S, update(s, b)))
135
136def cr.check.two(~S: Data, ~update: S -> B.Octets -> St.CrXorOut<S>, s: S, a: B.Octets, b: B.Octets) -> B.Octets:
137 cr.check.two.go(~S, ~update, update(s, a), b)
138
139def cr.check.rc4two(+o: R.LcRc4Out) -> B.Octets:
140 B.Octets.append(R.LcRc4.out_data(o), R.LcRc4.out_data(R.LcRc4.update(R.LcRc4.out_state(o), B.Octets.zeros(11n))))
141
142def cr.check.verify(r: Result<&2, &2, Cr.CrError, Bool>) -> String:
143 match r:
144 case Done{b}:
145 cat(" ", yes_no(b))
146 case Fail{e}:
147 " failed"
148
149def hx(s: String) -> B.Octets:
150 B.Octets.of_list(cr.check.hex(B.Octets.to_list(B.Octets.of_string(s)), Nil{}))
151
152
153def both(e: Result<&2, &2, Cr.CrError, B.Octets>, d: Result<&2, &2, Cr.CrError, B.Octets>) -> String:
154 cat(show(e), cat(" ", show(d)))
155
156def ok_if(same: Bool, tail: B.Octets) -> String:
157 match same:
158 case True{}:
159 cat(B.Octets.hex(tail), " ok")
160 case False{}:
161 "MISMATCH"
162
163
164def round(+m: B.Octets, c: Result<&2, &2, Cr.CrError, B.Octets>, d: Result<&2, &2, Cr.CrError, B.Octets>) -> String:
165 match c d:
166 case Done{+x} Done{y}:
167 ok_if(B.Octets.is_eq(y, m), B.Octets.drop(Nat.sub(B.Octets.len(x), 16n), x))
168 case _ _:
169 "failed"
170
171
172
173def aes(+k: B.Octets, +p: B.Octets) -> String:
174 both(Bk.CrBlockCipher.encrypt_with(~X.CmAesKey, ~M.CmBlock, ~A.CrAes.key, ~X.CmAes.encrypt, ~CM.CrBlk128.of_octets, ~CM.CrBlk128.to_octets, k, p), Result.bind(&2, &2, Cr.CrError, B.Octets, B.Octets, Bk.CrBlockCipher.encrypt_with(~X.CmAesKey, ~M.CmBlock, ~A.CrAes.key, ~X.CmAes.encrypt, ~CM.CrBlk128.of_octets, ~CM.CrBlk128.to_octets, k, p), c => Bk.CrBlockCipher.encrypt_with(~X.CmAesKey, ~M.CmBlock, ~A.CrAes.key, ~X.CmAes.decrypt, ~CM.CrBlk128.of_octets, ~CM.CrBlk128.to_octets, k, c)))
175
176def lc128(~K: Data, ~key: B.Octets -> Result<&2, &2, Cr.CrError, K>, ~enc: K -> M.CmBlock -> M.CmBlock, ~dec: K -> M.CmBlock -> M.CmBlock, +k: B.Octets, +p: B.Octets) -> String:
177 +c = Bk.CrBlockCipher.encrypt_with(~K, ~M.CmBlock, ~key, ~enc, ~CM.CrBlk128.of_octets, ~CM.CrBlk128.to_octets, k, p)
178 both(c, Result.bind(&2, &2, Cr.CrError, B.Octets, B.Octets, c, x => Bk.CrBlockCipher.encrypt_with(~K, ~M.CmBlock, ~key, ~dec, ~CM.CrBlk128.of_octets, ~CM.CrBlk128.to_octets, k, x)))
179
180def lc64(~K: Data, ~key: B.Octets -> Result<&2, &2, Cr.CrError, K>, ~enc: K -> L.LcBlock64 -> L.LcBlock64, ~dec: K -> L.LcBlock64 -> L.LcBlock64, +k: B.Octets, +p: B.Octets) -> String:
181 +c = Bk.CrBlockCipher.encrypt_with(~K, ~L.LcBlock64, ~key, ~enc, ~L.LcBlock64.of_octets, ~L.LcBlock64.to_octets, k, p)
182 both(c, Result.bind(&2, &2, Cr.CrError, B.Octets, B.Octets, c, x => Bk.CrBlockCipher.encrypt_with(~K, ~L.LcBlock64, ~key, ~dec, ~L.LcBlock64.of_octets, ~L.LcBlock64.to_octets, k, x)))
183
184
185
186def aes_ecb(+k: B.Octets, +m: B.Octets) -> String:
187 +r = A.CrAes.key(k)
188 +c = Result.bind(&2, &2, Cr.CrError, X.CmAesKey, B.Octets, r, kk => Md.CrEcb.encrypt(~X.CmAesKey, ~M.CmBlock, ~16n, ~X.CmAes.encrypt, ~CM.CrBlk128.of_octets, ~CM.CrBlk128.to_octets, kk, m))
189 both(c, Result.bind(&2, &2, Cr.CrError, X.CmAesKey, B.Octets, r, kk => Result.bind(&2, &2, Cr.CrError, B.Octets, B.Octets, c, x => Md.CrEcb.decrypt(~X.CmAesKey, ~M.CmBlock, ~16n, ~X.CmAes.decrypt, ~CM.CrBlk128.of_octets, ~CM.CrBlk128.to_octets, kk, x))))
190
191def aes_cbc_c(+k: B.Octets, +iv: B.Octets, +m: B.Octets) -> Result<&2, &2, Cr.CrError, B.Octets>:
192 Result.bind(&2, &2, Cr.CrError, X.CmAesKey, B.Octets, A.CrAes.key(k), kk => Md.CrCbc.encrypt(~X.CmAesKey, ~M.CmBlock, ~16n, ~X.CmAes.encrypt, ~CM.CrBlk128.xor, ~CM.CrBlk128.of_octets, ~CM.CrBlk128.to_octets, kk, iv, m))
193
194def aes_cbc_d(+k: B.Octets, +iv: B.Octets, c: Result<&2, &2, Cr.CrError, B.Octets>) -> Result<&2, &2, Cr.CrError, B.Octets>:
195 Result.bind(&2, &2, Cr.CrError, X.CmAesKey, B.Octets, A.CrAes.key(k), kk => Result.bind(&2, &2, Cr.CrError, B.Octets, B.Octets, c, x => Md.CrCbc.decrypt(~X.CmAesKey, ~M.CmBlock, ~16n, ~X.CmAes.decrypt, ~CM.CrBlk128.xor, ~CM.CrBlk128.of_octets, ~CM.CrBlk128.to_octets, kk, iv, x)))
196
197def aes_cbc(+k: B.Octets, +iv: B.Octets, +m: B.Octets) -> String:
198 +c = aes_cbc_c(k, iv, m)
199 both(c, aes_cbc_d(k, iv, c))
200
201def tdes_cbc(+k: B.Octets, +iv: B.Octets, +m: B.Octets) -> String:
202 +r = LC.CrLc.key(~T.LcTdesKey, ~T.LcTdes.key, k)
203 +c = Result.bind(&2, &2, Cr.CrError, T.LcTdesKey, B.Octets, r, kk => Md.CrCbc.encrypt(~T.LcTdesKey, ~L.LcBlock64, ~8n, ~T.LcTdes.encrypt, ~LC.CrBlk64.xor, ~L.LcBlock64.of_octets, ~L.LcBlock64.to_octets, kk, iv, m))
204 both(c, Result.bind(&2, &2, Cr.CrError, T.LcTdesKey, B.Octets, r, kk => Result.bind(&2, &2, Cr.CrError, B.Octets, B.Octets, c, x => Md.CrCbc.decrypt(~T.LcTdesKey, ~L.LcBlock64, ~8n, ~T.LcTdes.decrypt, ~LC.CrBlk64.xor, ~L.LcBlock64.of_octets, ~L.LcBlock64.to_octets, kk, iv, x))))
205
206
207def aes_ctr(+k: B.Octets, +iv: B.Octets, m: B.Octets) -> Result<&2, &2, Cr.CrError, B.Octets>:
208 St.CrStream.crypt(~Md.CrCtrState<X.CmAesKey>, ~St.CrXorOut<Md.CrCtrState<X.CmAesKey>>, ~Md.CrCtr.start(~X.CmAesKey, ~16n, ~A.CrAes.key_enc), ~Md.CrCtr.update(~X.CmAesKey, ~M.CmBlock, ~X.CmAes.encrypt, ~CM.CrBlk128.of_octets, ~CM.CrBlk128.to_octets), ~St.CrXor.out_data(~Md.CrCtrState<X.CmAesKey>), k, iv, m)
209
210
211def des_ctr_pieces(+k: B.Octets, +iv: B.Octets, +a: B.Octets, +b: B.Octets) -> Result<&2, &2, Cr.CrError, B.Octets>:
212 Result.map(&2, &2, Cr.CrError, Md.CrCtrState<D.LcDesKey>, B.Octets, +s => cr.check.two(~Md.CrCtrState<D.LcDesKey>, ~Md.CrCtr.update(~D.LcDesKey, ~L.LcBlock64, ~D.LcDes.encrypt, ~L.LcBlock64.of_octets, ~L.LcBlock64.to_octets), s, a, b), Md.CrCtr.start(~D.LcDesKey, ~8n, ~LC.CrLc.key(~D.LcDesKey, ~D.LcDes.key), k, iv))
213
214
215def rc4(+k: B.Octets, n: Nat) -> Result<&2, &2, Cr.CrError, B.Octets>:
216 St.CrStream.crypt(~R.LcRc4State, ~R.LcRc4Out, ~RC.CrRc4.start, ~R.LcRc4.update, ~R.LcRc4.out_data, k, B.Octets.empty(), B.Octets.zeros(n))
217
218def rc4_pieces(+k: B.Octets) -> Result<&2, &2, Cr.CrError, B.Octets>:
219 Result.map(&2, &2, Cr.CrError, R.LcRc4State, B.Octets, +s => cr.check.rc4two(R.LcRc4.update(s, B.Octets.zeros(5n))), RC.CrRc4.start(k, B.Octets.empty()))
220
221def rc4_drop(+k: B.Octets) -> Result<&2, &2, Cr.CrError, B.Octets>:
222 St.CrStream.crypt(~R.LcRc4State, ~R.LcRc4Out, ~RC.CrRc4.start_drop(~1536n), ~R.LcRc4.update, ~R.LcRc4.out_data, k, B.Octets.empty(), B.Octets.zeros(16n))
223
224
225
226def flip_last(+t: B.Octets) -> B.Octets:
227 B.Octets.append(B.Octets.take(Nat.sub(B.Octets.len(t), 1n), t), B.Octets.of_list([U32.xor(Maybe.default(&2, U32, B.Octets.at(t, Nat.sub(B.Octets.len(t), 1n)), 0), 1)]))
228
229def cmac(+k: B.Octets, +m: B.Octets) -> String:
230 +t = Mc.CrMac.compute_with(~X.CmAesKey, ~A.CrAes.key_enc, ~CMI.CrCmac.mac(~X.CmAesKey, ~X.CmAes.encrypt), k, m)
231 cat(show(t), cr.check.verify(Result.map(&2, &2, Cr.CrError, B.Octets, Bool, +tag => Result.default(&2, &2, Cr.CrError, Bool, Result.map(&2, &2, Cr.CrError, X.CmAesKey, Bool, +kk => Bool.and(CMI.CrCmac.verify(~X.CmAesKey, ~X.CmAes.encrypt, kk, m, tag), Bool.not(CMI.CrCmac.verify(~X.CmAesKey, ~X.CmAes.encrypt, kk, m, flip_last(tag)))), A.CrAes.key_enc(k)), False{}), t)))
232
233
234
235
236def ccm(~tlen: Nat, +k: B.Octets, +n: B.Octets, +a: B.Octets, +p: B.Octets) -> String:
237 +c = Ae.CrAead.seal_with(~X.CmAesKey, ~A.CrAes.key_enc, ~CC.CrCcm.seal(~X.CmAesKey, ~X.CmAes.encrypt, ~tlen), k, n, a, p)
238 +o = Result.bind(&2, &2, Cr.CrError, B.Octets, B.Octets, c, x => Ae.CrAead.open_with(~X.CmAesKey, ~A.CrAes.key_enc, ~CC.CrCcm.open(~X.CmAesKey, ~X.CmAes.encrypt, ~tlen), k, n, a, x))
239 +bad = Result.bind(&2, &2, Cr.CrError, B.Octets, B.Octets, c, x => Ae.CrAead.open_with(~X.CmAesKey, ~A.CrAes.key_enc, ~CC.CrCcm.open(~X.CmAesKey, ~X.CmAes.encrypt, ~tlen), k, n, a, flip_last(x)))
240 cat(show(c), cat(" ", cat(show(o), cat(" ", show(bad)))))
241
242
243def crc32_pieces(+m1: B.Octets, +m2: B.Octets, +m3: B.Octets) -> String:
244 cat(B.Octets.hex(CR.CrCrc32.digest(~C.crc32c.spec(), B.Octets.append(m1, B.Octets.append(m2, m3)))), cat(" ", B.Octets.hex(H.CrHash.of_pieces(~U32, ~CR.CrCrc32.init(~C.crc32c.spec()), ~CR.CrCrc32.update(~C.crc32c.spec()), ~CR.CrCrc32.final(~C.crc32c.spec()), [m1, m2, m3]))))
245
246
247def hmac_crc32(+k: B.Octets, +a: B.Octets, +b: B.Octets) -> String:
248 +kk = Hm.CrHmac.key(~64n, ~CR.CrCrc32.digest(~C.crc32.spec()), k)
249 +t = Hm.CrHmac.mac(~CR.CrCrc32.digest(~C.crc32.spec()), kk, B.Octets.append(a, b))
250 +s = Hm.CrHmac.start(~U32, ~CR.CrCrc32.init(~C.crc32.spec()), ~CR.CrCrc32.update(~C.crc32.spec()), kk)
251 +t2 = Hm.CrHmac.finish(~U32, ~CR.CrCrc32.init(~C.crc32.spec()), ~CR.CrCrc32.update(~C.crc32.spec()), ~CR.CrCrc32.final(~C.crc32.spec()), Hm.CrHmac.update(~U32, ~CR.CrCrc32.update(~C.crc32.spec()), Hm.CrHmac.update(~U32, ~CR.CrCrc32.update(~C.crc32.spec()), s, a), b))
252 cat(B.Octets.hex(t), cat(" ", cat(B.Octets.hex(t2), cat(" ", yes_no(Hm.CrHmac.verify(~CR.CrCrc32.digest(~C.crc32.spec()), kk, B.Octets.append(a, b), t))))))
253
254
255def hmac_crc64(+k: B.Octets, +m: B.Octets) -> String:
256 B.Octets.hex(Hm.CrHmac.mac(~CR.CrCrc64.digest(~C.crc64_xz.spec()), Hm.CrHmac.key(~64n, ~CR.CrCrc64.digest(~C.crc64_xz.spec()), k), m))
257
258def hkdf_crc32(+ikm: B.Octets, +salt: B.Octets, +info: B.Octets, n: Nat) -> String:
259 show(Hk.CrHkdf.derive(~64n, ~4n, ~CR.CrCrc32.digest(~C.crc32.spec()), ikm, salt, info, n))
260
261def cr.check.tail8(r: Result<&2, &2, Cr.CrError, B.Octets>) -> String:
262 match r:
263 case Done{+o}:
264 cat(Nat.show(B.Octets.len(o)), cat(" ", B.Octets.hex(B.Octets.drop(Nat.sub(B.Octets.len(o), 8n), o))))
265 case Fail{e}:
266 err(e)
267
268
269def hkdf_crc32_max(+ikm: B.Octets) -> String:
270 cr.check.tail8(Hk.CrHkdf.derive(~64n, ~4n, ~CR.CrCrc32.digest(~C.crc32.spec()), ikm, B.Octets.empty(), B.Octets.empty(), 1020n))
271
272
273def big.go(n: Nat, +x: U32, acc: List<&2, U32>) -> List<&2, U32>:
274 match n:
275 case 0n:
276 acc
277 case 1n+p:
278 big.go(p, U32.add(U32.mul(x, 1103515245), 12345), Con{B.byte(U32.shrn(x, 16n)), acc})
279
280def big(n: Nat) -> B.Octets:
281 B.Octets.of_list(big.go(n, 7, Nil{}))
282
283def sm4_cbc_big(+k: B.Octets, +iv: B.Octets, +m: B.Octets) -> String:
284 +r = LC.CrLc.key(~S.LcSm4Key, ~S.LcSm4.key, k)
285 +c = Result.bind(&2, &2, Cr.CrError, S.LcSm4Key, B.Octets, r, kk => Md.CrCbc.encrypt(~S.LcSm4Key, ~M.CmBlock, ~16n, ~S.LcSm4.encrypt, ~CM.CrBlk128.xor, ~CM.CrBlk128.of_octets, ~CM.CrBlk128.to_octets, kk, iv, m))
286 round(m, c, Result.bind(&2, &2, Cr.CrError, S.LcSm4Key, B.Octets, r, kk => Result.bind(&2, &2, Cr.CrError, B.Octets, B.Octets, c, x => Md.CrCbc.decrypt(~S.LcSm4Key, ~M.CmBlock, ~16n, ~S.LcSm4.decrypt, ~CM.CrBlk128.xor, ~CM.CrBlk128.of_octets, ~CM.CrBlk128.to_octets, kk, iv, x))))
287
288def aes_ctr_big(+k: B.Octets, +iv: B.Octets, +m: B.Octets) -> String:
289 +c = aes_ctr(k, iv, m)
290 round(m, c, Result.bind(&2, &2, Cr.CrError, B.Octets, B.Octets, c, x => aes_ctr(k, iv, x)))
291
292
293
294def sha(~digest: B.Octets -> B.Octets, +m: B.Octets) -> String:
295 B.Octets.hex(digest(m))
296
297def sha_pieces3(~init: SH.CrSha2State, +a: B.Octets, +b: B.Octets, +c: B.Octets) -> String:
298 B.Octets.hex(H.CrHash.of_pieces(~SH.CrSha2State, ~init, ~SH.CrSha2.update, ~SH.CrSha2.final, [a, b, c]))
299
300
301def hmac_sha(~bsize: Nat, ~digest: B.Octets -> B.Octets, +k: B.Octets, +m: B.Octets) -> B.Octets:
302 Hm.CrHmac.mac(~digest, Hm.CrHmac.key(~bsize, ~digest, k), m)
303
304
305def hmac_sha256_pieces(+k: B.Octets, +a: B.Octets, +b: B.Octets) -> String:
306 +kk = Hm.CrHmac.key(~64n, ~SH.CrSha2.sha256, k)
307 +t = Hm.CrHmac.finish(~SH.CrSha2State, ~SH.CrSha2.init256(), ~SH.CrSha2.update, ~SH.CrSha2.final, Hm.CrHmac.update(~SH.CrSha2State, ~SH.CrSha2.update, Hm.CrHmac.update(~SH.CrSha2State, ~SH.CrSha2.update, Hm.CrHmac.start(~SH.CrSha2State, ~SH.CrSha2.init256(), ~SH.CrSha2.update, kk), a), b))
308 cat(B.Octets.hex(t), cat(" ", yes_no(Hm.CrHmac.verify(~SH.CrSha2.sha256, kk, B.Octets.append(a, b), t))))
309
310def hkdf_sha256(+ikm: B.Octets, +salt: B.Octets, +info: B.Octets, n: Nat) -> String:
311 show(Hk.CrHkdf.derive(~64n, ~32n, ~SH.CrSha2.sha256, ikm, salt, info, n))
312
313
314
315def pcbc_fc(+k: B.Octets, +iv: B.Octets, +m: B.Octets) -> String:
316 +r = LC.CrLc.key(~Fc.LcFcryptKey, ~Fc.LcFcrypt.key, k)
317 +c = Result.bind(&2, &2, Cr.CrError, Fc.LcFcryptKey, B.Octets, r, kk => Pc.CrPcbc.encrypt(~Fc.LcFcryptKey, ~L.LcBlock64, ~8n, ~Fc.LcFcrypt.encrypt, ~LC.CrBlk64.xor, ~L.LcBlock64.of_octets, ~L.LcBlock64.to_octets, kk, iv, m))
318 both(c, Result.bind(&2, &2, Cr.CrError, Fc.LcFcryptKey, B.Octets, r, kk => Result.bind(&2, &2, Cr.CrError, B.Octets, B.Octets, c, x => Pc.CrPcbc.decrypt(~Fc.LcFcryptKey, ~L.LcBlock64, ~8n, ~Fc.LcFcrypt.decrypt, ~LC.CrBlk64.xor, ~L.LcBlock64.of_octets, ~L.LcBlock64.to_octets, kk, iv, x))))
319
320def lrw_aes(+k: B.Octets, +iv: B.Octets, +m: B.Octets) -> String:
321 +r = Lw.CrLrw.key(~X.CmAesKey, ~A.CrAes.key, k)
322 +c = Result.bind(&2, &2, Cr.CrError, Lw.CrLrwKey<X.CmAesKey>, B.Octets, r, kk => Lw.CrLrw.encrypt(~X.CmAesKey, ~M.CmBlock, ~16n, ~X.CmAes.encrypt, ~CM.CrBlk128.xor, ~CM.CrBlk128.of_octets, ~CM.CrBlk128.to_octets, kk, iv, m))
323 both(c, Result.bind(&2, &2, Cr.CrError, Lw.CrLrwKey<X.CmAesKey>, B.Octets, r, kk => Result.bind(&2, &2, Cr.CrError, B.Octets, B.Octets, c, x => Lw.CrLrw.decrypt(~X.CmAesKey, ~M.CmBlock, ~16n, ~X.CmAes.decrypt, ~CM.CrBlk128.xor, ~CM.CrBlk128.of_octets, ~CM.CrBlk128.to_octets, kk, iv, x))))
324
325def cts_aes(+k: B.Octets, +iv: B.Octets, +m: B.Octets) -> String:
326 +r = A.CrAes.key(k)
327 +c = Result.bind(&2, &2, Cr.CrError, X.CmAesKey, B.Octets, r, kk => Ct.CrCts.encrypt(~X.CmAesKey, ~M.CmBlock, ~16n, ~X.CmAes.encrypt, ~CM.CrBlk128.of_octets, ~CM.CrBlk128.to_octets, kk, iv, m))
328 both(c, Result.bind(&2, &2, Cr.CrError, X.CmAesKey, B.Octets, r, kk => Result.bind(&2, &2, Cr.CrError, B.Octets, B.Octets, c, x => Ct.CrCts.decrypt(~X.CmAesKey, ~M.CmBlock, ~16n, ~X.CmAes.decrypt, ~CM.CrBlk128.of_octets, ~CM.CrBlk128.to_octets, kk, iv, x))))
329
330def xcbc_aes(+k: B.Octets, +m: B.Octets) -> String:
331 show(Result.map(&2, &2, Cr.CrError, Xc.CrXcbcKey<X.CmAesKey>, B.Octets, kk => Xc.CrXcbc.mac(~X.CmAesKey, ~M.CmBlock, ~16n, ~X.CmAes.encrypt, ~CM.CrBlk128.of_octets, ~CM.CrBlk128.to_octets, kk, m), Xc.CrXcbc.key(~X.CmAesKey, ~M.CmBlock, ~16n, ~A.CrAes.key, ~X.CmAes.encrypt, ~CM.CrBlk128.of_octets, ~CM.CrBlk128.to_octets, k)))
332
333def vmac_aes(~words: Nat, +k: B.Octets, +n: B.Octets, +m: B.Octets) -> String:
334 show(Result.bind(&2, &2, Cr.CrError, Vm.CrVmacKey<X.CmAesKey>, B.Octets, Vm.CrVmac.key(~X.CmAesKey, ~M.CmBlock, ~16n, ~A.CrAes.key, ~X.CmAes.encrypt, ~CM.CrBlk128.of_octets, ~CM.CrBlk128.to_octets, ~words, k), kk => Vm.CrVmac.tag(~X.CmAesKey, ~M.CmBlock, ~16n, ~X.CmAes.encrypt, ~CM.CrBlk128.of_octets, ~CM.CrBlk128.to_octets, kk, n, m)))
335
336def rep(n: Nat, +s: String, acc: String) -> String:
337 match n:
338 case 0n:
339 acc
340 case 1n+p:
341 rep(p, s, cat(acc, s))
342def cr.check.first.of(m: Maybe<&2, U32>) -> U32:
343 match m:
344 case Some{b}:
345 b
346 case None{}:
347 0
348
349def cr.check.first(+o: B.Octets) -> U32:
350 cr.check.first.of(B.Octets.at(o, 0n))
351
352
353
354def hs_hash(~H: Data, ~init: H, ~update: H -> B.Octets -> H, ~final: H -> B.Octets, ~digest: B.Octets -> B.Octets, +a: B.Octets, +b: B.Octets, +c: B.Octets) -> String:
355 cat(B.Octets.hex(digest(B.Octets.append(a, B.Octets.append(b, c)))), cat(" ", B.Octets.hex(H.CrHash.of_pieces(~H, ~init, ~update, ~final, [a, b, c]))))
356
357
358
359def hs_hmac(~H: Data, ~bsize: Nat, ~init: H, ~update: H -> B.Octets -> H, ~final: H -> B.Octets, ~digest: B.Octets -> B.Octets, +k: B.Octets, +a: B.Octets, +b: B.Octets) -> String:
360 +kk = Hm.CrHmac.key(~bsize, ~digest, k)
361 +t = Hm.CrHmac.mac(~digest, kk, B.Octets.append(a, b))
362 +t2 = Hm.CrHmac.finish(~H, ~init, ~update, ~final, Hm.CrHmac.update(~H, ~update, Hm.CrHmac.update(~H, ~update, Hm.CrHmac.start(~H, ~init, ~update, kk), a), b))
363 cat(B.Octets.hex(t), cat(" ", cat(B.Octets.hex(t2), cat(" ", yes_no(Hm.CrHmac.verify(~digest, kk, B.Octets.append(a, b), t))))))
364
365
366
367def michael_mac.of(r: Result<&2, &2, Cr.CrError, Hs.HsW2>, +m: B.Octets) -> String:
368 match r:
369 case Fail{e}:
370 err(e)
371 case Done{+k}:
372 +t = HMI.HsMichael.mic(k, m)
373 cat(B.Octets.hex(t), cat(" ", cat(yes_no(MIC.CrMichael.verify(k, m, t)), cat(" ", yes_no(MIC.CrMichael.verify(k, m, B.Octets.append(B.Octets.of_list([U32.xor(cr.check.first(t), 1)]), B.Octets.drop(1n, t))))))))
374
375def michael_mac(k: B.Octets, +m: B.Octets) -> String:
376 michael_mac.of(MIC.CrMichael.key(k), m)
377
378def pbkdf2_sha1(+p: B.Octets, +s: B.Octets, c: U32, n: Nat) -> String:
379 show(PbI.CrPbkdf2.hmac_sha1(p, s, c, n))
380
381def pbkdf2_sha256(+p: B.Octets, +s: B.Octets, c: U32, n: Nat) -> String:
382 show(PbI.CrPbkdf2.hmac_sha256(p, s, c, n))
383
384
385def pbkdf2_md5_kdf(+p: B.Octets, +s: B.Octets, n: Nat) -> String:
386 show(Pb.CrPbkdf2.derive(~Hm.CrHmacKey, ~Hm.CrHmac.key_of(~64n, ~HM5.HsMd5.digest), ~Hm.CrHmac.mac(~HM5.HsMd5.digest), ~16n, ~1000, p, s, B.Octets.of_string("ignored"), n))
387
388
389
390
391def kbkdf_counter(n: Nat) -> String:
392 show(KbI.CrKbkdf.hmac_sha256_counter(B.Octets.of_string("secret key bytes"), B.Octets.of_string("label"), B.Octets.of_string("context"), n))
393
394def kbkdf_feedback(n: Nat) -> String:
395 show(KbI.CrKbkdf.hmac_sha256_feedback(B.Octets.of_string("secret key bytes"), B.Octets.of_string("an iv"), B.Octets.of_string("label"), B.Octets.of_string("context"), n))
396
397def kbkdf_short_counter(n: Nat) -> String:
398 show(Kb.CrKbkdf.counter_kdf(~Hm.CrHmacKey, ~Hm.CrHmac.key_of(~64n, ~SH.CrSha2.sha256), ~Hm.CrHmac.mac(~SH.CrSha2.sha256), ~32n, ~1n, ~4n, B.Octets.of_string("k"), B.Octets.empty(), B.Octets.empty(), n))
399
400def main() -> IO(Unit):
401 +k16 = hx("0123456789abcdeffedcba9876543210")
402 +z16 = B.Octets.zeros(16n)
403 +nk = hx("2b7e151628aed2a6abf7158809cf4f3c")
404 +f4 = hx("6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710")
405 +ck = hx("c0c1c2c3c4c5c6c7c8c9cacbcccdcecf")
406 do IO<Unit>:
407 IO.print(aes(hx("000102030405060708090a0b0c0d0e0f"), hx("00112233445566778899aabbccddeeff")))
408 IO.print(aes(hx("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f"), hx("00112233445566778899aabbccddeeff")))
409 IO.print(lc64(~D.LcDesKey, ~LC.CrLc.key(~D.LcDesKey, ~D.LcDes.key), ~D.LcDes.encrypt, ~D.LcDes.decrypt, hx("0101010101010101"), hx("8000000000000000")))
410 IO.print(lc64(~T.LcTdesKey, ~LC.CrLc.key(~T.LcTdesKey, ~T.LcTdes.key), ~T.LcTdes.encrypt, ~T.LcTdes.decrypt, hx("0123456789abcdef23456789abcdef01456789abcdef0123"), B.Octets.of_string("The qufc")))
411 IO.print(lc128(~S.LcSm4Key, ~LC.CrLc.key(~S.LcSm4Key, ~S.LcSm4.key), ~S.LcSm4.encrypt, ~S.LcSm4.decrypt, k16, k16))
412 IO.print(lc128(~Ca.LcCamelliaKey, ~LC.CrLc.key(~Ca.LcCamelliaKey, ~Ca.LcCamellia.key), ~Ca.LcCamellia.encrypt, ~Ca.LcCamellia.decrypt, k16, k16))
413 IO.print(lc128(~Sp.LcSerpentKey, ~LC.CrLc.key(~Sp.LcSerpentKey, ~Sp.LcSerpent.key), ~Sp.LcSerpent.encrypt, ~Sp.LcSerpent.decrypt, z16, z16))
414 IO.print(lc128(~Tf.LcTwofishKey, ~LC.CrLc.key(~Tf.LcTwofishKey, ~Tf.LcTwofish.key), ~Tf.LcTwofish.encrypt, ~Tf.LcTwofish.decrypt, z16, z16))
415 IO.print(aes_ecb(nk, f4))
416 IO.print(aes_cbc(nk, hx("000102030405060708090a0b0c0d0e0f"), f4))
417 IO.print(show(aes_ctr(nk, hx("f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff"), f4)))
418 IO.print(tdes_cbc(hx("0123456789abcdef23456789abcdef01456789abcdef0123"), hx("f69f2445df4f9b17"), B.Octets.of_string("The qufck brown fox jump")))
419 IO.print(show(des_ctr_pieces(hx("0123456789abcdef"), hx("fffffffffffffffe"), B.Octets.zeros(5n), B.Octets.zeros(14n))))
420 IO.print(show(rc4(hx("0102030405"), 16n)))
421 IO.print(show(rc4_pieces(hx("0102030405"))))
422 IO.print(show(rc4_drop(hx("0102030405"))))
423 IO.print(cmac(nk, B.Octets.empty()))
424 IO.print(cmac(nk, B.Octets.take(16n, f4)))
425 IO.print(cmac(nk, B.Octets.take(40n, f4)))
426 IO.print(cmac(nk, f4))
427 IO.print(ccm(~8n, ck, hx("00000003020100a0a1a2a3a4a5"), hx("0001020304050607"), hx("08090a0b0c0d0e0f101112131415161718191a1b1c1d1e")))
428 IO.print(ccm(~8n, ck, hx("00000004030201a0a1a2a3a4a5"), hx("0001020304050607"), hx("08090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f")))
429 IO.print(ccm(~8n, ck, hx("00000005040302a0a1a2a3a4a5"), hx("0001020304050607"), hx("08090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20")))
430 IO.print(B.Octets.hex(CR.CrCrc32.digest(~C.crc32.spec(), B.Octets.of_string("123456789"))))
431 IO.print(B.Octets.hex(CR.CrCrc64.digest(~C.crc64_xz.spec(), B.Octets.of_string("123456789"))))
432 IO.print(crc32_pieces(B.Octets.of_string("1234"), B.Octets.empty(), B.Octets.of_string("56789")))
433 IO.print(hmac_crc32(B.Octets.of_string("key"), B.Octets.of_string("The quick"), B.Octets.of_string(" brown fox")))
434 IO.print(hmac_crc64(big(100n), B.Octets.of_string("a message")))
435 IO.print(hkdf_crc32(B.Octets.of_string("secret"), B.Octets.of_string("salt"), B.Octets.of_string("info"), 10n))
436 IO.print(hkdf_crc32_max(B.Octets.of_string("secret")))
437 IO.print(sm4_cbc_big(k16, z16, big(32768n)))
438 IO.print(aes_ctr_big(nk, hx("f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff"), big(32771n)))
439 IO.print(show(Bk.CrBlockCipher.encrypt_with(~X.CmAesKey, ~M.CmBlock, ~A.CrAes.key, ~X.CmAes.encrypt, ~CM.CrBlk128.of_octets, ~CM.CrBlk128.to_octets, B.Octets.zeros(24n), z16)))
440 IO.print(show(Bk.CrBlockCipher.encrypt_with(~X.CmAesKey, ~M.CmBlock, ~A.CrAes.key, ~X.CmAes.encrypt, ~CM.CrBlk128.of_octets, ~CM.CrBlk128.to_octets, z16, B.Octets.zeros(15n))))
441 IO.print(show(aes_ctr(nk, B.Octets.zeros(12n), f4)))
442 IO.print(aes_cbc(nk, z16, B.Octets.zeros(17n)))
443 IO.print(show(rc4(B.Octets.empty(), 4n)))
444 IO.print(hkdf_crc32(B.Octets.of_string("secret"), B.Octets.empty(), B.Octets.empty(), 1021n))
445
446 IO.print(sha(~SH.CrSha2.sha256, B.Octets.of_string("abc")))
447 IO.print(sha(~SH.CrSha2.sha256, B.Octets.of_string("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq")))
448 IO.print(sha(~SH.CrSha2.sha512, B.Octets.of_string("abc")))
449 IO.print(sha_pieces3(~SH.CrSha2.init256(), B.Octets.of_string("abcdbcdecdefdefgefgh"), B.Octets.empty(), B.Octets.of_string("fghighijhijkijkljklmklmnlmnomnopnopq")))
450 IO.print(sha_pieces3(~SH.CrSha2.init512(), B.Octets.of_string("a"), B.Octets.of_string("b"), B.Octets.of_string("c")))
451 IO.print(sha(~S3.CrSha3.sha3_256, B.Octets.of_string("abc")))
452 IO.print(sha(~S3.CrSha3.sha3_256, B.Octets.empty()))
453 IO.print(sha(~S3.CrSha3.sha3_256, hx("a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3")))
454 IO.print(B.Octets.hex(H.CrHash.of_pieces(~S3.CrSha3State, ~S3.CrSha3.init(), ~S3.CrSha3.update, ~S3.CrSha3.final, [hx("a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3"), B.Octets.empty(), hx("a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3a3")])))
455 IO.print(B.Octets.hex(hmac_sha(~136n, ~S3.CrSha3.sha3_256, hx("4a656665"), hx("7768617420646f2079612077616e7420666f72206e6f7468696e673f"))))
456 IO.print(B.Octets.hex(hmac_sha(~64n, ~SH.CrSha2.sha256, hx("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b"), hx("4869205468657265"))))
457 IO.print(B.Octets.hex(hmac_sha(~128n, ~SH.CrSha2.sha512, hx("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b"), hx("4869205468657265"))))
458 IO.print(B.Octets.hex(hmac_sha(~64n, ~SH.CrSha2.sha256, hx("4a656665"), hx("7768617420646f2079612077616e7420666f72206e6f7468696e673f"))))
459 IO.print(B.Octets.hex(hmac_sha(~128n, ~SH.CrSha2.sha512, hx("4a656665"), hx("7768617420646f2079612077616e7420666f72206e6f7468696e673f"))))
460 IO.print(B.Octets.hex(hmac_sha(~64n, ~SH.CrSha2.sha256, hx("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"), hx("dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"))))
461 IO.print(B.Octets.hex(hmac_sha(~128n, ~SH.CrSha2.sha512, hx("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"), hx("dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"))))
462 IO.print(B.Octets.hex(hmac_sha(~64n, ~SH.CrSha2.sha256, hx("0102030405060708090a0b0c0d0e0f10111213141516171819"), hx("cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd"))))
463 IO.print(B.Octets.hex(hmac_sha(~128n, ~SH.CrSha2.sha512, hx("0102030405060708090a0b0c0d0e0f10111213141516171819"), hx("cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd"))))
464 IO.print(B.Octets.hex(B.Octets.take(16n, hmac_sha(~64n, ~SH.CrSha2.sha256, hx("0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c"), hx("546573742057697468205472756e636174696f6e")))))
465 IO.print(B.Octets.hex(B.Octets.take(16n, hmac_sha(~128n, ~SH.CrSha2.sha512, hx("0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c"), hx("546573742057697468205472756e636174696f6e")))))
466 IO.print(B.Octets.hex(hmac_sha(~64n, ~SH.CrSha2.sha256, hx("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"), hx("54657374205573696e67204c6172676572205468616e20426c6f636b2d53697a65204b6579202d2048617368204b6579204669727374"))))
467 IO.print(B.Octets.hex(hmac_sha(~128n, ~SH.CrSha2.sha512, hx("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"), hx("54657374205573696e67204c6172676572205468616e20426c6f636b2d53697a65204b6579202d2048617368204b6579204669727374"))))
468 IO.print(B.Octets.hex(hmac_sha(~64n, ~SH.CrSha2.sha256, hx("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"), hx("5468697320697320612074657374207573696e672061206c6172676572207468616e20626c6f636b2d73697a65206b657920616e642061206c6172676572207468616e20626c6f636b2d73697a6520646174612e20546865206b6579206e6565647320746f20626520686173686564206265666f7265206265696e6720757365642062792074686520484d414320616c676f726974686d2e"))))
469 IO.print(B.Octets.hex(hmac_sha(~128n, ~SH.CrSha2.sha512, hx("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"), hx("5468697320697320612074657374207573696e672061206c6172676572207468616e20626c6f636b2d73697a65206b657920616e642061206c6172676572207468616e20626c6f636b2d73697a6520646174612e20546865206b6579206e6565647320746f20626520686173686564206265666f7265206265696e6720757365642062792074686520484d414320616c676f726974686d2e"))))
470 IO.print(hmac_sha256_pieces(hx("4a656665"), B.Octets.of_string("what do ya"), B.Octets.of_string(" want for nothing?")))
471 IO.print(hkdf_sha256(hx("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b"), hx("000102030405060708090a0b0c"), hx("f0f1f2f3f4f5f6f7f8f9"), 42n))
472 IO.print(hkdf_sha256(hx("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f"), hx("606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeaf"), hx("b0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff"), 82n))
473 IO.print(hkdf_sha256(hx("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b"), hx(""), hx(""), 42n))
474 IO.print(lc128(~Ar.LcAriaKey, ~LC.CrLc.key(~Ar.LcAriaKey, ~Ar.LcAria.key), ~Ar.LcAria.encrypt, ~Ar.LcAria.decrypt, hx("000102030405060708090a0b0c0d0e0f"), hx("00112233445566778899aabbccddeeff")))
475 IO.print(lc128(~C6.LcCast6Key, ~LC.CrLc.key(~C6.LcCast6Key, ~C6.LcCast6.key), ~C6.LcCast6.encrypt, ~C6.LcCast6.decrypt, hx("2342bb9efa38542c0af75647f29f615d"), z16))
476 IO.print(lc64(~Bf.LcBlowfishKey, ~LC.CrLc.key(~Bf.LcBlowfishKey, ~Bf.LcBlowfish.key), ~Bf.LcBlowfish.encrypt, ~Bf.LcBlowfish.decrypt, hx("fedcba9876543210"), hx("0123456789abcdef")))
477 IO.print(lc64(~C5.LcCast5Key, ~LC.CrLc.key(~C5.LcCast5Key, ~C5.LcCast5.key), ~C5.LcCast5.encrypt, ~C5.LcCast5.decrypt, hx("0123456712345678234567893456789a"), hx("0123456789abcdef")))
478 IO.print(lc64(~Te.LcTeaKey, ~LC.CrLc.key(~Te.LcTeaKey, ~Te.LcTea.key), ~Te.LcTea.encrypt, ~Te.LcTea.decrypt, hx("000102030405060708090a0b0c0d0e0f"), hx("0001020304050607")))
479 IO.print(lc64(~Xt.LcXteaKey, ~LC.CrLc.key(~Xt.LcXteaKey, ~Xt.LcXtea.key), ~Xt.LcXtea.encrypt, ~Xt.LcXtea.decrypt, hx("000102030405060708090a0b0c0d0e0f"), hx("0001020304050607")))
480 IO.print(lc64(~Fc.LcFcryptKey, ~LC.CrLc.key(~Fc.LcFcryptKey, ~Fc.LcFcrypt.key), ~Fc.LcFcrypt.encrypt, ~Fc.LcFcrypt.decrypt, hx("114477aadd003366"), hx("123456789abcdef0")))
481 IO.print(pcbc_fc(hx("3141592653589793"), hx("2718281828459045"), hx("74686973206973206120746573740000")))
482 IO.print(lrw_aes(hx("4562ac25f828176d4c268414b5680185258e2a05e73e9d03ee5a830ccc094c87"), hx("00000000000000000000000000000001"), hx("30313233343536373839414243444546")))
483 IO.print(lrw_aes(hx("59704714f557478cd779e80f548879440d48f0b7b15a53ea1caa6b29c2cafbaf"), hx("00000000000000000000000000000002"), hx("30313233343536373839414243444546")))
484 IO.print(lrw_aes(hx("d82a9134b26a565030fe69e2377f9847cdf90b160c648fb6b00d0d1bae85871f"), hx("00000000000000000000000200000000"), hx("30313233343536373839414243444546")))
485 IO.print(lrw_aes(hx("f8d476ffd646ee6c2384cb1c77d6195dfef1a9f37bbc8d21a79c21f8cb900289a845348ec8c5b5f126f50e76fefd1b1e"), hx("00000000000000000000000000000001"), hx("30313233343536373839414243444546")))
486 IO.print(lrw_aes(hx("fb7615b23d80891dd470980bc79584c8b2fb64ce6097878d17fce45a49e830b76e7817e72d5e12d46064047af12f9e0c"), hx("00000000000000000000000200000000"), hx("30313233343536373839414243444546")))
487 IO.print(cts_aes(hx("636869636b656e207465726979616b69"), z16, hx("4920776f756c64206c696b652074686520")))
488 IO.print(cts_aes(hx("636869636b656e207465726979616b69"), z16, hx("4920776f756c64206c696b65207468652047656e6572616c20476175277320")))
489 IO.print(cts_aes(hx("636869636b656e207465726979616b69"), z16, hx("4920776f756c64206c696b65207468652047656e6572616c2047617527732043")))
490 IO.print(cts_aes(hx("636869636b656e207465726979616b69"), z16, hx("4920776f756c64206c696b65207468652047656e6572616c20476175277320436869636b656e2c20706c656173652c20616e6420776f6e746f6e20736f75702e")))
491 IO.print(cts_aes(hx("636869636b656e207465726979616b69"), z16, hx("4920776f756c64206c696b6520")))
492 IO.print(xcbc_aes(hx("000102030405060708090a0b0c0d0e0f"), B.Octets.empty()))
493 IO.print(xcbc_aes(hx("000102030405060708090a0b0c0d0e0f"), hx("000102")))
494 IO.print(xcbc_aes(hx("000102030405060708090a0b0c0d0e0f"), hx("000102030405060708090a0b0c0d0e0f")))
495 IO.print(xcbc_aes(hx("000102030405060708090a0b0c0d0e0f"), hx("000102030405060708090a0b0c0d0e0f10111213")))
496 IO.print(xcbc_aes(hx("000102030405060708090a0b0c0d0e0f"), hx("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f")))
497 IO.print(xcbc_aes(hx("000102030405060708090a0b0c0d0e0f"), hx("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f2021")))
498 IO.print(xcbc_aes(hx("000102030405060708090a0b0c0d0e0f"), B.Octets.zeros(1000n)))
499 IO.print(vmac_aes(~1n, B.Octets.of_string("abcdefghijklmnop"), B.Octets.of_string("bcdefghi"), B.Octets.empty()))
500 IO.print(vmac_aes(~2n, B.Octets.of_string("abcdefghijklmnop"), B.Octets.of_string("bcdefghi"), B.Octets.empty()))
501 IO.print(vmac_aes(~1n, B.Octets.of_string("abcdefghijklmnop"), B.Octets.of_string("bcdefghi"), B.Octets.of_string("abc")))
502 IO.print(vmac_aes(~2n, B.Octets.of_string("abcdefghijklmnop"), B.Octets.of_string("bcdefghi"), B.Octets.of_string("abc")))
503 IO.print(vmac_aes(~1n, B.Octets.of_string("abcdefghijklmnop"), B.Octets.of_string("bcdefghi"), B.Octets.of_string(rep(16n, "abc", ""))))
504 IO.print(vmac_aes(~2n, B.Octets.of_string("abcdefghijklmnop"), B.Octets.of_string("bcdefghi"), B.Octets.of_string(rep(16n, "abc", ""))))
505 IO.print(vmac_aes(~1n, B.Octets.of_string("abcdefghijklmnop"), B.Octets.of_string("bcdefghi"), B.Octets.of_string(rep(100n, "abc", ""))))
506 IO.print(vmac_aes(~2n, B.Octets.of_string("abcdefghijklmnop"), B.Octets.of_string("bcdefghi"), B.Octets.of_string(rep(100n, "abc", ""))))
507 IO.print(hs_hash(~Hs.HsMdState<Hs.HsW4>, ~HM4.HsMd4.init(), ~HM4.HsMd4.update, ~HM4.HsMd4.final, ~HM4.HsMd4.digest, B.Octets.of_string("The quick brown fox "), B.Octets.of_string("jumps over the lazy dog, and then some more bytes to cross a block bound"), B.Octets.of_string("ary.")))
508 IO.print(hs_hash(~Hs.HsMdState<Hs.HsW4>, ~HM5.HsMd5.init(), ~HM5.HsMd5.update, ~HM5.HsMd5.final, ~HM5.HsMd5.digest, B.Octets.of_string("The quick brown fox "), B.Octets.of_string("jumps over the lazy dog, and then some more bytes to cross a block bound"), B.Octets.of_string("ary.")))
509 IO.print(hs_hash(~Hs.HsMdState<Hs.HsW5>, ~HS1.HsSha1.init(), ~HS1.HsSha1.update, ~HS1.HsSha1.final, ~HS1.HsSha1.digest, B.Octets.of_string("The quick brown fox "), B.Octets.of_string("jumps over the lazy dog, and then some more bytes to cross a block bound"), B.Octets.of_string("ary.")))
510 IO.print(hs_hash(~Hs.HsMdState<Hs.HsW5>, ~HRM.HsRipemd160.init(), ~HRM.HsRipemd160.update, ~HRM.HsRipemd160.final, ~HRM.HsRipemd160.digest, B.Octets.of_string("The quick brown fox "), B.Octets.of_string("jumps over the lazy dog, and then some more bytes to cross a block bound"), B.Octets.of_string("ary.")))
511 IO.print(hs_hash(~Hs.HsMdState<Hs.HsW8>, ~HSM.HsSm3.init(), ~HSM.HsSm3.update, ~HSM.HsSm3.final, ~HSM.HsSm3.digest, B.Octets.of_string("The quick brown fox "), B.Octets.of_string("jumps over the lazy dog, and then some more bytes to cross a block bound"), B.Octets.of_string("ary.")))
512 IO.print(hs_hash(~Hs.HsMdState<HST.HsStbCv>, ~HST.HsStreebog.init256(), ~HST.HsStreebog.update, ~HST.HsStreebog.final256, ~HST.HsStreebog.digest256, B.Octets.of_string("The quick brown fox "), B.Octets.of_string("jumps over the lazy dog, and then some more bytes to cross a block bound"), B.Octets.of_string("ary.")))
513 IO.print(hs_hash(~Hs.HsMdState<HST.HsStbCv>, ~HST.HsStreebog.init512(), ~HST.HsStreebog.update, ~HST.HsStreebog.final512, ~HST.HsStreebog.digest512, B.Octets.of_string("The quick brown fox "), B.Octets.of_string("jumps over the lazy dog, and then some more bytes to cross a block bound"), B.Octets.of_string("ary.")))
514 IO.print(hs_hmac(~Hs.HsMdState<Hs.HsW4>, ~64n, ~HM5.HsMd5.init(), ~HM5.HsMd5.update, ~HM5.HsMd5.final, ~HM5.HsMd5.digest, hx("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b"), B.Octets.of_string("Hi Th"), B.Octets.of_string("ere")))
515 IO.print(hs_hmac(~Hs.HsMdState<Hs.HsW4>, ~64n, ~HM5.HsMd5.init(), ~HM5.HsMd5.update, ~HM5.HsMd5.final, ~HM5.HsMd5.digest, B.Octets.of_string("Jefe"), B.Octets.of_string("what "), B.Octets.of_string("do ya want for nothing?")))
516 IO.print(hs_hmac(~Hs.HsMdState<Hs.HsW5>, ~64n, ~HS1.HsSha1.init(), ~HS1.HsSha1.update, ~HS1.HsSha1.final, ~HS1.HsSha1.digest, hx("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b"), B.Octets.of_string("Hi Th"), B.Octets.of_string("ere")))
517 IO.print(hs_hmac(~Hs.HsMdState<Hs.HsW5>, ~64n, ~HS1.HsSha1.init(), ~HS1.HsSha1.update, ~HS1.HsSha1.final, ~HS1.HsSha1.digest, B.Octets.of_string("Jefe"), B.Octets.of_string("what "), B.Octets.of_string("do ya want for nothing?")))
518 IO.print(hs_hmac(~Hs.HsMdState<Hs.HsW4>, ~64n, ~HM4.HsMd4.init(), ~HM4.HsMd4.update, ~HM4.HsMd4.final, ~HM4.HsMd4.digest, hx("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445"), B.Octets.of_string("The quick brown fox "), B.Octets.of_string("jumps over the lazy dog, and then some more bytes to cross a block bound")))
519 IO.print(hs_hmac(~Hs.HsMdState<Hs.HsW5>, ~64n, ~HRM.HsRipemd160.init(), ~HRM.HsRipemd160.update, ~HRM.HsRipemd160.final, ~HRM.HsRipemd160.digest, hx("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445"), B.Octets.of_string("The quick brown fox "), B.Octets.of_string("jumps over the lazy dog, and then some more bytes to cross a block bound")))
520 IO.print(hs_hmac(~Hs.HsMdState<Hs.HsW8>, ~64n, ~HSM.HsSm3.init(), ~HSM.HsSm3.update, ~HSM.HsSm3.final, ~HSM.HsSm3.digest, hx("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445"), B.Octets.of_string("The quick brown fox "), B.Octets.of_string("jumps over the lazy dog, and then some more bytes to cross a block bound")))
521 IO.print(hs_hmac(~Hs.HsMdState<HST.HsStbCv>, ~64n, ~HST.HsStreebog.init256(), ~HST.HsStreebog.update, ~HST.HsStreebog.final256, ~HST.HsStreebog.digest256, hx("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445"), B.Octets.of_string("The quick brown fox "), B.Octets.of_string("jumps over the lazy dog, and then some more bytes to cross a block bound")))
522 IO.print(hs_hmac(~Hs.HsMdState<HST.HsStbCv>, ~64n, ~HST.HsStreebog.init512(), ~HST.HsStreebog.update, ~HST.HsStreebog.final512, ~HST.HsStreebog.digest512, hx("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445"), B.Octets.of_string("The quick brown fox "), B.Octets.of_string("jumps over the lazy dog, and then some more bytes to cross a block bound")))
523 IO.print(michael_mac(hx("0000000000000000"), B.Octets.empty()))
524 IO.print(michael_mac(hx("82925c1ca1d130b8"), B.Octets.of_string("M")))
525 IO.print(michael_mac(hx("434721ca40639b3f"), B.Octets.of_string("Mi")))
526 IO.print(michael_mac(hx("e8f9becae97e5d29"), B.Octets.of_string("Mic")))
527 IO.print(michael_mac(hx("90038fc6cf13c1db"), B.Octets.of_string("Mich")))
528 IO.print(michael_mac(hx("d55e100510128986"), B.Octets.of_string("Michael")))
529 IO.print(michael_mac(hx("00000000000000"), B.Octets.empty()))
530 IO.print(pbkdf2_sha1(B.Octets.of_string("password"), B.Octets.of_string("salt"), 1, 20n))
531 IO.print(pbkdf2_sha1(B.Octets.of_string("password"), B.Octets.of_string("salt"), 2, 20n))
532 IO.print(pbkdf2_sha1(B.Octets.of_string("password"), B.Octets.of_string("salt"), 4096, 20n))
533 IO.print(pbkdf2_sha1(B.Octets.of_string("passwordPASSWORDpassword"), B.Octets.of_string("saltSALTsaltSALTsaltSALTsaltSALTsalt"), 4096, 25n))
534 IO.print(pbkdf2_sha1(hx("7061737300776f7264"), hx("7361006c74"), 4096, 16n))
535 IO.print(pbkdf2_sha256(B.Octets.of_string("passwd"), B.Octets.of_string("salt"), 1, 64n))
536 IO.print(pbkdf2_sha256(B.Octets.of_string("Password"), B.Octets.of_string("NaCl"), 80000, 64n))
537 IO.print(pbkdf2_sha1(B.Octets.of_string("password"), B.Octets.of_string("salt"), 0, 20n))
538 IO.print(pbkdf2_md5_kdf(B.Octets.of_string("secret"), B.Octets.of_string("NaCl"), 40n))
539 IO.print(kbkdf_counter(42n))
540 IO.print(kbkdf_feedback(42n))
541 IO.print(kbkdf_short_counter(8161n))
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675