]> git.proxmox.com Git - ceph.git/blob - ceph/src/crypto/isa-l/isa-l_crypto/mh_sha1/mh_sha1_finalize_base.c
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / crypto / isa-l / isa-l_crypto / mh_sha1 / mh_sha1_finalize_base.c
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
30 /*
31 * mh_sha1_finalize_base.c contains the prototypes of mh_sha1_finalize_XXX
32 * and mh_sha1_tail_XXX. Default definitions are base type which generates
33 * mh_sha1_finalize_base and mh_sha1_tail_base. Other types are generated
34 * through different predefined macros by mh_sha1.c.
35 * mh_sha1_tail is used to calculate the last incomplete block of input
36 * data. mh_sha1_finalize is the mh_sha1_ctx wrapper of mh_sha1_tail.
37 */
38 #ifndef MH_SHA1_FINALIZE_FUNCTION
39 #include <string.h>
40 #include "mh_sha1_internal.h"
41
42 #define MH_SHA1_FINALIZE_FUNCTION mh_sha1_finalize_base
43 #define MH_SHA1_TAIL_FUNCTION mh_sha1_tail_base
44 #define MH_SHA1_BLOCK_FUNCTION mh_sha1_block_base
45 #define MH_SHA1_FINALIZE_SLVER
46 #endif
47
48 void MH_SHA1_TAIL_FUNCTION(uint8_t * partial_buffer, uint32_t total_len,
49 uint32_t(*mh_sha1_segs_digests)[HASH_SEGS], uint8_t * frame_buffer,
50 uint32_t digests[SHA1_DIGEST_WORDS])
51 {
52 uint64_t partial_buffer_len, len_in_bit;
53
54 partial_buffer_len = total_len % MH_SHA1_BLOCK_SIZE;
55
56 // Padding the first block
57 partial_buffer[partial_buffer_len] = 0x80;
58 partial_buffer_len++;
59 memset(partial_buffer + partial_buffer_len, 0,
60 MH_SHA1_BLOCK_SIZE - partial_buffer_len);
61
62 // Calculate the first block without total_length if padding needs 2 block
63 if (partial_buffer_len > (MH_SHA1_BLOCK_SIZE - 8)) {
64 MH_SHA1_BLOCK_FUNCTION(partial_buffer, mh_sha1_segs_digests, frame_buffer, 1);
65 //Padding the second block
66 memset(partial_buffer, 0, MH_SHA1_BLOCK_SIZE);
67 }
68 //Padding the block
69 len_in_bit = bswap64((uint64_t) total_len * 8);
70 *(uint64_t *) (partial_buffer + MH_SHA1_BLOCK_SIZE - 8) = len_in_bit;
71 MH_SHA1_BLOCK_FUNCTION(partial_buffer, mh_sha1_segs_digests, frame_buffer, 1);
72
73 //Calculate multi-hash SHA1 digests (segment digests as input message)
74 sha1_for_mh_sha1((uint8_t *) mh_sha1_segs_digests, digests,
75 4 * SHA1_DIGEST_WORDS * HASH_SEGS);
76
77 return;
78 }
79
80 int MH_SHA1_FINALIZE_FUNCTION(struct mh_sha1_ctx *ctx, void *mh_sha1_digest)
81 {
82 uint8_t *partial_block_buffer;
83 uint64_t total_len;
84 uint32_t(*mh_sha1_segs_digests)[HASH_SEGS];
85 uint8_t *aligned_frame_buffer;
86
87 if (ctx == NULL)
88 return MH_SHA1_CTX_ERROR_NULL;
89
90 total_len = ctx->total_length;
91 partial_block_buffer = ctx->partial_block_buffer;
92
93 /* mh_sha1 tail */
94 aligned_frame_buffer = (uint8_t *) ALIGN_64(ctx->frame_buffer);
95 mh_sha1_segs_digests = (uint32_t(*)[HASH_SEGS]) ctx->mh_sha1_interim_digests;
96
97 MH_SHA1_TAIL_FUNCTION(partial_block_buffer, total_len, mh_sha1_segs_digests,
98 aligned_frame_buffer, ctx->mh_sha1_digest);
99
100 /* Output the digests of mh_sha1 */
101 if (mh_sha1_digest != NULL) {
102 ((uint32_t *) mh_sha1_digest)[0] = ctx->mh_sha1_digest[0];
103 ((uint32_t *) mh_sha1_digest)[1] = ctx->mh_sha1_digest[1];
104 ((uint32_t *) mh_sha1_digest)[2] = ctx->mh_sha1_digest[2];
105 ((uint32_t *) mh_sha1_digest)[3] = ctx->mh_sha1_digest[3];
106 ((uint32_t *) mh_sha1_digest)[4] = ctx->mh_sha1_digest[4];
107 }
108
109 return MH_SHA1_CTX_ERROR_NONE;
110 }
111
112 #ifdef MH_SHA1_FINALIZE_SLVER
113 struct slver {
114 uint16_t snum;
115 uint8_t ver;
116 uint8_t core;
117 };
118
119 // Version info
120 struct slver mh_sha1_finalize_base_slver_0000027b;
121 struct slver mh_sha1_finalize_base_slver = { 0x027b, 0x00, 0x00 };
122 #endif