<commit_before>
/*
 * Licensed Materials - Property of IBM
 *
 * trousers - An open source TCG Software Stack
 *
 * (C) Copyright International Business Machines Corp. 2004
 *
 */


#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <inttypes.h>

#include "trousers/tss.h"
#include "spi_internal_types.h"
#include "tcs_tsp.h"
#include "tcsps.h"
#include "tcs_utils.h"
#include "tcs_int_literals.h"
#include "capabilities.h"
#include "tcslog.h"
#include "req_mgr.h"
#include "tcsd_wrap.h"
#include "tcsd.h"


/*
 * Get a random number generated by the TPM.  Most (all?) TPMs return a maximum number of random
 * bytes that's less than the max allowed to be returned at the TSP level, which is 4K bytes.
 * According to the TPM compliance work posted here: http://www.prosec.rub.de/tpmcompliance.html,
 * some TPMs return as little as 132 bytes per query, which would require about 30 loops to get 4K.
 * We'll be extremely conservative here and loop 50 times, since it won't affect performance on
 * TPMs that return more bytes.
 */
TSS_RESULT
TCSP_GetRandom_Internal(TCS_CONTEXT_HANDLE hContext,	/* in */
			UINT32 * bytesRequested,	/* in, out */
			BYTE ** randomBytes)	/* out */
{
	UINT64 offset;
	TSS_RESULT result;
	UINT32 paramSize, totalReturned = 0, bytesReturned, retries = 50;
	BYTE txBlob[TSS_TPM_TXBLOB_SIZE], *rnd_tmp = NULL;

	LogDebugFn("%u bytes", *bytesRequested);

	if ((result = ctx_verify_context(hContext)))
		return result;

	do {
		offset = 10;
		LoadBlob_UINT32(&offset, *bytesRequested - totalReturned,
				txBlob);
		LoadBlob_Header(TPM_TAG_RQU_COMMAND, offset, TPM_ORD_GetRandom,
				txBlob);

		if ((result = req_mgr_submit_req(txBlob)))
			return result;

		offset = 10;
		result = UnloadBlob_Header(txBlob, &paramSize);
		if (!result) {
			UnloadBlob_UINT32(&offset, &bytesReturned, txBlob);

			LogDebugFn("received %u bytes from the TPM",
				   bytesReturned);

			rnd_tmp = realloc(rnd_tmp, totalReturned + bytesReturned);
			if (rnd_tmp == NULL) {
				LogError("malloc of %d bytes failed.",
					 bytesReturned);
				return TCSERR(TSS_E_OUTOFMEMORY);
			}
			UnloadBlob(&offset, bytesReturned, txBlob, &rnd_tmp[totalReturned]);
			totalReturned += bytesReturned;
		} else {
			free(rnd_tmp);
			return result;
		}
	} while (totalReturned < *bytesRequested && retries--);

	if (totalReturned != *bytesRequested) {
		LogDebugFn("Only %u random bytes recieved from TPM.", totalReturned);
		free(rnd_tmp);
		result = TCSERR(TSS_E_FAIL);
	} else
		*randomBytes = rnd_tmp;

	return result;
}

TSS_RESULT
TCSP_StirRandom_Internal(TCS_CONTEXT_HANDLE hContext,	/* in */
			 UINT32 inDataSize,	/* in */
			 BYTE * inData)	/* in */
{
	UINT64 offset;
	UINT32 paramSize;
	TSS_RESULT result;
	BYTE txBlob[TSS_TPM_TXBLOB_SIZE];

	LogDebug("Entering stir random");

	if ((result = ctx_verify_context(hContext)))
		return result;

	offset = 10;
	LoadBlob_UINT32(&offset, inDataSize, txBlob);
	LoadBlob(&offset, inDataSize, txBlob, inData);
	LoadBlob_Header(TPM_TAG_RQU_COMMAND, offset, TPM_ORD_StirRandom,
			txBlob);

	if ((result = req_mgr_submit_req(txBlob)))
		return result;

	offset = 10;
	result = UnloadBlob_Header(txBlob, &paramSize);
	LogResult("Stir random", result);
	return result;
}
<commit_msg>return error when trying to stir random w/ more than 255 bytes<commit_after>
/*
 * Licensed Materials - Property of IBM
 *
 * trousers - An open source TCG Software Stack
 *
 * (C) Copyright International Business Machines Corp. 2004
 *
 */


