1 /**
2   ******************************************************************************
3   * @file    rtl8721dhp_aes_rom.c
4   * @author
5   * @version V1.0.0
6   * @date    2016-05-17
7   * @brief   This file provides firmware functions to manage the following
8   *          functionalities of the HW crypto:
9   *           - Initialization
10   *           - AES CBC/ECB/CTR/CFB/OFB/GCM
11   ******************************************************************************
12   * @attention
13   *
14   * This module is a confidential and proprietary property of RealTek and
15   * possession or use of this module requires written permission of RealTek.
16   *
17   * Copyright(c) 2015, Realtek Semiconductor Corporation. All rights reserved.
18   ******************************************************************************
19   */
20 
21 #include "ameba_soc.h"
22 
23 HAL_ROM_DATA_SECTION
24 static const u8 gcm_iv_tail[] __attribute__((aligned(32))) = {0x00, 0x00, 0x00, 0x01};
25 
26 /**
27   * @brief  AES-CBC init, set key
28   * @param	key		: need to be 4 byte alignment
29   * @param	keylen	: key length <= 32
30   * @retval	0		: SUCCESS <br>
31   *			others	: fail, refer to ERRNO
32   */
33 __weak HAL_ROM_TEXT_SECTION
rtl_crypto_aes_cbc_init(IN const u8 * key,IN const u32 keylen)34 int rtl_crypto_aes_cbc_init(IN const u8* key, IN const u32 keylen)
35 {
36 	HAL_CRYPTO_ADAPTER *pIE = &crypto_engine;
37 
38 	assert_param( (pIE != NULL) && (pIE->isInit == _TRUE) );
39 	if ( key == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
40 	if ( (u32)(key) & 0x3 ) return _ERRNO_CRYPTO_ADDR_NOT_4Byte_Aligned; // need to be 4-byte alignment
41 	if ( keylen > CRYPTO_MAX_KEY_LENGTH ) return _ERRNO_CRYPTO_KEY_OutRange;
42 
43 	return CRYPTO_CipherInit(pIE, CIPHER_TYPE_AES_CBC, key, keylen);
44 }
45 
46 /**
47   * @brief  AES-CBC encrypt
48   * @param 	message : input buffer
49   * @param    msglen 	: input buffer length
50   * @param    pResult	: output result buffer
51   * @param    iv		: IV
52   * @param    ivlen		: IV length
53   * @retval	0		: SUCCESS <br>
54   *			others	: fail, refer to ERRNO
55   */
56 __weak HAL_ROM_TEXT_SECTION
rtl_crypto_aes_cbc_encrypt(IN const u8 * message,IN const u32 msglen,IN const u8 * iv,IN const u32 ivlen,OUT u8 * pResult)57 int rtl_crypto_aes_cbc_encrypt(
58 	IN const u8* message, 	IN const u32 msglen,
59 	IN const u8* iv, 		IN const u32 ivlen,
60 	OUT u8* pResult)
61 {
62 	HAL_CRYPTO_ADAPTER *pIE = &crypto_engine;
63 
64 	if ( iv == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
65 	if ( (u32)(iv) & 0x3 ) return _ERRNO_CRYPTO_ADDR_NOT_4Byte_Aligned; // need to be 4-byte alignment
66 	if ( ivlen != 16 ) return _ERRNO_CRYPTO_IV_OutRange;
67 	if ( message == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
68 	if ( pResult == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
69 
70 	return CRYPTO_CipherEncryptAD(pIE, message, msglen, iv, ivlen, NULL, 0, pResult, NULL);
71 }
72 
73 
74 /**
75   * @brief  AES-CBC decrypt
76   * @param 	message : input buffer
77   * @param    msglen 	: input buffer length
78   * @param    pResult	: output result buffer
79   * @param    iv		: IV
80   * @param    ivlen		: IV length
81   * @retval	0		: SUCCESS <br>
82   *			others	: fail, refer to ERRNO
83   */
84 __weak HAL_ROM_TEXT_SECTION
rtl_crypto_aes_cbc_decrypt(IN const u8 * message,IN const u32 msglen,IN const u8 * iv,IN const u32 ivlen,OUT u8 * pResult)85 int rtl_crypto_aes_cbc_decrypt(
86 	IN const u8* message, 	IN const u32 msglen,
87 	IN const u8* iv, 		IN const u32 ivlen,
88 	OUT u8* pResult)
89 {
90 	HAL_CRYPTO_ADAPTER *pIE = &crypto_engine;
91 
92 	if ( iv == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
93 	if ( (u32)(iv) & 0x3 ) return _ERRNO_CRYPTO_ADDR_NOT_4Byte_Aligned; // need to be 4-byte alignment
94 	if ( ivlen != 16 ) return _ERRNO_CRYPTO_IV_OutRange;
95 	if ( message == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
96 	if ( pResult == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
97 
98 	return CRYPTO_CipherDecryptAD(pIE, message, msglen, iv, ivlen, NULL, 0, pResult, NULL);
99 }
100 
101 /**
102   * @brief  AES-ECB init, set key
103   * @param	key		: need to be 4 byte alignment
104   * @param	keylen	: key length <= 32
105   * @retval	0		: SUCCESS <br>
106   *			others	: fail, refer to ERRNO
107   */
108 __weak HAL_ROM_TEXT_SECTION
rtl_crypto_aes_ecb_init(IN const u8 * key,IN const u32 keylen)109 int rtl_crypto_aes_ecb_init(IN const u8* key, IN const u32 keylen)
110 {
111 	HAL_CRYPTO_ADAPTER *pIE = &crypto_engine;
112 
113 	assert_param( (pIE != NULL) && (pIE->isInit == _TRUE) );
114 	if ( key == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
115 	if ( (u32)(key) & 0x3 ) return _ERRNO_CRYPTO_ADDR_NOT_4Byte_Aligned; // need to be 4-byte alignment
116 	if ( keylen > CRYPTO_MAX_KEY_LENGTH ) return _ERRNO_CRYPTO_KEY_OutRange;
117 
118 	return CRYPTO_CipherInit(pIE, CIPHER_TYPE_AES_ECB, key, keylen);
119 }
120 
121 /**
122   * @brief  AES-ECB encrypt
123   * @param 	message : input buffer
124   * @param    msglen 	: input buffer length
125   * @param    pResult	: output result buffer
126   * @param    iv		: IV
127   * @param    ivlen		: IV length
128   * @retval	0		: SUCCESS <br>
129   *			others	: fail, refer to ERRNO
130   */
131 __weak HAL_ROM_TEXT_SECTION
rtl_crypto_aes_ecb_encrypt(IN const u8 * message,IN const u32 msglen,IN const u8 * iv,IN const u32 ivlen,OUT u8 * pResult)132 int rtl_crypto_aes_ecb_encrypt(
133 	IN const u8* message, 	IN const u32 msglen,
134 	IN const u8* iv, 		IN const u32 ivlen,
135 	OUT u8* pResult)
136 {
137 	HAL_CRYPTO_ADAPTER *pIE = &crypto_engine;
138 
139 	//if ( ivlen != 0 ) return _ERRNO_CRYPTO_IV_OutRange; // ECB mode : ivlen = 0
140 	if ( message == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
141 	if ( pResult == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
142 
143 	return CRYPTO_CipherEncryptAD(pIE, message, msglen, iv, ivlen, NULL, 0, pResult, NULL);
144 }
145 
146 /**
147   * @brief  AES-ECB decrypt
148   * @param 	message : input buffer
149   * @param    msglen 	: input buffer length
150   * @param    pResult	: output result buffer
151   * @param    iv		: IV
152   * @param    ivlen		: IV length
153   * @retval	0		: SUCCESS <br>
154   *			others	: fail, refer to ERRNO
155   */
156 __weak HAL_ROM_TEXT_SECTION
rtl_crypto_aes_ecb_decrypt(IN const u8 * message,IN const u32 msglen,IN const u8 * iv,IN const u32 ivlen,OUT u8 * pResult)157 int rtl_crypto_aes_ecb_decrypt(
158 		IN const u8* message, 	IN const u32 msglen,
159 		IN const u8* iv, 		IN const u32 ivlen,
160 		OUT u8* pResult)
161 {
162 	HAL_CRYPTO_ADAPTER *pIE = &crypto_engine;
163 
164 	if ( message == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
165 	if ( pResult == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
166 
167 	return CRYPTO_CipherDecryptAD(pIE, message, msglen, iv, ivlen, NULL, 0, pResult, NULL);
168 }
169 
170 /**
171   * @brief  AES-CTR init, set key
172   * @param	key		: need to be 4 byte alignment
173   * @param	keylen	: key length <= 32
174   * @retval	0		: SUCCESS <br>
175   *			others	: fail, refer to ERRNO
176   */
177 __weak HAL_ROM_TEXT_SECTION
rtl_crypto_aes_ctr_init(IN const u8 * key,IN const u32 keylen)178 int rtl_crypto_aes_ctr_init(IN const u8* key, 	IN const u32 keylen)
179 {
180 	HAL_CRYPTO_ADAPTER *pIE = &crypto_engine;
181 
182 	assert_param( (pIE != NULL) && (pIE->isInit == _TRUE) );
183 	if ( key == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
184 	if ( (u32)(key) & 0x3 ) return _ERRNO_CRYPTO_ADDR_NOT_4Byte_Aligned; // need to be 4-byte alignment
185 	if ( keylen > CRYPTO_MAX_KEY_LENGTH ) return _ERRNO_CRYPTO_KEY_OutRange;
186 
187 	return CRYPTO_CipherInit(pIE, CIPHER_TYPE_AES_CTR, key, keylen);
188 }
189 
190 /**
191   * @brief  AES-CTR encrypt
192   * @param 	message : input buffer
193   * @param    msglen 	: input buffer length
194   * @param    pResult	: output result buffer
195   * @param    iv		: IV
196   * @param    ivlen		: IV length
197   * @retval	0		: SUCCESS <br>
198   *			others	: fail, refer to ERRNO
199   */
200 __weak HAL_ROM_TEXT_SECTION
rtl_crypto_aes_ctr_encrypt(IN const u8 * message,IN const u32 msglen,IN const u8 * iv,IN const u32 ivlen,OUT u8 * pResult)201 int rtl_crypto_aes_ctr_encrypt(
202 	IN const u8* message, 	IN const u32 msglen,
203 	IN const u8* iv, 		IN const u32 ivlen,
204 	OUT u8* pResult)
205 {
206 	HAL_CRYPTO_ADAPTER *pIE = &crypto_engine;
207 
208 	if ( iv == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
209 	if ( (u32)(iv) & 0x3 ) return _ERRNO_CRYPTO_ADDR_NOT_4Byte_Aligned; // need to be 4-byte alignment
210 	if ( message == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
211 	if ( pResult == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
212 
213 	return CRYPTO_CipherEncryptAD(pIE, message, msglen, iv, ivlen, NULL, 0, pResult, NULL);
214 }
215 
216 /**
217   * @brief  AES-CTR decrypt
218   * @param 	message : input buffer
219   * @param    msglen 	: input buffer length
220   * @param    pResult	: output result buffer
221   * @param    iv		: IV
222   * @param    ivlen		: IV length
223   * @retval	0		: SUCCESS <br>
224   *			others	: fail, refer to ERRNO
225   */
226 __weak HAL_ROM_TEXT_SECTION
rtl_crypto_aes_ctr_decrypt(IN const u8 * message,IN const u32 msglen,IN const u8 * iv,IN const u32 ivlen,OUT u8 * pResult)227 int rtl_crypto_aes_ctr_decrypt(
228 	IN const u8* message, 	IN const u32 msglen,
229 	IN const u8* iv, 		IN const u32 ivlen,
230 	OUT u8* pResult)
231 {
232 	HAL_CRYPTO_ADAPTER *pIE = &crypto_engine;
233 
234 	if ( iv == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
235 	if ( (u32)(iv) & 0x3 ) return _ERRNO_CRYPTO_ADDR_NOT_4Byte_Aligned; // need to be 4-byte alignment
236 	if ( message == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
237 	if ( pResult == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
238 
239 	return CRYPTO_CipherDecryptAD(pIE, message, msglen, iv, ivlen, NULL, 0, pResult, NULL);
240 }
241 
242 /**
243   * @brief  AES-CFB init, set key
244   * @param	key		: need to be 4 byte alignment
245   * @param	keylen	: key length <= 32
246   * @retval	0		: SUCCESS <br>
247   *			others	: fail, refer to ERRNO
248   */
249 __weak HAL_ROM_TEXT_SECTION
rtl_crypto_aes_cfb_init(IN const u8 * key,IN const u32 keylen)250 int rtl_crypto_aes_cfb_init(IN const u8* key, 	IN const u32 keylen)
251 {
252 	HAL_CRYPTO_ADAPTER *pIE = &crypto_engine;
253 
254 	assert_param( (pIE != NULL) && (pIE->isInit == _TRUE) );
255 	if ( key == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
256 	if ( (u32)(key) & 0x3 ) return _ERRNO_CRYPTO_ADDR_NOT_4Byte_Aligned; // need to be 4-byte alignment
257 	if ( keylen > CRYPTO_MAX_KEY_LENGTH ) return _ERRNO_CRYPTO_KEY_OutRange;
258 
259 	return CRYPTO_CipherInit(pIE, CIPHER_TYPE_AES_CFB, key, keylen);
260 }
261 
262 /**
263   * @brief  AES-CFB encrypt
264   * @param 	message : input buffer
265   * @param    msglen 	: input buffer length
266   * @param    pResult	: output result buffer
267   * @param    iv		: IV
268   * @param    ivlen		: IV length
269   * @retval	0		: SUCCESS <br>
270   *			others	: fail, refer to ERRNO
271   */
272 __weak HAL_ROM_TEXT_SECTION
rtl_crypto_aes_cfb_encrypt(IN const u8 * message,IN const u32 msglen,IN const u8 * iv,IN const u32 ivlen,OUT u8 * pResult)273 int rtl_crypto_aes_cfb_encrypt(
274 	IN const u8* message, 	IN const u32 msglen,
275 	IN const u8* iv, 		IN const u32 ivlen,
276 	OUT u8* pResult)
277 {
278 	HAL_CRYPTO_ADAPTER *pIE = &crypto_engine;
279 
280 	if ( iv == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
281 	if ( (u32)(iv) & 0x3 ) return _ERRNO_CRYPTO_ADDR_NOT_4Byte_Aligned; // need to be 4-byte alignment
282 	if ( message == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
283 	if ( pResult == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
284 
285 	return CRYPTO_CipherEncryptAD(pIE, message, msglen, iv, ivlen, NULL, 0, pResult, NULL);
286 }
287 
288 /**
289   * @brief  AES-CFB decrypt
290   * @param 	message : input buffer
291   * @param    msglen 	: input buffer length
292   * @param    pResult	: output result buffer
293   * @param    iv		: IV
294   * @param    ivlen		: IV length
295   * @retval	0		: SUCCESS <br>
296   *			others	: fail, refer to ERRNO
297   */
298 __weak HAL_ROM_TEXT_SECTION
rtl_crypto_aes_cfb_decrypt(IN const u8 * message,IN const u32 msglen,IN const u8 * iv,IN const u32 ivlen,OUT u8 * pResult)299 int rtl_crypto_aes_cfb_decrypt(
300 	IN const u8* message, 	IN const u32 msglen,
301 	IN const u8* iv, 		IN const u32 ivlen,
302 	OUT u8* pResult)
303 {
304 	HAL_CRYPTO_ADAPTER *pIE = &crypto_engine;
305 
306 	if ( iv == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
307 	if ( (u32)(iv) & 0x3 ) return _ERRNO_CRYPTO_ADDR_NOT_4Byte_Aligned; // need to be 4-byte alignment
308 	if ( message == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
309 	if ( pResult == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
310 
311 	return CRYPTO_CipherDecryptAD(pIE, message, msglen, iv, ivlen, NULL, 0, pResult, NULL);
312 }
313 
314 /**
315   * @brief  AES-OFB init, set key
316   * @param	key		: need to be 4 byte alignment
317   * @param	keylen	: key length <= 32
318   * @retval	0		: SUCCESS <br>
319   *			others	: fail, refer to ERRNO
320   */
321 __weak HAL_ROM_TEXT_SECTION
rtl_crypto_aes_ofb_init(IN const u8 * key,IN const u32 keylen)322 int rtl_crypto_aes_ofb_init(IN const u8* key, 	IN const u32 keylen)
323 {
324 	HAL_CRYPTO_ADAPTER *pIE = &crypto_engine;
325 
326 	assert_param( (pIE != NULL) && (pIE->isInit == _TRUE) );
327 	if ( key == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
328 	if ( (u32)(key) & 0x3 ) return _ERRNO_CRYPTO_ADDR_NOT_4Byte_Aligned; // need to be 4-byte alignment
329 	if ( keylen > CRYPTO_MAX_KEY_LENGTH ) return _ERRNO_CRYPTO_KEY_OutRange;
330 
331 	return CRYPTO_CipherInit(pIE, CIPHER_TYPE_AES_OFB, key, keylen);
332 }
333 
334 /**
335   * @brief  AES-OFB encrypt
336   * @param 	message : input buffer
337   * @param    msglen 	: input buffer length
338   * @param    pResult	: output result buffer
339   * @param    iv		: IV
340   * @param    ivlen		: IV length
341   * @retval	0		: SUCCESS <br>
342   *			others	: fail, refer to ERRNO
343   */
344 __weak HAL_ROM_TEXT_SECTION
rtl_crypto_aes_ofb_encrypt(IN const u8 * message,IN const u32 msglen,IN const u8 * iv,IN const u32 ivlen,OUT u8 * pResult)345 int rtl_crypto_aes_ofb_encrypt(
346 	IN const u8* message, 	IN const u32 msglen,
347 	IN const u8* iv, 		IN const u32 ivlen,
348 	OUT u8* pResult)
349 {
350 	HAL_CRYPTO_ADAPTER *pIE = &crypto_engine;
351 
352 	if ( iv == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
353 	if ( (u32)(iv) & 0x3 ) return _ERRNO_CRYPTO_ADDR_NOT_4Byte_Aligned; // need to be 4-byte alignment
354 	if ( message == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
355 	if ( pResult == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
356 
357 	return CRYPTO_CipherEncryptAD(pIE, message, msglen, iv, ivlen, NULL, 0, pResult, NULL);
358 }
359 
360 /**
361   * @brief  AES-OFB decrypt
362   * @param 	message : input buffer
363   * @param    msglen 	: input buffer length
364   * @param    pResult	: output result buffer
365   * @param    iv		: IV
366   * @param    ivlen		: IV length
367   * @retval	0		: SUCCESS <br>
368   *			others	: fail, refer to ERRNO
369   */
370 __weak HAL_ROM_TEXT_SECTION
rtl_crypto_aes_ofb_decrypt(IN const u8 * message,IN const u32 msglen,IN const u8 * iv,IN const u32 ivlen,OUT u8 * pResult)371 int rtl_crypto_aes_ofb_decrypt(
372 	IN const u8* message, 	IN const u32 msglen,
373 	IN const u8* iv, 		IN const u32 ivlen,
374 	OUT u8* pResult)
375 {
376 	HAL_CRYPTO_ADAPTER *pIE = &crypto_engine;
377 
378 	if ( iv == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
379 	if ( (u32)(iv) & 0x3 ) return _ERRNO_CRYPTO_ADDR_NOT_4Byte_Aligned; // need to be 4-byte alignment
380 	if ( message == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
381 	if ( pResult == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
382 
383 	return CRYPTO_CipherDecryptAD(pIE, message, msglen, iv, ivlen, NULL, 0, pResult, NULL);
384 }
385 
386 /**
387   * @brief  AES-GCM init, set key
388   * @param	key		: need to be 4 byte alignment
389   * @param	keylen	: key length <= 32
390   * @retval	0		: SUCCESS <br>
391   *			others	: fail, refer to ERRNO
392   */
393 __weak HAL_ROM_TEXT_SECTION
rtl_crypto_aes_gcm_init(IN const u8 * key,IN const u32 keylen)394 int rtl_crypto_aes_gcm_init(IN const u8* key, 	IN const u32 keylen)
395 {
396 	HAL_CRYPTO_ADAPTER *pIE = &crypto_engine;
397 
398 	assert_param( (pIE != NULL) && (pIE->isInit == _TRUE) );
399 	if ( key == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
400 	if ( (u32)(key) & 0x3 ) return _ERRNO_CRYPTO_ADDR_NOT_4Byte_Aligned; // need to be 4-byte alignment
401 	if ( keylen > CRYPTO_MAX_KEY_LENGTH ) return _ERRNO_CRYPTO_KEY_OutRange;
402 
403 	return CRYPTO_CipherInit(pIE, CIPHER_TYPE_AES_GCM, key, keylen);
404 }
405 
406 /**
407   * @brief  AES-GCM encrypt
408   * @param 	message : input buffer
409   * @param    msglen 	: input buffer length
410   * @param    pResult	: output result buffer
411   * @param    iv		: IV
412   * @param  aad: Point to AAD(additional authentication data).
413   * @param  aadlen: AAD length.
414   * @param  pResult: Point to cipher result.
415   * @param  pTag: Point to MAC(Message Authentication Code) .
416   * @retval	0		: SUCCESS <br>
417   *			others	: fail, refer to ERRNO
418   */
419 __weak HAL_ROM_TEXT_SECTION
rtl_crypto_aes_gcm_encrypt(IN const u8 * message,IN const u32 msglen,IN const u8 * iv,IN const u8 * aad,IN const u32 aadlen,OUT u8 * pResult,OUT u8 * pTag)420 int rtl_crypto_aes_gcm_encrypt(
421 	IN const u8* message, 	IN const u32 msglen,
422 	IN const u8* iv, 	IN const u8 *aad, IN const u32 aadlen,
423 	OUT u8* pResult, OUT u8* pTag)
424 {
425 	HAL_CRYPTO_ADAPTER *pIE = &crypto_engine;
426 
427 	if ( iv == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
428 	if ( (u32)(iv) & 0x3 ) return _ERRNO_CRYPTO_ADDR_NOT_4Byte_Aligned; // need to be 4-byte alignment
429 	if ( message == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
430 	if ( (u32)(aad) & 0x3 ) return _ERRNO_CRYPTO_ADDR_NOT_4Byte_Aligned;
431 	if ( pResult == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
432 	if ( pTag == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
433 
434 	_memcpy((void*)(pIE->gcm_iv), (const void*)(iv), 96/8);
435 	_memcpy((void*)(&(pIE->gcm_iv)[12]), (const void*)(gcm_iv_tail), 4);
436 	return CRYPTO_CipherEncryptAD(pIE, message, msglen, (u8*)(pIE->gcm_iv), 16, aad, aadlen, pResult, pTag);
437 }
438 
439 /**
440   * @brief  AES-GCM decrypt
441   * @param 	message : input buffer
442   * @param    msglen 	: input buffer length
443   * @param    pResult	: output result buffer
444   * @param    iv		: IV
445   * @param  aad: Point to AAD(additional authentication data).
446   * @param  aadlen: AAD length.
447   * @param  pResult: Point to cipher result.
448   * @param  pTag: Point to MAC(Message Authentication Code) .
449   * @retval	0		: SUCCESS <br>
450   *			others	: fail, refer to ERRNO
451   */
452 __weak HAL_ROM_TEXT_SECTION
rtl_crypto_aes_gcm_decrypt(IN const u8 * message,IN const u32 msglen,IN const u8 * iv,IN const u8 * aad,IN const u32 aadlen,OUT u8 * pResult,OUT u8 * pTag)453 int rtl_crypto_aes_gcm_decrypt(
454 	IN const u8* message, 	IN const u32 msglen,
455 	IN const u8* iv, 	IN const u8 *aad, IN const u32 aadlen,
456 	OUT u8* pResult, OUT u8* pTag)
457 {
458 	HAL_CRYPTO_ADAPTER *pIE = &crypto_engine;
459 
460 	if ( iv == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
461 	if ( (u32)(iv) & 0x3 ) return _ERRNO_CRYPTO_ADDR_NOT_4Byte_Aligned; // need to be 4-byte alignment
462 	if ( message == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
463 	if ( (u32)(aad) & 0x3 ) return _ERRNO_CRYPTO_ADDR_NOT_4Byte_Aligned;
464 	if ( pResult == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
465 	if ( pTag == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
466 
467 	_memcpy((void*)(pIE->gcm_iv), (const void*)(iv), 96/8);
468 	_memcpy((void*)(&(pIE->gcm_iv)[12]), (const void*)(gcm_iv_tail), 4);
469 	return CRYPTO_CipherDecryptAD(pIE, message, msglen, (u8*)(pIE->gcm_iv), 16, aad, aadlen, pResult, pTag);
470 }
471 /******************* (C) COPYRIGHT 2016 Realtek Semiconductor *****END OF FILE****/
472