libbtc
bitcoinclibrary
Functions
ecc_key.c File Reference
#include "btc/ecc_key.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "btc/ecc.h"
#include "btc/hash.h"
#include "random.h"
#include "ripemd160.h"
#include "utils.h"

Go to the source code of this file.

Functions

void btc_privkey_init (btc_key *privkey)
 
btc_bool btc_privkey_is_valid (btc_key *privkey)
 
void btc_privkey_cleanse (btc_key *privkey)
 
void btc_privkey_gen (btc_key *privkey)
 
btc_bool btc_privkey_verify_pubkey (btc_key *privkey, btc_pubkey *pubkey)
 
void btc_pubkey_init (btc_pubkey *pubkey)
 
btc_bool btc_pubkey_is_valid (btc_pubkey *pubkey)
 
void btc_pubkey_cleanse (btc_pubkey *pubkey)
 
void btc_pubkey_get_hash160 (const btc_pubkey *pubkey, uint8_t *hash160)
 
void btc_pubkey_from_key (btc_key *privkey, btc_pubkey *pubkey_inout)
 
btc_bool btc_key_sign_hash (const btc_key *privkey, const uint8_t *hash, unsigned char *sigout, size_t *outlen)
 
btc_bool btc_pubkey_verify_sig (const btc_pubkey *pubkey, const uint8_t *hash, unsigned char *sigder, int len)
 

Function Documentation

btc_bool btc_key_sign_hash ( const btc_key privkey,
const uint8_t *  hash,
unsigned char *  sigout,
size_t *  outlen 
)

Definition at line 130 of file ecc_key.c.

References ecc_sign(), and btc_key_::privkey.

Referenced by btc_privkey_verify_pubkey().

131 {
132  return ecc_sign(privkey->privkey, hash, sigout, outlen);
133 }
btc_bool ecc_sign(const uint8_t *private_key, const uint8_t *hash, unsigned char *sigder, size_t *outlen)
uint8_t privkey[BTC_ECKEY_PKEY_LENGTH]
Definition: ecc_key.h:41
void btc_privkey_cleanse ( btc_key privkey)

Definition at line 54 of file ecc_key.c.

References BTC_ECKEY_PKEY_LENGTH, and btc_key_::privkey.

55 {
56  memset(&privkey->privkey, 0, BTC_ECKEY_PKEY_LENGTH);
57 }
#define BTC_ECKEY_PKEY_LENGTH
Definition: btc.h:70
uint8_t privkey[BTC_ECKEY_PKEY_LENGTH]
Definition: ecc_key.h:41
void btc_privkey_gen ( btc_key privkey)

Definition at line 60 of file ecc_key.c.

References BTC_ECKEY_PKEY_LENGTH, ecc_verify_privatekey(), btc_key_::privkey, and random_bytes().

61 {
62  if (privkey == NULL)
63  return;
64 
65  do {
67  } while (ecc_verify_privatekey(privkey->privkey) == 0);
68 }
#define BTC_ECKEY_PKEY_LENGTH
Definition: btc.h:70
btc_bool random_bytes(uint8_t *buf, uint32_t len, const uint8_t update_seed)
Definition: random.c:57
btc_bool ecc_verify_privatekey(const uint8_t *private_key)
verifies a given 32byte key
uint8_t privkey[BTC_ECKEY_PKEY_LENGTH]
Definition: ecc_key.h:41
void btc_privkey_init ( btc_key privkey)

Definition at line 42 of file ecc_key.c.

References BTC_ECKEY_PKEY_LENGTH, and btc_key_::privkey.

43 {
44  memset(&privkey->privkey, 0, BTC_ECKEY_PKEY_LENGTH);
45 }
#define BTC_ECKEY_PKEY_LENGTH
Definition: btc.h:70
uint8_t privkey[BTC_ECKEY_PKEY_LENGTH]
Definition: ecc_key.h:41
btc_bool btc_privkey_is_valid ( btc_key privkey)

Definition at line 48 of file ecc_key.c.

References ecc_verify_privatekey(), and btc_key_::privkey.

49 {
50  return ecc_verify_privatekey(privkey->privkey);
51 }
btc_bool ecc_verify_privatekey(const uint8_t *private_key)
verifies a given 32byte key
uint8_t privkey[BTC_ECKEY_PKEY_LENGTH]
Definition: ecc_key.h:41
btc_bool btc_privkey_verify_pubkey ( btc_key privkey,
btc_pubkey pubkey 
)

Definition at line 71 of file ecc_key.c.

References btc_hash(), btc_key_sign_hash(), btc_pubkey_verify_sig(), and random_bytes().

72 {
73  uint8_t rnddata[32], hash[32];
74  random_bytes(rnddata, 32, 0);
75  btc_hash(rnddata, 32, hash);
76 
77  unsigned char sig[74];
78  size_t siglen = 74;
79 
80  if (!btc_key_sign_hash(privkey, hash, sig, &siglen))
81  return false;
82 
83  return btc_pubkey_verify_sig(pubkey, hash, sig, siglen);
84 }
btc_bool btc_pubkey_verify_sig(const btc_pubkey *pubkey, const uint8_t *hash, unsigned char *sigder, int len)
Definition: ecc_key.c:136
btc_bool random_bytes(uint8_t *buf, uint32_t len, const uint8_t update_seed)
Definition: random.c:57
btc_bool btc_key_sign_hash(const btc_key *privkey, const uint8_t *hash, unsigned char *sigout, size_t *outlen)
Definition: ecc_key.c:130
void btc_hash(const unsigned char *datain, size_t length, uint256 hashout)
Definition: sha2.c:1026
void btc_pubkey_cleanse ( btc_pubkey pubkey)

