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