shabal.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /* shabal.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. * \file shabal.c
  18. * \author Daniel Otte
  19. * \email daniel.otte@rub.de
  20. * \date 2009-04-17
  21. * \license GPLv3 or later
  22. *
  23. */
  24. #include <stdint.h>
  25. #include <crypto/shabal.h>
  26. #include <string.h>
  27. #define SHABAL_O1 13
  28. #define SHABAL_O2 9
  29. #define SHABAL_O3 6
  30. static inline
  31. uint32_t shabal_u(uint32_t a){
  32. return (a<<1)+a; /* a*3 */
  33. }
  34. static inline
  35. uint32_t shabal_v(uint32_t a){
  36. return (a<<2)+a; /* a*5 */
  37. }
  38. #define ROTL32(a,n) (((a)<<(n))|((a)>>(32-(n))))
  39. static
  40. void shabal_p(shabal_ctx_t* ctx, const void* m){
  41. uint8_t i,j;
  42. for(i=0;i<16;++i){
  43. ctx->b[i] = ROTL32(ctx->b[i],17);
  44. }
  45. for(j=0;j<SHABAL_P;j++){
  46. for(i=0;i<16;++i){
  47. ctx->a[(i+j*16)%SHABAL_R] =
  48. shabal_u(ctx->a[(i+j*16)%SHABAL_R]
  49. ^ shabal_v(ROTL32(ctx->a[(i+j*16+SHABAL_R-1)%SHABAL_R],15))
  50. ^ ctx->c[(8-i+16)%16])
  51. ^ ctx->b[(i+SHABAL_O1)%16]
  52. ^ ((ctx->b[(i+SHABAL_O2)%16]) & ~(ctx->b[(i+SHABAL_O3)%16]))
  53. ^ ((uint32_t*)m)[i];
  54. ctx->b[i] = ROTL32(ctx->b[i], 1) ^ ~(ctx->a[(i+j*16)%SHABAL_R]);
  55. }
  56. }
  57. for(j=0;j<36;++j){
  58. ctx->a[j%SHABAL_R] += ctx->c[(j+3)%16];
  59. }
  60. }
  61. void shabal_nextBlock(shabal_ctx_t* ctx, const void* block){
  62. uint8_t i;
  63. uint32_t* t;
  64. for(i=0;i<16;++i){
  65. ctx->b[i] += ((uint32_t*)block)[i];
  66. }
  67. ctx->a[0] ^= ctx->w.w32[0];
  68. ctx->a[1] ^= ctx->w.w32[1];
  69. shabal_p(ctx, block);
  70. for(i=0;i<16;++i){
  71. ctx->c[i] -= ((uint32_t*)block)[i];
  72. }
  73. ctx->w.w64++;
  74. t = ctx->c;
  75. ctx->c = ctx->b;
  76. ctx->b = t;
  77. }
  78. void shabal_lastBlock(shabal_ctx_t* ctx, const void* block, uint16_t length_b){
  79. uint8_t i,j;
  80. uint32_t* t;
  81. uint8_t buffer[64];
  82. while(length_b>=SHABAL_BLOCKSIZE){
  83. shabal_nextBlock(ctx, block);
  84. block = (uint8_t*)block + SHABAL_BLOCKSIZE_B;
  85. length_b -= SHABAL_BLOCKSIZE;
  86. }
  87. memset(buffer, 0, 64);
  88. memcpy(buffer, block, (length_b+7)/8);
  89. buffer[length_b/8] |= 0x80>>(length_b%8);
  90. for(i=0;i<16;++i){
  91. ctx->b[i] += ((uint32_t*)buffer)[i];
  92. }
  93. for(j=0; j<4;++j){
  94. ctx->a[0] ^= ctx->w.w32[0];
  95. ctx->a[1] ^= ctx->w.w32[1];
  96. shabal_p(ctx, buffer);
  97. t = ctx->c;
  98. ctx->c = ctx->b;
  99. ctx->b = t;
  100. }
  101. }
  102. void shabal_ctx2hash(void* dest, const shabal_ctx_t* ctx, uint16_t outlength_b){
  103. memcpy(dest, &(ctx->c[16-outlength_b/32]), outlength_b/8);
  104. }