1 /* 2 * SHA2-256 implementation based on FIPS180-2. 3 * 4 * Copyright (c) 2009 Marko Kreen 5 * 6 * Permission to use, copy, modify, and/or distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 /** 20 * @file 21 * 22 * SHA256 and SHA224 cryptographic hashes. 23 */ 24 25 #ifndef _USUAL_CRYPTO_SHA256_H_ 26 #define _USUAL_CRYPTO_SHA256_H_ 27 28 #include <usual/base.h> 29 30 /** SHA224 block size in bytes */ 31 #define SHA224_BLOCK_SIZE (16*4) 32 33 /** SHA256 block size in bytes */ 34 #define SHA256_BLOCK_SIZE (16*4) 35 36 /** SHA224 result length in bytes */ 37 #define SHA224_DIGEST_LENGTH (224/8) 38 39 /** SHA256 result length in bytes */ 40 #define SHA256_DIGEST_LENGTH (256/8) 41 42 /** 43 * State structure for both SHA256 and SHA224. 44 */ 45 struct sha256_ctx { 46 union { 47 uint32_t words[16]; 48 uint8_t raw[16 * 4]; 49 } buf; 50 uint32_t state[8]; 51 uint64_t nbytes; 52 }; 53 54 /** Initialize structure for SHA256 */ 55 void sha256_reset(struct sha256_ctx *ctx); 56 57 /** Process more data */ 58 void sha256_update(struct sha256_ctx *ctx, const void *data, unsigned int len); 59 60 /** Calculate final result */ 61 void sha256_final(struct sha256_ctx *ctx, uint8_t *dst); 62 63 /** Initialize structure for SHA224 */ 64 void sha224_reset(struct sha256_ctx *ctx); 65 66 /** Process more data */ 67 void sha224_update(struct sha256_ctx *ctx, const void *data, unsigned int len); 68 69 /** Calculate final result */ 70 void sha224_final(struct sha256_ctx *ctx, uint8_t *dst); 71 72 #endif 73