nagendra modadugu | 4fae542 | 2016-05-10 16:11:54 -0700 | [diff] [blame] | 1 | // Copyright 2016 Google Inc. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | #include "cryptoc/sha256.h" |
| 15 | |
| 16 | #include <string.h> |
| 17 | #include <stdint.h> |
| 18 | |
| 19 | #define ror(value, bits) (((value) >> (bits)) | ((value) << (32 - (bits)))) |
| 20 | #define shr(value, bits) ((value) >> (bits)) |
| 21 | |
| 22 | static const uint32_t K[64] = { |
| 23 | 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, |
| 24 | 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, |
| 25 | 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, |
| 26 | 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, |
| 27 | 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, |
| 28 | 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, |
| 29 | 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, |
| 30 | 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, |
| 31 | 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, |
| 32 | 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, |
| 33 | 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, |
| 34 | 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, |
| 35 | 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, |
| 36 | 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, |
| 37 | 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, |
| 38 | 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 }; |
| 39 | |
| 40 | static void SHA256_Transform(LITE_SHA256_CTX* ctx) { |
| 41 | uint32_t W[64]; |
| 42 | uint32_t A, B, C, D, E, F, G, H; |
| 43 | uint8_t* p = ctx->buf; |
| 44 | int t; |
| 45 | |
| 46 | for(t = 0; t < 16; ++t) { |
| 47 | uint32_t tmp = *p++ << 24; |
| 48 | tmp |= *p++ << 16; |
| 49 | tmp |= *p++ << 8; |
| 50 | tmp |= *p++; |
| 51 | W[t] = tmp; |
| 52 | } |
| 53 | |
| 54 | for(; t < 64; t++) { |
| 55 | uint32_t s0 = ror(W[t-15], 7) ^ ror(W[t-15], 18) ^ shr(W[t-15], 3); |
| 56 | uint32_t s1 = ror(W[t-2], 17) ^ ror(W[t-2], 19) ^ shr(W[t-2], 10); |
| 57 | W[t] = W[t-16] + s0 + W[t-7] + s1; |
| 58 | } |
| 59 | |
| 60 | A = ctx->state[0]; |
| 61 | B = ctx->state[1]; |
| 62 | C = ctx->state[2]; |
| 63 | D = ctx->state[3]; |
| 64 | E = ctx->state[4]; |
| 65 | F = ctx->state[5]; |
| 66 | G = ctx->state[6]; |
| 67 | H = ctx->state[7]; |
| 68 | |
| 69 | for(t = 0; t < 64; t++) { |
| 70 | uint32_t s0 = ror(A, 2) ^ ror(A, 13) ^ ror(A, 22); |
| 71 | uint32_t maj = (A & B) ^ (A & C) ^ (B & C); |
| 72 | uint32_t t2 = s0 + maj; |
| 73 | uint32_t s1 = ror(E, 6) ^ ror(E, 11) ^ ror(E, 25); |
| 74 | uint32_t ch = (E & F) ^ ((~E) & G); |
| 75 | uint32_t t1 = H + s1 + ch + K[t] + W[t]; |
| 76 | |
| 77 | H = G; |
| 78 | G = F; |
| 79 | F = E; |
| 80 | E = D + t1; |
| 81 | D = C; |
| 82 | C = B; |
| 83 | B = A; |
| 84 | A = t1 + t2; |
| 85 | } |
| 86 | |
| 87 | ctx->state[0] += A; |
| 88 | ctx->state[1] += B; |
| 89 | ctx->state[2] += C; |
| 90 | ctx->state[3] += D; |
| 91 | ctx->state[4] += E; |
| 92 | ctx->state[5] += F; |
| 93 | ctx->state[6] += G; |
| 94 | ctx->state[7] += H; |
| 95 | } |
| 96 | |
| 97 | static const HASH_VTAB SHA256_VTAB = { |
| 98 | SHA256_init, |
| 99 | SHA256_update, |
| 100 | SHA256_final, |
| 101 | SHA256_hash, |
| 102 | SHA256_DIGEST_SIZE |
| 103 | }; |
| 104 | |
| 105 | void SHA256_init(LITE_SHA256_CTX* ctx) { |
| 106 | ctx->f = &SHA256_VTAB; |
| 107 | ctx->state[0] = 0x6a09e667; |
| 108 | ctx->state[1] = 0xbb67ae85; |
| 109 | ctx->state[2] = 0x3c6ef372; |
| 110 | ctx->state[3] = 0xa54ff53a; |
| 111 | ctx->state[4] = 0x510e527f; |
| 112 | ctx->state[5] = 0x9b05688c; |
| 113 | ctx->state[6] = 0x1f83d9ab; |
| 114 | ctx->state[7] = 0x5be0cd19; |
| 115 | ctx->count = 0; |
| 116 | } |
| 117 | |
| 118 | |
| 119 | void SHA256_update(LITE_SHA256_CTX* ctx, const void* data, unsigned int len) { |
| 120 | int i = (int) (ctx->count & 63); |
| 121 | const uint8_t* p = (const uint8_t*)data; |
| 122 | |
| 123 | ctx->count += len; |
| 124 | |
| 125 | while (len--) { |
| 126 | ctx->buf[i++] = *p++; |
| 127 | if (i == 64) { |
| 128 | SHA256_Transform(ctx); |
| 129 | i = 0; |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | |
| 135 | const uint8_t* SHA256_final(LITE_SHA256_CTX* ctx) { |
| 136 | uint8_t *p = ctx->buf; |
| 137 | uint64_t cnt = ctx->count * 8; |
| 138 | int i; |
| 139 | |
| 140 | SHA256_update(ctx, (uint8_t*)"\x80", 1); |
| 141 | while ((ctx->count & 63) != 56) { |
| 142 | SHA256_update(ctx, (uint8_t*)"\0", 1); |
| 143 | } |
| 144 | for (i = 0; i < 8; ++i) { |
| 145 | uint8_t tmp = (uint8_t) (cnt >> 56); |
| 146 | cnt <<= 8; |
| 147 | SHA256_update(ctx, &tmp, 1); |
| 148 | } |
| 149 | |
| 150 | for (i = 0; i < 8; i++) { |
| 151 | uint32_t tmp = ctx->state[i]; |
| 152 | *p++ = (uint8_t)(tmp >> 24); |
| 153 | *p++ = (uint8_t)(tmp >> 16); |
| 154 | *p++ = (uint8_t)(tmp >> 8); |
| 155 | *p++ = (uint8_t)(tmp >> 0); |
| 156 | } |
| 157 | |
| 158 | return ctx->buf; |
| 159 | } |
| 160 | |
| 161 | /* Convenience function */ |
| 162 | const uint8_t* SHA256_hash(const void* data, unsigned int len, |
| 163 | uint8_t* digest) { |
| 164 | LITE_SHA256_CTX ctx; |
| 165 | SHA256_init(&ctx); |
| 166 | SHA256_update(&ctx, data, len); |
| 167 | memcpy(digest, SHA256_final(&ctx), SHA256_DIGEST_SIZE); |
| 168 | return digest; |
| 169 | } |