123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326 |
- #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
- using System;
- using Org.BouncyCastle.Crypto.Digests;
- using Org.BouncyCastle.Crypto.Modes;
- using Org.BouncyCastle.Crypto.Parameters;
- using Org.BouncyCastle.Security;
- using Org.BouncyCastle.Utilities;
- namespace Org.BouncyCastle.Crypto.Engines
- {
-
- public class DesEdeWrapEngine
- : IWrapper
- {
-
- private CbcBlockCipher engine;
-
- private KeyParameter param;
-
- private ParametersWithIV paramPlusIV;
-
- private byte[] iv;
-
- private bool forWrapping;
-
- private static readonly byte[] IV2 = { (byte) 0x4a, (byte) 0xdd, (byte) 0xa2,
- (byte) 0x2c, (byte) 0x79, (byte) 0xe8,
- (byte) 0x21, (byte) 0x05 };
-
-
-
- private readonly IDigest sha1 = new Sha1Digest();
- private readonly byte[] digest = new byte[20];
-
- public virtual void Init(
- bool forWrapping,
- ICipherParameters parameters)
- {
- this.forWrapping = forWrapping;
- this.engine = new CbcBlockCipher(new DesEdeEngine());
- SecureRandom sr;
- if (parameters is ParametersWithRandom)
- {
- ParametersWithRandom pr = (ParametersWithRandom) parameters;
- parameters = pr.Parameters;
- sr = pr.Random;
- }
- else
- {
- sr = new SecureRandom();
- }
- if (parameters is KeyParameter)
- {
- this.param = (KeyParameter) parameters;
- if (this.forWrapping)
- {
-
-
- this.iv = new byte[8];
- sr.NextBytes(iv);
- this.paramPlusIV = new ParametersWithIV(this.param, this.iv);
- }
- }
- else if (parameters is ParametersWithIV)
- {
- if (!forWrapping)
- throw new ArgumentException("You should not supply an IV for unwrapping");
- this.paramPlusIV = (ParametersWithIV) parameters;
- this.iv = this.paramPlusIV.GetIV();
- this.param = (KeyParameter) this.paramPlusIV.Parameters;
- if (this.iv.Length != 8)
- throw new ArgumentException("IV is not 8 octets", "parameters");
- }
- }
-
- public virtual string AlgorithmName
- {
- get { return "DESede"; }
- }
-
- public virtual byte[] Wrap(
- byte[] input,
- int inOff,
- int length)
- {
- if (!forWrapping)
- {
- throw new InvalidOperationException("Not initialized for wrapping");
- }
- byte[] keyToBeWrapped = new byte[length];
- Array.Copy(input, inOff, keyToBeWrapped, 0, length);
-
- byte[] CKS = CalculateCmsKeyChecksum(keyToBeWrapped);
-
- byte[] WKCKS = new byte[keyToBeWrapped.Length + CKS.Length];
- Array.Copy(keyToBeWrapped, 0, WKCKS, 0, keyToBeWrapped.Length);
- Array.Copy(CKS, 0, WKCKS, keyToBeWrapped.Length, CKS.Length);
-
-
- int blockSize = engine.GetBlockSize();
- if (WKCKS.Length % blockSize != 0)
- throw new InvalidOperationException("Not multiple of block length");
- engine.Init(true, paramPlusIV);
- byte [] TEMP1 = new byte[WKCKS.Length];
- for (int currentBytePos = 0; currentBytePos != WKCKS.Length; currentBytePos += blockSize)
- {
- engine.ProcessBlock(WKCKS, currentBytePos, TEMP1, currentBytePos);
- }
-
- byte[] TEMP2 = new byte[this.iv.Length + TEMP1.Length];
- Array.Copy(this.iv, 0, TEMP2, 0, this.iv.Length);
- Array.Copy(TEMP1, 0, TEMP2, this.iv.Length, TEMP1.Length);
-
- byte[] TEMP3 = reverse(TEMP2);
-
-
-
- ParametersWithIV param2 = new ParametersWithIV(this.param, IV2);
- this.engine.Init(true, param2);
- for (int currentBytePos = 0; currentBytePos != TEMP3.Length; currentBytePos += blockSize)
- {
- engine.ProcessBlock(TEMP3, currentBytePos, TEMP3, currentBytePos);
- }
- return TEMP3;
- }
-
- public virtual byte[] Unwrap(
- byte[] input,
- int inOff,
- int length)
- {
- if (forWrapping)
- {
- throw new InvalidOperationException("Not set for unwrapping");
- }
- if (input == null)
- {
- throw new InvalidCipherTextException("Null pointer as ciphertext");
- }
- int blockSize = engine.GetBlockSize();
-
- if (length % blockSize != 0)
- {
- throw new InvalidCipherTextException("Ciphertext not multiple of " + blockSize);
- }
-
-
-
- ParametersWithIV param2 = new ParametersWithIV(this.param, IV2);
- this.engine.Init(false, param2);
- byte [] TEMP3 = new byte[length];
- for (int currentBytePos = 0; currentBytePos != TEMP3.Length; currentBytePos += blockSize)
- {
- engine.ProcessBlock(input, inOff + currentBytePos, TEMP3, currentBytePos);
- }
-
- byte[] TEMP2 = reverse(TEMP3);
-
- this.iv = new byte[8];
- byte[] TEMP1 = new byte[TEMP2.Length - 8];
- Array.Copy(TEMP2, 0, this.iv, 0, 8);
- Array.Copy(TEMP2, 8, TEMP1, 0, TEMP2.Length - 8);
-
-
- this.paramPlusIV = new ParametersWithIV(this.param, this.iv);
- this.engine.Init(false, this.paramPlusIV);
- byte[] WKCKS = new byte[TEMP1.Length];
- for (int currentBytePos = 0; currentBytePos != WKCKS.Length; currentBytePos += blockSize)
- {
- engine.ProcessBlock(TEMP1, currentBytePos, WKCKS, currentBytePos);
- }
-
-
- byte[] result = new byte[WKCKS.Length - 8];
- byte[] CKStoBeVerified = new byte[8];
- Array.Copy(WKCKS, 0, result, 0, WKCKS.Length - 8);
- Array.Copy(WKCKS, WKCKS.Length - 8, CKStoBeVerified, 0, 8);
-
-
- if (!CheckCmsKeyChecksum(result, CKStoBeVerified)) {
- throw new InvalidCipherTextException(
- "Checksum inside ciphertext is corrupted");
- }
-
- return result;
- }
-
- private byte[] CalculateCmsKeyChecksum(
- byte[] key)
- {
- sha1.BlockUpdate(key, 0, key.Length);
- sha1.DoFinal(digest, 0);
- byte[] result = new byte[8];
- Array.Copy(digest, 0, result, 0, 8);
- return result;
- }
-
- private bool CheckCmsKeyChecksum(
- byte[] key,
- byte[] checksum)
- {
- return Arrays.ConstantTimeAreEqual(CalculateCmsKeyChecksum(key), checksum);
- }
- private static byte[] reverse(byte[] bs)
- {
- byte[] result = new byte[bs.Length];
- for (int i = 0; i < bs.Length; i++)
- {
- result[i] = bs[bs.Length - (i + 1)];
- }
- return result;
- }
- }
- }
- #endif
|