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
/* renesas_rx64_hw_sha.c
 *
 * Contributed by Johnson Controls Tyco IP Holdings LLP.
 *
 * 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
 */
#include <string.h>
#include <stdio.h>

#ifdef HAVE_CONFIG_H
    #include <config.h>
#endif
#include <wolfssl/wolfcrypt/settings.h>

#if !defined(NO_SHA) || !defined(NO_SHA256)

#include <wolfssl/wolfcrypt/logging.h>

#if defined(WOLFSSL_RENESAS_RX64_HASH)

#include <wolfssl/wolfcrypt/error-crypt.h>
#include <wolfssl/wolfcrypt/port/Renesas/renesas-rx64-hw-crypt.h>

#include <wolfssl/wolfcrypt/sha.h>

typedef union
{
    R_sha1   sha1;
    R_sha224 sha224;
    R_sha256 sha256;
} R_Sha_Data;

/**
Default SHA Hash Data When Input Msg Buffers are NULL.

The source of this data can be obtained from a simple python
program that requests the hash of an empty input argument.
Example:
import hashlib
print("SHA default/empty hash values")
print(f"SHA1   {hashlib.sha1(b'').hexdigest()}")
print(f"SHA224 {hashlib.sha224(b'').hexdigest()}")
print(f"SHA256 {hashlib.sha256(b'').hexdigest()}")

OR

The following website also provide data for these hashes when
an empty buffer is given as input
https://www.di-mgt.com.au/sha_testvectors.html
**/

static byte const DefaultShaHashData[] =
{
  0xDA, 0x39, 0xA3, 0xEE, 0x5E, 0x6B, 0x4B, 0x0D,
  0x32, 0x55, 0xBF, 0xEF, 0x95, 0x60, 0x18, 0x90,
  0xAF, 0xD8, 0x07, 0x09
};

static byte const DefaultSha224HashData[] =
{
  0xD1, 0x4A, 0x02, 0x8C, 0x2A, 0x3A, 0x2B, 0xC9,
  0x47, 0x61, 0x02, 0xBB, 0x28, 0x82, 0x34, 0xC4,
  0x15, 0xA2, 0xB0, 0x1F, 0x82, 0x8E, 0xA6, 0x2A,
  0xC5, 0xB3, 0xE4, 0x2F
};

static byte const DefaultSha256HashData[] =
{
  0xE3, 0xB0, 0xC4, 0x42, 0x98, 0xFC, 0x1C, 0x14,
  0x9A, 0xFB, 0xF4, 0xC8, 0x99, 0x6F, 0xB9, 0x24,
  0x27, 0xAE, 0x41, 0xE4, 0x64, 0x9B, 0x93, 0x4C,
  0xA4, 0x95, 0x99, 0x1B, 0x78, 0x52, 0xB8, 0x55
};

/**
 * @brief Calculate a SHA hash using the RX64 SHA subsystem.
 *
 * @param[in] data buffer with data to sha
 * @param[in] len length of data
 * @param[out] out Output buffer to store sha result in
 * @param[in] sha_type Sha type to calculate, from RX64_SHA_TYPE
 * @return int R_PROCESS_COMPLETE (0) on success, see r_sha.h for failure codes.
 */
