forked from Microchip-MPLAB-Harmony/crypto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypto.h
2859 lines (2117 loc) · 87.1 KB
/
crypto.h
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
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**************************************************************************
Crypto Framework Library Header
Company:
Microchip Technology Inc.
File Name:
crypto.h
Summary:
Crypto Framework Library header for cryptographic functions.
Description:
This header file contains function prototypes and definitions of
the data types and constants that make up the Cryptographic Framework
Library for PIC32 families of Microchip microcontrollers.
**************************************************************************/
//DOM-IGNORE-BEGIN
/*****************************************************************************
Copyright (C) 2013-2018 Microchip Technology Inc. and its subsidiaries.
Microchip Technology Inc. and its subsidiaries.
Subject to your compliance with these terms, you may use Microchip software
and any derivatives exclusively with Microchip products. It is your
responsibility to comply with third party license terms applicable to your
use of third party software (including open source software) that may
accompany Microchip software.
THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER
EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED
WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A PARTICULAR
PURPOSE.
IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE,
INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND
WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS
BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE
FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN
ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY,
THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
*****************************************************************************/
//DOM-IGNORE-END
/* Defines Microchip CRYPTO API layer */
#ifndef MC_CRYPTO_API_H
#define MC_CRYPTO_API_H
#ifdef __cplusplus
extern "C" {
#endif
/* MD5 */
typedef struct CRYPT_MD5_CTX {
int holder[110]; /* This structure should be large enough to hold
the internal representation, the size is checked
during initialization*/
} CRYPT_MD5_CTX;
//******************************************************************************
/* Function:
int CRYPT_MD5_Initialize(CRYPT_MD5_CTX* md5)
Summary:
Initializes the internal structures necessary for MD5 hash calculations.
Description:
This function initializes the internal structures necessary for MD5 hash calculations.
Precondition:
None.
Parameters:
md5 - Pointer to CRYPT_MD5_CTX structure which holds the hash values.
Returns:
- BAD_FUNC_ARG - An invalid pointer was passed to the function.
- 0 - An invalid pointer was not passed to the function.
Example:
<code>
CRYPT_MD5_CTX md5;
uint8_t buffer[1024];
uint8_t md5sum[MD5_DIGEST_SIZE];
CRYPT_MD5_Initialize(&md5);
CRYPT_MD5_DataAdd(&md5, buffer, sizeof(buffer));
CRYPT_MD5_Finalize(&md5, md5sum);
</code>
Remarks:
All MD5 hashes have to start at a particular value before adding new data
to it. This function sets the necessary values for the structure.
*/
int CRYPT_MD5_Initialize(CRYPT_MD5_CTX*);
//******************************************************************************
/* Function:
int CRYPT_MD5_DataSizeSet(CRYPT_MD5_CTX* md5, unsigned int msgSize)
Summary:
This function sets the size of the input data for use with hardware accelerated
encryption.
Description:
The hardware encryption module needs to know the size of the data
before it starts processing. This function sets that value.
Precondition:
None.
Parameters:
md5 - Pointer to CRYPT_MD5_CTX structure which holds the hash values.
msgSize - Size of the data (in bytes) that will be processed.
Returns:
- BAD_FUNC_ARG - An invalid pointer was passed to the function.
- 0 - An invalid pointer was not passed to the function.
Example:
<code>
CRYPT_MD5_CTX md5;
uint8_t buffer[1024];
uint8_t md5sum[MD5_DIGEST_SIZE];
CRYPT_MD5_Initialize(&md5);
CRYPT_MD5DataSizeSet(&md5, sizeof(buffer));
CRYPT_MD5_DataAdd(&md5, buffer, sizeof(buffer));
CRYPT_MD5_Finalize(&md5, md5sum);
</code>
Remarks:
All MD5 hashes have to start at a particular value before adding new data
to it. This function sets the necessary values for the structure.
*/
int CRYPT_MD5_DataSizeSet(CRYPT_MD5_CTX*, unsigned int);
//********************************************************************************************
/*
Function:
int CRYPT_MD5_DataAdd(CRYPT_MD5_CTX* md5, const unsigned char* input, unsigned int sz)
Summary:
Updates the hash with the data provided.
Description:
This function updates the hash with the data provided.
Preconditions:
The MD5 context must be initialized prior to the first call of this
function. The context must not be modified by code outside of this
function.
Parameters:
md5 - Pointer to CRYPT_MD5_CTX structure which holds the hash values.
input - Pointer to the data to use to update the hash.
sz - Size of the data (in bytes) of the data to use to update the hash.
Returns:
* BAD_FUNC_ARG - An invalid pointer was passed to the function, either in md5 or input
* 0 - An invalid pointer was not passed to the function
Example:
<code>
CRYPT_MD5_CTX md5;
uint8_t buffer[1024];
uint8_t md5sum[MD5_DIGEST_SIZE];
CRYPT_MD5_Initialize(&md5);
CRYPT_MD5_DataAdd(&md5, buffer, sizeof(buffer));
CRYPT_MD5_Finalize(&md5, md5sum);
</code>
Remarks:
To preserve the validity of the MD5 hash, nothing must modify
the context holding variable between calls to CRYPT_MD5_DataAdd.
*/
int CRYPT_MD5_DataAdd(CRYPT_MD5_CTX*, const unsigned char*, unsigned int);
//******************************************************************************
/* Function:
int CRYPT_MD5_Finalize(CRYPT_MD5_CTX* md5, unsigned char* digest)
Summary:
Finalizes the hash and puts the result into digest.
Description:
This function finalizes the hash and puts the result into digest.
Precondition:
The MD5 context must be initialized prior to calling this function.
The context must not be modified by code outside of this function.
Parameters:
md5 - Pointer to CRYPT_MD5_CTX structure which holds the hash values.
digest - Pointer to byte array to store hash result.
Returns:
- BAD_FUNC_ARG - An invalid pointer was passed to the function, either in md5 or digest.
- 0 - An invalid pointer was not passed to the function.
Example:
<code>
CRYPT_MD5_CTX md5;
uint8_t buffer[1024];
uint8_t md5sum[MD5_DIGEST_SIZE];
CRYPT_MD5_Initialize(&md5);
CRYPT_MD5_DataAdd(&md5, buffer, sizeof(buffer));
CRYPT_MD5_Finalize(&md5, md5sum);
</code>
Remarks:
In order to preserve the validity of the MD5 hash, nothing must modify the
context holding variable between calls to CRYPT_MD5_DataAdd and CRYPT_MD5_Finalize.
*/
int CRYPT_MD5_Finalize(CRYPT_MD5_CTX*, unsigned char*);
enum {
CRYPT_MD5_DIGEST_SIZE = 16
};
/* SHA */
typedef struct CRYPT_SHA_CTX
{
/* This structure should be large enough to hold the internal representation, the size
is checked during initialization*/
int holder[110] __attribute__((aligned (8)));
} CRYPT_SHA_CTX;
//******************************************************************************
/* Function:
int CRYPT_SHA_Initialize(CRYPT_SHA_CTX* sha)
Summary:
Initializes the internal structures necessary for SHA hash calculations.
Description:
This function initializes the internal structures necessary for SHA hash calculations.
Precondition:
None.
Parameters:
sha - Pointer to CRYPT_SHA_CTX structure which holds the hash values.
Returns:
- BAD_FUNC_ARG - An invalid pointer was passed to the function.
- 0 - An invalid pointer was not passed to the function.
Example:
<code>
CRYPT_SHA_CTX sha;
uint8_t shaSum[SHA_DIGEST_SIZE];
CRYPT_SHA_Initialize(&sha);
CRYPT_SHA_DataAdd(&sha, buffer, sizeof(buffer));
CRYPT_SHA_Finalize(&sha, shaSum);
</code>
Remarks:
All SHA hashes have to start at a particular value before adding new data
to it. This function sets the necessary values for the structure.
*/
int CRYPT_SHA_Initialize(CRYPT_SHA_CTX*);
//******************************************************************************
/* Function:
int CRYPT_SHA_DataSizeSet(CRYPT_SHA_CTX* sha, unsigned int msgSize)
Summary:
For PIC32MZ hardware encryption, sets the size of the input data.
Description:
The PIC32MZ hardware encryption module needs to know the size of the data
before it starts processing. This function sets that value.
Precondition:
None.
Parameters:
sha - Pointer to CRYPT_SHA_CTX structure which holds the hash values.
msgSize - Size of the data (in bytes) that will be processed.
Returns:
- BAD_FUNC_ARG - An invalid pointer was passed to the function.
- 0 - An invalid pointer was not passed to the function.
Example:
<code>
CRYPT_SHA_CTX sha;
uint8_t buffer[1024];
uint8_t shasum[SHA_DIGEST_SIZE];
CRYPT_SHA_Initialize(&sha);
CRYPT_SHADataSizeSet(&sha, sizeof(buffer));
CRYPT_SHA_DataAdd(&sha, buffer, sizeof(buffer));
CRYPT_SHA_Finalize(&sha, shasum);
</code>
Remarks:
All SHA hashes have to start at a particular value before adding new data
to it. This function sets the necessary values for the structure.
*/
int CRYPT_SHA_DataSizeSet(CRYPT_SHA_CTX*, unsigned int);
//******************************************************************************
/* Function:
int CRYPT_SHA_DataAdd(CRYPT_SHA_CTX* sha, const unsigned char* input, unsigned int sz)
Summary:
Updates the hash with the data provided.
Description:
This function updates the hash with the data provided.
Precondition:
The SHA context must be initialized prior to the first call of this function.
The context must not be modified by code outside of this function.
Parameters:
sha - Pointer to CRYPT_SHA_CTX structure which holds the hash values.
input - Pointer to the data to use to update the hash.
sz - Size of the data (in bytes) of the data to use to update the hash.
Returns:
- BAD_FUNC_ARG - An invalid pointer was passed to the function, either in sha or input.
- 0 - An invalid pointer was not passed to the function.
Example:
<code>
CRYPT_SHA_CTX sha;
uint8_t buffer[1024];
uint8_t shaSum[SHA_DIGEST_SIZE];
CRYPT_SHA_Initialize(&sha);
CRYPT_SHA_DataAdd(&sha, buffer, sizeof(buffer));
CRYPT_SHA_Finalize(&sha, shaSum);
</code>
Remarks:
In order to preserve the validity of the SHA hash, nothing must modify the
context holding variable between calls to CRYPT_SHA_DataAdd.
*/
int CRYPT_SHA_DataAdd(CRYPT_SHA_CTX*, const unsigned char*, unsigned int);
//******************************************************************************
/* Function:
int CRYPT_SHA_Finalize(CRYPT_SHA_CTX* sha, unsigned char* digest)
Summary:
Finalizes the hash and puts the result into digest.
Description:
This function finalizes the hash and puts the result into digest.
Precondition:
The SHA context must be initialized prior to calling this function.
The context must not be modified by code outside of this function.
Parameters:
sha - Pointer to CRYPT_SHA_CTX structure which holds the hash values.
digest - Pointer to byte array to store hash result.
Returns:
- BAD_FUNC_ARG - An invalid pointer was passed to the function, either in sha or digest.
- 0 - An invalid pointer was not passed to the function.
Example:
<code>
CRYPT_SHA_CTX sha;
uint8_t buffer[1024];
uint8_t shaSum[SHA_DIGEST_SIZE];
CRYPT_SHA_Initialize(&sha);
CRYPT_SHA_DataAdd(&sha, buffer, sizeof(buffer));
CRYPT_SHA_Finalize(&sha, shaSum);
</code>
Remarks:
In order to preserve the validity of the SHA hash, nothing must modify the
context holding variable between calls to CRYPT_SHA_DataAdd and CRYPT_SHA_Finalize.
*/
int CRYPT_SHA_Finalize(CRYPT_SHA_CTX*, unsigned char*);
enum {
CRYPT_SHA_DIGEST_SIZE = 20
};
/* SHA-256 */
typedef struct CRYPT_SHA256_CTX {
/* This structure should be large enough to hold the internal representation, the size
is checked during initialization*/
int holder[110] __attribute__((aligned (8)));
} CRYPT_SHA256_CTX;
//******************************************************************************
/* Function:
int CRYPT_SHA256_Initialize(CRYPT_SHA256_CTX* sha256)
Summary:
Initializes the internal structures necessary for SHA256 hash calculations.
Description:
This function initializes the internal structures necessary for SHA256 hash calculations.
Precondition:
None.
Parameters:
sha256 - Pointer to context which saves state between calls.
Returns:
- BAD_FUNC_ARG - An invalid pointer was passed to the function.
- 0 - An invalid pointer was not passed to the function.
Example:
<code>
CRYPT_SHA256_CTX sha;
uint8_t shaSum[SHA256_DIGEST_SIZE];
CRYPT_SHA256_Initialize(&sha);
CRYPT_SHA256_DataAdd(&sha, buffer, sizeof(buffer));
CRYPT_SHA256_Finalize(&sha, shaSum);
</code>
Remarks:
All SHA hashes have to start at a particular value before adding new data
to it. This function sets the necessary values for the structure.
*/
int CRYPT_SHA256_Initialize(CRYPT_SHA256_CTX*);
//******************************************************************************
/* Function:
int CRYPT_SHA256_DataSizeSet(CRYPT_SHA256_CTX* sha256, unsigned int msgSize)
Summary:
For PIC32MZ hardware encryption, sets the size of the input data.
Description:
The PIC32MZ hardware encryption module needs to know the size of the data
before it starts processing. This function sets that value.
Precondition:
None.
Parameters:
sha256 - Pointer to CRYPT_SHA256_CTX structure which holds the hash values.
msgSize - Size of the data (in bytes) that will be processed.
Returns:
- BAD_FUNC_ARG - An invalid pointer was passed to the function.
- 0 - An invalid pointer was not passed to the function.
Example:
<code>
CRYPT_SHA256_CTX sha256;
uint8_t buffer[1024];
uint8_t sha256sum[SHA256_DIGEST_SIZE];
CRYPT_SHA256_Initialize(&sha256);
CRYPT_SHA256DataSizeSet(&sha256, sizeof(buffer));
CRYPT_SHA256_DataAdd(&sha256, buffer, sizeof(buffer));
CRYPT_SHA256_Finalize(&sha256, sha256sum);
</code>
Remarks:
All SHA256 hashes have to start at a particular value before adding new data
to it. This function sets the necessary values for the structure.
*/
int CRYPT_SHA256_DataSizeSet(CRYPT_SHA256_CTX*, unsigned int);
//******************************************************************************
/* Function:
int CRYPT_SHA256_DataAdd(CRYPT_SHA256_CTX* sha256, const unsigned char* input, unsigned int sz)
Summary:
Updates the hash with the data provided.
Description:
This function updates the hash with the data provided.
Precondition:
The SHA256 context must be initialized prior to the first call of this function.
The context must not be modified by code outside of this function.
Parameters:
sha256 - Pointer to CRYPT_SHA256_CTX structure which holds the hash values.
input - Pointer to the data to use to update the hash.
sz - Size of the data (in bytes) of the data to use to update the hash.
Returns:
- BAD_FUNC_ARG - An invalid pointer was passed to the function, either in sha256 or input.
- 0 - An invalid pointer was not passed to the function.
Example:
<code>
CRYPT_SHA256_CTX sha256;
uint8_t buffer[1024];
uint8_t shaSum[SHA256_DIGEST_SIZE];
CRYPT_SHA256_Initialize(&sha256);
CRYPT_SHA256_DataAdd(&sha256, buffer, sizeof(buffer));
CRYPT_SHA256_Finalize(&sha256, shaSum);
</code>
Remarks:
In order to preserve the validity of the SHA256 hash, nothing must modify the
context holding variable between calls to CRYPT_SHA256_DataAdd.
*/
int CRYPT_SHA256_DataAdd(CRYPT_SHA256_CTX*, const unsigned char*, unsigned int);
//******************************************************************************
/* Function:
int CRYPT_SHA256_Finalize(CRYPT_SHA256_CTX* sha256, unsigned char* digest)
Summary:
Finalizes the hash and puts the result into digest.
Description:
This function finalizes the hash and puts the result into digest.
Precondition:
The SHA256 context must be initialized prior to calling this function.
The context must not be modified by code outside of this function.
Parameters:
sha256 - Pointer to CRYPT_SHA256_CTX structure which holds the hash values.
digest - Pointer to byte array to store hash result.
Returns:
- BAD_FUNC_ARG - An invalid pointer was passed to the function, either in sha or digest.
- 0 - An invalid pointer was not passed to the function.
Example:
<code>
CRYPT_SHA256_CTX sha256;
uint8_t buffer[1024];
uint8_t shaSum[SHA256_DIGEST_SIZE];
CRYPT_SHA256_Initialize(&sha256);
CRYPT_SHA256_DataAdd(&sha256, buffer, sizeof(buffer));
CRYPT_SHA256_Finalize(&sha256, shaSum);
</code>
Remarks:
In order to preserve the validity of the SHA256 hash, nothing must modify the
context holding variable between calls to CRYPT_SHA256_DataAdd and CRYPT_SHA256_Finalize.
*/
int CRYPT_SHA256_Finalize(CRYPT_SHA256_CTX*, unsigned char*);
enum {
CRYPT_SHA256_DIGEST_SIZE = 32
};
//******************************************************************************
/* Function:
int CRYPT_SHA224_Initialize(CRYPT_SHA256_CTX* sha224)
Summary:
Initializes the internal structures necessary for SHA224 hash calculations.
Description:
This function initializes the internal structures necessary for SHA224
hash calculations.
Precondition:
None.
Parameters:
sha224 - Pointer to context which saves state between calls.
Returns:
- BAD_FUNC_ARG - An invalid pointer was passed to the function.
- 0 - An invalid pointer was not passed to the function.
Example:
<code>
CRYPT_SHA256_CTX sha;
uint8_t shaSum[SHA224_DIGEST_SIZE];
CRYPT_SHA224_Initialize(&sha);
CRYPT_SHA224_DataAdd(&sha, buffer, sizeof(buffer));
CRYPT_SHA224_Finalize(&sha, shaSum);
</code>
Remarks:
All SHA hashes have to start at a particular value before adding new data
to it. This function sets the necessary values for the structure.
*/
int CRYPT_SHA224_Initialize(CRYPT_SHA256_CTX*);
//******************************************************************************
/* Function:
int CRYPT_SHA224_DataAdd(CRYPT_SHA256_CTX* sha224, const unsigned char* input, unsigned int sz)
Summary:
Updates the hash with the data provided.
Description:
This function updates the hash with the data provided.
Precondition:
The SHA224 context must be initialized prior to the first call of this function.
The context must not be modified by code outside of this function.
Parameters:
sha224 - Pointer to CRYPT_SHA256_CTX structure which holds the hash values.
input - Pointer to the data to use to update the hash.
sz - Size of the data (in bytes) of the data to use to update the hash.
Returns:
- BAD_FUNC_ARG - An invalid pointer was passed to the function, either in sha224 or input.
- 0 - An invalid pointer was not passed to the function.
Example:
<code>
CRYPT_SHA256_CTX sha224;
uint8_t buffer[1024];
uint8_t shaSum[SHA224_DIGEST_SIZE];
CRYPT_SHA224_Initialize(&sha224);
CRYPT_SHA224_DataAdd(&sha224, buffer, sizeof(buffer));
CRYPT_SHA224_Finalize(&sha224, shaSum);
</code>
Remarks:
In order to preserve the validity of the SHA224 hash, nothing must modify the
context holding variable between calls to CRYPT_SHA224_DataAdd.
*/
int CRYPT_SHA224_DataAdd(CRYPT_SHA256_CTX*, const unsigned char*, unsigned int);
//******************************************************************************
/* Function:
int CRYPT_SHA224_Finalize(CRYPT_SHA256_CTX* sha224, unsigned char* digest)
Summary:
Finalizes the hash and puts the result into digest.
Description:
This function finalizes the hash and puts the result into digest.
Precondition:
The SHA224 context must be initialized prior to calling this function.
The context must not be modified by code outside of this function.
Parameters:
sha224 - Pointer to CRYPT_SHA256_CTX structure which holds the hash values.
digest - Pointer to byte array to store hash result.
Returns:
- BAD_FUNC_ARG - An invalid pointer was passed to the function, either in sha or digest.
- 0 - An invalid pointer was not passed to the function.
Example:
<code>
CRYPT_SHA256_CTX sha224;
uint8_t buffer[1024];
uint8_t shaSum[SHA224_DIGEST_SIZE];
CRYPT_SHA224_Initialize(&sha224);
CRYPT_SHA224_DataAdd(&sha224, buffer, sizeof(buffer));
CRYPT_SHA224_Finalize(&sha224, shaSum);
</code>
Remarks:
In order to preserve the validity of the SHA224 hash, nothing must modify the
context holding variable between calls to CRYPT_SHA224_DataAdd and CRYPT_SHA224_Finalize.
*/
int CRYPT_SHA224_Finalize(CRYPT_SHA256_CTX*, unsigned char*);
enum {
CRYPT_SHA224_DIGEST_SIZE = 28
};
/* SHA-384 */
typedef struct CRYPT_SHA384_CTX {
uint64_t holder[32]; /* This structure should be large enough to hold the internal
representation, the size is checked during initialization*/
} CRYPT_SHA384_CTX;
//******************************************************************************
/* Function:
int CRYPT_SHA384_Initialize(CRYPT_SHA384_CTX* sha384)
Summary:
Initializes the internal structures necessary for SHA384 hash calculations.
Description:
This function initializes the internal structures necessary for SHA384 hash calculations.
Precondition:
None.
Parameters:
sha384 - Pointer to CRYPT_SHA384_CTX structure which holds the hash values.
Returns:
- BAD_FUNC_ARG - An invalid pointer was passed to the function
- 0 - An invalid pointer was not passed to the function
Example:
<code>
CRYPT_SHA384_CTX sha384;
uint8_t shaSum[SHA384_DIGEST_SIZE];
CRYPT_SHA384_Initialize(&sha384);
CRYPT_SHA384_DataAdd(&sha384, buffer, sizeof(buffer));
CRYPT_SHA384_Finalize(&sha384, shaSum);
</code>
Remarks:
All SHA384 hashes have to start at a particular value before adding new data
to it. This function sets the necessary values for the structure.
*/
int CRYPT_SHA384_Initialize(CRYPT_SHA384_CTX*);
//******************************************************************************
/* Function:
int CRYPT_SHA384_DataAdd(CRYPT_SHA384_CTX* sha384, const unsigned char* input, unsigned int sz)
Summary:
Updates the hash with the data provided.
Description:
This function updates the hash with the data provided.
Precondition:
The SHA384 context must be initialized prior to the first call of this function.
The context must not be modified by code outside of this function.
Parameters:
sha384 - Pointer to CRYPT_SHA384_CTX structure which holds the hash values.
input - Pointer to the data to use to update the hash.
sz - Size of the data (in bytes) of the data to use to update the hash.
Returns:
- BAD_FUNC_ARG - An invalid pointer was passed to the function, either in sha384 or input.
- 0 - An invalid pointer was not passed to the function.
Example:
<code>
CRYPT_SHA384_CTX sha384;
uint8_t buffer[1024];
uint8_t shaSum[SHA384_DIGEST_SIZE];
CRYPT_SHA384_Initialize(&sha384);
CRYPT_SHA384_DataAdd(&sha384, buffer, sizeof(buffer));
CRYPT_SHA384_Finalize(&sha384, shaSum);
</code>
Remarks:
In order to preserve the validity of the SHA384 hash, nothing must modify the
context holding variable between calls to CRYPT_SHA384_DataAdd.
*/
int CRYPT_SHA384_DataAdd(CRYPT_SHA384_CTX*, const unsigned char*, unsigned int);
//******************************************************************************
/* Function:
int CRYPT_SHA384_Finalize(CRYPT_SHA384_CTX* sha384, unsigned char* digest)
Summary:
Finalizes the hash and puts the result into digest.
Description:
This function finalizes the hash and puts the result into digest.
Precondition:
The SHA384 context must be initialized prior to calling this function.
The context must not be modified by code outside of this function.
Parameters:
sha384 - Pointer to CRYPT_SHA384_CTX structure which holds the hash values.
digest - Pointer to byte array to store hash result.
Returns:
- BAD_FUNC_ARG - An invalid pointer was passed to the function, either in sha384 or digest.
- 0 - An invalid pointer was not passed to the function.
Example:
<code>
CRYPT_SHA384_CTX sha384;
uint8_t buffer[1024];
uint8_t shaSum[SHA384_DIGEST_SIZE];
CRYPT_SHA384_Initialize(&sha384);
CRYPT_SHA384_DataAdd(&sha384, buffer, sizeof(buffer));
CRYPT_SHA384_Finalize(&sha384, shaSum);
</code>
Remarks:
In order to preserve the validity of the SHA384 hash, nothing must modify the
context holding variable between calls to CRYPT_SHA384_DataAdd and CRYPT_SHA384_Finalize.
*/
int CRYPT_SHA384_Finalize(CRYPT_SHA384_CTX*, unsigned char*);
enum {
CRYPT_SHA384_DIGEST_SIZE = 48
};
/* SHA-512 */
typedef struct CRYPT_SHA512_CTX {
uint64_t holder[36]; /* This structure should be large enough to hold
the internal representation, the size is checked
during initialization*/
} CRYPT_SHA512_CTX;
//******************************************************************************
/* Function:
int CRYPT_SHA512_Initialize(CRYPT_SHA512_CTX* sha512)
Summary:
Initializes the internal structures necessary for SHA512 hash calculations.
Description:
This function initializes the internal structures necessary for SHA512 hash calculations.
Precondition:
None.
Parameters:
sha512 - Pointer to CRYPT_SHA512_CTX structure which holds the hash values.
Returns:
- BAD_FUNC_ARG - An invalid pointer was passed to the function.
- 0 - An invalid pointer was not passed to the function.
Example:
<code>
CRYPT_SHA512_CTX sha512;
uint8_t sha512Sum[SHA512_DIGEST_SIZE];
CRYPT_SHA512_Initialize(&sha512);
CRYPT_SHA512_DataAdd(&sha512, buffer, sizeof(buffer));
CRYPT_SHA512_Finalize(&sha512, sha512Sum);
</code>
Remarks:
All SHA512 hashes have to start at a particular value before adding new data
to it. This function sets the necessary values for the structure.
*/
int CRYPT_SHA512_Initialize(CRYPT_SHA512_CTX*);
//******************************************************************************
/* Function:
int CRYPT_SHA512_DataAdd(CRYPT_SHA512_CTX* sha512, const unsigned char* input, unsigned int sz)
Summary:
Updates the hash with the data provided.
Description:
This function updates the hash with the data provided.
Precondition:
The SHA512 context must be initialized prior to the first call of this function.
The context must not be modified by code outside of this function.
Parameters:
sha512 - Pointer to CRYPT_SHA512_CTX structure which holds the hash values.
input - Pointer to the data to use to update the hash.
sz - Size of the data (in bytes) of the data to use to update the hash.
Returns:
- BAD_FUNC_ARG - An invalid pointer was passed to the function, either in sha512 or input.
- 0 - An invalid pointer was not passed to the function.
Example:
<code>
CRYPT_SHA512_CTX sha512;
uint8_t buffer[1024];
uint8_t sha512Sum[SHA512_DIGEST_SIZE];
CRYPT_SHA512_Initialize(&sha512);
CRYPT_SHA512_DataAdd(&sha512, buffer, sizeof(buffer));
CRYPT_SHA512_Finalize(&sha512, sha512Sum);
</code>
Remarks:
In order to preserve the validity of the SHA512 hash, nothing must modify the
context holding variable between calls to CRYPT_SHA512_DataAdd.
*/
int CRYPT_SHA512_DataAdd(CRYPT_SHA512_CTX*, const unsigned char*, unsigned int);
//******************************************************************************
/* Function:
int CRYPT_SHA512_Finalize(CRYPT_SHA512_CTX* sha512, unsigned char* digest)
Summary:
Finalizes the hash and puts the result into digest.
Description:
This function finalizes the hash and puts the result into digest.
Precondition:
The SHA512 context must be initialized prior to calling this function.
The context must not be modified by code outside of this function.
Parameters:
sha512 - Pointer to CRYPT_SHA512_CTX structure which holds the hash values.
digest - Pointer to byte array to store hash result.
Returns:
- BAD_FUNC_ARG - An invalid pointer was passed to the function, either in sha512 or digest.
- 0 - An invalid pointer was not passed to the function.
Example:
<code>
CRYPT_SHA512_CTX sha512;
uint8_t buffer[1024];
uint8_t sha512Sum[SHA512_DIGEST_SIZE];
CRYPT_SHA512_Initialize(&sha512);
CRYPT_SHA512_DataAdd(&sha512, buffer, sizeof(buffer));
CRYPT_SHA512_Finalize(&sha512, sha512Sum);
</code>
Remarks:
In order to preserve the validity of the SHA512 hash, nothing must modify the
context holding variable between calls to CRYPT_SHA512_DataAdd and CRYPT_SHA512_Finalize.
*/
int CRYPT_SHA512_Finalize(CRYPT_SHA512_CTX*, unsigned char*);
enum {
CRYPT_SHA512_DIGEST_SIZE = 64
};
/* HMAC */
typedef struct CRYPT_HMAC_CTX {
uint64_t holder[100]; /* This structure should be large enough to hold
the internal representation, the size is checked
during initialization*/
} CRYPT_HMAC_CTX;
//*****************************************************************************
/*
Function:
int CRYPT_HMAC_SetKey(CRYPT_HMAC_CTX* hmac, int type, const unsigned char* key, unsigned int sz)
Summary:
Initializes the HMAC context and set the key for the hash.
Description:
This function initializes the HMAC context and set the key for the hash.
Preconditions:
None.
Parameters:
hmac - Pointer to context which saves state between calls.
type - Type of SHA operation to use with HMAC. Must be one of the
following:
* CRYPT_HMAC_SHA
* CRYPT_HMAC_SHA256
* CRYPT_HMAC_SHA384
* CRYPT_HMAC_SHA512
key - Secret key used for the hash.
sz - Size of the input data in bytes.
Returns:
* BAD_FUNC_ARG - An invalid pointer was passed to the function.
* 0 - An invalid pointer was not passed to the function.
Example:
<code>
CRYPT_HMAC_CTX mcHmac;
byte mcDigest[CRYPT_SHA512_DIGEST_SIZE];
CRYPT_HMAC_SetKey(&mcHmac, CRYPT_HMAC_SHA, key, 4);