|
|
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 "Hash.h" ! 10: ! 11: #include "Crypto/Rmd160.h" ! 12: #include "Crypto/Sha1.h" ! 13: #include "Crypto/Sha2.h" ! 14: #include "Crypto/Whirlpool.h" ! 15: ! 16: namespace TrueCrypt ! 17: { ! 18: HashList Hash::GetAvailableAlgorithms () ! 19: { ! 20: HashList l; ! 21: ! 22: l.push_back (shared_ptr <Hash> (new Ripemd160 ())); ! 23: l.push_back (shared_ptr <Hash> (new Sha512 ())); ! 24: l.push_back (shared_ptr <Hash> (new Whirlpool ())); ! 25: l.push_back (shared_ptr <Hash> (new Sha1 ())); ! 26: ! 27: return l; ! 28: } ! 29: ! 30: void Hash::ValidateDataParameters (const ConstBufferPtr &data) const ! 31: { ! 32: if (data.Size() < 1) ! 33: throw ParameterIncorrect (SRC_POS); ! 34: } ! 35: ! 36: void Hash::ValidateDigestParameters (const BufferPtr &buffer) const ! 37: { ! 38: if (buffer.Size() != GetDigestSize ()) ! 39: throw ParameterIncorrect (SRC_POS); ! 40: } ! 41: ! 42: // RIPEMD-160 ! 43: Ripemd160::Ripemd160 () ! 44: { ! 45: Context.Allocate (sizeof (RMD160_CTX)); ! 46: Init(); ! 47: } ! 48: ! 49: void Ripemd160::GetDigest (const BufferPtr &buffer) ! 50: { ! 51: if_debug (ValidateDigestParameters (buffer)); ! 52: RMD160Final (buffer, (RMD160_CTX *) Context.Ptr()); ! 53: } ! 54: ! 55: void Ripemd160::Init () ! 56: { ! 57: RMD160Init ((RMD160_CTX *) Context.Ptr()); ! 58: } ! 59: ! 60: void Ripemd160::ProcessData (const ConstBufferPtr &data) ! 61: { ! 62: if_debug (ValidateDataParameters (data)); ! 63: RMD160Update ((RMD160_CTX *) Context.Ptr(), data.Get(), (int) data.Size()); ! 64: } ! 65: ! 66: // SHA-1 ! 67: Sha1::Sha1 () ! 68: { ! 69: Deprecated = true; ! 70: Context.Allocate (sizeof (sha1_ctx)); ! 71: Init(); ! 72: } ! 73: ! 74: void Sha1::GetDigest (const BufferPtr &buffer) ! 75: { ! 76: if_debug (ValidateDigestParameters (buffer)); ! 77: sha1_end (buffer, (sha1_ctx *) Context.Ptr()); ! 78: } ! 79: ! 80: void Sha1::Init () ! 81: { ! 82: sha1_begin ((sha1_ctx *) Context.Ptr()); ! 83: } ! 84: ! 85: void Sha1::ProcessData (const ConstBufferPtr &data) ! 86: { ! 87: if_debug (ValidateDataParameters (data)); ! 88: sha1_hash (data.Get(), (int) data.Size(), (sha1_ctx *) Context.Ptr()); ! 89: } ! 90: ! 91: // SHA-512 ! 92: Sha512::Sha512 () ! 93: { ! 94: Context.Allocate (sizeof (sha512_ctx)); ! 95: Init(); ! 96: } ! 97: ! 98: void Sha512::GetDigest (const BufferPtr &buffer) ! 99: { ! 100: if_debug (ValidateDigestParameters (buffer)); ! 101: sha512_end (buffer, (sha512_ctx *) Context.Ptr()); ! 102: } ! 103: ! 104: void Sha512::Init () ! 105: { ! 106: sha512_begin ((sha512_ctx *) Context.Ptr()); ! 107: } ! 108: ! 109: void Sha512::ProcessData (const ConstBufferPtr &data) ! 110: { ! 111: if_debug (ValidateDataParameters (data)); ! 112: sha512_hash (data.Get(), (int) data.Size(), (sha512_ctx *) Context.Ptr()); ! 113: } ! 114: ! 115: // Whirlpool ! 116: Whirlpool::Whirlpool () ! 117: { ! 118: Context.Allocate (sizeof (WHIRLPOOL_CTX)); ! 119: Init(); ! 120: } ! 121: ! 122: void Whirlpool::GetDigest (const BufferPtr &buffer) ! 123: { ! 124: if_debug (ValidateDigestParameters (buffer)); ! 125: WHIRLPOOL_finalize ((WHIRLPOOL_CTX *) Context.Ptr(), buffer); ! 126: } ! 127: ! 128: void Whirlpool::Init () ! 129: { ! 130: WHIRLPOOL_init ((WHIRLPOOL_CTX *) Context.Ptr()); ! 131: } ! 132: ! 133: void Whirlpool::ProcessData (const ConstBufferPtr &data) ! 134: { ! 135: if_debug (ValidateDataParameters (data)); ! 136: WHIRLPOOL_add (data.Get(), (int) data.Size() * 8, (WHIRLPOOL_CTX *) Context.Ptr()); ! 137: } ! 138: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.