Software APIs
util.c
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 #include "sw/device/silicon_creator/lib/base/util.h"
6 
7 uint32_t util_round_up_to(uint32_t input, uint32_t align_bits) {
8  uint32_t mask = (1 << align_bits) - 1;
9  return (input + mask) & ~mask;
10 }
11 
12 uint32_t util_size_to_words(uint32_t bytes) {
13  return util_round_up_to(bytes, 2) / sizeof(uint32_t);
14 }
15 
16 void util_reverse_bytes(void *buf, size_t num_bytes) {
17  unsigned char temp;
18  unsigned char *byte_buf = buf;
19  for (size_t i = 0; i < (num_bytes / 2); ++i) {
20  temp = byte_buf[i];
21  byte_buf[i] = byte_buf[num_bytes - i - 1];
22  byte_buf[num_bytes - i - 1] = temp;
23  }
24 }
25 
26 // Returns hexdump character for the half-byte.
27 static inline uint8_t hexdump_halfbyte(uint8_t half_byte) {
28  if (half_byte < 10)
29  return '0' + half_byte;
30  else
31  return 'a' + half_byte - 10;
32 }
33 
34 void util_hexdump_byte(uint8_t byte, uint8_t *str) {
35  str[0] = hexdump_halfbyte((byte & 0xF0) >> 4);
36  str[1] = hexdump_halfbyte(byte & 0x0F);
37 }
38 
39 void util_p256_signature_le_to_be_convert(
40  uint32_t r[kUtilEcdsaP256SignatureComponentWords],
41  uint32_t s[kUtilEcdsaP256SignatureComponentWords]) {
42  util_reverse_bytes(r, kUtilEcdsaP256SignatureComponentBytes);
43  util_reverse_bytes(s, kUtilEcdsaP256SignatureComponentBytes);
44 }