1 /*
2    Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
3 
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; version 2 of the License.
7 
8    This program is distributed in the hope that it will be useful,
9    but WITHOUT ANY WARRANTY; without even the implied warranty of
10    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11    GNU General Public License for more details.
12 
13    You should have received a copy of the GNU General Public License
14    along with this program; see the file COPYING. If not, write to the
15    Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
16    MA  02110-1301  USA.
17 */
18 
19 /* hash.hpp provides a base for digest types
20 */
21 
22 
23 #ifndef TAO_CRYPT_HASH_HPP
24 #define TAO_CRYPT_HASH_HPP
25 
26 #include "misc.hpp"
27 
28 namespace TaoCrypt {
29 
30 
31 // HASH
32 class HASH : public virtual_base {
33 public:
~HASH()34     virtual ~HASH() {}
35 
36     virtual void Update(const byte*, word32) = 0;
37     virtual void Final(byte*)                = 0;
38 
39     virtual void Init() = 0;
40 
41     virtual word32 getBlockSize()  const = 0;
42     virtual word32 getDigestSize() const = 0;
43 };
44 
45 
46 // HASH with Transform
47 class HASHwithTransform : public HASH {
48 public:
49     HASHwithTransform(word32 digSz, word32 buffSz);
~HASHwithTransform()50     virtual ~HASHwithTransform() {}
51     virtual ByteOrder getByteOrder()  const = 0;
52     virtual word32    getPadSize()    const = 0;
53 
54     virtual void Update(const byte*, word32);
55     virtual void Final(byte*);
56 
GetBitCountLo() const57     word32  GetBitCountLo() const { return  loLen_ << 3; }
GetBitCountHi() const58     word32  GetBitCountHi() const { return (loLen_ >> (8*sizeof(loLen_) - 3)) +
59                                            (hiLen_ << 3); }
60     enum { MaxDigestSz = 8, MaxBufferSz = 64 };
61 protected:
62     typedef word32 HashLengthType;
63     word32          buffLen_;   // in bytes
64     HashLengthType  loLen_;     // length in bytes
65     HashLengthType  hiLen_;     // length in bytes
66     word32          digest_[MaxDigestSz];
67     word32          buffer_[MaxBufferSz / sizeof(word32)];
68 
69     virtual void Transform() = 0;
70 
71     void AddLength(word32);
72 };
73 
74 
75 #ifdef WORD64_AVAILABLE
76 
77 // 64-bit HASH with Transform
78 class HASH64withTransform : public HASH {
79 public:
80     HASH64withTransform(word32 digSz, word32 buffSz);
~HASH64withTransform()81     virtual ~HASH64withTransform() {}
82     virtual ByteOrder getByteOrder()  const = 0;
83     virtual word32    getPadSize()    const = 0;
84 
85     virtual void Update(const byte*, word32);
86     virtual void Final(byte*);
87 
GetBitCountLo() const88     word32  GetBitCountLo() const { return  loLen_ << 3; }
GetBitCountHi() const89     word32  GetBitCountHi() const { return (loLen_ >> (8*sizeof(loLen_) - 3)) +
90                                            (hiLen_ << 3); }
91     enum { MaxDigestSz = 8, MaxBufferSz = 128 };
92 protected:
93     typedef word32 HashLengthType;
94     word32          buffLen_;   // in bytes
95     HashLengthType  loLen_;     // length in bytes
96     HashLengthType  hiLen_;     // length in bytes
97     word64          digest_[MaxDigestSz];
98     word64          buffer_[MaxBufferSz / sizeof(word64)];
99 
100     virtual void Transform() = 0;
101 
102     void AddLength(word32);
103 };
104 
105 #endif // WORD64_AVAILABLE
106 
107 
108 } // namespace
109 
110 #endif // TAO_CRYPT_HASH_HPP
111