blob: b7410fbc09b08e887e0a559e2d32ce1e2386403a [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller4af51302000-04-16 11:18:38 +10002 *
Damien Miller95def091999-11-25 00:26:21 +11003 * cipher.h
Damien Miller4af51302000-04-16 11:18:38 +10004 *
Damien Miller95def091999-11-25 00:26:21 +11005 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller4af51302000-04-16 11:18:38 +10006 *
Damien Miller95def091999-11-25 00:26:21 +11007 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
8 * All rights reserved
Damien Miller4af51302000-04-16 11:18:38 +10009 *
Damien Miller95def091999-11-25 00:26:21 +110010 * Created: Wed Apr 19 16:50:42 1995 ylo
Damien Miller4af51302000-04-16 11:18:38 +100011 *
Damien Miller95def091999-11-25 00:26:21 +110012 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100013
Damien Miller30c3d422000-05-09 11:02:59 +100014/* RCSID("$Id: cipher.h,v 1.13 2000/05/09 01:03:00 damien Exp $"); */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100015
16#ifndef CIPHER_H
17#define CIPHER_H
18
19#include <openssl/des.h>
20#include <openssl/blowfish.h>
Damien Millerb38eff82000-04-01 11:09:21 +100021#include <openssl/rc4.h>
22#include <openssl/cast.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100023
24/* Cipher types. New types can be added, but old types should not be removed
25 for compatibility. The maximum allowed value is 31. */
Damien Miller30c3d422000-05-09 11:02:59 +100026#define SSH_CIPHER_ILLEGAL -2 /* No valid cipher selected. */
Damien Miller95def091999-11-25 00:26:21 +110027#define SSH_CIPHER_NOT_SET -1 /* None selected (invalid number). */
28#define SSH_CIPHER_NONE 0 /* no encryption */
29#define SSH_CIPHER_IDEA 1 /* IDEA CFB */
30#define SSH_CIPHER_DES 2 /* DES CBC */
31#define SSH_CIPHER_3DES 3 /* 3DES CBC */
32#define SSH_CIPHER_BROKEN_TSS 4 /* TRI's Simple Stream encryption CBC */
33#define SSH_CIPHER_BROKEN_RC4 5 /* Alleged RC4 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100034#define SSH_CIPHER_BLOWFISH 6
Damien Millerb38eff82000-04-01 11:09:21 +100035#define SSH_CIPHER_RESERVED 7
36
37/* these ciphers are used in SSH2: */
38#define SSH_CIPHER_BLOWFISH_CBC 8
39#define SSH_CIPHER_3DES_CBC 9
40#define SSH_CIPHER_ARCFOUR 10 /* Alleged RC4 */
41#define SSH_CIPHER_CAST128_CBC 11
Damien Millerd4a8b7e1999-10-27 13:42:43 +100042
43typedef struct {
Damien Miller95def091999-11-25 00:26:21 +110044 unsigned int type;
45 union {
46 struct {
47 des_key_schedule key1;
48 des_key_schedule key2;
49 des_cblock iv2;
50 des_key_schedule key3;
51 des_cblock iv3;
52 } des3;
53 struct {
54 struct bf_key_st key;
55 unsigned char iv[8];
56 } bf;
Damien Millerb38eff82000-04-01 11:09:21 +100057 struct {
58 CAST_KEY key;
59 unsigned char iv[8];
60 } cast;
61 RC4_KEY rc4;
Damien Miller95def091999-11-25 00:26:21 +110062 } u;
63} CipherContext;
Damien Miller5428f641999-11-25 11:54:57 +110064/*
65 * Returns a bit mask indicating which ciphers are supported by this
66 * implementation. The bit mask has the corresponding bit set of each
67 * supported cipher.
68 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100069unsigned int cipher_mask();
Damien Miller1383bd82000-04-06 12:32:37 +100070unsigned int cipher_mask1();
71unsigned int cipher_mask2();
Damien Millerd4a8b7e1999-10-27 13:42:43 +100072
73/* Returns the name of the cipher. */
74const char *cipher_name(int cipher);
75
Damien Miller5428f641999-11-25 11:54:57 +110076/*
77 * Parses the name of the cipher. Returns the number of the corresponding
78 * cipher, or -1 on error.
79 */
Damien Miller95def091999-11-25 00:26:21 +110080int cipher_number(const char *name);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100081
Damien Miller78928792000-04-12 20:17:38 +100082/* returns 1 if all ciphers are supported (ssh2 only) */
83int ciphers_valid(const char *names);
84
Damien Miller5428f641999-11-25 11:54:57 +110085/*
86 * Selects the cipher to use and sets the key. If for_encryption is true,
87 * the key is setup for encryption; otherwise it is setup for decryption.
88 */
Damien Miller4af51302000-04-16 11:18:38 +100089void
Damien Miller95def091999-11-25 00:26:21 +110090cipher_set_key(CipherContext * context, int cipher,
Damien Miller1383bd82000-04-06 12:32:37 +100091 const unsigned char *key, int keylen);
Damien Miller4af51302000-04-16 11:18:38 +100092void
Damien Millerb38eff82000-04-01 11:09:21 +100093cipher_set_key_iv(CipherContext * context, int cipher,
Damien Miller4af51302000-04-16 11:18:38 +100094 const unsigned char *key, int keylen,
Damien Millerb38eff82000-04-01 11:09:21 +100095 const unsigned char *iv, int ivlen);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100096
Damien Miller5428f641999-11-25 11:54:57 +110097/*
98 * Sets key for the cipher by computing the MD5 checksum of the passphrase,
99 * and using the resulting 16 bytes as the key.
100 */
Damien Miller4af51302000-04-16 11:18:38 +1000101void
Damien Miller95def091999-11-25 00:26:21 +1100102cipher_set_key_string(CipherContext * context, int cipher,
Damien Miller1383bd82000-04-06 12:32:37 +1000103 const char *passphrase);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000104
105/* Encrypts data using the cipher. */
Damien Miller4af51302000-04-16 11:18:38 +1000106void
Damien Miller95def091999-11-25 00:26:21 +1100107cipher_encrypt(CipherContext * context, unsigned char *dest,
108 const unsigned char *src, unsigned int len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000109
110/* Decrypts data using the cipher. */
Damien Miller4af51302000-04-16 11:18:38 +1000111void
Damien Miller95def091999-11-25 00:26:21 +1100112cipher_decrypt(CipherContext * context, unsigned char *dest,
113 const unsigned char *src, unsigned int len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000114
Damien Miller95def091999-11-25 00:26:21 +1100115#endif /* CIPHER_H */