Merge branch 'vendor/OPENSSH'
[dragonfly.git] / crypto / openssh / cipher.c
1 /* $OpenBSD: cipher.c,v 1.99 2014/06/24 01:13:21 djm 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         { "blowfish",   SSH_CIPHER_BLOWFISH, 8, 32, 0, 0, 0, 1, evp_ssh1_bf },
85 #endif /* WITH_SSH1 */
86 #ifdef WITH_OPENSSL
87         { "none",       SSH_CIPHER_NONE, 8, 0, 0, 0, 0, 0, EVP_enc_null },
88         { "3des-cbc",   SSH_CIPHER_SSH2, 8, 24, 0, 0, 0, 1, EVP_des_ede3_cbc },
89         { "blowfish-cbc",
90                         SSH_CIPHER_SSH2, 8, 16, 0, 0, 0, 1, EVP_bf_cbc },
91         { "cast128-cbc",
92                         SSH_CIPHER_SSH2, 8, 16, 0, 0, 0, 1, EVP_cast5_cbc },
93         { "arcfour",    SSH_CIPHER_SSH2, 8, 16, 0, 0, 0, 0, EVP_rc4 },
94         { "arcfour128", SSH_CIPHER_SSH2, 8, 16, 0, 0, 1536, 0, EVP_rc4 },
95         { "arcfour256", SSH_CIPHER_SSH2, 8, 32, 0, 0, 1536, 0, EVP_rc4 },
96         { "aes128-cbc", SSH_CIPHER_SSH2, 16, 16, 0, 0, 0, 1, EVP_aes_128_cbc },
97         { "aes192-cbc", SSH_CIPHER_SSH2, 16, 24, 0, 0, 0, 1, EVP_aes_192_cbc },
98         { "aes256-cbc", SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, 1, EVP_aes_256_cbc },
99         { "rijndael-cbc@lysator.liu.se",
100                         SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, 1, EVP_aes_256_cbc },
101         { "aes128-ctr", SSH_CIPHER_SSH2, 16, 16, 0, 0, 0, 0, EVP_aes_128_ctr },
102         { "aes192-ctr", SSH_CIPHER_SSH2, 16, 24, 0, 0, 0, 0, EVP_aes_192_ctr },
103         { "aes256-ctr", SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, 0, EVP_aes_256_ctr },
104 # ifdef OPENSSL_HAVE_EVPGCM
105         { "aes128-gcm@openssh.com",
106                         SSH_CIPHER_SSH2, 16, 16, 12, 16, 0, 0, EVP_aes_128_gcm },
107         { "aes256-gcm@openssh.com",
108                         SSH_CIPHER_SSH2, 16, 32, 12, 16, 0, 0, EVP_aes_256_gcm },
109 # endif /* OPENSSL_HAVE_EVPGCM */
110 #else /* WITH_OPENSSL */
111         { "aes128-ctr", SSH_CIPHER_SSH2, 16, 16, 0, 0, 0, CFLAG_AESCTR, NULL },
112         { "aes192-ctr", SSH_CIPHER_SSH2, 16, 24, 0, 0, 0, CFLAG_AESCTR, NULL },
113         { "aes256-ctr", SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, CFLAG_AESCTR, NULL },
114         { "none",       SSH_CIPHER_NONE, 8, 0, 0, 0, 0, CFLAG_NONE, NULL },
115 #endif /* WITH_OPENSSL */
116         { "chacha20-poly1305@openssh.com",
117                         SSH_CIPHER_SSH2, 8, 64, 0, 16, 0, CFLAG_CHACHAPOLY, NULL },
118
119         { NULL,         SSH_CIPHER_INVALID, 0, 0, 0, 0, 0, 0, NULL }
120 };
121
122 /*--*/
123
124 /* Returns a comma-separated list of supported ciphers. */
125 char *
126 cipher_alg_list(char sep, int auth_only)
127 {
128         char *tmp, *ret = NULL;
129         size_t nlen, rlen = 0;
130         const struct sshcipher *c;
131
132         for (c = ciphers; c->name != NULL; c++) {
133                 if (c->number != SSH_CIPHER_SSH2)
134                         continue;
135                 if (auth_only && c->auth_len == 0)
136                         continue;
137                 if (ret != NULL)
138                         ret[rlen++] = sep;
139                 nlen = strlen(c->name);
140                 if ((tmp = realloc(ret, rlen + nlen + 2)) == NULL) {
141                         free(ret);
142                         return NULL;
143                 }
144                 ret = tmp;
145                 memcpy(ret + rlen, c->name, nlen + 1);
146                 rlen += nlen;
147         }
148         return ret;
149 }
150
151 u_int
152 cipher_blocksize(const struct sshcipher *c)
153 {
154         return (c->block_size);
155 }
156
157 u_int
158 cipher_keylen(const struct sshcipher *c)
159 {
160         return (c->key_len);
161 }
162
163 u_int
164 cipher_seclen(const struct sshcipher *c)
165 {
166         if (strcmp("3des-cbc", c->name) == 0)
167                 return 14;
168         return cipher_keylen(c);
169 }
170
171 u_int
172 cipher_authlen(const struct sshcipher *c)
173 {
174         return (c->auth_len);
175 }
176
177 u_int
178 cipher_ivlen(const struct sshcipher *c)
179 {
180         /*
181          * Default is cipher block size, except for chacha20+poly1305 that
182          * needs no IV. XXX make iv_len == -1 default?
183          */
184         return (c->iv_len != 0 || (c->flags & CFLAG_CHACHAPOLY) != 0) ?
185             c->iv_len : c->block_size;
186 }
187
188 u_int
189 cipher_get_number(const struct sshcipher *c)
190 {
191         return (c->number);
192 }
193
194 u_int
195 cipher_is_cbc(const struct sshcipher *c)
196 {
197         return (c->flags & CFLAG_CBC) != 0;
198 }
199
200 u_int
201 cipher_mask_ssh1(int client)
202 {
203         u_int mask = 0;
204         mask |= 1 << SSH_CIPHER_3DES;           /* Mandatory */
205         mask |= 1 << SSH_CIPHER_BLOWFISH;
206         if (client) {
207                 mask |= 1 << SSH_CIPHER_DES;
208         }
209         return mask;
210 }
211
212 const struct sshcipher *
213 cipher_by_name(const char *name)
214 {
215         const struct sshcipher *c;
216         for (c = ciphers; c->name != NULL; c++)
217                 if (strcmp(c->name, name) == 0)
218                         return c;
219         return NULL;
220 }
221
222 const struct sshcipher *
223 cipher_by_number(int id)
224 {
225         const struct sshcipher *c;
226         for (c = ciphers; c->name != NULL; c++)
227                 if (c->number == id)
228                         return c;
229         return NULL;
230 }
231
232 #define CIPHER_SEP      ","
233 int
234 ciphers_valid(const char *names)
235 {
236         const struct sshcipher *c;
237         char *cipher_list, *cp;
238         char *p;
239
240         if (names == NULL || strcmp(names, "") == 0)
241                 return 0;
242         if ((cipher_list = cp = strdup(names)) == NULL)
243                 return 0;
244         for ((p = strsep(&cp, CIPHER_SEP)); p && *p != '\0';
245             (p = strsep(&cp, CIPHER_SEP))) {
246                 c = cipher_by_name(p);
247                 if (c == NULL || (c->number != SSH_CIPHER_SSH2 &&
248 c->number != SSH_CIPHER_NONE)) {
249                         free(cipher_list);
250                         return 0;
251                 }
252         }
253         free(cipher_list);
254         return 1;
255 }
256
257 /*
258  * Parses the name of the cipher.  Returns the number of the corresponding
259  * cipher, or -1 on error.
260  */
261
262 int
263 cipher_number(const char *name)
264 {
265         const struct sshcipher *c;
266         if (name == NULL)
267                 return -1;
268         for (c = ciphers; c->name != NULL; c++)
269                 if (strcasecmp(c->name, name) == 0)
270                         return c->number;
271         return -1;
272 }
273
274 char *
275 cipher_name(int id)
276 {
277         const struct sshcipher *c = cipher_by_number(id);
278         return (c==NULL) ? "<unknown>" : c->name;
279 }
280
281 const char *
282 cipher_warning_message(const struct sshcipher_ctx *cc)
283 {
284         if (cc == NULL || cc->cipher == NULL)
285                 return NULL;
286         if (cc->cipher->number == SSH_CIPHER_DES)
287                 return "use of DES is strongly discouraged due to "
288                     "cryptographic weaknesses";
289         return NULL;
290 }
291
292 int
293 cipher_init(struct sshcipher_ctx *cc, const struct sshcipher *cipher,
294     const u_char *key, u_int keylen, const u_char *iv, u_int ivlen,
295     int do_encrypt)
296 {
297 #ifdef WITH_OPENSSL
298         int ret = SSH_ERR_INTERNAL_ERROR;
299         const EVP_CIPHER *type;
300         int klen;
301         u_char *junk, *discard;
302
303         if (cipher->number == SSH_CIPHER_DES) {
304                 if (keylen > 8)
305                         keylen = 8;
306         }
307 #endif
308         cc->plaintext = (cipher->number == SSH_CIPHER_NONE);
309         cc->encrypt = do_encrypt;
310
311         if (keylen < cipher->key_len ||
312             (iv != NULL && ivlen < cipher_ivlen(cipher)))
313                 return SSH_ERR_INVALID_ARGUMENT;
314
315         cc->cipher = cipher;
316         if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
317                 return chachapoly_init(&cc->cp_ctx, key, keylen);
318         }
319 #ifndef WITH_OPENSSL
320         if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
321                 aesctr_keysetup(&cc->ac_ctx, key, 8 * keylen, 8 * ivlen);
322                 aesctr_ivsetup(&cc->ac_ctx, iv);
323                 return 0;
324         }
325         if ((cc->cipher->flags & CFLAG_NONE) != 0)
326                 return 0;
327         return SSH_ERR_INVALID_ARGUMENT;
328 #else
329         type = (*cipher->evptype)();
330         EVP_CIPHER_CTX_init(&cc->evp);
331         if (EVP_CipherInit(&cc->evp, type, NULL, (u_char *)iv,
332             (do_encrypt == CIPHER_ENCRYPT)) == 0) {
333                 ret = SSH_ERR_LIBCRYPTO_ERROR;
334                 goto bad;
335         }
336         if (cipher_authlen(cipher) &&
337             !EVP_CIPHER_CTX_ctrl(&cc->evp, EVP_CTRL_GCM_SET_IV_FIXED,
338             -1, (u_char *)iv)) {
339                 ret = SSH_ERR_LIBCRYPTO_ERROR;
340                 goto bad;
341         }
342         klen = EVP_CIPHER_CTX_key_length(&cc->evp);
343         if (klen > 0 && keylen != (u_int)klen) {
344                 if (EVP_CIPHER_CTX_set_key_length(&cc->evp, keylen) == 0) {
345                         ret = SSH_ERR_LIBCRYPTO_ERROR;
346                         goto bad;
347                 }
348         }
349         if (EVP_CipherInit(&cc->evp, NULL, (u_char *)key, NULL, -1) == 0) {
350                 ret = SSH_ERR_LIBCRYPTO_ERROR;
351                 goto bad;
352         }
353
354         if (cipher->discard_len > 0) {
355                 if ((junk = malloc(cipher->discard_len)) == NULL ||
356                     (discard = malloc(cipher->discard_len)) == NULL) {
357                         if (junk != NULL)
358                                 free(junk);
359                         ret = SSH_ERR_ALLOC_FAIL;
360                         goto bad;
361                 }
362                 ret = EVP_Cipher(&cc->evp, discard, junk, cipher->discard_len);
363                 explicit_bzero(discard, cipher->discard_len);
364                 free(junk);
365                 free(discard);
366                 if (ret != 1) {
367                         ret = SSH_ERR_LIBCRYPTO_ERROR;
368  bad:
369                         EVP_CIPHER_CTX_cleanup(&cc->evp);
370                         return ret;
371                 }
372         }
373 #endif
374         return 0;
375 }
376
377 /*
378  * cipher_crypt() operates as following:
379  * Copy 'aadlen' bytes (without en/decryption) from 'src' to 'dest'.
380  * Theses bytes are treated as additional authenticated data for
381  * authenticated encryption modes.
382  * En/Decrypt 'len' bytes at offset 'aadlen' from 'src' to 'dest'.
383  * Use 'authlen' bytes at offset 'len'+'aadlen' as the authentication tag.
384  * This tag is written on encryption and verified on decryption.
385  * Both 'aadlen' and 'authlen' can be set to 0.
386  */
387 int
388 cipher_crypt(struct sshcipher_ctx *cc, u_int seqnr, u_char *dest,
389    const u_char *src, u_int len, u_int aadlen, u_int authlen)
390 {
391         if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
392                 return chachapoly_crypt(&cc->cp_ctx, seqnr, dest, src,
393                     len, aadlen, authlen, cc->encrypt);
394         }
395 #ifndef WITH_OPENSSL
396         if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
397                 if (aadlen)
398                         memcpy(dest, src, aadlen);
399                 aesctr_encrypt_bytes(&cc->ac_ctx, src + aadlen,
400                     dest + aadlen, len);
401                 return 0;
402         }
403         if ((cc->cipher->flags & CFLAG_NONE) != 0) {
404                 memcpy(dest, src, aadlen + len);
405                 return 0;
406         }
407         return SSH_ERR_INVALID_ARGUMENT;
408 #else
409         if (authlen) {
410                 u_char lastiv[1];
411
412                 if (authlen != cipher_authlen(cc->cipher))
413                         return SSH_ERR_INVALID_ARGUMENT;
414                 /* increment IV */
415                 if (!EVP_CIPHER_CTX_ctrl(&cc->evp, EVP_CTRL_GCM_IV_GEN,
416                     1, lastiv))
417                         return SSH_ERR_LIBCRYPTO_ERROR;
418                 /* set tag on decyption */
419                 if (!cc->encrypt &&
420                     !EVP_CIPHER_CTX_ctrl(&cc->evp, EVP_CTRL_GCM_SET_TAG,
421                     authlen, (u_char *)src + aadlen + len))
422                         return SSH_ERR_LIBCRYPTO_ERROR;
423         }
424         if (aadlen) {
425                 if (authlen &&
426                     EVP_Cipher(&cc->evp, NULL, (u_char *)src, aadlen) < 0)
427                         return SSH_ERR_LIBCRYPTO_ERROR;
428                 memcpy(dest, src, aadlen);
429         }
430         if (len % cc->cipher->block_size)
431                 return SSH_ERR_INVALID_ARGUMENT;
432         if (EVP_Cipher(&cc->evp, dest + aadlen, (u_char *)src + aadlen,
433             len) < 0)
434                 return SSH_ERR_LIBCRYPTO_ERROR;
435         if (authlen) {
436                 /* compute tag (on encrypt) or verify tag (on decrypt) */
437                 if (EVP_Cipher(&cc->evp, NULL, NULL, 0) < 0)
438                         return cc->encrypt ?
439                             SSH_ERR_LIBCRYPTO_ERROR : SSH_ERR_MAC_INVALID;
440                 if (cc->encrypt &&
441                     !EVP_CIPHER_CTX_ctrl(&cc->evp, EVP_CTRL_GCM_GET_TAG,
442                     authlen, dest + aadlen + len))
443                         return SSH_ERR_LIBCRYPTO_ERROR;
444         }
445         return 0;
446 #endif
447 }
448
449 /* Extract the packet length, including any decryption necessary beforehand */
450 int
451 cipher_get_length(struct sshcipher_ctx *cc, u_int *plenp, u_int seqnr,
452     const u_char *cp, u_int len)
453 {
454         if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
455                 return chachapoly_get_length(&cc->cp_ctx, plenp, seqnr,
456                     cp, len);
457         if (len < 4)
458                 return SSH_ERR_MESSAGE_INCOMPLETE;
459         *plenp = get_u32(cp);
460         return 0;
461 }
462
463 int
464 cipher_cleanup(struct sshcipher_ctx *cc)
465 {
466         if (cc == NULL || cc->cipher == NULL)
467                 return 0;
468         if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
469                 explicit_bzero(&cc->cp_ctx, sizeof(cc->cp_ctx));
470         else if ((cc->cipher->flags & CFLAG_AESCTR) != 0)
471                 explicit_bzero(&cc->ac_ctx, sizeof(cc->ac_ctx));
472 #ifdef WITH_OPENSSL
473         else if (EVP_CIPHER_CTX_cleanup(&cc->evp) == 0)
474                 return SSH_ERR_LIBCRYPTO_ERROR;
475 #endif
476         return 0;
477 }
478
479 /*
480  * Selects the cipher, and keys if by computing the MD5 checksum of the
481  * passphrase and using the resulting 16 bytes as the key.
482  */
483 int
484 cipher_set_key_string(struct sshcipher_ctx *cc, const struct sshcipher *cipher,
485     const char *passphrase, int do_encrypt)
486 {
487         u_char digest[16];
488         int r = SSH_ERR_INTERNAL_ERROR;
489
490         if ((r = ssh_digest_memory(SSH_DIGEST_MD5,
491             passphrase, strlen(passphrase),
492             digest, sizeof(digest))) != 0)
493                 goto out;
494
495         r = cipher_init(cc, cipher, digest, 16, NULL, 0, do_encrypt);
496  out:
497         explicit_bzero(digest, sizeof(digest));
498         return r;
499 }
500
501 /*
502  * Exports an IV from the sshcipher_ctx required to export the key
503  * state back from the unprivileged child to the privileged parent
504  * process.
505  */
506 int
507 cipher_get_keyiv_len(const struct sshcipher_ctx *cc)
508 {
509         const struct sshcipher *c = cc->cipher;
510         int ivlen = 0;
511
512         if (c->number == SSH_CIPHER_3DES)
513                 ivlen = 24;
514         else if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
515                 ivlen = 0;
516 #ifdef WITH_OPENSSL
517         else
518                 ivlen = EVP_CIPHER_CTX_iv_length(&cc->evp);
519 #endif /* WITH_OPENSSL */
520         return (ivlen);
521 }
522
523 int
524 cipher_get_keyiv(struct sshcipher_ctx *cc, u_char *iv, u_int len)
525 {
526         const struct sshcipher *c = cc->cipher;
527 #ifdef WITH_OPENSSL
528         int evplen;
529 #endif
530
531         if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
532                 if (len != 0)
533                         return SSH_ERR_INVALID_ARGUMENT;
534                 return 0;
535         }
536         if ((cc->cipher->flags & CFLAG_NONE) != 0)
537                 return 0;
538
539         switch (c->number) {
540 #ifdef WITH_OPENSSL
541         case SSH_CIPHER_NONE:
542         case SSH_CIPHER_SSH2:
543         case SSH_CIPHER_DES:
544         case SSH_CIPHER_BLOWFISH:
545                 evplen = EVP_CIPHER_CTX_iv_length(&cc->evp);
546                 if (evplen == 0)
547                         return 0;
548                 else if (evplen < 0)
549                         return SSH_ERR_LIBCRYPTO_ERROR;
550                 if ((u_int)evplen != len)
551                         return SSH_ERR_INVALID_ARGUMENT;
552 #ifndef OPENSSL_HAVE_EVPCTR
553                 if (c->evptype == evp_aes_128_ctr)
554                         ssh_aes_ctr_iv(&cc->evp, 0, iv, len);
555                 else
556 #endif
557                 if (cipher_authlen(c)) {
558                         if (!EVP_CIPHER_CTX_ctrl(&cc->evp, EVP_CTRL_GCM_IV_GEN,
559                            len, iv))
560                                return SSH_ERR_LIBCRYPTO_ERROR;
561                 } else
562                         memcpy(iv, cc->evp.iv, len);
563                 break;
564 #endif
565 #ifdef WITH_SSH1
566         case SSH_CIPHER_3DES:
567                 return ssh1_3des_iv(&cc->evp, 0, iv, 24);
568 #endif
569         default:
570                 return SSH_ERR_INVALID_ARGUMENT;
571         }
572         return 0;
573 }
574
575 int
576 cipher_set_keyiv(struct sshcipher_ctx *cc, const u_char *iv)
577 {
578         const struct sshcipher *c = cc->cipher;
579 #ifdef WITH_OPENSSL
580         int evplen = 0;
581 #endif
582
583         if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
584                 return 0;
585         if ((cc->cipher->flags & CFLAG_NONE) != 0)
586                 return 0;
587
588         switch (c->number) {
589 #ifdef WITH_OPENSSL
590         case SSH_CIPHER_NONE:
591         case SSH_CIPHER_SSH2:
592         case SSH_CIPHER_DES:
593         case SSH_CIPHER_BLOWFISH:
594                 evplen = EVP_CIPHER_CTX_iv_length(&cc->evp);
595                 if (evplen <= 0)
596                         return SSH_ERR_LIBCRYPTO_ERROR;
597                 if (cipher_authlen(c)) {
598                         /* XXX iv arg is const, but EVP_CIPHER_CTX_ctrl isn't */
599                         if (!EVP_CIPHER_CTX_ctrl(&cc->evp,
600                             EVP_CTRL_GCM_SET_IV_FIXED, -1, (void *)iv))
601                                 return SSH_ERR_LIBCRYPTO_ERROR;
602                 } else
603                         memcpy(cc->evp.iv, iv, evplen);
604                 break;
605 #endif
606 #ifdef WITH_SSH1
607         case SSH_CIPHER_3DES:
608                 return ssh1_3des_iv(&cc->evp, 1, (u_char *)iv, 24);
609 #endif
610         default:
611                 return SSH_ERR_INVALID_ARGUMENT;
612         }
613         return 0;
614 }
615
616 #ifdef WITH_OPENSSL
617 #define EVP_X_STATE(evp)        (evp).cipher_data
618 #define EVP_X_STATE_LEN(evp)    (evp).cipher->ctx_size
619 #endif
620
621 int
622 cipher_get_keycontext(const struct sshcipher_ctx *cc, u_char *dat)
623 {
624 #ifdef WITH_OPENSSL
625         const struct sshcipher *c = cc->cipher;
626         int plen = 0;
627
628         if (c->evptype == EVP_rc4) {
629                 plen = EVP_X_STATE_LEN(cc->evp);
630                 if (dat == NULL)
631                         return (plen);
632                 memcpy(dat, EVP_X_STATE(cc->evp), plen);
633         }
634         return (plen);
635 #else
636         return 0;
637 #endif
638 }
639
640 void
641 cipher_set_keycontext(struct sshcipher_ctx *cc, const u_char *dat)
642 {
643 #ifdef WITH_OPENSSL
644         const struct sshcipher *c = cc->cipher;
645         int plen;
646
647         if (c->evptype == EVP_rc4) {
648                 plen = EVP_X_STATE_LEN(cc->evp);
649                 memcpy(EVP_X_STATE(cc->evp), dat, plen);
650         }
651 #endif
652 }