...

Source file src/golang.org/x/crypto/internal/wycheproof/rsa_signature_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/rsa"
     9  	"testing"
    10  )
    11  
    12  func TestRsa(t *testing.T) {
    13  	// KeyJwk Public key in JWK format
    14  	type KeyJwk struct {
    15  	}
    16  
    17  	// Notes a description of the labels used in the test vectors
    18  	type Notes struct {
    19  	}
    20  
    21  	// SignatureTestVector
    22  	type SignatureTestVector struct {
    23  
    24  		// A brief description of the test case
    25  		Comment string `json:"comment,omitempty"`
    26  
    27  		// A list of flags
    28  		Flags []string `json:"flags,omitempty"`
    29  
    30  		// The message to sign
    31  		Msg string `json:"msg,omitempty"`
    32  
    33  		// Test result
    34  		Result string `json:"result,omitempty"`
    35  
    36  		// A signature for msg
    37  		Sig string `json:"sig,omitempty"`
    38  
    39  		// Identifier of the test case
    40  		TcId int `json:"tcId,omitempty"`
    41  	}
    42  
    43  	// RsassaPkcs1TestGroup
    44  	type RsassaPkcs1TestGroup struct {
    45  
    46  		// The private exponent
    47  		D string `json:"d,omitempty"`
    48  
    49  		// The public exponent
    50  		E string `json:"e,omitempty"`
    51  
    52  		// ASN encoding of the sequence [n, e]
    53  		KeyAsn string `json:"keyAsn,omitempty"`
    54  
    55  		// ASN encoding of the public key
    56  		KeyDer string `json:"keyDer,omitempty"`
    57  
    58  		// Public key in JWK format
    59  		KeyJwk *KeyJwk `json:"keyJwk,omitempty"`
    60  
    61  		// Pem encoded public key
    62  		KeyPem string `json:"keyPem,omitempty"`
    63  
    64  		// the size of the modulus in bits
    65  		KeySize int `json:"keySize,omitempty"`
    66  
    67  		// The modulus of the key
    68  		N string `json:"n,omitempty"`
    69  
    70  		// the hash function used for the message
    71  		Sha   string                 `json:"sha,omitempty"`
    72  		Tests []*SignatureTestVector `json:"tests,omitempty"`
    73  		Type  interface{}            `json:"type,omitempty"`
    74  	}
    75  
    76  	// Root
    77  	type Root struct {
    78  
    79  		// the primitive tested in the test file
    80  		Algorithm string `json:"algorithm,omitempty"`
    81  
    82  		// the version of the test vectors.
    83  		GeneratorVersion string `json:"generatorVersion,omitempty"`
    84  
    85  		// additional documentation
    86  		Header []string `json:"header,omitempty"`
    87  
    88  		// a description of the labels used in the test vectors
    89  		Notes *Notes `json:"notes,omitempty"`
    90  
    91  		// the number of test vectors in this test
    92  		NumberOfTests int                     `json:"numberOfTests,omitempty"`
    93  		Schema        interface{}             `json:"schema,omitempty"`
    94  		TestGroups    []*RsassaPkcs1TestGroup `json:"testGroups,omitempty"`
    95  	}
    96  
    97  	flagsShouldPass := map[string]bool{
    98  		// Omitting the parameter field in an ASN encoded integer is a legacy behavior.
    99  		"MissingNull": false,
   100  		// Keys with a modulus less than 2048 bits are supported by crypto/rsa.
   101  		"SmallModulus": true,
   102  		// Small public keys are supported by crypto/rsa.
   103  		"SmallPublicKey": true,
   104  	}
   105  
   106  	var root Root
   107  	readTestVector(t, "rsa_signature_test.json", &root)
   108  	for _, tg := range root.TestGroups {
   109  		pub := decodePublicKey(tg.KeyDer).(*rsa.PublicKey)
   110  		ch := parseHash(tg.Sha)
   111  		h := ch.New()
   112  		for _, sig := range tg.Tests {
   113  			h.Reset()
   114  			h.Write(decodeHex(sig.Msg))
   115  			hashed := h.Sum(nil)
   116  			err := rsa.VerifyPKCS1v15(pub, ch, hashed, decodeHex(sig.Sig))
   117  			want := shouldPass(sig.Result, sig.Flags, flagsShouldPass)
   118  			if (err == nil) != 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