Example #1
0
int performDES(const char inputData[8], char outputData[8], const char key[8], const int decrypt)
{
	/* Function temporary variables: */
	int successValue = 1; // Bias toward success
	char tempData[8]; // Temporary data buffer for in-between encryption steps
	char roundKeys[16][6]; // To store the round keys

	// First check the prity bit of the key to ensure that it was (1:256 chance) created and shared correctly (not super necessary, but might as well use the bits if we have them)
	if (checkKeyParityBits(key) == 0) // 0 means incorrect  parity bits
	{
		successValue = 0; // 0 means unsuccessful algorithm. Still attempt encryption with non-parity key
	}

	keyCopy(inputData, tempData); // Initialize the tempData with the initial data
	
	// Perform the initial permutation
	generateInitialPermutation(tempData, outputData);
	keyCopy(outputData, tempData);

	// Create the keys for each round & run the rounds
	generatePerRoundKeys(key, roundKeys, decrypt);
	performEcryptionRounds(tempData, outputData, roundKeys);
	keyCopy(outputData, tempData);

	// Swap the halves of the data
	swapHalves(tempData, outputData);
	keyCopy(outputData, tempData);

	// Perform the final reverse permutation
	generateFinalPermutation(tempData, outputData);

	// Return the result
	return successValue;
}
Example #2
0
void performEcryptionRounds(const char inputData[8], char outputData[8], const char roundKeys[16][6])
{
	char tempData[8]; // Temporary data buffer for in-between encryption steps

	// Initialize tempData with inputData before key rounds
	keyCopy(inputData, tempData);

	// Run the 16 rounds
	for (int i = 0; i < 16; ++i)
	{
		generateNewRound(tempData, outputData, roundKeys[i]); // Run the round
		keyCopy(outputData, tempData); // Save the result for the next round
	}
}
Example #3
0
void AccountManager::setAuthURL(const QUrl& authURL) {
    if (_authURL != authURL) {
        _authURL = authURL;
        
        qDebug() << "URL for node authentication has been changed to" << qPrintable(_authURL.toString());
        qDebug() << "Re-setting authentication flow.";
        
        // check if there are existing access tokens to load from settings
        QSettings settings;
        settings.beginGroup(ACCOUNTS_GROUP);
        
        foreach(const QString& key, settings.allKeys()) {
            // take a key copy to perform the double slash replacement
            QString keyCopy(key);
            QUrl keyURL(keyCopy.replace("slashslash", "//"));
            
            if (keyURL == _authURL) {
                // pull out the stored access token and store it in memory
                _accountInfo = settings.value(key).value<DataServerAccountInfo>();
                qDebug() << "Found a data-server access token for" << qPrintable(keyURL.toString());
                
                emit accessTokenChanged();
            }
        }
        
        // tell listeners that the auth endpoint has changed
        emit authEndpointChanged();
    }
static void test_keyCopy_newKey (const size_t storagePlugin, const char * tmpFile)
{
	Key * parentKey = keyNew (TEST_ROOT_KEY, KEY_VALUE, tmpFile, KEY_END);
	open_storage_plugin (storagePlugin);
	Plugin * plugin = plugins[storagePlugin];

	KeySet * ks = metaTestKeySet ();
	succeed_if (plugin->kdbSet (plugin, ks, parentKey) == 1, "kdbSet was not successful");
	succeed_if (plugin->kdbGet (plugin, ks, parentKey) == 1, "kdbGet was not successful");

	Key * found = ksLookupByName (ks, "user/tests/storage/b", 0);
	succeed_if (found, "did not find key");

	Key * copy = keyNew (0, KEY_END);
	succeed_if (keyCopy (copy, found) != -1, "keyCopy failed");

	compare_key (found, copy);

	// check that keyCopy has not changed KeySet
	KeySet * expected = metaTestKeySet ();
	compare_keyset (ks, expected);

	// check that KeySet is intact after deleting Key copy
	keyDel (copy);
	compare_keyset (ks, expected);

	ksDel (expected);
	keyDel (parentKey);
	ksDel (ks);
	closeStoragePlugin (storagePlugin);
}
Example #5
0
/**
 * Return a duplicate of a key.
 *
 * Memory will be allocated as needed for dynamic properties.
 *
 * The new key will not be member of any KeySet and
 * will start with a new reference counter at 0. A
 * subsequent keyDel() will delete the key.
 *
 * @code
int f (const Key * source)
{
	Key * dup = keyDup (source);
	// work with duplicate
	keyDel (dup);
	// everything related to dup is freed
	// and source is unchanged
}
 * @endcode
 *
 * Like for a new key after keyNew() a subsequent ksAppend()
 * makes a KeySet to take care of the lifecycle of the key.
 *
 * @code
int g (const Key * source, KeySet * ks)
{
	Key * dup = keyDup (source);
	// work with duplicate
	ksAppendKey (ks, dup);
	// ksDel(ks) will also free the duplicate
	// source remains unchanged.
}
 * @endcode
 *
 * Duplication of keys should be preferred to keyNew(),
 * because data like owner can be filled with a copy
 * of the key instead of asking the environment.
 * It can also be optimized in the checks, because the keyname
 * is known to be valid.
 *
 * @param source has to be an initialized source Key
 * @retval 0 failure or on NULL pointer
 * @return a fully copy of source on success
 * @see ksAppend(), keyDel(), keyNew()
 * @ingroup key
 */
Key * keyDup (const Key * source)
{
	Key * dest = 0;

	if (!source) return 0;

	dest = elektraKeyMalloc ();
	if (!dest) return 0;

	/* Copy the struct data */
	*dest = *source;

	/* get rid of properties bound to old key */
	dest->ksReference = 0;
	dest->flags = KEY_FLAG_SYNC;

	/* prepare to set dynamic properties */
	dest->key = dest->data.v = dest->meta = 0;

	/* copy dynamic properties */
	if (keyCopy (dest, source) == -1)
	{
		keyDel (dest);
		return 0;
	}

	return dest;
}
Example #6
0
int setElementHash(Key *key, Hash *hash, int count)
{
  int index;
  Node *h,*p=NULL;

  index = hashIndex(key, hash->size);

  if( hash->table[index]==NULL )
    {
      if( (h = (Node*) mymalloc(sizeof(Node)))==NULL )
	{ fprintf(stderr, "Cannot allocate memory for new entry in hash table"); exit(2); }

      hash->table[index] = h;
      hash->total++;
      keyCopy(&(h->key),key);
      h->count = count;
      h->next = NULL;
      return count;
    }    

  h = hash->table[index];
  //assert(h);
  while( h!=NULL && !equalKey(&h->key, key))
    {
      p = h;
      h = h->next;
    }
  if(h == NULL)
    {
      if( (h = (Node*) mymalloc(sizeof(Node)))==NULL )
	{ fprintf(stderr, "Cannot allocate memory for new entry in hash table"); exit(2); }
      hash->total++;
      //assert(p);
      p->next = h;
      keyCopy(&(h->key),key);
      h->count = count;
      h->next = NULL;
      return count;
    }

  h->count = count;
  return h->count;
}
void QalfConfig::setProperty(QString &key, QString &value) {
    QString keyCopy(key) ;
    QString valueCopy(value) ;
    qDebug() << "asking for lock write" ;
    lock.lockForWrite() ;
    qDebug() << "lock got" ;
    properties[keyCopy] = valueCopy ;
    qDebug() << "releasing lock" ;
    lock.unlock() ;
}
Example #8
0
int perform3DES(const char inputData[8], char outputData[8], const char key[8], const int decrypt)
{
	int result1 = 1; // Bias toward success
	int result2 = 1;
	int result3 = 1;

	// For storing the result of meta-encryption steps
	char tempData[8];
	keyCopy(inputData, tempData);

	result1 = performDES(tempData, outputData, key, decrypt);
	keyCopy(outputData, tempData);

	result2 = performDES(tempData, outputData, key, !decrypt);
	keyCopy(outputData, tempData);

	result3 = performDES(tempData, outputData, key, decrypt);

	return (result1 && result2 && result3);
}
static void test_keyCopy_clearOverwriteKey (const size_t storagePlugin, const char * tmpFile)
{
	Key * parentKey = keyNew (TEST_ROOT_KEY, KEY_VALUE, tmpFile, KEY_END);
	open_storage_plugin (storagePlugin);
	Plugin * plugin = plugins[storagePlugin];

	KeySet * ks = metaTestKeySet ();
	succeed_if (plugin->kdbSet (plugin, ks, parentKey) == 1, "kdbSet was not successful");
	succeed_if (plugin->kdbGet (plugin, ks, parentKey) == 1, "kdbGet was not successful");

	Key * toCopy = keyNew ("user/tests/storage/newnewkey", KEY_VALUE, "new key", KEY_END);

	Key * found = ksLookupByName (ks, "user/tests/storage/b", KDB_O_POP);
	succeed_if (found, "did not find key");

	// currently, KDB_O_POP doest not clear the readonly name flag
	if (test_bit (found->flags, KEY_FLAG_RO_NAME))
	{
		clear_bit (found->flags, KEY_FLAG_RO_NAME);
	}

	// overwrite Key
	succeed_if (keyCopy (found, 0) == 0, "keyCopy: clear destination failed");
	succeed_if (keyCopy (found, toCopy) == 1, "keyCopy failed");
	compare_key (found, toCopy);
	keyDel (toCopy);

	// put key back into place
	ksAppendKey (ks, found);

	// write KeySet back to storage
	succeed_if (plugin->kdbSet (plugin, ks, parentKey) == 1, "kdbSet was not successful");
	succeed_if (plugin->kdbGet (plugin, ks, parentKey) == 1, "kdbGet was not successful");

	found = ksLookupByName (ks, "user/tests/storage/newnewkey", 0);
	succeed_if (found, "did not find key");

	keyDel (parentKey);
	ksDel (ks);
	closeStoragePlugin (storagePlugin);
}
Example #10
0
void j (Key *k)
{
	size_t size = keyGetValueSize (k);
	char *value = malloc (size);
	int bstring = keyIsString (k);

	// receive key g_c
	memcpy (value, keyValue(k), size);
	keyCopy (k, g_c);
	if (bstring) keySetString (k, value);
	else keySetBinary (k, value, size);
	free (value);
	// the caller will see the changed key k
	// with the metadata from g_c
}