Develop and Download Open Source Software

Browse Subversion Repository

Contents of /branches/ssh_chacha20poly1305/ttssh2/ttxssh/cipher-chachapoly-libcrypto.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 9212 - (show annotations) (download) (as text)
Sat Apr 17 14:54:57 2021 UTC (2 years, 11 months ago) by nmaya
File MIME type: text/x-csrc
File size: 5900 byte(s)
OpenSSH-8.5p1 から cipher-chachapoly, poly1305 のファイルを追加
1 /* Imported from OpenSSH-8.5p1, TeraTerm Project */
2
3 /*
4 * Copyright (c) 2013 Damien Miller <djm@mindrot.org>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 /* $OpenBSD: cipher-chachapoly-libcrypto.c,v 1.1 2020/04/03 04:32:21 djm Exp $ */
20
21 #include <sys/types.h>
22
23 #include <stdarg.h> /* needed for log.h */
24 #include <string.h>
25 #include <stdio.h> /* needed for misc.h */
26 #include <windows.h>
27
28 #include <openssl/evp.h>
29
30 #include "ssherr.h"
31 #include "cipher-chachapoly.h"
32
33
34 // import from sshbuf.h
35 /* $OpenBSD: sshbuf.h,v 1.23 2020/06/22 05:54:10 djm Exp $ */
36 #define PEEK_U32(p) \
37 (((u_int32_t)(((const u_char *)(p))[0]) << 24) | \
38 ((u_int32_t)(((const u_char *)(p))[1]) << 16) | \
39 ((u_int32_t)(((const u_char *)(p))[2]) << 8) | \
40 (u_int32_t)(((const u_char *)(p))[3]))
41 #define POKE_U64(p, v) \
42 do { \
43 const u_int64_t __v = (v); \
44 ((u_char *)(p))[0] = (__v >> 56) & 0xff; \
45 ((u_char *)(p))[1] = (__v >> 48) & 0xff; \
46 ((u_char *)(p))[2] = (__v >> 40) & 0xff; \
47 ((u_char *)(p))[3] = (__v >> 32) & 0xff; \
48 ((u_char *)(p))[4] = (__v >> 24) & 0xff; \
49 ((u_char *)(p))[5] = (__v >> 16) & 0xff; \
50 ((u_char *)(p))[6] = (__v >> 8) & 0xff; \
51 ((u_char *)(p))[7] = __v & 0xff; \
52 } while (0)
53
54
55 // import from openbsd-compat/timingsafe_bcmp.c
56 /* $OpenBSD: timingsafe_bcmp.c,v 1.1 2010/09/24 13:33:00 matthew Exp $ */
57 int
58 timingsafe_bcmp(const void* b1, const void* b2, size_t n)
59 {
60 const unsigned char* p1 = b1, * p2 = b2;
61 int ret = 0;
62
63 for (; n > 0; n--)
64 ret |= *p1++ ^ *p2++;
65 return (ret != 0);
66 }
67
68
69 struct chachapoly_ctx {
70 EVP_CIPHER_CTX *main_evp, *header_evp;
71 };
72
73 struct chachapoly_ctx *
74 chachapoly_new(const u_char *key, u_int keylen)
75 {
76 struct chachapoly_ctx *ctx;
77
78 if (keylen != (32 + 32)) /* 2 x 256 bit keys */
79 return NULL;
80 if ((ctx = calloc(1, sizeof(*ctx))) == NULL)
81 return NULL;
82 if ((ctx->main_evp = EVP_CIPHER_CTX_new()) == NULL ||
83 (ctx->header_evp = EVP_CIPHER_CTX_new()) == NULL)
84 goto fail;
85 if (!EVP_CipherInit(ctx->main_evp, EVP_chacha20(), key, NULL, 1))
86 goto fail;
87 if (!EVP_CipherInit(ctx->header_evp, EVP_chacha20(), key + 32, NULL, 1))
88 goto fail;
89 if (EVP_CIPHER_CTX_iv_length(ctx->header_evp) != 16)
90 goto fail;
91 return ctx;
92 fail:
93 chachapoly_free(ctx);
94 return NULL;
95 }
96
97 void
98 chachapoly_free(struct chachapoly_ctx *cpctx)
99 {
100 if (cpctx == NULL)
101 return;
102 EVP_CIPHER_CTX_free(cpctx->main_evp);
103 EVP_CIPHER_CTX_free(cpctx->header_evp);
104 SecureZeroMemory(cpctx, sizeof(*cpctx));
105 }
106
107 /*
108 * chachapoly_crypt() operates as following:
109 * En/decrypt with header key 'aadlen' bytes from 'src', storing result
110 * to 'dest'. The ciphertext here is treated as additional authenticated
111 * data for MAC calculation.
112 * En/decrypt 'len' bytes at offset 'aadlen' from 'src' to 'dest'. Use
113 * POLY1305_TAGLEN bytes at offset 'len'+'aadlen' as the authentication
114 * tag. This tag is written on encryption and verified on decryption.
115 */
116 int
117 chachapoly_crypt(struct chachapoly_ctx *ctx, u_int seqnr, u_char *dest,
118 const u_char *src, u_int len, u_int aadlen, u_int authlen, int do_encrypt)
119 {
120 u_char seqbuf[16]; /* layout: u64 counter || u64 seqno */
121 int r = SSH_ERR_INTERNAL_ERROR;
122 u_char expected_tag[POLY1305_TAGLEN], poly_key[POLY1305_KEYLEN];
123
124 /*
125 * Run ChaCha20 once to generate the Poly1305 key. The IV is the
126 * packet sequence number.
127 */
128 memset(seqbuf, 0, sizeof(seqbuf));
129 POKE_U64(seqbuf + 8, seqnr);
130 memset(poly_key, 0, sizeof(poly_key));
131 if (!EVP_CipherInit(ctx->main_evp, NULL, NULL, seqbuf, 1) ||
132 EVP_Cipher(ctx->main_evp, poly_key,
133 poly_key, sizeof(poly_key)) < 0) {
134 r = SSH_ERR_LIBCRYPTO_ERROR;
135 goto out;
136 }
137
138 /* If decrypting, check tag before anything else */
139 if (!do_encrypt) {
140 const u_char *tag = src + aadlen + len;
141
142 poly1305_auth(expected_tag, src, aadlen + len, poly_key);
143 if (timingsafe_bcmp(expected_tag, tag, POLY1305_TAGLEN) != 0) {
144 r = SSH_ERR_MAC_INVALID;
145 goto out;
146 }
147 }
148
149 /* Crypt additional data */
150 if (aadlen) {
151 if (!EVP_CipherInit(ctx->header_evp, NULL, NULL, seqbuf, 1) ||
152 EVP_Cipher(ctx->header_evp, dest, src, aadlen) < 0) {
153 r = SSH_ERR_LIBCRYPTO_ERROR;
154 goto out;
155 }
156 }
157
158 /* Set Chacha's block counter to 1 */
159 seqbuf[0] = 1;
160 if (!EVP_CipherInit(ctx->main_evp, NULL, NULL, seqbuf, 1) ||
161 EVP_Cipher(ctx->main_evp, dest + aadlen, src + aadlen, len) < 0) {
162 r = SSH_ERR_LIBCRYPTO_ERROR;
163 goto out;
164 }
165
166 /* If encrypting, calculate and append tag */
167 if (do_encrypt) {
168 poly1305_auth(dest + aadlen + len, dest, aadlen + len,
169 poly_key);
170 }
171 r = 0;
172 out:
173 SecureZeroMemory(expected_tag, sizeof(expected_tag));
174 SecureZeroMemory(seqbuf, sizeof(seqbuf));
175 SecureZeroMemory(poly_key, sizeof(poly_key));
176 return r;
177 }
178
179 /* Decrypt and extract the encrypted packet length */
180 int
181 chachapoly_get_length(struct chachapoly_ctx *ctx,
182 u_int *plenp, u_int seqnr, const u_char *cp, u_int len)
183 {
184 u_char buf[4], seqbuf[16];
185
186 if (len < 4)
187 return SSH_ERR_MESSAGE_INCOMPLETE;
188 memset(seqbuf, 0, sizeof(seqbuf));
189 POKE_U64(seqbuf + 8, seqnr);
190 if (!EVP_CipherInit(ctx->header_evp, NULL, NULL, seqbuf, 0))
191 return SSH_ERR_LIBCRYPTO_ERROR;
192 if (EVP_Cipher(ctx->header_evp, buf, (u_char *)cp, sizeof(buf)) < 0)
193 return SSH_ERR_LIBCRYPTO_ERROR;
194 *plenp = PEEK_U32(buf);
195 return 0;
196 }

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26