SHARKEncryption::SHARKEncryption(const byte *key, unsigned int keyLen, unsigned int rounds) : SHARKBase(rounds) { InitEncryptionRoundKeys(key, keyLen, rounds, roundkeys); #ifdef IS_LITTLE_ENDIAN roundkeys[0] = byteReverse(roundkeys[0]); roundkeys[rounds] = byteReverse(roundkeys[rounds]); #endif }
// construct an SHARKEncryption object with fixed round keys, to be used to initialize actual round keys SHARKEncryption::SHARKEncryption() : SHARKBase(DEFAULT_ROUNDS) { for (unsigned int i=0; i<DEFAULT_ROUNDS; i++) roundkeys[i] = cbox[0][i]; roundkeys[DEFAULT_ROUNDS] = SHARKTransform(cbox[0][DEFAULT_ROUNDS]); #ifdef IS_LITTLE_ENDIAN roundkeys[0] = byteReverse(roundkeys[0]); roundkeys[DEFAULT_ROUNDS] = byteReverse(roundkeys[DEFAULT_ROUNDS]); #endif }
// Put 8 bytes back into user's buffer in LITTLE-endian order static inline void PUTBLOCK(byte *block, word32 a, word32 b, word32 c, word32 d) { #ifdef IS_LITTLE_ENDIAN *(word32 *)block = a; *(word32 *)(block+4) = b; *(word32 *)(block+8) = c; *(word32 *)(block+12) = d; #else *(word32 *)block = byteReverse(a); *(word32 *)(block+4) = byteReverse(b); *(word32 *)(block+8) = byteReverse(c); *(word32 *)(block+12) = byteReverse(d); #endif }
void SHA1SKey(char *x) { GChecksum *checksum; guint8 digest[20]; gsize digest_len = sizeof (digest); guint32 *results; checksum = g_checksum_new (G_CHECKSUM_SHA1); g_checksum_update (checksum, (const guchar *) x, SKEY_SIZE); g_checksum_get_digest (checksum, digest, &digest_len); g_assert (digest_len == 20); results = (guint32 *) digest; #ifndef WORDS_BIGENDIAN HTONDIGEST(results); #else byteReverse((unsigned char *)digest, 5); #endif results[0] ^= results[2]; results[1] ^= results[3]; results[0] ^= results[4]; memcpy((void *)x, (void *)results, SKEY_SIZE); g_checksum_free (checksum); }
ANONYMOUS_NAMESPACE_BEGIN // Fetch 8 bytes from user's buffer into "a", "b", "c", "d" // in LITTLE-endian order static inline void GETBLOCK(const byte *block, word32 &a, word32 &b, word32 &c, word32 &d) { #ifdef IS_LITTLE_ENDIAN a = *(word32 *)block; b = *(word32 *)(block+4); c = *(word32 *)(block+8); d = *(word32 *)(block+12); #else a = byteReverse(*(word32 *)block); b = byteReverse(*(word32 *)(block+4)); c = byteReverse(*(word32 *)(block+8)); d = byteReverse(*(word32 *)(block+12)); #endif }
void RIPEMD160::HashBlock(const word32 *input) { #ifdef IS_LITTLE_ENDIAN Transform(digest, input); #else byteReverse(data.ptr, input, (unsigned int)DATASIZE); Transform(digest, data); #endif }
void MD5MAC::CorrectEndianess(word32 *out, const word32 *in, unsigned int byteCount) { #ifndef IS_LITTLE_ENDIAN byteReverse(out, in, byteCount); #else if (in!=out) memcpy(out, in, byteCount); #endif }
SHARKDecryption::SHARKDecryption(const byte *key, unsigned int keyLen, unsigned int rounds) : SHARKBase(rounds) { InitEncryptionRoundKeys(key, keyLen, rounds, roundkeys); unsigned int i; // transform encryption round keys into decryption round keys for (i=0; i<rounds/2; i++) std::swap(roundkeys[i], roundkeys[rounds-i]); for (i=1; i<rounds; i++) roundkeys[i] = SHARKTransform(roundkeys[i]); #ifdef IS_LITTLE_ENDIAN roundkeys[0] = byteReverse(roundkeys[0]); roundkeys[rounds] = byteReverse(roundkeys[rounds]); #endif }
static void cr_hashstring(char *src, char *trg) { unsigned char md[16]; MD5_CTX ctx; MD5Init(&ctx); MD5Update(&ctx, (unsigned char*)src, strlen(src)); MD5Final(md, &ctx); /* Endianness fix */ byteReverse(md, 4); ircsnprintf(trg, 9, "%08x", *(u_int32_t *) &md[0]); ircsnprintf(trg + 8, 9, "%08x", *(u_int32_t *) &md[4]); ircsnprintf(trg + 16, 9, "%08x", *(u_int32_t *) &md[8]); ircsnprintf(trg + 24, 9, "%08x", *(u_int32_t *) &md[12]); }
void Blowfish::ProcessBlock(const byte *in, byte *out) const { #ifdef IS_LITTLE_ENDIAN word32 left = byteReverse(*(word32 *)in); word32 right = byteReverse(*(word32 *)(in+4)); #else word32 left = *(word32 *)in; word32 right = *(word32 *)(in+4); #endif const word32 *const s=sbox; const word32 *p=pbox; left ^= p[0]; for (unsigned i=0; i<ROUNDS/2; i++) { right ^= (((s[GETBYTE(left,3)] + s[256+GETBYTE(left,2)]) ^ s[2*256+GETBYTE(left,1)]) + s[3*256+GETBYTE(left,0)]) ^ p[2*i+1]; left ^= (((s[GETBYTE(right,3)] + s[256+GETBYTE(right,2)]) ^ s[2*256+GETBYTE(right,1)]) + s[3*256+GETBYTE(right,0)]) ^ p[2*i+2]; } right ^= p[ROUNDS+1]; #ifdef IS_LITTLE_ENDIAN *(word32 *)out = byteReverse(right); *(word32 *)(out+4) = byteReverse(left); #else *(word32 *)out = right; *(word32 *)(out+4) = left; #endif }
void SHARKBase::InitEncryptionRoundKeys(const byte *key, unsigned int keyLen, unsigned int rounds, word64 *roundkeys) { // concatenate key enought times to fill a for (unsigned int i=0; i<(rounds+1)*8; i++) ((byte *)roundkeys)[i] = key[i%keyLen]; SHARKEncryption e; byte IV[8] = {0,0,0,0,0,0,0,0}; CFBEncryption cfb(e, IV); cfb.ProcessString((byte *)roundkeys, (rounds+1)*8); #ifdef IS_LITTLE_ENDIAN byteReverse(roundkeys, roundkeys, (rounds+1)*8); #endif roundkeys[rounds] = SHARKTransform(roundkeys[rounds]); }
int SHA1Keycrunch(char *result, const char *seed, const char *passphrase) { char *buf; gsize len; GChecksum *checksum; guint8 digest[20]; gsize digest_len = sizeof (digest); guint32 *results; len = strlen(seed) + strlen(passphrase); if ((buf = (char *)g_try_malloc(len+1)) == NULL) return -1; strcpy(buf, seed); skey_lowcase(buf); strcat(buf, passphrase); skey_sevenbit(buf); checksum = g_checksum_new (G_CHECKSUM_SHA1); g_checksum_update (checksum, (const guchar *) buf, len); g_free(buf); g_checksum_get_digest (checksum, digest, &digest_len); g_assert (digest_len == 20); results = (guint32 *) digest; #ifndef WORDS_BIGENDIAN HTONDIGEST(results); #else byteReverse((unsigned char *)digest, 5); #endif results = (guint32 *) digest; results[0] ^= results[2]; results[1] ^= results[3]; results[0] ^= results[4]; memcpy((void *)result, (void *)results, SKEY_SIZE); g_checksum_free (checksum); return 0; }
void SEAL::Generate(word32 in, byte *out) const { word32 a, b, c, d, n1, n2, n3, n4; unsigned int p, q; word32 *wout = (word32 *)out; for (unsigned int l=0; l<L/8192; l++) { a = in ^ R[4*l]; b = rotrFixed(in, 8U) ^ R[4*l+1]; c = rotrFixed(in, 16U) ^ R[4*l+2]; d = rotrFixed(in, 24U) ^ R[4*l+3]; #define Ttab(x) *(word32 *)((byte *)T.ptr+x) for (unsigned int j=0; j<2; j++) { p = a & 0x7fc; b += Ttab(p); a = rotrFixed(a, 9U); p = b & 0x7fc; c += Ttab(p); b = rotrFixed(b, 9U); p = c & 0x7fc; d += Ttab(p); c = rotrFixed(c, 9U); p = d & 0x7fc; a += Ttab(p); d = rotrFixed(d, 9U); } n1 = d; n2 = b; n3 = a; n4 = c; p = a & 0x7fc; b += Ttab(p); a = rotrFixed(a, 9U); p = b & 0x7fc; c += Ttab(p); b = rotrFixed(b, 9U); p = c & 0x7fc; d += Ttab(p); c = rotrFixed(c, 9U); p = d & 0x7fc; a += Ttab(p); d = rotrFixed(d, 9U); // generate 8192 bits for (unsigned int i=0; i<64; i++) { p = a & 0x7fc; a = rotrFixed(a, 9U); b += Ttab(p); b ^= a; q = b & 0x7fc; b = rotrFixed(b, 9U); c ^= Ttab(q); c += b; p = (p+c) & 0x7fc; c = rotrFixed(c, 9U); d += Ttab(p); d ^= c; q = (q+d) & 0x7fc; d = rotrFixed(d, 9U); a ^= Ttab(q); a += d; p = (p+a) & 0x7fc; b ^= Ttab(p); a = rotrFixed(a, 9U); q = (q+b) & 0x7fc; c += Ttab(q); b = rotrFixed(b, 9U); p = (p+c) & 0x7fc; d ^= Ttab(p); c = rotrFixed(c, 9U); q = (q+d) & 0x7fc; d = rotrFixed(d, 9U); a += Ttab(q); #ifdef IS_LITTLE_ENDIAN wout[0] = byteReverse(b + S[4*i+0]); wout[1] = byteReverse(c ^ S[4*i+1]); wout[2] = byteReverse(d + S[4*i+2]); wout[3] = byteReverse(a ^ S[4*i+3]); #else wout[0] = b + S[4*i+0]; wout[1] = c ^ S[4*i+1]; wout[2] = d + S[4*i+2]; wout[3] = a ^ S[4*i+3]; #endif wout += 4; if (i & 1) { a += n3; b += n4; c ^= n3; d ^= n4; } else { a += n1; b += n2; c ^= n1; d ^= n2; } } } a = b = c = d = n1 = n2 = n3 = n4 = 0; p = q = 0; }