"Fossies" - the Fresh Open Source Software Archive 
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.cpp" 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 #include "Common/Pkcs5.h"
14 #include "Pkcs5Kdf.h"
15 #include "VolumePassword.h"
16
17 namespace VeraCrypt
18 {
19 Pkcs5Kdf::Pkcs5Kdf (bool truecryptMode) : m_truecryptMode(truecryptMode)
20 {
21 }
22
23 Pkcs5Kdf::~Pkcs5Kdf ()
24 {
25 }
26
27 void Pkcs5Kdf::DeriveKey (const BufferPtr &key, const VolumePassword &password, int pim, const ConstBufferPtr &salt) const
28 {
29 DeriveKey (key, password, salt, GetIterationCount(pim));
30 }
31
32 shared_ptr <Pkcs5Kdf> Pkcs5Kdf::GetAlgorithm (const wstring &name, bool truecryptMode)
33 {
34 foreach (shared_ptr <Pkcs5Kdf> kdf, GetAvailableAlgorithms(truecryptMode))
35 {
36 if (kdf->GetName() == name)
37 return kdf;
38 }
39 throw ParameterIncorrect (SRC_POS);
40 }
41
42 shared_ptr <Pkcs5Kdf> Pkcs5Kdf::GetAlgorithm (const Hash &hash, bool truecryptMode)
43 {
44 foreach (shared_ptr <Pkcs5Kdf> kdf, GetAvailableAlgorithms(truecryptMode))
45 {
46 if (typeid (*kdf->GetHash()) == typeid (hash))
47 return kdf;
48 }
49
50 throw ParameterIncorrect (SRC_POS);
51 }
52
53 Pkcs5KdfList Pkcs5Kdf::GetAvailableAlgorithms (bool truecryptMode)
54 {
55 Pkcs5KdfList l;
56
57 if (truecryptMode)
58 {
59 l.push_back (shared_ptr <Pkcs5Kdf> (new Pkcs5HmacRipemd160 (true)));
60 l.push_back (shared_ptr <Pkcs5Kdf> (new Pkcs5HmacSha512 (true)));
61 l.push_back (shared_ptr <Pkcs5Kdf> (new Pkcs5HmacWhirlpool (true)));
62 }
63 else
64 {
65 l.push_back (shared_ptr <Pkcs5Kdf> (new Pkcs5HmacSha512 (false)));
66 l.push_back (shared_ptr <Pkcs5Kdf> (new Pkcs5HmacWhirlpool (false)));
67 l.push_back (shared_ptr <Pkcs5Kdf> (new Pkcs5HmacSha256 ()));
68 l.push_back (shared_ptr <Pkcs5Kdf> (new Pkcs5HmacRipemd160 (false)));
69 l.push_back (shared_ptr <Pkcs5Kdf> (new Pkcs5HmacStreebog ()));
70 }
71
72 return l;
73 }
74
75 void Pkcs5Kdf::ValidateParameters (const BufferPtr &key, const VolumePassword &password, const ConstBufferPtr &salt, int iterationCount) const
76 {
77 if (key.Size() < 1 || password.Size() < 1 || salt.Size() < 1 || iterationCount < 1)
78 throw ParameterIncorrect (SRC_POS);
79 }
80
81 void Pkcs5HmacRipemd160::DeriveKey (const BufferPtr &key, const VolumePassword &password, const ConstBufferPtr &salt, int iterationCount) const
82 {
83 ValidateParameters (key, password, salt, iterationCount);
84 derive_key_ripemd160 ((char *) password.DataPtr(), (int) password.Size(), (char *) salt.Get(), (int) salt.Size(), iterationCount, (char *) key.Get(), (int) key.Size());
85 }
86
87 void Pkcs5HmacRipemd160_1000::DeriveKey (const BufferPtr &key, const VolumePassword &password, const ConstBufferPtr &salt, int iterationCount) const
88 {
89 ValidateParameters (key, password, salt, iterationCount);
90 derive_key_ripemd160 ((char *) password.DataPtr(), (int) password.Size(), (char *) salt.Get(), (int) salt.Size(), iterationCount, (char *) key.Get(), (int) key.Size());
91 }
92
93 void Pkcs5HmacSha256_Boot::DeriveKey (const BufferPtr &key, const VolumePassword &password, const ConstBufferPtr &salt, int iterationCount) const
94 {
95 ValidateParameters (key, password, salt, iterationCount);
96 derive_key_sha256 ((char *) password.DataPtr(), (int) password.Size(), (char *) salt.Get(), (int) salt.Size(), iterationCount, (char *) key.Get(), (int) key.Size());
97 }
98
99 void Pkcs5HmacSha256::DeriveKey (const BufferPtr &key, const VolumePassword &password, const ConstBufferPtr &salt, int iterationCount) const
100 {
101 ValidateParameters (key, password, salt, iterationCount);
102 derive_key_sha256 ((char *) password.DataPtr(), (int) password.Size(), (char *) salt.Get(), (int) salt.Size(), iterationCount, (char *) key.Get(), (int) key.Size());
103 }
104
105 void Pkcs5HmacSha512::DeriveKey (const BufferPtr &key, const VolumePassword &password, const ConstBufferPtr &salt, int iterationCount) const
106 {
107 ValidateParameters (key, password, salt, iterationCount);
108 derive_key_sha512 ((char *) password.DataPtr(), (int) password.Size(), (char *) salt.Get(), (int) salt.Size(), iterationCount, (char *) key.Get(), (int) key.Size());
109 }
110
111 void Pkcs5HmacWhirlpool::DeriveKey (const BufferPtr &key, const VolumePassword &password, const ConstBufferPtr &salt, int iterationCount) const
112 {
113 ValidateParameters (key, password, salt, iterationCount);
114 derive_key_whirlpool ((char *) password.DataPtr(), (int) password.Size(), (char *) salt.Get(), (int) salt.Size(), iterationCount, (char *) key.Get(), (int) key.Size());
115 }
116
117 void Pkcs5HmacStreebog::DeriveKey (const BufferPtr &key, const VolumePassword &password, const ConstBufferPtr &salt, int iterationCount) const
118 {
119 ValidateParameters (key, password, salt, iterationCount);
120 derive_key_streebog ((char *) password.DataPtr(), (int) password.Size(), (char *) salt.Get(), (int) salt.Size(), iterationCount, (char *) key.Get(), (int) key.Size());
121 }
122
123 void Pkcs5HmacStreebog_Boot::DeriveKey (const BufferPtr &key, const VolumePassword &password, const ConstBufferPtr &salt, int iterationCount) const
124 {
125 ValidateParameters (key, password, salt, iterationCount);
126 derive_key_streebog ((char *) password.DataPtr(), (int) password.Size(), (char *) salt.Get(), (int) salt.Size(), iterationCount, (char *) key.Get(), (int) key.Size());
127 }
128 }