1 /*
2  * Copyright 2021 Google LLC
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 
17 #ifndef PLATFORMS_HAVEN_LIBCR51SIGN_LIBCR51SIGN_SUPPORT_H_
18 #define PLATFORMS_HAVEN_LIBCR51SIGN_LIBCR51SIGN_SUPPORT_H_
19 
20 #include <libcr51sign/libcr51sign.h>
21 #include <openssl/sha.h>
22 
23 #ifdef __cplusplus
24 extern "C"
25 {
26 #endif
27 
28 struct hash_ctx
29 {
30     enum hash_type hash_type;
31     union
32     {
33         SHA256_CTX sha256_ctx;
34         SHA512_CTX sha512_ctx;
35     };
36 };
37 
38 // @func hash_init get ready to compute a hash
39 //
40 // @param[in] ctx - context struct
41 // @param[in] hash_type - type of hash function to use
42 //
43 // @return nonzero on error, zero on success
44 
45 int hash_init(const void* ctx, enum hash_type type);
46 
47 // @func hash_update add data to the hash
48 //
49 // @param[in] ctx - context struct
50 // @param[in] buf - data to add to hash
51 // @param[in] count - number of bytes of data to add
52 //
53 // @return nonzero on error, zero on success
54 
55 int hash_update(void* ctx, const uint8_t* data, size_t size);
56 
57 // @func hash_final finish hash calculation
58 //
59 // @param[in] ctx - context struct
60 // @param[out] hash - buffer to write hash to (guaranteed to be big enough)
61 //
62 // @return nonzero on error, zero on success
63 
64 int hash_final(void* ctx, uint8_t* hash);
65 
66 // @func verify check that the signature is valid for given hashed data
67 //
68 // @param[in] ctx - context struct
69 // @param[in] scheme - type of signature, hash, etc.
70 // @param[in] sig - signature blob
71 // @param[in] sig_len - length of signature in bytes
72 // @param[in] data - pre-hashed data to verify
73 // @param[in] data_len - length of hashed data in bytes
74 //
75 // @return nonzero on error, zero on success
76 
77 int verify_signature(const void* ctx, enum signature_scheme sig_scheme,
78                      const uint8_t* sig, size_t sig_len, const uint8_t* data,
79                      size_t data_len);
80 
81 #ifdef __cplusplus
82 } //  extern "C"
83 #endif
84 #endif // PLATFORMS_HAVEN_LIBCR51SIGN_LIBCR51SIGN_SUPPORT_H_
85