Commit 24923806 authored by Wen Wei Li's avatar Wen Wei Li

fix bugs

parent b6c256c9
......@@ -471,13 +471,18 @@ public class CTAP2 extends Applet implements ExtendedLength {
// | ----- 32 byte is H(H(IV)||H(Nonce)) ----- | ----- 32 byte HMAC -----|
// for reduce the mem/flash usage, I reuse the buffer to store the retrun value
// put encryptedCx
int counter = hkdf.getIteration();
short counter = hkdf.getIteration();
// using CBOR Encoder to assemble the return value
// H(H(IV) || H(Nonce)) ret1[0:31] 32 bytes
// Nonce 32 bytes ret2[0:31]
// HMAC(output_key, H(IV||Nonce)) 32 bytes ret2[32:64]
// Iteration(Counter) 4 bytes integer : counter
// total return length : 100 bytes
/** TODO
* Using CBOREncoding to assemble the return value
*/
}
/**
* for alternative framework purpose
......
package com.josh.vku2f;
import javacard.framework.*;
public class HKDF
{
private byte[] ikm;
......@@ -9,48 +10,55 @@ public class HKDF
private HMAC hmac;
private byte[] temp = JCSystem.makeTransientByteArray((short)32, JCSystem.CLEAR_ON_RESET);
private byte[] tempPrk;
private int iteration;
private short iteration;
private boolean init;
private byte[] counter = new byte[1];
public HKDF(byte[] key, byte[] iv){
this.iteration = 0;
this.ikm = new byte[key.length];
this.iv = new byte[iv.length];
this.prev_key = new byte[32];
Util.arrayCopy(key, (short)0, this.ikm, (short)0, (short)key.length);
Util.arrayCopy(iv, (short)0, this.iv, (short)0, (short)iv.length);
hmac = new HMAC(new byte[32]);
tempPrk = JCSystem.makeTransientByteArray((short)iv.length, JCSystem.CLEAR_ON_RESET);
Util.arrayCopy(this.iv, (short)0, this.prev_key, (short)0, (short)iv.length);
prev_key = new byte[32];
this.iteration = 0;
this.init = true;
}
public void setIteration(int _iteration){
public void setIteration(short _iteration){
this.iteration = _iteration;
this.init = false;
}
public int getIteration(){
public short getIteration(){
return this.iteration;
}
public void getOutputKey(byte[] info, short length, byte[] output, short offset){
getPRK(tempPrk, (short)0);
expand(tempPrk, info, length, output, offset);
}
public void getNextOutputKey(byte[] info, short length, byte[] output, short offset){
if(this.init == false){
getPRK(tempPrk, (short)0);
}
expand(prev_key, info, length, output, offset);
}
public void getPRK( byte[] output, short offset){
public void getPRK(byte[] output, short offset){
//Util.arrayCopy(iv, (short)0, tempPrk, (short)0, (short)iv.length);
Util.arrayCopy(this.ikm ,(short)0, tempPrk, (short)0, (short)32);
for(short i=0; i<this.iteration; i++){
for(short i=0; i< this.iteration; i++){
extract(iv, tempPrk, tempPrk, (short)0);
}
Util.arrayCopy(tempPrk, (short)0, output, (short)0, (short)tempPrk.length);
Util.arrayCopy(tempPrk, (short)0, prev_key, (short)0, (short)prev_key.length);
this.init = true;
}
public void nextPRK(byte[] output, short offset){
if(this.init == false){
getPRK(tempPrk, (short)0);
}
Util.arrayCopy(this.prev_key, (short)0, tempPrk, (short)0, (short)this.prev_key.length);
extract(tempPrk, this.ikm, output, (short)offset);
Util.arrayCopy(output, (short)offset, this.prev_key, (short)0, (short)32);
this.iteration++;
}
private void extract(byte[] key, byte[] msg, byte[] output, short offset){
hmac.setKey(key);
......@@ -59,7 +67,7 @@ public class HKDF
}
// prf: pseudo random function
private void expand(byte[] prf, byte[] info, short length, byte[] output, short offset){
if(length/32>=255 && length%32>0){
if((short)(length/32) >= (short)255 && (short)length % (short)32 > (short)0){
// up to 255 rounds for hmac operation
// which means the max output length is 255*32=8160
return;
......@@ -74,9 +82,10 @@ public class HKDF
hmac.setKey(prf);
hmac.update(info, (short)0, (short)info.length);
counter[0] = (byte)(i+1);
hmac.update(counter, (short)0, (short)info.length);
if(i<N-1)
hmac.doFinal(output, (short)(i*32+offset));
hmac.update(counter, (short)0, (short)counter.length);
if( i < (short)(N-1)) {
hmac.doFinal(output, (short) (i * 32 + offset));
}
else{
if(remainder==0)
hmac.doFinal(output, (short)(i*32+offset));
......
......@@ -34,7 +34,7 @@ public class HMAC
Util.arrayFillNonAtomic(okeypad, prkLength, (short)(okeypad.length-prkLength), (byte)0x5c);
}
public void update(byte[] newMsg, short offset, short length){
if(length + msgCursor > msg.length)
if((short) (length + msgCursor) > (short)msg.length)
return;
Util.arrayCopy(newMsg, offset, msg, msgCursor, length);
msgCursor+=(short)newMsg.length;
......@@ -44,7 +44,7 @@ public class HMAC
sha256.doFinal(msg, (short)0, msgCursor, output, offset);
sha256.update(okeypad, (short)0, (short)okeypad.length);
sha256.doFinal(output, (short)0, (short)32, output, offset);
sha256.doFinal(output, (short)offset, (short)32, output, offset);
msgCursor = (short)0;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment