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