1 /*
2    Copyright (c) 2000, 2014, 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-1335  USA.
17 */
18 
19 /* sha.hpp provides SHA-1 digests, see RFC 3174
20 */
21 
22 #ifndef TAO_CRYPT_SHA_HPP
23 #define TAO_CRYPT_SHA_HPP
24 
25 #include "hash.hpp"
26 
27 
28 #if defined(TAOCRYPT_X86ASM_AVAILABLE) && defined(TAO_ASM)
29     #define DO_SHA_ASM
30 #endif
31 
32 namespace TaoCrypt {
33 
34 
35 // SHA-1 digest
36 class SHA : public HASHwithTransform {
37 public:
38     enum { BLOCK_SIZE = 64, DIGEST_SIZE = 20, PAD_SIZE = 56,
39            TAO_BYTE_ORDER = BigEndianOrder};   // in Bytes
40     SHA() : HASHwithTransform(DIGEST_SIZE / sizeof(word32), BLOCK_SIZE)
41                 { Init(); }
42     ByteOrder getByteOrder()  const { return ByteOrder(TAO_BYTE_ORDER); }
43     word32    getBlockSize()  const { return BLOCK_SIZE; }
44     word32    getDigestSize() const { return DIGEST_SIZE; }
45     word32    getPadSize()    const { return PAD_SIZE; }
46 
47 #ifdef DO_SHA_ASM
48     void Update(const byte* data, word32 len);
49 #endif
50     void Init();
51 
52     SHA(const SHA&);
53     SHA& operator= (const SHA&);
54 
55     void Swap(SHA&);
56 private:
57     void Transform();
58     void AsmTransform(const byte* data, word32 times);
59 };
60 
61 
62 inline void swap(SHA& a, SHA& b)
63 {
64     a.Swap(b);
65 }
66 
67 // SHA-256 digest
68 class SHA256 : public HASHwithTransform {
69 public:
70     enum { BLOCK_SIZE = 64, DIGEST_SIZE = 32, PAD_SIZE = 56,
71            TAO_BYTE_ORDER = BigEndianOrder};   // in Bytes
72     SHA256() : HASHwithTransform(DIGEST_SIZE / sizeof(word32), BLOCK_SIZE)
73                 { Init(); }
74     ByteOrder getByteOrder()  const { return ByteOrder(TAO_BYTE_ORDER); }
75     word32    getBlockSize()  const { return BLOCK_SIZE; }
76     word32    getDigestSize() const { return DIGEST_SIZE; }
77     word32    getPadSize()    const { return PAD_SIZE; }
78 
79     void Init();
80 
81     SHA256(const SHA256&);
82     SHA256& operator= (const SHA256&);
83 
84     void Swap(SHA256&);
85 private:
86     void Transform();
87 };
88 
89 
90 // SHA-224 digest
91 class SHA224 : public HASHwithTransform {
92 public:
93     enum { BLOCK_SIZE = 64, DIGEST_SIZE = 28, PAD_SIZE = 56,
94            TAO_BYTE_ORDER = BigEndianOrder};   // in Bytes
95     SHA224() : HASHwithTransform(SHA256::DIGEST_SIZE /sizeof(word32),BLOCK_SIZE)
96                 { Init(); }
97     ByteOrder getByteOrder()  const { return ByteOrder(TAO_BYTE_ORDER); }
98     word32    getBlockSize()  const { return BLOCK_SIZE; }
99     word32    getDigestSize() const { return DIGEST_SIZE; }
100     word32    getPadSize()    const { return PAD_SIZE; }
101 
102     void Init();
103 
104     SHA224(const SHA224&);
105     SHA224& operator= (const SHA224&);
106 
107     void Swap(SHA224&);
108 private:
109     void Transform();
110 };
111 
112 
113 #ifdef WORD64_AVAILABLE
114 
115 // SHA-512 digest
116 class SHA512 : public HASH64withTransform {
117 public:
118     enum { BLOCK_SIZE = 128, DIGEST_SIZE = 64, PAD_SIZE = 112,
119            TAO_BYTE_ORDER = BigEndianOrder};   // in Bytes
120     SHA512() : HASH64withTransform(DIGEST_SIZE / sizeof(word64), BLOCK_SIZE)
121                 { Init(); }
122     ByteOrder getByteOrder()  const { return ByteOrder(TAO_BYTE_ORDER); }
123     word32    getBlockSize()  const { return BLOCK_SIZE; }
124     word32    getDigestSize() const { return DIGEST_SIZE; }
125     word32    getPadSize()    const { return PAD_SIZE; }
126 
127     void Init();
128 
129     SHA512(const SHA512&);
130     SHA512& operator= (const SHA512&);
131 
132     void Swap(SHA512&);
133 private:
134     void Transform();
135 };
136 
137 
138 // SHA-384 digest
139 class SHA384 : public HASH64withTransform {
140 public:
141     enum { BLOCK_SIZE = 128, DIGEST_SIZE = 48, PAD_SIZE = 112,
142            TAO_BYTE_ORDER = BigEndianOrder};   // in Bytes
143     SHA384() : HASH64withTransform(SHA512::DIGEST_SIZE/ sizeof(word64),
144                                    BLOCK_SIZE)
145                 { Init(); }
146     ByteOrder getByteOrder()  const { return ByteOrder(TAO_BYTE_ORDER); }
147     word32    getBlockSize()  const { return BLOCK_SIZE; }
148     word32    getDigestSize() const { return DIGEST_SIZE; }
149     word32    getPadSize()    const { return PAD_SIZE; }
150 
151     void Init();
152 
153     SHA384(const SHA384&);
154     SHA384& operator= (const SHA384&);
155 
156     void Swap(SHA384&);
157 private:
158     void Transform();
159 };
160 
161 enum { MAX_SHA2_DIGEST_SIZE = 64 };   // SHA512
162 
163 #else
164 
165 enum { MAX_SHA2_DIGEST_SIZE = 32 };   // SHA256
166 
167 #endif // WORD64_AVAILABLE
168 
169 
170 } // namespace
171 
172 
173 #endif // TAO_CRYPT_SHA_HPP
174 
175