1 /**
2  * \file md_wrap.c
3  *
4  * \brief Generic message digest wrapper for mbed TLS
5  *
6  * \author Adriaan de Jong <dejong@fox-it.com>
7  *
8  *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
9  *  Copyright (C) 2018-2022, Intel Corporation.
10  *  SPDX-License-Identifier: Apache-2.0
11  *
12  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
13  *  not use this file except in compliance with the License.
14  *  You may obtain a copy of the License at
15  *
16  *  http://www.apache.org/licenses/LICENSE-2.0
17  *
18  *  Unless required by applicable law or agreed to in writing, software
19  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
20  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  *  See the License for the specific language governing permissions and
22  *  limitations under the License.
23  *
24  *  This file is part of mbed TLS (https://tls.mbed.org)
25  */
26 
27 #include "md_internal.h"
28 #include "sha256.h"
29 
30 /*
31  * Wrappers for generic message digests
32  */
33 
sha256_update_wrap(void * ctx,const uint8_t * input,size_t ilen)34 static int32_t sha256_update_wrap( void *ctx, const uint8_t *input,
35                                 size_t ilen )
36 {
37     return( mbedtls_sha256_update_ret( (mbedtls_sha256_context *) ctx,
38                                        input, ilen ) );
39 }
40 
sha256_finish_wrap(void * ctx,uint8_t * output)41 static int32_t sha256_finish_wrap( void *ctx, uint8_t *output )
42 {
43     return( mbedtls_sha256_finish_ret( (mbedtls_sha256_context *) ctx,
44                                        output ) );
45 }
46 
sha256_clone_wrap(void * dst,const void * src)47 static void sha256_clone_wrap( void *dst, const void *src )
48 {
49     mbedtls_sha256_clone( (mbedtls_sha256_context *) dst,
50                     (const mbedtls_sha256_context *) src );
51 }
52 
sha256_process_wrap(void * ctx,const uint8_t * data)53 static int32_t sha256_process_wrap( void *ctx, const uint8_t *data )
54 {
55     return( mbedtls_internal_sha256_process( (mbedtls_sha256_context *) ctx,
56                                              data ) );
57 }
58 
sha256_starts_wrap(void * ctx)59 static int32_t sha256_starts_wrap( void *ctx )
60 {
61     return( mbedtls_sha256_starts_ret( (mbedtls_sha256_context *) ctx, 0 ) );
62 }
63 
sha256_wrap(const uint8_t * input,size_t ilen,uint8_t * output)64 static int32_t sha256_wrap( const uint8_t *input, size_t ilen,
65                         uint8_t *output )
66 {
67     return( mbedtls_sha256_ret( input, ilen, output, 0 ) );
68 }
69 
70 const mbedtls_md_info_t mbedtls_sha256_info = {
71     MBEDTLS_MD_SHA256,
72     "SHA256",
73     32,
74     64U,
75     sha256_starts_wrap,
76     sha256_update_wrap,
77     sha256_finish_wrap,
78     sha256_wrap,
79     sha256_clone_wrap,
80     sha256_process_wrap,
81 };
82