-
Notifications
You must be signed in to change notification settings - Fork 0
/
suite.go
112 lines (94 loc) · 3.12 KB
/
suite.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
// SPDX-FileCopyrightText: 2023 Thibault NORMAND <me@zenithar.org>
//
// SPDX-License-Identifier: Apache-2.0 AND MIT
package hpke
import (
"crypto/ecdh"
"encoding/binary"
"zntr.io/crypto/hpke/aead"
"zntr.io/crypto/hpke/kdf"
"zntr.io/crypto/hpke/kem"
)
// Suite repesents a HPKE cipher suite contract.
type Suite interface {
IsValid() bool
Params() (kem.KEM, kdf.KDF, aead.AEAD)
KEM() kem.Scheme
AEAD() aead.AEAD
Sender(pkR *ecdh.PublicKey, info []byte) Sender
Receiver(skR *ecdh.PrivateKey, info []byte) Receiver
}
// New initializes a new HPKE suite.
func New(kemID kem.KEM, kdfID kdf.KDF, aeadID aead.AEAD) Suite {
return &cipherSuite{
kemID: kemID,
kdfID: kdfID,
aeadID: aeadID,
}
}
// Suite represents HPKE suite parameters.
type cipherSuite struct {
kemID kem.KEM
kdfID kdf.KDF
aeadID aead.AEAD
}
// IsValid checks if the suite is initialized with valid values.
func (s *cipherSuite) IsValid() bool {
return s.kemID.IsValid() && s.kdfID.IsValid() && s.aeadID.IsValid()
}
// KEM returns the associated KEM algorithm.
func (s *cipherSuite) KEM() kem.Scheme {
return s.kemID.Scheme()
}
// AEAD returns the associated AEAD algorithm.
func (s *cipherSuite) AEAD() aead.AEAD {
return s.aeadID
}
// SuiteID returns the public suite identifier used for material derivation.
func (s *cipherSuite) suiteID() []byte {
var out [10]byte
// suite_id = concat("HPKE", I2OSP(kem_id, 2), ISOSP(kdf_id, 2), ISOSP(aead_id, 2))
out[0], out[1], out[2], out[3] = 'H', 'P', 'K', 'E'
binary.BigEndian.PutUint16(out[4:6], uint16(s.kemID))
binary.BigEndian.PutUint16(out[6:8], uint16(s.kdfID))
binary.BigEndian.PutUint16(out[8:10], uint16(s.aeadID))
return out[:]
}
// Params returns suite parameters.
func (s *cipherSuite) Params() (kem.KEM, kdf.KDF, aead.AEAD) {
return s.kemID, s.kdfID, s.aeadID
}
// Sender returns a message sender context builder.
func (s *cipherSuite) Sender(pkR *ecdh.PublicKey, info []byte) Sender {
return &sender{
cipherSuite: s,
pkR: pkR,
info: info,
}
}
// Receiver returns a message receiver context builder.
func (s *cipherSuite) Receiver(skR *ecdh.PrivateKey, info []byte) Receiver {
return &receiver{
cipherSuite: s,
skR: skR,
info: info,
}
}
// -----------------------------------------------------------------------------
func (s *cipherSuite) labeledExtract(salt, label, ikm []byte) []byte {
// labeled_ikm = concat("HPKE-v1", suite_id, label, ikm)
labeledIKM := append([]byte("HPKE-v1"), s.suiteID()...)
labeledIKM = append(labeledIKM, label...)
labeledIKM = append(labeledIKM, ikm...)
return s.kdfID.Extract(labeledIKM, salt)
}
func (s *cipherSuite) labeledExpand(prk, label, info []byte, outputLen uint16) ([]byte, error) {
labeledInfo := make([]byte, 2, 2+7+10+len(label)+len(info))
// labeled_info = concat(I2OSP(L, 2), "HPKE-v1", suite_id, label, info)
binary.BigEndian.PutUint16(labeledInfo[0:2], outputLen)
labeledInfo = append(labeledInfo, []byte("HPKE-v1")...)
labeledInfo = append(labeledInfo, s.suiteID()...)
labeledInfo = append(labeledInfo, label...)
labeledInfo = append(labeledInfo, info...)
return s.kdfID.Expand(prk, labeledInfo, outputLen)
}