test_suite_xtea.function 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /* BEGIN_HEADER */
  2. #include "mbedtls/xtea.h"
  3. /* END_HEADER */
  4. /* BEGIN_DEPENDENCIES
  5. * depends_on:MBEDTLS_XTEA_C
  6. * END_DEPENDENCIES
  7. */
  8. /* BEGIN_CASE */
  9. void xtea_encrypt_ecb( char *hex_key_string, char *hex_src_string,
  10. char *hex_dst_string )
  11. {
  12. unsigned char key_str[100];
  13. unsigned char src_str[100];
  14. unsigned char dst_str[100];
  15. unsigned char output[100];
  16. mbedtls_xtea_context ctx;
  17. memset(key_str, 0x00, 100);
  18. memset(src_str, 0x00, 100);
  19. memset(dst_str, 0x00, 100);
  20. memset(output, 0x00, 100);
  21. unhexify( key_str, hex_key_string );
  22. unhexify( src_str, hex_src_string );
  23. mbedtls_xtea_setup( &ctx, key_str );
  24. TEST_ASSERT( mbedtls_xtea_crypt_ecb( &ctx, MBEDTLS_XTEA_ENCRYPT, src_str, output ) == 0 );
  25. hexify( dst_str, output, 8 );
  26. TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
  27. }
  28. /* END_CASE */
  29. /* BEGIN_CASE */
  30. void xtea_decrypt_ecb( char *hex_key_string, char *hex_src_string,
  31. char *hex_dst_string )
  32. {
  33. unsigned char key_str[100];
  34. unsigned char src_str[100];
  35. unsigned char dst_str[100];
  36. unsigned char output[100];
  37. mbedtls_xtea_context ctx;
  38. memset(key_str, 0x00, 100);
  39. memset(src_str, 0x00, 100);
  40. memset(dst_str, 0x00, 100);
  41. memset(output, 0x00, 100);
  42. unhexify( key_str, hex_key_string );
  43. unhexify( src_str, hex_src_string );
  44. mbedtls_xtea_setup( &ctx, key_str );
  45. TEST_ASSERT( mbedtls_xtea_crypt_ecb( &ctx, MBEDTLS_XTEA_DECRYPT, src_str, output ) == 0 );
  46. hexify( dst_str, output, 8 );
  47. TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
  48. }
  49. /* END_CASE */
  50. /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
  51. void xtea_encrypt_cbc( char *hex_key_string, char *hex_iv_string,
  52. char *hex_src_string, char *hex_dst_string )
  53. {
  54. unsigned char key_str[100];
  55. unsigned char src_str[100];
  56. unsigned char dst_str[100];
  57. unsigned char iv_str[100];
  58. unsigned char output[100];
  59. size_t len;
  60. mbedtls_xtea_context ctx;
  61. memset(key_str, 0x00, 100);
  62. memset(src_str, 0x00, 100);
  63. memset(dst_str, 0x00, 100);
  64. memset(iv_str, 0x00, 100);
  65. memset(output, 0x00, 100);
  66. unhexify( key_str, hex_key_string );
  67. unhexify( iv_str, hex_iv_string );
  68. len = unhexify( src_str, hex_src_string );
  69. mbedtls_xtea_setup( &ctx, key_str );
  70. TEST_ASSERT( mbedtls_xtea_crypt_cbc( &ctx, MBEDTLS_XTEA_ENCRYPT, len, iv_str,
  71. src_str, output ) == 0 );
  72. hexify( dst_str, output, len );
  73. TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
  74. }
  75. /* END_CASE */
  76. /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
  77. void xtea_decrypt_cbc( char *hex_key_string, char *hex_iv_string,
  78. char *hex_src_string, char *hex_dst_string )
  79. {
  80. unsigned char key_str[100];
  81. unsigned char src_str[100];
  82. unsigned char dst_str[100];
  83. unsigned char iv_str[100];
  84. unsigned char output[100];
  85. size_t len;
  86. mbedtls_xtea_context ctx;
  87. memset(key_str, 0x00, 100);
  88. memset(src_str, 0x00, 100);
  89. memset(dst_str, 0x00, 100);
  90. memset(iv_str, 0x00, 100);
  91. memset(output, 0x00, 100);
  92. unhexify( key_str, hex_key_string );
  93. unhexify( iv_str, hex_iv_string );
  94. len = unhexify( src_str, hex_src_string );
  95. mbedtls_xtea_setup( &ctx, key_str );
  96. TEST_ASSERT( mbedtls_xtea_crypt_cbc( &ctx, MBEDTLS_XTEA_DECRYPT, len, iv_str,
  97. src_str, output ) == 0 );
  98. hexify( dst_str, output, len );
  99. TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
  100. }
  101. /* END_CASE */
  102. /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
  103. void xtea_selftest()
  104. {
  105. TEST_ASSERT( mbedtls_xtea_self_test( 1 ) == 0 );
  106. }
  107. /* END_CASE */