rabbit.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /* rabbit.c
  2. *
  3. * Copyright (C) 2006-2011 Sawtooth Consulting Ltd.
  4. *
  5. * This file is part of CyaSSL.
  6. *
  7. * CyaSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * CyaSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  20. */
  21. #ifndef NO_RABBIT
  22. #include "rabbit.h"
  23. #include "misc.c"
  24. #ifdef BIG_ENDIAN_ORDER
  25. #define LITTLE32(x) ByteReverseWord32(x)
  26. #else
  27. #define LITTLE32(x) (x)
  28. #endif
  29. #define U32V(x) (word32)(x)
  30. /* Square a 32-bit unsigned integer to obtain the 64-bit result and return */
  31. /* the upper 32 bits XOR the lower 32 bits */
  32. static word32 RABBIT_g_func(word32 x)
  33. {
  34. /* Temporary variables */
  35. word32 a, b, h, l;
  36. /* Construct high and low argument for squaring */
  37. a = x&0xFFFF;
  38. b = x>>16;
  39. /* Calculate high and low result of squaring */
  40. h = (((U32V(a*a)>>17) + U32V(a*b))>>15) + b*b;
  41. l = x*x;
  42. /* Return high XOR low */
  43. return U32V(h^l);
  44. }
  45. /* Calculate the next internal state */
  46. static void RABBIT_next_state(RabbitCtx* ctx)
  47. {
  48. /* Temporary variables */
  49. word32 g[8], c_old[8], i;
  50. /* Save old counter values */
  51. for (i=0; i<8; i++)
  52. c_old[i] = ctx->c[i];
  53. /* Calculate new counter values */
  54. ctx->c[0] = U32V(ctx->c[0] + 0x4D34D34D + ctx->carry);
  55. ctx->c[1] = U32V(ctx->c[1] + 0xD34D34D3 + (ctx->c[0] < c_old[0]));
  56. ctx->c[2] = U32V(ctx->c[2] + 0x34D34D34 + (ctx->c[1] < c_old[1]));
  57. ctx->c[3] = U32V(ctx->c[3] + 0x4D34D34D + (ctx->c[2] < c_old[2]));
  58. ctx->c[4] = U32V(ctx->c[4] + 0xD34D34D3 + (ctx->c[3] < c_old[3]));
  59. ctx->c[5] = U32V(ctx->c[5] + 0x34D34D34 + (ctx->c[4] < c_old[4]));
  60. ctx->c[6] = U32V(ctx->c[6] + 0x4D34D34D + (ctx->c[5] < c_old[5]));
  61. ctx->c[7] = U32V(ctx->c[7] + 0xD34D34D3 + (ctx->c[6] < c_old[6]));
  62. ctx->carry = (ctx->c[7] < c_old[7]);
  63. /* Calculate the g-values */
  64. for (i=0;i<8;i++)
  65. g[i] = RABBIT_g_func(U32V(ctx->x[i] + ctx->c[i]));
  66. /* Calculate new state values */
  67. ctx->x[0] = U32V(g[0] + rotlFixed(g[7],16) + rotlFixed(g[6], 16));
  68. ctx->x[1] = U32V(g[1] + rotlFixed(g[0], 8) + g[7]);
  69. ctx->x[2] = U32V(g[2] + rotlFixed(g[1],16) + rotlFixed(g[0], 16));
  70. ctx->x[3] = U32V(g[3] + rotlFixed(g[2], 8) + g[1]);
  71. ctx->x[4] = U32V(g[4] + rotlFixed(g[3],16) + rotlFixed(g[2], 16));
  72. ctx->x[5] = U32V(g[5] + rotlFixed(g[4], 8) + g[3]);
  73. ctx->x[6] = U32V(g[6] + rotlFixed(g[5],16) + rotlFixed(g[4], 16));
  74. ctx->x[7] = U32V(g[7] + rotlFixed(g[6], 8) + g[5]);
  75. }
  76. /* IV setup */
  77. static void RabbitSetIV(Rabbit* ctx, const byte* iv)
  78. {
  79. /* Temporary variables */
  80. word32 i0, i1, i2, i3, i;
  81. /* Generate four subvectors */
  82. i0 = LITTLE32(*(word32*)(iv+0));
  83. i2 = LITTLE32(*(word32*)(iv+4));
  84. i1 = (i0>>16) | (i2&0xFFFF0000);
  85. i3 = (i2<<16) | (i0&0x0000FFFF);
  86. /* Modify counter values */
  87. ctx->workCtx.c[0] = ctx->masterCtx.c[0] ^ i0;
  88. ctx->workCtx.c[1] = ctx->masterCtx.c[1] ^ i1;
  89. ctx->workCtx.c[2] = ctx->masterCtx.c[2] ^ i2;
  90. ctx->workCtx.c[3] = ctx->masterCtx.c[3] ^ i3;
  91. ctx->workCtx.c[4] = ctx->masterCtx.c[4] ^ i0;
  92. ctx->workCtx.c[5] = ctx->masterCtx.c[5] ^ i1;
  93. ctx->workCtx.c[6] = ctx->masterCtx.c[6] ^ i2;
  94. ctx->workCtx.c[7] = ctx->masterCtx.c[7] ^ i3;
  95. /* Copy state variables */
  96. for (i=0; i<8; i++)
  97. ctx->workCtx.x[i] = ctx->masterCtx.x[i];
  98. ctx->workCtx.carry = ctx->masterCtx.carry;
  99. /* Iterate the system four times */
  100. for (i=0; i<4; i++)
  101. RABBIT_next_state(&(ctx->workCtx));
  102. }
  103. /* Key setup */
  104. void RabbitSetKey(Rabbit* ctx, const byte* key, const byte* iv)
  105. {
  106. /* Temporary variables */
  107. word32 k0, k1, k2, k3, i;
  108. /* Generate four subkeys */
  109. k0 = LITTLE32(*(word32*)(key+ 0));
  110. k1 = LITTLE32(*(word32*)(key+ 4));
  111. k2 = LITTLE32(*(word32*)(key+ 8));
  112. k3 = LITTLE32(*(word32*)(key+12));
  113. /* Generate initial state variables */
  114. ctx->masterCtx.x[0] = k0;
  115. ctx->masterCtx.x[2] = k1;
  116. ctx->masterCtx.x[4] = k2;
  117. ctx->masterCtx.x[6] = k3;
  118. ctx->masterCtx.x[1] = U32V(k3<<16) | (k2>>16);
  119. ctx->masterCtx.x[3] = U32V(k0<<16) | (k3>>16);
  120. ctx->masterCtx.x[5] = U32V(k1<<16) | (k0>>16);
  121. ctx->masterCtx.x[7] = U32V(k2<<16) | (k1>>16);
  122. /* Generate initial counter values */
  123. ctx->masterCtx.c[0] = rotlFixed(k2, 16);
  124. ctx->masterCtx.c[2] = rotlFixed(k3, 16);
  125. ctx->masterCtx.c[4] = rotlFixed(k0, 16);
  126. ctx->masterCtx.c[6] = rotlFixed(k1, 16);
  127. ctx->masterCtx.c[1] = (k0&0xFFFF0000) | (k1&0xFFFF);
  128. ctx->masterCtx.c[3] = (k1&0xFFFF0000) | (k2&0xFFFF);
  129. ctx->masterCtx.c[5] = (k2&0xFFFF0000) | (k3&0xFFFF);
  130. ctx->masterCtx.c[7] = (k3&0xFFFF0000) | (k0&0xFFFF);
  131. /* Clear carry bit */
  132. ctx->masterCtx.carry = 0;
  133. /* Iterate the system four times */
  134. for (i=0; i<4; i++)
  135. RABBIT_next_state(&(ctx->masterCtx));
  136. /* Modify the counters */
  137. for (i=0; i<8; i++)
  138. ctx->masterCtx.c[i] ^= ctx->masterCtx.x[(i+4)&0x7];
  139. /* Copy master instance to work instance */
  140. for (i=0; i<8; i++) {
  141. ctx->workCtx.x[i] = ctx->masterCtx.x[i];
  142. ctx->workCtx.c[i] = ctx->masterCtx.c[i];
  143. }
  144. ctx->workCtx.carry = ctx->masterCtx.carry;
  145. if (iv) RabbitSetIV(ctx, iv);
  146. }
  147. /* Encrypt/decrypt a message of any size */
  148. void RabbitProcess(Rabbit* ctx, byte* output, const byte* input, word32 msglen)
  149. {
  150. /* Encrypt/decrypt all full blocks */
  151. while (msglen >= 16) {
  152. /* Iterate the system */
  153. RABBIT_next_state(&(ctx->workCtx));
  154. /* Encrypt/decrypt 16 bytes of data */
  155. *(word32*)(output+ 0) = *(word32*)(input+ 0) ^
  156. LITTLE32(ctx->workCtx.x[0] ^ (ctx->workCtx.x[5]>>16) ^
  157. U32V(ctx->workCtx.x[3]<<16));
  158. *(word32*)(output+ 4) = *(word32*)(input+ 4) ^
  159. LITTLE32(ctx->workCtx.x[2] ^ (ctx->workCtx.x[7]>>16) ^
  160. U32V(ctx->workCtx.x[5]<<16));
  161. *(word32*)(output+ 8) = *(word32*)(input+ 8) ^
  162. LITTLE32(ctx->workCtx.x[4] ^ (ctx->workCtx.x[1]>>16) ^
  163. U32V(ctx->workCtx.x[7]<<16));
  164. *(word32*)(output+12) = *(word32*)(input+12) ^
  165. LITTLE32(ctx->workCtx.x[6] ^ (ctx->workCtx.x[3]>>16) ^
  166. U32V(ctx->workCtx.x[1]<<16));
  167. /* Increment pointers and decrement length */
  168. input += 16;
  169. output += 16;
  170. msglen -= 16;
  171. }
  172. /* Encrypt/decrypt remaining data */
  173. if (msglen) {
  174. word32 i;
  175. word32 tmp[4];
  176. byte* buffer = (byte*)tmp;
  177. /* Iterate the system */
  178. RABBIT_next_state(&(ctx->workCtx));
  179. /* Generate 16 bytes of pseudo-random data */
  180. tmp[0] = LITTLE32(ctx->workCtx.x[0] ^
  181. (ctx->workCtx.x[5]>>16) ^ U32V(ctx->workCtx.x[3]<<16));
  182. tmp[1] = LITTLE32(ctx->workCtx.x[2] ^
  183. (ctx->workCtx.x[7]>>16) ^ U32V(ctx->workCtx.x[5]<<16));
  184. tmp[2] = LITTLE32(ctx->workCtx.x[4] ^
  185. (ctx->workCtx.x[1]>>16) ^ U32V(ctx->workCtx.x[7]<<16));
  186. tmp[3] = LITTLE32(ctx->workCtx.x[6] ^
  187. (ctx->workCtx.x[3]>>16) ^ U32V(ctx->workCtx.x[1]<<16));
  188. /* Encrypt/decrypt the data */
  189. for (i=0; i<msglen; i++)
  190. output[i] = input[i] ^ buffer[i];
  191. }
  192. }
  193. #endif /* NO_RABBIT */