"Fossies" - the Fresh Open Source Software Archive

Member "src/Volume/Hash.cpp" (10 Oct 2018, 4003 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 "Hash.cpp" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 1.20_Source_vs_1.21_Source.

    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 "Hash.h"
   14 
   15 #include "Crypto/Rmd160.h"
   16 #include "Crypto/Sha2.h"
   17 #include "Crypto/Whirlpool.h"
   18 #include "Crypto/Streebog.h"
   19 
   20 namespace VeraCrypt
   21 {
   22     HashList Hash::GetAvailableAlgorithms ()
   23     {
   24         HashList l;
   25 
   26         l.push_back (shared_ptr <Hash> (new Sha512 ()));
   27         l.push_back (shared_ptr <Hash> (new Whirlpool ()));
   28         l.push_back (shared_ptr <Hash> (new Sha256 ()));
   29         l.push_back (shared_ptr <Hash> (new Streebog ()));
   30         l.push_back (shared_ptr <Hash> (new Ripemd160 ()));
   31 
   32         return l;
   33     }
   34 
   35     void Hash::ValidateDataParameters (const ConstBufferPtr &data) const
   36     {
   37         if (data.Size() < 1)
   38             throw ParameterIncorrect (SRC_POS);
   39     }
   40 
   41     void Hash::ValidateDigestParameters (const BufferPtr &buffer) const
   42     {
   43         if (buffer.Size() != GetDigestSize ())
   44             throw ParameterIncorrect (SRC_POS);
   45     }
   46 
   47     // RIPEMD-160
   48     Ripemd160::Ripemd160 ()
   49     {
   50         Deprecated = true; // Mark RIPEMD-160 as deprecated like on Windows.
   51         Context.Allocate (sizeof (RMD160_CTX), 32);
   52         Init();
   53     }
   54 
   55     void Ripemd160::GetDigest (const BufferPtr &buffer)
   56     {
   57         if_debug (ValidateDigestParameters (buffer));
   58         RMD160Final (buffer, (RMD160_CTX *) Context.Ptr());
   59     }
   60 
   61     void Ripemd160::Init ()
   62     {
   63         RMD160Init ((RMD160_CTX *) Context.Ptr());
   64     }
   65 
   66     void Ripemd160::ProcessData (const ConstBufferPtr &data)
   67     {
   68         if_debug (ValidateDataParameters (data));
   69         RMD160Update ((RMD160_CTX *) Context.Ptr(), data.Get(), (int) data.Size());
   70     }
   71 
   72     // SHA-256
   73     Sha256::Sha256 ()
   74     {
   75         Context.Allocate (sizeof (sha256_ctx), 32);
   76         Init();
   77     }
   78 
   79     void Sha256::GetDigest (const BufferPtr &buffer)
   80     {
   81         if_debug (ValidateDigestParameters (buffer));
   82         sha256_end (buffer, (sha256_ctx *) Context.Ptr());
   83     }
   84 
   85     void Sha256::Init ()
   86     {
   87         sha256_begin ((sha256_ctx *) Context.Ptr());
   88     }
   89 
   90     void Sha256::ProcessData (const ConstBufferPtr &data)
   91     {
   92         if_debug (ValidateDataParameters (data));
   93         sha256_hash (data.Get(), (int) data.Size(), (sha256_ctx *) Context.Ptr());
   94     }
   95 
   96     // SHA-512
   97     Sha512::Sha512 ()
   98     {
   99         Context.Allocate (sizeof (sha512_ctx), 32);
  100         Init();
  101     }
  102 
  103     void Sha512::GetDigest (const BufferPtr &buffer)
  104     {
  105         if_debug (ValidateDigestParameters (buffer));
  106         sha512_end (buffer, (sha512_ctx *) Context.Ptr());
  107     }
  108 
  109     void Sha512::Init ()
  110     {
  111         sha512_begin ((sha512_ctx *) Context.Ptr());
  112     }
  113 
  114     void Sha512::ProcessData (const ConstBufferPtr &data)
  115     {
  116         if_debug (ValidateDataParameters (data));
  117         sha512_hash (data.Get(), (int) data.Size(), (sha512_ctx *) Context.Ptr());
  118     }
  119 
  120     // Whirlpool
  121     Whirlpool::Whirlpool ()
  122     {
  123         Context.Allocate (sizeof (WHIRLPOOL_CTX), 32);
  124         Init();
  125     }
  126 
  127     void Whirlpool::GetDigest (const BufferPtr &buffer)
  128     {
  129         if_debug (ValidateDigestParameters (buffer));
  130         WHIRLPOOL_finalize ((WHIRLPOOL_CTX *) Context.Ptr(), buffer);
  131     }
  132 
  133     void Whirlpool::Init ()
  134     {
  135         WHIRLPOOL_init ((WHIRLPOOL_CTX *) Context.Ptr());
  136     }
  137 
  138     void Whirlpool::ProcessData (const ConstBufferPtr &data)
  139     {
  140         if_debug (ValidateDataParameters (data));
  141         WHIRLPOOL_add (data.Get(), (int) data.Size(), (WHIRLPOOL_CTX *) Context.Ptr());
  142     }
  143     
  144     // Streebog
  145     Streebog::Streebog ()
  146     {
  147         Context.Allocate (sizeof (STREEBOG_CTX), 32);
  148         Init();
  149     }
  150 
  151     void Streebog::GetDigest (const BufferPtr &buffer)
  152     {
  153         if_debug (ValidateDigestParameters (buffer));
  154         STREEBOG_finalize ((STREEBOG_CTX *) Context.Ptr(), buffer);
  155     }
  156 
  157     void Streebog::Init ()
  158     {
  159         STREEBOG_init ((STREEBOG_CTX *) Context.Ptr());
  160     }
  161 
  162     void Streebog::ProcessData (const ConstBufferPtr &data)
  163     {
  164         if_debug (ValidateDataParameters (data));
  165         STREEBOG_add ((STREEBOG_CTX *) Context.Ptr(), data.Get(), (int) data.Size());
  166     }
  167 }