• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 The Android Open Source Project
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 LOCATION_LBS_CONTEXTHUB_NANOAPPS_NEARBY_CRYPTO_SHA2_H_
18 #define LOCATION_LBS_CONTEXTHUB_NANOAPPS_NEARBY_CRYPTO_SHA2_H_
19 
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23 
24 /**
25  * Supported SHA2 mode:
26  * - SHA256
27  *
28  * External separated APIs:
29  *  - sha2init() for SHA256 initialization
30  *  - sha2processBytes() for updating input data
31  *  - sha2finish() for generating SHA256 hash output
32  *
33  * External a single API:
34  *  - sha256() for performing the separated three APIs at a time
35  */
36 
37 #include <stddef.h>
38 #include <stdint.h>
39 
40 #define SHA2_BLOCK_SIZE 64U      // in bytes
41 #define SHA2_WORDS_CTX_SIZE 64U  // in words
42 
43 #define SHA2_HASH_SIZE 32U  // in bytes
44 #define SHA2_HASH_WORDS 8U  // in words
45 
46 struct Sha2Context {
47   uint32_t h[SHA2_HASH_WORDS];
48   size_t msgLen;
49   union {
50     uint32_t w[SHA2_WORDS_CTX_SIZE];
51     uint8_t b[SHA2_BLOCK_SIZE];
52   };
53   uint8_t bufBytesUsed;
54 };
55 
56 /**
57  * sha256:
58  * @inData: input data byte array to hash
59  * @dataLen: number of bytes of the input byte array
60  * @outHash: output hash byte array
61  * @hashLen: number of bytes of the output byte array
62  *
63  * Initializes SHA256 context internally
64  * Updates input data to the context
65  * Generates 32 bytes hash and copy to the output byte array as much as
66  * min(SHA2_HASH_SIZE, hash_len)
67  *
68  * Returns:
69  */
70 void sha256(const void *inData, uint32_t dataLen, void *outHash,
71             uint32_t hashLen);
72 
73 /**
74  * sha2init:
75  * @ctx: SHA256 context
76  *
77  * Initializes the SHA256 context
78  *
79  * Returns:
80  */
81 void sha2init(struct Sha2Context *ctx);
82 
83 /**
84  * sha2processBytes:
85  * @ctx: SHA256 context initialized
86  * @inData: input data byte array to hash
87  * @dataLen: number of bytes of the input byte array
88  *
89  * Updates input data to the context
90  *
91  * Returns:
92  */
93 void sha2processBytes(struct Sha2Context *ctx, const void *inData,
94                       size_t dataLen);
95 
96 /**
97  * sha2finish:
98  * @ctx: SHA256 context initialized
99  * @outHash: output hash byte array
100  * @hashLen: number of bytes of the output byte array
101  *
102  * Generates 32 bytes hash and copy to the output byte array as much as
103  * min(SHA2_HASH_SIZE, hash_len)
104  *
105  * Returns:
106  */
107 void sha2finish(struct Sha2Context *ctx, void *outHash, uint32_t hashLen);
108 #ifdef __cplusplus
109 }
110 #endif
111 #endif  // LOCATION_LBS_CONTEXTHUB_NANOAPPS_NEARBY_CRYPTO_SHA2_H_
112