@@ -72,17 +72,21 @@ __Z_INLINE enum cx_md_e get_cx_hash_kind() {
72
72
}
73
73
}
74
74
75
- uint8_t crypto_extractPublicKey (const uint32_t path [HDPATH_LEN_DEFAULT ], uint8_t * pubKey , uint16_t pubKeyLen ) {
75
+ zxerr_t crypto_extractPublicKey (const uint32_t path [HDPATH_LEN_DEFAULT ], uint8_t * pubKey , uint16_t pubKeyLen ) {
76
+ zemu_log_stack ("crypto_extractPublicKey" );
76
77
MEMZERO (pubKey , pubKeyLen );
78
+
77
79
cx_curve_t curve = get_cx_curve ();
78
- if (curve == CX_CURVE_NONE ) {
79
- return 0 ;
80
+ if (curve != CX_CURVE_SECP256K1 && curve != CX_CURVE_SECP256R1 ) {
81
+ zemu_log_stack ("extractPublicKey: invalid_crypto_settings" );
82
+ return zxerr_invalid_crypto_settings ;
80
83
}
81
84
82
85
const uint32_t domainSize = 32 ;
83
86
const uint32_t pkSize = 1 + 2 * domainSize ;
84
87
if (pubKeyLen < pkSize ) {
85
- return 0 ;
88
+ zemu_log_stack ("extractPublicKey: zxerr_buffer_too_small" );
89
+ return zxerr_buffer_too_small ;
86
90
}
87
91
88
92
cx_ecfp_public_key_t cx_publicKey ;
@@ -92,13 +96,17 @@ uint8_t crypto_extractPublicKey(const uint32_t path[HDPATH_LEN_DEFAULT], uint8_t
92
96
BEGIN_TRY
93
97
{
94
98
TRY {
99
+ zemu_log_stack ("extractPublicKey: derive_node_bip32" );
95
100
os_perso_derive_node_bip32 (curve ,
96
101
path ,
97
102
HDPATH_LEN_DEFAULT ,
98
103
privateKeyData , NULL );
99
104
105
+ zemu_log_stack ("extractPublicKey: cx_ecfp_init_private_key" );
100
106
cx_ecfp_init_private_key (curve , privateKeyData , 32 , & cx_privateKey );
101
107
cx_ecfp_init_public_key (curve , NULL , 0 , & cx_publicKey );
108
+
109
+ zemu_log_stack ("extractPublicKey: cx_ecfp_generate_pair" );
102
110
cx_ecfp_generate_pair (curve , & cx_publicKey , & cx_privateKey , 1 );
103
111
}
104
112
FINALLY {
@@ -109,7 +117,7 @@ uint8_t crypto_extractPublicKey(const uint32_t path[HDPATH_LEN_DEFAULT], uint8_t
109
117
END_TRY ;
110
118
111
119
memcpy (pubKey , cx_publicKey .W , pkSize );
112
- return pkSize ;
120
+ return zxerr_ok ;
113
121
}
114
122
115
123
typedef struct {
@@ -131,14 +139,15 @@ uint16_t digest_message(uint8_t *digest, uint16_t digestMax, const uint8_t *mess
131
139
case sha2_256 : {
132
140
zemu_log_stack ("sha2_256" );
133
141
if (digestMax < CX_SHA256_SIZE ) {
134
- return 0 ;
142
+ zemu_log_stack ("digest_message: zxerr_buffer_too_small" );
143
+ return zxerr_buffer_too_small ;
135
144
}
136
145
sha256 (message , messageLen , digest );
137
146
return CX_SHA256_SIZE ;
138
147
}
139
148
case sha3_256 : {
140
149
if (digestMax < 32 ) {
141
- return 0 ;
150
+ return zxerr_buffer_too_small ;
142
151
}
143
152
zemu_log_stack ("sha3_256" );
144
153
cx_sha3_t sha3 ;
@@ -147,24 +156,35 @@ uint16_t digest_message(uint8_t *digest, uint16_t digestMax, const uint8_t *mess
147
156
zemu_log_stack ("sha3_256 ready" );
148
157
return 32 ;
149
158
}
150
- default :
151
- return 0 ;
159
+ default : {
160
+ zemu_log_stack ("digest_message: zxerr_invalid_crypto_settings" );
161
+ return zxerr_invalid_crypto_settings ;
162
+ }
152
163
}
153
164
}
154
165
155
- uint16_t crypto_sign (uint8_t * buffer , uint16_t signatureMaxlen , const uint8_t * message , uint16_t messageLen ) {
166
+ zxerr_t crypto_sign (uint8_t * buffer , uint16_t signatureMaxlen , const uint8_t * message , uint16_t messageLen , uint16_t * sigSize ) {
167
+ zemu_log_stack ("crypto_sign" );
168
+
156
169
cx_curve_t curve = get_cx_curve ();
157
- if (curve == CX_CURVE_NONE ) {
158
- return 0 ;
170
+ if (curve != CX_CURVE_SECP256K1 && curve != CX_CURVE_SECP256R1 ) {
171
+ zemu_log_stack ("crypto_sign: invalid_crypto_settings" );
172
+ return zxerr_invalid_crypto_settings ;
159
173
}
160
174
161
175
const uint32_t domainSize = 32 ;
162
- uint8_t messageDigest [128 ];
176
+ uint8_t messageDigest [32 ];
163
177
164
178
const enum cx_md_e cxhash_kind = get_cx_hash_kind ();
165
179
const uint16_t messageDigestSize = digest_message (messageDigest , sizeof (messageDigest ), message , messageLen );
166
- if (cxhash_kind == CX_NONE || messageDigestSize == 0 ) {
167
- return 0 ;
180
+ if (messageDigestSize != 32 ) {
181
+ zemu_log_stack ("crypto_sign: zxerr_out_of_bounds" );
182
+ return zxerr_out_of_bounds ;
183
+ }
184
+
185
+ if (cxhash_kind != CX_SHA256 && cxhash_kind != CX_SHA3 ) {
186
+ zemu_log_stack ("crypto_sign: zxerr_invalid_crypto_settings" );
187
+ return zxerr_invalid_crypto_settings ;
168
188
}
169
189
170
190
cx_ecfp_private_key_t cx_privateKey ;
@@ -191,7 +211,7 @@ uint16_t crypto_sign(uint8_t *buffer, uint16_t signatureMaxlen, const uint8_t *m
191
211
zemu_log_stack ("cx_ecdsa_sign" );
192
212
signatureLength = cx_ecdsa_sign (& cx_privateKey ,
193
213
CX_RND_RFC6979 | CX_LAST ,
194
- cxhash_kind ,
214
+ CX_SHA256 ,
195
215
messageDigest ,
196
216
messageDigestSize ,
197
217
signature -> der_signature ,
@@ -209,11 +229,12 @@ uint16_t crypto_sign(uint8_t *buffer, uint16_t signatureMaxlen, const uint8_t *m
209
229
err_convert_e err = convertDERtoRSV (signature -> der_signature , info , signature -> r , signature -> s , & signature -> v );
210
230
if (err != no_error ) {
211
231
// Error while converting so return length 0
212
- return 0 ;
232
+ return zxerr_invalid_crypto_settings ;
213
233
}
214
234
215
235
// return actual size using value from signatureLength
216
- return sizeof_field (signature_t , r ) + sizeof_field (signature_t , s ) + sizeof_field (signature_t , v ) + signatureLength ;
236
+ * sigSize = sizeof_field (signature_t , r ) + sizeof_field (signature_t , s ) + sizeof_field (signature_t , v ) + signatureLength ;
237
+ return zxerr_ok ;
217
238
}
218
239
219
240
typedef struct {
@@ -222,22 +243,25 @@ typedef struct {
222
243
uint8_t padding [4 ];
223
244
} __attribute__((packed )) answer_t ;
224
245
225
- uint16_t crypto_fillAddress (uint8_t * buffer , uint16_t buffer_len ) {
246
+ zxerr_t crypto_fillAddress (uint8_t * buffer , uint16_t buffer_len , uint16_t * addrLen ) {
226
247
MEMZERO (buffer , buffer_len );
227
248
228
249
if (buffer_len < sizeof (answer_t )) {
229
- return 0 ;
250
+ zemu_log_stack ("crypto_fillAddress: zxerr_buffer_too_small" );
251
+ return zxerr_buffer_too_small ;
230
252
}
231
253
232
254
answer_t * const answer = (answer_t * ) buffer ;
233
255
234
- if (crypto_extractPublicKey (hdPath , answer -> publicKey , sizeof_field (answer_t , publicKey )) == 0 ) {
235
- return 0 ;
256
+ zxerr_t err = crypto_extractPublicKey (hdPath , answer -> publicKey , sizeof_field (answer_t , publicKey ));
257
+ if ( err != zxerr_ok ) {
258
+ return err ;
236
259
}
237
260
238
261
array_to_hexstr (answer -> addrStr , sizeof_field (answer_t , addrStr ) + 2 , answer -> publicKey , sizeof_field (answer_t , publicKey ) );
239
262
240
- return sizeof (answer_t ) - sizeof_field (answer_t , padding );
263
+ * addrLen = sizeof (answer_t ) - sizeof_field (answer_t , padding );
264
+ return zxerr_ok ;
241
265
}
242
266
243
267
#endif
0 commit comments