threefish1024_enc.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* threefish1024_enc.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. *
  24. */
  25. #include <stdint.h>
  26. #include <string.h>
  27. #include <crypto/threefish.h>
  28. #define X(a) (((uint64_t*)data)[(a)])
  29. static
  30. void permute_16(void* data){
  31. uint64_t t;
  32. t = X(1);
  33. X(1) = X(9);
  34. X(9) = X(7);
  35. X(7) = X(15);
  36. X(15) = t;
  37. t = X(3);
  38. X(3) = X(13);
  39. X(13) = X(5);
  40. X(5) = X(11);
  41. X(11) = t;
  42. t = X(4);
  43. X(4) = X(6);
  44. X(6) = t;
  45. t = X(8);
  46. X(8) = X(10);
  47. X(10) = X(12);
  48. X(12) = X(14);
  49. X(14) = t;
  50. }
  51. //#define THREEFISH_KEY_CONST 0x5555555555555555LL /* 2**64/3 */
  52. #define THREEFISH_KEY_CONST 0x1BD11BDAA9FC1A22LL
  53. #define K(s) (((uint64_t*)key)[(s)])
  54. #define T(s) (((uint64_t*)tweak)[(s)])
  55. void threefish1024_init(const void* key, const void* tweak, threefish1024_ctx_t* ctx){
  56. memcpy(ctx->k, key, 16*8);
  57. if(tweak){
  58. memcpy(ctx->t, tweak, 2*8);
  59. ctx->t[2] = T(0) ^ T(1);
  60. }else{
  61. memset(ctx, 0, 3*8);
  62. }
  63. uint8_t i;
  64. ctx->k[16] = THREEFISH_KEY_CONST;
  65. for(i=0; i<16; ++i){
  66. ctx->k[16] ^= K(i);
  67. }
  68. }
  69. static
  70. void add_key_16(void* data, const threefish1024_ctx_t* ctx, uint8_t s){
  71. uint8_t i;
  72. for(i=0; i<13; ++i){
  73. X(i) += ctx->k[(s+i)%17];
  74. }
  75. X(13) += ctx->k[(s+13)%17] + ctx->t[s%3];
  76. X(14) += ctx->k[(s+14)%17] + ctx->t[(s+1)%3];
  77. X(15) += ctx->k[(s+15)%17] + s;
  78. }
  79. void threefish1024_enc(void* data, const threefish1024_ctx_t* ctx){
  80. uint8_t i=0,s=0;
  81. /* old constans, changed at round 2 of the SHA-3 contest
  82. uint8_t r0[8] = {55, 25, 33, 34, 28, 17, 58, 47};
  83. uint8_t r1[8] = {43, 25, 8, 43, 7, 6, 7, 49};
  84. uint8_t r2[8] = {37, 46, 18, 25, 47, 18, 32, 27};
  85. uint8_t r3[8] = {40, 13, 57, 60, 48, 25, 45, 58};
  86. uint8_t r4[8] = {16, 14, 21, 44, 51, 43, 19, 37};
  87. uint8_t r5[8] = {22, 13, 12, 9, 9, 42, 18, 48};
  88. uint8_t r6[8] = {38, 52, 32, 59, 35, 40, 2, 53};
  89. uint8_t r7[8] = {12, 57, 54, 34, 41, 15, 56, 56};
  90. */
  91. uint8_t r0[8] = {24, 38, 33, 5, 41, 16, 31, 9};
  92. uint8_t r1[8] = {13, 19, 4, 20, 9, 34, 44, 48};
  93. uint8_t r2[8] = { 8, 10, 51, 48, 37, 56, 47, 35};
  94. uint8_t r3[8] = {47, 55, 13, 41, 31, 51, 46, 52};
  95. uint8_t r4[8] = { 8, 49, 34, 47, 12, 4, 19, 23};
  96. uint8_t r5[8] = {17, 18, 41, 28, 47, 53, 42, 31};
  97. uint8_t r6[8] = {22, 23, 59, 16, 44, 42, 44, 37};
  98. uint8_t r7[8] = {37, 52, 17, 25, 30, 41, 25, 20};
  99. do{
  100. if(i%4==0){
  101. add_key_16(data, ctx, s);
  102. ++s;
  103. }
  104. threefish_mix((uint8_t*)data + 0, r0[i%8]);
  105. threefish_mix((uint8_t*)data + 16, r1[i%8]);
  106. threefish_mix((uint8_t*)data + 32, r2[i%8]);
  107. threefish_mix((uint8_t*)data + 48, r3[i%8]);
  108. threefish_mix((uint8_t*)data + 64, r4[i%8]);
  109. threefish_mix((uint8_t*)data + 80, r5[i%8]);
  110. threefish_mix((uint8_t*)data + 96, r6[i%8]);
  111. threefish_mix((uint8_t*)data +112, r7[i%8]);
  112. permute_16(data);
  113. ++i;
  114. }while(i!=80);
  115. add_key_16(data, ctx, s);
  116. }