1 /*
2 * Copyright (c) 2001-2021, Arm Limited and Contributors. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7 #define CC_PAL_LOG_CUR_COMPONENT CC_LOG_MASK_CC_API
8
9 #include "cc_pal_abort.h"
10 #include "mbedtls_cc_sha512_t.h"
11 #include "mbedtls/sha512.h"
12 #include "cc_pal_mem.h"
13
14 #if defined(MBEDTLS_SHA512_C)
15
16 #include "mbedtls/sha512.h"
17
18 #if defined(_MSC_VER) || defined(__WATCOMC__)
19 #define UL64(x) x##ui64
20 #else
21 #define UL64(x) x##ULL
22 #endif
23
24 #include <string.h>
25
26
27 #define MBEDTLS_SHA512_T_224_DIGEST_SIZE_BYTES 28
28 #define MBEDTLS_SHA512_T_256_DIGEST_SIZE_BYTES 32
29
mbedtls_sha512_t_init(mbedtls_sha512_context * ctx)30 void mbedtls_sha512_t_init( mbedtls_sha512_context *ctx )
31 {
32 if (ctx == NULL) {
33 CC_PalAbort("mbedtls_sha512_context cannot be NULL");
34 }
35 mbedtls_sha512_init(ctx);
36 }
37
mbedtls_sha512_t_free(mbedtls_sha512_context * ctx)38 void mbedtls_sha512_t_free( mbedtls_sha512_context *ctx )
39 {
40 if (ctx != NULL) {
41 mbedtls_sha512_free(ctx);
42 }
43 }
44
45 /*
46 * SHA-512_t context setup
47 */
mbedtls_sha512_t_starts(mbedtls_sha512_context * ctx,int is224)48 void mbedtls_sha512_t_starts( mbedtls_sha512_context *ctx, int is224 )
49 {
50 if (ctx == NULL) {
51 CC_PalAbort("mbedtls_sha512_context cannot be NULL");
52 }
53
54 if (is224 != 0 && is224 != 1 ) {
55 CC_PalAbort("mbedtls_sha512_starts: is224 must be 0 or 1");
56 }
57
58 ctx->MBEDTLS_PRIVATE(total)[0] = 0;
59 ctx->MBEDTLS_PRIVATE(total)[1] = 0;
60
61 if( is224 == 1 ) {
62 /* SHA-512/224 */
63 ctx->MBEDTLS_PRIVATE(state)[0] = UL64(0x8C3D37C819544DA2);
64 ctx->MBEDTLS_PRIVATE(state)[1] = UL64(0x73E1996689DCD4D6);
65 ctx->MBEDTLS_PRIVATE(state)[2] = UL64(0x1DFAB7AE32FF9C82);
66 ctx->MBEDTLS_PRIVATE(state)[3] = UL64(0x679DD514582F9FCF);
67 ctx->MBEDTLS_PRIVATE(state)[4] = UL64(0x0F6D2B697BD44DA8);
68 ctx->MBEDTLS_PRIVATE(state)[5] = UL64(0x77E36F7304C48942);
69 ctx->MBEDTLS_PRIVATE(state)[6] = UL64(0x3F9D85A86A1D36C8);
70 ctx->MBEDTLS_PRIVATE(state)[7] = UL64(0x1112E6AD91D692A1);
71 }
72 else {
73 /* SHA-512/256 */
74 ctx->MBEDTLS_PRIVATE(state)[0] = UL64(0x22312194FC2BF72C);
75 ctx->MBEDTLS_PRIVATE(state)[1] = UL64(0x9F555FA3C84C64C2);
76 ctx->MBEDTLS_PRIVATE(state)[2] = UL64(0x2393B86B6F53B151);
77 ctx->MBEDTLS_PRIVATE(state)[3] = UL64(0x963877195940EABD);
78 ctx->MBEDTLS_PRIVATE(state)[4] = UL64(0x96283EE2A88EFFE3);
79 ctx->MBEDTLS_PRIVATE(state)[5] = UL64(0xBE5E1E2553863992);
80 ctx->MBEDTLS_PRIVATE(state)[6] = UL64(0x2B0199FC2C85B8AA);
81 ctx->MBEDTLS_PRIVATE(state)[7] = UL64(0x0EB72DDC81C52CA2);
82 }
83
84 ctx->MBEDTLS_PRIVATE(is384) = 0;
85 }
86
87
mbedtls_sha512_t_process(mbedtls_sha512_context * ctx,const unsigned char data[128])88 void mbedtls_sha512_t_process( mbedtls_sha512_context *ctx, const unsigned char data[128] )
89 {
90 if (ctx == NULL || data == NULL) {
91 CC_PalAbort("mbedtls_sha512_context and data buffer cannot be NULL");
92 }
93 mbedtls_internal_sha512_process(ctx, data);
94 }
95
96 /*
97 * SHA-512 process buffer
98 */
mbedtls_sha512_t_update(mbedtls_sha512_context * ctx,const unsigned char * input,size_t ilen)99 void mbedtls_sha512_t_update( mbedtls_sha512_context *ctx, const unsigned char *input,
100 size_t ilen )
101 {
102 if (ctx == NULL || input == NULL) {
103 CC_PalAbort("mbedtls_sha512_context and input buffer cannot be NULL");
104 }
105 mbedtls_sha512_update(ctx,input,ilen);
106 }
107
108 /*
109 * SHA-512 final digest
110 */
mbedtls_sha512_t_finish(mbedtls_sha512_context * ctx,unsigned char output[32],int is224)111 void mbedtls_sha512_t_finish( mbedtls_sha512_context *ctx, unsigned char output[32], int is224 )
112 {
113 unsigned char output512[64] = {0};
114
115 if (ctx == NULL || output == NULL) {
116 CC_PalAbort("mbedtls_sha512_context and output buffer cannot be NULL");
117 }
118 if (is224 != 0 && is224 != 1 ) {
119 CC_PalAbort("mbedtls_sha512_t_finish: is224 must be 0 or 1");
120 }
121
122 mbedtls_sha512_finish(ctx, output512);
123 if (is224) {
124 CC_PalMemCopy(output, output512, MBEDTLS_SHA512_T_224_DIGEST_SIZE_BYTES);
125 }
126 else {
127 CC_PalMemCopy(output, output512, MBEDTLS_SHA512_T_256_DIGEST_SIZE_BYTES);
128 }
129 }
130
131 #endif /* !MBEDTLS_SHA512_ALT */
132
133 /*
134 * output = SHA-512( input buffer )
135 */
mbedtls_sha512_t(const unsigned char * input,size_t ilen,unsigned char output[32],int is224)136 void mbedtls_sha512_t( const unsigned char *input, size_t ilen,
137 unsigned char output[32], int is224 )
138 {
139 mbedtls_sha512_context ctx;
140
141 if (input == NULL || output == NULL) {
142 CC_PalAbort("input and output buffers cannot be NULL");
143 }
144
145 mbedtls_sha512_t_init( &ctx );
146 mbedtls_sha512_t_starts( &ctx, is224 );
147 mbedtls_sha512_t_update( &ctx, input, ilen );
148 mbedtls_sha512_t_finish( &ctx, output, is224 );
149 mbedtls_sha512_t_free( &ctx );
150 }
151