arc4.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * An implementation of the ARCFOUR algorithm
  3. *
  4. * Copyright (C) 2006-2010, Brainspark B.V.
  5. *
  6. * This file is part of PolarSSL (http://www.polarssl.org)
  7. * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
  8. *
  9. * All rights reserved.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License along
  22. * with this program; if not, write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  24. */
  25. /*
  26. * The ARCFOUR algorithm was publicly disclosed on 94/09.
  27. *
  28. * http://groups.google.com/group/sci.crypt/msg/10a300c9d21afca0
  29. */
  30. #include "config.h"
  31. #if defined(POLARSSL_ARC4_C)
  32. #include "polarssl/arc4.h"
  33. /*
  34. * ARC4 key schedule
  35. */
  36. void arc4_setup( arc4_context *ctx, const unsigned char *key, unsigned int keylen )
  37. {
  38. int i, j, a;
  39. unsigned int k;
  40. unsigned char *m;
  41. ctx->x = 0;
  42. ctx->y = 0;
  43. m = ctx->m;
  44. for( i = 0; i < 256; i++ )
  45. m[i] = (unsigned char) i;
  46. j = k = 0;
  47. for( i = 0; i < 256; i++, k++ )
  48. {
  49. if( k >= keylen ) k = 0;
  50. a = m[i];
  51. j = ( j + a + key[k] ) & 0xFF;
  52. m[i] = m[j];
  53. m[j] = (unsigned char) a;
  54. }
  55. }
  56. /*
  57. * ARC4 cipher function
  58. */
  59. int arc4_crypt( arc4_context *ctx, size_t length, const unsigned char *input,
  60. unsigned char *output )
  61. {
  62. int x, y, a, b;
  63. size_t i;
  64. unsigned char *m;
  65. x = ctx->x;
  66. y = ctx->y;
  67. m = ctx->m;
  68. for( i = 0; i < length; i++ )
  69. {
  70. x = ( x + 1 ) & 0xFF; a = m[x];
  71. y = ( y + a ) & 0xFF; b = m[y];
  72. m[x] = (unsigned char) b;
  73. m[y] = (unsigned char) a;
  74. output[i] = (unsigned char)
  75. ( input[i] ^ m[(unsigned char)( a + b )] );
  76. }
  77. ctx->x = x;
  78. ctx->y = y;
  79. return( 0 );
  80. }
  81. #if defined(POLARSSL_SELF_TEST)
  82. #include <string.h>
  83. #ifdef PRINTF_STDLIB
  84. #include <stdio.h>
  85. #endif
  86. #ifdef PRINTF_CUSTOM
  87. #include "tinystdio.h"
  88. #endif
  89. /*
  90. * ARC4 tests vectors as posted by Eric Rescorla in sep. 1994:
  91. *
  92. * http://groups.google.com/group/comp.security.misc/msg/10a300c9d21afca0
  93. */
  94. static const unsigned char arc4_test_key[3][8] =
  95. {
  96. { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
  97. { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
  98. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
  99. };
  100. static const unsigned char arc4_test_pt[3][8] =
  101. {
  102. { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
  103. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
  104. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
  105. };
  106. static const unsigned char arc4_test_ct[3][8] =
  107. {
  108. { 0x75, 0xB7, 0x87, 0x80, 0x99, 0xE0, 0xC5, 0x96 },
  109. { 0x74, 0x94, 0xC2, 0xE7, 0x10, 0x4B, 0x08, 0x79 },
  110. { 0xDE, 0x18, 0x89, 0x41, 0xA3, 0x37, 0x5D, 0x3A }
  111. };
  112. /*
  113. * Checkup routine
  114. */
  115. int arc4_self_test( int verbose )
  116. {
  117. int i;
  118. unsigned char ibuf[8];
  119. unsigned char obuf[8];
  120. arc4_context ctx;
  121. for( i = 0; i < 3; i++ )
  122. {
  123. if( verbose != 0 )
  124. printf( " ARC4 test #%d: ", i + 1 );
  125. memcpy( ibuf, arc4_test_pt[i], 8 );
  126. arc4_setup( &ctx, (unsigned char *) arc4_test_key[i], 8 );
  127. arc4_crypt( &ctx, 8, ibuf, obuf );
  128. if( memcmp( obuf, arc4_test_ct[i], 8 ) != 0 )
  129. {
  130. if( verbose != 0 )
  131. printf( "failed\n" );
  132. return( 1 );
  133. }
  134. if( verbose != 0 )
  135. printf( "passed\n" );
  136. }
  137. if( verbose != 0 )
  138. printf( "\n" );
  139. return( 0 );
  140. }
  141. #endif
  142. #endif