Skip to content

Commit 8ebcd61

Browse files
allow 16-64 byte seeds
1 parent 0477907 commit 8ebcd61

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

docs/BlindOracle.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ Loads or generates the root key on the card.
2828

2929
### Set seed
3030

31-
Calculates root key from bip32 seed (64 bytes)
31+
Calculates root key from bip32 seed (16-64 bytes)
3232

3333
| Field | Value |
3434
| ------ | ---------------------------------------- |
3535
| CMD | `0x10` |
3636
| SUBCMD | `0x00` |
37-
| DATA | 64-byte seed |
37+
| DATA | 16-64 byte seed |
3838
| RETURN | Responce code: `0x9000`, `DATA`: root xpub: `<chain_code><pubkey>` |
3939

4040
### Set root key

src/main/java/toys/BlindOracleApplet.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ public class BlindOracleApplet extends SecureApplet{
6666
private static final short CHAINCODE_LEN = (short)32;
6767
private static final short PUBKEY_LEN = (short)33;
6868
private static final short PRVKEY_LEN = (short)32;
69-
private static final short SEED_LEN = (short)64;
69+
private static final short SEED_LEN_MIN = (short)16;
70+
private static final short SEED_LEN_MAX = (short)64;
7071
private static final short MSG_LEN = (short)32;
7172
public static final byte[] HDKEY_SEED_KEY = {'B','i','t','c','o','i','n',' ','s','e','e','d'};
7273

@@ -142,14 +143,20 @@ protected short processSecureMessage(byte[] buf, short len){
142143
*
143144
* @param seed - buffer with the seed
144145
* @param seefOff - offset of the seed in the buffer
146+
* @param seedLen - length of the seed
145147
*/
146-
private void genKeyFromSeed(byte[] seed, short seedOff){
148+
private void genKeyFromSeed(byte[] seed, short seedOff, short seedLen){
149+
// check it's between 16 and 64 bytes
150+
if( (seedLen < SEED_LEN_MIN) || (seedLen > SEED_LEN_MAX))
151+
{
152+
ISOException.throwIt(ERR_INVALID_LEN);
153+
}
147154
// set depth, child number and fingerprint to zero
148155
short len = (short)64;
149156
short off = heap.allocate(len);
150157
// do hmac_sha512("Bitcoin seed", seed)
151158
Crypto.hmacSha512.init(HDKEY_SEED_KEY, (short)0, (short)(HDKEY_SEED_KEY.length));
152-
Crypto.hmacSha512.doFinal(seed, seedOff, SEED_LEN, heap.buffer, off);
159+
Crypto.hmacSha512.doFinal(seed, seedOff, seedLen, heap.buffer, off);
153160
// copy first 32 bytes to private key
154161
Util.arrayCopyNonAtomic(heap.buffer, off, rootPrv, (short)0, PRVKEY_LEN);
155162
// copy last 32 bytes to chain code
@@ -177,12 +184,9 @@ protected short processRootCommand(byte[] buf, short len){
177184
switch (subcmd){
178185
// set key from seed
179186
case SUBCMD_ROOT_SET_SEED:
180-
// check it's 64 bytes
181-
if(len!=(short)(SEED_LEN+LENGTH_CMD_SUBCMD)){
182-
ISOException.throwIt(ERR_INVALID_LEN);
183-
}
184187
// copy to defaulSeed
185-
genKeyFromSeed(buf, OFFSET_SECURE_PAYLOAD);
188+
genKeyFromSeed(buf, OFFSET_SECURE_PAYLOAD,
189+
(short)(len-LENGTH_CMD_SUBCMD));
186190
Util.arrayCopyNonAtomic(rootXpub, (short)0,
187191
buf, OFFSET_SECURE_PAYLOAD,
188192
BIP32_LEN);

0 commit comments

Comments
 (0)