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