Exemplo n.º 1
0
/**
 * This function process a block of data via the HASH Hardware.
 * The function receives as input an handle to the  HASH Context , that was initialized before
 * by an CRYS_HASH_Init function or by other CRYS_HASH_Update 
 * function. The function Sets the hardware with the last H's 
 * value that where stored in the CRYS HASH context and then 
 * process the data block using the hardware and in the end of 
 * the process stores in the HASH context the H's value HASH 
 * Context with the cryptographic attributes that are needed for 
 * the HASH block operation ( initialize H's value for the HASH 
 * algorithm ). This function is used in cases not all the data 
 * is arrange in one continues buffer.
 *
 * The function flow:
 *
 * 1) checking the parameters validty if there is an error the function shall exit with an error code. 
 * 2) Aquiring the working context from the CCM manager.
 * 3) If there isnt enouth data in the previous update data buff in the context plus the received data
 *    load it to the context buffer and exit the function.
 * 4) fill the previous update data buffer to contain an entire block. 
 * 5) Calling the hardware low level function to execute the update.
 * 6) fill the previous update data buffer with the data not processed at the end of the received data.
 * 7) release the CCM context.
 * 
 * @param[in] ContextID_ptr - a pointer to the HASH context buffer allocated by the user that
 *                       is used for the HASH machine operation.
 *
 * @param DataIn_ptr a pointer to the buffer that stores the data to be 
 *                       hashed .
 * 
 * @param DataInSize  The size of the data to be hashed in bytes. 
 *
 * @return CRYSError_t on success the function returns CRYS_OK else non ZERO error.
 *
 */
CEXPORT_C CRYSError_t CRYS_HASH_Update(CRYS_HASHUserContext_t*    ContextID_ptr,
                                       DxUint8_t*                 DataIn_ptr,
                                       DxUint32_t                 DataInSize )
{                             
	struct sep_ctx_hash *pHashContext;
	CRYS_HASHPrivateContext_t *pHashPrivContext;
	int symRc = DX_RET_OK;
	int hash_block_size_in_bytes = 0;

	if ( ContextID_ptr == DX_NULL ) {
		return CRYS_HASH_INVALID_USER_CONTEXT_POINTER_ERROR;
	}

	if( DataInSize == 0 ) {
		return CRYS_OK;
	}
	
	if( DataIn_ptr == DX_NULL ) {
		return CRYS_HASH_DATA_IN_POINTER_INVALID_ERROR;
	}
      // PRINT_INFO("--->NOW enter into CRYS_HASH_Update\n");
	/* check validity for priv */
	if (DxCcAcl_IsBuffAccessOk(ACCESS_READ_WRITE, ContextID_ptr, sizeof(CRYS_HASHUserContext_t))) {
		return CRYS_HASH_ILLEGAL_PARAMS_ERROR;
	}

	/* Get pointer to contiguous context in the HOST buffer */ 
	pHashContext = (struct sep_ctx_hash *)DX_GetUserCtxLocation(ContextID_ptr->buff);

	pHashPrivContext = (CRYS_HASHPrivateContext_t *)&(((uint32_t*)pHashContext)[CRYS_HASH_USER_CTX_ACTUAL_SIZE_IN_WORDS-1]);

	if (pHashPrivContext->isLastBlockProcessed != 0) {
		return CRYS_HASH_LAST_BLOCK_ALREADY_PROCESSED_ERROR;
	}

	if (pHashContext->mode < SEP_HASH_SHA512)
		hash_block_size_in_bytes = CRYS_HASH_BLOCK_SIZE_IN_BYTES;
	else
		hash_block_size_in_bytes = CRYS_HASH_SHA512_BLOCK_SIZE_IN_BYTES;

	if ((DataInSize % hash_block_size_in_bytes) == 0) {
		symRc = SymDriverAdaptorProcess((struct sep_ctx_generic *)pHashContext,
					DataIn_ptr, NULL, DataInSize);

		if (symRc != DX_RET_OK) {
			return DX_CRYS_RETURN_ERROR(symRc, 0, SymAdaptor2CrysHashErr);
		}
	} else { /* this is the last block */
		pHashPrivContext->isLastBlockProcessed = 1;
		symRc = SymDriverAdaptorFinalize((struct sep_ctx_generic *)pHashContext,
						DataIn_ptr, NULL, DataInSize);

		if (symRc != DX_RET_OK) {
			return DX_CRYS_RETURN_ERROR(symRc, 0, SymAdaptor2CrysHashErr);
		}
	}
       //PRINT_INFO("--->NOW leave CRYS_HASH_Update\n");
	return CRYS_OK;
}
Exemplo n.º 2
0
/**
 * This function finalizes the HMAC processing of a data block.
 * The function receives as input a handle to the HMAC Context that was previously initialized 
 * by a CRYS_HMAC_Init function or by a CRYS_HMAC_Update function.
 * This function finishes the HASH operation on the ipad and text, and then 
 * executes a new HASH operation with the key XOR opad and the previous HASH operation result.
 *
 *  @param[in] ContextID_ptr - A pointer to the HMAC context buffer allocated by the user 
 *                       that is used for the HMAC machine operation.
 *
 *  @retval HmacResultBuff - A pointer to the target buffer where the 
 *                       HMAC result stored in the context is loaded to.
 *
 * @return CRYSError_t - On success the function returns CRYS_OK, 
 *			and on failure a non-ZERO error.
 */
