Skip to content
Snippets Groups Projects
Select Git revision
  • 1df6b29c816c345d13e3d730ed033246da2f25f7
  • release default protected
  • XX-4719/announcementChannels
  • jonah/channelCodenames
  • master protected
  • XX-4601/HavenInvites
  • sihSize
  • project/HavenNotifications
  • hotfix/base8KeySizes
  • Anne/Project/DM
  • XX-4004_ownership_vector_test
  • XX-3566_constant_time_comparison
  • XX-4132-upgrade-channel-keying
  • XX-4133-rsa-to-private
  • XX-3958/ConnectionCLI
  • xx-3893/asymmetric
  • xx-3891/symmetric-integration
  • hotfix/groupChat
  • XX-3770/UpdateExternalDeps
  • dev
  • waitingRoundsRewrite
  • v0.0.9
  • v0.0.8
  • v0.0.7
  • v0.0.6
  • v0.0.5
  • v0.0.4
  • v0.0.3
  • v0.0.2
  • v0.0.1
30 results

mac.go

Blame
  • symmetric.go 2.33 KiB
    ////////////////////////////////////////////////////////////////////////////////
    // Copyright © 2020 xx network SEZC                                           //
    //                                                                            //
    // Use of this source code is governed by a license that can be found in the  //
    // LICENSE file                                                               //
    ////////////////////////////////////////////////////////////////////////////////
    
    package broadcast
    
    import (
    	"github.com/pkg/errors"
    	"gitlab.com/elixxir/client/cmix"
    	"gitlab.com/elixxir/client/cmix/message"
    	"gitlab.com/xx_network/primitives/id"
    	"gitlab.com/xx_network/primitives/id/ephemeral"
    )
    
    // Error messages.
    const (
    	// symmetricClient.Broadcast
    	errNetworkHealth       = "cannot send broadcast when the network is not healthy"
    	errPayloadSize         = "size of payload %d must be less than %d"
    	errBroadcastMethodType = "cannot call %s broadcast using %s channel"
    )
    
    // Tags.
    const (
    	symmCMixSendTag              = "SymmBcast"
    	symmetricBroadcastServiceTag = "SymmetricBroadcast"
    )
    
    // MaxSymmetricPayloadSize returns the maximum size for a broadcasted payload.
    func (bc *broadcastClient) maxSymmetricPayload() int {
    	return bc.net.GetMaxMessageLength()
    }
    
    // Broadcast broadcasts a payload over a symmetric channel.
    // Network must be healthy to send
    // Requires a payload of size bc.MaxSymmetricPayloadSize()
    func (bc *broadcastClient) Broadcast(payload []byte, cMixParams cmix.CMIXParams) (
    	id.Round, ephemeral.Id, error) {
    	if !bc.net.IsHealthy() {
    		return 0, ephemeral.Id{}, errors.New(errNetworkHealth)
    	}
    
    	if len(payload) != bc.maxSymmetricPayload() {
    		return 0, ephemeral.Id{},
    			errors.Errorf(errPayloadSize, len(payload), bc.maxSymmetricPayload())
    	}
    
    	// Encrypt payload
    	rng := bc.rng.GetStream()
    	encryptedPayload, mac, fp := bc.channel.EncryptSymmetric(payload, rng)
    	rng.Close()
    
    	// Create service using symmetric broadcast service tag & channel reception ID
    	// Allows anybody with this info to listen for messages on this channel
    	service := message.Service{
    		Identifier: bc.channel.ReceptionID.Bytes(),
    		Tag:        symmetricBroadcastServiceTag,
    	}
    
    	if cMixParams.DebugTag == cmix.DefaultDebugTag {
    		cMixParams.DebugTag = symmCMixSendTag
    	}
    
    	return bc.net.Send(
    		bc.channel.ReceptionID, fp, service, encryptedPayload, mac, cMixParams)
    }