...
1
2
3
4
5
6
7
8
9
10
11
12 package {{.Pkg}}
13
14 import (
15 cryptoRand "crypto/rand"
16 "io"
17
18 "github.com/cloudflare/circl/pke/kyber/{{.Pkg}}/internal"
19 )
20
21 const (
22
23 KeySeedSize = internal.SeedSize
24
25
26 EncryptionSeedSize = internal.SeedSize
27
28
29 PublicKeySize = internal.PublicKeySize
30
31
32 PrivateKeySize = internal.PrivateKeySize
33
34
35 CiphertextSize = internal.CiphertextSize
36
37
38 PlaintextSize = internal.PlaintextSize
39 )
40
41
42 type PublicKey internal.PublicKey
43
44
45 type PrivateKey internal.PrivateKey
46
47
48
49 func GenerateKey(rand io.Reader) (*PublicKey, *PrivateKey, error) {
50 var seed [KeySeedSize]byte
51 if rand == nil {
52 rand = cryptoRand.Reader
53 }
54 _, err := io.ReadFull(rand, seed[:])
55 if err != nil {
56 return nil, nil, err
57 }
58 pk, sk := internal.NewKeyFromSeed(seed[:])
59 return (*PublicKey)(pk), (*PrivateKey)(sk), nil
60 }
61
62
63
64
65 func NewKeyFromSeed(seed []byte) (*PublicKey, *PrivateKey) {
66 if len(seed) != KeySeedSize {
67 panic("seed must be of length KeySeedSize")
68 }
69 pk, sk := internal.NewKeyFromSeed(seed)
70 return (*PublicKey)(pk), (*PrivateKey)(sk)
71 }
72
73
74
75
76
77
78 func (pk *PublicKey) EncryptTo(ct []byte, pt []byte, seed []byte) {
79 if len(pt) != PlaintextSize {
80 panic("pt must be of length PlaintextSize")
81 }
82 if len(ct) != CiphertextSize {
83 panic("ct must be of length CiphertextSize")
84 }
85 if len(seed) != EncryptionSeedSize {
86 panic("seed must be of length EncryptionSeedSize")
87 }
88 (*internal.PublicKey)(pk).EncryptTo(ct, pt, seed)
89 }
90
91
92
93
94
95
96 func (sk *PrivateKey) DecryptTo(pt []byte, ct []byte) {
97 if len(pt) != PlaintextSize {
98 panic("pt must be of length PlaintextSize")
99 }
100 if len(ct) != CiphertextSize {
101 panic("ct must be of length CiphertextSize")
102 }
103 (*internal.PrivateKey)(sk).DecryptTo(pt, ct)
104 }
105
106
107
108
109 func (pk *PublicKey) Pack(buf []byte) {
110 if len(buf) != PublicKeySize {
111 panic("buf must be of size PublicKeySize")
112 }
113 (*internal.PublicKey)(pk).Pack(buf)
114 }
115
116
117
118
119 func (sk *PrivateKey) Pack(buf []byte) {
120 if len(buf) != PrivateKeySize {
121 panic("buf must be of size PrivateKeySize")
122 }
123 (*internal.PrivateKey)(sk).Pack(buf)
124 }
125
126
127
128
129 func (pk *PublicKey) Unpack(buf []byte) {
130 if len(buf) != PublicKeySize {
131 panic("buf must be of size PublicKeySize")
132 }
133 (*internal.PublicKey)(pk).Unpack(buf)
134 }
135
136
137
138
139 func (sk *PrivateKey) Unpack(buf []byte) {
140 if len(buf) != PrivateKeySize {
141 panic("buf must be of size PrivateKeySize")
142 }
143 (*internal.PrivateKey)(sk).Unpack(buf)
144 }
145
146
147 func (sk *PrivateKey) Equal(other *PrivateKey) bool {
148 return (*internal.PrivateKey)(sk).Equal((*internal.PrivateKey)(other))
149 }
150
View as plain text