...

Source file src/github.com/cloudflare/circl/pke/kyber/templates/pkg.templ.go

Documentation: github.com/cloudflare/circl/pke/kyber/templates

     1  // +build ignore
     2  // The previous line (and this one up to the warning below) is removed by the
     3  // template generator.
     4  
     5  // Code generated from modePkg.templ.go. DO NOT EDIT.
     6  
     7  // {{.Pkg}} implements the IND-CPA-secure Public Key Encryption
     8  // scheme {{.Name}}.CPAPKE as submitted to round 3 of the NIST PQC competition
     9  // and described in
    10  //
    11  // https://pq-crystals.org/kyber/data/kyber-specification-round3.pdf
    12  package {{.Pkg}}
    13  
    14  import (
    15  	cryptoRand "crypto/rand"
    16  	"io"
    17  
    18  	"github.com/cloudflare/circl/pke/kyber/{{.Pkg}}/internal"
    19  )
    20  
    21  const (
    22  	// Size of seed for NewKeyFromSeed
    23  	KeySeedSize = internal.SeedSize
    24  
    25  	// Size of seed for EncryptTo
    26  	EncryptionSeedSize = internal.SeedSize
    27  
    28  	// Size of a packed PublicKey
    29  	PublicKeySize = internal.PublicKeySize
    30  
    31  	// Size of a packed PrivateKey
    32  	PrivateKeySize = internal.PrivateKeySize
    33  
    34  	// Size of a ciphertext
    35  	CiphertextSize = internal.CiphertextSize
    36  
    37  	// Size of a plaintext
    38  	PlaintextSize = internal.PlaintextSize
    39  )
    40  
    41  // PublicKey is the type of {{.Name}}.CPAPKE public key
    42  type PublicKey internal.PublicKey
    43  
    44  // PrivateKey is the type of {{.Name}}.CPAPKE private key
    45  type PrivateKey internal.PrivateKey
    46  
    47  // GenerateKey generates a public/private key pair using entropy from rand.
    48  // If rand is nil, crypto/rand.Reader will be used.
    49  func GenerateKey(rand io.Reader) (*PublicKey, *PrivateKey, error) {
    50  	var seed [KeySeedSize]byte
    51  	if rand == nil {
    52  		rand = cryptoRand.Reader
    53  	}
    54  	_, err := io.ReadFull(rand, seed[:])
    55  	if err != nil {
    56  		return nil, nil, err
    57  	}
    58  	pk, sk := internal.NewKeyFromSeed(seed[:])
    59  	return (*PublicKey)(pk), (*PrivateKey)(sk), nil
    60  }
    61  
    62  // NewKeyFromSeed derives a public/private key pair using the given seed.
    63  //
    64  // Panics if seed is not of length KeySeedSize.
    65  func NewKeyFromSeed(seed []byte) (*PublicKey, *PrivateKey) {
    66  	if len(seed) != KeySeedSize {
    67  		panic("seed must be of length KeySeedSize")
    68  	}
    69  	pk, sk := internal.NewKeyFromSeed(seed)
    70  	return (*PublicKey)(pk), (*PrivateKey)(sk)
    71  }
    72  
    73  // EncryptTo encrypts message pt for the public key and writes the ciphertext
    74  // to ct using randomness from seed.
    75  //
    76  // This function panics if the lengths of pt, seed, and ct are not
    77  // PlaintextSize, EncryptionSeedSize, and CiphertextSize respectively.
    78  func (pk *PublicKey) EncryptTo(ct []byte, pt []byte, seed []byte) {
    79  	if len(pt) != PlaintextSize {
    80  		panic("pt must be of length PlaintextSize")
    81  	}
    82  	if len(ct) != CiphertextSize {
    83  		panic("ct must be of length CiphertextSize")
    84  	}
    85  	if len(seed) != EncryptionSeedSize {
    86  		panic("seed must be of length EncryptionSeedSize")
    87  	}
    88  	(*internal.PublicKey)(pk).EncryptTo(ct, pt, seed)
    89  }
    90  
    91  // DecryptTo decrypts message ct for the private key and writes the
    92  // plaintext to pt.
    93  //
    94  // This function panics if the lengths of ct and pt are not
    95  // CiphertextSize and PlaintextSize respectively.
    96  func (sk *PrivateKey) DecryptTo(pt []byte, ct []byte) {
    97  	if len(pt) != PlaintextSize {
    98  		panic("pt must be of length PlaintextSize")
    99  	}
   100  	if len(ct) != CiphertextSize {
   101  		panic("ct must be of length CiphertextSize")
   102  	}
   103  	(*internal.PrivateKey)(sk).DecryptTo(pt, ct)
   104  }
   105  
   106  // Packs pk into the given buffer.
   107  //
   108  // Panics if buf is not of length PublicKeySize.
   109  func (pk *PublicKey) Pack(buf []byte) {
   110  	if len(buf) != PublicKeySize {
   111  		panic("buf must be of size PublicKeySize")
   112  	}
   113  	(*internal.PublicKey)(pk).Pack(buf)
   114  }
   115  
   116  // Packs sk into the given buffer.
   117  //
   118  // Panics if buf is not of length PrivateKeySize.
   119  func (sk *PrivateKey) Pack(buf []byte) {
   120  	if len(buf) != PrivateKeySize {
   121  		panic("buf must be of size PrivateKeySize")
   122  	}
   123  	(*internal.PrivateKey)(sk).Pack(buf)
   124  }
   125  
   126  // Unpacks pk from the given buffer.
   127  //
   128  // Panics if buf is not of length PublicKeySize.
   129  func (pk *PublicKey) Unpack(buf []byte) {
   130  	if len(buf) != PublicKeySize {
   131  		panic("buf must be of size PublicKeySize")
   132  	}
   133  	(*internal.PublicKey)(pk).Unpack(buf)
   134  }
   135  
   136  // Unpacks sk from the given buffer.
   137  //
   138  // Panics if buf is not of length PrivateKeySize.
   139  func (sk *PrivateKey) Unpack(buf []byte) {
   140  	if len(buf) != PrivateKeySize {
   141  		panic("buf must be of size PrivateKeySize")
   142  	}
   143  	(*internal.PrivateKey)(sk).Unpack(buf)
   144  }
   145  
   146  // Returns whether the two private keys are equal.
   147  func (sk *PrivateKey) Equal(other *PrivateKey) bool {
   148  	return (*internal.PrivateKey)(sk).Equal((*internal.PrivateKey)(other))
   149  }
   150  

View as plain text