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 "crypt_aes_local.h"
24 #include "bsl_sal.h"
25
CRYPT_AES_SetEncryptKey128(CRYPT_AES_Key * ctx,const uint8_t * key,uint32_t len)26 int32_t CRYPT_AES_SetEncryptKey128(CRYPT_AES_Key *ctx, const uint8_t *key, uint32_t len)
27 {
28 if (ctx == NULL || key == NULL) {
29 BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT);
30 return CRYPT_NULL_INPUT;
31 }
32 if (len != 16) {
33 BSL_ERR_PUSH_ERROR(CRYPT_AES_ERR_KEYLEN);
34 return CRYPT_AES_ERR_KEYLEN;
35 }
36 SetEncryptKey128(ctx, key);
37 return CRYPT_SUCCESS;
38 }
39
CRYPT_AES_SetEncryptKey192(CRYPT_AES_Key * ctx,const uint8_t * key,uint32_t len)40 int32_t CRYPT_AES_SetEncryptKey192(CRYPT_AES_Key *ctx, const uint8_t *key, uint32_t len)
41 {
42 if (ctx == NULL || key == NULL) {
43 BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT);
44 return CRYPT_NULL_INPUT;
45 }
46 if (len != 24) {
47 BSL_ERR_PUSH_ERROR(CRYPT_AES_ERR_KEYLEN);
48 return CRYPT_AES_ERR_KEYLEN;
49 }
50 SetEncryptKey192(ctx, key);
51 return CRYPT_SUCCESS;
52 }
53
CRYPT_AES_SetEncryptKey256(CRYPT_AES_Key * ctx,const uint8_t * key,uint32_t len)54 int32_t CRYPT_AES_SetEncryptKey256(CRYPT_AES_Key *ctx, const uint8_t *key, uint32_t len)
55 {
56 if (ctx == NULL || key == NULL) {
57 BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT);
58 return CRYPT_NULL_INPUT;
59 }
60 if (len != 32) {
61 BSL_ERR_PUSH_ERROR(CRYPT_AES_ERR_KEYLEN);
62 return CRYPT_AES_ERR_KEYLEN;
63 }
64 SetEncryptKey256(ctx, key);
65 return CRYPT_SUCCESS;
66 }
67
CRYPT_AES_SetDecryptKey128(CRYPT_AES_Key * ctx,const uint8_t * key,uint32_t len)68 int32_t CRYPT_AES_SetDecryptKey128(CRYPT_AES_Key *ctx, const uint8_t *key, uint32_t len)
69 {
70 if (ctx == NULL || key == NULL) {
71 BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT);
72 return CRYPT_NULL_INPUT;
73 }
74 if (len != 16) {
75 BSL_ERR_PUSH_ERROR(CRYPT_AES_ERR_KEYLEN);
76 return CRYPT_AES_ERR_KEYLEN;
77 }
78 SetDecryptKey128(ctx, key);
79 return CRYPT_SUCCESS;
80 }
81
CRYPT_AES_SetDecryptKey192(CRYPT_AES_Key * ctx,const uint8_t * key,uint32_t len)82 int32_t CRYPT_AES_SetDecryptKey192(CRYPT_AES_Key *ctx, const uint8_t *key, uint32_t len)
83 {
84 if (ctx == NULL || key == NULL) {
85 BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT);
86 return CRYPT_NULL_INPUT;
87 }
88 if (len != 24) {
89 BSL_ERR_PUSH_ERROR(CRYPT_AES_ERR_KEYLEN);
90 return CRYPT_AES_ERR_KEYLEN;
91 }
92 SetDecryptKey192(ctx, key);
93 return CRYPT_SUCCESS;
94 }
95
CRYPT_AES_SetDecryptKey256(CRYPT_AES_Key * ctx,const uint8_t * key,uint32_t len)96 int32_t CRYPT_AES_SetDecryptKey256(CRYPT_AES_Key *ctx, const uint8_t *key, uint32_t len)
97 {
98 if (ctx == NULL || key == NULL) {
99 BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT);
100 return CRYPT_NULL_INPUT;
101 }
102 if (len != 32) {
103 BSL_ERR_PUSH_ERROR(CRYPT_AES_ERR_KEYLEN);
104 return CRYPT_AES_ERR_KEYLEN;
105 }
106 SetDecryptKey256(ctx, key);
107 return CRYPT_SUCCESS;
108 }
109
CRYPT_AES_Clean(CRYPT_AES_Key * ctx)110 void CRYPT_AES_Clean(CRYPT_AES_Key *ctx)
111 {
112 if (ctx == NULL) {
113 return;
114 }
115 BSL_SAL_CleanseData((void *)(ctx), sizeof(CRYPT_AES_Key));
116 }
117 #endif /* HITLS_CRYPTO_AES */
118