test-crypto-hmac.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * QEMU Crypto hmac algorithms tests
  3. *
  4. * Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD.
  5. *
  6. * Authors:
  7. * Longpeng(Mike) <longpeng2@huawei.com>
  8. *
  9. * This work is licensed under the terms of the GNU GPL, version 2 or
  10. * (at your option) any later version. See the COPYING file in the
  11. * top-level directory.
  12. *
  13. */
  14. #include "qemu/osdep.h"
  15. #include "crypto/init.h"
  16. #include "crypto/hmac.h"
  17. #define INPUT_TEXT1 "ABCDEFGHIJKLMNOPQRSTUVWXY"
  18. #define INPUT_TEXT2 "Zabcdefghijklmnopqrstuvwx"
  19. #define INPUT_TEXT3 "yz0123456789"
  20. #define INPUT_TEXT INPUT_TEXT1 \
  21. INPUT_TEXT2 \
  22. INPUT_TEXT3
  23. #define KEY "monkey monkey monkey monkey"
  24. typedef struct QCryptoHmacTestData QCryptoHmacTestData;
  25. struct QCryptoHmacTestData {
  26. QCryptoHashAlgo alg;
  27. const char *hex_digest;
  28. };
  29. static QCryptoHmacTestData test_data[] = {
  30. {
  31. .alg = QCRYPTO_HASH_ALGO_MD5,
  32. .hex_digest =
  33. "ede9cb83679ba82d88fbeae865b3f8fc",
  34. },
  35. {
  36. .alg = QCRYPTO_HASH_ALGO_SHA1,
  37. .hex_digest =
  38. "c7b5a631e3aac975c4ededfcd346e469"
  39. "dbc5f2d1",
  40. },
  41. {
  42. .alg = QCRYPTO_HASH_ALGO_SHA224,
  43. .hex_digest =
  44. "5f768179dbb29ca722875d0f461a2e2f"
  45. "597d0210340a84df1a8e9c63",
  46. },
  47. {
  48. .alg = QCRYPTO_HASH_ALGO_SHA256,
  49. .hex_digest =
  50. "3798f363c57afa6edaffe39016ca7bad"
  51. "efd1e670afb0e3987194307dec3197db",
  52. },
  53. {
  54. .alg = QCRYPTO_HASH_ALGO_SHA384,
  55. .hex_digest =
  56. "d218680a6032d33dccd9882d6a6a7164"
  57. "64f26623be257a9b2919b185294f4a49"
  58. "9e54b190bfd6bc5cedd2cd05c7e65e82",
  59. },
  60. {
  61. .alg = QCRYPTO_HASH_ALGO_SHA512,
  62. .hex_digest =
  63. "835a4f5b3750b4c1fccfa88da2f746a4"
  64. "900160c9f18964309bb736c13b59491b"
  65. "8e32d37b724cc5aebb0f554c6338a3b5"
  66. "94c4ba26862b2dadb59b7ede1d08d53e",
  67. },
  68. {
  69. .alg = QCRYPTO_HASH_ALGO_RIPEMD160,
  70. .hex_digest =
  71. "94964ed4c1155b62b668c241d67279e5"
  72. "8a711676",
  73. },
  74. #ifdef CONFIG_CRYPTO_SM3
  75. {
  76. .alg = QCRYPTO_HASH_ALGO_SM3,
  77. .hex_digest =
  78. "760e3799332bc913819b930085360ddb"
  79. "c05529261313d5b15b75bab4fd7ae91e",
  80. },
  81. #endif
  82. };
  83. static const char hex[] = "0123456789abcdef";
  84. static void test_hmac_alloc(void)
  85. {
  86. size_t i;
  87. for (i = 0; i < G_N_ELEMENTS(test_data); i++) {
  88. QCryptoHmacTestData *data = &test_data[i];
  89. QCryptoHmac *hmac = NULL;
  90. uint8_t *result = NULL;
  91. size_t resultlen = 0;
  92. const char *exp_output = NULL;
  93. int ret;
  94. size_t j;
  95. if (!qcrypto_hmac_supports(data->alg)) {
  96. return;
  97. }
  98. exp_output = data->hex_digest;
  99. hmac = qcrypto_hmac_new(data->alg, (const uint8_t *)KEY,
  100. strlen(KEY), &error_fatal);
  101. g_assert(hmac != NULL);
  102. ret = qcrypto_hmac_bytes(hmac, (const char *)INPUT_TEXT,
  103. strlen(INPUT_TEXT), &result,
  104. &resultlen, &error_fatal);
  105. g_assert(ret == 0);
  106. for (j = 0; j < resultlen; j++) {
  107. g_assert(exp_output[j * 2] == hex[(result[j] >> 4) & 0xf]);
  108. g_assert(exp_output[j * 2 + 1] == hex[result[j] & 0xf]);
  109. }
  110. qcrypto_hmac_free(hmac);
  111. g_free(result);
  112. }
  113. }
  114. static void test_hmac_prealloc(void)
  115. {
  116. size_t i;
  117. for (i = 0; i < G_N_ELEMENTS(test_data); i++) {
  118. QCryptoHmacTestData *data = &test_data[i];
  119. QCryptoHmac *hmac = NULL;
  120. uint8_t *result = NULL, *origresult = NULL;
  121. size_t resultlen = 0;
  122. const char *exp_output = NULL;
  123. int ret;
  124. size_t j;
  125. if (!qcrypto_hmac_supports(data->alg)) {
  126. return;
  127. }
  128. exp_output = data->hex_digest;
  129. resultlen = strlen(exp_output) / 2;
  130. origresult = result = g_new0(uint8_t, resultlen);
  131. hmac = qcrypto_hmac_new(data->alg, (const uint8_t *)KEY,
  132. strlen(KEY), &error_fatal);
  133. g_assert(hmac != NULL);
  134. ret = qcrypto_hmac_bytes(hmac, (const char *)INPUT_TEXT,
  135. strlen(INPUT_TEXT), &result,
  136. &resultlen, &error_fatal);
  137. g_assert(ret == 0);
  138. /* Validate that our pre-allocated pointer was not replaced */
  139. g_assert(result == origresult);
  140. exp_output = data->hex_digest;
  141. for (j = 0; j < resultlen; j++) {
  142. g_assert(exp_output[j * 2] == hex[(result[j] >> 4) & 0xf]);
  143. g_assert(exp_output[j * 2 + 1] == hex[result[j] & 0xf]);
  144. }
  145. qcrypto_hmac_free(hmac);
  146. g_free(result);
  147. }
  148. }
  149. static void test_hmac_iov(void)
  150. {
  151. size_t i;
  152. for (i = 0; i < G_N_ELEMENTS(test_data); i++) {
  153. QCryptoHmacTestData *data = &test_data[i];
  154. QCryptoHmac *hmac = NULL;
  155. uint8_t *result = NULL;
  156. size_t resultlen = 0;
  157. const char *exp_output = NULL;
  158. int ret;
  159. size_t j;
  160. struct iovec iov[3] = {
  161. { .iov_base = (char *)INPUT_TEXT1, .iov_len = strlen(INPUT_TEXT1) },
  162. { .iov_base = (char *)INPUT_TEXT2, .iov_len = strlen(INPUT_TEXT2) },
  163. { .iov_base = (char *)INPUT_TEXT3, .iov_len = strlen(INPUT_TEXT3) },
  164. };
  165. if (!qcrypto_hmac_supports(data->alg)) {
  166. return;
  167. }
  168. exp_output = data->hex_digest;
  169. hmac = qcrypto_hmac_new(data->alg, (const uint8_t *)KEY,
  170. strlen(KEY), &error_fatal);
  171. g_assert(hmac != NULL);
  172. ret = qcrypto_hmac_bytesv(hmac, iov, 3, &result,
  173. &resultlen, &error_fatal);
  174. g_assert(ret == 0);
  175. for (j = 0; j < resultlen; j++) {
  176. g_assert(exp_output[j * 2] == hex[(result[j] >> 4) & 0xf]);
  177. g_assert(exp_output[j * 2 + 1] == hex[result[j] & 0xf]);
  178. }
  179. qcrypto_hmac_free(hmac);
  180. g_free(result);
  181. }
  182. }
  183. static void test_hmac_digest(void)
  184. {
  185. size_t i;
  186. for (i = 0; i < G_N_ELEMENTS(test_data); i++) {
  187. QCryptoHmacTestData *data = &test_data[i];
  188. QCryptoHmac *hmac = NULL;
  189. uint8_t *result = NULL;
  190. const char *exp_output = NULL;
  191. int ret;
  192. if (!qcrypto_hmac_supports(data->alg)) {
  193. return;
  194. }
  195. exp_output = data->hex_digest;
  196. hmac = qcrypto_hmac_new(data->alg, (const uint8_t *)KEY,
  197. strlen(KEY), &error_fatal);
  198. g_assert(hmac != NULL);
  199. ret = qcrypto_hmac_digest(hmac, (const char *)INPUT_TEXT,
  200. strlen(INPUT_TEXT), (char **)&result,
  201. &error_fatal);
  202. g_assert(ret == 0);
  203. g_assert_cmpstr((const char *)result, ==, exp_output);
  204. qcrypto_hmac_free(hmac);
  205. g_free(result);
  206. }
  207. }
  208. int main(int argc, char **argv)
  209. {
  210. g_test_init(&argc, &argv, NULL);
  211. g_assert(qcrypto_init(NULL) == 0);
  212. g_test_add_func("/crypto/hmac/iov", test_hmac_iov);
  213. g_test_add_func("/crypto/hmac/alloc", test_hmac_alloc);
  214. g_test_add_func("/crypto/hmac/prealloc", test_hmac_prealloc);
  215. g_test_add_func("/crypto/hmac/digest", test_hmac_digest);
  216. return g_test_run();
  217. }