test_suite_camellia.function 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /* BEGIN_HEADER */
  2. #include "mbedtls/camellia.h"
  3. /* END_HEADER */
  4. /* BEGIN_DEPENDENCIES
  5. * depends_on:MBEDTLS_CAMELLIA_C
  6. * END_DEPENDENCIES
  7. */
  8. /* BEGIN_CASE */
  9. void camellia_invalid_param()
  10. {
  11. mbedtls_camellia_context ctx;
  12. unsigned char buf[16] = { 0 };
  13. const int invalid_mode = 42;
  14. size_t off;
  15. ((void) off);
  16. TEST_EQUAL(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA,
  17. mbedtls_camellia_crypt_ecb(&ctx,
  18. invalid_mode,
  19. buf, buf));
  20. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  21. TEST_EQUAL(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA,
  22. mbedtls_camellia_crypt_cbc(&ctx,
  23. invalid_mode,
  24. sizeof(buf),
  25. buf, buf, buf));
  26. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  27. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  28. TEST_EQUAL(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA,
  29. mbedtls_camellia_crypt_cfb128(&ctx,
  30. invalid_mode,
  31. sizeof(buf),
  32. &off, buf,
  33. buf, buf));
  34. #endif /* MBEDTLS_CIPHER_MODE_CFB */
  35. exit:
  36. return;
  37. }
  38. /* END_CASE */
  39. /* BEGIN_CASE */
  40. void camellia_encrypt_ecb(data_t *key_str, data_t *src_str,
  41. data_t *dst, int setkey_result)
  42. {
  43. unsigned char output[100];
  44. mbedtls_camellia_context ctx;
  45. memset(output, 0x00, 100);
  46. mbedtls_camellia_init(&ctx);
  47. TEST_ASSERT(mbedtls_camellia_setkey_enc(&ctx, key_str->x, key_str->len * 8) == setkey_result);
  48. if (setkey_result == 0) {
  49. TEST_ASSERT(mbedtls_camellia_crypt_ecb(&ctx, MBEDTLS_CAMELLIA_ENCRYPT, src_str->x,
  50. output) == 0);
  51. TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x, 16, dst->len) == 0);
  52. }
  53. exit:
  54. mbedtls_camellia_free(&ctx);
  55. }
  56. /* END_CASE */
  57. /* BEGIN_CASE */
  58. void camellia_decrypt_ecb(data_t *key_str, data_t *src_str,
  59. data_t *dst, int setkey_result)
  60. {
  61. unsigned char output[100];
  62. mbedtls_camellia_context ctx;
  63. memset(output, 0x00, 100);
  64. mbedtls_camellia_init(&ctx);
  65. TEST_ASSERT(mbedtls_camellia_setkey_dec(&ctx, key_str->x, key_str->len * 8) == setkey_result);
  66. if (setkey_result == 0) {
  67. TEST_ASSERT(mbedtls_camellia_crypt_ecb(&ctx, MBEDTLS_CAMELLIA_DECRYPT, src_str->x,
  68. output) == 0);
  69. TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x, 16, dst->len) == 0);
  70. }
  71. exit:
  72. mbedtls_camellia_free(&ctx);
  73. }
  74. /* END_CASE */
  75. /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
  76. void camellia_encrypt_cbc(data_t *key_str, data_t *iv_str,
  77. data_t *src_str, data_t *dst, int cbc_result)
  78. {
  79. unsigned char output[100];
  80. mbedtls_camellia_context ctx;
  81. memset(output, 0x00, 100);
  82. mbedtls_camellia_init(&ctx);
  83. mbedtls_camellia_setkey_enc(&ctx, key_str->x, key_str->len * 8);
  84. TEST_ASSERT(mbedtls_camellia_crypt_cbc(&ctx, MBEDTLS_CAMELLIA_ENCRYPT, src_str->len, iv_str->x,
  85. src_str->x, output) == cbc_result);
  86. if (cbc_result == 0) {
  87. TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x, src_str->len,
  88. dst->len) == 0);
  89. }
  90. exit:
  91. mbedtls_camellia_free(&ctx);
  92. }
  93. /* END_CASE */
  94. /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
  95. void camellia_decrypt_cbc(data_t *key_str, data_t *iv_str,
  96. data_t *src_str, data_t *dst,
  97. int cbc_result)
  98. {
  99. unsigned char output[100];
  100. mbedtls_camellia_context ctx;
  101. memset(output, 0x00, 100);
  102. mbedtls_camellia_init(&ctx);
  103. mbedtls_camellia_setkey_dec(&ctx, key_str->x, key_str->len * 8);
  104. TEST_ASSERT(mbedtls_camellia_crypt_cbc(&ctx, MBEDTLS_CAMELLIA_DECRYPT, src_str->len, iv_str->x,
  105. src_str->x, output) == cbc_result);
  106. if (cbc_result == 0) {
  107. TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x, src_str->len,
  108. dst->len) == 0);
  109. }
  110. exit:
  111. mbedtls_camellia_free(&ctx);
  112. }
  113. /* END_CASE */
  114. /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
  115. void camellia_encrypt_cfb128(data_t *key_str, data_t *iv_str,
  116. data_t *src_str, data_t *dst)
  117. {
  118. unsigned char output[100];
  119. mbedtls_camellia_context ctx;
  120. size_t iv_offset = 0;
  121. memset(output, 0x00, 100);
  122. mbedtls_camellia_init(&ctx);
  123. mbedtls_camellia_setkey_enc(&ctx, key_str->x, key_str->len * 8);
  124. TEST_ASSERT(mbedtls_camellia_crypt_cfb128(&ctx, MBEDTLS_CAMELLIA_ENCRYPT, 16, &iv_offset,
  125. iv_str->x, src_str->x, output) == 0);
  126. TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x, 16, dst->len) == 0);
  127. exit:
  128. mbedtls_camellia_free(&ctx);
  129. }
  130. /* END_CASE */
  131. /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
  132. void camellia_decrypt_cfb128(data_t *key_str, data_t *iv_str,
  133. data_t *src_str,
  134. data_t *dst)
  135. {
  136. unsigned char output[100];
  137. mbedtls_camellia_context ctx;
  138. size_t iv_offset = 0;
  139. memset(output, 0x00, 100);
  140. mbedtls_camellia_init(&ctx);
  141. mbedtls_camellia_setkey_enc(&ctx, key_str->x, key_str->len * 8);
  142. TEST_ASSERT(mbedtls_camellia_crypt_cfb128(&ctx, MBEDTLS_CAMELLIA_DECRYPT, 16, &iv_offset,
  143. iv_str->x, src_str->x, output) == 0);
  144. TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x, 16, dst->len) == 0);
  145. exit:
  146. mbedtls_camellia_free(&ctx);
  147. }
  148. /* END_CASE */
  149. /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
  150. void camellia_selftest()
  151. {
  152. TEST_ASSERT(mbedtls_camellia_self_test(1) == 0);
  153. }
  154. /* END_CASE */