| 1 |
maya |
3227 |
/*
|
| 2 |
|
|
Copyright (c) 1998-2001, Robert O'Callahan
|
| 3 |
|
|
All rights reserved.
|
| 4 |
|
|
|
| 5 |
|
|
Redistribution and use in source and binary forms, with or without modification,
|
| 6 |
|
|
are permitted provided that the following conditions are met:
|
| 7 |
|
|
|
| 8 |
|
|
Redistributions of source code must retain the above copyright notice, this list of
|
| 9 |
|
|
conditions and the following disclaimer.
|
| 10 |
|
|
|
| 11 |
|
|
Redistributions in binary form must reproduce the above copyright notice, this list
|
| 12 |
|
|
of conditions and the following disclaimer in the documentation and/or other materials
|
| 13 |
|
|
provided with the distribution.
|
| 14 |
|
|
|
| 15 |
|
|
The name of Robert O'Callahan may not be used to endorse or promote products derived from
|
| 16 |
|
|
this software without specific prior written permission.
|
| 17 |
|
|
|
| 18 |
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND
|
| 19 |
|
|
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
| 20 |
|
|
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
| 21 |
|
|
THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
| 22 |
|
|
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
| 23 |
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
| 24 |
|
|
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
| 25 |
|
|
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
| 26 |
|
|
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
| 27 |
|
|
*/
|
| 28 |
|
|
|
| 29 |
|
|
/*
|
| 30 |
|
|
This code is copyright (C) 1998-1999 Robert O'Callahan.
|
| 31 |
|
|
See LICENSE.TXT for the license.
|
| 32 |
|
|
*/
|
| 33 |
|
|
|
| 34 |
|
|
#ifndef __CRYPT_H
|
| 35 |
|
|
#define __CRYPT_H
|
| 36 |
|
|
|
| 37 |
|
|
#include <openssl/rsa.h>
|
| 38 |
|
|
#include <openssl/des.h>
|
| 39 |
|
|
#include <openssl/idea.h>
|
| 40 |
|
|
#include <openssl/rc4.h>
|
| 41 |
|
|
#include <openssl/blowfish.h>
|
| 42 |
|
|
|
| 43 |
|
|
#define SSH_SESSION_KEY_LENGTH 32
|
| 44 |
|
|
#define SSH_RSA_CHALLENGE_LENGTH 32
|
| 45 |
|
|
#define SSH_COOKIE_LENGTH 8
|
| 46 |
|
|
#define SSH2_COOKIE_LENGTH 16
|
| 47 |
|
|
|
| 48 |
|
|
#define CRYPT_KEY_LENGTH 32
|
| 49 |
|
|
#define COOKIE_LENGTH 16
|
| 50 |
|
|
|
| 51 |
|
|
typedef struct {
|
| 52 |
|
|
DES_key_schedule k1;
|
| 53 |
|
|
DES_key_schedule k2;
|
| 54 |
|
|
DES_key_schedule k3;
|
| 55 |
|
|
DES_cblock ivec1;
|
| 56 |
|
|
DES_cblock ivec2;
|
| 57 |
|
|
DES_cblock ivec3;
|
| 58 |
|
|
} Cipher3DESState;
|
| 59 |
|
|
|
| 60 |
|
|
typedef struct {
|
| 61 |
|
|
IDEA_KEY_SCHEDULE k;
|
| 62 |
|
|
unsigned char ivec[8];
|
| 63 |
|
|
} CipherIDEAState;
|
| 64 |
|
|
|
| 65 |
|
|
typedef struct {
|
| 66 |
|
|
DES_key_schedule k;
|
| 67 |
|
|
DES_cblock ivec;
|
| 68 |
|
|
} CipherDESState;
|
| 69 |
|
|
|
| 70 |
|
|
typedef struct {
|
| 71 |
|
|
RC4_KEY k;
|
| 72 |
|
|
} CipherRC4State;
|
| 73 |
|
|
|
| 74 |
|
|
typedef struct {
|
| 75 |
|
|
BF_KEY k;
|
| 76 |
|
|
unsigned char ivec[8];
|
| 77 |
|
|
} CipherBlowfishState;
|
| 78 |
|
|
|
| 79 |
|
|
typedef struct {
|
| 80 |
|
|
uint32 FAR * h;
|
| 81 |
|
|
uint32 n;
|
| 82 |
|
|
} CRYPTDetectAttack;
|
| 83 |
|
|
|
| 84 |
|
|
typedef struct {
|
| 85 |
maya |
4307 |
RSA *RSA_key;
|
| 86 |
maya |
3227 |
} CRYPTPublicKey;
|
| 87 |
|
|
|
| 88 |
|
|
typedef union {
|
| 89 |
|
|
Cipher3DESState c3DES;
|
| 90 |
|
|
CipherIDEAState cIDEA;
|
| 91 |
|
|
CipherDESState cDES;
|
| 92 |
|
|
CipherRC4State cRC4;
|
| 93 |
|
|
CipherBlowfishState cBlowfish;
|
| 94 |
|
|
} CRYPTCipherState;
|
| 95 |
|
|
|
| 96 |
|
|
typedef void (* CRYPTCryptFun)(PTInstVar pvar, unsigned char FAR * buf, int bytes);
|
| 97 |
|
|
|
| 98 |
|
|
typedef struct {
|
| 99 |
|
|
CRYPTDetectAttack detect_attack_statics;
|
| 100 |
|
|
|
| 101 |
|
|
CRYPTPublicKey server_key;
|
| 102 |
|
|
CRYPTPublicKey host_key;
|
| 103 |
|
|
|
| 104 |
|
|
char server_cookie[COOKIE_LENGTH];
|
| 105 |
|
|
char client_cookie[COOKIE_LENGTH];
|
| 106 |
|
|
|
| 107 |
|
|
int supported_sender_ciphers;
|
| 108 |
|
|
int supported_receiver_ciphers;
|
| 109 |
|
|
int sender_cipher;
|
| 110 |
|
|
int receiver_cipher;
|
| 111 |
|
|
char sender_cipher_key[CRYPT_KEY_LENGTH];
|
| 112 |
|
|
char receiver_cipher_key[CRYPT_KEY_LENGTH];
|
| 113 |
|
|
CRYPTCryptFun encrypt;
|
| 114 |
|
|
CRYPTCryptFun decrypt;
|
| 115 |
|
|
CRYPTCipherState enc;
|
| 116 |
|
|
CRYPTCipherState dec;
|
| 117 |
|
|
} CRYPTState;
|
| 118 |
|
|
|
| 119 |
|
|
void CRYPT_init(PTInstVar pvar);
|
| 120 |
|
|
/* this function is called during 'slack time' while we wait for a response
|
| 121 |
|
|
from the server. Therefore we have some time available to do some
|
| 122 |
|
|
moderately expensive computations. */
|
| 123 |
|
|
void CRYPT_initialize_random_numbers(PTInstVar pvar);
|
| 124 |
|
|
void CRYPT_set_random_data(PTInstVar pvar, unsigned char FAR * buf, int bytes);
|
| 125 |
|
|
void CRYPT_end(PTInstVar pvar);
|
| 126 |
|
|
|
| 127 |
|
|
void CRYPT_get_cipher_info(PTInstVar pvar, char FAR * dest, int len);
|
| 128 |
|
|
void CRYPT_get_server_key_info(PTInstVar pvar, char FAR * dest, int len);
|
| 129 |
|
|
|
| 130 |
|
|
void CRYPT_set_server_cookie(PTInstVar pvar, unsigned char FAR * cookie);
|
| 131 |
|
|
void CRYPT_set_client_cookie(PTInstVar pvar, unsigned char FAR * cookie);
|
| 132 |
|
|
#define CRYPT_get_server_cookie(pvar) ((pvar)->crypt_state.server_cookie)
|
| 133 |
|
|
|
| 134 |
|
|
void CRYPT_free_public_key(CRYPTPublicKey FAR * key);
|
| 135 |
|
|
|
| 136 |
|
|
BOOL CRYPT_set_server_RSA_key(PTInstVar pvar,
|
| 137 |
|
|
int bits, unsigned char FAR * exp, unsigned char FAR * mod);
|
| 138 |
|
|
BOOL CRYPT_set_host_RSA_key(PTInstVar pvar,
|
| 139 |
|
|
int bits, unsigned char FAR * exp, unsigned char FAR * mod);
|
| 140 |
|
|
int CRYPT_get_encrypted_session_key_len(PTInstVar pvar);
|
| 141 |
|
|
int CRYPT_choose_session_key(PTInstVar pvar, unsigned char FAR * encrypted_key_buf);
|
| 142 |
|
|
BOOL CRYPT_start_encryption(PTInstVar pvar, int sender_flag, int receiver_flag);
|
| 143 |
|
|
int CRYPT_generate_RSA_challenge_response(PTInstVar pvar, unsigned char FAR * challenge,
|
| 144 |
|
|
int challenge_len, unsigned char FAR * response);
|
| 145 |
|
|
|
| 146 |
|
|
int CRYPT_get_receiver_MAC_size(PTInstVar pvar);
|
| 147 |
|
|
BOOL CRYPT_verify_receiver_MAC(PTInstVar pvar, uint32 sequence_number,
|
| 148 |
|
|
char FAR * data, int len, char FAR * MAC);
|
| 149 |
|
|
int CRYPT_get_sender_MAC_size(PTInstVar pvar);
|
| 150 |
|
|
|
| 151 |
|
|
BOOL CRYPT_build_sender_MAC(PTInstVar pvar, uint32 sequence_number,
|
| 152 |
|
|
char FAR * data, int len, char FAR * MAC);
|
| 153 |
|
|
|
| 154 |
|
|
BOOL CRYPT_set_supported_ciphers(PTInstVar pvar, int sender_ciphers, int receiver_ciphers);
|
| 155 |
|
|
BOOL CRYPT_choose_ciphers(PTInstVar pvar);
|
| 156 |
|
|
#define CRYPT_get_sender_cipher(pvar) ((pvar)->crypt_state.sender_cipher)
|
| 157 |
|
|
#define CRYPT_get_receiver_cipher(pvar) ((pvar)->crypt_state.receiver_cipher)
|
| 158 |
|
|
int CRYPT_get_decryption_block_size(PTInstVar pvar);
|
| 159 |
|
|
int CRYPT_get_encryption_block_size(PTInstVar pvar);
|
| 160 |
|
|
#define CRYPT_encrypt(pvar, buf, bytes) \
|
| 161 |
|
|
((pvar)->crypt_state.encrypt((pvar), (buf), (bytes)))
|
| 162 |
|
|
#define CRYPT_decrypt(pvar, buf, bytes) \
|
| 163 |
|
|
((pvar)->crypt_state.decrypt((pvar), (buf), (bytes)))
|
| 164 |
|
|
|
| 165 |
|
|
BOOL CRYPT_detect_attack(PTInstVar pvar, unsigned char FAR * buf, int bytes);
|
| 166 |
|
|
int CRYPT_passphrase_decrypt(int cipher, char FAR * passphrase, char FAR * buf, int len);
|
| 167 |
|
|
RSA FAR *make_key(PTInstVar pvar,
|
| 168 |
maya |
4307 |
int bits, unsigned char FAR * exp,
|
| 169 |
|
|
unsigned char FAR * mod);
|
| 170 |
maya |
3227 |
|
| 171 |
|
|
#endif
|