blob: b7242c74f03d4d9bc76b7f4561d20f640810d994 [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)
Darryl Greena40a1012018-01-05 15:33:17 +00005 */
6/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02007 * Copyright The Mbed TLS Contributors
Dave Rodgman7ff79652023-11-03 12:04:52 +00008 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker7a7c78f2009-01-04 18:15:48 +00009 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010#ifndef MBEDTLS_XTEA_H
11#define MBEDTLS_XTEA_H
Paul Bakker7a7c78f2009-01-04 18:15:48 +000012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020013#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010014#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020015#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020016#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020017#endif
Paul Bakker90995b52013-06-24 19:20:35 +020018
Rich Evans00ab4702015-02-06 13:43:58 +000019#include <stddef.h>
Manuel Pégourié-Gonnardab229102015-04-15 11:53:16 +020020#include <stdint.h>
Paul Bakker0fdf3ca2009-05-03 12:54:07 +000021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#define MBEDTLS_XTEA_ENCRYPT 1
23#define MBEDTLS_XTEA_DECRYPT 0
Paul Bakker7a7c78f2009-01-04 18:15:48 +000024
Gilles Peskinea3974432021-07-26 18:48:10 +020025/** The data input has an invalid length. */
26#define MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH -0x0028
Ron Eldor9924bdc2018-10-04 10:59:13 +030027
28/* MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED is deprecated and should not be used. */
Gilles Peskinea3974432021-07-26 18:48:10 +020029/** XTEA hardware accelerator failed. */
30#define MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED -0x0029
Paul Bakker7a7c78f2009-01-04 18:15:48 +000031
Paul Bakker407a0da2013-06-27 14:29:21 +020032#ifdef __cplusplus
33extern "C" {
34#endif
35
Ron Eldorb2aacec2017-05-18 16:53:08 +030036#if !defined(MBEDTLS_XTEA_ALT)
37// Regular implementation
38//
39
Paul Bakker7a7c78f2009-01-04 18:15:48 +000040/**
41 * \brief XTEA context structure
42 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010043typedef struct mbedtls_xtea_context {
Paul Bakker0fdf3ca2009-05-03 12:54:07 +000044 uint32_t k[4]; /*!< key */
Paul Bakker7a7c78f2009-01-04 18:15:48 +000045}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046mbedtls_xtea_context;
Paul Bakker7a7c78f2009-01-04 18:15:48 +000047
Ron Eldorb2aacec2017-05-18 16:53:08 +030048#else /* MBEDTLS_XTEA_ALT */
49#include "xtea_alt.h"
50#endif /* MBEDTLS_XTEA_ALT */
51
Paul Bakker7a7c78f2009-01-04 18:15:48 +000052/**
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020053 * \brief Initialize XTEA context
54 *
55 * \param ctx XTEA context to be initialized
56 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010057void mbedtls_xtea_init(mbedtls_xtea_context *ctx);
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020058
59/**
60 * \brief Clear XTEA context
61 *
62 * \param ctx XTEA context to be cleared
63 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010064void mbedtls_xtea_free(mbedtls_xtea_context *ctx);
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020065
66/**
Paul Bakker7a7c78f2009-01-04 18:15:48 +000067 * \brief XTEA key schedule
68 *
69 * \param ctx XTEA context to be initialized
70 * \param key the secret key
71 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010072void mbedtls_xtea_setup(mbedtls_xtea_context *ctx, const unsigned char key[16]);
Paul Bakker7a7c78f2009-01-04 18:15:48 +000073
74/**
75 * \brief XTEA cipher function
76 *
77 * \param ctx XTEA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078 * \param mode MBEDTLS_XTEA_ENCRYPT or MBEDTLS_XTEA_DECRYPT
Paul Bakker7a7c78f2009-01-04 18:15:48 +000079 * \param input 8-byte input block
80 * \param output 8-byte output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +000081 *
Paul Bakker27caa8a2010-03-21 15:43:59 +000082 * \return 0 if successful
Paul Bakker7a7c78f2009-01-04 18:15:48 +000083 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010084int mbedtls_xtea_crypt_ecb(mbedtls_xtea_context *ctx,
85 int mode,
86 const unsigned char input[8],
87 unsigned char output[8]);
Paul Bakker7a7c78f2009-01-04 18:15:48 +000088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakkerb6ecaf52011-04-19 14:29:23 +000090/**
91 * \brief XTEA CBC cipher function
92 *
93 * \param ctx XTEA context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094 * \param mode MBEDTLS_XTEA_ENCRYPT or MBEDTLS_XTEA_DECRYPT
Paul Bakkerb6ecaf52011-04-19 14:29:23 +000095 * \param length the length of input, multiple of 8
96 * \param iv initialization vector for CBC mode
97 * \param input input block
98 * \param output output block
99 *
100 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101 * MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH if the length % 8 != 0
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000102 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100103int mbedtls_xtea_crypt_cbc(mbedtls_xtea_context *ctx,
104 int mode,
105 size_t length,
106 unsigned char iv[8],
107 const unsigned char *input,
108 unsigned char *output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000110
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500111#if defined(MBEDTLS_SELF_TEST)
112
Paul Bakker9a736322012-11-14 12:39:52 +0000113/**
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000114 * \brief Checkup routine
115 *
116 * \return 0 if successful, or 1 if the test failed
117 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100118int mbedtls_xtea_self_test(int verbose);
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000119
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500120#endif /* MBEDTLS_SELF_TEST */
121
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000122#ifdef __cplusplus
123}
124#endif
125
126#endif /* xtea.h */