Skip to content

Commit c4605e5

Browse files
committed
Integrate NUC472 SHA accelerator
1 parent 1eabed0 commit c4605e5

File tree

12 files changed

+1672
-0
lines changed

12 files changed

+1672
-0
lines changed

hal/targets/hal/TARGET_NUVOTON/TARGET_NUC472/crypto.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "nu_bitutil.h"
2222

2323
static int crypto_inited = 0;
24+
static int crypto_sha_hw_avail = 1;
2425

2526
void crypto_init(void)
2627
{
@@ -40,3 +41,22 @@ void crypto_zeroize(void *v, size_t n)
4041
*p++ = 0;
4142
}
4243
}
44+
45+
int crypto_sha_hw_acquire(void)
46+
{
47+
if (crypto_sha_hw_avail) {
48+
crypto_sha_hw_avail = 0;
49+
return 1;
50+
}
51+
else {
52+
return 0;
53+
}
54+
55+
}
56+
57+
void crypto_sha_hw_release(void)
58+
{
59+
if (! crypto_sha_hw_avail) {
60+
crypto_sha_hw_avail = 1;
61+
}
62+
}

hal/targets/hal/TARGET_NUVOTON/TARGET_NUC472/crypto.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ extern "C" {
2525

2626
void crypto_init(void);
2727
void crypto_zeroize(void *v, size_t n);
28+
int crypto_sha_hw_acquire(void);
29+
void crypto_sha_hw_release(void);
2830

2931
#ifdef __cplusplus
3032
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2015-2016 Nuvoton
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#if !defined(MBEDTLS_CONFIG_FILE)
18+
#include "mbedtls/config.h"
19+
#else
20+
#include MBEDTLS_CONFIG_FILE
21+
#endif
22+
23+
#if defined(MBEDTLS_SHA1_C)
24+
#if defined(MBEDTLS_SHA1_ALT)
25+
26+
#include "sha1_alt.h"
27+
#include "crypto.h"
28+
29+
void mbedtls_sha1_init(mbedtls_sha1_context *ctx)
30+
{
31+
if (crypto_sha_hw_acquire()) {
32+
ctx->mbedtls_sha1_init = mbedtls_sha1_hw_init;
33+
ctx->mbedtls_sha1_free = mbedtls_sha1_hw_free;
34+
ctx->mbedtls_sha1_clone = mbedtls_sha1_hw_clone;
35+
ctx->mbedtls_sha1_starts = mbedtls_sha1_hw_starts;
36+
ctx->mbedtls_sha1_update = mbedtls_sha1_hw_update;
37+
ctx->mbedtls_sha1_finish = mbedtls_sha1_hw_finish;
38+
ctx->mbedtls_sha1_process = mbedtls_sha1_hw_process;
39+
}
40+
else {
41+
ctx->mbedtls_sha1_init = mbedtls_sha1_sw_init;
42+
ctx->mbedtls_sha1_free = mbedtls_sha1_sw_free;
43+
ctx->mbedtls_sha1_clone = mbedtls_sha1_sw_clone;
44+
ctx->mbedtls_sha1_starts = mbedtls_sha1_sw_starts;
45+
ctx->mbedtls_sha1_update = mbedtls_sha1_sw_update;
46+
ctx->mbedtls_sha1_finish = mbedtls_sha1_sw_finish;
47+
ctx->mbedtls_sha1_process = mbedtls_sha1_sw_process;
48+
}
49+
50+
ctx->mbedtls_sha1_init(ctx);
51+
}
52+
53+
void mbedtls_sha1_free(mbedtls_sha1_context *ctx)
54+
{
55+
if (ctx == NULL) {
56+
return;
57+
}
58+
59+
ctx->mbedtls_sha1_free(ctx);
60+
61+
if (ctx->mbedtls_sha1_init == mbedtls_sha1_hw_init) {
62+
crypto_sha_hw_release();
63+
}
64+
}
65+
66+
void mbedtls_sha1_clone(mbedtls_sha1_context *dst,
67+
const mbedtls_sha1_context *src)
68+
{
69+
*dst = *src;
70+
}
71+
72+
/*
73+
* SHA-1 context setup
74+
*/
75+
void mbedtls_sha1_starts(mbedtls_sha1_context *ctx)
76+
{
77+
ctx->mbedtls_sha1_starts(ctx);
78+
79+
return;
80+
}
81+
82+
/*
83+
* SHA-1 process buffer
84+
*/
85+
void mbedtls_sha1_update(mbedtls_sha1_context *ctx, const unsigned char *input, size_t ilen)
86+
{
87+
ctx->mbedtls_sha1_update(ctx, input, ilen);
88+
}
89+
90+
/*
91+
* SHA-1 final digest
92+
*/
93+
void mbedtls_sha1_finish(mbedtls_sha1_context *ctx, unsigned char output[20])
94+
{
95+
ctx->mbedtls_sha1_finish(ctx, output);
96+
}
97+
98+
void mbedtls_sha1_process(mbedtls_sha1_context *ctx, const unsigned char data[64])
99+
{
100+
ctx->mbedtls_sha1_process(ctx, data);
101+
}
102+
103+
#endif /* MBEDTLS_SHA1_ALT */
104+
#endif /* MBEDTLS_SHA1_C */
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2015-2016 Nuvoton
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
#ifndef MBEDTLS_SHA1_ALT_H
17+
#define MBEDTLS_SHA1_ALT_H
18+
19+
#if !defined(MBEDTLS_CONFIG_FILE)
20+
#include "config.h"
21+
#else
22+
#include MBEDTLS_CONFIG_FILE
23+
#endif
24+
25+
#if defined(MBEDTLS_SHA1_C)
26+
#if defined(MBEDTLS_SHA1_ALT)
27+
28+
#include "sha1.h"
29+
#include "sha_alt_hw.h"
30+
#include "sha1_alt_sw.h"
31+
32+
#ifdef __cplusplus
33+
extern "C" {
34+
#endif
35+
36+
struct mbedtls_sha1_context_s;
37+
38+
/**
39+
* \brief SHA-1 context structure
40+
*/
41+
typedef struct mbedtls_sha1_context_s
42+
{
43+
union {
44+
crypto_sha_context hw_ctx;
45+
mbedtls_sha1_sw_context sw_ctx;
46+
};
47+
48+
void (*mbedtls_sha1_init)(struct mbedtls_sha1_context_s *ctx);
49+
void (*mbedtls_sha1_free)(struct mbedtls_sha1_context_s *ctx);
50+
void (*mbedtls_sha1_clone)(struct mbedtls_sha1_context_s *dst, const struct mbedtls_sha1_context_s *src);
51+
void (*mbedtls_sha1_starts)(struct mbedtls_sha1_context_s *ctx);
52+
void (*mbedtls_sha1_update)(struct mbedtls_sha1_context_s *ctx, const unsigned char *input, size_t ilen);
53+
void (*mbedtls_sha1_finish)(struct mbedtls_sha1_context_s *ctx, unsigned char output[20]);
54+
void (*mbedtls_sha1_process)(struct mbedtls_sha1_context_s *ctx, const unsigned char data[64]);
55+
}
56+
mbedtls_sha1_context;
57+
58+
/**
59+
* \brief Initialize SHA-1 context
60+
*
61+
* \param ctx SHA-1 context to be initialized
62+
*/
63+
void mbedtls_sha1_init( mbedtls_sha1_context *ctx );
64+
65+
/**
66+
* \brief Clear SHA-1 context
67+
*
68+
* \param ctx SHA-1 context to be cleared
69+
*/
70+
void mbedtls_sha1_free( mbedtls_sha1_context *ctx );
71+
72+
/**
73+
* \brief Clone (the state of) a SHA-1 context
74+
*
75+
* \param dst The destination context
76+
* \param src The context to be cloned
77+
*/
78+
void mbedtls_sha1_clone( mbedtls_sha1_context *dst,
79+
const mbedtls_sha1_context *src );
80+
81+
/**
82+
* \brief SHA-1 context setup
83+
*
84+
* \param ctx context to be initialized
85+
*/
86+
void mbedtls_sha1_starts( mbedtls_sha1_context *ctx );
87+
88+
/**
89+
* \brief SHA-1 process buffer
90+
*
91+
* \param ctx SHA-1 context
92+
* \param input buffer holding the data
93+
* \param ilen length of the input data
94+
*/
95+
void mbedtls_sha1_update( mbedtls_sha1_context *ctx, const unsigned char *input, size_t ilen );
96+
97+
/**
98+
* \brief SHA-1 final digest
99+
*
100+
* \param ctx SHA-1 context
101+
* \param output SHA-1 checksum result
102+
*/
103+
void mbedtls_sha1_finish( mbedtls_sha1_context *ctx, unsigned char output[20] );
104+
105+
/* Internal use */
106+
void mbedtls_sha1_process( mbedtls_sha1_context *ctx, const unsigned char data[64] );
107+
108+
#ifdef __cplusplus
109+
}
110+
#endif
111+
112+
#endif /* MBEDTLS_SHA1_ALT */
113+
#endif /* MBEDTLS_SHA1_C */
114+
115+
#endif /* sha1_alt.h */

0 commit comments

Comments
 (0)