Пример #1
0
// GetMixData decrypts the private portion of a nymaddress.
func (ad *Address) GetMixData(keysLookup KeyFunc) (*AddressPrivate, error) {
	pubkey := new([KeySize]byte)
	copy(pubkey[:], ad.MixPubKey)
	privkey := keysLookup(pubkey)
	if privkey == nil {
		return nil, ErrNoKey
	}
	sharedSecret := new([KeySize]byte)
	addrKey := new([KeySize]byte)
	copy(addrKey[:], ad.AddressKey)
	curve25519.ScalarMult(sharedSecret, privkey, addrKey)
	cr, err := lioness.New(sharedSecret[:]) // saves some bytes and is safe against tagging
	if err != nil {
		return nil, err
	}
	privmarshal, err := cr.Decrypt(ad.PrivateData)
	if err != nil {
		return nil, err
	}
	ap := new(AddressPrivate)
	_, err = asn1.Unmarshal(privmarshal, ap)
	if err != nil {
		return nil, err
	}
	return ap, nil
}
Пример #2
0
// NewAddress generates a new nymaddress for nym/address from AddressTemplate.
// Only the first KeySize bytes of Nym are used, so use a hash of the true nym
// here.
func (tmp AddressTemplate) NewAddress(MailboxAddress, Nym []byte) ([]byte, error) {
	xnym := make([]byte, KeySize)
	copy(xnym, Nym)
	Nym = xnym
	mixAddress := tmp.MixCandidates.Expire(tmp.Expire).Rand() // Select a random mix
	if mixAddress == nil {
		return nil, ErrNoMix
	}
	pubKeyRand, privKeyRand, err := genKeyRandom()
	if err != nil {
		return nil, err
	}
	nonce, err := genNonce()
	if err != nil {
		return nil, err
	}

	nymaddr := new(Address)
	nymaddr.MixAddress = []byte(mixAddress.Address)
	nymaddr.MixPubKey = mixAddress.Pubkey
	nymaddr.TokenPubKey = mixAddress.TokenKey
	nymaddr.Expire = tmp.Expire
	nymaddr.SingleUse = tmp.SingleUse
	nymaddr.AddressKey = pubKeyRand[:]

	nymprivate := new(AddressPrivate)
	nymprivate.System = tmp.System
	nymprivate.Expire = tmp.Expire
	nymprivate.Address = MailboxAddress
	nymprivate.SingleUse = tmp.SingleUse
	nymprivate.MinDelay = tmp.MinDelay
	nymprivate.MaxDelay = tmp.MaxDelay
	nymprivate.Nonce = nonce[:]

	// Calculate public key that the mix will encrypt TO
	recPub, _ := tmp.genDetermKeys(nymprivate.Nonce, Nym)
	nymprivate.ReceiverPubKey = recPub[:]

	// Calculate symmetric key
	symkey := tmp.genSymKey(nymprivate.Nonce, nymprivate.ReceiverPubKey, nymprivate.Address)

	nymprivate.EncNym = encryptNym(symkey, Nym)

	nymprivate.HMACHead = calcHmac(symkey, nymprivate.Nonce, nymprivate.ReceiverPubKey, nymprivate.EncNym)

	privmarshal, err := asn1.Marshal(*nymprivate)
	if err != nil {
		return nil, err
	}
	mixPubKey := new([KeySize]byte)
	sharedSecret := new([KeySize]byte)
	copy(mixPubKey[:], nymaddr.MixPubKey)
	curve25519.ScalarMult(sharedSecret, privKeyRand, mixPubKey)
	cr, err := lioness.New(sharedSecret[:]) // saves some bytes and is safe against tagging
	if err != nil {
		return nil, err
	}
	privmarshalEnc, err := cr.Encrypt(privmarshal)
	if err != nil {
		return nil, err
	}
	nymaddr.PrivateData = privmarshalEnc
	return asn1.Marshal(*nymaddr)
}