Ejemplo n.º 1
0
/**
 * This function initializes the HASH machine on the CRYS level.
 *
 * This function allocates and initializes the HASH Context .
 * The function receives as input a pointer to store the context handle to HASH Context , 
 * it initializes the 
 * HASH Context with the cryptographic attributes that are needed for 
 * the HASH block operation ( initialize H's value for the HASH algorithm ).
 *
 * The function flow:
 *
 * 1) checking the validity of the arguments - returnes an error on an illegal argument case.
 * 2) Aquiring the working context from the CCM manager.
 * 3) Initializing the context with the parameters passed by the user and with the init values
 *    of the HASH.
 * 4) loading the user tag to the context.
 * 5) 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[in] OperationMode - The operation mode : MD5 or SHA1.
 *
 * @return CRYSError_t on success the function returns CRYS_OK else non ZERO error.
 *      
 */
CEXPORT_C CRYSError_t CRYS_HASH_Init(CRYS_HASHUserContext_t*    ContextID_ptr,
                                     CRYS_HASH_OperationMode_t  OperationMode)
{
	struct sep_ctx_hash *pHashContext;
	CRYS_HASHPrivateContext_t *pHashPrivContext;
	int symRc = DX_RET_OK;
	if ( ContextID_ptr == DX_NULL ) {
		return CRYS_HASH_INVALID_USER_CONTEXT_POINTER_ERROR;
	}

	if( OperationMode >= CRYS_HASH_NumOfModes ) {
		//PRINT_INFO("HASH MODE ERROR\n");
		return CRYS_HASH_ILLEGAL_OPERATION_MODE_ERROR;
	}

	/*pointer for CTX  allocation*/ 
	/* FUNCTION LOGIC */
	/* 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_InitUserCtxLocation(ContextID_ptr->buff,
						  sizeof(CRYS_HASHUserContext_t), 
						  sizeof(struct sep_ctx_hash));
	pHashPrivContext = (CRYS_HASHPrivateContext_t *)&(((uint32_t*)pHashContext)[CRYS_HASH_USER_CTX_ACTUAL_SIZE_IN_WORDS-1]);

	pHashContext->alg = SEP_CRYPTO_ALG_HASH;
	pHashPrivContext->isLastBlockProcessed = 0;

	switch (OperationMode) {
		case CRYS_HASH_SHA1_mode:
			pHashContext->mode = SEP_HASH_SHA1;
			break;
		case CRYS_HASH_SHA224_mode:
			pHashContext->mode = SEP_HASH_SHA224;
			break;
		case CRYS_HASH_SHA256_mode:
			pHashContext->mode = SEP_HASH_SHA256;
			break;
		case CRYS_HASH_SHA384_mode:
			pHashContext->mode = SEP_HASH_SHA384;
			break;
		case CRYS_HASH_SHA512_mode:
			pHashContext->mode = SEP_HASH_SHA512;
			break;
		case CRYS_HASH_MD5_mode:
		default:
			return CRYS_HASH_ILLEGAL_OPERATION_MODE_ERROR;
	}
       //PRINT_INFO("--->NOW into CRYS_HASH_Init,begin SymDriverAdaptorInit\n");
	symRc = SymDriverAdaptorInit((struct sep_ctx_generic *)pHashContext);
	//PRINT_INFO("---> after SymDriverAdaptorInit,symRc=0x%x\n",symRc);
	return DX_CRYS_RETURN_ERROR(symRc, 0, SymAdaptor2CrysHashErr);
}
Ejemplo n.º 2
0
/**
 * @brief This function is used to initialize the RC4 machine.
 *        To operate the RC4 machine, this should be the first function called.      
 *
 * @param[in] ContextID_ptr - A pointer to the RC4 context buffer that is allocated by the user 
 *                       and is used for the RC4 machine operation.
 * @param[in] Key_ptr -  A pointer to the user's key buffer.
 * @param[in] KeySize - The size of the KEY in bytes. Requirements:
 *             - for SW implementation    0 < KeySize < CRYS_RC4_MAX_KEY_SIZE_IN_BYTES,
 *             - for HW implementation    LLF_RC4_MIN_KEY_SIZE_IN_BYTES  < KeySize < LLF_RC4_MAX_KEY_SIZE_IN_BYTES,
 *
 * @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_Init(CRYS_RC4UserContext_t *ContextID_ptr,
                                     DxUint8_t *Key_ptr, 
                                     DxUint32_t KeySizeInBytes)
{
	int symRc = DX_RET_OK;

	/* pointer on SEP RC4 context struct*/
	struct sep_ctx_rc4 *pRc4Context;
	/* ............... checking the parameters validity ................... */
	/* -------------------------------------------------------------------- */
    
	/* 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 Keys size is invalid return an error */
	if ((KeySizeInBytes == 0) ||
	    (KeySizeInBytes > CRYS_RC4_MAX_KEY_SIZE_IN_BYTES)) {
		return CRYS_RC4_ILLEGAL_KEY_SIZE_ERROR;
	}
	 
	/* If the the key pointer is not validity */
	if (Key_ptr == DX_NULL) {
		return CRYS_RC4_INVALID_KEY_POINTER_ERROR;
	}
	
	/* check validity for priv */
	if ( DxCcAcl_IsBuffAccessOk(ACCESS_READ, Key_ptr, KeySizeInBytes) ||
	     DxCcAcl_IsBuffAccessOk(ACCESS_READ_WRITE, ContextID_ptr, sizeof(CRYS_RC4UserContext_t)) ) {
		return CRYS_RC4_ILLEGAL_PARAMS_ERROR;
	}

	/* Get pointer within the buffer that can accomodate context without 
	   crossing a page */
	pRc4Context = (struct sep_ctx_rc4 *)DX_InitUserCtxLocation(ContextID_ptr->buff,
						  sizeof(CRYS_RC4UserContext_t), 
						  sizeof(struct sep_ctx_rc4));

	pRc4Context->alg = SEP_CRYPTO_ALG_RC4;
	pRc4Context->key_size = KeySizeInBytes;

	DX_PAL_MemCopy(pRc4Context->key, Key_ptr, KeySizeInBytes);

	/* ................. calling the low level init function ................. */
	/* ----------------------------------------------------------------------- */
	
	symRc = SymDriverAdaptorInit((struct sep_ctx_generic *)pRc4Context);
	return DX_CRYS_RETURN_ERROR(symRc, 0, SymAdaptor2CrysRc4Err);
}
Ejemplo n.º 3
0
/**
 * This function initializes the HMAC machine on the CRYS level.
 *
 * The function allocates and initializes the HMAC Context .
 * The function receives as input a pointer to store the context handle to HMAC Context. 
 *
 * The function executes a HASH_init session and processes a HASH update 
 * on the Key XOR ipad and stores it in the context.
 *
 * @param[in] ContextID_ptr - A pointer to the HMAC context buffer allocated by the user 
 *                       that is used for the HMAC machine operation.
 *
 * @param[in] OperationMode - The operation mode: MD5 or SHA1.
 *
 * @param[in] key_ptr - The pointer to the user's key buffer, 
 *			or its digest (if larger than the hash block size).
 *
 * @param[in] keySize - The size of the received key. Must not exceed the associated
 *                      hash block size. For larger keys the caller must provide
 *                      a hash digest of the key as the actual key.
 *
 * @return CRYSError_t - On success the function returns the value CRYS_OK, 
 *			and on failure a non-ZERO error.
 *      
 */
