1 /* port/ti/ti-aes.c
2  *
3  * Copyright (C) 2006-2021 wolfSSL Inc.
4  *
5  * This file is part of wolfSSL.
6  *
7  * wolfSSL is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * wolfSSL is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
20  */
21 
22 
23 #ifdef HAVE_CONFIG_H
24     #include <config.h>
25 #endif
26 
27 #include <wolfssl/wolfcrypt/settings.h>
28 
29 #ifndef NO_AES
30 
31 
32 #if defined(WOLFSSL_TI_CRYPT)
33 #include <stdbool.h>
34 #include <stdint.h>
35 
36 #include <wolfssl/wolfcrypt/aes.h>
37 #include <wolfssl/wolfcrypt/error-crypt.h>
38 #include <wolfssl/wolfcrypt/port/ti/ti-ccm.h>
39 
40 #include "inc/hw_aes.h"
41 #include "inc/hw_memmap.h"
42 #include "inc/hw_ints.h"
43 #include "driverlib/aes.h"
44 #include "driverlib/sysctl.h"
45 #include "driverlib/rom_map.h"
46 #include "driverlib/rom.h"
47 
AesSetIV(Aes * aes,const byte * iv)48 static int  AesSetIV(Aes* aes, const byte* iv)
49 {
50     if (aes == NULL)
51         return BAD_FUNC_ARG;
52 
53     if (iv)
54         XMEMCPY(aes->reg, iv, AES_BLOCK_SIZE);
55     else
56         XMEMSET(aes->reg,  0, AES_BLOCK_SIZE);
57 
58     return 0;
59 }
60 
wc_AesSetKey(Aes * aes,const byte * key,word32 len,const byte * iv,int dir)61 WOLFSSL_API int  wc_AesSetKey(Aes* aes, const byte* key, word32 len, const byte* iv,
62                           int dir)
63 {
64     if(!wolfSSL_TI_CCMInit())return 1 ;
65     if ((aes == NULL) || (key == NULL) || (iv == NULL))
66         return BAD_FUNC_ARG;
67     if(!((dir == AES_ENCRYPTION) || (dir == AES_DECRYPTION)))
68         return BAD_FUNC_ARG;
69 
70     switch(len) {
71     case 16: aes->keylen = AES_CFG_KEY_SIZE_128BIT ; break ;
72     case 24: aes->keylen = AES_CFG_KEY_SIZE_192BIT ; break ;
73     case 32: aes->keylen = AES_CFG_KEY_SIZE_256BIT ; break ;
74     default: return BAD_FUNC_ARG;
75     }
76 
77     XMEMCPY(aes->key, key, len) ;
78     #ifdef WOLFSSL_AES_COUNTER
79     aes->left = 0;
80     #endif /* WOLFSSL_AES_COUNTER */
81     return AesSetIV(aes, iv);
82 }
83 
84 #define AES_CFG_MODE_CTR_NOCTR AES_CFG_MODE_CTR+100
85 #define IS_ALIGN16(p) (((unsigned int)(p)&0xf) == 0)
86 
AesAlign16(Aes * aes,byte * out,const byte * in,word32 sz,word32 dir,word32 mode)87 static int  AesAlign16(Aes* aes, byte* out, const byte* in, word32 sz, word32 dir, word32 mode)
88 {
89     wolfSSL_TI_lockCCM() ;
90     ROM_AESReset(AES_BASE);
91     ROM_AESConfigSet(AES_BASE, (aes->keylen | dir |
92                      (mode==AES_CFG_MODE_CTR_NOCTR ? AES_CFG_MODE_CTR : mode)));
93     ROM_AESIVSet(AES_BASE, (uint32_t *)aes->reg);
94     ROM_AESKey1Set(AES_BASE, (uint32_t *)aes->key, aes->keylen);
95     if((dir == AES_CFG_DIR_DECRYPT)&& (mode == AES_CFG_MODE_CBC))
96         /* if input and output same will overwrite input iv */
97         XMEMCPY(aes->tmp, in + sz - AES_BLOCK_SIZE, AES_BLOCK_SIZE);
98     ROM_AESDataProcess(AES_BASE, (uint32_t *)in, (uint32_t *)out, sz);
99     wolfSSL_TI_unlockCCM() ;
100 
101     /* store iv for next call */
102     if(mode == AES_CFG_MODE_CBC){
103         if(dir == AES_CFG_DIR_ENCRYPT)
104             XMEMCPY(aes->reg, out + sz - AES_BLOCK_SIZE, AES_BLOCK_SIZE);
105         else
106             XMEMCPY(aes->reg, aes->tmp, AES_BLOCK_SIZE);
107     }
108 
109     if(mode == AES_CFG_MODE_CTR) {
110         do {
111             int i ;
112             for (i = AES_BLOCK_SIZE - 1; i >= 0; i--) {
113                  if (++((byte *)aes->reg)[i])
114                      break ;
115             }
116             sz -= AES_BLOCK_SIZE ;
117         } while((int)sz > 0) ;
118     }
119 
120     return 0 ;
121 }
122 
AesProcess(Aes * aes,byte * out,const byte * in,word32 sz,word32 dir,word32 mode)123 static int  AesProcess(Aes* aes, byte* out, const byte* in, word32 sz, word32 dir, word32 mode)
124 {
125     const byte * in_p ; byte * out_p ;
126     word32 size ;
127     #define TI_BUFFSIZE 1024
128     byte buff[TI_BUFFSIZE] ;
129 
130     if ((aes == NULL) || (in == NULL) || (out == NULL))
131         return BAD_FUNC_ARG;
132     if(sz % AES_BLOCK_SIZE)
133         return BAD_FUNC_ARG;
134 
135     while(sz > 0) {
136         size = sz ; in_p = in ; out_p = out ;
137         if(!IS_ALIGN16(in)){
138             size = sz>TI_BUFFSIZE ? TI_BUFFSIZE : sz ;
139             XMEMCPY(buff, in, size) ;
140             in_p = (const byte *)buff ;
141         }
142         if(!IS_ALIGN16(out)){
143             size = sz>TI_BUFFSIZE ? TI_BUFFSIZE : sz ;
144             out_p = buff ;
145         }
146 
147         AesAlign16(aes, out_p, in_p, size, dir, mode) ;
148 
149         if(!IS_ALIGN16(out)){
150             XMEMCPY(out, buff, size) ;
151         }
152         sz -= size ; in += size ; out += size ;
153     }
154 
155     return 0 ;
156 }
157 
wc_AesCbcEncrypt(Aes * aes,byte * out,const byte * in,word32 sz)158 WOLFSSL_API int  wc_AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
159 {
160     return AesProcess(aes, out, in, sz, AES_CFG_DIR_ENCRYPT, AES_CFG_MODE_CBC) ;
161 }
162 
wc_AesCbcDecrypt(Aes * aes,byte * out,const byte * in,word32 sz)163 WOLFSSL_API int  wc_AesCbcDecrypt(Aes* aes, byte* out, const byte* in, word32 sz)
164 {
165     return AesProcess(aes, out, in, sz, AES_CFG_DIR_DECRYPT, AES_CFG_MODE_CBC) ;
166 }
167 
168 #ifdef WOLFSSL_AES_COUNTER
wc_AesCtrEncrypt(Aes * aes,byte * out,const byte * in,word32 sz)169 WOLFSSL_API void wc_AesCtrEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
170 {
171             char out_block[AES_BLOCK_SIZE] ;
172             int odd ;
173             int even ;
174             char *tmp ; /* (char *)aes->tmp, for short */
175 
176             tmp = (char *)aes->tmp ;
177             if(aes->left) {
178                 if((aes->left + sz) >= AES_BLOCK_SIZE){
179                     odd = AES_BLOCK_SIZE - aes->left ;
180                 } else {
181                     odd = sz ;
182                 }
183                 XMEMCPY(tmp+aes->left, in, odd) ;
184                 if((odd+aes->left) == AES_BLOCK_SIZE){
185                     AesProcess(aes, (byte *)out_block, (byte const *)tmp, AES_BLOCK_SIZE,
186                              AES_CFG_DIR_ENCRYPT, AES_CFG_MODE_CTR) ;
187                     XMEMCPY(out, out_block+aes->left, odd) ;
188                     aes->left = 0 ;
189                     XMEMSET(tmp, 0x0, AES_BLOCK_SIZE) ;
190                 }
191                 in += odd ;
192                 out+= odd ;
193                 sz -= odd ;
194             }
195             odd = sz % AES_BLOCK_SIZE ;  /* if there is tail flagment */
196             if(sz / AES_BLOCK_SIZE) {
197                 even = (sz/AES_BLOCK_SIZE)*AES_BLOCK_SIZE ;
198                 AesProcess(aes, out, in, even, AES_CFG_DIR_ENCRYPT, AES_CFG_MODE_CTR);
199                 out += even ;
200                 in  += even ;
201             }
202             if(odd) {
203                 XMEMSET(tmp+aes->left, 0x0, AES_BLOCK_SIZE - aes->left) ;
204                 XMEMCPY(tmp+aes->left, in, odd) ;
205                 AesProcess(aes, (byte *)out_block, (byte const *)tmp, AES_BLOCK_SIZE,
206                            AES_CFG_DIR_ENCRYPT,
207                            AES_CFG_MODE_CTR_NOCTR /* Counter mode without counting IV */
208                            );
209                 XMEMCPY(out, out_block+aes->left,odd) ;
210                 aes->left += odd ;
211             }
212 }
213 #endif
214 
215 /* AES-DIRECT */
216 #if defined(WOLFSSL_AES_DIRECT)
wc_AesEncryptDirect(Aes * aes,byte * out,const byte * in)217 WOLFSSL_API void wc_AesEncryptDirect(Aes* aes, byte* out, const byte* in)
218 {
219     AesProcess(aes, out, in, AES_BLOCK_SIZE, AES_CFG_DIR_ENCRYPT, AES_CFG_MODE_CBC) ;
220 }
wc_AesDecryptDirect(Aes * aes,byte * out,const byte * in)221 WOLFSSL_API void wc_AesDecryptDirect(Aes* aes, byte* out, const byte* in)
222 {
223     AesProcess(aes, out, in, AES_BLOCK_SIZE, AES_CFG_DIR_DECRYPT, AES_CFG_MODE_CBC) ;
224 }
wc_AesSetKeyDirect(Aes * aes,const byte * key,word32 len,const byte * iv,int dir)225 WOLFSSL_API int wc_AesSetKeyDirect(Aes* aes, const byte* key, word32 len,
226                                      const byte* iv, int dir)
227 {
228      return(wc_AesSetKey(aes, key, len, iv, dir)) ;
229 }
230 #endif
231 
232 
233 #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
234 
AesAuthSetKey(Aes * aes,const byte * key,word32 keySz)235 static int  AesAuthSetKey(Aes* aes, const byte* key, word32 keySz)
236 {
237     byte nonce[AES_BLOCK_SIZE];
238 
239     if ((aes == NULL) || (key == NULL))
240         return BAD_FUNC_ARG ;
241     if (!((keySz == 16) || (keySz == 24) || (keySz == 32)))
242         return BAD_FUNC_ARG ;
243 
244     XMEMSET(nonce, 0, sizeof(nonce));
245     return wc_AesSetKey(aes, key, keySz, nonce, AES_ENCRYPTION);
246 }
247 
248 
AesAuthArgCheck(Aes * aes,byte * out,const byte * in,word32 inSz,const byte * nonce,word32 nonceSz,const byte * authTag,word32 authTagSz,const byte * authIn,word32 authInSz,word32 * M,word32 * L)249 static int AesAuthArgCheck(Aes* aes, byte* out, const byte* in, word32 inSz,
250                               const byte* nonce, word32 nonceSz,
251                               const byte* authTag, word32 authTagSz,
252                               const byte* authIn, word32 authInSz, word32 *M,  word32 *L)
253 {
254     (void) authInSz ;
255     if((aes == NULL)||(nonce == NULL)||(authTag== NULL)||(authIn == NULL))
256         return BAD_FUNC_ARG;
257     if((inSz != 0) && ((out == NULL)||(in == NULL)))
258         return BAD_FUNC_ARG;
259 
260     switch(authTagSz){
261     case  4:
262         *M = AES_CFG_CCM_M_4; break ;
263     case 6:
264         *M = AES_CFG_CCM_M_6; break ;
265     case 8:
266         *M = AES_CFG_CCM_M_8; break ;
267     case 10:
268         *M = AES_CFG_CCM_M_10; break ;
269     case 12:
270         *M = AES_CFG_CCM_M_12; break ;
271     case 14:
272         *M = AES_CFG_CCM_M_14; break ;
273     case 16:
274         *M = AES_CFG_CCM_M_16; break ;
275     default:
276         return 1 ;
277     }
278 
279     switch(nonceSz){
280     case 7:
281         *L = AES_CFG_CCM_L_8; break ;
282     case 8:
283         *L = AES_CFG_CCM_L_7; break ;
284     case 9:
285         *L = AES_CFG_CCM_L_6; break ;
286     case  10:
287         *L = AES_CFG_CCM_L_5; break ;
288     case 11:
289         *L = AES_CFG_CCM_L_4; break ;
290     case 12:
291         *L = AES_CFG_CCM_L_3; break ;
292     case 13:
293         *L = AES_CFG_CCM_L_2; break ;
294     case 14:
295         *L = AES_CFG_CCM_L_1; break ;
296     default:
297         return 1;
298     }
299     return 0 ;
300 }
301 
AesAuthSetIv(Aes * aes,const byte * nonce,word32 len,word32 L,int mode)302 static void AesAuthSetIv(Aes *aes, const byte *nonce, word32 len, word32 L, int mode) {
303 
304   if(mode == AES_CFG_MODE_CCM){
305     XMEMSET(aes->reg, 0, 16) ;
306     switch(L){
307     case AES_CFG_CCM_L_8:
308       aes->reg[0] = 0x7; break ;
309     case AES_CFG_CCM_L_7:
310       aes->reg[0] = 0x6; break ;
311     case AES_CFG_CCM_L_6:
312       aes->reg[0] = 0x5; break ;
313     case AES_CFG_CCM_L_5:
314       aes->reg[0] = 0x4; break ;
315     case AES_CFG_CCM_L_4:
316       aes->reg[0] = 0x3; break ;
317     case AES_CFG_CCM_L_3:
318       aes->reg[0] = 0x2; break ;
319     case AES_CFG_CCM_L_2:
320       aes->reg[0] = 0x1; break ;
321     case AES_CFG_CCM_L_1:
322       aes->reg[0] = 0x0; break ;
323     }
324     XMEMCPY(((byte *)aes->reg)+1, nonce, len) ;
325   } else {
326     byte *b = (byte *)aes->reg ;
327     XMEMSET(aes->reg, 0, AES_BLOCK_SIZE);
328     XMEMCPY(aes->reg, nonce, len);
329     b[AES_BLOCK_SIZE-4] = 0 ;
330     b[AES_BLOCK_SIZE-3] = 0 ;
331     b[AES_BLOCK_SIZE-2] = 0 ;
332     b[AES_BLOCK_SIZE-1] = 1 ;
333   }
334 }
335 
336 #define RoundUp16(n) ((n+15)&0xfffffff0)
337 #define FREE_ALL \
338     if(in_save)    XFREE(in_save, NULL, DYNAMIC_TYPE_TMP_BUFFER);\
339     if(out_save)   XFREE(out_save, NULL, DYNAMIC_TYPE_TMP_BUFFER);\
340     if(authIn_save)XFREE(authIn_save, NULL, DYNAMIC_TYPE_TMP_BUFFER);\
341     if(nonce_save) XFREE(nonce_save, NULL, DYNAMIC_TYPE_TMP_BUFFER);
342 
AesAuthEncrypt(Aes * aes,byte * out,const byte * in,word32 inSz,const byte * nonce,word32 nonceSz,byte * authTag,word32 authTagSz,const byte * authIn,word32 authInSz,int mode)343 static int AesAuthEncrypt(Aes* aes, byte* out, const byte* in, word32 inSz,
344                               const byte* nonce, word32 nonceSz,
345                               byte* authTag, word32 authTagSz,
346                               const byte* authIn, word32 authInSz, int mode)
347 {
348     word32 M, L ;
349     byte *in_a,     *in_save ;
350     byte *out_a,    *out_save ;
351     byte *authIn_a, *authIn_save ;
352     byte *nonce_a,    *nonce_save ;
353     word32 tmpTag[4] ;
354     int ret ;
355 
356     if(AesAuthArgCheck(aes, out, in, inSz, nonce, nonceSz, authTag, authTagSz, authIn, authInSz, &M, &L)
357        == BAD_FUNC_ARG)return BAD_FUNC_ARG ;
358 
359     /* 16 byte padding */
360     in_save = NULL ; out_save = NULL ; authIn_save = NULL ; nonce_save = NULL ;
361     if((inSz%16)==0){
362         in_save = NULL ; in_a = (byte *)in ;
363         out_save = NULL ; out_a = out ;
364     } else {
365       if((in_save = XMALLOC(RoundUp16(inSz), NULL, DYNAMIC_TYPE_TMP_BUFFER)) == NULL){
366           FREE_ALL; return MEMORY_E ; }
367       in_a = in_save ; XMEMSET(in_a, 0, RoundUp16(inSz)) ; XMEMCPY(in_a, in, inSz) ;
368 
369       if((out_save = XMALLOC(RoundUp16(inSz), NULL, DYNAMIC_TYPE_TMP_BUFFER)) == NULL){
370           FREE_ALL; return MEMORY_E ; }
371       out_a = out_save ;
372     }
373 
374     if((authInSz%16)==0){
375         authIn_save = NULL ; authIn_a = (byte *)authIn ;
376     } else {
377       if((authIn_save = XMALLOC(RoundUp16(authInSz), NULL, DYNAMIC_TYPE_TMP_BUFFER)) == NULL){
378           FREE_ALL; return MEMORY_E ; }
379       authIn_a = authIn_save ; XMEMSET(authIn_a, 0, RoundUp16(authInSz)) ; XMEMCPY(authIn_a, authIn, authInSz) ;
380     }
381 
382     if((nonceSz%16)==0){
383         nonce_save = NULL ; nonce_a = (byte *)nonce ;
384     } else {
385       if((nonce_save = XMALLOC(RoundUp16(nonceSz), NULL, DYNAMIC_TYPE_TMP_BUFFER)) == NULL){
386           FREE_ALL; return MEMORY_E; }
387       nonce_a = nonce_save ; XMEMSET(nonce_a, 0, RoundUp16(nonceSz)) ; XMEMCPY(nonce_a, nonce, nonceSz) ;
388     }
389 
390     /* do aes-ccm */
391     AesAuthSetIv(aes, nonce, nonceSz, L, mode) ;
392     ROM_AESReset(AES_BASE);
393     ROM_AESConfigSet(AES_BASE, (aes->keylen | AES_CFG_DIR_ENCRYPT |
394                                 AES_CFG_CTR_WIDTH_128 |
395                                 mode | ((mode== AES_CFG_MODE_CCM) ? (L | M) : 0 ))) ;
396     ROM_AESIVSet(AES_BASE, aes->reg);
397     ROM_AESKey1Set(AES_BASE, aes->key, aes->keylen);
398     ret = ROM_AESDataProcessAuth(AES_BASE, (unsigned int*)in_a, (unsigned int *)out_a, inSz,
399                            (unsigned int*)authIn_a, authInSz, (unsigned int *)tmpTag);
400     if(ret == false){
401         XMEMSET(out, 0, inSz) ;
402         XMEMSET(authTag, 0, authTagSz) ;
403     } else {
404         XMEMCPY(out, out_a, inSz) ;
405         XMEMCPY(authTag, tmpTag, authTagSz) ;
406     }
407 
408     FREE_ALL;
409     return 0 ;
410 }
411 
AesAuthDecrypt(Aes * aes,byte * out,const byte * in,word32 inSz,const byte * nonce,word32 nonceSz,const byte * authTag,word32 authTagSz,const byte * authIn,word32 authInSz,int mode)412 static int AesAuthDecrypt(Aes* aes, byte* out, const byte* in, word32 inSz,
413                               const byte* nonce, word32 nonceSz,
414                               const byte* authTag, word32 authTagSz,
415                               const byte* authIn, word32 authInSz, int mode)
416 {
417     word32 M, L ;
418     byte *in_a,     *in_save ;
419     byte *out_a,    *out_save ;
420     byte *authIn_a, *authIn_save ;
421     byte *nonce_a,    *nonce_save ;
422     word32 tmpTag[4] ;
423     bool ret ;
424 
425     if(AesAuthArgCheck(aes, out, in, inSz, nonce, nonceSz, authTag, authTagSz, authIn, authInSz, &M, &L)
426        == BAD_FUNC_ARG)return  BAD_FUNC_ARG ;
427 
428     /* 16 byte padding */
429     in_save = NULL ; out_save = NULL ; authIn_save = NULL ; nonce_save = NULL ;
430     if((inSz%16)==0){
431         in_save = NULL ; in_a = (byte *)in ;
432         out_save = NULL ; out_a = out ;
433     } else {
434       if((in_save = XMALLOC(RoundUp16(inSz), NULL, DYNAMIC_TYPE_TMP_BUFFER)) == NULL){
435           FREE_ALL; return MEMORY_E;}
436       in_a = in_save ; XMEMSET(in_a, 0, RoundUp16(inSz)) ; XMEMCPY(in_a, in, inSz) ;
437 
438       if((out_save = XMALLOC(RoundUp16(inSz), NULL, DYNAMIC_TYPE_TMP_BUFFER)) == NULL){
439           FREE_ALL; return MEMORY_E;}
440       out_a = out_save ;
441     }
442 
443     if((authInSz%16)==0){
444         authIn_save = NULL ; authIn_a = (byte *)authIn ;
445     } else {
446       if((authIn_save = XMALLOC(RoundUp16(authInSz), NULL, DYNAMIC_TYPE_TMP_BUFFER)) == NULL){
447           FREE_ALL; return MEMORY_E; }
448       authIn_a = authIn_save ; XMEMSET(authIn_a, 0, RoundUp16(authInSz)) ; XMEMCPY(authIn_a, authIn, authInSz) ;
449     }
450 
451     if((nonceSz%16)==0){
452         nonce_save = NULL ; nonce_a = (byte *)nonce ;
453     } else {
454       if((nonce_save = XMALLOC(RoundUp16(nonceSz), NULL, DYNAMIC_TYPE_TMP_BUFFER)) == NULL){
455           FREE_ALL; return MEMORY_E; }
456       nonce_a = nonce_save ; XMEMSET(nonce_a, 0, RoundUp16(nonceSz)) ; XMEMCPY(nonce_a, nonce, nonceSz) ;
457     }
458 
459     /* do aes-ccm */
460     AesAuthSetIv(aes, nonce, nonceSz, L, mode) ;
461     ROM_AESReset(AES_BASE);
462     ROM_AESConfigSet(AES_BASE, (aes->keylen | AES_CFG_DIR_DECRYPT |
463                                 AES_CFG_CTR_WIDTH_128 |
464                                   mode | ((mode== AES_CFG_MODE_CCM) ? (L | M) : 0 ))) ;
465     ROM_AESIVSet(AES_BASE, aes->reg);
466     ROM_AESKey1Set(AES_BASE, aes->key, aes->keylen);
467     ret = ROM_AESDataProcessAuth(AES_BASE, (unsigned int*)in_a, (unsigned int *)out_a, inSz,
468                            (unsigned int*)authIn_a, authInSz, (unsigned int *)tmpTag);
469     if((ret == false) || (XMEMCMP(authTag, tmpTag, authTagSz) != 0)){
470         XMEMSET(out, 0, inSz) ;
471         ret = false ;
472     } else {
473         XMEMCPY(out, out_a, inSz) ;
474     }
475 
476     FREE_ALL ;
477     return ret==true ? 0 : 1 ;
478 }
479 #endif
480 
481 
482 #ifdef HAVE_AESGCM
wc_AesGcmSetKey(Aes * aes,const byte * key,word32 len)483 WOLFSSL_API int  wc_AesGcmSetKey(Aes* aes, const byte* key, word32 len)
484 {
485     return AesAuthSetKey(aes, key, len) ;
486 }
487 
wc_AesGcmEncrypt(Aes * aes,byte * out,const byte * in,word32 sz,const byte * iv,word32 ivSz,byte * authTag,word32 authTagSz,const byte * authIn,word32 authInSz)488 WOLFSSL_API int  wc_AesGcmEncrypt(Aes* aes, byte* out, const byte* in, word32 sz,
489                               const byte* iv, word32 ivSz,
490                               byte* authTag, word32 authTagSz,
491                               const byte* authIn, word32 authInSz)
492 {
493     if (authTagSz < WOLFSSL_MIN_AUTH_TAG_SZ) {
494         return BAD_FUNC_ARG;
495     }
496     return AesAuthEncrypt(aes, out, in, sz, iv, ivSz, authTag, authTagSz,
497                               authIn, authInSz, AES_CFG_MODE_GCM_HY0CALC) ;
498 }
wc_AesGcmDecrypt(Aes * aes,byte * out,const byte * in,word32 sz,const byte * iv,word32 ivSz,const byte * authTag,word32 authTagSz,const byte * authIn,word32 authInSz)499 WOLFSSL_API int  wc_AesGcmDecrypt(Aes* aes, byte* out, const byte* in, word32 sz,
500                               const byte* iv, word32 ivSz,
501                               const byte* authTag, word32 authTagSz,
502                               const byte* authIn, word32 authInSz)
503 {
504     return AesAuthDecrypt(aes, out, in, sz, iv, ivSz, authTag, authTagSz,
505                               authIn, authInSz, AES_CFG_MODE_GCM_HY0CALC) ;
506 }
507 
wc_GmacSetKey(Gmac * gmac,const byte * key,word32 len)508 WOLFSSL_API int wc_GmacSetKey(Gmac* gmac, const byte* key, word32 len)
509 {
510       return AesAuthSetKey(&gmac->aes, key, len) ;
511 }
512 
wc_GmacUpdate(Gmac * gmac,const byte * iv,word32 ivSz,const byte * authIn,word32 authInSz,byte * authTag,word32 authTagSz)513 WOLFSSL_API int wc_GmacUpdate(Gmac* gmac, const byte* iv, word32 ivSz,
514                               const byte* authIn, word32 authInSz,
515                               byte* authTag, word32 authTagSz)
516 {
517     return AesAuthEncrypt(&gmac->aes, NULL, NULL, 0, iv, ivSz, authTag, authTagSz,
518                               authIn, authInSz, AES_CFG_MODE_GCM_HY0CALC) ;
519 }
520 
521 #endif /* HAVE_AESGCM */
522 
523 #ifdef HAVE_AESCCM
wc_AesCcmSetKey(Aes * aes,const byte * key,word32 keySz)524 WOLFSSL_API int wc_AesCcmSetKey(Aes* aes, const byte* key, word32 keySz)
525 {
526     return AesAuthSetKey(aes, key, keySz) ;
527 }
528 
wc_AesCcmEncrypt(Aes * aes,byte * out,const byte * in,word32 inSz,const byte * nonce,word32 nonceSz,byte * authTag,word32 authTagSz,const byte * authIn,word32 authInSz)529 WOLFSSL_API int wc_AesCcmEncrypt(Aes* aes, byte* out, const byte* in, word32 inSz,
530                               const byte* nonce, word32 nonceSz,
531                               byte* authTag, word32 authTagSz,
532                               const byte* authIn, word32 authInSz)
533 {
534     return AesAuthEncrypt(aes, out, in, inSz, nonce, nonceSz, authTag, authTagSz,
535                               authIn, authInSz, AES_CFG_MODE_CCM) ;
536 }
537 
wc_AesCcmDecrypt(Aes * aes,byte * out,const byte * in,word32 inSz,const byte * nonce,word32 nonceSz,const byte * authTag,word32 authTagSz,const byte * authIn,word32 authInSz)538 WOLFSSL_API int wc_AesCcmDecrypt(Aes* aes, byte* out, const byte* in, word32 inSz,
539                               const byte* nonce, word32 nonceSz,
540                               const byte* authTag, word32 authTagSz,
541                               const byte* authIn, word32 authInSz)
542 {
543     return AesAuthDecrypt(aes, out, in, inSz, nonce, nonceSz, authTag, authTagSz,
544                               authIn, authInSz, AES_CFG_MODE_CCM) ;
545 }
546 #endif /* HAVE_AESCCM */
547 
wc_AesInit(Aes * aes,void * heap,int devId)548 WOLFSSL_API int wc_AesInit(Aes* aes, void* heap, int devId)
549 {
550     if (aes == NULL)
551         return BAD_FUNC_ARG;
552 
553     aes->heap = heap;
554     (void)devId;
555 
556     return 0;
557 }
558 
wc_AesFree(Aes * aes)559 WOLFSSL_API void wc_AesFree(Aes* aes)
560 {
561     (void)aes;
562 }
563 
564 #endif /* WOLFSSL_TI_CRYPT */
565 
566 #endif /* NO_AES */
567 
568 
569 
570