rc6.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* rc6.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: rc6.c
  18. * Author: Daniel Otte
  19. * Date: 06.08.2006
  20. * License: GPL
  21. * Description: Implementation of the RC6 cipher algorithm.
  22. * This implementation is restricted to 32-bit words and to keys up to 65535 bit in length (but this is
  23. * quite easy to expand), but free in the choice of number of rounds (0 to 125).
  24. * so it is RC6-32/r/b
  25. * THIS ONLY WORKS FOR LITTEL ENDIAN!!!
  26. */
  27. #include <stdint.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <crypto/rc6.h>
  31. #define P32 0xB7E15163 /* e -2 */
  32. #define Q32 0x9E3779B9 /* Golden Ratio -1 */
  33. uint32_t rotl32(uint32_t a, uint8_t n){
  34. n &= 0x1f; /* higher rotates would not bring anything */
  35. return ( (a<<n)| (a>>(32-n)) );
  36. }
  37. uint32_t rotr32(uint32_t a, uint8_t n){
  38. n &= 0x1f; /* higher rotates would not bring anything */
  39. return ( (a>>n)| (a<<(32-n)) );
  40. }
  41. void rc6_init(const void* key, uint16_t keylength_b, rc6_ctx_t *s){
  42. rc6_initl(key, keylength_b, 20, s);
  43. }
  44. uint8_t rc6_initl(const void* key, uint16_t keylength_b, uint8_t rounds, rc6_ctx_t *s){
  45. uint8_t i,j;
  46. uint16_t v,p,c;
  47. uint32_t a,b;
  48. c =( keylength_b+31)/32;
  49. uint32_t local_key[c];
  50. if (rounds>125)
  51. return 2;
  52. if(!(s->S=malloc((2*rounds+4)*sizeof(uint32_t))))
  53. return 1;
  54. s->rounds=rounds;
  55. local_key[c-1] = 0;
  56. memcpy(local_key, key, (keylength_b+7)/8);
  57. s->S[0] = P32;
  58. for(i=1; i<2*rounds+4; ++i){
  59. s->S[i] = s->S[i-1] + Q32;
  60. }
  61. a=b=j=i=0;
  62. v = 3 * ((c > (2*rounds+4))?c:(2*rounds+4));
  63. for(p=1; p<=v; ++p){
  64. a = s->S[i] = rotl32(s->S[i] + a + b, 3);
  65. b = ((uint32_t*)local_key)[j] = rotl32(((uint32_t*)local_key)[j]+a+b, a+b);
  66. i = (i+1) % (2*rounds+4);
  67. j = (j+1) % c;
  68. }
  69. return 0;
  70. }
  71. void rc6_free(rc6_ctx_t *s){
  72. free(s->S);
  73. }
  74. #define LG_W 5
  75. #define A (((uint32_t*)block)[0])
  76. #define B (((uint32_t*)block)[1])
  77. #define C (((uint32_t*)block)[2])
  78. #define D (((uint32_t*)block)[3])
  79. void rc6_enc(void* block, rc6_ctx_t *s){
  80. uint8_t i;
  81. uint32_t t,u,x; /* greetings to Linux? */
  82. uint32_t *p;
  83. p=s->S;
  84. B += *p++;
  85. D += *p++;
  86. i=s->rounds;
  87. do{
  88. t = rotl32(B * (2*B+1), LG_W);
  89. u = rotl32(D * (2*D+1), LG_W);
  90. A = rotl32((A ^ t), u) + *p++;
  91. C = rotl32((C ^ u), t) + *p++;
  92. x = A;
  93. A = B;
  94. B = C;
  95. C = D;
  96. D = x;
  97. }while(--i);
  98. A += *p++;
  99. C += *p;
  100. }
  101. void rc6_dec(void* block, rc6_ctx_t *s){
  102. uint8_t i;
  103. uint32_t t,u,x; /* greetings to Linux? */
  104. uint32_t *p;
  105. p = &(s->S[2*(s->rounds)+3]);
  106. C -= *p--;
  107. A -= *p--;
  108. i=s->rounds;
  109. do{
  110. x=D;
  111. D=C;
  112. C=B;
  113. B=A;
  114. A=x;
  115. u = rotl32(D * (2*D+1), LG_W);
  116. t = rotl32(B * (2*B+1), LG_W);
  117. C = rotr32(C - *p--, t) ^ u;
  118. A = rotr32(A - *p--, u) ^ t;
  119. }while(--i);
  120. D -= *p--;
  121. B -= *p;
  122. }