|
|
1.1 ! root 1: /* ! 2: Copyright (c) 2008 TrueCrypt Foundation. All rights reserved. ! 3: ! 4: Governed by the TrueCrypt License 2.4 the full text of which is contained ! 5: in the file License.txt included in TrueCrypt binary and source code ! 6: distribution packages. ! 7: */ ! 8: ! 9: #include "Common/Pkcs5.h" ! 10: #include "Pkcs5Kdf.h" ! 11: #include "VolumePassword.h" ! 12: ! 13: namespace TrueCrypt ! 14: { ! 15: Pkcs5Kdf::Pkcs5Kdf () ! 16: { ! 17: } ! 18: ! 19: Pkcs5Kdf::~Pkcs5Kdf () ! 20: { ! 21: } ! 22: ! 23: void Pkcs5Kdf::DeriveKey (const BufferPtr &key, const VolumePassword &password, const ConstBufferPtr &salt) const ! 24: { ! 25: DeriveKey (key, password, salt, GetIterationCount()); ! 26: } ! 27: ! 28: shared_ptr <Pkcs5Kdf> Pkcs5Kdf::GetAlgorithm (const wstring &name) ! 29: { ! 30: foreach (shared_ptr <Pkcs5Kdf> kdf, GetAvailableAlgorithms()) ! 31: { ! 32: if (kdf->GetName() == name) ! 33: return kdf; ! 34: } ! 35: throw ParameterIncorrect (SRC_POS); ! 36: } ! 37: ! 38: shared_ptr <Pkcs5Kdf> Pkcs5Kdf::GetAlgorithm (const Hash &hash) ! 39: { ! 40: foreach (shared_ptr <Pkcs5Kdf> kdf, GetAvailableAlgorithms()) ! 41: { ! 42: if (typeid (*kdf->GetHash()) == typeid (hash)) ! 43: return kdf; ! 44: } ! 45: ! 46: throw ParameterIncorrect (SRC_POS); ! 47: } ! 48: ! 49: Pkcs5KdfList Pkcs5Kdf::GetAvailableAlgorithms () ! 50: { ! 51: Pkcs5KdfList l; ! 52: ! 53: l.push_back (shared_ptr <Pkcs5Kdf> (new Pkcs5HmacRipemd160 ())); ! 54: l.push_back (shared_ptr <Pkcs5Kdf> (new Pkcs5HmacSha512 ())); ! 55: l.push_back (shared_ptr <Pkcs5Kdf> (new Pkcs5HmacWhirlpool ())); ! 56: l.push_back (shared_ptr <Pkcs5Kdf> (new Pkcs5HmacSha1 ())); ! 57: ! 58: return l; ! 59: } ! 60: ! 61: void Pkcs5Kdf::ValidateParameters (const BufferPtr &key, const VolumePassword &password, const ConstBufferPtr &salt, int iterationCount) const ! 62: { ! 63: if (key.Size() < 1 || password.Size() < 1 || salt.Size() < 1 || iterationCount < 1) ! 64: throw ParameterIncorrect (SRC_POS); ! 65: } ! 66: ! 67: void Pkcs5HmacRipemd160::DeriveKey (const BufferPtr &key, const VolumePassword &password, const ConstBufferPtr &salt, int iterationCount) const ! 68: { ! 69: ValidateParameters (key, password, salt, iterationCount); ! 70: derive_key_ripemd160 ((char *) password.DataPtr(), (int) password.Size(), (char *) salt.Get(), (int) salt.Size(), iterationCount, (char *) key.Get(), (int) key.Size()); ! 71: } ! 72: ! 73: void Pkcs5HmacSha1::DeriveKey (const BufferPtr &key, const VolumePassword &password, const ConstBufferPtr &salt, int iterationCount) const ! 74: { ! 75: ValidateParameters (key, password, salt, iterationCount); ! 76: derive_key_sha1 ((char *) password.DataPtr(), (int) password.Size(), (char *) salt.Get(), (int) salt.Size(), iterationCount, (char *) key.Get(), (int) key.Size()); ! 77: } ! 78: ! 79: void Pkcs5HmacSha512::DeriveKey (const BufferPtr &key, const VolumePassword &password, const ConstBufferPtr &salt, int iterationCount) const ! 80: { ! 81: ValidateParameters (key, password, salt, iterationCount); ! 82: derive_key_sha512 ((char *) password.DataPtr(), (int) password.Size(), (char *) salt.Get(), (int) salt.Size(), iterationCount, (char *) key.Get(), (int) key.Size()); ! 83: } ! 84: ! 85: void Pkcs5HmacWhirlpool::DeriveKey (const BufferPtr &key, const VolumePassword &password, const ConstBufferPtr &salt, int iterationCount) const ! 86: { ! 87: ValidateParameters (key, password, salt, iterationCount); ! 88: derive_key_whirlpool ((char *) password.DataPtr(), (int) password.Size(), (char *) salt.Get(), (int) salt.Size(), iterationCount, (char *) key.Get(), (int) key.Size()); ! 89: } ! 90: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.