wolfssl-sys 4.0.0

System bindings for WolfSSL
Documentation
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
/* wolfcaam_qnx.c
 *
 * Copyright (C) 2006-2026 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 3 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
 */

#ifdef HAVE_CONFIG_H
    #include <config.h>
#endif

#include <wolfssl/wolfcrypt/settings.h>

#if defined(WOLFSSL_QNX_CAAM) && (defined(__QNX__) || defined(__QNXNTO__))

#include <wolfssl/wolfcrypt/logging.h>
#include <wolfssl/wolfcrypt/error-crypt.h>
#include <wolfssl/wolfcrypt/port/caam/wolfcaam.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <devctl.h>

#include <errno.h>

/* for devctl use */
int caamFd = -1;
static wolfSSL_Mutex caamMutex;

/* return 0 on success */
int wc_CAAMInitInterface()
{
    if (wc_InitMutex(&caamMutex) != 0) {
        WOLFSSL_MSG("Could not init mutex");
        return -1;
    }

    caamFd = open("/dev/wolfCrypt", O_RDWR);
    if (caamFd < 0) {
        WOLFSSL_MSG("Could not open /dev/wolfCrypt");
        return -1;
    }

    return 0;
}


void wc_CAAMFreeInterface()
{
    wc_FreeMutex(&caamMutex);
    if (caamFd >= 0)
        close(caamFd);
}

#define WC_TRNG_CMD __DIOTF(_DCMD_ALL, CAAM_ENTROPY, iov_t)
#define WC_CAAM_GET_PART __DIOTF(_DCMD_ALL, CAAM_GET_PART, iov_t)
#define WC_CAAM_FREE_PART __DIOT(_DCMD_ALL, CAAM_FREE_PART, iov_t)
#define WC_CAAM_FIND_PART __DIOTF(_DCMD_ALL, CAAM_FIND_PART, iov_t)
#define WC_CAAM_READ_PART __DIOTF(_DCMD_ALL, CAAM_READ_PART, iov_t)
#define WC_CAAM_WRITE_PART __DIOT(_DCMD_ALL, CAAM_WRITE_PART, iov_t)

#define WC_CAAM_ECDSA_KEYPAIR __DIOTF(_DCMD_ALL, CAAM_ECDSA_KEYPAIR, iov_t)
#define WC_CAAM_ECDSA_VERIFY __DIOT(_DCMD_ALL, CAAM_ECDSA_VERIFY, iov_t)
#define WC_CAAM_ECDSA_SIGN __DIOTF(_DCMD_ALL, CAAM_ECDSA_SIGN, iov_t)
#define WC_CAAM_ECDSA_ECDH __DIOTF(_DCMD_ALL, CAAM_ECDSA_ECDH, iov_t)

#define WC_CAAM_BLOB_ENCAP __DIOTF(_DCMD_ALL, CAAM_BLOB_ENCAP, iov_t)
#define WC_CAAM_BLOB_DECAP __DIOTF(_DCMD_ALL, CAAM_BLOB_DECAP, iov_t)

#define WC_CAAM_CMAC __DIOTF(_DCMD_ALL, CAAM_CMAC, iov_t)
#define WC_CAAM_AESECB __DIOTF(_DCMD_ALL, CAAM_AESECB, iov_t)
#define WC_CAAM_AESCTR __DIOTF(_DCMD_ALL, CAAM_AESCTR, iov_t)
#define WC_CAAM_AESCBC __DIOTF(_DCMD_ALL, CAAM_AESCBC, iov_t)
#define WC_CAAM_AESCCM __DIOTF(_DCMD_ALL, CAAM_AESCCM, iov_t)
#define WC_CAAM_AESGCM __DIOTF(_DCMD_ALL, CAAM_AESGCM, iov_t)

#define WC_CAAM_FIFO_S __DIOTF(_DCMD_ALL, CAAM_FIFO_S, iov_t)

#define MAX_IN_IOVS 6
#define MAX_OUT_IOVS 3

/* Do a synchronous operations and block till done
 * returns 0 on success */
