Skip to content

Commit 0867d33

Browse files
shuzhuofanghuaqi
authored andcommitted
mbedtls: add riscv k (scalar k and vector k) opt
1 parent 7d57a13 commit 0867d33

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+11957
-5
lines changed

accelerator/scalar_k/aes_alt.c

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
/*
2+
* Copyright (c) 2019 Nuclei Limited. All rights reserved.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
#include "common.h"
20+
21+
#if defined(MBEDTLS_AES_C)
22+
23+
#include "api_aes.h"
24+
#include <string.h>
25+
26+
// #define MBEDTLS_DEBUG
27+
28+
#include "mbedtls/aes.h"
29+
#include "mbedtls/platform.h"
30+
#include "mbedtls/platform_util.h"
31+
#include "mbedtls/error.h"
32+
#if defined(MBEDTLS_PADLOCK_C)
33+
#include "padlock.h"
34+
#endif
35+
#if defined(MBEDTLS_AESNI_C)
36+
#include "aesni.h"
37+
#endif
38+
39+
#if defined(MBEDTLS_SELF_TEST)
40+
#if defined(MBEDTLS_PLATFORM_C)
41+
#include "mbedtls/platform.h"
42+
#else
43+
#include <stdio.h>
44+
#define mbedtls_printf printf
45+
#endif /* MBEDTLS_PLATFORM_C */
46+
#endif /* MBEDTLS_SELF_TEST */
47+
48+
#include <nmsis_bench.h>
49+
BENCH_DECLARE_VAR();
50+
51+
//#define printf
52+
/*
53+
* AES key schedule (encryption)
54+
*/
55+
#if defined(MBEDTLS_AES_SETKEY_ENC_ALT)
56+
int mbedtls_aes_setkey_enc( mbedtls_aes_context *ctx, const unsigned char *key,
57+
unsigned int keybits )
58+
{
59+
uint32_t *RK;
60+
61+
ctx->rk_offset = 0;
62+
63+
RK = ctx->buf + ctx->rk_offset;
64+
// BENCH_START(mbedtls_aes_setkey_enc);
65+
switch( keybits )
66+
{
67+
case 128:
68+
ctx->nr = 10;
69+
aes_128_enc_key_schedule(RK, (unsigned char *)key);
70+
break;
71+
case 192:
72+
ctx->nr = 12;
73+
aes_192_enc_key_schedule(RK, (unsigned char *)key);
74+
break;
75+
case 256:
76+
ctx->nr = 14;
77+
aes_256_enc_key_schedule(RK, (unsigned char *)key);
78+
break;
79+
default : return( MBEDTLS_ERR_AES_INVALID_KEY_LENGTH );
80+
}
81+
// BENCH_END(mbedtls_aes_setkey_enc);
82+
return 0;
83+
84+
}
85+
#endif /* MBEDTLS_AES_SETKEY_ENC_ALT */
86+
87+
/*
88+
* AES key schedule (decryption)
89+
*/
90+
#if defined(MBEDTLS_AES_SETKEY_DEC_ALT)
91+
int mbedtls_aes_setkey_dec( mbedtls_aes_context *ctx, const unsigned char *key,
92+
unsigned int keybits )
93+
{
94+
uint32_t *RK;
95+
96+
ctx->rk_offset = 0;
97+
RK = ctx->buf + ctx->rk_offset;
98+
// BENCH_START(mbedtls_aes_setkey_dec);
99+
switch( keybits )
100+
{
101+
case 128:
102+
ctx->nr = 10;
103+
aes_128_dec_key_schedule(RK, (unsigned char *)key);
104+
break;
105+
case 192:
106+
ctx->nr = 12;
107+
aes_192_dec_key_schedule(RK, (unsigned char *)key);
108+
break;
109+
case 256:
110+
ctx->nr = 14;
111+
aes_256_dec_key_schedule(RK, (unsigned char *)key);
112+
break;
113+
default : return( MBEDTLS_ERR_AES_INVALID_KEY_LENGTH );
114+
}
115+
// BENCH_END(mbedtls_aes_setkey_dec);
116+
return 0;
117+
118+
}
119+
#endif /* MBEDTLS_AES_SETKEY_DEC_ALT */
120+
/*
121+
* AES-ECB block encryption
122+
*/
123+
#if defined(MBEDTLS_AES_ENCRYPT_ALT)
124+
int mbedtls_internal_aes_encrypt( mbedtls_aes_context *ctx,
125+
const unsigned char input[16],
126+
unsigned char output[16] )
127+
{
128+
uint32_t *RK = ctx->buf + ctx->rk_offset;
129+
//BENCH_START(mbedtls_internal_aes_encrypt);
130+
switch( ctx->nr )
131+
{
132+
case 10:
133+
aes_128_ecb_encrypt(output, (unsigned char *)input, RK);
134+
break;
135+
case 12:
136+
aes_192_ecb_encrypt(output, (unsigned char *)input, RK);
137+
break;
138+
case 14:
139+
aes_256_ecb_encrypt(output, (unsigned char *)input, RK);
140+
break;
141+
default : return( MBEDTLS_ERR_AES_INVALID_KEY_LENGTH );
142+
}
143+
//BENCH_END(mbedtls_internal_aes_encrypt);
144+
145+
return( 0 );
146+
}
147+
#endif /* MBEDTLS_AES_ENCRYPT_ALT */
148+
149+
/*
150+
* AES-ECB block decryption
151+
*/
152+
#if defined(MBEDTLS_AES_DECRYPT_ALT)
153+
154+
int mbedtls_internal_aes_decrypt( mbedtls_aes_context *ctx,
155+
const unsigned char input[16],
156+
unsigned char output[16] )
157+
{
158+
uint32_t *RK = ctx->buf + ctx->rk_offset;
159+
// BENCH_START(mbedtls_internal_aes_decrypt);
160+
switch( ctx->nr )
161+
{
162+
case 10:
163+
aes_128_ecb_decrypt(output, (unsigned char *)input, RK);
164+
break;
165+
case 12:
166+
aes_192_ecb_decrypt(output, (unsigned char *)input, RK);
167+
break;
168+
case 14:
169+
aes_256_ecb_decrypt(output, (unsigned char *)input, RK);
170+
break;
171+
default : return( MBEDTLS_ERR_AES_INVALID_KEY_LENGTH );
172+
}
173+
// BENCH_END(mbedtls_internal_aes_decrypt);
174+
return( 0 );
175+
176+
}
177+
#endif /* MBEDTLS_AES_DECRYPT_ALT */
178+
179+
#endif /* MBEDTLS_AES_C */

