Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2 | * |
| 3 | * cipher.h |
| 4 | * |
| 5 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
| 6 | * |
| 7 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 8 | * All rights reserved |
| 9 | * |
| 10 | * Created: Wed Apr 19 16:50:42 1995 ylo |
| 11 | * |
| 12 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 13 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame^] | 14 | /* RCSID("$Id: cipher.h,v 1.7 2000/04/01 01:09:23 damien Exp $"); */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 15 | |
| 16 | #ifndef CIPHER_H |
| 17 | #define CIPHER_H |
| 18 | |
Damien Miller | 7e8e820 | 1999-11-16 13:37:16 +1100 | [diff] [blame] | 19 | #include "config.h" |
| 20 | |
Damien Miller | 7f6ea02 | 1999-10-28 13:25:17 +1000 | [diff] [blame] | 21 | #ifdef HAVE_OPENSSL |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 22 | #include <openssl/des.h> |
| 23 | #include <openssl/blowfish.h> |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame^] | 24 | #include <openssl/rc4.h> |
| 25 | #include <openssl/cast.h> |
Damien Miller | 7f6ea02 | 1999-10-28 13:25:17 +1000 | [diff] [blame] | 26 | #endif |
| 27 | #ifdef HAVE_SSL |
| 28 | #include <ssl/des.h> |
| 29 | #include <ssl/blowfish.h> |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame^] | 30 | #include <ssl/rc4.h> |
| 31 | #include <ssl/cast.h> |
Damien Miller | 7f6ea02 | 1999-10-28 13:25:17 +1000 | [diff] [blame] | 32 | #endif |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 33 | |
| 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 Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 36 | #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 Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 43 | #define SSH_CIPHER_BLOWFISH 6 |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame^] | 44 | #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 Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 51 | |
| 52 | typedef struct { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 53 | 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 Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame^] | 66 | struct { |
| 67 | CAST_KEY key; |
| 68 | unsigned char iv[8]; |
| 69 | } cast; |
| 70 | RC4_KEY rc4; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 71 | } u; |
| 72 | } CipherContext; |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 73 | /* |
| 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 Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 78 | unsigned int cipher_mask(); |
| 79 | |
| 80 | /* Returns the name of the cipher. */ |
| 81 | const char *cipher_name(int cipher); |
| 82 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 83 | /* |
| 84 | * Parses the name of the cipher. Returns the number of the corresponding |
| 85 | * cipher, or -1 on error. |
| 86 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 87 | int cipher_number(const char *name); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 88 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 89 | /* |
| 90 | * Selects the cipher to use and sets the key. If for_encryption is true, |
| 91 | * the key is setup for encryption; otherwise it is setup for decryption. |
| 92 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 93 | void |
| 94 | cipher_set_key(CipherContext * context, int cipher, |
| 95 | const unsigned char *key, int keylen, int for_encryption); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame^] | 96 | void |
| 97 | cipher_set_key_iv(CipherContext * context, int cipher, |
| 98 | const unsigned char *key, int keylen, |
| 99 | const unsigned char *iv, int ivlen); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 100 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 101 | /* |
| 102 | * Sets key for the cipher by computing the MD5 checksum of the passphrase, |
| 103 | * and using the resulting 16 bytes as the key. |
| 104 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 105 | void |
| 106 | cipher_set_key_string(CipherContext * context, int cipher, |
| 107 | const char *passphrase, int for_encryption); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 108 | |
| 109 | /* Encrypts data using the cipher. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 110 | void |
| 111 | cipher_encrypt(CipherContext * context, unsigned char *dest, |
| 112 | const unsigned char *src, unsigned int len); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 113 | |
| 114 | /* Decrypts data using the cipher. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 115 | void |
| 116 | cipher_decrypt(CipherContext * context, unsigned char *dest, |
| 117 | const unsigned char *src, unsigned int len); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 118 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 119 | #endif /* CIPHER_H */ |