/**
 * Copyright (C) 2011 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Mobile Communication Division,
 * Digital Media & Communications Business, Samsung Electronics Co., Ltd.
 *
 * This software and its documentation are confidential and proprietary
 * information of Samsung Electronics Co., Ltd.  No part of the software and
 * documents may be copied, reproduced, transmitted, translated, or reduced to
 * any electronic medium or machine-readable form without the prior written
 * consent of Samsung Electronics.
 *
 * Samsung Electronics makes no representations with respect to the contents,
 * and assumes no responsibility for any errors that might appear in the
 * software and documents. This publication and the contents hereof are subject
 * to change without notice.
 *
 */
 /* tz_hdcp_key_WRAPAPI.c
 *
 *  Holds all trustzone receiver functions for HDCP2
 *
 */
#include <stdlib.h>
#include "tz_hdcp2_common.h"
#include <stdlib.h>
#include "tz_hdcp2_crypto.h"

int TZ_Decode_Key(u8 *encoded, u8 *decoded)
{
    int i = 0;
    u32 outlen = LC_128_SIZE;
    u8 prk[QSEE_AES128_KEY_SIZE] = {0};

    for (i = 0; i < QSEE_AES128_KEY_SIZE; i++)
        prk[i] = (u8) (0x20 + i);

    // 0 [16] lc key
    TZ_AES_decrypt(prk, QSEE_AES128_KEY_SIZE, encoded, LC_128_SIZE, decoded, &outlen, NULL, QSEE_CIPHER_MODE_ECB);
    if (outlen != LC_128_SIZE)
        return HDCP2_ERR_TRUSTZONE_DECODE_KEY;

    // 538 [320] private key
    outlen = 320;
    TZ_AES_decrypt(prk, QSEE_AES128_KEY_SIZE, encoded + 538, 320, decoded + 538, &outlen, NULL, QSEE_CIPHER_MODE_ECB);
    if (outlen != 320)
        return HDCP2_ERR_TRUSTZONE_DECODE_KEY;

    return HDCP2_OK;
}
