Embedded SDK
Embedded SDK
Loading...
Searching...
No Matches
lcx_hmac.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_HMAC_H
29#define LCX_HMAC_H
30
31#ifdef HAVE_HMAC
32
33#include "lcx_wrappers.h"
34#include "lcx_hash.h"
35#include "lcx_ripemd160.h"
36#include "lcx_sha256.h"
37#include "lcx_sha512.h"
38#include <stdbool.h>
39#include <stdint.h>
40
44typedef struct {
45 uint8_t key[128];
46 cx_hash_t hash_ctx;
47} cx_hmac_t;
48
49#ifdef HAVE_RIPEMD160
50
54typedef struct {
55 uint8_t key[128];
56 cx_ripemd160_t hash_ctx;
57} cx_hmac_ripemd160_t;
58
77WARN_UNUSED_RESULT cx_err_t cx_hmac_ripemd160_init_no_throw(cx_hmac_ripemd160_t *hmac,
78 const uint8_t *key,
79 size_t key_len);
80
85DEPRECATED static inline int cx_hmac_ripemd160_init(cx_hmac_ripemd160_t *hmac,
86 const unsigned char *key,
87 unsigned int key_len)
88{
89 CX_THROW(cx_hmac_ripemd160_init_no_throw(hmac, key, key_len));
90 return CX_RIPEMD160;
91}
92#endif
93
94#if defined(HAVE_SHA224) || defined(HAVE_SHA256)
98typedef struct {
99 uint8_t key[128];
100 cx_sha256_t hash_ctx;
101} cx_hmac_sha256_t;
102#endif
103
104#ifdef HAVE_SHA224
105
124WARN_UNUSED_RESULT cx_err_t cx_hmac_sha224_init(cx_hmac_sha256_t *hmac,
125 const uint8_t *key,
126 unsigned int key_len);
127#endif
128
129#ifdef HAVE_SHA256
130
149WARN_UNUSED_RESULT cx_err_t cx_hmac_sha256_init_no_throw(cx_hmac_sha256_t *hmac,
150 const uint8_t *key,
151 size_t key_len);
152
157DEPRECATED static inline int cx_hmac_sha256_init(cx_hmac_sha256_t *hmac,
158 const unsigned char *key,
159 unsigned int key_len)
160{
161 CX_THROW(cx_hmac_sha256_init_no_throw(hmac, key, key_len));
162 return CX_SHA256;
163}
164
184size_t cx_hmac_sha256(const uint8_t *key,
185 size_t key_len,
186 const uint8_t *in,
187 size_t len,
188 uint8_t *mac,
189 size_t mac_len);
190
191#endif
192
193#if defined(HAVE_SHA384) || defined(HAVE_SHA512)
197typedef struct {
198 uint8_t key[128];
199 cx_sha512_t hash_ctx;
200} cx_hmac_sha512_t;
201#endif
202
203#ifdef HAVE_SHA384
204
223WARN_UNUSED_RESULT cx_err_t cx_hmac_sha384_init(cx_hmac_sha512_t *hmac,
224 const uint8_t *key,
225 unsigned int key_len);
226#endif
227
228#ifdef HAVE_SHA512
229
248WARN_UNUSED_RESULT cx_err_t cx_hmac_sha512_init_no_throw(cx_hmac_sha512_t *hmac,
249 const uint8_t *key,
250 size_t key_len);
251
256DEPRECATED static inline int cx_hmac_sha512_init(cx_hmac_sha512_t *hmac,
257 const unsigned char *key,
258 unsigned int key_len)
259{
260 CX_THROW(cx_hmac_sha512_init_no_throw(hmac, key, key_len));
261 return CX_SHA512;
262}
263
283size_t cx_hmac_sha512(const uint8_t *key,
284 size_t key_len,
285 const uint8_t *in,
286 size_t len,
287 uint8_t *mac,
288 size_t mac_len);
289
290#endif
291
323WARN_UNUSED_RESULT cx_err_t cx_hmac_no_throw(cx_hmac_t *hmac,
324 uint32_t mode,
325 const uint8_t *in,
326 size_t len,
327 uint8_t *mac,
328 size_t mac_len);
329
334DEPRECATED static inline int cx_hmac(cx_hmac_t *hmac,
335 uint32_t mode,
336 const unsigned char *in,
337 unsigned int len,
338 unsigned char *mac,
339 unsigned int mac_len)
340{
341 CX_THROW(cx_hmac_no_throw(hmac, mode, in, len, mac, mac_len));
342
343 switch (hmac->hash_ctx.info->md_type) {
344#ifdef HAVE_SHA224
345 case CX_SHA224:
346 return CX_SHA224_SIZE;
347#endif
348#ifdef HAVE_SHA256
349 case CX_SHA256:
350 return CX_SHA256_SIZE;
351#endif
352#ifdef HAVE_SHA384
353 case CX_SHA384:
354 return CX_SHA384_SIZE;
355#endif
356#ifdef HAVE_SHA512
357 case CX_SHA512:
358 return CX_SHA512_SIZE;
359#endif
360#ifdef HAVE_RIPEMD160
361 case CX_RIPEMD160:
362 return CX_RIPEMD160_SIZE;
363#endif
364 default:
365 CX_THROW(CX_INVALID_PARAMETER);
366 return 0;
367 }
368}
369
390WARN_UNUSED_RESULT cx_err_t cx_hmac_init(cx_hmac_t *hmac,
391 cx_md_t hash_id,
392 const uint8_t *key,
393 size_t key_len);
394
412WARN_UNUSED_RESULT cx_err_t cx_hmac_update(cx_hmac_t *hmac, const uint8_t *in, size_t in_len);
413
429WARN_UNUSED_RESULT cx_err_t cx_hmac_final(cx_hmac_t *ctx, uint8_t *out, size_t *out_len);
430
431#endif // HAVE_HMAC
432
433#endif // LCX_HMAC_H
Hash functions.
RIPEMD-160 hash function.
SHA-2 (Secure Hash Algorithm 2)
SHA-2 (Secure Hash Algorithm 2)
#define CX_THROW(call)
unsigned char uint8_t
Definition usbd_conf.h:53