core enc util 全部转为javacard写法
This commit is contained in:
@@ -321,6 +321,181 @@ public final class Zuc256Util {
|
||||
output23Byte[22] = (byte) (((src[6] & 0x03) << 6) | src[7]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 32位加法: (a_hi:a_lo) + (b_hi:b_lo)
|
||||
* out[0] = lo, out[1] = hi
|
||||
*/
|
||||
static void add32(short a_lo, short a_hi,
|
||||
short b_lo, short b_hi,
|
||||
short[] out /*len=2*/) {
|
||||
|
||||
// ---- 低16位 ----
|
||||
short lo_low = (short)((a_lo & 0x00FF) + (b_lo & 0x00FF));
|
||||
short carry0 = (short)(((a_lo & 0x00FF) + (b_lo & 0x00FF)) >>> 8);
|
||||
|
||||
short a_lo_hi = (short)((a_lo >>> 8) & 0x00FF);
|
||||
short b_lo_hi = (short)((b_lo >>> 8) & 0x00FF);
|
||||
short lo_high = (short)(a_lo_hi + b_lo_hi + carry0);
|
||||
short carry1 = (short)(lo_high >>> 8);
|
||||
|
||||
short lo_res = (short)((lo_high << 8) | (lo_low & 0x00FF));
|
||||
|
||||
// ---- 高16位 ----
|
||||
short hi_low = (short)((a_hi & 0x00FF) + (b_hi & 0x00FF) + carry1);
|
||||
short carry2 = (short)(hi_low >>> 8);
|
||||
|
||||
short a_hi_hi = (short)((a_hi >>> 8) & 0x00FF);
|
||||
short b_hi_hi = (short)((b_hi >>> 8) & 0x00FF);
|
||||
short hi_high = (short)(a_hi_hi + b_hi_hi + carry2);
|
||||
|
||||
short hi_res = (short)((hi_high << 8) | (hi_low & 0x00FF));
|
||||
|
||||
// ---- 输出 ----
|
||||
out[0] = lo_res;
|
||||
out[1] = hi_res;
|
||||
}
|
||||
|
||||
/**
|
||||
* 32位加法 + 返回进位
|
||||
* 输入: (a_hi:a_lo) + (b_hi:b_lo)
|
||||
* 输出: out[0]=lo, out[1]=hi
|
||||
* 返回: 进位 (0或1)
|
||||
*/
|
||||
static short add32_with_carry(short a_lo, short a_hi,
|
||||
short b_lo, short b_hi,
|
||||
short[] out /*len=2*/) {
|
||||
// 用你现成的 add32 得到结果
|
||||
add32(a_lo, a_hi, b_lo, b_hi, out);
|
||||
|
||||
// 进位判断:如果结果 < 其中一个加数,则说明溢出
|
||||
// (因为 add32 是 mod 2^32 的)
|
||||
// 我们只看 hi 部分即可
|
||||
int sum_hi = (out[1] & 0xFFFF);
|
||||
int a_hi_u = (a_hi & 0xFFFF);
|
||||
int b_hi_u = (b_hi & 0xFFFF);
|
||||
|
||||
if (sum_hi < a_hi_u || sum_hi < b_hi_u) {
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 64位加法: a4 + b4 -> a4
|
||||
* 输入输出: 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) {
|
||||
short[] tmp = new short[2];
|
||||
|
||||
// 低 32 位
|
||||
short carry = add32_with_carry(a[0], a[1], b[0], b[1], tmp);
|
||||
a[0] = tmp[0];
|
||||
a[1] = tmp[1];
|
||||
|
||||
// 高 32 位 + carry
|
||||
add32((short)(a[2] + (carry & 0xFFFF)), a[3], b[2], b[3], tmp);
|
||||
a[2] = tmp[0];
|
||||
a[3] = tmp[1];
|
||||
}
|
||||
|
||||
|
||||
|
||||
// 32位异或
|
||||
public static void xor32(short a_lo, short a_hi, short b_lo, short b_hi, short[] out /*len==2*/) {
|
||||
out[0] = (short)(a_lo ^ b_lo);
|
||||
out[1] = (short)(a_hi ^ b_hi);
|
||||
}
|
||||
|
||||
/**
|
||||
* 把32位数 b (b[0]=lo, b[1]=hi) 左移 k 位 (0 <= k < 32),
|
||||
* 结果放到64位数 a (a[0]=最低16位 ... a[3]=最高16位)。
|
||||
*/
|
||||
static void create_64b_from_32b(short[] a/*len=4*/, short[] b/*len=2*/, short k) {
|
||||
// 先清零
|
||||
a[0] = 0; a[1] = 0; a[2] = 0; a[3] = 0;
|
||||
|
||||
if (k == 0) {
|
||||
a[0] = b[0];
|
||||
a[1] = b[1];
|
||||
return;
|
||||
}
|
||||
|
||||
if (k < 16) {
|
||||
// lo << k
|
||||
a[0] = (short)(b[0] << k);
|
||||
// hi << k, 以及 lo >>> (16-k) 进位
|
||||
a[1] = (short)((b[1] << k) | ((b[0] & 0xFFFF) >>> (16 - k)));
|
||||
// hi >>> (16-k) 残留进到 a[2]
|
||||
a[2] = (short)((b[1] & 0xFFFF) >>> (16 - k));
|
||||
return;
|
||||
}
|
||||
|
||||
if (k == 16) {
|
||||
a[1] = b[0];
|
||||
a[2] = b[1];
|
||||
return;
|
||||
}
|
||||
|
||||
// 16 < k < 32
|
||||
short kk = (short)(k - 16);
|
||||
a[1] = (short)(b[0] << kk);
|
||||
a[2] = (short)((b[1] << kk) | ((b[0] & 0xFFFF) >>> (16 - kk)));
|
||||
a[3] = (short)((b[1] & 0xFFFF) >>> (16 - kk));
|
||||
}
|
||||
|
||||
/**
|
||||
* (A & 0x7FFFFFFF),结果放在 out[4],只保留低32位并清掉最高bit。
|
||||
*/
|
||||
static void and64_7FFFFFFF_to32(short[] A, short[] out) {
|
||||
out[0] = A[0]; // lo16
|
||||
out[1] = (short)(A[1] & 0x7FFF); // hi16 (清除最高bit)
|
||||
out[2] = 0;
|
||||
out[3] = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 64位无符号右移 31 位
|
||||
* 输入: A[0..3] (short[4], A[0]最低16位)
|
||||
* 输出: out[0..3]
|
||||
*/
|
||||
static void shr64u_31(short[] A, short[] out) {
|
||||
// 先拼出 64bit 的逻辑,逐段右移
|
||||
// A >>> 31 = (A >>> 16) >>> 15
|
||||
|
||||
// 先右移 16,相当于丢掉 A[0],整体右移一半字
|
||||
out[0] = A[1]; // 原 A[1] -> 新低16位
|
||||
out[1] = A[2]; // 原 A[2]
|
||||
out[2] = A[3]; // 原 A[3]
|
||||
out[3] = 0; // 高位补0
|
||||
|
||||
// 再右移 15 位
|
||||
short c0 = (short)((out[0] & 0xFFFF) >>> 15); // out[0] 最后一位变进位
|
||||
short c1 = (short)((out[1] & 0xFFFF) >>> 15);
|
||||
short c2 = (short)((out[2] & 0xFFFF) >>> 15);
|
||||
|
||||
out[0] = (short)(((out[0] & 0xFFFF) >>> 15) | (out[1] << 1));
|
||||
out[1] = (short)(((out[1] & 0xFFFF) >>> 15) | (out[2] << 1));
|
||||
out[2] = (short)(((out[2] & 0xFFFF) >>> 15) | (out[3] << 1));
|
||||
out[3] = (short)((out[3] & 0xFFFF) >>> 15);
|
||||
}
|
||||
|
||||
/**
|
||||
* 32位无符号右移 1 位
|
||||
* 输入: lo,hi (short) 表示 32 位数 (hi:高16位, lo:低16位)
|
||||
* 输出: out[0]=lo, out[1]=hi
|
||||
*/
|
||||
static void shr32u1(short lo, short hi, short[] out) {
|
||||
// >>>1:先处理低16位
|
||||
short newLo = (short)(((lo & 0xFFFF) >>> 1) | ((hi & 0x0001) << 15));
|
||||
short newHi = (short)((hi & 0xFFFF) >>> 1);
|
||||
|
||||
out[0] = newLo;
|
||||
out[1] = newHi;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/** 打印/*十六进制(调试用,TODO 生产/JC 环境可移除) *//*
|
||||
public static void printHex(String label, byte[] data, int len) {
|
||||
System.out.print(label + ": ");
|
||||
|
||||
Reference in New Issue
Block a user