| 1 | package nacl |
| 2 | |
| 3 | import ( |
| 4 | "encoding/hex" |
| 5 | "fmt" |
| 6 | "io/ioutil" |
| 7 | |
| 8 | "github.com/pkg/errors" |
| 9 | "github.com/smallstep/cli/errs" |
| 10 | "github.com/smallstep/cli/utils" |
| 11 | "github.com/urfave/cli" |
| 12 | "golang.org/x/crypto/nacl/auth" |
| 13 | ) |
| 14 | |
| 15 | func authCommand() cli.Command { |
| 16 | return cli.Command{ |
| 17 | Name: "auth", |
| 18 | Usage: "authenticate a message using a secret key", |
| 19 | UsageText: "step crypto nacl auth <subcommand> [arguments] [global-flags] [subcommand-flags]", |
| 20 | Description: `**step crypto nacl auth** command group uses secret key cryptography to |
| 21 | authenticate and verify messages using a secret key. The implementation is based on NaCl's |
| 22 | crypto_auth function. |
| 23 | |
| 24 | NaCl crypto_auth function, viewed as a function of the message for a uniform |
| 25 | random key, is designed to meet the standard notion of unforgeability. This |
| 26 | means that an attacker cannot find authenticators for any messages not |
| 27 | authenticated by the sender, even if the attacker has adaptively influenced the |
| 28 | messages authenticated by the sender. For a formal definition see, e.g., Section |
| 29 | 2.4 of Bellare, Kilian, and Rogaway, "The security of the cipher block chaining |
| 30 | message authentication code," Journal of Computer and System Sciences 61 (2000), |
| 31 | 362–399; http://www-cse.ucsd.edu/~mihir/papers/cbc.html. |
| 32 | |
| 33 | NaCl crypto_auth does not make any promises regarding "strong" unforgeability; |
| 34 | perhaps one valid authenticator can be converted into another valid |
| 35 | authenticator for the same message. NaCl auth also does not make any promises |
| 36 | regarding "truncated unforgeability." |
| 37 | |
| 38 | NaCl crypto_auth is currently an implementation of HMAC-SHA-512-256, i.e., the |
| 39 | first 256 bits of HMAC-SHA-512. HMAC-SHA-512-256 is conjectured to meet the |
| 40 | standard notion of unforgeability. |
| 41 | |
| 42 | These commands are interoperable with NaCl: https://nacl.cr.yp.to/auth.html |
| 43 | |
| 44 | ## EXAMPLES |
| 45 | |
| 46 | Authenticate a message using a 256-bit key, a new nacl box private key can be |
| 47 | used as the secret: |
| 48 | ''' |
| 49 | $ step crypto nacl auth digest auth.key |
| 50 | Please enter text to authenticate: ******** |
| 51 | 33c54aeb54077808fcfccadcd2f01971b120e314dffa61516b0738b74fdc8ff1 |
| 52 | |
| 53 | $ cat message.txt | step crypto nacl auth digest auth.key |
| 54 | 33c54aeb54077808fcfccadcd2f01971b120e314dffa61516b0738b74fdc8ff1 |
| 55 | ''' |
| 56 | |
| 57 | Verify the message with the hash: |
| 58 | ''' |
| 59 | $ step crypto nacl auth verify auth.key 33c54aeb54077808fcfccadcd2f01971b120e314dffa61516b0738b74fdc8ff1 |
| 60 | Please enter text to verify: ******** |
| 61 | ok |
| 62 | |
| 63 | $ cat message.txt | step crypto nacl auth verify auth.key 33c54aeb54077808fcfccadcd2f01971b120e314dffa61516b0738b74fdc8ff1 |
| 64 | ok |
| 65 | '''`, |
| 66 | Subcommands: cli.Commands{ |
| 67 | authDigestCommand(), |
| 68 | authVerifyCommand(), |
| 69 | }, |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | func authDigestCommand() cli.Command { |
| 74 | return cli.Command{ |
| 75 | Name: "digest", |
| 76 | Action: cli.ActionFunc(authDigestAction), |
| 77 | Usage: "generate a 32-byte digest for a message", |
| 78 | UsageText: "**step crypto nacl auth digest** <key-file>", |
| 79 | Description: `**step crypto nacl auth digest** creates a digest to authenticate the message |
| 80 | is read from STDIN using the given secret key. |
| 81 | |
| 82 | This command uses an implementation of NaCl's crypto_auth function. |
| 83 | |
| 84 | For examples, see **step help crypto nacl auth**.`, |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | func authVerifyCommand() cli.Command { |
| 89 | return cli.Command{ |
| 90 | Name: "verify", |
| 91 | Action: cli.ActionFunc(authVerifyAction), |
| 92 | Usage: "validate a digest for a message", |
| 93 | UsageText: "**step crypto nacl auth verify** <key-file> <digest>", |
| 94 | Description: `**step crypto nacl auth verify** checks that the digest is a valid authenticator |
| 95 | of the message is read from STDIN under the given secret key file. |
| 96 | |
| 97 | This command uses an implementation of NaCl's crypto_auth_verify function. |
| 98 | |
| 99 | For examples, see **step help crypto nacl auth**.`, |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | func authDigestAction(ctx *cli.Context) error { |
| 104 | if err := errs.NumberOfArguments(ctx, 1); err != nil { |
| 105 | return err |
| 106 | } |
| 107 | |
| 108 | keyFile := ctx.Args().Get(0) |
| 109 | |
| 110 | key, err := ioutil.ReadFile(keyFile) |
| 111 | if err != nil { |
| 112 | return errs.FileError(err, keyFile) |
| 113 | } else if len(key) != auth.KeySize { |
| 114 | return errors.Errorf("invalid key file: key size is not %d bytes", auth.KeySize) |
| 115 | } |
| 116 | |
| 117 | input, err := utils.ReadInput("Please enter text to digest") |
| 118 | if err != nil { |
| 119 | return errors.Wrap(err, "error reading input") |
| 120 | } |
| 121 | |
| 122 | var k [32]byte |
| 123 | copy(k[:], key) |
| 124 | |
| 125 | sum := auth.Sum(input, &k) |
| 126 | fmt.Println(hex.EncodeToString(sum[:])) |
| 127 | return nil |
| 128 | } |
| 129 | |
| 130 | func authVerifyAction(ctx *cli.Context) error { |
| 131 | if err := errs.NumberOfArguments(ctx, 2); err != nil { |
| 132 | return err |
| 133 | } |
| 134 | |
| 135 | args := ctx.Args() |
| 136 | keyFile, digest := args[0], args[1] |
| 137 | |
| 138 | key, err := ioutil.ReadFile(keyFile) |
| 139 | if err != nil { |
| 140 | return errs.FileError(err, keyFile) |
| 141 | } else if len(key) != auth.KeySize { |
| 142 | return errors.Errorf("invalid key file: key size is not %d bytes", auth.KeySize) |
| 143 | } |
| 144 | |
| 145 | sum, err := hex.DecodeString(digest) |
| 146 | if err != nil { |
| 147 | return errors.Wrap(err, "error decoding digest") |
| 148 | } |
| 149 | |
| 150 | input, err := utils.ReadInput("Please enter text to verify") |
| 151 | if err != nil { |
| 152 | return errors.Wrap(err, "error reading input") |
| 153 | } |
| 154 | |
| 155 | var k [32]byte |
| 156 | copy(k[:], key) |
| 157 | |
| 158 | if auth.Verify(sum, input, &k) { |
| 159 | fmt.Println("ok") |
| 160 | return nil |
| 161 | } |
| 162 | |
| 163 | return errors.New("fail") |
| 164 | } |