...

Source file src/golang.org/x/crypto/internal/wycheproof/hkdf_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  	"bytes"
     9  	"io"
    10  	"testing"
    11  
    12  	"golang.org/x/crypto/hkdf"
    13  )
    14  
    15  func TestHkdf(t *testing.T) {
    16  
    17  	// HkdfTestVector
    18  	type HkdfTestVector struct {
    19  
    20  		// A brief description of the test case
    21  		Comment string `json:"comment,omitempty"`
    22  
    23  		// A list of flags
    24  		Flags []string `json:"flags,omitempty"`
    25  
    26  		// the key (input key material)
    27  		Ikm string `json:"ikm,omitempty"`
    28  
    29  		// additional information used in the key derivation
    30  		Info string `json:"info,omitempty"`
    31  
    32  		// the generated bytes (output key material)
    33  		Okm string `json:"okm,omitempty"`
    34  
    35  		// Test result
    36  		Result string `json:"result,omitempty"`
    37  
    38  		// the salt for the key derivation
    39  		Salt string `json:"salt,omitempty"`
    40  
    41  		// the size of the output in bytes
    42  		Size int `json:"size,omitempty"`
    43  
    44  		// Identifier of the test case
    45  		TcId int `json:"tcId,omitempty"`
    46  	}
    47  
    48  	// Notes a description of the labels used in the test vectors
    49  	type Notes struct {
    50  	}
    51  
    52  	// HkdfTestGroup
    53  	type HkdfTestGroup struct {
    54  
    55  		// the size of the ikm in bits
    56  		KeySize int               `json:"keySize,omitempty"`
    57  		Tests   []*HkdfTestVector `json:"tests,omitempty"`
    58  		Type    interface{}       `json:"type,omitempty"`
    59  	}
    60  
    61  	// Root
    62  	type Root struct {
    63  
    64  		// the primitive tested in the test file
    65  		Algorithm string `json:"algorithm,omitempty"`
    66  
    67  		// the version of the test vectors.
    68  		GeneratorVersion string `json:"generatorVersion,omitempty"`
    69  
    70  		// additional documentation
    71  		Header []string `json:"header,omitempty"`
    72  
    73  		// a description of the labels used in the test vectors
    74  		Notes *Notes `json:"notes,omitempty"`
    75  
    76  		// the number of test vectors in this test
    77  		NumberOfTests int              `json:"numberOfTests,omitempty"`
    78  		Schema        interface{}      `json:"schema,omitempty"`
    79  		TestGroups    []*HkdfTestGroup `json:"testGroups,omitempty"`
    80  	}
    81  
    82  	fileHashAlgorithms := map[string]string{
    83  		"hkdf_sha1_test.json":   "SHA-1",
    84  		"hkdf_sha256_test.json": "SHA-256",
    85  		"hkdf_sha384_test.json": "SHA-384",
    86  		"hkdf_sha512_test.json": "SHA-512",
    87  	}
    88  
    89  	for f := range fileHashAlgorithms {
    90  		var root Root
    91  		readTestVector(t, f, &root)
    92  		for _, tg := range root.TestGroups {
    93  			for _, tv := range tg.Tests {
    94  				h := parseHash(fileHashAlgorithms[f]).New
    95  				hkdf := hkdf.New(h, decodeHex(tv.Ikm), decodeHex(tv.Salt), decodeHex(tv.Info))
    96  				key := make([]byte, tv.Size)
    97  				wantPass := shouldPass(tv.Result, tv.Flags, nil)
    98  				_, err := io.ReadFull(hkdf, key)
    99  				if (err == nil) != wantPass {
   100  					t.Errorf("tcid: %d, type: %s, comment: %q, wanted success: %t, got: %v", tv.TcId, tv.Result, tv.Comment, wantPass, err)
   101  				}
   102  				if err != nil {
   103  					continue // don't validate output text if reading failed
   104  				}
   105  				if got, want := key, decodeHex(tv.Okm); !bytes.Equal(got, want) {
   106  					t.Errorf("tcid: %d, type: %s, comment: %q, output bytes don't match", tv.TcId, tv.Result, tv.Comment)
   107  				}
   108  			}
   109  		}
   110  	}
   111  }
   112  

View as plain text