hmac.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /* hmac.c - TinyCrypt implementation of the HMAC algorithm */
  2. /*
  3. * Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * - Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. *
  11. * - Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * - Neither the name of Intel Corporation nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  23. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29. * POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #include <tinycrypt/hmac.h>
  32. #include <tinycrypt/constants.h>
  33. #include <tinycrypt/utils.h>
  34. static void rekey(uint8_t *key, const uint8_t *new_key, unsigned int key_size)
  35. {
  36. const uint8_t inner_pad = (uint8_t) 0x36;
  37. const uint8_t outer_pad = (uint8_t) 0x5c;
  38. unsigned int i;
  39. for (i = 0; i < key_size; ++i) {
  40. key[i] = inner_pad ^ new_key[i];
  41. key[i + TC_SHA256_BLOCK_SIZE] = outer_pad ^ new_key[i];
  42. }
  43. for (; i < TC_SHA256_BLOCK_SIZE; ++i) {
  44. key[i] = inner_pad; key[i + TC_SHA256_BLOCK_SIZE] = outer_pad;
  45. }
  46. }
  47. int tc_hmac_set_key(TCHmacState_t ctx, const uint8_t *key,
  48. unsigned int key_size)
  49. {
  50. /* input sanity check: */
  51. if (ctx == (TCHmacState_t) 0 ||
  52. key == (const uint8_t *) 0 ||
  53. key_size == 0) {
  54. return TC_CRYPTO_FAIL;
  55. }
  56. const uint8_t dummy_key[key_size];
  57. struct tc_hmac_state_struct dummy_state;
  58. if (key_size <= TC_SHA256_BLOCK_SIZE) {
  59. /*
  60. * The next three lines consist of dummy calls just to avoid
  61. * certain timing attacks. Without these dummy calls,
  62. * adversaries would be able to learn whether the key_size is
  63. * greater than TC_SHA256_BLOCK_SIZE by measuring the time
  64. * consumed in this process.
  65. */
  66. (void)tc_sha256_init(&dummy_state.hash_state);
  67. (void)tc_sha256_update(&dummy_state.hash_state,
  68. dummy_key,
  69. key_size);
  70. (void)tc_sha256_final(&dummy_state.key[TC_SHA256_DIGEST_SIZE],
  71. &dummy_state.hash_state);
  72. /* Actual code for when key_size <= TC_SHA256_BLOCK_SIZE: */
  73. rekey(ctx->key, key, key_size);
  74. } else {
  75. (void)tc_sha256_init(&ctx->hash_state);
  76. (void)tc_sha256_update(&ctx->hash_state, key, key_size);
  77. (void)tc_sha256_final(&ctx->key[TC_SHA256_DIGEST_SIZE],
  78. &ctx->hash_state);
  79. rekey(ctx->key,
  80. &ctx->key[TC_SHA256_DIGEST_SIZE],
  81. TC_SHA256_DIGEST_SIZE);
  82. }
  83. return TC_CRYPTO_SUCCESS;
  84. }
  85. int tc_hmac_init(TCHmacState_t ctx)
  86. {
  87. /* input sanity check: */
  88. if (ctx == (TCHmacState_t) 0) {
  89. return TC_CRYPTO_FAIL;
  90. }
  91. (void) tc_sha256_init(&ctx->hash_state);
  92. (void) tc_sha256_update(&ctx->hash_state, ctx->key, TC_SHA256_BLOCK_SIZE);
  93. return TC_CRYPTO_SUCCESS;
  94. }
  95. int tc_hmac_update(TCHmacState_t ctx,
  96. const void *data,
  97. unsigned int data_length)
  98. {
  99. /* input sanity check: */
  100. if (ctx == (TCHmacState_t) 0) {
  101. return TC_CRYPTO_FAIL;
  102. }
  103. (void)tc_sha256_update(&ctx->hash_state, data, data_length);
  104. return TC_CRYPTO_SUCCESS;
  105. }
  106. int tc_hmac_final(uint8_t *tag, unsigned int taglen, TCHmacState_t ctx)
  107. {
  108. /* input sanity check: */
  109. if (tag == (uint8_t *) 0 ||
  110. taglen != TC_SHA256_DIGEST_SIZE ||
  111. ctx == (TCHmacState_t) 0) {
  112. return TC_CRYPTO_FAIL;
  113. }
  114. (void) tc_sha256_final(tag, &ctx->hash_state);
  115. (void)tc_sha256_init(&ctx->hash_state);
  116. (void)tc_sha256_update(&ctx->hash_state,
  117. &ctx->key[TC_SHA256_BLOCK_SIZE],
  118. TC_SHA256_BLOCK_SIZE);
  119. (void)tc_sha256_update(&ctx->hash_state, tag, TC_SHA256_DIGEST_SIZE);
  120. (void)tc_sha256_final(tag, &ctx->hash_state);
  121. /* destroy the current state */
  122. _set(ctx, 0, sizeof(*ctx));
  123. return TC_CRYPTO_SUCCESS;
  124. }