| 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 pkg.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/kem" |
| 19 |
"github.com/cloudflare/circl/pke/kyber/{{.Pkg}}/internal" |
| 20 |
) |
| 21 |
|
| 22 |
const ( |
| 23 |
// Size of seed for NewKeyFromSeed |
| 24 |
KeySeedSize = internal.SeedSize |
| 25 |
|
| 26 |
// Size of seed for EncryptTo |
| 27 |
EncryptionSeedSize = internal.SeedSize |
| 28 |
|
| 29 |
// Size of a packed PublicKey |
| 30 |
PublicKeySize = internal.PublicKeySize |
| 31 |
|
| 32 |
// Size of a packed PrivateKey |
| 33 |
PrivateKeySize = internal.PrivateKeySize |
| 34 |
|
| 35 |
// Size of a ciphertext |
| 36 |
CiphertextSize = internal.CiphertextSize |
| 37 |
|
| 38 |
// Size of a plaintext |
| 39 |
PlaintextSize = internal.PlaintextSize |
| 40 |
) |
| 41 |
|
| 42 |
// PublicKey is the type of {{.Name}}.CPAPKE public key |
| 43 |
type PublicKey internal.PublicKey |
| 44 |
|
| 45 |
// PrivateKey is the type of {{.Name}}.CPAPKE private key |
| 46 |
type PrivateKey internal.PrivateKey |
| 47 |
|
| 48 |
// GenerateKey generates a public/private key pair using entropy from rand. |
| 49 |
// If rand is nil, crypto/rand.Reader will be used. |
| 50 |
func GenerateKey(rand io.Reader) (*PublicKey, *PrivateKey, error) { |
| 51 |
var seed [KeySeedSize]byte |
| 52 |
if rand == nil { |
| 53 |
rand = cryptoRand.Reader |
| 54 |
} |
| 55 |
_, err := io.ReadFull(rand, seed[:]) |
| 56 |
if err != nil { |
| 57 |
return nil, nil, err |
| 58 |
} |
| 59 |
pk, sk := internal.NewKeyFromSeed(seed[:]) |
| 60 |
return (*PublicKey)(pk), (*PrivateKey)(sk), nil |
| 61 |
} |
| 62 |
|
| 63 |
// NewKeyFromSeed derives a public/private key pair using the given seed. |
| 64 |
// |
| 65 |
// Note: does not include the domain separation of ML-KEM (line 1, algorithm 13 |
| 66 |
// of FIPS 203). For that use NewKeyFromSeedMLKEM(). |
| 67 |
// |
| 68 |
// Panics if seed is not of length KeySeedSize. |
| 69 |
func NewKeyFromSeed(seed []byte) (*PublicKey, *PrivateKey) { |
| 70 |
if len(seed) != KeySeedSize { |
| 71 |
panic("seed must be of length KeySeedSize") |
| 72 |
} |
| 73 |
pk, sk := internal.NewKeyFromSeed(seed) |
| 74 |
return (*PublicKey)(pk), (*PrivateKey)(sk) |
| 75 |
} |
| 76 |
|
| 77 |
// NewKeyFromSeedMLKEM derives a public/private key pair using the given seed |
| 78 |
// using the domain separation of ML-KEM. |
| 79 |
// |
| 80 |
// Panics if seed is not of length KeySeedSize. |
| 81 |
func NewKeyFromSeedMLKEM(seed []byte) (*PublicKey, *PrivateKey) { |
| 82 |
if len(seed) != KeySeedSize { |
| 83 |
panic("seed must be of length KeySeedSize") |
| 84 |
} |
| 85 |
var seed2 [33]byte |
| 86 |
copy(seed2[:32], seed) |
| 87 |
seed2[32] = byte(internal.K) |
| 88 |
pk, sk := internal.NewKeyFromSeed(seed2[:]) |
| 89 |
return (*PublicKey)(pk), (*PrivateKey)(sk) |
| 90 |
} |
| 91 |
|
| 92 |
// EncryptTo encrypts message pt for the public key and writes the ciphertext |
| 93 |
// to ct using randomness from seed. |
| 94 |
// |
| 95 |
// This function panics if the lengths of pt, seed, and ct are not |
| 96 |
// PlaintextSize, EncryptionSeedSize, and CiphertextSize respectively. |
| 97 |
func (pk *PublicKey) EncryptTo(ct []byte, pt []byte, seed []byte) { |
| 98 |
if len(pt) != PlaintextSize { |
| 99 |
panic("pt must be of length PlaintextSize") |
| 100 |
} |
| 101 |
if len(ct) != CiphertextSize { |
| 102 |
panic("ct must be of length CiphertextSize") |
| 103 |
} |
| 104 |
if len(seed) != EncryptionSeedSize { |
| 105 |
panic("seed must be of length EncryptionSeedSize") |
| 106 |
} |
| 107 |
(*internal.PublicKey)(pk).EncryptTo(ct, pt, seed) |
| 108 |
} |
| 109 |
|
| 110 |
// DecryptTo decrypts message ct for the private key and writes the |
| 111 |
// plaintext to pt. |
| 112 |
// |
| 113 |
// This function panics if the lengths of ct and pt are not |
| 114 |
// CiphertextSize and PlaintextSize respectively. |
| 115 |
func (sk *PrivateKey) DecryptTo(pt []byte, ct []byte) { |
| 116 |
if len(pt) != PlaintextSize { |
| 117 |
panic("pt must be of length PlaintextSize") |
| 118 |
} |
| 119 |
if len(ct) != CiphertextSize { |
| 120 |
panic("ct must be of length CiphertextSize") |
| 121 |
} |
| 122 |
(*internal.PrivateKey)(sk).DecryptTo(pt, ct) |
| 123 |
} |
| 124 |
|
| 125 |
// Packs pk into the given buffer. |
| 126 |
// |
| 127 |
// Panics if buf is not of length PublicKeySize. |
| 128 |
func (pk *PublicKey) Pack(buf []byte) { |
| 129 |
if len(buf) != PublicKeySize { |
| 130 |
panic("buf must be of size PublicKeySize") |
| 131 |
} |
| 132 |
(*internal.PublicKey)(pk).Pack(buf) |
| 133 |
} |
| 134 |
|
| 135 |
// Packs sk into the given buffer. |
| 136 |
// |
| 137 |
// Panics if buf is not of length PrivateKeySize. |
| 138 |
func (sk *PrivateKey) Pack(buf []byte) { |
| 139 |
if len(buf) != PrivateKeySize { |
| 140 |
panic("buf must be of size PrivateKeySize") |
| 141 |
} |
| 142 |
(*internal.PrivateKey)(sk).Pack(buf) |
| 143 |
} |
| 144 |
|
| 145 |
// Unpacks pk from the given buffer. |
| 146 |
// |
| 147 |
// Panics if buf is not of length PublicKeySize. |
| 148 |
func (pk *PublicKey) Unpack(buf []byte) { |
| 149 |
if len(buf) != PublicKeySize { |
| 150 |
panic("buf must be of size PublicKeySize") |
| 151 |
} |
| 152 |
(*internal.PublicKey)(pk).Unpack(buf) |
| 153 |
} |
| 154 |
|
| 155 |
// Unpacks pk from the given buffer. |
| 156 |
// |
| 157 |
// Returns an error if the buffer is not of the right size, or the public |
| 158 |
// key is not normalized. |
| 159 |
func (pk *PublicKey) UnpackMLKEM(buf []byte) error { |
| 160 |
if len(buf) != PublicKeySize { |
| 161 |
return kem.ErrPubKeySize |
| 162 |
} |
| 163 |
return (*internal.PublicKey)(pk).UnpackMLKEM(buf) |
| 164 |
} |
| 165 |
|
| 166 |
// Unpacks sk from the given buffer. |
| 167 |
// |
| 168 |
// Panics if buf is not of length PrivateKeySize. |
| 169 |
func (sk *PrivateKey) Unpack(buf []byte) { |
| 170 |
if len(buf) != PrivateKeySize { |
| 171 |
panic("buf must be of size PrivateKeySize") |
| 172 |
} |
| 173 |
(*internal.PrivateKey)(sk).Unpack(buf) |
| 174 |
} |
| 175 |
|
| 176 |
// Returns whether the two private keys are equal. |
| 177 |
func (sk *PrivateKey) Equal(other *PrivateKey) bool { |
| 178 |
return (*internal.PrivateKey)(sk).Equal((*internal.PrivateKey)(other)) |
| 179 |
} |