Skip to content

Commit 57f4a50

Browse files
committed
Revamp
restyle the code add shell compile update the copyright comment add const attribute to FN_ functions; uninline _addbits extra check ctx->len in sha256_hash() update Makefile update self test add assumptions for cbmc get rid of MINIMIZE_STACK_IMPACT, move W to a context replace legacy with Mark 2 as the only variant update the README file update copyright
1 parent 84d4a0a commit 57f4a50

File tree

8 files changed

+377
-612
lines changed

8 files changed

+377
-612
lines changed

LICENSE

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
MIT License
22

3-
Copyright (c) 2021 Ilya O. Levin
3+
Copyright (c) 2010,2014 Literatecode, http://www.literatecode.com
4+
Copyright (c) 2022 Ilia Levin ([email protected])
5+
46

57
Permission is hereby granted, free of charge, to any person obtaining a copy
68
of this software and associated documentation files (the "Software"), to deal

Makefile

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
CC = clang
2+
CFLAGS= -O3 -pedantic -Wall -Wextra -std=c99
3+
CBMC = cbmc
4+
TARGET = sha256
5+
6+
.PHONY: clean verify
7+
8+
$(TARGET).o: sha256.c sha256.h
9+
$(CC) $(CFLAGS) -c -o $@ $<
10+
11+
test: sha256.c sha256.h
12+
$(CC) $(CFLAGS) -o $(TARGET) -DSHA256_SELF_TEST__ $<
13+
14+
all: test $(TARGET)
15+
16+
clean:
17+
rm -f $(TARGET) *.o
18+
19+
verify:
20+
$(CBMC) sha256.c -DSHA256_SELF_TEST__ -D_cbmc_ $(if $(FUNC),--function $(FUNC),) \
21+
--unwind 64 --partial-loops \
22+
--bounds-check \
23+
--memory-leak-check --malloc-may-fail --malloc-fail-null \
24+
--pointer-check --pointer-primitive-check --pointer-overflow-check \
25+
--div-by-zero-check --conversion-check \
26+
--signed-overflow-check --unsigned-overflow-check \
27+
--undefined-shift-check --float-overflow-check

README.md

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,51 @@
11
# SHA256
22

3-
SHA-256 implementation to compliment a portable byte-oriented AES-256
4-
implementation in C at http://www.literatecode.com/aes256
3+
This is an implementation of the SHA-256 secure hash algorithm defined in
4+
[FIPS 180-4](https://csrc.nist.gov/publications/detail/fips/180/4/final)
55

6-
There is also a newer version in the [mark2](mark2) directory. This version is
7-
cleaner and closer to a reference implementation. It no longer has
8-
built-in support features for endianness, but you may easily adapt
9-
the code to different endianness shall you need that.
6+
It is not a byte-oriented implementation. Still, it may complement
7+
a portable byte-oriented C version of AES-256 at
8+
[www.literatecode.com/aes256](http://www.literatecode.com/aes256)
109

11-
Unlike the previous implementation, the Mark 2 one is formally
12-
verifiable with [CBMC](http://www.cprover.org/cbmc/)
1310

11+
## Compile
12+
13+
This implementation supports `clang` (recommended) and `GCC` C compilers.
14+
Other compilers may also work with some minor code tweaking. Apologies for
15+
not caring about the seamless support of the MSVC compiler any longer.
16+
Check the legacy section below if you still need that.
17+
18+
Use `make` or `sh sha256.c -c -o sha256.o` to compile into an object file
19+
that you may link with your project later.
20+
21+
Use `make test` or `sh sha256.c -DSHA256_SELF_TEST__` to compile an
22+
executable binary that will perform a few known answer tests for SHA-256.
23+
24+
25+
## Formal verification
26+
27+
We rely on [C Bounded Model Checker](http://www.cprover.org/cbmc/) to formally
28+
verify code properties.
29+
30+
Use `make verify` to verify the self-testing code.
31+
32+
If you want to focus verification on a single function, use
33+
`make verify FUNC=XYZ`, where `XYZ` is a function name.
34+
35+
Check [https://github.com/diffblue/cbmc](https://github.com/diffblue/cbmc)
36+
for the latest version of CBMC.
37+
38+
39+
## History
40+
41+
* 2010: The original code was written.
42+
* 2013: The original code was published on [GitHub](https://github.com/ilvn/SHA256).
43+
* 2014: The Mark 2 version was written (cleaner, closer to a reference implementation, formally verifiable).
44+
* 2017: The Mark 2 version was added to the repository.
45+
* 2022: The revamped Mark 2 version superseded the original code.
46+
47+
### Legacy
48+
49+
The original version is still available under the tag
50+
[legacy](https://github.com/ilvn/SHA256/releases/tag/legacy) and provided
51+
only for reference. Therefore, it is no longer supported or recommended.

mark2/Makefile

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)