| 1 | #include <string.h> |
| 2 | |
| 3 | /* |
| 4 | * Useful thing. |
| 5 | */ |
| 6 | #ifndef lenof |
| 7 | #define lenof(x) ( (sizeof((x))) / (sizeof(*(x)))) |
| 8 | #endif |
| 9 | |
| 10 | #define SSH_CIPHER_IDEA 1 |
| 11 | #define SSH_CIPHER_DES 2 |
| 12 | #define SSH_CIPHER_3DES 3 |
| 13 | #define SSH_CIPHER_BLOWFISH 6 |
| 14 | |
| 15 | #ifdef MSCRYPTOAPI |
| 16 | #define APIEXTRA 8 |
| 17 | #else |
| 18 | #define APIEXTRA 0 |
| 19 | #endif |
| 20 | |
| 21 | /* |
| 22 | * A Bignum is stored as a sequence of `unsigned short' words. The |
| 23 | * first tells how many remain; the remaining ones are digits, LS |
| 24 | * first. |
| 25 | */ |
| 26 | typedef unsigned short *Bignum; |
| 27 | |
| 28 | struct RSAKey { |
| 29 | int bits; |
| 30 | int bytes; |
| 31 | #ifdef MSCRYPTOAPI |
| 32 | unsigned long exponent; |
| 33 | unsigned char *modulus; |
| 34 | #else |
| 35 | Bignum modulus; |
| 36 | Bignum exponent; |
| 37 | Bignum private_exponent; |
| 38 | #endif |
| 39 | char *comment; |
| 40 | }; |
| 41 | |
| 42 | int makekey(unsigned char *data, struct RSAKey *result, |
| 43 | unsigned char **keystr, int order); |
| 44 | int makeprivate(unsigned char *data, struct RSAKey *result); |
| 45 | void rsaencrypt(unsigned char *data, int length, struct RSAKey *key); |
| 46 | Bignum rsadecrypt(Bignum input, struct RSAKey *key); |
| 47 | void rsasign(unsigned char *data, int length, struct RSAKey *key); |
| 48 | void rsasanitise(struct RSAKey *key); |
| 49 | int rsastr_len(struct RSAKey *key); |
| 50 | void rsastr_fmt(char *str, struct RSAKey *key); |
| 51 | void rsa_fingerprint(char *str, int len, struct RSAKey *key); |
| 52 | void freersakey(struct RSAKey *key); |
| 53 | |
| 54 | typedef unsigned int word32; |
| 55 | typedef unsigned int uint32; |
| 56 | |
| 57 | unsigned long crc32(const void *s, size_t len); |
| 58 | |
| 59 | typedef struct { |
| 60 | uint32 h[4]; |
| 61 | } MD5_Core_State; |
| 62 | |
| 63 | struct MD5Context { |
| 64 | #ifdef MSCRYPTOAPI |
| 65 | unsigned long hHash; |
| 66 | #else |
| 67 | MD5_Core_State core; |
| 68 | unsigned char block[64]; |
| 69 | int blkused; |
| 70 | uint32 lenhi, lenlo; |
| 71 | #endif |
| 72 | }; |
| 73 | |
| 74 | void MD5Init(struct MD5Context *context); |
| 75 | void MD5Update(struct MD5Context *context, unsigned char const *buf, |
| 76 | unsigned len); |
| 77 | void MD5Final(unsigned char digest[16], struct MD5Context *context); |
| 78 | |
| 79 | typedef struct { |
| 80 | uint32 h[5]; |
| 81 | unsigned char block[64]; |
| 82 | int blkused; |
| 83 | uint32 lenhi, lenlo; |
| 84 | } SHA_State; |
| 85 | |
| 86 | void SHA_Init(SHA_State *s); |
| 87 | void SHA_Bytes(SHA_State *s, void *p, int len); |
| 88 | void SHA_Final(SHA_State *s, unsigned char *output); |
| 89 | void SHA_Simple(void *p, int len, unsigned char *output); |
| 90 | |
| 91 | struct ssh_cipher { |
| 92 | void (*sesskey)(unsigned char *key); /* for ssh 1 */ |
| 93 | void (*setcsiv)(unsigned char *key); /* for ssh 2 */ |
| 94 | void (*setcskey)(unsigned char *key); /* for ssh 2 */ |
| 95 | void (*setsciv)(unsigned char *key); /* for ssh 2 */ |
| 96 | void (*setsckey)(unsigned char *key); /* for ssh 2 */ |
| 97 | void (*encrypt)(unsigned char *blk, int len); |
| 98 | void (*decrypt)(unsigned char *blk, int len); |
| 99 | char *name; |
| 100 | int blksize; |
| 101 | }; |
| 102 | |
| 103 | struct ssh_mac { |
| 104 | void (*setcskey)(unsigned char *key); |
| 105 | void (*setsckey)(unsigned char *key); |
| 106 | void (*generate)(unsigned char *blk, int len, unsigned long seq); |
| 107 | int (*verify)(unsigned char *blk, int len, unsigned long seq); |
| 108 | char *name; |
| 109 | int len; |
| 110 | }; |
| 111 | |
| 112 | struct ssh_kex { |
| 113 | /* |
| 114 | * Plugging in another KEX algorithm requires structural chaos, |
| 115 | * so it's hard to abstract them into nice little structures |
| 116 | * like this. Hence, for the moment, this is just a |
| 117 | * placeholder. I claim justification in the fact that OpenSSH |
| 118 | * does this too :-) |
| 119 | */ |
| 120 | char *name; |
| 121 | }; |
| 122 | |
| 123 | struct ssh_hostkey { |
| 124 | void (*setkey)(char *data, int len); |
| 125 | char *(*fmtkey)(void); |
| 126 | char *(*fingerprint)(void); |
| 127 | int (*verifysig)(char *sig, int siglen, char *data, int datalen); |
| 128 | char *name; |
| 129 | char *keytype; /* for host key cache */ |
| 130 | }; |
| 131 | |
| 132 | struct ssh_compress { |
| 133 | char *name; |
| 134 | }; |
| 135 | |
| 136 | #ifndef MSCRYPTOAPI |
| 137 | void SHATransform(word32 *digest, word32 *data); |
| 138 | #endif |
| 139 | |
| 140 | int random_byte(void); |
| 141 | void random_add_noise(void *noise, int length); |
| 142 | |
| 143 | void logevent (char *); |
| 144 | |
| 145 | Bignum newbn(int length); |
| 146 | Bignum copybn(Bignum b); |
| 147 | void freebn(Bignum b); |
| 148 | void modpow(Bignum base, Bignum exp, Bignum mod, Bignum result); |
| 149 | void modmul(Bignum a, Bignum b, Bignum mod, Bignum result); |
| 150 | void decbn(Bignum n); |
| 151 | extern Bignum Zero, One; |
| 152 | int ssh1_read_bignum(unsigned char *data, Bignum *result); |
| 153 | int ssh1_bignum_bitcount(Bignum bn); |
| 154 | int ssh1_bignum_length(Bignum bn); |
| 155 | int bignum_byte(Bignum bn, int i); |
| 156 | int ssh1_write_bignum(void *data, Bignum bn); |
| 157 | |
| 158 | Bignum dh_create_e(void); |
| 159 | Bignum dh_find_K(Bignum f); |
| 160 | |
| 161 | int loadrsakey(char *filename, struct RSAKey *key, char *passphrase); |
| 162 | int rsakey_encrypted(char *filename, char **comment); |
| 163 | |
| 164 | void des3_decrypt_pubkey(unsigned char *key, |
| 165 | unsigned char *blk, int len); |