int RX64_ShaCalc(byte* data, word32 len, byte* out, word32 sha_type)
{
    int ret;
    uint8_t flag = R_SHA_INIT;
    word32 index = 0;
    uint16_t chunk_length;
    R_Sha_Data work_sha;

    if (data == NULL || len == 0 ||
        out == NULL || sha_type >= NUM_RX64_SHA_TYPES)
    {
        return BAD_FUNC_ARG;
    }

    XMEMSET(&work_sha, 0, sizeof(work_sha));

    rx64_hw_lock();
    do {
        /*
        The hardware functions can only accept UINT16_MAX bytes at a time.
        To work around this break the buffer up into chunks and pass the
        R_SHA_FINISH flag with the last chunk.
        */
        if (len - index <= UINT16_MAX) {
            flag = flag | R_SHA_FINISH;
            chunk_length = len - index;
        } else {
            chunk_length = UINT16_MAX;
        }
        /* Based on the hash type call the correct hardware function. */
        if (sha_type == RX64_SHA1) {
            ret = R_Sha1_HashDigest(&data[index], out, chunk_length, flag,
                                    &work_sha.sha1);
        } else if (sha_type == RX64_SHA224) {
            ret = R_Sha224_HashDigest(&data[index], out, chunk_length, flag,
                                      &work_sha.sha224);
        } else if (sha_type == RX64_SHA256) {
            ret = R_Sha256_HashDigest(&data[index], out, chunk_length, flag,
                                      &work_sha.sha256);
        }
        if (ret != R_PROCESS_COMPLETE) {
            /* On failure break, unlock hardware, return error. */
            break;
        }
        index += chunk_length;
        flag = R_SHA_ADD;
    } while (index < len);

    rx64_hw_unlock();
    return ret;
}

/**
 * @brief Free a hash for use with the RX64 SHA subsystem.
 *
 * @param[in] hash The hash to free
 */
static void RX64_HashFree(wolfssl_RX64_HW_Hash* hash)
{
    if (hash == NULL)
        return;

    XFREE(hash->msg, hash->heap, DYNAMIC_TYPE_TMP_BUFFER);
    hash->msg = NULL;
}

/**
 * @brief Initialize a hash for use with the RX64 SHA subsystem.
 *
 * @param[in] hash The hash to initialize
 * @param[in] heap Optional pointer to memory to use.
 * @param devId Unused
 * @param[in] sha_type The SHA type for this hash
 * @return int 0 on success, BAD_FUNC_ARG on failure
 */
static int RX64_HashInit(wolfssl_RX64_HW_Hash* hash, void* heap, int devId,
    word32 sha_type)
{
    if (hash == NULL) {
        return BAD_FUNC_ARG;
    }

    (void)devId;
    XMEMSET(hash, 0, sizeof(wolfssl_RX64_HW_Hash));

    hash->heap = heap;
    hash->len  = 0;
    hash->used = 0;
    hash->msg  = NULL;
    hash->sha_type = sha_type;

    return 0;
}

/**
 * @brief Add data to the hash with the RX64 SHA subsystem.
 *
 * Note that do to the limitations in the RX64 hardware
 * and it's inability to save the current state,
 * this function actually just adds the data to a buffer
 * that will then be processed when calling HashFinal or HashGet
 *
 * @param[in] hash Hash structure
 * @param[in] data data to hash
 * @param[in] sz size of the data
 * @return int 0 on success, BAD_FUNC_ARG or MEMORY_E on failure
 */
static int RX64_HashUpdate(wolfssl_RX64_HW_Hash* hash,
                           const byte* data, word32 sz)
{
    if (hash == NULL || (sz > 0 && data == NULL)) {
        return BAD_FUNC_ARG;
    }

    if (hash->len < hash->used + sz) {
        if (hash->msg == NULL) {
            hash->msg = (byte*)XMALLOC(hash->used + sz, hash->heap,
                    DYNAMIC_TYPE_TMP_BUFFER);
        } else {
            byte* pt = (byte*)XREALLOC(hash->msg, hash->used + sz, hash->heap,
                    DYNAMIC_TYPE_TMP_BUFFER);
            if (pt == NULL) {
                return MEMORY_E;
            }
            hash->msg = pt;
        }
        if (hash->msg == NULL) {
            return MEMORY_E;
        }
        hash->len = hash->used + sz;
    }
    XMEMCPY(hash->msg + hash->used, data , sz);
    hash->used += sz;

    return 0;
}

/**
 * @brief Calculate hash value with the RX64 SHA subsystem and reset the hash.
 *
 * @param[in] hash Structure containing the information on what to hash
 * @param[out] out Sha hash
 * @retval int R_PROCESS_COMPLETE (0) on success.
 * @retval int BAD_FUNC_ARG or see r_sha.h on failure.
 */
