forked from petertodd/hardware-bitcoin-wallet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wallet.c
1694 lines (1591 loc) · 42 KB
/
wallet.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/** \file wallet.c
*
* \brief Manages the storage and generation of Bitcoin addresses.
*
* Addresses are stored in wallets, which can be
* "loaded" or "unloaded". A loaded wallet can have operations (eg. new
* address) performed on it, whereas an unloaded wallet can only sit dormant.
* Addresses aren't actually physically stored in non-volatile storage;
* rather a seed for a deterministic private key generation algorithm is
* stored and private keys are generated when they are needed. This means
* that obtaining an address is a slow operation (requiring a point
* multiply), so the host should try to remember all public keys and
* addresses. The advantage of not storing addresses is that very little
* non-volatile storage space is needed per
* wallet - only #WALLET_RECORD_LENGTH bytes per wallet.
*
* Wallets can be encrypted or unencrypted. Actually, technically, all
* wallets are encrypted. However, wallets marked as "unencrypted" are
* encrypted using an encryption key consisting of all zeroes. This purely
* semantic definition was done to avoid having to insert special cases
* everytime encrypted storage needed to be accessed.
*
* This file is licensed as described by the file LICENCE.
*/
#ifdef TEST
#include <stdlib.h>
#include <stdio.h>
#endif // #ifdef TEST
#ifdef TEST_WALLET
#include "test_helpers.h"
#endif // #ifdef TEST_WALLET
#include "common.h"
#include "endian.h"
#include "wallet.h"
#include "prandom.h"
#include "sha256.h"
#include "ripemd160.h"
#include "ecdsa.h"
#include "hwinterface.h"
#include "xex.h"
#include "bignum256.h"
/** The most recent error to occur in a function in this file,
* or #WALLET_NO_ERROR if no error occurred in the most recent function
* call. See #WalletErrorsEnum for possible values. */
static WalletErrors last_error;
/** This will be 0 if a wallet is not currently loaded. This will be non-zero
* if a wallet is currently loaded. */
static uint8_t wallet_loaded;
/** This will only be valid if a wallet is loaded. It contains a cache of the
* number of addresses in the currently loaded wallet. */
static uint32_t num_addresses;
#ifdef TEST
/** The file to perform test non-volatile I/O on. */
FILE *wallet_test_file;
#endif // #ifdef TEST
/** Find out what the most recent error which occurred in any wallet function
* was. If no error occurred in the most recent wallet function that was
* called, this will return #WALLET_NO_ERROR.
* \return See #WalletErrorsEnum for possible values.
*/
WalletErrors walletGetLastError(void)
{
return last_error;
}
#ifdef TEST
void initWalletTest(void)
{
wallet_test_file = fopen("wallet_test.bin", "w+b");
if (wallet_test_file == NULL)
{
printf("Could not open \"wallet_test.bin\" for writing\n");
exit(1);
}
}
#endif // #ifdef TEST
#ifdef TEST_WALLET
/** Maximum of addresses which can be stored in storage area - for testing
* only. This should actually be the capacity of the wallet, since one
* of the tests is to see what happens when the wallet is full. */
#define MAX_TESTING_ADDRESSES 7
#endif // #ifdef TEST_WALLET
/**
* \defgroup WalletStorageFormat Wallet storage format
*
* Wallets are stored as sequential records in non-volatile
* storage. Each record is 160 bytes. If the wallet is encrypted, the
* first 48 bytes are unencrypted and the last 112 bytes are encrypted.
* The contents of each record:
* - 4 bytes: little endian version
* - 0x00000000: nothing here
* - 0x00000001: v0.1 wallet format (not supported)
* - 0x00000002: unencrypted wallet
* - 0x00000003: encrypted wallet, host provides key
* - 4 bytes: reserved
* - 40 bytes: name of wallet (padded with spaces)
* - 4 bytes: little endian number of addresses
* - 8 bytes: random data
* - 4 bytes: reserved
* - 64 bytes: seed for deterministic private key generator
* - 32 bytes: SHA-256 of everything except number of addresses and this
* @{
*/
/** Length of a record.
* \warning This must be a multiple of 32 in order for newWallet() to
* work properly.
* \warning This must also be a multiple of 16, since the block size of
* AES is 128 bits.
*/
#define WALLET_RECORD_LENGTH 160
/** The offset where encryption starts. The contents of a record before this
* offset are not encrypted, while the contents of a record at and after this
* offset are encrypted.
* \warning This must also be a multiple of 16, since the block size of
* AES is 128 bits.
*/
#define ENCRYPT_START 48
/** Offset within a record where version is. */
#define OFFSET_VERSION 0
/** Offset within a record where first reserved area is. */
#define OFFSET_RESERVED1 4
/** Offset within a record where name is. */
#define OFFSET_NAME 8
/** Offset within a record where number of addresses is. */
#define OFFSET_NUM_ADDRESSES 48
/** Offset within a record where some random data is. */
#define OFFSET_NONCE1 52
/** Offset within a record where second reserved area is. */
#define OFFSET_RESERVED2 60
/** Offset within a record where deterministic private key generator seed
* is. */
#define OFFSET_SEED 64
/** Offset within a record where some wallet checksum is. */
#define OFFSET_CHECKSUM 128
/** Version number which means "nothing here". */
#define VERSION_NOTHING_THERE 0x00000000
/** Version number which means "wallet is not encrypted".
* \warning A wallet which uses an encryption key consisting of
* all zeroes (see isEncryptionKeyNonZero()) is considered to be
* unencrypted.
*/
#define VERSION_UNENCRYPTED 0x00000002
/** Version number which means "wallet is encrypted". */
#define VERSION_IS_ENCRYPTED 0x00000003
/**@}*/
/** Calculate the checksum (SHA-256 hash) of the wallet's contents. The
* wallet checksum is invariant to the number of addresses in the wallet.
* This invariance is necessary to avoid having to rewrite the wallet
* checksum every time a new address is generated.
* \param hash The resulting SHA-256 hash will be written here. This must
* be a byte array with space for 32 bytes.
* \return See #NonVolatileReturnEnum.
*/
static NonVolatileReturn calculateWalletChecksum(uint8_t *hash)
{
uint16_t i;
uint8_t buffer[4];
HashState hs;
NonVolatileReturn r;
sha256Begin(&hs);
for (i = 0; i < WALLET_RECORD_LENGTH; i = (uint16_t)(i + 4))
{
// Skip number of addresses and checksum.
if (i == OFFSET_NUM_ADDRESSES)
{
i = (uint16_t)(i + 4);
}
if (i == OFFSET_CHECKSUM)
{
i = (uint16_t)(i + 32);
}
if (i < WALLET_RECORD_LENGTH)
{
// "The first 48 bytes are unencrypted, the last 112 bytes are
// encrypted."
if (i < ENCRYPT_START)
{
r = nonVolatileRead(buffer, i, 4);
}
else
{
r = encryptedNonVolatileRead(buffer, i, 4);
}
if (r != NV_NO_ERROR)
{
return r;
}
sha256WriteByte(&hs, buffer[0]);
sha256WriteByte(&hs, buffer[1]);
sha256WriteByte(&hs, buffer[2]);
sha256WriteByte(&hs, buffer[3]);
}
}
sha256Finish(&hs);
writeHashToByteArray(hash, &hs, 1);
return NV_NO_ERROR;
}
/** Initialise wallet (load it if it's there).
* \return #WALLET_NO_ERROR on success, or one of #WalletErrorsEnum if an
* error occurred.
*/
WalletErrors initWallet(void)
{
uint8_t buffer[32];
uint8_t hash[32];
uint32_t version;
wallet_loaded = 0;
// Read version.
if (nonVolatileRead(buffer, OFFSET_VERSION, 4) != NV_NO_ERROR)
{
last_error = WALLET_READ_ERROR;
return last_error;
}
version = readU32LittleEndian(buffer);
if ((version != VERSION_UNENCRYPTED) && (version != VERSION_IS_ENCRYPTED))
{
last_error = WALLET_NOT_THERE;
return last_error;
}
// Calculate checksum and check that it matches.
if (calculateWalletChecksum(hash) != NV_NO_ERROR)
{
last_error = WALLET_READ_ERROR;
return last_error;
}
if (encryptedNonVolatileRead(buffer, OFFSET_CHECKSUM, 32) != NV_NO_ERROR)
{
last_error = WALLET_READ_ERROR;
return last_error;
}
if (bigCompare(buffer, hash) != BIGCMP_EQUAL)
{
last_error = WALLET_NOT_THERE;
return last_error;
}
// Read number of addresses.
if (encryptedNonVolatileRead(buffer, OFFSET_NUM_ADDRESSES, 4) != NV_NO_ERROR)
{
last_error = WALLET_READ_ERROR;
return last_error;
}
num_addresses = readU32LittleEndian(buffer);
wallet_loaded = 1;
last_error = WALLET_NO_ERROR;
return last_error;
}
/** Unload wallet, so that it cannot be used until initWallet() is called.
* \return #WALLET_NO_ERROR on success, or one of #WalletErrorsEnum if an
* error occurred.
*/
WalletErrors uninitWallet(void)
{
wallet_loaded = 0;
num_addresses = 0;
last_error = WALLET_NO_ERROR;
return last_error;
}
/** Sanitise (clear) a selected area of non-volatile storage. This will clear
* the area between start (inclusive) and end (exclusive).
* \param start The first address which will be cleared.
* \param end One byte past the last address which will be cleared.
* \return #WALLET_NO_ERROR on success, or one of #WalletErrorsEnum if an
* error occurred. This will still return #WALLET_NO_ERROR even if
* end is an address beyond the end of the non-volatile storage area.
* This is done so that using start = 0 and end = 0xffffffff will
* clear the entire non-volatile storage area.
* \warning start and end must be a multiple of 32 (unless start is 0 and
* end is 0xffffffff).
*/
WalletErrors sanitiseNonVolatileStorage(uint32_t start, uint32_t end)
{
uint8_t buffer[32];
uint32_t address;
NonVolatileReturn r;
uint8_t pass;
r = NV_NO_ERROR;
for (pass = 0; pass < 4; pass++)
{
address = start;
r = NV_NO_ERROR;
while ((r == NV_NO_ERROR) && (address < end))
{
if (pass == 0)
{
memset(buffer, 0, sizeof(buffer));
}
else if (pass == 1)
{
memset(buffer, 0xff, sizeof(buffer));
}
else
{
getRandom256(buffer);
}
r = nonVolatileWrite(buffer, address, 32);
nonVolatileFlush();
address += 32;
}
if ((r != NV_INVALID_ADDRESS) && (r != NV_NO_ERROR))
{
// Uh oh, probably an I/O error.
break;
}
} // end for (pass = 0; pass < 4; pass++)
if ((r == NV_INVALID_ADDRESS) || (r == NV_NO_ERROR))
{
// Write VERSION_NOTHING_THERE to all possible locations of the
// version field. This ensures that a wallet won't accidentally
// (1 in 2 ^ 31 chance) be recognised as a valid wallet by
// getWalletInfo().
writeU32LittleEndian(buffer, VERSION_NOTHING_THERE);
r = nonVolatileWrite(buffer, OFFSET_VERSION, 4);
if (r == NV_NO_ERROR)
{
last_error = WALLET_NO_ERROR;
}
else
{
last_error = WALLET_WRITE_ERROR;
}
}
else
{
last_error = WALLET_WRITE_ERROR;
}
return last_error;
}
/** Writes 4 byte wallet version. This is in its own function because
* it's used by both newWallet() and changeEncryptionKey().
* \return See #NonVolatileReturnEnum.
*/
static NonVolatileReturn writeWalletVersion(void)
{
uint8_t buffer[4];
if (isEncryptionKeyNonZero())
{
writeU32LittleEndian(buffer, VERSION_IS_ENCRYPTED);
}
else
{
writeU32LittleEndian(buffer, VERSION_UNENCRYPTED);
}
return nonVolatileWrite(buffer, OFFSET_VERSION, 4);
}
/** Writes wallet checksum. This is in its own function because
* it's used by newWallet(), changeEncryptionKey() and changeWalletName().
* \return #WALLET_NO_ERROR on success, or one of #WalletErrorsEnum if an
* error occurred.
*/
static WalletErrors writeWalletChecksum(void)
{
uint8_t hash[32];
if (calculateWalletChecksum(hash) != NV_NO_ERROR)
{
return WALLET_READ_ERROR;
}
if (encryptedNonVolatileWrite(hash, OFFSET_CHECKSUM, 32) != NV_NO_ERROR)
{
return WALLET_WRITE_ERROR;
}
return WALLET_NO_ERROR;
}
/** Create new wallet. A brand new wallet contains no addresses and should
* have a unique, unpredictable deterministic private key generation seed.
* \param name Should point to #NAME_LENGTH bytes (padded with spaces if
* necessary) containing the desired name of the wallet.
* \return #WALLET_NO_ERROR on success, or one of #WalletErrorsEnum if an
* error occurred. If this returns #WALLET_NO_ERROR, then the
* wallet will also be loaded.
* \warning This will erase the current one.
*/
WalletErrors newWallet(uint8_t *name)
{
uint8_t buffer[32];
WalletErrors r;
// Erase all traces of the existing wallet.
r = sanitiseNonVolatileStorage(0, WALLET_RECORD_LENGTH);
if (r != WALLET_NO_ERROR)
{
last_error = r;
return last_error;
}
// Write version.
if (writeWalletVersion() != NV_NO_ERROR)
{
last_error = WALLET_WRITE_ERROR;
return last_error;
}
// Write reserved area 1.
writeU32LittleEndian(buffer, 0);
if (nonVolatileWrite(buffer, OFFSET_RESERVED1, 4) != NV_NO_ERROR)
{
last_error = WALLET_WRITE_ERROR;
return last_error;
}
// Write name of wallet.
if (nonVolatileWrite(name, OFFSET_NAME, NAME_LENGTH) != NV_NO_ERROR)
{
last_error = WALLET_WRITE_ERROR;
return last_error;
}
// Write number of addresses.
writeU32LittleEndian(buffer, 0);
if (encryptedNonVolatileWrite(buffer, OFFSET_NUM_ADDRESSES, 4) != NV_NO_ERROR)
{
last_error = WALLET_WRITE_ERROR;
return last_error;
}
// Write nonce 1.
getRandom256(buffer);
if (encryptedNonVolatileWrite(buffer, OFFSET_NONCE1, 8) != NV_NO_ERROR)
{
last_error = WALLET_WRITE_ERROR;
return last_error;
}
// Write reserved area 2.
writeU32LittleEndian(buffer, 0);
if (encryptedNonVolatileWrite(buffer, OFFSET_RESERVED2, 4) != NV_NO_ERROR)
{
last_error = WALLET_WRITE_ERROR;
return last_error;
}
// Write seed for deterministic address generator.
getRandom256(buffer);
if (encryptedNonVolatileWrite(buffer, OFFSET_SEED, 32) != NV_NO_ERROR)
{
last_error = WALLET_WRITE_ERROR;
return last_error;
}
getRandom256(buffer);
if (encryptedNonVolatileWrite(buffer, OFFSET_SEED + 32, 32) != NV_NO_ERROR)
{
last_error = WALLET_WRITE_ERROR;
return last_error;
}
nonVolatileFlush();
// Write checksum.
r = writeWalletChecksum();
if (r != WALLET_NO_ERROR)
{
last_error = r;
return last_error;
}
nonVolatileFlush();
last_error = initWallet();
return last_error;
}
/** Generate a new address using the deterministic private key generator.
* \param out_address The new address will be written here (if everything
* goes well). This must be a byte array with space for
* 20 bytes.
* \param out_public_key The public key corresponding to the new address will
* be written here (if everything goes well).
* \return The address handle of the new address on success,
* or #BAD_ADDRESS_HANDLE if an error occurred.
* Use walletGetLastError() to get more detail about an error.
*/
AddressHandle makeNewAddress(uint8_t *out_address, PointAffine *out_public_key)
{
uint8_t buffer[4];
if (!wallet_loaded)
{
last_error = WALLET_NOT_THERE;
return BAD_ADDRESS_HANDLE;
}
#ifdef TEST_WALLET
if (num_addresses == MAX_TESTING_ADDRESSES)
#else
if (num_addresses == MAX_ADDRESSES)
#endif // #ifdef TEST_WALLET
{
last_error = WALLET_FULL;
return BAD_ADDRESS_HANDLE;
}
num_addresses++;
writeU32LittleEndian(buffer, num_addresses);
if (encryptedNonVolatileWrite(buffer, OFFSET_NUM_ADDRESSES, 4) != NV_NO_ERROR)
{
last_error = WALLET_WRITE_ERROR;
return BAD_ADDRESS_HANDLE;
}
last_error = getAddressAndPublicKey(out_address, out_public_key, num_addresses);
if (last_error != WALLET_NO_ERROR)
{
return BAD_ADDRESS_HANDLE;
}
else
{
return num_addresses;
}
}
/** Given an address handle, use the deterministic private key
* generator to generate the address and public key associated
* with that address handle.
* \param out_address The address will be written here (if everything
* goes well). This must be a byte array with space for
* 20 bytes.
* \param out_public_key The public key corresponding to the address will
* be written here (if everything goes well).
* \param ah The address handle to obtain the address/public key of.
* \return #WALLET_NO_ERROR on success, or one of #WalletErrorsEnum if an
* error occurred.
*/
WalletErrors getAddressAndPublicKey(uint8_t *out_address, PointAffine *out_public_key, AddressHandle ah)
{
uint8_t buffer[32];
HashState hs;
WalletErrors r;
uint8_t i;
if (!wallet_loaded)
{
last_error = WALLET_NOT_THERE;
return last_error;
}
if (num_addresses == 0)
{
last_error = WALLET_EMPTY;
return last_error;
}
if ((ah == 0) || (ah > num_addresses) || (ah == BAD_ADDRESS_HANDLE))
{
last_error = WALLET_INVALID_HANDLE;
return last_error;
}
// Calculate private key.
r = getPrivateKey(buffer, ah);
if (r != WALLET_NO_ERROR)
{
last_error = r;
return r;
}
// Calculate public key.
setToG(out_public_key);
pointMultiply(out_public_key, buffer);
// Calculate address. The Bitcoin convention is to hash the public key in
// big-endian format, which is why the counters run backwards in the next
// two loops.
sha256Begin(&hs);
sha256WriteByte(&hs, 0x04);
for (i = 32; i--; )
{
sha256WriteByte(&hs, out_public_key->x[i]);
}
for (i = 32; i--; )
{
sha256WriteByte(&hs, out_public_key->y[i]);
}
sha256Finish(&hs);
writeHashToByteArray(buffer, &hs, 1);
ripemd160Begin(&hs);
for (i = 0; i < 32; i++)
{
ripemd160WriteByte(&hs, buffer[i]);
}
ripemd160Finish(&hs);
writeHashToByteArray(buffer, &hs, 1);
memcpy(out_address, buffer, 20);
last_error = WALLET_NO_ERROR;
return last_error;
}
/** Get the current number of addresses in a wallet.
* \return The current number of addresses on success, or 0 if an error
* occurred. Use walletGetLastError() to get more detail about
* an error.
*/
uint32_t getNumAddresses(void)
{
if (!wallet_loaded)
{
last_error = WALLET_NOT_THERE;
return 0;
}
if (num_addresses == 0)
{
last_error = WALLET_EMPTY;
return 0;
}
else
{
last_error = WALLET_NO_ERROR;
return num_addresses;
}
}
/** Given an address handle, use the deterministic private key
* generator to generate the private key associated with that address handle.
* \param out The private key will be written here (if everything goes well).
* This must be a byte array with space for 32 bytes.
* \param ah The address handle to obtain the private key of.
* \return #WALLET_NO_ERROR on success, or one of #WalletErrorsEnum if an
* error occurred.
*/
WalletErrors getPrivateKey(uint8_t *out, AddressHandle ah)
{
uint8_t seed[64];
if (!wallet_loaded)
{
last_error = WALLET_NOT_THERE;
return last_error;
}
if (num_addresses == 0)
{
last_error = WALLET_EMPTY;
return last_error;
}
if ((ah == 0) || (ah > num_addresses) || (ah == BAD_ADDRESS_HANDLE))
{
last_error = WALLET_INVALID_HANDLE;
return last_error;
}
if (encryptedNonVolatileRead(seed, OFFSET_SEED, 64) != NV_NO_ERROR)
{
last_error = WALLET_READ_ERROR;
return last_error;
}
generateDeterministic256(out, seed, ah);
last_error = WALLET_NO_ERROR;
return last_error;
}
/** Change the encryption key of a wallet.
* \param new_key A byte array of #WALLET_ENCRYPTION_KEY_LENGTH bytes
* specifying the new encryption key.
* An encryption key consisting of all zeroes is interpreted
* as meaning "no encryption".
* \return #WALLET_NO_ERROR on success, or one of #WalletErrorsEnum if an
* error occurred.
*/
WalletErrors changeEncryptionKey(uint8_t *new_key)
{
uint8_t old_key[WALLET_ENCRYPTION_KEY_LENGTH];
uint8_t buffer[16];
NonVolatileReturn r;
uint32_t address;
uint32_t end;
if (!wallet_loaded)
{
last_error = WALLET_NOT_THERE;
return last_error;
}
getEncryptionKey(old_key);
r = NV_NO_ERROR;
address = ENCRYPT_START;
end = WALLET_RECORD_LENGTH;
while ((r == NV_NO_ERROR) && (address < end))
{
setEncryptionKey(old_key);
r = encryptedNonVolatileRead(buffer, address, 16);
if (r == NV_NO_ERROR)
{
setEncryptionKey(new_key);
r = encryptedNonVolatileWrite(buffer, address, 16);
nonVolatileFlush();
}
address += 16;
}
setEncryptionKey(new_key);
if (r == NV_NO_ERROR)
{
// Update version and checksum.
if (writeWalletVersion() == NV_NO_ERROR)
{
last_error = writeWalletChecksum();;
}
else
{
last_error = WALLET_WRITE_ERROR;
}
}
else
{
last_error = WALLET_WRITE_ERROR;
}
return last_error;
}
/** Change the name of the currently loaded wallet.
* \param new_name This should point to #NAME_LENGTH bytes (padded with
* spaces if necessary) containing the new desired name of
* the wallet.
* \return #WALLET_NO_ERROR on success, or one of #WalletErrorsEnum if an
* error occurred.
*/
WalletErrors changeWalletName(uint8_t *new_name)
{
WalletErrors r;
if (!wallet_loaded)
{
last_error = WALLET_NOT_THERE;
return last_error;
}
// Write wallet name.
if (nonVolatileWrite(new_name, OFFSET_NAME, NAME_LENGTH) != NV_NO_ERROR)
{
last_error = WALLET_WRITE_ERROR;
return last_error;
}
// Write checksum.
r = writeWalletChecksum();
if (r != WALLET_NO_ERROR)
{
last_error = r;
return last_error;
}
nonVolatileFlush();
last_error = WALLET_NO_ERROR;
return last_error;
}
/** Obtain publicly available information about a wallet. "Publicly available"
* means that the leakage of that information would have a relatively low
* impact on security (compared to the leaking of, say, the deterministic
* private key generator seed).
*
* Note that unlike most of the other wallet functions, this function does
* not require the wallet to be loaded. This is so that a user can be
* presented with a list of all the wallets stored on a hardware Bitcoin
* wallet, without having to know the encryption key to each wallet.
* \param out_version The little-endian version of the wallet will be written
* to here (if everything goes well). This should be a
* byte array with enough space to store 4 bytes.
* \param out_name The (space-padded) name of the wallet will be written
* to here (if everything goes well). This should be a
* byte array with enough space to store #NAME_LENGTH bytes.
* \return #WALLET_NO_ERROR on success, or one of #WalletErrorsEnum if an
* error occurred.
*/
WalletErrors getWalletInfo(uint8_t *out_version, uint8_t *out_name)
{
if (nonVolatileRead(out_version, OFFSET_VERSION, 4) != NV_NO_ERROR)
{
last_error = WALLET_READ_ERROR;
return last_error;
}
if (nonVolatileRead(out_name, OFFSET_NAME, NAME_LENGTH) != NV_NO_ERROR)
{
last_error = WALLET_READ_ERROR;
return last_error;
}
last_error = WALLET_NO_ERROR;
return last_error;
}
#ifdef TEST
/** Size of storage area, in bytes. */
#define TEST_FILE_SIZE 1024
/** Write to non-volatile storage.
* \param data A pointer to the data to be written.
* \param address Byte offset specifying where in non-volatile storage to
* start writing to.
* \param length The number of bytes to write.
* \return See #NonVolatileReturnEnum for return values.
* \warning Writes may be buffered; use nonVolatileFlush() to be sure that
* data is actually written to non-volatile storage.
*/
NonVolatileReturn nonVolatileWrite(uint8_t *data, uint32_t address, uint8_t length)
{
#ifndef TEST_XEX
int i;
#endif // #ifndef TEST_XEX
if ((address + (uint32_t)length) > TEST_FILE_SIZE)
{
return NV_INVALID_ADDRESS;
}
// Don't output write debugging info when testing xex.c, otherwise the
// console will go crazy (since the unit tests in xex.c do a lot of
// writing).
#ifndef TEST_XEX
printf("nv write, addr = 0x%08x, length = 0x%04x, data =", (int)address, (int)length);
for (i = 0; i < length; i++)
{
printf(" %02x", data[i]);
}
printf("\n");
#endif // #ifndef TEST_XEX
fseek(wallet_test_file, (long)address, SEEK_SET);
fwrite(data, (size_t)length, 1, wallet_test_file);
return NV_NO_ERROR;
}
/** Read from non-volatile storage.
* \param data A pointer to the buffer which will receive the data.
* \param address Byte offset specifying where in non-volatile storage to
* start reading from.
* \param length The number of bytes to read.
* \return See #NonVolatileReturnEnum for return values.
*/
NonVolatileReturn nonVolatileRead(uint8_t *data, uint32_t address, uint8_t length)
{
if ((address + (uint32_t)length) > TEST_FILE_SIZE)
{
return NV_INVALID_ADDRESS;
}
fseek(wallet_test_file, (long)address, SEEK_SET);
fread(data, (size_t)length, 1, wallet_test_file);
return NV_NO_ERROR;
}
/** Ensure that all buffered writes are committed to non-volatile storage. */
void nonVolatileFlush(void)
{
fflush(wallet_test_file);
}
/** Pretend to overwrite anything in RAM which could contain sensitive
* data. */
void sanitiseRam(void)
{
// do nothing
}
#endif // #ifdef TEST
#ifdef TEST_WALLET
/** Call nearly all wallet functions and make sure they
* return #WALLET_NOT_THERE somehow. This should only be called if a wallet
* is not loaded. */
static void checkFunctionsReturnWalletNotThere(void)
{
uint8_t temp[128];
uint32_t check_num_addresses;
AddressHandle ah;
PointAffine public_key;
// newWallet() not tested because it calls initWallet() when it's done.
ah = makeNewAddress(temp, &public_key);
if ((ah == BAD_ADDRESS_HANDLE) && (walletGetLastError() == WALLET_NOT_THERE))
{
reportSuccess();
}
else
{
printf("makeNewAddress() doesn't recognise when wallet isn't there\n");
reportFailure();
}
check_num_addresses = getNumAddresses();
if ((check_num_addresses == 0) && (walletGetLastError() == WALLET_NOT_THERE))
{
reportSuccess();
}
else
{
printf("getNumAddresses() doesn't recognise when wallet isn't there\n");
reportFailure();
}
if (getAddressAndPublicKey(temp, &public_key, 0) == WALLET_NOT_THERE)
{
reportSuccess();
}
else
{
printf("getAddressAndPublicKey() doesn't recognise when wallet isn't there\n");
reportFailure();
}
if (getPrivateKey(temp, 0) == WALLET_NOT_THERE)
{
reportSuccess();
}
else
{
printf("getPrivateKey() doesn't recognise when wallet isn't there\n");
reportFailure();
}
if (changeEncryptionKey(temp) == WALLET_NOT_THERE)
{
reportSuccess();
}
else
{
printf("changeEncryptionKey() doesn't recognise when wallet isn't there\n");
reportFailure();
}
if (changeWalletName(temp) == WALLET_NOT_THERE)
{
reportSuccess();
}
else
{
printf("changeWalletName() doesn't recognise when wallet isn't there\n");
reportFailure();
}
}
int main(void)
{
uint8_t temp[128];
uint8_t address1[20];
uint8_t address2[20];
uint8_t name[NAME_LENGTH];
uint8_t encryption_key[WALLET_ENCRYPTION_KEY_LENGTH];
uint8_t new_encryption_key[WALLET_ENCRYPTION_KEY_LENGTH];
uint8_t version[4];
uint8_t *address_buffer;
uint8_t one_byte;
AddressHandle *handles_buffer;
AddressHandle ah;
PointAffine public_key;
PointAffine *public_key_buffer;
int abort;
int is_zero;
int abort_duplicate;
int abort_error;
int i;
int j;
initTests(__FILE__);
initWalletTest();
memset(encryption_key, 0, WALLET_ENCRYPTION_KEY_LENGTH);
setEncryptionKey(encryption_key);
// Blank out non-volatile storage area (set to all nulls).
temp[0] = 0;
for (i = 0; i < TEST_FILE_SIZE; i++)
{
fwrite(temp, 1, 1, wallet_test_file);
}
// sanitiseNonVolatileStorage() should nuke everything.
if (sanitiseNonVolatileStorage(0, 0xffffffff) == WALLET_NO_ERROR)
{
reportSuccess();
}
else
{
printf("Cannot nuke NV storage using sanitiseNonVolatileStorage()\n");
reportFailure();
}
// Check that the version field is "wallet not there".
if (getWalletInfo(version, temp) == WALLET_NO_ERROR)
{
reportSuccess();
}
else
{
printf("getWalletInfo() failed after sanitiseNonVolatileStorage() was called\n");
reportFailure();
}
if (readU32LittleEndian(version) == VERSION_NOTHING_THERE)
{
reportSuccess();
}
else
{
printf("sanitiseNonVolatileStorage() does not set version to nothing there\n");
reportFailure();
}
// initWallet() hasn't been called yet, so nearly every function should