1 /* port/ti/ti-des.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_DES3
30
31 #if defined(WOLFSSL_TI_CRYPT)
32 #include <stdbool.h>
33 #include <stdint.h>
34
35 #include <wolfssl/wolfcrypt/des3.h>
36 #include <wolfssl/wolfcrypt/error-crypt.h>
37 #include <wolfssl/wolfcrypt/port/ti/ti-ccm.h>
38
39 #include "inc/hw_des.h"
40 #include "inc/hw_memmap.h"
41 #include "inc/hw_ints.h"
42 #include "driverlib/des.h"
43 #include "driverlib/sysctl.h"
44 #include "driverlib/rom_map.h"
45 #include "driverlib/rom.h"
46
DesSetIV(Des * des,const byte * iv,int tri)47 static int DesSetIV(Des* des, const byte* iv, int tri)
48 {
49 if (des == NULL)
50 return BAD_FUNC_ARG;
51
52 if (iv)
53 XMEMCPY(des->reg, iv, tri == DES_CFG_TRIPLE ? DES3_IVLEN : DES_IVLEN);
54 else
55 XMEMSET(des->reg, 0, tri == DES_CFG_TRIPLE ? DES3_IVLEN : DES_IVLEN);
56
57 return 0;
58 }
59
DesSetKey(Des * des,const byte * key,const byte * iv,int dir,int tri)60 static int DesSetKey(Des* des, const byte* key, const byte* iv,int dir, int tri)
61 {
62 if(!wolfSSL_TI_CCMInit())return 1 ;
63 if ((des == NULL) || (key == NULL) || (iv == NULL))
64 return BAD_FUNC_ARG;
65 if(!((dir == DES_ENCRYPTION) || (dir == DES_DECRYPTION)))
66 return BAD_FUNC_ARG;
67
68 XMEMCPY(des->key, key, tri == DES_CFG_SINGLE ? DES_KEYLEN : DES3_KEYLEN) ;
69 return DesSetIV(des, iv, tri);
70 }
71
DesCbcAlign16(Des * des,byte * out,const byte * in,word32 sz,word32 dir,word32 tri)72 static int DesCbcAlign16(Des* des, byte* out, const byte* in, word32 sz, word32 dir, word32 tri)
73 {
74
75 wolfSSL_TI_lockCCM() ;
76 ROM_DESReset(DES_BASE);
77 ROM_DESConfigSet(DES_BASE, (dir | DES_CFG_MODE_CBC | tri));
78 ROM_DESIVSet(DES_BASE, (uint32_t*)des->reg);
79 ROM_DESKeySet(DES_BASE,(uint32_t*)des->key);
80 if(dir == DES_CFG_DIR_DECRYPT)
81 /* if input and output same will overwrite input iv */
82 XMEMCPY(des->tmp, in + sz - DES_BLOCK_SIZE, DES_BLOCK_SIZE);
83 ROM_DESDataProcess(DES_BASE, (uint32_t *)in, (uint32_t *)out, sz);
84 wolfSSL_TI_unlockCCM() ;
85
86 /* store iv for next call */
87 if(dir == DES_CFG_DIR_ENCRYPT)
88 XMEMCPY(des->reg, out + sz - DES_BLOCK_SIZE, DES_BLOCK_SIZE);
89 else
90 XMEMCPY(des->reg, des->tmp, DES_BLOCK_SIZE);
91
92 return 0 ;
93 }
94
95 #define IS_ALIGN16(p) (((unsigned int)(p)&0xf) == 0)
96
DesCbc(Des * des,byte * out,const byte * in,word32 sz,word32 dir,word32 tri)97 static int DesCbc(Des* des, byte* out, const byte* in, word32 sz, word32 dir, word32 tri)
98 {
99 const byte * in_p ; byte * out_p ;
100 word32 size ;
101 #define TI_BUFFSIZE 1024
102 byte buff[TI_BUFFSIZE] ;
103 if ((des == NULL) || (in == NULL) || (out == NULL))
104 return BAD_FUNC_ARG;
105 if(sz % DES_BLOCK_SIZE)
106 return BAD_FUNC_ARG;
107
108 while(sz > 0) {
109 size = sz ; in_p = in ; out_p = out ;
110 if(!IS_ALIGN16(in)){
111 size = sz>TI_BUFFSIZE ? TI_BUFFSIZE : sz ;
112 XMEMCPY(buff, in, size) ;
113 in_p = (const byte *)buff ;
114 }
115 if(!IS_ALIGN16(out)){
116 size = sz>TI_BUFFSIZE ? TI_BUFFSIZE : sz ;
117 out_p = (byte *)buff ;
118 }
119
120 DesCbcAlign16(des, out_p, in_p, size, dir, tri) ;
121
122 if(!IS_ALIGN16(out)){
123 XMEMCPY(out, buff, size) ;
124 }
125 sz -= size ; in += size ; out += size ;
126 }
127 return 0 ;
128 }
129
wc_Des_SetKey(Des * des,const byte * key,const byte * iv,int dir)130 WOLFSSL_API int wc_Des_SetKey(Des* des, const byte* key, const byte* iv,int dir)
131 {
132 return DesSetKey(des, key, iv, dir, DES_CFG_SINGLE) ;
133 }
134
wc_Des_SetIV(Des * des,const byte * iv)135 WOLFSSL_API void wc_Des_SetIV(Des* des, const byte* iv)
136 {
137 DesSetIV(des, iv, DES_CFG_SINGLE) ;
138 }
139
wc_Des3_SetKey(Des3 * des,const byte * key,const byte * iv,int dir)140 WOLFSSL_API int wc_Des3_SetKey(Des3* des, const byte* key, const byte* iv,int dir)
141 {
142 return DesSetKey((Des *)des, key, iv, dir, DES_CFG_TRIPLE) ;
143 }
144
wc_Des3_SetIV(Des3 * des,const byte * iv)145 WOLFSSL_API int wc_Des3_SetIV(Des3* des, const byte* iv)
146 {
147 return DesSetIV((Des *)des, iv, DES_CFG_TRIPLE) ;
148 }
149
150
wc_Des_CbcEncrypt(Des * des,byte * out,const byte * in,word32 sz)151 WOLFSSL_API int wc_Des_CbcEncrypt(Des* des, byte* out, const byte* in, word32 sz)
152 {
153 return DesCbc(des, out, in, sz, DES_CFG_DIR_ENCRYPT, DES_CFG_SINGLE) ;
154 }
155
wc_Des_CbcDecrypt(Des * des,byte * out,const byte * in,word32 sz)156 WOLFSSL_API int wc_Des_CbcDecrypt(Des* des, byte* out, const byte* in, word32 sz)
157 {
158 return DesCbc(des, out, in, sz, DES_CFG_DIR_DECRYPT, DES_CFG_SINGLE) ;
159 }
160
wc_Des_CbcDecryptWithKey(byte * out,const byte * in,word32 sz,const byte * key,const byte * iv)161 WOLFSSL_API int wc_Des_CbcDecryptWithKey(byte* out, const byte* in, word32 sz,
162 const byte* key, const byte* iv)
163 {
164 (void)out; (void)in; (void)sz; (void)key; (void)iv ;
165 return -1 ;
166 }
167
wc_Des3_CbcEncrypt(Des3 * des,byte * out,const byte * in,word32 sz)168 WOLFSSL_API int wc_Des3_CbcEncrypt(Des3* des, byte* out, const byte* in, word32 sz)
169 {
170 return DesCbc((Des *)des, out, in, sz, DES_CFG_DIR_ENCRYPT, DES_CFG_TRIPLE) ;
171 }
172
wc_Des3_CbcDecrypt(Des3 * des,byte * out,const byte * in,word32 sz)173 WOLFSSL_API int wc_Des3_CbcDecrypt(Des3* des, byte* out, const byte* in, word32 sz)
174 {
175 return DesCbc((Des *)des, out, in, sz, DES_CFG_DIR_DECRYPT, DES_CFG_TRIPLE) ;
176 }
177
wc_Des3_CbcDecryptWithKey(byte * out,const byte * in,word32 sz,const byte * key,const byte * iv)178 WOLFSSL_API int wc_Des3_CbcDecryptWithKey(byte* out, const byte* in, word32 sz,
179 const byte* key, const byte* iv)
180 {
181 (void)out; (void)in; (void)sz; (void)key; (void)iv ;
182 return -1 ;
183 }
184
wc_Des3Init(Des3 * des,void * heap,int devId)185 WOLFSSL_API int wc_Des3Init(Des3* des, void* heap, int devId)
186 {
187 if (des == NULL)
188 return BAD_FUNC_ARG;
189
190 des->heap = heap;
191 (void)devId;
192
193 return 0;
194 }
195
wc_Des3Free(Des3 * des)196 WOLFSSL_API void wc_Des3Free(Des3* des)
197 {
198 (void)des;
199 }
200
201
202 #endif /* WOLFSSL_TI_CRYPT */
203
204 #endif /* !NO_DES3 */
205