Source file src/crypto/internal/fips140test/hash_fips140v1.28_test.go

     1  // Copyright 2026 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  //go:build !(fips140v1.0 || fips140v1.26)
     6  
     7  package fipstest
     8  
     9  import (
    10  	"crypto/internal/fips140"
    11  	"crypto/internal/fips140/hmac"
    12  	"crypto/internal/fips140/sha256"
    13  	"crypto/internal/fips140/sha3"
    14  	"crypto/internal/fips140/sha512"
    15  	"hash"
    16  	"testing"
    17  )
    18  
    19  func TestApprovedHash(t *testing.T) {
    20  	type wrapper struct{ *sha256.Digest }
    21  
    22  	tests := []struct {
    23  		name     string
    24  		h        func() hash.Hash
    25  		approved bool
    26  	}{
    27  		{"SHA2-224", func() hash.Hash { return sha256.New224() }, true},
    28  		{"SHA2-256", func() hash.Hash { return sha256.New() }, true},
    29  		{"SHA2-384", func() hash.Hash { return sha512.New384() }, true},
    30  		{"SHA2-512", func() hash.Hash { return sha512.New() }, true},
    31  		{"SHA2-512/224", func() hash.Hash { return sha512.New512_224() }, true},
    32  		{"SHA2-512/256", func() hash.Hash { return sha512.New512_256() }, true},
    33  		{"SHA3-224", func() hash.Hash { return sha3.New224() }, true},
    34  		{"SHA3-256", func() hash.Hash { return sha3.New256() }, true},
    35  		{"SHA3-384", func() hash.Hash { return sha3.New384() }, true},
    36  		{"SHA3-512", func() hash.Hash { return sha3.New512() }, true},
    37  		{"Keccak-256", func() hash.Hash { return sha3.NewLegacyKeccak256() }, false},
    38  		{"Keccak-512", func() hash.Hash { return sha3.NewLegacyKeccak512() }, false},
    39  		{"wrapped SHA2-256", func() hash.Hash { return wrapper{sha256.New()} }, false},
    40  	}
    41  	for _, tt := range tests {
    42  		t.Run(tt.name, func(t *testing.T) {
    43  			if got := fips140.ApprovedHash(tt.h()); got != tt.approved {
    44  				t.Errorf("ApprovedHash = %v, want %v", got, tt.approved)
    45  			}
    46  
    47  			fips140.ResetServiceIndicator()
    48  			h := hmac.New(tt.h, make([]byte, 32))
    49  			h.Write([]byte("input"))
    50  			h.Sum(nil)
    51  			if got := fips140.ServiceIndicator(); got != tt.approved {
    52  				t.Errorf("HMAC service indicator = %v, want %v", got, tt.approved)
    53  			}
    54  		})
    55  	}
    56  }
    57  

View as plain text