static int RX64_HashFinal(wolfssl_RX64_HW_Hash* hash, byte* out)
{
    int ret = R_PROCESS_COMPLETE;
    void* heap;

    if (hash == NULL || out == NULL) {
        return BAD_FUNC_ARG;
    }
    if (hash->sha_type != RX64_SHA1 &&
        hash->sha_type != RX64_SHA224 &&
        hash->sha_type != RX64_SHA256)
    {
        return BAD_FUNC_ARG;
    }

    heap = hash->heap;

    /*
    RX64 HW SHA operations considers empty msgs to be an error,
    though some wolfSSL operations expects to have successful SHA operations on
    empty incoming buffers (e.g DeriveHandshakeSecret()).
    Thus we must support the expected default SHA hash data for this operation
    since TLS decrypt operations expect a specific SW Hash to be used when the
    input buffer was empty/NULL.
    */
    if ((hash->msg == NULL) && (hash->len == 0) && (hash->used == 0))
    {
        if (hash->sha_type == RX64_SHA1)
        {
            XMEMCPY(out, DefaultShaHashData, sizeof(DefaultShaHashData));
        }
        else if (hash->sha_type == RX64_SHA224)
        {
            XMEMCPY(out, DefaultSha224HashData, sizeof(DefaultSha224HashData));
        }
        else if (hash->sha_type == RX64_SHA256)
        {
            XMEMCPY(out, DefaultSha256HashData, sizeof(DefaultSha256HashData));
        }
    }
    else
    {
        /* Utilize RX64 SHA HW Acceleration for normal SHA operations. */
        ret = RX64_ShaCalc(hash->msg, hash->len, out, hash->sha_type);
        if (ret != R_PROCESS_COMPLETE)
        {
            return ret;
        }
    }

    RX64_HashFree(hash);
    return RX64_HashInit(hash, heap, 0, hash->sha_type);
}

/**
 * @brief Calculate hash value with the RX64 SHA subsystem.
 *
 * @param[in] hash Structure containing the information on what to hash
 * @param[out] out Sha hash
 * @retval int R_PROCESS_COMPLETE (0) on success.
 * @retval int BAD_FUNC_ARG or see r_sha.h on failure.
 */
static int RX64_HashGet(wolfssl_RX64_HW_Hash* hash, byte* out)
{
    int ret;

    if (hash == NULL || out == NULL) {
        return BAD_FUNC_ARG;
    }
    if (hash->sha_type != RX64_SHA1 &&
        hash->sha_type != RX64_SHA224 &&
        hash->sha_type != RX64_SHA256)
    {
        return BAD_FUNC_ARG;
    }

    ret = RX64_ShaCalc(hash->msg, hash->len, out, hash->sha_type);
    if (ret != R_PROCESS_COMPLETE) {
        return ret;
    }

    return 0;
}

/**
 * @brief Copy a hash for use with the RX64 SHA subsystem.
 *
 * @param[in] src Source hash structure
 * @param[out] dst Destination hash structure
 * @return int 0 on success, BAD_FUNC_ARG on failure
 */
static int RX64_HashCopy(wolfssl_RX64_HW_Hash* src, wolfssl_RX64_HW_Hash* dst)
{
    if (src == NULL || dst == NULL) {
        return BAD_FUNC_ARG;
    }

    XMEMCPY(dst, src, sizeof(wolfssl_RX64_HW_Hash));

    if (src->len > 0 && src->msg != NULL) {
        dst->msg = (byte*)XMALLOC(src->len, dst->heap, DYNAMIC_TYPE_TMP_BUFFER);
        if (dst->msg == NULL) {
            return MEMORY_E;
        }
        XMEMCPY(dst->msg, src->msg, src->len);
    }

    return 0;
}

