/**
* \file CryptoPlatform.h
* \brief High level crypto functions.
* \author Dmytro Podgornyi (d.podgornyi@samsung.com)
* \version 0.1
* \date Created May 28, 2013
* \par In Samsung Ukraine R&D Center (SURC) under a contract between
* \par LLC "Samsung Electronics Ukraine Company" (Kiev, Ukraine) and
* \par "Samsung Elecrtronics Co", Ltd (Seoul, Republic of Korea)
* \par Copyright: (c) Samsung Electronics Co, Ltd 2012. All rights reserved.
**/

#ifndef __CRYPTOPLATFORM_H_INCLUDED__
#define __CRYPTOPLATFORM_H_INCLUDED__

#include <stdint.h>
#include "CommLayerData.h"

#define SHA512_DIGEST_LEN 64
#define SHA256_DIGEST_LEN 32
#define SHA1_DIGEST_LEN 20

/* Callbacks */
/* prototype for genXXX_SK functions */
typedef int32_t (*genSKCallback_t)(KeyInfo_t*, void *, uint32_t *, void*, uint32_t, uint8_t*, uint32_t, uint8_t*, uint32_t);
/* prototype for hash digests */
typedef int32_t (*hashCallback_t)(uint8_t*, uint32_t, uint8_t*);

void TZ_LOG_HEX(const char *title, const unsigned char *data, int length);

int32_t getSKMCert(certType_t certType, uint8_t *out, uint32_t *outLen, void* wrapped, uint32_t wrappedLen);
int32_t getMLDAPCert(certType_t certType, uint8_t *out, uint32_t *outLen, void* wrapped, uint32_t wrappedLen);
int32_t signDataWithKey(KeyInfo_t *keyInfo, void *signData, uint32_t *signDataLen, void *wrappedIn, uint32_t wrappedInLen, uint8_t *TID, uint32_t TIDLen, uint8_t *attr, uint32_t attrLen);
int32_t storeOTACerts(KeyInfo_t *keyInfo, void *wrappedOut, uint32_t *wrappedOutLen, void *wrappedIn, uint32_t wrappedInLen, uint8_t *TID, uint32_t TIDLen, uint8_t *attr, uint32_t attrLen);
int32_t getOTA_SD_PK(KeyInfo_t *keyInfo, void *wrappedOut, uint32_t *wrappedOutLen, void *wrappedDevice, uint32_t wrappedDeviceLen, uint8_t *TID, uint32_t TIDLen, uint8_t *attr, uint32_t attrLen);
int32_t verifySDKey(void *wrapped, uint32_t wrappedLen, uint8_t *localTime, uint32_t localTimeLen);

int32_t verifyServiceKey(void *wrapped, uint32_t wrappedLen, uint8_t *localTime, uint32_t localTimeLen);
int32_t storeServiceKey(void *wrapped, uint32_t wrappedLen, uint8_t* outData, uint32_t* outDataLen);

#if defined(USE_QSEE) && !defined(USE_QSEE_WRAP_WITH_SFS)
int32_t wrapWithoutSFS(void *inData, uint32_t inDataLen, void *outData, uint32_t *outDataLen);
int32_t unwrapWithoutSFS(void *inData, uint32_t inDataLen, void *outData, uint32_t *outDataLen);
#endif

int32_t verifyRSA_SK(KeyInfo_t *keyInfo, void *wrapped, uint32_t wrappedLen);

/* TZ Platform specific API */
/* Hash */
int32_t getSHA1Digest(uint8_t* data, uint32_t len, uint8_t* digest);
int32_t getSHA256Digest(uint8_t* data, uint32_t len, uint8_t* digest);
int32_t getSHA512Digest(uint8_t* data, uint32_t len, uint8_t* digest);
/* Random */
int32_t getRandBlock(uint8_t* buf, uint32_t len);

/* Get DRK keys */
int32_t getSKMKeys(uint8_t** devCert, uint32_t* devCertLen, uint8_t** cert, uint32_t* certLen, uint8_t** prk, uint32_t* prkLen, uint8_t* keyBlob, uint32_t keyBlobLen);

/* Get keys generated by MLDAP */
int32_t getMLDAPKeys(uint8_t** certSM0, uint32_t* certSM0Len, uint8_t** certSM1, uint32_t* certSM1Len, uint8_t** certSD, uint32_t* certSDLen, uint8_t** privSD, uint32_t* privSDLen, uint8_t* keyBlob, uint32_t keyBlobLen);

/* Load Pcr. For Mobi Core from wrapped buffer, for Qualcomm from sfs */
int32_t loadPcr(uint8_t* wrapped, uint32_t wrappedLen, uint8_t* keyBlob, uint32_t* keyBlobLen);

/* Load key blob. For Mobi Core from wrapped buffer, for Qualcomm from sfs */
int32_t loadMLDAPKeyBlob(uint8_t* wrapped, uint32_t wrappedLen, KeyInfo_t* keyInfo, uint8_t* keyBlob, uint32_t* keyBlobLen);

/* Save key blob. For Mobi Core to wrapped buffer, for Qualcomm to sfs */
int32_t saveKeyBlob(uint8_t* keyBlob, uint32_t keyBlobLen, KeyInfo_t* keyInfo, uint8_t* wrappedOut, uint32_t* wrappedOutLen, uint8_t* TID, uint32_t TIDLen);

/* Load key blob. For Mobi Core from wrapped buffer, for Qualcomm from sfs or wrapped buffer */
int32_t loadSKMKeyBlob(uint8_t* wrapped, uint32_t wrappedLen, KeyInfo_t* keyInfo, uint8_t* keyBlob, uint32_t* keyBlobLen);

/* Integrity check */
int32_t getOemFlag(void);

#endif /* __CRYPTOPLATFORM_H_INCLUDED__ */