xtea.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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: GPL-2.0
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  22. *
  23. * This file is part of mbed TLS (https://tls.mbed.org)
  24. */
  25. #ifndef MBEDTLS_XTEA_H
  26. #define MBEDTLS_XTEA_H
  27. #if !defined(MBEDTLS_CONFIG_FILE)
  28. #include "config.h"
  29. #else
  30. #include MBEDTLS_CONFIG_FILE
  31. #endif
  32. #include <stddef.h>
  33. #include <stdint.h>
  34. #define MBEDTLS_XTEA_ENCRYPT 1
  35. #define MBEDTLS_XTEA_DECRYPT 0
  36. #define MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH -0x0028 /**< The data input has an invalid length. */
  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 */