xref: /isa-l_crypto/sha1_mb/sha1_multi_buffer_example.c (revision 8cb7fe780eac8ee5f1e0aa3ca37466e89c673ccd)
1 /**********************************************************************
2   Copyright(c) 2011-2016 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 #include <stdio.h>
30 #include <stdint.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include "sha1_mb.h"
34 #include "test.h"
35 
36 // Test messages
37 #define TST_STR "0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWX"
38 uint8_t msg1[] = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
39 uint8_t msg2[] = "0123456789:;<=>?@ABCDEFGHIJKLMNO";
40 uint8_t msg3[] = TST_STR TST_STR "0123456789:;<";
41 uint8_t msg4[] = TST_STR TST_STR TST_STR "0123456789:;<=>?@ABCDEFGHIJKLMNOPQR";
42 uint8_t msg5[] = TST_STR TST_STR TST_STR TST_STR TST_STR "0123456789:;<=>?";
43 uint8_t msg6[] =
44         TST_STR TST_STR TST_STR TST_STR TST_STR TST_STR "0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTU";
45 uint8_t msg7[] = "";
46 
47 // Expected digests
48 uint32_t dgst1[] = { 0x84983E44, 0x1C3BD26E, 0xBAAE4AA1, 0xF95129E5, 0xE54670F1 };
49 uint32_t dgst2[] = { 0xB7C66452, 0x0FD122B3, 0x55D539F2, 0xA35E6FAA, 0xC2A5A11D };
50 uint32_t dgst3[] = { 0x127729B6, 0xA8B2F8A0, 0xA4DDC819, 0x08E1D8B3, 0x67CEEA55 };
51 uint32_t dgst4[] = { 0xFDDE2D00, 0xABD5B7A3, 0x699DE6F2, 0x3FF1D1AC, 0x3B872AC2 };
52 uint32_t dgst5[] = { 0xE7FCA85C, 0xA4AB3740, 0x6A180B32, 0x0B8D362C, 0x622A96E6 };
53 uint32_t dgst6[] = { 0x505B0686, 0xE1ACDF42, 0xB3588B5A, 0xB043D52C, 0x6D8C7444 };
54 uint32_t dgst7[] = { 0xDA39A3EE, 0x5E6B4B0D, 0x3255BFEF, 0x95601890, 0xAFD80709 };
55 
56 uint8_t *msgs[] = { msg1, msg2, msg3, msg4, msg5, msg6, msg7 };
57 uint32_t *expected_digest[] = { dgst1, dgst2, dgst3, dgst4, dgst5, dgst6, dgst7 };
58 
59 int
check_job(uint32_t * ref,uint32_t * good,int words)60 check_job(uint32_t *ref, uint32_t *good, int words)
61 {
62         int i;
63         for (i = 0; i < words; i++)
64                 if (good[i] != ref[i])
65                         return 1;
66 
67         return 0;
68 }
69 
70 #define MAX_MSGS 7
71 
72 int
main(void)73 main(void)
74 {
75         ISAL_SHA1_HASH_CTX_MGR *mgr = NULL;
76         ISAL_SHA1_HASH_CTX ctxpool[MAX_MSGS];
77         ISAL_SHA1_HASH_CTX *p_job;
78         int i, checked = 0, failed = 0;
79         int n = sizeof(msgs) / sizeof(msgs[0]);
80         int ret;
81 
82         ret = posix_memalign((void *) &mgr, 16, sizeof(ISAL_SHA1_HASH_CTX_MGR));
83         if ((ret != 0) || (mgr == NULL)) {
84                 printf("posix_memalign failed test aborted\n");
85                 return 1;
86         }
87         // Initialize multi-buffer manager
88         ret = isal_sha1_ctx_mgr_init(mgr);
89         if (ret)
90                 return 1;
91 
92         for (i = 0; i < n; i++) {
93                 isal_hash_ctx_init(&ctxpool[i]);
94                 ctxpool[i].user_data = (void *) expected_digest[i];
95 
96                 ret = isal_sha1_ctx_mgr_submit(mgr, &ctxpool[i], &p_job, msgs[i],
97                                                strlen((char *) msgs[i]), ISAL_HASH_ENTIRE);
98                 if (ret)
99                         return 1;
100 
101                 if (p_job) { // If we have finished a job, process it
102                         checked++;
103                         failed += check_job(p_job->job.result_digest, p_job->user_data,
104                                             ISAL_SHA1_DIGEST_NWORDS);
105                 }
106         }
107 
108         // Finish remaining jobs
109         do {
110                 ret = isal_sha1_ctx_mgr_flush(mgr, &p_job);
111                 if (ret)
112                         return 1;
113                 if (p_job != NULL) {
114                         checked++;
115                         failed += check_job(p_job->job.result_digest, p_job->user_data,
116                                             ISAL_SHA1_DIGEST_NWORDS);
117                 }
118         } while (p_job != NULL);
119 
120         printf("Example multi-buffer sha1 completed=%d, failed=%d\n", checked, failed);
121         return failed;
122 }
123