Beispiel #1
0
/*!
 * Append a descriptor which will load the key and counter values into
 * Sahara.
 *
 * @param[in,out] desc_chain  Where to append the new descriptor
 * @param         user_ctx    Info for acquiring memory
 * @param         auth_ctx    Location of CTR value
 * @param         key_info    Location of the key
 *
 * @return    A return code of type #fsl_shw_return_t.
 */
static inline fsl_shw_return_t load_ctr_key(sah_Head_Desc ** desc_chain,
					    fsl_shw_uco_t * user_ctx,
					    fsl_shw_acco_t * auth_ctx,
					    fsl_shw_sko_t * key_info)
{
	fsl_shw_return_t status;

	/* Assume AES */
	uint32_t header = SAH_HDR_SKHA_SET_MODE_IV_KEY
	    ^ sah_insert_skha_encrypt
	    ^ sah_insert_skha_mode_ctr ^ sah_insert_skha_modulus_128;

	/* Assume CCM-AES for now, since that is all that is supported */
	status = sah_add_in_key_desc(header,
				     auth_ctx->cipher_ctx_info.context,
				     auth_ctx->cipher_ctx_info.block_size_bytes,
				     key_info, user_ctx->mem_util, desc_chain);
	return status;
}
/*!
 * Insert descriptors to calculate ICV = HMAC(key=T, data=LEN|ALG|KEY')
 *
 * @param  user_ctx      User's context for this operation
 * @param  desc_chain    Descriptor chain to append to
 * @param  t_key_info    T's key object
 * @param  black_key     Beginning of Black Key region
 * @param  key_length    Number of bytes of key' there are in @c black_key
 * @param[out] hmac      Location to store ICV.  Will be tagged "USES" so
 *                       sf routines will not try to free it.
 *
 * @return    A return code of type #fsl_shw_return_t.
 */
static inline fsl_shw_return_t create_icv_calc(fsl_shw_uco_t * user_ctx,
					       sah_Head_Desc ** desc_chain,
					       fsl_shw_sko_t * t_key_info,
					       const uint8_t * black_key,
					       uint32_t key_length,
					       uint8_t * hmac)
{
	fsl_shw_return_t sah_code;
	uint32_t header;
	sah_Link *link1 = NULL;
	sah_Link *link2 = NULL;

	/* Load up T as key for the HMAC */
	header = (SAH_HDR_MDHA_SET_MODE_MD_KEY	/* #6 */
		  ^ sah_insert_mdha_algorithm_sha256
		  ^ sah_insert_mdha_init ^ sah_insert_mdha_hmac ^
		  sah_insert_mdha_pdata ^ sah_insert_mdha_mac_full);
	sah_code = sah_add_in_key_desc(header, NULL, 0, t_key_info,	/* Reference T in RED */
				       user_ctx->mem_util, desc_chain);
	if (sah_code != FSL_RETURN_OK_S) {
		goto out;
	}

	/* Previous step loaded key; Now set up to hash the data */
	header = SAH_HDR_MDHA_HASH;	/* #10 */

	/* Input - start with ownerid */
	sah_code = sah_Create_Link(user_ctx->mem_util, &link1,
				   (void *)&t_key_info->userid,
				   sizeof(t_key_info->userid),
				   SAH_USES_LINK_DATA);
	if (sah_code != FSL_RETURN_OK_S) {
		goto out;
	}

	/* Still input  - Append black-key fields len, alg, key' */
	sah_code = sah_Append_Link(user_ctx->mem_util, link1,
				   (void *)black_key + LENGTH_OFFSET,
				   (LENGTH_LENGTH
				    + ALGORITHM_LENGTH
				    + key_length), SAH_USES_LINK_DATA);

	if (sah_code != FSL_RETURN_OK_S) {
		goto out;
	}
	/* Output - computed ICV/HMAC */
	sah_code = sah_Create_Link(user_ctx->mem_util, &link2,
				   hmac, ICV_LENGTH,
				   SAH_USES_LINK_DATA | SAH_OUTPUT_LINK);
	if (sah_code != FSL_RETURN_OK_S) {
		goto out;
	}

	sah_code = sah_Append_Desc(user_ctx->mem_util, desc_chain,
				   header, link1, link2);

      out:
	if (sah_code != FSL_RETURN_OK_S) {
		(void)sah_Destroy_Link(user_ctx->mem_util, link1);
		(void)sah_Destroy_Link(user_ctx->mem_util, link2);
	}

	return sah_code;
}				/* create_icv_calc */