-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcrypto.c
151 lines (127 loc) · 4.02 KB
/
crypto.c
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <string.h>
#include "crypto.h"
#include "ecall.h"
#include "sdk.h"
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)
{
key->curve = curve;
key->d_len = key_len;
memcpy(key->d, raw_key, key_len);
}
void ecfp_init_public_key(const cx_curve_t curve, const uint8_t *raw_key, cx_ecfp_public_key_t *key)
{
key->curve = curve;
key->W_len = sizeof(key->W);
memcpy(key->W, raw_key, sizeof(key->W));
}
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)
{
ecfp_init_private_key(curve, rawkey, rawkey_size, privkey);
return ecall_cx_ecfp_generate_pair(curve, pubkey, privkey, true);
}
/**
* Get the public key associated to the given private key.
*/
bool ecfp_get_pubkey(const cx_curve_t curve,
const cx_ecfp_private_key_t *privkey,
cx_ecfp_public_key_t *pubkey)
{
return ecall_cx_ecfp_generate_pair(curve, pubkey, (cx_ecfp_private_key_t *)privkey, true);
}
bool hash_update(const cx_hash_id_t hash_id,
ctx_hash_guest_t *ctx,
const uint8_t *buffer,
const size_t size)
{
return ecall_hash_update(hash_id, ctx, buffer, size);
}
bool hash_final(const cx_hash_id_t hash_id, ctx_hash_guest_t *ctx, uint8_t *digest)
{
return ecall_hash_final(hash_id, ctx, digest);
}
void ripemd160_init(ctx_ripemd160_t *ctx)
{
ctx->initialized = false;
}
void ripemd160_update(ctx_ripemd160_t *ctx, const uint8_t *buffer, const size_t size)
{
if (!hash_update(HASH_ID_RIPEMD160, (ctx_hash_guest_t *)ctx, buffer, size)) {
/* this should never happen unless ctx is corrupted */
fatal("ripemd160_update");
}
}
void ripemd160_final(ctx_ripemd160_t *ctx, uint8_t *digest)
{
if (!hash_final(HASH_ID_RIPEMD160, (ctx_hash_guest_t *)ctx, digest)) {
/* this should never happen unless ctx is corrupted */
fatal("ripemd160_final");
}
}
void ripemd160(const uint8_t *buffer, size_t size, uint8_t *digest)
{
ctx_ripemd160_t ctx;
ripemd160_init(&ctx);
ripemd160_update(&ctx, buffer, size);
ripemd160_final(&ctx, digest);
}
void sha3_256(const uint8_t *buffer, size_t size, uint8_t *digest)
{
ctx_sha3_t ctx;
sha3_256_init(&ctx);
sha3_256_update(&ctx, buffer, size);
sha3_256_final(&ctx, digest);
}
void sha3_256_init(ctx_sha3_t *ctx)
{
ctx->initialized = false;
}
void sha3_256_update(ctx_sha3_t *ctx, const uint8_t *buffer, const size_t size)
{
if (!hash_update(HASH_ID_SHA3_256, (ctx_hash_guest_t *)ctx, buffer, size)) {
/* this should never happen unless ctx is corrupted */
fatal("sha3_256_update");
}
}
void sha3_256_final(ctx_sha3_t *ctx, uint8_t *digest)
{
if (!hash_final(HASH_ID_SHA3_256, (ctx_hash_guest_t *)ctx, digest)) {
/* this should never happen unless ctx is corrupted */
fatal("sha3_256_final");
}
}
void sha256_init(ctx_sha256_t *ctx)
{
ctx->initialized = false;
}
void sha256_update(ctx_sha256_t *ctx, const uint8_t *buffer, const size_t size)
{
if (!hash_update(HASH_ID_SHA256, (ctx_hash_guest_t *)ctx, buffer, size)) {
/* this should never happen unless ctx is corrupted */
fatal("sha256_update");
}
}
void sha256_final(ctx_sha256_t *ctx, uint8_t *digest)
{
if (!hash_final(HASH_ID_SHA256, (ctx_hash_guest_t *)ctx, digest)) {
/* this should never happen unless ctx is corrupted */
fatal("sha256_final");
}
}
void sha256(const uint8_t *buffer, size_t size, uint8_t *digest)
{
ctx_sha256_t ctx;
sha256_init(&ctx);
sha256_update(&ctx, buffer, size);
sha256_final(&ctx, digest);
}
void sha256sum(const uint8_t *buffer, size_t size, uint8_t *digest)
{
return sha256(buffer, size, digest);
}