Hannes Tschofenig
/
aes-gcm-test-program
Example program to test AES-GCM functionality. Used for a workshop
SSL/library/arc4.c@0:796d0f61a05b, 2018-09-27 (annotated)
- Committer:
- HannesTschofenig
- Date:
- Thu Sep 27 06:34:22 2018 +0000
- Revision:
- 0:796d0f61a05b
Example AES-GCM test program
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
HannesTschofenig | 0:796d0f61a05b | 1 | /* |
HannesTschofenig | 0:796d0f61a05b | 2 | * An implementation of the ARCFOUR algorithm |
HannesTschofenig | 0:796d0f61a05b | 3 | * |
HannesTschofenig | 0:796d0f61a05b | 4 | * Copyright (C) 2006-2014, Brainspark B.V. |
HannesTschofenig | 0:796d0f61a05b | 5 | * |
HannesTschofenig | 0:796d0f61a05b | 6 | * This file is part of PolarSSL (http://www.polarssl.org) |
HannesTschofenig | 0:796d0f61a05b | 7 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> |
HannesTschofenig | 0:796d0f61a05b | 8 | * |
HannesTschofenig | 0:796d0f61a05b | 9 | * All rights reserved. |
HannesTschofenig | 0:796d0f61a05b | 10 | * |
HannesTschofenig | 0:796d0f61a05b | 11 | * This program is free software; you can redistribute it and/or modify |
HannesTschofenig | 0:796d0f61a05b | 12 | * it under the terms of the GNU General Public License as published by |
HannesTschofenig | 0:796d0f61a05b | 13 | * the Free Software Foundation; either version 2 of the License, or |
HannesTschofenig | 0:796d0f61a05b | 14 | * (at your option) any later version. |
HannesTschofenig | 0:796d0f61a05b | 15 | * |
HannesTschofenig | 0:796d0f61a05b | 16 | * This program is distributed in the hope that it will be useful, |
HannesTschofenig | 0:796d0f61a05b | 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
HannesTschofenig | 0:796d0f61a05b | 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
HannesTschofenig | 0:796d0f61a05b | 19 | * GNU General Public License for more details. |
HannesTschofenig | 0:796d0f61a05b | 20 | * |
HannesTschofenig | 0:796d0f61a05b | 21 | * You should have received a copy of the GNU General Public License along |
HannesTschofenig | 0:796d0f61a05b | 22 | * with this program; if not, write to the Free Software Foundation, Inc., |
HannesTschofenig | 0:796d0f61a05b | 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
HannesTschofenig | 0:796d0f61a05b | 24 | */ |
HannesTschofenig | 0:796d0f61a05b | 25 | /* |
HannesTschofenig | 0:796d0f61a05b | 26 | * The ARCFOUR algorithm was publicly disclosed on 94/09. |
HannesTschofenig | 0:796d0f61a05b | 27 | * |
HannesTschofenig | 0:796d0f61a05b | 28 | * http://groups.google.com/group/sci.crypt/msg/10a300c9d21afca0 |
HannesTschofenig | 0:796d0f61a05b | 29 | */ |
HannesTschofenig | 0:796d0f61a05b | 30 | |
HannesTschofenig | 0:796d0f61a05b | 31 | #if !defined(POLARSSL_CONFIG_FILE) |
HannesTschofenig | 0:796d0f61a05b | 32 | #include "polarssl/config.h" |
HannesTschofenig | 0:796d0f61a05b | 33 | #else |
HannesTschofenig | 0:796d0f61a05b | 34 | #include POLARSSL_CONFIG_FILE |
HannesTschofenig | 0:796d0f61a05b | 35 | #endif |
HannesTschofenig | 0:796d0f61a05b | 36 | |
HannesTschofenig | 0:796d0f61a05b | 37 | #if defined(POLARSSL_ARC4_C) |
HannesTschofenig | 0:796d0f61a05b | 38 | |
HannesTschofenig | 0:796d0f61a05b | 39 | #include "polarssl/arc4.h" |
HannesTschofenig | 0:796d0f61a05b | 40 | |
HannesTschofenig | 0:796d0f61a05b | 41 | #if defined(POLARSSL_PLATFORM_C) |
HannesTschofenig | 0:796d0f61a05b | 42 | #include "polarssl/platform.h" |
HannesTschofenig | 0:796d0f61a05b | 43 | #else |
HannesTschofenig | 0:796d0f61a05b | 44 | #define polarssl_printf printf |
HannesTschofenig | 0:796d0f61a05b | 45 | #endif |
HannesTschofenig | 0:796d0f61a05b | 46 | |
HannesTschofenig | 0:796d0f61a05b | 47 | #if !defined(POLARSSL_ARC4_ALT) |
HannesTschofenig | 0:796d0f61a05b | 48 | |
HannesTschofenig | 0:796d0f61a05b | 49 | /* |
HannesTschofenig | 0:796d0f61a05b | 50 | * ARC4 key schedule |
HannesTschofenig | 0:796d0f61a05b | 51 | */ |
HannesTschofenig | 0:796d0f61a05b | 52 | void arc4_setup( arc4_context *ctx, const unsigned char *key, |
HannesTschofenig | 0:796d0f61a05b | 53 | unsigned int keylen ) |
HannesTschofenig | 0:796d0f61a05b | 54 | { |
HannesTschofenig | 0:796d0f61a05b | 55 | int i, j, a; |
HannesTschofenig | 0:796d0f61a05b | 56 | unsigned int k; |
HannesTschofenig | 0:796d0f61a05b | 57 | unsigned char *m; |
HannesTschofenig | 0:796d0f61a05b | 58 | |
HannesTschofenig | 0:796d0f61a05b | 59 | ctx->x = 0; |
HannesTschofenig | 0:796d0f61a05b | 60 | ctx->y = 0; |
HannesTschofenig | 0:796d0f61a05b | 61 | m = ctx->m; |
HannesTschofenig | 0:796d0f61a05b | 62 | |
HannesTschofenig | 0:796d0f61a05b | 63 | for( i = 0; i < 256; i++ ) |
HannesTschofenig | 0:796d0f61a05b | 64 | m[i] = (unsigned char) i; |
HannesTschofenig | 0:796d0f61a05b | 65 | |
HannesTschofenig | 0:796d0f61a05b | 66 | j = k = 0; |
HannesTschofenig | 0:796d0f61a05b | 67 | |
HannesTschofenig | 0:796d0f61a05b | 68 | for( i = 0; i < 256; i++, k++ ) |
HannesTschofenig | 0:796d0f61a05b | 69 | { |
HannesTschofenig | 0:796d0f61a05b | 70 | if( k >= keylen ) k = 0; |
HannesTschofenig | 0:796d0f61a05b | 71 | |
HannesTschofenig | 0:796d0f61a05b | 72 | a = m[i]; |
HannesTschofenig | 0:796d0f61a05b | 73 | j = ( j + a + key[k] ) & 0xFF; |
HannesTschofenig | 0:796d0f61a05b | 74 | m[i] = m[j]; |
HannesTschofenig | 0:796d0f61a05b | 75 | m[j] = (unsigned char) a; |
HannesTschofenig | 0:796d0f61a05b | 76 | } |
HannesTschofenig | 0:796d0f61a05b | 77 | } |
HannesTschofenig | 0:796d0f61a05b | 78 | |
HannesTschofenig | 0:796d0f61a05b | 79 | /* |
HannesTschofenig | 0:796d0f61a05b | 80 | * ARC4 cipher function |
HannesTschofenig | 0:796d0f61a05b | 81 | */ |
HannesTschofenig | 0:796d0f61a05b | 82 | int arc4_crypt( arc4_context *ctx, size_t length, const unsigned char *input, |
HannesTschofenig | 0:796d0f61a05b | 83 | unsigned char *output ) |
HannesTschofenig | 0:796d0f61a05b | 84 | { |
HannesTschofenig | 0:796d0f61a05b | 85 | int x, y, a, b; |
HannesTschofenig | 0:796d0f61a05b | 86 | size_t i; |
HannesTschofenig | 0:796d0f61a05b | 87 | unsigned char *m; |
HannesTschofenig | 0:796d0f61a05b | 88 | |
HannesTschofenig | 0:796d0f61a05b | 89 | x = ctx->x; |
HannesTschofenig | 0:796d0f61a05b | 90 | y = ctx->y; |
HannesTschofenig | 0:796d0f61a05b | 91 | m = ctx->m; |
HannesTschofenig | 0:796d0f61a05b | 92 | |
HannesTschofenig | 0:796d0f61a05b | 93 | for( i = 0; i < length; i++ ) |
HannesTschofenig | 0:796d0f61a05b | 94 | { |
HannesTschofenig | 0:796d0f61a05b | 95 | x = ( x + 1 ) & 0xFF; a = m[x]; |
HannesTschofenig | 0:796d0f61a05b | 96 | y = ( y + a ) & 0xFF; b = m[y]; |
HannesTschofenig | 0:796d0f61a05b | 97 | |
HannesTschofenig | 0:796d0f61a05b | 98 | m[x] = (unsigned char) b; |
HannesTschofenig | 0:796d0f61a05b | 99 | m[y] = (unsigned char) a; |
HannesTschofenig | 0:796d0f61a05b | 100 | |
HannesTschofenig | 0:796d0f61a05b | 101 | output[i] = (unsigned char) |
HannesTschofenig | 0:796d0f61a05b | 102 | ( input[i] ^ m[(unsigned char)( a + b )] ); |
HannesTschofenig | 0:796d0f61a05b | 103 | } |
HannesTschofenig | 0:796d0f61a05b | 104 | |
HannesTschofenig | 0:796d0f61a05b | 105 | ctx->x = x; |
HannesTschofenig | 0:796d0f61a05b | 106 | ctx->y = y; |
HannesTschofenig | 0:796d0f61a05b | 107 | |
HannesTschofenig | 0:796d0f61a05b | 108 | return( 0 ); |
HannesTschofenig | 0:796d0f61a05b | 109 | } |
HannesTschofenig | 0:796d0f61a05b | 110 | |
HannesTschofenig | 0:796d0f61a05b | 111 | #endif /* !POLARSSL_ARC4_ALT */ |
HannesTschofenig | 0:796d0f61a05b | 112 | |
HannesTschofenig | 0:796d0f61a05b | 113 | #if defined(POLARSSL_SELF_TEST) |
HannesTschofenig | 0:796d0f61a05b | 114 | |
HannesTschofenig | 0:796d0f61a05b | 115 | #include <string.h> |
HannesTschofenig | 0:796d0f61a05b | 116 | #include <stdio.h> |
HannesTschofenig | 0:796d0f61a05b | 117 | |
HannesTschofenig | 0:796d0f61a05b | 118 | /* |
HannesTschofenig | 0:796d0f61a05b | 119 | * ARC4 tests vectors as posted by Eric Rescorla in sep. 1994: |
HannesTschofenig | 0:796d0f61a05b | 120 | * |
HannesTschofenig | 0:796d0f61a05b | 121 | * http://groups.google.com/group/comp.security.misc/msg/10a300c9d21afca0 |
HannesTschofenig | 0:796d0f61a05b | 122 | */ |
HannesTschofenig | 0:796d0f61a05b | 123 | static const unsigned char arc4_test_key[3][8] = |
HannesTschofenig | 0:796d0f61a05b | 124 | { |
HannesTschofenig | 0:796d0f61a05b | 125 | { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF }, |
HannesTschofenig | 0:796d0f61a05b | 126 | { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF }, |
HannesTschofenig | 0:796d0f61a05b | 127 | { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } |
HannesTschofenig | 0:796d0f61a05b | 128 | }; |
HannesTschofenig | 0:796d0f61a05b | 129 | |
HannesTschofenig | 0:796d0f61a05b | 130 | static const unsigned char arc4_test_pt[3][8] = |
HannesTschofenig | 0:796d0f61a05b | 131 | { |
HannesTschofenig | 0:796d0f61a05b | 132 | { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF }, |
HannesTschofenig | 0:796d0f61a05b | 133 | { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, |
HannesTschofenig | 0:796d0f61a05b | 134 | { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } |
HannesTschofenig | 0:796d0f61a05b | 135 | }; |
HannesTschofenig | 0:796d0f61a05b | 136 | |
HannesTschofenig | 0:796d0f61a05b | 137 | static const unsigned char arc4_test_ct[3][8] = |
HannesTschofenig | 0:796d0f61a05b | 138 | { |
HannesTschofenig | 0:796d0f61a05b | 139 | { 0x75, 0xB7, 0x87, 0x80, 0x99, 0xE0, 0xC5, 0x96 }, |
HannesTschofenig | 0:796d0f61a05b | 140 | { 0x74, 0x94, 0xC2, 0xE7, 0x10, 0x4B, 0x08, 0x79 }, |
HannesTschofenig | 0:796d0f61a05b | 141 | { 0xDE, 0x18, 0x89, 0x41, 0xA3, 0x37, 0x5D, 0x3A } |
HannesTschofenig | 0:796d0f61a05b | 142 | }; |
HannesTschofenig | 0:796d0f61a05b | 143 | |
HannesTschofenig | 0:796d0f61a05b | 144 | /* |
HannesTschofenig | 0:796d0f61a05b | 145 | * Checkup routine |
HannesTschofenig | 0:796d0f61a05b | 146 | */ |
HannesTschofenig | 0:796d0f61a05b | 147 | int arc4_self_test( int verbose ) |
HannesTschofenig | 0:796d0f61a05b | 148 | { |
HannesTschofenig | 0:796d0f61a05b | 149 | int i; |
HannesTschofenig | 0:796d0f61a05b | 150 | unsigned char ibuf[8]; |
HannesTschofenig | 0:796d0f61a05b | 151 | unsigned char obuf[8]; |
HannesTschofenig | 0:796d0f61a05b | 152 | arc4_context ctx; |
HannesTschofenig | 0:796d0f61a05b | 153 | |
HannesTschofenig | 0:796d0f61a05b | 154 | for( i = 0; i < 3; i++ ) |
HannesTschofenig | 0:796d0f61a05b | 155 | { |
HannesTschofenig | 0:796d0f61a05b | 156 | if( verbose != 0 ) |
HannesTschofenig | 0:796d0f61a05b | 157 | polarssl_printf( " ARC4 test #%d: ", i + 1 ); |
HannesTschofenig | 0:796d0f61a05b | 158 | |
HannesTschofenig | 0:796d0f61a05b | 159 | memcpy( ibuf, arc4_test_pt[i], 8 ); |
HannesTschofenig | 0:796d0f61a05b | 160 | |
HannesTschofenig | 0:796d0f61a05b | 161 | arc4_setup( &ctx, arc4_test_key[i], 8 ); |
HannesTschofenig | 0:796d0f61a05b | 162 | arc4_crypt( &ctx, 8, ibuf, obuf ); |
HannesTschofenig | 0:796d0f61a05b | 163 | |
HannesTschofenig | 0:796d0f61a05b | 164 | if( memcmp( obuf, arc4_test_ct[i], 8 ) != 0 ) |
HannesTschofenig | 0:796d0f61a05b | 165 | { |
HannesTschofenig | 0:796d0f61a05b | 166 | if( verbose != 0 ) |
HannesTschofenig | 0:796d0f61a05b | 167 | polarssl_printf( "failed\n" ); |
HannesTschofenig | 0:796d0f61a05b | 168 | |
HannesTschofenig | 0:796d0f61a05b | 169 | return( 1 ); |
HannesTschofenig | 0:796d0f61a05b | 170 | } |
HannesTschofenig | 0:796d0f61a05b | 171 | |
HannesTschofenig | 0:796d0f61a05b | 172 | if( verbose != 0 ) |
HannesTschofenig | 0:796d0f61a05b | 173 | polarssl_printf( "passed\n" ); |
HannesTschofenig | 0:796d0f61a05b | 174 | } |
HannesTschofenig | 0:796d0f61a05b | 175 | |
HannesTschofenig | 0:796d0f61a05b | 176 | if( verbose != 0 ) |
HannesTschofenig | 0:796d0f61a05b | 177 | polarssl_printf( "\n" ); |
HannesTschofenig | 0:796d0f61a05b | 178 | |
HannesTschofenig | 0:796d0f61a05b | 179 | return( 0 ); |
HannesTschofenig | 0:796d0f61a05b | 180 | } |
HannesTschofenig | 0:796d0f61a05b | 181 | |
HannesTschofenig | 0:796d0f61a05b | 182 | #endif /* POLARSSL_SELF_TEST */ |
HannesTschofenig | 0:796d0f61a05b | 183 | |
HannesTschofenig | 0:796d0f61a05b | 184 | #endif /* POLARSSL_ARC4_C */ |
HannesTschofenig | 0:796d0f61a05b | 185 | |
HannesTschofenig | 0:796d0f61a05b | 186 |