1cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com/* 2cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com * Copyright 2013 Google Inc. 3cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com * 4cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com * Use of this source code is governed by a BSD-style license that can be 5cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com * found in the LICENSE file. 6cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com * 7cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com * The following code is based on the description in RFC 3174. 8cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com * http://www.ietf.org/rfc/rfc3174.txt 9cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com */ 10cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com 11cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com#include "SkTypes.h" 12cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com#include "SkSHA1.h" 13cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com#include <string.h> 14cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com 15cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com/** SHA1 basic transformation. Transforms state based on block. */ 16cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.comstatic void transform(uint32_t state[5], const uint8_t block[64]); 17cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com 18cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com/** Encodes input into output (5 big endian 32 bit values). */ 19cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.comstatic void encode(uint8_t output[20], const uint32_t input[5]); 20cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com 21cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com/** Encodes input into output (big endian 64 bit value). */ 22cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.comstatic void encode(uint8_t output[8], const uint64_t input); 23cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com 24cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.comSkSHA1::SkSHA1() : byteCount(0) { 25cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com // These are magic numbers from the specification. The first four are the same as MD5. 26cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com this->state[0] = 0x67452301; 27cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com this->state[1] = 0xefcdab89; 28cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com this->state[2] = 0x98badcfe; 29cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com this->state[3] = 0x10325476; 30cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com this->state[4] = 0xc3d2e1f0; 31cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com} 32cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com 33cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.comvoid SkSHA1::update(const uint8_t* input, size_t inputLength) { 34cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com unsigned int bufferIndex = (unsigned int)(this->byteCount & 0x3F); 35cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com unsigned int bufferAvailable = 64 - bufferIndex; 36cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com 37cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com unsigned int inputIndex; 38cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com if (inputLength >= bufferAvailable) { 39cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com if (bufferIndex) { 40cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com memcpy(&this->buffer[bufferIndex], input, bufferAvailable); 41cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com transform(this->state, this->buffer); 42cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com inputIndex = bufferAvailable; 43cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com } else { 44cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com inputIndex = 0; 45cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com } 46cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com 47cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com for (; inputIndex + 63 < inputLength; inputIndex += 64) { 48cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com transform(this->state, &input[inputIndex]); 49cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com } 50cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com 51cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com bufferIndex = 0; 52cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com } else { 53cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com inputIndex = 0; 54cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com } 55cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com 56cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com memcpy(&this->buffer[bufferIndex], &input[inputIndex], inputLength - inputIndex); 57cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com 58cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com this->byteCount += inputLength; 59cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com} 60cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com 61cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.comvoid SkSHA1::finish(Digest& digest) { 62cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com // Get the number of bits before padding. 63cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com uint8_t bits[8]; 64cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com encode(bits, this->byteCount << 3); 65cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com 66cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com // Pad out to 56 mod 64. 67cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com unsigned int bufferIndex = (unsigned int)(this->byteCount & 0x3F); 68cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com unsigned int paddingLength = (bufferIndex < 56) ? (56 - bufferIndex) : (120 - bufferIndex); 69cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com static uint8_t PADDING[64] = { 70cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 71cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 72cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 73cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 74cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com }; 75cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com this->update(PADDING, paddingLength); 76cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com 77cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com // Append length (length before padding, will cause final update). 78cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com this->update(bits, 8); 79cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com 80cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com // Write out digest. 81cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com encode(digest.data, this->state); 82cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com 83cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com#if defined(SK_SHA1_CLEAR_DATA) 84cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com // Clear state. 85cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com memset(this, 0, sizeof(*this)); 86cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com#endif 87cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com} 88cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com 89cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.comstruct F1 { uint32_t operator()(uint32_t B, uint32_t C, uint32_t D) { 90cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com return (B & C) | ((~B) & D); 91cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com //return D ^ (B & (C ^ D)); 92cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com //return (B & C) ^ ((~B) & D); 93cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com //return (B & C) + ((~B) & D); 94cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com //return _mm_or_ps(_mm_andnot_ps(B, D), _mm_and_ps(B, C)); //SSE2 95cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com //return vec_sel(D, C, B); //PPC 96cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com}}; 97cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com 98cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.comstruct F2 { uint32_t operator()(uint32_t B, uint32_t C, uint32_t D) { 99cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com return B ^ C ^ D; 100cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com}}; 101cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com 102cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.comstruct F3 { uint32_t operator()(uint32_t B, uint32_t C, uint32_t D) { 103cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com return (B & C) | (B & D) | (C & D); 104cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com //return (B & C) | (D & (B | C)); 105cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com //return (B & C) | (D & (B ^ C)); 106cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com //return (B & C) + (D & (B ^ C)); 107cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com //return (B & C) ^ (B & D) ^ (C & D); 108cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com}}; 109cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com 110cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com/** Rotates x left n bits. */ 111cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.comstatic inline uint32_t rotate_left(uint32_t x, uint8_t n) { 112cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com return (x << n) | (x >> (32 - n)); 113cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com} 114cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com 115cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.comtemplate <typename T> 116cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.comstatic inline void operation(T operation, 117cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com uint32_t A, uint32_t& B, uint32_t C, uint32_t D, uint32_t& E, 118cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com uint32_t w, uint32_t k) { 119cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com E += rotate_left(A, 5) + operation(B, C, D) + w + k; 120cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com B = rotate_left(B, 30); 121cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com} 122cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com 123cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.comstatic void transform(uint32_t state[5], const uint8_t block[64]) { 124cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com uint32_t A = state[0], B = state[1], C = state[2], D = state[3], E = state[4]; 125cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com 126cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com // Round constants defined in SHA-1. 127cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com static const uint32_t K[] = { 128cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com 0x5A827999, //sqrt(2) * 2^30 129cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com 0x6ED9EBA1, //sqrt(3) * 2^30 130cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com 0x8F1BBCDC, //sqrt(5) * 2^30 131cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com 0xCA62C1D6, //sqrt(10) * 2^30 132cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com }; 133cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com 134cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com uint32_t W[80]; 135cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com 136cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com // Initialize the array W. 137cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com size_t i = 0; 138cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com for (size_t j = 0; i < 16; ++i, j += 4) { 139cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com W[i] = (((uint32_t)block[j ]) << 24) | 140cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com (((uint32_t)block[j+1]) << 16) | 141cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com (((uint32_t)block[j+2]) << 8) | 142cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com (((uint32_t)block[j+3]) ); 143cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com } 144cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com for (; i < 80; ++i) { 145cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com W[i] = rotate_left(W[i-3] ^ W[i-8] ^ W[i-14] ^ W[i-16], 1); 146cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com //The following is equivelent and speeds up SSE implementations, but slows non-SSE. 147cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com //W[i] = rotate_left(W[i-6] ^ W[i-16] ^ W[i-28] ^ W[i-32], 2); 148cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com } 149cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com 150cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com // Round 1 151cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F1(), A, B, C, D, E, W[ 0], K[0]); 152cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F1(), E, A, B, C, D, W[ 1], K[0]); 153cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F1(), D, E, A, B, C, W[ 2], K[0]); 154cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F1(), C, D, E, A, B, W[ 3], K[0]); 155cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F1(), B, C, D, E, A, W[ 4], K[0]); 156cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F1(), A, B, C, D, E, W[ 5], K[0]); 157cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F1(), E, A, B, C, D, W[ 6], K[0]); 158cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F1(), D, E, A, B, C, W[ 7], K[0]); 159cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F1(), C, D, E, A, B, W[ 8], K[0]); 160cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F1(), B, C, D, E, A, W[ 9], K[0]); 161cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F1(), A, B, C, D, E, W[10], K[0]); 162cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F1(), E, A, B, C, D, W[11], K[0]); 163cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F1(), D, E, A, B, C, W[12], K[0]); 164cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F1(), C, D, E, A, B, W[13], K[0]); 165cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F1(), B, C, D, E, A, W[14], K[0]); 166cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F1(), A, B, C, D, E, W[15], K[0]); 167cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F1(), E, A, B, C, D, W[16], K[0]); 168cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F1(), D, E, A, B, C, W[17], K[0]); 169cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F1(), C, D, E, A, B, W[18], K[0]); 170cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F1(), B, C, D, E, A, W[19], K[0]); 171cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com 172cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com // Round 2 173cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F2(), A, B, C, D, E, W[20], K[1]); 174cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F2(), E, A, B, C, D, W[21], K[1]); 175cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F2(), D, E, A, B, C, W[22], K[1]); 176cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F2(), C, D, E, A, B, W[23], K[1]); 177cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F2(), B, C, D, E, A, W[24], K[1]); 178cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F2(), A, B, C, D, E, W[25], K[1]); 179cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F2(), E, A, B, C, D, W[26], K[1]); 180cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F2(), D, E, A, B, C, W[27], K[1]); 181cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F2(), C, D, E, A, B, W[28], K[1]); 182cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F2(), B, C, D, E, A, W[29], K[1]); 183cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F2(), A, B, C, D, E, W[30], K[1]); 184cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F2(), E, A, B, C, D, W[31], K[1]); 185cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F2(), D, E, A, B, C, W[32], K[1]); 186cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F2(), C, D, E, A, B, W[33], K[1]); 187cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F2(), B, C, D, E, A, W[34], K[1]); 188cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F2(), A, B, C, D, E, W[35], K[1]); 189cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F2(), E, A, B, C, D, W[36], K[1]); 190cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F2(), D, E, A, B, C, W[37], K[1]); 191cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F2(), C, D, E, A, B, W[38], K[1]); 192cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F2(), B, C, D, E, A, W[39], K[1]); 193cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com 194cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com // Round 3 195cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F3(), A, B, C, D, E, W[40], K[2]); 196cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F3(), E, A, B, C, D, W[41], K[2]); 197cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F3(), D, E, A, B, C, W[42], K[2]); 198cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F3(), C, D, E, A, B, W[43], K[2]); 199cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F3(), B, C, D, E, A, W[44], K[2]); 200cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F3(), A, B, C, D, E, W[45], K[2]); 201cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F3(), E, A, B, C, D, W[46], K[2]); 202cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F3(), D, E, A, B, C, W[47], K[2]); 203cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F3(), C, D, E, A, B, W[48], K[2]); 204cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F3(), B, C, D, E, A, W[49], K[2]); 205cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F3(), A, B, C, D, E, W[50], K[2]); 206cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F3(), E, A, B, C, D, W[51], K[2]); 207cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F3(), D, E, A, B, C, W[52], K[2]); 208cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F3(), C, D, E, A, B, W[53], K[2]); 209cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F3(), B, C, D, E, A, W[54], K[2]); 210cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F3(), A, B, C, D, E, W[55], K[2]); 211cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F3(), E, A, B, C, D, W[56], K[2]); 212cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F3(), D, E, A, B, C, W[57], K[2]); 213cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F3(), C, D, E, A, B, W[58], K[2]); 214cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F3(), B, C, D, E, A, W[59], K[2]); 215cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com 216cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com // Round 4 217cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F2(), A, B, C, D, E, W[60], K[3]); 218cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F2(), E, A, B, C, D, W[61], K[3]); 219cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F2(), D, E, A, B, C, W[62], K[3]); 220cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F2(), C, D, E, A, B, W[63], K[3]); 221cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F2(), B, C, D, E, A, W[64], K[3]); 222cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F2(), A, B, C, D, E, W[65], K[3]); 223cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F2(), E, A, B, C, D, W[66], K[3]); 224cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F2(), D, E, A, B, C, W[67], K[3]); 225cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F2(), C, D, E, A, B, W[68], K[3]); 226cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F2(), B, C, D, E, A, W[69], K[3]); 227cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F2(), A, B, C, D, E, W[70], K[3]); 228cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F2(), E, A, B, C, D, W[71], K[3]); 229cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F2(), D, E, A, B, C, W[72], K[3]); 230cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F2(), C, D, E, A, B, W[73], K[3]); 231cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F2(), B, C, D, E, A, W[74], K[3]); 232cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F2(), A, B, C, D, E, W[75], K[3]); 233cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F2(), E, A, B, C, D, W[76], K[3]); 234cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F2(), D, E, A, B, C, W[77], K[3]); 235cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F2(), C, D, E, A, B, W[78], K[3]); 236cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com operation(F2(), B, C, D, E, A, W[79], K[3]); 237cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com 238cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com state[0] += A; 239cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com state[1] += B; 240cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com state[2] += C; 241cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com state[3] += D; 242cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com state[4] += E; 243cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com 244cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com#if defined(SK_SHA1_CLEAR_DATA) 245cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com // Clear sensitive information. 246cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com memset(W, 0, sizeof(W)); 247cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com#endif 248cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com} 249cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com 250cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.comstatic void encode(uint8_t output[20], const uint32_t input[5]) { 251cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com for (size_t i = 0, j = 0; i < 5; i++, j += 4) { 252cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com output[j ] = (uint8_t)((input[i] >> 24) & 0xff); 253cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com output[j+1] = (uint8_t)((input[i] >> 16) & 0xff); 254cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com output[j+2] = (uint8_t)((input[i] >> 8) & 0xff); 255cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com output[j+3] = (uint8_t)((input[i] ) & 0xff); 256cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com } 257cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com} 258cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com 259cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.comstatic void encode(uint8_t output[8], const uint64_t input) { 260cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com output[0] = (uint8_t)((input >> 56) & 0xff); 261cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com output[1] = (uint8_t)((input >> 48) & 0xff); 262cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com output[2] = (uint8_t)((input >> 40) & 0xff); 263cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com output[3] = (uint8_t)((input >> 32) & 0xff); 264cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com output[4] = (uint8_t)((input >> 24) & 0xff); 265cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com output[5] = (uint8_t)((input >> 16) & 0xff); 266cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com output[6] = (uint8_t)((input >> 8) & 0xff); 267cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com output[7] = (uint8_t)((input ) & 0xff); 268cfcb1bef94a8cfb565d9450b38f57d4f5c83790abungeman@google.com} 269