Esempio n. 1
0
        private Cipher GetCipher(ISecretKey key)
        {
            EncryptionHeader em = builder.GetHeader();
            ChainingMode     cm = em.ChainingMode;

            Debug.Assert(cm == ChainingMode.ecb);

            return(CryptoFunctions.GetCipher(key, em.CipherAlgorithm, cm, null, Cipher.DECRYPT_MODE));
        }
 public void Initialize(EncryptionInfo info,
                        CipherAlgorithm cipherAlgorithm, HashAlgorithm hashAlgorithm,
                        int keyBits, int blockSize, ChainingMode chainingMode)
 {
     this.info = info;
     header    = new BinaryRC4EncryptionHeader();
     verifier  = new BinaryRC4EncryptionVerifier();
     decryptor = new BinaryRC4Decryptor(this);
     encryptor = new BinaryRC4Encryptor(this);
 }
        /**
         * Initialize the builder from scratch
         */
        public void Initialize(EncryptionInfo info,
                               CipherAlgorithm cipherAlgorithm, HashAlgorithm hashAlgorithm,
                               int keyBits, int blockSize, ChainingMode chainingMode)
        {
            this.info = info;
            if (cipherAlgorithm == null)
            {
                cipherAlgorithm = CipherAlgorithm.rc4;
            }
            if (hashAlgorithm == null)
            {
                hashAlgorithm = HashAlgorithm.sha1;
            }
            if (keyBits == -1)
            {
                keyBits = 0x28;
            }
            Debug.Assert(cipherAlgorithm == CipherAlgorithm.rc4 && hashAlgorithm == HashAlgorithm.sha1);

            header    = new CryptoAPIEncryptionHeader(cipherAlgorithm, hashAlgorithm, keyBits, blockSize, chainingMode);
            verifier  = new CryptoAPIEncryptionVerifier(cipherAlgorithm, hashAlgorithm, keyBits, blockSize, chainingMode);
            decryptor = new CryptoAPIDecryptor(this);
            encryptor = new CryptoAPIEncryptor(this);
        }
Esempio n. 4
0
 protected internal StandardEncryptionHeader(CipherAlgorithm cipherAlgorithm, HashAlgorithm hashAlgorithm, int keyBits, int blockSize, ChainingMode chainingMode)
 {
     CipherAlgorithm = (cipherAlgorithm);
     HashAlgorithm   = (hashAlgorithm);
     KeySize         = (keyBits);
     BlockSize       = (blockSize);
     CipherProvider  = (cipherAlgorithm.provider);
     Flags           = (EncryptionInfo.flagCryptoAPI.SetBoolean(0, true)
                        | EncryptionInfo.flagAES.SetBoolean(0, cipherAlgorithm.provider == CipherProvider.aes));
     // see http://msdn.microsoft.com/en-us/library/windows/desktop/bb931357(v=vs.85).aspx for a full list
     // SetCspName("Microsoft Enhanced RSA and AES Cryptographic Provider");
 }
Esempio n. 5
0
 public AgileEncryptionHeader(CipherAlgorithm algorithm, HashAlgorithm hashAlgorithm, int keyBits, int blockSize, ChainingMode chainingMode)
 {
     CipherAlgorithm = (algorithm);
     HashAlgorithm   = (hashAlgorithm);
     KeySize         = (keyBits);
     BlockSize       = (blockSize);
     ChainingMode    = (chainingMode);
 }
Esempio n. 6
0
 protected internal StandardEncryptionVerifier(CipherAlgorithm cipherAlgorithm, HashAlgorithm hashAlgorithm, int keyBits, int blockSize, ChainingMode chainingMode)
 {
     CipherAlgorithm  = (cipherAlgorithm);
     HashAlgorithm    = (hashAlgorithm);
     ChainingMode     = (chainingMode);
     SpinCount        = (SPIN_COUNT);
     verifierHashSize = hashAlgorithm.hashSize;
 }
Esempio n. 7
0
 protected internal CryptoAPIEncryptionHeader(CipherAlgorithm cipherAlgorithm,
                                              HashAlgorithm hashAlgorithm, int keyBits, int blockSize,
                                              ChainingMode chainingMode)
     : base(cipherAlgorithm, hashAlgorithm, keyBits, blockSize, chainingMode)
 {
 }
