位置加密applet基本实现,目录下包含c java参考代码、打包工具、说明文档、 #2
@@ -17,22 +17,22 @@ public final class Method {
|
|||||||
// ======= 已按你提供的数据填充 =======
|
// ======= 已按你提供的数据填充 =======
|
||||||
|
|
||||||
// Key: 32字节
|
// Key: 32字节
|
||||||
private static final byte[] KEY32 = new byte[] {
|
private static final byte[] KEY32 = {
|
||||||
(byte)0x30,(byte)0x31,(byte)0x32,(byte)0x33,(byte)0x34,(byte)0x35,(byte)0x36,(byte)0x37,
|
(byte)0x30,(byte)0x31,(byte)0x32,(byte)0x33,(byte)0x34,(byte)0x35,(byte)0x36,(byte)0x37,
|
||||||
(byte)0x38,(byte)0x39,(byte)0x61,(byte)0x62,(byte)0x63,(byte)0x64,(byte)0x65,(byte)0x66,
|
(byte)0x38,(byte)0x39,(byte)0x61,(byte)0x62,(byte)0x63,(byte)0x64,(byte)0x65,(byte)0x66,
|
||||||
(byte)0x30,(byte)0x31,(byte)0x32,(byte)0x33,(byte)0x34,(byte)0x35,(byte)0x36,(byte)0x37,
|
(byte)0x30,(byte)0x31,(byte)0x32,(byte)0x33,(byte)0x34,(byte)0x35,(byte)0x36,(byte)0x37,
|
||||||
(byte)0x38,(byte)0x39,(byte)0x61,(byte)0x62,(byte)0x63,(byte)0x64,(byte)0x65,(byte)0x66
|
(byte)0x38,(byte)0x39,(byte)0x61,(byte)0x62,(byte)0x63,(byte)0x64,(byte)0x65,(byte)0x66
|
||||||
};
|
};
|
||||||
|
|
||||||
// IV: 你提供的是23字节(如需25字节请补齐两字节)
|
// IV
|
||||||
private static final byte[] IV25 = new byte[] {
|
private static final byte[] IV25 = {//todo 23 -> 25
|
||||||
(byte)0x30,(byte)0x31,(byte)0x32,(byte)0x33,(byte)0x34,(byte)0x35,(byte)0x36,(byte)0x37,
|
(byte)0x30,(byte)0x31,(byte)0x32,(byte)0x33,(byte)0x34,(byte)0x35,(byte)0x36,(byte)0x37,
|
||||||
(byte)0x38,(byte)0x39,(byte)0x61,(byte)0x62,(byte)0x63,(byte)0x64,(byte)0x65,(byte)0x66,
|
(byte)0x38,(byte)0x39,(byte)0x61,(byte)0x62,(byte)0x63,(byte)0x64,(byte)0x65,(byte)0x66,
|
||||||
(byte)0x67,(byte)0xC3,(byte)0x1C,(byte)0xB3,(byte)0xD3,(byte)0x5D,(byte)0xB7
|
(byte)0x67,(byte)0xC3,(byte)0x1C,(byte)0xB3,(byte)0xD3,(byte)0x5D,(byte)0xB7
|
||||||
};
|
};
|
||||||
|
|
||||||
// Input: 明文(38字节)
|
// Input: 明文(38字节)
|
||||||
private static final byte[] INPUT = new byte[] {
|
private static final byte[] INPUT = {
|
||||||
(byte)0x5A,(byte)0x55,(byte)0x43,(byte)0x32,(byte)0x35,(byte)0x36,(byte)0xE5,(byte)0xAF,
|
(byte)0x5A,(byte)0x55,(byte)0x43,(byte)0x32,(byte)0x35,(byte)0x36,(byte)0xE5,(byte)0xAF,
|
||||||
(byte)0xB9,(byte)0xE7,(byte)0xA7,(byte)0xB0,(byte)0xE5,(byte)0x8A,(byte)0xA0,(byte)0xE8,
|
(byte)0xB9,(byte)0xE7,(byte)0xA7,(byte)0xB0,(byte)0xE5,(byte)0x8A,(byte)0xA0,(byte)0xE8,
|
||||||
(byte)0xA7,(byte)0xA3,(byte)0xE5,(byte)0xAF,(byte)0x86,(byte)0xE6,(byte)0xB5,(byte)0x8B,
|
(byte)0xA7,(byte)0xA3,(byte)0xE5,(byte)0xAF,(byte)0x86,(byte)0xE6,(byte)0xB5,(byte)0x8B,
|
||||||
@@ -41,7 +41,7 @@ public final class Method {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// EncResult: 期望密文(38字节)
|
// EncResult: 期望密文(38字节)
|
||||||
private static final byte[] ENC_EXPECTED = new byte[] {
|
private static final byte[] ENC_EXPECTED = {
|
||||||
(byte)0x6C,(byte)0xEE,(byte)0x3C,(byte)0xFA,(byte)0xDE,(byte)0xBB,(byte)0xCB,(byte)0xE5,
|
(byte)0x6C,(byte)0xEE,(byte)0x3C,(byte)0xFA,(byte)0xDE,(byte)0xBB,(byte)0xCB,(byte)0xE5,
|
||||||
(byte)0x33,(byte)0x51,(byte)0x07,(byte)0x07,(byte)0x90,(byte)0x25,(byte)0x93,(byte)0x27,
|
(byte)0x33,(byte)0x51,(byte)0x07,(byte)0x07,(byte)0x90,(byte)0x25,(byte)0x93,(byte)0x27,
|
||||||
(byte)0x94,(byte)0xF5,(byte)0x18,(byte)0x70,(byte)0xEF,(byte)0x71,(byte)0x72,(byte)0x7D,
|
(byte)0x94,(byte)0xF5,(byte)0x18,(byte)0x70,(byte)0xEF,(byte)0x71,(byte)0x72,(byte)0x7D,
|
||||||
|
|||||||
@@ -29,7 +29,8 @@ public class XwSecurity extends Applet {
|
|||||||
|
|
||||||
public XwSecurity(byte[] bArray, short bOffset, byte bLength) {
|
public XwSecurity(byte[] bArray, short bOffset, byte bLength) {
|
||||||
// TODO Auto-generated constructor stub
|
// TODO Auto-generated constructor stub
|
||||||
method = new Method();
|
method = new Method(); //todo new?
|
||||||
|
// key store -> flash
|
||||||
key_store_byte = new byte[80];
|
key_store_byte = new byte[80];
|
||||||
|
|
||||||
register(bArray, (short)(bOffset + 1), bArray[bOffset]);
|
register(bArray, (short)(bOffset + 1), bArray[bOffset]);
|
||||||
@@ -37,6 +38,7 @@ public class XwSecurity extends Applet {
|
|||||||
|
|
||||||
public static void install(byte[] bArray, short bOffset, byte bLength)
|
public static void install(byte[] bArray, short bOffset, byte bLength)
|
||||||
{
|
{
|
||||||
|
//todo new?
|
||||||
// GP-compliant JavaCard applet registration
|
// GP-compliant JavaCard applet registration
|
||||||
new XwSecurity(bArray, bOffset, bLength);
|
new XwSecurity(bArray, bOffset, bLength);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -143,12 +143,12 @@ public class Zuc256Core {
|
|||||||
// a += (long)LFSR[13] << 17;
|
// a += (long)LFSR[13] << 17;
|
||||||
// a += (long)LFSR[15] << 15;
|
// a += (long)LFSR[15] << 15;
|
||||||
// ---- 先准备累加器 A (64位) ----
|
// ---- 先准备累加器 A (64位) ----
|
||||||
short[] A = new short[4]; // 64位累加器,初始全0
|
short[] A = JCSystem.makeTransientShortArray((short)4, JCSystem.MEMORY_TYPE_TRANSIENT_RESET); // 64位累加器,初始全0
|
||||||
A[0] = 0; A[1] = 0; A[2] = 0; A[3] = 0;
|
A[0] = 0; A[1] = 0; A[2] = 0; A[3] = 0;
|
||||||
|
|
||||||
// 临时缓冲
|
// 临时缓冲
|
||||||
short[] tmp32 = new short[2]; // 保存一个32位数 (lo,hi)
|
short[] tmp32 = JCSystem.makeTransientShortArray((short)2, JCSystem.MEMORY_TYPE_TRANSIENT_RESET);; // 保存一个32位数 (lo,hi)
|
||||||
short[] tmp64 = new short[4]; // 保存移位后的64位数
|
short[] tmp64 = JCSystem.makeTransientShortArray((short)4, JCSystem.MEMORY_TYPE_TRANSIENT_RESET);; // 保存移位后的64位数
|
||||||
|
|
||||||
// a = LFSR[0]
|
// a = LFSR[0]
|
||||||
tmp32[0] = state.LFSR_lo[0];
|
tmp32[0] = state.LFSR_lo[0];
|
||||||
@@ -186,8 +186,8 @@ public class Zuc256Core {
|
|||||||
|
|
||||||
// a = (a & 0x7FFFFFFF) + (a >>> 31);
|
// a = (a & 0x7FFFFFFF) + (a >>> 31);
|
||||||
// ---- 第一次折叠:a = (a & 0x7FFFFFFF) + (a >>> 31) ----
|
// ---- 第一次折叠:a = (a & 0x7FFFFFFF) + (a >>> 31) ----
|
||||||
short[] low31 = new short[4];
|
short[] low31 = JCSystem.makeTransientShortArray((short)4, JCSystem.MEMORY_TYPE_TRANSIENT_RESET);
|
||||||
short[] r31 = new short[4];
|
short[] r31 = JCSystem.makeTransientShortArray((short)4, JCSystem.MEMORY_TYPE_TRANSIENT_RESET);
|
||||||
|
|
||||||
and64_7FFFFFFF_to32(A, low31); // low31 = A & 0x7FFFFFFF
|
and64_7FFFFFFF_to32(A, low31); // low31 = A & 0x7FFFFFFF
|
||||||
shr64u_31(A, r31); // r31 = A >>> 31
|
shr64u_31(A, r31); // r31 = A >>> 31
|
||||||
@@ -197,9 +197,9 @@ public class Zuc256Core {
|
|||||||
add64(A, r31);
|
add64(A, r31);
|
||||||
// int v = (int) ((a & 0x7FFFFFFF) + (a >>> 31));
|
// int v = (int) ((a & 0x7FFFFFFF) + (a >>> 31));
|
||||||
// ---- 第二次折叠,得到 v(32位)----
|
// ---- 第二次折叠,得到 v(32位)----
|
||||||
short[] low31b = new short[4];
|
short[] low31b = JCSystem.makeTransientShortArray((short)4, JCSystem.MEMORY_TYPE_TRANSIENT_RESET);
|
||||||
short[] r31b = new short[4];
|
short[] r31b = JCSystem.makeTransientShortArray((short)4, JCSystem.MEMORY_TYPE_TRANSIENT_RESET);
|
||||||
short[] v64 = new short[4];
|
short[] v64 = JCSystem.makeTransientShortArray((short)4, JCSystem.MEMORY_TYPE_TRANSIENT_RESET);
|
||||||
|
|
||||||
and64_7FFFFFFF_to32(A, low31b);
|
and64_7FFFFFFF_to32(A, low31b);
|
||||||
shr64u_31(A, r31b);
|
shr64u_31(A, r31b);
|
||||||
@@ -303,7 +303,7 @@ public class Zuc256Core {
|
|||||||
Zuc256Tables.getDRow(row, D, (short)0);
|
Zuc256Tables.getDRow(row, D, (short)0);
|
||||||
|
|
||||||
|
|
||||||
short[] tmp = new short[2]; // 临时存储 makeU31 输出 (lo,hi)
|
short[] tmp = JCSystem.makeTransientShortArray((short)2, JCSystem.MEMORY_TYPE_TRANSIENT_RESET); // 临时存储 makeU31 输出 (lo,hi)
|
||||||
|
|
||||||
// 逐项装载 LFSR
|
// 逐项装载 LFSR
|
||||||
// LFSR[0] = makeU31(K[0] & 0xFF, D[0], K[21] & 0xFF, K[16] & 0xFF);
|
// LFSR[0] = makeU31(K[0] & 0xFF, D[0], K[21] & 0xFF, K[16] & 0xFF);
|
||||||
@@ -524,9 +524,9 @@ public class Zuc256Core {
|
|||||||
R2);
|
R2);
|
||||||
|
|
||||||
// ---- LFSRWithWorkMode ----
|
// ---- LFSRWithWorkMode ----
|
||||||
short[] A = new short[4]; // 64位累加器
|
short[] A = JCSystem.makeTransientShortArray((short)4, JCSystem.MEMORY_TYPE_TRANSIENT_RESET);; // 64位累加器
|
||||||
short[] tmp32 = new short[2];
|
short[] tmp32 = JCSystem.makeTransientShortArray((short)2, JCSystem.MEMORY_TYPE_TRANSIENT_RESET);;
|
||||||
short[] tmp64 = new short[4];
|
short[] tmp64 = JCSystem.makeTransientShortArray((short)4, JCSystem.MEMORY_TYPE_TRANSIENT_RESET);;
|
||||||
|
|
||||||
// LFSRWithWorkMode
|
// LFSRWithWorkMode
|
||||||
// long a = LFSR[0];
|
// long a = LFSR[0];
|
||||||
@@ -554,19 +554,19 @@ public class Zuc256Core {
|
|||||||
create_64b_from_32b(tmp64, tmp32, (short)15); add64(A, tmp64);
|
create_64b_from_32b(tmp64, tmp32, (short)15); add64(A, tmp64);
|
||||||
|
|
||||||
// a = (a & 0x7FFFFFFF) + (a >>> 31);
|
// a = (a & 0x7FFFFFFF) + (a >>> 31);
|
||||||
short[] low31 = new short[4];
|
short[] low31 = JCSystem.makeTransientShortArray((short)4, JCSystem.MEMORY_TYPE_TRANSIENT_RESET);;
|
||||||
short[] r31 = new short[4];
|
short[] r31 = JCSystem.makeTransientShortArray((short)4, JCSystem.MEMORY_TYPE_TRANSIENT_RESET);;
|
||||||
and64_7FFFFFFF_to32(A, low31);
|
and64_7FFFFFFF_to32(A, low31);
|
||||||
shr64u_31(A, r31);
|
shr64u_31(A, r31);
|
||||||
|
|
||||||
short[] v64 = new short[4];
|
short[] v64 = JCSystem.makeTransientShortArray((short)4, JCSystem.MEMORY_TYPE_TRANSIENT_RESET);;
|
||||||
add64(v64, low31);
|
add64(v64, low31);
|
||||||
add64(v64, r31);
|
add64(v64, r31);
|
||||||
|
|
||||||
// int v = (int) ((a & 0x7FFFFFFF) + (a >>> 31));
|
// int v = (int) ((a & 0x7FFFFFFF) + (a >>> 31));
|
||||||
and64_7FFFFFFF_to32(v64, low31);
|
and64_7FFFFFFF_to32(v64, low31);
|
||||||
shr64u_31(v64, r31);
|
shr64u_31(v64, r31);
|
||||||
short[] vv = new short[4];
|
short[] vv = JCSystem.makeTransientShortArray((short)4, JCSystem.MEMORY_TYPE_TRANSIENT_RESET);;
|
||||||
add64(vv, low31);
|
add64(vv, low31);
|
||||||
add64(vv, r31);
|
add64(vv, r31);
|
||||||
|
|
||||||
|
|||||||
@@ -25,12 +25,12 @@ public final class Zuc256EncryptCtx {
|
|||||||
|
|
||||||
public Zuc256EncryptCtx(Zuc256State state){
|
public Zuc256EncryptCtx(Zuc256State state){
|
||||||
this.state = state;
|
this.state = state;
|
||||||
this.buf = new byte[4];
|
this.buf = JCSystem.makeTransientByteArray((short)4, JCSystem.MEMORY_TYPE_TRANSIENT_RESET);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Zuc256EncryptCtx(){
|
public Zuc256EncryptCtx(){
|
||||||
this.state = new Zuc256State();
|
this.state = new Zuc256State(); //todo how to put in ram?
|
||||||
this.buf = new byte[4];
|
this.buf = JCSystem.makeTransientByteArray((short)4, JCSystem.MEMORY_TYPE_TRANSIENT_RESET);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 初始化加密上下文
|
// 初始化加密上下文
|
||||||
@@ -76,17 +76,17 @@ public final class Zuc256EncryptCtx {
|
|||||||
// 缓冲区已满,处理一个完整的4字节块
|
// 缓冲区已满,处理一个完整的4字节块
|
||||||
if (this.buflen == 4) {
|
if (this.buflen == 4) {
|
||||||
// int keystream = zuc256GenerateKeyword(this.state);
|
// int keystream = zuc256GenerateKeyword(this.state);
|
||||||
short[] ks = JCSystem.makeTransientShortArray((short)2, JCSystem.CLEAR_ON_DESELECT);
|
short[] ks = JCSystem.makeTransientShortArray((short)2, JCSystem.MEMORY_TYPE_TRANSIENT_RESET);
|
||||||
zuc256GenerateKeyword(this.state, ks); // ks[0]=lo, ks[1]=hi
|
zuc256GenerateKeyword(this.state, ks); // ks[0]=lo, ks[1]=hi
|
||||||
|
|
||||||
// int plain = getU32(this.buf, 0);
|
// int plain = getU32(this.buf, 0);
|
||||||
// 取出 4 字节明文 → plain[0]=lo, plain[1]=hi
|
// 取出 4 字节明文 → plain[0]=lo, plain[1]=hi
|
||||||
short[] plain = JCSystem.makeTransientShortArray((short)2, JCSystem.CLEAR_ON_DESELECT);
|
short[] plain = JCSystem.makeTransientShortArray((short)2, JCSystem.MEMORY_TYPE_TRANSIENT_RESET);
|
||||||
getU32(this.buf, (short)0, plain);
|
getU32(this.buf, (short)0, plain);
|
||||||
|
|
||||||
// putU32(out, 0, plain ^ keystream);
|
// putU32(out, 0, plain ^ keystream);
|
||||||
// plain ^ ks → res
|
// plain ^ ks → res
|
||||||
short[] res = JCSystem.makeTransientShortArray((short)2, JCSystem.CLEAR_ON_DESELECT);
|
short[] res = JCSystem.makeTransientShortArray((short)2, JCSystem.MEMORY_TYPE_TRANSIENT_RESET);
|
||||||
xor32(plain[0], plain[1], ks[0], ks[1], res);
|
xor32(plain[0], plain[1], ks[0], ks[1], res);
|
||||||
// 写回 out 的前4字节
|
// 写回 out 的前4字节
|
||||||
putU32(out, (short)0, res[0], res[1]);
|
putU32(out, (short)0, res[0], res[1]);
|
||||||
@@ -113,14 +113,14 @@ public final class Zuc256EncryptCtx {
|
|||||||
short fullBlocks = (short) (inlen / 4);
|
short fullBlocks = (short) (inlen / 4);
|
||||||
if (fullBlocks > 0) {
|
if (fullBlocks > 0) {
|
||||||
// int[] keystream = new int[fullBlocks];
|
// int[] keystream = new int[fullBlocks];
|
||||||
short[] ks_hi = JCSystem.makeTransientShortArray(fullBlocks, JCSystem.CLEAR_ON_DESELECT);
|
short[] ks_hi = JCSystem.makeTransientShortArray(fullBlocks, JCSystem.MEMORY_TYPE_TRANSIENT_RESET);
|
||||||
short[] ks_lo = JCSystem.makeTransientShortArray(fullBlocks, JCSystem.CLEAR_ON_DESELECT);
|
short[] ks_lo = JCSystem.makeTransientShortArray(fullBlocks, JCSystem.MEMORY_TYPE_TRANSIENT_RESET);
|
||||||
|
|
||||||
// zuc256GenerateKeystream(this.state, fullBlocks, keystream);
|
// zuc256GenerateKeystream(this.state, fullBlocks, keystream);
|
||||||
zuc256GenerateKeystream(this.state, fullBlocks, ks_hi, ks_lo);
|
zuc256GenerateKeystream(this.state, fullBlocks, ks_hi, ks_lo);
|
||||||
|
|
||||||
// 临时:装一个32位字
|
// 临时:装一个32位字
|
||||||
short[] word = JCSystem.makeTransientShortArray((short)2, JCSystem.CLEAR_ON_DESELECT);
|
short[] word = JCSystem.makeTransientShortArray((short)2, JCSystem.MEMORY_TYPE_TRANSIENT_RESET);
|
||||||
|
|
||||||
// 逐块异或加密
|
// 逐块异或加密
|
||||||
for (short i = 0; i < fullBlocks; i++) {
|
for (short i = 0; i < fullBlocks; i++) {
|
||||||
@@ -170,12 +170,12 @@ public final class Zuc256EncryptCtx {
|
|||||||
if (this.buflen > 0) {
|
if (this.buflen > 0) {
|
||||||
// int keystream = zuc256GenerateKeyword(this.state);
|
// int keystream = zuc256GenerateKeyword(this.state);
|
||||||
// 生成一个 32-bit 密钥字:ks[0]=lo16, ks[1]=hi16
|
// 生成一个 32-bit 密钥字:ks[0]=lo16, ks[1]=hi16
|
||||||
short[] ks = JCSystem.makeTransientShortArray((short)2, JCSystem.CLEAR_ON_DESELECT);
|
short[] ks = JCSystem.makeTransientShortArray((short)2, JCSystem.MEMORY_TYPE_TRANSIENT_RESET);
|
||||||
zuc256GenerateKeyword(this.state, ks);
|
zuc256GenerateKeyword(this.state, ks);
|
||||||
|
|
||||||
// byte[] keystreamBytes = new byte[4];
|
// byte[] keystreamBytes = new byte[4];
|
||||||
// putU32(keystreamBytes, 0, keystream);
|
// putU32(keystreamBytes, 0, keystream);
|
||||||
byte[] keystreamBytes = new byte[4];
|
byte[] keystreamBytes = JCSystem.makeTransientByteArray((short)4, JCSystem.MEMORY_TYPE_TRANSIENT_RESET);
|
||||||
putU32(keystreamBytes, (short)0, ks[0], ks[1]);
|
putU32(keystreamBytes, (short)0, ks[0], ks[1]);
|
||||||
|
|
||||||
// 逐字节异或
|
// 逐字节异或
|
||||||
|
|||||||
@@ -5,9 +5,9 @@ package com.cscn;
|
|||||||
*/
|
*/
|
||||||
public final class Zuc256MacCtx {
|
public final class Zuc256MacCtx {
|
||||||
// LFSR: 原本 int[16],拆成 hi/lo 各 16 short
|
// LFSR: 原本 int[16],拆成 hi/lo 各 16 short
|
||||||
short[] LFSR_hi = new short[16];
|
short[] LFSR_hi = JCSystem.makeTransientShortArray((short)16, JCSystem.MEMORY_TYPE_TRANSIENT_RESET);
|
||||||
//todo -> ram
|
//todo -> ram
|
||||||
short[] LFSR_lo = new short[16];
|
short[] LFSR_lo = JCSystem.makeTransientShortArray((short)16, JCSystem.MEMORY_TYPE_TRANSIENT_RESET);
|
||||||
|
|
||||||
// R1、R2: 原本 int,拆成 hi/lo
|
// R1、R2: 原本 int,拆成 hi/lo
|
||||||
short R1_hi;
|
short R1_hi;
|
||||||
@@ -16,16 +16,16 @@ public final class Zuc256MacCtx {
|
|||||||
short R2_lo;
|
short R2_lo;
|
||||||
|
|
||||||
// 缓冲区
|
// 缓冲区
|
||||||
byte[] buf = new byte[4];
|
byte[] buf = JCSystem.makeTransientByteArray((short)4, JCSystem.MEMORY_TYPE_TRANSIENT_RESET);
|
||||||
short buflen;
|
short buflen;
|
||||||
|
|
||||||
// T: 原本 int[4],拆成 hi/lo
|
// T: 原本 int[4],拆成 hi/lo
|
||||||
short[] T_hi = new short[4];
|
short[] T_hi = JCSystem.makeTransientShortArray((short)4, JCSystem.MEMORY_TYPE_TRANSIENT_RESET);
|
||||||
short[] T_lo = new short[4];
|
short[] T_lo = JCSystem.makeTransientShortArray((short)4, JCSystem.MEMORY_TYPE_TRANSIENT_RESET);
|
||||||
|
|
||||||
// K0: 原本 int[4],拆成 hi/lo
|
// K0: 原本 int[4],拆成 hi/lo
|
||||||
short[] K0_hi = new short[4];
|
short[] K0_hi = JCSystem.makeTransientShortArray((short)4, JCSystem.MEMORY_TYPE_TRANSIENT_RESET);
|
||||||
short[] K0_lo = new short[4];
|
short[] K0_lo = JCSystem.makeTransientShortArray((short)4, JCSystem.MEMORY_TYPE_TRANSIENT_RESET);
|
||||||
|
|
||||||
// macbits: 原本 int,改成 short 足够
|
// macbits: 原本 int,改成 short 足够
|
||||||
short macbits;
|
short macbits;
|
||||||
|
|||||||
@@ -5,12 +5,18 @@ package com.cscn;
|
|||||||
*/
|
*/
|
||||||
public class Zuc256State {
|
public class Zuc256State {
|
||||||
// LFSR: 原 int[16] -> hi/lo 各 16
|
// LFSR: 原 int[16] -> hi/lo 各 16
|
||||||
public short[] LFSR_hi = new short[16];
|
public short[] LFSR_hi;
|
||||||
public short[] LFSR_lo = new short[16];
|
public short[] LFSR_lo;
|
||||||
|
|
||||||
// R1, R2: 原 int -> hi/lo
|
// R1, R2: 原 int -> hi/lo
|
||||||
public short R1_hi;
|
public short R1_hi;
|
||||||
public short R1_lo;
|
public short R1_lo;
|
||||||
public short R2_hi;
|
public short R2_hi;
|
||||||
public short R2_lo;
|
public short R2_lo;
|
||||||
|
|
||||||
|
public Zuc256State() {
|
||||||
|
this.LFSR_hi = JCSystem.makeTransientShortArray((short)16, JCSystem.MEMORY_TYPE_TRANSIENT_RESET);
|
||||||
|
this.LFSR_lo = JCSystem.makeTransientShortArray((short)16, JCSystem.MEMORY_TYPE_TRANSIENT_RESET);
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -62,7 +62,7 @@ public final class Zuc256Tables {
|
|||||||
/**
|
/**
|
||||||
* 常量数组 D(16bit short二维数组适配)
|
* 常量数组 D(16bit short二维数组适配)
|
||||||
*/
|
*/
|
||||||
public static final short[] ZUC256_D_FLAT = new short[] {
|
public static final short[] ZUC256_D_FLAT = {
|
||||||
// row 0
|
// row 0
|
||||||
0x22,0x2F,0x24,0x2A,0x6D,0x40,0x40,0x40,
|
0x22,0x2F,0x24,0x2A,0x6D,0x40,0x40,0x40,
|
||||||
0x40,0x40,0x40,0x40,0x40,0x52,0x10,0x30,
|
0x40,0x40,0x40,0x40,0x40,0x52,0x10,0x30,
|
||||||
|
|||||||
@@ -98,7 +98,7 @@ public final class Zuc256Util {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 拆成 31 位数组 [bit0..bit30]
|
// 拆成 31 位数组 [bit0..bit30]
|
||||||
short[] bits = new short[31];
|
short[] bits = JCSystem.makeTransientShortArray((short)31, JCSystem.MEMORY_TYPE_TRANSIENT_RESET);
|
||||||
for (short i = 0; i < 16; i++) {
|
for (short i = 0; i < 16; i++) {
|
||||||
bits[i] = (short)((a_lo >>> i) & 1);
|
bits[i] = (short)((a_lo >>> i) & 1);
|
||||||
}
|
}
|
||||||
@@ -107,7 +107,7 @@ public final class Zuc256Util {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 旋转
|
// 旋转
|
||||||
short[] resBits = new short[31];
|
short[] resBits = JCSystem.makeTransientShortArray((short)31, JCSystem.MEMORY_TYPE_TRANSIENT_RESET);
|
||||||
for (short i = 0; i < 31; i++) {
|
for (short i = 0; i < 31; i++) {
|
||||||
short j = (short)((i + k) % 31);
|
short j = (short)((i + k) % 31);
|
||||||
resBits[j] = bits[i];
|
resBits[j] = bits[i];
|
||||||
@@ -162,8 +162,8 @@ public final class Zuc256Util {
|
|||||||
* 输出: out[0]=lo, out[1]=hi
|
* 输出: out[0]=lo, out[1]=hi
|
||||||
*/
|
*/
|
||||||
public static void L1(short x_lo, short x_hi, short[] out /*len==2*/) {
|
public static void L1(short x_lo, short x_hi, short[] out /*len==2*/) {
|
||||||
short[] t = new short[2];
|
short[] t = JCSystem.makeTransientShortArray((short)2, JCSystem.MEMORY_TYPE_TRANSIENT_RESET);
|
||||||
short[] acc = new short[2];
|
short[] acc = JCSystem.makeTransientShortArray((short)2, JCSystem.MEMORY_TYPE_TRANSIENT_RESET);
|
||||||
|
|
||||||
// acc = x
|
// acc = x
|
||||||
acc[0] = x_lo;
|
acc[0] = x_lo;
|
||||||
@@ -206,8 +206,8 @@ public final class Zuc256Util {
|
|||||||
* 输出: out[0]=lo, out[1]=hi
|
* 输出: out[0]=lo, out[1]=hi
|
||||||
*/
|
*/
|
||||||
public static void L2(short x_lo, short x_hi, short[] out /*len==2*/) {
|
public static void L2(short x_lo, short x_hi, short[] out /*len==2*/) {
|
||||||
short[] t = new short[2];//todo to ram
|
short[] t = JCSystem.makeTransientShortArray((short)2, JCSystem.MEMORY_TYPE_TRANSIENT_RESET);
|
||||||
short[] acc = new short[2];
|
short[] acc = JCSystem.makeTransientShortArray((short)2, JCSystem.MEMORY_TYPE_TRANSIENT_RESET);
|
||||||
|
|
||||||
// acc = x
|
// acc = x
|
||||||
acc[0] = x_lo;
|
acc[0] = x_lo;
|
||||||
@@ -295,7 +295,7 @@ public final class Zuc256Util {
|
|||||||
|
|
||||||
|
|
||||||
// 处理剩余8字节
|
// 处理剩余8字节
|
||||||
byte[] src = new byte[8];
|
byte[] src = JCSystem.makeTransientShortArray((short)8, JCSystem.MEMORY_TYPE_TRANSIENT_RESET);
|
||||||
for (short i = 0; i < 8; i++) {
|
for (short i = 0; i < 8; i++) {
|
||||||
src[i] = (byte) (input25Byte[(short)(17 + i)] & 0x3F);
|
src[i] = (byte) (input25Byte[(short)(17 + i)] & 0x3F);
|
||||||
}
|
}
|
||||||
@@ -381,7 +381,7 @@ public final class Zuc256Util {
|
|||||||
* 输入输出: short[4],低到高 (a[0]=lo16, a[1]=hi16, a[2]=lo16 of high dword, a[3]=hi16 of high dword)
|
* 输入输出: short[4],低到高 (a[0]=lo16, a[1]=hi16, a[2]=lo16 of high dword, a[3]=hi16 of high dword)
|
||||||
*/
|
*/
|
||||||
static void add64(short[] a, short[] b) {
|
static void add64(short[] a, short[] b) {
|
||||||
short[] tmp = new short[2];
|
short[] tmp = JCSystem.makeTransientShortArray((short)2, JCSystem.MEMORY_TYPE_TRANSIENT_RESET);
|
||||||
|
|
||||||
// 低 32 位
|
// 低 32 位
|
||||||
short carry = add32_with_carry(a[0], a[1], b[0], b[1], tmp);
|
short carry = add32_with_carry(a[0], a[1], b[0], b[1], tmp);
|
||||||
@@ -475,11 +475,11 @@ public final class Zuc256Util {
|
|||||||
*/
|
*/
|
||||||
static void shr32u1(short lo, short hi, short[] out) {
|
static void shr32u1(short lo, short hi, short[] out) {
|
||||||
// >>>1:先处理低16位
|
// >>>1:先处理低16位
|
||||||
short newLo = (short)(((((lo & (short)0xFFFF) >>> 1) & (short)0x7FFF)) | ((hi & 0x0001) << 15));
|
short nwLo = (short)(((((lo & (short)0xFFFF) >>> 1) & (short)0x7FFF)) | ((hi & 0x0001) << 15));
|
||||||
short newHi = (short)(((hi & (short)0xFFFF) >>> 1) & (short)0x7FFF);
|
short nwHi = (short)(((hi & (short)0xFFFF) >>> 1) & (short)0x7FFF);
|
||||||
|
|
||||||
out[0] = newLo;
|
out[0] = nwLo;
|
||||||
out[1] = newHi;
|
out[1] = nwHi;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user