| 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 | struct RSAAux { |
| 43 | Bignum p; |
| 44 | Bignum q; |
| 45 | Bignum iqmp; |
| 46 | }; |
| 47 | |
| 48 | int makekey(unsigned char *data, struct RSAKey *result, |
| 49 | unsigned char **keystr, int order); |
| 50 | int makeprivate(unsigned char *data, struct RSAKey *result); |
| 51 | void rsaencrypt(unsigned char *data, int length, struct RSAKey *key); |
| 52 | Bignum rsadecrypt(Bignum input, struct RSAKey *key); |
| 53 | void rsasign(unsigned char *data, int length, struct RSAKey *key); |
| 54 | void rsasanitise(struct RSAKey *key); |
| 55 | int rsastr_len(struct RSAKey *key); |
| 56 | void rsastr_fmt(char *str, struct RSAKey *key); |
| 57 | void rsa_fingerprint(char *str, int len, struct RSAKey *key); |
| 58 | void freersakey(struct RSAKey *key); |
| 59 | |
| 60 | typedef unsigned int word32; |
| 61 | typedef unsigned int uint32; |
| 62 | |
| 63 | unsigned long crc32(const void *s, size_t len); |
| 64 | |
| 65 | typedef struct { |
| 66 | uint32 h[4]; |
| 67 | } MD5_Core_State; |
| 68 | |
| 69 | struct MD5Context { |
| 70 | #ifdef MSCRYPTOAPI |
| 71 | unsigned long hHash; |
| 72 | #else |
| 73 | MD5_Core_State core; |
| 74 | unsigned char block[64]; |
| 75 | int blkused; |
| 76 | uint32 lenhi, lenlo; |
| 77 | #endif |
| 78 | }; |
| 79 | |
| 80 | void MD5Init(struct MD5Context *context); |
| 81 | void MD5Update(struct MD5Context *context, unsigned char const *buf, |
| 82 | unsigned len); |
| 83 | void MD5Final(unsigned char digest[16], struct MD5Context *context); |
| 84 | |
| 85 | typedef struct { |
| 86 | uint32 h[5]; |
| 87 | unsigned char block[64]; |
| 88 | int blkused; |
| 89 | uint32 lenhi, lenlo; |
| 90 | } SHA_State; |
| 91 | |
| 92 | void SHA_Init(SHA_State *s); |
| 93 | void SHA_Bytes(SHA_State *s, void *p, int len); |
| 94 | void SHA_Final(SHA_State *s, unsigned char *output); |
| 95 | void SHA_Simple(void *p, int len, unsigned char *output); |
| 96 | |
| 97 | struct ssh_cipher { |
| 98 | void (*sesskey)(unsigned char *key); /* for ssh 1 */ |
| 99 | void (*setcsiv)(unsigned char *key); /* for ssh 2 */ |
| 100 | void (*setcskey)(unsigned char *key); /* for ssh 2 */ |
| 101 | void (*setsciv)(unsigned char *key); /* for ssh 2 */ |
| 102 | void (*setsckey)(unsigned char *key); /* for ssh 2 */ |
| 103 | void (*encrypt)(unsigned char *blk, int len); |
| 104 | void (*decrypt)(unsigned char *blk, int len); |
| 105 | char *name; |
| 106 | int blksize; |
| 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_hostkey { |
| 130 | void (*setkey)(char *data, int len); |
| 131 | char *(*fmtkey)(void); |
| 132 | char *(*fingerprint)(void); |
| 133 | int (*verifysig)(char *sig, int siglen, char *data, int datalen); |
| 134 | char *name; |
| 135 | char *keytype; /* for host key cache */ |
| 136 | }; |
| 137 | |
| 138 | struct ssh_compress { |
| 139 | char *name; |
| 140 | }; |
| 141 | |
| 142 | #ifndef MSCRYPTOAPI |
| 143 | void SHATransform(word32 *digest, word32 *data); |
| 144 | #endif |
| 145 | |
| 146 | int random_byte(void); |
| 147 | void random_add_noise(void *noise, int length); |
| 148 | void random_add_heavynoise(void *noise, int length); |
| 149 | |
| 150 | void logevent (char *); |
| 151 | |
| 152 | Bignum newbn(int length); |
| 153 | Bignum copybn(Bignum b); |
| 154 | Bignum bignum_from_short(unsigned short n); |
| 155 | void freebn(Bignum b); |
| 156 | void modpow(Bignum base, Bignum exp, Bignum mod, Bignum result); |
| 157 | void modmul(Bignum a, Bignum b, Bignum mod, Bignum result); |
| 158 | void decbn(Bignum n); |
| 159 | extern Bignum Zero, One; |
| 160 | int ssh1_read_bignum(unsigned char *data, Bignum *result); |
| 161 | int ssh1_bignum_bitcount(Bignum bn); |
| 162 | int ssh1_bignum_length(Bignum bn); |
| 163 | int bignum_byte(Bignum bn, int i); |
| 164 | int bignum_bit(Bignum bn, int i); |
| 165 | void bignum_set_bit(Bignum bn, int i, int value); |
| 166 | int ssh1_write_bignum(void *data, Bignum bn); |
| 167 | Bignum biggcd(Bignum a, Bignum b); |
| 168 | unsigned short bignum_mod_short(Bignum number, unsigned short modulus); |
| 169 | Bignum bignum_add_long(Bignum number, unsigned long addend); |
| 170 | Bignum bigmul(Bignum a, Bignum b); |
| 171 | Bignum modinv(Bignum number, Bignum modulus); |
| 172 | Bignum bignum_rshift(Bignum number, int shift); |
| 173 | int bignum_cmp(Bignum a, Bignum b); |
| 174 | char *bignum_decimal(Bignum x); |
| 175 | |
| 176 | Bignum dh_create_e(void); |
| 177 | Bignum dh_find_K(Bignum f); |
| 178 | |
| 179 | int loadrsakey(char *filename, struct RSAKey *key, struct RSAAux *aux, |
| 180 | char *passphrase); |
| 181 | int rsakey_encrypted(char *filename, char **comment); |
| 182 | |
| 183 | int saversakey(char *filename, struct RSAKey *key, struct RSAAux *aux, |
| 184 | char *passphrase); |
| 185 | |
| 186 | void des3_decrypt_pubkey(unsigned char *key, |
| 187 | unsigned char *blk, int len); |
| 188 | void des3_encrypt_pubkey(unsigned char *key, |
| 189 | unsigned char *blk, int len); |
| 190 | |
| 191 | /* |
| 192 | * For progress updates in the key generation utility. |
| 193 | */ |
| 194 | typedef void (*progfn_t)(void *param, int phase, int progress); |
| 195 | |
| 196 | int rsa_generate(struct RSAKey *key, struct RSAAux *aux, int bits, |
| 197 | progfn_t pfn, void *pfnparam); |
| 198 | Bignum primegen(int bits, int modulus, int residue, |
| 199 | int phase, progfn_t pfn, void *pfnparam); |