1 /*
2  * Copyright (c) 2001-2019, Arm Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include "sha1_alt.h"
8 #include "hash_driver.h"
9 #include "cc_pal_mem.h"
10 #include "cc_pal_log.h"
11 #include "cc_pal_abort.h"
12 #include "mbedtls_hash_common.h"
13 #include <stdio.h>
14 
15 #if defined(MBEDTLS_SHA1_ALT) && defined(MBEDTLS_SHA1_C)
16 
17 /* Internal function*/
18 
mbedtls_sha1_init(mbedtls_sha1_context * ctx)19 void mbedtls_sha1_init( mbedtls_sha1_context *ctx )
20 {
21     if( NULL == ctx  )
22     {
23         CC_PalAbort("\nctx is NULL\n");
24     }
25     CC_PalMemSetZero(ctx, sizeof( mbedtls_sha1_context ) );
26 }
27 
mbedtls_sha1_free(mbedtls_sha1_context * ctx)28 void mbedtls_sha1_free( mbedtls_sha1_context *ctx )
29 {
30     if( NULL == ctx  )
31     {
32         CC_PAL_LOG_ERR("\nctx is NULL\n");
33         return;
34     }
35     mbedtls_zeroize_internal( ctx, sizeof( mbedtls_sha1_context ) );
36 }
37 
mbedtls_sha1_clone(mbedtls_sha1_context * dst,const mbedtls_sha1_context * src)38 void mbedtls_sha1_clone( mbedtls_sha1_context *dst,
39                          const mbedtls_sha1_context *src )
40 {
41     if( NULL == src || NULL == dst )
42     {
43         CC_PalAbort("src or dst are NULL\n" );
44     }
45 
46     *dst = *src;
47 }
48 
mbedtls_sha1_starts_ret(mbedtls_sha1_context * ctx)49 int mbedtls_sha1_starts_ret( mbedtls_sha1_context *ctx )
50 {
51     int ret;
52 
53     ret = mbedtls_sha_starts_internal( ctx, HASH_SHA1);
54     if( ret != 0 )
55     {
56         return( MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED );
57     }
58 
59     return( ret );
60 }
61 
mbedtls_internal_sha1_process(mbedtls_sha1_context * ctx,const unsigned char data[64])62 int mbedtls_internal_sha1_process( mbedtls_sha1_context *ctx, const unsigned char data[64] )
63 {
64     int ret;
65 
66     ret = mbedtls_sha_process_internal( ctx, data );
67     if( ret != 0 )
68     {
69         return( MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED );
70     }
71 
72     return( ret );
73 }
74 
75 
76 /*
77  * SHA-1 process buffer
78  */
mbedtls_sha1_update_ret(mbedtls_sha1_context * ctx,const unsigned char * input,size_t ilen)79 int mbedtls_sha1_update_ret( mbedtls_sha1_context *ctx, const unsigned char *input, size_t ilen )
80 {
81     int ret;
82 
83     ret = mbedtls_sha_update_internal( ctx, input, ilen );
84     if( ret != 0 )
85     {
86         return( MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED );
87     }
88 
89     return( ret );
90 }
91 
92 
93 /*
94  * SHA-1 final digest
95  */
mbedtls_sha1_finish_ret(mbedtls_sha1_context * ctx,unsigned char output[20])96 int mbedtls_sha1_finish_ret( mbedtls_sha1_context *ctx, unsigned char output[20] )
97 {
98     int ret;
99 
100     HashContext_t *pHashCtx = NULL;
101     if (NULL == ctx || NULL == output ){
102         CC_PAL_LOG_ERR( "ctx or output buffer are NULL\n" );
103         return( MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED );
104     }
105     pHashCtx = (HashContext_t *)ctx;
106 
107     ret = mbedtls_sha_finish_internal( ctx );
108     if( ret != 0)
109     {
110         return( MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED );
111     }
112 
113     CC_PalMemCopy(output, pHashCtx->digest, SHA1_DIGEST_SIZE_IN_BYTES);
114 
115     return( ret );
116 }
117 #endif /* #if defined(MBEDTLS_SHA1_ALT) && defined(MBEDTLS_SHA1_C) */
118 
119