libbtc
bitcoinclibrary
ecc_key.c
Go to the documentation of this file.
1 /*
2 
3  The MIT License (MIT)
4 
5  Copyright (c) 2015 Jonas Schnelli
6 
7  Permission is hereby granted, free of charge, to any person obtaining
8  a copy of this software and associated documentation files (the "Software"),
9  to deal in the Software without restriction, including without limitation
10  the rights to use, copy, modify, merge, publish, distribute, sublicense,
11  and/or sell copies of the Software, and to permit persons to whom the
12  Software is furnished to do so, subject to the following conditions:
13 
14  The above copyright notice and this permission notice shall be included
15  in all copies or substantial portions of the Software.
16 
17  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
21  OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22  ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23  OTHER DEALINGS IN THE SOFTWARE.
24 
25 */
26 
27 #include "btc/ecc_key.h"
28 
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <assert.h>
33 
34 #include "btc/ecc.h"
35 #include "btc/hash.h"
36 
37 #include "random.h"
38 #include "ripemd160.h"
39 #include "utils.h"
40 
41 
42 void btc_privkey_init(btc_key* privkey)
43 {
44  memset(&privkey->privkey, 0, BTC_ECKEY_PKEY_LENGTH);
45 }
46 
47 
49 {
50  return ecc_verify_privatekey(privkey->privkey);
51 }
52 
53 
55 {
56  memset(&privkey->privkey, 0, BTC_ECKEY_PKEY_LENGTH);
57 }
58 
59 
60 void btc_privkey_gen(btc_key* privkey)
61 {
62  if (privkey == NULL)
63  return;
64 
65  do {
67  } while (ecc_verify_privatekey(privkey->privkey) == 0);
68 }
69 
70 
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 }
85 
86 
88 {
89  if (pubkey == NULL)
90  return;
91 
92  memset(pubkey->pubkey, 0, BTC_ECKEY_UNCOMPRESSED_LENGTH);
93  pubkey->compressed = false;
94 }
95 
96 
98 {
99  return ecc_verify_pubkey(pubkey->pubkey, pubkey->compressed);
100 }
101 
102 
104 {
105  if (pubkey == NULL)
106  return;
107 
108  memset(pubkey->pubkey, 0, BTC_ECKEY_UNCOMPRESSED_LENGTH);
109 }
110 
111 
112 void btc_pubkey_get_hash160(const btc_pubkey* pubkey, uint8_t *hash160)
113 {
115 }
116 
117 
118 void btc_pubkey_from_key(btc_key* privkey, btc_pubkey* pubkey_inout)
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 }
128 
129 
130 btc_bool btc_key_sign_hash(const btc_key* privkey, const uint8_t* hash, unsigned char* sigout, size_t* outlen)
131 {
132  return ecc_sign(privkey->privkey, hash, sigout, outlen);
133 }
134 
135 
136 btc_bool btc_pubkey_verify_sig(const btc_pubkey* pubkey, const uint8_t* hash, unsigned char* sigder, int len)
137 {
138  return ecc_verify_sig(pubkey->pubkey, pubkey->compressed, hash, sigder, len);
139 }
uint8_t btc_bool
Definition: btc.h:34
btc_bool btc_pubkey_verify_sig(const btc_pubkey *pubkey, const uint8_t *hash, unsigned char *sigder, int len)
Definition: ecc_key.c:136
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
btc_bool btc_privkey_is_valid(btc_key *privkey)
Definition: ecc_key.c:48
btc_bool btc_privkey_verify_pubkey(btc_key *privkey, btc_pubkey *pubkey)
Definition: ecc_key.c:71
#define BTC_ECKEY_PKEY_LENGTH
Definition: btc.h:70
void btc_privkey_init(btc_key *privkey)
Definition: ecc_key.c:42
btc_bool random_bytes(uint8_t *buf, uint32_t len, const uint8_t update_seed)
Definition: random.c:57
#define BTC_ECKEY_COMPRESSED_LENGTH
Definition: btc.h:68
void btc_pubkey_cleanse(btc_pubkey *pubkey)
Definition: ecc_key.c:103
void btc_privkey_gen(btc_key *privkey)
Definition: ecc_key.c:60
btc_bool ecc_verify_pubkey(const uint8_t *public_key, btc_bool compressed)
verifies a given public key (compressed[33] or uncompressed[65] bytes)
#define BTC_ECKEY_UNCOMPRESSED_LENGTH
Definition: btc.h:67
size_t len
Definition: buffer.h:16
void btc_pubkey_init(btc_pubkey *pubkey)
Definition: ecc_key.c:87
void btc_privkey_cleanse(btc_key *privkey)
Definition: ecc_key.c:54
void ripemd160(const uint8_t *msg, uint32_t msg_len, uint8_t *hash)
Definition: ripemd160.c:292
btc_bool btc_pubkey_is_valid(btc_pubkey *pubkey)
Definition: ecc_key.c:97
btc_bool ecc_sign(const uint8_t *private_key, const uint8_t *hash, unsigned char *sigder, size_t *outlen)
void btc_pubkey_from_key(btc_key *privkey, btc_pubkey *pubkey_inout)
Definition: ecc_key.c:118
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_pubkey_get_hash160(const btc_pubkey *pubkey, uint8_t *hash160)
Definition: ecc_key.c:112
btc_bool compressed
Definition: ecc_key.h:46
void btc_hash(const unsigned char *datain, size_t length, uint256 hashout)
Definition: sha2.c:1026
uint8_t pubkey[BTC_ECKEY_UNCOMPRESSED_LENGTH]
Definition: ecc_key.h:47
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 ecc_verify_sig(const uint8_t *public_key, btc_bool compressed, const uint8_t *hash, unsigned char *sigder, size_t siglen)