CIMPORT_C CRYSError_t CRYS_HMAC_Finish( CRYS_HMACUserContext_t  *ContextID_ptr,
					CRYS_HASH_Result_t       HmacResultBuff )
{
	struct sep_ctx_hmac *pHmacContext;
	CRYS_HMACPrivateContext_t *pHmacPrivContext;
	int symRc = DX_RET_OK;
	uint32_t hmacDigesSize;

	/* if the users context ID pointer is DX_NULL return an error */
	if( ContextID_ptr == DX_NULL ) {
		return CRYS_HMAC_INVALID_USER_CONTEXT_POINTER_ERROR;
	}
	
	/* check validity for priv */
	if (DxCcAcl_IsBuffAccessOk(ACCESS_READ_WRITE, ContextID_ptr, sizeof(CRYS_HMACUserContext_t))) {
		return CRYS_HMAC_ILLEGAL_PARAMS_ERROR;
	}

	/* Get pointer to contiguous context in the HOST buffer */ 
	pHmacContext = (struct sep_ctx_hmac *)DX_GetUserCtxLocation(ContextID_ptr->buff);
	pHmacPrivContext = (CRYS_HMACPrivateContext_t *)&(((uint32_t*)pHmacContext)[CRYS_HMAC_USER_CTX_ACTUAL_SIZE_IN_WORDS-1]);

	/* check validity for priv */
	if (DxCcAcl_IsBuffAccessOk(ACCESS_READ_WRITE, pHmacContext, sizeof(struct sep_ctx_hmac))) {
		return CRYS_HMAC_ILLEGAL_PARAMS_ERROR;
	}

	if (pHmacPrivContext->isLastBlockProcessed == 0) {
		symRc = SymDriverAdaptorFinalize((struct sep_ctx_generic *)pHmacContext, NULL, NULL, 0);
		if (symRc != DX_RET_OK) {
			return DX_CRYS_RETURN_ERROR(symRc, 0, SymAdaptor2CrysHmacErr);
		}
	}
	switch(pHmacContext->mode){
		case SEP_HASH_SHA1:
			hmacDigesSize = SEP_SHA1_DIGEST_SIZE;
			break;
		case SEP_HASH_SHA224:
			hmacDigesSize = SEP_SHA224_DIGEST_SIZE;
			break;
		case SEP_HASH_SHA256:
			hmacDigesSize = SEP_SHA256_DIGEST_SIZE;
			break;
		case SEP_HASH_SHA384:
			hmacDigesSize = SEP_SHA384_DIGEST_SIZE;
			break;
		case SEP_HASH_SHA512:
			hmacDigesSize = SEP_SHA512_DIGEST_SIZE;
			break;
		default:
			hmacDigesSize = -1;
			break;
	}

	DX_PAL_MemCopy(HmacResultBuff, pHmacContext->digest, hmacDigesSize);
	return CRYS_OK;
}
Exemplo n.º 3
0
CIMPORT_C CRYSError_t CRYS_HMAC_Update(CRYS_HMACUserContext_t  *ContextID_ptr,
                             DxUint8_t                 *DataIn_ptr,
                             DxUint32_t                 DataInSize )
{
	struct sep_ctx_hmac *pHmacContext;
	CRYS_HMACPrivateContext_t *pHmacPrivContext;
	int symRc = DX_RET_OK;
	uint32_t blockSizeBytes;
	
	/* if the users context ID pointer is DX_NULL return an error */
	if( ContextID_ptr == DX_NULL ) {
		return CRYS_HMAC_INVALID_USER_CONTEXT_POINTER_ERROR;
	}

	/* if the users Data In pointer is illegal and the size is not 0 return an error */
	if( (DataIn_ptr == DX_NULL) && DataInSize ) {
		return CRYS_HMAC_DATA_IN_POINTER_INVALID_ERROR;
	}
	   
	/* if the data size is zero no need to execute an update , return CRYS_OK */
	if( DataInSize == 0 ) {
		return CRYS_OK;
	}
	
	/* check validity for priv */
	if (DxCcAcl_IsBuffAccessOk(ACCESS_READ_WRITE, ContextID_ptr, sizeof(CRYS_HMACUserContext_t))) {
		return CRYS_HMAC_ILLEGAL_PARAMS_ERROR;
	}

	/* Get pointer to contiguous context in the HOST buffer */ 
	pHmacContext = (struct sep_ctx_hmac *)DX_GetUserCtxLocation(ContextID_ptr->buff);
	pHmacPrivContext = (CRYS_HMACPrivateContext_t *)&(((uint32_t*)pHmacContext)[CRYS_HMAC_USER_CTX_ACTUAL_SIZE_IN_WORDS-1]);

	if (pHmacPrivContext->isLastBlockProcessed != 0) {
		return CRYS_HMAC_LAST_BLOCK_ALREADY_PROCESSED_ERROR;
	}

	blockSizeBytes = GetHmacBlocktSize(pHmacContext->mode);
	if ((DataInSize % blockSizeBytes) == 0) {
		symRc = SymDriverAdaptorProcess((struct sep_ctx_generic *)pHmacContext,
					DataIn_ptr, NULL, DataInSize);
		if (symRc != DX_RET_OK) {
			return DX_CRYS_RETURN_ERROR(symRc, 0, SymAdaptor2CrysHmacErr);
		}
	} else { /* this is the last block */
		pHmacPrivContext->isLastBlockProcessed = 1;
		symRc = SymDriverAdaptorFinalize((struct sep_ctx_generic *)pHmacContext,
						DataIn_ptr, NULL, DataInSize);
		if (symRc != DX_RET_OK) {
			return DX_CRYS_RETURN_ERROR(symRc, 0, SymAdaptor2CrysHmacErr);
		}
	}
	return CRYS_OK;
}
Exemplo n.º 4
0
/**
 * @brief This function is used to process a stream on the RC4 machine.
 *        This function should be called after the CRYS_RS4_Init. 
 *      
 *
 * @param[in] ContextID_ptr - A pointer to the RC4 context buffer allocated by the user 
 *                       that is used for the RC4 machine operation. This should be the 
 *                       same context as was used for the previous call of this session.
 *
 * @param[in] DataIn_ptr - The pointer to the buffer of the input data to the RC4. 
 *                   The pointer's value does not need to be word-aligned.
 *
 * @param[in] DataInSize - The size of the input data.
 *
 * @param[in,out] DataOut_ptr - The pointer to the buffer of the output data from the RC4. 
 *                        The pointer's value does not need to be word-aligned.  
 *
 * @return CRYSError_t - CRYS_OK,
 *                       CRYS_RC4_INVALID_USER_CONTEXT_POINTER_ERROR,
 *                       CRYS_RC4_ILLEGAL_KEY_SIZE_ERROR,
 *                       CRYS_RC4_INVALID_KEY_POINTER_ERROR
 */
