admin 管理员组

文章数量: 887021


2023年12月17日发(作者:linux实训心得)

extern "C" {#endif/* version */#define CRYPT 0x0096#define SCRYPT "0.96"/* error codes [will be expanded in future releases] */enum { CRYPT_OK=0, /* Result OK */ CRYPT_FAIL_TESTVECTOR, /* Algorithm failed test vectors */ CRYPT_INVALID_ARG, /* Generic invalid argument */ };/* type of argument checking, 0=default, 1=fatal and 2=none */#define ARGTYPE 0#ifdef _MSC_VER #define CONST64(n) n ## ui64 typedef unsigned __int64 ulong64;#else #define CONST64(n) n ## ULL typedef unsigned long long ulong64;#endiftypedef unsigned long ulong32;/* Controls endianess and size of registers. Leave uncommented to get platform neutral [slower] code *//* detect x86-32 machines somewhat */#if defined(INTEL_CC) || (defined(_MSC_VER) && defined(WIN32)) || (defined(__GNUC__) && (defined(__DJGPP__) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__i386__))) #define ENDIAN_LITTLE #define ENDIAN_32BITWORD#endif/* detects MIPS R5900 processors (PS2) */#if (defined(__R5900) || defined(R5900) || defined(__R5900__)) && (defined(_mips) || defined(__mips__) || defined(mips)) #define ENDIAN_LITTLE #define ENDIAN_64BITWORD#endif/* #define ENDIAN_LITTLE *//* #define ENDIAN_BIG *//* #define ENDIAN_32BITWORD *//* #define ENDIAN_64BITWORD */#if (defined(ENDIAN_BIG) || defined(ENDIAN_LITTLE)) && !(defined(ENDIAN_32BITWORD) || defined(ENDIAN_64BITWORD)) #error You must specify a word size as well as endianess in mycrypt_cfg.h#endif#if !(defined(ENDIAN_BIG) || defined(ENDIAN_LITTLE)) #define ENDIAN_NEUTRAL#endif#ifdef ENDIAN_NEUTRAL#define STORE32L(x, y) { (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); }#define LOAD32L(x, y) { x = ((unsigned long)((y)[3] & 255)<<24) | ((unsigned long)((y)[2] & 255)<<16) | ((unsigned long)((y)[1] & 255)<<8) | ((unsigned long)((y)[0] & 255)); }

#define STORE64L(x, y) { (y)[7] = (unsigned char)(((x)>>56)&255); (y)[6] = (unsigned char)(((x)>>48)&255); (y)[5] = (unsigned char)(((x)>>40)&255); (y)[4] = (unsigned char)(((x)>>32)&255); (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); }#define LOAD64L(x, y) { x = (((ulong64)((y)[7] & 255))<<56)|(((ulong64)((y)[6] & 255))<<48)| (((ulong64)((y)[5] & 255))<<40)|(((ulong64)((y)[4] & 255))<<32)| (((ulong64)((y)[3] & 255))<<24)|(((ulong64)((y)[2] & 255))<<16)| (((ulong64)((y)[1] & 255))<<8)|(((ulong64)((y)[0] & 255))); }#define STORE32H(x, y) { (y)[0] = (unsigned char)(((x)>>24)&255); (y)[1] = (unsigned char)(((x)>>16)&255); (y)[2] = (unsigned char)(((x)>>8)&255); (y)[3] = (unsigned char)((x)&255); }#define LOAD32H(x, y) { x = ((unsigned long)((y)[0] & 255)<<24) | ((unsigned long)((y)[1] & 255)<<16) | ((unsigned long)((y)[2] & 255)<<8) | ((unsigned long)((y)[3] & 255)); }#define STORE64H(x, y) { (y)[0] = (unsigned char)(((x)>>56)&255); (y)[1] = (unsigned char)(((x)>>48)&255); (y)[2] = (unsigned char)(((x)>>40)&255); (y)[3] = (unsigned char)(((x)>>32)&255); (y)[4] = (unsigned char)(((x)>>24)&255); (y)[5] = (unsigned char)(((x)>>16)&255); (y)[6] = (unsigned char)(((x)>>8)&255); (y)[7] = (unsigned char)((x)&255); }#define LOAD64H(x, y) { x = (((ulong64)((y)[0] & 255))<<56)|(((ulong64)((y)[1] & 255))<<48) | (((ulong64)((y)[2] & 255))<<40)|(((ulong64)((y)[3] & 255))<<32) | (((ulong64)((y)[4] & 255))<<24)|(((ulong64)((y)[5] & 255))<<16) | (((ulong64)((y)[6] & 255))<<8)|(((ulong64)((y)[7] & 255))); }#endif /* ENDIAN_NEUTRAL */#ifdef ENDIAN_LITTLE#define STORE32H(x, y) { (y)[0] = (unsigned char)(((x)>>24)&255); (y)[1] = (unsigned char)(((x)>>16)&255); (y)[2] = (unsigned char)(((x)>>8)&255); (y)[3] = (unsigned char)((x)&255); }#define LOAD32H(x, y) { x = ((unsigned long)((y)[0] & 255)<<24) | ((unsigned long)((y)[1] & 255)<<16) | ((unsigned long)((y)[2] & 255)<<8) | ((unsigned long)((y)[3] & 255)); }#define STORE64H(x, y) { (y)[0] = (unsigned char)(((x)>>56)&255); (y)[1] = (unsigned char)(((x)>>48)&255); (y)[2] = (unsigned char)(((x)>>40)&255); (y)[3] = (unsigned char)(((x)>>32)&255); (y)[4] = (unsigned char)(((x)>>24)&255); (y)[5] = (unsigned char)(((x)>>16)&255); (y)[6] = (unsigned char)(((x)>>8)&255); (y)[7] = (unsigned char)((x)&255); }#define LOAD64H(x, y) { x = (((ulong64)((y)[0] & 255))<<56)|(((ulong64)((y)[1] & 255))<<48) | (((ulong64)((y)[2] & 255))<<40)|(((ulong64)((y)[3] & 255))<<32) | (((ulong64)((y)[4] & 255))<<24)|(((ulong64)((y)[5] & 255))<<16) | (((ulong64)((y)[6] & 255))<<8)|(((ulong64)((y)[7] & 255))); }#ifdef ENDIAN_32BITWORD

#define STORE32L(x, y) { unsigned long __t = (x); memcpy(y, &__t, 4); }#define LOAD32L(x, y) memcpy(&(x), y, 4);#define STORE64L(x, y) { (y)[7] = (unsigned char)(((x)>>56)&255); (y)[6] = (unsigned char)(((x)>>48)&255); (y)[5] = (unsigned char)(((x)>>40)&255); (y)[4] = (unsigned char)(((x)>>32)&255); (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); }

(y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); }#define LOAD64L(x, y) { x = (((ulong64)((y)[7] & 255))<<56)|(((ulong64)((y)[6] & 255))<<48)| (((ulong64)((y)[5] & 255))<<40)|(((ulong64)((y)[4] & 255))<<32)| (((ulong64)((y)[3] & 255))<<24)|(((ulong64)((y)[2] & 255))<<16)| (((ulong64)((y)[1] & 255))<<8)|(((ulong64)((y)[0] & 255))); }#else /* 64-bit words then */#define STORE32L(x, y) { unsigned long __t = (x); memcpy(y, &__t, 4); }#define LOAD32L(x, y) { memcpy(&(x), y, 4); x &= 0xFFFFFFFF; }#define STORE64L(x, y) { ulong64 __t = (x); memcpy(y, &__t, 8); }#define LOAD64L(x, y) { memcpy(&(x), y, 8); }#endif /* ENDIAN_64BITWORD */#endif /* ENDIAN_LITTLE */#ifdef ENDIAN_BIG#define STORE32L(x, y) { (y)[0] = (unsigned char)(((x)>>24)&255); (y)[1] = (unsigned char)(((x)>>16)&255); (y)[2] = (unsigned char)(((x)>>8)&255); (y)[3] = (unsigned char)((x)&255); }#define LOAD32L(x, y) { x = ((unsigned long)((y)[0] & 255)<<24) | ((unsigned long)((y)[1] & 255)<<16) | ((unsigned long)((y)[2] & 255)<<8) | ((unsigned long)((y)[3] & 255)); }#define STORE64L(x, y) { (y)[0] = (unsigned char)(((x)>>56)&255); (y)[1] = (unsigned char)(((x)>>48)&255); (y)[2] = (unsigned char)(((x)>>40)&255); (y)[3] = (unsigned char)(((x)>>32)&255); (y)[4] = (unsigned char)(((x)>>24)&255); (y)[5] = (unsigned char)(((x)>>16)&255); (y)[6] = (unsigned char)(((x)>>8)&255); (y)[7] = (unsigned char)((x)&255); }#define LOAD64L(x, y) { x = (((ulong64)((y)[0] & 255))<<56)|(((ulong64)((y)[1] & 255))<<48) | (((ulong64)((y)[2] & 255))<<40)|(((ulong64)((y)[3] & 255))<<32) | (((ulong64)((y)[4] & 255))<<24)|(((ulong64)((y)[5] & 255))<<16) | (((ulong64)((y)[6] & 255))<<8)|(((ulong64)((y)[7] & 255))); }#ifdef ENDIAN_32BITWORD

