package sasquatch import ( "crypto/rand" "errors" "fmt" "strconv" "golang.org/x/crypto/chacha20poly1305" "golang.org/x/crypto/scrypt" "golang.org/x/crypto/ssh" ) const challengeLabel = "charm.sh/v1/challenge" type ChallengeRecipient struct { signer ssh.Signer workFactor int } var _ Recipient = &ChallengeRecipient{} func (*ChallengeRecipient) Type() string { return "challenge" } // NewChallengeRecipient returns a new ChallengeRecipient with the provided // signer. func NewChallengeRecipient(signer ssh.Signer) (*ChallengeRecipient, error) { r := &ChallengeRecipient{ signer: signer, // TODO: automatically scale this to 0.5s (with a min) in the CLI. workFactor: 8, // 0.5s on a modern machine } return r, nil } func (r *ChallengeRecipient) Wrap(fileKey []byte) (*Stanza, error) { challenge := make([]byte, 64) if _, err := rand.Read(challenge[:]); err != nil { return nil, err } sig, err := r.signer.Sign(rand.Reader, challenge) if err != nil { return nil, err } salt := make([]byte, 16) if _, err := rand.Read(salt[:]); err != nil { return nil, err } logN := r.workFactor l := &Stanza{ Type: "challenge", Args: []string{ sshFingerprint(r.signer.PublicKey()), EncodeToString(salt), strconv.Itoa(logN), EncodeToString(challenge), }, } salt = append([]byte(challengeLabel), salt...) k, err := scrypt.Key(sig.Blob, salt, 1<