xtea.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * A 32-bit implementation of the XTEA algorithm
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  8. * not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. #include "common.h"
  20. #if defined(MBEDTLS_XTEA_C)
  21. #include "mbedtls/xtea.h"
  22. #include "mbedtls/platform_util.h"
  23. #include <string.h>
  24. #include "mbedtls/platform.h"
  25. #if !defined(MBEDTLS_XTEA_ALT)
  26. void mbedtls_xtea_init(mbedtls_xtea_context *ctx)
  27. {
  28. memset(ctx, 0, sizeof(mbedtls_xtea_context));
  29. }
  30. void mbedtls_xtea_free(mbedtls_xtea_context *ctx)
  31. {
  32. if (ctx == NULL) {
  33. return;
  34. }
  35. mbedtls_platform_zeroize(ctx, sizeof(mbedtls_xtea_context));
  36. }
  37. /*
  38. * XTEA key schedule
  39. */
  40. void mbedtls_xtea_setup(mbedtls_xtea_context *ctx, const unsigned char key[16])
  41. {
  42. int i;
  43. memset(ctx, 0, sizeof(mbedtls_xtea_context));
  44. for (i = 0; i < 4; i++) {
  45. ctx->k[i] = MBEDTLS_GET_UINT32_BE(key, i << 2);
  46. }
  47. }
  48. /*
  49. * XTEA encrypt function
  50. */
  51. int mbedtls_xtea_crypt_ecb(mbedtls_xtea_context *ctx, int mode,
  52. const unsigned char input[8], unsigned char output[8])
  53. {
  54. uint32_t *k, v0, v1, i;
  55. k = ctx->k;
  56. v0 = MBEDTLS_GET_UINT32_BE(input, 0);
  57. v1 = MBEDTLS_GET_UINT32_BE(input, 4);
  58. if (mode == MBEDTLS_XTEA_ENCRYPT) {
  59. uint32_t sum = 0, delta = 0x9E3779B9;
  60. for (i = 0; i < 32; i++) {
  61. v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
  62. sum += delta;
  63. v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]);
  64. }
  65. } else { /* MBEDTLS_XTEA_DECRYPT */
  66. uint32_t delta = 0x9E3779B9, sum = delta * 32;
  67. for (i = 0; i < 32; i++) {
  68. v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]);
  69. sum -= delta;
  70. v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
  71. }
  72. }
  73. MBEDTLS_PUT_UINT32_BE(v0, output, 0);
  74. MBEDTLS_PUT_UINT32_BE(v1, output, 4);
  75. return 0;
  76. }
  77. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  78. /*
  79. * XTEA-CBC buffer encryption/decryption
  80. */
  81. int mbedtls_xtea_crypt_cbc(mbedtls_xtea_context *ctx, int mode, size_t length,
  82. unsigned char iv[8], const unsigned char *input,
  83. unsigned char *output)
  84. {
  85. int i;
  86. unsigned char temp[8];
  87. if (length % 8) {
  88. return MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH;
  89. }
  90. if (mode == MBEDTLS_XTEA_DECRYPT) {
  91. while (length > 0) {
  92. memcpy(temp, input, 8);
  93. mbedtls_xtea_crypt_ecb(ctx, mode, input, output);
  94. for (i = 0; i < 8; i++) {
  95. output[i] = (unsigned char) (output[i] ^ iv[i]);
  96. }
  97. memcpy(iv, temp, 8);
  98. input += 8;
  99. output += 8;
  100. length -= 8;
  101. }
  102. } else {
  103. while (length > 0) {
  104. for (i = 0; i < 8; i++) {
  105. output[i] = (unsigned char) (input[i] ^ iv[i]);
  106. }
  107. mbedtls_xtea_crypt_ecb(ctx, mode, output, output);
  108. memcpy(iv, output, 8);
  109. input += 8;
  110. output += 8;
  111. length -= 8;
  112. }
  113. }
  114. return 0;
  115. }
  116. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  117. #endif /* !MBEDTLS_XTEA_ALT */
  118. #if defined(MBEDTLS_SELF_TEST)
  119. /*
  120. * XTEA tests vectors (non-official)
  121. */
  122. static const unsigned char xtea_test_key[6][16] =
  123. {
  124. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  125. 0x0c, 0x0d, 0x0e, 0x0f },
  126. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  127. 0x0c, 0x0d, 0x0e, 0x0f },
  128. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  129. 0x0c, 0x0d, 0x0e, 0x0f },
  130. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  131. 0x00, 0x00, 0x00, 0x00 },
  132. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  133. 0x00, 0x00, 0x00, 0x00 },
  134. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  135. 0x00, 0x00, 0x00, 0x00 }
  136. };
  137. static const unsigned char xtea_test_pt[6][8] =
  138. {
  139. { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 },
  140. { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
  141. { 0x5a, 0x5b, 0x6e, 0x27, 0x89, 0x48, 0xd7, 0x7f },
  142. { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 },
  143. { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
  144. { 0x70, 0xe1, 0x22, 0x5d, 0x6e, 0x4e, 0x76, 0x55 }
  145. };
  146. static const unsigned char xtea_test_ct[6][8] =
  147. {
  148. { 0x49, 0x7d, 0xf3, 0xd0, 0x72, 0x61, 0x2c, 0xb5 },
  149. { 0xe7, 0x8f, 0x2d, 0x13, 0x74, 0x43, 0x41, 0xd8 },
  150. { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
  151. { 0xa0, 0x39, 0x05, 0x89, 0xf8, 0xb8, 0xef, 0xa5 },
  152. { 0xed, 0x23, 0x37, 0x5a, 0x82, 0x1a, 0x8c, 0x2d },
  153. { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 }
  154. };
  155. /*
  156. * Checkup routine
  157. */
  158. int mbedtls_xtea_self_test(int verbose)
  159. {
  160. int i, ret = 0;
  161. unsigned char buf[8];
  162. mbedtls_xtea_context ctx;
  163. mbedtls_xtea_init(&ctx);
  164. for (i = 0; i < 6; i++) {
  165. if (verbose != 0) {
  166. mbedtls_printf(" XTEA test #%d: ", i + 1);
  167. }
  168. memcpy(buf, xtea_test_pt[i], 8);
  169. mbedtls_xtea_setup(&ctx, xtea_test_key[i]);
  170. mbedtls_xtea_crypt_ecb(&ctx, MBEDTLS_XTEA_ENCRYPT, buf, buf);
  171. if (memcmp(buf, xtea_test_ct[i], 8) != 0) {
  172. if (verbose != 0) {
  173. mbedtls_printf("failed\n");
  174. }
  175. ret = 1;
  176. goto exit;
  177. }
  178. if (verbose != 0) {
  179. mbedtls_printf("passed\n");
  180. }
  181. }
  182. if (verbose != 0) {
  183. mbedtls_printf("\n");
  184. }
  185. exit:
  186. mbedtls_xtea_free(&ctx);
  187. return ret;
  188. }
  189. #endif /* MBEDTLS_SELF_TEST */
  190. #endif /* MBEDTLS_XTEA_C */