|
|
|
@ -164,10 +164,8 @@ public class KISA_SEED_CBC {
|
|
|
|
|
// R0, R1 : right input values at each round
|
|
|
|
|
// K : round keys at each round
|
|
|
|
|
private static final void SeedRound(int[] T, int LR[], int L0, int L1, int R0, int R1, int[] K, int K_offset) { |
|
|
|
|
int offset = K_offset + 0; |
|
|
|
|
int offset1 = K_offset + 1; |
|
|
|
|
T[0] = LR[R0] ^ K[offset]; |
|
|
|
|
T[1] = LR[R1] ^ K[offset1]; |
|
|
|
|
T[0] = LR[R0] ^ K[K_offset+0]; |
|
|
|
|
T[1] = LR[R1] ^ K[K_offset+1]; |
|
|
|
|
T[1] ^= T[0]; |
|
|
|
|
T[1] = SS0[GetB0(T[1])&0x0ff] ^ SS1[GetB1(T[1])&0x0ff] ^ |
|
|
|
|
SS2[GetB2(T[1])&0x0ff] ^ SS3[GetB3(T[1])&0x0ff]; |
|
|
|
@ -374,18 +372,12 @@ public class KISA_SEED_CBC {
|
|
|
|
|
if(ENDIAN != Common.BIG_ENDIAN) { |
|
|
|
|
for(i=0;i<inLen;i++) |
|
|
|
|
{ |
|
|
|
|
int temp = in[i / 4]; |
|
|
|
|
int shift = (i % 4) * 8; |
|
|
|
|
data[i] = (byte) (temp >> shift); |
|
|
|
|
// data[i] = (byte)(in[i/4] >> ((i%4)*8));
|
|
|
|
|
data[i] = (byte)(in[i/4] >> ((i%4)*8)); |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
for(i=0;i<inLen;i++) |
|
|
|
|
{ |
|
|
|
|
int temp = in[i / 4]; |
|
|
|
|
int shift = (3 - (i % 4)) * 8; |
|
|
|
|
data[i] = (byte) (temp >> shift); |
|
|
|
|
// data[i] = (byte)(in[i/4] >> ((3-(i%4))*8));
|
|
|
|
|
data[i] = (byte)(in[i/4] >> ((3-(i%4))*8)); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -406,9 +398,7 @@ public class KISA_SEED_CBC {
|
|
|
|
|
K = pInfo.seed_key.key_data; // Pointer of round keys
|
|
|
|
|
pInfo.encrypt = enc.value; //
|
|
|
|
|
Common.memcpy(pInfo.ivec, pbszIV, 16, ENDIAN); |
|
|
|
|
pInfo.buffer_length = 0; |
|
|
|
|
// pInfo.last_block_flag = pInfo.buffer_length = 0;
|
|
|
|
|
pInfo.last_block_flag = pInfo.buffer_length; |
|
|
|
|
pInfo.last_block_flag = pInfo.buffer_length = 0; |
|
|
|
|
|
|
|
|
|
// Set up input values for Key Schedule
|
|
|
|
|
ABCD[ABCD_A] = Common.byte_to_int(pbszUserKey, 0*4, ENDIAN); |
|
|
|
@ -746,23 +736,11 @@ public class KISA_SEED_CBC {
|
|
|
|
|
|
|
|
|
|
public KISA_SEED_INFO() { |
|
|
|
|
encrypt = 0; |
|
|
|
|
ivec[3] = 0; |
|
|
|
|
ivec[2] = ivec[3]; |
|
|
|
|
ivec[1] = ivec[2]; |
|
|
|
|
ivec[0] = ivec[1]; |
|
|
|
|
// ivec[0] = ivec[1] = ivec[2] = ivec[3] = 0;
|
|
|
|
|
ivec[0] = ivec[1] = ivec[2] = ivec[3] = 0; |
|
|
|
|
seed_key.init(); |
|
|
|
|
cbc_buffer[3] = 0; |
|
|
|
|
cbc_buffer[2] = cbc_buffer[3]; |
|
|
|
|
cbc_buffer[1] = cbc_buffer[2]; |
|
|
|
|
cbc_buffer[0] = cbc_buffer[1]; |
|
|
|
|
// cbc_buffer[0] = cbc_buffer[1] = cbc_buffer[2] = cbc_buffer[3] = 0;
|
|
|
|
|
cbc_buffer[0] = cbc_buffer[1] = cbc_buffer[2] = cbc_buffer[3] = 0; |
|
|
|
|
buffer_length = 0; |
|
|
|
|
cbc_last_block[3] = 0; |
|
|
|
|
cbc_last_block[2] = cbc_last_block[3]; |
|
|
|
|
cbc_last_block[1] = cbc_last_block[2]; |
|
|
|
|
cbc_last_block[0] = cbc_last_block[1]; |
|
|
|
|
// cbc_last_block[0] = cbc_last_block[1] = cbc_last_block[2] = cbc_last_block[3] = 0;
|
|
|
|
|
cbc_last_block[0] = cbc_last_block[1] = cbc_last_block[2] = cbc_last_block[3] = 0; |
|
|
|
|
last_block_flag = 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -822,21 +800,13 @@ public class KISA_SEED_CBC {
|
|
|
|
|
int mask_value = 0x0ff << shift_value; |
|
|
|
|
int mask_value2 = ~mask_value; |
|
|
|
|
int value2 = (value&0x0ff) << shift_value; |
|
|
|
|
|
|
|
|
|
int dstValue = dst[b_offset / 4]; |
|
|
|
|
dstValue = (dstValue & mask_value2) | (value2 & mask_value); |
|
|
|
|
dst[b_offset / 4] = dstValue; |
|
|
|
|
// dst[b_offset/4] = (dst[b_offset/4] & mask_value2) | (value2 & mask_value);
|
|
|
|
|
dst[b_offset/4] = (dst[b_offset/4] & mask_value2) | (value2 & mask_value); |
|
|
|
|
} else { |
|
|
|
|
int shift_value = (b_offset%4)*8; |
|
|
|
|
int mask_value = 0x0ff << shift_value; |
|
|
|
|
int mask_value2 = ~mask_value; |
|
|
|
|
int value2 = (value&0x0ff) << shift_value; |
|
|
|
|
|
|
|
|
|
int dstValue = dst[b_offset / 4]; |
|
|
|
|
dstValue = (dstValue & mask_value2) | (value2 & mask_value); |
|
|
|
|
dst[b_offset / 4] = dstValue; |
|
|
|
|
// dst[b_offset/4] = (dst[b_offset/4] & mask_value2) | (value2 & mask_value);
|
|
|
|
|
dst[b_offset/4] = (dst[b_offset/4] & mask_value2) | (value2 & mask_value); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -844,18 +814,12 @@ public class KISA_SEED_CBC {
|
|
|
|
|
if(ENDIAN == BIG_ENDIAN) { |
|
|
|
|
int shift_value = (3-b_offset%4)*8; |
|
|
|
|
int mask_value = 0x0ff << shift_value; |
|
|
|
|
int mask_value2 = src[b_offset/4]; |
|
|
|
|
int value2 = mask_value2 & mask_value; |
|
|
|
|
int value = value2 >> shift_value; |
|
|
|
|
// int value = (src[b_offset/4] & mask_value) >> shift_value;
|
|
|
|
|
int value = (src[b_offset/4] & mask_value) >> shift_value; |
|
|
|
|
return (byte)value; |
|
|
|
|
} else { |
|
|
|
|
int shift_value = (b_offset%4)*8; |
|
|
|
|
int mask_value = 0x0ff << shift_value; |
|
|
|
|
int mask_value2 = src[b_offset/4]; |
|
|
|
|
int value2 = mask_value2 & mask_value; |
|
|
|
|
int value = value2 >> shift_value; |
|
|
|
|
// int value = (src[b_offset/4] & mask_value) >> shift_value;
|
|
|
|
|
int value = (src[b_offset/4] & mask_value) >> shift_value; |
|
|
|
|
return (byte)value; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -872,13 +836,10 @@ public class KISA_SEED_CBC {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static void byte_to_int(int[] dst, int dst_offset, byte[] src, int src_offset, int ENDIAN) { |
|
|
|
|
int offset1 = src_offset + 1; |
|
|
|
|
int offset2 = src_offset + 2; |
|
|
|
|
int offset3 = src_offset + 3; |
|
|
|
|
if(ENDIAN == BIG_ENDIAN) { |
|
|
|
|
dst[dst_offset] = ((0x0ff&src[src_offset]) << 24) | ((0x0ff&src[offset1]) << 16) | ((0x0ff&src[offset2]) << 8) | ((0x0ff&src[offset3])); |
|
|
|
|
dst[dst_offset] = ((0x0ff&src[src_offset]) << 24) | ((0x0ff&src[src_offset+1]) << 16) | ((0x0ff&src[src_offset+2]) << 8) | ((0x0ff&src[src_offset+3])); |
|
|
|
|
} else { |
|
|
|
|
dst[dst_offset] = ((0x0ff&src[src_offset])) | ((0x0ff&src[offset1]) << 8) | ((0x0ff&src[offset2]) << 16) | ((0x0ff&src[offset3]) << 24); |
|
|
|
|
dst[dst_offset] = ((0x0ff&src[src_offset])) | ((0x0ff&src[src_offset+1]) << 8) | ((0x0ff&src[src_offset+2]) << 16) | ((0x0ff&src[src_offset+3]) << 24); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -926,8 +887,7 @@ public class KISA_SEED_CBC {
|
|
|
|
|
if(n >= 32) |
|
|
|
|
return 0; |
|
|
|
|
int v = x >> n; |
|
|
|
|
int shift = n - 1; |
|
|
|
|
int v_mask = ~(0x80000000 >> shift); |
|
|
|
|
int v_mask = ~(0x80000000 >> (n-1)); |
|
|
|
|
return v & v_mask; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -980,9 +940,7 @@ public class KISA_SEED_CBC {
|
|
|
|
|
K = pInfo.seed_key.key_data; // Pointer of round keys
|
|
|
|
|
pInfo.encrypt = enc.value; //
|
|
|
|
|
Common.memcpy(pInfo.ivec, pbszIV, 16, ENDIAN); |
|
|
|
|
pInfo.buffer_length = 0; |
|
|
|
|
// pInfo.last_block_flag = pInfo.buffer_length = 0;
|
|
|
|
|
pInfo.last_block_flag = pInfo.buffer_length; |
|
|
|
|
pInfo.last_block_flag = pInfo.buffer_length = 0; |
|
|
|
|
|
|
|
|
|
// Set up input values for Key Schedule
|
|
|
|
|
ABCD[ABCD_A] = Common.byte_to_int(pbszUserKey, 0*4, ENDIAN); |
|
|
|
@ -1084,10 +1042,10 @@ public class KISA_SEED_CBC {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// byte[] defaultCipherText = SEED_CBC_Encrypt(pbUserKey, bszIV, pbData, 0, PLAINTEXT_LENGTH);
|
|
|
|
|
//
|
|
|
|
|
//
|
|
|
|
|
// byte[] PPPPP = SEED_CBC_Decrypt(pbUserKey, bszIV, defaultCipherText, 0, CIPHERTEXT_LENGTH);
|
|
|
|
|
byte[] defaultCipherText = SEED_CBC_Encrypt(pbUserKey, bszIV, pbData, 0, PLAINTEXT_LENGTH); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
byte[] PPPPP = SEED_CBC_Decrypt(pbUserKey, bszIV, defaultCipherText, 0, CIPHERTEXT_LENGTH); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//System.out.print("\nIV\t\t\t\t: ");
|
|
|
|
@ -1106,9 +1064,9 @@ public class KISA_SEED_CBC {
|
|
|
|
|
//System.out.print("\n\n");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// byte[] Cipher1 = SEED_CBC_Encrypt(pbUserKey, bszIV, pbData1,0, 2);
|
|
|
|
|
//
|
|
|
|
|
// byte[] Plain1 = SEED_CBC_Decrypt(pbUserKey, bszIV, Cipher1, 0, 16);
|
|
|
|
|
byte[] Cipher1 = SEED_CBC_Encrypt(pbUserKey, bszIV, pbData1,0, 2); |
|
|
|
|
|
|
|
|
|
byte[] Plain1 = SEED_CBC_Decrypt(pbUserKey, bszIV, Cipher1, 0, 16); |
|
|
|
|
|
|
|
|
|
//System.out.print("IV\t\t\t\t: ");
|
|
|
|
|
//for (int i=0; i<16; i++)
|
|
|
|
@ -1126,9 +1084,9 @@ public class KISA_SEED_CBC {
|
|
|
|
|
//System.out.print("\n\n");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// byte[] Cipher2 = SEED_CBC_Encrypt(pbUserKey, bszIV, pbData2,0, 16);
|
|
|
|
|
//
|
|
|
|
|
// byte[] Plain2 = SEED_CBC_Decrypt(pbUserKey, bszIV, Cipher2, 0, 32);
|
|
|
|
|
byte[] Cipher2 = SEED_CBC_Encrypt(pbUserKey, bszIV, pbData2,0, 16); |
|
|
|
|
|
|
|
|
|
byte[] Plain2 = SEED_CBC_Decrypt(pbUserKey, bszIV, Cipher2, 0, 32); |
|
|
|
|
|
|
|
|
|
//System.out.print("IV\t\t\t\t: ");
|
|
|
|
|
//for (int i=0; i<16; i++)
|
|
|
|
@ -1148,9 +1106,9 @@ public class KISA_SEED_CBC {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// byte[] Cipher3 = SEED_CBC_Encrypt(pbUserKey, bszIV, pbData3, 0, 18);
|
|
|
|
|
//
|
|
|
|
|
// byte[] Plain3 = SEED_CBC_Decrypt(pbUserKey, bszIV, Cipher3, 0, 32);
|
|
|
|
|
byte[] Cipher3 = SEED_CBC_Encrypt(pbUserKey, bszIV, pbData3, 0, 18); |
|
|
|
|
|
|
|
|
|
byte[] Plain3 = SEED_CBC_Decrypt(pbUserKey, bszIV, Cipher3, 0, 32); |
|
|
|
|
|
|
|
|
|
// System.out.print("IV\t\t\t\t: ");
|
|
|
|
|
// for (int i=0; i<16; i++)
|
|
|
|
@ -1232,7 +1190,7 @@ public class KISA_SEED_CBC {
|
|
|
|
|
|
|
|
|
|
int[] outbuf = new int[process_blockLeng/4]; |
|
|
|
|
|
|
|
|
|
for(j=0; j<PLAINTEXT_LENGTH-process_blockLeng; j += process_blockLeng) |
|
|
|
|
for(j=0; j<PLAINTEXT_LENGTH-process_blockLeng; ) |
|
|
|
|
{ |
|
|
|
|
System.arraycopy(pbData, j, newpbszPlainText, 0, process_blockLeng); |
|
|
|
|
data = chartoint32_for_SEED_CBC(newpbszPlainText, process_blockLeng); |
|
|
|
@ -1317,7 +1275,7 @@ public class KISA_SEED_CBC {
|
|
|
|
|
byte []pbszPlainText = new byte[nCipherTextLen]; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for(j=0; j<nCipherTextLen - process_blockLeng; j+= process_blockLeng) |
|
|
|
|
for(j=0; j<nCipherTextLen - process_blockLeng; ) |
|
|
|
|
{ |
|
|
|
|
System.arraycopy(message, j, newpbszCipherText, 0, process_blockLeng); |
|
|
|
|
data = chartoint32_for_SEED_CBC(newpbszCipherText, process_blockLeng); |
|
|
|
@ -1419,7 +1377,7 @@ public class KISA_SEED_CBC {
|
|
|
|
|
outbuf = new int[process_blockLeng/4]; |
|
|
|
|
pbszPlainText = new byte[process_blockLeng]; |
|
|
|
|
|
|
|
|
|
for(j=0; j<PLAINTEXT_LENGTH-process_blockLeng; j+= process_blockLeng) |
|
|
|
|
for(j=0; j<PLAINTEXT_LENGTH-process_blockLeng; ) |
|
|
|
|
{ |
|
|
|
|
System.arraycopy(pbData1, j, newpbszPlainText, 0, process_blockLeng); |
|
|
|
|
data = chartoint32_for_SEED_CBC(newpbszPlainText, process_blockLeng); |
|
|
|
@ -1508,7 +1466,7 @@ public class KISA_SEED_CBC {
|
|
|
|
|
pbszPlainText = new byte[nCipherTextLen]; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for(j=0; j<nCipherTextLen - process_blockLeng; j+= process_blockLeng) |
|
|
|
|
for(j=0; j<nCipherTextLen - process_blockLeng; ) |
|
|
|
|
{ |
|
|
|
|
System.arraycopy(message, j, newpbszCipherText, 0, process_blockLeng); |
|
|
|
|
data = chartoint32_for_SEED_CBC(newpbszCipherText, process_blockLeng); |
|
|
|
@ -1595,7 +1553,7 @@ public class KISA_SEED_CBC {
|
|
|
|
|
outbuf = new int[process_blockLeng/4]; |
|
|
|
|
pbszPlainText = new byte[process_blockLeng]; |
|
|
|
|
|
|
|
|
|
for(j=0; j<PLAINTEXT_LENGTH-process_blockLeng; j+= process_blockLeng) |
|
|
|
|
for(j=0; j<PLAINTEXT_LENGTH-process_blockLeng; ) |
|
|
|
|
{ |
|
|
|
|
System.arraycopy(pbData2, j, newpbszPlainText, 0, process_blockLeng); |
|
|
|
|
data = chartoint32_for_SEED_CBC(newpbszPlainText, process_blockLeng); |
|
|
|
@ -1683,7 +1641,7 @@ public class KISA_SEED_CBC {
|
|
|
|
|
pbszPlainText = new byte[nCipherTextLen]; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for(j=0; j<nCipherTextLen - process_blockLeng; j+= process_blockLeng) |
|
|
|
|
for(j=0; j<nCipherTextLen - process_blockLeng; ) |
|
|
|
|
{ |
|
|
|
|
System.arraycopy(message, j, newpbszCipherText, 0, process_blockLeng); |
|
|
|
|
data = chartoint32_for_SEED_CBC(newpbszCipherText, process_blockLeng); |
|
|
|
@ -1772,7 +1730,7 @@ public class KISA_SEED_CBC {
|
|
|
|
|
outbuf = new int[process_blockLeng/4]; |
|
|
|
|
pbszPlainText = new byte[process_blockLeng]; |
|
|
|
|
|
|
|
|
|
for(j=0; j<PLAINTEXT_LENGTH-process_blockLeng; j+= process_blockLeng) |
|
|
|
|
for(j=0; j<PLAINTEXT_LENGTH-process_blockLeng; ) |
|
|
|
|
{ |
|
|
|
|
System.arraycopy(pbData3, j, newpbszPlainText, 0, process_blockLeng); |
|
|
|
|
data = chartoint32_for_SEED_CBC(newpbszPlainText, process_blockLeng); |
|
|
|
@ -1861,7 +1819,7 @@ public class KISA_SEED_CBC {
|
|
|
|
|
pbszPlainText = new byte[nCipherTextLen]; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for(j=0; j<nCipherTextLen - process_blockLeng; j+= process_blockLeng) |
|
|
|
|
for(j=0; j<nCipherTextLen - process_blockLeng; ) |
|
|
|
|
{ |
|
|
|
|
System.arraycopy(message, j, newpbszCipherText, 0, process_blockLeng); |
|
|
|
|
data = chartoint32_for_SEED_CBC(newpbszCipherText, process_blockLeng); |
|
|
|
|