blob: 95854d12204d17f85a1fff3e1bcb60270a9c0e59 [file] [log] [blame]
Paul Bakker7a7c78f2009-01-04 18:15:48 +00001/**
2 * \file xtea.h
3 *
Paul Bakker37ca75d2011-01-06 12:28:03 +00004 * \brief XTEA block cipher (32-bit)
5 *
Paul Bakker90995b52013-06-24 19:20:35 +02006 * Copyright (C) 2006-2013, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
8 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00009 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +000010 *
Paul Bakker77b385e2009-07-28 17:23:11 +000011 * All rights reserved.
Paul Bakker7a7c78f2009-01-04 18:15:48 +000012 *
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
Paul Bakker90995b52013-06-24 19:20:35 +020030#include "config.h"
31
Paul Bakker23986e52011-04-24 08:57:21 +000032#include <string.h>
33
Paul Bakkerfa6a6202013-10-28 18:48:30 +010034#if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker477fd322009-10-04 13:22:13 +000035#include <basetsd.h>
36typedef UINT32 uint32_t;
37#else
38#include <inttypes.h>
Paul Bakker80ab9f52009-05-24 14:42:46 +000039#endif
Paul Bakker0fdf3ca2009-05-03 12:54:07 +000040
Paul Bakker7a7c78f2009-01-04 18:15:48 +000041#define XTEA_ENCRYPT 1
42#define XTEA_DECRYPT 0
43
Paul Bakker9d781402011-05-09 16:17:09 +000044#define POLARSSL_ERR_XTEA_INVALID_INPUT_LENGTH -0x0028 /**< The data input has an invalid length. */
Paul Bakker7a7c78f2009-01-04 18:15:48 +000045
Paul Bakker90995b52013-06-24 19:20:35 +020046#if !defined(POLARSSL_XTEA_ALT)
47// Regular implementation
48//
49
Paul Bakker407a0da2013-06-27 14:29:21 +020050#ifdef __cplusplus
51extern "C" {
52#endif
53
Paul Bakker7a7c78f2009-01-04 18:15:48 +000054/**
55 * \brief XTEA context structure
56 */
57typedef struct
58{
Paul Bakker0fdf3ca2009-05-03 12:54:07 +000059 uint32_t k[4]; /*!< key */
Paul Bakker7a7c78f2009-01-04 18:15:48 +000060}
61xtea_context;
62
Paul Bakker7a7c78f2009-01-04 18:15:48 +000063/**
64 * \brief XTEA key schedule
65 *
66 * \param ctx XTEA context to be initialized
67 * \param key the secret key
68 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +020069void xtea_setup( xtea_context *ctx, const unsigned char key[16] );
Paul Bakker7a7c78f2009-01-04 18:15:48 +000070
71/**
72 * \brief XTEA cipher function
73 *
74 * \param ctx XTEA context
75 * \param mode XTEA_ENCRYPT or XTEA_DECRYPT
76 * \param input 8-byte input block
77 * \param output 8-byte output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +000078 *
Paul Bakker27caa8a2010-03-21 15:43:59 +000079 * \return 0 if successful
Paul Bakker7a7c78f2009-01-04 18:15:48 +000080 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +000081int xtea_crypt_ecb( xtea_context *ctx,
Paul Bakker23986e52011-04-24 08:57:21 +000082 int mode,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +020083 const unsigned char input[8],
Paul Bakker23986e52011-04-24 08:57:21 +000084 unsigned char output[8] );
Paul Bakker7a7c78f2009-01-04 18:15:48 +000085
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +020086#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakkerb6ecaf52011-04-19 14:29:23 +000087/**
88 * \brief XTEA CBC cipher function
89 *
90 * \param ctx XTEA context
91 * \param mode XTEA_ENCRYPT or XTEA_DECRYPT
92 * \param length the length of input, multiple of 8
93 * \param iv initialization vector for CBC mode
94 * \param input input block
95 * \param output output block
96 *
97 * \return 0 if successful,
98 * POLARSSL_ERR_XTEA_INVALID_INPUT_LENGTH if the length % 8 != 0
99 */
100int xtea_crypt_cbc( xtea_context *ctx,
101 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000102 size_t length,
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000103 unsigned char iv[8],
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200104 const unsigned char *input,
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000105 unsigned char *output);
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200106#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000107
Paul Bakker90995b52013-06-24 19:20:35 +0200108#ifdef __cplusplus
109}
110#endif
111
112#else /* POLARSSL_XTEA_ALT */
113#include "xtea_alt.h"
114#endif /* POLARSSL_XTEA_ALT */
115
116#ifdef __cplusplus
117extern "C" {
118#endif
119
Paul Bakker9a736322012-11-14 12:39:52 +0000120/**
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000121 * \brief Checkup routine
122 *
123 * \return 0 if successful, or 1 if the test failed
124 */
125int xtea_self_test( int verbose );
126
127#ifdef __cplusplus
128}
129#endif
130
131#endif /* xtea.h */