diff options
author | tb <> | 2018-10-06 05:02:21 +0000 |
---|---|---|
committer | tb <> | 2018-10-06 05:02:21 +0000 |
commit | bab8159a22ab8f619723d547fa03cb5ad0109e34 (patch) | |
tree | 630e198727f65cc170b409b1dcd5dfae2986dcec | |
parent | fbadd14d233ceaefd7974b4497ffbde562abc15f (diff) | |
download | openbsd-bab8159a22ab8f619723d547fa03cb5ad0109e34.tar.gz openbsd-bab8159a22ab8f619723d547fa03cb5ad0109e34.tar.bz2 openbsd-bab8159a22ab8f619723d547fa03cb5ad0109e34.zip |
factor ECDSA signature extraction into its own function
-rw-r--r-- | src/regress/lib/libcrypto/wycheproof/wycheproof.go | 59 |
1 files changed, 35 insertions, 24 deletions
diff --git a/src/regress/lib/libcrypto/wycheproof/wycheproof.go b/src/regress/lib/libcrypto/wycheproof/wycheproof.go index 1a5aac87f1..c7ea768939 100644 --- a/src/regress/lib/libcrypto/wycheproof/wycheproof.go +++ b/src/regress/lib/libcrypto/wycheproof/wycheproof.go | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: wycheproof.go,v 1.71 2018/10/06 04:35:54 tb Exp $ */ | 1 | /* $OpenBSD: wycheproof.go,v 1.72 2018/10/06 05:02:21 tb Exp $ */ |
2 | /* | 2 | /* |
3 | * Copyright (c) 2018 Joel Sing <jsing@openbsd.org> | 3 | * Copyright (c) 2018 Joel Sing <jsing@openbsd.org> |
4 | * Copyright (c) 2018 Theo Buehler <tb@openbsd.org> | 4 | * Copyright (c) 2018 Theo Buehler <tb@openbsd.org> |
@@ -1357,31 +1357,17 @@ func runECDSATestGroup(algorithm string, wtg *wycheproofTestGroupECDSA) bool { | |||
1357 | return success | 1357 | return success |
1358 | } | 1358 | } |
1359 | 1359 | ||
1360 | func runECDSAWebCryptoTest(ecKey *C.EC_KEY, nid int, h hash.Hash, wt *wycheproofTestECDSA) bool { | 1360 | // DER encode the signature (so that ECDSA_verify() can decode and encode it again...) |
1361 | msg, err := hex.DecodeString(wt.Msg) | 1361 | func encodeECDSAWebCryptoSig(wtSig string) (*C.uchar, C.int) { |
1362 | if err != nil { | ||
1363 | log.Fatalf("Failed to decode message %q: %v", wt.Msg, err) | ||
1364 | } | ||
1365 | |||
1366 | h.Reset() | ||
1367 | h.Write(msg) | ||
1368 | msg = h.Sum(nil) | ||
1369 | |||
1370 | msgLen := len(msg) | ||
1371 | if msgLen == 0 { | ||
1372 | msg = append(msg, 0) | ||
1373 | } | ||
1374 | |||
1375 | // DER encode the signature (so that ECDSA_verify() can decode and encode it again...) | ||
1376 | cSig := C.ECDSA_SIG_new() | 1362 | cSig := C.ECDSA_SIG_new() |
1377 | if cSig == nil { | 1363 | if cSig == nil { |
1378 | log.Fatal("ECDSA_SIG_new() failed") | 1364 | log.Fatal("ECDSA_SIG_new() failed") |
1379 | } | 1365 | } |
1380 | defer C.ECDSA_SIG_free(cSig) | 1366 | defer C.ECDSA_SIG_free(cSig) |
1381 | 1367 | ||
1382 | sigLen := len(wt.Sig) | 1368 | sigLen := len(wtSig) |
1383 | r := C.CString(wt.Sig[:sigLen/2]) | 1369 | r := C.CString(wtSig[:sigLen/2]) |
1384 | s := C.CString(wt.Sig[sigLen/2:]) | 1370 | s := C.CString(wtSig[sigLen/2:]) |
1385 | if C.BN_hex2bn(&cSig.r, r) == 0 { | 1371 | if C.BN_hex2bn(&cSig.r, r) == 0 { |
1386 | log.Fatal("Failed to set ECDSA r") | 1372 | log.Fatal("Failed to set ECDSA r") |
1387 | } | 1373 | } |
@@ -1393,21 +1379,46 @@ func runECDSAWebCryptoTest(ecKey *C.EC_KEY, nid int, h hash.Hash, wt *wycheproof | |||
1393 | 1379 | ||
1394 | derLen := C.i2d_ECDSA_SIG(cSig, nil) | 1380 | derLen := C.i2d_ECDSA_SIG(cSig, nil) |
1395 | if derLen == 0 { | 1381 | if derLen == 0 { |
1396 | log.Fatal("i2d_ECDSA_SIG(cSig, nil) failed") | 1382 | return nil, 0 |
1397 | } | 1383 | } |
1398 | cDer := (*C.uchar)(C.malloc(C.ulong(derLen))) | 1384 | cDer := (*C.uchar)(C.malloc(C.ulong(derLen))) |
1399 | if cDer == nil { | 1385 | if cDer == nil { |
1400 | log.Fatal("malloc failed") | 1386 | log.Fatal("malloc failed") |
1401 | } | 1387 | } |
1402 | defer C.free(unsafe.Pointer(cDer)) | ||
1403 | 1388 | ||
1404 | p := cDer | 1389 | p := cDer |
1405 | ret := C.i2d_ECDSA_SIG(cSig, (**C.uchar)(&p)) | 1390 | ret := C.i2d_ECDSA_SIG(cSig, (**C.uchar)(&p)) |
1406 | if ret == 0 || ret != derLen { | 1391 | if ret == 0 || ret != derLen { |
1407 | log.Fatalf("i2d_ECDSA_SIG(cSig, nil) failed, got %d, want %d", ret, derLen) | 1392 | C.free(unsafe.Pointer(cDer)) |
1393 | return nil, 0 | ||
1408 | } | 1394 | } |
1409 | 1395 | ||
1410 | ret = C.ECDSA_verify(0, (*C.uchar)(unsafe.Pointer(&msg[0])), C.int(msgLen), | 1396 | return cDer, derLen |
1397 | } | ||
1398 | |||
1399 | func runECDSAWebCryptoTest(ecKey *C.EC_KEY, nid int, h hash.Hash, wt *wycheproofTestECDSA) bool { | ||
1400 | msg, err := hex.DecodeString(wt.Msg) | ||
1401 | if err != nil { | ||
1402 | log.Fatalf("Failed to decode message %q: %v", wt.Msg, err) | ||
1403 | } | ||
1404 | |||
1405 | h.Reset() | ||
1406 | h.Write(msg) | ||
1407 | msg = h.Sum(nil) | ||
1408 | |||
1409 | msgLen := len(msg) | ||
1410 | if msgLen == 0 { | ||
1411 | msg = append(msg, 0) | ||
1412 | } | ||
1413 | |||
1414 | cDer, derLen := encodeECDSAWebCryptoSig(wt.Sig) | ||
1415 | if cDer == nil { | ||
1416 | fmt.Print("FAIL: unable to decode signature") | ||
1417 | return false | ||
1418 | } | ||
1419 | defer C.free(unsafe.Pointer(cDer)) | ||
1420 | |||
1421 | ret := C.ECDSA_verify(0, (*C.uchar)(unsafe.Pointer(&msg[0])), C.int(msgLen), | ||
1411 | (*C.uchar)(unsafe.Pointer(cDer)), C.int(derLen), ecKey) | 1422 | (*C.uchar)(unsafe.Pointer(cDer)), C.int(derLen), ecKey) |
1412 | 1423 | ||
1413 | // XXX audit acceptable cases... | 1424 | // XXX audit acceptable cases... |