miniroon

Simplistic macaroon-based authorization for Unix systems
git clone https://ccx.te2000.cz/git/miniroon
Log | Files | Refs

hmac_b2s_256.c (1247B)


      1 #include <skalibs/blake2s.h>
      2 
      3 #include "hmac_b2s_256.h"
      4 
      5 void hmac_b2s_256(const bytebuffer key, const bytebuffer msg, const bytebuffer output) {
      6   static const size_t block_size = 64, digest_size = 32;
      7   assert(key.len <= block_size);
      8   assert(output.len == digest_size);
      9   //assert(msg);
     10 
     11   dbg_print_bb1("HMAC key", key);
     12   dbg_print_bb1("HMAC message", msg);
     13   blake2s_ctx hash_ctx;
     14   uint8_t pad[block_size], ihash[digest_size];
     15 
     16   blake2s_init(&hash_ctx, digest_size);
     17   // i_key_pad := block_sized_key xor [0x36 blockSize]   // Inner padded key
     18   for(size_t i=0; i<block_size; i++) {
     19     pad[i] = (i < key.len ? key.data[i] : 0) ^ 0x36;
     20   }
     21   // ihash = hash(i_key_pad || message)
     22   blake2s_update(&hash_ctx, pad, block_size);
     23   blake2s_update(&hash_ctx, msg.data, msg.len);
     24   blake2s_final(&hash_ctx, ihash);
     25 
     26   blake2s_init(&hash_ctx, block_size);
     27   // o_key_pad := block_sized_key xor [0x5c blockSize]   // Outer padded key
     28   for(size_t i=0; i<block_size; i++) {
     29     pad[i] = (i < key.len ? key.data[i] : 0) ^ 0x5c;
     30   }
     31   // ohash = hash(o_key_pad || ihash)
     32   blake2s_update(&hash_ctx, pad, block_size);
     33   blake2s_update(&hash_ctx, ihash, digest_size);
     34   blake2s_final(&hash_ctx, output.data);
     35   dbg_print_bb1("HMAC output", output);
     36 }
     37