diff options
Diffstat (limited to 'cipher-chachapoly.c')
-rw-r--r-- | cipher-chachapoly.c | 31 |
1 files changed, 18 insertions, 13 deletions
diff --git a/cipher-chachapoly.c b/cipher-chachapoly.c index 251b94ec8..8665b41a3 100644 --- a/cipher-chachapoly.c +++ b/cipher-chachapoly.c | |||
@@ -14,7 +14,7 @@ | |||
14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
15 | */ | 15 | */ |
16 | 16 | ||
17 | /* $OpenBSD: cipher-chachapoly.c,v 1.4 2014/01/31 16:39:19 tedu Exp $ */ | 17 | /* $OpenBSD: cipher-chachapoly.c,v 1.6 2014/07/03 12:42:16 jsing Exp $ */ |
18 | 18 | ||
19 | #include "includes.h" | 19 | #include "includes.h" |
20 | 20 | ||
@@ -24,16 +24,18 @@ | |||
24 | #include <stdio.h> /* needed for misc.h */ | 24 | #include <stdio.h> /* needed for misc.h */ |
25 | 25 | ||
26 | #include "log.h" | 26 | #include "log.h" |
27 | #include "misc.h" | 27 | #include "sshbuf.h" |
28 | #include "ssherr.h" | ||
28 | #include "cipher-chachapoly.h" | 29 | #include "cipher-chachapoly.h" |
29 | 30 | ||
30 | void chachapoly_init(struct chachapoly_ctx *ctx, | 31 | int chachapoly_init(struct chachapoly_ctx *ctx, |
31 | const u_char *key, u_int keylen) | 32 | const u_char *key, u_int keylen) |
32 | { | 33 | { |
33 | if (keylen != (32 + 32)) /* 2 x 256 bit keys */ | 34 | if (keylen != (32 + 32)) /* 2 x 256 bit keys */ |
34 | fatal("%s: invalid keylen %u", __func__, keylen); | 35 | return SSH_ERR_INVALID_ARGUMENT; |
35 | chacha_keysetup(&ctx->main_ctx, key, 256); | 36 | chacha_keysetup(&ctx->main_ctx, key, 256); |
36 | chacha_keysetup(&ctx->header_ctx, key + 32, 256); | 37 | chacha_keysetup(&ctx->header_ctx, key + 32, 256); |
38 | return 0; | ||
37 | } | 39 | } |
38 | 40 | ||
39 | /* | 41 | /* |
@@ -52,33 +54,37 @@ chachapoly_crypt(struct chachapoly_ctx *ctx, u_int seqnr, u_char *dest, | |||
52 | u_char seqbuf[8]; | 54 | u_char seqbuf[8]; |
53 | const u_char one[8] = { 1, 0, 0, 0, 0, 0, 0, 0 }; /* NB little-endian */ | 55 | const u_char one[8] = { 1, 0, 0, 0, 0, 0, 0, 0 }; /* NB little-endian */ |
54 | u_char expected_tag[POLY1305_TAGLEN], poly_key[POLY1305_KEYLEN]; | 56 | u_char expected_tag[POLY1305_TAGLEN], poly_key[POLY1305_KEYLEN]; |
55 | int r = -1; | 57 | int r = SSH_ERR_INTERNAL_ERROR; |
56 | 58 | ||
57 | /* | 59 | /* |
58 | * Run ChaCha20 once to generate the Poly1305 key. The IV is the | 60 | * Run ChaCha20 once to generate the Poly1305 key. The IV is the |
59 | * packet sequence number. | 61 | * packet sequence number. |
60 | */ | 62 | */ |
61 | memset(poly_key, 0, sizeof(poly_key)); | 63 | memset(poly_key, 0, sizeof(poly_key)); |
62 | put_u64(seqbuf, seqnr); | 64 | POKE_U64(seqbuf, seqnr); |
63 | chacha_ivsetup(&ctx->main_ctx, seqbuf, NULL); | 65 | chacha_ivsetup(&ctx->main_ctx, seqbuf, NULL); |
64 | chacha_encrypt_bytes(&ctx->main_ctx, | 66 | chacha_encrypt_bytes(&ctx->main_ctx, |
65 | poly_key, poly_key, sizeof(poly_key)); | 67 | poly_key, poly_key, sizeof(poly_key)); |
66 | /* Set Chacha's block counter to 1 */ | ||
67 | chacha_ivsetup(&ctx->main_ctx, seqbuf, one); | ||
68 | 68 | ||
69 | /* If decrypting, check tag before anything else */ | 69 | /* If decrypting, check tag before anything else */ |
70 | if (!do_encrypt) { | 70 | if (!do_encrypt) { |
71 | const u_char *tag = src + aadlen + len; | 71 | const u_char *tag = src + aadlen + len; |
72 | 72 | ||
73 | poly1305_auth(expected_tag, src, aadlen + len, poly_key); | 73 | poly1305_auth(expected_tag, src, aadlen + len, poly_key); |
74 | if (timingsafe_bcmp(expected_tag, tag, POLY1305_TAGLEN) != 0) | 74 | if (timingsafe_bcmp(expected_tag, tag, POLY1305_TAGLEN) != 0) { |
75 | r = SSH_ERR_MAC_INVALID; | ||
75 | goto out; | 76 | goto out; |
77 | } | ||
76 | } | 78 | } |
79 | |||
77 | /* Crypt additional data */ | 80 | /* Crypt additional data */ |
78 | if (aadlen) { | 81 | if (aadlen) { |
79 | chacha_ivsetup(&ctx->header_ctx, seqbuf, NULL); | 82 | chacha_ivsetup(&ctx->header_ctx, seqbuf, NULL); |
80 | chacha_encrypt_bytes(&ctx->header_ctx, src, dest, aadlen); | 83 | chacha_encrypt_bytes(&ctx->header_ctx, src, dest, aadlen); |
81 | } | 84 | } |
85 | |||
86 | /* Set Chacha's block counter to 1 */ | ||
87 | chacha_ivsetup(&ctx->main_ctx, seqbuf, one); | ||
82 | chacha_encrypt_bytes(&ctx->main_ctx, src + aadlen, | 88 | chacha_encrypt_bytes(&ctx->main_ctx, src + aadlen, |
83 | dest + aadlen, len); | 89 | dest + aadlen, len); |
84 | 90 | ||
@@ -88,7 +94,6 @@ chachapoly_crypt(struct chachapoly_ctx *ctx, u_int seqnr, u_char *dest, | |||
88 | poly_key); | 94 | poly_key); |
89 | } | 95 | } |
90 | r = 0; | 96 | r = 0; |
91 | |||
92 | out: | 97 | out: |
93 | explicit_bzero(expected_tag, sizeof(expected_tag)); | 98 | explicit_bzero(expected_tag, sizeof(expected_tag)); |
94 | explicit_bzero(seqbuf, sizeof(seqbuf)); | 99 | explicit_bzero(seqbuf, sizeof(seqbuf)); |
@@ -104,11 +109,11 @@ chachapoly_get_length(struct chachapoly_ctx *ctx, | |||
104 | u_char buf[4], seqbuf[8]; | 109 | u_char buf[4], seqbuf[8]; |
105 | 110 | ||
106 | if (len < 4) | 111 | if (len < 4) |
107 | return -1; /* Insufficient length */ | 112 | return SSH_ERR_MESSAGE_INCOMPLETE; |
108 | put_u64(seqbuf, seqnr); | 113 | POKE_U64(seqbuf, seqnr); |
109 | chacha_ivsetup(&ctx->header_ctx, seqbuf, NULL); | 114 | chacha_ivsetup(&ctx->header_ctx, seqbuf, NULL); |
110 | chacha_encrypt_bytes(&ctx->header_ctx, cp, buf, 4); | 115 | chacha_encrypt_bytes(&ctx->header_ctx, cp, buf, 4); |
111 | *plenp = get_u32(buf); | 116 | *plenp = PEEK_U32(buf); |
112 | return 0; | 117 | return 0; |
113 | } | 118 | } |
114 | 119 | ||