xtea.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. #define MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED -0x0029 /**< XTEA hardware accelerator failed. */
  37. #ifdef __cplusplus
  38. extern "C" {
  39. #endif
  40. #if !defined(MBEDTLS_XTEA_ALT)
  41. // Regular implementation
  42. //
  43. /**
  44. * \brief XTEA context structure
  45. */
  46. typedef struct
  47. {
  48. uint32_t k[4]; /*!< key */
  49. }
  50. mbedtls_xtea_context;
  51. #else /* MBEDTLS_XTEA_ALT */
  52. #include "xtea_alt.h"
  53. #endif /* MBEDTLS_XTEA_ALT */
  54. /**
  55. * \brief Initialize XTEA context
  56. *
  57. * \param ctx XTEA context to be initialized
  58. */
  59. void mbedtls_xtea_init( mbedtls_xtea_context *ctx );
  60. /**
  61. * \brief Clear XTEA context
  62. *
  63. * \param ctx XTEA context to be cleared
  64. */
  65. void mbedtls_xtea_free( mbedtls_xtea_context *ctx );
  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. * \brief XTEA cipher function
  75. *
  76. * \param ctx XTEA context
  77. * \param mode MBEDTLS_XTEA_ENCRYPT or MBEDTLS_XTEA_DECRYPT
  78. * \param input 8-byte input block
  79. * \param output 8-byte output block
  80. *
  81. * \return 0 if successful
  82. */
  83. int mbedtls_xtea_crypt_ecb( mbedtls_xtea_context *ctx,
  84. int mode,
  85. const unsigned char input[8],
  86. unsigned char output[8] );
  87. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  88. /**
  89. * \brief XTEA CBC cipher function
  90. *
  91. * \param ctx XTEA context
  92. * \param mode MBEDTLS_XTEA_ENCRYPT or MBEDTLS_XTEA_DECRYPT
  93. * \param length the length of input, multiple of 8
  94. * \param iv initialization vector for CBC mode
  95. * \param input input block
  96. * \param output output block
  97. *
  98. * \return 0 if successful,
  99. * MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH if the length % 8 != 0
  100. */
  101. int mbedtls_xtea_crypt_cbc( mbedtls_xtea_context *ctx,
  102. int mode,
  103. size_t length,
  104. unsigned char iv[8],
  105. const unsigned char *input,
  106. unsigned char *output);
  107. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  108. /**
  109. * \brief Checkup routine
  110. *
  111. * \return 0 if successful, or 1 if the test failed
  112. */
  113. int mbedtls_xtea_self_test( int verbose );
  114. #ifdef __cplusplus
  115. }
  116. #endif
  117. #endif /* xtea.h */