Source file src/crypto/internal/fips140/rsa/pkcs1v15_test.go

     1  // Copyright 2024 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 rsa
     6  
     7  import (
     8  	"bytes"
     9  	"crypto"
    10  	"crypto/x509/pkix"
    11  	"encoding/asn1"
    12  	"testing"
    13  )
    14  
    15  func TestHashPrefixes(t *testing.T) {
    16  	prefixes := map[crypto.Hash]asn1.ObjectIdentifier{
    17  		// RFC 3370, Section 2.1 and 2.2
    18  		//
    19  		// sha-1 OBJECT IDENTIFIER ::= { iso(1) identified-organization(3)
    20  		//      oiw(14) secsig(3) algorithm(2) 26 }
    21  		//
    22  		// md5 OBJECT IDENTIFIER ::= { iso(1) member-body(2) us(840)
    23  		// 	rsadsi(113549) digestAlgorithm(2) 5 }
    24  		crypto.MD5:  {1, 2, 840, 113549, 2, 5},
    25  		crypto.SHA1: {1, 3, 14, 3, 2, 26},
    26  
    27  		// https://csrc.nist.gov/projects/computer-security-objects-register/algorithm-registration
    28  		//
    29  		// nistAlgorithms OBJECT IDENTIFIER ::= { joint-iso-ccitt(2) country(16) us(840)
    30  		//          organization(1) gov(101) csor(3) nistAlgorithm(4) }
    31  		//
    32  		// hashAlgs OBJECT IDENTIFIER ::= { nistAlgorithms 2 }
    33  		//
    34  		// id-sha256 OBJECT IDENTIFIER ::= { hashAlgs 1 }
    35  		// id-sha384 OBJECT IDENTIFIER ::= { hashAlgs 2 }
    36  		// id-sha512 OBJECT IDENTIFIER ::= { hashAlgs 3 }
    37  		// id-sha224 OBJECT IDENTIFIER ::= { hashAlgs 4 }
    38  		// id-sha512-224 OBJECT IDENTIFIER ::= { hashAlgs 5 }
    39  		// id-sha512-256 OBJECT IDENTIFIER ::= { hashAlgs 6 }
    40  		// id-sha3-224 OBJECT IDENTIFIER ::= { hashAlgs 7 }
    41  		// id-sha3-256 OBJECT IDENTIFIER ::= { hashAlgs 8 }
    42  		// id-sha3-384 OBJECT IDENTIFIER ::= { hashAlgs 9 }
    43  		// id-sha3-512 OBJECT IDENTIFIER ::= { hashAlgs 10 }
    44  		crypto.SHA224:     {2, 16, 840, 1, 101, 3, 4, 2, 4},
    45  		crypto.SHA256:     {2, 16, 840, 1, 101, 3, 4, 2, 1},
    46  		crypto.SHA384:     {2, 16, 840, 1, 101, 3, 4, 2, 2},
    47  		crypto.SHA512:     {2, 16, 840, 1, 101, 3, 4, 2, 3},
    48  		crypto.SHA512_224: {2, 16, 840, 1, 101, 3, 4, 2, 5},
    49  		crypto.SHA512_256: {2, 16, 840, 1, 101, 3, 4, 2, 6},
    50  		crypto.SHA3_224:   {2, 16, 840, 1, 101, 3, 4, 2, 7},
    51  		crypto.SHA3_256:   {2, 16, 840, 1, 101, 3, 4, 2, 8},
    52  		crypto.SHA3_384:   {2, 16, 840, 1, 101, 3, 4, 2, 9},
    53  		crypto.SHA3_512:   {2, 16, 840, 1, 101, 3, 4, 2, 10},
    54  	}
    55  
    56  	for h, oid := range prefixes {
    57  		want, err := asn1.Marshal(struct {
    58  			HashAlgorithm pkix.AlgorithmIdentifier
    59  			Hash          []byte
    60  		}{
    61  			HashAlgorithm: pkix.AlgorithmIdentifier{
    62  				Algorithm:  oid,
    63  				Parameters: asn1.NullRawValue,
    64  			},
    65  			Hash: make([]byte, h.Size()),
    66  		})
    67  		if err != nil {
    68  			t.Fatal(err)
    69  		}
    70  		want = want[:len(want)-h.Size()]
    71  		got := hashPrefixes[h.String()]
    72  		if !bytes.Equal(got, want) {
    73  			t.Errorf("%s: got %x, want %x", h, got, want)
    74  		}
    75  	}
    76  }
    77  

View as plain text