2018-07-24 03:47:47 +03:00
|
|
|
// Copyright 2015 Jeffrey Wilcke, Felix Lange, Gustav Simonsson. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be found in
|
|
|
|
// the LICENSE file.
|
2015-07-07 03:54:22 +03:00
|
|
|
|
2021-08-25 19:46:29 +03:00
|
|
|
//go:build !gofuzz && cgo
|
|
|
|
// +build !gofuzz,cgo
|
crypto/secp256k1: fix undefined behavior in BitCurve.Add (#22621)
This commit changes the behavior of BitCurve.Add to be more inline
with btcd. It fixes two different bugs:
1) When adding a point at infinity to another point, the other point
should be returned. While this is undefined behavior, it is better
to be more inline with the go standard library.
Thus (0,0) + (a, b) = (a,b)
2) Adding the same point to itself produced the point at infinity.
This is incorrect, now doubleJacobian is used to correctly calculate it.
Thus (a,b) + (a,b) == 2* (a,b) and not (0,0) anymore.
The change also adds a differential fuzzer for Add, testing it against btcd.
Co-authored-by: Felix Lange <fjl@twurst.com>
2021-05-27 14:30:25 +03:00
|
|
|
|
2017-01-12 23:29:11 +03:00
|
|
|
// Package secp256k1 wraps the bitcoin secp256k1 C library.
|
2015-01-22 01:35:00 +02:00
|
|
|
package secp256k1
|
|
|
|
|
|
|
|
/*
|
2015-09-28 12:19:23 +03:00
|
|
|
#cgo CFLAGS: -I./libsecp256k1
|
2015-09-29 20:37:44 +03:00
|
|
|
#cgo CFLAGS: -I./libsecp256k1/src/
|
2020-06-24 13:51:32 +03:00
|
|
|
|
|
|
|
#ifdef __SIZEOF_INT128__
|
|
|
|
# define HAVE___INT128
|
|
|
|
# define USE_FIELD_5X52
|
|
|
|
# define USE_SCALAR_4X64
|
|
|
|
#else
|
|
|
|
# define USE_FIELD_10X26
|
|
|
|
# define USE_SCALAR_8X32
|
|
|
|
#endif
|
|
|
|
|
2023-06-23 20:02:05 +03:00
|
|
|
#ifndef NDEBUG
|
|
|
|
# define NDEBUG
|
|
|
|
#endif
|
|
|
|
|
2020-06-24 13:51:32 +03:00
|
|
|
#define USE_ENDOMORPHISM
|
2015-12-03 21:04:39 +02:00
|
|
|
#define USE_NUM_NONE
|
2015-01-22 01:35:00 +02:00
|
|
|
#define USE_FIELD_INV_BUILTIN
|
2015-04-07 13:40:31 +03:00
|
|
|
#define USE_SCALAR_INV_BUILTIN
|
2015-09-28 12:19:23 +03:00
|
|
|
#include "./libsecp256k1/src/secp256k1.c"
|
|
|
|
#include "./libsecp256k1/src/modules/recovery/main_impl.h"
|
2017-01-12 23:29:11 +03:00
|
|
|
#include "ext.h"
|
2015-11-16 19:04:56 +02:00
|
|
|
|
|
|
|
typedef void (*callbackFunc) (const char* msg, void* data);
|
|
|
|
extern void secp256k1GoPanicIllegal(const char* msg, void* data);
|
|
|
|
extern void secp256k1GoPanicError(const char* msg, void* data);
|
2015-01-22 01:35:00 +02:00
|
|
|
*/
|
|
|
|
import "C"
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2017-12-06 18:07:08 +03:00
|
|
|
"math/big"
|
2015-01-22 01:35:00 +02:00
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
2017-02-18 11:24:12 +03:00
|
|
|
var context *C.secp256k1_context
|
2015-01-22 01:35:00 +02:00
|
|
|
|
2015-09-28 12:19:23 +03:00
|
|
|
func init() {
|
|
|
|
// around 20 ms on a modern CPU.
|
2017-01-12 23:29:11 +03:00
|
|
|
context = C.secp256k1_context_create_sign_verify()
|
2015-11-16 19:04:56 +02:00
|
|
|
C.secp256k1_context_set_illegal_callback(context, C.callbackFunc(C.secp256k1GoPanicIllegal), nil)
|
|
|
|
C.secp256k1_context_set_error_callback(context, C.callbackFunc(C.secp256k1GoPanicError), nil)
|
2015-01-22 01:35:00 +02:00
|
|
|
}
|
|
|
|
|
2015-11-16 18:11:26 +02:00
|
|
|
var (
|
2017-01-12 23:29:11 +03:00
|
|
|
ErrInvalidMsgLen = errors.New("invalid message length, need 32 bytes")
|
2015-11-16 18:11:26 +02:00
|
|
|
ErrInvalidSignatureLen = errors.New("invalid signature length")
|
|
|
|
ErrInvalidRecoveryID = errors.New("invalid signature recovery id")
|
2017-01-12 23:29:11 +03:00
|
|
|
ErrInvalidKey = errors.New("invalid private key")
|
2017-12-06 18:07:08 +03:00
|
|
|
ErrInvalidPubkey = errors.New("invalid public key")
|
2017-01-12 23:29:11 +03:00
|
|
|
ErrSignFailed = errors.New("signing failed")
|
|
|
|
ErrRecoverFailed = errors.New("recovery failed")
|
2015-11-16 18:11:26 +02:00
|
|
|
)
|
|
|
|
|
2017-01-12 23:29:11 +03:00
|
|
|
// Sign creates a recoverable ECDSA signature.
|
|
|
|
// The produced signature is in the 65-byte [R || S || V] format where V is 0 or 1.
|
|
|
|
//
|
|
|
|
// The caller is responsible for ensuring that msg cannot be chosen
|
|
|
|
// directly by an attacker. It is usually preferable to use a cryptographic
|
|
|
|
// hash function on any input before handing it to this function.
|
|
|
|
func Sign(msg []byte, seckey []byte) ([]byte, error) {
|
|
|
|
if len(msg) != 32 {
|
|
|
|
return nil, ErrInvalidMsgLen
|
2015-02-15 03:20:31 +02:00
|
|
|
}
|
2017-01-12 23:29:11 +03:00
|
|
|
if len(seckey) != 32 {
|
|
|
|
return nil, ErrInvalidKey
|
2015-01-22 01:35:00 +02:00
|
|
|
}
|
2017-01-12 23:29:11 +03:00
|
|
|
seckeydata := (*C.uchar)(unsafe.Pointer(&seckey[0]))
|
|
|
|
if C.secp256k1_ec_seckey_verify(context, seckeydata) != 1 {
|
|
|
|
return nil, ErrInvalidKey
|
2015-01-22 01:35:00 +02:00
|
|
|
}
|
|
|
|
|
2017-01-12 23:29:11 +03:00
|
|
|
var (
|
2017-01-23 01:28:47 +03:00
|
|
|
msgdata = (*C.uchar)(unsafe.Pointer(&msg[0]))
|
|
|
|
noncefunc = C.secp256k1_nonce_function_rfc6979
|
|
|
|
sigstruct C.secp256k1_ecdsa_recoverable_signature
|
2015-09-28 12:19:23 +03:00
|
|
|
)
|
2017-01-23 01:28:47 +03:00
|
|
|
if C.secp256k1_ecdsa_sign_recoverable(context, &sigstruct, msgdata, seckeydata, noncefunc, nil) == 0 {
|
2017-01-12 23:29:11 +03:00
|
|
|
return nil, ErrSignFailed
|
2015-01-22 01:35:00 +02:00
|
|
|
}
|
|
|
|
|
2017-01-12 23:29:11 +03:00
|
|
|
var (
|
|
|
|
sig = make([]byte, 65)
|
|
|
|
sigdata = (*C.uchar)(unsafe.Pointer(&sig[0]))
|
|
|
|
recid C.int
|
2015-09-28 12:19:23 +03:00
|
|
|
)
|
2017-01-12 23:29:11 +03:00
|
|
|
C.secp256k1_ecdsa_recoverable_signature_serialize_compact(context, sigdata, &recid, &sigstruct)
|
|
|
|
sig[64] = byte(recid) // add back recid to get 65 bytes sig
|
|
|
|
return sig, nil
|
2015-01-22 01:35:00 +02:00
|
|
|
}
|
|
|
|
|
2018-08-27 11:49:29 +03:00
|
|
|
// RecoverPubkey returns the public key of the signer.
|
2015-11-16 18:11:26 +02:00
|
|
|
// msg must be the 32-byte hash of the message to be signed.
|
|
|
|
// sig must be a 65-byte compact ECDSA signature containing the
|
|
|
|
// recovery id as the last element.
|
2015-01-22 01:35:00 +02:00
|
|
|
func RecoverPubkey(msg []byte, sig []byte) ([]byte, error) {
|
2015-11-16 18:11:26 +02:00
|
|
|
if len(msg) != 32 {
|
|
|
|
return nil, ErrInvalidMsgLen
|
|
|
|
}
|
|
|
|
if err := checkSignature(sig); err != nil {
|
|
|
|
return nil, err
|
2015-01-22 01:35:00 +02:00
|
|
|
}
|
|
|
|
|
2017-01-12 23:29:11 +03:00
|
|
|
var (
|
|
|
|
pubkey = make([]byte, 65)
|
|
|
|
sigdata = (*C.uchar)(unsafe.Pointer(&sig[0]))
|
|
|
|
msgdata = (*C.uchar)(unsafe.Pointer(&msg[0]))
|
2015-01-22 01:35:00 +02:00
|
|
|
)
|
2017-12-15 12:40:09 +03:00
|
|
|
if C.secp256k1_ext_ecdsa_recover(context, (*C.uchar)(unsafe.Pointer(&pubkey[0])), sigdata, msgdata) == 0 {
|
2017-01-12 23:29:11 +03:00
|
|
|
return nil, ErrRecoverFailed
|
2015-01-22 01:35:00 +02:00
|
|
|
}
|
2017-01-12 23:29:11 +03:00
|
|
|
return pubkey, nil
|
2015-11-16 18:11:26 +02:00
|
|
|
}
|
|
|
|
|
2017-12-06 18:07:08 +03:00
|
|
|
// VerifySignature checks that the given pubkey created signature over message.
|
|
|
|
// The signature should be in [R || S] format.
|
|
|
|
func VerifySignature(pubkey, msg, signature []byte) bool {
|
|
|
|
if len(msg) != 32 || len(signature) != 64 || len(pubkey) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
sigdata := (*C.uchar)(unsafe.Pointer(&signature[0]))
|
|
|
|
msgdata := (*C.uchar)(unsafe.Pointer(&msg[0]))
|
|
|
|
keydata := (*C.uchar)(unsafe.Pointer(&pubkey[0]))
|
2017-12-15 12:40:09 +03:00
|
|
|
return C.secp256k1_ext_ecdsa_verify(context, sigdata, msgdata, keydata, C.size_t(len(pubkey))) != 0
|
2017-12-06 18:07:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// DecompressPubkey parses a public key in the 33-byte compressed format.
|
|
|
|
// It returns non-nil coordinates if the public key is valid.
|
2017-12-15 12:40:09 +03:00
|
|
|
func DecompressPubkey(pubkey []byte) (x, y *big.Int) {
|
2017-12-06 18:07:08 +03:00
|
|
|
if len(pubkey) != 33 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2017-12-15 12:40:09 +03:00
|
|
|
var (
|
|
|
|
pubkeydata = (*C.uchar)(unsafe.Pointer(&pubkey[0]))
|
|
|
|
pubkeylen = C.size_t(len(pubkey))
|
|
|
|
out = make([]byte, 65)
|
|
|
|
outdata = (*C.uchar)(unsafe.Pointer(&out[0]))
|
|
|
|
outlen = C.size_t(len(out))
|
|
|
|
)
|
|
|
|
if C.secp256k1_ext_reencode_pubkey(context, outdata, outlen, pubkeydata, pubkeylen) == 0 {
|
2017-12-06 18:07:08 +03:00
|
|
|
return nil, nil
|
|
|
|
}
|
2017-12-15 12:40:09 +03:00
|
|
|
return new(big.Int).SetBytes(out[1:33]), new(big.Int).SetBytes(out[33:])
|
|
|
|
}
|
|
|
|
|
|
|
|
// CompressPubkey encodes a public key to 33-byte compressed format.
|
|
|
|
func CompressPubkey(x, y *big.Int) []byte {
|
|
|
|
var (
|
|
|
|
pubkey = S256().Marshal(x, y)
|
|
|
|
pubkeydata = (*C.uchar)(unsafe.Pointer(&pubkey[0]))
|
|
|
|
pubkeylen = C.size_t(len(pubkey))
|
|
|
|
out = make([]byte, 33)
|
|
|
|
outdata = (*C.uchar)(unsafe.Pointer(&out[0]))
|
|
|
|
outlen = C.size_t(len(out))
|
|
|
|
)
|
|
|
|
if C.secp256k1_ext_reencode_pubkey(context, outdata, outlen, pubkeydata, pubkeylen) == 0 {
|
|
|
|
panic("libsecp256k1 error")
|
|
|
|
}
|
|
|
|
return out
|
2017-12-06 18:07:08 +03:00
|
|
|
}
|
|
|
|
|
2015-11-16 18:11:26 +02:00
|
|
|
func checkSignature(sig []byte) error {
|
|
|
|
if len(sig) != 65 {
|
|
|
|
return ErrInvalidSignatureLen
|
|
|
|
}
|
|
|
|
if sig[64] >= 4 {
|
|
|
|
return ErrInvalidRecoveryID
|
|
|
|
}
|
|
|
|
return nil
|
2015-01-22 01:35:00 +02:00
|
|
|
}
|