blob: bc7a5e2243120032cc10842dc184993e3e0054af [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11003 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Miller4af51302000-04-16 11:18:38 +10005 *
Damien Millere4340be2000-09-16 13:29:08 +11006 * As far as I am concerned, the code I have written for this software
7 * can be used freely for any purpose. Any derived versions of this
8 * software must be clearly marked as such, and if the derived work is
9 * incompatible with the protocol description in the RFC file, it must be
10 * called by a name other than "ssh" or "Secure Shell".
Damien Miller95def091999-11-25 00:26:21 +110011 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100012
Damien Millere4340be2000-09-16 13:29:08 +110013/* RCSID("$OpenBSD: cipher.h,v 1.19 2000/09/07 20:27:50 deraadt Exp $"); */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100014
15#ifndef CIPHER_H
16#define CIPHER_H
17
18#include <openssl/des.h>
19#include <openssl/blowfish.h>
Damien Millerb38eff82000-04-01 11:09:21 +100020#include <openssl/rc4.h>
21#include <openssl/cast.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100022
23/* Cipher types. New types can be added, but old types should not be removed
24 for compatibility. The maximum allowed value is 31. */
Damien Miller30c3d422000-05-09 11:02:59 +100025#define SSH_CIPHER_ILLEGAL -2 /* No valid cipher selected. */
Damien Miller95def091999-11-25 00:26:21 +110026#define SSH_CIPHER_NOT_SET -1 /* None selected (invalid number). */
27#define SSH_CIPHER_NONE 0 /* no encryption */
28#define SSH_CIPHER_IDEA 1 /* IDEA CFB */
29#define SSH_CIPHER_DES 2 /* DES CBC */
30#define SSH_CIPHER_3DES 3 /* 3DES CBC */
31#define SSH_CIPHER_BROKEN_TSS 4 /* TRI's Simple Stream encryption CBC */
32#define SSH_CIPHER_BROKEN_RC4 5 /* Alleged RC4 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100033#define SSH_CIPHER_BLOWFISH 6
Damien Millerb38eff82000-04-01 11:09:21 +100034#define SSH_CIPHER_RESERVED 7
35
36/* these ciphers are used in SSH2: */
37#define SSH_CIPHER_BLOWFISH_CBC 8
38#define SSH_CIPHER_3DES_CBC 9
39#define SSH_CIPHER_ARCFOUR 10 /* Alleged RC4 */
40#define SSH_CIPHER_CAST128_CBC 11
Damien Millerd4a8b7e1999-10-27 13:42:43 +100041
42typedef struct {
Damien Miller95def091999-11-25 00:26:21 +110043 unsigned int type;
44 union {
45 struct {
46 des_key_schedule key1;
47 des_key_schedule key2;
48 des_cblock iv2;
49 des_key_schedule key3;
50 des_cblock iv3;
51 } des3;
52 struct {
53 struct bf_key_st key;
54 unsigned char iv[8];
55 } bf;
Damien Millerb38eff82000-04-01 11:09:21 +100056 struct {
57 CAST_KEY key;
58 unsigned char iv[8];
59 } cast;
60 RC4_KEY rc4;
Damien Miller95def091999-11-25 00:26:21 +110061 } u;
62} CipherContext;
Damien Miller5428f641999-11-25 11:54:57 +110063/*
64 * Returns a bit mask indicating which ciphers are supported by this
65 * implementation. The bit mask has the corresponding bit set of each
66 * supported cipher.
67 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100068unsigned int cipher_mask();
Damien Miller1383bd82000-04-06 12:32:37 +100069unsigned int cipher_mask1();
70unsigned int cipher_mask2();
Damien Millerd4a8b7e1999-10-27 13:42:43 +100071
72/* Returns the name of the cipher. */
73const char *cipher_name(int cipher);
74
Damien Miller5428f641999-11-25 11:54:57 +110075/*
76 * Parses the name of the cipher. Returns the number of the corresponding
77 * cipher, or -1 on error.
78 */
Damien Miller95def091999-11-25 00:26:21 +110079int cipher_number(const char *name);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100080
Damien Miller78928792000-04-12 20:17:38 +100081/* returns 1 if all ciphers are supported (ssh2 only) */
82int ciphers_valid(const char *names);
83
Damien Miller5428f641999-11-25 11:54:57 +110084/*
85 * Selects the cipher to use and sets the key. If for_encryption is true,
86 * the key is setup for encryption; otherwise it is setup for decryption.
87 */
Damien Miller4af51302000-04-16 11:18:38 +100088void
Damien Miller95def091999-11-25 00:26:21 +110089cipher_set_key(CipherContext * context, int cipher,
Damien Miller1383bd82000-04-06 12:32:37 +100090 const unsigned char *key, int keylen);
Damien Miller4af51302000-04-16 11:18:38 +100091void
Damien Millerb38eff82000-04-01 11:09:21 +100092cipher_set_key_iv(CipherContext * context, int cipher,
Damien Miller4af51302000-04-16 11:18:38 +100093 const unsigned char *key, int keylen,
Damien Millerb38eff82000-04-01 11:09:21 +100094 const unsigned char *iv, int ivlen);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100095
Damien Miller5428f641999-11-25 11:54:57 +110096/*
97 * Sets key for the cipher by computing the MD5 checksum of the passphrase,
98 * and using the resulting 16 bytes as the key.
99 */
Damien Miller4af51302000-04-16 11:18:38 +1000100void
Damien Miller95def091999-11-25 00:26:21 +1100101cipher_set_key_string(CipherContext * context, int cipher,
Damien Miller1383bd82000-04-06 12:32:37 +1000102 const char *passphrase);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000103
104/* Encrypts data using the cipher. */
Damien Miller4af51302000-04-16 11:18:38 +1000105void
Damien Miller95def091999-11-25 00:26:21 +1100106cipher_encrypt(CipherContext * context, unsigned char *dest,
107 const unsigned char *src, unsigned int len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000108
109/* Decrypts data using the cipher. */
Damien Miller4af51302000-04-16 11:18:38 +1000110void
Damien Miller95def091999-11-25 00:26:21 +1100111cipher_decrypt(CipherContext * context, unsigned char *dest,
112 const unsigned char *src, unsigned int len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000113
Damien Miller95def091999-11-25 00:26:21 +1100114#endif /* CIPHER_H */