Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

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 "mbedtls/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 
00041 /* MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED is deprecated and should not be used. */
00042 #define MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED                  -0x0029  /**< XTEA hardware accelerator failed. */
00043 
00044 #ifdef __cplusplus
00045 extern "C" {
00046 #endif
00047 
00048 #if !defined(MBEDTLS_XTEA_ALT)
00049 // Regular implementation
00050 //
00051 
00052 /**
00053  * \brief          XTEA context structure
00054  */
00055 typedef struct mbedtls_xtea_context
00056 {
00057     uint32_t k [4];       /*!< key */
00058 }
00059 mbedtls_xtea_context;
00060 
00061 #else  /* MBEDTLS_XTEA_ALT */
00062 #include "xtea_alt.h"
00063 #endif /* MBEDTLS_XTEA_ALT */
00064 
00065 /**
00066  * \brief          Initialize XTEA context
00067  *
00068  * \param ctx      XTEA context to be initialized
00069  */
00070 void mbedtls_xtea_init( mbedtls_xtea_context *ctx );
00071 
00072 /**
00073  * \brief          Clear XTEA context
00074  *
00075  * \param ctx      XTEA context to be cleared
00076  */
00077 void mbedtls_xtea_free( mbedtls_xtea_context *ctx );
00078 
00079 /**
00080  * \brief          XTEA key schedule
00081  *
00082  * \param ctx      XTEA context to be initialized
00083  * \param key      the secret key
00084  */
00085 void mbedtls_xtea_setup( mbedtls_xtea_context *ctx, const unsigned char key[16] );
00086 
00087 /**
00088  * \brief          XTEA cipher function
00089  *
00090  * \param ctx      XTEA context
00091  * \param mode     MBEDTLS_XTEA_ENCRYPT or MBEDTLS_XTEA_DECRYPT
00092  * \param input    8-byte input block
00093  * \param output   8-byte output block
00094  *
00095  * \return         0 if successful
00096  */
00097 int mbedtls_xtea_crypt_ecb( mbedtls_xtea_context *ctx,
00098                     int mode,
00099                     const unsigned char input[8],
00100                     unsigned char output[8] );
00101 
00102 #if defined(MBEDTLS_CIPHER_MODE_CBC)
00103 /**
00104  * \brief          XTEA CBC cipher function
00105  *
00106  * \param ctx      XTEA context
00107  * \param mode     MBEDTLS_XTEA_ENCRYPT or MBEDTLS_XTEA_DECRYPT
00108  * \param length   the length of input, multiple of 8
00109  * \param iv       initialization vector for CBC mode
00110  * \param input    input block
00111  * \param output   output block
00112  *
00113  * \return         0 if successful,
00114  *                 MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH if the length % 8 != 0
00115  */
00116 int mbedtls_xtea_crypt_cbc( mbedtls_xtea_context *ctx,
00117                     int mode,
00118                     size_t length,
00119                     unsigned char iv[8],
00120                     const unsigned char *input,
00121                     unsigned char *output);
00122 #endif /* MBEDTLS_CIPHER_MODE_CBC */
00123 
00124 #if defined(MBEDTLS_SELF_TEST)
00125 
00126 /**
00127  * \brief          Checkup routine
00128  *
00129  * \return         0 if successful, or 1 if the test failed
00130  */
00131 int mbedtls_xtea_self_test( int verbose );
00132 
00133 #endif /* MBEDTLS_SELF_TEST */
00134 
00135 #ifdef __cplusplus
00136 }
00137 #endif
00138 
00139 #endif /* xtea.h */