arc4.c 4.3 KB

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