forked from cloudflare/circl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshortkem.go
151 lines (124 loc) · 3.63 KB
/
shortkem.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package hpke
import (
"crypto/ecdh"
"crypto/rand"
"fmt"
"github.com/cloudflare/circl/kem"
)
type shortKEM struct {
dhKemBase
ecdh.Curve
}
func (s shortKEM) PrivateKeySize() int { return s.byteSize() }
func (s shortKEM) SeedSize() int { return s.byteSize() }
func (s shortKEM) CiphertextSize() int { return 1 + 2*s.byteSize() }
func (s shortKEM) PublicKeySize() int { return 1 + 2*s.byteSize() }
func (s shortKEM) EncapsulationSeedSize() int { return s.byteSize() }
func (s shortKEM) byteSize() int {
var bits int
switch s.Curve {
case ecdh.P256():
bits = 256
case ecdh.P384():
bits = 384
case ecdh.P521():
bits = 521
default:
panic(ErrInvalidKEM)
}
return (bits + 7) / 8
}
func (s shortKEM) sizeDH() int { return s.byteSize() }
func (s shortKEM) calcDH(dh []byte, sk kem.PrivateKey, pk kem.PublicKey) error {
PK, ok := pk.(*shortKEMPubKey)
if !ok {
return ErrInvalidKEMPublicKey
}
SK, ok := sk.(*shortKEMPrivKey)
if !ok {
return ErrInvalidKEMPrivateKey
}
x, err := SK.priv.ECDH(&PK.pub)
if err != nil {
return err
}
copy(dh, x)
return nil
}
// Deterministically derives a keypair from a seed. If you're unsure,
// you're better off using GenerateKey().
//
// Panics if seed is not of length SeedSize().
func (s shortKEM) DeriveKeyPair(seed []byte) (kem.PublicKey, kem.PrivateKey) {
// Implementation based on
// https://www.ietf.org/archive/id/draft-irtf-cfrg-hpke-07.html#name-derivekeypair
if len(seed) != s.SeedSize() {
panic(kem.ErrSeedSize)
}
bitmask := byte(0xFF)
if s.Curve == ecdh.P521() {
bitmask = 0x01
}
dkpPrk := s.labeledExtract([]byte(""), []byte("dkp_prk"), seed)
for ctr := 0; ctr <= 255; ctr++ {
bytes := s.labeledExpand(
dkpPrk,
[]byte("candidate"),
[]byte{byte(ctr)},
uint16(s.byteSize()),
)
bytes[0] &= bitmask
sk, err := s.UnmarshalBinaryPrivateKey(bytes)
if err == nil {
return sk.Public(), sk
}
}
panic(ErrInvalidKEMDeriveKey)
}
func (s shortKEM) GenerateKeyPair() (kem.PublicKey, kem.PrivateKey, error) {
key, err := s.Curve.GenerateKey(rand.Reader)
if err != nil {
return nil, nil, err
}
sk := &shortKEMPrivKey{s, key}
return sk.Public(), sk, err
}
func (s shortKEM) UnmarshalBinaryPrivateKey(data []byte) (kem.PrivateKey, error) {
key, err := s.Curve.NewPrivateKey(data)
if err != nil {
return nil, err
}
return &shortKEMPrivKey{s, key}, nil
}
func (s shortKEM) UnmarshalBinaryPublicKey(data []byte) (kem.PublicKey, error) {
key, err := s.Curve.NewPublicKey(data)
if err != nil {
return nil, err
}
return &shortKEMPubKey{s, *key}, nil
}
type shortKEMPubKey struct {
scheme shortKEM
pub ecdh.PublicKey
}
func (k *shortKEMPubKey) String() string { return fmt.Sprintf("%x", k.pub.Bytes()) }
func (k *shortKEMPubKey) Scheme() kem.Scheme { return k.scheme }
func (k *shortKEMPubKey) MarshalBinary() ([]byte, error) { return k.pub.Bytes(), nil }
func (k *shortKEMPubKey) Equal(pk kem.PublicKey) bool {
k1, ok := pk.(*shortKEMPubKey)
return ok && k.scheme == k1.scheme && k.pub.Equal(&k1.pub)
}
type shortKEMPrivKey struct {
scheme shortKEM
priv *ecdh.PrivateKey
}
func (k *shortKEMPrivKey) String() string { return fmt.Sprintf("%x", k.priv.Bytes()) }
func (k *shortKEMPrivKey) Scheme() kem.Scheme { return k.scheme }
func (k *shortKEMPrivKey) MarshalBinary() ([]byte, error) { return k.priv.Bytes(), nil }
func (k *shortKEMPrivKey) Equal(pk kem.PrivateKey) bool {
k1, ok := pk.(*shortKEMPrivKey)
return ok && k.scheme == k1.scheme && k.priv.Equal(k1.priv)
}
func (k *shortKEMPrivKey) Public() kem.PublicKey {
return &shortKEMPubKey{k.scheme, *k.priv.PublicKey()}
}