/*
 * Protobuf protocol for the unprivileged Unix Domain Socket (i.e. the socket
 * that is accessible by everybody).
 */

syntax = "proto3";

/******************************************************************************
 * get_status
 ******************************************************************************/

/**
 * @brief Request to get status
 *
 * @param maxlen Maximum size of buffer
 */
message StatusRequest {
	uint32 maxlen = 1;
}

/**
 * @brief Response to get status
 *
 * @param buffer_len Number of bytes present in the buffer - if no message
 *		     buffer is returned, this value specifies the number of
 *		     required bytes.
 * @param buffer Buffer with ASCII status - this is a NULL-terminated string
 */
message StatusResponse {
	int32 ret = 1;
	string buffer = 2;
}

/******************************************************************************
 * get_jent_status
 ******************************************************************************/

/**
 * @brief Request to get Jitterentropy status
 *
 * @param maxlen Maximum size of buffer
 */
message JentStatusRequest {
	uint32 maxlen = 1;
}

/**
 * @brief Response to get status
 *
 * @param buffer_len Number of bytes present in the buffer - if no message
 *		     buffer is returned, this value specifies the number of
 *		     required bytes.
 * @param buffer Buffer with ASCII status - this is a NULL-terminated string
 */
message JentStatusResponse {
	int32 ret = 1;
	string buffer = 2;
}

/******************************************************************************
 * get_ent_level
 ******************************************************************************/

/**
 * @brief Request to obtain the overall available entropy level
 */
message GetEntLvlRequest {
	/* No message */
}

/**
 * @brief Response returning the overall available entropy level
 *
 * @param ret Return code (0 on success, < 0 on error)
 * @param entcnt Available entropy in bits
 */
message GetEntLvlResponse {
	int32 ret = 1;
	uint32 entlvl = 2;
}

/******************************************************************************
 * is_fully_seeded
 ******************************************************************************/

/**
 * @brief Request to get fully seeded status
 *
 */
message IsFullySeededRequest {
}

/**
 * @brief Response to get fully seeded status
 *
 * @param ret Return code (0 on success, < 0 on error)
 * @param fully_seeded fully seeded status (true if fully seeded,
 *					   false otherwise)
 */
message IsFullySeededResponse {
	int32 ret = 1;
	bool fully_seeded = 2;
}

/******************************************************************************
 * get_random_bytes_full
 ******************************************************************************/

/**
 * @brief Request to get random bytes from fully seeded DRNG
 *
 * @param len number of random bytes that are requested
 */
message GetRandomBytesFullRequest {
	uint64 len = 1;
}

/**
 * @brief Response providing random bytes from fully seeded DRNG
 *
 * @param ret Return code of generation request (> 0 on success with the value
 *	      indicating the generated number of random bytes,
 *	      < -255 indicating the maximum number of bytes that can be
 *	      transferred in one request, < 0 on error)
 * @param randval Random bytes
 */
message GetRandomBytesFullResponse {
	int64 ret = 1;
	bytes randval = 2;
}

/******************************************************************************
 * get_random_bytes_pr
 ******************************************************************************/

/**
 * @brief Request to get random bytes from fully seeded DRNG with prediction
 *	  resistance enabled
 *
 * @param len number of random bytes that are requested
 */
message GetRandomBytesPrRequest {
	uint64 len = 1;
}

/**
 * @brief Response providing random bytes from fully seeded DRNG with prediction
 *	  resistance enabled.
 *
 * @param ret Return code of generation request (>= 0 on success with the value
 *	      indicating the generated number of random bytes,
 *	      < -255 indicating the maximum number of bytes that can be
 *	      transferred in one request, < 0 on error)
 * @param randval Random bytes
 */
message GetRandomBytesPrResponse {
	int64 ret = 1;
	bytes randval = 2;
}

/******************************************************************************
 * get_seed
 ******************************************************************************/

/**
 * @brief Request to get seed from entropy sources
 *
 * @param len buffer size provided by caller
 * @param flags the flags field - see esdm_get_seed documentation
 */
message GetSeedRequest {
	uint64 len = 1;
	uint32 flags = 2;
}

/**
 * @brief Response providing seed data from entropy sources.
 *
 * @param ret Return code of generation request as documented for esdm_get_seed
 * @param randval seed data
 */
message GetSeedResponse {
	int64 ret = 1;
	bytes randval = 2;
}

/******************************************************************************
 * get_random_bytes
 ******************************************************************************/

/**
 * @brief Request to get random bytes from DRNG that has no guarantee of being
 *	  initialized at the time random numbers are generated.
 *
 * @param len number of random bytes that are requested
 */
message GetRandomBytesRequest {
	uint64 len = 1;
}

