VeraCrypt

Documentation >> Technical Details >> Random Number Generator

Random Number Generator

The VeraCrypt random number generator (RNG) is used to generate the master encryption key, the secondary key (XTS mode), salt, and keyfiles. It creates a pool of random values in RAM (memory). The pool, which is 320 bytes long, is filled with data from the following sources:

Before a value obtained from any of the above-mentioned sources is written to the pool, it is divided into individual bytes (e.g., a 32-bit number is divided into four bytes). These bytes are then individually written to the pool with the modulo 28 addition operation (not by replacing the old values in the pool) at the position of the pool cursor. After a byte is written, the pool cursor position is advanced by one byte. When the cursor reaches the end of the pool, its position is set to the beginning of the pool. After every 16th byte written to the pool, the pool mixing function is automatically applied to the entire pool (see below).

Pool Mixing Function

The purpose of this function is to perform diffusion [2]. Diffusion spreads the influence of individual “raw” input bits over as much of the pool state as possible, which also hides statistical relationships. After every 16th byte written to the pool, this function is applied to the entire pool.

Description of the pool mixing function:

  1. Let R be the randomness pool.
  2. Let H be the hash function selected by the user (SHA-512, RIPEMD-160, or Whirlpool).
  3. l = byte size of the output of the hash function H (i.e., if H is RIPEMD-160, then l = 20; if H is SHA-512, l = 64)
  4. z = byte size of the randomness pool R (320 bytes)
  5. q = z / l – 1 (e.g., if H is Whirlpool, then q = 4)
  6. Ris divided intol-byte blocksB0...Bq.

    For 0 ≤ i ≤ q (i.e., for each block B) the following steps are performed:

    1. M = H (B0 || B1 || ... || Bq) [i.e., the randomness pool is hashed using the hash function H, which produces a hash M]
    2. Bi = Bi ^ M
  7. R = B0 || B1 || ... || Bq

For example, if q = 1, the randomness pool would be mixed as follows:

  1. (B0 || B1) = R
  2. B0 = B0 ^ H(B0 || B1)
  3. B1 = B1 ^ H(B0 || B1)
  4. R = B0 || B1

Generated Values

The content of the RNG pool is never directly exported (even when VeraCrypt instructs the RNG to generate and export a value). Thus, even if the attacker obtains a value generated by the RNG, it is infeasible for him to determine or predict (using the obtained value) any other values generated by the RNG during the session (it is infeasible to determine the content of the pool from a value generated by the RNG).

The RNG ensures this by performing the following steps whenever VeraCrypt instructs it to generate and export a value:

  1. Data obtained from the sources listed above is added to the pool as described above.
  2. The requested number of bytes is copied from the pool to the output buffer (the copying starts from the position of the pool cursor; when the end of the pool is reached, the copying continues from the beginning of the pool; if the requested number of bytes is greater than the size of the pool, no value is generated and an error is returned).
  3. The state of each bit in the pool is inverted (i.e., 0 is changed to 1, and 1 is changed to 0).
  4. Data obtained from some of the sources listed above is added to the pool as described above.
  5. The content of the pool is transformed using the pool mixing function. Note: The function uses a cryptographically secure one-way hash function selected by the user (for more information, see the section Pool Mixing Function above).
  6. The transformed content of the pool is XORed into the output buffer as follows:
    1. The output buffer write cursor is set to 0 (the first byte of the buffer).
    2. The byte at the position of the pool cursor is read from the pool and XORed into the byte in the output buffer at the position of the output buffer write cursor.
    3. The pool cursor position is advanced by one byte. If the end of the pool is reached, the cursor position is set to 0 (the first byte of the pool).
    4. The position of the output buffer write cursor is advanced by one byte.
    5. Steps b–d are repeated for each remaining byte of the output buffer (whose length is equal to the requested number of bytes).
    6. The content of the output buffer, which is the final value generated by the RNG, is exported.

Design Origins

The design and implementation of the random number generator are based on the following works:

 

Next Section >>

} /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
/*
 Derived from source code of TrueCrypt 7.1a, which is
 Copyright (c) 2008-2012 TrueCrypt Developers Association and which is governed
 by the TrueCrypt License 3.0.

 Modifications and additions to the original source code (contained in this file)
 and all other portions of this file are Copyright (c) 2013-2017 IDRIX
 and are governed by the Apache License 2.0 the full text of which is
 contained in the file License.txt included in VeraCrypt binary and source
 code distribution packages.
*/

#ifndef TC_HEADER_Main_Forms_NewSecurityTokenKeyfileDialog
#define TC_HEADER_Main_Forms_NewSecurityTokenKeyfileDialog

#include "Forms.h"
#include "Common/SecurityToken.h"

namespace VeraCrypt
{
	class NewSecurityTokenKeyfileDialog : public NewSecurityTokenKeyfileDialogBase
	{
	public:
		NewSecurityTokenKeyfileDialog (wxWindow* parent, const wstring &keyfileName);

		wstring GetKeyfileName () const { return wstring (KeyfileNameTextCtrl->GetValue()); }
		CK_SLOT_ID GetSelectedSlotId () const { return reinterpret_cast <CK_SLOT_ID> (SecurityTokenChoice->GetClientData (SecurityTokenChoice->GetSelection())); }

	protected:
		void OnKeyfileNameChanged (wxCommandEvent& event);
	};
}

#endif // TC_HEADER_Main_Forms_NewSecurityTokenKeyfileDialog