]>
git.zerfleddert.de Git - proxmark3-svn/blob - common/mbedtls/arc4.c
2 * An implementation of the ARCFOUR algorithm
4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: GPL-2.0
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.
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.
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.
21 * This file is part of mbed TLS (https://tls.mbed.org)
24 * The ARCFOUR algorithm was publicly disclosed on 94/09.
26 * http://groups.google.com/group/sci.crypt/msg/10a300c9d21afca0
29 #if !defined(MBEDTLS_CONFIG_FILE)
30 #include "mbedtls/config.h"
32 #include MBEDTLS_CONFIG_FILE
35 #if defined(MBEDTLS_ARC4_C)
37 #include "mbedtls/arc4.h"
38 #include "mbedtls/platform_util.h"
42 #if defined(MBEDTLS_SELF_TEST)
43 #if defined(MBEDTLS_PLATFORM_C)
44 #include "mbedtls/platform.h"
47 #define mbedtls_printf printf
48 #endif /* MBEDTLS_PLATFORM_C */
49 #endif /* MBEDTLS_SELF_TEST */
51 #if !defined(MBEDTLS_ARC4_ALT)
53 void mbedtls_arc4_init( mbedtls_arc4_context
*ctx
)
55 memset( ctx
, 0, sizeof( mbedtls_arc4_context
) );
58 void mbedtls_arc4_free( mbedtls_arc4_context
*ctx
)
63 mbedtls_platform_zeroize( ctx
, sizeof( mbedtls_arc4_context
) );
69 void mbedtls_arc4_setup( mbedtls_arc4_context
*ctx
, const unsigned char *key
,
80 for( i
= 0; i
< 256; i
++ )
81 m
[i
] = (unsigned char) i
;
85 for( i
= 0; i
< 256; i
++, k
++ )
87 if( k
>= keylen
) k
= 0;
90 j
= ( j
+ a
+ key
[k
] ) & 0xFF;
92 m
[j
] = (unsigned char) a
;
97 * ARC4 cipher function
99 int mbedtls_arc4_crypt( mbedtls_arc4_context
*ctx
, size_t length
, const unsigned char *input
,
100 unsigned char *output
)
110 for( i
= 0; i
< length
; i
++ )
112 x
= ( x
+ 1 ) & 0xFF; a
= m
[x
];
113 y
= ( y
+ a
) & 0xFF; b
= m
[y
];
115 m
[x
] = (unsigned char) b
;
116 m
[y
] = (unsigned char) a
;
118 output
[i
] = (unsigned char)
119 ( input
[i
] ^ m
[(unsigned char)( a
+ b
)] );
128 #endif /* !MBEDTLS_ARC4_ALT */
130 #if defined(MBEDTLS_SELF_TEST)
132 * ARC4 tests vectors as posted by Eric Rescorla in sep. 1994:
134 * http://groups.google.com/group/comp.security.misc/msg/10a300c9d21afca0
136 static const unsigned char arc4_test_key
[3][8] =
138 { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
139 { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
140 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
143 static const unsigned char arc4_test_pt
[3][8] =
145 { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
146 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
147 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
150 static const unsigned char arc4_test_ct
[3][8] =
152 { 0x75, 0xB7, 0x87, 0x80, 0x99, 0xE0, 0xC5, 0x96 },
153 { 0x74, 0x94, 0xC2, 0xE7, 0x10, 0x4B, 0x08, 0x79 },
154 { 0xDE, 0x18, 0x89, 0x41, 0xA3, 0x37, 0x5D, 0x3A }
160 int mbedtls_arc4_self_test( int verbose
)
163 unsigned char ibuf
[8];
164 unsigned char obuf
[8];
165 mbedtls_arc4_context ctx
;
167 mbedtls_arc4_init( &ctx
);
169 for( i
= 0; i
< 3; i
++ )
172 mbedtls_printf( " ARC4 test #%d: ", i
+ 1 );
174 memcpy( ibuf
, arc4_test_pt
[i
], 8 );
176 mbedtls_arc4_setup( &ctx
, arc4_test_key
[i
], 8 );
177 mbedtls_arc4_crypt( &ctx
, 8, ibuf
, obuf
);
179 if( memcmp( obuf
, arc4_test_ct
[i
], 8 ) != 0 )
182 mbedtls_printf( "failed\n" );
189 mbedtls_printf( "passed\n" );
193 mbedtls_printf( "\n" );
196 mbedtls_arc4_free( &ctx
);
201 #endif /* MBEDTLS_SELF_TEST */
203 #endif /* MBEDTLS_ARC4_C */