arc4.c 4.7 KB

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