...
1
2
3
4
5
6
7 package mode3aes
8
9 import (
10 "crypto"
11 "errors"
12 "io"
13
14 "github.com/cloudflare/circl/sign/dilithium/internal/common"
15 "github.com/cloudflare/circl/sign/dilithium/mode3aes/internal"
16 )
17
18 const (
19
20 SeedSize = common.SeedSize
21
22
23 PublicKeySize = internal.PublicKeySize
24
25
26 PrivateKeySize = internal.PrivateKeySize
27
28
29 SignatureSize = internal.SignatureSize
30 )
31
32
33 type PublicKey internal.PublicKey
34
35
36 type PrivateKey internal.PrivateKey
37
38
39
40 func GenerateKey(rand io.Reader) (*PublicKey, *PrivateKey, error) {
41 pk, sk, err := internal.GenerateKey(rand)
42 return (*PublicKey)(pk), (*PrivateKey)(sk), err
43 }
44
45
46 func NewKeyFromSeed(seed *[SeedSize]byte) (*PublicKey, *PrivateKey) {
47 pk, sk := internal.NewKeyFromSeed(seed)
48 return (*PublicKey)(pk), (*PrivateKey)(sk)
49 }
50
51
52
53 func SignTo(sk *PrivateKey, msg []byte, signature []byte) {
54 internal.SignTo(
55 (*internal.PrivateKey)(sk),
56 msg,
57 signature,
58 )
59 }
60
61
62 func Verify(pk *PublicKey, msg []byte, signature []byte) bool {
63 return internal.Verify(
64 (*internal.PublicKey)(pk),
65 msg,
66 signature,
67 )
68 }
69
70
71 func (pk *PublicKey) Unpack(buf *[PublicKeySize]byte) {
72 (*internal.PublicKey)(pk).Unpack(buf)
73 }
74
75
76 func (sk *PrivateKey) Unpack(buf *[PrivateKeySize]byte) {
77 (*internal.PrivateKey)(sk).Unpack(buf)
78 }
79
80
81 func (pk *PublicKey) Pack(buf *[PublicKeySize]byte) {
82 (*internal.PublicKey)(pk).Pack(buf)
83 }
84
85
86 func (sk *PrivateKey) Pack(buf *[PrivateKeySize]byte) {
87 (*internal.PrivateKey)(sk).Pack(buf)
88 }
89
90
91 func (pk *PublicKey) Bytes() []byte {
92 var buf [PublicKeySize]byte
93 pk.Pack(&buf)
94 return buf[:]
95 }
96
97
98 func (sk *PrivateKey) Bytes() []byte {
99 var buf [PrivateKeySize]byte
100 sk.Pack(&buf)
101 return buf[:]
102 }
103
104
105 func (pk *PublicKey) MarshalBinary() ([]byte, error) {
106 return pk.Bytes(), nil
107 }
108
109
110 func (sk *PrivateKey) MarshalBinary() ([]byte, error) {
111 return sk.Bytes(), nil
112 }
113
114
115 func (pk *PublicKey) UnmarshalBinary(data []byte) error {
116 if len(data) != PublicKeySize {
117 return errors.New("packed public key must be of mode3aes.PublicKeySize bytes")
118 }
119 var buf [PublicKeySize]byte
120 copy(buf[:], data)
121 pk.Unpack(&buf)
122 return nil
123 }
124
125
126 func (sk *PrivateKey) UnmarshalBinary(data []byte) error {
127 if len(data) != PrivateKeySize {
128 return errors.New("packed private key must be of mode3aes.PrivateKeySize bytes")
129 }
130 var buf [PrivateKeySize]byte
131 copy(buf[:], data)
132 sk.Unpack(&buf)
133 return nil
134 }
135
136
137
138
139
140
141
142
143
144
145 func (sk *PrivateKey) Sign(rand io.Reader, msg []byte, opts crypto.SignerOpts) (
146 signature []byte, err error) {
147 var sig [SignatureSize]byte
148
149 if opts.HashFunc() != crypto.Hash(0) {
150 return nil, errors.New("dilithium: cannot sign hashed message")
151 }
152
153 SignTo(sk, msg, sig[:])
154 return sig[:], nil
155 }
156
157
158
159
160
161 func (sk *PrivateKey) Public() crypto.PublicKey {
162 return (*PublicKey)((*internal.PrivateKey)(sk).Public())
163 }
164
165
166 func (sk *PrivateKey) Equal(other crypto.PrivateKey) bool {
167 castOther, ok := other.(*PrivateKey)
168 if !ok {
169 return false
170 }
171 return (*internal.PrivateKey)(sk).Equal((*internal.PrivateKey)(castOther))
172 }
173
174
175 func (pk *PublicKey) Equal(other crypto.PublicKey) bool {
176 castOther, ok := other.(*PublicKey)
177 if !ok {
178 return false
179 }
180 return (*internal.PublicKey)(pk).Equal((*internal.PublicKey)(castOther))
181 }
182
View as plain text