1 /**
2 ******************************************************************************
3 * @file rtl8721dhp_des_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 * - 3DES CBC/ECB
10 * - DES CBC/ECB
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 /**
24 * @brief 3DES-CBC init, set key
25 * @param key : need to be 4 byte alignment
26 * @param keylen : key length <= 32
27 * @retval 0 : SUCCESS <br>
28 * others : fail, refer to ERRNO
29 */
30 __weak HAL_ROM_TEXT_SECTION
rtl_crypto_3des_cbc_init(IN const u8 * key,IN const u32 keylen)31 int rtl_crypto_3des_cbc_init(IN const u8* key, IN const u32 keylen)
32 {
33 HAL_CRYPTO_ADAPTER *pIE = &crypto_engine;
34
35 assert_param( (pIE != NULL) && (pIE->isInit == _TRUE) );
36 if ( key == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
37 if ( (u32)(key) & 0x3 ) return _ERRNO_CRYPTO_ADDR_NOT_4Byte_Aligned; // need to be 4-byte alignment
38 if ( keylen > CRYPTO_MAX_KEY_LENGTH ) return _ERRNO_CRYPTO_KEY_OutRange;
39
40 return CRYPTO_CipherInit(pIE, CIPHER_TYPE_3DES_CBC, key, keylen);
41 }
42
43 /**
44 * @brief 3DES-CBC encrypt
45 * @param message : input buffer
46 * @param msglen : input buffer length
47 * @param pResult : output result buffer
48 * @param iv : IV
49 * @param ivlen : IV length
50 * @retval 0 : SUCCESS <br>
51 * others : fail, refer to ERRNO
52 */
53 __weak HAL_ROM_TEXT_SECTION
rtl_crypto_3des_cbc_encrypt(IN const u8 * message,IN const u32 msglen,IN const u8 * iv,IN const u32 ivlen,OUT u8 * pResult)54 int rtl_crypto_3des_cbc_encrypt(
55 IN const u8* message, IN const u32 msglen,
56 IN const u8* iv, IN const u32 ivlen,
57 OUT u8* pResult)
58 {
59 HAL_CRYPTO_ADAPTER *pIE = &crypto_engine;
60
61 if ( iv == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
62 if ( (u32)(iv) & 0x3 ) return _ERRNO_CRYPTO_ADDR_NOT_4Byte_Aligned; // need to be 4-byte alignment
63 if ( ivlen > CRYPTO_MAX_KEY_LENGTH) return _ERRNO_CRYPTO_IV_OutRange;
64 if ( message == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
65 if ( pResult == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
66
67 return CRYPTO_CipherEncryptAD(pIE, message, msglen, iv, ivlen, NULL, 0, pResult, NULL);
68 }
69
70 /**
71 * @brief 3DES-CBC decrypt
72 * @param message : input buffer
73 * @param msglen : input buffer length
74 * @param pResult : output result buffer
75 * @param iv : IV
76 * @param ivlen : IV length
77 * @retval 0 : SUCCESS <br>
78 * others : fail, refer to ERRNO
79 */
80 __weak HAL_ROM_TEXT_SECTION
rtl_crypto_3des_cbc_decrypt(IN const u8 * message,IN const u32 msglen,IN const u8 * iv,IN const u32 ivlen,OUT u8 * pResult)81 int rtl_crypto_3des_cbc_decrypt(
82 IN const u8* message, IN const u32 msglen,
83 IN const u8* iv, IN const u32 ivlen,
84 OUT u8* pResult)
85 {
86 HAL_CRYPTO_ADAPTER *pIE = &crypto_engine;
87
88 if ( iv == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
89 if ( (u32)(iv) & 0x3 ) return _ERRNO_CRYPTO_ADDR_NOT_4Byte_Aligned; // need to be 4-byte alignment
90 if ( ivlen > CRYPTO_MAX_KEY_LENGTH) return _ERRNO_CRYPTO_IV_OutRange;
91 if ( message == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
92 if ( pResult == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
93
94 return CRYPTO_CipherDecryptAD(pIE, message, msglen, iv, ivlen, NULL, 0, pResult, NULL);
95 }
96
97 /**
98 * @brief 3DES-ECB init, set key
99 * @param key : need to be 4 byte alignment
100 * @param keylen : key length <= 32
101 * @retval 0 : SUCCESS <br>
102 * others : fail, refer to ERRNO
103 */
104 __weak HAL_ROM_TEXT_SECTION
rtl_crypto_3des_ecb_init(IN const u8 * key,IN const u32 keylen)105 int rtl_crypto_3des_ecb_init(IN const u8* key, IN const u32 keylen)
106 {
107 HAL_CRYPTO_ADAPTER *pIE = &crypto_engine;
108
109 assert_param( (pIE != NULL) && (pIE->isInit == _TRUE) );
110 if ( key == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
111 if ( (u32)(key) & 0x3 ) return _ERRNO_CRYPTO_ADDR_NOT_4Byte_Aligned; // need to be 4-byte alignment
112 if ( keylen > CRYPTO_MAX_KEY_LENGTH ) return _ERRNO_CRYPTO_KEY_OutRange;
113
114 return CRYPTO_CipherInit(pIE, CIPHER_TYPE_3DES_ECB, key, keylen);
115 }
116
117 /**
118 * @brief 3DES-ECB encrypt
119 * @param message : input buffer
120 * @param msglen : input buffer length
121 * @param pResult : output result buffer
122 * @param iv : IV
123 * @param ivlen : IV length
124 * @retval 0 : SUCCESS <br>
125 * others : fail, refer to ERRNO
126 */
127 __weak HAL_ROM_TEXT_SECTION
rtl_crypto_3des_ecb_encrypt(IN const u8 * message,IN const u32 msglen,IN const u8 * iv,IN const u32 ivlen,OUT u8 * pResult)128 int rtl_crypto_3des_ecb_encrypt(
129 IN const u8* message, IN const u32 msglen,
130 IN const u8* iv, IN const u32 ivlen,
131 OUT u8* pResult)
132 {
133 HAL_CRYPTO_ADAPTER *pIE = &crypto_engine;
134
135 if ( message == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
136 if ( pResult == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
137
138 return CRYPTO_CipherEncryptAD(pIE, message, msglen, iv, ivlen, NULL, 0, pResult, NULL);
139 }
140
141 /**
142 * @brief 3DES-ECB decrypt
143 * @param message : input buffer
144 * @param msglen : input buffer length
145 * @param pResult : output result buffer
146 * @param iv : IV
147 * @param ivlen : IV length
148 * @retval 0 : SUCCESS <br>
149 * others : fail, refer to ERRNO
150 */
151 __weak HAL_ROM_TEXT_SECTION
rtl_crypto_3des_ecb_decrypt(IN const u8 * message,IN const u32 msglen,IN const u8 * iv,IN const u32 ivlen,OUT u8 * pResult)152 int rtl_crypto_3des_ecb_decrypt(
153 IN const u8* message, IN const u32 msglen,
154 IN const u8* iv, IN const u32 ivlen,
155 OUT u8* pResult)
156 {
157 HAL_CRYPTO_ADAPTER *pIE = &crypto_engine;
158
159 if ( message == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
160 if ( pResult == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
161
162 return CRYPTO_CipherDecryptAD(pIE, message, msglen, iv, ivlen, NULL, 0, pResult, NULL);
163 }
164
165 /**
166 * @brief 3DES-CFB init, set key
167 * @param key : need to be 4 byte alignment
168 * @param keylen : key length <= 32
169 * @retval 0 : SUCCESS <br>
170 * others : fail, refer to ERRNO
171 */
172 __weak HAL_ROM_TEXT_SECTION
rtl_crypto_3des_cfb_init(IN const u8 * key,IN const u32 keylen)173 int rtl_crypto_3des_cfb_init(IN const u8* key, IN const u32 keylen)
174 {
175 HAL_CRYPTO_ADAPTER *pIE = &crypto_engine;
176
177 assert_param( (pIE != NULL) && (pIE->isInit == _TRUE) );
178 if ( key == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
179 if ( (u32)(key) & 0x3 ) return _ERRNO_CRYPTO_ADDR_NOT_4Byte_Aligned; // need to be 4-byte alignment
180 if ( keylen > CRYPTO_MAX_KEY_LENGTH ) return _ERRNO_CRYPTO_KEY_OutRange;
181
182 return CRYPTO_CipherInit(pIE, CIPHER_TYPE_3DES_CFB, key, keylen);
183 }
184
185 /**
186 * @brief 3DES-CFB encrypt
187 * @param message : input buffer
188 * @param msglen : input buffer length
189 * @param pResult : output result buffer
190 * @param iv : IV
191 * @param ivlen : IV length
192 * @retval 0 : SUCCESS <br>
193 * others : fail, refer to ERRNO
194 */
195 __weak HAL_ROM_TEXT_SECTION
rtl_crypto_3des_cfb_encrypt(IN const u8 * message,IN const u32 msglen,IN const u8 * iv,IN const u32 ivlen,OUT u8 * pResult)196 int rtl_crypto_3des_cfb_encrypt(
197 IN const u8* message, IN const u32 msglen,
198 IN const u8* iv, IN const u32 ivlen,
199 OUT u8* pResult)
200 {
201 HAL_CRYPTO_ADAPTER *pIE = &crypto_engine;
202
203 if ( message == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
204 if ( pResult == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
205
206 return CRYPTO_CipherEncryptAD(pIE, message, msglen, iv, ivlen, NULL, 0, pResult, NULL);
207 }
208
209 /**
210 * @brief 3DES-CFB decrypt
211 * @param message : input buffer
212 * @param msglen : input buffer length
213 * @param pResult : output result buffer
214 * @param iv : IV
215 * @param ivlen : IV length
216 * @retval 0 : SUCCESS <br>
217 * others : fail, refer to ERRNO
218 */
219 __weak HAL_ROM_TEXT_SECTION
rtl_crypto_3des_cfb_decrypt(IN const u8 * message,IN const u32 msglen,IN const u8 * iv,IN const u32 ivlen,OUT u8 * pResult)220 int rtl_crypto_3des_cfb_decrypt(
221 IN const u8* message, IN const u32 msglen,
222 IN const u8* iv, IN const u32 ivlen,
223 OUT u8* pResult)
224 {
225 HAL_CRYPTO_ADAPTER *pIE = &crypto_engine;
226
227 if ( message == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
228 if ( pResult == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
229
230 return CRYPTO_CipherDecryptAD(pIE, message, msglen, iv, ivlen, NULL, 0, pResult, NULL);
231 }
232
233 /**
234 * @brief 3DES-OFB init, set key
235 * @param key : need to be 4 byte alignment
236 * @param keylen : key length <= 32
237 * @retval 0 : SUCCESS <br>
238 * others : fail, refer to ERRNO
239 */
240 __weak HAL_ROM_TEXT_SECTION
rtl_crypto_3des_ofb_init(IN const u8 * key,IN const u32 keylen)241 int rtl_crypto_3des_ofb_init(IN const u8* key, IN const u32 keylen)
242 {
243 HAL_CRYPTO_ADAPTER *pIE = &crypto_engine;
244
245 assert_param( (pIE != NULL) && (pIE->isInit == _TRUE) );
246 if ( key == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
247 if ( (u32)(key) & 0x3 ) return _ERRNO_CRYPTO_ADDR_NOT_4Byte_Aligned; // need to be 4-byte alignment
248 if ( keylen > CRYPTO_MAX_KEY_LENGTH ) return _ERRNO_CRYPTO_KEY_OutRange;
249
250 return CRYPTO_CipherInit(pIE, CIPHER_TYPE_3DES_OFB, key, keylen);
251 }
252
253 /**
254 * @brief 3DES-OFB encrypt
255 * @param message : input buffer
256 * @param msglen : input buffer length
257 * @param pResult : output result buffer
258 * @param iv : IV
259 * @param ivlen : IV length
260 * @retval 0 : SUCCESS <br>
261 * others : fail, refer to ERRNO
262 */
263 __weak HAL_ROM_TEXT_SECTION
rtl_crypto_3des_ofb_encrypt(IN const u8 * message,IN const u32 msglen,IN const u8 * iv,IN const u32 ivlen,OUT u8 * pResult)264 int rtl_crypto_3des_ofb_encrypt(
265 IN const u8* message, IN const u32 msglen,
266 IN const u8* iv, IN const u32 ivlen,
267 OUT u8* pResult)
268 {
269 HAL_CRYPTO_ADAPTER *pIE = &crypto_engine;
270
271 if ( message == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
272 if ( pResult == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
273
274 return CRYPTO_CipherEncryptAD(pIE, message, msglen, iv, ivlen, NULL, 0, pResult, NULL);
275 }
276
277 /**
278 * @brief 3DES-OFB decrypt
279 * @param message : input buffer
280 * @param msglen : input buffer length
281 * @param pResult : output result buffer
282 * @param iv : IV
283 * @param ivlen : IV length
284 * @retval 0 : SUCCESS <br>
285 * others : fail, refer to ERRNO
286 */
287 __weak HAL_ROM_TEXT_SECTION
rtl_crypto_3des_ofb_decrypt(IN const u8 * message,IN const u32 msglen,IN const u8 * iv,IN const u32 ivlen,OUT u8 * pResult)288 int rtl_crypto_3des_ofb_decrypt(
289 IN const u8* message, IN const u32 msglen,
290 IN const u8* iv, IN const u32 ivlen,
291 OUT u8* pResult)
292 {
293 HAL_CRYPTO_ADAPTER *pIE = &crypto_engine;
294
295 if ( message == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
296 if ( pResult == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
297
298 return CRYPTO_CipherDecryptAD(pIE, message, msglen, iv, ivlen, NULL, 0, pResult, NULL);
299 }
300
301 /**
302 * @brief 3DES-CTR init, set key
303 * @param key : need to be 4 byte alignment
304 * @param keylen : key length <= 32
305 * @retval 0 : SUCCESS <br>
306 * others : fail, refer to ERRNO
307 */
308 __weak HAL_ROM_TEXT_SECTION
rtl_crypto_3des_ctr_init(IN const u8 * key,IN const u32 keylen)309 int rtl_crypto_3des_ctr_init(IN const u8* key, IN const u32 keylen)
310 {
311 HAL_CRYPTO_ADAPTER *pIE = &crypto_engine;
312
313 assert_param( (pIE != NULL) && (pIE->isInit == _TRUE) );
314 if ( key == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
315 if ( (u32)(key) & 0x3 ) return _ERRNO_CRYPTO_ADDR_NOT_4Byte_Aligned; // need to be 4-byte alignment
316 if ( keylen > CRYPTO_MAX_KEY_LENGTH ) return _ERRNO_CRYPTO_KEY_OutRange;
317
318 return CRYPTO_CipherInit(pIE, CIPHER_TYPE_3DES_CTR, key, keylen);
319 }
320
321 /**
322 * @brief 3DES-CTR encrypt
323 * @param message : input buffer
324 * @param msglen : input buffer length
325 * @param pResult : output result buffer
326 * @param iv : IV
327 * @param ivlen : IV length
328 * @retval 0 : SUCCESS <br>
329 * others : fail, refer to ERRNO
330 */
331 __weak HAL_ROM_TEXT_SECTION
rtl_crypto_3des_ctr_encrypt(IN const u8 * message,IN const u32 msglen,IN const u8 * iv,IN const u32 ivlen,OUT u8 * pResult)332 int rtl_crypto_3des_ctr_encrypt(
333 IN const u8* message, IN const u32 msglen,
334 IN const u8* iv, IN const u32 ivlen,
335 OUT u8* pResult)
336 {
337 HAL_CRYPTO_ADAPTER *pIE = &crypto_engine;
338
339 if ( message == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
340 if ( pResult == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
341
342 return CRYPTO_CipherEncryptAD(pIE, message, msglen, iv, ivlen, NULL, 0, pResult, NULL);
343 }
344
345 /**
346 * @brief 3DES-CTR decrypt
347 * @param message : input buffer
348 * @param msglen : input buffer length
349 * @param pResult : output result buffer
350 * @param iv : IV
351 * @param ivlen : IV length
352 * @retval 0 : SUCCESS <br>
353 * others : fail, refer to ERRNO
354 */
355 __weak HAL_ROM_TEXT_SECTION
rtl_crypto_3des_ctr_decrypt(IN const u8 * message,IN const u32 msglen,IN const u8 * iv,IN const u32 ivlen,OUT u8 * pResult)356 int rtl_crypto_3des_ctr_decrypt(
357 IN const u8* message, IN const u32 msglen,
358 IN const u8* iv, IN const u32 ivlen,
359 OUT u8* pResult)
360 {
361 HAL_CRYPTO_ADAPTER *pIE = &crypto_engine;
362
363 if ( message == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
364 if ( pResult == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
365
366 return CRYPTO_CipherDecryptAD(pIE, message, msglen, iv, ivlen, NULL, 0, pResult, NULL);
367 }
368
369 /**
370 * @brief DES-CBC init, set key
371 * @param key : need to be 4 byte alignment
372 * @param keylen : key length <= 32
373 * @retval 0 : SUCCESS <br>
374 * others : fail, refer to ERRNO
375 */
376 __weak HAL_ROM_TEXT_SECTION
rtl_crypto_des_cbc_init(IN const u8 * key,IN const u32 keylen)377 int rtl_crypto_des_cbc_init(IN const u8* key, IN const u32 keylen)
378 {
379 HAL_CRYPTO_ADAPTER *pIE = &crypto_engine;
380
381 assert_param( (pIE != NULL) && (pIE->isInit == _TRUE) );
382 if ( key == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
383 if ( (u32)(key) & 0x3 ) return _ERRNO_CRYPTO_ADDR_NOT_4Byte_Aligned; // need to be 4-byte alignment
384 if ( keylen > CRYPTO_MAX_KEY_LENGTH ) return _ERRNO_CRYPTO_KEY_OutRange;
385
386 return CRYPTO_CipherInit(pIE, CIPHER_TYPE_DES_CBC, key, keylen);
387 }
388
389 /**
390 * @brief DES-CBC encrypt
391 * @param message : input buffer
392 * @param msglen : input buffer length
393 * @param pResult : output result buffer
394 * @param iv : IV
395 * @param ivlen : IV length
396 * @retval 0 : SUCCESS <br>
397 * others : fail, refer to ERRNO
398 */
399 __weak HAL_ROM_TEXT_SECTION
rtl_crypto_des_cbc_encrypt(IN const u8 * message,IN const u32 msglen,IN const u8 * iv,IN const u32 ivlen,OUT u8 * pResult)400 int rtl_crypto_des_cbc_encrypt(
401 IN const u8* message, IN const u32 msglen,
402 IN const u8* iv, IN const u32 ivlen,
403 OUT u8* pResult)
404 {
405 HAL_CRYPTO_ADAPTER *pIE = &crypto_engine;
406
407 if ( iv == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
408 if ( (u32)(iv) & 0x3 ) return _ERRNO_CRYPTO_ADDR_NOT_4Byte_Aligned; // need to be 4-byte alignment
409 if ( ivlen > CRYPTO_MAX_KEY_LENGTH) return _ERRNO_CRYPTO_IV_OutRange;
410 if ( ivlen != pIE->lenCipherKey ) return _ERRNO_CRYPTO_KEY_IV_LEN_DIFF;
411 if ( message == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
412 if ( pResult == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
413
414 return CRYPTO_CipherEncryptAD(pIE, message, msglen, iv, ivlen, NULL, 0, pResult, NULL);
415 }
416
417 /**
418 * @brief DES-CBC decrypt
419 * @param message : input buffer
420 * @param msglen : input buffer length
421 * @param pResult : output result buffer
422 * @param iv : IV
423 * @param ivlen : IV length
424 * @retval 0 : SUCCESS <br>
425 * others : fail, refer to ERRNO
426 */
427 __weak HAL_ROM_TEXT_SECTION
rtl_crypto_des_cbc_decrypt(IN const u8 * message,IN const u32 msglen,IN const u8 * iv,IN const u32 ivlen,OUT u8 * pResult)428 int rtl_crypto_des_cbc_decrypt(
429 IN const u8* message, IN const u32 msglen,
430 IN const u8* iv, IN const u32 ivlen,
431 OUT u8* pResult)
432 {
433 HAL_CRYPTO_ADAPTER *pIE = &crypto_engine;
434
435 if ( iv == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
436 if ( (u32)(iv) & 0x3 ) return _ERRNO_CRYPTO_ADDR_NOT_4Byte_Aligned; // need to be 4-byte alignment
437 if ( ivlen > CRYPTO_MAX_KEY_LENGTH) return _ERRNO_CRYPTO_IV_OutRange;
438 if ( ivlen != pIE->lenCipherKey ) return _ERRNO_CRYPTO_KEY_IV_LEN_DIFF;
439 if ( message == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
440 if ( pResult == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
441
442 return CRYPTO_CipherDecryptAD(pIE, message, msglen, iv, ivlen, NULL, 0, pResult, NULL);
443 }
444
445 /**
446 * @brief DES-ECB init, set key
447 * @param key : need to be 4 byte alignment
448 * @param keylen : key length <= 32
449 * @retval 0 : SUCCESS <br>
450 * others : fail, refer to ERRNO
451 */
452 __weak HAL_ROM_TEXT_SECTION
rtl_crypto_des_ecb_init(IN const u8 * key,IN const u32 keylen)453 int rtl_crypto_des_ecb_init(IN const u8* key, IN const u32 keylen)
454 {
455 HAL_CRYPTO_ADAPTER *pIE = &crypto_engine;
456
457 assert_param( (pIE != NULL) && (pIE->isInit == _TRUE) );
458 if ( key == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
459 if ( (u32)(key) & 0x3 ) return _ERRNO_CRYPTO_ADDR_NOT_4Byte_Aligned; // need to be 4-byte alignment
460 if ( keylen > CRYPTO_MAX_KEY_LENGTH ) return _ERRNO_CRYPTO_KEY_OutRange;
461
462 return CRYPTO_CipherInit(pIE, CIPHER_TYPE_DES_ECB, key, keylen);
463 }
464
465 /**
466 * @brief DES-ECB encrypt
467 * @param message : input buffer
468 * @param msglen : input buffer length
469 * @param pResult : output result buffer
470 * @param iv : IV
471 * @param ivlen : IV length
472 * @retval 0 : SUCCESS <br>
473 * others : fail, refer to ERRNO
474 */
475 __weak HAL_ROM_TEXT_SECTION
rtl_crypto_des_ecb_encrypt(IN const u8 * message,IN const u32 msglen,IN const u8 * iv,IN const u32 ivlen,OUT u8 * pResult)476 int rtl_crypto_des_ecb_encrypt(
477 IN const u8* message, IN const u32 msglen,
478 IN const u8* iv, IN const u32 ivlen,
479 OUT u8* pResult)
480 {
481 HAL_CRYPTO_ADAPTER *pIE = &crypto_engine;
482
483 if ( message == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
484 if ( pResult == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
485
486 return CRYPTO_CipherEncryptAD(pIE, message, msglen, iv, ivlen, NULL, 0, pResult, NULL);
487 }
488
489 /**
490 * @brief DES-ECB decrypt
491 * @param message : input buffer
492 * @param msglen : input buffer length
493 * @param pResult : output result buffer
494 * @param iv : IV
495 * @param ivlen : IV length
496 * @retval 0 : SUCCESS <br>
497 * others : fail, refer to ERRNO
498 */
499 __weak HAL_ROM_TEXT_SECTION
rtl_crypto_des_ecb_decrypt(IN const u8 * message,IN const u32 msglen,IN const u8 * iv,IN const u32 ivlen,OUT u8 * pResult)500 int rtl_crypto_des_ecb_decrypt(
501 IN const u8* message, IN const u32 msglen,
502 IN const u8* iv, IN const u32 ivlen,
503 OUT u8* pResult)
504 {
505 HAL_CRYPTO_ADAPTER *pIE = &crypto_engine;
506
507 if ( message == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
508 if ( pResult == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
509
510 return CRYPTO_CipherDecryptAD(pIE, message, msglen, iv, ivlen, NULL, 0, pResult, NULL);
511 }
512
513 /**
514 * @brief DES-CFB init, set key
515 * @param key : need to be 4 byte alignment
516 * @param keylen : key length <= 32
517 * @retval 0 : SUCCESS <br>
518 * others : fail, refer to ERRNO
519 */
520 __weak HAL_ROM_TEXT_SECTION
rtl_crypto_des_cfb_init(IN const u8 * key,IN const u32 keylen)521 int rtl_crypto_des_cfb_init(IN const u8* key, IN const u32 keylen)
522 {
523 HAL_CRYPTO_ADAPTER *pIE = &crypto_engine;
524
525 assert_param( (pIE != NULL) && (pIE->isInit == _TRUE) );
526 if ( key == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
527 if ( (u32)(key) & 0x3 ) return _ERRNO_CRYPTO_ADDR_NOT_4Byte_Aligned; // need to be 4-byte alignment
528 if ( keylen > CRYPTO_MAX_KEY_LENGTH ) return _ERRNO_CRYPTO_KEY_OutRange;
529
530 return CRYPTO_CipherInit(pIE, CIPHER_TYPE_DES_CFB, key, keylen);
531 }
532
533 /**
534 * @brief DES-CFB encrypt
535 * @param message : input buffer
536 * @param msglen : input buffer length
537 * @param pResult : output result buffer
538 * @param iv : IV
539 * @param ivlen : IV length
540 * @retval 0 : SUCCESS <br>
541 * others : fail, refer to ERRNO
542 */
543 __weak HAL_ROM_TEXT_SECTION
rtl_crypto_des_cfb_encrypt(IN const u8 * message,IN const u32 msglen,IN const u8 * iv,IN const u32 ivlen,OUT u8 * pResult)544 int rtl_crypto_des_cfb_encrypt(
545 IN const u8* message, IN const u32 msglen,
546 IN const u8* iv, IN const u32 ivlen,
547 OUT u8* pResult)
548 {
549 HAL_CRYPTO_ADAPTER *pIE = &crypto_engine;
550
551 if ( message == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
552 if ( pResult == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
553
554 return CRYPTO_CipherEncryptAD(pIE, message, msglen, iv, ivlen, NULL, 0, pResult, NULL);
555 }
556
557 /**
558 * @brief DES-CFB decrypt
559 * @param message : input buffer
560 * @param msglen : input buffer length
561 * @param pResult : output result buffer
562 * @param iv : IV
563 * @param ivlen : IV length
564 * @retval 0 : SUCCESS <br>
565 * others : fail, refer to ERRNO
566 */
567 __weak HAL_ROM_TEXT_SECTION
rtl_crypto_des_cfb_decrypt(IN const u8 * message,IN const u32 msglen,IN const u8 * iv,IN const u32 ivlen,OUT u8 * pResult)568 int rtl_crypto_des_cfb_decrypt(
569 IN const u8* message, IN const u32 msglen,
570 IN const u8* iv, IN const u32 ivlen,
571 OUT u8* pResult)
572 {
573 HAL_CRYPTO_ADAPTER *pIE = &crypto_engine;
574
575 if ( message == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
576 if ( pResult == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
577
578 return CRYPTO_CipherDecryptAD(pIE, message, msglen, iv, ivlen, NULL, 0, pResult, NULL);
579 }
580
581 /**
582 * @brief DES-OFB init, set key
583 * @param key : need to be 4 byte alignment
584 * @param keylen : key length <= 32
585 * @retval 0 : SUCCESS <br>
586 * others : fail, refer to ERRNO
587 */
588 __weak HAL_ROM_TEXT_SECTION
rtl_crypto_des_ofb_init(IN const u8 * key,IN const u32 keylen)589 int rtl_crypto_des_ofb_init(IN const u8* key, IN const u32 keylen)
590 {
591 HAL_CRYPTO_ADAPTER *pIE = &crypto_engine;
592
593 assert_param( (pIE != NULL) && (pIE->isInit == _TRUE) );
594 if ( key == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
595 if ( (u32)(key) & 0x3 ) return _ERRNO_CRYPTO_ADDR_NOT_4Byte_Aligned; // need to be 4-byte alignment
596 if ( keylen > CRYPTO_MAX_KEY_LENGTH ) return _ERRNO_CRYPTO_KEY_OutRange;
597
598 return CRYPTO_CipherInit(pIE, CIPHER_TYPE_DES_OFB, key, keylen);
599 }
600
601 /**
602 * @brief DES-OFB encrypt
603 * @param message : input buffer
604 * @param msglen : input buffer length
605 * @param pResult : output result buffer
606 * @param iv : IV
607 * @param ivlen : IV length
608 * @retval 0 : SUCCESS <br>
609 * others : fail, refer to ERRNO
610 */
611 __weak HAL_ROM_TEXT_SECTION
rtl_crypto_des_ofb_encrypt(IN const u8 * message,IN const u32 msglen,IN const u8 * iv,IN const u32 ivlen,OUT u8 * pResult)612 int rtl_crypto_des_ofb_encrypt(
613 IN const u8* message, IN const u32 msglen,
614 IN const u8* iv, IN const u32 ivlen,
615 OUT u8* pResult)
616 {
617 HAL_CRYPTO_ADAPTER *pIE = &crypto_engine;
618
619 if ( message == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
620 if ( pResult == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
621
622 return CRYPTO_CipherEncryptAD(pIE, message, msglen, iv, ivlen, NULL, 0, pResult, NULL);
623 }
624
625 /**
626 * @brief DES-OFB decrypt
627 * @param message : input buffer
628 * @param msglen : input buffer length
629 * @param pResult : output result buffer
630 * @param iv : IV
631 * @param ivlen : IV length
632 * @retval 0 : SUCCESS <br>
633 * others : fail, refer to ERRNO
634 */
635 __weak HAL_ROM_TEXT_SECTION
rtl_crypto_des_ofb_decrypt(IN const u8 * message,IN const u32 msglen,IN const u8 * iv,IN const u32 ivlen,OUT u8 * pResult)636 int rtl_crypto_des_ofb_decrypt(
637 IN const u8* message, IN const u32 msglen,
638 IN const u8* iv, IN const u32 ivlen,
639 OUT u8* pResult)
640 {
641 HAL_CRYPTO_ADAPTER *pIE = &crypto_engine;
642
643 if ( message == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
644 if ( pResult == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
645
646 return CRYPTO_CipherDecryptAD(pIE, message, msglen, iv, ivlen, NULL, 0, pResult, NULL);
647 }
648
649 /**
650 * @brief DES-CTR init, set key
651 * @param key : need to be 4 byte alignment
652 * @param keylen : key length <= 32
653 * @retval 0 : SUCCESS <br>
654 * others : fail, refer to ERRNO
655 */
656 __weak HAL_ROM_TEXT_SECTION
rtl_crypto_des_ctr_init(IN const u8 * key,IN const u32 keylen)657 int rtl_crypto_des_ctr_init(IN const u8* key, IN const u32 keylen)
658 {
659 HAL_CRYPTO_ADAPTER *pIE = &crypto_engine;
660
661 assert_param( (pIE != NULL) && (pIE->isInit == _TRUE) );
662 if ( key == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
663 if ( (u32)(key) & 0x3 ) return _ERRNO_CRYPTO_ADDR_NOT_4Byte_Aligned; // need to be 4-byte alignment
664 if ( keylen > CRYPTO_MAX_KEY_LENGTH ) return _ERRNO_CRYPTO_KEY_OutRange;
665
666 return CRYPTO_CipherInit(pIE, CIPHER_TYPE_DES_CTR, key, keylen);
667 }
668
669 /**
670 * @brief DES-CTR encrypt
671 * @param message : input buffer
672 * @param msglen : input buffer length
673 * @param pResult : output result buffer
674 * @param iv : IV
675 * @param ivlen : IV length
676 * @retval 0 : SUCCESS <br>
677 * others : fail, refer to ERRNO
678 */
679 __weak HAL_ROM_TEXT_SECTION
rtl_crypto_des_ctr_encrypt(IN const u8 * message,IN const u32 msglen,IN const u8 * iv,IN const u32 ivlen,OUT u8 * pResult)680 int rtl_crypto_des_ctr_encrypt(
681 IN const u8* message, IN const u32 msglen,
682 IN const u8* iv, IN const u32 ivlen,
683 OUT u8* pResult)
684 {
685 HAL_CRYPTO_ADAPTER *pIE = &crypto_engine;
686
687 if ( message == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
688 if ( pResult == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
689
690 return CRYPTO_CipherEncryptAD(pIE, message, msglen, iv, ivlen, NULL, 0, pResult, NULL);
691 }
692
693 /**
694 * @brief DES-CTR decrypt
695 * @param message : input buffer
696 * @param msglen : input buffer length
697 * @param pResult : output result buffer
698 * @param iv : IV
699 * @param ivlen : IV length
700 * @retval 0 : SUCCESS <br>
701 * others : fail, refer to ERRNO
702 */
703 __weak HAL_ROM_TEXT_SECTION
rtl_crypto_des_ctr_decrypt(IN const u8 * message,IN const u32 msglen,IN const u8 * iv,IN const u32 ivlen,OUT u8 * pResult)704 int rtl_crypto_des_ctr_decrypt(
705 IN const u8* message, IN const u32 msglen,
706 IN const u8* iv, IN const u32 ivlen,
707 OUT u8* pResult)
708 {
709 HAL_CRYPTO_ADAPTER *pIE = &crypto_engine;
710
711 if ( message == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
712 if ( pResult == NULL ) return _ERRNO_CRYPTO_NULL_POINTER;
713
714 return CRYPTO_CipherDecryptAD(pIE, message, msglen, iv, ivlen, NULL, 0, pResult, NULL);
715 }
716 /******************* (C) COPYRIGHT 2016 Realtek Semiconductor *****END OF FILE****/
717