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