Embedded SDK
Embedded SDK
bip32.c
Go to the documentation of this file.
1 /*****************************************************************************
2  * (c) 2020 Ledger SAS.
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 #include <stdio.h> // snprintf
18 #include <string.h> // memset, strlen
19 #include <stddef.h> // size_t
20 #include <stdint.h> // uint*_t
21 #include <stdbool.h> // bool
22 
23 #include "bip32.h"
24 #include "read.h"
25 
26 bool bip32_path_read(const uint8_t *in, size_t in_len, uint32_t *out, size_t out_len)
27 {
28  if (out_len == 0 || out_len > MAX_BIP32_PATH) {
29  return false;
30  }
31 
32  size_t offset = 0;
33 
34  for (size_t i = 0; i < out_len; i++) {
35  if (offset + 4 > in_len) {
36  return false;
37  }
38  out[i] = read_u32_be(in, offset);
39  offset += 4;
40  }
41 
42  return true;
43 }
44 
45 bool bip32_path_format(const uint32_t *bip32_path, size_t bip32_path_len, char *out, size_t out_len)
46 {
47  if (bip32_path_len == 0 || bip32_path_len > MAX_BIP32_PATH) {
48  return false;
49  }
50 
51  size_t offset = 0;
52 
53  for (uint16_t i = 0; i < bip32_path_len; i++) {
54  size_t written;
55 
56  snprintf(out + offset, out_len - offset, "%u", bip32_path[i] & 0x7FFFFFFFu);
57  written = strlen(out + offset);
58  if (written == 0 || written >= out_len - offset) {
59  memset(out, 0, out_len);
60  return false;
61  }
62  offset += written;
63 
64  if ((bip32_path[i] & 0x80000000u) != 0) {
65  snprintf(out + offset, out_len - offset, "'");
66  written = strlen(out + offset);
67  if (written == 0 || written >= out_len - offset) {
68  memset(out, 0, out_len);
69  return false;
70  }
71  offset += written;
72  }
73 
74  if (i != bip32_path_len - 1) {
75  snprintf(out + offset, out_len - offset, "/");
76  written = strlen(out + offset);
77  if (written == 0 || written >= out_len - offset) {
78  memset(out, 0, out_len);
79  return false;
80  }
81  offset += written;
82  }
83  }
84 
85  return true;
86 }
bool bip32_path_format(const uint32_t *bip32_path, size_t bip32_path_len, char *out, size_t out_len)
Definition: bip32.c:45
bool bip32_path_read(const uint8_t *in, size_t in_len, uint32_t *out, size_t out_len)
Definition: bip32.c:26
#define MAX_BIP32_PATH
Definition: bip32.h:10
uint32_t read_u32_be(const uint8_t *ptr, size_t offset)
Definition: read.c:26
unsigned short uint16_t
Definition: usbd_conf.h:54
unsigned char uint8_t
Definition: usbd_conf.h:53