| 1 | #include <string.h> |
| 2 | |
| 3 | #include "puttymem.h" |
| 4 | |
| 5 | /* |
| 6 | * Useful thing. |
| 7 | */ |
| 8 | #ifndef lenof |
| 9 | #define lenof(x) ( (sizeof((x))) / (sizeof(*(x)))) |
| 10 | #endif |
| 11 | |
| 12 | #define SSH_CIPHER_IDEA 1 |
| 13 | #define SSH_CIPHER_DES 2 |
| 14 | #define SSH_CIPHER_3DES 3 |
| 15 | #define SSH_CIPHER_BLOWFISH 6 |
| 16 | |
| 17 | #ifdef MSCRYPTOAPI |
| 18 | #define APIEXTRA 8 |
| 19 | #else |
| 20 | #define APIEXTRA 0 |
| 21 | #endif |
| 22 | |
| 23 | #ifndef BIGNUM_INTERNAL |
| 24 | typedef void *Bignum; |
| 25 | #endif |
| 26 | |
| 27 | struct RSAKey { |
| 28 | int bits; |
| 29 | int bytes; |
| 30 | #ifdef MSCRYPTOAPI |
| 31 | unsigned long exponent; |
| 32 | unsigned char *modulus; |
| 33 | #else |
| 34 | Bignum modulus; |
| 35 | Bignum exponent; |
| 36 | Bignum private_exponent; |
| 37 | #endif |
| 38 | char *comment; |
| 39 | }; |
| 40 | |
| 41 | struct RSAAux { |
| 42 | Bignum p; |
| 43 | Bignum q; |
| 44 | Bignum iqmp; |
| 45 | }; |
| 46 | |
| 47 | int makekey(unsigned char *data, struct RSAKey *result, |
| 48 | unsigned char **keystr, int order); |
| 49 | int makeprivate(unsigned char *data, struct RSAKey *result); |
| 50 | void rsaencrypt(unsigned char *data, int length, struct RSAKey *key); |
| 51 | Bignum rsadecrypt(Bignum input, struct RSAKey *key); |
| 52 | void rsasign(unsigned char *data, int length, struct RSAKey *key); |
| 53 | void rsasanitise(struct RSAKey *key); |
| 54 | int rsastr_len(struct RSAKey *key); |
| 55 | void rsastr_fmt(char *str, struct RSAKey *key); |
| 56 | void rsa_fingerprint(char *str, int len, struct RSAKey *key); |
| 57 | void freersakey(struct RSAKey *key); |
| 58 | |
| 59 | typedef unsigned int word32; |
| 60 | typedef unsigned int uint32; |
| 61 | |
| 62 | unsigned long crc32(const void *s, size_t len); |
| 63 | |
| 64 | typedef struct { |
| 65 | uint32 h[4]; |
| 66 | } MD5_Core_State; |
| 67 | |
| 68 | struct MD5Context { |
| 69 | #ifdef MSCRYPTOAPI |
| 70 | unsigned long hHash; |
| 71 | #else |
| 72 | MD5_Core_State core; |
| 73 | unsigned char block[64]; |
| 74 | int blkused; |
| 75 | uint32 lenhi, lenlo; |
| 76 | #endif |
| 77 | }; |
| 78 | |
| 79 | void MD5Init(struct MD5Context *context); |
| 80 | void MD5Update(struct MD5Context *context, unsigned char const *buf, |
| 81 | unsigned len); |
| 82 | void MD5Final(unsigned char digest[16], struct MD5Context *context); |
| 83 | |
| 84 | typedef struct { |
| 85 | uint32 h[5]; |
| 86 | unsigned char block[64]; |
| 87 | int blkused; |
| 88 | uint32 lenhi, lenlo; |
| 89 | } SHA_State; |
| 90 | |
| 91 | void SHA_Init(SHA_State *s); |
| 92 | void SHA_Bytes(SHA_State *s, void *p, int len); |
| 93 | void SHA_Final(SHA_State *s, unsigned char *output); |
| 94 | void SHA_Simple(void *p, int len, unsigned char *output); |
| 95 | |
| 96 | struct ssh_cipher { |
| 97 | void (*sesskey)(unsigned char *key); /* for ssh 1 */ |
| 98 | void (*setcsiv)(unsigned char *key); /* for ssh 2 */ |
| 99 | void (*setcskey)(unsigned char *key); /* for ssh 2 */ |
| 100 | void (*setsciv)(unsigned char *key); /* for ssh 2 */ |
| 101 | void (*setsckey)(unsigned char *key); /* for ssh 2 */ |
| 102 | void (*encrypt)(unsigned char *blk, int len); |
| 103 | void (*decrypt)(unsigned char *blk, int len); |
| 104 | char *name; |
| 105 | int blksize; |
| 106 | int keylen; |
| 107 | }; |
| 108 | |
| 109 | struct ssh_mac { |
| 110 | void (*setcskey)(unsigned char *key); |
| 111 | void (*setsckey)(unsigned char *key); |
| 112 | void (*generate)(unsigned char *blk, int len, unsigned long seq); |
| 113 | int (*verify)(unsigned char *blk, int len, unsigned long seq); |
| 114 | char *name; |
| 115 | int len; |
| 116 | }; |
| 117 | |
| 118 | struct ssh_kex { |
| 119 | /* |
| 120 | * Plugging in another KEX algorithm requires structural chaos, |
| 121 | * so it's hard to abstract them into nice little structures |
| 122 | * like this. Hence, for the moment, this is just a |
| 123 | * placeholder. I claim justification in the fact that OpenSSH |
| 124 | * does this too :-) |
| 125 | */ |
| 126 | char *name; |
| 127 | }; |
| 128 | |
| 129 | struct ssh_signkey { |
| 130 | void *(*newkey)(char *data, int len); |
| 131 | void (*freekey)(void *key); |
| 132 | char *(*fmtkey)(void *key); |
| 133 | char *(*fingerprint)(void *key); |
| 134 | int (*verifysig)(void *key, char *sig, int siglen, |
| 135 | char *data, int datalen); |
| 136 | int (*sign)(void *key, char *sig, int siglen, |
| 137 | char *data, int datalen); |
| 138 | char *name; |
| 139 | char *keytype; /* for host key cache */ |
| 140 | }; |
| 141 | |
| 142 | struct ssh_compress { |
| 143 | char *name; |
| 144 | void (*compress_init)(void); |
| 145 | int (*compress)(unsigned char *block, int len, |
| 146 | unsigned char **outblock, int *outlen); |
| 147 | void (*decompress_init)(void); |
| 148 | int (*decompress)(unsigned char *block, int len, |
| 149 | unsigned char **outblock, int *outlen); |
| 150 | }; |
| 151 | |
| 152 | #ifndef MSCRYPTOAPI |
| 153 | void SHATransform(word32 *digest, word32 *data); |
| 154 | #endif |
| 155 | |
| 156 | int random_byte(void); |
| 157 | void random_add_noise(void *noise, int length); |
| 158 | void random_add_heavynoise(void *noise, int length); |
| 159 | |
| 160 | void logevent (char *); |
| 161 | |
| 162 | Bignum copybn(Bignum b); |
| 163 | Bignum bn_power_2(int n); |
| 164 | void bn_restore_invariant(Bignum b); |
| 165 | Bignum bignum_from_short(unsigned short n); |
| 166 | void freebn(Bignum b); |
| 167 | Bignum modpow(Bignum base, Bignum exp, Bignum mod); |
| 168 | Bignum modmul(Bignum a, Bignum b, Bignum mod); |
| 169 | void decbn(Bignum n); |
| 170 | extern Bignum Zero, One; |
| 171 | Bignum bignum_from_bytes(unsigned char *data, int nbytes); |
| 172 | int ssh1_read_bignum(unsigned char *data, Bignum *result); |
| 173 | int ssh1_bignum_bitcount(Bignum bn); |
| 174 | int ssh1_bignum_length(Bignum bn); |
| 175 | int bignum_byte(Bignum bn, int i); |
| 176 | int bignum_bit(Bignum bn, int i); |
| 177 | void bignum_set_bit(Bignum bn, int i, int value); |
| 178 | int ssh1_write_bignum(void *data, Bignum bn); |
| 179 | Bignum biggcd(Bignum a, Bignum b); |
| 180 | unsigned short bignum_mod_short(Bignum number, unsigned short modulus); |
| 181 | Bignum bignum_add_long(Bignum number, unsigned long addend); |
| 182 | Bignum bigmul(Bignum a, Bignum b); |
| 183 | Bignum modinv(Bignum number, Bignum modulus); |
| 184 | Bignum bignum_bitmask(Bignum number); |
| 185 | Bignum bignum_rshift(Bignum number, int shift); |
| 186 | int bignum_cmp(Bignum a, Bignum b); |
| 187 | char *bignum_decimal(Bignum x); |
| 188 | |
| 189 | void dh_setup_group1(void); |
| 190 | void dh_cleanup(void); |
| 191 | Bignum dh_create_e(void); |
| 192 | Bignum dh_find_K(Bignum f); |
| 193 | |
| 194 | int loadrsakey(char *filename, struct RSAKey *key, struct RSAAux *aux, |
| 195 | char *passphrase); |
| 196 | int rsakey_encrypted(char *filename, char **comment); |
| 197 | |
| 198 | int saversakey(char *filename, struct RSAKey *key, struct RSAAux *aux, |
| 199 | char *passphrase); |
| 200 | |
| 201 | void des3_decrypt_pubkey(unsigned char *key, |
| 202 | unsigned char *blk, int len); |
| 203 | void des3_encrypt_pubkey(unsigned char *key, |
| 204 | unsigned char *blk, int len); |
| 205 | |
| 206 | /* |
| 207 | * For progress updates in the key generation utility. |
| 208 | */ |
| 209 | typedef void (*progfn_t)(void *param, int phase, int progress); |
| 210 | |
| 211 | int rsa_generate(struct RSAKey *key, struct RSAAux *aux, int bits, |
| 212 | progfn_t pfn, void *pfnparam); |
| 213 | Bignum primegen(int bits, int modulus, int residue, |
| 214 | int phase, progfn_t pfn, void *pfnparam); |
| 215 | |
| 216 | /* |
| 217 | * zlib compression. |
| 218 | */ |
| 219 | void zlib_compress_init(void); |
| 220 | void zlib_decompress_init(void); |
| 221 | int zlib_compress_block(unsigned char *block, int len, |
| 222 | unsigned char **outblock, int *outlen); |
| 223 | int zlib_decompress_block(unsigned char *block, int len, |
| 224 | unsigned char **outblock, int *outlen); |