#define STORE32H(x, y) { unsigned long __t = (x); memcpy(y, &__t, 4); }#define LOAD32H(x, y) memcpy(&(x), y, 4);#define STORE64H(x, y) { (y)[7] = (unsigned char)(((x)>>56)&255); (y)[6] = (unsigned char)(((x)>>48)&255); (y)[5] = (unsigned char)(((x)>>40)&255); (y)[4] = (unsigned char)(((x)>>32)&255); (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); }#define LOAD64H(x, y) { x = (((ulong64)((y)[7] & 255))<<56)|(((ulong64)((y)[6] & 255))<<48)| (((ulong64)((y)[5] & 255))<<40)|(((ulong64)((y)[4] & 255))<<32)| (((ulong64)((y)[3] & 255))<<24)|(((ulong64)((y)[2] & 255))<<16)| (((ulong64)((y)[1] & 255))<<8)|(((ulong64)((y)[0] & 255))); }#else /* 64-bit words then */#define STORE32H(x, y)

#define STORE32H(x, y) { unsigned long __t = (x); memcpy(y, &__t, 4); }#define LOAD32H(x, y) { memcpy(&(x), y, 4); x &= 0xFFFFFFFF; }#define STORE64H(x, y) { ulong64 __t = (x); memcpy(y, &__t, 8); }#define LOAD64H(x, y) { memcpy(&(x), y, 8); }#endif /* ENDIAN_64BITWORD */#endif /* ENDIAN_BIG */#define BSWAP(x) ( ((x>>24)&0x000000FFUL) | ((x<<24)&0xFF000000UL) | ((x>>8)&0x0000FF00UL) | ((x<<8)&0x00FF0000UL) )#ifdef _MSC_VER/* instrinsic rotate */#include #pragma intrinsic(_lrotr,_lrotl)#define ROR(x,n) _lrotr(x,n)#define ROL(x,n) _lrotl(x,n)#elif defined(__GNUC__) && defined(__i386__) && !defined(INTEL_CC)static inline unsigned long ROL(unsigned long word, int i){ __asm__("roll %%cl,%0" :"=r" (word) :"0" (word),"c" (i)); return word;}static inline unsigned long ROR(unsigned long word, int i){ __asm__("rorl %%cl,%0" :"=r" (word) :"0" (word),"c" (i)); return word;}#else/* rotates the hard way */#define ROL(x, y) ( (((unsigned long)(x)<<(unsigned long)((y)&31)) | (((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL)#define ROR(x, y) ( ((((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)((y)&31)) | ((unsigned long)(x)<<(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL)#endif#define ROL64(x, y) ( (((x)<<((ulong64)(y)&63)) | (((x)&CONST64(0xFFFFFFFFFFFFFFFF))>>((ulong64)64-((y)&63)))) & CONST64(0xFFFFFFFFFFFFFFFF))#define ROR64(x, y) ( ((((x)&CONST64(0xFFFFFFFFFFFFFFFF))>>((ulong64)(y)&CONST64(63))) | ((x)<<((ulong64)(64-((y)&CONST64(63)))))) & CONST64(0xFFFFFFFFFFFFFFFF))#undef MAX#undef MIN#define MAX(x, y) ( ((x)>(y))?(x):(y) )#define MIN(x, y) ( ((x)<(y))?(x):(y) )typedef struct md5_state {

