You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Fixed macros
* Refactored code to use macros
* added consistant 0-BSD headers
* SHA-2 Royalty-Free Patent Notice file just-in-case
* corrected CC-by-line from implementation author to patent holder (Clearification: Author's Code still 0BSD but some parts probably subject to the royalty-free U.S. Patent 6,829,355)
* added some coments to code
Public domain / CC0. Minimal SHA-2 declarations for sha256/sha384/sha512.
3
-
*/
2
+
3
+
Permission to use, copy, modify, and/or distribute this software for any
4
+
purpose with or without fee is hereby granted.
5
+
6
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
7
+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
8
+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
9
+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
10
+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
11
+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
12
+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
13
+
14
+
Minimal SHA-2 declarations for sha256/sha384/sha512.
15
+
*/
4
16
#ifndefFEATHERHASH_SHA2_H
17
+
18
+
/*!
19
+
@header sha2.h
20
+
@discussion
21
+
Lightweight, portable SHA-256 implementation (API only). This component provides three primary functions:
22
+
- sha256_init: initialize a context
23
+
- sha256_update: feed bytes into the running hash
24
+
- sha256_final: finalize and produce the 32-byte digest
25
+
26
+
The implementation is intentionally compact and suitable for embedded or utility code. It does not allocate dynamic memory and assumes a little- or big-endian-independent byte-level input (the algorithm itself works with big-endian message words).
27
+
28
+
The API operates on a caller-allocated sha256_ctx structure, defined as: ``sha256_ctx``.
29
+
30
+
Thread safety:
31
+
- Each sha256_ctx instance may be used by one thread at a time. The functions do not use global mutable state.
32
+
33
+
Security notes:
34
+
- The implementation zeroes the context in sha256_final to reduce lifetime of sensitive intermediate state.
35
+
- The implementation is not hardened against side-channel attacks (timing, cache). Do not use for high-security requirements without appropriate hardening.
36
+
37
+
Usage example:
38
+
@code
39
+
sha256_ctx ctx;
40
+
uint8_t digest[32];
41
+
const uint8_t *data = ...;
42
+
size_t len = ...;
43
+
44
+
sha256_init(&ctx);
45
+
sha256_update(&ctx, data, len);
46
+
sha256_final(&ctx, digest);
47
+
@endcode
48
+
*/
49
+
5
50
#if defined(__clang__) &&__clang__
6
51
#pragma mark -
7
52
#pragma mark sha2Header
@@ -51,24 +96,109 @@
51
96
extern"C" {
52
97
#endif/* !defined(__cplusplus) */
53
98
99
+
#if defined(__clang__) &&__clang__
100
+
#pragma mark -
101
+
#pragma mark Utility Functions
102
+
#endif/* !__clang__ */
103
+
104
+
/**!
105
+
Rotate-32 operation utility.
106
+
107
+
- Discussion: This version is an intuitive bitwise OR of right shifted bits and the left shifted 'overflow' bits (e.g., "carried over" if you will).
108
+
There are certainly better versions out there than this lightweight implementation, but this version is intended for stage0 toolchains
109
+
after all.
110
+
111
+
- Parameter x: The ``uint32_t`` value to rotate right (32 bits).
112
+
- Parameter n: The ``unsigned`` distance to rotate right by.
113
+
- seealso: A more [general form](https://stackoverflow.com/a/776523) (that one is CC-BY-SA-4.0)
114
+
- seealso: The two academic implementation of identical form (a cool coincidence)
115
+
* By John Regehr (Publicly available academic implementation; unknown copyright)
116
+
on [his blog](https://blog.regehr.org/archives/1063)
117
+
* And by Brad Conte (public domain implementation)
118
+
on [his github](https://github.com/B-Con/crypto-algorithms/blob/cfbde48414baacf51fc7c74f275190881f037d32/sha256.c#L22)
0 commit comments