1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <string.h>
18 #include <nanohub/sha2.h>
19 
20 
sha2init(struct Sha2state * state)21 void sha2init(struct Sha2state *state)
22 {
23     state->h[0] = 0x6a09e667;
24     state->h[1] = 0xbb67ae85;
25     state->h[2] = 0x3c6ef372;
26     state->h[3] = 0xa54ff53a;
27     state->h[4] = 0x510e527f;
28     state->h[5] = 0x9b05688c;
29     state->h[6] = 0x1f83d9ab;
30     state->h[7] = 0x5be0cd19;
31     state->msgLen = 0;
32     state->bufBytesUsed = 0;
33 }
34 
35 #ifdef ARM
36 
37     #define STRINFIGY2(b) #b
38     #define STRINGIFY(b) STRINFIGY2(b)
39     #define ror(v, b) ({uint32_t ret; if (b) asm("ror %0, #" STRINGIFY(b) :"=r"(ret):"0"(v)); else ret = v; ret;})
40 
41 #else
42 
ror(uint32_t val,uint32_t by)43     inline static uint32_t ror(uint32_t val, uint32_t by)
44     {
45         if (!by)
46             return val;
47 
48         val = (val >> by) | (val << (32 - by));
49 
50         return val;
51     }
52 
53 #endif
54 
55 
sha2processBlock(struct Sha2state * state)56 static void sha2processBlock(struct Sha2state *state)
57 {
58     static const uint32_t k[] = {
59         0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
60         0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
61         0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
62         0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
63         0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
64         0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
65         0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
66         0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
67     };
68     uint32_t i, a, b, c, d, e, f, g, h;
69 
70     //byteswap the input (if we're on a little endian cpu, as we are)
71     for (i = 0; i < SHA2_BLOCK_SIZE / sizeof(uint32_t); i++)
72         state->w[i] = __builtin_bswap32(state->w[i]);
73 
74     //expand input
75     for (;i < SHA2_WORDS_STATE_SIZE; i++) {
76         uint32_t s0 = ror(state->w[i-15], 7) ^ ror(state->w[i-15], 18) ^ (state->w[i-15] >> 3);
77         uint32_t s1 = ror(state->w[i-2], 17) ^ ror(state->w[i-2], 19) ^ (state->w[i-2] >> 10);
78         state->w[i] = state->w[i - 16] + s0 + state->w[i - 7] + s1;
79     }
80 
81     //init working variables
82     a = state->h[0];
83     b = state->h[1];
84     c = state->h[2];
85     d = state->h[3];
86     e = state->h[4];
87     f = state->h[5];
88     g = state->h[6];
89     h = state->h[7];
90 
91     //64 rounds
92     for (i = 0; i < 64; i++) {
93         uint32_t s1 = ror(e, 6) ^ ror(e, 11) ^ ror(e, 25);
94         uint32_t ch = (e & f) ^ ((~e) & g);
95         uint32_t temp1 = h + s1 + ch + k[i] + state->w[i];
96         uint32_t s0 = ror(a, 2) ^ ror(a, 13) ^ ror(a, 22);
97         uint32_t maj = (a & b) ^ (a & c) ^ (b & c);
98         uint32_t temp2 = s0 + maj;
99 
100         h = g;
101         g = f;
102         f = e;
103         e = d + temp1;
104         d = c;
105         c = b;
106         b = a;
107         a = temp1 + temp2;
108     }
109 
110     //put result back into state
111     state->h[0] += a;
112     state->h[1] += b;
113     state->h[2] += c;
114     state->h[3] += d;
115     state->h[4] += e;
116     state->h[5] += f;
117     state->h[6] += g;
118     state->h[7] += h;
119 }
120 
sha2processBytes(struct Sha2state * state,const void * bytes,uint32_t numBytes)121 void sha2processBytes(struct Sha2state *state, const void *bytes, uint32_t numBytes)
122 {
123     const uint8_t *inBytes = (const uint8_t*)bytes;
124 
125     state->msgLen += numBytes;
126     while (numBytes) {
127         uint32_t bytesToCopy;
128 
129         //step 1: copy data into state if there is space & there is data
130         bytesToCopy = numBytes;
131         if (bytesToCopy > SHA2_BLOCK_SIZE - state->bufBytesUsed)
132             bytesToCopy = SHA2_BLOCK_SIZE - state->bufBytesUsed;
133         memcpy(state->b + state->bufBytesUsed, inBytes, bytesToCopy);
134         inBytes += bytesToCopy;
135         numBytes -= bytesToCopy;
136         state->bufBytesUsed += bytesToCopy;
137 
138         //step 2: if there is a full block, process it
139         if (state->bufBytesUsed == SHA2_BLOCK_SIZE) {
140             sha2processBlock(state);
141             state->bufBytesUsed = 0;
142         }
143     }
144 }
145 
sha2finish(struct Sha2state * state)146 const uint32_t* sha2finish(struct Sha2state *state)
147 {
148     uint8_t appendend = 0x80;
149     uint64_t dataLenInBits = state->msgLen * 8;
150     uint32_t i;
151 
152     //append the one
153     sha2processBytes(state, &appendend, 1);
154 
155     //append the zeroes
156     appendend = 0;
157     while (state->bufBytesUsed != 56)
158         sha2processBytes(state, &appendend, 1);
159 
160     //append the length in bits (we can safely write into state since we're sure where to write to (we're definitely 56-bytes into a block)
161     for (i = 0; i < 8; i++, dataLenInBits >>= 8)
162         state->b[63 - i] = dataLenInBits;
163 
164     //process last block
165     sha2processBlock(state);
166 
167     //return pointer to hash
168     return state->h;
169 }
170 
171 
172 
173 
174 
175 
176