accelerator/scalar_k/api_aes.h

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
2+
#include <stdint.h>
3+
4+
/*!
5+
@defgroup crypto_block_aes Crypto Block AES
6+
@{
7+
8+
AES | Nk | Nb | Nr
9+
-----|-------|------|---------------
10+
128 | 4 | 4 | 10
11+
192 | 6 | 4 | 12
12+
256 | 8 | 4 | 14
13+
14+
*/
15+
16+
#ifndef __API_AES_H__
17+
#define __API_AES_H__
18+
19+
//! Number of bytes in a single AES block
20+
#define AES_BLOCK_BYTES 16
21+
22+
//! Block size in 4-byte words for AES 128
23+
#define AES_128_NB 4
24+
#define AES_192_NB 4
25+
#define AES_256_NB 4
26+
27+
//! Words in AES 128 cipher key
28+
#define AES_128_NK 4
29+
#define AES_192_NK 6
30+
#define AES_256_NK 8
31+
32+
//! Number of rounds for AES 128
33+
#define AES_128_NR 10
34+
#define AES_192_NR 12
35+
#define AES_256_NR 14
36+
37+
//! Bytes in an AES 128 Cipher key
38+
#define AES_128_KEY_BYTES (4*AES_128_NK)
39+
#define AES_192_KEY_BYTES (4*AES_192_NK)
40+
#define AES_256_KEY_BYTES (4*AES_256_NK)
41+
42+
#define AES_128_RK_WORDS 44
43+
#define AES_192_RK_WORDS 52
44+
#define AES_256_RK_WORDS 60
45+
46+
//! Number of bytes in the expanded AES 128 key
47+
#define AES_128_RK_BYTES (4*AES_128_RK_WORDS)
48+
#define AES_192_RK_BYTES (4*AES_192_RK_WORDS)
49+
#define AES_256_RK_BYTES (4*AES_256_RK_WORDS)
50+
51+
52+
/*!
53+
@brief Key expansion function for the AES 128 parameterisation - encrypt
54+
@param [out] rk - The expanded key schedule
55+
@param [in] ck - The cipher key to expand
56+
*/
57+
void aes_128_enc_key_schedule (
58+
uint32_t * const rk,
59+
uint8_t * const ck
60+
);
61+
62+
/*!
63+
@brief Key expansion function for the AES 192 parameterisation - encrypt
64+
@param [out] rk - The expanded key schedule
65+
@param [in] ck - The cipher key to expand
66+
*/
67+
void aes_192_enc_key_schedule (
68+
uint32_t * const rk,
69+
uint8_t * const ck
70+
);
71+
72+
/*!
73+
@brief Key expansion function for the AES 256 parameterisation - encrypt
74+
@param [out] rk - The expanded key schedule
75+
@param [in] ck - The cipher key to expand
76+
*/
77+
void aes_256_enc_key_schedule (
78+
uint32_t * const rk,
79+
uint8_t * const ck
80+
);
81+
82+
83+
/*!
84+
@brief Key expansion function for the AES 128 parameterisation - decrypt
85+
@param [out] rk - The expanded key schedule
86+
@param [in] ck - The cipher key to expand
87+
*/
88+
void aes_128_dec_key_schedule (
89+
uint32_t * const rk,
90+
uint8_t * const ck
91+
);
92+
93+
94+
/*!
95+
@brief Key expansion function for the AES 192 parameterisation - decrypt
96+
@param [out] rk - The expanded key schedule
97+
@param [in] ck - The cipher key to expand
98+
*/
99+
void aes_192_dec_key_schedule (
100+
uint32_t * const rk,
101+
uint8_t * const ck
102+
);
103+
104+
/*!
105+
@brief Key expansion function for the AES 256 parameterisation - decrypt
106+
@param [out] rk - The expanded key schedule
107+
@param [in] ck - The cipher key to expand
108+
*/
109+
void aes_256_dec_key_schedule (
110+
uint32_t * const rk,
111+
uint8_t * const ck
112+
);
113+
114+
115+
/*!
116+
@brief single-block AES 128 encrypt function
117+
@param [out] ct - Output cipher text
118+
@param [in] pt - Input plaintext
119+
@param [in] rk - The expanded key schedule
120+
@param [in] nr - Number of encryption rounds to perform.
121+
*/
122+
void aes_128_ecb_encrypt (
123+
uint8_t ct [AES_BLOCK_BYTES],
124+
uint8_t pt [AES_BLOCK_BYTES],
125+
uint32_t * rk
126+
);
127+
128+
/*!
129+
@brief single-block AES 192 encrypt function
130+
@param [out] ct - Output cipher text
131+
@param [in] pt - Input plaintext
132+
@param [in] rk - The expanded key schedule
133+
@param [in] nr - Number of encryption rounds to perform.
134+
*/
135+
void aes_192_ecb_encrypt (
136+
uint8_t ct [AES_BLOCK_BYTES],
137+
uint8_t pt [AES_BLOCK_BYTES],
138+
uint32_t * rk
139+
);
140+
141+
/*!
142+
@brief single-block AES 256 encrypt function
143+
@param [out] ct - Output cipher text
144+
@param [in] pt - Input plaintext
145+
@param [in] rk - The expanded key schedule
146+
@param [in] nr - Number of encryption rounds to perform.
147+
*/
148+
void aes_256_ecb_encrypt (
149+
uint8_t ct [AES_BLOCK_BYTES],
150+
uint8_t pt [AES_BLOCK_BYTES],
151+
uint32_t * rk
152+
);
153+
154+
/*!
155+
@brief single-block AES 128 decrypt function
156+
@param [out] pt - Output plaintext
157+
@param [in] ct - Input cipher text
158+
@param [in] rk - The expanded key schedule
159+
@param [in] nr - Number of decryption rounds to perform.
160+
*/
161+
void aes_128_ecb_decrypt (
162+
uint8_t pt [AES_BLOCK_BYTES],
163+
uint8_t ct [AES_BLOCK_BYTES],
164+
uint32_t * rk
165+
);
166+
167+
/*!
168+
@brief single-block AES 192 decrypt function
169+
@param [out] pt - Output plaintext
170+
@param [in] ct - Input cipher text
171+
@param [in] rk - The expanded key schedule
172+
@param [in] nr - Number of decryption rounds to perform.
173+
*/
174+
void aes_192_ecb_decrypt (
175+
uint8_t pt [AES_BLOCK_BYTES],
176+
uint8_t ct [AES_BLOCK_BYTES],
177+
uint32_t * rk
178+
);
179+
180+
181+
/*!
182+
@brief single-block AES 256 decrypt function
183+
@param [out] pt - Output plaintext
184+
@param [in] ct - Input cipher text
185+
@param [in] rk - The expanded key schedule
186+
@param [in] nr - Number of decryption rounds to perform.
187+
*/
188+
void aes_256_ecb_decrypt (
189+
uint8_t pt [AES_BLOCK_BYTES],
190+
uint8_t ct [AES_BLOCK_BYTES],
191+
uint32_t * rk
192+
);
193+
194+
#endif
195+
196+
//! @}

0 commit comments

Comments
 (0)