hmac_test.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* Copyright 2016 The Chromium OS Authors. All rights reserved.
  2. * Use of this source code is governed by a BSD-style license that can be
  3. * found in the LICENSE file.
  4. */
  5. #include <stdint.h>
  6. #include <string.h>
  7. #include <stdio.h>
  8. #include <openssl/hmac.h>
  9. #include "2sha.h"
  10. #include "2hmac.h"
  11. #include "test_common.h"
  12. const char short_key[] = "key";
  13. const char message[] = "The quick brown fox jumps over the lazy dog";
  14. /* This is supposed to be longer than the supported block sizes */
  15. const char long_key[] =
  16. "loooooooooooooooooooooooooooooooooooooooooooonooooooooooooooooooo"
  17. "ooooooooooooooooooooooooooooooooooooooooooooonooooooooooooog key";
  18. static void test_hmac_by_openssl(enum vb2_hash_algorithm alg,
  19. const void *key, uint32_t key_size,
  20. const void *msg, uint32_t msg_size)
  21. {
  22. uint8_t mac[VB2_MAX_DIGEST_SIZE];
  23. uint32_t mac_size = sizeof(mac);
  24. uint8_t md[VB2_MAX_DIGEST_SIZE];
  25. uint32_t md_size = sizeof(md);
  26. char test_name[256];
  27. switch (alg) {
  28. case VB2_HASH_SHA1:
  29. HMAC(EVP_sha1(), key, key_size, msg, msg_size, md, &md_size);
  30. break;
  31. case VB2_HASH_SHA256:
  32. HMAC(EVP_sha256(), key, key_size, msg, msg_size, md, &md_size);
  33. break;
  34. case VB2_HASH_SHA512:
  35. HMAC(EVP_sha512(), key, key_size, msg, msg_size, md, &md_size);
  36. break;
  37. default:
  38. TEST_SUCC(-1, "Unsupported hash algorithm");
  39. }
  40. sprintf(test_name, "%s: HMAC-%s (key_size=%d)",
  41. __func__, vb2_get_hash_algorithm_name(alg), key_size);
  42. TEST_SUCC(hmac(alg, key, key_size, msg, msg_size, mac, mac_size),
  43. test_name);
  44. TEST_SUCC(memcmp(mac, md, md_size), "HMAC digests match");
  45. }
  46. static void test_hmac_error(void)
  47. {
  48. uint8_t mac[VB2_MAX_DIGEST_SIZE];
  49. enum vb2_hash_algorithm alg;
  50. alg = VB2_HASH_SHA1;
  51. TEST_TRUE(hmac(alg, NULL, 0,
  52. message, strlen(message), mac, sizeof(mac)),
  53. "key = NULL");
  54. TEST_TRUE(hmac(alg, short_key, strlen(short_key),
  55. NULL, 0, mac, sizeof(mac)),
  56. "msg = NULL");
  57. TEST_TRUE(hmac(alg, short_key, strlen(short_key),
  58. message, strlen(message), NULL, 0),
  59. "mac = NULL");
  60. TEST_TRUE(hmac(alg, short_key, strlen(short_key),
  61. message, strlen(message), mac, 0),
  62. "Buffer too small");
  63. alg = -1;
  64. TEST_TRUE(hmac(alg, short_key, strlen(short_key),
  65. message, strlen(message), mac, sizeof(mac)),
  66. "Invalid algorithm");
  67. }
  68. static void test_hmac(void)
  69. {
  70. int alg;
  71. for (alg = 1; alg < VB2_HASH_ALG_COUNT; alg++) {
  72. /* Try short key */
  73. test_hmac_by_openssl(alg, short_key, strlen(short_key),
  74. message, strlen(message));
  75. /* Try key longer than a block size */
  76. test_hmac_by_openssl(alg, long_key, strlen(long_key),
  77. message, strlen(message));
  78. /* Try empty key and message */
  79. test_hmac_by_openssl(alg, "", 0, "", 0);
  80. }
  81. }
  82. int main(void)
  83. {
  84. test_hmac();
  85. test_hmac_error();
  86. return gTestSuccess ? 0 : 255;
  87. }