1 /*############################################################################
2 # Copyright 2017 Intel Corporation
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 ############################################################################*/
16 #ifndef EPID_MEMBER_TINY_MATH_HASHWRAP_H_
17 #define EPID_MEMBER_TINY_MATH_HASHWRAP_H_
18 /// Decleration of hash wrap function
19 /*! \file */
20 #include <stddef.h>
21 #include "epid/common/types.h"
22 #include "epid/member/tiny/math/mathtypes.h"
23 
24 #define SHA512_SUPPORT
25 #define SHA256_SUPPORT
26 
27 #ifdef SHA512_SUPPORT
28 #include "epid/member/tiny/math/sha512.h"
29 #endif
30 
31 #ifdef SHA256_SUPPORT
32 #include "epid/member/tiny/math/sha256.h"
33 #endif
34 
35 /// Sha Digest Element
36 typedef union sha_digest {
37 #ifdef SHA512_SUPPORT
38   uint8_t sha512_digest[SHA512_DIGEST_SIZE];  ///< Support digest for sha512
39 #endif
40 #ifdef SHA256_SUPPORT
41   uint8_t sha256_digest[SHA256_DIGEST_SIZE];  ///< Support digest for sha256
42 #endif
43   uint8_t digest[1];  ///< Pointer to digest
44 } sha_digest;
45 
46 /// Tiny Sha wrapper Element
47 typedef struct tiny_sha {
48   union {
49 #ifdef SHA512_SUPPORT
50     sha512_state sha512s;  ///< The state of sha512 if supported
51 #endif
52 #ifdef SHA256_SUPPORT
53     sha256_state sha256s;  ///< The state of sha256 if supported
54 #endif
55   } sha_state_t;     ///< Store state of hash algorithm
56   HashAlg hash_alg;  ///< The hash algorithem which selected
57 } tiny_sha;
58 
59 /// Initializes the hash state
60 /*!
61 
62 \param[in] sha_type
63 Type of hash algorithm
64 
65 \param[in,out] s
66 The hash state to initialize.
67 */
68 void tinysha_init(HashAlg sha_type, tiny_sha* s);
69 
70 /// Hashes data into state using a chosen hash algorithm
71 /*!
72 
73 \param[in,out] s
74 The hash state. Must be non-null or behavior is undefined.
75 
76 \param[in] data
77 The data to hash into s.
78 
79 \param[in] data_length
80 The size of data in bytes.
81 
82 
83 */
84 void tinysha_update(tiny_sha* s, void const* data, size_t data_length);
85 
86 /// Computes the hash algorithm in the digest buffer
87 /*!
88 
89 \param[out] digest
90 The computed digest. Must be non-null or behavior is undefined.
91 
92 \param[in] s
93 The hash state. Must be non-null or behavior is undefined.
94 */
95 void tinysha_final(unsigned char* digest, tiny_sha* s);
96 
97 /// Returns size of digest depending on hash algorithm
98 /*!
99 
100 \param[in] s
101 The hash state. Must be non-null or behavior is undefined.
102 
103 \returns
104 Size of digest
105 
106 */
107 size_t tinysha_digest_size(tiny_sha* s);
108 
109 #endif  // EPID_MEMBER_TINY_MATH_HASHWRAP_H_
110