LLVM: lib/Support/SHA1.cpp Source File (original) (raw)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

23

24using namespace llvm;

25

29

31

33 Buf[I & 15] = rol(Buf[(I + 13) & 15] ^ Buf[(I + 8) & 15] ^ Buf[(I + 2) & 15] ^

34 Buf[I & 15],

35 1);

36 return Buf[I & 15];

37}

38

41 E += ((B & (C ^ D)) ^ D) + blk0(Buf, I) + 0x5A827999 + rol(A, 5);

43}

44

47 E += ((B & (C ^ D)) ^ D) + blk(Buf, I) + 0x5A827999 + rol(A, 5);

49}

50

53 E += (B ^ C ^ D) + blk(Buf, I) + 0x6ED9EBA1 + rol(A, 5);

55}

56

59 E += (((B | C) & D) | (B & C)) + blk(Buf, I) + 0x8F1BBCDC + rol(A, 5);

61}

62

65 E += (B ^ C ^ D) + blk(Buf, I) + 0xCA62C1D6 + rol(A, 5);

67}

68

69

70#define SHA1_K0 0x5a827999

71#define SHA1_K20 0x6ed9eba1

72#define SHA1_K40 0x8f1bbcdc

73#define SHA1_K60 0xca62c1d6

74

75#define SEED_0 0x67452301

76#define SEED_1 0xefcdab89

77#define SEED_2 0x98badcfe

78#define SEED_3 0x10325476

79#define SEED_4 0xc3d2e1f0

80

82 InternalState.State[0] = SEED_0;

83 InternalState.State[1] = SEED_1;

84 InternalState.State[2] = SEED_2;

85 InternalState.State[3] = SEED_3;

86 InternalState.State[4] = SEED_4;

87 InternalState.ByteCount = 0;

88 InternalState.BufferOffset = 0;

89}

90

