1
1
// Copyright (c) 2009-2016 The Bitcoin Core developers
2
+ // Copyright (c) 2017 The Zcash developers
2
3
// Distributed under the MIT software license, see the accompanying
3
4
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5
@@ -18,41 +19,46 @@ static secp256k1_context* secp256k1_context_sign = NULL;
18
19
/* * These functions are taken from the libsecp256k1 distribution and are very ugly. */
19
20
static int ec_privkey_import_der (const secp256k1_context* ctx, unsigned char *out32, const unsigned char *privkey, size_t privkeylen) {
20
21
const unsigned char *end = privkey + privkeylen;
21
- int lenb = 0 ;
22
- int len = 0 ;
22
+ size_t lenb = 0 ;
23
+ size_t len = 0 ;
23
24
memset (out32, 0 , 32 );
24
25
/* sequence header */
25
- if (end < privkey+ 1 || *privkey != 0x30 ) {
26
+ if (end - privkey < 1 || *privkey != 0x30u ) {
26
27
return 0 ;
27
28
}
28
29
privkey++;
29
30
/* sequence length constructor */
30
- if (end < privkey+ 1 || !(*privkey & 0x80 )) {
31
+ if (end - privkey < 1 || !(*privkey & 0x80u )) {
31
32
return 0 ;
32
33
}
33
- lenb = *privkey & ~0x80 ; privkey++;
34
+ lenb = *privkey & ~0x80u ; privkey++;
34
35
if (lenb < 1 || lenb > 2 ) {
35
36
return 0 ;
36
37
}
37
- if (end < privkey+ lenb) {
38
+ if (end - privkey < lenb) {
38
39
return 0 ;
39
40
}
40
41
/* sequence length */
41
- len = privkey[lenb-1 ] | (lenb > 1 ? privkey[lenb-2 ] << 8 : 0 );
42
+ len = privkey[lenb-1 ] | (lenb > 1 ? privkey[lenb-2 ] << 8 : 0u );
42
43
privkey += lenb;
43
- if (end < privkey+ len) {
44
+ if (end - privkey < len) {
44
45
return 0 ;
45
46
}
46
47
/* sequence element 0: version number (=1) */
47
- if (end < privkey+ 3 || privkey[0 ] != 0x02 || privkey[1 ] != 0x01 || privkey[2 ] != 0x01 ) {
48
+ if (end - privkey < 3 || privkey[0 ] != 0x02u || privkey[1 ] != 0x01u || privkey[2 ] != 0x01u ) {
48
49
return 0 ;
49
50
}
50
51
privkey += 3 ;
51
52
/* sequence element 1: octet string, up to 32 bytes */
52
- if (end < privkey+ 2 || privkey[0 ] != 0x04 || privkey[ 1 ] > 0x20 || end < privkey+ 2 +privkey[ 1 ] ) {
53
+ if (end - privkey < 2 || privkey[0 ] != 0x04u ) {
53
54
return 0 ;
54
55
}
55
- memcpy (out32 + 32 - privkey[1 ], privkey + 2 , privkey[1 ]);
56
+ size_t oslen = privkey[1 ];
57
+ privkey += 2 ;
58
+ if (oslen > 32 || end - privkey < oslen) {
59
+ return 0 ;
60
+ }
61
+ memcpy (out32 + (32 - oslen), privkey, oslen);
56
62
if (!secp256k1_ec_seckey_verify (ctx, out32)) {
57
63
memset (out32, 0 , 32 );
58
64
return 0 ;
@@ -219,10 +225,10 @@ bool CKey::Derive(CKey& keyChild, ChainCode &ccChild, unsigned int nChild, const
219
225
std::vector<unsigned char , secure_allocator<unsigned char >> vout (64 );
220
226
if ((nChild >> 31 ) == 0 ) {
221
227
CPubKey pubkey = GetPubKey ();
222
- assert (pubkey.begin () + 33 == pubkey. end () );
228
+ assert (pubkey.size () == 33 );
223
229
BIP32Hash (cc, nChild, *pubkey.begin (), pubkey.begin ()+1 , vout.data ());
224
230
} else {
225
- assert (begin () + 32 == end () );
231
+ assert (size () == 32 );
226
232
BIP32Hash (cc, nChild, 0 , begin (), vout.data ());
227
233
}
228
234
memcpy (ccChild.begin (), vout.data ()+32 , 32 );
0 commit comments