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