...

Source file src/golang.org/x/crypto/internal/wycheproof/dsa_test.go

Documentation: golang.org/x/crypto/internal/wycheproof

     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/dsa"
     9  	"testing"
    10  
    11  	wdsa "golang.org/x/crypto/internal/wycheproof/internal/dsa"
    12  )
    13  
    14  func TestDsa(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  	// DsaPublicKey
    38  	type DsaPublicKey struct {
    39  
    40  		// the generator of the multiplicative subgroup
    41  		G string `json:"g,omitempty"`
    42  
    43  		// the key size in bits
    44  		KeySize int `json:"keySize,omitempty"`
    45  
    46  		// the modulus p
    47  		P string `json:"p,omitempty"`
    48  
    49  		// the order of the generator g
    50  		Q string `json:"q,omitempty"`
    51  
    52  		// the key type
    53  		Type string `json:"type,omitempty"`
    54  
    55  		// the public key value
    56  		Y string `json:"y,omitempty"`
    57  	}
    58  
    59  	// DsaTestGroup
    60  	type DsaTestGroup struct {
    61  
    62  		// unenocded DSA public key
    63  		Key *DsaPublicKey `json:"key,omitempty"`
    64  
    65  		// DER encoded public key
    66  		KeyDer string `json:"keyDer,omitempty"`
    67  
    68  		// Pem encoded public key
    69  		KeyPem string `json:"keyPem,omitempty"`
    70  
    71  		// the hash function used for DSA
    72  		Sha   string                    `json:"sha,omitempty"`
    73  		Tests []*AsnSignatureTestVector `json:"tests,omitempty"`
    74  		Type  interface{}               `json:"type,omitempty"`
    75  	}
    76  
    77  	// Notes a description of the labels used in the test vectors
    78  	type Notes struct {
    79  	}
    80  
    81  	// Root
    82  	type Root struct {
    83  
    84  		// the primitive tested in the test file
    85  		Algorithm string `json:"algorithm,omitempty"`
    86  
    87  		// the version of the test vectors.
    88  		GeneratorVersion string `json:"generatorVersion,omitempty"`
    89  
    90  		// additional documentation
    91  		Header []string `json:"header,omitempty"`
    92  
    93  		// a description of the labels used in the test vectors
    94  		Notes *Notes `json:"notes,omitempty"`
    95  
    96  		// the number of test vectors in this test
    97  		NumberOfTests int             `json:"numberOfTests,omitempty"`
    98  		Schema        interface{}     `json:"schema,omitempty"`
    99  		TestGroups    []*DsaTestGroup `json:"testGroups,omitempty"`
   100  	}
   101  
   102  	flagsShouldPass := map[string]bool{
   103  		// An encoded ASN.1 integer missing a leading zero is invalid, but accepted by some implementations.
   104  		"NoLeadingZero": false,
   105  	}
   106  
   107  	var root Root
   108  	readTestVector(t, "dsa_test.json", &root)
   109  	for _, tg := range root.TestGroups {
   110  		pub := decodePublicKey(tg.KeyDer).(*dsa.PublicKey)
   111  		h := parseHash(tg.Sha).New()
   112  		for _, sig := range tg.Tests {
   113  			h.Reset()
   114  			h.Write(decodeHex(sig.Msg))
   115  			hashed := h.Sum(nil)
   116  			hashed = hashed[:pub.Q.BitLen()/8] // Truncate to the byte-length of the subgroup (Q)
   117  			got := wdsa.VerifyASN1(pub, hashed, decodeHex(sig.Sig))
   118  			if want := shouldPass(sig.Result, sig.Flags, flagsShouldPass); got != want {
   119  				t.Errorf("tcid: %d, type: %s, comment: %q, wanted success: %t", sig.TcId, sig.Result, sig.Comment, want)
   120  			}
   121  		}
   122  	}
   123  }
   124  

View as plain text