salsa20.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* salsa20.c */
  2. /*
  3. This file is part of the ARM-Crypto-Lib.
  4. Copyright (C) 2006-2011 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. #include <stdint.h>
  17. #include <string.h>
  18. #include <crypto/salsa20.h>
  19. #define ROTL32(a,n) (((a)<<(n))|((a)>>(32-(n))))
  20. static
  21. void quaterround(uint32_t* a, uint32_t* b, uint32_t* c, uint32_t* d){
  22. *b ^= ROTL32(*a + *d, 7);
  23. *c ^= ROTL32(*b + *a, 9);
  24. *d ^= ROTL32(*c + *b, 13);
  25. *a ^= ROTL32(*d + *c, 18);
  26. }
  27. static
  28. void rowround(uint32_t* a){
  29. quaterround(a+ 0, a+ 1, a+ 2, a+ 3);
  30. quaterround(a+ 5, a+ 6, a+ 7, a+ 4);
  31. quaterround(a+10, a+11, a+ 8, a+ 9);
  32. quaterround(a+15, a+12, a+13, a+14);
  33. }
  34. static
  35. void columnround(uint32_t* a){
  36. quaterround(a+ 0, a+ 4, a+ 8, a+12);
  37. quaterround(a+ 5, a+ 9, a+13, a+ 1);
  38. quaterround(a+10, a+14, a+ 2, a+ 6);
  39. quaterround(a+15, a+ 3, a+ 7, a+11);
  40. }
  41. static
  42. void doubleround(uint32_t* a){
  43. columnround(a);
  44. rowround(a);
  45. }
  46. void salsa20_hash(uint32_t* a){
  47. uint8_t i;
  48. uint32_t b[16];
  49. memcpy(b, a, 64);
  50. for(i=0; i<10; ++i){
  51. doubleround(a);
  52. }
  53. for(i=0; i<16; ++i){
  54. a[i] += b[i];
  55. }
  56. }
  57. uint8_t sigma[] = {'e','x','p','a','n','d',' ','3','2','-','b','y','t','e',' ','k'};
  58. uint8_t theta[] = {'e','x','p','a','n','d',' ','1','6','-','b','y','t','e',' ','k'};
  59. void salsa_k32(uint32_t* dest, const uint32_t* k, const uint32_t* n){
  60. memcpy(dest+ 0, sigma+ 0, 4);
  61. memcpy(dest+ 4, k+ 0, 16);
  62. memcpy(dest+20, sigma+ 4, 4);
  63. memcpy(dest+24, n+ 0, 16);
  64. memcpy(dest+40, sigma+ 8, 4);
  65. memcpy(dest+44, k+16, 16);
  66. memcpy(dest+60, sigma+12, 4);
  67. salsa20_hash(dest);
  68. }
  69. void salsa_k16(uint32_t* dest, const uint32_t* k, const uint32_t* n){
  70. memcpy(dest+ 0, theta+ 0, 4);
  71. memcpy(dest+ 4, k+ 0, 16);
  72. memcpy(dest+20, theta+ 4, 4);
  73. memcpy(dest+24, n+ 0, 16);
  74. memcpy(dest+40, theta+ 8, 4);
  75. memcpy(dest+44, k+ 0, 16);
  76. memcpy(dest+60, theta+12, 4);
  77. salsa20_hash(dest);
  78. }
  79. void salsa20_genBlock256(void* dest, const void* k, const void* iv, uint64_t i){
  80. uint32_t n[4];
  81. memcpy(n, iv, 8);
  82. //? memcpy(n+8, &i, 8);
  83. salsa_k32((uint32_t*)dest, (uint32_t*)k, n);
  84. }
  85. void salsa20_genBlock128(void* dest, const void* k, const void* iv, uint64_t i){
  86. uint32_t n[4];
  87. memcpy(n, iv, 8);
  88. //? memcpy(n+8, &i, 8);
  89. salsa_k16((uint32_t*)dest, (uint32_t*)k, n);
  90. }
  91. void salsa20_init(void* key, uint16_t keylength_b, void* iv, salsa20_ctx_t* ctx){
  92. if(keylength_b==256){
  93. memcpy((ctx->a.v8+ 0), sigma+ 0, 4);
  94. memcpy((ctx->a.v8+20), sigma+ 4, 4);
  95. memcpy((ctx->a.v8+40), sigma+ 8, 4);
  96. memcpy((ctx->a.v8+44), (uint8_t*)key+16, 16);
  97. memcpy((ctx->a.v8+60), sigma+12, 4);
  98. }else{
  99. memcpy((ctx->a.v8+ 0), theta+ 0, 4);
  100. memcpy((ctx->a.v8+20), theta+ 4, 4);
  101. memcpy((ctx->a.v8+40), theta+ 8, 4);
  102. memcpy((ctx->a.v8+44), (uint8_t*)key+ 0, 16);
  103. memcpy((ctx->a.v8+60), theta+12, 4);
  104. }
  105. memcpy((ctx->a.v8+ 4), key, 16);
  106. memset((ctx->a.v8+24), 0, 16);
  107. if(iv){
  108. memcpy((ctx->a.v8+24), iv, 8);
  109. }
  110. ctx->buffer_idx=64;
  111. }
  112. uint8_t salsa20_gen(salsa20_ctx_t* ctx){
  113. if(ctx->buffer_idx==64){
  114. memcpy(ctx->buffer, ctx->a.v8, 64);
  115. salsa20_hash((uint32_t*)(ctx->buffer));
  116. ctx->a.v64[4] += 1;
  117. ctx->buffer_idx = 0;
  118. }
  119. return ctx->buffer[ctx->buffer_idx++];
  120. }