blob: da9a8a829835d5bbd2ce026839227562a3334928 [file] [log] [blame]
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////////
// Package signature provides subtle implementations of the Signer and Verifier primitives.
package signature
import (
"errors"
"fmt"
"math/big"
)
var errUnsupportedEncoding = errors.New("ecdsa: unsupported encoding")
// ECDSASignature is a struct holding r and s values of an ECDSA signature.
type ECDSASignature struct {
R, S *big.Int
}
// NewECDSASignature creates a new ecdsaSignature object.
func NewECDSASignature(r, s *big.Int) *ECDSASignature {
return &ECDSASignature{R: r, S: s}
}
// EncodeECDSASignature converts the signature to the given encoding format.
// Only DER encoding is supported now.
func (sig *ECDSASignature) EncodeECDSASignature(encoding string) ([]byte, error) {
switch encoding {
case "DER":
enc, err := asn1encode(sig)
if err != nil {
return nil, fmt.Errorf("ecdsa: can't convert ECDSA signature to %s encoding: %v", encoding, err)
}
return enc, nil
default:
return nil, errUnsupportedEncoding
}
}
// DecodeECDSASignature creates a new ECDSA signature using the given byte slice.
// The function assumes that the byte slice is the concatenation of the BigEndian
// representation of two big integer r and s.
func DecodeECDSASignature(encodedBytes []byte,
encoding string) (*ECDSASignature, error) {
switch encoding {
case "DER":
sig, err := asn1decode(encodedBytes)
if err != nil {
return nil, fmt.Errorf("ecdsa: %s", err)
}
return sig, nil
default:
return nil, errUnsupportedEncoding
}
}
// ValidateECDSAParams validates ECDSA parameters.
// The hash's strength must not be weaker than the curve's strength.
// Only DER encoding is supported now.
func ValidateECDSAParams(hashAlg string, curve string, encoding string) error {
switch encoding {
case "DER":
break
default:
return errUnsupportedEncoding
}
switch curve {
case "NIST_P256":
if hashAlg != "SHA256" {
return errors.New("invalid hash type, expect SHA-256")
}
case "NIST_P384", "NIST_P521":
if hashAlg != "SHA512" {
return errors.New("invalid hash type, expect SHA-512")
}
default:
return fmt.Errorf("unsupported curve: %s", curve)
}
return nil
}