diff options
Diffstat (limited to '')
| -rw-r--r-- | src/regress/lib/libcrypto/wycheproof/wycheproof.go | 95 |
1 files changed, 82 insertions, 13 deletions
diff --git a/src/regress/lib/libcrypto/wycheproof/wycheproof.go b/src/regress/lib/libcrypto/wycheproof/wycheproof.go index e7d8140cd6..427dc47aec 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.3 2018/08/10 16:14:40 jsing Exp $ */ | 1 | /* $OpenBSD: wycheproof.go,v 1.4 2018/08/10 16:18:55 jsing Exp $ */ |
| 2 | /* | 2 | /* |
| 3 | * Copyright (c) 2018 Joel Sing <jsing@openbsd.org> | 3 | * Copyright (c) 2018 Joel Sing <jsing@openbsd.org> |
| 4 | * | 4 | * |
| @@ -22,12 +22,14 @@ package main | |||
| 22 | #cgo LDFLAGS: -lcrypto | 22 | #cgo LDFLAGS: -lcrypto |
| 23 | 23 | ||
| 24 | #include <openssl/bn.h> | 24 | #include <openssl/bn.h> |
| 25 | #include <openssl/curve25519.h> | ||
| 25 | #include <openssl/objects.h> | 26 | #include <openssl/objects.h> |
| 26 | #include <openssl/rsa.h> | 27 | #include <openssl/rsa.h> |
| 27 | */ | 28 | */ |
| 28 | import "C" | 29 | import "C" |
| 29 | 30 | ||
| 30 | import ( | 31 | import ( |
| 32 | "bytes" | ||
| 31 | "crypto/sha1" | 33 | "crypto/sha1" |
| 32 | "crypto/sha256" | 34 | "crypto/sha256" |
| 33 | "crypto/sha512" | 35 | "crypto/sha512" |
| @@ -55,17 +57,33 @@ type wycheproofTestRSA struct { | |||
| 55 | } | 57 | } |
| 56 | 58 | ||
| 57 | type wycheproofTestGroupRSA struct { | 59 | type wycheproofTestGroupRSA struct { |
| 58 | E string `json:"e"` | 60 | E string `json:"e"` |
| 59 | KeyASN string `json:"keyAsn"` | 61 | KeyASN string `json:"keyAsn"` |
| 60 | KeyDER string `json:"keyDer"` | 62 | KeyDER string `json:"keyDer"` |
| 61 | KeyPEM string `json:"keyPem"` | 63 | KeyPEM string `json:"keyPem"` |
| 62 | KeySize int `json:"keysize"` | 64 | KeySize int `json:"keysize"` |
| 63 | N string `json:"n"` | 65 | N string `json:"n"` |
| 64 | SHA string `json:"sha"` | 66 | SHA string `json:"sha"` |
| 65 | Type string `json:"type"` | 67 | Type string `json:"type"` |
| 66 | Tests []*wycheproofTestRSA `json:"tests"` | 68 | Tests []*wycheproofTestRSA `json:"tests"` |
| 67 | } | 69 | } |
| 68 | 70 | ||
| 71 | type wycheproofTestX25519 struct { | ||
| 72 | TCID int `json:"tcId"` | ||
| 73 | Comment string `json:"comment"` | ||
| 74 | Curve string `json:"curve"` | ||
| 75 | Public string `json:"public"` | ||
| 76 | Private string `json:"private"` | ||
| 77 | Shared string `json:"shared"` | ||
| 78 | Result string `json:"result"` | ||
| 79 | Flags []string `json:"flags"` | ||
| 80 | } | ||
| 81 | |||
| 82 | type wycheproofTestGroupX25519 struct { | ||
| 83 | Curve string `json:"curve"` | ||
| 84 | Tests []*wycheproofTestX25519 `json:"tests"` | ||
| 85 | } | ||
| 86 | |||
| 69 | type wycheproofTestVectors struct { | 87 | type wycheproofTestVectors struct { |
| 70 | Algorithm string `json:"algorithm"` | 88 | Algorithm string `json:"algorithm"` |
| 71 | GeneratorVersion string `json:"generatorVersion"` | 89 | GeneratorVersion string `json:"generatorVersion"` |
| @@ -76,7 +94,7 @@ type wycheproofTestVectors struct { | |||
| 76 | } | 94 | } |
| 77 | 95 | ||
| 78 | var nids = map[string]int{ | 96 | var nids = map[string]int{ |
| 79 | "SHA-1": C.NID_sha1, | 97 | "SHA-1": C.NID_sha1, |
| 80 | "SHA-224": C.NID_sha224, | 98 | "SHA-224": C.NID_sha224, |
| 81 | "SHA-256": C.NID_sha256, | 99 | "SHA-256": C.NID_sha256, |
| 82 | "SHA-384": C.NID_sha384, | 100 | "SHA-384": C.NID_sha384, |
| @@ -182,6 +200,50 @@ func runRSATestGroup(wtg *wycheproofTestGroupRSA) bool { | |||
| 182 | return success | 200 | return success |
| 183 | } | 201 | } |
| 184 | 202 | ||
| 203 | func runX25519Test(wt *wycheproofTestX25519) bool { | ||
| 204 | public, err := hex.DecodeString(wt.Public) | ||
| 205 | if err != nil { | ||
| 206 | log.Fatalf("Failed to decode public %q: %v", wt.Public, err) | ||
| 207 | } | ||
| 208 | private, err := hex.DecodeString(wt.Private) | ||
| 209 | if err != nil { | ||
| 210 | log.Fatalf("Failed to decode private %q: %v", wt.Private, err) | ||
| 211 | } | ||
| 212 | shared, err := hex.DecodeString(wt.Shared) | ||
| 213 | if err != nil { | ||
| 214 | log.Fatalf("Failed to decode shared %q: %v", wt.Shared, err) | ||
| 215 | } | ||
| 216 | |||
| 217 | got := make([]byte, C.X25519_KEY_LENGTH) | ||
| 218 | result := true | ||
| 219 | |||
| 220 | if C.X25519((*C.uint8_t)(unsafe.Pointer(&got[0])), (*C.uint8_t)(unsafe.Pointer(&private[0])), (*C.uint8_t)(unsafe.Pointer(&public[0]))) != 1 { | ||
| 221 | result = false | ||
| 222 | } else { | ||
| 223 | result = bytes.Equal(got, shared) | ||
| 224 | } | ||
| 225 | |||
| 226 | // XXX audit acceptable cases... | ||
| 227 | success := true | ||
| 228 | if result != (wt.Result == "valid") && wt.Result != "acceptable" { | ||
| 229 | fmt.Printf("FAIL: Test case %d (%q) - X25519(), want %v\n", wt.TCID, wt.Comment, wt.Result) | ||
| 230 | success = false | ||
| 231 | } | ||
| 232 | return success | ||
| 233 | } | ||
| 234 | |||
| 235 | func runX25519TestGroup(wtg *wycheproofTestGroupX25519) bool { | ||
| 236 | fmt.Printf("Running X25519 test group with curve %v...\n", wtg.Curve) | ||
| 237 | |||
| 238 | success := true | ||
| 239 | for _, wt := range wtg.Tests { | ||
| 240 | if !runX25519Test(wt) { | ||
| 241 | success = false | ||
| 242 | } | ||
| 243 | } | ||
| 244 | return success | ||
| 245 | } | ||
| 246 | |||
| 185 | func runTestVectors(path string) bool { | 247 | func runTestVectors(path string) bool { |
| 186 | b, err := ioutil.ReadFile(path) | 248 | b, err := ioutil.ReadFile(path) |
| 187 | if err != nil { | 249 | if err != nil { |
| @@ -197,6 +259,8 @@ func runTestVectors(path string) bool { | |||
| 197 | switch wtv.Algorithm { | 259 | switch wtv.Algorithm { |
| 198 | case "RSASig": | 260 | case "RSASig": |
| 199 | wtg = &wycheproofTestGroupRSA{} | 261 | wtg = &wycheproofTestGroupRSA{} |
| 262 | case "X25519": | ||
| 263 | wtg = &wycheproofTestGroupX25519{} | ||
| 200 | default: | 264 | default: |
| 201 | log.Fatalf("Unknown test vector algorithm %q", wtv.Algorithm) | 265 | log.Fatalf("Unknown test vector algorithm %q", wtv.Algorithm) |
| 202 | } | 266 | } |
| @@ -211,6 +275,10 @@ func runTestVectors(path string) bool { | |||
| 211 | if !runRSATestGroup(wtg.(*wycheproofTestGroupRSA)) { | 275 | if !runRSATestGroup(wtg.(*wycheproofTestGroupRSA)) { |
| 212 | success = false | 276 | success = false |
| 213 | } | 277 | } |
| 278 | case "X25519": | ||
| 279 | if !runX25519TestGroup(wtg.(*wycheproofTestGroupX25519)) { | ||
| 280 | success = false | ||
| 281 | } | ||
| 214 | default: | 282 | default: |
| 215 | log.Fatalf("Unknown test vector algorithm %q", wtv.Algorithm) | 283 | log.Fatalf("Unknown test vector algorithm %q", wtv.Algorithm) |
| 216 | } | 284 | } |
| @@ -225,12 +293,13 @@ func main() { | |||
| 225 | os.Exit(0) | 293 | os.Exit(0) |
| 226 | } | 294 | } |
| 227 | 295 | ||
| 228 | // TODO: AES, Chacha20Poly1305, DSA, ECDH, ECDSA, X25519 | 296 | // TODO: AES, Chacha20Poly1305, DSA, ECDH, ECDSA, RSA-PSS. |
| 229 | tests := []struct{ | 297 | tests := []struct { |
| 230 | name string | 298 | name string |
| 231 | pattern string | 299 | pattern string |
| 232 | }{ | 300 | }{ |
| 233 | {"RSA signature", "rsa_signature_*test.json"}, | 301 | {"RSA signature", "rsa_signature_*test.json"}, |
| 302 | {"X25519", "x25519_*test.json"}, | ||
| 234 | } | 303 | } |
| 235 | 304 | ||
| 236 | success := true | 305 | success := true |
