hmac_sha256.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * hmac.c - HMAC
  3. *
  4. * Copyright (C) 2017 Sergei Glushchenko
  5. * Author: Sergei Glushchenko <gl.sergei@gmail.com>
  6. *
  7. * This file is a part of U2F firmware for STM32
  8. *
  9. * This program is free software: you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * As additional permission under GNU GPL version 3 section 7, you may
  23. * distribute non-source form of the Program without the copy of the
  24. * GNU GPL normally required by section 4, provided you inform the
  25. * recipients of GNU GPL by a written offer.
  26. *
  27. */
  28. #include <stdint.h>
  29. #include "sha256.h"
  30. #include "hmac_sha256.h"
  31. static void
  32. _hmac_sha256_init (const hmac_context *ctx)
  33. {
  34. hmac_sha256_context *context = (hmac_sha256_context *)ctx;
  35. sha256_start(&context->sha_ctx);
  36. }
  37. static void
  38. _hmac_sha256_update (const hmac_context *ctx, const uint8_t *message,
  39. unsigned message_size)
  40. {
  41. hmac_sha256_context *context = (hmac_sha256_context *)ctx;
  42. sha256_update(&context->sha_ctx, message, message_size);
  43. }
  44. static void
  45. _hmac_sha256_finish (const hmac_context *ctx, uint8_t *hash_result)
  46. {
  47. hmac_sha256_context *context = (hmac_sha256_context *)ctx;
  48. sha256_finish(&context->sha_ctx, hash_result);
  49. }
  50. /* Compute an HMAC using K as a key (as in RFC 6979). Note that K is always
  51. the same size as the hash result size. */
  52. static void
  53. hmac_init(const hmac_context *ctx, const uint8_t *K)
  54. {
  55. uint8_t *pad = ctx->tmp + 2 * ctx->result_size;
  56. unsigned i;
  57. for (i = 0; i < ctx->result_size; ++i)
  58. pad[i] = K[i] ^ 0x36;
  59. for (; i < ctx->block_size; ++i)
  60. pad[i] = 0x36;
  61. ctx->init_hash (ctx);
  62. ctx->update_hash (ctx, pad, ctx->block_size);
  63. }
  64. static void
  65. hmac_update(const hmac_context *ctx,
  66. const uint8_t *message,
  67. unsigned message_size)
  68. {
  69. ctx->update_hash (ctx, message, message_size);
  70. }
  71. static void
  72. hmac_finish(const hmac_context *ctx,
  73. const uint8_t *K,
  74. uint8_t *result)
  75. {
  76. uint8_t *pad = ctx->tmp + 2 * ctx->result_size;
  77. unsigned i;
  78. for (i = 0; i < ctx->result_size; ++i)
  79. pad[i] = K[i] ^ 0x5c;
  80. for (; i < ctx->block_size; ++i)
  81. pad[i] = 0x5c;
  82. ctx->finish_hash (ctx, result);
  83. ctx->init_hash (ctx);
  84. ctx->update_hash (ctx, pad, ctx->block_size);
  85. ctx->update_hash (ctx, result, ctx->result_size);
  86. ctx->finish_hash (ctx, result);
  87. }
  88. void
  89. hmac_sha256_init (hmac_sha256_context *ctx, const uint8_t *K)
  90. {
  91. ctx->hmac_ctx.init_hash = _hmac_sha256_init;
  92. ctx->hmac_ctx.update_hash = _hmac_sha256_update;
  93. ctx->hmac_ctx.finish_hash = _hmac_sha256_finish;
  94. ctx->hmac_ctx.block_size = 64;
  95. ctx->hmac_ctx.result_size = 32;
  96. ctx->hmac_ctx.tmp = ctx->tmp;
  97. hmac_init (&ctx->hmac_ctx, K);
  98. }
  99. void
  100. hmac_sha256_update (const hmac_sha256_context *ctx, const uint8_t *message,
  101. unsigned message_size)
  102. {
  103. hmac_update (&ctx->hmac_ctx, message, message_size);
  104. }
  105. void
  106. hmac_sha256_finish (const hmac_sha256_context *ctx, const uint8_t *K,
  107. uint8_t *hash_result)
  108. {
  109. hmac_finish (&ctx->hmac_ctx, K, hash_result);
  110. }