bool extract(payment_address& address, const script_type& script) { // Cast a data_chunk to a short_hash and set the address auto set_hash_data = [&address](uint8_t version, const data_chunk& raw_hash) { short_hash hash_data; BITCOIN_ASSERT(raw_hash.size() == hash_data.size()); std::copy(raw_hash.begin(), raw_hash.end(), hash_data.begin()); address.set(version, hash_data); }; const operation_stack& ops = script.operations(); payment_type pay_type = script.type(); switch (pay_type) { case payment_type::pubkey: BITCOIN_ASSERT(ops.size() == 2); set_public_key(address, ops[0].data); return true; case payment_type::pubkey_hash: BITCOIN_ASSERT(ops.size() == 5); set_hash_data(payment_address::pubkey_version, ops[2].data); return true; case payment_type::script_hash: BITCOIN_ASSERT(ops.size() == 3); set_hash_data(payment_address::script_version, ops[1].data); return true; case payment_type::multisig: // Unimplemented... return false; case payment_type::pubkey_hash_sig: BITCOIN_ASSERT(ops.size() == 2); set_public_key(address, ops[1].data); return true; case payment_type::script_code_sig: // Should have at least 1 sig and the script code. BITCOIN_ASSERT(ops.size() > 1); set_script_hash(address, bitcoin_short_hash(ops.back().data)); return true; default: return false; } // Should never happen! return false; }
void ElGamalDecrypter::generate_private_key() { key_type prime = Primality::instance().get_next_prime(); m_privatkey = Primality::instance().get_random_integer(prime - ElGamalDecrypter::key_type::unit); key_type generator = Primality::instance().get_random_integer(prime); key_type y = generator.mod_exp(m_privatkey,prime); set_public_key(prime,generator,y); }
bool extract_input_address( payment_address& address, const script& input_script) { const operation_stack& ops = input_script.operations(); if (!input_has_pubkey(ops)) return false; BITCOIN_ASSERT(ops.size() == 2); const data_chunk& pubkey = ops[1].data; if (!set_public_key(address, pubkey)) return false; return true; }
void dialog_createavatar::InitElements() { stringw title = stringw(lang_[L"CreateAvatar_WindowTitle"].c_str()); window_ = new CGUIDecentralisedDialog(env_, env_->getRootGUIElement(), e_gui_elements::WindowCreateAvatar, rect<s32>(80, 40, 400, 250), true); window_->setDialogSkin(elems_.TxDialogBack, elems_.TxDialogFore); window_->setVisible(true); window_->setText(title.c_str()); env_->setFocus(window_); window_->drop(); s32 posy = 40; elems_.CreateAvPublicKeyLabel = env_->addStaticText(lang_[L"CreateAvatar_PublicKeyLabel"].c_str(), rect<s32>(20, posy, 260, posy + 22), false, true, window_, 0); posy += 22; elems_.CreateAvPublicKeyBox = new CGUIDecentralisedTextbox(L"", true, env_, window_, e_gui_elements::CreateAvPasswordBox, rect<s32>(20, posy, 295, posy + 22)); elems_.CreateAvPublicKeyBox->setEnabled(false); elems_.CreateAvPublicKeyBox->setOverrideColor(SColor(255, 0, 0, 0)); posy += 32; elems_.CreateAvFirstNameLabel = env_->addStaticText(lang_[L"CreateAvatar_FirstnameLabel"].c_str(), rect<s32>(20, posy, 140, posy + 22), false, true, window_, 0); elems_.CreateAvLastNameLabel = env_->addStaticText(lang_[L"CreateAvatar_LastnameLabel"].c_str(), rect<s32>(160, posy, 310, posy + 22), false, true, window_, 0); posy += 22; elems_.CreateAvFirstNameTextBox = new CGUIDecentralisedTextbox(L"", true, env_, window_, e_gui_elements::CreateAvFirstnameBox, rect<s32>(20, posy, 155, posy + 22)); elems_.CreateAvFirstNameTextBox->setEnabled(true); elems_.CreateAvFirstNameTextBox->setOverrideColor(SColor(255, 0, 0, 0)); elems_.CreateAvFirstNameTextBox->drop(); elems_.CreateAvLastNameTextBox = new CGUIDecentralisedTextbox(L"", true, env_, window_, e_gui_elements::CreateAvLastnameBox, rect<s32>(160, posy, 295, posy + 22)); elems_.CreateAvLastNameTextBox->setEnabled(true); elems_.CreateAvLastNameTextBox->setOverrideColor(SColor(255, 0, 0, 0)); elems_.CreateAvLastNameTextBox->drop(); posy += 40; elems_.CreateAvCreateButton = new CGUIDecentralisedButton(env_, window_, e_gui_elements::CreateAvCreateButton, rect<s32>(20, posy, 180, posy + 25)); elems_.CreateAvCreateButton->setImages(elems_.TxButtonLeft, elems_.TxButtonMiddle, elems_.TxButtonRight, elems_.TxButtonPressedLeft, elems_.TxButtonPressedMiddle, elems_.TxButtonPressedRight); elems_.CreateAvCreateButton->setText(lang_[L"CreateAvatar_CreateButtonText"].c_str()); elems_.CreateAvCreateButton->drop(); if (elems_.LoginButton) elems_.LoginButton->setEnabled(true); data_chunk publicKeyChunk = keyPair_.public_key(); payment_address address; set_public_key(address, keyPair_.public_key()); std::string encoded = address.encoded(); std::wstring publicKey(encoded.begin(), encoded.end()); elems_.CreateAvPublicKeyBox->setText(publicKey.c_str()); }
struct item *asymmetric_encryption(struct item *key, struct item *payload) /*@ requires [?f]world(?pub, ?key_clsfy) &*& principal(?principal1, ?count1) &*& item(payload, ?pay, pub) &*& item(key, ?k, pub) &*& k == public_key_item(?principal2, ?count2); @*/ /*@ ensures [f]world(pub, key_clsfy) &*& principal(principal1, count1 + 1) &*& item(payload, pay, pub) &*& item(key, k, pub) &*& item(result, ?enc, pub) &*& col ? true : enc == asymmetric_encrypted_item(principal2, count2, some(pay), ?ent); @*/ { debug_print("ASYM ENCRYPTING:\n"); print_item(payload); struct item* result; result = malloc(sizeof(struct item)); if (result == 0) abort_crypto_lib("Malloc failed"); { pk_context context; unsigned int olen; char output[MAX_PACKAGE_SIZE]; // Key //@ close pk_context(&context); //@ open [f]world(pub, key_clsfy); pk_init(&context); //@ close [f]world(pub, key_clsfy); set_public_key(&context, key); //@ open [f]world(pub, key_clsfy); /*@ assert pk_context_with_key(&context, pk_public, ?principal, ?count, RSA_BIT_KEY_SIZE); @*/ //@ assert col || principal == principal2; //@ assert col || count == count2; // Encryption //@ open item(payload, pay, pub); //@ assert [_]item_constraints(pay, ?pay_cs, pub); if (payload->size > RSA_KEY_SIZE) abort_crypto_lib("Asymmetric encryption failed: incorrect sizes"); void *random_state = nonces_expose_state(); //@ close random_state_predicate(havege_state_initialized); /*@ produce_function_pointer_chunk random_function( asym_enc_havege_random_stub) (havege_state_initialized)(state, out, len) { call(); } @*/ //@ open principal(principal1, count1); if(pk_encrypt(&context, payload->content, (unsigned int) payload->size, output, &olen, MAX_PACKAGE_SIZE, asym_enc_havege_random_stub, random_state) != 0) abort_crypto_lib("Encryption failed"); //@ close principal(principal1, count1 + 1); //@ open cryptogram(output, ?enc_length, ?enc_cs, ?enc_cg); //@ assert enc_cg == cg_asym_encrypted(principal, count, pay_cs, ?ent); //@ assert u_integer(&olen, enc_length); //@ assert enc_length > 0 &*& enc_length < MAX_PACKAGE_SIZE; //@ assert enc_length > 0 &*& enc_length <= RSA_SERIALIZED_KEY_SIZE; nonces_hide_state(random_state); //@ pk_release_context_with_key(&context); pk_free(&context); //@ open pk_context(&context); //@ close [f]world(pub, key_clsfy); // Create item result->size = TAG_LENGTH + (int) olen; result->content = malloc(result->size); if (result->content == 0) {abort_crypto_lib("Malloc failed");} write_tag(result->content, TAG_ASYMMETRIC_ENC); //@ assert result->content |-> ?cont &*& result->size |-> ?size; if (olen < MINIMAL_STRING_SIZE) {abort_crypto_lib("Asymmetric encryption failed: to small");} memcpy(result->content + TAG_LENGTH, output, olen); //@ assert chars(cont, TAG_LENGTH, ?cs_tag); //@ public_chars(cont, TAG_LENGTH); //@ chars_to_secret_crypto_chars(cont, TAG_LENGTH); //@ assert cs_tag == full_tag(TAG_ASYMMETRIC_ENC); //@ crypto_chars_join(cont); //@ item enc = asymmetric_encrypted_item(principal, count, some(pay), ent); //@ list<char> cs = append(cs_tag, enc_cs); //@ WELL_FORMED(cs_tag, enc_cs, TAG_ASYMMETRIC_ENC) //@ close ic_parts(enc)(cs_tag, enc_cs); //@ close ic_cg(enc)(enc_cs, enc_cg); /*@ if (col) { crypto_chars_to_chars(cont, size); public_chars(cont, size); chars_to_secret_crypto_chars(cont, size); public_generated_split(polarssl_pub(pub), cs, TAG_LENGTH); } @*/ //@ well_formed_item_constraints(pay, enc); //@ close item_constraints(enc, cs, pub); //@ leak item_constraints(enc, cs, pub); //@ close item(result, enc, pub); zeroize(output, (int) olen); //@ chars_join(output); //@ close item(payload, pay, pub); } debug_print("ENCRYPTING RESULT:\n"); print_item(result); return result; }
const std::string pubkey_to_address(const std::string& pubkey) { bc::payment_address payaddr; set_public_key(payaddr, bc::data_chunk(pubkey.begin(), pubkey.end())); return payaddr.encoded(); }
BCW_API payment_address hd_public_key::address() const { payment_address address; set_public_key(address, K_); return address; }
void asymmetric_signature_verify(struct item *key, struct item *item, struct item *signature) /*@ requires [?f]world(?pub, ?key_clsfy) &*& item(item, ?i, pub) &*& item(key, ?k, pub) &*& item(signature, ?sig, pub) &*& sig == asymmetric_signature_item(?principal1, ?count1, ?pay1, ?ent) &*& k == public_key_item(?principal2, ?count2); @*/ /*@ ensures [f]world(pub, key_clsfy) &*& item(item, i, pub) &*& item(key, k, pub) &*& item(signature, sig, pub) &*& switch(pay1) { case some(pay2): return col || (principal1 == principal2 && count1 == count2 && i == pay2); case none: return true == col; }; @*/ { debug_print("ASYM SIGNATURE CHECKING:\n"); print_item(signature); check_is_asymmetric_signature(signature); { pk_context context; unsigned int olen; char* output; // Key //@ close pk_context(&context); //@ open [f]world(pub, key_clsfy); pk_init(&context); //@ close [f]world(pub, key_clsfy); set_public_key(&context, key); /*@ assert pk_context_with_key(&context, pk_public, ?principal3, ?count3, RSA_BIT_KEY_SIZE); @*/ // Signature checking //@ open item(item, ?i_old, pub); //@ assert item->content |-> ?i_cont &*& item->size |-> ?i_size; //@ assert crypto_chars(secret, i_cont, i_size, ?i_cs); //@ assert [_]item_constraints(i, i_cs, pub); //@ open item(signature, _, pub); //@ assert signature->content |-> ?s_cont &*& signature->size |-> ?s_size; //@ assert crypto_chars(secret, s_cont, s_size, ?sig_cs); //@ open [_]item_constraints(sig, sig_cs, pub); //@ open ic_parts(sig)(?sig_tag, ?sig_cont); //@ take_append(TAG_LENGTH, sig_tag, sig_cont); //@ drop_append(TAG_LENGTH, sig_tag, sig_cont); //@ open [_]ic_cg(sig)(_, ?cg_sig); //@ assert cg_sig == cg_asym_signature(principal1, count1, ?cs_pay, ent); if (item->size > RSA_KEY_SIZE) abort_crypto_lib("Assymetric signature checking failed: incorrect sizes"); //@ crypto_chars_limits(s_cont); //@ crypto_chars_split(s_cont, TAG_LENGTH); //@ assert crypto_chars(secret, s_cont, TAG_LENGTH, sig_tag); //@ assert crypto_chars(secret, s_cont + TAG_LENGTH, ?s, sig_cont); //@ assert s == s_size - TAG_LENGTH; //@ if (col) cg_sig = chars_for_cg_sur(sig_cont, tag_asym_signature); //@ if (col) crypto_chars_to_chars(s_cont + TAG_LENGTH, s); //@ if (col) public_chars_extract(s_cont + TAG_LENGTH, cg_sig); //@ if (col) chars_to_secret_crypto_chars(s_cont + TAG_LENGTH, s); //@ close cryptogram(s_cont + TAG_LENGTH, s, sig_cont, cg_sig); if(pk_verify(&context, POLARSSL_MD_NONE, item->content, (unsigned int) item->size, signature->content + TAG_LENGTH, (unsigned int) (signature->size - TAG_LENGTH)) != 0) abort_crypto_lib("Signing check failed: signature" "was not created with the provided key"); //@ pk_release_context_with_key(&context); pk_free(&context); //@ open pk_context(&context); //@ open cryptogram(s_cont + TAG_LENGTH, s, sig_cont, cg_sig); //@ crypto_chars_join(s_cont); //@ close item(signature, sig, pub); /*@ if (!col) { assert principal2 == principal3; assert count2 == count3; assert cs_pay == i_cs; open [_]item_constraints(i, cs_pay, pub); switch(pay1) { case some(pay2): assert [_]item_constraints(pay2, cs_pay, pub); item_constraints_injective(i, pay2, cs_pay); case none: open [_]ill_formed_item_chars(sig)(cs_pay); assert false; } } @*/ //@ close item(item, i, pub); } }