mbed TLS library
Dependents: HTTPClient-SSL WS_SERVER
include/polarssl/xtea.h@0:137634ff4186, 2015-06-11 (annotated)
- Committer:
- ansond
- Date:
- Thu Jun 11 03:27:03 2015 +0000
- Revision:
- 0:137634ff4186
initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ansond | 0:137634ff4186 | 1 | /** |
ansond | 0:137634ff4186 | 2 | * \file xtea.h |
ansond | 0:137634ff4186 | 3 | * |
ansond | 0:137634ff4186 | 4 | * \brief XTEA block cipher (32-bit) |
ansond | 0:137634ff4186 | 5 | * |
ansond | 0:137634ff4186 | 6 | * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved |
ansond | 0:137634ff4186 | 7 | * |
ansond | 0:137634ff4186 | 8 | * This file is part of mbed TLS (https://tls.mbed.org) |
ansond | 0:137634ff4186 | 9 | * |
ansond | 0:137634ff4186 | 10 | * This program is free software; you can redistribute it and/or modify |
ansond | 0:137634ff4186 | 11 | * it under the terms of the GNU General Public License as published by |
ansond | 0:137634ff4186 | 12 | * the Free Software Foundation; either version 2 of the License, or |
ansond | 0:137634ff4186 | 13 | * (at your option) any later version. |
ansond | 0:137634ff4186 | 14 | * |
ansond | 0:137634ff4186 | 15 | * This program is distributed in the hope that it will be useful, |
ansond | 0:137634ff4186 | 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
ansond | 0:137634ff4186 | 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
ansond | 0:137634ff4186 | 18 | * GNU General Public License for more details. |
ansond | 0:137634ff4186 | 19 | * |
ansond | 0:137634ff4186 | 20 | * You should have received a copy of the GNU General Public License along |
ansond | 0:137634ff4186 | 21 | * with this program; if not, write to the Free Software Foundation, Inc., |
ansond | 0:137634ff4186 | 22 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
ansond | 0:137634ff4186 | 23 | */ |
ansond | 0:137634ff4186 | 24 | #ifndef POLARSSL_XTEA_H |
ansond | 0:137634ff4186 | 25 | #define POLARSSL_XTEA_H |
ansond | 0:137634ff4186 | 26 | |
ansond | 0:137634ff4186 | 27 | #if !defined(POLARSSL_CONFIG_FILE) |
ansond | 0:137634ff4186 | 28 | #include "config.h" |
ansond | 0:137634ff4186 | 29 | #else |
ansond | 0:137634ff4186 | 30 | #include POLARSSL_CONFIG_FILE |
ansond | 0:137634ff4186 | 31 | #endif |
ansond | 0:137634ff4186 | 32 | |
ansond | 0:137634ff4186 | 33 | #include <stddef.h> |
ansond | 0:137634ff4186 | 34 | |
ansond | 0:137634ff4186 | 35 | #if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32) |
ansond | 0:137634ff4186 | 36 | #include <basetsd.h> |
ansond | 0:137634ff4186 | 37 | typedef UINT32 uint32_t; |
ansond | 0:137634ff4186 | 38 | #else |
ansond | 0:137634ff4186 | 39 | #include <inttypes.h> |
ansond | 0:137634ff4186 | 40 | #endif |
ansond | 0:137634ff4186 | 41 | |
ansond | 0:137634ff4186 | 42 | #define XTEA_ENCRYPT 1 |
ansond | 0:137634ff4186 | 43 | #define XTEA_DECRYPT 0 |
ansond | 0:137634ff4186 | 44 | |
ansond | 0:137634ff4186 | 45 | #define POLARSSL_ERR_XTEA_INVALID_INPUT_LENGTH -0x0028 /**< The data input has an invalid length. */ |
ansond | 0:137634ff4186 | 46 | |
ansond | 0:137634ff4186 | 47 | #if !defined(POLARSSL_XTEA_ALT) |
ansond | 0:137634ff4186 | 48 | // Regular implementation |
ansond | 0:137634ff4186 | 49 | // |
ansond | 0:137634ff4186 | 50 | |
ansond | 0:137634ff4186 | 51 | #ifdef __cplusplus |
ansond | 0:137634ff4186 | 52 | extern "C" { |
ansond | 0:137634ff4186 | 53 | #endif |
ansond | 0:137634ff4186 | 54 | |
ansond | 0:137634ff4186 | 55 | /** |
ansond | 0:137634ff4186 | 56 | * \brief XTEA context structure |
ansond | 0:137634ff4186 | 57 | */ |
ansond | 0:137634ff4186 | 58 | typedef struct |
ansond | 0:137634ff4186 | 59 | { |
ansond | 0:137634ff4186 | 60 | uint32_t k[4]; /*!< key */ |
ansond | 0:137634ff4186 | 61 | } |
ansond | 0:137634ff4186 | 62 | xtea_context; |
ansond | 0:137634ff4186 | 63 | |
ansond | 0:137634ff4186 | 64 | /** |
ansond | 0:137634ff4186 | 65 | * \brief Initialize XTEA context |
ansond | 0:137634ff4186 | 66 | * |
ansond | 0:137634ff4186 | 67 | * \param ctx XTEA context to be initialized |
ansond | 0:137634ff4186 | 68 | */ |
ansond | 0:137634ff4186 | 69 | void xtea_init( xtea_context *ctx ); |
ansond | 0:137634ff4186 | 70 | |
ansond | 0:137634ff4186 | 71 | /** |
ansond | 0:137634ff4186 | 72 | * \brief Clear XTEA context |
ansond | 0:137634ff4186 | 73 | * |
ansond | 0:137634ff4186 | 74 | * \param ctx XTEA context to be cleared |
ansond | 0:137634ff4186 | 75 | */ |
ansond | 0:137634ff4186 | 76 | void xtea_free( xtea_context *ctx ); |
ansond | 0:137634ff4186 | 77 | |
ansond | 0:137634ff4186 | 78 | /** |
ansond | 0:137634ff4186 | 79 | * \brief XTEA key schedule |
ansond | 0:137634ff4186 | 80 | * |
ansond | 0:137634ff4186 | 81 | * \param ctx XTEA context to be initialized |
ansond | 0:137634ff4186 | 82 | * \param key the secret key |
ansond | 0:137634ff4186 | 83 | */ |
ansond | 0:137634ff4186 | 84 | void xtea_setup( xtea_context *ctx, const unsigned char key[16] ); |
ansond | 0:137634ff4186 | 85 | |
ansond | 0:137634ff4186 | 86 | /** |
ansond | 0:137634ff4186 | 87 | * \brief XTEA cipher function |
ansond | 0:137634ff4186 | 88 | * |
ansond | 0:137634ff4186 | 89 | * \param ctx XTEA context |
ansond | 0:137634ff4186 | 90 | * \param mode XTEA_ENCRYPT or XTEA_DECRYPT |
ansond | 0:137634ff4186 | 91 | * \param input 8-byte input block |
ansond | 0:137634ff4186 | 92 | * \param output 8-byte output block |
ansond | 0:137634ff4186 | 93 | * |
ansond | 0:137634ff4186 | 94 | * \return 0 if successful |
ansond | 0:137634ff4186 | 95 | */ |
ansond | 0:137634ff4186 | 96 | int xtea_crypt_ecb( xtea_context *ctx, |
ansond | 0:137634ff4186 | 97 | int mode, |
ansond | 0:137634ff4186 | 98 | const unsigned char input[8], |
ansond | 0:137634ff4186 | 99 | unsigned char output[8] ); |
ansond | 0:137634ff4186 | 100 | |
ansond | 0:137634ff4186 | 101 | #if defined(POLARSSL_CIPHER_MODE_CBC) |
ansond | 0:137634ff4186 | 102 | /** |
ansond | 0:137634ff4186 | 103 | * \brief XTEA CBC cipher function |
ansond | 0:137634ff4186 | 104 | * |
ansond | 0:137634ff4186 | 105 | * \param ctx XTEA context |
ansond | 0:137634ff4186 | 106 | * \param mode XTEA_ENCRYPT or XTEA_DECRYPT |
ansond | 0:137634ff4186 | 107 | * \param length the length of input, multiple of 8 |
ansond | 0:137634ff4186 | 108 | * \param iv initialization vector for CBC mode |
ansond | 0:137634ff4186 | 109 | * \param input input block |
ansond | 0:137634ff4186 | 110 | * \param output output block |
ansond | 0:137634ff4186 | 111 | * |
ansond | 0:137634ff4186 | 112 | * \return 0 if successful, |
ansond | 0:137634ff4186 | 113 | * POLARSSL_ERR_XTEA_INVALID_INPUT_LENGTH if the length % 8 != 0 |
ansond | 0:137634ff4186 | 114 | */ |
ansond | 0:137634ff4186 | 115 | int xtea_crypt_cbc( xtea_context *ctx, |
ansond | 0:137634ff4186 | 116 | int mode, |
ansond | 0:137634ff4186 | 117 | size_t length, |
ansond | 0:137634ff4186 | 118 | unsigned char iv[8], |
ansond | 0:137634ff4186 | 119 | const unsigned char *input, |
ansond | 0:137634ff4186 | 120 | unsigned char *output); |
ansond | 0:137634ff4186 | 121 | #endif /* POLARSSL_CIPHER_MODE_CBC */ |
ansond | 0:137634ff4186 | 122 | |
ansond | 0:137634ff4186 | 123 | #ifdef __cplusplus |
ansond | 0:137634ff4186 | 124 | } |
ansond | 0:137634ff4186 | 125 | #endif |
ansond | 0:137634ff4186 | 126 | |
ansond | 0:137634ff4186 | 127 | #else /* POLARSSL_XTEA_ALT */ |
ansond | 0:137634ff4186 | 128 | #include "xtea_alt.h" |
ansond | 0:137634ff4186 | 129 | #endif /* POLARSSL_XTEA_ALT */ |
ansond | 0:137634ff4186 | 130 | |
ansond | 0:137634ff4186 | 131 | #ifdef __cplusplus |
ansond | 0:137634ff4186 | 132 | extern "C" { |
ansond | 0:137634ff4186 | 133 | #endif |
ansond | 0:137634ff4186 | 134 | |
ansond | 0:137634ff4186 | 135 | /** |
ansond | 0:137634ff4186 | 136 | * \brief Checkup routine |
ansond | 0:137634ff4186 | 137 | * |
ansond | 0:137634ff4186 | 138 | * \return 0 if successful, or 1 if the test failed |
ansond | 0:137634ff4186 | 139 | */ |
ansond | 0:137634ff4186 | 140 | int xtea_self_test( int verbose ); |
ansond | 0:137634ff4186 | 141 | |
ansond | 0:137634ff4186 | 142 | #ifdef __cplusplus |
ansond | 0:137634ff4186 | 143 | } |
ansond | 0:137634ff4186 | 144 | #endif |
ansond | 0:137634ff4186 | 145 | |
ansond | 0:137634ff4186 | 146 | #endif /* xtea.h */ |
ansond | 0:137634ff4186 | 147 |