91void SHA1::hashBlock() {

92 uint32_t A = InternalState.State[0];

93 uint32_t B = InternalState.State[1];

94 uint32_t C = InternalState.State[2];

95 uint32_t D = InternalState.State[3];

96 uint32_t E = InternalState.State[4];

97

98

99 r0(A, B, C, D, E, 0, InternalState.Buffer.L);

100 r0(E, A, B, C, D, 1, InternalState.Buffer.L);

101 r0(D, E, A, B, C, 2, InternalState.Buffer.L);

102 r0(C, D, E, A, B, 3, InternalState.Buffer.L);

103 r0(B, C, D, E, A, 4, InternalState.Buffer.L);

104 r0(A, B, C, D, E, 5, InternalState.Buffer.L);

105 r0(E, A, B, C, D, 6, InternalState.Buffer.L);

106 r0(D, E, A, B, C, 7, InternalState.Buffer.L);

107 r0(C, D, E, A, B, 8, InternalState.Buffer.L);

108 r0(B, C, D, E, A, 9, InternalState.Buffer.L);

109 r0(A, B, C, D, E, 10, InternalState.Buffer.L);

110 r0(E, A, B, C, D, 11, InternalState.Buffer.L);

111 r0(D, E, A, B, C, 12, InternalState.Buffer.L);

112 r0(C, D, E, A, B, 13, InternalState.Buffer.L);

113 r0(B, C, D, E, A, 14, InternalState.Buffer.L);

114 r0(A, B, C, D, E, 15, InternalState.Buffer.L);

115 r1(E, A, B, C, D, 16, InternalState.Buffer.L);

116 r1(D, E, A, B, C, 17, InternalState.Buffer.L);

117 r1(C, D, E, A, B, 18, InternalState.Buffer.L);

118 r1(B, C, D, E, A, 19, InternalState.Buffer.L);

119

120 r2(A, B, C, D, E, 20, InternalState.Buffer.L);

121 r2(E, A, B, C, D, 21, InternalState.Buffer.L);

122 r2(D, E, A, B, C, 22, InternalState.Buffer.L);

123 r2(C, D, E, A, B, 23, InternalState.Buffer.L);

124 r2(B, C, D, E, A, 24, InternalState.Buffer.L);

125 r2(A, B, C, D, E, 25, InternalState.Buffer.L);

126 r2(E, A, B, C, D, 26, InternalState.Buffer.L);

127 r2(D, E, A, B, C, 27, InternalState.Buffer.L);

128 r2(C, D, E, A, B, 28, InternalState.Buffer.L);

129 r2(B, C, D, E, A, 29, InternalState.Buffer.L);

130 r2(A, B, C, D, E, 30, InternalState.Buffer.L);

131 r2(E, A, B, C, D, 31, InternalState.Buffer.L);

132 r2(D, E, A, B, C, 32, InternalState.Buffer.L);

133 r2(C, D, E, A, B, 33, InternalState.Buffer.L);

134 r2(B, C, D, E, A, 34, InternalState.Buffer.L);

135 r2(A, B, C, D, E, 35, InternalState.Buffer.L);

136 r2(E, A, B, C, D, 36, InternalState.Buffer.L);

137 r2(D, E, A, B, C, 37, InternalState.Buffer.L);

138 r2(C, D, E, A, B, 38, InternalState.Buffer.L);

139 r2(B, C, D, E, A, 39, InternalState.Buffer.L);

140

141 r3(A, B, C, D, E, 40, InternalState.Buffer.L);

142 r3(E, A, B, C, D, 41, InternalState.Buffer.L);

143 r3(D, E, A, B, C, 42, InternalState.Buffer.L);

144 r3(C, D, E, A, B, 43, InternalState.Buffer.L);

145 r3(B, C, D, E, A, 44, InternalState.Buffer.L);

146 r3(A, B, C, D, E, 45, InternalState.Buffer.L);

147 r3(E, A, B, C, D, 46, InternalState.Buffer.L);

148 r3(D, E, A, B, C, 47, InternalState.Buffer.L);

149 r3(C, D, E, A, B, 48, InternalState.Buffer.L);

150 r3(B, C, D, E, A, 49, InternalState.Buffer.L);

151 r3(A, B, C, D, E, 50, InternalState.Buffer.L);

152 r3(E, A, B, C, D, 51, InternalState.Buffer.L);

153 r3(D, E, A, B, C, 52, InternalState.Buffer.L);

154 r3(C, D, E, A, B, 53, InternalState.Buffer.L);

155 r3(B, C, D, E, A, 54, InternalState.Buffer.L);

156 r3(A, B, C, D, E, 55, InternalState.Buffer.L);

157 r3(E, A, B, C, D, 56, InternalState.Buffer.L);

158 r3(D, E, A, B, C, 57, InternalState.Buffer.L);

159 r3(C, D, E, A, B, 58, InternalState.Buffer.L);

160 r3(B, C, D, E, A, 59, InternalState.Buffer.L);

161

162 r4(A, B, C, D, E, 60, InternalState.Buffer.L);

163 r4(E, A, B, C, D, 61, InternalState.Buffer.L);

164 r4(D, E, A, B, C, 62, InternalState.Buffer.L);

165 r4(C, D, E, A, B, 63, InternalState.Buffer.L);

166 r4(B, C, D, E, A, 64, InternalState.Buffer.L);

167 r4(A, B, C, D, E, 65, InternalState.Buffer.L);

168 r4(E, A, B, C, D, 66, InternalState.Buffer.L);

169 r4(D, E, A, B, C, 67, InternalState.Buffer.L);

170 r4(C, D, E, A, B, 68, InternalState.Buffer.L);

171 r4(B, C, D, E, A, 69, InternalState.Buffer.L);

172 r4(A, B, C, D, E, 70, InternalState.Buffer.L);

173 r4(E, A, B, C, D, 71, InternalState.Buffer.L);

174 r4(D, E, A, B, C, 72, InternalState.Buffer.L);

175 r4(C, D, E, A, B, 73, InternalState.Buffer.L);

176 r4(B, C, D, E, A, 74, InternalState.Buffer.L);

177 r4(A, B, C, D, E, 75, InternalState.Buffer.L);

178 r4(E, A, B, C, D, 76, InternalState.Buffer.L);

179 r4(D, E, A, B, C, 77, InternalState.Buffer.L);

180 r4(C, D, E, A, B, 78, InternalState.Buffer.L);

181 r4(B, C, D, E, A, 79, InternalState.Buffer.L);

182

183 InternalState.State[0] += A;

184 InternalState.State[1] += B;

185 InternalState.State[2] += C;

186 InternalState.State[3] += D;

187 InternalState.State[4] += E;

188}

189

192 InternalState.Buffer.C[InternalState.BufferOffset] = Data;

193 else

194 InternalState.Buffer.C[InternalState.BufferOffset ^ 3] = Data;

195

196 InternalState.BufferOffset++;

197 if (InternalState.BufferOffset == BLOCK_LENGTH) {

199 InternalState.BufferOffset = 0;

200 }

201}

202

203void SHA1::writebyte(uint8_t Data) {

204 ++InternalState.ByteCount;

205 addUncounted(Data);

206}

207

209 InternalState.ByteCount += Data.size();

210

211

212 if (InternalState.BufferOffset > 0) {

213 const size_t Remainder = std::min<size_t>(

214 Data.size(), BLOCK_LENGTH - InternalState.BufferOffset);

215 for (size_t I = 0; I < Remainder; ++I)

216 addUncounted(Data[I]);

217 Data = Data.drop_front(Remainder);

218 }

219

220

