arc4.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * An implementation of the ARCFOUR algorithm
  3. *
  4. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  5. * SPDX-License-Identifier: GPL-2.0
  6. *
  7. * This program 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. * This program 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 along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. *
  21. * This file is part of mbed TLS (https://tls.mbed.org)
  22. */
  23. /*
  24. * The ARCFOUR algorithm was publicly disclosed on 94/09.
  25. *
  26. * http://groups.google.com/group/sci.crypt/msg/10a300c9d21afca0
  27. */
  28. #if !defined(MBEDTLS_CONFIG_FILE)
  29. #include "mbedtls/config.h"
  30. #else
  31. #include MBEDTLS_CONFIG_FILE
  32. #endif
  33. #if defined(MBEDTLS_ARC4_C)
  34. #include "mbedtls/arc4.h"
  35. #include <string.h>
  36. #if defined(MBEDTLS_SELF_TEST)
  37. #if defined(MBEDTLS_PLATFORM_C)
  38. #include "mbedtls/platform.h"
  39. #else
  40. #include <stdio.h>
  41. #define mbedtls_printf printf
  42. #endif /* MBEDTLS_PLATFORM_C */
  43. #endif /* MBEDTLS_SELF_TEST */
  44. #if !defined(MBEDTLS_ARC4_ALT)
  45. /* Implementation that should never be optimized out by the compiler */
  46. static void mbedtls_zeroize( void *v, size_t n ) {
  47. volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
  48. }
  49. void mbedtls_arc4_init( mbedtls_arc4_context *ctx )
  50. {
  51. memset( ctx, 0, sizeof( mbedtls_arc4_context ) );
  52. }
  53. void mbedtls_arc4_free( mbedtls_arc4_context *ctx )
  54. {
  55. if( ctx == NULL )
  56. return;
  57. mbedtls_zeroize( ctx, sizeof( mbedtls_arc4_context ) );
  58. }
  59. /*
  60. * ARC4 key schedule
  61. */
  62. void mbedtls_arc4_setup( mbedtls_arc4_context *ctx, const unsigned char *key,
  63. unsigned int keylen )
  64. {
  65. int i, j, a;
  66. unsigned int k;
  67. unsigned char *m;
  68. ctx->x = 0;
  69. ctx->y = 0;
  70. m = ctx->m;
  71. for( i = 0; i < 256; i++ )
  72. m[i] = (unsigned char) i;
  73. j = k = 0;
  74. for( i = 0; i < 256; i++, k++ )
  75. {
  76. if( k >= keylen ) k = 0;
  77. a = m[i];
  78. j = ( j + a + key[k] ) & 0xFF;
  79. m[i] = m[j];
  80. m[j] = (unsigned char) a;
  81. }
  82. }
  83. /*
  84. * ARC4 cipher function
  85. */
  86. int mbedtls_arc4_crypt( mbedtls_arc4_context *ctx, size_t length, const unsigned char *input,
  87. unsigned char *output )
  88. {
  89. int x, y, a, b;
  90. size_t i;
  91. unsigned char *m;
  92. x = ctx->x;
  93. y = ctx->y;
  94. m = ctx->m;
  95. for( i = 0; i < length; i++ )
  96. {
  97. x = ( x + 1 ) & 0xFF; a = m[x];
  98. y = ( y + a ) & 0xFF; b = m[y];
  99. m[x] = (unsigned char) b;
  100. m[y] = (unsigned char) a;
  101. output[i] = (unsigned char)
  102. ( input[i] ^ m[(unsigned char)( a + b )] );
  103. }
  104. ctx->x = x;
  105. ctx->y = y;
  106. return( 0 );
  107. }
  108. #endif /* !MBEDTLS_ARC4_ALT */
  109. #if defined(MBEDTLS_SELF_TEST)
  110. /*
  111. * ARC4 tests vectors as posted by Eric Rescorla in sep. 1994:
  112. *
  113. * http://groups.google.com/group/comp.security.misc/msg/10a300c9d21afca0
  114. */
  115. static const unsigned char arc4_test_key[3][8] =
  116. {
  117. { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
  118. { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
  119. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
  120. };
  121. static const unsigned char arc4_test_pt[3][8] =
  122. {
  123. { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
  124. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
  125. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
  126. };
  127. static const unsigned char arc4_test_ct[3][8] =
  128. {
  129. { 0x75, 0xB7, 0x87, 0x80, 0x99, 0xE0, 0xC5, 0x96 },
  130. { 0x74, 0x94, 0xC2, 0xE7, 0x10, 0x4B, 0x08, 0x79 },
  131. { 0xDE, 0x18, 0x89, 0x41, 0xA3, 0x37, 0x5D, 0x3A }
  132. };
  133. /*
  134. * Checkup routine
  135. */
  136. int mbedtls_arc4_self_test( int verbose )
  137. {
  138. int i, ret = 0;
  139. unsigned char ibuf[8];
  140. unsigned char obuf[8];
  141. mbedtls_arc4_context ctx;
  142. mbedtls_arc4_init( &ctx );
  143. for( i = 0; i < 3; i++ )
  144. {
  145. if( verbose != 0 )
  146. mbedtls_printf( " ARC4 test #%d: ", i + 1 );
  147. memcpy( ibuf, arc4_test_pt[i], 8 );
  148. mbedtls_arc4_setup( &ctx, arc4_test_key[i], 8 );
  149. mbedtls_arc4_crypt( &ctx, 8, ibuf, obuf );
  150. if( memcmp( obuf, arc4_test_ct[i], 8 ) != 0 )
  151. {
  152. if( verbose != 0 )
  153. mbedtls_printf( "failed\n" );
  154. ret = 1;
  155. goto exit;
  156. }
  157. if( verbose != 0 )
  158. mbedtls_printf( "passed\n" );
  159. }
  160. if( verbose != 0 )
  161. mbedtls_printf( "\n" );
  162. exit:
  163. mbedtls_arc4_free( &ctx );
  164. return( ret );
  165. }
  166. #endif /* MBEDTLS_SELF_TEST */
  167. #endif /* MBEDTLS_ARC4_C */