WSF
UtSHA.hpp
Go to the documentation of this file.
1// ****************************************************************************
2// CUI
3//
4// The Advanced Framework for Simulation, Integration, and Modeling (AFSIM)
5//
6// Copyright 2003-2015 The Boeing Company. All rights reserved.
7//
8// The use, dissemination or disclosure of data in this file is subject to
9// limitation or restriction. See accompanying README and LICENSE for details.
10// ****************************************************************************
11
12
13#ifndef UTSHA_HPP
14#define UTSHA_HPP
15
16#include "WsfUtExport.hpp"
17
18// Typical DISCLAIMER:
19// The code in this project is Copyright (C) 2003 by George Anescu. You have the right to
20// use and distribute the code in any way you see fit as long as this paragraph is included
21// with the distribution. No warranties or claims are made as to the validity of the
22// information and code contained herein, so use it at your own risk.
23
24#include <string>
25class UtSHA_Digest;
26// SHA Message Digest algorithm
28{
29public:
30 UtSHA();
31 // Update context to reflect the concatenation of another buffer of bytes.
32 void AddData(char const* aData, size_t aDataLength);
33
34 // Convenience method to call AddData with a single value
35 template<typename T>
36 void Add(const T& value)
37 {
38 AddData((const char*)&value, (unsigned int)sizeof(value));
39 }
40
41 // Final wrapup - pad to 64-byte boundary with the bit pattern
42 // 1 0*(64-bit count of bits processed, MSB-first)
43 void FinalDigest(char* aDigest);
44 void FinalDigest(UtSHA_Digest& aDigest);
45 // Reset current operation in order to prepare a new one
46 void Reset();
47
48private:
49 enum
50 {
51 BLOCKSIZE = 64
52 };
53 enum
54 {
55 SHA256LENGTH = 8
56 };
57
58 // Context Variables
59 unsigned int mBuf[SHA256LENGTH]; // Maximum for SHA256
60 size_t mBits[2];
61 unsigned char mIn[BLOCKSIZE];
62
63 // Internal auxiliary static functions
64 unsigned int CircularShift(unsigned int aBits, unsigned int aWord);
65 static unsigned int CH(unsigned int aX, unsigned int aY, unsigned int aZ);
66 static unsigned int MAJ(unsigned int aX, unsigned int aY, unsigned int aZ);
67
68 static unsigned int SIG0(unsigned int aX);
69 static unsigned int SIG1(unsigned int aX);
70 static unsigned int sig0(unsigned int aX);
71 static unsigned int sig1(unsigned int aX);
72
73 static void Bytes2Word(unsigned char const* aBytes, unsigned int& aWord);
74 static void Word2Bytes(size_t const& aWord, unsigned char* aBytes);
75
76 // Transformation Function
77 void Transform();
78
79 // The Method
80 static const unsigned int mK256[64];
81 static const unsigned int mH256[SHA256LENGTH];
82 // Control Flag
83 // bool mAddData;
84};
85
88{
89public:
90 void Clear() { mWord64[0] = mWord64[1] = mWord64[2] = mWord64[3] = 0; }
91 enum
92 {
94 };
95 bool operator==(const UtSHA_Digest& aRhs) const
96 {
97 return mWord64[0] == aRhs.mWord64[0] && mWord64[1] == aRhs.mWord64[1] && mWord64[2] == aRhs.mWord64[2] &&
98 mWord64[3] == aRhs.mWord64[3];
99 }
100 bool operator!=(const UtSHA_Digest& aRhs) const { return !(*this == aRhs); }
101 bool operator<(const UtSHA_Digest& aRhs) const
102 {
103 if (mWord64[0] < aRhs.mWord64[0])
104 return true;
105 if (mWord64[0] > aRhs.mWord64[0])
106 return false;
107 if (mWord64[1] < aRhs.mWord64[1])
108 return true;
109 if (mWord64[1] > aRhs.mWord64[1])
110 return false;
111 if (mWord64[2] < aRhs.mWord64[2])
112 return true;
113 if (mWord64[2] > aRhs.mWord64[2])
114 return false;
115 return mWord64[3] < aRhs.mWord64[3];
116 }
117 std::string ToHex() const;
118 union
119 {
121 unsigned int mWords[8];
122 unsigned long long mWord64[4];
123 };
124};
125
126inline unsigned int UtSHA::CircularShift(unsigned int aBits, unsigned int aWord)
127{
128 return (aWord << aBits) | (aWord >> (32 - aBits));
129}
130
131inline unsigned int UtSHA::CH(unsigned int aX, unsigned int aY, unsigned int aZ)
132{
133 return ((aX & (aY ^ aZ)) ^ aZ);
134}
135
136inline unsigned int UtSHA::MAJ(unsigned int aX, unsigned int aY, unsigned int aZ)
137{
138 return (((aX | aY) & aZ) | (aX & aY));
139}
140
141inline unsigned int UtSHA::SIG0(unsigned int aX)
142{
143 return ((aX >> 2) | (aX << 30)) ^ ((aX >> 13) | (aX << 19)) ^ ((aX >> 22) | (aX << 10));
144}
145
146inline unsigned int UtSHA::SIG1(unsigned int aX)
147{
148 return ((aX >> 6) | (aX << 26)) ^ ((aX >> 11) | (aX << 21)) ^ ((aX >> 25) | (aX << 7));
149}
150
151inline unsigned int UtSHA::sig0(unsigned int aX)
152{
153 return ((aX >> 7) | (aX << 25)) ^ ((aX >> 18) | (aX << 14)) ^ (aX >> 3);
154}
155
156inline unsigned int UtSHA::sig1(unsigned int aX)
157{
158 return ((aX >> 17) | (aX << 15)) ^ ((aX >> 19) | (aX << 13)) ^ (aX >> 10);
159}
160
161inline void UtSHA::Bytes2Word(unsigned char const* aBytes, unsigned int& aWord)
162{
163 aWord = (unsigned int)*(aBytes + 3) | (unsigned int)(*(aBytes + 2) << 8) | (unsigned int)(*(aBytes + 1) << 16) |
164 (unsigned int)(*aBytes << 24);
165}
166
167inline void UtSHA::Word2Bytes(size_t const& aWord, unsigned char* aBytes)
168{
169 aBytes += 3;
170 *aBytes = aWord & 0xff;
171 *--aBytes = (aWord >> 8) & 0xff;
172 *--aBytes = (aWord >> 16) & 0xff;
173 *--aBytes = (aWord >> 24) & 0xff;
174}
175
176#endif
#define WSF_UT_EXPORT
Definition WsfUtExport.hpp:32
Container for a UtSHA Digest.
Definition UtSHA.hpp:88
@ cBUFFER_SIZE
Definition UtSHA.hpp:93
void Clear()
Definition UtSHA.hpp:90
unsigned int mWords[8]
Definition UtSHA.hpp:121
unsigned long long mWord64[4]
Definition UtSHA.hpp:122
bool operator!=(const UtSHA_Digest &aRhs) const
Definition UtSHA.hpp:100
bool operator==(const UtSHA_Digest &aRhs) const
Definition UtSHA.hpp:95
char mBytes[cBUFFER_SIZE]
Definition UtSHA.hpp:120
bool operator<(const UtSHA_Digest &aRhs) const
Definition UtSHA.hpp:101
void Add(const T &value)
Definition UtSHA.hpp:36
UtSHA()
Definition UtSHA.cpp:35
void AddData(char const *aData, size_t aDataLength)
Definition UtSHA.cpp:50
Copyrights Multiple, All Rights Reserved