xref: /isa-l_crypto/sm3_mb/sm3_ref_test.c (revision 228b4c8d752832b075901a0857f4eed11e8e25ff)
1  /**********************************************************************
2    Copyright(c) 2011-2019 Intel Corporation All rights reserved.
3  
4    Redistribution and use in source and binary forms, with or without
5    modification, are permitted provided that the following conditions
6    are met:
7      * Redistributions of source code must retain the above copyright
8        notice, this list of conditions and the following disclaimer.
9      * Redistributions in binary form must reproduce the above copyright
10        notice, this list of conditions and the following disclaimer in
11        the documentation and/or other materials provided with the
12        distribution.
13      * Neither the name of Intel Corporation nor the names of its
14        contributors may be used to endorse or promote products derived
15        from this software without specific prior written permission.
16  
17    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20    A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21    OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22    SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23    LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27    OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  **********************************************************************/
29  #define ISAL_UNIT_TEST
30  #include <stdio.h>
31  #include <stdlib.h>
32  #include <string.h>
33  #include "sm3_mb.h"
34  #include "endian_helper.h"
35  
36  typedef uint32_t digest_sm3[SM3_DIGEST_NWORDS];
37  
38  #define MSGS     2
39  #define NUM_JOBS 1000
40  
41  #define PSEUDO_RANDOM_NUM(seed) ((seed) * 5 + ((seed) * (seed)) / 64) % MSGS
42  
43  static uint8_t msg1[] = "abc";
44  static uint8_t msg2[] = "abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd";
45  
46  /* small endian */
47  static digest_sm3 exp_result_digest1 = { 0x66c7f0f4, 0x62eeedd9, 0xd1f2d46b, 0xdc10e4e2,
48                                           0x4167c487, 0x5cf2f7a2, 0x297da02b, 0x8f4ba8e0 };
49  
50  /* small endian */
51  static digest_sm3 exp_result_digest2 = { 0xdebe9ff9, 0x2275b8a1, 0x38604889, 0xc18e5a4d,
52                                           0x6fdb70e5, 0x387e5765, 0x293dcba3, 0x9c0c5732 };
53  
54  static uint8_t *msgs[MSGS] = { msg1, msg2 };
55  
56  static uint32_t *exp_result_digest[MSGS] = { exp_result_digest1, exp_result_digest2 };
57  
58  int
59  main(void)
60  {
61          SM3_HASH_CTX_MGR *mgr = NULL;
62          SM3_HASH_CTX ctxpool[NUM_JOBS], *ctx = NULL;
63          uint32_t i, j, k, t, checked = 0;
64          uint32_t *good;
65          int rc, ret = -1;
66  
67          rc = posix_memalign((void *) &mgr, 16, sizeof(SM3_HASH_CTX_MGR));
68          if ((rc != 0) || (mgr == NULL)) {
69                  printf("posix_memalign failed test aborted\n");
70                  return 1;
71          }
72  
73          sm3_ctx_mgr_init(mgr);
74  
75          // Init contexts before first use
76          for (i = 0; i < MSGS; i++) {
77                  hash_ctx_init(&ctxpool[i]);
78                  ctxpool[i].user_data = (void *) ((uint64_t) i);
79          }
80  
81          for (i = 0; i < MSGS; i++) {
82                  ctx = sm3_ctx_mgr_submit(mgr, &ctxpool[i], msgs[i], strlen((char *) msgs[i]),
83                                           HASH_ENTIRE);
84  
85                  if (ctx) {
86                          t = (uint32_t) ((uintptr_t) ctx->user_data);
87                          good = exp_result_digest[t];
88                          checked++;
89                          for (j = 0; j < SM3_DIGEST_NWORDS; j++) {
90                                  if (byteswap32(good[j]) != ctxpool[t].job.result_digest[j]) {
91                                          printf("Test %d, digest %d is %08X, should be %08X\n", t, j,
92                                                 ctxpool[t].job.result_digest[j],
93                                                 byteswap32(good[j]));
94                                          goto end;
95                                  }
96                          }
97  
98                          if (ctx->error) {
99                                  printf("Something bad happened during the submit."
100                                         " Error code: %d",
101                                         ctx->error);
102                                  goto end;
103                          }
104                  }
105          }
106  
107          while (1) {
108                  ctx = sm3_ctx_mgr_flush(mgr);
109  
110                  if (ctx) {
111                          t = (unsigned long) (uintptr_t) (ctx->user_data);
112                          good = exp_result_digest[t];
113                          checked++;
114                          for (j = 0; j < SM3_DIGEST_NWORDS; j++) {
115                                  if (byteswap32(good[j]) != ctxpool[t].job.result_digest[j]) {
116                                          printf("Test %d, digest %d is %08X, should be %08X\n", t, j,
117                                                 ctxpool[t].job.result_digest[j],
118                                                 byteswap32(good[j]));
119                                          goto end;
120                                  }
121                          }
122  
123                          if (ctx->error) {
124                                  printf("Something bad happened during the submit."
125                                         " Error code: %d",
126                                         ctx->error);
127                                  goto end;
128                          }
129                  } else {
130                          break;
131                  }
132          }
133  
134          // do larger test in pseudo-random order
135  
136          // Init contexts before first use
137          for (i = 0; i < NUM_JOBS; i++) {
138                  hash_ctx_init(&ctxpool[i]);
139                  ctxpool[i].user_data = (void *) ((uint64_t) i);
140          }
141  
142          checked = 0;
143          for (i = 0; i < NUM_JOBS; i++) {
144                  j = PSEUDO_RANDOM_NUM(i);
145                  ctx = sm3_ctx_mgr_submit(mgr, &ctxpool[i], msgs[j], strlen((char *) msgs[j]),
146                                           HASH_ENTIRE);
147                  if (ctx) {
148                          t = (unsigned long) (uintptr_t) (ctx->user_data);
149                          k = PSEUDO_RANDOM_NUM(t);
150                          good = exp_result_digest[k];
151                          checked++;
152                          for (j = 0; j < SM3_DIGEST_NWORDS; j++) {
153                                  if (byteswap32(good[j]) != ctxpool[t].job.result_digest[j]) {
154                                          printf("Test %d, digest %d is %08X, should be %08X\n", t, j,
155                                                 ctxpool[t].job.result_digest[j],
156                                                 byteswap32(good[j]));
157                                          goto end;
158                                  }
159                          }
160  
161                          if (ctx->error) {
162                                  printf("Something bad happened during the"
163                                         " submit. Error code: %d",
164                                         ctx->error);
165                                  goto end;
166                          }
167                  }
168          }
169          while (1) {
170                  ctx = sm3_ctx_mgr_flush(mgr);
171  
172                  if (ctx) {
173                          t = (unsigned long) (uintptr_t) (ctx->user_data);
174                          k = PSEUDO_RANDOM_NUM(t);
175                          good = exp_result_digest[k];
176                          checked++;
177                          for (j = 0; j < SM3_DIGEST_NWORDS; j++) {
178                                  if (byteswap32(good[j]) != ctxpool[t].job.result_digest[j]) {
179                                          printf("Test %d, digest %d is %08X, should be %08X\n", t, j,
180                                                 ctxpool[t].job.result_digest[j],
181                                                 byteswap32(good[j]));
182                                          goto end;
183                                  }
184                          }
185  
186                          if (ctx->error) {
187                                  printf("Something bad happened during the submit."
188                                         " Error code: %d",
189                                         ctx->error);
190                                  goto end;
191                          }
192                  } else {
193                          break;
194                  }
195          }
196  
197          if (checked != NUM_JOBS) {
198                  printf("only tested %d rather than %d\n", checked, NUM_JOBS);
199                  goto end;
200          }
201          ret = 0;
202  
203          printf(" multibinary_sm3 test: Pass\n");
204  end:
205          aligned_free(mgr);
206  
207          return ret;
208  }
209