...

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

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

     1  // Copyright 2020 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/hmac"
     9  	"testing"
    10  )
    11  
    12  func TestHMAC(t *testing.T) {
    13  	// MacTestVector
    14  	type MacTestVector struct {
    15  
    16  		// A brief description of the test case
    17  		Comment string `json:"comment,omitempty"`
    18  
    19  		// A list of flags
    20  		Flags []string `json:"flags,omitempty"`
    21  
    22  		// the key
    23  		Key string `json:"key,omitempty"`
    24  
    25  		// the plaintext
    26  		Msg string `json:"msg,omitempty"`
    27  
    28  		// Test result
    29  		Result string `json:"result,omitempty"`
    30  
    31  		// the authentication tag
    32  		Tag string `json:"tag,omitempty"`
    33  
    34  		// Identifier of the test case
    35  		TcId int `json:"tcId,omitempty"`
    36  	}
    37  
    38  	// MacTestGroup
    39  	type MacTestGroup struct {
    40  
    41  		// the keySize in bits
    42  		KeySize int `json:"keySize,omitempty"`
    43  
    44  		// the expected size of the tag in bits
    45  		TagSize int              `json:"tagSize,omitempty"`
    46  		Tests   []*MacTestVector `json:"tests,omitempty"`
    47  		Type    interface{}      `json:"type,omitempty"`
    48  	}
    49  
    50  	// Notes a description of the labels used in the test vectors
    51  	type Notes struct {
    52  	}
    53  
    54  	// Root
    55  	type Root struct {
    56  
    57  		// the primitive tested in the test file
    58  		Algorithm string `json:"algorithm,omitempty"`
    59  
    60  		// the version of the test vectors.
    61  		GeneratorVersion string `json:"generatorVersion,omitempty"`
    62  
    63  		// additional documentation
    64  		Header []string `json:"header,omitempty"`
    65  
    66  		// a description of the labels used in the test vectors
    67  		Notes *Notes `json:"notes,omitempty"`
    68  
    69  		// the number of test vectors in this test
    70  		NumberOfTests int             `json:"numberOfTests,omitempty"`
    71  		Schema        interface{}     `json:"schema,omitempty"`
    72  		TestGroups    []*MacTestGroup `json:"testGroups,omitempty"`
    73  	}
    74  
    75  	fileHashAlgs := map[string]string{
    76  		"hmac_sha1_test.json":   "SHA-1",
    77  		"hmac_sha224_test.json": "SHA-224",
    78  		"hmac_sha256_test.json": "SHA-256",
    79  		"hmac_sha384_test.json": "SHA-384",
    80  		"hmac_sha512_test.json": "SHA-512",
    81  	}
    82  
    83  	for f := range fileHashAlgs {
    84  		var root Root
    85  		readTestVector(t, f, &root)
    86  		for _, tg := range root.TestGroups {
    87  			h := parseHash(fileHashAlgs[f])
    88  			// Skip test vectors where the tag length does not equal the
    89  			// hash length, since crypto/hmac does not support generating
    90  			// these truncated tags.
    91  			if tg.TagSize/8 != h.Size() {
    92  				continue
    93  			}
    94  			for _, tv := range tg.Tests {
    95  				hm := hmac.New(h.New, decodeHex(tv.Key))
    96  				hm.Write(decodeHex(tv.Msg))
    97  				tag := hm.Sum(nil)
    98  				got := hmac.Equal(decodeHex(tv.Tag), tag)
    99  				if want := shouldPass(tv.Result, tv.Flags, nil); want != got {
   100  					t.Errorf("%s, tcid: %d, type: %s, comment: %q, unexpected result", f, tv.TcId, tv.Result, tv.Comment)
   101  				}
   102  			}
   103  		}
   104  	}
   105  }
   106  

View as plain text