CIMPORT_C CRYSError_t CRYS_HMAC_Init(CRYS_HMACUserContext_t *ContextID_ptr,
                           CRYS_HASH_OperationMode_t OperationMode,
                           DxUint8_t *key_ptr,
                           DxUint16_t keySize)
{
	struct sep_ctx_hmac *pHmacContext;
	CRYS_HMACPrivateContext_t *pHmacPrivContext;
	int symRc = DX_RET_OK;
	DxUint32_t HashBlockSize;

	/* 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 if the key pointer is valid */
	if( key_ptr == DX_NULL ) {
		return CRYS_HMAC_INVALID_KEY_POINTER_ERROR;
	}

	/* check if the operation mode is legal and set hash block size */
	switch (OperationMode) {
		case CRYS_HASH_SHA1_mode:
		case CRYS_HASH_SHA224_mode:
		case CRYS_HASH_SHA256_mode:
			HashBlockSize = CRYS_HASH_BLOCK_SIZE_IN_BYTES;
			break;
		case CRYS_HASH_SHA384_mode:
		case CRYS_HASH_SHA512_mode:
			HashBlockSize = CRYS_HASH_SHA512_BLOCK_SIZE_IN_BYTES;
			break;
		default:
			return CRYS_HMAC_ILLEGAL_OPERATION_MODE_ERROR;
	}
	
	/* check if the key size is valid */
	if (keySize == 0) {
		return CRYS_HMAC_UNVALID_KEY_SIZE_ERROR;
	}

	/* check validity for priv */
	if ( DxCcAcl_IsBuffAccessOk(ACCESS_READ, key_ptr, keySize) ||
	     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_InitUserCtxLocation(ContextID_ptr->buff,
						  sizeof(CRYS_HMACUserContext_t),
						  sizeof(struct sep_ctx_hmac));
	pHmacPrivContext = (CRYS_HMACPrivateContext_t *)&(((uint32_t*)pHmacContext)[CRYS_HMAC_USER_CTX_ACTUAL_SIZE_IN_WORDS-1]);

	pHmacContext->alg = SEP_CRYPTO_ALG_HMAC;
	pHmacContext->mode = Crys2SepHashMode(OperationMode);
	pHmacPrivContext->isLastBlockProcessed = 0;

	if( keySize > HashBlockSize ) {
		symRc = CRYS_HASH  ( OperationMode,
					   key_ptr,
					   keySize,
					   (uint32_t*)pHmacContext->k0 );/*Write the result into th context*/
                           
		if( symRc != CRYS_OK )
			return symRc;
      
		/* update the new key size according to the mode */
		switch(OperationMode) {
      			case CRYS_HASH_SHA1_mode:
      				keySize = CRYS_HASH_SHA1_DIGEST_SIZE_IN_BYTES;
      				break;
      			case CRYS_HASH_SHA224_mode:
      				keySize = CRYS_HASH_SHA224_DIGEST_SIZE_IN_BYTES;
      				break;
      			case CRYS_HASH_SHA256_mode:
      				keySize = CRYS_HASH_SHA256_DIGEST_SIZE_IN_BYTES;
      				break;
      			case CRYS_HASH_SHA384_mode:
      				keySize = CRYS_HASH_SHA384_DIGEST_SIZE_IN_BYTES;
      				break;
      			case CRYS_HASH_SHA512_mode:
      				keySize = CRYS_HASH_SHA512_DIGEST_SIZE_IN_BYTES;
      				break;
      			default:
      				break;
		}
	}/* end of key larger then 64 bytes case */
	else {
	      DX_PAL_MemCopy(pHmacContext->k0 , key_ptr , keySize );                                   
	}
	pHmacContext->k0_size = keySize;

	symRc = SymDriverAdaptorInit((struct sep_ctx_generic *)pHmacContext);
	return DX_CRYS_RETURN_ERROR(symRc, 0, SymAdaptor2CrysHmacErr);
}
Ejemplo n.º 4
0
CIMPORT_C CRYSError_t  CRYS_DES_Init( CRYS_DESUserContext_t *ContextID_ptr,
				  CRYS_DES_Iv_t            IV_ptr,
				  CRYS_DES_Key_t           *Key_ptr,
				  CRYS_DES_NumOfKeys_t     NumOfKeys,
				  CRYS_DES_EncryptMode_t   EncryptDecryptFlag,
				  CRYS_DES_OperationMode_t OperationMode )
{
	int symRc = DX_RET_OK;
	
	/* pointer on SEP DES context struct*/
	struct sep_ctx_cipher *pDesContext;

	/* ............... checking the parameters validity ................... */
	/* -------------------------------------------------------------------- */
	
	/* 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;
	}
	  
	/* check if the operation mode is legal */
	if( OperationMode >= CRYS_DES_NumOfModes ) {
		return CRYS_DES_ILLEGAL_OPERATION_MODE_ERROR;
	}
	  
	/* if the operation mode selected is CBC then check the validity of
	  the IV counter pointer */    
	if( (OperationMode == CRYS_DES_CBC_mode) && (IV_ptr == DX_NULL) ) {
		return CRYS_DES_INVALID_IV_PTR_ON_NON_ECB_MODE_ERROR;
	}
	  
	/* If the number of keys in invalid return an error */
	if( (NumOfKeys >= CRYS_DES_NumOfKeysOptions) || (NumOfKeys == 0) ) {
		return CRYS_DES_ILLEGAL_NUM_OF_KEYS_ERROR;
	}
	 
	/*check the valisity of the key pointer */
	if( Key_ptr == DX_NULL ) {
		return CRYS_DES_INVALID_KEY_POINTER_ERROR;
	}
	  
	/* Check the Encrypt / Decrypt flag validity */
	if( EncryptDecryptFlag >= CRYS_DES_EncryptNumOfOptions ) {
		return CRYS_DES_INVALID_ENCRYPT_MODE_ERROR;
	}

	/* check validity for priv */
	if ( DxCcAcl_IsBuffAccessOk(ACCESS_READ, Key_ptr, NumOfKeys * SEP_DES_ONE_KEY_SIZE) ||
	     DxCcAcl_IsBuffAccessOk(ACCESS_READ_WRITE, ContextID_ptr, sizeof(CRYS_DESUserContext_t)) ||
	     ((IV_ptr != DX_NULL) && (DxCcAcl_IsBuffAccessOk(ACCESS_READ, IV_ptr, sizeof(CRYS_DES_Iv_t)))) ) {
		return CRYS_DES_ILLEGAL_PARAMS_ERROR;
	}
	/* Get pointer to contiguous context in the HOST buffer */ 
	pDesContext = (struct sep_ctx_cipher *)DX_InitUserCtxLocation(ContextID_ptr->buff,
						  sizeof(CRYS_DESUserContext_t), 
						  sizeof(struct sep_ctx_cipher));
	pDesContext->alg = SEP_CRYPTO_ALG_DES;
	pDesContext->mode = MakeSepDesMode(OperationMode);
	pDesContext->direction = (enum sep_crypto_direction)EncryptDecryptFlag;
	pDesContext->key_size = NumOfKeys * SEP_DES_BLOCK_SIZE;

	DX_PAL_MemCopy(pDesContext->key, Key_ptr, pDesContext->key_size);

	if (pDesContext->mode == SEP_CIPHER_CBC) {
		DX_PAL_MemCopy(pDesContext->block_state, IV_ptr, CRYS_DES_IV_SIZE_IN_BYTES);
	}

	symRc = SymDriverAdaptorInit((struct sep_ctx_generic *)pDesContext);
	return DX_CRYS_RETURN_ERROR(symRc, 0, SymAdaptor2CrysDesErr);
}