blob: 5db947de8f7d4b7df780d89e3f64b4abc7efeb5a [file] [log] [blame]
Jaeden Ameroe54e6932018-08-06 16:19:58 +01001/**
2 * \file xtea.h
3 *
4 * \brief XTEA block cipher (32-bit)
5 */
6/*
7 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
8 * SPDX-License-Identifier: Apache-2.0
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License"); you may
11 * not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 *
22 * This file is part of Mbed Crypto (https://tls.mbed.org)
23 */
24#ifndef MBEDCRYPTO_XTEA_H
25#define MBEDCRYPTO_XTEA_H
26
27#if !defined(MBEDCRYPTO_CONFIG_FILE)
28#include "config.h"
29#else
30#include MBEDCRYPTO_CONFIG_FILE
31#endif
32
33#include <stddef.h>
34#include <stdint.h>
35
36#define MBEDCRYPTO_XTEA_ENCRYPT 1
37#define MBEDCRYPTO_XTEA_DECRYPT 0
38
39#define MBEDCRYPTO_ERR_XTEA_INVALID_INPUT_LENGTH -0x0028 /**< The data input has an invalid length. */
40#define MBEDCRYPTO_ERR_XTEA_HW_ACCEL_FAILED -0x0029 /**< XTEA hardware accelerator failed. */
41
42#ifdef __cplusplus
43extern "C" {
44#endif
45
46#if !defined(MBEDCRYPTO_XTEA_ALT)
47// Regular implementation
48//
49
50/**
51 * \brief XTEA context structure
52 */
53typedef struct
54{
55 uint32_t k[4]; /*!< key */
56}
57mbedcrypto_xtea_context;
58
59#else /* MBEDCRYPTO_XTEA_ALT */
60#include "xtea_alt.h"
61#endif /* MBEDCRYPTO_XTEA_ALT */
62
63/**
64 * \brief Initialize XTEA context
65 *
66 * \param ctx XTEA context to be initialized
67 */
68void mbedcrypto_xtea_init( mbedcrypto_xtea_context *ctx );
69
70/**
71 * \brief Clear XTEA context
72 *
73 * \param ctx XTEA context to be cleared
74 */
75void mbedcrypto_xtea_free( mbedcrypto_xtea_context *ctx );
76
77/**
78 * \brief XTEA key schedule
79 *
80 * \param ctx XTEA context to be initialized
81 * \param key the secret key
82 */
83void mbedcrypto_xtea_setup( mbedcrypto_xtea_context *ctx, const unsigned char key[16] );
84
85/**
86 * \brief XTEA cipher function
87 *
88 * \param ctx XTEA context
89 * \param mode MBEDCRYPTO_XTEA_ENCRYPT or MBEDCRYPTO_XTEA_DECRYPT
90 * \param input 8-byte input block
91 * \param output 8-byte output block
92 *
93 * \return 0 if successful
94 */
95int mbedcrypto_xtea_crypt_ecb( mbedcrypto_xtea_context *ctx,
96 int mode,
97 const unsigned char input[8],
98 unsigned char output[8] );
99
100#if defined(MBEDCRYPTO_CIPHER_MODE_CBC)
101/**
102 * \brief XTEA CBC cipher function
103 *
104 * \param ctx XTEA context
105 * \param mode MBEDCRYPTO_XTEA_ENCRYPT or MBEDCRYPTO_XTEA_DECRYPT
106 * \param length the length of input, multiple of 8
107 * \param iv initialization vector for CBC mode
108 * \param input input block
109 * \param output output block
110 *
111 * \return 0 if successful,
112 * MBEDCRYPTO_ERR_XTEA_INVALID_INPUT_LENGTH if the length % 8 != 0
113 */
114int mbedcrypto_xtea_crypt_cbc( mbedcrypto_xtea_context *ctx,
115 int mode,
116 size_t length,
117 unsigned char iv[8],
118 const unsigned char *input,
119 unsigned char *output);
120#endif /* MBEDCRYPTO_CIPHER_MODE_CBC */
121
122/**
123 * \brief Checkup routine
124 *
125 * \return 0 if successful, or 1 if the test failed
126 */
127int mbedcrypto_xtea_self_test( int verbose );
128
129#ifdef __cplusplus
130}
131#endif
132
133#endif /* xtea.h */