| 1 | /** |
|---|
| 2 | * \file xtea.h |
|---|
| 3 | * |
|---|
| 4 | * \brief XTEA block cipher (32-bit) |
|---|
| 5 | * |
|---|
| 6 | * Copyright (C) 2006-2010, Brainspark B.V. |
|---|
| 7 | * |
|---|
| 8 | * This file is part of PolarSSL (http://www.polarssl.org) |
|---|
| 9 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> |
|---|
| 10 | * |
|---|
| 11 | * All rights reserved. |
|---|
| 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 | |
|---|
| 30 | #include <string.h> |
|---|
| 31 | |
|---|
| 32 | #ifdef _MSC_VER |
|---|
| 33 | #include <basetsd.h> |
|---|
| 34 | typedef UINT32 uint32_t; |
|---|
| 35 | #else |
|---|
| 36 | #include <inttypes.h> |
|---|
| 37 | #endif |
|---|
| 38 | |
|---|
| 39 | #define XTEA_ENCRYPT 1 |
|---|
| 40 | #define XTEA_DECRYPT 0 |
|---|
| 41 | |
|---|
| 42 | #define POLARSSL_ERR_XTEA_INVALID_INPUT_LENGTH -0x0028 /**< The data input has an invalid length. */ |
|---|
| 43 | |
|---|
| 44 | /** |
|---|
| 45 | * \brief XTEA context structure |
|---|
| 46 | */ |
|---|
| 47 | typedef struct |
|---|
| 48 | { |
|---|
| 49 | uint32_t k[4]; /*!< key */ |
|---|
| 50 | } |
|---|
| 51 | xtea_context; |
|---|
| 52 | |
|---|
| 53 | #ifdef __cplusplus |
|---|
| 54 | extern "C" { |
|---|
| 55 | #endif |
|---|
| 56 | |
|---|
| 57 | /** |
|---|
| 58 | * \brief XTEA key schedule |
|---|
| 59 | * |
|---|
| 60 | * \param ctx XTEA context to be initialized |
|---|
| 61 | * \param key the secret key |
|---|
| 62 | */ |
|---|
| 63 | void xtea_setup( xtea_context *ctx, unsigned char key[16] ); |
|---|
| 64 | |
|---|
| 65 | /** |
|---|
| 66 | * \brief XTEA cipher function |
|---|
| 67 | * |
|---|
| 68 | * \param ctx XTEA context |
|---|
| 69 | * \param mode XTEA_ENCRYPT or XTEA_DECRYPT |
|---|
| 70 | * \param input 8-byte input block |
|---|
| 71 | * \param output 8-byte output block |
|---|
| 72 | * |
|---|
| 73 | * \return 0 if successful |
|---|
| 74 | */ |
|---|
| 75 | int xtea_crypt_ecb( xtea_context *ctx, |
|---|
| 76 | int mode, |
|---|
| 77 | unsigned char input[8], |
|---|
| 78 | unsigned char output[8] ); |
|---|
| 79 | |
|---|
| 80 | /** |
|---|
| 81 | * \brief XTEA CBC cipher function |
|---|
| 82 | * |
|---|
| 83 | * \param ctx XTEA context |
|---|
| 84 | * \param mode XTEA_ENCRYPT or XTEA_DECRYPT |
|---|
| 85 | * \param length the length of input, multiple of 8 |
|---|
| 86 | * \param iv initialization vector for CBC mode |
|---|
| 87 | * \param input input block |
|---|
| 88 | * \param output output block |
|---|
| 89 | * |
|---|
| 90 | * \return 0 if successful, |
|---|
| 91 | * POLARSSL_ERR_XTEA_INVALID_INPUT_LENGTH if the length % 8 != 0 |
|---|
| 92 | */ |
|---|
| 93 | int xtea_crypt_cbc( xtea_context *ctx, |
|---|
| 94 | int mode, |
|---|
| 95 | size_t length, |
|---|
| 96 | unsigned char iv[8], |
|---|
| 97 | unsigned char *input, |
|---|
| 98 | unsigned char *output); |
|---|
| 99 | |
|---|
| 100 | /* |
|---|
| 101 | * \brief Checkup routine |
|---|
| 102 | * |
|---|
| 103 | * \return 0 if successful, or 1 if the test failed |
|---|
| 104 | */ |
|---|
| 105 | int xtea_self_test( int verbose ); |
|---|
| 106 | |
|---|
| 107 | #ifdef __cplusplus |
|---|
| 108 | } |
|---|
| 109 | #endif |
|---|
| 110 | |
|---|
| 111 | #endif /* xtea.h */ |
|---|