CIMPORT_C CRYSError_t CRYS_RC4_Stream(CRYS_RC4UserContext_t *ContextID_ptr,   
					DxUint8_t *DataIn_ptr,                                  
					DxUint32_t DataInSize,
					DxUint8_t *DataOut_ptr)
{                                
	int symRc = DX_RET_OK;

	/* pointer on SEP RC4 context struct*/
	struct sep_ctx_rc4 *pRc4Context;
	/* ............... checking the parameters validity ................... */
	/* -------------------------------------------------------------------- */
	
	/* if no data to process -we're done */
	if (DataInSize == 0) {
		return CRYS_OK;
	}

	/* if the users context ID pointer is DX_NULL return an error */
	if (ContextID_ptr == DX_NULL) {
		return CRYS_RC4_INVALID_USER_CONTEXT_POINTER_ERROR;
	}
	
	/* if the users Data In pointer is illegal return an error */
	if (DataIn_ptr == DX_NULL) {
		return CRYS_RC4_DATA_IN_POINTER_INVALID_ERROR;
	}
	
	/* if the users Data Out pointer is illegal return an error */
	if (DataOut_ptr == DX_NULL) {
		return CRYS_RC4_DATA_OUT_POINTER_INVALID_ERROR;
	}

	/* data size must be a positive number */
	if (DataInSize == 0) {
		return CRYS_RC4_DATA_SIZE_ILLEGAL;
	}

	/* check validity for priv */
	if (DxCcAcl_IsBuffAccessOk(ACCESS_READ_WRITE, ContextID_ptr, sizeof(CRYS_RC4UserContext_t))) {
		return CRYS_RC4_ILLEGAL_PARAMS_ERROR;
	}

	pRc4Context = (struct sep_ctx_rc4 *)DX_GetUserCtxLocation(ContextID_ptr->buff);
    
	symRc = SymDriverAdaptorProcess((struct sep_ctx_generic *)pRc4Context,
					DataIn_ptr, DataOut_ptr, DataInSize);

	return DX_CRYS_RETURN_ERROR(symRc, 0, SymAdaptor2CrysRc4Err);
}
Exemplo n.º 5
0
static void InitCombinedContext(
	struct sep_ctx_combined *pCtx,
	CrysCombinedConfig_t *pConfig)
{
	CrysCombinedEngineSource_e engineSrc = INPUT_NULL;
	enum sep_engine_type engineType = SEP_ENGINE_NULL;
	int engineIdx;
	int done = 0;

	/* set alg */
	pCtx->alg = SEP_CRYPTO_ALG_COMBINED;
	pCtx->mode = 0;

	/* encode engines connections into SEP format */
	for (engineIdx = 0; (engineIdx < CRYS_COMBINED_MAX_NODES) && (!done); engineIdx++) {

		if(pConfig->node[engineIdx].pContext) {
			/* set context's pointers */
			pCtx->sub_ctx[engineIdx] = (struct sep_ctx_cache_entry *)DX_GetUserCtxLocation(pConfig->node[engineIdx].pContext);
		}
		else {
			pCtx->sub_ctx[engineIdx] = NULL;
		}
		/* set engine source */
		engineSrc = CrysCombinedEngineSrcGet(pConfig, engineIdx);

		/* set engine type */
		if (pCtx->sub_ctx[engineIdx] != NULL) {
			engineType = GetCryptoEngineType(pCtx->sub_ctx[engineIdx]->alg);
		} else if (engineSrc != INPUT_NULL) {
			/* incase engine source is not NULL and NULL sub-context
			is passed then DOUT is -DOUT */
			engineType = SEP_ENGINE_DOUT;
			done = 1; /* do not allow to set DOUT twice! */
		} else {
			/* both context pointer & input type are NULL -we're done */
			engineType = SEP_ENGINE_NULL;
			done = 1;
		}

		SepCombinedEnginePropsSet(&pCtx->mode, engineIdx, engineSrc, engineType);
		DX_PAL_LOG_DEBUG("engineSrc=%d  engineType=%d\n", engineSrc, engineType);
	}
}
Exemplo n.º 6
0
CEXPORT_C CRYSError_t CRYS_HASH_Finish( CRYS_HASHUserContext_t*   ContextID_ptr,
                                        CRYS_HASH_Result_t        HashResultBuff )
{  
	struct sep_ctx_hash *pHashContext;
	CRYS_HASHPrivateContext_t *pHashPrivContext;
	int symRc = DX_RET_OK;
       //PRINT_INFO("--->NOW enter into CRYS_HASH_Finish\n");
	if ( ContextID_ptr == DX_NULL ) {
		return CRYS_HASH_INVALID_USER_CONTEXT_POINTER_ERROR;
	}
   
	if ( HashResultBuff == DX_NULL ) {
		return CRYS_HASH_INVALID_RESULT_BUFFER_POINTER_ERROR;
	}

	/* check validity for priv */
	if (DxCcAcl_IsBuffAccessOk(ACCESS_READ_WRITE, ContextID_ptr, sizeof(CRYS_HASHUserContext_t))) {
		return CRYS_HASH_ILLEGAL_PARAMS_ERROR;
	}

	/* Get pointer to contiguous context in the HOST buffer */ 
	pHashContext = (struct sep_ctx_hash *)DX_GetUserCtxLocation(ContextID_ptr->buff);

	pHashPrivContext = (CRYS_HASHPrivateContext_t *)&(((uint32_t*)pHashContext)[CRYS_HASH_USER_CTX_ACTUAL_SIZE_IN_WORDS-1]);

	/* check access permission for applet */
	if (DxCcAcl_IsBuffAccessOk(ACCESS_READ_WRITE, HashResultBuff, sizeof(struct sep_ctx_hash))) {
		return CRYS_HASH_ILLEGAL_PARAMS_ERROR;
	}

	if (pHashPrivContext->isLastBlockProcessed == 0) {
		symRc = SymDriverAdaptorFinalize((struct sep_ctx_generic *)pHashContext, NULL, NULL, 0);

		if (symRc != DX_RET_OK) {
			return DX_CRYS_RETURN_ERROR(symRc, 0, SymAdaptor2CrysHashErr);
		}
	}

	/* Copy the result to the user buffer */
	DX_PAL_MemCopy(HashResultBuff, pHashContext->digest, CRYS_HASH_RESULT_SIZE_IN_WORDS*sizeof(DxUint32_t));

	return CRYS_OK;
}
Exemplo n.º 7
0
/**
 * @brief This function is used to process a block on the DES machine.
 *        This function should be called after the CRYS_DES_Init function was called.
 *      
 *
 * @param[in] ContextID_ptr - a pointer to the DES context buffer allocated by the user that
 *                       is used for the DES machine operation. this should be the same context that was
 *                       used on the previous call of this session.
 *
 * @param[in] DataIn_ptr - The pointer to the buffer of the input data to the DES. The pointer does 
 *                         not need to be aligned.
 *
 * @param[in] DataInSize - The size of the input data in bytes: must be not 0 and must be multiple 
 *                         of 8 bytes.
 *
 * @param[in/out] DataOut_ptr - The pointer to the buffer of the output data from the DES. The pointer does not 
 *                              need to be aligned.
 *
 * @return CRYSError_t - On success CRYS_OK is returned, on failure a
 *                        value MODULE_* crys_des_error.h
 */
