skein512.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /* skein512.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 skein512_init(skein512_ctx_t* ctx, uint16_t outsize_b){
  28. skein_config_t conf;
  29. uint8_t null[UBI512_BLOCKSIZE_B];
  30. memset(null, 0, UBI512_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. ubi512_init(&(ctx->ubictx), null, UBI_TYPE_CFG);
  40. ubi512_lastBlock(&(ctx->ubictx), &conf, 256);
  41. ubi512_init(&(ctx->ubictx), ctx->ubictx.g, UBI_TYPE_MSG);
  42. }
  43. void skein512_nextBlock(skein512_ctx_t* ctx, const void* block){
  44. ubi512_nextBlock(&(ctx->ubictx), block);
  45. }
  46. void skein512_lastBlock(skein512_ctx_t* ctx, const void* block, uint16_t length_b){
  47. ubi512_lastBlock(&(ctx->ubictx), block, length_b);
  48. }
  49. void skein512_ctx2hash(void* dest, skein512_ctx_t* ctx){
  50. ubi512_ctx_t uctx;
  51. uint16_t outsize_b;
  52. uint64_t counter=0;
  53. uint8_t outbuffer[UBI512_BLOCKSIZE_B];
  54. ubi512_init(&(ctx->ubictx), ctx->ubictx.g, UBI_TYPE_OUT);
  55. outsize_b = ctx->outsize_b;
  56. while(1){
  57. memcpy(&uctx, &(ctx->ubictx), sizeof(ubi512_ctx_t));
  58. ubi512_lastBlock(&uctx, &counter, 64);
  59. ubi512_ctx2hash(outbuffer, &uctx);
  60. if(outsize_b<=UBI512_BLOCKSIZE){
  61. memcpy(dest, outbuffer, (ctx->outsize_b+7)/8);
  62. break;
  63. }else{
  64. memcpy(dest, outbuffer, UBI512_BLOCKSIZE_B);
  65. dest = (uint8_t*)dest + UBI512_BLOCKSIZE_B;
  66. outsize_b -= UBI512_BLOCKSIZE;
  67. counter++;
  68. }
  69. }
  70. }
  71. void skein512(void* dest, uint16_t outlength_b,const void* msg, uint32_t length_b){
  72. skein512_ctx_t ctx;
  73. skein512_init(&ctx, outlength_b);
  74. while(length_b>SKEIN512_BLOCKSIZE){
  75. skein512_nextBlock(&ctx, msg);
  76. msg = (uint8_t*)msg + SKEIN512_BLOCKSIZE_B;
  77. length_b -= SKEIN512_BLOCKSIZE;
  78. }
  79. skein512_lastBlock(&ctx, msg, length_b);
  80. skein512_ctx2hash(dest, &ctx);
  81. }