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