Paul Bakker | 7a7c78f | 2009-01-04 18:15:48 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file xtea.h |
| 3 | * |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 4 | * \brief XTEA block cipher (32-bit) |
| 5 | * |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 6 | * Copyright (C) 2006-2013, Brainspark B.V. |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 7 | * |
| 8 | * This file is part of PolarSSL (http://www.polarssl.org) |
Paul Bakker | 84f12b7 | 2010-07-18 10:13:04 +0000 | [diff] [blame] | 9 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 10 | * |
Paul Bakker | 77b385e | 2009-07-28 17:23:11 +0000 | [diff] [blame] | 11 | * All rights reserved. |
Paul Bakker | 7a7c78f | 2009-01-04 18:15:48 +0000 | [diff] [blame] | 12 | * |
| 13 | * This program is free software; you can redistribute it and/or modify |
| 14 | * it under the terms of the GNU General Public License as published by |
| 15 | * the Free Software Foundation; either version 2 of the License, or |
| 16 | * (at your option) any later version. |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | * GNU General Public License for more details. |
| 22 | * |
| 23 | * You should have received a copy of the GNU General Public License along |
| 24 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 25 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 26 | */ |
| 27 | #ifndef POLARSSL_XTEA_H |
| 28 | #define POLARSSL_XTEA_H |
| 29 | |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 30 | #if !defined(POLARSSL_CONFIG_FILE) |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 31 | #include "config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 32 | #else |
| 33 | #include POLARSSL_CONFIG_FILE |
| 34 | #endif |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 35 | |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 36 | #include <string.h> |
| 37 | |
Paul Bakker | fa6a620 | 2013-10-28 18:48:30 +0100 | [diff] [blame] | 38 | #if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32) |
Paul Bakker | 477fd32 | 2009-10-04 13:22:13 +0000 | [diff] [blame] | 39 | #include <basetsd.h> |
| 40 | typedef UINT32 uint32_t; |
| 41 | #else |
| 42 | #include <inttypes.h> |
Paul Bakker | 80ab9f5 | 2009-05-24 14:42:46 +0000 | [diff] [blame] | 43 | #endif |
Paul Bakker | 0fdf3ca | 2009-05-03 12:54:07 +0000 | [diff] [blame] | 44 | |
Paul Bakker | 7a7c78f | 2009-01-04 18:15:48 +0000 | [diff] [blame] | 45 | #define XTEA_ENCRYPT 1 |
| 46 | #define XTEA_DECRYPT 0 |
| 47 | |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 48 | #define POLARSSL_ERR_XTEA_INVALID_INPUT_LENGTH -0x0028 /**< The data input has an invalid length. */ |
Paul Bakker | 7a7c78f | 2009-01-04 18:15:48 +0000 | [diff] [blame] | 49 | |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 50 | #if !defined(POLARSSL_XTEA_ALT) |
| 51 | // Regular implementation |
| 52 | // |
| 53 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 54 | #ifdef __cplusplus |
| 55 | extern "C" { |
| 56 | #endif |
| 57 | |
Paul Bakker | 7a7c78f | 2009-01-04 18:15:48 +0000 | [diff] [blame] | 58 | /** |
| 59 | * \brief XTEA context structure |
| 60 | */ |
| 61 | typedef struct |
| 62 | { |
Paul Bakker | 0fdf3ca | 2009-05-03 12:54:07 +0000 | [diff] [blame] | 63 | uint32_t k[4]; /*!< key */ |
Paul Bakker | 7a7c78f | 2009-01-04 18:15:48 +0000 | [diff] [blame] | 64 | } |
| 65 | xtea_context; |
| 66 | |
Paul Bakker | 7a7c78f | 2009-01-04 18:15:48 +0000 | [diff] [blame] | 67 | /** |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame^] | 68 | * \brief Initialize XTEA context |
| 69 | * |
| 70 | * \param ctx XTEA context to be initialized |
| 71 | */ |
| 72 | void xtea_init( xtea_context *ctx ); |
| 73 | |
| 74 | /** |
| 75 | * \brief Clear XTEA context |
| 76 | * |
| 77 | * \param ctx XTEA context to be cleared |
| 78 | */ |
| 79 | void xtea_free( xtea_context *ctx ); |
| 80 | |
| 81 | /** |
Paul Bakker | 7a7c78f | 2009-01-04 18:15:48 +0000 | [diff] [blame] | 82 | * \brief XTEA key schedule |
| 83 | * |
| 84 | * \param ctx XTEA context to be initialized |
| 85 | * \param key the secret key |
| 86 | */ |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 87 | void xtea_setup( xtea_context *ctx, const unsigned char key[16] ); |
Paul Bakker | 7a7c78f | 2009-01-04 18:15:48 +0000 | [diff] [blame] | 88 | |
| 89 | /** |
| 90 | * \brief XTEA cipher function |
| 91 | * |
| 92 | * \param ctx XTEA context |
| 93 | * \param mode XTEA_ENCRYPT or XTEA_DECRYPT |
| 94 | * \param input 8-byte input block |
| 95 | * \param output 8-byte output block |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 96 | * |
Paul Bakker | 27caa8a | 2010-03-21 15:43:59 +0000 | [diff] [blame] | 97 | * \return 0 if successful |
Paul Bakker | 7a7c78f | 2009-01-04 18:15:48 +0000 | [diff] [blame] | 98 | */ |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 99 | int xtea_crypt_ecb( xtea_context *ctx, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 100 | int mode, |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 101 | const unsigned char input[8], |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 102 | unsigned char output[8] ); |
Paul Bakker | 7a7c78f | 2009-01-04 18:15:48 +0000 | [diff] [blame] | 103 | |
Manuel Pégourié-Gonnard | 92cb1d3 | 2013-09-13 16:24:20 +0200 | [diff] [blame] | 104 | #if defined(POLARSSL_CIPHER_MODE_CBC) |
Paul Bakker | b6ecaf5 | 2011-04-19 14:29:23 +0000 | [diff] [blame] | 105 | /** |
| 106 | * \brief XTEA CBC cipher function |
| 107 | * |
| 108 | * \param ctx XTEA context |
| 109 | * \param mode XTEA_ENCRYPT or XTEA_DECRYPT |
| 110 | * \param length the length of input, multiple of 8 |
| 111 | * \param iv initialization vector for CBC mode |
| 112 | * \param input input block |
| 113 | * \param output output block |
| 114 | * |
| 115 | * \return 0 if successful, |
| 116 | * POLARSSL_ERR_XTEA_INVALID_INPUT_LENGTH if the length % 8 != 0 |
| 117 | */ |
| 118 | int xtea_crypt_cbc( xtea_context *ctx, |
| 119 | int mode, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 120 | size_t length, |
Paul Bakker | b6ecaf5 | 2011-04-19 14:29:23 +0000 | [diff] [blame] | 121 | unsigned char iv[8], |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 122 | const unsigned char *input, |
Paul Bakker | b6ecaf5 | 2011-04-19 14:29:23 +0000 | [diff] [blame] | 123 | unsigned char *output); |
Manuel Pégourié-Gonnard | 92cb1d3 | 2013-09-13 16:24:20 +0200 | [diff] [blame] | 124 | #endif /* POLARSSL_CIPHER_MODE_CBC */ |
Paul Bakker | b6ecaf5 | 2011-04-19 14:29:23 +0000 | [diff] [blame] | 125 | |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 126 | #ifdef __cplusplus |
| 127 | } |
| 128 | #endif |
| 129 | |
| 130 | #else /* POLARSSL_XTEA_ALT */ |
| 131 | #include "xtea_alt.h" |
| 132 | #endif /* POLARSSL_XTEA_ALT */ |
| 133 | |
| 134 | #ifdef __cplusplus |
| 135 | extern "C" { |
| 136 | #endif |
| 137 | |
Paul Bakker | 9a73632 | 2012-11-14 12:39:52 +0000 | [diff] [blame] | 138 | /** |
Paul Bakker | 7a7c78f | 2009-01-04 18:15:48 +0000 | [diff] [blame] | 139 | * \brief Checkup routine |
| 140 | * |
| 141 | * \return 0 if successful, or 1 if the test failed |
| 142 | */ |
| 143 | int xtea_self_test( int verbose ); |
| 144 | |
| 145 | #ifdef __cplusplus |
| 146 | } |
| 147 | #endif |
| 148 | |
| 149 | #endif /* xtea.h */ |