xref: /isa-l_crypto/sha1_mb/sha1_multi_buffer_example.c (revision dabcf5870f52bef92fcd58f5d75ba7f356301b41)
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 {
61 	int i;
62 	for (i = 0; i < words; i++)
63 		if (good[i] != ref[i])
64 			return 1;
65 
66 	return 0;
67 }
68 
69 #define MAX_MSGS 7
70 
71 int main(void)
72 {
73 	SHA1_HASH_CTX_MGR *mgr = NULL;
74 	SHA1_HASH_CTX ctxpool[MAX_MSGS];
75 	SHA1_HASH_CTX *p_job;
76 	int i, checked = 0, failed = 0;
77 	int n = sizeof(msgs) / sizeof(msgs[0]);
78 	int ret;
79 
80 	ret = posix_memalign((void *)&mgr, 16, sizeof(SHA1_HASH_CTX_MGR));
81 	if ((ret != 0) || (mgr == NULL)) {
82 		printf("posix_memalign failed test aborted\n");
83 		return 1;
84 	}
85 	// Initialize multi-buffer manager
86 	sha1_ctx_mgr_init(mgr);
87 
88 	for (i = 0; i < n; i++) {
89 		hash_ctx_init(&ctxpool[i]);
90 		ctxpool[i].user_data = (void *)expected_digest[i];
91 
92 		p_job = sha1_ctx_mgr_submit(mgr, &ctxpool[i], msgs[i],
93 					    strlen((char *)msgs[i]), HASH_ENTIRE);
94 
95 		if (p_job) {	// If we have finished a job, process it
96 			checked++;
97 			failed +=
98 			    check_job(p_job->job.result_digest, p_job->user_data,
99 				      SHA1_DIGEST_NWORDS);
100 		}
101 	}
102 
103 	// Finish remaining jobs
104 	while (NULL != (p_job = sha1_ctx_mgr_flush(mgr))) {
105 		checked++;
106 		failed +=
107 		    check_job(p_job->job.result_digest, p_job->user_data, SHA1_DIGEST_NWORDS);
108 	}
109 
110 	printf("Example multi-buffer sha1 completed=%d, failed=%d\n", checked, failed);
111 	return failed;
112 }
113