forked from gojimmypi/wolfssl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesp32_shaV2.c
872 lines (743 loc) · 23 KB
/
esp32_shaV2.c
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
/* esp32_sha.c
*
* Copyright (C) 2006-2023 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
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <wolfssl/wolfcrypt/settings.h>
/*****************************************************************************/
/* this entire file content is excluded when NO_SHA, NO_SHA256
* or when using WC_SHA384 or WC_SHA512
*/
#if !defined(NO_SHA) || !defined(NO_SHA256) || defined(WC_SHA384) || \
defined(WC_SHA512)
#include "wolfssl/wolfcrypt/logging.h"
/* this entire file content is excluded if not using HW hash acceleration */
#if defined(WOLFSSL_ESP32_CRYPT) && \
!defined(NO_WOLFSSL_ESP32_CRYPT_HASH)
#include <hal/sha_hal.h>
#if !defined(CONFIG_IDF_TARGET_ESP32)
#include <esp_crypto_lock.h>
#endif
#include <wolfssl/wolfcrypt/sha.h>
#include <wolfssl/wolfcrypt/sha256.h>
#include <wolfssl/wolfcrypt/sha512.h>
#include "wolfssl/wolfcrypt/port/Espressif/esp32-crypt.h"
#include "wolfssl/wolfcrypt/error-crypt.h"
#ifdef NO_INLINE
#include <wolfssl/wolfcrypt/misc.h>
#else
#define WOLFSSL_MISC_INCLUDED
#include <wolfcrypt/src/misc.c>
#endif
/*
* Local types
* */
struct CalculationContext
{
/* Accelerator context. NULL if not doing a hardware calculation. */
WC_ESP32SHA* Context;
/* Next value to use for the WC_ESP32SHA calculation token. Never 0.*/
uint32_t NextToken;
};
/* Locally
* undefine SOC_SHA_SUPPORT_RESUME : to test hashing without session resumption
* even on micros that support it.
* define SINGLE_THREADED : for single threading support (useful for printing
* diagnostic messages from within critical sections).
**/
//#undef SOC_SHA_SUPPORT_RESUME
//#define SINGLE_THREADED
/* So we never have to return magic numbers! */
#define SUCCESS (0)
#if !defined(CONFIG_IDF_TARGET_ESP32)
#define SHA_INVALID SHA_TYPE_MAX
#endif
/*
* Local static variables.
* */
static const char* TAG = "wolf_hw_sha";
/* Track the number of times we have enabled the hardware accelerator so
* we can disable it when it isn't used any more. */
static int32_t EnableCount = 0;
/* Context for the Sha calculation the hardware is currently working on. */
static struct CalculationContext HardwareContext = { NULL, 1 };
/* Value used for WC_ESP32SHA when there is no partial result for a Sha in
* the harware. */
const uint32_t CalculationToken_PartialNotInHardwre = 0;
/* If protect fields that could be accessed by multiple threads with a
* critical section. This includes fields in WC_ESP32SHA. */
#if !defined(SINGLE_THREADED)
static portMUX_TYPE sha_crit_sect = portMUX_INITIALIZER_UNLOCKED;
#endif
/*
* Private function prototypes
* */
static void esp_sha_init_ctx(WC_ESP32SHA* ctx);
static void Enter__ShaCriticalSection();
static void Leave__ShaCriticalSection();
static WC_ESP_SHA_TYPE MapHashType(enum wc_HashType hash_type);
static bool CanAccelerate(WC_ESP_SHA_TYPE type);
static bool IsWorkingOn(WC_ESP32SHA* pContext);
static void ClearWorkingOn();
static void StashIntermediateResult();
static void FlipEndian(const word32* pSource, word32* pDestination, size_t szData);
static int HashBlock(const word32* pData, int nBlockSize, WC_ESP32SHA* ctx);
static int RetrieveDigest(WC_ESP32SHA* ctx, word32* pDigest, size_t szDigest);
static int HashAndFinishDigest(const word32* pData, size_t szData, word32* pDigest, size_t szDigest, WC_ESP32SHA* pContext);
static void PrintResult(const char* pchContext, int nReturnValue);
static void PrintHex(const char* pchContext, const byte* pData, size_t szData);
/*
* Public functions. */
/* esp_sha_enable_hw_accelerator
* Enables the sha hardware accelerator. Must call esp_sha_disable_hw_accelerator
* exactly the same number of times.
*
* Returns:
* SUCCESS for succes.
* SHA_HW_FALLBACK : if hardware acceleration is not supported.
* */
int esp_sha_enable_hw_accelerator()
{
int ret = SUCCESS;
bool bLock;
Enter__ShaCriticalSection();
{
bLock = 0 == EnableCount;
++EnableCount;
}
Leave__ShaCriticalSection();
if (bLock)
{
// can't call inside critical section.
periph_module_enable(PERIPH_SHA_MODULE);
#if !defined(CONFIG_IDF_TARGET_ESP32)
esp_crypto_sha_aes_lock_acquire();
#endif
}
assert(SUCCESS == ret || SHA_HW_FALLBACK == ret);
return ret;
}
/* esp_sha_enable_hw_accelerator
* Disable the sha hardware accelerator. Must call esp_sha_enable_hw_accelerator
* exactly the same number of times.
*
* Returns:
* 0 for succes.
* -1 if hardware acceleration is not supported.
* */
int esp_sha_disable_hw_accelerator()
{
int ret = SUCCESS;
bool bUnlock;
Enter__ShaCriticalSection();
// Too many disables?
assert(EnableCount > 0);
--EnableCount;
bUnlock = 0 == EnableCount;
Leave__ShaCriticalSection();
if (bUnlock)
{
// can't call inside critical section.
#if !defined(CONFIG_IDF_TARGET_ESP32)
esp_crypto_sha_aes_lock_release();
#endif
periph_module_disable(PERIPH_SHA_MODULE);
}
assert(SUCCESS == ret || SHA_HW_FALLBACK == ret);
return ret;
}
/* esp_sha_init
**
** ctx: any wolfSSL ctx from any hash algo
** hash_type: the specific wolfSSL enum for hash type
** returns: 0 on success (never fails).
**
** Initializes ctx based on chipset capabilities and current state.
** Active HW states, such as from during a copy operation, are demoted to SW.
** For hash_type not available in HW, set SW mode.
**
** See esp_sha_init_ctx(ctx)
*/
int esp_sha_init_2(WC_ESP32SHA* ctx, enum wc_HashType hash_type)
{
int ret = 0;
ctx->sha_type = MapHashType(hash_type);
if (SHA_INVALID == ctx->sha_type)
{
ESP_LOGW(TAG, "Unexpected hash_type in esp_sha_init");
}
if (CanAccelerate(ctx->sha_type))
{
bool bAcceleratorEnabled;
Enter__ShaCriticalSection();
{
bAcceleratorEnabled = EnableCount > 0;
if (bAcceleratorEnabled)
{
ctx->can_accelerate = true;
esp_sha_init_ctx(ctx);
}
else
{
ctx->can_accelerate = false;
}
}
Leave__ShaCriticalSection();
if (!bAcceleratorEnabled)
{
ESP_LOGW(TAG, "hardware acceleration disabled in esp_sha_init");
}
}
else
{
ctx->can_accelerate = false;
}
return ret;
}
int esp_sha_free_2(WC_ESP32SHA* ctx)
{
Enter__ShaCriticalSection();
{
if (IsWorkingOn(ctx))
{
ClearWorkingOn();
}
ctx->can_accelerate = false;
}
Leave__ShaCriticalSection();
return SUCCESS;
}
/*
** esp_sha_ctx_copy
** Copy hardware context information (other places take care of copy the wc_Sha data).
*/
int esp_sha_ctx_copy_2(struct wc_Sha* src, struct wc_Sha* dst)
{
int ret = SUCCESS;
#if SOC_SHA_SUPPORT_RESUME
WC_ESP32SHA* pSource = &src->ctx;
WC_ESP32SHA* pDestination = &dst->ctx;
Enter__ShaCriticalSection();
{
if (IsWorkingOn(pSource))
{
sha_hal_wait_idle();
sha_hal_read_digest(pDestination->sha_type, pDestination->partial_result);
}
else
{
assert(CalculationToken_PartialNotInHardwre == pSource->calculation_token);
memcpy(pDestination->partial_result, pSource->partial_result, WC_SHA_DIGEST_SIZE);
}
pDestination->calculation_token = CalculationToken_PartialNotInHardwre;
pDestination->can_accelerate = pSource->can_accelerate;
pDestination->isfirstblock = pSource->isfirstblock;
}
Leave__ShaCriticalSection();
assert(HardwareContext.Context == NULL || HardwareContext.Context->calculation_token != CalculationToken_PartialNotInHardwre);
#else
WC_ESP32SHA* pSource = &src->ctx;
WC_ESP32SHA* pDestination = &dst->ctx;
Enter__ShaCriticalSection();
{
// Without resumption support we can only have one sha accelerated at a time
// so even if src is being accelerated with an intermediate result in the
// hardware, the destination is not in hardware because dst will have a
// different memory location for the digest.
pDestination->calculation_token = CalculationToken_PartialNotInHardwre;
pDestination->can_accelerate = pSource->can_accelerate;
pDestination->isfirstblock = pSource->isfirstblock;
}
Leave__ShaCriticalSection();
#endif
return ret;
} /* esp_sha_ctx_copy */
#ifndef NO_SHA
/*
** sha1 process
**
** sha: context for hash operation
** data: data to hash.
**
** returns:
** SUCCESS : Hash operation completed.
** SHA_HW_FALLBACK : Unable to do the hash operation. Fallback to software.
*/
int esp_sha_process_2(struct wc_Sha* sha, const byte* data)
{
int ret;
ESP_LOGV(TAG, "enter esp_sha_process");
static_assert(sizeof(HardwareContext.Context->partial_result) >= WC_SHA_DIGEST_SIZE, "partial result buffer too small");
ret = HashBlock((const word32*)data, WC_SHA_BLOCK_SIZE, &sha->ctx);
ESP_LOGV(TAG, "leave esp_sha_process");
return ret;
} /* esp_sha_process */
/*
** retrieve sha1 digest
*/
int esp_sha_finish_digest_2(struct wc_Sha* sha, byte blockprocess)
{
int ret = SUCCESS;
ESP_LOGV(TAG, "enter esp_sha_digest_process");
static_assert(sizeof(HardwareContext.Context->partial_result) >= WC_SHA_DIGEST_SIZE, "partial result buffer too small");
ret = HashAndFinishDigest(blockprocess ? sha->buffer : NULL, WC_SHA_BLOCK_SIZE, sha->digest, WC_SHA_DIGEST_SIZE, &sha->ctx);
ESP_LOGV(TAG, "leave esp_sha_digest_process");
return ret;
} /* esp_sha_digest_process */
#endif /* NO_SHA */
/*
* Private functions. */
void Enter__ShaCriticalSection()
{
#if !defined(SINGLE_THREADED)
taskENTER_CRITICAL(&sha_crit_sect);
#endif
}
void Leave__ShaCriticalSection()
{
#if !defined(SINGLE_THREADED)
taskEXIT_CRITICAL(&sha_crit_sect);
#endif
}
/* MapHashType
* Translates WolfSSL hash type enumeration to Espressif hash type enumeration.
* Returns SHA_TYPE_MAX for unsupported types. */
WC_ESP_SHA_TYPE MapHashType(enum wc_HashType hash_type)
{
switch (hash_type) { /* check each wolfSSL hash type WC_[n] */
#if !defined(NO_SHA)
case WC_HASH_TYPE_SHA:
return SHA1;
#endif
#if !defined(NO_SHA256)
case WC_HASH_TYPE_SHA256:
return SHA2_256;
#endif
#if defined(WC_SHA384)
case WC_HASH_TYPE_SHA384:
return SHA2_384;
#endif
#if defined(WC_SHA512)
case WC_HASH_TYPE_SHA512:
return SHA2_512;
#endif
#ifndef WOLFSSL_NOSHA512_224
case WC_HASH_TYPE_SHA512_224:
return SHA2_512;
#endif
#ifndef WOLFSSL_NOSHA512_256
case WC_HASH_TYPE_SHA512_256:
return SHA2_512;
#endif
default:
return SHA_INVALID;
}
}
/* CanAccelerate
* Returns true iff the target hardware can accelerate the hash type supplied. */
bool CanAccelerate(WC_ESP_SHA_TYPE type)
{
switch (type) {
#if SOC_SHA_SUPPORT_SHA1 && !defined(NO_SHA)
case SHA1:
return true;
#endif
#if SOC_SHA_SUPPORT_SHA224
case SHA2_224:
return true;
#endif
#if SOC_SHA_SUPPORT_SHA256 && !defined(NO_SHA256)
case SHA2_256:
return true;
#endif
#if SOC_SHA_SUPPORT_SHA384 && defined(WC_SHA384)
case SHA2_384:
return true;
#endif
#if SOC_SHA_SUPPORT_SHA512 && defined(WC_SHA512)
case SHA2_512:
return true;
#endif
#if SOC_SHA_SUPPORT_SHA512_T && defined(WOLFSSL_NOSHA512_224)
case SHA2_512224:
return true;
#endif
#if SOC_SHA_SUPPORT_SHA512_T && defined(WOLFSSL_NOSHA512_256)
case SHA2_512256:
case SHA2_512T:
return true;
#endif
case SHA_INVALID:
default:
return false;
}
return false;
}
/* esp_sha_init_ctx
* Initialize the context we use to keep track of sha hardware acceleration. */
static void esp_sha_init_ctx(WC_ESP32SHA* ctx)
{
// Must have a valid sha type at this point.
assert(ctx->sha_type != SHA_INVALID);
assert(CanAccelerate(ctx->sha_type));
memset(ctx->partial_result, 0, sizeof(ctx->partial_result));
ctx->calculation_token = CalculationToken_PartialNotInHardwre;
ctx->isfirstblock = true;
// may not be needed?
ctx->initializer = NULL;
ctx->lockDepth = 0;
} /* esp_sha_init_ctx */
/* GetNextCalculationToken
*
* Retrieves a new calculation token. Never 0 and unique until
* uint32_t wraps.
* */
uint32_t GetNextCalculationToken()
{
++HardwareContext.NextToken;
if (0 == HardwareContext.NextToken)
{
// valid tokens are never 0.
HardwareContext.NextToken = 1;
}
return HardwareContext.NextToken;
}
/* IsWorkingOn
*
* Returns true iff the hardware engine is currently computing or holding a partial
* hash result for pContext.
**/
bool IsWorkingOn(WC_ESP32SHA* pContext)
{
if (NULL == pContext)
{
assert(false);
return false;
}
if (NULL == HardwareContext.Context)
{
return false;
}
// Calculation from context in hardware?
uint32_t uContextToken = pContext->calculation_token;
uint32_t uInProcessToken = HardwareContext.Context->calculation_token;
assert(uInProcessToken != CalculationToken_PartialNotInHardwre);
return uContextToken == uInProcessToken && uContextToken != CalculationToken_PartialNotInHardwre;
}
/* ClearWorkingOn
*
* Clear record of which context the hardware accelerator is working on. Frees the
* hardware for another calculation.
**/
void ClearWorkingOn()
{
if (NULL != HardwareContext.Context)
{
HardwareContext.Context->calculation_token = CalculationToken_PartialNotInHardwre;
HardwareContext.Context = NULL;
}
}
/* ContinueOrAvailable
*
* Returns true iff the hardware accelerator is working on nothing or pContext
* */
bool ContinueOrAvailable(WC_ESP32SHA* pContext)
{
assert(NULL != pContext);
return NULL == HardwareContext.Context || IsWorkingOn(pContext);
}
/* StashIntermediateResult
*
* Stash the partial result stored in the hardware accelerator in a temporary buffer so
* the calculation can be resumed later.
**/
void StashIntermediateResult()
{
assert(NULL != HardwareContext.Context);
if (NULL != HardwareContext.Context)
{
sha_hal_read_digest(HardwareContext.Context->sha_type, HardwareContext.Context->partial_result);
HardwareContext.Context->calculation_token = CalculationToken_PartialNotInHardwre;
HardwareContext.Context = NULL;
}
}
/* RestoreIntermediateResult
*
* Restore a partial result from a previously stashed calculation so the hash can be resumed.
**/
#if defined(SOC_SHA_SUPPORT_RESUME)
void RestoreIntermediateResult(WC_ESP32SHA* ctx)
{
assert(NULL == HardwareContext.Context);
assert(ctx->can_accelerate);
assert(CalculationToken_PartialNotInHardwre == ctx->calculation_token);
if (!ctx->isfirstblock)
{
// no history from first block, so only subsequent ones need to be loaded.
sha_hal_write_digest(ctx->sha_type, ctx->partial_result);
}
ctx->calculation_token = GetNextCalculationToken();
assert(CalculationToken_PartialNotInHardwre != ctx->calculation_token);
// we save the context and the digest store in case another sha
// operation is required before this one is all done.
HardwareContext.Context = ctx;
}
#endif
/* SetAccelerationContext
*
* Set the context currently being accelerated by hardware, provided none is using the hardware.
**/
void SetAccelerationContext(WC_ESP32SHA* pContext)
{
assert(NULL != pContext);
assert(NULL == HardwareContext.Context || pContext->calculation_token == HardwareContext.Context->calculation_token);
if (HardwareContext.Context == NULL)
{
assert(pContext->calculation_token == CalculationToken_PartialNotInHardwre);
HardwareContext.Context = pContext;
pContext->calculation_token = GetNextCalculationToken();
assert(CalculationToken_PartialNotInHardwre != pContext->calculation_token);
}
}
/* FlipEndian
*
* Swaps byte order for words in pSource, writing filped bytes into
* pDestination. pSource and pDestination may point to the same memory
* location.
* pSource: data to be flipped
* pDestination: output buffer (may be same as pSource)
* szData: number of _bytes_ to reorder.
**/
void FlipEndian(const word32* pSource, word32* pDestination, size_t szData)
{
ByteReverseWords(pDestination, pSource, szData);
}
/*
* HashBlock
*
* Starts hashing data using the hardware accelerator, if possible.
* This may stash the intermediate result from another hash in its digest store
* while we use the hardware for this calculation. The hardware accelerator may
* still be working on the hash, in parallel, when this function finishes.
*
* IMPORTANT NOTE:
* The digest store and context are retained to support future operations. For
* example, if another hash is requested the results for the ctx hash may be
* stashed temporarily in the pDigestStore until the next operation for ctx. At
* that point, its partial result will be reloaded into the hardware before
* continuing with the hash operation.
*
* pData: the data block to hash.
* nBlockSize: size of the datablock in bytes (must be suitable for the hash being computed)
* ctx: hardware acceleration context for the hash
*
* Returns:
* SUCCESS : the data was hashed.
* SHA_HW_FALLBACK : hardware wasn't available to do the hash. find another way.
*
**/
int HashBlock(const word32* pData, int nBlockSize, WC_ESP32SHA* ctx)
{
int ret = SHA_HW_FALLBACK;
assert(NULL != pData);
assert(NULL != ctx);
if (NULL == pData || NULL == ctx)
{
return BAD_FUNC_ARG;
}
if (!ctx->can_accelerate)
{
return SHA_HW_FALLBACK;
}
#if SOC_SHA_SUPPORT_RESUME
Enter__ShaCriticalSection();
{
sha_hal_wait_idle();
if (HardwareContext.Context == NULL)
{
RestoreIntermediateResult(ctx);
}
else if (!IsWorkingOn(ctx))
{
StashIntermediateResult();
RestoreIntermediateResult(ctx);
}
word32 aTemp[nBlockSize / sizeof(word32)];
FlipEndian(pData, aTemp, nBlockSize);
sha_hal_hash_block(ctx->sha_type, aTemp, nBlockSize / sizeof(word32), ctx->isfirstblock);
ctx->isfirstblock = false;
}
Leave__ShaCriticalSection();
ret = SUCCESS;
assert(HardwareContext.Context == NULL || HardwareContext.Context->calculation_token != CalculationToken_PartialNotInHardwre);
return ret;
#else
Enter__ShaCriticalSection();
{
bool bCanAccelerate = ContinueOrAvailable(ctx);
if (bCanAccelerate && HardwareContext.Context != ctx)
{
assert(NULL == HardwareContext.Context);
assert(ctx->isfirstblock);
SetAccelerationContext(ctx);
}
if (bCanAccelerate)
{
word32 aTemp[nBlockSize / sizeof(word32)];
FlipEndian(pData, aTemp, nBlockSize);
sha_hal_hash_block(ctx->sha_type, aTemp, nBlockSize / sizeof(word32), ctx->isfirstblock);
ctx->isfirstblock = false;
ret = SUCCESS;
}
else
{
ret = SHA_HW_FALLBACK;
}
}
Leave__ShaCriticalSection();
return ret;
#endif
}
/* HashAndFinishDigest
*
* Optionally hash a block of data and retrieve the hash of this block and all
* previous blocks. Completes digest by clearing hardware context.
* pData, szData: data & its length (in bytes) to hash before returning the digest
* if not NULL,
* pDigest, szDigest: destination for the digest.
* pContext: accelerator context for the hash.
*
* Returns:
* SUCCESS : if the hash is completed successfully.
* SHA_HW_FALLBACK : if the hash could not be completed with the hardware and
* must be performed in software.
* BAD_FUNC_ARG : if pDigest or pContext are null or szDigest is 0.
**/
int HashAndFinishDigest(const word32* pData, size_t szData, word32* pDigest, size_t szDigest, WC_ESP32SHA* pContext)
{
int ret = SUCCESS;
if (NULL == pDigest || NULL == pContext || 0 == szDigest)
{
return BAD_FUNC_ARG;
}
if (NULL != pData)
{
ret = HashBlock(pData, szData, pContext);
}
if (SUCCESS == ret)
{
ret = RetrieveDigest(pContext, pDigest, szDigest);
}
// This is needed because callers reinitialize the sha context so we need to
// make sure we aren't in the accelerator still. Removing this would allow
// intermediate hash results to be retrieved, but that isn't needed.
esp_sha_free_2(pContext);
assert(SUCCESS == ret || SHA_HW_FALLBACK == ret || BAD_FUNC_ARG == ret);
return ret;
}
/* RetrieveDigest
*
* Retrieves the hash digest for the context provided. The digets may come
* from hardware registers or prior stashed result (if another sha calculation
* required the hardware in between times).
*
* Returns:
* SUCCESS : if the digest is retrieved successfully.
* SHA_HW_FALLBACK : if the hash could not be retrieved with the hardware and
* must be performed in software.
* BAD_FUNC_ARG : if pDigest or pContext are null or szDigest is 0.
**/
int RetrieveDigest(WC_ESP32SHA* ctx, word32* pDigestStore, size_t szDigest)
{
assert(NULL != ctx);
assert(NULL != pDigestStore);
if (NULL == ctx || NULL == pDigestStore)
{
return BAD_FUNC_ARG;
}
if (!ctx->can_accelerate)
{
return SHA_HW_FALLBACK;
}
#if SOC_SHA_SUPPORT_RESUME
Enter__ShaCriticalSection();
{
if (IsWorkingOn(ctx))
{
sha_hal_wait_idle();
sha_hal_read_digest(ctx->sha_type, pDigestStore);
}
else
{
assert(CalculationToken_PartialNotInHardwre == ctx->calculation_token);
memcpy(pDigestStore, ctx->partial_result, szDigest);
}
FlipEndian(pDigestStore, pDigestStore, szDigest);
}
Leave__ShaCriticalSection();
assert(HardwareContext.Context == NULL || HardwareContext.Context->calculation_token != CalculationToken_PartialNotInHardwre);
return SUCCESS;
#else
int ret = SUCCESS;
Enter__ShaCriticalSection();
{
assert(IsWorkingOn(ctx));
if (IsWorkingOn(ctx))
{
sha_hal_wait_idle();
sha_hal_read_digest(ctx->sha_type, pDigestStore);
FlipEndian(pDigestStore, pDigestStore, szDigest);
}
else
{
ret = SHA_HW_INTERNAL_ERROR;
}
}
Leave__ShaCriticalSection();
return ret;
#endif
}
static void PrintResult(const char* pchContext, int ret)
{
if (SUCCESS == ret)
{
printf("%s: hardware\n", pchContext);
}
else if (SHA_HW_FALLBACK == ret)
{
printf("%s: software hash\n", pchContext);
}
else
{
printf("%s: error %d\n", pchContext, ret);
}
}
static void PrintHex(const char* pchContext, const byte* pData, size_t szData)
{
printf("%s - ", pchContext);
while (szData--)
{
printf("%02X-", *pData++);
}
printf("\n");
}
#endif /* WOLFSSL_ESP32_CRYPT */
#endif /* !defined(NO_SHA) ||... */