Example #1
0
bool dbWatchDog::open(char const* name, int flags)
{
    key_t key = IPC_PRIVATE;
    if (name != NULL) { 
        int fd;
        char* path = (char*)name;
        if (strchr(name, '/') == NULL) { 
            path = new char[strlen(name)+strlen(keyFileDir)+1];
            sprintf(path, "%s%s", keyFileDir, name);
        }
        fd = ::open(path, O_WRONLY|O_CREAT, ACCESS_PERMISSION_MASK);
        if (fd < 0) {
            if (path != name) { 
                delete[] path;
            }
            PRINT_ERROR("open");
            return -1;
        }
        ::close(fd);
        key = getKeyFromFile(path);
        if (path != name) { 
            delete[] path;
        }
        if (key < 0) {
            PRINT_ERROR("getKeyFromFile");
            return -1;
        }
    }
    return (id = semget(key, 1, flags)) >= 0;
}
Example #2
0
bool dbSharedMemory::open(char const* name, size_t size)
{
    char* fileName = (char*)name;
#ifndef VXWORKS    
    //Why this portion is commented is we don't have file system now
    //It is used to generate keys. Once our file system is up then we can use this
    if (strchr(name, '/') == NULL) { 
        fileName = new char[strlen(name)+strlen(keyFileDir)+1];
        sprintf(fileName, "%s%s", keyFileDir, name);
    }
    int fd = ::open(fileName, O_RDWR|O_CREAT, ACCESS_PERMISSION_MASK);
    if (fd < 0) { 
        if (fileName != name) { 
            delete[] fileName;
        }
        return false;
    } 
    ::close(fd);
#endif // ndef VXWORKS
    int key = getKeyFromFile(fileName);
    if (fileName != name) { 
        delete[] fileName;
    }
    if (key < 0) { 
        return false;
    }
    shm = shmget(key, DOALIGN(size, 4096), IPC_CREAT|ACCESS_PERMISSION_MASK);
    if (shm < 0) { 
        return false;
    }
    ptr = (char*)shmat(shm, NULL, 0);
    return (ptr != (char*)-1);
}
Example #3
0
bool dbSharedMemory::open(char const* name, size_t size)
{
    char* fileName = (char*)name;
    if (strchr(name, '/') == NULL) { 
        fileName = new char[strlen(name)+strlen(keyFileDir)+1];
        sprintf(fileName, "%s%s", keyFileDir, name);
    }
    int fd = ::open(fileName, O_RDWR|O_CREAT, 0777);
    if (fd < 0) { 
        if (fileName != name) { 
            delete[] fileName;
        }
        return false;
    } 
    ::close(fd);
    int key = getKeyFromFile(fileName);
    if (fileName != name) { 
        delete[] fileName;
    }
    if (key < 0) { 
        return false;
    }
    shm = shmget(key, DOALIGN(size, 4096), IPC_CREAT|0777);
    if (shm < 0) { 
        return false;
    }
    ptr = (char*)shmat(shm, NULL, 0);
    return (ptr != (char*)-1);
}
Example #4
0
int sem_init(int& sem, char const* name, unsigned init_value)
{
    key_t key = IPC_PRIVATE;
    int semid;
    struct sembuf sops[3];
    sops[0].sem_num = 1;
    sops[0].sem_op  = 0; /* check if semaphore was already initialized */
    sops[0].sem_flg = IPC_NOWAIT;
    sops[1].sem_num = 1;
    sops[1].sem_op  = 1; /* mark semaphore as initialized */
    sops[1].sem_flg = 0;
    sops[2].sem_num = 0;
    sops[2].sem_op  = init_value;
    sops[2].sem_flg = 0;
    if (name != NULL) { 
        int fd;
        char* path = (char*)name;
        if (strchr(name, '/') == NULL) { 
            path = new char[strlen(name)+strlen(keyFileDir)+1];
            sprintf(path, "%s%s", keyFileDir, name);
        }
        fd = open(path, O_WRONLY|O_CREAT, ACCESS_PERMISSION_MASK);
        if (fd < 0) {
            if (path != name) { 
                delete[] path;
            }
            PRINT_ERROR("open");
            return -1;
        }
        close(fd);
        key = getKeyFromFile(path);
        if (path != name) { 
            delete[] path;
        }
        if (key < 0) {
            PRINT_ERROR("getKeyFromFile");
            return -1;
        }
    }
    semid = semget(key, 2, IPC_CREAT|ACCESS_PERMISSION_MASK);
    if (semid < 0) { 
        PRINT_ERROR("semget");
        return -1;
    }
    if (semop(semid, sops, itemsof(sops)) != 0 && errno != EAGAIN) { 
        PRINT_ERROR("semop");
        return -1;
    }
    sem = semid;
    return 0;
}
Example #5
0
static void
setKey()
{
	if(randomKeyLen != -1)
		getRandomKey();
	else if(keyfile != NULL)
		getKeyFromFile(keyfile);
	else if(keyprog != NULL)
		gcryGetKeyFromProg(keyprog, &cry_key, &cry_keylen);
	if(cry_key == NULL) {
		fprintf(stderr, "ERROR: key must be set via some method\n");
		exit(1);
	}
}
Example #6
0
dbInitializationMutex::initializationStatus 
dbInitializationMutex::initialize(char const* name)
{
    struct sembuf sops[5];
    char* path = (char*)name;
    if (strchr(name, '/') == NULL) { 
        path = new char[strlen(name)+strlen(keyFileDir)+1];
        sprintf(path, "%s%s", keyFileDir, name);
    }
    int fd = open(path, O_WRONLY|O_CREAT, ACCESS_PERMISSION_MASK);
    if (fd < 0) {
        if (path != name) { 
            delete[] path;
        }
        PRINT_ERROR("open");
        return InitializationError;
    }
    ::close(fd);
    int key = getKeyFromFile(path);
    if (path != name) { 
        delete[] path;
    }
    if (key < 0) {
        PRINT_ERROR("getKeyFromFile");
        return InitializationError;
    }
    while (true) { 
        semid = semget(key, 3, IPC_CREAT|ACCESS_PERMISSION_MASK);
        if (semid < 0) { 
            PRINT_ERROR("semget");
            return InitializationError;
        }
        // Semaphore 0 - number of active processes
        // Semaphore 1 - intialization in progress (1 while initialization, 0 after it)
        // Semaphore 2 - semaphore was destroyed
        
        sops[0].sem_num = 0;
        sops[0].sem_op  = 0; /* check if semaphore was already initialized */
        sops[0].sem_flg = IPC_NOWAIT;
        sops[1].sem_num = 0;
        sops[1].sem_op  = 1; /* increment number of active processes */
        sops[1].sem_flg = SEM_UNDO;
        sops[2].sem_num = 1;
        sops[2].sem_op  = 1; /* enter critical section (initialization in process) */
        sops[2].sem_flg = SEM_UNDO;
        sops[3].sem_num = 2;
        sops[3].sem_op  = 0; /* check if semaphore was destroyed */
        sops[3].sem_flg = IPC_NOWAIT;
        if (semop(semid, sops, 4) < 0) { 
            if (errno == EAGAIN) { 
                sops[0].sem_num = 0;
                sops[0].sem_op  = -1; /* check if semaphore was already initialized */
                sops[0].sem_flg = SEM_UNDO|IPC_NOWAIT;
                sops[1].sem_num = 1;
                sops[1].sem_op  = 0; /* wait until inialization completed */
                sops[1].sem_flg = 0;
                sops[2].sem_num = 1;
                sops[2].sem_op  = 1; /* enter critical section */
                sops[2].sem_flg = SEM_UNDO;
                sops[3].sem_num = 0;
                sops[3].sem_op  = 2; /* increment number of active processes */
                sops[3].sem_flg = SEM_UNDO;
                sops[4].sem_num = 2;
                sops[4].sem_op  = 0; /* check if semaphore was destroyed */
                sops[4].sem_flg = IPC_NOWAIT;
                if (semop(semid, sops, 5) == 0) { 
                    return AlreadyInitialized;
                }
                if (errno == EAGAIN) { 
                    sleep(1);
                    continue;
                }
            } 
            if (errno == EIDRM) {
                continue;
            }
            PRINT_ERROR("semop");
            return InitializationError;
        } else { 
            return NotYetInitialized;
        }
    }
}
Example #7
0
void executeCipher(int op, int mode, int cript, int rounds, char* filepath)
{
	//getTransKeyFromFile();
	byte *iv = getIVFromFile();

	//printf("%d\n", iv);
	byte *key = getKeyFromFile();

	//printf("%d\n", key);

	int tam = strlen(filepath);
	char* addString;
	if (op == 1)
		addString = "-cripto";
	if (op == 2)
		addString = "-decripto";

	int addStringTam = strlen(addString);
	char* destFile = (char*)malloc((tam + addStringTam) * sizeof(char));
	char *codif = (char*)malloc(5 * sizeof(char));
	strcpy(codif, filepath + (tam - 4));
	//printf("%s\n", codif);
	strncpy(destFile, filepath, tam - 4);
	destFile[tam - 4] = '\0';
	//printf("%s\n",destFile);
	destFile = concat(destFile, addString);
	destFile = concat(destFile, codif);
	printf("\n");
	printf("%s", "Arquivo gerado: ");
	printf("%s\n\n", destFile);
	//CRIPTOGRAFAR
	if (op == 1)
	{

		//ECB
		if (mode == 1)
		{
			//AES APENAS COM ADDROUNDKEY
			if (cript == 1)

				process(filepath, destFile, key, rounds, NULL, ECB, encryptAddRoundKey);
			//AES CONVENCIONAL
			if (cript == 2)
				process(filepath, destFile, key, rounds, NULL, ECB, encrypt);
			//process(filepath, destFile, key, rounds, NULL, ECB,encrypt);
			//AES MODIFICADO
			if (cript == 3)
				process(filepath, destFile, key, rounds, NULL, ECB, encryptAlternative);

		}
		//CBC
		if (mode == 2)
		{
			//AES APENAS COM ADDROUNDKEY
			if (cript == 1)
				process(filepath, destFile, key, rounds, iv, CBC, encryptAddRoundKey);
			//AES CONVENCIONAL
			if (cript == 2)
				process(filepath, destFile, key, rounds, iv, CBC, encrypt);
			//AES MODIFICADO
			if (cript == 3)
				process(filepath, destFile, key, rounds, iv, CBC, encryptAlternative);
		}

		printf("Distância de hamming entre %s e %s: %f\n", filepath, destFile, getImageHammingDistance(filepath, destFile));
	}
	//DECRIPTOGRAFAR
	if (op == 2)
	{
		//ECB
		if (mode == 1)
		{

			if (cript == 1)
				process(filepath, destFile, key, rounds, NULL, ECB, decryptAddRoundKey);
			if (cript == 2)
				process(filepath, destFile, key, rounds, NULL, ECB, decrypt);
			if (cript == 3)
				process(filepath, destFile, key, rounds, NULL, ECB, decryptAlternative);

		}
		//CBC
		if (mode == 2)
		{
			if (cript == 1)
				process(filepath, destFile, key, rounds, iv, CBC, decryptAddRoundKey);
			if (cript == 2)
				process(filepath, destFile, key, rounds, iv, CBC, decrypt);
			if (cript == 3)
				process(filepath, destFile, key, rounds, iv, CBC, decryptAlternative);

		}
	}
	free(iv);
	free(key);
	//	free(addString);
	free(destFile);
	free(codif);

}
Example #8
0
dbInitializationMutex::initializationStatus 
dbInitializationMutex::initialize(char const* name)
{
    struct sembuf sops[4];
    char* path = (char*)name;
    if (strchr(name, '/') == NULL) { 
        path = new char[strlen(name)+strlen(keyFileDir)+1];
        sprintf(path, "%s%s", keyFileDir, name);
    }
    int fd = open(path, O_WRONLY|O_CREAT, 0777);
    if (fd < 0) {
        OsSysLog::add(FAC_DB, PRI_ERR, "Error attempting to open '%s' for writing.\n", path);
        if (path != name) { 
            delete[] path;
        }
        PRINT_ERROR("open");
        return InitializationError;
    }
    ::close(fd);
    int key = getKeyFromFile(path);
    OsSysLog::add(FAC_DB, PRI_DEBUG,
                  "dbInitializationMutex::initialize path = '%s', key = 0x%x",
                  path, key);
    if (path != name) { 
        delete[] path;
    }
    if (key < 0) {
        PRINT_ERROR("getKeyFromFile");
        return InitializationError;
    }
    while (true) { 
       semid = semget(key, 3, IPC_CREAT|0777);
       OsSysLog::add(FAC_DB, PRI_DEBUG,
                     "dbInitializationMutex::initialize semget(0x%x, 3, IPC_CREAT|0777)=%d",
                     key, semid);
        if (semid < 0) { 
            PRINT_ERROR("semget");
            OsSysLog::add(FAC_DB, PRI_CRIT,
                          "sem_init semget(3) failed - error: %s, key = 0x%x",
                          strerror(errno),
                          key);
            return InitializationError;
        }
        // Semaphore 0 - number of active processes
        // Semaphore 1 - intialization in progress (1 while initialization, 0 after it)
        // Semaphore 2 - semaphore was destroyed
        
        sops[0].sem_num = 0;
        sops[0].sem_op  = 0; /* check if semaphore was already initialized */
        sops[0].sem_flg = IPC_NOWAIT;
        sops[1].sem_num = 0;
        sops[1].sem_op  = 1; /* increment number of active processes */
        sops[1].sem_flg = SEM_UNDO;
        sops[2].sem_num = 1;
        sops[2].sem_op  = 1; /* initialization in process */
        sops[2].sem_flg = SEM_UNDO;
        sops[3].sem_num = 2;
        sops[3].sem_op  = 0; /* check if semaphore was destroyed */
        sops[3].sem_flg = IPC_NOWAIT;
        if (semop(semid, sops, 4) < 0) { 
            if (errno == EAGAIN) { 
                sops[0].sem_num = 0;
                sops[0].sem_op  = -1; /* check if semaphore was already initialized */
                sops[0].sem_flg = SEM_UNDO|IPC_NOWAIT;
                sops[1].sem_num = 1;
                sops[1].sem_op  = 0; /* wait until initialization completed */
                sops[1].sem_flg = 0;
                sops[2].sem_num = 0;
                sops[2].sem_op  = 2; /* increment number of active processes */
                sops[2].sem_flg = SEM_UNDO;
                sops[3].sem_num = 2;
                sops[3].sem_op  = 0; /* check if semaphore was destroyed */
                sops[3].sem_flg = IPC_NOWAIT;
                if (semop(semid, sops, 4) == 0) { 
                    return AlreadyInitialized;
                }
                if (errno == EAGAIN) { 
                    sleep(1);
                    continue;
                }
            } 
            if (errno == EIDRM) {
                continue;
            }
            PRINT_ERROR("semop");
            return InitializationError;
        } else { 
            return NotYetInitialized;
        }
    }
}
Example #9
0
int sem_init(int& sem, char const* name, unsigned init_value)
{
    key_t key = IPC_PRIVATE;
    int semid;
    struct sembuf sops[3];
    sops[0].sem_num = 1;
    sops[0].sem_op  = 0; /* check if semaphore was already initialized */
    sops[0].sem_flg = IPC_NOWAIT;
    sops[1].sem_num = 1;
    sops[1].sem_op  = 1; /* mark semaphore as initialized */
    sops[1].sem_flg = 0;
    sops[2].sem_num = 0;
    sops[2].sem_op  = init_value;
    sops[2].sem_flg = 0;
    if (name != NULL) { 
        int fd;
        char* path = (char*)name;
        if (strchr(name, '/') == NULL) { 
            path = new char[strlen(name)+strlen(keyFileDir)+1];
            sprintf(path, "%s%s", keyFileDir, name);
        }
        fd = open(path, O_WRONLY|O_CREAT, 0777);
        if (fd < 0) {
            OsSysLog::add(FAC_DB, PRI_CRIT,
                          "Error attempting to open '%s' for writing.",
                          path);
            if (path != name) { 
                delete[] path;
            }
            PRINT_ERROR("open");
            return -1;
        }
        close(fd);
        key = getKeyFromFile(path);
        OsSysLog::add(FAC_DB, PRI_DEBUG,
                      "sem_init path = '%s', key = 0x%x",
                      path, key);
        if (path != name) { 
            delete[] path;
        }
        if (key < 0) {
            PRINT_ERROR("getKeyFromFile");
            return -1;
        }
    }
    OsSysLog::add(FAC_DB, PRI_DEBUG,
                  "sem_init semget(0x%x, 2, IPC_CREAT|0777)",
                  key);
    semid = semget(key, 2, IPC_CREAT|0777);
    if (semid < 0) { 
        PRINT_ERROR("semget");
        OsSysLog::add(FAC_DB, PRI_CRIT,
                      "sem_init semget failed - error: %s, key = 0x%x",
                      strerror(errno),
                      key);
        return -1;
    }
    if (semop(semid, sops, itemsof(sops)) != 0 && errno != EAGAIN) { 
        PRINT_ERROR("semop");
        return -1;
    }
    sem = semid;
    return 0;
}