123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374 |
- #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 RC2WrapEngine
- : IWrapper
- {
-
- private CbcBlockCipher engine;
-
- private ICipherParameters parameters;
-
- private ParametersWithIV paramPlusIV;
-
- private byte[] iv;
-
- private bool forWrapping;
- private SecureRandom sr;
-
- private static readonly byte[] IV2 =
- {
- (byte) 0x4a, (byte) 0xdd, (byte) 0xa2,
- (byte) 0x2c, (byte) 0x79, (byte) 0xe8,
- (byte) 0x21, (byte) 0x05
- };
-
-
-
- IDigest sha1 = new Sha1Digest();
- byte[] digest = new byte[20];
-
- public virtual void Init(
- bool forWrapping,
- ICipherParameters parameters)
- {
- this.forWrapping = forWrapping;
- this.engine = new CbcBlockCipher(new RC2Engine());
- if (parameters is ParametersWithRandom)
- {
- ParametersWithRandom pWithR = (ParametersWithRandom)parameters;
- sr = pWithR.Random;
- parameters = pWithR.Parameters;
- }
- else
- {
- sr = new SecureRandom();
- }
- 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.parameters = this.paramPlusIV.Parameters;
- if (this.iv.Length != 8)
- throw new ArgumentException("IV is not 8 octets");
- }
- else
- {
- this.parameters = parameters;
- if (this.forWrapping)
- {
-
-
- this.iv = new byte[8];
- sr.NextBytes(iv);
- this.paramPlusIV = new ParametersWithIV(this.parameters, this.iv);
- }
- }
- }
-
- public virtual string AlgorithmName
- {
- get { return "RC2"; }
- }
-
- public virtual byte[] Wrap(
- byte[] input,
- int inOff,
- int length)
- {
- if (!forWrapping)
- {
- throw new InvalidOperationException("Not initialized for wrapping");
- }
- int len = length + 1;
- if ((len % 8) != 0)
- {
- len += 8 - (len % 8);
- }
- byte [] keyToBeWrapped = new byte[len];
- keyToBeWrapped[0] = (byte)length;
- Array.Copy(input, inOff, keyToBeWrapped, 1, length);
- byte[] pad = new byte[keyToBeWrapped.Length - length - 1];
- if (pad.Length > 0)
- {
- sr.NextBytes(pad);
- Array.Copy(pad, 0, keyToBeWrapped, length + 1, pad.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);
-
-
- byte [] TEMP1 = new byte[WKCKS.Length];
- Array.Copy(WKCKS, 0, TEMP1, 0, WKCKS.Length);
- int noOfBlocks = WKCKS.Length / engine.GetBlockSize();
- int extraBytes = WKCKS.Length % engine.GetBlockSize();
- if (extraBytes != 0)
- {
- throw new InvalidOperationException("Not multiple of block length");
- }
- engine.Init(true, paramPlusIV);
- for (int i = 0; i < noOfBlocks; i++)
- {
- int currentBytePos = i * engine.GetBlockSize();
- engine.ProcessBlock(TEMP1, 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 = new byte[TEMP2.Length];
- for (int i = 0; i < TEMP2.Length; i++)
- {
- TEMP3[i] = TEMP2[TEMP2.Length - (i + 1)];
- }
-
-
-
- ParametersWithIV param2 = new ParametersWithIV(this.parameters, IV2);
- this.engine.Init(true, param2);
- for (int i = 0; i < noOfBlocks + 1; i++)
- {
- int currentBytePos = i * engine.GetBlockSize();
- 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");
- }
- if (length % engine.GetBlockSize() != 0)
- {
- throw new InvalidCipherTextException("Ciphertext not multiple of "
- + engine.GetBlockSize());
- }
-
-
-
- ParametersWithIV param2 = new ParametersWithIV(this.parameters, IV2);
- this.engine.Init(false, param2);
- byte [] TEMP3 = new byte[length];
- Array.Copy(input, inOff, TEMP3, 0, length);
- for (int i = 0; i < (TEMP3.Length / engine.GetBlockSize()); i++)
- {
- int currentBytePos = i * engine.GetBlockSize();
- engine.ProcessBlock(TEMP3, currentBytePos, TEMP3, currentBytePos);
- }
-
- byte[] TEMP2 = new byte[TEMP3.Length];
- for (int i = 0; i < TEMP3.Length; i++)
- {
- TEMP2[i] = TEMP3[TEMP3.Length - (i + 1)];
- }
-
- 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.parameters, this.iv);
- this.engine.Init(false, this.paramPlusIV);
- byte[] LCEKPADICV = new byte[TEMP1.Length];
- Array.Copy(TEMP1, 0, LCEKPADICV, 0, TEMP1.Length);
- for (int i = 0; i < (LCEKPADICV.Length / engine.GetBlockSize()); i++)
- {
- int currentBytePos = i * engine.GetBlockSize();
- engine.ProcessBlock(LCEKPADICV, currentBytePos, LCEKPADICV, currentBytePos);
- }
-
-
- byte[] result = new byte[LCEKPADICV.Length - 8];
- byte[] CKStoBeVerified = new byte[8];
- Array.Copy(LCEKPADICV, 0, result, 0, LCEKPADICV.Length - 8);
- Array.Copy(LCEKPADICV, LCEKPADICV.Length - 8, CKStoBeVerified, 0, 8);
-
-
- if (!CheckCmsKeyChecksum(result, CKStoBeVerified))
- {
- throw new InvalidCipherTextException(
- "Checksum inside ciphertext is corrupted");
- }
- if ((result.Length - ((result[0] & 0xff) + 1)) > 7)
- {
- throw new InvalidCipherTextException(
- "too many pad bytes (" + (result.Length - ((result[0] & 0xff) + 1)) + ")");
- }
-
- byte[] CEK = new byte[result[0]];
- Array.Copy(result, 1, CEK, 0, CEK.Length);
- return CEK;
- }
-
- 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);
- }
- }
- }
- #endif
|