github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/teams/box.go (about)

     1  package teams
     2  
     3  import (
     4  	"encoding/base64"
     5  
     6  	"github.com/keybase/client/go/kbcrypto"
     7  	"github.com/keybase/client/go/libkb"
     8  	"github.com/keybase/client/go/protocol/keybase1"
     9  )
    10  
    11  // TeamBox comes from api server team/get endpoint.
    12  type TeamBox struct {
    13  	Nonce           string
    14  	SenderKID       keybase1.KID `json:"sender_kid"`
    15  	Generation      keybase1.PerTeamKeyGeneration
    16  	Ctext           string
    17  	PerUserKeySeqno keybase1.Seqno `json:"per_user_key_seqno"`
    18  }
    19  
    20  // Open decrypts Ctext using encKey.
    21  func (t *TeamBox) Open(encKey *libkb.NaclDHKeyPair) (keybase1.PerTeamKeySeed, error) {
    22  	var ret keybase1.PerTeamKeySeed
    23  
    24  	nonce, err := t.nonceBytes()
    25  	if err != nil {
    26  		return ret, err
    27  	}
    28  	ctext, err := t.ctextBytes()
    29  	if err != nil {
    30  		return ret, err
    31  	}
    32  	nei := &libkb.NaclEncryptionInfo{
    33  		Ciphertext:     ctext,
    34  		EncryptionType: kbcrypto.KIDNaclDH,
    35  		Nonce:          nonce,
    36  		Receiver:       encKey.GetKID().ToBytes(),
    37  		Sender:         t.SenderKID.ToBytes(),
    38  	}
    39  
    40  	plaintext, _, err := encKey.Decrypt(nei)
    41  	if err != nil {
    42  		return ret, err
    43  	}
    44  
    45  	return libkb.MakeByte32Soft(plaintext)
    46  }
    47  
    48  func (t *TeamBox) nonceBytes() ([]byte, error) {
    49  	return base64.StdEncoding.DecodeString(t.Nonce)
    50  }
    51  
    52  func (t *TeamBox) ctextBytes() ([]byte, error) {
    53  	return base64.StdEncoding.DecodeString(t.Ctext)
    54  }