Advertisement
Guest User

openssl enc -rc4 is broken proof code

a guest
Jul 23rd, 2014
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.53 KB | None | 0 0
  1. /*
  2.     PoC application showing openssl enc -rc4 -K <8byte_key_hd> is broken
  3.     128-bit key HD is 0123456789abcdef
  4.    
  5.     # this file: rc4.c
  6.     $ gcc -o rc4_polarssl rc4.c
  7.     $ gcc -o rc4_openssl rc4.c -lcrypto -DOPENSSL
  8.     $ head -c 8 /dev/zero | openssl enc -rc4 -K 0123456789abcdef | hexdump -C
  9.     00000000  68 51 73 3e 92 f9 8f 5e                           |hQs>...^|
  10.     00000008
  11.     $ head -c 8 /dev/zero | ./rc4_polarssl | hexdump -C
  12.     00000000  74 94 c2 e7 10 4b 08 79                           |t....K.y|
  13.     00000008
  14.     $ head -c 8 /dev/zero | ./rc4_openssl | hexdump -C
  15.     00000000  74 94 c2 e7 10 4b 08 79                           |t....K.y|
  16.     00000008
  17.     $
  18.     # the above result of crypting 8 zero-bytes with key 0123456789abcdef (128 bit/8 bytes) matches proposed polarssl test vector.
  19. */
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <unistd.h>
  24.  
  25. #ifdef OPENSSL
  26. #include <openssl/rc4.h>
  27. #endif
  28.  
  29. #ifndef OPENSSL
  30. typedef struct
  31. {
  32.     int x;                      /*!< permutation index */
  33.     int y;                      /*!< permutation index */
  34.     unsigned char m[256];       /*!< permutation table */
  35. } arc4_context;
  36.  
  37. void arc4_setup( arc4_context *ctx, const unsigned char *key, unsigned int keylen );
  38. void arc4_crypt( arc4_context *ctx, size_t length, const unsigned char *input,unsigned char *output );
  39. int arc4_self_test( int verbose );
  40. #endif
  41.  
  42. int main() {
  43.     unsigned char * in =  alloca(4096);
  44.     unsigned char * out = alloca(4096);
  45.     int rr;
  46. #ifndef OPENSSL
  47.     arc4_context ctx;
  48. #else
  49.     RC4_KEY k;
  50. #endif
  51.    
  52.     //arc4_self_test(1);
  53.    
  54.     //arc4_setup(&ctx,"\x38\xe6\x89\x75\x49\x52\x5e\x82",8);
  55. #ifndef OPENSSL
  56.     arc4_setup(&ctx,"\x01\x23\x45\x67\x89\xab\xcd\xef",8);
  57. #else
  58.     RC4_set_key(&k,8,"\x01\x23\x45\x67\x89\xab\xcd\xef");
  59. #endif
  60.    
  61.     while ( 0 < ( rr = read(0,in,4096) ) ) {
  62. #ifndef OPENSSL
  63.         arc4_crypt(&ctx,rr,in,out);
  64. #else
  65.         RC4(&k,rr,in,out);
  66. #endif
  67.         if ( rr != write(1,out,rr) )
  68.             return 1;
  69.     }
  70.     return 0;
  71. }
  72.  
  73. #ifndef OPENSSL
  74. static const unsigned char arc4_test_key[3][8] =
  75. {
  76.     { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
  77.     { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
  78.     { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
  79. };
  80.  
  81. static const unsigned char arc4_test_pt[3][8] =
  82. {
  83.     { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
  84.     { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
  85.     { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
  86. };
  87.  
  88. static const unsigned char arc4_test_ct[3][8] =
  89. {
  90.     { 0x75, 0xB7, 0x87, 0x80, 0x99, 0xE0, 0xC5, 0x96 },
  91.     { 0x74, 0x94, 0xC2, 0xE7, 0x10, 0x4B, 0x08, 0x79 },
  92.     { 0xDE, 0x18, 0x89, 0x41, 0xA3, 0x37, 0x5D, 0x3A }
  93. };
  94.  
  95. int arc4_self_test( int verbose )
  96. {
  97.     int i;
  98.     unsigned char ibuf[8];
  99.     unsigned char obuf[8];
  100.     arc4_context ctx;
  101.  
  102.     for( i = 0; i < 3; i++ )
  103.     {
  104.         if( verbose != 0 )
  105.             printf( "  ARC4 test #%d: ", i + 1 );
  106.  
  107.         memcpy( ibuf, arc4_test_pt[i], 8 );
  108.  
  109.         arc4_setup( &ctx, (unsigned char *) arc4_test_key[i], 8 );
  110.         arc4_crypt( &ctx, 8, ibuf, obuf );
  111.  
  112.         if( memcmp( obuf, arc4_test_ct[i], 8 ) != 0 )
  113.         {
  114.             if( verbose != 0 )
  115.                 printf( "failed\n" );
  116.  
  117.             return( 1 );
  118.         }
  119.  
  120.         if( verbose != 0 )
  121.             printf( "passed\n" );
  122.     }
  123.  
  124.     if( verbose != 0 )
  125.         printf( "\n" );
  126.  
  127.     return( 0 );
  128. }
  129.  
  130. void arc4_setup( arc4_context *ctx, const unsigned char *key, unsigned int keylen )
  131. {
  132.     int i, j, a;
  133.     unsigned int k;
  134.     unsigned char *m;
  135.  
  136.     ctx->x = 0;
  137.     ctx->y = 0;
  138.     m = ctx->m;
  139.  
  140.     for( i = 0; i < 256; i++ )
  141.         m[i] = (unsigned char) i;
  142.  
  143.     j = k = 0;
  144.  
  145.     for( i = 0; i < 256; i++, k++ )
  146.     {
  147.         if( k >= keylen ) k = 0;
  148.  
  149.         a = m[i];
  150.         j = ( j + a + key[k] ) & 0xFF;
  151.         m[i] = m[j];
  152.         m[j] = (unsigned char) a;
  153.     }
  154. }
  155.  
  156. void arc4_crypt( arc4_context *ctx, size_t length, const unsigned char *input,
  157.                 unsigned char *output )
  158. {
  159.     int x, y, a, b;
  160.     size_t i;
  161.     unsigned char *m;
  162.  
  163.     x = ctx->x;
  164.     y = ctx->y;
  165.     m = ctx->m;
  166.  
  167.     for( i = 0; i < length; i++ )
  168.     {
  169.         x = ( x + 1 ) & 0xFF; a = m[x];
  170.         y = ( y + a ) & 0xFF; b = m[y];
  171.  
  172.         m[x] = (unsigned char) b;
  173.         m[y] = (unsigned char) a;
  174.  
  175.         output[i] = (unsigned char)
  176.             ( input[i] ^ m[(unsigned char)( a + b )] );
  177.     }
  178.  
  179.     ctx->x = x;
  180.     ctx->y = y;
  181. }
  182.  
  183. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement