在method里面统一定义缓存变量
This commit is contained in:
@@ -51,6 +51,8 @@ public final class Method {
|
||||
|
||||
// ======================================
|
||||
|
||||
// 输入数据缓冲区最大值
|
||||
static final short MAX_DATA_BLOCK_SIZE = 128;
|
||||
// 运行时缓冲:放RAM,避免写EEPROM
|
||||
byte[] ctx_buf;
|
||||
short ctx_buflen;
|
||||
@@ -147,12 +149,13 @@ public final class Method {
|
||||
|
||||
|
||||
public Method() {
|
||||
// start todo
|
||||
ctx_buf = JCSystem.makeTransientByteArray((short)4, JCSystem.MEMORY_TYPE_TRANSIENT_RESET);
|
||||
LFSR_hi = JCSystem.makeTransientShortArray((short)16, JCSystem.MEMORY_TYPE_TRANSIENT_RESET);
|
||||
LFSR_lo = JCSystem.makeTransientShortArray((short)16, JCSystem.MEMORY_TYPE_TRANSIENT_RESET);
|
||||
buf1 = JCSystem.makeTransientByteArray(L, JCSystem.MEMORY_TYPE_TRANSIENT_RESET);
|
||||
buf2 = JCSystem.makeTransientByteArray(L, JCSystem.MEMORY_TYPE_TRANSIENT_RESET);
|
||||
// todo buf1 if not use, delete please
|
||||
buf1 = JCSystem.makeTransientByteArray(MAX_DATA_BLOCK_SIZE, JCSystem.MEMORY_TYPE_TRANSIENT_RESET);
|
||||
// todo buf2 if not use, delete please
|
||||
buf2 = JCSystem.makeTransientByteArray(MAX_DATA_BLOCK_SIZE, JCSystem.MEMORY_TYPE_TRANSIENT_RESET);
|
||||
|
||||
stmsi = JCSystem.makeTransientByteArray((short)6, JCSystem.MEMORY_TYPE_TRANSIENT_RESET);
|
||||
location_data = JCSystem.makeTransientByteArray((short)5, JCSystem.MEMORY_TYPE_TRANSIENT_RESET);
|
||||
@@ -201,8 +204,8 @@ public final class Method {
|
||||
updateZuc256EncryptCtx_ks = JCSystem.makeTransientShortArray((short)2, JCSystem.MEMORY_TYPE_TRANSIENT_RESET);
|
||||
updateZuc256EncryptCtx_plain = JCSystem.makeTransientShortArray((short)2, JCSystem.MEMORY_TYPE_TRANSIENT_RESET);
|
||||
updateZuc256EncryptCtx_res = JCSystem.makeTransientShortArray((short)2, JCSystem.MEMORY_TYPE_TRANSIENT_RESET);
|
||||
updateZuc256EncryptCtx_ks_hi = JCSystem.makeTransientShortArray(fullBlocks, JCSystem.MEMORY_TYPE_TRANSIENT_RESET);
|
||||
updateZuc256EncryptCtx_ks_lo = JCSystem.makeTransientShortArray(fullBlocks, JCSystem.MEMORY_TYPE_TRANSIENT_RESET);
|
||||
updateZuc256EncryptCtx_ks_hi = JCSystem.makeTransientShortArray((short)(MAX_DATA_BLOCK_SIZE/4), JCSystem.MEMORY_TYPE_TRANSIENT_RESET);
|
||||
updateZuc256EncryptCtx_ks_lo = JCSystem.makeTransientShortArray((short)(MAX_DATA_BLOCK_SIZE/4), JCSystem.MEMORY_TYPE_TRANSIENT_RESET);
|
||||
// 临时:装一个32位字
|
||||
updateZuc256EncryptCtx_word = JCSystem.makeTransientShortArray((short)2, JCSystem.MEMORY_TYPE_TRANSIENT_RESET);
|
||||
|
||||
@@ -240,21 +243,20 @@ public final class Method {
|
||||
|
||||
|
||||
public void processData(APDU apdu) {
|
||||
short L = (short) INPUT.length;
|
||||
|
||||
byte[] apduBuf = apdu.getBuffer();
|
||||
|
||||
// 第一次:Enc(Input)
|
||||
initZuc256EncryptCtx(KEY32, IV25);
|
||||
updateZuc256EncryptCtx(INPUT, (short) INPUT.length, buf1);
|
||||
finishZuc256EncryptCtx(buf1); // 若 Input 长度为 4 的倍数则通常无副作用,留着更稳妥
|
||||
finishZuc256EncryptCtx(buf1, (short) INPUT.length); // 若 Input 长度为 4 的倍数则通常无副作用,留着更稳妥
|
||||
|
||||
boolean encMatch = (Util.arrayCompare(buf1, (short)0, ENC_EXPECTED, (short)0, (short)INPUT.length) == 0);
|
||||
|
||||
// 第二次:Enc(Enc(Input)) 应还原 Input
|
||||
initZuc256EncryptCtx(KEY32, IV25);
|
||||
updateZuc256EncryptCtx(buf1, (short) INPUT.length, buf2);
|
||||
finishZuc256EncryptCtx(buf2);
|
||||
finishZuc256EncryptCtx(buf2, (short) INPUT.length);
|
||||
boolean dblOk = (Util.arrayCompare(buf2, (short)0, INPUT, (short)0, (short)INPUT.length) == 0);
|
||||
|
||||
// 返回 2 字节结果:[encMatch, dblOk],1=真, 0=假
|
||||
@@ -943,7 +945,10 @@ public final class Method {
|
||||
// a += (long)LFSR[13] << 17;
|
||||
// a += (long)LFSR[15] << 15;
|
||||
// ---- 先准备累加器 A (64位) ----
|
||||
zuc256GenerateKeyword_A[0] = 0; zuc256GenerateKeyword_A[1] = 0; zuc256GenerateKeyword_A[2] = 0; zuc256GenerateKeyword_A[3] = 0;
|
||||
zuc256GenerateKeyword_A[0] = 0;
|
||||
zuc256GenerateKeyword_A[1] = 0;
|
||||
zuc256GenerateKeyword_A[2] = 0;
|
||||
zuc256GenerateKeyword_A[3] = 0;
|
||||
|
||||
// 临时缓冲
|
||||
|
||||
@@ -996,7 +1001,10 @@ public final class Method {
|
||||
and64_7FFFFFFF_to32(zuc256GenerateKeyword_A, zuc256GenerateKeyword_low31b);
|
||||
shr64u_31(zuc256GenerateKeyword_A, zuc256GenerateKeyword_r31b);
|
||||
|
||||
zuc256GenerateKeyword_v64[0]=0; zuc256GenerateKeyword_v64[1]=0; zuc256GenerateKeyword_v64[2]=0; zuc256GenerateKeyword_v64[3]=0;
|
||||
zuc256GenerateKeyword_v64[0]=0;
|
||||
zuc256GenerateKeyword_v64[1]=0;
|
||||
zuc256GenerateKeyword_v64[2]=0;
|
||||
zuc256GenerateKeyword_v64[3]=0;
|
||||
add64(zuc256GenerateKeyword_v64, zuc256GenerateKeyword_low31b);
|
||||
add64(zuc256GenerateKeyword_v64, zuc256GenerateKeyword_r31b);
|
||||
|
||||
@@ -1052,6 +1060,17 @@ public final class Method {
|
||||
|
||||
// 初始化MAC密钥
|
||||
private void zuc256SetMacKey(byte[] K, byte[] IV, short macbits) {
|
||||
|
||||
for(short i=0; i<2; i++) {
|
||||
zuc256SetMacKey_R1[i] = (short) 0;
|
||||
zuc256SetMacKey_R2[i] = (short) 0;
|
||||
}
|
||||
for(short i=0; i<4; i++) {
|
||||
zuc256SetMacKey_A[i] = (short) 0;
|
||||
zuc256SetMacKey_vv[i] = (short) 0;
|
||||
zuc256SetMacKey_v64[i] = (short) 0;
|
||||
}
|
||||
|
||||
// short[] zuc256SetMacKey_D = JCSystem.makeTransientShortArray(Zuc256Tables.D_COLS, JCSystem.MEMORY_TYPE_TRANSIENT_RESET);
|
||||
// short[] zuc256SetMacKey_TMP = JCSystem.makeTransientShortArray((short)2, JCSystem.MEMORY_TYPE_TRANSIENT_RESET);
|
||||
// short[] zuc256SetMacKey_X0 = JCSystem.makeTransientShortArray((short)2, JCSystem.MEMORY_TYPE_TRANSIENT_RESET);
|
||||
@@ -1315,26 +1334,32 @@ public final class Method {
|
||||
// long a = LFSR[0];
|
||||
zuc256SetMacKey_tmp32[0] = LFSR_lo[0];
|
||||
zuc256SetMacKey_tmp32[1] = LFSR_hi[0];
|
||||
create_64b_from_32b(zuc256SetMacKey_tmp64, zuc256SetMacKey_tmp32, (short)0); add64(zuc256SetMacKey_A, zuc256SetMacKey_tmp64);
|
||||
create_64b_from_32b(zuc256SetMacKey_tmp64, zuc256SetMacKey_tmp32, (short)0);
|
||||
add64(zuc256SetMacKey_A, zuc256SetMacKey_tmp64);
|
||||
|
||||
// a += (long)LFSR[0] << 8;
|
||||
create_64b_from_32b(zuc256SetMacKey_tmp64, zuc256SetMacKey_tmp32, (short)8); add64(zuc256SetMacKey_A, zuc256SetMacKey_tmp64);
|
||||
create_64b_from_32b(zuc256SetMacKey_tmp64, zuc256SetMacKey_tmp32, (short)8);
|
||||
add64(zuc256SetMacKey_A, zuc256SetMacKey_tmp64);
|
||||
|
||||
// a += (long)LFSR[4] << 20;
|
||||
zuc256SetMacKey_tmp32[0] = LFSR_lo[4]; zuc256SetMacKey_tmp32[1] = LFSR_hi[4];
|
||||
create_64b_from_32b(zuc256SetMacKey_tmp64, zuc256SetMacKey_tmp32, (short)20); add64(zuc256SetMacKey_A, zuc256SetMacKey_tmp64);
|
||||
create_64b_from_32b(zuc256SetMacKey_tmp64, zuc256SetMacKey_tmp32, (short)20);
|
||||
add64(zuc256SetMacKey_A, zuc256SetMacKey_tmp64);
|
||||
|
||||
// a += (long)LFSR[10] << 21;
|
||||
zuc256SetMacKey_tmp32[0] = LFSR_lo[10]; zuc256SetMacKey_tmp32[1] = LFSR_hi[10];
|
||||
create_64b_from_32b(zuc256SetMacKey_tmp64, zuc256SetMacKey_tmp32, (short)21); add64(zuc256SetMacKey_A, zuc256SetMacKey_tmp64);
|
||||
create_64b_from_32b(zuc256SetMacKey_tmp64, zuc256SetMacKey_tmp32, (short)21);
|
||||
add64(zuc256SetMacKey_A, zuc256SetMacKey_tmp64);
|
||||
|
||||
// a += (long)LFSR[13] << 17;
|
||||
zuc256SetMacKey_tmp32[0] = LFSR_lo[13]; zuc256SetMacKey_tmp32[1] = LFSR_hi[13];
|
||||
create_64b_from_32b(zuc256SetMacKey_tmp64, zuc256SetMacKey_tmp32, (short)17); add64(zuc256SetMacKey_A, zuc256SetMacKey_tmp64);
|
||||
create_64b_from_32b(zuc256SetMacKey_tmp64, zuc256SetMacKey_tmp32, (short)17);
|
||||
add64(zuc256SetMacKey_A, zuc256SetMacKey_tmp64);
|
||||
|
||||
// a += (long)LFSR[15] << 15;
|
||||
zuc256SetMacKey_tmp32[0] = LFSR_lo[15]; zuc256SetMacKey_tmp32[1] = LFSR_hi[15];
|
||||
create_64b_from_32b(zuc256SetMacKey_tmp64, zuc256SetMacKey_tmp32, (short)15); add64(zuc256SetMacKey_A, zuc256SetMacKey_tmp64);
|
||||
create_64b_from_32b(zuc256SetMacKey_tmp64, zuc256SetMacKey_tmp32, (short)15);
|
||||
add64(zuc256SetMacKey_A, zuc256SetMacKey_tmp64);
|
||||
|
||||
// a = (a & 0x7FFFFFFF) + (a >>> 31);
|
||||
and64_7FFFFFFF_to32(zuc256SetMacKey_A, zuc256SetMacKey_low31);
|
||||
@@ -1499,7 +1524,7 @@ public final class Method {
|
||||
}
|
||||
|
||||
// 完成加密处理
|
||||
public void finishZuc256EncryptCtx(byte[] out) {
|
||||
public void finishZuc256EncryptCtx(byte[] out, short outLen) {
|
||||
if (out == null) return;
|
||||
// 处理缓冲区中剩余的不足4字节数据
|
||||
if (this.ctx_buflen > 0) {
|
||||
@@ -1512,7 +1537,7 @@ public final class Method {
|
||||
putU32(finishZuc256EncryptCtx_keystreamBytes, (short)0, finishZuc256EncryptCtx_ks[0], finishZuc256EncryptCtx_ks[1]);
|
||||
|
||||
// 逐字节异或
|
||||
short outOffset = (short)(out.length - this.ctx_buflen);
|
||||
short outOffset = (short)(outLen - this.ctx_buflen);
|
||||
for (short i = 0; i < this.ctx_buflen; i++) {
|
||||
out[(short)(i+outOffset)] = (byte) (this.ctx_buf[i] ^ finishZuc256EncryptCtx_keystreamBytes[i]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user