...

Source file src/github.com/theupdateframework/go-tuf/pkg/keys/ed25519_test.go

Documentation: github.com/theupdateframework/go-tuf/pkg/keys

     1  package keys
     2  
     3  import (
     4  	"crypto/ed25519"
     5  	"crypto/rand"
     6  	"encoding/hex"
     7  	"encoding/json"
     8  	"errors"
     9  	"io"
    10  	"strings"
    11  
    12  	fuzz "github.com/google/gofuzz"
    13  	"github.com/theupdateframework/go-tuf/data"
    14  	. "gopkg.in/check.v1"
    15  )
    16  
    17  type Ed25519Suite struct{}
    18  
    19  var _ = Suite(&Ed25519Suite{})
    20  
    21  func (Ed25519Suite) TestUnmarshalEd25519(c *C) {
    22  	pub, _, err := ed25519.GenerateKey(strings.NewReader("00001-deterministic-buffer-for-key-generation"))
    23  	c.Assert(err, IsNil)
    24  
    25  	publicKey, err := json.Marshal(map[string]string{
    26  		"public": hex.EncodeToString(pub),
    27  	})
    28  	c.Assert(err, IsNil)
    29  
    30  	badKey := &data.PublicKey{
    31  		Type:       data.KeyTypeEd25519,
    32  		Scheme:     data.KeySchemeEd25519,
    33  		Algorithms: data.HashAlgorithms,
    34  		Value:      publicKey,
    35  	}
    36  	verifier := NewEd25519Verifier()
    37  	c.Assert(verifier.UnmarshalPublicKey(badKey), IsNil)
    38  }
    39  
    40  func (Ed25519Suite) TestUnmarshalEd25519_Invalid(c *C) {
    41  	badKeyValue, err := json.Marshal(true)
    42  	c.Assert(err, IsNil)
    43  	badKey := &data.PublicKey{
    44  		Type:       data.KeyTypeEd25519,
    45  		Scheme:     data.KeySchemeEd25519,
    46  		Algorithms: data.HashAlgorithms,
    47  		Value:      badKeyValue,
    48  	}
    49  	verifier := NewEd25519Verifier()
    50  	c.Assert(verifier.UnmarshalPublicKey(badKey), ErrorMatches, "json: cannot unmarshal.*")
    51  }
    52  
    53  func (Ed25519Suite) TestUnmarshalEd25519_FastFuzz(c *C) {
    54  	verifier := NewEd25519Verifier()
    55  	for i := 0; i < 50; i++ {
    56  		// Ensure no basic panic
    57  
    58  		f := fuzz.New()
    59  		var publicData data.PublicKey
    60  		f.Fuzz(&publicData)
    61  
    62  		verifier.UnmarshalPublicKey(&publicData)
    63  	}
    64  }
    65  
    66  func (Ed25519Suite) TestUnmarshalEd25519_TooLongContent(c *C) {
    67  	randomSeed := make([]byte, MaxJSONKeySize)
    68  	_, err := io.ReadFull(rand.Reader, randomSeed)
    69  	c.Assert(err, IsNil)
    70  
    71  	tooLongPayload, err := json.Marshal(
    72  		&ed25519Verifier{
    73  			PublicKey: data.HexBytes(hex.EncodeToString(randomSeed)),
    74  		},
    75  	)
    76  	c.Assert(err, IsNil)
    77  
    78  	badKey := &data.PublicKey{
    79  		Type:       data.KeyTypeEd25519,
    80  		Scheme:     data.KeySchemeEd25519,
    81  		Algorithms: data.HashAlgorithms,
    82  		Value:      tooLongPayload,
    83  	}
    84  	verifier := NewEd25519Verifier()
    85  	err = verifier.UnmarshalPublicKey(badKey)
    86  	c.Assert(errors.Is(err, io.ErrUnexpectedEOF), Equals, true)
    87  }
    88  
    89  func (Ed25519Suite) TestSignVerify(c *C) {
    90  	signer, err := GenerateEd25519Key()
    91  	c.Assert(err, IsNil)
    92  	msg := []byte("foo")
    93  	sig, err := signer.SignMessage(msg)
    94  	c.Assert(err, IsNil)
    95  	publicData := signer.PublicData()
    96  	pubKey, err := GetVerifier(publicData)
    97  	c.Assert(err, IsNil)
    98  	c.Assert(pubKey.Verify(msg, sig), IsNil)
    99  }
   100  

View as plain text