|
| 1 | +package kem |
| 2 | + |
| 3 | +import ( |
| 4 | + "circl/dh/sidh" |
| 5 | + "circl/kem/schemes" |
| 6 | + "encoding/binary" |
| 7 | + "errors" |
| 8 | + "fmt" |
| 9 | + "io" |
| 10 | + |
| 11 | + "golang.org/x/crypto/curve25519" |
| 12 | +) |
| 13 | + |
| 14 | +// ID identifies each flavor of KEM. |
| 15 | +type ID uint16 |
| 16 | + |
| 17 | +const ( |
| 18 | + // KEM25519 is X25519 as a KEM. Not quantum-safe. |
| 19 | + KEM25519 ID = 0x01fb |
| 20 | + // Kyber512 is a post-quantum KEM based on MLWE |
| 21 | + Kyber512 ID = 0x01fc |
| 22 | + // SIKEp434 is a post-quantum KEM |
| 23 | + SIKEp434 ID = 0x01fd |
| 24 | + |
| 25 | + // minimum |
| 26 | + minKEM = KEM25519 |
| 27 | + // maximum |
| 28 | + maxKEM = SIKEp434 |
| 29 | +) |
| 30 | + |
| 31 | +// PrivateKey is a private key. |
| 32 | +type PrivateKey struct { |
| 33 | + KEMId ID |
| 34 | + PrivateKey []byte |
| 35 | +} |
| 36 | + |
| 37 | +// PublicKey is a public key. |
| 38 | +type PublicKey struct { |
| 39 | + KEMId ID |
| 40 | + PublicKey []byte |
| 41 | +} |
| 42 | + |
| 43 | +// MarshalBinary returns the byte representation of a public key. |
| 44 | +func (pubKey *PublicKey) MarshalBinary() ([]byte, error) { |
| 45 | + buf := make([]byte, 2+len(pubKey.PublicKey)) |
| 46 | + binary.LittleEndian.PutUint16(buf, uint16(pubKey.KEMId)) |
| 47 | + copy(buf[2:], pubKey.PublicKey) |
| 48 | + return buf, nil |
| 49 | +} |
| 50 | + |
| 51 | +// UnmarshalBinary produces a PublicKey from a byte array. |
| 52 | +func (pubKey *PublicKey) UnmarshalBinary(data []byte) error { |
| 53 | + id := ID(binary.LittleEndian.Uint16(data[:2])) |
| 54 | + if id < minKEM || id > maxKEM { |
| 55 | + return errors.New("Invalid KEM type") |
| 56 | + } |
| 57 | + |
| 58 | + pubKey.KEMId = id |
| 59 | + pubKey.PublicKey = data[2:] |
| 60 | + return nil |
| 61 | +} |
| 62 | + |
| 63 | +// GenerateKey generates a keypair for a given KEM. |
| 64 | +// It returns a public and private key. |
| 65 | +func GenerateKey(rand io.Reader, kemID ID) (*PublicKey, *PrivateKey, error) { |
| 66 | + switch kemID { |
| 67 | + case Kyber512: |
| 68 | + scheme := schemes.ByName("Kyber512") |
| 69 | + seed := make([]byte, scheme.SeedSize()) |
| 70 | + if _, err := io.ReadFull(rand, seed); err != nil { |
| 71 | + return nil, nil, err |
| 72 | + } |
| 73 | + publicKey, privateKey := scheme.DeriveKeyPair(seed) |
| 74 | + pk, _ := publicKey.MarshalBinary() |
| 75 | + sk, _ := privateKey.MarshalBinary() |
| 76 | + |
| 77 | + return &PublicKey{KEMId: kemID, PublicKey: pk}, &PrivateKey{KEMId: kemID, PrivateKey: sk}, nil |
| 78 | + case KEM25519: |
| 79 | + privateKey := make([]byte, curve25519.ScalarSize) |
| 80 | + if _, err := io.ReadFull(rand, privateKey); err != nil { |
| 81 | + return nil, nil, err |
| 82 | + } |
| 83 | + publicKey, err := curve25519.X25519(privateKey, curve25519.Basepoint) |
| 84 | + if err != nil { |
| 85 | + return nil, nil, err |
| 86 | + } |
| 87 | + return &PublicKey{KEMId: kemID, PublicKey: publicKey}, &PrivateKey{KEMId: kemID, PrivateKey: privateKey}, nil |
| 88 | + case SIKEp434: |
| 89 | + privateKey := sidh.NewPrivateKey(sidh.Fp434, sidh.KeyVariantSike) |
| 90 | + publicKey := sidh.NewPublicKey(sidh.Fp434, sidh.KeyVariantSike) |
| 91 | + if err := privateKey.Generate(rand); err != nil { |
| 92 | + return nil, nil, err |
| 93 | + } |
| 94 | + privateKey.GeneratePublicKey(publicKey) |
| 95 | + |
| 96 | + pubBytes := make([]byte, publicKey.Size()) |
| 97 | + privBytes := make([]byte, privateKey.Size()) |
| 98 | + publicKey.Export(pubBytes) |
| 99 | + privateKey.Export(privBytes) |
| 100 | + return &PublicKey{KEMId: kemID, PublicKey: pubBytes}, &PrivateKey{KEMId: kemID, PrivateKey: privBytes}, nil |
| 101 | + default: |
| 102 | + return nil, nil, fmt.Errorf("crypto/kem: internal error: unsupported KEM %d", kemID) |
| 103 | + } |
| 104 | + |
| 105 | +} |
| 106 | + |
| 107 | +// Encapsulate returns a shared secret and a ciphertext. |
| 108 | +func Encapsulate(rand io.Reader, pk *PublicKey) ([]byte, []byte, error) { |
| 109 | + switch pk.KEMId { |
| 110 | + case Kyber512: |
| 111 | + scheme := schemes.ByName("Kyber512") |
| 112 | + pub, err := scheme.UnmarshalBinaryPublicKey(pk.PublicKey) |
| 113 | + if err != nil { |
| 114 | + return nil, nil, err |
| 115 | + } |
| 116 | + |
| 117 | + seed := make([]byte, scheme.EncapsulationSeedSize()) |
| 118 | + if _, err := io.ReadFull(rand, seed); err != nil { |
| 119 | + return nil, nil, err |
| 120 | + } |
| 121 | + |
| 122 | + ct, ss, err := scheme.EncapsulateDeterministically(pub, seed) |
| 123 | + if err != nil { |
| 124 | + return nil, nil, err |
| 125 | + } |
| 126 | + |
| 127 | + return ss, ct, nil |
| 128 | + case KEM25519: |
| 129 | + privateKey := make([]byte, curve25519.ScalarSize) |
| 130 | + if _, err := io.ReadFull(rand, privateKey); err != nil { |
| 131 | + return nil, nil, err |
| 132 | + } |
| 133 | + ciphertext, err := curve25519.X25519(privateKey, curve25519.Basepoint) |
| 134 | + if err != nil { |
| 135 | + return nil, nil, err |
| 136 | + } |
| 137 | + sharedSecret, err := curve25519.X25519(privateKey, pk.PublicKey) |
| 138 | + if err != nil { |
| 139 | + return nil, nil, err |
| 140 | + } |
| 141 | + return sharedSecret, ciphertext, nil |
| 142 | + case SIKEp434: |
| 143 | + kem := sidh.NewSike434(rand) |
| 144 | + sikepk := sidh.NewPublicKey(sidh.Fp434, sidh.KeyVariantSike) |
| 145 | + err := sikepk.Import(pk.PublicKey) |
| 146 | + if err != nil { |
| 147 | + return nil, nil, err |
| 148 | + } |
| 149 | + |
| 150 | + ct := make([]byte, kem.CiphertextSize()) |
| 151 | + ss := make([]byte, kem.SharedSecretSize()) |
| 152 | + err = kem.Encapsulate(ct, ss, sikepk) |
| 153 | + if err != nil { |
| 154 | + return nil, nil, err |
| 155 | + } |
| 156 | + |
| 157 | + return ss, ct, nil |
| 158 | + default: |
| 159 | + return nil, nil, errors.New("crypto/kem: internal error: unsupported KEM in Encapsulate") |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +// Decapsulate generates the shared secret. |
| 164 | +func Decapsulate(privateKey *PrivateKey, ciphertext []byte) ([]byte, error) { |
| 165 | + switch privateKey.KEMId { |
| 166 | + case Kyber512: |
| 167 | + scheme := schemes.ByName("Kyber512") |
| 168 | + sk, err := scheme.UnmarshalBinaryPrivateKey(privateKey.PrivateKey) |
| 169 | + if err != nil { |
| 170 | + return nil, err |
| 171 | + } |
| 172 | + if len(ciphertext) != scheme.CiphertextSize() { |
| 173 | + return nil, fmt.Errorf("crypto/kem: ciphertext is of len %d, expected %d", len(ciphertext), scheme.CiphertextSize()) |
| 174 | + } |
| 175 | + ss, err := scheme.Decapsulate(sk, ciphertext) |
| 176 | + if err != nil { |
| 177 | + return nil, err |
| 178 | + } |
| 179 | + |
| 180 | + return ss, nil |
| 181 | + case KEM25519: |
| 182 | + sharedSecret, err := curve25519.X25519(privateKey.PrivateKey, ciphertext) |
| 183 | + if err != nil { |
| 184 | + return nil, err |
| 185 | + } |
| 186 | + return sharedSecret, nil |
| 187 | + case SIKEp434: |
| 188 | + kem := sidh.NewSike434(nil) |
| 189 | + sikesk := sidh.NewPrivateKey(sidh.Fp434, sidh.KeyVariantSike) |
| 190 | + err := sikesk.Import(privateKey.PrivateKey) |
| 191 | + if err != nil { |
| 192 | + return nil, err |
| 193 | + } |
| 194 | + |
| 195 | + sikepk := sidh.NewPublicKey(sidh.Fp434, sidh.KeyVariantSike) |
| 196 | + sikesk.GeneratePublicKey(sikepk) |
| 197 | + ss := make([]byte, kem.SharedSecretSize()) |
| 198 | + err = kem.Decapsulate(ss, sikesk, sikepk, ciphertext) |
| 199 | + if err != nil { |
| 200 | + return nil, err |
| 201 | + } |
| 202 | + |
| 203 | + return ss, nil |
| 204 | + default: |
| 205 | + return nil, errors.New("crypto/kem: internal error: unsupported KEM in Decapsulate") |
| 206 | + } |
| 207 | +} |
0 commit comments