Exemple #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="randstate_t"/> class.
        /// </summary>
        /// <param name="algorithm">The algorithm.</param>
        /// <param name="parameters">The algorithm parameters.</param>
        private randstate_t(RngAlgorithm algorithm, object[] parameters)
        {
            switch (algorithm)
            {
            default:
            case RngAlgorithm.Default:
                mp_randinit_default(ref Value);
                break;

            case RngAlgorithm.MersenneTwister:
                mp_randinit_mt(ref Value);
                break;

            case RngAlgorithm.LinearCongruential:
                if (parameters.Length == 3 && parameters[0] is mpz_t a && parameters[1] is ulong c && parameters[2] is ulong m2exp)
                {
                    mp_randinit_lc_2exp(ref Value, ref a.Value, (mpir_ui)c, (mp_bitcnt_t)m2exp);
                }
                else if (parameters.Length == 1 && parameters[0] is ulong size)
                {
                    mp_randinit_lc_2exp_size(ref Value, (mp_bitcnt_t)size);
                }
                else
                {
                    throw new ArgumentException();
                }
                break;
            }
Exemple #2
0
 /// <summary>
 /// Initializes a new KdbxWriter with the given options.
 /// </summary>
 /// <param name="cipher">The algorithm to use for encrypting the database.</param>
 /// <param name="rngAlgorithm">The random number generator used for String protection.</param>
 /// <param name="compression">The document compression algorithm.</param>
 /// <param name="kdfParams">Recipe for transforming the raw key. This will be reseeded.</param>
 private KdbxWriter(
     EncryptionAlgorithm cipher,
     RngAlgorithm rngAlgorithm,
     CompressionAlgorithm compression,
     KdfParameters kdfParams
     )
     : base()
 {
     SeedHeaderData(cipher, rngAlgorithm, compression, kdfParams);
 }
Exemple #3
0
 /// <summary>
 /// Initializes a new KdbxWriter with the given options.
 /// </summary>
 /// <param name="tokenList">An enumeration of security tokens used for encryption.</param>
 /// <param name="cipher">The algorithm to use for encrypting the database.</param>
 /// <param name="rngAlgorithm">The random number generator used for String protection.</param>
 /// <param name="compression">The document compression algorithm.</param>
 /// <param name="kdfParams">Recipe for transforming the raw key.</param>
 public KdbxWriter(
     IEnumerable <ISecurityToken> securityTokens,
     EncryptionAlgorithm cipher,
     RngAlgorithm rngAlgorithm,
     CompressionAlgorithm compression,
     KdfParameters kdfParams
     )
     : this(cipher, rngAlgorithm, compression, kdfParams)
 {
     this.securityTokens = securityTokens ?? throw new ArgumentNullException(nameof(securityTokens));
 }
Exemple #4
0
        /// <summary>
        /// Ensures the writer's serialization settings are correct for the given user preferences.
        /// </summary>
        /// <param name="cipher">The algorithm to use for encrypting the database.</param>
        /// <param name="rngAlgorithm">The random number generator used for String protection.</param>
        /// <param name="compression">The document compression algorithm.</param>
        /// <param name="kdfParams">Recipe for transforming the raw key. This will be reseeded.</param>
        private void SeedHeaderData(
            EncryptionAlgorithm cipher,
            RngAlgorithm rngAlgorithm,
            CompressionAlgorithm compression,
            KdfParameters kdfParams
            )
        {
            if (kdfParams == null)
            {
                throw new ArgumentNullException(nameof(kdfParams));
            }

            KdbxVersion version = KdbxVersion.Three;

            if (cipher == EncryptionAlgorithm.ChaCha20 || rngAlgorithm == RngAlgorithm.ChaCha20 ||
                !kdfParams.Uuid.Equals(AesParameters.AesUuid))
            {
                DebugHelper.Trace("Using KDBX4 for serialization due to header parameters");
                version = KdbxVersion.Four;
            }

            this.parameters = new KdbxSerializationParameters(version)
            {
                Compression = compression
            };

            // "Stream start bytes" are random data encrypted at the beginning
            // of the KDBX data block. They have been superceded by HMAC authentication.
            IBuffer streamStartBytes;

            if (this.parameters.UseHmacBlocks)
            {
                streamStartBytes = new byte[0].AsBuffer();
            }
            else
            {
                streamStartBytes = CryptographicBuffer.GenerateRandom(32);
            }

            HeaderData = new KdbxHeaderData(KdbxHeaderData.Mode.Write)
            {
                Cipher               = cipher, // This will automatically set EncryptionIV
                Compression          = compression,
                MasterSeed           = CryptographicBuffer.GenerateRandom(32),
                KdfParameters        = kdfParams.Reseed(),
                StreamStartBytes     = streamStartBytes,
                InnerRandomStreamKey = CryptographicBuffer.GenerateRandom(32).ToArray(),
                InnerRandomStream    = rngAlgorithm
            };
        }
Exemple #5
0
        /// <summary>
        /// Initializes a new KdbxWriter with the given options.
        /// </summary>
        /// <param name="rawKey">The raw 32-byte key for encryption.</param>
        /// <param name="cipher">The algorithm to use for encrypting the database.</param>
        /// <param name="rngAlgorithm">The random number generator used for String protection.</param>
        /// <param name="compression">The document compression algorithm.</param>
        /// <param name="kdfParams">Recipe for transforming <paramref name="rawKey"/>.</param>
        public KdbxWriter(
            IBuffer rawKey,
            EncryptionAlgorithm cipher,
            RngAlgorithm rngAlgorithm,
            CompressionAlgorithm compression,
            KdfParameters kdfParams
            )
            : this(cipher, rngAlgorithm, compression, kdfParams)
        {
            if (rawKey == null)
            {
                throw new ArgumentNullException(nameof(rawKey));
            }

            if (rawKey.Length != 32)
            {
                throw new ArgumentException("Raw key must be 32 bytes", nameof(rawKey));
            }

            this.rawKey = rawKey;
        }
Exemple #6
0
 /// <summary>
 /// Creates a random state.
 /// </summary>
 /// <param name="algorithm">The algorithm.</param>
 /// <param name="parameters">The algorithm parameters.</param>
 public static randstate_t Create(RngAlgorithm algorithm = RngAlgorithm.Default, params object[] parameters)
 {
     return(new randstate_t(algorithm, parameters));
 }