Exemplo n.º 1
0
void fsm_msgEncryptMessage(EncryptMessage *msg)
{
	if (!msg->has_pubkey) {
		fsm_sendFailure(FailureType_Failure_SyntaxError, "No public key provided");
		return;
	}
	if (!msg->has_message) {
		fsm_sendFailure(FailureType_Failure_SyntaxError, "No message provided");
		return;
	}
	curve_point pubkey;
	if (msg->pubkey.size != 33 || ecdsa_read_pubkey(&secp256k1, msg->pubkey.bytes, &pubkey) == 0) {
		fsm_sendFailure(FailureType_Failure_SyntaxError, "Invalid public key provided");
		return;
	}
	bool display_only = msg->has_display_only && msg->display_only;
	bool signing = msg->address_n_count > 0;
	RESP_INIT(EncryptedMessage);
	const CoinType *coin = 0;
	const HDNode *node = 0;
	uint8_t address_raw[21];
	if (signing) {
		coin = coinByName(msg->coin_name);
		if (!coin) {
			fsm_sendFailure(FailureType_Failure_Other, "Invalid coin name");
			return;
		}
		if (!protectPin(true)) {
			layoutHome();
			return;
		}
		node = fsm_getDerivedNode(msg->address_n, msg->address_n_count);
		if (!node) return;
		uint8_t public_key[33];
		ecdsa_get_public_key33(&secp256k1, node->private_key, public_key);
		ecdsa_get_address_raw(public_key, coin->address_type, address_raw);
	}
	layoutEncryptMessage(msg->message.bytes, msg->message.size, signing);
	if (!protectButton(ButtonRequestType_ButtonRequest_ProtectCall, false)) {
		fsm_sendFailure(FailureType_Failure_ActionCancelled, "Encrypt message cancelled");
		layoutHome();
		return;
	}
	layoutProgressSwipe("Encrypting", 0);
	if (cryptoMessageEncrypt(&pubkey, msg->message.bytes, msg->message.size, display_only, resp->nonce.bytes, &(resp->nonce.size), resp->message.bytes, &(resp->message.size), resp->hmac.bytes, &(resp->hmac.size), signing ? node->private_key : 0, signing ? address_raw : 0) != 0) {
		fsm_sendFailure(FailureType_Failure_ActionCancelled, "Error encrypting message");
		layoutHome();
		return;
	}
	resp->has_nonce = true;
	resp->has_message = true;
	resp->has_hmac = true;
	msg_write(MessageType_MessageType_EncryptedMessage, resp);
	layoutHome();
}
Exemplo n.º 2
0
void fsm_msgEncryptMessage(EncryptMessage *msg)
{

    if (!storage_is_initialized()) 
    {
        fsm_sendFailure(FailureType_Failure_NotInitialized, "Device not initialized");
        return;
    }

    if(!msg->has_pubkey)
    {
        fsm_sendFailure(FailureType_Failure_SyntaxError, "No public key provided");
        return;
    }

    if(!msg->has_message)
    {
        fsm_sendFailure(FailureType_Failure_SyntaxError, "No message provided");
        return;
    }

    curve_point pubkey;

    if(msg->pubkey.size != 33 ||
            ecdsa_read_pubkey(&secp256k1, msg->pubkey.bytes, &pubkey) == 0)
    {
        fsm_sendFailure(FailureType_Failure_SyntaxError, "Invalid public key provided");
        return;
    }

    bool display_only = msg->has_display_only && msg->display_only;
    bool signing = msg->address_n_count > 0;
    RESP_INIT(EncryptedMessage);
    const CoinType *coin = 0;
    const HDNode *node = 0;
    uint8_t address_raw[21];

    if(signing)
    {
        coin = coinByName(msg->coin_name);

        if(!coin)
        {
            fsm_sendFailure(FailureType_Failure_Other, "Invalid coin name");
            return;
        }

        if(!pin_protect_cached())
        {
            go_home();
            return;
        }

        node = fsm_getDerivedNode(msg->address_n, msg->address_n_count);

        if(!node) { return; }

        uint8_t public_key[33];
        ecdsa_get_public_key33(&secp256k1, node->private_key, public_key);
        ecdsa_get_address_raw(public_key, coin->address_type, address_raw);
    }

    if(!confirm_encrypt_msg((char *)msg->message.bytes, signing))
    {
        fsm_sendFailure(FailureType_Failure_ActionCancelled,
                        "Encrypt message cancelled");
        go_home();
        return;
    }

    layout_simple_message("Encrypting Message...");

    if(cryptoMessageEncrypt(&pubkey, msg->message.bytes, msg->message.size,
                            display_only,
                            resp->nonce.bytes, &(resp->nonce.size), resp->message.bytes,
                            &(resp->message.size),
                            resp->hmac.bytes, &(resp->hmac.size), signing ? node->private_key : 0,
                            signing ? address_raw : 0) != 0)
    {
        fsm_sendFailure(FailureType_Failure_ActionCancelled,
                        "Error encrypting message");
        go_home();
        return;
    }

    resp->has_nonce = true;
    resp->has_message = true;
    resp->has_hmac = true;
    msg_write(MessageType_MessageType_EncryptedMessage, resp);
    go_home();
}