error.go raw

   1  // Copyright (c) 2020-2022 The Decred developers
   2  // Use of this source code is governed by an ISC
   3  // license that can be found in the LICENSE file.
   4  
   5  package ecdsa
   6  
   7  // ErrorKind identifies a kind of error.  It has full support for
   8  // errors.Is and errors.As, so the caller can directly check against
   9  // an error kind when determining the reason for an error.
  10  type ErrorKind string
  11  
  12  // These constants are used to identify a specific Error.
  13  const (
  14  	// ErrSigTooShort is returned when a signature that should be a DER
  15  	// signature is too short.
  16  	ErrSigTooShort = ErrorKind("ErrSigTooShort")
  17  
  18  	// ErrSigTooLong is returned when a signature that should be a DER signature
  19  	// is too long.
  20  	ErrSigTooLong = ErrorKind("ErrSigTooLong")
  21  
  22  	// ErrSigInvalidSeqID is returned when a signature that should be a DER
  23  	// signature does not have the expected ASN.1 sequence ID.
  24  	ErrSigInvalidSeqID = ErrorKind("ErrSigInvalidSeqID")
  25  
  26  	// ErrSigInvalidDataLen is returned when a signature that should be a DER
  27  	// signature does not specify the correct number of remaining bytes for the
  28  	// R and S portions.
  29  	ErrSigInvalidDataLen = ErrorKind("ErrSigInvalidDataLen")
  30  
  31  	// ErrSigMissingSTypeID is returned when a signature that should be a DER
  32  	// signature does not provide the ASN.1 type ID for S.
  33  	ErrSigMissingSTypeID = ErrorKind("ErrSigMissingSTypeID")
  34  
  35  	// ErrSigMissingSLen is returned when a signature that should be a DER
  36  	// signature does not provide the length of S.
  37  	ErrSigMissingSLen = ErrorKind("ErrSigMissingSLen")
  38  
  39  	// ErrSigInvalidSLen is returned when a signature that should be a DER
  40  	// signature does not specify the correct number of bytes for the S portion.
  41  	ErrSigInvalidSLen = ErrorKind("ErrSigInvalidSLen")
  42  
  43  	// ErrSigInvalidRIntID is returned when a signature that should be a DER
  44  	// signature does not have the expected ASN.1 integer ID for R.
  45  	ErrSigInvalidRIntID = ErrorKind("ErrSigInvalidRIntID")
  46  
  47  	// ErrSigZeroRLen is returned when a signature that should be a DER
  48  	// signature has an R length of zero.
  49  	ErrSigZeroRLen = ErrorKind("ErrSigZeroRLen")
  50  
  51  	// ErrSigNegativeR is returned when a signature that should be a DER
  52  	// signature has a negative value for R.
  53  	ErrSigNegativeR = ErrorKind("ErrSigNegativeR")
  54  
  55  	// ErrSigTooMuchRPadding is returned when a signature that should be a DER
  56  	// signature has too much padding for R.
  57  	ErrSigTooMuchRPadding = ErrorKind("ErrSigTooMuchRPadding")
  58  
  59  	// ErrSigRIsZero is returned when a signature has R set to the value zero.
  60  	ErrSigRIsZero = ErrorKind("ErrSigRIsZero")
  61  
  62  	// ErrSigRTooBig is returned when a signature has R with a value that is
  63  	// greater than or equal to the group order.
  64  	ErrSigRTooBig = ErrorKind("ErrSigRTooBig")
  65  
  66  	// ErrSigInvalidSIntID is returned when a signature that should be a DER
  67  	// signature does not have the expected ASN.1 integer ID for S.
  68  	ErrSigInvalidSIntID = ErrorKind("ErrSigInvalidSIntID")
  69  
  70  	// ErrSigZeroSLen is returned when a signature that should be a DER
  71  	// signature has an S length of zero.
  72  	ErrSigZeroSLen = ErrorKind("ErrSigZeroSLen")
  73  
  74  	// ErrSigNegativeS is returned when a signature that should be a DER
  75  	// signature has a negative value for S.
  76  	ErrSigNegativeS = ErrorKind("ErrSigNegativeS")
  77  
  78  	// ErrSigTooMuchSPadding is returned when a signature that should be a DER
  79  	// signature has too much padding for S.
  80  	ErrSigTooMuchSPadding = ErrorKind("ErrSigTooMuchSPadding")
  81  
  82  	// ErrSigSIsZero is returned when a signature has S set to the value zero.
  83  	ErrSigSIsZero = ErrorKind("ErrSigSIsZero")
  84  
  85  	// ErrSigSTooBig is returned when a signature has S with a value that is
  86  	// greater than or equal to the group order.
  87  	ErrSigSTooBig = ErrorKind("ErrSigSTooBig")
  88  
  89  	// ErrSigInvalidLen is returned when a signature that should be a compact
  90  	// signature is not the required length.
  91  	ErrSigInvalidLen = ErrorKind("ErrSigInvalidLen")
  92  
  93  	// ErrSigInvalidRecoveryCode is returned when a signature that should be a
  94  	// compact signature has an invalid value for the public key recovery code.
  95  	ErrSigInvalidRecoveryCode = ErrorKind("ErrSigInvalidRecoveryCode")
  96  
  97  	// ErrSigOverflowsPrime is returned when a signature that should be a
  98  	// compact signature has the overflow bit set but adding the order to it
  99  	// would overflow the underlying field prime.
 100  	ErrSigOverflowsPrime = ErrorKind("ErrSigOverflowsPrime")
 101  
 102  	// ErrPointNotOnCurve is returned when attempting to recover a public key
 103  	// from a compact signature results in a point that is not on the elliptic
 104  	// curve.
 105  	ErrPointNotOnCurve = ErrorKind("ErrPointNotOnCurve")
 106  )
 107  
 108  // Error satisfies the error interface and prints human-readable errors.
 109  func (e ErrorKind) Error() string {
 110  	return string(e)
 111  }
 112  
 113  // Error identifies an error related to an ECDSA signature. It has full
 114  // support for errors.Is and errors.As, so the caller can ascertain the
 115  // specific reason for the error by checking the underlying error.
 116  type Error struct {
 117  	Err         error
 118  	Description string
 119  }
 120  
 121  // Error satisfies the error interface and prints human-readable errors.
 122  func (e Error) Error() string {
 123  	return e.Description
 124  }
 125  
 126  // Unwrap returns the underlying wrapped error.
 127  func (e Error) Unwrap() error {
 128  	return e.Err
 129  }
 130  
 131  // signatureError creates an Error given a set of arguments.
 132  func signatureError(kind ErrorKind, desc string) Error {
 133  	return Error{Err: kind, Description: desc}
 134  }
 135