xref: /xnu-8020.121.3/osfmk/corecrypto/ccdigest_final_64be.c (revision fdd8201d7b966f0c3ea610489d29bd841d358941)
1 /*
2  *  ccdigest_final_64be.c
3  *  corecrypto
4  *
5  *  Created on 12/06/2010
6  *
7  *  Copyright (c) 2010,2011,2015 Apple Inc. All rights reserved.
8  *
9  *
10  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
11  *
12  * This file contains Original Code and/or Modifications of Original Code
13  * as defined in and that are subject to the Apple Public Source License
14  * Version 2.0 (the 'License'). You may not use this file except in
15  * compliance with the License. The rights granted to you under the License
16  * may not be used to create, or enable the creation or redistribution of,
17  * unlawful or unlicensed copies of an Apple operating system, or to
18  * circumvent, violate, or enable the circumvention or violation of, any
19  * terms of an Apple operating system software license agreement.
20  *
21  * Please obtain a copy of the License at
22  * http://www.opensource.apple.com/apsl/ and read it before using this file.
23  *
24  * The Original Code and all software distributed under the License are
25  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
26  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
27  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
28  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
29  * Please see the License for the specific language governing rights and
30  * limitations under the License.
31  *
32  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
33  */
34 
35 #include <corecrypto/ccdigest_priv.h>
36 #include <corecrypto/cc_priv.h>
37 #include "ccdigest_internal.h"
38 
39 /* This can be used for SHA1, SHA256 and SHA224 */
40 void
ccdigest_final_64be(const struct ccdigest_info * di,ccdigest_ctx_t ctx,unsigned char * digest)41 ccdigest_final_64be(const struct ccdigest_info *di, ccdigest_ctx_t ctx, unsigned char *digest)
42 {
43 	// Sanity check to recover from ctx corruptions.
44 	if (ccdigest_num(di, ctx) >= di->block_size) {
45 		ccdigest_num(di, ctx) = 0;
46 	}
47 
48 	// Clone the state.
49 	ccdigest_di_decl(di, tmp);
50 	cc_memcpy(tmp, ctx, ccdigest_di_size(di));
51 
52 	ccdigest_nbits(di, tmp) += ccdigest_num(di, tmp) * 8;
53 	ccdigest_data(di, tmp)[ccdigest_num(di, tmp)++] = 0x80;
54 
55 	/* If we don't have at least 8 bytes (for the length) left we need to add
56 	 *  a second block. */
57 	if (ccdigest_num(di, tmp) > 64 - 8) {
58 		while (ccdigest_num(di, tmp) < 64) {
59 			ccdigest_data(di, tmp)[ccdigest_num(di, tmp)++] = 0;
60 		}
61 		di->compress(ccdigest_state(di, tmp), 1, ccdigest_data(di, tmp));
62 		ccdigest_num(di, tmp) = 0;
63 	}
64 
65 	/* pad upto block_size minus 8 with 0s */
66 	while (ccdigest_num(di, tmp) < 64 - 8) {
67 		ccdigest_data(di, tmp)[ccdigest_num(di, tmp)++] = 0;
68 	}
69 
70 	CC_STORE64_BE(ccdigest_nbits(di, tmp), ccdigest_data(di, tmp) + 64 - 8);
71 	di->compress(ccdigest_state(di, tmp), 1, ccdigest_data(di, tmp));
72 
73 	/* copy output */
74 	for (unsigned int i = 0; i < di->output_size / 4; i++) {
75 		CC_STORE32_BE(ccdigest_state_u32(di, tmp)[i], digest + (4 * i));
76 	}
77 
78 	ccdigest_di_clear(di, tmp);
79 }
80