|
| 1 | +// Copyright 2019 The Go Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package wycheproof |
| 6 | + |
| 7 | +import ( |
| 8 | + "crypto/ecdsa" |
| 9 | + "testing" |
| 10 | + |
| 11 | + wecdsa "golang.org/x/crypto/internal/wycheproof/internal/ecdsa" |
| 12 | +) |
| 13 | + |
| 14 | +func TestEcdsa(t *testing.T) { |
| 15 | + // AsnSignatureTestVector |
| 16 | + type AsnSignatureTestVector struct { |
| 17 | + |
| 18 | + // A brief description of the test case |
| 19 | + Comment string `json:"comment,omitempty"` |
| 20 | + |
| 21 | + // A list of flags |
| 22 | + Flags []string `json:"flags,omitempty"` |
| 23 | + |
| 24 | + // The message to sign |
| 25 | + Msg string `json:"msg,omitempty"` |
| 26 | + |
| 27 | + // Test result |
| 28 | + Result string `json:"result,omitempty"` |
| 29 | + |
| 30 | + // An ASN encoded signature for msg |
| 31 | + Sig string `json:"sig,omitempty"` |
| 32 | + |
| 33 | + // Identifier of the test case |
| 34 | + TcId int `json:"tcId,omitempty"` |
| 35 | + } |
| 36 | + |
| 37 | + // EcPublicKey |
| 38 | + type EcPublicKey struct { |
| 39 | + |
| 40 | + // the EC group used by this public key |
| 41 | + Curve interface{} `json:"curve,omitempty"` |
| 42 | + |
| 43 | + // the key size in bits |
| 44 | + KeySize int `json:"keySize,omitempty"` |
| 45 | + |
| 46 | + // the key type |
| 47 | + Type string `json:"type,omitempty"` |
| 48 | + |
| 49 | + // encoded public key point |
| 50 | + Uncompressed string `json:"uncompressed,omitempty"` |
| 51 | + |
| 52 | + // the x-coordinate of the public key point |
| 53 | + Wx string `json:"wx,omitempty"` |
| 54 | + |
| 55 | + // the y-coordinate of the public key point |
| 56 | + Wy string `json:"wy,omitempty"` |
| 57 | + } |
| 58 | + |
| 59 | + // EcUnnamedGroup |
| 60 | + type EcUnnamedGroup struct { |
| 61 | + |
| 62 | + // coefficient a of the elliptic curve equation |
| 63 | + A string `json:"a,omitempty"` |
| 64 | + |
| 65 | + // coefficient b of the elliptic curve equation |
| 66 | + B string `json:"b,omitempty"` |
| 67 | + |
| 68 | + // the x-coordinate of the generator |
| 69 | + Gx string `json:"gx,omitempty"` |
| 70 | + |
| 71 | + // the y-coordinate of the generator |
| 72 | + Gy string `json:"gy,omitempty"` |
| 73 | + |
| 74 | + // the cofactor |
| 75 | + H int `json:"h,omitempty"` |
| 76 | + |
| 77 | + // the order of the generator |
| 78 | + N string `json:"n,omitempty"` |
| 79 | + |
| 80 | + // the order of the underlying field |
| 81 | + P string `json:"p,omitempty"` |
| 82 | + |
| 83 | + // an unnamed EC group over a prime field in Weierstrass form |
| 84 | + Type string `json:"type,omitempty"` |
| 85 | + } |
| 86 | + |
| 87 | + // EcdsaTestGroup |
| 88 | + type EcdsaTestGroup struct { |
| 89 | + |
| 90 | + // unenocded EC public key |
| 91 | + Key *EcPublicKey `json:"key,omitempty"` |
| 92 | + |
| 93 | + // DER encoded public key |
| 94 | + KeyDer string `json:"keyDer,omitempty"` |
| 95 | + |
| 96 | + // Pem encoded public key |
| 97 | + KeyPem string `json:"keyPem,omitempty"` |
| 98 | + |
| 99 | + // the hash function used for ECDSA |
| 100 | + Sha string `json:"sha,omitempty"` |
| 101 | + Tests []*AsnSignatureTestVector `json:"tests,omitempty"` |
| 102 | + Type interface{} `json:"type,omitempty"` |
| 103 | + } |
| 104 | + |
| 105 | + // Notes a description of the labels used in the test vectors |
| 106 | + type Notes struct { |
| 107 | + } |
| 108 | + |
| 109 | + // Root |
| 110 | + type Root struct { |
| 111 | + |
| 112 | + // the primitive tested in the test file |
| 113 | + Algorithm string `json:"algorithm,omitempty"` |
| 114 | + |
| 115 | + // the version of the test vectors. |
| 116 | + GeneratorVersion string `json:"generatorVersion,omitempty"` |
| 117 | + |
| 118 | + // additional documentation |
| 119 | + Header []string `json:"header,omitempty"` |
| 120 | + |
| 121 | + // a description of the labels used in the test vectors |
| 122 | + Notes *Notes `json:"notes,omitempty"` |
| 123 | + |
| 124 | + // the number of test vectors in this test |
| 125 | + NumberOfTests int `json:"numberOfTests,omitempty"` |
| 126 | + Schema interface{} `json:"schema,omitempty"` |
| 127 | + TestGroups []*EcdsaTestGroup `json:"testGroups,omitempty"` |
| 128 | + } |
| 129 | + |
| 130 | + flagsShouldPass := map[string]bool{ |
| 131 | + // An encoded ASN.1 integer missing a leading zero is invalid, but accepted by some implementations. |
| 132 | + "MissingZero": false, |
| 133 | + // A signature using a weaker hash than the EC params is not a security risk, as long as the hash is secure. |
| 134 | + // https://www.imperialviolet.org/2014/05/25/strengthmatching.html |
| 135 | + "WeakHash": true, |
| 136 | + } |
| 137 | + |
| 138 | + // supportedCurves is a map of all elliptic curves supported |
| 139 | + // by crypto/elliptic, which can subsequently be parsed and tested. |
| 140 | + supportedCurves := map[string]bool{ |
| 141 | + "secp224r1": true, |
| 142 | + "secp256r1": true, |
| 143 | + "secp384r1": true, |
| 144 | + "secp521r1": true, |
| 145 | + } |
| 146 | + |
| 147 | + var root Root |
| 148 | + readTestVector(t, "ecdsa_test.json", &root) |
| 149 | + for _, tg := range root.TestGroups { |
| 150 | + curve := tg.Key.Curve.(string) |
| 151 | + if !supportedCurves[curve] { |
| 152 | + continue |
| 153 | + } |
| 154 | + pub := decodePublicKey(tg.KeyDer).(*ecdsa.PublicKey) |
| 155 | + h := parseHash(tg.Sha).New() |
| 156 | + for _, sig := range tg.Tests { |
| 157 | + h.Reset() |
| 158 | + h.Write(decodeHex(sig.Msg)) |
| 159 | + hashed := h.Sum(nil) |
| 160 | + got := wecdsa.VerifyASN1(pub, hashed, decodeHex(sig.Sig)) |
| 161 | + if want := shouldPass(sig.Result, sig.Flags, flagsShouldPass); got != want { |
| 162 | + t.Errorf("tcid: %d, type: %s, comment: %q, wanted success: %t", sig.TcId, sig.Result, sig.Comment, want) |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | +} |
0 commit comments