| 1 | #ifndef _SHA256_H_ |
| 2 | #define _SHA256_H_ |
| 3 | |
| 4 | #include <stddef.h> |
| 5 | #include <stdint.h> |
| 6 | |
| 7 | /* |
| 8 | * Use #defines in order to avoid namespace collisions with anyone else's |
| 9 | * SHA256 code (e.g., the code in OpenSSL). |
| 10 | */ |
| 11 | #define SHA256_Init libcperciva_SHA256_Init |
| 12 | #define SHA256_Update libcperciva_SHA256_Update |
| 13 | #define SHA256_Final libcperciva_SHA256_Final |
| 14 | #define SHA256_Buf libcperciva_SHA256_Buf |
| 15 | #define SHA256_CTX libcperciva_SHA256_CTX |
| 16 | #define HMAC_SHA256_Init libcperciva_HMAC_SHA256_Init |
| 17 | #define HMAC_SHA256_Update libcperciva_HMAC_SHA256_Update |
| 18 | #define HMAC_SHA256_Final libcperciva_HMAC_SHA256_Final |
| 19 | #define HMAC_SHA256_Buf libcperciva_HMAC_SHA256_Buf |
| 20 | #define HMAC_SHA256_CTX libcperciva_HMAC_SHA256_CTX |
| 21 | |
| 22 | /* Context structure for SHA256 operations. */ |
| 23 | typedef struct { |
| 24 | uint32_t state[8]; |
| 25 | uint64_t count; |
| 26 | uint8_t buf[64]; |
| 27 | } SHA256_CTX; |
| 28 | |
| 29 | /** |
| 30 | * SHA256_Init(ctx): |
| 31 | * Initialize the SHA256 context ${ctx}. |
| 32 | */ |
| 33 | void SHA256_Init(SHA256_CTX *); |
| 34 | |
| 35 | /** |
| 36 | * SHA256_Update(ctx, in, len): |
| 37 | * Input ${len} bytes from ${in} into the SHA256 context ${ctx}. |
| 38 | */ |
| 39 | void SHA256_Update(SHA256_CTX *, const void *, size_t); |
| 40 | |
| 41 | /** |
| 42 | * SHA256_Final(digest, ctx): |
| 43 | * Output the SHA256 hash of the data input to the context ${ctx} into the |
| 44 | * buffer ${digest}. |
| 45 | */ |
| 46 | void SHA256_Final(uint8_t[32], SHA256_CTX *); |
| 47 | |
| 48 | /** |
| 49 | * SHA256_Buf(in, len, digest): |
| 50 | * Compute the SHA256 hash of ${len} bytes from ${in} and write it to ${digest}. |
| 51 | */ |
| 52 | void SHA256_Buf(const void *, size_t, uint8_t[32]); |
| 53 | |
| 54 | /* Context structure for HMAC-SHA256 operations. */ |
| 55 | typedef struct { |
| 56 | SHA256_CTX ictx; |
| 57 | SHA256_CTX octx; |
| 58 | } HMAC_SHA256_CTX; |
| 59 | |
| 60 | /** |
| 61 | * HMAC_SHA256_Init(ctx, K, Klen): |
| 62 | * Initialize the HMAC-SHA256 context ${ctx} with ${Klen} bytes of key from |
| 63 | * ${K}. |
| 64 | */ |
| 65 | void HMAC_SHA256_Init(HMAC_SHA256_CTX *, const void *, size_t); |
| 66 | |
| 67 | /** |
| 68 | * HMAC_SHA256_Update(ctx, in, len): |
| 69 | * Input ${len} bytes from ${in} into the HMAC-SHA256 context ${ctx}. |
| 70 | */ |
| 71 | void HMAC_SHA256_Update(HMAC_SHA256_CTX *, const void *, size_t); |
| 72 | |
| 73 | /** |
| 74 | * HMAC_SHA256_Final(digest, ctx): |
| 75 | * Output the HMAC-SHA256 of the data input to the context ${ctx} into the |
| 76 | * buffer ${digest}. |
| 77 | */ |
| 78 | void HMAC_SHA256_Final(uint8_t[32], HMAC_SHA256_CTX *); |
| 79 | |
| 80 | /** |
| 81 | * HMAC_SHA256_Buf(K, Klen, in, len, digest): |
| 82 | * Compute the HMAC-SHA256 of ${len} bytes from ${in} using the key ${K} of |
| 83 | * length ${Klen}, and write the result to ${digest}. |
| 84 | */ |
| 85 | void HMAC_SHA256_Buf(const void *, size_t, const void *, size_t, uint8_t[32]); |
| 86 | |
| 87 | /** |
| 88 | * PBKDF2_SHA256(passwd, passwdlen, salt, saltlen, c, buf, dkLen): |
| 89 | * Compute PBKDF2(passwd, salt, c, dkLen) using HMAC-SHA256 as the PRF, and |
| 90 | * write the output to buf. The value dkLen must be at most 32 * (2^32 - 1). |
| 91 | */ |
| 92 | void PBKDF2_SHA256(const uint8_t *, size_t, const uint8_t *, size_t, |
| 93 | uint64_t, uint8_t *, size_t); |
| 94 | |
| 95 | #endif /* !_SHA256_H_ */ |