xtea.h 3.5 KB

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