xtea.h 3.6 KB

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