Merge branch 'vendor/OPENSSH'
[dragonfly.git] / crypto / openssh / cipher.c
1 /* $OpenBSD: cipher.c,v 1.101 2015/12/10 17:08:40 mmcc Exp $ */
2 /*
3  * Author: Tatu Ylonen <ylo@cs.hut.fi>
4  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5  *                    All rights reserved
6  *
7  * As far as I am concerned, the code I have written for this software
8  * can be used freely for any purpose.  Any derived versions of this
9  * software must be clearly marked as such, and if the derived work is
10  * incompatible with the protocol description in the RFC file, it must be
11  * called by a name other than "ssh" or "Secure Shell".
12  *
13  *
14  * Copyright (c) 1999 Niels Provos.  All rights reserved.
15  * Copyright (c) 1999, 2000 Markus Friedl.  All rights reserved.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions
19  * are met:
20  * 1. Redistributions of source code must retain the above copyright
21  *    notice, this list of conditions and the following disclaimer.
22  * 2. Redistributions in binary form must reproduce the above copyright
23  *    notice, this list of conditions and the following disclaimer in the
24  *    documentation and/or other materials provided with the distribution.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
27  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
30  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  */
37
38 #include "includes.h"
39
40 #include <sys/types.h>
41
42 #include <string.h>
43 #include <stdarg.h>
44 #include <stdio.h>
45
46 #include "cipher.h"
47 #include "misc.h"
48 #include "sshbuf.h"
49 #include "ssherr.h"
50 #include "digest.h"
51
52 #include "openbsd-compat/openssl-compat.h"
53
54 #ifdef WITH_SSH1
55 extern const EVP_CIPHER *evp_ssh1_bf(void);
56 extern const EVP_CIPHER *evp_ssh1_3des(void);
57 extern int ssh1_3des_iv(EVP_CIPHER_CTX *, int, u_char *, int);
58 #endif
59
60 struct sshcipher {
61         char    *name;
62         int     number;         /* for ssh1 only */
63         u_int   block_size;
64         u_int   key_len;
65         u_int   iv_len;         /* defaults to block_size */
66         u_int   auth_len;
67         u_int   discard_len;
68         u_int   flags;
69 #define CFLAG_CBC               (1<<0)
70 #define CFLAG_CHACHAPOLY        (1<<1)
71 #define CFLAG_AESCTR            (1<<2)
72 #define CFLAG_NONE              (1<<3)
73 #ifdef WITH_OPENSSL
74         const EVP_CIPHER        *(*evptype)(void);
75 #else
76         void    *ignored;
77 #endif
78 };
79
80 static const struct sshcipher ciphers[] = {
81 #ifdef WITH_SSH1
82         { "des",        SSH_CIPHER_DES, 8, 8, 0, 0, 0, 1, EVP_des_cbc },
83         { "3des",       SSH_CIPHER_3DES, 8, 16, 0, 0, 0, 1, evp_ssh1_3des },
84 # ifndef OPENSSL_NO_BF
85         { "blowfish",   SSH_CIPHER_BLOWFISH, 8, 32, 0, 0, 0, 1, evp_ssh1_bf },
86 # endif /* OPENSSL_NO_BF */
87 #endif /* WITH_SSH1 */
88 #ifdef WITH_OPENSSL
89         { "none",       SSH_CIPHER_NONE, 8, 0, 0, 0, 0, 0, EVP_enc_null },
90         { "3des-cbc",   SSH_CIPHER_SSH2, 8, 24, 0, 0, 0, 1, EVP_des_ede3_cbc },
91 # ifndef OPENSSL_NO_BF
92         { "blowfish-cbc",
93                         SSH_CIPHER_SSH2, 8, 16, 0, 0, 0, 1, EVP_bf_cbc },
94 # endif /* OPENSSL_NO_BF */
95 # ifndef OPENSSL_NO_CAST
96         { "cast128-cbc",
97                         SSH_CIPHER_SSH2, 8, 16, 0, 0, 0, 1, EVP_cast5_cbc },
98 # endif /* OPENSSL_NO_CAST */
99 # ifndef OPENSSL_NO_RC4
100         { "arcfour",    SSH_CIPHER_SSH2, 8, 16, 0, 0, 0, 0, EVP_rc4 },
101         { "arcfour128", SSH_CIPHER_SSH2, 8, 16, 0, 0, 1536, 0, EVP_rc4 },
102         { "arcfour256", SSH_CIPHER_SSH2, 8, 32, 0, 0, 1536, 0, EVP_rc4 },
103 # endif /* OPENSSL_NO_RC4 */
104         { "aes128-cbc", SSH_CIPHER_SSH2, 16, 16, 0, 0, 0, 1, EVP_aes_128_cbc },
105         { "aes192-cbc", SSH_CIPHER_SSH2, 16, 24, 0, 0, 0, 1, EVP_aes_192_cbc },
106         { "aes256-cbc", SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, 1, EVP_aes_256_cbc },
107         { "rijndael-cbc@lysator.liu.se",
108                         SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, 1, EVP_aes_256_cbc },
109         { "aes128-ctr", SSH_CIPHER_SSH2, 16, 16, 0, 0, 0, 0, EVP_aes_128_ctr },
110         { "aes192-ctr", SSH_CIPHER_SSH2, 16, 24, 0, 0, 0, 0, EVP_aes_192_ctr },
111         { "aes256-ctr", SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, 0, EVP_aes_256_ctr },
112 # ifdef OPENSSL_HAVE_EVPGCM
113         { "aes128-gcm@openssh.com",
114                         SSH_CIPHER_SSH2, 16, 16, 12, 16, 0, 0, EVP_aes_128_gcm },
115         { "aes256-gcm@openssh.com",
116                         SSH_CIPHER_SSH2, 16, 32, 12, 16, 0, 0, EVP_aes_256_gcm },
117 # endif /* OPENSSL_HAVE_EVPGCM */
118 #else /* WITH_OPENSSL */
119         { "aes128-ctr", SSH_CIPHER_SSH2, 16, 16, 0, 0, 0, CFLAG_AESCTR, NULL },
120         { "aes192-ctr", SSH_CIPHER_SSH2, 16, 24, 0, 0, 0, CFLAG_AESCTR, NULL },
121         { "aes256-ctr", SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, CFLAG_AESCTR, NULL },
122         { "none",       SSH_CIPHER_NONE, 8, 0, 0, 0, 0, CFLAG_NONE, NULL },
123 #endif /* WITH_OPENSSL */
124         { "chacha20-poly1305@openssh.com",
125                         SSH_CIPHER_SSH2, 8, 64, 0, 16, 0, CFLAG_CHACHAPOLY, NULL },
126
127         { NULL,         SSH_CIPHER_INVALID, 0, 0, 0, 0, 0, 0, NULL }
128 };
129
130 /*--*/
131
132 /* Returns a comma-separated list of supported ciphers. */
133 char *
134 cipher_alg_list(char sep, int auth_only)
135 {
136         char *tmp, *ret = NULL;
137         size_t nlen, rlen = 0;
138         const struct sshcipher *c;
139
140         for (c = ciphers; c->name != NULL; c++) {
141                 if (c->number != SSH_CIPHER_SSH2)
142                         continue;
143                 if (auth_only && c->auth_len == 0)
144                         continue;
145                 if (ret != NULL)
146                         ret[rlen++] = sep;
147                 nlen = strlen(c->name);
148                 if ((tmp = realloc(ret, rlen + nlen + 2)) == NULL) {
149                         free(ret);
150                         return NULL;
151                 }
152                 ret = tmp;
153                 memcpy(ret + rlen, c->name, nlen + 1);
154                 rlen += nlen;
155         }
156         return ret;
157 }
158
159 u_int
160 cipher_blocksize(const struct sshcipher *c)
161 {
162         return (c->block_size);
163 }
164
165 u_int
166 cipher_keylen(const struct sshcipher *c)
167 {
168         return (c->key_len);
169 }
170
171 u_int
172 cipher_seclen(const struct sshcipher *c)
173 {
174         if (strcmp("3des-cbc", c->name) == 0)
175                 return 14;
176         return cipher_keylen(c);
177 }
178
179 u_int
180 cipher_authlen(const struct sshcipher *c)
181 {
182         return (c->auth_len);
183 }
184
185 u_int
186 cipher_ivlen(const struct sshcipher *c)
187 {
188         /*
189          * Default is cipher block size, except for chacha20+poly1305 that
190          * needs no IV. XXX make iv_len == -1 default?
191          */
192         return (c->iv_len != 0 || (c->flags & CFLAG_CHACHAPOLY) != 0) ?
193             c->iv_len : c->block_size;
194 }
195
196 u_int
197 cipher_get_number(const struct sshcipher *c)
198 {
199         return (c->number);
200 }
201
202 u_int
203 cipher_is_cbc(const struct sshcipher *c)
204 {
205         return (c->flags & CFLAG_CBC) != 0;
206 }
207
208 u_int
209 cipher_mask_ssh1(int client)
210 {
211         u_int mask = 0;
212         mask |= 1 << SSH_CIPHER_3DES;           /* Mandatory */
213         mask |= 1 << SSH_CIPHER_BLOWFISH;
214         if (client) {
215                 mask |= 1 << SSH_CIPHER_DES;
216         }
217         return mask;
218 }
219
220 const struct sshcipher *
221 cipher_by_name(const char *name)
222 {
223         const struct sshcipher *c;
224         for (c = ciphers; c->name != NULL; c++)
225                 if (strcmp(c->name, name) == 0)
226                         return c;
227         return NULL;
228 }
229
230 const struct sshcipher *
231 cipher_by_number(int id)
232 {
233         const struct sshcipher *c;
234         for (c = ciphers; c->name != NULL; c++)
235                 if (c->number == id)
236                         return c;
237         return NULL;
238 }
239
240 #define CIPHER_SEP      ","
241 int
242 ciphers_valid(const char *names)
243 {
244         const struct sshcipher *c;
245         char *cipher_list, *cp;
246         char *p;
247
248         if (names == NULL || strcmp(names, "") == 0)
249                 return 0;
250         if ((cipher_list = cp = strdup(names)) == NULL)
251                 return 0;
252         for ((p = strsep(&cp, CIPHER_SEP)); p && *p != '\0';
253             (p = strsep(&cp, CIPHER_SEP))) {
254                 c = cipher_by_name(p);
255                 if (c == NULL || c->number != SSH_CIPHER_SSH2) {
256                         free(cipher_list);
257                         return 0;
258                 }
259         }
260         free(cipher_list);
261         return 1;
262 }
263
264 /*
265  * Parses the name of the cipher.  Returns the number of the corresponding
266  * cipher, or -1 on error.
267  */
268
269 int
270 cipher_number(const char *name)
271 {
272         const struct sshcipher *c;
273         if (name == NULL)
274                 return -1;
275         for (c = ciphers; c->name != NULL; c++)
276                 if (strcasecmp(c->name, name) == 0)
277                         return c->number;
278         return -1;
279 }
280
281 char *
282 cipher_name(int id)
283 {
284         const struct sshcipher *c = cipher_by_number(id);
285         return (c==NULL) ? "<unknown>" : c->name;
286 }
287
288 const char *
289 cipher_warning_message(const struct sshcipher_ctx *cc)
290 {
291         if (cc == NULL || cc->cipher == NULL)
292                 return NULL;
293         if (cc->cipher->number == SSH_CIPHER_DES)
294                 return "use of DES is strongly discouraged due to "
295                     "cryptographic weaknesses";
296         return NULL;
297 }
298
299 int
300 cipher_init(struct sshcipher_ctx *cc, const struct sshcipher *cipher,
301     const u_char *key, u_int keylen, const u_char *iv, u_int ivlen,
302     int do_encrypt)
303 {
304 #ifdef WITH_OPENSSL
305         int ret = SSH_ERR_INTERNAL_ERROR;
306         const EVP_CIPHER *type;
307         int klen;
308         u_char *junk, *discard;
309
310         if (cipher->number == SSH_CIPHER_DES) {
311                 if (keylen > 8)
312                         keylen = 8;
313         }
314 #endif
315         cc->plaintext = (cipher->number == SSH_CIPHER_NONE);
316         cc->encrypt = do_encrypt;
317
318         if (keylen < cipher->key_len ||
319             (iv != NULL && ivlen < cipher_ivlen(cipher)))
320                 return SSH_ERR_INVALID_ARGUMENT;
321
322         cc->cipher = cipher;
323         if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
324                 return chachapoly_init(&cc->cp_ctx, key, keylen);
325         }
326 #ifndef WITH_OPENSSL
327         if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
328                 aesctr_keysetup(&cc->ac_ctx, key, 8 * keylen, 8 * ivlen);
329                 aesctr_ivsetup(&cc->ac_ctx, iv);
330                 return 0;
331         }
332         if ((cc->cipher->flags & CFLAG_NONE) != 0)
333                 return 0;
334         return SSH_ERR_INVALID_ARGUMENT;
335 #else
336         type = (*cipher->evptype)();
337         EVP_CIPHER_CTX_init(&cc->evp);
338         if (EVP_CipherInit(&cc->evp, type, NULL, (u_char *)iv,
339             (do_encrypt == CIPHER_ENCRYPT)) == 0) {
340                 ret = SSH_ERR_LIBCRYPTO_ERROR;
341                 goto bad;
342         }
343         if (cipher_authlen(cipher) &&
344             !EVP_CIPHER_CTX_ctrl(&cc->evp, EVP_CTRL_GCM_SET_IV_FIXED,
345             -1, (u_char *)iv)) {
346                 ret = SSH_ERR_LIBCRYPTO_ERROR;
347                 goto bad;
348         }
349         klen = EVP_CIPHER_CTX_key_length(&cc->evp);
350         if (klen > 0 && keylen != (u_int)klen) {
351                 if (EVP_CIPHER_CTX_set_key_length(&cc->evp, keylen) == 0) {
352                         ret = SSH_ERR_LIBCRYPTO_ERROR;
353                         goto bad;
354                 }
355         }
356         if (EVP_CipherInit(&cc->evp, NULL, (u_char *)key, NULL, -1) == 0) {
357                 ret = SSH_ERR_LIBCRYPTO_ERROR;
358                 goto bad;
359         }
360
361         if (cipher->discard_len > 0) {
362                 if ((junk = malloc(cipher->discard_len)) == NULL ||
363                     (discard = malloc(cipher->discard_len)) == NULL) {
364                         free(junk);
365                         ret = SSH_ERR_ALLOC_FAIL;
366                         goto bad;
367                 }
368                 ret = EVP_Cipher(&cc->evp, discard, junk, cipher->discard_len);
369                 explicit_bzero(discard, cipher->discard_len);
370                 free(junk);
371                 free(discard);
372                 if (ret != 1) {
373                         ret = SSH_ERR_LIBCRYPTO_ERROR;
374  bad:
375                         EVP_CIPHER_CTX_cleanup(&cc->evp);
376                         return ret;
377                 }
378         }
379 #endif
380         return 0;
381 }
382
383 /*
384  * cipher_crypt() operates as following:
385  * Copy 'aadlen' bytes (without en/decryption) from 'src' to 'dest'.
386  * Theses bytes are treated as additional authenticated data for
387  * authenticated encryption modes.
388  * En/Decrypt 'len' bytes at offset 'aadlen' from 'src' to 'dest'.
389  * Use 'authlen' bytes at offset 'len'+'aadlen' as the authentication tag.
390  * This tag is written on encryption and verified on decryption.
391  * Both 'aadlen' and 'authlen' can be set to 0.
392  */
393 int
394 cipher_crypt(struct sshcipher_ctx *cc, u_int seqnr, u_char *dest,
395    const u_char *src, u_int len, u_int aadlen, u_int authlen)
396 {
397         if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
398                 return chachapoly_crypt(&cc->cp_ctx, seqnr, dest, src,
399                     len, aadlen, authlen, cc->encrypt);
400         }
401 #ifndef WITH_OPENSSL
402         if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
403                 if (aadlen)
404                         memcpy(dest, src, aadlen);
405                 aesctr_encrypt_bytes(&cc->ac_ctx, src + aadlen,
406                     dest + aadlen, len);
407                 return 0;
408         }
409         if ((cc->cipher->flags & CFLAG_NONE) != 0) {
410                 memcpy(dest, src, aadlen + len);
411                 return 0;
412         }
413         return SSH_ERR_INVALID_ARGUMENT;
414 #else
415         if (authlen) {
416                 u_char lastiv[1];
417
418                 if (authlen != cipher_authlen(cc->cipher))
419                         return SSH_ERR_INVALID_ARGUMENT;
420                 /* increment IV */
421                 if (!EVP_CIPHER_CTX_ctrl(&cc->evp, EVP_CTRL_GCM_IV_GEN,
422                     1, lastiv))
423                         return SSH_ERR_LIBCRYPTO_ERROR;
424                 /* set tag on decyption */
425                 if (!cc->encrypt &&
426                     !EVP_CIPHER_CTX_ctrl(&cc->evp, EVP_CTRL_GCM_SET_TAG,
427                     authlen, (u_char *)src + aadlen + len))
428                         return SSH_ERR_LIBCRYPTO_ERROR;
429         }
430         if (aadlen) {
431                 if (authlen &&
432                     EVP_Cipher(&cc->evp, NULL, (u_char *)src, aadlen) < 0)
433                         return SSH_ERR_LIBCRYPTO_ERROR;
434                 memcpy(dest, src, aadlen);
435         }
436         if (len % cc->cipher->block_size)
437                 return SSH_ERR_INVALID_ARGUMENT;
438         if (EVP_Cipher(&cc->evp, dest + aadlen, (u_char *)src + aadlen,
439             len) < 0)
440                 return SSH_ERR_LIBCRYPTO_ERROR;
441         if (authlen) {
442                 /* compute tag (on encrypt) or verify tag (on decrypt) */
443                 if (EVP_Cipher(&cc->evp, NULL, NULL, 0) < 0)
444                         return cc->encrypt ?
445                             SSH_ERR_LIBCRYPTO_ERROR : SSH_ERR_MAC_INVALID;
446                 if (cc->encrypt &&
447                     !EVP_CIPHER_CTX_ctrl(&cc->evp, EVP_CTRL_GCM_GET_TAG,
448                     authlen, dest + aadlen + len))
449                         return SSH_ERR_LIBCRYPTO_ERROR;
450         }
451         return 0;
452 #endif
453 }
454
455 /* Extract the packet length, including any decryption necessary beforehand */
456 int
457 cipher_get_length(struct sshcipher_ctx *cc, u_int *plenp, u_int seqnr,
458     const u_char *cp, u_int len)
459 {
460         if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
461                 return chachapoly_get_length(&cc->cp_ctx, plenp, seqnr,
462                     cp, len);
463         if (len < 4)
464                 return SSH_ERR_MESSAGE_INCOMPLETE;
465         *plenp = get_u32(cp);
466         return 0;
467 }
468
469 int
470 cipher_cleanup(struct sshcipher_ctx *cc)
471 {
472         if (cc == NULL || cc->cipher == NULL)
473                 return 0;
474         if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
475                 explicit_bzero(&cc->cp_ctx, sizeof(cc->cp_ctx));
476         else if ((cc->cipher->flags & CFLAG_AESCTR) != 0)
477                 explicit_bzero(&cc->ac_ctx, sizeof(cc->ac_ctx));
478 #ifdef WITH_OPENSSL
479         else if (EVP_CIPHER_CTX_cleanup(&cc->evp) == 0)
480                 return SSH_ERR_LIBCRYPTO_ERROR;
481 #endif
482         return 0;
483 }
484
485 /*
486  * Selects the cipher, and keys if by computing the MD5 checksum of the
487  * passphrase and using the resulting 16 bytes as the key.
488  */
489 int
490 cipher_set_key_string(struct sshcipher_ctx *cc, const struct sshcipher *cipher,
491     const char *passphrase, int do_encrypt)
492 {
493         u_char digest[16];
494         int r = SSH_ERR_INTERNAL_ERROR;
495
496         if ((r = ssh_digest_memory(SSH_DIGEST_MD5,
497             passphrase, strlen(passphrase),
498             digest, sizeof(digest))) != 0)
499                 goto out;
500
501         r = cipher_init(cc, cipher, digest, 16, NULL, 0, do_encrypt);
502  out:
503         explicit_bzero(digest, sizeof(digest));
504         return r;
505 }
506
507 /*
508  * Exports an IV from the sshcipher_ctx required to export the key
509  * state back from the unprivileged child to the privileged parent
510  * process.
511  */
512 int
513 cipher_get_keyiv_len(const struct sshcipher_ctx *cc)
514 {
515         const struct sshcipher *c = cc->cipher;
516         int ivlen = 0;
517
518         if (c->number == SSH_CIPHER_3DES)
519                 ivlen = 24;
520         else if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
521                 ivlen = 0;
522         else if ((cc->cipher->flags & CFLAG_AESCTR) != 0)
523                 ivlen = sizeof(cc->ac_ctx.ctr);
524 #ifdef WITH_OPENSSL
525         else
526                 ivlen = EVP_CIPHER_CTX_iv_length(&cc->evp);
527 #endif /* WITH_OPENSSL */
528         return (ivlen);
529 }
530
531 int
532 cipher_get_keyiv(struct sshcipher_ctx *cc, u_char *iv, u_int len)
533 {
534         const struct sshcipher *c = cc->cipher;
535 #ifdef WITH_OPENSSL
536         int evplen;
537 #endif
538
539         if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
540                 if (len != 0)
541                         return SSH_ERR_INVALID_ARGUMENT;
542                 return 0;
543         }
544         if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
545                 if (len != sizeof(cc->ac_ctx.ctr))
546                         return SSH_ERR_INVALID_ARGUMENT;
547                 memcpy(iv, cc->ac_ctx.ctr, len);
548                 return 0;
549         }
550         if ((cc->cipher->flags & CFLAG_NONE) != 0)
551                 return 0;
552
553         switch (c->number) {
554 #ifdef WITH_OPENSSL
555         case SSH_CIPHER_SSH2:
556         case SSH_CIPHER_DES:
557         case SSH_CIPHER_BLOWFISH:
558                 evplen = EVP_CIPHER_CTX_iv_length(&cc->evp);
559                 if (evplen == 0)
560                         return 0;
561                 else if (evplen < 0)
562                         return SSH_ERR_LIBCRYPTO_ERROR;
563                 if ((u_int)evplen != len)
564                         return SSH_ERR_INVALID_ARGUMENT;
565 #ifndef OPENSSL_HAVE_EVPCTR
566                 if (c->evptype == evp_aes_128_ctr)
567                         ssh_aes_ctr_iv(&cc->evp, 0, iv, len);
568                 else
569 #endif
570                 if (cipher_authlen(c)) {
571                         if (!EVP_CIPHER_CTX_ctrl(&cc->evp, EVP_CTRL_GCM_IV_GEN,
572                            len, iv))
573                                return SSH_ERR_LIBCRYPTO_ERROR;
574                 } else
575                         memcpy(iv, cc->evp.iv, len);
576                 break;
577 #endif
578 #ifdef WITH_SSH1
579         case SSH_CIPHER_3DES:
580                 return ssh1_3des_iv(&cc->evp, 0, iv, 24);
581 #endif
582         default:
583                 return SSH_ERR_INVALID_ARGUMENT;
584         }
585         return 0;
586 }
587
588 int
589 cipher_set_keyiv(struct sshcipher_ctx *cc, const u_char *iv)
590 {
591         const struct sshcipher *c = cc->cipher;
592 #ifdef WITH_OPENSSL
593         int evplen = 0;
594 #endif
595
596         if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
597                 return 0;
598         if ((cc->cipher->flags & CFLAG_NONE) != 0)
599                 return 0;
600
601         switch (c->number) {
602 #ifdef WITH_OPENSSL
603         case SSH_CIPHER_SSH2:
604         case SSH_CIPHER_DES:
605         case SSH_CIPHER_BLOWFISH:
606                 evplen = EVP_CIPHER_CTX_iv_length(&cc->evp);
607                 if (evplen <= 0)
608                         return SSH_ERR_LIBCRYPTO_ERROR;
609                 if (cipher_authlen(c)) {
610                         /* XXX iv arg is const, but EVP_CIPHER_CTX_ctrl isn't */
611                         if (!EVP_CIPHER_CTX_ctrl(&cc->evp,
612                             EVP_CTRL_GCM_SET_IV_FIXED, -1, (void *)iv))
613                                 return SSH_ERR_LIBCRYPTO_ERROR;
614                 } else
615                         memcpy(cc->evp.iv, iv, evplen);
616                 break;
617 #endif
618 #ifdef WITH_SSH1
619         case SSH_CIPHER_3DES:
620                 return ssh1_3des_iv(&cc->evp, 1, (u_char *)iv, 24);
621 #endif
622         default:
623                 return SSH_ERR_INVALID_ARGUMENT;
624         }
625         return 0;
626 }
627
628 #ifdef WITH_OPENSSL
629 #define EVP_X_STATE(evp)        (evp).cipher_data
630 #define EVP_X_STATE_LEN(evp)    (evp).cipher->ctx_size
631 #endif
632
633 int
634 cipher_get_keycontext(const struct sshcipher_ctx *cc, u_char *dat)
635 {
636 #if defined(WITH_OPENSSL) && !defined(OPENSSL_NO_RC4)
637         const struct sshcipher *c = cc->cipher;
638         int plen = 0;
639
640         if (c->evptype == EVP_rc4) {
641                 plen = EVP_X_STATE_LEN(cc->evp);
642                 if (dat == NULL)
643                         return (plen);
644                 memcpy(dat, EVP_X_STATE(cc->evp), plen);
645         }
646         return (plen);
647 #else
648         return 0;
649 #endif
650 }
651
652 void
653 cipher_set_keycontext(struct sshcipher_ctx *cc, const u_char *dat)
654 {
655 #if defined(WITH_OPENSSL) && !defined(OPENSSL_NO_RC4)
656         const struct sshcipher *c = cc->cipher;
657         int plen;
658
659         if (c->evptype == EVP_rc4) {
660                 plen = EVP_X_STATE_LEN(cc->evp);
661                 memcpy(EVP_X_STATE(cc->evp), dat, plen);
662         }
663 #endif
664 }