/* WolfCrypt wrapper function for RX64 SHA1 Init */
int wc_InitSha_ex(wc_Sha* sha, void* heap, int devId)
{
    return RX64_HashInit((wolfssl_RX64_HW_Hash*)sha, heap, devId, RX64_SHA1);
}
/* WolfCrypt wrapper function for RX64 SHA1 Update */
int wc_ShaUpdate(wc_Sha* sha, const byte* in, word32 sz)
{
    return RX64_HashUpdate((wolfssl_RX64_HW_Hash*)sha, in, sz);
}
/* WolfCrypt wrapper function for RX64 SHA1 Final */
int wc_ShaFinal(wc_Sha* sha, byte* hash)
{
    return RX64_HashFinal((wolfssl_RX64_HW_Hash*)sha, hash);
}
/* WolfCrypt wrapper function for RX64 SHA1 Get */
int wc_ShaGetHash(wc_Sha* sha, byte* hash)
{
    return RX64_HashGet((wolfssl_RX64_HW_Hash*)sha, hash);
}
/* WolfCrypt wrapper function for RX64 SHA1 Copy */
int wc_ShaCopy(wc_Sha* src, wc_Sha* dst)
{
    return RX64_HashCopy((wolfssl_RX64_HW_Hash*)src, (wolfssl_RX64_HW_Hash*)dst);
}

#if defined(WOLFSSL_SHA224)
#include <wolfssl/wolfcrypt/sha256.h>

/* WolfCrypt wrapper function for RX64 SHA224 Init */
int wc_InitSha224_ex(wc_Sha224* sha, void* heap, int devId)
{
    return RX64_HashInit((wolfssl_RX64_HW_Hash*)sha, heap, devId, RX64_SHA224);
}
/* WolfCrypt wrapper function for RX64 SHA224 Update */
int wc_Sha224Update(wc_Sha224* sha, const byte* in, word32 sz)
{
    return RX64_HashUpdate((wolfssl_RX64_HW_Hash*)sha, in, sz);
}
/* WolfCrypt wrapper function for RX64 SHA224 Final */
int wc_Sha224Final(wc_Sha224* sha, byte* hash)
{
    return RX64_HashFinal((wolfssl_RX64_HW_Hash*)sha, hash);
}
/* WolfCrypt wrapper function for RX64 SHA224 Get */
int wc_Sha224GetHash(wc_Sha224* sha, byte* hash)
{
    return RX64_HashGet((wolfssl_RX64_HW_Hash*)sha, hash);
}
/* WolfCrypt wrapper function for RX64 SHA224 Copy */
int wc_Sha224Copy(wc_Sha224* src, wc_Sha224* dst)
{
    return RX64_HashCopy((wolfssl_RX64_HW_Hash*)src, (wolfssl_RX64_HW_Hash*)dst);
}
#endif /* WOLFSSL_SHA224 */

#if !defined(NO_SHA256)
#include <wolfssl/wolfcrypt/sha256.h>

/* WolfCrypt wrapper function for RX64 SHA256 Init */
int wc_InitSha256_ex(wc_Sha256* sha, void* heap, int devId)
{
    return RX64_HashInit((wolfssl_RX64_HW_Hash*)sha, heap, devId, RX64_SHA256);
}
/* WolfCrypt wrapper function for RX64 SHA256 Update */
int wc_Sha256Update(wc_Sha256* sha, const byte* in, word32 sz)
{
    return RX64_HashUpdate((wolfssl_RX64_HW_Hash*)sha, in, sz);
}
/* WolfCrypt wrapper function for RX64 SHA256 Final */
int wc_Sha256Final(wc_Sha256* sha, byte* hash)
{
    return RX64_HashFinal((wolfssl_RX64_HW_Hash*)sha, hash);
}
/* WolfCrypt wrapper function for RX64 SHA256 Get */
int wc_Sha256GetHash(wc_Sha256* sha, byte* hash)
{
    return RX64_HashGet((wolfssl_RX64_HW_Hash*)sha, hash);
}
/* WolfCrypt wrapper function for RX64 SHA256 Copy */
int wc_Sha256Copy(wc_Sha256* src, wc_Sha256* dst)
{
    return RX64_HashCopy((wolfssl_RX64_HW_Hash*)src, (wolfssl_RX64_HW_Hash*)dst);
}
#endif /* !NO_SHA256 */
#endif /* WOLFSSL_RENESAS_RX64_HASH */
#endif /* #if !defined(NO_SHA) || !defined(NO_SHA256) */