/**
 * @brief Response providing random bytes from DRNG that has no guarantee of
 *	  being initialized at the time random numbers are generated.
 *
 * @param ret Return code of generation request (> 0 on success with the value
 *	      indicating the generated number of random bytes,
 *	      < -255 indicating the maximum number of bytes that can be
 *	      transferred in one request, < 0 on error)
 * @param randval Random bytes
 */
message GetRandomBytesResponse {
	int64 ret = 1;
	bytes randval = 2;
}

/******************************************************************************
 * Write data
 ******************************************************************************/

/**
 * @brief Request to write data into the auxiliary entropy pool and perform an
 *	  immediate reseed.
 *
 * @param data Buffer with the data to write into the DRNG - the entropy
 *	       estimator will remain unchanged.
 */
message WriteDataRequest {
	bytes data = 1;
}

/**
 * @brief Response providing the result of the write operation
 *
 * @param ret Return code (0 on success, < 0 on error)
 */
message WriteDataResponse {
	int32 ret = 1;
}

/******************************************************************************
 * RNDGETENTCNT IOCTL
 ******************************************************************************/

/**
 * @brief Request to obtain the available entropy in auxiliary pool
 */
message RndGetEntCntRequest {
	/* No message */
}

/**
 * @brief Response returning the available entropy in auxiliary pool
 *
 * @param ret Return code (0 on success, < 0 on error)
 * @param entcnt Available entropy in bits
 */
message RndGetEntCntResponse {
	int32 ret = 1;
	uint32 entcnt = 2;
}

/******************************************************************************
 * Poolsize /proc file
 ******************************************************************************/

/**
 * @brief Request to obtain the available poolsize
 */
message GetPoolsizeRequest {
	/* No message */
}

/**
 * @brief Response returning the available poolsize
 *
 * @param ret Return code (0 on success, < 0 on error)
 * @param poolsize Available poolsize in bits
 */
message GetPoolsizeResponse {
	int32 ret = 1;
	uint32 poolsize = 2;
}

/******************************************************************************
 * read of write_wakeup_threshold /proc file
 ******************************************************************************/

/**
 * @brief Request to obtain the available write wakeup threshold
 */
message GetWriteWakeupThreshRequest {
	/* No message */
}

/**
 * @brief Response returning the available write wakeup threshold
 *
 * @param ret Return code (0 on success, < 0 on error)
 * @param wakeup Available write wakeup threshold in bits
 */
message GetWriteWakeupThreshResponse {
	int32 ret = 1;
	uint32 wakeup = 2;
}

/******************************************************************************
 * read of urandom_min_reseed_secs /proc file
 ******************************************************************************/

/**
 * @brief Request to obtain the available write wakeup threshold
 */
message GetMinReseedSecsRequest {
	/* No message */
}

/**
 * @brief Response returning the min reseed secs
 *
 * @param ret Return code (0 on success, < 0 on error)
 * @param seconds Read time in seconds
 */
message GetMinReseedSecsResponse {
	int32 ret = 1;
	uint32 seconds = 2;
}

/******************************************************************************
 * Protocol handler
 ******************************************************************************/
service UnprivAccess {
	/* status calls */
	rpc RpcStatus (StatusRequest) returns (StatusResponse);
	rpc RpcGetEntLvl (GetEntLvlRequest) returns (GetEntLvlResponse);
	rpc RpcIsFullySeeded (IsFullySeededRequest) returns (IsFullySeededResponse);

	rpc RpcGetRandomBytesFull (GetRandomBytesFullRequest) returns
				  (GetRandomBytesFullResponse);
	rpc RpcGetRandomBytesPr (GetRandomBytesPrRequest) returns
				(GetRandomBytesPrResponse);
	rpc RpcGetRandomBytes (GetRandomBytesRequest) returns
			      (GetRandomBytesResponse);
	rpc RpcGetSeed (GetSeedRequest) returns (GetSeedResponse);
	rpc RpcWriteData (WriteDataRequest) returns (WriteDataResponse);

	/* IOCTLs */
	rpc RpcRndGetEntCnt (RndGetEntCntRequest) returns
			    (RndGetEntCntResponse);
	/* /proc entry */
	rpc RpcGetPoolsize (GetPoolsizeRequest) returns
			      (GetPoolsizeResponse);
	rpc RpcGetWriteWakeupThresh (GetWriteWakeupThreshRequest) returns
				    (GetWriteWakeupThreshResponse);
	rpc RpcGetMinReseedSecs (GetMinReseedSecsRequest) returns
				(GetMinReseedSecsResponse);

	/* entropy source specific calls */
	rpc RpcJentStatus (JentStatusRequest) returns (JentStatusResponse);
}
