| 1 |
package nacl |
| 2 |
|
| 3 |
import ( |
| 4 |
"crypto/rand" |
| 5 |
"fmt" |
| 6 |
"io/ioutil" |
| 7 |
"os" |
| 8 |
|
| 9 |
"github.com/pkg/errors" |
| 10 |
"github.com/smallstep/cli/command" |
| 11 |
"github.com/smallstep/cli/errs" |
| 12 |
"github.com/smallstep/cli/flags" |
| 13 |
"github.com/smallstep/cli/ui" |
| 14 |
"github.com/smallstep/cli/utils" |
| 15 |
"github.com/urfave/cli" |
| 16 |
"golang.org/x/crypto/nacl/box" |
| 17 |
) |
| 18 |
|
| 19 |
func boxCommand() cli.Command { |
| 20 |
return cli.Command{ |
| 21 |
Name: "box", |
| 22 |
Usage: "authenticate and encrypt small messages using public-key cryptography", |
| 23 |
UsageText: "step crypto nacl box <subcommand> [arguments] [global-flags] [subcommand-flags]", |
| 24 |
Description: `**step crypto nacl box** command group uses public-key cryptography to encrypt, |
| 25 |
decrypt and authenticate messages. The implementation is based on NaCl's |
| 26 |
crypto_box function. |
| 27 |
|
| 28 |
NaCl crypto_box function is designed to meet the standard notions of |
| 29 |
privacy and third-party unforgeability for a public-key authenticated-encryption |
| 30 |
scheme using nonces. For formal definitions see, e.g., Jee Hea An, |
| 31 |
"Authenticated encryption in the public-key setting: security notions and |
| 32 |
analyzes," https://eprint.iacr.org/2001/079. Distinct messages between the same |
| 33 |
{sender, receiver} set are required to have distinct nonces. For example, the |
| 34 |
lexicographically smaller public key can use nonce 1 for its first message to |
| 35 |
the other key, nonce 3 for its second message, nonce 5 for its third message, |
| 36 |
etc., while the lexicographically larger public key uses nonce 2 for its first |
| 37 |
message to the other key, nonce 4 for its second message, nonce 6 for its third |
| 38 |
message, etc. Nonces are long enough that randomly generated nonces have |
| 39 |
negligible risk of collision. |
| 40 |
|
| 41 |
There is no harm in having the same nonce for different messages if the {sender, |
| 42 |
receiver} sets are different. This is true even if the sets overlap. For |
| 43 |
example, a sender can use the same nonce for two different messages if the |
| 44 |
messages are sent to two different public keys. |
| 45 |
|
| 46 |
By default nonces are alphanumeric, but it's possible to use binary nonces using |
| 47 |
the prefix 'base64:' and the standard base64 encoding of the data, e.g. |
| 48 |
'base64:081D3pFPBkwx1bURR9HQjiYbAUxigo0Z'. The prefix 'string:' is also |
| 49 |
accepted, but it will be equivalent to not using a prefix. Nonces cannot be |
| 50 |
longer than 24 bytes. |
| 51 |
|
| 52 |
NaCl crypto_box is not meant to provide non-repudiation. On the contrary: they |
| 53 |
guarantee repudiability. A receiver can freely modify a boxed message, and |
| 54 |
therefore cannot convince third parties that this particular message came from |
| 55 |
the sender. The sender and receiver are nevertheless protected against forgeries |
| 56 |
by other parties. In the terminology of |
| 57 |
https://groups.google.com/group/sci.crypt/msg/ec5c18b23b11d82c, NaCl crypto_box |
| 58 |
uses "public-key authenticators" rather than "public-key signatures." |
| 59 |
|
| 60 |
Users who want public verifiability (or receiver-assisted public verifiability) |
| 61 |
should instead use signatures (or signcryption). |
| 62 |
|
| 63 |
NaCl crypto_box is curve25519xsalsa20poly1305, a particular combination of |
| 64 |
Curve25519, Salsa20, and Poly1305 specified in "Cryptography in NaCl". This |
| 65 |
function is conjectured to meet the standard notions of privacy and third-party |
| 66 |
unforgeability. |
| 67 |
|
| 68 |
These commands are interoperable with NaCl: https://nacl.cr.yp.to/box.html |
| 69 |
|
| 70 |
## EXAMPLES |
| 71 |
|
| 72 |
Create a keypair for encrypting/decrypting messages: |
| 73 |
''' |
| 74 |
# Bob |
| 75 |
$ step crypto nacl box keypair bob.box.pub bob.box.priv |
| 76 |
|
| 77 |
# Alice |
| 78 |
$ step crypto nacl box keypair alice.box.pub alice.box.priv |
| 79 |
''' |
| 80 |
|
| 81 |
Bob encrypts a message for Alice using her public key and signs it using his |
| 82 |
private key: |
| 83 |
''' |
| 84 |
$ echo message | step crypto nacl box seal nonce alice.box.pub bob.box.priv |
| 85 |
0oM0A6xIezA6iMYssZECmbMRQh77mzDt |
| 86 |
''' |
| 87 |
|
| 88 |
Alice receives the encrypted message and the nonce and decrypts with her |
| 89 |
private key and validates the message from Bob using his public key: |
| 90 |
''' |
| 91 |
$ echo 0oM0A6xIezA6iMYssZECmbMRQh77mzDt | step crypto nacl box open nonce bob.box.pub alice.box.priv |
| 92 |
message |
| 93 |
''' |
| 94 |
|
| 95 |
Decrypt the message using a base64 nonce: |
| 96 |
''' |
| 97 |
$ echo 0oM0A6xIezA6iMYssZECmbMRQh77mzDt | step crypto nacl box open base64:bm9uY2U= bob.box.pub alice.box.priv |
| 98 |
message |
| 99 |
'''`, |
| 100 |
Subcommands: cli.Commands{ |
| 101 |
boxKeypairCommand(), |
| 102 |
boxOpenCommand(), |
| 103 |
boxSealCommand(), |
| 104 |
}, |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
func boxKeypairCommand() cli.Command { |
| 109 |
return cli.Command{ |
| 110 |
Name: "keypair", |
| 111 |
Action: command.ActionFunc(boxKeypairAction), |
| 112 |
Usage: "generate a key for use with seal and open", |
| 113 |
UsageText: "**step crypto nacl box keypair** <pub-file> <priv-file>", |
| 114 |
Description: `Generates a new public/private keypair suitable for use with seal and open. |
| 115 |
The private key is encrypted using a password in a nacl secretbox. |
| 116 |
|
| 117 |
This command uses an implementation of NaCl's crypto_box_keypair function. |
| 118 |
|
| 119 |
For examples, see **step help crypto nacl box**. |
| 120 |
|
| 121 |
## POSITIONAL ARGUMENTS |
| 122 |
|
| 123 |
<pub-file> |
| 124 |
: The path to write the public key. |
| 125 |
|
| 126 |
<priv-file> |
| 127 |
: The path to write the encrypted private key.`, |
| 128 |
Flags: []cli.Flag{flags.Force}, |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
func boxOpenCommand() cli.Command { |
| 133 |
return cli.Command{ |
| 134 |
Name: "open", |
| 135 |
Action: cli.ActionFunc(boxOpenAction), |
| 136 |
Usage: "authenticate and decrypt a box produced by seal", |
| 137 |
UsageText: `**step crypto nacl box open** <nonce> <sender-pub-key> <priv-key> |
| 138 |
[--raw]`, |
| 139 |
Description: `Authenticate and decrypt a box produced by seal using the specified KEY. If |
| 140 |
PRIV_KEY is encrypted you will be prompted for the password. The sealed box is |
| 141 |
read from STDIN and the decrypted plaintext is written to STDOUT. |
| 142 |
|
| 143 |
This command uses an implementation of NaCl's crypto_box_open function. |
| 144 |
|
| 145 |
For examples, see **step help crypto nacl box**. |
| 146 |
|
| 147 |
## POSITIONAL ARGUMENTS |
| 148 |
|
| 149 |
<nonce> |
| 150 |
: The nonce provided when the box was sealed. |
| 151 |
|
| 152 |
: To use a binary nonce use the prefix 'base64:' and the standard base64 |
| 153 |
encoding. e.g. base64:081D3pFPBkwx1bURR9HQjiYbAUxigo0Z |
| 154 |
|
| 155 |
<sender-pub-key> |
| 156 |
: The path to the public key of the peer that produced the sealed box. |
| 157 |
|
| 158 |
<priv-key> |
| 159 |
: The path to the private key used to open the box.`, |
| 160 |
Flags: []cli.Flag{ |
| 161 |
cli.BoolFlag{ |
| 162 |
Name: "raw", |
| 163 |
Usage: "Indicates that input is not base64 encoded", |
| 164 |
}, |
| 165 |
}, |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
func boxSealCommand() cli.Command { |
| 170 |
return cli.Command{ |
| 171 |
Name: "seal", |
| 172 |
Action: cli.ActionFunc(boxSealAction), |
| 173 |
Usage: "produce an authenticated and encrypted ciphertext", |
| 174 |
UsageText: `**step crypto nacl box seal** <nonce> <recipient-pub-key> <priv-key> |
| 175 |
[--raw]`, |
| 176 |
Description: `Reads plaintext from STDIN and writes an encrypted and authenticated |
| 177 |
ciphertext to STDOUT. The "box" can be open by the a recipient who has access |
| 178 |
to the private key corresponding to <recipient-pub-key>. |
| 179 |
|
| 180 |
This command uses an implementation of NaCl's crypto_box function. |
| 181 |
|
| 182 |
For examples, see **step help crypto nacl box**. |
| 183 |
|
| 184 |
## POSITIONAL ARGUMENTS |
| 185 |
|
| 186 |
<nonce> |
| 187 |
: Must be unique for each distinct message for a given pair of keys. |
| 188 |
|
| 189 |
: To use a binary nonce use the prefix 'base64:' and the standard base64 |
| 190 |
encoding. e.g. base64:081D3pFPBkwx1bURR9HQjiYbAUxigo0Z |
| 191 |
|
| 192 |
<recipient-pub-key> |
| 193 |
: The path to the public key of the intended recipient of the sealed box. |
| 194 |
|
| 195 |
<priv-key> |
| 196 |
: The path to the private key used for authentication.`, |
| 197 |
Flags: []cli.Flag{ |
| 198 |
cli.BoolFlag{ |
| 199 |
Name: "raw", |
| 200 |
Usage: "Do not base64 encode output", |
| 201 |
}, |
| 202 |
}, |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
func boxKeypairAction(ctx *cli.Context) error { |
| 207 |
if err := errs.NumberOfArguments(ctx, 2); err != nil { |
| 208 |
return err |
| 209 |
} |
| 210 |
|
| 211 |
args := ctx.Args() |
| 212 |
pubFile, privFile := args[0], args[1] |
| 213 |
if pubFile == privFile { |
| 214 |
return errs.EqualArguments(ctx, "<pub-file>", "<priv-file>") |
| 215 |
} |
| 216 |
|
| 217 |
pub, priv, err := box.GenerateKey(rand.Reader) |
| 218 |
if err != nil { |
| 219 |
return errors.Wrap(err, "error generating key") |
| 220 |
} |
| 221 |
|
| 222 |
if err := utils.WriteFile(pubFile, pub[:], 0600); err != nil { |
| 223 |
return errs.FileError(err, pubFile) |
| 224 |
} |
| 225 |
|
| 226 |
if err := utils.WriteFile(privFile, priv[:], 0600); err != nil { |
| 227 |
return errs.FileError(err, privFile) |
| 228 |
} |
| 229 |
|
| 230 |
ui.Printf("Your public key has been saved in %s.\n", pubFile) |
| 231 |
ui.Printf("Your private key has been saved in %s.\n", privFile) |
| 232 |
return nil |
| 233 |
} |
| 234 |
|
| 235 |
func boxOpenAction(ctx *cli.Context) error { |
| 236 |
if err := errs.NumberOfArguments(ctx, 3); err != nil { |
| 237 |
return err |
| 238 |
} |
| 239 |
|
| 240 |
args := ctx.Args() |
| 241 |
nonce, err := decodeNonce(args[0]) |
| 242 |
if err != nil { |
| 243 |
return err |
| 244 |
} |
| 245 |
pubFile, privFile := args[1], args[2] |
| 246 |
|
| 247 |
if len(nonce) > 24 { |
| 248 |
return errors.New("nonce cannot be longer than 24 bytes") |
| 249 |
} |
| 250 |
|
| 251 |
pub, err := ioutil.ReadFile(pubFile) |
| 252 |
if err != nil { |
| 253 |
return errs.FileError(err, pubFile) |
| 254 |
} else if len(pub) != 32 { |
| 255 |
return errors.New("invalid public key: key size is not 32 bytes") |
| 256 |
} |
| 257 |
|
| 258 |
priv, err := ioutil.ReadFile(privFile) |
| 259 |
if err != nil { |
| 260 |
return errs.FileError(err, privFile) |
| 261 |
} else if len(priv) != 32 { |
| 262 |
return errors.New("invalid private key: key size is not 32 bytes") |
| 263 |
} |
| 264 |
|
| 265 |
input, err := utils.ReadAll(os.Stdin) |
| 266 |
if err != nil { |
| 267 |
return errs.Wrap(err, "error reading input") |
| 268 |
} |
| 269 |
|
| 270 |
var rawInput []byte |
| 271 |
if ctx.Bool("raw") { |
| 272 |
rawInput = input |
| 273 |
} else { |
| 274 |
|
| 275 |
|
| 276 |
rawInput = make([]byte, b64Encoder.DecodedLen(len(input))) |
| 277 |
n, err := b64Encoder.Decode(rawInput, input) |
| 278 |
if err != nil { |
| 279 |
return errors.Wrap(err, "error decoding base64 input") |
| 280 |
} |
| 281 |
rawInput = rawInput[:n] |
| 282 |
} |
| 283 |
|
| 284 |
var n [24]byte |
| 285 |
var pb, pv [32]byte |
| 286 |
copy(n[:], nonce) |
| 287 |
copy(pb[:], pub) |
| 288 |
copy(pv[:], priv) |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
raw, ok := box.Open(nil, rawInput, &n, &pb, &pv) |
| 293 |
if !ok { |
| 294 |
return errors.New("error authenticating or decrypting input") |
| 295 |
} |
| 296 |
|
| 297 |
os.Stdout.Write(raw) |
| 298 |
return nil |
| 299 |
} |
| 300 |
|
| 301 |
func boxSealAction(ctx *cli.Context) error { |
| 302 |
if err := errs.NumberOfArguments(ctx, 3); err != nil { |
| 303 |
return err |
| 304 |
} |
| 305 |
|
| 306 |
args := ctx.Args() |
| 307 |
nonce, err := decodeNonce(args[0]) |
| 308 |
if err != nil { |
| 309 |
return err |
| 310 |
} |
| 311 |
pubFile, privFile := args[1], args[2] |
| 312 |
|
| 313 |
if len(nonce) > 24 { |
| 314 |
return errors.New("nonce cannot be longer than 24 bytes") |
| 315 |
} |
| 316 |
|
| 317 |
pub, err := ioutil.ReadFile(pubFile) |
| 318 |
if err != nil { |
| 319 |
return errs.FileError(err, pubFile) |
| 320 |
} else if len(pub) != 32 { |
| 321 |
return errors.New("invalid public key: key size is not 32 bytes") |
| 322 |
} |
| 323 |
|
| 324 |
priv, err := ioutil.ReadFile(privFile) |
| 325 |
if err != nil { |
| 326 |
return errs.FileError(err, privFile) |
| 327 |
} else if len(priv) != 32 { |
| 328 |
return errors.New("invalid private key: key size is not 32 bytes") |
| 329 |
} |
| 330 |
|
| 331 |
input, err := utils.ReadInput("Please enter text to seal") |
| 332 |
if err != nil { |
| 333 |
return errors.Wrap(err, "error reading input") |
| 334 |
} |
| 335 |
|
| 336 |
var n [24]byte |
| 337 |
var pb, pv [32]byte |
| 338 |
copy(n[:], nonce) |
| 339 |
copy(pb[:], pub) |
| 340 |
copy(pv[:], priv) |
| 341 |
|
| 342 |
|
| 343 |
raw := box.Seal(nil, input, &n, &pb, &pv) |
| 344 |
if ctx.Bool("raw") { |
| 345 |
os.Stdout.Write(raw) |
| 346 |
} else { |
| 347 |
fmt.Println(b64Encoder.EncodeToString(raw)) |
| 348 |
} |
| 349 |
|
| 350 |
return nil |
| 351 |
} |