ubi1024.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /* ubi1024.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/threefish.h>
  26. #include <crypto/memxor.h>
  27. #include <crypto/ubi.h>
  28. void ubi1024_init(ubi1024_ctx_t* ctx, const void* g, uint8_t type){
  29. memset(ctx->tweak.v8, 0, 15);
  30. ctx->tweak.v8[15] = 0x40+type;
  31. memcpy(ctx->g, g, UBI1024_BLOCKSIZE_B);
  32. }
  33. void ubi1024_nextBlock(ubi1024_ctx_t* ctx, const void* block){
  34. threefish1024_ctx_t tfctx;
  35. ctx->tweak.v64[0] += UBI1024_BLOCKSIZE_B;
  36. threefish1024_init(ctx->g, ctx->tweak.v8, &tfctx);
  37. memcpy(ctx->g, block, UBI1024_BLOCKSIZE_B);
  38. threefish1024_enc(ctx->g, &tfctx);
  39. memxor(ctx->g, block, UBI1024_BLOCKSIZE_B);
  40. ctx->tweak.v8[15] &= (uint8_t)~0x40;
  41. }
  42. void ubi1024_lastBlock(ubi1024_ctx_t* ctx, const void* block, uint16_t length_b){
  43. threefish1024_ctx_t tfctx;
  44. while(length_b>UBI1024_BLOCKSIZE){
  45. ubi1024_nextBlock(ctx, block);
  46. block = (uint8_t*)block + UBI1024_BLOCKSIZE_B;
  47. length_b -= UBI1024_BLOCKSIZE;
  48. }
  49. ctx->tweak.v8[15] |= 0x80;
  50. ctx->tweak.v64[0] += (length_b+7)/8;
  51. if(length_b & 0x07)
  52. ctx->tweak.v8[14] |= 0x80;
  53. threefish1024_init(ctx->g, ctx->tweak.v8, &tfctx);
  54. memset(ctx->g, 0, UBI1024_BLOCKSIZE_B);
  55. memcpy(ctx->g, block, (length_b+7)/8);
  56. if(length_b & 0x07)
  57. ctx->g[(length_b+7)/8-1] |= 0x80>>(length_b&7);
  58. threefish1024_enc(ctx->g, &tfctx);
  59. memxor(ctx->g, block, (length_b+7)/8);
  60. if(length_b & 0x07){
  61. ctx->g[((length_b+7)/8)-1] ^= 0x80>>(length_b&7);
  62. }
  63. }
  64. void ubi1024_ctx2hash(void* dest, const ubi1024_ctx_t* ctx){
  65. memcpy(dest, ctx->g, UBI1024_BLOCKSIZE_B);
  66. }