Beispiel #1
0
int tsdp_header_serialize(const tsdp_header_t *self, tsk_buffer_t *output)
{
    static char name;
    int ret = -1;
    if(!self || !output) {
        return -1;
    }

    /* Name */
    name = tsdp_header_get_nameex(self);
    tsk_buffer_append_2(output, "%c=", name);

    /* Value */
    if((ret = self->tostring(self, output))) {
        // Abort?
    }

    /* CRLF*/
    if(output->size>2) {
        if(*(TSK_BUFFER_TO_U8(output)+TSK_BUFFER_SIZE(output)-2) != '\r'
                && *(TSK_BUFFER_TO_U8(output)+TSK_BUFFER_SIZE(output)-1) != '\n') {
            ret = tsk_buffer_append(output, "\r\n", 2);
        }
    }
    else {
        ret = tsk_buffer_append(output, "\r\n", 2);
    }

    return ret;
}
Beispiel #2
0
/**@ingroup tsk_buffer_group
* Appends data to the buffer.
* @param self The buffer to append to. The buffer should be created using @ref tsk_buffer_create or @ref tsk_buffer_create_null.
* @param data The data to append to the buffer.
* @param size The size of the @a data to append.
* @retval Zero if succeed and non-zero error code otherwise.
* @sa @ref tsk_buffer_append_2.
*
* @code
* tsk_buffer_t* buffer = tsk_buffer_create_null();
* tsk_buffer_append(buffer, "doubango", tsk_strlen("doubango"));
* printf(TSK_BUFFER_TO_STRING(buffer));
* TSK_OBJECT_SAFE_FREE(buffer);
* @endcode
*/
int tsk_buffer_append(tsk_buffer_t* self, const void* data, tsk_size_t size)
{
	if(self && size){
		tsk_size_t oldsize = self->size;
		tsk_size_t newsize = oldsize + size;
		
		if(oldsize){
			self->data = tsk_realloc(self->data, newsize);
		}
		else{
			self->data = tsk_calloc(size, sizeof(uint8_t));
		}

		if(self->data){
			if(data){
				memcpy((void*)(TSK_BUFFER_TO_U8(self) + oldsize), data, size);
			}
			self->size = newsize;
			return 0;
		}
	}
	else{
		TSK_DEBUG_ERROR("Invalid parameter");
	}
	return -1;
}
Beispiel #3
0
int tsk_hmac_xxxcompute(const uint8_t* input, tsk_size_t input_size, const char* key, tsk_size_t key_size, tsk_hash_type_t type, uint8_t* digest)
{
#define TSK_MAX_BLOCK_SIZE	TSK_SHA1_BLOCK_SIZE

	tsk_size_t i, newkey_size;

	tsk_size_t block_size = type == md5 ? TSK_MD5_BLOCK_SIZE : TSK_SHA1_BLOCK_SIZE; // Only SHA-1 and MD5 are supported for now
	tsk_size_t digest_size = type == md5 ? TSK_MD5_DIGEST_SIZE : TSK_SHA1_DIGEST_SIZE;
	char hkey [TSK_MAX_BLOCK_SIZE];
	
	uint8_t ipad [TSK_MAX_BLOCK_SIZE];
	uint8_t opad [TSK_MAX_BLOCK_SIZE];
	

	memset(ipad, 0, sizeof(ipad));
	memset(opad, 0, sizeof(ipad));

	/*
	*	H(K XOR opad, H(K XOR ipad, input))
	*/

	// Check key len
	if (key_size > block_size){
		if(type == md5){
			TSK_MD5_DIGEST_CALC(key, key_size, (uint8_t*)hkey);
		}
		else if(type == sha1){
			TSK_SHA1_DIGEST_CALC((uint8_t*)key, (unsigned int)key_size, (uint8_t*)hkey);
		}
		else return -3;
		
		newkey_size = digest_size;
	}
	else{
		memcpy(hkey, key, key_size);
		newkey_size = key_size;
	}

	memcpy(ipad, hkey, newkey_size);
	memcpy(opad, hkey, newkey_size);
	
	/* [K XOR ipad] and [K XOR opad]*/
	for (i=0; i<block_size; i++){
		ipad[i] ^= 0x36;
		opad[i] ^= 0x5c;
	}
	
	
	{
		tsk_buffer_t *passx; // pass1 or pass2
		int pass1_done = 0;
		
		passx = tsk_buffer_create(ipad, block_size); // pass1
		tsk_buffer_append(passx, input, input_size);

digest_compute:
		if(type == md5){
			TSK_MD5_DIGEST_CALC(TSK_BUFFER_TO_U8(passx), (unsigned int)TSK_BUFFER_SIZE(passx), digest);
		}
		else{
			TSK_SHA1_DIGEST_CALC(TSK_BUFFER_TO_U8(passx), (unsigned int)TSK_BUFFER_SIZE(passx), digest);
		}

		if(pass1_done){
			TSK_OBJECT_SAFE_FREE(passx);
			goto pass1_and_pass2_done;
		}
		else{
			pass1_done = 1;
		}

		tsk_buffer_cleanup(passx);
		tsk_buffer_append(passx, opad, block_size); // pass2
		tsk_buffer_append(passx, digest, digest_size);

		goto digest_compute;
	}

pass1_and_pass2_done:

	return 0;
}