CRYPTO-C v1.1.3
C/C++ Documentation
sha256.h
Go to the documentation of this file.
1
15/* include guard */
16#ifndef CRYPTO_SHA256_H
17#define CRYPTO_SHA256_H
18
19
20#include "utildev.h"
21
22#define SHA256LEN 32
24/* SHA256 transform routines */
25#define EP0(x) xor3(ror32(x, 2), ror32(x, 13), ror32(x, 22))
26#define EP1(x) xor3(ror32(x, 6), ror32(x, 11), ror32(x, 25))
27#define S0(x) xor3(ror32(x, 7), ror32(x, 18), ((x) >> 3))
28#define S1(x) xor3(ror32(x, 17), ror32(x, 19), ((x) >> 10))
29
30#define sha256_blk0(i) ( W[i] = bswap32(W[i]) )
31#define sha256_blk(i) ( W[i & 15] += \
32 S1(W[(i - 2) & 15]) + W[(i - 7) & 15] + S0(W[(i - 15) & 15]) )
33
34#define Ch(x, y, z) xandx(x, y, z)
35#define Maj(x, y, z) ( (x & y) | (z & (x | y)) )
36
37/* initializer type round */
38#define R0(a, b, c, d, e, f, g, h, i) \
39 h += EP1(e) + Ch(e,f,g) + k[i] + sha256_blk0(i); \
40 d += h; h += EP0(a) + Maj(a, b, c)
41/* normal type round */
42#define R(a, b, c, d, e, f, g, h, i, j) \
43 h += EP1(e) + Ch(e,f,g) + k[i+j] + sha256_blk(i); \
44 d += h; h += EP0(a) + Maj(a, b, c)
45/* round expansion for initializer type rounds */
46#define RX0_8(i) \
47 R0(a, b, c, d, e, f, g, h, i); \
48 R0(h, a, b, c, d, e, f, g, (i + 1)); \
49 R0(g, h, a, b, c, d, e, f, (i + 2)); \
50 R0(f, g, h, a, b, c, d, e, (i + 3)); \
51 R0(e, f, g, h, a, b, c, d, (i + 4)); \
52 R0(d, e, f, g, h, a, b, c, (i + 5)); \
53 R0(c, d, e, f, g, h, a, b, (i + 6)); \
54 R0(b, c, d, e, f, g, h, a, (i + 7))
55/* round expansion for normal type rounds */
56#define RX_8(i, j) \
57 R(a, b, c, d, e, f, g, h, i, j); \
58 R(h, a, b, c, d, e, f, g, (i + 1), j); \
59 R(g, h, a, b, c, d, e, f, (i + 2), j); \
60 R(f, g, h, a, b, c, d, e, (i + 3), j); \
61 R(e, f, g, h, a, b, c, d, (i + 4), j); \
62 R(d, e, f, g, h, a, b, c, (i + 5), j); \
63 R(c, d, e, f, g, h, a, b, (i + 6), j); \
64 R(b, c, d, e, f, g, h, a, (i + 7), j)
65
66typedef struct {
67 uint8_t data[64];
68 uint32_t bitlen[2];
69 uint32_t state[8];
70 uint32_t datalen;
71} SHA256_CTX;
73/* C/C++ compatible function prototypes */
74#ifdef __cplusplus
75extern "C" {
76#endif
77
78void sha256_init(SHA256_CTX *ctx);
79void sha256_update(
80 SHA256_CTX *ctx, const void *in, size_t inlen);
81void sha256_final(SHA256_CTX *ctx, void *out);
82void sha256(const void *in, size_t inlen, void *out);
83
84/* CUDA testing functions */
85#ifdef CUDA
86 void test_kcu_sha256(const void *in, size_t *inlen, size_t max_inlen,
87 void *out, int num);
88#endif
89
90/* end extern "C" {} for C++ */
91#ifdef __cplusplus
92}
93#endif
94
95/* end include guard */
96#endif
void sha256(const void *in, size_t inlen, void *out)
Convenient all-in-one SHA256 computation.
Definition: sha256.c:177
void sha256_final(SHA256_CTX *ctx, void *out)
Finalize a SHA256 message digest.
Definition: sha256.c:129
void sha256_init(SHA256_CTX *ctx)
Initialize a SHA256 context.
Definition: sha256.c:82
void sha256_update(SHA256_CTX *ctx, const void *in, size_t inlen)
Add inlen bytes from in to a SHA256 context for hashing.
Definition: sha256.c:102
Definition: sha256.h:66
uint32_t datalen
Length of buffered input.
Definition: sha256.h:70
Device utilities and includes support.