int SynchronousSendRequest(int type, unsigned int args[4], CAAM_BUFFER *buf,
        int sz)
{
    int ret, inIdx = 0, outIdx = 0;
    int cmd = 0;
    iov_t in[MAX_IN_IOVS], out[MAX_OUT_IOVS];
    CAAM_ADDRESS privkey;

    if (args != NULL) {
        SETIOV(&in[inIdx], args, sizeof(unsigned int) * 4);
        inIdx = inIdx + 1;
    }
    else {
        unsigned int localArgs[4] = {0};
        SETIOV(&in[inIdx], localArgs, sizeof(unsigned int) * 4);
        inIdx = inIdx + 1;
    }

    switch (type) {
    case CAAM_ENTROPY:
        SETIOV(&out[outIdx], (buf->TheAddress), (buf->Length));
        outIdx = outIdx + 1;
        cmd = WC_TRNG_CMD;
        break;

    case CAAM_GET_PART:
        SETIOV(&out[outIdx], (buf->TheAddress), (buf->Length));
        outIdx = outIdx + 1;
        cmd = WC_CAAM_GET_PART;
        break;

    case CAAM_FREE_PART:
        cmd = WC_CAAM_FREE_PART;
        break;

    case CAAM_FIND_PART:
        SETIOV(&out[outIdx], (buf->TheAddress), (buf->Length));
        outIdx = outIdx + 1;
        cmd = WC_CAAM_FIND_PART;
        break;

    case CAAM_READ_PART:
        SETIOV(&out[outIdx], (buf->TheAddress), (buf->Length));
        outIdx = outIdx + 1;
        cmd = WC_CAAM_READ_PART;
        break;

    case CAAM_WRITE_PART:
        SETIOV(&in[inIdx], (buf->TheAddress), (buf->Length));
        inIdx = inIdx + 1;
        cmd = WC_CAAM_WRITE_PART;
        break;

    case CAAM_ECDSA_KEYPAIR:
        /* set output to store directly to CAAM_BUFFER's */
        SETIOV(&out[outIdx], buf[0].TheAddress, buf[0].Length);
        outIdx = outIdx + 1;

        SETIOV(&out[outIdx], buf[1].TheAddress, buf[1].Length);
        outIdx = outIdx + 1;

        /* get args for updated partition number used */
        SETIOV(&out[outIdx], args, sizeof(unsigned int) * 4);
        outIdx = outIdx + 1;

        cmd = WC_CAAM_ECDSA_KEYPAIR;
        break;

    case CAAM_ECDSA_VERIFY:
        /* public key */
        SETIOV(&in[inIdx], buf[0].TheAddress, buf[0].Length);
        inIdx = inIdx + 1;

        /* msg */
        SETIOV(&in[inIdx], buf[1].TheAddress, buf[1].Length);
        inIdx = inIdx + 1;

        /* r */
        SETIOV(&in[inIdx], buf[2].TheAddress, buf[2].Length);
        inIdx = inIdx + 1;

        /* s */
        SETIOV(&in[inIdx], buf[3].TheAddress, buf[3].Length);
        inIdx = inIdx + 1;

        cmd = WC_CAAM_ECDSA_VERIFY;
        break;

    case CAAM_ECDSA_SIGN:
        /* private key */
        if (args[0] == 1) {
            privkey = buf[0].TheAddress;
            SETIOV(&in[inIdx], &privkey, sizeof(unsigned int));
            inIdx = inIdx + 1;
        }
        else {
            SETIOV(&in[inIdx], buf[0].TheAddress, buf[0].Length);
            inIdx = inIdx + 1;
        }

        /* msg */
        SETIOV(&in[inIdx], buf[1].TheAddress, buf[1].Length);
        inIdx = inIdx + 1;

        /* r out */
        SETIOV(&out[outIdx], buf[2].TheAddress, buf[2].Length);
        outIdx = outIdx + 1;

        /* s out */
        SETIOV(&out[outIdx], buf[3].TheAddress, buf[3].Length);
        outIdx = outIdx + 1;

        cmd = WC_CAAM_ECDSA_SIGN;
        break;

    case CAAM_ECDSA_ECDH:
        /* when using memory in secure partition just send the address */
        SETIOV(&in[inIdx], buf[0].TheAddress, buf[0].Length);
        inIdx = inIdx + 1;

        /* private key */
        if (args[0] == CAAM_BLACK_KEY_SM) {
            privkey = buf[1].TheAddress;
            SETIOV(&in[inIdx], &privkey, sizeof(unsigned int));
            inIdx = inIdx + 1;
        }
        else {
            SETIOV(&in[inIdx], buf[1].TheAddress, buf[1].Length);
            inIdx = inIdx + 1;
        }

        /* shared secret */
        SETIOV(&out[outIdx], buf[2].TheAddress, buf[2].Length);
        outIdx = outIdx + 1;

        cmd = WC_CAAM_ECDSA_ECDH;
        break;

    case CAAM_BLOB_ENCAP:
        SETIOV(&in[inIdx], buf[0].TheAddress, buf[0].Length);
        inIdx = inIdx + 1;

        if (args[0] == 1) {
            SETIOV(&in[inIdx], buf[1].TheAddress, buf[1].Length + WC_CAAM_MAC_SZ);
            inIdx = inIdx + 1;
        }
        else {
            SETIOV(&in[inIdx], buf[1].TheAddress, buf[1].Length);
            inIdx = inIdx + 1;
        }

        SETIOV(&out[outIdx], buf[2].TheAddress, buf[2].Length);
        outIdx = outIdx + 1;
        cmd = WC_CAAM_BLOB_ENCAP;
        break;

    case CAAM_BLOB_DECAP:
        SETIOV(&in[inIdx], buf[0].TheAddress, buf[0].Length);
        inIdx = inIdx + 1;

        SETIOV(&in[inIdx], buf[1].TheAddress, buf[1].Length);
        inIdx = inIdx + 1;

        if (args[0] == 1) {
            SETIOV(&out[outIdx], buf[2].TheAddress,
                    buf[2].Length + WC_CAAM_MAC_SZ);
            outIdx = outIdx + 1;
        }
        else {
            SETIOV(&out[outIdx], buf[2].TheAddress, buf[2].Length);
            outIdx = outIdx + 1;
        }

        cmd = WC_CAAM_BLOB_DECAP;
        break;

    case CAAM_AESECB:
        /* key */
        SETIOV(&in[inIdx], buf[0].TheAddress, buf[0].Length);
        inIdx = inIdx + 1;

        /* in */
        SETIOV(&in[inIdx], buf[1].TheAddress, buf[1].Length);
        inIdx = inIdx + 1;

        /* out */
        SETIOV(&out[outIdx], buf[2].TheAddress, buf[2].Length);
        outIdx = outIdx + 1;
        cmd = WC_CAAM_AESECB;
        break;

    case CAAM_AESCBC:
        /* key */
        SETIOV(&in[inIdx], buf[0].TheAddress, buf[0].Length);
        inIdx = inIdx + 1;

        /* in */
        SETIOV(&in[inIdx], buf[1].TheAddress, buf[1].Length);
        inIdx = inIdx + 1;

        /* iv */
        SETIOV(&in[inIdx], buf[2].TheAddress, buf[2].Length);
        inIdx = inIdx + 1;

        /* out */
        SETIOV(&out[outIdx], buf[3].TheAddress, buf[3].Length);
        outIdx = outIdx + 1;

        /* out updated IV */
        SETIOV(&out[outIdx], buf[4].TheAddress, buf[4].Length);
        outIdx = outIdx + 1;

        cmd = WC_CAAM_AESCBC;
        break;

    case CAAM_AESCTR:
        /* key */
        SETIOV(&in[inIdx], buf[0].TheAddress, buf[0].Length);
        inIdx = inIdx + 1;

        /* iv */
        SETIOV(&in[inIdx], buf[1].TheAddress, buf[1].Length);
        inIdx = inIdx + 1;

        /* in */
        SETIOV(&in[inIdx], buf[2].TheAddress, buf[2].Length);
        inIdx = inIdx + 1;

        /* out */
        SETIOV(&out[outIdx], buf[3].TheAddress, buf[3].Length);
        outIdx = outIdx + 1;

        /* out updated IV */
        SETIOV(&out[outIdx], buf[4].TheAddress, buf[4].Length);
        outIdx = outIdx + 1;

        cmd = WC_CAAM_AESCTR;
        break;

    case CAAM_AESCCM:
        /* key */
        SETIOV(&in[inIdx], buf[0].TheAddress, buf[0].Length);
        inIdx = inIdx + 1;

        /* iv */
        SETIOV(&in[inIdx], buf[1].TheAddress, buf[1].Length);
        inIdx = inIdx + 1;

        /* in */
        SETIOV(&in[inIdx], buf[2].TheAddress, buf[2].Length);
        inIdx = inIdx + 1;

        /* out */
        SETIOV(&out[outIdx], buf[3].TheAddress, buf[3].Length);
        outIdx = outIdx + 1;

        /* set TAG as input or output */
        if ((args[0] & 0xFFFF) == CAAM_ENC) {
            SETIOV(&out[outIdx], buf[4].TheAddress, buf[4].Length);
            outIdx = outIdx + 1;
        }
        else {
            SETIOV(&in[inIdx], buf[4].TheAddress, buf[4].Length);
            inIdx = inIdx + 1;
        }

        /* aad */
        SETIOV(&in[inIdx], buf[5].TheAddress, buf[5].Length);
        inIdx = inIdx + 1;

        cmd = WC_CAAM_AESCCM;
        break;

    case CAAM_AESGCM:
        /* key */
        SETIOV(&in[inIdx], buf[0].TheAddress, buf[0].Length);
        inIdx = inIdx + 1;

        /* iv */
        SETIOV(&in[inIdx], buf[1].TheAddress, buf[1].Length);
        inIdx = inIdx + 1;

        /* in */
        SETIOV(&in[inIdx], buf[2].TheAddress, buf[2].Length);
        inIdx = inIdx + 1;

        /* out */
        SETIOV(&out[outIdx], buf[3].TheAddress, buf[3].Length);
        outIdx = outIdx + 1;

        /* set TAG as input or output */
        if ((args[0] & 0xFFFF) == CAAM_ENC) {
            SETIOV(&out[outIdx], buf[4].TheAddress, buf[4].Length);
            outIdx = outIdx + 1;
        }
        else {
            SETIOV(&in[inIdx], buf[4].TheAddress, buf[4].Length);
            inIdx = inIdx + 1;
        }

        /* aad */
        SETIOV(&in[inIdx], buf[5].TheAddress, buf[5].Length);
        inIdx = inIdx + 1;

        cmd = WC_CAAM_AESGCM;
        break;

    case CAAM_CMAC:
        {
            int i;

            if (args[2] == 1) {
                SETIOV(&in[inIdx], buf[0].TheAddress, buf[0].Length + 16);
                inIdx = inIdx + 1;
            }
            else {
                SETIOV(&in[inIdx], buf[0].TheAddress, buf[0].Length);
                inIdx = inIdx + 1;
            }

            SETIOV(&in[inIdx], buf[1].TheAddress, buf[1].Length);
            inIdx = inIdx + 1;

            /* get input buffers */
            args[3] = 0;
            for (i = 2; i < sz && i < MAX_IN_IOVS; i++) {
                SETIOV(&in[inIdx], buf[i].TheAddress, buf[i].Length);
                inIdx = inIdx + 1;
                args[3] += buf[i].Length;
            }

            SETIOV(&out[outIdx], buf[1].TheAddress, buf[1].Length);
            outIdx = outIdx + 1;
        }
        cmd = WC_CAAM_CMAC;
        break;

    case CAAM_FIFO_S:
        SETIOV(&in[inIdx], buf[0].TheAddress, buf[0].Length);
        inIdx = inIdx + 1;

        SETIOV(&out[outIdx], buf[1].TheAddress, buf[1].Length + WC_CAAM_MAC_SZ);
        outIdx = outIdx + 1;
        cmd = WC_CAAM_FIFO_S;
        break;

    default:
        WOLFSSL_MSG("Unknown/unsupported type");
        return -1;
    }

    ret = wc_LockMutex(&caamMutex);
    if (ret == 0) {
        ret = devctlv(caamFd, cmd, inIdx, outIdx, in, out, NULL);
        wc_UnLockMutex(&caamMutex);
    }

    if (ret != 0) {
        if (ret == EFAULT) {
            WOLFSSL_MSG("bad address on one of the in/out buffers");
            return -1;
        }

        if (ret == EAGAIN && type == CAAM_ENTROPY) {
            return CAAM_WAITING;
        }
        return -1;
    }

    return Success;
}

#endif /* WOLFSSL_QNX_CAAM && (__QNX__ || __QNXNTO__) */