Embedded SDK
Embedded SDK
Loading...
Searching...
No Matches
lcx_rng.h
Go to the documentation of this file.
1
2/*******************************************************************************
3 * Ledger Nano S - Secure firmware
4 * (c) 2022 Ledger
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 ********************************************************************************/
18
28#ifndef LCX_RNG_H
29#define LCX_RNG_H
30
31#include "lcx_wrappers.h"
32#include "lcx_hash.h"
33#include "libcxng.h"
34
35#ifdef HAVE_RNG_RFC6979
36#define CX_RFC6979_BUFFER_LENGTH 64
37#define CX_RFC6979_MAX_RLEN 66
38
39typedef struct {
40 uint8_t v[CX_RFC6979_BUFFER_LENGTH + 1];
41 uint8_t k[CX_RFC6979_BUFFER_LENGTH];
42 uint8_t q[CX_RFC6979_MAX_RLEN];
43 uint32_t q_len;
44 uint32_t r_len;
45 uint8_t tmp[CX_RFC6979_MAX_RLEN];
46 cx_md_t hash_id;
47 size_t md_len;
48
49 union {
50#if (!defined(HAVE_SHA512) && !defined(HAVE_SHA384) && !defined(HAVE_SHA256) \
51 && !defined(HAVE_SHA224)) \
52 || !defined(HAVE_HMAC)
53#error No hmac defined for rfc6979 support
54#endif
55
56 cx_hmac_t hmac;
57
58#if defined(HAVE_SHA512) || defined(HAVE_SHA384)
59 cx_hmac_sha512_t hmac_sha512;
60#endif
61
62#if defined(HAVE_SHA256) || defined(HAVE_SHA224)
63 cx_hmac_sha256_t hmac_sha256;
64#endif
65 };
66} cx_rnd_rfc6979_ctx_t;
67#endif // HAVE_RNG_RFC6979
68
69#ifdef HAVE_RNG
79void cx_rng_no_throw(uint8_t *buffer, size_t len);
80
92static inline unsigned char *cx_rng(uint8_t *buffer, size_t len)
93{
94 cx_rng_no_throw(buffer, len);
95 return buffer;
96}
97
103static inline uint32_t cx_rng_u32(void)
104{
105 uint32_t r;
106 cx_rng_no_throw((uint8_t *) &r, sizeof(uint32_t));
107 return r;
108}
109
115static inline uint8_t cx_rng_u8(void)
116{
117 uint8_t r;
118 cx_rng_no_throw((uint8_t *) &r, sizeof(uint8_t));
119 return r;
120}
121
122typedef uint32_t (*cx_rng_u32_range_randfunc_t)(void);
123
139uint32_t cx_rng_u32_range_func(uint32_t a, uint32_t b, cx_rng_u32_range_randfunc_t randfunc);
140
153static inline uint32_t cx_rng_u32_range(uint32_t a, uint32_t b)
154{
155 return cx_rng_u32_range_func(a, b, cx_rng_u32);
156}
157
184WARN_UNUSED_RESULT cx_err_t cx_rng_rfc6979(cx_md_t hash_id,
185 const uint8_t *x,
186 size_t x_len,
187 const uint8_t *h1,
188 size_t h1_len,
189 const uint8_t *q,
190 size_t q_len,
191 uint8_t *out,
192 size_t out_len);
193#endif // HAVE_RNG
194
195#ifdef HAVE_RNG_RFC6979
220WARN_UNUSED_RESULT cx_err_t cx_rng_rfc6979_init(cx_rnd_rfc6979_ctx_t *rfc_ctx,
221 cx_md_t hash_id,
222 const uint8_t *x,
223 size_t x_len,
224 const uint8_t *h1,
225 size_t h1_len,
226 const uint8_t *q,
227 size_t q_len);
228
245WARN_UNUSED_RESULT cx_err_t cx_rng_rfc6979_next(cx_rnd_rfc6979_ctx_t *rfc_ctx,
246 uint8_t *out,
247 size_t out_len);
248#endif // HAVE_RNG_RFC6979
249
250#endif // LCX_RNG_H
Hash functions.
Include cryptography files.