• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * This file is part of the openHiTLS project.
3  *
4  * openHiTLS is licensed under the Mulan PSL v2.
5  * You can use this software according to the terms and conditions of the Mulan PSL v2.
6  * You may obtain a copy of Mulan PSL v2 at:
7  *
8  *     http://license.coscl.org.cn/MulanPSL2
9  *
10  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11  * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12  * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13  * See the Mulan PSL v2 for more details.
14  */
15 
16 #include "hitls_build.h"
17 #ifdef HITLS_CRYPTO_AES
18 
19 #include "securec.h"
20 #include "bsl_err_internal.h"
21 #include "crypt_utils.h"
22 #include "crypt_errno.h"
23 #include "bsl_sal.h"
24 #ifdef HITLS_CRYPTO_AES_PRECALC_TABLES
25 #include "crypt_aes_tbox.h"
26 #else
27 #include "crypt_aes_sbox.h"
28 #endif
29 #include "crypt_aes.h"
30 
SetEncryptKey128(CRYPT_AES_Key * ctx,const uint8_t * key)31 void SetEncryptKey128(CRYPT_AES_Key *ctx, const uint8_t *key)
32 {
33     ctx->rounds = 10;  // 10 rounds
34 #ifdef HITLS_CRYPTO_AES_PRECALC_TABLES
35     SetAesKeyExpansionTbox(ctx, CRYPT_AES_128, key, true);
36 #else
37     SetAesKeyExpansionSbox(ctx, CRYPT_AES_128, key);
38 #endif
39 }
40 
SetEncryptKey192(CRYPT_AES_Key * ctx,const uint8_t * key)41 void SetEncryptKey192(CRYPT_AES_Key *ctx, const uint8_t *key)
42 {
43     ctx->rounds = 12;  // 12 rounds
44 #ifdef HITLS_CRYPTO_AES_PRECALC_TABLES
45     SetAesKeyExpansionTbox(ctx, CRYPT_AES_192, key, true);
46 #else
47     SetAesKeyExpansionSbox(ctx, CRYPT_AES_192, key);
48 #endif
49 }
50 
SetEncryptKey256(CRYPT_AES_Key * ctx,const uint8_t * key)51 void SetEncryptKey256(CRYPT_AES_Key *ctx, const uint8_t *key)
52 {
53     ctx->rounds = 14;  // 14 rounds
54 #ifdef HITLS_CRYPTO_AES_PRECALC_TABLES
55     SetAesKeyExpansionTbox(ctx, CRYPT_AES_256, key, true);
56 #else
57     SetAesKeyExpansionSbox(ctx, CRYPT_AES_256, key);
58 #endif
59 }
60 
SetDecryptKey128(CRYPT_AES_Key * ctx,const uint8_t * key)61 void SetDecryptKey128(CRYPT_AES_Key *ctx, const uint8_t *key)
62 {
63     ctx->rounds = 10;  // 10 rounds
64 #ifdef HITLS_CRYPTO_AES_PRECALC_TABLES
65     SetAesKeyExpansionTbox(ctx, CRYPT_AES_128, key, false);
66 #else
67     SetAesKeyExpansionSbox(ctx, CRYPT_AES_128, key);
68 #endif
69 }
70 
SetDecryptKey192(CRYPT_AES_Key * ctx,const uint8_t * key)71 void SetDecryptKey192(CRYPT_AES_Key *ctx, const uint8_t *key)
72 {
73     ctx->rounds = 12;  // 12 rounds
74 #ifdef HITLS_CRYPTO_AES_PRECALC_TABLES
75     SetAesKeyExpansionTbox(ctx, CRYPT_AES_192, key, false);
76 #else
77     SetAesKeyExpansionSbox(ctx, CRYPT_AES_192, key);
78 #endif
79 }
80 
SetDecryptKey256(CRYPT_AES_Key * ctx,const uint8_t * key)81 void SetDecryptKey256(CRYPT_AES_Key *ctx, const uint8_t *key)
82 {
83     ctx->rounds = 14;  // 14 rounds
84 #ifdef HITLS_CRYPTO_AES_PRECALC_TABLES
85     SetAesKeyExpansionTbox(ctx, CRYPT_AES_256, key, false);
86 #else
87     SetAesKeyExpansionSbox(ctx, CRYPT_AES_256, key);
88 #endif
89 }
90 
CRYPT_AES_Encrypt(const CRYPT_AES_Key * ctx,const uint8_t * in,uint8_t * out,uint32_t len)91 int32_t CRYPT_AES_Encrypt(const CRYPT_AES_Key *ctx, const uint8_t *in, uint8_t *out, uint32_t len)
92 {
93 #ifdef HITLS_CRYPTO_AES_PRECALC_TABLES
94     CRYPT_AES_EncryptTbox(ctx, in, out, len);
95 #else
96     CRYPT_AES_EncryptSbox(ctx, in, out, len);
97 #endif
98     return CRYPT_SUCCESS;
99 }
100 
CRYPT_AES_Decrypt(const CRYPT_AES_Key * ctx,const uint8_t * in,uint8_t * out,uint32_t len)101 int32_t CRYPT_AES_Decrypt(const CRYPT_AES_Key *ctx, const uint8_t *in, uint8_t *out, uint32_t len)
102 {
103 #ifdef HITLS_CRYPTO_AES_PRECALC_TABLES
104     CRYPT_AES_DecryptTbox(ctx, in, out, len);
105 #else
106     CRYPT_AES_DecryptSbox(ctx, in, out, len);
107 #endif
108     return CRYPT_SUCCESS;
109 }
110 #endif /* HITLS_CRYPTO_AES */
111