blob: 383e3444f02c750149a48831a8796dd462b2fe90 [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 Miller4af51302000-04-16 11:18:38 +100014/* RCSID("$Id: cipher.h,v 1.11 2000/04/16 01:18:41 damien Exp $"); */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100015
16#ifndef CIPHER_H
17#define CIPHER_H
18
Damien Miller7e8e8201999-11-16 13:37:16 +110019#include "config.h"
20
Damien Miller7f6ea021999-10-28 13:25:17 +100021#ifdef HAVE_OPENSSL
Damien Millerd4a8b7e1999-10-27 13:42:43 +100022#include <openssl/des.h>
23#include <openssl/blowfish.h>
Damien Millerb38eff82000-04-01 11:09:21 +100024#include <openssl/rc4.h>
25#include <openssl/cast.h>
Damien Miller7f6ea021999-10-28 13:25:17 +100026#endif
27#ifdef HAVE_SSL
28#include <ssl/des.h>
29#include <ssl/blowfish.h>
Damien Millerb38eff82000-04-01 11:09:21 +100030#include <ssl/rc4.h>
31#include <ssl/cast.h>
Damien Miller7f6ea021999-10-28 13:25:17 +100032#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +100033
34/* Cipher types. New types can be added, but old types should not be removed
35 for compatibility. The maximum allowed value is 31. */
Damien Miller95def091999-11-25 00:26:21 +110036#define SSH_CIPHER_NOT_SET -1 /* None selected (invalid number). */
37#define SSH_CIPHER_NONE 0 /* no encryption */
38#define SSH_CIPHER_IDEA 1 /* IDEA CFB */
39#define SSH_CIPHER_DES 2 /* DES CBC */
40#define SSH_CIPHER_3DES 3 /* 3DES CBC */
41#define SSH_CIPHER_BROKEN_TSS 4 /* TRI's Simple Stream encryption CBC */
42#define SSH_CIPHER_BROKEN_RC4 5 /* Alleged RC4 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100043#define SSH_CIPHER_BLOWFISH 6
Damien Millerb38eff82000-04-01 11:09:21 +100044#define SSH_CIPHER_RESERVED 7
45
46/* these ciphers are used in SSH2: */
47#define SSH_CIPHER_BLOWFISH_CBC 8
48#define SSH_CIPHER_3DES_CBC 9
49#define SSH_CIPHER_ARCFOUR 10 /* Alleged RC4 */
50#define SSH_CIPHER_CAST128_CBC 11
Damien Millerd4a8b7e1999-10-27 13:42:43 +100051
52typedef struct {
Damien Miller95def091999-11-25 00:26:21 +110053 unsigned int type;
54 union {
55 struct {
56 des_key_schedule key1;
57 des_key_schedule key2;
58 des_cblock iv2;
59 des_key_schedule key3;
60 des_cblock iv3;
61 } des3;
62 struct {
63 struct bf_key_st key;
64 unsigned char iv[8];
65 } bf;
Damien Millerb38eff82000-04-01 11:09:21 +100066 struct {
67 CAST_KEY key;
68 unsigned char iv[8];
69 } cast;
70 RC4_KEY rc4;
Damien Miller95def091999-11-25 00:26:21 +110071 } u;
72} CipherContext;
Damien Miller5428f641999-11-25 11:54:57 +110073/*
74 * Returns a bit mask indicating which ciphers are supported by this
75 * implementation. The bit mask has the corresponding bit set of each
76 * supported cipher.
77 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100078unsigned int cipher_mask();
Damien Miller1383bd82000-04-06 12:32:37 +100079unsigned int cipher_mask1();
80unsigned int cipher_mask2();
Damien Millerd4a8b7e1999-10-27 13:42:43 +100081
82/* Returns the name of the cipher. */
83const char *cipher_name(int cipher);
84
Damien Miller5428f641999-11-25 11:54:57 +110085/*
86 * Parses the name of the cipher. Returns the number of the corresponding
87 * cipher, or -1 on error.
88 */
Damien Miller95def091999-11-25 00:26:21 +110089int cipher_number(const char *name);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100090
Damien Miller78928792000-04-12 20:17:38 +100091/* returns 1 if all ciphers are supported (ssh2 only) */
92int ciphers_valid(const char *names);
93
Damien Miller5428f641999-11-25 11:54:57 +110094/*
95 * Selects the cipher to use and sets the key. If for_encryption is true,
96 * the key is setup for encryption; otherwise it is setup for decryption.
97 */
Damien Miller4af51302000-04-16 11:18:38 +100098void
Damien Miller95def091999-11-25 00:26:21 +110099cipher_set_key(CipherContext * context, int cipher,
Damien Miller1383bd82000-04-06 12:32:37 +1000100 const unsigned char *key, int keylen);
Damien Miller4af51302000-04-16 11:18:38 +1000101void
Damien Millerb38eff82000-04-01 11:09:21 +1000102cipher_set_key_iv(CipherContext * context, int cipher,
Damien Miller4af51302000-04-16 11:18:38 +1000103 const unsigned char *key, int keylen,
Damien Millerb38eff82000-04-01 11:09:21 +1000104 const unsigned char *iv, int ivlen);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000105
Damien Miller5428f641999-11-25 11:54:57 +1100106/*
107 * Sets key for the cipher by computing the MD5 checksum of the passphrase,
108 * and using the resulting 16 bytes as the key.
109 */
Damien Miller4af51302000-04-16 11:18:38 +1000110void
Damien Miller95def091999-11-25 00:26:21 +1100111cipher_set_key_string(CipherContext * context, int cipher,
Damien Miller1383bd82000-04-06 12:32:37 +1000112 const char *passphrase);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000113
114/* Encrypts data using the cipher. */
Damien Miller4af51302000-04-16 11:18:38 +1000115void
Damien Miller95def091999-11-25 00:26:21 +1100116cipher_encrypt(CipherContext * context, unsigned char *dest,
117 const unsigned char *src, unsigned int len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000118
119/* Decrypts data using the cipher. */
Damien Miller4af51302000-04-16 11:18:38 +1000120void
Damien Miller95def091999-11-25 00:26:21 +1100121cipher_decrypt(CipherContext * context, unsigned char *dest,
122 const unsigned char *src, unsigned int len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000123
Damien Miller95def091999-11-25 00:26:21 +1100124#endif /* CIPHER_H */