| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package testkit |
| 6 |
|
| 7 |
import ( |
| 8 |
"bytes" |
| 9 |
"compress/zlib" |
| 10 |
"crypto/hmac" |
| 11 |
"crypto/sha256" |
| 12 |
"encoding/base64" |
| 13 |
"fmt" |
| 14 |
"io" |
| 15 |
"os" |
| 16 |
"strconv" |
| 17 |
"strings" |
| 18 |
|
| 19 |
"c2sp.org/CCTV/age/internal/bech32" |
| 20 |
"golang.org/x/crypto/chacha20" |
| 21 |
"golang.org/x/crypto/chacha20poly1305" |
| 22 |
"golang.org/x/crypto/curve25519" |
| 23 |
"golang.org/x/crypto/hkdf" |
| 24 |
"golang.org/x/crypto/scrypt" |
| 25 |
) |
| 26 |
|
| 27 |
var TestFileKey = []byte("YELLOW SUBMARINE") |
| 28 |
|
| 29 |
var _, TestX25519Identity, _ = bech32.Decode( |
| 30 |
"AGE-SECRET-KEY-1EGTZVFFV20835NWYV6270LXYVK2VKNX2MMDKWYKLMGR48UAWX40Q2P2LM0") |
| 31 |
|
| 32 |
var TestX25519Recipient, _ = curve25519.X25519(TestX25519Identity, curve25519.Basepoint) |
| 33 |
|
| 34 |
const ChunkSize = 64 * 1024 |
| 35 |
|
| 36 |
func NotCanonicalBase64(s string) string { |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
alphabet := "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" |
| 42 |
idx := strings.IndexByte(alphabet, s[len(s)-1]) |
| 43 |
return s[:len(s)-1] + string(alphabet[idx+1]) |
| 44 |
} |
| 45 |
|
| 46 |
type TestFile struct { |
| 47 |
Buf bytes.Buffer |
| 48 |
Rand func(n int) []byte |
| 49 |
|
| 50 |
fileKey []byte |
| 51 |
streamKey []byte |
| 52 |
nonce [12]byte |
| 53 |
payload bytes.Buffer |
| 54 |
expect string |
| 55 |
comment string |
| 56 |
identities []string |
| 57 |
passphrases []string |
| 58 |
armor bool |
| 59 |
} |
| 60 |
|
| 61 |
func NewTestFile() *TestFile { |
| 62 |
c, _ := chacha20.NewUnauthenticatedCipher( |
| 63 |
[]byte("TEST RANDOMNESS TEST RANDOMNESS!"), make([]byte, chacha20.NonceSize)) |
| 64 |
rand := func(n int) []byte { |
| 65 |
out := make([]byte, n) |
| 66 |
c.XORKeyStream(out, out) |
| 67 |
return out |
| 68 |
} |
| 69 |
return &TestFile{Rand: rand, expect: "success", fileKey: TestFileKey} |
| 70 |
} |
| 71 |
|
| 72 |
func (f *TestFile) FileKey(key []byte) { |
| 73 |
f.fileKey = key |
| 74 |
} |
| 75 |
|
| 76 |
func (f *TestFile) TextLine(s string) { |
| 77 |
f.Buf.WriteString(s) |
| 78 |
f.Buf.WriteString("\n") |
| 79 |
} |
| 80 |
|
| 81 |
func (f *TestFile) UnreadLine() string { |
| 82 |
buf := bytes.TrimSuffix(f.Buf.Bytes(), []byte("\n")) |
| 83 |
idx := bytes.LastIndex(buf, []byte("\n")) + 1 |
| 84 |
f.Buf.Reset() |
| 85 |
f.Buf.Write(buf[:idx]) |
| 86 |
return string(buf[idx:]) |
| 87 |
} |
| 88 |
|
| 89 |
func (f *TestFile) VersionLine(v string) { |
| 90 |
f.TextLine("age-encryption.org/" + v) |
| 91 |
} |
| 92 |
|
| 93 |
func (f *TestFile) ArgsLine(args ...string) { |
| 94 |
f.TextLine(strings.Join(append([]string{"->"}, args...), " ")) |
| 95 |
} |
| 96 |
|
| 97 |
func (f *TestFile) UnreadArgsLine() []string { |
| 98 |
line := strings.TrimPrefix(f.UnreadLine(), "-> ") |
| 99 |
return strings.Split(line, " ") |
| 100 |
} |
| 101 |
|
| 102 |
var b64 = base64.RawStdEncoding.EncodeToString |
| 103 |
|
| 104 |
func (f *TestFile) Body(body []byte) { |
| 105 |
for { |
| 106 |
line := body |
| 107 |
if len(line) > 48 { |
| 108 |
line = line[:48] |
| 109 |
} |
| 110 |
f.TextLine(b64(line)) |
| 111 |
body = body[len(line):] |
| 112 |
if len(line) < 48 { |
| 113 |
break |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
func (f *TestFile) Base64Padding() { |
| 119 |
line := f.UnreadLine() |
| 120 |
paddingLen := 4 - len(line)%4 |
| 121 |
if paddingLen == 4 { |
| 122 |
paddingLen = 0 |
| 123 |
} |
| 124 |
padding := strings.Repeat("=", paddingLen) |
| 125 |
f.TextLine(line + padding) |
| 126 |
} |
| 127 |
|
| 128 |
func (f *TestFile) AEADBody(key, body []byte) { |
| 129 |
aead, _ := chacha20poly1305.New(key) |
| 130 |
f.Body(aead.Seal(nil, make([]byte, chacha20poly1305.NonceSize), body, nil)) |
| 131 |
} |
| 132 |
|
| 133 |
func x25519(scalar, point []byte) []byte { |
| 134 |
secret, err := curve25519.X25519(scalar, point) |
| 135 |
if err != nil { |
| 136 |
if strings.Contains(err.Error(), "low order point") { |
| 137 |
return make([]byte, 32) |
| 138 |
} |
| 139 |
panic(err) |
| 140 |
} |
| 141 |
return secret |
| 142 |
} |
| 143 |
|
| 144 |
func (f *TestFile) X25519(identity []byte) { |
| 145 |
f.X25519RecordIdentity(identity) |
| 146 |
f.X25519NoRecordIdentity(identity) |
| 147 |
} |
| 148 |
|
| 149 |
func (f *TestFile) X25519RecordIdentity(identity []byte) { |
| 150 |
id, _ := bech32.Encode("AGE-SECRET-KEY-", identity) |
| 151 |
f.identities = append(f.identities, id) |
| 152 |
} |
| 153 |
|
| 154 |
func (f *TestFile) X25519NoRecordIdentity(identity []byte) { |
| 155 |
share := x25519(f.Rand(32), curve25519.Basepoint) |
| 156 |
f.X25519Stanza(share, identity) |
| 157 |
} |
| 158 |
|
| 159 |
func (f *TestFile) X25519Stanza(share, identity []byte) { |
| 160 |
recipient := x25519(identity, curve25519.Basepoint) |
| 161 |
f.ArgsLine("X25519", b64(share)) |
| 162 |
|
| 163 |
|
| 164 |
secret := x25519(identity, share) |
| 165 |
key := make([]byte, 32) |
| 166 |
hkdf.New(sha256.New, secret, append(share, recipient...), |
| 167 |
[]byte("age-encryption.org/v1/X25519")).Read(key) |
| 168 |
f.AEADBody(key, f.fileKey) |
| 169 |
} |
| 170 |
|
| 171 |
func (f *TestFile) Scrypt(passphrase string, workFactor int) { |
| 172 |
f.ScryptRecordPassphrase(passphrase) |
| 173 |
f.ScryptNoRecordPassphrase(passphrase, workFactor) |
| 174 |
} |
| 175 |
|
| 176 |
func (f *TestFile) ScryptRecordPassphrase(passphrase string) { |
| 177 |
f.passphrases = append(f.passphrases, passphrase) |
| 178 |
} |
| 179 |
|
| 180 |
func (f *TestFile) ScryptNoRecordPassphrase(passphrase string, workFactor int) { |
| 181 |
salt := f.Rand(16) |
| 182 |
f.ScryptNoRecordPassphraseWithSalt(passphrase, workFactor, salt) |
| 183 |
} |
| 184 |
|
| 185 |
func (f *TestFile) ScryptNoRecordPassphraseWithSalt(passphrase string, workFactor int, salt []byte) { |
| 186 |
f.ArgsLine("scrypt", b64(salt), strconv.Itoa(workFactor)) |
| 187 |
key, err := scrypt.Key([]byte(passphrase), append([]byte("age-encryption.org/v1/scrypt"), salt...), |
| 188 |
1<<workFactor, 8, 1, 32) |
| 189 |
if err != nil { |
| 190 |
panic(err) |
| 191 |
} |
| 192 |
f.AEADBody(key, f.fileKey) |
| 193 |
} |
| 194 |
|
| 195 |
func (f *TestFile) HMACLine(h []byte) { |
| 196 |
f.TextLine("--- " + b64(h)) |
| 197 |
} |
| 198 |
|
| 199 |
func (f *TestFile) HMAC() { |
| 200 |
key := make([]byte, 32) |
| 201 |
hkdf.New(sha256.New, f.fileKey, nil, []byte("header")).Read(key) |
| 202 |
h := hmac.New(sha256.New, key) |
| 203 |
h.Write(f.Buf.Bytes()) |
| 204 |
h.Write([]byte("---")) |
| 205 |
f.HMACLine(h.Sum(nil)) |
| 206 |
} |
| 207 |
|
| 208 |
func (f *TestFile) Nonce() { |
| 209 |
nonce := f.Rand(16) |
| 210 |
f.streamKey = make([]byte, 32) |
| 211 |
hkdf.New(sha256.New, f.fileKey, nonce, []byte("payload")).Read(f.streamKey) |
| 212 |
f.Buf.Write(nonce) |
| 213 |
} |
| 214 |
|
| 215 |
func (f *TestFile) PayloadChunk(size int) { |
| 216 |
plaintext := bytes.Repeat([]byte{0}, size) |
| 217 |
s, _ := chacha20.NewUnauthenticatedCipher(f.streamKey, f.nonce[:]) |
| 218 |
s.SetCounter(1) |
| 219 |
s.XORKeyStream(plaintext, plaintext) |
| 220 |
f.payloadChunk(plaintext) |
| 221 |
} |
| 222 |
|
| 223 |
func (f *TestFile) payloadChunk(plaintext []byte) { |
| 224 |
f.payload.Write(plaintext) |
| 225 |
aead, _ := chacha20poly1305.New(f.streamKey) |
| 226 |
f.Buf.Write(aead.Seal(nil, f.nonce[:], plaintext, nil)) |
| 227 |
|
| 228 |
for i := 10; i >= 0; i-- { |
| 229 |
f.nonce[i]++ |
| 230 |
if f.nonce[i] != 0 { |
| 231 |
break |
| 232 |
} |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
func (f *TestFile) PayloadChunkFinal(size int) { |
| 237 |
f.nonce[11] = 1 |
| 238 |
f.PayloadChunk(size) |
| 239 |
} |
| 240 |
|
| 241 |
func (f *TestFile) Payload(plaintext string) { |
| 242 |
f.Nonce() |
| 243 |
f.nonce[11] = 1 |
| 244 |
f.payloadChunk([]byte(plaintext)) |
| 245 |
} |
| 246 |
|
| 247 |
func (f *TestFile) ExpectHeaderFailure() { |
| 248 |
f.expect = "header failure" |
| 249 |
} |
| 250 |
|
| 251 |
func (f *TestFile) ExpectArmorFailure() { |
| 252 |
f.armor = true |
| 253 |
f.expect = "armor failure" |
| 254 |
} |
| 255 |
|
| 256 |
func (f *TestFile) ExpectPayloadFailure() { |
| 257 |
f.expect = "payload failure" |
| 258 |
f.payload.Reset() |
| 259 |
} |
| 260 |
|
| 261 |
func (f *TestFile) ExpectPartialPayload(goodBytes int) { |
| 262 |
f.expect = "payload failure" |
| 263 |
payload := f.payload.Bytes() |
| 264 |
f.payload.Reset() |
| 265 |
f.payload.Write(payload[:goodBytes]) |
| 266 |
} |
| 267 |
|
| 268 |
func (f *TestFile) ExpectHMACFailure() { |
| 269 |
f.expect = "HMAC failure" |
| 270 |
} |
| 271 |
|
| 272 |
func (f *TestFile) ExpectNoMatch() { |
| 273 |
f.expect = "no match" |
| 274 |
} |
| 275 |
|
| 276 |
func (f *TestFile) Comment(c string) { |
| 277 |
f.comment = c |
| 278 |
} |
| 279 |
|
| 280 |
func (f *TestFile) BeginArmor(t string) { |
| 281 |
f.armor = true |
| 282 |
f.TextLine("-----BEGIN " + t + "-----") |
| 283 |
} |
| 284 |
|
| 285 |
func (f *TestFile) EndArmor(t string) { |
| 286 |
f.armor = true |
| 287 |
f.TextLine("-----END " + t + "-----") |
| 288 |
} |
| 289 |
|
| 290 |
func (f *TestFile) Bytes() []byte { |
| 291 |
out := make([]byte, f.Buf.Len()) |
| 292 |
copy(out, f.Buf.Bytes()) |
| 293 |
return out |
| 294 |
} |
| 295 |
|
| 296 |
func (f *TestFile) Generate() { |
| 297 |
fmt.Printf("expect: %s\n", f.expect) |
| 298 |
if f.expect == "success" || f.expect == "payload failure" { |
| 299 |
fmt.Printf("payload: %x\n", sha256.Sum256(f.payload.Bytes())) |
| 300 |
} |
| 301 |
fmt.Printf("file key: %x\n", f.fileKey) |
| 302 |
for _, id := range f.identities { |
| 303 |
fmt.Printf("identity: %s\n", id) |
| 304 |
} |
| 305 |
for _, p := range f.passphrases { |
| 306 |
fmt.Printf("passphrase: %s\n", p) |
| 307 |
} |
| 308 |
if f.armor { |
| 309 |
fmt.Printf("armored: yes\n") |
| 310 |
} |
| 311 |
if f.comment != "" { |
| 312 |
fmt.Printf("comment: %s\n", f.comment) |
| 313 |
} |
| 314 |
out := io.Writer(os.Stdout) |
| 315 |
if f.Buf.Len() > 1024 { |
| 316 |
fmt.Printf("compressed: zlib\n") |
| 317 |
out, _ = zlib.NewWriterLevel(os.Stdout, zlib.BestCompression) |
| 318 |
defer out.(*zlib.Writer).Close() |
| 319 |
} |
| 320 |
fmt.Println() |
| 321 |
io.Copy(out, &f.Buf) |
| 322 |
} |