CIMPORT_C CRYSError_t  CRYS_DES_Block( CRYS_DESUserContext_t	*ContextID_ptr,
				       DxUint8_t 		*DataIn_ptr,
				       DxUint32_t 		DataInSize,
				       DxUint8_t 		*DataOut_ptr )
{
	int symRc = DX_RET_OK;

	 /* pointer on SEP DES context struct*/
	struct sep_ctx_cipher *pDesContext;
	/* if the users context ID pointer is DX_NULL return an error */
	if( ContextID_ptr == DX_NULL ) {
		return CRYS_DES_INVALID_USER_CONTEXT_POINTER_ERROR;
	}
	
	/* if the users Data In pointer is illegal return an error */
	if( DataIn_ptr == DX_NULL ) {
		return CRYS_DES_DATA_IN_POINTER_INVALID_ERROR;
	}
	
	/* if the users Data Out pointer is illegal return an error */
	if( DataOut_ptr == DX_NULL ) {
		return CRYS_DES_DATA_OUT_POINTER_INVALID_ERROR;
	}

	/* data size must be a positive number and a block size mult */
	if (((DataInSize % CRYS_DES_BLOCK_SIZE_IN_BYTES) != 0) || (DataInSize == 0)) {
		return CRYS_DES_DATA_SIZE_ILLEGAL; 
	}

	/* check validity for priv */
	if (DxCcAcl_IsBuffAccessOk(ACCESS_READ_WRITE, ContextID_ptr, sizeof(CRYS_DESUserContext_t))) {
		return CRYS_DES_ILLEGAL_PARAMS_ERROR;
	}

	/* Get pointer to contiguous context in the HOST buffer */ 
	pDesContext = (struct sep_ctx_cipher *)DX_GetUserCtxLocation(ContextID_ptr->buff);

	symRc = SymDriverAdaptorProcess((struct sep_ctx_generic *)pDesContext,
					DataIn_ptr, DataOut_ptr, DataInSize);
	return DX_CRYS_RETURN_ERROR(symRc, 0, SymAdaptor2CrysDesErr);
}