| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id$ |
| 4 | * |
| 5 | * Implementation of the SHA-256 hash function |
| 6 | * |
| 7 | * (c) 2000 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 | /*----- Notes on the SHA-256 hash function --------------------------------* |
| 31 | * |
| 32 | * SHA-1 (Secure Hash Algorithm) was designed by the NSA, for use with the |
| 33 | * Digital Signature Algorithm. This is an evolution with a larger output |
| 34 | * size, intended to provide security commensurate with 128-bit AES. At the |
| 35 | * time of writing, SHA-256 is very new, and can't be trusted too far. |
| 36 | */ |
| 37 | |
| 38 | #ifndef CATACOMB_SHA256_H |
| 39 | #define CATACOMB_SHA256_H |
| 40 | #define CATACOMB_SHA224_H |
| 41 | |
| 42 | #ifdef __cplusplus |
| 43 | extern "C" { |
| 44 | #endif |
| 45 | |
| 46 | /*----- Header files ------------------------------------------------------*/ |
| 47 | |
| 48 | #include <mLib/bits.h> |
| 49 | |
| 50 | #ifndef CATACOMB_GHASH_H |
| 51 | # include "ghash.h" |
| 52 | #endif |
| 53 | |
| 54 | /*----- Magic numbers -----------------------------------------------------*/ |
| 55 | |
| 56 | #define SHA256_BUFSZ 64 |
| 57 | #define SHA256_HASHSZ 32 |
| 58 | #define SHA256_STATESZ 32 |
| 59 | |
| 60 | #define SHA224_BUFSZ 64 |
| 61 | #define SHA224_HASHSZ 28 |
| 62 | #define SHA224_STATESZ 32 |
| 63 | |
| 64 | /*----- Data structures ---------------------------------------------------*/ |
| 65 | |
| 66 | typedef struct sha256_ctx { |
| 67 | uint32 a, b, c, d, e, f, g, h; /* Chaining variables */ |
| 68 | uint32 nl, nh; /* Byte count so far */ |
| 69 | unsigned off; /* Offset into buffer */ |
| 70 | octet buf[SHA256_BUFSZ]; /* Accumulation buffer */ |
| 71 | } sha256_ctx, sha224_ctx; |
| 72 | |
| 73 | /*----- Functions provided ------------------------------------------------*/ |
| 74 | |
| 75 | /* --- @sha256_compress@, @sha224_compress@ --- * |
| 76 | * |
| 77 | * Arguments: @sha256_ctx *ctx@ = pointer to context block |
| 78 | * @const void *sbuf@ = pointer to buffer of appropriate size |
| 79 | * |
| 80 | * Returns: --- |
| 81 | * |
| 82 | * Use: SHA-256 compression function. |
| 83 | */ |
| 84 | |
| 85 | extern void sha256_compress(sha256_ctx */*ctx*/, const void */*sbuf*/); |
| 86 | #define sha224_compress sha256_compress |
| 87 | |
| 88 | /* --- @sha256_init@, @sha224_init@ --- * |
| 89 | * |
| 90 | * Arguments: @sha256_ctx *ctx@ = pointer to context block to initialize |
| 91 | * |
| 92 | * Returns: --- |
| 93 | * |
| 94 | * Use: Initializes a context block ready for hashing. |
| 95 | */ |
| 96 | |
| 97 | extern void sha256_init(sha256_ctx */*ctx*/); |
| 98 | extern void sha224_init(sha256_ctx */*ctx*/); |
| 99 | |
| 100 | /* --- @sha256_set@, @sha224_set@ --- * |
| 101 | * |
| 102 | * Arguments: @sha256_ctx *ctx@ = pointer to context block |
| 103 | * @const void *buf@ = pointer to state buffer |
| 104 | * @unsigned long count@ = current count of bytes processed |
| 105 | * |
| 106 | * Returns: --- |
| 107 | * |
| 108 | * Use: Initializes a context block from a given state. This is |
| 109 | * useful in cases where the initial hash state is meant to be |
| 110 | * secret, e.g., for NMAC and HMAC support. |
| 111 | */ |
| 112 | |
| 113 | extern void sha256_set(sha256_ctx */*ctx*/, const void */*buf*/, |
| 114 | unsigned long /*count*/); |
| 115 | #define sha224_set sha256_set |
| 116 | |
| 117 | /* --- @sha256_hash@, @sha224_hash@ --- * |
| 118 | * |
| 119 | * Arguments: @sha256_ctx *ctx@ = pointer to context block |
| 120 | * @const void *buf@ = buffer of data to hash |
| 121 | * @size_t sz@ = size of buffer to hash |
| 122 | * |
| 123 | * Returns: --- |
| 124 | * |
| 125 | * Use: Hashes a buffer of data. The buffer may be of any size and |
| 126 | * alignment. |
| 127 | */ |
| 128 | |
| 129 | extern void sha256_hash(sha256_ctx */*ctx*/, |
| 130 | const void */*buf*/, size_t /*sz*/); |
| 131 | #define sha224_hash sha256_hash |
| 132 | |
| 133 | /* --- @sha256_done@, @sha224_done@ --- * |
| 134 | * |
| 135 | * Arguments: @sha256_ctx *ctx@ = pointer to context block |
| 136 | * @void *hash@ = pointer to output buffer |
| 137 | * |
| 138 | * Returns: --- |
| 139 | * |
| 140 | * Use: Returns the hash of the data read so far. |
| 141 | */ |
| 142 | |
| 143 | extern void sha256_done(sha256_ctx */*ctx*/, void */*hash*/); |
| 144 | extern void sha224_done(sha256_ctx */*ctx*/, void */*hash*/); |
| 145 | |
| 146 | /* --- @sha256_state@, @sha224_state@ --- * |
| 147 | * |
| 148 | * Arguments: @sha256_ctx *ctx@ = pointer to context |
| 149 | * @void *state@ = pointer to buffer for current state |
| 150 | * |
| 151 | * Returns: Number of bytes written to the hash function so far. |
| 152 | * |
| 153 | * Use: Returns the current state of the hash function such that |
| 154 | * it can be passed to @sha256_set@. |
| 155 | */ |
| 156 | |
| 157 | extern unsigned long sha256_state(sha256_ctx */*ctx*/, void */*state*/); |
| 158 | #define sha224_state sha256_state |
| 159 | |
| 160 | /*----- Generic hash interface --------------------------------------------*/ |
| 161 | |
| 162 | extern const gchash sha256; |
| 163 | extern const gchash sha224; |
| 164 | |
| 165 | /*----- That's all, folks -------------------------------------------------*/ |
| 166 | |
| 167 | #ifdef __cplusplus |
| 168 | } |
| 169 | #endif |
| 170 | |
| 171 | #endif |