hmac.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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,
  28. const uint32_t keylen) {
  29. static CONFIDENTIAL uint8_t i_key_pad[SHA256_BLOCK_LENGTH];
  30. memzero(i_key_pad, SHA256_BLOCK_LENGTH);
  31. if (keylen > SHA256_BLOCK_LENGTH) {
  32. sha256_Raw(key, keylen, i_key_pad);
  33. } else {
  34. memcpy(i_key_pad, key, keylen);
  35. }
  36. for (int i = 0; i < SHA256_BLOCK_LENGTH; i++) {
  37. hctx->o_key_pad[i] = i_key_pad[i] ^ 0x5c;
  38. i_key_pad[i] ^= 0x36;
  39. }
  40. sha256_Init(&(hctx->ctx));
  41. sha256_Update(&(hctx->ctx), i_key_pad, SHA256_BLOCK_LENGTH);
  42. memzero(i_key_pad, sizeof(i_key_pad));
  43. }
  44. void hmac_sha256_Update(HMAC_SHA256_CTX *hctx, const uint8_t *msg,
  45. const uint32_t msglen) {
  46. sha256_Update(&(hctx->ctx), msg, msglen);
  47. }
  48. void hmac_sha256_Final(HMAC_SHA256_CTX *hctx, uint8_t *hmac) {
  49. sha256_Final(&(hctx->ctx), hmac);
  50. sha256_Init(&(hctx->ctx));
  51. sha256_Update(&(hctx->ctx), hctx->o_key_pad, SHA256_BLOCK_LENGTH);
  52. sha256_Update(&(hctx->ctx), hmac, SHA256_DIGEST_LENGTH);
  53. sha256_Final(&(hctx->ctx), hmac);
  54. memzero(hctx, sizeof(HMAC_SHA256_CTX));
  55. }
  56. void hmac_sha256(const uint8_t *key, const uint32_t keylen, const uint8_t *msg,
  57. const uint32_t msglen, uint8_t *hmac) {
  58. static CONFIDENTIAL HMAC_SHA256_CTX hctx;
  59. hmac_sha256_Init(&hctx, key, keylen);
  60. hmac_sha256_Update(&hctx, msg, msglen);
  61. hmac_sha256_Final(&hctx, hmac);
  62. }
  63. void hmac_sha256_prepare(const uint8_t *key, const uint32_t keylen,
  64. uint32_t *opad_digest, uint32_t *ipad_digest) {
  65. static CONFIDENTIAL uint32_t key_pad[SHA256_BLOCK_LENGTH / sizeof(uint32_t)];
  66. memzero(key_pad, sizeof(key_pad));
  67. if (keylen > SHA256_BLOCK_LENGTH) {
  68. static CONFIDENTIAL SHA256_CTX context;
  69. sha256_Init(&context);
  70. sha256_Update(&context, key, keylen);
  71. sha256_Final(&context, (uint8_t *)key_pad);
  72. } else {
  73. memcpy(key_pad, key, keylen);
  74. }
  75. /* compute o_key_pad and its digest */
  76. for (int i = 0; i < SHA256_BLOCK_LENGTH / (int)sizeof(uint32_t); i++) {
  77. uint32_t data = 0;
  78. #if BYTE_ORDER == LITTLE_ENDIAN
  79. REVERSE32(key_pad[i], data);
  80. #else
  81. data = key_pad[i];
  82. #endif
  83. key_pad[i] = data ^ 0x5c5c5c5c;
  84. }
  85. sha256_Transform(sha256_initial_hash_value, key_pad, opad_digest);
  86. /* convert o_key_pad to i_key_pad and compute its digest */
  87. for (int i = 0; i < SHA256_BLOCK_LENGTH / (int)sizeof(uint32_t); i++) {
  88. key_pad[i] = key_pad[i] ^ 0x5c5c5c5c ^ 0x36363636;
  89. }
  90. sha256_Transform(sha256_initial_hash_value, key_pad, ipad_digest);
  91. memzero(key_pad, sizeof(key_pad));
  92. }
  93. void hmac_sha512_Init(HMAC_SHA512_CTX *hctx, const uint8_t *key,
  94. const uint32_t keylen) {
  95. static CONFIDENTIAL uint8_t i_key_pad[SHA512_BLOCK_LENGTH];
  96. memzero(i_key_pad, SHA512_BLOCK_LENGTH);
  97. if (keylen > SHA512_BLOCK_LENGTH) {
  98. sha512_Raw(key, keylen, i_key_pad);
  99. } else {
  100. memcpy(i_key_pad, key, keylen);
  101. }
  102. for (int i = 0; i < SHA512_BLOCK_LENGTH; i++) {
  103. hctx->o_key_pad[i] = i_key_pad[i] ^ 0x5c;
  104. i_key_pad[i] ^= 0x36;
  105. }
  106. sha512_Init(&(hctx->ctx));
  107. sha512_Update(&(hctx->ctx), i_key_pad, SHA512_BLOCK_LENGTH);
  108. memzero(i_key_pad, sizeof(i_key_pad));
  109. }
  110. void hmac_sha512_Update(HMAC_SHA512_CTX *hctx, const uint8_t *msg,
  111. const uint32_t msglen) {
  112. sha512_Update(&(hctx->ctx), msg, msglen);
  113. }
  114. void hmac_sha512_Final(HMAC_SHA512_CTX *hctx, uint8_t *hmac) {
  115. sha512_Final(&(hctx->ctx), hmac);
  116. sha512_Init(&(hctx->ctx));
  117. sha512_Update(&(hctx->ctx), hctx->o_key_pad, SHA512_BLOCK_LENGTH);
  118. sha512_Update(&(hctx->ctx), hmac, SHA512_DIGEST_LENGTH);
  119. sha512_Final(&(hctx->ctx), hmac);
  120. memzero(hctx, sizeof(HMAC_SHA512_CTX));
  121. }
  122. void hmac_sha512(const uint8_t *key, const uint32_t keylen, const uint8_t *msg,
  123. const uint32_t msglen, uint8_t *hmac) {
  124. HMAC_SHA512_CTX hctx = {0};
  125. hmac_sha512_Init(&hctx, key, keylen);
  126. hmac_sha512_Update(&hctx, msg, msglen);
  127. hmac_sha512_Final(&hctx, hmac);
  128. }
  129. void hmac_sha512_prepare(const uint8_t *key, const uint32_t keylen,
  130. uint64_t *opad_digest, uint64_t *ipad_digest) {
  131. static CONFIDENTIAL uint64_t key_pad[SHA512_BLOCK_LENGTH / sizeof(uint64_t)];
  132. memzero(key_pad, sizeof(key_pad));
  133. if (keylen > SHA512_BLOCK_LENGTH) {
  134. static CONFIDENTIAL SHA512_CTX context;
  135. sha512_Init(&context);
  136. sha512_Update(&context, key, keylen);
  137. sha512_Final(&context, (uint8_t *)key_pad);
  138. } else {
  139. memcpy(key_pad, key, keylen);
  140. }
  141. /* compute o_key_pad and its digest */
  142. for (int i = 0; i < SHA512_BLOCK_LENGTH / (int)sizeof(uint64_t); i++) {
  143. uint64_t data = 0;
  144. #if BYTE_ORDER == LITTLE_ENDIAN
  145. REVERSE64(key_pad[i], data);
  146. #else
  147. data = key_pad[i];
  148. #endif
  149. key_pad[i] = data ^ 0x5c5c5c5c5c5c5c5c;
  150. }
  151. sha512_Transform(sha512_initial_hash_value, key_pad, opad_digest);
  152. /* convert o_key_pad to i_key_pad and compute its digest */
  153. for (int i = 0; i < SHA512_BLOCK_LENGTH / (int)sizeof(uint64_t); i++) {
  154. key_pad[i] = key_pad[i] ^ 0x5c5c5c5c5c5c5c5c ^ 0x3636363636363636;
  155. }
  156. sha512_Transform(sha512_initial_hash_value, key_pad, ipad_digest);
  157. memzero(key_pad, sizeof(key_pad));
  158. }