| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: sha.c,v 1.2 1999/12/10 23:20:03 mdw Exp $ |
| 4 | * |
| 5 | * Implementation of the SHA-1 hash function |
| 6 | * |
| 7 | * (c) 1999 Straylight/Edgeware |
| 8 | */ |
| 9 | |
| 10 | /*----- Licensing notice --------------------------------------------------* |
| 11 | * |
| 12 | * This file is part of Catacomb. |
| 13 | * |
| 14 | * Catacomb is free software; you can redistribute it and/or modify |
| 15 | * it under the terms of the GNU Library General Public License as |
| 16 | * published by the Free Software Foundation; either version 2 of the |
| 17 | * License, or (at your option) any later version. |
| 18 | * |
| 19 | * Catacomb is distributed in the hope that it will be useful, |
| 20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | * GNU Library General Public License for more details. |
| 23 | * |
| 24 | * You should have received a copy of the GNU Library General Public |
| 25 | * License along with Catacomb; if not, write to the Free |
| 26 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
| 27 | * MA 02111-1307, USA. |
| 28 | */ |
| 29 | |
| 30 | /*----- Revision history --------------------------------------------------* |
| 31 | * |
| 32 | * $Log: sha.c,v $ |
| 33 | * Revision 1.2 1999/12/10 23:20:03 mdw |
| 34 | * New hash interface requirements. |
| 35 | * |
| 36 | * Revision 1.1 1999/09/03 08:41:12 mdw |
| 37 | * Initial import. |
| 38 | * |
| 39 | */ |
| 40 | |
| 41 | /*----- Header files ------------------------------------------------------*/ |
| 42 | |
| 43 | #include <mLib/bits.h> |
| 44 | |
| 45 | #include "ghash.h" |
| 46 | #include "ghash-def.h" |
| 47 | #include "hash.h" |
| 48 | #include "sha.h" |
| 49 | |
| 50 | /*----- Main code ---------------------------------------------------------*/ |
| 51 | |
| 52 | /* --- @sha_compress@ --- * |
| 53 | * |
| 54 | * Arguments: @sha_ctx *ctx@ = pointer to context block |
| 55 | * @const void *sbuf@ = pointer to buffer of appropriate size |
| 56 | * |
| 57 | * Returns: --- |
| 58 | * |
| 59 | * Use: SHA-1 compression function. |
| 60 | */ |
| 61 | |
| 62 | void sha_compress(sha_ctx *ctx, const void *sbuf) |
| 63 | { |
| 64 | uint32 a, b, c, d, e; |
| 65 | uint32 buf[80]; |
| 66 | |
| 67 | /* --- Fetch the chaining variables --- */ |
| 68 | |
| 69 | a = ctx->a; |
| 70 | b = ctx->b; |
| 71 | c = ctx->c; |
| 72 | d = ctx->d; |
| 73 | e = ctx->e; |
| 74 | |
| 75 | /* --- Fetch and expand the buffer contents --- */ |
| 76 | |
| 77 | { |
| 78 | int i; |
| 79 | const octet *p; |
| 80 | |
| 81 | for (i = 0, p = sbuf; i < 16; i++, p += 4) |
| 82 | buf[i] = LOAD32(p); |
| 83 | for (i = 16; i < 80; i++) { |
| 84 | uint32 x = buf[i - 3] ^ buf[i - 8] ^ buf[i - 14] ^ buf[i - 16]; |
| 85 | buf[i] = ROL32(x, 1); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | /* --- Definitions for round functions --- */ |
| 90 | |
| 91 | #define F(x, y, z) (((x) & (y)) | (~(x) & (z))) |
| 92 | #define G(x, y, z) ((x) ^ (y) ^ (z)) |
| 93 | #define H(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z))) |
| 94 | |
| 95 | #define T(v, w, x, y, z, i, f, k) do { \ |
| 96 | z = ROL32(v, 5) + f(w, x, y) + z + buf[i] + k; \ |
| 97 | w = ROR32(w, 2); \ |
| 98 | } while (0) |
| 99 | |
| 100 | #define FF(v, w, x, y, z, i) T(v, w, x, y, z, i, F, 0x5a827999) |
| 101 | #define GG(v, w, x, y, z, i) T(v, w, x, y, z, i, G, 0x6ed9eba1) |
| 102 | #define HH(v, w, x, y, z, i) T(v, w, x, y, z, i, H, 0x8f1bbcdc) |
| 103 | #define II(v, w, x, y, z, i) T(v, w, x, y, z, i, G, 0xca62c1d6) |
| 104 | |
| 105 | /* --- The main compression function --- */ |
| 106 | |
| 107 | FF(a, b, c, d, e, 0); |
| 108 | FF(e, a, b, c, d, 1); |
| 109 | FF(d, e, a, b, c, 2); |
| 110 | FF(c, d, e, a, b, 3); |
| 111 | FF(b, c, d, e, a, 4); |
| 112 | FF(a, b, c, d, e, 5); |
| 113 | FF(e, a, b, c, d, 6); |
| 114 | FF(d, e, a, b, c, 7); |
| 115 | FF(c, d, e, a, b, 8); |
| 116 | FF(b, c, d, e, a, 9); |
| 117 | FF(a, b, c, d, e, 10); |
| 118 | FF(e, a, b, c, d, 11); |
| 119 | FF(d, e, a, b, c, 12); |
| 120 | FF(c, d, e, a, b, 13); |
| 121 | FF(b, c, d, e, a, 14); |
| 122 | FF(a, b, c, d, e, 15); |
| 123 | FF(e, a, b, c, d, 16); |
| 124 | FF(d, e, a, b, c, 17); |
| 125 | FF(c, d, e, a, b, 18); |
| 126 | FF(b, c, d, e, a, 19); |
| 127 | |
| 128 | GG(a, b, c, d, e, 20); |
| 129 | GG(e, a, b, c, d, 21); |
| 130 | GG(d, e, a, b, c, 22); |
| 131 | GG(c, d, e, a, b, 23); |
| 132 | GG(b, c, d, e, a, 24); |
| 133 | GG(a, b, c, d, e, 25); |
| 134 | GG(e, a, b, c, d, 26); |
| 135 | GG(d, e, a, b, c, 27); |
| 136 | GG(c, d, e, a, b, 28); |
| 137 | GG(b, c, d, e, a, 29); |
| 138 | GG(a, b, c, d, e, 30); |
| 139 | GG(e, a, b, c, d, 31); |
| 140 | GG(d, e, a, b, c, 32); |
| 141 | GG(c, d, e, a, b, 33); |
| 142 | GG(b, c, d, e, a, 34); |
| 143 | GG(a, b, c, d, e, 35); |
| 144 | GG(e, a, b, c, d, 36); |
| 145 | GG(d, e, a, b, c, 37); |
| 146 | GG(c, d, e, a, b, 38); |
| 147 | GG(b, c, d, e, a, 39); |
| 148 | |
| 149 | HH(a, b, c, d, e, 40); |
| 150 | HH(e, a, b, c, d, 41); |
| 151 | HH(d, e, a, b, c, 42); |
| 152 | HH(c, d, e, a, b, 43); |
| 153 | HH(b, c, d, e, a, 44); |
| 154 | HH(a, b, c, d, e, 45); |
| 155 | HH(e, a, b, c, d, 46); |
| 156 | HH(d, e, a, b, c, 47); |
| 157 | HH(c, d, e, a, b, 48); |
| 158 | HH(b, c, d, e, a, 49); |
| 159 | HH(a, b, c, d, e, 50); |
| 160 | HH(e, a, b, c, d, 51); |
| 161 | HH(d, e, a, b, c, 52); |
| 162 | HH(c, d, e, a, b, 53); |
| 163 | HH(b, c, d, e, a, 54); |
| 164 | HH(a, b, c, d, e, 55); |
| 165 | HH(e, a, b, c, d, 56); |
| 166 | HH(d, e, a, b, c, 57); |
| 167 | HH(c, d, e, a, b, 58); |
| 168 | HH(b, c, d, e, a, 59); |
| 169 | |
| 170 | II(a, b, c, d, e, 60); |
| 171 | II(e, a, b, c, d, 61); |
| 172 | II(d, e, a, b, c, 62); |
| 173 | II(c, d, e, a, b, 63); |
| 174 | II(b, c, d, e, a, 64); |
| 175 | II(a, b, c, d, e, 65); |
| 176 | II(e, a, b, c, d, 66); |
| 177 | II(d, e, a, b, c, 67); |
| 178 | II(c, d, e, a, b, 68); |
| 179 | II(b, c, d, e, a, 69); |
| 180 | II(a, b, c, d, e, 70); |
| 181 | II(e, a, b, c, d, 71); |
| 182 | II(d, e, a, b, c, 72); |
| 183 | II(c, d, e, a, b, 73); |
| 184 | II(b, c, d, e, a, 74); |
| 185 | II(a, b, c, d, e, 75); |
| 186 | II(e, a, b, c, d, 76); |
| 187 | II(d, e, a, b, c, 77); |
| 188 | II(c, d, e, a, b, 78); |
| 189 | II(b, c, d, e, a, 79); |
| 190 | |
| 191 | /* --- Update the chaining variables --- */ |
| 192 | |
| 193 | ctx->a += a; |
| 194 | ctx->b += b; |
| 195 | ctx->c += c; |
| 196 | ctx->d += d; |
| 197 | ctx->e += e; |
| 198 | } |
| 199 | |
| 200 | /* --- @sha_init@ --- * |
| 201 | * |
| 202 | * Arguments: @sha_ctx *ctx@ = pointer to context block to initialize |
| 203 | * |
| 204 | * Returns: --- |
| 205 | * |
| 206 | * Use: Initializes a context block ready for hashing. |
| 207 | */ |
| 208 | |
| 209 | void sha_init(sha_ctx *ctx) |
| 210 | { |
| 211 | ctx->a = 0x67452301; |
| 212 | ctx->b = 0xefcdab89; |
| 213 | ctx->c = 0x98badcfe; |
| 214 | ctx->d = 0x10325476; |
| 215 | ctx->e = 0xc3d2e1f0; |
| 216 | ctx->off = 0; |
| 217 | ctx->nl = ctx->nh = 0; |
| 218 | } |
| 219 | |
| 220 | /* --- @sha_set@ --- * |
| 221 | * |
| 222 | * Arguments: @sha_ctx *ctx@ = pointer to context block |
| 223 | * @const void *buf@ = pointer to state buffer |
| 224 | * @unsigned long count@ = current count of bytes processed |
| 225 | * |
| 226 | * Returns: --- |
| 227 | * |
| 228 | * Use: Initializes a context block from a given state. This is |
| 229 | * useful in cases where the initial hash state is meant to be |
| 230 | * secret, e.g., for NMAC and HMAC support. |
| 231 | */ |
| 232 | |
| 233 | void sha_set(sha_ctx *ctx, const void *buf, unsigned long count) |
| 234 | { |
| 235 | const octet *p = buf; |
| 236 | ctx->a = LOAD32(p + 0); |
| 237 | ctx->b = LOAD32(p + 4); |
| 238 | ctx->c = LOAD32(p + 8); |
| 239 | ctx->d = LOAD32(p + 12); |
| 240 | ctx->e = LOAD32(p + 16); |
| 241 | ctx->off = 0; |
| 242 | ctx->nl = U32(count); |
| 243 | ctx->nh = U32((count >> 16) >> 16); |
| 244 | } |
| 245 | |
| 246 | /* --- @sha_hash@ --- * |
| 247 | * |
| 248 | * Arguments: @sha_ctx *ctx@ = pointer to context block |
| 249 | * @const void *buf@ = buffer of data to hash |
| 250 | * @size_t sz@ = size of buffer to hash |
| 251 | * |
| 252 | * Returns: --- |
| 253 | * |
| 254 | * Use: Hashes a buffer of data. The buffer may be of any size and |
| 255 | * alignment. |
| 256 | */ |
| 257 | |
| 258 | void sha_hash(sha_ctx *ctx, const void *buf, size_t sz) |
| 259 | { |
| 260 | HASH_BUFFER(SHA, sha, ctx, buf, sz); |
| 261 | } |
| 262 | |
| 263 | /* --- @sha_done@ --- * |
| 264 | * |
| 265 | * Arguments: @sha_ctx *ctx@ = pointer to context block |
| 266 | * @void *hash@ = pointer to output buffer |
| 267 | * |
| 268 | * Returns: --- |
| 269 | * |
| 270 | * Use: Returns the hash of the data read so far. |
| 271 | */ |
| 272 | |
| 273 | void sha_done(sha_ctx *ctx, void *hash) |
| 274 | { |
| 275 | octet *p = hash; |
| 276 | HASH_PAD(SHA, sha, ctx, 0x80, 0, 8); |
| 277 | STORE32(ctx->buf + SHA_BUFSZ - 8, (ctx->nl >> 29) | (ctx->nh << 3)); |
| 278 | STORE32(ctx->buf + SHA_BUFSZ - 4, ctx->nl << 3); |
| 279 | sha_compress(ctx, ctx->buf); |
| 280 | STORE32(p + 0, ctx->a); |
| 281 | STORE32(p + 4, ctx->b); |
| 282 | STORE32(p + 8, ctx->c); |
| 283 | STORE32(p + 12, ctx->d); |
| 284 | STORE32(p + 16, ctx->e); |
| 285 | } |
| 286 | |
| 287 | /* --- @sha_state@ --- * |
| 288 | * |
| 289 | * Arguments: @sha_ctx *ctx@ = pointer to context |
| 290 | * @void *state@ = pointer to buffer for current state |
| 291 | * |
| 292 | * Returns: Number of bytes written to the hash function so far. |
| 293 | * |
| 294 | * Use: Returns the current state of the hash function such that |
| 295 | * it can be passed to @sha_set@. |
| 296 | */ |
| 297 | |
| 298 | unsigned long sha_state(sha_ctx *ctx, void *state) |
| 299 | { |
| 300 | octet *p = state; |
| 301 | STORE32(p + 0, ctx->a); |
| 302 | STORE32(p + 4, ctx->b); |
| 303 | STORE32(p + 8, ctx->c); |
| 304 | STORE32(p + 12, ctx->d); |
| 305 | STORE32(p + 16, ctx->e); |
| 306 | return (ctx->nl | ((ctx->nh << 16) << 16)); |
| 307 | } |
| 308 | |
| 309 | /* --- Generic interface --- */ |
| 310 | |
| 311 | GHASH_DEF(SHA, sha) |
| 312 | |
| 313 | /* --- Test code --- */ |
| 314 | |
| 315 | HASH_TEST(SHA, sha) |
| 316 | |
| 317 | /*----- That's all, folks -------------------------------------------------*/ |