Embedded SDK
Embedded SDK
Loading...
Searching...
No Matches
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
26bool bip32_path_read(const uint8_t *in, size_t in_len, uint32_t *out, size_t out_len)
27{
28 if (in == 0 || in_len == 0) {
29 return false;
30 }
31
32 if (out_len == 0 || out_len > MAX_BIP32_PATH) {
33 return false;
34 }
35
36 size_t offset = 0;
37
38 for (size_t i = 0; i < out_len; i++) {
39 if (offset + 4 > in_len) {
40 return false;
41 }
42 out[i] = read_u32_be(in, offset);
43 offset += 4;
44 }
45
46 return true;
47}
48
49bool bip32_path_format(const uint32_t *bip32_path, size_t bip32_path_len, char *out, size_t out_len)
50{
51 if (bip32_path_len == 0 || bip32_path_len > MAX_BIP32_PATH) {
52 return false;
53 }
54
55 size_t offset = 0;
56
57 for (uint16_t i = 0; i < bip32_path_len; i++) {
58 size_t written;
59
60 snprintf(out + offset, out_len - offset, "%u", bip32_path[i] & 0x7FFFFFFFu);
61 written = strlen(out + offset);
62 if (written == 0 || written >= out_len - offset) {
63 memset(out, 0, out_len);
64 return false;
65 }
66 offset += written;
67
68 if ((bip32_path[i] & 0x80000000u) != 0) {
69 snprintf(out + offset, out_len - offset, "'");
70 written = strlen(out + offset);
71 if (written == 0 || written >= out_len - offset) {
72 memset(out, 0, out_len);
73 return false;
74 }
75 offset += written;
76 }
77
78 if (i != bip32_path_len - 1) {
79 snprintf(out + offset, out_len - offset, "/");
80 written = strlen(out + offset);
81 if (written == 0 || written >= out_len - offset) {
82 memset(out, 0, out_len);
83 return false;
84 }
85 offset += written;
86 }
87 }
88
89 return true;
90}
91
92bool bip32_path_format_simple(path_bip32_t *bip32, char *out, size_t out_len)
93{
94 if (bip32 == NULL || out == NULL || out_len == 0) {
95 return false;
96 }
97 return bip32_path_format(bip32->path, bip32->length, out, out_len);
98}
bool bip32_path_format(const uint32_t *bip32_path, size_t bip32_path_len, char *out, size_t out_len)
Definition bip32.c:49
bool bip32_path_format_simple(path_bip32_t *bip32, char *out, size_t out_len)
Format a BIP32 path as a string.
Definition bip32.c:92
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:15
uint32_t read_u32_be(const uint8_t *ptr, size_t offset)
Definition read.c:26
uint32_t path[MAX_BIP32_PATH]
Definition bip32.h:19
uint8_t length
Definition bip32.h:18