github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/kbfs/kbfscrypto/test_util.go (about) 1 // Copyright 2016 Keybase Inc. All rights reserved. 2 // Use of this source code is governed by a BSD 3 // license that can be found in the LICENSE file. 4 5 package kbfscrypto 6 7 import ( 8 "strings" 9 10 "github.com/keybase/client/go/libkb" 11 ) 12 13 // The functions below must be used only in tests. 14 15 func makeFakeRandomBytes(seed string, byteCount int) []byte { 16 paddingLen := byteCount - len(seed) 17 if paddingLen > 0 { 18 seed += strings.Repeat("0", paddingLen) 19 } 20 return []byte(seed[:byteCount]) 21 } 22 23 // MakeFakeSigningKeyOrBust makes a new signing key from fake 24 // randomness made from the given seed. 25 func MakeFakeSigningKeyOrBust(seed string) SigningKey { 26 fakeRandomBytes := makeFakeRandomBytes( 27 seed, libkb.NaclSigningKeySecretSize) 28 var secret [libkb.NaclSigningKeySecretSize]byte 29 copy(secret[:], fakeRandomBytes) 30 kp, err := libkb.MakeNaclSigningKeyPairFromSecret(secret) 31 if err != nil { 32 panic(err) 33 } 34 return NewSigningKey(kp) 35 } 36 37 // MakeFakeVerifyingKeyOrBust makes a new key suitable for verifying 38 // signatures made from the fake signing key made with the same seed. 39 func MakeFakeVerifyingKeyOrBust(seed string) VerifyingKey { 40 sk := MakeFakeSigningKeyOrBust(seed) 41 return sk.GetVerifyingKey() 42 } 43 44 // MakeFakeCryptPrivateKeyOrBust makes a new crypt private key from 45 // fake randomness made from the given seed. 46 func MakeFakeCryptPrivateKeyOrBust(seed string) CryptPrivateKey { 47 fakeRandomBytes := makeFakeRandomBytes(seed, libkb.NaclDHKeySecretSize) 48 var secret [libkb.NaclDHKeySecretSize]byte 49 copy(secret[:], fakeRandomBytes) 50 kp, err := libkb.MakeNaclDHKeyPairFromSecret(secret) 51 if err != nil { 52 panic(err) 53 } 54 return NewCryptPrivateKey(kp) 55 } 56 57 // MakeFakeCryptPublicKeyOrBust makes the public key corresponding to 58 // the crypt private key made with the same seed. 59 func MakeFakeCryptPublicKeyOrBust(seed string) CryptPublicKey { 60 k := MakeFakeCryptPrivateKeyOrBust(seed) 61 return k.GetPublicKey() 62 } 63 64 // MakeFakeTLFCryptKeyOrBust makes a TLF crypt key from the given 65 // seed. 66 func MakeFakeTLFCryptKeyOrBust(seed string) TLFCryptKey { 67 fakeRandomBytes := makeFakeRandomBytes(seed, 32) 68 var key [32]byte 69 copy(key[:], fakeRandomBytes[:32]) 70 return MakeTLFCryptKey(key) 71 } 72 73 // MakeEncryptedTLFCryptKeyClientHalfForTest returns an 74 // EncryptedTLFCryptKeyClientHalf with copies of the given data. 75 func MakeEncryptedTLFCryptKeyClientHalfForTest( 76 version EncryptionVer, encodedClientHalf, nonce []byte) EncryptedTLFCryptKeyClientHalf { 77 clientHalfCopy := make([]byte, len(encodedClientHalf)) 78 nonceCopy := make([]byte, len(nonce)) 79 copy(clientHalfCopy, encodedClientHalf) 80 copy(nonceCopy, nonce) 81 return EncryptedTLFCryptKeyClientHalf{ 82 encryptedData{ 83 Version: version, 84 EncryptedData: clientHalfCopy, 85 Nonce: nonceCopy, 86 }, 87 } 88 }