mirror of
https://github.com/golang/go
synced 2024-11-23 15:30:05 -07:00
crypto/ed25519: improve Ed25519ctx error for oversized contexts
Previously if PrivateKey.Sign was called for Ed25519ctx with a context longer than 255 bytes, the error message would mention Ed25519ph. For Ed25519ph, the order of message length vs context length errors now matches VerifyWithOptions. A message length error will be surfaced in preference to a context length error. It also preferences hash errors ahead of context length errors which also matches the behaviour of VerifyWithOptions. Change-Id: Iae380b3d879e0a9877ea057806fcd1e0ef7f7376 Reviewed-on: https://go-review.googlesource.com/c/go/+/473595 Run-TryBot: Filippo Valsorda <filippo@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Carlos Amedee <carlos@golang.org> Reviewed-by: Roland Shoemaker <roland@golang.org> Auto-Submit: Filippo Valsorda <filippo@golang.org> Reviewed-by: Filippo Valsorda <filippo@golang.org>
This commit is contained in:
parent
c6cdfdabef
commit
84609d874e
@ -90,18 +90,21 @@ func (priv PrivateKey) Sign(rand io.Reader, message []byte, opts crypto.SignerOp
|
||||
if opts, ok := opts.(*Options); ok {
|
||||
context = opts.Context
|
||||
}
|
||||
if l := len(context); l > 255 {
|
||||
return nil, errors.New("ed25519: bad Ed25519ph context length: " + strconv.Itoa(l))
|
||||
}
|
||||
switch {
|
||||
case hash == crypto.SHA512: // Ed25519ph
|
||||
if l := len(message); l != sha512.Size {
|
||||
return nil, errors.New("ed25519: bad Ed25519ph message hash length: " + strconv.Itoa(l))
|
||||
}
|
||||
if l := len(context); l > 255 {
|
||||
return nil, errors.New("ed25519: bad Ed25519ph context length: " + strconv.Itoa(l))
|
||||
}
|
||||
signature := make([]byte, SignatureSize)
|
||||
sign(signature, priv, message, domPrefixPh, context)
|
||||
return signature, nil
|
||||
case hash == crypto.Hash(0) && context != "": // Ed25519ctx
|
||||
if l := len(context); l > 255 {
|
||||
return nil, errors.New("ed25519: bad Ed25519ctx context length: " + strconv.Itoa(l))
|
||||
}
|
||||
signature := make([]byte, SignatureSize)
|
||||
sign(signature, priv, message, domPrefixCtx, context)
|
||||
return signature, nil
|
||||
|
Loading…
Reference in New Issue
Block a user