@@ -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