Example #1
0
static void* consumer_operation()
{
	set_signal_mask();
	while(status_on)
	{	
	 	if(pthread_mutex_lock(&buffer_lock_mtx)!=0)
			print_log(WARNING,"\nthere is error in lock cons");
		
		while(is_buffer_empty())
		{	
			if(pthread_cond_wait(&buffer_not_empty_cond,&buffer_lock_mtx)!=0)
				print_log(WARNING,"\nthere is error in wait cons");
		}	

		int peer_sfd = buffer_get();

		if(peer_sfd < 0) 
		{
			print_log(WARNING,"\nRECIEVED FAILED ACCEPT SOCKET");
			pthread_mutex_unlock(&buffer_lock_mtx)!=0;
			continue;
		}

		if(pthread_cond_broadcast(&buffer_not_full_cond)!=0)
			print_log(WARNING,"\nthere is error in broadcast cons");
		if(pthread_mutex_unlock(&buffer_lock_mtx)!=0)
			print_log(WARNING,"\nthere is error in unlock cons");

		manage_single_request(peer_sfd);
		close(peer_sfd);
	}
	pthread_exit(pthread_self);
}
Example #2
0
 std::string Server::message() {
   std::string first_message = "";
   if (!is_buffer_empty()) {
     first_message = buffer.front();
     buffer.pop();
   }
   return first_message;
 }
