Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. void hmac_sha256(unsigned char *digest, const unsigned char *in_key, int key_length, const unsigned char *in_message, int message_length) {
  2. SHA256_CTX ctx;
  3. unsigned char key[64] = { 0 };
  4. unsigned char o_key_pad[64] = { 0 };
  5. unsigned char i_key_pad[64] = { 0 };
  6.  
  7. unsigned char* key_pads[] = {o_key_pad, i_key_pad, NULL};
  8. unsigned char** cur_key_pad = key_pads;
  9. unsigned char key_pad_xor = 0x5c;
  10.  
  11. if (key_length > 64) {
  12. sha256_init(&ctx);
  13. sha256_update(&ctx, in_key, key_length);
  14. sha256_final(&ctx, key);
  15. } else {
  16. const unsigned char* cur_key = in_key;
  17. const unsigned char* end_key = in_key + key_length;
  18. unsigned char* out_key = key;
  19. while (cur_key < end_key) {
  20. *(out_key++) = *(cur_key++);
  21. }
  22. }
  23.  
  24. // o_key_pad ← keyxor [0x5c * blockSize] //Outer padded key
  25. // i_key_pad ← keyxor [0x36 * blockSize] //Inner padded key
  26. // key_pad_xor = 0x5c -> xor 0x6a -> 0x36
  27. for (; *cur_key_pad; cur_key_pad++) {
  28. const unsigned char* cur_key = key;
  29. const unsigned char* end_key = key + 64;
  30. unsigned char* out_key = *cur_key_pad;
  31. while (cur_key < end_key) {
  32. *(out_key++) = *(cur_key++) ^ key_pad_xor;
  33. }
  34. key_pad_xor ^= 0x6A;
  35. }
  36.  
  37. sha256_init(&ctx);
  38. sha256_update(&ctx, i_key_pad, 64);
  39. if(message_length)
  40. sha256_update(&ctx, in_message, message_length);
  41. sha256_final(&ctx, key);
  42.  
  43. sha256_init(&ctx);
  44. sha256_update(&ctx, o_key_pad, 64);
  45. sha256_update(&ctx, key, 32);
  46. sha256_final(&ctx, digest);
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement