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