Example #3
0
static int read_data(char * out, buffer_stream_t *bs, int size)
{
	if(bs->bInited==0)
		return -1;//read failed
	int ret=is_buffer_empty(bs);
	if(ret==1)
	{
		//printf("=====buffer empty \n");
		return 0;//buffer empty
	}
	int len= MIN(bs->buf_level,size);
	if(bs->wr_ptr>bs->rd_ptr)
	{
		memcpy(out,bs->rd_ptr,len);
		bs->rd_ptr+=len;
		bs->buf_level-=len;
		if(bs->rd_ptr==(bs->data+bs->buf_length))
		     bs->rd_ptr=bs->data;
		//printf("=====read ok: condition 1 read :%d byte \n",len);
		return len;
	}
	else if(len<(bs->data+bs->buf_length-bs->rd_ptr))
	{
		memcpy(out,bs->rd_ptr,len);
		bs->rd_ptr+=len;
		bs->buf_level-=len;
		if(bs->rd_ptr==(bs->data+bs->buf_length))
		     bs->rd_ptr=bs->data;
		//printf("=====read ok: condition 2 read :%d byte \n",len);
		return len;

	}
	else
	{
		int tail_len=(bs->data+bs->buf_length-bs->rd_ptr);
		memcpy(out,bs->rd_ptr,tail_len);
		memcpy(out+tail_len,bs->data,len-tail_len);
		bs->rd_ptr=bs->data+len-tail_len;
		bs->buf_level-=len;
		if(bs->rd_ptr==(bs->data+bs->buf_length))
		     bs->rd_ptr=bs->data;
		//printf("=====read ok: condition 3 read :%d byte \n",len);
		return len;
	}

}
/*
************************************************************************************************************************
*                                    Receive  a msg
*
* Description: This function is called to receive a msg
*
* Arguments  :q_b is the address of the queue buffer object
*                 -----
*                  msg is the address of a point, and it will be filled data lwithin this api.
*                  if you want to use the extension memcpy, make sure the msg address is 4 bytes aligned.
*                  -----	    
*                  wait_option: is  how the service behaves if the msg queue is full.
*							The wait options are
*							defined as follows:
*							RAW_NO_WAIT (0x00000000)
*							RAW_WAIT_FOREVER (0xFFFFFFFF)
*							timeout value (0x00000001
*							through
*							0xFFFFFFFE)
*                  receive_size:is the msg size received.
*				         
* Returns			
*						RAW_SUCCESS: raw os return success
*						RAW_BLOCK_DEL: if this queue is deleted.
*						RAW_BLOCK_TIMEOUT: queue is still full during waiting time when sending msg.
*						RAW_BLOCK_ABORT:queue is aborted during waiting time when sending msg.
*						RAW_STATE_UNKNOWN: possibly system error.
* Note(s)    	
*
*             
************************************************************************************************************************
*/
RAW_U16 raw_queue_buffer_receive(RAW_QUEUE_BUFFER *q_b, RAW_TICK_TYPE wait_option, RAW_VOID *msg, MSG_SIZE_TYPE *receive_size)
{
	RAW_U16 result;
	
	RAW_SR_ALLOC();


	#if (RAW_QUEUE_BUFFER_FUNCTION_CHECK > 0)

	if (raw_int_nesting) {
		
		return RAW_NOT_CALLED_BY_ISR;
		
	}

	if (q_b == 0) {
		
		return RAW_NULL_OBJECT;
	}

	
	if (msg == 0) {
		
		return RAW_NULL_POINTER;
	}
	
	#endif

	RAW_CRITICAL_ENTER();

	if (q_b->common_block_obj.object_type != RAW_QUEUE_BUFFER_OBJ_TYPE) {

		RAW_CRITICAL_EXIT();
		return RAW_ERROR_OBJECT_TYPE;
	}

  	if (!is_buffer_empty(q_b)) {
		
		*receive_size = buffer_to_msg(q_b, msg);
		
		RAW_CRITICAL_EXIT();

		return RAW_SUCCESS;   

  	}	
		

	if (wait_option == RAW_NO_WAIT) {   
	
		RAW_CRITICAL_EXIT();
		return RAW_NO_PEND_WAIT;
	} 

	SYSTEM_LOCK_PROCESS();

	raw_task_active->msg = msg;
	raw_pend_object((RAW_COMMON_BLOCK_OBJECT  *)q_b, raw_task_active, wait_option);
	
	RAW_CRITICAL_EXIT();

	raw_sched();

	result = block_state_post_process(raw_task_active, 0);
	
	/*if get the msg successful then take it*/
	if (result == RAW_SUCCESS) {

		*receive_size = raw_task_active->qb_msg_size;
	}
	
	return result;


}
Example #5
0
int32_t psa_asymmetric_encrypt_test(security_t caller)
{
    int                     num_checks = sizeof(check1)/sizeof(check1[0]);
    int32_t                 i, status;
    const uint8_t           *key_data;
    uint8_t                 *salt;
    size_t                  length;
    psa_key_policy_t        policy;

    /* Initialize the PSA crypto library*/
    status = val->crypto_function(VAL_CRYPTO_INIT);
    TEST_ASSERT_EQUAL(status, PSA_SUCCESS, TEST_CHECKPOINT_NUM(1));

    for (i = 0; i < num_checks; i++)
    {
        val->print(PRINT_TEST, "[Check %d] ", g_test_count++);
        val->print(PRINT_TEST, check1[i].test_desc, 0);

        /* Initialize a key policy structure to a default that forbids all
         * usage of the key
         */
        val->crypto_function(VAL_CRYPTO_KEY_POLICY_INIT, &policy);

        /* Setting up the watchdog timer for each check */
        status = val->wd_reprogram_timer(WD_CRYPTO_TIMEOUT);
        TEST_ASSERT_EQUAL(status, VAL_STATUS_SUCCESS, TEST_CHECKPOINT_NUM(2));

        /* Set the standard fields of a policy structure */
        val->crypto_function(VAL_CRYPTO_KEY_POLICY_SET_USAGE, &policy, check1[i].usage,
                                                                          check1[i].key_alg);

        memset(output, 0, sizeof(output));

        /* Set the key data based on key type */
        if (PSA_KEY_TYPE_IS_RSA(check1[i].key_type))
        {
            if (check1[i].key_type == PSA_KEY_TYPE_RSA_KEYPAIR)
            {
                if (check1[i].expected_bit_length == BYTES_TO_BITS(384))
                    key_data = rsa_384_keypair;
                else if (check1[i].expected_bit_length == BYTES_TO_BITS(256))
                    key_data = rsa_256_keypair;
                else if (check1[i].expected_bit_length == BYTES_TO_BITS(128))
                    key_data = rsa_128_keypair;
                else
                    return VAL_STATUS_INVALID;
            }
            else
            {
                if (check1[i].expected_bit_length == BYTES_TO_BITS(384))
                    key_data = rsa_384_keydata;
                else if (check1[i].expected_bit_length == BYTES_TO_BITS(256))
                    key_data = rsa_256_keydata;
                else if (check1[i].expected_bit_length == BYTES_TO_BITS(128))
                    key_data = rsa_128_keydata;
                else
                    return VAL_STATUS_INVALID;
            }
        }
        else if (PSA_KEY_TYPE_IS_ECC(check1[i].key_type))
        {
            if (PSA_KEY_TYPE_IS_ECC_KEYPAIR(check1[i].key_type))
                key_data = ec_keypair;
            else
                key_data = ec_keydata;
        }
        else
            key_data = check1[i].key_data;

        /* Allocate a key slot for a transient key */
        status = val->crypto_function(VAL_CRYPTO_ALLOCATE_KEY, &check1[i].key_handle);
        TEST_ASSERT_EQUAL(status, PSA_SUCCESS, TEST_CHECKPOINT_NUM(3));

        /* Set the usage policy on a key slot */
        status = val->crypto_function(VAL_CRYPTO_SET_KEY_POLICY, check1[i].key_handle,
                    &policy);
        TEST_ASSERT_EQUAL(status, PSA_SUCCESS, TEST_CHECKPOINT_NUM(4));

        /* Import the key data into the key slot */
        status = val->crypto_function(VAL_CRYPTO_IMPORT_KEY, check1[i].key_handle,
                    check1[i].key_type, key_data, check1[i].key_length);
        TEST_ASSERT_EQUAL(status, PSA_SUCCESS, TEST_CHECKPOINT_NUM(5));

        if (is_buffer_empty(check1[i].salt, check1[i].salt_length) == TRUE)
            salt = NULL;
        else
            salt = check1[i].salt;

        /* Encrypt a short message with a public key */
        status = val->crypto_function(VAL_CRYPTO_ASYMMTERIC_ENCRYPT, check1[i].key_handle,
                    check1[i].key_alg, check1[i].input, check1[i].input_length, salt,
                    check1[i].salt_length, output, check1[i].output_size, &length);
        TEST_ASSERT_EQUAL(status, check1[i].expected_status, TEST_CHECKPOINT_NUM(6));

        if (check1[i].expected_status != PSA_SUCCESS)
            continue;

        /* Check if the output length matches with the expected output length */
        TEST_ASSERT_EQUAL(length, check1[i].expected_output_length, TEST_CHECKPOINT_NUM(7));

        /* We test encryption by checking that encrypt-then-decrypt gives back
         * the original plaintext because of the non-optional random
         * part of encryption process which prevents using fixed vectors. */
        if ((check1[i].usage & PSA_KEY_USAGE_DECRYPT) == PSA_KEY_USAGE_DECRYPT)
        {
            status = val->crypto_function(VAL_CRYPTO_ASYMMTERIC_DECRYPT,
                        check1[i].key_handle, check1[i].key_alg, output, length, salt,
                        check1[i].salt_length, output, check1[i].output_size, &length);
            TEST_ASSERT_EQUAL(status, PSA_SUCCESS, TEST_CHECKPOINT_NUM(8));

            /* Check if the output length matches with the input length */
            TEST_ASSERT_EQUAL(length, check1[i].input_length, TEST_CHECKPOINT_NUM(9));

            /* Check if the output matches with the given input data */
            TEST_ASSERT_MEMCMP(output, check1[i].input, length, TEST_CHECKPOINT_NUM(10));
        }
    }

    return VAL_STATUS_SUCCESS;
}
Example #6
0
int32_t psa_asymmetric_encrypt_negative_test(security_t caller)
{
    int                     num_checks = sizeof(check2)/sizeof(check2[0]);
    int32_t                 i, status;
    uint8_t                 *salt;
    size_t                  length;
    psa_key_policy_t        policy;

    /* Initialize the PSA crypto library*/
    status = val->crypto_function(VAL_CRYPTO_INIT);
    TEST_ASSERT_EQUAL(status, PSA_SUCCESS, TEST_CHECKPOINT_NUM(1));

    for (i = 0; i < num_checks; i++)
    {
        val->print(PRINT_TEST, "[Check %d] Test psa_asymmetric_encrypt - Invalid key handle\n",
                                                                                 g_test_count++);

        /* Initialize a key policy structure to a default that forbids all
         * usage of the key
         */
        val->crypto_function(VAL_CRYPTO_KEY_POLICY_INIT, &policy);

        /* Setting up the watchdog timer for each check */
        status = val->wd_reprogram_timer(WD_CRYPTO_TIMEOUT);
        TEST_ASSERT_EQUAL(status, VAL_STATUS_SUCCESS, TEST_CHECKPOINT_NUM(2));

        /* Set the standard fields of a policy structure */
        val->crypto_function(VAL_CRYPTO_KEY_POLICY_SET_USAGE, &policy, check2[i].usage,
                                                                          check2[i].key_alg);

        val->print(PRINT_TEST, "[Check %d] Test psa_asymmetric_encrypt - Invalid key handle\n",
                                                                                 g_test_count++);
        /* Encrypt a short message with a public key */
        status = val->crypto_function(VAL_CRYPTO_ASYMMTERIC_ENCRYPT, check2[i].key_handle,
                    check2[i].key_alg, check2[i].input, check2[i].input_length, salt,
                    check2[i].salt_length, output, check2[i].output_size, &length);
        TEST_ASSERT_EQUAL(status, PSA_ERROR_INVALID_HANDLE, TEST_CHECKPOINT_NUM(3));

        val->print(PRINT_TEST, "[Check %d] Test psa_asymmetric_encrypt - Zero as key handle\n",
                                                                                 g_test_count++);
        /* Encrypt a short message with a public key */
        status = val->crypto_function(VAL_CRYPTO_ASYMMTERIC_ENCRYPT, 0,
                    check2[i].key_alg, check2[i].input, check2[i].input_length, salt,
                    check2[i].salt_length, output, check2[i].output_size, &length);
        TEST_ASSERT_EQUAL(status, PSA_ERROR_INVALID_HANDLE, TEST_CHECKPOINT_NUM(4));

        val->print(PRINT_TEST, "[Check %d] Test psa_asymmetric_encrypt - Empty key handle\n",
                                                                                 g_test_count++);
        /* Allocate a key slot for a transient key */
        status = val->crypto_function(VAL_CRYPTO_ALLOCATE_KEY, &check2[i].key_handle);
        TEST_ASSERT_EQUAL(status, PSA_SUCCESS, TEST_CHECKPOINT_NUM(5));

        /* Set the usage policy on a key slot */
        status = val->crypto_function(VAL_CRYPTO_SET_KEY_POLICY, check2[i].key_handle,
                    &policy);
        TEST_ASSERT_EQUAL(status, PSA_SUCCESS, TEST_CHECKPOINT_NUM(6));

        if (is_buffer_empty(check1[i].salt, check1[i].salt_length) == TRUE)
            salt = NULL;
        else
            salt = check1[i].salt;

        /* Encrypt a short message with a public key */
        status = val->crypto_function(VAL_CRYPTO_ASYMMTERIC_ENCRYPT, check2[i].key_handle,
                    check2[i].key_alg, check2[i].input, check2[i].input_length, salt,
                    check2[i].salt_length, output, check2[i].output_size, &length);
        TEST_ASSERT_EQUAL(status, PSA_ERROR_EMPTY_SLOT, TEST_CHECKPOINT_NUM(7));
    }

    return VAL_STATUS_SUCCESS;
}
Example #7
0
int32_t psa_aead_decrypt_test(security_t caller)
{
    int32_t          i, status;
    uint8_t          plaintext[BUFFER_SIZE];
    psa_key_policy_t policy;
    size_t           plaintext_length;
    int              num_checks = sizeof(check1)/sizeof(check1[0]);
    uint8_t          *nonce, *additional_data;

    /* Initialize the PSA crypto library*/
    status = val->crypto_function(VAL_CRYPTO_INIT);
    TEST_ASSERT_EQUAL(status, PSA_SUCCESS, TEST_CHECKPOINT_NUM(1));

    for (i = 0; i < num_checks; i++)
    {
        val->print(PRINT_TEST, "[Check %d] ", g_test_count++);
        val->print(PRINT_TEST, check1[i].test_desc, 0);

        /* Initialize a key policy structure to a default that forbids all
         * usage of the key
         */
        val->crypto_function(VAL_CRYPTO_KEY_POLICY_INIT, &policy);

        /* Setting up the watchdog timer for each check */
        status = val->wd_reprogram_timer(WD_CRYPTO_TIMEOUT);
        TEST_ASSERT_EQUAL(status, VAL_STATUS_SUCCESS, TEST_CHECKPOINT_NUM(2));

        /* Set the standard fields of a policy structure */
        val->crypto_function(VAL_CRYPTO_KEY_POLICY_SET_USAGE, &policy, check1[i].usage,
                                                                          check1[i].key_alg);

        memset(plaintext, 0, sizeof(plaintext));
        /* Allocate a key slot for a transient key */
        status = val->crypto_function(VAL_CRYPTO_ALLOCATE_KEY, &check1[i].key_handle);
        TEST_ASSERT_EQUAL(status, PSA_SUCCESS, TEST_CHECKPOINT_NUM(3));

        /* Set the usage policy on a key slot */
        status = val->crypto_function(VAL_CRYPTO_SET_KEY_POLICY, check1[i].key_handle,
                                      &policy);
        TEST_ASSERT_EQUAL(status, PSA_SUCCESS, TEST_CHECKPOINT_NUM(4));

        /* Import the key data into the key slot */
        status = val->crypto_function(VAL_CRYPTO_IMPORT_KEY, check1[i].key_handle,
                    check1[i].key_type, check1[i].key_data, check1[i].key_length);
        TEST_ASSERT_EQUAL(status, PSA_SUCCESS, TEST_CHECKPOINT_NUM(5));

        if (is_buffer_empty(check1[i].nonce, check1[i].nonce_length) == TRUE)
        {
            nonce = NULL;
            check1[i].nonce_length = 0;
        }
        else
            nonce = check1[i].nonce;

        if (is_buffer_empty(check1[i].additional_data, check1[i].additional_data_length) == TRUE)
        {
            additional_data = NULL;
            check1[i].additional_data_length = 0;
        }
        else
            additional_data = check1[i].additional_data;

        /* Process an authenticated decryption operation */
        status = val->crypto_function(VAL_CRYPTO_AEAD_DECRYPT, check1[i].key_handle,
                  check1[i].key_alg, nonce, check1[i].nonce_length, additional_data,
                  check1[i].additional_data_length, check1[i].ciphertext, check1[i].ciphertext_size,
                  plaintext, check1[i].plaintext_size, &plaintext_length);
        TEST_ASSERT_EQUAL(status, check1[i].expected_status, TEST_CHECKPOINT_NUM(6));

        if (check1[i].expected_status != PSA_SUCCESS)
            continue;

        /* Check if the length matches */
        TEST_ASSERT_EQUAL(plaintext_length, check1[i].expected_plaintext_length,
                          TEST_CHECKPOINT_NUM(7));

        /* Check if the data matches */
        TEST_ASSERT_MEMCMP(plaintext, check1[i].expected_plaintext, plaintext_length,
                           TEST_CHECKPOINT_NUM(8));
    }

    return VAL_STATUS_SUCCESS;
}
Example #8
0
int32_t psa_aead_decrypt_negative_test(security_t caller)
{
    int32_t          i, status;
    uint8_t          plaintext[BUFFER_SIZE];
    psa_key_policy_t policy;
    size_t           plaintext_length;
    int              num_checks = sizeof(check2)/sizeof(check2[0]);
    uint8_t          *nonce, *additional_data;

    /* Initialize the PSA crypto library*/
    status = val->crypto_function(VAL_CRYPTO_INIT);
    TEST_ASSERT_EQUAL(status, PSA_SUCCESS, TEST_CHECKPOINT_NUM(1));

    for (i = 0; i < num_checks; i++)
    {
        /* Initialize a key policy structure to a default that forbids all
         * usage of the key
         */
        val->crypto_function(VAL_CRYPTO_KEY_POLICY_INIT, &policy);

        /* Setting up the watchdog timer for each check */
        status = val->wd_reprogram_timer(WD_CRYPTO_TIMEOUT);
        TEST_ASSERT_EQUAL(status, VAL_STATUS_SUCCESS, TEST_CHECKPOINT_NUM(2));

        /* Set the standard fields of a policy structure */
        val->crypto_function(VAL_CRYPTO_KEY_POLICY_SET_USAGE, &policy, check2[i].usage,
                                                                          check2[i].key_alg);

        if (is_buffer_empty(check2[i].nonce, check2[i].nonce_length) == TRUE)
        {
            nonce = NULL;
            check2[i].nonce_length = 0;
        }
        else
            nonce = check2[i].nonce;

        if (is_buffer_empty(check2[i].additional_data, check2[i].additional_data_length) == TRUE)
        {
            additional_data = NULL;
            check2[i].additional_data_length = 0;
        }
        else
            additional_data = check2[i].additional_data;

        val->print(PRINT_TEST, "[Check %d] Test psa_aead_decrypt - invalid key handle\n",
                                                                             g_test_count++);
        /* Process an authenticated decryption operation */
        status = val->crypto_function(VAL_CRYPTO_AEAD_DECRYPT, check2[i].key_handle,
                  check2[i].key_alg, nonce, check2[i].nonce_length, additional_data,
                  check2[i].additional_data_length, check2[i].ciphertext, check2[i].ciphertext_size,
                  plaintext, check2[i].plaintext_size, &plaintext_length);
        TEST_ASSERT_EQUAL(status, PSA_ERROR_INVALID_HANDLE, TEST_CHECKPOINT_NUM(3));

        val->print(PRINT_TEST, "[Check %d] Test psa_aead_decrypt - zero as key handle\n",
                                                                             g_test_count++);
        /* Process an authenticated decryption operation */
        status = val->crypto_function(VAL_CRYPTO_AEAD_DECRYPT, 0,
                  check2[i].key_alg, nonce, check2[i].nonce_length, additional_data,
                  check2[i].additional_data_length, check2[i].ciphertext, check2[i].ciphertext_size,
                  plaintext, check2[i].plaintext_size, &plaintext_length);
        TEST_ASSERT_EQUAL(status, PSA_ERROR_INVALID_HANDLE, TEST_CHECKPOINT_NUM(4));

        val->print(PRINT_TEST, "[Check %d] Test psa_aead_decrypt - empty key handle\n",
                                                                             g_test_count++);
        /* Allocate a key slot for a transient key */
        status = val->crypto_function(VAL_CRYPTO_ALLOCATE_KEY, &check2[i].key_handle);
        TEST_ASSERT_EQUAL(status, PSA_SUCCESS, TEST_CHECKPOINT_NUM(5));

        /* Set the usage policy on a key slot */
        status = val->crypto_function(VAL_CRYPTO_SET_KEY_POLICY, check2[i].key_handle,
                        &policy);
        TEST_ASSERT_EQUAL(status, PSA_SUCCESS, TEST_CHECKPOINT_NUM(6));

        /* Process an authenticated decryption operation */
        status = val->crypto_function(VAL_CRYPTO_AEAD_DECRYPT, check2[i].key_handle,
                  check2[i].key_alg, nonce, check2[i].nonce_length, additional_data,
                  check2[i].additional_data_length, check2[i].ciphertext, check2[i].ciphertext_size,
                  plaintext, check2[i].plaintext_size, &plaintext_length);
        TEST_ASSERT_EQUAL(status, PSA_ERROR_EMPTY_SLOT, TEST_CHECKPOINT_NUM(7));
     }

     return VAL_STATUS_SUCCESS;
}