221 while (Data.size() >= BLOCK_LENGTH) {

222 assert(InternalState.BufferOffset == 0);

223 static_assert(BLOCK_LENGTH % 4 == 0);

224 constexpr size_t BLOCK_LENGTH_32 = BLOCK_LENGTH / 4;

225 for (size_t I = 0; I < BLOCK_LENGTH_32; ++I)

227 hashBlock();

228 Data = Data.drop_front(BLOCK_LENGTH);

229 }

230

231

233 addUncounted(C);

234}

235

240

241void SHA1::pad() {

242

243

244

245 addUncounted(0x80);

246 while (InternalState.BufferOffset != 56)

247 addUncounted(0x00);

248

249

250 addUncounted(0);

251 addUncounted(0);

252 addUncounted(0);

253 addUncounted(InternalState.ByteCount >> 29);

254 addUncounted(InternalState.ByteCount >>

255 21);

256 addUncounted(InternalState.ByteCount >> 13);

257 addUncounted(InternalState.ByteCount >> 5);

258 addUncounted(InternalState.ByteCount << 3);

259}

260

261void SHA1::final(std::array<uint32_t, HASH_LENGTH / 4> &HashResult) {

262

264

266

267 for (int i = 0; i < 5; i++) {

268 HashResult[i] = InternalState.State[i];

269 }

270 } else {

271

272 for (int i = 0; i < 5; i++) {

273 HashResult[i] = llvm::byteswap(InternalState.State[i]);

274 }

275 }

276}

277

279 union {

280 std::array<uint32_t, HASH_LENGTH / 4> HashResult;

281 std::array<uint8_t, HASH_LENGTH> ReturnResult;

282 };

283 static_assert(sizeof(HashResult) == sizeof(ReturnResult));

284 final(HashResult);

285 return ReturnResult;

286}

287

289 auto StateToRestore = InternalState;

290

291 auto Hash = final();

292

293

294 InternalState = StateToRestore;

295

296

297 return Hash;

298}

299

303 return Hash.final();

304}

assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")

static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")

static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")

static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")

static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")

uint64_t hashBlock(const MachineBasicBlock &MBB, bool HashOperands)

static void r0(uint32_t &A, uint32_t &B, uint32_t &C, uint32_t &D, uint32_t &E, int I, uint32_t *Buf)

Definition SHA1.cpp:39

#define SEED_3

Definition SHA1.cpp:78

static void r3(uint32_t &A, uint32_t &B, uint32_t &C, uint32_t &D, uint32_t &E, int I, uint32_t *Buf)

Definition SHA1.cpp:57

#define SEED_4

Definition SHA1.cpp:79

static uint32_t rol(uint32_t Number, int Bits)

Definition SHA1.cpp:26

static void r2(uint32_t &A, uint32_t &B, uint32_t &C, uint32_t &D, uint32_t &E, int I, uint32_t *Buf)

Definition SHA1.cpp:51

static uint32_t blk0(uint32_t *Buf, int I)

Definition SHA1.cpp:30

#define SEED_0

Definition SHA1.cpp:75

static void r4(uint32_t &A, uint32_t &B, uint32_t &C, uint32_t &D, uint32_t &E, int I, uint32_t *Buf)

Definition SHA1.cpp:63

static void r1(uint32_t &A, uint32_t &B, uint32_t &C, uint32_t &D, uint32_t &E, int I, uint32_t *Buf)

Definition SHA1.cpp:45

static uint32_t blk(uint32_t *Buf, int I)

Definition SHA1.cpp:32

#define SEED_1

Definition SHA1.cpp:76

#define SEED_2

Definition SHA1.cpp:77

static void pad(raw_fd_ostream &OS)

ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...

LLVM_ABI std::array< uint8_t, 20 > final()

Return the current raw 160-bits SHA1 for the digested data since the last call to init().

Definition SHA1.cpp:278

LLVM_ABI void update(ArrayRef< uint8_t > Data)

Digest more data.

Definition SHA1.cpp:208

static LLVM_ABI std::array< uint8_t, 20 > hash(ArrayRef< uint8_t > Data)

Returns a raw 160-bit SHA1 hash for the given data.

Definition SHA1.cpp:300

LLVM_ABI std::array< uint8_t, 20 > result()

Return the current raw 160-bits SHA1 for the digested data since the last call to init().

Definition SHA1.cpp:288

LLVM_ABI void init()

Reinitialize the internal state.

Definition SHA1.cpp:81

StringRef - Represent a constant reference to a string, i.e.

@ C

The default llvm calling convention, compatible with C.

uint32_t read32be(const void *P)

constexpr bool IsBigEndianHost

This is an optimization pass for GlobalISel generic memory operations.

constexpr T byteswap(T V) noexcept

Reverses the bytes in the given integer value V.

FunctionAddr VTableAddr uintptr_t uintptr_t Data