github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/encrypteddb/secretkeys.go (about)

     1  package encrypteddb
     2  
     3  import (
     4  	"github.com/keybase/client/go/engine"
     5  	"github.com/keybase/client/go/libkb"
     6  	"github.com/keybase/client/go/protocol/keybase1"
     7  	"golang.org/x/net/context"
     8  )
     9  
    10  func GetSecretBoxKey(ctx context.Context, g *libkb.GlobalContext,
    11  	reason libkb.EncryptionReason, reasonStr string) (fkey [32]byte, err error) {
    12  	// Get secret device key
    13  	encKey, err := engine.GetMySecretKey(ctx, g, libkb.DeviceEncryptionKeyType,
    14  		reasonStr)
    15  	if err != nil {
    16  		return fkey, err
    17  	}
    18  	kp, ok := encKey.(libkb.NaclDHKeyPair)
    19  	if !ok || kp.Private == nil {
    20  		return fkey, libkb.KeyCannotDecryptError{}
    21  	}
    22  
    23  	// Derive symmetric key from device key
    24  	skey, err := encKey.SecretSymmetricKey(reason)
    25  	if err != nil {
    26  		return fkey, err
    27  	}
    28  
    29  	copy(fkey[:], skey[:])
    30  	return fkey, nil
    31  }
    32  
    33  func GetSecretBoxKeyWithUID(ctx context.Context, g *libkb.GlobalContext, uid keybase1.UID,
    34  	reason libkb.EncryptionReason, reasonStr string) (fkey [32]byte, err error) {
    35  	// Get secret device key
    36  	encKey, err := engine.GetMySecretKeyWithUID(ctx, g, uid,
    37  		libkb.DeviceEncryptionKeyType, reasonStr)
    38  	if err != nil {
    39  		return fkey, err
    40  	}
    41  	kp, ok := encKey.(libkb.NaclDHKeyPair)
    42  	if !ok || kp.Private == nil {
    43  		return fkey, libkb.KeyCannotDecryptError{}
    44  	}
    45  
    46  	// Derive symmetric key from device key
    47  	skey, err := encKey.SecretSymmetricKey(reason)
    48  	if err != nil {
    49  		return fkey, err
    50  	}
    51  
    52  	copy(fkey[:], skey[:])
    53  	return fkey, nil
    54  }