crypto_vectors.mx raw
1 // Known-answer vectors for the symmetric and hash primitives whose state is
2 // carried as limbs and buffers: AES (FIPS-197 C.1-C.3, SP 800-38A CBC, the
3 // standard GCM tag cases), RC4, BLAKE2b-512/256 (RFC 7693 appendix A), and an
4 // Argon2id self-consistency check.
5 //
6 // These packages had no fixture at all, so a refactor of their state layout -
7 // a fixed extent becoming a slice, a table moving into a package variable
8 // built in init() - could only be caught by "the handshake still works", which
9 // exercises exactly one code path per primitive. The AES vectors are the
10 // round-key expansion in all three key sizes; the GCM cases are the tag
11 // derivation, which is where a zeroed state word shows up.
12 //
13 // usage: crypto_vectors exit 0 when every vector matches
14 package main
15
16 import (
17 "crypto/aes"
18 "crypto/cipher"
19 "crypto/rc4"
20 "golang.org/x/crypto/argon2"
21 "golang.org/x/crypto/blake2b"
22 "os"
23 )
24
25 func hexOf(b []byte) (s string) {
26 const d = "0123456789abcdef"
27 out := []byte{:int32(len(b)) * 2}
28 for i := int32(0); i < int32(len(b)); i++ {
29 out[i*2] = d[b[i]>>4]
30 out[i*2+1] = d[b[i]&0xf]
31 }
32 return string(out)
33 }
34
35 func unhex(s []byte) (b []byte) {
36 b = []byte{:int32(len(s)) / 2}
37 for i := int32(0); i < int32(len(b)); i++ {
38 hi := hexVal(s[i*2])
39 lo := hexVal(s[i*2+1])
40 b[i] = hi<<4 | lo
41 }
42 return b
43 }
44
45 func hexVal(c byte) (v byte) {
46 if c >= '0' && c <= '9' {
47 return c - '0'
48 }
49 return c - 'a' + 10
50 }
51
52 // checker carries the failure count. A package global cannot be mutated
53 // outside init(), and a closure cannot capture, so the count lives in a
54 // self-mutating type passed as a receiver.
55 type checker struct {
56 fails int32
57 }
58
59 func (c *checker) want(name []byte, got []byte, expect []byte) {
60 if string(got) == string(expect) {
61 println("ok ", name)
62 return
63 }
64 c.fails++
65 println("FAIL", name)
66 println(" got ", hexOf(got))
67 println(" want ", hexOf(expect))
68 }
69
70 func (c *checker) aesBlock(keyHex []byte, name []byte, ptHex []byte, wantHex []byte) {
71 key := unhex(keyHex)
72 pt := unhex(ptHex)
73 blk, err := aes.NewCipher(key)
74 if err != nil {
75 c.fails++
76 println("FAIL", name, "newcipher:", err.Error())
77 return
78 }
79 dst := []byte{:16}
80 blk.Encrypt(dst, pt)
81 c.want(name, dst, unhex(wantHex))
82 back := []byte{:16}
83 blk.Decrypt(back, dst)
84 c.want([]byte("dec ") | name, back, pt)
85 }
86
87 func main() {
88 // Termination: a batch program must still be stoppable, so the entry
89 // handles the channel the runtime closes on SIGINT/SIGTERM.
90 select {
91 case <-shutdown:
92 return
93 default:
94 }
95
96 var c checker
97 // FIPS-197 appendix C: the same plaintext under all three key sizes.
98 pt := []byte("00112233445566778899aabbccddeeff")
99 c.aesBlock([]byte("000102030405060708090a0b0c0d0e0f"), []byte("aes128"), pt,
100 []byte("69c4e0d86a7b0430d8cdb78070b4c55a"))
101 c.aesBlock([]byte("000102030405060708090a0b0c0d0e0f1011121314151617"), []byte("aes192"), pt,
102 []byte("dda97ca4864cdfe06eaf70a0ec0d7191"))
103 c.aesBlock([]byte("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f"), []byte("aes256"), pt,
104 []byte("8ea2b7ca516745bfeafc49904b496089"))
105
106 // SP 800-38A F.2.1: one CBC block.
107 {
108 key := unhex([]byte("2b7e151628aed2a6abf7158809cf4f3c"))
109 iv := unhex([]byte("000102030405060708090a0b0c0d0e0f"))
110 src := unhex([]byte("6bc1bee22e409f96e93d7e117393172a"))
111 blk, _ := aes.NewCipher(key)
112 mode := cipher.NewCBCEncrypter(blk, iv)
113 dst := []byte{:16}
114 mode.CryptBlocks(dst, src)
115 c.want([]byte("aes-cbc"), dst, unhex([]byte("7649abac8119b246cee98e9b12e9197d")))
116 }
117
118 // GCM: the two standard zero-key cases (empty and one zero block).
119 {
120 blk, _ := aes.NewCipher([]byte{:16})
121 gcm, gerr := cipher.NewGCM(blk)
122 if gerr != nil {
123 c.fails++
124 println("FAIL gcm new: ", gerr.Error())
125 } else {
126 nonce := []byte{:12}
127 sealed := gcm.Seal(nil, nonce, nil, nil)
128 c.want([]byte("gcm-empty"), sealed,
129 unhex([]byte("58e2fccefa7e3061367f1d57a4e7455a")))
130 zero := []byte{:16}
131 sealed2 := gcm.Seal(nil, nonce, zero, nil)
132 c.want([]byte("gcm-zeros"), sealed2,
133 unhex([]byte("0388dace60b6a392f328c2b971b2fe78ab6e47d42cec13bdf53a67b21257bddf")))
134 opened, oerr := gcm.Open(nil, nonce, sealed2, nil)
135 if oerr != nil {
136 c.fails++
137 println("FAIL gcm open: ", oerr.Error())
138 } else {
139 c.want([]byte("gcm-open"), opened, zero)
140 }
141 }
142 }
143
144 // RC4: the classic "Key" / "Plaintext" vector.
145 {
146 rc, cerr := rc4.NewCipher([]byte("Key"))
147 if cerr != nil {
148 c.fails++
149 println("FAIL rc4 new: ", cerr.Error())
150 } else {
151 dst := []byte{:9}
152 rc.XORKeyStream(dst, []byte("Plaintext"))
153 c.want([]byte("rc4"), dst, unhex([]byte("bbf316e8d940af0ad3")))
154 }
155 }
156
157 // RFC 7693 appendix A: BLAKE2b-512("abc"). The 256-bit case is the Go
158 // stdlib's own vector, which also covers the digest's size parameter.
159 {
160 sum := blake2b.Sum512([]byte("abc"))
161 c.want([]byte("blake2b-512"),
162 sum,
163 unhex([]byte("ba80a53f981c4d0d6a2797b69f12f6e94c212f14685ac4b74b12bb6fdbffa2d17d87c5392aab792dc252d5de4533cc9518d38aa8dbf1925ab92386edd4009923")))
164 sum256 := blake2b.Sum256([]byte("abc"))
165 c.want([]byte("blake2b-256"),
166 sum256,
167 unhex([]byte("bddd813c634239723171ef3fee98579b94964e3bb1cb3e427262c8c068d52319")))
168 // The streaming interface must agree with the one-shot form.
169 h, herr := blake2b.New512(nil)
170 if herr != nil {
171 c.fails++
172 println("FAIL blake2b new: ", herr.Error())
173 } else {
174 h.Write([]byte("a"))
175 h.Write([]byte("bc"))
176 c.want([]byte("blake2b-stream"), h.Sum(nil), sum)
177 }
178 }
179
180 // Argon2id has no fixture either: check that two identical calls agree and
181 // a different password does not.
182 {
183 a := argon2.IDKey([]byte("password"), []byte("somesalt"), 1, 64, 1, 32)
184 b := argon2.IDKey([]byte("password"), []byte("somesalt"), 1, 64, 1, 32)
185 c.want([]byte("argon2id-repeat"), a, b)
186 diff := argon2.IDKey([]byte("passwore"), []byte("somesalt"), 1, 64, 1, 32)
187 if string(a) == string(diff) {
188 c.fails++
189 println("FAIL argon2id: different passwords gave the same key")
190 } else {
191 println("ok argon2id")
192 }
193 if len(a) != 32 {
194 c.fails++
195 println("FAIL argon2id length")
196 }
197 }
198
199 if c.fails != 0 {
200 println("crypto_vectors:", c.fails, "case(s) failed")
201 os.Exit(1)
202 }
203 println("crypto_vectors ok")
204 }
205