Skip to content

Commit 19e2adf

Browse files
Ron EldorRon Eldor
authored andcommitted
HW Accelerated SHA1 and SHA256
Add Poritng for Sha1 and SHA256 over Cryptocell
1 parent fdf5587 commit 19e2adf

File tree

5 files changed

+442
-1
lines changed

5 files changed

+442
-1
lines changed

features/mbedtls/targets/TARGET_CRYPTOCELL310/mbedtls_device.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
#ifndef __MBEDTLS_DEVICE__
2222
#define __MBEDTLS_DEVICE__
2323

24-
#define MBEDTLS_AES_ALT
2524
#define MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT
25+
#define MBEDTLS_AES_ALT
2626
#define MBEDTLS_SHA1_ALT
2727
#define MBEDTLS_SHA256_ALT
2828
#define MBEDTLS_CCM_ALT
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* sha1_alt.c
3+
*
4+
* Copyright (C) 2018, ARM Limited, All Rights Reserved
5+
* SPDX-License-Identifier: Apache-2.0
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License"); you may
8+
* not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*
19+
*/
20+
21+
#include "mbedtls/sha1.h"
22+
#if defined(MBEDTLS_SHA1_ALT)
23+
#include <string.h>
24+
25+
void mbedtls_sha1_init( mbedtls_sha1_context *ctx )
26+
{
27+
memset( ctx, 0, sizeof( mbedtls_sha1_context ) );
28+
29+
}
30+
31+
void mbedtls_sha1_free( mbedtls_sha1_context *ctx )
32+
{
33+
if( ctx == NULL )
34+
return;
35+
36+
CRYS_HASH_Free( &ctx->crys_hash_ctx );
37+
38+
memset( ctx, 0, sizeof( mbedtls_sha1_context ) );
39+
}
40+
41+
void mbedtls_sha1_clone( mbedtls_sha1_context *dst,
42+
const mbedtls_sha1_context *src )
43+
{
44+
memcpy( dst, src, sizeof( mbedtls_sha1_context ) );
45+
}
46+
47+
int mbedtls_sha1_starts_ret( mbedtls_sha1_context *ctx )
48+
{
49+
if( CRYS_HASH_Init( &ctx->crys_hash_ctx, CRYS_HASH_SHA1_mode ) != CRYS_OK )
50+
return ( MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED );
51+
return ( 0 );
52+
}
53+
54+
55+
int mbedtls_sha1_update_ret( mbedtls_sha1_context *ctx,
56+
const unsigned char *input,
57+
size_t ilen )
58+
{
59+
if( CRYS_HASH_Update( &ctx->crys_hash_ctx, (uint8_t*)input, ilen ) != CRYS_OK )
60+
return ( MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED );
61+
return ( 0 );
62+
}
63+
64+
int mbedtls_sha1_finish_ret( mbedtls_sha1_context *ctx,
65+
unsigned char output[20] )
66+
{
67+
CRYSError_t CrysErr = CRYS_OK;
68+
CRYS_HASH_Result_t crys_result = {0};
69+
CrysErr = CRYS_HASH_Finish( &ctx->crys_hash_ctx, crys_result );
70+
if( CrysErr == CRYS_OK )
71+
{
72+
memcpy( output, crys_result, 20 );
73+
return ( 0 );
74+
}
75+
else
76+
return ( MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED );
77+
}
78+
79+
int mbedtls_internal_sha1_process( mbedtls_sha1_context *ctx,
80+
const unsigned char data[64] )
81+
{
82+
if( CRYS_HASH_Update( &ctx->crys_hash_ctx, (uint8_t*)data, 64 ) != CRYS_OK )
83+
return ( MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED );
84+
return ( 0 );
85+
}
86+
#endif //MBEDTLS_SHA1_ALT
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
/*
2+
* sha1_alt.h
3+
*
4+
* Copyright (C) 2018, ARM Limited, All Rights Reserved
5+
* SPDX-License-Identifier: Apache-2.0
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License"); you may
8+
* not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*
19+
*/
20+
21+
#ifndef __SHA1_ALT__
22+
#define __SHA1_ALT__
23+
#if defined(MBEDTLS_SHA1_ALT)
24+
#include "crys_hash.h"
25+
#ifdef __cplusplus
26+
extern "C" {
27+
#endif
28+
29+
30+
/**
31+
* \brief SHA-1 context structure
32+
*/
33+
typedef struct
34+
{
35+
CRYS_HASHUserContext_t crys_hash_ctx;
36+
} mbedtls_sha1_context;
37+
38+
/**
39+
* \brief This function initializes a SHA-1 context.
40+
*
41+
* \param ctx The SHA-1 context to initialize.
42+
*
43+
* \warning SHA-1 is considered a weak message digest and its use
44+
* constitutes a security risk. We recommend considering
45+
* stronger message digests instead.
46+
*
47+
*/
48+
void mbedtls_sha1_init( mbedtls_sha1_context *ctx );
49+
50+
/**
51+
* \brief This function clears a SHA-1 context.
52+
*
53+
* \param ctx The SHA-1 context to clear.
54+
*
55+
* \warning SHA-1 is considered a weak message digest and its use
56+
* constitutes a security risk. We recommend considering
57+
* stronger message digests instead.
58+
*
59+
*/
60+
void mbedtls_sha1_free( mbedtls_sha1_context *ctx );
61+
62+
/**
63+
* \brief This function clones the state of a SHA-1 context.
64+
*
65+
* \param dst The destination context.
66+
* \param src The context to clone.
67+
*
68+
* \warning SHA-1 is considered a weak message digest and its use
69+
* constitutes a security risk. We recommend considering
70+
* stronger message digests instead.
71+
*
72+
*/
73+
void mbedtls_sha1_clone( mbedtls_sha1_context *dst,
74+
const mbedtls_sha1_context *src );
75+
76+
/**
77+
* \brief This function starts a SHA-1 checksum calculation.
78+
*
79+
* \param ctx The context to initialize.
80+
*
81+
* \return \c 0 if successful
82+
*
83+
* \warning SHA-1 is considered a weak message digest and its use
84+
* constitutes a security risk. We recommend considering
85+
* stronger message digests instead.
86+
*
87+
*/
88+
int mbedtls_sha1_starts_ret( mbedtls_sha1_context *ctx );
89+
90+
/**
91+
* \brief This function feeds an input buffer into an ongoing SHA-1
92+
* checksum calculation.
93+
*
94+
* \param ctx The SHA-1 context.
95+
* \param input The buffer holding the input data.
96+
* \param ilen The length of the input data.
97+
*
98+
* \return \c 0 if successful
99+
*
100+
* \warning SHA-1 is considered a weak message digest and its use
101+
* constitutes a security risk. We recommend considering
102+
* stronger message digests instead.
103+
*
104+
*/
105+
int mbedtls_sha1_update_ret( mbedtls_sha1_context *ctx,
106+
const unsigned char *input,
107+
size_t ilen );
108+
109+
/**
110+
* \brief This function finishes the SHA-1 operation, and writes
111+
* the result to the output buffer.
112+
*
113+
* \param ctx The SHA-1 context.
114+
* \param output The SHA-1 checksum result.
115+
*
116+
* \return \c 0 if successful
117+
*
118+
* \warning SHA-1 is considered a weak message digest and its use
119+
* constitutes a security risk. We recommend considering
120+
* stronger message digests instead.
121+
*
122+
*/
123+
int mbedtls_sha1_finish_ret( mbedtls_sha1_context *ctx,
124+
unsigned char output[20] );
125+
126+
/**
127+
* \brief SHA-1 process data block (internal use only)
128+
*
129+
* \param ctx SHA-1 context
130+
* \param data The data block being processed.
131+
*
132+
* \return \c 0 if successful
133+
*
134+
* \warning SHA-1 is considered a weak message digest and its use
135+
* constitutes a security risk. We recommend considering
136+
* stronger message digests instead.
137+
*
138+
*/
139+
int mbedtls_internal_sha1_process( mbedtls_sha1_context *ctx,
140+
const unsigned char data[64] );
141+
142+
#ifdef __cplusplus
143+
}
144+
#endif
145+
146+
#endif //MBEDTLS_SHA1_ALT
147+
#endif //__SHA1_ALT__
148+
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* sha256_alt.c
3+
*
4+
* Copyright (C) 2018, ARM Limited, All Rights Reserved
5+
* SPDX-License-Identifier: Apache-2.0
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License"); you may
8+
* not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*
19+
*/
20+
21+
#include "mbedtls/sha256.h"
22+
#if defined(MBEDTLS_SHA256_ALT)
23+
#include <string.h>
24+
25+
void mbedtls_sha256_init( mbedtls_sha256_context *ctx )
26+
{
27+
memset( ctx, 0, sizeof( mbedtls_sha256_context ) );
28+
29+
}
30+
31+
void mbedtls_sha256_free( mbedtls_sha256_context *ctx )
32+
{
33+
if( ctx == NULL )
34+
return;
35+
CRYS_HASH_Free( &ctx->crys_hash_ctx );
36+
memset( ctx, 0, sizeof( mbedtls_sha256_context ) );
37+
}
38+
39+
void mbedtls_sha256_clone( mbedtls_sha256_context *dst,
40+
const mbedtls_sha256_context *src )
41+
{
42+
memcpy( dst, src, sizeof( mbedtls_sha256_context ) );
43+
}
44+
45+
46+
int mbedtls_sha256_starts_ret( mbedtls_sha256_context *ctx, int is224 )
47+
{
48+
if(CRYS_HASH_Init( &ctx->crys_hash_ctx, is224 ?
49+
CRYS_HASH_SHA224_mode : CRYS_HASH_SHA256_mode ) != CRYS_OK )
50+
return ( MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED );
51+
return ( 0 );
52+
}
53+
54+
int mbedtls_internal_sha256_process( mbedtls_sha256_context *ctx,
55+
const unsigned char data[64] )
56+
{
57+
if( CRYS_HASH_Update( &ctx->crys_hash_ctx, (uint8_t*)data, 64 ) != CRYS_OK )
58+
return ( MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED );
59+
return ( 0 );
60+
}
61+
62+
int mbedtls_sha256_update_ret( mbedtls_sha256_context *ctx,
63+
const unsigned char *input,
64+
size_t ilen )
65+
{
66+
if( CRYS_HASH_Update( &ctx->crys_hash_ctx, (uint8_t*)input, ilen ) != CRYS_OK )
67+
return ( MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED );
68+
return ( 0 );
69+
}
70+
71+
int mbedtls_sha256_finish_ret( mbedtls_sha256_context *ctx,
72+
unsigned char output[32] )
73+
{
74+
CRYSError_t CrysErr = CRYS_OK;
75+
CRYS_HASH_Result_t crys_result = {0};
76+
CrysErr = CRYS_HASH_Finish( &ctx->crys_hash_ctx, crys_result );
77+
if( CrysErr == CRYS_OK )
78+
{
79+
memcpy( output, crys_result, 32 );
80+
return ( 0 );
81+
}
82+
else
83+
return ( MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED );
84+
}
85+
#endif //MBEDTLS_SHA256_ALT
86+

0 commit comments

Comments
 (0)