Skip to content

Commit 824f0c2

Browse files
authored
Merge pull request #282 from smallstep/nacl-string-format
Add support for the string: prefix
2 parents 90dea5d + 71ff471 commit 824f0c2

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

Diff for: command/crypto/nacl/box.go

+6
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ receiver} sets are different. This is true even if the sets overlap. For
4343
example, a sender can use the same nonce for two different messages if the
4444
messages are sent to two different public keys.
4545
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+
4652
NaCl crypto_box is not meant to provide non-repudiation. On the contrary: they
4753
guarantee repudiability. A receiver can freely modify a boxed message, and
4854
therefore cannot convince third parties that this particular message came from

Diff for: command/crypto/nacl/nacl.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,18 @@ var b64Encoder = base64.RawURLEncoding
4949
// it will decode the rest using the base64 standard encoding.
5050
func decodeNonce(in string) ([]byte, error) {
5151
nonce := []byte(in)
52-
if strings.HasPrefix(in, "base64:") {
52+
switch {
53+
case strings.HasPrefix(in, "string:"):
54+
return nonce[7:], nil
55+
case strings.HasPrefix(in, "base64:"):
5356
input := nonce[7:]
5457
nonce = make([]byte, base64.StdEncoding.DecodedLen(len(input)))
5558
n, err := base64.StdEncoding.Decode(nonce, input)
5659
if err != nil {
5760
return nil, errors.Wrap(err, "error decoding base64 nonce")
5861
}
5962
return nonce[:n], nil
63+
default:
64+
return nonce, nil
6065
}
61-
return nonce, nil
6266
}

Diff for: command/crypto/nacl/secretbox.go

+6
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ uniqueness of nonces—for example, by using nonce 1 for the first message, nonc
3232
2 for the second message, etc. Nonces are long enough that randomly generated
3333
nonces have negligible risk of collision.
3434
35+
By default nonces are alphanumeric, but it's possible to use binary nonces using
36+
the prefix 'base64:' and the standard base64 encoding of the data, e.g.
37+
'base64:081D3pFPBkwx1bURR9HQjiYbAUxigo0Z'. The prefix 'string:' is also
38+
accepted, but it will be equivalent to not using a prefix. Nonces cannot be
39+
longer than 24 bytes.
40+
3541
NaCl crypto_secretbox is crypto_secretbox_xsalsa20poly1305, a particular
3642
combination of Salsa20 and Poly1305 specified in "Cryptography in NaCl". This
3743
function is conjectured to meet the standard notions of privacy and

0 commit comments

Comments
 (0)