def test_cosi_combination(keyset): message = hashlib.sha512(b"You all have to sign this.").digest() selection = [RFC8032_VECTORS[i] for i in keyset] # zip(*iterable) turns a list of tuples to a tuple of lists privkeys, pubkeys, _, _ = zip(*selection) nonce_pairs = [cosi.get_nonce(pk, message) for pk in privkeys] nonces, commits = zip(*nonce_pairs) # calculate global pubkey and commitment global_pk = cosi.combine_keys(pubkeys) global_commit = cosi.combine_keys(commits) # generate individual signatures signatures = [ cosi.sign_with_privkey(message, privkey, global_pk, nonce, global_commit) for privkey, nonce in zip(privkeys, nonces) ] # combine signatures global_sig = cosi.combine_sig(global_commit, signatures) try: cosi.verify(global_sig, message, global_pk) except Exception: pytest.fail("Failed to validate global signature")
def test_cosi_sign(self): self.setup_mnemonic_pin_passphrase() digest = sha256(b"this is a message").digest() c0 = cosi.commit(self.client, parse_path("10018'/0'"), digest) c1 = cosi.commit(self.client, parse_path("10018'/1'"), digest) c2 = cosi.commit(self.client, parse_path("10018'/2'"), digest) global_pk = cosi.combine_keys([c0.pubkey, c1.pubkey, c2.pubkey]) global_R = cosi.combine_keys( [c0.commitment, c1.commitment, c2.commitment]) # fmt: off sig0 = cosi.sign(self.client, parse_path("10018'/0'"), digest, global_R, global_pk) sig1 = cosi.sign(self.client, parse_path("10018'/1'"), digest, global_R, global_pk) sig2 = cosi.sign(self.client, parse_path("10018'/2'"), digest, global_R, global_pk) # fmt: on sig = cosi.combine_sig( global_R, [sig0.signature, sig1.signature, sig2.signature]) cosi.verify(sig, digest, global_pk)
def test_cosi_combination(keyset): message = hashlib.sha512(b"You all have to sign this.").digest() selection = [RFC8032_VECTORS[i] for i in keyset] # zip(*iterable) turns a list of tuples to a tuple of lists privkeys, pubkeys, _, _ = zip(*selection) nonce_pairs = [cosi.get_nonce(pk, message) for pk in privkeys] nonces, commits = zip(*nonce_pairs) # calculate global pubkey and commitment global_pk = cosi.combine_keys(pubkeys) global_commit = cosi.combine_keys(commits) # generate individual signatures signatures = [ cosi.sign_with_privkey(message, privkey, global_pk, nonce, global_commit) for privkey, nonce in zip(privkeys, nonces) ] # combine signatures global_sig = cosi.combine_sig(global_commit, signatures) try: cosi.verify(global_sig, message, global_pk) except ValueError: pytest.fail("Failed to validate global signature")
def _check_signature_any(header: c.Container, m: int, pubkeys: List[bytes], is_devel: bool) -> Optional[bool]: if all_zero(header.signature) and header.sigmask == 0: return Status.MISSING try: digest = firmware.header_digest(header) cosi.verify(header.signature, digest, m, pubkeys, header.sigmask) return Status.VALID if not is_devel else Status.DEVEL except Exception: return Status.INVALID
def test_single_eddsa_vector(privkey, pubkey, message, signature): my_pubkey = cosi.pubkey_from_privkey(privkey) assert my_pubkey == pubkey try: cosi.verify(signature, message, pubkey) except ValueError: pytest.fail("Signature does not verify.") fake_signature = signature[:37] + b"\xf0" + signature[38:] with pytest.raises(_ed25519.SignatureMismatch): cosi.verify(fake_signature, message, pubkey)
def test_single_eddsa_vector(privkey, pubkey, message, signature): my_pubkey = cosi.pubkey_from_privkey(privkey) assert my_pubkey == pubkey try: cosi.verify(signature, message, pubkey) except ValueError: pytest.fail("Signature does not verify.") fake_signature = b"\xf1" + signature[1:] with pytest.raises(ValueError): cosi.verify(fake_signature, message, pubkey)
def test_cosi_compat(self): self.setup_mnemonic_pin_passphrase() digest = sha256(b'this is not a pipe').digest() remote_commit = self.client.cosi_commit(parse_path("10018'/0'"), digest) local_privkey = sha256(b'private key').digest()[:32] local_pubkey = cosi.pubkey_from_privkey(local_privkey) local_nonce, local_commitment = cosi.get_nonce(local_privkey, digest, 42) global_pk = cosi.combine_keys([remote_commit.pubkey, local_pubkey]) global_R = cosi.combine_keys([remote_commit.commitment, local_commitment]) remote_sig = self.client.cosi_sign(parse_path("10018'/0'"), digest, global_R, global_pk) local_sig = cosi.sign_with_privkey(digest, local_privkey, global_pk, local_nonce, global_R) sig = cosi.combine_sig(global_R, [remote_sig.signature, local_sig]) cosi.verify(sig, digest, global_pk)
def test_cosi_compat(self, client): digest = sha256(b"this is not a pipe").digest() remote_commit = cosi.commit(client, parse_path("10018'/0'"), digest) local_privkey = sha256(b"private key").digest()[:32] local_pubkey = cosi.pubkey_from_privkey(local_privkey) local_nonce, local_commitment = cosi.get_nonce(local_privkey, digest, 42) global_pk = cosi.combine_keys([remote_commit.pubkey, local_pubkey]) global_R = cosi.combine_keys([remote_commit.commitment, local_commitment]) remote_sig = cosi.sign( client, parse_path("10018'/0'"), digest, global_R, global_pk ) local_sig = cosi.sign_with_privkey( digest, local_privkey, global_pk, local_nonce, global_R ) sig = cosi.combine_sig(global_R, [remote_sig.signature, local_sig]) cosi.verify(sig, digest, global_pk)
def test_cosi_compat(self): self.setup_mnemonic_pin_passphrase() digest = sha256(b"this is not a pipe").digest() remote_commit = cosi.commit(self.client, parse_path("10018'/0'"), digest) local_privkey = sha256(b"private key").digest()[:32] local_pubkey = cosi.pubkey_from_privkey(local_privkey) local_nonce, local_commitment = cosi.get_nonce(local_privkey, digest, 42) global_pk = cosi.combine_keys([remote_commit.pubkey, local_pubkey]) global_R = cosi.combine_keys([remote_commit.commitment, local_commitment]) remote_sig = cosi.sign( self.client, parse_path("10018'/0'"), digest, global_R, global_pk ) local_sig = cosi.sign_with_privkey( digest, local_privkey, global_pk, local_nonce, global_R ) sig = cosi.combine_sig(global_R, [remote_sig.signature, local_sig]) cosi.verify(sig, digest, global_pk)
def test_cosi_sign(self): self.setup_mnemonic_pin_passphrase() digest = sha256(b"this is a message").digest() c0 = cosi.commit(self.client, parse_path("10018'/0'"), digest) c1 = cosi.commit(self.client, parse_path("10018'/1'"), digest) c2 = cosi.commit(self.client, parse_path("10018'/2'"), digest) global_pk = cosi.combine_keys([c0.pubkey, c1.pubkey, c2.pubkey]) global_R = cosi.combine_keys([c0.commitment, c1.commitment, c2.commitment]) # fmt: off sig0 = cosi.sign(self.client, parse_path("10018'/0'"), digest, global_R, global_pk) sig1 = cosi.sign(self.client, parse_path("10018'/1'"), digest, global_R, global_pk) sig2 = cosi.sign(self.client, parse_path("10018'/2'"), digest, global_R, global_pk) # fmt: on sig = cosi.combine_sig( global_R, [sig0.signature, sig1.signature, sig2.signature] ) cosi.verify(sig, digest, global_pk)
def test_m_of_n(): privkeys, pubkeys, _, _ = zip(*RFC8032_VECTORS) message = hashlib.sha512(b"My hovercraft is full of eels!").digest() signer_ids = 0, 2, 3 signers = [privkeys[i] for i in signer_ids] signer_pubkeys = [pubkeys[i] for i in signer_ids] sigmask = sum(1 << i for i in signer_ids) # generate multisignature nonce_pairs = [cosi.get_nonce(pk, message) for pk in signers] nonces, commits = zip(*nonce_pairs) global_pk = cosi.combine_keys(signer_pubkeys) global_commit = cosi.combine_keys(commits) signatures = [ cosi.sign_with_privkey(message, privkey, global_pk, nonce, global_commit) for privkey, nonce in zip(signers, nonces) ] global_sig = cosi.combine_sig(global_commit, signatures) try: # this is what we are actually doing cosi.verify(global_sig, message, 3, pubkeys, sigmask) # we can require less signers too cosi.verify(global_sig, message, 1, pubkeys, sigmask) except Exception: pytest.fail("Failed to validate by sigmask") # and now for various ways that should fail with pytest.raises(ValueError) as e: cosi.verify(global_sig, message, 3, pubkeys[:2], sigmask) assert "more public keys than provided" in e.value.args[0] with pytest.raises(ValueError) as e: cosi.verify(global_sig, message, 0, pubkeys, 0) assert "At least one signer" in e.value.args[0] with pytest.raises(_ed25519.SignatureMismatch) as e: # at least 5 signatures required cosi.verify(global_sig, message, 5, pubkeys, sigmask) assert "Insufficient number of signatures" in e.value.args[0] with pytest.raises(_ed25519.SignatureMismatch) as e: # wrong sigmask cosi.verify(global_sig, message, 3, pubkeys, 7) assert "signature does not pass verification" in e.value.args[0]