Source file src/crypto/internal/fips140/ecdh/cast.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 ecdh
     6  
     7  import (
     8  	"bytes"
     9  	"crypto/internal/fips140"
    10  	_ "crypto/internal/fips140/check"
    11  	"errors"
    12  	"sync"
    13  )
    14  
    15  var fipsSelfTest = sync.OnceFunc(func() {
    16  	// Per IG D.F, Scenario 2, path (1).
    17  	fips140.CAST("KAS-ECC-SSC P-256", func() error {
    18  		privateKey := []byte{
    19  			0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
    20  			0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
    21  			0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
    22  			0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
    23  		}
    24  		publicKey := []byte{
    25  			0x04,
    26  			0x51, 0x5c, 0x3d, 0x6e, 0xb9, 0xe3, 0x96, 0xb9,
    27  			0x04, 0xd3, 0xfe, 0xca, 0x7f, 0x54, 0xfd, 0xcd,
    28  			0x0c, 0xc1, 0xe9, 0x97, 0xbf, 0x37, 0x5d, 0xca,
    29  			0x51, 0x5a, 0xd0, 0xa6, 0xc3, 0xb4, 0x03, 0x5f,
    30  			0x45, 0x36, 0xbe, 0x3a, 0x50, 0xf3, 0x18, 0xfb,
    31  			0xf9, 0xa5, 0x47, 0x59, 0x02, 0xa2, 0x21, 0x50,
    32  			0x2b, 0xef, 0x0d, 0x57, 0xe0, 0x8c, 0x53, 0xb2,
    33  			0xcc, 0x0a, 0x56, 0xf1, 0x7d, 0x9f, 0x93, 0x54,
    34  		}
    35  		want := []byte{
    36  			0xb4, 0xf1, 0xfc, 0xce, 0x40, 0x73, 0x5f, 0x83,
    37  			0x6a, 0xf8, 0xd6, 0x31, 0x2d, 0x24, 0x8d, 0x1a,
    38  			0x83, 0x48, 0x40, 0x56, 0x69, 0xa1, 0x95, 0xfa,
    39  			0xc5, 0x35, 0x04, 0x06, 0xba, 0x76, 0xbc, 0xce,
    40  		}
    41  		k := &PrivateKey{d: privateKey, pub: PublicKey{curve: p256}}
    42  		peer := &PublicKey{curve: p256, q: publicKey}
    43  		got, err := ecdh(P256(), k, peer)
    44  		if err != nil {
    45  			return err
    46  		}
    47  		if !bytes.Equal(got, want) {
    48  			return errors.New("unexpected result")
    49  		}
    50  		return nil
    51  	})
    52  })
    53  

View as plain text