sha256.h (1725B)
1 #ifndef SHA256_H__ 2 #define SHA256_H__ 3 4 #include <stdint.h> 5 #include <string.h> 6 7 #define SHA256_MAC_LEN 32 8 9 10 // DBL_INT_ADD treats two unsigned ints a and b as one 64-bit integer and adds c to it 11 #define DBL_INT_ADD(a,b,c) if (a > 0xffffffff - (c)) ++b; a += c; 12 #define ROTLEFT(a,b) (((a) << (b)) | ((a) >> (32-(b)))) 13 #define ROTRIGHT(a,b) (((a) >> (b)) | ((a) << (32-(b)))) 14 15 #define CH(x,y,z) (((x) & (y)) ^ (~(x) & (z))) 16 #define MAJ(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z))) 17 #define EP0(x) (ROTRIGHT(x,2) ^ ROTRIGHT(x,13) ^ ROTRIGHT(x,22)) 18 #define EP1(x) (ROTRIGHT(x,6) ^ ROTRIGHT(x,11) ^ ROTRIGHT(x,25)) 19 #define SIG0(x) (ROTRIGHT(x,7) ^ ROTRIGHT(x,18) ^ ((x) >> 3)) 20 #define SIG1(x) (ROTRIGHT(x,17) ^ ROTRIGHT(x,19) ^ ((x) >> 10)) 21 22 typedef struct { 23 uint8_t data[64]; 24 uint32_t datalen; 25 uint32_t bitlen[2]; 26 uint32_t state[8]; 27 } SHA256_CTX; 28 29 30 void sha256_vector(size_t num_elem, uint8_t *addr[], size_t *len, 31 uint8_t *mac); 32 33 void sha256_transform(SHA256_CTX *ctx, uint8_t data[]); 34 void sha256_init(SHA256_CTX *ctx); 35 void sha256_update(SHA256_CTX *ctx, uint8_t data[], uint32_t len); 36 void sha256_final(SHA256_CTX *ctx, uint8_t hash[]); 37 void hmac_sha256_vector( uint8_t *key, size_t key_len, size_t num_elem, 38 uint8_t *addr[], size_t *len, uint8_t *mac); 39 void hmac_sha256( uint8_t *key, size_t key_len, uint8_t *data, 40 size_t data_len, uint8_t *mac); 41 void sha256_vector(size_t num_elem, uint8_t *addr[], size_t *len, 42 uint8_t *mac); 43 44 uint32_t sha256_32_vector(size_t num_elem, uint8_t *addr[], size_t *len); 45 int sha256_file(const char *file, uint8_t *mac); 46 int sha256_32_file(const char *file, uint32_t *nid); 47 48 #endif