GithubHelp home page GithubHelp logo

ptyx092 / sha3iuf Goto Github PK

View Code? Open in Web Editor NEW

This project forked from brainhub/sha3iuf

0.0 1.0 0.0 128 KB

Single-file C implementation of the SHA-3 implementation with Init/Update/Finalize hashing (NIST FIPS 202)

C 100.00%

sha3iuf's Introduction

A single-file C implementation of SHA-3 with Init/Update/Finalize API

The purpose of this project is:

  • provide an API that hashes bytes, not bits
  • provide a simple single-file reference implementation of a SHA-3 message digest algorithm, as defined in the FIPS 202 standard;
  • implement the hashing API that employs the IUF paradigm (or Init, Update, Finalize style).
  • answer the design questions, such as:
    • what does the state for IUF look like?
    • how small can the state be (224 bytes on a 64-bit system for a unified SHA-3 algorithm)
    • what is the incremental cost of adding e.g. SHA3-384 to a SHA3-256 implementation?

The implementation is written in C and uses uint64_t types to manage the state. The code will compile and run on 64-bit and 32-bit architectures (gcc and gcc -m32 on x86_64 were tested).

License, prior work

This work is in public domain.

I would appreciate any attribution to this work if you used the code or ideas. I thank you for this in advance.

This is a clean-room implementation of IUF API for SHA3. The keccakf() is based on the code from keccak.noekeon.org.

1600-bit message hashing test vectors are NIST test vectors.

Overview of the API

Let's hash 'abc' with SHA3-256 using two methods: single buffer (but using IUF paradigm), and using the IUF API.

sha3_context c;
uint8_t *hash;

Single-buffer hashing:

sha3_Init256(&c);
sha3_Update(&c, "abc", 3);
hash = sha3_Finalize(&c);
// 'hash' points to a buffer inside 'c'
// with the value of SHA3-256

Alternatively, IUF hashing:

sha3_Init256(&c);
sha3_Update(&c, "a", 1);
sha3_Update(&c, "bc", 2);
hash = sha3_Finalize(&c);

// no free for 'c' is needed

The hash points to the same 256/8=32 bytes in both cases.

Self-tests

$ gcc -Wall sha3.c -o _ && ./_
SHA3-256, SHA3-384, SHA3-512 tests passed OK

or

$ gcc -m32 Wall sha3.c -o _ && ./_
SHA3-256, SHA3-384, SHA3-512 tests passed OK

API

  • the same sha3_context object maintains the state for SHA3-256, SHA3-384, or SHA3-512 algorithm;
  • the hash algorithm used is determined by how the context was initialized with sha3_InitX, e.g. sha3_Init256, sha3_Init384, or sha3_Init512 call;
  • sha3_Update and sha3_Finalize are the same for regardless the type of the algorithm (X);
  • the buffer returned by sha3_Finalize will have X bits of hash;
  • sha3_InitX works also as Reset or Free (zeroization) of the hash context.

See sha3.c for details.

Notes

SHA3-224 is not supported, but can easily be added.

The code was written to work with the Microsoft Visual Studio compiler (under _MSC_VER), but this build target was not tested.

This project was created to support SHA3 in OpenPGP work, but it applies to other protocols and formats, e.g. TLS.

sha3iuf's People

Contributors

brainhub avatar

Watchers

ptyx avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.