xtea.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. #if !defined(MBEDTLS_CONFIG_FILE)
  26. #include "config.h"
  27. #else
  28. #include MBEDTLS_CONFIG_FILE
  29. #endif
  30. #include <stddef.h>
  31. #include <stdint.h>
  32. #define MBEDTLS_XTEA_ENCRYPT 1
  33. #define MBEDTLS_XTEA_DECRYPT 0
  34. #define MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH -0x0028 /**< The data input has an invalid length. */
  35. #if !defined(MBEDTLS_XTEA_ALT)
  36. // Regular implementation
  37. //
  38. #ifdef __cplusplus
  39. extern "C" {
  40. #endif
  41. /**
  42. * \brief XTEA context structure
  43. */
  44. typedef struct
  45. {
  46. uint32_t k[4]; /*!< key */
  47. }
  48. mbedtls_xtea_context;
  49. /**
  50. * \brief Initialize XTEA context
  51. *
  52. * \param ctx XTEA context to be initialized
  53. */
  54. void mbedtls_xtea_init( mbedtls_xtea_context *ctx );
  55. /**
  56. * \brief Clear XTEA context
  57. *
  58. * \param ctx XTEA context to be cleared
  59. */
  60. void mbedtls_xtea_free( mbedtls_xtea_context *ctx );
  61. /**
  62. * \brief XTEA key schedule
  63. *
  64. * \param ctx XTEA context to be initialized
  65. * \param key the secret key
  66. */
  67. void mbedtls_xtea_setup( mbedtls_xtea_context *ctx, const unsigned char key[16] );
  68. /**
  69. * \brief XTEA cipher function
  70. *
  71. * \param ctx XTEA context
  72. * \param mode MBEDTLS_XTEA_ENCRYPT or MBEDTLS_XTEA_DECRYPT
  73. * \param input 8-byte input block
  74. * \param output 8-byte output block
  75. *
  76. * \return 0 if successful
  77. */
  78. int mbedtls_xtea_crypt_ecb( mbedtls_xtea_context *ctx,
  79. int mode,
  80. const unsigned char input[8],
  81. unsigned char output[8] );
  82. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  83. /**
  84. * \brief XTEA CBC cipher function
  85. *
  86. * \param ctx XTEA context
  87. * \param mode MBEDTLS_XTEA_ENCRYPT or MBEDTLS_XTEA_DECRYPT
  88. * \param length the length of input, multiple of 8
  89. * \param iv initialization vector for CBC mode
  90. * \param input input block
  91. * \param output output block
  92. *
  93. * \return 0 if successful,
  94. * MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH if the length % 8 != 0
  95. */
  96. int mbedtls_xtea_crypt_cbc( mbedtls_xtea_context *ctx,
  97. int mode,
  98. size_t length,
  99. unsigned char iv[8],
  100. const unsigned char *input,
  101. unsigned char *output);
  102. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  103. #ifdef __cplusplus
  104. }
  105. #endif
  106. #else /* MBEDTLS_XTEA_ALT */
  107. #include "xtea_alt.h"
  108. #endif /* MBEDTLS_XTEA_ALT */
  109. #ifdef __cplusplus
  110. extern "C" {
  111. #endif
  112. /**
  113. * \brief Checkup routine
  114. *
  115. * \return 0 if successful, or 1 if the test failed
  116. */
  117. int mbedtls_xtea_self_test( int verbose );
  118. #ifdef __cplusplus
  119. }
  120. #endif
  121. #endif /* xtea.h */