skein1024.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /* skein1024.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. /*
  17. * \author Daniel Otte
  18. * \email daniel.otte@rub.de
  19. * \date 2009-03-12
  20. * \license GPLv3 or later
  21. *
  22. */
  23. #include <stdint.h>
  24. #include <string.h>
  25. #include <crypto/ubi.h>
  26. #include <crypto/skein.h>
  27. void skein1024_init(skein1024_ctx_t* ctx, uint16_t outsize_b){
  28. skein_config_t conf;
  29. uint8_t null[UBI1024_BLOCKSIZE_B];
  30. memset(null, 0, UBI1024_BLOCKSIZE_B);
  31. memset(&conf, 0, sizeof(skein_config_t));
  32. conf.schema[0] = 'S';
  33. conf.schema[1] = 'H';
  34. conf.schema[2] = 'A';
  35. conf.schema[3] = '3';
  36. conf.version = 1;
  37. conf.out_length = outsize_b;
  38. ctx->outsize_b = outsize_b;
  39. ubi1024_init(&(ctx->ubictx), null, UBI_TYPE_CFG);
  40. ubi1024_lastBlock(&(ctx->ubictx), &conf, 256);
  41. ubi1024_init(&(ctx->ubictx), ctx->ubictx.g, UBI_TYPE_MSG);
  42. }
  43. void skein1024_nextBlock(skein1024_ctx_t* ctx, const void* block){
  44. ubi1024_nextBlock(&(ctx->ubictx), block);
  45. }
  46. void skein1024_lastBlock(skein1024_ctx_t* ctx, const void* block, uint16_t length_b){
  47. ubi1024_lastBlock(&(ctx->ubictx), block, length_b);
  48. }
  49. void skein1024_ctx2hash(void* dest, skein1024_ctx_t* ctx){
  50. ubi1024_ctx_t uctx;
  51. uint16_t outsize_b;
  52. uint64_t counter=0;
  53. uint8_t outbuffer[UBI1024_BLOCKSIZE_B];
  54. ubi1024_init(&(ctx->ubictx), ctx->ubictx.g, UBI_TYPE_OUT);
  55. outsize_b = ctx->outsize_b;
  56. while(1){
  57. memcpy(&uctx, &(ctx->ubictx), sizeof(ubi1024_ctx_t));
  58. ubi1024_lastBlock(&uctx, &counter, 64);
  59. ubi1024_ctx2hash(outbuffer, &uctx);
  60. if(outsize_b<=UBI1024_BLOCKSIZE){
  61. memcpy(dest, outbuffer, (ctx->outsize_b+7)/8);
  62. break;
  63. }else{
  64. memcpy(dest, outbuffer, UBI1024_BLOCKSIZE_B);
  65. dest = (uint8_t*)dest + UBI1024_BLOCKSIZE_B;
  66. outsize_b -= UBI1024_BLOCKSIZE;
  67. counter++;
  68. }
  69. }
  70. }
  71. void skein1024(void* dest, uint16_t outlength_b, const void* msg, uint32_t length_b){
  72. skein1024_ctx_t ctx;
  73. skein1024_init(&ctx, outlength_b);
  74. while(length_b>SKEIN1024_BLOCKSIZE){
  75. skein1024_nextBlock(&ctx, msg);
  76. msg = (uint8_t*)msg + SKEIN1024_BLOCKSIZE_B;
  77. length_b -= SKEIN1024_BLOCKSIZE;
  78. }
  79. skein1024_lastBlock(&ctx, msg, length_b);
  80. skein1024_ctx2hash(dest, &ctx);
  81. }