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