typedef struct md5_state { ulong64 length; ulong32 state[4], curlen; unsigned char buf[64];}md5_state;extern void md5_init(md5_state * md);extern int md5_process(md5_state * md, const unsigned char *buf, unsigned long len);extern int md5_done(md5_state * md, unsigned char *hash);extern int md5_test(void);#if ARGTYPE == 0#include /* this is the default LibTomCrypt macro */extern void crypt_argchk(char *v, char *s, int d);#define _ARGCHK(x) if (!(x)) { crypt_argchk(#x, __FILE__, __LINE__); }#elif ARGTYPE == 1/* fatal type of error */#define _ARGCHK(x) assert((x))#elif ARGTYPE == 2#define _ARGCHK(x)

#endif#ifdef __cplusplus }#endif#endif /* CRYPT_H_ */主函数:#include "md5.h"#ifdef MD5#define F(x,y,z) (z ^ (x & (y ^ z)))#define G(x,y,z) (y ^ (z & (y ^ x)))#define H(x,y,z) (x^y^z)#define I(x,y,z) (y^(x|(~z)))#define FF(a,b,c,d,M,s,t) a = (a + F(b,c,d) + M + t); a = ROL(a, s) + b;

#define GG(a,b,c,d,M,s,t) a = (a + G(b,c,d) + M + t); a = ROL(a, s) + b;#define HH(a,b,c,d,M,s,t) a = (a + H(b,c,d) + M + t); a = ROL(a, s) + b;#define II(a,b,c,d,M,s,t) a = (a + I(b,c,d) + M + t); a = ROL(a, s) + b;#define max 10240#ifdef SMALL_CODEstatic const unsigned char Worder[64] = { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,

