[ActiveLock-Development] CVS: alcrypto ALCrypto.def,NONE,1.1 ALCrypto.dsp,NONE,1.1 ALCrypto.dsw,NONE
Brought to you by:
ialkan
From: Thanh H. T. <th...@us...> - 2003-07-28 01:01:06
|
Update of /cvsroot/activelock/alcrypto In directory sc8-pr-cvs1:/tmp/cvs-serv28000 Added Files: ALCrypto.def ALCrypto.dsp ALCrypto.dsw BIGNUM.C CRYPT.H INT64.H MD5.C MISC.C MISC.H NOISE.C PRIME.C RAND.C RSA.C RSA.h RSAG.C SHA.C SSH.C StdAfx.cpp StdAfx.h Log Message: ActiveLock Cryptographic Library --- NEW FILE: ALCrypto.def --- ; Sample Export definition file to create Microsoft Visual C++ (Win32) ; compatible LIB file for ALCRYPTO.DLL. ; LIBRARY "ALCrypto" DESCRIPTION "ActiveLock Cryptographic Library" EXPORTS fnRSA @1 rsa_generate @2 rsa_decrypt @3 rsa_encrypt @4 rsa_public_key_blob @5 rsa_private_key_blob @6 rsa_createkey @7 rsa_sign @8 rsa_verifysig @9 rsa_freekey @10 --- NEW FILE: ALCrypto.dsp --- # Microsoft Developer Studio Project File - Name="ALCrypto" - Package Owner=<4> # Microsoft Developer Studio Generated Build File, Format Version 6.00 # ** DO NOT EDIT ** # TARGTYPE "Win32 (x86) Dynamic-Link Library" 0x0102 CFG=ALCrypto - Win32 Debug !MESSAGE This is not a valid makefile. To build this project using NMAKE, !MESSAGE use the Export Makefile command and run !MESSAGE !MESSAGE NMAKE /f "ALCrypto.mak". !MESSAGE !MESSAGE You can specify a configuration when running NMAKE !MESSAGE by defining the macro CFG on the command line. For example: !MESSAGE !MESSAGE NMAKE /f "ALCrypto.mak" CFG="ALCrypto - Win32 Debug" !MESSAGE !MESSAGE Possible choices for configuration are: !MESSAGE !MESSAGE "ALCrypto - Win32 Release" (based on "Win32 (x86) Dynamic-Link Library") !MESSAGE "ALCrypto - Win32 Debug" (based on "Win32 (x86) Dynamic-Link Library") !MESSAGE # Begin Project # PROP AllowPerConfigDependencies 0 # PROP Scc_ProjName "" # PROP Scc_LocalPath "" CPP=cl.exe MTL=midl.exe RSC=rc.exe !IF "$(CFG)" == "ALCrypto - Win32 Release" # PROP BASE Use_MFC 0 # PROP BASE Use_Debug_Libraries 0 # PROP BASE Output_Dir "Release" # PROP BASE Intermediate_Dir "Release" # PROP BASE Target_Dir "" # PROP Use_MFC 0 # PROP Use_Debug_Libraries 0 # PROP Output_Dir "Release" # PROP Intermediate_Dir "Release" # PROP Ignore_Export_Lib 0 # PROP Target_Dir "" # ADD BASE CPP /nologo /MT /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "ALCRYPTO_EXPORTS" /YX /FD /c # ADD CPP /nologo /MT /W3 /GX /O2 /D "WIN32" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "ALCRYPTO_EXPORTS" /FR /YX /FD /c # ADD BASE MTL /nologo /D "NDEBUG" /mktyplib203 /win32 # ADD MTL /nologo /D "NDEBUG" /mktyplib203 /win32 # ADD BASE RSC /l 0x1009 /d "NDEBUG" # ADD RSC /l 0x1009 /d "NDEBUG" BSC32=bscmake.exe # ADD BASE BSC32 /nologo # ADD BSC32 /nologo LINK32=link.exe # ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /machine:I386 # ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /machine:I386 # Begin Special Build Tool SOURCE="$(InputPath)" PostBuild_Cmds=copy release\alcrypto.dll test\ # End Special Build Tool !ELSEIF "$(CFG)" == "ALCrypto - Win32 Debug" # PROP BASE Use_MFC 0 # PROP BASE Use_Debug_Libraries 1 # PROP BASE Output_Dir "ALCrypto___Win32_Debug" # PROP BASE Intermediate_Dir "ALCrypto___Win32_Debug" # PROP BASE Target_Dir "" # PROP Use_MFC 0 # PROP Use_Debug_Libraries 1 # PROP Output_Dir "Debug" # PROP Intermediate_Dir "Debug" # PROP Ignore_Export_Lib 0 # PROP Target_Dir "" # ADD BASE CPP /nologo /MTd /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "ALCRYPTO_EXPORTS" /YX /FD /GZ /c # ADD CPP /nologo /MTd /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "ALCRYPTO_EXPORTS" /YX /FD /GZ /c # ADD BASE MTL /nologo /D "_DEBUG" /mktyplib203 /win32 # ADD MTL /nologo /D "_DEBUG" /mktyplib203 /win32 # ADD BASE RSC /l 0x1009 /d "_DEBUG" # ADD RSC /l 0x1009 /d "_DEBUG" BSC32=bscmake.exe # ADD BASE BSC32 /nologo # ADD BSC32 /nologo LINK32=link.exe # ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /debug /machine:I386 /pdbtype:sept # ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /debug /machine:I386 /pdbtype:sept # Begin Special Build Tool SOURCE="$(InputPath)" PostBuild_Cmds=copy debug\alcrypto.dll test\ # End Special Build Tool !ENDIF # Begin Target # Name "ALCrypto - Win32 Release" # Name "ALCrypto - Win32 Debug" # Begin Group "Source Files" # PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat" # Begin Source File SOURCE=.\ALCRYPTO.DEF # End Source File # Begin Source File SOURCE=.\BIGNUM.C # End Source File # Begin Source File SOURCE=.\MD5.C # End Source File # Begin Source File SOURCE=.\MISC.C # End Source File # Begin Source File SOURCE=.\NOISE.C # End Source File # Begin Source File SOURCE=.\PRIME.C # End Source File # Begin Source File SOURCE=.\RAND.C # End Source File # Begin Source File SOURCE=.\RSA.C # End Source File # Begin Source File SOURCE=.\RSAG.C # End Source File # Begin Source File SOURCE=.\SHA.C # End Source File # Begin Source File SOURCE=.\StdAfx.cpp # ADD CPP /Yc"stdafx.h" # End Source File # End Group # Begin Group "Header Files" # PROP Default_Filter "h;hpp;hxx;hm;inl" # Begin Source File SOURCE=.\CRYPT.H # End Source File # Begin Source File SOURCE=.\INT64.H # End Source File # Begin Source File SOURCE=.\MISC.H # End Source File # Begin Source File SOURCE=.\RSA.h # End Source File # Begin Source File SOURCE=.\StdAfx.h # End Source File # End Group # Begin Group "Resource Files" # PROP Default_Filter "ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe" # End Group # Begin Source File SOURCE=.\ReadMe.txt # End Source File # End Target # End Project --- NEW FILE: ALCrypto.dsw --- Microsoft Developer Studio Workspace File, Format Version 6.00 # WARNING: DO NOT EDIT OR DELETE THIS WORKSPACE FILE! ############################################################################### Project: "ALCrypto"=.\ALCrypto.dsp - Package Owner=<4> Package=<5> {{{ }}} Package=<4> {{{ }}} ############################################################################### Global: Package=<5> {{{ }}} Package=<3> {{{ }}} ############################################################################### --- NEW FILE: BIGNUM.C --- /** * ActiveLock Cryptographic Library * Copyright 2003 The ActiveLock Software Group (ASG) * Portions Copyright by Simon Tatham and the PuTTY project. * * All material is the property of the contributing authors. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: * * [o] Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * [o] Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the following * disclaimer in the documentation and/or other materials provided * with the distribution. * [...1048 lines suppressed...] workspace[i] = (BignumInt) (carry / 10); if (workspace[i]) iszero = 0; carry %= 10; } ret[--ndigit] = (char) (carry + '0'); } while (!iszero); /* * There's a chance we've fallen short of the start of the * string. Correct if so. */ if (ndigit > 0) memmove(ret, ret + ndigit, ndigits - ndigit); /* * Done. */ return ret; } --- NEW FILE: CRYPT.H --- /** * ActiveLock Cryptographic Library * Copyright 2003 The ActiveLock Software Group (ASG) * Portions Copyright by Simon Tatham and the PuTTY project. * * All material is the property of the contributing authors. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: * * [o] Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * [o] Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the following * disclaimer in the documentation and/or other materials provided * with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * PuTTY License * ============= * * PuTTY is copyright 1997-2001 Simon Tatham. * * Portions copyright Robert de Bath, Joris van Rantwijk, Delian * Delchev, Andreas Schultz, Jeroen Massar, Wez Furlong, Nicolas Barry, * Justin Bradford, and CORE SDI S.A. * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation files * (the "Software"), to deal in the Software without restriction, * including without limitation the rights to use, copy, modify, merge, * publish, distribute, sublicense, and/or sell copies of the Software, * and to permit persons to whom the Software is furnished to do so, * subject to the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * */ #ifndef ALCRYPTO_CRYPT_H #define ALCRYPTO_CRYPT_H #endif #include <string.h> #include <stdio.h> /* for FILENAME_MAX */ #include "stdafx.h" #include "misc.h" #include "int64.h" #include "rsa.h" /* * Useful thing. */ #ifndef lenof #define lenof(x) ( (sizeof((x))) / (sizeof(*(x)))) #endif #define SSH_CIPHER_IDEA 1 #define SSH_CIPHER_DES 2 #define SSH_CIPHER_3DES 3 #define SSH_CIPHER_BLOWFISH 6 #ifdef MSCRYPTOAPI #define APIEXTRA 8 #else #define APIEXTRA 0 #endif struct dss_key { Bignum p, q, g, y, x; }; int makekey(unsigned char *data, struct RSAKey *result, unsigned char **keystr, int order); int makeprivate(unsigned char *data, struct RSAKey *result); void rsasign(unsigned char *data, int length, struct RSAKey *key); void rsasanitise(struct RSAKey *key); int rsastr_len(struct RSAKey *key); void rsastr_fmt(char *str, struct RSAKey *key); void rsa_fingerprint(char *str, int len, struct RSAKey *key); int rsa_verify(struct RSAKey *key); /* externally accessible prototypes */ unsigned char *rsa_public_blob(struct RSAKey *key, int *len); void rsaencrypt(int type, unsigned char *data, int *length, struct RSAKey *key); Bignum rsadecrypt(int type, Bignum input, struct RSAKey *key); int ssh1_read_bignum(const unsigned char *data, Bignum * result); int ssh1_write_bignum(void *data, Bignum bn); Bignum bignum_from_bytes(const unsigned char *data, int nbytes); void bignum_to_bytes(Bignum bn, unsigned char *data); int rsa2_public_blob_len(void *key); void rsa2_public_blob(void *key, unsigned char *blob); int rsa2_private_blob_len(void *key); void rsa2_private_blob(void *key, unsigned char *blob); void rsa2_createkey(unsigned char *pub_blob, int pub_len, unsigned char *priv_blob, int priv_len, struct RSAKey *key); int rsa_public_blob_len(void *data); void freersakey(struct RSAKey *key); typedef unsigned int word32; typedef unsigned int uint32; unsigned long crc32(const void *s, size_t len); unsigned long crc32_update(unsigned long crc_input, const void *s, size_t len); /* SSH CRC compensation attack detector */ int detect_attack(unsigned char *buf, uint32 len, unsigned char *IV); typedef struct { uint32 h[4]; } MD5_Core_State; struct MD5Context { #ifdef MSCRYPTOAPI unsigned long hHash; #else MD5_Core_State core; unsigned char block[64]; int blkused; uint32 lenhi, lenlo; #endif }; void MD5Init(struct MD5Context *context); void MD5Update(struct MD5Context *context, unsigned char const *buf, unsigned len); void MD5Final(unsigned char digest[16], struct MD5Context *context); typedef struct { uint32 h[5]; unsigned char block[64]; int blkused; uint32 lenhi, lenlo; } SHA_State; void SHA_Init(SHA_State * s); void SHA_Bytes(SHA_State * s, void *p, int len); void SHA_Final(SHA_State * s, unsigned char *output); void SHA_Simple(void *p, int len, unsigned char *output); void hmac_sha1_simple(void *key, int keylen, void *data, int datalen, unsigned char *output); typedef struct { uint64 h[8]; unsigned char block[128]; int blkused; uint32 len[4]; } SHA512_State; void SHA512_Init(SHA512_State * s); void SHA512_Bytes(SHA512_State * s, const void *p, int len); void SHA512_Final(SHA512_State * s, unsigned char *output); void SHA512_Simple(const void *p, int len, unsigned char *output); struct ssh_cipher { void (*sesskey) (unsigned char *key); /* for ssh 1 */ void (*encrypt) (unsigned char *blk, int len); void (*decrypt) (unsigned char *blk, int len); int blksize; }; struct ssh2_cipher { void (*setcsiv) (unsigned char *key); /* for ssh 2 */ void (*setcskey) (unsigned char *key); /* for ssh 2 */ void (*setsciv) (unsigned char *key); /* for ssh 2 */ void (*setsckey) (unsigned char *key); /* for ssh 2 */ void (*encrypt) (unsigned char *blk, int len); void (*decrypt) (unsigned char *blk, int len); char *name; int blksize; int keylen; }; struct ssh2_ciphers { int nciphers; const struct ssh2_cipher *const *list; }; struct ssh_mac { void (*setcskey) (unsigned char *key); void (*setsckey) (unsigned char *key); void (*generate) (unsigned char *blk, int len, unsigned long seq); int (*verify) (unsigned char *blk, int len, unsigned long seq); char *name; int len; }; struct ssh_kex { /* * Plugging in another KEX algorithm requires structural chaos, * so it's hard to abstract them into nice little structures * like this. Hence, for the moment, this is just a * placeholder. I claim justification in the fact that OpenSSH * does this too :-) */ char *name; }; struct ssh_signkey { void *(*newkey) (char *data, int len); void (*freekey) (void *key); char *(*fmtkey) (void *key); unsigned char *(*public_blob) (void *key, int *len); unsigned char *(*private_blob) (void *key, int *len); void *(*createkey) (unsigned char *pub_blob, int pub_len, unsigned char *priv_blob, int priv_len); void *(*openssh_createkey) (unsigned char **blob, int *len); int (*openssh_fmtkey) (void *key, unsigned char *blob, int len); char *(*fingerprint) (void *key); int (*verifysig) (void *key, char *sig, int siglen, char *data, int datalen); unsigned char *(*sign) (void *key, char *data, int datalen, int *siglen); char *name; char *keytype; /* for host key cache */ }; struct ssh_compress { char *name; void (*compress_init) (void); int (*compress) (unsigned char *block, int len, unsigned char **outblock, int *outlen); void (*decompress_init) (void); int (*decompress) (unsigned char *block, int len, unsigned char **outblock, int *outlen); int (*disable_compression) (void); }; struct ssh2_userkey { const struct ssh_signkey *alg; /* the key algorithm */ void *data; /* the key data */ char *comment; /* the key comment */ }; extern const struct ssh_cipher ssh_3des; extern const struct ssh_cipher ssh_des; extern const struct ssh_cipher ssh_blowfish_ssh1; extern const struct ssh2_ciphers ssh2_3des; extern const struct ssh2_ciphers ssh2_des; extern const struct ssh2_ciphers ssh2_aes; extern const struct ssh2_ciphers ssh2_blowfish; extern const struct ssh_kex ssh_diffiehellman; extern const struct ssh_kex ssh_diffiehellman_gex; extern const struct ssh_signkey ssh_dss; extern const struct ssh_signkey ssh_rsa; extern const struct ssh_mac ssh_md5; extern const struct ssh_mac ssh_sha1; extern const struct ssh_mac ssh_sha1_buggy; #ifndef MSCRYPTOAPI void SHATransform(word32 * digest, word32 * data); #endif int random_byte(void); void random_add_noise(void *noise, int length); void random_add_heavynoise(void *noise, int length); void logevent(char *); #ifdef _DEBUG void diagbn(char *prefix, Bignum md); #endif Bignum copybn(Bignum b); Bignum bn_power_2(int n); void bn_restore_invariant(Bignum b); Bignum bignum_from_long(unsigned long n); void freebn(Bignum b); Bignum modpow(Bignum base, Bignum exp, Bignum mod); Bignum modmul(Bignum a, Bignum b, Bignum mod); void decbn(Bignum n); extern Bignum Zero, One; int bignum_bitcount(Bignum bn); int ssh1_bignum_length(Bignum bn); int ssh2_bignum_length(Bignum bn); int bignum_byte(Bignum bn, int i); int bignum_bit(Bignum bn, int i); void bignum_set_bit(Bignum bn, int i, int value); Bignum biggcd(Bignum a, Bignum b); unsigned short bignum_mod_short(Bignum number, unsigned short modulus); Bignum bignum_add_long(Bignum number, unsigned long addend); Bignum bigmul(Bignum a, Bignum b); Bignum bigmuladd(Bignum a, Bignum b, Bignum addend); Bignum bigdiv(Bignum a, Bignum b); Bignum bigmod(Bignum a, Bignum b); Bignum modinv(Bignum number, Bignum modulus); Bignum bignum_bitmask(Bignum number); Bignum bignum_rshift(Bignum number, int shift); int bignum_cmp(Bignum a, Bignum b); char *bignum_decimal(Bignum x); void des3_decrypt_pubkey(unsigned char *key, unsigned char *blk, int len); void des3_encrypt_pubkey(unsigned char *key, unsigned char *blk, int len); void des3_decrypt_pubkey_ossh(unsigned char *key, unsigned char *iv, unsigned char *blk, int len); void des3_encrypt_pubkey_ossh(unsigned char *key, unsigned char *iv, unsigned char *blk, int len); void aes256_encrypt_pubkey(unsigned char *key, unsigned char *blk, int len); void aes256_decrypt_pubkey(unsigned char *key, unsigned char *blk, int len); /* * For progress updates in the key generation utility. */ #define PROGFN_INITIALISE 1 #define PROGFN_LIN_PHASE 2 #define PROGFN_EXP_PHASE 3 #define PROGFN_PHASE_EXTENT 4 #define PROGFN_READY 5 #define PROGFN_PROGRESS 6 typedef void (__stdcall *progfn_t) (void *param, int action, int phase, int progress); ALCRYPTO_API LRESULT WINAPI rsa_generate(struct RSAKey *key, int bits, progfn_t pfn, void *pfnparam); int dsa_generate(struct dss_key *key, int bits, progfn_t pfn, void *pfnparam); Bignum primegen(int bits, int modulus, int residue, Bignum factor, int phase, progfn_t pfn, void *pfnparam); /* * zlib compression. */ void zlib_compress_init(void); void zlib_decompress_init(void); int zlib_compress_block(unsigned char *block, int len, unsigned char **outblock, int *outlen); int zlib_decompress_block(unsigned char *block, int len, unsigned char **outblock, int *outlen); /* * Need this to warn about support for the original SSH2 keyfile * format. */ void old_keyfile_warning(void); #define REG_HOME "Software\\ActiveLock Software Group\\ActiveLock" #define GLOBAL extern /* Three attribute types: * The ATTRs (normal attributes) are stored with the characters in the main * display arrays * * The TATTRs (temporary attributes) are generated on the fly, they can overlap * with characters but not with normal attributes. * * The LATTRs (line attributes) conflict with no others and only have one * value per line. But on area clears the LATTR cells are set to the erase_char * (or DEFAULT_ATTR + 'E') * * ATTR_INVALID is an illegal colour combination. */ #define TATTR_ACTCURS 0x4UL /* active cursor (block) */ #define TATTR_PASCURS 0x2UL /* passive cursor (box) */ #define TATTR_RIGHTCURS 0x1UL /* cursor-on-RHS */ #define LATTR_NORM 0x00000000UL #define LATTR_WIDE 0x01000000UL #define LATTR_TOP 0x02000000UL #define LATTR_BOT 0x03000000UL #define LATTR_MODE 0x03000000UL #define LATTR_WRAPPED 0x10000000UL #define ATTR_INVALID 0x00FF0000UL /* Like Linux use the F000 page for direct to font. */ #define ATTR_OEMCP 0x0000F000UL /* OEM Codepage DTF */ #define ATTR_ACP 0x0000F100UL /* Ansi Codepage DTF */ /* These are internal use overlapping with the UTF-16 surrogates */ #define ATTR_ASCII 0x0000D800UL /* normal ASCII charset ESC ( B */ #define ATTR_LINEDRW 0x0000D900UL /* line drawing charset ESC ( 0 */ #define ATTR_SCOACS 0x0000DA00UL /* SCO Alternate charset */ #define ATTR_GBCHR 0x0000DB00UL /* UK variant charset ESC ( A */ #define CSET_MASK 0x0000FF00UL /* Character set mask; MUST be 0xFF00 */ #define DIRECT_CHAR(c) ((c&0xFC00)==0xD800) #define DIRECT_FONT(c) ((c&0xFE00)==0xF000) #define UCSERR (ATTR_LINEDRW|'a') /* UCS Format error character. */ #define UCSWIDE 0x303F #define ATTR_NARROW 0x20000000UL #define ATTR_WIDE 0x10000000UL #define ATTR_BOLD 0x01000000UL #define ATTR_UNDER 0x02000000UL #define ATTR_REVERSE 0x04000000UL #define ATTR_BLINK 0x08000000UL #define ATTR_FGMASK 0x000F0000UL #define ATTR_BGMASK 0x00F00000UL #define ATTR_COLOURS 0x00FF0000UL #define ATTR_FGSHIFT 16 #define ATTR_BGSHIFT 20 #define ATTR_DEFAULT 0x00980000UL #define ATTR_DEFFG 0x00080000UL #define ATTR_DEFBG 0x00900000UL #define ERASE_CHAR (ATTR_DEFAULT | ATTR_ASCII | ' ') #define ATTR_MASK 0xFFFFFF00UL #define CHAR_MASK 0x000000FFUL #define ATTR_CUR_AND (~(ATTR_BOLD|ATTR_REVERSE|ATTR_BLINK|ATTR_COLOURS)) #define ATTR_CUR_XOR 0x00BA0000UL typedef HDC Context; #define SEL_NL { 13, 10 } #define in_utf (utf || line_codepage==CP_UTF8) #define LGXF_OVR 1 /* existing logfile overwrite */ #define LGXF_APN 0 /* existing logfile append */ #define LGXF_ASK -1 /* existing logfile ask */ #define LGTYP_NONE 0 /* logmode: no logging */ #define LGTYP_ASCII 1 /* logmode: pure ascii */ #define LGTYP_DEBUG 2 /* logmode: all chars of traffic */ #define LGTYP_PACKETS 3 /* logmode: SSH data packets */ /* * I've just looked in the windows standard headr files for WM_USER, there * are hundreds of flags defined using the form WM_USER+123 so I've * renumbered this NETEVENT value and the two in window.c */ #define WM_XUSER (WM_USER + 0x2000) #define WM_NETEVENT (WM_XUSER + 5) typedef enum { TS_AYT, TS_BRK, TS_SYNCH, TS_EC, TS_EL, TS_GA, TS_NOP, TS_ABORT, TS_AO, TS_IP, TS_SUSP, TS_EOR, TS_EOF, TS_LECHO, TS_RECHO, TS_PING, TS_EOL } Telnet_Special; typedef enum { MBT_NOTHING, MBT_LEFT, MBT_MIDDLE, MBT_RIGHT, /* `raw' button designations */ MBT_SELECT, MBT_EXTEND, MBT_PASTE, /* `cooked' button designations */ MBT_WHEEL_UP, MBT_WHEEL_DOWN /* mouse wheel */ } Mouse_Button; typedef enum { MA_NOTHING, MA_CLICK, MA_2CLK, MA_3CLK, MA_DRAG, MA_RELEASE } Mouse_Action; typedef enum { VT_XWINDOWS, VT_OEMANSI, VT_OEMONLY, VT_POORMAN, VT_UNICODE } VT_Mode; enum { /* * SSH ciphers (both SSH1 and SSH2) */ CIPHER_WARN, /* pseudo 'cipher' */ CIPHER_3DES, CIPHER_BLOWFISH, CIPHER_AES, /* (SSH 2 only) */ CIPHER_DES, CIPHER_MAX /* no. ciphers (inc warn) */ }; enum { /* * Line discipline option states: off, on, up to the backend. */ LD_YES, LD_NO, LD_BACKEND }; enum { /* * Line discipline options which the backend might try to control. */ LD_EDIT, /* local line editing */ LD_ECHO /* local echo */ }; enum { /* * Close On Exit behaviours. (cfg.close_on_exit) */ COE_NEVER, /* Never close the window */ COE_NORMAL, /* Close window on "normal" (non-error) exits only */ COE_ALWAYS /* Always close the window */ }; /* * You can compile with -DSSH_DEFAULT to have ssh by default. */ #ifndef SSH_DEFAULT #define DEFAULT_PROTOCOL PROT_TELNET #define DEFAULT_PORT 23 #else #define DEFAULT_PROTOCOL PROT_SSH #define DEFAULT_PORT 22 #endif /* * Some global flags denoting the type of application. * * FLAG_VERBOSE is set when the user requests verbose details. * * FLAG_STDERR is set in command-line applications (which have a * functioning stderr that it makes sense to write to) and not in * GUI applications (which don't). * * FLAG_INTERACTIVE is set when a full interactive shell session is * being run, _either_ because no remote command has been provided * _or_ because the application is GUI and can't run non- * interactively. */ #define FLAG_VERBOSE 0x0001 #define FLAG_STDERR 0x0002 #define FLAG_INTERACTIVE 0x0004 GLOBAL int flags; struct RSAKey; /* be a little careful of scope */ void cleanup_exit(int); /* * Exports from noise.c. */ void noise_get_heavy(void (*func) (void *, int)); void noise_get_light(void (*func) (void *, int)); void noise_regular(void); void noise_ultralight(DWORD data); void random_save_seed(void); void random_destroy_seed(void); /* * Exports from rand.c. */ void random_add_noise(void *noise, int length); void random_init(void); int random_byte(void); void random_get_savedata(void **data, int *len); extern int random_active; /* * Exports from misc.c. */ #include "misc.h" /* * Exports from sizetip.c. */ void UpdateSizeTip(HWND src, int cx, int cy); void EnableSizeTip(int bEnable); /* * Exports from mscrypto.c */ #ifdef MSCRYPTOAPI int crypto_startup(); void crypto_wrapup(); #endif --- NEW FILE: INT64.H --- /** * ActiveLock Cryptographic Library * Copyright 2003 The ActiveLock Software Group (ASG) * Portions Copyright by Simon Tatham and the PuTTY project. * * All material is the property of the contributing authors. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: * * [o] Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * [o] Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the following * disclaimer in the documentation and/or other materials provided * with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * PuTTY License * ============= * * PuTTY is copyright 1997-2001 Simon Tatham. * * Portions copyright Robert de Bath, Joris van Rantwijk, Delian * Delchev, Andreas Schultz, Jeroen Massar, Wez Furlong, Nicolas Barry, * Justin Bradford, and CORE SDI S.A. * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation files * (the "Software"), to deal in the Software without restriction, * including without limitation the rights to use, copy, modify, merge, * publish, distribute, sublicense, and/or sell copies of the Software, * and to permit persons to whom the Software is furnished to do so, * subject to the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * */ /* * Header for int64.c. */ #ifndef PUTTY_INT64_H #define PUTTY_INT64_H typedef struct { unsigned long hi, lo; } uint64, int64; uint64 uint64_div10(uint64 x, int *remainder); void uint64_decimal(uint64 x, char *buffer); uint64 uint64_make(unsigned long hi, unsigned long lo); uint64 uint64_add(uint64 x, uint64 y); uint64 uint64_add32(uint64 x, unsigned long y); int uint64_compare(uint64 x, uint64 y); #endif --- NEW FILE: MD5.C --- /** * ActiveLock Cryptographic Library * Copyright 2003 The ActiveLock Software Group (ASG) * Portions Copyright by Simon Tatham and the PuTTY project. * * All material is the property of the contributing authors. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: * * [o] Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * [o] Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the following * disclaimer in the documentation and/or other materials provided * with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * PuTTY License * ============= * * PuTTY is copyright 1997-2001 Simon Tatham. * * Portions copyright Robert de Bath, Joris van Rantwijk, Delian * Delchev, Andreas Schultz, Jeroen Massar, Wez Furlong, Nicolas Barry, * Justin Bradford, and CORE SDI S.A. * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation files * (the "Software"), to deal in the Software without restriction, * including without limitation the rights to use, copy, modify, merge, * publish, distribute, sublicense, and/or sell copies of the Software, * and to permit persons to whom the Software is furnished to do so, * subject to the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * */ #include "crypt.h" /* * MD5 implementation for PuTTY. Written directly from the spec by * Simon Tatham. */ /* ---------------------------------------------------------------------- * Core MD5 algorithm: processes 16-word blocks into a message digest. */ /********************************************************************************************** * Change Log * ========== * * Date (MM/DD/YY) Author Description * --------------- ----------- -------------------------------------------------------------- * 07/27/03 th2tran adapted from PuTTY project for used by ActiveLock project. * ***********************************************************************************************/ #define F(x,y,z) ( ((x) & (y)) | ((~(x)) & (z)) ) #define G(x,y,z) ( ((x) & (z)) | ((~(z)) & (y)) ) #define H(x,y,z) ( (x) ^ (y) ^ (z) ) #define I(x,y,z) ( (y) ^ ( (x) | ~(z) ) ) #define rol(x,y) ( ((x) << (y)) | (((uint32)x) >> (32-y)) ) #define subround(f,w,x,y,z,k,s,ti) \ w = x + rol(w + f(x,y,z) + block[k] + ti, s) void MD5_Core_Init(MD5_Core_State * s) { s->h[0] = 0x67452301; s->h[1] = 0xefcdab89; s->h[2] = 0x98badcfe; s->h[3] = 0x10325476; } void MD5_Block(MD5_Core_State * s, uint32 * block) { uint32 a, b, c, d; a = s->h[0]; b = s->h[1]; c = s->h[2]; d = s->h[3]; subround(F, a, b, c, d, 0, 7, 0xd76aa478); subround(F, d, a, b, c, 1, 12, 0xe8c7b756); subround(F, c, d, a, b, 2, 17, 0x242070db); subround(F, b, c, d, a, 3, 22, 0xc1bdceee); subround(F, a, b, c, d, 4, 7, 0xf57c0faf); subround(F, d, a, b, c, 5, 12, 0x4787c62a); subround(F, c, d, a, b, 6, 17, 0xa8304613); subround(F, b, c, d, a, 7, 22, 0xfd469501); subround(F, a, b, c, d, 8, 7, 0x698098d8); subround(F, d, a, b, c, 9, 12, 0x8b44f7af); subround(F, c, d, a, b, 10, 17, 0xffff5bb1); subround(F, b, c, d, a, 11, 22, 0x895cd7be); subround(F, a, b, c, d, 12, 7, 0x6b901122); subround(F, d, a, b, c, 13, 12, 0xfd987193); subround(F, c, d, a, b, 14, 17, 0xa679438e); subround(F, b, c, d, a, 15, 22, 0x49b40821); subround(G, a, b, c, d, 1, 5, 0xf61e2562); subround(G, d, a, b, c, 6, 9, 0xc040b340); subround(G, c, d, a, b, 11, 14, 0x265e5a51); subround(G, b, c, d, a, 0, 20, 0xe9b6c7aa); subround(G, a, b, c, d, 5, 5, 0xd62f105d); subround(G, d, a, b, c, 10, 9, 0x02441453); subround(G, c, d, a, b, 15, 14, 0xd8a1e681); subround(G, b, c, d, a, 4, 20, 0xe7d3fbc8); subround(G, a, b, c, d, 9, 5, 0x21e1cde6); subround(G, d, a, b, c, 14, 9, 0xc33707d6); subround(G, c, d, a, b, 3, 14, 0xf4d50d87); subround(G, b, c, d, a, 8, 20, 0x455a14ed); subround(G, a, b, c, d, 13, 5, 0xa9e3e905); subround(G, d, a, b, c, 2, 9, 0xfcefa3f8); subround(G, c, d, a, b, 7, 14, 0x676f02d9); subround(G, b, c, d, a, 12, 20, 0x8d2a4c8a); subround(H, a, b, c, d, 5, 4, 0xfffa3942); subround(H, d, a, b, c, 8, 11, 0x8771f681); subround(H, c, d, a, b, 11, 16, 0x6d9d6122); subround(H, b, c, d, a, 14, 23, 0xfde5380c); subround(H, a, b, c, d, 1, 4, 0xa4beea44); subround(H, d, a, b, c, 4, 11, 0x4bdecfa9); subround(H, c, d, a, b, 7, 16, 0xf6bb4b60); subround(H, b, c, d, a, 10, 23, 0xbebfbc70); subround(H, a, b, c, d, 13, 4, 0x289b7ec6); subround(H, d, a, b, c, 0, 11, 0xeaa127fa); subround(H, c, d, a, b, 3, 16, 0xd4ef3085); subround(H, b, c, d, a, 6, 23, 0x04881d05); subround(H, a, b, c, d, 9, 4, 0xd9d4d039); subround(H, d, a, b, c, 12, 11, 0xe6db99e5); subround(H, c, d, a, b, 15, 16, 0x1fa27cf8); subround(H, b, c, d, a, 2, 23, 0xc4ac5665); subround(I, a, b, c, d, 0, 6, 0xf4292244); subround(I, d, a, b, c, 7, 10, 0x432aff97); subround(I, c, d, a, b, 14, 15, 0xab9423a7); subround(I, b, c, d, a, 5, 21, 0xfc93a039); subround(I, a, b, c, d, 12, 6, 0x655b59c3); subround(I, d, a, b, c, 3, 10, 0x8f0ccc92); subround(I, c, d, a, b, 10, 15, 0xffeff47d); subround(I, b, c, d, a, 1, 21, 0x85845dd1); subround(I, a, b, c, d, 8, 6, 0x6fa87e4f); subround(I, d, a, b, c, 15, 10, 0xfe2ce6e0); subround(I, c, d, a, b, 6, 15, 0xa3014314); subround(I, b, c, d, a, 13, 21, 0x4e0811a1); subround(I, a, b, c, d, 4, 6, 0xf7537e82); subround(I, d, a, b, c, 11, 10, 0xbd3af235); subround(I, c, d, a, b, 2, 15, 0x2ad7d2bb); subround(I, b, c, d, a, 9, 21, 0xeb86d391); s->h[0] += a; s->h[1] += b; s->h[2] += c; s->h[3] += d; } /* ---------------------------------------------------------------------- * Outer MD5 algorithm: take an arbitrary length byte string, * convert it into 16-word blocks with the prescribed padding at * the end, and pass those blocks to the core MD5 algorithm. */ #define BLKSIZE 64 void MD5Init(struct MD5Context *s) { MD5_Core_Init(&s->core); s->blkused = 0; s->lenhi = s->lenlo = 0; } void MD5Update(struct MD5Context *s, unsigned char const *p, unsigned len) { unsigned char *q = (unsigned char *) p; uint32 wordblock[16]; uint32 lenw = len; int i; /* * Update the length field. */ s->lenlo += lenw; s->lenhi += (s->lenlo < lenw); if (s->blkused + len < BLKSIZE) { /* * Trivial case: just add to the block. */ memcpy(s->block + s->blkused, q, len); s->blkused += len; } else { /* * We must complete and process at least one block. */ while (s->blkused + len >= BLKSIZE) { memcpy(s->block + s->blkused, q, BLKSIZE - s->blkused); q += BLKSIZE - s->blkused; len -= BLKSIZE - s->blkused; /* Now process the block. Gather bytes little-endian into words */ for (i = 0; i < 16; i++) { wordblock[i] = (((uint32) s->block[i * 4 + 3]) << 24) | (((uint32) s->block[i * 4 + 2]) << 16) | (((uint32) s->block[i * 4 + 1]) << 8) | (((uint32) s->block[i * 4 + 0]) << 0); } MD5_Block(&s->core, wordblock); s->blkused = 0; } memcpy(s->block, q, len); s->blkused = len; } } void MD5Final(unsigned char output[16], struct MD5Context *s) { int i; unsigned pad; unsigned char c[64]; uint32 lenhi, lenlo; if (s->blkused >= 56) pad = 56 + 64 - s->blkused; else pad = 56 - s->blkused; lenhi = (s->lenhi << 3) | (s->lenlo >> (32 - 3)); lenlo = (s->lenlo << 3); memset(c, 0, pad); c[0] = 0x80; MD5Update(s, c, pad); c[7] = (lenhi >> 24) & 0xFF; c[6] = (lenhi >> 16) & 0xFF; c[5] = (lenhi >> 8) & 0xFF; c[4] = (lenhi >> 0) & 0xFF; c[3] = (lenlo >> 24) & 0xFF; c[2] = (lenlo >> 16) & 0xFF; c[1] = (lenlo >> 8) & 0xFF; c[0] = (lenlo >> 0) & 0xFF; MD5Update(s, c, 8); for (i = 0; i < 4; i++) { output[4 * i + 3] = (s->core.h[i] >> 24) & 0xFF; output[4 * i + 2] = (s->core.h[i] >> 16) & 0xFF; output[4 * i + 1] = (s->core.h[i] >> 8) & 0xFF; output[4 * i + 0] = (s->core.h[i] >> 0) & 0xFF; } } /* ---------------------------------------------------------------------- * The above is the MD5 algorithm itself. Now we implement the * HMAC wrapper on it. */ static struct MD5Context md5_cs_mac_s1, md5_cs_mac_s2; static struct MD5Context md5_sc_mac_s1, md5_sc_mac_s2; static void md5_key(struct MD5Context *s1, struct MD5Context *s2, unsigned char *key, int len) { unsigned char foo[64]; int i; memset(foo, 0x36, 64); for (i = 0; i < len && i < 64; i++) foo[i] ^= key[i]; MD5Init(s1); MD5Update(s1, foo, 64); memset(foo, 0x5C, 64); for (i = 0; i < len && i < 64; i++) foo[i] ^= key[i]; MD5Init(s2); MD5Update(s2, foo, 64); memset(foo, 0, 64); /* burn the evidence */ } static void md5_cskey(unsigned char *key) { md5_key(&md5_cs_mac_s1, &md5_cs_mac_s2, key, 16); } static void md5_sckey(unsigned char *key) { md5_key(&md5_sc_mac_s1, &md5_sc_mac_s2, key, 16); } static void md5_do_hmac(struct MD5Context *s1, struct MD5Context *s2, unsigned char *blk, int len, unsigned long seq, unsigned char *hmac) { struct MD5Context s; unsigned char intermediate[16]; intermediate[0] = (unsigned char) ((seq >> 24) & 0xFF); intermediate[1] = (unsigned char) ((seq >> 16) & 0xFF); intermediate[2] = (unsigned char) ((seq >> 8) & 0xFF); intermediate[3] = (unsigned char) ((seq) & 0xFF); s = *s1; /* structure copy */ MD5Update(&s, intermediate, 4); MD5Update(&s, blk, len); MD5Final(intermediate, &s); s = *s2; /* structure copy */ MD5Update(&s, intermediate, 16); MD5Final(hmac, &s); } static void md5_generate(unsigned char *blk, int len, unsigned long seq) { md5_do_hmac(&md5_cs_mac_s1, &md5_cs_mac_s2, blk, len, seq, blk + len); } static int md5_verify(unsigned char *blk, int len, unsigned long seq) { unsigned char correct[16]; md5_do_hmac(&md5_sc_mac_s1, &md5_sc_mac_s2, blk, len, seq, correct); return !memcmp(correct, blk + len, 16); } const struct ssh_mac ssh_md5 = { md5_cskey, md5_sckey, md5_generate, md5_verify, "hmac-md5", 16 }; --- NEW FILE: MISC.C --- /** * ActiveLock Cryptographic Library * Copyright 2003 The ActiveLock Software Group (ASG) * Portions Copyright by Simon Tatham and the PuTTY project. * * All material is the property of the contributing authors. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: * * [o] Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * [o] Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the following * disclaimer in the documentation and/or other materials provided * with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * PuTTY License * ============= * * PuTTY is copyright 1997-2001 Simon Tatham. * * Portions copyright Robert de Bath, Joris van Rantwijk, Delian * Delchev, Andreas Schultz, Jeroen Massar, Wez Furlong, Nicolas Barry, * Justin Bradford, and CORE SDI S.A. * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation files * (the "Software"), to deal in the Software without restriction, * including without limitation the rights to use, copy, modify, merge, * publish, distribute, sublicense, and/or sell copies of the Software, * and to permit persons to whom the Software is furnished to do so, * subject to the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * */ /** * Miscellaneous utility functions. */ /********************************************************************************************** * Change Log * ========== * * Date (MM/DD/YY) Author Description * --------------- ----------- -------------------------------------------------------------- * 07/27/03 th2tran adapted from PuTTY project for used by ActiveLock project. * ***********************************************************************************************/ #include <windows.h> #include <stdio.h> #include <stdlib.h> #include <assert.h> #include "crypt.h" static char seedpath[2 * MAX_PATH + 10] = "\0"; static char hex[16] = "0123456789ABCDEF"; void cleanup_exit(int code) { exit(code); } /* ---------------------------------------------------------------------- * String handling routines. */ char *dupstr(char *s) { int len = strlen(s); char *p = smalloc(len + 1); strcpy(p, s); return p; } /* Allocate the concatenation of N strings. Terminate arg list with NULL. */ char *dupcat(char *s1, ...) { int len; char *p, *q, *sn; va_list ap; len = strlen(s1); va_start(ap, s1); while (1) { sn = va_arg(ap, char *); if (!sn) break; len += strlen(sn); } va_end(ap); p = smalloc(len + 1); strcpy(p, s1); q = p + strlen(p); va_start(ap, s1); while (1) { sn = va_arg(ap, char *); if (!sn) break; strcpy(q, sn); q += strlen(q); } va_end(ap); return p; } /* ---------------------------------------------------------------------- * Base64 encoding routine. This is required in public-key writing * but also in HTTP proxy handling, so it's centralised here. */ void base64_encode_atom(unsigned char *data, int n, char *out) { static const char base64_chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; unsigned word; word = data[0] << 16; if (n > 1) word |= data[1] << 8; if (n > 2) word |= data[2]; out[0] = base64_chars[(word >> 18) & 0x3F]; out[1] = base64_chars[(word >> 12) & 0x3F]; if (n > 1) out[2] = base64_chars[(word >> 6) & 0x3F]; else out[2] = '='; if (n > 2) out[3] = base64_chars[word & 0x3F]; else out[3] = '='; } int base64_decode_atom(char *atom, unsigned char *out) { int vals[4]; int i, v, len; unsigned word; char c; for (i = 0; i < 4; i++) { c = atom[i]; if (c >= 'A' && c <= 'Z') v = c - 'A'; else if (c >= 'a' && c <= 'z') v = c - 'a' + 26; else if (c >= '0' && c <= '9') v = c - '0' + 52; else if (c == '+') v = 62; else if (c == '/') v = 63; else if (c == '=') v = -1; else return 0; /* invalid atom */ vals[i] = v; } if (vals[0] == -1 || vals[1] == -1) return 0; if (vals[2] == -1 && vals[3] != -1) return 0; if (vals[3] != -1) len = 3; else if (vals[2] != -1) len = 2; else len = 1; word = ((vals[0] << 18) | (vals[1] << 12) | ((vals[2] & 0x3F) << 6) | (vals[3] & 0x3F)); out[0] = (word >> 16) & 0xFF; if (len > 1) out[1] = (word >> 8) & 0xFF; if (len > 2) out[2] = word & 0xFF; return len; } /* ---------------------------------------------------------------------- * Generic routines to deal with send buffers: a linked list of * smallish blocks, with the operations * * - add an arbitrary amount of data to the end of the list * - remove the first N bytes from the list * - return a (pointer,length) pair giving some initial data in * the list, suitable for passing to a send or write system * call * - retrieve a larger amount of initial data from the list * - return the current size of the buffer chain in bytes */ #define BUFFER_GRANULE 512 struct bufchain_granule { struct bufchain_granule *next; int buflen, bufpos; char buf[BUFFER_GRANULE]; }; void bufchain_init(bufchain *ch) { ch->head = ch->tail = NULL; ch->buffersize = 0; } void bufchain_clear(bufchain *ch) { struct bufchain_granule *b; while (ch->head) { b = ch->head; ch->head = ch->head->next; sfree(b); } ch->tail = NULL; ch->buffersize = 0; } int bufchain_size(bufchain *ch) { return ch->buffersize; } void bufchain_add(bufchain *ch, void *data, int len) { char *buf = (char *)data; ch->buffersize += len; if (ch->tail && ch->tail->buflen < BUFFER_GRANULE) { int copylen = min(len, BUFFER_GRANULE - ch->tail->buflen); memcpy(ch->tail->buf + ch->tail->buflen, buf, copylen); buf += copylen; len -= copylen; ch->tail->buflen += copylen; } while (len > 0) { int grainlen = min(len, BUFFER_GRANULE); struct bufchain_granule *newbuf; newbuf = smalloc(sizeof(struct bufchain_granule)); newbuf->bufpos = 0; newbuf->buflen = grainlen; memcpy(newbuf->buf, buf, grainlen); buf += grainlen; len -= grainlen; if (ch->tail) ch->tail->next = newbuf; else ch->head = ch->tail = newbuf; newbuf->next = NULL; ch->tail = newbuf; } } void bufchain_consume(bufchain *ch, int len) { struct bufchain_granule *tmp; assert(ch->buffersize >= len); while (len > 0) { int remlen = len; assert(ch->head != NULL); if (remlen >= ch->head->buflen - ch->head->bufpos) { remlen = ch->head->buflen - ch->head->bufpos; tmp = ch->head; ch->head = tmp->next; sfree(tmp); if (!ch->head) ch->tail = NULL; } else ch->head->bufpos += remlen; ch->buffersize -= remlen; len -= remlen; } } void bufchain_prefix(bufchain *ch, void **data, int *len) { *len = ch->head->buflen - ch->head->bufpos; *data = ch->head->buf + ch->head->bufpos; } void bufchain_fetch(bufchain *ch, void *data, int len) { struct bufchain_granule *tmp; char *data_c = (char *)data; tmp = ch->head; assert(ch->buffersize >= len); while (len > 0) { int remlen = len; assert(tmp != NULL); if (remlen >= tmp->buflen - tmp->bufpos) remlen = tmp->buflen - tmp->bufpos; memcpy(data_c, tmp->buf + tmp->bufpos, remlen); tmp = tmp->next; len -= remlen; data_c += remlen; } } /* ---------------------------------------------------------------------- * My own versions of malloc, realloc and free. Because I want * malloc and realloc to bomb out and exit the program if they run * out of memory, realloc to reliably call malloc if passed a NULL * pointer, and free to reliably do nothing if passed a NULL * pointer. We can also put trace printouts in, if we need to; and * we can also replace the allocator with an ElectricFence-like * one. */ #ifdef MINEFIELD /* * Minefield - a Windows equivalent for Electric Fence */ #define PAGESIZE 4096 /* * Design: * * We start by reserving as much virtual address space as Windows * will sensibly (or not sensibly) let us have. We flag it all as * invalid memory. * * Any allocation attempt is satisfied by committing one or more * pages, with an uncommitted page on either side. The returned * memory region is jammed up against the _end_ of the pages. * * Freeing anything causes instantaneous decommitment of the pages * involved, so stale pointers are caught as soon as possible. */ static int minefield_initialised = 0; static void *minefield_region = NULL; static long minefield_size = 0; static long minefield_npages = 0; static long minefield_curpos = 0; static unsigned short *minefield_admin = NULL; static void *minefield_pages = NULL; static void minefield_admin_hide(int hide) { int access = hide ? PAGE_NOACCESS : PAGE_READWRITE; VirtualProtect(minefield_admin, minefield_npages * 2, access, NULL); } static void minefield_init(void) { int size; int admin_size; int i; for (size = 0x40000000; size > 0; size = ((size >> 3) * 7) & ~0xFFF) { minefield_region = VirtualAlloc(NULL, size, MEM_RESERVE, PAGE_NOACCESS); if (minefield_region) break; } minefield_size = size; /* * Firstly, allocate a section of that to be the admin block. * We'll need a two-byte field for each page. */ minefield_admin = minefield_region; minefield_npages = minefield_size / PAGESIZE; admin_size = (minefield_npages * 2 + PAGESIZE - 1) & ~(PAGESIZE - 1); minefield_npages = (minefield_size - admin_size) / PAGESIZE; minefield_pages = (char *) minefield_region + admin_size; /* * Commit the admin region. */ VirtualAlloc(minefield_admin, minefield_npages * 2, MEM_COMMIT, PAGE_READWRITE); /* * Mark all pages as unused (0xFFFF). */ for (i = 0; i < minefield_npages; i++) minefield_admin[i] = 0xFFFF; /* * Hide the admin region. */ minefield_admin_hide(1); minefield_initialised = 1; } static void minefield_bomb(void) { div(1, *(int *) minefield_pages); } static void *minefield_alloc(int size) { int npages; int pos, lim, region_end, region_start; int start; int i; npages = (size + PAGESIZE - 1) / PAGESIZE; minefield_admin_hide(0); /* * Search from current position until we find a contiguous * bunch of npages+2 unused pages. */ pos = minefield_curpos; lim = minefield_npages; while (1) { /* Skip over used pages. */ while (pos < lim && minefield_admin[pos] != 0xFFFF) pos++; /* Count unused pages. */ start = pos; while (pos < lim && pos - start < npages + 2 && minefield_admin[pos] == 0xFFFF) pos++; if (pos - start == npages + 2) break; /* If we've reached the limit, reset the limit or stop. */ if (pos >= lim) { if (lim == minefield_npages) { /* go round and start again at zero */ lim = minefield_curpos; pos = 0; } else { minefield_admin_hide(1); return NULL; } } } minefield_curpos = pos - 1; /* * We have npages+2 unused pages starting at start. We leave * the first and last of these alone and use the rest. */ region_end = (start + npages + 1) * PAGESIZE; region_start = region_end - size; /* FIXME: could align here if we wanted */ /* * Update the admin region. */ for (i = start + 2; i < start + npages + 1; i++) minefield_admin[i] = 0xFFFE; /* used but no region starts here */ minefield_admin[start + 1] = region_start % PAGESIZE; minefield_admin_hide(1); VirtualAlloc((char *) minefield_pages + region_start, size, MEM_COMMIT, PAGE_READWRITE); return (char *) minefield_pages + region_start; } static void minefield_free(void *ptr) { int region_start, i, j; minefield_admin_hide(0); region_start = (char *) ptr - (char *) minefield_pages; i = region_start / PAGESIZE; if (i < 0 || i >= minefield_npages || minefield_admin[i] != region_start % PAGESIZE) minefield_bomb(); for (j = i; j < minefield_npages && minefield_admin[j] != 0xFFFF; j++) { minefield_admin[j] = 0xFFFF; } VirtualFree(ptr, j * PAGESIZE - region_start, MEM_DECOMMIT); minefield_admin_hide(1); } static int minefield_get_size(void *ptr) { int region_start, i, j; minefield_admin_hide(0); region_start = (char *) ptr - (char *) minefield_pages; i = region_start / PAGESIZE; if (i < 0 || i >= minefield_npages || minefield_admin[i] != region_start % PAGESIZE) minefield_bomb(); for (j = i; j < minefield_npages && minefield_admin[j] != 0xFFFF; j++); minefield_admin_hide(1); return j * PAGESIZE - region_start; } static void *minefield_c_malloc(size_t size) { if (!minefield_initialised) minefield_init(); return minefield_alloc(size); } static void minefield_c_free(void *p) { if (!minefield_initialised) minefield_init(); minefield_free(p); } /* * realloc _always_ moves the chunk, for rapid detection of code * that assumes it won't. */ static void *minefield_c_realloc(void *p, size_t size) { size_t oldsize; void *q; if (!minefield_initialised) minefield_init(); q = minefield_alloc(size); oldsize = minefield_get_size(p); memcpy(q, p, (oldsize < size ? oldsize : size)); minefield_free(p); return q; } #endif /* MINEFIELD */ #ifdef MALLOC_LOG static FILE *fp = NULL; static char *mlog_file = NULL; static int mlog_line = 0; void mlog(char *file, int line) { mlog_file = file; mlog_line = line; if (!fp) { fp = fopen("alcrypto_mem.log", "w"); setvbuf(fp, NULL, _IONBF, BUFSIZ); } if (fp) fprintf(fp, "%s:%d: ", file, line); } #endif void *safemalloc(size_t size) { void *p; #ifdef MINEFIELD p = minefield_c_malloc(size); #else p = malloc(size); #endif if (!p) { char str[200]; #ifdef MALLOC_LOG sprintf(str, "Out of memory! (%s:%d, size=%d)", mlog_file, mlog_line, size); fprintf(fp, "*** %s\n", str); fclose(fp); #else strcpy(str, "Out of memory!"); #endif MessageBox(NULL, str, "ALCrypto Fatal Error", MB_SYSTEMMODAL | MB_ICONERROR | MB_OK); cleanup_exit(1); } #ifdef MALLOC_LOG if (fp) fprintf(fp, "malloc(%d) returns %p\n", size, p); #endif return p; } void *saferealloc(void *ptr, size_t size) { void *p; if (!ptr) { #ifdef MINEFIELD p = minefield_c_malloc(size); #else p = malloc(size); #endif } else { #ifdef MINEFIELD p = minefield_c_realloc(ptr, size); #else p = realloc(ptr, size); #endif } if (!p) { char str[200]; #ifdef MALLOC_LOG sprintf(str, "Out of memory! (%s:%d, size=%d)", mlog_file, mlog_line, size); fprintf(fp, "*** %s\n", str); fclose(fp); #else strcpy(str, "Out of memory!"); #endif MessageBox(NULL, str, "ALCrypto Fatal Error", MB_SYSTEMMODAL | MB_ICONERROR | MB_OK); cleanup_exit(1); } #ifdef MALLOC_LOG if (fp) fprintf(fp, "realloc(%p,%d) returns %p\n", ptr, size, p); #endif return p; } void safefree(void *ptr) { if (ptr) { #ifdef MALLOC_LOG if (fp) fprintf(fp, "free(%p)\n", ptr); #endif #ifdef MINEFIELD minefield_c_free(ptr); #else free(ptr); #endif } #ifdef MALLOC_LOG else if (fp) fprintf(fp, "freeing null pointer - no action taken\n"); #endif } /* ---------------------------------------------------------------------- * Debugging routines. */ #ifdef _DEBUG static FILE *debug_fp = NULL; static int debug_got_console = 0; static void dputs(char *buf) { DWORD dw; if (!debug_got_console) { AllocConsole(); debug_got_console = 1; } if (!debug_fp) { debug_fp = fopen("debug.log", "w"); } WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), buf, strlen(buf), &dw, NULL); fputs(buf, debug_fp); fflush(debug_fp); } void dprintf(char *fmt, ...) { char buf[2048]; va_list ap; va_start(ap, fmt); vsprintf(buf, fmt, ap); dputs(buf); va_end(ap); } void debug_memdump(void *buf, int len, int L) { int i; unsigned char *p = buf; char foo[17]; if (L) { int delta; dprintf("\t%d (0x%x) bytes:\n", len, len); delta = 15 & (int) p; p -= delta; len += delta; } for (; 0 < len; p += 16, len -= 16) { dputs(" "); if (L) dprintf("%p: ", p); strcpy(foo, "................"); /* sixteen dots */ for (i = 0; i < 16 && i < len; ++i) { if (&p[i] < (unsigned char *) buf) { dputs(" "); /* 3 spaces */ foo[i] = ' '; } else { dprintf("%c%02.2x", &p[i] != (unsigned char *) buf && i % 4 ? '.' : ' ', p[i] ); if (p[i] >= ' ' && p[i] <= '~') foo[i] = (char) p[i]; } } foo[i] = '\0'; dprintf("%*s%s\n", (16 - i) * 3 + 2, "", foo); } } #endif /* def DEBUG */ /* * Find the random seed file path and store it in `seedpath'. */ static void get_seedpath(void) { HKEY rkey; DWORD type, size; size = sizeof(seedpath); if (RegOpenKey(HKEY_CURRENT_USER, REG_HOME, &rkey) == ERROR_SUCCESS) { i... [truncated message content] |