github.com/cloudflare/circl@v1.5.0/sign/ed25519/signapi.go (about)

     1  package ed25519
     2  
     3  import (
     4  	"crypto/rand"
     5  	"encoding/asn1"
     6  
     7  	"github.com/cloudflare/circl/sign"
     8  )
     9  
    10  var sch sign.Scheme = &scheme{}
    11  
    12  // Scheme returns a signature interface.
    13  func Scheme() sign.Scheme { return sch }
    14  
    15  type scheme struct{}
    16  
    17  func (*scheme) Name() string          { return "Ed25519" }
    18  func (*scheme) PublicKeySize() int    { return PublicKeySize }
    19  func (*scheme) PrivateKeySize() int   { return PrivateKeySize }
    20  func (*scheme) SignatureSize() int    { return SignatureSize }
    21  func (*scheme) SeedSize() int         { return SeedSize }
    22  func (*scheme) TLSIdentifier() uint   { return 0x0807 }
    23  func (*scheme) SupportsContext() bool { return false }
    24  func (*scheme) Oid() asn1.ObjectIdentifier {
    25  	return asn1.ObjectIdentifier{1, 3, 101, 112}
    26  }
    27  
    28  func (*scheme) GenerateKey() (sign.PublicKey, sign.PrivateKey, error) {
    29  	return GenerateKey(rand.Reader)
    30  }
    31  
    32  func (*scheme) Sign(
    33  	sk sign.PrivateKey,
    34  	message []byte,
    35  	opts *sign.SignatureOpts,
    36  ) []byte {
    37  	priv, ok := sk.(PrivateKey)
    38  	if !ok {
    39  		panic(sign.ErrTypeMismatch)
    40  	}
    41  	if opts != nil && opts.Context != "" {
    42  		panic(sign.ErrContextNotSupported)
    43  	}
    44  	return Sign(priv, message)
    45  }
    46  
    47  func (*scheme) Verify(
    48  	pk sign.PublicKey,
    49  	message, signature []byte,
    50  	opts *sign.SignatureOpts,
    51  ) bool {
    52  	pub, ok := pk.(PublicKey)
    53  	if !ok {
    54  		panic(sign.ErrTypeMismatch)
    55  	}
    56  	if opts != nil {
    57  		if opts.Context != "" {
    58  			panic(sign.ErrContextNotSupported)
    59  		}
    60  	}
    61  	return Verify(pub, message, signature)
    62  }
    63  
    64  func (*scheme) DeriveKey(seed []byte) (sign.PublicKey, sign.PrivateKey) {
    65  	privateKey := NewKeyFromSeed(seed)
    66  	publicKey := make(PublicKey, PublicKeySize)
    67  	copy(publicKey, privateKey[SeedSize:])
    68  	return publicKey, privateKey
    69  }
    70  
    71  func (*scheme) UnmarshalBinaryPublicKey(buf []byte) (sign.PublicKey, error) {
    72  	if len(buf) < PublicKeySize {
    73  		return nil, sign.ErrPubKeySize
    74  	}
    75  	pub := make(PublicKey, PublicKeySize)
    76  	copy(pub, buf[:PublicKeySize])
    77  	return pub, nil
    78  }
    79  
    80  func (*scheme) UnmarshalBinaryPrivateKey(buf []byte) (sign.PrivateKey, error) {
    81  	if len(buf) < PrivateKeySize {
    82  		return nil, sign.ErrPrivKeySize
    83  	}
    84  	priv := make(PrivateKey, PrivateKeySize)
    85  	copy(priv, buf[:PrivateKeySize])
    86  	return priv, nil
    87  }