1 /*
2 * Copyright (C) 2019-2020 Alibaba Group Holding Limited
3 */
4
5 #include <string.h>
6 #include "linkkit/wrappers/wrappers_os.h"
7 #include "mbedtls/aes.h"
8
9 #define AES_BLOCK_SIZE 16
10
11 typedef struct {
12 mbedtls_aes_context ctx;
13 uint8_t iv[16];
14 } platform_aes_t;
15
HAL_Aes128_Init(_IN_ const uint8_t * key,_IN_ const uint8_t * iv,_IN_ AES_DIR_t dir)16 p_HAL_Aes128_t HAL_Aes128_Init(_IN_ const uint8_t *key, _IN_ const uint8_t *iv,
17 _IN_ AES_DIR_t dir)
18 {
19 int ret = 0;
20 platform_aes_t *p_aes128 = NULL;
21
22 if (!key || !iv)
23 return p_aes128;
24
25 p_aes128 = (platform_aes_t *)calloc(1, sizeof(platform_aes_t));
26 if (!p_aes128)
27 return p_aes128;
28
29 mbedtls_aes_init(&p_aes128->ctx);
30
31 if (dir == HAL_AES_ENCRYPTION) {
32 ret = mbedtls_aes_setkey_enc(&p_aes128->ctx, key, 128);
33 } else {
34 ret = mbedtls_aes_setkey_dec(&p_aes128->ctx, key, 128);
35 }
36
37 if (ret == 0) {
38 memcpy(p_aes128->iv, iv, 16);
39 } else {
40 free(p_aes128);
41 p_aes128 = NULL;
42 }
43
44 return (p_HAL_Aes128_t *)p_aes128;
45 }
46
HAL_Aes128_Destroy(_IN_ p_HAL_Aes128_t aes)47 int HAL_Aes128_Destroy(_IN_ p_HAL_Aes128_t aes)
48 {
49 if (!aes)
50 return -1;
51
52 mbedtls_aes_free(&((platform_aes_t *)aes)->ctx);
53 free(aes);
54
55 return 0;
56 }
57
HAL_Aes128_Cbc_Encrypt(_IN_ p_HAL_Aes128_t aes,_IN_ const void * src,_IN_ size_t blockNum,_OU_ void * dst)58 int HAL_Aes128_Cbc_Encrypt(_IN_ p_HAL_Aes128_t aes, _IN_ const void *src,
59 _IN_ size_t blockNum, _OU_ void *dst)
60 {
61 int i = 0;
62 int ret = 0;
63 platform_aes_t *p_aes128 = (platform_aes_t *)aes;
64
65 if (!aes || !src || !dst)
66 return -1;
67
68 for (i = 0; i < blockNum; ++i) {
69 ret = mbedtls_aes_crypt_cbc(&p_aes128->ctx, MBEDTLS_AES_ENCRYPT,
70 AES_BLOCK_SIZE, p_aes128->iv, src, dst);
71 src = (char *)src + 16;
72 dst = (char *)dst + 16;
73 }
74
75 return ret;
76 }
77
HAL_Aes128_Cbc_Decrypt(_IN_ p_HAL_Aes128_t aes,_IN_ const void * src,_IN_ size_t blockNum,_OU_ void * dst)78 int HAL_Aes128_Cbc_Decrypt(_IN_ p_HAL_Aes128_t aes, _IN_ const void *src,
79 _IN_ size_t blockNum, _OU_ void *dst)
80 {
81 int i = 0;
82 int ret = -1;
83 platform_aes_t *p_aes128 = (platform_aes_t *)aes;
84
85 if (!aes || !src || !dst)
86 return ret;
87
88 for (i = 0; i < blockNum; ++i) {
89 ret = mbedtls_aes_crypt_cbc(&p_aes128->ctx, MBEDTLS_AES_DECRYPT,
90 AES_BLOCK_SIZE, p_aes128->iv, src, dst);
91 src = (char *)src + 16;
92 dst = (char *)dst + 16;
93 }
94
95 return ret;
96 }
97 #if defined(MBEDTLS_CIPHER_MODE_CFB)
HAL_Aes128_Cfb_Encrypt(_IN_ p_HAL_Aes128_t aes,_IN_ const void * src,_IN_ size_t length,_OU_ void * dst)98 int HAL_Aes128_Cfb_Encrypt(_IN_ p_HAL_Aes128_t aes, _IN_ const void *src,
99 _IN_ size_t length, _OU_ void *dst)
100 {
101 size_t offset = 0;
102 int ret = -1;
103 platform_aes_t *p_aes128 = (platform_aes_t *)aes;
104
105 if (!aes || !src || !dst)
106 return ret;
107
108 ret = mbedtls_aes_crypt_cfb128(&p_aes128->ctx, MBEDTLS_AES_ENCRYPT, length,
109 &offset, p_aes128->iv, src, dst);
110 return ret;
111 }
112 #endif
113
114 #if defined(MBEDTLS_CIPHER_MODE_CFB)
HAL_Aes128_Cfb_Decrypt(_IN_ p_HAL_Aes128_t aes,_IN_ const void * src,_IN_ size_t length,_OU_ void * dst)115 int HAL_Aes128_Cfb_Decrypt(_IN_ p_HAL_Aes128_t aes, _IN_ const void *src,
116 _IN_ size_t length, _OU_ void *dst)
117 {
118 size_t offset = 0;
119 int ret = -1;
120 platform_aes_t *p_aes128 = (platform_aes_t *)aes;
121
122 if (!aes || !src || !dst)
123 return ret;
124
125 ret = mbedtls_aes_crypt_cfb128(&p_aes128->ctx, MBEDTLS_AES_DECRYPT, length,
126 &offset, p_aes128->iv, src, dst);
127 return ret;
128 }
129 #endif
130
131 /**
132 * @brief 获取配网服务的安全等级
133 *
134 * @param None.
135 * @return The security level:
136 @verbatim
137 0: open (no encrypt)
138 1: aes256cfb with default aes-key and aes-iv
139 2: aes128cfb with default aes-key and aes-iv
140 3: aes128cfb with aes-key per product and aes-iv = 0
141 4: aes128cfb with aes-key per device and aes-iv = 0
142 5: aes128cfb with aes-key per manufacture and aes-iv = 0
143 others: invalid
144 @endverbatim
145 * @see None.
146 */
HAL_Awss_Get_Encrypt_Type()147 int HAL_Awss_Get_Encrypt_Type()
148 {
149 return 3;
150 }
151
HAL_Awss_Get_Conn_Encrypt_Type(void)152 int HAL_Awss_Get_Conn_Encrypt_Type(void)
153 {
154 return 3;
155 }
156