Knight KE / Mbed OS Game_Master
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers sha256_alt.c Source File

sha256_alt.c

00001 /*
00002  *  sha256_alt.c
00003  *
00004  *  Copyright (C) 2018, Arm Limited, All Rights Reserved
00005  *  SPDX-License-Identifier: Apache-2.0
00006  *
00007  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
00008  *  not use this file except in compliance with the License.
00009  *  You may obtain a copy of the License at
00010  *
00011  *  http://www.apache.org/licenses/LICENSE-2.0
00012  *
00013  *  Unless required by applicable law or agreed to in writing, software
00014  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00015  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00016  *  See the License for the specific language governing permissions and
00017  *  limitations under the License.
00018  *
00019  */
00020 
00021 #include "mbedtls/sha256.h"
00022 #if defined(MBEDTLS_SHA256_ALT)
00023 #include <string.h>
00024 
00025 void mbedtls_sha256_init( mbedtls_sha256_context *ctx )
00026 {
00027     memset( ctx, 0, sizeof( mbedtls_sha256_context ) );
00028 
00029 }
00030 
00031 void mbedtls_sha256_free( mbedtls_sha256_context *ctx )
00032 {
00033     if( ctx == NULL )
00034         return;
00035     CRYS_HASH_Free( &ctx->crys_hash_ctx );
00036     memset( ctx, 0, sizeof( mbedtls_sha256_context ) );
00037 }
00038 
00039 void mbedtls_sha256_clone( mbedtls_sha256_context *dst,
00040                            const mbedtls_sha256_context *src )
00041 {
00042     memcpy( dst, src, sizeof( mbedtls_sha256_context ) );
00043 }
00044 
00045 
00046 int mbedtls_sha256_starts_ret( mbedtls_sha256_context *ctx, int is224 )
00047 {
00048     if(CRYS_HASH_Init( &ctx->crys_hash_ctx, is224 ?
00049                     CRYS_HASH_SHA224_mode  : CRYS_HASH_SHA256_mode  ) != CRYS_OK )
00050         return ( MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED );
00051     return ( 0 );
00052 }
00053 
00054 int mbedtls_internal_sha256_process( mbedtls_sha256_context *ctx,
00055                                      const unsigned char data[64] )
00056 {
00057     if( CRYS_HASH_Update( &ctx->crys_hash_ctx, (uint8_t*)data, 64 ) != CRYS_OK )
00058         return ( MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED );
00059     return ( 0 );
00060 }
00061 
00062 int mbedtls_sha256_update_ret( mbedtls_sha256_context *ctx,
00063                                const unsigned char *input,
00064                                size_t ilen )
00065 {
00066     if( CRYS_HASH_Update( &ctx->crys_hash_ctx, (uint8_t*)input, ilen ) != CRYS_OK )
00067         return ( MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED );
00068     return ( 0 );
00069 }
00070 
00071 int mbedtls_sha256_finish_ret( mbedtls_sha256_context *ctx,
00072                                unsigned char output[32] )
00073 {
00074     CRYSError_t  CrysErr = CRYS_OK;
00075     CRYS_HASH_Result_t  crys_result = {0};
00076     CrysErr = CRYS_HASH_Finish( &ctx->crys_hash_ctx, crys_result );
00077     if( CrysErr == CRYS_OK )
00078     {
00079         memcpy( output, crys_result, 32 );
00080         return ( 0 );
00081     }
00082     else
00083         return ( MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED );
00084 }
00085 #endif //MBEDTLS_SHA256_ALT
00086