"Fossies" - the Fresh Open Source Software Archive

Member "src/Volume/Pkcs5Kdf.h" (10 Oct 2018, 8166 Bytes) of package /windows/misc/VeraCrypt_1.23-Hotfix-2_Source.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "Pkcs5Kdf.h" see the Fossies "Dox" file reference documentation.

    1 /*
    2  Derived from source code of TrueCrypt 7.1a, which is
    3  Copyright (c) 2008-2012 TrueCrypt Developers Association and which is governed
    4  by the TrueCrypt License 3.0.
    5 
    6  Modifications and additions to the original source code (contained in this file)
    7  and all other portions of this file are Copyright (c) 2013-2017 IDRIX
    8  and are governed by the Apache License 2.0 the full text of which is
    9  contained in the file License.txt included in VeraCrypt binary and source
   10  code distribution packages.
   11 */
   12 
   13 #ifndef TC_HEADER_Encryption_Pkcs5
   14 #define TC_HEADER_Encryption_Pkcs5
   15 
   16 #include "Platform/Platform.h"
   17 #include "Hash.h"
   18 #include "VolumePassword.h"
   19 
   20 namespace VeraCrypt
   21 {
   22     class Pkcs5Kdf;
   23     typedef list < shared_ptr <Pkcs5Kdf> > Pkcs5KdfList;
   24 
   25     class Pkcs5Kdf
   26     {
   27     public:
   28         virtual ~Pkcs5Kdf ();
   29 
   30         virtual void DeriveKey (const BufferPtr &key, const VolumePassword &password, int pim, const ConstBufferPtr &salt) const;
   31         virtual void DeriveKey (const BufferPtr &key, const VolumePassword &password, const ConstBufferPtr &salt, int iterationCount) const = 0;
   32         static shared_ptr <Pkcs5Kdf> GetAlgorithm (const wstring &name, bool truecryptMode);
   33         static shared_ptr <Pkcs5Kdf> GetAlgorithm (const Hash &hash, bool truecryptMode);
   34         static Pkcs5KdfList GetAvailableAlgorithms (bool truecryptMode);
   35         virtual shared_ptr <Hash> GetHash () const = 0;
   36         virtual int GetIterationCount (int pim) const = 0;
   37         virtual wstring GetName () const = 0;
   38         virtual Pkcs5Kdf* Clone () const = 0;
   39         virtual bool IsDeprecated () const { return GetHash()->IsDeprecated(); }
   40         bool GetTrueCryptMode () const { return m_truecryptMode;}
   41         void SetTrueCryptMode (bool truecryptMode) { m_truecryptMode = truecryptMode;}
   42 
   43     protected:
   44         bool m_truecryptMode;
   45         Pkcs5Kdf (bool truecryptMode);
   46 
   47         void ValidateParameters (const BufferPtr &key, const VolumePassword &password, const ConstBufferPtr &salt, int iterationCount) const;
   48 
   49     private:
   50         Pkcs5Kdf (const Pkcs5Kdf &);
   51         Pkcs5Kdf &operator= (const Pkcs5Kdf &);
   52     };
   53 
   54     class Pkcs5HmacRipemd160 : public Pkcs5Kdf
   55     {
   56     public:
   57         Pkcs5HmacRipemd160 (bool truecryptMode) : Pkcs5Kdf (truecryptMode) { }
   58         virtual ~Pkcs5HmacRipemd160 () { }
   59 
   60         virtual void DeriveKey (const BufferPtr &key, const VolumePassword &password, const ConstBufferPtr &salt, int iterationCount) const;
   61         virtual shared_ptr <Hash> GetHash () const { return shared_ptr <Hash> (new Ripemd160); }
   62         virtual int GetIterationCount (int pim) const { return m_truecryptMode? 2000 : (pim <= 0 ? 655331 : (15000 + (pim * 1000))) ; }
   63         virtual wstring GetName () const { return L"HMAC-RIPEMD-160"; }
   64         virtual Pkcs5Kdf* Clone () const { return new Pkcs5HmacRipemd160(m_truecryptMode); }
   65 
   66     private:
   67         Pkcs5HmacRipemd160 (const Pkcs5HmacRipemd160 &);
   68         Pkcs5HmacRipemd160 &operator= (const Pkcs5HmacRipemd160 &);
   69     };
   70 
   71     class Pkcs5HmacRipemd160_1000 : public Pkcs5Kdf
   72     {
   73     public:
   74         Pkcs5HmacRipemd160_1000 (bool truecryptMode) : Pkcs5Kdf(truecryptMode) { }
   75         virtual ~Pkcs5HmacRipemd160_1000 () { }
   76 
   77         virtual void DeriveKey (const BufferPtr &key, const VolumePassword &password, const ConstBufferPtr &salt, int iterationCount) const;
   78         virtual shared_ptr <Hash> GetHash () const { return shared_ptr <Hash> (new Ripemd160); }
   79         virtual int GetIterationCount (int pim) const { return m_truecryptMode? 1000 : (pim <= 0 ? 327661 : (pim * 2048)); }
   80         virtual wstring GetName () const { return L"HMAC-RIPEMD-160"; }
   81         virtual Pkcs5Kdf* Clone () const { return new Pkcs5HmacRipemd160_1000(m_truecryptMode); }
   82 
   83     private:
   84         Pkcs5HmacRipemd160_1000 (const Pkcs5HmacRipemd160_1000 &);
   85         Pkcs5HmacRipemd160_1000 &operator= (const Pkcs5HmacRipemd160_1000 &);
   86     };
   87 
   88     class Pkcs5HmacSha256_Boot : public Pkcs5Kdf
   89     {
   90     public:
   91         Pkcs5HmacSha256_Boot () : Pkcs5Kdf(false) { }
   92         virtual ~Pkcs5HmacSha256_Boot () { }
   93 
   94         virtual void DeriveKey (const BufferPtr &key, const VolumePassword &password, const ConstBufferPtr &salt, int iterationCount) const;
   95         virtual shared_ptr <Hash> GetHash () const { return shared_ptr <Hash> (new Sha256); }
   96         virtual int GetIterationCount (int pim) const { return pim <= 0 ? 200000 : (pim * 2048); }
   97         virtual wstring GetName () const { return L"HMAC-SHA-256"; }
   98         virtual Pkcs5Kdf* Clone () const { return new Pkcs5HmacSha256_Boot(); }
   99 
  100     private:
  101         Pkcs5HmacSha256_Boot (const Pkcs5HmacSha256_Boot &);
  102         Pkcs5HmacSha256_Boot &operator= (const Pkcs5HmacSha256_Boot &);
  103     };
  104 
  105     class Pkcs5HmacSha256 : public Pkcs5Kdf
  106     {
  107     public:
  108         Pkcs5HmacSha256 () : Pkcs5Kdf(false) { }
  109         virtual ~Pkcs5HmacSha256 () { }
  110 
  111         virtual void DeriveKey (const BufferPtr &key, const VolumePassword &password, const ConstBufferPtr &salt, int iterationCount) const;
  112         virtual shared_ptr <Hash> GetHash () const { return shared_ptr <Hash> (new Sha256); }
  113         virtual int GetIterationCount (int pim) const { return pim <= 0 ? 500000 : (15000 + (pim * 1000)); }
  114         virtual wstring GetName () const { return L"HMAC-SHA-256"; }
  115         virtual Pkcs5Kdf* Clone () const { return new Pkcs5HmacSha256(); }
  116 
  117     private:
  118         Pkcs5HmacSha256 (const Pkcs5HmacSha256 &);
  119         Pkcs5HmacSha256 &operator= (const Pkcs5HmacSha256 &);
  120     };
  121 
  122     class Pkcs5HmacSha512 : public Pkcs5Kdf
  123     {
  124     public:
  125         Pkcs5HmacSha512 (bool truecryptMode) : Pkcs5Kdf(truecryptMode) { }
  126         virtual ~Pkcs5HmacSha512 () { }
  127 
  128         virtual void DeriveKey (const BufferPtr &key, const VolumePassword &password, const ConstBufferPtr &salt, int iterationCount) const;
  129         virtual shared_ptr <Hash> GetHash () const { return shared_ptr <Hash> (new Sha512); }
  130         virtual int GetIterationCount (int pim) const { return m_truecryptMode? 1000 : (pim <= 0 ? 500000 : (15000 + (pim * 1000))); }
  131         virtual wstring GetName () const { return L"HMAC-SHA-512"; }
  132         virtual Pkcs5Kdf* Clone () const { return new Pkcs5HmacSha512(m_truecryptMode); }
  133 
  134     private:
  135         Pkcs5HmacSha512 (const Pkcs5HmacSha512 &);
  136         Pkcs5HmacSha512 &operator= (const Pkcs5HmacSha512 &);
  137     };
  138 
  139     class Pkcs5HmacWhirlpool : public Pkcs5Kdf
  140     {
  141     public:
  142         Pkcs5HmacWhirlpool (bool truecryptMode) : Pkcs5Kdf(truecryptMode) { }
  143         virtual ~Pkcs5HmacWhirlpool () { }
  144 
  145         virtual void DeriveKey (const BufferPtr &key, const VolumePassword &password, const ConstBufferPtr &salt, int iterationCount) const;
  146         virtual shared_ptr <Hash> GetHash () const { return shared_ptr <Hash> (new Whirlpool); }
  147         virtual int GetIterationCount (int pim) const { return m_truecryptMode? 1000 : (pim <= 0 ? 500000 : (15000 + (pim * 1000))); }
  148         virtual wstring GetName () const { return L"HMAC-Whirlpool"; }
  149         virtual Pkcs5Kdf* Clone () const { return new Pkcs5HmacWhirlpool(m_truecryptMode); }
  150 
  151     private:
  152         Pkcs5HmacWhirlpool (const Pkcs5HmacWhirlpool &);
  153         Pkcs5HmacWhirlpool &operator= (const Pkcs5HmacWhirlpool &);
  154     };
  155     
  156     class Pkcs5HmacStreebog : public Pkcs5Kdf
  157     {
  158     public:
  159         Pkcs5HmacStreebog () : Pkcs5Kdf(false) { }
  160         virtual ~Pkcs5HmacStreebog () { }
  161 
  162         virtual void DeriveKey (const BufferPtr &key, const VolumePassword &password, const ConstBufferPtr &salt, int iterationCount) const;
  163         virtual shared_ptr <Hash> GetHash () const { return shared_ptr <Hash> (new Streebog); }
  164         virtual int GetIterationCount (int pim) const { return pim <= 0 ? 500000 : (15000 + (pim * 1000)); }
  165         virtual wstring GetName () const { return L"HMAC-Streebog"; }
  166         virtual Pkcs5Kdf* Clone () const { return new Pkcs5HmacStreebog(); }
  167 
  168     private:
  169         Pkcs5HmacStreebog (const Pkcs5HmacStreebog &);
  170         Pkcs5HmacStreebog &operator= (const Pkcs5HmacStreebog &);
  171     };
  172     
  173     class Pkcs5HmacStreebog_Boot : public Pkcs5Kdf
  174     {
  175     public:
  176         Pkcs5HmacStreebog_Boot () : Pkcs5Kdf(false) { }
  177         virtual ~Pkcs5HmacStreebog_Boot () { }
  178 
  179         virtual void DeriveKey (const BufferPtr &key, const VolumePassword &password, const ConstBufferPtr &salt, int iterationCount) const;
  180         virtual shared_ptr <Hash> GetHash () const { return shared_ptr <Hash> (new Streebog); }
  181         virtual int GetIterationCount (int pim) const { return pim <= 0 ? 200000 : pim * 2048; }
  182         virtual wstring GetName () const { return L"HMAC-Streebog"; }
  183         virtual Pkcs5Kdf* Clone () const { return new Pkcs5HmacStreebog_Boot(); }
  184 
  185     private:
  186         Pkcs5HmacStreebog_Boot (const Pkcs5HmacStreebog_Boot &);
  187         Pkcs5HmacStreebog_Boot &operator= (const Pkcs5HmacStreebog_Boot &);
  188     };
  189 }
  190 
  191 #endif // TC_HEADER_Encryption_Pkcs5