#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <inttypes.h>

#include "trousers/tss.h"
#include "spi_internal_types.h"
#include "tcs_tsp.h"
#include "tcsps.h"
#include "tcs_utils.h"
#include "tcs_int_literals.h"
#include "capabilities.h"
#include "tcslog.h"
#include "req_mgr.h"
#include "tcsd_wrap.h"
#include "tcsd.h"


/*
 * Get a random number generated by the TPM.  Most (all?) TPMs return a maximum number of random
 * bytes that's less than the max allowed to be returned at the TSP level, which is 4K bytes.
 * According to the TPM compliance work posted here: http://www.prosec.rub.de/tpmcompliance.html,
 * some TPMs return as little as 132 bytes per query, which would require about 30 loops to get 4K.
 * We'll be extremely conservative here and loop 50 times, since it won't affect performance on
 * TPMs that return more bytes.
 */
TSS_RESULT
TCSP_GetRandom_Internal(TCS_CONTEXT_HANDLE hContext,	/* in */
			UINT32 * bytesRequested,	/* in, out */
			BYTE ** randomBytes)	/* out */
{
	UINT64 offset;
	TSS_RESULT result;
	UINT32 paramSize, totalReturned = 0, bytesReturned, retries = 50;
	BYTE txBlob[TSS_TPM_TXBLOB_SIZE], *rnd_tmp = NULL;

	LogDebugFn("%u bytes", *bytesRequested);

	if ((result = ctx_verify_context(hContext)))
		return result;

	do {
		offset = 10;
		LoadBlob_UINT32(&offset, *bytesRequested - totalReturned,
				txBlob);
		LoadBlob_Header(TPM_TAG_RQU_COMMAND, offset, TPM_ORD_GetRandom,
				txBlob);

		if ((result = req_mgr_submit_req(txBlob)))
			return result;

		offset = 10;
		result = UnloadBlob_Header(txBlob, &paramSize);
		if (!result) {
			UnloadBlob_UINT32(&offset, &bytesReturned, txBlob);

			LogDebugFn("received %u bytes from the TPM",
				   bytesReturned);

			rnd_tmp = realloc(rnd_tmp, totalReturned + bytesReturned);
			if (rnd_tmp == NULL) {
				LogError("malloc of %d bytes failed.",
					 bytesReturned);
				return TCSERR(TSS_E_OUTOFMEMORY);
			}
			UnloadBlob(&offset, bytesReturned, txBlob, &rnd_tmp[totalReturned]);
			totalReturned += bytesReturned;
		} else {
			free(rnd_tmp);
			return result;
		}
	} while (totalReturned < *bytesRequested && retries--);

	if (totalReturned != *bytesRequested) {
		LogDebugFn("Only %u random bytes recieved from TPM.", totalReturned);
		free(rnd_tmp);
		result = TCSERR(TSS_E_FAIL);
	} else
		*randomBytes = rnd_tmp;

	return result;
}

TSS_RESULT
TCSP_StirRandom_Internal(TCS_CONTEXT_HANDLE hContext,	/* in */
			 UINT32 inDataSize,	/* in */
			 BYTE * inData)	/* in */
{
	UINT64 offset;
	UINT32 paramSize;
	TSS_RESULT result;
	BYTE txBlob[TSS_TPM_TXBLOB_SIZE];

	LogDebug("Entering stir random");

	if (inDataSize > 255) {
		LogDebugFn("inData is too large! (%u bytes)", inDataSize);
		return TCSERR(TSS_E_BAD_PARAMETER);
	}

	if ((result = ctx_verify_context(hContext)))
		return result;

	offset = 10;
	LoadBlob_UINT32(&offset, inDataSize, txBlob);
	LoadBlob(&offset, inDataSize, txBlob, inData);
	LoadBlob_Header(TPM_TAG_RQU_COMMAND, offset, TPM_ORD_StirRandom, txBlob);

	if ((result = req_mgr_submit_req(txBlob)))
		return result;

	result = UnloadBlob_Header(txBlob, &paramSize);
	LogResult("Stir random", result);
	return result;
}

{'fraction_non_alphanumeric': 0.1157086969605778, 'fraction_numerical': 0.013241047246464039, 'mean_word_length': 4.484323432343234, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5862343', 'n_tokens_mistral': 2612, 'n_tokens_neox': 2362, 'n_words': 625}