Example #1
0
        /// <summary>
        ///     Creates a Poly1305 primitive using a symmetric block cipher primitive (cipher must have a block size of 128 bits).
        ///     If salt is used, it is applied as: salt||message, where || is concatenation.
        /// </summary>
        /// <param name="cipherEnum">Cipher primitive to use as the basis for the Poly1305 construction.</param>
        /// <param name="key">Cryptographic key to use in the MAC operation.</param>
        /// <param name="nonce">Initialisation vector/nonce. Required.</param>
        /// <param name="salt">Cryptographic salt to use in the MAC operation, if any.</param>
        /// <returns>Pre-initialised Poly1305 MAC primitive as a <see cref="IMac" />.</returns>
        public static IMac CreatePoly1305Primitive(BlockCipher cipherEnum, byte[] key, byte[] nonce, byte[] salt = null)
        {
            if (Athena.Cryptography.BlockCiphers[cipherEnum].DefaultBlockSizeBits != 128)
            {
                throw new NotSupportedException();
            }

            var macObj = new Poly1305Mac(CipherFactory.CreateBlockCipher(cipherEnum));

            macObj.Init(key, nonce);
            if (salt.IsNullOrZeroLength() == false)
            {
                macObj.BlockUpdate(salt, 0, salt.Length);
            }

            return(macObj);
        }
Example #2
0
        /// <summary>
        ///     Creates a CMAC primitive using a symmetric block cipher primitive configured with default block size.
        ///     Default block sizes (and so, output sizes) can be found by querying <see cref="Athena" />.
        /// </summary>
        /// <param name="cipherEnum">
        ///     Cipher primitive to use as the basis for the CMAC construction. Block size must be 64 or 128
        ///     bits.
        /// </param>
        /// <param name="key">Cryptographic key to use in the MAC operation.</param>
        /// <param name="salt">Cryptographic salt to use in the MAC operation, if any.</param>
        /// <returns>Pre-initialised CMAC primitive as a <see cref="IMac" />.</returns>
        public static IMac CreateCmacPrimitive(BlockCipher cipherEnum, byte[] key, byte[] salt = null)
        {
            int?defaultBlockSize = Athena.Cryptography.BlockCiphers[cipherEnum].DefaultBlockSizeBits;

            if (defaultBlockSize != 64 && defaultBlockSize != 128)
            {
                throw new NotSupportedException("CMAC/OMAC1 only supports ciphers with 64 / 128 bit block sizes.");
            }
            var macObj = new CMac(CipherFactory.CreateBlockCipher(cipherEnum, null));

            macObj.Init(key);
            if (salt.IsNullOrZeroLength() == false)
            {
                macObj.BlockUpdate(salt, 0, salt.Length);
            }

            return(macObj);
        }