1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
|
/* caam_init.c
*
* Copyright (C) 2006-2020 wolfSSL Inc.
*
* This file is part of wolfSSL.
*
* wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* wolfSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
#include <wolfssl/wolfcrypt/settings.h>
#if defined(WOLFSSL_IMX6_CAAM) || defined(WOLFSSL_IMX6_CAAM_RNG) || \
defined(WOLFSSL_IMX6_CAAM_BLOB)
#include <wolfssl/wolfcrypt/logging.h>
#include <wolfssl/wolfcrypt/error-crypt.h>
#include <wolfssl/wolfcrypt/port/caam/wolfcaam.h>
#define WC_CAAM_BLOB_SZ 48
#ifndef WC_CAAM_PASSWORD
#define WC_CAAM_PASSWORD "!systempassword"
#endif
#if defined(__INTEGRITY) || defined(INTEGRITY)
#include <INTEGRITY.h>
#include <wolfssl/wolfcrypt/port/caam/caam_driver.h>
static IODevice caam = NULLIODevice;
#endif
#if defined(WOLFSSL_CAAM_PRINT) || defined(WOLFSSL_CAAM_DEBUG)
#include <stdio.h>
#include <wolfssl/version.h>
static void wc_caamBanner(void)
{
printf("********* wolfSSL Version %s : Printing Out CAAM Information ********\n",
LIBWOLFSSL_VERSION_STRING);
printf("CAAM Status [0x%8.8x] = 0x%8.8x\n",
CAAM_STATUS, WC_CAAM_READ(CAAM_STATUS));
printf("CAAM Version MS Register [0x%8.8x] = 0x%8.8x\n",
CAAM_VERSION_MS, WC_CAAM_READ(CAAM_VERSION_MS));
printf("CAAM Version LS Register [0x%8.8x] = 0x%8.8x\n",
CAAM_VERSION_LS, WC_CAAM_READ(CAAM_VERSION_LS));
printf("CAAM Support MS Register [0x%8.8x] = 0x%8.8x\n",
CAMM_SUPPORT_MS, WC_CAAM_READ(CAMM_SUPPORT_MS));
printf("CAAM Support LS [0x%8.8x] = 0x%8.8x\n",
CAMM_SUPPORT_LS, WC_CAAM_READ(CAMM_SUPPORT_LS));
printf("********************************************************************\n\n");
}
#endif
/* Allow runtime setting for CAAM IODevice in case user wants to use password
* at run time.
*
* returns 0 on success
*
* NOTE this is how IODevice is defined in INTEGRITY "typedef struct
* IODeviceStruct *IODevice;"
*/
int wc_caamSetResource(IODevice ioDev)
{
WOLFSSL_MSG("Setting CAAM driver");
caam = ioDev;
return 0;
}
/* Check hardware support
*
* returns 0 on success
*/
int wc_caamInit(void)
{
int ret;
word32 reg;
/* get the driver up */
if (caam == NULLIODevice) {
WOLFSSL_MSG("Starting CAAM driver");
if ((ret = (int)RequestResource((Object *)&caam, "wolfSSL_CAAM_Driver",
WC_CAAM_PASSWORD)) != (int)Success) {
WOLFSSL_MSG("Unable to get the CAAM IODevice, check password?");
WOLFSSL_LEAVE("wc_caamInit: error from driver = ", ret);
ret = 0; /* not a hard failure because user can set resource */
}
}
#if defined(WOLFSSL_CAAM_PRINT) || defined(WOLFSSL_CAAM_DEBUG)
/* print out CAAM version/info and wolfSSL version */
wc_caamBanner();
#endif
/* check that for implemented modules
* bits 0-3 AES, 4-7 DES, 12-15 Hashing , 16-19 RNG */
reg = WC_CAAM_READ(CAMM_SUPPORT_LS);
#ifndef WC_NO_RNG
if (((reg & 0x000F0000) >> 16) > 0) {
WOLFSSL_MSG("Found CAAM RNG hardware module");
if ((WC_CAAM_READ(CAAM_RTMCTL) & 0x40000001) != 0x40000001) {
WOLFSSL_MSG("Error CAAM RNG has not been set up");
}
}
#endif
#ifndef NO_SHA256
if ((reg & 0x0000F000) > 0) {
WOLFSSL_MSG("Found CAAM MDHA module");
}
else {
WOLFSSL_MSG("Hashing not supported by CAAM");
return WC_HW_E;
}
#endif
#ifndef NO_AES
if ((reg & 0x0000000F) > 0) {
WOLFSSL_MSG("Found CAAM AES module");
}
else {
WOLFSSL_MSG("AES not supported by CAAM");
return WC_HW_E;
}
#endif
(void)ret;
return 0;
}
int wc_caamFree(void)
{
return 0;
}
word32 wc_caamReadRegister(word32 reg)
{
Value out = 0;
if (caam == NULLIODevice) {
WOLFSSL_MSG("Error CAAM IODevice not found! Bad password?");
return 0;
}
if (ReadIODeviceRegister(caam, reg, &out) != Success) {
WOLFSSL_MSG("Error reading register\n");
}
return (word32)out;
}
void wc_caamWriteRegister(word32 reg, word32 value)
{
if (caam == NULLIODevice) {
WOLFSSL_MSG("Error CAAM IODevice not found! Bad password?");
return;
}
if (WriteIODeviceRegister(caam, reg, value) != Success) {
WOLFSSL_MSG("Error writing to register\n");
}
}
/* return 0 on success and WC_HW_E on failure. Can also return WC_HW_WAIT_E
* in the case that the driver is waiting for a resource or RAN_BLOCK_E if
* waiting for entropy. */
int wc_caamAddAndWait(Buffer* buf, word32 arg[4], word32 type)
{
int ret;
if (caam == NULLIODevice) {
WOLFSSL_MSG("Error CAAM IODevice not found! Bad password?");
return WC_HW_E;
}
if ((ret = SynchronousSendIORequest(caam, type, (const Value*)arg, buf))
!= Success) {
#if defined(WOLFSSL_CAAM_PRINT) || defined(WOLFSSL_CAAM_DEBUG)
printf("ret of SynchronousSendIORequest = %d type = %d\n", ret, type);
#endif
/* if waiting for resource or RNG return waiting */
if (ret == Waiting) {
WOLFSSL_MSG("Waiting on entropy from driver");
return RAN_BLOCK_E;
}
if (ret == ResourceNotAvailable) {
WOLFSSL_MSG("Waiting on CAAM driver");
return WC_HW_WAIT_E;
}
return WC_HW_E;
}
(void)ret;
return 0;
}
int wc_caamCreateBlob(byte* data, word32 dataSz, byte* out, word32* outSz)
{
Buffer in[3];
word32 arg[4];
int ret;
word32 local[2] = {0,0};
if (data == NULL || out == NULL || outSz == NULL ||
*outSz < dataSz + WC_CAAM_BLOB_SZ) {
return BAD_FUNC_ARG;
}
in[0].BufferType = DataBuffer;
in[0].TheAddress = (Address)local;
in[0].Length = sizeof(local);
in[1].BufferType = DataBuffer;
in[1].TheAddress = (Address)data;
in[1].Length = dataSz;
in[2].BufferType = DataBuffer | LastBuffer;
in[2].TheAddress = (Address)out;
in[2].Length = dataSz + WC_CAAM_BLOB_SZ;
arg[2] = dataSz;
if ((ret = wc_caamAddAndWait(in, arg, CAAM_BLOB_ENCAP)) != 0) {
WOLFSSL_MSG("Error with CAAM blob create");
return ret;
}
*outSz = dataSz + WC_CAAM_BLOB_SZ;
return 0;
}
int wc_caamOpenBlob(byte* data, word32 dataSz, byte* out, word32* outSz)
{
Buffer in[3];
word32 arg[4];
int ret;
word32 local[2] = {0,0};
if (data == NULL || out == NULL || outSz == NULL ||
*outSz < dataSz - WC_CAAM_BLOB_SZ) {
return BAD_FUNC_ARG;
}
in[0].BufferType = DataBuffer;
in[0].TheAddress = (Address)local;
in[0].Length = sizeof(local);
in[0].BufferType = DataBuffer;
in[0].TheAddress = (Address)data;
in[0].Length = dataSz;
in[1].BufferType = DataBuffer | LastBuffer;
in[1].TheAddress = (Address)out;
in[1].Length = dataSz - WC_CAAM_BLOB_SZ;
arg[2] = dataSz;
if ((ret = wc_caamAddAndWait(in, arg, CAAM_BLOB_DECAP)) != 0) {
WOLFSSL_MSG("Error with CAAM blob create");
return ret;
}
*outSz = dataSz - WC_CAAM_BLOB_SZ;
return 0;
}
#endif /* WOLFSSL_IMX6_CAAM */
|