hfal-hmac.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /* hfal-hmac.c */
  2. /*
  3. This file is part of the ARM-Crypto-Lib.
  4. Copyright (C) 2006-2010 Daniel Otte (daniel.otte@rub.de)
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <crypto/hashfunction_descriptor.h>
  17. #include <crypto/hfal-basic.h>
  18. #include <crypto/hfal-hmac.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #define IPAD 0x36
  22. #define OPAD 0x5C
  23. uint8_t hfal_hmac_init(const hfdesc_t* hash_descriptor,
  24. hfhmacgen_ctx_t* ctx,
  25. const void* key, uint16_t keylength_b){
  26. uint16_t bs = hfal_hash_getBlocksize(hash_descriptor);
  27. uint8_t buffer[bs/8];
  28. uint8_t i;
  29. hf_init_fpt init;
  30. hf_nextBlock_fpt nextBlock;
  31. memset(buffer, 0, bs/8);
  32. ctx->desc = hash_descriptor;
  33. ctx->ctx = malloc(hash_descriptor->ctxsize_B);
  34. ctx->finctx = malloc(hash_descriptor->ctxsize_B);
  35. if(ctx->ctx == NULL && ctx->finctx == NULL)
  36. return 3;
  37. if(ctx->finctx == NULL){
  38. free(ctx->ctx);
  39. return 2;
  40. }
  41. if(ctx->ctx == NULL){
  42. free(ctx->finctx);
  43. return 1;
  44. }
  45. if(keylength_b>bs){
  46. hfal_hash_mem(hash_descriptor, buffer, key, keylength_b);
  47. } else {
  48. memcpy(buffer, key, (keylength_b+7)/8);
  49. }
  50. for(i=0; i<bs/8; ++i){
  51. buffer[i] ^= IPAD;
  52. }
  53. init = hash_descriptor->init;
  54. nextBlock = hash_descriptor->nextBlock;
  55. init(ctx->ctx);
  56. init(ctx->finctx);
  57. nextBlock(ctx->ctx, buffer);
  58. for(i=0; i<bs/8; ++i){
  59. buffer[i] ^= IPAD^OPAD;
  60. }
  61. nextBlock(ctx->finctx, buffer);
  62. memset(buffer, 0, bs/8);
  63. return 0;
  64. }
  65. uint8_t hfal_hmac_ctxcopy(hfhmacgen_ctx_t* dest, hfhmacgen_ctx_t* src){
  66. dest->desc = src->desc;
  67. dest->ctx = malloc(dest->desc->ctxsize_B);
  68. if(dest->ctx == NULL){
  69. return 1;
  70. }
  71. memcpy(dest->ctx, src->ctx, dest->desc->ctxsize_B);
  72. dest->finctx = malloc(dest->desc->ctxsize_B);
  73. if(dest->finctx == NULL){
  74. free(dest->ctx);
  75. return 1;
  76. }
  77. memcpy(dest->finctx, src->finctx, dest->desc->ctxsize_B);
  78. return 0;
  79. }
  80. void hfal_hmac_nextBlock(hfhmacgen_ctx_t* ctx, const void* block){
  81. ctx->desc->nextBlock(ctx->ctx, block);
  82. }
  83. void hfal_hmac_lastBlock(hfhmacgen_ctx_t* ctx, const void* block, uint16_t length_b){
  84. hf_lastBlock_fpt lastBlock;
  85. hf_ctx2hash_fpt ctx2hash;
  86. uint16_t hs = ctx->desc->hashsize_b;
  87. uint8_t buffer[(hs+7)/8];
  88. lastBlock = ctx->desc->lastBlock;
  89. ctx2hash = ctx->desc->ctx2hash;
  90. lastBlock(ctx->ctx, block, length_b);
  91. ctx2hash(buffer, ctx->ctx);
  92. lastBlock(ctx->finctx, buffer, hs);
  93. }
  94. void hfal_hmac_ctx2mac(void* dest, hfhmacgen_ctx_t* ctx){
  95. hf_ctx2hash_fpt ctx2hash;
  96. ctx2hash = ctx->desc->ctx2hash;
  97. ctx2hash(dest, ctx->finctx);
  98. }
  99. void hfal_hmac_free(hfhmacgen_ctx_t* ctx){
  100. hf_free_fpt free_fpt;
  101. free_fpt = ctx->desc->free;
  102. if(free_fpt){
  103. free_fpt(ctx->ctx);
  104. free_fpt(ctx->finctx);
  105. }
  106. free(ctx->ctx);
  107. free(ctx->finctx);
  108. }
  109. void hfal_hmac_mem(const hfdesc_t* hash_descriptor, const void* key, uint16_t keylength_b, void* dest, const void* msg, uint32_t length_b){
  110. hfhmacgen_ctx_t ctx;
  111. uint16_t bs = hfal_hash_getBlocksize(hash_descriptor);
  112. hfal_hmac_init(hash_descriptor, &ctx, key, keylength_b);
  113. while(length_b>bs){
  114. hfal_hmac_nextBlock(&ctx, msg);
  115. msg = (uint8_t*)msg + bs/8;
  116. length_b-=bs;
  117. }
  118. hfal_hmac_lastBlock(&ctx, msg, length_b);
  119. hfal_hmac_ctx2mac(dest, &ctx);
  120. hfal_hmac_free(&ctx);
  121. }
  122. uint16_t hfal_hmac_getBlocksize(const hfdesc_t* hash_descriptor){
  123. return hfal_hash_getBlocksize(hash_descriptor);
  124. }
  125. uint16_t hfal_hmac_getMACsize(const hfdesc_t* hash_descriptor){
  126. return hfal_hash_getHashsize(hash_descriptor);
  127. }