You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

248 lines
5.7 KiB

  1. package secp256k1
  2. // TODO: set USE_SCALAR_4X64 depending on platform?
  3. /*
  4. #cgo CFLAGS: -I./secp256k1
  5. #cgo darwin CFLAGS: -I/usr/local/include
  6. #cgo linux,arm CFLAGS: -I/usr/local/arm/include
  7. #cgo LDFLAGS: -lgmp
  8. #cgo darwin LDFLAGS: -L/usr/local/lib
  9. #cgo linux,arm LDFLAGS: -L/usr/local/arm/lib
  10. #define USE_NUM_GMP
  11. #define USE_FIELD_10X26
  12. #define USE_FIELD_INV_BUILTIN
  13. #define USE_SCALAR_8X32
  14. #define USE_SCALAR_INV_BUILTIN
  15. #define NDEBUG
  16. #include "./secp256k1/src/secp256k1.c"
  17. */
  18. import "C"
  19. import (
  20. "bytes"
  21. "errors"
  22. "unsafe"
  23. "github.com/tendermint/tendermint/vm/randentropy"
  24. )
  25. //#define USE_FIELD_5X64
  26. /*
  27. Todo:
  28. > Centralize key management in module
  29. > add pubkey/private key struct
  30. > Dont let keys leave module; address keys as ints
  31. > store private keys in buffer and shuffle (deters persistance on swap disc)
  32. > Byte permutation (changing)
  33. > xor with chaning random block (to deter scanning memory for 0x63) (stream cipher?)
  34. On Disk
  35. > Store keys in wallets
  36. > use slow key derivation function for wallet encryption key (2 seconds)
  37. */
  38. func init() {
  39. //takes 10ms to 100ms
  40. C.secp256k1_start(3) // SECP256K1_START_SIGN | SECP256K1_START_VERIFY
  41. }
  42. func Stop() {
  43. C.secp256k1_stop()
  44. }
  45. func GenerateKeyPair() ([]byte, []byte) {
  46. pubkey_len := C.int(65)
  47. const seckey_len = 32
  48. var pubkey []byte = make([]byte, pubkey_len)
  49. var seckey []byte = randentropy.GetEntropyCSPRNG(seckey_len)
  50. var pubkey_ptr *C.uchar = (*C.uchar)(unsafe.Pointer(&pubkey[0]))
  51. var seckey_ptr *C.uchar = (*C.uchar)(unsafe.Pointer(&seckey[0]))
  52. ret := C.secp256k1_ec_pubkey_create(
  53. pubkey_ptr, &pubkey_len,
  54. seckey_ptr, 0)
  55. if ret != C.int(1) {
  56. return GenerateKeyPair() //invalid secret, try again
  57. }
  58. return pubkey, seckey
  59. }
  60. func GeneratePubKey(seckey []byte) ([]byte, error) {
  61. if err := VerifySeckeyValidity(seckey); err != nil {
  62. return nil, err
  63. }
  64. pubkey_len := C.int(65)
  65. const seckey_len = 32
  66. var pubkey []byte = make([]byte, pubkey_len)
  67. var pubkey_ptr *C.uchar = (*C.uchar)(unsafe.Pointer(&pubkey[0]))
  68. var seckey_ptr *C.uchar = (*C.uchar)(unsafe.Pointer(&seckey[0]))
  69. ret := C.secp256k1_ec_pubkey_create(
  70. pubkey_ptr, &pubkey_len,
  71. seckey_ptr, 0)
  72. if ret != C.int(1) {
  73. return nil, errors.New("Unable to generate pubkey from seckey")
  74. }
  75. return pubkey, nil
  76. }
  77. func Sign(msg []byte, seckey []byte) ([]byte, error) {
  78. nonce := randentropy.GetEntropyCSPRNG(32)
  79. var sig []byte = make([]byte, 65)
  80. var recid C.int
  81. var msg_ptr *C.uchar = (*C.uchar)(unsafe.Pointer(&msg[0]))
  82. var sig_ptr *C.uchar = (*C.uchar)(unsafe.Pointer(&sig[0]))
  83. var seckey_ptr *C.uchar = (*C.uchar)(unsafe.Pointer(&seckey[0]))
  84. var noncefp_ptr = &(*C.secp256k1_nonce_function_default)
  85. var ndata_ptr = unsafe.Pointer(&nonce[0])
  86. if C.secp256k1_ec_seckey_verify(seckey_ptr) != C.int(1) {
  87. return nil, errors.New("Invalid secret key")
  88. }
  89. ret := C.secp256k1_ecdsa_sign_compact(
  90. msg_ptr,
  91. sig_ptr,
  92. seckey_ptr,
  93. noncefp_ptr,
  94. ndata_ptr,
  95. &recid)
  96. sig[64] = byte(int(recid))
  97. if ret != C.int(1) {
  98. // nonce invalid, retry
  99. return Sign(msg, seckey)
  100. }
  101. return sig, nil
  102. }
  103. func VerifySeckeyValidity(seckey []byte) error {
  104. if len(seckey) != 32 {
  105. return errors.New("priv key is not 32 bytes")
  106. }
  107. var seckey_ptr *C.uchar = (*C.uchar)(unsafe.Pointer(&seckey[0]))
  108. ret := C.secp256k1_ec_seckey_verify(seckey_ptr)
  109. if int(ret) != 1 {
  110. return errors.New("invalid seckey")
  111. }
  112. return nil
  113. }
  114. func VerifyPubkeyValidity(pubkey []byte) error {
  115. if len(pubkey) != 65 {
  116. return errors.New("pub key is not 65 bytes")
  117. }
  118. var pubkey_ptr *C.uchar = (*C.uchar)(unsafe.Pointer(&pubkey[0]))
  119. ret := C.secp256k1_ec_pubkey_verify(pubkey_ptr, 65)
  120. if int(ret) != 1 {
  121. return errors.New("invalid pubkey")
  122. }
  123. return nil
  124. }
  125. func VerifySignatureValidity(sig []byte) bool {
  126. //64+1
  127. if len(sig) != 65 {
  128. return false
  129. }
  130. //malleability check, highest bit must be 1
  131. if (sig[32] & 0x80) == 0x80 {
  132. return false
  133. }
  134. //recovery id check
  135. if sig[64] >= 4 {
  136. return false
  137. }
  138. return true
  139. }
  140. //for compressed signatures, does not need pubkey
  141. func VerifySignature(msg []byte, sig []byte, pubkey1 []byte) error {
  142. if msg == nil || sig == nil || pubkey1 == nil {
  143. return errors.New("inputs must be non-nil")
  144. }
  145. if len(sig) != 65 {
  146. return errors.New("invalid signature length")
  147. }
  148. if len(pubkey1) != 65 {
  149. return errors.New("Invalid public key length")
  150. }
  151. //to enforce malleability, highest bit of S must be 0
  152. //S starts at 32nd byte
  153. if (sig[32] & 0x80) == 0x80 { //highest bit must be 1
  154. return errors.New("Signature not malleable")
  155. }
  156. if sig[64] >= 4 {
  157. return errors.New("Recover byte invalid")
  158. }
  159. // if pubkey recovered, signature valid
  160. pubkey2, err := RecoverPubkey(msg, sig)
  161. if err != nil {
  162. return err
  163. }
  164. if len(pubkey2) != 65 {
  165. return errors.New("Invalid recovered public key length")
  166. }
  167. if !bytes.Equal(pubkey1, pubkey2) {
  168. return errors.New("Public key does not match recovered public key")
  169. }
  170. return nil
  171. }
  172. //recovers the public key from the signature
  173. //recovery of pubkey means correct signature
  174. func RecoverPubkey(msg []byte, sig []byte) ([]byte, error) {
  175. if len(sig) != 65 {
  176. return nil, errors.New("Invalid signature length")
  177. }
  178. var pubkey []byte = make([]byte, 65)
  179. var msg_ptr *C.uchar = (*C.uchar)(unsafe.Pointer(&msg[0]))
  180. var sig_ptr *C.uchar = (*C.uchar)(unsafe.Pointer(&sig[0]))
  181. var pubkey_ptr *C.uchar = (*C.uchar)(unsafe.Pointer(&pubkey[0]))
  182. var pubkeylen C.int
  183. ret := C.secp256k1_ecdsa_recover_compact(
  184. msg_ptr,
  185. sig_ptr,
  186. pubkey_ptr,
  187. &pubkeylen,
  188. C.int(0),
  189. C.int(sig[64]),
  190. )
  191. if ret == C.int(0) {
  192. return nil, errors.New("Failed to recover public key")
  193. } else if pubkeylen != C.int(65) {
  194. return nil, errors.New("Impossible Error: Invalid recovered public key length")
  195. } else {
  196. return pubkey, nil
  197. }
  198. return nil, errors.New("Impossible Error: func RecoverPubkey has reached an unreachable state")
  199. }