ubi256.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /* ubi256.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 ubi256_init(ubi256_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, 32);
  32. }
  33. void ubi256_nextBlock(ubi256_ctx_t* ctx, const void* block){
  34. threefish256_ctx_t tfctx;
  35. ctx->tweak.v64[0] += UBI256_BLOCKSIZE_B;
  36. threefish256_init(ctx->g, ctx->tweak.v8, &tfctx);
  37. memcpy(ctx->g, block, UBI256_BLOCKSIZE_B);
  38. threefish256_enc(ctx->g, &tfctx);
  39. memxor(ctx->g, block, UBI256_BLOCKSIZE_B);
  40. ctx->tweak.v8[15] &= (uint8_t)~0x40;
  41. }
  42. void ubi256_lastBlock(ubi256_ctx_t* ctx, const void* block, uint16_t length_b){
  43. threefish256_ctx_t tfctx;
  44. while(length_b>UBI256_BLOCKSIZE){
  45. ubi256_nextBlock(ctx, block);
  46. block = (uint8_t*)block + UBI256_BLOCKSIZE_B;
  47. length_b -= UBI256_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. }
  54. threefish256_init(ctx->g, ctx->tweak.v8, &tfctx);
  55. memset(ctx->g, 0, UBI256_BLOCKSIZE_B);
  56. memcpy(ctx->g, block, (length_b+7)/8);
  57. if(length_b & 0x07){
  58. ctx->g[((length_b+7)/8)-1] |= 0x80>>(length_b&7);
  59. ctx->g[((length_b+7)/8)-1] &= ~((0x80>>(length_b&7))-1);
  60. }
  61. threefish256_enc(ctx->g, &tfctx);
  62. memxor(ctx->g, block, (length_b+7)/8);
  63. if(length_b & 0x07){
  64. ctx->g[((length_b+7)/8)-1] ^= 0x80>>(length_b&7);
  65. }
  66. }
  67. void ubi256_ctx2hash(void* dest, const ubi256_ctx_t* ctx){
  68. memcpy(dest, ctx->g, UBI256_BLOCKSIZE_B);
  69. }