1 2 #include <assert.h> 3 #include <limits.h> 4 #include <stdint.h> 5 6 #include "blake2.h" 7 #include "crypto_generichash_blake2b.h" 8 #include "private/implementations.h" 9 10 int 11 crypto_generichash_blake2b(unsigned char *out, size_t outlen, 12 const unsigned char *in, unsigned long long inlen, 13 const unsigned char *key, size_t keylen) 14 { 15 if (outlen <= 0U || outlen > BLAKE2B_OUTBYTES || 16 keylen > BLAKE2B_KEYBYTES || inlen > UINT64_MAX) { 17 return -1; 18 } 19 assert(outlen <= UINT8_MAX); 20 assert(keylen <= UINT8_MAX); 21 22 return blake2b((uint8_t *) out, in, key, (uint8_t) outlen, (uint64_t) inlen, 23 (uint8_t) keylen); 24 } 25 26 int 27 crypto_generichash_blake2b_salt_personal( 28 unsigned char *out, size_t outlen, const unsigned char *in, 29 unsigned long long inlen, const unsigned char *key, size_t keylen, 30 const unsigned char *salt, const unsigned char *personal) 31 { 32 if (outlen <= 0U || outlen > BLAKE2B_OUTBYTES || 33 keylen > BLAKE2B_KEYBYTES || inlen > UINT64_MAX) { 34 return -1; 35 } 36 assert(outlen <= UINT8_MAX); 37 assert(keylen <= UINT8_MAX); 38 39 return blake2b_salt_personal((uint8_t *) out, in, key, (uint8_t) outlen, 40 (uint64_t) inlen, (uint8_t) keylen, salt, 41 personal); 42 } 43 44 int 45 crypto_generichash_blake2b_init(crypto_generichash_blake2b_state *state, 46 const unsigned char *key, const size_t keylen, 47 const size_t outlen) 48 { 49 if (outlen <= 0U || outlen > BLAKE2B_OUTBYTES || 50 keylen > BLAKE2B_KEYBYTES) { 51 return -1; 52 } 53 assert(outlen <= UINT8_MAX); 54 assert(keylen <= UINT8_MAX); 55 if (key == NULL || keylen <= 0U) { 56 if (blake2b_init(state, (uint8_t) outlen) != 0) { 57 return -1; /* LCOV_EXCL_LINE */ 58 } 59 } else if (blake2b_init_key(state, (uint8_t) outlen, key, 60 (uint8_t) keylen) != 0) { 61 return -1; /* LCOV_EXCL_LINE */ 62 } 63 return 0; 64 } 65 66 int 67 crypto_generichash_blake2b_init_salt_personal( 68 crypto_generichash_blake2b_state *state, const unsigned char *key, 69 const size_t keylen, const size_t outlen, const unsigned char *salt, 70 const unsigned char *personal) 71 { 72 if (outlen <= 0U || outlen > BLAKE2B_OUTBYTES || 73 keylen > BLAKE2B_KEYBYTES) { 74 return -1; 75 } 76 assert(outlen <= UINT8_MAX); 77 assert(keylen <= UINT8_MAX); 78 if (key == NULL || keylen <= 0U) { 79 if (blake2b_init_salt_personal(state, (uint8_t) outlen, salt, 80 personal) != 0) { 81 return -1; /* LCOV_EXCL_LINE */ 82 } 83 } else if (blake2b_init_key_salt_personal(state, (uint8_t) outlen, key, 84 (uint8_t) keylen, salt, 85 personal) != 0) { 86 return -1; /* LCOV_EXCL_LINE */ 87 } 88 return 0; 89 } 90 91 int 92 crypto_generichash_blake2b_update(crypto_generichash_blake2b_state *state, 93 const unsigned char *in, 94 unsigned long long inlen) 95 { 96 return blake2b_update(state, (const uint8_t *) in, (uint64_t) inlen); 97 } 98 99 int 100 crypto_generichash_blake2b_final(crypto_generichash_blake2b_state *state, 101 unsigned char *out, const size_t outlen) 102 { 103 assert(outlen <= UINT8_MAX); 104 return blake2b_final(state, (uint8_t *) out, (uint8_t) outlen); 105 } 106 107 int 108 _crypto_generichash_blake2b_pick_best_implementation(void) 109 { 110 return blake2b_pick_best_implementation(); 111 } 112