2
0

sm3_internal_test.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * Copyright 2021-2022 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright 2021 UnionTech. All Rights Reserved.
  4. *
  5. * Licensed under the Apache License 2.0 (the "License"). You may not use
  6. * this file except in compliance with the License. You can obtain a copy
  7. * in the file LICENSE in the source distribution or at
  8. * https://www.openssl.org/source/license.html
  9. */
  10. /*
  11. * Internal tests for the SM3 module.
  12. */
  13. #include <string.h>
  14. #include <openssl/opensslconf.h>
  15. #include "testutil.h"
  16. #ifndef OPENSSL_NO_SM3
  17. # include "internal/sm3.h"
  18. static int test_sm3(void)
  19. {
  20. static const unsigned char input1[] = {
  21. 0x61, 0x62, 0x63
  22. };
  23. /*
  24. * This test vector comes from Example 1 (A.1) of GM/T 0004-2012
  25. */
  26. static const unsigned char expected1[SM3_DIGEST_LENGTH] = {
  27. 0x66, 0xc7, 0xf0, 0xf4, 0x62, 0xee, 0xed, 0xd9,
  28. 0xd1, 0xf2, 0xd4, 0x6b, 0xdc, 0x10, 0xe4, 0xe2,
  29. 0x41, 0x67, 0xc4, 0x87, 0x5c, 0xf2, 0xf7, 0xa2,
  30. 0x29, 0x7d, 0xa0, 0x2b, 0x8f, 0x4b, 0xa8, 0xe0
  31. };
  32. static const unsigned char input2[] = {
  33. 0x61, 0x62, 0x63, 0x64, 0x61, 0x62, 0x63, 0x64,
  34. 0x61, 0x62, 0x63, 0x64, 0x61, 0x62, 0x63, 0x64,
  35. 0x61, 0x62, 0x63, 0x64, 0x61, 0x62, 0x63, 0x64,
  36. 0x61, 0x62, 0x63, 0x64, 0x61, 0x62, 0x63, 0x64,
  37. 0x61, 0x62, 0x63, 0x64, 0x61, 0x62, 0x63, 0x64,
  38. 0x61, 0x62, 0x63, 0x64, 0x61, 0x62, 0x63, 0x64,
  39. 0x61, 0x62, 0x63, 0x64, 0x61, 0x62, 0x63, 0x64,
  40. 0x61, 0x62, 0x63, 0x64, 0x61, 0x62, 0x63, 0x64
  41. };
  42. /*
  43. * This test vector comes from Example 2 (A.2) from GM/T 0004-2012
  44. */
  45. static const unsigned char expected2[SM3_DIGEST_LENGTH] = {
  46. 0xde, 0xbe, 0x9f, 0xf9, 0x22, 0x75, 0xb8, 0xa1,
  47. 0x38, 0x60, 0x48, 0x89, 0xc1, 0x8e, 0x5a, 0x4d,
  48. 0x6f, 0xdb, 0x70, 0xe5, 0x38, 0x7e, 0x57, 0x65,
  49. 0x29, 0x3d, 0xcb, 0xa3, 0x9c, 0x0c, 0x57, 0x32
  50. };
  51. SM3_CTX ctx1, ctx2;
  52. unsigned char md1[SM3_DIGEST_LENGTH], md2[SM3_DIGEST_LENGTH];
  53. if (!TEST_true(ossl_sm3_init(&ctx1))
  54. || !TEST_true(ossl_sm3_update(&ctx1, input1, sizeof(input1)))
  55. || !TEST_true(ossl_sm3_final(md1, &ctx1))
  56. || !TEST_mem_eq(md1, SM3_DIGEST_LENGTH, expected1, SM3_DIGEST_LENGTH))
  57. return 0;
  58. if (!TEST_true(ossl_sm3_init(&ctx2))
  59. || !TEST_true(ossl_sm3_update(&ctx2, input2, sizeof(input2)))
  60. || !TEST_true(ossl_sm3_final(md2, &ctx2))
  61. || !TEST_mem_eq(md2, SM3_DIGEST_LENGTH, expected2, SM3_DIGEST_LENGTH))
  62. return 0;
  63. return 1;
  64. }
  65. #endif
  66. int setup_tests(void)
  67. {
  68. #ifndef OPENSSL_NO_SM3
  69. ADD_TEST(test_sm3);
  70. #endif
  71. return 1;
  72. }