1 2 #include <stddef.h> 3 #include <stdint.h> 4 #include <string.h> 5 6 #include "crypto_auth_hmacsha512.h" 7 #include "crypto_auth_hmacsha512256.h" 8 #include "crypto_hash_sha512.h" 9 #include "crypto_verify_32.h" 10 #include "randombytes.h" 11 #include "utils.h" 12 13 size_t 14 crypto_auth_hmacsha512256_bytes(void) 15 { 16 return crypto_auth_hmacsha512256_BYTES; 17 } 18 19 size_t 20 crypto_auth_hmacsha512256_keybytes(void) 21 { 22 return crypto_auth_hmacsha512256_KEYBYTES; 23 } 24 25 size_t 26 crypto_auth_hmacsha512256_statebytes(void) 27 { 28 return sizeof(crypto_auth_hmacsha512256_state); 29 } 30 31 void 32 crypto_auth_hmacsha512256_keygen( 33 unsigned char k[crypto_auth_hmacsha512256_KEYBYTES]) 34 { 35 randombytes_buf(k, crypto_auth_hmacsha512256_KEYBYTES); 36 } 37 38 int 39 crypto_auth_hmacsha512256_init(crypto_auth_hmacsha512256_state *state, 40 const unsigned char *key, size_t keylen) 41 { 42 return crypto_auth_hmacsha512_init((crypto_auth_hmacsha512_state *) state, 43 key, keylen); 44 } 45 46 int 47 crypto_auth_hmacsha512256_update(crypto_auth_hmacsha512256_state *state, 48 const unsigned char *in, 49 unsigned long long inlen) 50 { 51 return crypto_auth_hmacsha512_update((crypto_auth_hmacsha512_state *) state, 52 in, inlen); 53 } 54 55 int 56 crypto_auth_hmacsha512256_final(crypto_auth_hmacsha512256_state *state, 57 unsigned char *out) 58 { 59 unsigned char out0[64]; 60 61 crypto_auth_hmacsha512_final((crypto_auth_hmacsha512_state *) state, out0); 62 memcpy(out, out0, 32); 63 64 return 0; 65 } 66 67 int 68 crypto_auth_hmacsha512256(unsigned char *out, const unsigned char *in, 69 unsigned long long inlen, const unsigned char *k) 70 { 71 crypto_auth_hmacsha512256_state state; 72 73 crypto_auth_hmacsha512256_init(&state, k, 74 crypto_auth_hmacsha512256_KEYBYTES); 75 crypto_auth_hmacsha512256_update(&state, in, inlen); 76 crypto_auth_hmacsha512256_final(&state, out); 77 78 return 0; 79 } 80 81 int 82 crypto_auth_hmacsha512256_verify(const unsigned char *h, 83 const unsigned char *in, 84 unsigned long long inlen, 85 const unsigned char *k) 86 { 87 unsigned char correct[32]; 88 89 crypto_auth_hmacsha512256(correct, in, inlen, k); 90 91 return crypto_verify_32(h, correct) | (-(h == correct)) | 92 sodium_memcmp(correct, h, 32); 93 } 94