private int processBlock192or256(byte[] input, int inOff, byte[] output, int outOff) { for (int i = 0; i < 4; i++) { this.state[i] = CamelliaLightEngine.bytes2uint(input, inOff + i * 4); this.state[i] ^= this.kw[i]; } this.camelliaF2(this.state, this.subkey, 0); this.camelliaF2(this.state, this.subkey, 4); this.camelliaF2(this.state, this.subkey, 8); this.camelliaFLs(this.state, this.ke, 0); this.camelliaF2(this.state, this.subkey, 12); this.camelliaF2(this.state, this.subkey, 16); this.camelliaF2(this.state, this.subkey, 20); this.camelliaFLs(this.state, this.ke, 4); this.camelliaF2(this.state, this.subkey, 24); this.camelliaF2(this.state, this.subkey, 28); this.camelliaF2(this.state, this.subkey, 32); this.camelliaFLs(this.state, this.ke, 8); this.camelliaF2(this.state, this.subkey, 36); this.camelliaF2(this.state, this.subkey, 40); this.camelliaF2(this.state, this.subkey, 44); this.state[2] ^= this.kw[4]; this.state[3] ^= this.kw[5]; this.state[0] ^= this.kw[6]; this.state[1] ^= this.kw[7]; CamelliaLightEngine.uint2bytes(this.state[2], output, outOff); CamelliaLightEngine.uint2bytes(this.state[3], output, outOff + 4); CamelliaLightEngine.uint2bytes(this.state[0], output, outOff + 8); CamelliaLightEngine.uint2bytes(this.state[1], output, outOff + 12); return(16); }
private void camelliaFLs(uint[] s, uint[] fkey, int keyoff) { s[1] ^= CamelliaLightEngine.leftRotate(s[0] & fkey[keyoff], 1); s[0] ^= (fkey[1 + keyoff] | s[1]); s[2] ^= (fkey[3 + keyoff] | s[3]); s[3] ^= CamelliaLightEngine.leftRotate(fkey[2 + keyoff] & s[2], 1); }
private void camelliaF2(uint[] s, uint[] skey, int keyoff) { uint num = s[0] ^ skey[keyoff]; uint num2 = this.sbox4((int)((byte)num)); num2 |= this.sbox3((int)((byte)(num >> 8))) << 8; num2 |= this.sbox2((int)((byte)(num >> 16))) << 16; num2 |= (uint)((uint)CamelliaLightEngine.SBOX1[(int)((byte)(num >> 24))] << 24); uint num3 = s[1] ^ skey[1 + keyoff]; uint num4 = (uint)CamelliaLightEngine.SBOX1[(int)((byte)num3)]; num4 |= this.sbox4((int)((byte)(num3 >> 8))) << 8; num4 |= this.sbox3((int)((byte)(num3 >> 16))) << 16; num4 |= this.sbox2((int)((byte)(num3 >> 24))) << 24; num4 = CamelliaLightEngine.leftRotate(num4, 8); num2 ^= num4; num4 = (CamelliaLightEngine.leftRotate(num4, 8) ^ num2); num2 = (CamelliaLightEngine.rightRotate(num2, 8) ^ num4); s[2] ^= (CamelliaLightEngine.leftRotate(num4, 16) ^ num2); s[3] ^= CamelliaLightEngine.leftRotate(num2, 8); num = (s[2] ^ skey[2 + keyoff]); num2 = this.sbox4((int)((byte)num)); num2 |= this.sbox3((int)((byte)(num >> 8))) << 8; num2 |= this.sbox2((int)((byte)(num >> 16))) << 16; num2 |= (uint)((uint)CamelliaLightEngine.SBOX1[(int)((byte)(num >> 24))] << 24); num3 = (s[3] ^ skey[3 + keyoff]); num4 = (uint)CamelliaLightEngine.SBOX1[(int)((byte)num3)]; num4 |= this.sbox4((int)((byte)(num3 >> 8))) << 8; num4 |= this.sbox3((int)((byte)(num3 >> 16))) << 16; num4 |= this.sbox2((int)((byte)(num3 >> 24))) << 24; num4 = CamelliaLightEngine.leftRotate(num4, 8); num2 ^= num4; num4 = (CamelliaLightEngine.leftRotate(num4, 8) ^ num2); num2 = (CamelliaLightEngine.rightRotate(num2, 8) ^ num4); s[0] ^= (CamelliaLightEngine.leftRotate(num4, 16) ^ num2); s[1] ^= CamelliaLightEngine.leftRotate(num2, 8); }
private BufferedBlockCipher CreateScryptoEngine() { IBlockCipher engine; switch (_MSec.Algorithm) { case ESec.SCRYPTO_AES: engine = new AesEngine(); _MSec.KeySize = 32; _MSec.IVSize = 0; break; case ESec.SCRYPTO_AESFAST: engine = new AesFastEngine(); _MSec.KeySize = 32; _MSec.IVSize = 0; break; case ESec.SCRYPTO_AESLIGHT: engine = new AesLightEngine(); _MSec.KeySize = 32; _MSec.IVSize = 0; break; case ESec.SCRYPTO_BLOWFISH: engine = new BlowfishEngine(); _MSec.KeySize = 56; _MSec.IVSize = 0; break; case ESec.SCRYPTO_CAMELLIA: engine = new CamelliaEngine(); _MSec.KeySize = 32; _MSec.IVSize = 0; break; case ESec.SCRYPTO_CAMELLIALIGHT: engine = new CamelliaLightEngine(); _MSec.KeySize = 32; _MSec.IVSize = 0; break; case ESec.SCRYPTO_CAST5: engine = new Cast5Engine(); _MSec.KeySize = 16; _MSec.IVSize = 0; break; case ESec.SCRYPTO_CAST6: engine = new Cast6Engine(); _MSec.KeySize = 32; _MSec.IVSize = 0; break; case ESec.SCRYPTO_DES: engine = new DesEngine(); _MSec.KeySize = 8; _MSec.IVSize = 0; break; case ESec.SCRYPTO_DESEDE: engine = new DesEdeEngine(); _MSec.KeySize = 24; _MSec.IVSize = 0; break; case ESec.SCRYPTO_GOST28147: engine = new Gost28147Engine(); _MSec.KeySize = 32; _MSec.IVSize = 0; break; case ESec.SCRYPTO_NOEKEON: engine = new NoekeonEngine(); _MSec.KeySize = 16; _MSec.IVSize = 0; break; case ESec.SCRYPTO_NULL: engine = new NullEngine(); _MSec.KeySize = 32; _MSec.IVSize = 16; break; case ESec.SCRYPTO_RC2: engine = new RC2Engine(); _MSec.KeySize = 128; _MSec.IVSize = 0; break; case ESec.SCRYPTO_RC532: engine = new RC532Engine(); _MSec.KeySize = 16; _MSec.IVSize = 0; break; case ESec.SCRYPTO_RC564: engine = new RC564Engine(); _MSec.KeySize = 16; _MSec.IVSize = 0; break; case ESec.SCRYPTO_RC6: engine = new RC6Engine(); _MSec.KeySize = 32; _MSec.IVSize = 0; break; case ESec.SCRYPTO_RIJNDAEL: engine = new RijndaelEngine(); _MSec.KeySize = 32; _MSec.IVSize = 0; break; case ESec.SCRYPTO_SEED: engine = new SeedEngine(); _MSec.KeySize = 16; _MSec.IVSize = 0; break; case ESec.SCRYPTO_SERPENT: engine = new SerpentEngine(); _MSec.KeySize = 32; _MSec.IVSize = 0; break; case ESec.SCRYPTO_SKIPJACK: engine = new SkipjackEngine(); _MSec.KeySize = 16; _MSec.IVSize = 0; break; case ESec.SCRYPTO_TEA: engine = new TeaEngine(); _MSec.KeySize = 16; _MSec.IVSize = 0; break; case ESec.SCRYPTO_TWOFISH: engine = new TwofishEngine(); _MSec.KeySize = 32; _MSec.IVSize = 0; break; case ESec.SCRYPTO_XTEA: engine = new XteaEngine(); _MSec.KeySize = 16; _MSec.IVSize = 0; break; default: engine = new AesEngine(); _MSec.KeySize = 32; _MSec.IVSize = 0; break; } switch (_MSec.Mode) { case ESec.MODE_CBC: engine = new CbcBlockCipher(engine); break; case ESec.MODE_CFB: engine = new CfbBlockCipher(engine, 8); break; case ESec.MODE_GOFB: engine = new GOfbBlockCipher(engine); break; case ESec.MODE_OFB: engine = new OfbBlockCipher(engine, 8); break; case ESec.MODE_OPENPGPCFB: engine = new OpenPgpCfbBlockCipher(engine); break; case ESec.MODE_SIC: engine = new SicBlockCipher(engine); break; default: engine = new CbcBlockCipher(engine); break; } IBlockCipherPadding padding = null; switch (_MSec.Padding) { case ESec.PADDING_ISO10126d2: padding = new ISO10126d2Padding(); break; case ESec.PADDING_ISO7816d4: padding = new ISO7816d4Padding(); break; case ESec.PADDING_PKCS7: padding = new Pkcs7Padding(); break; case ESec.PADDING_TBC: padding = new TbcPadding(); break; case ESec.PADDING_X923: padding = new X923Padding(); break; case ESec.PADDING_ZEROBYTE: padding = new ZeroBytePadding(); break; default: padding = new Pkcs7Padding(); break; } return new PaddedBufferedBlockCipher(engine, padding); }
private void setKey(bool forEncryption, byte[] key) { uint[] array = new uint[8]; uint[] array2 = new uint[4]; uint[] array3 = new uint[4]; uint[] array4 = new uint[4]; int num = key.Length; if (num != 16) { if (num != 24) { if (num != 32) { throw new ArgumentException("key sizes are only 16/24/32 bytes."); } array[0] = CamelliaLightEngine.bytes2uint(key, 0); array[1] = CamelliaLightEngine.bytes2uint(key, 4); array[2] = CamelliaLightEngine.bytes2uint(key, 8); array[3] = CamelliaLightEngine.bytes2uint(key, 12); array[4] = CamelliaLightEngine.bytes2uint(key, 16); array[5] = CamelliaLightEngine.bytes2uint(key, 20); array[6] = CamelliaLightEngine.bytes2uint(key, 24); array[7] = CamelliaLightEngine.bytes2uint(key, 28); this._keyis128 = false; } else { array[0] = CamelliaLightEngine.bytes2uint(key, 0); array[1] = CamelliaLightEngine.bytes2uint(key, 4); array[2] = CamelliaLightEngine.bytes2uint(key, 8); array[3] = CamelliaLightEngine.bytes2uint(key, 12); array[4] = CamelliaLightEngine.bytes2uint(key, 16); array[5] = CamelliaLightEngine.bytes2uint(key, 20); array[6] = ~array[4]; array[7] = ~array[5]; this._keyis128 = false; } } else { this._keyis128 = true; array[0] = CamelliaLightEngine.bytes2uint(key, 0); array[1] = CamelliaLightEngine.bytes2uint(key, 4); array[2] = CamelliaLightEngine.bytes2uint(key, 8); array[3] = CamelliaLightEngine.bytes2uint(key, 12); array[4] = (array[5] = (array[6] = (array[7] = 0u))); } for (int i = 0; i < 4; i++) { array2[i] = (array[i] ^ array[i + 4]); } this.camelliaF2(array2, CamelliaLightEngine.SIGMA, 0); for (int j = 0; j < 4; j++) { array2[j] ^= array[j]; } this.camelliaF2(array2, CamelliaLightEngine.SIGMA, 4); if (this._keyis128) { if (forEncryption) { this.kw[0] = array[0]; this.kw[1] = array[1]; this.kw[2] = array[2]; this.kw[3] = array[3]; CamelliaLightEngine.roldq(15, array, 0, this.subkey, 4); CamelliaLightEngine.roldq(30, array, 0, this.subkey, 12); CamelliaLightEngine.roldq(15, array, 0, array4, 0); this.subkey[18] = array4[2]; this.subkey[19] = array4[3]; CamelliaLightEngine.roldq(17, array, 0, this.ke, 4); CamelliaLightEngine.roldq(17, array, 0, this.subkey, 24); CamelliaLightEngine.roldq(17, array, 0, this.subkey, 32); this.subkey[0] = array2[0]; this.subkey[1] = array2[1]; this.subkey[2] = array2[2]; this.subkey[3] = array2[3]; CamelliaLightEngine.roldq(15, array2, 0, this.subkey, 8); CamelliaLightEngine.roldq(15, array2, 0, this.ke, 0); CamelliaLightEngine.roldq(15, array2, 0, array4, 0); this.subkey[16] = array4[0]; this.subkey[17] = array4[1]; CamelliaLightEngine.roldq(15, array2, 0, this.subkey, 20); CamelliaLightEngine.roldqo32(34, array2, 0, this.subkey, 28); CamelliaLightEngine.roldq(17, array2, 0, this.kw, 4); return; } this.kw[4] = array[0]; this.kw[5] = array[1]; this.kw[6] = array[2]; this.kw[7] = array[3]; CamelliaLightEngine.decroldq(15, array, 0, this.subkey, 28); CamelliaLightEngine.decroldq(30, array, 0, this.subkey, 20); CamelliaLightEngine.decroldq(15, array, 0, array4, 0); this.subkey[16] = array4[0]; this.subkey[17] = array4[1]; CamelliaLightEngine.decroldq(17, array, 0, this.ke, 0); CamelliaLightEngine.decroldq(17, array, 0, this.subkey, 8); CamelliaLightEngine.decroldq(17, array, 0, this.subkey, 0); this.subkey[34] = array2[0]; this.subkey[35] = array2[1]; this.subkey[32] = array2[2]; this.subkey[33] = array2[3]; CamelliaLightEngine.decroldq(15, array2, 0, this.subkey, 24); CamelliaLightEngine.decroldq(15, array2, 0, this.ke, 4); CamelliaLightEngine.decroldq(15, array2, 0, array4, 0); this.subkey[18] = array4[2]; this.subkey[19] = array4[3]; CamelliaLightEngine.decroldq(15, array2, 0, this.subkey, 12); CamelliaLightEngine.decroldqo32(34, array2, 0, this.subkey, 4); CamelliaLightEngine.roldq(17, array2, 0, this.kw, 0); return; } else { for (int k = 0; k < 4; k++) { array3[k] = (array2[k] ^ array[k + 4]); } this.camelliaF2(array3, CamelliaLightEngine.SIGMA, 8); if (forEncryption) { this.kw[0] = array[0]; this.kw[1] = array[1]; this.kw[2] = array[2]; this.kw[3] = array[3]; CamelliaLightEngine.roldqo32(45, array, 0, this.subkey, 16); CamelliaLightEngine.roldq(15, array, 0, this.ke, 4); CamelliaLightEngine.roldq(17, array, 0, this.subkey, 32); CamelliaLightEngine.roldqo32(34, array, 0, this.subkey, 44); CamelliaLightEngine.roldq(15, array, 4, this.subkey, 4); CamelliaLightEngine.roldq(15, array, 4, this.ke, 0); CamelliaLightEngine.roldq(30, array, 4, this.subkey, 24); CamelliaLightEngine.roldqo32(34, array, 4, this.subkey, 36); CamelliaLightEngine.roldq(15, array2, 0, this.subkey, 8); CamelliaLightEngine.roldq(30, array2, 0, this.subkey, 20); this.ke[8] = array2[1]; this.ke[9] = array2[2]; this.ke[10] = array2[3]; this.ke[11] = array2[0]; CamelliaLightEngine.roldqo32(49, array2, 0, this.subkey, 40); this.subkey[0] = array3[0]; this.subkey[1] = array3[1]; this.subkey[2] = array3[2]; this.subkey[3] = array3[3]; CamelliaLightEngine.roldq(30, array3, 0, this.subkey, 12); CamelliaLightEngine.roldq(30, array3, 0, this.subkey, 28); CamelliaLightEngine.roldqo32(51, array3, 0, this.kw, 4); return; } this.kw[4] = array[0]; this.kw[5] = array[1]; this.kw[6] = array[2]; this.kw[7] = array[3]; CamelliaLightEngine.decroldqo32(45, array, 0, this.subkey, 28); CamelliaLightEngine.decroldq(15, array, 0, this.ke, 4); CamelliaLightEngine.decroldq(17, array, 0, this.subkey, 12); CamelliaLightEngine.decroldqo32(34, array, 0, this.subkey, 0); CamelliaLightEngine.decroldq(15, array, 4, this.subkey, 40); CamelliaLightEngine.decroldq(15, array, 4, this.ke, 8); CamelliaLightEngine.decroldq(30, array, 4, this.subkey, 20); CamelliaLightEngine.decroldqo32(34, array, 4, this.subkey, 8); CamelliaLightEngine.decroldq(15, array2, 0, this.subkey, 36); CamelliaLightEngine.decroldq(30, array2, 0, this.subkey, 24); this.ke[2] = array2[1]; this.ke[3] = array2[2]; this.ke[0] = array2[3]; this.ke[1] = array2[0]; CamelliaLightEngine.decroldqo32(49, array2, 0, this.subkey, 4); this.subkey[46] = array3[0]; this.subkey[47] = array3[1]; this.subkey[44] = array3[2]; this.subkey[45] = array3[3]; CamelliaLightEngine.decroldq(30, array3, 0, this.subkey, 32); CamelliaLightEngine.decroldq(30, array3, 0, this.subkey, 16); CamelliaLightEngine.roldqo32(51, array3, 0, this.kw, 0); return; } }