pqc/common/sha2.h

175 rader
3.9 KiB
C
Permalänk Normal vy Historik

2019-01-15 14:35:25 +00:00
#ifndef SHA2_H
#define SHA2_H
2019-03-11 13:41:11 +00:00
#include <stddef.h>
#include <stdint.h>
/* The incremental API allows hashing of individual input blocks; these blocks
must be exactly 64 bytes each.
Use the 'finalize' functions for any remaining bytes (possibly over 64). */
#define PQC_SHA256CTX_BYTES 40
/* Structure for the incremental API */
2019-05-20 08:20:12 +01:00
typedef struct {
uint8_t *ctx;
2019-05-20 08:20:12 +01:00
} sha224ctx;
/* Structure for the incremental API */
2019-05-20 08:20:12 +01:00
typedef struct {
uint8_t *ctx;
2019-05-20 08:20:12 +01:00
} sha256ctx;
#define PQC_SHA512CTX_BYTES 72
/* Structure for the incremental API */
2019-05-20 08:20:12 +01:00
typedef struct {
uint8_t *ctx;
2019-05-20 08:20:12 +01:00
} sha384ctx;
/* Structure for the incremental API */
2019-05-20 08:20:12 +01:00
typedef struct {
uint8_t *ctx;
2019-05-20 08:20:12 +01:00
} sha512ctx;
/* ====== SHA224 API ==== */
/**
* Initialize the incremental hashing API.
*
* Can't be called multiple times.
*/
2019-05-20 08:20:12 +01:00
void sha224_inc_init(sha224ctx *state);
/**
* Copy the hashing state
*/
void sha224_inc_ctx_clone(sha224ctx *stateout, const sha224ctx *statein);
/**
* Absorb blocks
*/
2019-05-20 08:20:12 +01:00
void sha224_inc_blocks(sha224ctx *state, const uint8_t *in, size_t inblocks);
/**
* Finalize and obtain the digest
*
* If applicable, this function will free the memory associated with the sha224ctx.
*
* If not calling this function, call `sha224_inc_ctx_release`
*/
2019-05-20 08:20:12 +01:00
void sha224_inc_finalize(uint8_t *out, sha224ctx *state, const uint8_t *in, size_t inlen);
/**
* Destroy the state. Make sure to use this, as this API may not always be stack-based.
*/
void sha224_inc_ctx_release(sha224ctx *state);
/**
* All-in-one sha224 function
*/
2019-03-11 15:48:29 +00:00
void sha224(uint8_t *out, const uint8_t *in, size_t inlen);
/* ====== SHA256 API ==== */
/**
* Initialize the incremental hashing API
*/
2019-05-20 08:20:12 +01:00
void sha256_inc_init(sha256ctx *state);
/**
* Copy the hashing state
*/
void sha256_inc_ctx_clone(sha256ctx *stateout, const sha256ctx *statein);
/**
* Absorb blocks
*/
2019-05-20 08:20:12 +01:00
void sha256_inc_blocks(sha256ctx *state, const uint8_t *in, size_t inblocks);
/**
* Finalize and obtain the digest
*
* If applicable, this function will free the memory associated with the sha256ctx.
*/
2019-05-20 08:20:12 +01:00
void sha256_inc_finalize(uint8_t *out, sha256ctx *state, const uint8_t *in, size_t inlen);
/**
* Destroy the state. Make sure to use this, as this API may not always be stack-based.
*/
void sha256_inc_ctx_release(sha256ctx *state);
/**
* All-in-one sha256 function
*/
2019-03-11 15:33:47 +00:00
void sha256(uint8_t *out, const uint8_t *in, size_t inlen);
2019-03-11 13:41:11 +00:00
/* ====== SHA384 API ==== */
/**
* Initialize the incremental hashing API
*/
2019-05-20 08:20:12 +01:00
void sha384_inc_init(sha384ctx *state);
/**
* Copy the hashing state
*/
void sha384_inc_ctx_clone(sha384ctx *stateout, const sha384ctx *statein);
/**
* Absorb blocks
*/
2019-05-20 08:20:12 +01:00
void sha384_inc_blocks(sha384ctx *state, const uint8_t *in, size_t inblocks);
/**
* Finalize and obtain the digest.
*
* If applicable, this function will free the memory associated with the sha384ctx.
*/
2019-05-20 08:20:12 +01:00
void sha384_inc_finalize(uint8_t *out, sha384ctx *state, const uint8_t *in, size_t inlen);
/**
* Destroy the state. Make sure to use this if not calling finalize, as this API may not always be stack-based.
*/
void sha384_inc_ctx_release(sha384ctx *state);
/**
* All-in-one sha384 function
*/
2019-03-11 15:33:47 +00:00
void sha384(uint8_t *out, const uint8_t *in, size_t inlen);
2019-03-11 13:41:11 +00:00
/* ====== SHA512 API ==== */
/**
* Initialize the incremental hashing API
*/
2019-05-20 08:20:12 +01:00
void sha512_inc_init(sha512ctx *state);
/**
* Copy the hashing state
*/
void sha512_inc_ctx_clone(sha512ctx *stateout, const sha512ctx *statein);
/**
* Absorb blocks
*/
2019-05-20 08:20:12 +01:00
void sha512_inc_blocks(sha512ctx *state, const uint8_t *in, size_t inblocks);
/**
* Finalize and obtain the digest
*
* If applicable, this function will free the memory associated with the sha512ctx.
*/
2019-05-20 08:20:12 +01:00
void sha512_inc_finalize(uint8_t *out, sha512ctx *state, const uint8_t *in, size_t inlen);
/**
* Destroy the state. Make sure to use this if not calling finalize, as this API may not always be stack-based.
*/
void sha512_inc_ctx_release(sha512ctx *state);
/**
* All-in-one sha512 function
*/
2019-03-11 15:33:47 +00:00
void sha512(uint8_t *out, const uint8_t *in, size_t inlen);
2019-01-15 14:35:25 +00:00
#endif