Hannes Tschofenig
/
aes-gcm-test-program
Example program to test AES-GCM functionality. Used for a workshop
SSL/include/polarssl/xtea.h@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 | * \file xtea.h |
HannesTschofenig | 0:796d0f61a05b | 3 | * |
HannesTschofenig | 0:796d0f61a05b | 4 | * \brief XTEA block cipher (32-bit) |
HannesTschofenig | 0:796d0f61a05b | 5 | * |
HannesTschofenig | 0:796d0f61a05b | 6 | * Copyright (C) 2006-2013, Brainspark B.V. |
HannesTschofenig | 0:796d0f61a05b | 7 | * |
HannesTschofenig | 0:796d0f61a05b | 8 | * This file is part of PolarSSL (http://www.polarssl.org) |
HannesTschofenig | 0:796d0f61a05b | 9 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> |
HannesTschofenig | 0:796d0f61a05b | 10 | * |
HannesTschofenig | 0:796d0f61a05b | 11 | * All rights reserved. |
HannesTschofenig | 0:796d0f61a05b | 12 | * |
HannesTschofenig | 0:796d0f61a05b | 13 | * This program is free software; you can redistribute it and/or modify |
HannesTschofenig | 0:796d0f61a05b | 14 | * it under the terms of the GNU General Public License as published by |
HannesTschofenig | 0:796d0f61a05b | 15 | * the Free Software Foundation; either version 2 of the License, or |
HannesTschofenig | 0:796d0f61a05b | 16 | * (at your option) any later version. |
HannesTschofenig | 0:796d0f61a05b | 17 | * |
HannesTschofenig | 0:796d0f61a05b | 18 | * This program is distributed in the hope that it will be useful, |
HannesTschofenig | 0:796d0f61a05b | 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
HannesTschofenig | 0:796d0f61a05b | 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
HannesTschofenig | 0:796d0f61a05b | 21 | * GNU General Public License for more details. |
HannesTschofenig | 0:796d0f61a05b | 22 | * |
HannesTschofenig | 0:796d0f61a05b | 23 | * You should have received a copy of the GNU General Public License along |
HannesTschofenig | 0:796d0f61a05b | 24 | * with this program; if not, write to the Free Software Foundation, Inc., |
HannesTschofenig | 0:796d0f61a05b | 25 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
HannesTschofenig | 0:796d0f61a05b | 26 | */ |
HannesTschofenig | 0:796d0f61a05b | 27 | #ifndef POLARSSL_XTEA_H |
HannesTschofenig | 0:796d0f61a05b | 28 | #define POLARSSL_XTEA_H |
HannesTschofenig | 0:796d0f61a05b | 29 | |
HannesTschofenig | 0:796d0f61a05b | 30 | #if !defined(POLARSSL_CONFIG_FILE) |
HannesTschofenig | 0:796d0f61a05b | 31 | #include "config.h" |
HannesTschofenig | 0:796d0f61a05b | 32 | #else |
HannesTschofenig | 0:796d0f61a05b | 33 | #include POLARSSL_CONFIG_FILE |
HannesTschofenig | 0:796d0f61a05b | 34 | #endif |
HannesTschofenig | 0:796d0f61a05b | 35 | |
HannesTschofenig | 0:796d0f61a05b | 36 | #include <string.h> |
HannesTschofenig | 0:796d0f61a05b | 37 | |
HannesTschofenig | 0:796d0f61a05b | 38 | #if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32) |
HannesTschofenig | 0:796d0f61a05b | 39 | #include <basetsd.h> |
HannesTschofenig | 0:796d0f61a05b | 40 | typedef UINT32 uint32_t; |
HannesTschofenig | 0:796d0f61a05b | 41 | #else |
HannesTschofenig | 0:796d0f61a05b | 42 | #include <inttypes.h> |
HannesTschofenig | 0:796d0f61a05b | 43 | #endif |
HannesTschofenig | 0:796d0f61a05b | 44 | |
HannesTschofenig | 0:796d0f61a05b | 45 | #define XTEA_ENCRYPT 1 |
HannesTschofenig | 0:796d0f61a05b | 46 | #define XTEA_DECRYPT 0 |
HannesTschofenig | 0:796d0f61a05b | 47 | |
HannesTschofenig | 0:796d0f61a05b | 48 | #define POLARSSL_ERR_XTEA_INVALID_INPUT_LENGTH -0x0028 /**< The data input has an invalid length. */ |
HannesTschofenig | 0:796d0f61a05b | 49 | |
HannesTschofenig | 0:796d0f61a05b | 50 | #if !defined(POLARSSL_XTEA_ALT) |
HannesTschofenig | 0:796d0f61a05b | 51 | // Regular implementation |
HannesTschofenig | 0:796d0f61a05b | 52 | // |
HannesTschofenig | 0:796d0f61a05b | 53 | |
HannesTschofenig | 0:796d0f61a05b | 54 | #ifdef __cplusplus |
HannesTschofenig | 0:796d0f61a05b | 55 | extern "C" { |
HannesTschofenig | 0:796d0f61a05b | 56 | #endif |
HannesTschofenig | 0:796d0f61a05b | 57 | |
HannesTschofenig | 0:796d0f61a05b | 58 | /** |
HannesTschofenig | 0:796d0f61a05b | 59 | * \brief XTEA context structure |
HannesTschofenig | 0:796d0f61a05b | 60 | */ |
HannesTschofenig | 0:796d0f61a05b | 61 | typedef struct |
HannesTschofenig | 0:796d0f61a05b | 62 | { |
HannesTschofenig | 0:796d0f61a05b | 63 | uint32_t k[4]; /*!< key */ |
HannesTschofenig | 0:796d0f61a05b | 64 | } |
HannesTschofenig | 0:796d0f61a05b | 65 | xtea_context; |
HannesTschofenig | 0:796d0f61a05b | 66 | |
HannesTschofenig | 0:796d0f61a05b | 67 | /** |
HannesTschofenig | 0:796d0f61a05b | 68 | * \brief XTEA key schedule |
HannesTschofenig | 0:796d0f61a05b | 69 | * |
HannesTschofenig | 0:796d0f61a05b | 70 | * \param ctx XTEA context to be initialized |
HannesTschofenig | 0:796d0f61a05b | 71 | * \param key the secret key |
HannesTschofenig | 0:796d0f61a05b | 72 | */ |
HannesTschofenig | 0:796d0f61a05b | 73 | void xtea_setup( xtea_context *ctx, const unsigned char key[16] ); |
HannesTschofenig | 0:796d0f61a05b | 74 | |
HannesTschofenig | 0:796d0f61a05b | 75 | /** |
HannesTschofenig | 0:796d0f61a05b | 76 | * \brief XTEA cipher function |
HannesTschofenig | 0:796d0f61a05b | 77 | * |
HannesTschofenig | 0:796d0f61a05b | 78 | * \param ctx XTEA context |
HannesTschofenig | 0:796d0f61a05b | 79 | * \param mode XTEA_ENCRYPT or XTEA_DECRYPT |
HannesTschofenig | 0:796d0f61a05b | 80 | * \param input 8-byte input block |
HannesTschofenig | 0:796d0f61a05b | 81 | * \param output 8-byte output block |
HannesTschofenig | 0:796d0f61a05b | 82 | * |
HannesTschofenig | 0:796d0f61a05b | 83 | * \return 0 if successful |
HannesTschofenig | 0:796d0f61a05b | 84 | */ |
HannesTschofenig | 0:796d0f61a05b | 85 | int xtea_crypt_ecb( xtea_context *ctx, |
HannesTschofenig | 0:796d0f61a05b | 86 | int mode, |
HannesTschofenig | 0:796d0f61a05b | 87 | const unsigned char input[8], |
HannesTschofenig | 0:796d0f61a05b | 88 | unsigned char output[8] ); |
HannesTschofenig | 0:796d0f61a05b | 89 | |
HannesTschofenig | 0:796d0f61a05b | 90 | #if defined(POLARSSL_CIPHER_MODE_CBC) |
HannesTschofenig | 0:796d0f61a05b | 91 | /** |
HannesTschofenig | 0:796d0f61a05b | 92 | * \brief XTEA CBC cipher function |
HannesTschofenig | 0:796d0f61a05b | 93 | * |
HannesTschofenig | 0:796d0f61a05b | 94 | * \param ctx XTEA context |
HannesTschofenig | 0:796d0f61a05b | 95 | * \param mode XTEA_ENCRYPT or XTEA_DECRYPT |
HannesTschofenig | 0:796d0f61a05b | 96 | * \param length the length of input, multiple of 8 |
HannesTschofenig | 0:796d0f61a05b | 97 | * \param iv initialization vector for CBC mode |
HannesTschofenig | 0:796d0f61a05b | 98 | * \param input input block |
HannesTschofenig | 0:796d0f61a05b | 99 | * \param output output block |
HannesTschofenig | 0:796d0f61a05b | 100 | * |
HannesTschofenig | 0:796d0f61a05b | 101 | * \return 0 if successful, |
HannesTschofenig | 0:796d0f61a05b | 102 | * POLARSSL_ERR_XTEA_INVALID_INPUT_LENGTH if the length % 8 != 0 |
HannesTschofenig | 0:796d0f61a05b | 103 | */ |
HannesTschofenig | 0:796d0f61a05b | 104 | int xtea_crypt_cbc( xtea_context *ctx, |
HannesTschofenig | 0:796d0f61a05b | 105 | int mode, |
HannesTschofenig | 0:796d0f61a05b | 106 | size_t length, |
HannesTschofenig | 0:796d0f61a05b | 107 | unsigned char iv[8], |
HannesTschofenig | 0:796d0f61a05b | 108 | const unsigned char *input, |
HannesTschofenig | 0:796d0f61a05b | 109 | unsigned char *output); |
HannesTschofenig | 0:796d0f61a05b | 110 | #endif /* POLARSSL_CIPHER_MODE_CBC */ |
HannesTschofenig | 0:796d0f61a05b | 111 | |
HannesTschofenig | 0:796d0f61a05b | 112 | #ifdef __cplusplus |
HannesTschofenig | 0:796d0f61a05b | 113 | } |
HannesTschofenig | 0:796d0f61a05b | 114 | #endif |
HannesTschofenig | 0:796d0f61a05b | 115 | |
HannesTschofenig | 0:796d0f61a05b | 116 | #else /* POLARSSL_XTEA_ALT */ |
HannesTschofenig | 0:796d0f61a05b | 117 | #include "xtea_alt.h" |
HannesTschofenig | 0:796d0f61a05b | 118 | #endif /* POLARSSL_XTEA_ALT */ |
HannesTschofenig | 0:796d0f61a05b | 119 | |
HannesTschofenig | 0:796d0f61a05b | 120 | #ifdef __cplusplus |
HannesTschofenig | 0:796d0f61a05b | 121 | extern "C" { |
HannesTschofenig | 0:796d0f61a05b | 122 | #endif |
HannesTschofenig | 0:796d0f61a05b | 123 | |
HannesTschofenig | 0:796d0f61a05b | 124 | /** |
HannesTschofenig | 0:796d0f61a05b | 125 | * \brief Checkup routine |
HannesTschofenig | 0:796d0f61a05b | 126 | * |
HannesTschofenig | 0:796d0f61a05b | 127 | * \return 0 if successful, or 1 if the test failed |
HannesTschofenig | 0:796d0f61a05b | 128 | */ |
HannesTschofenig | 0:796d0f61a05b | 129 | int xtea_self_test( int verbose ); |
HannesTschofenig | 0:796d0f61a05b | 130 | |
HannesTschofenig | 0:796d0f61a05b | 131 | #ifdef __cplusplus |
HannesTschofenig | 0:796d0f61a05b | 132 | } |
HannesTschofenig | 0:796d0f61a05b | 133 | #endif |
HannesTschofenig | 0:796d0f61a05b | 134 | |
HannesTschofenig | 0:796d0f61a05b | 135 | #endif /* xtea.h */ |
HannesTschofenig | 0:796d0f61a05b | 136 | |
HannesTschofenig | 0:796d0f61a05b | 137 |