hfal-basic.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /* hfal-basic.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 <stdlib.h>
  19. uint8_t hfal_hash_init(const hfdesc_t* hash_descriptor, hfgen_ctx_t* ctx){
  20. ctx->desc_ptr = (hfdesc_t*)hash_descriptor;
  21. if(!(ctx->ctx=malloc(hash_descriptor->ctxsize_B)))
  22. return 3;
  23. hash_descriptor->init(ctx->ctx);
  24. return 0;
  25. }
  26. void hfal_hash_nextBlock(hfgen_ctx_t* ctx, const void* block){
  27. ctx->desc_ptr->nextBlock(ctx->ctx, block);
  28. }
  29. void hfal_hash_lastBlock(hfgen_ctx_t* ctx, const void* block, uint16_t length_b){
  30. ctx->desc_ptr->lastBlock(ctx->ctx, block, length_b);
  31. }
  32. void hfal_hash_ctx2hash(void* dest, hfgen_ctx_t* ctx){
  33. ctx->desc_ptr->ctx2hash(dest, ctx->ctx);
  34. }
  35. void hfal_hash_free(hfgen_ctx_t* ctx){
  36. hf_free_fpt f;
  37. f = ctx->desc_ptr->free;
  38. if(f)
  39. f(ctx->ctx);
  40. free(ctx->ctx);
  41. }
  42. void hfal_hash_mem(const hfdesc_t* hash_descriptor, void* dest, const void* msg, uint32_t length_b){
  43. void_fpt f;
  44. f = (void_fpt)(hash_descriptor->mem);
  45. if(f){
  46. ((hf_mem_fpt)f)(dest, msg, length_b);
  47. }else{
  48. uint16_t bs,bsb;
  49. uint8_t ctx[hash_descriptor->ctxsize_B];
  50. hash_descriptor->init(ctx);
  51. bs = hash_descriptor->blocksize_b;
  52. bsb=bs/8;
  53. f=(void_fpt)(hash_descriptor->nextBlock);
  54. while(length_b>bs){
  55. ((hf_nextBlock_fpt)f)(ctx, msg);
  56. length_b -= bs;
  57. msg = (uint8_t*)msg + bsb;
  58. }
  59. hash_descriptor->lastBlock(ctx, msg, length_b);
  60. hash_descriptor->ctx2hash(dest, ctx);
  61. }
  62. }
  63. uint16_t hfal_hash_getCtxsize_B(const hfdesc_t* hash_descriptor){
  64. return hash_descriptor->ctxsize_B;
  65. }
  66. uint16_t hfal_hash_getBlocksize(const hfdesc_t* hash_descriptor){
  67. return hash_descriptor->blocksize_b;
  68. }
  69. uint16_t hfal_hash_getHashsize(const hfdesc_t* hash_descriptor){
  70. return hash_descriptor->hashsize_b;
  71. }