• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
3  *
4  *  Licensed under the Apache License, Version 2.0 (the License); you may
5  *  not use this file except in compliance with the License.
6  *
7  *  http://www.apache.org/licenses/LICENSE-2.0
8  */
9 
10 
11 
12 #ifndef GMSSL_SHA2_H
13 #define GMSSL_SHA2_H
14 
15 #include <string.h>
16 #include <stdint.h>
17 #include <sys/types.h>
18 
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22 
23 
24 #define SHA2_IS_BIG_ENDIAN	1
25 
26 
27 #define SHA224_DIGEST_SIZE	28
28 #define SHA224_BLOCK_SIZE	64
29 #define SHA224_STATE_WORDS	8
30 
31 typedef struct {
32 	uint32_t state[SHA224_STATE_WORDS];
33 	uint64_t nblocks;
34 	uint8_t block[SHA224_BLOCK_SIZE];
35 	int num;
36 } SHA224_CTX;
37 
38 void sha224_init(SHA224_CTX *ctx);
39 void sha224_update(SHA224_CTX *ctx, const uint8_t* data, size_t datalen);
40 void sha224_finish(SHA224_CTX *ctx, uint8_t dgst[SHA224_DIGEST_SIZE]);
41 void sha224_digest(const uint8_t *data, size_t datalen,
42 	uint8_t dgst[SHA224_DIGEST_SIZE]);
43 
44 
45 #define SHA256_DIGEST_SIZE	32
46 #define SHA256_BLOCK_SIZE	64
47 #define SHA256_STATE_WORDS	8
48 
49 typedef struct {
50 	uint32_t state[SHA256_STATE_WORDS];
51 	uint64_t nblocks;
52 	uint8_t block[SHA256_BLOCK_SIZE];
53 	int num;
54 } SHA256_CTX;
55 
56 void sha256_init(SHA256_CTX *ctx);
57 void sha256_update(SHA256_CTX *ctx, const uint8_t* data, size_t datalen);
58 void sha256_finish(SHA256_CTX *ctx, uint8_t dgst[SHA256_DIGEST_SIZE]);
59 void sha256_digest(const uint8_t *data, size_t datalen,
60 	uint8_t dgst[SHA256_DIGEST_SIZE]);
61 
62 
63 #define SHA384_DIGEST_SIZE	48
64 #define SHA384_BLOCK_SIZE	128
65 #define SHA384_STATE_WORDS	8
66 
67 typedef struct {
68 	uint64_t state[SHA384_STATE_WORDS];
69 	uint64_t nblocks;
70 	uint8_t block[SHA384_BLOCK_SIZE];
71 	int num;
72 } SHA384_CTX;
73 
74 void sha384_init(SHA384_CTX *ctx);
75 void sha384_update(SHA384_CTX *ctx, const uint8_t* data, size_t datalen);
76 void sha384_finish(SHA384_CTX *ctx, uint8_t dgst[SHA384_DIGEST_SIZE]);
77 void sha384_digest(const uint8_t *data, size_t datalen,
78 	uint8_t dgst[SHA384_DIGEST_SIZE]);
79 
80 
81 #define SHA512_DIGEST_SIZE	64
82 #define SHA512_BLOCK_SIZE	128
83 #define SHA512_STATE_WORDS	8
84 
85 typedef struct {
86 	uint64_t state[SHA512_STATE_WORDS];
87 	uint64_t nblocks;
88 	uint8_t block[SHA512_BLOCK_SIZE];
89 	int num;
90 } SHA512_CTX;
91 
92 void sha512_init(SHA512_CTX *ctx);
93 void sha512_update(SHA512_CTX *ctx, const uint8_t* data, size_t datalen);
94 void sha512_finish(SHA512_CTX *ctx, uint8_t dgst[SHA512_DIGEST_SIZE]);
95 void sha512_digest(const uint8_t *data, size_t datalen,
96 	uint8_t dgst[SHA512_DIGEST_SIZE]);
97 
98 
99 #ifdef __cplusplus
100 }
101 #endif
102 #endif
103