hmac.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /**
  2. * Copyright (c) 2013-2014 Tomas Dzetkulic
  3. * Copyright (c) 2013-2014 Pavol Rusnak
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included
  13. * in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  16. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
  19. * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  20. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. * OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include <string.h>
  24. #include "hmac.h"
  25. #include "memzero.h"
  26. #include "options.h"
  27. void hmac_sha256_Init(HMAC_SHA256_CTX* hctx, const uint8_t* key, const uint32_t keylen) {
  28. static CONFIDENTIAL uint8_t i_key_pad[SHA256_BLOCK_LENGTH];
  29. memzero(i_key_pad, SHA256_BLOCK_LENGTH);
  30. if(keylen > SHA256_BLOCK_LENGTH) {
  31. sha256_Raw(key, keylen, i_key_pad);
  32. } else {
  33. memcpy(i_key_pad, key, keylen);
  34. }
  35. for(int i = 0; i < SHA256_BLOCK_LENGTH; i++) {
  36. hctx->o_key_pad[i] = i_key_pad[i] ^ 0x5c;
  37. i_key_pad[i] ^= 0x36;
  38. }
  39. sha256_Init(&(hctx->ctx));
  40. sha256_Update(&(hctx->ctx), i_key_pad, SHA256_BLOCK_LENGTH);
  41. memzero(i_key_pad, sizeof(i_key_pad));
  42. }
  43. void hmac_sha256_Update(HMAC_SHA256_CTX* hctx, const uint8_t* msg, const uint32_t msglen) {
  44. sha256_Update(&(hctx->ctx), msg, msglen);
  45. }
  46. void hmac_sha256_Final(HMAC_SHA256_CTX* hctx, uint8_t* hmac) {
  47. sha256_Final(&(hctx->ctx), hmac);
  48. sha256_Init(&(hctx->ctx));
  49. sha256_Update(&(hctx->ctx), hctx->o_key_pad, SHA256_BLOCK_LENGTH);
  50. sha256_Update(&(hctx->ctx), hmac, SHA256_DIGEST_LENGTH);
  51. sha256_Final(&(hctx->ctx), hmac);
  52. memzero(hctx, sizeof(HMAC_SHA256_CTX));
  53. }
  54. void hmac_sha256(
  55. const uint8_t* key,
  56. const uint32_t keylen,
  57. const uint8_t* msg,
  58. const uint32_t msglen,
  59. uint8_t* hmac) {
  60. static CONFIDENTIAL HMAC_SHA256_CTX hctx;
  61. hmac_sha256_Init(&hctx, key, keylen);
  62. hmac_sha256_Update(&hctx, msg, msglen);
  63. hmac_sha256_Final(&hctx, hmac);
  64. }
  65. void hmac_sha256_prepare(
  66. const uint8_t* key,
  67. const uint32_t keylen,
  68. uint32_t* opad_digest,
  69. uint32_t* ipad_digest) {
  70. static CONFIDENTIAL uint32_t key_pad[SHA256_BLOCK_LENGTH / sizeof(uint32_t)];
  71. memzero(key_pad, sizeof(key_pad));
  72. if(keylen > SHA256_BLOCK_LENGTH) {
  73. static CONFIDENTIAL SHA256_CTX context;
  74. sha256_Init(&context);
  75. sha256_Update(&context, key, keylen);
  76. sha256_Final(&context, (uint8_t*)key_pad);
  77. } else {
  78. memcpy(key_pad, key, keylen);
  79. }
  80. /* compute o_key_pad and its digest */
  81. for(int i = 0; i < SHA256_BLOCK_LENGTH / (int)sizeof(uint32_t); i++) {
  82. uint32_t data = 0;
  83. #if BYTE_ORDER == LITTLE_ENDIAN
  84. REVERSE32(key_pad[i], data);
  85. #else
  86. data = key_pad[i];
  87. #endif
  88. key_pad[i] = data ^ 0x5c5c5c5c;
  89. }
  90. sha256_Transform(sha256_initial_hash_value, key_pad, opad_digest);
  91. /* convert o_key_pad to i_key_pad and compute its digest */
  92. for(int i = 0; i < SHA256_BLOCK_LENGTH / (int)sizeof(uint32_t); i++) {
  93. key_pad[i] = key_pad[i] ^ 0x5c5c5c5c ^ 0x36363636;
  94. }
  95. sha256_Transform(sha256_initial_hash_value, key_pad, ipad_digest);
  96. memzero(key_pad, sizeof(key_pad));
  97. }
  98. void hmac_sha512_Init(HMAC_SHA512_CTX* hctx, const uint8_t* key, const uint32_t keylen) {
  99. static CONFIDENTIAL uint8_t i_key_pad[SHA512_BLOCK_LENGTH];
  100. memzero(i_key_pad, SHA512_BLOCK_LENGTH);
  101. if(keylen > SHA512_BLOCK_LENGTH) {
  102. sha512_Raw(key, keylen, i_key_pad);
  103. } else {
  104. memcpy(i_key_pad, key, keylen);
  105. }
  106. for(int i = 0; i < SHA512_BLOCK_LENGTH; i++) {
  107. hctx->o_key_pad[i] = i_key_pad[i] ^ 0x5c;
  108. i_key_pad[i] ^= 0x36;
  109. }
  110. sha512_Init(&(hctx->ctx));
  111. sha512_Update(&(hctx->ctx), i_key_pad, SHA512_BLOCK_LENGTH);
  112. memzero(i_key_pad, sizeof(i_key_pad));
  113. }
  114. void hmac_sha512_Update(HMAC_SHA512_CTX* hctx, const uint8_t* msg, const uint32_t msglen) {
  115. sha512_Update(&(hctx->ctx), msg, msglen);
  116. }
  117. void hmac_sha512_Final(HMAC_SHA512_CTX* hctx, uint8_t* hmac) {
  118. sha512_Final(&(hctx->ctx), hmac);
  119. sha512_Init(&(hctx->ctx));
  120. sha512_Update(&(hctx->ctx), hctx->o_key_pad, SHA512_BLOCK_LENGTH);
  121. sha512_Update(&(hctx->ctx), hmac, SHA512_DIGEST_LENGTH);
  122. sha512_Final(&(hctx->ctx), hmac);
  123. memzero(hctx, sizeof(HMAC_SHA512_CTX));
  124. }
  125. void hmac_sha512(
  126. const uint8_t* key,
  127. const uint32_t keylen,
  128. const uint8_t* msg,
  129. const uint32_t msglen,
  130. uint8_t* hmac) {
  131. HMAC_SHA512_CTX hctx = {0};
  132. hmac_sha512_Init(&hctx, key, keylen);
  133. hmac_sha512_Update(&hctx, msg, msglen);
  134. hmac_sha512_Final(&hctx, hmac);
  135. }
  136. void hmac_sha512_prepare(
  137. const uint8_t* key,
  138. const uint32_t keylen,
  139. uint64_t* opad_digest,
  140. uint64_t* ipad_digest) {
  141. static CONFIDENTIAL uint64_t key_pad[SHA512_BLOCK_LENGTH / sizeof(uint64_t)];
  142. memzero(key_pad, sizeof(key_pad));
  143. if(keylen > SHA512_BLOCK_LENGTH) {
  144. static CONFIDENTIAL SHA512_CTX context;
  145. sha512_Init(&context);
  146. sha512_Update(&context, key, keylen);
  147. sha512_Final(&context, (uint8_t*)key_pad);
  148. } else {
  149. memcpy(key_pad, key, keylen);
  150. }
  151. /* compute o_key_pad and its digest */
  152. for(int i = 0; i < SHA512_BLOCK_LENGTH / (int)sizeof(uint64_t); i++) {
  153. uint64_t data = 0;
  154. #if BYTE_ORDER == LITTLE_ENDIAN
  155. REVERSE64(key_pad[i], data);
  156. #else
  157. data = key_pad[i];
  158. #endif
  159. key_pad[i] = data ^ 0x5c5c5c5c5c5c5c5c;
  160. }
  161. sha512_Transform(sha512_initial_hash_value, key_pad, opad_digest);
  162. /* convert o_key_pad to i_key_pad and compute its digest */
  163. for(int i = 0; i < SHA512_BLOCK_LENGTH / (int)sizeof(uint64_t); i++) {
  164. key_pad[i] = key_pad[i] ^ 0x5c5c5c5c5c5c5c5c ^ 0x3636363636363636;
  165. }
  166. sha512_Transform(sha512_initial_hash_value, key_pad, ipad_digest);
  167. memzero(key_pad, sizeof(key_pad));
  168. }