mirror of
https://github.com/trezor/trezor-firmware.git
synced 2024-12-26 16:18:22 +00:00
trezor.crypto: remove vstr usage, refactor mp_obj_new_str_from_vstr into mp_obj_new_bytes and mp_obj_new_str
This commit is contained in:
parent
8dd5edb4a1
commit
4a7592d470
@ -78,7 +78,7 @@ STATIC mp_obj_t mod_trezorconfig_get(mp_obj_t app, mp_obj_t key) {
|
||||
if (sectrue != storage_get(appkey, &val, &len) || len == 0) {
|
||||
return mp_const_empty_bytes;
|
||||
}
|
||||
return mp_obj_new_str_of_type(&mp_type_bytes, val, len);
|
||||
return mp_obj_new_bytes(val, len);
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorconfig_get_obj, mod_trezorconfig_get);
|
||||
|
||||
|
@ -98,11 +98,10 @@ STATIC mp_obj_t mod_trezorcrypto_AES_make_new(const mp_obj_type_t *type, size_t
|
||||
STATIC mp_obj_t mod_trezorcrypto_AES_update(mp_obj_t self, mp_obj_t data) {
|
||||
mp_buffer_info_t buf;
|
||||
mp_get_buffer_raise(data, &buf, MP_BUFFER_READ);
|
||||
vstr_t vstr;
|
||||
vstr_init_len(&vstr, buf.len);
|
||||
if (buf.len == 0) {
|
||||
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||||
return mp_const_empty_bytes;
|
||||
}
|
||||
uint8_t out[buf.len];
|
||||
mp_obj_AES_t *o = MP_OBJ_TO_PTR(self);
|
||||
switch (o->mode & AESModeMask) {
|
||||
case ECB:
|
||||
@ -110,9 +109,9 @@ STATIC mp_obj_t mod_trezorcrypto_AES_update(mp_obj_t self, mp_obj_t data) {
|
||||
mp_raise_ValueError("Invalid data length");
|
||||
}
|
||||
if ((o->mode & AESDirMask) == Encrypt) {
|
||||
aes_ecb_encrypt(buf.buf, (unsigned char *)vstr.buf, buf.len, &(o->ctx.encrypt_ctx));
|
||||
aes_ecb_encrypt(buf.buf, out, buf.len, &(o->ctx.encrypt_ctx));
|
||||
} else {
|
||||
aes_ecb_decrypt(buf.buf, (unsigned char *)vstr.buf, buf.len, &(o->ctx.decrypt_ctx));
|
||||
aes_ecb_decrypt(buf.buf, out, buf.len, &(o->ctx.decrypt_ctx));
|
||||
}
|
||||
break;
|
||||
case CBC:
|
||||
@ -120,26 +119,26 @@ STATIC mp_obj_t mod_trezorcrypto_AES_update(mp_obj_t self, mp_obj_t data) {
|
||||
mp_raise_ValueError("Invalid data length");
|
||||
}
|
||||
if ((o->mode & AESDirMask) == Encrypt) {
|
||||
aes_cbc_encrypt(buf.buf, (unsigned char *)vstr.buf, buf.len, o->iv, &(o->ctx.encrypt_ctx));
|
||||
aes_cbc_encrypt(buf.buf, out, buf.len, o->iv, &(o->ctx.encrypt_ctx));
|
||||
} else {
|
||||
aes_cbc_decrypt(buf.buf, (unsigned char *)vstr.buf, buf.len, o->iv, &(o->ctx.decrypt_ctx));
|
||||
aes_cbc_decrypt(buf.buf, out, buf.len, o->iv, &(o->ctx.decrypt_ctx));
|
||||
}
|
||||
break;
|
||||
case CFB:
|
||||
if ((o->mode & AESDirMask) == Encrypt) {
|
||||
aes_cfb_encrypt(buf.buf, (unsigned char *)vstr.buf, buf.len, o->iv, &(o->ctx.encrypt_ctx));
|
||||
aes_cfb_encrypt(buf.buf, out, buf.len, o->iv, &(o->ctx.encrypt_ctx));
|
||||
} else {
|
||||
aes_cfb_decrypt(buf.buf, (unsigned char *)vstr.buf, buf.len, o->iv, &(o->ctx.encrypt_ctx));
|
||||
aes_cfb_decrypt(buf.buf, out, buf.len, o->iv, &(o->ctx.encrypt_ctx));
|
||||
}
|
||||
break;
|
||||
case OFB: // (encrypt == decrypt)
|
||||
aes_ofb_crypt(buf.buf, (unsigned char *)vstr.buf, buf.len, o->iv, &(o->ctx.encrypt_ctx));
|
||||
aes_ofb_crypt(buf.buf, out, buf.len, o->iv, &(o->ctx.encrypt_ctx));
|
||||
break;
|
||||
case CTR: // (encrypt == decrypt)
|
||||
aes_ctr_crypt(buf.buf, (unsigned char *)vstr.buf, buf.len, o->ctr, aes_ctr_cbuf_inc, &(o->ctx.encrypt_ctx));
|
||||
aes_ctr_crypt(buf.buf, out, buf.len, o->ctr, aes_ctr_cbuf_inc, &(o->ctx.encrypt_ctx));
|
||||
break;
|
||||
}
|
||||
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||||
return mp_obj_new_bytes(out, sizeof(out));
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_AES_update_obj, mod_trezorcrypto_AES_update);
|
||||
|
||||
|
@ -167,23 +167,18 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_HDNode_derive_path_obj, mod_tr
|
||||
|
||||
STATIC mp_obj_t serialize_public_private(mp_obj_t self, bool use_public, uint32_t version) {
|
||||
mp_obj_HDNode_t *o = MP_OBJ_TO_PTR(self);
|
||||
|
||||
vstr_t vstr;
|
||||
vstr_init(&vstr, XPUB_MAXLEN);
|
||||
|
||||
char xpub[XPUB_MAXLEN];
|
||||
int written;
|
||||
if (use_public) {
|
||||
hdnode_fill_public_key(&o->hdnode);
|
||||
written = hdnode_serialize_public(&o->hdnode, o->fingerprint, version, vstr.buf, vstr.alloc);
|
||||
written = hdnode_serialize_public(&o->hdnode, o->fingerprint, version, xpub, XPUB_MAXLEN);
|
||||
} else {
|
||||
written = hdnode_serialize_private(&o->hdnode, o->fingerprint, version, vstr.buf, vstr.alloc);
|
||||
written = hdnode_serialize_private(&o->hdnode, o->fingerprint, version, xpub, XPUB_MAXLEN);
|
||||
}
|
||||
if (written <= 0) {
|
||||
mp_raise_ValueError("Failed to serialize");
|
||||
}
|
||||
vstr.len = written - 1; // written includes 0 at the end
|
||||
|
||||
return mp_obj_new_str_from_vstr(&mp_type_str, &vstr);
|
||||
return mp_obj_new_str(xpub, written - 1, false); // written includes 0 at the end
|
||||
}
|
||||
|
||||
/// def serialize_public(self, version: int) -> str:
|
||||
@ -256,7 +251,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_HDNode_child_num_obj, mod_trez
|
||||
/// '''
|
||||
STATIC mp_obj_t mod_trezorcrypto_HDNode_chain_code(mp_obj_t self) {
|
||||
mp_obj_HDNode_t *o = MP_OBJ_TO_PTR(self);
|
||||
return mp_obj_new_str_of_type(&mp_type_bytes, o->hdnode.chain_code, sizeof(o->hdnode.chain_code));
|
||||
return mp_obj_new_bytes(o->hdnode.chain_code, sizeof(o->hdnode.chain_code));
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_HDNode_chain_code_obj, mod_trezorcrypto_HDNode_chain_code);
|
||||
|
||||
@ -266,7 +261,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_HDNode_chain_code_obj, mod_tre
|
||||
/// '''
|
||||
STATIC mp_obj_t mod_trezorcrypto_HDNode_private_key(mp_obj_t self) {
|
||||
mp_obj_HDNode_t *o = MP_OBJ_TO_PTR(self);
|
||||
return mp_obj_new_str_of_type(&mp_type_bytes, o->hdnode.private_key, sizeof(o->hdnode.private_key));
|
||||
return mp_obj_new_bytes(o->hdnode.private_key, sizeof(o->hdnode.private_key));
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_HDNode_private_key_obj, mod_trezorcrypto_HDNode_private_key);
|
||||
|
||||
@ -277,7 +272,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_HDNode_private_key_obj, mod_tr
|
||||
STATIC mp_obj_t mod_trezorcrypto_HDNode_public_key(mp_obj_t self) {
|
||||
mp_obj_HDNode_t *o = MP_OBJ_TO_PTR(self);
|
||||
hdnode_fill_public_key(&o->hdnode);
|
||||
return mp_obj_new_str_of_type(&mp_type_bytes, o->hdnode.public_key, sizeof(o->hdnode.public_key));
|
||||
return mp_obj_new_bytes(o->hdnode.public_key, sizeof(o->hdnode.public_key));
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_HDNode_public_key_obj, mod_trezorcrypto_HDNode_public_key);
|
||||
|
||||
@ -289,12 +284,9 @@ STATIC mp_obj_t mod_trezorcrypto_HDNode_address(mp_obj_t self, mp_obj_t version)
|
||||
mp_obj_HDNode_t *o = MP_OBJ_TO_PTR(self);
|
||||
|
||||
uint32_t v = mp_obj_get_int_truncated(version);
|
||||
vstr_t vstr;
|
||||
vstr_init(&vstr, ADDRESS_MAXLEN);
|
||||
|
||||
hdnode_get_address(&o->hdnode, v, vstr.buf, vstr.alloc);
|
||||
vstr.len = strlen(vstr.buf);
|
||||
return mp_obj_new_str_from_vstr(&mp_type_str, &vstr);
|
||||
char address[ADDRESS_MAXLEN];
|
||||
hdnode_get_address(&o->hdnode, v, address, ADDRESS_MAXLEN);
|
||||
return mp_obj_new_str(address, strlen(address), false);
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_HDNode_address_obj, mod_trezorcrypto_HDNode_address);
|
||||
|
||||
@ -305,11 +297,9 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_HDNode_address_obj, mod_trezor
|
||||
STATIC mp_obj_t mod_trezorcrypto_HDNode_ethereum_pubkeyhash(mp_obj_t self) {
|
||||
mp_obj_HDNode_t *o = MP_OBJ_TO_PTR(self);
|
||||
|
||||
vstr_t vstr;
|
||||
vstr_init_len(&vstr, 20);
|
||||
|
||||
hdnode_get_ethereum_pubkeyhash(&o->hdnode, (uint8_t *)vstr.buf);
|
||||
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||||
uint8_t pkh[20];
|
||||
hdnode_get_ethereum_pubkeyhash(&o->hdnode, pkh);
|
||||
return mp_obj_new_bytes(pkh, sizeof(pkh));
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_HDNode_ethereum_pubkeyhash_obj, mod_trezorcrypto_HDNode_ethereum_pubkeyhash);
|
||||
|
||||
|
@ -22,7 +22,7 @@ STATIC mp_obj_t mod_trezorcrypto_bip39_find_word(mp_obj_t prefix)
|
||||
}
|
||||
for (const char * const *w = mnemonic_wordlist(); *w != 0; w++) {
|
||||
if (strncmp(*w, pfx.buf, pfx.len) == 0) {
|
||||
return mp_obj_new_str_of_type(&mp_type_str, (const byte *)*w, strlen(*w));
|
||||
return mp_obj_new_str(*w, strlen(*w), false);
|
||||
}
|
||||
}
|
||||
return mp_const_none;
|
||||
@ -66,7 +66,7 @@ STATIC mp_obj_t mod_trezorcrypto_bip39_generate(mp_obj_t strength) {
|
||||
mp_raise_ValueError("Invalid bit strength (only 128, 160, 192, 224 and 256 values are allowed)");
|
||||
}
|
||||
const char *mnemo = mnemonic_generate(bits);
|
||||
return mp_obj_new_str_of_type(&mp_type_str, (uint8_t *)mnemo, strlen(mnemo));
|
||||
return mp_obj_new_str(mnemo, strlen(mnemo), false);
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_bip39_generate_obj, mod_trezorcrypto_bip39_generate);
|
||||
|
||||
@ -81,7 +81,7 @@ STATIC mp_obj_t mod_trezorcrypto_bip39_from_data(mp_obj_t data) {
|
||||
mp_raise_ValueError("Invalid data length (only 16, 20, 24, 28 and 32 bytes are allowed)");
|
||||
}
|
||||
const char *mnemo = mnemonic_from_data(bin.buf, bin.len);
|
||||
return mp_obj_new_str_of_type(&mp_type_str, (uint8_t *)mnemo, strlen(mnemo));
|
||||
return mp_obj_new_str(mnemo, strlen(mnemo), false);
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_bip39_from_data_obj, mod_trezorcrypto_bip39_from_data);
|
||||
|
||||
@ -105,12 +105,11 @@ STATIC mp_obj_t mod_trezorcrypto_bip39_seed(mp_obj_t mnemonic, mp_obj_t passphra
|
||||
mp_buffer_info_t phrase;
|
||||
mp_get_buffer_raise(mnemonic, &mnemo, MP_BUFFER_READ);
|
||||
mp_get_buffer_raise(passphrase, &phrase, MP_BUFFER_READ);
|
||||
vstr_t vstr;
|
||||
vstr_init_len(&vstr, 64);
|
||||
uint8_t seed[64];
|
||||
const char *pmnemonic = mnemo.len > 0 ? mnemo.buf : "";
|
||||
const char *ppassphrase = phrase.len > 0 ? phrase.buf : "";
|
||||
mnemonic_to_seed(pmnemonic, ppassphrase, (uint8_t *)vstr.buf, NULL); // no callback for now
|
||||
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||||
mnemonic_to_seed(pmnemonic, ppassphrase, seed, NULL); // no callback for now
|
||||
return mp_obj_new_bytes(seed, sizeof(seed));
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_bip39_seed_obj, mod_trezorcrypto_bip39_seed);
|
||||
|
||||
|
@ -57,13 +57,12 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_Blake256_update_obj, mod_trezo
|
||||
/// '''
|
||||
STATIC mp_obj_t mod_trezorcrypto_Blake256_digest(mp_obj_t self) {
|
||||
mp_obj_Blake256_t *o = MP_OBJ_TO_PTR(self);
|
||||
vstr_t vstr;
|
||||
vstr_init_len(&vstr, BLAKE256_DIGEST_LENGTH);
|
||||
uint8_t hash[BLAKE256_DIGEST_LENGTH];
|
||||
BLAKE256_CTX ctx;
|
||||
memcpy(&ctx, &(o->ctx), sizeof(BLAKE256_CTX));
|
||||
blake256_Final(&ctx, (uint8_t *)vstr.buf);
|
||||
blake256_Final(&ctx, hash);
|
||||
memset(&ctx, 0, sizeof(BLAKE256_CTX));
|
||||
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||||
return mp_obj_new_bytes(hash, sizeof(hash));
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_Blake256_digest_obj, mod_trezorcrypto_Blake256_digest);
|
||||
|
||||
|
@ -64,13 +64,12 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_Blake2b_update_obj, mod_trezor
|
||||
/// '''
|
||||
STATIC mp_obj_t mod_trezorcrypto_Blake2b_digest(mp_obj_t self) {
|
||||
mp_obj_Blake2b_t *o = MP_OBJ_TO_PTR(self);
|
||||
vstr_t vstr;
|
||||
vstr_init_len(&vstr, BLAKE2B_DIGEST_LENGTH);
|
||||
uint8_t out[BLAKE2B_DIGEST_LENGTH];
|
||||
BLAKE2B_CTX ctx;
|
||||
memcpy(&ctx, &(o->ctx), sizeof(BLAKE2B_CTX));
|
||||
blake2b_Final(&ctx, (uint8_t *)vstr.buf, BLAKE2B_DIGEST_LENGTH);
|
||||
blake2b_Final(&ctx, out, BLAKE2B_DIGEST_LENGTH);
|
||||
memset(&ctx, 0, sizeof(BLAKE2B_CTX));
|
||||
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||||
return mp_obj_new_bytes(out, sizeof(out));
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_Blake2b_digest_obj, mod_trezorcrypto_Blake2b_digest);
|
||||
|
||||
|
@ -64,13 +64,12 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_Blake2s_update_obj, mod_trezor
|
||||
/// '''
|
||||
STATIC mp_obj_t mod_trezorcrypto_Blake2s_digest(mp_obj_t self) {
|
||||
mp_obj_Blake2s_t *o = MP_OBJ_TO_PTR(self);
|
||||
vstr_t vstr;
|
||||
vstr_init_len(&vstr, BLAKE2S_DIGEST_LENGTH);
|
||||
uint8_t out[BLAKE2S_DIGEST_LENGTH];
|
||||
BLAKE2S_CTX ctx;
|
||||
memcpy(&ctx, &(o->ctx), sizeof(BLAKE2S_CTX));
|
||||
blake2s_Final(&ctx, (uint8_t *)vstr.buf, BLAKE2S_DIGEST_LENGTH);
|
||||
blake2s_Final(&ctx, out, BLAKE2S_DIGEST_LENGTH);
|
||||
memset(&ctx, 0, sizeof(BLAKE2S_CTX));
|
||||
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||||
return mp_obj_new_bytes(out, sizeof(out));
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_Blake2s_digest_obj, mod_trezorcrypto_Blake2s_digest);
|
||||
|
||||
|
@ -51,11 +51,10 @@ STATIC mp_obj_t mod_trezorcrypto_ChaCha20Poly1305_encrypt(mp_obj_t self, mp_obj_
|
||||
mp_obj_ChaCha20Poly1305_t *o = MP_OBJ_TO_PTR(self);
|
||||
mp_buffer_info_t in;
|
||||
mp_get_buffer_raise(data, &in, MP_BUFFER_READ);
|
||||
vstr_t vstr;
|
||||
vstr_init_len(&vstr, in.len);
|
||||
chacha20poly1305_encrypt(&(o->ctx), in.buf, (uint8_t *)vstr.buf, in.len);
|
||||
uint8_t out[in.len];
|
||||
chacha20poly1305_encrypt(&(o->ctx), in.buf, out, in.len);
|
||||
o->plen += in.len;
|
||||
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||||
return mp_obj_new_bytes(out, sizeof(out));
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_ChaCha20Poly1305_encrypt_obj, mod_trezorcrypto_ChaCha20Poly1305_encrypt);
|
||||
|
||||
@ -67,11 +66,10 @@ STATIC mp_obj_t mod_trezorcrypto_ChaCha20Poly1305_decrypt(mp_obj_t self, mp_obj_
|
||||
mp_obj_ChaCha20Poly1305_t *o = MP_OBJ_TO_PTR(self);
|
||||
mp_buffer_info_t in;
|
||||
mp_get_buffer_raise(data, &in, MP_BUFFER_READ);
|
||||
vstr_t vstr;
|
||||
vstr_init_len(&vstr, in.len);
|
||||
chacha20poly1305_decrypt(&(o->ctx), in.buf, (uint8_t *)vstr.buf, in.len);
|
||||
uint8_t out[in.len];
|
||||
chacha20poly1305_decrypt(&(o->ctx), in.buf, out, in.len);
|
||||
o->plen += in.len;
|
||||
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||||
return mp_obj_new_bytes(out, sizeof(out));
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_ChaCha20Poly1305_decrypt_obj, mod_trezorcrypto_ChaCha20Poly1305_decrypt);
|
||||
|
||||
@ -97,10 +95,9 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_ChaCha20Poly1305_auth_obj, mod
|
||||
/// '''
|
||||
STATIC mp_obj_t mod_trezorcrypto_ChaCha20Poly1305_finish(mp_obj_t self) {
|
||||
mp_obj_ChaCha20Poly1305_t *o = MP_OBJ_TO_PTR(self);
|
||||
vstr_t vstr;
|
||||
vstr_init_len(&vstr, 16);
|
||||
rfc7539_finish(&(o->ctx), o->alen, o->plen, (uint8_t *)vstr.buf);
|
||||
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||||
uint8_t out[16];
|
||||
rfc7539_finish(&(o->ctx), o->alen, o->plen, out);
|
||||
return mp_obj_new_bytes(out, sizeof(out));
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_ChaCha20Poly1305_finish_obj, mod_trezorcrypto_ChaCha20Poly1305_finish);
|
||||
|
||||
|
@ -16,14 +16,13 @@
|
||||
/// Generate secret key.
|
||||
/// '''
|
||||
STATIC mp_obj_t mod_trezorcrypto_curve25519_generate_secret() {
|
||||
vstr_t vstr;
|
||||
vstr_init_len(&vstr, 32);
|
||||
random_buffer((uint8_t *)vstr.buf, 32);
|
||||
uint8_t out[32];
|
||||
random_buffer(out, 32);
|
||||
// taken from https://cr.yp.to/ecdh.html
|
||||
vstr.buf[0] &= 248;
|
||||
vstr.buf[31] &= 127;
|
||||
vstr.buf[31] |= 64;
|
||||
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||||
out[0] &= 248;
|
||||
out[31] &= 127;
|
||||
out[31] |= 64;
|
||||
return mp_obj_new_bytes(out, sizeof(out));
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_trezorcrypto_curve25519_generate_secret_obj, mod_trezorcrypto_curve25519_generate_secret);
|
||||
|
||||
@ -37,10 +36,9 @@ STATIC mp_obj_t mod_trezorcrypto_curve25519_publickey(mp_obj_t secret_key) {
|
||||
if (sk.len != 32) {
|
||||
mp_raise_ValueError("Invalid length of secret key");
|
||||
}
|
||||
vstr_t vstr;
|
||||
vstr_init_len(&vstr, 32);
|
||||
curve25519_scalarmult_basepoint((uint8_t *)vstr.buf, (const uint8_t *)sk.buf);
|
||||
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||||
uint8_t out[32];
|
||||
curve25519_scalarmult_basepoint(out, (const uint8_t *)sk.buf);
|
||||
return mp_obj_new_bytes(out, sizeof(out));
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_curve25519_publickey_obj, mod_trezorcrypto_curve25519_publickey);
|
||||
|
||||
@ -59,10 +57,9 @@ STATIC mp_obj_t mod_trezorcrypto_curve25519_multiply(mp_obj_t secret_key, mp_obj
|
||||
if (pk.len != 32) {
|
||||
mp_raise_ValueError("Invalid length of public key");
|
||||
}
|
||||
vstr_t vstr;
|
||||
vstr_init_len(&vstr, 32);
|
||||
curve25519_scalarmult((uint8_t *)vstr.buf, (const uint8_t *)sk.buf, (const uint8_t *)pk.buf);
|
||||
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||||
uint8_t out[32];
|
||||
curve25519_scalarmult(out, (const uint8_t *)sk.buf, (const uint8_t *)pk.buf);
|
||||
return mp_obj_new_bytes(out, sizeof(out));
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_curve25519_multiply_obj, mod_trezorcrypto_curve25519_multiply);
|
||||
|
||||
|
@ -16,14 +16,13 @@
|
||||
/// Generate secret key.
|
||||
/// '''
|
||||
STATIC mp_obj_t mod_trezorcrypto_ed25519_generate_secret() {
|
||||
vstr_t vstr;
|
||||
vstr_init_len(&vstr, 32);
|
||||
random_buffer((uint8_t *)vstr.buf, 32);
|
||||
uint8_t out[32];
|
||||
random_buffer(out, 32);
|
||||
// taken from https://cr.yp.to/ecdh.html
|
||||
vstr.buf[0] &= 248;
|
||||
vstr.buf[31] &= 127;
|
||||
vstr.buf[31] |= 64;
|
||||
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||||
out[0] &= 248;
|
||||
out[31] &= 127;
|
||||
out[31] |= 64;
|
||||
return mp_obj_new_bytes(out, sizeof(out));
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_trezorcrypto_ed25519_generate_secret_obj, mod_trezorcrypto_ed25519_generate_secret);
|
||||
|
||||
@ -37,10 +36,9 @@ STATIC mp_obj_t mod_trezorcrypto_ed25519_publickey(mp_obj_t secret_key) {
|
||||
if (sk.len != 32) {
|
||||
mp_raise_ValueError("Invalid length of secret key");
|
||||
}
|
||||
vstr_t vstr;
|
||||
vstr_init_len(&vstr, 32);
|
||||
ed25519_publickey(*(const ed25519_secret_key *)sk.buf, *(ed25519_public_key *)vstr.buf);
|
||||
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||||
uint8_t out[32];
|
||||
ed25519_publickey(*(const ed25519_secret_key *)sk.buf, *(ed25519_public_key *)out);
|
||||
return mp_obj_new_bytes(out, sizeof(out));
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_ed25519_publickey_obj, mod_trezorcrypto_ed25519_publickey);
|
||||
|
||||
@ -60,10 +58,9 @@ STATIC mp_obj_t mod_trezorcrypto_ed25519_sign(mp_obj_t secret_key, mp_obj_t mess
|
||||
}
|
||||
ed25519_public_key pk;
|
||||
ed25519_publickey(*(const ed25519_secret_key *)sk.buf, pk);
|
||||
vstr_t vstr;
|
||||
vstr_init_len(&vstr, 64);
|
||||
ed25519_sign(msg.buf, msg.len, *(const ed25519_secret_key *)sk.buf, pk, *(ed25519_signature *)vstr.buf);
|
||||
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||||
uint8_t out[64];
|
||||
ed25519_sign(msg.buf, msg.len, *(const ed25519_secret_key *)sk.buf, pk, *(ed25519_signature *)out);
|
||||
return mp_obj_new_bytes(out, sizeof(out));
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_ed25519_sign_obj, mod_trezorcrypto_ed25519_sign);
|
||||
|
||||
@ -110,12 +107,11 @@ STATIC mp_obj_t mod_trezorcrypto_ed25519_cosi_combine_publickeys(mp_obj_t public
|
||||
}
|
||||
memcpy(pks[i], buf.buf, buf.len);
|
||||
}
|
||||
vstr_t vstr;
|
||||
vstr_init_len(&vstr, 32);
|
||||
if (0 != ed25519_cosi_combine_publickeys(*(ed25519_public_key *)vstr.buf, pks, pklen)) {
|
||||
uint8_t out[32];
|
||||
if (0 != ed25519_cosi_combine_publickeys(*(ed25519_public_key *)out, pks, pklen)) {
|
||||
mp_raise_ValueError("Error combining public keys");
|
||||
}
|
||||
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||||
return mp_obj_new_bytes(out, sizeof(out));
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_ed25519_cosi_combine_publickeys_obj, mod_trezorcrypto_ed25519_cosi_combine_publickeys);
|
||||
|
||||
@ -144,10 +140,9 @@ STATIC mp_obj_t mod_trezorcrypto_ed25519_cosi_combine_signatures(mp_obj_t R, mp_
|
||||
}
|
||||
memcpy(sigs[i], buf.buf, buf.len);
|
||||
}
|
||||
vstr_t vstr;
|
||||
vstr_init_len(&vstr, 64);
|
||||
ed25519_cosi_combine_signatures(*(ed25519_signature *)vstr.buf, *(const ed25519_public_key *)sigR.buf, sigs, siglen);
|
||||
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||||
uint8_t out[64];
|
||||
ed25519_cosi_combine_signatures(*(ed25519_signature *)out, *(const ed25519_public_key *)sigR.buf, sigs, siglen);
|
||||
return mp_obj_new_bytes(out, sizeof(out));
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_ed25519_cosi_combine_signatures_obj, mod_trezorcrypto_ed25519_cosi_combine_signatures);
|
||||
|
||||
@ -174,10 +169,9 @@ STATIC mp_obj_t mod_trezorcrypto_ed25519_cosi_sign(size_t n_args, const mp_obj_t
|
||||
if (pk.len != 32) {
|
||||
mp_raise_ValueError("Invalid length of aggregated public key");
|
||||
}
|
||||
vstr_t vstr;
|
||||
vstr_init_len(&vstr, 32);
|
||||
ed25519_cosi_sign(msg.buf, msg.len, *(const ed25519_secret_key *)sk.buf, *(const ed25519_secret_key *)nonce.buf, *(const ed25519_public_key *)sigR.buf, *(const ed25519_secret_key *)pk.buf, *(ed25519_cosi_signature *)vstr.buf);
|
||||
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||||
uint8_t out[32];
|
||||
ed25519_cosi_sign(msg.buf, msg.len, *(const ed25519_secret_key *)sk.buf, *(const ed25519_secret_key *)nonce.buf, *(const ed25519_public_key *)sigR.buf, *(const ed25519_secret_key *)pk.buf, *(ed25519_cosi_signature *)out);
|
||||
return mp_obj_new_bytes(out, sizeof(out));
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_trezorcrypto_ed25519_cosi_sign_obj, 5, 5, mod_trezorcrypto_ed25519_cosi_sign);
|
||||
|
||||
|
@ -15,16 +15,15 @@
|
||||
/// Generate secret key.
|
||||
/// '''
|
||||
STATIC mp_obj_t mod_trezorcrypto_nist256p1_generate_secret() {
|
||||
vstr_t vstr;
|
||||
vstr_init_len(&vstr, 32);
|
||||
uint8_t out[32];
|
||||
for (;;) {
|
||||
random_buffer((uint8_t *)vstr.buf, 32);
|
||||
random_buffer(out, 32);
|
||||
// check whether secret > 0 && secret < curve_order
|
||||
if (0 == memcmp(vstr.buf, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 32)) continue;
|
||||
if (0 <= memcmp(vstr.buf, "\xFF\xFF\xFF\xFF\x00\x00\x00\x00\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xBC\xE6\xFA\xAD\xA7\x17\x9E\x84\xF3\xB9\xCA\xC2\xFC\x63\x25\x51", 32)) continue;
|
||||
if (0 == memcmp(out, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 32)) continue;
|
||||
if (0 <= memcmp(out, "\xFF\xFF\xFF\xFF\x00\x00\x00\x00\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xBC\xE6\xFA\xAD\xA7\x17\x9E\x84\xF3\xB9\xCA\xC2\xFC\x63\x25\x51", 32)) continue;
|
||||
break;
|
||||
}
|
||||
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||||
return mp_obj_new_bytes(out, sizeof(out));
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_trezorcrypto_nist256p1_generate_secret_obj, mod_trezorcrypto_nist256p1_generate_secret);
|
||||
|
||||
@ -39,15 +38,15 @@ STATIC mp_obj_t mod_trezorcrypto_nist256p1_publickey(size_t n_args, const mp_obj
|
||||
mp_raise_ValueError("Invalid length of secret key");
|
||||
}
|
||||
bool compressed = n_args < 2 || args[1] == mp_const_true;
|
||||
vstr_t vstr;
|
||||
if (compressed) {
|
||||
vstr_init_len(&vstr, 33);
|
||||
ecdsa_get_public_key33(&nist256p1, (const uint8_t *)sk.buf, (uint8_t *)vstr.buf);
|
||||
uint8_t out[33];
|
||||
ecdsa_get_public_key33(&nist256p1, (const uint8_t *)sk.buf, out);
|
||||
return mp_obj_new_bytes(out, sizeof(out));
|
||||
} else {
|
||||
vstr_init_len(&vstr, 65);
|
||||
ecdsa_get_public_key65(&nist256p1, (const uint8_t *)sk.buf, (uint8_t *)vstr.buf);
|
||||
uint8_t out[65];
|
||||
ecdsa_get_public_key65(&nist256p1, (const uint8_t *)sk.buf, out);
|
||||
return mp_obj_new_bytes(out, sizeof(out));
|
||||
}
|
||||
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_trezorcrypto_nist256p1_publickey_obj, 1, 2, mod_trezorcrypto_nist256p1_publickey);
|
||||
|
||||
@ -66,14 +65,12 @@ STATIC mp_obj_t mod_trezorcrypto_nist256p1_sign(size_t n_args, const mp_obj_t *a
|
||||
if (dig.len != 32) {
|
||||
mp_raise_ValueError("Invalid length of digest");
|
||||
}
|
||||
vstr_t vstr;
|
||||
vstr_init_len(&vstr, 65);
|
||||
uint8_t pby;
|
||||
if (0 != ecdsa_sign_digest(&nist256p1, (const uint8_t *)sk.buf, (const uint8_t *)dig.buf, (uint8_t *)vstr.buf + 1, &pby, NULL)) {
|
||||
uint8_t out[65], pby;
|
||||
if (0 != ecdsa_sign_digest(&nist256p1, (const uint8_t *)sk.buf, (const uint8_t *)dig.buf, out + 1, &pby, NULL)) {
|
||||
mp_raise_ValueError("Signing failed");
|
||||
}
|
||||
vstr.buf[0] = 27 + pby + compressed * 4;
|
||||
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||||
out[0] = 27 + pby + compressed * 4;
|
||||
return mp_obj_new_bytes(out, sizeof(out));
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_trezorcrypto_nist256p1_sign_obj, 2, 3, mod_trezorcrypto_nist256p1_sign);
|
||||
|
||||
@ -122,14 +119,13 @@ STATIC mp_obj_t mod_trezorcrypto_nist256p1_verify_recover(mp_obj_t signature, mp
|
||||
}
|
||||
bool compressed = (recid >= 4);
|
||||
recid &= 3;
|
||||
vstr_t vstr;
|
||||
vstr_init_len(&vstr, 65);
|
||||
if (0 == ecdsa_verify_digest_recover(&nist256p1, (uint8_t *)vstr.buf, (const uint8_t *)sig.buf + 1, (const uint8_t *)dig.buf, recid)) {
|
||||
uint8_t out[65];
|
||||
if (0 == ecdsa_verify_digest_recover(&nist256p1, out, (const uint8_t *)sig.buf + 1, (const uint8_t *)dig.buf, recid)) {
|
||||
if (compressed) {
|
||||
vstr.buf[0] = 0x02 | (vstr.buf[64] & 1);
|
||||
vstr.len = 33;
|
||||
out[0] = 0x02 | (out[64] & 1);
|
||||
return mp_obj_new_bytes(out, 33);
|
||||
}
|
||||
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||||
return mp_obj_new_bytes(out, sizeof(out));
|
||||
} else {
|
||||
return mp_const_none;
|
||||
}
|
||||
@ -151,12 +147,11 @@ STATIC mp_obj_t mod_trezorcrypto_nist256p1_multiply(mp_obj_t secret_key, mp_obj_
|
||||
if (pk.len != 33 && pk.len != 65) {
|
||||
mp_raise_ValueError("Invalid length of public key");
|
||||
}
|
||||
vstr_t vstr;
|
||||
vstr_init_len(&vstr, 65);
|
||||
if (0 != ecdh_multiply(&nist256p1, (const uint8_t *)sk.buf, (const uint8_t *)pk.buf, (uint8_t *)vstr.buf)) {
|
||||
uint8_t out[65];
|
||||
if (0 != ecdh_multiply(&nist256p1, (const uint8_t *)sk.buf, (const uint8_t *)pk.buf, out)) {
|
||||
mp_raise_ValueError("Multiply failed");
|
||||
}
|
||||
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||||
return mp_obj_new_bytes(out, sizeof(out));
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_nist256p1_multiply_obj, mod_trezorcrypto_nist256p1_multiply);
|
||||
|
||||
|
@ -89,22 +89,23 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_Pbkdf2_update_obj, mod_trezorc
|
||||
/// '''
|
||||
STATIC mp_obj_t mod_trezorcrypto_Pbkdf2_key(mp_obj_t self) {
|
||||
mp_obj_Pbkdf2_t *o = MP_OBJ_TO_PTR(self);
|
||||
vstr_t vstr;
|
||||
if (o->prf == 256) {
|
||||
PBKDF2_HMAC_SHA256_CTX ctx;
|
||||
memcpy(&ctx, &(o->ctx256), sizeof(PBKDF2_HMAC_SHA256_CTX));
|
||||
vstr_init_len(&vstr, SHA256_DIGEST_LENGTH);
|
||||
pbkdf2_hmac_sha256_Final(&ctx, (uint8_t *)vstr.buf);
|
||||
uint8_t out[SHA256_DIGEST_LENGTH];
|
||||
pbkdf2_hmac_sha256_Final(&ctx, out);
|
||||
memset(&ctx, 0, sizeof(PBKDF2_HMAC_SHA256_CTX));
|
||||
return mp_obj_new_bytes(out, sizeof(out));
|
||||
}
|
||||
if (o->prf == 512) {
|
||||
PBKDF2_HMAC_SHA512_CTX ctx;
|
||||
memcpy(&ctx, &(o->ctx512), sizeof(PBKDF2_HMAC_SHA512_CTX));
|
||||
vstr_init_len(&vstr, SHA512_DIGEST_LENGTH);
|
||||
pbkdf2_hmac_sha512_Final(&ctx, (uint8_t *)vstr.buf);
|
||||
uint8_t out[SHA512_DIGEST_LENGTH];
|
||||
pbkdf2_hmac_sha512_Final(&ctx, out);
|
||||
memset(&ctx, 0, sizeof(PBKDF2_HMAC_SHA512_CTX));
|
||||
return mp_obj_new_bytes(out, sizeof(out));
|
||||
}
|
||||
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||||
return mp_const_none;
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_Pbkdf2_key_obj, mod_trezorcrypto_Pbkdf2_key);
|
||||
|
||||
|
@ -28,13 +28,12 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_random_uniform_obj, mod_trezor
|
||||
/// '''
|
||||
STATIC mp_obj_t mod_trezorcrypto_random_bytes(mp_obj_t len) {
|
||||
uint32_t l = mp_obj_get_int(len);
|
||||
if (l > 8192) {
|
||||
mp_raise_ValueError("Maximum requested size is 8192");
|
||||
if (l > 1024) {
|
||||
mp_raise_ValueError("Maximum requested size is 1024");
|
||||
}
|
||||
vstr_t vstr;
|
||||
vstr_init_len(&vstr, l);
|
||||
random_buffer((uint8_t *)vstr.buf, l);
|
||||
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||||
uint8_t out[l];
|
||||
random_buffer(out, l);
|
||||
return mp_obj_new_bytes(out, sizeof(out));
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_random_bytes_obj, mod_trezorcrypto_random_bytes);
|
||||
|
||||
|
@ -45,10 +45,9 @@ STATIC mp_obj_t mod_trezorcrypto_Rfc6979_make_new(const mp_obj_type_t *type, siz
|
||||
/// '''
|
||||
STATIC mp_obj_t mod_trezorcrypto_Rfc6979_next(mp_obj_t self) {
|
||||
mp_obj_Rfc6979_t *o = MP_OBJ_TO_PTR(self);
|
||||
vstr_t vstr;
|
||||
vstr_init_len(&vstr, 32);
|
||||
generate_rfc6979((uint8_t *)vstr.buf, &(o->rng));
|
||||
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||||
uint8_t out[32];
|
||||
generate_rfc6979(out, &(o->rng));
|
||||
return mp_obj_new_bytes(out, sizeof(out));
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_Rfc6979_next_obj, mod_trezorcrypto_Rfc6979_next);
|
||||
|
||||
|
@ -57,13 +57,12 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_Ripemd160_update_obj, mod_trez
|
||||
/// '''
|
||||
STATIC mp_obj_t mod_trezorcrypto_Ripemd160_digest(mp_obj_t self) {
|
||||
mp_obj_Ripemd160_t *o = MP_OBJ_TO_PTR(self);
|
||||
vstr_t vstr;
|
||||
vstr_init_len(&vstr, RIPEMD160_DIGEST_LENGTH);
|
||||
uint8_t out[RIPEMD160_DIGEST_LENGTH];
|
||||
RIPEMD160_CTX ctx;
|
||||
memcpy(&ctx, &(o->ctx), sizeof(RIPEMD160_CTX));
|
||||
ripemd160_Final(&ctx, (uint8_t *)vstr.buf);
|
||||
ripemd160_Final(&ctx, out);
|
||||
memset(&ctx, 0, sizeof(RIPEMD160_CTX));
|
||||
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||||
return mp_obj_new_bytes(out, sizeof(out));
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_Ripemd160_digest_obj, mod_trezorcrypto_Ripemd160_digest);
|
||||
|
||||
|
@ -15,16 +15,15 @@
|
||||
/// Generate secret key.
|
||||
/// '''
|
||||
STATIC mp_obj_t mod_trezorcrypto_secp256k1_generate_secret() {
|
||||
vstr_t vstr;
|
||||
vstr_init_len(&vstr, 32);
|
||||
uint8_t out[32];
|
||||
for (;;) {
|
||||
random_buffer((uint8_t *)vstr.buf, 32);
|
||||
random_buffer(out, 32);
|
||||
// check whether secret > 0 && secret < curve_order
|
||||
if (0 == memcmp(vstr.buf, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 32)) continue;
|
||||
if (0 <= memcmp(vstr.buf, "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFE\xBA\xAE\xDC\xE6\xAF\x48\xA0\x3B\xBF\xD2\x5E\x8C\xD0\x36\x41\x41", 32)) continue;
|
||||
if (0 == memcmp(out, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 32)) continue;
|
||||
if (0 <= memcmp(out, "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFE\xBA\xAE\xDC\xE6\xAF\x48\xA0\x3B\xBF\xD2\x5E\x8C\xD0\x36\x41\x41", 32)) continue;
|
||||
break;
|
||||
}
|
||||
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||||
return mp_obj_new_bytes(out, sizeof(out));
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_trezorcrypto_secp256k1_generate_secret_obj, mod_trezorcrypto_secp256k1_generate_secret);
|
||||
|
||||
@ -39,15 +38,15 @@ STATIC mp_obj_t mod_trezorcrypto_secp256k1_publickey(size_t n_args, const mp_obj
|
||||
mp_raise_ValueError("Invalid length of secret key");
|
||||
}
|
||||
bool compressed = n_args < 2 || args[1] == mp_const_true;
|
||||
vstr_t vstr;
|
||||
if (compressed) {
|
||||
vstr_init_len(&vstr, 33);
|
||||
ecdsa_get_public_key33(&secp256k1, (const uint8_t *)sk.buf, (uint8_t *)vstr.buf);
|
||||
uint8_t out[33];
|
||||
ecdsa_get_public_key33(&secp256k1, (const uint8_t *)sk.buf, out);
|
||||
return mp_obj_new_bytes(out, sizeof(out));
|
||||
} else {
|
||||
vstr_init_len(&vstr, 65);
|
||||
ecdsa_get_public_key65(&secp256k1, (const uint8_t *)sk.buf, (uint8_t *)vstr.buf);
|
||||
uint8_t out[65];
|
||||
ecdsa_get_public_key65(&secp256k1, (const uint8_t *)sk.buf, out);
|
||||
return mp_obj_new_bytes(out, sizeof(out));
|
||||
}
|
||||
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_trezorcrypto_secp256k1_publickey_obj, 1, 2, mod_trezorcrypto_secp256k1_publickey);
|
||||
|
||||
@ -66,14 +65,12 @@ STATIC mp_obj_t mod_trezorcrypto_secp256k1_sign(size_t n_args, const mp_obj_t *a
|
||||
if (dig.len != 32) {
|
||||
mp_raise_ValueError("Invalid length of digest");
|
||||
}
|
||||
vstr_t vstr;
|
||||
vstr_init_len(&vstr, 65);
|
||||
uint8_t pby;
|
||||
if (0 != ecdsa_sign_digest(&secp256k1, (const uint8_t *)sk.buf, (const uint8_t *)dig.buf, (uint8_t *)vstr.buf + 1, &pby, NULL)) {
|
||||
uint8_t out[65], pby;
|
||||
if (0 != ecdsa_sign_digest(&secp256k1, (const uint8_t *)sk.buf, (const uint8_t *)dig.buf, out + 1, &pby, NULL)) {
|
||||
mp_raise_ValueError("Signing failed");
|
||||
}
|
||||
vstr.buf[0] = 27 + pby + compressed * 4;
|
||||
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||||
out[0] = 27 + pby + compressed * 4;
|
||||
return mp_obj_new_bytes(out, sizeof(out));
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_trezorcrypto_secp256k1_sign_obj, 2, 3, mod_trezorcrypto_secp256k1_sign);
|
||||
|
||||
@ -122,14 +119,13 @@ STATIC mp_obj_t mod_trezorcrypto_secp256k1_verify_recover(mp_obj_t signature, mp
|
||||
}
|
||||
bool compressed = (recid >= 4);
|
||||
recid &= 3;
|
||||
vstr_t vstr;
|
||||
vstr_init_len(&vstr, 65);
|
||||
if (0 == ecdsa_verify_digest_recover(&secp256k1, (uint8_t *)vstr.buf, (const uint8_t *)sig.buf + 1, (const uint8_t *)dig.buf, recid)) {
|
||||
uint8_t out[65];
|
||||
if (0 == ecdsa_verify_digest_recover(&secp256k1, out, (const uint8_t *)sig.buf + 1, (const uint8_t *)dig.buf, recid)) {
|
||||
if (compressed) {
|
||||
vstr.buf[0] = 0x02 | (vstr.buf[64] & 1);
|
||||
vstr.len = 33;
|
||||
out[0] = 0x02 | (out[64] & 1);
|
||||
return mp_obj_new_bytes(out, 33);
|
||||
}
|
||||
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||||
return mp_obj_new_bytes(out, sizeof(out));
|
||||
} else {
|
||||
return mp_const_none;
|
||||
}
|
||||
@ -151,12 +147,11 @@ STATIC mp_obj_t mod_trezorcrypto_secp256k1_multiply(mp_obj_t secret_key, mp_obj_
|
||||
if (pk.len != 33 && pk.len != 65) {
|
||||
mp_raise_ValueError("Invalid length of public key");
|
||||
}
|
||||
vstr_t vstr;
|
||||
vstr_init_len(&vstr, 65);
|
||||
if (0 != ecdh_multiply(&secp256k1, (const uint8_t *)sk.buf, (const uint8_t *)pk.buf, (uint8_t *)vstr.buf)) {
|
||||
uint8_t out[65];
|
||||
if (0 != ecdh_multiply(&secp256k1, (const uint8_t *)sk.buf, (const uint8_t *)pk.buf, out)) {
|
||||
mp_raise_ValueError("Multiply failed");
|
||||
}
|
||||
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||||
return mp_obj_new_bytes(out, sizeof(out));
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_secp256k1_multiply_obj, mod_trezorcrypto_secp256k1_multiply);
|
||||
|
||||
|
@ -57,13 +57,12 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_Sha1_update_obj, mod_trezorcry
|
||||
/// '''
|
||||
STATIC mp_obj_t mod_trezorcrypto_Sha1_digest(mp_obj_t self) {
|
||||
mp_obj_Sha1_t *o = MP_OBJ_TO_PTR(self);
|
||||
vstr_t vstr;
|
||||
vstr_init_len(&vstr, SHA1_DIGEST_LENGTH);
|
||||
uint8_t out[SHA1_DIGEST_LENGTH];
|
||||
SHA1_CTX ctx;
|
||||
memcpy(&ctx, &(o->ctx), sizeof(SHA1_CTX));
|
||||
sha1_Final(&ctx, (uint8_t *)vstr.buf);
|
||||
sha1_Final(&ctx, out);
|
||||
memset(&ctx, 0, sizeof(SHA1_CTX));
|
||||
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||||
return mp_obj_new_bytes(out, sizeof(out));
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_Sha1_digest_obj, mod_trezorcrypto_Sha1_digest);
|
||||
|
||||
|
@ -57,13 +57,12 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_Sha256_update_obj, mod_trezorc
|
||||
/// '''
|
||||
STATIC mp_obj_t mod_trezorcrypto_Sha256_digest(mp_obj_t self) {
|
||||
mp_obj_Sha256_t *o = MP_OBJ_TO_PTR(self);
|
||||
vstr_t vstr;
|
||||
vstr_init_len(&vstr, SHA256_DIGEST_LENGTH);
|
||||
uint8_t out[SHA256_DIGEST_LENGTH];
|
||||
SHA256_CTX ctx;
|
||||
memcpy(&ctx, &(o->ctx), sizeof(SHA256_CTX));
|
||||
sha256_Final(&ctx, (uint8_t *)vstr.buf);
|
||||
sha256_Final(&ctx, out);
|
||||
memset(&ctx, 0, sizeof(SHA256_CTX));
|
||||
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||||
return mp_obj_new_bytes(out, sizeof(out));
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_Sha256_digest_obj, mod_trezorcrypto_Sha256_digest);
|
||||
|
||||
|
@ -57,17 +57,16 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_Sha3_256_update_obj, mod_trezo
|
||||
/// '''
|
||||
STATIC mp_obj_t mod_trezorcrypto_Sha3_256_digest(size_t n_args, const mp_obj_t *args) {
|
||||
mp_obj_Sha3_256_t *o = MP_OBJ_TO_PTR(args[0]);
|
||||
vstr_t vstr;
|
||||
vstr_init_len(&vstr, SHA3_256_DIGEST_LENGTH);
|
||||
uint8_t out[SHA3_256_DIGEST_LENGTH];
|
||||
SHA3_CTX ctx;
|
||||
memcpy(&ctx, &(o->ctx), sizeof(SHA3_CTX));
|
||||
if (n_args >= 1 && args[1] == mp_const_true) {
|
||||
keccak_Final(&ctx, (uint8_t *)vstr.buf);
|
||||
keccak_Final(&ctx, out);
|
||||
} else {
|
||||
sha3_Final(&ctx, (uint8_t *)vstr.buf);
|
||||
sha3_Final(&ctx, out);
|
||||
}
|
||||
memset(&ctx, 0, sizeof(SHA3_CTX));
|
||||
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||||
return mp_obj_new_bytes(out, sizeof(out));
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_trezorcrypto_Sha3_256_digest_obj, 1, 2, mod_trezorcrypto_Sha3_256_digest);
|
||||
|
||||
|
@ -57,17 +57,16 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_Sha3_512_update_obj, mod_trezo
|
||||
/// '''
|
||||
STATIC mp_obj_t mod_trezorcrypto_Sha3_512_digest(size_t n_args, const mp_obj_t *args) {
|
||||
mp_obj_Sha3_512_t *o = MP_OBJ_TO_PTR(args[0]);
|
||||
vstr_t vstr;
|
||||
vstr_init_len(&vstr, SHA3_512_DIGEST_LENGTH);
|
||||
uint8_t out[SHA3_512_DIGEST_LENGTH];
|
||||
SHA3_CTX ctx;
|
||||
memcpy(&ctx, &(o->ctx), sizeof(SHA3_CTX));
|
||||
if (n_args >= 1 && args[1] == mp_const_true) {
|
||||
keccak_Final(&ctx, (uint8_t *)vstr.buf);
|
||||
keccak_Final(&ctx, out);
|
||||
} else {
|
||||
sha3_Final(&ctx, (uint8_t *)vstr.buf);
|
||||
sha3_Final(&ctx, out);
|
||||
}
|
||||
memset(&ctx, 0, sizeof(SHA3_CTX));
|
||||
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||||
return mp_obj_new_bytes(out, sizeof(out));
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_trezorcrypto_Sha3_512_digest_obj, 1, 2, mod_trezorcrypto_Sha3_512_digest);
|
||||
|
||||
|
@ -56,13 +56,12 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_Sha512_update_obj, mod_trezorc
|
||||
/// '''
|
||||
STATIC mp_obj_t mod_trezorcrypto_Sha512_digest(mp_obj_t self) {
|
||||
mp_obj_Sha512_t *o = MP_OBJ_TO_PTR(self);
|
||||
vstr_t vstr;
|
||||
vstr_init_len(&vstr, SHA512_DIGEST_LENGTH);
|
||||
uint8_t out[SHA512_DIGEST_LENGTH];
|
||||
SHA512_CTX ctx;
|
||||
memcpy(&ctx, &(o->ctx), sizeof(SHA512_CTX));
|
||||
sha512_Final(&ctx, (uint8_t *)vstr.buf);
|
||||
sha512_Final(&ctx, out);
|
||||
memset(&ctx, 0, sizeof(SHA512_CTX));
|
||||
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||||
return mp_obj_new_bytes(out, sizeof(out));
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorcrypto_Sha512_digest_obj, mod_trezorcrypto_Sha512_digest);
|
||||
|
||||
|
@ -63,10 +63,10 @@ STATIC mp_obj_t mod_trezorio_poll(mp_obj_t ifaces, mp_obj_t list_ref, mp_obj_t t
|
||||
if (mode == POLL_READ) {
|
||||
if (sectrue == usb_hid_can_read(iface)) {
|
||||
uint8_t buf[64];
|
||||
int l = usb_hid_read(iface, buf, sizeof(buf));
|
||||
if (l > 0) {
|
||||
int len = usb_hid_read(iface, buf, sizeof(buf));
|
||||
if (len > 0) {
|
||||
ret->items[0] = MP_OBJ_NEW_SMALL_INT(i);
|
||||
ret->items[1] = mp_obj_new_str_of_type(&mp_type_bytes, buf, l);
|
||||
ret->items[1] = mp_obj_new_bytes(buf, len);
|
||||
return mp_const_true;
|
||||
}
|
||||
}
|
||||
|
@ -26,12 +26,13 @@ class TestCryptoRandom(unittest.TestCase):
|
||||
|
||||
def test_bytes_uniform(self):
|
||||
for _ in range(100):
|
||||
b = random.bytes(8000)
|
||||
c = {}
|
||||
for h in '0123456789abcdef':
|
||||
c[h] = 0
|
||||
for h in hexlify(b):
|
||||
c[chr(h)] += 1
|
||||
for _ in range(8):
|
||||
b = random.bytes(1000)
|
||||
for h in hexlify(b):
|
||||
c[chr(h)] += 1
|
||||
for h in '0123456789abcdef':
|
||||
self.assertAlmostEqual(c[h], 1000, delta=150)
|
||||
|
||||
|
@ -1,5 +1,4 @@
|
||||
import sys
|
||||
import uio
|
||||
|
||||
__all__ = [
|
||||
'run_tests',
|
||||
@ -62,9 +61,7 @@ def report_test(name, test, result):
|
||||
|
||||
|
||||
def report_exception(exc):
|
||||
sio = uio.StringIO()
|
||||
sys.print_exception(exc, sio)
|
||||
print(sio.getvalue())
|
||||
sys.print_exception(exc)
|
||||
|
||||
|
||||
def report_total(total, ok, failed):
|
||||
|
Loading…
Reference in New Issue
Block a user