|
| 1 | +/* |
| 2 | + * Copyright (c) 2015, Cameron Rich |
| 3 | + * |
| 4 | + * All rights reserved. |
| 5 | + * |
| 6 | + * Redistribution and use in source and binary forms, with or without |
| 7 | + * modification, are permitted provided that the following conditions are met: |
| 8 | + * |
| 9 | + * * Redistributions of source code must retain the above copyright notice, |
| 10 | + * this list of conditions and the following disclaimer. |
| 11 | + * * Redistributions in binary form must reproduce the above copyright notice, |
| 12 | + * this list of conditions and the following disclaimer in the documentation |
| 13 | + * and/or other materials provided with the distribution. |
| 14 | + * * Neither the name of the axTLS project nor the names of its contributors |
| 15 | + * may be used to endorse or promote products derived from this software |
| 16 | + * without specific prior written permission. |
| 17 | + * |
| 18 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 22 | + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 23 | + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 24 | + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 25 | + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 26 | + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 27 | + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 28 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | + */ |
| 30 | + |
| 31 | +#include <string.h> |
| 32 | +#include "os_port.h" |
| 33 | +#include "crypto.h" |
| 34 | + |
| 35 | +#define SHR64(a, n) ((a) >> (n)) |
| 36 | +#define ROR64(a, n) (((a) >> (n)) | ((a) << (64 - (n)))) |
| 37 | +#define CH(x, y, z) (((x) & (y)) | (~(x) & (z))) |
| 38 | +#define MAJ(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z))) |
| 39 | +#define SIGMA1(x) (ROR64(x, 28) ^ ROR64(x, 34) ^ ROR64(x, 39)) |
| 40 | +#define SIGMA2(x) (ROR64(x, 14) ^ ROR64(x, 18) ^ ROR64(x, 41)) |
| 41 | +#define SIGMA3(x) (ROR64(x, 1) ^ ROR64(x, 8) ^ SHR64(x, 7)) |
| 42 | +#define SIGMA4(x) (ROR64(x, 19) ^ ROR64(x, 61) ^ SHR64(x, 6)) |
| 43 | +#define MIN(x, y) ((x) < (y) ? x : y) |
| 44 | + |
| 45 | +static const uint8_t padding[128] = |
| 46 | +{ |
| 47 | + 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 48 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 49 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 50 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 51 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 52 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 53 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 54 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 |
| 55 | +}; |
| 56 | + |
| 57 | +static const uint64_t k[80] = |
| 58 | +{ |
| 59 | + 0x428A2F98D728AE22, 0x7137449123EF65CD, 0xB5C0FBCFEC4D3B2F, 0xE9B5DBA58189DBBC, |
| 60 | + 0x3956C25BF348B538, 0x59F111F1B605D019, 0x923F82A4AF194F9B, 0xAB1C5ED5DA6D8118, |
| 61 | + 0xD807AA98A3030242, 0x12835B0145706FBE, 0x243185BE4EE4B28C, 0x550C7DC3D5FFB4E2, |
| 62 | + 0x72BE5D74F27B896F, 0x80DEB1FE3B1696B1, 0x9BDC06A725C71235, 0xC19BF174CF692694, |
| 63 | + 0xE49B69C19EF14AD2, 0xEFBE4786384F25E3, 0x0FC19DC68B8CD5B5, 0x240CA1CC77AC9C65, |
| 64 | + 0x2DE92C6F592B0275, 0x4A7484AA6EA6E483, 0x5CB0A9DCBD41FBD4, 0x76F988DA831153B5, |
| 65 | + 0x983E5152EE66DFAB, 0xA831C66D2DB43210, 0xB00327C898FB213F, 0xBF597FC7BEEF0EE4, |
| 66 | + 0xC6E00BF33DA88FC2, 0xD5A79147930AA725, 0x06CA6351E003826F, 0x142929670A0E6E70, |
| 67 | + 0x27B70A8546D22FFC, 0x2E1B21385C26C926, 0x4D2C6DFC5AC42AED, 0x53380D139D95B3DF, |
| 68 | + 0x650A73548BAF63DE, 0x766A0ABB3C77B2A8, 0x81C2C92E47EDAEE6, 0x92722C851482353B, |
| 69 | + 0xA2BFE8A14CF10364, 0xA81A664BBC423001, 0xC24B8B70D0F89791, 0xC76C51A30654BE30, |
| 70 | + 0xD192E819D6EF5218, 0xD69906245565A910, 0xF40E35855771202A, 0x106AA07032BBD1B8, |
| 71 | + 0x19A4C116B8D2D0C8, 0x1E376C085141AB53, 0x2748774CDF8EEB99, 0x34B0BCB5E19B48A8, |
| 72 | + 0x391C0CB3C5C95A63, 0x4ED8AA4AE3418ACB, 0x5B9CCA4F7763E373, 0x682E6FF3D6B2B8A3, |
| 73 | + 0x748F82EE5DEFB2FC, 0x78A5636F43172F60, 0x84C87814A1F0AB72, 0x8CC702081A6439EC, |
| 74 | + 0x90BEFFFA23631E28, 0xA4506CEBDE82BDE9, 0xBEF9A3F7B2C67915, 0xC67178F2E372532B, |
| 75 | + 0xCA273ECEEA26619C, 0xD186B8C721C0C207, 0xEADA7DD6CDE0EB1E, 0xF57D4F7FEE6ED178, |
| 76 | + 0x06F067AA72176FBA, 0x0A637DC5A2C898A6, 0x113F9804BEF90DAE, 0x1B710B35131C471B, |
| 77 | + 0x28DB77F523047D84, 0x32CAAB7B40C72493, 0x3C9EBE0A15C9BEBC, 0x431D67C49C100D4C, |
| 78 | + 0x4CC5D4BECB3E42B6, 0x597F299CFC657E2A, 0x5FCB6FAB3AD6FAEC, 0x6C44198C4A475817 |
| 79 | +}; |
| 80 | + |
| 81 | +/** |
| 82 | +* Initialize the SHA512 context |
| 83 | +*/ |
| 84 | +void SHA512_Init(SHA512_CTX *ctx) |
| 85 | +{ |
| 86 | + ctx->h[0] = 0x6A09E667F3BCC908; |
| 87 | + ctx->h[1] = 0xBB67AE8584CAA73B; |
| 88 | + ctx->h[2] = 0x3C6EF372FE94F82B; |
| 89 | + ctx->h[3] = 0xA54FF53A5F1D36F1; |
| 90 | + ctx->h[4] = 0x510E527FADE682D1; |
| 91 | + ctx->h[5] = 0x9B05688C2B3E6C1F; |
| 92 | + ctx->h[6] = 0x1F83D9ABFB41BD6B; |
| 93 | + ctx->h[7] = 0x5BE0CD19137E2179; |
| 94 | + ctx->size = 0; |
| 95 | + ctx->totalSize = 0; |
| 96 | +} |
| 97 | + |
| 98 | +static void SHA512_Process(SHA512_CTX *ctx) |
| 99 | +{ |
| 100 | + int t; |
| 101 | + uint64_t temp1; |
| 102 | + uint64_t temp2; |
| 103 | + |
| 104 | + // Initialize the 8 working registers |
| 105 | + uint64_t a = ctx->h[0]; |
| 106 | + uint64_t b = ctx->h[1]; |
| 107 | + uint64_t c = ctx->h[2]; |
| 108 | + uint64_t d = ctx->h[3]; |
| 109 | + uint64_t e = ctx->h[4]; |
| 110 | + uint64_t f = ctx->h[5]; |
| 111 | + uint64_t g = ctx->h[6]; |
| 112 | + uint64_t h = ctx->h[7]; |
| 113 | + |
| 114 | + // Process message in 16-word blocks |
| 115 | + uint64_t *w = ctx->w; |
| 116 | + |
| 117 | + // Convert from big-endian byte order to host byte order |
| 118 | + for (t = 0; t < 16; t++) |
| 119 | + w[t] = be64toh(w[t]); |
| 120 | + |
| 121 | + // Prepare the message schedule |
| 122 | + for (t = 16; t < 80; t++) |
| 123 | + w[t] = SIGMA4(w[t - 2]) + w[t - 7] + SIGMA3(w[t - 15]) + w[t - 16]; |
| 124 | + |
| 125 | + // SHA-512 hash computation |
| 126 | + for (t = 0; t < 80; t++) |
| 127 | + { |
| 128 | + // Calculate T1 and T2 |
| 129 | + temp1 = h + SIGMA2(e) + CH(e, f, g) + k[t] + w[t]; |
| 130 | + temp2 = SIGMA1(a) + MAJ(a, b, c); |
| 131 | + |
| 132 | + // Update the working registers |
| 133 | + h = g; |
| 134 | + g = f; |
| 135 | + f = e; |
| 136 | + e = d + temp1; |
| 137 | + d = c; |
| 138 | + c = b; |
| 139 | + b = a; |
| 140 | + a = temp1 + temp2; |
| 141 | + } |
| 142 | + |
| 143 | + // Update the hash value |
| 144 | + ctx->h[0] += a; |
| 145 | + ctx->h[1] += b; |
| 146 | + ctx->h[2] += c; |
| 147 | + ctx->h[3] += d; |
| 148 | + ctx->h[4] += e; |
| 149 | + ctx->h[5] += f; |
| 150 | + ctx->h[6] += g; |
| 151 | + ctx->h[7] += h; |
| 152 | + } |
| 153 | + |
| 154 | +/** |
| 155 | +* Accepts an array of octets as the next portion of the message. |
| 156 | +*/ |
| 157 | +void SHA512_Update(SHA512_CTX *ctx, const uint8_t * msg, int len) |
| 158 | +{ |
| 159 | + // Process the incoming data |
| 160 | + while (len > 0) |
| 161 | + { |
| 162 | + // The buffer can hold at most 128 bytes |
| 163 | + size_t n = MIN(len, 128 - ctx->size); |
| 164 | + |
| 165 | + // Copy the data to the buffer |
| 166 | + memcpy(ctx->buffer + ctx->size, msg, n); |
| 167 | + |
| 168 | + // Update the SHA-512 ctx |
| 169 | + ctx->size += n; |
| 170 | + ctx->totalSize += n; |
| 171 | + // Advance the data pointer |
| 172 | + msg = (uint8_t *) msg + n; |
| 173 | + // Remaining bytes to process |
| 174 | + len -= n; |
| 175 | + |
| 176 | + // Process message in 16-word blocks |
| 177 | + if (ctx->size == 128) |
| 178 | + { |
| 179 | + // Transform the 16-word block |
| 180 | + SHA512_Process(ctx); |
| 181 | + // Empty the buffer |
| 182 | + ctx->size = 0; |
| 183 | + } |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +/** |
| 188 | +* Return the 512-bit message digest into the user's array |
| 189 | +*/ |
| 190 | +void SHA512_Final(uint8_t *digest, SHA512_CTX *ctx) |
| 191 | +{ |
| 192 | + int i; |
| 193 | + size_t paddingSize; |
| 194 | + uint64_t totalSize; |
| 195 | + |
| 196 | + // Length of the original message (before padding) |
| 197 | + totalSize = ctx->totalSize * 8; |
| 198 | + |
| 199 | + // Pad the message so that its length is congruent to 112 modulo 128 |
| 200 | + paddingSize = (ctx->size < 112) ? (112 - ctx->size) : |
| 201 | + (128 + 112 - ctx->size); |
| 202 | + // Append padding |
| 203 | + SHA512_Update(ctx, padding, paddingSize); |
| 204 | + |
| 205 | + // Append the length of the original message |
| 206 | + ctx->w[14] = 0; |
| 207 | + ctx->w[15] = be64toh(totalSize); |
| 208 | + |
| 209 | + // Calculate the message digest |
| 210 | + SHA512_Process(ctx); |
| 211 | + |
| 212 | + // Convert from host byte order to big-endian byte order |
| 213 | + for (i = 0; i < 8; i++) |
| 214 | + ctx->h[i] = be64toh(ctx->h[i]); |
| 215 | + |
| 216 | + // Copy the resulting digest |
| 217 | + if (digest != NULL) |
| 218 | + memcpy(digest, ctx->digest, SHA512_SIZE); |
| 219 | + } |
| 220 | + |
0 commit comments