blob: 07118d977f00544526e17c27c7f7b083e13d77ed [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
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker90995b52013-06-24 19:20:35 +020031#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#else
33#include POLARSSL_CONFIG_FILE
34#endif
Paul Bakker90995b52013-06-24 19:20:35 +020035
Paul Bakker23986e52011-04-24 08:57:21 +000036#include <string.h>
37
Paul Bakkerfa6a6202013-10-28 18:48:30 +010038#if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker477fd322009-10-04 13:22:13 +000039#include <basetsd.h>
40typedef UINT32 uint32_t;
41#else
42#include <inttypes.h>
Paul Bakker80ab9f52009-05-24 14:42:46 +000043#endif
Paul Bakker0fdf3ca2009-05-03 12:54:07 +000044
Paul Bakker7a7c78f2009-01-04 18:15:48 +000045#define XTEA_ENCRYPT 1
46#define XTEA_DECRYPT 0
47
Paul Bakker9d781402011-05-09 16:17:09 +000048#define POLARSSL_ERR_XTEA_INVALID_INPUT_LENGTH -0x0028 /**< The data input has an invalid length. */
Paul Bakker7a7c78f2009-01-04 18:15:48 +000049
Paul Bakker90995b52013-06-24 19:20:35 +020050#if !defined(POLARSSL_XTEA_ALT)
51// Regular implementation
52//
53
Paul Bakker407a0da2013-06-27 14:29:21 +020054#ifdef __cplusplus
55extern "C" {
56#endif
57
Paul Bakker7a7c78f2009-01-04 18:15:48 +000058/**
59 * \brief XTEA context structure
60 */
61typedef struct
62{
Paul Bakker0fdf3ca2009-05-03 12:54:07 +000063 uint32_t k[4]; /*!< key */
Paul Bakker7a7c78f2009-01-04 18:15:48 +000064}
65xtea_context;
66
Paul Bakker7a7c78f2009-01-04 18:15:48 +000067/**
68 * \brief XTEA key schedule
69 *
70 * \param ctx XTEA context to be initialized
71 * \param key the secret key
72 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +020073void xtea_setup( xtea_context *ctx, const unsigned char key[16] );
Paul Bakker7a7c78f2009-01-04 18:15:48 +000074
75/**
76 * \brief XTEA cipher function
77 *
78 * \param ctx XTEA context
79 * \param mode XTEA_ENCRYPT or XTEA_DECRYPT
80 * \param input 8-byte input block
81 * \param output 8-byte output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +000082 *
Paul Bakker27caa8a2010-03-21 15:43:59 +000083 * \return 0 if successful
Paul Bakker7a7c78f2009-01-04 18:15:48 +000084 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +000085int xtea_crypt_ecb( xtea_context *ctx,
Paul Bakker23986e52011-04-24 08:57:21 +000086 int mode,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +020087 const unsigned char input[8],
Paul Bakker23986e52011-04-24 08:57:21 +000088 unsigned char output[8] );
Paul Bakker7a7c78f2009-01-04 18:15:48 +000089
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +020090#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakkerb6ecaf52011-04-19 14:29:23 +000091/**
92 * \brief XTEA CBC cipher function
93 *
94 * \param ctx XTEA context
95 * \param mode XTEA_ENCRYPT or XTEA_DECRYPT
96 * \param length the length of input, multiple of 8
97 * \param iv initialization vector for CBC mode
98 * \param input input block
99 * \param output output block
100 *
101 * \return 0 if successful,
102 * POLARSSL_ERR_XTEA_INVALID_INPUT_LENGTH if the length % 8 != 0
103 */
104int xtea_crypt_cbc( xtea_context *ctx,
105 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000106 size_t length,
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000107 unsigned char iv[8],
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200108 const unsigned char *input,
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000109 unsigned char *output);
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200110#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000111
Paul Bakker90995b52013-06-24 19:20:35 +0200112#ifdef __cplusplus
113}
114#endif
115
116#else /* POLARSSL_XTEA_ALT */
117#include "xtea_alt.h"
118#endif /* POLARSSL_XTEA_ALT */
119
120#ifdef __cplusplus
121extern "C" {
122#endif
123
Paul Bakker9a736322012-11-14 12:39:52 +0000124/**
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000125 * \brief Checkup routine
126 *
127 * \return 0 if successful, or 1 if the test failed
128 */
129int xtea_self_test( int verbose );
130
131#ifdef __cplusplus
132}
133#endif
134
135#endif /* xtea.h */