Knight KE / Mbed OS Game_Master
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers xtea.h Source File

xtea.h

Go to the documentation of this file.
00001 /**
00002  * \file xtea.h
00003  *
00004  * \brief XTEA block cipher (32-bit)
00005  */
00006 /*
00007  *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
00008  *  SPDX-License-Identifier: Apache-2.0
00009  *
00010  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
00011  *  not use this file except in compliance with the License.
00012  *  You may obtain a copy of the License at
00013  *
00014  *  http://www.apache.org/licenses/LICENSE-2.0
00015  *
00016  *  Unless required by applicable law or agreed to in writing, software
00017  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00018  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00019  *  See the License for the specific language governing permissions and
00020  *  limitations under the License.
00021  *
00022  *  This file is part of mbed TLS (https://tls.mbed.org)
00023  */
00024 #ifndef MBEDTLS_XTEA_H
00025 #define MBEDTLS_XTEA_H
00026 
00027 #if !defined(MBEDTLS_CONFIG_FILE)
00028 #include "config.h"
00029 #else
00030 #include MBEDTLS_CONFIG_FILE
00031 #endif
00032 
00033 #include <stddef.h>
00034 #include <stdint.h>
00035 
00036 #define MBEDTLS_XTEA_ENCRYPT     1
00037 #define MBEDTLS_XTEA_DECRYPT     0
00038 
00039 #define MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH             -0x0028  /**< The data input has an invalid length. */
00040 #define MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED                  -0x0029  /**< XTEA hardware accelerator failed. */
00041 
00042 #ifdef __cplusplus
00043 extern "C" {
00044 #endif
00045 
00046 #if !defined(MBEDTLS_XTEA_ALT)
00047 // Regular implementation
00048 //
00049 
00050 /**
00051  * \brief          XTEA context structure
00052  */
00053 typedef struct
00054 {
00055     uint32_t k[4];       /*!< key */
00056 }
00057 mbedtls_xtea_context;
00058 
00059 #else  /* MBEDTLS_XTEA_ALT */
00060 #include "xtea_alt.h"
00061 #endif /* MBEDTLS_XTEA_ALT */
00062 
00063 /**
00064  * \brief          Initialize XTEA context
00065  *
00066  * \param ctx      XTEA context to be initialized
00067  */
00068 void mbedtls_xtea_init( mbedtls_xtea_context *ctx );
00069 
00070 /**
00071  * \brief          Clear XTEA context
00072  *
00073  * \param ctx      XTEA context to be cleared
00074  */
00075 void mbedtls_xtea_free( mbedtls_xtea_context *ctx );
00076 
00077 /**
00078  * \brief          XTEA key schedule
00079  *
00080  * \param ctx      XTEA context to be initialized
00081  * \param key      the secret key
00082  */
00083 void mbedtls_xtea_setup( mbedtls_xtea_context *ctx, const unsigned char key[16] );
00084 
00085 /**
00086  * \brief          XTEA cipher function
00087  *
00088  * \param ctx      XTEA context
00089  * \param mode     MBEDTLS_XTEA_ENCRYPT or MBEDTLS_XTEA_DECRYPT
00090  * \param input    8-byte input block
00091  * \param output   8-byte output block
00092  *
00093  * \return         0 if successful
00094  */
00095 int mbedtls_xtea_crypt_ecb( mbedtls_xtea_context *ctx,
00096                     int mode,
00097                     const unsigned char input[8],
00098                     unsigned char output[8] );
00099 
00100 #if defined(MBEDTLS_CIPHER_MODE_CBC)
00101 /**
00102  * \brief          XTEA CBC cipher function
00103  *
00104  * \param ctx      XTEA context
00105  * \param mode     MBEDTLS_XTEA_ENCRYPT or MBEDTLS_XTEA_DECRYPT
00106  * \param length   the length of input, multiple of 8
00107  * \param iv       initialization vector for CBC mode
00108  * \param input    input block
00109  * \param output   output block
00110  *
00111  * \return         0 if successful,
00112  *                 MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH if the length % 8 != 0
00113  */
00114 int mbedtls_xtea_crypt_cbc( mbedtls_xtea_context *ctx,
00115                     int mode,
00116                     size_t length,
00117                     unsigned char iv[8],
00118                     const unsigned char *input,
00119                     unsigned char *output);
00120 #endif /* MBEDTLS_CIPHER_MODE_CBC */
00121 
00122 /**
00123  * \brief          Checkup routine
00124  *
00125  * \return         0 if successful, or 1 if the test failed
00126  */
00127 int mbedtls_xtea_self_test( int verbose );
00128 
00129 #ifdef __cplusplus
00130 }
00131 #endif
00132 
00133 #endif /* xtea.h */