-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcrypto.h
105 lines (93 loc) · 4.03 KB
/
crypto.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#pragma once
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include "api/ecall-params.h"
#include "api/uint256.h"
#include "ecall.h"
#include "speculos.h"
#define CX_CURVE_256K1 CX_CURVE_SECP256K1
#define CX_CURVE_256R1 CX_CURVE_SECP256R1
void ecfp_init_private_key(const cx_curve_t curve,
const uint8_t *raw_key,
const size_t key_len,
cx_ecfp_private_key_t *key);
void ecfp_init_public_key(const cx_curve_t curve,
const uint8_t *raw_key,
cx_ecfp_public_key_t *key);
bool ecfp_generate_keypair(const cx_curve_t curve,
const uint8_t *rawkey,
const size_t rawkey_size,
cx_ecfp_public_key_t *pubkey,
cx_ecfp_private_key_t *privkey);
bool ecfp_get_pubkey(const cx_curve_t curve,
const cx_ecfp_private_key_t *privkey,
cx_ecfp_public_key_t *pubkey);
bool hash_update(const cx_hash_id_t hash_id,
ctx_hash_guest_t *ctx,
const uint8_t *buffer,
const size_t size);
bool hash_final(const cx_hash_id_t hash_id, ctx_hash_guest_t *ctx, uint8_t *digest);
void ripemd160_init(ctx_ripemd160_t *ctx);
void ripemd160_update(ctx_ripemd160_t *ctx, const uint8_t *buffer, const size_t size);
void ripemd160_final(ctx_ripemd160_t *ctx, uint8_t *digest);
void ripemd160(const uint8_t *buffer, size_t size, uint8_t *digest);
void sha256sum(const uint8_t *buffer, size_t size, uint8_t *digest);
void sha256_init(ctx_sha256_t *ctx);
void sha256_update(ctx_sha256_t *ctx, const uint8_t *buffer, const size_t size);
void sha256_final(ctx_sha256_t *ctx, uint8_t *digest);
void sha256(const uint8_t *buffer, size_t size, uint8_t *digest);
void sha3_256(const uint8_t *buffer, size_t size, uint8_t *digest);
void sha3_256_init(ctx_sha3_t *ctx);
void sha3_256_update(ctx_sha3_t *ctx, const uint8_t *buffer, const size_t size);
void sha3_256_final(ctx_sha3_t *ctx, uint8_t *digest);
static inline bool derive_node_bip32(cx_curve_t curve,
const unsigned int *path,
size_t path_count,
uint8_t *private_key,
uint8_t *chain)
{
return ecall_derive_node_bip32(curve, path, path_count, private_key, chain);
}
static inline size_t ecdsa_sign(const cx_ecfp_private_key_t *key,
const int mode,
const cx_md_t hash_id,
const uint8_t *hash,
uint8_t *sig,
size_t sig_len)
{
return ecall_ecdsa_sign(key, mode, hash_id, hash, sig, sig_len, NULL);
}
static inline size_t extended_ecdsa_sign(const cx_ecfp_private_key_t *key,
const int mode,
const cx_md_t hash_id,
const uint8_t *hash,
uint8_t *sig,
size_t sig_len,
int *parity)
{
return ecall_ecdsa_sign(key, mode, hash_id, hash, sig, sig_len, parity);
}
static bool ecdsa_verify(const cx_ecfp_public_key_t *key,
const uint8_t *hash,
const uint8_t *sig,
const size_t sig_len)
{
return ecall_ecdsa_verify(key, hash, sig, sig_len);
}
static void get_random_bytes(uint8_t *buffer, const size_t size)
{
ecall_get_random_bytes(buffer, size);
}
static inline bool mult(uint8_t *r, const uint8_t *a, const uint8_t *b, size_t len)
{
return ecall_multm(r, a, b, NULL, len);
}
static inline bool multm(uint8_t *r,
const uint8_t *a,
const uint8_t *b,
const uint8_t *m,
size_t len)
{
return ecall_multm(r, a, b, m, len);
}