Definition at line 103 of file ecc_key.c.

References BTC_ECKEY_UNCOMPRESSED_LENGTH, and btc_pubkey_::pubkey.

104 {
105  if (pubkey == NULL)
106  return;
107 
108  memset(pubkey->pubkey, 0, BTC_ECKEY_UNCOMPRESSED_LENGTH);
109 }
#define BTC_ECKEY_UNCOMPRESSED_LENGTH
Definition: btc.h:67
uint8_t pubkey[BTC_ECKEY_UNCOMPRESSED_LENGTH]
Definition: ecc_key.h:47
void btc_pubkey_from_key ( btc_key privkey,
btc_pubkey pubkey_inout 
)

Definition at line 118 of file ecc_key.c.

References BTC_ECKEY_COMPRESSED_LENGTH, btc_pubkey_::compressed, ecc_get_pubkey(), btc_key_::privkey, and btc_pubkey_::pubkey.

119 {
120  if (pubkey_inout == NULL || privkey == NULL)
121  return;
122 
123  size_t in_out_len = BTC_ECKEY_COMPRESSED_LENGTH;
124 
125  ecc_get_pubkey(privkey->privkey, pubkey_inout->pubkey, &in_out_len, true);
126  pubkey_inout->compressed = true;
127 }
void ecc_get_pubkey(const uint8_t *private_key, uint8_t *public_key, size_t *in_outlen, btc_bool compressed)
get public key from given private key
#define BTC_ECKEY_COMPRESSED_LENGTH
Definition: btc.h:68
btc_bool compressed
Definition: ecc_key.h:46
uint8_t pubkey[BTC_ECKEY_UNCOMPRESSED_LENGTH]
Definition: ecc_key.h:47
uint8_t privkey[BTC_ECKEY_PKEY_LENGTH]
Definition: ecc_key.h:41
void btc_pubkey_get_hash160 ( const btc_pubkey pubkey,
uint8_t *  hash160 
)

Definition at line 112 of file ecc_key.c.

References BTC_ECKEY_COMPRESSED_LENGTH, BTC_ECKEY_UNCOMPRESSED_LENGTH, btc_pubkey_::compressed, btc_pubkey_::pubkey, and ripemd160().

Referenced by btc_tx_add_p2pkh_out().

113 {
115 }
#define BTC_ECKEY_COMPRESSED_LENGTH
Definition: btc.h:68
#define BTC_ECKEY_UNCOMPRESSED_LENGTH
Definition: btc.h:67
void ripemd160(const uint8_t *msg, uint32_t msg_len, uint8_t *hash)
Definition: ripemd160.c:292
btc_bool compressed
Definition: ecc_key.h:46
uint8_t pubkey[BTC_ECKEY_UNCOMPRESSED_LENGTH]
Definition: ecc_key.h:47
void btc_pubkey_init ( btc_pubkey pubkey)

Definition at line 87 of file ecc_key.c.

References BTC_ECKEY_UNCOMPRESSED_LENGTH, btc_pubkey_::compressed, and btc_pubkey_::pubkey.

88 {
89  if (pubkey == NULL)
90  return;
91 
92  memset(pubkey->pubkey, 0, BTC_ECKEY_UNCOMPRESSED_LENGTH);
93  pubkey->compressed = false;
94 }
#define BTC_ECKEY_UNCOMPRESSED_LENGTH
Definition: btc.h:67
btc_bool compressed
Definition: ecc_key.h:46
uint8_t pubkey[BTC_ECKEY_UNCOMPRESSED_LENGTH]
Definition: ecc_key.h:47
btc_bool btc_pubkey_is_valid ( btc_pubkey pubkey)

Definition at line 97 of file ecc_key.c.

References btc_pubkey_::compressed, ecc_verify_pubkey(), and btc_pubkey_::pubkey.

98 {
99  return ecc_verify_pubkey(pubkey->pubkey, pubkey->compressed);
100 }
btc_bool ecc_verify_pubkey(const uint8_t *public_key, btc_bool compressed)
verifies a given public key (compressed[33] or uncompressed[65] bytes)
btc_bool compressed
Definition: ecc_key.h:46
uint8_t pubkey[BTC_ECKEY_UNCOMPRESSED_LENGTH]
Definition: ecc_key.h:47
btc_bool btc_pubkey_verify_sig ( const btc_pubkey pubkey,
const uint8_t *  hash,
unsigned char *  sigder,
int  len 
)

Definition at line 136 of file ecc_key.c.

References btc_pubkey_::compressed, ecc_verify_sig(), and btc_pubkey_::pubkey.

Referenced by btc_privkey_verify_pubkey().

137 {
138  return ecc_verify_sig(pubkey->pubkey, pubkey->compressed, hash, sigder, len);
139 }
size_t len
Definition: buffer.h:16
btc_bool compressed
Definition: ecc_key.h:46
uint8_t pubkey[BTC_ECKEY_UNCOMPRESSED_LENGTH]
Definition: ecc_key.h:47
btc_bool ecc_verify_sig(const uint8_t *public_key, btc_bool compressed, const uint8_t *hash, unsigned char *sigder, size_t siglen)