00001
00020 #define ENDIAN_LITTLE
00021
00022
00023
00024 #define CRYPT 0x0116
00025 #define SCRYPT "1.16"
00026
00027
00028 #define MAXBLOCKSIZE 128
00029
00030
00031 #define TAB_SIZE 32
00032
00033
00034 enum {
00035 CRYPT_OK=0,
00036 CRYPT_ERROR,
00037 CRYPT_NOP,
00038
00039 CRYPT_INVALID_KEYSIZE,
00040 CRYPT_INVALID_ROUNDS,
00041 CRYPT_FAIL_TESTVECTOR,
00042
00043 CRYPT_BUFFER_OVERFLOW,
00044 CRYPT_INVALID_PACKET,
00045
00046 CRYPT_INVALID_PRNGSIZE,
00047 CRYPT_ERROR_READPRNG,
00048
00049 CRYPT_INVALID_CIPHER,
00050 CRYPT_INVALID_HASH,
00051 CRYPT_INVALID_PRNG,
00052
00053 CRYPT_MEM,
00054
00055 CRYPT_PK_TYPE_MISMATCH,
00056 CRYPT_PK_NOT_PRIVATE,
00057
00058 CRYPT_INVALID_ARG,
00059 CRYPT_FILE_NOTFOUND,
00060
00061 CRYPT_PK_INVALID_TYPE,
00062 CRYPT_PK_INVALID_SYSTEM,
00063 CRYPT_PK_DUP,
00064 CRYPT_PK_NOT_FOUND,
00065 CRYPT_PK_INVALID_SIZE,
00066
00067 CRYPT_INVALID_PRIME_SIZE,
00068 CRYPT_PK_INVALID_PADDING
00069 };
00070
00071
00072
00073 #define CONST64(n) n ## ULL
00074 typedef unsigned long long ulong64;
00075
00076 typedef unsigned ulong32;
00077
00078 #ifdef ENDIAN_NEUTRAL
00079
00080 #define STORE32L(x, y) \
00081 { (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \
00082 (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); }
00083
00084 #define LOAD32L(x, y) \
00085 { x = ((unsigned long)((y)[3] & 255)<<24) | \
00086 ((unsigned long)((y)[2] & 255)<<16) | \
00087 ((unsigned long)((y)[1] & 255)<<8) | \
00088 ((unsigned long)((y)[0] & 255)); }
00089
00090 #define STORE64L(x, y) \
00091 { (y)[7] = (unsigned char)(((x)>>56)&255); (y)[6] = (unsigned char)(((x)>>48)&255); \
00092 (y)[5] = (unsigned char)(((x)>>40)&255); (y)[4] = (unsigned char)(((x)>>32)&255); \
00093 (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \
00094 (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); }
00095
00096 #define LOAD64L(x, y) \
00097 { x = (((ulong64)((y)[7] & 255))<<56)|(((ulong64)((y)[6] & 255))<<48)| \
00098 (((ulong64)((y)[5] & 255))<<40)|(((ulong64)((y)[4] & 255))<<32)| \
00099 (((ulong64)((y)[3] & 255))<<24)|(((ulong64)((y)[2] & 255))<<16)| \
00100 (((ulong64)((y)[1] & 255))<<8)|(((ulong64)((y)[0] & 255))); }
00101
00102 #define STORE32H(x, y) \
00103 { (y)[0] = (unsigned char)(((x)>>24)&255); (y)[1] = (unsigned char)(((x)>>16)&255); \
00104 (y)[2] = (unsigned char)(((x)>>8)&255); (y)[3] = (unsigned char)((x)&255); }
00105
00106 #define LOAD32H(x, y) \
00107 { x = ((unsigned long)((y)[0] & 255)<<24) | \
00108 ((unsigned long)((y)[1] & 255)<<16) | \
00109 ((unsigned long)((y)[2] & 255)<<8) | \
00110 ((unsigned long)((y)[3] & 255)); }
00111
00112 #define STORE64H(x, y) \
00113 { (y)[0] = (unsigned char)(((x)>>56)&255); (y)[1] = (unsigned char)(((x)>>48)&255); \
00114 (y)[2] = (unsigned char)(((x)>>40)&255); (y)[3] = (unsigned char)(((x)>>32)&255); \
00115 (y)[4] = (unsigned char)(((x)>>24)&255); (y)[5] = (unsigned char)(((x)>>16)&255); \
00116 (y)[6] = (unsigned char)(((x)>>8)&255); (y)[7] = (unsigned char)((x)&255); }
00117
00118 #define LOAD64H(x, y) \
00119 { x = (((ulong64)((y)[0] & 255))<<56)|(((ulong64)((y)[1] & 255))<<48) | \
00120 (((ulong64)((y)[2] & 255))<<40)|(((ulong64)((y)[3] & 255))<<32) | \
00121 (((ulong64)((y)[4] & 255))<<24)|(((ulong64)((y)[5] & 255))<<16) | \
00122 (((ulong64)((y)[6] & 255))<<8)|(((ulong64)((y)[7] & 255))); }
00123
00124 #endif
00125
00126 #ifdef ENDIAN_LITTLE
00127
00128 #if !defined(LTC_NO_BSWAP) && (defined(INTEL_CC) || (defined(__GNUC__) && (defined(__DJGPP__) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__i386__) || defined(__x86_64__))))
00129
00130 #define STORE32H(x, y) \
00131 asm __volatile__ ( \
00132 "bswapl %0 \n\t" \
00133 "movl %0,(%1)\n\t" \
00134 "bswapl %0 \n\t" \
00135 ::"r"(x), "r"(y));
00136
00137 #define LOAD32H(x, y) \
00138 asm __volatile__ ( \
00139 "movl (%1),%0\n\t" \
00140 "bswapl %0\n\t" \
00141 :"=r"(x): "r"(y));
00142
00143 #else
00144
00145 #define STORE32H(x, y) \
00146 { (y)[0] = (unsigned char)(((x)>>24)&255); (y)[1] = (unsigned char)(((x)>>16)&255); \
00147 (y)[2] = (unsigned char)(((x)>>8)&255); (y)[3] = (unsigned char)((x)&255); }
00148
00149 #define LOAD32H(x, y) \
00150 { x = ((unsigned long)((y)[0] & 255)<<24) | \
00151 ((unsigned long)((y)[1] & 255)<<16) | \
00152 ((unsigned long)((y)[2] & 255)<<8) | \
00153 ((unsigned long)((y)[3] & 255)); }
00154
00155 #endif
00156
00157
00158
00159 #if !defined(LTC_NO_BSWAP) && (defined(__GNUC__) && defined(__x86_64__))
00160
00161 #define STORE64H(x, y) \
00162 asm __volatile__ ( \
00163 "bswapq %0 \n\t" \
00164 "movq %0,(%1)\n\t" \
00165 "bswapq %0 \n\t" \
00166 ::"r"(x), "r"(y));
00167
00168 #define LOAD64H(x, y) \
00169 asm __volatile__ ( \
00170 "movq (%1),%0\n\t" \
00171 "bswapq %0\n\t" \
00172 :"=r"(x): "r"(y));
00173
00174 #else
00175
00176 #define STORE64H(x, y) \
00177 { (y)[0] = (unsigned char)(((x)>>56)&255); (y)[1] = (unsigned char)(((x)>>48)&255); \
00178 (y)[2] = (unsigned char)(((x)>>40)&255); (y)[3] = (unsigned char)(((x)>>32)&255); \
00179 (y)[4] = (unsigned char)(((x)>>24)&255); (y)[5] = (unsigned char)(((x)>>16)&255); \
00180 (y)[6] = (unsigned char)(((x)>>8)&255); (y)[7] = (unsigned char)((x)&255); }
00181
00182 #define LOAD64H(x, y) \
00183 { x = (((ulong64)((y)[0] & 255))<<56)|(((ulong64)((y)[1] & 255))<<48) | \
00184 (((ulong64)((y)[2] & 255))<<40)|(((ulong64)((y)[3] & 255))<<32) | \
00185 (((ulong64)((y)[4] & 255))<<24)|(((ulong64)((y)[5] & 255))<<16) | \
00186 (((ulong64)((y)[6] & 255))<<8)|(((ulong64)((y)[7] & 255))); }
00187
00188 #endif
00189
00190 #ifdef ENDIAN_32BITWORD
00191
00192 #define STORE32L(x, y) \
00193 { ulong32 __t = (x); XMEMCPY(y, &__t, 4); }
00194
00195 #define LOAD32L(x, y) \
00196 XMEMCPY(&(x), y, 4);
00197
00198 #define STORE64L(x, y) \
00199 { (y)[7] = (unsigned char)(((x)>>56)&255); (y)[6] = (unsigned char)(((x)>>48)&255); \
00200 (y)[5] = (unsigned char)(((x)>>40)&255); (y)[4] = (unsigned char)(((x)>>32)&255); \
00201 (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \
00202 (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); }
00203
00204 #define LOAD64L(x, y) \
00205 { x = (((ulong64)((y)[7] & 255))<<56)|(((ulong64)((y)[6] & 255))<<48)| \
00206 (((ulong64)((y)[5] & 255))<<40)|(((ulong64)((y)[4] & 255))<<32)| \
00207 (((ulong64)((y)[3] & 255))<<24)|(((ulong64)((y)[2] & 255))<<16)| \
00208 (((ulong64)((y)[1] & 255))<<8)|(((ulong64)((y)[0] & 255))); }
00209
00210 #else
00211
00212 #define STORE32L(x, y) \
00213 { ulong32 __t = (x); XMEMCPY(y, &__t, 4); }
00214
00215 #define LOAD32L(x, y) \
00216 { XMEMCPY(&(x), y, 4); x &= 0xFFFFFFFF; }
00217
00218 #define STORE64L(x, y) \
00219 { ulong64 __t = (x); XMEMCPY(y, &__t, 8); }
00220
00221 #define LOAD64L(x, y) \
00222 { XMEMCPY(&(x), y, 8); }
00223
00224 #endif
00225
00226 #endif
00227
00228 #ifdef ENDIAN_BIG
00229 #define STORE32L(x, y) \
00230 { (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \
00231 (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); }
00232
00233 #define LOAD32L(x, y) \
00234 { x = ((unsigned long)((y)[3] & 255)<<24) | \
00235 ((unsigned long)((y)[2] & 255)<<16) | \
00236 ((unsigned long)((y)[1] & 255)<<8) | \
00237 ((unsigned long)((y)[0] & 255)); }
00238
00239 #define STORE64L(x, y) \
00240 { (y)[7] = (unsigned char)(((x)>>56)&255); (y)[6] = (unsigned char)(((x)>>48)&255); \
00241 (y)[5] = (unsigned char)(((x)>>40)&255); (y)[4] = (unsigned char)(((x)>>32)&255); \
00242 (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \
00243 (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); }
00244
00245 #define LOAD64L(x, y) \
00246 { x = (((ulong64)((y)[7] & 255))<<56)|(((ulong64)((y)[6] & 255))<<48) | \
00247 (((ulong64)((y)[5] & 255))<<40)|(((ulong64)((y)[4] & 255))<<32) | \
00248 (((ulong64)((y)[3] & 255))<<24)|(((ulong64)((y)[2] & 255))<<16) | \
00249 (((ulong64)((y)[1] & 255))<<8)|(((ulong64)((y)[0] & 255))); }
00250
00251 #ifdef ENDIAN_32BITWORD
00252
00253 #define STORE32H(x, y) \
00254 { ulong32 __t = (x); XMEMCPY(y, &__t, 4); }
00255
00256 #define LOAD32H(x, y) \
00257 XMEMCPY(&(x), y, 4);
00258
00259 #define STORE64H(x, y) \
00260 { (y)[0] = (unsigned char)(((x)>>56)&255); (y)[1] = (unsigned char)(((x)>>48)&255); \
00261 (y)[2] = (unsigned char)(((x)>>40)&255); (y)[3] = (unsigned char)(((x)>>32)&255); \
00262 (y)[4] = (unsigned char)(((x)>>24)&255); (y)[5] = (unsigned char)(((x)>>16)&255); \
00263 (y)[6] = (unsigned char)(((x)>>8)&255); (y)[7] = (unsigned char)((x)&255); }
00264
00265 #define LOAD64H(x, y) \
00266 { x = (((ulong64)((y)[0] & 255))<<56)|(((ulong64)((y)[1] & 255))<<48)| \
00267 (((ulong64)((y)[2] & 255))<<40)|(((ulong64)((y)[3] & 255))<<32)| \
00268 (((ulong64)((y)[4] & 255))<<24)|(((ulong64)((y)[5] & 255))<<16)| \
00269 (((ulong64)((y)[6] & 255))<<8)| (((ulong64)((y)[7] & 255))); }
00270
00271 #else
00272
00273 #define STORE32H(x, y) \
00274 { ulong32 __t = (x); XMEMCPY(y, &__t, 4); }
00275
00276 #define LOAD32H(x, y) \
00277 { XMEMCPY(&(x), y, 4); x &= 0xFFFFFFFF; }
00278
00279 #define STORE64H(x, y) \
00280 { ulong64 __t = (x); XMEMCPY(y, &__t, 8); }
00281
00282 #define LOAD64H(x, y) \
00283 { XMEMCPY(&(x), y, 8); }
00284
00285 #endif
00286 #endif
00287
00288 #define BSWAP(x) ( ((x>>24)&0x000000FFUL) | ((x<<24)&0xFF000000UL) | \
00289 ((x>>8)&0x0000FF00UL) | ((x<<8)&0x00FF0000UL) )
00290
00291
00292 #define ROLc ROL
00293 #define RORc ROR
00294
00295
00296
00297 #define ROL(x, y) ( (((unsigned long)(x)<<(unsigned long)((y)&31)) | (((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL)
00298 #define ROR(x, y) ( ((((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)((y)&31)) | ((unsigned long)(x)<<(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL)
00299
00300
00301 #define ROL64c ROL64
00302 #define ROR64c ROR64
00303
00304 #define ROL64(x, y) \
00305 ( (((x)<<((ulong64)(y)&63)) | \
00306 (((x)&CONST64(0xFFFFFFFFFFFFFFFF))>>((ulong64)64-((y)&63)))) & CONST64(0xFFFFFFFFFFFFFFFF))
00307
00308 #define ROR64(x, y) \
00309 ( ((((x)&CONST64(0xFFFFFFFFFFFFFFFF))>>((ulong64)(y)&CONST64(63))) | \
00310 ((x)<<((ulong64)(64-((y)&CONST64(63)))))) & CONST64(0xFFFFFFFFFFFFFFFF))
00311
00312 #ifndef MAX
00313 #define MAX(x, y) ( ((x)>(y))?(x):(y) )
00314 #endif
00315
00316 #ifndef MIN
00317 #define MIN(x, y) ( ((x)<(y))?(x):(y) )
00318 #endif
00319
00320
00321 #ifdef _MSC_VER
00322 #define byte(x, n) ((unsigned char)((x) >> (8 * (n))))
00323 #else
00324 #define byte(x, n) (((x) >> (8 * (n))) & 255)
00325 #endif
00326
00327
00328
00329
00330
00331 #include <signal.h>
00332
00333
00334 void crypt_argchk(char *v, char *s, int d);
00335 #define LTC_ARGCHK(x) if (!(x)) { crypt_argchk(#x, __FILE__, __LINE__); }
00336 #define LTC_ARGCHKVD(x) LTC_ARGCHK(x)
00337
00338 struct sha256_state
00339 {
00340 ulong64 length;
00341 ulong32 state[8], curlen;
00342 unsigned char buf[64];
00343 };
00344
00345 typedef union Hash_state {
00346 char dummy[1];
00347 struct sha256_state sha256;
00348 void *data;
00349 } hash_state;
00350
00351
00353 extern struct ltc_hash_descriptor
00354 {
00356 char *name;
00358 unsigned char ID;
00360 unsigned long hashsize;
00362 unsigned long blocksize;
00364 unsigned long OID[16];
00366 unsigned long OIDlen;
00367
00372 int (*init)(hash_state *hash);
00373
00380 int (*process)(hash_state *hash, const unsigned char *in, unsigned long inlen);
00381
00387 int (*done)(hash_state *hash, unsigned char *out);
00388
00392 int (*test)(void);
00393
00394
00395
00396
00397 int (*hmac_block)(const unsigned char *key, unsigned long keylen,
00398 const unsigned char *in, unsigned long inlen,
00399 unsigned char *out, unsigned long *outlen);
00400
00401 }
00402 hash_descriptor[];
00403
00409 int sha256_init(hash_state * md);
00410
00411
00419 int sha256_process(hash_state * md, const unsigned char *in,
00420 unsigned long inlen);
00421
00422
00429 int sha256_done(hash_state *md, unsigned char *hash);
00430
00431
00432 extern const struct ltc_hash_descriptor sha256_desc;
00433
00434
00435 #define HASH_PROCESS(func_name, compress_name, state_var, block_size) \
00436 int func_name (hash_state * md, const unsigned char *in, unsigned long inlen) \
00437 { \
00438 unsigned long n; \
00439 int err; \
00440 LTC_ARGCHK(md != NULL); \
00441 LTC_ARGCHK(in != NULL); \
00442 if (md-> state_var .curlen > sizeof(md-> state_var .buf)) { \
00443 return CRYPT_INVALID_ARG; \
00444 } \
00445 while (inlen > 0) { \
00446 if (md-> state_var .curlen == 0 && inlen >= block_size) { \
00447 if ((err = compress_name (md, (unsigned char *)in)) != CRYPT_OK) { \
00448 return err; \
00449 } \
00450 md-> state_var .length += block_size * 8; \
00451 in += block_size; \
00452 inlen -= block_size; \
00453 } else { \
00454 n = MIN(inlen, (block_size - md-> state_var .curlen)); \
00455 memcpy(md-> state_var .buf + md-> state_var.curlen, in, (size_t)n); \
00456 md-> state_var .curlen += n; \
00457 in += n; \
00458 inlen -= n; \
00459 if (md-> state_var .curlen == block_size) { \
00460 if ((err = compress_name (md, md-> state_var .buf)) != CRYPT_OK) { \
00461 return err; \
00462 } \
00463 md-> state_var .length += 8*block_size; \
00464 md-> state_var .curlen = 0; \
00465 } \
00466 } \
00467 } \
00468 return CRYPT_OK; \
00469 }
00470
00471 #include <string>
00472
00479 std::string sha256archivo(std::string narch);
00480