Software APIs
crc32.h
1 // Copyright lowRISC contributors (OpenTitan project).
2 // Licensed under the Apache License, Version 2.0, see LICENSE for details.
3 // SPDX-License-Identifier: Apache-2.0
4 
5 #ifndef OPENTITAN_SW_DEVICE_LIB_BASE_CRC32_H_
6 #define OPENTITAN_SW_DEVICE_LIB_BASE_CRC32_H_
7 
8 #include <stddef.h>
9 #include <stdint.h>
10 
12 
13 #ifdef __cplusplus
14 extern "C" {
15 #endif // __cplusplus
16 
17 /**
18  * Initializes the context variable for a CRC32 computation.
19  *
20  * @param[out] ctx Context variable.
21  */
22 void crc32_init(uint32_t *ctx);
23 
24 /**
25  * Adds a byte to a CRC32.
26  *
27  * @param[in, out] ctx Context variable.
28  * @param byte Byte to be added.
29  */
30 void crc32_add8(uint32_t *ctx, uint8_t byte);
31 
32 /**
33  * Adds a word to a CRC32.
34  *
35  * @param[in, out] ctx Context variable.
36  * @param word Word to be added.
37  */
38 void crc32_add32(uint32_t *ctx, uint32_t word);
39 
40 /**
41  * Adds a buffer to a CRC32.
42  *
43  * @param[in, out] ctx Context variable.
44  * @param buf A buffer, little-endian.
45  * @param len Size of the buffer.
46  */
47 void crc32_add(uint32_t *ctx, const void *buf, size_t len);
48 
49 /**
50  * Finishes a CRC32 computation.
51  *
52  * This function does not modify the context variable `ctx`.
53  *
54  * @param ctx Context variable.
55  * @return Result of the computation.
56  */
58 uint32_t crc32_finish(const uint32_t *ctx);
59 
60 /**
61  * Computes the CRC32 of a buffer as defined by IEEE 802.3 CRC-32. It also
62  * matches Python's `zlib.crc32()`.
63  *
64  * @param buf A buffer, little-endian.
65  * @param len Size of the buffer.
66  * @return CRC32 of the buffer.
67  */
69 uint32_t crc32(const void *buf, size_t len);
70 
71 #ifdef __cplusplus
72 } // extern "C"
73 #endif // __cplusplus
74 
75 #endif // OPENTITAN_SW_DEVICE_LIB_BASE_CRC32_H_