📄 camelliaengine.java
字号:
s[1] ^= leftRotate(s[0] & fkey[0 + keyoff], 1); s[0] ^= fkey[1 + keyoff] | s[1]; s[2] ^= fkey[3 + keyoff] | s[3]; s[3] ^= leftRotate(fkey[2 + keyoff] & s[2], 1); } private final void setKey(boolean forEncryption, byte[] key) { int[] k = new int[8]; int[] ka = new int[4]; int[] kb = new int[4]; int[] t = new int[4]; switch (key.length) { case 16: _keyIs128 = true; k[0] = bytes2int(key, 0); k[1] = bytes2int(key, 4); k[2] = bytes2int(key, 8); k[3] = bytes2int(key, 12); k[4] = k[5] = k[6] = k[7] = 0; break; case 24: k[0] = bytes2int(key, 0); k[1] = bytes2int(key, 4); k[2] = bytes2int(key, 8); k[3] = bytes2int(key, 12); k[4] = bytes2int(key, 16); k[5] = bytes2int(key, 20); k[6] = ~k[4]; k[7] = ~k[5]; _keyIs128 = false; break; case 32: k[0] = bytes2int(key, 0); k[1] = bytes2int(key, 4); k[2] = bytes2int(key, 8); k[3] = bytes2int(key, 12); k[4] = bytes2int(key, 16); k[5] = bytes2int(key, 20); k[6] = bytes2int(key, 24); k[7] = bytes2int(key, 28); _keyIs128 = false; break; default: throw new IllegalArgumentException("key sizes are only 16/24/32 bytes."); } for (int i = 0; i < 4; i++) { ka[i] = k[i] ^ k[i + 4]; } /* compute KA */ camelliaF2(ka, SIGMA, 0); for (int i = 0; i < 4; i++) { ka[i] ^= k[i]; } camelliaF2(ka, SIGMA, 4); if (_keyIs128) { if (forEncryption) { /* KL dependant keys */ kw[0] = k[0]; kw[1] = k[1]; kw[2] = k[2]; kw[3] = k[3]; roldq(15, k, 0, subkey, 4); roldq(30, k, 0, subkey, 12); roldq(15, k, 0, t, 0); subkey[18] = t[2]; subkey[19] = t[3]; roldq(17, k, 0, ke, 4); roldq(17, k, 0, subkey, 24); roldq(17, k, 0, subkey, 32); /* KA dependant keys */ subkey[0] = ka[0]; subkey[1] = ka[1]; subkey[2] = ka[2]; subkey[3] = ka[3]; roldq(15, ka, 0, subkey, 8); roldq(15, ka, 0, ke, 0); roldq(15, ka, 0, t, 0); subkey[16] = t[0]; subkey[17] = t[1]; roldq(15, ka, 0, subkey, 20); roldqo32(34, ka, 0, subkey, 28); roldq(17, ka, 0, kw, 4); } else { // decryption /* KL dependant keys */ kw[4] = k[0]; kw[5] = k[1]; kw[6] = k[2]; kw[7] = k[3]; decroldq(15, k, 0, subkey, 28); decroldq(30, k, 0, subkey, 20); decroldq(15, k, 0, t, 0); subkey[16] = t[0]; subkey[17] = t[1]; decroldq(17, k, 0, ke, 0); decroldq(17, k, 0, subkey, 8); decroldq(17, k, 0, subkey, 0); /* KA dependant keys */ subkey[34] = ka[0]; subkey[35] = ka[1]; subkey[32] = ka[2]; subkey[33] = ka[3]; decroldq(15, ka, 0, subkey, 24); decroldq(15, ka, 0, ke, 4); decroldq(15, ka, 0, t, 0); subkey[18] = t[2]; subkey[19] = t[3]; decroldq(15, ka, 0, subkey, 12); decroldqo32(34, ka, 0, subkey, 4); roldq(17, ka, 0, kw, 0); } } else { // 192bit or 256bit /* compute KB */ for (int i = 0; i < 4; i++) { kb[i] = ka[i] ^ k[i + 4]; } camelliaF2(kb, SIGMA, 8); if (forEncryption) { /* KL dependant keys */ kw[0] = k[0]; kw[1] = k[1]; kw[2] = k[2]; kw[3] = k[3]; roldqo32(45, k, 0, subkey, 16); roldq(15, k, 0, ke, 4); roldq(17, k, 0, subkey, 32); roldqo32(34, k, 0, subkey, 44); /* KR dependant keys */ roldq(15, k, 4, subkey, 4); roldq(15, k, 4, ke, 0); roldq(30, k, 4, subkey, 24); roldqo32(34, k, 4, subkey, 36); /* KA dependant keys */ roldq(15, ka, 0, subkey, 8); roldq(30, ka, 0, subkey, 20); /* 32bit rotation */ ke[8] = ka[1]; ke[9] = ka[2]; ke[10] = ka[3]; ke[11] = ka[0]; roldqo32(49, ka, 0, subkey, 40); /* KB dependant keys */ subkey[0] = kb[0]; subkey[1] = kb[1]; subkey[2] = kb[2]; subkey[3] = kb[3]; roldq(30, kb, 0, subkey, 12); roldq(30, kb, 0, subkey, 28); roldqo32(51, kb, 0, kw, 4); } else { // decryption /* KL dependant keys */ kw[4] = k[0]; kw[5] = k[1]; kw[6] = k[2]; kw[7] = k[3]; decroldqo32(45, k, 0, subkey, 28); decroldq(15, k, 0, ke, 4); decroldq(17, k, 0, subkey, 12); decroldqo32(34, k, 0, subkey, 0); /* KR dependant keys */ decroldq(15, k, 4, subkey, 40); decroldq(15, k, 4, ke, 8); decroldq(30, k, 4, subkey, 20); decroldqo32(34, k, 4, subkey, 8); /* KA dependant keys */ decroldq(15, ka, 0, subkey, 36); decroldq(30, ka, 0, subkey, 24); /* 32bit rotation */ ke[2] = ka[1]; ke[3] = ka[2]; ke[0] = ka[3]; ke[1] = ka[0]; decroldqo32(49, ka, 0, subkey, 4); /* KB dependant keys */ subkey[46] = kb[0]; subkey[47] = kb[1]; subkey[44] = kb[2]; subkey[45] = kb[3]; decroldq(30, kb, 0, subkey, 32); decroldq(30, kb, 0, subkey, 16); roldqo32(51, kb, 0, kw, 0); } } } private final int processBlock128(byte[] in, int inOff, byte[] out, int outOff) { for (int i = 0; i < 4; i++) { state[i] = bytes2int(in, inOff + (i * 4)); state[i] ^= kw[i]; } camelliaF2(state, subkey, 0); camelliaF2(state, subkey, 4); camelliaF2(state, subkey, 8); camelliaFLs(state, ke, 0); camelliaF2(state, subkey, 12); camelliaF2(state, subkey, 16); camelliaF2(state, subkey, 20); camelliaFLs(state, ke, 4); camelliaF2(state, subkey, 24); camelliaF2(state, subkey, 28); camelliaF2(state, subkey, 32); state[2] ^= kw[4]; state[3] ^= kw[5]; state[0] ^= kw[6]; state[1] ^= kw[7]; int2bytes(state[2], out, outOff); int2bytes(state[3], out, outOff + 4); int2bytes(state[0], out, outOff + 8); int2bytes(state[1], out, outOff + 12); return BLOCK_SIZE; } private final int processBlock192or256(byte[] in, int inOff, byte[] out, int outOff) { for (int i = 0; i < 4; i++) { state[i] = bytes2int(in, inOff + (i * 4)); state[i] ^= kw[i]; } camelliaF2(state, subkey, 0); camelliaF2(state, subkey, 4); camelliaF2(state, subkey, 8); camelliaFLs(state, ke, 0); camelliaF2(state, subkey, 12); camelliaF2(state, subkey, 16); camelliaF2(state, subkey, 20); camelliaFLs(state, ke, 4); camelliaF2(state, subkey, 24); camelliaF2(state, subkey, 28); camelliaF2(state, subkey, 32); camelliaFLs(state, ke, 8); camelliaF2(state, subkey, 36); camelliaF2(state, subkey, 40); camelliaF2(state, subkey, 44); state[2] ^= kw[4]; state[3] ^= kw[5]; state[0] ^= kw[6]; state[1] ^= kw[7]; int2bytes(state[2], out, outOff); int2bytes(state[3], out, outOff + 4); int2bytes(state[0], out, outOff + 8); int2bytes(state[1], out, outOff + 12); return BLOCK_SIZE; } public CamelliaEngine() { } public void init(boolean forEncryption, CipherParameters params) throws IllegalArgumentException { if (!(params instanceof KeyParameter)) { throw new IllegalArgumentException("only simple KeyParameter expected."); } setKey(forEncryption, ((KeyParameter)params).getKey()); initialised = true; } public String getAlgorithmName() { return "Camellia"; } public int getBlockSize() { return BLOCK_SIZE; } public int processBlock( byte[] in, int inOff, byte[] out, int outOff) throws DataLengthException, IllegalStateException { if (!initialised) { throw new IllegalStateException("Camellia engine not initialised"); } if ((inOff + BLOCK_SIZE) > in.length) { throw new DataLengthException("input buffer too short"); } if ((outOff + BLOCK_SIZE) > out.length) { throw new DataLengthException("output buffer too short"); } if (_keyIs128) { return processBlock128(in, inOff, out, outOff); } else { return processBlock192or256(in, inOff, out, outOff); } } public void reset() { // nothing }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -