2016-12-08 23:56:37 +03:00
|
|
|
package lnwire
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2018-06-05 04:34:16 +03:00
|
|
|
"github.com/btcsuite/btcd/btcec"
|
2020-04-06 03:06:38 +03:00
|
|
|
"github.com/lightningnetwork/lnd/input"
|
2016-12-08 23:56:37 +03:00
|
|
|
)
|
|
|
|
|
2018-01-31 06:40:30 +03:00
|
|
|
// Sig is a fixed-sized ECDSA signature. Unlike Bitcoin, we use fixed sized
|
|
|
|
// signatures on the wire, instead of DER encoded signatures. This type
|
|
|
|
// provides several methods to convert to/from a regular Bitcoin DER encoded
|
|
|
|
// signature (raw bytes and *btcec.Signature).
|
|
|
|
type Sig [64]byte
|
2016-12-08 23:56:37 +03:00
|
|
|
|
2018-01-31 06:40:30 +03:00
|
|
|
// NewSigFromRawSignature returns a Sig from a Bitcoin raw signature encoded in
|
2018-02-07 11:30:09 +03:00
|
|
|
// the canonical DER encoding.
|
2018-01-31 06:40:30 +03:00
|
|
|
func NewSigFromRawSignature(sig []byte) (Sig, error) {
|
|
|
|
var b Sig
|
2016-12-08 23:56:37 +03:00
|
|
|
|
2018-06-08 23:24:59 +03:00
|
|
|
if len(sig) == 0 {
|
|
|
|
return b, fmt.Errorf("cannot decode empty signature")
|
|
|
|
}
|
|
|
|
|
2016-12-08 23:56:37 +03:00
|
|
|
// Extract lengths of R and S. The DER representation is laid out as
|
|
|
|
// 0x30 <length> 0x02 <length r> r 0x02 <length s> s
|
|
|
|
// which means the length of R is the 4th byte and the length of S
|
|
|
|
// is the second byte after R ends. 0x02 signifies a length-prefixed,
|
2018-02-07 06:11:11 +03:00
|
|
|
// zero-padded, big-endian bigint. 0x30 signifies a DER signature.
|
2016-12-08 23:56:37 +03:00
|
|
|
// See the Serialize() method for btcec.Signature for details.
|
2017-02-23 22:07:01 +03:00
|
|
|
rLen := sig[3]
|
|
|
|
sLen := sig[5+rLen]
|
2016-12-08 23:56:37 +03:00
|
|
|
|
|
|
|
// Check to make sure R and S can both fit into their intended buffers.
|
2018-01-31 06:40:30 +03:00
|
|
|
// We check S first because these code blocks decrement sLen and rLen
|
|
|
|
// in the case of a 33-byte 0-padded integer returned from Serialize()
|
|
|
|
// and rLen is used in calculating array indices for S. We can track
|
|
|
|
// this with additional variables, but it's more efficient to just
|
|
|
|
// check S first.
|
2016-12-08 23:56:37 +03:00
|
|
|
if sLen > 32 {
|
|
|
|
if (sLen > 33) || (sig[6+rLen] != 0x00) {
|
2018-01-31 06:40:30 +03:00
|
|
|
return b, fmt.Errorf("S is over 32 bytes long " +
|
2016-12-08 23:56:37 +03:00
|
|
|
"without padding")
|
|
|
|
}
|
2017-02-23 22:56:47 +03:00
|
|
|
sLen--
|
|
|
|
copy(b[64-sLen:], sig[7+rLen:])
|
2016-12-08 23:56:37 +03:00
|
|
|
} else {
|
|
|
|
copy(b[64-sLen:], sig[6+rLen:])
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do the same for R as we did for S
|
|
|
|
if rLen > 32 {
|
|
|
|
if (rLen > 33) || (sig[4] != 0x00) {
|
2018-01-31 06:40:30 +03:00
|
|
|
return b, fmt.Errorf("R is over 32 bytes long " +
|
2016-12-08 23:56:37 +03:00
|
|
|
"without padding")
|
|
|
|
}
|
2017-02-23 22:56:47 +03:00
|
|
|
rLen--
|
|
|
|
copy(b[32-rLen:], sig[5:5+rLen])
|
2016-12-08 23:56:37 +03:00
|
|
|
} else {
|
|
|
|
copy(b[32-rLen:], sig[4:4+rLen])
|
|
|
|
}
|
2018-01-31 06:40:30 +03:00
|
|
|
|
|
|
|
return b, nil
|
2016-12-08 23:56:37 +03:00
|
|
|
}
|
|
|
|
|
2018-01-31 06:40:30 +03:00
|
|
|
// NewSigFromSignature creates a new signature as used on the wire, from an
|
|
|
|
// existing btcec.Signature.
|
2020-04-06 03:06:38 +03:00
|
|
|
func NewSigFromSignature(e input.Signature) (Sig, error) {
|
2018-06-08 23:24:59 +03:00
|
|
|
if e == nil {
|
|
|
|
return Sig{}, fmt.Errorf("cannot decode empty signature")
|
|
|
|
}
|
|
|
|
|
2018-01-31 06:40:30 +03:00
|
|
|
// Serialize the signature with all the checks that entails.
|
|
|
|
return NewSigFromRawSignature(e.Serialize())
|
|
|
|
}
|
2016-12-08 23:56:37 +03:00
|
|
|
|
2018-01-31 06:40:30 +03:00
|
|
|
// ToSignature converts the fixed-sized signature to a btcec.Signature objects
|
|
|
|
// which can be used for signature validation checks.
|
|
|
|
func (b *Sig) ToSignature() (*btcec.Signature, error) {
|
|
|
|
// Parse the signature with strict checks.
|
|
|
|
sigBytes := b.ToSignatureBytes()
|
|
|
|
sig, err := btcec.ParseDERSignature(sigBytes, btcec.S256())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return sig, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToSignatureBytes serializes the target fixed-sized signature into the raw
|
|
|
|
// bytes of a DER encoding.
|
|
|
|
func (b *Sig) ToSignatureBytes() []byte {
|
2016-12-08 23:56:37 +03:00
|
|
|
// Extract canonically-padded bigint representations from buffer
|
|
|
|
r := extractCanonicalPadding(b[0:32])
|
|
|
|
s := extractCanonicalPadding(b[32:64])
|
|
|
|
rLen := uint8(len(r))
|
|
|
|
sLen := uint8(len(s))
|
|
|
|
|
|
|
|
// Create a canonical serialized signature. DER format is:
|
|
|
|
// 0x30 <length> 0x02 <length r> r 0x02 <length s> s
|
2017-03-16 14:04:19 +03:00
|
|
|
sigBytes := make([]byte, 6+rLen+sLen)
|
2016-12-08 23:56:37 +03:00
|
|
|
sigBytes[0] = 0x30 // DER signature magic value
|
|
|
|
sigBytes[1] = 4 + rLen + sLen // Length of rest of signature
|
|
|
|
sigBytes[2] = 0x02 // Big integer magic value
|
|
|
|
sigBytes[3] = rLen // Length of R
|
|
|
|
sigBytes[rLen+4] = 0x02 // Big integer magic value
|
|
|
|
sigBytes[rLen+5] = sLen // Length of S
|
|
|
|
copy(sigBytes[4:], r) // Copy R
|
|
|
|
copy(sigBytes[rLen+6:], s) // Copy S
|
|
|
|
|
2018-01-31 06:40:30 +03:00
|
|
|
return sigBytes
|
2016-12-08 23:56:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// extractCanonicalPadding is a utility function to extract the canonical
|
|
|
|
// padding of a big-endian integer from the wire encoding (a 0-padded
|
|
|
|
// big-endian integer) such that it passes btcec.canonicalPadding test.
|
|
|
|
func extractCanonicalPadding(b []byte) []byte {
|
|
|
|
for i := 0; i < len(b); i++ {
|
|
|
|
// Found first non-zero byte.
|
|
|
|
if b[i] > 0 {
|
|
|
|
// If the MSB is set, we need zero padding.
|
|
|
|
if b[i]&0x80 == 0x80 {
|
|
|
|
return append([]byte{0x00}, b[i:]...)
|
|
|
|
}
|
2017-02-23 22:56:47 +03:00
|
|
|
return b[i:]
|
2016-12-08 23:56:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return []byte{0x00}
|
|
|
|
}
|