]>
git.proxmox.com Git - ceph.git/blob - ceph/src/crypto/isa-l/isa-l_crypto/sha1_mb/sha1_mb_rand_test.c
1 /**********************************************************************
2 Copyright(c) 2011-2016 Intel Corporation All rights reserved.
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
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
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.
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 **********************************************************************/
34 #define TEST_LEN (1024*1024)
40 # define TEST_SEED 0x1234
43 static uint32_t digest_ref
[TEST_BUFS
][SHA1_DIGEST_NWORDS
];
45 // Compare against reference function
46 extern void sha1_ref(uint8_t * input_data
, uint32_t * digest
, uint32_t len
);
48 // Generates pseudo-random data
49 void rand_buffer(unsigned char *buf
, const long buffer_size
)
52 for (i
= 0; i
< buffer_size
; i
++)
58 SHA1_HASH_CTX_MGR
*mgr
= NULL
;
59 SHA1_HASH_CTX ctxpool
[TEST_BUFS
];
60 uint32_t i
, j
, fail
= 0;
61 unsigned char *bufs
[TEST_BUFS
];
62 uint32_t lens
[TEST_BUFS
];
67 printf("multibinary_sha1 test, %d sets of %dx%d max: ", RANDOMS
, TEST_BUFS
, TEST_LEN
);
69 ret
= posix_memalign((void *)&mgr
, 16, sizeof(SHA1_HASH_CTX_MGR
));
70 if ((ret
!= 0) || (mgr
== NULL
)) {
71 printf("posix_memalign failed test aborted\n");
75 sha1_ctx_mgr_init(mgr
);
79 for (i
= 0; i
< TEST_BUFS
; i
++) {
80 // Allocate and fill buffer
81 bufs
[i
] = (unsigned char *)malloc(TEST_LEN
);
82 if (bufs
[i
] == NULL
) {
83 printf("malloc failed test aborted\n");
86 rand_buffer(bufs
[i
], TEST_LEN
);
89 hash_ctx_init(&ctxpool
[i
]);
90 ctxpool
[i
].user_data
= (void *)((uint64_t) i
);
93 sha1_ref(bufs
[i
], digest_ref
[i
], TEST_LEN
);
96 sha1_ctx_mgr_submit(mgr
, &ctxpool
[i
], bufs
[i
], TEST_LEN
, HASH_ENTIRE
);
99 while (sha1_ctx_mgr_flush(mgr
)) ;
101 for (i
= 0; i
< TEST_BUFS
; i
++) {
102 for (j
= 0; j
< SHA1_DIGEST_NWORDS
; j
++) {
103 if (ctxpool
[i
].job
.result_digest
[j
] != digest_ref
[i
][j
]) {
105 printf("Test%d fixed size, digest%d "
106 "fail 0x%08X <=> 0x%08X \n",
107 i
, j
, ctxpool
[i
].job
.result_digest
[j
],
114 printf("Test failed function check %d\n", fail
);
117 // Run tests with random size and number of jobs
118 for (t
= 0; t
< RANDOMS
; t
++) {
119 jobs
= rand() % (TEST_BUFS
);
121 sha1_ctx_mgr_init(mgr
);
123 for (i
= 0; i
< jobs
; i
++) {
124 // Use buffer with random len and contents
125 lens
[i
] = rand() % (TEST_LEN
);
126 rand_buffer(bufs
[i
], lens
[i
]);
128 // Run reference test
129 sha1_ref(bufs
[i
], digest_ref
[i
], lens
[i
]);
132 sha1_ctx_mgr_submit(mgr
, &ctxpool
[i
], bufs
[i
], lens
[i
], HASH_ENTIRE
);
135 while (sha1_ctx_mgr_flush(mgr
)) ;
137 for (i
= 0; i
< jobs
; i
++) {
138 for (j
= 0; j
< SHA1_DIGEST_NWORDS
; j
++) {
139 if (ctxpool
[i
].job
.result_digest
[j
] != digest_ref
[i
][j
]) {
141 printf("Test%d, digest%d fail "
142 "0x%08X <=> 0x%08X\n",
143 i
, j
, ctxpool
[i
].job
.result_digest
[j
],
149 printf("Test failed function check %d\n", fail
);
157 // Test at the end of buffer
158 jobs
= rand() % TEST_BUFS
;
159 tmp_buf
= (uint8_t *) malloc(sizeof(uint8_t) * jobs
);
161 printf("malloc failed, end test aborted.\n");
165 rand_buffer(tmp_buf
, jobs
);
167 sha1_ctx_mgr_init(mgr
);
169 // Extend to the end of allocated buffer to construct jobs
170 for (i
= 0; i
< jobs
; i
++) {
171 bufs
[i
] = (uint8_t *) & tmp_buf
[i
];
175 sha1_ref(bufs
[i
], digest_ref
[i
], lens
[i
]);
178 sha1_ctx_mgr_submit(mgr
, &ctxpool
[i
], bufs
[i
], lens
[i
], HASH_ENTIRE
);
181 while (sha1_ctx_mgr_flush(mgr
)) ;
183 for (i
= 0; i
< jobs
; i
++) {
184 for (j
= 0; j
< SHA1_DIGEST_NWORDS
; j
++) {
185 if (ctxpool
[i
].job
.result_digest
[j
] != digest_ref
[i
][j
]) {
187 printf("End test failed at offset %d - result: 0x%08X"
188 ", ref: 0x%08X\n", i
, ctxpool
[i
].job
.result_digest
[j
],
197 printf("Test failed function check %d\n", fail
);
199 printf(" multibinary_sha1 rand: Pass\n");