0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 1,6,11,0,5,10,15,4,9,14,3,8,13,2,7,12, 5,8,11,14,1,4,7,10,13,0,3,6,9,12,15,2, 0,7,14,5,12,3,10,1,8,15,6,13,4,11,2,9};static const unsigned char Rorder[64] = { 7,12,17,22,7,12,17,22,7,12,17,22,7,12,17,22, 5,9,14,20,5,9,14,20,5,9,14,20,5,9,14,20, 4,11,16,23,4,11,16,23,4,11,16,23,4,11,16,23, 6,10,15,21,6,10,15,21,6,10,15,21,6,10,15,21};static const ulong32 Korder[64] = {0xd76aa478UL, 0xe8c7b756UL, 0x242070dbUL, 0xc1bdceeeUL, 0xf57c0fafUL, 0x4787c62aUL, 0xa8304613UL, 0xfd469501UL,0x698098d8UL, 0x8b44f7afUL, 0xffff5bb1UL, 0x895cd7beUL, 0x6b901122UL, 0xfd987193UL, 0xa679438eUL, 0x49b40821UL,0xf61e2562UL, 0xc040b340UL, 0x265e5a51UL, 0xe9b6c7aaUL, 0xd62f105dUL, 0x02441453UL, 0xd8a1e681UL, 0xe7d3fbc8UL,0x21e1cde6UL, 0xc33707d6UL, 0xf4d50d87UL, 0x455a14edUL, 0xa9e3e905UL, 0xfcefa3f8UL, 0x676f02d9UL, 0x8d2a4c8aUL,0xfffa3942UL, 0x8771f681UL, 0x6d9d6122UL, 0xfde5380cUL, 0xa4beea44UL, 0x4bdecfa9UL, 0xf6bb4b60UL, 0xbebfbc70UL,0x289b7ec6UL, 0xeaa127faUL, 0xd4ef3085UL, 0x04881d05UL, 0xd9d4d039UL, 0xe6db99e5UL, 0x1fa27cf8UL, 0xc4ac5665UL,0xf4292244UL, 0x432aff97UL, 0xab9423a7UL, 0xfc93a039UL, 0x655b59c3UL, 0x8f0ccc92UL, 0xffeff47dUL, 0x85845dd1UL,0x6fa87e4fUL, 0xfe2ce6e0UL, 0xa3014314UL, 0x4e0811a1UL, 0xf7537e82UL, 0xbd3af235UL, 0x2ad7d2bbUL, 0xeb86d391UL};#endif

#include #if (ARGTYPE == 0)void crypt_argchk(char *v, char *s, int d){ fprintf(stderr, "_ARGCHK '%s' failure on line %d of file %sn", v, d, s); (void)raise(SIGABRT);}#endifvoid zeromem(void *dst, size_t len){ unsigned char *mem = (unsigned char *)dst; _ARGCHK(dst != NULL); while (len-- > 0) *mem++ = 0;}void burn_stack(unsigned long len){ unsigned char buf[32]; zeromem(buf, sizeof(buf)); if (len > (unsigned long)sizeof(buf)) burn_stack(len - sizeof(buf));}/*this function compress 512bits once,and it is called by md5_process and md5_done*/#ifdef CLEAN_STACKstatic void _md5_compress(md5_state *md, unsigned char *buf)#elsestatic void md5_compress(md5_state *md, unsigned char *buf)#endif{ ulong32 i, W[16], a, b, c, d;#ifdef SMALL_CODE ulong32 t;#endif /* copy the state into 512-bits into W[0..15] */