Esempio n. 8
0
 public AgileEncryptionVerifier(CipherAlgorithm cipherAlgorithm, HashAlgorithm hashAlgorithm, int keyBits, int blockSize, ChainingMode chainingMode)
 {
     CipherAlgorithm = (cipherAlgorithm);
     HashAlgorithm   = (hashAlgorithm);
     ChainingMode    = (chainingMode);
     SpinCount       = (100000); // TODO: use parameter
 }
Esempio n. 9
0
        /**
         * Initialize the builder from scratch
         */
        public void Initialize(EncryptionInfo info, CipherAlgorithm cipherAlgorithm, HashAlgorithm hashAlgorithm, int keyBits, int blockSize, ChainingMode chainingMode)
        {
            this.info = info;

            if (cipherAlgorithm == null)
            {
                cipherAlgorithm = CipherAlgorithm.aes128;
            }
            if (cipherAlgorithm != CipherAlgorithm.aes128 &&
                cipherAlgorithm != CipherAlgorithm.aes192 &&
                cipherAlgorithm != CipherAlgorithm.aes256)
            {
                throw new EncryptedDocumentException("Standard encryption only supports AES128/192/256.");
            }

            if (hashAlgorithm == null)
            {
                hashAlgorithm = HashAlgorithm.sha1;
            }
            if (hashAlgorithm != HashAlgorithm.sha1)
            {
                throw new EncryptedDocumentException("Standard encryption only supports SHA-1.");
            }
            if (chainingMode == null)
            {
                chainingMode = ChainingMode.ecb;
            }
            if (chainingMode != ChainingMode.ecb)
            {
                throw new EncryptedDocumentException("Standard encryption only supports ECB chaining.");
            }
            if (keyBits == -1)
            {
                keyBits = cipherAlgorithm.defaultKeySize;
            }
            if (blockSize == -1)
            {
                blockSize = cipherAlgorithm.blockSize;
            }
            bool found = false;

            foreach (int ks in cipherAlgorithm.allowedKeySize)
            {
                found |= (ks == keyBits);
            }
            if (!found)
            {
                throw new EncryptedDocumentException("KeySize " + keyBits + " not allowed for Cipher " + cipherAlgorithm.ToString());
            }
            header    = new StandardEncryptionHeader(cipherAlgorithm, hashAlgorithm, keyBits, blockSize, chainingMode);
            verifier  = new StandardEncryptionVerifier(cipherAlgorithm, hashAlgorithm, keyBits, blockSize, chainingMode);
            decryptor = new StandardDecryptor(this);
            encryptor = new StandardEncryptor(this);
        }
Esempio n. 10
0
        public void Initialize(EncryptionInfo info, CipherAlgorithm cipherAlgorithm, HashAlgorithm hashAlgorithm, int keyBits, int blockSize, ChainingMode chainingMode)
        {
            this.info = info;

            if (cipherAlgorithm == null)
            {
                cipherAlgorithm = CipherAlgorithm.aes128;
            }
            if (cipherAlgorithm == CipherAlgorithm.rc4)
            {
                throw new EncryptedDocumentException("RC4 must not be used with agile encryption.");
            }
            if (hashAlgorithm == null)
            {
                hashAlgorithm = HashAlgorithm.sha1;
            }
            if (chainingMode == null)
            {
                chainingMode = ChainingMode.cbc;
            }
            if (!(chainingMode == ChainingMode.cbc || chainingMode == ChainingMode.cfb))
            {
                throw new EncryptedDocumentException("Agile encryption only supports CBC/CFB chaining.");
            }
            if (keyBits == -1)
            {
                keyBits = cipherAlgorithm.defaultKeySize;
            }
            if (blockSize == -1)
            {
                blockSize = cipherAlgorithm.blockSize;
            }
            bool found = false;

            foreach (int ks in cipherAlgorithm.allowedKeySize)
            {
                found |= (ks == keyBits);
            }
            if (!found)
            {
                throw new EncryptedDocumentException("KeySize " + keyBits + " not allowed for Cipher " + cipherAlgorithm.ToString());
            }
            header    = new AgileEncryptionHeader(cipherAlgorithm, hashAlgorithm, keyBits, blockSize, chainingMode);
            verifier  = new AgileEncryptionVerifier(cipherAlgorithm, hashAlgorithm, keyBits, blockSize, chainingMode);
            decryptor = new AgileDecryptor(this);
            encryptor = new AgileEncryptor(this);
        }