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

     1  package eddilithium3
     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 "Ed448-Dilithium3" }
    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 0xfe62 /* temp */ }
    23  func (*scheme) SupportsContext() bool { return false }
    24  func (*scheme) Oid() asn1.ObjectIdentifier {
    25  	return asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 44363, 45, 10}
    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  	var sig [SignatureSize]byte
    45  	SignTo(priv, message, sig[:])
    46  	return sig[:]
    47  }
    48  
    49  func (*scheme) Verify(
    50  	pk sign.PublicKey,
    51  	message, signature []byte,
    52  	opts *sign.SignatureOpts,
    53  ) bool {
    54  	pub, ok := pk.(*PublicKey)
    55  	if !ok {
    56  		panic(sign.ErrTypeMismatch)
    57  	}
    58  	if opts != nil && opts.Context != "" {
    59  		panic(sign.ErrContextNotSupported)
    60  	}
    61  	return Verify(pub, message, signature)
    62  }
    63  
    64  func (*scheme) DeriveKey(seed []byte) (sign.PublicKey, sign.PrivateKey) {
    65  	if len(seed) != SeedSize {
    66  		panic(sign.ErrSeedSize)
    67  	}
    68  	var tmp [SeedSize]byte
    69  	copy(tmp[:], seed)
    70  	return NewKeyFromSeed(&tmp)
    71  }
    72  
    73  func (*scheme) UnmarshalBinaryPublicKey(buf []byte) (sign.PublicKey, error) {
    74  	if len(buf) != PublicKeySize {
    75  		return nil, sign.ErrPubKeySize
    76  	}
    77  	var tmp [PublicKeySize]byte
    78  	copy(tmp[:], buf)
    79  	var ret PublicKey
    80  	ret.Unpack(&tmp)
    81  	return &ret, nil
    82  }
    83  
    84  func (*scheme) UnmarshalBinaryPrivateKey(buf []byte) (sign.PrivateKey, error) {
    85  	if len(buf) != PrivateKeySize {
    86  		return nil, sign.ErrPrivKeySize
    87  	}
    88  	var tmp [PrivateKeySize]byte
    89  	copy(tmp[:], buf)
    90  	var ret PrivateKey
    91  	ret.Unpack(&tmp)
    92  	return &ret, nil
    93  }