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