| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package main |
| 6 |
|
| 7 |
import ( |
| 8 |
"bufio" |
| 9 |
"errors" |
| 10 |
"fmt" |
| 11 |
"io" |
| 12 |
"io/ioutil" |
| 13 |
"os" |
| 14 |
"strings" |
| 15 |
"unicode" |
| 16 |
) |
| 17 |
|
| 18 |
type cipherData struct { |
| 19 |
cipher string |
| 20 |
keyLenBytes string |
| 21 |
keyLenBits string |
| 22 |
} |
| 23 |
|
| 24 |
var ( |
| 25 |
hashes = map[string]string{ |
| 26 |
"SHA-1": "SHA1", |
| 27 |
"SHA-224": "SHA224", |
| 28 |
"SHA-256": "SHA256", |
| 29 |
"SHA-384": "SHA384", |
| 30 |
"SHA-512": "SHA512", |
| 31 |
"SHA-512/224": "SHA512_224", |
| 32 |
"SHA-512/256": "SHA512_256", |
| 33 |
} |
| 34 |
|
| 35 |
ciphers = map[string]cipherData{ |
| 36 |
"AES-128 use df": {"AES", "16", "128"}, |
| 37 |
"AES-192 use df": {"AES", "24", "192"}, |
| 38 |
"AES-256 use df": {"AES", "32", "256"}, |
| 39 |
} |
| 40 |
) |
| 41 |
|
| 42 |
func scanTokens(data []byte, atEOF bool) (int, []byte, error) { |
| 43 |
|
| 44 |
lineAdv, tok, err := bufio.ScanLines(data, atEOF) |
| 45 |
switch { |
| 46 |
case err != nil: |
| 47 |
return 0, nil, err |
| 48 |
case lineAdv == 0: |
| 49 |
|
| 50 |
return 0, nil, nil |
| 51 |
case len(tok) == 0: |
| 52 |
|
| 53 |
return lineAdv, []byte{'\n'}, nil |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
adv := strings.IndexFunc(string(tok), func(r rune) bool { |
| 58 |
return !unicode.IsSpace(r) |
| 59 |
}) |
| 60 |
if adv < 0 { |
| 61 |
|
| 62 |
return lineAdv, []byte{'\n'}, nil |
| 63 |
} |
| 64 |
tok = tok[adv:] |
| 65 |
|
| 66 |
|
| 67 |
if tok[0] == '#' { |
| 68 |
return lineAdv, []byte{'\n'}, nil |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
i := strings.IndexAny(string(tok), "[]=") |
| 73 |
switch { |
| 74 |
case i == 0: |
| 75 |
tok = []byte{tok[0]} |
| 76 |
case i >= 0: |
| 77 |
tok = tok[:i] |
| 78 |
} |
| 79 |
|
| 80 |
tok = []byte(strings.TrimSpace(string(tok))) |
| 81 |
|
| 82 |
return adv + len(tok), tok, nil |
| 83 |
} |
| 84 |
|
| 85 |
type testCase map[string][]string |
| 86 |
|
| 87 |
type testSuite struct { |
| 88 |
name string |
| 89 |
params map[string]string |
| 90 |
tests []testCase |
| 91 |
} |
| 92 |
|
| 93 |
type stateFunc func(string) (stateFunc, error) |
| 94 |
|
| 95 |
type parser struct { |
| 96 |
scanner *bufio.Scanner |
| 97 |
current stateFunc |
| 98 |
|
| 99 |
suites []*testSuite |
| 100 |
currentSuite *testSuite |
| 101 |
currentTest testCase |
| 102 |
currentName string |
| 103 |
} |
| 104 |
|
| 105 |
func (p *parser) handleEndTestCaseParam(tok string) (stateFunc, error) { |
| 106 |
switch { |
| 107 |
case tok == "\n": |
| 108 |
return p.handleStartTestCaseParam, nil |
| 109 |
default: |
| 110 |
return nil, fmt.Errorf("handleEndTestCaseParam: unexpected token %v", tok) |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
func (p *parser) handleTestCaseParam(tok string) (stateFunc, error) { |
| 115 |
p.currentTest[p.currentName] = append(p.currentTest[p.currentName], tok) |
| 116 |
return p.handleEndTestCaseParam, nil |
| 117 |
} |
| 118 |
|
| 119 |
func (p *parser) handleEndTestSuiteParam2(tok string) (stateFunc, error) { |
| 120 |
switch { |
| 121 |
case tok == "\n": |
| 122 |
return p.handleStartTestSuiteParam, nil |
| 123 |
default: |
| 124 |
return nil, fmt.Errorf("handleEndTestSuiteParam2: unexpected token %v", tok) |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
func (p *parser) handleEndTestSuiteParam(tok string) (stateFunc, error) { |
| 129 |
switch { |
| 130 |
case tok == "]": |
| 131 |
return p.handleEndTestSuiteParam2, nil |
| 132 |
default: |
| 133 |
return nil, fmt.Errorf("handleEndTestSuiteParam: unexpected token %v", tok) |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
func (p *parser) handleEndTestSuiteName(tok string) (stateFunc, error) { |
| 138 |
switch { |
| 139 |
case tok == "]": |
| 140 |
p.currentSuite.name = p.currentName |
| 141 |
return p.handleEndTestSuiteParam(tok) |
| 142 |
case tok == "=": |
| 143 |
return p.handleEqual(tok) |
| 144 |
default: |
| 145 |
return nil, fmt.Errorf("handleEndTestSuiteName: unexpected token %v", tok) |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
func (p *parser) handleTestSuiteParam(tok string) (stateFunc, error) { |
| 150 |
if p.currentSuite.name == "" { |
| 151 |
p.currentSuite.name = tok |
| 152 |
} |
| 153 |
p.currentSuite.params[p.currentName] = tok |
| 154 |
return p.handleEndTestSuiteParam, nil |
| 155 |
} |
| 156 |
|
| 157 |
func (p *parser) handleParamValue(tok string) (stateFunc, error) { |
| 158 |
switch { |
| 159 |
case tok == "[" || tok == "]" || tok == "=": |
| 160 |
return nil, fmt.Errorf("handleParamValue: unexpected token %v", tok) |
| 161 |
case tok == "\n" && p.currentTest != nil: |
| 162 |
n, _ := p.handleTestCaseParam("") |
| 163 |
return n(tok) |
| 164 |
case tok == "\n": |
| 165 |
return nil, fmt.Errorf("handleParamValue: unexpected token %v", tok) |
| 166 |
case p.currentTest != nil: |
| 167 |
return p.handleTestCaseParam(tok) |
| 168 |
default: |
| 169 |
return p.handleTestSuiteParam(tok) |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
func (p *parser) handleEqual(tok string) (stateFunc, error) { |
| 174 |
switch { |
| 175 |
case tok == "=": |
| 176 |
return p.handleParamValue, nil |
| 177 |
default: |
| 178 |
return nil, fmt.Errorf("handleEqual: unexpected token %v", tok) |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
func (p *parser) handleParamName(tok string) (stateFunc, error) { |
| 183 |
switch { |
| 184 |
case tok == "\n" || tok == "[" || tok == "]" || tok == "=": |
| 185 |
return nil, fmt.Errorf("handleParamName: unexpected token %v", tok) |
| 186 |
default: |
| 187 |
p.currentName = string(tok) |
| 188 |
return p.handleEqual, nil |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
func (p *parser) handleStartTestCaseParam(tok string) (stateFunc, error) { |
| 193 |
switch { |
| 194 |
case tok == "\n": |
| 195 |
p.currentSuite.tests = append(p.currentSuite.tests, p.currentTest) |
| 196 |
p.currentTest = nil |
| 197 |
return p.start, nil |
| 198 |
case tok == "[" || tok == "]" || tok == "=": |
| 199 |
return nil, fmt.Errorf("handleStartTestCaseParam: unexpected token %v", tok) |
| 200 |
default: |
| 201 |
return p.handleParamName(tok) |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
func (p *parser) handleStartTestSuiteParam2(tok string) (stateFunc, error) { |
| 206 |
switch { |
| 207 |
case tok == "[" || tok == "]" || tok == "=" || tok == "\n": |
| 208 |
return nil, fmt.Errorf("handleStartTestSuiteParam2: unexpected token %v", tok) |
| 209 |
default: |
| 210 |
return p.handleParamName(tok) |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
func (p *parser) handleStartTestSuiteParam(tok string) (stateFunc, error) { |
| 215 |
switch { |
| 216 |
case tok == "\n": |
| 217 |
return p.start, nil |
| 218 |
case tok == "[": |
| 219 |
return p.handleStartTestSuiteParam2, nil |
| 220 |
case tok == "]" || tok == "=": |
| 221 |
return nil, fmt.Errorf("handleStartTestSuiteParam: unexpected token %v", tok) |
| 222 |
default: |
| 223 |
p.currentTest = make(testCase) |
| 224 |
return p.handleStartTestCaseParam(tok) |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
func (p *parser) handleStartTestSuiteName2(tok string) (stateFunc, error) { |
| 229 |
switch { |
| 230 |
case tok == "[" || tok == "]" || tok == "=" || tok == "\n": |
| 231 |
return nil, fmt.Errorf("handleStartTestSuiteName2: unexpected token %v", tok) |
| 232 |
default: |
| 233 |
p.currentName = tok |
| 234 |
return p.handleEndTestSuiteName, nil |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
func (p *parser) handleStartTestSuiteName(tok string) (stateFunc, error) { |
| 239 |
switch { |
| 240 |
case tok == "[": |
| 241 |
return p.handleStartTestSuiteName2, nil |
| 242 |
default: |
| 243 |
return nil, fmt.Errorf("handleStartTestSuiteName: unexpected token %v", tok) |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
func (p *parser) start(tok string) (stateFunc, error) { |
| 248 |
switch { |
| 249 |
case tok == "\n": |
| 250 |
return nil, nil |
| 251 |
case tok == "[": |
| 252 |
p.currentSuite = &testSuite{params: make(map[string]string)} |
| 253 |
p.suites = append(p.suites, p.currentSuite) |
| 254 |
return p.handleStartTestSuiteName(tok) |
| 255 |
case tok == "]" || tok == "=": |
| 256 |
return nil, fmt.Errorf("start: unexpected token %v", tok) |
| 257 |
default: |
| 258 |
if p.currentSuite == nil { |
| 259 |
return nil, fmt.Errorf("start: unexpected token %v (no current suite)", tok) |
| 260 |
} |
| 261 |
p.currentTest = make(testCase) |
| 262 |
return p.handleStartTestCaseParam(tok) |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
func (p *parser) run() error { |
| 267 |
for p.scanner.Scan() { |
| 268 |
next, err := p.current(p.scanner.Text()) |
| 269 |
if err != nil { |
| 270 |
return err |
| 271 |
} |
| 272 |
if next != nil { |
| 273 |
p.current = next |
| 274 |
} |
| 275 |
} |
| 276 |
return nil |
| 277 |
} |
| 278 |
|
| 279 |
func newParser(r io.Reader) *parser { |
| 280 |
scanner := bufio.NewScanner(r) |
| 281 |
scanner.Split(scanTokens) |
| 282 |
p := &parser{scanner: scanner} |
| 283 |
p.current = p.start |
| 284 |
return p |
| 285 |
} |
| 286 |
|
| 287 |
var errSkipSuite = errors.New("") |
| 288 |
|
| 289 |
func generateTests(vectors string, filter map[string]string, emitSuite func(*testSuite, int) error, emitTest func(*testSuite, int, int, testCase) error) error { |
| 290 |
f, err := os.Open(vectors) |
| 291 |
if err != nil { |
| 292 |
return err |
| 293 |
} |
| 294 |
defer f.Close() |
| 295 |
|
| 296 |
parser := newParser(f) |
| 297 |
if err := parser.run(); err != nil { |
| 298 |
return err |
| 299 |
} |
| 300 |
|
| 301 |
for i, suite := range parser.suites { |
| 302 |
skip := false |
| 303 |
for k, v := range filter { |
| 304 |
if suite.params[k] != v { |
| 305 |
skip = true |
| 306 |
break |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
if skip { |
| 311 |
continue |
| 312 |
} |
| 313 |
|
| 314 |
if err := emitSuite(suite, i); err != nil { |
| 315 |
if err == errSkipSuite { |
| 316 |
continue |
| 317 |
} |
| 318 |
return err |
| 319 |
} |
| 320 |
|
| 321 |
for j, test := range suite.tests { |
| 322 |
if err := emitTest(suite, i, j, test); err != nil { |
| 323 |
return err |
| 324 |
} |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
return nil |
| 329 |
} |
| 330 |
|
| 331 |
type atomicFile struct { |
| 332 |
*os.File |
| 333 |
path string |
| 334 |
} |
| 335 |
|
| 336 |
func (f *atomicFile) Commit() error { |
| 337 |
return os.Rename(f.Name(), f.path) |
| 338 |
} |
| 339 |
|
| 340 |
func (f *atomicFile) Close() error { |
| 341 |
os.Remove(f.Name()) |
| 342 |
return f.File.Close() |
| 343 |
} |
| 344 |
|
| 345 |
func newAtomicFile(path string) (*atomicFile, error) { |
| 346 |
f, err := ioutil.TempFile("", "gentest") |
| 347 |
if err != nil { |
| 348 |
return nil, fmt.Errorf("cannot create temporary file: %v", err) |
| 349 |
} |
| 350 |
return &atomicFile{f, path}, nil |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
func generateHashTests() error { |
| 355 |
tmpl, err := os.Open("testdata/hash_test.go.in") |
| 356 |
if err != nil { |
| 357 |
return err |
| 358 |
} |
| 359 |
defer tmpl.Close() |
| 360 |
|
| 361 |
f, err := newAtomicFile("hash_test.go") |
| 362 |
if err != nil { |
| 363 |
return err |
| 364 |
} |
| 365 |
defer f.Close() |
| 366 |
|
| 367 |
if _, err := io.Copy(f, tmpl); err != nil { |
| 368 |
return fmt.Errorf("cannot copy template: %v", err) |
| 369 |
} |
| 370 |
|
| 371 |
if err := generateTests("testdata/no_reseed/Hash_DRBG.rsp", map[string]string{"AdditionalInputLen":"0"}, |
| 372 |
func (suite *testSuite, i int) error { |
| 373 |
h, ok := hashes[suite.name] |
| 374 |
if !ok { |
| 375 |
return errSkipSuite |
| 376 |
} |
| 377 |
suite.params["HASH"] = h |
| 378 |
_, err := fmt.Fprintf(f, ` |
| 379 |
|
| 380 |
func (s *drbgSuite) testHash%[1]d_%[2]s(c *C, data *testData) { |
| 381 |
s.testHash(c, crypto.%[2]s, data) |
| 382 |
}`, |
| 383 |
i, h) |
| 384 |
return err |
| 385 |
}, |
| 386 |
func (suite *testSuite, i, j int, test testCase) error { |
| 387 |
_, err := fmt.Fprintf(f, ` |
| 388 |
|
| 389 |
func (s *drbgSuite) TestHash%[1]d_%[2]s_%[3]d(c *C) { |
| 390 |
s.testHash%[1]d_%[2]s(c, &testData{ |
| 391 |
entropyInput: decodeHexString(c, "%[4]s"), |
| 392 |
nonce: decodeHexString(c, "%[5]s"), |
| 393 |
personalization: decodeHexString(c, "%[6]s"), |
| 394 |
expected: decodeHexString(c, "%[7]s"), |
| 395 |
}) |
| 396 |
}`, |
| 397 |
i, suite.params["HASH"], j, test["EntropyInput"][0], test["Nonce"][0], test["PersonalizationString"][0], test["ReturnedBits"][0]) |
| 398 |
return err |
| 399 |
}, |
| 400 |
); err != nil { |
| 401 |
return err |
| 402 |
} |
| 403 |
|
| 404 |
if err := generateTests("testdata/no_reseed/Hash_DRBG.rsp", nil, |
| 405 |
func (suite *testSuite, i int) error { |
| 406 |
h, ok := hashes[suite.name] |
| 407 |
if !ok { |
| 408 |
return errSkipSuite |
| 409 |
} |
| 410 |
suite.params["HASH"] = h |
| 411 |
|
| 412 |
if suite.params["AdditionalInputLen"] == "0" { |
| 413 |
return errSkipSuite |
| 414 |
} |
| 415 |
|
| 416 |
_, err := fmt.Fprintf(f, ` |
| 417 |
|
| 418 |
func (s *drbgSuite) testHashGenerate%[1]d_%[2]s(c *C, data *testData) { |
| 419 |
s.testHashGenerate(c, crypto.%[2]s, data) |
| 420 |
}`, |
| 421 |
i, h) |
| 422 |
return err |
| 423 |
}, |
| 424 |
func (suite *testSuite, i, j int, test testCase) error { |
| 425 |
_, err := fmt.Fprintf(f, ` |
| 426 |
|
| 427 |
func (s *drbgSuite) TestHashGenerate%[1]d_%[2]s_%[3]d(c *C) { |
| 428 |
s.testHashGenerate%[1]d_%[2]s(c, &testData{ |
| 429 |
entropyInput: decodeHexString(c, "%[4]s"), |
| 430 |
nonce: decodeHexString(c, "%[5]s"), |
| 431 |
personalization: decodeHexString(c, "%[6]s"), |
| 432 |
additionalInput: [2][]byte{ |
| 433 |
decodeHexString(c, "%[7]s"), |
| 434 |
decodeHexString(c, "%[8]s"), |
| 435 |
}, |
| 436 |
expected: decodeHexString(c, "%[9]s"), |
| 437 |
}) |
| 438 |
}`, |
| 439 |
i, suite.params["HASH"], j, test["EntropyInput"][0], test["Nonce"][0], test["PersonalizationString"][0], test["AdditionalInput"][0], test["AdditionalInput"][1], test["ReturnedBits"][0]) |
| 440 |
return err |
| 441 |
}, |
| 442 |
); err != nil { |
| 443 |
return err |
| 444 |
} |
| 445 |
|
| 446 |
if err := generateTests("testdata/pr_false/Hash_DRBG.rsp", map[string]string{"AdditionalInputLen":"0"}, |
| 447 |
func (suite *testSuite, i int) error { |
| 448 |
h, ok := hashes[suite.name] |
| 449 |
if !ok { |
| 450 |
return errSkipSuite |
| 451 |
} |
| 452 |
suite.params["HASH"] = h |
| 453 |
_, err := fmt.Fprintf(f, ` |
| 454 |
|
| 455 |
func (s *drbgSuite) testHashAfterReseed%[1]d_%[2]s(c *C, data *testData) { |
| 456 |
s.testHashAfterReseed(c, crypto.%[2]s, data) |
| 457 |
}`, |
| 458 |
i, h) |
| 459 |
return err |
| 460 |
}, |
| 461 |
func (suite *testSuite, i, j int, test testCase) error { |
| 462 |
_, err := fmt.Fprintf(f, ` |
| 463 |
|
| 464 |
func (s *drbgSuite) TestHashAfterReseed%[1]d_%[2]s_%[3]d(c *C) { |
| 465 |
s.testHashAfterReseed%[1]d_%[2]s(c, &testData{ |
| 466 |
entropyInput: decodeHexString(c, "%[4]s"), |
| 467 |
nonce: decodeHexString(c, "%[5]s"), |
| 468 |
personalization: decodeHexString(c, "%[6]s"), |
| 469 |
entropyInputReseed: decodeHexString(c, "%[7]s"), |
| 470 |
expected: decodeHexString(c, "%[8]s"), |
| 471 |
}) |
| 472 |
}`, |
| 473 |
i, suite.params["HASH"], j, test["EntropyInput"][0], test["Nonce"][0], test["PersonalizationString"][0], test["EntropyInputReseed"][0], test["ReturnedBits"][0]) |
| 474 |
return err |
| 475 |
}, |
| 476 |
); err != nil { |
| 477 |
return err |
| 478 |
} |
| 479 |
|
| 480 |
if err := generateTests("testdata/pr_false/Hash_DRBG.rsp", nil, |
| 481 |
func (suite *testSuite, i int) error { |
| 482 |
h, ok := hashes[suite.name] |
| 483 |
if !ok { |
| 484 |
return errSkipSuite |
| 485 |
} |
| 486 |
suite.params["HASH"] = h |
| 487 |
|
| 488 |
if suite.params["AdditionalInputLen"] == "0" { |
| 489 |
return errSkipSuite |
| 490 |
} |
| 491 |
|
| 492 |
_, err := fmt.Fprintf(f, ` |
| 493 |
|
| 494 |
func (s *drbgSuite) testHashGenerateAfterReseed%[1]d_%[2]s(c *C, data *testData) { |
| 495 |
s.testHashGenerateAfterReseed(c, crypto.%[2]s, data) |
| 496 |
}`, |
| 497 |
i, h) |
| 498 |
return err |
| 499 |
}, |
| 500 |
func (suite *testSuite, i, j int, test testCase) error { |
| 501 |
_, err := fmt.Fprintf(f, ` |
| 502 |
|
| 503 |
func (s *drbgSuite) TestHashGenerateAfterReseed%[1]d_%[2]s_%[3]d(c *C) { |
| 504 |
s.testHashGenerateAfterReseed%[1]d_%[2]s(c, &testData{ |
| 505 |
entropyInput: decodeHexString(c, "%[4]s"), |
| 506 |
nonce: decodeHexString(c, "%[5]s"), |
| 507 |
personalization: decodeHexString(c, "%[6]s"), |
| 508 |
entropyInputReseed: decodeHexString(c, "%[7]s"), |
| 509 |
additionalInputReseed: decodeHexString(c, "%[8]s"), |
| 510 |
additionalInput: [2][]byte{ |
| 511 |
decodeHexString(c, "%[9]s"), |
| 512 |
decodeHexString(c, "%[10]s"), |
| 513 |
}, |
| 514 |
expected: decodeHexString(c, "%[11]s"), |
| 515 |
}) |
| 516 |
}`, |
| 517 |
i, suite.params["HASH"], j, test["EntropyInput"][0], test["Nonce"][0], test["PersonalizationString"][0], test["EntropyInputReseed"][0], test["AdditionalInputReseed"][0], test["AdditionalInput"][0], test["AdditionalInput"][1], test["ReturnedBits"][0]) |
| 518 |
return err |
| 519 |
}, |
| 520 |
); err != nil { |
| 521 |
return err |
| 522 |
} |
| 523 |
|
| 524 |
if err := f.Commit(); err != nil { |
| 525 |
return fmt.Errorf("cannot commit file: %v", err) |
| 526 |
} |
| 527 |
return nil |
| 528 |
} |
| 529 |
|
| 530 |
func generateHMACTests() error { |
| 531 |
tmpl, err := os.Open("testdata/hmac_test.go.in") |
| 532 |
if err != nil { |
| 533 |
return err |
| 534 |
} |
| 535 |
defer tmpl.Close() |
| 536 |
|
| 537 |
f, err := newAtomicFile("hmac_test.go") |
| 538 |
if err != nil { |
| 539 |
return err |
| 540 |
} |
| 541 |
defer f.Close() |
| 542 |
|
| 543 |
if _, err := io.Copy(f, tmpl); err != nil { |
| 544 |
return fmt.Errorf("cannot copy template: %v", err) |
| 545 |
} |
| 546 |
|
| 547 |
if err := generateTests("testdata/no_reseed/HMAC_DRBG.rsp", map[string]string{"AdditionalInputLen":"0"}, |
| 548 |
func (suite *testSuite, i int) error { |
| 549 |
h, ok := hashes[suite.name] |
| 550 |
if !ok { |
| 551 |
return errSkipSuite |
| 552 |
} |
| 553 |
suite.params["HASH"] = h |
| 554 |
_, err := fmt.Fprintf(f, ` |
| 555 |
|
| 556 |
func (s *drbgSuite) testHMAC%[1]d_%[2]s(c *C, data *testData) { |
| 557 |
s.testHMAC(c, crypto.%[2]s, data) |
| 558 |
}`, |
| 559 |
i, h) |
| 560 |
return err |
| 561 |
}, |
| 562 |
func (suite *testSuite, i, j int, test testCase) error { |
| 563 |
_, err := fmt.Fprintf(f, ` |
| 564 |
|
| 565 |
func (s *drbgSuite) TestHMAC%[1]d_%[2]s_%[3]d(c *C) { |
| 566 |
s.testHMAC%[1]d_%[2]s(c, &testData{ |
| 567 |
entropyInput: decodeHexString(c, "%[4]s"), |
| 568 |
nonce: decodeHexString(c, "%[5]s"), |
| 569 |
personalization: decodeHexString(c, "%[6]s"), |
| 570 |
expected: decodeHexString(c, "%[7]s"), |
| 571 |
}) |
| 572 |
}`, |
| 573 |
i, suite.params["HASH"], j, test["EntropyInput"][0], test["Nonce"][0], test["PersonalizationString"][0], test["ReturnedBits"][0]) |
| 574 |
return err |
| 575 |
}, |
| 576 |
); err != nil { |
| 577 |
return err |
| 578 |
} |
| 579 |
|
| 580 |
if err := generateTests("testdata/no_reseed/HMAC_DRBG.rsp", nil, |
| 581 |
func (suite *testSuite, i int) error { |
| 582 |
h, ok := hashes[suite.name] |
| 583 |
if !ok { |
| 584 |
return errSkipSuite |
| 585 |
} |
| 586 |
suite.params["HASH"] = h |
| 587 |
|
| 588 |
if suite.params["AdditionalInputLen"] == "0" { |
| 589 |
return errSkipSuite |
| 590 |
} |
| 591 |
|
| 592 |
_, err := fmt.Fprintf(f, ` |
| 593 |
|
| 594 |
func (s *drbgSuite) testHMACGenerate%[1]d_%[2]s(c *C, data *testData) { |
| 595 |
s.testHMACGenerate(c, crypto.%[2]s, data) |
| 596 |
}`, |
| 597 |
i, h) |
| 598 |
return err |
| 599 |
}, |
| 600 |
func (suite *testSuite, i, j int, test testCase) error { |
| 601 |
_, err := fmt.Fprintf(f, ` |
| 602 |
|
| 603 |
func (s *drbgSuite) TestHMACGenerate%[1]d_%[2]s_%[3]d(c *C) { |
| 604 |
s.testHMACGenerate%[1]d_%[2]s(c, &testData{ |
| 605 |
entropyInput: decodeHexString(c, "%[4]s"), |
| 606 |
nonce: decodeHexString(c, "%[5]s"), |
| 607 |
personalization: decodeHexString(c, "%[6]s"), |
| 608 |
additionalInput: [2][]byte{ |
| 609 |
decodeHexString(c, "%[7]s"), |
| 610 |
decodeHexString(c, "%[8]s"), |
| 611 |
}, |
| 612 |
expected: decodeHexString(c, "%[9]s"), |
| 613 |
}) |
| 614 |
}`, |
| 615 |
i, suite.params["HASH"], j, test["EntropyInput"][0], test["Nonce"][0], test["PersonalizationString"][0], test["AdditionalInput"][0], test["AdditionalInput"][1], test["ReturnedBits"][0]) |
| 616 |
return err |
| 617 |
}, |
| 618 |
); err != nil { |
| 619 |
return err |
| 620 |
} |
| 621 |
|
| 622 |
if err := generateTests("testdata/pr_false/HMAC_DRBG.rsp", map[string]string{"AdditionalInputLen":"0"}, |
| 623 |
func (suite *testSuite, i int) error { |
| 624 |
h, ok := hashes[suite.name] |
| 625 |
if !ok { |
| 626 |
return errSkipSuite |
| 627 |
} |
| 628 |
suite.params["HASH"] = h |
| 629 |
_, err := fmt.Fprintf(f, ` |
| 630 |
|
| 631 |
func (s *drbgSuite) testHMACAfterReseed%[1]d_%[2]s(c *C, data *testData) { |
| 632 |
s.testHMACAfterReseed(c, crypto.%[2]s, data) |
| 633 |
}`, |
| 634 |
i, h) |
| 635 |
return err |
| 636 |
}, |
| 637 |
func (suite *testSuite, i, j int, test testCase) error { |
| 638 |
_, err := fmt.Fprintf(f, ` |
| 639 |
|
| 640 |
func (s *drbgSuite) TestHMACAfterReseed%[1]d_%[2]s_%[3]d(c *C) { |
| 641 |
s.testHMACAfterReseed%[1]d_%[2]s(c, &testData{ |
| 642 |
entropyInput: decodeHexString(c, "%[4]s"), |
| 643 |
nonce: decodeHexString(c, "%[5]s"), |
| 644 |
personalization: decodeHexString(c, "%[6]s"), |
| 645 |
entropyInputReseed: decodeHexString(c, "%[7]s"), |
| 646 |
expected: decodeHexString(c, "%[8]s"), |
| 647 |
}) |
| 648 |
}`, |
| 649 |
i, suite.params["HASH"], j, test["EntropyInput"][0], test["Nonce"][0], test["PersonalizationString"][0], test["EntropyInputReseed"][0], test["ReturnedBits"][0]) |
| 650 |
return err |
| 651 |
}, |
| 652 |
); err != nil { |
| 653 |
return err |
| 654 |
} |
| 655 |
|
| 656 |
if err := generateTests("testdata/pr_false/HMAC_DRBG.rsp", nil, |
| 657 |
func (suite *testSuite, i int) error { |
| 658 |
h, ok := hashes[suite.name] |
| 659 |
if !ok { |
| 660 |
return errSkipSuite |
| 661 |
} |
| 662 |
suite.params["HASH"] = h |
| 663 |
|
| 664 |
if suite.params["AdditionalInputLen"] == "0" { |
| 665 |
return errSkipSuite |
| 666 |
} |
| 667 |
|
| 668 |
_, err := fmt.Fprintf(f, ` |
| 669 |
|
| 670 |
func (s *drbgSuite) testHMACGenerateAfterReseed%[1]d_%[2]s(c *C, data *testData) { |
| 671 |
s.testHMACGenerateAfterReseed(c, crypto.%[2]s, data) |
| 672 |
}`, |
| 673 |
i, h) |
| 674 |
return err |
| 675 |
}, |
| 676 |
func (suite *testSuite, i, j int, test testCase) error { |
| 677 |
_, err := fmt.Fprintf(f, ` |
| 678 |
|
| 679 |
func (s *drbgSuite) TestHMACGenerateAfterReseed%[1]d_%[2]s_%[3]d(c *C) { |
| 680 |
s.testHMACGenerateAfterReseed%[1]d_%[2]s(c, &testData{ |
| 681 |
entropyInput: decodeHexString(c, "%[4]s"), |
| 682 |
nonce: decodeHexString(c, "%[5]s"), |
| 683 |
personalization: decodeHexString(c, "%[6]s"), |
| 684 |
entropyInputReseed: decodeHexString(c, "%[7]s"), |
| 685 |
additionalInputReseed: decodeHexString(c, "%[8]s"), |
| 686 |
additionalInput: [2][]byte{ |
| 687 |
decodeHexString(c, "%[9]s"), |
| 688 |
decodeHexString(c, "%[10]s"), |
| 689 |
}, |
| 690 |
expected: decodeHexString(c, "%[11]s"), |
| 691 |
}) |
| 692 |
}`, |
| 693 |
i, suite.params["HASH"], j, test["EntropyInput"][0], test["Nonce"][0], test["PersonalizationString"][0], test["EntropyInputReseed"][0], test["AdditionalInputReseed"][0], test["AdditionalInput"][0], test["AdditionalInput"][1], test["ReturnedBits"][0]) |
| 694 |
return err |
| 695 |
}, |
| 696 |
); err != nil { |
| 697 |
return err |
| 698 |
} |
| 699 |
|
| 700 |
if err := f.Commit(); err != nil { |
| 701 |
return fmt.Errorf("cannot commit file: %v", err) |
| 702 |
} |
| 703 |
return nil |
| 704 |
} |
| 705 |
|
| 706 |
func generateCTRTests() error { |
| 707 |
tmpl, err := os.Open("testdata/ctr_test.go.in") |
| 708 |
if err != nil { |
| 709 |
return err |
| 710 |
} |
| 711 |
defer tmpl.Close() |
| 712 |
|
| 713 |
f, err := newAtomicFile("ctr_test.go") |
| 714 |
if err != nil { |
| 715 |
return err |
| 716 |
} |
| 717 |
defer f.Close() |
| 718 |
|
| 719 |
if _, err := io.Copy(f, tmpl); err != nil { |
| 720 |
return fmt.Errorf("cannot copy template: %v", err) |
| 721 |
} |
| 722 |
|
| 723 |
if err := generateTests("testdata/no_reseed/CTR_DRBG.rsp", map[string]string{"AdditionalInputLen":"0"}, |
| 724 |
func (suite *testSuite, i int) error { |
| 725 |
c, ok := ciphers[suite.name] |
| 726 |
if !ok { |
| 727 |
return errSkipSuite |
| 728 |
} |
| 729 |
suite.params["KEYLEN"] = c.keyLenBits |
| 730 |
_, err := fmt.Fprintf(f, ` |
| 731 |
|
| 732 |
func (s *drbgSuite) testCTR%[1]d_AES%[2]s(c *C, data *testData) { |
| 733 |
s.testCTR(c, %[3]s, data) |
| 734 |
}`, |
| 735 |
i, c.keyLenBits, c.keyLenBytes) |
| 736 |
return err |
| 737 |
}, |
| 738 |
func (suite *testSuite, i, j int, test testCase) error { |
| 739 |
_, err := fmt.Fprintf(f, ` |
| 740 |
|
| 741 |
func (s *drbgSuite) TestCTR%[1]d_AES%[2]s_%[3]d(c *C) { |
| 742 |
s.testCTR%[1]d_AES%[2]s(c, &testData{ |
| 743 |
entropyInput: decodeHexString(c, "%[4]s"), |
| 744 |
nonce: decodeHexString(c, "%[5]s"), |
| 745 |
personalization: decodeHexString(c, "%[6]s"), |
| 746 |
expected: decodeHexString(c, "%[7]s"), |
| 747 |
}) |
| 748 |
}`, |
| 749 |
i, suite.params["KEYLEN"], j, test["EntropyInput"][0], test["Nonce"][0], test["PersonalizationString"][0], test["ReturnedBits"][0]) |
| 750 |
return err |
| 751 |
}, |
| 752 |
); err != nil { |
| 753 |
return err |
| 754 |
} |
| 755 |
|
| 756 |
if err := generateTests("testdata/no_reseed/CTR_DRBG.rsp", nil, |
| 757 |
func (suite *testSuite, i int) error { |
| 758 |
c, ok := ciphers[suite.name] |
| 759 |
if !ok { |
| 760 |
return errSkipSuite |
| 761 |
} |
| 762 |
suite.params["KEYLEN"] = c.keyLenBits |
| 763 |
|
| 764 |
if suite.params["AdditionalInputLen"] == "0" { |
| 765 |
return errSkipSuite |
| 766 |
} |
| 767 |
|
| 768 |
_, err := fmt.Fprintf(f, ` |
| 769 |
|
| 770 |
func (s *drbgSuite) testCTRGenerate%[1]d_AES%[2]s(c *C, data *testData) { |
| 771 |
s.testCTRGenerate(c, %[3]s, data) |
| 772 |
}`, |
| 773 |
i, c.keyLenBits, c.keyLenBytes) |
| 774 |
return err |
| 775 |
}, |
| 776 |
func (suite *testSuite, i, j int, test testCase) error { |
| 777 |
_, err := fmt.Fprintf(f, ` |
| 778 |
|
| 779 |
func (s *drbgSuite) TestCTRGenerate%[1]d_AES%[2]s_%[3]d(c *C) { |
| 780 |
s.testCTRGenerate%[1]d_AES%[2]s(c, &testData{ |
| 781 |
entropyInput: decodeHexString(c, "%[4]s"), |
| 782 |
nonce: decodeHexString(c, "%[5]s"), |
| 783 |
personalization: decodeHexString(c, "%[6]s"), |
| 784 |
additionalInput: [2][]byte{ |
| 785 |
decodeHexString(c, "%[7]s"), |
| 786 |
decodeHexString(c, "%[8]s"), |
| 787 |
}, |
| 788 |
expected: decodeHexString(c, "%[9]s"), |
| 789 |
}) |
| 790 |
}`, |
| 791 |
i, suite.params["KEYLEN"], j, test["EntropyInput"][0], test["Nonce"][0], test["PersonalizationString"][0], test["AdditionalInput"][0], test["AdditionalInput"][1], test["ReturnedBits"][0]) |
| 792 |
return err |
| 793 |
}, |
| 794 |
); err != nil { |
| 795 |
return err |
| 796 |
} |
| 797 |
|
| 798 |
if err := generateTests("testdata/pr_false/CTR_DRBG.rsp", map[string]string{"AdditionalInputLen":"0"}, |
| 799 |
func (suite *testSuite, i int) error { |
| 800 |
c, ok := ciphers[suite.name] |
| 801 |
if !ok { |
| 802 |
return errSkipSuite |
| 803 |
} |
| 804 |
suite.params["KEYLEN"] = c.keyLenBits |
| 805 |
_, err := fmt.Fprintf(f, ` |
| 806 |
|
| 807 |
func (s *drbgSuite) testCTRAfterReseed%[1]d_AES%[2]s(c *C, data *testData) { |
| 808 |
s.testCTRAfterReseed(c, %[3]s, data) |
| 809 |
}`, |
| 810 |
i, c.keyLenBits, c.keyLenBytes) |
| 811 |
return err |
| 812 |
}, |
| 813 |
func (suite *testSuite, i, j int, test testCase) error { |
| 814 |
_, err := fmt.Fprintf(f, ` |
| 815 |
|
| 816 |
func (s *drbgSuite) TestCTRAfterReseed%[1]d_AES%[2]s_%[3]d(c *C) { |
| 817 |
s.testCTRAfterReseed%[1]d_AES%[2]s(c, &testData{ |
| 818 |
entropyInput: decodeHexString(c, "%[4]s"), |
| 819 |
nonce: decodeHexString(c, "%[5]s"), |
| 820 |
personalization: decodeHexString(c, "%[6]s"), |
| 821 |
entropyInputReseed: decodeHexString(c, "%[7]s"), |
| 822 |
expected: decodeHexString(c, "%[8]s"), |
| 823 |
}) |
| 824 |
}`, |
| 825 |
i, suite.params["KEYLEN"], j, test["EntropyInput"][0], test["Nonce"][0], test["PersonalizationString"][0], test["EntropyInputReseed"][0], test["ReturnedBits"][0]) |
| 826 |
return err |
| 827 |
}, |
| 828 |
); err != nil { |
| 829 |
return err |
| 830 |
} |
| 831 |
|
| 832 |
if err := generateTests("testdata/pr_false/CTR_DRBG.rsp", nil, |
| 833 |
func (suite *testSuite, i int) error { |
| 834 |
c, ok := ciphers[suite.name] |
| 835 |
if !ok { |
| 836 |
return errSkipSuite |
| 837 |
} |
| 838 |
suite.params["KEYLEN"] = c.keyLenBits |
| 839 |
|
| 840 |
if suite.params["AdditionalInputLen"] == "0" { |
| 841 |
return errSkipSuite |
| 842 |
} |
| 843 |
|
| 844 |
_, err := fmt.Fprintf(f, ` |
| 845 |
|
| 846 |
func (s *drbgSuite) testCTRGenerateAfterReseed%[1]d_AES%[2]s(c *C, data *testData) { |
| 847 |
s.testCTRGenerateAfterReseed(c, %[3]s, data) |
| 848 |
}`, |
| 849 |
i, c.keyLenBits, c.keyLenBytes) |
| 850 |
return err |
| 851 |
}, |
| 852 |
func (suite *testSuite, i, j int, test testCase) error { |
| 853 |
_, err := fmt.Fprintf(f, ` |
| 854 |
|
| 855 |
func (s *drbgSuite) TestCTRGenerateAfterReseed%[1]d_AES%[2]s_%[3]d(c *C) { |
| 856 |
s.testCTRGenerateAfterReseed%[1]d_AES%[2]s(c, &testData{ |
| 857 |
entropyInput: decodeHexString(c, "%[4]s"), |
| 858 |
nonce: decodeHexString(c, "%[5]s"), |
| 859 |
personalization: decodeHexString(c, "%[6]s"), |
| 860 |
entropyInputReseed: decodeHexString(c, "%[7]s"), |
| 861 |
additionalInputReseed: decodeHexString(c, "%[8]s"), |
| 862 |
additionalInput: [2][]byte{ |
| 863 |
decodeHexString(c, "%[9]s"), |
| 864 |
decodeHexString(c, "%[10]s"), |
| 865 |
}, |
| 866 |
expected: decodeHexString(c, "%[11]s"), |
| 867 |
}) |
| 868 |
}`, |
| 869 |
i, suite.params["KEYLEN"], j, test["EntropyInput"][0], test["Nonce"][0], test["PersonalizationString"][0], test["EntropyInputReseed"][0], test["AdditionalInputReseed"][0], test["AdditionalInput"][0], test["AdditionalInput"][1], test["ReturnedBits"][0]) |
| 870 |
return err |
| 871 |
}, |
| 872 |
); err != nil { |
| 873 |
return err |
| 874 |
} |
| 875 |
|
| 876 |
if err := f.Commit(); err != nil { |
| 877 |
return fmt.Errorf("cannot commit file: %v", err) |
| 878 |
} |
| 879 |
return nil |
| 880 |
} |
| 881 |
|
| 882 |
func run() error { |
| 883 |
if err := generateHashTests(); err != nil { |
| 884 |
return fmt.Errorf("cannot generate hash tests: %v", err) |
| 885 |
} |
| 886 |
if err := generateHMACTests(); err != nil { |
| 887 |
return fmt.Errorf("cannot generate HMAC tests: %v", err) |
| 888 |
} |
| 889 |
if err := generateCTRTests(); err != nil { |
| 890 |
return fmt.Errorf("cannot generate CTR tests: %v", err) |
| 891 |
} |
| 892 |
|
| 893 |
return nil |
| 894 |
} |
| 895 |
|
| 896 |
func main() { |
| 897 |
if err := run(); err != nil { |
| 898 |
fmt.Fprintf(os.Stderr, "%v\n", err) |
| 899 |
os.Exit(1) |
| 900 |
} |
| 901 |
} |