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 // ErrSigTooLong is returned when a signature that should be a DER signature
18 // is too long.
19 ErrSigTooLong = ErrorKind("ErrSigTooLong")
20 // ErrSigInvalidSeqID is returned when a signature that should be a DER
21 // signature does not have the expected ASN.1 sequence ID.
22 ErrSigInvalidSeqID = ErrorKind("ErrSigInvalidSeqID")
23 // ErrSigInvalidDataLen is returned when a signature that should be a DER
24 // signature does not specify the correct number of remaining bytes for the
25 // R and S portions.
26 ErrSigInvalidDataLen = ErrorKind("ErrSigInvalidDataLen")
27 // ErrSigMissingSTypeID is returned when a signature that should be a DER
28 // signature does not provide the ASN.1 type ID for S.
29 ErrSigMissingSTypeID = ErrorKind("ErrSigMissingSTypeID")
30 // ErrSigMissingSLen is returned when a signature that should be a DER
31 // signature does not provide the length of S.
32 ErrSigMissingSLen = ErrorKind("ErrSigMissingSLen")
33 // ErrSigInvalidSLen is returned when a signature that should be a DER
34 // signature does not specify the correct number of bytes for the S portion.
35 ErrSigInvalidSLen = ErrorKind("ErrSigInvalidSLen")
36 // ErrSigInvalidRIntID is returned when a signature that should be a DER
37 // signature does not have the expected ASN.1 integer ID for R.
38 ErrSigInvalidRIntID = ErrorKind("ErrSigInvalidRIntID")
39 // ErrSigZeroRLen is returned when a signature that should be a DER
40 // signature has an R length of zero.
41 ErrSigZeroRLen = ErrorKind("ErrSigZeroRLen")
42 // ErrSigNegativeR is returned when a signature that should be a DER
43 // signature has a negative value for R.
44 ErrSigNegativeR = ErrorKind("ErrSigNegativeR")
45 // ErrSigTooMuchRPadding is returned when a signature that should be a DER
46 // signature has too much padding for R.
47 ErrSigTooMuchRPadding = ErrorKind("ErrSigTooMuchRPadding")
48 // ErrSigRIsZero is returned when a signature has R set to the value zero.
49 ErrSigRIsZero = ErrorKind("ErrSigRIsZero")
50 // ErrSigRTooBig is returned when a signature has R with a value that is
51 // greater than or equal to the group order.
52 ErrSigRTooBig = ErrorKind("ErrSigRTooBig")
53 // ErrSigInvalidSIntID is returned when a signature that should be a DER
54 // signature does not have the expected ASN.1 integer ID for S.
55 ErrSigInvalidSIntID = ErrorKind("ErrSigInvalidSIntID")
56 // ErrSigZeroSLen is returned when a signature that should be a DER
57 // signature has an S length of zero.
58 ErrSigZeroSLen = ErrorKind("ErrSigZeroSLen")
59 // ErrSigNegativeS is returned when a signature that should be a DER
60 // signature has a negative value for S.
61 ErrSigNegativeS = ErrorKind("ErrSigNegativeS")
62 // ErrSigTooMuchSPadding is returned when a signature that should be a DER
63 // signature has too much padding for S.
64 ErrSigTooMuchSPadding = ErrorKind("ErrSigTooMuchSPadding")
65 // ErrSigSIsZero is returned when a signature has S set to the value zero.
66 ErrSigSIsZero = ErrorKind("ErrSigSIsZero")
67 // ErrSigSTooBig is returned when a signature has S with a value that is
68 // greater than or equal to the group order.
69 ErrSigSTooBig = ErrorKind("ErrSigSTooBig")
70 // ErrSigInvalidLen is returned when a signature that should be a compact
71 // signature is not the required length.
72 ErrSigInvalidLen = ErrorKind("ErrSigInvalidLen")
73 // ErrSigInvalidRecoveryCode is returned when a signature that should be a
74 // compact signature has an invalid value for the public key recovery code.
75 ErrSigInvalidRecoveryCode = ErrorKind("ErrSigInvalidRecoveryCode")
76 // ErrSigOverflowsPrime is returned when a signature that should be a
77 // compact signature has the overflow bit set but adding the order to it
78 // would overflow the underlying field prime.
79 ErrSigOverflowsPrime = ErrorKind("ErrSigOverflowsPrime")
80 // ErrPointNotOnCurve is returned when attempting to recover a public key
81 // from a compact signature results in a point that is not on the elliptic
82 // curve.
83 ErrPointNotOnCurve = ErrorKind("ErrPointNotOnCurve")
84 )
85 86 // Error satisfies the error interface and prints human-readable errors.
87 func (e ErrorKind) Error() string { return string(e) }
88 89 // Error identifies an error related to an ECDSA signature. It has full
90 // support for errors.Is and errors.As, so the caller can ascertain the
91 // specific reason for the error by checking the underlying error.
92 type Error struct {
93 Err error
94 Description string
95 }
96 97 // Error satisfies the error interface and prints human-readable errors.
98 func (e Error) Error() string { return e.Description }
99 100 // Unwrap returns the underlying wrapped error.
101 func (e Error) Unwrap() error { return e.Err }
102 103 // signatureError creates an Error given a set of arguments.
104 func signatureError(kind ErrorKind, desc string) Error {
105 return Error{Err: kind, Description: string(append([]byte(nil), desc...))}
106 }
107