...

Source file src/github.com/cloudflare/circl/pke/kyber/kyber768/kyber.go

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

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

View as plain text