github.com/cloudflare/circl@v1.5.0/sign/dilithium/templates/pkg.templ.go (about) 1 // +build ignore 2 // The previous line (and this one up to the warning below) is removed by the 3 // template generator. 4 5 // Code generated from pkg.templ.go. DO NOT EDIT. 6 7 {{ if .NIST }} 8 // {{.Pkg}} implements NIST signature scheme {{.Name}} as defined in FIPS204. 9 {{- else }} 10 // {{.Pkg}} implements the CRYSTALS-Dilithium signature scheme {{.Name}} 11 // as submitted to round3 of the NIST PQC competition and described in 12 // 13 // https://pq-crystals.org/dilithium/data/dilithium-specification-round3-20210208.pdf 14 {{- end }} 15 package {{.Pkg}} 16 17 import ( 18 "crypto" 19 "errors" 20 "io" 21 22 {{- if .NIST }} 23 cryptoRand "crypto/rand" 24 {{- end }} 25 26 "github.com/cloudflare/circl/sign" 27 28 {{- if .NIST }} 29 "github.com/cloudflare/circl/sign/mldsa/{{.Pkg}}/internal" 30 {{- else }} 31 "github.com/cloudflare/circl/sign/dilithium/{{.Pkg}}/internal" 32 {{- end }} 33 common "github.com/cloudflare/circl/sign/internal/dilithium" 34 ) 35 36 const ( 37 // Size of seed for NewKeyFromSeed 38 SeedSize = common.SeedSize 39 40 // Size of a packed PublicKey 41 PublicKeySize = internal.PublicKeySize 42 43 // Size of a packed PrivateKey 44 PrivateKeySize = internal.PrivateKeySize 45 46 // Size of a signature 47 SignatureSize = internal.SignatureSize 48 ) 49 50 // PublicKey is the type of {{.Name}} public key 51 type PublicKey internal.PublicKey 52 53 // PrivateKey is the type of {{.Name}} private key 54 type PrivateKey internal.PrivateKey 55 56 // GenerateKey generates a public/private key pair using entropy from rand. 57 // If rand is nil, crypto/rand.Reader will be used. 58 func GenerateKey(rand io.Reader) (*PublicKey, *PrivateKey, error) { 59 pk, sk, err := internal.GenerateKey(rand) 60 return (*PublicKey)(pk), (*PrivateKey)(sk), err 61 } 62 63 // NewKeyFromSeed derives a public/private key pair using the given seed. 64 func NewKeyFromSeed(seed *[SeedSize]byte) (*PublicKey, *PrivateKey) { 65 pk, sk := internal.NewKeyFromSeed(seed) 66 return (*PublicKey)(pk), (*PrivateKey)(sk) 67 } 68 69 // SignTo signs the given message and writes the signature into signature. 70 // It will panic if signature is not of length at least SignatureSize. 71 {{- if .NIST }} 72 // 73 // ctx is the optional context string. Errors if ctx is larger than 255 bytes. 74 // A nil context string is equivalent to an empty context string. 75 func SignTo(sk *PrivateKey, msg, ctx []byte, randomized bool, sig []byte) error { 76 {{- else }} 77 func SignTo(sk *PrivateKey, msg, sig []byte) { 78 {{- end }} 79 var rnd [32]byte 80 81 {{- if .NIST }} 82 if randomized { 83 _, err := cryptoRand.Read(rnd[:]) 84 if err != nil { 85 return err 86 } 87 } 88 89 if len(ctx) > 255 { 90 return sign.ErrContextTooLong 91 } 92 {{- end }} 93 94 internal.SignTo( 95 (*internal.PrivateKey)(sk), 96 func (w io.Writer) { 97 {{- if .NIST }} 98 _, _ = w.Write([]byte{0}) 99 _, _ = w.Write([]byte{byte(len(ctx))}) 100 101 if ctx != nil { 102 _, _ = w.Write(ctx) 103 } 104 105 {{- end }} 106 w.Write(msg) 107 }, 108 rnd, 109 sig, 110 ) 111 112 {{- if .NIST }} 113 return nil 114 {{- end }} 115 } 116 117 {{- if .NIST }} 118 // Do not use. Implements ML-DSA.Sign_internal used for compatibility tests. 119 func (sk *PrivateKey) unsafeSignInternal(msg []byte, rnd [32]byte) []byte { 120 var ret [SignatureSize]byte 121 internal.SignTo( 122 (*internal.PrivateKey)(sk), 123 func (w io.Writer) { 124 _, _ = w.Write(msg) 125 }, 126 rnd, 127 ret[:], 128 ) 129 return ret[:] 130 } 131 132 // Do not use. Implements ML-DSA.Verify_internal used for compatibility tests. 133 func unsafeVerifyInternal(pk *PublicKey, msg, sig []byte) bool { 134 return internal.Verify( 135 (*internal.PublicKey)(pk), 136 func(w io.Writer) { 137 _, _ = w.Write(msg) 138 }, 139 sig, 140 ) 141 } 142 {{- end }} 143 144 // Verify checks whether the given signature by pk on msg is valid. 145 {{- if .NIST }} 146 // 147 // ctx is the optional context string. Fails if ctx is larger than 255 bytes. 148 // A nil context string is equivalent to an empty context string. 149 func Verify(pk *PublicKey, msg, ctx, sig []byte) bool { 150 if len(ctx) > 255 { 151 return false 152 } 153 {{- else }} 154 func Verify(pk *PublicKey, msg, sig []byte) bool { 155 {{- end }} 156 return internal.Verify( 157 (*internal.PublicKey)(pk), 158 func (w io.Writer) { 159 {{- if .NIST }} 160 _, _ = w.Write([]byte{0}) 161 _, _ = w.Write([]byte{byte(len(ctx))}) 162 163 if ctx != nil { 164 _, _ = w.Write(ctx) 165 } 166 167 {{- end }} 168 _, _ = w.Write(msg) 169 }, 170 sig, 171 ) 172 } 173 174 // Sets pk to the public key encoded in buf. 175 func (pk *PublicKey) Unpack(buf *[PublicKeySize]byte) { 176 (*internal.PublicKey)(pk).Unpack(buf) 177 } 178 179 // Sets sk to the private key encoded in buf. 180 func (sk *PrivateKey) Unpack(buf *[PrivateKeySize]byte) { 181 (*internal.PrivateKey)(sk).Unpack(buf) 182 } 183 184 // Packs the public key into buf. 185 func (pk *PublicKey) Pack(buf *[PublicKeySize]byte) { 186 (*internal.PublicKey)(pk).Pack(buf) 187 } 188 189 // Packs the private key into buf. 190 func (sk *PrivateKey) Pack(buf *[PrivateKeySize]byte) { 191 (*internal.PrivateKey)(sk).Pack(buf) 192 } 193 194 // Packs the public key. 195 func (pk *PublicKey) Bytes() []byte { 196 var buf [PublicKeySize]byte 197 pk.Pack(&buf) 198 return buf[:] 199 } 200 201 // Packs the private key. 202 func (sk *PrivateKey) Bytes() []byte { 203 var buf [PrivateKeySize]byte 204 sk.Pack(&buf) 205 return buf[:] 206 } 207 208 // Packs the public key. 209 func (pk *PublicKey) MarshalBinary() ([]byte, error) { 210 return pk.Bytes(), nil 211 } 212 213 // Packs the private key. 214 func (sk *PrivateKey) MarshalBinary() ([]byte, error) { 215 return sk.Bytes(), nil 216 } 217 218 // Unpacks the public key from data. 219 func (pk *PublicKey) UnmarshalBinary(data []byte) error { 220 if len(data) != PublicKeySize { 221 return errors.New("packed public key must be of {{.Pkg}}.PublicKeySize bytes") 222 } 223 var buf [PublicKeySize]byte 224 copy(buf[:], data) 225 pk.Unpack(&buf) 226 return nil 227 } 228 229 // Unpacks the private key from data. 230 func (sk *PrivateKey) UnmarshalBinary(data []byte) error { 231 if len(data) != PrivateKeySize { 232 return errors.New("packed private key must be of {{.Pkg}}.PrivateKeySize bytes") 233 } 234 var buf [PrivateKeySize]byte 235 copy(buf[:], data) 236 sk.Unpack(&buf) 237 return nil 238 } 239 240 // Sign signs the given message. 241 // 242 // opts.HashFunc() must return zero, which can be achieved by passing 243 // crypto.Hash(0) for opts. rand is ignored. Will only return an error 244 // if opts.HashFunc() is non-zero. 245 // 246 // This function is used to make PrivateKey implement the crypto.Signer 247 // interface. The package-level SignTo function might be more convenient 248 // to use. 249 func (sk *PrivateKey) Sign(rand io.Reader, msg []byte, opts crypto.SignerOpts) ( 250 sig []byte, err error) { 251 var ret [SignatureSize]byte 252 253 if opts.HashFunc() != crypto.Hash(0) { 254 return nil, errors.New("dilithium: cannot sign hashed message") 255 } 256 257 {{- if .NIST }} 258 if err = SignTo(sk, msg, nil, false, ret[:]); err != nil { 259 return nil, err 260 } 261 {{- else }} 262 SignTo(sk, msg, ret[:]) 263 {{- end }} 264 265 return ret[:], nil 266 } 267 268 // Computes the public key corresponding to this private key. 269 // 270 // Returns a *PublicKey. The type crypto.PublicKey is used to make 271 // PrivateKey implement the crypto.Signer interface. 272 func (sk *PrivateKey) Public() crypto.PublicKey { 273 return (*PublicKey)((*internal.PrivateKey)(sk).Public()) 274 } 275 276 // Equal returns whether the two private keys equal. 277 func (sk *PrivateKey) Equal(other crypto.PrivateKey) bool { 278 castOther, ok := other.(*PrivateKey) 279 if !ok { 280 return false 281 } 282 return (*internal.PrivateKey)(sk).Equal((*internal.PrivateKey)(castOther)) 283 } 284 285 // Equal returns whether the two public keys equal. 286 func (pk *PublicKey) Equal(other crypto.PublicKey) bool { 287 castOther, ok := other.(*PublicKey) 288 if !ok { 289 return false 290 } 291 return (*internal.PublicKey)(pk).Equal((*internal.PublicKey)(castOther)) 292 } 293 294 295 // Boilerplate for generic signatures API 296 297 type scheme struct{} 298 var sch sign.Scheme = &scheme{} 299 300 // Scheme returns a generic signature interface for {{ .Name }}. 301 func Scheme() sign.Scheme { return sch } 302 303 func (*scheme) Name() string { return "{{ .Name }}" } 304 func (*scheme) PublicKeySize() int { return PublicKeySize } 305 func (*scheme) PrivateKeySize() int { return PrivateKeySize } 306 func (*scheme) SignatureSize() int { return SignatureSize } 307 func (*scheme) SeedSize() int { return SeedSize } 308 // TODO TLSIdentifier() and OID() 309 310 func (*scheme) SupportsContext() bool { 311 {{- if .NIST }} 312 return true 313 {{- else }} 314 return false 315 {{- end }} 316 } 317 318 func (*scheme) GenerateKey() (sign.PublicKey, sign.PrivateKey, error) { 319 return GenerateKey(nil) 320 } 321 322 func (*scheme) Sign( 323 sk sign.PrivateKey, 324 msg []byte, 325 opts *sign.SignatureOpts, 326 ) []byte { 327 {{- if .NIST }} 328 var ctx []byte 329 {{- end }} 330 sig := make([]byte, SignatureSize) 331 332 priv, ok := sk.(*PrivateKey) 333 if !ok { 334 panic(sign.ErrTypeMismatch) 335 } 336 if opts != nil && opts.Context != "" { 337 {{- if .NIST }} 338 ctx = []byte(opts.Context) 339 {{- else }} 340 panic(sign.ErrContextNotSupported) 341 {{- end }} 342 } 343 344 {{- if .NIST }} 345 err := SignTo(priv, msg, ctx, false, sig) 346 if err != nil { 347 panic(err) 348 } 349 {{- else }} 350 SignTo(priv, msg, sig) 351 {{ end }} 352 353 return sig 354 } 355 356 func (*scheme) Verify( 357 pk sign.PublicKey, 358 msg, sig []byte, 359 opts *sign.SignatureOpts, 360 ) bool { 361 {{- if .NIST }} 362 var ctx []byte 363 {{- end }} 364 pub, ok := pk.(*PublicKey) 365 if !ok { 366 panic(sign.ErrTypeMismatch) 367 } 368 if opts != nil && opts.Context != "" { 369 {{- if .NIST }} 370 ctx = []byte(opts.Context) 371 {{- else }} 372 panic(sign.ErrContextNotSupported) 373 {{- end }} 374 } 375 {{- if .NIST }} 376 return Verify(pub, msg, ctx, sig) 377 {{- else }} 378 return Verify(pub, msg, sig) 379 {{- end }} 380 } 381 382 func (*scheme) DeriveKey(seed []byte) (sign.PublicKey, sign.PrivateKey) { 383 if len(seed) != SeedSize { 384 panic(sign.ErrSeedSize) 385 } 386 var seed2 [SeedSize]byte 387 copy(seed2[:], seed) 388 return NewKeyFromSeed(&seed2) 389 } 390 391 func (*scheme) UnmarshalBinaryPublicKey(buf []byte) (sign.PublicKey, error) { 392 if len(buf) != PublicKeySize { 393 return nil, sign.ErrPubKeySize 394 } 395 396 var ( 397 buf2 [PublicKeySize]byte 398 ret PublicKey 399 ) 400 401 copy(buf2[:], buf) 402 ret.Unpack(&buf2) 403 return &ret, nil 404 } 405 406 func (*scheme) UnmarshalBinaryPrivateKey(buf []byte) (sign.PrivateKey, error) { 407 if len(buf) != PrivateKeySize { 408 return nil, sign.ErrPrivKeySize 409 } 410 411 var ( 412 buf2 [PrivateKeySize]byte 413 ret PrivateKey 414 ) 415 416 copy(buf2[:], buf) 417 ret.Unpack(&buf2) 418 return &ret, nil 419 } 420 421 func (sk *PrivateKey) Scheme() sign.Scheme { 422 return sch 423 } 424 425 func (sk *PublicKey) Scheme() sign.Scheme { 426 return sch 427 }