static ulong32 FL( ulong32 in, int round_no, symmetric_key *key ) { u16 l, r, a, b; /* split out the left and right halves */ l = (u16)(in>>16); r = (u16)(in)&0xFFFF; /* do the FL() operations */ a = (u16) (l & key->kasumi.KLi1[round_no]); r ^= ROL16(a,1); b = (u16)(r | key->kasumi.KLi2[round_no]); l ^= ROL16(b,1); /* put the two halves back together */ return (((ulong32)l)<<16) + r; }
const char* appStrdupPool(const char* str) { int len = strlen(str); int hash = 0; for (int i = 0; i < len; i++) { char c = str[i]; #if 0 hash = (hash + c) ^ 0xABCDEF; #else hash = ROL16(hash, 5) - hash + ((c << 4) + c ^ 0x13F); // some crazy hash function #endif } hash &= (STRING_HASH_SIZE - 1); for (const CStringPoolEntry* s = StringHashTable[hash]; s; s = s->HashNext) { if (s->Length == len && !strcmp(str, s->Str)) // found a string return s->Str; } if (!StringPool) StringPool = new CMemoryChain(); // allocate new string from pool CStringPoolEntry* n = (CStringPoolEntry*)StringPool->Alloc(sizeof(CStringPoolEntry) + len); // note: null byte is taken into account in CStringPoolEntry n->HashNext = StringHashTable[hash]; StringHashTable[hash] = n; n->Length = len; memcpy(n->Str, str, len+1); return n->Str; }
const char* appStrdupPool(const char* str) { int len = strlen(str); int hash = 0; for (int i = 0; i < len; i++) { char c = str[i]; #if 0 hash = (hash + c) ^ 0xABCDEF; #else hash = ROL16(hash, 5) - hash + ((c << 4) + c ^ 0x13F); // some crazy hash function #endif } hash &= (STRING_HASH_SIZE - 1); #if 0 if (true) { // Compare "Length" and first 2 characters with single operation uint32 cmp = len | (str[0] << 16) | (str[1] << 24); for (const CStringPoolEntry* s = StringHashTable[hash]; s; s = s->HashNext) { uint32 cmp2 = *(uint32*)&s->Length; if (cmp == cmp2) { if (!memcmp(str, s->Str, len)) { // found a string return s->Str; } } } } else #endif { for (const CStringPoolEntry* s = StringHashTable[hash]; s; s = s->HashNext) { if (s->Length == len && !memcmp(str, s->Str, len)) { // found a string return s->Str; } } } if (!StringPool) StringPool = new CMemoryChain(); // allocate new string from pool CStringPoolEntry* n = (CStringPoolEntry*)StringPool->Alloc(sizeof(CStringPoolEntry) + len); // note: null byte is taken into account in CStringPoolEntry n->HashNext = StringHashTable[hash]; StringHashTable[hash] = n; n->Length = len; memcpy(n->Str, str, len+1); return n->Str; }
int kasumi_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey) { static const u16 C[8] = { 0x0123,0x4567,0x89AB,0xCDEF, 0xFEDC,0xBA98,0x7654,0x3210 }; u16 ukey[8], Kprime[8]; int n; LTC_ARGCHK(key != NULL); LTC_ARGCHK(skey != NULL); if (keylen != 16) { return CRYPT_INVALID_KEYSIZE; } if (num_rounds != 0 && num_rounds != 8) { return CRYPT_INVALID_ROUNDS; } /* Start by ensuring the subkeys are endian correct on a 16-bit basis */ for (n = 0; n < 8; n++ ) { ukey[n] = (((u16)key[2*n]) << 8) | key[2*n+1]; } /* Now build the K'[] keys */ for (n = 0; n < 8; n++) { Kprime[n] = ukey[n] ^ C[n]; } /* Finally construct the various sub keys */ for(n = 0; n < 8; n++) { skey->kasumi.KLi1[n] = ROL16(ukey[n],1); skey->kasumi.KLi2[n] = Kprime[(n+2)&0x7]; skey->kasumi.KOi1[n] = ROL16(ukey[(n+1)&0x7],5); skey->kasumi.KOi2[n] = ROL16(ukey[(n+5)&0x7],8); skey->kasumi.KOi3[n] = ROL16(ukey[(n+6)&0x7],13); skey->kasumi.KIi1[n] = Kprime[(n+4)&0x7]; skey->kasumi.KIi2[n] = Kprime[(n+3)&0x7]; skey->kasumi.KIi3[n] = Kprime[(n+7)&0x7]; } return CRYPT_OK; }
static int GetHashForFileName(const char* FileName) { const char* s1 = strrchr(FileName, '/'); // assume path delimiters are normalized s1 = (s1 != NULL) ? s1 + 1 : FileName; const char* s2 = strrchr(s1, '.'); int len = (s2 != NULL) ? s2 - s1 : strlen(s1); int hash = 0; for (int i = 0; i < len; i++) { char c = s1[i]; if (c >= 'A' && c <= 'Z') c += 'a' - 'A'; // lowercase a character hash = ROL16(hash, 5) - hash + ((c << 4) + c ^ 0x13F); // some crazy hash function } // hash += (len << 6) - len; hash &= GAME_FILE_HASH_MASK; #ifdef DEBUG_HASH_NAME if (strstr(FileName, DEBUG_HASH_NAME)) printf("hash[%s] (%s,%d) -> %X\n", FileName, s1, len, hash); #endif return hash; }