| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * Implementation of the SHA-1 hash function |
| 4 | * |
| 5 | * (c) 1999 Straylight/Edgeware |
| 6 | */ |
| 7 | |
| 8 | /*----- Licensing notice --------------------------------------------------* |
| 9 | * |
| 10 | * This file is part of Catacomb. |
| 11 | * |
| 12 | * Catacomb is free software; you can redistribute it and/or modify |
| 13 | * it under the terms of the GNU Library General Public License as |
| 14 | * published by the Free Software Foundation; either version 2 of the |
| 15 | * License, or (at your option) any later version. |
| 16 | * |
| 17 | * Catacomb is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | * GNU Library General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU Library General Public |
| 23 | * License along with Catacomb; if not, write to the Free |
| 24 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
| 25 | * MA 02111-1307, USA. |
| 26 | */ |
| 27 | |
| 28 | /*----- Notes on the SHA-1 hash function ----------------------------------* |
| 29 | * |
| 30 | * SHA (Secure Hash Algorithm) was designed by the NSA, for use with the |
| 31 | * Digital Signature Algorithm. It is defined by FIPS 180-1. It has gained |
| 32 | * wide acceptance since its initial publication, and is probably now most |
| 33 | * people's collision-resistant function of choice. The author prefers |
| 34 | * RIPEMD-160, for no particularly good reasons. |
| 35 | */ |
| 36 | |
| 37 | #ifndef CATACOMB_SHA_H |
| 38 | #define CATACOMB_SHA_H |
| 39 | |
| 40 | #ifdef __cplusplus |
| 41 | extern "C" { |
| 42 | #endif |
| 43 | |
| 44 | /*----- Header files ------------------------------------------------------*/ |
| 45 | |
| 46 | #include <mLib/bits.h> |
| 47 | |
| 48 | #ifndef CATACOMB_GHASH_H |
| 49 | # include "ghash.h" |
| 50 | #endif |
| 51 | |
| 52 | /*----- Magic numbers -----------------------------------------------------*/ |
| 53 | |
| 54 | #define SHA_BUFSZ 64 |
| 55 | #define SHA_HASHSZ 20 |
| 56 | #define SHA_STATESZ 20 |
| 57 | |
| 58 | /*----- Data structures ---------------------------------------------------*/ |
| 59 | |
| 60 | typedef struct sha_ctx { |
| 61 | uint32 a, b, c, d, e; /* Chaining variables */ |
| 62 | uint32 nl, nh; /* Byte count so far */ |
| 63 | unsigned off; /* Offset into buffer */ |
| 64 | octet buf[SHA_BUFSZ]; /* Accumulation buffer */ |
| 65 | } sha_ctx; |
| 66 | |
| 67 | /*----- Functions provided ------------------------------------------------*/ |
| 68 | |
| 69 | /* --- @sha_compress@ --- * |
| 70 | * |
| 71 | * Arguments: @sha_ctx *ctx@ = pointer to context block |
| 72 | * @const void *sbuf@ = pointer to buffer of appropriate size |
| 73 | * |
| 74 | * Returns: --- |
| 75 | * |
| 76 | * Use: SHA compression function. |
| 77 | */ |
| 78 | |
| 79 | extern void sha_compress(sha_ctx */*ctx*/, const void */*sbuf*/); |
| 80 | |
| 81 | /* --- @sha_init@ --- * |
| 82 | * |
| 83 | * Arguments: @sha_ctx *ctx@ = pointer to context block to initialize |
| 84 | * |
| 85 | * Returns: --- |
| 86 | * |
| 87 | * Use: Initializes a context block ready for hashing. |
| 88 | */ |
| 89 | |
| 90 | extern void sha_init(sha_ctx */*ctx*/); |
| 91 | |
| 92 | /* --- @sha_set@ --- * |
| 93 | * |
| 94 | * Arguments: @sha_ctx *ctx@ = pointer to context block |
| 95 | * @const void *buf@ = pointer to state buffer |
| 96 | * @unsigned long count@ = current count of bytes processed |
| 97 | * |
| 98 | * Returns: --- |
| 99 | * |
| 100 | * Use: Initializes a context block from a given state. This is |
| 101 | * useful in cases where the initial hash state is meant to be |
| 102 | * secret, e.g., for NMAC and HMAC support. |
| 103 | */ |
| 104 | |
| 105 | extern void sha_set(sha_ctx */*ctx*/, const void */*buf*/, |
| 106 | unsigned long /*count*/); |
| 107 | |
| 108 | /* --- @sha_hash@ --- * |
| 109 | * |
| 110 | * Arguments: @sha_ctx *ctx@ = pointer to context block |
| 111 | * @const void *buf@ = buffer of data to hash |
| 112 | * @size_t sz@ = size of buffer to hash |
| 113 | * |
| 114 | * Returns: --- |
| 115 | * |
| 116 | * Use: Hashes a buffer of data. The buffer may be of any size and |
| 117 | * alignment. |
| 118 | */ |
| 119 | |
| 120 | extern void sha_hash(sha_ctx */*ctx*/, const void */*buf*/, size_t /*sz*/); |
| 121 | |
| 122 | /* --- @sha_done@ --- * |
| 123 | * |
| 124 | * Arguments: @sha_ctx *ctx@ = pointer to context block |
| 125 | * @void *hash@ = pointer to output buffer |
| 126 | * |
| 127 | * Returns: --- |
| 128 | * |
| 129 | * Use: Returns the hash of the data read so far. |
| 130 | */ |
| 131 | |
| 132 | extern void sha_done(sha_ctx */*ctx*/, void */*hash*/); |
| 133 | |
| 134 | /* --- @sha_state@ --- * |
| 135 | * |
| 136 | * Arguments: @sha_ctx *ctx@ = pointer to context |
| 137 | * @void *state@ = pointer to buffer for current state |
| 138 | * |
| 139 | * Returns: Number of bytes written to the hash function so far. |
| 140 | * |
| 141 | * Use: Returns the current state of the hash function such that |
| 142 | * it can be passed to @sha_set@. |
| 143 | */ |
| 144 | |
| 145 | extern unsigned long sha_state(sha_ctx */*ctx*/, void */*state*/); |
| 146 | |
| 147 | /*----- Generic hash interface --------------------------------------------*/ |
| 148 | |
| 149 | extern const gchash sha; |
| 150 | |
| 151 | /*----- That's all, folks -------------------------------------------------*/ |
| 152 | |
| 153 | #ifdef __cplusplus |
| 154 | } |
| 155 | #endif |
| 156 | |
| 157 | #endif |