Software APIs
util.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_SILICON_CREATOR_LIB_BASE_UTIL_H_
6 #define OPENTITAN_SW_DEVICE_SILICON_CREATOR_LIB_BASE_UTIL_H_
7 
8 #include <stddef.h>
9 #include <stdint.h>
10 
11 #ifdef __cplusplus
12 extern "C" {
13 #endif
14 
15 enum {
16  /**
17  * Size of an ECDSA P256 signature component in bits.
18  */
19  kUtilEcdsaP256SignatureComponentBits = 256,
20  /**
21  * Size of an ECDSA P256 signature component in bytes.
22  */
23  kUtilEcdsaP256SignatureComponentBytes =
24  kUtilEcdsaP256SignatureComponentBits / 8,
25  /**
26  * Size of an ECDSA P256 signature component in 32b words.
27  */
28  kUtilEcdsaP256SignatureComponentWords =
29  kUtilEcdsaP256SignatureComponentBytes / sizeof(uint32_t),
30 };
31 
32 /**
33  * Rounds up the passed value to get it aligned to the requested number of bits.
34  *
35  * @param input Value to be rounded up.
36  * @param align_bits Number of bits to round the input value to.
37  * @return Rounded up value.
38  */
39 uint32_t util_round_up_to(uint32_t input, uint32_t align_bits);
40 
41 /**
42  * Compute the number of 32-bit words required to store a number of bytes.
43  *
44  * @param bytes Buffer of four bytes that represents the ASN1 header.
45  * @return Size (in 32-bit words) required to store the input size in bytes.
46  */
47 uint32_t util_size_to_words(uint32_t bytes);
48 
49 /**
50  * Reverses a buffer of bytes in place.
51  *
52  * @param[inout] buf Buffer in little endian order.
53  * @param num_bytes Number of bytes in the buffer above.
54  */
55 void util_reverse_bytes(void *buf, size_t num_bytes);
56 
57 /**
58  * Fills hexdump of the byte (lowercase).
59  *
60  * @param byte Byte to convert to a hex string.
61  * @param[out] str String buffer (always 2 bytes) to place hex string encoded
62  * byte in.
63  */
64 void util_hexdump_byte(uint8_t byte, uint8_t *str);
65 
66 /**
67  * Convert the calculated signature (r,s) from little endian to big endian
68  *
69  * @param r ECDSA signature r value
70  * @param s ECDSA signature s value
71  */
72 void util_p256_signature_le_to_be_convert(
73  uint32_t r[kUtilEcdsaP256SignatureComponentWords],
74  uint32_t s[kUtilEcdsaP256SignatureComponentWords]);
75 
76 #ifdef __cplusplus
77 }
78 #endif
79 
80 #endif // OPENTITAN_SW_DEVICE_SILICON_CREATOR_LIB_BASE_UTIL_H_