1 /** 2 * \file xtea.h 3 * 4 * \brief XTEA block cipher (32-bit) 5 */ 6 /* 7 * Copyright The Mbed TLS Contributors 8 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later 9 */ 10 #ifndef MBEDTLS_XTEA_H 11 #define MBEDTLS_XTEA_H 12 13 #if !defined(MBEDTLS_CONFIG_FILE) 14 #include "mbedtls/config.h" 15 #else 16 #include MBEDTLS_CONFIG_FILE 17 #endif 18 19 #include <stddef.h> 20 #include <stdint.h> 21 22 #define MBEDTLS_XTEA_ENCRYPT 1 23 #define MBEDTLS_XTEA_DECRYPT 0 24 25 /** The data input has an invalid length. */ 26 #define MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH -0x0028 27 28 /* MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED is deprecated and should not be used. */ 29 /** XTEA hardware accelerator failed. */ 30 #define MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED -0x0029 31 32 #ifdef __cplusplus 33 extern "C" { 34 #endif 35 36 #if !defined(MBEDTLS_XTEA_ALT) 37 // Regular implementation 38 // 39 40 /** 41 * \brief XTEA context structure 42 */ 43 typedef struct mbedtls_xtea_context { 44 uint32_t k[4]; /*!< key */ 45 } 46 mbedtls_xtea_context; 47 48 #else /* MBEDTLS_XTEA_ALT */ 49 #include "xtea_alt.h" 50 #endif /* MBEDTLS_XTEA_ALT */ 51 52 /** 53 * \brief Initialize XTEA context 54 * 55 * \param ctx XTEA context to be initialized 56 */ 57 void mbedtls_xtea_init(mbedtls_xtea_context *ctx); 58 59 /** 60 * \brief Clear XTEA context 61 * 62 * \param ctx XTEA context to be cleared 63 */ 64 void mbedtls_xtea_free(mbedtls_xtea_context *ctx); 65 66 /** 67 * \brief XTEA key schedule 68 * 69 * \param ctx XTEA context to be initialized 70 * \param key the secret key 71 */ 72 void mbedtls_xtea_setup(mbedtls_xtea_context *ctx, const unsigned char key[16]); 73 74 /** 75 * \brief XTEA cipher function 76 * 77 * \param ctx XTEA context 78 * \param mode MBEDTLS_XTEA_ENCRYPT or MBEDTLS_XTEA_DECRYPT 79 * \param input 8-byte input block 80 * \param output 8-byte output block 81 * 82 * \return 0 if successful 83 */ 84 int mbedtls_xtea_crypt_ecb(mbedtls_xtea_context *ctx, 85 int mode, 86 const unsigned char input[8], 87 unsigned char output[8]); 88 89 #if defined(MBEDTLS_CIPHER_MODE_CBC) 90 /** 91 * \brief XTEA CBC cipher function 92 * 93 * \param ctx XTEA context 94 * \param mode MBEDTLS_XTEA_ENCRYPT or MBEDTLS_XTEA_DECRYPT 95 * \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, 101 * MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH if the length % 8 != 0 102 */ 103 int 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); 109 #endif /* MBEDTLS_CIPHER_MODE_CBC */ 110 111 #if defined(MBEDTLS_SELF_TEST) 112 113 /** 114 * \brief Checkup routine 115 * 116 * \return 0 if successful, or 1 if the test failed 117 */ 118 int mbedtls_xtea_self_test(int verbose); 119 120 #endif /* MBEDTLS_SELF_TEST */ 121 122 #ifdef __cplusplus 123 } 124 #endif 125 126 #endif /* xtea.h */ 127