sha.h (5225B)
1 // https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf 2 3 #ifndef SHA_H 4 #define SHA_H 5 6 #include <limits.h> 7 #include <stdalign.h> 8 #include <stddef.h> 9 #include <stdint.h> 10 11 // sha definitions and constants 12 // =========================================================================== 13 14 #define SHA_ALIGN 64 // align structures to a 64-byte/512-bit boundary 15 16 typedef uint32_t sha256_word_t; 17 18 #define SHA256_WORD_BITS 32 19 #define SHA256_BLOCK_BITS 512 20 #define SHA256_DIGEST_BITS 256 21 #define SHA256_SCHEDULE_BITS 2048 22 23 #define SHA256_BLOCK_WORDS (SHA256_BLOCK_BITS / SHA256_WORD_BITS) 24 #define SHA256_BLOCK_SIZE (SHA256_BLOCK_BITS / CHAR_BIT) 25 #define SHA256_BLOCK_MASK (SHA256_BLOCK_SIZE - 1) 26 27 #define SHA256_DIGEST_WORDS (SHA256_DIGEST_BITS / SHA256_WORD_BITS) 28 #define SHA256_DIGEST_SIZE (SHA256_DIGEST_BITS / CHAR_BIT) 29 30 #define SHA256_SCHEDULE_WORDS (SHA256_SCHEDULE_BITS / SHA256_WORD_BITS) 31 #define SHA256_SCHEDULE_SIZE (SHA256_SCHEDULE_BITS / CHAR_BIT) 32 33 struct sha256_ctx { 34 alignas(SHA_ALIGN) 35 sha256_word_t schedule[SHA256_SCHEDULE_WORDS]; 36 sha256_word_t digest[SHA256_DIGEST_WORDS]; 37 size_t ctr; 38 }; 39 40 // sha224 re-usees the sha256 definitions 41 #define SHA224_DIGEST_BITS 224 42 #define SHA224_DIGEST_WORDS (SHA224_DIGEST_BITS / SHA256_WORD_BITS) 43 #define SHA224_DIGEST_SIZE (SHA224_DIGEST_BITS / CHAR_BIT) 44 45 typedef uint64_t sha512_word_t; 46 47 #define SHA512_WORD_BITS 64 48 #define SHA512_BLOCK_BITS 1024 49 #define SHA512_DIGEST_BITS 512 50 #define SHA512_SCHEDULE_BITS 5120 51 52 #define SHA512_BLOCK_WORDS (SHA512_BLOCK_BITS / SHA512_WORD_BITS) 53 #define SHA512_BLOCK_SIZE (SHA512_BLOCK_BITS / CHAR_BIT) 54 #define SHA512_BLOCK_MASK (SHA512_BLOCK_SIZE - 1) 55 56 #define SHA512_DIGEST_WORDS (SHA512_DIGEST_BITS / SHA512_WORD_BITS) 57 #define SHA512_DIGEST_SIZE (SHA512_DIGEST_BITS / CHAR_BIT) 58 59 #define SHA512_SCHEDULE_WORDS (SHA512_SCHEDULE_BITS / SHA512_WORD_BITS) 60 #define SHA512_SCHEDULE_SIZE (SHA512_SCHEDULE_BITS / CHAR_BIT) 61 62 struct sha512_ctx { 63 alignas(SHA_ALIGN) 64 sha512_word_t schedule[SHA512_SCHEDULE_WORDS]; 65 sha512_word_t block[SHA512_BLOCK_WORDS]; 66 sha512_word_t digest[SHA512_DIGEST_WORDS]; 67 size_t ctr; 68 }; 69 70 // sha384 re-uses the sha512 definitions 71 #define SHA384_DIGEST_BITS 384 72 #define SHA384_DIGEST_WORDS (SHA384_DIGEST_BITS / SHA512_WORD_BITS) 73 #define SHA384_DIGEST_SIZE (SHA384_DIGEST_BITS / CHAR_BIT) 74 75 // sha512/224 re-uses the sha512 definitions 76 #define SHA512_224_DIGEST_BITS 224 77 #define SHA512_224_DIGEST_WORDS (SHA512_224_DIGEST_BITS / SHA512_WORD_BITS) 78 #define SHA512_224_DIGEST_SIZE (SHA512_224_DIGEST_BITS / CHAR_BIT) 79 80 // sha512/256 re-uses the sha512 definitions 81 #define SHA512_256_DIGEST_BITS 256 82 #define SHA512_256_DIGEST_WORDS (SHA512_256_DIGEST_BITS / SHA512_WORD_BITS) 83 #define SHA512_256_DIGEST_SIZE (SHA512_256_DIGEST_BITS / CHAR_BIT) 84 85 // sha256 86 // =========================================================================== 87 88 void 89 sha256_init(struct sha256_ctx *ctx); 90 91 void 92 sha256_push(struct sha256_ctx *ctx, void const *buf, size_t len); 93 94 void 95 sha256_dgst(struct sha256_ctx *ctx, unsigned char digest[static SHA256_DIGEST_SIZE]); 96 97 void 98 sha256(void const *buf, size_t len, unsigned char digest[static SHA256_DIGEST_SIZE]); 99 100 // sha224 101 // =========================================================================== 102 103 void 104 sha224_init(struct sha256_ctx *ctx); 105 106 void 107 sha224_push(struct sha256_ctx *ctx, void const *buf, size_t len); 108 109 void 110 sha224_dgst(struct sha256_ctx *ctx, unsigned char digest[static SHA224_DIGEST_SIZE]); 111 112 void 113 sha224(void const *buf, size_t len, unsigned char digest[static SHA224_DIGEST_SIZE]); 114 115 // sha512 116 // =========================================================================== 117 118 void 119 sha512_init(struct sha512_ctx *ctx); 120 121 void 122 sha512_push(struct sha512_ctx *ctx, void const *buf, size_t len); 123 124 void 125 sha512_dgst(struct sha512_ctx *ctx, unsigned char digest[static SHA512_DIGEST_SIZE]); 126 127 void 128 sha512(void const *buf, size_t len, unsigned char digest[static SHA512_DIGEST_SIZE]); 129 130 // sha384 131 // =========================================================================== 132 133 void 134 sha384_init(struct sha512_ctx *ctx); 135 136 void 137 sha384_push(struct sha512_ctx *ctx, void const *buf, size_t len); 138 139 void 140 sha384_dgst(struct sha512_ctx *ctx, unsigned char digest[static SHA384_DIGEST_SIZE]); 141 142 void 143 sha384(void const *buf, size_t len, unsigned char digest[static SHA384_DIGEST_SIZE]); 144 145 // sha512/224 146 // =========================================================================== 147 148 void 149 sha512_224_init(struct sha512_ctx *ctx); 150 151 void 152 sha512_224_push(struct sha512_ctx *ctx, void const *buf, size_t len); 153 154 void 155 sha512_224_dgst(struct sha512_ctx *ctx, unsigned char digest[static SHA512_224_DIGEST_SIZE]); 156 157 void 158 sha512_224(void const *buf, size_t len, unsigned char digest[static SHA512_224_DIGEST_SIZE]); 159 160 // sha512/256 161 // =========================================================================== 162 163 void 164 sha512_256_init(struct sha512_ctx *ctx); 165 166 void 167 sha512_256_push(struct sha512_ctx *ctx, void const *buf, size_t len); 168 169 void 170 sha512_256_dgst(struct sha512_ctx *ctx, unsigned char digest[static SHA512_256_DIGEST_SIZE]); 171 172 void 173 sha512_256(void const *buf, size_t len, unsigned char digest[static SHA512_256_DIGEST_SIZE]); 174 175 #endif /* SHA_H */