*/

int md5_process (md5_state * md, const unsigned char *buf, unsigned long len)

{

unsigned long n;

_ARGCHK(md != NULL);

_ARGCHK(buf != NULL);

if (md->curlen > sizeof(md->buf)) {

return CRYPT_INVALID_ARG;

}

while (len > 0) {

if (md->curlen == 0 && len >= 64) {

md5_compress (md, (unsigned char *)buf);

md->length += 64 * 8;

buf += 64;

len -= 64;

} else {

n = MIN(len, (len - md->curlen));

memcpy(md->buf + md->curlen, buf, (size_t)n);

md->curlen += n;

buf+= n;

len-= n;

if (md->curlen == len) {

md5_compress (md, md->buf);

md->length += 8*len;

md->curlen = 0;

}

}

}

return CRYPT_OK;

}int md5_done(md5_state * md, unsigned char *hash){ int i; _ARGCHK(md != NULL); _ARGCHK(hash != NULL); if (md->curlen >= sizeof(md->buf)) { return CRYPT_INVALID_ARG; }

md->length += md->curlen * 8; /* append the '1' bit */ md->buf[md->curlen++] = (unsigned char)0x80; /* if the length is currently above 56 bytes we append zeros * then compress. Then we can fall back to padding zeros and length * encoding like normal. */ if (md->curlen > 56) { while (md->curlen < 64) { md->buf[md->curlen++] = (unsigned char)0; } md5_compress(md, md->buf); md->curlen = 0; } /* pad upto 56 bytes of zeroes */ while (md->curlen < 56) { md->buf[md->curlen++] = (unsigned char)0; }

} /* store length */ STORE64L(md->length, md->buf+56); md5_compress(md, md->buf); /* copy output */ for (i = 0; i < 4; i++) { STORE32L(md->state[i], hash+(4*i)); }#ifdef CLEAN_STACK zeromem(md, sizeof(md5_state));#endif return CRYPT_OK;}int md5_test(void){ #ifndef LTC_TEST return CRYPT_NOP; #else

static const struct { char *msg; unsigned char hash[16]; } tests[] = { { "", { 0xd4, 0x1d, 0x8c, 0xd9, 0x8f, 0x00, 0xb2, 0x04,

0xe9, 0x80, 0x09, 0x98, 0xec, 0xf8, 0x42, 0x7e } }, { "a", {0x0c, 0xc1, 0x75, 0xb9, 0xc0, 0xf1, 0xb6, 0xa8,

0x31, 0xc3, 0x99, 0xe2, 0x69, 0x77, 0x26, 0x61 } }, { "abc", { 0x90, 0x01, 0x50, 0x98, 0x3c, 0xd2, 0x4f, 0xb0,

0xd6, 0x96, 0x3f, 0x7d, 0x28, 0xe1, 0x7f, 0x72 } }, { "message digest",

{ 0xf9, 0x6b, 0x69, 0x7d, 0x7c, 0xb7, 0x93, 0x8d,

0x52, 0x5a, 0x2f, 0x31, 0xaa, 0xf1, 0x61, 0xd0 } },

{ "abcdefghijklmnopqrstuvwxyz", { 0xc3, 0xfc, 0xd3, 0xd7, 0x61, 0x92, 0xe4, 0x00,

0x7d, 0xfb, 0x49, 0x6c, 0xca, 0x67, 0xe1, 0x3b } }, { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", { 0xd1, 0x74, 0xab, 0x98, 0xd2, 0x77, 0xd9, 0xf5,

0xa5, 0x61, 0x1c, 0x2c, 0x9f, 0x41, 0x9d, 0x9f } }, { "4567892345678923456789", { 0x57, 0xed, 0xf4, 0xa2, 0x2b, 0xe3, 0xc9, 0x55,

0xac, 0x49, 0xda, 0x2e, 0x21, 0x07, 0xb6, 0x7a } },

{ NULL, { 0 } } }; int i,j; unsigned char tmp[16]; md5_state md; for (i = 0; tests[i].msg != NULL; i++) { md5_init(&md); md5_process(&md, (unsigned char *)tests[i].msg, (unsigned long)strlen(tests[i].msg)); md5_done(&md, tmp); if (memcmp(tmp, tests[i].hash, 16) != 0) { return CRYPT_FAIL_TESTVECTOR; } for(j=0;j<16;j++) printf("%x",tmp[j]); printf("n"); } return CRYPT_OK; #endif


本文标签: 心得 实训 作者