| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * The SHA256 hash function |
| 4 | * |
| 5 | * (c) 2020 Mark Wooding |
| 6 | */ |
| 7 | |
| 8 | /*----- Licensing notice --------------------------------------------------* |
| 9 | * |
| 10 | * This file is part of Runlisp, a tool for invoking Common Lisp scripts. |
| 11 | * |
| 12 | * Runlisp is free software: you can redistribute it and/or modify it |
| 13 | * under the terms of the GNU General Public License as published by the |
| 14 | * Free Software Foundation; either version 3 of the License, or (at your |
| 15 | * option) any later version. |
| 16 | * |
| 17 | * Runlisp is distributed in the hope that it will be useful, but WITHOUT |
| 18 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 19 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 20 | * for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License |
| 23 | * along with Runlisp. If not, see <https://www.gnu.org/licenses/>. |
| 24 | */ |
| 25 | |
| 26 | #ifndef SHA256_H |
| 27 | #define SHA256_H |
| 28 | |
| 29 | #ifdef __cplusplus |
| 30 | extern "C" { |
| 31 | #endif |
| 32 | |
| 33 | /*----- Header files ------------------------------------------------------*/ |
| 34 | |
| 35 | #include <limits.h> |
| 36 | |
| 37 | /*----- Data types --------------------------------------------------------*/ |
| 38 | |
| 39 | /* Select a suitable type for 32-bit words. */ |
| 40 | #if UINT_MAX >= 0xffffffff |
| 41 | typedef unsigned u32; |
| 42 | #else |
| 43 | typedef unsigned long u32; |
| 44 | #endif |
| 45 | |
| 46 | #define SHA256_BLKSZ 64 /* input block size in bytes */ |
| 47 | #define SHA256_HASHSZ 32 /* output hash size in bytes */ |
| 48 | |
| 49 | struct sha256_state { |
| 50 | unsigned n; /* number of live bytes in buffer */ |
| 51 | size_t nblk; /* number of blocks hashed so far */ |
| 52 | u32 a[8]; /* hash state */ |
| 53 | unsigned char buf[SHA256_BLKSZ]; /* input buffer */ |
| 54 | }; |
| 55 | |
| 56 | /*----- Functions provided ------------------------------------------------*/ |
| 57 | |
| 58 | extern void sha256_init(struct sha256_state */**/); |
| 59 | /* Initialize the hash state S. */ |
| 60 | |
| 61 | extern void sha256_hash(struct sha256_state */*s*/, |
| 62 | const void */*m*/, size_t /*sz*/); |
| 63 | /* Append SZ bytes of data starting at M to the hash state S. */ |
| 64 | |
| 65 | extern void sha256_done(struct sha256_state */*s*/, unsigned char */*h*/); |
| 66 | /* Write the final hash of state S to buffer H. */ |
| 67 | |
| 68 | /*----- That's all, folks -------------------------------------------------*/ |
| 69 | |
| 70 | #ifdef __cplusplus |
| 71 | } |
| 72 | #endif |
| 73 | |
| 74 | #endif |