jh_simple_aux.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* jh_simple_speed.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 <stdint.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <crypto/memxor.h>
  20. #include <crypto/jh_simple.h>
  21. #define DEBUG 0
  22. #if DEBUG
  23. #include <crypto/cli.h>
  24. #endif
  25. void jh_encrypt(uint8_t *a);
  26. void jh_init(uint16_t hashlen_b, jh_ctx_t* ctx){
  27. memset(ctx->a, 0, 128);
  28. ctx->a[0] = hashlen_b>>8;
  29. ctx->a[1] = hashlen_b&0xff;
  30. jh_encrypt(ctx->a);
  31. ctx->block_hashed=0;
  32. }
  33. void jh_nextBlock(jh_ctx_t* ctx, void* block){
  34. memxor(ctx->a, block, 64);
  35. jh_encrypt(ctx->a);
  36. memxor(ctx->a+64, block, 64);
  37. ctx->block_hashed++;
  38. }
  39. void jh_lastBlock(jh_ctx_t* ctx, void* block, uint16_t length_b){
  40. while(length_b>=64*8){
  41. jh_nextBlock(ctx, block);
  42. block = (uint8_t*)block + 64;
  43. length_b -= 64*8;
  44. }
  45. uint8_t buffer[64];
  46. uint64_t total_length;
  47. memset(buffer, 0, 64);
  48. memcpy(buffer, block, (length_b+7)/8);
  49. buffer[length_b/8] |= 0x80>>(length_b%8);
  50. total_length=ctx->block_hashed*512+length_b;
  51. if(length_b==0){
  52. }else{
  53. jh_nextBlock(ctx, buffer);
  54. buffer[0]=0;
  55. }
  56. memset(buffer+1, 0, 64-8-1);
  57. buffer[63] = total_length&0xff;
  58. buffer[62] = (total_length>> 8)&0xff;
  59. buffer[61] = (total_length>>16)&0xff;
  60. buffer[60] = (total_length>>24)&0xff;
  61. buffer[59] = (total_length>>32)&0xff;
  62. buffer[58] = (total_length>>40)&0xff;
  63. buffer[57] = (total_length>>48)&0xff;
  64. buffer[56] = (total_length>>56)&0xff;
  65. jh_nextBlock(ctx, buffer);
  66. }
  67. void jh_ctx2hash(void* dest, uint16_t length_b, jh_ctx_t* ctx){
  68. memcpy(dest, ctx->a+128-(length_b+7)/8, (length_b+7)/8);
  69. }
  70. void jh224_init(jh_ctx_t* ctx){
  71. jh_init(224, ctx);
  72. }
  73. void jh224_ctx2hash(void* dest, jh_ctx_t* ctx){
  74. jh_ctx2hash(dest, 224, ctx);
  75. }
  76. void jh256_init(jh_ctx_t* ctx){
  77. jh_init(256, ctx);
  78. }
  79. void jh256_ctx2hash(void* dest, jh_ctx_t* ctx){
  80. jh_ctx2hash(dest, 256, ctx);
  81. }
  82. void jh384_init(jh_ctx_t* ctx){
  83. jh_init(384, ctx);
  84. }
  85. void jh384_ctx2hash(void* dest, jh_ctx_t* ctx){
  86. jh_ctx2hash(dest, 384, ctx);
  87. }
  88. void jh512_init(jh_ctx_t* ctx){
  89. jh_init(512, ctx);
  90. }
  91. void jh512_ctx2hash(void* dest, jh_ctx_t* ctx){
  92. jh_ctx2hash(dest, 512, ctx);
  93. }