2009-01-03 22:22:43 +01:00
|
|
|
/*
|
|
|
|
* An implementation of the ARCFOUR algorithm
|
|
|
|
*
|
2020-08-19 16:54:51 +02:00
|
|
|
* Copyright The Mbed TLS Contributors
|
Update license headers to Apache-2.0 OR GPL-2.0-or-later
This will allow us to ship the LTS branches in a single archive
This commit was generated using the following script:
# ========================
#!/bin/sh
header1='\ * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later\
*\
* This file is provided under the Apache License 2.0, or the\
* GNU General Public License v2.0 or later.\
*\
* **********\
* Apache License 2.0:\
*\
* Licensed under the Apache License, Version 2.0 (the "License"); you may\
* not use this file except in compliance with the License.\
* You may obtain a copy of the License at\
*\
* http://www.apache.org/licenses/LICENSE-2.0\
*\
* Unless required by applicable law or agreed to in writing, software\
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT\
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\
* See the License for the specific language governing permissions and\
* limitations under the License.\
*\
* **********\
*\
* **********\
* GNU General Public License v2.0 or later:\
*\
* This program is free software; you can redistribute it and/or modify\
* it under the terms of the GNU General Public License as published by\
* the Free Software Foundation; either version 2 of the License, or\
* (at your option) any later version.\
*\
* This program is distributed in the hope that it will be useful,\
* but WITHOUT ANY WARRANTY; without even the implied warranty of\
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\
* GNU General Public License for more details.\
*\
* You should have received a copy of the GNU General Public License along\
* with this program; if not, write to the Free Software Foundation, Inc.,\
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.\
*\
* **********'
find -path './.git' -prune -o '(' -name '*.c' -o -name '*.cpp' -o -name '*.fmt' -o -name '*.h' ')' -print | xargs sed -i "
# Normalize the first line of the copyright headers (no text on the first line of a block comment)
/^\/\*.*Copyright.*Arm/I s/\/\*/&\n */
# Insert new copyright header
/SPDX-License-Identifier/ i\
$header1
# Delete old copyright header
/SPDX-License-Identifier/,$ {
# Delete lines until the one preceding the mbedtls declaration
N
1,/This file is part of/ {
/This file is part of/! D
}
}
"
# Format copyright header for inclusion into scripts
header2=$(echo "$header1" | sed 's/^\\\? \* \?/#/')
find -path './.git' -prune -o '(' -name '*.gdb' -o -name '*.pl' -o -name '*.py' -o -name '*.sh' ')' -print | xargs sed -i "
# Insert new copyright header
/SPDX-License-Identifier/ i\
$header2
# Delete old copyright header
/SPDX-License-Identifier/,$ {
# Delete lines until the one preceding the mbedtls declaration
N
1,/This file is part of/ {
/This file is part of/! D
}
}
"
# ========================
Signed-off-by: Bence Szépkúti <bence.szepkuti@arm.com>
2020-06-05 13:02:18 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
|
|
|
|
*
|
|
|
|
* This file is provided under the Apache License 2.0, or the
|
|
|
|
* GNU General Public License v2.0 or later.
|
|
|
|
*
|
|
|
|
* **********
|
|
|
|
* Apache License 2.0:
|
2010-07-18 22:36:00 +02:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
* not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2010-07-18 22:36:00 +02:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2009-01-03 22:22:43 +01:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2009-01-03 22:22:43 +01:00
|
|
|
*
|
Update license headers to Apache-2.0 OR GPL-2.0-or-later
This will allow us to ship the LTS branches in a single archive
This commit was generated using the following script:
# ========================
#!/bin/sh
header1='\ * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later\
*\
* This file is provided under the Apache License 2.0, or the\
* GNU General Public License v2.0 or later.\
*\
* **********\
* Apache License 2.0:\
*\
* Licensed under the Apache License, Version 2.0 (the "License"); you may\
* not use this file except in compliance with the License.\
* You may obtain a copy of the License at\
*\
* http://www.apache.org/licenses/LICENSE-2.0\
*\
* Unless required by applicable law or agreed to in writing, software\
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT\
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\
* See the License for the specific language governing permissions and\
* limitations under the License.\
*\
* **********\
*\
* **********\
* GNU General Public License v2.0 or later:\
*\
* This program is free software; you can redistribute it and/or modify\
* it under the terms of the GNU General Public License as published by\
* the Free Software Foundation; either version 2 of the License, or\
* (at your option) any later version.\
*\
* This program is distributed in the hope that it will be useful,\
* but WITHOUT ANY WARRANTY; without even the implied warranty of\
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\
* GNU General Public License for more details.\
*\
* You should have received a copy of the GNU General Public License along\
* with this program; if not, write to the Free Software Foundation, Inc.,\
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.\
*\
* **********'
find -path './.git' -prune -o '(' -name '*.c' -o -name '*.cpp' -o -name '*.fmt' -o -name '*.h' ')' -print | xargs sed -i "
# Normalize the first line of the copyright headers (no text on the first line of a block comment)
/^\/\*.*Copyright.*Arm/I s/\/\*/&\n */
# Insert new copyright header
/SPDX-License-Identifier/ i\
$header1
# Delete old copyright header
/SPDX-License-Identifier/,$ {
# Delete lines until the one preceding the mbedtls declaration
N
1,/This file is part of/ {
/This file is part of/! D
}
}
"
# Format copyright header for inclusion into scripts
header2=$(echo "$header1" | sed 's/^\\\? \* \?/#/')
find -path './.git' -prune -o '(' -name '*.gdb' -o -name '*.pl' -o -name '*.py' -o -name '*.sh' ')' -print | xargs sed -i "
# Insert new copyright header
/SPDX-License-Identifier/ i\
$header2
# Delete old copyright header
/SPDX-License-Identifier/,$ {
# Delete lines until the one preceding the mbedtls declaration
N
1,/This file is part of/ {
/This file is part of/! D
}
}
"
# ========================
Signed-off-by: Bence Szépkúti <bence.szepkuti@arm.com>
2020-06-05 13:02:18 +02:00
|
|
|
* **********
|
|
|
|
*
|
|
|
|
* **********
|
|
|
|
* GNU General Public License v2.0 or later:
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
|
|
|
* **********
|
2009-01-03 22:22:43 +01:00
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* The ARCFOUR algorithm was publicly disclosed on 94/09.
|
|
|
|
*
|
|
|
|
* http://groups.google.com/group/sci.crypt/msg/10a300c9d21afca0
|
|
|
|
*/
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if !defined(MBEDTLS_CONFIG_FILE)
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/config.h"
|
2014-04-29 12:39:06 +02:00
|
|
|
#else
|
2015-04-08 12:49:31 +02:00
|
|
|
#include MBEDTLS_CONFIG_FILE
|
2014-04-29 12:39:06 +02:00
|
|
|
#endif
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_ARC4_C)
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/arc4.h"
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2015-02-06 14:43:58 +01:00
|
|
|
#include <string.h>
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_SELF_TEST)
|
|
|
|
#if defined(MBEDTLS_PLATFORM_C)
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/platform.h"
|
2014-02-01 22:50:26 +01:00
|
|
|
#else
|
2015-02-06 14:43:58 +01:00
|
|
|
#include <stdio.h>
|
2015-04-08 12:49:31 +02:00
|
|
|
#define mbedtls_printf printf
|
|
|
|
#endif /* MBEDTLS_PLATFORM_C */
|
|
|
|
#endif /* MBEDTLS_SELF_TEST */
|
2014-02-01 22:50:26 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if !defined(MBEDTLS_ARC4_ALT)
|
2013-06-24 19:20:35 +02:00
|
|
|
|
2014-06-18 11:12:03 +02:00
|
|
|
/* Implementation that should never be optimized out by the compiler */
|
2015-04-08 12:49:31 +02:00
|
|
|
static void mbedtls_zeroize( void *v, size_t n ) {
|
2016-05-20 01:00:37 +02:00
|
|
|
volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
|
2014-06-18 11:12:03 +02:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
void mbedtls_arc4_init( mbedtls_arc4_context *ctx )
|
2014-06-18 11:12:03 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
memset( ctx, 0, sizeof( mbedtls_arc4_context ) );
|
2014-06-18 11:12:03 +02:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
void mbedtls_arc4_free( mbedtls_arc4_context *ctx )
|
2014-06-18 11:12:03 +02:00
|
|
|
{
|
|
|
|
if( ctx == NULL )
|
|
|
|
return;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_zeroize( ctx, sizeof( mbedtls_arc4_context ) );
|
2014-06-18 11:12:03 +02:00
|
|
|
}
|
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
/*
|
|
|
|
* ARC4 key schedule
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
void mbedtls_arc4_setup( mbedtls_arc4_context *ctx, const unsigned char *key,
|
2014-05-01 14:18:25 +02:00
|
|
|
unsigned int keylen )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2011-04-24 10:57:21 +02:00
|
|
|
int i, j, a;
|
|
|
|
unsigned int k;
|
2009-01-03 22:22:43 +01:00
|
|
|
unsigned char *m;
|
|
|
|
|
|
|
|
ctx->x = 0;
|
|
|
|
ctx->y = 0;
|
|
|
|
m = ctx->m;
|
|
|
|
|
|
|
|
for( i = 0; i < 256; i++ )
|
|
|
|
m[i] = (unsigned char) i;
|
|
|
|
|
|
|
|
j = k = 0;
|
|
|
|
|
|
|
|
for( i = 0; i < 256; i++, k++ )
|
|
|
|
{
|
|
|
|
if( k >= keylen ) k = 0;
|
|
|
|
|
|
|
|
a = m[i];
|
|
|
|
j = ( j + a + key[k] ) & 0xFF;
|
|
|
|
m[i] = m[j];
|
|
|
|
m[j] = (unsigned char) a;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ARC4 cipher function
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_arc4_crypt( mbedtls_arc4_context *ctx, size_t length, const unsigned char *input,
|
2010-03-21 16:42:15 +01:00
|
|
|
unsigned char *output )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2011-04-24 10:57:21 +02:00
|
|
|
int x, y, a, b;
|
|
|
|
size_t i;
|
2009-01-03 22:22:43 +01:00
|
|
|
unsigned char *m;
|
|
|
|
|
|
|
|
x = ctx->x;
|
|
|
|
y = ctx->y;
|
|
|
|
m = ctx->m;
|
|
|
|
|
2010-03-21 16:42:15 +01:00
|
|
|
for( i = 0; i < length; i++ )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
|
|
|
x = ( x + 1 ) & 0xFF; a = m[x];
|
|
|
|
y = ( y + a ) & 0xFF; b = m[y];
|
|
|
|
|
|
|
|
m[x] = (unsigned char) b;
|
|
|
|
m[y] = (unsigned char) a;
|
|
|
|
|
2010-03-21 16:42:15 +01:00
|
|
|
output[i] = (unsigned char)
|
|
|
|
( input[i] ^ m[(unsigned char)( a + b )] );
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx->x = x;
|
|
|
|
ctx->y = y;
|
2010-03-18 22:21:02 +01:00
|
|
|
|
|
|
|
return( 0 );
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* !MBEDTLS_ARC4_ALT */
|
2013-06-24 19:20:35 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_SELF_TEST)
|
2009-01-03 22:22:43 +01:00
|
|
|
/*
|
|
|
|
* ARC4 tests vectors as posted by Eric Rescorla in sep. 1994:
|
|
|
|
*
|
|
|
|
* http://groups.google.com/group/comp.security.misc/msg/10a300c9d21afca0
|
|
|
|
*/
|
|
|
|
static const unsigned char arc4_test_key[3][8] =
|
|
|
|
{
|
|
|
|
{ 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
|
|
|
|
{ 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
|
|
|
|
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const unsigned char arc4_test_pt[3][8] =
|
|
|
|
{
|
|
|
|
{ 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
|
|
|
|
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
|
|
|
|
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const unsigned char arc4_test_ct[3][8] =
|
|
|
|
{
|
|
|
|
{ 0x75, 0xB7, 0x87, 0x80, 0x99, 0xE0, 0xC5, 0x96 },
|
|
|
|
{ 0x74, 0x94, 0xC2, 0xE7, 0x10, 0x4B, 0x08, 0x79 },
|
|
|
|
{ 0xDE, 0x18, 0x89, 0x41, 0xA3, 0x37, 0x5D, 0x3A }
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Checkup routine
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_arc4_self_test( int verbose )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2014-06-18 11:12:03 +02:00
|
|
|
int i, ret = 0;
|
2010-03-21 16:42:15 +01:00
|
|
|
unsigned char ibuf[8];
|
|
|
|
unsigned char obuf[8];
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_arc4_context ctx;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_arc4_init( &ctx );
|
2014-06-18 11:12:03 +02:00
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
for( i = 0; i < 3; i++ )
|
|
|
|
{
|
|
|
|
if( verbose != 0 )
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_printf( " ARC4 test #%d: ", i + 1 );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2010-03-21 16:42:15 +01:00
|
|
|
memcpy( ibuf, arc4_test_pt[i], 8 );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_arc4_setup( &ctx, arc4_test_key[i], 8 );
|
|
|
|
mbedtls_arc4_crypt( &ctx, 8, ibuf, obuf );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2010-03-21 16:42:15 +01:00
|
|
|
if( memcmp( obuf, arc4_test_ct[i], 8 ) != 0 )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
|
|
|
if( verbose != 0 )
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_printf( "failed\n" );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2014-06-18 11:12:03 +02:00
|
|
|
ret = 1;
|
|
|
|
goto exit;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if( verbose != 0 )
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_printf( "passed\n" );
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if( verbose != 0 )
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_printf( "\n" );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2014-06-18 11:12:03 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_arc4_free( &ctx );
|
2014-06-18 11:12:03 +02:00
|
|
|
|
|
|
|
return( ret );
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_SELF_TEST */
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_ARC4_C */
|