From 1b8ee5fbf7a554a6ca5b18fc758f4e19799e9b93 Mon Sep 17 00:00:00 2001 From: Piotr Sawicki Date: Mon, 23 Oct 2017 09:04:14 +0200 Subject: [PATCH 01/16] Make changing of shm file mode thread safe Change-Id: Id7b07a203878fdf16414c3b3fae281918671d345 --- TEECLib/src/teec_api.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/TEECLib/src/teec_api.c b/TEECLib/src/teec_api.c index a84aac7..0860520 100644 --- a/TEECLib/src/teec_api.c +++ b/TEECLib/src/teec_api.c @@ -41,6 +41,7 @@ #define SHM_MAX_ID INT32_MAX #define SHM_NAME_TEMPLATE "/teec_shm%d" +#define SHM_FILE_MODE 0660 /*----------------------------------------------------------------------------- * Globals *-----------------------------------------------------------------------------*/ @@ -98,23 +99,27 @@ static int32_t allocateSharedMemory(TEEC_SharedMemory *shm) { int fd_shm = -1; int res; - mode_t origMask = umask(0); - do { res = snprintf(shm_name, sizeof(shm_name), SHM_NAME_TEMPLATE, memKey); if (res == sizeof(shm_name)) { - umask(origMask); LOGE(TEEC_LIB, "the shm object name is too long"); return TEEC_ERROR_GENERIC; } - fd_shm = shm_open(shm_name, O_RDWR | O_CREAT | O_EXCL, 0660); + fd_shm = shm_open(shm_name, O_RDWR | O_CREAT | O_EXCL, SHM_FILE_MODE); if (fd_shm >= 0) { + res = fchmod(fd_shm, SHM_FILE_MODE); + if (res == -1) { + close(fd_shm); + shm_unlink(shm_name); + LOGE(TEEC_LIB, "Cannot change permission of the %s shared memory file, error: %s", + shm_name, strerror(errno)); + return TEEC_ERROR_GENERIC; + } break; } if (errno != EEXIST) { - umask(origMask); LOGE(TEEC_LIB, "Cannot create shared memory object, error: %s", strerror(errno)); return TEEC_ERROR_GENERIC; } @@ -122,8 +127,6 @@ static int32_t allocateSharedMemory(TEEC_SharedMemory *shm) { memKey++; } while (memKey < SHM_MAX_ID); - umask(origMask); - if (memKey == SHM_MAX_ID) { LOGE(TEEC_LIB, "Cannot find free shared memory slot"); return TEEC_ERROR_GENERIC; -- 2.7.4 From 59eebd3432bfb4b5083fcf062f6086db35f6fcc5 Mon Sep 17 00:00:00 2001 From: Krzysztof Dynowski Date: Tue, 24 Oct 2017 11:03:59 +0200 Subject: [PATCH 02/16] SVACE: fix DEREF_OF_NULL* warning Change-Id: I7a9b30bcafea43a61addb0f2e8fd899717bce7e1 --- TEECLib/src/teec_api.c | 6 +++++ TEEStub/PropertyAccess/PropertyApi.cpp | 5 ++-- ssflib/dep/cryptocore/source/CC_API.c | 3 +-- ssflib/dep/cryptocore/source/base/cc_bignum.c | 35 +++++++++++++++++++-------- ssflib/dep/uci/source/uci_api.c | 5 +++- ssflib/src/ssf_crypto.cpp | 11 +++++++-- ssflib/src/ssf_taentrypoint.cpp | 5 +++- 7 files changed, 52 insertions(+), 18 deletions(-) diff --git a/TEECLib/src/teec_api.c b/TEECLib/src/teec_api.c index 0860520..8c18e21 100644 --- a/TEECLib/src/teec_api.c +++ b/TEECLib/src/teec_api.c @@ -727,6 +727,9 @@ TEEC_Result TEEC_RegisterSharedMemory(TEEC_Context *context, sharedMem->imp = (TEEC_SharedMemoryImp*)OsaMalloc( sizeof(TEEC_SharedMemoryImp)); + if (sharedMem->imp == NULL) { + return TEE_ERROR_OUT_OF_MEMORY; + } TEEC_SharedMemoryImp* sharedMem_imp = (TEEC_SharedMemoryImp*)sharedMem->imp; sharedMem_imp->context = context; regmem.contextID = context_imp->contextID; @@ -839,6 +842,9 @@ TEEC_Result TEEC_AllocateSharedMemory(TEEC_Context *context, // Generate Shared Memory imp structure sharedMem->imp = (TEEC_SharedMemoryImp*)OsaMalloc( sizeof(TEEC_SharedMemoryImp)); + if (sharedMem->imp == NULL) { + return TEE_ERROR_OUT_OF_MEMORY; + } TEEC_SharedMemoryImp* sharedMem_imp = (TEEC_SharedMemoryImp*)sharedMem->imp; sharedMem->buffer = NULL; sharedMem_imp->context = context; diff --git a/TEEStub/PropertyAccess/PropertyApi.cpp b/TEEStub/PropertyAccess/PropertyApi.cpp index 60f1199..9b85dc1 100644 --- a/TEEStub/PropertyAccess/PropertyApi.cpp +++ b/TEEStub/PropertyAccess/PropertyApi.cpp @@ -410,9 +410,10 @@ uintptr_t _GetTargetPropsetType(TEE_PropSetHandle propsetOrEnumerator) { Property *targetProperty = NULL; PropertyEnumHandle *enumHandle = (PropertyEnumHandle*)propsetOrEnumerator; - if (enumHandle && enumHandle->property) + if (enumHandle && enumHandle->property) { targetProperty = enumHandle->property; - return targetProperty->propset; + return targetProperty->propset; + } } return 0; } diff --git a/ssflib/dep/cryptocore/source/CC_API.c b/ssflib/dep/cryptocore/source/CC_API.c index 8cb4f9f..03dad71 100644 --- a/ssflib/dep/cryptocore/source/CC_API.c +++ b/ssflib/dep/cryptocore/source/CC_API.c @@ -366,8 +366,7 @@ CryptoCoreContainer *create_CryptoCoreContainer(cc_u32 algorithm) // free CryptoCoreContainer data structure free(crt->ctx); free(crt); - crt = NULL; - break; + return NULL; } printf("TEST!!! after in create_CryptoCoreContainer(%p %d)\n",crt, ID_AES128); diff --git a/ssflib/dep/cryptocore/source/base/cc_bignum.c b/ssflib/dep/cryptocore/source/base/cc_bignum.c index f36508b..d9175ec 100644 --- a/ssflib/dep/cryptocore/source/base/cc_bignum.c +++ b/ssflib/dep/cryptocore/source/base/cc_bignum.c @@ -1608,12 +1608,15 @@ int SDRM_BN_Div(SDRM_BIG_NUM *BN_Quotient, SDRM_BIG_NUM *BN_Remainder, SDRM_BIG_ return CRYPTO_SUCCESS; } + tmp = 0; if (BN_Quotient == NULL) { - BN_Remainder->Length = temp_Divisor->Length; - tmp = SDRM_DWD_Div(bnTmp, BN_Remainder->pData, temp_Dividend->pData, temp_Dividend->Length, temp_Divisor->pData, temp_Divisor->Length); - SDRM_BN_OPTIMIZE_LENGTH(BN_Remainder); - BN_Remainder->sign = BN_Dividend->sign; + if (BN_Remainder != NULL) { + BN_Remainder->Length = temp_Divisor->Length; + tmp = SDRM_DWD_Div(bnTmp, BN_Remainder->pData, temp_Dividend->pData, temp_Dividend->Length, temp_Divisor->pData, temp_Divisor->Length); + SDRM_BN_OPTIMIZE_LENGTH(BN_Remainder); + BN_Remainder->sign = BN_Dividend->sign; + } } else if (BN_Remainder == NULL) { @@ -2852,6 +2855,9 @@ int SDRM_HEX2BN(cc_u8* pbSrc, SDRM_BIG_NUM *BN_Dst) //full string: bufferHex mod Length = 0 bufferHex = (cc_u8 *)malloc( sizeof(cc_u8) * (BN_Dst->Length * SDRM_SIZE_BLOCK)); + if (bufferHex == NULL) { + return CRYPTO_MEMORY_ALLOC_FAIL; + } //init byffer by 0 for(i = 0; i < BN_Dst->Length * SDRM_SIZE_BLOCK; i++) @@ -2992,6 +2998,11 @@ cc_u8 * SDRM_BN2STRBIN(cc_u32 *numberBits, SDRM_BIG_NUM *BN_Src) (*numberBits) = sizeof(cc_u8) * BN_Src->Length * SDRM_SIZE_OF_DWORD * 8 + 1; tempC = (cc_u8*)malloc(*numberBits); tempR = (cc_u8*)malloc(*numberBits); + if (tempC == NULL || tempR == NULL) { + free(tempR); + free(tempC); + return NULL; + } tempC[(*numberBits) - 1] = '\0'; for(i = BN_Src->Length - 1; (int)i >= 0 ; i--) { @@ -3104,7 +3115,7 @@ cc_u8 * SDRM_BN2STRFOUR(cc_u32 *numberBits, SDRM_BIG_NUM *BN_Src) } tempREM = SDRM_BN_Init(BN_Src->Size); num = SDRM_BN_Init(BN_Src->Size); - if( num == NULL)//fix prevent cid = 89093 by guoxing.xu + if(tempREM == NULL || num == NULL)//fix prevent cid = 89093 by guoxing.xu { free(strDestTemp); SDRM_BN_FREE(tempREM); @@ -3127,17 +3138,21 @@ cc_u8 * SDRM_BN2STRFOUR(cc_u32 *numberBits, SDRM_BIG_NUM *BN_Src) if((*numberBits) != 0) { strDest = (cc_u8*)malloc((*numberBits) + 1); - for(i = 0; i < (*numberBits); i++) { - strDest[i] = strDestTemp[((*numberBits) - 1) - i]; + if (strDest != NULL) { + for(i = 0; i < (*numberBits); i++) { + strDest[i] = strDestTemp[((*numberBits) - 1) - i]; + } + strDest[(*numberBits)] = '\0'; } - strDest[(*numberBits)] = '\0'; } else { (*numberBits) = 1; strDest = (cc_u8*)malloc((*numberBits) + 1); - strDest[0] = '0'; - strDest[(*numberBits)] = '\0'; + if (strDest != NULL) { + strDest[0] = '0'; + strDest[(*numberBits)] = '\0'; + } } free(strDestTemp); diff --git a/ssflib/dep/uci/source/uci_api.c b/ssflib/dep/uci/source/uci_api.c index c68357d..9ee995a 100644 --- a/ssflib/dep/uci/source/uci_api.c +++ b/ssflib/dep/uci/source/uci_api.c @@ -85,6 +85,9 @@ int uci_context_alloc(unsigned int algorithm, uci_engine_config_e config, UCI_HA #endif if (algorithm == ID_UCI_XCBCMAC) { ctx = (uci_context_s*)OsaMalloc(sizeof(uci_context_s)); + if (ctx == NULL) { + return UCI_ERROR; + } ctx->imp = (aes_xcbc_state *)OsaMalloc(sizeof(aes_xcbc_state)); ctx->alg = ID_UCI_XCBCMAC; *context = ctx; @@ -584,7 +587,7 @@ int uci_dup_handle(UCI_HANDLE srcoh, UCI_HANDLE* destoh) { uci_context_s *srcctx = (uci_context_s *)srcoh; uci_context_s *destctx = NULL; - if (destoh != NULL) { + if (destoh == NULL) { return UCI_ERROR; } diff --git a/ssflib/src/ssf_crypto.cpp b/ssflib/src/ssf_crypto.cpp index 7e4dda3..4020cdf 100644 --- a/ssflib/src/ssf_crypto.cpp +++ b/ssflib/src/ssf_crypto.cpp @@ -1146,7 +1146,9 @@ int crypto_internal_final(crypto_internal_operation *operation, unsigned char *s } total_processing_len += processing_len; out_size -= processing_len; - out_data = (unsigned char*)((unsigned long) out_data + processing_len); + if (out_data) { + out_data = (unsigned char*)((unsigned long) out_data + processing_len); + } operation->data_len = 0; } } @@ -1174,7 +1176,9 @@ int crypto_internal_final(crypto_internal_operation *operation, unsigned char *s total_processing_len += processing_len; in_size -= processing_len; in_data = (unsigned char*)((unsigned long) in_data + processing_len); - out_data = (unsigned char*)((unsigned long) out_data + processing_len); + if (out_data) { + out_data = (unsigned char*)((unsigned long) out_data + processing_len); + } } if (operation->info.mode == TEE_MODE_ENCRYPT) @@ -1239,6 +1243,9 @@ int crypto_internal_final(crypto_internal_operation *operation, unsigned char *s } else if (operation->info.mode == TEE_MODE_DECRYPT) { unsigned char * pad = out_data; unsigned int npad = 0; + if (pad == NULL) { + goto exit; + } if (operation->info.algorithm == TEE_ALG_AES_ECB_NOPAD || operation->info.algorithm == TEE_ALG_AES_CBC_NOPAD || operation->info.algorithm == TEE_ALG_DES_ECB_NOPAD || operation->info.algorithm == TEE_ALG_DES_CBC_NOPAD || diff --git a/ssflib/src/ssf_taentrypoint.cpp b/ssflib/src/ssf_taentrypoint.cpp index 5b66369..ac9c859 100644 --- a/ssflib/src/ssf_taentrypoint.cpp +++ b/ssflib/src/ssf_taentrypoint.cpp @@ -87,6 +87,10 @@ TEE_Result TEE_OpenTASession(const TEE_UUID* destination, if (!session || !destination) { return TEE_ERROR_BAD_PARAMETERS; } + uint32_t* sessionData = (uint32_t*)OsaMalloc(sizeof(uint32_t)); + if (sessionData == NULL) { + return TEE_ERROR_OUT_OF_MEMORY; + } memset(&data, 0, sizeof(IntTAOpenSessionData)); data.source = ssf_sharedthisTAUUID; data.destination = *destination; @@ -119,7 +123,6 @@ TEE_Result TEE_OpenTASession(const TEE_UUID* destination, // [inout] TEE_Param params[4], // [out] TEE_TASessionHandle* session, // [out] uint32_t* returnOrigin); - uint32_t* sessionData = (uint32_t*)OsaMalloc(sizeof(uint32_t)); if(params != NULL) { __TEE_Postprocess_Operation; -- 2.7.4 From efaed2173de54163757450100c885d9c29c665bc Mon Sep 17 00:00:00 2001 From: Krzysztof Dynowski Date: Tue, 24 Oct 2017 11:39:58 +0200 Subject: [PATCH 03/16] SVAVE: fix INVARIANT_RESULT.OP_ASSIGN, NULL_AFTER_DEREF Change-Id: I9b5a4854f2ad9ff703e036425f7ccf09ea45c888 --- TEEStub/PropertyAccess/PropertyApi.cpp | 7 ++++--- ssflib/dep/cryptocore/source/base/cc_bignum.c | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/TEEStub/PropertyAccess/PropertyApi.cpp b/TEEStub/PropertyAccess/PropertyApi.cpp index 9b85dc1..2a52fdf 100644 --- a/TEEStub/PropertyAccess/PropertyApi.cpp +++ b/TEEStub/PropertyAccess/PropertyApi.cpp @@ -351,9 +351,10 @@ void TEE_StartPropertyEnumerator(TEE_PropSetHandle enumerator, } } - newEnumHandle->property->setPropSet((uintptr_t)propSet); - if (newEnumHandle && newEnumHandle->property) - newEnumHandle->property->start(); + if (newEnumHandle && newEnumHandle->property) { + newEnumHandle->property->setPropSet((uintptr_t)propSet); + newEnumHandle->property->start(); + } } void TEE_ResetPropertyEnumerator(TEE_PropSetHandle enumerator) { diff --git a/ssflib/dep/cryptocore/source/base/cc_bignum.c b/ssflib/dep/cryptocore/source/base/cc_bignum.c index d9175ec..3d4a1b6 100644 --- a/ssflib/dep/cryptocore/source/base/cc_bignum.c +++ b/ssflib/dep/cryptocore/source/base/cc_bignum.c @@ -2879,7 +2879,7 @@ int SDRM_HEX2BN(cc_u8* pbSrc, SDRM_BIG_NUM *BN_Dst) { case '0': BN_Dst->pData[i] = BN_Dst->pData[i] << 4; - BN_Dst->pData[i] |= 0x0; + //BN_Dst->pData[i] |= 0x0; Unnecessary break; case '1': BN_Dst->pData[i] = BN_Dst->pData[i] << 4; -- 2.7.4 From 4a7348c1ac1062230b10f12c10d407adf48c3751 Mon Sep 17 00:00:00 2001 From: Mariusz Domanski Date: Tue, 24 Oct 2017 10:56:28 +0200 Subject: [PATCH 04/16] WRONG_ARGUMENTS_ORDER - SVACE related fixes Change-Id: I873b75218372ea4e3396797c99a3e887701f818d --- ssflib/dep/uci/source/uci_api.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ssflib/dep/uci/source/uci_api.c b/ssflib/dep/uci/source/uci_api.c index 9ee995a..7989065 100644 --- a/ssflib/dep/uci/source/uci_api.c +++ b/ssflib/dep/uci/source/uci_api.c @@ -289,7 +289,7 @@ int uci_se_decrypt_oneblock(UCI_HANDLE oh, unsigned char *plain_text, if (cipher_text == NULL || plain_text == NULL || user_key == NULL) { return UCI_ERROR; } - return cryptocore_se_decrypt_oneblock(oh, cipher_text, plain_text, user_key); + return cryptocore_se_decrypt_oneblock(oh, plain_text, cipher_text, user_key); } int uci_wbse_init(UCI_HANDLE oh, int flag, unsigned char *key, -- 2.7.4 From df52dcdecc32ad1d076c81392cf48735521a4aa4 Mon Sep 17 00:00:00 2001 From: Jaroslaw Pelczar Date: Tue, 24 Oct 2017 11:12:33 +0200 Subject: [PATCH 05/16] disconnectfromServer: fix socket fd validity condition Change-Id: I71e566e7c791cc5dd10724210a477a20f88ba5ad Signed-off-by: Jaroslaw Pelczar --- TEECLib/src/teec_connection.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TEECLib/src/teec_connection.c b/TEECLib/src/teec_connection.c index af623ea..5f51621 100644 --- a/TEECLib/src/teec_connection.c +++ b/TEECLib/src/teec_connection.c @@ -86,7 +86,7 @@ void disconnectfromServer(int32_t serverSocket) { int32_t result; LOGD(TEEC_LIB, "Entry"); - if (serverSocket > 0) { + if (serverSocket >= 0) { // shutdown the socket result = shutdown(serverSocket, SHUT_WR); if (result != 0) -- 2.7.4 From 52e4d5015165757b24bda0d728e56bb6dcbc7fb5 Mon Sep 17 00:00:00 2001 From: Jaroslaw Pelczar Date: Tue, 24 Oct 2017 11:34:05 +0200 Subject: [PATCH 06/16] Fix error checking on msgctl() Change-Id: I53fcdb05c3567e12b42a2ad7a11536e875f972cf Signed-off-by: Jaroslaw Pelczar --- osal/OsaQueue.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/osal/OsaQueue.c b/osal/OsaQueue.c index c1b5c16..ec9cc1f 100644 --- a/osal/OsaQueue.c +++ b/osal/OsaQueue.c @@ -449,7 +449,11 @@ int OsaQueueSetinfo(unsigned int uiQid, void* pvBuf) { data_t = (struct QueueInfo*)pvBuf; - msgctl((int)uiQid, IPC_STAT, &buf); + if(msgctl((int)uiQid, IPC_STAT, &buf) < 0) { + perror("In OsaQueueGetinfo() : msgctl: msgctl failed"); + return ((int)errno); + } + buf.msg_qbytes = data_t->maxlen; if (msgctl((int)uiQid, IPC_SET, &buf) < 0) { -- 2.7.4 From e9832deb061f5bfc0d1ad42130cbbeadcfad1297 Mon Sep 17 00:00:00 2001 From: Igor Kotrasinski Date: Tue, 24 Oct 2017 11:06:53 +0200 Subject: [PATCH 07/16] Fix InitContextData passed by value Change-Id: I4ff306def7a002c111a23cbd1bf17ea3d9c863dd Signed-off-by: Igor Kotrasinski --- simulatordaemon/inc/ClientCommands/CommandInitContext.h | 2 +- simulatordaemon/src/ClientCommands/CommandInitContext.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/simulatordaemon/inc/ClientCommands/CommandInitContext.h b/simulatordaemon/inc/ClientCommands/CommandInitContext.h index 7b1435f..caf8f45 100644 --- a/simulatordaemon/inc/ClientCommands/CommandInitContext.h +++ b/simulatordaemon/inc/ClientCommands/CommandInitContext.h @@ -36,7 +36,7 @@ class CommandInitContext: public CommandBase { public: InitContextData data; - CommandInitContext(InitContextData data, TEEContext *TEECtx); + CommandInitContext(InitContextData &data, TEEContext *TEECtx); void execute(); virtual ~CommandInitContext(); }; diff --git a/simulatordaemon/src/ClientCommands/CommandInitContext.cpp b/simulatordaemon/src/ClientCommands/CommandInitContext.cpp index 12ba4b5..4ccbf09 100644 --- a/simulatordaemon/src/ClientCommands/CommandInitContext.cpp +++ b/simulatordaemon/src/ClientCommands/CommandInitContext.cpp @@ -35,7 +35,7 @@ * @param data received from TEECLib * @param TEECtx object associated with command */ -CommandInitContext::CommandInitContext(InitContextData data, TEEContext *TEECtx) : +CommandInitContext::CommandInitContext(InitContextData &data, TEEContext *TEECtx) : CommandBase(TEECtx) { this->data = data; } -- 2.7.4 From 4e4cef67b64cb4191a4949a41a5d439fc65fa4e8 Mon Sep 17 00:00:00 2001 From: Igor Kotrasinski Date: Tue, 24 Oct 2017 11:41:51 +0200 Subject: [PATCH 08/16] Rework char to hex conversion Remove unnecessary 22-case switch. Change-Id: Id9b07c7893b498642032bee24edd5a7d857718aa Signed-off-by: Igor Kotrasinski --- ssflib/dep/cryptocore/source/base/cc_bignum.c | 110 ++++---------------------- 1 file changed, 15 insertions(+), 95 deletions(-) diff --git a/ssflib/dep/cryptocore/source/base/cc_bignum.c b/ssflib/dep/cryptocore/source/base/cc_bignum.c index 3d4a1b6..185b03c 100644 --- a/ssflib/dep/cryptocore/source/base/cc_bignum.c +++ b/ssflib/dep/cryptocore/source/base/cc_bignum.c @@ -2875,102 +2875,22 @@ int SDRM_HEX2BN(cc_u8* pbSrc, SDRM_BIG_NUM *BN_Dst) { for(j = (BN_Dst->Length * SDRM_SIZE_BLOCK) - (i * SDRM_SIZE_BLOCK) - SDRM_SIZE_BLOCK; j < (BN_Dst->Length * SDRM_SIZE_BLOCK) - (i * SDRM_SIZE_BLOCK) ; j++) { - switch(bufferHex[j]) - { - case '0': - BN_Dst->pData[i] = BN_Dst->pData[i] << 4; - //BN_Dst->pData[i] |= 0x0; Unnecessary - break; - case '1': - BN_Dst->pData[i] = BN_Dst->pData[i] << 4; - BN_Dst->pData[i] |= 0x1; - break; - case '2': - BN_Dst->pData[i] = BN_Dst->pData[i] << 4; - BN_Dst->pData[i] |= 0x2; - break; - case '3': - BN_Dst->pData[i] = BN_Dst->pData[i] << 4; - BN_Dst->pData[i] |= 0x3; - break; - case '4': - BN_Dst->pData[i] = BN_Dst->pData[i] << 4; - BN_Dst->pData[i] |= 0x4; - break; - case '5': - BN_Dst->pData[i] = BN_Dst->pData[i] << 4; - BN_Dst->pData[i] |= 0x5; - break; - case '6': - BN_Dst->pData[i] = BN_Dst->pData[i] << 4; - BN_Dst->pData[i] |= 0x6; - break; - case '7': - BN_Dst->pData[i] = BN_Dst->pData[i] << 4; - BN_Dst->pData[i] |= 0x7; - break; - case '8': - BN_Dst->pData[i] = BN_Dst->pData[i] << 4; - BN_Dst->pData[i] |= 0x8; - break; - case '9': - BN_Dst->pData[i] = BN_Dst->pData[i] << 4; - BN_Dst->pData[i] |= 0x9; - break; - case 'a': - BN_Dst->pData[i] = BN_Dst->pData[i] << 4; - BN_Dst->pData[i] |= 0xa; - break; - case 'A': - BN_Dst->pData[i] = BN_Dst->pData[i] << 4; - BN_Dst->pData[i] |= 0xa; - break; - case 'b': - BN_Dst->pData[i] = BN_Dst->pData[i] << 4; - BN_Dst->pData[i] |= 0xb; - break; - case 'B': - BN_Dst->pData[i] = BN_Dst->pData[i] << 4; - BN_Dst->pData[i] |= 0xb; - break; - case 'c': - BN_Dst->pData[i] = BN_Dst->pData[i] << 4; - BN_Dst->pData[i] |= 0xc; - break; - case 'C': - BN_Dst->pData[i] = BN_Dst->pData[i] << 4; - BN_Dst->pData[i] |= 0xc; - break; - case 'd': - BN_Dst->pData[i] = BN_Dst->pData[i] << 4; - BN_Dst->pData[i] |= 0xd; - break; - case 'D': - BN_Dst->pData[i] = BN_Dst->pData[i] << 4; - BN_Dst->pData[i] |= 0xd; - break; - case 'e': - BN_Dst->pData[i] = BN_Dst->pData[i] << 4; - BN_Dst->pData[i] |= 0xe; - break; - case 'E': - BN_Dst->pData[i] = BN_Dst->pData[i] << 4; - BN_Dst->pData[i] |= 0xe; - break; - case 'f': - BN_Dst->pData[i] = BN_Dst->pData[i] << 4; - BN_Dst->pData[i] |= 0xf; - break; - case 'F': - BN_Dst->pData[i] = BN_Dst->pData[i] << 4; - BN_Dst->pData[i] |= 0xf; - break; - default: - { - free(bufferHex); - return CRYPTO_INVALID_ARGUMENT; - } + char c = bufferHex[j]; + int c_int; + + if (c >= '0' && c <= '9') { + c_int = c - '0'; + } else if (c >= 'a' && c <= 'f') { + c_int = c - 'a' + 10; + } else if (c >= 'A' && c <= 'F') { + c_int = c - 'A' + 10; + } else { + free(bufferHex); + return CRYPTO_INVALID_ARGUMENT; } + + BN_Dst->pData[i] = BN_Dst->pData[i] << 4; + BN_Dst->pData[i] |= c_int; } } -- 2.7.4 From 5937ca9b98a95f1717b9bc935f62c3d064c68d8d Mon Sep 17 00:00:00 2001 From: Krzysztof Dynowski Date: Tue, 24 Oct 2017 12:38:51 +0200 Subject: [PATCH 09/16] SVACE: fix NO_CATCH Change-Id: I91ebb7616216d26f68599513c257fad26a272f5b --- simulatordaemon/src/SimulatorDaemon.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/simulatordaemon/src/SimulatorDaemon.cpp b/simulatordaemon/src/SimulatorDaemon.cpp index 56160b3..f5928f1 100644 --- a/simulatordaemon/src/SimulatorDaemon.cpp +++ b/simulatordaemon/src/SimulatorDaemon.cpp @@ -125,6 +125,8 @@ int main() { } catch (std::exception& e) { syslog(LOG_ERR | LOG_USER, "Exception: %s", e.what()); LOGE(SIM_DAEMON, "Exception: %s", e.what()); + } catch (...) { + LOGE(SIM_DAEMON, "Unknown exception in SimulatorDaemon"); } stopServer(ioService::getInstance()); return result; -- 2.7.4 From 77cc0165f1fdc6bdb978c662eaace51c45d7c092 Mon Sep 17 00:00:00 2001 From: Tomasz Swierczek Date: Tue, 24 Oct 2017 13:00:01 +0200 Subject: [PATCH 10/16] Fix coding style in C files Change-Id: I5855f086706db38236e09f6fca7d7314339bead6 --- TEECLib/src/teec_api.c | 647 ++++-- TEECLib/src/teec_connection.c | 290 ++- helloworld/host/main.c | 9 +- helloworld/ta/hello_world.c | 21 +- log/log.c | 208 +- osal/OsaCommon.c | 120 +- osal/OsaIpc.c | 129 +- osal/OsaQueue.c | 120 +- osal/OsaSem.c | 225 +- osal/OsaSignal.c | 49 +- osal/OsaTask.c | 143 +- ssflib/dep/cryptocore/include/base/cc_des.h | 4 +- ssflib/dep/cryptocore/include/drm_macro.h | 4 +- ssflib/dep/cryptocore/source/CC_API.c | 792 ++++--- ssflib/dep/cryptocore/source/base/cc_ANSI_x931.c | 46 +- ssflib/dep/cryptocore/source/base/cc_aes.c | 2345 ++++++++++--------- ssflib/dep/cryptocore/source/base/cc_bignum.c | 2357 +++++++++----------- ssflib/dep/cryptocore/source/base/cc_des.c | 101 +- ssflib/dep/cryptocore/source/base/cc_ecc.c | 1028 ++++----- ssflib/dep/cryptocore/source/base/cc_fast_math.c | 618 +++-- ssflib/dep/cryptocore/source/base/cc_hash.c | 383 ++-- ssflib/dep/cryptocore/source/base/cc_md5.c | 230 +- ssflib/dep/cryptocore/source/base/cc_moo.c | 668 +++--- ssflib/dep/cryptocore/source/base/cc_pkcs1_v21.c | 823 ++++--- ssflib/dep/cryptocore/source/base/cc_rc4.c | 69 +- ssflib/dep/cryptocore/source/base/cc_sha1.c | 634 +++--- ssflib/dep/cryptocore/source/base/cc_sha2.c | 585 +++-- ssflib/dep/cryptocore/source/base/cc_snow2.c | 162 +- ssflib/dep/cryptocore/source/middle/cc_cmac.c | 206 +- ssflib/dep/cryptocore/source/middle/cc_dh.c | 192 +- ssflib/dep/cryptocore/source/middle/cc_dsa.c | 500 ++--- ssflib/dep/cryptocore/source/middle/cc_ecdh.c | 110 +- ssflib/dep/cryptocore/source/middle/cc_ecdsa.c | 528 +++-- ssflib/dep/cryptocore/source/middle/cc_hmac.c | 621 +++--- ssflib/dep/cryptocore/source/middle/cc_rng.c | 39 +- ssflib/dep/cryptocore/source/middle/cc_rsa.c | 1706 +++++++------- ssflib/dep/cryptocore/source/middle/cc_symmetric.c | 2229 +++++++++--------- ssflib/dep/cryptocore/source/middle/cc_tdes.c | 91 +- ssflib/dep/uci/source/uci_aes_xcbc_mac.c | 60 +- ssflib/dep/uci/source/uci_api.c | 686 +++--- ssflib/dep/uci/source/uci_cryptocore.c | 1034 +++++---- ssflib/dep/uci/source/uci_hwcrypto.c | 1037 +++++---- 42 files changed, 10938 insertions(+), 10911 deletions(-) diff --git a/TEECLib/src/teec_api.c b/TEECLib/src/teec_api.c index 8c18e21..a3c0c71 100644 --- a/TEECLib/src/teec_api.c +++ b/TEECLib/src/teec_api.c @@ -36,12 +36,12 @@ /*----------------------------------------------------------------------------- * MACROS *-----------------------------------------------------------------------------*/ -#define PAGE_SIZE 0x1000 -#define PAGE_MASK (~(PAGE_SIZE - 1)) +#define PAGE_SIZE 0x1000 +#define PAGE_MASK (~(PAGE_SIZE - 1)) -#define SHM_MAX_ID INT32_MAX -#define SHM_NAME_TEMPLATE "/teec_shm%d" -#define SHM_FILE_MODE 0660 +#define SHM_MAX_ID INT32_MAX +#define SHM_NAME_TEMPLATE "/teec_shm%d" +#define SHM_FILE_MODE 0660 /*----------------------------------------------------------------------------- * Globals *-----------------------------------------------------------------------------*/ @@ -51,7 +51,8 @@ typedef struct sTEEC_ContextList { TEEC_Context *context; } TEEC_ContextList; -LIST_HEAD(context_listhead, sTEEC_ContextList) context_list = LIST_HEAD_INITIALIZER(NULL); +LIST_HEAD(context_listhead, + sTEEC_ContextList) context_list = LIST_HEAD_INITIALIZER(NULL); static pthread_rwlock_t context_list_lock = PTHREAD_RWLOCK_INITIALIZER; @@ -70,11 +71,11 @@ static uint32_t alignSize(uint32_t size) { uint32_t retSize = size; - if (retSize < PAGE_SIZE) { + if (retSize < PAGE_SIZE) retSize = PAGE_SIZE; - } else if (retSize & (PAGE_SIZE - 1)) { + + else if (retSize & (PAGE_SIZE - 1)) retSize = (retSize & ~(PAGE_SIZE - 1)) + PAGE_SIZE; - } retSize = (retSize + (PAGE_SIZE - 1)) & PAGE_MASK; @@ -90,9 +91,10 @@ static uint32_t alignSize(uint32_t size) * -1 on failure * ===================================================================================== */ -static int32_t allocateSharedMemory(TEEC_SharedMemory *shm) { +static int32_t allocateSharedMemory(TEEC_SharedMemory *shm) +{ LOGD(TEEC_LIB, "Entry"); - TEEC_SharedMemoryImp* sharedMem_imp = (TEEC_SharedMemoryImp*)shm->imp; + TEEC_SharedMemoryImp *sharedMem_imp = (TEEC_SharedMemoryImp *)shm->imp; int32_t memKey = 0; uint32_t size = shm->size; char shm_name[NAME_MAX]; @@ -101,26 +103,32 @@ static int32_t allocateSharedMemory(TEEC_SharedMemory *shm) { do { res = snprintf(shm_name, sizeof(shm_name), SHM_NAME_TEMPLATE, memKey); + if (res == sizeof(shm_name)) { LOGE(TEEC_LIB, "the shm object name is too long"); return TEEC_ERROR_GENERIC; } fd_shm = shm_open(shm_name, O_RDWR | O_CREAT | O_EXCL, SHM_FILE_MODE); + if (fd_shm >= 0) { res = fchmod(fd_shm, SHM_FILE_MODE); + if (res == -1) { close(fd_shm); shm_unlink(shm_name); - LOGE(TEEC_LIB, "Cannot change permission of the %s shared memory file, error: %s", - shm_name, strerror(errno)); + LOGE(TEEC_LIB, + "Cannot change permission of the %s shared memory file, error: %s", + shm_name, strerror(errno)); return TEEC_ERROR_GENERIC; } + break; } if (errno != EEXIST) { - LOGE(TEEC_LIB, "Cannot create shared memory object, error: %s", strerror(errno)); + LOGE(TEEC_LIB, "Cannot create shared memory object, error: %s", + strerror(errno)); return TEEC_ERROR_GENERIC; } @@ -141,7 +149,9 @@ static int32_t allocateSharedMemory(TEEC_SharedMemory *shm) { return TEEC_ERROR_OUT_OF_MEMORY; } - shm->buffer = (void *) mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd_shm, 0); + shm->buffer = (void *) mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, + fd_shm, 0); + if (shm->buffer == MAP_FAILED) { close(fd_shm); shm_unlink(shm_name); @@ -168,26 +178,30 @@ static int32_t allocateSharedMemory(TEEC_SharedMemory *shm) { * Parameters: shm - TEEC_SharedMemory type of data to free memory * ===================================================================================== */ -static void freeSharedMemory(TEEC_SharedMemory *shm) { +static void freeSharedMemory(TEEC_SharedMemory *shm) +{ LOGD(TEEC_LIB, "Entry"); char shm_name[NAME_MAX]; int ret; - TEEC_SharedMemoryImp* sharedMem_imp = (TEEC_SharedMemoryImp*)shm->imp; + TEEC_SharedMemoryImp *sharedMem_imp = (TEEC_SharedMemoryImp *)shm->imp; if (munmap(sharedMem_imp->allocPtr, sharedMem_imp->size) == -1) { LOGE(TEEC_LIB, "munmap failed, error: %s", strerror(errno)); return; } - ret = snprintf(shm_name, sizeof(shm_name), SHM_NAME_TEMPLATE, sharedMem_imp->shmKey); + ret = snprintf(shm_name, sizeof(shm_name), SHM_NAME_TEMPLATE, + sharedMem_imp->shmKey); + if (ret == sizeof(shm_name)) { LOGE(TEE_STUB, "the shm object name is too long"); return; } if (shm_unlink(shm_name) == -1) { - LOGE(TEE_STUB, "shm_unlink failed for %s, error: %s", shm_name, strerror(errno)); + LOGE(TEE_STUB, "shm_unlink failed for %s, error: %s", shm_name, + strerror(errno)); return; } @@ -203,7 +217,8 @@ static void freeSharedMemory(TEEC_SharedMemory *shm) { * Parameters: shm - TEEC_SharedMemory type of data to allocate memory and update * ===================================================================================== */ -static TEEC_Result getMemoryKey(TEEC_SharedMemory *shm) { +static TEEC_Result getMemoryKey(TEEC_SharedMemory *shm) +{ TEEC_Result result; LOGD(TEEC_LIB, "Entry"); TEEC_SharedMemory sharedmem; @@ -214,9 +229,10 @@ static TEEC_Result getMemoryKey(TEEC_SharedMemory *shm) { // Allocate shared memory and get the shared memory key result = allocateSharedMemory(&sharedmem); + // Copy the input buffer data to shared buffer if ((result == TEEC_SUCCESS) && ((shm->flags && TEEC_MEM_INPUT) != 0)) - memcpy(sharedmem.buffer, shm->buffer, shm->size); + memcpy(sharedmem.buffer, shm->buffer, shm->size); return result; } @@ -230,7 +246,8 @@ static TEEC_Result getMemoryKey(TEEC_SharedMemory *shm) { * -1 on failure * ===================================================================================== */ -static uint32_t checkContext(TEEC_Context * context) { +static uint32_t checkContext(TEEC_Context *context) +{ LOGD(TEEC_LIB, "Entry"); TEEC_ContextList *pContext; @@ -240,8 +257,7 @@ static uint32_t checkContext(TEEC_Context * context) { * contexts */ pthread_rwlock_wrlock(&context_list_lock); - LIST_FOREACH(pContext, &context_list, list) - { + LIST_FOREACH(pContext, &context_list, list) { if (pContext->context == context) { found = 1; break; @@ -264,8 +280,9 @@ static uint32_t checkContext(TEEC_Context * context) { * ===================================================================================== */ static TEEC_Result preProcessOperation(TEEC_Session *session, - TEEC_Operation *operation, OperationData *op, - TEEC_SharedMemory *tmpSharedMem[4]) { + TEEC_Operation *operation, OperationData *op, + TEEC_SharedMemory *tmpSharedMem[4]) +{ TEEC_Context *context = NULL; LOGD(TEEC_LIB, "Entry"); @@ -276,138 +293,131 @@ static TEEC_Result preProcessOperation(TEEC_Session *session, // Check if Session is valid if (session) - context = ((TEEC_SessionImp*)session->imp)->context; + context = ((TEEC_SessionImp *)session->imp)->context; else return TEEC_ERROR_BAD_PARAMETERS; + // Check if output Operation structure is valid - if (!op) { + if (!op) return TEEC_ERROR_GENERIC; - } + // Initialize output Operation structure paramtypes to NONE op->paramTypes = (TEE_PARAM_TYPE_NONE << 24) | (TEE_PARAM_TYPE_NONE << 16) - | (TEE_PARAM_TYPE_NONE << 8) | TEE_PARAM_TYPE_NONE; + | (TEE_PARAM_TYPE_NONE << 8) | TEE_PARAM_TYPE_NONE; /* Process params in output Operation structure based on input Operation * structure */ for (i = 0; i < 4; i++) { type = ((operation->paramTypes) >> (8 * i)) & 0x7f; + switch (type) { - case TEEC_NONE: - op->paramTypes |= TEE_PARAM_TYPE_NONE << (8 * i); - break; - case TEEC_VALUE_INPUT: - case TEEC_VALUE_OUTPUT: - case TEEC_VALUE_INOUT: - /* - * TEEC_VALUE_* and TEE_PARAM_TYPE_VALUE_* constants have - * equal values according to TEE Internal API and Client API - * specifications, so assign them without modifications. - */ - op->paramTypes |= type << (8 * i); - memcpy(&op->params[i].value, &operation->params[i].value, - sizeof(TEEC_Value)); - break; - case TEEC_MEMREF_TEMP_INPUT: - case TEEC_MEMREF_TEMP_OUTPUT: - case TEEC_MEMREF_TEMP_INOUT: - /* - * TEEC_MEMREF_TEMP_* and TEE_PARAM_TYPE_MEMREF_* constants have - * equal values according to TEE Internal API and Client API - * specifications, so assign them without modifications. - */ - op->paramTypes |= type << (8 * i); - if (!tmpSharedMem[i]) { - tmpSharedMem[i] = (TEEC_SharedMemory*)OsaMalloc( - sizeof(TEEC_SharedMemory)); - tmpSharedMem[i]->size = operation->params[i].tmpref.size; - tmpSharedMem[i]->buffer = operation->params[i].tmpref.buffer; - if (type == TEEC_MEMREF_TEMP_INPUT) { - tmpSharedMem[i]->flags = TEEC_MEM_INPUT; - } else if (type == TEEC_MEMREF_TEMP_OUTPUT) { - tmpSharedMem[i]->flags = TEEC_MEM_OUTPUT; - } else if (type == TEEC_MEMREF_TEMP_INOUT) { - tmpSharedMem[i]->flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT; - } + case TEEC_NONE: + op->paramTypes |= TEE_PARAM_TYPE_NONE << (8 * i); + break; - result = TEEC_RegisterSharedMemory( - ((TEEC_SessionImp*)session->imp)->context, tmpSharedMem[i]); - if (result != TEEC_SUCCESS) { - for (i = 0; i < 4; i++) { - if (tmpSharedMem[i]) { - OsaFree(tmpSharedMem[i]); - tmpSharedMem[i] = NULL; - } - } - return result; - } + case TEEC_VALUE_INPUT: + case TEEC_VALUE_OUTPUT: + case TEEC_VALUE_INOUT: + /* + * TEEC_VALUE_* and TEE_PARAM_TYPE_VALUE_* constants have + * equal values according to TEE Internal API and Client API + * specifications, so assign them without modifications. + */ + op->paramTypes |= type << (8 * i); + memcpy(&op->params[i].value, &operation->params[i].value, + sizeof(TEEC_Value)); + break; - if (type & TEEC_MEMREF_TEMP_INPUT) { - memcpy(((TEEC_SharedMemoryImp*)tmpSharedMem[i]->imp)->allocPtr, - tmpSharedMem[i]->buffer, tmpSharedMem[i]->size); - } - } - op->params[i].mem.size = tmpSharedMem[i]->size; - op->params[i].mem.offset = 0; - op->params[i].mem.shmKey = ((TEEC_SharedMemoryImp*)tmpSharedMem[i]->imp) - ->shmKey; - break; - case TEEC_MEMREF_WHOLE: - op->paramTypes |= TEE_PARAM_TYPE_MEMREF_INOUT << (8 * i); - memref = &operation->params[i].memref; - if ((NULL == memref) || (NULL == memref->parent) - || (((TEEC_SharedMemoryImp*)memref->parent->imp)->context->imp - != context->imp)) { + case TEEC_MEMREF_TEMP_INPUT: + case TEEC_MEMREF_TEMP_OUTPUT: + case TEEC_MEMREF_TEMP_INOUT: + /* + * TEEC_MEMREF_TEMP_* and TEE_PARAM_TYPE_MEMREF_* constants have + * equal values according to TEE Internal API and Client API + * specifications, so assign them without modifications. + */ + op->paramTypes |= type << (8 * i); + + if (!tmpSharedMem[i]) { + tmpSharedMem[i] = (TEEC_SharedMemory *)OsaMalloc( + sizeof(TEEC_SharedMemory)); + tmpSharedMem[i]->size = operation->params[i].tmpref.size; + tmpSharedMem[i]->buffer = operation->params[i].tmpref.buffer; + + if (type == TEEC_MEMREF_TEMP_INPUT) + tmpSharedMem[i]->flags = TEEC_MEM_INPUT; + + else if (type == TEEC_MEMREF_TEMP_OUTPUT) + tmpSharedMem[i]->flags = TEEC_MEM_OUTPUT; + + else if (type == TEEC_MEMREF_TEMP_INOUT) + tmpSharedMem[i]->flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT; + + result = TEEC_RegisterSharedMemory( + ((TEEC_SessionImp *)session->imp)->context, tmpSharedMem[i]); + + if (result != TEEC_SUCCESS) { for (i = 0; i < 4; i++) { if (tmpSharedMem[i]) { - TEEC_ReleaseSharedMemory(tmpSharedMem[i]); OsaFree(tmpSharedMem[i]); tmpSharedMem[i] = NULL; } } - LOGE(TEEC_LIB, "Bad parameters"); - return TEEC_ERROR_BAD_PARAMETERS; + + return result; } - memref_imp = (TEEC_SharedMemoryImp*)memref->parent->imp; - op->params[i].mem.offset = 0; - op->params[i].mem.size = memref->parent->size; - op->params[i].mem.shmKey = memref_imp->shmKey; - if (memref->parent->buffer != memref_imp->allocPtr) { - memcpy(memref_imp->allocPtr, memref->parent->buffer, memref->size); + + if (type & TEEC_MEMREF_TEMP_INPUT) { + memcpy(((TEEC_SharedMemoryImp *)tmpSharedMem[i]->imp)->allocPtr, + tmpSharedMem[i]->buffer, tmpSharedMem[i]->size); } - break; - case TEEC_MEMREF_PARTIAL_INPUT: - case TEEC_MEMREF_PARTIAL_OUTPUT: - case TEEC_MEMREF_PARTIAL_INOUT: - op->paramTypes |= (type + TEE_PARAM_TYPE_MEMREF_INPUT - - TEEC_MEMREF_PARTIAL_INPUT) << (8 * i); - memref = &operation->params[i].memref; - if ((NULL == memref) || (NULL == memref->parent) - || (((TEEC_SharedMemoryImp*)memref->parent->imp)->context->imp - != context->imp)) { - for (i = 0; i < 4; i++) { - if (tmpSharedMem[i]) { - TEEC_ReleaseSharedMemory(tmpSharedMem[i]); - OsaFree(tmpSharedMem[i]); - tmpSharedMem[i] = NULL; - } + } + + op->params[i].mem.size = tmpSharedMem[i]->size; + op->params[i].mem.offset = 0; + op->params[i].mem.shmKey = ((TEEC_SharedMemoryImp *)tmpSharedMem[i]->imp) + ->shmKey; + break; + + case TEEC_MEMREF_WHOLE: + op->paramTypes |= TEE_PARAM_TYPE_MEMREF_INOUT << (8 * i); + memref = &operation->params[i].memref; + + if ((NULL == memref) || (NULL == memref->parent) + || (((TEEC_SharedMemoryImp *)memref->parent->imp)->context->imp + != context->imp)) { + for (i = 0; i < 4; i++) { + if (tmpSharedMem[i]) { + TEEC_ReleaseSharedMemory(tmpSharedMem[i]); + OsaFree(tmpSharedMem[i]); + tmpSharedMem[i] = NULL; } - LOGE(TEEC_LIB, "Bad parameters"); - return TEEC_ERROR_BAD_PARAMETERS; - } - memref_imp = (TEEC_SharedMemoryImp*)memref->parent->imp; - op->params[i].mem.size = memref->size; - op->params[i].mem.offset = memref->offset; - op->params[i].mem.shmKey = memref_imp->shmKey; - - if (memref->parent->buffer != memref_imp->allocPtr) { - if ((type == TEEC_MEMREF_PARTIAL_INPUT) - || (type == TEEC_MEMREF_PARTIAL_INOUT)) - memcpy((uint8_t*)(memref_imp->allocPtr) + memref->offset, - (uint8_t*)(memref->parent->buffer) + memref->offset, - memref->size); } - break; - default: + + LOGE(TEEC_LIB, "Bad parameters"); + return TEEC_ERROR_BAD_PARAMETERS; + } + + memref_imp = (TEEC_SharedMemoryImp *)memref->parent->imp; + op->params[i].mem.offset = 0; + op->params[i].mem.size = memref->parent->size; + op->params[i].mem.shmKey = memref_imp->shmKey; + + if (memref->parent->buffer != memref_imp->allocPtr) + memcpy(memref_imp->allocPtr, memref->parent->buffer, memref->size); + + break; + + case TEEC_MEMREF_PARTIAL_INPUT: + case TEEC_MEMREF_PARTIAL_OUTPUT: + case TEEC_MEMREF_PARTIAL_INOUT: + op->paramTypes |= (type + TEE_PARAM_TYPE_MEMREF_INPUT + - TEEC_MEMREF_PARTIAL_INPUT) << (8 * i); + memref = &operation->params[i].memref; + + if ((NULL == memref) || (NULL == memref->parent) + || (((TEEC_SharedMemoryImp *)memref->parent->imp)->context->imp + != context->imp)) { for (i = 0; i < 4; i++) { if (tmpSharedMem[i]) { TEEC_ReleaseSharedMemory(tmpSharedMem[i]); @@ -415,9 +425,39 @@ static TEEC_Result preProcessOperation(TEEC_Session *session, tmpSharedMem[i] = NULL; } } + + LOGE(TEEC_LIB, "Bad parameters"); return TEEC_ERROR_BAD_PARAMETERS; + } + + memref_imp = (TEEC_SharedMemoryImp *)memref->parent->imp; + op->params[i].mem.size = memref->size; + op->params[i].mem.offset = memref->offset; + op->params[i].mem.shmKey = memref_imp->shmKey; + + if (memref->parent->buffer != memref_imp->allocPtr) { + if ((type == TEEC_MEMREF_PARTIAL_INPUT) + || (type == TEEC_MEMREF_PARTIAL_INOUT)) + memcpy((uint8_t *)(memref_imp->allocPtr) + memref->offset, + (uint8_t *)(memref->parent->buffer) + memref->offset, + memref->size); + } + + break; + + default: + for (i = 0; i < 4; i++) { + if (tmpSharedMem[i]) { + TEEC_ReleaseSharedMemory(tmpSharedMem[i]); + OsaFree(tmpSharedMem[i]); + tmpSharedMem[i] = NULL; + } + } + + return TEEC_ERROR_BAD_PARAMETERS; } } + return TEEC_SUCCESS; } @@ -432,7 +472,8 @@ static TEEC_Result preProcessOperation(TEEC_Session *session, * ===================================================================================== */ static void postProcessOperation(TEEC_Operation *operation, OperationData *op, - TEEC_SharedMemory *tmpSharedMem[4]) { + TEEC_SharedMemory *tmpSharedMem[4]) +{ LOGD(TEEC_LIB, "Entry"); uint32_t i, type; TEEC_RegisteredMemoryReference *memref; @@ -443,40 +484,47 @@ static void postProcessOperation(TEEC_Operation *operation, OperationData *op, */ for (i = 0; i < 4; i++) { type = ((operation->paramTypes) >> (8 * i)) & 0x7f; + switch (type) { - case TEEC_VALUE_INPUT: - case TEEC_VALUE_OUTPUT: - case TEEC_VALUE_INOUT: - memcpy(&operation->params[i].value, &op->params[i].value, - sizeof(TEEC_Value)); - break; - case TEEC_MEMREF_TEMP_OUTPUT: - case TEEC_MEMREF_TEMP_INOUT: - operation->params[i].tmpref.size = op->params[i].mem.size; - memcpy(operation->params[i].tmpref.buffer, - ((TEEC_SharedMemoryImp*)tmpSharedMem[i]->imp)->allocPtr, - operation->params[i].tmpref.size); - break; - case TEEC_MEMREF_WHOLE: - memref = &operation->params[i].memref; - memref->parent->size = op->params[i].mem.size; - memref_imp = (TEEC_SharedMemoryImp*)memref->parent->imp; - - if (memref->parent->buffer != memref_imp->allocPtr) { - memcpy((uint8_t*)(memref->parent->buffer) + memref->offset, - (uint8_t*)(memref_imp->allocPtr) + memref->offset, memref->size); - } - break; - case TEEC_MEMREF_PARTIAL_OUTPUT: - case TEEC_MEMREF_PARTIAL_INOUT: - memref = &operation->params[i].memref; - memref_imp = (TEEC_SharedMemoryImp*)memref->parent->imp; - memref->size = op->params[i].mem.size; - if (memref->parent->buffer != memref_imp->allocPtr) { - memcpy((uint8_t*)(memref->parent->buffer) + memref->offset, - (uint8_t*)(memref_imp->allocPtr) + memref->offset, memref->size); - } - break; + case TEEC_VALUE_INPUT: + case TEEC_VALUE_OUTPUT: + case TEEC_VALUE_INOUT: + memcpy(&operation->params[i].value, &op->params[i].value, + sizeof(TEEC_Value)); + break; + + case TEEC_MEMREF_TEMP_OUTPUT: + case TEEC_MEMREF_TEMP_INOUT: + operation->params[i].tmpref.size = op->params[i].mem.size; + memcpy(operation->params[i].tmpref.buffer, + ((TEEC_SharedMemoryImp *)tmpSharedMem[i]->imp)->allocPtr, + operation->params[i].tmpref.size); + break; + + case TEEC_MEMREF_WHOLE: + memref = &operation->params[i].memref; + memref->parent->size = op->params[i].mem.size; + memref_imp = (TEEC_SharedMemoryImp *)memref->parent->imp; + + if (memref->parent->buffer != memref_imp->allocPtr) { + memcpy((uint8_t *)(memref->parent->buffer) + memref->offset, + (uint8_t *)(memref_imp->allocPtr) + memref->offset, memref->size); + } + + break; + + case TEEC_MEMREF_PARTIAL_OUTPUT: + case TEEC_MEMREF_PARTIAL_INOUT: + memref = &operation->params[i].memref; + memref_imp = (TEEC_SharedMemoryImp *)memref->parent->imp; + memref->size = op->params[i].mem.size; + + if (memref->parent->buffer != memref_imp->allocPtr) { + memcpy((uint8_t *)(memref->parent->buffer) + memref->offset, + (uint8_t *)(memref_imp->allocPtr) + memref->offset, memref->size); + } + + break; } } } @@ -498,11 +546,12 @@ static void postProcessOperation(TEEC_Operation *operation, OperationData *op, * ===================================================================================== */ -TEEC_Result TEEC_InitializeContext(const char *name, TEEC_Context *context) { +TEEC_Result TEEC_InitializeContext(const char *name, TEEC_Context *context) +{ LOGD(TEEC_LIB, "Entry"); TEEC_Result result = TEEC_SUCCESS; - TEEC_ContextImp* context_imp = NULL; - TEEC_ContextList* pContext = NULL; + TEEC_ContextImp *context_imp = NULL; + TEEC_ContextList *pContext = NULL; InitContextData ctx; // Check if the context is valid @@ -510,13 +559,16 @@ TEEC_Result TEEC_InitializeContext(const char *name, TEEC_Context *context) { LOGE(TEEC_LIB, "NULL Context"); return TEEC_ERROR_BAD_PARAMETERS; } + // Initialize Context imp structure - context->imp = (TEEC_ContextImp*)OsaMalloc(sizeof(TEEC_ContextImp)); - context_imp = (TEEC_ContextImp*)context->imp; + context->imp = (TEEC_ContextImp *)OsaMalloc(sizeof(TEEC_ContextImp)); + context_imp = (TEEC_ContextImp *)context->imp; + if (!context_imp) { LOGE(TEEC_LIB, "context_imp malloc failed"); return TEEC_ERROR_OUT_OF_MEMORY; } + memset(context_imp, 0x00, sizeof(TEEC_ContextImp)); // Initialize InitContextData structure to be sent to Simulator Daemon @@ -526,28 +578,32 @@ TEEC_Result TEEC_InitializeContext(const char *name, TEEC_Context *context) { * to zero else update it accordingly, also update TEEName accordingly */ ctx.nameLength = (name == NULL) ? 0 : strlen(name) + 1; + if (ctx.nameLength > MAX_CONTEXT_NAME_LEN) { OsaFree(context_imp); context->imp = NULL; LOGE(TEEC_LIB, "TEE name length exceeding"); return TEEC_ERROR_BAD_PARAMETERS; } + if (name) strncpy(ctx.TEEName, name, ctx.nameLength); // Connect to Simulator Daemon as a client context_imp->sockfd = connecttoServer(); + if (context_imp->sockfd == -1) { OsaFree(context_imp); context->imp = NULL; LOGE(TEEC_LIB, "Unable to connect to Simulator daemon"); return TEEC_ERROR_GENERIC; } + ctx.returnValue = TEEC_SUCCESS; // Send the command and data to Simulator Daemon through the socket pthread_mutex_lock(&context_imp->lock); result = sendCommand(context_imp->sockfd, INITIALIZE_CONTEXT, &ctx, - sizeof(InitContextData)); + sizeof(InitContextData)); pthread_mutex_unlock(&context_imp->lock); if (result != TEEC_SUCCESS) { // Communication Failure @@ -574,11 +630,13 @@ TEEC_Result TEEC_InitializeContext(const char *name, TEEC_Context *context) { context_imp->contextID = ctx.contextID; // Add the context in the Context list - pContext = (TEEC_ContextList*)OsaMalloc(sizeof(TEEC_ContextList)); + pContext = (TEEC_ContextList *)OsaMalloc(sizeof(TEEC_ContextList)); + if (!pContext) { LOGE(TEEC_LIB, "pContext malloc failed"); return TEEC_ERROR_OUT_OF_MEMORY; } + pContext->context = context; pthread_rwlock_wrlock(&context_list_lock); LIST_INSERT_HEAD(&context_list, pContext, list); @@ -595,7 +653,8 @@ TEEC_Result TEEC_InitializeContext(const char *name, TEEC_Context *context) { * finalized. * ===================================================================================== */ -void TEEC_FinalizeContext(TEEC_Context *context) { +void TEEC_FinalizeContext(TEEC_Context *context) +{ LOGD(TEEC_LIB, "Entry"); TEEC_Result result = TEEC_SUCCESS; FinalizeContextData ctx; @@ -607,10 +666,10 @@ void TEEC_FinalizeContext(TEEC_Context *context) { LOGE(TEEC_LIB, "NULL context"); return; } + // Remove the Context from the list pthread_rwlock_wrlock(&context_list_lock); - LIST_FOREACH(pContext, &context_list, list) - { + LIST_FOREACH(pContext, &context_list, list) { if (pContext->context == context) { context_initialized = 1; LIST_REMOVE(pContext, list); @@ -624,8 +683,10 @@ void TEEC_FinalizeContext(TEEC_Context *context) { LOGE(TEEC_LIB, "Invalid Context"); return; } + // Check if the Context imp structure is valid - TEEC_ContextImp* context_imp = (TEEC_ContextImp*)context->imp; + TEEC_ContextImp *context_imp = (TEEC_ContextImp *)context->imp; + if (!context_imp) { LOGE(TEEC_LIB, "NULL context_imp"); return; @@ -638,11 +699,11 @@ void TEEC_FinalizeContext(TEEC_Context *context) { // Send the command and data to Simulator Daemon through the socket pthread_mutex_lock(&context_imp->lock); result = sendCommand(context_imp->sockfd, FINALIZE_CONTEXT, &ctx, - sizeof(FinalizeContextData)); + sizeof(FinalizeContextData)); pthread_mutex_unlock(&context_imp->lock); if (result != TEEC_SUCCESS) // Communication Failure - LOGE(TEEC_LIB, "sendCommand to Simulator Daemon failed"); + LOGE(TEEC_LIB, "sendCommand to Simulator Daemon failed"); // Disconnect from Simulator Daemon disconnectfromServer(context_imp->sockfd); @@ -668,7 +729,8 @@ void TEEC_FinalizeContext(TEEC_Context *context) { * ===================================================================================== */ TEEC_Result TEEC_RegisterSharedMemory(TEEC_Context *context, - TEEC_SharedMemory *sharedMem) { + TEEC_SharedMemory *sharedMem) +{ LOGD(TEEC_LIB, "Entry"); TEEC_Result result = TEEC_SUCCESS; RegSharedMemData regmem; @@ -678,23 +740,28 @@ TEEC_Result TEEC_RegisterSharedMemory(TEEC_Context *context, LOGE(TEEC_LIB, "NULL context"); return TEEC_ERROR_BAD_PARAMETERS; } + // Check if the context is initialized if (!checkContext(context)) { LOGE(TEEC_LIB, "Invalid context"); return TEEC_ERROR_BAD_PARAMETERS; } + // Check if the Context imp structure is valid - TEEC_ContextImp* context_imp = (TEEC_ContextImp*)context->imp; + TEEC_ContextImp *context_imp = (TEEC_ContextImp *)context->imp; + if (!context_imp) { LOGE(TEEC_LIB, "NULL context_imp"); return TEEC_ERROR_BAD_PARAMETERS; } + // Check if the socket is valid if (context_imp->sockfd < 0) { LOGE(TEEC_LIB, "Bad parameter context_imp->sockfd = %d", - context_imp->sockfd); + context_imp->sockfd); return TEEC_ERROR_BAD_PARAMETERS; } + // Check if shared memory pointer is valid if (!sharedMem) { LOGE(TEEC_LIB, "Shared Memory is NULL"); @@ -715,9 +782,10 @@ TEEC_Result TEEC_RegisterSharedMemory(TEEC_Context *context, LOGE(TEEC_LIB, "Shared Memory buffer is NULL"); return TEEC_ERROR_NO_DATA; } + // Check if the Shared memory flags are valid if ((sharedMem->flags == 0) - || (sharedMem->flags > (TEEC_MEM_INPUT | TEEC_MEM_OUTPUT))) { + || (sharedMem->flags > (TEEC_MEM_INPUT | TEEC_MEM_OUTPUT))) { LOGE(TEEC_LIB, "Shared Memory flag is a bad parameter"); return TEEC_ERROR_BAD_PARAMETERS; } @@ -725,12 +793,13 @@ TEEC_Result TEEC_RegisterSharedMemory(TEEC_Context *context, // Initialize RegSharedMemData structure to sent to Simulator Daemon memset(®mem, 0x00, sizeof(RegSharedMemData)); - sharedMem->imp = (TEEC_SharedMemoryImp*)OsaMalloc( - sizeof(TEEC_SharedMemoryImp)); - if (sharedMem->imp == NULL) { + sharedMem->imp = (TEEC_SharedMemoryImp *)OsaMalloc( + sizeof(TEEC_SharedMemoryImp)); + + if (sharedMem->imp == NULL) return TEE_ERROR_OUT_OF_MEMORY; - } - TEEC_SharedMemoryImp* sharedMem_imp = (TEEC_SharedMemoryImp*)sharedMem->imp; + + TEEC_SharedMemoryImp *sharedMem_imp = (TEEC_SharedMemoryImp *)sharedMem->imp; sharedMem_imp->context = context; regmem.contextID = context_imp->contextID; regmem.sharedMem.size = sharedMem->size; @@ -739,23 +808,26 @@ TEEC_Result TEEC_RegisterSharedMemory(TEEC_Context *context, // Generate shared memory key to be shared with TEEStub result = getMemoryKey(sharedMem); + if (result != TEEC_SUCCESS) { // Memory allocation Failure LOGE(TEEC_LIB, "Memory alocation failed"); OsaFree(sharedMem_imp); return result; } + // Check if the obained shared memory is valid if (sharedMem_imp->shmKey == -1) { LOGE(TEEC_LIB, "Failed to get MemoryID"); OsaFree(sharedMem_imp); return TEEC_ERROR_GENERIC; } + regmem.sharedMem.shmKey = sharedMem_imp->shmKey; // Send the command and data to Simulator Daemon through the socket pthread_mutex_lock(&context_imp->lock); result = sendCommand(context_imp->sockfd, REGISTER_SHARED_MEMORY, ®mem, - sizeof(RegSharedMemData)); + sizeof(RegSharedMemData)); pthread_mutex_unlock(&context_imp->lock); if (result != TEEC_SUCCESS) { // Communication Failure @@ -763,12 +835,15 @@ TEEC_Result TEEC_RegisterSharedMemory(TEEC_Context *context, OsaFree(sharedMem_imp); return result; } + result = regmem.returnValue; + if (result != TEEC_SUCCESS) { // Command Failure LOGE(TEEC_LIB, "Simulator Daemon Register Shared Memory returned failure"); OsaFree(sharedMem_imp); return result; } + return result; } @@ -790,10 +865,11 @@ TEEC_Result TEEC_RegisterSharedMemory(TEEC_Context *context, * ===================================================================================== */ TEEC_Result TEEC_AllocateSharedMemory(TEEC_Context *context, - TEEC_SharedMemory *sharedMem) { + TEEC_SharedMemory *sharedMem) +{ LOGD(TEEC_LIB, "Entry"); TEEC_Result result = TEEC_SUCCESS; - TEEC_ContextImp* context_imp; + TEEC_ContextImp *context_imp; RegSharedMemData regmem; // Check if the Context is valid @@ -801,23 +877,28 @@ TEEC_Result TEEC_AllocateSharedMemory(TEEC_Context *context, LOGE(TEEC_LIB, "context is NULL"); return TEEC_ERROR_BAD_PARAMETERS; } + // Check if the Context is initialized if (!checkContext(context)) { LOGE(TEEC_LIB, "context is not found"); return TEEC_ERROR_BAD_PARAMETERS; } + // Check if the Context imp structure is valid - context_imp = (TEEC_ContextImp*)context->imp; + context_imp = (TEEC_ContextImp *)context->imp; + if (!context_imp) { LOGE(TEEC_LIB, "context_imp is not found"); return TEEC_ERROR_BAD_PARAMETERS; } + // Check if the socket is valid if (context_imp->sockfd < 0) { LOGE(TEEC_LIB, "Bad parameter context_imp->sockfd = %d", - context_imp->sockfd); + context_imp->sockfd); return TEEC_ERROR_BAD_PARAMETERS; } + // Check if shared memory pointer is valid if (!sharedMem) { LOGE(TEEC_LIB, "Shared Memory is NULL"); @@ -835,17 +916,19 @@ TEEC_Result TEEC_AllocateSharedMemory(TEEC_Context *context, // Check if the Shared memory flags are valid if ((sharedMem->flags == 0) - || (sharedMem->flags > (TEEC_MEM_INPUT | TEEC_MEM_OUTPUT))) { + || (sharedMem->flags > (TEEC_MEM_INPUT | TEEC_MEM_OUTPUT))) { LOGE(TEEC_LIB, "Shared Memory flag is a bad parameter"); return TEEC_ERROR_BAD_PARAMETERS; } + // Generate Shared Memory imp structure - sharedMem->imp = (TEEC_SharedMemoryImp*)OsaMalloc( - sizeof(TEEC_SharedMemoryImp)); - if (sharedMem->imp == NULL) { + sharedMem->imp = (TEEC_SharedMemoryImp *)OsaMalloc( + sizeof(TEEC_SharedMemoryImp)); + + if (sharedMem->imp == NULL) return TEE_ERROR_OUT_OF_MEMORY; - } - TEEC_SharedMemoryImp* sharedMem_imp = (TEEC_SharedMemoryImp*)sharedMem->imp; + + TEEC_SharedMemoryImp *sharedMem_imp = (TEEC_SharedMemoryImp *)sharedMem->imp; sharedMem->buffer = NULL; sharedMem_imp->context = context; @@ -853,11 +936,13 @@ TEEC_Result TEEC_AllocateSharedMemory(TEEC_Context *context, * TEEStub */ result = allocateSharedMemory(sharedMem); + if (result != TEEC_SUCCESS) { // Memory Allocation Failure LOGE(TEEC_LIB, "allocateSharedMemory failed"); OsaFree(sharedMem_imp); return result; } + // Check if the obtained key is valid if (sharedMem_imp->shmKey == -1) { LOGE(TEEC_LIB, "allocateSharedMemory failed"); @@ -877,7 +962,7 @@ TEEC_Result TEEC_AllocateSharedMemory(TEEC_Context *context, // Send the command and data to Simulator Daemon through the socket pthread_mutex_lock(&context_imp->lock); result = sendCommand(context_imp->sockfd, REGISTER_SHARED_MEMORY, ®mem, - sizeof(RegSharedMemData)); + sizeof(RegSharedMemData)); pthread_mutex_unlock(&context_imp->lock); if (result != TEEC_SUCCESS) { // Communication Failure @@ -886,12 +971,15 @@ TEEC_Result TEEC_AllocateSharedMemory(TEEC_Context *context, OsaFree(sharedMem_imp); return result; } + result = regmem.returnValue; + if (result != TEEC_SUCCESS) { // Command Failure LOGE(TEEC_LIB, "Simulator Daemon Allocate Shared Memory returned failure"); OsaFree(sharedMem_imp); return result; } + return result; } @@ -902,11 +990,12 @@ TEEC_Result TEEC_AllocateSharedMemory(TEEC_Context *context, * Parameters: sharedMem - a pointer to a valid Shared Memory structure * ===================================================================================== */ -void TEEC_ReleaseSharedMemory(TEEC_SharedMemory *sharedMem) { +void TEEC_ReleaseSharedMemory(TEEC_SharedMemory *sharedMem) +{ LOGD(TEEC_LIB, "Entry"); TEEC_Result result = TEEC_SUCCESS; - TEEC_Context* context; - TEEC_ContextImp* context_imp; + TEEC_Context *context; + TEEC_ContextImp *context_imp; RelSharedMemData relmem; // Check if the Shared Memory is valid @@ -914,20 +1003,26 @@ void TEEC_ReleaseSharedMemory(TEEC_SharedMemory *sharedMem) { LOGE(TEEC_LIB, "SharedMem is NULL"); return; } + // Check if the Shared Memory imp structure is valid - TEEC_SharedMemoryImp* sharedMem_imp = (TEEC_SharedMemoryImp*)sharedMem->imp; + TEEC_SharedMemoryImp *sharedMem_imp = (TEEC_SharedMemoryImp *)sharedMem->imp; + if (!sharedMem_imp) { LOGE(TEEC_LIB, "NULL sharedMem_imp"); return; } + // Check if the Context is valid context = sharedMem_imp->context; + if (!context) { LOGE(TEEC_LIB, "context is NULL"); return; } + // Check if the Context imp structure is valid - context_imp = (TEEC_ContextImp*)context->imp; + context_imp = (TEEC_ContextImp *)context->imp; + if (!context_imp) { LOGE(TEEC_LIB, "context_imp is NULL"); return; @@ -944,13 +1039,14 @@ void TEEC_ReleaseSharedMemory(TEEC_SharedMemory *sharedMem) { // Send the command and data to Simulator Daemon through the socket pthread_mutex_lock(&context_imp->lock); result = sendCommand(context_imp->sockfd, RELEASE_SHARED_MEMORY, &relmem, - sizeof(RelSharedMemData)); + sizeof(RelSharedMemData)); pthread_mutex_unlock(&context_imp->lock); if (result != TEEC_SUCCESS) { // Communication Failure LOGE(TEEC_LIB, "sendCommand to Simulator Daemon failed"); return; } + // free Shared Memory freeSharedMemory(sharedMem); OsaFree(sharedMem_imp); @@ -986,9 +1082,10 @@ void TEEC_ReleaseSharedMemory(TEEC_SharedMemory *sharedMem) { * ===================================================================================== */ TEEC_Result TEEC_OpenSession(TEEC_Context *context, TEEC_Session *session, - const TEEC_UUID *destination, uint32_t connectionMethod, - const void *connectionData, TEEC_Operation *operation, - uint32_t *returnOrigin) { + const TEEC_UUID *destination, uint32_t connectionMethod, + const void *connectionData, TEEC_Operation *operation, + uint32_t *returnOrigin) +{ LOGD(TEEC_LIB, "Entry"); TEEC_Result result = TEEC_SUCCESS; @@ -1003,8 +1100,10 @@ TEEC_Result TEEC_OpenSession(TEEC_Context *context, TEEC_Session *session, LOGE(TEEC_LIB, "Invalid input parameters"); return TEEC_ERROR_BAD_PARAMETERS; } + // Check if the context imp is valid - TEEC_ContextImp* context_imp = (TEEC_ContextImp*)context->imp; + TEEC_ContextImp *context_imp = (TEEC_ContextImp *)context->imp; + if (!context_imp) { LOGE(TEEC_LIB, "NULL context_imp"); return TEEC_ERROR_BAD_PARAMETERS; @@ -1024,45 +1123,53 @@ TEEC_Result TEEC_OpenSession(TEEC_Context *context, TEEC_Session *session, // Update Context ID os.contextID = context_imp->contextID; + // Update Connection details switch (connectionMethod) { - case TEEC_LOGIN_PUBLIC: - case TEEC_LOGIN_USER: - case TEEC_LOGIN_APPLICATION: - case TEEC_LOGIN_USER_APPLICATION: - if (connectionData != NULL) { - return TEEC_ERROR_BAD_PARAMETERS; - } - break; - case TEEC_LOGIN_GROUP: - case TEEC_LOGIN_GROUP_APPLICATION: - if (connectionData == NULL) { - return TEEC_ERROR_BAD_PARAMETERS; - } - os.connData = *(uint32_t*)connectionData; - break; - default: + case TEEC_LOGIN_PUBLIC: + case TEEC_LOGIN_USER: + case TEEC_LOGIN_APPLICATION: + case TEEC_LOGIN_USER_APPLICATION: + if (connectionData != NULL) return TEEC_ERROR_BAD_PARAMETERS; + + break; + + case TEEC_LOGIN_GROUP: + case TEEC_LOGIN_GROUP_APPLICATION: + if (connectionData == NULL) + return TEEC_ERROR_BAD_PARAMETERS; + + os.connData = *(uint32_t *)connectionData; + break; + + default: + return TEEC_ERROR_BAD_PARAMETERS; } os.connMeth = connectionMethod; - session->imp = (TEEC_SessionImp*)OsaMalloc(sizeof(TEEC_SessionImp)); - TEEC_SessionImp* session_imp = (TEEC_SessionImp*)session->imp; + session->imp = (TEEC_SessionImp *)OsaMalloc(sizeof(TEEC_SessionImp)); + TEEC_SessionImp *session_imp = (TEEC_SessionImp *)session->imp; + if (!session_imp) { LOGE(TEEC_LIB, "NULL session_imp"); return TEEC_ERROR_OUT_OF_MEMORY; } + session_imp->context = context; memcpy(&os.uuid, destination, sizeof(TEEC_UUID)); if (operation) { result = preProcessOperation(session, operation, &op, tmpSharedMem); + if (result != TEEC_SUCCESS) { LOGE(TEEC_LIB, "preProcessOperation failed"); OsaFree(session_imp); session->imp = NULL; + if (operation) postProcessOperation(operation, &op, tmpSharedMem); + /* temp memref cleanup & release */ for (i = 0; i < 4; i++) { if (tmpSharedMem[i]) { @@ -1071,8 +1178,10 @@ TEEC_Result TEEC_OpenSession(TEEC_Context *context, TEEC_Session *session, tmpSharedMem[i] = NULL; } } + return result; } + memcpy(&os.operation, &op, sizeof(OperationData)); } else memset(&os.operation, 0x00, sizeof(OperationData)); @@ -1081,15 +1190,19 @@ TEEC_Result TEEC_OpenSession(TEEC_Context *context, TEEC_Session *session, // Send the command and data to Simulator Daemon through the socket pthread_mutex_lock(&context_imp->lock); result = sendCommand(context_imp->sockfd, OPEN_SESSION, &os, - sizeof(OpenSessionData)); + sizeof(OpenSessionData)); pthread_mutex_unlock(&context_imp->lock); if (result != TEEC_SUCCESS) { // Communication Failure LOGE(TEEC_LIB, "sendCommand to Simulator Daemon failed"); + if (returnOrigin) *returnOrigin = TEEC_ORIGIN_COMMS; + OsaFree(session_imp); session->imp = NULL; + if (operation) postProcessOperation(operation, &os.operation, tmpSharedMem); + /* temp memref cleanup & release */ for (i = 0; i < 4; i++) { if (tmpSharedMem[i]) { @@ -1098,17 +1211,21 @@ TEEC_Result TEEC_OpenSession(TEEC_Context *context, TEEC_Session *session, tmpSharedMem[i] = NULL; } } + return result; } if (returnOrigin) *returnOrigin = os.returnOrigin; result = os.returnValue; + if (result != TEEC_SUCCESS) { // Command Failure LOGE(TEEC_LIB, "Simulator Daemon Open Session returned failure"); OsaFree(session_imp); session->imp = NULL; + if (operation) postProcessOperation(operation, &os.operation, tmpSharedMem); + /* temp memref cleanup & release */ for (i = 0; i < 4; i++) { if (tmpSharedMem[i]) { @@ -1117,10 +1234,12 @@ TEEC_Result TEEC_OpenSession(TEEC_Context *context, TEEC_Session *session, tmpSharedMem[i] = NULL; } } + return result; } else session_imp->sessionID = os.sessionID; if (operation) postProcessOperation(operation, &os.operation, tmpSharedMem); + /* temp memref cleanup & release */ for (i = 0; i < 4; i++) { if (tmpSharedMem[i]) { @@ -1129,6 +1248,7 @@ TEEC_Result TEEC_OpenSession(TEEC_Context *context, TEEC_Session *session, tmpSharedMem[i] = NULL; } } + return result; } @@ -1139,7 +1259,8 @@ TEEC_Result TEEC_OpenSession(TEEC_Context *context, TEEC_Session *session, * Parameters: session - the session to close. * ===================================================================================== */ -void TEEC_CloseSession(TEEC_Session *session) { +void TEEC_CloseSession(TEEC_Session *session) +{ LOGD(TEEC_LIB, "Entry"); TEEC_Result result = TEEC_SUCCESS; @@ -1150,14 +1271,18 @@ void TEEC_CloseSession(TEEC_Session *session) { LOGE(TEEC_LIB, "NULL session"); return; } + // Check if Session imp is valid - TEEC_SessionImp* session_imp = (TEEC_SessionImp*)session->imp; + TEEC_SessionImp *session_imp = (TEEC_SessionImp *)session->imp; + if (!session_imp) { LOGE(TEEC_LIB, "NULL session_imp"); return; } + // Check if Context imp is valid - TEEC_ContextImp* context_imp = (TEEC_ContextImp*)session_imp->context->imp; + TEEC_ContextImp *context_imp = (TEEC_ContextImp *)session_imp->context->imp; + if (!context_imp || context_imp->sockfd < 0) { LOGE(TEEC_LIB, "Bad parameters"); return; @@ -1172,13 +1297,14 @@ void TEEC_CloseSession(TEEC_Session *session) { // Send the command and data to Simulator Daemon through the socket pthread_mutex_lock(&context_imp->lock); result = sendCommand(context_imp->sockfd, CLOSE_SESSION, &cs, - sizeof(CloseSessionData)); + sizeof(CloseSessionData)); pthread_mutex_unlock(&context_imp->lock); if (result != TEEC_SUCCESS) { // Communication Failure LOGE(TEEC_LIB, "sendCommand to Simulator Daemon failed"); return; } + // Release Session session->imp = NULL; OsaFree(session_imp); @@ -1207,7 +1333,8 @@ void TEEC_CloseSession(TEEC_Session *session) { * ===================================================================================== */ TEEC_Result TEEC_InvokeCommand(TEEC_Session *session, uint32_t commandID, - TEEC_Operation *operation, uint32_t *returnOrigin) { + TEEC_Operation *operation, uint32_t *returnOrigin) +{ LOGD(TEEC_LIB, "Entry"); TEEC_Result result = TEEC_SUCCESS; @@ -1215,7 +1342,7 @@ TEEC_Result TEEC_InvokeCommand(TEEC_Session *session, uint32_t commandID, InvokeCommandData ic; OperationData op; TEEC_SharedMemory *tmpSharedMem[4]; - memset(tmpSharedMem, 0x0, sizeof(TEEC_SharedMemory*) * 4); + memset(tmpSharedMem, 0x0, sizeof(TEEC_SharedMemory *) * 4); uint32_t i; // Check if Session is valid @@ -1225,13 +1352,16 @@ TEEC_Result TEEC_InvokeCommand(TEEC_Session *session, uint32_t commandID, } // Check if Session imp is valid - TEEC_SessionImp* session_imp = (TEEC_SessionImp*)session->imp; + TEEC_SessionImp *session_imp = (TEEC_SessionImp *)session->imp; + if (!session_imp) { LOGE(TEEC_LIB, "NULL session_imp"); return TEEC_ERROR_BAD_PARAMETERS; } + // Check if Context imp is valid - TEEC_ContextImp *context_imp = (TEEC_ContextImp*)session_imp->context->imp; + TEEC_ContextImp *context_imp = (TEEC_ContextImp *)session_imp->context->imp; + if (!context_imp) { LOGE(TEEC_LIB, "NULL context_imp"); return TEEC_ERROR_BAD_PARAMETERS; @@ -1251,9 +1381,12 @@ TEEC_Result TEEC_InvokeCommand(TEEC_Session *session, uint32_t commandID, if (operation) { result = preProcessOperation(session, operation, &op, tmpSharedMem); + if (result != TEEC_SUCCESS) { LOGE(TEEC_LIB, "preProcessOperation failed"); + if (operation) postProcessOperation(operation, &op, tmpSharedMem); + /* temp memref cleanup & release */ for (i = 0; i < 4; i++) { if (tmpSharedMem[i]) { @@ -1262,8 +1395,10 @@ TEEC_Result TEEC_InvokeCommand(TEEC_Session *session, uint32_t commandID, tmpSharedMem[i] = NULL; } } + return result; } + memcpy(&ic.operation, &op, sizeof(OperationData)); } else memset(&ic.operation, 0x00, sizeof(OperationData)); @@ -1275,13 +1410,16 @@ TEEC_Result TEEC_InvokeCommand(TEEC_Session *session, uint32_t commandID, // Send the command and data to Simulator Daemon through the socket pthread_mutex_lock(&context_imp->lock); result = sendCommand(context_imp->sockfd, INVOKE_COMMAND, &ic, - sizeof(InvokeCommandData)); + sizeof(InvokeCommandData)); pthread_mutex_unlock(&context_imp->lock); if (result != TEEC_SUCCESS) { // Communication Failure LOGE(TEEC_LIB, "sendCommand to Simulator Daemon failed"); + if (returnOrigin) *returnOrigin = TEEC_ORIGIN_COMMS; + if (operation) postProcessOperation(operation, &ic.operation, tmpSharedMem); + /* temp memref cleanup & release */ for (i = 0; i < 4; i++) { if (tmpSharedMem[i]) { @@ -1290,14 +1428,17 @@ TEEC_Result TEEC_InvokeCommand(TEEC_Session *session, uint32_t commandID, tmpSharedMem[i] = NULL; } } + return result; } if (returnOrigin) *returnOrigin = ic.returnOrigin; result = ic.returnValue; + if (result != TEEC_SUCCESS) { // Command Failure if (operation) postProcessOperation(operation, &ic.operation, tmpSharedMem); + /* temp memref cleanup & release */ for (i = 0; i < 4; i++) { if (tmpSharedMem[i]) { @@ -1306,10 +1447,12 @@ TEEC_Result TEEC_InvokeCommand(TEEC_Session *session, uint32_t commandID, tmpSharedMem[i] = NULL; } } + return result; } if (operation) postProcessOperation(operation, &ic.operation, tmpSharedMem); + /* temp memref cleanup & release */ for (i = 0; i < 4; i++) { if (tmpSharedMem[i]) { @@ -1318,6 +1461,7 @@ TEEC_Result TEEC_InvokeCommand(TEEC_Session *session, uint32_t commandID, tmpSharedMem[i] = NULL; } } + return result; } @@ -1329,7 +1473,8 @@ TEEC_Result TEEC_InvokeCommand(TEEC_Session *session, uint32_t commandID, * structure. * ===================================================================================== */ -void TEEC_RequestCancellation(TEEC_Operation *operation) { +void TEEC_RequestCancellation(TEEC_Operation *operation) +{ LOGD(TEEC_LIB, "Entry"); TEEC_Result result = TEEC_SUCCESS; @@ -1341,25 +1486,32 @@ void TEEC_RequestCancellation(TEEC_Operation *operation) { LOGE(TEEC_LIB, "Cancellation not allowed"); return; } + // Check if Operation imp is valid - TEEC_OperationImp* operation_imp = (TEEC_OperationImp*)operation->imp; + TEEC_OperationImp *operation_imp = (TEEC_OperationImp *)operation->imp; + if (!operation_imp) { LOGE(TEEC_LIB, "NULL operation_imp"); return; } + // Check if session is valid if (!operation_imp->session) { LOGE(TEEC_LIB, "NULL session"); return; } + // check if Session imp is valid - TEEC_SessionImp* session_imp = (TEEC_SessionImp*)operation_imp->session->imp; + TEEC_SessionImp *session_imp = (TEEC_SessionImp *)operation_imp->session->imp; + if (!session_imp) { LOGE(TEEC_LIB, "NULL session_imp"); return; } + // Check if Context imp is valid - TEEC_ContextImp *context_imp = (TEEC_ContextImp*)session_imp->context->imp; + TEEC_ContextImp *context_imp = (TEEC_ContextImp *)session_imp->context->imp; + if (!context_imp) { LOGE(TEEC_LIB, "NULL context_imp"); return; @@ -1375,10 +1527,11 @@ void TEEC_RequestCancellation(TEEC_Operation *operation) { // Send the command and data to Simulator Daemon through the socket pthread_mutex_lock(&context_imp->lock); result = sendCommand(context_imp->sockfd, REQUEST_CANCELLATION, &rc, - sizeof(ReqCancellationData)); + sizeof(ReqCancellationData)); pthread_mutex_unlock(&context_imp->lock); - if (result != TEEC_SUCCESS) { + + if (result != TEEC_SUCCESS) LOGE(TEEC_LIB, "sendCommand to Simulator Daemon failed"); - } + return; } diff --git a/TEECLib/src/teec_connection.c b/TEECLib/src/teec_connection.c index 5f51621..8aadfc1 100644 --- a/TEECLib/src/teec_connection.c +++ b/TEECLib/src/teec_connection.c @@ -41,15 +41,16 @@ * Name: connecttoServer * Description: API (Interface for TEECAPI) implementation for connecting to * the Simulator Daemon through socket - * Return: Socket fd on success - * -1 on failure + * Return: Socket fd on success + * -1 on failure * ===================================================================================== */ -int32_t connecttoServer(void) { +int32_t connecttoServer(void) +{ LOGD(TEEC_LIB, "Entry"); int32_t serverSocket, socklen; size_t sock_path_len = 0; - struct sockaddr* sockptr; + struct sockaddr *sockptr; struct sockaddr_un daemonsock; // Get socket decriptor @@ -57,13 +58,14 @@ int32_t connecttoServer(void) { LOGE(TEEC_LIB, "No socket for simdaemon"); return -1; } + daemonsock.sun_family = AF_UNIX; sock_path_len = strlen(SOCKPATH); - strncpy(daemonsock.sun_path, SOCKPATH, sock_path_len+1); + strncpy(daemonsock.sun_path, SOCKPATH, sock_path_len + 1); socklen = sizeof(daemonsock); - sockptr = (struct sockaddr*)&daemonsock; + sockptr = (struct sockaddr *)&daemonsock; // Connect to Simulator Daemon if (connect(serverSocket, sockptr, socklen) == -1) { @@ -71,6 +73,7 @@ int32_t connecttoServer(void) { close(serverSocket); return -1; } + return serverSocket; } @@ -79,24 +82,25 @@ int32_t connecttoServer(void) { * Name: disconnectfromServer * Description: API (Interface for TEECAPI) implementation for disconnecting * from the Simulator daemon through socket - * Parameters: serverSocket - Socket fd + * Parameters: serverSocket - Socket fd * ===================================================================================== */ -void disconnectfromServer(int32_t serverSocket) { +void disconnectfromServer(int32_t serverSocket) +{ int32_t result; LOGD(TEEC_LIB, "Entry"); if (serverSocket >= 0) { // shutdown the socket result = shutdown(serverSocket, SHUT_WR); + if (result != 0) - LOGE(TEEC_LIB, "disconnectfromServer failed"); + LOGE(TEEC_LIB, "disconnectfromServer failed"); // close the socket close(serverSocket); - } else { + } else LOGE(TEEC_LIB, "Invalid socket, disconnectfromServer failed"); - } } /* @@ -104,14 +108,15 @@ void disconnectfromServer(int32_t serverSocket) { * Name: sendCommandtoDaemon * Description: Function implementation for sending data to Simulator daemon * through socket - * Parameters: serverSocket - Socket fd - * fdata - data to be sent to Simulator Daemon - * size - Size of data to be sent - * Return: 0 on success - * errno on failure + * Parameters: serverSocket - Socket fd + * fdata - data to be sent to Simulator Daemon + * size - Size of data to be sent + * Return: 0 on success + * errno on failure * ===================================================================================== */ -static uint32_t sendCommandtoDaemon(int32_t sockfd, char* fdata, size_t size) { +static uint32_t sendCommandtoDaemon(int32_t sockfd, char *fdata, size_t size) +{ LOGD(TEEC_LIB, "Entry"); ssize_t nwrite = 0; size_t nbytes = 0; @@ -121,9 +126,12 @@ static uint32_t sendCommandtoDaemon(int32_t sockfd, char* fdata, size_t size) { do { nwrite = send(sockfd, fdata + nbytes, size - nbytes, 0); } while ((nwrite == -1 && errno == EINTR) || (nwrite > 0 && ((nbytes += - nwrite) < size))); + + nwrite) < size))); + return (size != nbytes) ? errno : 0; } + LOGE(TEEC_LIB, "failed"); return TEEC_ERROR_COMMUNICATION; } @@ -133,14 +141,15 @@ static uint32_t sendCommandtoDaemon(int32_t sockfd, char* fdata, size_t size) { * Name: receiveResponse * Description: Function implementation for recieving data from Simulator * daemon through socket - * Parameters: serverSocket - Socket fd - * fdata - data received from Simulator Daemon - * size - Size of received data - * Return: 0 on success - * errno on failure + * Parameters: serverSocket - Socket fd + * fdata - data received from Simulator Daemon + * size - Size of received data + * Return: 0 on success + * errno on failure * ===================================================================================== */ -static uint32_t receiveResponse(int32_t sockfd, char* fdata, size_t size) { +static uint32_t receiveResponse(int32_t sockfd, char *fdata, size_t size) +{ LOGD(TEEC_LIB, "Entry"); ssize_t nread = 0; size_t nbytes = 0; @@ -150,10 +159,12 @@ static uint32_t receiveResponse(int32_t sockfd, char* fdata, size_t size) { do { nread = recv(sockfd, fdata + nbytes, size - nbytes, 0); } while ((nread == -1 && errno == EINTR) - || (nread > 0 && ((nbytes += nread) < size))); + + || (nread > 0 && ((nbytes += nread) < size))); return (size != nbytes) ? errno : 0; } + LOGE(TEEC_LIB, "failed"); return TEEC_ERROR_COMMUNICATION; } @@ -163,16 +174,17 @@ static uint32_t receiveResponse(int32_t sockfd, char* fdata, size_t size) { * === FUNCTION ====================================================================== * Name: Test * Description: Local function for Unit testing of TEECLib module - * Parameters: cmd - Command sent/received - * fdata - data sent/received - * size - size of data sent/received - * in - 1: Sent data - * Any other value: Received data - * Return: TEEC_SUCCESS: Success - * TEEC_ERROR_GENERIC: Failure + * Parameters: cmd - Command sent/received + * fdata - data sent/received + * size - size of data sent/received + * in - 1: Sent data + * Any other value: Received data + * Return: TEEC_SUCCESS: Success + * TEEC_ERROR_GENERIC: Failure * ===================================================================================== */ -static uint32_t Test(char cmd, char* fdata, size_t size, uint32_t in) { +static uint32_t Test(char cmd, char *fdata, size_t size, uint32_t in) +{ LOGD(TEEC_LIB, "Entry"); FILE *f1; @@ -181,84 +193,97 @@ static uint32_t Test(char cmd, char* fdata, size_t size, uint32_t in) { char *buffer; switch (cmd) { - case INITIALIZE_CONTEXT: + case INITIALIZE_CONTEXT: if (in == 1) - fname = "InitContext.txt"; + fname = "InitContext.txt"; else - fname = "InitContextResult.txt"; + fname = "InitContextResult.txt"; + f1 = fopen(fname, "w+"); - InitContextData initdata = *(InitContextData*) fdata; + InitContextData initdata = *(InitContextData *) fdata; fprintf(f1, - "InitializeContextData ---------\ncontextID %d\nnameLength %d\ - \nTEEName %s\nreturnValue %d\n", + "InitializeContextData ---------\ncontextID %d\nnameLength %d" + " \nTEEName %s\nreturnValue %d\n", initdata.contextID, initdata.nameLength, &initdata.TEEName[0], initdata.returnValue); fclose(f1); break; - case FINALIZE_CONTEXT: + + case FINALIZE_CONTEXT: if (in == 1) - fname = "FinContext.txt"; + fname = "FinContext.txt"; else - fname = "FinContextResult.txt"; + fname = "FinContextResult.txt"; + f1 = fopen(fname, "w+"); - FinalizeContextData findata = *(FinalizeContextData*) fdata; + FinalizeContextData findata = *(FinalizeContextData *) fdata; fprintf(f1, "FinalizeContextData ---------\ncontextID %d\n", findata.contextID); fclose(f1); break; - case REGISTER_SHARED_MEMORY: + + case REGISTER_SHARED_MEMORY: if (in == 1) - fname = "RegMem.txt"; + fname = "RegMem.txt"; else - fname = "RegMemResult.txt"; + fname = "RegMemResult.txt"; + f1 = fopen(fname, "w+"); - RegSharedMemData regdata = *(RegSharedMemData*) fdata; + RegSharedMemData regdata = *(RegSharedMemData *) fdata; fprintf(f1, - "RegSharedMemData ---------\ncontextID %d\nsize %d\nflags %d\ - \nshmKey %d\nreturnValue %d\n", + "RegSharedMemData ---------\ncontextID %d\nsize %d\nflags %d" + " \nshmKey %d\nreturnValue %d\n", regdata.contextID, regdata.sharedMem.size, regdata.sharedMem.flags, regdata.sharedMem.shmKey, regdata.returnValue); fclose(f1); break; - case RELEASE_SHARED_MEMORY: + + case RELEASE_SHARED_MEMORY: if (in == 1) - fname = "RelMem.txt"; + fname = "RelMem.txt"; else - fname = "RelMemResult.txt"; + fname = "RelMemResult.txt"; + f1 = fopen(fname, "w+"); - RelSharedMemData reldata = *(RelSharedMemData*) fdata; + RelSharedMemData reldata = *(RelSharedMemData *) fdata; fprintf(f1, - "RelSharedMemData ---------\ncontextID %d\nsize %d\nflags %d\ - \nshmKey %d\n", + "RelSharedMemData ---------\ncontextID %d\nsize %d\nflags %d" + " \nshmKey %d\n", reldata.contextID, reldata.sharedMem.size, reldata.sharedMem.flags, reldata.sharedMem.shmKey); break; - case OPEN_SESSION: + + case OPEN_SESSION: if (in == 1) - fname = "OpenSess.txt"; + fname = "OpenSess.txt"; else - fname = "OpenSessResult.txt"; + fname = "OpenSessResult.txt"; + f1 = fopen(fname, "w+"); - OpenSessionData osdata = *(OpenSessionData*) fdata; + OpenSessionData osdata = *(OpenSessionData *) fdata; fprintf(f1, - "OpensessionData ---------\ncontextID %d\nSessionID %d\ - \nuuidtimeLow %d\nuuidtimeMid %d\ntimeHiAndVersion %d\n", + "OpensessionData ---------\ncontextID %d\nSessionID %d" + " \nuuidtimeLow %d\nuuidtimeMid %d\ntimeHiAndVersion %d\n", osdata.contextID, osdata.sessionID, osdata.uuid.timeLow, osdata.uuid.timeMid, osdata.uuid.timeHiAndVersion); + for (i = 0; i < 8; i++) - fprintf(f1, "clockSeqAndNode[%d] %d\n", i, - osdata.uuid.clockSeqAndNode[i]); + fprintf(f1, "clockSeqAndNode[%d] %d\n", i, + osdata.uuid.clockSeqAndNode[i]); + fprintf(f1, "connMethod %d\nconnData %d\nparamTypes %d\n", osdata.connMeth, osdata.connData, osdata.operation.paramTypes); + for (i = 0; i < 4; i++) { type = ((osdata.operation.paramTypes) >> (8 * i)) & 0x7f; + if (type == TEEC_NONE) - fprintf(f1, "param[%d] NONE\n", i); + fprintf(f1, "param[%d] NONE\n", i); else if ((type - == TEEC_VALUE_INPUT) | (type == TEEC_VALUE_OUTPUT) | (type == TEEC_VALUE_INOUT)) - fprintf(f1, "param[%d] value a %d value b %d\n", i, - osdata.operation.params[i].value.a, - osdata.operation.params[i].value.b); + == TEEC_VALUE_INPUT) | (type == TEEC_VALUE_OUTPUT) | (type == TEEC_VALUE_INOUT)) + fprintf(f1, "param[%d] value a %d value b %d\n", i, + osdata.operation.params[i].value.a, + osdata.operation.params[i].value.b); else { fprintf(f1, "param[%d] mem shmKey %d size %d offset %d\n", i, osdata.operation.params[i].mem.shmKey, @@ -266,13 +291,14 @@ static uint32_t Test(char cmd, char* fdata, size_t size, uint32_t in) { osdata.operation.params[i].mem.offset); key = osdata.operation.params[i].mem.shmKey; shmid = shmget(key, osdata.operation.params[i].mem.size, - IPC_CREAT | 0666); + IPC_CREAT | 0666); + if (shmid == -1) { LOGE(TEEC_LIB, "shmget failed"); return TEEC_ERROR_GENERIC; } - if ((buffer = (char*) shmat(shmid, NULL, 0)) == (char*) -1) { + if ((buffer = (char *) shmat(shmid, NULL, 0)) == (char *) - 1) { LOGE(TEEC_LIB, "shmat failed"); return TEEC_ERROR_GENERIC; } @@ -284,52 +310,61 @@ static uint32_t Test(char cmd, char* fdata, size_t size, uint32_t in) { fprintf(f1, "SharedMemData: \n"); char *shmdata = (buffer + osdata.operation.params[i].mem.offset); + for (j = 0; j < osdata.operation.params[i].mem.size; j++) - fprintf(f1, "%x", shmdata[j]); + fprintf(f1, "%x", shmdata[j]); + fprintf(f1, "\n"); - if (shmdt(buffer) == -1) { + if (shmdt(buffer) == -1) printf("shmdt failed"); - } + buffer = NULL; } } + fprintf(f1, "OperationID %d\nreturnOrigin %d\nreturnValue %d\n", osdata.operation.OperationID, osdata.returnOrigin, osdata.returnValue); fclose(f1); break; - case CLOSE_SESSION: + + case CLOSE_SESSION: if (in == 1) - fname = "CloseSess.txt"; + fname = "CloseSess.txt"; else - fname = "CloseSessResult.txt"; + fname = "CloseSessResult.txt"; + f1 = fopen(fname, "w+"); - CloseSessionData csdata = *(CloseSessionData*) fdata; + CloseSessionData csdata = *(CloseSessionData *) fdata; fprintf(f1, "ClosesessionData ---------\ncontextID %d\nSessionID %d\n", csdata.contextID, csdata.sessionID); fclose(f1); break; - case INVOKE_COMMAND: + + case INVOKE_COMMAND: if (in == 1) - fname = "InvComm.txt"; + fname = "InvComm.txt"; else - fname = "InvCommResult.txt"; + fname = "InvCommResult.txt"; + f1 = fopen(fname, "w+"); - InvokeCommandData icdata = *(InvokeCommandData*) fdata; + InvokeCommandData icdata = *(InvokeCommandData *) fdata; fprintf(f1, - "InvokeCommandData ---------\ncontextID %d\nSessionID %d\ - \nCommandID %d\nparamTypes %d\n", + "InvokeCommandData ---------\ncontextID %d\nSessionID %d" + " \nCommandID %d\nparamTypes %d\n", icdata.contextID, icdata.sessionID, icdata.commandID, icdata.operation.paramTypes); + for (i = 0; i < 4; i++) { type = ((icdata.operation.paramTypes) >> (8 * i)) & 0x7f; + if (type == TEEC_NONE) - fprintf(f1, "param[%d] NONE\n", i); + fprintf(f1, "param[%d] NONE\n", i); else if ((type - == TEEC_VALUE_INPUT) | (type == TEEC_VALUE_OUTPUT) | (type == TEEC_VALUE_INOUT)) - fprintf(f1, "param[%d] value a %d value b %d\n", i, - icdata.operation.params[i].value.a, - icdata.operation.params[i].value.b); + == TEEC_VALUE_INPUT) | (type == TEEC_VALUE_OUTPUT) | (type == TEEC_VALUE_INOUT)) + fprintf(f1, "param[%d] value a %d value b %d\n", i, + icdata.operation.params[i].value.a, + icdata.operation.params[i].value.b); else { fprintf(f1, "param[%d] mem shmKey %d size %d offset %d\n", i, icdata.operation.params[i].mem.shmKey, @@ -337,13 +372,14 @@ static uint32_t Test(char cmd, char* fdata, size_t size, uint32_t in) { icdata.operation.params[i].mem.offset); key = icdata.operation.params[i].mem.shmKey; shmid = shmget(key, icdata.operation.params[i].mem.size, - IPC_CREAT | 0666); + IPC_CREAT | 0666); + if (shmid == -1) { LOGE(TEEC_LIB, "shmget failed"); return TEEC_ERROR_GENERIC; } - if ((buffer = (char*) shmat(shmid, NULL, 0)) == (char*) -1) { + if ((buffer = (char *) shmat(shmid, NULL, 0)) == (char *) - 1) { LOGE(TEEC_LIB, "shmat failed"); return TEEC_ERROR_GENERIC; } @@ -355,30 +391,37 @@ static uint32_t Test(char cmd, char* fdata, size_t size, uint32_t in) { fprintf(f1, "SharedMemData: \n"); char *shmdata = (buffer + icdata.operation.params[i].mem.offset); + for (j = 0; j < icdata.operation.params[i].mem.size; j++) - fprintf(f1, "%x", shmdata[j]); + fprintf(f1, "%x", shmdata[j]); + fprintf(f1, "\n"); - if (shmdt(buffer) == -1) { + if (shmdt(buffer) == -1) printf("shmdt failed"); - } + buffer = NULL; } } + fprintf(f1, "OperationID %d\nreturnOrigin %d\nreturnValue %d\n", icdata.operation.OperationID, icdata.returnOrigin, icdata.returnValue); fclose(f1); break; - case REQUEST_CANCELLATION: + + case REQUEST_CANCELLATION: if (in == 1) - fname = "ReqCancell.txt"; + fname = "ReqCancell.txt"; else - fname = "ReqCancellResult.txt"; + fname = "ReqCancellResult.txt"; + //Not supported in current design break; - default: + + default: LOGE(TEEC_LIB, "Invalid command"); } + return TEEC_SUCCESS; } #endif @@ -388,15 +431,16 @@ static uint32_t Test(char cmd, char* fdata, size_t size, uint32_t in) { * Name: sendCommand * Description: API (Interface for TEECAPI) implementation for sending a * command to Simulator daemon - * Parameters: sockfd - Socket fd - * cmd - Command sent/received - * fdata - data sent/received - * size - size of data sent/received - * Return: TEEC_SUCCESS: Success - * TEEC_ERROR_GENERIC: Failure + * Parameters: sockfd - Socket fd + * cmd - Command sent/received + * fdata - data sent/received + * size - size of data sent/received + * Return: TEEC_SUCCESS: Success + * TEEC_ERROR_GENERIC: Failure * ===================================================================================== */ -uint32_t sendCommand(int32_t sockfd, TEE_CMD cmd, void* data, size_t size) { +uint32_t sendCommand(int32_t sockfd, TEE_CMD cmd, void *data, size_t size) +{ LOGD(TEEC_LIB, "Entry"); TEEC_Result result = TEEC_SUCCESS; char command = (char)cmd; @@ -404,36 +448,42 @@ uint32_t sendCommand(int32_t sockfd, TEE_CMD cmd, void* data, size_t size) { #ifdef TEST // Check if the data is proper result = Test(command, data, size, 1); - if (result != TEEC_SUCCESS) { + + if (result != TEEC_SUCCESS) return TEEC_ERROR_GENERIC; - } + #endif // send command to Simulator Daemon - result = sendCommandtoDaemon(sockfd, (char*)&command, sizeof(char)); - if (result != TEEC_SUCCESS) { + result = sendCommandtoDaemon(sockfd, (char *)&command, sizeof(char)); + + if (result != TEEC_SUCCESS) return TEEC_ERROR_GENERIC; - } + // send command data to Simulator Daemon - result = sendCommandtoDaemon(sockfd, (char*)data, size); - if (result != TEEC_SUCCESS) { + result = sendCommandtoDaemon(sockfd, (char *)data, size); + + if (result != TEEC_SUCCESS) return TEEC_ERROR_GENERIC; - } + // receive command from Simulator Daemon - result = receiveResponse(sockfd, (char*)&command, sizeof(char)); - if (result != TEEC_SUCCESS) { + result = receiveResponse(sockfd, (char *)&command, sizeof(char)); + + if (result != TEEC_SUCCESS) return TEEC_ERROR_GENERIC; - } + // receive command data from Simulator Daemon - result = receiveResponse(sockfd, (char*)data, size); - if (result != TEEC_SUCCESS) { + result = receiveResponse(sockfd, (char *)data, size); + + if (result != TEEC_SUCCESS) return TEEC_ERROR_GENERIC; - } + #ifdef TEST // Check if the data is proper result = Test(command, data, size, 0); - if (result != TEEC_SUCCESS) { + + if (result != TEEC_SUCCESS) return TEEC_ERROR_GENERIC; - } + #endif return result; } diff --git a/helloworld/host/main.c b/helloworld/host/main.c index b5cdc1b..1f75ae6 100644 --- a/helloworld/host/main.c +++ b/helloworld/host/main.c @@ -38,23 +38,28 @@ int main(int argc, char *argv[]) uint32_t error; result = TEEC_InitializeContext(NULL, &ctx); + if (result != TEEC_SUCCESS) { printf("TEEC_InitializeContext failed with result %x\n", result); return -1; } - result = TEEC_OpenSession(&ctx, &sess, &uuid, TEEC_LOGIN_PUBLIC, NULL, NULL, &error); + result = TEEC_OpenSession(&ctx, &sess, &uuid, TEEC_LOGIN_PUBLIC, NULL, NULL, + &error); + if (result != TEEC_SUCCESS) { printf("TEEC_OpenSession failed with result %x\n", result); return -2; } - op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INOUT, TEEC_NONE, TEEC_NONE, TEEC_NONE); + op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INOUT, TEEC_NONE, TEEC_NONE, + TEEC_NONE); op.params[0].value.a = 1; op.params[0].value.b = 0; printf("Incrementing %d via TA\n", op.params[0].value.a); result = TEEC_InvokeCommand(&sess, HELLO_WORLD_CMD_INC, &op, &error); + if (result != TEEC_SUCCESS) { printf("TEEC_InvokeCommand failed with result %x, error %x\n", result, error); return -3; diff --git a/helloworld/ta/hello_world.c b/helloworld/ta/hello_world.c index 188b97d..c2ebc3d 100644 --- a/helloworld/ta/hello_world.c +++ b/helloworld/ta/hello_world.c @@ -49,9 +49,10 @@ TEE_Result TA_OpenSessionEntryPoint(uint32_t param_types, LOG_FUNC(); uint32_t expectedParams = TEE_PARAM_TYPES(TEE_PARAM_TYPE_NONE, - TEE_PARAM_TYPE_NONE, - TEE_PARAM_TYPE_NONE, - TEE_PARAM_TYPE_NONE); + TEE_PARAM_TYPE_NONE, + TEE_PARAM_TYPE_NONE, + TEE_PARAM_TYPE_NONE); + if (param_types != expectedParams) return TEE_ERROR_BAD_PARAMETERS; @@ -70,11 +71,12 @@ static TEE_Result inc_value(uint32_t param_types, TEE_Param params[4]) { uint32_t exp_param_types = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INOUT, - TEE_PARAM_TYPE_NONE, - TEE_PARAM_TYPE_NONE, - TEE_PARAM_TYPE_NONE); + TEE_PARAM_TYPE_NONE, + TEE_PARAM_TYPE_NONE, + TEE_PARAM_TYPE_NONE); LOG_FUNC(); + if (param_types != exp_param_types) return TEE_ERROR_BAD_PARAMETERS; @@ -85,9 +87,9 @@ static TEE_Result inc_value(uint32_t param_types, } TEE_Result TA_InvokeCommandEntryPoint(void *sess_ctx, - uint32_t cmd_id, - uint32_t param_types, - TEE_Param params[4]) + uint32_t cmd_id, + uint32_t param_types, + TEE_Param params[4]) { (void)&sess_ctx; @@ -96,6 +98,7 @@ TEE_Result TA_InvokeCommandEntryPoint(void *sess_ctx, switch (cmd_id) { case HELLO_WORLD_CMD_INC: return inc_value(param_types, params); + default: return TEE_ERROR_BAD_PARAMETERS; } diff --git a/log/log.c b/log/log.c index f99f6b8..cfc55c6 100644 --- a/log/log.c +++ b/log/log.c @@ -41,21 +41,22 @@ static int32_t gmodule_level = 0xfffffff; /* * This method is used to set module level and debug level to debug * - * @param dbg_level - * [IN] enum value of DebugLevel + * @param dbg_level + * [IN] enum value of DebugLevel * - * @param mdl_level - * [IN] enum value of ModuleLevel + * @param mdl_level + * [IN] enum value of ModuleLevel * - * @return void + * @return void */ -__attribute__ ((visibility("default"))) +__attribute__((visibility("default"))) void SetDebugAndModuleLevel(IN const int32_t module_level, - IN const int32_t debug_level) { + IN const int32_t debug_level) +{ if (module_level < UTILS || module_level > ALL_MODULES - || debug_level < INFO_LEVEL_LOG || debug_level > VERBOSE_LEVEL_LOG) { + || debug_level < INFO_LEVEL_LOG || debug_level > VERBOSE_LEVEL_LOG) return; - } + /* * set global variables for module level and debug level */ @@ -67,134 +68,167 @@ void SetDebugAndModuleLevel(IN const int32_t module_level, /* * This method is to get debug level set * - * @param dbg_level - * [IN] enum value of DebugLevel + * @param dbg_level + * [IN] enum value of DebugLevel * - * @return const char pointer + * @return const char pointer */ -const char* GetDebugLevel(IN int32_t dbg_level) { +const char *GetDebugLevel(IN int32_t dbg_level) +{ switch (dbg_level) { - case INFO_LEVEL_LOG: - return "INFO"; - case PACKET_LEVEL_LOG: - return "PACKET"; - case ERROR_LEVEL_LOG: - return "ERROR"; - case DEBUG_LEVEL_LOG: - return "DEBUG"; - case SECURED_LEVEL_LOG: - return "SECURED"; - case VERBOSE_LEVEL_LOG: - return "VERBOSE"; - default: - return ""; + case INFO_LEVEL_LOG: + return "INFO"; + + case PACKET_LEVEL_LOG: + return "PACKET"; + + case ERROR_LEVEL_LOG: + return "ERROR"; + + case DEBUG_LEVEL_LOG: + return "DEBUG"; + + case SECURED_LEVEL_LOG: + return "SECURED"; + + case VERBOSE_LEVEL_LOG: + return "VERBOSE"; + + default: + return ""; } } /* * This method is to get module for which debug is set * - * @param mdl_level - * [IN] enum value of ModuleLevel + * @param mdl_level + * [IN] enum value of ModuleLevel * - * @return const char pointer + * @return const char pointer */ -const char* GetModuleLevel(IN int32_t module_level) { +const char *GetModuleLevel(IN int32_t module_level) +{ switch (module_level) { - case UTILS: - return "UTILS"; - case SIM_DAEMON: - return "SIM_DAEMON"; - case TEEC_LIB: - return "TEEC_LIB"; - case TEE_STUB: - return "TEE_STUB"; - case SSF_LIB: - return "SSF_LIB"; - default: - return "TA_SDK"; + case UTILS: + return "UTILS"; + + case SIM_DAEMON: + return "SIM_DAEMON"; + + case TEEC_LIB: + return "TEEC_LIB"; + + case TEE_STUB: + return "TEE_STUB"; + + case SSF_LIB: + return "SSF_LIB"; + + default: + return "TA_SDK"; } } /* * This method is used to print the debug logs * - * @param function_name - * [IN] name of the fuction + * @param function_name + * [IN] name of the fuction * - * @param line_no - * [IN] line number of debug statement + * @param line_no + * [IN] line number of debug statement * - * @param module_level - * [IN] enum value of ModuleLevel + * @param module_level + * [IN] enum value of ModuleLevel * - * @param dbg_level - * [IN] enum value of DebugLevel + * @param dbg_level + * [IN] enum value of DebugLevel * - * @param message - * [IN] message which needs to be displayed + * @param message + * [IN] message which needs to be displayed * - * @return void + * @return void */ -__attribute__ ((visibility("default"))) -void PrintLog(IN const char* function_name, IN const int32_t line_no, - IN int32_t module_level, IN int32_t debug_level, IN const char* message, - ...) { +__attribute__((visibility("default"))) +void PrintLog(IN const char *function_name, IN const int32_t line_no, + IN int32_t module_level, IN int32_t debug_level, IN const char *message, + ...) +{ #ifndef _ANDROID_NDK + if (0 == (module_level & gmodule_level) - || 0 == (debug_level & gdebug_level)) { + || 0 == (debug_level & gdebug_level)) return; - } + #endif - const char* module = GetModuleLevel(module_level); + const char *module = GetModuleLevel(module_level); va_list variable_list; va_start(variable_list, message); #if defined(__TIZEN__) char buf[512] = {0,}; vsnprintf(buf, 511, message, variable_list); - switch(debug_level) - { - case INFO_LEVEL_LOG: + + switch (debug_level) { + case INFO_LEVEL_LOG: LOG(LOG_INFO, TA_SDK_TAG, "[%s][%s:%d]%s", module, function_name, line_no, buf); break; - case PACKET_LEVEL_LOG: - LOG(LOG_DEBUG, TA_SDK_TAG, "[%s][%s:%d]%s", module, function_name, line_no, buf); + + case PACKET_LEVEL_LOG: + LOG(LOG_DEBUG, TA_SDK_TAG, "[%s][%s:%d]%s", module, function_name, line_no, + buf); break; - case ERROR_LEVEL_LOG: - LOG(LOG_ERROR, TA_SDK_TAG, "[%s][%s:%d]%s", module, function_name, line_no, buf); + + case ERROR_LEVEL_LOG: + LOG(LOG_ERROR, TA_SDK_TAG, "[%s][%s:%d]%s", module, function_name, line_no, + buf); break; - case DEBUG_LEVEL_LOG: - case SECURED_LEVEL_LOG: - case VERBOSE_LEVEL_LOG: - LOG(LOG_DEBUG, TA_SDK_TAG, "[%s][%s:%d]%s", module, function_name, line_no, buf); + + case DEBUG_LEVEL_LOG: + case SECURED_LEVEL_LOG: + case VERBOSE_LEVEL_LOG: + LOG(LOG_DEBUG, TA_SDK_TAG, "[%s][%s:%d]%s", module, function_name, line_no, + buf); break; - default: - LOG(LOG_DEBUG, TA_SDK_TAG, "[%s][%s:%d]%s", module, function_name, line_no, buf); + + default: + LOG(LOG_DEBUG, TA_SDK_TAG, "[%s][%s:%d]%s", module, function_name, line_no, + buf); break; } + #elif defined(_ANDROID_NDK) char buf[512] = {'\0'}; vsnprintf(buf, sizeof(buf), message, variable_list); - switch(debug_level) - { - case INFO_LEVEL_LOG: - __android_log_print(ANDROID_LOG_INFO, gtag, "[%s] %s: %d: %s\n", module, function_name, line_no, buf); + + switch (debug_level) { + case INFO_LEVEL_LOG: + __android_log_print(ANDROID_LOG_INFO, gtag, "[%s] %s: %d: %s\n", module, + function_name, line_no, buf); break; - case ERROR_LEVEL_LOG: - __android_log_print(ANDROID_LOG_ERROR, gtag, "[%s] %s: %d: %s\n", module, function_name, line_no, buf); + + case ERROR_LEVEL_LOG: + __android_log_print(ANDROID_LOG_ERROR, gtag, "[%s] %s: %d: %s\n", module, + function_name, line_no, buf); break; - case DEBUG_LEVEL_LOG: - __android_log_print(ANDROID_LOG_WARN, gtag, "[%s] %s: %d: %s\n", module, function_name, line_no, buf); + + case DEBUG_LEVEL_LOG: + __android_log_print(ANDROID_LOG_WARN, gtag, "[%s] %s: %d: %s\n", module, + function_name, line_no, buf); break; - case SECURED_LEVEL_LOG: - __android_log_print(ANDROID_LOG_DEBUG, gtag, "[%s] %s: %d: %s\n", module, function_name, line_no, buf); + + case SECURED_LEVEL_LOG: + __android_log_print(ANDROID_LOG_DEBUG, gtag, "[%s] %s: %d: %s\n", module, + function_name, line_no, buf); break; - default: -// __android_log_print(ANDROID_LOG_VERBOSE, gtag, "[%s]%s: %d: %s\n", module, function_name, line_no, buf); + + default: + // __android_log_print(ANDROID_LOG_VERBOSE, gtag, "[%s]%s: %d: %s\n", module, function_name, line_no, buf); break; } + #else - const char* severity = GetDebugLevel(debug_level); + const char *severity = GetDebugLevel(debug_level); printf("[%s] [%s] %s: %d: ", module, severity, function_name, line_no); vprintf(message, variable_list); printf("\n"); diff --git a/osal/OsaCommon.c b/osal/OsaCommon.c index a563e0a..3caabef 100644 --- a/osal/OsaCommon.c +++ b/osal/OsaCommon.c @@ -28,19 +28,19 @@ /*----------------------------------------------------------------------------- * Globals *-----------------------------------------------------------------------------*/ -// Global Timer Datatypes +// Global Timer Datatypes void timer_handler(int iJunk); typedef void (*tHandler)(int data); -typedef void (*TimerProcT)(void* pvdata); +typedef void (*TimerProcT)(void *pvdata); struct Timerdata { unsigned int stop_timer; unsigned int start_timer; struct itimerval iTval_t; TimerProcT callback; - void* pvTmpdata; + void *pvTmpdata; }; struct Timerdata Timer_data_t; @@ -48,22 +48,25 @@ struct Timerdata Timer_data_t; /*----------------------------------------------------------------------------- * Functions *-----------------------------------------------------------------------------*/ -void * OsaMalloc(unsigned int size) { +void *OsaMalloc(unsigned int size) +{ return malloc(size); } -void *OsaFree(void *pbuf) { +void *OsaFree(void *pbuf) +{ if (pbuf != NULL) { free(pbuf); pbuf = NULL; } + return pbuf; } /* // $$$ //----------------------------------------------------------------------------- - // Function Name : InitTimerData + // Function Name : InitTimerData // Detail Description : This function initialises the timer data structure. // // Return Data Type : void @@ -72,7 +75,8 @@ void *OsaFree(void *pbuf) { //------------------------------------------------------------------------------ // $$$ */ -void InitTimerData(void) { +void InitTimerData(void) +{ Timer_data_t.stop_timer = FALSE; Timer_data_t.start_timer = FALSE; @@ -87,10 +91,10 @@ void InitTimerData(void) { /* // $$$ //----------------------------------------------------------------------------- - // Function Name : OsaTimerCreate - // Detail Description : This function creates the timer.It initialises the - // timer data and sets the callback function to be called - // upon timer expiry. + // Function Name : OsaTimerCreate + // Detail Description : This function creates the timer.It initialises the + // timer data and sets the callback function to be called + // upon timer expiry. // // Return Data Type : ErrorType. // @@ -98,8 +102,9 @@ void InitTimerData(void) { //------------------------------------------------------------------------------ // $$$ */ -int OsaTimerCreate(int* pTimerId, int periodic, int s32Time, - void (*entry)(void* data), void* pvAr) { +int OsaTimerCreate(int *pTimerId, int periodic, int s32Time, + void (*entry)(void *data), void *pvAr) +{ unsigned int uiDiv, uiRem; struct sigaction Action_t; @@ -170,51 +175,52 @@ int OsaTimerCreate(int* pTimerId, int periodic, int s32Time, sigaction(SIGALRM, &Action_t, NULL); return OSAL_OK; - } else { + } else return OSAL_ERROR; - } } /* // $$$ //----------------------------------------------------------------------------- - // Function Name : OsaTimerStart + // Function Name : OsaTimerStart // Detail Description : This function starts the already created timer . // - // Return Data Type : ErrorType. + // Return Data Type : ErrorType. / // Programming Note : None. //------------------------------------------------------------------------------ // $$$ */ -int OsaTimerStart(int iTimerId) { +int OsaTimerStart(int iTimerId) +{ /* The timer is started */ if (Timer_data_t.start_timer != TRUE) { if (setitimer(ITIMER_REAL, &Timer_data_t.iTval_t, NULL) < 0) { //PrintError("In OsaTimerStart() : OsaTimerStart failed \n "); return OSAL_ERROR; } + Timer_data_t.start_timer = TRUE; return OSAL_OK; - } else { + } else return OSAL_ERROR; - } } /* // $$$ //----------------------------------------------------------------------------- - // Function Name : OsaTimerStop + // Function Name : OsaTimerStop // Detail Description : This function stops the current running timer . // // Return Data Type : ErrorType // - // Programming Note : None. + // Programming Note : None. //------------------------------------------------------------------------------ // $$$ */ -int OsaTimerStop(int iTimerId) { +int OsaTimerStop(int iTimerId) +{ //struct sigaction Action_t; //Action_t.sa_flags = 0; struct itimerval trivial_it; /* OSAL_080918_1 */ @@ -232,24 +238,25 @@ int OsaTimerStop(int iTimerId) { /* * Add SIGALRM in the signal List */ - if(sigaddset(&(Action_t.sa_mask),SIGALRM) <0) - { + if (sigaddset(&(Action_t.sa_mask), SIGALRM) < 0) { PrintError("In OsaTimerStop() : Could Not Stop \n"); return OSAL_ERROR; } + /* * Block SIGALRM */ - if(sigprocmask(SIG_BLOCK,&(Action_t.sa_mask),NULL) <0) - { + if (sigprocmask(SIG_BLOCK, &(Action_t.sa_mask), NULL) < 0) { PrintError("In OsaTimerStop() : Could not mask the Signal \n"); return OSAL_ERROR; } + #endif /* OSAL_080918_1 : stop interval timer after alarm signal blocked */ trivial_it.it_value.tv_sec = 0; trivial_it.it_value.tv_usec = 0; + if (setitimer(ITIMER_REAL, &trivial_it, NULL) == -1) { //PrintError("OsaTimerStop failed\n "); return OSAL_ERROR; @@ -261,7 +268,7 @@ int OsaTimerStop(int iTimerId) { /* // $$$ //----------------------------------------------------------------------------- - // Function Name : OsaTimerDelete + // Function Name : OsaTimerDelete // Detail Description : This function deletes the current timer . // // Return Data Type : ErrorType @@ -270,7 +277,8 @@ int OsaTimerStop(int iTimerId) { //------------------------------------------------------------------------------ // $$$ */ -int OsaTimerDelete(int iTimerId) { +int OsaTimerDelete(int iTimerId) +{ //struct sigaction Action_t; struct itimerval iTmpval_t; @@ -292,6 +300,7 @@ int OsaTimerDelete(int iTimerId) { //PrintError("In OsaTimerDelete() : pOsaTimerDelete failed \n"); return -1; } + Timer_data_t.stop_timer = TRUE; Timer_data_t.start_timer = FALSE; return OSAL_OK; @@ -300,7 +309,7 @@ int OsaTimerDelete(int iTimerId) { /* // $$$ //----------------------------------------------------------------------------- - // Function Name : OsaTimerRestart + // Function Name : OsaTimerRestart // Detail Description : This function restarts the stopped timer . // // Return Data Type : ErrorType @@ -309,8 +318,10 @@ int OsaTimerDelete(int iTimerId) { //------------------------------------------------------------------------------ // $$$ */ -int OsaTimerRestart(int iTimerId) { +int OsaTimerRestart(int iTimerId) +{ struct sigaction Action_t; + if (Timer_data_t.stop_timer == TRUE) { //PrintError("In OsaTimerRestart() : Has been stopped forever \n"); return OSAL_ERROR; @@ -328,17 +339,19 @@ int OsaTimerRestart(int iTimerId) { sigaddset(&(Action_t.sa_mask), SIGALRM); Action_t.sa_flags = 0; + if (sigprocmask(SIG_UNBLOCK, &(Action_t.sa_mask), NULL) < 0) { //PrintError("In OsaTimerRestart() : Could Not Start Again \n"); return OSAL_ERROR; } + return OSAL_OK; } /* // $$$ //----------------------------------------------------------------------------- - // Function Name : timer_handler + // Function Name : timer_handler // Detail Description : Private timer_handler // // Return Data Type : ErrorType @@ -347,12 +360,14 @@ int OsaTimerRestart(int iTimerId) { //------------------------------------------------------------------------------ // $$$ */ -void timer_handler(int iJunk) { +void timer_handler(int iJunk) +{ (Timer_data_t.callback)(Timer_data_t.pvTmpdata); } -void OsaWait(int mSecs) { +void OsaWait(int mSecs) +{ unsigned int delay = mSecs * 1000; usleep(delay); } @@ -360,22 +375,23 @@ void OsaWait(int mSecs) { /* // $$$ //----------------------------------------------------------------------------- - // Function Name : OsaCurrentTime + // Function Name : OsaCurrentTime // Detail Description : This API returns the current time in _milliseconds_ - // using CPU timer + // using CPU timer // Return Data Type : ErrorType // // Programming Note : - // (Linux OsaCurrentTime, OsaCurrentTimeVal function) - // 1) kernel should support the high-resolution clocksource. - // Otherwise, the maximum resolution is only 1 or 10 mili sec. - // Default clocksource of arm linux kernel is system tick timer. - // 2) In some cases, time-warp can be occur. (time goes back in 10 milliseconds or less) - // When system lose the timer tick interrupt. + // (Linux OsaCurrentTime, OsaCurrentTimeVal function) + // 1) kernel should support the high-resolution clocksource. + // Otherwise, the maximum resolution is only 1 or 10 mili sec. + // Default clocksource of arm linux kernel is system tick timer. + // 2) In some cases, time-warp can be occur. (time goes back in 10 milliseconds or less) + // When system lose the timer tick interrupt. //------------------------------------------------------------------------------ // $$$ */ -unsigned int OsaCurrentTime(void) { +unsigned int OsaCurrentTime(void) +{ struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts); // SoC_D00003324 @@ -386,7 +402,7 @@ unsigned int OsaCurrentTime(void) { /* // $$$ //----------------------------------------------------------------------------- - // Function Name : OsaCurrentTimeVal + // Function Name : OsaCurrentTimeVal // Detail Description : This function returns the current timeval using CPU clock // Return Data Type : pOsaTime_t (1st argument) // @@ -394,7 +410,8 @@ unsigned int OsaCurrentTime(void) { //------------------------------------------------------------------------------ // $$$ */ -void OsaCurrentTimeVal(pOsaTime_t pTimeVal) { +void OsaCurrentTimeVal(pOsaTime_t pTimeVal) +{ struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts); // SoC_D00003324 @@ -407,7 +424,7 @@ void OsaCurrentTimeVal(pOsaTime_t pTimeVal) { /* // $$$ //----------------------------------------------------------------------------- - // Function Name : OsaDiffTimeVal + // Function Name : OsaDiffTimeVal // Detail Description : Subtract pVal2 to pVal1, stores result to pResult // Return Data Type : pOsaTime_t (1st argument) // @@ -416,10 +433,11 @@ void OsaCurrentTimeVal(pOsaTime_t pTimeVal) { // $$$ */ void OsaDiffTimeVal(pOsaTime_t pResult, const pOsaTime_t pVal1, - const pOsaTime_t pVal2) { + const pOsaTime_t pVal2) +{ /* no negative result */ if ((pVal2->Sec < pVal1->Sec) - || ((pVal1->Sec == pVal2->Sec) && (pVal2->NanoSec < pVal1->NanoSec))) { + || ((pVal1->Sec == pVal2->Sec) && (pVal2->NanoSec < pVal1->NanoSec))) { pResult->Sec = 0; pResult->NanoSec = 0; return; @@ -431,12 +449,12 @@ void OsaDiffTimeVal(pOsaTime_t pResult, const pOsaTime_t pVal1, if (pVal2->NanoSec < pVal1->NanoSec) { pResult->Sec--; pResult->NanoSec = pVal2->NanoSec + 1000000000 - pVal1->NanoSec; - } else { + } else pResult->NanoSec = pVal2->NanoSec - pVal1->NanoSec; - } } -int OsaGetTicksPerSecond(void) { +int OsaGetTicksPerSecond(void) +{ /* return OS Timer frequency(usually 100hz). */ return 100; } diff --git a/osal/OsaIpc.c b/osal/OsaIpc.c index 74660a9..07d2f89 100644 --- a/osal/OsaIpc.c +++ b/osal/OsaIpc.c @@ -26,7 +26,8 @@ *-----------------------------------------------------------------------------*/ #include "OsaLinuxUser.h" -key_t OsaGetKey(const char pcName[10]) { +key_t OsaGetKey(const char pcName[10]) +{ uid_t uid; key_t key; int i, len, acc; @@ -43,6 +44,7 @@ key_t OsaGetKey(const char pcName[10]) { acc <<= 1; acc = acc + aName[i]; } + key = (uid << 16) | (acc & 0x0000FFFF); return (key); @@ -50,56 +52,55 @@ key_t OsaGetKey(const char pcName[10]) { /* // ------------------------------------------------------------------- - // Shared Memory Implementation + // Shared Memory Implementation // ------------------------------------------------------------------- */ int OsaShmCreate(const char pcName[10], unsigned int u32ShmSegSize, - int* ps32ShmId) { + int *ps32ShmId) +{ key_t key = (key_t)OsaGetKey(pcName); - if (NULL == ps32ShmId) { + if (NULL == ps32ShmId) return OSAL_ERROR; - } *ps32ShmId = shmget(key, u32ShmSegSize, IPC_CREAT | IPC_EXCL | 0666); + if (*ps32ShmId == -1) { if (errno == EEXIST) { *ps32ShmId = shmget(key, u32ShmSegSize, 0666); return OSAL_EXIST; - } else { + } else return OSAL_ERROR; - } } return OSAL_OK; } -int OsaShmDelete(int s32ShmId) { - if (shmctl(s32ShmId, IPC_RMID, NULL) == -1) { +int OsaShmDelete(int s32ShmId) +{ + if (shmctl(s32ShmId, IPC_RMID, NULL) == -1) return OSAL_ERROR; - } return OSAL_OK; } -int OsaShmAttach(int s32ShmId, void** pShmAddr) { - if (NULL == pShmAddr) { +int OsaShmAttach(int s32ShmId, void **pShmAddr) +{ + if (NULL == pShmAddr) return OSAL_ERROR; - } - *pShmAddr = (void*)shmat(s32ShmId, NULL, 0); + *pShmAddr = (void *)shmat(s32ShmId, NULL, 0); - if (*pShmAddr == (void*)-1) { + if (*pShmAddr == (void *) - 1) return OSAL_ERROR; - } return OSAL_OK; } -int OsaShmDetach(const void *pShmAddr) { - if (NULL == pShmAddr) { +int OsaShmDetach(const void *pShmAddr) +{ + if (NULL == pShmAddr) return OSAL_ERROR; - } if (shmdt(pShmAddr) == -1) { //PrintError("Error in Detaching"); @@ -111,7 +112,7 @@ int OsaShmDetach(const void *pShmAddr) { /* // ------------------------------------------------------------------- - // Semaphore Implementation(System V) + // Semaphore Implementation(System V) // ------------------------------------------------------------------- */ union semun { @@ -137,12 +138,14 @@ typedef struct { #define MAX_NAMEDSEM_MGR 256 static int UlOsaNamedSemCreate(const char pcName[10], int iCount, - int iAttribute, void **puiSmid) { + int iAttribute, void **puiSmid) +{ int iRetVal = OSAL_OK; - UlOsaSem_t* sem; + UlOsaSem_t *sem; key_t semKey; - sem = (UlOsaSem_t*)malloc(sizeof(*sem)); + sem = (UlOsaSem_t *)malloc(sizeof(*sem)); + if (!sem) { //PrintError ("UlOsaSemCreate, Out of memory!\n"); return OSAL_ERROR; @@ -156,6 +159,7 @@ static int UlOsaNamedSemCreate(const char pcName[10], int iCount, union semun semUnion; semUnion.val = iCount; + if (semctl(sem->iSemId, 0, SETVAL, semUnion) == -1) { semctl(sem->iSemId, 0, IPC_RMID, NULL); free(sem); @@ -177,46 +181,42 @@ static int UlOsaNamedSemCreate(const char pcName[10], int iCount, sem->iCount = iCount; sem->semKey = semKey; - memcpy((void*)sem->bName, (const void*)pcName, (size_t)10); + memcpy((void *)sem->bName, (const void *)pcName, (size_t)10); sem->bName[10] = '\0'; - *puiSmid = (void*)sem; + *puiSmid = (void *)sem; return iRetVal; } -#if 0 // unused funciton ����. +#if 0 // unused funciton ����. static int UlOsaNamedSemDelete(unsigned int uiSmid) { - UlOsaSem_t *sem = (UlOsaSem_t*)uiSmid; + UlOsaSem_t *sem = (UlOsaSem_t *)uiSmid; if (!sem) - { return OSAL_ERROR; - } - if (semctl (sem->iSemId, 0, IPC_RMID, NULL) == -1) - { + if (semctl(sem->iSemId, 0, IPC_RMID, NULL) == -1) return OSAL_ERROR; - } - free((void*)uiSmid); + free((void *)uiSmid); return OSAL_OK; } #endif -static int UlOsaNamedSemGet(void *uiSmid, int iFlags, int iTimeout) { +static int UlOsaNamedSemGet(void *uiSmid, int iFlags, int iTimeout) +{ struct sembuf semBuf; struct timespec ts; struct timeval tv; int ret; - UlOsaSem_t *sem = (UlOsaSem_t*)uiSmid; + UlOsaSem_t *sem = (UlOsaSem_t *)uiSmid; - if (!sem) { + if (!sem) return OSAL_ERROR; - } if (iFlags == OSAL_SEM_NOWAIT) { semBuf.sem_num = 0; @@ -243,10 +243,12 @@ static int UlOsaNamedSemGet(void *uiSmid, int iFlags, int iTimeout) { gettimeofday(&tv, NULL); tv.tv_sec += iTimeout / 1000000; tv.tv_usec += iTimeout % 1000000; + if (tv.tv_usec >= 1000000) { tv.tv_sec += tv.tv_usec / 1000000; tv.tv_usec %= 1000000; } + ts.tv_sec = tv.tv_sec; ts.tv_nsec = tv.tv_usec * 1000; @@ -262,7 +264,7 @@ static int UlOsaNamedSemGet(void *uiSmid, int iFlags, int iTimeout) { //PrintError ("UlOsaSemGet-nowait: now locked, failed to get.\n"); return OSAL_ERROR; } else if (iTimeout > 0 && errno == EAGAIN) { - //PrintError ("UlOsaSemGet-timeout(%s): time-out\n", sem->bName); + //PrintError ("UlOsaSemGet-timeout(%s): time-out\n", sem->bName); return OSAL_ERR_TIMEOUT; } else { //PrintError ("UlOsaSemGet error, errno=%d\n", errno); @@ -271,13 +273,13 @@ static int UlOsaNamedSemGet(void *uiSmid, int iFlags, int iTimeout) { } } -static int UlOsaNamedSemRelease(void *uiSmid) { - UlOsaSem_t *sem = (UlOsaSem_t*)uiSmid; +static int UlOsaNamedSemRelease(void *uiSmid) +{ + UlOsaSem_t *sem = (UlOsaSem_t *)uiSmid; struct sembuf semBuf; - if (!sem) { + if (!sem) return OSAL_ERROR; - } //PrintDbg ("UlOsaSemRelease(%s)\n", sem->bName); semBuf.sem_num = 0; @@ -287,21 +289,21 @@ static int UlOsaNamedSemRelease(void *uiSmid) { if (semop(sem->iSemId, &semBuf, 1) == -1) { //PrintError ("UlOsaSemRelease(%s) error! errno=%d.\n", sem->bName, errno); return OSAL_ERROR; - } else { + } else return OSAL_OK; - } } -static int UlOsaNamedSemReset(void *uiSmid) { - UlOsaSem_t *sem = (UlOsaSem_t*)uiSmid; +static int UlOsaNamedSemReset(void *uiSmid) +{ + UlOsaSem_t *sem = (UlOsaSem_t *)uiSmid; union semun semUnion; - if (!sem) { + if (!sem) return OSAL_ERROR; - } //PrintDbg ("UlOsaSemReset(%s).\n", sem->bName); semUnion.val = sem->iCount; + if (semctl(sem->iSemId, 0, SETVAL, semUnion) == -1) { //PrintError ("UlOsaSemReset, semctl Failed!\n"); return OSAL_ERROR; @@ -310,14 +312,16 @@ static int UlOsaNamedSemReset(void *uiSmid) { return OSAL_OK; } -static int UlOsaNamedSemGetval(void *uiSmid) { - UlOsaSem_t *sem = (UlOsaSem_t*)uiSmid; +static int UlOsaNamedSemGetval(void *uiSmid) +{ + UlOsaSem_t *sem = (UlOsaSem_t *)uiSmid; int n; - if (!sem) { + if (!sem) return OSAL_ERROR; - } + n = semctl(sem->iSemId, 0, GETVAL, NULL); + if (n == -1) { //PrintError ("UlOsaSemGetval, semctl Failed!\n"); return OSAL_ERROR; @@ -328,28 +332,35 @@ static int UlOsaNamedSemGetval(void *uiSmid) { } int OsaNamedSemCreate(const char bName[10], int iCount, int iAttribute, - void **puiSmid) { + void **puiSmid) +{ return UlOsaNamedSemCreate(bName, iCount, iAttribute, puiSmid); } -int OsaNamedSemGet(void *uiSmid, int iFlags, int iTimeout) { +int OsaNamedSemGet(void *uiSmid, int iFlags, int iTimeout) +{ return UlOsaNamedSemGet(uiSmid, iFlags, iTimeout); } -int OsaNamedSemRelease(void *uiSmid) { +int OsaNamedSemRelease(void *uiSmid) +{ return UlOsaNamedSemRelease(uiSmid); } -int OsaNamedSemDelete(void *uiSmid) { // Deleting Semaphore : Never USE!!! - junhyeong.kim, sukki.min 12.04.20 -//return UlOsaNamedSemDelete (uiSmid); +int OsaNamedSemDelete(void + *uiSmid) // Deleting Semaphore : Never USE!!! - junhyeong.kim, sukki.min 12.04.20 +{ + //return UlOsaNamedSemDelete (uiSmid); return -1; } -int OsaNamedSemGetval(void *uiSmid) { +int OsaNamedSemGetval(void *uiSmid) +{ return UlOsaNamedSemGetval(uiSmid); } -int OsaNamedSemReset(void *uiSmid) { +int OsaNamedSemReset(void *uiSmid) +{ return UlOsaNamedSemReset(uiSmid); } diff --git a/osal/OsaQueue.c b/osal/OsaQueue.c index ec9cc1f..ac5567b 100644 --- a/osal/OsaQueue.c +++ b/osal/OsaQueue.c @@ -51,14 +51,14 @@ typedef struct _osaMsgBuf_t { // $$$ */ int OsaQueueCreate(const char bName[10], unsigned int uiFlags, - unsigned int uiMaxnum, unsigned int uiMaxlen, unsigned int* puiQid) { + unsigned int uiMaxnum, unsigned int uiMaxlen, unsigned int *puiQid) +{ #ifndef __NO_OS__ #ifdef POSIX_QUEUE struct mq_attr MqAttr; mqd_t QuId; - if (puiQid == NULL) - { + if (puiQid == NULL) { PrintDbg("Null Argument(s) \n"); return OSAL_ERROR; } @@ -66,11 +66,11 @@ int OsaQueueCreate(const char bName[10], unsigned int uiFlags, MqAttr.mq_maxmsg = uiMaxnum; MqAttr.mq_msgsize = uiMaxlen; - if((QuId = mq_open(bName, uiFlags, 0 ,&MqAttr)) < 0) - { + if ((QuId = mq_open(bName, uiFlags, 0, &MqAttr)) < 0) { perror("mq_open() Failed: \n"); return errno; } + *puiQid = (unsigned int)QuId; #else /* SYS5 MSG QUEUE*/ /* @@ -78,17 +78,15 @@ int OsaQueueCreate(const char bName[10], unsigned int uiFlags, */ struct msqid_ds tSetMqAttr; - if (uiMaxnum == OSAL_DEFAULT_Q_NUM || uiMaxnum > MAXNBM) { + if (uiMaxnum == OSAL_DEFAULT_Q_NUM || uiMaxnum > MAXNBM) uiMaxnum = MAXNBM; - } - if (uiMaxlen == OSAL_DEFAULT_Q_LEN || uiMaxlen > MAXML) { + if (uiMaxlen == OSAL_DEFAULT_Q_LEN || uiMaxlen > MAXML) uiMaxlen = MAXML; - } *puiQid = msgget(IPC_PRIVATE/*OsaGetKeyMQ(bName)*/, (int)uiFlags); - if (((int)*puiQid) == -1) //IPC_CREATE - { + + if (((int)*puiQid) == -1) { //IPC_CREATE perror("In OsaQueueCreate() : msgget: msgget failed"); //PrintError("In OsaQueueCreate() : Error no. : %d\n",errno); return ((int)errno); @@ -128,17 +126,19 @@ int OsaQueueCreate(const char bName[10], unsigned int uiFlags, //--------------------------------------------------------------------------------- // $$$ */ -int OsaQueueClose(unsigned int uiQid) { +int OsaQueueClose(unsigned int uiQid) +{ #ifdef POSIX_QUEUE - if (mq_close((mqd_t)uiQid) < 0) - { + + if (mq_close((mqd_t)uiQid) < 0) { perror("mq_close() Failed :\n"); return ((int)errno); } + return OSAL_OK; #endif return OSAL_OK; -}/*OsaQueueClose*/ +} /*OsaQueueClose*/ /* // $$$ @@ -153,14 +153,16 @@ int OsaQueueClose(unsigned int uiQid) { //--------------------------------------------------------------------------------- // $$$ */ -int OsaQueueDelete(const char* bName, unsigned int uiQid) { +int OsaQueueDelete(const char *bName, unsigned int uiQid) +{ #ifndef __NO_OS__ #ifdef POSIX_QUEUE - if (mq_unlink(bName) < 0) - { + + if (mq_unlink(bName) < 0) { perror("mq_unlink() Failed :\n"); return ((int)errno); } + #else /*SYS5 MSG QUEUE*/ // bName is not applicable in this case @@ -188,8 +190,8 @@ int OsaQueueDelete(const char* bName, unsigned int uiQid) { //-------------------------------------------------------------------------------- // $$$ */ -int OsaQueueSend(unsigned int uiQid, unsigned int uiFlags, void* pvMsg_buf, - unsigned int uiMsgLen, unsigned int uiPriority, pOsaTime_t ptTimeOut) +int OsaQueueSend(unsigned int uiQid, unsigned int uiFlags, void *pvMsg_buf, + unsigned int uiMsgLen, unsigned int uiPriority, pOsaTime_t ptTimeOut) { #ifndef __NO_OS__ @@ -198,19 +200,17 @@ int OsaQueueSend(unsigned int uiQid, unsigned int uiFlags, void* pvMsg_buf, unsigned int err_no; struct mq_attr tMqAttr; - if (uiFlags & OSAL_Q_NOWAIT) - { + if (uiFlags & OSAL_Q_NOWAIT) { tMqAttr.mq_flags = O_NONBLOCK; - if((err_no = mq_setattr((mqd_t)uiQid, &tMqAttr, (struct mq_attr *)NULL)) < 0) - { + if ((err_no = mq_setattr((mqd_t)uiQid, &tMqAttr, (struct mq_attr *)NULL)) < 0) { ////PrintError("mq_setattr(): mq_setattr() Failed errno=%d\n",err_no,0,0,0,0,0); //COMMON_071024_1 return (OSAL_ERROR); } } - if ((err_no=mq_send((mqd_t)uiQid, (const char *)pvMsg_buf, uiMsgLen, uiPriority)) < 0) - { + if ((err_no = mq_send((mqd_t)uiQid, (const char *)pvMsg_buf, uiMsgLen, + uiPriority)) < 0) { ////PrintError("mq_send():Failed errno=%d qid=%x flag=%d\n",err_no,uiQid,uiFlags,0,0,0); //COMMON_071024_1 return (OSAL_ERROR); } @@ -234,12 +234,14 @@ int OsaQueueSend(unsigned int uiQid, unsigned int uiFlags, void* pvMsg_buf, /* OSAL_080714 */ ret = OSAL_FAILURE_RETRY( - msgsnd((int )uiQid, (void* )&osaMsg, uiMsgLen, (int )uiFlags)); + msgsnd((int)uiQid, (void *)&osaMsg, uiMsgLen, (int)uiFlags)); + if (ret != 0) { //perror("In OsaQueueSend () : msgsnd failed"); //COMMON_071024_1 ////PrintError("In OsaQueueSend() : Error no. : %d\n",errno); //COMMON_071024_1 return ((int)errno); } + #endif #endif /* EOF POSIX_QUEUE */ @@ -259,8 +261,8 @@ int OsaQueueSend(unsigned int uiQid, unsigned int uiFlags, void* pvMsg_buf, //-------------------------------------------------------------------------------- // $$$ */ -int OsaQueueReceive(unsigned int uiQid, unsigned int uiFlags, void* pvMsgBuf, - unsigned int uiBufLen, unsigned int* pMsgLen, pOsaTime_t ptTimeOut) +int OsaQueueReceive(unsigned int uiQid, unsigned int uiFlags, void *pvMsgBuf, + unsigned int uiBufLen, unsigned int *pMsgLen, pOsaTime_t ptTimeOut) { #ifndef __NO_OS__ @@ -272,37 +274,33 @@ int OsaQueueReceive(unsigned int uiQid, unsigned int uiFlags, void* pvMsgBuf, struct timespec absTimeOut; #endif struct mq_attr tMqAttr; - int uiMsgLen =0; + int uiMsgLen = 0; if (uiFlags & OSAL_Q_NOWAIT) - { tMqAttr.mq_flags = O_NONBLOCK; - } + else - { tMqAttr.mq_flags = 0; - } + #if 0 - if (ptTimeOut && (!(uiFlags & OSAL_Q_NOWAIT))) - { + + if (ptTimeOut && (!(uiFlags & OSAL_Q_NOWAIT))) { absTimeOut.tv_sec = ptTimeOut->Sec; absTimeOut.tv_nsec = ptTimeOut->NanoSec; - } - else - { + } else { absTimeOut.tv_sec = 0; absTimeOut.tv_nsec = 0; } + #endif - if(mq_setattr((mqd_t)uiQid, &tMqAttr, (struct mq_attr *)NULL) < 0) - { + + if (mq_setattr((mqd_t)uiQid, &tMqAttr, (struct mq_attr *)NULL) < 0) { perror("OsaQueueSend(): mq_setattr() Failed \n"); return ((int)errno); } if ((uiMsgLen = mq_receive((mqd_t)uiQid, (char *)pvMsgBuf, uiBufLen, - (unsigned int *)NULL)) < 0) - { + (unsigned int *)NULL)) < 0) { perror("OsaQueueReceive(): mq_receive() Failed \n"); return ((int)errno); } @@ -323,7 +321,7 @@ int OsaQueueReceive(unsigned int uiQid, unsigned int uiFlags, void* pvMsgBuf, */ /* OSAL_080714 */ iRet = OSAL_FAILURE_RETRY( - msgrcv((int)uiQid,(void*)&osaMsg, uiBufLen,OSAL_ZERO , (int)uiFlags)); + msgrcv((int)uiQid, (void *)&osaMsg, uiBufLen, OSAL_ZERO, (int)uiFlags)); if (iRet == -1) { *pMsgLen = OSAL_ZERO; @@ -333,11 +331,13 @@ int OsaQueueReceive(unsigned int uiQid, unsigned int uiFlags, void* pvMsgBuf, perror("In OsaQueueReceive() : msgrcv failed"); //PrintError("In OsaQueueReceive() : Msg id %d, Error no. : %d\n", uiQid, errno); } + return ((int)errno); } else { *pMsgLen = iRet; memcpy(pvMsgBuf, (void *)osaMsg.msgBuf, (unsigned int)iRet); } + #endif #endif /* EOF POSIX_QUEUE */ return OSAL_OK; @@ -356,19 +356,18 @@ int OsaQueueReceive(unsigned int uiQid, unsigned int uiFlags, void* pvMsgBuf, //--------------------------------------------------------------------------------- // $$$ */ -int OsaQueueGetinfo(unsigned int uiQid, void* pvBuf) { +int OsaQueueGetinfo(unsigned int uiQid, void *pvBuf) +{ #ifndef __NO_OS__ #ifdef POSIX_QUEUE struct mq_attr tMqAttr; - if (pvBuf == NULL) - { + if (pvBuf == NULL) { //PrintError("Null Argument(s) \n"); return OSAL_ERROR; } - if (mq_getattr((mqd_t)uiQid, &tMqAttr) , 0) - { + if (mq_getattr((mqd_t)uiQid, &tMqAttr), 0) { perror("OsaQueueGetinfo(): mq_getattr() Failed \n"); return ((int)errno); } @@ -381,11 +380,10 @@ int OsaQueueGetinfo(unsigned int uiQid, void* pvBuf) { struct msqid_ds buf; struct QueueInfo *data_t; - if (pvBuf == NULL) { + if (pvBuf == NULL) return OSAL_ERROR; - } - data_t = (struct QueueInfo*)pvBuf; + data_t = (struct QueueInfo *)pvBuf; if (msgctl((int)uiQid, IPC_STAT, &buf) < 0) { perror("In OsaQueueGetinfo() : msgctl: msgctl failed"); @@ -420,36 +418,35 @@ int OsaQueueGetinfo(unsigned int uiQid, void* pvBuf) { //--------------------------------------------------------------------------------- // $$$ */ -int OsaQueueSetinfo(unsigned int uiQid, void* pvBuf) { +int OsaQueueSetinfo(unsigned int uiQid, void *pvBuf) +{ #ifndef __NO_OS__ #ifdef POSIX_QUEUE struct mq_attr tMqAttr; - if (pvBuf == NULL) - { + if (pvBuf == NULL) { //PrintError("Null Argument(s) \n"); return OSAL_ERROR; } tMqAttr.mq_flags = ((pOsaQueueInfo_t)pvBuf)->flags; - if (mq_setattr((mqd_t)uiQid, &tMqAttr, (struct mq_attr *)NULL) , 0) - { + if (mq_setattr((mqd_t)uiQid, &tMqAttr, (struct mq_attr *)NULL), 0) { perror("OsaQueueGetinfo(): mq_getattr() Failed \n"); return ((int)errno); } + #else /*SYS5 MSG QUEUE*/ struct msqid_ds buf; struct QueueInfo *data_t; - if (pvBuf == NULL) { + if (pvBuf == NULL) return OSAL_ERROR; - } - data_t = (struct QueueInfo*)pvBuf; + data_t = (struct QueueInfo *)pvBuf; - if(msgctl((int)uiQid, IPC_STAT, &buf) < 0) { + if (msgctl((int)uiQid, IPC_STAT, &buf) < 0) { perror("In OsaQueueGetinfo() : msgctl: msgctl failed"); return ((int)errno); } @@ -461,6 +458,7 @@ int OsaQueueSetinfo(unsigned int uiQid, void* pvBuf) { //PrintError("In OsaQueueGetinfo() : Error no. : %d\n",errno); return ((int)errno); } + #endif #endif /* EOF POSIX_QUEUE */ diff --git a/osal/OsaSem.c b/osal/OsaSem.c index 81f525a..710a009 100644 --- a/osal/OsaSem.c +++ b/osal/OsaSem.c @@ -42,10 +42,12 @@ typedef struct _UlOsaSem { /* TODO: apply iAttribute */ // COMMON_071008_1 static int UlOsaSemCreate(const char bName[10], int iCount, int iAttribute, - void **puiSmid) { - UlOsaSem_t* sem; + void **puiSmid) +{ + UlOsaSem_t *sem; + + sem = (UlOsaSem_t *)malloc(sizeof(*sem)); - sem = (UlOsaSem_t*)malloc(sizeof(*sem)); if (!sem) { //PrintError ("UlOsaSemCreate, Out of memory!\n"); return OSAL_ERROR; @@ -60,20 +62,20 @@ static int UlOsaSemCreate(const char bName[10], int iCount, int iAttribute, sem->iAttribute = iAttribute; sem->iCount = iCount; - memcpy((void*)sem->bName, (const void*)bName, (size_t)10); + memcpy((void *)sem->bName, (const void *)bName, (size_t)10); sem->bName[10] = '\0'; - *puiSmid = (void*)sem; + *puiSmid = (void *)sem; return OSAL_OK; } -static int UlOsaSemDelete(void *uiSmid) { - UlOsaSem_t *sem = (UlOsaSem_t*)uiSmid; +static int UlOsaSemDelete(void *uiSmid) +{ + UlOsaSem_t *sem = (UlOsaSem_t *)uiSmid; - if (!sem) { + if (!sem) return OSAL_ERROR; - } sem_destroy(&sem->sem); free(sem); @@ -81,13 +83,13 @@ static int UlOsaSemDelete(void *uiSmid) { return OSAL_OK; } -static int UlOsaSemGet(void *uiSmid, int iFlags, int iTimeout) { +static int UlOsaSemGet(void *uiSmid, int iFlags, int iTimeout) +{ int ret; - UlOsaSem_t *sem = (UlOsaSem_t*)uiSmid; + UlOsaSem_t *sem = (UlOsaSem_t *)uiSmid; - if (!sem) { + if (!sem) return OSAL_ERROR; - } if (iFlags == OSAL_SEM_NOWAIT) { /* no wait */ @@ -104,32 +106,33 @@ static int UlOsaSemGet(void *uiSmid, int iFlags, int iTimeout) { //PrintError ("UlOsaSemGet-timeout: invalid arg!\n"); return OSAL_ERROR; } + #if 0 struct timeval tv; - gettimeofday (&tv, NULL); + gettimeofday(&tv, NULL); tv.tv_sec += iTimeout / 1000000; tv.tv_usec += iTimeout % 1000000; - if (tv.tv_usec >= 1000000) - { + + if (tv.tv_usec >= 1000000) { tv.tv_sec += tv.tv_usec / 1000000; tv.tv_usec %= 1000000; } + ts.tv_sec = tv.tv_sec; ts.tv_nsec = tv.tv_usec * 1000; - ret = OSAL_FAILURE_RETRY(sem_timedwait (&sem->sem, &ts)); + ret = OSAL_FAILURE_RETRY(sem_timedwait(&sem->sem, &ts)); #endif - do // SoC_D00003324 - { + + do { // SoC_D00003324 ret = sem_trywait(&sem->sem); + if (ret != 0 && errno == EAGAIN) { usleep(10000); iTimeout -= 10000; - } else // No-Error + Error without EAGAIN - { + } else // No-Error + Error without EAGAIN break; - } } while (iTimeout > 0); } @@ -139,10 +142,11 @@ static int UlOsaSemGet(void *uiSmid, int iFlags, int iTimeout) { return OSAL_OK; } else { if (iFlags == OSAL_SEM_NOWAIT && errno == EAGAIN) { -// PrintError ("UlOsaSemGet-nowait: now locked, failed to get.\n"); + // PrintError ("UlOsaSemGet-nowait: now locked, failed to get.\n"); return OSAL_ERROR; - } else if (iFlags == OSAL_SEM_WAIT && iTimeout <= 0) {// Before : cond - (iTimeout > 0 && errno == ETIMEDOUT) - //PrintError ("UlOsaSemGet-timeout(%s): time-out\n",sem->bName); + } else if (iFlags == OSAL_SEM_WAIT && + iTimeout <= 0) {// Before : cond - (iTimeout > 0 && errno == ETIMEDOUT) + //PrintError ("UlOsaSemGet-timeout(%s): time-out\n",sem->bName); return OSAL_ERR_TIMEOUT; } else { //PrintError ("UlOsaSemGet error, errno=%d\n", errno); @@ -151,26 +155,28 @@ static int UlOsaSemGet(void *uiSmid, int iFlags, int iTimeout) { } } -static int UlOsaSemRelease(void *uiSmid) { - UlOsaSem_t *sem = (UlOsaSem_t*)uiSmid; - if (!sem) { +static int UlOsaSemRelease(void *uiSmid) +{ + UlOsaSem_t *sem = (UlOsaSem_t *)uiSmid; + + if (!sem) return OSAL_ERROR; - } //PrintDbg ("UlOsaSemRelease(%s)\n", sem->bName); if (sem_post(&sem->sem) != 0) { //PrintError ("UlOsaSemRelease(%s) error! errno=%d.\n", sem->bName, errno); return OSAL_ERROR; - } else { + } else return OSAL_OK; - } } -static int UlOsaSemReset(void *uiSmid) { - UlOsaSem_t *sem = (UlOsaSem_t*)uiSmid; - if (!sem) { +static int UlOsaSemReset(void *uiSmid) +{ + UlOsaSem_t *sem = (UlOsaSem_t *)uiSmid; + + if (!sem) return OSAL_ERROR; - } + //PrintDbg ("UlOsaSemReset(%s).\n", sem->bName); /* For threads currently blocked, the effect of destroying is not defined in POSIX. @@ -186,12 +192,13 @@ static int UlOsaSemReset(void *uiSmid) { return OSAL_OK; } -static int UlOsaSemGetval(void *uiSmid) { - UlOsaSem_t *sem = (UlOsaSem_t*)uiSmid; +static int UlOsaSemGetval(void *uiSmid) +{ + UlOsaSem_t *sem = (UlOsaSem_t *)uiSmid; int n; - if (!sem) { + + if (!sem) return OSAL_ERROR; - } if (sem_getvalue(&sem->sem, &n) != 0) { //PrintError ("UlOsaSemGetval(%s), sem_getvalue errno=%d\n", sem->bName, errno); @@ -203,88 +210,94 @@ static int UlOsaSemGetval(void *uiSmid) { } /** - * @brief Create a semaphore object. - * @remarks the semaphore value shall be incremented more than initial value. - * @param bName [in] semaphore name, for debugging. - * @param iCount [in] initial semaphore value - * @param iAttribute [in] not used now implementation. - * @param puiSmid [out] semaphore object ID. - * @retval [OSAL_OK | OSAL_ERROR] + * @brief Create a semaphore object. + * @remarks the semaphore value shall be incremented more than initial value. + * @param bName [in] semaphore name, for debugging. + * @param iCount [in] initial semaphore value + * @param iAttribute [in] not used now implementation. + * @param puiSmid [out] semaphore object ID. + * @retval [OSAL_OK | OSAL_ERROR] */ int OsaSemCreate(const char bName[10], int iCount, int iAttribute, - void **puiSmid) { + void **puiSmid) +{ /* OSA_070901 */ return UlOsaSemCreate(bName, iCount, iAttribute, puiSmid); } /** - * @brief Lock a semaphore. + * @brief Lock a semaphore. * @remarks perform a semaphore lock operation. - * @param uiSmid [in] semaphore object ID created by OsaSemCreate. - * @param iFlags [in] [OSAL_SEM_NOWAIT | OSAL_SEM_WAIT] + * @param uiSmid [in] semaphore object ID created by OsaSemCreate. + * @param iFlags [in] [OSAL_SEM_NOWAIT | OSAL_SEM_WAIT] - OSAL_SEM_NOWAIT : lock the semaphore only if the semaphore value is currently not locked. otherwise, return OSAL_ERROR. - OSAL_SEM_WAIT : lock the semaphore. If the semaphore values is currently locked, the calling thread shall be blocked. In linux kernel mode, iFlags is not used, always wait infinitely. - * @param iTimeout [in] timeout value in _microsecond_. + * @param iTimeout [in] timeout value in _microsecond_. Only affected when iFlags is OSAL_SEM_WAIT. - 0 : wait infinitely. - positive value : wait will be terminated when expires and return OSAL_ERR_TIMEOUT. - negative value : error. - * @retval [OSAL_OK | OSAL_ERROR | OSAL_ERR_TIMEOUT] + * @retval [OSAL_OK | OSAL_ERROR | OSAL_ERR_TIMEOUT] */ -int OsaSemGet(void *uiSmid, int iFlags, int iTimeout) { +int OsaSemGet(void *uiSmid, int iFlags, int iTimeout) +{ /* OSA_070901 */ return UlOsaSemGet(uiSmid, iFlags, iTimeout); } /** - * @brief Unlock a semaphore - * @remarks If the semaphore value is positive currently, then no threads were + * @brief Unlock a semaphore + * @remarks If the semaphore value is positive currently, then no threads were blocked by semaphore and simply semaphore value is incremented. - * @param uiSmid [id] semaphore object ID created by OsaSemCreate. - * @retval [OSAL_OK | OSAL_ERROR] + * @param uiSmid [id] semaphore object ID created by OsaSemCreate. + * @retval [OSAL_OK | OSAL_ERROR] */ -int OsaSemRelease(void *uiSmid) { +int OsaSemRelease(void *uiSmid) +{ /* OSA_070901 */ return UlOsaSemRelease(uiSmid); } /** - * @brief Destroy a semaphore object. - * @remarks It is unsafe if any thread is currently blocked by the semaphore. - * @param uiSmid [id] semaphore object ID created by OsaSemCreate. - * @retval [OSAL_OK | OSAL_ERROR] + * @brief Destroy a semaphore object. + * @remarks It is unsafe if any thread is currently blocked by the semaphore. + * @param uiSmid [id] semaphore object ID created by OsaSemCreate. + * @retval [OSAL_OK | OSAL_ERROR] */ -int OsaSemDelete(void *uiSmid) { +int OsaSemDelete(void *uiSmid) +{ /* OSA_070901 */ return UlOsaSemDelete(uiSmid); } /** - * @brief Get the current semaphore value - * @remarks If the semaphore is locked, returns zero or negative value. + * @brief Get the current semaphore value + * @remarks If the semaphore is locked, returns zero or negative value. Only implemented in linux user mode. - * @param uiSmid [id] semaphore object ID created by OsaSemCreate. - * @retval [OSAL_OK | OSAL_ERROR] + * @param uiSmid [id] semaphore object ID created by OsaSemCreate. + * @retval [OSAL_OK | OSAL_ERROR] */ -int OsaSemGetval(void *uiSmid) { +int OsaSemGetval(void *uiSmid) +{ /* OSA_070901 */ return UlOsaSemGetval(uiSmid); } /** - * @brief Reset the semaphore to initial state. - * @remarks Set the semaphore value to iCount that given by OsaSemCreate(). + * @brief Reset the semaphore to initial state. + * @remarks Set the semaphore value to iCount that given by OsaSemCreate(). Blocked threads by this semaphore shall not released if initial value was zero or negative. If initial value was positive, same number(semaphore value) of threads shall be released. Only implemented in linux user mode. - * @param uiSmid [id] semaphore object ID created by OsaSemCreate. - * @retval [OSAL_OK | OSAL_ERROR] + * @param uiSmid [id] semaphore object ID created by OsaSemCreate. + * @retval [OSAL_OK | OSAL_ERROR] */ -int OsaSemReset(void *uiSmid) { +int OsaSemReset(void *uiSmid) +{ /* OSA_070901 */ return UlOsaSemReset(uiSmid); } @@ -305,9 +318,10 @@ int OsaSemReset(void *uiSmid) { //------------------------------------------------------------------------------ // $$$ */ -int OsaMutCreate(const char bName[10], int iAttributes, void **puiMutid) { +int OsaMutCreate(const char bName[10], int iAttributes, void **puiMutid) +{ pthread_mutexattr_t attr_t; - pthread_mutex_t* pmutex_t; + pthread_mutex_t *pmutex_t; if (puiMutid == NULL) { //PrintError("In OsaMutCreate() : NULL PTR ERROR"); @@ -315,24 +329,27 @@ int OsaMutCreate(const char bName[10], int iAttributes, void **puiMutid) { } pmutex_t = (pthread_mutex_t *)malloc(sizeof(pthread_mutex_t)); + if (pmutex_t) { pthread_mutexattr_init(&attr_t); switch (iAttributes) { - case OSAL_MU_RECURSIVE: - pthread_mutexattr_settype(&attr_t, PTHREAD_MUTEX_RECURSIVE_NP); - pthread_mutex_init(pmutex_t, (const pthread_mutexattr_t *)&attr_t); - break; - case OSAL_MU_NOERROR: - pthread_mutexattr_settype(&attr_t, PTHREAD_MUTEX_ERRORCHECK_NP); - pthread_mutex_init(pmutex_t, (const pthread_mutexattr_t *)&attr_t); - break; - default: - pthread_mutex_init(pmutex_t, NULL); - break; + case OSAL_MU_RECURSIVE: + pthread_mutexattr_settype(&attr_t, PTHREAD_MUTEX_RECURSIVE_NP); + pthread_mutex_init(pmutex_t, (const pthread_mutexattr_t *)&attr_t); + break; + + case OSAL_MU_NOERROR: + pthread_mutexattr_settype(&attr_t, PTHREAD_MUTEX_ERRORCHECK_NP); + pthread_mutex_init(pmutex_t, (const pthread_mutexattr_t *)&attr_t); + break; + + default: + pthread_mutex_init(pmutex_t, NULL); + break; } - (*puiMutid) = (void*)pmutex_t; + (*puiMutid) = (void *)pmutex_t; pthread_mutexattr_destroy(&attr_t); } else { @@ -359,15 +376,17 @@ int OsaMutCreate(const char bName[10], int iAttributes, void **puiMutid) { //------------------------------------------------------------------------------ // $$$ */ -int OsaMutDelete(void *uiMutid) { +int OsaMutDelete(void *uiMutid) +{ int iRet; - pthread_mutex_t* pmutex_t = (pthread_mutex_t *)uiMutid; - if (pmutex_t == NULL) { + pthread_mutex_t *pmutex_t = (pthread_mutex_t *)uiMutid; + + if (pmutex_t == NULL) return OSAL_OK; - } iRet = pthread_mutex_destroy(pmutex_t); + if (iRet < 0) { perror("In OsaMutDelete() : failed "); //PrintError("Error no. : %d\n",errno); @@ -391,16 +410,19 @@ int OsaMutDelete(void *uiMutid) { //------------------------------------------------------------------------------ // $$$ */ -int OsaMutRelease(void *uiMutid) { +int OsaMutRelease(void *uiMutid) +{ int iRet; - pthread_mutex_t* pmutex_t = (pthread_mutex_t *)uiMutid; + pthread_mutex_t *pmutex_t = (pthread_mutex_t *)uiMutid; iRet = pthread_mutex_unlock(pmutex_t); + if (iRet < 0) { perror("In OsaMutRelease() : failed "); //PrintError("Error no. : %d\n",errno); return ((int)errno); } + return OSAL_OK; } @@ -416,15 +438,18 @@ int OsaMutRelease(void *uiMutid) { //------------------------------------------------------------------------------ // $$$ */ -int OsaMutGet(void *uiMutid, int iFlags, int iTimeout) { +int OsaMutGet(void *uiMutid, int iFlags, int iTimeout) +{ int iRet; - pthread_mutex_t* pmutex_t = (pthread_mutex_t *)uiMutid; + pthread_mutex_t *pmutex_t = (pthread_mutex_t *)uiMutid; iRet = pthread_mutex_lock(pmutex_t); + if (iRet < 0) { perror("In OsaMutGet() : failed "); //PrintError("Error no. : %d\n",errno); return ((int)errno); } + return OSAL_OK; } @@ -439,14 +464,16 @@ int OsaMutGet(void *uiMutid, int iFlags, int iTimeout) { // Programming Note : None. //------------------------------------------------------------------------------ // $$$ -int OsaMutTryGet(void *uiMutid, int iFlags, int iTimeout) { +int OsaMutTryGet(void *uiMutid, int iFlags, int iTimeout) +{ int iRet; - pthread_mutex_t* pmutex_t = (pthread_mutex_t *)uiMutid; + pthread_mutex_t *pmutex_t = (pthread_mutex_t *)uiMutid; iRet = pthread_mutex_trylock(pmutex_t); - if (iRet) { + + if (iRet) return ((int)iRet); - } + return OSAL_OK; } diff --git a/osal/OsaSignal.c b/osal/OsaSignal.c index 4d944fd..14edc56 100644 --- a/osal/OsaSignal.c +++ b/osal/OsaSignal.c @@ -31,92 +31,102 @@ /* // $$$ //----------------------------------------------------------------------------- - // Function Name : OsaSigProcmask + // Function Name : OsaSigProcmask // Detail Description : Examine and/or change the list of currently blocked - // signals + // signals // - // Return Data Type : ErrorType + // Return Data Type : ErrorType // // Programming Note : None. //------------------------------------------------------------------------------ // $$$ */ -int OsaSigProcmask(int iMode, const unsigned int* puiNewmask, - unsigned int* puiOldmask) { +int OsaSigProcmask(int iMode, const unsigned int *puiNewmask, + unsigned int *puiOldmask) +{ int iRet; iRet = sigprocmask(iMode, (const sigset_t *)puiNewmask, - (sigset_t*)puiOldmask); + (sigset_t *)puiOldmask); + if (iRet) { perror("SigProcMask: SigProcMask Failed "); //PrintError("Error No. : %d\n",errno); return ((int)errno); } + return OSAL_OK; } /* // $$$ //----------------------------------------------------------------------------- - // Function Name : OsaSigSuspend + // Function Name : OsaSigSuspend // Detail Description : Suspend the task until delivery of a signal // - // Return Data Type : ErroType + // Return Data Type : ErroType // - // Programming Note : None. + // Programming Note : None. //------------------------------------------------------------------------------ // $$$ */ -int OsaSigSuspend(unsigned int* puiPending) { +int OsaSigSuspend(unsigned int *puiPending) +{ int iRet; iRet = sigpending((sigset_t *)puiPending); + if (iRet) { perror("SigSuspend: SigSuspend INTR "); //PrintError("Error No. : %d\n",errno); return ((int)errno); } + return OSAL_OK; } /* // $$$ //----------------------------------------------------------------------------- - // Function Name : OsaSigTimedwait + // Function Name : OsaSigTimedwait // Detail Description : Wait for a signal // // Return Data Type : ErrorType // - // Programming Note : None. + // Programming Note : None. //------------------------------------------------------------------------------ // $$$ */ -int OsaSigTimedwait(void) { +int OsaSigTimedwait(void) +{ int iRet; iRet = pause(); + if (iRet) { perror("TimeWait: TimeWait INTR "); //PrintError("Error No. : %d\n",errno); return ((int)errno); } + return OSAL_OK; } /* // $$$ //----------------------------------------------------------------------------- - // Function Name : OsaSigSetmask + // Function Name : OsaSigSetmask // Detail Description : Set the signal mask // - // Return Data Type : ErrorType + // Return Data Type : ErrorType // // Programming Note : None. //------------------------------------------------------------------------------ // $$$ */ -int OsaSigSetmask(int iSigno) { +int OsaSigSetmask(int iSigno) +{ struct sigaction Action_t; Action_t.sa_flags = 0; @@ -126,13 +136,14 @@ int OsaSigSetmask(int iSigno) { //PrintError("Error No. : %d\n",errno); return ((int)errno); } + return OSAL_OK; } /* // $$$ //----------------------------------------------------------------------------- - // Function Name : OsaKill + // Function Name : OsaKill // Detail Description : Send a kill signal to a task // // Return Data Type : ErrorType @@ -141,10 +152,12 @@ int OsaSigSetmask(int iSigno) { //------------------------------------------------------------------------------ // $$$ */ -int OsaKill(int iId, int iSigno) { +int OsaKill(int iId, int iSigno) +{ if (kill(iId, iSigno) < 0) { perror("KILL: Error "); return OSAL_ERROR; } + return OSAL_OK; } diff --git a/osal/OsaTask.c b/osal/OsaTask.c index 1d939f7..afcc382 100644 --- a/osal/OsaTask.c +++ b/osal/OsaTask.c @@ -33,28 +33,30 @@ /*----------------------------------------------------------------------------- * Globals *-----------------------------------------------------------------------------*/ -typedef void (*pEntry_f)(void*); +typedef void (*pEntry_f)(void *); typedef struct { char aName[TASK_COMM_LEN]; pEntry_f pEntryFunc; - void* pArg; + void *pArg; } ThreadParam_t; /*----------------------------------------------------------------------------- * Functions *-----------------------------------------------------------------------------*/ -static void* _thread_start_handler(void* pArg) { +static void *_thread_start_handler(void *pArg) +{ int iRet; ThreadParam_t sThreadParam; - if (NULL == pArg) { + if (NULL == pArg) return 0; - } - sThreadParam = *((ThreadParam_t*)pArg); + + sThreadParam = *((ThreadParam_t *)pArg); free(pArg); iRet = prctl(PR_SET_NAME, sThreadParam.aName, 0, 0, 0); + if (iRet) { perror("In OsaTaskSpawn() : prctl() Failed\n "); //PrintError("In OsaTaskSpawn() : prctl() error no. : %d\n", iRet); @@ -67,20 +69,21 @@ static void* _thread_start_handler(void* pArg) { /* // $$$ //----------------------------------------------------------------------------- - // Function Name : OsaTaskSpawn + // Function Name : OsaTaskSpawn // Detail Description : This API function creates and activates a new task - // with a specified priority and options. It returns - // a system-assigned ID. + // with a specified priority and options. It returns + // a system-assigned ID. // // Return Data Type : ErrorType // - // Programming Note : None. + // Programming Note : None. //------------------------------------------------------------------------------ // $$$ */ -int OsaTaskSpawn(const char* pName, void **puiTid, int iPriority, - int iStacksize, unsigned int uiMode, void* pEntryPt, int iArg1, int iArg2, - int iArg3, int iArg4, void* pvArg5) { +int OsaTaskSpawn(const char *pName, void **puiTid, int iPriority, + int iStacksize, unsigned int uiMode, void *pEntryPt, int iArg1, int iArg2, + int iArg3, int iArg4, void *pvArg5) +{ #define OSAL_SCHED_POLICY SCHED_FIFO @@ -91,24 +94,24 @@ int OsaTaskSpawn(const char* pName, void **puiTid, int iPriority, struct sched_param param_t; int curThreadPolicy; int createThreadPolicy = OSAL_SCHED_POLICY; - ThreadParam_t* pThreadParam; + ThreadParam_t *pThreadParam; pthread_getschedparam(curThread, &curThreadPolicy, ¶m_t); - if (iPriority == 0) { + if (iPriority == 0) createThreadPolicy = SCHED_OTHER; - } else if (iPriority > sched_get_priority_max(OSAL_SCHED_POLICY)) { + + else if (iPriority > sched_get_priority_max(OSAL_SCHED_POLICY)) iPriority = sched_get_priority_max(OSAL_SCHED_POLICY); - } else if (iPriority < sched_get_priority_min(OSAL_SCHED_POLICY)) { + + else if (iPriority < sched_get_priority_min(OSAL_SCHED_POLICY)) iPriority = sched_get_priority_min(OSAL_SCHED_POLICY); - } - if (iStacksize < OSAL_DEFAULT_STSZ) { + if (iStacksize < OSAL_DEFAULT_STSZ) iStacksize = OSAL_DEFAULT_STSZ; - } /* set thread parameters: name, entry func and argument. */ - pThreadParam = (ThreadParam_t*)malloc(sizeof(ThreadParam_t)); + pThreadParam = (ThreadParam_t *)malloc(sizeof(ThreadParam_t)); if (NULL == pThreadParam) { perror("Memory Allocation Failed : \n"); @@ -123,7 +126,8 @@ int OsaTaskSpawn(const char* pName, void **puiTid, int iPriority, /* set stack size : 16Kbyte */ if (iStacksize <= PTHREAD_STACK_MIN) { iRet = pthread_create(&createThread, (pthread_attr_t *)NULL, - _thread_start_handler, (void*)pThreadParam); + _thread_start_handler, (void *)pThreadParam); + if (iRet) { *puiTid = NULL; free(pThreadParam); @@ -135,6 +139,7 @@ int OsaTaskSpawn(const char* pName, void **puiTid, int iPriority, // set Stakc size by iStacksize, using pthread_attr_t else { iRet = pthread_attr_init(&tattr_t); + if (iRet) { *puiTid = NULL; free(pThreadParam); @@ -142,7 +147,9 @@ int OsaTaskSpawn(const char* pName, void **puiTid, int iPriority, //PrintError("In OsaTaskSpawn() : error no. : %d\n", iRet); return ((int)iRet); } + iRet = pthread_attr_setstacksize(&tattr_t, (unsigned int)iStacksize); + if (iRet) { *puiTid = NULL; free(pThreadParam); @@ -151,8 +158,10 @@ int OsaTaskSpawn(const char* pName, void **puiTid, int iPriority, pthread_attr_destroy(&tattr_t); return ((int)iRet); } + iRet = pthread_create(&createThread, (pthread_attr_t *)&tattr_t, - _thread_start_handler, (void*)pThreadParam); + _thread_start_handler, (void *)pThreadParam); + if (iRet) { *puiTid = NULL; free(pThreadParam); @@ -169,6 +178,7 @@ int OsaTaskSpawn(const char* pName, void **puiTid, int iPriority, if (iPriority != param_t.__sched_priority) { param_t.__sched_priority = iPriority; iRet = pthread_setschedparam(createThread, createThreadPolicy, ¶m_t); + if (iRet) { *puiTid = NULL; perror("In OsaTaskSpawn() : pthread setschedparam Failed\n "); @@ -179,6 +189,7 @@ int OsaTaskSpawn(const char* pName, void **puiTid, int iPriority, } iRet = pthread_detach(createThread); + if (iRet) { *puiTid = NULL; perror("In OsaTaskSpawn() : pthread_detach Failed\n "); @@ -186,7 +197,8 @@ int OsaTaskSpawn(const char* pName, void **puiTid, int iPriority, pthread_kill(createThread, 0); return ((int)iRet); } - *puiTid = (void*)createThread; + + *puiTid = (void *)createThread; //PrintDbg("%s thread created policy: %d, priority %d\n", pName, createThreadPolicy, iPriority); return OSAL_OK; @@ -195,7 +207,7 @@ int OsaTaskSpawn(const char* pName, void **puiTid, int iPriority, /* // $$$ //----------------------------------------------------------------------------- - // Function Name : OsaExit + // Function Name : OsaExit // Detail Description : This function terminates the calling thread. // // Return Data Type : Void @@ -204,16 +216,17 @@ int OsaTaskSpawn(const char* pName, void **puiTid, int iPriority, //------------------------------------------------------------------------------ // $$$ */ -void OsaExit(int iStatus) { +void OsaExit(int iStatus) +{ pthread_exit(0); } /* // $$$ //----------------------------------------------------------------------------- - // Function Name : OsaTaskDelete + // Function Name : OsaTaskDelete // Detail Description : This function terminates the thread having the - // corresponding thread ID . + // corresponding thread ID . // // Return Data Type : ErrorType // @@ -221,21 +234,24 @@ void OsaExit(int iStatus) { //------------------------------------------------------------------------------ // $$$ */ -int OsaTaskDelete(void *uiTid) { +int OsaTaskDelete(void *uiTid) +{ int iRet = 0; iRet = pthread_cancel((pthread_t)uiTid); + if (iRet) { perror("In OsaTaskDelete() : TaskDelete Failed "); //PrintError("In OsaTaskDelete() : error no. : %d\n",errno); return ((int)errno); } + return OSAL_OK; } /* // $$$ //----------------------------------------------------------------------------- - // Function Name : OsaTaskSetPriority + // Function Name : OsaTaskSetPriority // Detail Description : This function sets the Priority for the created thread. // // Return Data Type : ErrorType @@ -244,7 +260,8 @@ int OsaTaskDelete(void *uiTid) { //------------------------------------------------------------------------------ // $$$ */ -int OsaTaskSetPriority(unsigned int uiTid, int iNewpriority) { +int OsaTaskSetPriority(unsigned int uiTid, int iNewpriority) +{ int iRet, iPolicy; struct sched_param param_t; /* sched_priority will be the priority of the thread */ @@ -253,11 +270,13 @@ int OsaTaskSetPriority(unsigned int uiTid, int iNewpriority) { iPolicy = SCHED_FIFO; /* scheduling parameters of target thread */ iRet = pthread_setschedparam(uiTid, iPolicy, ¶m_t); + if (iRet) { perror("In OsaTaskSetPriority() : TaskSetPriority set Failed "); //PrintError("In OsaTaskSetPriority() : error no. : %d\n",errno); return ((int)errno); } + return OSAL_OK; } @@ -266,25 +285,28 @@ int OsaTaskSetPriority(unsigned int uiTid, int iNewpriority) { //----------------------------------------------------------------------------- // Function Name : OsaTaskGetPriority // Detail Description : This function gets the corresponding priority of the - // supplied thread ID . + // supplied thread ID . // // Return Data Type : ErrorType // - // Programming Note : None. + // Programming Note : None. //------------------------------------------------------------------------------ // $$$ */ -int OsaTaskGetPriority(unsigned int uiTid, int* piPriority) { +int OsaTaskGetPriority(unsigned int uiTid, int *piPriority) +{ int iRet, iPolicy = 0; struct sched_param param_t; iRet = pthread_getschedparam(uiTid, (int *)&iPolicy, ¶m_t); + if (iRet) { piPriority = NULL; perror("In OsaTaskGetPriority() : TaskGetPriority Failed "); //PrintError("In OsaTaskGetPriority() : error no. : %d\n",errno); return ((int)errno); } + *piPriority = param_t.sched_priority; return OSAL_OK; } @@ -292,26 +314,26 @@ int OsaTaskGetPriority(unsigned int uiTid, int* piPriority) { /* // $$$ //----------------------------------------------------------------------------- - // Function Name : OsaTaskNanosleep + // Function Name : OsaTaskNanosleep // Detail Description : This function makes the thread to sleep for nanosec - // precision. + // precision. // - // Return Data Type : ErrorType + // Return Data Type : ErrorType // // Programming Note : None. //------------------------------------------------------------------------------ // $$$ */ -int OsaTaskNanosleep(int iNanosec) { +int OsaTaskNanosleep(int iNanosec) +{ struct timespec timeSpec_t, timeSpecRem_t; int iRetval; timeSpec_t.tv_sec = 0; - if (iNanosec > OSAL_NSEC_MAX) { + if (iNanosec > OSAL_NSEC_MAX) iNanosec = OSAL_NSEC_MAX; - } timeSpec_t.tv_nsec = iNanosec; @@ -330,33 +352,35 @@ int OsaTaskNanosleep(int iNanosec) { /* // $$$ //----------------------------------------------------------------------------- - // Function Name : OsaTaskGetId + // Function Name : OsaTaskGetId // Detail Description : This function get the current thread ID. // // Return Data Type : Current thread ID . // - // Programming Note : None. + // Programming Note : None. //------------------------------------------------------------------------------ // $$$ */ -int OsaTaskGetId(void) { +int OsaTaskGetId(void) +{ return ((pthread_t)pthread_self()); } /* // $$$ //----------------------------------------------------------------------------- - // Function Name : OsaTaskDelaymsecs + // Function Name : OsaTaskDelaymsecs // Detail Description : This function makes the thread to sleep for millisec - // precision. + // precision. // - // Return Data Type : ErrorType + // Return Data Type : ErrorType // - // Programming Note : None. + // Programming Note : None. //------------------------------------------------------------------------------ // $$$ */ -int OsaTaskDelaymsecs(unsigned int uiMsec) { +int OsaTaskDelaymsecs(unsigned int uiMsec) +{ unsigned int uiDiv, uiRem; struct timespec timeSpec_t, timeSpecRem_t; int iRetval; @@ -374,13 +398,15 @@ int OsaTaskDelaymsecs(unsigned int uiMsec) { //PrintError("In OsaTaskDelaymsecs() : error no. : %d\n",errno); return ((int)errno); } + return OSAL_OK; } /***************************************************************************** ** OsaTaskDelayticks - *****************************************************************************/ -int OsaTaskDelayticks(int iTicks) { +int OsaTaskDelayticks(int iTicks) +{ struct timespec ts, tsr; unsigned int hz; int ret; @@ -391,20 +417,23 @@ int OsaTaskDelayticks(int iTicks) { for (;;) { ret = nanosleep(&ts, &tsr); - if (ret == 0) { + + if (ret == 0) return OSAL_OK; - } else if (errno == EINTR) { + + else if (errno == EINTR) ts = tsr; - } else { + + else return OSAL_ERROR; - } } } /***************************************************************************** ** OsaTaskSuspend- *****************************************************************************/ -int OsaTaskSuspend(unsigned int uiTid) { +int OsaTaskSuspend(unsigned int uiTid) +{ //Not Implemented return OSAL_OK; @@ -413,7 +442,8 @@ int OsaTaskSuspend(unsigned int uiTid) { /***************************************************************************** ** OsaTaskResume- *****************************************************************************/ -int OsaTaskResume(unsigned int uiTid) { +int OsaTaskResume(unsigned int uiTid) +{ //Not Implemented return OSAL_OK; @@ -422,7 +452,8 @@ int OsaTaskResume(unsigned int uiTid) { /***************************************************************************** ** OsaTaskRestart- *****************************************************************************/ -int OsaTaskRestart(unsigned int uiTid) { +int OsaTaskRestart(unsigned int uiTid) +{ //Not Implemented return OSAL_OK; } diff --git a/ssflib/dep/cryptocore/include/base/cc_des.h b/ssflib/dep/cryptocore/include/base/cc_des.h index f3180f5..289f302 100644 --- a/ssflib/dep/cryptocore/include/base/cc_des.h +++ b/ssflib/dep/cryptocore/include/base/cc_des.h @@ -74,7 +74,7 @@ } /*! @brief encrypt one round */ -#define SDRM_D_ENCRYPT(L, R) { \ +#define SDRM_D_ENCRYPT(L, R) do { \ u = R ^ RoundKey[i][0]; \ t = R ^ RoundKey[i][1]; \ t = SDRM_rotr32(t, 4); \ @@ -86,7 +86,7 @@ SDRM_DES_SPtrans[3][(t >> 10U) & 0x3f]^ \ SDRM_DES_SPtrans[5][(t >> 18U) & 0x3f]^ \ SDRM_DES_SPtrans[7][(t >> 26U) & 0x3f]; \ -} +} while (0) //////////////////////////////////////////////////////////////////////////// // static values - specified in FIPS 46 diff --git a/ssflib/dep/cryptocore/include/drm_macro.h b/ssflib/dep/cryptocore/include/drm_macro.h index 8cdf1a5..b3332b6 100644 --- a/ssflib/dep/cryptocore/include/drm_macro.h +++ b/ssflib/dep/cryptocore/include/drm_macro.h @@ -91,11 +91,11 @@ /*! @brief xor 16 byte block */ #undef BlockXor -#define BlockXor(pbDst, phSrc1, phSrc2) { \ +#define BlockXor(pbDst, phSrc1, phSrc2) do { \ int idx; \ for(idx = 0; idx < 16; idx++) \ (pbDst)[idx] = (phSrc1)[idx] ^ (phSrc2)[idx]; \ -} +} while (0) /*! @brief convert 32-bit unit to 4 byte */ #undef GET_UINT32 diff --git a/ssflib/dep/cryptocore/source/CC_API.c b/ssflib/dep/cryptocore/source/CC_API.c index 03dad71..bdee2b5 100644 --- a/ssflib/dep/cryptocore/source/CC_API.c +++ b/ssflib/dep/cryptocore/source/CC_API.c @@ -42,442 +42,494 @@ //////////////////////////////////////////////////////////////////////////// void *CCMalloc(int siz) { - cc_u8 *pbBuf = (cc_u8*)malloc(siz); + cc_u8 *pbBuf = (cc_u8 *)malloc(siz); if (pbBuf == NULL) - { return NULL; - } - else - { + + else { memset(pbBuf, 0, siz); - return (void*)pbBuf; + return (void *)pbBuf; } } void CCFree(void *ptr) { if (ptr != NULL) - { free(ptr); - } } /* - * @fn CryptoCoreContainer *create_CryptoCoreContainer(cc_u32 algorithm) - * @brief memory allocation and initialize the crypt sturcture + * @fn CryptoCoreContainer *create_CryptoCoreContainer(cc_u32 algorithm) + * @brief memory allocation and initialize the crypt sturcture * - * @param algorithm [in]algorithm want to use + * @param algorithm [in]algorithm want to use * - * @return address of created sturcture + * @return address of created sturcture */ CryptoCoreContainer *create_CryptoCoreContainer(cc_u32 algorithm) { CryptoCoreContainer *crt; static int add_value = 0; - if(++add_value == 10000) add_value = 0; - srand(time(NULL) + add_value ); + if (++add_value == 10000) add_value = 0; + + srand(time(NULL) + add_value); // allocate memory for crypt data structure (by using CCMalloc) crt = (CryptoCoreContainer *)CCMalloc(sizeof(CryptoCoreContainer)); + if (crt == NULL) - { return NULL; - } crt->ctx = (CryptoCoreCTX *)CCMalloc(sizeof(CryptoCoreCTX)); - if (crt->ctx == NULL) - { + + if (crt->ctx == NULL) { free(crt); return NULL; } - crt->PRNG_seed = NULL; - crt->PRNG_get = NULL; - crt->MD_init = NULL; - crt->MD_update = NULL; - crt->MD_final = NULL; - crt->MD_getHASH = NULL; - crt->MAC_init = NULL; - crt->MAC_update = NULL; - crt->MAC_final = NULL; - crt->MAC_getMAC = NULL; - crt->SE_init = NULL; - crt->SE_process = NULL; - crt->SE_final = NULL; - crt->AE_encrypt = NULL; - crt->AE_decrypt = NULL; - crt->DS_sign = NULL; - crt->DS_verify = NULL; - crt->DSA_genParam = NULL; - crt->DSA_setParam = NULL; - crt->DSA_genKeypair = NULL; - crt->DSA_setKeyPair = NULL; - crt->RSA_genKeypair = NULL; - crt->RSA_genKeypairWithE= NULL; - crt->RSA_genKeypairForCRT = NULL; + crt->PRNG_seed = NULL; + crt->PRNG_get = NULL; + crt->MD_init = NULL; + crt->MD_update = NULL; + crt->MD_final = NULL; + crt->MD_getHASH = NULL; + crt->MAC_init = NULL; + crt->MAC_update = NULL; + crt->MAC_final = NULL; + crt->MAC_getMAC = NULL; + crt->SE_init = NULL; + crt->SE_process = NULL; + crt->SE_final = NULL; + crt->AE_encrypt = NULL; + crt->AE_decrypt = NULL; + crt->DS_sign = NULL; + crt->DS_verify = NULL; + crt->DSA_genParam = NULL; + crt->DSA_setParam = NULL; + crt->DSA_genKeypair = NULL; + crt->DSA_setKeyPair = NULL; + crt->RSA_genKeypair = NULL; + crt->RSA_genKeypairWithE = NULL; + crt->RSA_genKeypairForCRT = NULL; crt->RSA_genKeyDWithPQE = NULL; - crt->RSA_genKeypairWithEforCRT= NULL; - crt->RSA_setKeypair = NULL; - crt->RSA_setKeypairForCRT = NULL; - crt->EC_setCurve = NULL; - crt->EC_genKeypair = NULL; - crt->EC_setKeypair = NULL; - crt->DH_GenerateParam = NULL; - crt->DH_SetParam = NULL; - crt->DH_Gen1stPhaseKey = NULL; - crt->DH_GenAuthKey = NULL; - crt->ECDH_Gen1stPhaseKey= NULL; - crt->ECDH_GenAuthKey = NULL; - - printf("TEST!!! step 1 in create_CryptoCoreContainer(%d)\n",algorithm); + crt->RSA_genKeypairWithEforCRT = NULL; + crt->RSA_setKeypair = NULL; + crt->RSA_setKeypairForCRT = NULL; + crt->EC_setCurve = NULL; + crt->EC_genKeypair = NULL; + crt->EC_setKeypair = NULL; + crt->DH_GenerateParam = NULL; + crt->DH_SetParam = NULL; + crt->DH_Gen1stPhaseKey = NULL; + crt->DH_GenAuthKey = NULL; + crt->ECDH_Gen1stPhaseKey = NULL; + crt->ECDH_GenAuthKey = NULL; + + printf("TEST!!! step 1 in create_CryptoCoreContainer(%d)\n", algorithm); // allocate memory for context data structure // and set up the member functions according to the algorithm crt->alg = algorithm; - switch(algorithm) - { - case ID_X931: - crt->ctx->x931ctx = (SDRM_X931Context*)CCMalloc(sizeof(SDRM_X931Context)); - crt->PRNG_seed = SDRM_X931_seed; - crt->PRNG_get = SDRM_X931_get; - break; - case ID_MD5: - crt->ctx->md5ctx = (SDRM_MD5Context*)CCMalloc(sizeof(SDRM_MD5Context)); - crt->MD_init = SDRM_MD5_init; - crt->MD_update = SDRM_MD5_update; - crt->MD_final = SDRM_MD5_final; - crt->MD_getHASH = SDRM_MD5_hash; - break; - case ID_SHA1: - crt->ctx->sha1ctx = (SDRM_SHA1Context*)CCMalloc(sizeof(SDRM_SHA1Context)); - crt->MD_init = SDRM_SHA1_init; - crt->MD_update = SDRM_SHA1_update; - crt->MD_final = SDRM_SHA1_final; - crt->MD_getHASH = SDRM_SHA1_hash; - break; - case ID_SHA224: - crt->ctx->sha224ctx = (SDRM_SHA224Context*)CCMalloc(sizeof(SDRM_SHA224Context)); - crt->MD_init = SDRM_SHA224_init; - crt->MD_update = SDRM_SHA224_update; - crt->MD_final = SDRM_SHA224_final; - crt->MD_getHASH = SDRM_SHA224_hash; - break; - case ID_SHA256: - crt->ctx->sha256ctx = (SDRM_SHA256Context*)CCMalloc(sizeof(SDRM_SHA256Context)); - crt->MD_init = SDRM_SHA256_init; - crt->MD_update = SDRM_SHA256_update; - crt->MD_final = SDRM_SHA256_final; - crt->MD_getHASH = SDRM_SHA256_hash; - break; + + switch (algorithm) { + case ID_X931: + crt->ctx->x931ctx = (SDRM_X931Context *)CCMalloc(sizeof( + SDRM_X931Context)); + crt->PRNG_seed = SDRM_X931_seed; + crt->PRNG_get = SDRM_X931_get; + break; + + case ID_MD5: + crt->ctx->md5ctx = (SDRM_MD5Context *)CCMalloc(sizeof( + SDRM_MD5Context)); + crt->MD_init = SDRM_MD5_init; + crt->MD_update = SDRM_MD5_update; + crt->MD_final = SDRM_MD5_final; + crt->MD_getHASH = SDRM_MD5_hash; + break; + + case ID_SHA1: + crt->ctx->sha1ctx = (SDRM_SHA1Context *)CCMalloc(sizeof( + SDRM_SHA1Context)); + crt->MD_init = SDRM_SHA1_init; + crt->MD_update = SDRM_SHA1_update; + crt->MD_final = SDRM_SHA1_final; + crt->MD_getHASH = SDRM_SHA1_hash; + break; + + case ID_SHA224: + crt->ctx->sha224ctx = (SDRM_SHA224Context *)CCMalloc(sizeof( + SDRM_SHA224Context)); + crt->MD_init = SDRM_SHA224_init; + crt->MD_update = SDRM_SHA224_update; + crt->MD_final = SDRM_SHA224_final; + crt->MD_getHASH = SDRM_SHA224_hash; + break; + + case ID_SHA256: + crt->ctx->sha256ctx = (SDRM_SHA256Context *)CCMalloc(sizeof( + SDRM_SHA256Context)); + crt->MD_init = SDRM_SHA256_init; + crt->MD_update = SDRM_SHA256_update; + crt->MD_final = SDRM_SHA256_final; + crt->MD_getHASH = SDRM_SHA256_hash; + break; #ifndef _OP64_NOTSUPPORTED - case ID_SHA384: - crt->ctx->sha384ctx = (SDRM_SHA384Context*)CCMalloc(sizeof(SDRM_SHA384Context)); - crt->MD_init = SDRM_SHA384_init; - crt->MD_update = SDRM_SHA384_update; - crt->MD_final = SDRM_SHA384_final; - crt->MD_getHASH = SDRM_SHA384_hash; - break; - case ID_SHA512: - crt->ctx->sha512ctx = (SDRM_SHA512Context*)CCMalloc(sizeof(SDRM_SHA512Context)); - crt->MD_init = SDRM_SHA512_init; - crt->MD_update = SDRM_SHA512_update; - crt->MD_final = SDRM_SHA512_final; - crt->MD_getHASH = SDRM_SHA512_hash; - break; + + case ID_SHA384: + crt->ctx->sha384ctx = (SDRM_SHA384Context *)CCMalloc(sizeof( + SDRM_SHA384Context)); + crt->MD_init = SDRM_SHA384_init; + crt->MD_update = SDRM_SHA384_update; + crt->MD_final = SDRM_SHA384_final; + crt->MD_getHASH = SDRM_SHA384_hash; + break; + + case ID_SHA512: + crt->ctx->sha512ctx = (SDRM_SHA512Context *)CCMalloc(sizeof( + SDRM_SHA512Context)); + crt->MD_init = SDRM_SHA512_init; + crt->MD_update = SDRM_SHA512_update; + crt->MD_final = SDRM_SHA512_final; + crt->MD_getHASH = SDRM_SHA512_hash; + break; #endif - case ID_CMAC: - crt->ctx->cmacctx = (SDRM_CMACContext*)CCMalloc(sizeof(SDRM_CMACContext)); - crt->MAC_init = SDRM_CMAC_init; - crt->MAC_update = SDRM_CMAC_update; - crt->MAC_final = SDRM_CMAC_final; - crt->MAC_getMAC = SDRM_CMAC_getMAC; - break; - case ID_HMD5: - case ID_HSHA1: - case ID_HSHA224: - case ID_HSHA256: + + case ID_CMAC: + crt->ctx->cmacctx = (SDRM_CMACContext *)CCMalloc(sizeof( + SDRM_CMACContext)); + crt->MAC_init = SDRM_CMAC_init; + crt->MAC_update = SDRM_CMAC_update; + crt->MAC_final = SDRM_CMAC_final; + crt->MAC_getMAC = SDRM_CMAC_getMAC; + break; + + case ID_HMD5: + case ID_HSHA1: + case ID_HSHA224: + case ID_HSHA256: #ifndef _OP64_NOTSUPPORTED - case ID_HSHA384: - case ID_HSHA512: + case ID_HSHA384: + case ID_HSHA512: #endif //_OP64_NOTSUPPORTED - crt->ctx->hmacctx = (SDRM_HMACContext*)CCMalloc(sizeof(SDRM_HMACContext)); - crt->MAC_init = SDRM_HMAC_init; - crt->MAC_update = SDRM_HMAC_update; - crt->MAC_final = SDRM_HMAC_final; - crt->MAC_getMAC = SDRM_HMAC_getMAC; - break; - case ID_DH : - crt->ctx->dhctx = (SDRM_DHContext*)CCMalloc(sizeof(SDRM_DHContext)); - crt->DH_GenerateParam = SDRM_GenerateDHParam; - crt->DH_SetParam = SDRM_SetDHParam; - crt->DH_Gen1stPhaseKey = SDRM_GenerateDHPrivate; - crt->DH_GenAuthKey = SDRM_GetDHSharedSecret; - break; - case ID_ECDH : - crt->ctx->ecdhctx = (SDRM_ECDHContext*)SDRM_CURVE_Init(); - crt->EC_setCurve = SDRM_ECC_Set_CTX; - crt->EC_genKeypair = SDRM_ECC_genKeypair; - crt->EC_setKeypair = SDRM_ECC_setKeypair; - crt->ECDH_Gen1stPhaseKey = SDRM_generateDH1stPhaseKey; - crt->ECDH_GenAuthKey = SDRM_generateDHKey; - break; - case ID_AES128: - crt->ctx->aesctx = (SDRM_AESContext*)CCMalloc(sizeof(SDRM_AESContext)); - crt->SE_init = SDRM_AES_init; - crt->SE_process = SDRM_AES_process; - crt->SE_final = SDRM_AES_final; - crt->SE_EncryptOneBlock = SDRM_AES128_Encryption; - crt->SE_DecryptOneBlock = SDRM_AES128_Decryption; - break; - case ID_AES192: - crt->ctx->aesctx = (SDRM_AESContext*)CCMalloc(sizeof(SDRM_AESContext)); - crt->SE_init = SDRM_AES_init; - crt->SE_process = SDRM_AES_process; - crt->SE_final = SDRM_AES_final; - crt->SE_EncryptOneBlock = SDRM_AES192_Encryption; - crt->SE_DecryptOneBlock = SDRM_AES192_Decryption; - break; - case ID_AES256: - crt->ctx->aesctx = (SDRM_AESContext*)CCMalloc(sizeof(SDRM_AESContext)); - crt->SE_init = SDRM_AES_init; - crt->SE_process = SDRM_AES_process; - crt->SE_final = SDRM_AES_final; - crt->SE_EncryptOneBlock = SDRM_AES256_Encryption; - crt->SE_DecryptOneBlock = SDRM_AES256_Decryption; - break; - case ID_DES: - crt->ctx->desctx = (SDRM_DESContext*)CCMalloc(sizeof(SDRM_DESContext)); - crt->SE_init = SDRM_DES_init; - crt->SE_process = SDRM_DES_process; - crt->SE_final = SDRM_DES_final; - crt->SE_EncryptOneBlock = SDRM_DES64_Encryption; - crt->SE_DecryptOneBlock = SDRM_DES64_Decryption; - break; - case ID_TDES: - crt->ctx->tdesctx = (SDRM_TDESContext*)CCMalloc(sizeof(SDRM_TDESContext)); - crt->SE_init = SDRM_TDES_init; - crt->SE_process = SDRM_TDES_process; - crt->SE_final = SDRM_TDES_final; - crt->SE_EncryptOneBlock = SDRM_TDES64_Encryption; - crt->SE_DecryptOneBlock = SDRM_TDES64_Decryption; - break; - case ID_RC4: - crt->ctx->rc4ctx = (SDRM_RC4Context*)CCMalloc(sizeof(SDRM_RC4Context)); - crt->SE_init = SDRM_RC4_init; - crt->SE_process = SDRM_RC4_process; - break; - case ID_SNOW2: - crt->ctx->snow2ctx = (SDRM_SNOW2Context*)CCMalloc(sizeof(SDRM_SNOW2Context)); - crt->SE_init = SDRM_SNOW2_init; - crt->SE_process = SDRM_SNOW2_process; - break; - case ID_RSA512: - crt->ctx->rsactx = SDRM_RSA_InitCrt(64); - crt->RSA_genKeypair = SDRM_RSA_GenerateKey; - crt->RSA_genKeypairWithE = SDRM_RSA_GenerateND; - crt->RSA_genKeyDWithPQE = SDRM_RSA_GenerateDwithPQE; - crt->RSA_genKeypairWithEforCRT = SDRM_RSA_GenerateKeyforCRT; - crt->RSA_setKeypair = SDRM_RSA_setNED; - crt->RSA_setKeypairForCRT = SDRM_RSA_setNEDPQ; - crt->AE_encrypt = SDRM_RSA_encrypt; - crt->AE_decrypt = SDRM_RSA_decrypt; - crt->AE_decryptByCRT = SDRM_RSA_decryptByCRT; - crt->DS_sign = SDRM_RSA_sign; - crt->DS_verify = SDRM_RSA_verify; - break; - case ID_RSA: - case ID_RSA1024: - crt->ctx->rsactx = SDRM_RSA_InitCrt(128); - crt->RSA_genKeypair = SDRM_RSA_GenerateKey; - crt->RSA_genKeypairWithE = SDRM_RSA_GenerateND; - crt->RSA_genKeyDWithPQE = SDRM_RSA_GenerateDwithPQE; - crt->RSA_genKeypairWithEforCRT = SDRM_RSA_GenerateKeyforCRT; - crt->RSA_setKeypair = SDRM_RSA_setNED; - crt->RSA_setKeypairForCRT = SDRM_RSA_setNEDPQ; - crt->AE_encrypt = SDRM_RSA_encrypt; - crt->AE_decrypt = SDRM_RSA_decrypt; - crt->AE_decryptByCRT = SDRM_RSA_decryptByCRT; - crt->DS_sign = SDRM_RSA_sign; - crt->DS_verify = SDRM_RSA_verify; - break; - case ID_RSA2048: - crt->ctx->rsactx = SDRM_RSA_InitCrt(256); - crt->RSA_genKeypair = SDRM_RSA_GenerateKey; - crt->RSA_genKeypairWithE = SDRM_RSA_GenerateND; - crt->RSA_genKeyDWithPQE = SDRM_RSA_GenerateDwithPQE; - crt->RSA_genKeypairWithEforCRT = SDRM_RSA_GenerateKeyforCRT; - crt->RSA_setKeypair = SDRM_RSA_setNED; - crt->RSA_setKeypairForCRT = SDRM_RSA_setNEDPQ; - crt->AE_encrypt = SDRM_RSA_encrypt; - crt->AE_decrypt = SDRM_RSA_decrypt; - crt->AE_decryptByCRT = SDRM_RSA_decryptByCRT; - crt->DS_sign = SDRM_RSA_sign; - crt->DS_verify = SDRM_RSA_verify; - break; - case ID_RSA3072: - crt->ctx->rsactx = SDRM_RSA_InitCrt(384); - crt->RSA_genKeypair = SDRM_RSA_GenerateKey; - crt->RSA_genKeypairWithE = SDRM_RSA_GenerateND; - crt->RSA_genKeyDWithPQE = SDRM_RSA_GenerateDwithPQE; - crt->RSA_genKeypairWithEforCRT = SDRM_RSA_GenerateKeyforCRT; - crt->RSA_setKeypair = SDRM_RSA_setNED; - crt->RSA_setKeypairForCRT = SDRM_RSA_setNEDPQ; - crt->AE_encrypt = SDRM_RSA_encrypt; - crt->AE_decrypt = SDRM_RSA_decrypt; - crt->AE_decryptByCRT = SDRM_RSA_decryptByCRT; - crt->DS_sign = SDRM_RSA_sign; - crt->DS_verify = SDRM_RSA_verify; - break; - case ID_RSA4096: - crt->ctx->rsactx = SDRM_RSA_InitCrt(512); - crt->RSA_genKeypair = SDRM_RSA_GenerateKey; - crt->RSA_genKeypairWithE = SDRM_RSA_GenerateND; - crt->RSA_genKeyDWithPQE = SDRM_RSA_GenerateDwithPQE; - crt->RSA_genKeypairWithEforCRT = SDRM_RSA_GenerateKeyforCRT; - crt->RSA_setKeypair = SDRM_RSA_setNED; - crt->RSA_setKeypairForCRT = SDRM_RSA_setNEDPQ; - crt->AE_encrypt = SDRM_RSA_encrypt; - crt->AE_decrypt = SDRM_RSA_decrypt; - crt->AE_decryptByCRT = SDRM_RSA_decryptByCRT; - crt->DS_sign = SDRM_RSA_sign; - crt->DS_verify = SDRM_RSA_verify; - break; - case ID_DSA: - crt->ctx->dsactx = (SDRM_DSAContext*)SDRM_DSA_InitCrt(); - crt->DSA_genParam = SDRM_DSA_GenParam; - crt->DSA_setParam = SDRM_DSA_SetParam; - crt->DSA_genKeypair = SDRM_DSA_GenKeypair; - crt->DSA_setKeyPair = SDRM_DSA_SetKeyPair; - crt->DS_sign = SDRM_DSA_sign; - crt->DS_verify = SDRM_DSA_verify; - break; - case ID_ECDSA: - crt->ctx->ecdsactx = (SDRM_ECDSAContext*)SDRM_CURVE_Init(); - crt->EC_setCurve = SDRM_ECC_Set_CTX; - crt->EC_genKeypair = SDRM_ECC_genKeypair; - crt->EC_setKeypair = SDRM_ECC_setKeypair; - crt->DS_sign = SDRM_ECDSA_sign; - crt->DS_verify = SDRM_ECDSA_verify; - break; - default: - // free CryptoCoreContainer data structure - free(crt->ctx); - free(crt); - return NULL; + crt->ctx->hmacctx = (SDRM_HMACContext *)CCMalloc(sizeof( + SDRM_HMACContext)); + crt->MAC_init = SDRM_HMAC_init; + crt->MAC_update = SDRM_HMAC_update; + crt->MAC_final = SDRM_HMAC_final; + crt->MAC_getMAC = SDRM_HMAC_getMAC; + break; + + case ID_DH: + crt->ctx->dhctx = (SDRM_DHContext *)CCMalloc(sizeof( + SDRM_DHContext)); + crt->DH_GenerateParam = SDRM_GenerateDHParam; + crt->DH_SetParam = SDRM_SetDHParam; + crt->DH_Gen1stPhaseKey = SDRM_GenerateDHPrivate; + crt->DH_GenAuthKey = SDRM_GetDHSharedSecret; + break; + + case ID_ECDH: + crt->ctx->ecdhctx = (SDRM_ECDHContext *)SDRM_CURVE_Init(); + crt->EC_setCurve = SDRM_ECC_Set_CTX; + crt->EC_genKeypair = SDRM_ECC_genKeypair; + crt->EC_setKeypair = SDRM_ECC_setKeypair; + crt->ECDH_Gen1stPhaseKey = SDRM_generateDH1stPhaseKey; + crt->ECDH_GenAuthKey = SDRM_generateDHKey; + break; + + case ID_AES128: + crt->ctx->aesctx = (SDRM_AESContext *)CCMalloc(sizeof( + SDRM_AESContext)); + crt->SE_init = SDRM_AES_init; + crt->SE_process = SDRM_AES_process; + crt->SE_final = SDRM_AES_final; + crt->SE_EncryptOneBlock = SDRM_AES128_Encryption; + crt->SE_DecryptOneBlock = SDRM_AES128_Decryption; + break; + + case ID_AES192: + crt->ctx->aesctx = (SDRM_AESContext *)CCMalloc(sizeof( + SDRM_AESContext)); + crt->SE_init = SDRM_AES_init; + crt->SE_process = SDRM_AES_process; + crt->SE_final = SDRM_AES_final; + crt->SE_EncryptOneBlock = SDRM_AES192_Encryption; + crt->SE_DecryptOneBlock = SDRM_AES192_Decryption; + break; + + case ID_AES256: + crt->ctx->aesctx = (SDRM_AESContext *)CCMalloc(sizeof( + SDRM_AESContext)); + crt->SE_init = SDRM_AES_init; + crt->SE_process = SDRM_AES_process; + crt->SE_final = SDRM_AES_final; + crt->SE_EncryptOneBlock = SDRM_AES256_Encryption; + crt->SE_DecryptOneBlock = SDRM_AES256_Decryption; + break; + + case ID_DES: + crt->ctx->desctx = (SDRM_DESContext *)CCMalloc(sizeof( + SDRM_DESContext)); + crt->SE_init = SDRM_DES_init; + crt->SE_process = SDRM_DES_process; + crt->SE_final = SDRM_DES_final; + crt->SE_EncryptOneBlock = SDRM_DES64_Encryption; + crt->SE_DecryptOneBlock = SDRM_DES64_Decryption; + break; + + case ID_TDES: + crt->ctx->tdesctx = (SDRM_TDESContext *)CCMalloc(sizeof( + SDRM_TDESContext)); + crt->SE_init = SDRM_TDES_init; + crt->SE_process = SDRM_TDES_process; + crt->SE_final = SDRM_TDES_final; + crt->SE_EncryptOneBlock = SDRM_TDES64_Encryption; + crt->SE_DecryptOneBlock = SDRM_TDES64_Decryption; + break; + + case ID_RC4: + crt->ctx->rc4ctx = (SDRM_RC4Context *)CCMalloc(sizeof( + SDRM_RC4Context)); + crt->SE_init = SDRM_RC4_init; + crt->SE_process = SDRM_RC4_process; + break; + + case ID_SNOW2: + crt->ctx->snow2ctx = (SDRM_SNOW2Context *)CCMalloc(sizeof( + SDRM_SNOW2Context)); + crt->SE_init = SDRM_SNOW2_init; + crt->SE_process = SDRM_SNOW2_process; + break; + + case ID_RSA512: + crt->ctx->rsactx = SDRM_RSA_InitCrt(64); + crt->RSA_genKeypair = SDRM_RSA_GenerateKey; + crt->RSA_genKeypairWithE = SDRM_RSA_GenerateND; + crt->RSA_genKeyDWithPQE = SDRM_RSA_GenerateDwithPQE; + crt->RSA_genKeypairWithEforCRT = SDRM_RSA_GenerateKeyforCRT; + crt->RSA_setKeypair = SDRM_RSA_setNED; + crt->RSA_setKeypairForCRT = SDRM_RSA_setNEDPQ; + crt->AE_encrypt = SDRM_RSA_encrypt; + crt->AE_decrypt = SDRM_RSA_decrypt; + crt->AE_decryptByCRT = SDRM_RSA_decryptByCRT; + crt->DS_sign = SDRM_RSA_sign; + crt->DS_verify = SDRM_RSA_verify; + break; + + case ID_RSA: + case ID_RSA1024: + crt->ctx->rsactx = SDRM_RSA_InitCrt(128); + crt->RSA_genKeypair = SDRM_RSA_GenerateKey; + crt->RSA_genKeypairWithE = SDRM_RSA_GenerateND; + crt->RSA_genKeyDWithPQE = SDRM_RSA_GenerateDwithPQE; + crt->RSA_genKeypairWithEforCRT = SDRM_RSA_GenerateKeyforCRT; + crt->RSA_setKeypair = SDRM_RSA_setNED; + crt->RSA_setKeypairForCRT = SDRM_RSA_setNEDPQ; + crt->AE_encrypt = SDRM_RSA_encrypt; + crt->AE_decrypt = SDRM_RSA_decrypt; + crt->AE_decryptByCRT = SDRM_RSA_decryptByCRT; + crt->DS_sign = SDRM_RSA_sign; + crt->DS_verify = SDRM_RSA_verify; + break; + + case ID_RSA2048: + crt->ctx->rsactx = SDRM_RSA_InitCrt(256); + crt->RSA_genKeypair = SDRM_RSA_GenerateKey; + crt->RSA_genKeypairWithE = SDRM_RSA_GenerateND; + crt->RSA_genKeyDWithPQE = SDRM_RSA_GenerateDwithPQE; + crt->RSA_genKeypairWithEforCRT = SDRM_RSA_GenerateKeyforCRT; + crt->RSA_setKeypair = SDRM_RSA_setNED; + crt->RSA_setKeypairForCRT = SDRM_RSA_setNEDPQ; + crt->AE_encrypt = SDRM_RSA_encrypt; + crt->AE_decrypt = SDRM_RSA_decrypt; + crt->AE_decryptByCRT = SDRM_RSA_decryptByCRT; + crt->DS_sign = SDRM_RSA_sign; + crt->DS_verify = SDRM_RSA_verify; + break; + + case ID_RSA3072: + crt->ctx->rsactx = SDRM_RSA_InitCrt(384); + crt->RSA_genKeypair = SDRM_RSA_GenerateKey; + crt->RSA_genKeypairWithE = SDRM_RSA_GenerateND; + crt->RSA_genKeyDWithPQE = SDRM_RSA_GenerateDwithPQE; + crt->RSA_genKeypairWithEforCRT = SDRM_RSA_GenerateKeyforCRT; + crt->RSA_setKeypair = SDRM_RSA_setNED; + crt->RSA_setKeypairForCRT = SDRM_RSA_setNEDPQ; + crt->AE_encrypt = SDRM_RSA_encrypt; + crt->AE_decrypt = SDRM_RSA_decrypt; + crt->AE_decryptByCRT = SDRM_RSA_decryptByCRT; + crt->DS_sign = SDRM_RSA_sign; + crt->DS_verify = SDRM_RSA_verify; + break; + + case ID_RSA4096: + crt->ctx->rsactx = SDRM_RSA_InitCrt(512); + crt->RSA_genKeypair = SDRM_RSA_GenerateKey; + crt->RSA_genKeypairWithE = SDRM_RSA_GenerateND; + crt->RSA_genKeyDWithPQE = SDRM_RSA_GenerateDwithPQE; + crt->RSA_genKeypairWithEforCRT = SDRM_RSA_GenerateKeyforCRT; + crt->RSA_setKeypair = SDRM_RSA_setNED; + crt->RSA_setKeypairForCRT = SDRM_RSA_setNEDPQ; + crt->AE_encrypt = SDRM_RSA_encrypt; + crt->AE_decrypt = SDRM_RSA_decrypt; + crt->AE_decryptByCRT = SDRM_RSA_decryptByCRT; + crt->DS_sign = SDRM_RSA_sign; + crt->DS_verify = SDRM_RSA_verify; + break; + + case ID_DSA: + crt->ctx->dsactx = (SDRM_DSAContext *)SDRM_DSA_InitCrt(); + crt->DSA_genParam = SDRM_DSA_GenParam; + crt->DSA_setParam = SDRM_DSA_SetParam; + crt->DSA_genKeypair = SDRM_DSA_GenKeypair; + crt->DSA_setKeyPair = SDRM_DSA_SetKeyPair; + crt->DS_sign = SDRM_DSA_sign; + crt->DS_verify = SDRM_DSA_verify; + break; + + case ID_ECDSA: + crt->ctx->ecdsactx = (SDRM_ECDSAContext *)SDRM_CURVE_Init(); + crt->EC_setCurve = SDRM_ECC_Set_CTX; + crt->EC_genKeypair = SDRM_ECC_genKeypair; + crt->EC_setKeypair = SDRM_ECC_setKeypair; + crt->DS_sign = SDRM_ECDSA_sign; + crt->DS_verify = SDRM_ECDSA_verify; + break; + + default: + // free CryptoCoreContainer data structure + free(crt->ctx); + free(crt); + return NULL; } - printf("TEST!!! after in create_CryptoCoreContainer(%p %d)\n",crt, ID_AES128); - printf("TEST!!! after in create_CryptoCoreContainer(%p)\n",crt->SE_init); -/* crt->SE_init = SDRM_AES_init; - crt->SE_process = SDRM_AES_process; - crt->SE_final = SDRM_AES_final; - crt->SE_EncryptOneBlock = SDRM_AES128_Encryption; - crt->SE_DecryptOneBlock = SDRM_AES128_Decryption;*/ + printf("TEST!!! after in create_CryptoCoreContainer(%p %d)\n", crt, ID_AES128); + printf("TEST!!! after in create_CryptoCoreContainer(%p)\n", crt->SE_init); + /* crt->SE_init = SDRM_AES_init; + crt->SE_process = SDRM_AES_process; + crt->SE_final = SDRM_AES_final; + crt->SE_EncryptOneBlock = SDRM_AES128_Encryption; + crt->SE_DecryptOneBlock = SDRM_AES128_Decryption;*/ return crt; } /* - * @fn void destroy_CryptoCoreContainer(CryptoCoreContainer* crt) + * @fn void destroy_CryptoCoreContainer(CryptoCoreContainer* crt) * - * @brief free allocated memory - * @param crt [in]crypt context + * @brief free allocated memory + * @param crt [in]crypt context * - * @return void + * @return void */ -void destroy_CryptoCoreContainer(CryptoCoreContainer* crt) +void destroy_CryptoCoreContainer(CryptoCoreContainer *crt) { if (crt == NULL) - { return; - } - if (crt->ctx == NULL) - { + if (crt->ctx == NULL) { free(crt); return; } // free context data structure - switch(crt->alg) - { - case ID_X931: - CCFree(crt->ctx->x931ctx); - break; - case ID_MD5: - CCFree(crt->ctx->md5ctx); - break; - case ID_SHA1: - CCFree(crt->ctx->sha1ctx); - break; - case ID_SHA224: - CCFree(crt->ctx->sha224ctx); - break; - case ID_SHA256: - CCFree(crt->ctx->sha256ctx); - break; + switch (crt->alg) { + case ID_X931: + CCFree(crt->ctx->x931ctx); + break; + + case ID_MD5: + CCFree(crt->ctx->md5ctx); + break; + + case ID_SHA1: + CCFree(crt->ctx->sha1ctx); + break; + + case ID_SHA224: + CCFree(crt->ctx->sha224ctx); + break; + + case ID_SHA256: + CCFree(crt->ctx->sha256ctx); + break; #ifndef _OP64_NOTSUPPORTED - case ID_SHA384: - CCFree(crt->ctx->sha384ctx); - break; - case ID_SHA512: - CCFree(crt->ctx->sha512ctx); - break; + + case ID_SHA384: + CCFree(crt->ctx->sha384ctx); + break; + + case ID_SHA512: + CCFree(crt->ctx->sha512ctx); + break; #endif - case ID_CMAC: - CCFree(crt->ctx->cmacctx); - break; - case ID_HMD5: - case ID_HSHA1: - case ID_HSHA224: - case ID_HSHA256: + + case ID_CMAC: + CCFree(crt->ctx->cmacctx); + break; + + case ID_HMD5: + case ID_HSHA1: + case ID_HSHA224: + case ID_HSHA256: #ifndef _OP64_NOTSUPPORTED - case ID_HSHA384: - case ID_HSHA512: + case ID_HSHA384: + case ID_HSHA512: #endif //_OP64_NOTSUPPORTED - CCFree(crt->ctx->hmacctx); - break; - case ID_AES128: - case ID_AES192: - case ID_AES256: - CCFree(crt->ctx->aesctx); - break; - case ID_DES: - CCFree(crt->ctx->desctx); - break; - case ID_TDES: - CCFree(crt->ctx->tdesctx); - break; - case ID_RC4: - CCFree(crt->ctx->rc4ctx); - break; - case ID_SNOW2: - CCFree(crt->ctx->snow2ctx); - break; - case ID_RSA512: - case ID_RSA: - case ID_RSA1024: - case ID_RSA2048: - case ID_RSA3072: - case ID_RSA4096: - CCFree(crt->ctx->rsactx); - break; - case ID_DSA: - CCFree(crt->ctx->dsactx); - break; - case ID_ECDSA: - CCFree(crt->ctx->ecdsactx); - break; - case ID_ECDH: - CCFree(crt->ctx->ecdhctx); - break; - case ID_DH : - SDRM_FreeDHContext(crt->ctx->dhctx); - CCFree(crt->ctx->dhctx); - break; + CCFree(crt->ctx->hmacctx); + break; + + case ID_AES128: + case ID_AES192: + case ID_AES256: + CCFree(crt->ctx->aesctx); + break; + + case ID_DES: + CCFree(crt->ctx->desctx); + break; + + case ID_TDES: + CCFree(crt->ctx->tdesctx); + break; + + case ID_RC4: + CCFree(crt->ctx->rc4ctx); + break; + + case ID_SNOW2: + CCFree(crt->ctx->snow2ctx); + break; + + case ID_RSA512: + case ID_RSA: + case ID_RSA1024: + case ID_RSA2048: + case ID_RSA3072: + case ID_RSA4096: + CCFree(crt->ctx->rsactx); + break; + + case ID_DSA: + CCFree(crt->ctx->dsactx); + break; + + case ID_ECDSA: + CCFree(crt->ctx->ecdsactx); + break; + + case ID_ECDH: + CCFree(crt->ctx->ecdhctx); + break; + + case ID_DH: + SDRM_FreeDHContext(crt->ctx->dhctx); + CCFree(crt->ctx->dhctx); + break; } // free CryptoCoreContainer data structure diff --git a/ssflib/dep/cryptocore/source/base/cc_ANSI_x931.c b/ssflib/dep/cryptocore/source/base/cc_ANSI_x931.c index 9a6a9aa..330dc53 100644 --- a/ssflib/dep/cryptocore/source/base/cc_ANSI_x931.c +++ b/ssflib/dep/cryptocore/source/base/cc_ANSI_x931.c @@ -36,27 +36,27 @@ // Functions //////////////////////////////////////////////////////////////////////////// /* - * @fn SDRM_RNG_X931 - * @brief generate random number with seed + * @fn SDRM_RNG_X931 + * @brief generate random number with seed * - * @param Seed [in]seed for RNG System - * @param bitLength [in]bit length of data to generate - * @param data [out]generated data + * @param Seed [in]seed for RNG System + * @param bitLength [in]bit length of data to generate + * @param data [out]generated data * - * @return CRYPTO_SUCCESS if success + * @return CRYPTO_SUCCESS if success */ -int SDRM_RNG_X931(cc_u8 *Si_ANSI_X9_31, cc_u32 bitLength, cc_u8 *data) +int SDRM_RNG_X931(cc_u8 *Si_ANSI_X9_31, cc_u32 bitLength, cc_u8 *data) { static cc_u8 K_ANSI_X9_31[SDRM_X931_SEED_SIZ] = {0xfd, 0x74, 0x3d, 0xe1, 0xdc, 0x08, 0xdc, 0x3d, 0x0f, 0xea, 0xf5, 0xa3, 0x6e, 0xb1, 0xc0, 0x7f}; - int res = CRYPTO_SUCCESS; - int i, offset; - int byteLength, residue; - int numBlock, residueBlock; - cc_u8 *DT; - cc_u8 I[SDRM_X931_SEED_SIZ] = {0}; - cc_u8 Ri_ANSI_X9_31[SDRM_X931_SEED_SIZ]; - cc_u32 Date[SDRM_X931_SEED_SIZ / 4]; - cc_u32 RoundKey[4*(10 + 1)]; //AES Round Key + int res = CRYPTO_SUCCESS; + int i, offset; + int byteLength, residue; + int numBlock, residueBlock; + cc_u8 *DT; + cc_u8 I[SDRM_X931_SEED_SIZ] = {0}; + cc_u8 Ri_ANSI_X9_31[SDRM_X931_SEED_SIZ]; + cc_u32 Date[SDRM_X931_SEED_SIZ / 4]; + cc_u32 RoundKey[4 * (10 + 1)]; //AES Round Key time_t nowTime; @@ -66,7 +66,7 @@ int SDRM_RNG_X931(cc_u8 *Si_ANSI_X9_31, cc_u32 bitLength, cc_u8 *data) Date[2] = rand(); Date[3] = rand(); - DT = (cc_u8*)Date; //DT : Time | Clock | RND | RND + DT = (cc_u8 *)Date; //DT : Time | Clock | RND | RND SDRM_rijndaelKeySetupDec(RoundKey, K_ANSI_X9_31, 128); @@ -74,11 +74,9 @@ int SDRM_RNG_X931(cc_u8 *Si_ANSI_X9_31, cc_u32 bitLength, cc_u8 *data) residue = bitLength - byteLength * 8; if (residue == 0) - { memset(data, 0x0, byteLength); - } - else - { + + else { byteLength += 1; memset(data, 0x0, byteLength); } @@ -87,8 +85,7 @@ int SDRM_RNG_X931(cc_u8 *Si_ANSI_X9_31, cc_u32 bitLength, cc_u8 *data) residueBlock = byteLength - numBlock * SDRM_X931_SEED_SIZ; offset = 0; - for(i = 0; i < numBlock; i++) - { + for (i = 0; i < numBlock; i++) { SDRM_rijndaelDecrypt(RoundKey, 10, DT, I); BlockXor(I, I, Si_ANSI_X9_31); @@ -100,8 +97,7 @@ int SDRM_RNG_X931(cc_u8 *Si_ANSI_X9_31, cc_u32 bitLength, cc_u8 *data) offset += SDRM_X931_SEED_SIZ; } - if (residueBlock != 0) - { + if (residueBlock != 0) { SDRM_rijndaelDecrypt(RoundKey, 10, DT, I); BlockXor(I, I, Si_ANSI_X9_31); diff --git a/ssflib/dep/cryptocore/source/base/cc_aes.c b/ssflib/dep/cryptocore/source/base/cc_aes.c index cbce0d7..f001314 100644 --- a/ssflib/dep/cryptocore/source/base/cc_aes.c +++ b/ssflib/dep/cryptocore/source/base/cc_aes.c @@ -35,669 +35,669 @@ // AES conversion matrix //////////////////////////////////////////////////////////////////////////// static const cc_u32 SDRM_Te0[256] = { - 0xc66363a5U, 0xf87c7c84U, 0xee777799U, 0xf67b7b8dU, - 0xfff2f20dU, 0xd66b6bbdU, 0xde6f6fb1U, 0x91c5c554U, - 0x60303050U, 0x02010103U, 0xce6767a9U, 0x562b2b7dU, - 0xe7fefe19U, 0xb5d7d762U, 0x4dababe6U, 0xec76769aU, - 0x8fcaca45U, 0x1f82829dU, 0x89c9c940U, 0xfa7d7d87U, - 0xeffafa15U, 0xb25959ebU, 0x8e4747c9U, 0xfbf0f00bU, - 0x41adadecU, 0xb3d4d467U, 0x5fa2a2fdU, 0x45afafeaU, - 0x239c9cbfU, 0x53a4a4f7U, 0xe4727296U, 0x9bc0c05bU, - 0x75b7b7c2U, 0xe1fdfd1cU, 0x3d9393aeU, 0x4c26266aU, - 0x6c36365aU, 0x7e3f3f41U, 0xf5f7f702U, 0x83cccc4fU, - 0x6834345cU, 0x51a5a5f4U, 0xd1e5e534U, 0xf9f1f108U, - 0xe2717193U, 0xabd8d873U, 0x62313153U, 0x2a15153fU, - 0x0804040cU, 0x95c7c752U, 0x46232365U, 0x9dc3c35eU, - 0x30181828U, 0x379696a1U, 0x0a05050fU, 0x2f9a9ab5U, - 0x0e070709U, 0x24121236U, 0x1b80809bU, 0xdfe2e23dU, - 0xcdebeb26U, 0x4e272769U, 0x7fb2b2cdU, 0xea75759fU, - 0x1209091bU, 0x1d83839eU, 0x582c2c74U, 0x341a1a2eU, - 0x361b1b2dU, 0xdc6e6eb2U, 0xb45a5aeeU, 0x5ba0a0fbU, - 0xa45252f6U, 0x763b3b4dU, 0xb7d6d661U, 0x7db3b3ceU, - 0x5229297bU, 0xdde3e33eU, 0x5e2f2f71U, 0x13848497U, - 0xa65353f5U, 0xb9d1d168U, 0x00000000U, 0xc1eded2cU, - 0x40202060U, 0xe3fcfc1fU, 0x79b1b1c8U, 0xb65b5bedU, - 0xd46a6abeU, 0x8dcbcb46U, 0x67bebed9U, 0x7239394bU, - 0x944a4adeU, 0x984c4cd4U, 0xb05858e8U, 0x85cfcf4aU, - 0xbbd0d06bU, 0xc5efef2aU, 0x4faaaae5U, 0xedfbfb16U, - 0x864343c5U, 0x9a4d4dd7U, 0x66333355U, 0x11858594U, - 0x8a4545cfU, 0xe9f9f910U, 0x04020206U, 0xfe7f7f81U, - 0xa05050f0U, 0x783c3c44U, 0x259f9fbaU, 0x4ba8a8e3U, - 0xa25151f3U, 0x5da3a3feU, 0x804040c0U, 0x058f8f8aU, - 0x3f9292adU, 0x219d9dbcU, 0x70383848U, 0xf1f5f504U, - 0x63bcbcdfU, 0x77b6b6c1U, 0xafdada75U, 0x42212163U, - 0x20101030U, 0xe5ffff1aU, 0xfdf3f30eU, 0xbfd2d26dU, - 0x81cdcd4cU, 0x180c0c14U, 0x26131335U, 0xc3ecec2fU, - 0xbe5f5fe1U, 0x359797a2U, 0x884444ccU, 0x2e171739U, - 0x93c4c457U, 0x55a7a7f2U, 0xfc7e7e82U, 0x7a3d3d47U, - 0xc86464acU, 0xba5d5de7U, 0x3219192bU, 0xe6737395U, - 0xc06060a0U, 0x19818198U, 0x9e4f4fd1U, 0xa3dcdc7fU, - 0x44222266U, 0x542a2a7eU, 0x3b9090abU, 0x0b888883U, - 0x8c4646caU, 0xc7eeee29U, 0x6bb8b8d3U, 0x2814143cU, - 0xa7dede79U, 0xbc5e5ee2U, 0x160b0b1dU, 0xaddbdb76U, - 0xdbe0e03bU, 0x64323256U, 0x743a3a4eU, 0x140a0a1eU, - 0x924949dbU, 0x0c06060aU, 0x4824246cU, 0xb85c5ce4U, - 0x9fc2c25dU, 0xbdd3d36eU, 0x43acacefU, 0xc46262a6U, - 0x399191a8U, 0x319595a4U, 0xd3e4e437U, 0xf279798bU, - 0xd5e7e732U, 0x8bc8c843U, 0x6e373759U, 0xda6d6db7U, - 0x018d8d8cU, 0xb1d5d564U, 0x9c4e4ed2U, 0x49a9a9e0U, - 0xd86c6cb4U, 0xac5656faU, 0xf3f4f407U, 0xcfeaea25U, - 0xca6565afU, 0xf47a7a8eU, 0x47aeaee9U, 0x10080818U, - 0x6fbabad5U, 0xf0787888U, 0x4a25256fU, 0x5c2e2e72U, - 0x381c1c24U, 0x57a6a6f1U, 0x73b4b4c7U, 0x97c6c651U, - 0xcbe8e823U, 0xa1dddd7cU, 0xe874749cU, 0x3e1f1f21U, - 0x964b4bddU, 0x61bdbddcU, 0x0d8b8b86U, 0x0f8a8a85U, - 0xe0707090U, 0x7c3e3e42U, 0x71b5b5c4U, 0xcc6666aaU, - 0x904848d8U, 0x06030305U, 0xf7f6f601U, 0x1c0e0e12U, - 0xc26161a3U, 0x6a35355fU, 0xae5757f9U, 0x69b9b9d0U, - 0x17868691U, 0x99c1c158U, 0x3a1d1d27U, 0x279e9eb9U, - 0xd9e1e138U, 0xebf8f813U, 0x2b9898b3U, 0x22111133U, - 0xd26969bbU, 0xa9d9d970U, 0x078e8e89U, 0x339494a7U, - 0x2d9b9bb6U, 0x3c1e1e22U, 0x15878792U, 0xc9e9e920U, - 0x87cece49U, 0xaa5555ffU, 0x50282878U, 0xa5dfdf7aU, - 0x038c8c8fU, 0x59a1a1f8U, 0x09898980U, 0x1a0d0d17U, - 0x65bfbfdaU, 0xd7e6e631U, 0x844242c6U, 0xd06868b8U, - 0x824141c3U, 0x299999b0U, 0x5a2d2d77U, 0x1e0f0f11U, - 0x7bb0b0cbU, 0xa85454fcU, 0x6dbbbbd6U, 0x2c16163aU, + 0xc66363a5U, 0xf87c7c84U, 0xee777799U, 0xf67b7b8dU, + 0xfff2f20dU, 0xd66b6bbdU, 0xde6f6fb1U, 0x91c5c554U, + 0x60303050U, 0x02010103U, 0xce6767a9U, 0x562b2b7dU, + 0xe7fefe19U, 0xb5d7d762U, 0x4dababe6U, 0xec76769aU, + 0x8fcaca45U, 0x1f82829dU, 0x89c9c940U, 0xfa7d7d87U, + 0xeffafa15U, 0xb25959ebU, 0x8e4747c9U, 0xfbf0f00bU, + 0x41adadecU, 0xb3d4d467U, 0x5fa2a2fdU, 0x45afafeaU, + 0x239c9cbfU, 0x53a4a4f7U, 0xe4727296U, 0x9bc0c05bU, + 0x75b7b7c2U, 0xe1fdfd1cU, 0x3d9393aeU, 0x4c26266aU, + 0x6c36365aU, 0x7e3f3f41U, 0xf5f7f702U, 0x83cccc4fU, + 0x6834345cU, 0x51a5a5f4U, 0xd1e5e534U, 0xf9f1f108U, + 0xe2717193U, 0xabd8d873U, 0x62313153U, 0x2a15153fU, + 0x0804040cU, 0x95c7c752U, 0x46232365U, 0x9dc3c35eU, + 0x30181828U, 0x379696a1U, 0x0a05050fU, 0x2f9a9ab5U, + 0x0e070709U, 0x24121236U, 0x1b80809bU, 0xdfe2e23dU, + 0xcdebeb26U, 0x4e272769U, 0x7fb2b2cdU, 0xea75759fU, + 0x1209091bU, 0x1d83839eU, 0x582c2c74U, 0x341a1a2eU, + 0x361b1b2dU, 0xdc6e6eb2U, 0xb45a5aeeU, 0x5ba0a0fbU, + 0xa45252f6U, 0x763b3b4dU, 0xb7d6d661U, 0x7db3b3ceU, + 0x5229297bU, 0xdde3e33eU, 0x5e2f2f71U, 0x13848497U, + 0xa65353f5U, 0xb9d1d168U, 0x00000000U, 0xc1eded2cU, + 0x40202060U, 0xe3fcfc1fU, 0x79b1b1c8U, 0xb65b5bedU, + 0xd46a6abeU, 0x8dcbcb46U, 0x67bebed9U, 0x7239394bU, + 0x944a4adeU, 0x984c4cd4U, 0xb05858e8U, 0x85cfcf4aU, + 0xbbd0d06bU, 0xc5efef2aU, 0x4faaaae5U, 0xedfbfb16U, + 0x864343c5U, 0x9a4d4dd7U, 0x66333355U, 0x11858594U, + 0x8a4545cfU, 0xe9f9f910U, 0x04020206U, 0xfe7f7f81U, + 0xa05050f0U, 0x783c3c44U, 0x259f9fbaU, 0x4ba8a8e3U, + 0xa25151f3U, 0x5da3a3feU, 0x804040c0U, 0x058f8f8aU, + 0x3f9292adU, 0x219d9dbcU, 0x70383848U, 0xf1f5f504U, + 0x63bcbcdfU, 0x77b6b6c1U, 0xafdada75U, 0x42212163U, + 0x20101030U, 0xe5ffff1aU, 0xfdf3f30eU, 0xbfd2d26dU, + 0x81cdcd4cU, 0x180c0c14U, 0x26131335U, 0xc3ecec2fU, + 0xbe5f5fe1U, 0x359797a2U, 0x884444ccU, 0x2e171739U, + 0x93c4c457U, 0x55a7a7f2U, 0xfc7e7e82U, 0x7a3d3d47U, + 0xc86464acU, 0xba5d5de7U, 0x3219192bU, 0xe6737395U, + 0xc06060a0U, 0x19818198U, 0x9e4f4fd1U, 0xa3dcdc7fU, + 0x44222266U, 0x542a2a7eU, 0x3b9090abU, 0x0b888883U, + 0x8c4646caU, 0xc7eeee29U, 0x6bb8b8d3U, 0x2814143cU, + 0xa7dede79U, 0xbc5e5ee2U, 0x160b0b1dU, 0xaddbdb76U, + 0xdbe0e03bU, 0x64323256U, 0x743a3a4eU, 0x140a0a1eU, + 0x924949dbU, 0x0c06060aU, 0x4824246cU, 0xb85c5ce4U, + 0x9fc2c25dU, 0xbdd3d36eU, 0x43acacefU, 0xc46262a6U, + 0x399191a8U, 0x319595a4U, 0xd3e4e437U, 0xf279798bU, + 0xd5e7e732U, 0x8bc8c843U, 0x6e373759U, 0xda6d6db7U, + 0x018d8d8cU, 0xb1d5d564U, 0x9c4e4ed2U, 0x49a9a9e0U, + 0xd86c6cb4U, 0xac5656faU, 0xf3f4f407U, 0xcfeaea25U, + 0xca6565afU, 0xf47a7a8eU, 0x47aeaee9U, 0x10080818U, + 0x6fbabad5U, 0xf0787888U, 0x4a25256fU, 0x5c2e2e72U, + 0x381c1c24U, 0x57a6a6f1U, 0x73b4b4c7U, 0x97c6c651U, + 0xcbe8e823U, 0xa1dddd7cU, 0xe874749cU, 0x3e1f1f21U, + 0x964b4bddU, 0x61bdbddcU, 0x0d8b8b86U, 0x0f8a8a85U, + 0xe0707090U, 0x7c3e3e42U, 0x71b5b5c4U, 0xcc6666aaU, + 0x904848d8U, 0x06030305U, 0xf7f6f601U, 0x1c0e0e12U, + 0xc26161a3U, 0x6a35355fU, 0xae5757f9U, 0x69b9b9d0U, + 0x17868691U, 0x99c1c158U, 0x3a1d1d27U, 0x279e9eb9U, + 0xd9e1e138U, 0xebf8f813U, 0x2b9898b3U, 0x22111133U, + 0xd26969bbU, 0xa9d9d970U, 0x078e8e89U, 0x339494a7U, + 0x2d9b9bb6U, 0x3c1e1e22U, 0x15878792U, 0xc9e9e920U, + 0x87cece49U, 0xaa5555ffU, 0x50282878U, 0xa5dfdf7aU, + 0x038c8c8fU, 0x59a1a1f8U, 0x09898980U, 0x1a0d0d17U, + 0x65bfbfdaU, 0xd7e6e631U, 0x844242c6U, 0xd06868b8U, + 0x824141c3U, 0x299999b0U, 0x5a2d2d77U, 0x1e0f0f11U, + 0x7bb0b0cbU, 0xa85454fcU, 0x6dbbbbd6U, 0x2c16163aU, }; static const cc_u32 SDRM_Te1[256] = { - 0xa5c66363U, 0x84f87c7cU, 0x99ee7777U, 0x8df67b7bU, - 0x0dfff2f2U, 0xbdd66b6bU, 0xb1de6f6fU, 0x5491c5c5U, - 0x50603030U, 0x03020101U, 0xa9ce6767U, 0x7d562b2bU, - 0x19e7fefeU, 0x62b5d7d7U, 0xe64dababU, 0x9aec7676U, - 0x458fcacaU, 0x9d1f8282U, 0x4089c9c9U, 0x87fa7d7dU, - 0x15effafaU, 0xebb25959U, 0xc98e4747U, 0x0bfbf0f0U, - 0xec41adadU, 0x67b3d4d4U, 0xfd5fa2a2U, 0xea45afafU, - 0xbf239c9cU, 0xf753a4a4U, 0x96e47272U, 0x5b9bc0c0U, - 0xc275b7b7U, 0x1ce1fdfdU, 0xae3d9393U, 0x6a4c2626U, - 0x5a6c3636U, 0x417e3f3fU, 0x02f5f7f7U, 0x4f83ccccU, - 0x5c683434U, 0xf451a5a5U, 0x34d1e5e5U, 0x08f9f1f1U, - 0x93e27171U, 0x73abd8d8U, 0x53623131U, 0x3f2a1515U, - 0x0c080404U, 0x5295c7c7U, 0x65462323U, 0x5e9dc3c3U, - 0x28301818U, 0xa1379696U, 0x0f0a0505U, 0xb52f9a9aU, - 0x090e0707U, 0x36241212U, 0x9b1b8080U, 0x3ddfe2e2U, - 0x26cdebebU, 0x694e2727U, 0xcd7fb2b2U, 0x9fea7575U, - 0x1b120909U, 0x9e1d8383U, 0x74582c2cU, 0x2e341a1aU, - 0x2d361b1bU, 0xb2dc6e6eU, 0xeeb45a5aU, 0xfb5ba0a0U, - 0xf6a45252U, 0x4d763b3bU, 0x61b7d6d6U, 0xce7db3b3U, - 0x7b522929U, 0x3edde3e3U, 0x715e2f2fU, 0x97138484U, - 0xf5a65353U, 0x68b9d1d1U, 0x00000000U, 0x2cc1ededU, - 0x60402020U, 0x1fe3fcfcU, 0xc879b1b1U, 0xedb65b5bU, - 0xbed46a6aU, 0x468dcbcbU, 0xd967bebeU, 0x4b723939U, - 0xde944a4aU, 0xd4984c4cU, 0xe8b05858U, 0x4a85cfcfU, - 0x6bbbd0d0U, 0x2ac5efefU, 0xe54faaaaU, 0x16edfbfbU, - 0xc5864343U, 0xd79a4d4dU, 0x55663333U, 0x94118585U, - 0xcf8a4545U, 0x10e9f9f9U, 0x06040202U, 0x81fe7f7fU, - 0xf0a05050U, 0x44783c3cU, 0xba259f9fU, 0xe34ba8a8U, - 0xf3a25151U, 0xfe5da3a3U, 0xc0804040U, 0x8a058f8fU, - 0xad3f9292U, 0xbc219d9dU, 0x48703838U, 0x04f1f5f5U, - 0xdf63bcbcU, 0xc177b6b6U, 0x75afdadaU, 0x63422121U, - 0x30201010U, 0x1ae5ffffU, 0x0efdf3f3U, 0x6dbfd2d2U, - 0x4c81cdcdU, 0x14180c0cU, 0x35261313U, 0x2fc3ececU, - 0xe1be5f5fU, 0xa2359797U, 0xcc884444U, 0x392e1717U, - 0x5793c4c4U, 0xf255a7a7U, 0x82fc7e7eU, 0x477a3d3dU, - 0xacc86464U, 0xe7ba5d5dU, 0x2b321919U, 0x95e67373U, - 0xa0c06060U, 0x98198181U, 0xd19e4f4fU, 0x7fa3dcdcU, - 0x66442222U, 0x7e542a2aU, 0xab3b9090U, 0x830b8888U, - 0xca8c4646U, 0x29c7eeeeU, 0xd36bb8b8U, 0x3c281414U, - 0x79a7dedeU, 0xe2bc5e5eU, 0x1d160b0bU, 0x76addbdbU, - 0x3bdbe0e0U, 0x56643232U, 0x4e743a3aU, 0x1e140a0aU, - 0xdb924949U, 0x0a0c0606U, 0x6c482424U, 0xe4b85c5cU, - 0x5d9fc2c2U, 0x6ebdd3d3U, 0xef43acacU, 0xa6c46262U, - 0xa8399191U, 0xa4319595U, 0x37d3e4e4U, 0x8bf27979U, - 0x32d5e7e7U, 0x438bc8c8U, 0x596e3737U, 0xb7da6d6dU, - 0x8c018d8dU, 0x64b1d5d5U, 0xd29c4e4eU, 0xe049a9a9U, - 0xb4d86c6cU, 0xfaac5656U, 0x07f3f4f4U, 0x25cfeaeaU, - 0xafca6565U, 0x8ef47a7aU, 0xe947aeaeU, 0x18100808U, - 0xd56fbabaU, 0x88f07878U, 0x6f4a2525U, 0x725c2e2eU, - 0x24381c1cU, 0xf157a6a6U, 0xc773b4b4U, 0x5197c6c6U, - 0x23cbe8e8U, 0x7ca1ddddU, 0x9ce87474U, 0x213e1f1fU, - 0xdd964b4bU, 0xdc61bdbdU, 0x860d8b8bU, 0x850f8a8aU, - 0x90e07070U, 0x427c3e3eU, 0xc471b5b5U, 0xaacc6666U, - 0xd8904848U, 0x05060303U, 0x01f7f6f6U, 0x121c0e0eU, - 0xa3c26161U, 0x5f6a3535U, 0xf9ae5757U, 0xd069b9b9U, - 0x91178686U, 0x5899c1c1U, 0x273a1d1dU, 0xb9279e9eU, - 0x38d9e1e1U, 0x13ebf8f8U, 0xb32b9898U, 0x33221111U, - 0xbbd26969U, 0x70a9d9d9U, 0x89078e8eU, 0xa7339494U, - 0xb62d9b9bU, 0x223c1e1eU, 0x92158787U, 0x20c9e9e9U, - 0x4987ceceU, 0xffaa5555U, 0x78502828U, 0x7aa5dfdfU, - 0x8f038c8cU, 0xf859a1a1U, 0x80098989U, 0x171a0d0dU, - 0xda65bfbfU, 0x31d7e6e6U, 0xc6844242U, 0xb8d06868U, - 0xc3824141U, 0xb0299999U, 0x775a2d2dU, 0x111e0f0fU, - 0xcb7bb0b0U, 0xfca85454U, 0xd66dbbbbU, 0x3a2c1616U, + 0xa5c66363U, 0x84f87c7cU, 0x99ee7777U, 0x8df67b7bU, + 0x0dfff2f2U, 0xbdd66b6bU, 0xb1de6f6fU, 0x5491c5c5U, + 0x50603030U, 0x03020101U, 0xa9ce6767U, 0x7d562b2bU, + 0x19e7fefeU, 0x62b5d7d7U, 0xe64dababU, 0x9aec7676U, + 0x458fcacaU, 0x9d1f8282U, 0x4089c9c9U, 0x87fa7d7dU, + 0x15effafaU, 0xebb25959U, 0xc98e4747U, 0x0bfbf0f0U, + 0xec41adadU, 0x67b3d4d4U, 0xfd5fa2a2U, 0xea45afafU, + 0xbf239c9cU, 0xf753a4a4U, 0x96e47272U, 0x5b9bc0c0U, + 0xc275b7b7U, 0x1ce1fdfdU, 0xae3d9393U, 0x6a4c2626U, + 0x5a6c3636U, 0x417e3f3fU, 0x02f5f7f7U, 0x4f83ccccU, + 0x5c683434U, 0xf451a5a5U, 0x34d1e5e5U, 0x08f9f1f1U, + 0x93e27171U, 0x73abd8d8U, 0x53623131U, 0x3f2a1515U, + 0x0c080404U, 0x5295c7c7U, 0x65462323U, 0x5e9dc3c3U, + 0x28301818U, 0xa1379696U, 0x0f0a0505U, 0xb52f9a9aU, + 0x090e0707U, 0x36241212U, 0x9b1b8080U, 0x3ddfe2e2U, + 0x26cdebebU, 0x694e2727U, 0xcd7fb2b2U, 0x9fea7575U, + 0x1b120909U, 0x9e1d8383U, 0x74582c2cU, 0x2e341a1aU, + 0x2d361b1bU, 0xb2dc6e6eU, 0xeeb45a5aU, 0xfb5ba0a0U, + 0xf6a45252U, 0x4d763b3bU, 0x61b7d6d6U, 0xce7db3b3U, + 0x7b522929U, 0x3edde3e3U, 0x715e2f2fU, 0x97138484U, + 0xf5a65353U, 0x68b9d1d1U, 0x00000000U, 0x2cc1ededU, + 0x60402020U, 0x1fe3fcfcU, 0xc879b1b1U, 0xedb65b5bU, + 0xbed46a6aU, 0x468dcbcbU, 0xd967bebeU, 0x4b723939U, + 0xde944a4aU, 0xd4984c4cU, 0xe8b05858U, 0x4a85cfcfU, + 0x6bbbd0d0U, 0x2ac5efefU, 0xe54faaaaU, 0x16edfbfbU, + 0xc5864343U, 0xd79a4d4dU, 0x55663333U, 0x94118585U, + 0xcf8a4545U, 0x10e9f9f9U, 0x06040202U, 0x81fe7f7fU, + 0xf0a05050U, 0x44783c3cU, 0xba259f9fU, 0xe34ba8a8U, + 0xf3a25151U, 0xfe5da3a3U, 0xc0804040U, 0x8a058f8fU, + 0xad3f9292U, 0xbc219d9dU, 0x48703838U, 0x04f1f5f5U, + 0xdf63bcbcU, 0xc177b6b6U, 0x75afdadaU, 0x63422121U, + 0x30201010U, 0x1ae5ffffU, 0x0efdf3f3U, 0x6dbfd2d2U, + 0x4c81cdcdU, 0x14180c0cU, 0x35261313U, 0x2fc3ececU, + 0xe1be5f5fU, 0xa2359797U, 0xcc884444U, 0x392e1717U, + 0x5793c4c4U, 0xf255a7a7U, 0x82fc7e7eU, 0x477a3d3dU, + 0xacc86464U, 0xe7ba5d5dU, 0x2b321919U, 0x95e67373U, + 0xa0c06060U, 0x98198181U, 0xd19e4f4fU, 0x7fa3dcdcU, + 0x66442222U, 0x7e542a2aU, 0xab3b9090U, 0x830b8888U, + 0xca8c4646U, 0x29c7eeeeU, 0xd36bb8b8U, 0x3c281414U, + 0x79a7dedeU, 0xe2bc5e5eU, 0x1d160b0bU, 0x76addbdbU, + 0x3bdbe0e0U, 0x56643232U, 0x4e743a3aU, 0x1e140a0aU, + 0xdb924949U, 0x0a0c0606U, 0x6c482424U, 0xe4b85c5cU, + 0x5d9fc2c2U, 0x6ebdd3d3U, 0xef43acacU, 0xa6c46262U, + 0xa8399191U, 0xa4319595U, 0x37d3e4e4U, 0x8bf27979U, + 0x32d5e7e7U, 0x438bc8c8U, 0x596e3737U, 0xb7da6d6dU, + 0x8c018d8dU, 0x64b1d5d5U, 0xd29c4e4eU, 0xe049a9a9U, + 0xb4d86c6cU, 0xfaac5656U, 0x07f3f4f4U, 0x25cfeaeaU, + 0xafca6565U, 0x8ef47a7aU, 0xe947aeaeU, 0x18100808U, + 0xd56fbabaU, 0x88f07878U, 0x6f4a2525U, 0x725c2e2eU, + 0x24381c1cU, 0xf157a6a6U, 0xc773b4b4U, 0x5197c6c6U, + 0x23cbe8e8U, 0x7ca1ddddU, 0x9ce87474U, 0x213e1f1fU, + 0xdd964b4bU, 0xdc61bdbdU, 0x860d8b8bU, 0x850f8a8aU, + 0x90e07070U, 0x427c3e3eU, 0xc471b5b5U, 0xaacc6666U, + 0xd8904848U, 0x05060303U, 0x01f7f6f6U, 0x121c0e0eU, + 0xa3c26161U, 0x5f6a3535U, 0xf9ae5757U, 0xd069b9b9U, + 0x91178686U, 0x5899c1c1U, 0x273a1d1dU, 0xb9279e9eU, + 0x38d9e1e1U, 0x13ebf8f8U, 0xb32b9898U, 0x33221111U, + 0xbbd26969U, 0x70a9d9d9U, 0x89078e8eU, 0xa7339494U, + 0xb62d9b9bU, 0x223c1e1eU, 0x92158787U, 0x20c9e9e9U, + 0x4987ceceU, 0xffaa5555U, 0x78502828U, 0x7aa5dfdfU, + 0x8f038c8cU, 0xf859a1a1U, 0x80098989U, 0x171a0d0dU, + 0xda65bfbfU, 0x31d7e6e6U, 0xc6844242U, 0xb8d06868U, + 0xc3824141U, 0xb0299999U, 0x775a2d2dU, 0x111e0f0fU, + 0xcb7bb0b0U, 0xfca85454U, 0xd66dbbbbU, 0x3a2c1616U, }; static const cc_u32 SDRM_Te2[256] = { - 0x63a5c663U, 0x7c84f87cU, 0x7799ee77U, 0x7b8df67bU, - 0xf20dfff2U, 0x6bbdd66bU, 0x6fb1de6fU, 0xc55491c5U, - 0x30506030U, 0x01030201U, 0x67a9ce67U, 0x2b7d562bU, - 0xfe19e7feU, 0xd762b5d7U, 0xabe64dabU, 0x769aec76U, - 0xca458fcaU, 0x829d1f82U, 0xc94089c9U, 0x7d87fa7dU, - 0xfa15effaU, 0x59ebb259U, 0x47c98e47U, 0xf00bfbf0U, - 0xadec41adU, 0xd467b3d4U, 0xa2fd5fa2U, 0xafea45afU, - 0x9cbf239cU, 0xa4f753a4U, 0x7296e472U, 0xc05b9bc0U, - 0xb7c275b7U, 0xfd1ce1fdU, 0x93ae3d93U, 0x266a4c26U, - 0x365a6c36U, 0x3f417e3fU, 0xf702f5f7U, 0xcc4f83ccU, - 0x345c6834U, 0xa5f451a5U, 0xe534d1e5U, 0xf108f9f1U, - 0x7193e271U, 0xd873abd8U, 0x31536231U, 0x153f2a15U, - 0x040c0804U, 0xc75295c7U, 0x23654623U, 0xc35e9dc3U, - 0x18283018U, 0x96a13796U, 0x050f0a05U, 0x9ab52f9aU, - 0x07090e07U, 0x12362412U, 0x809b1b80U, 0xe23ddfe2U, - 0xeb26cdebU, 0x27694e27U, 0xb2cd7fb2U, 0x759fea75U, - 0x091b1209U, 0x839e1d83U, 0x2c74582cU, 0x1a2e341aU, - 0x1b2d361bU, 0x6eb2dc6eU, 0x5aeeb45aU, 0xa0fb5ba0U, - 0x52f6a452U, 0x3b4d763bU, 0xd661b7d6U, 0xb3ce7db3U, - 0x297b5229U, 0xe33edde3U, 0x2f715e2fU, 0x84971384U, - 0x53f5a653U, 0xd168b9d1U, 0x00000000U, 0xed2cc1edU, - 0x20604020U, 0xfc1fe3fcU, 0xb1c879b1U, 0x5bedb65bU, - 0x6abed46aU, 0xcb468dcbU, 0xbed967beU, 0x394b7239U, - 0x4ade944aU, 0x4cd4984cU, 0x58e8b058U, 0xcf4a85cfU, - 0xd06bbbd0U, 0xef2ac5efU, 0xaae54faaU, 0xfb16edfbU, - 0x43c58643U, 0x4dd79a4dU, 0x33556633U, 0x85941185U, - 0x45cf8a45U, 0xf910e9f9U, 0x02060402U, 0x7f81fe7fU, - 0x50f0a050U, 0x3c44783cU, 0x9fba259fU, 0xa8e34ba8U, - 0x51f3a251U, 0xa3fe5da3U, 0x40c08040U, 0x8f8a058fU, - 0x92ad3f92U, 0x9dbc219dU, 0x38487038U, 0xf504f1f5U, - 0xbcdf63bcU, 0xb6c177b6U, 0xda75afdaU, 0x21634221U, - 0x10302010U, 0xff1ae5ffU, 0xf30efdf3U, 0xd26dbfd2U, - 0xcd4c81cdU, 0x0c14180cU, 0x13352613U, 0xec2fc3ecU, - 0x5fe1be5fU, 0x97a23597U, 0x44cc8844U, 0x17392e17U, - 0xc45793c4U, 0xa7f255a7U, 0x7e82fc7eU, 0x3d477a3dU, - 0x64acc864U, 0x5de7ba5dU, 0x192b3219U, 0x7395e673U, - 0x60a0c060U, 0x81981981U, 0x4fd19e4fU, 0xdc7fa3dcU, - 0x22664422U, 0x2a7e542aU, 0x90ab3b90U, 0x88830b88U, - 0x46ca8c46U, 0xee29c7eeU, 0xb8d36bb8U, 0x143c2814U, - 0xde79a7deU, 0x5ee2bc5eU, 0x0b1d160bU, 0xdb76addbU, - 0xe03bdbe0U, 0x32566432U, 0x3a4e743aU, 0x0a1e140aU, - 0x49db9249U, 0x060a0c06U, 0x246c4824U, 0x5ce4b85cU, - 0xc25d9fc2U, 0xd36ebdd3U, 0xacef43acU, 0x62a6c462U, - 0x91a83991U, 0x95a43195U, 0xe437d3e4U, 0x798bf279U, - 0xe732d5e7U, 0xc8438bc8U, 0x37596e37U, 0x6db7da6dU, - 0x8d8c018dU, 0xd564b1d5U, 0x4ed29c4eU, 0xa9e049a9U, - 0x6cb4d86cU, 0x56faac56U, 0xf407f3f4U, 0xea25cfeaU, - 0x65afca65U, 0x7a8ef47aU, 0xaee947aeU, 0x08181008U, - 0xbad56fbaU, 0x7888f078U, 0x256f4a25U, 0x2e725c2eU, - 0x1c24381cU, 0xa6f157a6U, 0xb4c773b4U, 0xc65197c6U, - 0xe823cbe8U, 0xdd7ca1ddU, 0x749ce874U, 0x1f213e1fU, - 0x4bdd964bU, 0xbddc61bdU, 0x8b860d8bU, 0x8a850f8aU, - 0x7090e070U, 0x3e427c3eU, 0xb5c471b5U, 0x66aacc66U, - 0x48d89048U, 0x03050603U, 0xf601f7f6U, 0x0e121c0eU, - 0x61a3c261U, 0x355f6a35U, 0x57f9ae57U, 0xb9d069b9U, - 0x86911786U, 0xc15899c1U, 0x1d273a1dU, 0x9eb9279eU, - 0xe138d9e1U, 0xf813ebf8U, 0x98b32b98U, 0x11332211U, - 0x69bbd269U, 0xd970a9d9U, 0x8e89078eU, 0x94a73394U, - 0x9bb62d9bU, 0x1e223c1eU, 0x87921587U, 0xe920c9e9U, - 0xce4987ceU, 0x55ffaa55U, 0x28785028U, 0xdf7aa5dfU, - 0x8c8f038cU, 0xa1f859a1U, 0x89800989U, 0x0d171a0dU, - 0xbfda65bfU, 0xe631d7e6U, 0x42c68442U, 0x68b8d068U, - 0x41c38241U, 0x99b02999U, 0x2d775a2dU, 0x0f111e0fU, - 0xb0cb7bb0U, 0x54fca854U, 0xbbd66dbbU, 0x163a2c16U, + 0x63a5c663U, 0x7c84f87cU, 0x7799ee77U, 0x7b8df67bU, + 0xf20dfff2U, 0x6bbdd66bU, 0x6fb1de6fU, 0xc55491c5U, + 0x30506030U, 0x01030201U, 0x67a9ce67U, 0x2b7d562bU, + 0xfe19e7feU, 0xd762b5d7U, 0xabe64dabU, 0x769aec76U, + 0xca458fcaU, 0x829d1f82U, 0xc94089c9U, 0x7d87fa7dU, + 0xfa15effaU, 0x59ebb259U, 0x47c98e47U, 0xf00bfbf0U, + 0xadec41adU, 0xd467b3d4U, 0xa2fd5fa2U, 0xafea45afU, + 0x9cbf239cU, 0xa4f753a4U, 0x7296e472U, 0xc05b9bc0U, + 0xb7c275b7U, 0xfd1ce1fdU, 0x93ae3d93U, 0x266a4c26U, + 0x365a6c36U, 0x3f417e3fU, 0xf702f5f7U, 0xcc4f83ccU, + 0x345c6834U, 0xa5f451a5U, 0xe534d1e5U, 0xf108f9f1U, + 0x7193e271U, 0xd873abd8U, 0x31536231U, 0x153f2a15U, + 0x040c0804U, 0xc75295c7U, 0x23654623U, 0xc35e9dc3U, + 0x18283018U, 0x96a13796U, 0x050f0a05U, 0x9ab52f9aU, + 0x07090e07U, 0x12362412U, 0x809b1b80U, 0xe23ddfe2U, + 0xeb26cdebU, 0x27694e27U, 0xb2cd7fb2U, 0x759fea75U, + 0x091b1209U, 0x839e1d83U, 0x2c74582cU, 0x1a2e341aU, + 0x1b2d361bU, 0x6eb2dc6eU, 0x5aeeb45aU, 0xa0fb5ba0U, + 0x52f6a452U, 0x3b4d763bU, 0xd661b7d6U, 0xb3ce7db3U, + 0x297b5229U, 0xe33edde3U, 0x2f715e2fU, 0x84971384U, + 0x53f5a653U, 0xd168b9d1U, 0x00000000U, 0xed2cc1edU, + 0x20604020U, 0xfc1fe3fcU, 0xb1c879b1U, 0x5bedb65bU, + 0x6abed46aU, 0xcb468dcbU, 0xbed967beU, 0x394b7239U, + 0x4ade944aU, 0x4cd4984cU, 0x58e8b058U, 0xcf4a85cfU, + 0xd06bbbd0U, 0xef2ac5efU, 0xaae54faaU, 0xfb16edfbU, + 0x43c58643U, 0x4dd79a4dU, 0x33556633U, 0x85941185U, + 0x45cf8a45U, 0xf910e9f9U, 0x02060402U, 0x7f81fe7fU, + 0x50f0a050U, 0x3c44783cU, 0x9fba259fU, 0xa8e34ba8U, + 0x51f3a251U, 0xa3fe5da3U, 0x40c08040U, 0x8f8a058fU, + 0x92ad3f92U, 0x9dbc219dU, 0x38487038U, 0xf504f1f5U, + 0xbcdf63bcU, 0xb6c177b6U, 0xda75afdaU, 0x21634221U, + 0x10302010U, 0xff1ae5ffU, 0xf30efdf3U, 0xd26dbfd2U, + 0xcd4c81cdU, 0x0c14180cU, 0x13352613U, 0xec2fc3ecU, + 0x5fe1be5fU, 0x97a23597U, 0x44cc8844U, 0x17392e17U, + 0xc45793c4U, 0xa7f255a7U, 0x7e82fc7eU, 0x3d477a3dU, + 0x64acc864U, 0x5de7ba5dU, 0x192b3219U, 0x7395e673U, + 0x60a0c060U, 0x81981981U, 0x4fd19e4fU, 0xdc7fa3dcU, + 0x22664422U, 0x2a7e542aU, 0x90ab3b90U, 0x88830b88U, + 0x46ca8c46U, 0xee29c7eeU, 0xb8d36bb8U, 0x143c2814U, + 0xde79a7deU, 0x5ee2bc5eU, 0x0b1d160bU, 0xdb76addbU, + 0xe03bdbe0U, 0x32566432U, 0x3a4e743aU, 0x0a1e140aU, + 0x49db9249U, 0x060a0c06U, 0x246c4824U, 0x5ce4b85cU, + 0xc25d9fc2U, 0xd36ebdd3U, 0xacef43acU, 0x62a6c462U, + 0x91a83991U, 0x95a43195U, 0xe437d3e4U, 0x798bf279U, + 0xe732d5e7U, 0xc8438bc8U, 0x37596e37U, 0x6db7da6dU, + 0x8d8c018dU, 0xd564b1d5U, 0x4ed29c4eU, 0xa9e049a9U, + 0x6cb4d86cU, 0x56faac56U, 0xf407f3f4U, 0xea25cfeaU, + 0x65afca65U, 0x7a8ef47aU, 0xaee947aeU, 0x08181008U, + 0xbad56fbaU, 0x7888f078U, 0x256f4a25U, 0x2e725c2eU, + 0x1c24381cU, 0xa6f157a6U, 0xb4c773b4U, 0xc65197c6U, + 0xe823cbe8U, 0xdd7ca1ddU, 0x749ce874U, 0x1f213e1fU, + 0x4bdd964bU, 0xbddc61bdU, 0x8b860d8bU, 0x8a850f8aU, + 0x7090e070U, 0x3e427c3eU, 0xb5c471b5U, 0x66aacc66U, + 0x48d89048U, 0x03050603U, 0xf601f7f6U, 0x0e121c0eU, + 0x61a3c261U, 0x355f6a35U, 0x57f9ae57U, 0xb9d069b9U, + 0x86911786U, 0xc15899c1U, 0x1d273a1dU, 0x9eb9279eU, + 0xe138d9e1U, 0xf813ebf8U, 0x98b32b98U, 0x11332211U, + 0x69bbd269U, 0xd970a9d9U, 0x8e89078eU, 0x94a73394U, + 0x9bb62d9bU, 0x1e223c1eU, 0x87921587U, 0xe920c9e9U, + 0xce4987ceU, 0x55ffaa55U, 0x28785028U, 0xdf7aa5dfU, + 0x8c8f038cU, 0xa1f859a1U, 0x89800989U, 0x0d171a0dU, + 0xbfda65bfU, 0xe631d7e6U, 0x42c68442U, 0x68b8d068U, + 0x41c38241U, 0x99b02999U, 0x2d775a2dU, 0x0f111e0fU, + 0xb0cb7bb0U, 0x54fca854U, 0xbbd66dbbU, 0x163a2c16U, }; static const cc_u32 SDRM_Te3[256] = { - 0x6363a5c6U, 0x7c7c84f8U, 0x777799eeU, 0x7b7b8df6U, - 0xf2f20dffU, 0x6b6bbdd6U, 0x6f6fb1deU, 0xc5c55491U, - 0x30305060U, 0x01010302U, 0x6767a9ceU, 0x2b2b7d56U, - 0xfefe19e7U, 0xd7d762b5U, 0xababe64dU, 0x76769aecU, - 0xcaca458fU, 0x82829d1fU, 0xc9c94089U, 0x7d7d87faU, - 0xfafa15efU, 0x5959ebb2U, 0x4747c98eU, 0xf0f00bfbU, - 0xadadec41U, 0xd4d467b3U, 0xa2a2fd5fU, 0xafafea45U, - 0x9c9cbf23U, 0xa4a4f753U, 0x727296e4U, 0xc0c05b9bU, - 0xb7b7c275U, 0xfdfd1ce1U, 0x9393ae3dU, 0x26266a4cU, - 0x36365a6cU, 0x3f3f417eU, 0xf7f702f5U, 0xcccc4f83U, - 0x34345c68U, 0xa5a5f451U, 0xe5e534d1U, 0xf1f108f9U, - 0x717193e2U, 0xd8d873abU, 0x31315362U, 0x15153f2aU, - 0x04040c08U, 0xc7c75295U, 0x23236546U, 0xc3c35e9dU, - 0x18182830U, 0x9696a137U, 0x05050f0aU, 0x9a9ab52fU, - 0x0707090eU, 0x12123624U, 0x80809b1bU, 0xe2e23ddfU, - 0xebeb26cdU, 0x2727694eU, 0xb2b2cd7fU, 0x75759feaU, - 0x09091b12U, 0x83839e1dU, 0x2c2c7458U, 0x1a1a2e34U, - 0x1b1b2d36U, 0x6e6eb2dcU, 0x5a5aeeb4U, 0xa0a0fb5bU, - 0x5252f6a4U, 0x3b3b4d76U, 0xd6d661b7U, 0xb3b3ce7dU, - 0x29297b52U, 0xe3e33eddU, 0x2f2f715eU, 0x84849713U, - 0x5353f5a6U, 0xd1d168b9U, 0x00000000U, 0xeded2cc1U, - 0x20206040U, 0xfcfc1fe3U, 0xb1b1c879U, 0x5b5bedb6U, - 0x6a6abed4U, 0xcbcb468dU, 0xbebed967U, 0x39394b72U, - 0x4a4ade94U, 0x4c4cd498U, 0x5858e8b0U, 0xcfcf4a85U, - 0xd0d06bbbU, 0xefef2ac5U, 0xaaaae54fU, 0xfbfb16edU, - 0x4343c586U, 0x4d4dd79aU, 0x33335566U, 0x85859411U, - 0x4545cf8aU, 0xf9f910e9U, 0x02020604U, 0x7f7f81feU, - 0x5050f0a0U, 0x3c3c4478U, 0x9f9fba25U, 0xa8a8e34bU, - 0x5151f3a2U, 0xa3a3fe5dU, 0x4040c080U, 0x8f8f8a05U, - 0x9292ad3fU, 0x9d9dbc21U, 0x38384870U, 0xf5f504f1U, - 0xbcbcdf63U, 0xb6b6c177U, 0xdada75afU, 0x21216342U, - 0x10103020U, 0xffff1ae5U, 0xf3f30efdU, 0xd2d26dbfU, - 0xcdcd4c81U, 0x0c0c1418U, 0x13133526U, 0xecec2fc3U, - 0x5f5fe1beU, 0x9797a235U, 0x4444cc88U, 0x1717392eU, - 0xc4c45793U, 0xa7a7f255U, 0x7e7e82fcU, 0x3d3d477aU, - 0x6464acc8U, 0x5d5de7baU, 0x19192b32U, 0x737395e6U, - 0x6060a0c0U, 0x81819819U, 0x4f4fd19eU, 0xdcdc7fa3U, - 0x22226644U, 0x2a2a7e54U, 0x9090ab3bU, 0x8888830bU, - 0x4646ca8cU, 0xeeee29c7U, 0xb8b8d36bU, 0x14143c28U, - 0xdede79a7U, 0x5e5ee2bcU, 0x0b0b1d16U, 0xdbdb76adU, - 0xe0e03bdbU, 0x32325664U, 0x3a3a4e74U, 0x0a0a1e14U, - 0x4949db92U, 0x06060a0cU, 0x24246c48U, 0x5c5ce4b8U, - 0xc2c25d9fU, 0xd3d36ebdU, 0xacacef43U, 0x6262a6c4U, - 0x9191a839U, 0x9595a431U, 0xe4e437d3U, 0x79798bf2U, - 0xe7e732d5U, 0xc8c8438bU, 0x3737596eU, 0x6d6db7daU, - 0x8d8d8c01U, 0xd5d564b1U, 0x4e4ed29cU, 0xa9a9e049U, - 0x6c6cb4d8U, 0x5656faacU, 0xf4f407f3U, 0xeaea25cfU, - 0x6565afcaU, 0x7a7a8ef4U, 0xaeaee947U, 0x08081810U, - 0xbabad56fU, 0x787888f0U, 0x25256f4aU, 0x2e2e725cU, - 0x1c1c2438U, 0xa6a6f157U, 0xb4b4c773U, 0xc6c65197U, - 0xe8e823cbU, 0xdddd7ca1U, 0x74749ce8U, 0x1f1f213eU, - 0x4b4bdd96U, 0xbdbddc61U, 0x8b8b860dU, 0x8a8a850fU, - 0x707090e0U, 0x3e3e427cU, 0xb5b5c471U, 0x6666aaccU, - 0x4848d890U, 0x03030506U, 0xf6f601f7U, 0x0e0e121cU, - 0x6161a3c2U, 0x35355f6aU, 0x5757f9aeU, 0xb9b9d069U, - 0x86869117U, 0xc1c15899U, 0x1d1d273aU, 0x9e9eb927U, - 0xe1e138d9U, 0xf8f813ebU, 0x9898b32bU, 0x11113322U, - 0x6969bbd2U, 0xd9d970a9U, 0x8e8e8907U, 0x9494a733U, - 0x9b9bb62dU, 0x1e1e223cU, 0x87879215U, 0xe9e920c9U, - 0xcece4987U, 0x5555ffaaU, 0x28287850U, 0xdfdf7aa5U, - 0x8c8c8f03U, 0xa1a1f859U, 0x89898009U, 0x0d0d171aU, - 0xbfbfda65U, 0xe6e631d7U, 0x4242c684U, 0x6868b8d0U, - 0x4141c382U, 0x9999b029U, 0x2d2d775aU, 0x0f0f111eU, - 0xb0b0cb7bU, 0x5454fca8U, 0xbbbbd66dU, 0x16163a2cU, + 0x6363a5c6U, 0x7c7c84f8U, 0x777799eeU, 0x7b7b8df6U, + 0xf2f20dffU, 0x6b6bbdd6U, 0x6f6fb1deU, 0xc5c55491U, + 0x30305060U, 0x01010302U, 0x6767a9ceU, 0x2b2b7d56U, + 0xfefe19e7U, 0xd7d762b5U, 0xababe64dU, 0x76769aecU, + 0xcaca458fU, 0x82829d1fU, 0xc9c94089U, 0x7d7d87faU, + 0xfafa15efU, 0x5959ebb2U, 0x4747c98eU, 0xf0f00bfbU, + 0xadadec41U, 0xd4d467b3U, 0xa2a2fd5fU, 0xafafea45U, + 0x9c9cbf23U, 0xa4a4f753U, 0x727296e4U, 0xc0c05b9bU, + 0xb7b7c275U, 0xfdfd1ce1U, 0x9393ae3dU, 0x26266a4cU, + 0x36365a6cU, 0x3f3f417eU, 0xf7f702f5U, 0xcccc4f83U, + 0x34345c68U, 0xa5a5f451U, 0xe5e534d1U, 0xf1f108f9U, + 0x717193e2U, 0xd8d873abU, 0x31315362U, 0x15153f2aU, + 0x04040c08U, 0xc7c75295U, 0x23236546U, 0xc3c35e9dU, + 0x18182830U, 0x9696a137U, 0x05050f0aU, 0x9a9ab52fU, + 0x0707090eU, 0x12123624U, 0x80809b1bU, 0xe2e23ddfU, + 0xebeb26cdU, 0x2727694eU, 0xb2b2cd7fU, 0x75759feaU, + 0x09091b12U, 0x83839e1dU, 0x2c2c7458U, 0x1a1a2e34U, + 0x1b1b2d36U, 0x6e6eb2dcU, 0x5a5aeeb4U, 0xa0a0fb5bU, + 0x5252f6a4U, 0x3b3b4d76U, 0xd6d661b7U, 0xb3b3ce7dU, + 0x29297b52U, 0xe3e33eddU, 0x2f2f715eU, 0x84849713U, + 0x5353f5a6U, 0xd1d168b9U, 0x00000000U, 0xeded2cc1U, + 0x20206040U, 0xfcfc1fe3U, 0xb1b1c879U, 0x5b5bedb6U, + 0x6a6abed4U, 0xcbcb468dU, 0xbebed967U, 0x39394b72U, + 0x4a4ade94U, 0x4c4cd498U, 0x5858e8b0U, 0xcfcf4a85U, + 0xd0d06bbbU, 0xefef2ac5U, 0xaaaae54fU, 0xfbfb16edU, + 0x4343c586U, 0x4d4dd79aU, 0x33335566U, 0x85859411U, + 0x4545cf8aU, 0xf9f910e9U, 0x02020604U, 0x7f7f81feU, + 0x5050f0a0U, 0x3c3c4478U, 0x9f9fba25U, 0xa8a8e34bU, + 0x5151f3a2U, 0xa3a3fe5dU, 0x4040c080U, 0x8f8f8a05U, + 0x9292ad3fU, 0x9d9dbc21U, 0x38384870U, 0xf5f504f1U, + 0xbcbcdf63U, 0xb6b6c177U, 0xdada75afU, 0x21216342U, + 0x10103020U, 0xffff1ae5U, 0xf3f30efdU, 0xd2d26dbfU, + 0xcdcd4c81U, 0x0c0c1418U, 0x13133526U, 0xecec2fc3U, + 0x5f5fe1beU, 0x9797a235U, 0x4444cc88U, 0x1717392eU, + 0xc4c45793U, 0xa7a7f255U, 0x7e7e82fcU, 0x3d3d477aU, + 0x6464acc8U, 0x5d5de7baU, 0x19192b32U, 0x737395e6U, + 0x6060a0c0U, 0x81819819U, 0x4f4fd19eU, 0xdcdc7fa3U, + 0x22226644U, 0x2a2a7e54U, 0x9090ab3bU, 0x8888830bU, + 0x4646ca8cU, 0xeeee29c7U, 0xb8b8d36bU, 0x14143c28U, + 0xdede79a7U, 0x5e5ee2bcU, 0x0b0b1d16U, 0xdbdb76adU, + 0xe0e03bdbU, 0x32325664U, 0x3a3a4e74U, 0x0a0a1e14U, + 0x4949db92U, 0x06060a0cU, 0x24246c48U, 0x5c5ce4b8U, + 0xc2c25d9fU, 0xd3d36ebdU, 0xacacef43U, 0x6262a6c4U, + 0x9191a839U, 0x9595a431U, 0xe4e437d3U, 0x79798bf2U, + 0xe7e732d5U, 0xc8c8438bU, 0x3737596eU, 0x6d6db7daU, + 0x8d8d8c01U, 0xd5d564b1U, 0x4e4ed29cU, 0xa9a9e049U, + 0x6c6cb4d8U, 0x5656faacU, 0xf4f407f3U, 0xeaea25cfU, + 0x6565afcaU, 0x7a7a8ef4U, 0xaeaee947U, 0x08081810U, + 0xbabad56fU, 0x787888f0U, 0x25256f4aU, 0x2e2e725cU, + 0x1c1c2438U, 0xa6a6f157U, 0xb4b4c773U, 0xc6c65197U, + 0xe8e823cbU, 0xdddd7ca1U, 0x74749ce8U, 0x1f1f213eU, + 0x4b4bdd96U, 0xbdbddc61U, 0x8b8b860dU, 0x8a8a850fU, + 0x707090e0U, 0x3e3e427cU, 0xb5b5c471U, 0x6666aaccU, + 0x4848d890U, 0x03030506U, 0xf6f601f7U, 0x0e0e121cU, + 0x6161a3c2U, 0x35355f6aU, 0x5757f9aeU, 0xb9b9d069U, + 0x86869117U, 0xc1c15899U, 0x1d1d273aU, 0x9e9eb927U, + 0xe1e138d9U, 0xf8f813ebU, 0x9898b32bU, 0x11113322U, + 0x6969bbd2U, 0xd9d970a9U, 0x8e8e8907U, 0x9494a733U, + 0x9b9bb62dU, 0x1e1e223cU, 0x87879215U, 0xe9e920c9U, + 0xcece4987U, 0x5555ffaaU, 0x28287850U, 0xdfdf7aa5U, + 0x8c8c8f03U, 0xa1a1f859U, 0x89898009U, 0x0d0d171aU, + 0xbfbfda65U, 0xe6e631d7U, 0x4242c684U, 0x6868b8d0U, + 0x4141c382U, 0x9999b029U, 0x2d2d775aU, 0x0f0f111eU, + 0xb0b0cb7bU, 0x5454fca8U, 0xbbbbd66dU, 0x16163a2cU, }; static const cc_u32 SDRM_Te4[256] = { - 0x63636363U, 0x7c7c7c7cU, 0x77777777U, 0x7b7b7b7bU, - 0xf2f2f2f2U, 0x6b6b6b6bU, 0x6f6f6f6fU, 0xc5c5c5c5U, - 0x30303030U, 0x01010101U, 0x67676767U, 0x2b2b2b2bU, - 0xfefefefeU, 0xd7d7d7d7U, 0xababababU, 0x76767676U, - 0xcacacacaU, 0x82828282U, 0xc9c9c9c9U, 0x7d7d7d7dU, - 0xfafafafaU, 0x59595959U, 0x47474747U, 0xf0f0f0f0U, - 0xadadadadU, 0xd4d4d4d4U, 0xa2a2a2a2U, 0xafafafafU, - 0x9c9c9c9cU, 0xa4a4a4a4U, 0x72727272U, 0xc0c0c0c0U, - 0xb7b7b7b7U, 0xfdfdfdfdU, 0x93939393U, 0x26262626U, - 0x36363636U, 0x3f3f3f3fU, 0xf7f7f7f7U, 0xccccccccU, - 0x34343434U, 0xa5a5a5a5U, 0xe5e5e5e5U, 0xf1f1f1f1U, - 0x71717171U, 0xd8d8d8d8U, 0x31313131U, 0x15151515U, - 0x04040404U, 0xc7c7c7c7U, 0x23232323U, 0xc3c3c3c3U, - 0x18181818U, 0x96969696U, 0x05050505U, 0x9a9a9a9aU, - 0x07070707U, 0x12121212U, 0x80808080U, 0xe2e2e2e2U, - 0xebebebebU, 0x27272727U, 0xb2b2b2b2U, 0x75757575U, - 0x09090909U, 0x83838383U, 0x2c2c2c2cU, 0x1a1a1a1aU, - 0x1b1b1b1bU, 0x6e6e6e6eU, 0x5a5a5a5aU, 0xa0a0a0a0U, - 0x52525252U, 0x3b3b3b3bU, 0xd6d6d6d6U, 0xb3b3b3b3U, - 0x29292929U, 0xe3e3e3e3U, 0x2f2f2f2fU, 0x84848484U, - 0x53535353U, 0xd1d1d1d1U, 0x00000000U, 0xededededU, - 0x20202020U, 0xfcfcfcfcU, 0xb1b1b1b1U, 0x5b5b5b5bU, - 0x6a6a6a6aU, 0xcbcbcbcbU, 0xbebebebeU, 0x39393939U, - 0x4a4a4a4aU, 0x4c4c4c4cU, 0x58585858U, 0xcfcfcfcfU, - 0xd0d0d0d0U, 0xefefefefU, 0xaaaaaaaaU, 0xfbfbfbfbU, - 0x43434343U, 0x4d4d4d4dU, 0x33333333U, 0x85858585U, - 0x45454545U, 0xf9f9f9f9U, 0x02020202U, 0x7f7f7f7fU, - 0x50505050U, 0x3c3c3c3cU, 0x9f9f9f9fU, 0xa8a8a8a8U, - 0x51515151U, 0xa3a3a3a3U, 0x40404040U, 0x8f8f8f8fU, - 0x92929292U, 0x9d9d9d9dU, 0x38383838U, 0xf5f5f5f5U, - 0xbcbcbcbcU, 0xb6b6b6b6U, 0xdadadadaU, 0x21212121U, - 0x10101010U, 0xffffffffU, 0xf3f3f3f3U, 0xd2d2d2d2U, - 0xcdcdcdcdU, 0x0c0c0c0cU, 0x13131313U, 0xececececU, - 0x5f5f5f5fU, 0x97979797U, 0x44444444U, 0x17171717U, - 0xc4c4c4c4U, 0xa7a7a7a7U, 0x7e7e7e7eU, 0x3d3d3d3dU, - 0x64646464U, 0x5d5d5d5dU, 0x19191919U, 0x73737373U, - 0x60606060U, 0x81818181U, 0x4f4f4f4fU, 0xdcdcdcdcU, - 0x22222222U, 0x2a2a2a2aU, 0x90909090U, 0x88888888U, - 0x46464646U, 0xeeeeeeeeU, 0xb8b8b8b8U, 0x14141414U, - 0xdedededeU, 0x5e5e5e5eU, 0x0b0b0b0bU, 0xdbdbdbdbU, - 0xe0e0e0e0U, 0x32323232U, 0x3a3a3a3aU, 0x0a0a0a0aU, - 0x49494949U, 0x06060606U, 0x24242424U, 0x5c5c5c5cU, - 0xc2c2c2c2U, 0xd3d3d3d3U, 0xacacacacU, 0x62626262U, - 0x91919191U, 0x95959595U, 0xe4e4e4e4U, 0x79797979U, - 0xe7e7e7e7U, 0xc8c8c8c8U, 0x37373737U, 0x6d6d6d6dU, - 0x8d8d8d8dU, 0xd5d5d5d5U, 0x4e4e4e4eU, 0xa9a9a9a9U, - 0x6c6c6c6cU, 0x56565656U, 0xf4f4f4f4U, 0xeaeaeaeaU, - 0x65656565U, 0x7a7a7a7aU, 0xaeaeaeaeU, 0x08080808U, - 0xbabababaU, 0x78787878U, 0x25252525U, 0x2e2e2e2eU, - 0x1c1c1c1cU, 0xa6a6a6a6U, 0xb4b4b4b4U, 0xc6c6c6c6U, - 0xe8e8e8e8U, 0xddddddddU, 0x74747474U, 0x1f1f1f1fU, - 0x4b4b4b4bU, 0xbdbdbdbdU, 0x8b8b8b8bU, 0x8a8a8a8aU, - 0x70707070U, 0x3e3e3e3eU, 0xb5b5b5b5U, 0x66666666U, - 0x48484848U, 0x03030303U, 0xf6f6f6f6U, 0x0e0e0e0eU, - 0x61616161U, 0x35353535U, 0x57575757U, 0xb9b9b9b9U, - 0x86868686U, 0xc1c1c1c1U, 0x1d1d1d1dU, 0x9e9e9e9eU, - 0xe1e1e1e1U, 0xf8f8f8f8U, 0x98989898U, 0x11111111U, - 0x69696969U, 0xd9d9d9d9U, 0x8e8e8e8eU, 0x94949494U, - 0x9b9b9b9bU, 0x1e1e1e1eU, 0x87878787U, 0xe9e9e9e9U, - 0xcecececeU, 0x55555555U, 0x28282828U, 0xdfdfdfdfU, - 0x8c8c8c8cU, 0xa1a1a1a1U, 0x89898989U, 0x0d0d0d0dU, - 0xbfbfbfbfU, 0xe6e6e6e6U, 0x42424242U, 0x68686868U, - 0x41414141U, 0x99999999U, 0x2d2d2d2dU, 0x0f0f0f0fU, - 0xb0b0b0b0U, 0x54545454U, 0xbbbbbbbbU, 0x16161616U, + 0x63636363U, 0x7c7c7c7cU, 0x77777777U, 0x7b7b7b7bU, + 0xf2f2f2f2U, 0x6b6b6b6bU, 0x6f6f6f6fU, 0xc5c5c5c5U, + 0x30303030U, 0x01010101U, 0x67676767U, 0x2b2b2b2bU, + 0xfefefefeU, 0xd7d7d7d7U, 0xababababU, 0x76767676U, + 0xcacacacaU, 0x82828282U, 0xc9c9c9c9U, 0x7d7d7d7dU, + 0xfafafafaU, 0x59595959U, 0x47474747U, 0xf0f0f0f0U, + 0xadadadadU, 0xd4d4d4d4U, 0xa2a2a2a2U, 0xafafafafU, + 0x9c9c9c9cU, 0xa4a4a4a4U, 0x72727272U, 0xc0c0c0c0U, + 0xb7b7b7b7U, 0xfdfdfdfdU, 0x93939393U, 0x26262626U, + 0x36363636U, 0x3f3f3f3fU, 0xf7f7f7f7U, 0xccccccccU, + 0x34343434U, 0xa5a5a5a5U, 0xe5e5e5e5U, 0xf1f1f1f1U, + 0x71717171U, 0xd8d8d8d8U, 0x31313131U, 0x15151515U, + 0x04040404U, 0xc7c7c7c7U, 0x23232323U, 0xc3c3c3c3U, + 0x18181818U, 0x96969696U, 0x05050505U, 0x9a9a9a9aU, + 0x07070707U, 0x12121212U, 0x80808080U, 0xe2e2e2e2U, + 0xebebebebU, 0x27272727U, 0xb2b2b2b2U, 0x75757575U, + 0x09090909U, 0x83838383U, 0x2c2c2c2cU, 0x1a1a1a1aU, + 0x1b1b1b1bU, 0x6e6e6e6eU, 0x5a5a5a5aU, 0xa0a0a0a0U, + 0x52525252U, 0x3b3b3b3bU, 0xd6d6d6d6U, 0xb3b3b3b3U, + 0x29292929U, 0xe3e3e3e3U, 0x2f2f2f2fU, 0x84848484U, + 0x53535353U, 0xd1d1d1d1U, 0x00000000U, 0xededededU, + 0x20202020U, 0xfcfcfcfcU, 0xb1b1b1b1U, 0x5b5b5b5bU, + 0x6a6a6a6aU, 0xcbcbcbcbU, 0xbebebebeU, 0x39393939U, + 0x4a4a4a4aU, 0x4c4c4c4cU, 0x58585858U, 0xcfcfcfcfU, + 0xd0d0d0d0U, 0xefefefefU, 0xaaaaaaaaU, 0xfbfbfbfbU, + 0x43434343U, 0x4d4d4d4dU, 0x33333333U, 0x85858585U, + 0x45454545U, 0xf9f9f9f9U, 0x02020202U, 0x7f7f7f7fU, + 0x50505050U, 0x3c3c3c3cU, 0x9f9f9f9fU, 0xa8a8a8a8U, + 0x51515151U, 0xa3a3a3a3U, 0x40404040U, 0x8f8f8f8fU, + 0x92929292U, 0x9d9d9d9dU, 0x38383838U, 0xf5f5f5f5U, + 0xbcbcbcbcU, 0xb6b6b6b6U, 0xdadadadaU, 0x21212121U, + 0x10101010U, 0xffffffffU, 0xf3f3f3f3U, 0xd2d2d2d2U, + 0xcdcdcdcdU, 0x0c0c0c0cU, 0x13131313U, 0xececececU, + 0x5f5f5f5fU, 0x97979797U, 0x44444444U, 0x17171717U, + 0xc4c4c4c4U, 0xa7a7a7a7U, 0x7e7e7e7eU, 0x3d3d3d3dU, + 0x64646464U, 0x5d5d5d5dU, 0x19191919U, 0x73737373U, + 0x60606060U, 0x81818181U, 0x4f4f4f4fU, 0xdcdcdcdcU, + 0x22222222U, 0x2a2a2a2aU, 0x90909090U, 0x88888888U, + 0x46464646U, 0xeeeeeeeeU, 0xb8b8b8b8U, 0x14141414U, + 0xdedededeU, 0x5e5e5e5eU, 0x0b0b0b0bU, 0xdbdbdbdbU, + 0xe0e0e0e0U, 0x32323232U, 0x3a3a3a3aU, 0x0a0a0a0aU, + 0x49494949U, 0x06060606U, 0x24242424U, 0x5c5c5c5cU, + 0xc2c2c2c2U, 0xd3d3d3d3U, 0xacacacacU, 0x62626262U, + 0x91919191U, 0x95959595U, 0xe4e4e4e4U, 0x79797979U, + 0xe7e7e7e7U, 0xc8c8c8c8U, 0x37373737U, 0x6d6d6d6dU, + 0x8d8d8d8dU, 0xd5d5d5d5U, 0x4e4e4e4eU, 0xa9a9a9a9U, + 0x6c6c6c6cU, 0x56565656U, 0xf4f4f4f4U, 0xeaeaeaeaU, + 0x65656565U, 0x7a7a7a7aU, 0xaeaeaeaeU, 0x08080808U, + 0xbabababaU, 0x78787878U, 0x25252525U, 0x2e2e2e2eU, + 0x1c1c1c1cU, 0xa6a6a6a6U, 0xb4b4b4b4U, 0xc6c6c6c6U, + 0xe8e8e8e8U, 0xddddddddU, 0x74747474U, 0x1f1f1f1fU, + 0x4b4b4b4bU, 0xbdbdbdbdU, 0x8b8b8b8bU, 0x8a8a8a8aU, + 0x70707070U, 0x3e3e3e3eU, 0xb5b5b5b5U, 0x66666666U, + 0x48484848U, 0x03030303U, 0xf6f6f6f6U, 0x0e0e0e0eU, + 0x61616161U, 0x35353535U, 0x57575757U, 0xb9b9b9b9U, + 0x86868686U, 0xc1c1c1c1U, 0x1d1d1d1dU, 0x9e9e9e9eU, + 0xe1e1e1e1U, 0xf8f8f8f8U, 0x98989898U, 0x11111111U, + 0x69696969U, 0xd9d9d9d9U, 0x8e8e8e8eU, 0x94949494U, + 0x9b9b9b9bU, 0x1e1e1e1eU, 0x87878787U, 0xe9e9e9e9U, + 0xcecececeU, 0x55555555U, 0x28282828U, 0xdfdfdfdfU, + 0x8c8c8c8cU, 0xa1a1a1a1U, 0x89898989U, 0x0d0d0d0dU, + 0xbfbfbfbfU, 0xe6e6e6e6U, 0x42424242U, 0x68686868U, + 0x41414141U, 0x99999999U, 0x2d2d2d2dU, 0x0f0f0f0fU, + 0xb0b0b0b0U, 0x54545454U, 0xbbbbbbbbU, 0x16161616U, }; static const cc_u32 SDRM_Td0[256] = { - 0x51f4a750U, 0x7e416553U, 0x1a17a4c3U, 0x3a275e96U, - 0x3bab6bcbU, 0x1f9d45f1U, 0xacfa58abU, 0x4be30393U, - 0x2030fa55U, 0xad766df6U, 0x88cc7691U, 0xf5024c25U, - 0x4fe5d7fcU, 0xc52acbd7U, 0x26354480U, 0xb562a38fU, - 0xdeb15a49U, 0x25ba1b67U, 0x45ea0e98U, 0x5dfec0e1U, - 0xc32f7502U, 0x814cf012U, 0x8d4697a3U, 0x6bd3f9c6U, - 0x038f5fe7U, 0x15929c95U, 0xbf6d7aebU, 0x955259daU, - 0xd4be832dU, 0x587421d3U, 0x49e06929U, 0x8ec9c844U, - 0x75c2896aU, 0xf48e7978U, 0x99583e6bU, 0x27b971ddU, - 0xbee14fb6U, 0xf088ad17U, 0xc920ac66U, 0x7dce3ab4U, - 0x63df4a18U, 0xe51a3182U, 0x97513360U, 0x62537f45U, - 0xb16477e0U, 0xbb6bae84U, 0xfe81a01cU, 0xf9082b94U, - 0x70486858U, 0x8f45fd19U, 0x94de6c87U, 0x527bf8b7U, - 0xab73d323U, 0x724b02e2U, 0xe31f8f57U, 0x6655ab2aU, - 0xb2eb2807U, 0x2fb5c203U, 0x86c57b9aU, 0xd33708a5U, - 0x302887f2U, 0x23bfa5b2U, 0x02036abaU, 0xed16825cU, - 0x8acf1c2bU, 0xa779b492U, 0xf307f2f0U, 0x4e69e2a1U, - 0x65daf4cdU, 0x0605bed5U, 0xd134621fU, 0xc4a6fe8aU, - 0x342e539dU, 0xa2f355a0U, 0x058ae132U, 0xa4f6eb75U, - 0x0b83ec39U, 0x4060efaaU, 0x5e719f06U, 0xbd6e1051U, - 0x3e218af9U, 0x96dd063dU, 0xdd3e05aeU, 0x4de6bd46U, - 0x91548db5U, 0x71c45d05U, 0x0406d46fU, 0x605015ffU, - 0x1998fb24U, 0xd6bde997U, 0x894043ccU, 0x67d99e77U, - 0xb0e842bdU, 0x07898b88U, 0xe7195b38U, 0x79c8eedbU, - 0xa17c0a47U, 0x7c420fe9U, 0xf8841ec9U, 0x00000000U, - 0x09808683U, 0x322bed48U, 0x1e1170acU, 0x6c5a724eU, - 0xfd0efffbU, 0x0f853856U, 0x3daed51eU, 0x362d3927U, - 0x0a0fd964U, 0x685ca621U, 0x9b5b54d1U, 0x24362e3aU, - 0x0c0a67b1U, 0x9357e70fU, 0xb4ee96d2U, 0x1b9b919eU, - 0x80c0c54fU, 0x61dc20a2U, 0x5a774b69U, 0x1c121a16U, - 0xe293ba0aU, 0xc0a02ae5U, 0x3c22e043U, 0x121b171dU, - 0x0e090d0bU, 0xf28bc7adU, 0x2db6a8b9U, 0x141ea9c8U, - 0x57f11985U, 0xaf75074cU, 0xee99ddbbU, 0xa37f60fdU, - 0xf701269fU, 0x5c72f5bcU, 0x44663bc5U, 0x5bfb7e34U, - 0x8b432976U, 0xcb23c6dcU, 0xb6edfc68U, 0xb8e4f163U, - 0xd731dccaU, 0x42638510U, 0x13972240U, 0x84c61120U, - 0x854a247dU, 0xd2bb3df8U, 0xaef93211U, 0xc729a16dU, - 0x1d9e2f4bU, 0xdcb230f3U, 0x0d8652ecU, 0x77c1e3d0U, - 0x2bb3166cU, 0xa970b999U, 0x119448faU, 0x47e96422U, - 0xa8fc8cc4U, 0xa0f03f1aU, 0x567d2cd8U, 0x223390efU, - 0x87494ec7U, 0xd938d1c1U, 0x8ccaa2feU, 0x98d40b36U, - 0xa6f581cfU, 0xa57ade28U, 0xdab78e26U, 0x3fadbfa4U, - 0x2c3a9de4U, 0x5078920dU, 0x6a5fcc9bU, 0x547e4662U, - 0xf68d13c2U, 0x90d8b8e8U, 0x2e39f75eU, 0x82c3aff5U, - 0x9f5d80beU, 0x69d0937cU, 0x6fd52da9U, 0xcf2512b3U, - 0xc8ac993bU, 0x10187da7U, 0xe89c636eU, 0xdb3bbb7bU, - 0xcd267809U, 0x6e5918f4U, 0xec9ab701U, 0x834f9aa8U, - 0xe6956e65U, 0xaaffe67eU, 0x21bccf08U, 0xef15e8e6U, - 0xbae79bd9U, 0x4a6f36ceU, 0xea9f09d4U, 0x29b07cd6U, - 0x31a4b2afU, 0x2a3f2331U, 0xc6a59430U, 0x35a266c0U, - 0x744ebc37U, 0xfc82caa6U, 0xe090d0b0U, 0x33a7d815U, - 0xf104984aU, 0x41ecdaf7U, 0x7fcd500eU, 0x1791f62fU, - 0x764dd68dU, 0x43efb04dU, 0xccaa4d54U, 0xe49604dfU, - 0x9ed1b5e3U, 0x4c6a881bU, 0xc12c1fb8U, 0x4665517fU, - 0x9d5eea04U, 0x018c355dU, 0xfa877473U, 0xfb0b412eU, - 0xb3671d5aU, 0x92dbd252U, 0xe9105633U, 0x6dd64713U, - 0x9ad7618cU, 0x37a10c7aU, 0x59f8148eU, 0xeb133c89U, - 0xcea927eeU, 0xb761c935U, 0xe11ce5edU, 0x7a47b13cU, - 0x9cd2df59U, 0x55f2733fU, 0x1814ce79U, 0x73c737bfU, - 0x53f7cdeaU, 0x5ffdaa5bU, 0xdf3d6f14U, 0x7844db86U, - 0xcaaff381U, 0xb968c43eU, 0x3824342cU, 0xc2a3405fU, - 0x161dc372U, 0xbce2250cU, 0x283c498bU, 0xff0d9541U, - 0x39a80171U, 0x080cb3deU, 0xd8b4e49cU, 0x6456c190U, - 0x7bcb8461U, 0xd532b670U, 0x486c5c74U, 0xd0b85742U, + 0x51f4a750U, 0x7e416553U, 0x1a17a4c3U, 0x3a275e96U, + 0x3bab6bcbU, 0x1f9d45f1U, 0xacfa58abU, 0x4be30393U, + 0x2030fa55U, 0xad766df6U, 0x88cc7691U, 0xf5024c25U, + 0x4fe5d7fcU, 0xc52acbd7U, 0x26354480U, 0xb562a38fU, + 0xdeb15a49U, 0x25ba1b67U, 0x45ea0e98U, 0x5dfec0e1U, + 0xc32f7502U, 0x814cf012U, 0x8d4697a3U, 0x6bd3f9c6U, + 0x038f5fe7U, 0x15929c95U, 0xbf6d7aebU, 0x955259daU, + 0xd4be832dU, 0x587421d3U, 0x49e06929U, 0x8ec9c844U, + 0x75c2896aU, 0xf48e7978U, 0x99583e6bU, 0x27b971ddU, + 0xbee14fb6U, 0xf088ad17U, 0xc920ac66U, 0x7dce3ab4U, + 0x63df4a18U, 0xe51a3182U, 0x97513360U, 0x62537f45U, + 0xb16477e0U, 0xbb6bae84U, 0xfe81a01cU, 0xf9082b94U, + 0x70486858U, 0x8f45fd19U, 0x94de6c87U, 0x527bf8b7U, + 0xab73d323U, 0x724b02e2U, 0xe31f8f57U, 0x6655ab2aU, + 0xb2eb2807U, 0x2fb5c203U, 0x86c57b9aU, 0xd33708a5U, + 0x302887f2U, 0x23bfa5b2U, 0x02036abaU, 0xed16825cU, + 0x8acf1c2bU, 0xa779b492U, 0xf307f2f0U, 0x4e69e2a1U, + 0x65daf4cdU, 0x0605bed5U, 0xd134621fU, 0xc4a6fe8aU, + 0x342e539dU, 0xa2f355a0U, 0x058ae132U, 0xa4f6eb75U, + 0x0b83ec39U, 0x4060efaaU, 0x5e719f06U, 0xbd6e1051U, + 0x3e218af9U, 0x96dd063dU, 0xdd3e05aeU, 0x4de6bd46U, + 0x91548db5U, 0x71c45d05U, 0x0406d46fU, 0x605015ffU, + 0x1998fb24U, 0xd6bde997U, 0x894043ccU, 0x67d99e77U, + 0xb0e842bdU, 0x07898b88U, 0xe7195b38U, 0x79c8eedbU, + 0xa17c0a47U, 0x7c420fe9U, 0xf8841ec9U, 0x00000000U, + 0x09808683U, 0x322bed48U, 0x1e1170acU, 0x6c5a724eU, + 0xfd0efffbU, 0x0f853856U, 0x3daed51eU, 0x362d3927U, + 0x0a0fd964U, 0x685ca621U, 0x9b5b54d1U, 0x24362e3aU, + 0x0c0a67b1U, 0x9357e70fU, 0xb4ee96d2U, 0x1b9b919eU, + 0x80c0c54fU, 0x61dc20a2U, 0x5a774b69U, 0x1c121a16U, + 0xe293ba0aU, 0xc0a02ae5U, 0x3c22e043U, 0x121b171dU, + 0x0e090d0bU, 0xf28bc7adU, 0x2db6a8b9U, 0x141ea9c8U, + 0x57f11985U, 0xaf75074cU, 0xee99ddbbU, 0xa37f60fdU, + 0xf701269fU, 0x5c72f5bcU, 0x44663bc5U, 0x5bfb7e34U, + 0x8b432976U, 0xcb23c6dcU, 0xb6edfc68U, 0xb8e4f163U, + 0xd731dccaU, 0x42638510U, 0x13972240U, 0x84c61120U, + 0x854a247dU, 0xd2bb3df8U, 0xaef93211U, 0xc729a16dU, + 0x1d9e2f4bU, 0xdcb230f3U, 0x0d8652ecU, 0x77c1e3d0U, + 0x2bb3166cU, 0xa970b999U, 0x119448faU, 0x47e96422U, + 0xa8fc8cc4U, 0xa0f03f1aU, 0x567d2cd8U, 0x223390efU, + 0x87494ec7U, 0xd938d1c1U, 0x8ccaa2feU, 0x98d40b36U, + 0xa6f581cfU, 0xa57ade28U, 0xdab78e26U, 0x3fadbfa4U, + 0x2c3a9de4U, 0x5078920dU, 0x6a5fcc9bU, 0x547e4662U, + 0xf68d13c2U, 0x90d8b8e8U, 0x2e39f75eU, 0x82c3aff5U, + 0x9f5d80beU, 0x69d0937cU, 0x6fd52da9U, 0xcf2512b3U, + 0xc8ac993bU, 0x10187da7U, 0xe89c636eU, 0xdb3bbb7bU, + 0xcd267809U, 0x6e5918f4U, 0xec9ab701U, 0x834f9aa8U, + 0xe6956e65U, 0xaaffe67eU, 0x21bccf08U, 0xef15e8e6U, + 0xbae79bd9U, 0x4a6f36ceU, 0xea9f09d4U, 0x29b07cd6U, + 0x31a4b2afU, 0x2a3f2331U, 0xc6a59430U, 0x35a266c0U, + 0x744ebc37U, 0xfc82caa6U, 0xe090d0b0U, 0x33a7d815U, + 0xf104984aU, 0x41ecdaf7U, 0x7fcd500eU, 0x1791f62fU, + 0x764dd68dU, 0x43efb04dU, 0xccaa4d54U, 0xe49604dfU, + 0x9ed1b5e3U, 0x4c6a881bU, 0xc12c1fb8U, 0x4665517fU, + 0x9d5eea04U, 0x018c355dU, 0xfa877473U, 0xfb0b412eU, + 0xb3671d5aU, 0x92dbd252U, 0xe9105633U, 0x6dd64713U, + 0x9ad7618cU, 0x37a10c7aU, 0x59f8148eU, 0xeb133c89U, + 0xcea927eeU, 0xb761c935U, 0xe11ce5edU, 0x7a47b13cU, + 0x9cd2df59U, 0x55f2733fU, 0x1814ce79U, 0x73c737bfU, + 0x53f7cdeaU, 0x5ffdaa5bU, 0xdf3d6f14U, 0x7844db86U, + 0xcaaff381U, 0xb968c43eU, 0x3824342cU, 0xc2a3405fU, + 0x161dc372U, 0xbce2250cU, 0x283c498bU, 0xff0d9541U, + 0x39a80171U, 0x080cb3deU, 0xd8b4e49cU, 0x6456c190U, + 0x7bcb8461U, 0xd532b670U, 0x486c5c74U, 0xd0b85742U, }; static const cc_u32 SDRM_Td1[256] = { - 0x5051f4a7U, 0x537e4165U, 0xc31a17a4U, 0x963a275eU, - 0xcb3bab6bU, 0xf11f9d45U, 0xabacfa58U, 0x934be303U, - 0x552030faU, 0xf6ad766dU, 0x9188cc76U, 0x25f5024cU, - 0xfc4fe5d7U, 0xd7c52acbU, 0x80263544U, 0x8fb562a3U, - 0x49deb15aU, 0x6725ba1bU, 0x9845ea0eU, 0xe15dfec0U, - 0x02c32f75U, 0x12814cf0U, 0xa38d4697U, 0xc66bd3f9U, - 0xe7038f5fU, 0x9515929cU, 0xebbf6d7aU, 0xda955259U, - 0x2dd4be83U, 0xd3587421U, 0x2949e069U, 0x448ec9c8U, - 0x6a75c289U, 0x78f48e79U, 0x6b99583eU, 0xdd27b971U, - 0xb6bee14fU, 0x17f088adU, 0x66c920acU, 0xb47dce3aU, - 0x1863df4aU, 0x82e51a31U, 0x60975133U, 0x4562537fU, - 0xe0b16477U, 0x84bb6baeU, 0x1cfe81a0U, 0x94f9082bU, - 0x58704868U, 0x198f45fdU, 0x8794de6cU, 0xb7527bf8U, - 0x23ab73d3U, 0xe2724b02U, 0x57e31f8fU, 0x2a6655abU, - 0x07b2eb28U, 0x032fb5c2U, 0x9a86c57bU, 0xa5d33708U, - 0xf2302887U, 0xb223bfa5U, 0xba02036aU, 0x5ced1682U, - 0x2b8acf1cU, 0x92a779b4U, 0xf0f307f2U, 0xa14e69e2U, - 0xcd65daf4U, 0xd50605beU, 0x1fd13462U, 0x8ac4a6feU, - 0x9d342e53U, 0xa0a2f355U, 0x32058ae1U, 0x75a4f6ebU, - 0x390b83ecU, 0xaa4060efU, 0x065e719fU, 0x51bd6e10U, - 0xf93e218aU, 0x3d96dd06U, 0xaedd3e05U, 0x464de6bdU, - 0xb591548dU, 0x0571c45dU, 0x6f0406d4U, 0xff605015U, - 0x241998fbU, 0x97d6bde9U, 0xcc894043U, 0x7767d99eU, - 0xbdb0e842U, 0x8807898bU, 0x38e7195bU, 0xdb79c8eeU, - 0x47a17c0aU, 0xe97c420fU, 0xc9f8841eU, 0x00000000U, - 0x83098086U, 0x48322bedU, 0xac1e1170U, 0x4e6c5a72U, - 0xfbfd0effU, 0x560f8538U, 0x1e3daed5U, 0x27362d39U, - 0x640a0fd9U, 0x21685ca6U, 0xd19b5b54U, 0x3a24362eU, - 0xb10c0a67U, 0x0f9357e7U, 0xd2b4ee96U, 0x9e1b9b91U, - 0x4f80c0c5U, 0xa261dc20U, 0x695a774bU, 0x161c121aU, - 0x0ae293baU, 0xe5c0a02aU, 0x433c22e0U, 0x1d121b17U, - 0x0b0e090dU, 0xadf28bc7U, 0xb92db6a8U, 0xc8141ea9U, - 0x8557f119U, 0x4caf7507U, 0xbbee99ddU, 0xfda37f60U, - 0x9ff70126U, 0xbc5c72f5U, 0xc544663bU, 0x345bfb7eU, - 0x768b4329U, 0xdccb23c6U, 0x68b6edfcU, 0x63b8e4f1U, - 0xcad731dcU, 0x10426385U, 0x40139722U, 0x2084c611U, - 0x7d854a24U, 0xf8d2bb3dU, 0x11aef932U, 0x6dc729a1U, - 0x4b1d9e2fU, 0xf3dcb230U, 0xec0d8652U, 0xd077c1e3U, - 0x6c2bb316U, 0x99a970b9U, 0xfa119448U, 0x2247e964U, - 0xc4a8fc8cU, 0x1aa0f03fU, 0xd8567d2cU, 0xef223390U, - 0xc787494eU, 0xc1d938d1U, 0xfe8ccaa2U, 0x3698d40bU, - 0xcfa6f581U, 0x28a57adeU, 0x26dab78eU, 0xa43fadbfU, - 0xe42c3a9dU, 0x0d507892U, 0x9b6a5fccU, 0x62547e46U, - 0xc2f68d13U, 0xe890d8b8U, 0x5e2e39f7U, 0xf582c3afU, - 0xbe9f5d80U, 0x7c69d093U, 0xa96fd52dU, 0xb3cf2512U, - 0x3bc8ac99U, 0xa710187dU, 0x6ee89c63U, 0x7bdb3bbbU, - 0x09cd2678U, 0xf46e5918U, 0x01ec9ab7U, 0xa8834f9aU, - 0x65e6956eU, 0x7eaaffe6U, 0x0821bccfU, 0xe6ef15e8U, - 0xd9bae79bU, 0xce4a6f36U, 0xd4ea9f09U, 0xd629b07cU, - 0xaf31a4b2U, 0x312a3f23U, 0x30c6a594U, 0xc035a266U, - 0x37744ebcU, 0xa6fc82caU, 0xb0e090d0U, 0x1533a7d8U, - 0x4af10498U, 0xf741ecdaU, 0x0e7fcd50U, 0x2f1791f6U, - 0x8d764dd6U, 0x4d43efb0U, 0x54ccaa4dU, 0xdfe49604U, - 0xe39ed1b5U, 0x1b4c6a88U, 0xb8c12c1fU, 0x7f466551U, - 0x049d5eeaU, 0x5d018c35U, 0x73fa8774U, 0x2efb0b41U, - 0x5ab3671dU, 0x5292dbd2U, 0x33e91056U, 0x136dd647U, - 0x8c9ad761U, 0x7a37a10cU, 0x8e59f814U, 0x89eb133cU, - 0xeecea927U, 0x35b761c9U, 0xede11ce5U, 0x3c7a47b1U, - 0x599cd2dfU, 0x3f55f273U, 0x791814ceU, 0xbf73c737U, - 0xea53f7cdU, 0x5b5ffdaaU, 0x14df3d6fU, 0x867844dbU, - 0x81caaff3U, 0x3eb968c4U, 0x2c382434U, 0x5fc2a340U, - 0x72161dc3U, 0x0cbce225U, 0x8b283c49U, 0x41ff0d95U, - 0x7139a801U, 0xde080cb3U, 0x9cd8b4e4U, 0x906456c1U, - 0x617bcb84U, 0x70d532b6U, 0x74486c5cU, 0x42d0b857U, + 0x5051f4a7U, 0x537e4165U, 0xc31a17a4U, 0x963a275eU, + 0xcb3bab6bU, 0xf11f9d45U, 0xabacfa58U, 0x934be303U, + 0x552030faU, 0xf6ad766dU, 0x9188cc76U, 0x25f5024cU, + 0xfc4fe5d7U, 0xd7c52acbU, 0x80263544U, 0x8fb562a3U, + 0x49deb15aU, 0x6725ba1bU, 0x9845ea0eU, 0xe15dfec0U, + 0x02c32f75U, 0x12814cf0U, 0xa38d4697U, 0xc66bd3f9U, + 0xe7038f5fU, 0x9515929cU, 0xebbf6d7aU, 0xda955259U, + 0x2dd4be83U, 0xd3587421U, 0x2949e069U, 0x448ec9c8U, + 0x6a75c289U, 0x78f48e79U, 0x6b99583eU, 0xdd27b971U, + 0xb6bee14fU, 0x17f088adU, 0x66c920acU, 0xb47dce3aU, + 0x1863df4aU, 0x82e51a31U, 0x60975133U, 0x4562537fU, + 0xe0b16477U, 0x84bb6baeU, 0x1cfe81a0U, 0x94f9082bU, + 0x58704868U, 0x198f45fdU, 0x8794de6cU, 0xb7527bf8U, + 0x23ab73d3U, 0xe2724b02U, 0x57e31f8fU, 0x2a6655abU, + 0x07b2eb28U, 0x032fb5c2U, 0x9a86c57bU, 0xa5d33708U, + 0xf2302887U, 0xb223bfa5U, 0xba02036aU, 0x5ced1682U, + 0x2b8acf1cU, 0x92a779b4U, 0xf0f307f2U, 0xa14e69e2U, + 0xcd65daf4U, 0xd50605beU, 0x1fd13462U, 0x8ac4a6feU, + 0x9d342e53U, 0xa0a2f355U, 0x32058ae1U, 0x75a4f6ebU, + 0x390b83ecU, 0xaa4060efU, 0x065e719fU, 0x51bd6e10U, + 0xf93e218aU, 0x3d96dd06U, 0xaedd3e05U, 0x464de6bdU, + 0xb591548dU, 0x0571c45dU, 0x6f0406d4U, 0xff605015U, + 0x241998fbU, 0x97d6bde9U, 0xcc894043U, 0x7767d99eU, + 0xbdb0e842U, 0x8807898bU, 0x38e7195bU, 0xdb79c8eeU, + 0x47a17c0aU, 0xe97c420fU, 0xc9f8841eU, 0x00000000U, + 0x83098086U, 0x48322bedU, 0xac1e1170U, 0x4e6c5a72U, + 0xfbfd0effU, 0x560f8538U, 0x1e3daed5U, 0x27362d39U, + 0x640a0fd9U, 0x21685ca6U, 0xd19b5b54U, 0x3a24362eU, + 0xb10c0a67U, 0x0f9357e7U, 0xd2b4ee96U, 0x9e1b9b91U, + 0x4f80c0c5U, 0xa261dc20U, 0x695a774bU, 0x161c121aU, + 0x0ae293baU, 0xe5c0a02aU, 0x433c22e0U, 0x1d121b17U, + 0x0b0e090dU, 0xadf28bc7U, 0xb92db6a8U, 0xc8141ea9U, + 0x8557f119U, 0x4caf7507U, 0xbbee99ddU, 0xfda37f60U, + 0x9ff70126U, 0xbc5c72f5U, 0xc544663bU, 0x345bfb7eU, + 0x768b4329U, 0xdccb23c6U, 0x68b6edfcU, 0x63b8e4f1U, + 0xcad731dcU, 0x10426385U, 0x40139722U, 0x2084c611U, + 0x7d854a24U, 0xf8d2bb3dU, 0x11aef932U, 0x6dc729a1U, + 0x4b1d9e2fU, 0xf3dcb230U, 0xec0d8652U, 0xd077c1e3U, + 0x6c2bb316U, 0x99a970b9U, 0xfa119448U, 0x2247e964U, + 0xc4a8fc8cU, 0x1aa0f03fU, 0xd8567d2cU, 0xef223390U, + 0xc787494eU, 0xc1d938d1U, 0xfe8ccaa2U, 0x3698d40bU, + 0xcfa6f581U, 0x28a57adeU, 0x26dab78eU, 0xa43fadbfU, + 0xe42c3a9dU, 0x0d507892U, 0x9b6a5fccU, 0x62547e46U, + 0xc2f68d13U, 0xe890d8b8U, 0x5e2e39f7U, 0xf582c3afU, + 0xbe9f5d80U, 0x7c69d093U, 0xa96fd52dU, 0xb3cf2512U, + 0x3bc8ac99U, 0xa710187dU, 0x6ee89c63U, 0x7bdb3bbbU, + 0x09cd2678U, 0xf46e5918U, 0x01ec9ab7U, 0xa8834f9aU, + 0x65e6956eU, 0x7eaaffe6U, 0x0821bccfU, 0xe6ef15e8U, + 0xd9bae79bU, 0xce4a6f36U, 0xd4ea9f09U, 0xd629b07cU, + 0xaf31a4b2U, 0x312a3f23U, 0x30c6a594U, 0xc035a266U, + 0x37744ebcU, 0xa6fc82caU, 0xb0e090d0U, 0x1533a7d8U, + 0x4af10498U, 0xf741ecdaU, 0x0e7fcd50U, 0x2f1791f6U, + 0x8d764dd6U, 0x4d43efb0U, 0x54ccaa4dU, 0xdfe49604U, + 0xe39ed1b5U, 0x1b4c6a88U, 0xb8c12c1fU, 0x7f466551U, + 0x049d5eeaU, 0x5d018c35U, 0x73fa8774U, 0x2efb0b41U, + 0x5ab3671dU, 0x5292dbd2U, 0x33e91056U, 0x136dd647U, + 0x8c9ad761U, 0x7a37a10cU, 0x8e59f814U, 0x89eb133cU, + 0xeecea927U, 0x35b761c9U, 0xede11ce5U, 0x3c7a47b1U, + 0x599cd2dfU, 0x3f55f273U, 0x791814ceU, 0xbf73c737U, + 0xea53f7cdU, 0x5b5ffdaaU, 0x14df3d6fU, 0x867844dbU, + 0x81caaff3U, 0x3eb968c4U, 0x2c382434U, 0x5fc2a340U, + 0x72161dc3U, 0x0cbce225U, 0x8b283c49U, 0x41ff0d95U, + 0x7139a801U, 0xde080cb3U, 0x9cd8b4e4U, 0x906456c1U, + 0x617bcb84U, 0x70d532b6U, 0x74486c5cU, 0x42d0b857U, }; static const cc_u32 SDRM_Td2[256] = { - 0xa75051f4U, 0x65537e41U, 0xa4c31a17U, 0x5e963a27U, - 0x6bcb3babU, 0x45f11f9dU, 0x58abacfaU, 0x03934be3U, - 0xfa552030U, 0x6df6ad76U, 0x769188ccU, 0x4c25f502U, - 0xd7fc4fe5U, 0xcbd7c52aU, 0x44802635U, 0xa38fb562U, - 0x5a49deb1U, 0x1b6725baU, 0x0e9845eaU, 0xc0e15dfeU, - 0x7502c32fU, 0xf012814cU, 0x97a38d46U, 0xf9c66bd3U, - 0x5fe7038fU, 0x9c951592U, 0x7aebbf6dU, 0x59da9552U, - 0x832dd4beU, 0x21d35874U, 0x692949e0U, 0xc8448ec9U, - 0x896a75c2U, 0x7978f48eU, 0x3e6b9958U, 0x71dd27b9U, - 0x4fb6bee1U, 0xad17f088U, 0xac66c920U, 0x3ab47dceU, - 0x4a1863dfU, 0x3182e51aU, 0x33609751U, 0x7f456253U, - 0x77e0b164U, 0xae84bb6bU, 0xa01cfe81U, 0x2b94f908U, - 0x68587048U, 0xfd198f45U, 0x6c8794deU, 0xf8b7527bU, - 0xd323ab73U, 0x02e2724bU, 0x8f57e31fU, 0xab2a6655U, - 0x2807b2ebU, 0xc2032fb5U, 0x7b9a86c5U, 0x08a5d337U, - 0x87f23028U, 0xa5b223bfU, 0x6aba0203U, 0x825ced16U, - 0x1c2b8acfU, 0xb492a779U, 0xf2f0f307U, 0xe2a14e69U, - 0xf4cd65daU, 0xbed50605U, 0x621fd134U, 0xfe8ac4a6U, - 0x539d342eU, 0x55a0a2f3U, 0xe132058aU, 0xeb75a4f6U, - 0xec390b83U, 0xefaa4060U, 0x9f065e71U, 0x1051bd6eU, - 0x8af93e21U, 0x063d96ddU, 0x05aedd3eU, 0xbd464de6U, - 0x8db59154U, 0x5d0571c4U, 0xd46f0406U, 0x15ff6050U, - 0xfb241998U, 0xe997d6bdU, 0x43cc8940U, 0x9e7767d9U, - 0x42bdb0e8U, 0x8b880789U, 0x5b38e719U, 0xeedb79c8U, - 0x0a47a17cU, 0x0fe97c42U, 0x1ec9f884U, 0x00000000U, - 0x86830980U, 0xed48322bU, 0x70ac1e11U, 0x724e6c5aU, - 0xfffbfd0eU, 0x38560f85U, 0xd51e3daeU, 0x3927362dU, - 0xd9640a0fU, 0xa621685cU, 0x54d19b5bU, 0x2e3a2436U, - 0x67b10c0aU, 0xe70f9357U, 0x96d2b4eeU, 0x919e1b9bU, - 0xc54f80c0U, 0x20a261dcU, 0x4b695a77U, 0x1a161c12U, - 0xba0ae293U, 0x2ae5c0a0U, 0xe0433c22U, 0x171d121bU, - 0x0d0b0e09U, 0xc7adf28bU, 0xa8b92db6U, 0xa9c8141eU, - 0x198557f1U, 0x074caf75U, 0xddbbee99U, 0x60fda37fU, - 0x269ff701U, 0xf5bc5c72U, 0x3bc54466U, 0x7e345bfbU, - 0x29768b43U, 0xc6dccb23U, 0xfc68b6edU, 0xf163b8e4U, - 0xdccad731U, 0x85104263U, 0x22401397U, 0x112084c6U, - 0x247d854aU, 0x3df8d2bbU, 0x3211aef9U, 0xa16dc729U, - 0x2f4b1d9eU, 0x30f3dcb2U, 0x52ec0d86U, 0xe3d077c1U, - 0x166c2bb3U, 0xb999a970U, 0x48fa1194U, 0x642247e9U, - 0x8cc4a8fcU, 0x3f1aa0f0U, 0x2cd8567dU, 0x90ef2233U, - 0x4ec78749U, 0xd1c1d938U, 0xa2fe8ccaU, 0x0b3698d4U, - 0x81cfa6f5U, 0xde28a57aU, 0x8e26dab7U, 0xbfa43fadU, - 0x9de42c3aU, 0x920d5078U, 0xcc9b6a5fU, 0x4662547eU, - 0x13c2f68dU, 0xb8e890d8U, 0xf75e2e39U, 0xaff582c3U, - 0x80be9f5dU, 0x937c69d0U, 0x2da96fd5U, 0x12b3cf25U, - 0x993bc8acU, 0x7da71018U, 0x636ee89cU, 0xbb7bdb3bU, - 0x7809cd26U, 0x18f46e59U, 0xb701ec9aU, 0x9aa8834fU, - 0x6e65e695U, 0xe67eaaffU, 0xcf0821bcU, 0xe8e6ef15U, - 0x9bd9bae7U, 0x36ce4a6fU, 0x09d4ea9fU, 0x7cd629b0U, - 0xb2af31a4U, 0x23312a3fU, 0x9430c6a5U, 0x66c035a2U, - 0xbc37744eU, 0xcaa6fc82U, 0xd0b0e090U, 0xd81533a7U, - 0x984af104U, 0xdaf741ecU, 0x500e7fcdU, 0xf62f1791U, - 0xd68d764dU, 0xb04d43efU, 0x4d54ccaaU, 0x04dfe496U, - 0xb5e39ed1U, 0x881b4c6aU, 0x1fb8c12cU, 0x517f4665U, - 0xea049d5eU, 0x355d018cU, 0x7473fa87U, 0x412efb0bU, - 0x1d5ab367U, 0xd25292dbU, 0x5633e910U, 0x47136dd6U, - 0x618c9ad7U, 0x0c7a37a1U, 0x148e59f8U, 0x3c89eb13U, - 0x27eecea9U, 0xc935b761U, 0xe5ede11cU, 0xb13c7a47U, - 0xdf599cd2U, 0x733f55f2U, 0xce791814U, 0x37bf73c7U, - 0xcdea53f7U, 0xaa5b5ffdU, 0x6f14df3dU, 0xdb867844U, - 0xf381caafU, 0xc43eb968U, 0x342c3824U, 0x405fc2a3U, - 0xc372161dU, 0x250cbce2U, 0x498b283cU, 0x9541ff0dU, - 0x017139a8U, 0xb3de080cU, 0xe49cd8b4U, 0xc1906456U, - 0x84617bcbU, 0xb670d532U, 0x5c74486cU, 0x5742d0b8U, + 0xa75051f4U, 0x65537e41U, 0xa4c31a17U, 0x5e963a27U, + 0x6bcb3babU, 0x45f11f9dU, 0x58abacfaU, 0x03934be3U, + 0xfa552030U, 0x6df6ad76U, 0x769188ccU, 0x4c25f502U, + 0xd7fc4fe5U, 0xcbd7c52aU, 0x44802635U, 0xa38fb562U, + 0x5a49deb1U, 0x1b6725baU, 0x0e9845eaU, 0xc0e15dfeU, + 0x7502c32fU, 0xf012814cU, 0x97a38d46U, 0xf9c66bd3U, + 0x5fe7038fU, 0x9c951592U, 0x7aebbf6dU, 0x59da9552U, + 0x832dd4beU, 0x21d35874U, 0x692949e0U, 0xc8448ec9U, + 0x896a75c2U, 0x7978f48eU, 0x3e6b9958U, 0x71dd27b9U, + 0x4fb6bee1U, 0xad17f088U, 0xac66c920U, 0x3ab47dceU, + 0x4a1863dfU, 0x3182e51aU, 0x33609751U, 0x7f456253U, + 0x77e0b164U, 0xae84bb6bU, 0xa01cfe81U, 0x2b94f908U, + 0x68587048U, 0xfd198f45U, 0x6c8794deU, 0xf8b7527bU, + 0xd323ab73U, 0x02e2724bU, 0x8f57e31fU, 0xab2a6655U, + 0x2807b2ebU, 0xc2032fb5U, 0x7b9a86c5U, 0x08a5d337U, + 0x87f23028U, 0xa5b223bfU, 0x6aba0203U, 0x825ced16U, + 0x1c2b8acfU, 0xb492a779U, 0xf2f0f307U, 0xe2a14e69U, + 0xf4cd65daU, 0xbed50605U, 0x621fd134U, 0xfe8ac4a6U, + 0x539d342eU, 0x55a0a2f3U, 0xe132058aU, 0xeb75a4f6U, + 0xec390b83U, 0xefaa4060U, 0x9f065e71U, 0x1051bd6eU, + 0x8af93e21U, 0x063d96ddU, 0x05aedd3eU, 0xbd464de6U, + 0x8db59154U, 0x5d0571c4U, 0xd46f0406U, 0x15ff6050U, + 0xfb241998U, 0xe997d6bdU, 0x43cc8940U, 0x9e7767d9U, + 0x42bdb0e8U, 0x8b880789U, 0x5b38e719U, 0xeedb79c8U, + 0x0a47a17cU, 0x0fe97c42U, 0x1ec9f884U, 0x00000000U, + 0x86830980U, 0xed48322bU, 0x70ac1e11U, 0x724e6c5aU, + 0xfffbfd0eU, 0x38560f85U, 0xd51e3daeU, 0x3927362dU, + 0xd9640a0fU, 0xa621685cU, 0x54d19b5bU, 0x2e3a2436U, + 0x67b10c0aU, 0xe70f9357U, 0x96d2b4eeU, 0x919e1b9bU, + 0xc54f80c0U, 0x20a261dcU, 0x4b695a77U, 0x1a161c12U, + 0xba0ae293U, 0x2ae5c0a0U, 0xe0433c22U, 0x171d121bU, + 0x0d0b0e09U, 0xc7adf28bU, 0xa8b92db6U, 0xa9c8141eU, + 0x198557f1U, 0x074caf75U, 0xddbbee99U, 0x60fda37fU, + 0x269ff701U, 0xf5bc5c72U, 0x3bc54466U, 0x7e345bfbU, + 0x29768b43U, 0xc6dccb23U, 0xfc68b6edU, 0xf163b8e4U, + 0xdccad731U, 0x85104263U, 0x22401397U, 0x112084c6U, + 0x247d854aU, 0x3df8d2bbU, 0x3211aef9U, 0xa16dc729U, + 0x2f4b1d9eU, 0x30f3dcb2U, 0x52ec0d86U, 0xe3d077c1U, + 0x166c2bb3U, 0xb999a970U, 0x48fa1194U, 0x642247e9U, + 0x8cc4a8fcU, 0x3f1aa0f0U, 0x2cd8567dU, 0x90ef2233U, + 0x4ec78749U, 0xd1c1d938U, 0xa2fe8ccaU, 0x0b3698d4U, + 0x81cfa6f5U, 0xde28a57aU, 0x8e26dab7U, 0xbfa43fadU, + 0x9de42c3aU, 0x920d5078U, 0xcc9b6a5fU, 0x4662547eU, + 0x13c2f68dU, 0xb8e890d8U, 0xf75e2e39U, 0xaff582c3U, + 0x80be9f5dU, 0x937c69d0U, 0x2da96fd5U, 0x12b3cf25U, + 0x993bc8acU, 0x7da71018U, 0x636ee89cU, 0xbb7bdb3bU, + 0x7809cd26U, 0x18f46e59U, 0xb701ec9aU, 0x9aa8834fU, + 0x6e65e695U, 0xe67eaaffU, 0xcf0821bcU, 0xe8e6ef15U, + 0x9bd9bae7U, 0x36ce4a6fU, 0x09d4ea9fU, 0x7cd629b0U, + 0xb2af31a4U, 0x23312a3fU, 0x9430c6a5U, 0x66c035a2U, + 0xbc37744eU, 0xcaa6fc82U, 0xd0b0e090U, 0xd81533a7U, + 0x984af104U, 0xdaf741ecU, 0x500e7fcdU, 0xf62f1791U, + 0xd68d764dU, 0xb04d43efU, 0x4d54ccaaU, 0x04dfe496U, + 0xb5e39ed1U, 0x881b4c6aU, 0x1fb8c12cU, 0x517f4665U, + 0xea049d5eU, 0x355d018cU, 0x7473fa87U, 0x412efb0bU, + 0x1d5ab367U, 0xd25292dbU, 0x5633e910U, 0x47136dd6U, + 0x618c9ad7U, 0x0c7a37a1U, 0x148e59f8U, 0x3c89eb13U, + 0x27eecea9U, 0xc935b761U, 0xe5ede11cU, 0xb13c7a47U, + 0xdf599cd2U, 0x733f55f2U, 0xce791814U, 0x37bf73c7U, + 0xcdea53f7U, 0xaa5b5ffdU, 0x6f14df3dU, 0xdb867844U, + 0xf381caafU, 0xc43eb968U, 0x342c3824U, 0x405fc2a3U, + 0xc372161dU, 0x250cbce2U, 0x498b283cU, 0x9541ff0dU, + 0x017139a8U, 0xb3de080cU, 0xe49cd8b4U, 0xc1906456U, + 0x84617bcbU, 0xb670d532U, 0x5c74486cU, 0x5742d0b8U, }; static const cc_u32 SDRM_Td3[256] = { - 0xf4a75051U, 0x4165537eU, 0x17a4c31aU, 0x275e963aU, - 0xab6bcb3bU, 0x9d45f11fU, 0xfa58abacU, 0xe303934bU, - 0x30fa5520U, 0x766df6adU, 0xcc769188U, 0x024c25f5U, - 0xe5d7fc4fU, 0x2acbd7c5U, 0x35448026U, 0x62a38fb5U, - 0xb15a49deU, 0xba1b6725U, 0xea0e9845U, 0xfec0e15dU, - 0x2f7502c3U, 0x4cf01281U, 0x4697a38dU, 0xd3f9c66bU, - 0x8f5fe703U, 0x929c9515U, 0x6d7aebbfU, 0x5259da95U, - 0xbe832dd4U, 0x7421d358U, 0xe0692949U, 0xc9c8448eU, - 0xc2896a75U, 0x8e7978f4U, 0x583e6b99U, 0xb971dd27U, - 0xe14fb6beU, 0x88ad17f0U, 0x20ac66c9U, 0xce3ab47dU, - 0xdf4a1863U, 0x1a3182e5U, 0x51336097U, 0x537f4562U, - 0x6477e0b1U, 0x6bae84bbU, 0x81a01cfeU, 0x082b94f9U, - 0x48685870U, 0x45fd198fU, 0xde6c8794U, 0x7bf8b752U, - 0x73d323abU, 0x4b02e272U, 0x1f8f57e3U, 0x55ab2a66U, - 0xeb2807b2U, 0xb5c2032fU, 0xc57b9a86U, 0x3708a5d3U, - 0x2887f230U, 0xbfa5b223U, 0x036aba02U, 0x16825cedU, - 0xcf1c2b8aU, 0x79b492a7U, 0x07f2f0f3U, 0x69e2a14eU, - 0xdaf4cd65U, 0x05bed506U, 0x34621fd1U, 0xa6fe8ac4U, - 0x2e539d34U, 0xf355a0a2U, 0x8ae13205U, 0xf6eb75a4U, - 0x83ec390bU, 0x60efaa40U, 0x719f065eU, 0x6e1051bdU, - 0x218af93eU, 0xdd063d96U, 0x3e05aeddU, 0xe6bd464dU, - 0x548db591U, 0xc45d0571U, 0x06d46f04U, 0x5015ff60U, - 0x98fb2419U, 0xbde997d6U, 0x4043cc89U, 0xd99e7767U, - 0xe842bdb0U, 0x898b8807U, 0x195b38e7U, 0xc8eedb79U, - 0x7c0a47a1U, 0x420fe97cU, 0x841ec9f8U, 0x00000000U, - 0x80868309U, 0x2bed4832U, 0x1170ac1eU, 0x5a724e6cU, - 0x0efffbfdU, 0x8538560fU, 0xaed51e3dU, 0x2d392736U, - 0x0fd9640aU, 0x5ca62168U, 0x5b54d19bU, 0x362e3a24U, - 0x0a67b10cU, 0x57e70f93U, 0xee96d2b4U, 0x9b919e1bU, - 0xc0c54f80U, 0xdc20a261U, 0x774b695aU, 0x121a161cU, - 0x93ba0ae2U, 0xa02ae5c0U, 0x22e0433cU, 0x1b171d12U, - 0x090d0b0eU, 0x8bc7adf2U, 0xb6a8b92dU, 0x1ea9c814U, - 0xf1198557U, 0x75074cafU, 0x99ddbbeeU, 0x7f60fda3U, - 0x01269ff7U, 0x72f5bc5cU, 0x663bc544U, 0xfb7e345bU, - 0x4329768bU, 0x23c6dccbU, 0xedfc68b6U, 0xe4f163b8U, - 0x31dccad7U, 0x63851042U, 0x97224013U, 0xc6112084U, - 0x4a247d85U, 0xbb3df8d2U, 0xf93211aeU, 0x29a16dc7U, - 0x9e2f4b1dU, 0xb230f3dcU, 0x8652ec0dU, 0xc1e3d077U, - 0xb3166c2bU, 0x70b999a9U, 0x9448fa11U, 0xe9642247U, - 0xfc8cc4a8U, 0xf03f1aa0U, 0x7d2cd856U, 0x3390ef22U, - 0x494ec787U, 0x38d1c1d9U, 0xcaa2fe8cU, 0xd40b3698U, - 0xf581cfa6U, 0x7ade28a5U, 0xb78e26daU, 0xadbfa43fU, - 0x3a9de42cU, 0x78920d50U, 0x5fcc9b6aU, 0x7e466254U, - 0x8d13c2f6U, 0xd8b8e890U, 0x39f75e2eU, 0xc3aff582U, - 0x5d80be9fU, 0xd0937c69U, 0xd52da96fU, 0x2512b3cfU, - 0xac993bc8U, 0x187da710U, 0x9c636ee8U, 0x3bbb7bdbU, - 0x267809cdU, 0x5918f46eU, 0x9ab701ecU, 0x4f9aa883U, - 0x956e65e6U, 0xffe67eaaU, 0xbccf0821U, 0x15e8e6efU, - 0xe79bd9baU, 0x6f36ce4aU, 0x9f09d4eaU, 0xb07cd629U, - 0xa4b2af31U, 0x3f23312aU, 0xa59430c6U, 0xa266c035U, - 0x4ebc3774U, 0x82caa6fcU, 0x90d0b0e0U, 0xa7d81533U, - 0x04984af1U, 0xecdaf741U, 0xcd500e7fU, 0x91f62f17U, - 0x4dd68d76U, 0xefb04d43U, 0xaa4d54ccU, 0x9604dfe4U, - 0xd1b5e39eU, 0x6a881b4cU, 0x2c1fb8c1U, 0x65517f46U, - 0x5eea049dU, 0x8c355d01U, 0x877473faU, 0x0b412efbU, - 0x671d5ab3U, 0xdbd25292U, 0x105633e9U, 0xd647136dU, - 0xd7618c9aU, 0xa10c7a37U, 0xf8148e59U, 0x133c89ebU, - 0xa927eeceU, 0x61c935b7U, 0x1ce5ede1U, 0x47b13c7aU, - 0xd2df599cU, 0xf2733f55U, 0x14ce7918U, 0xc737bf73U, - 0xf7cdea53U, 0xfdaa5b5fU, 0x3d6f14dfU, 0x44db8678U, - 0xaff381caU, 0x68c43eb9U, 0x24342c38U, 0xa3405fc2U, - 0x1dc37216U, 0xe2250cbcU, 0x3c498b28U, 0x0d9541ffU, - 0xa8017139U, 0x0cb3de08U, 0xb4e49cd8U, 0x56c19064U, - 0xcb84617bU, 0x32b670d5U, 0x6c5c7448U, 0xb85742d0U, + 0xf4a75051U, 0x4165537eU, 0x17a4c31aU, 0x275e963aU, + 0xab6bcb3bU, 0x9d45f11fU, 0xfa58abacU, 0xe303934bU, + 0x30fa5520U, 0x766df6adU, 0xcc769188U, 0x024c25f5U, + 0xe5d7fc4fU, 0x2acbd7c5U, 0x35448026U, 0x62a38fb5U, + 0xb15a49deU, 0xba1b6725U, 0xea0e9845U, 0xfec0e15dU, + 0x2f7502c3U, 0x4cf01281U, 0x4697a38dU, 0xd3f9c66bU, + 0x8f5fe703U, 0x929c9515U, 0x6d7aebbfU, 0x5259da95U, + 0xbe832dd4U, 0x7421d358U, 0xe0692949U, 0xc9c8448eU, + 0xc2896a75U, 0x8e7978f4U, 0x583e6b99U, 0xb971dd27U, + 0xe14fb6beU, 0x88ad17f0U, 0x20ac66c9U, 0xce3ab47dU, + 0xdf4a1863U, 0x1a3182e5U, 0x51336097U, 0x537f4562U, + 0x6477e0b1U, 0x6bae84bbU, 0x81a01cfeU, 0x082b94f9U, + 0x48685870U, 0x45fd198fU, 0xde6c8794U, 0x7bf8b752U, + 0x73d323abU, 0x4b02e272U, 0x1f8f57e3U, 0x55ab2a66U, + 0xeb2807b2U, 0xb5c2032fU, 0xc57b9a86U, 0x3708a5d3U, + 0x2887f230U, 0xbfa5b223U, 0x036aba02U, 0x16825cedU, + 0xcf1c2b8aU, 0x79b492a7U, 0x07f2f0f3U, 0x69e2a14eU, + 0xdaf4cd65U, 0x05bed506U, 0x34621fd1U, 0xa6fe8ac4U, + 0x2e539d34U, 0xf355a0a2U, 0x8ae13205U, 0xf6eb75a4U, + 0x83ec390bU, 0x60efaa40U, 0x719f065eU, 0x6e1051bdU, + 0x218af93eU, 0xdd063d96U, 0x3e05aeddU, 0xe6bd464dU, + 0x548db591U, 0xc45d0571U, 0x06d46f04U, 0x5015ff60U, + 0x98fb2419U, 0xbde997d6U, 0x4043cc89U, 0xd99e7767U, + 0xe842bdb0U, 0x898b8807U, 0x195b38e7U, 0xc8eedb79U, + 0x7c0a47a1U, 0x420fe97cU, 0x841ec9f8U, 0x00000000U, + 0x80868309U, 0x2bed4832U, 0x1170ac1eU, 0x5a724e6cU, + 0x0efffbfdU, 0x8538560fU, 0xaed51e3dU, 0x2d392736U, + 0x0fd9640aU, 0x5ca62168U, 0x5b54d19bU, 0x362e3a24U, + 0x0a67b10cU, 0x57e70f93U, 0xee96d2b4U, 0x9b919e1bU, + 0xc0c54f80U, 0xdc20a261U, 0x774b695aU, 0x121a161cU, + 0x93ba0ae2U, 0xa02ae5c0U, 0x22e0433cU, 0x1b171d12U, + 0x090d0b0eU, 0x8bc7adf2U, 0xb6a8b92dU, 0x1ea9c814U, + 0xf1198557U, 0x75074cafU, 0x99ddbbeeU, 0x7f60fda3U, + 0x01269ff7U, 0x72f5bc5cU, 0x663bc544U, 0xfb7e345bU, + 0x4329768bU, 0x23c6dccbU, 0xedfc68b6U, 0xe4f163b8U, + 0x31dccad7U, 0x63851042U, 0x97224013U, 0xc6112084U, + 0x4a247d85U, 0xbb3df8d2U, 0xf93211aeU, 0x29a16dc7U, + 0x9e2f4b1dU, 0xb230f3dcU, 0x8652ec0dU, 0xc1e3d077U, + 0xb3166c2bU, 0x70b999a9U, 0x9448fa11U, 0xe9642247U, + 0xfc8cc4a8U, 0xf03f1aa0U, 0x7d2cd856U, 0x3390ef22U, + 0x494ec787U, 0x38d1c1d9U, 0xcaa2fe8cU, 0xd40b3698U, + 0xf581cfa6U, 0x7ade28a5U, 0xb78e26daU, 0xadbfa43fU, + 0x3a9de42cU, 0x78920d50U, 0x5fcc9b6aU, 0x7e466254U, + 0x8d13c2f6U, 0xd8b8e890U, 0x39f75e2eU, 0xc3aff582U, + 0x5d80be9fU, 0xd0937c69U, 0xd52da96fU, 0x2512b3cfU, + 0xac993bc8U, 0x187da710U, 0x9c636ee8U, 0x3bbb7bdbU, + 0x267809cdU, 0x5918f46eU, 0x9ab701ecU, 0x4f9aa883U, + 0x956e65e6U, 0xffe67eaaU, 0xbccf0821U, 0x15e8e6efU, + 0xe79bd9baU, 0x6f36ce4aU, 0x9f09d4eaU, 0xb07cd629U, + 0xa4b2af31U, 0x3f23312aU, 0xa59430c6U, 0xa266c035U, + 0x4ebc3774U, 0x82caa6fcU, 0x90d0b0e0U, 0xa7d81533U, + 0x04984af1U, 0xecdaf741U, 0xcd500e7fU, 0x91f62f17U, + 0x4dd68d76U, 0xefb04d43U, 0xaa4d54ccU, 0x9604dfe4U, + 0xd1b5e39eU, 0x6a881b4cU, 0x2c1fb8c1U, 0x65517f46U, + 0x5eea049dU, 0x8c355d01U, 0x877473faU, 0x0b412efbU, + 0x671d5ab3U, 0xdbd25292U, 0x105633e9U, 0xd647136dU, + 0xd7618c9aU, 0xa10c7a37U, 0xf8148e59U, 0x133c89ebU, + 0xa927eeceU, 0x61c935b7U, 0x1ce5ede1U, 0x47b13c7aU, + 0xd2df599cU, 0xf2733f55U, 0x14ce7918U, 0xc737bf73U, + 0xf7cdea53U, 0xfdaa5b5fU, 0x3d6f14dfU, 0x44db8678U, + 0xaff381caU, 0x68c43eb9U, 0x24342c38U, 0xa3405fc2U, + 0x1dc37216U, 0xe2250cbcU, 0x3c498b28U, 0x0d9541ffU, + 0xa8017139U, 0x0cb3de08U, 0xb4e49cd8U, 0x56c19064U, + 0xcb84617bU, 0x32b670d5U, 0x6c5c7448U, 0xb85742d0U, }; static const cc_u32 SDRM_Td4[256] = { - 0x52525252U, 0x09090909U, 0x6a6a6a6aU, 0xd5d5d5d5U, - 0x30303030U, 0x36363636U, 0xa5a5a5a5U, 0x38383838U, - 0xbfbfbfbfU, 0x40404040U, 0xa3a3a3a3U, 0x9e9e9e9eU, - 0x81818181U, 0xf3f3f3f3U, 0xd7d7d7d7U, 0xfbfbfbfbU, - 0x7c7c7c7cU, 0xe3e3e3e3U, 0x39393939U, 0x82828282U, - 0x9b9b9b9bU, 0x2f2f2f2fU, 0xffffffffU, 0x87878787U, - 0x34343434U, 0x8e8e8e8eU, 0x43434343U, 0x44444444U, - 0xc4c4c4c4U, 0xdedededeU, 0xe9e9e9e9U, 0xcbcbcbcbU, - 0x54545454U, 0x7b7b7b7bU, 0x94949494U, 0x32323232U, - 0xa6a6a6a6U, 0xc2c2c2c2U, 0x23232323U, 0x3d3d3d3dU, - 0xeeeeeeeeU, 0x4c4c4c4cU, 0x95959595U, 0x0b0b0b0bU, - 0x42424242U, 0xfafafafaU, 0xc3c3c3c3U, 0x4e4e4e4eU, - 0x08080808U, 0x2e2e2e2eU, 0xa1a1a1a1U, 0x66666666U, - 0x28282828U, 0xd9d9d9d9U, 0x24242424U, 0xb2b2b2b2U, - 0x76767676U, 0x5b5b5b5bU, 0xa2a2a2a2U, 0x49494949U, - 0x6d6d6d6dU, 0x8b8b8b8bU, 0xd1d1d1d1U, 0x25252525U, - 0x72727272U, 0xf8f8f8f8U, 0xf6f6f6f6U, 0x64646464U, - 0x86868686U, 0x68686868U, 0x98989898U, 0x16161616U, - 0xd4d4d4d4U, 0xa4a4a4a4U, 0x5c5c5c5cU, 0xccccccccU, - 0x5d5d5d5dU, 0x65656565U, 0xb6b6b6b6U, 0x92929292U, - 0x6c6c6c6cU, 0x70707070U, 0x48484848U, 0x50505050U, - 0xfdfdfdfdU, 0xededededU, 0xb9b9b9b9U, 0xdadadadaU, - 0x5e5e5e5eU, 0x15151515U, 0x46464646U, 0x57575757U, - 0xa7a7a7a7U, 0x8d8d8d8dU, 0x9d9d9d9dU, 0x84848484U, - 0x90909090U, 0xd8d8d8d8U, 0xababababU, 0x00000000U, - 0x8c8c8c8cU, 0xbcbcbcbcU, 0xd3d3d3d3U, 0x0a0a0a0aU, - 0xf7f7f7f7U, 0xe4e4e4e4U, 0x58585858U, 0x05050505U, - 0xb8b8b8b8U, 0xb3b3b3b3U, 0x45454545U, 0x06060606U, - 0xd0d0d0d0U, 0x2c2c2c2cU, 0x1e1e1e1eU, 0x8f8f8f8fU, - 0xcacacacaU, 0x3f3f3f3fU, 0x0f0f0f0fU, 0x02020202U, - 0xc1c1c1c1U, 0xafafafafU, 0xbdbdbdbdU, 0x03030303U, - 0x01010101U, 0x13131313U, 0x8a8a8a8aU, 0x6b6b6b6bU, - 0x3a3a3a3aU, 0x91919191U, 0x11111111U, 0x41414141U, - 0x4f4f4f4fU, 0x67676767U, 0xdcdcdcdcU, 0xeaeaeaeaU, - 0x97979797U, 0xf2f2f2f2U, 0xcfcfcfcfU, 0xcecececeU, - 0xf0f0f0f0U, 0xb4b4b4b4U, 0xe6e6e6e6U, 0x73737373U, - 0x96969696U, 0xacacacacU, 0x74747474U, 0x22222222U, - 0xe7e7e7e7U, 0xadadadadU, 0x35353535U, 0x85858585U, - 0xe2e2e2e2U, 0xf9f9f9f9U, 0x37373737U, 0xe8e8e8e8U, - 0x1c1c1c1cU, 0x75757575U, 0xdfdfdfdfU, 0x6e6e6e6eU, - 0x47474747U, 0xf1f1f1f1U, 0x1a1a1a1aU, 0x71717171U, - 0x1d1d1d1dU, 0x29292929U, 0xc5c5c5c5U, 0x89898989U, - 0x6f6f6f6fU, 0xb7b7b7b7U, 0x62626262U, 0x0e0e0e0eU, - 0xaaaaaaaaU, 0x18181818U, 0xbebebebeU, 0x1b1b1b1bU, - 0xfcfcfcfcU, 0x56565656U, 0x3e3e3e3eU, 0x4b4b4b4bU, - 0xc6c6c6c6U, 0xd2d2d2d2U, 0x79797979U, 0x20202020U, - 0x9a9a9a9aU, 0xdbdbdbdbU, 0xc0c0c0c0U, 0xfefefefeU, - 0x78787878U, 0xcdcdcdcdU, 0x5a5a5a5aU, 0xf4f4f4f4U, - 0x1f1f1f1fU, 0xddddddddU, 0xa8a8a8a8U, 0x33333333U, - 0x88888888U, 0x07070707U, 0xc7c7c7c7U, 0x31313131U, - 0xb1b1b1b1U, 0x12121212U, 0x10101010U, 0x59595959U, - 0x27272727U, 0x80808080U, 0xececececU, 0x5f5f5f5fU, - 0x60606060U, 0x51515151U, 0x7f7f7f7fU, 0xa9a9a9a9U, - 0x19191919U, 0xb5b5b5b5U, 0x4a4a4a4aU, 0x0d0d0d0dU, - 0x2d2d2d2dU, 0xe5e5e5e5U, 0x7a7a7a7aU, 0x9f9f9f9fU, - 0x93939393U, 0xc9c9c9c9U, 0x9c9c9c9cU, 0xefefefefU, - 0xa0a0a0a0U, 0xe0e0e0e0U, 0x3b3b3b3bU, 0x4d4d4d4dU, - 0xaeaeaeaeU, 0x2a2a2a2aU, 0xf5f5f5f5U, 0xb0b0b0b0U, - 0xc8c8c8c8U, 0xebebebebU, 0xbbbbbbbbU, 0x3c3c3c3cU, - 0x83838383U, 0x53535353U, 0x99999999U, 0x61616161U, - 0x17171717U, 0x2b2b2b2bU, 0x04040404U, 0x7e7e7e7eU, - 0xbabababaU, 0x77777777U, 0xd6d6d6d6U, 0x26262626U, - 0xe1e1e1e1U, 0x69696969U, 0x14141414U, 0x63636363U, - 0x55555555U, 0x21212121U, 0x0c0c0c0cU, 0x7d7d7d7dU, + 0x52525252U, 0x09090909U, 0x6a6a6a6aU, 0xd5d5d5d5U, + 0x30303030U, 0x36363636U, 0xa5a5a5a5U, 0x38383838U, + 0xbfbfbfbfU, 0x40404040U, 0xa3a3a3a3U, 0x9e9e9e9eU, + 0x81818181U, 0xf3f3f3f3U, 0xd7d7d7d7U, 0xfbfbfbfbU, + 0x7c7c7c7cU, 0xe3e3e3e3U, 0x39393939U, 0x82828282U, + 0x9b9b9b9bU, 0x2f2f2f2fU, 0xffffffffU, 0x87878787U, + 0x34343434U, 0x8e8e8e8eU, 0x43434343U, 0x44444444U, + 0xc4c4c4c4U, 0xdedededeU, 0xe9e9e9e9U, 0xcbcbcbcbU, + 0x54545454U, 0x7b7b7b7bU, 0x94949494U, 0x32323232U, + 0xa6a6a6a6U, 0xc2c2c2c2U, 0x23232323U, 0x3d3d3d3dU, + 0xeeeeeeeeU, 0x4c4c4c4cU, 0x95959595U, 0x0b0b0b0bU, + 0x42424242U, 0xfafafafaU, 0xc3c3c3c3U, 0x4e4e4e4eU, + 0x08080808U, 0x2e2e2e2eU, 0xa1a1a1a1U, 0x66666666U, + 0x28282828U, 0xd9d9d9d9U, 0x24242424U, 0xb2b2b2b2U, + 0x76767676U, 0x5b5b5b5bU, 0xa2a2a2a2U, 0x49494949U, + 0x6d6d6d6dU, 0x8b8b8b8bU, 0xd1d1d1d1U, 0x25252525U, + 0x72727272U, 0xf8f8f8f8U, 0xf6f6f6f6U, 0x64646464U, + 0x86868686U, 0x68686868U, 0x98989898U, 0x16161616U, + 0xd4d4d4d4U, 0xa4a4a4a4U, 0x5c5c5c5cU, 0xccccccccU, + 0x5d5d5d5dU, 0x65656565U, 0xb6b6b6b6U, 0x92929292U, + 0x6c6c6c6cU, 0x70707070U, 0x48484848U, 0x50505050U, + 0xfdfdfdfdU, 0xededededU, 0xb9b9b9b9U, 0xdadadadaU, + 0x5e5e5e5eU, 0x15151515U, 0x46464646U, 0x57575757U, + 0xa7a7a7a7U, 0x8d8d8d8dU, 0x9d9d9d9dU, 0x84848484U, + 0x90909090U, 0xd8d8d8d8U, 0xababababU, 0x00000000U, + 0x8c8c8c8cU, 0xbcbcbcbcU, 0xd3d3d3d3U, 0x0a0a0a0aU, + 0xf7f7f7f7U, 0xe4e4e4e4U, 0x58585858U, 0x05050505U, + 0xb8b8b8b8U, 0xb3b3b3b3U, 0x45454545U, 0x06060606U, + 0xd0d0d0d0U, 0x2c2c2c2cU, 0x1e1e1e1eU, 0x8f8f8f8fU, + 0xcacacacaU, 0x3f3f3f3fU, 0x0f0f0f0fU, 0x02020202U, + 0xc1c1c1c1U, 0xafafafafU, 0xbdbdbdbdU, 0x03030303U, + 0x01010101U, 0x13131313U, 0x8a8a8a8aU, 0x6b6b6b6bU, + 0x3a3a3a3aU, 0x91919191U, 0x11111111U, 0x41414141U, + 0x4f4f4f4fU, 0x67676767U, 0xdcdcdcdcU, 0xeaeaeaeaU, + 0x97979797U, 0xf2f2f2f2U, 0xcfcfcfcfU, 0xcecececeU, + 0xf0f0f0f0U, 0xb4b4b4b4U, 0xe6e6e6e6U, 0x73737373U, + 0x96969696U, 0xacacacacU, 0x74747474U, 0x22222222U, + 0xe7e7e7e7U, 0xadadadadU, 0x35353535U, 0x85858585U, + 0xe2e2e2e2U, 0xf9f9f9f9U, 0x37373737U, 0xe8e8e8e8U, + 0x1c1c1c1cU, 0x75757575U, 0xdfdfdfdfU, 0x6e6e6e6eU, + 0x47474747U, 0xf1f1f1f1U, 0x1a1a1a1aU, 0x71717171U, + 0x1d1d1d1dU, 0x29292929U, 0xc5c5c5c5U, 0x89898989U, + 0x6f6f6f6fU, 0xb7b7b7b7U, 0x62626262U, 0x0e0e0e0eU, + 0xaaaaaaaaU, 0x18181818U, 0xbebebebeU, 0x1b1b1b1bU, + 0xfcfcfcfcU, 0x56565656U, 0x3e3e3e3eU, 0x4b4b4b4bU, + 0xc6c6c6c6U, 0xd2d2d2d2U, 0x79797979U, 0x20202020U, + 0x9a9a9a9aU, 0xdbdbdbdbU, 0xc0c0c0c0U, 0xfefefefeU, + 0x78787878U, 0xcdcdcdcdU, 0x5a5a5a5aU, 0xf4f4f4f4U, + 0x1f1f1f1fU, 0xddddddddU, 0xa8a8a8a8U, 0x33333333U, + 0x88888888U, 0x07070707U, 0xc7c7c7c7U, 0x31313131U, + 0xb1b1b1b1U, 0x12121212U, 0x10101010U, 0x59595959U, + 0x27272727U, 0x80808080U, 0xececececU, 0x5f5f5f5fU, + 0x60606060U, 0x51515151U, 0x7f7f7f7fU, 0xa9a9a9a9U, + 0x19191919U, 0xb5b5b5b5U, 0x4a4a4a4aU, 0x0d0d0d0dU, + 0x2d2d2d2dU, 0xe5e5e5e5U, 0x7a7a7a7aU, 0x9f9f9f9fU, + 0x93939393U, 0xc9c9c9c9U, 0x9c9c9c9cU, 0xefefefefU, + 0xa0a0a0a0U, 0xe0e0e0e0U, 0x3b3b3b3bU, 0x4d4d4d4dU, + 0xaeaeaeaeU, 0x2a2a2a2aU, 0xf5f5f5f5U, 0xb0b0b0b0U, + 0xc8c8c8c8U, 0xebebebebU, 0xbbbbbbbbU, 0x3c3c3c3cU, + 0x83838383U, 0x53535353U, 0x99999999U, 0x61616161U, + 0x17171717U, 0x2b2b2b2bU, 0x04040404U, 0x7e7e7e7eU, + 0xbabababaU, 0x77777777U, 0xd6d6d6d6U, 0x26262626U, + 0xe1e1e1e1U, 0x69696969U, 0x14141414U, 0x63636363U, + 0x55555555U, 0x21212121U, 0x0c0c0c0cU, 0x7d7d7d7dU, }; static const cc_u32 SDRM_rcon[] = { 0x01000000, 0x02000000, 0x04000000, 0x08000000, 0x10000000, 0x20000000, 0x40000000, 0x80000000, - 0x1B000000, 0x36000000, + 0x1B000000, 0x36000000, /* for 128-bit blocks, Rijndael never uses more than 10 _rcon values */ }; @@ -707,572 +707,693 @@ static const cc_u32 SDRM_rcon[] = { #define SWAP(x) (_lrotl(x, 8) & 0x00ff00ff | _lrotr(x, 8) & 0xff00ff00) #ifdef _MSC_VER - #define GETUINT32(p) SWAP(*((cc_u32 *)(p))) - #define PUTUINT32(ct, st) { *((cc_u32 *)(ct)) = SWAP((st)); } +#define GETUINT32(p) SWAP(*((cc_u32 *)(p))) +#define PUTUINT32(ct, st) { *((cc_u32 *)(ct)) = SWAP((st)); } #else - #define GETUINT32(pt) (((cc_u32)(pt)[0] << 24) ^ ((cc_u32)(pt)[1] << 16) ^ ((cc_u32)(pt)[2] << 8) ^ ((cc_u32)(pt)[3])) - #define PUTUINT32(ct, st) { (ct)[0] = (cc_u8)((st) >> 24); (ct)[1] = (cc_u8)((st) >> 16); (ct)[2] = (cc_u8)((st) >> 8); (ct)[3] = (cc_u8)(st); } +#define GETUINT32(pt) (((cc_u32)(pt)[0] << 24) ^ ((cc_u32)(pt)[1] << 16) ^ ((cc_u32)(pt)[2] << 8) ^ ((cc_u32)(pt)[3])) +#define PUTUINT32(ct, st) { (ct)[0] = (cc_u8)((st) >> 24); (ct)[1] = (cc_u8)((st) >> 16); (ct)[2] = (cc_u8)((st) >> 8); (ct)[3] = (cc_u8)(st); } #endif /* - * @fn SDRM_rijndaelKeySetupEnc - * @brief Expand the cipher key into the encryption key schedule + * @fn SDRM_rijndaelKeySetupEnc + * @brief Expand the cipher key into the encryption key schedule * - * @param rk [out]expanded round key - * @param cipherKey [in]user key - * @param keyBits [in]bit-length of cipherKey + * @param rk [out]expanded round key + * @param cipherKey [in]user key + * @param keyBits [in]bit-length of cipherKey * - * @return the number of rounds for the given cipher key size + * @return the number of rounds for the given cipher key size */ -int SDRM_rijndaelKeySetupEnc(cc_u32 rk[/*4*(Nr + 1)*/], const cc_u8 cipherKey[], int keyBits) +int SDRM_rijndaelKeySetupEnc(cc_u32 rk[/*4*(Nr + 1)*/], const cc_u8 cipherKey[], + int keyBits) { - int i = 0; - cc_u32 temp; + int i = 0; + cc_u32 temp; - rk[0] = GETUINT32(cipherKey ); + rk[0] = GETUINT32(cipherKey); rk[1] = GETUINT32(cipherKey + 4); rk[2] = GETUINT32(cipherKey + 8); rk[3] = GETUINT32(cipherKey + 12); - if (keyBits == 128) - { - for (;;) - { + + if (keyBits == 128) { + for (;;) { temp = rk[3]; rk[4] = rk[0] ^ - (SDRM_Te4[(temp >> 16) & 0xff] & 0xff000000) ^ - (SDRM_Te4[(temp >> 8) & 0xff] & 0x00ff0000) ^ - (SDRM_Te4[(temp ) & 0xff] & 0x0000ff00) ^ - (SDRM_Te4[(temp >> 24) ] & 0x000000ff) ^ - SDRM_rcon[i]; + (SDRM_Te4[(temp >> 16) & 0xff] & 0xff000000) ^ + (SDRM_Te4[(temp >> 8) & 0xff] & 0x00ff0000) ^ + (SDRM_Te4[(temp) & 0xff] & 0x0000ff00) ^ + (SDRM_Te4[(temp >> 24)] & 0x000000ff) ^ + SDRM_rcon[i]; rk[5] = rk[1] ^ rk[4]; rk[6] = rk[2] ^ rk[5]; rk[7] = rk[3] ^ rk[6]; + if (++i == 10) - { return 10; - } + rk += 4; } } rk[4] = GETUINT32(cipherKey + 16); rk[5] = GETUINT32(cipherKey + 20); - if (keyBits == 192) - { + + if (keyBits == 192) { for (;;) { - temp = rk[ 5]; - rk[ 6] = rk[ 0] ^ - (SDRM_Te4[(temp >> 16) & 0xff] & 0xff000000) ^ - (SDRM_Te4[(temp >> 8) & 0xff] & 0x00ff0000) ^ - (SDRM_Te4[(temp ) & 0xff] & 0x0000ff00) ^ - (SDRM_Te4[(temp >> 24) ] & 0x000000ff) ^ - SDRM_rcon[i]; - rk[ 7] = rk[ 1] ^ rk[ 6]; - rk[ 8] = rk[ 2] ^ rk[ 7]; - rk[ 9] = rk[ 3] ^ rk[ 8]; + temp = rk[5]; + rk[6] = rk[0] ^ + (SDRM_Te4[(temp >> 16) & 0xff] & 0xff000000) ^ + (SDRM_Te4[(temp >> 8) & 0xff] & 0x00ff0000) ^ + (SDRM_Te4[(temp) & 0xff] & 0x0000ff00) ^ + (SDRM_Te4[(temp >> 24)] & 0x000000ff) ^ + SDRM_rcon[i]; + rk[7] = rk[1] ^ rk[6]; + rk[8] = rk[2] ^ rk[7]; + rk[9] = rk[3] ^ rk[8]; + if (++i == 8) - { return 12; - } - rk[10] = rk[ 4] ^ rk[ 9]; - rk[11] = rk[ 5] ^ rk[10]; + + rk[10] = rk[4] ^ rk[9]; + rk[11] = rk[5] ^ rk[10]; rk += 6; } } + rk[6] = GETUINT32(cipherKey + 24); rk[7] = GETUINT32(cipherKey + 28); - if (keyBits == 256) - { - for (;;) - { - temp = rk[ 7]; - rk[ 8] = rk[ 0] ^ - (SDRM_Te4[(temp >> 16) & 0xff] & 0xff000000) ^ - (SDRM_Te4[(temp >> 8) & 0xff] & 0x00ff0000) ^ - (SDRM_Te4[(temp ) & 0xff] & 0x0000ff00) ^ - (SDRM_Te4[(temp >> 24) ] & 0x000000ff) ^ - SDRM_rcon[i]; - rk[ 9] = rk[ 1] ^ rk[ 8]; - rk[10] = rk[ 2] ^ rk[ 9]; - rk[11] = rk[ 3] ^ rk[10]; - if (++i == 7) { + + if (keyBits == 256) { + for (;;) { + temp = rk[7]; + rk[8] = rk[0] ^ + (SDRM_Te4[(temp >> 16) & 0xff] & 0xff000000) ^ + (SDRM_Te4[(temp >> 8) & 0xff] & 0x00ff0000) ^ + (SDRM_Te4[(temp) & 0xff] & 0x0000ff00) ^ + (SDRM_Te4[(temp >> 24)] & 0x000000ff) ^ + SDRM_rcon[i]; + rk[9] = rk[1] ^ rk[8]; + rk[10] = rk[2] ^ rk[9]; + rk[11] = rk[3] ^ rk[10]; + + if (++i == 7) return 14; - } - temp = rk[11]; - rk[12] = rk[ 4] ^ - (SDRM_Te4[(temp >> 24) ] & 0xff000000) ^ - (SDRM_Te4[(temp >> 16) & 0xff] & 0x00ff0000) ^ - (SDRM_Te4[(temp >> 8) & 0xff] & 0x0000ff00) ^ - (SDRM_Te4[(temp ) & 0xff] & 0x000000ff); - rk[13] = rk[ 5] ^ rk[12]; - rk[14] = rk[ 6] ^ rk[13]; - rk[15] = rk[ 7] ^ rk[14]; + + temp = rk[11]; + rk[12] = rk[4] ^ + (SDRM_Te4[(temp >> 24)] & 0xff000000) ^ + (SDRM_Te4[(temp >> 16) & 0xff] & 0x00ff0000) ^ + (SDRM_Te4[(temp >> 8) & 0xff] & 0x0000ff00) ^ + (SDRM_Te4[(temp) & 0xff] & 0x000000ff); + rk[13] = rk[5] ^ rk[12]; + rk[14] = rk[6] ^ rk[13]; + rk[15] = rk[7] ^ rk[14]; rk += 8; - } + } } + return CRYPTO_SUCCESS; } /* - * @fn SDRM_rijndaelKeySetupDec - * @brief Expand the cipher key into the decryption key schedule + * @fn SDRM_rijndaelKeySetupDec + * @brief Expand the cipher key into the decryption key schedule * - * @param rk [out]expanded round key - * @param cipherKey [in]user key - * @param keyBits [in]bit-length of cipherKey + * @param rk [out]expanded round key + * @param cipherKey [in]user key + * @param keyBits [in]bit-length of cipherKey * - * @return the number of rounds for the given cipher key size + * @return the number of rounds for the given cipher key size */ -int SDRM_rijndaelKeySetupDec(cc_u32 rk[/*4*(Nr + 1)*/], const cc_u8 cipherKey[], int keyBits) +int SDRM_rijndaelKeySetupDec(cc_u32 rk[/*4*(Nr + 1)*/], const cc_u8 cipherKey[], + int keyBits) { - int Nr, i, j; - cc_u32 temp; + int Nr, i, j; + cc_u32 temp; /* expand the cipher key: */ Nr = SDRM_rijndaelKeySetupEnc(rk, cipherKey, keyBits); + /* invert the order of the round keys: */ - for (i = 0, j = 4*Nr; i < j; i += 4, j -= 4) - { - temp = rk[i ]; rk[i ] = rk[j ]; rk[j ] = temp; - temp = rk[i + 1]; rk[i + 1] = rk[j + 1]; rk[j + 1] = temp; - temp = rk[i + 2]; rk[i + 2] = rk[j + 2]; rk[j + 2] = temp; - temp = rk[i + 3]; rk[i + 3] = rk[j + 3]; rk[j + 3] = temp; + for (i = 0, j = 4 * Nr; i < j; i += 4, j -= 4) { + temp = rk[i]; + rk[i] = rk[j]; + rk[j] = temp; + temp = rk[i + 1]; + rk[i + 1] = rk[j + 1]; + rk[j + 1] = temp; + temp = rk[i + 2]; + rk[i + 2] = rk[j + 2]; + rk[j + 2] = temp; + temp = rk[i + 3]; + rk[i + 3] = rk[j + 3]; + rk[j + 3] = temp; } /* apply the inverse MixColumn transform to all round keys but the first and the last: */ - for (i = 1; i < Nr; i++) - { + for (i = 1; i < Nr; i++) { rk += 4; rk[0] = - SDRM_Td0[SDRM_Te4[(rk[0] >> 24) ] & 0xff] ^ + SDRM_Td0[SDRM_Te4[(rk[0] >> 24)] & 0xff] ^ SDRM_Td1[SDRM_Te4[(rk[0] >> 16) & 0xff] & 0xff] ^ SDRM_Td2[SDRM_Te4[(rk[0] >> 8) & 0xff] & 0xff] ^ - SDRM_Td3[SDRM_Te4[(rk[0] ) & 0xff] & 0xff]; + SDRM_Td3[SDRM_Te4[(rk[0]) & 0xff] & 0xff]; rk[1] = - SDRM_Td0[SDRM_Te4[(rk[1] >> 24) ] & 0xff] ^ + SDRM_Td0[SDRM_Te4[(rk[1] >> 24)] & 0xff] ^ SDRM_Td1[SDRM_Te4[(rk[1] >> 16) & 0xff] & 0xff] ^ SDRM_Td2[SDRM_Te4[(rk[1] >> 8) & 0xff] & 0xff] ^ - SDRM_Td3[SDRM_Te4[(rk[1] ) & 0xff] & 0xff]; + SDRM_Td3[SDRM_Te4[(rk[1]) & 0xff] & 0xff]; rk[2] = - SDRM_Td0[SDRM_Te4[(rk[2] >> 24) ] & 0xff] ^ + SDRM_Td0[SDRM_Te4[(rk[2] >> 24)] & 0xff] ^ SDRM_Td1[SDRM_Te4[(rk[2] >> 16) & 0xff] & 0xff] ^ SDRM_Td2[SDRM_Te4[(rk[2] >> 8) & 0xff] & 0xff] ^ - SDRM_Td3[SDRM_Te4[(rk[2] ) & 0xff] & 0xff]; + SDRM_Td3[SDRM_Te4[(rk[2]) & 0xff] & 0xff]; rk[3] = - SDRM_Td0[SDRM_Te4[(rk[3] >> 24) ] & 0xff] ^ + SDRM_Td0[SDRM_Te4[(rk[3] >> 24)] & 0xff] ^ SDRM_Td1[SDRM_Te4[(rk[3] >> 16) & 0xff] & 0xff] ^ SDRM_Td2[SDRM_Te4[(rk[3] >> 8) & 0xff] & 0xff] ^ - SDRM_Td3[SDRM_Te4[(rk[3] ) & 0xff] & 0xff]; + SDRM_Td3[SDRM_Te4[(rk[3]) & 0xff] & 0xff]; } + return Nr; } /* - * @fn SDRM_rijndaelEncrypt - * @brief 16 byte AES Encryption with round key + * @fn SDRM_rijndaelEncrypt + * @brief 16 byte AES Encryption with round key * - * @param rk [in]expanded round key - * @param Nr [in]numer of rounds - * @param pt [in]plain text - * @param ct [out]cipher text + * @param rk [in]expanded round key + * @param Nr [in]numer of rounds + * @param pt [in]plain text + * @param ct [out]cipher text * - * @return void + * @return void */ -void SDRM_rijndaelEncrypt(const cc_u32 rk[/*4*(Nr + 1)*/], int Nr, const cc_u8 pt[16], cc_u8 ct[16]) +void SDRM_rijndaelEncrypt(const cc_u32 rk[/*4*(Nr + 1)*/], int Nr, + const cc_u8 pt[16], cc_u8 ct[16]) { cc_u32 s0, s1, s2, s3, t0, t1, t2, t3; #ifndef FULL_UNROLL - int r; + int r; #endif /* ?FULL_UNROLL */ - /* + /* * map byte array block to cipher state * and add initial round key: */ - s0 = GETUINT32(pt ) ^ rk[0]; + s0 = GETUINT32(pt) ^ rk[0]; s1 = GETUINT32(pt + 4) ^ rk[1]; s2 = GETUINT32(pt + 8) ^ rk[2]; s3 = GETUINT32(pt + 12) ^ rk[3]; #ifdef FULL_UNROLL - /* round 1: */ - t0 = SDRM_Te0[s0 >> 24] ^ SDRM_Te1[(s1 >> 16) & 0xff] ^ SDRM_Te2[(s2 >> 8) & 0xff] ^ SDRM_Te3[s3 & 0xff] ^ rk[ 4]; - t1 = SDRM_Te0[s1 >> 24] ^ SDRM_Te1[(s2 >> 16) & 0xff] ^ SDRM_Te2[(s3 >> 8) & 0xff] ^ SDRM_Te3[s0 & 0xff] ^ rk[ 5]; - t2 = SDRM_Te0[s2 >> 24] ^ SDRM_Te1[(s3 >> 16) & 0xff] ^ SDRM_Te2[(s0 >> 8) & 0xff] ^ SDRM_Te3[s1 & 0xff] ^ rk[ 6]; - t3 = SDRM_Te0[s3 >> 24] ^ SDRM_Te1[(s0 >> 16) & 0xff] ^ SDRM_Te2[(s1 >> 8) & 0xff] ^ SDRM_Te3[s2 & 0xff] ^ rk[ 7]; - /* round 2: */ - s0 = SDRM_Te0[t0 >> 24] ^ SDRM_Te1[(t1 >> 16) & 0xff] ^ SDRM_Te2[(t2 >> 8) & 0xff] ^ SDRM_Te3[t3 & 0xff] ^ rk[ 8]; - s1 = SDRM_Te0[t1 >> 24] ^ SDRM_Te1[(t2 >> 16) & 0xff] ^ SDRM_Te2[(t3 >> 8) & 0xff] ^ SDRM_Te3[t0 & 0xff] ^ rk[ 9]; - s2 = SDRM_Te0[t2 >> 24] ^ SDRM_Te1[(t3 >> 16) & 0xff] ^ SDRM_Te2[(t0 >> 8) & 0xff] ^ SDRM_Te3[t1 & 0xff] ^ rk[10]; - s3 = SDRM_Te0[t3 >> 24] ^ SDRM_Te1[(t0 >> 16) & 0xff] ^ SDRM_Te2[(t1 >> 8) & 0xff] ^ SDRM_Te3[t2 & 0xff] ^ rk[11]; - /* round 3: */ - t0 = SDRM_Te0[s0 >> 24] ^ SDRM_Te1[(s1 >> 16) & 0xff] ^ SDRM_Te2[(s2 >> 8) & 0xff] ^ SDRM_Te3[s3 & 0xff] ^ rk[12]; - t1 = SDRM_Te0[s1 >> 24] ^ SDRM_Te1[(s2 >> 16) & 0xff] ^ SDRM_Te2[(s3 >> 8) & 0xff] ^ SDRM_Te3[s0 & 0xff] ^ rk[13]; - t2 = SDRM_Te0[s2 >> 24] ^ SDRM_Te1[(s3 >> 16) & 0xff] ^ SDRM_Te2[(s0 >> 8) & 0xff] ^ SDRM_Te3[s1 & 0xff] ^ rk[14]; - t3 = SDRM_Te0[s3 >> 24] ^ SDRM_Te1[(s0 >> 16) & 0xff] ^ SDRM_Te2[(s1 >> 8) & 0xff] ^ SDRM_Te3[s2 & 0xff] ^ rk[15]; - /* round 4: */ - s0 = SDRM_Te0[t0 >> 24] ^ SDRM_Te1[(t1 >> 16) & 0xff] ^ SDRM_Te2[(t2 >> 8) & 0xff] ^ SDRM_Te3[t3 & 0xff] ^ rk[16]; - s1 = SDRM_Te0[t1 >> 24] ^ SDRM_Te1[(t2 >> 16) & 0xff] ^ SDRM_Te2[(t3 >> 8) & 0xff] ^ SDRM_Te3[t0 & 0xff] ^ rk[17]; - s2 = SDRM_Te0[t2 >> 24] ^ SDRM_Te1[(t3 >> 16) & 0xff] ^ SDRM_Te2[(t0 >> 8) & 0xff] ^ SDRM_Te3[t1 & 0xff] ^ rk[18]; - s3 = SDRM_Te0[t3 >> 24] ^ SDRM_Te1[(t0 >> 16) & 0xff] ^ SDRM_Te2[(t1 >> 8) & 0xff] ^ SDRM_Te3[t2 & 0xff] ^ rk[19]; - /* round 5: */ - t0 = SDRM_Te0[s0 >> 24] ^ SDRM_Te1[(s1 >> 16) & 0xff] ^ SDRM_Te2[(s2 >> 8) & 0xff] ^ SDRM_Te3[s3 & 0xff] ^ rk[20]; - t1 = SDRM_Te0[s1 >> 24] ^ SDRM_Te1[(s2 >> 16) & 0xff] ^ SDRM_Te2[(s3 >> 8) & 0xff] ^ SDRM_Te3[s0 & 0xff] ^ rk[21]; - t2 = SDRM_Te0[s2 >> 24] ^ SDRM_Te1[(s3 >> 16) & 0xff] ^ SDRM_Te2[(s0 >> 8) & 0xff] ^ SDRM_Te3[s1 & 0xff] ^ rk[22]; - t3 = SDRM_Te0[s3 >> 24] ^ SDRM_Te1[(s0 >> 16) & 0xff] ^ SDRM_Te2[(s1 >> 8) & 0xff] ^ SDRM_Te3[s2 & 0xff] ^ rk[23]; - /* round 6: */ - s0 = SDRM_Te0[t0 >> 24] ^ SDRM_Te1[(t1 >> 16) & 0xff] ^ SDRM_Te2[(t2 >> 8) & 0xff] ^ SDRM_Te3[t3 & 0xff] ^ rk[24]; - s1 = SDRM_Te0[t1 >> 24] ^ SDRM_Te1[(t2 >> 16) & 0xff] ^ SDRM_Te2[(t3 >> 8) & 0xff] ^ SDRM_Te3[t0 & 0xff] ^ rk[25]; - s2 = SDRM_Te0[t2 >> 24] ^ SDRM_Te1[(t3 >> 16) & 0xff] ^ SDRM_Te2[(t0 >> 8) & 0xff] ^ SDRM_Te3[t1 & 0xff] ^ rk[26]; - s3 = SDRM_Te0[t3 >> 24] ^ SDRM_Te1[(t0 >> 16) & 0xff] ^ SDRM_Te2[(t1 >> 8) & 0xff] ^ SDRM_Te3[t2 & 0xff] ^ rk[27]; - /* round 7: */ - t0 = SDRM_Te0[s0 >> 24] ^ SDRM_Te1[(s1 >> 16) & 0xff] ^ SDRM_Te2[(s2 >> 8) & 0xff] ^ SDRM_Te3[s3 & 0xff] ^ rk[28]; - t1 = SDRM_Te0[s1 >> 24] ^ SDRM_Te1[(s2 >> 16) & 0xff] ^ SDRM_Te2[(s3 >> 8) & 0xff] ^ SDRM_Te3[s0 & 0xff] ^ rk[29]; - t2 = SDRM_Te0[s2 >> 24] ^ SDRM_Te1[(s3 >> 16) & 0xff] ^ SDRM_Te2[(s0 >> 8) & 0xff] ^ SDRM_Te3[s1 & 0xff] ^ rk[30]; - t3 = SDRM_Te0[s3 >> 24] ^ SDRM_Te1[(s0 >> 16) & 0xff] ^ SDRM_Te2[(s1 >> 8) & 0xff] ^ SDRM_Te3[s2 & 0xff] ^ rk[31]; - /* round 8: */ - s0 = SDRM_Te0[t0 >> 24] ^ SDRM_Te1[(t1 >> 16) & 0xff] ^ SDRM_Te2[(t2 >> 8) & 0xff] ^ SDRM_Te3[t3 & 0xff] ^ rk[32]; - s1 = SDRM_Te0[t1 >> 24] ^ SDRM_Te1[(t2 >> 16) & 0xff] ^ SDRM_Te2[(t3 >> 8) & 0xff] ^ SDRM_Te3[t0 & 0xff] ^ rk[33]; - s2 = SDRM_Te0[t2 >> 24] ^ SDRM_Te1[(t3 >> 16) & 0xff] ^ SDRM_Te2[(t0 >> 8) & 0xff] ^ SDRM_Te3[t1 & 0xff] ^ rk[34]; - s3 = SDRM_Te0[t3 >> 24] ^ SDRM_Te1[(t0 >> 16) & 0xff] ^ SDRM_Te2[(t1 >> 8) & 0xff] ^ SDRM_Te3[t2 & 0xff] ^ rk[35]; - /* round 9: */ - t0 = SDRM_Te0[s0 >> 24] ^ SDRM_Te1[(s1 >> 16) & 0xff] ^ SDRM_Te2[(s2 >> 8) & 0xff] ^ SDRM_Te3[s3 & 0xff] ^ rk[36]; - t1 = SDRM_Te0[s1 >> 24] ^ SDRM_Te1[(s2 >> 16) & 0xff] ^ SDRM_Te2[(s3 >> 8) & 0xff] ^ SDRM_Te3[s0 & 0xff] ^ rk[37]; - t2 = SDRM_Te0[s2 >> 24] ^ SDRM_Te1[(s3 >> 16) & 0xff] ^ SDRM_Te2[(s0 >> 8) & 0xff] ^ SDRM_Te3[s1 & 0xff] ^ rk[38]; - t3 = SDRM_Te0[s3 >> 24] ^ SDRM_Te1[(s0 >> 16) & 0xff] ^ SDRM_Te2[(s1 >> 8) & 0xff] ^ SDRM_Te3[s2 & 0xff] ^ rk[39]; - if (Nr > 10) - { - /* round 10: */ - s0 = SDRM_Te0[t0 >> 24] ^ SDRM_Te1[(t1 >> 16) & 0xff] ^ SDRM_Te2[(t2 >> 8) & 0xff] ^ SDRM_Te3[t3 & 0xff] ^ rk[40]; - s1 = SDRM_Te0[t1 >> 24] ^ SDRM_Te1[(t2 >> 16) & 0xff] ^ SDRM_Te2[(t3 >> 8) & 0xff] ^ SDRM_Te3[t0 & 0xff] ^ rk[41]; - s2 = SDRM_Te0[t2 >> 24] ^ SDRM_Te1[(t3 >> 16) & 0xff] ^ SDRM_Te2[(t0 >> 8) & 0xff] ^ SDRM_Te3[t1 & 0xff] ^ rk[42]; - s3 = SDRM_Te0[t3 >> 24] ^ SDRM_Te1[(t0 >> 16) & 0xff] ^ SDRM_Te2[(t1 >> 8) & 0xff] ^ SDRM_Te3[t2 & 0xff] ^ rk[43]; - /* round 11: */ - t0 = SDRM_Te0[s0 >> 24] ^ SDRM_Te1[(s1 >> 16) & 0xff] ^ SDRM_Te2[(s2 >> 8) & 0xff] ^ SDRM_Te3[s3 & 0xff] ^ rk[44]; - t1 = SDRM_Te0[s1 >> 24] ^ SDRM_Te1[(s2 >> 16) & 0xff] ^ SDRM_Te2[(s3 >> 8) & 0xff] ^ SDRM_Te3[s0 & 0xff] ^ rk[45]; - t2 = SDRM_Te0[s2 >> 24] ^ SDRM_Te1[(s3 >> 16) & 0xff] ^ SDRM_Te2[(s0 >> 8) & 0xff] ^ SDRM_Te3[s1 & 0xff] ^ rk[46]; - t3 = SDRM_Te0[s3 >> 24] ^ SDRM_Te1[(s0 >> 16) & 0xff] ^ SDRM_Te2[(s1 >> 8) & 0xff] ^ SDRM_Te3[s2 & 0xff] ^ rk[47]; - if (Nr > 12) { - /* round 12: */ - s0 = SDRM_Te0[t0 >> 24] ^ SDRM_Te1[(t1 >> 16) & 0xff] ^ SDRM_Te2[(t2 >> 8) & 0xff] ^ SDRM_Te3[t3 & 0xff] ^ rk[48]; - s1 = SDRM_Te0[t1 >> 24] ^ SDRM_Te1[(t2 >> 16) & 0xff] ^ SDRM_Te2[(t3 >> 8) & 0xff] ^ SDRM_Te3[t0 & 0xff] ^ rk[49]; - s2 = SDRM_Te0[t2 >> 24] ^ SDRM_Te1[(t3 >> 16) & 0xff] ^ SDRM_Te2[(t0 >> 8) & 0xff] ^ SDRM_Te3[t1 & 0xff] ^ rk[50]; - s3 = SDRM_Te0[t3 >> 24] ^ SDRM_Te1[(t0 >> 16) & 0xff] ^ SDRM_Te2[(t1 >> 8) & 0xff] ^ SDRM_Te3[t2 & 0xff] ^ rk[51]; - /* round 13: */ - t0 = SDRM_Te0[s0 >> 24] ^ SDRM_Te1[(s1 >> 16) & 0xff] ^ SDRM_Te2[(s2 >> 8) & 0xff] ^ SDRM_Te3[s3 & 0xff] ^ rk[52]; - t1 = SDRM_Te0[s1 >> 24] ^ SDRM_Te1[(s2 >> 16) & 0xff] ^ SDRM_Te2[(s3 >> 8) & 0xff] ^ SDRM_Te3[s0 & 0xff] ^ rk[53]; - t2 = SDRM_Te0[s2 >> 24] ^ SDRM_Te1[(s3 >> 16) & 0xff] ^ SDRM_Te2[(s0 >> 8) & 0xff] ^ SDRM_Te3[s1 & 0xff] ^ rk[54]; - t3 = SDRM_Te0[s3 >> 24] ^ SDRM_Te1[(s0 >> 16) & 0xff] ^ SDRM_Te2[(s1 >> 8) & 0xff] ^ SDRM_Te3[s2 & 0xff] ^ rk[55]; - } - } - rk += Nr << 2; + /* round 1: */ + t0 = SDRM_Te0[s0 >> 24] ^ SDRM_Te1[(s1 >> 16) & 0xff] ^ + SDRM_Te2[(s2 >> 8) & 0xff] ^ SDRM_Te3[s3 & 0xff] ^ rk[4]; + t1 = SDRM_Te0[s1 >> 24] ^ SDRM_Te1[(s2 >> 16) & 0xff] ^ + SDRM_Te2[(s3 >> 8) & 0xff] ^ SDRM_Te3[s0 & 0xff] ^ rk[5]; + t2 = SDRM_Te0[s2 >> 24] ^ SDRM_Te1[(s3 >> 16) & 0xff] ^ + SDRM_Te2[(s0 >> 8) & 0xff] ^ SDRM_Te3[s1 & 0xff] ^ rk[6]; + t3 = SDRM_Te0[s3 >> 24] ^ SDRM_Te1[(s0 >> 16) & 0xff] ^ + SDRM_Te2[(s1 >> 8) & 0xff] ^ SDRM_Te3[s2 & 0xff] ^ rk[7]; + /* round 2: */ + s0 = SDRM_Te0[t0 >> 24] ^ SDRM_Te1[(t1 >> 16) & 0xff] ^ + SDRM_Te2[(t2 >> 8) & 0xff] ^ SDRM_Te3[t3 & 0xff] ^ rk[8]; + s1 = SDRM_Te0[t1 >> 24] ^ SDRM_Te1[(t2 >> 16) & 0xff] ^ + SDRM_Te2[(t3 >> 8) & 0xff] ^ SDRM_Te3[t0 & 0xff] ^ rk[9]; + s2 = SDRM_Te0[t2 >> 24] ^ SDRM_Te1[(t3 >> 16) & 0xff] ^ + SDRM_Te2[(t0 >> 8) & 0xff] ^ SDRM_Te3[t1 & 0xff] ^ rk[10]; + s3 = SDRM_Te0[t3 >> 24] ^ SDRM_Te1[(t0 >> 16) & 0xff] ^ + SDRM_Te2[(t1 >> 8) & 0xff] ^ SDRM_Te3[t2 & 0xff] ^ rk[11]; + /* round 3: */ + t0 = SDRM_Te0[s0 >> 24] ^ SDRM_Te1[(s1 >> 16) & 0xff] ^ + SDRM_Te2[(s2 >> 8) & 0xff] ^ SDRM_Te3[s3 & 0xff] ^ rk[12]; + t1 = SDRM_Te0[s1 >> 24] ^ SDRM_Te1[(s2 >> 16) & 0xff] ^ + SDRM_Te2[(s3 >> 8) & 0xff] ^ SDRM_Te3[s0 & 0xff] ^ rk[13]; + t2 = SDRM_Te0[s2 >> 24] ^ SDRM_Te1[(s3 >> 16) & 0xff] ^ + SDRM_Te2[(s0 >> 8) & 0xff] ^ SDRM_Te3[s1 & 0xff] ^ rk[14]; + t3 = SDRM_Te0[s3 >> 24] ^ SDRM_Te1[(s0 >> 16) & 0xff] ^ + SDRM_Te2[(s1 >> 8) & 0xff] ^ SDRM_Te3[s2 & 0xff] ^ rk[15]; + /* round 4: */ + s0 = SDRM_Te0[t0 >> 24] ^ SDRM_Te1[(t1 >> 16) & 0xff] ^ + SDRM_Te2[(t2 >> 8) & 0xff] ^ SDRM_Te3[t3 & 0xff] ^ rk[16]; + s1 = SDRM_Te0[t1 >> 24] ^ SDRM_Te1[(t2 >> 16) & 0xff] ^ + SDRM_Te2[(t3 >> 8) & 0xff] ^ SDRM_Te3[t0 & 0xff] ^ rk[17]; + s2 = SDRM_Te0[t2 >> 24] ^ SDRM_Te1[(t3 >> 16) & 0xff] ^ + SDRM_Te2[(t0 >> 8) & 0xff] ^ SDRM_Te3[t1 & 0xff] ^ rk[18]; + s3 = SDRM_Te0[t3 >> 24] ^ SDRM_Te1[(t0 >> 16) & 0xff] ^ + SDRM_Te2[(t1 >> 8) & 0xff] ^ SDRM_Te3[t2 & 0xff] ^ rk[19]; + /* round 5: */ + t0 = SDRM_Te0[s0 >> 24] ^ SDRM_Te1[(s1 >> 16) & 0xff] ^ + SDRM_Te2[(s2 >> 8) & 0xff] ^ SDRM_Te3[s3 & 0xff] ^ rk[20]; + t1 = SDRM_Te0[s1 >> 24] ^ SDRM_Te1[(s2 >> 16) & 0xff] ^ + SDRM_Te2[(s3 >> 8) & 0xff] ^ SDRM_Te3[s0 & 0xff] ^ rk[21]; + t2 = SDRM_Te0[s2 >> 24] ^ SDRM_Te1[(s3 >> 16) & 0xff] ^ + SDRM_Te2[(s0 >> 8) & 0xff] ^ SDRM_Te3[s1 & 0xff] ^ rk[22]; + t3 = SDRM_Te0[s3 >> 24] ^ SDRM_Te1[(s0 >> 16) & 0xff] ^ + SDRM_Te2[(s1 >> 8) & 0xff] ^ SDRM_Te3[s2 & 0xff] ^ rk[23]; + /* round 6: */ + s0 = SDRM_Te0[t0 >> 24] ^ SDRM_Te1[(t1 >> 16) & 0xff] ^ + SDRM_Te2[(t2 >> 8) & 0xff] ^ SDRM_Te3[t3 & 0xff] ^ rk[24]; + s1 = SDRM_Te0[t1 >> 24] ^ SDRM_Te1[(t2 >> 16) & 0xff] ^ + SDRM_Te2[(t3 >> 8) & 0xff] ^ SDRM_Te3[t0 & 0xff] ^ rk[25]; + s2 = SDRM_Te0[t2 >> 24] ^ SDRM_Te1[(t3 >> 16) & 0xff] ^ + SDRM_Te2[(t0 >> 8) & 0xff] ^ SDRM_Te3[t1 & 0xff] ^ rk[26]; + s3 = SDRM_Te0[t3 >> 24] ^ SDRM_Te1[(t0 >> 16) & 0xff] ^ + SDRM_Te2[(t1 >> 8) & 0xff] ^ SDRM_Te3[t2 & 0xff] ^ rk[27]; + /* round 7: */ + t0 = SDRM_Te0[s0 >> 24] ^ SDRM_Te1[(s1 >> 16) & 0xff] ^ + SDRM_Te2[(s2 >> 8) & 0xff] ^ SDRM_Te3[s3 & 0xff] ^ rk[28]; + t1 = SDRM_Te0[s1 >> 24] ^ SDRM_Te1[(s2 >> 16) & 0xff] ^ + SDRM_Te2[(s3 >> 8) & 0xff] ^ SDRM_Te3[s0 & 0xff] ^ rk[29]; + t2 = SDRM_Te0[s2 >> 24] ^ SDRM_Te1[(s3 >> 16) & 0xff] ^ + SDRM_Te2[(s0 >> 8) & 0xff] ^ SDRM_Te3[s1 & 0xff] ^ rk[30]; + t3 = SDRM_Te0[s3 >> 24] ^ SDRM_Te1[(s0 >> 16) & 0xff] ^ + SDRM_Te2[(s1 >> 8) & 0xff] ^ SDRM_Te3[s2 & 0xff] ^ rk[31]; + /* round 8: */ + s0 = SDRM_Te0[t0 >> 24] ^ SDRM_Te1[(t1 >> 16) & 0xff] ^ + SDRM_Te2[(t2 >> 8) & 0xff] ^ SDRM_Te3[t3 & 0xff] ^ rk[32]; + s1 = SDRM_Te0[t1 >> 24] ^ SDRM_Te1[(t2 >> 16) & 0xff] ^ + SDRM_Te2[(t3 >> 8) & 0xff] ^ SDRM_Te3[t0 & 0xff] ^ rk[33]; + s2 = SDRM_Te0[t2 >> 24] ^ SDRM_Te1[(t3 >> 16) & 0xff] ^ + SDRM_Te2[(t0 >> 8) & 0xff] ^ SDRM_Te3[t1 & 0xff] ^ rk[34]; + s3 = SDRM_Te0[t3 >> 24] ^ SDRM_Te1[(t0 >> 16) & 0xff] ^ + SDRM_Te2[(t1 >> 8) & 0xff] ^ SDRM_Te3[t2 & 0xff] ^ rk[35]; + /* round 9: */ + t0 = SDRM_Te0[s0 >> 24] ^ SDRM_Te1[(s1 >> 16) & 0xff] ^ + SDRM_Te2[(s2 >> 8) & 0xff] ^ SDRM_Te3[s3 & 0xff] ^ rk[36]; + t1 = SDRM_Te0[s1 >> 24] ^ SDRM_Te1[(s2 >> 16) & 0xff] ^ + SDRM_Te2[(s3 >> 8) & 0xff] ^ SDRM_Te3[s0 & 0xff] ^ rk[37]; + t2 = SDRM_Te0[s2 >> 24] ^ SDRM_Te1[(s3 >> 16) & 0xff] ^ + SDRM_Te2[(s0 >> 8) & 0xff] ^ SDRM_Te3[s1 & 0xff] ^ rk[38]; + t3 = SDRM_Te0[s3 >> 24] ^ SDRM_Te1[(s0 >> 16) & 0xff] ^ + SDRM_Te2[(s1 >> 8) & 0xff] ^ SDRM_Te3[s2 & 0xff] ^ rk[39]; + + if (Nr > 10) { + /* round 10: */ + s0 = SDRM_Te0[t0 >> 24] ^ SDRM_Te1[(t1 >> 16) & 0xff] ^ + SDRM_Te2[(t2 >> 8) & 0xff] ^ SDRM_Te3[t3 & 0xff] ^ rk[40]; + s1 = SDRM_Te0[t1 >> 24] ^ SDRM_Te1[(t2 >> 16) & 0xff] ^ + SDRM_Te2[(t3 >> 8) & 0xff] ^ SDRM_Te3[t0 & 0xff] ^ rk[41]; + s2 = SDRM_Te0[t2 >> 24] ^ SDRM_Te1[(t3 >> 16) & 0xff] ^ + SDRM_Te2[(t0 >> 8) & 0xff] ^ SDRM_Te3[t1 & 0xff] ^ rk[42]; + s3 = SDRM_Te0[t3 >> 24] ^ SDRM_Te1[(t0 >> 16) & 0xff] ^ + SDRM_Te2[(t1 >> 8) & 0xff] ^ SDRM_Te3[t2 & 0xff] ^ rk[43]; + /* round 11: */ + t0 = SDRM_Te0[s0 >> 24] ^ SDRM_Te1[(s1 >> 16) & 0xff] ^ + SDRM_Te2[(s2 >> 8) & 0xff] ^ SDRM_Te3[s3 & 0xff] ^ rk[44]; + t1 = SDRM_Te0[s1 >> 24] ^ SDRM_Te1[(s2 >> 16) & 0xff] ^ + SDRM_Te2[(s3 >> 8) & 0xff] ^ SDRM_Te3[s0 & 0xff] ^ rk[45]; + t2 = SDRM_Te0[s2 >> 24] ^ SDRM_Te1[(s3 >> 16) & 0xff] ^ + SDRM_Te2[(s0 >> 8) & 0xff] ^ SDRM_Te3[s1 & 0xff] ^ rk[46]; + t3 = SDRM_Te0[s3 >> 24] ^ SDRM_Te1[(s0 >> 16) & 0xff] ^ + SDRM_Te2[(s1 >> 8) & 0xff] ^ SDRM_Te3[s2 & 0xff] ^ rk[47]; + + if (Nr > 12) { + /* round 12: */ + s0 = SDRM_Te0[t0 >> 24] ^ SDRM_Te1[(t1 >> 16) & 0xff] ^ + SDRM_Te2[(t2 >> 8) & 0xff] ^ SDRM_Te3[t3 & 0xff] ^ rk[48]; + s1 = SDRM_Te0[t1 >> 24] ^ SDRM_Te1[(t2 >> 16) & 0xff] ^ + SDRM_Te2[(t3 >> 8) & 0xff] ^ SDRM_Te3[t0 & 0xff] ^ rk[49]; + s2 = SDRM_Te0[t2 >> 24] ^ SDRM_Te1[(t3 >> 16) & 0xff] ^ + SDRM_Te2[(t0 >> 8) & 0xff] ^ SDRM_Te3[t1 & 0xff] ^ rk[50]; + s3 = SDRM_Te0[t3 >> 24] ^ SDRM_Te1[(t0 >> 16) & 0xff] ^ + SDRM_Te2[(t1 >> 8) & 0xff] ^ SDRM_Te3[t2 & 0xff] ^ rk[51]; + /* round 13: */ + t0 = SDRM_Te0[s0 >> 24] ^ SDRM_Te1[(s1 >> 16) & 0xff] ^ + SDRM_Te2[(s2 >> 8) & 0xff] ^ SDRM_Te3[s3 & 0xff] ^ rk[52]; + t1 = SDRM_Te0[s1 >> 24] ^ SDRM_Te1[(s2 >> 16) & 0xff] ^ + SDRM_Te2[(s3 >> 8) & 0xff] ^ SDRM_Te3[s0 & 0xff] ^ rk[53]; + t2 = SDRM_Te0[s2 >> 24] ^ SDRM_Te1[(s3 >> 16) & 0xff] ^ + SDRM_Te2[(s0 >> 8) & 0xff] ^ SDRM_Te3[s1 & 0xff] ^ rk[54]; + t3 = SDRM_Te0[s3 >> 24] ^ SDRM_Te1[(s0 >> 16) & 0xff] ^ + SDRM_Te2[(s1 >> 8) & 0xff] ^ SDRM_Te3[s2 & 0xff] ^ rk[55]; + } + } + + rk += Nr << 2; #else /* !FULL_UNROLL */ - /* + /* * Nr - 1 full rounds: */ - r = Nr >> 1; - for (;;) - { - t0 = - SDRM_Te0[(s0 >> 24) ] ^ - SDRM_Te1[(s1 >> 16) & 0xff] ^ - SDRM_Te2[(s2 >> 8) & 0xff] ^ - SDRM_Te3[(s3 ) & 0xff] ^ - rk[4]; - t1 = - SDRM_Te0[(s1 >> 24) ] ^ - SDRM_Te1[(s2 >> 16) & 0xff] ^ - SDRM_Te2[(s3 >> 8) & 0xff] ^ - SDRM_Te3[(s0 ) & 0xff] ^ - rk[5]; - t2 = - SDRM_Te0[(s2 >> 24) ] ^ - SDRM_Te1[(s3 >> 16) & 0xff] ^ - SDRM_Te2[(s0 >> 8) & 0xff] ^ - SDRM_Te3[(s1 ) & 0xff] ^ - rk[6]; - t3 = - SDRM_Te0[(s3 >> 24) ] ^ - SDRM_Te1[(s0 >> 16) & 0xff] ^ - SDRM_Te2[(s1 >> 8) & 0xff] ^ - SDRM_Te3[(s2 ) & 0xff] ^ - rk[7]; - - rk += 8; - if (--r == 0) - { - break; - } - - s0 = - SDRM_Te0[(t0 >> 24) ] ^ - SDRM_Te1[(t1 >> 16) & 0xff] ^ - SDRM_Te2[(t2 >> 8) & 0xff] ^ - SDRM_Te3[(t3 ) & 0xff] ^ - rk[0]; - s1 = - SDRM_Te0[(t1 >> 24) ] ^ - SDRM_Te1[(t2 >> 16) & 0xff] ^ - SDRM_Te2[(t3 >> 8) & 0xff] ^ - SDRM_Te3[(t0 ) & 0xff] ^ - rk[1]; - s2 = - SDRM_Te0[(t2 >> 24) ] ^ - SDRM_Te1[(t3 >> 16) & 0xff] ^ - SDRM_Te2[(t0 >> 8) & 0xff] ^ - SDRM_Te3[(t1 ) & 0xff] ^ - rk[2]; - s3 = - SDRM_Te0[(t3 >> 24) ] ^ - SDRM_Te1[(t0 >> 16) & 0xff] ^ - SDRM_Te2[(t1 >> 8) & 0xff] ^ - SDRM_Te3[(t2 ) & 0xff] ^ - rk[3]; - } + r = Nr >> 1; + + for (;;) { + t0 = + SDRM_Te0[(s0 >> 24)] ^ + SDRM_Te1[(s1 >> 16) & 0xff] ^ + SDRM_Te2[(s2 >> 8) & 0xff] ^ + SDRM_Te3[(s3) & 0xff] ^ + rk[4]; + t1 = + SDRM_Te0[(s1 >> 24)] ^ + SDRM_Te1[(s2 >> 16) & 0xff] ^ + SDRM_Te2[(s3 >> 8) & 0xff] ^ + SDRM_Te3[(s0) & 0xff] ^ + rk[5]; + t2 = + SDRM_Te0[(s2 >> 24)] ^ + SDRM_Te1[(s3 >> 16) & 0xff] ^ + SDRM_Te2[(s0 >> 8) & 0xff] ^ + SDRM_Te3[(s1) & 0xff] ^ + rk[6]; + t3 = + SDRM_Te0[(s3 >> 24)] ^ + SDRM_Te1[(s0 >> 16) & 0xff] ^ + SDRM_Te2[(s1 >> 8) & 0xff] ^ + SDRM_Te3[(s2) & 0xff] ^ + rk[7]; + + rk += 8; + + if (--r == 0) + break; + + s0 = + SDRM_Te0[(t0 >> 24)] ^ + SDRM_Te1[(t1 >> 16) & 0xff] ^ + SDRM_Te2[(t2 >> 8) & 0xff] ^ + SDRM_Te3[(t3) & 0xff] ^ + rk[0]; + s1 = + SDRM_Te0[(t1 >> 24)] ^ + SDRM_Te1[(t2 >> 16) & 0xff] ^ + SDRM_Te2[(t3 >> 8) & 0xff] ^ + SDRM_Te3[(t0) & 0xff] ^ + rk[1]; + s2 = + SDRM_Te0[(t2 >> 24)] ^ + SDRM_Te1[(t3 >> 16) & 0xff] ^ + SDRM_Te2[(t0 >> 8) & 0xff] ^ + SDRM_Te3[(t1) & 0xff] ^ + rk[2]; + s3 = + SDRM_Te0[(t3 >> 24)] ^ + SDRM_Te1[(t0 >> 16) & 0xff] ^ + SDRM_Te2[(t1 >> 8) & 0xff] ^ + SDRM_Te3[(t2) & 0xff] ^ + rk[3]; + } + #endif /* ?FULL_UNROLL */ - /* + /* * apply last round and * map cipher state to byte array block: */ s0 = - (SDRM_Te4[(t0 >> 24) ] & 0xff000000) ^ + (SDRM_Te4[(t0 >> 24)] & 0xff000000) ^ (SDRM_Te4[(t1 >> 16) & 0xff] & 0x00ff0000) ^ (SDRM_Te4[(t2 >> 8) & 0xff] & 0x0000ff00) ^ - (SDRM_Te4[(t3 ) & 0xff] & 0x000000ff) ^ + (SDRM_Te4[(t3) & 0xff] & 0x000000ff) ^ rk[0]; - PUTUINT32(ct , s0); + PUTUINT32(ct, s0); s1 = - (SDRM_Te4[(t1 >> 24) ] & 0xff000000) ^ + (SDRM_Te4[(t1 >> 24)] & 0xff000000) ^ (SDRM_Te4[(t2 >> 16) & 0xff] & 0x00ff0000) ^ (SDRM_Te4[(t3 >> 8) & 0xff] & 0x0000ff00) ^ - (SDRM_Te4[(t0 ) & 0xff] & 0x000000ff) ^ + (SDRM_Te4[(t0) & 0xff] & 0x000000ff) ^ rk[1]; PUTUINT32(ct + 4, s1); s2 = - (SDRM_Te4[(t2 >> 24) ] & 0xff000000) ^ + (SDRM_Te4[(t2 >> 24)] & 0xff000000) ^ (SDRM_Te4[(t3 >> 16) & 0xff] & 0x00ff0000) ^ (SDRM_Te4[(t0 >> 8) & 0xff] & 0x0000ff00) ^ - (SDRM_Te4[(t1 ) & 0xff] & 0x000000ff) ^ + (SDRM_Te4[(t1) & 0xff] & 0x000000ff) ^ rk[2]; PUTUINT32(ct + 8, s2); s3 = - (SDRM_Te4[(t3 >> 24) ] & 0xff000000) ^ + (SDRM_Te4[(t3 >> 24)] & 0xff000000) ^ (SDRM_Te4[(t0 >> 16) & 0xff] & 0x00ff0000) ^ (SDRM_Te4[(t1 >> 8) & 0xff] & 0x0000ff00) ^ - (SDRM_Te4[(t2 ) & 0xff] & 0x000000ff) ^ + (SDRM_Te4[(t2) & 0xff] & 0x000000ff) ^ rk[3]; PUTUINT32(ct + 12, s3); } /* - * @fn SDRM_rijndaelDecrypt - * @brief 16 byte AES Decryption with round key + * @fn SDRM_rijndaelDecrypt + * @brief 16 byte AES Decryption with round key * - * @param rk [in]expanded round key - * @param Nr [in]numer of rounds - * @param ct [in]cipher text - * @param pt [out]plain text + * @param rk [in]expanded round key + * @param Nr [in]numer of rounds + * @param ct [in]cipher text + * @param pt [out]plain text * - * @return void + * @return void */ -void SDRM_rijndaelDecrypt(const cc_u32 rk[/*4*(Nr + 1)*/], int Nr, const cc_u8 ct[16], cc_u8 pt[16]) +void SDRM_rijndaelDecrypt(const cc_u32 rk[/*4*(Nr + 1)*/], int Nr, + const cc_u8 ct[16], cc_u8 pt[16]) { cc_u32 s0, s1, s2, s3, t0, t1, t2, t3; #ifndef FULL_UNROLL - int r; + int r; #endif /* ?FULL_UNROLL */ - /* + /* * map byte array block to cipher state * and add initial round key: */ - s0 = GETUINT32(ct ) ^ rk[0]; - s1 = GETUINT32(ct + 4) ^ rk[1]; - s2 = GETUINT32(ct + 8) ^ rk[2]; - s3 = GETUINT32(ct + 12) ^ rk[3]; + s0 = GETUINT32(ct) ^ rk[0]; + s1 = GETUINT32(ct + 4) ^ rk[1]; + s2 = GETUINT32(ct + 8) ^ rk[2]; + s3 = GETUINT32(ct + 12) ^ rk[3]; #ifdef FULL_UNROLL - /* round 1: */ - t0 = SDRM_Td0[s0 >> 24] ^ SDRM_Td1[(s3 >> 16) & 0xff] ^ SDRM_Td2[(s2 >> 8) & 0xff] ^ SDRM_Td3[s1 & 0xff] ^ rk[ 4]; - t1 = SDRM_Td0[s1 >> 24] ^ SDRM_Td1[(s0 >> 16) & 0xff] ^ SDRM_Td2[(s3 >> 8) & 0xff] ^ SDRM_Td3[s2 & 0xff] ^ rk[ 5]; - t2 = SDRM_Td0[s2 >> 24] ^ SDRM_Td1[(s1 >> 16) & 0xff] ^ SDRM_Td2[(s0 >> 8) & 0xff] ^ SDRM_Td3[s3 & 0xff] ^ rk[ 6]; - t3 = SDRM_Td0[s3 >> 24] ^ SDRM_Td1[(s2 >> 16) & 0xff] ^ SDRM_Td2[(s1 >> 8) & 0xff] ^ SDRM_Td3[s0 & 0xff] ^ rk[ 7]; - /* round 2: */ - s0 = SDRM_Td0[t0 >> 24] ^ SDRM_Td1[(t3 >> 16) & 0xff] ^ SDRM_Td2[(t2 >> 8) & 0xff] ^ SDRM_Td3[t1 & 0xff] ^ rk[ 8]; - s1 = SDRM_Td0[t1 >> 24] ^ SDRM_Td1[(t0 >> 16) & 0xff] ^ SDRM_Td2[(t3 >> 8) & 0xff] ^ SDRM_Td3[t2 & 0xff] ^ rk[ 9]; - s2 = SDRM_Td0[t2 >> 24] ^ SDRM_Td1[(t1 >> 16) & 0xff] ^ SDRM_Td2[(t0 >> 8) & 0xff] ^ SDRM_Td3[t3 & 0xff] ^ rk[10]; - s3 = SDRM_Td0[t3 >> 24] ^ SDRM_Td1[(t2 >> 16) & 0xff] ^ SDRM_Td2[(t1 >> 8) & 0xff] ^ SDRM_Td3[t0 & 0xff] ^ rk[11]; - /* round 3: */ - t0 = SDRM_Td0[s0 >> 24] ^ SDRM_Td1[(s3 >> 16) & 0xff] ^ SDRM_Td2[(s2 >> 8) & 0xff] ^ SDRM_Td3[s1 & 0xff] ^ rk[12]; - t1 = SDRM_Td0[s1 >> 24] ^ SDRM_Td1[(s0 >> 16) & 0xff] ^ SDRM_Td2[(s3 >> 8) & 0xff] ^ SDRM_Td3[s2 & 0xff] ^ rk[13]; - t2 = SDRM_Td0[s2 >> 24] ^ SDRM_Td1[(s1 >> 16) & 0xff] ^ SDRM_Td2[(s0 >> 8) & 0xff] ^ SDRM_Td3[s3 & 0xff] ^ rk[14]; - t3 = SDRM_Td0[s3 >> 24] ^ SDRM_Td1[(s2 >> 16) & 0xff] ^ SDRM_Td2[(s1 >> 8) & 0xff] ^ SDRM_Td3[s0 & 0xff] ^ rk[15]; - /* round 4: */ - s0 = SDRM_Td0[t0 >> 24] ^ SDRM_Td1[(t3 >> 16) & 0xff] ^ SDRM_Td2[(t2 >> 8) & 0xff] ^ SDRM_Td3[t1 & 0xff] ^ rk[16]; - s1 = SDRM_Td0[t1 >> 24] ^ SDRM_Td1[(t0 >> 16) & 0xff] ^ SDRM_Td2[(t3 >> 8) & 0xff] ^ SDRM_Td3[t2 & 0xff] ^ rk[17]; - s2 = SDRM_Td0[t2 >> 24] ^ SDRM_Td1[(t1 >> 16) & 0xff] ^ SDRM_Td2[(t0 >> 8) & 0xff] ^ SDRM_Td3[t3 & 0xff] ^ rk[18]; - s3 = SDRM_Td0[t3 >> 24] ^ SDRM_Td1[(t2 >> 16) & 0xff] ^ SDRM_Td2[(t1 >> 8) & 0xff] ^ SDRM_Td3[t0 & 0xff] ^ rk[19]; - /* round 5: */ - t0 = SDRM_Td0[s0 >> 24] ^ SDRM_Td1[(s3 >> 16) & 0xff] ^ SDRM_Td2[(s2 >> 8) & 0xff] ^ SDRM_Td3[s1 & 0xff] ^ rk[20]; - t1 = SDRM_Td0[s1 >> 24] ^ SDRM_Td1[(s0 >> 16) & 0xff] ^ SDRM_Td2[(s3 >> 8) & 0xff] ^ SDRM_Td3[s2 & 0xff] ^ rk[21]; - t2 = SDRM_Td0[s2 >> 24] ^ SDRM_Td1[(s1 >> 16) & 0xff] ^ SDRM_Td2[(s0 >> 8) & 0xff] ^ SDRM_Td3[s3 & 0xff] ^ rk[22]; - t3 = SDRM_Td0[s3 >> 24] ^ SDRM_Td1[(s2 >> 16) & 0xff] ^ SDRM_Td2[(s1 >> 8) & 0xff] ^ SDRM_Td3[s0 & 0xff] ^ rk[23]; - /* round 6: */ - s0 = SDRM_Td0[t0 >> 24] ^ SDRM_Td1[(t3 >> 16) & 0xff] ^ SDRM_Td2[(t2 >> 8) & 0xff] ^ SDRM_Td3[t1 & 0xff] ^ rk[24]; - s1 = SDRM_Td0[t1 >> 24] ^ SDRM_Td1[(t0 >> 16) & 0xff] ^ SDRM_Td2[(t3 >> 8) & 0xff] ^ SDRM_Td3[t2 & 0xff] ^ rk[25]; - s2 = SDRM_Td0[t2 >> 24] ^ SDRM_Td1[(t1 >> 16) & 0xff] ^ SDRM_Td2[(t0 >> 8) & 0xff] ^ SDRM_Td3[t3 & 0xff] ^ rk[26]; - s3 = SDRM_Td0[t3 >> 24] ^ SDRM_Td1[(t2 >> 16) & 0xff] ^ SDRM_Td2[(t1 >> 8) & 0xff] ^ SDRM_Td3[t0 & 0xff] ^ rk[27]; - /* round 7: */ - t0 = SDRM_Td0[s0 >> 24] ^ SDRM_Td1[(s3 >> 16) & 0xff] ^ SDRM_Td2[(s2 >> 8) & 0xff] ^ SDRM_Td3[s1 & 0xff] ^ rk[28]; - t1 = SDRM_Td0[s1 >> 24] ^ SDRM_Td1[(s0 >> 16) & 0xff] ^ SDRM_Td2[(s3 >> 8) & 0xff] ^ SDRM_Td3[s2 & 0xff] ^ rk[29]; - t2 = SDRM_Td0[s2 >> 24] ^ SDRM_Td1[(s1 >> 16) & 0xff] ^ SDRM_Td2[(s0 >> 8) & 0xff] ^ SDRM_Td3[s3 & 0xff] ^ rk[30]; - t3 = SDRM_Td0[s3 >> 24] ^ SDRM_Td1[(s2 >> 16) & 0xff] ^ SDRM_Td2[(s1 >> 8) & 0xff] ^ SDRM_Td3[s0 & 0xff] ^ rk[31]; - /* round 8: */ - s0 = SDRM_Td0[t0 >> 24] ^ SDRM_Td1[(t3 >> 16) & 0xff] ^ SDRM_Td2[(t2 >> 8) & 0xff] ^ SDRM_Td3[t1 & 0xff] ^ rk[32]; - s1 = SDRM_Td0[t1 >> 24] ^ SDRM_Td1[(t0 >> 16) & 0xff] ^ SDRM_Td2[(t3 >> 8) & 0xff] ^ SDRM_Td3[t2 & 0xff] ^ rk[33]; - s2 = SDRM_Td0[t2 >> 24] ^ SDRM_Td1[(t1 >> 16) & 0xff] ^ SDRM_Td2[(t0 >> 8) & 0xff] ^ SDRM_Td3[t3 & 0xff] ^ rk[34]; - s3 = SDRM_Td0[t3 >> 24] ^ SDRM_Td1[(t2 >> 16) & 0xff] ^ SDRM_Td2[(t1 >> 8) & 0xff] ^ SDRM_Td3[t0 & 0xff] ^ rk[35]; - /* round 9: */ - t0 = SDRM_Td0[s0 >> 24] ^ SDRM_Td1[(s3 >> 16) & 0xff] ^ SDRM_Td2[(s2 >> 8) & 0xff] ^ SDRM_Td3[s1 & 0xff] ^ rk[36]; - t1 = SDRM_Td0[s1 >> 24] ^ SDRM_Td1[(s0 >> 16) & 0xff] ^ SDRM_Td2[(s3 >> 8) & 0xff] ^ SDRM_Td3[s2 & 0xff] ^ rk[37]; - t2 = SDRM_Td0[s2 >> 24] ^ SDRM_Td1[(s1 >> 16) & 0xff] ^ SDRM_Td2[(s0 >> 8) & 0xff] ^ SDRM_Td3[s3 & 0xff] ^ rk[38]; - t3 = SDRM_Td0[s3 >> 24] ^ SDRM_Td1[(s2 >> 16) & 0xff] ^ SDRM_Td2[(s1 >> 8) & 0xff] ^ SDRM_Td3[s0 & 0xff] ^ rk[39]; - if (Nr > 10) - { - /* round 10: */ - s0 = SDRM_Td0[t0 >> 24] ^ SDRM_Td1[(t3 >> 16) & 0xff] ^ SDRM_Td2[(t2 >> 8) & 0xff] ^ SDRM_Td3[t1 & 0xff] ^ rk[40]; - s1 = SDRM_Td0[t1 >> 24] ^ SDRM_Td1[(t0 >> 16) & 0xff] ^ SDRM_Td2[(t3 >> 8) & 0xff] ^ SDRM_Td3[t2 & 0xff] ^ rk[41]; - s2 = SDRM_Td0[t2 >> 24] ^ SDRM_Td1[(t1 >> 16) & 0xff] ^ SDRM_Td2[(t0 >> 8) & 0xff] ^ SDRM_Td3[t3 & 0xff] ^ rk[42]; - s3 = SDRM_Td0[t3 >> 24] ^ SDRM_Td1[(t2 >> 16) & 0xff] ^ SDRM_Td2[(t1 >> 8) & 0xff] ^ SDRM_Td3[t0 & 0xff] ^ rk[43]; - /* round 11: */ - t0 = SDRM_Td0[s0 >> 24] ^ SDRM_Td1[(s3 >> 16) & 0xff] ^ SDRM_Td2[(s2 >> 8) & 0xff] ^ SDRM_Td3[s1 & 0xff] ^ rk[44]; - t1 = SDRM_Td0[s1 >> 24] ^ SDRM_Td1[(s0 >> 16) & 0xff] ^ SDRM_Td2[(s3 >> 8) & 0xff] ^ SDRM_Td3[s2 & 0xff] ^ rk[45]; - t2 = SDRM_Td0[s2 >> 24] ^ SDRM_Td1[(s1 >> 16) & 0xff] ^ SDRM_Td2[(s0 >> 8) & 0xff] ^ SDRM_Td3[s3 & 0xff] ^ rk[46]; - t3 = SDRM_Td0[s3 >> 24] ^ SDRM_Td1[(s2 >> 16) & 0xff] ^ SDRM_Td2[(s1 >> 8) & 0xff] ^ SDRM_Td3[s0 & 0xff] ^ rk[47]; - if (Nr > 12) { - /* round 12: */ - s0 = SDRM_Td0[t0 >> 24] ^ SDRM_Td1[(t3 >> 16) & 0xff] ^ SDRM_Td2[(t2 >> 8) & 0xff] ^ SDRM_Td3[t1 & 0xff] ^ rk[48]; - s1 = SDRM_Td0[t1 >> 24] ^ SDRM_Td1[(t0 >> 16) & 0xff] ^ SDRM_Td2[(t3 >> 8) & 0xff] ^ SDRM_Td3[t2 & 0xff] ^ rk[49]; - s2 = SDRM_Td0[t2 >> 24] ^ SDRM_Td1[(t1 >> 16) & 0xff] ^ SDRM_Td2[(t0 >> 8) & 0xff] ^ SDRM_Td3[t3 & 0xff] ^ rk[50]; - s3 = SDRM_Td0[t3 >> 24] ^ SDRM_Td1[(t2 >> 16) & 0xff] ^ SDRM_Td2[(t1 >> 8) & 0xff] ^ SDRM_Td3[t0 & 0xff] ^ rk[51]; - /* round 13: */ - t0 = SDRM_Td0[s0 >> 24] ^ SDRM_Td1[(s3 >> 16) & 0xff] ^ SDRM_Td2[(s2 >> 8) & 0xff] ^ SDRM_Td3[s1 & 0xff] ^ rk[52]; - t1 = SDRM_Td0[s1 >> 24] ^ SDRM_Td1[(s0 >> 16) & 0xff] ^ SDRM_Td2[(s3 >> 8) & 0xff] ^ SDRM_Td3[s2 & 0xff] ^ rk[53]; - t2 = SDRM_Td0[s2 >> 24] ^ SDRM_Td1[(s1 >> 16) & 0xff] ^ SDRM_Td2[(s0 >> 8) & 0xff] ^ SDRM_Td3[s3 & 0xff] ^ rk[54]; - t3 = SDRM_Td0[s3 >> 24] ^ SDRM_Td1[(s2 >> 16) & 0xff] ^ SDRM_Td2[(s1 >> 8) & 0xff] ^ SDRM_Td3[s0 & 0xff] ^ rk[55]; - } - } + /* round 1: */ + t0 = SDRM_Td0[s0 >> 24] ^ SDRM_Td1[(s3 >> 16) & 0xff] ^ + SDRM_Td2[(s2 >> 8) & 0xff] ^ SDRM_Td3[s1 & 0xff] ^ rk[4]; + t1 = SDRM_Td0[s1 >> 24] ^ SDRM_Td1[(s0 >> 16) & 0xff] ^ + SDRM_Td2[(s3 >> 8) & 0xff] ^ SDRM_Td3[s2 & 0xff] ^ rk[5]; + t2 = SDRM_Td0[s2 >> 24] ^ SDRM_Td1[(s1 >> 16) & 0xff] ^ + SDRM_Td2[(s0 >> 8) & 0xff] ^ SDRM_Td3[s3 & 0xff] ^ rk[6]; + t3 = SDRM_Td0[s3 >> 24] ^ SDRM_Td1[(s2 >> 16) & 0xff] ^ + SDRM_Td2[(s1 >> 8) & 0xff] ^ SDRM_Td3[s0 & 0xff] ^ rk[7]; + /* round 2: */ + s0 = SDRM_Td0[t0 >> 24] ^ SDRM_Td1[(t3 >> 16) & 0xff] ^ + SDRM_Td2[(t2 >> 8) & 0xff] ^ SDRM_Td3[t1 & 0xff] ^ rk[8]; + s1 = SDRM_Td0[t1 >> 24] ^ SDRM_Td1[(t0 >> 16) & 0xff] ^ + SDRM_Td2[(t3 >> 8) & 0xff] ^ SDRM_Td3[t2 & 0xff] ^ rk[9]; + s2 = SDRM_Td0[t2 >> 24] ^ SDRM_Td1[(t1 >> 16) & 0xff] ^ + SDRM_Td2[(t0 >> 8) & 0xff] ^ SDRM_Td3[t3 & 0xff] ^ rk[10]; + s3 = SDRM_Td0[t3 >> 24] ^ SDRM_Td1[(t2 >> 16) & 0xff] ^ + SDRM_Td2[(t1 >> 8) & 0xff] ^ SDRM_Td3[t0 & 0xff] ^ rk[11]; + /* round 3: */ + t0 = SDRM_Td0[s0 >> 24] ^ SDRM_Td1[(s3 >> 16) & 0xff] ^ + SDRM_Td2[(s2 >> 8) & 0xff] ^ SDRM_Td3[s1 & 0xff] ^ rk[12]; + t1 = SDRM_Td0[s1 >> 24] ^ SDRM_Td1[(s0 >> 16) & 0xff] ^ + SDRM_Td2[(s3 >> 8) & 0xff] ^ SDRM_Td3[s2 & 0xff] ^ rk[13]; + t2 = SDRM_Td0[s2 >> 24] ^ SDRM_Td1[(s1 >> 16) & 0xff] ^ + SDRM_Td2[(s0 >> 8) & 0xff] ^ SDRM_Td3[s3 & 0xff] ^ rk[14]; + t3 = SDRM_Td0[s3 >> 24] ^ SDRM_Td1[(s2 >> 16) & 0xff] ^ + SDRM_Td2[(s1 >> 8) & 0xff] ^ SDRM_Td3[s0 & 0xff] ^ rk[15]; + /* round 4: */ + s0 = SDRM_Td0[t0 >> 24] ^ SDRM_Td1[(t3 >> 16) & 0xff] ^ + SDRM_Td2[(t2 >> 8) & 0xff] ^ SDRM_Td3[t1 & 0xff] ^ rk[16]; + s1 = SDRM_Td0[t1 >> 24] ^ SDRM_Td1[(t0 >> 16) & 0xff] ^ + SDRM_Td2[(t3 >> 8) & 0xff] ^ SDRM_Td3[t2 & 0xff] ^ rk[17]; + s2 = SDRM_Td0[t2 >> 24] ^ SDRM_Td1[(t1 >> 16) & 0xff] ^ + SDRM_Td2[(t0 >> 8) & 0xff] ^ SDRM_Td3[t3 & 0xff] ^ rk[18]; + s3 = SDRM_Td0[t3 >> 24] ^ SDRM_Td1[(t2 >> 16) & 0xff] ^ + SDRM_Td2[(t1 >> 8) & 0xff] ^ SDRM_Td3[t0 & 0xff] ^ rk[19]; + /* round 5: */ + t0 = SDRM_Td0[s0 >> 24] ^ SDRM_Td1[(s3 >> 16) & 0xff] ^ + SDRM_Td2[(s2 >> 8) & 0xff] ^ SDRM_Td3[s1 & 0xff] ^ rk[20]; + t1 = SDRM_Td0[s1 >> 24] ^ SDRM_Td1[(s0 >> 16) & 0xff] ^ + SDRM_Td2[(s3 >> 8) & 0xff] ^ SDRM_Td3[s2 & 0xff] ^ rk[21]; + t2 = SDRM_Td0[s2 >> 24] ^ SDRM_Td1[(s1 >> 16) & 0xff] ^ + SDRM_Td2[(s0 >> 8) & 0xff] ^ SDRM_Td3[s3 & 0xff] ^ rk[22]; + t3 = SDRM_Td0[s3 >> 24] ^ SDRM_Td1[(s2 >> 16) & 0xff] ^ + SDRM_Td2[(s1 >> 8) & 0xff] ^ SDRM_Td3[s0 & 0xff] ^ rk[23]; + /* round 6: */ + s0 = SDRM_Td0[t0 >> 24] ^ SDRM_Td1[(t3 >> 16) & 0xff] ^ + SDRM_Td2[(t2 >> 8) & 0xff] ^ SDRM_Td3[t1 & 0xff] ^ rk[24]; + s1 = SDRM_Td0[t1 >> 24] ^ SDRM_Td1[(t0 >> 16) & 0xff] ^ + SDRM_Td2[(t3 >> 8) & 0xff] ^ SDRM_Td3[t2 & 0xff] ^ rk[25]; + s2 = SDRM_Td0[t2 >> 24] ^ SDRM_Td1[(t1 >> 16) & 0xff] ^ + SDRM_Td2[(t0 >> 8) & 0xff] ^ SDRM_Td3[t3 & 0xff] ^ rk[26]; + s3 = SDRM_Td0[t3 >> 24] ^ SDRM_Td1[(t2 >> 16) & 0xff] ^ + SDRM_Td2[(t1 >> 8) & 0xff] ^ SDRM_Td3[t0 & 0xff] ^ rk[27]; + /* round 7: */ + t0 = SDRM_Td0[s0 >> 24] ^ SDRM_Td1[(s3 >> 16) & 0xff] ^ + SDRM_Td2[(s2 >> 8) & 0xff] ^ SDRM_Td3[s1 & 0xff] ^ rk[28]; + t1 = SDRM_Td0[s1 >> 24] ^ SDRM_Td1[(s0 >> 16) & 0xff] ^ + SDRM_Td2[(s3 >> 8) & 0xff] ^ SDRM_Td3[s2 & 0xff] ^ rk[29]; + t2 = SDRM_Td0[s2 >> 24] ^ SDRM_Td1[(s1 >> 16) & 0xff] ^ + SDRM_Td2[(s0 >> 8) & 0xff] ^ SDRM_Td3[s3 & 0xff] ^ rk[30]; + t3 = SDRM_Td0[s3 >> 24] ^ SDRM_Td1[(s2 >> 16) & 0xff] ^ + SDRM_Td2[(s1 >> 8) & 0xff] ^ SDRM_Td3[s0 & 0xff] ^ rk[31]; + /* round 8: */ + s0 = SDRM_Td0[t0 >> 24] ^ SDRM_Td1[(t3 >> 16) & 0xff] ^ + SDRM_Td2[(t2 >> 8) & 0xff] ^ SDRM_Td3[t1 & 0xff] ^ rk[32]; + s1 = SDRM_Td0[t1 >> 24] ^ SDRM_Td1[(t0 >> 16) & 0xff] ^ + SDRM_Td2[(t3 >> 8) & 0xff] ^ SDRM_Td3[t2 & 0xff] ^ rk[33]; + s2 = SDRM_Td0[t2 >> 24] ^ SDRM_Td1[(t1 >> 16) & 0xff] ^ + SDRM_Td2[(t0 >> 8) & 0xff] ^ SDRM_Td3[t3 & 0xff] ^ rk[34]; + s3 = SDRM_Td0[t3 >> 24] ^ SDRM_Td1[(t2 >> 16) & 0xff] ^ + SDRM_Td2[(t1 >> 8) & 0xff] ^ SDRM_Td3[t0 & 0xff] ^ rk[35]; + /* round 9: */ + t0 = SDRM_Td0[s0 >> 24] ^ SDRM_Td1[(s3 >> 16) & 0xff] ^ + SDRM_Td2[(s2 >> 8) & 0xff] ^ SDRM_Td3[s1 & 0xff] ^ rk[36]; + t1 = SDRM_Td0[s1 >> 24] ^ SDRM_Td1[(s0 >> 16) & 0xff] ^ + SDRM_Td2[(s3 >> 8) & 0xff] ^ SDRM_Td3[s2 & 0xff] ^ rk[37]; + t2 = SDRM_Td0[s2 >> 24] ^ SDRM_Td1[(s1 >> 16) & 0xff] ^ + SDRM_Td2[(s0 >> 8) & 0xff] ^ SDRM_Td3[s3 & 0xff] ^ rk[38]; + t3 = SDRM_Td0[s3 >> 24] ^ SDRM_Td1[(s2 >> 16) & 0xff] ^ + SDRM_Td2[(s1 >> 8) & 0xff] ^ SDRM_Td3[s0 & 0xff] ^ rk[39]; + + if (Nr > 10) { + /* round 10: */ + s0 = SDRM_Td0[t0 >> 24] ^ SDRM_Td1[(t3 >> 16) & 0xff] ^ + SDRM_Td2[(t2 >> 8) & 0xff] ^ SDRM_Td3[t1 & 0xff] ^ rk[40]; + s1 = SDRM_Td0[t1 >> 24] ^ SDRM_Td1[(t0 >> 16) & 0xff] ^ + SDRM_Td2[(t3 >> 8) & 0xff] ^ SDRM_Td3[t2 & 0xff] ^ rk[41]; + s2 = SDRM_Td0[t2 >> 24] ^ SDRM_Td1[(t1 >> 16) & 0xff] ^ + SDRM_Td2[(t0 >> 8) & 0xff] ^ SDRM_Td3[t3 & 0xff] ^ rk[42]; + s3 = SDRM_Td0[t3 >> 24] ^ SDRM_Td1[(t2 >> 16) & 0xff] ^ + SDRM_Td2[(t1 >> 8) & 0xff] ^ SDRM_Td3[t0 & 0xff] ^ rk[43]; + /* round 11: */ + t0 = SDRM_Td0[s0 >> 24] ^ SDRM_Td1[(s3 >> 16) & 0xff] ^ + SDRM_Td2[(s2 >> 8) & 0xff] ^ SDRM_Td3[s1 & 0xff] ^ rk[44]; + t1 = SDRM_Td0[s1 >> 24] ^ SDRM_Td1[(s0 >> 16) & 0xff] ^ + SDRM_Td2[(s3 >> 8) & 0xff] ^ SDRM_Td3[s2 & 0xff] ^ rk[45]; + t2 = SDRM_Td0[s2 >> 24] ^ SDRM_Td1[(s1 >> 16) & 0xff] ^ + SDRM_Td2[(s0 >> 8) & 0xff] ^ SDRM_Td3[s3 & 0xff] ^ rk[46]; + t3 = SDRM_Td0[s3 >> 24] ^ SDRM_Td1[(s2 >> 16) & 0xff] ^ + SDRM_Td2[(s1 >> 8) & 0xff] ^ SDRM_Td3[s0 & 0xff] ^ rk[47]; + + if (Nr > 12) { + /* round 12: */ + s0 = SDRM_Td0[t0 >> 24] ^ SDRM_Td1[(t3 >> 16) & 0xff] ^ + SDRM_Td2[(t2 >> 8) & 0xff] ^ SDRM_Td3[t1 & 0xff] ^ rk[48]; + s1 = SDRM_Td0[t1 >> 24] ^ SDRM_Td1[(t0 >> 16) & 0xff] ^ + SDRM_Td2[(t3 >> 8) & 0xff] ^ SDRM_Td3[t2 & 0xff] ^ rk[49]; + s2 = SDRM_Td0[t2 >> 24] ^ SDRM_Td1[(t1 >> 16) & 0xff] ^ + SDRM_Td2[(t0 >> 8) & 0xff] ^ SDRM_Td3[t3 & 0xff] ^ rk[50]; + s3 = SDRM_Td0[t3 >> 24] ^ SDRM_Td1[(t2 >> 16) & 0xff] ^ + SDRM_Td2[(t1 >> 8) & 0xff] ^ SDRM_Td3[t0 & 0xff] ^ rk[51]; + /* round 13: */ + t0 = SDRM_Td0[s0 >> 24] ^ SDRM_Td1[(s3 >> 16) & 0xff] ^ + SDRM_Td2[(s2 >> 8) & 0xff] ^ SDRM_Td3[s1 & 0xff] ^ rk[52]; + t1 = SDRM_Td0[s1 >> 24] ^ SDRM_Td1[(s0 >> 16) & 0xff] ^ + SDRM_Td2[(s3 >> 8) & 0xff] ^ SDRM_Td3[s2 & 0xff] ^ rk[53]; + t2 = SDRM_Td0[s2 >> 24] ^ SDRM_Td1[(s1 >> 16) & 0xff] ^ + SDRM_Td2[(s0 >> 8) & 0xff] ^ SDRM_Td3[s3 & 0xff] ^ rk[54]; + t3 = SDRM_Td0[s3 >> 24] ^ SDRM_Td1[(s2 >> 16) & 0xff] ^ + SDRM_Td2[(s1 >> 8) & 0xff] ^ SDRM_Td3[s0 & 0xff] ^ rk[55]; + } + } + rk += Nr << 2; #else /* !FULL_UNROLL */ - /* - * Nr - 1 full rounds: - */ - r = Nr >> 1; - for (;;) - { - t0 = - SDRM_Td0[(s0 >> 24) ] ^ - SDRM_Td1[(s3 >> 16) & 0xff] ^ - SDRM_Td2[(s2 >> 8) & 0xff] ^ - SDRM_Td3[(s1 ) & 0xff] ^ - rk[4]; - t1 = - SDRM_Td0[(s1 >> 24) ] ^ - SDRM_Td1[(s0 >> 16) & 0xff] ^ - SDRM_Td2[(s3 >> 8) & 0xff] ^ - SDRM_Td3[(s2 ) & 0xff] ^ - rk[5]; - t2 = - SDRM_Td0[(s2 >> 24) ] ^ - SDRM_Td1[(s1 >> 16) & 0xff] ^ - SDRM_Td2[(s0 >> 8) & 0xff] ^ - SDRM_Td3[(s3 ) & 0xff] ^ - rk[6]; - t3 = - SDRM_Td0[(s3 >> 24) ] ^ - SDRM_Td1[(s2 >> 16) & 0xff] ^ - SDRM_Td2[(s1 >> 8) & 0xff] ^ - SDRM_Td3[(s0 ) & 0xff] ^ - rk[7]; - - rk += 8; - if (--r == 0) - { - break; - } - - s0 = - SDRM_Td0[(t0 >> 24) ] ^ - SDRM_Td1[(t3 >> 16) & 0xff] ^ - SDRM_Td2[(t2 >> 8) & 0xff] ^ - SDRM_Td3[(t1 ) & 0xff] ^ - rk[0]; - s1 = - SDRM_Td0[(t1 >> 24) ] ^ - SDRM_Td1[(t0 >> 16) & 0xff] ^ - SDRM_Td2[(t3 >> 8) & 0xff] ^ - SDRM_Td3[(t2 ) & 0xff] ^ - rk[1]; - s2 = - SDRM_Td0[(t2 >> 24) ] ^ - SDRM_Td1[(t1 >> 16) & 0xff] ^ - SDRM_Td2[(t0 >> 8) & 0xff] ^ - SDRM_Td3[(t3 ) & 0xff] ^ - rk[2]; - s3 = - SDRM_Td0[(t3 >> 24) ] ^ - SDRM_Td1[(t2 >> 16) & 0xff] ^ - SDRM_Td2[(t1 >> 8) & 0xff] ^ - SDRM_Td3[(t0 ) & 0xff] ^ - rk[3]; - } + /* + * Nr - 1 full rounds: + */ + r = Nr >> 1; + + for (;;) { + t0 = + SDRM_Td0[(s0 >> 24)] ^ + SDRM_Td1[(s3 >> 16) & 0xff] ^ + SDRM_Td2[(s2 >> 8) & 0xff] ^ + SDRM_Td3[(s1) & 0xff] ^ + rk[4]; + t1 = + SDRM_Td0[(s1 >> 24)] ^ + SDRM_Td1[(s0 >> 16) & 0xff] ^ + SDRM_Td2[(s3 >> 8) & 0xff] ^ + SDRM_Td3[(s2) & 0xff] ^ + rk[5]; + t2 = + SDRM_Td0[(s2 >> 24)] ^ + SDRM_Td1[(s1 >> 16) & 0xff] ^ + SDRM_Td2[(s0 >> 8) & 0xff] ^ + SDRM_Td3[(s3) & 0xff] ^ + rk[6]; + t3 = + SDRM_Td0[(s3 >> 24)] ^ + SDRM_Td1[(s2 >> 16) & 0xff] ^ + SDRM_Td2[(s1 >> 8) & 0xff] ^ + SDRM_Td3[(s0) & 0xff] ^ + rk[7]; + + rk += 8; + + if (--r == 0) + break; + + s0 = + SDRM_Td0[(t0 >> 24)] ^ + SDRM_Td1[(t3 >> 16) & 0xff] ^ + SDRM_Td2[(t2 >> 8) & 0xff] ^ + SDRM_Td3[(t1) & 0xff] ^ + rk[0]; + s1 = + SDRM_Td0[(t1 >> 24)] ^ + SDRM_Td1[(t0 >> 16) & 0xff] ^ + SDRM_Td2[(t3 >> 8) & 0xff] ^ + SDRM_Td3[(t2) & 0xff] ^ + rk[1]; + s2 = + SDRM_Td0[(t2 >> 24)] ^ + SDRM_Td1[(t1 >> 16) & 0xff] ^ + SDRM_Td2[(t0 >> 8) & 0xff] ^ + SDRM_Td3[(t3) & 0xff] ^ + rk[2]; + s3 = + SDRM_Td0[(t3 >> 24)] ^ + SDRM_Td1[(t2 >> 16) & 0xff] ^ + SDRM_Td2[(t1 >> 8) & 0xff] ^ + SDRM_Td3[(t0) & 0xff] ^ + rk[3]; + } + #endif /* ?FULL_UNROLL */ - /* + /* * apply last round and * map cipher state to byte array block: */ - s0 = - (SDRM_Td4[(t0 >> 24) ] & 0xff000000) ^ - (SDRM_Td4[(t3 >> 16) & 0xff] & 0x00ff0000) ^ - (SDRM_Td4[(t2 >> 8) & 0xff] & 0x0000ff00) ^ - (SDRM_Td4[(t1 ) & 0xff] & 0x000000ff) ^ - rk[0]; - PUTUINT32(pt , s0); - s1 = - (SDRM_Td4[(t1 >> 24) ] & 0xff000000) ^ - (SDRM_Td4[(t0 >> 16) & 0xff] & 0x00ff0000) ^ - (SDRM_Td4[(t3 >> 8) & 0xff] & 0x0000ff00) ^ - (SDRM_Td4[(t2 ) & 0xff] & 0x000000ff) ^ - rk[1]; + s0 = + (SDRM_Td4[(t0 >> 24)] & 0xff000000) ^ + (SDRM_Td4[(t3 >> 16) & 0xff] & 0x00ff0000) ^ + (SDRM_Td4[(t2 >> 8) & 0xff] & 0x0000ff00) ^ + (SDRM_Td4[(t1) & 0xff] & 0x000000ff) ^ + rk[0]; + PUTUINT32(pt, s0); + s1 = + (SDRM_Td4[(t1 >> 24)] & 0xff000000) ^ + (SDRM_Td4[(t0 >> 16) & 0xff] & 0x00ff0000) ^ + (SDRM_Td4[(t3 >> 8) & 0xff] & 0x0000ff00) ^ + (SDRM_Td4[(t2) & 0xff] & 0x000000ff) ^ + rk[1]; PUTUINT32(pt + 4, s1); - s2 = - (SDRM_Td4[(t2 >> 24) ] & 0xff000000) ^ - (SDRM_Td4[(t1 >> 16) & 0xff] & 0x00ff0000) ^ - (SDRM_Td4[(t0 >> 8) & 0xff] & 0x0000ff00) ^ - (SDRM_Td4[(t3 ) & 0xff] & 0x000000ff) ^ - rk[2]; + s2 = + (SDRM_Td4[(t2 >> 24)] & 0xff000000) ^ + (SDRM_Td4[(t1 >> 16) & 0xff] & 0x00ff0000) ^ + (SDRM_Td4[(t0 >> 8) & 0xff] & 0x0000ff00) ^ + (SDRM_Td4[(t3) & 0xff] & 0x000000ff) ^ + rk[2]; PUTUINT32(pt + 8, s2); - s3 = - (SDRM_Td4[(t3 >> 24) ] & 0xff000000) ^ - (SDRM_Td4[(t2 >> 16) & 0xff] & 0x00ff0000) ^ - (SDRM_Td4[(t1 >> 8) & 0xff] & 0x0000ff00) ^ - (SDRM_Td4[(t0 ) & 0xff] & 0x000000ff) ^ - rk[3]; + s3 = + (SDRM_Td4[(t3 >> 24)] & 0xff000000) ^ + (SDRM_Td4[(t2 >> 16) & 0xff] & 0x00ff0000) ^ + (SDRM_Td4[(t1 >> 8) & 0xff] & 0x0000ff00) ^ + (SDRM_Td4[(t0) & 0xff] & 0x000000ff) ^ + rk[3]; PUTUINT32(pt + 12, s3); } /* - * @fn SDRM_AES128_Encryption - * @brief AES-128 Encryption + * @fn SDRM_AES128_Encryption + * @brief AES-128 Encryption * - * @param cipherText [out]encrypted text - * @param plainText [in]plain text - * @param UserKey [in]user key + * @param cipherText [out]encrypted text + * @param plainText [in]plain text + * @param UserKey [in]user key * - * @return CRYPTO_SUCCESS if success + * @return CRYPTO_SUCCESS if success */ -int SDRM_AES128_Encryption(cc_u8 *cipherText, cc_u8 *plainText, cc_u8 *UserKey) +int SDRM_AES128_Encryption(cc_u8 *cipherText, cc_u8 *plainText, cc_u8 *UserKey) { - cc_u32 RoundKey[4*(10 + 1)]; + cc_u32 RoundKey[4 * (10 + 1)]; SDRM_rijndaelKeySetupEnc(RoundKey, UserKey, 128); @@ -1282,39 +1403,39 @@ int SDRM_AES128_Encryption(cc_u8 *cipherText, cc_u8 *plainText, cc_u8 *UserKey) } /* - * @fn SDRM_AES128_Decryption - * @brief AES-128 Decryption + * @fn SDRM_AES128_Decryption + * @brief AES-128 Decryption * - * @param plainText [out]decrypted text - * @param cipherText [in]cipher text - * @param UserKey [in]user key + * @param plainText [out]decrypted text + * @param cipherText [in]cipher text + * @param UserKey [in]user key * - * @return CRYPTO_SUCCESS if success + * @return CRYPTO_SUCCESS if success */ -int SDRM_AES128_Decryption(cc_u8 *plainText, cc_u8 *cipherText, cc_u8 *UserKey) -{ - cc_u32 RoundKey[4*(10 + 1)]; +int SDRM_AES128_Decryption(cc_u8 *plainText, cc_u8 *cipherText, cc_u8 *UserKey) +{ + cc_u32 RoundKey[4 * (10 + 1)]; SDRM_rijndaelKeySetupDec(RoundKey, UserKey, 128); SDRM_rijndaelDecrypt(RoundKey, 10, cipherText, plainText); - return CRYPTO_SUCCESS; + return CRYPTO_SUCCESS; } /* - * @fn SDRM_AES192_Encryption - * @brief AES-192 Encryption + * @fn SDRM_AES192_Encryption + * @brief AES-192 Encryption * - * @param cipherText [out]encrypted text - * @param plainText [in]plain text - * @param UserKey [in]user key + * @param cipherText [out]encrypted text + * @param plainText [in]plain text + * @param UserKey [in]user key * - * @return CRYPTO_SUCCESS if success + * @return CRYPTO_SUCCESS if success */ -int SDRM_AES192_Encryption(cc_u8 *cipherText, cc_u8 *plainText, cc_u8 *UserKey) +int SDRM_AES192_Encryption(cc_u8 *cipherText, cc_u8 *plainText, cc_u8 *UserKey) { - cc_u32 RoundKey[4*(12 + 1)]; + cc_u32 RoundKey[4 * (12 + 1)]; SDRM_rijndaelKeySetupEnc(RoundKey, UserKey, 192); @@ -1324,39 +1445,39 @@ int SDRM_AES192_Encryption(cc_u8 *cipherText, cc_u8 *plainText, cc_u8 *UserKey) } /* - * @fn SDRM_AES192_Decryption - * @brief AES-192 Decryption + * @fn SDRM_AES192_Decryption + * @brief AES-192 Decryption * - * @param plainText [out]decrypted text - * @param cipherText [in]cipher text - * @param UserKey [in]user key + * @param plainText [out]decrypted text + * @param cipherText [in]cipher text + * @param UserKey [in]user key * - * @return CRYPTO_SUCCESS if success + * @return CRYPTO_SUCCESS if success */ -int SDRM_AES192_Decryption(cc_u8 *plainText, cc_u8 *cipherText, cc_u8 *UserKey) -{ - cc_u32 RoundKey[4*(12 + 1)]; +int SDRM_AES192_Decryption(cc_u8 *plainText, cc_u8 *cipherText, cc_u8 *UserKey) +{ + cc_u32 RoundKey[4 * (12 + 1)]; SDRM_rijndaelKeySetupDec(RoundKey, UserKey, 192); SDRM_rijndaelDecrypt(RoundKey, 12, cipherText, plainText); - return CRYPTO_SUCCESS; + return CRYPTO_SUCCESS; } /* - * @fn SDRM_AES256_Encryption - * @brief AES-256 Encryption + * @fn SDRM_AES256_Encryption + * @brief AES-256 Encryption * - * @param cipherText [out]encrypted text - * @param plainText [in]plain text - * @param UserKey [in]user key + * @param cipherText [out]encrypted text + * @param plainText [in]plain text + * @param UserKey [in]user key * - * @return CRYPTO_SUCCESS if success + * @return CRYPTO_SUCCESS if success */ -int SDRM_AES256_Encryption(cc_u8 *cipherText, cc_u8 *plainText, cc_u8 *UserKey) +int SDRM_AES256_Encryption(cc_u8 *cipherText, cc_u8 *plainText, cc_u8 *UserKey) { - cc_u32 RoundKey[4*(14 + 1)]; + cc_u32 RoundKey[4 * (14 + 1)]; SDRM_rijndaelKeySetupEnc(RoundKey, UserKey, 256); @@ -1366,24 +1487,24 @@ int SDRM_AES256_Encryption(cc_u8 *cipherText, cc_u8 *plainText, cc_u8 *UserKey) } /* - * @fn SDRM_AES256_Decryption - * @brief AES-256 Decryption + * @fn SDRM_AES256_Decryption + * @brief AES-256 Decryption * - * @param plainText [out]decrypted text - * @param cipherText [in]cipher text - * @param UserKey [in]user key + * @param plainText [out]decrypted text + * @param cipherText [in]cipher text + * @param UserKey [in]user key * - * @return CRYPTO_SUCCESS if success + * @return CRYPTO_SUCCESS if success */ -int SDRM_AES256_Decryption(cc_u8 *plainText, cc_u8 *cipherText, cc_u8 *UserKey) -{ - cc_u32 RoundKey[4*(14 + 1)]; +int SDRM_AES256_Decryption(cc_u8 *plainText, cc_u8 *cipherText, cc_u8 *UserKey) +{ + cc_u32 RoundKey[4 * (14 + 1)]; SDRM_rijndaelKeySetupDec(RoundKey, UserKey, 256); SDRM_rijndaelDecrypt(RoundKey, 14, cipherText, plainText); - return CRYPTO_SUCCESS; + return CRYPTO_SUCCESS; } -/***************************** End of File *****************************/ \ No newline at end of file +/***************************** End of File *****************************/ diff --git a/ssflib/dep/cryptocore/source/base/cc_bignum.c b/ssflib/dep/cryptocore/source/base/cc_bignum.c index 185b03c..2119c79 100644 --- a/ssflib/dep/cryptocore/source/base/cc_bignum.c +++ b/ssflib/dep/cryptocore/source/base/cc_bignum.c @@ -29,40 +29,41 @@ //////////////////////////////////////////////////////////////////////////// // Global Variables //////////////////////////////////////////////////////////////////////////// -cc_u32 DWD_Zero[2] = {0, 0}; -cc_u32 DWD_One[2] = {1, 0}; +cc_u32 DWD_Zero[2] = {0, 0}; +cc_u32 DWD_One[2] = {1, 0}; -SDRM_BIG_NUM _BN_Zero = {0, 0, 2, DWD_Zero}; -SDRM_BIG_NUM _BN_One = {0, 1, 2, DWD_One}; +SDRM_BIG_NUM _BN_Zero = {0, 0, 2, DWD_Zero}; +SDRM_BIG_NUM _BN_One = {0, 1, 2, DWD_One}; -SDRM_BIG_NUM *BN_Zero = &_BN_Zero; -SDRM_BIG_NUM *BN_One = &_BN_One; +SDRM_BIG_NUM *BN_Zero = &_BN_Zero; +SDRM_BIG_NUM *BN_One = &_BN_One; //////////////////////////////////////////////////////////////////////////// // Local Functon Protypes //////////////////////////////////////////////////////////////////////////// -int SDRM_DWD_Classical_REDC(cc_u32 *pdDest, cc_u32 DstLen, cc_u32 *pdModulus, cc_u32 ModLen); +int SDRM_DWD_Classical_REDC(cc_u32 *pdDest, cc_u32 DstLen, cc_u32 *pdModulus, + cc_u32 ModLen); #ifdef _OP64_NOTSUPPORTED -#define SDRM_HL(A) (cc_u32)(((A) >> 16) & 0xffffu) -#define SDRM_LL(A) (cc_u32)((A) & 0xffffu) -#define SDRM_LH(A) (cc_u32)(((A) & 0xffffu) << 16) -#define NOT(A) (~(A)) +#define SDRM_HL(A) (cc_u32)(((A) >> 16) & 0xffffu) +#define SDRM_LL(A) (cc_u32)((A) & 0xffffu) +#define SDRM_LH(A) (cc_u32)(((A) & 0xffffu) << 16) +#define NOT(A) (~(A)) /* - * @fn SDRM_DIGIT_Mul - * @brief Double-width UINT32 Multiplication + * @fn SDRM_DIGIT_Mul + * @brief Double-width UINT32 Multiplication * - * \n Dest [out]destination, 2-cc_u32-size array - * \n Src1 [in]first element - * \n Src2 [in]second element + * \n Dest [out]destination, 2-cc_u32-size array + * \n Src1 [in]first element + * \n Src2 [in]second element * - * @return void + * @return void */ void SDRM_DIGIT_Mul(cc_u32 *Dest, cc_u32 Src1, cc_u32 Src2) { - cc_u32 Da, Db, R1, R0; + cc_u32 Da, Db, R1, R0; R1 = SDRM_HL(Src1) * SDRM_HL(Src2); R0 = SDRM_LL(Src1) * SDRM_LL(Src2); @@ -70,12 +71,11 @@ void SDRM_DIGIT_Mul(cc_u32 *Dest, cc_u32 Src1, cc_u32 Src2) Da = SDRM_HL(Src1) * SDRM_LL(Src2); Db = SDRM_LL(Src1) * SDRM_HL(Src2); - if ((Db += Da) < Da) { + if ((Db += Da) < Da) R1 += ((cc_u32)1) << 16; - } - if ((R0 += SDRM_LH(Db)) < SDRM_LH(Db)) { + + if ((R0 += SDRM_LH(Db)) < SDRM_LH(Db)) R1++; - } Dest[0] = R0; Dest[1] = R1 + SDRM_HL(Db); @@ -84,58 +84,54 @@ void SDRM_DIGIT_Mul(cc_u32 *Dest, cc_u32 Src1, cc_u32 Src2) } /* - * @fn SDRM_DIGIT_Div - * @brief Doublue-width DWROD Division + * @fn SDRM_DIGIT_Div + * @brief Doublue-width DWROD Division * - * \n Src1 [in]upper-digit of dividend - * \n Src2 [in]lower-digit of dividend - * \n Div [in]divisor + * \n Src1 [in]upper-digit of dividend + * \n Src2 [in]lower-digit of dividend + * \n Div [in]divisor */ cc_u32 SDRM_DIGIT_Div(cc_u32 Src1, cc_u32 Src2, cc_u32 Div) { - cc_u32 Dq, Dr, Dx, Dy, Dt; + cc_u32 Dq, Dr, Dx, Dy, Dt; - // Estimate high half of quotient + // Estimate high half of quotient Dq = Src1 / (SDRM_HL(Div) + 1); - // Subtract the product of estimate and divisor from the dividend + // Subtract the product of estimate and divisor from the dividend Dr = Src1 - (SDRM_HL(Div) * Dq); Dx = SDRM_HL(Dr); Dy = SDRM_LH(Dr) + SDRM_HL(Src2); + if ((Dr = Dy - (SDRM_LL(Div) * Dq)) > Dy) - { Dx--; - } - // Correct estimate - while ((Dx > 0) || ((Dx == 0) && (Dr >= Div))) - { - if((Dr -= Div) > NOT(Div)) - { + // Correct estimate + while ((Dx > 0) || ((Dx == 0) && (Dr >= Div))) { + if ((Dr -= Div) > NOT(Div)) Dx--; - } + Dq++; } + Dt = SDRM_LH(Dq); - // Estimate low half of quotient + // Estimate low half of quotient Dq = Dr / (SDRM_HL(Div) + 1); - // Subtract the product of estimate and divisor from the dividend + // Subtract the product of estimate and divisor from the dividend Dr -= SDRM_HL(Div) * Dq; Dx = SDRM_HL(Dr); Dy = SDRM_LH(Dr) + SDRM_LL(Src2); - if ((Dr = Dy - (SDRM_LL(Div) * Dq)) > Dy) { + + if ((Dr = Dy - (SDRM_LL(Div) * Dq)) > Dy) Dx--; - } - // Correct estimate - while ((Dx > 0) || ((Dx == 0) && (Dr >= Div))) - { + // Correct estimate + while ((Dx > 0) || ((Dx == 0) && (Dr >= Div))) { if ((Dr -= Div) > NOT(Div)) - { Dx--; - } + Dq++; } @@ -143,57 +139,49 @@ cc_u32 SDRM_DIGIT_Div(cc_u32 Src1, cc_u32 Src2, cc_u32 Div) } /* - * @fn SDRM_DIGIT_Mod - * @brief Doublue-width DWROD Modular + * @fn SDRM_DIGIT_Mod + * @brief Doublue-width DWROD Modular * - * \n Src1 [in]upper-digit of dividend - * \n Src2 [in]lower-digit of dividend - * \n Div [in]divisor + * \n Src1 [in]upper-digit of dividend + * \n Src2 [in]lower-digit of dividend + * \n Div [in]divisor */ cc_u32 SDRM_DIGIT_Mod(cc_u32 Src1, cc_u32 Src2, cc_u32 Div) { - cc_u32 Dq, Dr, Dx, Dy; + cc_u32 Dq, Dr, Dx, Dy; - // Estimate high half of quotient + // Estimate high half of quotient Dq = Src1 / (SDRM_HL(Div) + 1); - // Subtract the from dividend the product of estimate and divisor + // Subtract the from dividend the product of estimate and divisor Dr = Src1 - (SDRM_HL(Div) * Dq); Dx = SDRM_HL(Dr); Dy = SDRM_LH(Dr) + SDRM_HL(Src2); + if ((Dr = Dy - (SDRM_LL(Div) * Dq)) > Dy) - { Dx--; - } - // Correct estimate - while ((Dx > 0) || ((Dx == 0) && (Dr >= Div))) - { + // Correct estimate + while ((Dx > 0) || ((Dx == 0) && (Dr >= Div))) { if ((Dr -= Div) > NOT(Div)) - { Dx--; - } } - // Estimate low half of quotient + // Estimate low half of quotient Dq = Dr / (SDRM_HL(Div) + 1); - // Subtract the from dividend the product of estimate and divisor + // Subtract the from dividend the product of estimate and divisor Dr -= SDRM_HL(Div) * Dq; Dx = SDRM_HL(Dr); Dy = SDRM_LH(Dr) + SDRM_LL(Src2); + if ((Dr = Dy - (SDRM_LL(Div) * Dq)) > Dy) - { Dx--; - } - // Correct estimate - while ((Dx > 0) || ((Dx == 0) && (Dr >= Div))) - { - if ((Dr -= Div) > NOT(Div) ) - { + // Correct estimate + while ((Dx > 0) || ((Dx == 0) && (Dr >= Div))) { + if ((Dr -= Div) > NOT(Div)) Dx--; - } } return Dr; @@ -202,61 +190,47 @@ cc_u32 SDRM_DIGIT_Mod(cc_u32 Src1, cc_u32 Src2, cc_u32 Div) #endif //_OP64_NOTSUPPORTED /* - * @fn SDRM_DWD_Cmp - * @brief cc_u32 Array Comparison + * @fn SDRM_DWD_Cmp + * @brief cc_u32 Array Comparison * - * @param pdSrc1 [in]first element - * @param dSrcLen1 [in]legnth of pdSrc1 - * @param pdSrc2 [in]second element - * @param dSrcLen2 [in]legnth of pdSrc2 + * @param pdSrc1 [in]first element + * @param dSrcLen1 [in]legnth of pdSrc1 + * @param pdSrc2 [in]second element + * @param dSrcLen2 [in]legnth of pdSrc2 * - * @return 1 if pdSrc1 is larger than pdSrc2 - * \n 0 if same - * \n -1 if pdSrc2 is larger than pdSrc1 + * @return 1 if pdSrc1 is larger than pdSrc2 + * \n 0 if same + * \n -1 if pdSrc2 is larger than pdSrc1 */ -static int SDRM_DWD_Cmp(cc_u32 *pdSrc1, cc_u32 dSrcLen1, cc_u32 *pdSrc2, cc_u32 dSrcLen2) +static int SDRM_DWD_Cmp(cc_u32 *pdSrc1, cc_u32 dSrcLen1, cc_u32 *pdSrc2, + cc_u32 dSrcLen2) { - cc_u32 i; + cc_u32 i; //When the length is different - if (dSrcLen1 >= dSrcLen2) - { - for (i = dSrcLen1 - 1; i != dSrcLen2 - 1; i--) - { + if (dSrcLen1 >= dSrcLen2) { + for (i = dSrcLen1 - 1; i != dSrcLen2 - 1; i--) { if (pdSrc1[i]) - { return +1; - } } - } - else - { - for (i = dSrcLen2 - 1; i != dSrcLen1 - 1; i--) - { + } else { + for (i = dSrcLen2 - 1; i != dSrcLen1 - 1; i--) { if (pdSrc2[i]) - { return -1; - } } } //Compare common digits - for (; i != (cc_u32)-1; i--) - { + for (; i != (cc_u32) - 1; i--) { if (pdSrc1[i] == pdSrc2[i]) - { continue; - } - else - { + + else { if (pdSrc1[i] > pdSrc2[i]) - { return +1; - } + else - { return -1; - } } } @@ -264,32 +238,32 @@ static int SDRM_DWD_Cmp(cc_u32 *pdSrc1, cc_u32 dSrcLen1, cc_u32 *pdSrc2, cc_u32 } /* - * @fn SDRM_DWD_SHL - * @brief Shift left the digit array + * @fn SDRM_DWD_SHL + * @brief Shift left the digit array * - * @param pdDest [out]destination - * @param pdSrc [in]source - * @param dSrcLen [in]legnth of pdSrc - * @param dNumOfShift [in]shift amount + * @param pdDest [out]destination + * @param pdSrc [in]source + * @param dSrcLen [in]legnth of pdSrc + * @param dNumOfShift [in]shift amount * - * @return carry + * @return carry */ -static cc_u32 SDRM_DWD_SHL(cc_u32 *pdDest, cc_u32 *pdSrc, cc_u32 dSrcLen, cc_u32 dNumOfShift) +static cc_u32 SDRM_DWD_SHL(cc_u32 *pdDest, cc_u32 *pdSrc, cc_u32 dSrcLen, + cc_u32 dNumOfShift) { - cc_u32 i = dSrcLen - 1; - cc_u32 dRet; + cc_u32 i = dSrcLen - 1; + cc_u32 dRet; - if (dSrcLen == 0) - { + if (dSrcLen == 0) { *pdDest = 0; return 0; } dRet = pdSrc[i] >> (SDRM_BitsInDWORD - dNumOfShift); - for (; i != 0; i--) - { - pdDest[i] = (pdSrc[i] << dNumOfShift) ^ (pdSrc[i - 1] >> (SDRM_BitsInDWORD - dNumOfShift)); + for (; i != 0; i--) { + pdDest[i] = (pdSrc[i] << dNumOfShift) ^ (pdSrc[i - 1] >> + (SDRM_BitsInDWORD - dNumOfShift)); } pdDest[i] = pdSrc[i] << dNumOfShift; @@ -298,26 +272,27 @@ static cc_u32 SDRM_DWD_SHL(cc_u32 *pdDest, cc_u32 *pdSrc, cc_u32 dSrcLen, cc_u32 } /* - * @fn SDRM_DWD_SHR - * @brief Shift right the digit array + * @fn SDRM_DWD_SHR + * @brief Shift right the digit array * - * @param pdDest [out]destination - * @param pdSrc [in]source - * @param dSrcLen [in]legnth of pdSrc - * @param dNumOfShift [in]shift amount + * @param pdDest [out]destination + * @param pdSrc [in]source + * @param dSrcLen [in]legnth of pdSrc + * @param dNumOfShift [in]shift amount * - * @return carry + * @return carry */ -static cc_u32 SDRM_DWD_SHR(cc_u32 *pdDest, cc_u32 *pdSrc, cc_u32 dSrcLen, cc_u32 dNumOfShift) +static cc_u32 SDRM_DWD_SHR(cc_u32 *pdDest, cc_u32 *pdSrc, cc_u32 dSrcLen, + cc_u32 dNumOfShift) { cc_u32 i = 0; cc_u32 dRet; dRet = pdSrc[i] << (SDRM_BitsInDWORD - dNumOfShift); - for (; i < dSrcLen - 1; i++) - { - pdDest[i] = (pdSrc[i] >> dNumOfShift) ^ (pdSrc[i + 1] << (SDRM_BitsInDWORD - dNumOfShift)); + for (; i < dSrcLen - 1; i++) { + pdDest[i] = (pdSrc[i] >> dNumOfShift) ^ (pdSrc[i + 1] << + (SDRM_BitsInDWORD - dNumOfShift)); } pdDest[i] = pdSrc[i] >> dNumOfShift; @@ -326,56 +301,47 @@ static cc_u32 SDRM_DWD_SHR(cc_u32 *pdDest, cc_u32 *pdSrc, cc_u32 dSrcLen, cc_u32 } /* - * @fn SDRM_DWD_Add - * @brief Add two digit array + * @fn SDRM_DWD_Add + * @brief Add two digit array * - * @param pdDest [out]destination - * @param pdSrc1 [in]first element - * @param dSrcLen1 [in]legnth of pdSrc1 - * @param pdSrc2 [in]second element - * @param dSrcLen2 [in]legnth of pdSrc2 + * @param pdDest [out]destination + * @param pdSrc1 [in]first element + * @param dSrcLen1 [in]legnth of pdSrc1 + * @param pdSrc2 [in]second element + * @param dSrcLen2 [in]legnth of pdSrc2 * - * @return carry + * @return carry */ -static cc_u32 SDRM_DWD_Add(cc_u32 *pdDest, cc_u32 *pdSrc1, cc_u32 dSrcLen1, cc_u32 *pdSrc2, cc_u32 dSrcLen2) +static cc_u32 SDRM_DWD_Add(cc_u32 *pdDest, cc_u32 *pdSrc1, cc_u32 dSrcLen1, + cc_u32 *pdSrc2, cc_u32 dSrcLen2) { - cc_u32 i; - cc_u32 dCarry = 0, dTemp; + cc_u32 i; + cc_u32 dCarry = 0, dTemp; //add low digits - for (i = 0; i < dSrcLen2; i++) - { - if ((pdSrc2[i] == ((cc_u32)-1)) && (dCarry)) - { + for (i = 0; i < dSrcLen2; i++) { + if ((pdSrc2[i] == ((cc_u32) - 1)) && (dCarry)) pdDest[i] = pdSrc1[i]; - } - else - { + + else { dTemp = pdSrc2[i] + dCarry; pdDest[i] = pdSrc1[i] + dTemp; - dCarry = (pdDest[i] < dTemp ) ? 1 : 0; + dCarry = (pdDest[i] < dTemp) ? 1 : 0; } } //copy high digits if (dSrcLen1 > i) - { memcpy(pdDest + i, pdSrc1 + i, (dSrcLen1 - i) * SDRM_SIZE_OF_DWORD); - } //process carry if (!dCarry) - { return 0; - } - else - { - for (i = dSrcLen2; i < dSrcLen1; i++) - { + + else { + for (i = dSrcLen2; i < dSrcLen1; i++) { if (++pdDest[i]) - { return 0; - } } } @@ -383,31 +349,29 @@ static cc_u32 SDRM_DWD_Add(cc_u32 *pdDest, cc_u32 *pdSrc1, cc_u32 dSrcLen1, cc_u } /* - * @fn SDRM_DWD_Sub - * @brief subtract digit array + * @fn SDRM_DWD_Sub + * @brief subtract digit array * - * @param pdDest [out]destination - * @param pdSrc1 [in]first element - * @param dSrcLen1 [in]legnth of pdSrc1 - * @param pdSrc2 [in]second element - * @param dSrcLen2 [in]legnth of pdSrc2 + * @param pdDest [out]destination + * @param pdSrc1 [in]first element + * @param dSrcLen1 [in]legnth of pdSrc1 + * @param pdSrc2 [in]second element + * @param dSrcLen2 [in]legnth of pdSrc2 * - * @return carry + * @return carry */ -static cc_u32 SDRM_DWD_Sub(cc_u32 *pdDest, cc_u32 *pdSrc1, cc_u32 dSrcLen1, cc_u32 *pdSrc2, cc_u32 dSrcLen2) +static cc_u32 SDRM_DWD_Sub(cc_u32 *pdDest, cc_u32 *pdSrc1, cc_u32 dSrcLen1, + cc_u32 *pdSrc2, cc_u32 dSrcLen2) { - cc_u32 i; - cc_u32 dCarry = 0, dTemp; + cc_u32 i; + cc_u32 dCarry = 0, dTemp; //subtract low digits - for (i = 0; i < dSrcLen2; i++) - { + for (i = 0; i < dSrcLen2; i++) { if (pdSrc2[i] + dCarry == 0) - { pdDest[i] = pdSrc1[i]; - } - else - { + + else { dTemp = pdSrc2[i] + dCarry; pdDest[i] = pdSrc1[i] - dTemp; dCarry = ((pdDest[i]) > ~(dTemp)) ? 1 : 0; @@ -416,23 +380,16 @@ static cc_u32 SDRM_DWD_Sub(cc_u32 *pdDest, cc_u32 *pdSrc1, cc_u32 dSrcLen1, cc_u //copy high digits if (dSrcLen1 > i) - { memcpy(pdDest + i, pdSrc1 + i, (dSrcLen1 - i) * SDRM_SIZE_OF_DWORD); - } //process carry if (!dCarry) - { return 0; - } - else - { - for (i = dSrcLen2 ; i < dSrcLen1; i++) - { + + else { + for (i = dSrcLen2 ; i < dSrcLen1; i++) { if (pdDest[i]--) - { return 0; - } } } @@ -440,348 +397,308 @@ static cc_u32 SDRM_DWD_Sub(cc_u32 *pdDest, cc_u32 *pdSrc1, cc_u32 dSrcLen1, cc_u } /* - * @fn SDRM_DWD_MulAdd - * @brief Add multiple + * @fn SDRM_DWD_MulAdd + * @brief Add multiple * - * @param pdDest [out]destination - * @param dDstLen [in]legnth of pbDest - * @param pdSrc [in]source - * @param dSrcLen [in]legnth of pdSrc - * @param dMultiplier [in]multiplier + * @param pdDest [out]destination + * @param dDstLen [in]legnth of pbDest + * @param pdSrc [in]source + * @param dSrcLen [in]legnth of pdSrc + * @param dMultiplier [in]multiplier * - * @return carry + * @return carry */ -static cc_u32 SDRM_DWD_MulAdd(cc_u32 *pdDest, cc_u32 dDstLen, cc_u32 *pdSrc, cc_u32 dSrcLen, cc_u32 dMultiplier) +static cc_u32 SDRM_DWD_MulAdd(cc_u32 *pdDest, cc_u32 dDstLen, cc_u32 *pdSrc, + cc_u32 dSrcLen, cc_u32 dMultiplier) { cc_u32 i; cc_u32 dTemp = 0; cc_u64 pdDigit2; //Multiplication part - for (i = 0; i < dSrcLen; i++) - { + for (i = 0; i < dSrcLen; i++) { /* * Time code optimization. To be continue! */ - pdDigit2 =((cc_u64)pdSrc[i] * dMultiplier) + pdDest[i] + dTemp; - pdDest[i] = (cc_u32)pdDigit2; - dTemp = pdDigit2 >> 32; + pdDigit2 = ((cc_u64)pdSrc[i] * dMultiplier) + pdDest[i] + dTemp; + pdDest[i] = (cc_u32)pdDigit2; + dTemp = pdDigit2 >> 32; /*SDRM_DIGIT_Mul(pdDigit, dMultiplier, pdSrc[i]); if ((dTemp += pdDigit[0]) < pdDigit[0]) { - pdDigit[1]++; + pdDigit[1]++; } if ((pdDest[i] += dTemp) < dTemp) { - pdDigit[1]++; + pdDigit[1]++; } dTemp = pdDigit[1];*/ } if (i == dDstLen) - { return dTemp; - } //process top digit if ((pdDest[i] += dTemp) >= dTemp) - { return 0; - } - for (i++; i < dDstLen; i++) - { + for (i++; i < dDstLen; i++) { if ((++pdDest[i]) != 0) - { return 0; - } } return 1; } /* - * @fn SDRM_DWD_MulSub - * @brief Multiply and Subtract Digit Array + * @fn SDRM_DWD_MulSub + * @brief Multiply and Subtract Digit Array * - * @param pdDest [out]destination - * @param dDstLen [in]legnth of pbDest - * @param pdSrc [in]source - * @param dSrcLen [in]legnth of pdSrc - * @param dMultiplier [in]multiplier + * @param pdDest [out]destination + * @param dDstLen [in]legnth of pbDest + * @param pdSrc [in]source + * @param dSrcLen [in]legnth of pdSrc + * @param dMultiplier [in]multiplier * - * @return carry + * @return carry */ -static cc_u32 SDRM_DWD_MulSub(cc_u32 *pdDest, cc_u32 dDstLen, cc_u32 *pdSrc, cc_u32 dSrcLen, cc_u32 dMultiplier) +static cc_u32 SDRM_DWD_MulSub(cc_u32 *pdDest, cc_u32 dDstLen, cc_u32 *pdSrc, + cc_u32 dSrcLen, cc_u32 dMultiplier) { - cc_u32 i; - cc_u32 pdDigit[2], dTemp = 0; + cc_u32 i; + cc_u32 pdDigit[2], dTemp = 0; //Multiplication part - for (i = 0; i < dSrcLen; i++) - { + for (i = 0; i < dSrcLen; i++) { SDRM_DIGIT_Mul(pdDigit, dMultiplier, pdSrc[i]); dTemp += pdDigit[0]; if (dTemp < pdDigit[0]) - { pdDigit[1]++; - } if (pdDest[i] < dTemp) - { pdDigit[1]++; - } pdDest[i] -= dTemp; dTemp = pdDigit[1]; } if (i == dDstLen) - { return dTemp; - } //process top digit - if (pdDest[i] >= dTemp) - { + if (pdDest[i] >= dTemp) { pdDest[i] -= dTemp; return 0; - } - else - { + } else pdDest[i] -= dTemp; - } - for (i++; i < dDstLen; i++) - { + for (i++; i < dDstLen; i++) { if ((pdDest[i]--) != 0) - { return 0; - } } return 1; } /* - * @fn SDRM_DWD_Mul - * @brief Multiply tow Digit array + * @fn SDRM_DWD_Mul + * @brief Multiply tow Digit array * - * @param pdDest [out]destination - * @param pdSrc1 [in]first element - * @param dSrcLen1 [in]legnth of pdSrc1 - * @param pdSrc2 [in]second element - * @param dSrcLen2 [in]legnth of pdSrc2 + * @param pdDest [out]destination + * @param pdSrc1 [in]first element + * @param dSrcLen1 [in]legnth of pdSrc1 + * @param pdSrc2 [in]second element + * @param dSrcLen2 [in]legnth of pdSrc2 * - * @return void + * @return void */ -static void SDRM_DWD_Mul(cc_u32 *pdDest, cc_u32 *pdSrc1, cc_u32 dSrcLen1, cc_u32 *pdSrc2, cc_u32 dSrcLen2) +static void SDRM_DWD_Mul(cc_u32 *pdDest, cc_u32 *pdSrc1, cc_u32 dSrcLen1, + cc_u32 *pdSrc2, cc_u32 dSrcLen2) { - cc_u32 i, j; - cc_u32 dTemp; + cc_u32 i, j; + cc_u32 dTemp; cc_u64 pdDigit2; memset(pdDest, 0, (dSrcLen1 + dSrcLen2) * SDRM_SIZE_OF_DWORD); - for (j = 0; j < dSrcLen2; j++) - { + for (j = 0; j < dSrcLen2; j++) { dTemp = 0; - for (i = 0; i < dSrcLen1; i++) - { - pdDigit2 =((cc_u64)pdSrc1[i] * pdSrc2[j]) + pdDest[i + j] + dTemp; - pdDest[i + j] = (cc_u32)pdDigit2; - dTemp = pdDigit2 >> 32; + for (i = 0; i < dSrcLen1; i++) { + + pdDigit2 = ((cc_u64)pdSrc1[i] * pdSrc2[j]) + pdDest[i + j] + dTemp; + pdDest[i + j] = (cc_u32)pdDigit2; + dTemp = pdDigit2 >> 32; /*SDRM_DIGIT_Mul(pdDigit, pdSrc1[i], pdSrc2[j]); if ((dTemp += pdDigit[0]) < pdDigit[0]) { - pdDigit[1]++; + pdDigit[1]++; } if ((pdDest[i + j] += dTemp) < dTemp) { - pdDigit[1]++; + pdDigit[1]++; } dTemp = pdDigit[1];*/ } + pdDest[i + j] = dTemp; } } /* - * @fn SDRM_DWD_Div - * @brief Multiply tow Digit array - * - * @param pdDest [out]quotient - * @param pdRem [out]remainder - * @param pdSrc1 [in]divisor - * @param dSrcLen1 [in]legnth of pdSrc1 - * @param pdSrc2 [in]dividend - * @param dSrcLen2 [in]legnth of pdSrc2 - * - * @return 0 if reaminder is zero - * \n 1 otherwise + * @fn SDRM_DWD_Div + * @brief Multiply tow Digit array + * + * @param pdDest [out]quotient + * @param pdRem [out]remainder + * @param pdSrc1 [in]divisor + * @param dSrcLen1 [in]legnth of pdSrc1 + * @param pdSrc2 [in]dividend + * @param dSrcLen2 [in]legnth of pdSrc2 + * + * @return 0 if reaminder is zero + * \n 1 otherwise */ static cc_u32 SDRM_DWD_Div(cc_u32 *pdDest, cc_u32 *pdRem, cc_u32 *pdSrc1, cc_u32 dSrcLen1, cc_u32 *pdSrc2, cc_u32 dSrcLen2) { - cc_u32 i, q, c, dNum_of_Shift = 0; - cc_u32 *C = (cc_u32*)malloc(SDRM_SIZE_OF_DWORD * 2 * (MAX2(dSrcLen1, dSrcLen2) + 2)); + cc_u32 i, q, c, dNum_of_Shift = 0; + cc_u32 *C = (cc_u32 *)malloc(SDRM_SIZE_OF_DWORD * 2 * (MAX2(dSrcLen1, + dSrcLen2) + 2)); if (!C) - { return (cc_u32)CRYPTO_MEMORY_ALLOC_FAIL; - } SDRM_DWD_Copy(C, pdSrc1, dSrcLen1); C[dSrcLen1] = 0; c = dSrcLen1 + 1; //Remove lowest '0's - for (i = dSrcLen2 * SDRM_BitsInDWORD-1; !SDRM_CheckBitUINT32(pdSrc2, i); i--, dNum_of_Shift++); + for (i = dSrcLen2 * SDRM_BitsInDWORD - 1; !SDRM_CheckBitUINT32(pdSrc2, i); + i--, dNum_of_Shift++); - if (dNum_of_Shift) - { + if (dNum_of_Shift) { SDRM_DWD_SHL(C, C, c, dNum_of_Shift); SDRM_DWD_SHL(pdSrc2, pdSrc2, dSrcLen2, dNum_of_Shift); } - for (i = c-dSrcLen2 - 1; i != (cc_u32)-1; i--) - { - if (C[dSrcLen2 + i]==pdSrc2[dSrcLen2 - 1] ) - { - q = (cc_u32)-1; - } + for (i = c - dSrcLen2 - 1; i != (cc_u32) - 1; i--) { + if (C[dSrcLen2 + i] == pdSrc2[dSrcLen2 - 1]) + q = (cc_u32) - 1; + else - { q = SDRM_DIGIT_Div(C[dSrcLen2 + i], C[dSrcLen2 + i - 1], pdSrc2[dSrcLen2 - 1]); - } - if (SDRM_DWD_MulSub(C + i, dSrcLen2 + 1, pdSrc2, dSrcLen2, q) ) - { + if (SDRM_DWD_MulSub(C + i, dSrcLen2 + 1, pdSrc2, dSrcLen2, q)) { q--; - if (!SDRM_DWD_Add(C + i, C + i, dSrcLen2 + 1, pdSrc2, dSrcLen2)) - { + + if (!SDRM_DWD_Add(C + i, C + i, dSrcLen2 + 1, pdSrc2, dSrcLen2)) { q--; SDRM_DWD_Add(C + i, C + i, dSrcLen2 + 1, pdSrc2, dSrcLen2); } } + pdDest[i] = q; } //Recover lowest '0's - if (dNum_of_Shift) - { + if (dNum_of_Shift) { SDRM_DWD_SHR(pdSrc2, pdSrc2, dSrcLen2, dNum_of_Shift); SDRM_DWD_SHR(C, C, dSrcLen2, dNum_of_Shift); } if (pdRem) - { SDRM_DWD_Copy(pdRem, C, dSrcLen2); - } - for (i = 0; i < c; i++) - { - if (C[i]) - { + for (i = 0; i < c; i++) { + if (C[i]) { free(C); return 1; } } + free(C); return 0; } /* - * @fn SDRM_DWD_Classical_REDC - * @brief Classical Modular Reduction Algorithm + * @fn SDRM_DWD_Classical_REDC + * @brief Classical Modular Reduction Algorithm * - * @param pdDest [out]destination - * @param DstLen [in]length of pdDest - * @param pdModulus [in]modulus - * @param ModLen [in]legnth of pdModulus + * @param pdDest [out]destination + * @param DstLen [in]length of pdDest + * @param pdModulus [in]modulus + * @param ModLen [in]legnth of pdModulus * - * @return CRYPTO_SUCCESS if no error is occured + * @return CRYPTO_SUCCESS if no error is occured */ -int SDRM_DWD_Classical_REDC(cc_u32 *pdDest, cc_u32 DstLen, cc_u32 *pdModulus, cc_u32 ModLen) +int SDRM_DWD_Classical_REDC(cc_u32 *pdDest, cc_u32 DstLen, cc_u32 *pdModulus, + cc_u32 ModLen) { - cc_u32 i; - cc_u32 MSB=0, TTTT=0, FLAG=0, D_Quotient, MSD_Modulus; + cc_u32 i; + cc_u32 MSB = 0, TTTT = 0, FLAG = 0, D_Quotient, MSD_Modulus; if (DstLen < ModLen) - { return CRYPTO_SUCCESS; - } - if (pdDest[DstLen - 1] >= pdModulus[ModLen - 1]) - { + if (pdDest[DstLen - 1] >= pdModulus[ModLen - 1]) { FLAG++; TTTT = pdDest[DstLen]; pdDest[DstLen++] = 0; } - for (i = SDRM_BitsInDWORD - 1; i != (cc_u32)-1; i--) - { + for (i = SDRM_BitsInDWORD - 1; i != (cc_u32) - 1; i--) { if (pdModulus[ModLen - 1] & ((cc_u32)1 << i)) - { break; - } MSB++; } - if (MSB) - { + if (MSB) { SDRM_DWD_SHL(pdModulus, pdModulus, ModLen, MSB); SDRM_DWD_SHL(pdDest, pdDest, DstLen, MSB); } - // Step 2 : main part + // Step 2 : main part MSD_Modulus = pdModulus[ModLen - 1]; - for (i = DstLen - ModLen - 1; i != (cc_u32)-1; i--) - { - // Step 2-1 : Estimate D_Quotient + + for (i = DstLen - ModLen - 1; i != (cc_u32) - 1; i--) { + // Step 2-1 : Estimate D_Quotient if (pdDest[ModLen + i] == MSD_Modulus) - { - D_Quotient = (cc_u32)-1; - } - else - { - D_Quotient = SDRM_DIGIT_Div(pdDest[ModLen + i], pdDest[ModLen + i - 1], MSD_Modulus); + D_Quotient = (cc_u32) - 1; + + else { + D_Quotient = SDRM_DIGIT_Div(pdDest[ModLen + i], pdDest[ModLen + i - 1], + MSD_Modulus); } - // Step 2-2 : Make pdDest <- pdDest-D_Quotient*pdModulus - if (SDRM_DWD_MulSub(pdDest + i, ModLen + 1, pdModulus, ModLen, D_Quotient) ) - { + // Step 2-2 : Make pdDest <- pdDest-D_Quotient*pdModulus + if (SDRM_DWD_MulSub(pdDest + i, ModLen + 1, pdModulus, ModLen, D_Quotient)) { if (SDRM_DWD_Add(pdDest + i, pdDest + i, ModLen + 1, pdModulus, ModLen) == 0) - { SDRM_DWD_Add(pdDest + i, pdDest + i, ModLen + 1, pdModulus, ModLen); - } } } - // Step 4 : inverse part of Step 2 - if (MSB) - { + // Step 4 : inverse part of Step 2 + if (MSB) { SDRM_DWD_SHR(pdModulus, pdModulus, ModLen, MSB); SDRM_DWD_SHR(pdDest, pdDest, ModLen, MSB); } - // Step 4.5 : inverse part of Step 1.5 - if (FLAG) - { + // Step 4.5 : inverse part of Step 1.5 + if (FLAG) { DstLen--; pdDest[DstLen] = TTTT; } @@ -790,27 +707,25 @@ int SDRM_DWD_Classical_REDC(cc_u32 *pdDest, cc_u32 DstLen, cc_u32 *pdModulus, cc } /* - * @fn SDRM_DIGIT_Gcd - * @brief get gcd of two digits + * @fn SDRM_DIGIT_Gcd + * @brief get gcd of two digits * - * @param s1 [in]first element - * @param s2 [in]second element + * @param s1 [in]first element + * @param s2 [in]second element * - * @return gcd + * @return gcd */ cc_u32 SDRM_DIGIT_Gcd(cc_u32 s1, cc_u32 s2) - { +{ cc_u32 dTemp; - if (s1 < s2) - { + if (s1 < s2) { dTemp = s1; s1 = s2; s2 = dTemp; } - while(s2) - { + while (s2) { dTemp = s1 % s2; s1 = s2; s2 = dTemp; @@ -820,28 +735,27 @@ cc_u32 SDRM_DIGIT_Gcd(cc_u32 s1, cc_u32 s2) } /* - * @fn SDRM_PrintBN - * @brief Show out a Big Number + * @fn SDRM_PrintBN + * @brief Show out a Big Number * - * @param level [in]log level - * @param s [in]title - * @param bn [in]big number to show out + * @param level [in]log level + * @param s [in]title + * @param bn [in]big number to show out * - * @return void + * @return void */ -void SDRM_PrintBN(const char* s, SDRM_BIG_NUM* bn) +void SDRM_PrintBN(const char *s, SDRM_BIG_NUM *bn) { cc_u32 i; - if(bn->sign == 0) { + + if (bn->sign == 0) printf("%15s %d :", s, (int)(bn->Length)); - } - else { + + else printf("%15s-%d :", s, (int)(bn->Length)); - } + for (i = 0; i < bn->Length ; i++) - { - printf("%08x ", (int)(bn->pData[bn->Length - i -1])); - } + printf("%08x ", (int)(bn->pData[bn->Length - i - 1])); printf("\n"); @@ -849,82 +763,69 @@ void SDRM_PrintBN(const char* s, SDRM_BIG_NUM* bn) } /* - * @fn SDRM_BN2OS - * @brief Convert Big Number to Octet String + * @fn SDRM_BN2OS + * @brief Convert Big Number to Octet String * - * @param BN_Src [in]source integer - * @param dDstLen [in]Byte-length of pbDst - * @param pbDst [out]output octet string + * @param BN_Src [in]source integer + * @param dDstLen [in]Byte-length of pbDst + * @param pbDst [out]output octet string * - * @return CRYPTO_SUCCESS if no error is occured - * \n CRYPTO_MEMORY_ALLOC_FAIL if arrary is too small + * @return CRYPTO_SUCCESS if no error is occured + * \n CRYPTO_MEMORY_ALLOC_FAIL if arrary is too small */ -int SDRM_BN2OS(SDRM_BIG_NUM *BN_Src, cc_u32 dDstLen, cc_u8 *pbDst) +int SDRM_BN2OS(SDRM_BIG_NUM *BN_Src, cc_u32 dDstLen, cc_u8 *pbDst) { - cc_u32 i; + cc_u32 i; if ((BN_Src == NULL) || (pbDst == NULL)) - { return CRYPTO_NULL_POINTER; - } SDRM_BN_OPTIMIZE_LENGTH(BN_Src); - if (BN_Src->sign) - { + if (BN_Src->sign) { pbDst[0] = '-'; dDstLen += 1; } - if ((SDRM_SIZE_OF_DWORD * BN_Src->Length) <= dDstLen) - { + if ((SDRM_SIZE_OF_DWORD * BN_Src->Length) <= dDstLen) { memset(pbDst, 0, dDstLen); - for (i = 0; (dDstLen != 0) && (i < BN_Src->Length); i++) - { - pbDst[--dDstLen] = (cc_u8)((BN_Src->pData[i] ) & 0xff); - pbDst[--dDstLen] = (cc_u8)((BN_Src->pData[i]>> 8) & 0xff); - pbDst[--dDstLen] = (cc_u8)((BN_Src->pData[i]>>16) & 0xff); - pbDst[--dDstLen] = (cc_u8)((BN_Src->pData[i]>>24) & 0xff); + for (i = 0; (dDstLen != 0) && (i < BN_Src->Length); i++) { + pbDst[--dDstLen] = (cc_u8)((BN_Src->pData[i]) & 0xff); + pbDst[--dDstLen] = (cc_u8)((BN_Src->pData[i] >> 8) & 0xff); + pbDst[--dDstLen] = (cc_u8)((BN_Src->pData[i] >> 16) & 0xff); + pbDst[--dDstLen] = (cc_u8)((BN_Src->pData[i] >> 24) & 0xff); } - } - else - { + } else { i = (SDRM_SIZE_OF_DWORD * BN_Src->Length) - dDstLen; + if (i >= SDRM_SIZE_OF_DWORD) - { return CRYPTO_BUFFER_TOO_SMALL; - } - else if ( BN_Src->pData[BN_Src->Length - 1] >> (8 * (SDRM_SIZE_OF_DWORD - i))) - { + + else if (BN_Src->pData[BN_Src->Length - 1] >> (8 * (SDRM_SIZE_OF_DWORD - + i))) return CRYPTO_BUFFER_TOO_SMALL; - } - for (i = 0;; i++) - { - pbDst[--dDstLen] = (cc_u8)((BN_Src->pData[i] ) & 0xff); + for (i = 0;; i++) { + pbDst[--dDstLen] = (cc_u8)((BN_Src->pData[i]) & 0xff); + if (dDstLen == 0) - { break; - } - pbDst[--dDstLen] = (cc_u8)((BN_Src->pData[i]>> 8) & 0xFF); + pbDst[--dDstLen] = (cc_u8)((BN_Src->pData[i] >> 8) & 0xFF); + if (dDstLen == 0) - { break; - } - pbDst[--dDstLen] = (cc_u8)((BN_Src->pData[i]>>16) & 0xFF); + pbDst[--dDstLen] = (cc_u8)((BN_Src->pData[i] >> 16) & 0xFF); + if (dDstLen == 0) - { break; - } - pbDst[--dDstLen] = (cc_u8)((BN_Src->pData[i]>>24) & 0xFF); + pbDst[--dDstLen] = (cc_u8)((BN_Src->pData[i] >> 24) & 0xFF); + if (dDstLen == 0) - { break; - } } } @@ -932,80 +833,74 @@ int SDRM_BN2OS(SDRM_BIG_NUM *BN_Src, cc_u32 dDstLen, cc_u8 *pbDst) } /* - * @fn SDRM_OS2BN - * @brief Convert Octet String to Big Number + * @fn SDRM_OS2BN + * @brief Convert Octet String to Big Number * - * @param pbSrc [in]source octet string - * @param dSrcLen [in]Byte-length of pbSrc - * @param BN_Dst [out]output big number + * @param pbSrc [in]source octet string + * @param dSrcLen [in]Byte-length of pbSrc + * @param BN_Dst [out]output big number * - * @return CRYPTO_SUCCESS if no error is occured - * \n CRYPTO_MEMORY_ALLOC_FAIL if arrary is too small + * @return CRYPTO_SUCCESS if no error is occured + * \n CRYPTO_MEMORY_ALLOC_FAIL if arrary is too small */ -int SDRM_OS2BN(cc_u8 *pbSrc, cc_u32 dSrcLen, SDRM_BIG_NUM *BN_Dst) +int SDRM_OS2BN(cc_u8 *pbSrc, cc_u32 dSrcLen, SDRM_BIG_NUM *BN_Dst) { - cc_u32 i; - int ret; + cc_u32 i; + int ret; if ((pbSrc == NULL) || (BN_Dst == NULL)) - { return CRYPTO_INVALID_ARGUMENT; - } SDRM_BN_Clr(BN_Dst); - for (i = 0; i < dSrcLen; i++) - { + for (i = 0; i < dSrcLen; i++) { ret = SDRM_BN_SHL(BN_Dst, BN_Dst, 8); if (ret != CRYPTO_SUCCESS) - { return ret; - } BN_Dst->pData[0] ^= pbSrc[i]; - if (BN_Dst->Length == 0) { + + if (BN_Dst->Length == 0) BN_Dst->Length = 1; - } } return CRYPTO_SUCCESS; } /* - * @fn SDRM_I2OSP - * @brief Converts a nonnonegative integer to an octet string of a specified length + * @fn SDRM_I2OSP + * @brief Converts a nonnonegative integer to an octet string of a specified length * - * @param BN_Src [in]nonnegative integer to be converted - * @param dDstLen [in]intended length of the resulting octet string - * @param pbDst [out]corresponding octet string of length dDstLen + * @param BN_Src [in]nonnegative integer to be converted + * @param dDstLen [in]intended length of the resulting octet string + * @param pbDst [out]corresponding octet string of length dDstLen * - * @return CRYPTO_SUCCESS if no error is occured + * @return CRYPTO_SUCCESS if no error is occured */ -int SDRM_I2OSP(SDRM_BIG_NUM *BN_Src, cc_u32 dDstLen, cc_u8 *pbDst) +int SDRM_I2OSP(SDRM_BIG_NUM *BN_Src, cc_u32 dDstLen, cc_u8 *pbDst) { int count; SDRM_BN_OPTIMIZE_LENGTH(BN_Src); count = 0; + for (dDstLen--; (int)dDstLen >= 0; dDstLen--) - { pbDst[count++] = SDRM_CheckByteUINT32(BN_Src->pData, dDstLen); - } return CRYPTO_SUCCESS; } /* - * @fn SDRM_BN_Clr - * @brief Clear the SDRM_BIG_NUM structure + * @fn SDRM_BN_Clr + * @brief Clear the SDRM_BIG_NUM structure * - * @param BN_Src [in]source + * @param BN_Src [in]source * - * @return CRYPTO_SUCCESS + * @return CRYPTO_SUCCESS */ -int SDRM_BN_Clr(SDRM_BIG_NUM* BN_Src) +int SDRM_BN_Clr(SDRM_BIG_NUM *BN_Src) { BN_Src->sign = 0; BN_Src->Length = 0; @@ -1016,20 +911,18 @@ int SDRM_BN_Clr(SDRM_BIG_NUM* BN_Src) } /* - * @fn SDRM_BN_Copy - * @brief copy SDRM_BIG_NUM + * @fn SDRM_BN_Copy + * @brief copy SDRM_BIG_NUM * - * @param BN_Dest [out]destination - * @param BN_Src [in]source + * @param BN_Dest [out]destination + * @param BN_Src [in]source * - * @return CRYPTO_SUCCESS + * @return CRYPTO_SUCCESS */ -int SDRM_BN_Copy(SDRM_BIG_NUM* BN_Dest, SDRM_BIG_NUM* BN_Src) +int SDRM_BN_Copy(SDRM_BIG_NUM *BN_Dest, SDRM_BIG_NUM *BN_Src) { if (BN_Src->Length > BN_Dest->Size) - { return CRYPTO_BUFFER_TOO_SMALL; - } BN_Dest->sign = BN_Src->sign; BN_Dest->Length = BN_Src->Length; @@ -1040,25 +933,23 @@ int SDRM_BN_Copy(SDRM_BIG_NUM* BN_Dest, SDRM_BIG_NUM* BN_Src) } /* - * @fn SDRM_BN_Alloc - * @brief allocate big number from buffer + * @fn SDRM_BN_Alloc + * @brief allocate big number from buffer * - * @param pbSrc [in]start pointer of buffer - * @param dSize [in]buffer size of big number + * @param pbSrc [in]start pointer of buffer + * @param dSize [in]buffer size of big number * - * @return pointer of SDRM_BIG_NUM structure + * @return pointer of SDRM_BIG_NUM structure */ -SDRM_BIG_NUM *SDRM_BN_Alloc(cc_u8* pbSrc, cc_u32 dSize) +SDRM_BIG_NUM *SDRM_BN_Alloc(cc_u8 *pbSrc, cc_u32 dSize) { - SDRM_BIG_NUM *BN_Dest = (SDRM_BIG_NUM*)(void*)pbSrc; + SDRM_BIG_NUM *BN_Dest = (SDRM_BIG_NUM *)(void *)pbSrc; if (pbSrc == NULL) - { return NULL; - } memset(BN_Dest, 0, sizeof(SDRM_BIG_NUM) + dSize * SDRM_SIZE_OF_DWORD); - BN_Dest->pData = (cc_u32*)(void*)(pbSrc + sizeof(SDRM_BIG_NUM)); + BN_Dest->pData = (cc_u32 *)(void *)(pbSrc + sizeof(SDRM_BIG_NUM)); BN_Dest->Size = dSize; BN_Dest->Length = 0; @@ -1066,108 +957,100 @@ SDRM_BIG_NUM *SDRM_BN_Alloc(cc_u8* pbSrc, cc_u32 dSize) } /* - * @fn SDRM_BN_Init - * @brief Allocate a new big number object + * @fn SDRM_BN_Init + * @brief Allocate a new big number object * - * @param dSize [in]buffer size of big number + * @param dSize [in]buffer size of big number * - * @return pointer of SDRM_BIG_NUM structure - * \n NULL if memory allocation is failed + * @return pointer of SDRM_BIG_NUM structure + * \n NULL if memory allocation is failed */ SDRM_BIG_NUM *SDRM_BN_Init(cc_u32 dSize) { - cc_u32 AllocSize = sizeof(SDRM_BIG_NUM) + dSize * SDRM_SIZE_OF_DWORD; - SDRM_BIG_NUM *BN_Src = (SDRM_BIG_NUM*)malloc(AllocSize); + cc_u32 AllocSize = sizeof(SDRM_BIG_NUM) + dSize * SDRM_SIZE_OF_DWORD; + SDRM_BIG_NUM *BN_Src = (SDRM_BIG_NUM *)malloc(AllocSize); + if (BN_Src == NULL) - { return NULL; - } memset(BN_Src, 0, AllocSize); - BN_Src->pData = (cc_u32*)(void*)((char*)BN_Src + sizeof(SDRM_BIG_NUM)); + BN_Src->pData = (cc_u32 *)(void *)((char *)BN_Src + sizeof(SDRM_BIG_NUM)); BN_Src->Size = dSize; return BN_Src; } /* - * @fn SDRM_BN_Cmp - * @brief Compare two Big Number + * @fn SDRM_BN_Cmp + * @brief Compare two Big Number * - * @param BN_Src1 [in]first element - * @param BN_Src2 [in]second element + * @param BN_Src1 [in]first element + * @param BN_Src2 [in]second element * - * @return 1 if BN_Src1 is larger than pdSrc2 - * \n 0 if same - * \n -1 if BN_Src2 is larger than pdSrc1 + * @return 1 if BN_Src1 is larger than pdSrc2 + * \n 0 if same + * \n -1 if BN_Src2 is larger than pdSrc1 */ int SDRM_BN_Cmp(SDRM_BIG_NUM *BN_Src1, SDRM_BIG_NUM *BN_Src2) { - if (BN_Src1->Length >= BN_Src2->Length) - { - return SDRM_DWD_Cmp(BN_Src1->pData, BN_Src1->Length, BN_Src2->pData, BN_Src2->Length); - } - else - { - return -SDRM_DWD_Cmp(BN_Src2->pData, BN_Src2->Length, BN_Src1->pData, BN_Src1->Length); + if (BN_Src1->Length >= BN_Src2->Length) { + return SDRM_DWD_Cmp(BN_Src1->pData, BN_Src1->Length, BN_Src2->pData, + BN_Src2->Length); + } else { + return -SDRM_DWD_Cmp(BN_Src2->pData, BN_Src2->Length, BN_Src1->pData, + BN_Src1->Length); } } /* - * @fn SDRM_BN_Cmp_sign - * @brief Compare two Big Number considering sign + * @fn SDRM_BN_Cmp_sign + * @brief Compare two Big Number considering sign * - * @param BN_Src1 [in]first element - * @param BN_Src2 [in]second element + * @param BN_Src1 [in]first element + * @param BN_Src2 [in]second element * - * @return 1 if BN_Src1 is larger than pdSrc2 - * \n 0 if same - * \n -1 if BN_Src2 is larger than pdSrc1 + * @return 1 if BN_Src1 is larger than pdSrc2 + * \n 0 if same + * \n -1 if BN_Src2 is larger than pdSrc1 */ int SDRM_BN_Cmp_sign(SDRM_BIG_NUM *BN_Src1, SDRM_BIG_NUM *BN_Src2) { if (BN_Src1->sign > BN_Src2->sign) - { return -1; - } + else if (BN_Src1->sign < BN_Src2->sign) - { return 1; - } - if ( BN_Src1->Length >= BN_Src2->Length ) - { - return SDRM_DWD_Cmp(BN_Src1->pData, BN_Src1->Length, BN_Src2->pData, BN_Src2->Length); - } - else - { - return -SDRM_DWD_Cmp(BN_Src2->pData, BN_Src2->Length, BN_Src1->pData, BN_Src1->Length); + if (BN_Src1->Length >= BN_Src2->Length) { + return SDRM_DWD_Cmp(BN_Src1->pData, BN_Src1->Length, BN_Src2->pData, + BN_Src2->Length); + } else { + return -SDRM_DWD_Cmp(BN_Src2->pData, BN_Src2->Length, BN_Src1->pData, + BN_Src1->Length); } } /* - * @fn SDRM_BN_Rand - * @brief Generate simple random number + * @fn SDRM_BN_Rand + * @brief Generate simple random number * - * @param BN_Dst [out]destination - * @param BitLen [in]bit-length of generated random number + * @param BN_Dst [out]destination + * @param BitLen [in]bit-length of generated random number * - * @return CRYPTO_SUCCESS if no error is occured + * @return CRYPTO_SUCCESS if no error is occured */ int SDRM_BN_Rand(SDRM_BIG_NUM *BN_Dst, cc_u32 BitLen) { - cc_u32 i, j; + cc_u32 i, j; SDRM_BN_Clr(BN_Dst); for (i = 0; i < (BitLen / SDRM_BitsInDWORD); i++) - { BN_Dst->pData[i] = rand() ^ (rand() << 11); - } j = BitLen % SDRM_BitsInDWORD; - if (j) - { + + if (j) { BN_Dst->pData[i] = rand() ^ (rand() << 11); BN_Dst->pData[i] &= (((cc_u32)1) << j) - 1; i++; @@ -1181,21 +1064,20 @@ int SDRM_BN_Rand(SDRM_BIG_NUM *BN_Dst, cc_u32 BitLen) } /* - * @fn SDRM_BN_SHL - * @brief Big Number Shift Left + * @fn SDRM_BN_SHL + * @brief Big Number Shift Left * - * @param BN_Dst [out]destination - * @param BN_Src [in]source - * @param NumOfShift [in]shift amount + * @param BN_Dst [out]destination + * @param BN_Src [in]source + * @param NumOfShift [in]shift amount * - * @return CRYPTO_SUCCESS if no error occured + * @return CRYPTO_SUCCESS if no error occured */ int SDRM_BN_SHL(SDRM_BIG_NUM *BN_Dst, SDRM_BIG_NUM *BN_Src, cc_u32 NumOfShift) { - cc_u32 t; + cc_u32 t; - if (!BN_Src->Length) - { + if (!BN_Src->Length) { SDRM_BN_Copy(BN_Dst, BN_Zero); return CRYPTO_SUCCESS; } @@ -1203,26 +1085,23 @@ int SDRM_BN_SHL(SDRM_BIG_NUM *BN_Dst, SDRM_BIG_NUM *BN_Src, cc_u32 NumOfShift) BN_Dst->sign = BN_Src->sign; t = NumOfShift % SDRM_BitsInDWORD; - if (t) - { + + if (t) { BN_Dst->Length = BN_Src->Length; t = SDRM_DWD_SHL(BN_Dst->pData, BN_Src->pData, BN_Src->Length, t); + if (t) - { BN_Dst->pData[BN_Dst->Length++] = t; - } - } - else - { + } else SDRM_BN_Copy(BN_Dst, BN_Src); - } t = NumOfShift / SDRM_BitsInDWORD; - if (t) - { + + if (t) { BN_Dst->Length += t; - memmove((BN_Dst->pData) + t, BN_Dst->pData, (BN_Dst->Length - t) * SDRM_SIZE_OF_DWORD); + memmove((BN_Dst->pData) + t, BN_Dst->pData, + (BN_Dst->Length - t) * SDRM_SIZE_OF_DWORD); memset(BN_Dst->pData, 0, t * SDRM_SIZE_OF_DWORD); } @@ -1233,50 +1112,45 @@ int SDRM_BN_SHL(SDRM_BIG_NUM *BN_Dst, SDRM_BIG_NUM *BN_Src, cc_u32 NumOfShift) } /* - * @fn SDRM_BN_SHR - * @brief Big Number Shift Right + * @fn SDRM_BN_SHR + * @brief Big Number Shift Right * - * @param BN_Dst [out]destination - * @param BN_Src [in]source - * @param NumOfShift [in]shift amount + * @param BN_Dst [out]destination + * @param BN_Src [in]source + * @param NumOfShift [in]shift amount * - * @return CRYPTO_SUCCESS if no error occured + * @return CRYPTO_SUCCESS if no error occured */ int SDRM_BN_SHR(SDRM_BIG_NUM *BN_Dst, SDRM_BIG_NUM *BN_Src, cc_u32 NumOfShift) { - cc_u32 t; + cc_u32 t; - if (!BN_Src->Length) - { + if (!BN_Src->Length) { SDRM_BN_Copy(BN_Dst, BN_Src); return CRYPTO_SUCCESS; } t = NumOfShift / SDRM_BitsInDWORD; - if (t) - { - if (t >= BN_Src->Length) - { + + if (t) { + if (t >= BN_Src->Length) { SDRM_BN_Copy(BN_Dst, BN_Zero); return CRYPTO_SUCCESS; } - memcpy(BN_Dst->pData, (BN_Src->pData) + t, (BN_Src->Length - t) * SDRM_SIZE_OF_DWORD); + memcpy(BN_Dst->pData, (BN_Src->pData) + t, + (BN_Src->Length - t) * SDRM_SIZE_OF_DWORD); BN_Dst->Length = BN_Src->Length - t; BN_Dst->sign = BN_Src->sign; SDRM_BN_OPTIMIZE_LENGTH(BN_Dst); - } - else - { + } else SDRM_BN_Copy(BN_Dst, BN_Src); - } t = NumOfShift % SDRM_BitsInDWORD; + if (t) - { SDRM_DWD_SHR(BN_Dst->pData, BN_Dst->pData, BN_Dst->Length, t); - } SDRM_BN_OPTIMIZE_LENGTH(BN_Dst); @@ -1284,37 +1158,35 @@ int SDRM_BN_SHR(SDRM_BIG_NUM *BN_Dst, SDRM_BIG_NUM *BN_Src, cc_u32 NumOfShift) } /* - * @fn SDRM_BN_Add - * @brief Big Number Addition + * @fn SDRM_BN_Add + * @brief Big Number Addition * - * @param BN_Dst [out]destination - * @param BN_Src1 [in]first element - * @param BN_Src2 [in]second element + * @param BN_Dst [out]destination + * @param BN_Src1 [in]first element + * @param BN_Src2 [in]second element * - * @return CRYPTO_SUCCESS if no error occured - * \n CRYPTO_MEMORY_ALLOC_FAIL if malloc is failed + * @return CRYPTO_SUCCESS if no error occured + * \n CRYPTO_MEMORY_ALLOC_FAIL if malloc is failed */ -int SDRM_BN_Add(SDRM_BIG_NUM *BN_Dst, SDRM_BIG_NUM *BN_Src1, SDRM_BIG_NUM *BN_Src2) +int SDRM_BN_Add(SDRM_BIG_NUM *BN_Dst, SDRM_BIG_NUM *BN_Src1, + SDRM_BIG_NUM *BN_Src2) { - cc_u32 carry, dSize, dAllocSize; - SDRM_BIG_NUM *temp, *temp_Src1, *temp_Src2; - cc_u8 *pbBuf; + cc_u32 carry, dSize, dAllocSize; + SDRM_BIG_NUM *temp, *temp_Src1, *temp_Src2; + cc_u8 *pbBuf; dSize = MAX2(BN_Src1->Size, BN_Src2->Size); dAllocSize = sizeof(SDRM_BIG_NUM) + dSize * SDRM_SIZE_OF_DWORD; - pbBuf = (cc_u8*)malloc(dAllocSize * 2); + pbBuf = (cc_u8 *)malloc(dAllocSize * 2); if (!pbBuf) - { return CRYPTO_MEMORY_ALLOC_FAIL; - } temp_Src1 = SDRM_BN_Alloc(pbBuf, dSize); temp_Src2 = SDRM_BN_Alloc(pbBuf + dAllocSize, dSize); - if (!BN_Src1->Length) - { + if (!BN_Src1->Length) { SDRM_BN_Copy(BN_Dst, BN_Src2); free(pbBuf); @@ -1331,24 +1203,21 @@ int SDRM_BN_Add(SDRM_BIG_NUM *BN_Dst, SDRM_BIG_NUM *BN_Src1, SDRM_BIG_NUM *BN_Sr SDRM_BN_Copy(temp_Src1, BN_Src1); SDRM_BN_Copy(temp_Src2, BN_Src2); - if (temp_Src1->sign ^ temp_Src2->sign) - { - if (temp_Src1->sign) - { + if (temp_Src1->sign ^ temp_Src2->sign) { + if (temp_Src1->sign) { temp = temp_Src1; temp_Src1 = temp_Src2; temp_Src2 = temp; } - if (SDRM_BN_Cmp(temp_Src1, temp_Src2) < 0) - { - SDRM_DWD_Sub(BN_Dst->pData, temp_Src2->pData, temp_Src2->Length, temp_Src1->pData, temp_Src1->Length); + if (SDRM_BN_Cmp(temp_Src1, temp_Src2) < 0) { + SDRM_DWD_Sub(BN_Dst->pData, temp_Src2->pData, temp_Src2->Length, + temp_Src1->pData, temp_Src1->Length); BN_Dst->sign = 1; BN_Dst->Length = temp_Src2->Length; - } - else - { - SDRM_DWD_Sub(BN_Dst->pData, temp_Src1->pData, temp_Src1->Length, temp_Src2->pData, temp_Src2->Length); + } else { + SDRM_DWD_Sub(BN_Dst->pData, temp_Src1->pData, temp_Src1->Length, + temp_Src2->pData, temp_Src2->Length); BN_Dst->sign = 0; BN_Dst->Length = temp_Src1->Length; } @@ -1359,31 +1228,25 @@ int SDRM_BN_Add(SDRM_BIG_NUM *BN_Dst, SDRM_BIG_NUM *BN_Src1, SDRM_BIG_NUM *BN_Sr } if (temp_Src1->sign) - { BN_Dst->sign = 1; - } + else - { BN_Dst->sign = 0; - } - if (temp_Src1->Length > temp_Src2->Length) - { + if (temp_Src1->Length > temp_Src2->Length) { BN_Dst->Length = temp_Src1->Length; - carry = SDRM_DWD_Add(BN_Dst->pData, temp_Src1->pData, temp_Src1->Length, temp_Src2->pData, temp_Src2->Length); + carry = SDRM_DWD_Add(BN_Dst->pData, temp_Src1->pData, temp_Src1->Length, + temp_Src2->pData, temp_Src2->Length); + if (carry) - { BN_Dst->pData[BN_Dst->Length++] = carry; - } - } - else - { + } else { BN_Dst->Length = temp_Src2->Length; - carry = SDRM_DWD_Add(BN_Dst->pData, temp_Src2->pData, temp_Src2->Length, temp_Src1->pData, temp_Src1->Length); - if ( carry ) - { + carry = SDRM_DWD_Add(BN_Dst->pData, temp_Src2->pData, temp_Src2->Length, + temp_Src1->pData, temp_Src1->Length); + + if (carry) BN_Dst->pData[BN_Dst->Length++] = carry; - } } SDRM_BN_OPTIMIZE_LENGTH(BN_Dst); @@ -1394,30 +1257,29 @@ int SDRM_BN_Add(SDRM_BIG_NUM *BN_Dst, SDRM_BIG_NUM *BN_Src1, SDRM_BIG_NUM *BN_Sr } /* - * @fn SDRM_BN_Sub - * @brief Big Number Subtraction + * @fn SDRM_BN_Sub + * @brief Big Number Subtraction * - * @param BN_Dst [out]destination - * @param BN_Src1 [in]first element - * @param BN_Src2 [in]second element + * @param BN_Dst [out]destination + * @param BN_Src1 [in]first element + * @param BN_Src2 [in]second element * - * @return CRYPTO_SUCCESS if no error occured - * \n CRYPTO_MEMORY_ALLOC_FAIL if malloc is failed + * @return CRYPTO_SUCCESS if no error occured + * \n CRYPTO_MEMORY_ALLOC_FAIL if malloc is failed */ -int SDRM_BN_Sub(SDRM_BIG_NUM *BN_Dst, SDRM_BIG_NUM *BN_Src1, SDRM_BIG_NUM *BN_Src2) +int SDRM_BN_Sub(SDRM_BIG_NUM *BN_Dst, SDRM_BIG_NUM *BN_Src1, + SDRM_BIG_NUM *BN_Src2) { - int i, add = 0, dSize, dAllocSize; - SDRM_BIG_NUM *temp, *temp_Src1, *temp_Src2; - cc_u8 *pbBuf; + int i, add = 0, dSize, dAllocSize; + SDRM_BIG_NUM *temp, *temp_Src1, *temp_Src2; + cc_u8 *pbBuf; dSize = MAX2(BN_Src1->Size, BN_Src2->Size); dAllocSize = sizeof(SDRM_BIG_NUM) + dSize * SDRM_SIZE_OF_DWORD; - pbBuf = (cc_u8*)malloc(dAllocSize * 2); + pbBuf = (cc_u8 *)malloc(dAllocSize * 2); if (!pbBuf) - { return CRYPTO_MEMORY_ALLOC_FAIL; - } temp_Src1 = SDRM_BN_Alloc(pbBuf, dSize); temp_Src2 = SDRM_BN_Alloc(pbBuf + dAllocSize, dSize); @@ -1425,8 +1287,7 @@ int SDRM_BN_Sub(SDRM_BIG_NUM *BN_Dst, SDRM_BIG_NUM *BN_Src1, SDRM_BIG_NUM *BN_Sr SDRM_BN_Copy(temp_Src1, BN_Src1); SDRM_BN_Copy(temp_Src2, BN_Src2); - if (BN_Src1 == BN_Src2) - { + if (BN_Src1 == BN_Src2) { SDRM_BN_Clr(BN_Dst); free(pbBuf); @@ -1434,58 +1295,48 @@ int SDRM_BN_Sub(SDRM_BIG_NUM *BN_Dst, SDRM_BIG_NUM *BN_Src1, SDRM_BIG_NUM *BN_Sr } //to process sign - if (temp_Src1->sign) - { - if (temp_Src2->sign) - { + if (temp_Src1->sign) { + if (temp_Src2->sign) { temp = temp_Src1; temp_Src1 = temp_Src2; temp_Src2 = temp; - } - else - { + } else { add = 1; temp_Src2->sign = 1; } - } - else - { - if (temp_Src2->sign) - { + } else { + if (temp_Src2->sign) { add = 1; temp_Src2->sign = 0; } } - if (add) - { - i = (temp_Src1->Length | temp_Src2->Length) +1; + if (add) { + i = (temp_Src1->Length | temp_Src2->Length) + 1; + if (i) - { SDRM_BN_Add(BN_Dst, temp_Src1, temp_Src2); - } + else - { SDRM_BN_Add(BN_Dst, temp_Src2, temp_Src1); - } free(pbBuf); return CRYPTO_SUCCESS; } - if (SDRM_BN_Cmp(temp_Src1, temp_Src2) < 0) - { - SDRM_DWD_Sub(BN_Dst->pData, temp_Src2->pData, temp_Src2->Length, temp_Src1->pData, temp_Src1->Length); + if (SDRM_BN_Cmp(temp_Src1, temp_Src2) < 0) { + SDRM_DWD_Sub(BN_Dst->pData, temp_Src2->pData, temp_Src2->Length, + temp_Src1->pData, temp_Src1->Length); BN_Dst->sign = 1; BN_Dst->Length = temp_Src2->Length; - } - else - { - SDRM_DWD_Sub(BN_Dst->pData, temp_Src1->pData, temp_Src1->Length, temp_Src2->pData, temp_Src2->Length); + } else { + SDRM_DWD_Sub(BN_Dst->pData, temp_Src1->pData, temp_Src1->Length, + temp_Src2->pData, temp_Src2->Length); BN_Dst->sign = 0; BN_Dst->Length = temp_Src1->Length; } + SDRM_BN_OPTIMIZE_LENGTH(BN_Dst); free(pbBuf); @@ -1494,50 +1345,45 @@ int SDRM_BN_Sub(SDRM_BIG_NUM *BN_Dst, SDRM_BIG_NUM *BN_Src1, SDRM_BIG_NUM *BN_Sr } /* - * @fn SDRM_BN_Mul - * @brief Big Number Multiplication + * @fn SDRM_BN_Mul + * @brief Big Number Multiplication * - * @param BN_Dst [out]destination - * @param BN_Multiplicand [in]first element - * @param BN_Multiplier [in]second element + * @param BN_Dst [out]destination + * @param BN_Multiplicand [in]first element + * @param BN_Multiplier [in]second element * - * @return CRYPTO_SUCCESS if no error occured - * \n CRYPTO_MEMORY_ALLOC_FAIL if malloc is failed + * @return CRYPTO_SUCCESS if no error occured + * \n CRYPTO_MEMORY_ALLOC_FAIL if malloc is failed */ -int SDRM_BN_Mul(SDRM_BIG_NUM *BN_Dst, SDRM_BIG_NUM *BN_Multiplicand, SDRM_BIG_NUM *BN_Multiplier) +int SDRM_BN_Mul(SDRM_BIG_NUM *BN_Dst, SDRM_BIG_NUM *BN_Multiplicand, + SDRM_BIG_NUM *BN_Multiplier) { - SDRM_BIG_NUM *Dst; + SDRM_BIG_NUM *Dst; - if ((BN_Multiplicand->Length == 0) || (BN_Multiplier->Length == 0)) - { + if ((BN_Multiplicand->Length == 0) || (BN_Multiplier->Length == 0)) { SDRM_BN_Clr(BN_Dst); return CRYPTO_SUCCESS; } Dst = SDRM_BN_Init(BN_Dst->Size * 2); + if (Dst == NULL) - { return CRYPTO_MEMORY_ALLOC_FAIL; - } Dst->Length = BN_Multiplicand->Length + BN_Multiplier->Length; if (BN_Multiplicand->sign != BN_Multiplier->sign) - { Dst->sign = 1; - } + else - { Dst->sign = 0; - } - if (BN_Multiplicand->Length > BN_Multiplier->Length) - { - SDRM_DWD_Mul(Dst->pData, BN_Multiplicand->pData, BN_Multiplicand->Length, BN_Multiplier->pData, BN_Multiplier->Length); - } - else - { - SDRM_DWD_Mul(Dst->pData, BN_Multiplier->pData, BN_Multiplier->Length, BN_Multiplicand->pData, BN_Multiplicand->Length); + if (BN_Multiplicand->Length > BN_Multiplier->Length) { + SDRM_DWD_Mul(Dst->pData, BN_Multiplicand->pData, BN_Multiplicand->Length, + BN_Multiplier->pData, BN_Multiplier->Length); + } else { + SDRM_DWD_Mul(Dst->pData, BN_Multiplier->pData, BN_Multiplier->Length, + BN_Multiplicand->pData, BN_Multiplicand->Length); } SDRM_BN_OPTIMIZE_LENGTH(Dst); @@ -1549,90 +1395,83 @@ int SDRM_BN_Mul(SDRM_BIG_NUM *BN_Dst, SDRM_BIG_NUM *BN_Multiplicand, SDRM_BIG_NU } /* - * @fn SDRM_BN_Div - * @brief Big Number Division + * @fn SDRM_BN_Div + * @brief Big Number Division * - * @param BN_Quotient [out]quotient - * @param BN_Remainder [out]remainder - * @param BN_Dividend [in]dividend - * @param BN_Divisor [in]divisor + * @param BN_Quotient [out]quotient + * @param BN_Remainder [out]remainder + * @param BN_Dividend [in]dividend + * @param BN_Divisor [in]divisor * - * @return CRYPTO_SUCCESS if no error occured - * \n CRYPTO_MEMORY_ALLOC_FAIL if malloc is failed + * @return CRYPTO_SUCCESS if no error occured + * \n CRYPTO_MEMORY_ALLOC_FAIL if malloc is failed */ -int SDRM_BN_Div(SDRM_BIG_NUM *BN_Quotient, SDRM_BIG_NUM *BN_Remainder, SDRM_BIG_NUM *BN_Dividend, SDRM_BIG_NUM *BN_Divisor) +int SDRM_BN_Div(SDRM_BIG_NUM *BN_Quotient, SDRM_BIG_NUM *BN_Remainder, + SDRM_BIG_NUM *BN_Dividend, SDRM_BIG_NUM *BN_Divisor) { - cc_u32 tmp, dSize, dAllocSize; - SDRM_BIG_NUM *temp_Dividend, *temp_Divisor; - cc_u32 *bnTmp; - cc_u8 *pbBuf; + cc_u32 tmp, dSize, dAllocSize; + SDRM_BIG_NUM *temp_Dividend, *temp_Divisor; + cc_u32 *bnTmp; + cc_u8 *pbBuf; if (BN_Quotient != NULL) - { dSize = MAX2(BN_Quotient->Size, BN_Dividend->Size); - } + else - { dSize = BN_Dividend->Size; - } dAllocSize = sizeof(SDRM_BIG_NUM) + dSize * SDRM_SIZE_OF_DWORD; - pbBuf = (cc_u8*)malloc(dAllocSize * 3 + 2 * SDRM_SIZE_OF_DWORD); + pbBuf = (cc_u8 *)malloc(dAllocSize * 3 + 2 * SDRM_SIZE_OF_DWORD); + if (!pbBuf) - { return CRYPTO_MEMORY_ALLOC_FAIL; - } - temp_Dividend = SDRM_BN_Alloc(pbBuf, dSize); - temp_Divisor = SDRM_BN_Alloc(pbBuf + dAllocSize, dSize); - bnTmp = (cc_u32*)(void*)(pbBuf + dSize + dAllocSize); + temp_Dividend = SDRM_BN_Alloc(pbBuf, dSize); + temp_Divisor = SDRM_BN_Alloc(pbBuf + dAllocSize, dSize); + bnTmp = (cc_u32 *)(void *)(pbBuf + dSize + dAllocSize); SDRM_BN_Copy(temp_Dividend, BN_Dividend); SDRM_BN_Copy(temp_Divisor, BN_Divisor); - if (SDRM_BN_Cmp(temp_Dividend, temp_Divisor) < 0) - { - if (BN_Remainder != NULL) - { + if (SDRM_BN_Cmp(temp_Dividend, temp_Divisor) < 0) { + if (BN_Remainder != NULL) { SDRM_BN_Copy(BN_Remainder, temp_Dividend); //free(pbBuf); //return CRYPTO_SUCCESS; modify by Chalyi Aleksandr: it is not correct } if (BN_Quotient != NULL) - { SDRM_BN_Clr(BN_Quotient); - } + free(pbBuf); return CRYPTO_SUCCESS; } tmp = 0; - if (BN_Quotient == NULL) - { + + if (BN_Quotient == NULL) { if (BN_Remainder != NULL) { BN_Remainder->Length = temp_Divisor->Length; - tmp = SDRM_DWD_Div(bnTmp, BN_Remainder->pData, temp_Dividend->pData, temp_Dividend->Length, temp_Divisor->pData, temp_Divisor->Length); + tmp = SDRM_DWD_Div(bnTmp, BN_Remainder->pData, temp_Dividend->pData, + temp_Dividend->Length, temp_Divisor->pData, temp_Divisor->Length); SDRM_BN_OPTIMIZE_LENGTH(BN_Remainder); BN_Remainder->sign = BN_Dividend->sign; } - } - else if (BN_Remainder == NULL) - { + } else if (BN_Remainder == NULL) { BN_Quotient->Length = temp_Dividend->Length - temp_Divisor->Length + 1; - tmp = SDRM_DWD_Div(BN_Quotient->pData, bnTmp, temp_Dividend->pData, temp_Dividend->Length, temp_Divisor->pData, temp_Divisor->Length); + tmp = SDRM_DWD_Div(BN_Quotient->pData, bnTmp, temp_Dividend->pData, + temp_Dividend->Length, temp_Divisor->pData, temp_Divisor->Length); SDRM_BN_OPTIMIZE_LENGTH(BN_Quotient); - BN_Quotient->sign= (BN_Dividend->sign^BN_Divisor->sign); - } - else - { + BN_Quotient->sign = (BN_Dividend->sign ^ BN_Divisor->sign); + } else { BN_Quotient->Length = temp_Dividend->Length - temp_Divisor->Length + 1; BN_Remainder->Length = temp_Divisor->Length; - BN_Quotient->sign= (BN_Dividend->sign^BN_Divisor->sign); + BN_Quotient->sign = (BN_Dividend->sign ^ BN_Divisor->sign); BN_Remainder->sign = BN_Dividend->sign; - tmp = SDRM_DWD_Div(BN_Quotient->pData, BN_Remainder->pData, BN_Dividend->pData, BN_Dividend->Length, BN_Divisor->pData, BN_Divisor->Length); + tmp = SDRM_DWD_Div(BN_Quotient->pData, BN_Remainder->pData, BN_Dividend->pData, + BN_Dividend->Length, BN_Divisor->pData, BN_Divisor->Length); SDRM_BN_OPTIMIZE_LENGTH(BN_Quotient); SDRM_BN_OPTIMIZE_LENGTH(BN_Remainder); @@ -1641,40 +1480,37 @@ int SDRM_BN_Div(SDRM_BIG_NUM *BN_Quotient, SDRM_BIG_NUM *BN_Remainder, SDRM_BIG_ free(pbBuf); if (!(tmp == 0 || tmp == 1)) - { return CRYPTO_ERROR; - } return CRYPTO_SUCCESS; } /* - * @fn SDRM_BN_ModAdd - * @brief Big Number Modular Addition + * @fn SDRM_BN_ModAdd + * @brief Big Number Modular Addition * - * @param BN_Dst [out]destination - * @param BN_Src1 [in]first element of addition - * @param BN_Src2 [in]second element of addition - * @param BN_Modulus [in]modular m + * @param BN_Dst [out]destination + * @param BN_Src1 [in]first element of addition + * @param BN_Src2 [in]second element of addition + * @param BN_Modulus [in]modular m * - * @return CRYPTO_SUCCESS if no error occured - * \n CRYPTO_MEMORY_ALLOC_FAIL if malloc is failed + * @return CRYPTO_SUCCESS if no error occured + * \n CRYPTO_MEMORY_ALLOC_FAIL if malloc is failed */ -int SDRM_BN_ModAdd(SDRM_BIG_NUM *BN_Dst, SDRM_BIG_NUM *BN_Src1, SDRM_BIG_NUM *BN_Src2, SDRM_BIG_NUM *BN_Modulus) +int SDRM_BN_ModAdd(SDRM_BIG_NUM *BN_Dst, SDRM_BIG_NUM *BN_Src1, + SDRM_BIG_NUM *BN_Src2, SDRM_BIG_NUM *BN_Modulus) { - SDRM_BIG_NUM *BN_Src1_temp, *BN_Src2_temp; - cc_u8 *pbBuf; - cc_u32 tmp = 0, dSize, AllocSize; + SDRM_BIG_NUM *BN_Src1_temp, *BN_Src2_temp; + cc_u8 *pbBuf; + cc_u32 tmp = 0, dSize, AllocSize; dSize = MAX3(BN_Src1->Size, BN_Src2->Size, BN_Modulus->Size); AllocSize = sizeof(SDRM_BIG_NUM) + dSize * SDRM_SIZE_OF_DWORD; - pbBuf = (cc_u8*)malloc(AllocSize * 2); + pbBuf = (cc_u8 *)malloc(AllocSize * 2); if (!pbBuf) - { return CRYPTO_MEMORY_ALLOC_FAIL; - } BN_Src1_temp = SDRM_BN_Alloc(pbBuf, dSize); BN_Src2_temp = SDRM_BN_Alloc(pbBuf + AllocSize, dSize); @@ -1682,40 +1518,33 @@ int SDRM_BN_ModAdd(SDRM_BIG_NUM *BN_Dst, SDRM_BIG_NUM *BN_Src1, SDRM_BIG_NUM *BN SDRM_BN_Copy(BN_Src1_temp, BN_Src1); SDRM_BN_Copy(BN_Src2_temp, BN_Src2); - if ((SDRM_BN_Cmp(BN_Src1, BN_Modulus)>=0)) - { + if ((SDRM_BN_Cmp(BN_Src1, BN_Modulus) >= 0)) SDRM_BN_ModRed(BN_Src1_temp, BN_Src1, BN_Modulus); - } - if ((SDRM_BN_Cmp(BN_Src2, BN_Modulus)>=0)) - { + if ((SDRM_BN_Cmp(BN_Src2, BN_Modulus) >= 0)) SDRM_BN_ModRed(BN_Src2_temp, BN_Src2, BN_Modulus); - } - if (BN_Src1_temp->Length >= BN_Src2_temp->Length) - { + if (BN_Src1_temp->Length >= BN_Src2_temp->Length) { BN_Dst->Length = BN_Src1_temp->Length; BN_Dst->sign = BN_Src1_temp->sign; - tmp = SDRM_DWD_Add(BN_Dst->pData, BN_Src1_temp->pData, BN_Src1_temp->Length, BN_Src2_temp->pData, BN_Src2_temp->Length); - } - else - { + tmp = SDRM_DWD_Add(BN_Dst->pData, BN_Src1_temp->pData, BN_Src1_temp->Length, + BN_Src2_temp->pData, BN_Src2_temp->Length); + } else { BN_Dst->Length = BN_Src2_temp->Length; BN_Dst->sign = BN_Src2_temp->sign; tmp = SDRM_DWD_Add(BN_Dst->pData, BN_Src2_temp->pData, BN_Src2_temp->Length, - BN_Src1_temp->pData, BN_Src1_temp->Length); + BN_Src1_temp->pData, BN_Src1_temp->Length); } if (tmp) - { BN_Dst->pData[BN_Dst->Length++] = tmp; - } SDRM_BN_ModRed(BN_Dst, BN_Dst, BN_Modulus); - if (SDRM_DWD_Cmp(BN_Dst->pData, BN_Dst->Length, BN_Modulus->pData, BN_Modulus->Length) >= 0) - { - SDRM_DWD_Sub(BN_Dst->pData, BN_Dst->pData, BN_Dst->Length, BN_Modulus->pData, BN_Modulus->Length); + if (SDRM_DWD_Cmp(BN_Dst->pData, BN_Dst->Length, BN_Modulus->pData, + BN_Modulus->Length) >= 0) { + SDRM_DWD_Sub(BN_Dst->pData, BN_Dst->pData, BN_Dst->Length, BN_Modulus->pData, + BN_Modulus->Length); } SDRM_BN_OPTIMIZE_LENGTH(BN_Dst); @@ -1725,32 +1554,31 @@ int SDRM_BN_ModAdd(SDRM_BIG_NUM *BN_Dst, SDRM_BIG_NUM *BN_Src1, SDRM_BIG_NUM *BN } /* - * @fn SDRM_BN_ModSub - * @brief Big Number Modular Subtraction + * @fn SDRM_BN_ModSub + * @brief Big Number Modular Subtraction * - * @param BN_Dst [out]destination - * @param BN_Src1 [in]first element of subtraction - * @param BN_Src2 [in]second element of subtraction - * @param BN_Modulus [in]modular m + * @param BN_Dst [out]destination + * @param BN_Src1 [in]first element of subtraction + * @param BN_Src2 [in]second element of subtraction + * @param BN_Modulus [in]modular m * - * @return CRYPTO_SUCCESS if no error occured - * \n CRYPTO_MEMORY_ALLOC_FAIL if malloc is failed + * @return CRYPTO_SUCCESS if no error occured + * \n CRYPTO_MEMORY_ALLOC_FAIL if malloc is failed */ -int SDRM_BN_ModSub(SDRM_BIG_NUM *BN_Dst, SDRM_BIG_NUM *BN_Src1, SDRM_BIG_NUM *BN_Src2, SDRM_BIG_NUM *BN_Modulus) +int SDRM_BN_ModSub(SDRM_BIG_NUM *BN_Dst, SDRM_BIG_NUM *BN_Src1, + SDRM_BIG_NUM *BN_Src2, SDRM_BIG_NUM *BN_Modulus) { - cc_u32 tmp = 0, dSize, AllocSize; - SDRM_BIG_NUM *BN_Src1_temp, *BN_Src2_temp; - cc_u8 *pbBuf; + cc_u32 tmp = 0, dSize, AllocSize; + SDRM_BIG_NUM *BN_Src1_temp, *BN_Src2_temp; + cc_u8 *pbBuf; dSize = MAX3(BN_Src1->Size, BN_Src2->Size, BN_Modulus->Size); AllocSize = sizeof(SDRM_BIG_NUM) + dSize * SDRM_SIZE_OF_DWORD; - pbBuf = (cc_u8*)malloc(AllocSize * 2); + pbBuf = (cc_u8 *)malloc(AllocSize * 2); if (!pbBuf) - { return CRYPTO_MEMORY_ALLOC_FAIL; - } BN_Src1_temp = SDRM_BN_Alloc(pbBuf, dSize); BN_Src2_temp = SDRM_BN_Alloc(pbBuf + AllocSize, dSize); @@ -1759,64 +1587,58 @@ int SDRM_BN_ModSub(SDRM_BIG_NUM *BN_Dst, SDRM_BIG_NUM *BN_Src1, SDRM_BIG_NUM *BN SDRM_BN_Copy(BN_Src2_temp, BN_Src2); if ((SDRM_BN_Cmp(BN_Src1, BN_Modulus) >= 0)) - { SDRM_BN_ModRed(BN_Src1_temp, BN_Src1, BN_Modulus); - } if ((SDRM_BN_Cmp(BN_Src2, BN_Modulus) >= 0)) - { SDRM_BN_ModRed(BN_Src2_temp, BN_Src2, BN_Modulus); - } - if (SDRM_DWD_Cmp(BN_Src1_temp->pData, BN_Src1_temp->Length, BN_Src2_temp->pData, BN_Src2_temp->Length) >= 0) - { + if (SDRM_DWD_Cmp(BN_Src1_temp->pData, BN_Src1_temp->Length, BN_Src2_temp->pData, + BN_Src2_temp->Length) >= 0) { BN_Dst->Length = BN_Src1_temp->Length; BN_Dst->sign = BN_Src1_temp->sign; - tmp = SDRM_DWD_Sub(BN_Dst->pData, BN_Src1_temp->pData, BN_Src1_temp->Length, BN_Src2_temp->pData, BN_Src2_temp->Length); - } - else - { + tmp = SDRM_DWD_Sub(BN_Dst->pData, BN_Src1_temp->pData, BN_Src1_temp->Length, + BN_Src2_temp->pData, BN_Src2_temp->Length); + } else { BN_Dst->Length = BN_Modulus->Length; BN_Dst->sign = BN_Modulus->sign; - SDRM_DWD_Add(BN_Dst->pData, BN_Modulus->pData, BN_Modulus->Length, BN_Src1_temp->pData, BN_Src1_temp->Length); - SDRM_DWD_Sub(BN_Dst->pData, BN_Dst->pData, BN_Dst->Length, BN_Src2_temp->pData, BN_Src2_temp->Length); + SDRM_DWD_Add(BN_Dst->pData, BN_Modulus->pData, BN_Modulus->Length, + BN_Src1_temp->pData, BN_Src1_temp->Length); + SDRM_DWD_Sub(BN_Dst->pData, BN_Dst->pData, BN_Dst->Length, BN_Src2_temp->pData, + BN_Src2_temp->Length); } SDRM_BN_OPTIMIZE_LENGTH(BN_Dst); free(pbBuf); if (tmp != 0) - { return CRYPTO_ERROR; - } return CRYPTO_SUCCESS; } /* - * @fn SDRM_BN_ModRed - * @brief Big Number Modular Reduction + * @fn SDRM_BN_ModRed + * @brief Big Number Modular Reduction * - * @param BN_Dst [out]destination - * @param BN_Src [in]source - * @param BN_Modulus [in]modular m + * @param BN_Dst [out]destination + * @param BN_Src [in]source + * @param BN_Modulus [in]modular m * - * @return CRYPTO_SUCCESS if no error occured - * \n CRYPTO_MEMORY_ALLOC_FAIL if malloc is failed + * @return CRYPTO_SUCCESS if no error occured + * \n CRYPTO_MEMORY_ALLOC_FAIL if malloc is failed */ -int SDRM_BN_ModRed(SDRM_BIG_NUM *BN_Dst, SDRM_BIG_NUM *BN_Src, SDRM_BIG_NUM *BN_Modulus) +int SDRM_BN_ModRed(SDRM_BIG_NUM *BN_Dst, SDRM_BIG_NUM *BN_Src, + SDRM_BIG_NUM *BN_Modulus) { - int ret; - cc_u32 *Value = (cc_u32*)malloc(SDRM_SIZE_OF_DWORD * 2 * (sizeof(SDRM_BIG_NUM) + MAX2(BN_Src->Size, BN_Modulus->Size) + 2)); + int ret; + cc_u32 *Value = (cc_u32 *)malloc(SDRM_SIZE_OF_DWORD * 2 * (sizeof( + SDRM_BIG_NUM) + MAX2(BN_Src->Size, BN_Modulus->Size) + 2)); if (!Value) - { return CRYPTO_MEMORY_ALLOC_FAIL; - } - if (SDRM_BN_Cmp(BN_Src, BN_Modulus) < 0) - { + if (SDRM_BN_Cmp(BN_Src, BN_Modulus) < 0) { SDRM_BN_Copy(BN_Dst, BN_Src); free(Value); return CRYPTO_SUCCESS; @@ -1824,10 +1646,10 @@ int SDRM_BN_ModRed(SDRM_BIG_NUM *BN_Dst, SDRM_BIG_NUM *BN_Src, SDRM_BIG_NUM *BN_ memcpy(Value, BN_Src->pData, BN_Src->Length * SDRM_SIZE_OF_DWORD); - ret = SDRM_DWD_Classical_REDC(Value, BN_Src->Length, BN_Modulus->pData, BN_Modulus->Length); + ret = SDRM_DWD_Classical_REDC(Value, BN_Src->Length, BN_Modulus->pData, + BN_Modulus->Length); - if (ret != CRYPTO_SUCCESS) - { + if (ret != CRYPTO_SUCCESS) { free(Value); return ret; } @@ -1844,41 +1666,44 @@ int SDRM_BN_ModRed(SDRM_BIG_NUM *BN_Dst, SDRM_BIG_NUM *BN_Src, SDRM_BIG_NUM *BN_ } /* - * @fn SDRM_BN_ModMul - * @brief Big Number Modular Multiplication + * @fn SDRM_BN_ModMul + * @brief Big Number Modular Multiplication * - * @param BN_Dst [out]destination - * @param BN_Src1 [in]first element of multiplication - * @param BN_Src2 [in]second element of multipliation - * @param BN_Modulus [in]modular m + * @param BN_Dst [out]destination + * @param BN_Src1 [in]first element of multiplication + * @param BN_Src2 [in]second element of multipliation + * @param BN_Modulus [in]modular m * - * @return CRYPTO_SUCCESS if no error occured - * \n CRYPTO_MEMORY_ALLOC_FAIL if malloc is failed + * @return CRYPTO_SUCCESS if no error occured + * \n CRYPTO_MEMORY_ALLOC_FAIL if malloc is failed */ -int SDRM_BN_ModMul(SDRM_BIG_NUM *BN_Dst, SDRM_BIG_NUM *BN_Src1, SDRM_BIG_NUM *BN_Src2, SDRM_BIG_NUM *BN_Modulus) +int SDRM_BN_ModMul(SDRM_BIG_NUM *BN_Dst, SDRM_BIG_NUM *BN_Src1, + SDRM_BIG_NUM *BN_Src2, SDRM_BIG_NUM *BN_Modulus) { - int ret; + int ret; SDRM_BIG_NUM Dest; - cc_u32 *Value = (cc_u32*)malloc(SDRM_SIZE_OF_DWORD * (MAX3(BN_Src1->Size, BN_Src2->Size, BN_Modulus->Size) + 2)); + cc_u32 *Value = (cc_u32 *)malloc(SDRM_SIZE_OF_DWORD * (MAX3(BN_Src1->Size, + BN_Src2->Size, BN_Modulus->Size) + 2)); if (!Value) - { return CRYPTO_MEMORY_ALLOC_FAIL; - } - memset(Value, 0x00, SDRM_SIZE_OF_DWORD * (MAX3(BN_Src1->Size, BN_Src2->Size, BN_Modulus->Size) + 2)); + memset(Value, 0x00, SDRM_SIZE_OF_DWORD * (MAX3(BN_Src1->Size, BN_Src2->Size, + BN_Modulus->Size) + 2)); - SDRM_DWD_Mul(Value, BN_Src1->pData, BN_Src1->Length, BN_Src2->pData, BN_Src2->Length); + SDRM_DWD_Mul(Value, BN_Src1->pData, BN_Src1->Length, BN_Src2->pData, + BN_Src2->Length); - ret = SDRM_DWD_Classical_REDC(Value, BN_Src1->Length + BN_Src2->Length, BN_Modulus->pData, BN_Modulus->Length); - if (ret != CRYPTO_SUCCESS) - { + ret = SDRM_DWD_Classical_REDC(Value, BN_Src1->Length + BN_Src2->Length, + BN_Modulus->pData, BN_Modulus->Length); + + if (ret != CRYPTO_SUCCESS) { free(Value); return ret; } - Dest.sign = (BN_Src1->sign == BN_Src2->sign)? 0 : 1; + Dest.sign = (BN_Src1->sign == BN_Src2->sign) ? 0 : 1; Dest.Length = BN_Modulus->Length; Dest.pData = Value; @@ -1892,44 +1717,42 @@ int SDRM_BN_ModMul(SDRM_BIG_NUM *BN_Dst, SDRM_BIG_NUM *BN_Src1, SDRM_BIG_NUM *BN } /* - * @fn SDRM_BN_ModInv - * @brief Big Number Modular Inverse + * @fn SDRM_BN_ModInv + * @brief Big Number Modular Inverse * - * @param BN_Dest [out]destination - * @param BN_Src [in]soure - * @param BN_Modulus [in]modular m + * @param BN_Dest [out]destination + * @param BN_Src [in]soure + * @param BN_Modulus [in]modular m * - * @return CRYPTO_SUCCESS if no error occured - * \n CRYPTO_MEMORY_ALLOC_FAIL if malloc is failed - * \n CRYPTO_NEGATIVE_INPUT if source is negative value - * \n CRYPTO_INVERSE_NOT_EXIST if inverse is not exists + * @return CRYPTO_SUCCESS if no error occured + * \n CRYPTO_MEMORY_ALLOC_FAIL if malloc is failed + * \n CRYPTO_NEGATIVE_INPUT if source is negative value + * \n CRYPTO_INVERSE_NOT_EXIST if inverse is not exists */ -int SDRM_BN_ModInv(SDRM_BIG_NUM *BN_Dest, SDRM_BIG_NUM *BN_Src, SDRM_BIG_NUM *BN_Modulus) +int SDRM_BN_ModInv(SDRM_BIG_NUM *BN_Dest, SDRM_BIG_NUM *BN_Src, + SDRM_BIG_NUM *BN_Modulus) { - SDRM_BIG_NUM *BN_G0, *BN_G1, *BN_V0, *BN_V1, *BN_Y, *BN_Temp1, *BN_Temp2; - cc_u8 *pbBuf = NULL; - cc_u32 dSize, dAllocSize; + SDRM_BIG_NUM *BN_G0, *BN_G1, *BN_V0, *BN_V1, *BN_Y, *BN_Temp1, *BN_Temp2; + cc_u8 *pbBuf = NULL; + cc_u32 dSize, dAllocSize; dSize = MAX2(BN_Src->Size, BN_Modulus->Size); dAllocSize = sizeof(SDRM_BIG_NUM) + dSize * SDRM_SIZE_OF_DWORD; - pbBuf = (cc_u8*)malloc(dAllocSize * 7); + pbBuf = (cc_u8 *)malloc(dAllocSize * 7); if (!pbBuf) - { return CRYPTO_MEMORY_ALLOC_FAIL; - } BN_G0 = SDRM_BN_Alloc(pbBuf, dSize); - BN_G1 = SDRM_BN_Alloc((cc_u8*)BN_G0 + dAllocSize, dSize); - BN_V0 = SDRM_BN_Alloc((cc_u8*)BN_G1 + dAllocSize, dSize); - BN_V1 = SDRM_BN_Alloc((cc_u8*)BN_V0 + dAllocSize, dSize); - BN_Y = SDRM_BN_Alloc((cc_u8*)BN_V1 + dAllocSize, dSize); - BN_Temp1 = SDRM_BN_Alloc((cc_u8*)BN_Y + dAllocSize, dSize); - BN_Temp2 = SDRM_BN_Alloc((cc_u8*)BN_Temp1 + dAllocSize, dSize); - - if (BN_Src->sign) - { + BN_G1 = SDRM_BN_Alloc((cc_u8 *)BN_G0 + dAllocSize, dSize); + BN_V0 = SDRM_BN_Alloc((cc_u8 *)BN_G1 + dAllocSize, dSize); + BN_V1 = SDRM_BN_Alloc((cc_u8 *)BN_V0 + dAllocSize, dSize); + BN_Y = SDRM_BN_Alloc((cc_u8 *)BN_V1 + dAllocSize, dSize); + BN_Temp1 = SDRM_BN_Alloc((cc_u8 *)BN_Y + dAllocSize, dSize); + BN_Temp2 = SDRM_BN_Alloc((cc_u8 *)BN_Temp1 + dAllocSize, dSize); + + if (BN_Src->sign) { free(pbBuf); return CRYPTO_NEGATIVE_INPUT; } @@ -1944,10 +1767,8 @@ int SDRM_BN_ModInv(SDRM_BIG_NUM *BN_Dest, SDRM_BIG_NUM *BN_Src, SDRM_BIG_NUM *BN SDRM_BN_Clr(BN_Y); SDRM_BN_Clr(BN_Dest); - while(SDRM_BN_Cmp(BN_G1, BN_Zero)) - { - if (!SDRM_BN_Cmp(BN_G1, BN_One)) - { + while (SDRM_BN_Cmp(BN_G1, BN_Zero)) { + if (!SDRM_BN_Cmp(BN_G1, BN_One)) { SDRM_BN_Copy(BN_Dest, BN_V1); SDRM_BN_OPTIMIZE_LENGTH(BN_Dest); free(pbBuf); @@ -1957,7 +1778,8 @@ int SDRM_BN_ModInv(SDRM_BIG_NUM *BN_Dest, SDRM_BIG_NUM *BN_Src, SDRM_BIG_NUM *BN SDRM_BN_Clr(BN_Y); SDRM_BN_Clr(BN_Temp1); - SDRM_DWD_Div(BN_Y->pData, BN_Temp1->pData, BN_G0->pData, BN_G0->Length, BN_G1->pData, BN_G1->Length); + SDRM_DWD_Div(BN_Y->pData, BN_Temp1->pData, BN_G0->pData, BN_G0->Length, + BN_G1->pData, BN_G1->Length); BN_Y->Length = BN_G0->Length; SDRM_BN_OPTIMIZE_LENGTH(BN_Y); @@ -1967,29 +1789,25 @@ int SDRM_BN_ModInv(SDRM_BIG_NUM *BN_Dest, SDRM_BIG_NUM *BN_Src, SDRM_BIG_NUM *BN SDRM_BN_OPTIMIZE_LENGTH(BN_G0); SDRM_BN_Clr(BN_Temp1); - SDRM_DWD_Mul(BN_Temp1->pData, BN_Y->pData, BN_Y->Length, BN_V1->pData, BN_V1->Length); + SDRM_DWD_Mul(BN_Temp1->pData, BN_Y->pData, BN_Y->Length, BN_V1->pData, + BN_V1->Length); BN_Temp1->Length = BN_Y->Length + BN_V1->Length; SDRM_BN_OPTIMIZE_LENGTH(BN_Temp1); SDRM_BN_Clr(BN_Temp2); + if (SDRM_BN_Cmp(BN_V0, BN_Temp1) >= 0) - { SDRM_BN_Add(BN_Temp2, BN_V0, BN_Temp1); - } + else - { SDRM_BN_Add(BN_Temp2, BN_Temp1, BN_V0); - } SDRM_BN_Copy(BN_V0, BN_Temp2); if (!SDRM_BN_Cmp(BN_G0, BN_Zero)) - { break; - } - if (!SDRM_BN_Cmp(BN_G0, BN_One)) - { + if (!SDRM_BN_Cmp(BN_G0, BN_One)) { SDRM_BN_Sub(BN_Dest, BN_Modulus, BN_V0); SDRM_BN_OPTIMIZE_LENGTH(BN_Dest); free(pbBuf); @@ -1999,7 +1817,8 @@ int SDRM_BN_ModInv(SDRM_BIG_NUM *BN_Dest, SDRM_BIG_NUM *BN_Src, SDRM_BIG_NUM *BN SDRM_BN_Clr(BN_Y); SDRM_BN_Clr(BN_Temp1); - SDRM_DWD_Div(BN_Y->pData, BN_Temp1->pData, BN_G1->pData, BN_G1->Length, BN_G0->pData, BN_G0->Length); + SDRM_DWD_Div(BN_Y->pData, BN_Temp1->pData, BN_G1->pData, BN_G1->Length, + BN_G0->pData, BN_G0->Length); BN_Y->Length = BN_G1->Length; SDRM_BN_OPTIMIZE_LENGTH(BN_Y); @@ -2009,19 +1828,18 @@ int SDRM_BN_ModInv(SDRM_BIG_NUM *BN_Dest, SDRM_BIG_NUM *BN_Src, SDRM_BIG_NUM *BN SDRM_BN_OPTIMIZE_LENGTH(BN_G1); SDRM_BN_Clr(BN_Temp1); - SDRM_DWD_Mul(BN_Temp1->pData, BN_Y->pData, BN_Y->Length, BN_V0->pData, BN_V0->Length); + SDRM_DWD_Mul(BN_Temp1->pData, BN_Y->pData, BN_Y->Length, BN_V0->pData, + BN_V0->Length); BN_Temp1->Length = BN_Y->Length + BN_V0->Length; - SDRM_BN_OPTIMIZE_LENGTH(BN_Temp1); + SDRM_BN_OPTIMIZE_LENGTH(BN_Temp1); SDRM_BN_Clr(BN_Temp2); + if (SDRM_BN_Cmp(BN_V1, BN_Temp1) >= 0) - { SDRM_BN_Add(BN_Temp2, BN_V1, BN_Temp1); - } + else - { SDRM_BN_Add(BN_Temp2, BN_Temp1, BN_V1); - } SDRM_BN_Copy(BN_V1, BN_Temp2); } @@ -2033,23 +1851,23 @@ int SDRM_BN_ModInv(SDRM_BIG_NUM *BN_Dest, SDRM_BIG_NUM *BN_Src, SDRM_BIG_NUM *BN } /* - * @fn SDRM_MONT_Rzn2zn - * @brief Convert Montgomery number to noraml number + * @fn SDRM_MONT_Rzn2zn + * @brief Convert Montgomery number to noraml number * - * @param BN_Dst [out]destination, normal number - * @param BN_Src1 [in]source, montgomery number - * @param Mont [in]montgomery parameters + * @param BN_Dst [out]destination, normal number + * @param BN_Src1 [in]source, montgomery number + * @param Mont [in]montgomery parameters * - * @return CRYPTO_SUCCESS if no error occured + * @return CRYPTO_SUCCESS if no error occured */ -int SDRM_MONT_Rzn2zn(SDRM_BIG_NUM *BN_Dst, SDRM_BIG_NUM *BN_Src1, SDRM_BIG_MONT *Mont) +int SDRM_MONT_Rzn2zn(SDRM_BIG_NUM *BN_Dst, SDRM_BIG_NUM *BN_Src1, + SDRM_BIG_MONT *Mont) { - cc_u32 Src1_Len, Mod_Len, ri, i; - cc_u32 carry; - SDRM_BIG_NUM *Src1 = NULL; + cc_u32 Src1_Len, Mod_Len, ri, i; + cc_u32 carry; + SDRM_BIG_NUM *Src1 = NULL; - if (!BN_Src1->Length) - { + if (!BN_Src1->Length) { BN_Dst->Length = 0; return CRYPTO_SUCCESS; @@ -2059,14 +1877,13 @@ int SDRM_MONT_Rzn2zn(SDRM_BIG_NUM *BN_Dst, SDRM_BIG_NUM *BN_Src1, SDRM_BIG_MONT Mod_Len = Mont->Mod->Length + 1; Src1 = SDRM_BN_Init(BN_Src1->Size + Mod_Len); - if(Src1 == NULL)//fixed prevent cid=89093 by guoxing.xu - { - return CRYPTO_ERROR; - } + + if (Src1 == NULL) //fixed prevent cid=89093 by guoxing.xu + return CRYPTO_ERROR; + SDRM_BN_Copy(Src1, BN_Src1); - if (!Src1_Len || !Mod_Len) - { + if (!Src1_Len || !Mod_Len) { BN_Dst->Length = 0; BN_Dst->pData[0] = 0; SDRM_BN_FREE(Src1); @@ -2076,29 +1893,30 @@ int SDRM_MONT_Rzn2zn(SDRM_BIG_NUM *BN_Dst, SDRM_BIG_NUM *BN_Src1, SDRM_BIG_MONT Src1->sign = BN_Src1->sign ^ Mont->Mod->sign; - memset(Src1->pData + Src1->Length, 0, (Mod_Len + BN_Src1->Length - Src1->Length) * SDRM_SIZE_OF_DWORD); + memset(Src1->pData + Src1->Length, 0, + (Mod_Len + BN_Src1->Length - Src1->Length) * SDRM_SIZE_OF_DWORD); Src1->Length = Mod_Len + BN_Src1->Length; - for (i = 0; i < Mod_Len; i++) - { - if ((carry = SDRM_DWD_MulAdd(Src1->pData + i, Src1->Length - i, Mont->Mod->pData, Mod_Len, (cc_u32)Src1->pData[i] * Mont->N0))) - { - Src1->pData[Src1->Length++] = carry; //Added by Park Ji soon, 05-03-2006 - } // (cc_u32)A.pData[i]*modulus_p <== u=a[i]*m' mod b - // A=A+ (A.pData[i]*modulus_p* modulus[i])*b^i; + for (i = 0; i < Mod_Len; i++) { + if ((carry = SDRM_DWD_MulAdd(Src1->pData + i, Src1->Length - i, + Mont->Mod->pData, Mod_Len, (cc_u32)Src1->pData[i] * Mont->N0))) { + Src1->pData[Src1->Length++] = + carry; //Added by Park Ji soon, 05-03-2006 + } // (cc_u32)A.pData[i]*modulus_p <== u=a[i]*m' mod b + + // A=A+ (A.pData[i]*modulus_p* modulus[i])*b^i; } + SDRM_BN_OPTIMIZE_LENGTH(Src1); SDRM_BN_SHR(BN_Dst, Src1, (Mod_Len) * 32); //BN_Dst->Length = Src1->Length - ri; - BN_Dst->Length = Src1->Length - ri- 1;//Added by yhhwang + BN_Dst->Length = Src1->Length - ri - 1; //Added by yhhwang //if (SDRM_BN_Cmp(BN_Dst, Mont->Mod) >= 0) while (SDRM_BN_Cmp(BN_Dst, Mont->Mod) >= 0) - { SDRM_BN_Sub(BN_Dst, BN_Dst, Mont->Mod); - } SDRM_BN_FREE(Src1); @@ -2106,17 +1924,18 @@ int SDRM_MONT_Rzn2zn(SDRM_BIG_NUM *BN_Dst, SDRM_BIG_NUM *BN_Src1, SDRM_BIG_MONT } /* - * @fn SDRM_MONT_Mul - * @brief Montgomery Multiplication + * @fn SDRM_MONT_Mul + * @brief Montgomery Multiplication * - * @param BN_Dst [out]destination, montgomery number - * @param BN_Src1 [in]first element, montgomery number - * @param BN_Src2 [in]second element, montgomery number - * @param Mont [in]montgomery parameters + * @param BN_Dst [out]destination, montgomery number + * @param BN_Src1 [in]first element, montgomery number + * @param BN_Src2 [in]second element, montgomery number + * @param Mont [in]montgomery parameters * - * @return CRYPTO_SUCCESS if no error occured + * @return CRYPTO_SUCCESS if no error occured */ -int SDRM_MONT_Mul(SDRM_BIG_NUM *BN_Dst, SDRM_BIG_NUM *BN_Src1, SDRM_BIG_NUM *BN_Src2, SDRM_BIG_MONT *Mont) +int SDRM_MONT_Mul(SDRM_BIG_NUM *BN_Dst, SDRM_BIG_NUM *BN_Src1, + SDRM_BIG_NUM *BN_Src2, SDRM_BIG_MONT *Mont) { int ret; @@ -2124,47 +1943,46 @@ int SDRM_MONT_Mul(SDRM_BIG_NUM *BN_Dst, SDRM_BIG_NUM *BN_Src1, SDRM_BIG_NUM *BN_ /* if (SDRM_BN_Cmp(BN_Src1, Mont->Mod) >= 0) { - ret = SDRM_BN_ModRed(BN_Src1, BN_Src1, Mont->Mod); - if (ret != CRYPTO_SUCCESS) - { + ret = SDRM_BN_ModRed(BN_Src1, BN_Src1, Mont->Mod); + if (ret != CRYPTO_SUCCESS) + { return ret; - } + } } else if ( BN_Src1->sign == 1) { - printf("Minus Value\n"); - ret = SDRM_BN_Add(BN_Src1, BN_Src1, Mont->Mod); - if (BN_Src1->sign == 1) - { + printf("Minus Value\n"); + ret = SDRM_BN_Add(BN_Src1, BN_Src1, Mont->Mod); + if (BN_Src1->sign == 1) + { printf("Value Fail.\n"); return CRYPTO_ERROR; - } + } } if (SDRM_BN_Cmp(BN_Src2, Mont->Mod) >= 0) { - ret = SDRM_BN_ModRed(BN_Src2, BN_Src2, Mont->Mod); - if (ret != CRYPTO_SUCCESS) - { + ret = SDRM_BN_ModRed(BN_Src2, BN_Src2, Mont->Mod); + if (ret != CRYPTO_SUCCESS) + { return ret; - } + } } else if ( BN_Src2->sign == 1) { - printf("Minus Value\n"); - ret = SDRM_BN_Add(BN_Src2, BN_Src2, Mont->Mod); - if (BN_Src2->sign == 1) - { + printf("Minus Value\n"); + ret = SDRM_BN_Add(BN_Src2, BN_Src2, Mont->Mod); + if (BN_Src2->sign == 1) + { printf("Value Fail.\n"); return CRYPTO_ERROR; - } + } } */ /* End - Add to test input range by Yong Ho Hwang (20120809) */ ret = SDRM_BN_Mul(BN_Dst, BN_Src1, BN_Src2); + if (ret != CRYPTO_SUCCESS) - { return ret; - } ret = SDRM_MONT_Rzn2zn(BN_Dst, BN_Dst, Mont); @@ -2172,10 +1990,10 @@ int SDRM_MONT_Mul(SDRM_BIG_NUM *BN_Dst, SDRM_BIG_NUM *BN_Src1, SDRM_BIG_NUM *BN_ /* if (SDRM_BN_Cmp(BN_Dst, Mont->Mod) >= 0) { - printf("Output is bigger than Mod\n"); + printf("Output is bigger than Mod\n"); } else if ( BN_Dst->sign == 1) { - printf("Minus Value\n"); + printf("Minus Value\n"); } */ /* End - Add to test input range by Yong Ho Hwang (20120809) */ @@ -2184,98 +2002,91 @@ int SDRM_MONT_Mul(SDRM_BIG_NUM *BN_Dst, SDRM_BIG_NUM *BN_Src1, SDRM_BIG_NUM *BN_ } /* - * @fn SDRM_MONT_Set - * @brief Set Montgomery parameters + * @fn SDRM_MONT_Set + * @brief Set Montgomery parameters * - * @param Mont [out]montgomery parameter - * @param BN_Modulus [in]modular m + * @param Mont [out]montgomery parameter + * @param BN_Modulus [in]modular m * - * @return CRYPTO_SUCCESS if no error occured - * \n BN_NOT_ENOUGHT_BUFFER if malloc is failed - * \n CRYPTO_INVERSE_NOT_EXIST if inverse is not exists + * @return CRYPTO_SUCCESS if no error occured + * \n BN_NOT_ENOUGHT_BUFFER if malloc is failed + * \n CRYPTO_INVERSE_NOT_EXIST if inverse is not exists */ int SDRM_MONT_Set(SDRM_BIG_MONT *Mont, SDRM_BIG_NUM *BN_Modulus) { - SDRM_BIG_NUM *Ri, *R; - SDRM_BIG_NUM *temp, *Rsquare; - cc_u8 *pbBuf; - cc_u32 buf[2], dSize, dAllocSize, r2Size; + SDRM_BIG_NUM *Ri, *R; + SDRM_BIG_NUM *temp, *Rsquare; + cc_u8 *pbBuf; + cc_u32 buf[2], dSize, dAllocSize, r2Size; if ((Mont == NULL) || (BN_Modulus == NULL)) - { return CRYPTO_INVALID_ARGUMENT; - } if (Mont->R == NULL) - { Mont->R = SDRM_BN_Init(BN_Modulus->Size); - } - if (Mont->R == NULL)//fix prevent 89095 by guoxing.xu - { - return CRYPTO_MEMORY_ALLOC_FAIL; - } + + if (Mont->R == NULL) //fix prevent 89095 by guoxing.xu + return CRYPTO_MEMORY_ALLOC_FAIL; + if (Mont->Mod == NULL) - { Mont->Mod = SDRM_BN_Init(BN_Modulus->Size); + + if (Mont->Mod == NULL) { //fix prevent 89-95 by guoxing.xu + SDRM_BN_FREE(Mont->R); + return CRYPTO_MEMORY_ALLOC_FAIL; } - if (Mont->Mod == NULL)//fix prevent 89-95 by guoxing.xu - { - SDRM_BN_FREE(Mont->R); - return CRYPTO_MEMORY_ALLOC_FAIL; - } + if (SDRM_BN_Cmp(Mont->Mod, BN_Modulus) == 0) - { return CRYPTO_SUCCESS; - } dSize = BN_Modulus->Size + 1; dAllocSize = sizeof(SDRM_BIG_NUM) + dSize * SDRM_SIZE_OF_DWORD; - if (!(pbBuf = (cc_u8*)malloc(dAllocSize * 3))) - { + + if (!(pbBuf = (cc_u8 *)malloc(dAllocSize * 3))) return CRYPTO_MEMORY_ALLOC_FAIL; - } - Ri = SDRM_BN_Alloc( pbBuf, dSize); - R = SDRM_BN_Alloc((cc_u8*)Ri + dAllocSize, dSize); - temp = SDRM_BN_Alloc((cc_u8*)R + dAllocSize, dSize); + Ri = SDRM_BN_Alloc(pbBuf, dSize); + R = SDRM_BN_Alloc((cc_u8 *)Ri + dAllocSize, dSize); + temp = SDRM_BN_Alloc((cc_u8 *)R + dAllocSize, dSize); -//++ 2012.08.20 - modified by yhhwang to apply R=2^(160+32) -/* == DELETED == - SDRM_BN_Copy(Mont->Mod, BN_Modulus); + //++ 2012.08.20 - modified by yhhwang to apply R=2^(160+32) + /* == DELETED == + SDRM_BN_Copy(Mont->Mod, BN_Modulus); - Mont->ri = (SDRM_BN_num_bits(BN_Modulus) + (SDRM_BitsInDWORD - 1)) / SDRM_BitsInDWORD * SDRM_BitsInDWORD; + Mont->ri = (SDRM_BN_num_bits(BN_Modulus) + (SDRM_BitsInDWORD - 1)) / SDRM_BitsInDWORD * SDRM_BitsInDWORD; - SDRM_BN_SHL(R, BN_One, SDRM_BitsInDWORD); + SDRM_BN_SHL(R, BN_One, SDRM_BitsInDWORD); - buf[0] = BN_Modulus->pData[0]; - buf[1] = 0; - temp->pData[0] = buf[0]; - temp->Length = 1; - temp->sign = BN_Modulus->sign; + buf[0] = BN_Modulus->pData[0]; + buf[1] = 0; + temp->pData[0] = buf[0]; + temp->Length = 1; + temp->sign = BN_Modulus->sign; - SDRM_BN_ModInv(Ri, R, temp); - if (Ri == NULL) - { - free(pbBuf); + SDRM_BN_ModInv(Ri, R, temp); + if (Ri == NULL) + { + free(pbBuf); - return CRYPTO_INVERSE_NOT_EXIST; - } + return CRYPTO_INVERSE_NOT_EXIST; + } - SDRM_BN_SHL(Ri, Ri, SDRM_BitsInDWORD); - SDRM_BN_Sub(Ri, Ri, BN_One); - SDRM_BN_Div(Ri, NULL, Ri, temp); - SDRM_BN_Copy(Mont->Inv_Mod, Ri); - Mont->N0 = Ri->pData[0]; + SDRM_BN_SHL(Ri, Ri, SDRM_BitsInDWORD); + SDRM_BN_Sub(Ri, Ri, BN_One); + SDRM_BN_Div(Ri, NULL, Ri, temp); + SDRM_BN_Copy(Mont->Inv_Mod, Ri); + Mont->N0 = Ri->pData[0]; - SDRM_BN_SHL(Mont->R, BN_One, 2 * (32 + Mont->ri)); - SDRM_BN_ModRed(Mont->R, Mont->R, Mont->Mod); -*/ + SDRM_BN_SHL(Mont->R, BN_One, 2 * (32 + Mont->ri)); + SDRM_BN_ModRed(Mont->R, Mont->R, Mont->Mod); + */ -// == NEW CODE == + // == NEW CODE == SDRM_BN_Copy(Mont->Mod, BN_Modulus); Mont->Mod->pData[Mont->Mod->Length] = 0; - Mont->ri = (SDRM_BN_num_bits(BN_Modulus) + (SDRM_BitsInDWORD - 1)) / SDRM_BitsInDWORD * SDRM_BitsInDWORD; + Mont->ri = (SDRM_BN_num_bits(BN_Modulus) + (SDRM_BitsInDWORD - 1)) / + SDRM_BitsInDWORD * SDRM_BitsInDWORD; SDRM_BN_SHL(R, BN_One, SDRM_BitsInDWORD); @@ -2293,8 +2104,8 @@ int SDRM_MONT_Set(SDRM_BIG_MONT *Mont, SDRM_BIG_NUM *BN_Modulus) r2Size = 2 * (SDRM_BitsInDWORD + Mont->ri); Rsquare = SDRM_BN_Init(r2Size / SDRM_BitsInDWORD + 1); - if (Rsquare == NULL) - { + + if (Rsquare == NULL) { free(pbBuf); return CRYPTO_MEMORY_ALLOC_FAIL; } @@ -2302,7 +2113,7 @@ int SDRM_MONT_Set(SDRM_BIG_MONT *Mont, SDRM_BIG_NUM *BN_Modulus) // Compute R and R^2 mod M SDRM_BN_SHL(Rsquare, BN_One, r2Size); SDRM_BN_ModRed(Mont->R, Rsquare, BN_Modulus); -//-- 2012.08.20 - modified by yhhwang + //-- 2012.08.20 - modified by yhhwang free(pbBuf); free(Rsquare); @@ -2311,57 +2122,53 @@ int SDRM_MONT_Set(SDRM_BIG_MONT *Mont, SDRM_BIG_NUM *BN_Modulus) } /* - * @fn SDRM_MONT_Init - * @brief Allocate new momory for Montgomery parameter + * @fn SDRM_MONT_Init + * @brief Allocate new momory for Montgomery parameter * - * @param dSize [in]size of buffer of big number + * @param dSize [in]size of buffer of big number * - * @return Pointer to created structure - * \n NULL if malloc failed + * @return Pointer to created structure + * \n NULL if malloc failed */ SDRM_BIG_MONT *SDRM_MONT_Init(cc_u32 dSize) { - SDRM_BIG_MONT *Mont; - cc_u32 AllocSiz = sizeof(SDRM_BIG_NUM) + dSize * SDRM_SIZE_OF_DWORD; + SDRM_BIG_MONT *Mont; + cc_u32 AllocSiz = sizeof(SDRM_BIG_NUM) + dSize * SDRM_SIZE_OF_DWORD; Mont = (SDRM_BIG_MONT *)malloc(sizeof(SDRM_BIG_MONT) + AllocSiz * 3); + if (Mont == NULL) - { return NULL; - } - Mont->ri = 0; - Mont->R = SDRM_BN_Alloc((cc_u8*)Mont + sizeof(SDRM_BIG_MONT), dSize); - Mont->Mod = SDRM_BN_Alloc((cc_u8*)Mont->R + AllocSiz, dSize); - Mont->Inv_Mod = SDRM_BN_Alloc((cc_u8*)Mont->Mod + AllocSiz, dSize); + Mont->ri = 0; + Mont->R = SDRM_BN_Alloc((cc_u8 *)Mont + sizeof(SDRM_BIG_MONT), dSize); + Mont->Mod = SDRM_BN_Alloc((cc_u8 *)Mont->R + AllocSiz, dSize); + Mont->Inv_Mod = SDRM_BN_Alloc((cc_u8 *)Mont->Mod + AllocSiz, dSize); return Mont; } /* - * @fn SDRM_MONT_Free - * @brief Free allocated memory for montgomery paramter + * @fn SDRM_MONT_Free + * @brief Free allocated memory for montgomery paramter * - * @param Mont [in]montgomery parameters + * @param Mont [in]montgomery parameters * - * @return void + * @return void */ void SDRM_MONT_Free(SDRM_BIG_MONT *Mont) { - if (Mont != NULL) { + if (Mont != NULL) free(Mont); - } } int SDRM_BN_num_bits_index(SDRM_BIG_NUM *BN_Src, cc_u32 bitIndex) { - cc_u32 l; + cc_u32 l; int j; if (BN_Src->Length == 0) - { return 0; - } int div = bitIndex / (sizeof(cc_u32) * 8); int rem = bitIndex % (sizeof(cc_u32) * 8); @@ -2373,77 +2180,70 @@ int SDRM_BN_num_bits_index(SDRM_BIG_NUM *BN_Src, cc_u32 bitIndex) int SDRM_UINT32_num_bits_index(cc_u32 *pdSrc, cc_u32 bitIndex) { - cc_u32 i = 0; - cc_u32 temp; + cc_u32 i = 0; + cc_u32 temp; temp = *pdSrc; if (!temp) - { return 0; - } - while(temp) - { - if(bitIndex == i) { + while (temp) { + if (bitIndex == i) break; - } + temp >>= 1; i++; } + return temp & 0x00000001; } /* - * @fn SDRM_BN_num_bits - * @brief Calc bit-length of Big Number + * @fn SDRM_BN_num_bits + * @brief Calc bit-length of Big Number * - * @param BN_Src [in]source + * @param BN_Src [in]source * - * @return bit-length + * @return bit-length */ int SDRM_BN_num_bits(SDRM_BIG_NUM *BN_Src) { - cc_u32 l; - int i, j; + cc_u32 l; + int i, j; if (BN_Src->Length == 0) - { return 0; - } l = BN_Src->pData[BN_Src->Length - 1]; - i = (BN_Src->Length-1) * SDRM_BitsInDWORD; + i = (BN_Src->Length - 1) * SDRM_BitsInDWORD; j = SDRM_UINT32_num_bits(&l); - return(i + j); + return (i + j); } /* - * @fn SDRM_UINT32_num_bits - * @brief Calc bit-length of cc_u32 + * @fn SDRM_UINT32_num_bits + * @brief Calc bit-length of cc_u32 * - * @param pdSrc [in]source + * @param pdSrc [in]source * - * @return bit-length + * @return bit-length */ -int SDRM_UINT32_num_bits(cc_u32 *pdSrc) +int SDRM_UINT32_num_bits(cc_u32 *pdSrc) { - int i = 0; - cc_u32 temp; + int i = 0; + cc_u32 temp; temp = *pdSrc; if (!temp) - { return 0; - } - while(temp) - { + while (temp) { temp >>= 1; i++; } @@ -2452,24 +2252,21 @@ int SDRM_UINT32_num_bits(cc_u32 *pdSrc) } /* - * @fn SDRM_INT_num_bits - * @brief Calc bit-length of integer + * @fn SDRM_INT_num_bits + * @brief Calc bit-length of integer * - * @param Src [in]source + * @param Src [in]source * - * @return bit-length + * @return bit-length */ -int SDRM_INT_num_bits(int Src) +int SDRM_INT_num_bits(int Src) { int i = 0; if (!Src) - { return 0; - } - while(Src) - { + while (Src) { Src >>= 1; i++; } @@ -2478,50 +2275,46 @@ int SDRM_INT_num_bits(int Src) } /* - * @fn SDRM_BN_ModExp - * @brief Big Number Modular Exponentiation + * @fn SDRM_BN_ModExp + * @brief Big Number Modular Exponentiation * - * @param BN_Dst [out]destination - * @param BN_Base [in]base - * @param BN_Exponent [in]exponent - * @param BN_Modulus [in]modular m + * @param BN_Dst [out]destination + * @param BN_Base [in]base + * @param BN_Exponent [in]exponent + * @param BN_Modulus [in]modular m * - * @return CRYPTO_SUCCESS if no error occured - * \n CRYPTO_MEMORY_ALLOC_FAIL if malloc is failed - * \n CRYPTO_ERROR if evaluation is failed + * @return CRYPTO_SUCCESS if no error occured + * \n CRYPTO_MEMORY_ALLOC_FAIL if malloc is failed + * \n CRYPTO_ERROR if evaluation is failed */ -int SDRM_BN_ModExp(SDRM_BIG_NUM *BN_Dst, SDRM_BIG_NUM *BN_Base, SDRM_BIG_NUM *BN_Exponent, SDRM_BIG_NUM *BN_Modulus) +int SDRM_BN_ModExp(SDRM_BIG_NUM *BN_Dst, SDRM_BIG_NUM *BN_Base, + SDRM_BIG_NUM *BN_Exponent, SDRM_BIG_NUM *BN_Modulus) { - SDRM_BIG_NUM *c_, *a_, *BN_Temp; - SDRM_BIG_MONT *Mont; - int i, m; - cc_u8 *pbBuf; - cc_u32 dSize, dAllocSize; + SDRM_BIG_NUM *c_, *a_, *BN_Temp; + SDRM_BIG_MONT *Mont; + int i, m; + cc_u8 *pbBuf; + cc_u32 dSize, dAllocSize; dSize = MAX3(BN_Base->Size, BN_Exponent->Size, BN_Modulus->Size); dAllocSize = sizeof(SDRM_BIG_NUM) + dSize * SDRM_SIZE_OF_DWORD; - pbBuf = (cc_u8*)malloc(dAllocSize * 3); + pbBuf = (cc_u8 *)malloc(dAllocSize * 3); + if (pbBuf == NULL) - { return CRYPTO_MEMORY_ALLOC_FAIL; - } c_ = SDRM_BN_Alloc(pbBuf, dSize); - a_ = SDRM_BN_Alloc((cc_u8*)c_ + dAllocSize, dSize); - BN_Temp = SDRM_BN_Alloc((cc_u8*)a_ + dAllocSize, dSize); + a_ = SDRM_BN_Alloc((cc_u8 *)c_ + dAllocSize, dSize); + BN_Temp = SDRM_BN_Alloc((cc_u8 *)a_ + dAllocSize, dSize); if (SDRM_BN_Cmp(BN_Base, BN_Modulus) >= 0) - { SDRM_BN_ModRed(BN_Temp, BN_Base, BN_Modulus); - } + else - { BN_Temp = BN_Base; - } - if (SDRM_BN_Cmp(BN_Temp, BN_Zero) == 0) - { + if (SDRM_BN_Cmp(BN_Temp, BN_Zero) == 0) { SDRM_BN_Copy(BN_Dst, BN_Zero); free(pbBuf); @@ -2536,14 +2329,11 @@ int SDRM_BN_ModExp(SDRM_BIG_NUM *BN_Dst, SDRM_BIG_NUM *BN_Base, SDRM_BIG_NUM *BN m = SDRM_BN_num_bits(BN_Exponent); - for (i = m - 1; i >= 0; i--) - { + for (i = m - 1; i >= 0; i--) { SDRM_MONT_Mul(c_, c_, c_, Mont); if (SDRM_CheckBitUINT32(BN_Exponent->pData, i) == 1) - { SDRM_MONT_Mul(c_, c_, a_, Mont); - } } SDRM_MONT_Rzn2zn(BN_Dst, c_, Mont); @@ -2556,123 +2346,105 @@ int SDRM_BN_ModExp(SDRM_BIG_NUM *BN_Dst, SDRM_BIG_NUM *BN_Base, SDRM_BIG_NUM *BN } /* - * @fn SDRM_BN_ModExp2 - * @brief Big Number Modular Exponentiation2 - Karen's method + * @fn SDRM_BN_ModExp2 + * @brief Big Number Modular Exponentiation2 - Karen's method * - * @param BN_Dst [out]destination - * @param BN_Base [in]base - * @param BN_Exponent [in]exponent - * @param BN_Modulus [in]modular m + * @param BN_Dst [out]destination + * @param BN_Base [in]base + * @param BN_Exponent [in]exponent + * @param BN_Modulus [in]modular m * - * @return CRYPTO_SUCCESS if no error occured - * \n CRYPTO_MEMORY_ALLOC_FAIL if malloc is failed - * \n CRYPTO_ERROR if evaluation is failed + * @return CRYPTO_SUCCESS if no error occured + * \n CRYPTO_MEMORY_ALLOC_FAIL if malloc is failed + * \n CRYPTO_ERROR if evaluation is failed */ -int SDRM_BN_ModExp2(SDRM_BIG_NUM *BN_Dst, SDRM_BIG_NUM *BN_Base, SDRM_BIG_NUM *BN_Exponent, SDRM_BIG_NUM *BN_Modulus) +int SDRM_BN_ModExp2(SDRM_BIG_NUM *BN_Dst, SDRM_BIG_NUM *BN_Base, + SDRM_BIG_NUM *BN_Exponent, SDRM_BIG_NUM *BN_Modulus) { int retVal; SDRM_BIG_NUM *BN_Temp; if ((BN_Dst != BN_Base) && (BN_Dst != BN_Exponent) && (BN_Dst != BN_Modulus)) - { SDRM_BN_Clr(BN_Dst); - } - if (SDRM_BN_Cmp(BN_Base, BN_Modulus) >= 0) - { - BN_Temp = SDRM_BN_Init(MAX3(BN_Base->Size, BN_Exponent->Size, BN_Modulus->Size)); + if (SDRM_BN_Cmp(BN_Base, BN_Modulus) >= 0) { + BN_Temp = SDRM_BN_Init(MAX3(BN_Base->Size, BN_Exponent->Size, + BN_Modulus->Size)); + if (BN_Temp == NULL) - { return CRYPTO_MEMORY_ALLOC_FAIL; - } - if (BN_Temp == BN_Base) - { + if (BN_Temp == BN_Base) { free(BN_Temp); return CRYPTO_ERROR; } SDRM_BN_ModRed(BN_Temp, BN_Base, BN_Modulus); - } - else - { + } else BN_Temp = BN_Base; - } - if (SDRM_BN_Cmp(BN_Temp, BN_Zero) == 0) - { + if (SDRM_BN_Cmp(BN_Temp, BN_Zero) == 0) { SDRM_BN_Clr(BN_Dst); if (BN_Temp != BN_Base) - { free(BN_Temp); - } return CRYPTO_SUCCESS; } - retVal = SDRM_ll_ExpMod(BN_Temp->pData, BN_Temp->Length * 4, BN_Exponent->pData, BN_Exponent->Length * 4, BN_Modulus->pData, BN_Modulus->Length * 4, BN_Dst->pData); - if (retVal != CRYPTO_SUCCESS) - { + retVal = SDRM_ll_ExpMod(BN_Temp->pData, BN_Temp->Length * 4, BN_Exponent->pData, + BN_Exponent->Length * 4, BN_Modulus->pData, BN_Modulus->Length * 4, + BN_Dst->pData); + + if (retVal != CRYPTO_SUCCESS) { if (BN_Temp != BN_Base) - { free(BN_Temp); - } return retVal; } BN_Dst->Length = BN_Dst->Size; - while(BN_Dst->pData[BN_Dst->Length - 1] == 0) - { + while (BN_Dst->pData[BN_Dst->Length - 1] == 0) BN_Dst->Length--; - } if (BN_Temp != BN_Base) - { free(BN_Temp); - } return CRYPTO_SUCCESS; } /* - * @fn SDRM_BN_CheckRelativelyPrime - * @brief get gcd of two big number + * @fn SDRM_BN_CheckRelativelyPrime + * @brief get gcd of two big number * - * @param BN_Src1 [in]first element - * @param BN_Src2 [in]second element + * @param BN_Src1 [in]first element + * @param BN_Src2 [in]second element * - * @return CRYPTO_ISPRIME if two elements are relatively prime - * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed - * \n CRYPTO_ERROR otherwise + * @return CRYPTO_ISPRIME if two elements are relatively prime + * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed + * \n CRYPTO_ERROR otherwise */ int SDRM_BN_CheckRelativelyPrime(SDRM_BIG_NUM *BN_Src1, SDRM_BIG_NUM *BN_Src2) { - SDRM_BIG_NUM *Temp, *S1, *S2; - cc_u8 *pbBuf; - cc_u32 dSize, dAllocSize; + SDRM_BIG_NUM *Temp, *S1, *S2; + cc_u8 *pbBuf; + cc_u32 dSize, dAllocSize; dSize = MAX2(BN_Src1->Size, BN_Src2->Size); dAllocSize = sizeof(SDRM_BIG_NUM) + dSize * SDRM_SIZE_OF_DWORD; - if (!(pbBuf = (cc_u8*)malloc(dAllocSize * 3))) - { + if (!(pbBuf = (cc_u8 *)malloc(dAllocSize * 3))) return CRYPTO_MEMORY_ALLOC_FAIL; - } - S1 = SDRM_BN_Alloc(pbBuf, dSize); - S2 = SDRM_BN_Alloc((cc_u8*)S1 + dAllocSize, dSize); - Temp = SDRM_BN_Alloc((cc_u8*)S2 + dAllocSize, dSize); + S1 = SDRM_BN_Alloc(pbBuf, dSize); + S2 = SDRM_BN_Alloc((cc_u8 *)S1 + dAllocSize, dSize); + Temp = SDRM_BN_Alloc((cc_u8 *)S2 + dAllocSize, dSize); - if (SDRM_BN_Cmp(BN_Src1, BN_Src2) >= 0) - { + if (SDRM_BN_Cmp(BN_Src1, BN_Src2) >= 0) { SDRM_BN_Copy(S1, BN_Src1); SDRM_BN_Copy(S2, BN_Src2); - } - else - { + } else { SDRM_BN_Copy(S1, BN_Src2); SDRM_BN_Copy(S2, BN_Src1); } @@ -2680,15 +2452,13 @@ int SDRM_BN_CheckRelativelyPrime(SDRM_BIG_NUM *BN_Src1, SDRM_BIG_NUM *BN_Src2) SDRM_BN_OPTIMIZE_LENGTH(S1); SDRM_BN_OPTIMIZE_LENGTH(S2); - while(S2->Length) - { + while (S2->Length) { SDRM_BN_ModRed(Temp, S1, S2); SDRM_BN_Copy(S1, S2); SDRM_BN_Copy(S2, Temp); } - if (SDRM_BN_Cmp(S1, BN_One) == 0) - { + if (SDRM_BN_Cmp(S1, BN_One) == 0) { free(pbBuf); return CRYPTO_ISPRIME; @@ -2708,183 +2478,167 @@ static cc_u32 miniPrimes[] = { }; /* - * @fn SDRM_BN_MILLER_RABIN - * @brief MILLER_RABIN Test + * @fn SDRM_BN_MILLER_RABIN + * @brief MILLER_RABIN Test * - * @param n [in]value to test - * @param t [in]security parameter + * @param n [in]value to test + * @param t [in]security parameter * - * @return CRYPTO_ISPRIME if n is (probably) prime - * \n CRYPTO_INVALID_ARGUMENT if n is composite + * @return CRYPTO_ISPRIME if n is (probably) prime + * \n CRYPTO_INVALID_ARGUMENT if n is composite */ -int SDRM_BN_MILLER_RABIN(SDRM_BIG_NUM* n, cc_u32 t) +int SDRM_BN_MILLER_RABIN(SDRM_BIG_NUM *n, cc_u32 t) { - SDRM_BIG_NUM *r, *a, *y, *n1; - cc_u32 i, j, tmp, srcLen, s = 1; - cc_u8 *pbBuf; - cc_u32 dSize, dAllocSize; + SDRM_BIG_NUM *r, *a, *y, *n1; + cc_u32 i, j, tmp, srcLen, s = 1; + cc_u8 *pbBuf; + cc_u32 dSize, dAllocSize; dSize = n->Size; dAllocSize = sizeof(SDRM_BIG_NUM) + dSize * SDRM_SIZE_OF_DWORD; if (n->Length == 0) - { return CRYPTO_INVALID_ARGUMENT; - } if ((n->pData[0] & 0x01) == 0) - { return CRYPTO_INVALID_ARGUMENT; - } - for (i = 0; miniPrimes[i] != 0; i++) - { + for (i = 0; miniPrimes[i] != 0; i++) { tmp = 0; - for (j = n->Length - 1; j != (cc_u32)-1; j--) - { + + for (j = n->Length - 1; j != (cc_u32) - 1; j--) tmp = SDRM_DIGIT_Mod(tmp, n->pData[j], miniPrimes[i]); - } - if(SDRM_DIGIT_Gcd(miniPrimes[i], tmp) != 1) - { + if (SDRM_DIGIT_Gcd(miniPrimes[i], tmp) != 1) return CRYPTO_INVALID_ARGUMENT; - } } - while(SDRM_CheckBitUINT32(n->pData, s) == 0) { + while (SDRM_CheckBitUINT32(n->pData, s) == 0) s++; - } - pbBuf = (cc_u8*)malloc(dAllocSize * 4); + pbBuf = (cc_u8 *)malloc(dAllocSize * 4); + if (pbBuf == NULL) - { return CRYPTO_MEMORY_ALLOC_FAIL; - } - r = SDRM_BN_Alloc( pbBuf, dSize); - a = SDRM_BN_Alloc((cc_u8*)r + dAllocSize, dSize); - y = SDRM_BN_Alloc((cc_u8*)a + dAllocSize, dSize); - n1 = SDRM_BN_Alloc((cc_u8*)y + dAllocSize, dSize); + r = SDRM_BN_Alloc(pbBuf, dSize); + a = SDRM_BN_Alloc((cc_u8 *)r + dAllocSize, dSize); + y = SDRM_BN_Alloc((cc_u8 *)a + dAllocSize, dSize); + n1 = SDRM_BN_Alloc((cc_u8 *)y + dAllocSize, dSize); SDRM_BN_Sub(n1, n, BN_One); SDRM_BN_SHR(r, n1, s); srcLen = SDRM_BN_num_bits(n); - for (i = 1; i <= t; i++) - { + for (i = 1; i <= t; i++) { SDRM_BN_Rand(a, srcLen); a->pData[n->Length - 1] %= n->pData[n->Length - 1]; SDRM_BN_ModExp(y, a, r, n); + if ((SDRM_BN_Cmp(y, BN_One) == 0) || (SDRM_BN_Cmp(y, n1) == 0)) - { continue; - } - for (j = 1; (j < s) && SDRM_BN_Cmp(y, n1) != 0; j++) - { + for (j = 1; (j < s) && SDRM_BN_Cmp(y, n1) != 0; j++) { SDRM_BN_ModMul(y, y, y, n); - if (SDRM_BN_Cmp(y, BN_One) == 0) - { + if (SDRM_BN_Cmp(y, BN_One) == 0) { free(pbBuf); return CRYPTO_INVALID_ARGUMENT; } } - if (SDRM_BN_Cmp(y, n1) != 0) - { + if (SDRM_BN_Cmp(y, n1) != 0) { free(pbBuf); return CRYPTO_INVALID_ARGUMENT; } } + free(pbBuf); return CRYPTO_ISPRIME; } /* - * @fn int SDRM_HEX2BN(cc_u8* pbSrc, SDRM_BIG_NUM *BN_Dst) - * @brief Convert Hex String to Big Number + * @fn int SDRM_HEX2BN(cc_u8* pbSrc, SDRM_BIG_NUM *BN_Dst) + * @brief Convert Hex String to Big Number * - * @param pbSrc [in]source hex string - * @param BN_Dst [out]output big number + * @param pbSrc [in]source hex string + * @param BN_Dst [out]output big number * - * @return CRYPTO_SUCCESS if no error is occured - * \n CRYPTO_MEMORY_ALLOC_FAIL if arrary is too small + * @return CRYPTO_SUCCESS if no error is occured + * \n CRYPTO_MEMORY_ALLOC_FAIL if arrary is too small */ -int SDRM_HEX2BN(cc_u8* pbSrc, SDRM_BIG_NUM *BN_Dst) +int SDRM_HEX2BN(cc_u8 *pbSrc, SDRM_BIG_NUM *BN_Dst) { cc_u32 i, n, k, j; - cc_u8 * bufferHex = NULL; + cc_u8 *bufferHex = NULL; - n = (cc_u32)strlen((const char*)pbSrc); + n = (cc_u32)strlen((const char *)pbSrc); - if (!BN_Dst) - { + if (!BN_Dst) { BN_Dst = SDRM_BN_Init((n / SDRM_SIZE_BLOCK) * SDRM_SIZE_OF_DWORD * 8); - if(BN_Dst == NULL) - { + + if (BN_Dst == NULL) return CRYPTO_MEMORY_ALLOC_FAIL; - } } - if(pbSrc[0] == '-') - { + + if (pbSrc[0] == '-') { BN_Dst->sign = 1; pbSrc[0] = '0'; } BN_Dst->Length = n / SDRM_SIZE_BLOCK; + //normalize length - if( n % SDRM_SIZE_BLOCK != 0 ) { - BN_Dst->Length+=1; - } + if (n % SDRM_SIZE_BLOCK != 0) + BN_Dst->Length += 1; + #if 0 //fix prevent problem by guoxing.xu 20140826. move to before + if (!BN_Dst) - { BN_Dst = SDRM_BN_Init(BN_Dst->Length * SDRM_SIZE_OF_DWORD * 8); - } + #endif - for(i = 0; i < BN_Dst->Length ; i++) - { + + for (i = 0; i < BN_Dst->Length ; i++) BN_Dst->pData[i] = 0; - } //full string: bufferHex mod Length = 0 - bufferHex = (cc_u8 *)malloc( sizeof(cc_u8) * (BN_Dst->Length * SDRM_SIZE_BLOCK)); - if (bufferHex == NULL) { + bufferHex = (cc_u8 *)malloc(sizeof(cc_u8) * (BN_Dst->Length * SDRM_SIZE_BLOCK)); + + if (bufferHex == NULL) return CRYPTO_MEMORY_ALLOC_FAIL; - } //init byffer by 0 - for(i = 0; i < BN_Dst->Length * SDRM_SIZE_BLOCK; i++) - { + for (i = 0; i < BN_Dst->Length * SDRM_SIZE_BLOCK; i++) bufferHex[i] = '0'; - } k = n - 1; - for(i = (BN_Dst->Length * SDRM_SIZE_BLOCK) - 1; (int)k >= 0; i--, k--) - { + + for (i = (BN_Dst->Length * SDRM_SIZE_BLOCK) - 1; (int)k >= 0; i--, k--) bufferHex[i] = pbSrc[k]; - } - for(i = 0; i < BN_Dst->Length; i++) - { - for(j = (BN_Dst->Length * SDRM_SIZE_BLOCK) - (i * SDRM_SIZE_BLOCK) - SDRM_SIZE_BLOCK; j < (BN_Dst->Length * SDRM_SIZE_BLOCK) - (i * SDRM_SIZE_BLOCK) ; j++) - { + for (i = 0; i < BN_Dst->Length; i++) { + for (j = (BN_Dst->Length * SDRM_SIZE_BLOCK) - (i * SDRM_SIZE_BLOCK) - + SDRM_SIZE_BLOCK; + j < (BN_Dst->Length * SDRM_SIZE_BLOCK) - (i * SDRM_SIZE_BLOCK) ; j++) { char c = bufferHex[j]; int c_int; - if (c >= '0' && c <= '9') { + if (c >= '0' && c <= '9') c_int = c - '0'; - } else if (c >= 'a' && c <= 'f') { + + else if (c >= 'a' && c <= 'f') c_int = c - 'a' + 10; - } else if (c >= 'A' && c <= 'F') { + + else if (c >= 'A' && c <= 'F') c_int = c - 'A' + 10; - } else { + + else { free(bufferHex); return CRYPTO_INVALID_ARGUMENT; } @@ -2901,60 +2655,62 @@ int SDRM_HEX2BN(cc_u8* pbSrc, SDRM_BIG_NUM *BN_Dst) } /* - * @fn cc_u8 * SDRM_BN2STRBIN(cc_u32 *numberBits, SDRM_BIG_NUM *BN_Src); - * @brief Convert Big Number to binary String + * @fn cc_u8 * SDRM_BN2STRBIN(cc_u32 *numberBits, SDRM_BIG_NUM *BN_Src); + * @brief Convert Big Number to binary String * - * @param pbSrc [in]source big number - * @param BN_Dst [out]output numberBits of uot string + * @param pbSrc [in]source big number + * @param BN_Dst [out]output numberBits of uot string * - * @return (cc_u8 *) binary string + * @return (cc_u8 *) binary string */ -cc_u8 * SDRM_BN2STRBIN(cc_u32 *numberBits, SDRM_BIG_NUM *BN_Src) +cc_u8 *SDRM_BN2STRBIN(cc_u32 *numberBits, SDRM_BIG_NUM *BN_Src) { - cc_u32 i,j,k; + cc_u32 i, j, k; cc_u32 mask = 0x80000000; cc_u32 temp, check; cc_u8 *tempC, *tempR; (*numberBits) = sizeof(cc_u8) * BN_Src->Length * SDRM_SIZE_OF_DWORD * 8 + 1; - tempC = (cc_u8*)malloc(*numberBits); - tempR = (cc_u8*)malloc(*numberBits); + tempC = (cc_u8 *)malloc(*numberBits); + tempR = (cc_u8 *)malloc(*numberBits); + if (tempC == NULL || tempR == NULL) { free(tempR); free(tempC); return NULL; } + tempC[(*numberBits) - 1] = '\0'; - for(i = BN_Src->Length - 1; (int)i >= 0 ; i--) - { + + for (i = BN_Src->Length - 1; (int)i >= 0 ; i--) { temp = BN_Src->pData[i]; - for(j = 0; j < (SDRM_SIZE_OF_DWORD * 8); j++) - { - if((temp & mask) > 0) { + + for (j = 0; j < (SDRM_SIZE_OF_DWORD * 8); j++) { + if ((temp & mask) > 0) tempC[(((BN_Src->Length - 1) - i) * (SDRM_SIZE_OF_DWORD * 8)) + j] = '1'; - } - else { + + else tempC[(((BN_Src->Length - 1) - i) * (SDRM_SIZE_OF_DWORD * 8)) + j] = '0'; - } temp = temp << 1; } } //next block for normalize length string (eg 0111 = 111) - check = 0; k = 0; - for(i = 0; i < (*numberBits); i++) - { - if(tempC[i] == '0' && check == 0) { + check = 0; + k = 0; + + for (i = 0; i < (*numberBits); i++) { + if (tempC[i] == '0' && check == 0) continue; - } - else { + + else check = 1; - } tempR[k] = tempC[i]; k++; } + tempR[k] = '\0'; (*numberBits) = k - 1; @@ -2964,24 +2720,21 @@ cc_u8 * SDRM_BN2STRBIN(cc_u32 *numberBits, SDRM_BIG_NUM *BN_Src) } /* - * @fn int SDRM_BN_SetWord(SDRM_BIG_NUM *BN_Dst, cc_u32 t); - * @brief Set word to Big Number + * @fn int SDRM_BN_SetWord(SDRM_BIG_NUM *BN_Dst, cc_u32 t); + * @brief Set word to Big Number * - * @param pbSrc [in]source word - * @param BN_Dst [out]output Big Nubmer + * @param pbSrc [in]source word + * @param BN_Dst [out]output Big Nubmer * - * @return CRYPTO_SUCCESS if no error is occuredg + * @return CRYPTO_SUCCESS if no error is occuredg */ int SDRM_BN_SetWord(SDRM_BIG_NUM *BN_Dst, cc_u32 t) { - if((int)t >= 0) - { + if ((int)t >= 0) { BN_Dst->Length = 1; BN_Dst->sign = 0; BN_Dst->pData[0] = t; - } - else - { + } else { BN_Dst->Length = 1; BN_Dst->sign = 1; BN_Dst->pData[0] = t * (-1); @@ -2991,84 +2744,84 @@ int SDRM_BN_SetWord(SDRM_BIG_NUM *BN_Dst, cc_u32 t) } /* - * @fn int SDRM_BN_isZero(SDRM_BIG_NUM *BN_Src); - * @brief check big number is zero + * @fn int SDRM_BN_isZero(SDRM_BIG_NUM *BN_Src); + * @brief check big number is zero * - * @param pbSrc [in]source big number + * @param pbSrc [in]source big number * - * @return 0 - false, 1 - true + * @return 0 - false, 1 - true */ int SDRM_BN_isZero(SDRM_BIG_NUM *BN_Src) { - return ((BN_Src)->Length == 0)?1:0; + return ((BN_Src)->Length == 0) ? 1 : 0; } /* - * @fn cc_u8 * SDRM_BN2STRFOUR(cc_u32 *numberBits, SDRM_BIG_NUM *BN_Src); - * @brief Convert Big Number to four String + * @fn cc_u8 * SDRM_BN2STRFOUR(cc_u32 *numberBits, SDRM_BIG_NUM *BN_Src); + * @brief Convert Big Number to four String * - * @param pbSrc [in]source big number - * @param BN_Dst [out]output numberBits of four string + * @param pbSrc [in]source big number + * @param BN_Dst [out]output numberBits of four string * - * @return (cc_u8 *) four string + * @return (cc_u8 *) four string */ -cc_u8 * SDRM_BN2STRFOUR(cc_u32 *numberBits, SDRM_BIG_NUM *BN_Src) +cc_u8 *SDRM_BN2STRFOUR(cc_u32 *numberBits, SDRM_BIG_NUM *BN_Src) { SDRM_BIG_NUM *d, *tempREM, *num; cc_u32 i; cc_u32 sLength = (2 * SDRM_SIZE_OF_DWORD) * BN_Src->Length; - cc_u8 * strDestTemp = (cc_u8*)malloc(sLength * 10); - cc_u8 * strDest; + cc_u8 *strDestTemp = (cc_u8 *)malloc(sLength * 10); + cc_u8 *strDest; cc_u8 tempChar[10]; (*numberBits) = 0; - if(strDestTemp == NULL) - { + if (strDestTemp == NULL) return NULL; - } d = SDRM_BN_Init(BN_Src->Size); - if( d == NULL)// fix prevent cid =89093 by guoxing.xu - { + + if (d == NULL) { // fix prevent cid =89093 by guoxing.xu free(strDestTemp); - return NULL; - } + return NULL; + } + tempREM = SDRM_BN_Init(BN_Src->Size); num = SDRM_BN_Init(BN_Src->Size); - if(tempREM == NULL || num == NULL)//fix prevent cid = 89093 by guoxing.xu - { + + if (tempREM == NULL || num == NULL) { //fix prevent cid = 89093 by guoxing.xu free(strDestTemp); SDRM_BN_FREE(tempREM); - SDRM_BN_FREE(d); - return NULL; - } + SDRM_BN_FREE(d); + return NULL; + } + SDRM_BN_Copy(num, BN_Src); SDRM_BN_SetWord(d, 4); - while (!SDRM_BN_isZero(num)) - { + while (!SDRM_BN_isZero(num)) { SDRM_BN_Div(num, tempREM, num, d); //itoa(tempREM->pData[0], (char *)tempChar, 10); //sprintf((char*)tempChar, "%d", tempREM->pData[0]); - snprintf((char*)tempChar, sizeof(tempChar), "%d", tempREM->pData[0]);// fix prevnet 60199 by guoxing.xu + snprintf((char *)tempChar, sizeof(tempChar), "%d", + tempREM->pData[0]); // fix prevnet 60199 by guoxing.xu strDestTemp[(*numberBits)] = tempChar[0]; (*numberBits)++; } - if((*numberBits) != 0) - { - strDest = (cc_u8*)malloc((*numberBits) + 1); + + if ((*numberBits) != 0) { + strDest = (cc_u8 *)malloc((*numberBits) + 1); + if (strDest != NULL) { - for(i = 0; i < (*numberBits); i++) { + for (i = 0; i < (*numberBits); i++) strDest[i] = strDestTemp[((*numberBits) - 1) - i]; - } + strDest[(*numberBits)] = '\0'; } - } - else - { + } else { (*numberBits) = 1; - strDest = (cc_u8*)malloc((*numberBits) + 1); + strDest = (cc_u8 *)malloc((*numberBits) + 1); + if (strDest != NULL) { strDest[0] = '0'; strDest[(*numberBits)] = '\0'; @@ -3084,41 +2837,40 @@ cc_u8 * SDRM_BN2STRFOUR(cc_u32 *numberBits, SDRM_BIG_NUM *BN_Src) } /* - * @fn SDRM_BN_MassInit - * @brief Allocate a series of big number object + * @fn SDRM_BN_MassInit + * @brief Allocate a series of big number object * - * @param dBufSize [in]buffer size of each big number - * @param count [in]number BigNumbers + * @param dBufSize [in]buffer size of each big number + * @param count [in]number BigNumbers * - * @return double pointer of SDRM_BIG_NUM structure - * \n NULL if memory allocation is failed + * @return double pointer of SDRM_BIG_NUM structure + * \n NULL if memory allocation is failed */ SDRM_BIG_NUM **SDRM_BN_MassInit(cc_u32 dBufSize, cc_u32 count) { cc_u32 i; cc_u32 bnsiz = sizeof(SDRM_BIG_NUM) + dBufSize * SDRM_SIZE_OF_DWORD; - cc_u8* ptr; - void * tmp; + cc_u8 *ptr; + void *tmp; - SDRM_BIG_NUM** BN_Buf = (SDRM_BIG_NUM**)malloc((sizeof(SDRM_BIG_NUM*) + bnsiz) * count); + SDRM_BIG_NUM **BN_Buf = (SDRM_BIG_NUM **)malloc((sizeof( + SDRM_BIG_NUM *) + bnsiz) * count); if (BN_Buf == NULL) - { return NULL; - } - memset(BN_Buf, 0x00, (sizeof(SDRM_BIG_NUM*) + bnsiz) * count); + memset(BN_Buf, 0x00, (sizeof(SDRM_BIG_NUM *) + bnsiz) * count); - ptr = (cc_u8*)BN_Buf + sizeof(SDRM_BIG_NUM*) * count; - for(i = 0; i < count; i++) - { - //add by guoxing.xu to avoid warning. 2/15/2014 - tmp = ptr; - BN_Buf[i] = (SDRM_BIG_NUM*)tmp; + ptr = (cc_u8 *)BN_Buf + sizeof(SDRM_BIG_NUM *) * count; + + for (i = 0; i < count; i++) { + //add by guoxing.xu to avoid warning. 2/15/2014 + tmp = ptr; + BN_Buf[i] = (SDRM_BIG_NUM *)tmp; //BN_Buf[i] = (SDRM_BIG_NUM*)ptr; BN_Buf[i]->Size = dBufSize; - tmp = (ptr + sizeof(SDRM_BIG_NUM)); - BN_Buf[i]->pData = (cc_u32*)tmp; + tmp = (ptr + sizeof(SDRM_BIG_NUM)); + BN_Buf[i]->pData = (cc_u32 *)tmp; //BN_Buf[i]->pData = (cc_u32*)(ptr + sizeof(SDRM_BIG_NUM)); ptr += bnsiz; } @@ -3127,22 +2879,21 @@ SDRM_BIG_NUM **SDRM_BN_MassInit(cc_u32 dBufSize, cc_u32 count) } /* - * @fn SDRM_BN_IntInit - * @brief Allocate a big number object and assign an integer value + * @fn SDRM_BN_IntInit + * @brief Allocate a big number object and assign an integer value * - * @param dSize [in]buffer size of each big number - * @param data [in]integer value + * @param dSize [in]buffer size of each big number + * @param data [in]integer value * - * @return double pointer of SDRM_BIG_NUM structure - * \n NULL if memory allocation is failed + * @return double pointer of SDRM_BIG_NUM structure + * \n NULL if memory allocation is failed */ SDRM_BIG_NUM *SDRM_BN_IntInit(cc_u32 dSize, cc_u32 data) { - SDRM_BIG_NUM* BN_Buf = SDRM_BN_Init(dSize); + SDRM_BIG_NUM *BN_Buf = SDRM_BN_Init(dSize); + if (BN_Buf == NULL) - { return NULL; - } BN_Buf->pData[0] = data; BN_Buf->Length = 1; diff --git a/ssflib/dep/cryptocore/source/base/cc_des.c b/ssflib/dep/cryptocore/source/base/cc_des.c index cbcb459..fb50d58 100644 --- a/ssflib/dep/cryptocore/source/base/cc_des.c +++ b/ssflib/dep/cryptocore/source/base/cc_des.c @@ -30,49 +30,48 @@ // Functions //////////////////////////////////////////////////////////////////////////// /* - * @fn SDRM_DES_KeySched - * @brief Expand the cipher key into the encryption key schedule + * @fn SDRM_DES_KeySched + * @brief Expand the cipher key into the encryption key schedule * - * @param RoundKey [out]generated round key - * @param UserKey [in]user key, 8 byte - * @param RKPos [in]index of round key starts - * @param RKStep [in]step for index + * @param RoundKey [out]generated round key + * @param UserKey [in]user key, 8 byte + * @param RKPos [in]index of round key starts + * @param RKStep [in]step for index * - * @return the number of rounds for the given cipher key size + * @return the number of rounds for the given cipher key size */ -int SDRM_DES_KeySched(cc_u8 *RoundKey, cc_u8 *UserKey, cc_u32 RKPos, cc_u32 RKStep) +int SDRM_DES_KeySched(cc_u8 *RoundKey, cc_u8 *UserKey, cc_u32 RKPos, + cc_u32 RKStep) { - cc_u32 round, i, s, t, t2; - cc_u32 roundkey[16][2]; - cc_u32 c = 0, d = 0; + cc_u32 round, i, s, t, t2; + cc_u32 roundkey[16][2]; + cc_u32 c = 0, d = 0; //process Permuted Choice 1 - for (i = 0; i < 28; i++) - { + for (i = 0; i < 28; i++) { t = SDRM_DES_KS_PC1[i]; c |= (UserKey[t >> 3] & SDRM_DES_BitMask[t & 0x07]) ? (1 << i) : 0; } - for (i = 28; i < 56; i++) - { + for (i = 28; i < 56; i++) { t = SDRM_DES_KS_PC1[i]; d |= (UserKey[t >> 3] & SDRM_DES_BitMask[t & 0x07]) ? (1 << (i - 28)) : 0; } //get round key - for (round = 0; round < SDRM_DES_NUM_OF_ROUNDS; round++) - { + for (round = 0; round < SDRM_DES_NUM_OF_ROUNDS; round++) { //shift left operation c = (c >> SDRM_DES_KS_SHIFT[round]) | (c << (28 - SDRM_DES_KS_SHIFT[round])); d = (d >> SDRM_DES_KS_SHIFT[round]) | (d << (28 - SDRM_DES_KS_SHIFT[round])); - s = SDRM_des_skb[0][((c) ) & 0x3f] | + s = SDRM_des_skb[0][((c)) & 0x3f] | SDRM_des_skb[1][((c >> 6L) & 0x03) | ((c >> 7L) & 0x3c)] | SDRM_des_skb[2][((c >> 13L) & 0x0f) | ((c >> 14L) & 0x30)] | - SDRM_des_skb[3][((c >> 20L) & 0x01) | ((c >> 21L) & 0x06) | ((c>>22L)&0x38)]; - t = SDRM_des_skb[4][((d) ) & 0x3f] | + SDRM_des_skb[3][((c >> 20L) & 0x01) | ((c >> 21L) & 0x06) | (( + c >> 22L) & 0x38)]; + t = SDRM_des_skb[4][((d)) & 0x3f] | SDRM_des_skb[5][((d >> 7L) & 0x03) | ((d >> 8L) & 0x3c)] | - SDRM_des_skb[6][ (d >> 15L) & 0x3f] | + SDRM_des_skb[6][(d >> 15L) & 0x3f] | SDRM_des_skb[7][((d >> 21L) & 0x0f) | ((d >> 22L) & 0x30)]; t2 = (t << 16L) | (s & 0x0000ffffL); @@ -90,37 +89,32 @@ int SDRM_DES_KeySched(cc_u8 *RoundKey, cc_u8 *UserKey, cc_u32 RKPos, cc_u32 RKSt } /* - * @fn SDRM_DES_Encryption - * @brief DES processing for one block + * @fn SDRM_DES_Encryption + * @brief DES processing for one block * - * @param RoundKey [in]expanded round key - * @param msg [in]8 byte plaintext - * @param out [out]8 byte ciphertext + * @param RoundKey [in]expanded round key + * @param msg [in]8 byte plaintext + * @param out [out]8 byte ciphertext * - * @return CRYPTO_SUCCESS if no error is occured + * @return CRYPTO_SUCCESS if no error is occured */ int SDRM_DES_Encryption(cc_u32 RoundKey[][2], cc_u8 *msg, cc_u8 *out) { - cc_u32 l, r, i, t, u; + cc_u32 l, r, i, t, u; - r = *(cc_u32*)(void*)(msg); - l = *(cc_u32*)(void*)(msg + 4); + r = *(cc_u32 *)(void *)(msg); + l = *(cc_u32 *)(void *)(msg + 4); - SDRM_IP(r,l); + SDRM_IP(r, l); r = SDRM_rotr32(r, 29); l = SDRM_rotr32(l, 29); - for (i = 0; i < SDRM_DES_NUM_OF_ROUNDS; i++) - { + for (i = 0; i < SDRM_DES_NUM_OF_ROUNDS; i++) { if (i & 0x01) - { SDRM_D_ENCRYPT(r, l); - } else - { SDRM_D_ENCRYPT(l, r); - } } r = SDRM_rotr32(r, 3); @@ -128,27 +122,27 @@ int SDRM_DES_Encryption(cc_u32 RoundKey[][2], cc_u8 *msg, cc_u8 *out) SDRM_INV_IP(r, l); - memcpy(out , &l, 4); + memcpy(out, &l, 4); memcpy(out + 4, &r, 4); return CRYPTO_SUCCESS; } /* - * @fn SDRM_DES64_Encryption - * @brief one block DES Encryption + * @fn SDRM_DES64_Encryption + * @brief one block DES Encryption * - * @param cipherText [out]encrypted text - * @param plainText [in]plain text - * @param UserKey [in]user key + * @param cipherText [out]encrypted text + * @param plainText [in]plain text + * @param UserKey [in]user key * - * @return CRYPTO_SUCCESS if success + * @return CRYPTO_SUCCESS if success */ int SDRM_DES64_Encryption(cc_u8 *cipherText, cc_u8 *plainText, cc_u8 *UserKey) { cc_u32 RoundKey[16][2]; - SDRM_DES_KeySched((cc_u8*)RoundKey, UserKey, 0, 1); + SDRM_DES_KeySched((cc_u8 *)RoundKey, UserKey, 0, 1); SDRM_DES_Encryption(RoundKey, plainText, cipherText); @@ -156,24 +150,25 @@ int SDRM_DES64_Encryption(cc_u8 *cipherText, cc_u8 *plainText, cc_u8 *UserKey) } /* - * @fn SDRM_DES64_Decryption - * @brief one block DES Decryption + * @fn SDRM_DES64_Decryption + * @brief one block DES Decryption * - * @param plainText [out]decrypted text - * @param cipherText [in]cipher text - * @param UserKey [in]user key + * @param plainText [out]decrypted text + * @param cipherText [in]cipher text + * @param UserKey [in]user key * - * @return CRYPTO_SUCCESS if success + * @return CRYPTO_SUCCESS if success */ int SDRM_DES64_Decryption(cc_u8 *plainText, cc_u8 *cipherText, cc_u8 *UserKey) { cc_u32 RoundKey[16][2]; - SDRM_DES_KeySched((cc_u8*)RoundKey, UserKey, 15, (cc_u32)-1); + SDRM_DES_KeySched((cc_u8 *)RoundKey, UserKey, 15, (cc_u32) - 1); SDRM_DES_Encryption(RoundKey, cipherText, plainText); return CRYPTO_SUCCESS; } -/***************************** End of File *****************************/ \ No newline at end of file +/***************************** End of File *****************************/ + diff --git a/ssflib/dep/cryptocore/source/base/cc_ecc.c b/ssflib/dep/cryptocore/source/base/cc_ecc.c index 7325b19..9908edd 100644 --- a/ssflib/dep/cryptocore/source/base/cc_ecc.c +++ b/ssflib/dep/cryptocore/source/base/cc_ecc.c @@ -30,77 +30,99 @@ // Functions //////////////////////////////////////////////////////////////////////////// /* - * @fn SDRM_ECC_Init - * @brief return SDRM_EC_POINT structure + * @fn SDRM_ECC_Init + * @brief return SDRM_EC_POINT structure * - * @return address of allocate structure - * \n NULL if memory allocation is failed + * @return address of allocate structure + * \n NULL if memory allocation is failed */ SDRM_EC_POINT *SDRM_ECC_Init() { - SDRM_EC_POINT *temp; + SDRM_EC_POINT *temp; temp = (SDRM_EC_POINT *)malloc(sizeof(SDRM_EC_POINT) + SDRM_ECC_ALLOC_SIZE * 5); + if (!temp) - { return NULL; - } temp->IsInfinity = 0; - temp->x = SDRM_BN_Alloc((cc_u8*)temp + sizeof(SDRM_EC_POINT), SDRM_ECC_BN_BUFSIZE); - temp->y = SDRM_BN_Alloc((cc_u8*)temp->x + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE); - temp->z = SDRM_BN_Alloc((cc_u8*)temp->y + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE); - temp->z2 = SDRM_BN_Alloc((cc_u8*)temp->z + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE); - temp->z3 = SDRM_BN_Alloc((cc_u8*)temp->z2 + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE); + temp->x = SDRM_BN_Alloc((cc_u8 *)temp + sizeof(SDRM_EC_POINT), + SDRM_ECC_BN_BUFSIZE); + temp->y = SDRM_BN_Alloc((cc_u8 *)temp->x + SDRM_ECC_ALLOC_SIZE, + SDRM_ECC_BN_BUFSIZE); + temp->z = SDRM_BN_Alloc((cc_u8 *)temp->y + SDRM_ECC_ALLOC_SIZE, + SDRM_ECC_BN_BUFSIZE); + temp->z2 = SDRM_BN_Alloc((cc_u8 *)temp->z + SDRM_ECC_ALLOC_SIZE, + SDRM_ECC_BN_BUFSIZE); + temp->z3 = SDRM_BN_Alloc((cc_u8 *)temp->z2 + SDRM_ECC_ALLOC_SIZE, + SDRM_ECC_BN_BUFSIZE); return temp; } /* - * @fn SDRM_CURVE_Init - * @brief return SDRM_ECC_CTX structure + * @fn SDRM_CURVE_Init + * @brief return SDRM_ECC_CTX structure * - * @return address of allocate structure - * \n NULL if memory allocation is failed + * @return address of allocate structure + * \n NULL if memory allocation is failed */ SDRM_ECC_CTX *SDRM_CURVE_Init() { - SDRM_ECC_CTX *temp; - SDRM_EC_POINT *ptr; - cc_u8 *pbBlk; + SDRM_ECC_CTX *temp; + SDRM_EC_POINT *ptr; + cc_u8 *pbBlk; - temp = (SDRM_ECC_CTX *)malloc(sizeof(SDRM_ECC_CTX) + SDRM_ECC_ALLOC_SIZE * 15 + 2 * sizeof(SDRM_EC_POINT)); - if (!temp) { + temp = (SDRM_ECC_CTX *)malloc(sizeof(SDRM_ECC_CTX) + SDRM_ECC_ALLOC_SIZE * 15 + + 2 * sizeof(SDRM_EC_POINT)); + + if (!temp) return NULL; - } - pbBlk = (cc_u8*)temp + sizeof(SDRM_ECC_CTX); + pbBlk = (cc_u8 *)temp + sizeof(SDRM_ECC_CTX); - temp->ECC_a = SDRM_BN_Alloc(pbBlk, SDRM_ECC_BN_BUFSIZE); - temp->ECC_b = SDRM_BN_Alloc((cc_u8*)temp->ECC_a + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE); - temp->ECC_p = SDRM_BN_Alloc((cc_u8*)temp->ECC_b + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE); - temp->ECC_n = SDRM_BN_Alloc((cc_u8*)temp->ECC_p + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE); - temp->PRIV_KEY = SDRM_BN_Alloc((cc_u8*)temp->ECC_n + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE); + temp->ECC_a = SDRM_BN_Alloc(pbBlk, + SDRM_ECC_BN_BUFSIZE); + temp->ECC_b = SDRM_BN_Alloc((cc_u8 *)temp->ECC_a + SDRM_ECC_ALLOC_SIZE, + SDRM_ECC_BN_BUFSIZE); + temp->ECC_p = SDRM_BN_Alloc((cc_u8 *)temp->ECC_b + SDRM_ECC_ALLOC_SIZE, + SDRM_ECC_BN_BUFSIZE); + temp->ECC_n = SDRM_BN_Alloc((cc_u8 *)temp->ECC_p + SDRM_ECC_ALLOC_SIZE, + SDRM_ECC_BN_BUFSIZE); + temp->PRIV_KEY = SDRM_BN_Alloc((cc_u8 *)temp->ECC_n + SDRM_ECC_ALLOC_SIZE, + SDRM_ECC_BN_BUFSIZE); temp->uDimension = 0; - ptr = (SDRM_EC_POINT*)(void*)((cc_u8*)temp + sizeof(SDRM_ECC_CTX) + SDRM_ECC_ALLOC_SIZE * 5); + ptr = (SDRM_EC_POINT *)(void *)((cc_u8 *)temp + sizeof(SDRM_ECC_CTX) + + SDRM_ECC_ALLOC_SIZE * 5); ptr->IsInfinity = 0; - ptr->x = SDRM_BN_Alloc((cc_u8*)ptr + sizeof(SDRM_EC_POINT), SDRM_ECC_BN_BUFSIZE); - ptr->y = SDRM_BN_Alloc((cc_u8*)ptr->x + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE); - ptr->z = SDRM_BN_Alloc((cc_u8*)ptr->y + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE); - ptr->z2 = SDRM_BN_Alloc((cc_u8*)ptr->z + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE); - ptr->z3 = SDRM_BN_Alloc((cc_u8*)ptr->z2 + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE); + ptr->x = SDRM_BN_Alloc((cc_u8 *)ptr + sizeof(SDRM_EC_POINT), + SDRM_ECC_BN_BUFSIZE); + ptr->y = SDRM_BN_Alloc((cc_u8 *)ptr->x + SDRM_ECC_ALLOC_SIZE, + SDRM_ECC_BN_BUFSIZE); + ptr->z = SDRM_BN_Alloc((cc_u8 *)ptr->y + SDRM_ECC_ALLOC_SIZE, + SDRM_ECC_BN_BUFSIZE); + ptr->z2 = SDRM_BN_Alloc((cc_u8 *)ptr->z + SDRM_ECC_ALLOC_SIZE, + SDRM_ECC_BN_BUFSIZE); + ptr->z3 = SDRM_BN_Alloc((cc_u8 *)ptr->z2 + SDRM_ECC_ALLOC_SIZE, + SDRM_ECC_BN_BUFSIZE); temp->ECC_G = ptr; - ptr = (SDRM_EC_POINT*)(void*)((cc_u8*)ptr + sizeof(SDRM_EC_POINT) + SDRM_ECC_ALLOC_SIZE * 5); + ptr = (SDRM_EC_POINT *)(void *)((cc_u8 *)ptr + sizeof(SDRM_EC_POINT) + + SDRM_ECC_ALLOC_SIZE * 5); ptr->IsInfinity = 0; - ptr->x = SDRM_BN_Alloc((cc_u8*)ptr + sizeof(SDRM_EC_POINT), SDRM_ECC_BN_BUFSIZE); - ptr->y = SDRM_BN_Alloc((cc_u8*)ptr->x + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE); - ptr->z = SDRM_BN_Alloc((cc_u8*)ptr->y + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE); - ptr->z2 = SDRM_BN_Alloc((cc_u8*)ptr->z + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE); - ptr->z3 = SDRM_BN_Alloc((cc_u8*)ptr->z2 + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE); + ptr->x = SDRM_BN_Alloc((cc_u8 *)ptr + sizeof(SDRM_EC_POINT), + SDRM_ECC_BN_BUFSIZE); + ptr->y = SDRM_BN_Alloc((cc_u8 *)ptr->x + SDRM_ECC_ALLOC_SIZE, + SDRM_ECC_BN_BUFSIZE); + ptr->z = SDRM_BN_Alloc((cc_u8 *)ptr->y + SDRM_ECC_ALLOC_SIZE, + SDRM_ECC_BN_BUFSIZE); + ptr->z2 = SDRM_BN_Alloc((cc_u8 *)ptr->z + SDRM_ECC_ALLOC_SIZE, + SDRM_ECC_BN_BUFSIZE); + ptr->z3 = SDRM_BN_Alloc((cc_u8 *)ptr->z2 + SDRM_ECC_ALLOC_SIZE, + SDRM_ECC_BN_BUFSIZE); temp->PUBLIC_KEY = ptr; @@ -111,56 +133,48 @@ SDRM_ECC_CTX *SDRM_CURVE_Init() // ECC Ô¼ //////////////////////////////////////////////////////////////////////////// /* - * @fn SDRM_CHECK_EC_POINT_ZERO - * @brief check if the point points zero + * @fn SDRM_CHECK_EC_POINT_ZERO + * @brief check if the point points zero * - * @param r [in]point + * @param r [in]point * - * @return 1 if the point is pointing zero - * \n 0 otherwise + * @return 1 if the point is pointing zero + * \n 0 otherwise */ -int SDRM_CHECK_EC_POINT_ZERO(SDRM_EC_POINT* r) +int SDRM_CHECK_EC_POINT_ZERO(SDRM_EC_POINT *r) { - if ((r->x->Length == 0) | (r->y->Length == 0)) - { - // return = 1 if input is zero + if ((r->x->Length == 0) | (r->y->Length == 0)) { + // return = 1 if input is zero return 1; - } - else - { + } else return 0; - } } /* - * @fn SDRM_Mont_Jm2Jc - * @brief ǥȯ 1 : Modified Jacobian => Chundnovsky Jacobian - * (A->y) <= (A->y)/2 - * (A->z2) <= (A->z)^2 - * (A->z3) <= (A->z)^3 + * @fn SDRM_Mont_Jm2Jc + * @brief ǥȯ 1 : Modified Jacobian => Chundnovsky Jacobian + * (A->y) <= (A->y)/2 + * (A->z2) <= (A->z)^2 + * (A->z3) <= (A->z)^3 * - * @param EC_Dst [out]destination - * @param new_a [in]first element - * @param new_b [in]second element - * @param Mont [in]montgomery context + * @param EC_Dst [out]destination + * @param new_a [in]first element + * @param new_b [in]second element + * @param Mont [in]montgomery context * - * @return CRYPTO_SUCCESS if no error is occured - * \n CRYPTO_ERROR if evaluation is failed + * @return CRYPTO_SUCCESS if no error is occured + * \n CRYPTO_ERROR if evaluation is failed */ -int SDRM_Mont_Jm2Jc(SDRM_EC_POINT *EC_Dst, SDRM_BIG_NUM *new_a, SDRM_BIG_NUM *new_b, SDRM_BIG_MONT *Mont) +int SDRM_Mont_Jm2Jc(SDRM_EC_POINT *EC_Dst, SDRM_BIG_NUM *new_a, + SDRM_BIG_NUM *new_b, SDRM_BIG_MONT *Mont) { - if (SDRM_BN_IS_ODD(EC_Dst->y)) - { + if (SDRM_BN_IS_ODD(EC_Dst->y)) { if (SDRM_BN_Add(EC_Dst->y, EC_Dst->y, Mont->Mod) != CRYPTO_SUCCESS) - { return CRYPTO_ERROR; - } } if (SDRM_BN_SHR(EC_Dst->y, EC_Dst->y, 1) != CRYPTO_SUCCESS) - { return CRYPTO_ERROR; - } SDRM_MONT_Mul(EC_Dst->z2, EC_Dst->z, EC_Dst->z, Mont); SDRM_MONT_Mul(EC_Dst->z3, EC_Dst->z, EC_Dst->z2, Mont); @@ -169,30 +183,27 @@ int SDRM_Mont_Jm2Jc(SDRM_EC_POINT *EC_Dst, SDRM_BIG_NUM *new_a, SDRM_BIG_NUM *ne } /* - * @fn SDRM_Mont_Jc2Jm - * @brief ǥȯ 2 : Chundnovsky Jacobian => Modified Jacobian - * (A->y) <= 2*(A->y) - * (A->z2) <= new_a*(A->z)^4 + * @fn SDRM_Mont_Jc2Jm + * @brief ǥȯ 2 : Chundnovsky Jacobian => Modified Jacobian + * (A->y) <= 2*(A->y) + * (A->z2) <= new_a*(A->z)^4 * - * @param A [out]destination - * @param new_a [in]first element - * @param new_b [in]second element - * @param Mont [in]montgomery context + * @param A [out]destination + * @param new_a [in]first element + * @param new_b [in]second element + * @param Mont [in]montgomery context * - * @return CRYPTO_SUCCESS if no error is occured - * \n CRYPTO_ERROR if evaluation is failed + * @return CRYPTO_SUCCESS if no error is occured + * \n CRYPTO_ERROR if evaluation is failed */ -int SDRM_Mont_Jc2Jm(SDRM_EC_POINT *A, SDRM_BIG_NUM *new_a, SDRM_BIG_NUM *new_b, SDRM_BIG_MONT *Mont) +int SDRM_Mont_Jc2Jm(SDRM_EC_POINT *A, SDRM_BIG_NUM *new_a, SDRM_BIG_NUM *new_b, + SDRM_BIG_MONT *Mont) { if (SDRM_BN_SHL(A->y, A->y, 1) != CRYPTO_SUCCESS) - { return CRYPTO_ERROR; - } - if (SDRM_BN_Cmp(A->y, Mont->Mod)>=0) - { + if (SDRM_BN_Cmp(A->y, Mont->Mod) >= 0) SDRM_BN_Sub(A->y, A->y, Mont->Mod); - } SDRM_MONT_Mul(A->z2, A->z, A->z, Mont); SDRM_MONT_Mul(A->z2, A->z2, A->z2, Mont); @@ -203,88 +214,96 @@ int SDRM_Mont_Jc2Jm(SDRM_EC_POINT *A, SDRM_BIG_NUM *new_a, SDRM_BIG_NUM *new_b, /* - * @fn SDRM_CTX_EC_Add - * @brief Affine Coordinate (A = B + C) + * @fn SDRM_CTX_EC_Add + * @brief Affine Coordinate (A = B + C) * - * @param ctx [in]ECC context - * @param EC_Dst [out]destination(A) - * @param EC_Src1 [in]first element(B) - * @param EC_Src2 [in]second element(C) + * @param ctx [in]ECC context + * @param EC_Dst [out]destination(A) + * @param EC_Src1 [in]first element(B) + * @param EC_Src2 [in]second element(C) * - * @return CRYPTO_SUCCESS if no error is occured - * \n CRYPTO_ERROR if evaluation is failed - * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed + * @return CRYPTO_SUCCESS if no error is occured + * \n CRYPTO_ERROR if evaluation is failed + * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed */ -int SDRM_CTX_EC_Add(SDRM_ECC_CTX *ctx, SDRM_EC_POINT* EC_Dst, SDRM_EC_POINT *EC_Src1, SDRM_EC_POINT *EC_Src2) +int SDRM_CTX_EC_Add(SDRM_ECC_CTX *ctx, SDRM_EC_POINT *EC_Dst, + SDRM_EC_POINT *EC_Src1, SDRM_EC_POINT *EC_Src2) { - SDRM_BIG_NUM *t1, *t2, *t3, *lambda, *lambda_sqr; - SDRM_BIG_NUM *x3, *y3; - cc_u8 *pbBuf = (cc_u8*)malloc(SDRM_ECC_ALLOC_SIZE * 7); + SDRM_BIG_NUM *t1, *t2, *t3, *lambda, *lambda_sqr; + SDRM_BIG_NUM *x3, *y3; + cc_u8 *pbBuf = (cc_u8 *)malloc(SDRM_ECC_ALLOC_SIZE * 7); if (!pbBuf) - { return CRYPTO_MEMORY_ALLOC_FAIL; - } - if (SDRM_CHECK_EC_POINT_ZERO(EC_Src1)) - { + if (SDRM_CHECK_EC_POINT_ZERO(EC_Src1)) { SDRM_EC_COPY(EC_Dst, EC_Src2); free(pbBuf); return CRYPTO_SUCCESS; - } - else if (SDRM_CHECK_EC_POINT_ZERO(EC_Src2)) - { + } else if (SDRM_CHECK_EC_POINT_ZERO(EC_Src2)) { SDRM_EC_COPY(EC_Dst, EC_Src1); free(pbBuf); return CRYPTO_SUCCESS; } - t1 = SDRM_BN_Alloc(pbBuf, SDRM_ECC_BN_BUFSIZE); - t2 = SDRM_BN_Alloc((cc_u8*)t1 + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE); - t3 = SDRM_BN_Alloc((cc_u8*)t2 + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE); - lambda = SDRM_BN_Alloc((cc_u8*)t3 + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE); - lambda_sqr = SDRM_BN_Alloc((cc_u8*)lambda + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE); - x3 = SDRM_BN_Alloc((cc_u8*)lambda_sqr + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE); - y3 = SDRM_BN_Alloc((cc_u8*)x3 + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE); - - if (SDRM_BN_Cmp(EC_Src1->x, EC_Src2->x) == 0) /* xÇ¥ Ù¸ */ - { - if (SDRM_BN_Cmp(EC_Src1->y, EC_Src2->y) != 0) /* y Ç¥ ٸٸ */ - { // (B = -C) + t1 = SDRM_BN_Alloc(pbBuf, + SDRM_ECC_BN_BUFSIZE); + t2 = SDRM_BN_Alloc((cc_u8 *)t1 + SDRM_ECC_ALLOC_SIZE, + SDRM_ECC_BN_BUFSIZE); + t3 = SDRM_BN_Alloc((cc_u8 *)t2 + SDRM_ECC_ALLOC_SIZE, + SDRM_ECC_BN_BUFSIZE); + lambda = SDRM_BN_Alloc((cc_u8 *)t3 + SDRM_ECC_ALLOC_SIZE, + SDRM_ECC_BN_BUFSIZE); + lambda_sqr = SDRM_BN_Alloc((cc_u8 *)lambda + SDRM_ECC_ALLOC_SIZE, + SDRM_ECC_BN_BUFSIZE); + x3 = SDRM_BN_Alloc((cc_u8 *)lambda_sqr + SDRM_ECC_ALLOC_SIZE, + SDRM_ECC_BN_BUFSIZE); + y3 = SDRM_BN_Alloc((cc_u8 *)x3 + SDRM_ECC_ALLOC_SIZE, + SDRM_ECC_BN_BUFSIZE); + + if (SDRM_BN_Cmp(EC_Src1->x, EC_Src2->x) == 0) { /* xÇ¥ Ù¸ */ + if (SDRM_BN_Cmp(EC_Src1->y, EC_Src2->y) != 0) { /* y Ç¥ ٸٸ */ + // (B = -C) SDRM_EC_SET_ZERO(EC_Dst); free(pbBuf); return CRYPTO_SUCCESS; + } else { /* y Ç¥ Ù¸ */ + // (B = C) + SDRM_BN_ModAdd(t1, EC_Src1->y, EC_Src1->y, ctx->ECC_p); /* t1 = 2 * y1 */ + SDRM_BN_ModInv(t1, t1, ctx->ECC_p); /* t1 = 1/(2 * y1) */ + + SDRM_BN_ModMul(t2, EC_Src1->x, EC_Src1->x, ctx->ECC_p); /* t2 = x1^2 */ + SDRM_BN_ModAdd(t3, t2, t2, ctx->ECC_p); /* t3 = t2 + t2 */ + SDRM_BN_ModAdd(t3, t3, t2, + ctx->ECC_p); /* t2 = t3 + t2 = 3 * x1^2*/ + SDRM_BN_ModAdd(t3, t3, ctx->ECC_a, ctx->ECC_p); /* t3 = 3 * x1^2 + a */ + + SDRM_BN_ModMul(lambda, t3, t1, + ctx->ECC_p); /* lambda = (3 * x1^2 + a) / (2 * y1) */ } - else /* y Ç¥ Ù¸ */ - { // (B = C) - SDRM_BN_ModAdd(t1, EC_Src1->y, EC_Src1->y, ctx->ECC_p); /* t1 = 2 * y1 */ - SDRM_BN_ModInv(t1, t1, ctx->ECC_p); /* t1 = 1/(2 * y1) */ - - SDRM_BN_ModMul(t2, EC_Src1->x, EC_Src1->x, ctx->ECC_p); /* t2 = x1^2 */ - SDRM_BN_ModAdd(t3, t2, t2, ctx->ECC_p); /* t3 = t2 + t2 */ - SDRM_BN_ModAdd(t3, t3, t2, ctx->ECC_p); /* t2 = t3 + t2 = 3 * x1^2*/ - SDRM_BN_ModAdd(t3, t3, ctx->ECC_a, ctx->ECC_p); /* t3 = 3 * x1^2 + a */ - - SDRM_BN_ModMul(lambda, t3, t1, ctx->ECC_p); /* lambda = (3 * x1^2 + a) / (2 * y1) */ - } - } - else /* x Ç¥ ٸٸ */ - { - SDRM_BN_ModSub(t1, EC_Src2->x, EC_Src1->x, ctx->ECC_p); /* t1 = x2 - x1 */ - SDRM_BN_ModSub(t2, EC_Src2->y, EC_Src1->y, ctx->ECC_p); /* t2 = y2 - y1 */ - - SDRM_BN_ModInv(t1, t1, ctx->ECC_p); /* t1 = t1^(-1) = 1/(x2-x1) */ - SDRM_BN_ModMul(lambda, t1, t2, ctx->ECC_p); /* lambda = (y2-y1)/(x2-x1) */ + } else { /* x Ç¥ ٸٸ */ + SDRM_BN_ModSub(t1, EC_Src2->x, EC_Src1->x, ctx->ECC_p); /* t1 = x2 - x1 */ + SDRM_BN_ModSub(t2, EC_Src2->y, EC_Src1->y, ctx->ECC_p); /* t2 = y2 - y1 */ + + SDRM_BN_ModInv(t1, t1, + ctx->ECC_p); /* t1 = t1^(-1) = 1/(x2-x1) */ + SDRM_BN_ModMul(lambda, t1, t2, + ctx->ECC_p); /* lambda = (y2-y1)/(x2-x1) */ } - SDRM_BN_ModMul(lambda_sqr, lambda, lambda, ctx->ECC_p); /* lambda^2 */ - SDRM_BN_ModSub(t1, lambda_sqr, EC_Src1->x, ctx->ECC_p); /* x3 = lambda^2 - x1 */ - SDRM_BN_ModSub(x3, t1, EC_Src2->x, ctx->ECC_p); /* x3 = lambda^2 - x1 - x2 */ + SDRM_BN_ModMul(lambda_sqr, lambda, lambda, ctx->ECC_p); /* lambda^2 */ + SDRM_BN_ModSub(t1, lambda_sqr, EC_Src1->x, + ctx->ECC_p); /* x3 = lambda^2 - x1 */ + SDRM_BN_ModSub(x3, t1, EC_Src2->x, + ctx->ECC_p); /* x3 = lambda^2 - x1 - x2 */ - SDRM_BN_ModSub(t1, EC_Src1->x, x3, ctx->ECC_p); /* t1 = x1 - x3 */ - SDRM_BN_ModMul(t2, t1, lambda, ctx->ECC_p); /* t2 = (x1 - x3) * lambda */ - SDRM_BN_ModSub(y3, t2, EC_Src1->y, ctx->ECC_p); /* y3 = (x1 - x3) * lambda - y1 */ + SDRM_BN_ModSub(t1, EC_Src1->x, x3, + ctx->ECC_p); /* t1 = x1 - x3 */ + SDRM_BN_ModMul(t2, t1, lambda, + ctx->ECC_p); /* t2 = (x1 - x3) * lambda */ + SDRM_BN_ModSub(y3, t2, EC_Src1->y, + ctx->ECC_p); /* y3 = (x1 - x3) * lambda - y1 */ SDRM_BN_Copy(EC_Dst->x, x3); SDRM_BN_Copy(EC_Dst->y, y3); @@ -295,54 +314,51 @@ int SDRM_CTX_EC_Add(SDRM_ECC_CTX *ctx, SDRM_EC_POINT* EC_Dst, SDRM_EC_POINT *EC_ } /* - * @fn SDRM_CTX_EC_Add_Jc - * @brief Chundnovsky Jacobian coordinate - * using montgomery (A = B + C) + * @fn SDRM_CTX_EC_Add_Jc + * @brief Chundnovsky Jacobian coordinate + * using montgomery (A = B + C) * - * @param EC_Dst [out]destination(A) - * @param EC_Src1 [in]first element(B) - * @param EC_Src2 [in]second element(C) - * @param new_a [in]ECC_A's montgomery value - * @param new_b [in]ECC_B's montgomery value - * @param Mont [in]montgomery context + * @param EC_Dst [out]destination(A) + * @param EC_Src1 [in]first element(B) + * @param EC_Src2 [in]second element(C) + * @param new_a [in]ECC_A's montgomery value + * @param new_b [in]ECC_B's montgomery value + * @param Mont [in]montgomery context * - * @return CRYPTO_SUCCESS if no error is occured - * \n CRYPTO_ERROR if evaluation is failed - * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed + * @return CRYPTO_SUCCESS if no error is occured + * \n CRYPTO_ERROR if evaluation is failed + * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed */ -int SDRM_CTX_EC_Add_Jc(SDRM_EC_POINT *EC_Dst, SDRM_EC_POINT *EC_Src1, SDRM_EC_POINT *EC_Src2, SDRM_BIG_NUM *new_a, SDRM_BIG_NUM *new_b, SDRM_BIG_MONT *Mont) +int SDRM_CTX_EC_Add_Jc(SDRM_EC_POINT *EC_Dst, SDRM_EC_POINT *EC_Src1, + SDRM_EC_POINT *EC_Src2, SDRM_BIG_NUM *new_a, SDRM_BIG_NUM *new_b, + SDRM_BIG_MONT *Mont) { - SDRM_BIG_NUM *u1, *u2, *s1, *s2, *h, *r, *tmp1, *tmp2; - cc_u8 *pbBuf = (cc_u8*)malloc(SDRM_ECC_ALLOC_SIZE * 8); + SDRM_BIG_NUM *u1, *u2, *s1, *s2, *h, *r, *tmp1, *tmp2; + cc_u8 *pbBuf = (cc_u8 *)malloc(SDRM_ECC_ALLOC_SIZE * 8); if (!pbBuf) - { return CRYPTO_MEMORY_ALLOC_FAIL; - } - if (EC_Src1->IsInfinity || SDRM_CHECK_EC_POINT_ZERO(EC_Src1)) - { + if (EC_Src1->IsInfinity || SDRM_CHECK_EC_POINT_ZERO(EC_Src1)) { SDRM_EC_COPY(EC_Dst, EC_Src2); free(pbBuf); return CRYPTO_SUCCESS; - } - else if (EC_Src2->IsInfinity || SDRM_CHECK_EC_POINT_ZERO(EC_Src2)) - { + } else if (EC_Src2->IsInfinity || SDRM_CHECK_EC_POINT_ZERO(EC_Src2)) { SDRM_EC_COPY(EC_Dst, EC_Src1); free(pbBuf); return CRYPTO_SUCCESS; } - u1 = SDRM_BN_Alloc(pbBuf , SDRM_ECC_BN_BUFSIZE); - u2 = SDRM_BN_Alloc((cc_u8*)u1 + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE); - s1 = SDRM_BN_Alloc((cc_u8*)u2 + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE); - s2 = SDRM_BN_Alloc((cc_u8*)s1 + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE); - h = SDRM_BN_Alloc((cc_u8*)s2 + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE); - r = SDRM_BN_Alloc((cc_u8*)h + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE); - tmp1 = SDRM_BN_Alloc((cc_u8*)r + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE); - tmp2 = SDRM_BN_Alloc((cc_u8*)tmp1 + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE); + u1 = SDRM_BN_Alloc(pbBuf, SDRM_ECC_BN_BUFSIZE); + u2 = SDRM_BN_Alloc((cc_u8 *)u1 + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE); + s1 = SDRM_BN_Alloc((cc_u8 *)u2 + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE); + s2 = SDRM_BN_Alloc((cc_u8 *)s1 + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE); + h = SDRM_BN_Alloc((cc_u8 *)s2 + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE); + r = SDRM_BN_Alloc((cc_u8 *)h + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE); + tmp1 = SDRM_BN_Alloc((cc_u8 *)r + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE); + tmp2 = SDRM_BN_Alloc((cc_u8 *)tmp1 + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE); // u1 SDRM_MONT_Mul(u1, EC_Src1->x, EC_Src2->z2, Mont); @@ -354,31 +370,25 @@ int SDRM_CTX_EC_Add_Jc(SDRM_EC_POINT *EC_Dst, SDRM_EC_POINT *EC_Src1, SDRM_EC_PO SDRM_MONT_Mul(s2, EC_Src2->y, EC_Src1->z3, Mont); SDRM_BN_Sub(h, u2, u1); + if (h->sign) - { SDRM_BN_Add(h, h, Mont->Mod); - } // r SDRM_BN_Sub(r, s2, s1); + if (r->sign) - { SDRM_BN_Add(r, r, Mont->Mod); - } // exception cases check - if (h->Length == 0) - { - if (r->Length == 0) - { + if (h->Length == 0) { + if (r->Length == 0) { // If (h == 0) & (r == 0), CTX_EC_Double_Jc // because B, C are same point. free(pbBuf); return SDRM_CTX_EC_Double_Jc(EC_Dst, EC_Src1, new_a, new_b, Mont); - } - else - { + } else { // If (h == 0) & (r != 0), A = Infinity point EC_Dst->IsInfinity = 1; free(pbBuf); @@ -389,15 +399,14 @@ int SDRM_CTX_EC_Add_Jc(SDRM_EC_POINT *EC_Dst, SDRM_EC_POINT *EC_Src1, SDRM_EC_PO // EC_Dst->x SDRM_MONT_Mul(EC_Dst->x, r, r, Mont); - SDRM_MONT_Mul(EC_Dst->y, h, h, Mont); // A->y : h^2, temp - SDRM_MONT_Mul(tmp1, EC_Dst->y, h, Mont); // tmp1 : h^3, temp - SDRM_MONT_Mul(EC_Dst->y, u1, EC_Dst->y, Mont); // A->y : u1*h^2 + SDRM_MONT_Mul(EC_Dst->y, h, h, Mont); // A->y : h^2, temp + SDRM_MONT_Mul(tmp1, EC_Dst->y, h, Mont); // tmp1 : h^3, temp + SDRM_MONT_Mul(EC_Dst->y, u1, EC_Dst->y, Mont); // A->y : u1*h^2 SDRM_BN_SHL(tmp2, EC_Dst->y, 1); + if (SDRM_BN_Cmp(tmp2, Mont->Mod) >= 0) - { SDRM_BN_Sub(tmp2, tmp2, Mont->Mod); - } SDRM_BN_ModSub(EC_Dst->x, EC_Dst->x, tmp2, Mont->Mod); SDRM_BN_ModSub(EC_Dst->x, EC_Dst->x, tmp1, Mont->Mod); @@ -426,46 +435,44 @@ int SDRM_CTX_EC_Add_Jc(SDRM_EC_POINT *EC_Dst, SDRM_EC_POINT *EC_Src1, SDRM_EC_PO } /* - * @fn SDRM_CTX_EC_Double_Jc - * @brief Chundnovsky Jacobian coordinate - * montgomery (A = 2B) + * @fn SDRM_CTX_EC_Double_Jc + * @brief Chundnovsky Jacobian coordinate + * montgomery (A = 2B) * - * @param EC_Dst [out]destination(A) - * @param EC_Src1 [in]first element(B) - * @param new_a [in]ECC_A's montgomery value - * @param new_b [in]ECC_B's montgomery value - * @param Mont [in]montgomery context + * @param EC_Dst [out]destination(A) + * @param EC_Src1 [in]first element(B) + * @param new_a [in]ECC_A's montgomery value + * @param new_b [in]ECC_B's montgomery value + * @param Mont [in]montgomery context * - * @return CRYPTO_SUCCESS if no error is occured - * \n CRYPTO_ERROR if evaluation is failed - * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed + * @return CRYPTO_SUCCESS if no error is occured + * \n CRYPTO_ERROR if evaluation is failed + * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed */ -int SDRM_CTX_EC_Double_Jc(SDRM_EC_POINT *EC_Dst, SDRM_EC_POINT *EC_Src1, SDRM_BIG_NUM *new_a, SDRM_BIG_NUM *new_b, SDRM_BIG_MONT *Mont) +int SDRM_CTX_EC_Double_Jc(SDRM_EC_POINT *EC_Dst, SDRM_EC_POINT *EC_Src1, + SDRM_BIG_NUM *new_a, SDRM_BIG_NUM *new_b, SDRM_BIG_MONT *Mont) { - SDRM_BIG_NUM *s, *k, *tmp1, *tmp2; - cc_u8 *pbBuf = (cc_u8*)malloc(SDRM_ECC_ALLOC_SIZE * 4); + SDRM_BIG_NUM *s, *k, *tmp1, *tmp2; + cc_u8 *pbBuf = (cc_u8 *)malloc(SDRM_ECC_ALLOC_SIZE * 4); if (!pbBuf) - { return CRYPTO_MEMORY_ALLOC_FAIL; - } // If B = infinite point || (B->y) = 0, A = infinite point. - if (EC_Src1->IsInfinity || SDRM_CHECK_EC_POINT_ZERO(EC_Src1)) - { + if (EC_Src1->IsInfinity || SDRM_CHECK_EC_POINT_ZERO(EC_Src1)) { EC_Dst->IsInfinity = 1; free(pbBuf); return CRYPTO_SUCCESS; } - s = SDRM_BN_Alloc(pbBuf , SDRM_ECC_BN_BUFSIZE); - k = SDRM_BN_Alloc((cc_u8*)s + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE); - tmp1 = SDRM_BN_Alloc((cc_u8*)k + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE); - tmp2 = SDRM_BN_Alloc((cc_u8*)tmp1 + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE); + s = SDRM_BN_Alloc(pbBuf, SDRM_ECC_BN_BUFSIZE); + k = SDRM_BN_Alloc((cc_u8 *)s + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE); + tmp1 = SDRM_BN_Alloc((cc_u8 *)k + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE); + tmp2 = SDRM_BN_Alloc((cc_u8 *)tmp1 + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE); // s - SDRM_MONT_Mul(s, EC_Src1->y, EC_Src1->y, Mont); // s = (B->y)^2 - SDRM_MONT_Mul(tmp1, s, s, Mont); // tmp1 = (B->y)^4 + SDRM_MONT_Mul(s, EC_Src1->y, EC_Src1->y, Mont); // s = (B->y)^2 + SDRM_MONT_Mul(tmp1, s, s, Mont); // tmp1 = (B->y)^4 SDRM_MONT_Mul(s, EC_Src1->x, s, Mont); SDRM_BN_SHL(s, s, 2); SDRM_BN_ModRed(s, s, Mont->Mod); @@ -481,10 +488,8 @@ int SDRM_CTX_EC_Double_Jc(SDRM_EC_POINT *EC_Dst, SDRM_EC_POINT *EC_Src1, SDRM_BI // t & EC_Dst->x SDRM_BN_SHL(tmp2, s, 1); - if (SDRM_BN_Cmp(tmp2, Mont->Mod)>=0) - { + if (SDRM_BN_Cmp(tmp2, Mont->Mod) >= 0) SDRM_BN_Sub(tmp2, tmp2, Mont->Mod); - } SDRM_MONT_Mul(EC_Dst->x, k, k, Mont); SDRM_BN_ModSub(EC_Dst->x, EC_Dst->x, tmp2, Mont->Mod); @@ -493,25 +498,21 @@ int SDRM_CTX_EC_Double_Jc(SDRM_EC_POINT *EC_Dst, SDRM_EC_POINT *EC_Src1, SDRM_BI SDRM_MONT_Mul(EC_Dst->z, EC_Src1->y, EC_Src1->z, Mont); SDRM_BN_SHL(EC_Dst->z, EC_Dst->z, 1); - if (SDRM_BN_Cmp(EC_Dst->z, Mont->Mod)>=0) - { + if (SDRM_BN_Cmp(EC_Dst->z, Mont->Mod) >= 0) SDRM_BN_Sub(EC_Dst->z, EC_Dst->z, Mont->Mod); - } // EC_Dst->y SDRM_BN_SHL(EC_Dst->y, tmp1, 3); - while(SDRM_BN_Cmp(EC_Dst->y, Mont->Mod) >= 0) - { + + while (SDRM_BN_Cmp(EC_Dst->y, Mont->Mod) >= 0) SDRM_BN_Sub(EC_Dst->y, EC_Dst->y, Mont->Mod); - } - SDRM_BN_ModSub(tmp1, s, EC_Dst->x, Mont->Mod); // tmp1 = s-t (s Ù²) - SDRM_MONT_Mul(tmp1, k, tmp1, Mont); // k(s-t) + SDRM_BN_ModSub(tmp1, s, EC_Dst->x, Mont->Mod); // tmp1 = s-t (s Ù²) + SDRM_MONT_Mul(tmp1, k, tmp1, Mont); // k(s-t) SDRM_BN_ModSub(EC_Dst->y, tmp1, EC_Dst->y, Mont->Mod); + if (EC_Dst->y->sign) - { SDRM_BN_Add(EC_Dst->y, EC_Dst->y, Mont->Mod); - } // EC_Dst->z2 SDRM_MONT_Mul(EC_Dst->z2, EC_Dst->z, EC_Dst->z, Mont); @@ -527,33 +528,31 @@ int SDRM_CTX_EC_Double_Jc(SDRM_EC_POINT *EC_Dst, SDRM_EC_POINT *EC_Src1, SDRM_BI /* - * @fn SDRM_CTX_EC_Double_Jm - * @brief Modified Jacobian coordinate - * montgomery (A = 2B) + * @fn SDRM_CTX_EC_Double_Jm + * @brief Modified Jacobian coordinate + * montgomery (A = 2B) * - * @param EC_Dst [out]destination(A) - * @param EC_Src1 [in]first element(B) - * @param new_a [in]ECC_A's montgomery value - * @param new_b [in]ECC_B's montgomery value - * @param Mont [in]montgomery context + * @param EC_Dst [out]destination(A) + * @param EC_Src1 [in]first element(B) + * @param new_a [in]ECC_A's montgomery value + * @param new_b [in]ECC_B's montgomery value + * @param Mont [in]montgomery context * - * @return CRYPTO_SUCCESS if no error is occured - * \n CRYPTO_ERROR if evaluation is failed - * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed + * @return CRYPTO_SUCCESS if no error is occured + * \n CRYPTO_ERROR if evaluation is failed + * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed */ -int SDRM_CTX_EC_Double_Jm(SDRM_EC_POINT *EC_Dst, SDRM_EC_POINT *EC_Src1, SDRM_BIG_NUM *new_a, SDRM_BIG_NUM *new_b, SDRM_BIG_MONT *Mont) +int SDRM_CTX_EC_Double_Jm(SDRM_EC_POINT *EC_Dst, SDRM_EC_POINT *EC_Src1, + SDRM_BIG_NUM *new_a, SDRM_BIG_NUM *new_b, SDRM_BIG_MONT *Mont) { - SDRM_BIG_NUM *a, *b, *c, *tmp1; - cc_u8 *pbBuf = (cc_u8*)malloc(SDRM_ECC_ALLOC_SIZE * 4); + SDRM_BIG_NUM *a, *b, *c, *tmp1; + cc_u8 *pbBuf = (cc_u8 *)malloc(SDRM_ECC_ALLOC_SIZE * 4); if (!pbBuf) - { return CRYPTO_MEMORY_ALLOC_FAIL; - } // If B is the infinite point or (B->y) is zero, A is the infinite point. - if (EC_Src1->IsInfinity || SDRM_CHECK_EC_POINT_ZERO(EC_Src1)) - { + if (EC_Src1->IsInfinity || SDRM_CHECK_EC_POINT_ZERO(EC_Src1)) { EC_Dst->IsInfinity = 1; free(pbBuf); @@ -561,29 +560,27 @@ int SDRM_CTX_EC_Double_Jm(SDRM_EC_POINT *EC_Dst, SDRM_EC_POINT *EC_Src1, SDRM_BI } a = SDRM_BN_Alloc(pbBuf, SDRM_ECC_BN_BUFSIZE); - b = SDRM_BN_Alloc((cc_u8*)a + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE); - c = SDRM_BN_Alloc((cc_u8*)b + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE); - tmp1 = SDRM_BN_Alloc((cc_u8*)c + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE); + b = SDRM_BN_Alloc((cc_u8 *)a + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE); + c = SDRM_BN_Alloc((cc_u8 *)b + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE); + tmp1 = SDRM_BN_Alloc((cc_u8 *)c + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE); // a SDRM_MONT_Mul(a, EC_Src1->x, EC_Src1->x, Mont); SDRM_BN_SHL(tmp1, a, 1); SDRM_BN_Add(a, tmp1, a); SDRM_BN_Add(a, a, EC_Src1->z2); - while(SDRM_BN_Cmp(a, Mont->Mod) >= 0) - { + + while (SDRM_BN_Cmp(a, Mont->Mod) >= 0) SDRM_BN_Sub(a, a, Mont->Mod); - } // b & c - SDRM_MONT_Mul(b, EC_Src1->y, EC_Src1->y, Mont); // b = (y1)^2 - SDRM_MONT_Mul(c, b, b, Mont); // c = (y1)^4 + SDRM_MONT_Mul(b, EC_Src1->y, EC_Src1->y, Mont); // b = (y1)^2 + SDRM_MONT_Mul(c, b, b, Mont); // c = (y1)^4 SDRM_MONT_Mul(b, EC_Src1->x, b, Mont); SDRM_BN_SHL(b, b, 1); - if (SDRM_BN_Cmp(b, Mont->Mod)>=0) - { + + if (SDRM_BN_Cmp(b, Mont->Mod) >= 0) SDRM_BN_Sub(b, b, Mont->Mod); - } // EC_Dst->x SDRM_MONT_Mul(EC_Dst->x, a, a, Mont); @@ -595,17 +592,13 @@ int SDRM_CTX_EC_Double_Jm(SDRM_EC_POINT *EC_Dst, SDRM_EC_POINT *EC_Src1, SDRM_BI // EC_Dst->y SDRM_BN_SHL(EC_Dst->y, EC_Dst->x, 1); - if (SDRM_BN_Cmp(EC_Dst->y, Mont->Mod)>=0) - { + if (SDRM_BN_Cmp(EC_Dst->y, Mont->Mod) >= 0) SDRM_BN_Sub(EC_Dst->y, EC_Dst->y, Mont->Mod); - } SDRM_BN_Sub(EC_Dst->y, b, EC_Dst->y); if (EC_Dst->y->sign) - { SDRM_BN_Add(EC_Dst->y, EC_Dst->y, Mont->Mod); - } SDRM_MONT_Mul(EC_Dst->y, a, EC_Dst->y, Mont); SDRM_BN_ModSub(EC_Dst->y, EC_Dst->y, c, Mont->Mod); @@ -619,172 +612,127 @@ int SDRM_CTX_EC_Double_Jm(SDRM_EC_POINT *EC_Dst, SDRM_EC_POINT *EC_Src1, SDRM_BI } /* - * @fn SDRM_CTX_EC_Chain - * @brief Chain Ô¼ - * signed wondow method : size of window = 4 - * chain for addition/subtraction of k Using sliding window method + * @fn SDRM_CTX_EC_Chain + * @brief Chain Ô¼ + * signed wondow method : size of window = 4 + * chain for addition/subtraction of k Using sliding window method * - * @param chain [out]destination - * @param L_Src [in]byte-length of chain - * @param Len_Src [in]number of doubling in chain - * @param k [in]source - * @param window_size [in]size of window + * @param chain [out]destination + * @param L_Src [in]byte-length of chain + * @param Len_Src [in]number of doubling in chain + * @param k [in]source + * @param window_size [in]size of window * - * @return CRYPTO_SUCCESS if no error is occured - * \n CRYPTO_INVALID_ARGUMENT if given value is incorrect + * @return CRYPTO_SUCCESS if no error is occured + * \n CRYPTO_INVALID_ARGUMENT if given value is incorrect */ -int SDRM_CTX_EC_Chain(signed char *chain, cc_u32 *L_Src, cc_u32 *Len_Src, SDRM_BIG_NUM *k, int window_size) +int SDRM_CTX_EC_Chain(signed char *chain, cc_u32 *L_Src, cc_u32 *Len_Src, + SDRM_BIG_NUM *k, int window_size) { - int i, j = 0, AddorSub, last = 0, doublings = 0; - int bits_k = 0, subtract=0, pos = 0, temp_1 = 0; - cc_u32 temp = 0; - cc_u32 numDoubling = 0; // number of doubling(= lshift) + int i, j = 0, AddorSub, last = 0, doublings = 0; + int bits_k = 0, subtract = 0, pos = 0, temp_1 = 0; + cc_u32 temp = 0; + cc_u32 numDoubling = 0; // number of doubling(= lshift) // k È¿ check if (k->sign) - { return CRYPTO_INVALID_ARGUMENT; - } - bits_k=(SDRM_BN_num_bits(k)-1); + bits_k = (SDRM_BN_num_bits(k) - 1); // sliding window method ('06) - while(bits_k>=0) - { - if ((bits_k + 1) < window_size) { + while (bits_k >= 0) { + if ((bits_k + 1) < window_size) window_size = bits_k + 1; - } if ((subtract == 0) || (subtract == 10)) - { AddorSub = 0; - } + else - { AddorSub = 1; - } - for(i = bits_k; i >= bits_k - window_size + 1; i--) - { + for (i = bits_k; i >= bits_k - window_size + 1; i--) { temp <<= 1; temp += AddorSub ^ SDRM_CheckBitUINT32(k->pData, i); } bits_k -= window_size; - if ((SDRM_CheckBitUINT32(k->pData, bits_k) == (cc_u32)1 - AddorSub) && (bits_k >= 0)) - { + if ((SDRM_CheckBitUINT32(k->pData, bits_k) == (cc_u32)1 - AddorSub) && + (bits_k >= 0)) { temp++; AddorSub = 1 - AddorSub; } - if ((bits_k == -1) && (AddorSub == 1)) { + if ((bits_k == -1) && (AddorSub == 1)) temp++; - } - if (bits_k>=0) - { - if (SDRM_CheckBitUINT32(k->pData, bits_k)==1) - { + if (bits_k >= 0) { + if (SDRM_CheckBitUINT32(k->pData, bits_k) == 1) { if ((subtract == 0) || (subtract == 10)) - { subtract = 1; - } + else - { subtract = 11; - } - for(temp_1 = 0 ; SDRM_CheckBitUINT32(k->pData, bits_k)==1; bits_k--) - { - if (bits_k >=0) - { + for (temp_1 = 0 ; SDRM_CheckBitUINT32(k->pData, bits_k) == 1; bits_k--) { + if (bits_k >= 0) temp_1++; - } } - } - else - { + } else { if ((subtract == 0) || (subtract == 10)) - { subtract = 0; - } + else - { subtract = 10; - } - for(temp_1 = 0 ; SDRM_CheckBitUINT32(k->pData, bits_k)==0; bits_k--) - { - if (bits_k >=0) - { + for (temp_1 = 0 ; SDRM_CheckBitUINT32(k->pData, bits_k) == 0; bits_k--) { + if (bits_k >= 0) temp_1++; - } } } if (bits_k < 0) - { last = 1; - } - } - else - { + } else { if ((subtract == 0) || (subtract == 10)) - { subtract = 0; - } + else - { subtract = 10; - } } j = temp >> window_size; - if (temp != 0) - { - for(doublings = 0; !(temp&0x1); doublings++) - { + if (temp != 0) { + for (doublings = 0; !(temp & 0x1); doublings++) temp >>= 1; - } + doublings += temp_1; - } - else - { + } else doublings = temp_1; - } - if (pos > 0) - { - for(i = temp ; i > j ; i>>=1) - { + if (pos > 0) { + for (i = temp ; i > j ; i >>= 1) { chain[++pos] = 0; numDoubling++; } } - if ((subtract==10) || (subtract == 11)) - { + if ((subtract == 10) || (subtract == 11)) chain[++pos] = (char)((~temp + 1) & 0xff); - } + else - { chain[++pos] = (char)((temp) & 0xff); - } - for( ; doublings > 0; doublings--) - { + for (; doublings > 0; doublings--) { chain[++pos] = 0; numDoubling++; } - if (last == 1) - { + if (last == 1) { if (AddorSub == 1) - { chain[++pos] = -1; - } } temp = 0; @@ -798,80 +746,82 @@ int SDRM_CTX_EC_Chain(signed char *chain, cc_u32 *L_Src, cc_u32 *Len_Src, SDRM_B } /* - * @fn SDRM_CTX_EC_kP - * @brief get EC_Dst = kP by Montgomery Method + * @fn SDRM_CTX_EC_kP + * @brief get EC_Dst = kP by Montgomery Method * - * @param ctx [in]ecc context - * @param EC_Dst [out]destination - * @param EC_Src [in]first element(P) - * @param k [in]second element(k) + * @param ctx [in]ecc context + * @param EC_Dst [out]destination + * @param EC_Src [in]first element(P) + * @param k [in]second element(k) * - * @return CRYPTO_SUCCESS if no error is occured - * \n CRYPTO_INVALID_ARGUMENT if the arguemnt represents a minus value - * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed - * \n CRYPTO_INFINITY_INPUT if the argument is a infinity value + * @return CRYPTO_SUCCESS if no error is occured + * \n CRYPTO_INVALID_ARGUMENT if the arguemnt represents a minus value + * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed + * \n CRYPTO_INFINITY_INPUT if the argument is a infinity value */ -int SDRM_CTX_EC_kP(SDRM_ECC_CTX *ctx, SDRM_EC_POINT* EC_Dst, SDRM_EC_POINT *EC_Src, SDRM_BIG_NUM *k) +int SDRM_CTX_EC_kP(SDRM_ECC_CTX *ctx, SDRM_EC_POINT *EC_Dst, + SDRM_EC_POINT *EC_Src, SDRM_BIG_NUM *k) { - int res, i; - int window_size = 4; // window size - int w_p = (1 << (window_size-1)) + 1; // pre-computation number -// int add = 0, subtract = 0; // add : num_(addition + subtract) - // subtract : 0 - before = 0 & after = 0 - // 10 - before = 1 & after = 0 - // 1 - before = 0 & after = 1 - // 11 - before = 1 & after = 1 - // => 0 : no subtract / 1 : subtract - SDRM_EC_POINT *Pw[9] = {0}; // number of precomputation data : 9 = w_p = 2^(window_size-1) + 1 - SDRM_BIG_MONT *Mont; - SDRM_BIG_NUM *new_a, *new_b; - SDRM_BIG_NUM *t1, *t2; - signed char chain[2 * SDRM_MAX_DIMENSION_ECC]; // DIMENSION_ECC : ecdsa.h define - cc_u32 length; // addition & subtrction chain length of k1 & k2 - cc_u32 lenD; // number of doubling of addition & subtrction chain of k1 & k2 - - cc_u8 *pbBuf = (cc_u8*)malloc(SDRM_ECC_ALLOC_SIZE * 4); + int res, i; + int window_size = 4; // window size + int w_p = (1 << (window_size - 1)) + 1; // pre-computation number + // int add = 0, subtract = 0; // add : num_(addition + subtract) + // subtract : 0 - before = 0 & after = 0 + // 10 - before = 1 & after = 0 + // 1 - before = 0 & after = 1 + // 11 - before = 1 & after = 1 + // => 0 : no subtract / 1 : subtract + SDRM_EC_POINT *Pw[9] = {0}; // number of precomputation data : 9 = w_p = 2^(window_size-1) + 1 + SDRM_BIG_MONT *Mont; + SDRM_BIG_NUM *new_a, *new_b; + SDRM_BIG_NUM *t1, *t2; + signed char + chain[2 * SDRM_MAX_DIMENSION_ECC]; // DIMENSION_ECC : ecdsa.h define + cc_u32 + length; // addition & subtrction chain length of k1 & k2 + cc_u32 + lenD; // number of doubling of addition & subtrction chain of k1 & k2 + + cc_u8 *pbBuf = (cc_u8 *)malloc(SDRM_ECC_ALLOC_SIZE * 4); + if (!pbBuf) - { return CRYPTO_MEMORY_ALLOC_FAIL; - } // k P È¿ check - if (k->sign) - { + if (k->sign) { free(pbBuf); return CRYPTO_INVALID_ARGUMENT; } - if (EC_Src->x->sign|EC_Src->y->sign) - { + if (EC_Src->x->sign | EC_Src->y->sign) { free(pbBuf); return CRYPTO_INVALID_ARGUMENT; } Mont = SDRM_MONT_Init(ctx->ECC_p->Size); - if (Mont == NULL) - { + + if (Mont == NULL) { free(pbBuf); return CRYPTO_MEMORY_ALLOC_FAIL; } SDRM_MONT_Set(Mont, ctx->ECC_p); - new_a = SDRM_BN_Alloc(pbBuf, SDRM_ECC_BN_BUFSIZE); - new_b = SDRM_BN_Alloc((cc_u8*)new_a + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE); - t1 = SDRM_BN_Alloc((cc_u8*)new_b + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE); - t2 = SDRM_BN_Alloc((cc_u8*)t1 + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE); + new_a = SDRM_BN_Alloc(pbBuf, SDRM_ECC_BN_BUFSIZE); + new_b = SDRM_BN_Alloc((cc_u8 *)new_a + SDRM_ECC_ALLOC_SIZE, + SDRM_ECC_BN_BUFSIZE); + t1 = SDRM_BN_Alloc((cc_u8 *)new_b + SDRM_ECC_ALLOC_SIZE, + SDRM_ECC_BN_BUFSIZE); + t2 = SDRM_BN_Alloc((cc_u8 *)t1 + SDRM_ECC_ALLOC_SIZE, + SDRM_ECC_BN_BUFSIZE); - if (SDRM_MONT_Zn2rzn(new_a, ctx->ECC_a, Mont) != CRYPTO_SUCCESS) - { + if (SDRM_MONT_Zn2rzn(new_a, ctx->ECC_a, Mont) != CRYPTO_SUCCESS) { free(pbBuf); SDRM_MONT_Free(Mont); return CRYPTO_ERROR; } - if (SDRM_MONT_Zn2rzn(new_b, ctx->ECC_b, Mont) != CRYPTO_SUCCESS) - { + if (SDRM_MONT_Zn2rzn(new_b, ctx->ECC_b, Mont) != CRYPTO_SUCCESS) { free(pbBuf); SDRM_MONT_Free(Mont); return CRYPTO_ERROR; @@ -880,31 +830,30 @@ int SDRM_CTX_EC_kP(SDRM_ECC_CTX *ctx, SDRM_EC_POINT* EC_Dst, SDRM_EC_POINT *EC_S //chain res = SDRM_CTX_EC_Chain(chain, &length, &lenD, k, window_size); - if (res != CRYPTO_SUCCESS) - { + + if (res != CRYPTO_SUCCESS) { free(pbBuf); SDRM_MONT_Free(Mont); return CRYPTO_ERROR; } - // pre-computation Data : Chunvosky algorithm - // Pw[1] = EC_Src - // Pw[2] = 3 * EC_Src - // Pw[3] = 5 * EC_Src - // Pw[4] = 7 * EC_Src - // .................. - for(i = 0; i < 9; i++) - { + // pre-computation Data : Chunvosky algorithm + // Pw[1] = EC_Src + // Pw[2] = 3 * EC_Src + // Pw[3] = 5 * EC_Src + // Pw[4] = 7 * EC_Src + // .................. + for (i = 0; i < 9; i++) { Pw[i] = SDRM_ECC_Init(); - if (Pw[i] == NULL) - { + + if (Pw[i] == NULL) { free(pbBuf); SDRM_MONT_Free(Mont); + while (i > 0) - { free(Pw[--i]); - } + return CRYPTO_MEMORY_ALLOC_FAIL; } } @@ -921,10 +870,9 @@ int SDRM_CTX_EC_kP(SDRM_ECC_CTX *ctx, SDRM_EC_POINT* EC_Dst, SDRM_EC_POINT *EC_S SDRM_EC_SET_ZERO(Pw[0]); SDRM_CTX_EC_Double_Jc(Pw[0], Pw[1], new_a, new_b, Mont); - for (i = 2; i < w_p; i++) - { + for (i = 2; i < w_p; i++) { SDRM_EC_SET_ZERO(Pw[i]); - SDRM_CTX_EC_Add_Jc(Pw[i], Pw[i-1], Pw[0], new_a, new_b, Mont); + SDRM_CTX_EC_Add_Jc(Pw[i], Pw[i - 1], Pw[0], new_a, new_b, Mont); SDRM_MONT_Mul(Pw[i]->z2, Pw[i]->z, Pw[i]->z, Mont); SDRM_MONT_Mul(Pw[i]->z3, Pw[i]->z2, Pw[i]->z, Mont); @@ -932,29 +880,25 @@ int SDRM_CTX_EC_kP(SDRM_ECC_CTX *ctx, SDRM_EC_POINT* EC_Dst, SDRM_EC_POINT *EC_S EC_Dst->IsInfinity = 1; - for(i = 0; i != (int)length; i++) - { - if (chain[i + 1]==0) - { + for (i = 0; i != (int)length; i++) { + if (chain[i + 1] == 0) { // EC_Dst = 2 * EC_Dst SDRM_CTX_EC_Double_Jm(EC_Dst, EC_Dst, new_a, new_b, Mont); lenD--; - } - else - { + } else { SDRM_Mont_Jm2Jc(EC_Dst, new_a, new_b, Mont); - if (chain[i + 1]>0) - { + + if (chain[i + 1] > 0) { // EC_Dst = EC_Dst + Pw[(chain[i + 1]+1)/2] - SDRM_CTX_EC_Add_Jc(EC_Dst, EC_Dst, Pw[(chain[i + 1]+1)/2], new_a, new_b, Mont); - } - else - { + SDRM_CTX_EC_Add_Jc(EC_Dst, EC_Dst, Pw[(chain[i + 1] + 1) / 2], new_a, new_b, + Mont); + } else { // EC_Dst = EC_Dst - Pw[(chain[i + 1]]+1)/2] - SDRM_EC_COPY(Pw[0], Pw[(-chain[i + 1]+1)/2]); + SDRM_EC_COPY(Pw[0], Pw[(-chain[i + 1] + 1) / 2]); SDRM_BN_Sub(Pw[0]->y, ctx->ECC_p, Pw[0]->y); SDRM_CTX_EC_Add_Jc(EC_Dst, EC_Dst, Pw[0], new_a, new_b, Mont); } + SDRM_Mont_Jc2Jm(EC_Dst, new_a, new_b, Mont); } } @@ -964,12 +908,10 @@ int SDRM_CTX_EC_kP(SDRM_ECC_CTX *ctx, SDRM_EC_POINT* EC_Dst, SDRM_EC_POINT *EC_S SDRM_MONT_Rzn2zn(EC_Dst->y, EC_Dst->y, Mont); SDRM_MONT_Rzn2zn(EC_Dst->z, EC_Dst->z, Mont); - if (EC_Dst->z->Length == 0) - { - for(i = 0; i < 9; i++) - { + if (EC_Dst->z->Length == 0) { + for (i = 0; i < 9; i++) free(Pw[i]); - } + free(pbBuf); SDRM_MONT_Free(Mont); @@ -977,6 +919,7 @@ int SDRM_CTX_EC_kP(SDRM_ECC_CTX *ctx, SDRM_EC_POINT* EC_Dst, SDRM_EC_POINT *EC_S return CRYPTO_INFINITY_INPUT; } + // Convert coordinate : "Modified Jacobian" => "Affine" // (EC_Dst->x) <= (EC_Dst->x) * { ((EC_Dst->z)^2)^-1 } // (EC_Dst->y) <= (EC_Dst->y) * { (2*((EC_Dst->z)^3))^-1 } @@ -991,10 +934,8 @@ int SDRM_CTX_EC_kP(SDRM_ECC_CTX *ctx, SDRM_EC_POINT* EC_Dst, SDRM_EC_POINT *EC_S SDRM_BN_ModMul(EC_Dst->y, EC_Dst->y, t2, ctx->ECC_p); // Memory Free - for(i = 0; i < 9; i++) - { + for (i = 0; i < 9; i++) free(Pw[i]); - } free(pbBuf); SDRM_MONT_Free(Mont); @@ -1003,51 +944,55 @@ int SDRM_CTX_EC_kP(SDRM_ECC_CTX *ctx, SDRM_EC_POINT* EC_Dst, SDRM_EC_POINT *EC_S } /* - * @fn SDRM_CTX_EC_2kP - * @brief get EC_Dst = k1*C1 + k2*C2 + * @fn SDRM_CTX_EC_2kP + * @brief get EC_Dst = k1*C1 + k2*C2 * - * @param ctx [in]ecc context - * @param EC_Dst [out]destination - * @param k1 [in]first element(k1) - * @param EC_Src1 [in]second element(C1) - * @param k2 [in]third element(k2) - * @param EC_Src2 [in]fourth element(C2) + * @param ctx [in]ecc context + * @param EC_Dst [out]destination + * @param k1 [in]first element(k1) + * @param EC_Src1 [in]second element(C1) + * @param k2 [in]third element(k2) + * @param EC_Src2 [in]fourth element(C2) * - * @return CRYPTO_SUCCESS if no error is occured - * \n CRYPTO_INVALID_ARGUMENT if the arguemnt represents a minus value - * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed - * \n CRYPTO_INFINITY_INPUT if the argument is a infinity value + * @return CRYPTO_SUCCESS if no error is occured + * \n CRYPTO_INVALID_ARGUMENT if the arguemnt represents a minus value + * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed + * \n CRYPTO_INFINITY_INPUT if the argument is a infinity value */ -int SDRM_CTX_EC_2kP(SDRM_ECC_CTX *ctx, SDRM_EC_POINT *EC_Dst, SDRM_BIG_NUM *k1, SDRM_EC_POINT *EC_Src1, SDRM_BIG_NUM *k2, SDRM_EC_POINT *EC_Src2) +int SDRM_CTX_EC_2kP(SDRM_ECC_CTX *ctx, SDRM_EC_POINT *EC_Dst, SDRM_BIG_NUM *k1, + SDRM_EC_POINT *EC_Src1, SDRM_BIG_NUM *k2, SDRM_EC_POINT *EC_Src2) { - signed char chain[2][2 * SDRM_MAX_DIMENSION_ECC]; // addition/subtrction chain of k1 k2 - cc_u32 length[2]; // addition/subtrction chain length of k1 k2 - cc_u32 lenD[2]; // # of doubling of addition/subtrction chain of k1 k2 - cc_u32 idx[2]; - int window_size = 4; // window size - int w2 = (1 << (window_size - 1)) + 1; // 2^(window_size-1)+1 : the precomputation point number - int i, j, res; - SDRM_EC_POINT *Pw[2][9]; // precomputation data - SDRM_BIG_MONT *Mont=NULL; - SDRM_BIG_NUM *new_a, *new_b; - SDRM_BIG_NUM *t1, *t2; // Used in coordinate change from "Modified Jacobian" to "Affine" - - cc_u8 *pbBuf = (cc_u8*)malloc(SDRM_ECC_ALLOC_SIZE * 4); + signed char + chain[2][2 * + SDRM_MAX_DIMENSION_ECC]; // addition/subtrction chain of k1 k2 + cc_u32 + length[2]; // addition/subtrction chain length of k1 k2 + cc_u32 + lenD[2]; // # of doubling of addition/subtrction chain of k1 k2 + cc_u32 idx[2]; + int window_size = 4; // window size + int w2 = (1 << (window_size - 1)) + + 1; // 2^(window_size-1)+1 : the precomputation point number + int i, j, res; + SDRM_EC_POINT *Pw[2][9]; // precomputation data + SDRM_BIG_MONT *Mont = NULL; + SDRM_BIG_NUM *new_a, *new_b; + SDRM_BIG_NUM *t1, + *t2; // Used in coordinate change from "Modified Jacobian" to "Affine" + + cc_u8 *pbBuf = (cc_u8 *)malloc(SDRM_ECC_ALLOC_SIZE * 4); + if (!pbBuf) - { return CRYPTO_MEMORY_ALLOC_FAIL; - } // k1 & k2 & C1 & C2 È¿ check - if (k1->sign | k2->sign) - { + if (k1->sign | k2->sign) { free(pbBuf); return CRYPTO_ERROR; } - if (EC_Src1->x->sign | EC_Src1->y->sign | EC_Src2->x->sign | EC_Src2->y->sign) - { + if (EC_Src1->x->sign | EC_Src1->y->sign | EC_Src2->x->sign | EC_Src2->y->sign) { free(pbBuf); return CRYPTO_ERROR; @@ -1056,20 +1001,21 @@ int SDRM_CTX_EC_2kP(SDRM_ECC_CTX *ctx, SDRM_EC_POINT *EC_Dst, SDRM_BIG_NUM *k1, Mont = SDRM_MONT_Init(ctx->ECC_p->Size); SDRM_MONT_Set(Mont, ctx->ECC_p); - new_a = SDRM_BN_Alloc(pbBuf, SDRM_ECC_BN_BUFSIZE); - new_b = SDRM_BN_Alloc((cc_u8*)new_a + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE); - t1 = SDRM_BN_Alloc((cc_u8*)new_b + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE); - t2 = SDRM_BN_Alloc((cc_u8*)t1 + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE); + new_a = SDRM_BN_Alloc(pbBuf, SDRM_ECC_BN_BUFSIZE); + new_b = SDRM_BN_Alloc((cc_u8 *)new_a + SDRM_ECC_ALLOC_SIZE, + SDRM_ECC_BN_BUFSIZE); + t1 = SDRM_BN_Alloc((cc_u8 *)new_b + SDRM_ECC_ALLOC_SIZE, + SDRM_ECC_BN_BUFSIZE); + t2 = SDRM_BN_Alloc((cc_u8 *)t1 + SDRM_ECC_ALLOC_SIZE, + SDRM_ECC_BN_BUFSIZE); - if (SDRM_MONT_Zn2rzn(new_a, ctx->ECC_a, Mont) != CRYPTO_SUCCESS) - { + if (SDRM_MONT_Zn2rzn(new_a, ctx->ECC_a, Mont) != CRYPTO_SUCCESS) { free(pbBuf); SDRM_MONT_Free(Mont); return CRYPTO_ERROR; } - if (SDRM_MONT_Zn2rzn(new_b, ctx->ECC_b, Mont) != CRYPTO_SUCCESS) - { + if (SDRM_MONT_Zn2rzn(new_b, ctx->ECC_b, Mont) != CRYPTO_SUCCESS) { free(pbBuf); SDRM_MONT_Free(Mont); return CRYPTO_ERROR; @@ -1077,37 +1023,33 @@ int SDRM_CTX_EC_2kP(SDRM_ECC_CTX *ctx, SDRM_EC_POINT *EC_Dst, SDRM_BIG_NUM *k1, // chain res = SDRM_CTX_EC_Chain(chain[0], &length[0], &lenD[0], k1, window_size); - if (res != CRYPTO_SUCCESS) - { + + if (res != CRYPTO_SUCCESS) { free(pbBuf); SDRM_MONT_Free(Mont); return res; } res = SDRM_CTX_EC_Chain(chain[1], &length[1], &lenD[1], k2, window_size); - if (res != CRYPTO_SUCCESS) - { + + if (res != CRYPTO_SUCCESS) { free(pbBuf); SDRM_MONT_Free(Mont); return res; } // Precomputation data - for(i = 0; i < 2; i++) - { -// Pw[i] = (SDRM_EC_POINT **)malloc(sizeof(SDRM_EC_POINT *) * w2); -// if (!Pw[i]) return CRYPTO_MEMORY_ALLOC_FAIL; - for(j = 0; j < 9; j++) - { + for (i = 0; i < 2; i++) { + // Pw[i] = (SDRM_EC_POINT **)malloc(sizeof(SDRM_EC_POINT *) * w2); + // if (!Pw[i]) return CRYPTO_MEMORY_ALLOC_FAIL; + for (j = 0; j < 9; j++) Pw[i][j] = SDRM_ECC_Init(); - } } SDRM_EC_COPY(Pw[0][1], EC_Src1); SDRM_EC_COPY(Pw[1][1], EC_Src2); - for (i=0;i<2;i++) - { + for (i = 0; i < 2; i++) { SDRM_MONT_Zn2rzn(Pw[i][1]->x, Pw[i][1]->x, Mont); SDRM_MONT_Zn2rzn(Pw[i][1]->y, Pw[i][1]->y, Mont); SDRM_MONT_Zn2rzn(Pw[i][1]->z, BN_One, Mont); @@ -1115,9 +1057,8 @@ int SDRM_CTX_EC_2kP(SDRM_ECC_CTX *ctx, SDRM_EC_POINT *EC_Dst, SDRM_BIG_NUM *k1, SDRM_BN_Copy(Pw[i][1]->z3, Pw[i][1]->z); SDRM_CTX_EC_Double_Jc(Pw[i][0], Pw[i][1], new_a, new_b, Mont); - for (j=2;jz2, Pw[i][j]->z, Pw[i][j]->z, Mont); SDRM_MONT_Mul(Pw[i][j]->z3, Pw[i][j]->z2, Pw[i][j]->z, Mont); } @@ -1127,42 +1068,35 @@ int SDRM_CTX_EC_2kP(SDRM_ECC_CTX *ctx, SDRM_EC_POINT *EC_Dst, SDRM_BIG_NUM *k1, idx[0] = idx[1] = 1; // ì¼± doubling Å« - if (lenD[0] != lenD[1]) - { + if (lenD[0] != lenD[1]) { i = ((lenD[0] > lenD[1]) ? 0 : 1); - for (;lenD[0] != lenD[1]; idx[i]++) - { - if (chain[i][idx[i]] == 0) - { + + for (; lenD[0] != lenD[1]; idx[i]++) { + if (chain[i][idx[i]] == 0) { // EC_Dst = 2EC_Dst SDRM_CTX_EC_Double_Jm(EC_Dst, EC_Dst, new_a, new_b, Mont); lenD[i]--; - } - else - { + } else { SDRM_Mont_Jm2Jc(EC_Dst, new_a, new_b, Mont); - if (chain[i][idx[i]]>0) - { + if (chain[i][idx[i]] > 0) { // EC_Dst = EC_Dst + Pw[i][(chain[i][idx[i]]+1)/2] - SDRM_CTX_EC_Add_Jc(EC_Dst, EC_Dst, Pw[i][(chain[i][idx[i]]+1)/2], new_a, new_b, Mont); - } - else - { + SDRM_CTX_EC_Add_Jc(EC_Dst, EC_Dst, Pw[i][(chain[i][idx[i]] + 1) / 2], new_a, + new_b, Mont); + } else { // EC_Dst = EC_Dst - Pw[i][(chain[i][idx[i]]+1)/2] - SDRM_EC_COPY(Pw[i][0], Pw[i][(-chain[i][idx[i]]+1)/2]); + SDRM_EC_COPY(Pw[i][0], Pw[i][(-chain[i][idx[i]] + 1) / 2]); SDRM_BN_Sub(Pw[i][0]->y, ctx->ECC_p, Pw[i][0]->y); SDRM_CTX_EC_Add_Jc(EC_Dst, EC_Dst, Pw[i][0], new_a, new_b, Mont); } + SDRM_Mont_Jc2Jm(EC_Dst, new_a, new_b, Mont); } } } - while ((idx[0] <= length[0]) && (idx[1] <= length[1])) - { - if ((chain[0][idx[0]] == 0) && (chain[1][idx[1]] == 0)) - { + while ((idx[0] <= length[0]) && (idx[1] <= length[1])) { + if ((chain[0][idx[0]] == 0) && (chain[1][idx[1]] == 0)) { // EC_Dst = 2EC_Dst SDRM_CTX_EC_Double_Jm(EC_Dst, EC_Dst, new_a, new_b, Mont); idx[0]++; @@ -1172,15 +1106,12 @@ int SDRM_CTX_EC_2kP(SDRM_ECC_CTX *ctx, SDRM_EC_POINT *EC_Dst, SDRM_BIG_NUM *k1, SDRM_Mont_Jm2Jc(EC_Dst, new_a, new_b, Mont); - if (chain[0][idx[0]]!=0) - { - if (chain[0][idx[0]]>0) - { + if (chain[0][idx[0]] != 0) { + if (chain[0][idx[0]] > 0) { // EC_Dst = EC_Dst + Pw[0][(chain[0][idx[0]]+1)/2] - SDRM_CTX_EC_Add_Jc(EC_Dst, EC_Dst, Pw[0][(chain[0][idx[0]] + 1) / 2], new_a, new_b, Mont); - } - else - { + SDRM_CTX_EC_Add_Jc(EC_Dst, EC_Dst, Pw[0][(chain[0][idx[0]] + 1) / 2], new_a, + new_b, Mont); + } else { // EC_Dst = EC_Dst - Pw[0][(chain[0][idx[0]]+1)/2] SDRM_EC_COPY(Pw[0][0], Pw[0][(-chain[0][idx[0]] + 1) / 2]); SDRM_BN_Sub(Pw[0][0]->y, ctx->ECC_p, Pw[0][0]->y); @@ -1189,51 +1120,47 @@ int SDRM_CTX_EC_2kP(SDRM_ECC_CTX *ctx, SDRM_EC_POINT *EC_Dst, SDRM_BIG_NUM *k1, idx[0]++; - if (chain[1][idx[1]] != 0) - { + if (chain[1][idx[1]] != 0) { // make z^2, z^3 for next computation SDRM_MONT_Mul(EC_Dst->z2, EC_Dst->z, EC_Dst->z, Mont); SDRM_MONT_Mul(EC_Dst->z3, EC_Dst->z2, EC_Dst->z, Mont); } } - if (chain[1][idx[1]]!=0) - { - if (chain[1][idx[1]]>0) - { + if (chain[1][idx[1]] != 0) { + if (chain[1][idx[1]] > 0) { // EC_Dst = EC_Dst + Pw[1][(chain[1][idx[1]]+1)/2] - SDRM_CTX_EC_Add_Jc(EC_Dst, EC_Dst, Pw[1][(chain[1][idx[1]]+1)/2], new_a, new_b, Mont); - } - else - { + SDRM_CTX_EC_Add_Jc(EC_Dst, EC_Dst, Pw[1][(chain[1][idx[1]] + 1) / 2], new_a, + new_b, Mont); + } else { // EC_Dst = EC_Dst - Pw[1][(chain[1][idx[1]]+1)/2] - SDRM_EC_COPY(Pw[1][0], Pw[1][(-chain[1][idx[1]]+1)/2]); + SDRM_EC_COPY(Pw[1][0], Pw[1][(-chain[1][idx[1]] + 1) / 2]); SDRM_BN_Sub(Pw[1][0]->y, ctx->ECC_p, Pw[1][0]->y); SDRM_CTX_EC_Add_Jc(EC_Dst, EC_Dst, Pw[1][0], new_a, new_b, Mont); } + idx[1]++; } + SDRM_Mont_Jc2Jm(EC_Dst, new_a, new_b, Mont); } - if ((idx[0]==length[0]) || (idx[1]==length[1])) - { - i = ((idx[0]==length[0]) ? 0 : 1); + if ((idx[0] == length[0]) || (idx[1] == length[1])) { + i = ((idx[0] == length[0]) ? 0 : 1); SDRM_Mont_Jm2Jc(EC_Dst, new_a, new_b, Mont); - if (chain[i][idx[i]]>0) - { + if (chain[i][idx[i]] > 0) { // EC_Dst = EC_Dst + Pw[i][(chain[i][idx[i]]+1)/2] - SDRM_CTX_EC_Add_Jc(EC_Dst, EC_Dst, Pw[i][(chain[i][idx[i]] + 1) / 2], new_a, new_b, Mont); - } - else - { + SDRM_CTX_EC_Add_Jc(EC_Dst, EC_Dst, Pw[i][(chain[i][idx[i]] + 1) / 2], new_a, + new_b, Mont); + } else { // EC_Dst = EC_Dst - Pw[i][(chain[i][idx[i]]+1)/2] SDRM_EC_COPY(Pw[i][0], Pw[i][(-chain[i][idx[i]] + 1) / 2]); SDRM_BN_Sub(Pw[i][0]->y, ctx->ECC_p, Pw[i][0]->y); SDRM_CTX_EC_Add_Jc(EC_Dst, EC_Dst, Pw[i][0], new_a, new_b, Mont); } + SDRM_Mont_Jc2Jm(EC_Dst, new_a, new_b, Mont); } @@ -1243,14 +1170,10 @@ int SDRM_CTX_EC_2kP(SDRM_ECC_CTX *ctx, SDRM_EC_POINT *EC_Dst, SDRM_BIG_NUM *k1, SDRM_MONT_Rzn2zn(EC_Dst->y, EC_Dst->y, Mont); SDRM_MONT_Rzn2zn(EC_Dst->z, EC_Dst->z, Mont); - if (EC_Dst->z->Length == 0) - { - for(i = 0; i < 2; i++) - { - for(j = 0; j < 9; j++) - { + if (EC_Dst->z->Length == 0) { + for (i = 0; i < 2; i++) { + for (j = 0; j < 9; j++) free(Pw[i][j]); - } } free(pbBuf); @@ -1261,7 +1184,7 @@ int SDRM_CTX_EC_2kP(SDRM_ECC_CTX *ctx, SDRM_EC_POINT *EC_Dst, SDRM_BIG_NUM *k1, return CRYPTO_INFINITY_INPUT; } - // ǥȯ : Modified Jacobian => Affine + // ǥȯ : Modified Jacobian => Affine // (EC_Dst->x) <= (EC_Dst->x) * { ((EC_Dst->z)^2)^-1 } // (EC_Dst->y) <= (EC_Dst->y) * { (2*((EC_Dst->z)^3))^-1 } SDRM_BN_ModMul(t1, EC_Dst->z, EC_Dst->z, ctx->ECC_p); @@ -1273,12 +1196,9 @@ int SDRM_CTX_EC_2kP(SDRM_ECC_CTX *ctx, SDRM_EC_POINT *EC_Dst, SDRM_BIG_NUM *k1, SDRM_BN_ModInv(t1, t2, ctx->ECC_p); SDRM_BN_ModMul(EC_Dst->y, EC_Dst->y, t1, ctx->ECC_p); - for(i = 0; i < 2; i++) - { - for(j = 0; j < 9; j++) - { + for (i = 0; i < 2; i++) { + for (j = 0; j < 9; j++) free(Pw[i][j]); - } } free(pbBuf); diff --git a/ssflib/dep/cryptocore/source/base/cc_fast_math.c b/ssflib/dep/cryptocore/source/base/cc_fast_math.c index 98f1cc2..102a924 100644 --- a/ssflib/dep/cryptocore/source/base/cc_fast_math.c +++ b/ssflib/dep/cryptocore/source/base/cc_fast_math.c @@ -31,144 +31,141 @@ #include "cc_fast_math.h" /** - * @fn SDRM_ll_Cmp - * @brief Compare two large unsigned integers + * @fn SDRM_ll_Cmp + * @brief Compare two large unsigned integers * - * @param pFirstOperand [in] the first operand - * @param pSecondOperand [in] the second operand + * @param pFirstOperand [in] the first operand + * @param pSecondOperand [in] the second operand * - * @return 0 if they are equal - * 1 if first bigger then second - * -1 if the seond one is bigger then first + * @return 0 if they are equal + * 1 if first bigger then second + * -1 if the seond one is bigger then first */ -int SDRM_ll_Cmp(const BasicWord *pFirstOperand, const BasicWord *pSecondOperand, unsigned uOperandLength) +int SDRM_ll_Cmp(const BasicWord *pFirstOperand, const BasicWord *pSecondOperand, + unsigned uOperandLength) { pFirstOperand += uOperandLength; pSecondOperand += uOperandLength; while (uOperandLength--) { - if (*--pFirstOperand != *--pSecondOperand) - { + if (*--pFirstOperand != *--pSecondOperand) { if (*pFirstOperand < *pSecondOperand) - { return -1; - } + else - { return 1; - } } } + return 0; } /** - * @fn SDRM_ll_Copy - * @brief Just copy two large unsigned integers from one into another + * @fn SDRM_ll_Copy + * @brief Just copy two large unsigned integers from one into another */ -void SDRM_ll_Copy(BasicWord *pFirstOperand, const BasicWord *pSecondOperand, unsigned uOperandLength) +void SDRM_ll_Copy(BasicWord *pFirstOperand, const BasicWord *pSecondOperand, + unsigned uOperandLength) { while (uOperandLength--) - { *pFirstOperand++ = *pSecondOperand++; - } } /** - * @fn SDRM_ll_bit_RShift - * @brief Shift large unsigned integer to the right by uBits + * @fn SDRM_ll_bit_RShift + * @brief Shift large unsigned integer to the right by uBits * - * @param pOperand [inout] pointer to the operand to be shifted + * @param pOperand [inout] pointer to the operand to be shifted * - * @return Nothing - * @warning We have to be careful when using this function because it modifies uOperandLength+1 words - * that is by 1 word bigger then operand original size. - * WWW....Operand...WWW|W <- it modifies the word immediately after the last one of passed operand. + * @return Nothing + * @warning We have to be careful when using this function because it modifies uOperandLength+1 words + * that is by 1 word bigger then operand original size. + * WWW....Operand...WWW|W <- it modifies the word immediately after the last one of passed operand. */ -void SDRM_ll_bit_RShift(IN OUT BasicWord *pOperand, IN BasicWord uOperandLength, OUT BasicWord uBits) +void SDRM_ll_bit_RShift(IN OUT BasicWord *pOperand, IN BasicWord uOperandLength, + OUT BasicWord uBits) { BasicWord uLastIndex = (BasicWord)(uOperandLength - 1); register BasicWord t; - while (uLastIndex--) - { + while (uLastIndex--) { t = *pOperand >> uBits; *pOperand = t | (*(pOperand + 1) << (BASICWORD_BITS_COUNT - uBits)); pOperand++; } + *pOperand >>= uBits; } /** - * @fn SDRM_ll_bit_LShift - * @brief Shift large unsigned integer to the left by uBits + * @fn SDRM_ll_bit_LShift + * @brief Shift large unsigned integer to the left by uBits * - * @param pOperand [inout] pointer to the operand to be shifted + * @param pOperand [inout] pointer to the operand to be shifted * - * @return Nothing - * @warning We have to be careful when using this function because it modifies uOperandLength+1 words - * that is by 1 word bigger then operand original size. - * It modifies the word immediately prior to the first one of passed operand -> W|WWW....Operand...WWW + * @return Nothing + * @warning We have to be careful when using this function because it modifies uOperandLength+1 words + * that is by 1 word bigger then operand original size. + * It modifies the word immediately prior to the first one of passed operand -> W|WWW....Operand...WWW */ -void SDRM_ll_bit_LShift(IN OUT BasicWord *pOperand, IN BasicWord uOperandLength, OUT BasicWord uBits) +void SDRM_ll_bit_LShift(IN OUT BasicWord *pOperand, IN BasicWord uOperandLength, + OUT BasicWord uBits) { BasicWord uLastIndex = (BasicWord)(uOperandLength - 1); BasicWord t; - pOperand += uOperandLength-1; - while (uLastIndex--) - { + pOperand += uOperandLength - 1; + + while (uLastIndex--) { t = *pOperand << uBits; *pOperand = t | (*(pOperand - 1) >> (BASICWORD_BITS_COUNT - uBits)); pOperand--; } + *pOperand <<= uBits; } /** - * @fn SDRM_ll_getMSW - * @brief Return index of most significant word. + * @fn SDRM_ll_getMSW + * @brief Return index of most significant word. * - * @param pOperand [in] pointer to the large integer. + * @param pOperand [in] pointer to the large integer. * - * @return The index of most significant word. - * -1 if passed integer actually is equal to 0. + * @return The index of most significant word. + * -1 if passed integer actually is equal to 0. */ int SDRM_ll_getMSW(IN const BasicWord *pOperand, IN BasicWord uOperandLength) { int nEl; - for(nEl = uOperandLength - 1; nEl >= 0; nEl--) - { + + for (nEl = uOperandLength - 1; nEl >= 0; nEl--) { if (0 != pOperand[nEl]) - { break; - } } + return nEl; } /** - * @fn SDRM_ll_getMSB - * @brief Find the leftmost non-zero bit in passed unsigned integer. + * @fn SDRM_ll_getMSB + * @brief Find the leftmost non-zero bit in passed unsigned integer. * - * @param oneWord [in] value of unsigned integer + * @param oneWord [in] value of unsigned integer * - * @return Position of leftmost non-zero bit. - * @warning Actually this function returns the position of leftmost non-zero bit started from the end of the integer. - * For example if we considering the unsigned integer with value 0x80000000 then SDRM_ll_getMSB will return 0 as a result. - * Or in the case if integer has value equal t 1, then SDRM_ll_getMSB will return BASICWORD_BITS_COUNT as a result. + * @return Position of leftmost non-zero bit. + * @warning Actually this function returns the position of leftmost non-zero bit started from the end of the integer. + * For example if we considering the unsigned integer with value 0x80000000 then SDRM_ll_getMSB will return 0 as a result. + * Or in the case if integer has value equal t 1, then SDRM_ll_getMSB will return BASICWORD_BITS_COUNT as a result. */ int SDRM_ll_getMSB(IN BasicWord oneWord) { - register BasicWord mask = (1 << (BASICWORD_BITS_COUNT-1)); + register BasicWord mask = (1 << (BASICWORD_BITS_COUNT - 1)); int nPos = 0; - if ( !oneWord ) { + if (!oneWord) return BASICWORD_BITS_COUNT; - } - while (!(oneWord & mask)) - { + while (!(oneWord & mask)) { nPos++; mask >>= 1; } @@ -177,13 +174,13 @@ int SDRM_ll_getMSB(IN BasicWord oneWord) } /** - * @fn SDRM_ll_bit_getBitValue - * @brief Return one bit value in the large integer number. + * @fn SDRM_ll_bit_getBitValue + * @brief Return one bit value in the large integer number. * - * @param pOperand [in] pointer to large integer - * @param nBit [in] bit position in the large integer. + * @param pOperand [in] pointer to large integer + * @param nBit [in] bit position in the large integer. * - * @return 0 or 1 depends on actual bit value. + * @return 0 or 1 depends on actual bit value. */ int SDRM_ll_bit_getBitValue(IN BasicWord *pOperand, IN BasicWord nBit) { @@ -194,70 +191,62 @@ int SDRM_ll_bit_getBitValue(IN BasicWord *pOperand, IN BasicWord nBit) } /** - * @fn [Mandatory] Function name - * @brief [Mandatory] Description of major features and algorithms + * @fn [Mandatory] Function name + * @brief [Mandatory] Description of major features and algorithms * - * @param [Optional] description of parameters ([one among in, out, inout]) + * @param [Optional] description of parameters ([one among in, out, inout]) * - * @return [Optional] description of return value - * @warning [Optional] constraints or notices - * @see [Optional] related information + * @return [Optional] description of return value + * @warning [Optional] constraints or notices + * @see [Optional] related information */ -int SDRM_ll_bit_getBitsValue(IN const BasicWord *pOperand, IN BasicWord uStartBit, IN BasicWord uBitsCount) +int SDRM_ll_bit_getBitsValue(IN const BasicWord *pOperand, + IN BasicWord uStartBit, IN BasicWord uBitsCount) { int nValueLen, i; BasicWord uValue = 0; - BasicWord uStartOrdNum = uStartBit / BASICWORD_BITS_COUNT; /* number of ords */ - BasicWord uStartBitNum = uStartBit % BASICWORD_BITS_COUNT; /* number of bits in remainder BasicWord */ + BasicWord uStartOrdNum = uStartBit / BASICWORD_BITS_COUNT; /* number of ords */ + BasicWord uStartBitNum = uStartBit % + BASICWORD_BITS_COUNT; /* number of bits in remainder BasicWord */ - if((nValueLen = (int)(uStartBitNum + 1 - uBitsCount)) >= 0) - { - for(i = uStartBitNum; i >= nValueLen; i--) - { + if ((nValueLen = (int)(uStartBitNum + 1 - uBitsCount)) >= 0) { + for (i = uStartBitNum; i >= nValueLen; i--) uValue = (BasicWord)(((pOperand[uStartOrdNum] >> i) & 1) | (uValue << 1)); - } - } - else - { + } else { nValueLen = uBitsCount - uStartBitNum - 1; - for(i = uStartBitNum; i >= 0; i--) - { + + for (i = uStartBitNum; i >= 0; i--) uValue = (BasicWord)(((pOperand[uStartOrdNum] >> i) & 1) | (uValue << 1)); - } uStartOrdNum--; nValueLen = BASICWORD_BITS_COUNT - nValueLen; - for(i = BASICWORD_BITS_COUNT - 1; i >= nValueLen; i--) - { + + for (i = BASICWORD_BITS_COUNT - 1; i >= nValueLen; i--) uValue = (BasicWord)(((pOperand[uStartOrdNum] >> i) & 1) | (uValue << 1)); - } } - do - { - if(0 != (uValue & 1)) - { + do { + if (0 != (uValue & 1)) break; - } + uValue >>= 1; - } - while(1); + } while (1); - uValue = uValue >> 1; /* get rid of least significant bit */ + uValue = uValue >> 1; /* get rid of least significant bit */ return uValue; } /** - * @fn SDRM_ll_Add - * @brief Add two large unsigned integers that have the same size. + * @fn SDRM_ll_Add + * @brief Add two large unsigned integers that have the same size. * - * @param pFirstOperand [in] pointer to first large integer - * @param pSecondOperand [in] pointer to second large integer - * @param uOperandsLength [in] length of the operands in words - * @param pResult [out] pointer to result of subtraction + * @param pFirstOperand [in] pointer to first large integer + * @param pSecondOperand [in] pointer to second large integer + * @param uOperandsLength [in] length of the operands in words + * @param pResult [out] pointer to result of subtraction * - * @return carry if so. + * @return carry if so. */ int SDRM_ll_Add(IN const BasicWord *pFirstOperand, IN const BasicWord *pSecondOperand, @@ -270,13 +259,13 @@ int SDRM_ll_Add(IN const BasicWord *pFirstOperand, fo = *pFirstOperand++; so = *pSecondOperand++; - _add_add_(fo,so,0,rl,rh) + _add_add_(fo, so, 0, rl, rh) *pResult++ = rl; - for (; i < uOperandsLength; i++) - { + + for (; i < uOperandsLength; i++) { fo = *pFirstOperand++; so = *pSecondOperand++; - _add_add_(fo,so,rh,rl,rh) + _add_add_(fo, so, rh, rl, rh) *pResult++ = rl; } @@ -284,46 +273,42 @@ int SDRM_ll_Add(IN const BasicWord *pFirstOperand, } /** - * @fn SDRM_ll_AddCarry - * @brief Add carry to large unsigned integer + * @fn SDRM_ll_AddCarry + * @brief Add carry to large unsigned integer * - * @param oneWord [in] value of carry - * @param pOperand [inout] pointer to large integer - * @param uOperandLength [in] length of the second operand in words + * @param oneWord [in] value of carry + * @param pOperand [inout] pointer to large integer + * @param uOperandLength [in] length of the second operand in words * - * @return carry if so. + * @return carry if so. */ -int SDRM_ll_AddCarry(IN BasicWord oneWord, IN BasicWord *pOperand, IN BasicWord uOperandLength) +int SDRM_ll_AddCarry(IN BasicWord oneWord, IN BasicWord *pOperand, + IN BasicWord uOperandLength) { - BasicWord i = 1; + BasicWord i = 1; register BasicWord ow = oneWord; if ((pOperand[0] += ow) >= ow) - { return 0; - } - while(i < uOperandLength) - { - if(++pOperand[i++] != 0) - { + while (i < uOperandLength) { + if (++pOperand[i++] != 0) return 0; - } } return 1; } /** - * @fn SDRM_ll_Sub - * @brief Subtract two large unsigned integers that have the same size. + * @fn SDRM_ll_Sub + * @brief Subtract two large unsigned integers that have the same size. * - * @param pFirstOperand [in] pointer to first large integer - * @param pSecondOperand [in] pointer to second large integer - * @param uOperandsLength [in] length of the operands in words - * @param pResult [out] pointer to result of subtraction + * @param pFirstOperand [in] pointer to first large integer + * @param pSecondOperand [in] pointer to second large integer + * @param uOperandsLength [in] length of the operands in words + * @param pResult [out] pointer to result of subtraction * - * @return borrow if so. + * @return borrow if so. */ int SDRM_ll_Sub(IN const BasicWord *pFirstOperand, IN const BasicWord *pSecondOperand, @@ -332,29 +317,30 @@ int SDRM_ll_Sub(IN const BasicWord *pFirstOperand, { register BasicWord temp, borrow = 0; - while (uOperandsLength--) - { + while (uOperandsLength--) { temp = *pFirstOperand - *pSecondOperand - borrow; - borrow = (borrow && (*pFirstOperand == *pSecondOperand)) || (*pFirstOperand < *pSecondOperand); + borrow = (borrow && (*pFirstOperand == *pSecondOperand)) || + (*pFirstOperand < *pSecondOperand); *pResult++ = temp; pFirstOperand++; pSecondOperand++; } + return (borrow); } /** - * @fn SDRM_ll_Mul1 - * @brief Multiply large integer by one word. - * Result = oneWord*SecondOperand. + * @fn SDRM_ll_Mul1 + * @brief Multiply large integer by one word. + * Result = oneWord*SecondOperand. * - * @param oneWord [in] value of first multiplayer. - * @param pSecondOperand [in] pointer to large integer - * @param uSecondOperandsLength [in] length of the second operand in words - * @param pResult [out] pointer to result of multiplication + * @param oneWord [in] value of first multiplayer. + * @param pSecondOperand [in] pointer to large integer + * @param uSecondOperandsLength [in] length of the second operand in words + * @param pResult [out] pointer to result of multiplication * - * @warning Routine doesn't store the last word of multiplication result, - * so we have to be carefull and take care about it after calling this function. + * @warning Routine doesn't store the last word of multiplication result, + * so we have to be carefull and take care about it after calling this function. */ BasicWord SDRM_ll_Mul1(IN BasicWord oneWord, IN BasicWord *pSecondOperand, BasicWord uSecondOperandsLength, @@ -367,8 +353,8 @@ BasicWord SDRM_ll_Mul1(IN BasicWord oneWord, r = *pResult; _mul_add_add(op2, ow, 0, 0, r, rh) *pResult++ = r; - while ( --uSecondOperandsLength ) - { + + while (--uSecondOperandsLength) { op2 = *pSecondOperand++; r = *pResult; _mul_add_add(op2, ow, 0, rh, r, rh) @@ -379,17 +365,17 @@ BasicWord SDRM_ll_Mul1(IN BasicWord oneWord, } /** - * @fn SDRM_ll_Mul1 - * @brief Multiply large integer by one word and add result to the another large integer. - * Result += oneWord*SecondOperand. + * @fn SDRM_ll_Mul1 + * @brief Multiply large integer by one word and add result to the another large integer. + * Result += oneWord*SecondOperand. * - * @param oneWord [in] value of first multiplayer. - * @param pSecondOperand [in] pointer to large integer - * @param uSecondOperandsLength [in] length of the second operand in words - * @param pResult [inout] pointer to result of multiplication + * @param oneWord [in] value of first multiplayer. + * @param pSecondOperand [in] pointer to large integer + * @param uSecondOperandsLength [in] length of the second operand in words + * @param pResult [inout] pointer to result of multiplication * - * @warning Routine doesn't store the last word of multiplication result, - * so we have to be carefull and take care about it after calling this function. + * @warning Routine doesn't store the last word of multiplication result, + * so we have to be carefull and take care about it after calling this function. */ BasicWord SDRM_ll_MulAdd1(IN BasicWord oneWord, IN BasicWord *pSecondOperand, BasicWord uSecondOperandsLength, @@ -402,8 +388,8 @@ BasicWord SDRM_ll_MulAdd1(IN BasicWord oneWord, r = *pResult; _mul_add_add(op2, ow, r, 0, r, rh) *pResult++ = r; - while (--uSecondOperandsLength) - { + + while (--uSecondOperandsLength) { op2 = *pSecondOperand++; r = *pResult; _mul_add_add(op2, ow, r, rh, r, rh) @@ -414,59 +400,63 @@ BasicWord SDRM_ll_MulAdd1(IN BasicWord oneWord, } /** - * @fn [Mandatory] Function name - * @brief [Mandatory] Description of major features and algorithms + * @fn [Mandatory] Function name + * @brief [Mandatory] Description of major features and algorithms * - * @param [Optional] description of parameters ([one among in, out, inout]) + * @param [Optional] description of parameters ([one among in, out, inout]) * - * @return [Optional] description of return value - * @warning [Optional] constraints or notices - * @see [Optional] related information + * @return [Optional] description of return value + * @warning [Optional] constraints or notices + * @see [Optional] related information */ -void SDRM_ll_MulAdd(IN BasicWord *pFirstOperand, IN BasicWord uFirstOperandsLength, +void SDRM_ll_MulAdd(IN BasicWord *pFirstOperand, + IN BasicWord uFirstOperandsLength, IN BasicWord *pSecondOperand, IN BasicWord uSecondOperandsLength, OUT BasicWord *pResult) { - while (uFirstOperandsLength--) - { - *(pResult+uSecondOperandsLength) = SDRM_ll_MulAdd1(*pFirstOperand++, pSecondOperand, uSecondOperandsLength, pResult); + while (uFirstOperandsLength--) { + *(pResult + uSecondOperandsLength) = SDRM_ll_MulAdd1(*pFirstOperand++, + pSecondOperand, uSecondOperandsLength, pResult); pResult++; } } /** - * @fn [Mandatory] Function name - * @brief [Mandatory] Description of major features and algorithms + * @fn [Mandatory] Function name + * @brief [Mandatory] Description of major features and algorithms * - * @param [Optional] description of parameters ([one among in, out, inout]) + * @param [Optional] description of parameters ([one among in, out, inout]) * - * @return [Optional] description of return value - * @warning [Optional] constraints or notices - * @see [Optional] related information + * @return [Optional] description of return value + * @warning [Optional] constraints or notices + * @see [Optional] related information */ void SDRM_ll_Mul(IN BasicWord *pFirstOperand, IN BasicWord uFirstOperandsLength, IN BasicWord *pSecondOperand, IN BasicWord uSecondOperandsLength, OUT BasicWord *pResult) { - *(pResult+uSecondOperandsLength) = SDRM_ll_Mul1(*pFirstOperand++, pSecondOperand, uSecondOperandsLength, pResult); - while (--uFirstOperandsLength) - { - *(pResult+uSecondOperandsLength) = SDRM_ll_MulAdd1(*pFirstOperand++, pSecondOperand, uSecondOperandsLength, pResult); + *(pResult + uSecondOperandsLength) = SDRM_ll_Mul1(*pFirstOperand++, + pSecondOperand, uSecondOperandsLength, pResult); + + while (--uFirstOperandsLength) { + *(pResult + uSecondOperandsLength) = SDRM_ll_MulAdd1(*pFirstOperand++, + pSecondOperand, uSecondOperandsLength, pResult); pResult++; } } /** - * @fn [Mandatory] Function name - * @brief [Mandatory] Description of major features and algorithms + * @fn [Mandatory] Function name + * @brief [Mandatory] Description of major features and algorithms * - * @param [Optional] description of parameters ([one among in, out, inout]) + * @param [Optional] description of parameters ([one among in, out, inout]) * - * @return [Optional] description of return value - * @warning [Optional] constraints or notices - * @see [Optional] related information + * @return [Optional] description of return value + * @warning [Optional] constraints or notices + * @see [Optional] related information */ -void SDRM_ll_Square(IN BasicWord *pOperand, IN BasicWord uOperandLength, OUT BasicWord *pResult) +void SDRM_ll_Square(IN BasicWord *pOperand, IN BasicWord uOperandLength, + OUT BasicWord *pResult) { BasicWord i; BasicWord j; @@ -474,11 +464,10 @@ void SDRM_ll_Square(IN BasicWord *pOperand, IN BasicWord uOperandLength, OUT Bas BasicWord len; /* Compute the product of diagonal elements */ - for(i = 0; i < uOperandLength; i++) - { + for (i = 0; i < uOperandLength; i++) { BasicWord rl, rh, op; op = pOperand[i]; - _mul_add_add(op,op,0,0,rl,rh) + _mul_add_add(op, op, 0, 0, rl, rh) pResult[i * 2] = rl; pResult[i * 2 + 1] = rh; } @@ -490,11 +479,12 @@ void SDRM_ll_Square(IN BasicWord *pOperand, IN BasicWord uOperandLength, OUT Bas i = 0; j = 0; len = uOperandLength; - while (--len) - { - t = SDRM_ll_MulAdd1(pOperand[i], pOperand+i+1, (BasicWord)len, pResult + j + 1); - SDRM_ll_AddCarry(t, pResult+len+j+1, len+1); - j+=2; + + while (--len) { + t = SDRM_ll_MulAdd1(pOperand[i], pOperand + i + 1, (BasicWord)len, + pResult + j + 1); + SDRM_ll_AddCarry(t, pResult + len + j + 1, len + 1); + j += 2; i++; } @@ -502,17 +492,15 @@ void SDRM_ll_Square(IN BasicWord *pOperand, IN BasicWord uOperandLength, OUT Bas SDRM_ll_bit_LShift(pResult, (BasicWord)(uOperandLength << 1), 1); /* Restore the least significant bit */ - if((pOperand[0] & 0x1L) != 0) - { + if ((pOperand[0] & 0x1L) != 0) pResult[0] |= 0x1L; - } } /** - * @fn SDRM_ll_Rem - * @brief Compute reminder of division. + * @fn SDRM_ll_Rem + * @brief Compute reminder of division. * - * @warning This is a temporary solution. It has been created mostly for testing purposes. + * @warning This is a temporary solution. It has been created mostly for testing purposes. */ int SDRM_ll_Rem(IN BasicWord *pOperand, IN BasicWord uOperandLengthInBytes, IN BasicWord *pModule, IN BasicWord uModuleLengthInBytes, @@ -525,11 +513,10 @@ int SDRM_ll_Rem(IN BasicWord *pOperand, IN BasicWord uOperandLengthInBytes, nWordX = SDRM_ll_getMSW(pOperand, nWordX) + 1; nWordP = SDRM_ll_getMSW(pModule, nWordX) + 1; - pTempResult = (BasicWord *)calloc(nWordX+1,BASICWORD_BYTES_COUNT); + pTempResult = (BasicWord *)calloc(nWordX + 1, BASICWORD_BYTES_COUNT); + if (!pTempResult) - { return CRYPTO_MEMORY_ALLOC_FAIL; - } SDRM_ll_Copy(pTempResult, pOperand, nWordX); @@ -543,35 +530,32 @@ int SDRM_ll_Rem(IN BasicWord *pOperand, IN BasicWord uOperandLengthInBytes, } /** - * @fn [Mandatory] Function name - * @brief [Mandatory] Description of major features and algorithms + * @fn [Mandatory] Function name + * @brief [Mandatory] Description of major features and algorithms * - * @param [Optional] description of parameters ([one among in, out, inout]) + * @param [Optional] description of parameters ([one among in, out, inout]) * - * @return [Optional] description of return value - * @warning [Optional] constraints or notices - * @see [Optional] related information + * @return [Optional] description of return value + * @warning [Optional] constraints or notices + * @see [Optional] related information */ int SDRM_ll_mont_Inverse(OUT BasicWord *out, IN BasicWord oneWord) { /* - t = m^(-1) mod b - m^(-1) = t*(2-m*t) mod (b^2) + t = m^(-1) mod b + m^(-1) = t*(2-m*t) mod (b^2) - So we are just using some simple iteration t <- t*(2-m*t) and check the condition that t*m == 1 mod b. + So we are just using some simple iteration t <- t*(2-m*t) and check the condition that t*m == 1 mod b. */ BasicWord t = oneWord; - BasicWord r = t*t; + BasicWord r = t * t; - while (r != 1) - { - t = t*(2 - r); - r = oneWord*t; + while (r != 1) { + t = t * (2 - r); + r = oneWord * t; if (!(r) && !(t)) - { return -1; - } } *out = (BasicWord)(-t); @@ -580,14 +564,14 @@ int SDRM_ll_mont_Inverse(OUT BasicWord *out, IN BasicWord oneWord) } /** - * @fn [Mandatory] Function name - * @brief [Mandatory] Description of major features and algorithms + * @fn [Mandatory] Function name + * @brief [Mandatory] Description of major features and algorithms * - * @param [Optional] description of parameters ([one among in, out, inout]) + * @param [Optional] description of parameters ([one among in, out, inout]) * - * @return [Optional] description of return value - * @warning [Optional] constraints or notices - * @see [Optional] related information + * @return [Optional] description of return value + * @warning [Optional] constraints or notices + * @see [Optional] related information */ void SDRM_ll_mont_Rem(IN OUT BasicWord *pFirstOperand, IN BasicWord *pModule, @@ -600,37 +584,32 @@ void SDRM_ll_mont_Rem(IN OUT BasicWord *pFirstOperand, temp_longs = uModuleLength; lp = SDRM_ll_getMSW(pModule, uModuleLength) + 1; - do - { + + do { temp = inv * pFirstOperand[0]; temp = SDRM_ll_MulAdd1(temp, pModule, lp, pFirstOperand); - carry += SDRM_ll_AddCarry(temp, pFirstOperand+uModuleLength, temp_longs); + carry += SDRM_ll_AddCarry(temp, pFirstOperand + uModuleLength, temp_longs); pFirstOperand++; - } - while(--temp_longs); + } while (--temp_longs); - while(carry) - { - if(SDRM_ll_Sub(pFirstOperand, pModule, uModuleLength, pFirstOperand)) { + while (carry) { + if (SDRM_ll_Sub(pFirstOperand, pModule, uModuleLength, pFirstOperand)) carry--; - } } - while(SDRM_ll_Cmp(pFirstOperand, pModule, uModuleLength) >= 0) - { + while (SDRM_ll_Cmp(pFirstOperand, pModule, uModuleLength) >= 0) SDRM_ll_Sub(pFirstOperand, pModule, uModuleLength, pFirstOperand); - } } /** - * @fn [Mandatory] Function name - * @brief [Mandatory] Description of major features and algorithms + * @fn [Mandatory] Function name + * @brief [Mandatory] Description of major features and algorithms * - * @param [Optional] description of parameters ([one among in, out, inout]) + * @param [Optional] description of parameters ([one among in, out, inout]) * - * @return [Optional] description of return value - * @warning [Optional] constraints or notices - * @see [Optional] related information + * @return [Optional] description of return value + * @warning [Optional] constraints or notices + * @see [Optional] related information */ int SDRM_ll_mont_Square(IN BasicWord *pFirstOperand, IN BasicWord *pModule, @@ -647,19 +626,20 @@ int SDRM_ll_mont_Square(IN BasicWord *pFirstOperand, /* Note: The next step for making toolkit faster is to redesign Montgomery functions and remove all memory allocation and copying from there. That means that exponentiation routine should be redesigne as well. */ - memcpy(pFirstOperand, pResult + uModuleLength, MUL_BY_ORD_BYTES_COUNT(uModuleLength)); + memcpy(pFirstOperand, pResult + uModuleLength, + MUL_BY_ORD_BYTES_COUNT(uModuleLength)); return CRYPTO_SUCCESS; } /** - * @fn [Mandatory] Function name - * @brief [Mandatory] Description of major features and algorithms + * @fn [Mandatory] Function name + * @brief [Mandatory] Description of major features and algorithms * - * @param [Optional] description of parameters ([one among in, out, inout]) + * @param [Optional] description of parameters ([one among in, out, inout]) * - * @return [Optional] description of return value - * @warning [Optional] constraints or notices - * @see [Optional] related information + * @return [Optional] description of return value + * @warning [Optional] constraints or notices + * @see [Optional] related information */ int SDRM_ll_mont_Mul(IN BasicWord *pFirstOperand, IN BasicWord *pSecondOperand, @@ -673,11 +653,10 @@ int SDRM_ll_mont_Mul(IN BasicWord *pFirstOperand, BasicWord *XY; /* pointer to product result */ P_longs = DIV_BY_ORD_BYTES_COUNT(uModuleLengthInBytes); - XY = (BasicWord*)calloc(2 * P_longs + 1, BASICWORD_BYTES_COUNT); - if(!XY) - { + XY = (BasicWord *)calloc(2 * P_longs + 1, BASICWORD_BYTES_COUNT); + + if (!XY) return CRYPTO_MEMORY_ALLOC_FAIL; - } /* Find leftmost non-zero elements */ lx = SDRM_ll_getMSW(pFirstOperand, P_longs) + 1; @@ -695,21 +674,21 @@ int SDRM_ll_mont_Mul(IN BasicWord *pFirstOperand, } /** - * @fn [Mandatory] Function name - * @brief [Mandatory] Description of major features and algorithms + * @fn [Mandatory] Function name + * @brief [Mandatory] Description of major features and algorithms * - * @param [Optional] description of parameters ([one among in, out, inout]) + * @param [Optional] description of parameters ([one among in, out, inout]) * - * @return [Optional] description of return value - * @warning [Optional] constraints or notices - * @see [Optional] related information + * @return [Optional] description of return value + * @warning [Optional] constraints or notices + * @see [Optional] related information */ #define _win_pval(i) (BasicWord*)(temp_1 + (i) * uOrdsP) -int SDRM_ll_ExpMod( IN BasicWord *pBase, IN BasicWord uBaseLengthInBytes, - IN BasicWord *pExponent, IN BasicWord uExponentLengthInBytes, - IN BasicWord *pModule, IN BasicWord uModuleLengthInBytes, - OUT BasicWord *pResult) +int SDRM_ll_ExpMod(IN BasicWord *pBase, IN BasicWord uBaseLengthInBytes, + IN BasicWord *pExponent, IN BasicWord uExponentLengthInBytes, + IN BasicWord *pModule, IN BasicWord uModuleLengthInBytes, + OUT BasicWord *pResult) { int nStatus = CRYPTO_SUCCESS; BasicWord *temp_1, inv; @@ -722,22 +701,23 @@ int SDRM_ll_ExpMod( IN BasicWord *pBase, IN BasicWord uBaseLengthInBytes, /* The values of num_squar array given below represents the lengths of particular window values in bits */ /* We have to take into account that we store only odd values. */ /* window values -> 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, etc... */ - /* num_squar -> 1, 2, 3, 3, 4, 4, 4, 4, 5, 5, etc... */ + /* num_squar -> 1, 2, 3, 3, 4, 4, 4, 4, 5, 5, etc... */ int num_squar[32] = {1, 2, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, - 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,}; + 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, + }; uOrdsY = DIV_BY_ORD_BYTES_COUNT(uExponentLengthInBytes); uOrdsP = DIV_BY_ORD_BYTES_COUNT(uModuleLengthInBytes); + /* Find the leftmost non-zero element of modulo */ - if(-1 == SDRM_ll_getMSW(pModule, uOrdsP)) { + if (-1 == SDRM_ll_getMSW(pModule, uOrdsP)) return CRYPTO_INVALID_ARGUMENT; - } + /* Find the leftmost non-zero element of exponent */ ly = SDRM_ll_getMSW(pExponent, uOrdsY); /* if exponent equal to 0 result is 1 */ - if(-1 == ly) - { + if (-1 == ly) { memset(pResult, 0, uModuleLengthInBytes); pResult[0] = 1; return CRYPTO_SUCCESS; @@ -748,61 +728,60 @@ int SDRM_ll_ExpMod( IN BasicWord *pBase, IN BasicWord uBaseLengthInBytes, /* Choose window length */ k = BASICWORD_BITS_COUNT * (ly + 1) - eb - 1; - if(k < 512) - { + + if (k < 512) win_len = 4; - } - else if((k >= 512) && (k < 1024)) - { + + else if ((k >= 512) && (k < 1024)) win_len = 5; - } - else - { /* for any k >= 1024 */ + + else { + /* for any k >= 1024 */ win_len = 6; } /* Obtain number of precomputed elements */ n_mem = (1 << (win_len - 1)) + 1; /* Allocate storage for precomputetd values */ - temp_1 = (BasicWord*)calloc((n_mem + 1) * uOrdsP, BASICWORD_BYTES_COUNT); + temp_1 = (BasicWord *)calloc((n_mem + 1) * uOrdsP, BASICWORD_BYTES_COUNT); + if (!temp_1) - { return CRYPTO_MEMORY_ALLOC_FAIL; - } /* Allocate temporary storages */ - m_temp = (BasicWord*)calloc(2 * uOrdsP + 1, BASICWORD_BYTES_COUNT); - if (!m_temp) - { + m_temp = (BasicWord *)calloc(2 * uOrdsP + 1, BASICWORD_BYTES_COUNT); + + if (!m_temp) { free(temp_1); return CRYPTO_MEMORY_ALLOC_FAIL; } - m_sq = (BasicWord*)malloc(2 * uModuleLengthInBytes + BASICWORD_BYTES_COUNT); - if (!m_sq) - { + + m_sq = (BasicWord *)malloc(2 * uModuleLengthInBytes + BASICWORD_BYTES_COUNT); + + if (!m_sq) { free(temp_1); free(m_temp); return CRYPTO_MEMORY_ALLOC_FAIL; } - do - { + do { /* Convert Base to Montgomery form */ inv = *pModule; - if (SDRM_ll_mont_Inverse(&inv, inv) != 0) - { + + if (SDRM_ll_mont_Inverse(&inv, inv) != 0) { nStatus = CRYPTO_INVERSE_NOT_EXIST; break; } + /* Move n up "mlen" words into a */ /* Actually we obtain X*R where R is Montgomery reduction coefficient */ memcpy(m_temp + uOrdsP, pBase, uBaseLengthInBytes); /* Do the division - dump the quotient in the high-order words */ - if((nStatus = SDRM_ll_Rem(m_temp, (BasicWord)(uBaseLengthInBytes + uModuleLengthInBytes), pModule, uModuleLengthInBytes, temp_1)) != CRYPTO_SUCCESS) - { + if ((nStatus = SDRM_ll_Rem(m_temp, + (BasicWord)(uBaseLengthInBytes + uModuleLengthInBytes), pModule, + uModuleLengthInBytes, temp_1)) != CRYPTO_SUCCESS) break; - } /* After this operation we will obtain X*R mod P */ memcpy(m_temp, temp_1, uModuleLengthInBytes); @@ -819,48 +798,47 @@ int SDRM_ll_ExpMod( IN BasicWord *pBase, IN BasicWord uBaseLengthInBytes, /* element (2) <- X^5*R = element (1) * X^2*R mod P */ /* element (3) <- X^7*R = element (2) * X^2*R mod P */ /* element (4) <- ... */ - for(i = 1; i < n_mem; i++) - { - SDRM_ll_mont_Mul(_win_pval(i - 1), _win_pval(n_mem), pModule, uModuleLengthInBytes, inv, _win_pval(i)); + for (i = 1; i < n_mem; i++) { + SDRM_ll_mont_Mul(_win_pval(i - 1), _win_pval(n_mem), pModule, + uModuleLengthInBytes, inv, _win_pval(i)); } /* OK, now let compute R mod P */ memset(m_temp, 0, 2 * uModuleLengthInBytes + 1); m_temp[uOrdsP] = 1; - if((nStatus = SDRM_ll_Rem(m_temp, (BasicWord)(uModuleLengthInBytes + BASICWORD_BYTES_COUNT), pModule, uModuleLengthInBytes, m_temp)) != CRYPTO_SUCCESS) { + + if ((nStatus = SDRM_ll_Rem(m_temp, + (BasicWord)(uModuleLengthInBytes + BASICWORD_BYTES_COUNT), pModule, + uModuleLengthInBytes, m_temp)) != CRYPTO_SUCCESS) break; - } /* Compute the exponent */ - for(i = k; i >= win_len-1; ) - { + for (i = k; i >= win_len - 1;) { /* Note: I don't like this solution, but it was easy and from that point of view was suitable for short development cycle. During further refactoring exponent bits processing should be changed in a way that makes possible to perform all computations inside of the one cycle. See some additional related comments right after the body of this cycle.*/ /* Find next suitable bits for computations */ - nIndex = (BasicWord)SDRM_ll_bit_getBitsValue(pExponent, (BasicWord)i, (BasicWord)win_len); + nIndex = (BasicWord)SDRM_ll_bit_getBitsValue(pExponent, (BasicWord)i, + (BasicWord)win_len); /* Square the intermediate result */ - for(j = 0; j < num_squar[nIndex]; j++) - { + for (j = 0; j < num_squar[nIndex]; j++) SDRM_ll_mont_Square(m_temp, pModule, uOrdsP, inv, m_sq); - } /* Multiply with the precomputed data */ - SDRM_ll_mont_Mul(m_temp, _win_pval(nIndex), pModule, uModuleLengthInBytes, inv, m_temp); + SDRM_ll_mont_Mul(m_temp, _win_pval(nIndex), pModule, uModuleLengthInBytes, inv, + m_temp); /* Square (win_len - num_squar) times */ - for(j = 0 ; j < win_len - num_squar[nIndex]; j++) - { + for (j = 0 ; j < win_len - num_squar[nIndex]; j++) SDRM_ll_mont_Square(m_temp, pModule, uOrdsP, inv, m_sq); - } i -= win_len; /* perform squering till first nonzero bit */ - while((i >= win_len - 1) && !SDRM_ll_bit_getBitValue(pExponent, (BasicWord)i)) - { + while ((i >= win_len - 1) && + !SDRM_ll_bit_getBitValue(pExponent, (BasicWord)i)) { SDRM_ll_mont_Square(m_temp, pModule, uOrdsP, inv, m_sq); i--; } @@ -871,29 +849,26 @@ int SDRM_ll_ExpMod( IN BasicWord *pBase, IN BasicWord uBaseLengthInBytes, /* if we still have some bit(s) ... */ /* perform squering till first nonzero bit */ - while((i >= 0) && !SDRM_ll_bit_getBitValue(pExponent, (BasicWord)i)) - { + while ((i >= 0) && !SDRM_ll_bit_getBitValue(pExponent, (BasicWord)i)) { SDRM_ll_mont_Square(m_temp, pModule, uOrdsP, inv, m_sq); i--; } /* if we still have some nonzero bit(s) ... */ - if(i >= 0) - { - nIndex = (BasicWord)SDRM_ll_bit_getBitsValue(pExponent, (BasicWord)i, (BasicWord)(i + 1)); - for(j = 0; j < num_squar[nIndex]; j++) - { + if (i >= 0) { + nIndex = (BasicWord)SDRM_ll_bit_getBitsValue(pExponent, (BasicWord)i, + (BasicWord)(i + 1)); + + for (j = 0; j < num_squar[nIndex]; j++) SDRM_ll_mont_Square(m_temp, pModule, uOrdsP, inv, m_sq); - } /* Multiply with precomputed data*/ - SDRM_ll_mont_Mul(m_temp, _win_pval(nIndex), pModule, uModuleLengthInBytes, inv, m_temp); + SDRM_ll_mont_Mul(m_temp, _win_pval(nIndex), pModule, uModuleLengthInBytes, inv, + m_temp); /* Square (win_len - num_squar) times */ - for(j = 0 ; j <= i - num_squar[nIndex]; j++) - { + for (j = 0 ; j <= i - num_squar[nIndex]; j++) SDRM_ll_mont_Square(m_temp, pModule, uOrdsP, inv, m_sq); - } } /* Convert the result out of Montgomery form */ @@ -903,9 +878,8 @@ int SDRM_ll_ExpMod( IN BasicWord *pBase, IN BasicWord uBaseLengthInBytes, /* So we just need to remove that additional R */ pResult[0]--; - break; /* always break this loop */ - } - while(0); + break; /* always break this loop */ + } while (0); free(temp_1); free(m_temp); diff --git a/ssflib/dep/cryptocore/source/base/cc_hash.c b/ssflib/dep/cryptocore/source/base/cc_hash.c index fd4bea3..b726b59 100644 --- a/ssflib/dep/cryptocore/source/base/cc_hash.c +++ b/ssflib/dep/cryptocore/source/base/cc_hash.c @@ -33,20 +33,18 @@ // functions //////////////////////////////////////////////////////////////////////////// /* - * @fn SDRM_SHA1_init - * @brief initialize CryptoCoreContainer context + * @fn SDRM_SHA1_init + * @brief initialize CryptoCoreContainer context * - * @param crt [out]CryptoCoreContainer context + * @param crt [out]CryptoCoreContainer context * - * @return CRYPTO_SUCCESS if no error is occured - * \n CRYPTO_NULL_POINTER if given argument is a null pointer + * @return CRYPTO_SUCCESS if no error is occured + * \n CRYPTO_NULL_POINTER if given argument is a null pointer */ int SDRM_SHA1_init(CryptoCoreContainer *crt) { if ((crt == NULL) || (crt->ctx == NULL)) - { return CRYPTO_NULL_POINTER; - } SDRM_SHA1_Init(crt->ctx->sha1ctx); @@ -54,22 +52,20 @@ int SDRM_SHA1_init(CryptoCoreContainer *crt) } /* - * @fn SDRM_SHA1_update - * @brief process a message block + * @fn SDRM_SHA1_update + * @brief process a message block * - * @param crt [out]CryptoCoreContainer context - * @param msg [in]message - * @param msglen [in]cc_u8-length of msg + * @param crt [out]CryptoCoreContainer context + * @param msg [in]message + * @param msglen [in]cc_u8-length of msg * - * @return CRYPTO_SUCCESS if no error is occured - * \n CRYPTO_NULL_POINTER if given argument is a null pointer + * @return CRYPTO_SUCCESS if no error is occured + * \n CRYPTO_NULL_POINTER if given argument is a null pointer */ int SDRM_SHA1_update(CryptoCoreContainer *crt, cc_u8 *msg, cc_u32 msglen) { if ((crt == NULL) || (crt->ctx == NULL)) - { return CRYPTO_NULL_POINTER; - } SDRM_SHA1_Update(crt->ctx->sha1ctx, msg, msglen); @@ -77,21 +73,19 @@ int SDRM_SHA1_update(CryptoCoreContainer *crt, cc_u8 *msg, cc_u32 msglen) } /* - * @fn SDRM_SHA1_final - * @brief get hashed message + * @fn SDRM_SHA1_final + * @brief get hashed message * - * @param crt [in]CryptoCoreContainer context - * @param output [out]hashed message + * @param crt [in]CryptoCoreContainer context + * @param output [out]hashed message * - * @return CRYPTO_SUCCESS if no error is occured - * \n CRYPTO_NULL_POINTER if given argument is a null pointer + * @return CRYPTO_SUCCESS if no error is occured + * \n CRYPTO_NULL_POINTER if given argument is a null pointer */ int SDRM_SHA1_final(CryptoCoreContainer *crt, cc_u8 *output) { if ((crt == NULL) || (crt->ctx == NULL) || (output == NULL)) - { return CRYPTO_NULL_POINTER; - } SDRM_SHA1_Final(crt->ctx->sha1ctx, output); @@ -99,23 +93,22 @@ int SDRM_SHA1_final(CryptoCoreContainer *crt, cc_u8 *output) } /* - * @fn SDRM_SHA1_hash - * @brief get hashed message from message + * @fn SDRM_SHA1_hash + * @brief get hashed message from message * - * @param crt [in]CryptoCoreContainer context - * @param msg [in]message - * @param msglen [in]byte-length of msg - * @param output [out]hashed message + * @param crt [in]CryptoCoreContainer context + * @param msg [in]message + * @param msglen [in]byte-length of msg + * @param output [out]hashed message * - * @return CRYPTO_SUCCESS if no error is occured - * \n CRYPTO_NULL_POINTER if given argument is a null pointer + * @return CRYPTO_SUCCESS if no error is occured + * \n CRYPTO_NULL_POINTER if given argument is a null pointer */ -int SDRM_SHA1_hash(CryptoCoreContainer *crt, cc_u8 *msg, cc_u32 msglen, cc_u8 *output) +int SDRM_SHA1_hash(CryptoCoreContainer *crt, cc_u8 *msg, cc_u32 msglen, + cc_u8 *output) { if ((crt == NULL) || (crt->ctx == NULL)) - { return CRYPTO_NULL_POINTER; - } SDRM_SHA1_Init(crt->ctx->sha1ctx); SDRM_SHA1_Update(crt->ctx->sha1ctx, msg, msglen); @@ -125,20 +118,18 @@ int SDRM_SHA1_hash(CryptoCoreContainer *crt, cc_u8 *msg, cc_u32 msglen, cc_u8 *o } /* - * @fn SDRM_SHA224_init - * @brief initialize CryptoCoreContainer context + * @fn SDRM_SHA224_init + * @brief initialize CryptoCoreContainer context * - * @param crt [out]CryptoCoreContainer context + * @param crt [out]CryptoCoreContainer context * - * @return CRYPTO_SUCCESS if no error is occured - * \n CRYPTO_NULL_POINTER if given argument is a null pointer + * @return CRYPTO_SUCCESS if no error is occured + * \n CRYPTO_NULL_POINTER if given argument is a null pointer */ int SDRM_SHA224_init(CryptoCoreContainer *crt) { if ((crt == NULL) || (crt->ctx == NULL)) - { return CRYPTO_NULL_POINTER; - } SDRM_SHA224_Init(crt->ctx->sha224ctx); @@ -146,22 +137,20 @@ int SDRM_SHA224_init(CryptoCoreContainer *crt) } /* - * @fn SDRM_SHA224_update - * @brief process a message block + * @fn SDRM_SHA224_update + * @brief process a message block * - * @param crt [out]CryptoCoreContainer context - * @param msg [in]message - * @param msglen [in]byte-length of msg + * @param crt [out]CryptoCoreContainer context + * @param msg [in]message + * @param msglen [in]byte-length of msg * - * @return CRYPTO_SUCCESS if no error is occured - * \n CRYPTO_NULL_POINTER if given argument is a null pointer + * @return CRYPTO_SUCCESS if no error is occured + * \n CRYPTO_NULL_POINTER if given argument is a null pointer */ int SDRM_SHA224_update(CryptoCoreContainer *crt, cc_u8 *msg, cc_u32 msglen) { if ((crt == NULL) || (crt->ctx == NULL)) - { return CRYPTO_NULL_POINTER; - } SDRM_SHA224_Update(crt->ctx->sha224ctx, msg, msglen); @@ -169,20 +158,19 @@ int SDRM_SHA224_update(CryptoCoreContainer *crt, cc_u8 *msg, cc_u32 msglen) } /* - * @fn SDRM_SHA224_final - * @brief get hashed message + * @fn SDRM_SHA224_final + * @brief get hashed message * - * @param crt [in]CryptoCoreContainer context - * @param output [out]hashed message + * @param crt [in]CryptoCoreContainer context + * @param output [out]hashed message * - * @return CRYPTO_SUCCESS if no error is occured - * \n CRYPTO_NULL_POINTER if given argument is a null pointer + * @return CRYPTO_SUCCESS if no error is occured + * \n CRYPTO_NULL_POINTER if given argument is a null pointer */ int SDRM_SHA224_final(CryptoCoreContainer *crt, cc_u8 *output) { - if ((crt == NULL) || (crt->ctx == NULL) || (output == NULL)) { + if ((crt == NULL) || (crt->ctx == NULL) || (output == NULL)) return CRYPTO_NULL_POINTER; - } SDRM_SHA224_Final(crt->ctx->sha224ctx, output); @@ -190,23 +178,22 @@ int SDRM_SHA224_final(CryptoCoreContainer *crt, cc_u8 *output) } /* - * @fn SDRM_SHA224_hash - * @brief get hashed message from message + * @fn SDRM_SHA224_hash + * @brief get hashed message from message * - * @param crt [in]CryptoCoreContainer context - * @param msg [in]message - * @param msglen [in]byte-length of msg - * @param output [out]hashed message + * @param crt [in]CryptoCoreContainer context + * @param msg [in]message + * @param msglen [in]byte-length of msg + * @param output [out]hashed message * - * @return CRYPTO_SUCCESS if no error is occured - * \n CRYPTO_NULL_POINTER if given argument is a null pointer + * @return CRYPTO_SUCCESS if no error is occured + * \n CRYPTO_NULL_POINTER if given argument is a null pointer */ -int SDRM_SHA224_hash(CryptoCoreContainer *crt, cc_u8 *msg, cc_u32 msglen, cc_u8 *output) +int SDRM_SHA224_hash(CryptoCoreContainer *crt, cc_u8 *msg, cc_u32 msglen, + cc_u8 *output) { if ((crt == NULL) || (crt->ctx == NULL)) - { return CRYPTO_NULL_POINTER; - } SDRM_SHA224_Init(crt->ctx->sha224ctx); SDRM_SHA224_Update(crt->ctx->sha224ctx, msg, msglen); @@ -216,20 +203,18 @@ int SDRM_SHA224_hash(CryptoCoreContainer *crt, cc_u8 *msg, cc_u32 msglen, cc_u8 } /* - * @fn SDRM_SHA256_init - * @brief initialize CryptoCoreContainer context + * @fn SDRM_SHA256_init + * @brief initialize CryptoCoreContainer context * - * @param crt [out]CryptoCoreContainer context + * @param crt [out]CryptoCoreContainer context * - * @return CRYPTO_SUCCESS if no error is occured - * \n CRYPTO_NULL_POINTER if given argument is a null pointer + * @return CRYPTO_SUCCESS if no error is occured + * \n CRYPTO_NULL_POINTER if given argument is a null pointer */ int SDRM_SHA256_init(CryptoCoreContainer *crt) { if ((crt == NULL) || (crt->ctx == NULL)) - { return CRYPTO_NULL_POINTER; - } SDRM_SHA256_Init(crt->ctx->sha256ctx); @@ -237,22 +222,20 @@ int SDRM_SHA256_init(CryptoCoreContainer *crt) } /* - * @fn SDRM_SHA256_update - * @brief process a message block + * @fn SDRM_SHA256_update + * @brief process a message block * - * @param crt [out]CryptoCoreContainer context - * @param msg [in]message - * @param msglen [in]byte-length of msg + * @param crt [out]CryptoCoreContainer context + * @param msg [in]message + * @param msglen [in]byte-length of msg * - * @return CRYPTO_SUCCESS if no error is occured - * \n CRYPTO_NULL_POINTER if given argument is a null pointer + * @return CRYPTO_SUCCESS if no error is occured + * \n CRYPTO_NULL_POINTER if given argument is a null pointer */ int SDRM_SHA256_update(CryptoCoreContainer *crt, cc_u8 *msg, cc_u32 msglen) { if ((crt == NULL) || (crt->ctx == NULL)) - { return CRYPTO_NULL_POINTER; - } SDRM_SHA256_Update(crt->ctx->sha256ctx, msg, msglen); @@ -260,20 +243,19 @@ int SDRM_SHA256_update(CryptoCoreContainer *crt, cc_u8 *msg, cc_u32 msglen) } /* - * @fn SDRM_SHA256_final - * @brief get hashed message + * @fn SDRM_SHA256_final + * @brief get hashed message * - * @param crt [in]CryptoCoreContainer context - * @param output [out]hashed message + * @param crt [in]CryptoCoreContainer context + * @param output [out]hashed message * - * @return CRYPTO_SUCCESS if no error is occured - * \n CRYPTO_NULL_POINTER if given argument is a null pointer + * @return CRYPTO_SUCCESS if no error is occured + * \n CRYPTO_NULL_POINTER if given argument is a null pointer */ int SDRM_SHA256_final(CryptoCoreContainer *crt, cc_u8 *output) { - if ((crt == NULL) || (crt->ctx == NULL) || (output == NULL)) { + if ((crt == NULL) || (crt->ctx == NULL) || (output == NULL)) return CRYPTO_NULL_POINTER; - } SDRM_SHA256_Final(crt->ctx->sha256ctx, output); @@ -281,23 +263,22 @@ int SDRM_SHA256_final(CryptoCoreContainer *crt, cc_u8 *output) } /* - * @fn SDRM_SHA256_hash - * @brief get hashed message from message + * @fn SDRM_SHA256_hash + * @brief get hashed message from message * - * @param crt [in]CryptoCoreContainer context - * @param msg [in]message - * @param msglen [in]byte-length of msg - * @param output [out]hashed message + * @param crt [in]CryptoCoreContainer context + * @param msg [in]message + * @param msglen [in]byte-length of msg + * @param output [out]hashed message * - * @return CRYPTO_SUCCESS if no error is occured - * \n CRYPTO_NULL_POINTER if given argument is a null pointer + * @return CRYPTO_SUCCESS if no error is occured + * \n CRYPTO_NULL_POINTER if given argument is a null pointer */ -int SDRM_SHA256_hash(CryptoCoreContainer *crt, cc_u8 *msg, cc_u32 msglen, cc_u8 *output) +int SDRM_SHA256_hash(CryptoCoreContainer *crt, cc_u8 *msg, cc_u32 msglen, + cc_u8 *output) { if ((crt == NULL) || (crt->ctx == NULL)) - { return CRYPTO_NULL_POINTER; - } SDRM_SHA256_Init(crt->ctx->sha256ctx); SDRM_SHA256_Update(crt->ctx->sha256ctx, msg, msglen); @@ -309,20 +290,18 @@ int SDRM_SHA256_hash(CryptoCoreContainer *crt, cc_u8 *msg, cc_u32 msglen, cc_u8 #ifndef _OP64_NOTSUPPORTED /* - * @fn SDRM_SHA384_init - * @brief initialize CryptoCoreContainer context + * @fn SDRM_SHA384_init + * @brief initialize CryptoCoreContainer context * - * @param crt [out]CryptoCoreContainer context + * @param crt [out]CryptoCoreContainer context * - * @return CRYPTO_SUCCESS if no error is occured - * \n CRYPTO_NULL_POINTER if given argument is a null pointer + * @return CRYPTO_SUCCESS if no error is occured + * \n CRYPTO_NULL_POINTER if given argument is a null pointer */ int SDRM_SHA384_init(CryptoCoreContainer *crt) { if ((crt == NULL) || (crt->ctx == NULL)) - { return CRYPTO_NULL_POINTER; - } SDRM_SHA384_Init(crt->ctx->sha384ctx); @@ -330,22 +309,20 @@ int SDRM_SHA384_init(CryptoCoreContainer *crt) } /* - * @fn SDRM_SHA384_update - * @brief process a message block + * @fn SDRM_SHA384_update + * @brief process a message block * - * @param crt [out]CryptoCoreContainer context - * @param msg [in]message - * @param msglen [in]byte-length of msg + * @param crt [out]CryptoCoreContainer context + * @param msg [in]message + * @param msglen [in]byte-length of msg * - * @return CRYPTO_SUCCESS if no error is occured - * \n CRYPTO_NULL_POINTER if given argument is a null pointer + * @return CRYPTO_SUCCESS if no error is occured + * \n CRYPTO_NULL_POINTER if given argument is a null pointer */ int SDRM_SHA384_update(CryptoCoreContainer *crt, cc_u8 *msg, cc_u32 msglen) { if ((crt == NULL) || (crt->ctx == NULL)) - { return CRYPTO_NULL_POINTER; - } SDRM_SHA384_Update(crt->ctx->sha384ctx, msg, msglen); @@ -353,21 +330,19 @@ int SDRM_SHA384_update(CryptoCoreContainer *crt, cc_u8 *msg, cc_u32 msglen) } /* - * @fn SDRM_SHA384_final - * @brief get hashed message + * @fn SDRM_SHA384_final + * @brief get hashed message * - * @param crt [in]CryptoCoreContainer context - * @param output [out]hashed message + * @param crt [in]CryptoCoreContainer context + * @param output [out]hashed message * - * @return CRYPTO_SUCCESS if no error is occured - * \n CRYPTO_NULL_POINTER if given argument is a null pointer + * @return CRYPTO_SUCCESS if no error is occured + * \n CRYPTO_NULL_POINTER if given argument is a null pointer */ int SDRM_SHA384_final(CryptoCoreContainer *crt, cc_u8 *output) { if ((crt == NULL) || (crt->ctx == NULL) || (output == NULL)) - { return CRYPTO_NULL_POINTER; - } SDRM_SHA384_Final(crt->ctx->sha384ctx, output); @@ -375,23 +350,22 @@ int SDRM_SHA384_final(CryptoCoreContainer *crt, cc_u8 *output) } /* - * @fn SDRM_SHA384_hash - * @brief get hashed message from message + * @fn SDRM_SHA384_hash + * @brief get hashed message from message * - * @param crt [in]CryptoCoreContainer context - * @param msg [in]message - * @param msglen [in]byte-length of msg - * @param output [out]hashed message + * @param crt [in]CryptoCoreContainer context + * @param msg [in]message + * @param msglen [in]byte-length of msg + * @param output [out]hashed message * - * @return CRYPTO_SUCCESS if no error is occured - * \n CRYPTO_NULL_POINTER if given argument is a null pointer + * @return CRYPTO_SUCCESS if no error is occured + * \n CRYPTO_NULL_POINTER if given argument is a null pointer */ -int SDRM_SHA384_hash(CryptoCoreContainer *crt, cc_u8 *msg, cc_u32 msglen, cc_u8 *output) +int SDRM_SHA384_hash(CryptoCoreContainer *crt, cc_u8 *msg, cc_u32 msglen, + cc_u8 *output) { if ((crt == NULL) || (crt->ctx == NULL)) - { return CRYPTO_NULL_POINTER; - } SDRM_SHA384_Init(crt->ctx->sha384ctx); SDRM_SHA384_Update(crt->ctx->sha384ctx, msg, msglen); @@ -401,20 +375,18 @@ int SDRM_SHA384_hash(CryptoCoreContainer *crt, cc_u8 *msg, cc_u32 msglen, cc_u8 } /* - * @fn SDRM_SHA512_init - * @brief initialize CryptoCoreContainer context + * @fn SDRM_SHA512_init + * @brief initialize CryptoCoreContainer context * - * @param crt [out]CryptoCoreContainer context + * @param crt [out]CryptoCoreContainer context * - * @return CRYPTO_SUCCESS if no error is occured - * \n CRYPTO_NULL_POINTER if given argument is a null pointer + * @return CRYPTO_SUCCESS if no error is occured + * \n CRYPTO_NULL_POINTER if given argument is a null pointer */ int SDRM_SHA512_init(CryptoCoreContainer *crt) { if ((crt == NULL) || (crt->ctx == NULL)) - { return CRYPTO_NULL_POINTER; - } SDRM_SHA512_Init(crt->ctx->sha512ctx); @@ -422,22 +394,20 @@ int SDRM_SHA512_init(CryptoCoreContainer *crt) } /* - * @fn SDRM_SHA512_update - * @brief process a message block + * @fn SDRM_SHA512_update + * @brief process a message block * - * @param crt [out]CryptoCoreContainer context - * @param msg [in]message - * @param msglen [in]byte-length of msg + * @param crt [out]CryptoCoreContainer context + * @param msg [in]message + * @param msglen [in]byte-length of msg * - * @return CRYPTO_SUCCESS if no error is occured - * \n CRYPTO_NULL_POINTER if given argument is a null pointer + * @return CRYPTO_SUCCESS if no error is occured + * \n CRYPTO_NULL_POINTER if given argument is a null pointer */ int SDRM_SHA512_update(CryptoCoreContainer *crt, cc_u8 *msg, cc_u32 msglen) { if ((crt == NULL) || (crt->ctx == NULL)) - { return CRYPTO_NULL_POINTER; - } SDRM_SHA512_Update(crt->ctx->sha512ctx, msg, msglen); @@ -445,21 +415,19 @@ int SDRM_SHA512_update(CryptoCoreContainer *crt, cc_u8 *msg, cc_u32 msglen) } /* - * @fn SDRM_SHA512_final - * @brief get hashed message + * @fn SDRM_SHA512_final + * @brief get hashed message * - * @param crt [in]CryptoCoreContainer context - * @param output [out]hashed message + * @param crt [in]CryptoCoreContainer context + * @param output [out]hashed message * - * @return CRYPTO_SUCCESS if no error is occured - * \n CRYPTO_NULL_POINTER if given argument is a null pointer + * @return CRYPTO_SUCCESS if no error is occured + * \n CRYPTO_NULL_POINTER if given argument is a null pointer */ int SDRM_SHA512_final(CryptoCoreContainer *crt, cc_u8 *output) { if ((crt == NULL) || (crt->ctx == NULL) || (output == NULL)) - { return CRYPTO_NULL_POINTER; - } SDRM_SHA512_Final(crt->ctx->sha512ctx, output); @@ -467,23 +435,22 @@ int SDRM_SHA512_final(CryptoCoreContainer *crt, cc_u8 *output) } /* - * @fn SDRM_SHA512_hash - * @brief get hashed message from message + * @fn SDRM_SHA512_hash + * @brief get hashed message from message * - * @param crt [in]CryptoCoreContainer context - * @param msg [in]message - * @param msglen [in]byte-length of msg - * @param output [out]hashed message + * @param crt [in]CryptoCoreContainer context + * @param msg [in]message + * @param msglen [in]byte-length of msg + * @param output [out]hashed message * - * @return CRYPTO_SUCCESS if no error is occured - * \n CRYPTO_NULL_POINTER if given argument is a null pointer + * @return CRYPTO_SUCCESS if no error is occured + * \n CRYPTO_NULL_POINTER if given argument is a null pointer */ -int SDRM_SHA512_hash(CryptoCoreContainer *crt, cc_u8 *msg, cc_u32 msglen, cc_u8 *output) +int SDRM_SHA512_hash(CryptoCoreContainer *crt, cc_u8 *msg, cc_u32 msglen, + cc_u8 *output) { if ((crt == NULL) || (crt->ctx == NULL)) - { return CRYPTO_NULL_POINTER; - } SDRM_SHA512_Init(crt->ctx->sha512ctx); SDRM_SHA512_Update(crt->ctx->sha512ctx, msg, msglen); @@ -495,20 +462,18 @@ int SDRM_SHA512_hash(CryptoCoreContainer *crt, cc_u8 *msg, cc_u32 msglen, cc_u8 #endif //_OP64_NOTSUPPORTED /* - * @fn SDRM_MD5_init - * @brief initialize CryptoCoreContainer context + * @fn SDRM_MD5_init + * @brief initialize CryptoCoreContainer context * - * @param crt [out]CryptoCoreContainer context + * @param crt [out]CryptoCoreContainer context * - * @return CRYPTO_SUCCESS if no error is occured - * \n CRYPTO_NULL_POINTER if given argument is a null pointer + * @return CRYPTO_SUCCESS if no error is occured + * \n CRYPTO_NULL_POINTER if given argument is a null pointer */ int SDRM_MD5_init(CryptoCoreContainer *crt) { if ((crt == NULL) || (crt->ctx == NULL)) - { return CRYPTO_NULL_POINTER; - } SDRM_MD5_Init(crt->ctx->md5ctx); @@ -516,22 +481,20 @@ int SDRM_MD5_init(CryptoCoreContainer *crt) } /* - * @fn SDRM_MD5_update - * @brief process a message block + * @fn SDRM_MD5_update + * @brief process a message block * - * @param crt [out]CryptoCoreContainer context - * @param msg [in]message - * @param msglen [in]byte-length of msg + * @param crt [out]CryptoCoreContainer context + * @param msg [in]message + * @param msglen [in]byte-length of msg * - * @return CRYPTO_SUCCESS if no error is occured - * \n CRYPTO_NULL_POINTER if given argument is a null pointer + * @return CRYPTO_SUCCESS if no error is occured + * \n CRYPTO_NULL_POINTER if given argument is a null pointer */ int SDRM_MD5_update(CryptoCoreContainer *crt, cc_u8 *msg, cc_u32 msglen) { if ((crt == NULL) || (crt->ctx == NULL)) - { return CRYPTO_NULL_POINTER; - } SDRM_MD5_Update(crt->ctx->md5ctx, msg, msglen); @@ -539,21 +502,19 @@ int SDRM_MD5_update(CryptoCoreContainer *crt, cc_u8 *msg, cc_u32 msglen) } /* - * @fn SDRM_MD5_final - * @brief get hashed message + * @fn SDRM_MD5_final + * @brief get hashed message * - * @param crt [in]CryptoCoreContainer context - * @param output [out]hashed message + * @param crt [in]CryptoCoreContainer context + * @param output [out]hashed message * - * @return CRYPTO_SUCCESS if no error is occured - * \n CRYPTO_NULL_POINTER if given argument is a null pointer + * @return CRYPTO_SUCCESS if no error is occured + * \n CRYPTO_NULL_POINTER if given argument is a null pointer */ int SDRM_MD5_final(CryptoCoreContainer *crt, cc_u8 *output) { if ((crt == NULL) || (crt->ctx == NULL) || (output == NULL)) - { return CRYPTO_NULL_POINTER; - } SDRM_MD5_Final(crt->ctx->md5ctx, output); @@ -561,23 +522,22 @@ int SDRM_MD5_final(CryptoCoreContainer *crt, cc_u8 *output) } /* - * @fn SDRM_MD5_hash - * @brief get hashed message from message + * @fn SDRM_MD5_hash + * @brief get hashed message from message * - * @param crt [in]CryptoCoreContainer context - * @param msg [in]message - * @param msglen [in]byte-length of msg - * @param output [out]hashed message + * @param crt [in]CryptoCoreContainer context + * @param msg [in]message + * @param msglen [in]byte-length of msg + * @param output [out]hashed message * - * @return CRYPTO_SUCCESS if no error is occured - * \n CRYPTO_NULL_POINTER if given argument is a null pointer + * @return CRYPTO_SUCCESS if no error is occured + * \n CRYPTO_NULL_POINTER if given argument is a null pointer */ -int SDRM_MD5_hash(CryptoCoreContainer *crt, cc_u8 *msg, cc_u32 msglen, cc_u8 *output) +int SDRM_MD5_hash(CryptoCoreContainer *crt, cc_u8 *msg, cc_u32 msglen, + cc_u8 *output) { if ((crt == NULL) || (crt->ctx == NULL)) - { return CRYPTO_NULL_POINTER; - } SDRM_MD5_Init(crt->ctx->md5ctx); SDRM_MD5_Update(crt->ctx->md5ctx, msg, msglen); @@ -586,4 +546,5 @@ int SDRM_MD5_hash(CryptoCoreContainer *crt, cc_u8 *msg, cc_u32 msglen, cc_u8 *ou return CRYPTO_SUCCESS; } -/***************************** End of File *****************************/ \ No newline at end of file +/***************************** End of File *****************************/ + diff --git a/ssflib/dep/cryptocore/source/base/cc_md5.c b/ssflib/dep/cryptocore/source/base/cc_md5.c index 89ad0c0..cbb667f 100644 --- a/ssflib/dep/cryptocore/source/base/cc_md5.c +++ b/ssflib/dep/cryptocore/source/base/cc_md5.c @@ -30,9 +30,9 @@ static cc_u8 S[4][4] = { {6, 10, 15, 21} }; -static void SDRM_MD5Transform(cc_u32 [4], const unsigned char*); -static void SDRM_Encode (unsigned char *, cc_u32 *, cc_u32); -static void SDRM_Decode (cc_u32 *, const unsigned char *, cc_u32); +static void SDRM_MD5Transform(cc_u32 [4], const unsigned char *); +static void SDRM_Encode(unsigned char *, cc_u32 *, cc_u32); +static void SDRM_Decode(cc_u32 *, const unsigned char *, cc_u32); static unsigned char PADDING[64] = {0x80, 0,}; @@ -56,26 +56,26 @@ static unsigned char PADDING[64] = {0x80, 0,}; /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4. Rotation is separate from addition to prevent recomputation. */ -#define FF(a, b, c, d, x, s, ac) { \ - (a) += F ((b), (c), (d)) + (x) + (cc_u32)(ac); \ - (a) = ROTATE_LEFT ((a), (s)); \ - (a) += (b); \ -} -#define GG(a, b, c, d, x, s, ac) { \ - (a) += G ((b), (c), (d)) + (x) + (cc_u32)(ac); \ - (a) = ROTATE_LEFT ((a), (s)); \ - (a) += (b); \ -} -#define HH(a, b, c, d, x, s, ac) { \ - (a) += H ((b), (c), (d)) + (x) + (cc_u32)(ac); \ - (a) = ROTATE_LEFT ((a), (s)); \ - (a) += (b); \ -} -#define II(a, b, c, d, x, s, ac) { \ - (a) += I ((b), (c), (d)) + (x) + (cc_u32)(ac); \ - (a) = ROTATE_LEFT ((a), (s)); \ - (a) += (b); \ -} +#define FF(a, b, c, d, x, s, ac) { \ + (a) += F((b), (c), (d)) + (x) + (cc_u32)(ac); \ + (a) = ROTATE_LEFT((a), (s)); \ + (a) += (b); \ + } +#define GG(a, b, c, d, x, s, ac) { \ + (a) += G((b), (c), (d)) + (x) + (cc_u32)(ac); \ + (a) = ROTATE_LEFT((a), (s)); \ + (a) += (b); \ + } +#define HH(a, b, c, d, x, s, ac) { \ + (a) += H((b), (c), (d)) + (x) + (cc_u32)(ac); \ + (a) = ROTATE_LEFT((a), (s)); \ + (a) += (b); \ + } +#define II(a, b, c, d, x, s, ac) { \ + (a) += I((b), (c), (d)) + (x) + (cc_u32)(ac); \ + (a) = ROTATE_LEFT((a), (s)); \ + (a) += (b); \ + } /* MD5 initialization. Begins an MD5 operation, writing a new context. */ @@ -95,7 +95,7 @@ void SDRM_MD5_Init(SDRM_MD5Context *ctx) operation, processing another message block, and updating the context. */ -void SDRM_MD5_Update(SDRM_MD5Context *ctx, cc_u8* input, cc_u32 inputLen) +void SDRM_MD5_Update(SDRM_MD5Context *ctx, cc_u8 *input, cc_u32 inputLen) { cc_u32 i, idx, partLen; @@ -104,40 +104,32 @@ void SDRM_MD5_Update(SDRM_MD5Context *ctx, cc_u8* input, cc_u32 inputLen) /* Update number of bits */ if ((ctx->count[0] += ((cc_u32)inputLen << 3)) < ((cc_u32)inputLen << 3)) - { ctx->count[1]++; - } ctx->count[1] += ((cc_u32)inputLen >> 29); partLen = 64 - idx; // Transform as many times as possible. - if (inputLen >= partLen) - { + if (inputLen >= partLen) { memcpy(&ctx->buffer[idx], input, partLen); SDRM_MD5Transform(ctx->state, ctx->buffer); for (i = partLen; i + 63 < inputLen; i += 64) - { SDRM_MD5Transform(ctx->state, &input[i]); - } idx = 0; - } - else - { + } else i = 0; - } /* Buffer remaining input */ - memcpy(&ctx->buffer[idx], &input[i], inputLen-i); + memcpy(&ctx->buffer[idx], &input[i], inputLen - i); } /* MD5 finalization. Ends an MD5 message-digest operation, writing the the message digest and zeroizing the context. */ -void SDRM_MD5_Final(SDRM_MD5Context *ctx, cc_u8* digest) +void SDRM_MD5_Final(SDRM_MD5Context *ctx, cc_u8 *digest) { unsigned char bits[8]; cc_u32 idx, padLen; @@ -148,13 +140,13 @@ void SDRM_MD5_Final(SDRM_MD5Context *ctx, cc_u8* digest) // Pad out to 56 mod 64. idx = (cc_u32)((ctx->count[0] >> 3) & 0x3f); padLen = (idx < 56) ? (56 - idx) : (120 - idx); - SDRM_MD5_Update (ctx, PADDING, padLen); + SDRM_MD5_Update(ctx, PADDING, padLen); /* Append length (before padding) */ - SDRM_MD5_Update (ctx, bits, 8); + SDRM_MD5_Update(ctx, bits, 8); /* Store state in digest */ - SDRM_Encode (digest, ctx->state, 16); + SDRM_Encode(digest, ctx->state, 16); // Zeroize sensitive information. memset(ctx, 0, sizeof(*ctx)); @@ -162,83 +154,83 @@ void SDRM_MD5_Final(SDRM_MD5Context *ctx, cc_u8* digest) /* MD5 basic transformation. Transforms state based on block. */ -static void SDRM_MD5Transform (cc_u32 state[4], const unsigned char* block) +static void SDRM_MD5Transform(cc_u32 state[4], const unsigned char *block) { cc_u32 a = state[0], b = state[1], c = state[2], d = state[3], x[16]; - SDRM_Decode (x, block, 64); + SDRM_Decode(x, block, 64); /* Round 1 */ - FF (a, b, c, d, x[ 0], S[0][0], 0xd76aa478); /* 1 */ - FF (d, a, b, c, x[ 1], S[0][1], 0xe8c7b756); /* 2 */ - FF (c, d, a, b, x[ 2], S[0][2], 0x242070db); /* 3 */ - FF (b, c, d, a, x[ 3], S[0][3], 0xc1bdceee); /* 4 */ - FF (a, b, c, d, x[ 4], S[0][0], 0xf57c0faf); /* 5 */ - FF (d, a, b, c, x[ 5], S[0][1], 0x4787c62a); /* 6 */ - FF (c, d, a, b, x[ 6], S[0][2], 0xa8304613); /* 7 */ - FF (b, c, d, a, x[ 7], S[0][3], 0xfd469501); /* 8 */ - FF (a, b, c, d, x[ 8], S[0][0], 0x698098d8); /* 9 */ - FF (d, a, b, c, x[ 9], S[0][1], 0x8b44f7af); /* 10 */ - FF (c, d, a, b, x[10], S[0][2], 0xffff5bb1); /* 11 */ - FF (b, c, d, a, x[11], S[0][3], 0x895cd7be); /* 12 */ - FF (a, b, c, d, x[12], S[0][0], 0x6b901122); /* 13 */ - FF (d, a, b, c, x[13], S[0][1], 0xfd987193); /* 14 */ - FF (c, d, a, b, x[14], S[0][2], 0xa679438e); /* 15 */ - FF (b, c, d, a, x[15], S[0][3], 0x49b40821); /* 16 */ + FF(a, b, c, d, x[0], S[0][0], 0xd76aa478); /* 1 */ + FF(d, a, b, c, x[1], S[0][1], 0xe8c7b756); /* 2 */ + FF(c, d, a, b, x[2], S[0][2], 0x242070db); /* 3 */ + FF(b, c, d, a, x[3], S[0][3], 0xc1bdceee); /* 4 */ + FF(a, b, c, d, x[4], S[0][0], 0xf57c0faf); /* 5 */ + FF(d, a, b, c, x[5], S[0][1], 0x4787c62a); /* 6 */ + FF(c, d, a, b, x[6], S[0][2], 0xa8304613); /* 7 */ + FF(b, c, d, a, x[7], S[0][3], 0xfd469501); /* 8 */ + FF(a, b, c, d, x[8], S[0][0], 0x698098d8); /* 9 */ + FF(d, a, b, c, x[9], S[0][1], 0x8b44f7af); /* 10 */ + FF(c, d, a, b, x[10], S[0][2], 0xffff5bb1); /* 11 */ + FF(b, c, d, a, x[11], S[0][3], 0x895cd7be); /* 12 */ + FF(a, b, c, d, x[12], S[0][0], 0x6b901122); /* 13 */ + FF(d, a, b, c, x[13], S[0][1], 0xfd987193); /* 14 */ + FF(c, d, a, b, x[14], S[0][2], 0xa679438e); /* 15 */ + FF(b, c, d, a, x[15], S[0][3], 0x49b40821); /* 16 */ /* Round 2 */ - GG (a, b, c, d, x[ 1], S[1][0], 0xf61e2562); /* 17 */ - GG (d, a, b, c, x[ 6], S[1][1], 0xc040b340); /* 18 */ - GG (c, d, a, b, x[11], S[1][2], 0x265e5a51); /* 19 */ - GG (b, c, d, a, x[ 0], S[1][3], 0xe9b6c7aa); /* 20 */ - GG (a, b, c, d, x[ 5], S[1][0], 0xd62f105d); /* 21 */ - GG (d, a, b, c, x[10], S[1][1], 0x02441453); /* 22 */ - GG (c, d, a, b, x[15], S[1][2], 0xd8a1e681); /* 23 */ - GG (b, c, d, a, x[ 4], S[1][3], 0xe7d3fbc8); /* 24 */ - GG (a, b, c, d, x[ 9], S[1][0], 0x21e1cde6); /* 25 */ - GG (d, a, b, c, x[14], S[1][1], 0xc33707d6); /* 26 */ - GG (c, d, a, b, x[ 3], S[1][2], 0xf4d50d87); /* 27 */ - GG (b, c, d, a, x[ 8], S[1][3], 0x455a14ed); /* 28 */ - GG (a, b, c, d, x[13], S[1][0], 0xa9e3e905); /* 29 */ - GG (d, a, b, c, x[ 2], S[1][1], 0xfcefa3f8); /* 30 */ - GG (c, d, a, b, x[ 7], S[1][2], 0x676f02d9); /* 31 */ - GG (b, c, d, a, x[12], S[1][3], 0x8d2a4c8a); /* 32 */ + GG(a, b, c, d, x[1], S[1][0], 0xf61e2562); /* 17 */ + GG(d, a, b, c, x[6], S[1][1], 0xc040b340); /* 18 */ + GG(c, d, a, b, x[11], S[1][2], 0x265e5a51); /* 19 */ + GG(b, c, d, a, x[0], S[1][3], 0xe9b6c7aa); /* 20 */ + GG(a, b, c, d, x[5], S[1][0], 0xd62f105d); /* 21 */ + GG(d, a, b, c, x[10], S[1][1], 0x02441453); /* 22 */ + GG(c, d, a, b, x[15], S[1][2], 0xd8a1e681); /* 23 */ + GG(b, c, d, a, x[4], S[1][3], 0xe7d3fbc8); /* 24 */ + GG(a, b, c, d, x[9], S[1][0], 0x21e1cde6); /* 25 */ + GG(d, a, b, c, x[14], S[1][1], 0xc33707d6); /* 26 */ + GG(c, d, a, b, x[3], S[1][2], 0xf4d50d87); /* 27 */ + GG(b, c, d, a, x[8], S[1][3], 0x455a14ed); /* 28 */ + GG(a, b, c, d, x[13], S[1][0], 0xa9e3e905); /* 29 */ + GG(d, a, b, c, x[2], S[1][1], 0xfcefa3f8); /* 30 */ + GG(c, d, a, b, x[7], S[1][2], 0x676f02d9); /* 31 */ + GG(b, c, d, a, x[12], S[1][3], 0x8d2a4c8a); /* 32 */ /* Round 3 */ - HH (a, b, c, d, x[ 5], S[2][0], 0xfffa3942); /* 33 */ - HH (d, a, b, c, x[ 8], S[2][1], 0x8771f681); /* 34 */ - HH (c, d, a, b, x[11], S[2][2], 0x6d9d6122); /* 35 */ - HH (b, c, d, a, x[14], S[2][3], 0xfde5380c); /* 36 */ - HH (a, b, c, d, x[ 1], S[2][0], 0xa4beea44); /* 37 */ - HH (d, a, b, c, x[ 4], S[2][1], 0x4bdecfa9); /* 38 */ - HH (c, d, a, b, x[ 7], S[2][2], 0xf6bb4b60); /* 39 */ - HH (b, c, d, a, x[10], S[2][3], 0xbebfbc70); /* 40 */ - HH (a, b, c, d, x[13], S[2][0], 0x289b7ec6); /* 41 */ - HH (d, a, b, c, x[ 0], S[2][1], 0xeaa127fa); /* 42 */ - HH (c, d, a, b, x[ 3], S[2][2], 0xd4ef3085); /* 43 */ - HH (b, c, d, a, x[ 6], S[2][3], 0x04881d05); /* 44 */ - HH (a, b, c, d, x[ 9], S[2][0], 0xd9d4d039); /* 45 */ - HH (d, a, b, c, x[12], S[2][1], 0xe6db99e5); /* 46 */ - HH (c, d, a, b, x[15], S[2][2], 0x1fa27cf8); /* 47 */ - HH (b, c, d, a, x[ 2], S[2][3], 0xc4ac5665); /* 48 */ + HH(a, b, c, d, x[5], S[2][0], 0xfffa3942); /* 33 */ + HH(d, a, b, c, x[8], S[2][1], 0x8771f681); /* 34 */ + HH(c, d, a, b, x[11], S[2][2], 0x6d9d6122); /* 35 */ + HH(b, c, d, a, x[14], S[2][3], 0xfde5380c); /* 36 */ + HH(a, b, c, d, x[1], S[2][0], 0xa4beea44); /* 37 */ + HH(d, a, b, c, x[4], S[2][1], 0x4bdecfa9); /* 38 */ + HH(c, d, a, b, x[7], S[2][2], 0xf6bb4b60); /* 39 */ + HH(b, c, d, a, x[10], S[2][3], 0xbebfbc70); /* 40 */ + HH(a, b, c, d, x[13], S[2][0], 0x289b7ec6); /* 41 */ + HH(d, a, b, c, x[0], S[2][1], 0xeaa127fa); /* 42 */ + HH(c, d, a, b, x[3], S[2][2], 0xd4ef3085); /* 43 */ + HH(b, c, d, a, x[6], S[2][3], 0x04881d05); /* 44 */ + HH(a, b, c, d, x[9], S[2][0], 0xd9d4d039); /* 45 */ + HH(d, a, b, c, x[12], S[2][1], 0xe6db99e5); /* 46 */ + HH(c, d, a, b, x[15], S[2][2], 0x1fa27cf8); /* 47 */ + HH(b, c, d, a, x[2], S[2][3], 0xc4ac5665); /* 48 */ /* Round 4 */ - II (a, b, c, d, x[ 0], S[3][0], 0xf4292244); /* 49 */ - II (d, a, b, c, x[ 7], S[3][1], 0x432aff97); /* 50 */ - II (c, d, a, b, x[14], S[3][2], 0xab9423a7); /* 51 */ - II (b, c, d, a, x[ 5], S[3][3], 0xfc93a039); /* 52 */ - II (a, b, c, d, x[12], S[3][0], 0x655b59c3); /* 53 */ - II (d, a, b, c, x[ 3], S[3][1], 0x8f0ccc92); /* 54 */ - II (c, d, a, b, x[10], S[3][2], 0xffeff47d); /* 55 */ - II (b, c, d, a, x[ 1], S[3][3], 0x85845dd1); /* 56 */ - II (a, b, c, d, x[ 8], S[3][0], 0x6fa87e4f); /* 57 */ - II (d, a, b, c, x[15], S[3][1], 0xfe2ce6e0); /* 58 */ - II (c, d, a, b, x[ 6], S[3][2], 0xa3014314); /* 59 */ - II (b, c, d, a, x[13], S[3][3], 0x4e0811a1); /* 60 */ - II (a, b, c, d, x[ 4], S[3][0], 0xf7537e82); /* 61 */ - II (d, a, b, c, x[11], S[3][1], 0xbd3af235); /* 62 */ - II (c, d, a, b, x[ 2], S[3][2], 0x2ad7d2bb); /* 63 */ - II (b, c, d, a, x[ 9], S[3][3], 0xeb86d391); /* 64 */ + II(a, b, c, d, x[0], S[3][0], 0xf4292244); /* 49 */ + II(d, a, b, c, x[7], S[3][1], 0x432aff97); /* 50 */ + II(c, d, a, b, x[14], S[3][2], 0xab9423a7); /* 51 */ + II(b, c, d, a, x[5], S[3][3], 0xfc93a039); /* 52 */ + II(a, b, c, d, x[12], S[3][0], 0x655b59c3); /* 53 */ + II(d, a, b, c, x[3], S[3][1], 0x8f0ccc92); /* 54 */ + II(c, d, a, b, x[10], S[3][2], 0xffeff47d); /* 55 */ + II(b, c, d, a, x[1], S[3][3], 0x85845dd1); /* 56 */ + II(a, b, c, d, x[8], S[3][0], 0x6fa87e4f); /* 57 */ + II(d, a, b, c, x[15], S[3][1], 0xfe2ce6e0); /* 58 */ + II(c, d, a, b, x[6], S[3][2], 0xa3014314); /* 59 */ + II(b, c, d, a, x[13], S[3][3], 0x4e0811a1); /* 60 */ + II(a, b, c, d, x[4], S[3][0], 0xf7537e82); /* 61 */ + II(d, a, b, c, x[11], S[3][1], 0xbd3af235); /* 62 */ + II(c, d, a, b, x[2], S[3][2], 0x2ad7d2bb); /* 63 */ + II(b, c, d, a, x[9], S[3][3], 0xeb86d391); /* 64 */ state[0] += a; state[1] += b; @@ -246,39 +238,37 @@ static void SDRM_MD5Transform (cc_u32 state[4], const unsigned char* block) state[3] += d; //Zeroize sensitive information. - memset (x, 0, sizeof (x)); + memset(x, 0, sizeof(x)); } /* Encodes input (cc_u4) into output (unsigned char). Assumes len is a multiple of 4. */ -static void SDRM_Encode (unsigned char *output, cc_u32 *input, cc_u32 len) +static void SDRM_Encode(unsigned char *output, cc_u32 *input, cc_u32 len) { cc_u32 i, j; - for (i = 0, j = 0; j < len; i++, j += 4) - { - output[j ] = (unsigned char)((input[i] ) & 0xff); - output[j+1] = (unsigned char)((input[i] >> 8) & 0xff); - output[j+2] = (unsigned char)((input[i] >> 16) & 0xff); - output[j+3] = (unsigned char)((input[i] >> 24) & 0xff); + for (i = 0, j = 0; j < len; i++, j += 4) { + output[j] = (unsigned char)((input[i]) & 0xff); + output[j + 1] = (unsigned char)((input[i] >> 8) & 0xff); + output[j + 2] = (unsigned char)((input[i] >> 16) & 0xff); + output[j + 3] = (unsigned char)((input[i] >> 24) & 0xff); } } /* Decodes input (unsigned char) into output (UINT4). Assumes len is a multiple of 4. */ -static void SDRM_Decode (cc_u32 *output, const unsigned char *input, cc_u32 len) +static void SDRM_Decode(cc_u32 *output, const unsigned char *input, cc_u32 len) { cc_u32 i, j; - for (i = 0, j = 0; j < len; i++, j += 4) - { + for (i = 0, j = 0; j < len; i++, j += 4) { output[i] = ( - (((cc_u32)input[j] ) ) | - (((cc_u32)input[j + 1]) << 8) | - (((cc_u32)input[j + 2]) << 16) | - (((cc_u32)input[j + 3]) << 24)); + (((cc_u32)input[j])) | + (((cc_u32)input[j + 1]) << 8) | + (((cc_u32)input[j + 2]) << 16) | + (((cc_u32)input[j + 3]) << 24)); } } diff --git a/ssflib/dep/cryptocore/source/base/cc_moo.c b/ssflib/dep/cryptocore/source/base/cc_moo.c index b8a202d..0ab4c13 100644 --- a/ssflib/dep/cryptocore/source/base/cc_moo.c +++ b/ssflib/dep/cryptocore/source/base/cc_moo.c @@ -30,210 +30,215 @@ // Functions //////////////////////////////////////////////////////////////////////////// /* - * @fn SDRM_ECB_Enc - * @brief Encrypt a block with ECB mode + * @fn SDRM_ECB_Enc + * @brief Encrypt a block with ECB mode * - * @param Algorithm [in]algorithm - * @param out [out]cipher text block - * @param in [in]plain text block - * @param key [in]user key + * @param Algorithm [in]algorithm + * @param out [out]cipher text block + * @param in [in]plain text block + * @param key [in]user key * - * @return CRYPTO_SUCCESS if no error is occured - * \n CRYPTO_INVALID_ARGUMENT if parameter is invalid + * @return CRYPTO_SUCCESS if no error is occured + * \n CRYPTO_INVALID_ARGUMENT if parameter is invalid */ int SDRM_ECB_Enc(int Algorithm, cc_u8 *out, cc_u8 *in, cc_u8 *key) { - switch(Algorithm) - { - case ID_AES128 : - SDRM_rijndaelEncrypt((cc_u32*)(void*)key, 10, in, out); - return CRYPTO_SUCCESS; - case ID_AES192 : - SDRM_rijndaelEncrypt((cc_u32*)(void*)key, 12, in, out); - return CRYPTO_SUCCESS; - case ID_AES256 : - SDRM_rijndaelEncrypt((cc_u32*)(void*)key, 14, in, out); - return CRYPTO_SUCCESS; - case ID_DES : - return SDRM_DES_Encryption((cc_u32(*)[2])(void*)key, in, out); - case ID_TDES : - return SDRM_TDES_Encryption((cc_u32(*)[2])(void*)key, in, out); - default : - break; + switch (Algorithm) { + case ID_AES128: + SDRM_rijndaelEncrypt((cc_u32 *)(void *)key, 10, in, out); + return CRYPTO_SUCCESS; + + case ID_AES192: + SDRM_rijndaelEncrypt((cc_u32 *)(void *)key, 12, in, out); + return CRYPTO_SUCCESS; + + case ID_AES256: + SDRM_rijndaelEncrypt((cc_u32 *)(void *)key, 14, in, out); + return CRYPTO_SUCCESS; + + case ID_DES: + return SDRM_DES_Encryption((cc_u32(*)[2])(void *)key, in, out); + + case ID_TDES: + return SDRM_TDES_Encryption((cc_u32(*)[2])(void *)key, in, out); + + default: + break; } return CRYPTO_INVALID_ARGUMENT; } /* - * @fn SDRM_ECB_Dec - * @brief Decrypt a block with ECB mode + * @fn SDRM_ECB_Dec + * @brief Decrypt a block with ECB mode * - * @param Algorithm [in]algorithm - * @param out [out]plain text block - * @param in [in]cipher text block - * @param key [in]user key + * @param Algorithm [in]algorithm + * @param out [out]plain text block + * @param in [in]cipher text block + * @param key [in]user key * - * @return CRYPTO_SUCCESS if no error is occured - * \n CRYPTO_INVALID_ARGUMENT if parameter is invalid + * @return CRYPTO_SUCCESS if no error is occured + * \n CRYPTO_INVALID_ARGUMENT if parameter is invalid */ int SDRM_ECB_Dec(int Algorithm, cc_u8 *out, cc_u8 *in, cc_u8 *key) { - switch(Algorithm) - { - case ID_AES128 : - SDRM_rijndaelDecrypt((const cc_u32*)(void*)key, 10, in, out); - return CRYPTO_SUCCESS; - case ID_AES192 : - SDRM_rijndaelDecrypt((const cc_u32*)(void*)key, 12, in, out); - return CRYPTO_SUCCESS; - case ID_AES256 : - SDRM_rijndaelDecrypt((const cc_u32*)(void*)key, 14, in, out); - return CRYPTO_SUCCESS; - case ID_DES : - return SDRM_DES_Encryption((cc_u32(*)[2])(void*)key, in, out); - case ID_TDES : - return SDRM_TDES_Encryption((cc_u32(*)[2])(void*)key, in, out); - default : - break; + switch (Algorithm) { + case ID_AES128: + SDRM_rijndaelDecrypt((const cc_u32 *)(void *)key, 10, in, out); + return CRYPTO_SUCCESS; + + case ID_AES192: + SDRM_rijndaelDecrypt((const cc_u32 *)(void *)key, 12, in, out); + return CRYPTO_SUCCESS; + + case ID_AES256: + SDRM_rijndaelDecrypt((const cc_u32 *)(void *)key, 14, in, out); + return CRYPTO_SUCCESS; + + case ID_DES: + return SDRM_DES_Encryption((cc_u32(*)[2])(void *)key, in, out); + + case ID_TDES: + return SDRM_TDES_Encryption((cc_u32(*)[2])(void *)key, in, out); + + default: + break; } return CRYPTO_INVALID_ARGUMENT; } /* - * @fn SDRM_CBC_Enc - * @brief Encrypt a block with CBC mode + * @fn SDRM_CBC_Enc + * @brief Encrypt a block with CBC mode * - * @param Algorithm [in]algorithm - * @param out [out]cipher text block - * @param in [in]plain text block - * @param key [in]user key - * @param IV [in]initial vector + * @param Algorithm [in]algorithm + * @param out [out]cipher text block + * @param in [in]plain text block + * @param key [in]user key + * @param IV [in]initial vector * - * @return CRYPTO_SUCCESS if no error is occured - * \n CRYPTO_INVALID_ARGUMENT if parameter is invalid + * @return CRYPTO_SUCCESS if no error is occured + * \n CRYPTO_INVALID_ARGUMENT if parameter is invalid */ int SDRM_CBC_Enc(int Algorithm, cc_u8 *out, cc_u8 *in, cc_u8 *key, cc_u8 *IV) { int i; - switch(Algorithm) - { - case ID_AES128 : - for (i = 0; i < 16; i++) - { - IV[i] ^= in[i]; - } + switch (Algorithm) { + case ID_AES128: + for (i = 0; i < 16; i++) + IV[i] ^= in[i]; + + SDRM_rijndaelEncrypt((const cc_u32 *)(void *)key, 10, IV, out); - SDRM_rijndaelEncrypt((const cc_u32*)(void*)key, 10, IV, out); + memcpy(IV, out, 16); - memcpy(IV, out, 16); + break; - break; - case ID_AES192 : - for (i = 0; i < 16; i++) - { - IV[i] ^= in[i]; - } + case ID_AES192: + for (i = 0; i < 16; i++) + IV[i] ^= in[i]; - SDRM_rijndaelEncrypt((const cc_u32*)(void*)key, 12, IV, out); + SDRM_rijndaelEncrypt((const cc_u32 *)(void *)key, 12, IV, out); - memcpy(IV, out, 16); + memcpy(IV, out, 16); - break; - case ID_AES256 : - for (i = 0; i < 16; i++) - { - IV[i] ^= in[i]; - } + break; - SDRM_rijndaelEncrypt((const cc_u32*)(void*)key, 14, IV, out); + case ID_AES256: + for (i = 0; i < 16; i++) + IV[i] ^= in[i]; - memcpy(IV, out, 16); + SDRM_rijndaelEncrypt((const cc_u32 *)(void *)key, 14, IV, out); - break; - case ID_DES : - for (i = 0; i < 8; i++) - { - IV[i] ^= in[i]; - } + memcpy(IV, out, 16); - SDRM_DES_Encryption((cc_u32(*)[2])(void*)key, IV, out); + break; - memcpy(IV, out, 8); + case ID_DES: + for (i = 0; i < 8; i++) + IV[i] ^= in[i]; - break; - case ID_TDES : - for (i = 0; i < 8; i++) - { - IV[i] ^= in[i]; - } + SDRM_DES_Encryption((cc_u32(*)[2])(void *)key, IV, out); - SDRM_TDES_Encryption((cc_u32(*)[2])(void*)key, IV, out); + memcpy(IV, out, 8); - memcpy(IV, out, 8); + break; - break; - default : - return CRYPTO_INVALID_ARGUMENT; + case ID_TDES: + for (i = 0; i < 8; i++) + IV[i] ^= in[i]; + + SDRM_TDES_Encryption((cc_u32(*)[2])(void *)key, IV, out); + + memcpy(IV, out, 8); + + break; + + default: + return CRYPTO_INVALID_ARGUMENT; } return CRYPTO_SUCCESS; } /* - * @fn SDRM_CBC_Dec - * @brief Decrypt a block with CBC mode + * @fn SDRM_CBC_Dec + * @brief Decrypt a block with CBC mode * - * @param Algorithm [in]algorithm - * @param out [out]plain text block - * @param in [in]cipher text block - * @param key [in]user key - * @param IV [in]initial vector + * @param Algorithm [in]algorithm + * @param out [out]plain text block + * @param in [in]cipher text block + * @param key [in]user key + * @param IV [in]initial vector * - * @return CRYPTO_SUCCESS if no error is occured - * \n CRYPTO_INVALID_ARGUMENT if parameter is invalid + * @return CRYPTO_SUCCESS if no error is occured + * \n CRYPTO_INVALID_ARGUMENT if parameter is invalid */ -int SDRM_CBC_Dec(int Algorithm, cc_u8 *out, cc_u8 *in, cc_u8 *key, cc_u8 *IV){ +int SDRM_CBC_Dec(int Algorithm, cc_u8 *out, cc_u8 *in, cc_u8 *key, cc_u8 *IV) +{ int i, BlockLen; cc_u8 buf[16]; - switch(Algorithm) - { - case ID_AES128 : - BlockLen = 16; - memcpy(buf, in, BlockLen); - SDRM_rijndaelDecrypt((const cc_u32*)(void*)key, 10, in, out); - break; - case ID_AES192 : - BlockLen = 16; - memcpy(buf, in, BlockLen); - SDRM_rijndaelDecrypt((const cc_u32*)(void*)key, 12, in, out); - break; - case ID_AES256 : - BlockLen = 16; - memcpy(buf, in, BlockLen); - SDRM_rijndaelDecrypt((const cc_u32*)(void*)key, 14, in, out); - break; - case ID_DES : - BlockLen = 8; - memcpy(buf, in, BlockLen); - SDRM_DES_Encryption((cc_u32(*)[2])(void*)key, in, out); - break; - case ID_TDES : - BlockLen = 8; - memcpy(buf, in, BlockLen); - SDRM_TDES_Encryption((cc_u32(*)[2])(void*)key, in, out); - break; - default : - return CRYPTO_INVALID_ARGUMENT; + switch (Algorithm) { + case ID_AES128: + BlockLen = 16; + memcpy(buf, in, BlockLen); + SDRM_rijndaelDecrypt((const cc_u32 *)(void *)key, 10, in, out); + break; + + case ID_AES192: + BlockLen = 16; + memcpy(buf, in, BlockLen); + SDRM_rijndaelDecrypt((const cc_u32 *)(void *)key, 12, in, out); + break; + + case ID_AES256: + BlockLen = 16; + memcpy(buf, in, BlockLen); + SDRM_rijndaelDecrypt((const cc_u32 *)(void *)key, 14, in, out); + break; + + case ID_DES: + BlockLen = 8; + memcpy(buf, in, BlockLen); + SDRM_DES_Encryption((cc_u32(*)[2])(void *)key, in, out); + break; + + case ID_TDES: + BlockLen = 8; + memcpy(buf, in, BlockLen); + SDRM_TDES_Encryption((cc_u32(*)[2])(void *)key, in, out); + break; + + default: + return CRYPTO_INVALID_ARGUMENT; } for (i = 0; i < BlockLen; i++) - { out[i] ^= IV[i]; - } memcpy(IV, buf, BlockLen); @@ -241,58 +246,60 @@ int SDRM_CBC_Dec(int Algorithm, cc_u8 *out, cc_u8 *in, cc_u8 *key, cc_u8 *IV){ } /* - * @fn SDRM_CFB_Enc - * @brief Encrypt a block with CFB mode + * @fn SDRM_CFB_Enc + * @brief Encrypt a block with CFB mode * - * @param Algorithm [in]algorithm - * @param out [out]cipher text block - * @param in [in]plain text block - * @param key [in]user key - * @param IV [in]initial vector + * @param Algorithm [in]algorithm + * @param out [out]cipher text block + * @param in [in]plain text block + * @param key [in]user key + * @param IV [in]initial vector * - * @return CRYPTO_SUCCESS if no error is occured - * \n CRYPTO_INVALID_ARGUMENT if parameter is invalid + * @return CRYPTO_SUCCESS if no error is occured + * \n CRYPTO_INVALID_ARGUMENT if parameter is invalid */ int SDRM_CFB_Enc(int Algorithm, cc_u8 *out, cc_u8 *in, cc_u8 *key, cc_u8 *IV) { int i, BlockLen; cc_u8 buf[16]; - switch(Algorithm) - { - case ID_AES128 : - BlockLen = 16; - memcpy(buf, in, BlockLen); - SDRM_rijndaelEncrypt((const cc_u32*)(void*)key, 10, IV, out); - break; - case ID_AES192 : - BlockLen = 16; - memcpy(buf, in, BlockLen); - SDRM_rijndaelEncrypt((const cc_u32*)(void*)key, 12, IV, out); - break; - case ID_AES256 : - BlockLen = 16; - memcpy(buf, in, BlockLen); - SDRM_rijndaelEncrypt((const cc_u32*)(void*)key, 14, IV, out); - break; - case ID_DES : - BlockLen = 8; - memcpy(buf, in, BlockLen); - SDRM_DES_Encryption((cc_u32(*)[2])(void*)key, IV, out); - break; - case ID_TDES : - BlockLen = 8; - memcpy(buf, in, BlockLen); - SDRM_TDES_Encryption((cc_u32(*)[2])(void*)key, IV, out); - break; - default : - return CRYPTO_INVALID_ARGUMENT; + switch (Algorithm) { + case ID_AES128: + BlockLen = 16; + memcpy(buf, in, BlockLen); + SDRM_rijndaelEncrypt((const cc_u32 *)(void *)key, 10, IV, out); + break; + + case ID_AES192: + BlockLen = 16; + memcpy(buf, in, BlockLen); + SDRM_rijndaelEncrypt((const cc_u32 *)(void *)key, 12, IV, out); + break; + + case ID_AES256: + BlockLen = 16; + memcpy(buf, in, BlockLen); + SDRM_rijndaelEncrypt((const cc_u32 *)(void *)key, 14, IV, out); + break; + + case ID_DES: + BlockLen = 8; + memcpy(buf, in, BlockLen); + SDRM_DES_Encryption((cc_u32(*)[2])(void *)key, IV, out); + break; + + case ID_TDES: + BlockLen = 8; + memcpy(buf, in, BlockLen); + SDRM_TDES_Encryption((cc_u32(*)[2])(void *)key, IV, out); + break; + + default: + return CRYPTO_INVALID_ARGUMENT; } for (i = 0; i < BlockLen; i++) - { out[i] ^= buf[i]; - } memcpy(IV, out, BlockLen); @@ -300,58 +307,60 @@ int SDRM_CFB_Enc(int Algorithm, cc_u8 *out, cc_u8 *in, cc_u8 *key, cc_u8 *IV) } /* - * @fn SDRM_CFB_Dec - * @brief Decrypt a block with CFB mode + * @fn SDRM_CFB_Dec + * @brief Decrypt a block with CFB mode * - * @param Algorithm [in]algorithm - * @param out [out]plain text block - * @param in [in]cipher text block - * @param key [in]user key - * @param IV [in]initial vector + * @param Algorithm [in]algorithm + * @param out [out]plain text block + * @param in [in]cipher text block + * @param key [in]user key + * @param IV [in]initial vector * - * @return CRYPTO_SUCCESS if no error is occured - * \n CRYPTO_INVALID_ARGUMENT if parameter is invalid + * @return CRYPTO_SUCCESS if no error is occured + * \n CRYPTO_INVALID_ARGUMENT if parameter is invalid */ int SDRM_CFB_Dec(int Algorithm, cc_u8 *out, cc_u8 *in, cc_u8 *key, cc_u8 *IV) { int i, BlockLen; cc_u8 buf[16]; - switch(Algorithm) - { - case ID_AES128 : - BlockLen = 16; - memcpy(buf, in, BlockLen); - SDRM_rijndaelEncrypt((const cc_u32*)(void*)key, 10, IV, out); - break; - case ID_AES192 : - BlockLen = 16; - memcpy(buf, in, BlockLen); - SDRM_rijndaelEncrypt((const cc_u32*)(void*)key, 12, IV, out); - break; - case ID_AES256 : - BlockLen = 16; - memcpy(buf, in, BlockLen); - SDRM_rijndaelEncrypt((const cc_u32*)(void*)key, 14, IV, out); - break; - case ID_DES : - BlockLen = 8; - memcpy(buf, in, BlockLen); - SDRM_DES_Encryption((cc_u32(*)[2])(void*)key, IV, out); - break; - case ID_TDES : - BlockLen = 8; - memcpy(buf, in, BlockLen); - SDRM_TDES_Encryption((cc_u32(*)[2])(void*)key, IV, out); - break; - default : - return CRYPTO_INVALID_ARGUMENT; + switch (Algorithm) { + case ID_AES128: + BlockLen = 16; + memcpy(buf, in, BlockLen); + SDRM_rijndaelEncrypt((const cc_u32 *)(void *)key, 10, IV, out); + break; + + case ID_AES192: + BlockLen = 16; + memcpy(buf, in, BlockLen); + SDRM_rijndaelEncrypt((const cc_u32 *)(void *)key, 12, IV, out); + break; + + case ID_AES256: + BlockLen = 16; + memcpy(buf, in, BlockLen); + SDRM_rijndaelEncrypt((const cc_u32 *)(void *)key, 14, IV, out); + break; + + case ID_DES: + BlockLen = 8; + memcpy(buf, in, BlockLen); + SDRM_DES_Encryption((cc_u32(*)[2])(void *)key, IV, out); + break; + + case ID_TDES: + BlockLen = 8; + memcpy(buf, in, BlockLen); + SDRM_TDES_Encryption((cc_u32(*)[2])(void *)key, IV, out); + break; + + default: + return CRYPTO_INVALID_ARGUMENT; } for (i = 0; i < BlockLen; i++) - { out[i] ^= buf[i]; - } memcpy(IV, buf, BlockLen); @@ -359,140 +368,145 @@ int SDRM_CFB_Dec(int Algorithm, cc_u8 *out, cc_u8 *in, cc_u8 *key, cc_u8 *IV) } /* - * @fn SDRM_OFB_Enc - * @brief Encrypt a block with OFB mode + * @fn SDRM_OFB_Enc + * @brief Encrypt a block with OFB mode * - * @param Algorithm [in]algorithm - * @param out [out]cipher text block - * @param in [in]plain text block - * @param key [in]user key - * @param IV [in]initial vector + * @param Algorithm [in]algorithm + * @param out [out]cipher text block + * @param in [in]plain text block + * @param key [in]user key + * @param IV [in]initial vector * - * @return CRYPTO_SUCCESS if no error is occured - * \n CRYPTO_INVALID_ARGUMENT if parameter is invalid + * @return CRYPTO_SUCCESS if no error is occured + * \n CRYPTO_INVALID_ARGUMENT if parameter is invalid */ int SDRM_OFB_Enc(int Algorithm, cc_u8 *out, cc_u8 *in, cc_u8 *key, cc_u8 *IV) { int i, BlockLen; cc_u8 buf[16]; - switch(Algorithm) - { - case ID_AES128 : - BlockLen = 16; - memcpy(buf, in, BlockLen); - SDRM_rijndaelEncrypt((const cc_u32*)(void*)key, 10, IV, out); - break; - case ID_AES192 : - BlockLen = 16; - memcpy(buf, in, BlockLen); - SDRM_rijndaelEncrypt((const cc_u32*)(void*)key, 12, IV, out); - break; - case ID_AES256 : - BlockLen = 16; - memcpy(buf, in, BlockLen); - SDRM_rijndaelEncrypt((const cc_u32*)(void*)key, 14, IV, out); - break; - case ID_DES : - BlockLen = 8; - memcpy(buf, in, BlockLen); - SDRM_DES_Encryption((cc_u32(*)[2])(void*)key, IV, out); - break; - case ID_TDES : - BlockLen = 8; - memcpy(buf, in, BlockLen); - SDRM_TDES_Encryption((cc_u32(*)[2])(void*)key, IV, out); - break; - default : - return CRYPTO_INVALID_ARGUMENT; + switch (Algorithm) { + case ID_AES128: + BlockLen = 16; + memcpy(buf, in, BlockLen); + SDRM_rijndaelEncrypt((const cc_u32 *)(void *)key, 10, IV, out); + break; + + case ID_AES192: + BlockLen = 16; + memcpy(buf, in, BlockLen); + SDRM_rijndaelEncrypt((const cc_u32 *)(void *)key, 12, IV, out); + break; + + case ID_AES256: + BlockLen = 16; + memcpy(buf, in, BlockLen); + SDRM_rijndaelEncrypt((const cc_u32 *)(void *)key, 14, IV, out); + break; + + case ID_DES: + BlockLen = 8; + memcpy(buf, in, BlockLen); + SDRM_DES_Encryption((cc_u32(*)[2])(void *)key, IV, out); + break; + + case ID_TDES: + BlockLen = 8; + memcpy(buf, in, BlockLen); + SDRM_TDES_Encryption((cc_u32(*)[2])(void *)key, IV, out); + break; + + default: + return CRYPTO_INVALID_ARGUMENT; } memcpy(IV, out, BlockLen); for (i = 0; i < BlockLen; i++) - { out[i] ^= buf[i]; - } return CRYPTO_SUCCESS; } /* - * @fn SDRM_CTR_Enc - * @brief Encrypt a block with CTR mode + * @fn SDRM_CTR_Enc + * @brief Encrypt a block with CTR mode * - * @param Algorithm [in]algorithm - * @param out [out]cipher text block - * @param in [in]plain text block - * @param key [in]user key - * @param IV [in]initial vector - * @param counter [in] + * @param Algorithm [in]algorithm + * @param out [out]cipher text block + * @param in [in]plain text block + * @param key [in]user key + * @param IV [in]initial vector + * @param counter [in] * - * @return CRYPTO_SUCCESS if no error is occured - * \n CRYPTO_INVALID_ARGUMENT if parameter is invalid + * @return CRYPTO_SUCCESS if no error is occured + * \n CRYPTO_INVALID_ARGUMENT if parameter is invalid */ -int SDRM_CTR_Enc(int Algorithm, cc_u8 *out, cc_u8 *in, cc_u8 *key, cc_u8 *IV, cc_u32 counter) +int SDRM_CTR_Enc(int Algorithm, cc_u8 *out, cc_u8 *in, cc_u8 *key, cc_u8 *IV, + cc_u32 counter) { int i, BlockLen; cc_u8 buf[16]; - switch(Algorithm) - { - case ID_AES128 : - BlockLen = 16; - IV[12] = (cc_u8)(0xff & (counter >> 24)); - IV[13] = (cc_u8)(0xff & (counter >> 16)); - IV[14] = (cc_u8)(0xff & (counter >> 8 )); - IV[15] = (cc_u8)(0xff & (counter )); - memcpy(buf, in, BlockLen); - SDRM_rijndaelEncrypt((const cc_u32*)(void*)key, 10, IV, out); - break; - case ID_AES192 : - BlockLen = 16; - IV[12] = (cc_u8)(0xff & (counter >> 24)); - IV[13] = (cc_u8)(0xff & (counter >> 16)); - IV[14] = (cc_u8)(0xff & (counter >> 8 )); - IV[15] = (cc_u8)(0xff & (counter )); - memcpy(buf, in, BlockLen); - SDRM_rijndaelEncrypt((const cc_u32*)(void*)key, 12, IV, out); - break; - case ID_AES256 : - BlockLen = 16; - IV[12] = (cc_u8)(0xff & (counter >> 24)); - IV[13] = (cc_u8)(0xff & (counter >> 16)); - IV[14] = (cc_u8)(0xff & (counter >> 8 )); - IV[15] = (cc_u8)(0xff & (counter )); - memcpy(buf, in, BlockLen); - SDRM_rijndaelEncrypt((const cc_u32*)(void*)key, 14, IV, out); - break; - case ID_DES : - BlockLen = 8; - IV[4] = (cc_u8)(0xff & (counter >> 24)); - IV[5] = (cc_u8)(0xff & (counter >> 16)); - IV[6] = (cc_u8)(0xff & (counter >> 8 )); - IV[7] = (cc_u8)(0xff & (counter )); - memcpy(buf, in, BlockLen); - SDRM_DES_Encryption((cc_u32(*)[2])(void*)key, IV, out); - break; - case ID_TDES : - BlockLen = 8; - IV[4] = (cc_u8)(0xff & (counter >> 24)); - IV[5] = (cc_u8)(0xff & (counter >> 16)); - IV[6] = (cc_u8)(0xff & (counter >> 8 )); - IV[7] = (cc_u8)(0xff & (counter )); - memcpy(buf, in, BlockLen); - SDRM_TDES_Encryption((cc_u32(*)[2])(void*)key, IV, out); - break; - default : - return CRYPTO_INVALID_ARGUMENT; + switch (Algorithm) { + case ID_AES128: + BlockLen = 16; + IV[12] = (cc_u8)(0xff & (counter >> 24)); + IV[13] = (cc_u8)(0xff & (counter >> 16)); + IV[14] = (cc_u8)(0xff & (counter >> 8)); + IV[15] = (cc_u8)(0xff & (counter)); + memcpy(buf, in, BlockLen); + SDRM_rijndaelEncrypt((const cc_u32 *)(void *)key, 10, IV, out); + break; + + case ID_AES192: + BlockLen = 16; + IV[12] = (cc_u8)(0xff & (counter >> 24)); + IV[13] = (cc_u8)(0xff & (counter >> 16)); + IV[14] = (cc_u8)(0xff & (counter >> 8)); + IV[15] = (cc_u8)(0xff & (counter)); + memcpy(buf, in, BlockLen); + SDRM_rijndaelEncrypt((const cc_u32 *)(void *)key, 12, IV, out); + break; + + case ID_AES256: + BlockLen = 16; + IV[12] = (cc_u8)(0xff & (counter >> 24)); + IV[13] = (cc_u8)(0xff & (counter >> 16)); + IV[14] = (cc_u8)(0xff & (counter >> 8)); + IV[15] = (cc_u8)(0xff & (counter)); + memcpy(buf, in, BlockLen); + SDRM_rijndaelEncrypt((const cc_u32 *)(void *)key, 14, IV, out); + break; + + case ID_DES: + BlockLen = 8; + IV[4] = (cc_u8)(0xff & (counter >> 24)); + IV[5] = (cc_u8)(0xff & (counter >> 16)); + IV[6] = (cc_u8)(0xff & (counter >> 8)); + IV[7] = (cc_u8)(0xff & (counter)); + memcpy(buf, in, BlockLen); + SDRM_DES_Encryption((cc_u32(*)[2])(void *)key, IV, out); + break; + + case ID_TDES: + BlockLen = 8; + IV[4] = (cc_u8)(0xff & (counter >> 24)); + IV[5] = (cc_u8)(0xff & (counter >> 16)); + IV[6] = (cc_u8)(0xff & (counter >> 8)); + IV[7] = (cc_u8)(0xff & (counter)); + memcpy(buf, in, BlockLen); + SDRM_TDES_Encryption((cc_u32(*)[2])(void *)key, IV, out); + break; + + default: + return CRYPTO_INVALID_ARGUMENT; } for (i = 0; i < BlockLen; i++) - { out[i] ^= buf[i]; - } return CRYPTO_SUCCESS; } -/***************************** End of File *****************************/ \ No newline at end of file +/***************************** End of File *****************************/ diff --git a/ssflib/dep/cryptocore/source/base/cc_pkcs1_v21.c b/ssflib/dep/cryptocore/source/base/cc_pkcs1_v21.c index b202f0a..74f7eb5 100644 --- a/ssflib/dep/cryptocore/source/base/cc_pkcs1_v21.c +++ b/ssflib/dep/cryptocore/source/base/cc_pkcs1_v21.c @@ -33,19 +33,20 @@ ////////////////////////////////////////////////////////////////////////// // Functions ////////////////////////////////////////////////////////////////////////// -static int SDRM_MGF1(int HASH_Algorithm, cc_u8* mask, cc_u8* pbSeed, cc_u32 SeedLen, cc_u32 dMaskLen); +static int SDRM_MGF1(int HASH_Algorithm, cc_u8 *mask, cc_u8 *pbSeed, + cc_u32 SeedLen, cc_u32 dMaskLen); /* -* @fn int SDRM_Enpad_Rsaes_pkcs15(cc_u8 *EM, cc_u8 *m, cc_u32 mLen, cc_u32 k) -* @brief RSAES PKCS#1 v1.5 enpadding +* @fn int SDRM_Enpad_Rsaes_pkcs15(cc_u8 *EM, cc_u8 *m, cc_u32 mLen, cc_u32 k) +* @brief RSAES PKCS#1 v1.5 enpadding * -* @param EM [out]Enpadded msg -* @param m [in]Message to pad -* @param mLen [in]byte-size of m -* @param k [in]byte-size of n(RSA modulus) +* @param EM [out]Enpadded msg +* @param m [in]Message to pad +* @param mLen [in]byte-size of m +* @param k [in]byte-size of n(RSA modulus) * -* @return CRYPTO_SUCCESS if no error is occured -* \n CRYPTO_MSG_TOO_LONG if message is longer then key +* @return CRYPTO_SUCCESS if no error is occured +* \n CRYPTO_MSG_TOO_LONG if message is longer then key */ int SDRM_Enpad_Rsaes_pkcs15(cc_u8 *EM, cc_u8 *m, cc_u32 mLen, cc_u32 k) { @@ -53,17 +54,13 @@ int SDRM_Enpad_Rsaes_pkcs15(cc_u8 *EM, cc_u8 *m, cc_u32 mLen, cc_u32 k) cc_u32 i; if (mLen > k - 11) - { return CRYPTO_MSG_TOO_LONG; - } - EM[0] = 0x00; - EM[1] = 0x02; + EM[0] = 0x00; + EM[1] = 0x02; - for (i = 0; i < 16; i++) - { - Si_ANSI_X9_31[i] = ((rand() << 16) + rand()) & 0xff; - } + for (i = 0; i < 16; i++) + Si_ANSI_X9_31[i] = ((rand() << 16) + rand()) & 0xff; srand(time(NULL)); @@ -71,10 +68,8 @@ int SDRM_Enpad_Rsaes_pkcs15(cc_u8 *EM, cc_u8 *m, cc_u32 mLen, cc_u32 k) EM[k - mLen - 1] = 0x00; - for (i = 2; i < (k - mLen - 1); i++) - { - if (EM[i] == 0) - { + for (i = 2; i < (k - mLen - 1); i++) { + if (EM[i] == 0) { EM[i--] = (cc_u8)(((rand() << 16) + rand()) & 0xff); continue; } @@ -88,49 +83,42 @@ int SDRM_Enpad_Rsaes_pkcs15(cc_u8 *EM, cc_u8 *m, cc_u32 mLen, cc_u32 k) } /* -* @fn int SDRM_Enpad_Rsaes_pkcs15(cc_u8 *EM, cc_u8 *m, cc_u32 mLen, cc_u32 k) -* @brief RSAES PKCS#1 v1.5 depadding +* @fn int SDRM_Enpad_Rsaes_pkcs15(cc_u8 *EM, cc_u8 *m, cc_u32 mLen, cc_u32 k) +* @brief RSAES PKCS#1 v1.5 depadding * -* @param m [out]Depadded msg -* @param mLen [out]byte-size of m -* @param EM [in]Enpadded msg -* @param emLen [in]byte-size of EM -* @param k [in]byte-size of n(RSA modulus) +* @param m [out]Depadded msg +* @param mLen [out]byte-size of m +* @param EM [in]Enpadded msg +* @param emLen [in]byte-size of EM +* @param k [in]byte-size of n(RSA modulus) * -* @return CRYPTO_SUCCESS if no error is occured -* \n CRYPTO_INVALID_ARGUMENT if enpadded message is out of RSA padding scheme +* @return CRYPTO_SUCCESS if no error is occured +* \n CRYPTO_INVALID_ARGUMENT if enpadded message is out of RSA padding scheme */ -int SDRM_Depad_Rsaes_pkcs15(cc_u8 *m, cc_u32* mLen, cc_u8 *EM, cc_u32 emLen, cc_u32 k) +int SDRM_Depad_Rsaes_pkcs15(cc_u8 *m, cc_u32 *mLen, cc_u8 *EM, cc_u32 emLen, + cc_u32 k) { cc_u32 i; - if ((emLen == k) && (EM[0] == 0)) - { + if ((emLen == k) && (EM[0] == 0)) { EM = EM + 1; emLen = emLen - 1; } if ((emLen != k - 1) || (EM[0] != 0x02)) - { return CRYPTO_INVALID_ARGUMENT; - } EM = EM + 1; emLen = emLen - 1; - for (i = 0; i < emLen; i++) - { + for (i = 0; i < emLen; i++) { if (EM[i] == 0) - { break; - } } //i : length of PS if ((i == emLen) || (i < 8)) - { return CRYPTO_INVALID_ARGUMENT; - } EM = EM + i + 1; emLen = emLen - i - 1; @@ -138,9 +126,7 @@ int SDRM_Depad_Rsaes_pkcs15(cc_u8 *m, cc_u32* mLen, cc_u8 *EM, cc_u32 emLen, cc_ memmove(m, EM, emLen); if (mLen != NULL) - { *mLen = emLen; - } return CRYPTO_SUCCESS; } @@ -148,57 +134,63 @@ int SDRM_Depad_Rsaes_pkcs15(cc_u8 *m, cc_u32* mLen, cc_u8 *EM, cc_u32 emLen, cc_ /* -* @fn int SDRM_Enpad_Rsaes_oaep(cc_u8 *EM, cc_u8 *m, cc_u32 mLen, cc_u32 k, int HASH_Algorithm) -* @brief RSAES OAEP enpadding +* @fn int SDRM_Enpad_Rsaes_oaep(cc_u8 *EM, cc_u8 *m, cc_u32 mLen, cc_u32 k, int HASH_Algorithm) +* @brief RSAES OAEP enpadding * -* @param EM [out]Enpadded msg -* @param m [in]Message to pad -* @param mLen [in]byte-size of m -* @param k [in]byte-size of n(RSA modulus) -* @param HASH_Algorithm [in]hash algorithm id +* @param EM [out]Enpadded msg +* @param m [in]Message to pad +* @param mLen [in]byte-size of m +* @param k [in]byte-size of n(RSA modulus) +* @param HASH_Algorithm [in]hash algorithm id * -* @return CRYPTO_SUCCESS if no error is occured -* \n CRYPTO_MSG_TOO_LONG if message is longer then key +* @return CRYPTO_SUCCESS if no error is occured +* \n CRYPTO_MSG_TOO_LONG if message is longer then key */ -int SDRM_Enpad_Rsaes_oaep(cc_u8 *EM, cc_u8 *m, cc_u32 mLen, cc_u32 k, int HASH_Algorithm) +int SDRM_Enpad_Rsaes_oaep(cc_u8 *EM, cc_u8 *m, cc_u32 mLen, cc_u32 k, + int HASH_Algorithm) { cc_u8 Si_ANSI_X9_31[SDRM_X931_SEED_SIZ]; cc_u8 *DB, *seed, *dbMask, *maskedDB, *seedMask, *maskedSeed; cc_u32 i, dbLen; cc_u32 hLen = 0; - SDRM_MD5Context md5_ctx; //Hash env var - SDRM_SHA1Context sha1_ctx; //Hash env var - SDRM_SHA224Context sha224_ctx; //Hash env var - SDRM_SHA256Context sha256_ctx; //Hash env var + SDRM_MD5Context md5_ctx; //Hash env var + SDRM_SHA1Context sha1_ctx; //Hash env var + SDRM_SHA224Context sha224_ctx; //Hash env var + SDRM_SHA256Context sha256_ctx; //Hash env var #ifndef _OP64_NOTSUPPORTED - SDRM_SHA384Context sha384_ctx; //Hash env var - SDRM_SHA512Context sha512_ctx; //Hash env var -#endif //_OP64_NOTSUPPORTED + SDRM_SHA384Context sha384_ctx; //Hash env var + SDRM_SHA512Context sha512_ctx; //Hash env var +#endif //_OP64_NOTSUPPORTED - switch (HASH_Algorithm) - { + switch (HASH_Algorithm) { case ID_MD5: hLen = SDRM_MD5_BLOCK_SIZ; break; + case 0: case ID_SHA1: hLen = SDRM_SHA1_BLOCK_SIZ; break; - case ID_SHA224 : + + case ID_SHA224: hLen = SDRM_SHA224_BLOCK_SIZ; break; + case ID_SHA256: hLen = SDRM_SHA256_BLOCK_SIZ; break; #ifndef _OP64_NOTSUPPORTED + case ID_SHA384: hLen = SDRM_SHA384_BLOCK_SIZ; break; + case ID_SHA512: hLen = SDRM_SHA512_BLOCK_SIZ; break; -#endif //_OP64_NOTSUPPORTED +#endif //_OP64_NOTSUPPORTED + default: return CRYPTO_INVALID_ARGUMENT; } @@ -206,11 +198,10 @@ int SDRM_Enpad_Rsaes_oaep(cc_u8 *EM, cc_u8 *m, cc_u32 mLen, cc_u32 k, int HASH_A dbLen = k - hLen - 1; //Memory allocation - DB = (cc_u8*)malloc(k * 2); + DB = (cc_u8 *)malloc(k * 2); + if (DB == NULL) - { return CRYPTO_MEMORY_ALLOC_FAIL; - } maskedSeed = EM + 1; maskedDB = maskedSeed + hLen; @@ -220,44 +211,54 @@ int SDRM_Enpad_Rsaes_oaep(cc_u8 *EM, cc_u8 *m, cc_u32 mLen, cc_u32 k, int HASH_A seedMask = seed + hLen; //Check message length - if (mLen > k - 2 * hLen - 2) - { + if (mLen > k - 2 * hLen - 2) { free(DB); return CRYPTO_MSG_TOO_LONG; } //Get hash of 'L' - switch (HASH_Algorithm) - { + switch (HASH_Algorithm) { case ID_MD5: - SDRM_MD5_Init(&md5_ctx); //Init hash function - SDRM_MD5_Final(&md5_ctx, DB); //'L' is an empty string, so get output immediately + SDRM_MD5_Init(&md5_ctx); //Init hash function + SDRM_MD5_Final(&md5_ctx, + DB); //'L' is an empty string, so get output immediately break; + case 0: case ID_SHA1: - SDRM_SHA1_Init(&sha1_ctx); //Init hash function - SDRM_SHA1_Final(&sha1_ctx, DB); //'L' is an empty string, so get output immediately + SDRM_SHA1_Init(&sha1_ctx); //Init hash function + SDRM_SHA1_Final(&sha1_ctx, + DB); //'L' is an empty string, so get output immediately break; - case ID_SHA224 : - SDRM_SHA224_Init(&sha224_ctx); //Init hash function - SDRM_SHA224_Final(&sha224_ctx, DB); //'L' is an empty string, so get output immediately + + case ID_SHA224: + SDRM_SHA224_Init(&sha224_ctx); //Init hash function + SDRM_SHA224_Final(&sha224_ctx, + DB); //'L' is an empty string, so get output immediately break; + case ID_SHA256: - SDRM_SHA256_Init(&sha256_ctx); //Init hash function - SDRM_SHA256_Final(&sha256_ctx, DB); //'L' is an empty string, so get output immediately + SDRM_SHA256_Init(&sha256_ctx); //Init hash function + SDRM_SHA256_Final(&sha256_ctx, + DB); //'L' is an empty string, so get output immediately break; #ifndef _OP64_NOTSUPPORTED + case ID_SHA384: - SDRM_SHA384_Init(&sha384_ctx); //Init hash function - SDRM_SHA384_Final(&sha384_ctx, DB); //'L' is an empty string, so get output immediately + SDRM_SHA384_Init(&sha384_ctx); //Init hash function + SDRM_SHA384_Final(&sha384_ctx, + DB); //'L' is an empty string, so get output immediately break; + case ID_SHA512: - SDRM_SHA512_Init(&sha512_ctx); //Init hash function - SDRM_SHA512_Final(&sha512_ctx, DB); //'L' is an empty string, so get output immediately + SDRM_SHA512_Init(&sha512_ctx); //Init hash function + SDRM_SHA512_Final(&sha512_ctx, + DB); //'L' is an empty string, so get output immediately break; -#endif //_OP64_NOTSUPPORTED +#endif //_OP64_NOTSUPPORTED + default: - free(DB); + free(DB); return CRYPTO_INVALID_ARGUMENT; } @@ -268,9 +269,7 @@ int SDRM_Enpad_Rsaes_oaep(cc_u8 *EM, cc_u8 *m, cc_u32 mLen, cc_u32 k, int HASH_A //Generate random seed for (i = 0; i < 16; i++) - { Si_ANSI_X9_31[i] = ((rand() << 16) + rand()) & 0xff; - } SDRM_RNG_X931(Si_ANSI_X9_31, hLen * 8, seed); @@ -278,17 +277,13 @@ int SDRM_Enpad_Rsaes_oaep(cc_u8 *EM, cc_u8 *m, cc_u32 mLen, cc_u32 k, int HASH_A SDRM_MGF1(HASH_Algorithm, dbMask, seed, hLen, dbLen); for (i = 0; i < dbLen; i++) - { maskedDB[i] = DB[i] ^ dbMask[i]; - } //seedMask = MGF(maskedDB, SDRM_SHA1_BLOCK_SIZ), maskedSeed = Seed ^ seedMask SDRM_MGF1(HASH_Algorithm, seedMask, maskedDB, dbLen, hLen); for (i = 0; i < hLen; i++) - { maskedSeed[i] = seed[i] ^ seedMask[i]; - } //EM = 0x00||maskedSeed||maskedDB EM[0] = 0x00; @@ -301,72 +296,76 @@ int SDRM_Enpad_Rsaes_oaep(cc_u8 *EM, cc_u8 *m, cc_u32 mLen, cc_u32 k, int HASH_A } /* -* @fn int SDRM_Depad_Rsaes_oaep(cc_u8 *m, cc_u32* mLen, cc_u8 *EM, cc_u32 emLen, cc_u32 k, int HASH_Algorithm) -* @brief RSAES PKCS#1 v1.5 depadding +* @fn int SDRM_Depad_Rsaes_oaep(cc_u8 *m, cc_u32* mLen, cc_u8 *EM, cc_u32 emLen, cc_u32 k, int HASH_Algorithm) +* @brief RSAES PKCS#1 v1.5 depadding * -* @param m [out]Depadded msg -* @param mLen [out]byte-size of m -* @param EM [in]Enpadded msg -* @param emLen [in]byte-size of EM -* @param k [in]byte-size of n(RSA modulus) -* @param HASH_Algorithm [in]hash algorithm id +* @param m [out]Depadded msg +* @param mLen [out]byte-size of m +* @param EM [in]Enpadded msg +* @param emLen [in]byte-size of EM +* @param k [in]byte-size of n(RSA modulus) +* @param HASH_Algorithm [in]hash algorithm id * -* @return CRYPTO_SUCCESS if no error is occured -* \n CRYPTO_INVALID_ARGUMENT if enpadded message is out of RSA padding scheme +* @return CRYPTO_SUCCESS if no error is occured +* \n CRYPTO_INVALID_ARGUMENT if enpadded message is out of RSA padding scheme */ -int SDRM_Depad_Rsaes_oaep(cc_u8 *m, cc_u32* mLen, cc_u8 *EM, cc_u32 emLen, cc_u32 k, int HASH_Algorithm) +int SDRM_Depad_Rsaes_oaep(cc_u8 *m, cc_u32 *mLen, cc_u8 *EM, cc_u32 emLen, + cc_u32 k, int HASH_Algorithm) { cc_u8 *DB, *seed, *dbMask, *maskedDB, *seedMask, *maskedSeed; cc_u8 hash[SDRM_SHA512_DATA_SIZE]; cc_u32 i, dbLen; cc_u32 hLen = 0; - SDRM_MD5Context md5_ctx; //Hash env var - SDRM_SHA1Context sha1_ctx; //Hash env var - SDRM_SHA224Context sha224_ctx; //Hash env var - SDRM_SHA256Context sha256_ctx; //Hash env var + SDRM_MD5Context md5_ctx; //Hash env var + SDRM_SHA1Context sha1_ctx; //Hash env var + SDRM_SHA224Context sha224_ctx; //Hash env var + SDRM_SHA256Context sha256_ctx; //Hash env var #ifndef _OP64_NOTSUPPORTED - SDRM_SHA384Context sha384_ctx; //Hash env var - SDRM_SHA512Context sha512_ctx; //Hash env var -#endif //_OP64_NOTSUPPORTED + SDRM_SHA384Context sha384_ctx; //Hash env var + SDRM_SHA512Context sha512_ctx; //Hash env var +#endif //_OP64_NOTSUPPORTED - if ((emLen == k) && (EM[0] == 0)) - { + if ((emLen == k) && (EM[0] == 0)) { EM = EM + 1; emLen = emLen - 1; } if (emLen != k - 1) - { return CRYPTO_INVALID_ARGUMENT; - } + /* EM = EM + 1; emLen = emLen - 1; */ - switch (HASH_Algorithm) - { + switch (HASH_Algorithm) { case ID_MD5: hLen = SDRM_MD5_BLOCK_SIZ; break; + case 0: case ID_SHA1: hLen = SDRM_SHA1_BLOCK_SIZ; break; + case ID_SHA224: hLen = SDRM_SHA224_BLOCK_SIZ; break; + case ID_SHA256: hLen = SDRM_SHA256_BLOCK_SIZ; break; #ifndef _OP64_NOTSUPPORTED + case ID_SHA384: hLen = SDRM_SHA384_BLOCK_SIZ; break; + case ID_SHA512: hLen = SDRM_SHA512_BLOCK_SIZ; break; -#endif //_OP64_NOTSUPPORTED +#endif //_OP64_NOTSUPPORTED + default: return CRYPTO_INVALID_ARGUMENT; } @@ -374,11 +373,10 @@ int SDRM_Depad_Rsaes_oaep(cc_u8 *m, cc_u32* mLen, cc_u8 *EM, cc_u32 emLen, cc_u3 dbLen = k - hLen - 1; //Memory allocation - DB = (cc_u8*)malloc(k * 2); + DB = (cc_u8 *)malloc(k * 2); + if (DB == NULL) - { return CRYPTO_MEMORY_ALLOC_FAIL; - } maskedSeed = EM; maskedDB = maskedSeed + hLen; @@ -389,74 +387,77 @@ int SDRM_Depad_Rsaes_oaep(cc_u8 *m, cc_u32* mLen, cc_u8 *EM, cc_u32 emLen, cc_u3 //seedMask = MGF(maskedDB, SDRM_SHA1_BLOCK_SIZ), Seed = maskedSeed ^ seedMask SDRM_MGF1(HASH_Algorithm, seedMask, maskedDB, dbLen, hLen); + for (i = 0; i < hLen; i++) - { seed[i] = maskedSeed[i] ^ seedMask[i]; - } //dbMask = MGF(Seed, dbLen), DB = maskedDB ^ dbMask SDRM_MGF1(HASH_Algorithm, dbMask, seed, hLen, dbLen); + for (i = 0; i < dbLen; i++) - { DB[i] = maskedDB[i] ^ dbMask[i]; - } //Get hash of 'L' - switch (HASH_Algorithm) - { + switch (HASH_Algorithm) { case ID_MD5: - SDRM_MD5_Init(&md5_ctx); //Init hash function - SDRM_MD5_Final(&md5_ctx, hash); //'L' is an empty string, so get output immediately + SDRM_MD5_Init(&md5_ctx); //Init hash function + SDRM_MD5_Final(&md5_ctx, + hash); //'L' is an empty string, so get output immediately break; + case 0: case ID_SHA1: - SDRM_SHA1_Init(&sha1_ctx); //Init hash function - SDRM_SHA1_Final(&sha1_ctx, hash); //'L' is an empty string, so get output immediately + SDRM_SHA1_Init(&sha1_ctx); //Init hash function + SDRM_SHA1_Final(&sha1_ctx, + hash); //'L' is an empty string, so get output immediately break; - case ID_SHA224 : - SDRM_SHA224_Init(&sha224_ctx); //Init hash function - SDRM_SHA224_Final(&sha224_ctx, hash); //'L' is an empty string, so get output immediately + + case ID_SHA224: + SDRM_SHA224_Init(&sha224_ctx); //Init hash function + SDRM_SHA224_Final(&sha224_ctx, + hash); //'L' is an empty string, so get output immediately break; + case ID_SHA256: - SDRM_SHA256_Init(&sha256_ctx); //Init hash function - SDRM_SHA256_Final(&sha256_ctx, hash); //'L' is an empty string, so get output immediately + SDRM_SHA256_Init(&sha256_ctx); //Init hash function + SDRM_SHA256_Final(&sha256_ctx, + hash); //'L' is an empty string, so get output immediately break; #ifndef _OP64_NOTSUPPORTED + case ID_SHA384: - SDRM_SHA384_Init(&sha384_ctx); //Init hash function - SDRM_SHA384_Final(&sha384_ctx, hash); //'L' is an empty string, so get output immediately + SDRM_SHA384_Init(&sha384_ctx); //Init hash function + SDRM_SHA384_Final(&sha384_ctx, + hash); //'L' is an empty string, so get output immediately break; + case ID_SHA512: - SDRM_SHA512_Init(&sha512_ctx); //Init hash function - SDRM_SHA512_Final(&sha512_ctx, hash); //'L' is an empty string, so get output immediately + SDRM_SHA512_Init(&sha512_ctx); //Init hash function + SDRM_SHA512_Final(&sha512_ctx, + hash); //'L' is an empty string, so get output immediately break; -#endif //_OP64_NOTSUPPORTED +#endif //_OP64_NOTSUPPORTED + default: - free(DB); + free(DB); return CRYPTO_INVALID_ARGUMENT; } //Compare hash value - for (i = 0; i < hLen; i++) - { - if (hash[i] != DB[i]) - { + for (i = 0; i < hLen; i++) { + if (hash[i] != DB[i]) { free(DB); return CRYPTO_INVALID_ARGUMENT; } } //ignore 0x00s after hash(PS) - for (; i < dbLen; i++) - { + for (; i < dbLen; i++) { if (DB[i] != 0x00) - { break; - } } - if ((i == dbLen) || (DB[i] != 0x01)) - { + if ((i == dbLen) || (DB[i] != 0x01)) { free(DB); return CRYPTO_INVALID_ARGUMENT; } @@ -464,9 +465,7 @@ int SDRM_Depad_Rsaes_oaep(cc_u8 *m, cc_u32* mLen, cc_u8 *EM, cc_u32 emLen, cc_u3 memmove(m, DB + i + 1, dbLen - i - 1); if (mLen != NULL) - { *mLen = dbLen - i - 1; - } memset(DB, 0x00, k * 2); free(DB); @@ -476,96 +475,98 @@ int SDRM_Depad_Rsaes_oaep(cc_u8 *m, cc_u32* mLen, cc_u8 *EM, cc_u32 emLen, cc_u3 cc_u8 SDRM_DER_MD5[SDRM_DIGESTINFO_MD5_LEN] = SDRM_DIGESTINFO_MD5_VALUE; cc_u8 SDRM_DER_SHA1[SDRM_DIGESTINFO_SHA1_LEN] = SDRM_DIGESTINFO_SHA1_VALUE; -cc_u8 SDRM_DER_SHA224[SDRM_DIGESTINFO_SHA224_LEN] = SDRM_DIGESTINFO_SHA224_VALUE; -cc_u8 SDRM_DER_SHA256[SDRM_DIGESTINFO_SHA256_LEN] = SDRM_DIGESTINFO_SHA256_VALUE; -cc_u8 SDRM_DER_SHA384[SDRM_DIGESTINFO_SHA384_LEN] = SDRM_DIGESTINFO_SHA384_VALUE; -cc_u8 SDRM_DER_SHA512[SDRM_DIGESTINFO_SHA512_LEN] = SDRM_DIGESTINFO_SHA512_VALUE; +cc_u8 SDRM_DER_SHA224[SDRM_DIGESTINFO_SHA224_LEN] = + SDRM_DIGESTINFO_SHA224_VALUE; +cc_u8 SDRM_DER_SHA256[SDRM_DIGESTINFO_SHA256_LEN] = + SDRM_DIGESTINFO_SHA256_VALUE; +cc_u8 SDRM_DER_SHA384[SDRM_DIGESTINFO_SHA384_LEN] = + SDRM_DIGESTINFO_SHA384_VALUE; +cc_u8 SDRM_DER_SHA512[SDRM_DIGESTINFO_SHA512_LEN] = + SDRM_DIGESTINFO_SHA512_VALUE; /* -* @fn int SDRM_Enpad_Rsaes_pkcs15(cc_u8 *EM, cc_u8 *m, cc_u32 mLen, cc_u32 k) -* @brief RSAES PKCS#1 v1.5 enpadding +* @fn int SDRM_Enpad_Rsaes_pkcs15(cc_u8 *EM, cc_u8 *m, cc_u32 mLen, cc_u32 k) +* @brief RSAES PKCS#1 v1.5 enpadding * -* @param EM [out]Enpadded msg -* @param m [in]Message to pad -* @param mLen [in]byte-size of m -* @param k [in]byte-size of n(RSA modulus) +* @param EM [out]Enpadded msg +* @param m [in]Message to pad +* @param mLen [in]byte-size of m +* @param k [in]byte-size of n(RSA modulus) * -* @return CRYPTO_SUCCESS if no error is occured -* \n CRYPTO_MSG_TOO_LONG if message is longer then key +* @return CRYPTO_SUCCESS if no error is occured +* \n CRYPTO_MSG_TOO_LONG if message is longer then key */ -int SDRM_EMSA_PKCS1_v1_5_Encode(cc_u8 *EM, cc_u32 emLen, cc_u8 *h, cc_u32 hLen, int HASH_Algorithm) +int SDRM_EMSA_PKCS1_v1_5_Encode(cc_u8 *EM, cc_u32 emLen, cc_u8 *h, cc_u32 hLen, + int HASH_Algorithm) { cc_u32 tLen; cc_u32 DigestInfoLen = 0; cc_u8 DER[32]; - switch (HASH_Algorithm) - { + switch (HASH_Algorithm) { case ID_MD5: if (hLen != SDRM_MD5_BLOCK_SIZ) - { return CRYPTO_INVALID_ARGUMENT; - } + DigestInfoLen = SDRM_DIGESTINFO_MD5_LEN; memcpy(DER, SDRM_DER_MD5, SDRM_DIGESTINFO_MD5_LEN); tLen = DigestInfoLen + SDRM_MD5_BLOCK_SIZ; break; + case 0: case ID_SHA1: if (hLen != SDRM_SHA1_BLOCK_SIZ) - { return CRYPTO_INVALID_ARGUMENT; - } + DigestInfoLen = SDRM_DIGESTINFO_SHA1_LEN; memcpy(DER, SDRM_DER_SHA1, SDRM_DIGESTINFO_SHA1_LEN); tLen = DigestInfoLen + SDRM_SHA1_BLOCK_SIZ; break; + case ID_SHA224: if (hLen != SDRM_SHA224_BLOCK_SIZ) - { return CRYPTO_INVALID_ARGUMENT; - } + DigestInfoLen = SDRM_DIGESTINFO_SHA224_LEN; memcpy(DER, SDRM_DER_SHA224, SDRM_DIGESTINFO_SHA224_LEN); tLen = DigestInfoLen + SDRM_SHA224_BLOCK_SIZ; break; + case ID_SHA256: if (hLen != SDRM_SHA256_BLOCK_SIZ) - { return CRYPTO_INVALID_ARGUMENT; - } + DigestInfoLen = SDRM_DIGESTINFO_SHA256_LEN; memcpy(DER, SDRM_DER_SHA256, SDRM_DIGESTINFO_SHA256_LEN); tLen = DigestInfoLen + SDRM_SHA256_BLOCK_SIZ; break; #ifndef _OP64_NOTSUPPORTED + case ID_SHA384: if (hLen != SDRM_SHA384_BLOCK_SIZ) - { return CRYPTO_INVALID_ARGUMENT; - } + DigestInfoLen = SDRM_DIGESTINFO_SHA384_LEN; memcpy(DER, SDRM_DER_SHA384, SDRM_DIGESTINFO_SHA384_LEN); tLen = DigestInfoLen + SDRM_SHA384_BLOCK_SIZ; break; + case ID_SHA512: if (hLen != SDRM_SHA512_BLOCK_SIZ) - { return CRYPTO_INVALID_ARGUMENT; - } + DigestInfoLen = SDRM_DIGESTINFO_SHA512_LEN; memcpy(DER, SDRM_DER_SHA512, SDRM_DIGESTINFO_SHA512_LEN); tLen = DigestInfoLen + SDRM_SHA512_BLOCK_SIZ; break; -#endif //_OP64_NOTSUPPORTED +#endif //_OP64_NOTSUPPORTED + default: return CRYPTO_INVALID_ARGUMENT; } if (emLen < tLen + 11) - { return CRYPTO_INVALID_ARGUMENT; - } EM[0] = 0x00; EM[1] = 0x01; @@ -580,61 +581,56 @@ int SDRM_EMSA_PKCS1_v1_5_Encode(cc_u8 *EM, cc_u32 emLen, cc_u8 *h, cc_u32 hLen, /* -* @fn int SDRM_Enpad_Rsaes_pkcs15(cc_u8 *EM, cc_u8 *m, cc_u32 mLen, cc_u32 k) -* @brief RSAES PKCS#1 v1.5 enpadding +* @fn int SDRM_Enpad_Rsaes_pkcs15(cc_u8 *EM, cc_u8 *m, cc_u32 mLen, cc_u32 k) +* @brief RSAES PKCS#1 v1.5 enpadding * -* @param EM [out]Enpadded msg -* @param m [in]Message to pad -* @param mLen [in]byte-size of m -* @param k [in]byte-size of n(RSA modulus) +* @param EM [out]Enpadded msg +* @param m [in]Message to pad +* @param mLen [in]byte-size of m +* @param k [in]byte-size of n(RSA modulus) * -* @return CRYPTO_SUCCESS if no error is occured -* \n CRYPTO_MSG_TOO_LONG if message is longer then key +* @return CRYPTO_SUCCESS if no error is occured +* \n CRYPTO_MSG_TOO_LONG if message is longer then key */ -int SDRM_Enpad_Rsassa_pkcs15(cc_u8 *EM, cc_u32 emLen, cc_u8 *h, cc_u32 hLen, int HASH_Algorithm) +int SDRM_Enpad_Rsassa_pkcs15(cc_u8 *EM, cc_u32 emLen, cc_u8 *h, cc_u32 hLen, + int HASH_Algorithm) { return SDRM_EMSA_PKCS1_v1_5_Encode(EM, emLen, h, hLen, HASH_Algorithm); } -int SDRM_Depad_Rsassa_pkcs15(cc_u8 *EM, cc_u32 emLen, cc_u8 *h, cc_u32 hLen, int HASH_Algorithm) +int SDRM_Depad_Rsassa_pkcs15(cc_u8 *EM, cc_u32 emLen, cc_u8 *h, cc_u32 hLen, + int HASH_Algorithm) { int ret; - cc_u8* EM_; + cc_u8 *EM_; cc_u32 em_Len; - if (EM[0] == 0x00) - { + if (EM[0] == 0x00) { EM = EM + 1; emLen = emLen - 1; } if (EM[0] != 0x01) - { return CRYPTO_INVALID_ARGUMENT; - } em_Len = emLen + 1; - EM_ = (cc_u8*)malloc(em_Len); + EM_ = (cc_u8 *)malloc(em_Len); + if (EM_ == NULL) - { return CRYPTO_MEMORY_ALLOC_FAIL; - } ret = SDRM_EMSA_PKCS1_v1_5_Encode(EM_, em_Len, h, hLen, HASH_Algorithm); - if (ret != CRYPTO_SUCCESS) - { + + if (ret != CRYPTO_SUCCESS) { free(EM_); return ret; } if (memcmp(EM_ + 1, EM, emLen) == 0) - { ret = CRYPTO_VALID_SIGN; - } + else - { ret = CRYPTO_INVALID_SIGN; - } memset(EM_, 0x00, em_Len); free(EM_); @@ -643,7 +639,8 @@ int SDRM_Depad_Rsassa_pkcs15(cc_u8 *EM, cc_u32 emLen, cc_u8 *h, cc_u32 hLen, int } -int SDRM_Enpad_Rsassa_pss(cc_u8 *EM, cc_u32 nBits, cc_u8 *h, cc_u32 hLen, cc_u32 k, int HASH_Algorithm) +int SDRM_Enpad_Rsassa_pss(cc_u8 *EM, cc_u32 nBits, cc_u8 *h, cc_u32 hLen, + cc_u32 k, int HASH_Algorithm) { cc_u8 Si_ANSI_X9_31[SDRM_X931_SEED_SIZ]; cc_u8 salt[64]; @@ -654,108 +651,102 @@ int SDRM_Enpad_Rsassa_pss(cc_u8 *EM, cc_u32 nBits, cc_u8 *h, cc_u32 hLen, cc_u32 cc_u32 dbLen; cc_u32 i; - SDRM_MD5Context md5_ctx; //Hash env var - SDRM_SHA1Context sha1_ctx; //Hash env var - SDRM_SHA224Context sha224_ctx; //Hash env var - SDRM_SHA256Context sha256_ctx; //Hash env var + SDRM_MD5Context md5_ctx; //Hash env var + SDRM_SHA1Context sha1_ctx; //Hash env var + SDRM_SHA224Context sha224_ctx; //Hash env var + SDRM_SHA256Context sha256_ctx; //Hash env var #ifndef _OP64_NOTSUPPORTED - SDRM_SHA384Context sha384_ctx; //Hash env var - SDRM_SHA512Context sha512_ctx; //Hash env var -#endif //_OP64_NOTSUPPORTED + SDRM_SHA384Context sha384_ctx; //Hash env var + SDRM_SHA512Context sha512_ctx; //Hash env var +#endif //_OP64_NOTSUPPORTED - if (msBits == 0) - { + if (msBits == 0) { EM[0] = 0; EM = EM + 1; emLen = emLen - 1; } if (emLen < hLen + sLen + 2) - { return CRYPTO_INVALID_ARGUMENT; - } dbLen = emLen - hLen - 1; for (i = 0; i < 16; i++) - { Si_ANSI_X9_31[i] = ((rand() << 16) + rand()) & 0xff; - } SDRM_RNG_X931(Si_ANSI_X9_31, sLen * 8, salt); //Get Hash of M' - switch (HASH_Algorithm) - { + switch (HASH_Algorithm) { case ID_MD5: if (hLen != SDRM_MD5_BLOCK_SIZ) - { return CRYPTO_INVALID_ARGUMENT; - } - SDRM_MD5_Init(&md5_ctx); //Init hash function - SDRM_MD5_Update(&md5_ctx, eight_zeros, 8); //Input data + + SDRM_MD5_Init(&md5_ctx); //Init hash function + SDRM_MD5_Update(&md5_ctx, eight_zeros, 8); //Input data SDRM_MD5_Update(&md5_ctx, h, hLen); SDRM_MD5_Update(&md5_ctx, salt, sLen); - SDRM_MD5_Final(&md5_ctx, EM + dbLen); //Get Output + SDRM_MD5_Final(&md5_ctx, EM + dbLen); //Get Output break; + case 0: case ID_SHA1: if (hLen != SDRM_SHA1_BLOCK_SIZ) - { return CRYPTO_INVALID_ARGUMENT; - } - SDRM_SHA1_Init(&sha1_ctx); //Init hash function - SDRM_SHA1_Update(&sha1_ctx, eight_zeros, 8); //Input data + + SDRM_SHA1_Init(&sha1_ctx); //Init hash function + SDRM_SHA1_Update(&sha1_ctx, eight_zeros, 8); //Input data SDRM_SHA1_Update(&sha1_ctx, h, hLen); SDRM_SHA1_Update(&sha1_ctx, salt, sLen); - SDRM_SHA1_Final(&sha1_ctx, EM + dbLen); //Get Output + SDRM_SHA1_Final(&sha1_ctx, EM + dbLen); //Get Output break; + case ID_SHA224: if (hLen != SDRM_SHA224_BLOCK_SIZ) - { return CRYPTO_INVALID_ARGUMENT; - } - SDRM_SHA224_Init(&sha224_ctx); //Init hash function - SDRM_SHA224_Update(&sha224_ctx, eight_zeros, 8); //Input data + + SDRM_SHA224_Init(&sha224_ctx); //Init hash function + SDRM_SHA224_Update(&sha224_ctx, eight_zeros, 8); //Input data SDRM_SHA224_Update(&sha224_ctx, h, hLen); SDRM_SHA224_Update(&sha224_ctx, salt, sLen); - SDRM_SHA224_Final(&sha224_ctx, EM + dbLen); //Get Output + SDRM_SHA224_Final(&sha224_ctx, EM + dbLen); //Get Output break; + case ID_SHA256: if (hLen != SDRM_SHA256_BLOCK_SIZ) - { return CRYPTO_INVALID_ARGUMENT; - } - SDRM_SHA256_Init(&sha256_ctx); //Init hash function - SDRM_SHA256_Update(&sha256_ctx, eight_zeros, 8); //Input data + + SDRM_SHA256_Init(&sha256_ctx); //Init hash function + SDRM_SHA256_Update(&sha256_ctx, eight_zeros, 8); //Input data SDRM_SHA256_Update(&sha256_ctx, h, hLen); SDRM_SHA256_Update(&sha256_ctx, salt, sLen); - SDRM_SHA256_Final(&sha256_ctx, EM + dbLen); //Get Output + SDRM_SHA256_Final(&sha256_ctx, EM + dbLen); //Get Output break; #ifndef _OP64_NOTSUPPORTED + case ID_SHA384: if (hLen != SDRM_SHA384_BLOCK_SIZ) - { return CRYPTO_INVALID_ARGUMENT; - } - SDRM_SHA384_Init(&sha384_ctx); //Init hash function - SDRM_SHA384_Update(&sha384_ctx, eight_zeros, 8); //Input data + + SDRM_SHA384_Init(&sha384_ctx); //Init hash function + SDRM_SHA384_Update(&sha384_ctx, eight_zeros, 8); //Input data SDRM_SHA384_Update(&sha384_ctx, h, hLen); SDRM_SHA384_Update(&sha384_ctx, salt, sLen); - SDRM_SHA384_Final(&sha384_ctx, EM + dbLen); //Get Output + SDRM_SHA384_Final(&sha384_ctx, EM + dbLen); //Get Output break; + case ID_SHA512: if (hLen != SDRM_SHA512_BLOCK_SIZ) - { return CRYPTO_INVALID_ARGUMENT; - } - SDRM_SHA512_Init(&sha512_ctx); //Init hash function - SDRM_SHA512_Update(&sha512_ctx, eight_zeros, 8); //Input data + + SDRM_SHA512_Init(&sha512_ctx); //Init hash function + SDRM_SHA512_Update(&sha512_ctx, eight_zeros, 8); //Input data SDRM_SHA512_Update(&sha512_ctx, h, hLen); SDRM_SHA512_Update(&sha512_ctx, salt, sLen); - SDRM_SHA512_Final(&sha512_ctx, EM + dbLen); //Get Output + SDRM_SHA512_Final(&sha512_ctx, EM + dbLen); //Get Output break; -#endif //_OP64_NOTSUPPORTED +#endif //_OP64_NOTSUPPORTED + default: return CRYPTO_INVALID_ARGUMENT; } @@ -766,14 +757,10 @@ int SDRM_Enpad_Rsassa_pss(cc_u8 *EM, cc_u32 nBits, cc_u8 *h, cc_u32 hLen, cc_u32 //memset(EM, 0x00, emLen - sLen - hLen - 2); for (i = 0; i < sLen; i++) - { EM[emLen - sLen - hLen - 1 + i] ^= salt[i]; - } if (msBits != 0) - { EM[0] &= (0xff >> (8 - msBits)); - } EM[emLen - 1] = 0xbc; @@ -784,9 +771,10 @@ int SDRM_Enpad_Rsassa_pss(cc_u8 *EM, cc_u32 nBits, cc_u8 *h, cc_u32 hLen, cc_u32 } -int SDRM_Depad_Rsassa_pss(cc_u8 *EM, cc_u32 nBits, cc_u8 *h, cc_u32 hLen, cc_u32 k, int HASH_Algorithm) +int SDRM_Depad_Rsassa_pss(cc_u8 *EM, cc_u32 nBits, cc_u8 *h, cc_u32 hLen, + cc_u32 k, int HASH_Algorithm) { - cc_u8* DB; + cc_u8 *DB; cc_u8 eight_zeros[8] = { 0 }; cc_u32 msBits = (nBits - 1) & 0x07; cc_u32 emLen = k; @@ -795,60 +783,46 @@ int SDRM_Depad_Rsassa_pss(cc_u8 *EM, cc_u32 nBits, cc_u8 *h, cc_u32 hLen, cc_u32 cc_u32 i; cc_u8 hash[64] = {0}; - SDRM_MD5Context md5_ctx; //Hash env var - SDRM_SHA1Context sha1_ctx; //Hash env var - SDRM_SHA224Context sha224_ctx; //Hash env var - SDRM_SHA256Context sha256_ctx; //Hash env var + SDRM_MD5Context md5_ctx; //Hash env var + SDRM_SHA1Context sha1_ctx; //Hash env var + SDRM_SHA224Context sha224_ctx; //Hash env var + SDRM_SHA256Context sha256_ctx; //Hash env var #ifndef _OP64_NOTSUPPORTED - SDRM_SHA384Context sha384_ctx; //Hash env var - SDRM_SHA512Context sha512_ctx; //Hash env var -#endif //_OP64_NOTSUPPORTED + SDRM_SHA384Context sha384_ctx; //Hash env var + SDRM_SHA512Context sha512_ctx; //Hash env var +#endif //_OP64_NOTSUPPORTED if (EM[0] & (0xff << msBits)) - { return CRYPTO_INVALID_SIGN; - } - if (msBits == 0) - { + if (msBits == 0) { EM = EM + 1; emLen = emLen - 1; } if (EM[emLen - 1] != 0xbc) - { return CRYPTO_INVALID_SIGN; - } dbLen = emLen - hLen - 1; - DB = (cc_u8*)malloc(dbLen); + DB = (cc_u8 *)malloc(dbLen); + if (DB == NULL) - { return CRYPTO_MEMORY_ALLOC_FAIL; - } SDRM_MGF1(HASH_Algorithm, DB, EM + dbLen, hLen, dbLen); for (i = 0; i < dbLen; i++) - { DB[i] = DB[i] ^ EM[i]; - } if (msBits) - { DB[0] &= (0xff >> (8 - msBits)); - } - for (i = 0; i < dbLen; i++) - { + for (i = 0; i < dbLen; i++) { if (DB[i] != 0) - { break; - } } - if ((i == dbLen) || (DB[i] != 0x01)) - { + if ((i == dbLen) || (DB[i] != 0x01)) { free(DB); return CRYPTO_INVALID_ARGUMENT; } @@ -856,83 +830,88 @@ int SDRM_Depad_Rsassa_pss(cc_u8 *EM, cc_u32 nBits, cc_u8 *h, cc_u32 hLen, cc_u32 sLen = dbLen - i - 1; //Get Hash of M' - switch (HASH_Algorithm) - { + switch (HASH_Algorithm) { case ID_MD5: - if (hLen != SDRM_MD5_BLOCK_SIZ) - { - free(DB); + if (hLen != SDRM_MD5_BLOCK_SIZ) { + free(DB); return CRYPTO_INVALID_ARGUMENT; } - SDRM_MD5_Init(&md5_ctx); //Init hash function - SDRM_MD5_Update(&md5_ctx, eight_zeros, 8); //Input data + + SDRM_MD5_Init(&md5_ctx); //Init hash function + SDRM_MD5_Update(&md5_ctx, eight_zeros, 8); //Input data SDRM_MD5_Update(&md5_ctx, h, hLen); SDRM_MD5_Update(&md5_ctx, DB + i + 1, sLen); - SDRM_MD5_Final(&md5_ctx, hash); //Get Output + SDRM_MD5_Final(&md5_ctx, hash); //Get Output break; + case 0: case ID_SHA1: - if (hLen != SDRM_SHA1_BLOCK_SIZ) - { - free(DB); + if (hLen != SDRM_SHA1_BLOCK_SIZ) { + free(DB); return CRYPTO_INVALID_ARGUMENT; } - SDRM_SHA1_Init(&sha1_ctx); //Init hash function - SDRM_SHA1_Update(&sha1_ctx, eight_zeros, 8); //Input data + + SDRM_SHA1_Init(&sha1_ctx); //Init hash function + SDRM_SHA1_Update(&sha1_ctx, eight_zeros, 8); //Input data SDRM_SHA1_Update(&sha1_ctx, h, hLen); SDRM_SHA1_Update(&sha1_ctx, DB + i + 1, sLen); - SDRM_SHA1_Final(&sha1_ctx, hash); //Get Output + SDRM_SHA1_Final(&sha1_ctx, hash); //Get Output break; + case ID_SHA224: - if (hLen != SDRM_SHA224_BLOCK_SIZ) - { - free(DB); + if (hLen != SDRM_SHA224_BLOCK_SIZ) { + free(DB); return CRYPTO_INVALID_ARGUMENT; } - SDRM_SHA224_Init(&sha224_ctx); //Init hash function - SDRM_SHA224_Update(&sha224_ctx, eight_zeros, 8); //Input data + + SDRM_SHA224_Init(&sha224_ctx); //Init hash function + SDRM_SHA224_Update(&sha224_ctx, eight_zeros, 8); //Input data SDRM_SHA224_Update(&sha224_ctx, h, hLen); SDRM_SHA224_Update(&sha224_ctx, DB + i + 1, sLen); - SDRM_SHA224_Final(&sha224_ctx, hash); //Get Output + SDRM_SHA224_Final(&sha224_ctx, hash); //Get Output break; + case ID_SHA256: - if (hLen != SDRM_SHA256_BLOCK_SIZ) - { - free(DB); + if (hLen != SDRM_SHA256_BLOCK_SIZ) { + free(DB); return CRYPTO_INVALID_ARGUMENT; } - SDRM_SHA256_Init(&sha256_ctx); //Init hash function - SDRM_SHA256_Update(&sha256_ctx, eight_zeros, 8); //Input data + + SDRM_SHA256_Init(&sha256_ctx); //Init hash function + SDRM_SHA256_Update(&sha256_ctx, eight_zeros, 8); //Input data SDRM_SHA256_Update(&sha256_ctx, h, hLen); SDRM_SHA256_Update(&sha256_ctx, DB + i + 1, sLen); - SDRM_SHA256_Final(&sha256_ctx, hash); //Get Output + SDRM_SHA256_Final(&sha256_ctx, hash); //Get Output break; #ifndef _OP64_NOTSUPPORTED + case ID_SHA384: - if (hLen != SDRM_SHA384_BLOCK_SIZ) - { - free(DB); + if (hLen != SDRM_SHA384_BLOCK_SIZ) { + free(DB); return CRYPTO_INVALID_ARGUMENT; } - SDRM_SHA384_Init(&sha384_ctx); //Init hash function - SDRM_SHA384_Update(&sha384_ctx, eight_zeros, 8); //Input data + + SDRM_SHA384_Init(&sha384_ctx); //Init hash function + SDRM_SHA384_Update(&sha384_ctx, eight_zeros, 8); //Input data SDRM_SHA384_Update(&sha384_ctx, h, hLen); SDRM_SHA384_Update(&sha384_ctx, DB + i + 1, sLen); - SDRM_SHA384_Final(&sha384_ctx, hash); //Get Output + SDRM_SHA384_Final(&sha384_ctx, hash); //Get Output break; + case ID_SHA512: - if (hLen != SDRM_SHA512_BLOCK_SIZ) - { - free(DB); + if (hLen != SDRM_SHA512_BLOCK_SIZ) { + free(DB); return CRYPTO_INVALID_ARGUMENT; } - SDRM_SHA512_Init(&sha512_ctx); //Init hash function - SDRM_SHA512_Update(&sha512_ctx, eight_zeros, 8); //Input data + + SDRM_SHA512_Init(&sha512_ctx); //Init hash function + SDRM_SHA512_Update(&sha512_ctx, eight_zeros, 8); //Input data SDRM_SHA512_Update(&sha512_ctx, h, hLen); SDRM_SHA512_Update(&sha512_ctx, DB + i + 1, sLen); - SDRM_SHA512_Final(&sha512_ctx, hash); //Get Output + SDRM_SHA512_Final(&sha512_ctx, hash); //Get Output break; -#endif //_OP64_NOTSUPPORTED +#endif //_OP64_NOTSUPPORTED + default: free(DB); return CRYPTO_INVALID_ARGUMENT; @@ -942,74 +921,76 @@ int SDRM_Depad_Rsassa_pss(cc_u8 *EM, cc_u32 nBits, cc_u8 *h, cc_u32 hLen, cc_u32 free(DB); if (memcmp(hash, EM + dbLen, hLen) == 0) - { return CRYPTO_VALID_SIGN; - } + else - { return CRYPTO_INVALID_SIGN; - } } /* - * @fn int SDRM_MGF1(int HASH_Algorithm, cc_u8* mask, cc_u8* pbSeed, cc_u32 SeedLen, cc_u32 dMaskLen) - * @brief SDRM_MGF1 Function (Mask Generation Function based on a hash function) + * @fn int SDRM_MGF1(int HASH_Algorithm, cc_u8* mask, cc_u8* pbSeed, cc_u32 SeedLen, cc_u32 dMaskLen) + * @brief SDRM_MGF1 Function (Mask Generation Function based on a hash function) * - * @param mask [out]byte-length of generated mask - * @param pbSeed [in]seed for MGF - * @param SeedLen [in]byte-length of pbSeed - * @param dMaskLen [in]byte-length of mask + * @param mask [out]byte-length of generated mask + * @param pbSeed [in]seed for MGF + * @param SeedLen [in]byte-length of pbSeed + * @param dMaskLen [in]byte-length of mask * - * @return CRYPTO_SUCCESS if no error is occured - * \n CRYPTO_MEMORY_ALLOC_FAIL if malloc is failed + * @return CRYPTO_SUCCESS if no error is occured + * \n CRYPTO_MEMORY_ALLOC_FAIL if malloc is failed */ -static int SDRM_MGF1(int HASH_Algorithm, cc_u8* mask, cc_u8* pbSeed, cc_u32 SeedLen, cc_u32 dMaskLen) +static int SDRM_MGF1(int HASH_Algorithm, cc_u8 *mask, cc_u8 *pbSeed, + cc_u32 SeedLen, cc_u32 dMaskLen) { - cc_u8 *T, *Seed, *pbBuf; - cc_u32 counter; - cc_u8 hash[64]; //SHA-1 output size is 160 bit - cc_u8 hashlen; - SDRM_MD5Context md5_ctx; //Hash env var - SDRM_SHA1Context sha1_ctx; //Hash env var - SDRM_SHA224Context sha224_ctx; //Hash env var - SDRM_SHA256Context sha256_ctx; //Hash env var + cc_u8 *T, *Seed, *pbBuf; + cc_u32 counter; + cc_u8 hash[64]; //SHA-1 output size is 160 bit + cc_u8 hashlen; + SDRM_MD5Context md5_ctx; //Hash env var + SDRM_SHA1Context sha1_ctx; //Hash env var + SDRM_SHA224Context sha224_ctx; //Hash env var + SDRM_SHA256Context sha256_ctx; //Hash env var #ifndef _OP64_NOTSUPPORTED - SDRM_SHA384Context sha384_ctx; //Hash env var - SDRM_SHA512Context sha512_ctx; //Hash env var -#endif //_OP64_NOTSUPPORTED - - switch(HASH_Algorithm) - { - case ID_MD5 : - hashlen = SDRM_MD5_BLOCK_SIZ; - break; - case 0 : - case ID_SHA1 : - hashlen = SDRM_SHA1_BLOCK_SIZ; - break; - case ID_SHA224 : - hashlen = SDRM_SHA224_BLOCK_SIZ; - break; - case ID_SHA256 : - hashlen = SDRM_SHA256_BLOCK_SIZ; - break; + SDRM_SHA384Context sha384_ctx; //Hash env var + SDRM_SHA512Context sha512_ctx; //Hash env var +#endif //_OP64_NOTSUPPORTED + + switch (HASH_Algorithm) { + case ID_MD5: + hashlen = SDRM_MD5_BLOCK_SIZ; + break; + + case 0: + case ID_SHA1: + hashlen = SDRM_SHA1_BLOCK_SIZ; + break; + + case ID_SHA224: + hashlen = SDRM_SHA224_BLOCK_SIZ; + break; + + case ID_SHA256: + hashlen = SDRM_SHA256_BLOCK_SIZ; + break; #ifndef _OP64_NOTSUPPORTED - case ID_SHA384 : - hashlen = SDRM_SHA384_BLOCK_SIZ; - break; - case ID_SHA512 : - hashlen = SDRM_SHA512_BLOCK_SIZ; - break; -#endif //_OP64_NOTSUPPORTED - default : - return CRYPTO_INVALID_ARGUMENT; + + case ID_SHA384: + hashlen = SDRM_SHA384_BLOCK_SIZ; + break; + + case ID_SHA512: + hashlen = SDRM_SHA512_BLOCK_SIZ; + break; +#endif //_OP64_NOTSUPPORTED + + default: + return CRYPTO_INVALID_ARGUMENT; } - pbBuf = (cc_u8*)malloc(dMaskLen + hashlen + SeedLen + 4); + pbBuf = (cc_u8 *)malloc(dMaskLen + hashlen + SeedLen + 4); + if (pbBuf == NULL) - { return CRYPTO_MEMORY_ALLOC_FAIL; - } T = pbBuf; Seed = T + dMaskLen + hashlen; @@ -1017,52 +998,56 @@ static int SDRM_MGF1(int HASH_Algorithm, cc_u8* mask, cc_u8* pbSeed, cc_u32 Seed memset(mask, 0, dMaskLen); memcpy(Seed, pbSeed, SeedLen); - for (counter = 0; counter < (dMaskLen - 1) / hashlen + 1; counter++) - { - Seed[SeedLen ] = (cc_u8)(counter >> 24); + for (counter = 0; counter < (dMaskLen - 1) / hashlen + 1; counter++) { + Seed[SeedLen] = (cc_u8)(counter >> 24); Seed[SeedLen + 1] = (cc_u8)(counter >> 16); - Seed[SeedLen + 2] = (cc_u8)(counter >> 8 ); - Seed[SeedLen + 3] = (cc_u8)(counter ); + Seed[SeedLen + 2] = (cc_u8)(counter >> 8); + Seed[SeedLen + 3] = (cc_u8)(counter); //Hash(Seed||counter) - switch(HASH_Algorithm) - { - case ID_MD5 : - SDRM_MD5_Init(&md5_ctx); //Init hash function - SDRM_MD5_Update(&md5_ctx, Seed, SeedLen + 4); //Input data - SDRM_MD5_Final(&md5_ctx, hash); //Get Output - break; - case 0 : - case ID_SHA1 : - SDRM_SHA1_Init(&sha1_ctx); //Init hash function - SDRM_SHA1_Update(&sha1_ctx, Seed, SeedLen + 4); //Input data - SDRM_SHA1_Final(&sha1_ctx, hash); //Get Output - break; - case ID_SHA224 : - SDRM_SHA224_Init(&sha224_ctx); //Init hash function - SDRM_SHA224_Update(&sha224_ctx, Seed, SeedLen + 4); //Input data - SDRM_SHA224_Final(&sha224_ctx, hash); //Get Output - break; - case ID_SHA256 : - SDRM_SHA256_Init(&sha256_ctx); //Init hash function - SDRM_SHA256_Update(&sha256_ctx, Seed, SeedLen + 4); //Input data - SDRM_SHA256_Final(&sha256_ctx, hash); //Get Output - break; + switch (HASH_Algorithm) { + case ID_MD5: + SDRM_MD5_Init(&md5_ctx); //Init hash function + SDRM_MD5_Update(&md5_ctx, Seed, SeedLen + 4); //Input data + SDRM_MD5_Final(&md5_ctx, hash); //Get Output + break; + + case 0: + case ID_SHA1: + SDRM_SHA1_Init(&sha1_ctx); //Init hash function + SDRM_SHA1_Update(&sha1_ctx, Seed, SeedLen + 4); //Input data + SDRM_SHA1_Final(&sha1_ctx, hash); //Get Output + break; + + case ID_SHA224: + SDRM_SHA224_Init(&sha224_ctx); //Init hash function + SDRM_SHA224_Update(&sha224_ctx, Seed, SeedLen + 4); //Input data + SDRM_SHA224_Final(&sha224_ctx, hash); //Get Output + break; + + case ID_SHA256: + SDRM_SHA256_Init(&sha256_ctx); //Init hash function + SDRM_SHA256_Update(&sha256_ctx, Seed, SeedLen + 4); //Input data + SDRM_SHA256_Final(&sha256_ctx, hash); //Get Output + break; #ifndef _OP64_NOTSUPPORTED - case ID_SHA384 : - SDRM_SHA384_Init(&sha384_ctx); //Init hash function - SDRM_SHA384_Update(&sha384_ctx, Seed, SeedLen + 4); //Input data - SDRM_SHA384_Final(&sha384_ctx, hash); //Get Output - break; - case ID_SHA512 : - SDRM_SHA512_Init(&sha512_ctx); //Init hash function - SDRM_SHA512_Update(&sha512_ctx, Seed, SeedLen + 4); //Input data - SDRM_SHA512_Final(&sha512_ctx, hash); //Get Output - break; -#endif //_OP64_NOTSUPPORTED - default : - free(pbBuf); - return CRYPTO_INVALID_ARGUMENT; + + case ID_SHA384: + SDRM_SHA384_Init(&sha384_ctx); //Init hash function + SDRM_SHA384_Update(&sha384_ctx, Seed, SeedLen + 4); //Input data + SDRM_SHA384_Final(&sha384_ctx, hash); //Get Output + break; + + case ID_SHA512: + SDRM_SHA512_Init(&sha512_ctx); //Init hash function + SDRM_SHA512_Update(&sha512_ctx, Seed, SeedLen + 4); //Input data + SDRM_SHA512_Final(&sha512_ctx, hash); //Get Output + break; +#endif //_OP64_NOTSUPPORTED + + default: + free(pbBuf); + return CRYPTO_INVALID_ARGUMENT; } memcpy(T + counter * hashlen, hash, hashlen); diff --git a/ssflib/dep/cryptocore/source/base/cc_rc4.c b/ssflib/dep/cryptocore/source/base/cc_rc4.c index 51fdb0c..ec769e6 100644 --- a/ssflib/dep/cryptocore/source/base/cc_rc4.c +++ b/ssflib/dep/cryptocore/source/base/cc_rc4.c @@ -69,30 +69,28 @@ static cc_u32 RC4_S_VALUE_BIG[] = { }; /* - * @fn SDRM_RC4_Setup - * @brief intialize s + * @fn SDRM_RC4_Setup + * @brief intialize s * - * @param ctx [in]crypto context - * @param UserKey [in]user key - * @param keyLen [out]byte-length of UserKey + * @param ctx [in]crypto context + * @param UserKey [in]user key + * @param keyLen [out]byte-length of UserKey * - * @return CRYPTO_SUCCESS if no error is occured + * @return CRYPTO_SUCCESS if no error is occured */ int SDRM_RC4_Setup(SDRM_RC4Context *ctx, cc_u8 *UserKey, cc_u32 keyLen) { - cc_u32 i, j, loc = keyLen; - cc_u8 temp; + cc_u32 i, j, loc = keyLen; + cc_u8 temp; //initialization i = 0xff; - if (((cc_u8*)&i)[0] == 0xff) - { -// LOG4DRM_INFO(&CryptoLogCTX), "is Little Endian machine\n"); + + if (((cc_u8 *)&i)[0] == 0xff) { + // LOG4DRM_INFO(&CryptoLogCTX), "is Little Endian machine\n"); memcpy(ctx->s, RC4_S_VALUE_LITTLE, 256); - } - else - { -// LOG4DRM_INFO(&CryptoLogCTX), "is Big Endian machine\n"); + } else { + // LOG4DRM_INFO(&CryptoLogCTX), "is Big Endian machine\n"); memcpy(ctx->s, RC4_S_VALUE_BIG, 256); } @@ -103,21 +101,17 @@ int SDRM_RC4_Setup(SDRM_RC4Context *ctx, cc_u8 *UserKey, cc_u32 keyLen) ctx->j = 0; //scrambling - if ((keyLen == 16) || (keyLen == 32)) - { + if ((keyLen == 16) || (keyLen == 32)) { loc--; - for (i = 0, j = 0; i < 256; ++i) - { - j= (j + ctx->key[i & loc] + ctx->s[i]) & 0xff; + + for (i = 0, j = 0; i < 256; ++i) { + j = (j + ctx->key[i & loc] + ctx->s[i]) & 0xff; temp = ctx->s[i]; ctx->s[i] = ctx->s[j]; ctx->s[j] = temp; } - } - else - { - for (i = 0, j = 0; i < 256; ++i) - { + } else { + for (i = 0, j = 0; i < 256; ++i) { j = (j + ctx->key[i % ctx->keyLen] + ctx->s[i]) & 0xff; temp = ctx->s[i]; ctx->s[i] = ctx->s[j]; @@ -129,26 +123,25 @@ int SDRM_RC4_Setup(SDRM_RC4Context *ctx, cc_u8 *UserKey, cc_u32 keyLen) } /* - * @fn SDRM_RC4_PRNG - * @brief process stream data + * @fn SDRM_RC4_PRNG + * @brief process stream data * - * @param ctx [in]crypto context - * @param in [in]plaintext - * @param inLen [in]byte-length of in - * @param out [out]cipher text + * @param ctx [in]crypto context + * @param in [in]plaintext + * @param inLen [in]byte-length of in + * @param out [out]cipher text * - * @return CRYPTO_SUCCESS if no error is occured + * @return CRYPTO_SUCCESS if no error is occured */ int SDRM_RC4_PRNG(SDRM_RC4Context *ctx, cc_u8 *in, cc_u32 inLen, cc_u8 *out) { - cc_u32 i, j, k; - cc_u8 temp; + cc_u32 i, j, k; + cc_u8 temp; i = ctx->i; j = ctx->j; - for (k = 0; k < inLen; k++) - { + for (k = 0; k < inLen; k++) { i++; i &= 0xff; j += ctx->s[i]; @@ -160,7 +153,7 @@ int SDRM_RC4_PRNG(SDRM_RC4Context *ctx, cc_u8 *in, cc_u32 inLen, cc_u8 *out) temp = ctx->s[i] + ctx->s[j]; - out[k] = in[k]^(ctx->s[temp]); + out[k] = in[k] ^ (ctx->s[temp]); } ctx->i = i; @@ -169,4 +162,4 @@ int SDRM_RC4_PRNG(SDRM_RC4Context *ctx, cc_u8 *in, cc_u32 inLen, cc_u8 *out) return CRYPTO_SUCCESS; } -/***************************** End of File *****************************/ \ No newline at end of file +/***************************** End of File *****************************/ diff --git a/ssflib/dep/cryptocore/source/base/cc_sha1.c b/ssflib/dep/cryptocore/source/base/cc_sha1.c index 33bd721..8f3de28 100644 --- a/ssflib/dep/cryptocore/source/base/cc_sha1.c +++ b/ssflib/dep/cryptocore/source/base/cc_sha1.c @@ -19,11 +19,11 @@ /* SHA: NIST's Secure Hash Algorithm */ -/* This version written November 2000 by David Ireland of - DI Management Services Pty Limited +/* This version written November 2000 by David Ireland of + DI Management Services Pty Limited - Adapted from code in the Python Cryptography Toolkit, - version 1.0.0 by A.M. Kuchling 1995. + Adapted from code in the Python Cryptography Toolkit, + version 1.0.0 by A.M. Kuchling 1995. */ /* AM Kuchling's posting:- @@ -46,13 +46,13 @@ effort (for example the reengineering of a great many Capstone chips). */ /* JS Park's posting: - Modification for naming confilct. - - Attach prefix 'SDRM_SHA1_' for all function and constants. - - Change name of data context to 'SDRM_SHA1Context' - endianTest code is modified to avoid gcc warning. - Primitive data types are used, instead of user-defined data types. - Prototypes are moved to header file. - Not using functions are commented out. + Modification for naming confilct. + - Attach prefix 'SDRM_SHA1_' for all function and constants. + - Change name of data context to 'SDRM_SHA1Context' + endianTest code is modified to avoid gcc warning. + Primitive data types are used, instead of user-defined data types. + Prototypes are moved to header file. + Not using functions are commented out. */ @@ -62,7 +62,8 @@ void SDRM_endianTest(int *endianness); #include "cc_sha1.h" -static void SDRM_SHAtoByte(unsigned char *output, unsigned int *input, unsigned int len); +static void SDRM_SHAtoByte(unsigned char *output, unsigned int *input, + unsigned int len); /* The SHS block size and message digest sizes, in bytes */ @@ -74,12 +75,12 @@ static void SDRM_SHAtoByte(unsigned char *output, unsigned int *input, unsigned save one boolean operation each - thanks to Rich Schroeppel, rcs@cs.arizona.edu for discovering this */ -/*#define SDRM_SHA1_f1(x,y,z) ((x & y) | (~x & z)) // Rounds 0-19 */ -#define SDRM_SHA1_f1(x,y,z) ((z) ^ ((x) & ((y) ^ (z)))) /* Rounds 0-19 */ -#define SDRM_SHA1_f2(x,y,z) ((x) ^ (y) ^ (z)) /* Rounds 20-39 */ -/*#define SDRM_SHA1_f3(x,y,z) ((x & y) | (x & z) | (y & z)) // Rounds 40-59 */ -#define SDRM_SHA1_f3(x,y,z) (((x) & (y)) | ((z) & ((x) | (y)))) /* Rounds 40-59 */ -#define SDRM_SHA1_f4(x,y,z) ((x) ^ (y) ^ (z)) /* Rounds 60-79 */ +/*#define SDRM_SHA1_f1(x,y,z) ((x & y) | (~x & z)) // Rounds 0-19 */ +#define SDRM_SHA1_f1(x, y, z) ((z) ^ ((x) & ((y) ^ (z)))) /* Rounds 0-19 */ +#define SDRM_SHA1_f2(x, y, z) ((x) ^ (y) ^ (z)) /* Rounds 20-39 */ +/*#define SDRM_SHA1_f3(x,y,z) ((x & y) | (x & z) | (y & z)) // Rounds 40-59 */ +#define SDRM_SHA1_f3(x, y, z) (((x) & (y)) | ((z) & ((x) | (y)))) /* Rounds 40-59 */ +#define SDRM_SHA1_f4(x, y, z) ((x) ^ (y) ^ (z)) /* Rounds 60-79 */ /* The SHS Mysterious Constants */ @@ -106,7 +107,7 @@ static void SDRM_SHAtoByte(unsigned char *output, unsigned int *input, unsigned 80-UINT2 expanded input array W, where the first 16 are copies of the input data, and the remaining 64 are defined by - W[ i ] = W[ i - 16 ] ^ W[ i - 14 ] ^ W[ i - 8 ] ^ W[ i - 3 ] + W[i] = W[i - 16] ^ W[i - 14] ^ W[i - 8] ^ W[i - 3] This implementation generates these values on the fly in a circular buffer - thanks to Colin Plumb, colin@nyx10.cs.du.edu for this @@ -117,39 +118,39 @@ static void SDRM_SHAtoByte(unsigned char *output, unsigned int *input, unsigned for this information */ #define SDRM_SHA1_expand(W, i) (W[(i) & 15] = SDRM_SHA1_ROTL(1, (W[(i) & 15] ^ W[((i) - 14) & 15] ^ \ - W[((i) - 8) & 15] ^ W[((i) - 3) & 15]))) + W[((i) - 8) & 15] ^ W[((i) - 3) & 15]))) /* The prototype SHS sub-round. The fundamental sub-round is: - a' = e + ROTL( 5, a ) + f( b, c, d ) + k + data; - b' = a; - c' = ROTL( 30, b ); - d' = c; - e' = d; + a' = e + ROTL(5, a) + f(b, c, d) + k + data; + b' = a; + c' = ROTL(30, b); + d' = c; + e' = d; but this is implemented by unrolling the loop 5 times and renaming the - variables ( e, a, b, c, d ) = ( a', b', c', d', e' ) each iteration. + variables (e, a, b, c, d) = (a', b', c', d', e') each iteration. This code is then replicated 20 times for each of the 4 functions, using the next 20 values from the W[] array each time */ #define SDRM_SHA1_subRound(a, b, c, d, e, f, k, data) \ - (e += SDRM_SHA1_ROTL(5, a) + f(b, c, d) + (k) + (data), b = SDRM_SHA1_ROTL(30, b)) + (e += SDRM_SHA1_ROTL(5, a) + f(b, c, d) + (k) + (data), b = SDRM_SHA1_ROTL(30, b)) /* Initialize the SHS values */ void SDRM_SHA1_Init(SDRM_SHA1Context *shsInfo) { - SDRM_endianTest(&shsInfo->Endianness); - /* Set the h-vars to their initial values */ - shsInfo->digest[ 0 ] = SDRM_SHA1_h0init; - shsInfo->digest[ 1 ] = SDRM_SHA1_h1init; - shsInfo->digest[ 2 ] = SDRM_SHA1_h2init; - shsInfo->digest[ 3 ] = SDRM_SHA1_h3init; - shsInfo->digest[ 4 ] = SDRM_SHA1_h4init; - - /* Initialise bit count */ - shsInfo->countLo = shsInfo->countHi = 0; + SDRM_endianTest(&shsInfo->Endianness); + /* Set the h-vars to their initial values */ + shsInfo->digest[0] = SDRM_SHA1_h0init; + shsInfo->digest[1] = SDRM_SHA1_h1init; + shsInfo->digest[2] = SDRM_SHA1_h2init; + shsInfo->digest[3] = SDRM_SHA1_h3init; + shsInfo->digest[4] = SDRM_SHA1_h4init; + + /* Initialise bit count */ + shsInfo->countLo = shsInfo->countHi = 0; } @@ -160,243 +161,305 @@ void SDRM_SHA1_Init(SDRM_SHA1Context *shsInfo) Note that this corrupts the shsInfo->data area */ -static void SDRM_SHSTransform(unsigned int *digest, unsigned int *data ) - { - unsigned int A, B, C, D, E; /* Local vars */ - unsigned int eData[ 16 ]; /* Expanded data */ - - /* Set up first buffer and local data buffer */ - A = digest[ 0 ]; - B = digest[ 1 ]; - C = digest[ 2 ]; - D = digest[ 3 ]; - E = digest[ 4 ]; - memcpy( (unsigned char*)eData, (unsigned char*)data, SDRM_SHA1_DATASIZE ); - - /* Heavy mangling, in 4 sub-rounds of 20 interations each. */ - SDRM_SHA1_subRound( A, B, C, D, E, SDRM_SHA1_f1, SDRM_SHA1_K1, eData[ 0 ] ); - SDRM_SHA1_subRound( E, A, B, C, D, SDRM_SHA1_f1, SDRM_SHA1_K1, eData[ 1 ] ); - SDRM_SHA1_subRound( D, E, A, B, C, SDRM_SHA1_f1, SDRM_SHA1_K1, eData[ 2 ] ); - SDRM_SHA1_subRound( C, D, E, A, B, SDRM_SHA1_f1, SDRM_SHA1_K1, eData[ 3 ] ); - SDRM_SHA1_subRound( B, C, D, E, A, SDRM_SHA1_f1, SDRM_SHA1_K1, eData[ 4 ] ); - SDRM_SHA1_subRound( A, B, C, D, E, SDRM_SHA1_f1, SDRM_SHA1_K1, eData[ 5 ] ); - SDRM_SHA1_subRound( E, A, B, C, D, SDRM_SHA1_f1, SDRM_SHA1_K1, eData[ 6 ] ); - SDRM_SHA1_subRound( D, E, A, B, C, SDRM_SHA1_f1, SDRM_SHA1_K1, eData[ 7 ] ); - SDRM_SHA1_subRound( C, D, E, A, B, SDRM_SHA1_f1, SDRM_SHA1_K1, eData[ 8 ] ); - SDRM_SHA1_subRound( B, C, D, E, A, SDRM_SHA1_f1, SDRM_SHA1_K1, eData[ 9 ] ); - SDRM_SHA1_subRound( A, B, C, D, E, SDRM_SHA1_f1, SDRM_SHA1_K1, eData[ 10 ] ); - SDRM_SHA1_subRound( E, A, B, C, D, SDRM_SHA1_f1, SDRM_SHA1_K1, eData[ 11 ] ); - SDRM_SHA1_subRound( D, E, A, B, C, SDRM_SHA1_f1, SDRM_SHA1_K1, eData[ 12 ] ); - SDRM_SHA1_subRound( C, D, E, A, B, SDRM_SHA1_f1, SDRM_SHA1_K1, eData[ 13 ] ); - SDRM_SHA1_subRound( B, C, D, E, A, SDRM_SHA1_f1, SDRM_SHA1_K1, eData[ 14 ] ); - SDRM_SHA1_subRound( A, B, C, D, E, SDRM_SHA1_f1, SDRM_SHA1_K1, eData[ 15 ] ); - SDRM_SHA1_subRound( E, A, B, C, D, SDRM_SHA1_f1, SDRM_SHA1_K1, SDRM_SHA1_expand( eData, 16 ) ); - SDRM_SHA1_subRound( D, E, A, B, C, SDRM_SHA1_f1, SDRM_SHA1_K1, SDRM_SHA1_expand( eData, 17 ) ); - SDRM_SHA1_subRound( C, D, E, A, B, SDRM_SHA1_f1, SDRM_SHA1_K1, SDRM_SHA1_expand( eData, 18 ) ); - SDRM_SHA1_subRound( B, C, D, E, A, SDRM_SHA1_f1, SDRM_SHA1_K1, SDRM_SHA1_expand( eData, 19 ) ); - - SDRM_SHA1_subRound( A, B, C, D, E, SDRM_SHA1_f2, SDRM_SHA1_K2, SDRM_SHA1_expand( eData, 20 ) ); - SDRM_SHA1_subRound( E, A, B, C, D, SDRM_SHA1_f2, SDRM_SHA1_K2, SDRM_SHA1_expand( eData, 21 ) ); - SDRM_SHA1_subRound( D, E, A, B, C, SDRM_SHA1_f2, SDRM_SHA1_K2, SDRM_SHA1_expand( eData, 22 ) ); - SDRM_SHA1_subRound( C, D, E, A, B, SDRM_SHA1_f2, SDRM_SHA1_K2, SDRM_SHA1_expand( eData, 23 ) ); - SDRM_SHA1_subRound( B, C, D, E, A, SDRM_SHA1_f2, SDRM_SHA1_K2, SDRM_SHA1_expand( eData, 24 ) ); - SDRM_SHA1_subRound( A, B, C, D, E, SDRM_SHA1_f2, SDRM_SHA1_K2, SDRM_SHA1_expand( eData, 25 ) ); - SDRM_SHA1_subRound( E, A, B, C, D, SDRM_SHA1_f2, SDRM_SHA1_K2, SDRM_SHA1_expand( eData, 26 ) ); - SDRM_SHA1_subRound( D, E, A, B, C, SDRM_SHA1_f2, SDRM_SHA1_K2, SDRM_SHA1_expand( eData, 27 ) ); - SDRM_SHA1_subRound( C, D, E, A, B, SDRM_SHA1_f2, SDRM_SHA1_K2, SDRM_SHA1_expand( eData, 28 ) ); - SDRM_SHA1_subRound( B, C, D, E, A, SDRM_SHA1_f2, SDRM_SHA1_K2, SDRM_SHA1_expand( eData, 29 ) ); - SDRM_SHA1_subRound( A, B, C, D, E, SDRM_SHA1_f2, SDRM_SHA1_K2, SDRM_SHA1_expand( eData, 30 ) ); - SDRM_SHA1_subRound( E, A, B, C, D, SDRM_SHA1_f2, SDRM_SHA1_K2, SDRM_SHA1_expand( eData, 31 ) ); - SDRM_SHA1_subRound( D, E, A, B, C, SDRM_SHA1_f2, SDRM_SHA1_K2, SDRM_SHA1_expand( eData, 32 ) ); - SDRM_SHA1_subRound( C, D, E, A, B, SDRM_SHA1_f2, SDRM_SHA1_K2, SDRM_SHA1_expand( eData, 33 ) ); - SDRM_SHA1_subRound( B, C, D, E, A, SDRM_SHA1_f2, SDRM_SHA1_K2, SDRM_SHA1_expand( eData, 34 ) ); - SDRM_SHA1_subRound( A, B, C, D, E, SDRM_SHA1_f2, SDRM_SHA1_K2, SDRM_SHA1_expand( eData, 35 ) ); - SDRM_SHA1_subRound( E, A, B, C, D, SDRM_SHA1_f2, SDRM_SHA1_K2, SDRM_SHA1_expand( eData, 36 ) ); - SDRM_SHA1_subRound( D, E, A, B, C, SDRM_SHA1_f2, SDRM_SHA1_K2, SDRM_SHA1_expand( eData, 37 ) ); - SDRM_SHA1_subRound( C, D, E, A, B, SDRM_SHA1_f2, SDRM_SHA1_K2, SDRM_SHA1_expand( eData, 38 ) ); - SDRM_SHA1_subRound( B, C, D, E, A, SDRM_SHA1_f2, SDRM_SHA1_K2, SDRM_SHA1_expand( eData, 39 ) ); - - SDRM_SHA1_subRound( A, B, C, D, E, SDRM_SHA1_f3, SDRM_SHA1_K3, SDRM_SHA1_expand( eData, 40 ) ); - SDRM_SHA1_subRound( E, A, B, C, D, SDRM_SHA1_f3, SDRM_SHA1_K3, SDRM_SHA1_expand( eData, 41 ) ); - SDRM_SHA1_subRound( D, E, A, B, C, SDRM_SHA1_f3, SDRM_SHA1_K3, SDRM_SHA1_expand( eData, 42 ) ); - SDRM_SHA1_subRound( C, D, E, A, B, SDRM_SHA1_f3, SDRM_SHA1_K3, SDRM_SHA1_expand( eData, 43 ) ); - SDRM_SHA1_subRound( B, C, D, E, A, SDRM_SHA1_f3, SDRM_SHA1_K3, SDRM_SHA1_expand( eData, 44 ) ); - SDRM_SHA1_subRound( A, B, C, D, E, SDRM_SHA1_f3, SDRM_SHA1_K3, SDRM_SHA1_expand( eData, 45 ) ); - SDRM_SHA1_subRound( E, A, B, C, D, SDRM_SHA1_f3, SDRM_SHA1_K3, SDRM_SHA1_expand( eData, 46 ) ); - SDRM_SHA1_subRound( D, E, A, B, C, SDRM_SHA1_f3, SDRM_SHA1_K3, SDRM_SHA1_expand( eData, 47 ) ); - SDRM_SHA1_subRound( C, D, E, A, B, SDRM_SHA1_f3, SDRM_SHA1_K3, SDRM_SHA1_expand( eData, 48 ) ); - SDRM_SHA1_subRound( B, C, D, E, A, SDRM_SHA1_f3, SDRM_SHA1_K3, SDRM_SHA1_expand( eData, 49 ) ); - SDRM_SHA1_subRound( A, B, C, D, E, SDRM_SHA1_f3, SDRM_SHA1_K3, SDRM_SHA1_expand( eData, 50 ) ); - SDRM_SHA1_subRound( E, A, B, C, D, SDRM_SHA1_f3, SDRM_SHA1_K3, SDRM_SHA1_expand( eData, 51 ) ); - SDRM_SHA1_subRound( D, E, A, B, C, SDRM_SHA1_f3, SDRM_SHA1_K3, SDRM_SHA1_expand( eData, 52 ) ); - SDRM_SHA1_subRound( C, D, E, A, B, SDRM_SHA1_f3, SDRM_SHA1_K3, SDRM_SHA1_expand( eData, 53 ) ); - SDRM_SHA1_subRound( B, C, D, E, A, SDRM_SHA1_f3, SDRM_SHA1_K3, SDRM_SHA1_expand( eData, 54 ) ); - SDRM_SHA1_subRound( A, B, C, D, E, SDRM_SHA1_f3, SDRM_SHA1_K3, SDRM_SHA1_expand( eData, 55 ) ); - SDRM_SHA1_subRound( E, A, B, C, D, SDRM_SHA1_f3, SDRM_SHA1_K3, SDRM_SHA1_expand( eData, 56 ) ); - SDRM_SHA1_subRound( D, E, A, B, C, SDRM_SHA1_f3, SDRM_SHA1_K3, SDRM_SHA1_expand( eData, 57 ) ); - SDRM_SHA1_subRound( C, D, E, A, B, SDRM_SHA1_f3, SDRM_SHA1_K3, SDRM_SHA1_expand( eData, 58 ) ); - SDRM_SHA1_subRound( B, C, D, E, A, SDRM_SHA1_f3, SDRM_SHA1_K3, SDRM_SHA1_expand( eData, 59 ) ); - - SDRM_SHA1_subRound( A, B, C, D, E, SDRM_SHA1_f4, SDRM_SHA1_K4, SDRM_SHA1_expand( eData, 60 ) ); - SDRM_SHA1_subRound( E, A, B, C, D, SDRM_SHA1_f4, SDRM_SHA1_K4, SDRM_SHA1_expand( eData, 61 ) ); - SDRM_SHA1_subRound( D, E, A, B, C, SDRM_SHA1_f4, SDRM_SHA1_K4, SDRM_SHA1_expand( eData, 62 ) ); - SDRM_SHA1_subRound( C, D, E, A, B, SDRM_SHA1_f4, SDRM_SHA1_K4, SDRM_SHA1_expand( eData, 63 ) ); - SDRM_SHA1_subRound( B, C, D, E, A, SDRM_SHA1_f4, SDRM_SHA1_K4, SDRM_SHA1_expand( eData, 64 ) ); - SDRM_SHA1_subRound( A, B, C, D, E, SDRM_SHA1_f4, SDRM_SHA1_K4, SDRM_SHA1_expand( eData, 65 ) ); - SDRM_SHA1_subRound( E, A, B, C, D, SDRM_SHA1_f4, SDRM_SHA1_K4, SDRM_SHA1_expand( eData, 66 ) ); - SDRM_SHA1_subRound( D, E, A, B, C, SDRM_SHA1_f4, SDRM_SHA1_K4, SDRM_SHA1_expand( eData, 67 ) ); - SDRM_SHA1_subRound( C, D, E, A, B, SDRM_SHA1_f4, SDRM_SHA1_K4, SDRM_SHA1_expand( eData, 68 ) ); - SDRM_SHA1_subRound( B, C, D, E, A, SDRM_SHA1_f4, SDRM_SHA1_K4, SDRM_SHA1_expand( eData, 69 ) ); - SDRM_SHA1_subRound( A, B, C, D, E, SDRM_SHA1_f4, SDRM_SHA1_K4, SDRM_SHA1_expand( eData, 70 ) ); - SDRM_SHA1_subRound( E, A, B, C, D, SDRM_SHA1_f4, SDRM_SHA1_K4, SDRM_SHA1_expand( eData, 71 ) ); - SDRM_SHA1_subRound( D, E, A, B, C, SDRM_SHA1_f4, SDRM_SHA1_K4, SDRM_SHA1_expand( eData, 72 ) ); - SDRM_SHA1_subRound( C, D, E, A, B, SDRM_SHA1_f4, SDRM_SHA1_K4, SDRM_SHA1_expand( eData, 73 ) ); - SDRM_SHA1_subRound( B, C, D, E, A, SDRM_SHA1_f4, SDRM_SHA1_K4, SDRM_SHA1_expand( eData, 74 ) ); - SDRM_SHA1_subRound( A, B, C, D, E, SDRM_SHA1_f4, SDRM_SHA1_K4, SDRM_SHA1_expand( eData, 75 ) ); - SDRM_SHA1_subRound( E, A, B, C, D, SDRM_SHA1_f4, SDRM_SHA1_K4, SDRM_SHA1_expand( eData, 76 ) ); - SDRM_SHA1_subRound( D, E, A, B, C, SDRM_SHA1_f4, SDRM_SHA1_K4, SDRM_SHA1_expand( eData, 77 ) ); - SDRM_SHA1_subRound( C, D, E, A, B, SDRM_SHA1_f4, SDRM_SHA1_K4, SDRM_SHA1_expand( eData, 78 ) ); - SDRM_SHA1_subRound( B, C, D, E, A, SDRM_SHA1_f4, SDRM_SHA1_K4, SDRM_SHA1_expand( eData, 79 ) ); - - /* Build message digest */ - digest[ 0 ] += A; - digest[ 1 ] += B; - digest[ 2 ] += C; - digest[ 3 ] += D; - digest[ 4 ] += E; - } +static void SDRM_SHSTransform(unsigned int *digest, unsigned int *data) +{ + unsigned int A, B, C, D, E; /* Local vars */ + unsigned int eData[16]; /* Expanded data */ + + /* Set up first buffer and local data buffer */ + A = digest[0]; + B = digest[1]; + C = digest[2]; + D = digest[3]; + E = digest[4]; + memcpy((unsigned char *)eData, (unsigned char *)data, SDRM_SHA1_DATASIZE); + + /* Heavy mangling, in 4 sub-rounds of 20 interations each. */ + SDRM_SHA1_subRound(A, B, C, D, E, SDRM_SHA1_f1, SDRM_SHA1_K1, eData[0]); + SDRM_SHA1_subRound(E, A, B, C, D, SDRM_SHA1_f1, SDRM_SHA1_K1, eData[1]); + SDRM_SHA1_subRound(D, E, A, B, C, SDRM_SHA1_f1, SDRM_SHA1_K1, eData[2]); + SDRM_SHA1_subRound(C, D, E, A, B, SDRM_SHA1_f1, SDRM_SHA1_K1, eData[3]); + SDRM_SHA1_subRound(B, C, D, E, A, SDRM_SHA1_f1, SDRM_SHA1_K1, eData[4]); + SDRM_SHA1_subRound(A, B, C, D, E, SDRM_SHA1_f1, SDRM_SHA1_K1, eData[5]); + SDRM_SHA1_subRound(E, A, B, C, D, SDRM_SHA1_f1, SDRM_SHA1_K1, eData[6]); + SDRM_SHA1_subRound(D, E, A, B, C, SDRM_SHA1_f1, SDRM_SHA1_K1, eData[7]); + SDRM_SHA1_subRound(C, D, E, A, B, SDRM_SHA1_f1, SDRM_SHA1_K1, eData[8]); + SDRM_SHA1_subRound(B, C, D, E, A, SDRM_SHA1_f1, SDRM_SHA1_K1, eData[9]); + SDRM_SHA1_subRound(A, B, C, D, E, SDRM_SHA1_f1, SDRM_SHA1_K1, eData[10]); + SDRM_SHA1_subRound(E, A, B, C, D, SDRM_SHA1_f1, SDRM_SHA1_K1, eData[11]); + SDRM_SHA1_subRound(D, E, A, B, C, SDRM_SHA1_f1, SDRM_SHA1_K1, eData[12]); + SDRM_SHA1_subRound(C, D, E, A, B, SDRM_SHA1_f1, SDRM_SHA1_K1, eData[13]); + SDRM_SHA1_subRound(B, C, D, E, A, SDRM_SHA1_f1, SDRM_SHA1_K1, eData[14]); + SDRM_SHA1_subRound(A, B, C, D, E, SDRM_SHA1_f1, SDRM_SHA1_K1, eData[15]); + SDRM_SHA1_subRound(E, A, B, C, D, SDRM_SHA1_f1, SDRM_SHA1_K1, + SDRM_SHA1_expand(eData, 16)); + SDRM_SHA1_subRound(D, E, A, B, C, SDRM_SHA1_f1, SDRM_SHA1_K1, + SDRM_SHA1_expand(eData, 17)); + SDRM_SHA1_subRound(C, D, E, A, B, SDRM_SHA1_f1, SDRM_SHA1_K1, + SDRM_SHA1_expand(eData, 18)); + SDRM_SHA1_subRound(B, C, D, E, A, SDRM_SHA1_f1, SDRM_SHA1_K1, + SDRM_SHA1_expand(eData, 19)); + + SDRM_SHA1_subRound(A, B, C, D, E, SDRM_SHA1_f2, SDRM_SHA1_K2, + SDRM_SHA1_expand(eData, 20)); + SDRM_SHA1_subRound(E, A, B, C, D, SDRM_SHA1_f2, SDRM_SHA1_K2, + SDRM_SHA1_expand(eData, 21)); + SDRM_SHA1_subRound(D, E, A, B, C, SDRM_SHA1_f2, SDRM_SHA1_K2, + SDRM_SHA1_expand(eData, 22)); + SDRM_SHA1_subRound(C, D, E, A, B, SDRM_SHA1_f2, SDRM_SHA1_K2, + SDRM_SHA1_expand(eData, 23)); + SDRM_SHA1_subRound(B, C, D, E, A, SDRM_SHA1_f2, SDRM_SHA1_K2, + SDRM_SHA1_expand(eData, 24)); + SDRM_SHA1_subRound(A, B, C, D, E, SDRM_SHA1_f2, SDRM_SHA1_K2, + SDRM_SHA1_expand(eData, 25)); + SDRM_SHA1_subRound(E, A, B, C, D, SDRM_SHA1_f2, SDRM_SHA1_K2, + SDRM_SHA1_expand(eData, 26)); + SDRM_SHA1_subRound(D, E, A, B, C, SDRM_SHA1_f2, SDRM_SHA1_K2, + SDRM_SHA1_expand(eData, 27)); + SDRM_SHA1_subRound(C, D, E, A, B, SDRM_SHA1_f2, SDRM_SHA1_K2, + SDRM_SHA1_expand(eData, 28)); + SDRM_SHA1_subRound(B, C, D, E, A, SDRM_SHA1_f2, SDRM_SHA1_K2, + SDRM_SHA1_expand(eData, 29)); + SDRM_SHA1_subRound(A, B, C, D, E, SDRM_SHA1_f2, SDRM_SHA1_K2, + SDRM_SHA1_expand(eData, 30)); + SDRM_SHA1_subRound(E, A, B, C, D, SDRM_SHA1_f2, SDRM_SHA1_K2, + SDRM_SHA1_expand(eData, 31)); + SDRM_SHA1_subRound(D, E, A, B, C, SDRM_SHA1_f2, SDRM_SHA1_K2, + SDRM_SHA1_expand(eData, 32)); + SDRM_SHA1_subRound(C, D, E, A, B, SDRM_SHA1_f2, SDRM_SHA1_K2, + SDRM_SHA1_expand(eData, 33)); + SDRM_SHA1_subRound(B, C, D, E, A, SDRM_SHA1_f2, SDRM_SHA1_K2, + SDRM_SHA1_expand(eData, 34)); + SDRM_SHA1_subRound(A, B, C, D, E, SDRM_SHA1_f2, SDRM_SHA1_K2, + SDRM_SHA1_expand(eData, 35)); + SDRM_SHA1_subRound(E, A, B, C, D, SDRM_SHA1_f2, SDRM_SHA1_K2, + SDRM_SHA1_expand(eData, 36)); + SDRM_SHA1_subRound(D, E, A, B, C, SDRM_SHA1_f2, SDRM_SHA1_K2, + SDRM_SHA1_expand(eData, 37)); + SDRM_SHA1_subRound(C, D, E, A, B, SDRM_SHA1_f2, SDRM_SHA1_K2, + SDRM_SHA1_expand(eData, 38)); + SDRM_SHA1_subRound(B, C, D, E, A, SDRM_SHA1_f2, SDRM_SHA1_K2, + SDRM_SHA1_expand(eData, 39)); + + SDRM_SHA1_subRound(A, B, C, D, E, SDRM_SHA1_f3, SDRM_SHA1_K3, + SDRM_SHA1_expand(eData, 40)); + SDRM_SHA1_subRound(E, A, B, C, D, SDRM_SHA1_f3, SDRM_SHA1_K3, + SDRM_SHA1_expand(eData, 41)); + SDRM_SHA1_subRound(D, E, A, B, C, SDRM_SHA1_f3, SDRM_SHA1_K3, + SDRM_SHA1_expand(eData, 42)); + SDRM_SHA1_subRound(C, D, E, A, B, SDRM_SHA1_f3, SDRM_SHA1_K3, + SDRM_SHA1_expand(eData, 43)); + SDRM_SHA1_subRound(B, C, D, E, A, SDRM_SHA1_f3, SDRM_SHA1_K3, + SDRM_SHA1_expand(eData, 44)); + SDRM_SHA1_subRound(A, B, C, D, E, SDRM_SHA1_f3, SDRM_SHA1_K3, + SDRM_SHA1_expand(eData, 45)); + SDRM_SHA1_subRound(E, A, B, C, D, SDRM_SHA1_f3, SDRM_SHA1_K3, + SDRM_SHA1_expand(eData, 46)); + SDRM_SHA1_subRound(D, E, A, B, C, SDRM_SHA1_f3, SDRM_SHA1_K3, + SDRM_SHA1_expand(eData, 47)); + SDRM_SHA1_subRound(C, D, E, A, B, SDRM_SHA1_f3, SDRM_SHA1_K3, + SDRM_SHA1_expand(eData, 48)); + SDRM_SHA1_subRound(B, C, D, E, A, SDRM_SHA1_f3, SDRM_SHA1_K3, + SDRM_SHA1_expand(eData, 49)); + SDRM_SHA1_subRound(A, B, C, D, E, SDRM_SHA1_f3, SDRM_SHA1_K3, + SDRM_SHA1_expand(eData, 50)); + SDRM_SHA1_subRound(E, A, B, C, D, SDRM_SHA1_f3, SDRM_SHA1_K3, + SDRM_SHA1_expand(eData, 51)); + SDRM_SHA1_subRound(D, E, A, B, C, SDRM_SHA1_f3, SDRM_SHA1_K3, + SDRM_SHA1_expand(eData, 52)); + SDRM_SHA1_subRound(C, D, E, A, B, SDRM_SHA1_f3, SDRM_SHA1_K3, + SDRM_SHA1_expand(eData, 53)); + SDRM_SHA1_subRound(B, C, D, E, A, SDRM_SHA1_f3, SDRM_SHA1_K3, + SDRM_SHA1_expand(eData, 54)); + SDRM_SHA1_subRound(A, B, C, D, E, SDRM_SHA1_f3, SDRM_SHA1_K3, + SDRM_SHA1_expand(eData, 55)); + SDRM_SHA1_subRound(E, A, B, C, D, SDRM_SHA1_f3, SDRM_SHA1_K3, + SDRM_SHA1_expand(eData, 56)); + SDRM_SHA1_subRound(D, E, A, B, C, SDRM_SHA1_f3, SDRM_SHA1_K3, + SDRM_SHA1_expand(eData, 57)); + SDRM_SHA1_subRound(C, D, E, A, B, SDRM_SHA1_f3, SDRM_SHA1_K3, + SDRM_SHA1_expand(eData, 58)); + SDRM_SHA1_subRound(B, C, D, E, A, SDRM_SHA1_f3, SDRM_SHA1_K3, + SDRM_SHA1_expand(eData, 59)); + + SDRM_SHA1_subRound(A, B, C, D, E, SDRM_SHA1_f4, SDRM_SHA1_K4, + SDRM_SHA1_expand(eData, 60)); + SDRM_SHA1_subRound(E, A, B, C, D, SDRM_SHA1_f4, SDRM_SHA1_K4, + SDRM_SHA1_expand(eData, 61)); + SDRM_SHA1_subRound(D, E, A, B, C, SDRM_SHA1_f4, SDRM_SHA1_K4, + SDRM_SHA1_expand(eData, 62)); + SDRM_SHA1_subRound(C, D, E, A, B, SDRM_SHA1_f4, SDRM_SHA1_K4, + SDRM_SHA1_expand(eData, 63)); + SDRM_SHA1_subRound(B, C, D, E, A, SDRM_SHA1_f4, SDRM_SHA1_K4, + SDRM_SHA1_expand(eData, 64)); + SDRM_SHA1_subRound(A, B, C, D, E, SDRM_SHA1_f4, SDRM_SHA1_K4, + SDRM_SHA1_expand(eData, 65)); + SDRM_SHA1_subRound(E, A, B, C, D, SDRM_SHA1_f4, SDRM_SHA1_K4, + SDRM_SHA1_expand(eData, 66)); + SDRM_SHA1_subRound(D, E, A, B, C, SDRM_SHA1_f4, SDRM_SHA1_K4, + SDRM_SHA1_expand(eData, 67)); + SDRM_SHA1_subRound(C, D, E, A, B, SDRM_SHA1_f4, SDRM_SHA1_K4, + SDRM_SHA1_expand(eData, 68)); + SDRM_SHA1_subRound(B, C, D, E, A, SDRM_SHA1_f4, SDRM_SHA1_K4, + SDRM_SHA1_expand(eData, 69)); + SDRM_SHA1_subRound(A, B, C, D, E, SDRM_SHA1_f4, SDRM_SHA1_K4, + SDRM_SHA1_expand(eData, 70)); + SDRM_SHA1_subRound(E, A, B, C, D, SDRM_SHA1_f4, SDRM_SHA1_K4, + SDRM_SHA1_expand(eData, 71)); + SDRM_SHA1_subRound(D, E, A, B, C, SDRM_SHA1_f4, SDRM_SHA1_K4, + SDRM_SHA1_expand(eData, 72)); + SDRM_SHA1_subRound(C, D, E, A, B, SDRM_SHA1_f4, SDRM_SHA1_K4, + SDRM_SHA1_expand(eData, 73)); + SDRM_SHA1_subRound(B, C, D, E, A, SDRM_SHA1_f4, SDRM_SHA1_K4, + SDRM_SHA1_expand(eData, 74)); + SDRM_SHA1_subRound(A, B, C, D, E, SDRM_SHA1_f4, SDRM_SHA1_K4, + SDRM_SHA1_expand(eData, 75)); + SDRM_SHA1_subRound(E, A, B, C, D, SDRM_SHA1_f4, SDRM_SHA1_K4, + SDRM_SHA1_expand(eData, 76)); + SDRM_SHA1_subRound(D, E, A, B, C, SDRM_SHA1_f4, SDRM_SHA1_K4, + SDRM_SHA1_expand(eData, 77)); + SDRM_SHA1_subRound(C, D, E, A, B, SDRM_SHA1_f4, SDRM_SHA1_K4, + SDRM_SHA1_expand(eData, 78)); + SDRM_SHA1_subRound(B, C, D, E, A, SDRM_SHA1_f4, SDRM_SHA1_K4, + SDRM_SHA1_expand(eData, 79)); + + /* Build message digest */ + digest[0] += A; + digest[1] += B; + digest[2] += C; + digest[3] += D; + digest[4] += E; +} /* When run on a little-endian CPU we need to perform byte reversal on an array of long words. */ -static void SDRM_longReverse(unsigned int *buffer, int byteCount, int Endianness) +static void SDRM_longReverse(unsigned int *buffer, int byteCount, + int Endianness) { - unsigned int value; + unsigned int value; - if (Endianness == !(0)) { + if (Endianness == !(0)) return; - } - byteCount /= sizeof( unsigned int ); - while(byteCount--) - { - value = *buffer; - value = ((value & 0xFF00FF00L) >> 8) | \ - ((value & 0x00FF00FFL ) << 8); - *buffer++ = (value << 16) | (value >> 16); + + byteCount /= sizeof(unsigned int); + + while (byteCount--) { + value = *buffer; + value = ((value & 0xFF00FF00L) >> 8) | \ + ((value & 0x00FF00FFL) << 8); + *buffer++ = (value << 16) | (value >> 16); } } /* Update SHS for a block of data */ -void SDRM_SHA1_Update(SDRM_SHA1Context *shsInfo, const unsigned char *buffer, int count) +void SDRM_SHA1_Update(SDRM_SHA1Context *shsInfo, const unsigned char *buffer, + int count) { - unsigned int tmp; - int dataCount; + unsigned int tmp; + int dataCount; + + /* Update bitcount */ + tmp = shsInfo->countLo; - /* Update bitcount */ - tmp = shsInfo->countLo; - if ((shsInfo->countLo = tmp + ((unsigned int)count << 3)) < tmp) { + if ((shsInfo->countLo = tmp + ((unsigned int)count << 3)) < tmp) shsInfo->countHi++; /* Carry from low to high */ + + shsInfo->countHi += count >> 29; + + /* Get count of bytes already in data */ + dataCount = (int)(tmp >> 3) & 0x3F; + + /* Handle any leading odd-sized chunks */ + if (dataCount) { + unsigned char *p = (unsigned char *) shsInfo->data + dataCount; + + dataCount = SDRM_SHA1_DATASIZE - dataCount; + + if (count < dataCount) { + memcpy(p, buffer, count); + return; + } + + memcpy(p, buffer, dataCount); + SDRM_longReverse(shsInfo->data, SDRM_SHA1_DATASIZE, shsInfo->Endianness); + SDRM_SHSTransform(shsInfo->digest, shsInfo->data); + buffer += dataCount; + count -= dataCount; } - shsInfo->countHi += count >> 29; - - /* Get count of bytes already in data */ - dataCount = (int)(tmp >> 3) & 0x3F; - - /* Handle any leading odd-sized chunks */ - if (dataCount) - { - unsigned char *p = (unsigned char*) shsInfo->data + dataCount; - - dataCount = SDRM_SHA1_DATASIZE - dataCount; - if(count < dataCount) - { - memcpy(p, buffer, count); - return; - } - memcpy(p, buffer, dataCount); - SDRM_longReverse(shsInfo->data, SDRM_SHA1_DATASIZE, shsInfo->Endianness); - SDRM_SHSTransform(shsInfo->digest, shsInfo->data); - buffer += dataCount; - count -= dataCount; - } - - /* Process data in SHS_DATASIZE chunks */ - while(count >= SDRM_SHA1_DATASIZE) - { - memcpy((unsigned char*)shsInfo->data, buffer, SDRM_SHA1_DATASIZE); - SDRM_longReverse(shsInfo->data, SDRM_SHA1_DATASIZE, shsInfo->Endianness); - SDRM_SHSTransform(shsInfo->digest, shsInfo->data); - buffer += SDRM_SHA1_DATASIZE; - count -= SDRM_SHA1_DATASIZE; - } - - /* Handle any remaining bytes of data. */ - memcpy( (unsigned char*)shsInfo->data, buffer, count); - } + /* Process data in SHS_DATASIZE chunks */ + while (count >= SDRM_SHA1_DATASIZE) { + memcpy((unsigned char *)shsInfo->data, buffer, SDRM_SHA1_DATASIZE); + SDRM_longReverse(shsInfo->data, SDRM_SHA1_DATASIZE, shsInfo->Endianness); + SDRM_SHSTransform(shsInfo->digest, shsInfo->data); + buffer += SDRM_SHA1_DATASIZE; + count -= SDRM_SHA1_DATASIZE; + } + + /* Handle any remaining bytes of data. */ + memcpy((unsigned char *)shsInfo->data, buffer, count); +} /* Final wrapup - pad to SHS_DATASIZE-byte boundary with the bit pattern 1 0* (64-bit count of bits processed, MSB-first) */ void SDRM_SHA1_Final(SDRM_SHA1Context *shsInfo, unsigned char *output) { - int count; - unsigned char *dataPtr; - - /* Compute number of bytes mod 64 */ - count = (int) shsInfo->countLo; - count = (count >> 3) & 0x3F; - - /* Set the first char of padding to 0x80. This is safe since there is - always at least one byte free */ - dataPtr = (unsigned char*) shsInfo->data + count; - *dataPtr++ = 0x80; - - /* Bytes of padding needed to make 64 bytes */ - count = SDRM_SHA1_DATASIZE - 1 - count; - - /* Pad out to 56 mod 64 */ - if( count < 8 ) - { - /* Two lots of padding: Pad the first block to 64 bytes */ - memset(dataPtr, 0, count); - SDRM_longReverse(shsInfo->data, SDRM_SHA1_DATASIZE, shsInfo->Endianness); - SDRM_SHSTransform(shsInfo->digest, shsInfo->data); - - /* Now fill the next block with 56 bytes */ - memset((unsigned char*)shsInfo->data, 0, SDRM_SHA1_DATASIZE - 8); - } - else - /* Pad block to 56 bytes */ - { - memset(dataPtr, 0, count - 8); - } + int count; + unsigned char *dataPtr; + + /* Compute number of bytes mod 64 */ + count = (int) shsInfo->countLo; + count = (count >> 3) & 0x3F; + + /* Set the first char of padding to 0x80. This is safe since there is + always at least one byte free */ + dataPtr = (unsigned char *) shsInfo->data + count; + *dataPtr++ = 0x80; - /* Append length in bits and transform */ - shsInfo->data[14] = shsInfo->countHi; - shsInfo->data[15] = shsInfo->countLo; + /* Bytes of padding needed to make 64 bytes */ + count = SDRM_SHA1_DATASIZE - 1 - count; - SDRM_longReverse(shsInfo->data, SDRM_SHA1_DATASIZE - 8, shsInfo->Endianness); - SDRM_SHSTransform(shsInfo->digest, shsInfo->data); + /* Pad out to 56 mod 64 */ + if (count < 8) { + /* Two lots of padding: Pad the first block to 64 bytes */ + memset(dataPtr, 0, count); + SDRM_longReverse(shsInfo->data, SDRM_SHA1_DATASIZE, shsInfo->Endianness); + SDRM_SHSTransform(shsInfo->digest, shsInfo->data); + + /* Now fill the next block with 56 bytes */ + memset((unsigned char *)shsInfo->data, 0, SDRM_SHA1_DATASIZE - 8); + } else + /* Pad block to 56 bytes */ + memset(dataPtr, 0, count - 8); + + /* Append length in bits and transform */ + shsInfo->data[14] = shsInfo->countHi; + shsInfo->data[15] = shsInfo->countLo; + + SDRM_longReverse(shsInfo->data, SDRM_SHA1_DATASIZE - 8, shsInfo->Endianness); + SDRM_SHSTransform(shsInfo->digest, shsInfo->data); /* Output to an array of bytes */ SDRM_SHAtoByte(output, shsInfo->digest, SDRM_SHA1_DIGESTSIZE); /* Zeroise sensitive stuff */ - memset((unsigned char*)shsInfo, 0, sizeof(SDRM_SHA1Context)); + memset((unsigned char *)shsInfo, 0, sizeof(SDRM_SHA1Context)); } -static void SDRM_SHAtoByte(unsigned char *output, unsigned int *input, unsigned int len) -{ /* Output SHA digest in byte array */ +static void SDRM_SHAtoByte(unsigned char *output, unsigned int *input, + unsigned int len) +{ + /* Output SHA digest in byte array */ unsigned int i, j; - for(i = 0, j = 0; j < len; i++, j += 4) - { - output[j+3] = (unsigned char)( input[i] & 0xff); - output[j+2] = (unsigned char)((input[i] >> 8 ) & 0xff); - output[j+1] = (unsigned char)((input[i] >> 16) & 0xff); - output[j ] = (unsigned char)((input[i] >> 24) & 0xff); + for (i = 0, j = 0; j < len; i++, j += 4) { + output[j + 3] = (unsigned char)(input[i] & 0xff); + output[j + 2] = (unsigned char)((input[i] >> 8) & 0xff); + output[j + 1] = (unsigned char)((input[i] >> 16) & 0xff); + output[j] = (unsigned char)((input[i] >> 24) & 0xff); } } @@ -404,7 +467,7 @@ static void SDRM_SHAtoByte(unsigned char *output, unsigned int *input, unsigned //unsigned char digest[20]; //unsigned char message[3] = {'a', 'b', 'c' }; //unsigned char *mess56 = -// "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"; +// "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"; /* Correct solutions from FIPS PUB 180-1 */ //char *dig1 = "A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D"; @@ -422,71 +485,68 @@ static void SDRM_SHAtoByte(unsigned char *output, unsigned int *input, unsigned //main() //{ -// SHA_CTX sha; -// int i; -// BYTE big[1000]; +// SHA_CTX sha; +// int i; +// BYTE big[1000]; // -// SHAInit(&sha); -// SHAUpdate(&sha, message, 3); -// SHAFinal(digest, &sha); +// SHAInit(&sha); +// SHAUpdate(&sha, message, 3); +// SHAFinal(digest, &sha); // -// for (i = 0; i < 20; i++) -// { -// if ((i % 4) == 0) printf(" "); -// printf("%02x", digest[i]); -// } -// printf("\n"); -// printf(" %s <= correct\n", dig1); +// for (i = 0; i < 20; i++) +// { +// if ((i % 4) == 0) printf(" "); +// printf("%02x", digest[i]); +// } +// printf("\n"); +// printf(" %s <= correct\n", dig1); // -// SHAInit(&sha); -// SHAUpdate(&sha, mess56, 56); -// SHAFinal(digest, &sha); +// SHAInit(&sha); +// SHAUpdate(&sha, mess56, 56); +// SHAFinal(digest, &sha); // -// for (i = 0; i < 20; i++) -// { -// if ((i % 4) == 0) printf(" "); -// printf("%02x", digest[i]); -// } -// printf("\n"); -// printf(" %s <= correct\n", dig2); +// for (i = 0; i < 20; i++) +// { +// if ((i % 4) == 0) printf(" "); +// printf("%02x", digest[i]); +// } +// printf("\n"); +// printf(" %s <= correct\n", dig2); // -// /* Fill up big array */ -// for (i = 0; i < 1000; i++) -// big[i] = 'a'; +// /* Fill up big array */ +// for (i = 0; i < 1000; i++) +// big[i] = 'a'; // -// SHAInit(&sha); -// /* Digest 1 million x 'a' */ -// for (i = 0; i < 1000; i++) -// SHAUpdate(&sha, big, 1000); -// SHAFinal(digest, &sha); +// SHAInit(&sha); +// /* Digest 1 million x 'a' */ +// for (i = 0; i < 1000; i++) +// SHAUpdate(&sha, big, 1000); +// SHAFinal(digest, &sha); // -// for (i = 0; i < 20; i++) -// { -// if ((i % 4) == 0) printf(" "); -// printf("%02x", digest[i]); -// } -// printf("\n"); -// printf(" %s <= correct\n", dig3); +// for (i = 0; i < 20; i++) +// { +// if ((i % 4) == 0) printf(" "); +// printf("%02x", digest[i]); +// } +// printf("\n"); +// printf(" %s <= correct\n", dig3); // -// return 0; +// return 0; //} /* endian.c */ void SDRM_endianTest(int *endian_ness) { - static short test = 1; + static short test = 1; - if ( *((char *) &test) != 1) - { + if (*((char *) &test) != 1) { /* printf("Big endian = no change\n"); */ *endian_ness = !(0); - } - else - { + } else { /* printf("Little endian = swap\n"); */ *endian_ness = 0; - } + } } /***************************** End of File *****************************/ diff --git a/ssflib/dep/cryptocore/source/base/cc_sha2.c b/ssflib/dep/cryptocore/source/base/cc_sha2.c index 75ad2a9..7411093 100644 --- a/ssflib/dep/cryptocore/source/base/cc_sha2.c +++ b/ssflib/dep/cryptocore/source/base/cc_sha2.c @@ -32,16 +32,16 @@ */ /* JS Park's posting: - Modification for naming confilct. - Attach prefix 'SDRM_' for all function and constants. - Change name of data context to 'SDRM_SHAxxxContext' (xxx is bit length of digest) + Modification for naming confilct. + Attach prefix 'SDRM_' for all function and constants. + Change name of data context to 'SDRM_SHAxxxContext' (xxx is bit length of digest) */ #include #include "cc_sha2.h" -#define SDRM_SHA2_SHFR(x, n) ((x) >> (n)) +#define SDRM_SHA2_SHFR(x, n) ((x) >> (n)) #define SDRM_SHA2_ROTR(x, n) (((x) >> (n)) | ((x) << ((sizeof(x) << 3) - (n)))) #define SDRM_SHA2_ROTL(x, n) (((x) << (n)) | ((x) >> ((sizeof(x) << 3) - (n)))) #define SDRM_SHA2_CH(x, y, z) (((x) & (y)) ^ (~(x) & (z))) @@ -57,220 +57,230 @@ #define SDRM_SHA2_SHA512_F3(x) (SDRM_SHA2_ROTR(x, 1) ^ SDRM_SHA2_ROTR(x, 8) ^ SDRM_SHA2_SHFR(x, 7)) #define SDRM_SHA2_SHA512_F4(x) (SDRM_SHA2_ROTR(x, 19) ^ SDRM_SHA2_ROTR(x, 61) ^ SDRM_SHA2_SHFR(x, 6)) -#define SDRM_SHA2_UNPACK32(x, str) \ -do { \ - *((str) + 3) = (cc_u8) ((x) ); \ - *((str) + 2) = (cc_u8) ((x) >> 8); \ - *((str) + 1) = (cc_u8) ((x) >> 16); \ - *((str) + 0) = (cc_u8) ((x) >> 24); \ -} while(0) - -#define SDRM_SHA2_PACK32(str, x) \ -do { \ - *(x) = ((cc_u32) *((str) + 3) ) \ - | ((cc_u32) *((str) + 2) << 8) \ - | ((cc_u32) *((str) + 1) << 16) \ - | ((cc_u32) *((str) + 0) << 24); \ -} while(0) - -#define SDRM_SHA2_UNPACK64(x, str) \ -do { \ - *((str) + 7) = (cc_u8) ((x) ); \ - *((str) + 6) = (cc_u8) ((x) >> 8); \ - *((str) + 5) = (cc_u8) ((x) >> 16); \ - *((str) + 4) = (cc_u8) ((x) >> 24); \ - *((str) + 3) = (cc_u8) ((x) >> 32); \ - *((str) + 2) = (cc_u8) ((x) >> 40); \ - *((str) + 1) = (cc_u8) ((x) >> 48); \ - *((str) + 0) = (cc_u8) ((x) >> 56); \ -} while(0) - -#define SDRM_SHA2_PACK64(str, x) \ -do { \ - *(x) = ((cc_u64) *((str) + 7) ) \ - | ((cc_u64) *((str) + 6) << 8) \ - | ((cc_u64) *((str) + 5) << 16) \ - | ((cc_u64) *((str) + 4) << 24) \ - | ((cc_u64) *((str) + 3) << 32) \ - | ((cc_u64) *((str) + 2) << 40) \ - | ((cc_u64) *((str) + 1) << 48) \ - | ((cc_u64) *((str) + 0) << 56); \ -} while(0) +#define SDRM_SHA2_UNPACK32(x, str) \ + do { \ + *((str) + 3) = (cc_u8) ((x)); \ + *((str) + 2) = (cc_u8) ((x) >> 8); \ + *((str) + 1) = (cc_u8) ((x) >> 16); \ + *((str) + 0) = (cc_u8) ((x) >> 24); \ + } while (0) + +#define SDRM_SHA2_PACK32(str, x) \ + do { \ + *(x) = ((cc_u32) *((str) + 3)) \ + | ((cc_u32) *((str) + 2) << 8) \ + | ((cc_u32) *((str) + 1) << 16) \ + | ((cc_u32) *((str) + 0) << 24); \ + } while (0) + +#define SDRM_SHA2_UNPACK64(x, str) \ + do { \ + *((str) + 7) = (cc_u8) ((x)); \ + *((str) + 6) = (cc_u8) ((x) >> 8); \ + *((str) + 5) = (cc_u8) ((x) >> 16); \ + *((str) + 4) = (cc_u8) ((x) >> 24); \ + *((str) + 3) = (cc_u8) ((x) >> 32); \ + *((str) + 2) = (cc_u8) ((x) >> 40); \ + *((str) + 1) = (cc_u8) ((x) >> 48); \ + *((str) + 0) = (cc_u8) ((x) >> 56); \ + } while (0) + +#define SDRM_SHA2_PACK64(str, x) \ + do { \ + *(x) = ((cc_u64) *((str) + 7)) \ + | ((cc_u64) *((str) + 6) << 8) \ + | ((cc_u64) *((str) + 5) << 16) \ + | ((cc_u64) *((str) + 4) << 24) \ + | ((cc_u64) *((str) + 3) << 32) \ + | ((cc_u64) *((str) + 2) << 40) \ + | ((cc_u64) *((str) + 1) << 48) \ + | ((cc_u64) *((str) + 0) << 56); \ + } while (0) /* Macros used for loops unrolling */ -#define SDRM_SHA2_SHA256_SCR(i) \ -{ \ - w[i] = SDRM_SHA2_SHA256_F4(w[(i) - 2]) + w[(i) - 7] \ - + SDRM_SHA2_SHA256_F3(w[(i) - 15]) + w[(i) - 16]; \ -} +#define SDRM_SHA2_SHA256_SCR(i) \ + { \ + w[i] = SDRM_SHA2_SHA256_F4(w[(i) - 2]) + w[(i) - 7] \ + + SDRM_SHA2_SHA256_F3(w[(i) - 15]) + w[(i) - 16]; \ + } -#define SDRM_SHA2_SHA512_SCR(i) \ -{ \ - w[i] = SDRM_SHA2_SHA512_F4(w[(i) - 2]) + w[(i) - 7] \ - + SDRM_SHA2_SHA512_F3(w[(i) - 15]) + w[(i) - 16]; \ -} +#define SDRM_SHA2_SHA512_SCR(i) \ + { \ + w[i] = SDRM_SHA2_SHA512_F4(w[(i) - 2]) + w[(i) - 7] \ + + SDRM_SHA2_SHA512_F3(w[(i) - 15]) + w[(i) - 16]; \ + } -#define SDRM_SHA2_SHA256_EXP(a, b, c, d, e, f, g, h, j) \ -{ \ - t1 = wv[h] + SDRM_SHA2_SHA256_F2(wv[e]) + SDRM_SHA2_CH(wv[e], wv[f], wv[g]) \ - + sha256_k[j] + w[j]; \ - t2 = SDRM_SHA2_SHA256_F1(wv[a]) + SDRM_SHA2_MAJ(wv[a], wv[b], wv[c]); \ - wv[d] += t1; \ - wv[h] = t1 + t2; \ -} +#define SDRM_SHA2_SHA256_EXP(a, b, c, d, e, f, g, h, j) \ + { \ + t1 = wv[h] + SDRM_SHA2_SHA256_F2(wv[e]) + SDRM_SHA2_CH(wv[e], wv[f], wv[g]) \ + + sha256_k[j] + w[j]; \ + t2 = SDRM_SHA2_SHA256_F1(wv[a]) + SDRM_SHA2_MAJ(wv[a], wv[b], wv[c]); \ + wv[d] += t1; \ + wv[h] = t1 + t2; \ + } -#define SDRM_SHA2_SHA512_EXP(a, b, c, d, e, f, g ,h, j) \ -{ \ - t1 = wv[h] + SDRM_SHA2_SHA512_F2(wv[e]) + SDRM_SHA2_CH(wv[e], wv[f], wv[g]) \ - + sha512_k[j] + w[j]; \ - t2 = SDRM_SHA2_SHA512_F1(wv[a]) + SDRM_SHA2_MAJ(wv[a], wv[b], wv[c]); \ - wv[d] += t1; \ - wv[h] = t1 + t2; \ -} +#define SDRM_SHA2_SHA512_EXP(a, b, c, d, e, f, g, h, j) \ + { \ + t1 = wv[h] + SDRM_SHA2_SHA512_F2(wv[e]) + SDRM_SHA2_CH(wv[e], wv[f], wv[g]) \ + + sha512_k[j] + w[j]; \ + t2 = SDRM_SHA2_SHA512_F1(wv[a]) + SDRM_SHA2_MAJ(wv[a], wv[b], wv[c]); \ + wv[d] += t1; \ + wv[h] = t1 + t2; \ + } -cc_u32 sha224_h0[8] = - {0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939, - 0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4}; - -cc_u32 sha256_h0[8] = - {0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, - 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19}; - -cc_u32 sha256_k[64] = - {0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, - 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, - 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, - 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, - 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, - 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, - 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, - 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, - 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, - 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, - 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, - 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, - 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, - 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, - 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, - 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2}; +cc_u32 sha224_h0[8] = { + 0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939, + 0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4 +}; + +cc_u32 sha256_h0[8] = { + 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, + 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 +}; + +cc_u32 sha256_k[64] = { + 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, + 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, + 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, + 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, + 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, + 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, + 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, + 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, + 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, + 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, + 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, + 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, + 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, + 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, + 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, + 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 +}; #ifndef _OP64_NOTSUPPORTED #ifdef _WIN32 -cc_u64 sha384_h0[8] = - {0xcbbb9d5dc1059ed8, 0x629a292a367cd507, - 0x9159015a3070dd17, 0x152fecd8f70e5939, - 0x67332667ffc00b31, 0x8eb44a8768581511, - 0xdb0c2e0d64f98fa7, 0x47b5481dbefa4fa4}; - -cc_u64 sha512_h0[8] = - {0x6a09e667f3bcc908, 0xbb67ae8584caa73b, - 0x3c6ef372fe94f82b, 0xa54ff53a5f1d36f1, - 0x510e527fade682d1, 0x9b05688c2b3e6c1f, - 0x1f83d9abfb41bd6b, 0x5be0cd19137e2179}; - -cc_u64 sha512_k[80] = - {0x428a2f98d728ae22, 0x7137449123ef65cd, - 0xb5c0fbcfec4d3b2f, 0xe9b5dba58189dbbc, - 0x3956c25bf348b538, 0x59f111f1b605d019, - 0x923f82a4af194f9b, 0xab1c5ed5da6d8118, - 0xd807aa98a3030242, 0x12835b0145706fbe, - 0x243185be4ee4b28c, 0x550c7dc3d5ffb4e2, - 0x72be5d74f27b896f, 0x80deb1fe3b1696b1, - 0x9bdc06a725c71235, 0xc19bf174cf692694, - 0xe49b69c19ef14ad2, 0xefbe4786384f25e3, - 0x0fc19dc68b8cd5b5, 0x240ca1cc77ac9c65, - 0x2de92c6f592b0275, 0x4a7484aa6ea6e483, - 0x5cb0a9dcbd41fbd4, 0x76f988da831153b5, - 0x983e5152ee66dfab, 0xa831c66d2db43210, - 0xb00327c898fb213f, 0xbf597fc7beef0ee4, - 0xc6e00bf33da88fc2, 0xd5a79147930aa725, - 0x06ca6351e003826f, 0x142929670a0e6e70, - 0x27b70a8546d22ffc, 0x2e1b21385c26c926, - 0x4d2c6dfc5ac42aed, 0x53380d139d95b3df, - 0x650a73548baf63de, 0x766a0abb3c77b2a8, - 0x81c2c92e47edaee6, 0x92722c851482353b, - 0xa2bfe8a14cf10364, 0xa81a664bbc423001, - 0xc24b8b70d0f89791, 0xc76c51a30654be30, - 0xd192e819d6ef5218, 0xd69906245565a910, - 0xf40e35855771202a, 0x106aa07032bbd1b8, - 0x19a4c116b8d2d0c8, 0x1e376c085141ab53, - 0x2748774cdf8eeb99, 0x34b0bcb5e19b48a8, - 0x391c0cb3c5c95a63, 0x4ed8aa4ae3418acb, - 0x5b9cca4f7763e373, 0x682e6ff3d6b2b8a3, - 0x748f82ee5defb2fc, 0x78a5636f43172f60, - 0x84c87814a1f0ab72, 0x8cc702081a6439ec, - 0x90befffa23631e28, 0xa4506cebde82bde9, - 0xbef9a3f7b2c67915, 0xc67178f2e372532b, - 0xca273eceea26619c, 0xd186b8c721c0c207, - 0xeada7dd6cde0eb1e, 0xf57d4f7fee6ed178, - 0x06f067aa72176fba, 0x0a637dc5a2c898a6, - 0x113f9804bef90dae, 0x1b710b35131c471b, - 0x28db77f523047d84, 0x32caab7b40c72493, - 0x3c9ebe0a15c9bebc, 0x431d67c49c100d4c, - 0x4cc5d4becb3e42b6, 0x597f299cfc657e2a, - 0x5fcb6fab3ad6faec, 0x6c44198c4a475817}; +cc_u64 sha384_h0[8] = { + 0xcbbb9d5dc1059ed8, 0x629a292a367cd507, + 0x9159015a3070dd17, 0x152fecd8f70e5939, + 0x67332667ffc00b31, 0x8eb44a8768581511, + 0xdb0c2e0d64f98fa7, 0x47b5481dbefa4fa4 +}; + +cc_u64 sha512_h0[8] = { + 0x6a09e667f3bcc908, 0xbb67ae8584caa73b, + 0x3c6ef372fe94f82b, 0xa54ff53a5f1d36f1, + 0x510e527fade682d1, 0x9b05688c2b3e6c1f, + 0x1f83d9abfb41bd6b, 0x5be0cd19137e2179 +}; + +cc_u64 sha512_k[80] = { + 0x428a2f98d728ae22, 0x7137449123ef65cd, + 0xb5c0fbcfec4d3b2f, 0xe9b5dba58189dbbc, + 0x3956c25bf348b538, 0x59f111f1b605d019, + 0x923f82a4af194f9b, 0xab1c5ed5da6d8118, + 0xd807aa98a3030242, 0x12835b0145706fbe, + 0x243185be4ee4b28c, 0x550c7dc3d5ffb4e2, + 0x72be5d74f27b896f, 0x80deb1fe3b1696b1, + 0x9bdc06a725c71235, 0xc19bf174cf692694, + 0xe49b69c19ef14ad2, 0xefbe4786384f25e3, + 0x0fc19dc68b8cd5b5, 0x240ca1cc77ac9c65, + 0x2de92c6f592b0275, 0x4a7484aa6ea6e483, + 0x5cb0a9dcbd41fbd4, 0x76f988da831153b5, + 0x983e5152ee66dfab, 0xa831c66d2db43210, + 0xb00327c898fb213f, 0xbf597fc7beef0ee4, + 0xc6e00bf33da88fc2, 0xd5a79147930aa725, + 0x06ca6351e003826f, 0x142929670a0e6e70, + 0x27b70a8546d22ffc, 0x2e1b21385c26c926, + 0x4d2c6dfc5ac42aed, 0x53380d139d95b3df, + 0x650a73548baf63de, 0x766a0abb3c77b2a8, + 0x81c2c92e47edaee6, 0x92722c851482353b, + 0xa2bfe8a14cf10364, 0xa81a664bbc423001, + 0xc24b8b70d0f89791, 0xc76c51a30654be30, + 0xd192e819d6ef5218, 0xd69906245565a910, + 0xf40e35855771202a, 0x106aa07032bbd1b8, + 0x19a4c116b8d2d0c8, 0x1e376c085141ab53, + 0x2748774cdf8eeb99, 0x34b0bcb5e19b48a8, + 0x391c0cb3c5c95a63, 0x4ed8aa4ae3418acb, + 0x5b9cca4f7763e373, 0x682e6ff3d6b2b8a3, + 0x748f82ee5defb2fc, 0x78a5636f43172f60, + 0x84c87814a1f0ab72, 0x8cc702081a6439ec, + 0x90befffa23631e28, 0xa4506cebde82bde9, + 0xbef9a3f7b2c67915, 0xc67178f2e372532b, + 0xca273eceea26619c, 0xd186b8c721c0c207, + 0xeada7dd6cde0eb1e, 0xf57d4f7fee6ed178, + 0x06f067aa72176fba, 0x0a637dc5a2c898a6, + 0x113f9804bef90dae, 0x1b710b35131c471b, + 0x28db77f523047d84, 0x32caab7b40c72493, + 0x3c9ebe0a15c9bebc, 0x431d67c49c100d4c, + 0x4cc5d4becb3e42b6, 0x597f299cfc657e2a, + 0x5fcb6fab3ad6faec, 0x6c44198c4a475817 +}; #else -cc_u64 sha384_h0[8] = - {0xcbbb9d5dc1059ed8ULL, 0x629a292a367cd507ULL, - 0x9159015a3070dd17ULL, 0x152fecd8f70e5939ULL, - 0x67332667ffc00b31ULL, 0x8eb44a8768581511ULL, - 0xdb0c2e0d64f98fa7ULL, 0x47b5481dbefa4fa4ULL}; - -cc_u64 sha512_h0[8] = - {0x6a09e667f3bcc908ULL, 0xbb67ae8584caa73bULL, - 0x3c6ef372fe94f82bULL, 0xa54ff53a5f1d36f1ULL, - 0x510e527fade682d1ULL, 0x9b05688c2b3e6c1fULL, - 0x1f83d9abfb41bd6bULL, 0x5be0cd19137e2179ULL}; - -cc_u64 sha512_k[80] = - {0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL, - 0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL, - 0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL, - 0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL, - 0xd807aa98a3030242ULL, 0x12835b0145706fbeULL, - 0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL, - 0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL, - 0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL, - 0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL, - 0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL, - 0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL, - 0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL, - 0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL, - 0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL, - 0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL, - 0x06ca6351e003826fULL, 0x142929670a0e6e70ULL, - 0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL, - 0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL, - 0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL, - 0x81c2c92e47edaee6ULL, 0x92722c851482353bULL, - 0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL, - 0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL, - 0xd192e819d6ef5218ULL, 0xd69906245565a910ULL, - 0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL, - 0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL, - 0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL, - 0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL, - 0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL, - 0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL, - 0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL, - 0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL, - 0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL, - 0xca273eceea26619cULL, 0xd186b8c721c0c207ULL, - 0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL, - 0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL, - 0x113f9804bef90daeULL, 0x1b710b35131c471bULL, - 0x28db77f523047d84ULL, 0x32caab7b40c72493ULL, - 0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL, - 0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL, - 0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL}; +cc_u64 sha384_h0[8] = { + 0xcbbb9d5dc1059ed8ULL, 0x629a292a367cd507ULL, + 0x9159015a3070dd17ULL, 0x152fecd8f70e5939ULL, + 0x67332667ffc00b31ULL, 0x8eb44a8768581511ULL, + 0xdb0c2e0d64f98fa7ULL, 0x47b5481dbefa4fa4ULL +}; + +cc_u64 sha512_h0[8] = { + 0x6a09e667f3bcc908ULL, 0xbb67ae8584caa73bULL, + 0x3c6ef372fe94f82bULL, 0xa54ff53a5f1d36f1ULL, + 0x510e527fade682d1ULL, 0x9b05688c2b3e6c1fULL, + 0x1f83d9abfb41bd6bULL, 0x5be0cd19137e2179ULL +}; + +cc_u64 sha512_k[80] = { + 0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL, + 0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL, + 0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL, + 0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL, + 0xd807aa98a3030242ULL, 0x12835b0145706fbeULL, + 0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL, + 0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL, + 0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL, + 0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL, + 0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL, + 0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL, + 0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL, + 0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL, + 0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL, + 0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL, + 0x06ca6351e003826fULL, 0x142929670a0e6e70ULL, + 0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL, + 0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL, + 0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL, + 0x81c2c92e47edaee6ULL, 0x92722c851482353bULL, + 0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL, + 0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL, + 0xd192e819d6ef5218ULL, 0xd69906245565a910ULL, + 0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL, + 0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL, + 0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL, + 0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL, + 0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL, + 0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL, + 0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL, + 0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL, + 0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL, + 0xca273eceea26619cULL, 0xd186b8c721c0c207ULL, + 0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL, + 0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL, + 0x113f9804bef90daeULL, 0x1b710b35131c471bULL, + 0x28db77f523047d84ULL, 0x32caab7b40c72493ULL, + 0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL, + 0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL, + 0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL +}; #endif //_WIN32 #endif //_OP64_NOTSUPPORTED /* SHA-256 functions */ -void SDRM_SHA256_Transf(SDRM_SHA256Context* ctx, const cc_u8 *message, cc_u32 block_nb) +void SDRM_SHA256_Transf(SDRM_SHA256Context *ctx, const cc_u8 *message, + cc_u32 block_nb) { cc_u32 w[64]; cc_u32 wv[8]; @@ -280,28 +290,21 @@ void SDRM_SHA256_Transf(SDRM_SHA256Context* ctx, const cc_u8 *message, cc_u32 bl int j; - for (i = 0; i < (int) block_nb; i++) - { + for (i = 0; i < (int) block_nb; i++) { sub_block = message + (i << 6); for (j = 0; j < 16; j++) - { SDRM_SHA2_PACK32(&sub_block[j << 2], &w[j]); - } for (j = 16; j < 64; j++) - { SDRM_SHA2_SHA256_SCR(j); - } for (j = 0; j < 8; j++) - { wv[j] = ctx->h[j]; - } - for (j = 0; j < 64; j++) - { - t1 = wv[7] + SDRM_SHA2_SHA256_F2(wv[4]) + SDRM_SHA2_CH(wv[4], wv[5], wv[6]) + sha256_k[j] + w[j]; + for (j = 0; j < 64; j++) { + t1 = wv[7] + SDRM_SHA2_SHA256_F2(wv[4]) + SDRM_SHA2_CH(wv[4], wv[5], + wv[6]) + sha256_k[j] + w[j]; t2 = SDRM_SHA2_SHA256_F1(wv[0]) + SDRM_SHA2_MAJ(wv[0], wv[1], wv[2]); wv[7] = wv[6]; wv[6] = wv[5]; @@ -314,25 +317,23 @@ void SDRM_SHA256_Transf(SDRM_SHA256Context* ctx, const cc_u8 *message, cc_u32 bl } for (j = 0; j < 8; j++) - { ctx->h[j] += wv[j]; - } } } -void SDRM_SHA256_Init(SDRM_SHA256Context* ctx) +void SDRM_SHA256_Init(SDRM_SHA256Context *ctx) { int i; + for (i = 0; i < 8; i++) - { ctx->h[i] = sha256_h0[i]; - } ctx->len = 0; ctx->tot_len = 0; } -void SDRM_SHA256_Update(SDRM_SHA256Context* ctx, const cc_u8 *message, cc_u32 len) +void SDRM_SHA256_Update(SDRM_SHA256Context *ctx, const cc_u8 *message, + cc_u32 len) { cc_u32 block_nb; cc_u32 new_len, rem_len, tmp_len; @@ -343,8 +344,7 @@ void SDRM_SHA256_Update(SDRM_SHA256Context* ctx, const cc_u8 *message, cc_u32 le memcpy(&ctx->block[ctx->len], message, rem_len); - if (ctx->len + len < SDRM_SHA256_DATA_SIZE) - { + if (ctx->len + len < SDRM_SHA256_DATA_SIZE) { ctx->len += len; return; } @@ -365,7 +365,7 @@ void SDRM_SHA256_Update(SDRM_SHA256Context* ctx, const cc_u8 *message, cc_u32 le ctx->tot_len += (block_nb + 1) << 6; } -void SDRM_SHA256_Final(SDRM_SHA256Context* ctx, cc_u8 *digest) +void SDRM_SHA256_Final(SDRM_SHA256Context *ctx, cc_u8 *digest) { cc_u32 block_nb; cc_u32 pm_len; @@ -373,7 +373,8 @@ void SDRM_SHA256_Final(SDRM_SHA256Context* ctx, cc_u8 *digest) int i; - block_nb = (1 + ((SDRM_SHA256_DATA_SIZE - 9) < (ctx->len % SDRM_SHA256_DATA_SIZE))); + block_nb = (1 + ((SDRM_SHA256_DATA_SIZE - 9) < (ctx->len % + SDRM_SHA256_DATA_SIZE))); len_b = (ctx->tot_len + ctx->len) << 3; pm_len = block_nb << 6; @@ -385,16 +386,15 @@ void SDRM_SHA256_Final(SDRM_SHA256Context* ctx, cc_u8 *digest) SDRM_SHA256_Transf(ctx, ctx->block, block_nb); for (i = 0 ; i < 8; i++) - { SDRM_SHA2_UNPACK32(ctx->h[i], &digest[i << 2]); - } } #ifndef _OP64_NOTSUPPORTED /* SHA-512 functions */ -void SDRM_SHA512_Transf(SDRM_SHA512Context* ctx, const cc_u8 *message, cc_u32 block_nb) +void SDRM_SHA512_Transf(SDRM_SHA512Context *ctx, const cc_u8 *message, + cc_u32 block_nb) { cc_u64 w[80]; cc_u64 wv[8]; @@ -402,29 +402,21 @@ void SDRM_SHA512_Transf(SDRM_SHA512Context* ctx, const cc_u8 *message, cc_u32 bl const cc_u8 *sub_block; int i, j; - for (i = 0; i < (int) block_nb; i++) - { + for (i = 0; i < (int) block_nb; i++) { sub_block = message + (i << 7); for (j = 0; j < 16; j++) - { SDRM_SHA2_PACK64(&sub_block[j << 3], &w[j]); - } for (j = 16; j < 80; j++) - { SDRM_SHA2_SHA512_SCR(j); - } for (j = 0; j < 8; j++) - { wv[j] = ctx->h[j]; - } - for (j = 0; j < 80; j++) - { + for (j = 0; j < 80; j++) { t1 = wv[7] + SDRM_SHA2_SHA512_F2(wv[4]) + SDRM_SHA2_CH(wv[4], wv[5], wv[6]) - + sha512_k[j] + w[j]; + + sha512_k[j] + w[j]; t2 = SDRM_SHA2_SHA512_F1(wv[0]) + SDRM_SHA2_MAJ(wv[0], wv[1], wv[2]); wv[7] = wv[6]; wv[6] = wv[5]; @@ -437,25 +429,23 @@ void SDRM_SHA512_Transf(SDRM_SHA512Context* ctx, const cc_u8 *message, cc_u32 bl } for (j = 0; j < 8; j++) - { ctx->h[j] += wv[j]; - } } } -void SDRM_SHA512_Init(SDRM_SHA512Context* ctx) +void SDRM_SHA512_Init(SDRM_SHA512Context *ctx) { int i; + for (i = 0; i < 8; i++) - { ctx->h[i] = sha512_h0[i]; - } ctx->len = 0; ctx->tot_len = 0; } -void SDRM_SHA512_Update(SDRM_SHA512Context* ctx, const cc_u8 *message, cc_u32 len) +void SDRM_SHA512_Update(SDRM_SHA512Context *ctx, const cc_u8 *message, + cc_u32 len) { cc_u32 block_nb; cc_u32 new_len, rem_len, tmp_len; @@ -466,8 +456,7 @@ void SDRM_SHA512_Update(SDRM_SHA512Context* ctx, const cc_u8 *message, cc_u32 le memcpy(&ctx->block[ctx->len], message, rem_len); - if (ctx->len + len < SDRM_SHA512_DATA_SIZE) - { + if (ctx->len + len < SDRM_SHA512_DATA_SIZE) { ctx->len += len; return; } @@ -488,7 +477,7 @@ void SDRM_SHA512_Update(SDRM_SHA512Context* ctx, const cc_u8 *message, cc_u32 le ctx->tot_len += (block_nb + 1) << 7; } -void SDRM_SHA512_Final(SDRM_SHA512Context* ctx, cc_u8 *digest) +void SDRM_SHA512_Final(SDRM_SHA512Context *ctx, cc_u8 *digest) { cc_u32 block_nb; cc_u32 pm_len; @@ -496,7 +485,8 @@ void SDRM_SHA512_Final(SDRM_SHA512Context* ctx, cc_u8 *digest) int i; - block_nb = 1 + ((SDRM_SHA512_DATA_SIZE - 17) < (ctx->len % SDRM_SHA512_DATA_SIZE)); + block_nb = 1 + ((SDRM_SHA512_DATA_SIZE - 17) < (ctx->len % + SDRM_SHA512_DATA_SIZE)); len_b = (ctx->tot_len + ctx->len) << 3; pm_len = block_nb << 7; @@ -508,26 +498,24 @@ void SDRM_SHA512_Final(SDRM_SHA512Context* ctx, cc_u8 *digest) SDRM_SHA512_Transf(ctx, ctx->block, block_nb); for (i = 0 ; i < 8; i++) - { SDRM_SHA2_UNPACK64(ctx->h[i], &digest[i << 3]); - } } /* SHA-384 functions */ -void SDRM_SHA384_Init(SDRM_SHA384Context* ctx) +void SDRM_SHA384_Init(SDRM_SHA384Context *ctx) { int i; + for (i = 0; i < 8; i++) - { ctx->h[i] = sha384_h0[i]; - } ctx->len = 0; ctx->tot_len = 0; } -void SDRM_SHA384_Update(SDRM_SHA384Context* ctx, const cc_u8 *message, cc_u32 len) +void SDRM_SHA384_Update(SDRM_SHA384Context *ctx, const cc_u8 *message, + cc_u32 len) { cc_u32 block_nb; cc_u32 new_len, rem_len, tmp_len; @@ -538,8 +526,7 @@ void SDRM_SHA384_Update(SDRM_SHA384Context* ctx, const cc_u8 *message, cc_u32 le memcpy(&ctx->block[ctx->len], message, rem_len); - if (ctx->len + len < SDRM_SHA384_DATA_SIZE) - { + if (ctx->len + len < SDRM_SHA384_DATA_SIZE) { ctx->len += len; return; } @@ -560,7 +547,7 @@ void SDRM_SHA384_Update(SDRM_SHA384Context* ctx, const cc_u8 *message, cc_u32 le ctx->tot_len += (block_nb + 1) << 7; } -void SDRM_SHA384_Final(SDRM_SHA384Context* ctx, cc_u8 *digest) +void SDRM_SHA384_Final(SDRM_SHA384Context *ctx, cc_u8 *digest) { cc_u32 block_nb; cc_u32 pm_len; @@ -568,7 +555,8 @@ void SDRM_SHA384_Final(SDRM_SHA384Context* ctx, cc_u8 *digest) int i; - block_nb = (1 + ((SDRM_SHA384_DATA_SIZE - 17) < (ctx->len % SDRM_SHA384_DATA_SIZE))); + block_nb = (1 + ((SDRM_SHA384_DATA_SIZE - 17) < (ctx->len % + SDRM_SHA384_DATA_SIZE))); len_b = (ctx->tot_len + ctx->len) << 3; pm_len = block_nb << 7; @@ -580,9 +568,7 @@ void SDRM_SHA384_Final(SDRM_SHA384Context* ctx, cc_u8 *digest) SDRM_SHA512_Transf(ctx, ctx->block, block_nb); for (i = 0 ; i < 6; i++) - { SDRM_SHA2_UNPACK64(ctx->h[i], &digest[i << 3]); - } } #endif //_OP64_NOTSUPPORTED @@ -591,70 +577,69 @@ void SDRM_SHA384_Final(SDRM_SHA384Context* ctx, cc_u8 *digest) void SDRM_SHA224_Init(SDRM_SHA224Context *ctx) { - int i; - for (i = 0; i < 8; i++) { - ctx->h[i] = sha224_h0[i]; - } + int i; + + for (i = 0; i < 8; i++) + ctx->h[i] = sha224_h0[i]; - ctx->len = 0; - ctx->tot_len = 0; + ctx->len = 0; + ctx->tot_len = 0; } void SDRM_SHA224_Update(SDRM_SHA224Context *ctx, const unsigned char *message, - unsigned int len) + unsigned int len) { - unsigned int block_nb; - unsigned int new_len, rem_len, tmp_len; - const unsigned char *shifted_message; + unsigned int block_nb; + unsigned int new_len, rem_len, tmp_len; + const unsigned char *shifted_message; - tmp_len = SDRM_SHA224_DATA_SIZE - ctx->len; - rem_len = len < tmp_len ? len : tmp_len; + tmp_len = SDRM_SHA224_DATA_SIZE - ctx->len; + rem_len = len < tmp_len ? len : tmp_len; - memcpy(&ctx->block[ctx->len], message, rem_len); + memcpy(&ctx->block[ctx->len], message, rem_len); - if (ctx->len + len < SDRM_SHA224_DATA_SIZE) { - ctx->len += len; - return; - } + if (ctx->len + len < SDRM_SHA224_DATA_SIZE) { + ctx->len += len; + return; + } - new_len = len - rem_len; - block_nb = new_len / SDRM_SHA224_DATA_SIZE; + new_len = len - rem_len; + block_nb = new_len / SDRM_SHA224_DATA_SIZE; - shifted_message = message + rem_len; + shifted_message = message + rem_len; - SDRM_SHA256_Transf(ctx, ctx->block, 1); - SDRM_SHA256_Transf(ctx, shifted_message, block_nb); + SDRM_SHA256_Transf(ctx, ctx->block, 1); + SDRM_SHA256_Transf(ctx, shifted_message, block_nb); - rem_len = new_len % SDRM_SHA224_DATA_SIZE; + rem_len = new_len % SDRM_SHA224_DATA_SIZE; - memcpy(ctx->block, &shifted_message[block_nb << 6], - rem_len); + memcpy(ctx->block, &shifted_message[block_nb << 6], + rem_len); - ctx->len = rem_len; - ctx->tot_len += (block_nb + 1) << 6; + ctx->len = rem_len; + ctx->tot_len += (block_nb + 1) << 6; } void SDRM_SHA224_Final(SDRM_SHA224Context *ctx, unsigned char *digest) { - unsigned int block_nb; - unsigned int pm_len; - unsigned int len_b; + unsigned int block_nb; + unsigned int pm_len; + unsigned int len_b; - int i; + int i; - block_nb = (1 + ((SDRM_SHA224_DATA_SIZE - 9) - < (ctx->len % SDRM_SHA224_DATA_SIZE))); + block_nb = (1 + ((SDRM_SHA224_DATA_SIZE - 9) + < (ctx->len % SDRM_SHA224_DATA_SIZE))); - len_b = (ctx->tot_len + ctx->len) << 3; - pm_len = block_nb << 6; + len_b = (ctx->tot_len + ctx->len) << 3; + pm_len = block_nb << 6; - memset(ctx->block + ctx->len, 0, pm_len - ctx->len); - ctx->block[ctx->len] = 0x80; - SDRM_SHA2_UNPACK32(len_b, ctx->block + pm_len - 4); + memset(ctx->block + ctx->len, 0, pm_len - ctx->len); + ctx->block[ctx->len] = 0x80; + SDRM_SHA2_UNPACK32(len_b, ctx->block + pm_len - 4); - SDRM_SHA256_Transf(ctx, ctx->block, block_nb); + SDRM_SHA256_Transf(ctx, ctx->block, block_nb); - for (i = 0 ; i < 7; i++) { - SDRM_SHA2_UNPACK32(ctx->h[i], &digest[i << 2]); - } + for (i = 0 ; i < 7; i++) + SDRM_SHA2_UNPACK32(ctx->h[i], &digest[i << 2]); } diff --git a/ssflib/dep/cryptocore/source/base/cc_snow2.c b/ssflib/dep/cryptocore/source/base/cc_snow2.c index 1d941ff..c959800 100644 --- a/ssflib/dep/cryptocore/source/base/cc_snow2.c +++ b/ssflib/dep/cryptocore/source/base/cc_snow2.c @@ -29,7 +29,7 @@ //////////////////////////////////////////////////////////////////////////// // pre-computated values //////////////////////////////////////////////////////////////////////////// -static cc_u32 SNOW2_MUL_a[256]= { +static cc_u32 SNOW2_MUL_a[256] = { 0x00000000, 0xE19FCF13, 0x6B973726, 0x8A08F835, 0xD6876E4C, 0x3718A15F, 0xBD10596A, 0x5C8F9679, 0x05A7DC98, 0xE438138B, 0x6E30EBBE, 0x8FAF24AD, 0xD320B2D4, 0x32BF7DC7, 0xB8B785F2, 0x59284AE1, 0x0AE71199, 0xEB78DE8A, 0x617026BF, 0x80EFE9AC, 0xDC607FD5, 0x3DFFB0C6, 0xB7F748F3, 0x566887E0, @@ -64,8 +64,8 @@ static cc_u32 SNOW2_MUL_a[256]= { 0x63DC2392, 0x8243EC81, 0x084B14B4, 0xE9D4DBA7, 0xB55B4DDE, 0x54C482CD, 0xDECC7AF8, 0x3F53B5EB }; -static cc_u32 SNOW2_MUL_ainverse[256]= { - 0x00000000, 0x180F40CD, 0x301E8033, 0x2811C0FE, 0x603CA966, 0x7833E9AB, 0x50222955, 0x482D6998, +static cc_u32 SNOW2_MUL_ainverse[256] = { + 0x00000000, 0x180F40CD, 0x301E8033, 0x2811C0FE, 0x603CA966, 0x7833E9AB, 0x50222955, 0x482D6998, 0xC078FBCC, 0xD877BB01, 0xF0667BFF, 0xE8693B32, 0xA04452AA, 0xB84B1267, 0x905AD299, 0x88559254, 0x29F05F31, 0x31FF1FFC, 0x19EEDF02, 0x01E19FCF, 0x49CCF657, 0x51C3B69A, 0x79D27664, 0x61DD36A9, 0xE988A4FD, 0xF187E430, 0xD99624CE, 0xC1996403, 0x89B40D9B, 0x91BB4D56, 0xB9AA8DA8, 0xA1A5CD65, @@ -84,7 +84,7 @@ static cc_u32 SNOW2_MUL_ainverse[256]= { 0xE18D0321, 0xF98243EC, 0xD1938312, 0xC99CC3DF, 0x81B1AA47, 0x99BEEA8A, 0xB1AF2A74, 0xA9A06AB9, 0x21F5F8ED, 0x39FAB820, 0x11EB78DE, 0x09E43813, 0x41C9518B, 0x59C61146, 0x71D7D1B8, 0x69D89175, 0xC87D5C10, 0xD0721CDD, 0xF863DC23, 0xE06C9CEE, 0xA841F576, 0xB04EB5BB, 0x985F7545, 0x80503588, - 0x0805A7DC, 0x100AE711, 0x381B27EF, 0x20146722, 0x68390EBA, 0x70364E77, 0x58278E89, 0x4028CE44, + 0x0805A7DC, 0x100AE711, 0x381B27EF, 0x20146722, 0x68390EBA, 0x70364E77, 0x58278E89, 0x4028CE44, 0xB3C4BD43, 0xABCBFD8E, 0x83DA3D70, 0x9BD57DBD, 0xD3F81425, 0xCBF754E8, 0xE3E69416, 0xFBE9D4DB, 0x73BC468F, 0x6BB30642, 0x43A2C6BC, 0x5BAD8671, 0x1380EFE9, 0x0B8FAF24, 0x239E6FDA, 0x3B912F17, 0x9A34E272, 0x823BA2BF, 0xAA2A6241, 0xB225228C, 0xFA084B14, 0xE2070BD9, 0xCA16CB27, 0xD2198BEA, @@ -99,15 +99,15 @@ static cc_u32 SNOW2_MUL_ainverse[256]= { 0xFEDECC7A, 0xE6D18CB7, 0xCEC04C49, 0xD6CF0C84, 0x9EE2651C, 0x86ED25D1, 0xAEFCE52F, 0xB6F3A5E2 }; -static cc_u32 SNOW2_T0[256]= { +static cc_u32 SNOW2_T0[256] = { 0xa56363c6, 0x847c7cf8, 0x997777ee, 0x8d7b7bf6, 0x0df2f2ff, 0xbd6b6bd6, 0xb16f6fde, 0x54c5c591, 0x50303060, 0x03010102, 0xa96767ce, 0x7d2b2b56, 0x19fefee7, 0x62d7d7b5, 0xe6abab4d, 0x9a7676ec, 0x45caca8f, 0x9d82821f, 0x40c9c989, 0x877d7dfa, 0x15fafaef, 0xeb5959b2, 0xc947478e, 0x0bf0f0fb, 0xecadad41, 0x67d4d4b3, 0xfda2a25f, 0xeaafaf45, 0xbf9c9c23, 0xf7a4a453, 0x967272e4, 0x5bc0c09b, 0xc2b7b775, 0x1cfdfde1, 0xae93933d, 0x6a26264c, 0x5a36366c, 0x413f3f7e, 0x02f7f7f5, 0x4fcccc83, 0x5c343468, 0xf4a5a551, 0x34e5e5d1, 0x08f1f1f9, 0x937171e2, 0x73d8d8ab, 0x53313162, 0x3f15152a, - 0x0c040408, 0x52c7c795, 0x65232346, 0x5ec3c39d, 0x28181830, 0xa1969637, 0x0f05050a, 0xb59a9a2f, - 0x0907070e, 0x36121224, 0x9b80801b, 0x3de2e2df, 0x26ebebcd, 0x6927274e, 0xcdb2b27f, 0x9f7575ea, + 0x0c040408, 0x52c7c795, 0x65232346, 0x5ec3c39d, 0x28181830, 0xa1969637, 0x0f05050a, 0xb59a9a2f, + 0x0907070e, 0x36121224, 0x9b80801b, 0x3de2e2df, 0x26ebebcd, 0x6927274e, 0xcdb2b27f, 0x9f7575ea, 0x1b090912, 0x9e83831d, 0x742c2c58, 0x2e1a1a34, 0x2d1b1b36, 0xb26e6edc, 0xee5a5ab4, 0xfba0a05b, 0xf65252a4, 0x4d3b3b76, 0x61d6d6b7, 0xceb3b37d, 0x7b292952, 0x3ee3e3dd, 0x712f2f5e, 0x97848413, 0xf55353a6, 0x68d1d1b9, 0x00000000, 0x2cededc1, 0x60202040, 0x1ffcfce3, 0xc8b1b179, 0xed5b5bb6, @@ -134,16 +134,16 @@ static cc_u32 SNOW2_T0[256]= { 0xc3414182, 0xb0999929, 0x772d2d5a, 0x110f0f1e, 0xcbb0b07b, 0xfc5454a8, 0xd6bbbb6d, 0x3a16162c }; -static cc_u32 SNOW2_T1[256]= { +static cc_u32 SNOW2_T1[256] = { 0x6363c6a5, 0x7c7cf884, 0x7777ee99, 0x7b7bf68d, 0xf2f2ff0d, 0x6b6bd6bd, 0x6f6fdeb1, 0xc5c59154, 0x30306050, 0x01010203, 0x6767cea9, 0x2b2b567d, 0xfefee719, 0xd7d7b562, 0xabab4de6, 0x7676ec9a, 0xcaca8f45, 0x82821f9d, 0xc9c98940, 0x7d7dfa87, 0xfafaef15, 0x5959b2eb, 0x47478ec9, 0xf0f0fb0b, 0xadad41ec, 0xd4d4b367, 0xa2a25ffd, 0xafaf45ea, 0x9c9c23bf, 0xa4a453f7, 0x7272e496, 0xc0c09b5b, 0xb7b775c2, 0xfdfde11c, 0x93933dae, 0x26264c6a, 0x36366c5a, 0x3f3f7e41, 0xf7f7f502, 0xcccc834f, 0x3434685c, 0xa5a551f4, 0xe5e5d134, 0xf1f1f908, 0x7171e293, 0xd8d8ab73, 0x31316253, 0x15152a3f, - 0x0404080c, 0xc7c79552, 0x23234665, 0xc3c39d5e, 0x18183028, 0x969637a1, 0x05050a0f, 0x9a9a2fb5, - 0x07070e09, 0x12122436, 0x80801b9b, 0xe2e2df3d, 0xebebcd26, 0x27274e69, 0xb2b27fcd, 0x7575ea9f, - 0x0909121b, 0x83831d9e, 0x2c2c5874, 0x1a1a342e, 0x1b1b362d, 0x6e6edcb2, 0x5a5ab4ee, 0xa0a05bfb, + 0x0404080c, 0xc7c79552, 0x23234665, 0xc3c39d5e, 0x18183028, 0x969637a1, 0x05050a0f, 0x9a9a2fb5, + 0x07070e09, 0x12122436, 0x80801b9b, 0xe2e2df3d, 0xebebcd26, 0x27274e69, 0xb2b27fcd, 0x7575ea9f, + 0x0909121b, 0x83831d9e, 0x2c2c5874, 0x1a1a342e, 0x1b1b362d, 0x6e6edcb2, 0x5a5ab4ee, 0xa0a05bfb, 0x5252a4f6, 0x3b3b764d, 0xd6d6b761, 0xb3b37dce, 0x2929527b, 0xe3e3dd3e, 0x2f2f5e71, 0x84841397, 0x5353a6f5, 0xd1d1b968, 0x00000000, 0xededc12c, 0x20204060, 0xfcfce31f, 0xb1b179c8, 0x5b5bb6ed, 0x6a6ad4be, 0xcbcb8d46, 0xbebe67d9, 0x3939724b, 0x4a4a94de, 0x4c4c98d4, 0x5858b0e8, 0xcfcf854a, @@ -169,16 +169,16 @@ static cc_u32 SNOW2_T1[256]= { 0x414182c3, 0x999929b0, 0x2d2d5a77, 0x0f0f1e11, 0xb0b07bcb, 0x5454a8fc, 0xbbbb6dd6, 0x16162c3a }; -static cc_u32 SNOW2_T2[256]= { +static cc_u32 SNOW2_T2[256] = { 0x63c6a563, 0x7cf8847c, 0x77ee9977, 0x7bf68d7b, 0xf2ff0df2, 0x6bd6bd6b, 0x6fdeb16f, 0xc59154c5, 0x30605030, 0x01020301, 0x67cea967, 0x2b567d2b, 0xfee719fe, 0xd7b562d7, 0xab4de6ab, 0x76ec9a76, 0xca8f45ca, 0x821f9d82, 0xc98940c9, 0x7dfa877d, 0xfaef15fa, 0x59b2eb59, 0x478ec947, 0xf0fb0bf0, 0xad41ecad, 0xd4b367d4, 0xa25ffda2, 0xaf45eaaf, 0x9c23bf9c, 0xa453f7a4, 0x72e49672, 0xc09b5bc0, 0xb775c2b7, 0xfde11cfd, 0x933dae93, 0x264c6a26, 0x366c5a36, 0x3f7e413f, 0xf7f502f7, 0xcc834fcc, 0x34685c34, 0xa551f4a5, 0xe5d134e5, 0xf1f908f1, 0x71e29371, 0xd8ab73d8, 0x31625331, 0x152a3f15, - 0x04080c04, 0xc79552c7, 0x23466523, 0xc39d5ec3, 0x18302818, 0x9637a196, 0x050a0f05, 0x9a2fb59a, - 0x070e0907, 0x12243612, 0x801b9b80, 0xe2df3de2, 0xebcd26eb, 0x274e6927, 0xb27fcdb2, 0x75ea9f75, - 0x09121b09, 0x831d9e83, 0x2c58742c, 0x1a342e1a, 0x1b362d1b, 0x6edcb26e, 0x5ab4ee5a, 0xa05bfba0, + 0x04080c04, 0xc79552c7, 0x23466523, 0xc39d5ec3, 0x18302818, 0x9637a196, 0x050a0f05, 0x9a2fb59a, + 0x070e0907, 0x12243612, 0x801b9b80, 0xe2df3de2, 0xebcd26eb, 0x274e6927, 0xb27fcdb2, 0x75ea9f75, + 0x09121b09, 0x831d9e83, 0x2c58742c, 0x1a342e1a, 0x1b362d1b, 0x6edcb26e, 0x5ab4ee5a, 0xa05bfba0, 0x52a4f652, 0x3b764d3b, 0xd6b761d6, 0xb37dceb3, 0x29527b29, 0xe3dd3ee3, 0x2f5e712f, 0x84139784, 0x53a6f553, 0xd1b968d1, 0x00000000, 0xedc12ced, 0x20406020, 0xfce31ffc, 0xb179c8b1, 0x5bb6ed5b, 0x6ad4be6a, 0xcb8d46cb, 0xbe67d9be, 0x39724b39, 0x4a94de4a, 0x4c98d44c, 0x58b0e858, 0xcf854acf, @@ -204,15 +204,15 @@ static cc_u32 SNOW2_T2[256]= { 0x4182c341, 0x9929b099, 0x2d5a772d, 0x0f1e110f, 0xb07bcbb0, 0x54a8fc54, 0xbb6dd6bb, 0x162c3a16 }; -static cc_u32 SNOW2_T3[256]= { +static cc_u32 SNOW2_T3[256] = { 0xc6a56363, 0xf8847c7c, 0xee997777, 0xf68d7b7b, 0xff0df2f2, 0xd6bd6b6b, 0xdeb16f6f, 0x9154c5c5, 0x60503030, 0x02030101, 0xcea96767, 0x567d2b2b, 0xe719fefe, 0xb562d7d7, 0x4de6abab, 0xec9a7676, 0x8f45caca, 0x1f9d8282, 0x8940c9c9, 0xfa877d7d, 0xef15fafa, 0xb2eb5959, 0x8ec94747, 0xfb0bf0f0, 0x41ecadad, 0xb367d4d4, 0x5ffda2a2, 0x45eaafaf, 0x23bf9c9c, 0x53f7a4a4, 0xe4967272, 0x9b5bc0c0, 0x75c2b7b7, 0xe11cfdfd, 0x3dae9393, 0x4c6a2626, 0x6c5a3636, 0x7e413f3f, 0xf502f7f7, 0x834fcccc, 0x685c3434, 0x51f4a5a5, 0xd134e5e5, 0xf908f1f1, 0xe2937171, 0xab73d8d8, 0x62533131, 0x2a3f1515, - 0x080c0404, 0x9552c7c7, 0x46652323, 0x9d5ec3c3, 0x30281818, 0x37a19696, 0xa0f0505, 0x2fb59a9a, - 0x0e090707, 0x24361212, 0x1b9b8080, 0xdf3de2e2, 0xcd26ebeb, 0x4e692727, 0x7fcdb2b2, 0xea9f7575, + 0x080c0404, 0x9552c7c7, 0x46652323, 0x9d5ec3c3, 0x30281818, 0x37a19696, 0xa0f0505, 0x2fb59a9a, + 0x0e090707, 0x24361212, 0x1b9b8080, 0xdf3de2e2, 0xcd26ebeb, 0x4e692727, 0x7fcdb2b2, 0xea9f7575, 0x121b0909, 0x1d9e8383, 0x58742c2c, 0x342e1a1a, 0x362d1b1b, 0xdcb26e6e, 0xb4ee5a5a, 0x5bfba0a0, 0xa4f65252, 0x764d3b3b, 0xb761d6d6, 0x7dceb3b3, 0x527b2929, 0xdd3ee3e3, 0x5e712f2f, 0x13978484, 0xa6f55353, 0xb968d1d1, 0x00000000, 0xc12ceded, 0x40602020, 0xe31ffcfc, 0x79c8b1b1, 0xb6ed5b5b, @@ -235,36 +235,37 @@ static cc_u32 SNOW2_T3[256]= { 0xc2a36161, 0x6a5f3535, 0xaef95757, 0x69d0b9b9, 0x17918686, 0x9958c1c1, 0x3a271d1d, 0x27b99e9e, 0xd938e1e1, 0xeb13f8f8, 0x2bb39898, 0x22331111, 0xd2bb6969, 0xa970d9d9, 0x07898e8e, 0x33a79494, 0x2db69b9b, 0x3c221e1e, 0x15928787, 0xc920e9e9, 0x8749cece, 0xaaff5555, 0x50782828, 0xa57adfdf, - 0x038f8c8c, 0x59f8a1a1, 0x09808989, 0x1a170d0d, 0x65dabfbf, 0xd731e6e6, 0x84c64242, 0xd0b86868, + 0x038f8c8c, 0x59f8a1a1, 0x09808989, 0x1a170d0d, 0x65dabfbf, 0xd731e6e6, 0x84c64242, 0xd0b86868, 0x82c34141, 0x29b09999, 0x5a772d2d, 0x1e110f0f, 0x7bcbb0b0, 0xa8fc5454, 0x6dd6bbbb, 0x2c3a1616 }; //////////////////////////////////////////////////////////////////////////// // Macros //////////////////////////////////////////////////////////////////////////// -#define a_MUL(w) (((w) << 8) ^ SNOW2_MUL_a[(w) >> 24]) -#define ainv_MUL(w) (((w) >> 8) ^ SNOW2_MUL_ainverse[(w) & 0xff]) +#define a_MUL(w) (((w) << 8) ^ SNOW2_MUL_a[(w) >> 24]) +#define ainv_MUL(w) (((w) >> 8) ^ SNOW2_MUL_ainverse[(w) & 0xff]) -#define BYTE0(w) ( (w) & 0xff) -#define BYTE1(w) (((w) >> 8) & 0xff) -#define BYTE2(w) (((w) >> 16) & 0xff) -#define BYTE3(w) (((w) >> 24) & 0xff) +#define BYTE0(w) ((w) & 0xff) +#define BYTE1(w) (((w) >> 8) & 0xff) +#define BYTE2(w) (((w) >> 16) & 0xff) +#define BYTE3(w) (((w) >> 24) & 0xff) //////////////////////////////////////////////////////////////////////////// // Functions //////////////////////////////////////////////////////////////////////////// /* - * @fn SDRM_SNOW2_Setup - * @brief Setup FSM and s values + * @fn SDRM_SNOW2_Setup + * @brief Setup FSM and s values * - * @param ctx [out]crypto context - * @param UserKey [in]User Key, 128 or 256 bit - * @param keyLen [in]byte-size of User Key, 16 or 32 - * @param IV [in]16 byte initial vector + * @param ctx [out]crypto context + * @param UserKey [in]User Key, 128 or 256 bit + * @param keyLen [in]byte-size of User Key, 16 or 32 + * @param IV [in]16 byte initial vector * - * @return CRYPTO_SUCCESS if no error is occured + * @return CRYPTO_SUCCESS if no error is occured */ -int SDRM_SNOW2_Setup(SDRM_SNOW2Context *ctx, cc_u8 *UserKey, cc_u32 keyLen, cc_u8 *IV) +int SDRM_SNOW2_Setup(SDRM_SNOW2Context *ctx, cc_u8 *UserKey, cc_u32 keyLen, + cc_u8 *IV) { cc_u32 IV0, IV1, IV2, IV3; cc_u32 *s = ctx->s; @@ -273,7 +274,8 @@ int SDRM_SNOW2_Setup(SDRM_SNOW2Context *ctx, cc_u8 *UserKey, cc_u32 keyLen, cc_u //test endian i = 0xff; - ctx->endian = (*((cc_u8*)&i) == 0xff) ? CRYPTO_LITTLE_ENDIAN : CRYPTO_BIG_ENDIAN; + ctx->endian = (*((cc_u8 *)&i) == 0xff) ? CRYPTO_LITTLE_ENDIAN : + CRYPTO_BIG_ENDIAN; //Initialize IV GET_UINT32(IV3, IV, 0) @@ -289,60 +291,59 @@ int SDRM_SNOW2_Setup(SDRM_SNOW2Context *ctx, cc_u8 *UserKey, cc_u32 keyLen, cc_u GET_UINT32(s[12], UserKey, 12) s[11] = ~s[15]; s[10] = ~s[14]; - s[ 9] = ~s[13]; - s[ 8] = ~s[12]; - s[ 7] = s[15]; - s[ 6] = s[14]; - s[ 5] = s[13]; - s[ 4] = s[12]; - s[ 3] = ~s[15]; - s[ 2] = ~s[14]; - s[ 1] = ~s[13]; - s[ 0] = ~s[12]; - } - else { + s[9] = ~s[13]; + s[8] = ~s[12]; + s[7] = s[15]; + s[6] = s[14]; + s[5] = s[13]; + s[4] = s[12]; + s[3] = ~s[15]; + s[2] = ~s[14]; + s[1] = ~s[13]; + s[0] = ~s[12]; + } else { GET_UINT32(s[15], UserKey, 0) GET_UINT32(s[14], UserKey, 4) GET_UINT32(s[13], UserKey, 8) GET_UINT32(s[12], UserKey, 12) GET_UINT32(s[11], UserKey, 16) GET_UINT32(s[10], UserKey, 20) - GET_UINT32(s[ 9], UserKey, 24) - GET_UINT32(s[ 8], UserKey, 28) - s[ 7] = ~s[15]; - s[ 6] = ~s[14]; - s[ 5] = ~s[13]; - s[ 4] = ~s[12]; - s[ 3] = ~s[11]; - s[ 2] = ~s[10]; - s[ 1] = ~s[ 9]; - s[ 0] = ~s[ 8]; + GET_UINT32(s[9], UserKey, 24) + GET_UINT32(s[8], UserKey, 28) + s[7] = ~s[15]; + s[6] = ~s[14]; + s[5] = ~s[13]; + s[4] = ~s[12]; + s[3] = ~s[11]; + s[2] = ~s[10]; + s[1] = ~s[9]; + s[0] = ~s[8]; } s[15] ^= IV0; s[12] ^= IV1; s[10] ^= IV2; - s[ 9] ^= IV3; + s[9] ^= IV3; r1 = 0; r2 = 0; // clock 32 times without producing any output - for (i = 0; i < 16; i++) - { + for (i = 0; i < 16; i++) { Ft = (r1 + s[(i - 1) & 0x0f]) ^ r2; s[i] = a_MUL(s[i]) ^ s[(i + 2) & 0x0f] ^ ainv_MUL(s[(i + 11) & 0x0f]) ^ Ft; R1_next = r2 + s[(i + 5) & 0x0f]; - r2 = SNOW2_T0[BYTE0(r1)] ^ SNOW2_T1[BYTE1(r1)] ^ SNOW2_T2[BYTE2(r1)] ^ SNOW2_T3[BYTE3(r1)]; + r2 = SNOW2_T0[BYTE0(r1)] ^ SNOW2_T1[BYTE1(r1)] ^ SNOW2_T2[BYTE2(r1)] ^ + SNOW2_T3[BYTE3(r1)]; r1 = R1_next; } - for (i = 0; i < 16; i++) - { + for (i = 0; i < 16; i++) { Ft = (r1 + s[(i - 1) & 0x0f]) ^ r2; s[i] = a_MUL(s[i]) ^ s[(i + 2) & 0x0f] ^ ainv_MUL(s[(i + 11) & 0x0f]) ^ Ft; R1_next = r2 + s[(i + 5) & 0x0f]; - r2 = SNOW2_T0[BYTE0(r1)] ^ SNOW2_T1[BYTE1(r1)] ^ SNOW2_T2[BYTE2(r1)] ^ SNOW2_T3[BYTE3(r1)]; + r2 = SNOW2_T0[BYTE0(r1)] ^ SNOW2_T1[BYTE1(r1)] ^ SNOW2_T2[BYTE2(r1)] ^ + SNOW2_T3[BYTE3(r1)]; r1 = R1_next; } @@ -355,13 +356,13 @@ int SDRM_SNOW2_Setup(SDRM_SNOW2Context *ctx, cc_u8 *UserKey, cc_u32 keyLen, cc_u } /* - * @fn SDRM_SNOW2_getKeyStream64 - * @brief get 64 byte key stream + * @fn SDRM_SNOW2_getKeyStream64 + * @brief get 64 byte key stream * - * @param ctx [out]crypto context - * @param keyStream64 [in]generated key stream + * @param ctx [out]crypto context + * @param keyStream64 [in]generated key stream * - * @return CRYPTO_SUCCESS if no error is occured + * @return CRYPTO_SUCCESS if no error is occured */ int SDRM_SNOW2_getKeyStream64(SDRM_SNOW2Context *ctx, cc_u32 *keyStream64) { @@ -369,11 +370,12 @@ int SDRM_SNOW2_getKeyStream64(SDRM_SNOW2Context *ctx, cc_u32 *keyStream64) cc_u32 *s = ctx->s; cc_u32 t = ctx->t; - for (i = t; i < t + 16; i++) - { - s[i & 0x0f] = a_MUL(s[i & 0x0f]) ^ s[(i + 2) & 0x0f] ^ ainv_MUL(s[(i + 11) & 0x0f]); + for (i = t; i < t + 16; i++) { + s[i & 0x0f] = a_MUL(s[i & 0x0f]) ^ s[(i + 2) & 0x0f] ^ ainv_MUL( + s[(i + 11) & 0x0f]); R1_next = ctx->r2 + s[(i + 5) & 0x0f]; - ctx->r2 = SNOW2_T0[BYTE0(ctx->r1)] ^ SNOW2_T1[BYTE1(ctx->r1)] ^ SNOW2_T2[BYTE2(ctx->r1)] ^ SNOW2_T3[BYTE3(ctx->r1)]; + ctx->r2 = SNOW2_T0[BYTE0(ctx->r1)] ^ SNOW2_T1[BYTE1(ctx->r1)] ^ + SNOW2_T2[BYTE2(ctx->r1)] ^ SNOW2_T3[BYTE3(ctx->r1)]; ctx->r1 = R1_next; keyStream64[i] = (ctx->r1 + s[i & 0x0f]) ^ ctx->r2 ^ s[(i + 1) & 0x0f]; @@ -386,13 +388,13 @@ int SDRM_SNOW2_getKeyStream64(SDRM_SNOW2Context *ctx, cc_u32 *keyStream64) /* - * @fn SDRM_SNOW2_getKeyStream - * @brief get 4 byte key stream + * @fn SDRM_SNOW2_getKeyStream + * @brief get 4 byte key stream * - * @param ctx [out]crypto context - * @param keyStream [in]generated key stream + * @param ctx [out]crypto context + * @param keyStream [in]generated key stream * - * @return CRYPTO_SUCCESS if no error is occured + * @return CRYPTO_SUCCESS if no error is occured */ int SDRM_SNOW2_getKeyStream(SDRM_SNOW2Context *ctx, cc_u32 *keyStream) { @@ -400,9 +402,11 @@ int SDRM_SNOW2_getKeyStream(SDRM_SNOW2Context *ctx, cc_u32 *keyStream) cc_u32 *s = ctx->s; cc_u32 t = ctx->t; - s[t & 0x0f] = a_MUL(s[t & 0x0f]) ^ s[(t + 2) & 0x0f] ^ ainv_MUL(s[(t + 11) & 0x0f]); + s[t & 0x0f] = a_MUL(s[t & 0x0f]) ^ s[(t + 2) & 0x0f] ^ ainv_MUL( + s[(t + 11) & 0x0f]); R1_next = ctx->r2 + s[(t + 5) & 0x0f]; - ctx->r2 = SNOW2_T0[BYTE0(ctx->r1)] ^ SNOW2_T1[BYTE1(ctx->r1)] ^ SNOW2_T2[BYTE2(ctx->r1)] ^ SNOW2_T3[BYTE3(ctx->r1)]; + ctx->r2 = SNOW2_T0[BYTE0(ctx->r1)] ^ SNOW2_T1[BYTE1(ctx->r1)] ^ + SNOW2_T2[BYTE2(ctx->r1)] ^ SNOW2_T3[BYTE3(ctx->r1)]; ctx->r1 = R1_next; *keyStream = (ctx->r1 + s[t & 0x0f]) ^ ctx->r2 ^ s[(t + 1) & 0x0f]; diff --git a/ssflib/dep/cryptocore/source/middle/cc_cmac.c b/ssflib/dep/cryptocore/source/middle/cc_cmac.c index 9c960ac..1e33d93 100644 --- a/ssflib/dep/cryptocore/source/middle/cc_cmac.c +++ b/ssflib/dep/cryptocore/source/middle/cc_cmac.c @@ -30,53 +30,50 @@ //////////////////////////////////////////////////////////////////////////// // Constants //////////////////////////////////////////////////////////////////////////// -/*! @brief max block columns */ -#define CMAC_MAXBC (256/32) +/*! @brief max block columns */ +#define CMAC_MAXBC (256/32) -/*! @brief max key columns */ -#define CMAC_MAXKC (256/32) +/*! @brief max key columns */ +#define CMAC_MAXKC (256/32) -/*! @brief max rounds */ -#define CMAC_MAXROUNDS 14 +/*! @brief max rounds */ +#define CMAC_MAXROUNDS 14 -/*! @brief constant - defined in OMAC1a(One-Key CBC MAC1, submitted by Iwata and Kurosawa) */ +/*! @brief constant - defined in OMAC1a(One-Key CBC MAC1, submitted by Iwata and Kurosawa) */ static cc_u8 R_b[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x87}; /* - * @fn int SDRM_CMAC_init(CryptoCoreContainer *crt, cc_u8 *Key, cc_u32 KeyLen) + * @fn int SDRM_CMAC_init(CryptoCoreContainer *crt, cc_u8 *Key, cc_u32 KeyLen) * - * @brief Parameter setting for mac code generation - * @param crt [out]crypto parameter - * @param Key [in]user key - * @param KeyLen [in]byte-length of Key + * @brief Parameter setting for mac code generation + * @param crt [out]crypto parameter + * @param Key [in]user key + * @param KeyLen [in]byte-length of Key * - * @return CRYPTO_SUCCESS if no error is occured - * \n CRYPTO_NULL_POINTER if Parameter is NULL - * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed + * @return CRYPTO_SUCCESS if no error is occured + * \n CRYPTO_NULL_POINTER if Parameter is NULL + * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed */ int SDRM_CMAC_init(CryptoCoreContainer *crt, cc_u8 *Key, cc_u32 KeyLen) { - cc_u8 *K1, *K2, temp[16] = {0}; - cc_u8 ZERO[16] = {0}; - int i; - cc_u32 *RoundKey; + cc_u8 *K1, *K2, temp[16] = {0}; + cc_u8 ZERO[16] = {0}; + int i; + cc_u32 *RoundKey; - if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->cmacctx == NULL) || (Key == NULL)) - { + if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->cmacctx == NULL) || + (Key == NULL)) return CRYPTO_NULL_POINTER; - } if (KeyLen != 16) - { return CRYPTO_INVALID_ARGUMENT; - } memset(crt->ctx->cmacctx->IV, 0, SDRM_AES_BLOCK_SIZ); crt->ctx->cmacctx->BlockLen = 0; - RoundKey = (cc_u32*)(void*)(crt->ctx->cmacctx->RoundKey); + RoundKey = (cc_u32 *)(void *)(crt->ctx->cmacctx->RoundKey); K1 = crt->ctx->cmacctx->K1; K2 = crt->ctx->cmacctx->K2; @@ -84,98 +81,87 @@ int SDRM_CMAC_init(CryptoCoreContainer *crt, cc_u8 *Key, cc_u32 KeyLen) SDRM_rijndaelEncrypt(RoundKey, 10, ZERO, temp); - if((temp[0] >> 7) == 0x00) // L << 1 - { + if ((temp[0] >> 7) == 0x00) { // L << 1 for (i = 0; i < 15; i++) - { - K1[i] = (temp[i] << 1) | (temp[i+1] >> 7); - } + K1[i] = (temp[i] << 1) | (temp[i + 1] >> 7); + K1[15] = temp[i] << 1; - } - else if ((temp[0] >> 7) == 0x01) - { + } else if ((temp[0] >> 7) == 0x01) { for (i = 0; i < 15; i++) - { - K1[i] = (temp[i] << 1) | (temp[i+1] >> 7); - } + K1[i] = (temp[i] << 1) | (temp[i + 1] >> 7); + K1[15] = temp[i] << 1; BlockXor(K1, K1, R_b); } - if((K1[0] >> 7) == 0x00) // K1 << 1 - { + if ((K1[0] >> 7) == 0x00) { // K1 << 1 for (i = 0; i < 15; i++) - { - K2[i] = (K1[i] << 1) | (K1[i+1] >> 7); - } + K2[i] = (K1[i] << 1) | (K1[i + 1] >> 7); + K2[15] = K1[i] << 1; - } - else if ((K1[0] >> 7) == 0x01) - { + } else if ((K1[0] >> 7) == 0x01) { for (i = 0; i < 15; i++) - { - K2[i] = (K1[i] << 1) | (K1[i+1] >> 7); - } + K2[i] = (K1[i] << 1) | (K1[i + 1] >> 7); + K2[15] = K1[i] << 1; BlockXor(K2, K2, R_b); } -// LOG4DRM_BUFFER(&CryptoLogCTX), LOG_DEBUG, "K1", K1, 16); -// LOG4DRM_BUFFER(&CryptoLogCTX), LOG_DEBUG, "K2", K2, 16); + // LOG4DRM_BUFFER(&CryptoLogCTX), LOG_DEBUG, "K1", K1, 16); + // LOG4DRM_BUFFER(&CryptoLogCTX), LOG_DEBUG, "K2", K2, 16); return CRYPTO_SUCCESS; } /* - * @fn int SDRM_CMAC_update(CryptoCoreContainer *crt, cc_u8 *msg, cc_u32 msgLen) - * @brief process data blocks + * @fn int SDRM_CMAC_update(CryptoCoreContainer *crt, cc_u8 *msg, cc_u32 msgLen) + * @brief process data blocks * - * @param crt [out]crypto parameter - * @param msg [in]data block - * @param msgLen [in]byte-length of Text + * @param crt [out]crypto parameter + * @param msg [in]data block + * @param msgLen [in]byte-length of Text * - * @return CRYPTO_SUCCESS if no error is occured - * \n CRYPTO_NULL_POINTER if Parameter is NULL - * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed + * @return CRYPTO_SUCCESS if no error is occured + * \n CRYPTO_NULL_POINTER if Parameter is NULL + * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed */ int SDRM_CMAC_update(CryptoCoreContainer *crt, cc_u8 *msg, cc_u32 msgLen) { - int Loop; - cc_u8 *ptr; + int Loop; + cc_u8 *ptr; if (msgLen == 0) - { return CRYPTO_SUCCESS; - } - if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->cmacctx == NULL) || (msg == NULL)) - { + if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->cmacctx == NULL) || + (msg == NULL)) return CRYPTO_NULL_POINTER; - } - if (msgLen + crt->ctx->cmacctx->BlockLen <= SDRM_AES_BLOCK_SIZ) - { + if (msgLen + crt->ctx->cmacctx->BlockLen <= SDRM_AES_BLOCK_SIZ) { memcpy(crt->ctx->cmacctx->Block + crt->ctx->cmacctx->BlockLen, msg, msgLen); crt->ctx->cmacctx->BlockLen += msgLen; return CRYPTO_SUCCESS; } - memcpy(crt->ctx->cmacctx->Block + crt->ctx->cmacctx->BlockLen, msg, SDRM_AES_BLOCK_SIZ - crt->ctx->cmacctx->BlockLen); - SDRM_CBC_Enc(ID_AES128, crt->ctx->cmacctx->IV, crt->ctx->cmacctx->Block, crt->ctx->cmacctx->RoundKey, crt->ctx->cmacctx->IV); + memcpy(crt->ctx->cmacctx->Block + crt->ctx->cmacctx->BlockLen, msg, + SDRM_AES_BLOCK_SIZ - crt->ctx->cmacctx->BlockLen); + SDRM_CBC_Enc(ID_AES128, crt->ctx->cmacctx->IV, crt->ctx->cmacctx->Block, + crt->ctx->cmacctx->RoundKey, crt->ctx->cmacctx->IV); Loop = (msgLen + crt->ctx->cmacctx->BlockLen - 1) / SDRM_AES_BLOCK_SIZ - 1; ptr = msg + SDRM_AES_BLOCK_SIZ - crt->ctx->cmacctx->BlockLen; - crt->ctx->cmacctx->BlockLen = (cc_u32)(msg + msgLen - ptr) - Loop * SDRM_AES_BLOCK_SIZ; + crt->ctx->cmacctx->BlockLen = (cc_u32)(msg + msgLen - ptr) - Loop * + SDRM_AES_BLOCK_SIZ; - while (Loop > 0) - { - SDRM_CBC_Enc(ID_AES128, crt->ctx->cmacctx->IV, ptr, crt->ctx->cmacctx->RoundKey, crt->ctx->cmacctx->IV); + while (Loop > 0) { + SDRM_CBC_Enc(ID_AES128, crt->ctx->cmacctx->IV, ptr, crt->ctx->cmacctx->RoundKey, + crt->ctx->cmacctx->IV); Loop--; ptr += SDRM_AES_BLOCK_SIZ; } -// LOG4DRM_BUFFER(&CryptoLogCTX), LOG_DEBUG, "Block", crt->ctx->cmacctx->IV, 16); + // LOG4DRM_BUFFER(&CryptoLogCTX), LOG_DEBUG, "Block", crt->ctx->cmacctx->IV, 16); memcpy(crt->ctx->cmacctx->Block, ptr, crt->ctx->cmacctx->BlockLen); @@ -184,85 +170,81 @@ int SDRM_CMAC_update(CryptoCoreContainer *crt, cc_u8 *msg, cc_u32 msgLen) /* - * @fn int SDRM_CMAC_final(CryptoCoreContainer *crt, cc_u8 *output, cc_u32 *outputLen) - * @brief process last data block + * @fn int SDRM_CMAC_final(CryptoCoreContainer *crt, cc_u8 *output, cc_u32 *outputLen) + * @brief process last data block * - * @param crt [in]crypto parameter - * @param output [out]generated MAC - * @param outputLen [out]byte-length of output + * @param crt [in]crypto parameter + * @param output [out]generated MAC + * @param outputLen [out]byte-length of output * - * @return CRYPTO_SUCCESS if no error is occured - * \n CRYPTO_NULL_POINTER if Parameter is NULL - * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed + * @return CRYPTO_SUCCESS if no error is occured + * \n CRYPTO_NULL_POINTER if Parameter is NULL + * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed */ int SDRM_CMAC_final(CryptoCoreContainer *crt, cc_u8 *output, cc_u32 *outputLen) { cc_u8 *K1, *K2; - if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->cmacctx == NULL) || (output == NULL)) - { + if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->cmacctx == NULL) || + (output == NULL)) return CRYPTO_NULL_POINTER; - } K1 = crt->ctx->cmacctx->K1; K2 = crt->ctx->cmacctx->K2; if (crt->ctx->cmacctx->BlockLen == SDRM_AES_BLOCK_SIZ) - { BlockXor(crt->ctx->cmacctx->Block, crt->ctx->cmacctx->Block, K1); - } - else - { + + else { crt->ctx->cmacctx->IV[crt->ctx->cmacctx->BlockLen] ^= 0x80; - BlockXor(crt->ctx->cmacctx->IV, crt->ctx->cmacctx->IV, K2); // input = input XOR K2 - memset(crt->ctx->cmacctx->Block + crt->ctx->cmacctx->BlockLen, 0, SDRM_AES_BLOCK_SIZ - crt->ctx->cmacctx->BlockLen); + BlockXor(crt->ctx->cmacctx->IV, crt->ctx->cmacctx->IV, + K2); // input = input XOR K2 + memset(crt->ctx->cmacctx->Block + crt->ctx->cmacctx->BlockLen, 0, + SDRM_AES_BLOCK_SIZ - crt->ctx->cmacctx->BlockLen); } - SDRM_CBC_Enc(ID_AES128, output, crt->ctx->cmacctx->Block, crt->ctx->cmacctx->RoundKey, crt->ctx->cmacctx->IV); + SDRM_CBC_Enc(ID_AES128, output, crt->ctx->cmacctx->Block, + crt->ctx->cmacctx->RoundKey, crt->ctx->cmacctx->IV); if (outputLen != NULL) - { *outputLen = 16; - } return CRYPTO_SUCCESS; } /* - * @fn int SDRM_CMAC_getMAC(CryptoCoreContainer *crt, cc_u8 *Key, cc_u32 KeyLen, cc_u8 *msg, cc_u32 msgLen, cc_u8 *output, cc_u32 *outputLen) - * @brief generate c-mac code + * @fn int SDRM_CMAC_getMAC(CryptoCoreContainer *crt, cc_u8 *Key, cc_u32 KeyLen, cc_u8 *msg, cc_u32 msgLen, cc_u8 *output, cc_u32 *outputLen) + * @brief generate c-mac code * - * @param crt [in]crypto parameter - * @param Key [in]user key - * @param KeyLen [in]byte-length of Key - * @param msg [in]data block - * @param msgLen [in]byte-length of Text - * @param output [out]generated MAC - * @param outputLen [out]byte-length of output + * @param crt [in]crypto parameter + * @param Key [in]user key + * @param KeyLen [in]byte-length of Key + * @param msg [in]data block + * @param msgLen [in]byte-length of Text + * @param output [out]generated MAC + * @param outputLen [out]byte-length of output * - * @return CRYPTO_SUCCESS if no error is occured + * @return CRYPTO_SUCCESS if no error is occured */ -int SDRM_CMAC_getMAC(CryptoCoreContainer *crt, cc_u8 *Key, cc_u32 KeyLen, cc_u8 *msg, cc_u32 msgLen, cc_u8 *output, cc_u32 *outputLen) +int SDRM_CMAC_getMAC(CryptoCoreContainer *crt, cc_u8 *Key, cc_u32 KeyLen, + cc_u8 *msg, cc_u32 msgLen, cc_u8 *output, cc_u32 *outputLen) { int result; - if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->cmacctx == NULL) || (Key == NULL) || (output == NULL)) - { + if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->cmacctx == NULL) || + (Key == NULL) || (output == NULL)) return CRYPTO_NULL_POINTER; - } result = SDRM_CMAC_init(crt, Key, KeyLen); + if (result != CRYPTO_SUCCESS) - { return result; - } result = SDRM_CMAC_update(crt, msg, msgLen); + if (result != CRYPTO_SUCCESS) - { - return result; - } + return result; return SDRM_CMAC_final(crt, output, outputLen); } diff --git a/ssflib/dep/cryptocore/source/middle/cc_dh.c b/ssflib/dep/cryptocore/source/middle/cc_dh.c index a7f087a..8f38c17 100644 --- a/ssflib/dep/cryptocore/source/middle/cc_dh.c +++ b/ssflib/dep/cryptocore/source/middle/cc_dh.c @@ -33,78 +33,70 @@ // Functions //////////////////////////////////////////////////////////////////////////// /** - * @fn SDRM_GenerateDHParam(CryptoCoreContainer* crt, unsigned char* pPrime, unsigned int nPrimeLen, unsigned int* pGenerator) - * @brief generate parameters for Diffie-Hellman protocol + * @fn SDRM_GenerateDHParam(CryptoCoreContainer* crt, unsigned char* pPrime, unsigned int nPrimeLen, unsigned int* pGenerator) + * @brief generate parameters for Diffie-Hellman protocol * - * @param [out] crt context - * @param [out] pPrime prime number - * @param [in] nPrimeLen size of pPrime buffer - * @param [out] pGenerator generator value + * @param [out] crt context + * @param [out] pPrime prime number + * @param [in] nPrimeLen size of pPrime buffer + * @param [out] pGenerator generator value * - * @return int + * @return int */ -int SDRM_GenerateDHParam(CryptoCoreContainer* crt, unsigned char* pPrime, unsigned int nPrimeLen, unsigned char* pGenerator) +int SDRM_GenerateDHParam(CryptoCoreContainer *crt, unsigned char *pPrime, + unsigned int nPrimeLen, unsigned char *pGenerator) { - SDRM_DHContext* ctx; + SDRM_DHContext *ctx; cc_u32 Seed[4]; int i, sp, t1; SDRM_BIG_NUM *p = NULL; SDRM_BIG_NUM *g = NULL; - if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->dhctx == NULL) || (pPrime == NULL) || (pGenerator == NULL)) - { + if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->dhctx == NULL) || + (pPrime == NULL) || (pGenerator == NULL)) return CRYPTO_NULL_POINTER; - } ctx = crt->ctx->dhctx; p = SDRM_BN_Init(nPrimeLen / 2 + 1); + if (p == NULL) - { return CRYPTO_MEMORY_ALLOC_FAIL; - } g = SDRM_BN_Init(nPrimeLen / 2 + 1); - if (g == NULL) - { + + if (g == NULL) { free(p); return CRYPTO_MEMORY_ALLOC_FAIL; } for (i = 0; i < 4; i++) - { Seed[i] = (rand() << 16) ^ rand(); - } t1 = (nPrimeLen * 8 - 1) % 32; //set security parameter for miller-rabin probabilistic primality test if (nPrimeLen >= 128) - { sp = 3; - } + else if (nPrimeLen >= 64) - { sp = 5; - } + else if (nPrimeLen >= 15) - { sp = 15; - } + else - { sp = 30; - } //generate p p->Length = (nPrimeLen + 3) / 4; + do { - SDRM_RNG_X931((cc_u8 *)Seed, nPrimeLen * 8, (cc_u8*)p->pData); + SDRM_RNG_X931((cc_u8 *)Seed, nPrimeLen * 8, (cc_u8 *)p->pData); p->pData[0] |= 1L; p->pData[p->Length - 1] &= ~((-1L) << t1); p->pData[p->Length - 1] |= (1L << t1); - } - while(SDRM_BN_MILLER_RABIN(p, sp) != CRYPTO_ISPRIME); + } while (SDRM_BN_MILLER_RABIN(p, sp) != CRYPTO_ISPRIME); SDRM_I2OSP(p, nPrimeLen, pPrime); @@ -114,15 +106,13 @@ int SDRM_GenerateDHParam(CryptoCoreContainer* crt, unsigned char* pPrime, unsign SDRM_OS2BN(pGenerator, nPrimeLen, g); if (ctx->p != NULL) - { free(ctx->p); - } + ctx->p = p; if (ctx->g != NULL) - { free(ctx->g); - } + ctx->g = g; ctx->PrimeLen = nPrimeLen; @@ -131,38 +121,37 @@ int SDRM_GenerateDHParam(CryptoCoreContainer* crt, unsigned char* pPrime, unsign } /** - * @fn SDRM_SetDHParam(CryptoCoreContainer* crt, unsigned char* pPrime, unsigned int nPrimeLen, unsigned int pGenerator) - * @brief set parameters for Diffie-Hellman protocol + * @fn SDRM_SetDHParam(CryptoCoreContainer* crt, unsigned char* pPrime, unsigned int nPrimeLen, unsigned int pGenerator) + * @brief set parameters for Diffie-Hellman protocol * - * @param [out] crt context - * @param [in] pPrime prime number - * @param [in] nPrimeLen size of pPrime buffer - * @param [in] pGenerator generator value + * @param [out] crt context + * @param [in] pPrime prime number + * @param [in] nPrimeLen size of pPrime buffer + * @param [in] pGenerator generator value * - * @return int + * @return int */ -int SDRM_SetDHParam(CryptoCoreContainer* crt, unsigned char* pPrime, unsigned int nPrimeLen, unsigned char* pGenerator, unsigned int nGeneratorLen) +int SDRM_SetDHParam(CryptoCoreContainer *crt, unsigned char *pPrime, + unsigned int nPrimeLen, unsigned char *pGenerator, unsigned int nGeneratorLen) { - SDRM_DHContext* ctx; - SDRM_BIG_NUM* p = NULL; - SDRM_BIG_NUM* g = NULL; + SDRM_DHContext *ctx; + SDRM_BIG_NUM *p = NULL; + SDRM_BIG_NUM *g = NULL; - if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->dhctx == NULL) || (pPrime == NULL) || (pGenerator == NULL)) - { + if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->dhctx == NULL) || + (pPrime == NULL) || (pGenerator == NULL)) return CRYPTO_NULL_POINTER; - } ctx = crt->ctx->dhctx; p = SDRM_BN_Init(nPrimeLen / 2 + 1); + if (p == NULL) - { return CRYPTO_MEMORY_ALLOC_FAIL; - } g = SDRM_BN_Init(nPrimeLen / 2 + 1); - if (g == NULL) - { + + if (g == NULL) { free(p); return CRYPTO_MEMORY_ALLOC_FAIL; } @@ -178,56 +167,54 @@ int SDRM_SetDHParam(CryptoCoreContainer* crt, unsigned char* pPrime, unsigned in } /** - * @fn SDRM_GenerateDHPrivate(CryptoCoreContainer* crt, unsigned char* pPub) - * @brief generate private value and calculate public value + * @fn SDRM_GenerateDHPrivate(CryptoCoreContainer* crt, unsigned char* pPub) + * @brief generate private value and calculate public value * - * @param [in] crt context - * @param [out] pPriv private value - * @param [out] pPub public value + * @param [in] crt context + * @param [out] pPriv private value + * @param [out] pPub public value * - * @return int + * @return int */ -int SDRM_GenerateDHPrivate(CryptoCoreContainer* crt, unsigned char* pPriv, unsigned char* pPub) +int SDRM_GenerateDHPrivate(CryptoCoreContainer *crt, unsigned char *pPriv, + unsigned char *pPub) { - SDRM_DHContext* ctx; + SDRM_DHContext *ctx; cc_u32 Seed[4] = {0,}; int retVal; - SDRM_BIG_NUM* priv = NULL; - SDRM_BIG_NUM* pub = NULL; + SDRM_BIG_NUM *priv = NULL; + SDRM_BIG_NUM *pub = NULL; - if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->dhctx == NULL) || (pPriv == NULL) || (pPub == NULL)) - { + if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->dhctx == NULL) || + (pPriv == NULL) || (pPub == NULL)) return CRYPTO_NULL_POINTER; - } ctx = crt->ctx->dhctx; priv = SDRM_BN_Init(ctx->PrimeLen / 2 + 1); + if (priv == NULL) - { return CRYPTO_MEMORY_ALLOC_FAIL; - } pub = SDRM_BN_Init(ctx->PrimeLen / 2 + 1); - if (pub == NULL) - { + + if (pub == NULL) { free(priv); return CRYPTO_MEMORY_ALLOC_FAIL; } //generate priv priv->Length = (ctx->PrimeLen + 3) / 4; - SDRM_RNG_X931((cc_u8 *)Seed, ctx->PrimeLen * 8, (cc_u8*)priv->pData); + SDRM_RNG_X931((cc_u8 *)Seed, ctx->PrimeLen * 8, (cc_u8 *)priv->pData); SDRM_BN_ModRed(priv, priv, ctx->p); #ifndef _OP64_NOTSUPPORTED retVal = SDRM_BN_ModExp2(pub, ctx->g, priv, ctx->p); #else retVal = SDRM_BN_ModExp(pub, ctx->g, priv, ctx->p); -#endif //_OP64_NOTSUPPORTED +#endif //_OP64_NOTSUPPORTED - if (retVal != CRYPTO_SUCCESS) - { + if (retVal != CRYPTO_SUCCESS) { free(priv); free(pub); @@ -244,47 +231,46 @@ int SDRM_GenerateDHPrivate(CryptoCoreContainer* crt, unsigned char* pPriv, unsig } /** - * @fn SDRM_GetDHSharedSecret(CryptoCoreContainer* crt, unsigned char* pPriv, unsigned char* pPub, unsigned char* pSharedSecret) - * @brief calculate shared secret + * @fn SDRM_GetDHSharedSecret(CryptoCoreContainer* crt, unsigned char* pPriv, unsigned char* pPub, unsigned char* pSharedSecret) + * @brief calculate shared secret * - * @param [in] crt context - * @param [in] Priv private value - * @param [in] pPub guest's public value - * @param [out] pSharedSecret public value + * @param [in] crt context + * @param [in] Priv private value + * @param [in] pPub guest's public value + * @param [out] pSharedSecret public value * - * @return int + * @return int */ -int SDRM_GetDHSharedSecret(CryptoCoreContainer* crt, unsigned char* pPriv, unsigned char* pPub, unsigned char* pSharedSecret) +int SDRM_GetDHSharedSecret(CryptoCoreContainer *crt, unsigned char *pPriv, + unsigned char *pPub, unsigned char *pSharedSecret) { - SDRM_DHContext* ctx; - SDRM_BIG_NUM* priv = NULL; - SDRM_BIG_NUM* pub = NULL; - SDRM_BIG_NUM* SharedSecret = NULL; + SDRM_DHContext *ctx; + SDRM_BIG_NUM *priv = NULL; + SDRM_BIG_NUM *pub = NULL; + SDRM_BIG_NUM *SharedSecret = NULL; int retVal; - if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->dhctx == NULL) || (pPriv == NULL) || (pPub == NULL) || (pSharedSecret == NULL)) - { + if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->dhctx == NULL) || + (pPriv == NULL) || (pPub == NULL) || (pSharedSecret == NULL)) return CRYPTO_NULL_POINTER; - } ctx = crt->ctx->dhctx; priv = SDRM_BN_Init(ctx->PrimeLen / 2 + 1); + if (priv == NULL) - { return CRYPTO_MEMORY_ALLOC_FAIL; - } pub = SDRM_BN_Init(ctx->PrimeLen / 2 + 1); - if (pub == NULL) - { + + if (pub == NULL) { free(priv); return CRYPTO_MEMORY_ALLOC_FAIL; } SharedSecret = SDRM_BN_Init(ctx->PrimeLen / 2 + 1); - if (SharedSecret == NULL) - { + + if (SharedSecret == NULL) { free(priv); free(pub); return CRYPTO_MEMORY_ALLOC_FAIL; @@ -297,10 +283,9 @@ int SDRM_GetDHSharedSecret(CryptoCoreContainer* crt, unsigned char* pPriv, unsig retVal = SDRM_BN_ModExp2(SharedSecret, pub, priv, ctx->p); #else retVal = SDRM_BN_ModExp(SharedSecret, pub, priv, ctx->p); -#endif //_OP64_NOTSUPPORTED +#endif //_OP64_NOTSUPPORTED - if (retVal != CRYPTO_SUCCESS) - { + if (retVal != CRYPTO_SUCCESS) { free(priv); free(pub); free(SharedSecret); @@ -318,24 +303,19 @@ int SDRM_GetDHSharedSecret(CryptoCoreContainer* crt, unsigned char* pPriv, unsig } /** - * @fn SDRM_FreeDHContext(CryptoCoreContainer* crt) - * @brief free context buffer + * @fn SDRM_FreeDHContext(CryptoCoreContainer* crt) + * @brief free context buffer * - * @param [in] crt context + * @param [in] crt context */ -void SDRM_FreeDHContext(SDRM_DHContext* ctx) +void SDRM_FreeDHContext(SDRM_DHContext *ctx) { - if (ctx != NULL) - { + if (ctx != NULL) { if (ctx->p != NULL) - { free(ctx->p); - } if (ctx->g != NULL) - { free(ctx->g); - } memset(ctx, 0x00, sizeof(SDRM_DHContext)); } diff --git a/ssflib/dep/cryptocore/source/middle/cc_dsa.c b/ssflib/dep/cryptocore/source/middle/cc_dsa.c index b8bb689..da53040 100644 --- a/ssflib/dep/cryptocore/source/middle/cc_dsa.c +++ b/ssflib/dep/cryptocore/source/middle/cc_dsa.c @@ -33,37 +33,33 @@ // Functions //////////////////////////////////////////////////////////////////////////// /* - * @fn SDRM_Add_DW2BA - * @brief Add a UINT32 value to a Byte Array - * function works correctly only when dLen >= 4 + * @fn SDRM_Add_DW2BA + * @brief Add a UINT32 value to a Byte Array + * function works correctly only when dLen >= 4 * - * @param BA [i/o]byte array - * @param dLen [in]byte-length of BA - * @param val [in]value to add + * @param BA [i/o]byte array + * @param dLen [in]byte-length of BA + * @param val [in]value to add * - * @return void + * @return void */ -void SDRM_Add_DW2BA(cc_u8* BA, cc_u32 dLen, cc_u32 val) +void SDRM_Add_DW2BA(cc_u8 *BA, cc_u32 dLen, cc_u32 val) { cc_u32 i, DIGIT = 0; - if (dLen >= 4) - { - DIGIT = BA[dLen - 4] ^ (BA[dLen - 3] << 8) ^ (BA[dLen - 2] << 16) ^ (BA[dLen - 1] << 24); + if (dLen >= 4) { + DIGIT = BA[dLen - 4] ^ (BA[dLen - 3] << 8) ^ (BA[dLen - 2] << 16) ^ + (BA[dLen - 1] << 24); DIGIT += val; - BA[dLen - 4] = (cc_u8)(DIGIT ) & 0xff; - BA[dLen - 3] = (cc_u8)(DIGIT >> 8 ) & 0xff; + BA[dLen - 4] = (cc_u8)(DIGIT) & 0xff; + BA[dLen - 3] = (cc_u8)(DIGIT >> 8) & 0xff; BA[dLen - 2] = (cc_u8)(DIGIT >> 16) & 0xff; BA[dLen - 1] = (cc_u8)(DIGIT >> 24) & 0xff; - if (DIGIT < val) - { - for (i = dLen - 5; i != (cc_u32)-1; i--) - { + if (DIGIT < val) { + for (i = dLen - 5; i != (cc_u32) - 1; i--) { if (++BA[i] != 0) - { return; - } } } } @@ -72,61 +68,64 @@ void SDRM_Add_DW2BA(cc_u8* BA, cc_u32 dLen, cc_u32 val) } /* - * @fn SDRM_DSA_InitCrt - * @brief generate DSA Context + * @fn SDRM_DSA_InitCrt + * @brief generate DSA Context * - * @return pointer to the generated context - * \n NULL if memory allocation is failed + * @return pointer to the generated context + * \n NULL if memory allocation is failed */ SDRM_DSAContext *SDRM_DSA_InitCrt() { SDRM_DSAContext *ctx; - cc_u8 *pbBuf = (cc_u8*)malloc(sizeof(SDRM_DSAContext) + SDRM_DSA_ALLOC_SIZE * 5); + cc_u8 *pbBuf = (cc_u8 *)malloc(sizeof(SDRM_DSAContext) + + SDRM_DSA_ALLOC_SIZE * 5); if (pbBuf == NULL) - { return NULL; - } - ctx = (SDRM_DSAContext*)(void*)pbBuf; - ctx->p = SDRM_BN_Alloc((cc_u8*)ctx + sizeof(SDRM_DSAContext), SDRM_DSA_BN_BUFSIZE); - ctx->q = SDRM_BN_Alloc((cc_u8*)ctx->p + SDRM_DSA_ALLOC_SIZE, SDRM_DSA_BN_BUFSIZE); - ctx->al = SDRM_BN_Alloc((cc_u8*)ctx->q + SDRM_DSA_ALLOC_SIZE, SDRM_DSA_BN_BUFSIZE); - ctx->y = SDRM_BN_Alloc((cc_u8*)ctx->al + SDRM_DSA_ALLOC_SIZE, SDRM_DSA_BN_BUFSIZE); - ctx->a = SDRM_BN_Alloc((cc_u8*)ctx->y + SDRM_DSA_ALLOC_SIZE, SDRM_DSA_BN_BUFSIZE); + ctx = (SDRM_DSAContext *)(void *)pbBuf; + ctx->p = SDRM_BN_Alloc((cc_u8 *)ctx + sizeof(SDRM_DSAContext), + SDRM_DSA_BN_BUFSIZE); + ctx->q = SDRM_BN_Alloc((cc_u8 *)ctx->p + SDRM_DSA_ALLOC_SIZE, + SDRM_DSA_BN_BUFSIZE); + ctx->al = SDRM_BN_Alloc((cc_u8 *)ctx->q + SDRM_DSA_ALLOC_SIZE, + SDRM_DSA_BN_BUFSIZE); + ctx->y = SDRM_BN_Alloc((cc_u8 *)ctx->al + SDRM_DSA_ALLOC_SIZE, + SDRM_DSA_BN_BUFSIZE); + ctx->a = SDRM_BN_Alloc((cc_u8 *)ctx->y + SDRM_DSA_ALLOC_SIZE, + SDRM_DSA_BN_BUFSIZE); return ctx; } /* - * @fn int SDRM_DSA_SetParam(CryptoCoreContainer *crt, - cc_u8 *DSA_P_Data, cc_u32 DSA_P_Len, - cc_u8 *DSA_Q_Data, cc_u32 DSA_Q_Len, - cc_u8 *DSA_G_Data, cc_u32 DSA_G_Len) - * @brief set DSA parameters + * @fn int SDRM_DSA_SetParam(CryptoCoreContainer *crt, + cc_u8 *DSA_P_Data, cc_u32 DSA_P_Len, + cc_u8 *DSA_Q_Data, cc_u32 DSA_Q_Len, + cc_u8 *DSA_G_Data, cc_u32 DSA_G_Len) + * @brief set DSA parameters * - * @param crt [out]dsa context - * @param DSA_P_Data [in]octet string of p value - * @param DSA_P_Len [in]legnth of p_val - * @param DSA_Q_Data [in]octet string of q value - * @param DSA_Q_Len [in]legnth of q_val - * @param DSA_G_Data [in]octet string of al value - * @param DSA_G_Len [in]legnth of al_val + * @param crt [out]dsa context + * @param DSA_P_Data [in]octet string of p value + * @param DSA_P_Len [in]legnth of p_val + * @param DSA_Q_Data [in]octet string of q value + * @param DSA_Q_Len [in]legnth of q_val + * @param DSA_G_Data [in]octet string of al value + * @param DSA_G_Len [in]legnth of al_val * - * @return CRYPTO_SUCCESS if no error is occured - * \n CRYPTO_NULL_POINTER if input parameter pointer is null - * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed - * \n CRYPTO_ERROR if conversion is failed + * @return CRYPTO_SUCCESS if no error is occured + * \n CRYPTO_NULL_POINTER if input parameter pointer is null + * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed + * \n CRYPTO_ERROR if conversion is failed */ int SDRM_DSA_SetParam(CryptoCoreContainer *crt, - cc_u8 *DSA_P_Data, cc_u32 DSA_P_Len, - cc_u8 *DSA_Q_Data, cc_u32 DSA_Q_Len, - cc_u8 *DSA_G_Data, cc_u32 DSA_G_Len) + cc_u8 *DSA_P_Data, cc_u32 DSA_P_Len, + cc_u8 *DSA_Q_Data, cc_u32 DSA_Q_Len, + cc_u8 *DSA_G_Data, cc_u32 DSA_G_Len) { - if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->dsactx == NULL) || (DSA_P_Data == NULL) || (DSA_Q_Data == NULL) || (DSA_G_Data == NULL)) - { + if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->dsactx == NULL) || + (DSA_P_Data == NULL) || (DSA_Q_Data == NULL) || (DSA_G_Data == NULL)) return CRYPTO_NULL_POINTER; - } SDRM_OS2BN(DSA_P_Data, DSA_P_Len, crt->ctx->dsactx->p); SDRM_BN_OPTIMIZE_LENGTH(crt->ctx->dsactx->p); @@ -142,39 +141,35 @@ int SDRM_DSA_SetParam(CryptoCoreContainer *crt, /* - * @fn int SDRM_DSA_SetKeyPair(CryptoCoreContainer *crt, - cc_u8 *DSA_Y_Data, cc_u32 DSA_Y_Len, - cc_u8 *DSA_X_Data, cc_u32 DSA_X_Len) - * @brief set DSA parameters + * @fn int SDRM_DSA_SetKeyPair(CryptoCoreContainer *crt, + cc_u8 *DSA_Y_Data, cc_u32 DSA_Y_Len, + cc_u8 *DSA_X_Data, cc_u32 DSA_X_Len) + * @brief set DSA parameters * - * @param crt [out]dsa context - * @param DSA_Y_Data [in]octet string of y value - * @param DSA_Y_Len [in]legnth of y_val - * @param DSA_X_Data [in]octet string of a value - * @param DSA_X_Len [in]legnth of a_val + * @param crt [out]dsa context + * @param DSA_Y_Data [in]octet string of y value + * @param DSA_Y_Len [in]legnth of y_val + * @param DSA_X_Data [in]octet string of a value + * @param DSA_X_Len [in]legnth of a_val * - * @return CRYPTO_SUCCESS if no error is occured - * \n CRYPTO_NULL_POINTER if input parameter pointer is null - * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed - * \n CRYPTO_ERROR if conversion is failed + * @return CRYPTO_SUCCESS if no error is occured + * \n CRYPTO_NULL_POINTER if input parameter pointer is null + * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed + * \n CRYPTO_ERROR if conversion is failed */ int SDRM_DSA_SetKeyPair(CryptoCoreContainer *crt, - cc_u8 *DSA_Y_Data, cc_u32 DSA_Y_Len, - cc_u8 *DSA_X_Data, cc_u32 DSA_X_Len) + cc_u8 *DSA_Y_Data, cc_u32 DSA_Y_Len, + cc_u8 *DSA_X_Data, cc_u32 DSA_X_Len) { if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->dsactx == NULL)) - { return CRYPTO_NULL_POINTER; - } - if (DSA_Y_Data != NULL) - { + if (DSA_Y_Data != NULL) { SDRM_OS2BN(DSA_Y_Data, DSA_Y_Len, crt->ctx->dsactx->y); SDRM_BN_OPTIMIZE_LENGTH(crt->ctx->dsactx->y); } - if (DSA_X_Data != NULL) - { + if (DSA_X_Data != NULL) { SDRM_OS2BN(DSA_X_Data, DSA_X_Len, crt->ctx->dsactx->a); SDRM_BN_OPTIMIZE_LENGTH(crt->ctx->dsactx->a); } @@ -183,76 +178,69 @@ int SDRM_DSA_SetKeyPair(CryptoCoreContainer *crt, } /* - * @fn int SDRM_DSA_GenParam(CryptoCoreContainer *crt, cc_u32 T_Siz, - cc_u8 *DSA_P_Data, cc_u32 *DSA_P_Len, - cc_u8 *DSA_Q_Data, cc_u32 *DSA_Q_Len, - cc_u8 *DSA_G_Data, cc_u32 *DSA_G_Len) - * @brief generate and set DSA parameters + * @fn int SDRM_DSA_GenParam(CryptoCoreContainer *crt, cc_u32 T_Siz, + cc_u8 *DSA_P_Data, cc_u32 *DSA_P_Len, + cc_u8 *DSA_Q_Data, cc_u32 *DSA_Q_Len, + cc_u8 *DSA_G_Data, cc_u32 *DSA_G_Len) + * @brief generate and set DSA parameters * - * @param crt [out]dsa context - * @param T_Siz [in]fix the length of p to 512 + 64t bit (0 <= T_Siz <= 8) - * @param DSA_P_Data [out]octet string of p value - * @param DSA_P_Len [out]legnth of p_val - * @param DSA_Q_Data [out]octet string of q value - * @param DSA_Q_Len [out]legnth of q_val - * @param DSA_G_Data [out]octet string of al value - * @param DSA_G_Len [out]legnth of al_val + * @param crt [out]dsa context + * @param T_Siz [in]fix the length of p to 512 + 64t bit (0 <= T_Siz <= 8) + * @param DSA_P_Data [out]octet string of p value + * @param DSA_P_Len [out]legnth of p_val + * @param DSA_Q_Data [out]octet string of q value + * @param DSA_Q_Len [out]legnth of q_val + * @param DSA_G_Data [out]octet string of al value + * @param DSA_G_Len [out]legnth of al_val * - * @return CRYPTO_SUCCESS if no error is occured - * \n CRYPTO_NULL_POINTER if input parameter pointer is null - * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed - * \n CRYPTO_ERROR if conversion is failed + * @return CRYPTO_SUCCESS if no error is occured + * \n CRYPTO_NULL_POINTER if input parameter pointer is null + * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed + * \n CRYPTO_ERROR if conversion is failed */ int SDRM_DSA_GenParam(CryptoCoreContainer *crt, cc_u32 T_Siz, - cc_u8 *DSA_P_Data, cc_u32 *DSA_P_Len, - cc_u8 *DSA_Q_Data, cc_u32 *DSA_Q_Len, - cc_u8 *DSA_G_Data, cc_u32 *DSA_G_Len) + cc_u8 *DSA_P_Data, cc_u32 *DSA_P_Len, + cc_u8 *DSA_Q_Data, cc_u32 *DSA_Q_Len, + cc_u8 *DSA_G_Data, cc_u32 *DSA_G_Len) { - cc_u32 i, k, L, n/*, g*/; - cc_u8 pbTemp[260], pbSeed[64]; + cc_u32 i, k, L, n/*, g*/; + cc_u8 pbTemp[260], pbSeed[64]; SDRM_SHA1Context ctx; - SDRM_BIG_NUM /**BN_A, */*BN_G, *BN_P, *BN_Q, *BN_AL, *BN_Temp; - cc_u8 *pbBuf = NULL; + SDRM_BIG_NUM /**BN_A, */*BN_G, *BN_P, *BN_Q, *BN_AL, *BN_Temp; + cc_u8 *pbBuf = NULL; if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->dsactx == NULL)) - { return CRYPTO_NULL_POINTER; - } if (T_Siz > 8) - { return CRYPTO_INVALID_ARGUMENT; - } L = 512 + 64 * T_Siz; n = (L - 1) / 160; -// g = (L - 1) % 160; + // g = (L - 1) % 160; + + pbBuf = (cc_u8 *)malloc(SDRM_DSA_ALLOC_SIZE * 2); - pbBuf = (cc_u8*)malloc(SDRM_DSA_ALLOC_SIZE * 2); if (pbBuf == NULL) - { return CRYPTO_MEMORY_ALLOC_FAIL; - } - BN_G = SDRM_BN_Alloc((cc_u8*)pbBuf, SDRM_DSA_BN_BUFSIZE); - BN_Temp = SDRM_BN_Alloc((cc_u8*)BN_G + SDRM_DSA_ALLOC_SIZE, SDRM_DSA_BN_BUFSIZE); + BN_G = SDRM_BN_Alloc((cc_u8 *)pbBuf, SDRM_DSA_BN_BUFSIZE); + BN_Temp = SDRM_BN_Alloc((cc_u8 *)BN_G + SDRM_DSA_ALLOC_SIZE, + SDRM_DSA_BN_BUFSIZE); BN_P = crt->ctx->dsactx->p; BN_Q = crt->ctx->dsactx->q; BN_AL = crt->ctx->dsactx->al; -// BN_A = crt->ctx->dsactx->a; + // BN_A = crt->ctx->dsactx->a; //generate p and q - while(1) - { - do - { + while (1) { + do { //choose a random seed s of bitlength g >= 160 for (i = 0; i < SDRM_SHA1_BLOCK_SIZ; i++) - { pbSeed[i] = (rand() << 16) ^ rand(); - } + pbSeed[0] |= 0x80; pbSeed[SDRM_SHA1_BLOCK_SIZ - 1] |= 0x01; @@ -267,24 +255,21 @@ int SDRM_DSA_GenParam(CryptoCoreContainer *crt, cc_u32 T_Siz, SDRM_SHA1_Update(&ctx, pbSeed, SDRM_SHA1_BLOCK_SIZ); SDRM_SHA1_Final(&ctx, pbTemp + SDRM_SHA1_BLOCK_SIZ); - for (i = 0; i < SDRM_SHA1_BLOCK_SIZ / sizeof(cc_u32); i++) - { - ((cc_u32*)(void*)pbTemp)[i] ^= ((cc_u32*)(void*)pbTemp)[i + SDRM_SHA1_BLOCK_SIZ / sizeof(cc_u32)]; + for (i = 0; i < SDRM_SHA1_BLOCK_SIZ / sizeof(cc_u32); i++) { + ((cc_u32 *)(void *)pbTemp)[i] ^= ((cc_u32 *)(void *) + pbTemp)[i + SDRM_SHA1_BLOCK_SIZ / sizeof(cc_u32)]; } pbTemp[0] |= 0x80; pbTemp[SDRM_SHA1_BLOCK_SIZ - 1] |= 0x01; SDRM_OS2BN(pbTemp, SDRM_SHA1_BLOCK_SIZ, BN_Q); - } - while(SDRM_BN_MILLER_RABIN(BN_Q, 18) != CRYPTO_SUCCESS); + } while (SDRM_BN_MILLER_RABIN(BN_Q, 18) != CRYPTO_SUCCESS); SDRM_INC_BA(pbSeed, SDRM_SHA1_BLOCK_SIZ); - for (i = 0; i < 4096; i++) - { - for (k = 0; k <= n; k++) - { + for (i = 0; i < 4096; i++) { + for (k = 0; k <= n; k++) { SDRM_SHA1_Init(&ctx); SDRM_SHA1_Update(&ctx, pbSeed, SDRM_SHA1_BLOCK_SIZ); SDRM_SHA1_Final(&ctx, pbTemp + (n - k) * SDRM_SHA1_BLOCK_SIZ); @@ -300,21 +285,17 @@ int SDRM_DSA_GenParam(CryptoCoreContainer *crt, cc_u32 T_Siz, SDRM_BN_Sub(BN_P, BN_P, BN_Temp); SDRM_BN_Add(BN_P, BN_P, BN_One); - if (SDRM_CheckBitUINT32(BN_P->pData, L - 1)) - { + if (SDRM_CheckBitUINT32(BN_P->pData, L - 1)) { if (SDRM_BN_MILLER_RABIN(BN_P, 5) == CRYPTO_ISPRIME) - { goto SUCCESS; - } + else - { break; - } } } } -SUCCESS : +SUCCESS: //select a generator al(alpha) of the unique cyclic group of order q in Zp SDRM_BN_Clr(BN_Temp); //temp = (p-1)/q @@ -324,47 +305,33 @@ SUCCESS : do { //select an element g excluded in Zp* do { - SDRM_RNG_X931(pbSeed, L, (cc_u8*)BN_G->pData); + SDRM_RNG_X931(pbSeed, L, (cc_u8 *)BN_G->pData); BN_G->Length = L / 32 + 1; SDRM_BN_OPTIMIZE_LENGTH(BN_G); - } - while(SDRM_BN_Cmp(BN_G, BN_P) >= 0); + } while (SDRM_BN_Cmp(BN_G, BN_P) >= 0); //al(alpha) = g^temp mod p SDRM_BN_ModExp(BN_AL, BN_G, BN_Temp, BN_P); - } - while (SDRM_BN_Cmp(BN_AL, BN_One) == 0); + } while (SDRM_BN_Cmp(BN_AL, BN_One) == 0); //write output if (DSA_P_Data != NULL) - { SDRM_I2OSP(BN_P, L / 8, DSA_P_Data); - } if (DSA_P_Len != NULL) - { *DSA_P_Len = L / 8; - } if (DSA_Q_Data != NULL) - { SDRM_I2OSP(BN_Q, 20, DSA_Q_Data); - } if (DSA_Q_Len != NULL) - { *DSA_Q_Len = 20; - } if (DSA_G_Data != NULL) - { SDRM_I2OSP(BN_AL, BN_AL->Length * 4, DSA_G_Data); - } if (DSA_G_Len != NULL) - { *DSA_G_Len = BN_AL->Length * 4; - } free(pbBuf); @@ -372,116 +339,109 @@ SUCCESS : } /* - * @fn int SDRM_DSA_GenKeypair(CryptoCoreContainer *crt, - cc_u8 *DSA_Y_Data, cc_u32 *DSA_Y_Len, - cc_u8 *DSA_X_Data, cc_u32 *DSA_X_Len) - * @brief generate and set DSA parameters + * @fn int SDRM_DSA_GenKeypair(CryptoCoreContainer *crt, + cc_u8 *DSA_Y_Data, cc_u32 *DSA_Y_Len, + cc_u8 *DSA_X_Data, cc_u32 *DSA_X_Len) + * @brief generate and set DSA parameters * - * @param crt [out]dsa context - * @param DSA_Y_Data [out]octet string of y value - * @param DSA_Y_Len [out]legnth of y_val - * @param DSA_X_Data [out]octet string of a value - * @param DSA_X_Len [out]legnth of a_val + * @param crt [out]dsa context + * @param DSA_Y_Data [out]octet string of y value + * @param DSA_Y_Len [out]legnth of y_val + * @param DSA_X_Data [out]octet string of a value + * @param DSA_X_Len [out]legnth of a_val * - * @return CRYPTO_SUCCESS if no error is occured - * \n CRYPTO_NULL_POINTER if input parameter pointer is null - * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed - * \n CRYPTO_ERROR if conversion is failed + * @return CRYPTO_SUCCESS if no error is occured + * \n CRYPTO_NULL_POINTER if input parameter pointer is null + * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed + * \n CRYPTO_ERROR if conversion is failed */ int SDRM_DSA_GenKeypair(CryptoCoreContainer *crt, - cc_u8 *DSA_Y_Data, cc_u32 *DSA_Y_Len, - cc_u8 *DSA_X_Data, cc_u32 *DSA_X_Len) + cc_u8 *DSA_Y_Data, cc_u32 *DSA_Y_Len, + cc_u8 *DSA_X_Data, cc_u32 *DSA_X_Len) { SDRM_BIG_NUM *BN_A; - cc_u32 Seed[4], i; + cc_u32 Seed[4], i; if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->dsactx == NULL)) - { return CRYPTO_NULL_POINTER; - } BN_A = crt->ctx->dsactx->a; for (i = 0; i < 4; i++) - { Seed[i] = (rand() << 16) ^ rand(); - } //Select a random integer a such that 1 <= a <= q-1 do { - SDRM_RNG_X931((cc_u8*)Seed, 160, (cc_u8*)BN_A->pData); - BN_A->Length = 6; //6 = 160 / 32 + 1 + SDRM_RNG_X931((cc_u8 *)Seed, 160, (cc_u8 *)BN_A->pData); + BN_A->Length = 6; //6 = 160 / 32 + 1 SDRM_BN_OPTIMIZE_LENGTH(BN_A); - } - while(SDRM_BN_Cmp(BN_A, crt->ctx->dsactx->q) >= 0); + } while (SDRM_BN_Cmp(BN_A, crt->ctx->dsactx->q) >= 0); //y = al ^ a mod p - SDRM_BN_ModExp(crt->ctx->dsactx->y, crt->ctx->dsactx->al, BN_A, crt->ctx->dsactx->p); + SDRM_BN_ModExp(crt->ctx->dsactx->y, crt->ctx->dsactx->al, BN_A, + crt->ctx->dsactx->p); //write output if (DSA_Y_Data != NULL) - { SDRM_I2OSP(crt->ctx->dsactx->y, crt->ctx->dsactx->y->Length * 4, DSA_Y_Data); - } if (DSA_Y_Len != NULL) - { *DSA_Y_Len = crt->ctx->dsactx->y->Length * 4; - } if (DSA_X_Data != NULL) - { SDRM_I2OSP(BN_A, BN_A->Length * 4, DSA_X_Data); - } if (DSA_X_Len != NULL) - { *DSA_X_Len = BN_A->Length * 4; - } return CRYPTO_SUCCESS; } /* - * @fn int SDRM_DSA_sign(CryptoCoreContainer *crt, cc_u8 *hash, cc_u32 hashLen, cc_u8 *signature, cc_u32 *signLen) - * @brief generate signature for given value + * @fn int SDRM_DSA_sign(CryptoCoreContainer *crt, cc_u8 *hash, cc_u32 hashLen, cc_u8 *signature, cc_u32 *signLen) + * @brief generate signature for given value * - * @param crt [in]crypto env structure - * @param hash [in]hash value - * @param hashLen [in]byte-length of hash - * @param signature [out]generated signature - * @param signLen [out]byte-length of signature + * @param crt [in]crypto env structure + * @param hash [in]hash value + * @param hashLen [in]byte-length of hash + * @param signature [out]generated signature + * @param signLen [out]byte-length of signature * - * @return CRYPTO_SUCCESS if success - * \n CRYPTO_NULL_POINTER if given argument is a null pointer + * @return CRYPTO_SUCCESS if success + * \n CRYPTO_NULL_POINTER if given argument is a null pointer */ -int SDRM_DSA_sign(CryptoCoreContainer *crt, cc_u8 *hash, cc_u32 hashLen, cc_u8 *signature, cc_u32 *signLen) +int SDRM_DSA_sign(CryptoCoreContainer *crt, cc_u8 *hash, cc_u32 hashLen, + cc_u8 *signature, cc_u32 *signLen) { - cc_u8 pbSeed[16] = {0}; + cc_u8 pbSeed[16] = {0}; SDRM_BIG_NUM *BN_P, *BN_Q, *BN_AL, *BN_A; SDRM_BIG_NUM *BN_r, *BN_s, *BN_k, *BN_hash, *BN_ar, *temp1, *temp2; - cc_u8* pbBuf = NULL; + cc_u8 *pbBuf = NULL; - if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->dsactx == NULL) || (crt->ctx->dsactx->a == NULL)) - { + if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->dsactx == NULL) || + (crt->ctx->dsactx->a == NULL)) return CRYPTO_NULL_POINTER; - } - pbBuf = (cc_u8*)malloc(SDRM_DSA_ALLOC_SIZE * 7); + pbBuf = (cc_u8 *)malloc(SDRM_DSA_ALLOC_SIZE * 7); + if (!pbBuf) - { return CRYPTO_MEMORY_ALLOC_FAIL; - } - BN_r = SDRM_BN_Alloc( pbBuf, SDRM_DSA_BN_BUFSIZE); - BN_s = SDRM_BN_Alloc((cc_u8*)BN_r + SDRM_DSA_ALLOC_SIZE, SDRM_DSA_BN_BUFSIZE); - BN_k = SDRM_BN_Alloc((cc_u8*)BN_s + SDRM_DSA_ALLOC_SIZE, SDRM_DSA_BN_BUFSIZE); - BN_hash = SDRM_BN_Alloc((cc_u8*)BN_k + SDRM_DSA_ALLOC_SIZE, SDRM_DSA_BN_BUFSIZE); - BN_ar = SDRM_BN_Alloc((cc_u8*)BN_hash + SDRM_DSA_ALLOC_SIZE, SDRM_DSA_BN_BUFSIZE); - temp1 = SDRM_BN_Alloc((cc_u8*)BN_ar + SDRM_DSA_ALLOC_SIZE, SDRM_DSA_BN_BUFSIZE); - temp2 = SDRM_BN_Alloc((cc_u8*)temp1 + SDRM_DSA_ALLOC_SIZE, SDRM_DSA_BN_BUFSIZE); + BN_r = SDRM_BN_Alloc(pbBuf, SDRM_DSA_BN_BUFSIZE); + BN_s = SDRM_BN_Alloc((cc_u8 *)BN_r + SDRM_DSA_ALLOC_SIZE, + SDRM_DSA_BN_BUFSIZE); + BN_k = SDRM_BN_Alloc((cc_u8 *)BN_s + SDRM_DSA_ALLOC_SIZE, + SDRM_DSA_BN_BUFSIZE); + BN_hash = SDRM_BN_Alloc((cc_u8 *)BN_k + SDRM_DSA_ALLOC_SIZE, + SDRM_DSA_BN_BUFSIZE); + BN_ar = SDRM_BN_Alloc((cc_u8 *)BN_hash + SDRM_DSA_ALLOC_SIZE, + SDRM_DSA_BN_BUFSIZE); + temp1 = SDRM_BN_Alloc((cc_u8 *)BN_ar + SDRM_DSA_ALLOC_SIZE, + SDRM_DSA_BN_BUFSIZE); + temp2 = SDRM_BN_Alloc((cc_u8 *)temp1 + SDRM_DSA_ALLOC_SIZE, + SDRM_DSA_BN_BUFSIZE); BN_P = crt->ctx->dsactx->p; BN_Q = crt->ctx->dsactx->q; @@ -490,11 +450,10 @@ int SDRM_DSA_sign(CryptoCoreContainer *crt, cc_u8 *hash, cc_u32 hashLen, cc_u8 * //select a random secret integer k, 0 < k < q do { - SDRM_RNG_X931(pbSeed, 160, (cc_u8*)BN_k->pData); - BN_k->Length = 6; //6 = 160 / 32 + 1 + SDRM_RNG_X931(pbSeed, 160, (cc_u8 *)BN_k->pData); + BN_k->Length = 6; //6 = 160 / 32 + 1 SDRM_BN_OPTIMIZE_LENGTH(BN_k); - } - while(SDRM_BN_Cmp(BN_k, BN_Q) > 0); + } while (SDRM_BN_Cmp(BN_k, BN_Q) > 0); SDRM_BN_ModExp(temp1, BN_AL, BN_k, BN_P); //r = (al ^ k mod p) mod q @@ -502,7 +461,7 @@ int SDRM_DSA_sign(CryptoCoreContainer *crt, cc_u8 *hash, cc_u32 hashLen, cc_u8 * SDRM_BN_ModInv(temp1, BN_k, BN_Q); - SDRM_OS2BN((cc_u8*)hash, hashLen, BN_hash); + SDRM_OS2BN((cc_u8 *)hash, hashLen, BN_hash); SDRM_BN_Mul(BN_ar, BN_A, BN_r); SDRM_BN_Add(temp2, BN_hash, BN_ar); @@ -515,101 +474,110 @@ int SDRM_DSA_sign(CryptoCoreContainer *crt, cc_u8 *hash, cc_u32 hashLen, cc_u8 * SDRM_I2OSP(BN_s, 20, signature + 20); if (signLen != NULL) - { *signLen = 40; - } free(pbBuf); return CRYPTO_SUCCESS; - } +} /* - * @fn int SDRM_DSA_verify(CryptoCoreContainer *crt, cc_u8 *hash, cc_u32 hashLen, cc_u8 *signature, cc_u32 signLen, int *result) - * @brief generate signature for given value + * @fn int SDRM_DSA_verify(CryptoCoreContainer *crt, cc_u8 *hash, cc_u32 hashLen, cc_u8 *signature, cc_u32 signLen, int *result) + * @brief generate signature for given value * - * @param crt [in]crypto env structure - * @param hash [in]hash value - * @param hashLen [in]byte-length of hash - * @param signature [in]signature - * @param signLen [in]byte-length of signature - * @param result [in]result of veryfing signature + * @param crt [in]crypto env structure + * @param hash [in]hash value + * @param hashLen [in]byte-length of hash + * @param signature [in]signature + * @param signLen [in]byte-length of signature + * @param result [in]result of veryfing signature * - * @return CRYPTO_SUCCESS if success - * \n CRYPTO_NULL_POINTER if given argument is a null pointer - * \n CRYPTO_INVALID_ARGUMENT if the length of signature is invalid + * @return CRYPTO_SUCCESS if success + * \n CRYPTO_NULL_POINTER if given argument is a null pointer + * \n CRYPTO_INVALID_ARGUMENT if the length of signature is invalid */ -int SDRM_DSA_verify(CryptoCoreContainer *crt, cc_u8 *hash, cc_u32 hashLen, cc_u8 *signature, cc_u32 signLen, int *result) +int SDRM_DSA_verify(CryptoCoreContainer *crt, cc_u8 *hash, cc_u32 hashLen, + cc_u8 *signature, cc_u32 signLen, int *result) { SDRM_BIG_NUM *w, *u1, *u2, *v, *BNH_m, *BN_r, *BN_s; SDRM_BIG_NUM *temp1, *temp2, *temp3; - cc_u8 *pbBuf = NULL; + cc_u8 *pbBuf = NULL; - if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->dsactx == NULL) || (crt->ctx->dsactx->y == NULL)) - { + if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->dsactx == NULL) || + (crt->ctx->dsactx->y == NULL)) return CRYPTO_NULL_POINTER; - } - pbBuf = (cc_u8*)malloc(SDRM_DSA_ALLOC_SIZE * 10); + pbBuf = (cc_u8 *)malloc(SDRM_DSA_ALLOC_SIZE * 10); + if (!pbBuf) - { return CRYPTO_MEMORY_ALLOC_FAIL; - } - w = SDRM_BN_Alloc( pbBuf, SDRM_DSA_BN_BUFSIZE); - u1 = SDRM_BN_Alloc((cc_u8*)w + SDRM_DSA_ALLOC_SIZE, SDRM_DSA_BN_BUFSIZE); - u2 = SDRM_BN_Alloc((cc_u8*)u1 + SDRM_DSA_ALLOC_SIZE, SDRM_DSA_BN_BUFSIZE); - v = SDRM_BN_Alloc((cc_u8*)u2 + SDRM_DSA_ALLOC_SIZE, SDRM_DSA_BN_BUFSIZE); - BNH_m = SDRM_BN_Alloc((cc_u8*)v + SDRM_DSA_ALLOC_SIZE, SDRM_DSA_BN_BUFSIZE); - BN_r = SDRM_BN_Alloc((cc_u8*)BNH_m + SDRM_DSA_ALLOC_SIZE, SDRM_DSA_BN_BUFSIZE); - BN_s = SDRM_BN_Alloc((cc_u8*)BN_r + SDRM_DSA_ALLOC_SIZE, SDRM_DSA_BN_BUFSIZE); - temp1 = SDRM_BN_Alloc((cc_u8*)BN_s + SDRM_DSA_ALLOC_SIZE, SDRM_DSA_BN_BUFSIZE); - temp2 = SDRM_BN_Alloc((cc_u8*)temp1 + SDRM_DSA_ALLOC_SIZE, SDRM_DSA_BN_BUFSIZE); - temp3 = SDRM_BN_Alloc((cc_u8*)temp2 + SDRM_DSA_ALLOC_SIZE, SDRM_DSA_BN_BUFSIZE); - - - if ((SDRM_BN_Cmp(BN_r, crt->ctx->dsactx->q) >= 0) || (SDRM_BN_Cmp(BN_s, crt->ctx->dsactx->q) >= 0)) //r < q and s < q - { + w = SDRM_BN_Alloc(pbBuf, SDRM_DSA_BN_BUFSIZE); + u1 = SDRM_BN_Alloc((cc_u8 *)w + SDRM_DSA_ALLOC_SIZE, + SDRM_DSA_BN_BUFSIZE); + u2 = SDRM_BN_Alloc((cc_u8 *)u1 + SDRM_DSA_ALLOC_SIZE, + SDRM_DSA_BN_BUFSIZE); + v = SDRM_BN_Alloc((cc_u8 *)u2 + SDRM_DSA_ALLOC_SIZE, + SDRM_DSA_BN_BUFSIZE); + BNH_m = SDRM_BN_Alloc((cc_u8 *)v + SDRM_DSA_ALLOC_SIZE, + SDRM_DSA_BN_BUFSIZE); + BN_r = SDRM_BN_Alloc((cc_u8 *)BNH_m + SDRM_DSA_ALLOC_SIZE, + SDRM_DSA_BN_BUFSIZE); + BN_s = SDRM_BN_Alloc((cc_u8 *)BN_r + SDRM_DSA_ALLOC_SIZE, + SDRM_DSA_BN_BUFSIZE); + temp1 = SDRM_BN_Alloc((cc_u8 *)BN_s + SDRM_DSA_ALLOC_SIZE, + SDRM_DSA_BN_BUFSIZE); + temp2 = SDRM_BN_Alloc((cc_u8 *)temp1 + SDRM_DSA_ALLOC_SIZE, + SDRM_DSA_BN_BUFSIZE); + temp3 = SDRM_BN_Alloc((cc_u8 *)temp2 + SDRM_DSA_ALLOC_SIZE, + SDRM_DSA_BN_BUFSIZE); + + + if ((SDRM_BN_Cmp(BN_r, crt->ctx->dsactx->q) >= 0) || + (SDRM_BN_Cmp(BN_s, crt->ctx->dsactx->q) >= 0)) { //r < q and s < q free(pbBuf); return CRYPTO_ERROR; } - if (signLen != 40) - { + if (signLen != 40) { free(pbBuf); return CRYPTO_INVALID_ARGUMENT; } - SDRM_OS2BN((cc_u8*)signature, 20, BN_r); - SDRM_OS2BN((cc_u8*)signature + 20, 20, BN_s); + SDRM_OS2BN((cc_u8 *)signature, 20, BN_r); + SDRM_OS2BN((cc_u8 *)signature + 20, 20, BN_s); - SDRM_BN_ModInv(w, BN_s, crt->ctx->dsactx->q); //w = s^-1 mod q - SDRM_OS2BN((cc_u8*)hash, 20, BNH_m); + SDRM_BN_ModInv(w, BN_s, + crt->ctx->dsactx->q); //w = s^-1 mod q + SDRM_OS2BN((cc_u8 *)hash, 20, BNH_m); - SDRM_BN_ModMul(u1, w, BNH_m, crt->ctx->dsactx->q); //u1 = w x h(m) mod q - SDRM_BN_ModMul(u2, BN_r, w, crt->ctx->dsactx->q); //u2 = rw mod q + SDRM_BN_ModMul(u1, w, BNH_m, + crt->ctx->dsactx->q); //u1 = w x h(m) mod q + SDRM_BN_ModMul(u2, BN_r, w, + crt->ctx->dsactx->q); //u2 = rw mod q - SDRM_BN_ModExp(temp1, crt->ctx->dsactx->al, u1, crt->ctx->dsactx->p); //temp1 = alpha^u1 mod p - SDRM_BN_ModExp(temp2, crt->ctx->dsactx->y, u2, crt->ctx->dsactx->p); //temp2 = y^u2 mod p + SDRM_BN_ModExp(temp1, crt->ctx->dsactx->al, u1, + crt->ctx->dsactx->p); //temp1 = alpha^u1 mod p + SDRM_BN_ModExp(temp2, crt->ctx->dsactx->y, u2, + crt->ctx->dsactx->p); //temp2 = y^u2 mod p - SDRM_BN_ModMul(temp3, temp1, temp2, crt->ctx->dsactx->p); //temp3 = (alpha^u1 x y^u2 mod p) mod p + SDRM_BN_ModMul(temp3, temp1, temp2, + crt->ctx->dsactx->p); //temp3 = (alpha^u1 x y^u2 mod p) mod p - SDRM_BN_ModRed(v, temp3, crt->ctx->dsactx->q); //v = (alpha^u1 x y^u2 mod p) mod q + SDRM_BN_ModRed(v, temp3, + crt->ctx->dsactx->q); //v = (alpha^u1 x y^u2 mod p) mod q -// SDRM_PrintBN("v : ", v); -// SDRM_PrintBN("Hash : ", BNH_m); + // SDRM_PrintBN("v : ", v); + // SDRM_PrintBN("Hash : ", BNH_m); if (SDRM_BN_Cmp(v, BN_r) == 0) - { *result = CRYPTO_VALID_SIGN; - } + else - { *result = CRYPTO_INVALID_SIGN; - } free(pbBuf); diff --git a/ssflib/dep/cryptocore/source/middle/cc_ecdh.c b/ssflib/dep/cryptocore/source/middle/cc_ecdh.c index 67ad4fb..764e837 100644 --- a/ssflib/dep/cryptocore/source/middle/cc_ecdh.c +++ b/ssflib/dep/cryptocore/source/middle/cc_ecdh.c @@ -32,68 +32,64 @@ // Functions //////////////////////////////////////////////////////////////////////////// /* - * @fn SDRM_generateDH1stPhaseKey - * @brief generate Xk and its Xv + * @fn SDRM_generateDH1stPhaseKey + * @brief generate Xk and its Xv * - * @param crt [in]crypto context - * @param pchXk [out]Generated Random Number - * @param pchXv [out]DH 1st phase value + * @param crt [in]crypto context + * @param pchXk [out]Generated Random Number + * @param pchXv [out]DH 1st phase value * - * @return CRYPTO_SUCCESS if no error is occured + * @return CRYPTO_SUCCESS if no error is occured */ -int SDRM_generateDH1stPhaseKey(CryptoCoreContainer *crt, cc_u8 *pchXk, cc_u8 *pchXv) +int SDRM_generateDH1stPhaseKey(CryptoCoreContainer *crt, cc_u8 *pchXk, + cc_u8 *pchXv) { cc_u8 Si_ANSI_X9_31[SDRM_X931_SEED_SIZ]; - SDRM_BIG_NUM *BN_Xk, *BN_Temp; - SDRM_EC_POINT *kP; - SDRM_ECC_CTX *ctx; + SDRM_BIG_NUM *BN_Xk, *BN_Temp; + SDRM_EC_POINT *kP; + SDRM_ECC_CTX *ctx; int i; - if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->ecdhctx == NULL) || (pchXk == NULL) || (pchXv == NULL)) - { + if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->ecdhctx == NULL) || + (pchXk == NULL) || (pchXv == NULL)) return CRYPTO_NULL_POINTER; - } ctx = crt->ctx->ecdhctx; for (i = 0; i < SDRM_X931_SEED_SIZ; i++) - { Si_ANSI_X9_31[i] = ((rand() << 16) + rand()) & 0xff; - } BN_Temp = SDRM_BN_Init(crt->ctx->ecdsactx->uDimension >> 3); + if (BN_Temp == NULL) - { return CRYPTO_MEMORY_ALLOC_FAIL; - } BN_Xk = SDRM_BN_Init(crt->ctx->ecdsactx->uDimension >> 3); - if (BN_Xk == NULL) - { + + if (BN_Xk == NULL) { free(BN_Temp); return CRYPTO_MEMORY_ALLOC_FAIL; } SDRM_BN_Sub(BN_Temp, ctx->ECC_n, BN_One); + do { SDRM_RNG_X931(Si_ANSI_X9_31, crt->ctx->ecdsactx->uDimension, pchXk); SDRM_OS2BN(pchXk, crt->ctx->ecdsactx->uDimension >> 3, BN_Xk); - } - while ((SDRM_BN_Cmp(BN_Xk, BN_One) < 0) || (SDRM_BN_Cmp(BN_Xk, BN_Temp) > 0)); + } while ((SDRM_BN_Cmp(BN_Xk, BN_One) < 0) || (SDRM_BN_Cmp(BN_Xk, BN_Temp) > 0)); kP = SDRM_ECC_Init(); - if (kP == NULL) - { + + if (kP == NULL) { free(BN_Temp); free(BN_Xk); return CRYPTO_MEMORY_ALLOC_FAIL; } - if (SDRM_CTX_EC_kP(ctx, kP, ctx->ECC_G, BN_Xk) == CRYPTO_MEMORY_ALLOC_FAIL) - { + if (SDRM_CTX_EC_kP(ctx, kP, ctx->ECC_G, BN_Xk) == CRYPTO_MEMORY_ALLOC_FAIL) { free(BN_Temp); free(BN_Xk); free(kP); @@ -102,7 +98,8 @@ int SDRM_generateDH1stPhaseKey(CryptoCoreContainer *crt, cc_u8 *pchXk, cc_u8 *pc } SDRM_BN2OS(kP->x, crt->ctx->ecdsactx->uDimension >> 3, pchXv); - SDRM_BN2OS(kP->y, crt->ctx->ecdsactx->uDimension >> 3, pchXv + (crt->ctx->ecdsactx->uDimension >> 3)); + SDRM_BN2OS(kP->y, crt->ctx->ecdsactx->uDimension >> 3, + pchXv + (crt->ctx->ecdsactx->uDimension >> 3)); free(BN_Temp); free(BN_Xk); @@ -112,53 +109,52 @@ int SDRM_generateDH1stPhaseKey(CryptoCoreContainer *crt, cc_u8 *pchXk, cc_u8 *pc } /* - * @fn SDRM_generateDHKey - * @brief genenrate auth key with Xk and Yv + * @fn SDRM_generateDHKey + * @brief genenrate auth key with Xk and Yv * - * @param crt [in]crypto context - * @param pchXk [in]Generated Random Number - * @param pchYv [in]DH 1st phase value - * @param pchKauth [out]authentication key + * @param crt [in]crypto context + * @param pchXk [in]Generated Random Number + * @param pchYv [in]DH 1st phase value + * @param pchKauth [out]authentication key * - * @return CRYPTO_SUCCESS if no error is occured + * @return CRYPTO_SUCCESS if no error is occured */ -int SDRM_generateDHKey(CryptoCoreContainer *crt, cc_u8* pchXk, cc_u8* pchYv, cc_u8* pchKauth) +int SDRM_generateDHKey(CryptoCoreContainer *crt, cc_u8 *pchXk, cc_u8 *pchYv, + cc_u8 *pchKauth) { - SDRM_BIG_NUM *BN_Xk; - SDRM_EC_POINT *kP, *EC_Yv; - SDRM_ECC_CTX *ctx; + SDRM_BIG_NUM *BN_Xk; + SDRM_EC_POINT *kP, *EC_Yv; + SDRM_ECC_CTX *ctx; int retVal; - if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->ecdhctx == NULL) || (pchXk == NULL) || (pchYv == NULL) || (pchKauth == NULL)) - { + if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->ecdhctx == NULL) || + (pchXk == NULL) || (pchYv == NULL) || (pchKauth == NULL)) return CRYPTO_NULL_POINTER; - } ctx = crt->ctx->ecdhctx; BN_Xk = SDRM_BN_Init(crt->ctx->ecdsactx->uDimension >> 3); + if (BN_Xk == NULL) - { return CRYPTO_MEMORY_ALLOC_FAIL; - } retVal = SDRM_OS2BN(pchXk, crt->ctx->ecdsactx->uDimension >> 3, BN_Xk); - if (retVal != CRYPTO_SUCCESS) - { + + if (retVal != CRYPTO_SUCCESS) { free(BN_Xk); return retVal; } kP = SDRM_ECC_Init(); - if (kP == NULL) - { + + if (kP == NULL) { free(BN_Xk); return CRYPTO_MEMORY_ALLOC_FAIL; } EC_Yv = SDRM_ECC_Init(); - if (EC_Yv == NULL) - { + + if (EC_Yv == NULL) { free(BN_Xk); free(kP); @@ -167,8 +163,8 @@ int SDRM_generateDHKey(CryptoCoreContainer *crt, cc_u8* pchXk, cc_u8* pchYv, cc_ SDRM_EC_CLR(EC_Yv); retVal = SDRM_OS2BN(pchYv, crt->ctx->ecdsactx->uDimension >> 3, EC_Yv->x); - if (retVal != CRYPTO_SUCCESS) - { + + if (retVal != CRYPTO_SUCCESS) { free(BN_Xk); free(kP); free(EC_Yv); @@ -176,9 +172,10 @@ int SDRM_generateDHKey(CryptoCoreContainer *crt, cc_u8* pchXk, cc_u8* pchYv, cc_ return CRYPTO_MEMORY_ALLOC_FAIL; } - retVal = SDRM_OS2BN(pchYv + (crt->ctx->ecdsactx->uDimension >> 3), crt->ctx->ecdsactx->uDimension >> 3, EC_Yv->y); - if (retVal != CRYPTO_SUCCESS) - { + retVal = SDRM_OS2BN(pchYv + (crt->ctx->ecdsactx->uDimension >> 3), + crt->ctx->ecdsactx->uDimension >> 3, EC_Yv->y); + + if (retVal != CRYPTO_SUCCESS) { free(BN_Xk); free(kP); free(EC_Yv); @@ -186,8 +183,7 @@ int SDRM_generateDHKey(CryptoCoreContainer *crt, cc_u8* pchXk, cc_u8* pchYv, cc_ return CRYPTO_MEMORY_ALLOC_FAIL; } - if (SDRM_CTX_EC_kP(ctx, kP, EC_Yv, BN_Xk) == CRYPTO_MEMORY_ALLOC_FAIL) - { + if (SDRM_CTX_EC_kP(ctx, kP, EC_Yv, BN_Xk) == CRYPTO_MEMORY_ALLOC_FAIL) { free(BN_Xk); free(kP); free(EC_Yv); @@ -196,8 +192,8 @@ int SDRM_generateDHKey(CryptoCoreContainer *crt, cc_u8* pchXk, cc_u8* pchYv, cc_ } retVal = SDRM_BN2OS(kP->x, crt->ctx->ecdsactx->uDimension >> 3, pchKauth); - if (retVal != CRYPTO_SUCCESS) - { + + if (retVal != CRYPTO_SUCCESS) { free(BN_Xk); free(kP); free(EC_Yv); diff --git a/ssflib/dep/cryptocore/source/middle/cc_ecdsa.c b/ssflib/dep/cryptocore/source/middle/cc_ecdsa.c index 2436f09..1725ead 100644 --- a/ssflib/dep/cryptocore/source/middle/cc_ecdsa.c +++ b/ssflib/dep/cryptocore/source/middle/cc_ecdsa.c @@ -35,63 +35,59 @@ // Functions //////////////////////////////////////////////////////////////////////////// /* - * @fn SDRM_CTX_ECDSA_KEY_GEN - * @brief generate signature + * @fn SDRM_CTX_ECDSA_KEY_GEN + * @brief generate signature * - * @param ctx [out]ecc context + * @param ctx [out]ecc context * - * @return CRYPTO_SUCCESS if no error is occured - * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed - * \n CRYPTO_NULL_POINTER if any argument is a null pointer + * @return CRYPTO_SUCCESS if no error is occured + * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed + * \n CRYPTO_NULL_POINTER if any argument is a null pointer */ int SDRM_CTX_ECDSA_KEY_GEN(SDRM_ECC_CTX *ctx) { - int i, retVal; - cc_u32 Seed[4]; - SDRM_BIG_NUM *BN_d, *BN_temp; - SDRM_EC_POINT *kP; + int i, retVal; + cc_u32 Seed[4]; + SDRM_BIG_NUM *BN_d, *BN_temp; + SDRM_EC_POINT *kP; - cc_u8 *pbBuf = NULL; + cc_u8 *pbBuf = NULL; if (ctx == NULL) - { return CRYPTO_NULL_POINTER; - } - pbBuf = (cc_u8*)malloc(SDRM_ECC_ALLOC_SIZE * 2); + pbBuf = (cc_u8 *)malloc(SDRM_ECC_ALLOC_SIZE * 2); + if (!pbBuf) - { return CRYPTO_MEMORY_ALLOC_FAIL; - } - BN_d = SDRM_BN_Alloc( pbBuf , SDRM_ECC_BN_BUFSIZE); - BN_temp = SDRM_BN_Alloc((cc_u8*)BN_d + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE); + BN_d = SDRM_BN_Alloc(pbBuf, SDRM_ECC_BN_BUFSIZE); + BN_temp = SDRM_BN_Alloc((cc_u8 *)BN_d + SDRM_ECC_ALLOC_SIZE, + SDRM_ECC_BN_BUFSIZE); kP = SDRM_ECC_Init(); - if (kP == NULL) - { + + if (kP == NULL) { free(pbBuf); return CRYPTO_MEMORY_ALLOC_FAIL; } for (i = 0; i < 4; i++) - { Seed[i] = (rand() << 16) ^ rand(); - } SDRM_BN_Sub(BN_temp, ctx->ECC_n, BN_One); + do { - SDRM_RNG_X931((cc_u8 *)Seed, ctx->uDimension, (cc_u8*)BN_d->pData); + SDRM_RNG_X931((cc_u8 *)Seed, ctx->uDimension, (cc_u8 *)BN_d->pData); BN_d->Length = ctx->uDimension / 32; - } - while ((SDRM_BN_Cmp(BN_d, BN_One) < 0) || (SDRM_BN_Cmp(BN_d, BN_temp) > 0)); + } while ((SDRM_BN_Cmp(BN_d, BN_One) < 0) || (SDRM_BN_Cmp(BN_d, BN_temp) > 0)); SDRM_BN_OPTIMIZE_LENGTH(BN_d); SDRM_EC_SET_ZERO(kP); retVal = SDRM_CTX_EC_kP(ctx, kP, ctx->ECC_G, BN_d); - if (retVal != CRYPTO_SUCCESS) - { + + if (retVal != CRYPTO_SUCCESS) { free(pbBuf); free(kP); @@ -108,75 +104,76 @@ int SDRM_CTX_ECDSA_KEY_GEN(SDRM_ECC_CTX *ctx) } /* - * @fn SDRM_CTX_ECDSA_SIG_GEN - * @brief generate signature + * @fn SDRM_CTX_ECDSA_SIG_GEN + * @brief generate signature * - * @param ctx [in]ecc context - * @param sig [out]generated signature - * @param hash [in]hashed message + * @param ctx [in]ecc context + * @param sig [out]generated signature + * @param hash [in]hashed message * - * @return CRYPTO_SUCCESS if no error is occured - * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed - * \n CRYPTO_NULL_POINTER if any argument is a null pointer + * @return CRYPTO_SUCCESS if no error is occured + * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed + * \n CRYPTO_NULL_POINTER if any argument is a null pointer */ -int SDRM_CTX_ECDSA_SIG_GEN(SDRM_ECC_CTX *ctx, cc_u8 *sig, cc_u8 *hash, unsigned int hashLen) +int SDRM_CTX_ECDSA_SIG_GEN(SDRM_ECC_CTX *ctx, cc_u8 *sig, cc_u8 *hash, + unsigned int hashLen) { - int i, res = -1; - cc_u32 Seed[20]; - SDRM_BIG_NUM *BN_Tmp1, *BN_Tmp2, *BN_Tmp3; - SDRM_BIG_NUM *BN_k, *BN_r, *BN_s, *BN_hash; - SDRM_EC_POINT *kP; + int i, res = -1; + cc_u32 Seed[20]; + SDRM_BIG_NUM *BN_Tmp1, *BN_Tmp2, *BN_Tmp3; + SDRM_BIG_NUM *BN_k, *BN_r, *BN_s, *BN_hash; + SDRM_EC_POINT *kP; - cc_u8 *pbBuf = NULL; + cc_u8 *pbBuf = NULL; - if ((ctx== NULL) || (sig == NULL) || (hash == NULL)) - { + if ((ctx == NULL) || (sig == NULL) || (hash == NULL)) return CRYPTO_NULL_POINTER; - } - pbBuf = (cc_u8*)malloc(SDRM_ECC_ALLOC_SIZE * 7); + pbBuf = (cc_u8 *)malloc(SDRM_ECC_ALLOC_SIZE * 7); + if (!pbBuf) - { return CRYPTO_MEMORY_ALLOC_FAIL; - } - BN_Tmp1 = SDRM_BN_Alloc( pbBuf, SDRM_ECC_BN_BUFSIZE); - BN_Tmp2 = SDRM_BN_Alloc((cc_u8*)BN_Tmp1 + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE); - BN_Tmp3 = SDRM_BN_Alloc((cc_u8*)BN_Tmp2 + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE); - BN_k = SDRM_BN_Alloc((cc_u8*)BN_Tmp3 + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE); - BN_r = SDRM_BN_Alloc((cc_u8*)BN_k + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE); - BN_s = SDRM_BN_Alloc((cc_u8*)BN_r + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE); - BN_hash = SDRM_BN_Alloc((cc_u8*)BN_s + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE); + BN_Tmp1 = SDRM_BN_Alloc(pbBuf, + SDRM_ECC_BN_BUFSIZE); + BN_Tmp2 = SDRM_BN_Alloc((cc_u8 *)BN_Tmp1 + SDRM_ECC_ALLOC_SIZE, + SDRM_ECC_BN_BUFSIZE); + BN_Tmp3 = SDRM_BN_Alloc((cc_u8 *)BN_Tmp2 + SDRM_ECC_ALLOC_SIZE, + SDRM_ECC_BN_BUFSIZE); + BN_k = SDRM_BN_Alloc((cc_u8 *)BN_Tmp3 + SDRM_ECC_ALLOC_SIZE, + SDRM_ECC_BN_BUFSIZE); + BN_r = SDRM_BN_Alloc((cc_u8 *)BN_k + SDRM_ECC_ALLOC_SIZE, + SDRM_ECC_BN_BUFSIZE); + BN_s = SDRM_BN_Alloc((cc_u8 *)BN_r + SDRM_ECC_ALLOC_SIZE, + SDRM_ECC_BN_BUFSIZE); + BN_hash = SDRM_BN_Alloc((cc_u8 *)BN_s + SDRM_ECC_ALLOC_SIZE, + SDRM_ECC_BN_BUFSIZE); kP = SDRM_ECC_Init(); - if (kP == NULL) - { + + if (kP == NULL) { free(pbBuf); return CRYPTO_MEMORY_ALLOC_FAIL; } for (i = 0; i < 4; i++) - { Seed[i] = (rand() << 16) ^ rand(); - } - while(1) - { - while(1) - { - // 1. [1, r-1] ������ ���� k ���� + while (1) { + while (1) { + // 1. [1, r-1] ������ ���� k ���� SDRM_BN_Sub(BN_Tmp1, ctx->ECC_n, BN_One); + do { - SDRM_RNG_X931((cc_u8 *)Seed, ctx->uDimension, (cc_u8*)BN_k->pData); + SDRM_RNG_X931((cc_u8 *)Seed, ctx->uDimension, (cc_u8 *)BN_k->pData); BN_k->Length = ctx->uDimension / 32; - } - while((SDRM_BN_Cmp(BN_k, BN_One) < 0) || (SDRM_BN_Cmp(BN_k, BN_Tmp1) > 0)); + } while ((SDRM_BN_Cmp(BN_k, BN_One) < 0) || (SDRM_BN_Cmp(BN_k, BN_Tmp1) > 0)); - // 2. kP = (x1, y1), r = x1 mod n(&ctx.ECC_n) ���. r = 0 �̸� k �ٽ� ���� + // 2. kP = (x1, y1), r = x1 mod n(&ctx.ECC_n) ���. r = 0 �̸� k �ٽ� ���� SDRM_EC_SET_ZERO(kP); res = SDRM_CTX_EC_kP(ctx, kP, ctx->ECC_G, BN_k); - if (res != CRYPTO_SUCCESS) - { + + if (res != CRYPTO_SUCCESS) { free(pbBuf); free(kP); @@ -185,33 +182,31 @@ int SDRM_CTX_ECDSA_SIG_GEN(SDRM_ECC_CTX *ctx, cc_u8 *sig, cc_u8 *hash, unsigned //SDRM_PrintBN("kP->x", kP->x); SDRM_BN_ModRed(BN_r, kP->x, ctx->ECC_n); - if (BN_r->Length > 0) // r = 0 �̸� k �ٽ� ���� - { + + if (BN_r->Length > 0) // r = 0 �̸� k �ٽ� ���� break; - } } - // 3. k^{-1} mod n ���. + // 3. k^{-1} mod n ���. SDRM_BN_ModInv(BN_Tmp1, BN_k, ctx->ECC_n); //SDRM_PrintBN("BN_k", BN_k); //SDRM_PrintBN("ctx->ECC_n", ctx->ECC_n); //SDRM_PrintBN("BN_Tmp1 = k^{-1} mod n", BN_Tmp1); - // 4. s = k^{-1}(hash + dr) mod n ��� (d = private key). s = 0 �̸� 1������. + // 4. s = k^{-1}(hash + dr) mod n ��� (d = private key). s = 0 �̸� 1������. // BN_Tmp2 = dr SDRM_OS2BN(hash, hashLen, BN_hash); SDRM_BN_ModMul(BN_Tmp2, ctx->PRIV_KEY, BN_r, ctx->ECC_n); SDRM_BN_ModAdd(BN_Tmp3, BN_hash, BN_Tmp2, ctx->ECC_n); SDRM_BN_ModMul(BN_s, BN_Tmp1, BN_Tmp3, ctx->ECC_n); + if (BN_s->Length > 0) - { break; - } } -// (r, s) �������� ���. + // (r, s) �������� ���. //SDRM_PrintBN("BN_r", BN_r); //SDRM_PrintBN("BN_s", BN_s); @@ -225,59 +220,64 @@ int SDRM_CTX_ECDSA_SIG_GEN(SDRM_ECC_CTX *ctx, cc_u8 *sig, cc_u8 *hash, unsigned } /* - * @fn SDRM_CTX_ECDSA_SIG_VERIFY - * @brief verify ecdsa signature + * @fn SDRM_CTX_ECDSA_SIG_VERIFY + * @brief verify ecdsa signature * - * @param ctx [in]ecc context - * @param sig [out]generated signature - * @param signLen [out]byte-length of signature - * @param hash [in]hash value - * @param hashLen [in]byte-length of hash + * @param ctx [in]ecc context + * @param sig [out]generated signature + * @param signLen [out]byte-length of signature + * @param hash [in]hash value + * @param hashLen [in]byte-length of hash * - * @return CRYPTO_VALID_SIGN if given signature is valid - * \n CRYPTO_INVALID_SIGN if given signature is invalid - * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed - * \n CRYPTO_INVALID_ARGUMENT if any argument is out of range - * \n CRYPTO_INFINITY_INPUT if given argument represents an infinity value + * @return CRYPTO_VALID_SIGN if given signature is valid + * \n CRYPTO_INVALID_SIGN if given signature is invalid + * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed + * \n CRYPTO_INVALID_ARGUMENT if any argument is out of range + * \n CRYPTO_INFINITY_INPUT if given argument represents an infinity value */ -int SDRM_CTX_ECDSA_SIG_VERIFY(SDRM_ECC_CTX *ctx, cc_u8 *sig, int signLen, cc_u8 *hash, int hashLen) +int SDRM_CTX_ECDSA_SIG_VERIFY(SDRM_ECC_CTX *ctx, cc_u8 *sig, int signLen, + cc_u8 *hash, int hashLen) { - int res; - SDRM_BIG_NUM *BN_tmp, *BN_u1, *BN_u2, *BN_w, *BN_hash, *pBN_r, *pBN_s; - SDRM_EC_POINT *EC_temp1, *EC_temp2; + int res; + SDRM_BIG_NUM *BN_tmp, *BN_u1, *BN_u2, *BN_w, *BN_hash, *pBN_r, *pBN_s; + SDRM_EC_POINT *EC_temp1, *EC_temp2; - cc_u8 *pbBuf = (cc_u8*)malloc(SDRM_ECC_ALLOC_SIZE * 7); + cc_u8 *pbBuf = (cc_u8 *)malloc(SDRM_ECC_ALLOC_SIZE * 7); if (!pbBuf) - { return CRYPTO_MEMORY_ALLOC_FAIL; - } - BN_tmp = SDRM_BN_Alloc( pbBuf, SDRM_ECC_BN_BUFSIZE); - BN_u1 = SDRM_BN_Alloc((cc_u8*)BN_tmp + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE); - BN_u2 = SDRM_BN_Alloc((cc_u8*)BN_u1 + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE); - BN_w = SDRM_BN_Alloc((cc_u8*)BN_u2 + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE); - BN_hash = SDRM_BN_Alloc((cc_u8*)BN_w + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE); - pBN_r = SDRM_BN_Alloc((cc_u8*)BN_hash+ SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE); - pBN_s = SDRM_BN_Alloc((cc_u8*)pBN_r + SDRM_ECC_ALLOC_SIZE, SDRM_ECC_BN_BUFSIZE); + BN_tmp = SDRM_BN_Alloc(pbBuf, + SDRM_ECC_BN_BUFSIZE); + BN_u1 = SDRM_BN_Alloc((cc_u8 *)BN_tmp + SDRM_ECC_ALLOC_SIZE, + SDRM_ECC_BN_BUFSIZE); + BN_u2 = SDRM_BN_Alloc((cc_u8 *)BN_u1 + SDRM_ECC_ALLOC_SIZE, + SDRM_ECC_BN_BUFSIZE); + BN_w = SDRM_BN_Alloc((cc_u8 *)BN_u2 + SDRM_ECC_ALLOC_SIZE, + SDRM_ECC_BN_BUFSIZE); + BN_hash = SDRM_BN_Alloc((cc_u8 *)BN_w + SDRM_ECC_ALLOC_SIZE, + SDRM_ECC_BN_BUFSIZE); + pBN_r = SDRM_BN_Alloc((cc_u8 *)BN_hash + SDRM_ECC_ALLOC_SIZE, + SDRM_ECC_BN_BUFSIZE); + pBN_s = SDRM_BN_Alloc((cc_u8 *)pBN_r + SDRM_ECC_ALLOC_SIZE, + SDRM_ECC_BN_BUFSIZE); EC_temp1 = SDRM_ECC_Init(); - if (EC_temp1 == NULL) - { + + if (EC_temp1 == NULL) { free(pbBuf); return CRYPTO_MEMORY_ALLOC_FAIL; } EC_temp2 = SDRM_ECC_Init(); - if (EC_temp2 == NULL) - { + + if (EC_temp2 == NULL) { free(pbBuf); free(EC_temp1); return CRYPTO_MEMORY_ALLOC_FAIL; } - if ((cc_u32)signLen != (ctx->uDimension / 4)) - { + if ((cc_u32)signLen != (ctx->uDimension / 4)) { free(pbBuf); free(EC_temp1); free(EC_temp2); @@ -289,10 +289,10 @@ int SDRM_CTX_ECDSA_SIG_VERIFY(SDRM_ECC_CTX *ctx, cc_u8 *sig, int signLen, cc_u8 //SDRM_PrintBN("BN_r", pBN_r); //SDRM_PrintBN("BN_s", pBN_s); - // 1. r�� s�� ���� ���� + // 1. r�� s�� ���� ���� SDRM_BN_Sub(BN_tmp, ctx->ECC_n, BN_One); - if ((SDRM_BN_Cmp(pBN_r, BN_One) < 0) || (SDRM_BN_Cmp(pBN_r, BN_tmp) > 0)) - { + + if ((SDRM_BN_Cmp(pBN_r, BN_One) < 0) || (SDRM_BN_Cmp(pBN_r, BN_tmp) > 0)) { free(pbBuf); free(EC_temp1); free(EC_temp2); @@ -300,8 +300,7 @@ int SDRM_CTX_ECDSA_SIG_VERIFY(SDRM_ECC_CTX *ctx, cc_u8 *sig, int signLen, cc_u8 return CRYPTO_INVALID_ARGUMENT; } - if ((SDRM_BN_Cmp(pBN_s, BN_One) < 0) || (SDRM_BN_Cmp(pBN_s, BN_tmp) > 0)) - { + if ((SDRM_BN_Cmp(pBN_s, BN_One) < 0) || (SDRM_BN_Cmp(pBN_s, BN_tmp) > 0)) { free(pbBuf); free(EC_temp1); free(EC_temp2); @@ -309,13 +308,12 @@ int SDRM_CTX_ECDSA_SIG_VERIFY(SDRM_ECC_CTX *ctx, cc_u8 *sig, int signLen, cc_u8 return CRYPTO_INVALID_ARGUMENT; } - // 2. w = s^(-1) mod n, BN_hash ��� + // 2. w = s^(-1) mod n, BN_hash ��� SDRM_OS2BN(hash, hashLen, BN_hash); res = SDRM_BN_ModInv(BN_w, pBN_s, ctx->ECC_n); -//SDRM_PrintBN("BN_w", BN_w); + //SDRM_PrintBN("BN_w", BN_w); - if (res != CRYPTO_SUCCESS) - { + if (res != CRYPTO_SUCCESS) { free(pbBuf); free(EC_temp1); free(EC_temp2); @@ -323,24 +321,22 @@ int SDRM_CTX_ECDSA_SIG_VERIFY(SDRM_ECC_CTX *ctx, cc_u8 *sig, int signLen, cc_u8 return res; } - // 3. u1 = BN_hash *w mod n, u2 = rw mod n + // 3. u1 = BN_hash *w mod n, u2 = rw mod n SDRM_BN_ModMul(BN_u1, BN_hash, BN_w, ctx->ECC_n); SDRM_BN_ModMul(BN_u2, pBN_r, BN_w, ctx->ECC_n); -//SDRM_PrintBN("BN_u1", BN_u1); -//SDRM_PrintBN("BN_u2", BN_u2); + //SDRM_PrintBN("BN_u1", BN_u1); + //SDRM_PrintBN("BN_u2", BN_u2); - // 4. (x0, y0) = u1P + u2Q, V = x0 mod n + // 4. (x0, y0) = u1P + u2Q, V = x0 mod n res = SDRM_CTX_EC_2kP(ctx, EC_temp1, BN_u1, ctx->ECC_G, BN_u2, ctx->PUBLIC_KEY); - if (res != CRYPTO_SUCCESS) - { + + if (res != CRYPTO_SUCCESS) { free(pbBuf); free(EC_temp1); free(EC_temp2); return res; - } - else if(EC_temp1->IsInfinity == 1) - { + } else if (EC_temp1->IsInfinity == 1) { res = CRYPTO_INFINITY_INPUT; free(pbBuf); free(EC_temp1); @@ -349,16 +345,16 @@ int SDRM_CTX_ECDSA_SIG_VERIFY(SDRM_ECC_CTX *ctx, cc_u8 *sig, int signLen, cc_u8 return res; } -// SDRM_PrintBN("EC_temp1->x", EC_temp1->x); -// SDRM_PrintBN("ctx->ECC_n", ctx->ECC_n); + // SDRM_PrintBN("EC_temp1->x", EC_temp1->x); + // SDRM_PrintBN("ctx->ECC_n", ctx->ECC_n); SDRM_BN_ModRed(BN_tmp, EC_temp1->x, ctx->ECC_n); -// SDRM_PrintBN("BN_tmp", BN_tmp); -// SDRM_PrintBN("pBN_r", pBN_r); + // SDRM_PrintBN("BN_tmp", BN_tmp); + // SDRM_PrintBN("pBN_r", pBN_r); // 5. V = r�� ��� ���� ok res = SDRM_BN_Cmp_sign(BN_tmp, pBN_r); - if (res != 0) - { + + if (res != 0) { res = CRYPTO_INVALID_SIGN; free(pbBuf); free(EC_temp1); @@ -376,162 +372,154 @@ int SDRM_CTX_ECDSA_SIG_VERIFY(SDRM_ECC_CTX *ctx, cc_u8 *sig, int signLen, cc_u8 } /* - * @fn SDRM_ECDSA_sign - * @brief generate signature for given value + * @fn SDRM_ECDSA_sign + * @brief generate signature for given value * - * @param crt [in]crypto env structure - * @param hash [in]hash value - * @param hashLen [in]byte-length of hash - * @param signature [out]generated signature - * @param signLen [out]byte-length of signature + * @param crt [in]crypto env structure + * @param hash [in]hash value + * @param hashLen [in]byte-length of hash + * @param signature [out]generated signature + * @param signLen [out]byte-length of signature * - * @return CRYPTO_SUCCESS if success - * \n CRYPTO_NULL_POINTER if given argument is a null pointer + * @return CRYPTO_SUCCESS if success + * \n CRYPTO_NULL_POINTER if given argument is a null pointer */ -int SDRM_ECDSA_sign(CryptoCoreContainer *crt, cc_u8 *hash, cc_u32 hashLen, cc_u8 *signature, cc_u32 *signLen) +int SDRM_ECDSA_sign(CryptoCoreContainer *crt, cc_u8 *hash, cc_u32 hashLen, + cc_u8 *signature, cc_u32 *signLen) { - if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->ecdsactx == NULL) || (hash == NULL) || (signature == NULL)) - { + if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->ecdsactx == NULL) || + (hash == NULL) || (signature == NULL)) return CRYPTO_NULL_POINTER; - } if (signLen) - { *signLen = crt->ctx->ecdsactx->uDimension / 4; - } return SDRM_CTX_ECDSA_SIG_GEN(crt->ctx->ecdsactx, signature, hash, hashLen); } /* - * @fn SDRM_ECDSA_verify - * @brief generate signature for given value + * @fn SDRM_ECDSA_verify + * @brief generate signature for given value * - * @param crt [in]crypto env structure - * @param hash [in]hash value - * @param hashLen [in]byte-length of hash - * @param signature [in]signature - * @param signLen [in]byte-length of signature - * @param result [in]result of veryfing signature + * @param crt [in]crypto env structure + * @param hash [in]hash value + * @param hashLen [in]byte-length of hash + * @param signature [in]signature + * @param signLen [in]byte-length of signature + * @param result [in]result of veryfing signature * - * @return CRYPTO_SUCCESS if success - * \n CRYPTO_NULL_POINTER if given argument is a null pointer - * \n CRYPTO_INVALID_ARGUMENT if the length of signature is invalid + * @return CRYPTO_SUCCESS if success + * \n CRYPTO_NULL_POINTER if given argument is a null pointer + * \n CRYPTO_INVALID_ARGUMENT if the length of signature is invalid */ -int SDRM_ECDSA_verify(CryptoCoreContainer *crt, cc_u8 *hash, cc_u32 hashLen, cc_u8 *signature, cc_u32 signLen, int *result) +int SDRM_ECDSA_verify(CryptoCoreContainer *crt, cc_u8 *hash, cc_u32 hashLen, + cc_u8 *signature, cc_u32 signLen, int *result) { int retVal; - if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->ecdsactx == NULL) || (hash == NULL) || (signature == NULL) || (result == NULL)) - { + if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->ecdsactx == NULL) || + (hash == NULL) || (signature == NULL) || (result == NULL)) return CRYPTO_NULL_POINTER; - } if (signLen != (crt->ctx->ecdsactx->uDimension / 4)) - { return CRYPTO_INVALID_ARGUMENT; - } - retVal = SDRM_CTX_ECDSA_SIG_VERIFY(crt->ctx->ecdsactx, signature, signLen, hash, hashLen); + retVal = SDRM_CTX_ECDSA_SIG_VERIFY(crt->ctx->ecdsactx, signature, signLen, hash, + hashLen); if (retVal == CRYPTO_VALID_SIGN) - { *result = CRYPTO_VALID_SIGN; - } + else - { *result = CRYPTO_INVALID_SIGN; - } return retVal; } /* - * @fn SDRM_ECC_Set_CTX - * @brief Set parameters for ECC + * @fn SDRM_ECC_Set_CTX + * @brief Set parameters for ECC * - * @param crt [out]crypto env structure - * @param Dimension [in]dimension - * @param ECC_P_Data [in]represents p - * @param ECC_P_Len [in]byte-length of p - * @param ECC_A_Data [in]represents a - * @param ECC_A_Len [in]byte-length of a - * @param ECC_B_Data [in]represents b - * @param ECC_B_Len [in]byte-length of b - * @param ECC_G_X_Data [in]represents x coordinate of g - * @param ECC_G_X_Len [in]byte-length of x coordinate of g - * @param ECC_G_Y_Data [in]represents y coordinate of g - * @param ECC_G_Y_Len [in]byte-length of y coordinate of g - * @param ECC_R_Data [in]represents r - * @param ECC_R_Len [in]byte-length of r + * @param crt [out]crypto env structure + * @param Dimension [in]dimension + * @param ECC_P_Data [in]represents p + * @param ECC_P_Len [in]byte-length of p + * @param ECC_A_Data [in]represents a + * @param ECC_A_Len [in]byte-length of a + * @param ECC_B_Data [in]represents b + * @param ECC_B_Len [in]byte-length of b + * @param ECC_G_X_Data [in]represents x coordinate of g + * @param ECC_G_X_Len [in]byte-length of x coordinate of g + * @param ECC_G_Y_Data [in]represents y coordinate of g + * @param ECC_G_Y_Len [in]byte-length of y coordinate of g + * @param ECC_R_Data [in]represents r + * @param ECC_R_Len [in]byte-length of r * - * @return CRYPTO_SUCCESS if no error is occured - * \n CRYPTO_NULL_POINTER if argument is null + * @return CRYPTO_SUCCESS if no error is occured + * \n CRYPTO_NULL_POINTER if argument is null */ int SDRM_ECC_Set_CTX(CryptoCoreContainer *crt, cc_u16 Dimension, - cc_u8* ECC_P_Data, cc_u32 ECC_P_Len, - cc_u8* ECC_A_Data, cc_u32 ECC_A_Len, - cc_u8* ECC_B_Data, cc_u32 ECC_B_Len, - cc_u8* ECC_G_X_Data, cc_u32 ECC_G_X_Len, - cc_u8* ECC_G_Y_Data, cc_u32 ECC_G_Y_Len, - cc_u8* ECC_R_Data, cc_u32 ECC_R_Len) + cc_u8 *ECC_P_Data, cc_u32 ECC_P_Len, + cc_u8 *ECC_A_Data, cc_u32 ECC_A_Len, + cc_u8 *ECC_B_Data, cc_u32 ECC_B_Len, + cc_u8 *ECC_G_X_Data, cc_u32 ECC_G_X_Len, + cc_u8 *ECC_G_Y_Data, cc_u32 ECC_G_Y_Len, + cc_u8 *ECC_R_Data, cc_u32 ECC_R_Len) { - int retVal; - cc_u8 zero[] = {0x00}; - SDRM_ECC_CTX *ECC_ctx; + int retVal; + cc_u8 zero[] = {0x00}; + SDRM_ECC_CTX *ECC_ctx; if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->ecdsactx == NULL)) - { return CRYPTO_NULL_POINTER; - } - if ((ECC_P_Data == NULL) || (ECC_A_Data == NULL) || (ECC_B_Data == NULL) || (ECC_G_X_Data == NULL) || (ECC_G_Y_Data == NULL) || (ECC_R_Data == NULL)) - { + if ((ECC_P_Data == NULL) || (ECC_A_Data == NULL) || (ECC_B_Data == NULL) || + (ECC_G_X_Data == NULL) || (ECC_G_Y_Data == NULL) || (ECC_R_Data == NULL)) return CRYPTO_NULL_POINTER; - } ECC_ctx = crt->ctx->ecdhctx; ECC_ctx->uDimension = Dimension; retVal = SDRM_OS2BN(ECC_P_Data, ECC_P_Len, ECC_ctx->ECC_p); - if (retVal != CRYPTO_SUCCESS) - { + + if (retVal != CRYPTO_SUCCESS) { free(ECC_ctx); return retVal; } retVal = SDRM_OS2BN(ECC_A_Data, ECC_A_Len, ECC_ctx->ECC_a); - if (retVal != CRYPTO_SUCCESS) - { + + if (retVal != CRYPTO_SUCCESS) { free(ECC_ctx); return retVal; } retVal = SDRM_OS2BN(ECC_B_Data, ECC_B_Len, ECC_ctx->ECC_b); - if (retVal != CRYPTO_SUCCESS) - { + + if (retVal != CRYPTO_SUCCESS) { free(ECC_ctx); return retVal; } retVal = SDRM_OS2BN(ECC_R_Data, ECC_R_Len, ECC_ctx->ECC_n); - if (retVal != CRYPTO_SUCCESS) - { + + if (retVal != CRYPTO_SUCCESS) { free(ECC_ctx); return retVal; } ECC_ctx->ECC_G->IsInfinity = 0; retVal = SDRM_OS2BN(ECC_G_X_Data, ECC_G_X_Len, ECC_ctx->ECC_G->x); - if (retVal != CRYPTO_SUCCESS) - { + + if (retVal != CRYPTO_SUCCESS) { free(ECC_ctx); return retVal; } + retVal = SDRM_OS2BN(ECC_G_Y_Data, ECC_G_Y_Len, ECC_ctx->ECC_G->y); - if (retVal != CRYPTO_SUCCESS) - { + + if (retVal != CRYPTO_SUCCESS) { free(ECC_ctx); return retVal; } @@ -544,127 +532,111 @@ int SDRM_ECC_Set_CTX(CryptoCoreContainer *crt, cc_u16 Dimension, } /* - * @fn SDRM_ECC_genKeypair - * @brief Generate Private Key and Generate Key Pair for ECC Signature + * @fn SDRM_ECC_genKeypair + * @brief Generate Private Key and Generate Key Pair for ECC Signature * - * @param crt [out]crypto env structure - * @param PrivateKey [in]represents x coordinate of public key - * @param PrivateKeyLen [in]byte-length of x coordinate of public key - * @param PublicKey_X [in]represents x coordinate of public key - * @param PublicKey_XLen [in]byte-length of x coordinate of public key - * @param PublicKey_Y [in]represents y coordinate of public key - * @param PublicKey_YLen [in]byte-length of y coordinate of public key + * @param crt [out]crypto env structure + * @param PrivateKey [in]represents x coordinate of public key + * @param PrivateKeyLen [in]byte-length of x coordinate of public key + * @param PublicKey_X [in]represents x coordinate of public key + * @param PublicKey_XLen [in]byte-length of x coordinate of public key + * @param PublicKey_Y [in]represents y coordinate of public key + * @param PublicKey_YLen [in]byte-length of y coordinate of public key * - * @return CRYPTO_SUCCESS if no error is occured - * \n CRYPTO_NULL_POINTER if argument is null + * @return CRYPTO_SUCCESS if no error is occured + * \n CRYPTO_NULL_POINTER if argument is null */ -int SDRM_ECC_genKeypair (CryptoCoreContainer *crt, - cc_u8 *PrivateKey, cc_u32 *PrivateKeyLen, - cc_u8 *PublicKey_X, cc_u32 *PublicKey_XLen, - cc_u8 *PublicKey_Y, cc_u32 *PublicKey_YLen) +int SDRM_ECC_genKeypair(CryptoCoreContainer *crt, + cc_u8 *PrivateKey, cc_u32 *PrivateKeyLen, + cc_u8 *PublicKey_X, cc_u32 *PublicKey_XLen, + cc_u8 *PublicKey_Y, cc_u32 *PublicKey_YLen) { int retVal; if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->ecdsactx == NULL)) - { return CRYPTO_NULL_POINTER; - } retVal = SDRM_CTX_ECDSA_KEY_GEN(crt->ctx->ecdsactx); + if (retVal != CRYPTO_SUCCESS) - { return retVal; - } - if (PrivateKey != NULL) - { - SDRM_I2OSP(crt->ctx->ecdsactx->PRIV_KEY, crt->ctx->ecdsactx->uDimension / 8, PrivateKey); + if (PrivateKey != NULL) { + SDRM_I2OSP(crt->ctx->ecdsactx->PRIV_KEY, crt->ctx->ecdsactx->uDimension / 8, + PrivateKey); } if (PrivateKeyLen != NULL) - { *PrivateKeyLen = crt->ctx->ecdsactx->uDimension / 8; - } - if (PublicKey_X != NULL) - { - SDRM_I2OSP(crt->ctx->ecdsactx->PUBLIC_KEY->x, crt->ctx->ecdsactx->uDimension / 8, PublicKey_X); + if (PublicKey_X != NULL) { + SDRM_I2OSP(crt->ctx->ecdsactx->PUBLIC_KEY->x, + crt->ctx->ecdsactx->uDimension / 8, PublicKey_X); } if (PublicKey_XLen != NULL) - { *PublicKey_XLen = crt->ctx->ecdsactx->uDimension / 8; - } - if (PublicKey_Y != NULL) - { - SDRM_I2OSP(crt->ctx->ecdsactx->PUBLIC_KEY->y, crt->ctx->ecdsactx->uDimension / 8, PublicKey_Y); + if (PublicKey_Y != NULL) { + SDRM_I2OSP(crt->ctx->ecdsactx->PUBLIC_KEY->y, + crt->ctx->ecdsactx->uDimension / 8, PublicKey_Y); } if (PublicKey_YLen != NULL) - { *PublicKey_YLen = crt->ctx->ecdsactx->uDimension / 8; - } return CRYPTO_SUCCESS; } /* - * @fn SDRM_ECC_setKeypair - * @brief Set key data for ECC + * @fn SDRM_ECC_setKeypair + * @brief Set key data for ECC * - * @param crt [out]crypto env structure - * @param PRIV_Data [in]represents private key - * @param PRIV_Len [in]byte-length of private key - * @param PUB_X_Data [in]represents x coordinate of public key - * @param PUB_X_Len [in]byte-length of x coordinate of public key - * @param PUB_Y_Data [in]represents y coordinate of public key - * @param PUB_Y_Len [in]byte-length of y coordinate of public key + * @param crt [out]crypto env structure + * @param PRIV_Data [in]represents private key + * @param PRIV_Len [in]byte-length of private key + * @param PUB_X_Data [in]represents x coordinate of public key + * @param PUB_X_Len [in]byte-length of x coordinate of public key + * @param PUB_Y_Data [in]represents y coordinate of public key + * @param PUB_Y_Len [in]byte-length of y coordinate of public key * - * @return CRYPTO_SUCCESS if no error is occured - * \n CRYPTO_NULL_POINTER if argument is null + * @return CRYPTO_SUCCESS if no error is occured + * \n CRYPTO_NULL_POINTER if argument is null */ int SDRM_ECC_setKeypair(CryptoCoreContainer *crt, - cc_u8* PRIV_Data, cc_u32 PRIV_Len, - cc_u8* PUB_X_Data, cc_u32 PUB_X_Len, - cc_u8* PUB_Y_Data, cc_u32 PUB_Y_Len) + cc_u8 *PRIV_Data, cc_u32 PRIV_Len, + cc_u8 *PUB_X_Data, cc_u32 PUB_X_Len, + cc_u8 *PUB_Y_Data, cc_u32 PUB_Y_Len) { - int retVal; - cc_u8 zero[] = {0x00}; - SDRM_ECC_CTX *ECC_ctx; + int retVal; + cc_u8 zero[] = {0x00}; + SDRM_ECC_CTX *ECC_ctx; if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->ecdsactx == NULL)) - { return CRYPTO_NULL_POINTER; - } ECC_ctx = crt->ctx->ecdsactx; ECC_ctx->PUBLIC_KEY->IsInfinity = 0; - if (PRIV_Data != NULL) - { + if (PRIV_Data != NULL) { retVal = SDRM_OS2BN(PRIV_Data, PRIV_Len, ECC_ctx->PRIV_KEY); + if (retVal != CRYPTO_SUCCESS) - { return retVal; - } } - if (PUB_X_Data != NULL && PUB_Y_Data != NULL) - { + if (PUB_X_Data != NULL && PUB_Y_Data != NULL) { retVal = SDRM_OS2BN(PUB_X_Data, PUB_X_Len, ECC_ctx->PUBLIC_KEY->x); + if (retVal != CRYPTO_SUCCESS) - { return retVal; - } retVal = SDRM_OS2BN(PUB_Y_Data, PUB_Y_Len, ECC_ctx->PUBLIC_KEY->y); + if (retVal != CRYPTO_SUCCESS) - { return retVal; - } } SDRM_OS2BN(zero, 0, ECC_ctx->PUBLIC_KEY->z); diff --git a/ssflib/dep/cryptocore/source/middle/cc_hmac.c b/ssflib/dep/cryptocore/source/middle/cc_hmac.c index 731fbbd..09edd9e 100644 --- a/ssflib/dep/cryptocore/source/middle/cc_hmac.c +++ b/ssflib/dep/cryptocore/source/middle/cc_hmac.c @@ -32,75 +32,74 @@ //////////////////////////////////////////////////////////////////////////// // Functions //////////////////////////////////////////////////////////////////////////// -int SDRM_getK0(cc_u8* k0, cc_u8* Key, cc_u32 KeyLen, cc_u32 Algorithm, cc_u32 B); +int SDRM_getK0(cc_u8 *k0, cc_u8 *Key, cc_u32 KeyLen, cc_u32 Algorithm, + cc_u32 B); /* - * @fn SDRM_HMAC_init - * @brief Parameter setting for mac code generation + * @fn SDRM_HMAC_init + * @brief Parameter setting for mac code generation * - * @param crt [out]crypto parameter - * @param Key [in]user key - * @param KeyLen [in]byte-length of Key + * @param crt [out]crypto parameter + * @param Key [in]user key + * @param KeyLen [in]byte-length of Key * - * @return CRYPTO_SUCCESS if no error is occured - * \n CRYPTO_NULL_POINTER if Parameter is NULL - * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed + * @return CRYPTO_SUCCESS if no error is occured + * \n CRYPTO_NULL_POINTER if Parameter is NULL + * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed */ int SDRM_HMAC_init(CryptoCoreContainer *crt, cc_u8 *Key, cc_u32 KeyLen) { SDRM_HMACContext *ctx; - cc_u8 *ipad; - cc_u32 i; + cc_u8 *ipad; + cc_u32 i; - if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->hmacctx == NULL) || (Key == NULL)) - { + if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->hmacctx == NULL) || + (Key == NULL)) return CRYPTO_NULL_POINTER; - } ctx = crt->ctx->hmacctx; ctx->algorithm = crt->alg; - switch(ctx->algorithm) - { - case ID_HMD5 : - ctx->B = SDRM_MD5_DATA_SIZE; - break; + switch (ctx->algorithm) { + case ID_HMD5: + ctx->B = SDRM_MD5_DATA_SIZE; + break; - case ID_HSHA1 : - ctx->B = SDRM_SHA1_DATA_SIZE; - break; + case ID_HSHA1: + ctx->B = SDRM_SHA1_DATA_SIZE; + break; - case ID_HSHA224 : - ctx->B = SDRM_SHA224_DATA_SIZE; - break; + case ID_HSHA224: + ctx->B = SDRM_SHA224_DATA_SIZE; + break; - case ID_HSHA256 : - ctx->B = SDRM_SHA256_DATA_SIZE; - break; + case ID_HSHA256: + ctx->B = SDRM_SHA256_DATA_SIZE; + break; #ifndef _OP64_NOTSUPPORTED - case ID_HSHA384 : - ctx->B = SDRM_SHA384_DATA_SIZE; - break; - case ID_HSHA512 : - ctx->B = SDRM_SHA512_DATA_SIZE; - break; + case ID_HSHA384: + ctx->B = SDRM_SHA384_DATA_SIZE; + break; + + case ID_HSHA512: + ctx->B = SDRM_SHA512_DATA_SIZE; + break; #endif //_OP64_NOTSUPPORTED - default : - return CRYPTO_INVALID_ARGUMENT; + default: + return CRYPTO_INVALID_ARGUMENT; } - ipad = (cc_u8*)malloc(ctx->B); + ipad = (cc_u8 *)malloc(ctx->B); + if (ipad == NULL) - { return CRYPTO_MEMORY_ALLOC_FAIL; - } - ctx->k0 = (cc_u8*)malloc(ctx->B); - if (ctx->k0 == NULL) - { + ctx->k0 = (cc_u8 *)malloc(ctx->B); + + if (ctx->k0 == NULL) { free(ipad); return CRYPTO_MEMORY_ALLOC_FAIL; } @@ -110,16 +109,11 @@ int SDRM_HMAC_init(CryptoCoreContainer *crt, cc_u8 *Key, cc_u32 KeyLen) //ipad = k0 xor ipad for (i = 0; i < ctx->B; i++) - { ipad[i] = ctx->k0[i] ^ 0x36; - } - if (i != ctx->B) - { + if (i != ctx->B) { for (; i < ctx->B; i++) - { ipad[i] = ctx->k0[i] ^ 0x36; - } } ctx->md5_ctx = NULL; @@ -131,109 +125,97 @@ int SDRM_HMAC_init(CryptoCoreContainer *crt, cc_u8 *Key, cc_u32 KeyLen) ctx->sha512_ctx = NULL; #endif //_OP64_NOTSUPPORTED - switch(ctx->algorithm) - { - case ID_HMD5 : - ctx->md5_ctx = (SDRM_MD5Context*)malloc(sizeof(SDRM_MD5Context)); - - if (ctx->md5_ctx == NULL) - { - if (ipad != NULL) - { - free(ipad); - } - return CRYPTO_MEMORY_ALLOC_FAIL; - } - - SDRM_MD5_Init(ctx->md5_ctx); - SDRM_MD5_Update(ctx->md5_ctx, ipad, ctx->B); - break; + switch (ctx->algorithm) { + case ID_HMD5: + ctx->md5_ctx = (SDRM_MD5Context *)malloc(sizeof(SDRM_MD5Context)); - case ID_HSHA1 : - ctx->sha1_ctx = (SDRM_SHA1Context*)malloc(sizeof(SDRM_SHA1Context)); + if (ctx->md5_ctx == NULL) { + if (ipad != NULL) + free(ipad); - if (ctx->sha1_ctx == NULL) - { - if (ipad != NULL) - { - free(ipad); - } - return CRYPTO_MEMORY_ALLOC_FAIL; - } + return CRYPTO_MEMORY_ALLOC_FAIL; + } - SDRM_SHA1_Init(ctx->sha1_ctx); - SDRM_SHA1_Update(ctx->sha1_ctx, ipad, ctx->B); - break; + SDRM_MD5_Init(ctx->md5_ctx); + SDRM_MD5_Update(ctx->md5_ctx, ipad, ctx->B); + break; - case ID_HSHA224 : - ctx->sha224_ctx = (SDRM_SHA224Context*)malloc(sizeof(SDRM_SHA224Context)); + case ID_HSHA1: + ctx->sha1_ctx = (SDRM_SHA1Context *)malloc(sizeof(SDRM_SHA1Context)); - if (ctx->sha224_ctx == NULL) - { - if (ipad != NULL) - { - free(ipad); - } - return CRYPTO_MEMORY_ALLOC_FAIL; - } + if (ctx->sha1_ctx == NULL) { + if (ipad != NULL) + free(ipad); - SDRM_SHA224_Init(ctx->sha224_ctx); - SDRM_SHA224_Update(ctx->sha224_ctx, ipad, ctx->B); - break; + return CRYPTO_MEMORY_ALLOC_FAIL; + } - case ID_HSHA256 : - ctx->sha256_ctx = (SDRM_SHA256Context*)malloc(sizeof(SDRM_SHA256Context)); + SDRM_SHA1_Init(ctx->sha1_ctx); + SDRM_SHA1_Update(ctx->sha1_ctx, ipad, ctx->B); + break; - if (ctx->sha256_ctx == NULL) - { - if (ipad != NULL) - { - free(ipad); - } - return CRYPTO_MEMORY_ALLOC_FAIL; - } + case ID_HSHA224: + ctx->sha224_ctx = (SDRM_SHA224Context *)malloc(sizeof(SDRM_SHA224Context)); - SDRM_SHA256_Init(ctx->sha256_ctx); - SDRM_SHA256_Update(ctx->sha256_ctx, ipad, ctx->B); - break; + if (ctx->sha224_ctx == NULL) { + if (ipad != NULL) + free(ipad); + + return CRYPTO_MEMORY_ALLOC_FAIL; + } + + SDRM_SHA224_Init(ctx->sha224_ctx); + SDRM_SHA224_Update(ctx->sha224_ctx, ipad, ctx->B); + break; + + case ID_HSHA256: + ctx->sha256_ctx = (SDRM_SHA256Context *)malloc(sizeof(SDRM_SHA256Context)); + + if (ctx->sha256_ctx == NULL) { + if (ipad != NULL) + free(ipad); + + return CRYPTO_MEMORY_ALLOC_FAIL; + } + + SDRM_SHA256_Init(ctx->sha256_ctx); + SDRM_SHA256_Update(ctx->sha256_ctx, ipad, ctx->B); + break; #ifndef _OP64_NOTSUPPORTED - case ID_HSHA384 : - ctx->sha384_ctx = (SDRM_SHA384Context*)malloc(sizeof(SDRM_SHA384Context)); - - if (ctx->sha384_ctx == NULL) - { - if (ipad != NULL) - { - free(ipad); - } - return CRYPTO_MEMORY_ALLOC_FAIL; - } - - SDRM_SHA384_Init(ctx->sha384_ctx); - SDRM_SHA384_Update(ctx->sha384_ctx, ipad, ctx->B); - break; - case ID_HSHA512 : - ctx->sha512_ctx = (SDRM_SHA512Context*)malloc(sizeof(SDRM_SHA512Context)); + case ID_HSHA384: + ctx->sha384_ctx = (SDRM_SHA384Context *)malloc(sizeof(SDRM_SHA384Context)); - if (ctx->sha512_ctx == NULL) - { - if (ipad != NULL) - { - free(ipad); - } - return CRYPTO_MEMORY_ALLOC_FAIL; - } + if (ctx->sha384_ctx == NULL) { + if (ipad != NULL) + free(ipad); - SDRM_SHA512_Init(ctx->sha512_ctx); - SDRM_SHA512_Update(ctx->sha512_ctx, ipad, ctx->B); - break; + return CRYPTO_MEMORY_ALLOC_FAIL; + } + + SDRM_SHA384_Init(ctx->sha384_ctx); + SDRM_SHA384_Update(ctx->sha384_ctx, ipad, ctx->B); + break; + + case ID_HSHA512: + ctx->sha512_ctx = (SDRM_SHA512Context *)malloc(sizeof(SDRM_SHA512Context)); + + if (ctx->sha512_ctx == NULL) { + if (ipad != NULL) + free(ipad); + + return CRYPTO_MEMORY_ALLOC_FAIL; + } + + SDRM_SHA512_Init(ctx->sha512_ctx); + SDRM_SHA512_Update(ctx->sha512_ctx, ipad, ctx->B); + break; #endif //_OP64_NOTSUPPORTED - default : - free(ipad); - return CRYPTO_INVALID_ARGUMENT; + default: + free(ipad); + return CRYPTO_INVALID_ARGUMENT; } free(ipad); @@ -241,250 +223,239 @@ int SDRM_HMAC_init(CryptoCoreContainer *crt, cc_u8 *Key, cc_u32 KeyLen) } /* - * @fn SDRM_HMAC_update - * @brief process data blocks + * @fn SDRM_HMAC_update + * @brief process data blocks * - * @param crt [out]crypto parameter - * @param msg [in]data block - * @param msgLen [in]byte-length of Text + * @param crt [out]crypto parameter + * @param msg [in]data block + * @param msgLen [in]byte-length of Text * - * @return CRYPTO_SUCCESS if no error is occured - * \n CRYPTO_NULL_POINTER if Parameter is NULL - * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed + * @return CRYPTO_SUCCESS if no error is occured + * \n CRYPTO_NULL_POINTER if Parameter is NULL + * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed */ int SDRM_HMAC_update(CryptoCoreContainer *crt, cc_u8 *msg, cc_u32 msgLen) { if (msgLen == 0) - { return CRYPTO_SUCCESS; - } - if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->hmacctx == NULL) || (msg == NULL)) - { + if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->hmacctx == NULL) || + (msg == NULL)) return CRYPTO_NULL_POINTER; - } - switch(crt->ctx->hmacctx->algorithm) - { - case ID_HMD5 : - SDRM_MD5_Update(crt->ctx->hmacctx->md5_ctx, msg, msgLen); - break; - case ID_HSHA1 : - SDRM_SHA1_Update(crt->ctx->hmacctx->sha1_ctx, msg, msgLen); - break; - case ID_HSHA224 : - SDRM_SHA224_Update(crt->ctx->hmacctx->sha224_ctx, msg, msgLen); - break; - case ID_HSHA256 : - SDRM_SHA256_Update(crt->ctx->hmacctx->sha256_ctx, msg, msgLen); - break; + switch (crt->ctx->hmacctx->algorithm) { + case ID_HMD5: + SDRM_MD5_Update(crt->ctx->hmacctx->md5_ctx, msg, msgLen); + break; + + case ID_HSHA1: + SDRM_SHA1_Update(crt->ctx->hmacctx->sha1_ctx, msg, msgLen); + break; + + case ID_HSHA224: + SDRM_SHA224_Update(crt->ctx->hmacctx->sha224_ctx, msg, msgLen); + break; + + case ID_HSHA256: + SDRM_SHA256_Update(crt->ctx->hmacctx->sha256_ctx, msg, msgLen); + break; #ifndef _OP64_NOTSUPPORTED - case ID_HSHA384 : - SDRM_SHA384_Update(crt->ctx->hmacctx->sha384_ctx, msg, msgLen); - break; - case ID_HSHA512 : - SDRM_SHA512_Update(crt->ctx->hmacctx->sha512_ctx, msg, msgLen); - break; + + case ID_HSHA384: + SDRM_SHA384_Update(crt->ctx->hmacctx->sha384_ctx, msg, msgLen); + break; + + case ID_HSHA512: + SDRM_SHA512_Update(crt->ctx->hmacctx->sha512_ctx, msg, msgLen); + break; #endif //OP64_NOTSUPPORTED - default : - return CRYPTO_INVALID_ARGUMENT; + default: + return CRYPTO_INVALID_ARGUMENT; } return CRYPTO_SUCCESS; } /* - * @fn SDRM_HMAC_final - * @brief process last data block + * @fn SDRM_HMAC_final + * @brief process last data block * - * @param crt [in]crypto parameter - * @param output [out]generated MAC - * @param outputLen [out]byte-length of output + * @param crt [in]crypto parameter + * @param output [out]generated MAC + * @param outputLen [out]byte-length of output * - * @return CRYPTO_SUCCESS if no error is occured - * \n CRYPTO_NULL_POINTER if Parameter is NULL - * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed + * @return CRYPTO_SUCCESS if no error is occured + * \n CRYPTO_NULL_POINTER if Parameter is NULL + * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed */ int SDRM_HMAC_final(CryptoCoreContainer *crt, cc_u8 *output, cc_u32 *outputLen) { - SDRM_HMACContext *ctx; - SDRM_MD5Context MD5ctx; - SDRM_SHA1Context SHA1ctx; - SDRM_SHA224Context SHA224ctx; - SDRM_SHA256Context SHA256ctx; + SDRM_HMACContext *ctx; + SDRM_MD5Context MD5ctx; + SDRM_SHA1Context SHA1ctx; + SDRM_SHA224Context SHA224ctx; + SDRM_SHA256Context SHA256ctx; #ifndef _OP64_NOTSUPPORTED - SDRM_SHA384Context SHA384ctx; - SDRM_SHA512Context SHA512ctx; + SDRM_SHA384Context SHA384ctx; + SDRM_SHA512Context SHA512ctx; #endif //_OP64_NOTSUPPORTED - cc_u8 Step6_Result[64]; - cc_u32 HashLen, i; + cc_u8 Step6_Result[64]; + cc_u32 HashLen, i; - if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->hmacctx == NULL) || (output == NULL)) - { + if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->hmacctx == NULL) || + (output == NULL)) return CRYPTO_NULL_POINTER; - } ctx = crt->ctx->hmacctx; //k0 = k0 xor opad for (i = 0; i < ctx->B; i++) - { ctx->k0[i] ^= 0x5c; - } - if (i != ctx->B) - { + if (i != ctx->B) { for (; i < ctx->B; i++) - { ctx->k0[i] ^= 0x5c; - } } //Step6 : get H((k0 xor ipad) | text) & Step 9 : make hash - switch(ctx->algorithm) - { - case ID_HMD5 : - SDRM_MD5_Final(ctx->md5_ctx, Step6_Result); - free(ctx->md5_ctx); + switch (ctx->algorithm) { + case ID_HMD5: + SDRM_MD5_Final(ctx->md5_ctx, Step6_Result); + free(ctx->md5_ctx); - HashLen = SDRM_MD5_BLOCK_SIZ; + HashLen = SDRM_MD5_BLOCK_SIZ; - SDRM_MD5_Init(&MD5ctx); - SDRM_MD5_Update(&MD5ctx, ctx->k0, ctx->B); - SDRM_MD5_Update(&MD5ctx, Step6_Result, HashLen); - SDRM_MD5_Final(&MD5ctx, output); + SDRM_MD5_Init(&MD5ctx); + SDRM_MD5_Update(&MD5ctx, ctx->k0, ctx->B); + SDRM_MD5_Update(&MD5ctx, Step6_Result, HashLen); + SDRM_MD5_Final(&MD5ctx, output); - break; + break; - case ID_HSHA1 : - SDRM_SHA1_Final(ctx->sha1_ctx, Step6_Result); - free(ctx->sha1_ctx); + case ID_HSHA1: + SDRM_SHA1_Final(ctx->sha1_ctx, Step6_Result); + free(ctx->sha1_ctx); - HashLen = SDRM_SHA1_BLOCK_SIZ; + HashLen = SDRM_SHA1_BLOCK_SIZ; - SDRM_SHA1_Init(&SHA1ctx); - SDRM_SHA1_Update(&SHA1ctx, ctx->k0, ctx->B); - SDRM_SHA1_Update(&SHA1ctx, Step6_Result, HashLen); - SDRM_SHA1_Final(&SHA1ctx, output); + SDRM_SHA1_Init(&SHA1ctx); + SDRM_SHA1_Update(&SHA1ctx, ctx->k0, ctx->B); + SDRM_SHA1_Update(&SHA1ctx, Step6_Result, HashLen); + SDRM_SHA1_Final(&SHA1ctx, output); - break; + break; - case ID_HSHA224 : - SDRM_SHA224_Final(ctx->sha224_ctx, Step6_Result); - free(ctx->sha224_ctx); + case ID_HSHA224: + SDRM_SHA224_Final(ctx->sha224_ctx, Step6_Result); + free(ctx->sha224_ctx); - HashLen = SDRM_SHA224_BLOCK_SIZ; + HashLen = SDRM_SHA224_BLOCK_SIZ; - SDRM_SHA224_Init(&SHA224ctx); - SDRM_SHA224_Update(&SHA224ctx, ctx->k0, ctx->B); - SDRM_SHA224_Update(&SHA224ctx, Step6_Result, HashLen); - SDRM_SHA224_Final(&SHA224ctx, output); + SDRM_SHA224_Init(&SHA224ctx); + SDRM_SHA224_Update(&SHA224ctx, ctx->k0, ctx->B); + SDRM_SHA224_Update(&SHA224ctx, Step6_Result, HashLen); + SDRM_SHA224_Final(&SHA224ctx, output); - break; + break; - case ID_HSHA256 : - SDRM_SHA256_Final(ctx->sha256_ctx, Step6_Result); - free(ctx->sha256_ctx); + case ID_HSHA256: + SDRM_SHA256_Final(ctx->sha256_ctx, Step6_Result); + free(ctx->sha256_ctx); - HashLen = SDRM_SHA256_BLOCK_SIZ; + HashLen = SDRM_SHA256_BLOCK_SIZ; - SDRM_SHA256_Init(&SHA256ctx); - SDRM_SHA256_Update(&SHA256ctx, ctx->k0, ctx->B); - SDRM_SHA256_Update(&SHA256ctx, Step6_Result, HashLen); - SDRM_SHA256_Final(&SHA256ctx, output); + SDRM_SHA256_Init(&SHA256ctx); + SDRM_SHA256_Update(&SHA256ctx, ctx->k0, ctx->B); + SDRM_SHA256_Update(&SHA256ctx, Step6_Result, HashLen); + SDRM_SHA256_Final(&SHA256ctx, output); - break; + break; #ifndef _OP64_NOTSUPPORTED - case ID_HSHA384 : - SDRM_SHA384_Final(ctx->sha384_ctx, Step6_Result); - free(ctx->sha384_ctx); - HashLen = SDRM_SHA384_BLOCK_SIZ; + case ID_HSHA384: + SDRM_SHA384_Final(ctx->sha384_ctx, Step6_Result); + free(ctx->sha384_ctx); - SDRM_SHA384_Init(&SHA384ctx); - SDRM_SHA384_Update(&SHA384ctx, ctx->k0, ctx->B); - SDRM_SHA384_Update(&SHA384ctx, Step6_Result, HashLen); - SDRM_SHA384_Final(&SHA384ctx, output); + HashLen = SDRM_SHA384_BLOCK_SIZ; - break; + SDRM_SHA384_Init(&SHA384ctx); + SDRM_SHA384_Update(&SHA384ctx, ctx->k0, ctx->B); + SDRM_SHA384_Update(&SHA384ctx, Step6_Result, HashLen); + SDRM_SHA384_Final(&SHA384ctx, output); - case ID_HSHA512 : - SDRM_SHA512_Final(ctx->sha512_ctx, Step6_Result); - free(ctx->sha512_ctx); + break; - HashLen = SDRM_SHA512_BLOCK_SIZ; + case ID_HSHA512: + SDRM_SHA512_Final(ctx->sha512_ctx, Step6_Result); + free(ctx->sha512_ctx); - SDRM_SHA512_Init(&SHA512ctx); - SDRM_SHA512_Update(&SHA512ctx, ctx->k0, ctx->B); - SDRM_SHA512_Update(&SHA512ctx, Step6_Result, HashLen); - SDRM_SHA512_Final(&SHA512ctx, output); + HashLen = SDRM_SHA512_BLOCK_SIZ; - break; + SDRM_SHA512_Init(&SHA512ctx); + SDRM_SHA512_Update(&SHA512ctx, ctx->k0, ctx->B); + SDRM_SHA512_Update(&SHA512ctx, Step6_Result, HashLen); + SDRM_SHA512_Final(&SHA512ctx, output); + + break; #endif - default : - if (ctx->k0) { + default: + if (ctx->k0) free(ctx->k0); - } - return CRYPTO_INVALID_ARGUMENT; + + return CRYPTO_INVALID_ARGUMENT; } if (outputLen != NULL) - { *outputLen = HashLen; - } if (ctx->k0) - { free(ctx->k0); - } return CRYPTO_SUCCESS; } /* - * @fn SDRM_HMAC_getMAC - * @brief generate h-mac code + * @fn SDRM_HMAC_getMAC + * @brief generate h-mac code * - * @param crt [in]crypto parameter - * @param Key [in]user key - * @param KeyLen [in]byte-length of Key - * @param msg [in]data block - * @param msgLen [in]byte-length of Text - * @param output [out]generated MAC - * @param outputLen [out]byte-length of output + * @param crt [in]crypto parameter + * @param Key [in]user key + * @param KeyLen [in]byte-length of Key + * @param msg [in]data block + * @param msgLen [in]byte-length of Text + * @param output [out]generated MAC + * @param outputLen [out]byte-length of output * - * @return CRYPTO_SUCCESS if no error is occured + * @return CRYPTO_SUCCESS if no error is occured */ -int SDRM_HMAC_getMAC(CryptoCoreContainer *crt, cc_u8 *Key, cc_u32 KeyLen, cc_u8 *msg, cc_u32 msgLen, cc_u8 *output, cc_u32 *outputLen) +int SDRM_HMAC_getMAC(CryptoCoreContainer *crt, cc_u8 *Key, cc_u32 KeyLen, + cc_u8 *msg, cc_u32 msgLen, cc_u8 *output, cc_u32 *outputLen) { int result; - if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->hmacctx == NULL) || (Key == NULL) || (output == NULL)) - { + if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->hmacctx == NULL) || + (Key == NULL) || (output == NULL)) return CRYPTO_NULL_POINTER; - } result = SDRM_HMAC_init(crt, Key, KeyLen); + if (result != CRYPTO_SUCCESS) - { return result; - } result = SDRM_HMAC_update(crt, msg, msgLen); + if (result != CRYPTO_SUCCESS) - { return result; - } return SDRM_HMAC_final(crt, output, outputLen); } -int SDRM_getK0(cc_u8* k0, cc_u8* Key, cc_u32 KeyLen, cc_u32 Algorithm, cc_u32 B) +int SDRM_getK0(cc_u8 *k0, cc_u8 *Key, cc_u32 KeyLen, cc_u32 Algorithm, cc_u32 B) { SDRM_MD5Context MD5ctx; SDRM_SHA1Context SHA1ctx; @@ -497,83 +468,79 @@ int SDRM_getK0(cc_u8* k0, cc_u8* Key, cc_u32 KeyLen, cc_u32 Algorithm, cc_u32 B) int L; - if (KeyLen == B) - { + if (KeyLen == B) { //if the length of K = B : set K0 = K memcpy(k0, Key, B); - } - else if (KeyLen > B) - { + } else if (KeyLen > B) { //if the length of K > B : get hask(K) and append (B - L) zeros //get hash(K) - switch(Algorithm) - { - case ID_HMD5 : - SDRM_MD5_Init(&MD5ctx); - SDRM_MD5_Update(&MD5ctx, Key, KeyLen); - SDRM_MD5_Final(&MD5ctx, k0); + switch (Algorithm) { + case ID_HMD5: + SDRM_MD5_Init(&MD5ctx); + SDRM_MD5_Update(&MD5ctx, Key, KeyLen); + SDRM_MD5_Final(&MD5ctx, k0); - L = SDRM_MD5_BLOCK_SIZ; + L = SDRM_MD5_BLOCK_SIZ; - break; + break; - case ID_HSHA1 : - SDRM_SHA1_Init(&SHA1ctx); - SDRM_SHA1_Update(&SHA1ctx, Key, KeyLen); - SDRM_SHA1_Final(&SHA1ctx, k0); + case ID_HSHA1: + SDRM_SHA1_Init(&SHA1ctx); + SDRM_SHA1_Update(&SHA1ctx, Key, KeyLen); + SDRM_SHA1_Final(&SHA1ctx, k0); - L = SDRM_SHA1_BLOCK_SIZ; + L = SDRM_SHA1_BLOCK_SIZ; - break; + break; - case ID_HSHA224 : - SDRM_SHA224_Init(&SHA224ctx); - SDRM_SHA224_Update(&SHA224ctx, Key, KeyLen); - SDRM_SHA224_Final(&SHA224ctx, k0); + case ID_HSHA224: + SDRM_SHA224_Init(&SHA224ctx); + SDRM_SHA224_Update(&SHA224ctx, Key, KeyLen); + SDRM_SHA224_Final(&SHA224ctx, k0); - L = SDRM_SHA224_BLOCK_SIZ; + L = SDRM_SHA224_BLOCK_SIZ; - break; + break; - case ID_HSHA256 : - SDRM_SHA256_Init(&SHA256ctx); - SDRM_SHA256_Update(&SHA256ctx, Key, KeyLen); - SDRM_SHA256_Final(&SHA256ctx, k0); + case ID_HSHA256: + SDRM_SHA256_Init(&SHA256ctx); + SDRM_SHA256_Update(&SHA256ctx, Key, KeyLen); + SDRM_SHA256_Final(&SHA256ctx, k0); - L = SDRM_SHA256_BLOCK_SIZ; + L = SDRM_SHA256_BLOCK_SIZ; - break; + break; #ifndef _OP64_NOTSUPPORTED - case ID_HSHA384 : - SDRM_SHA384_Init(&SHA384ctx); - SDRM_SHA384_Update(&SHA384ctx, Key, KeyLen); - SDRM_SHA384_Final(&SHA384ctx, k0); - L = SDRM_SHA384_BLOCK_SIZ; + case ID_HSHA384: + SDRM_SHA384_Init(&SHA384ctx); + SDRM_SHA384_Update(&SHA384ctx, Key, KeyLen); + SDRM_SHA384_Final(&SHA384ctx, k0); - break; + L = SDRM_SHA384_BLOCK_SIZ; - case ID_HSHA512 : - SDRM_SHA512_Init(&SHA512ctx); - SDRM_SHA512_Update(&SHA512ctx, Key, KeyLen); - SDRM_SHA512_Final(&SHA512ctx, k0); + break; - L = SDRM_SHA512_BLOCK_SIZ; + case ID_HSHA512: + SDRM_SHA512_Init(&SHA512ctx); + SDRM_SHA512_Update(&SHA512ctx, Key, KeyLen); + SDRM_SHA512_Final(&SHA512ctx, k0); + + L = SDRM_SHA512_BLOCK_SIZ; - break; + break; #endif - default : - return CRYPTO_INVALID_ARGUMENT; + default: + return CRYPTO_INVALID_ARGUMENT; } //append zeros memset(k0 + L, 0x00, B - L); - } - else { + } else { //if the length of K < B : append zerots to the end of K memcpy(k0, Key, KeyLen); memset(k0 + KeyLen, 0x00, B - KeyLen); @@ -582,4 +549,4 @@ int SDRM_getK0(cc_u8* k0, cc_u8* Key, cc_u32 KeyLen, cc_u32 Algorithm, cc_u32 B) return CRYPTO_SUCCESS; } -/***************************** End of File *****************************/ \ No newline at end of file +/***************************** End of File *****************************/ diff --git a/ssflib/dep/cryptocore/source/middle/cc_rng.c b/ssflib/dep/cryptocore/source/middle/cc_rng.c index 42d38a1..61f0f85 100644 --- a/ssflib/dep/cryptocore/source/middle/cc_rng.c +++ b/ssflib/dep/cryptocore/source/middle/cc_rng.c @@ -31,20 +31,19 @@ // Functions //////////////////////////////////////////////////////////////////////////// /* - * @fn SDRM_X931_seed - * @brief Seed RNG System + * @fn SDRM_X931_seed + * @brief Seed RNG System * - * @param crt [in]crypto env structure - * @param seed [in]seed for RNG System + * @param crt [in]crypto env structure + * @param seed [in]seed for RNG System * - * @return CRYPTO_SUCCESS if success + * @return CRYPTO_SUCCESS if success */ int SDRM_X931_seed(CryptoCoreContainer *crt, cc_u8 *seed) { - if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->x931ctx == NULL) || (seed == NULL)) - { + if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->x931ctx == NULL) || + (seed == NULL)) return CRYPTO_NULL_POINTER; - } memcpy(crt->ctx->x931ctx->Seed, seed, SDRM_X931_SEED_SIZ); @@ -52,30 +51,30 @@ int SDRM_X931_seed(CryptoCoreContainer *crt, cc_u8 *seed) } /* - * @fn SDRM_X931_get - * @brief generate random number + * @fn SDRM_X931_get + * @brief generate random number * - * @param crt [in]crypto env structure - * @param bitLength [in]bit length for generated number - * @param data [out]generated data + * @param crt [in]crypto env structure + * @param bitLength [in]bit length for generated number + * @param data [out]generated data * - * @return CRYPTO_SUCCESS if success + * @return CRYPTO_SUCCESS if success */ -int SDRM_X931_get(CryptoCoreContainer *crt, cc_u32 bitLength, cc_u8 *data) +int SDRM_X931_get(CryptoCoreContainer *crt, cc_u32 bitLength, cc_u8 *data) { - if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->x931ctx == NULL) || (data == NULL)) - { + if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->x931ctx == NULL) || + (data == NULL)) return CRYPTO_NULL_POINTER; - } #ifdef _WIN32_WCE srand(GetTickCount()); #else static int add_value = 0; - if(++add_value == 10000) add_value = 0; - srand(time(NULL) + add_value ); + if (++add_value == 10000) add_value = 0; + + srand(time(NULL) + add_value); #endif diff --git a/ssflib/dep/cryptocore/source/middle/cc_rsa.c b/ssflib/dep/cryptocore/source/middle/cc_rsa.c index e0a2a36..8aad8ad 100644 --- a/ssflib/dep/cryptocore/source/middle/cc_rsa.c +++ b/ssflib/dep/cryptocore/source/middle/cc_rsa.c @@ -33,77 +33,81 @@ // Functions ////////////////////////////////////////////////////////////////////////// /* - * @fn SDRM_RSAContext *SDRM_RSA_InitCrt(cc_u32 KeyByteLen) + * @fn SDRM_RSAContext *SDRM_RSA_InitCrt(cc_u32 KeyByteLen) * - * @brief generate RSA Context + * @brief generate RSA Context * - * @return pointer to the generated context - * \n NULL if memory allocation is failed + * @return pointer to the generated context + * \n NULL if memory allocation is failed */ SDRM_RSAContext *SDRM_RSA_InitCrt(cc_u32 KeyByteLen) { SDRM_RSAContext *ctx; - cc_u32 RSA_KeyByteLen = KeyByteLen; - cc_u8 *pbBuf = (cc_u8*)malloc(sizeof(SDRM_RSAContext) + SDRM_RSA_ALLOC_SIZE * 8); + cc_u32 RSA_KeyByteLen = KeyByteLen; + cc_u8 *pbBuf = (cc_u8 *)malloc(sizeof(SDRM_RSAContext) + + SDRM_RSA_ALLOC_SIZE * 8); if (pbBuf == NULL) - { return NULL; - } - - ctx = (SDRM_RSAContext*)(void*)pbBuf; - ctx->n = SDRM_BN_Alloc((cc_u8*)ctx + sizeof(SDRM_RSAContext), SDRM_RSA_BN_BUFSIZE); - ctx->e = SDRM_BN_Alloc((cc_u8*)ctx->n + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE); - ctx->d = SDRM_BN_Alloc((cc_u8*)ctx->e + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE); - ctx->p = SDRM_BN_Alloc((cc_u8*)ctx->d + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE); - ctx->q = SDRM_BN_Alloc((cc_u8*)ctx->p + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE); - ctx->dmodp1 = SDRM_BN_Alloc((cc_u8*)ctx->q + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE); - ctx->dmodq1 = SDRM_BN_Alloc((cc_u8*)ctx->dmodp1 + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE); - ctx->iqmodp = SDRM_BN_Alloc((cc_u8*)ctx->dmodq1 + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE); - ctx->crt_operation = (unsigned int)-1; - ctx->k = RSA_KeyByteLen; + ctx = (SDRM_RSAContext *)(void *)pbBuf; + ctx->n = SDRM_BN_Alloc((cc_u8 *)ctx + sizeof(SDRM_RSAContext), + SDRM_RSA_BN_BUFSIZE); + ctx->e = SDRM_BN_Alloc((cc_u8 *)ctx->n + SDRM_RSA_ALLOC_SIZE, + SDRM_RSA_BN_BUFSIZE); + ctx->d = SDRM_BN_Alloc((cc_u8 *)ctx->e + SDRM_RSA_ALLOC_SIZE, + SDRM_RSA_BN_BUFSIZE); + ctx->p = SDRM_BN_Alloc((cc_u8 *)ctx->d + SDRM_RSA_ALLOC_SIZE, + SDRM_RSA_BN_BUFSIZE); + ctx->q = SDRM_BN_Alloc((cc_u8 *)ctx->p + SDRM_RSA_ALLOC_SIZE, + SDRM_RSA_BN_BUFSIZE); + ctx->dmodp1 = SDRM_BN_Alloc((cc_u8 *)ctx->q + SDRM_RSA_ALLOC_SIZE, + SDRM_RSA_BN_BUFSIZE); + ctx->dmodq1 = SDRM_BN_Alloc((cc_u8 *)ctx->dmodp1 + SDRM_RSA_ALLOC_SIZE, + SDRM_RSA_BN_BUFSIZE); + ctx->iqmodp = SDRM_BN_Alloc((cc_u8 *)ctx->dmodq1 + SDRM_RSA_ALLOC_SIZE, + SDRM_RSA_BN_BUFSIZE); + + ctx->crt_operation = (unsigned int) - 1; + ctx->k = RSA_KeyByteLen; return ctx; } /* - * @fn int SDRM_RSA_setNED(CryptoCoreContainer *crt, cc_u32 PaddingMethod, cc_u8* RSA_N_Data, cc_u32 RSA_N_Len, cc_u8* RSA_E_Data, cc_u32 RSA_E_Len, cc_u8* RSA_D_Data, cc_u32 RSA_D_Len) - * @brief set RSA parameters + * @fn int SDRM_RSA_setNED(CryptoCoreContainer *crt, cc_u32 PaddingMethod, cc_u8* RSA_N_Data, cc_u32 RSA_N_Len, cc_u8* RSA_E_Data, cc_u32 RSA_E_Len, cc_u8* RSA_D_Data, cc_u32 RSA_D_Len) + * @brief set RSA parameters * - * @param crt [out]rsa context - * @param PaddingMethod [in]padding method - * @param RSA_N_Data [in]n value - * @param RSA_N_Len [in]byte-length of n - * @param RSA_E_Data [in]e value - * @param RSA_E_Len [in]byte-length of e - * @param RSA_D_Data [in]d value - * @param RSA_D_Len [in]byte-length of d + * @param crt [out]rsa context + * @param PaddingMethod [in]padding method + * @param RSA_N_Data [in]n value + * @param RSA_N_Len [in]byte-length of n + * @param RSA_E_Data [in]e value + * @param RSA_E_Len [in]byte-length of e + * @param RSA_D_Data [in]d value + * @param RSA_D_Len [in]byte-length of d * - * @return CRYPTO_SUCCESS if no error is occured - * \n CRYPTO_NULL_POINTER if an argument is a null pointer + * @return CRYPTO_SUCCESS if no error is occured + * \n CRYPTO_NULL_POINTER if an argument is a null pointer */ int SDRM_RSA_setNED(CryptoCoreContainer *crt, cc_u32 PaddingMethod, - cc_u8* RSA_N_Data, cc_u32 RSA_N_Len, - cc_u8* RSA_E_Data, cc_u32 RSA_E_Len, - cc_u8* RSA_D_Data, cc_u32 RSA_D_Len) + cc_u8 *RSA_N_Data, cc_u32 RSA_N_Len, + cc_u8 *RSA_E_Data, cc_u32 RSA_E_Len, + cc_u8 *RSA_D_Data, cc_u32 RSA_D_Len) { - if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->rsactx == NULL) || (RSA_N_Data == NULL)) - { + if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->rsactx == NULL) || + (RSA_N_Data == NULL)) return CRYPTO_NULL_POINTER; - } SDRM_OS2BN(RSA_N_Data, RSA_N_Len, crt->ctx->rsactx->n); SDRM_BN_OPTIMIZE_LENGTH(crt->ctx->rsactx->n); - if (RSA_E_Data != NULL) - { + if (RSA_E_Data != NULL) { SDRM_OS2BN(RSA_E_Data, RSA_E_Len, crt->ctx->rsactx->e); SDRM_BN_OPTIMIZE_LENGTH(crt->ctx->rsactx->e); } - if (RSA_D_Data != NULL) - { + if (RSA_D_Data != NULL) { SDRM_OS2BN(RSA_D_Data, RSA_D_Len, crt->ctx->rsactx->d); SDRM_BN_OPTIMIZE_LENGTH(crt->ctx->rsactx->d); } @@ -115,119 +119,100 @@ int SDRM_RSA_setNED(CryptoCoreContainer *crt, cc_u32 PaddingMethod, } /* - * @fn int SDRM_RSA_setNEDPQ(CryptoCoreContainer *crt, cc_u32 PaddingMethod, - * cc_u8* RSA_N_Data, cc_u32 RSA_N_Len, - * cc_u8* RSA_E_Data, cc_u32 RSA_E_Len, - * cc_u8* RSA_D_Data, cc_u32 RSA_D_Len, - * cc_u8* RSA_P_Data, cc_u32 RSA_P_Len, - * cc_u8* RSA_Q_Data, cc_u32 RSA_Q_Len, - * cc_u8* RSA_DmodP1_Data, cc_u32 RSA_DmodP1_Len, - * cc_u8* RSA_DmodQ1_Data, cc_u32 RSA_DmodQ1_Len, - * cc_u8* RSA_iQmodP_Data, cc_u32 RSA_iQmodP_Len) + * @fn int SDRM_RSA_setNEDPQ(CryptoCoreContainer *crt, cc_u32 PaddingMethod, + * cc_u8* RSA_N_Data, cc_u32 RSA_N_Len, + * cc_u8* RSA_E_Data, cc_u32 RSA_E_Len, + * cc_u8* RSA_D_Data, cc_u32 RSA_D_Len, + * cc_u8* RSA_P_Data, cc_u32 RSA_P_Len, + * cc_u8* RSA_Q_Data, cc_u32 RSA_Q_Len, + * cc_u8* RSA_DmodP1_Data, cc_u32 RSA_DmodP1_Len, + * cc_u8* RSA_DmodQ1_Data, cc_u32 RSA_DmodQ1_Len, + * cc_u8* RSA_iQmodP_Data, cc_u32 RSA_iQmodP_Len) * - * @brief set RSA parameters + * @brief set RSA parameters * - * @param crt [out]rsa context - * @param PaddingMethod [in]padding method - * @param RSA_N_Data [in]n value - * @param RSA_N_Len [in]byte-length of n - * @param RSA_E_Data [in]e value - * @param RSA_E_Len [in]byte-length of e - * @param RSA_D_Data [in]d value - * @param RSA_D_Len [in]byte-length of d - * @param RSA_P_Data [in]p value - * @param RSA_P_Len [in]byte-length of p - * @param RSA_Q_Data [in]q value - * @param RSA_Q_Len [in]byte-length of q - * @param RSA_DmodP1_Data [in]d mod (p-1) value - * @param RSA_DmodP1_Len [in]byte-length of d mod (p-1) - * @param RSA_DmodQ1_Data [in]d mod (q-1) value - * @param RSA_DmodQ1_Len [in]byte-length of d mod (q-1) - * @param RSA_iQmodP_Data [in]q^(-1) mod p value - * @param RSA_iQmodP_Len [in]byte-length of q^(-1) mod p + * @param crt [out]rsa context + * @param PaddingMethod [in]padding method + * @param RSA_N_Data [in]n value + * @param RSA_N_Len [in]byte-length of n + * @param RSA_E_Data [in]e value + * @param RSA_E_Len [in]byte-length of e + * @param RSA_D_Data [in]d value + * @param RSA_D_Len [in]byte-length of d + * @param RSA_P_Data [in]p value + * @param RSA_P_Len [in]byte-length of p + * @param RSA_Q_Data [in]q value + * @param RSA_Q_Len [in]byte-length of q + * @param RSA_DmodP1_Data [in]d mod (p-1) value + * @param RSA_DmodP1_Len [in]byte-length of d mod (p-1) + * @param RSA_DmodQ1_Data [in]d mod (q-1) value + * @param RSA_DmodQ1_Len [in]byte-length of d mod (q-1) + * @param RSA_iQmodP_Data [in]q^(-1) mod p value + * @param RSA_iQmodP_Len [in]byte-length of q^(-1) mod p * - * @return CRYPTO_SUCCESS if no error is occured - * \n CRYPTO_NULL_POINTER if an argument is a null pointer + * @return CRYPTO_SUCCESS if no error is occured + * \n CRYPTO_NULL_POINTER if an argument is a null pointer */ int SDRM_RSA_setNEDPQ(CryptoCoreContainer *crt, cc_u32 PaddingMethod, - cc_u8* RSA_N_Data, cc_u32 RSA_N_Len, - cc_u8* RSA_E_Data, cc_u32 RSA_E_Len, - cc_u8* RSA_D_Data, cc_u32 RSA_D_Len, - cc_u8* RSA_P_Data, cc_u32 RSA_P_Len, - cc_u8* RSA_Q_Data, cc_u32 RSA_Q_Len, - cc_u8* RSA_DmodP1_Data, cc_u32 RSA_DmodP1_Len, - cc_u8* RSA_DmodQ1_Data, cc_u32 RSA_DmodQ1_Len, - cc_u8* RSA_iQmodP_Data, cc_u32 RSA_iQmodP_Len) + cc_u8 *RSA_N_Data, cc_u32 RSA_N_Len, + cc_u8 *RSA_E_Data, cc_u32 RSA_E_Len, + cc_u8 *RSA_D_Data, cc_u32 RSA_D_Len, + cc_u8 *RSA_P_Data, cc_u32 RSA_P_Len, + cc_u8 *RSA_Q_Data, cc_u32 RSA_Q_Len, + cc_u8 *RSA_DmodP1_Data, cc_u32 RSA_DmodP1_Len, + cc_u8 *RSA_DmodQ1_Data, cc_u32 RSA_DmodQ1_Len, + cc_u8 *RSA_iQmodP_Data, cc_u32 RSA_iQmodP_Len) { if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->rsactx == NULL)) - { return CRYPTO_NULL_POINTER; - } crt->ctx->rsactx->crt_operation = 0; - if ((RSA_P_Data != NULL) && (RSA_Q_Data != NULL) && (RSA_DmodP1_Data != NULL) && (RSA_DmodQ1_Data != NULL) && (RSA_iQmodP_Data != NULL)) - { + + if ((RSA_P_Data != NULL) && (RSA_Q_Data != NULL) && (RSA_DmodP1_Data != NULL) && + (RSA_DmodQ1_Data != NULL) && (RSA_iQmodP_Data != NULL)) crt->ctx->rsactx->crt_operation = 1; - } + else if (RSA_N_Data == NULL) - { return CRYPTO_NULL_POINTER; - } - if (RSA_N_Data != NULL) - { - if( !SDRM_OS2BN(RSA_N_Data, RSA_N_Len, crt->ctx->rsactx->n) ) { - SDRM_BN_OPTIMIZE_LENGTH(crt->ctx->rsactx->n); - } + if (RSA_N_Data != NULL) { + if (!SDRM_OS2BN(RSA_N_Data, RSA_N_Len, crt->ctx->rsactx->n)) + SDRM_BN_OPTIMIZE_LENGTH(crt->ctx->rsactx->n); } - if (RSA_E_Data != NULL) - { - if( !SDRM_OS2BN(RSA_E_Data, RSA_E_Len, crt->ctx->rsactx->e) ) { - SDRM_BN_OPTIMIZE_LENGTH(crt->ctx->rsactx->e); - } + if (RSA_E_Data != NULL) { + if (!SDRM_OS2BN(RSA_E_Data, RSA_E_Len, crt->ctx->rsactx->e)) + SDRM_BN_OPTIMIZE_LENGTH(crt->ctx->rsactx->e); } - if (RSA_D_Data != NULL) - { - if( !SDRM_OS2BN(RSA_D_Data, RSA_D_Len, crt->ctx->rsactx->d) ) { - SDRM_BN_OPTIMIZE_LENGTH(crt->ctx->rsactx->d); - } + if (RSA_D_Data != NULL) { + if (!SDRM_OS2BN(RSA_D_Data, RSA_D_Len, crt->ctx->rsactx->d)) + SDRM_BN_OPTIMIZE_LENGTH(crt->ctx->rsactx->d); } - if (RSA_P_Data != NULL) - { - if( !SDRM_OS2BN(RSA_P_Data, RSA_P_Len, crt->ctx->rsactx->p) ) { - SDRM_BN_OPTIMIZE_LENGTH(crt->ctx->rsactx->p); - } + if (RSA_P_Data != NULL) { + if (!SDRM_OS2BN(RSA_P_Data, RSA_P_Len, crt->ctx->rsactx->p)) + SDRM_BN_OPTIMIZE_LENGTH(crt->ctx->rsactx->p); } - if (RSA_Q_Data != NULL) - { - if( !SDRM_OS2BN(RSA_Q_Data, RSA_Q_Len, crt->ctx->rsactx->q) ) { - SDRM_BN_OPTIMIZE_LENGTH(crt->ctx->rsactx->q); - } + if (RSA_Q_Data != NULL) { + if (!SDRM_OS2BN(RSA_Q_Data, RSA_Q_Len, crt->ctx->rsactx->q)) + SDRM_BN_OPTIMIZE_LENGTH(crt->ctx->rsactx->q); } - if (RSA_DmodP1_Data != NULL) - { - if( !SDRM_OS2BN(RSA_DmodP1_Data, RSA_DmodP1_Len, crt->ctx->rsactx->dmodp1) ) { - SDRM_BN_OPTIMIZE_LENGTH(crt->ctx->rsactx->dmodp1); - } + if (RSA_DmodP1_Data != NULL) { + if (!SDRM_OS2BN(RSA_DmodP1_Data, RSA_DmodP1_Len, crt->ctx->rsactx->dmodp1)) + SDRM_BN_OPTIMIZE_LENGTH(crt->ctx->rsactx->dmodp1); } - if (RSA_DmodQ1_Data != NULL) - { - if( !SDRM_OS2BN(RSA_DmodQ1_Data, RSA_DmodQ1_Len, crt->ctx->rsactx->dmodq1) ) { - SDRM_BN_OPTIMIZE_LENGTH(crt->ctx->rsactx->dmodq1); - } + if (RSA_DmodQ1_Data != NULL) { + if (!SDRM_OS2BN(RSA_DmodQ1_Data, RSA_DmodQ1_Len, crt->ctx->rsactx->dmodq1)) + SDRM_BN_OPTIMIZE_LENGTH(crt->ctx->rsactx->dmodq1); } - if (RSA_iQmodP_Data != NULL) - { - if( !SDRM_OS2BN(RSA_iQmodP_Data, RSA_iQmodP_Len, crt->ctx->rsactx->iqmodp) ) { - SDRM_BN_OPTIMIZE_LENGTH(crt->ctx->rsactx->iqmodp); - } + if (RSA_iQmodP_Data != NULL) { + if (!SDRM_OS2BN(RSA_iQmodP_Data, RSA_iQmodP_Len, crt->ctx->rsactx->iqmodp)) + SDRM_BN_OPTIMIZE_LENGTH(crt->ctx->rsactx->iqmodp); } crt->ctx->rsactx->pm = PaddingMethod; @@ -237,104 +222,97 @@ int SDRM_RSA_setNEDPQ(CryptoCoreContainer *crt, cc_u32 PaddingMethod, /* - * @fn int SDRM_RSA_GenerateKey(CryptoCoreContainer *crt, cc_u32 PaddingMethod, - * cc_u8* RSA_N_Data, cc_u32 *RSA_N_Len, - * cc_u8* RSA_E_Data, cc_u32 *RSA_E_Len, - * cc_u8* RSA_D_Data, cc_u32 *RSA_D_Len) - * @brief generate and set RSA parameters + * @fn int SDRM_RSA_GenerateKey(CryptoCoreContainer *crt, cc_u32 PaddingMethod, + * cc_u8* RSA_N_Data, cc_u32 *RSA_N_Len, + * cc_u8* RSA_E_Data, cc_u32 *RSA_E_Len, + * cc_u8* RSA_D_Data, cc_u32 *RSA_D_Len) + * @brief generate and set RSA parameters * - * @param crt [in/out]rsa context - * @param PaddingMethod [in]padding method - * @param RSA_N_Data [out]n value - * @param RSA_N_Len [out]byte-length of n - * @param RSA_E_Data [out]e value - * @param RSA_E_Len [out]byte-length of e - * @param RSA_D_Data [out]d value - * @param RSA_D_Len [out]byte-length of d + * @param crt [in/out]rsa context + * @param PaddingMethod [in]padding method + * @param RSA_N_Data [out]n value + * @param RSA_N_Len [out]byte-length of n + * @param RSA_E_Data [out]e value + * @param RSA_E_Len [out]byte-length of e + * @param RSA_D_Data [out]d value + * @param RSA_D_Len [out]byte-length of d * - * @return CRYPTO_SUCCESS if no error is occured - * \n CRYPTO_NULL_POINTER if an argument is a null pointer - * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed + * @return CRYPTO_SUCCESS if no error is occured + * \n CRYPTO_NULL_POINTER if an argument is a null pointer + * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed */ int SDRM_RSA_GenerateKey(CryptoCoreContainer *crt, cc_u32 PaddingMethod, - cc_u8* RSA_N_Data, cc_u32 *RSA_N_Len, - cc_u8* RSA_E_Data, cc_u32 *RSA_E_Len, - cc_u8* RSA_D_Data, cc_u32 *RSA_D_Len) + cc_u8 *RSA_N_Data, cc_u32 *RSA_N_Len, + cc_u8 *RSA_E_Data, cc_u32 *RSA_E_Len, + cc_u8 *RSA_D_Data, cc_u32 *RSA_D_Len) { - cc_u32 Seed[4]; + cc_u32 Seed[4]; SDRM_BIG_NUM *p, *q, *pi, *e, *temp1, *temp2; - cc_u32 RSA_KeyByteLen = 0; - int i, sp, t1; + cc_u32 RSA_KeyByteLen = 0; + int i, sp, t1; cc_u8 *pbBuf = NULL; if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->rsactx == NULL)) - { return CRYPTO_NULL_POINTER; - } RSA_KeyByteLen = crt->ctx->rsactx->k; t1 = (RSA_KeyByteLen * 4 - 1) % 32; - pbBuf = (cc_u8*)malloc(SDRM_RSA_ALLOC_SIZE * 5); + pbBuf = (cc_u8 *)malloc(SDRM_RSA_ALLOC_SIZE * 5); + if (pbBuf == NULL) - { return CRYPTO_MEMORY_ALLOC_FAIL; - } - p = SDRM_BN_Alloc((cc_u8*)pbBuf, SDRM_RSA_BN_BUFSIZE); - q = SDRM_BN_Alloc((cc_u8*)p + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE); - pi = SDRM_BN_Alloc((cc_u8*)q + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE); - temp1 = SDRM_BN_Alloc((cc_u8*)pi + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE); - temp2 = SDRM_BN_Alloc((cc_u8*)temp1 + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE); + p = SDRM_BN_Alloc((cc_u8 *)pbBuf, + SDRM_RSA_BN_BUFSIZE); + q = SDRM_BN_Alloc((cc_u8 *)p + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE); + pi = SDRM_BN_Alloc((cc_u8 *)q + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE); + temp1 = SDRM_BN_Alloc((cc_u8 *)pi + SDRM_RSA_ALLOC_SIZE, + SDRM_RSA_BN_BUFSIZE); + temp2 = SDRM_BN_Alloc((cc_u8 *)temp1 + SDRM_RSA_ALLOC_SIZE, + SDRM_RSA_BN_BUFSIZE); e = crt->ctx->rsactx->e; for (i = 0; i < 4; i++) - { Seed[i] = (rand() << 16) ^rand(); - } //set security parameter for miller-rabin probabilistic primality test if (RSA_KeyByteLen >= 256) - { sp = 3; - } + else if (RSA_KeyByteLen >= 128) - { sp = 5; - } + else if (RSA_KeyByteLen >= 30) - { sp = 15; - } + else - { sp = 30; - } GEN_RND: //Generate p p->Length = (RSA_KeyByteLen + 7) / 8; + do { - SDRM_RNG_X931((cc_u8 *)Seed, RSA_KeyByteLen * 4, (cc_u8*)p->pData); + SDRM_RNG_X931((cc_u8 *)Seed, RSA_KeyByteLen * 4, (cc_u8 *)p->pData); p->pData[0] |= 1L; p->pData[p->Length - 1] &= ~((-1L) << t1); p->pData[p->Length - 1] |= (1L << t1); - } - while(SDRM_BN_MILLER_RABIN(p, sp) != CRYPTO_ISPRIME); + } while (SDRM_BN_MILLER_RABIN(p, sp) != CRYPTO_ISPRIME); //Generate q q->Length = (RSA_KeyByteLen + 7) / 8; + do { - SDRM_RNG_X931((cc_u8 *)Seed, RSA_KeyByteLen * 4, (cc_u8*)q->pData); + SDRM_RNG_X931((cc_u8 *)Seed, RSA_KeyByteLen * 4, (cc_u8 *)q->pData); q->pData[0] |= 1L; q->pData[q->Length - 1] &= ~((-1L) << t1); q->pData[q->Length - 1] |= (1L << t1); - } - while(SDRM_BN_MILLER_RABIN(q, sp) != CRYPTO_ISPRIME); + } while (SDRM_BN_MILLER_RABIN(q, sp) != CRYPTO_ISPRIME); -// SDRM_PrintBN("p", p); -// SDRM_PrintBN("q", q); + // SDRM_PrintBN("p", p); + // SDRM_PrintBN("q", q); //temp1 = (p - 1), temp2 = (q - 1) SDRM_BN_Sub(temp1, p, BN_One); @@ -347,51 +325,36 @@ GEN_RND: //generate e e->Length = (RSA_KeyByteLen + 3) / 4; + do { do { - SDRM_RNG_X931((cc_u8 *)Seed, RSA_KeyByteLen * 8 - 8, (cc_u8*)e->pData); + SDRM_RNG_X931((cc_u8 *)Seed, RSA_KeyByteLen * 8 - 8, (cc_u8 *)e->pData); e->pData[0] |= 0x01; - } - while(SDRM_BN_CheckRelativelyPrime(e, pi) != CRYPTO_ISPRIME); - } - while (SDRM_BN_Cmp(e, pi) >= 0); + } while (SDRM_BN_CheckRelativelyPrime(e, pi) != CRYPTO_ISPRIME); + } while (SDRM_BN_Cmp(e, pi) >= 0); if (SDRM_BN_ModInv(crt->ctx->rsactx->d, e, pi) != CRYPTO_SUCCESS) - { goto GEN_RND; - } crt->ctx->rsactx->pm = PaddingMethod; if (RSA_N_Data != NULL) - { SDRM_I2OSP(crt->ctx->rsactx->n, RSA_KeyByteLen, RSA_N_Data); - } if (RSA_N_Len != NULL) - { *RSA_N_Len = RSA_KeyByteLen; - } if (RSA_E_Data != NULL) - { SDRM_I2OSP(crt->ctx->rsactx->e, RSA_KeyByteLen, RSA_E_Data); - } if (RSA_E_Len != NULL) - { *RSA_E_Len = RSA_KeyByteLen; - } if (RSA_D_Data != NULL) - { SDRM_I2OSP(crt->ctx->rsactx->d, RSA_KeyByteLen, RSA_D_Data); - } if (RSA_D_Len != NULL) - { *RSA_D_Len = RSA_KeyByteLen; - } free(pbBuf); @@ -401,254 +364,235 @@ GEN_RND: } int SDRM_RSA_GenerateKeyforCRT(CryptoCoreContainer *crt, cc_u32 PaddingMethod, - cc_u8* RSA_E_Data, cc_u32 RSA_E_Len, - cc_u8* RSA_N_Data, cc_u32 *RSA_N_Len, - cc_u8* RSA_D_Data, cc_u32 *RSA_D_Len, - cc_u8* RSA_P_Data, cc_u32 *RSA_P_Len, - cc_u8* RSA_Q_Data, cc_u32 *RSA_Q_Len, - cc_u8* RSA_DP_Data, cc_u32 *RSA_DP_Len, - cc_u8* RSA_DQ_Data, cc_u32 *RSA_DQ_Len, - cc_u8* RSA_QP_Data, cc_u32 *RSA_QP_Len) + cc_u8 *RSA_E_Data, cc_u32 RSA_E_Len, + cc_u8 *RSA_N_Data, cc_u32 *RSA_N_Len, + cc_u8 *RSA_D_Data, cc_u32 *RSA_D_Len, + cc_u8 *RSA_P_Data, cc_u32 *RSA_P_Len, + cc_u8 *RSA_Q_Data, cc_u32 *RSA_Q_Len, + cc_u8 *RSA_DP_Data, cc_u32 *RSA_DP_Len, + cc_u8 *RSA_DQ_Data, cc_u32 *RSA_DQ_Len, + cc_u8 *RSA_QP_Data, cc_u32 *RSA_QP_Len) { - cc_u32 Seed[4]; + cc_u32 Seed[4]; SDRM_BIG_NUM *p, *q, *h, *e, *p1, *q1; - cc_u32 RSA_KeyByteLen = 0; - int i, sp, t1; + cc_u32 RSA_KeyByteLen = 0; + int i, sp, t1; cc_u8 *pbBuf = NULL; - if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->rsactx == NULL)) { + + if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->rsactx == NULL)) return CRYPTO_NULL_POINTER; - } + RSA_KeyByteLen = crt->ctx->rsactx->k; t1 = (RSA_KeyByteLen * 4 - 1) % 32; - pbBuf = (cc_u8*)malloc(SDRM_RSA_ALLOC_SIZE * 5); + pbBuf = (cc_u8 *)malloc(SDRM_RSA_ALLOC_SIZE * 5); + if (pbBuf == NULL) - { return CRYPTO_MEMORY_ALLOC_FAIL; - } - p = SDRM_BN_Alloc((cc_u8*)pbBuf, SDRM_RSA_BN_BUFSIZE); - q = SDRM_BN_Alloc((cc_u8*)p + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE); - h = SDRM_BN_Alloc((cc_u8*)q + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE); - p1 = SDRM_BN_Alloc((cc_u8*)h + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE); - q1 = SDRM_BN_Alloc((cc_u8*)p1 + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE); + + p = SDRM_BN_Alloc((cc_u8 *)pbBuf, + SDRM_RSA_BN_BUFSIZE); + q = SDRM_BN_Alloc((cc_u8 *)p + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE); + h = SDRM_BN_Alloc((cc_u8 *)q + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE); + p1 = SDRM_BN_Alloc((cc_u8 *)h + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE); + q1 = SDRM_BN_Alloc((cc_u8 *)p1 + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE); e = crt->ctx->rsactx->e; + for (i = 0; i < 4; i++) - { Seed[i] = (rand() << 16) ^ rand(); - } + if (RSA_KeyByteLen >= 256) - { sp = 3; - } + else if (RSA_KeyByteLen >= 128) - { sp = 5; - } + else if (RSA_KeyByteLen >= 30) - { sp = 15; - } + else - { sp = 30; - } + GEN_RND: p->Length = (RSA_KeyByteLen + 7) / 8; + do { - SDRM_RNG_X931((cc_u8 *)Seed, RSA_KeyByteLen * 4, (cc_u8*)p->pData); + SDRM_RNG_X931((cc_u8 *)Seed, RSA_KeyByteLen * 4, (cc_u8 *)p->pData); p->pData[0] |= 1L; p->pData[p->Length - 1] &= ~((-1L) << t1); p->pData[p->Length - 1] |= (1L << t1); - } - while(SDRM_BN_MILLER_RABIN(p, sp) != CRYPTO_ISPRIME); + } while (SDRM_BN_MILLER_RABIN(p, sp) != CRYPTO_ISPRIME); + q->Length = (RSA_KeyByteLen + 7) / 8; + do { - SDRM_RNG_X931((cc_u8 *)Seed, RSA_KeyByteLen * 4, (cc_u8*)q->pData); + SDRM_RNG_X931((cc_u8 *)Seed, RSA_KeyByteLen * 4, (cc_u8 *)q->pData); q->pData[0] |= 1L; q->pData[q->Length - 1] &= ~((-1L) << t1); q->pData[q->Length - 1] |= (1L << t1); - } - while(SDRM_BN_MILLER_RABIN(q, sp) != CRYPTO_ISPRIME); + } while (SDRM_BN_MILLER_RABIN(q, sp) != CRYPTO_ISPRIME); + SDRM_BN_Sub(p1, p, BN_One); SDRM_BN_Sub(q1, q, BN_One); SDRM_BN_Mul(h, p1, q1); SDRM_OS2BN(RSA_E_Data, RSA_E_Len, e); - if ((SDRM_BN_CheckRelativelyPrime(e, h) != CRYPTO_ISPRIME) || (SDRM_BN_Cmp(e, h) >= 0)) - { + + if ((SDRM_BN_CheckRelativelyPrime(e, h) != CRYPTO_ISPRIME) || + (SDRM_BN_Cmp(e, h) >= 0)) goto GEN_RND; - } + SDRM_BN_Mul(crt->ctx->rsactx->n, p, q); + if (SDRM_BN_ModInv(crt->ctx->rsactx->d, e, h) != CRYPTO_SUCCESS) - { goto GEN_RND; - } - if (SDRM_BN_ModRed(crt->ctx->rsactx->dmodp1, crt->ctx->rsactx->d, p1) != CRYPTO_SUCCESS) - { + + if (SDRM_BN_ModRed(crt->ctx->rsactx->dmodp1, crt->ctx->rsactx->d, + p1) != CRYPTO_SUCCESS) goto GEN_RND; - } - if (SDRM_BN_ModRed(crt->ctx->rsactx->dmodq1, crt->ctx->rsactx->d, q1) != CRYPTO_SUCCESS) - { + + if (SDRM_BN_ModRed(crt->ctx->rsactx->dmodq1, crt->ctx->rsactx->d, + q1) != CRYPTO_SUCCESS) goto GEN_RND; - } + if (SDRM_BN_ModInv(crt->ctx->rsactx->iqmodp, q, p) != CRYPTO_SUCCESS) - { goto GEN_RND; - } + crt->ctx->rsactx->pm = PaddingMethod; + if (RSA_N_Data != NULL) - { SDRM_I2OSP(crt->ctx->rsactx->n, RSA_KeyByteLen, RSA_N_Data); - } + if (RSA_N_Len != NULL) - { *RSA_N_Len = RSA_KeyByteLen; - } + if (RSA_D_Data != NULL) - { SDRM_I2OSP(crt->ctx->rsactx->d, RSA_KeyByteLen, RSA_D_Data); - } + if (RSA_D_Len != NULL) - { *RSA_D_Len = RSA_KeyByteLen; - } + if (RSA_P_Data != NULL) - { - SDRM_I2OSP(p, RSA_KeyByteLen/2, RSA_P_Data); - } + SDRM_I2OSP(p, RSA_KeyByteLen / 2, RSA_P_Data); + if (RSA_P_Len != NULL) - { - *RSA_P_Len = RSA_KeyByteLen/2; - } + *RSA_P_Len = RSA_KeyByteLen / 2; + if (RSA_Q_Data != NULL) - { - SDRM_I2OSP(q, RSA_KeyByteLen/2, RSA_Q_Data); - } + SDRM_I2OSP(q, RSA_KeyByteLen / 2, RSA_Q_Data); + if (RSA_Q_Len != NULL) - { - *RSA_Q_Len = RSA_KeyByteLen/2; - } + *RSA_Q_Len = RSA_KeyByteLen / 2; + if (RSA_DP_Data != NULL) - { - SDRM_I2OSP(crt->ctx->rsactx->dmodp1, RSA_KeyByteLen/2, RSA_DP_Data); - } + SDRM_I2OSP(crt->ctx->rsactx->dmodp1, RSA_KeyByteLen / 2, RSA_DP_Data); + if (RSA_DP_Len != NULL) - { - *RSA_DP_Len = RSA_KeyByteLen/2; - } + *RSA_DP_Len = RSA_KeyByteLen / 2; + if (RSA_DQ_Data != NULL) - { - SDRM_I2OSP(crt->ctx->rsactx->dmodq1, RSA_KeyByteLen/2, RSA_DQ_Data); - } + SDRM_I2OSP(crt->ctx->rsactx->dmodq1, RSA_KeyByteLen / 2, RSA_DQ_Data); + if (RSA_DQ_Len != NULL) - { - *RSA_DQ_Len = RSA_KeyByteLen/2; - } + *RSA_DQ_Len = RSA_KeyByteLen / 2; + if (RSA_QP_Data != NULL) - { - SDRM_I2OSP(crt->ctx->rsactx->iqmodp, RSA_KeyByteLen/2, RSA_QP_Data); - } + SDRM_I2OSP(crt->ctx->rsactx->iqmodp, RSA_KeyByteLen / 2, RSA_QP_Data); + if (RSA_QP_Len != NULL) - { - *RSA_QP_Len = RSA_KeyByteLen/2; - } + *RSA_QP_Len = RSA_KeyByteLen / 2; + free(pbBuf); crt->ctx->rsactx->crt_operation = 0; return CRYPTO_SUCCESS; } /* - * @fn int SDRM_RSA_GenerateND(CryptoCoreContainer *crt, cc_u32 PaddingMethod, - * cc_u8* RSA_E_Data, cc_u32 RSA_E_Len, - * cc_u8* RSA_N_Data, cc_u32 *RSA_N_Len, - * cc_u8* RSA_D_Data, cc_u32 *RSA_D_Len) - * @brief generate and set RSA parameters with specfied e + * @fn int SDRM_RSA_GenerateND(CryptoCoreContainer *crt, cc_u32 PaddingMethod, + * cc_u8* RSA_E_Data, cc_u32 RSA_E_Len, + * cc_u8* RSA_N_Data, cc_u32 *RSA_N_Len, + * cc_u8* RSA_D_Data, cc_u32 *RSA_D_Len) + * @brief generate and set RSA parameters with specfied e * - * @param crt [in/out]rsa context - * @param PaddingMethod [in]padding method - * @param RSA_E_Data [in]e value - * @param RSA_E_Len [in]byte-length of e - * @param RSA_N_Data [out]n value - * @param RSA_N_Len [out]byte-length of n - * @param RSA_D_Data [out]d value - * @param RSA_D_Len [out]byte-length of d + * @param crt [in/out]rsa context + * @param PaddingMethod [in]padding method + * @param RSA_E_Data [in]e value + * @param RSA_E_Len [in]byte-length of e + * @param RSA_N_Data [out]n value + * @param RSA_N_Len [out]byte-length of n + * @param RSA_D_Data [out]d value + * @param RSA_D_Len [out]byte-length of d * - * @return CRYPTO_SUCCESS if no error is occured - * \n CRYPTO_NULL_POINTER if an argument is a null pointer - * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed + * @return CRYPTO_SUCCESS if no error is occured + * \n CRYPTO_NULL_POINTER if an argument is a null pointer + * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed */ int SDRM_RSA_GenerateND(CryptoCoreContainer *crt, cc_u32 PaddingMethod, - cc_u8* RSA_E_Data, cc_u32 RSA_E_Len, - cc_u8* RSA_N_Data, cc_u32 *RSA_N_Len, - cc_u8* RSA_D_Data, cc_u32 *RSA_D_Len) + cc_u8 *RSA_E_Data, cc_u32 RSA_E_Len, + cc_u8 *RSA_N_Data, cc_u32 *RSA_N_Len, + cc_u8 *RSA_D_Data, cc_u32 *RSA_D_Len) { - cc_u32 Seed[4]; + cc_u32 Seed[4]; SDRM_BIG_NUM *p, *q, *pi, *e, *temp1, *temp2; - cc_u32 RSA_KeyByteLen = 0; - int i, sp, t1; + cc_u32 RSA_KeyByteLen = 0; + int i, sp, t1; cc_u8 *pbBuf = NULL; - if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->rsactx == NULL)) { + if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->rsactx == NULL)) return CRYPTO_NULL_POINTER; - } RSA_KeyByteLen = crt->ctx->rsactx->k; t1 = (RSA_KeyByteLen * 4 - 1) % 32; - pbBuf = (cc_u8*)malloc(SDRM_RSA_ALLOC_SIZE * 5); + pbBuf = (cc_u8 *)malloc(SDRM_RSA_ALLOC_SIZE * 5); + if (pbBuf == NULL) - { return CRYPTO_MEMORY_ALLOC_FAIL; - } - p = SDRM_BN_Alloc((cc_u8*)pbBuf, SDRM_RSA_BN_BUFSIZE); - q = SDRM_BN_Alloc((cc_u8*)p + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE); - pi = SDRM_BN_Alloc((cc_u8*)q + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE); - temp1 = SDRM_BN_Alloc((cc_u8*)pi + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE); - temp2 = SDRM_BN_Alloc((cc_u8*)temp1 + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE); + p = SDRM_BN_Alloc((cc_u8 *)pbBuf, + SDRM_RSA_BN_BUFSIZE); + q = SDRM_BN_Alloc((cc_u8 *)p + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE); + pi = SDRM_BN_Alloc((cc_u8 *)q + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE); + temp1 = SDRM_BN_Alloc((cc_u8 *)pi + SDRM_RSA_ALLOC_SIZE, + SDRM_RSA_BN_BUFSIZE); + temp2 = SDRM_BN_Alloc((cc_u8 *)temp1 + SDRM_RSA_ALLOC_SIZE, + SDRM_RSA_BN_BUFSIZE); e = crt->ctx->rsactx->e; for (i = 0; i < 4; i++) - { Seed[i] = (rand() << 16) ^ rand(); - } //set security parameter for miller-rabin probabilistic primality test if (RSA_KeyByteLen >= 256) - { sp = 3; - } + else if (RSA_KeyByteLen >= 128) - { sp = 5; - } + else if (RSA_KeyByteLen >= 30) - { sp = 15; - } + else - { sp = 30; - } GEN_RND: //Generate p p->Length = (RSA_KeyByteLen + 7) / 8; + do { - SDRM_RNG_X931((cc_u8 *)Seed, RSA_KeyByteLen * 4, (cc_u8*)p->pData); + SDRM_RNG_X931((cc_u8 *)Seed, RSA_KeyByteLen * 4, (cc_u8 *)p->pData); p->pData[0] |= 1L; p->pData[p->Length - 1] &= ~((-1L) << t1); p->pData[p->Length - 1] |= (1L << t1); - } - while(SDRM_BN_MILLER_RABIN(p, sp) != CRYPTO_ISPRIME); + } while (SDRM_BN_MILLER_RABIN(p, sp) != CRYPTO_ISPRIME); //Generate q q->Length = (RSA_KeyByteLen + 7) / 8; + do { - SDRM_RNG_X931((cc_u8 *)Seed, RSA_KeyByteLen * 4, (cc_u8*)q->pData); + SDRM_RNG_X931((cc_u8 *)Seed, RSA_KeyByteLen * 4, (cc_u8 *)q->pData); q->pData[0] |= 1L; q->pData[q->Length - 1] &= ~((-1L) << t1); q->pData[q->Length - 1] |= (1L << t1); - } - while(SDRM_BN_MILLER_RABIN(q, sp) != CRYPTO_ISPRIME); + } while (SDRM_BN_MILLER_RABIN(q, sp) != CRYPTO_ISPRIME); //temp1 = (p - 1), temp2 = (q - 1) SDRM_BN_Sub(temp1, p, BN_One); @@ -661,37 +605,27 @@ GEN_RND: //check N for e SDRM_OS2BN(RSA_E_Data, RSA_E_Len, e); - if ((SDRM_BN_CheckRelativelyPrime(e, pi) != CRYPTO_ISPRIME) || (SDRM_BN_Cmp(e, pi) >= 0)) - { + + if ((SDRM_BN_CheckRelativelyPrime(e, pi) != CRYPTO_ISPRIME) || + (SDRM_BN_Cmp(e, pi) >= 0)) goto GEN_RND; - } if (SDRM_BN_ModInv(crt->ctx->rsactx->d, e, pi) != CRYPTO_SUCCESS) - { goto GEN_RND; - } crt->ctx->rsactx->pm = PaddingMethod; if (RSA_N_Data != NULL) - { SDRM_I2OSP(crt->ctx->rsactx->n, RSA_KeyByteLen, RSA_N_Data); - } if (RSA_N_Len != NULL) - { *RSA_N_Len = RSA_KeyByteLen; - } if (RSA_D_Data != NULL) - { SDRM_I2OSP(crt->ctx->rsactx->d, RSA_KeyByteLen, RSA_D_Data); - } if (RSA_D_Len != NULL) - { *RSA_D_Len = RSA_KeyByteLen; - } free(pbBuf); @@ -701,94 +635,89 @@ GEN_RND: } /* - * @fn int SDRM_RSA_GenerateDwithPQE(CryptoCoreContainer *crt, cc_u32 PaddingMethod, - * cc_u8* RSA_E_Data, cc_u32 RSA_E_Len, - * cc_u8* RSA_P_Data, cc_u32 RSA_P_Len, - * cc_u8* RSA_Q_Data, cc_u32 RSA_Q_Len, - * cc_u8* RSA_N_Data, cc_u32 *RSA_N_Len, - * cc_u8* RSA_D_Data, cc_u32 *RSA_D_Len) - * @brief generate D with specfied p, q, d mod (p-1), d mod (q-1) and e + * @fn int SDRM_RSA_GenerateDwithPQE(CryptoCoreContainer *crt, cc_u32 PaddingMethod, + * cc_u8* RSA_E_Data, cc_u32 RSA_E_Len, + * cc_u8* RSA_P_Data, cc_u32 RSA_P_Len, + * cc_u8* RSA_Q_Data, cc_u32 RSA_Q_Len, + * cc_u8* RSA_N_Data, cc_u32 *RSA_N_Len, + * cc_u8* RSA_D_Data, cc_u32 *RSA_D_Len) + * @brief generate D with specfied p, q, d mod (p-1), d mod (q-1) and e * - * @param crt [in/out]rsa context - * @param PaddingMethod [in]padding method - * @param RSA_E_Data [in]e value - * @param RSA_E_Len [in]byte-length of e - * @param RSA_P_Data [in]n value - * @param RSA_P_Len [in]byte-length of n - * @param RSA_Q_Data [in]d value - * @param RSA_Q_Len [in]byte-length of d - * @param RSA_D_P_Data [in]d mod (p-1) value - * @param RSA_D_P_Len [in]byte-length of d mod (p-1) - * @param RSA_D_Q_Data [in]d mod (q-1) value - * @param RSA_D_Q_Len [in]byte-length of d mod (q-1) - * @param RSA_D_Data [out]d value - * @param RSA_D_Len [out]byte-length of d + * @param crt [in/out]rsa context + * @param PaddingMethod [in]padding method + * @param RSA_E_Data [in]e value + * @param RSA_E_Len [in]byte-length of e + * @param RSA_P_Data [in]n value + * @param RSA_P_Len [in]byte-length of n + * @param RSA_Q_Data [in]d value + * @param RSA_Q_Len [in]byte-length of d + * @param RSA_D_P_Data [in]d mod (p-1) value + * @param RSA_D_P_Len [in]byte-length of d mod (p-1) + * @param RSA_D_Q_Data [in]d mod (q-1) value + * @param RSA_D_Q_Len [in]byte-length of d mod (q-1) + * @param RSA_D_Data [out]d value + * @param RSA_D_Len [out]byte-length of d * - * @return CRYPTO_SUCCESS if no error is occured - * \n CRYPTO_NULL_POINTER if an argument is a null pointer - * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed + * @return CRYPTO_SUCCESS if no error is occured + * \n CRYPTO_NULL_POINTER if an argument is a null pointer + * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed */ int SDRM_RSA_GenerateDwithPQE(CryptoCoreContainer *crt, cc_u32 PaddingMethod, - cc_u8* RSA_E_Data, cc_u32 RSA_E_Len, - cc_u8* RSA_P_Data, cc_u32 RSA_P_Len, - cc_u8* RSA_Q_Data, cc_u32 RSA_Q_Len, - cc_u8* RSA_N_Data, cc_u32 *RSA_N_Len, - cc_u8* RSA_D_Data, cc_u32 *RSA_D_Len) + cc_u8 *RSA_E_Data, cc_u32 RSA_E_Len, + cc_u8 *RSA_P_Data, cc_u32 RSA_P_Len, + cc_u8 *RSA_Q_Data, cc_u32 RSA_Q_Len, + cc_u8 *RSA_N_Data, cc_u32 *RSA_N_Len, + cc_u8 *RSA_D_Data, cc_u32 *RSA_D_Len) { SDRM_BIG_NUM *p, *q, *pi, *e, *temp1, *temp2; - cc_u32 RSA_KeyByteLen = 0; - int sp; + cc_u32 RSA_KeyByteLen = 0; + int sp; cc_u8 *pbBuf = NULL; if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->rsactx == NULL)) - { return CRYPTO_NULL_POINTER; - } RSA_KeyByteLen = crt->ctx->rsactx->k; - pbBuf = (cc_u8*)malloc(SDRM_RSA_ALLOC_SIZE * 5); + pbBuf = (cc_u8 *)malloc(SDRM_RSA_ALLOC_SIZE * 5); + if (pbBuf == NULL) - { return CRYPTO_MEMORY_ALLOC_FAIL; - } - p = SDRM_BN_Alloc((cc_u8*)pbBuf, SDRM_RSA_BN_BUFSIZE); - q = SDRM_BN_Alloc((cc_u8*)p + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE); - pi = SDRM_BN_Alloc((cc_u8*)q + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE); - temp1 = SDRM_BN_Alloc((cc_u8*)pi + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE); - temp2 = SDRM_BN_Alloc((cc_u8*)temp1 + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE); + p = SDRM_BN_Alloc((cc_u8 *)pbBuf, + SDRM_RSA_BN_BUFSIZE); + q = SDRM_BN_Alloc((cc_u8 *)p + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE); + pi = SDRM_BN_Alloc((cc_u8 *)q + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE); + temp1 = SDRM_BN_Alloc((cc_u8 *)pi + SDRM_RSA_ALLOC_SIZE, + SDRM_RSA_BN_BUFSIZE); + temp2 = SDRM_BN_Alloc((cc_u8 *)temp1 + SDRM_RSA_ALLOC_SIZE, + SDRM_RSA_BN_BUFSIZE); e = crt->ctx->rsactx->e; //set security parameter for miller-rabin probabilistic primality test if (RSA_KeyByteLen >= 256) - { sp = 3; - } + else if (RSA_KeyByteLen >= 128) - { sp = 5; - } + else if (RSA_KeyByteLen >= 30) - { sp = 15; - } + else - { sp = 30; - } - SDRM_OS2BN((cc_u8*)RSA_P_Data, RSA_P_Len, p); - if (SDRM_BN_MILLER_RABIN(p, sp) != CRYPTO_ISPRIME) - { + SDRM_OS2BN((cc_u8 *)RSA_P_Data, RSA_P_Len, p); + + if (SDRM_BN_MILLER_RABIN(p, sp) != CRYPTO_ISPRIME) { free(pbBuf); return CRYPTO_INVALID_ARGUMENT; } - SDRM_OS2BN((cc_u8*)RSA_Q_Data, RSA_Q_Len, q); - if (SDRM_BN_MILLER_RABIN(q, sp) != CRYPTO_ISPRIME) - { + SDRM_OS2BN((cc_u8 *)RSA_Q_Data, RSA_Q_Len, q); + + if (SDRM_BN_MILLER_RABIN(q, sp) != CRYPTO_ISPRIME) { free(pbBuf); return CRYPTO_INVALID_ARGUMENT; } @@ -804,14 +733,14 @@ int SDRM_RSA_GenerateDwithPQE(CryptoCoreContainer *crt, cc_u32 PaddingMethod, //check N for e SDRM_OS2BN(RSA_E_Data, RSA_E_Len, e); - if ((SDRM_BN_CheckRelativelyPrime(e, pi) != CRYPTO_ISPRIME) || (SDRM_BN_Cmp(e, pi) >= 0)) - { + + if ((SDRM_BN_CheckRelativelyPrime(e, pi) != CRYPTO_ISPRIME) || + (SDRM_BN_Cmp(e, pi) >= 0)) { free(pbBuf); return CRYPTO_INVALID_ARGUMENT; } - if (SDRM_BN_ModInv(crt->ctx->rsactx->d, e, pi) != CRYPTO_SUCCESS) - { + if (SDRM_BN_ModInv(crt->ctx->rsactx->d, e, pi) != CRYPTO_SUCCESS) { free(pbBuf); return CRYPTO_INVALID_ARGUMENT; } @@ -819,24 +748,16 @@ int SDRM_RSA_GenerateDwithPQE(CryptoCoreContainer *crt, cc_u32 PaddingMethod, crt->ctx->rsactx->pm = PaddingMethod; if (RSA_N_Data != NULL) - { SDRM_I2OSP(crt->ctx->rsactx->n, RSA_KeyByteLen, RSA_N_Data); - } if (RSA_N_Len != NULL) - { *RSA_N_Len = RSA_KeyByteLen; - } if (RSA_D_Data != NULL) - { SDRM_I2OSP(crt->ctx->rsactx->d, RSA_KeyByteLen, RSA_D_Data); - } if (RSA_D_Len != NULL) - { *RSA_D_Len = RSA_KeyByteLen; - } free(pbBuf); @@ -846,101 +767,94 @@ int SDRM_RSA_GenerateDwithPQE(CryptoCoreContainer *crt, cc_u32 PaddingMethod, } /* - * @fn int SDRM_RSA_GenerateKeyForCRT(CryptoCoreContainer *crt, cc_u32 PaddingMethod, - * cc_u8* RSA_N_Data, cc_u32 *RSA_N_Len, - * cc_u8* RSA_E_Data, cc_u32 *RSA_E_Len, - * cc_u8* RSA_D_Data, cc_u32 *RSA_D_Len, - * cc_u8* RSA_P_Data, cc_u32 *RSA_P_Len, - * cc_u8* RSA_Q_Data, cc_u32 *RSA_Q_Len, - * cc_u8* RSA_DmodP1_Data, cc_u32 *RSA_DmodP1_Len, - * cc_u8* RSA_DmodQ1_Data, cc_u32 *RSA_DmodQ1_Len, - * cc_u8* RSA_iQmodP_Data, cc_u32 *RSA_iQmodP_Len) - * @brief generate and set RSA parameters for CRT + * @fn int SDRM_RSA_GenerateKeyForCRT(CryptoCoreContainer *crt, cc_u32 PaddingMethod, + * cc_u8* RSA_N_Data, cc_u32 *RSA_N_Len, + * cc_u8* RSA_E_Data, cc_u32 *RSA_E_Len, + * cc_u8* RSA_D_Data, cc_u32 *RSA_D_Len, + * cc_u8* RSA_P_Data, cc_u32 *RSA_P_Len, + * cc_u8* RSA_Q_Data, cc_u32 *RSA_Q_Len, + * cc_u8* RSA_DmodP1_Data, cc_u32 *RSA_DmodP1_Len, + * cc_u8* RSA_DmodQ1_Data, cc_u32 *RSA_DmodQ1_Len, + * cc_u8* RSA_iQmodP_Data, cc_u32 *RSA_iQmodP_Len) + * @brief generate and set RSA parameters for CRT * - * @param crt [in/out]rsa context - * @param PaddingMethod [in]padding method - * @param RSA_N_Data [out]n value - * @param RSA_N_Len [out]byte-length of n - * @param RSA_E_Data [out]e value - * @param RSA_E_Len [out]byte-length of e - * @param RSA_D_Data [out]d value - * @param RSA_D_Len [out]byte-length of d - * @param RSA_P_Len [out]byte-length of p - * @param RSA_Q_Data [out]q value - * @param RSA_Q_Len [out]byte-length of q - * @param RSA_DmodP1_Data [out]d mod (p-1) value - * @param RSA_DmodP1_Len [out]byte-length of d mod (p-1) - * @param RSA_DmodQ1_Data [out]d mod (q-1) value - * @param RSA_DmodQ1_Len [out]byte-length of d mod (q-1) - * @param RSA_iQmodP_Data [out]q^(-1) mod p value - * @param RSA_iQmodP_Len [out]byte-length of q^(-1) mod p + * @param crt [in/out]rsa context + * @param PaddingMethod [in]padding method + * @param RSA_N_Data [out]n value + * @param RSA_N_Len [out]byte-length of n + * @param RSA_E_Data [out]e value + * @param RSA_E_Len [out]byte-length of e + * @param RSA_D_Data [out]d value + * @param RSA_D_Len [out]byte-length of d + * @param RSA_P_Len [out]byte-length of p + * @param RSA_Q_Data [out]q value + * @param RSA_Q_Len [out]byte-length of q + * @param RSA_DmodP1_Data [out]d mod (p-1) value + * @param RSA_DmodP1_Len [out]byte-length of d mod (p-1) + * @param RSA_DmodQ1_Data [out]d mod (q-1) value + * @param RSA_DmodQ1_Len [out]byte-length of d mod (q-1) + * @param RSA_iQmodP_Data [out]q^(-1) mod p value + * @param RSA_iQmodP_Len [out]byte-length of q^(-1) mod p * - * @return CRYPTO_SUCCESS if no error is occured - * \n CRYPTO_NULL_POINTER if an argument is a null pointer - * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed + * @return CRYPTO_SUCCESS if no error is occured + * \n CRYPTO_NULL_POINTER if an argument is a null pointer + * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed */ int SDRM_RSA_GenNEDPQ(CryptoCoreContainer *crt, cc_u32 PaddingMethod, - cc_u8* RSA_N_Data, cc_u32 *RSA_N_Len, - cc_u8* RSA_E_Data, cc_u32 *RSA_E_Len, - cc_u8* RSA_D_Data, cc_u32 *RSA_D_Len, - cc_u8* RSA_P_Data, cc_u32 *RSA_P_Len, - cc_u8* RSA_Q_Data, cc_u32 *RSA_Q_Len, - cc_u8* RSA_DmodP1_Data, cc_u32 *RSA_DmodP1_Len, - cc_u8* RSA_DmodQ1_Data, cc_u32 *RSA_DmodQ1_Len, - cc_u8* RSA_iQmodP_Data, cc_u32 *RSA_iQmodP_Len) + cc_u8 *RSA_N_Data, cc_u32 *RSA_N_Len, + cc_u8 *RSA_E_Data, cc_u32 *RSA_E_Len, + cc_u8 *RSA_D_Data, cc_u32 *RSA_D_Len, + cc_u8 *RSA_P_Data, cc_u32 *RSA_P_Len, + cc_u8 *RSA_Q_Data, cc_u32 *RSA_Q_Len, + cc_u8 *RSA_DmodP1_Data, cc_u32 *RSA_DmodP1_Len, + cc_u8 *RSA_DmodQ1_Data, cc_u32 *RSA_DmodQ1_Len, + cc_u8 *RSA_iQmodP_Data, cc_u32 *RSA_iQmodP_Len) { - cc_u32 Seed[4]; + cc_u32 Seed[4]; SDRM_BIG_NUM *p, *q, *pi, *e, *temp1, *temp2; - cc_u32 RSA_KeyByteLen = 0; - int i, sp, t1; + cc_u32 RSA_KeyByteLen = 0; + int i, sp, t1; cc_u8 *pbBuf = NULL; if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->rsactx == NULL)) - { return CRYPTO_NULL_POINTER; - } RSA_KeyByteLen = crt->ctx->rsactx->k; t1 = (RSA_KeyByteLen * 4 - 1) % 32; - pbBuf = (cc_u8*)malloc(SDRM_RSA_ALLOC_SIZE * 3); + pbBuf = (cc_u8 *)malloc(SDRM_RSA_ALLOC_SIZE * 3); + if (pbBuf == NULL) - { return CRYPTO_MEMORY_ALLOC_FAIL; - } - pi = SDRM_BN_Alloc((cc_u8*)pbBuf, SDRM_RSA_BN_BUFSIZE); - temp1 = SDRM_BN_Alloc((cc_u8*)pi + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE); - temp2 = SDRM_BN_Alloc((cc_u8*)temp1 + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE); + pi = SDRM_BN_Alloc((cc_u8 *)pbBuf, + SDRM_RSA_BN_BUFSIZE); + temp1 = SDRM_BN_Alloc((cc_u8 *)pi + SDRM_RSA_ALLOC_SIZE, + SDRM_RSA_BN_BUFSIZE); + temp2 = SDRM_BN_Alloc((cc_u8 *)temp1 + SDRM_RSA_ALLOC_SIZE, + SDRM_RSA_BN_BUFSIZE); e = crt->ctx->rsactx->e; p = crt->ctx->rsactx->p; q = crt->ctx->rsactx->q; for (i = 0; i < 4; i++) - { Seed[i] = (rand() << 16) ^ rand(); - } //set security parameter for miller-rabin probabilistic primality test if (RSA_KeyByteLen >= 256) - { sp = 3; - } + else if (RSA_KeyByteLen >= 128) - { sp = 5; - } + else if (RSA_KeyByteLen >= 30) - { sp = 15; - } + else - { sp = 30; - } GEN_RND: @@ -948,26 +862,24 @@ GEN_RND: p->Length = (RSA_KeyByteLen + 7) / 8; do { - SDRM_RNG_X931((cc_u8 *)Seed, RSA_KeyByteLen * 4, (cc_u8*)p->pData); + SDRM_RNG_X931((cc_u8 *)Seed, RSA_KeyByteLen * 4, (cc_u8 *)p->pData); p->pData[0] |= 1L; p->pData[p->Length - 1] &= ~((-1L) << t1); p->pData[p->Length - 1] |= (1L << t1); - } - while(SDRM_BN_MILLER_RABIN(p, sp) != CRYPTO_ISPRIME); + } while (SDRM_BN_MILLER_RABIN(p, sp) != CRYPTO_ISPRIME); //Generate q q->Length = (RSA_KeyByteLen + 7) / 8; do { - SDRM_RNG_X931((cc_u8 *)Seed, RSA_KeyByteLen * 4, (cc_u8*)q->pData); + SDRM_RNG_X931((cc_u8 *)Seed, RSA_KeyByteLen * 4, (cc_u8 *)q->pData); q->pData[0] |= 1L; q->pData[q->Length - 1] &= ~((-1L) << t1); q->pData[q->Length - 1] |= (1L << t1); - } - while(SDRM_BN_MILLER_RABIN(q, sp) != CRYPTO_ISPRIME); + } while (SDRM_BN_MILLER_RABIN(q, sp) != CRYPTO_ISPRIME); -// SDRM_PrintBN("p", p); -// SDRM_PrintBN("q", q); + // SDRM_PrintBN("p", p); + // SDRM_PrintBN("q", q); //temp1 = (p - 1), temp2 = (q - 1) @@ -981,19 +893,16 @@ GEN_RND: //generate e e->Length = (RSA_KeyByteLen + 3) / 4; + do { do { - SDRM_RNG_X931((cc_u8 *)Seed, RSA_KeyByteLen * 8 - 8, (cc_u8*)e->pData); + SDRM_RNG_X931((cc_u8 *)Seed, RSA_KeyByteLen * 8 - 8, (cc_u8 *)e->pData); e->pData[0] |= 0x01; - } - while(SDRM_BN_CheckRelativelyPrime(e, pi) != CRYPTO_ISPRIME); - } - while (SDRM_BN_Cmp(e, pi) >= 0); + } while (SDRM_BN_CheckRelativelyPrime(e, pi) != CRYPTO_ISPRIME); + } while (SDRM_BN_Cmp(e, pi) >= 0); if (SDRM_BN_ModInv(crt->ctx->rsactx->d, e, pi) != CRYPTO_SUCCESS) - { goto GEN_RND; - } //calc dmodp1 = d mod (p - 1) SDRM_BN_ModRed(crt->ctx->rsactx->dmodp1, crt->ctx->rsactx->d, temp1); @@ -1007,84 +916,52 @@ GEN_RND: crt->ctx->rsactx->pm = PaddingMethod; if (RSA_N_Data != NULL) - { SDRM_I2OSP(crt->ctx->rsactx->n, RSA_KeyByteLen, RSA_N_Data); - } if (RSA_N_Len != NULL) - { *RSA_N_Len = RSA_KeyByteLen; - } if (RSA_E_Data != NULL) - { SDRM_I2OSP(crt->ctx->rsactx->e, RSA_KeyByteLen, RSA_E_Data); - } if (RSA_E_Len != NULL) - { *RSA_E_Len = RSA_KeyByteLen; - } if (RSA_D_Data != NULL) - { SDRM_I2OSP(crt->ctx->rsactx->d, RSA_KeyByteLen, RSA_D_Data); - } if (RSA_D_Len != NULL) - { *RSA_D_Len = RSA_KeyByteLen; - } if (RSA_P_Data != NULL) - { SDRM_I2OSP(crt->ctx->rsactx->p, RSA_KeyByteLen / 2, RSA_P_Data); - } if (RSA_P_Len != NULL) - { *RSA_P_Len = RSA_KeyByteLen / 2; - } if (RSA_Q_Data != NULL) - { SDRM_I2OSP(crt->ctx->rsactx->q, RSA_KeyByteLen / 2, RSA_Q_Data); - } if (RSA_Q_Len != NULL) - { *RSA_Q_Len = RSA_KeyByteLen / 2; - } if (RSA_DmodP1_Data != NULL) - { SDRM_I2OSP(crt->ctx->rsactx->dmodp1, RSA_KeyByteLen / 2, RSA_DmodP1_Data); - } if (RSA_DmodP1_Len != NULL) - { *RSA_DmodP1_Len = RSA_KeyByteLen / 2; - } if (RSA_DmodQ1_Data != NULL) - { SDRM_I2OSP(crt->ctx->rsactx->dmodq1, RSA_KeyByteLen / 2, RSA_DmodQ1_Data); - } if (RSA_DmodQ1_Len != NULL) - { *RSA_DmodQ1_Len = RSA_KeyByteLen / 2; - } if (RSA_iQmodP_Data != NULL) - { SDRM_I2OSP(crt->ctx->rsactx->iqmodp, RSA_KeyByteLen / 2, RSA_iQmodP_Data); - } if (RSA_iQmodP_Len != NULL) - { *RSA_iQmodP_Len = RSA_KeyByteLen / 2; - } free(pbBuf); @@ -1094,74 +971,75 @@ GEN_RND: } /* - * @fn int SDRM_RSA_encrypt(CryptoCoreContainer *crt, cc_u8 *in, cc_u32 inLen, cc_u8 *out, cc_u32 *outLen) - * @brief RSA Encryption + * @fn int SDRM_RSA_encrypt(CryptoCoreContainer *crt, cc_u8 *in, cc_u32 inLen, cc_u8 *out, cc_u32 *outLen) + * @brief RSA Encryption * - * @param crt [in]rsa context - * @param in [in]message to encrypt - * @param inLen [in]byte-length of in - * @param out [out]encrypted message - * @param outLen [out]byte-length of out + * @param crt [in]rsa context + * @param in [in]message to encrypt + * @param inLen [in]byte-length of in + * @param out [out]encrypted message + * @param outLen [out]byte-length of out * - * @return CRYPTO_SUCCESS if no error is occured - * \n CRYPTO_NULL_POINTER if an argument is a null pointer - * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed + * @return CRYPTO_SUCCESS if no error is occured + * \n CRYPTO_NULL_POINTER if an argument is a null pointer + * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed */ -int SDRM_RSA_encrypt(CryptoCoreContainer *crt, cc_u8 *in, cc_u32 inLen, cc_u8 *out, cc_u32 *outLen) +int SDRM_RSA_encrypt(CryptoCoreContainer *crt, cc_u8 *in, cc_u32 inLen, + cc_u8 *out, cc_u32 *outLen) { SDRM_BIG_NUM *BN_pMsg, *BN_Cipher; - int retVal = CRYPTO_ERROR; - cc_u32 RSA_KeyByteLen = 0; - cc_u8 *pbBuf = NULL; + int retVal = CRYPTO_ERROR; + cc_u32 RSA_KeyByteLen = 0; + cc_u8 *pbBuf = NULL; - if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->rsactx == NULL) || (in == NULL) || (out == NULL)) - { + if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->rsactx == NULL) || + (in == NULL) || (out == NULL)) return CRYPTO_NULL_POINTER; - } RSA_KeyByteLen = crt->ctx->rsactx->k; + if (inLen > RSA_KeyByteLen) - { return CRYPTO_MSG_TOO_LONG; - } - pbBuf = (cc_u8*)malloc(SDRM_RSA_ALLOC_SIZE * 2 + RSA_KeyByteLen); + pbBuf = (cc_u8 *)malloc(SDRM_RSA_ALLOC_SIZE * 2 + RSA_KeyByteLen); + if (pbBuf == NULL) - { return CRYPTO_MEMORY_ALLOC_FAIL; - } - BN_pMsg = SDRM_BN_Alloc((cc_u8*)pbBuf + RSA_KeyByteLen, SDRM_RSA_BN_BUFSIZE); - BN_Cipher = SDRM_BN_Alloc((cc_u8*)BN_pMsg + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE); + BN_pMsg = SDRM_BN_Alloc((cc_u8 *)pbBuf + RSA_KeyByteLen, SDRM_RSA_BN_BUFSIZE); + BN_Cipher = SDRM_BN_Alloc((cc_u8 *)BN_pMsg + SDRM_RSA_ALLOC_SIZE, + SDRM_RSA_BN_BUFSIZE); //Padding the message - switch(SDRM_LOW_HALF(crt->ctx->rsactx->pm)) - { - case ID_RSAES_PKCS15 : - retVal = SDRM_Enpad_Rsaes_pkcs15(pbBuf, in, inLen, RSA_KeyByteLen); - break; - case ID_RSAES_OAEP : - retVal = SDRM_Enpad_Rsaes_oaep(pbBuf, in, inLen, RSA_KeyByteLen, SDRM_HIGH_HALF(crt->ctx->rsactx->pm)); - break; - case ID_NO_PADDING : - if( inLen != RSA_KeyByteLen) // add by guoxing.xu 20140919 - { - free(pbBuf); - return CRYPTO_INVALID_ARGUMENT; - } - memset(pbBuf, 0x00, RSA_KeyByteLen - inLen); - memcpy(pbBuf + RSA_KeyByteLen - inLen, in, inLen); - retVal= CRYPTO_SUCCESS;// add by guoxing.xu 20140919 - break; - default : + switch (SDRM_LOW_HALF(crt->ctx->rsactx->pm)) { + case ID_RSAES_PKCS15: + retVal = SDRM_Enpad_Rsaes_pkcs15(pbBuf, in, inLen, RSA_KeyByteLen); + break; + + case ID_RSAES_OAEP: + retVal = SDRM_Enpad_Rsaes_oaep(pbBuf, in, inLen, RSA_KeyByteLen, + SDRM_HIGH_HALF(crt->ctx->rsactx->pm)); + break; + + case ID_NO_PADDING: + if (inLen != RSA_KeyByteLen) { // add by guoxing.xu 20140919 free(pbBuf); return CRYPTO_INVALID_ARGUMENT; + } + + memset(pbBuf, 0x00, RSA_KeyByteLen - inLen); + memcpy(pbBuf + RSA_KeyByteLen - inLen, in, inLen); + retVal = CRYPTO_SUCCESS; // add by guoxing.xu 20140919 + break; + + default: + free(pbBuf); + return CRYPTO_INVALID_ARGUMENT; } -// SDRM_PrintBN("ENPADDED Text : ", BN_pMsg); + // SDRM_PrintBN("ENPADDED Text : ", BN_pMsg); - if (retVal != CRYPTO_SUCCESS) - { + if (retVal != CRYPTO_SUCCESS) { free(pbBuf); return retVal; } @@ -1170,13 +1048,14 @@ int SDRM_RSA_encrypt(CryptoCoreContainer *crt, cc_u8 *in, cc_u32 inLen, cc_u8 *o //RSA Encryption by modular exponent #ifndef _OP64_NOTSUPPORTED - retVal = SDRM_BN_ModExp2(BN_Cipher, BN_pMsg, crt->ctx->rsactx->e, crt->ctx->rsactx->n); + retVal = SDRM_BN_ModExp2(BN_Cipher, BN_pMsg, crt->ctx->rsactx->e, + crt->ctx->rsactx->n); #else - retVal = SDRM_BN_ModExp(BN_Cipher, BN_pMsg, crt->ctx->rsactx->e, crt->ctx->rsactx->n); -#endif //_OP64_NOTSUPPORTED + retVal = SDRM_BN_ModExp(BN_Cipher, BN_pMsg, crt->ctx->rsactx->e, + crt->ctx->rsactx->n); +#endif //_OP64_NOTSUPPORTED - if (retVal != CRYPTO_SUCCESS) - { + if (retVal != CRYPTO_SUCCESS) { free(pbBuf); return retVal; } @@ -1184,9 +1063,7 @@ int SDRM_RSA_encrypt(CryptoCoreContainer *crt, cc_u8 *in, cc_u32 inLen, cc_u8 *o SDRM_I2OSP(BN_Cipher, RSA_KeyByteLen, out); if (outLen != NULL) - { *outLen = RSA_KeyByteLen; - } memset(pbBuf, 0x00, SDRM_RSA_ALLOC_SIZE * 2 + RSA_KeyByteLen); free(pbBuf); @@ -1195,58 +1072,58 @@ int SDRM_RSA_encrypt(CryptoCoreContainer *crt, cc_u8 *in, cc_u32 inLen, cc_u8 *o } /* - * @fn int SDRM_RSA_decrypt(CryptoCoreContainer *crt, cc_u8 *in, cc_u32 inLen, cc_u8 *out, cc_u32 *outLen) - * @brief RSA Decryption + * @fn int SDRM_RSA_decrypt(CryptoCoreContainer *crt, cc_u8 *in, cc_u32 inLen, cc_u8 *out, cc_u32 *outLen) + * @brief RSA Decryption * - * @param crt [in]rsa context - * @param in [in]message to decrypt - * @param inLen [in]byte-length of in - * @param out [out]decrypted message - * @param outLen [out]byte-length of out + * @param crt [in]rsa context + * @param in [in]message to decrypt + * @param inLen [in]byte-length of in + * @param out [out]decrypted message + * @param outLen [out]byte-length of out * - * @return CRYPTO_SUCCESS if no error is occured - * \n CRYPTO_NULL_POINTER if an argument is a null pointer - * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed + * @return CRYPTO_SUCCESS if no error is occured + * \n CRYPTO_NULL_POINTER if an argument is a null pointer + * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed */ -int SDRM_RSA_decrypt(CryptoCoreContainer *crt, cc_u8 *in, cc_u32 inLen, cc_u8 *out, cc_u32 *outLen) +int SDRM_RSA_decrypt(CryptoCoreContainer *crt, cc_u8 *in, cc_u32 inLen, + cc_u8 *out, cc_u32 *outLen) { - SDRM_BIG_NUM *BN_dMsg, *BN_Src; - int retVal; - cc_u32 plainLen; - cc_u32 RSA_KeyByteLen = 0; - cc_u8 *pbBuf = NULL; - - if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->rsactx == NULL) || (in == NULL) || (out == NULL)) - { + SDRM_BIG_NUM *BN_dMsg, *BN_Src; + int retVal; + cc_u32 plainLen; + cc_u32 RSA_KeyByteLen = 0; + cc_u8 *pbBuf = NULL; + + if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->rsactx == NULL) || + (in == NULL) || (out == NULL)) return CRYPTO_NULL_POINTER; - } RSA_KeyByteLen = crt->ctx->rsactx->k; + if (inLen > RSA_KeyByteLen) - { return CRYPTO_MSG_TOO_LONG; - } - pbBuf = (cc_u8*)malloc(SDRM_RSA_ALLOC_SIZE * 2 + RSA_KeyByteLen); + pbBuf = (cc_u8 *)malloc(SDRM_RSA_ALLOC_SIZE * 2 + RSA_KeyByteLen); + if (pbBuf == NULL) - { return CRYPTO_MEMORY_ALLOC_FAIL; - } - BN_dMsg = SDRM_BN_Alloc((cc_u8*)pbBuf + RSA_KeyByteLen, SDRM_RSA_BN_BUFSIZE); - BN_Src = SDRM_BN_Alloc((cc_u8*)BN_dMsg + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE); + BN_dMsg = SDRM_BN_Alloc((cc_u8 *)pbBuf + RSA_KeyByteLen, SDRM_RSA_BN_BUFSIZE); + BN_Src = SDRM_BN_Alloc((cc_u8 *)BN_dMsg + SDRM_RSA_ALLOC_SIZE, + SDRM_RSA_BN_BUFSIZE); SDRM_OS2BN(in, inLen, BN_Src); //RSA Decryption by modular exponent #ifndef _OP64_NOTSUPPORTED - retVal = SDRM_BN_ModExp2(BN_dMsg, BN_Src, crt->ctx->rsactx->d, crt->ctx->rsactx->n); + retVal = SDRM_BN_ModExp2(BN_dMsg, BN_Src, crt->ctx->rsactx->d, + crt->ctx->rsactx->n); #else - retVal = SDRM_BN_ModExp(BN_dMsg, BN_Src, crt->ctx->rsactx->d, crt->ctx->rsactx->n); -#endif //_OP64_NOTSUPPORTED + retVal = SDRM_BN_ModExp(BN_dMsg, BN_Src, crt->ctx->rsactx->d, + crt->ctx->rsactx->n); +#endif //_OP64_NOTSUPPORTED - if (retVal != CRYPTO_SUCCESS) - { + if (retVal != CRYPTO_SUCCESS) { free(pbBuf); return retVal; } @@ -1254,34 +1131,35 @@ int SDRM_RSA_decrypt(CryptoCoreContainer *crt, cc_u8 *in, cc_u32 inLen, cc_u8 *o SDRM_I2OSP(BN_dMsg, RSA_KeyByteLen, pbBuf); //Remove Padding from message - switch(SDRM_LOW_HALF(crt->ctx->rsactx->pm)) - { - case ID_RSAES_PKCS15 : - retVal = SDRM_Depad_Rsaes_pkcs15(out, &plainLen, pbBuf, RSA_KeyByteLen, RSA_KeyByteLen); - break; - case ID_RSAES_OAEP : - retVal = SDRM_Depad_Rsaes_oaep(out, &plainLen, pbBuf, RSA_KeyByteLen, RSA_KeyByteLen, SDRM_HIGH_HALF(crt->ctx->rsactx->pm)); - break; - case ID_NO_PADDING : - memcpy(out, pbBuf, RSA_KeyByteLen); - plainLen = RSA_KeyByteLen; - retVal = CRYPTO_SUCCESS; - break; - default : - free(pbBuf); - return CRYPTO_INVALID_ARGUMENT; + switch (SDRM_LOW_HALF(crt->ctx->rsactx->pm)) { + case ID_RSAES_PKCS15: + retVal = SDRM_Depad_Rsaes_pkcs15(out, &plainLen, pbBuf, RSA_KeyByteLen, + RSA_KeyByteLen); + break; + + case ID_RSAES_OAEP: + retVal = SDRM_Depad_Rsaes_oaep(out, &plainLen, pbBuf, RSA_KeyByteLen, + RSA_KeyByteLen, SDRM_HIGH_HALF(crt->ctx->rsactx->pm)); + break; + + case ID_NO_PADDING: + memcpy(out, pbBuf, RSA_KeyByteLen); + plainLen = RSA_KeyByteLen; + retVal = CRYPTO_SUCCESS; + break; + + default: + free(pbBuf); + return CRYPTO_INVALID_ARGUMENT; } - if (retVal != CRYPTO_SUCCESS) - { + if (retVal != CRYPTO_SUCCESS) { free(pbBuf); return retVal; } if (outLen != NULL) - { *outLen = plainLen; - } memset(pbBuf, 0x00, SDRM_RSA_ALLOC_SIZE * 2 + RSA_KeyByteLen); free(pbBuf); @@ -1290,166 +1168,165 @@ int SDRM_RSA_decrypt(CryptoCoreContainer *crt, cc_u8 *in, cc_u32 inLen, cc_u8 *o } /* - * @fn int SDRM_RSA_decryptByCRT(CryptoCoreContainer *crt, cc_u8 *in, cc_u32 inLen, cc_u8 *out, cc_u32 *outLen) - * @brief RSA Decryption using CRT + * @fn int SDRM_RSA_decryptByCRT(CryptoCoreContainer *crt, cc_u8 *in, cc_u32 inLen, cc_u8 *out, cc_u32 *outLen) + * @brief RSA Decryption using CRT * - * @param crt [in]rsa context - * @param in [in]message to decrypt - * @param inLen [in]byte-length of in - * @param out [out]decrypted message - * @param outLen [out]byte-length of out + * @param crt [in]rsa context + * @param in [in]message to decrypt + * @param inLen [in]byte-length of in + * @param out [out]decrypted message + * @param outLen [out]byte-length of out * - * @return CRYPTO_SUCCESS if no error is occured - * \n CRYPTO_NULL_POINTER if an argument is a null pointer - * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed + * @return CRYPTO_SUCCESS if no error is occured + * \n CRYPTO_NULL_POINTER if an argument is a null pointer + * \n CRYPTO_MEMORY_ALLOC_FAIL if memory allocation is failed */ -int SDRM_RSA_decryptByCRT(CryptoCoreContainer *crt, cc_u8 *in, cc_u32 inLen, cc_u8 *out, cc_u32 *outLen) +int SDRM_RSA_decryptByCRT(CryptoCoreContainer *crt, cc_u8 *in, cc_u32 inLen, + cc_u8 *out, cc_u32 *outLen) { - SDRM_BIG_NUM *BN_dMsg, *BN_Src; - int retVal; - cc_u32 plainLen; - cc_u32 RSA_KeyByteLen = 0; + SDRM_BIG_NUM *BN_dMsg, *BN_Src; + int retVal; + cc_u32 plainLen; + cc_u32 RSA_KeyByteLen = 0; SDRM_BIG_NUM *pi, *temp1, *temp2, *m1, *m2, *h; - cc_u8 *pbBuf = NULL; + cc_u8 *pbBuf = NULL; - if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->rsactx == NULL) || (in == NULL) || (out == NULL)) - { + if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->rsactx == NULL) || + (in == NULL) || (out == NULL)) return CRYPTO_NULL_POINTER; - } if (crt->ctx->rsactx->crt_operation != 1) - { return CRYPTO_INVALID_ARGUMENT; - } RSA_KeyByteLen = crt->ctx->rsactx->k; + if (inLen > RSA_KeyByteLen) - { return CRYPTO_MSG_TOO_LONG; - } - pbBuf = (cc_u8*)malloc(SDRM_RSA_ALLOC_SIZE * 8 + RSA_KeyByteLen); + pbBuf = (cc_u8 *)malloc(SDRM_RSA_ALLOC_SIZE * 8 + RSA_KeyByteLen); + if (pbBuf == NULL) - { return CRYPTO_MEMORY_ALLOC_FAIL; - } - BN_dMsg = SDRM_BN_Alloc((cc_u8*)pbBuf + RSA_KeyByteLen, SDRM_RSA_BN_BUFSIZE); - BN_Src = SDRM_BN_Alloc((cc_u8*)BN_dMsg + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE); - pi = SDRM_BN_Alloc((cc_u8*)BN_Src + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE); - temp1 = SDRM_BN_Alloc((cc_u8*)pi + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE); - temp2 = SDRM_BN_Alloc((cc_u8*)temp1 + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE); - m1 = SDRM_BN_Alloc((cc_u8*)temp2 + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE); - m2 = SDRM_BN_Alloc((cc_u8*)m1 + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE); - h = SDRM_BN_Alloc((cc_u8*)m2 + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE); + BN_dMsg = SDRM_BN_Alloc((cc_u8 *)pbBuf + RSA_KeyByteLen, + SDRM_RSA_BN_BUFSIZE); + BN_Src = SDRM_BN_Alloc((cc_u8 *)BN_dMsg + SDRM_RSA_ALLOC_SIZE, + SDRM_RSA_BN_BUFSIZE); + pi = SDRM_BN_Alloc((cc_u8 *)BN_Src + SDRM_RSA_ALLOC_SIZE, + SDRM_RSA_BN_BUFSIZE); + temp1 = SDRM_BN_Alloc((cc_u8 *)pi + SDRM_RSA_ALLOC_SIZE, + SDRM_RSA_BN_BUFSIZE); + temp2 = SDRM_BN_Alloc((cc_u8 *)temp1 + SDRM_RSA_ALLOC_SIZE, + SDRM_RSA_BN_BUFSIZE); + m1 = SDRM_BN_Alloc((cc_u8 *)temp2 + SDRM_RSA_ALLOC_SIZE, + SDRM_RSA_BN_BUFSIZE); + m2 = SDRM_BN_Alloc((cc_u8 *)m1 + SDRM_RSA_ALLOC_SIZE, + SDRM_RSA_BN_BUFSIZE); + h = SDRM_BN_Alloc((cc_u8 *)m2 + SDRM_RSA_ALLOC_SIZE, + SDRM_RSA_BN_BUFSIZE); SDRM_OS2BN(in, inLen, BN_Src); //RSA Decryption by CRT /* - dp = d mod (p - 1) - dq = d mod (q - 1) - qInv = (1/q) mod p where p > q + dp = d mod (p - 1) + dq = d mod (q - 1) + qInv = (1/q) mod p where p > q => - m1 = c^dp mod p - m2 = c^dq mod q - h = qInv(m1 - m2) mod p if (m1 >= m2) or h = qInv(m1 + p - m2) mod p if (m1 < m2) - m = m2 + hq + m1 = c^dp mod p + m2 = c^dq mod q + h = qInv(m1 - m2) mod p if (m1 >= m2) or h = qInv(m1 + p - m2) mod p if (m1 < m2) + m = m2 + hq */ // Prepare variables // 1. dP = d mod (p - 1) - // dP is already set when SDRM_RSA_setNEDPQ + // dP is already set when SDRM_RSA_setNEDPQ // 2. dQ = d mod (q - 1) - // dQ is already set when SDRM_RSA_setNEDPQ + // dQ is already set when SDRM_RSA_setNEDPQ // 3. qInv = (1/q) mod p where p > q - // qInv is already set when SDRM_RSA_setNEDPQ + // qInv is already set when SDRM_RSA_setNEDPQ // Computation // 4. m1 = c^dP mod p - if(SDRM_BN_ModExp2(m1, BN_Src, crt->ctx->rsactx->dmodp1, crt->ctx->rsactx->p)) - { + if (SDRM_BN_ModExp2(m1, BN_Src, crt->ctx->rsactx->dmodp1, + crt->ctx->rsactx->p)) { free(pbBuf); return CRYPTO_INVALID_ARGUMENT; } // 5. m2 = c^dQ mod q - if(SDRM_BN_ModExp2(m2, BN_Src, crt->ctx->rsactx->dmodq1, crt->ctx->rsactx->q)) - { + if (SDRM_BN_ModExp2(m2, BN_Src, crt->ctx->rsactx->dmodq1, + crt->ctx->rsactx->q)) { free(pbBuf); return CRYPTO_INVALID_ARGUMENT; } // 6. h = qInv(m1 - m2) mod p if (m1 >= m2) or h = qInv(m1 + p - m2) mod p if (m1 < m2) - if(SDRM_BN_Cmp(m1, m2) < 0) - { - if(SDRM_BN_Add(m1, m1, crt->ctx->rsactx->p)) - { + if (SDRM_BN_Cmp(m1, m2) < 0) { + if (SDRM_BN_Add(m1, m1, crt->ctx->rsactx->p)) { free(pbBuf); return CRYPTO_INVALID_ARGUMENT; } } - if(SDRM_BN_Sub(m1, m1, m2)) - { + if (SDRM_BN_Sub(m1, m1, m2)) { free(pbBuf); return CRYPTO_INVALID_ARGUMENT; } - if(SDRM_BN_ModMul(h, crt->ctx->rsactx->iqmodp, m1, crt->ctx->rsactx->p)) - { + if (SDRM_BN_ModMul(h, crt->ctx->rsactx->iqmodp, m1, crt->ctx->rsactx->p)) { free(pbBuf); return CRYPTO_INVALID_ARGUMENT; } // 7. m = m2 + hq - if(SDRM_BN_Mul(h, h, crt->ctx->rsactx->q)) - { + if (SDRM_BN_Mul(h, h, crt->ctx->rsactx->q)) { free(pbBuf); return CRYPTO_INVALID_ARGUMENT; } - if(SDRM_BN_Add(BN_dMsg, m2, h)) - { + if (SDRM_BN_Add(BN_dMsg, m2, h)) { free(pbBuf); return CRYPTO_INVALID_ARGUMENT; } -// SDRM_PrintBN("OAEP Text : ", BN_dMsg); + // SDRM_PrintBN("OAEP Text : ", BN_dMsg); SDRM_I2OSP(BN_dMsg, RSA_KeyByteLen, pbBuf); //Remove Padding from message - switch (SDRM_LOW_HALF(crt->ctx->rsactx->pm)) - { + switch (SDRM_LOW_HALF(crt->ctx->rsactx->pm)) { case ID_RSAES_PKCS15: - retVal = SDRM_Depad_Rsaes_pkcs15(out, &plainLen, pbBuf, RSA_KeyByteLen, RSA_KeyByteLen); + retVal = SDRM_Depad_Rsaes_pkcs15(out, &plainLen, pbBuf, RSA_KeyByteLen, + RSA_KeyByteLen); break; + case ID_RSAES_OAEP: - retVal = SDRM_Depad_Rsaes_oaep(out, &plainLen, pbBuf, RSA_KeyByteLen, RSA_KeyByteLen, SDRM_HIGH_HALF(crt->ctx->rsactx->pm)); + retVal = SDRM_Depad_Rsaes_oaep(out, &plainLen, pbBuf, RSA_KeyByteLen, + RSA_KeyByteLen, SDRM_HIGH_HALF(crt->ctx->rsactx->pm)); break; + case ID_NO_PADDING: memcpy(out, pbBuf, RSA_KeyByteLen); plainLen = RSA_KeyByteLen; retVal = CRYPTO_SUCCESS; break; + default: free(pbBuf); return CRYPTO_INVALID_ARGUMENT; } - if (retVal != CRYPTO_SUCCESS) - { + if (retVal != CRYPTO_SUCCESS) { free(pbBuf); return retVal; } if (outLen != NULL) - { *outLen = plainLen; - } memset(pbBuf, 0x00, SDRM_RSA_ALLOC_SIZE * 8 + RSA_KeyByteLen); free(pbBuf); @@ -1458,86 +1335,90 @@ int SDRM_RSA_decryptByCRT(CryptoCoreContainer *crt, cc_u8 *in, cc_u32 inLen, cc_ } /* - * @fn int SDRM_RSA_sign(CryptoCoreContainer *crt, cc_u8 *hash, cc_u32 hashLen, cc_u8 *signature, cc_u32 *signLen) - * @brief generate signature for given value + * @fn int SDRM_RSA_sign(CryptoCoreContainer *crt, cc_u8 *hash, cc_u32 hashLen, cc_u8 *signature, cc_u32 *signLen) + * @brief generate signature for given value * - * @param crt [in]crypto env structure - * @param hash [in]hash value - * @param hashLen [in]byte-length of hash - * @param signature [out]generated signature - * @param signLen [out]byte-length of signature + * @param crt [in]crypto env structure + * @param hash [in]hash value + * @param hashLen [in]byte-length of hash + * @param signature [out]generated signature + * @param signLen [out]byte-length of signature * - * @return CRYPTO_SUCCESS if success - * \n CRYPTO_NULL_POINTER if given argument is a null pointer + * @return CRYPTO_SUCCESS if success + * \n CRYPTO_NULL_POINTER if given argument is a null pointer */ -int SDRM_RSA_sign(CryptoCoreContainer *crt, cc_u8 *hash, cc_u32 hashLen, cc_u8 *signature, cc_u32 *signLen) +int SDRM_RSA_sign(CryptoCoreContainer *crt, cc_u8 *hash, cc_u32 hashLen, + cc_u8 *signature, cc_u32 *signLen) { - SDRM_BIG_NUM *BN_pMsg, *BN_Sign; - int retVal; - cc_u32 RSA_KeyByteLen = 0; - cc_u8 *pbBuf = NULL; - cc_u32 nBits; - - if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->rsactx == NULL) || (hash == NULL) || (signature == NULL)) - { + SDRM_BIG_NUM *BN_pMsg, *BN_Sign; + int retVal; + cc_u32 RSA_KeyByteLen = 0; + cc_u8 *pbBuf = NULL; + cc_u32 nBits; + + if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->rsactx == NULL) || + (hash == NULL) || (signature == NULL)) return CRYPTO_NULL_POINTER; - } RSA_KeyByteLen = crt->ctx->rsactx->k; + if (hashLen > RSA_KeyByteLen) - { return CRYPTO_MSG_TOO_LONG; - } - pbBuf = (cc_u8*)malloc(SDRM_RSA_ALLOC_SIZE * 2 + RSA_KeyByteLen); + pbBuf = (cc_u8 *)malloc(SDRM_RSA_ALLOC_SIZE * 2 + RSA_KeyByteLen); + if (pbBuf == NULL) - { return CRYPTO_MEMORY_ALLOC_FAIL; - } - BN_pMsg = SDRM_BN_Alloc((cc_u8*)pbBuf + RSA_KeyByteLen, SDRM_RSA_BN_BUFSIZE); - BN_Sign = SDRM_BN_Alloc((cc_u8*)BN_pMsg + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE); + BN_pMsg = SDRM_BN_Alloc((cc_u8 *)pbBuf + RSA_KeyByteLen, SDRM_RSA_BN_BUFSIZE); + BN_Sign = SDRM_BN_Alloc((cc_u8 *)BN_pMsg + SDRM_RSA_ALLOC_SIZE, + SDRM_RSA_BN_BUFSIZE); //Msg Padding - switch(SDRM_LOW_HALF(crt->ctx->rsactx->pm)) - { - case ID_RSASSA_PKCS15 : - retVal = SDRM_Enpad_Rsassa_pkcs15(pbBuf, RSA_KeyByteLen, hash, hashLen, SDRM_HIGH_HALF(crt->ctx->rsactx->pm)); - break; - case ID_RSASSA_PSS : - SDRM_BN_GETBITLEN(crt->ctx->rsactx->n, nBits); - retVal = SDRM_Enpad_Rsassa_pss(pbBuf, nBits, hash, hashLen, RSA_KeyByteLen, SDRM_HIGH_HALF(crt->ctx->rsactx->pm)); - break; - case ID_NO_PADDING : - memset(pbBuf, 0x00, RSA_KeyByteLen - hashLen); - //memcpy(pbBuf + hashLen, hash, RSA_KeyByteLen); - memcpy(pbBuf + RSA_KeyByteLen - hashLen, hash, hashLen);// fixed by guoxing.xu 20140919 - retVal = CRYPTO_SUCCESS; - break; - default : - free(pbBuf); - return CRYPTO_INVALID_ARGUMENT; + switch (SDRM_LOW_HALF(crt->ctx->rsactx->pm)) { + case ID_RSASSA_PKCS15: + retVal = SDRM_Enpad_Rsassa_pkcs15(pbBuf, RSA_KeyByteLen, hash, hashLen, + SDRM_HIGH_HALF(crt->ctx->rsactx->pm)); + break; + + case ID_RSASSA_PSS: + SDRM_BN_GETBITLEN(crt->ctx->rsactx->n, nBits); + retVal = SDRM_Enpad_Rsassa_pss(pbBuf, nBits, hash, hashLen, RSA_KeyByteLen, + SDRM_HIGH_HALF(crt->ctx->rsactx->pm)); + break; + + case ID_NO_PADDING: + memset(pbBuf, 0x00, RSA_KeyByteLen - hashLen); + //memcpy(pbBuf + hashLen, hash, RSA_KeyByteLen); + memcpy(pbBuf + RSA_KeyByteLen - hashLen, hash, + hashLen);// fixed by guoxing.xu 20140919 + retVal = CRYPTO_SUCCESS; + break; + + default: + free(pbBuf); + return CRYPTO_INVALID_ARGUMENT; } - if (retVal != CRYPTO_SUCCESS) - { + if (retVal != CRYPTO_SUCCESS) { free(pbBuf); return retVal; } -// SDRM_PrintBN("ENPADDED Msg : ", BN_pMsg); + // SDRM_PrintBN("ENPADDED Msg : ", BN_pMsg); SDRM_OS2BN(pbBuf, RSA_KeyByteLen, BN_pMsg); //RSA Signature by modular exponent #ifndef _OP64_NOTSUPPORTED - retVal = SDRM_BN_ModExp2(BN_Sign, BN_pMsg, crt->ctx->rsactx->d, crt->ctx->rsactx->n); + retVal = SDRM_BN_ModExp2(BN_Sign, BN_pMsg, crt->ctx->rsactx->d, + crt->ctx->rsactx->n); #else - retVal = SDRM_BN_ModExp(BN_Sign, BN_pMsg, crt->ctx->rsactx->d, crt->ctx->rsactx->n); -#endif //_OP64_NOTSUPPORTED + retVal = SDRM_BN_ModExp(BN_Sign, BN_pMsg, crt->ctx->rsactx->d, + crt->ctx->rsactx->n); +#endif //_OP64_NOTSUPPORTED - if (retVal != CRYPTO_SUCCESS) - { + if (retVal != CRYPTO_SUCCESS) { free(pbBuf); return retVal; } @@ -1545,9 +1426,7 @@ int SDRM_RSA_sign(CryptoCoreContainer *crt, cc_u8 *hash, cc_u32 hashLen, cc_u8 * SDRM_I2OSP(BN_Sign, RSA_KeyByteLen, signature); if (signLen != NULL) - { *signLen = RSA_KeyByteLen; - } memset(pbBuf, 0x00, SDRM_RSA_ALLOC_SIZE * 2 + RSA_KeyByteLen); free(pbBuf); @@ -1556,61 +1435,61 @@ int SDRM_RSA_sign(CryptoCoreContainer *crt, cc_u8 *hash, cc_u32 hashLen, cc_u8 * } /* - * @fn int SDRM_RSA_verify(CryptoCoreContainer *crt, cc_u8 *hash, cc_u32 hashLen, cc_u8 *signature, cc_u32 signLen, int *result) - * @brief generate signature for given value + * @fn int SDRM_RSA_verify(CryptoCoreContainer *crt, cc_u8 *hash, cc_u32 hashLen, cc_u8 *signature, cc_u32 signLen, int *result) + * @brief generate signature for given value * - * @param crt [in]crypto env structure - * @param hash [in]hash value - * @param hashLen [in]byte-length of hash - * @param signature [in]signature - * @param signLen [in]byte-length of signature - * @param result [in]result of verifying signature + * @param crt [in]crypto env structure + * @param hash [in]hash value + * @param hashLen [in]byte-length of hash + * @param signature [in]signature + * @param signLen [in]byte-length of signature + * @param result [in]result of verifying signature * - * @return CRYPTO_SUCCESS if success - * \n CRYPTO_NULL_POINTER if given argument is a null pointer - * \n CRYPTO_INVALID_ARGUMENT if the length of signature is invalid + * @return CRYPTO_SUCCESS if success + * \n CRYPTO_NULL_POINTER if given argument is a null pointer + * \n CRYPTO_INVALID_ARGUMENT if the length of signature is invalid */ -int SDRM_RSA_verify(CryptoCoreContainer *crt, cc_u8 *hash, cc_u32 hashLen, cc_u8 *signature, cc_u32 signLen, int *result) +int SDRM_RSA_verify(CryptoCoreContainer *crt, cc_u8 *hash, cc_u32 hashLen, + cc_u8 *signature, cc_u32 signLen, int *result) { - SDRM_BIG_NUM *BN_dMsg, *BN_Sign; - int retVal; - cc_u32 RSA_KeyByteLen = 0; - cc_u8 *pbBuf = NULL; - cc_u32 nBits; + SDRM_BIG_NUM *BN_dMsg, *BN_Sign; + int retVal; + cc_u32 RSA_KeyByteLen = 0; + cc_u8 *pbBuf = NULL; + cc_u32 nBits; cc_u32 i; - if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->rsactx == NULL) || (hash == NULL) || (signature == NULL)) - { + if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->rsactx == NULL) || + (hash == NULL) || (signature == NULL)) return CRYPTO_NULL_POINTER; - } RSA_KeyByteLen = crt->ctx->rsactx->k; + if (hashLen > RSA_KeyByteLen) - { return CRYPTO_MSG_TOO_LONG; - } - pbBuf = (cc_u8*)malloc(SDRM_RSA_ALLOC_SIZE * 2 + RSA_KeyByteLen); + pbBuf = (cc_u8 *)malloc(SDRM_RSA_ALLOC_SIZE * 2 + RSA_KeyByteLen); + if (pbBuf == NULL) - { return CRYPTO_MEMORY_ALLOC_FAIL; - } - BN_dMsg = SDRM_BN_Alloc((cc_u8*)pbBuf + RSA_KeyByteLen, SDRM_RSA_BN_BUFSIZE); - BN_Sign = SDRM_BN_Alloc((cc_u8*)BN_dMsg + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE); + BN_dMsg = SDRM_BN_Alloc((cc_u8 *)pbBuf + RSA_KeyByteLen, SDRM_RSA_BN_BUFSIZE); + BN_Sign = SDRM_BN_Alloc((cc_u8 *)BN_dMsg + SDRM_RSA_ALLOC_SIZE, + SDRM_RSA_BN_BUFSIZE); SDRM_OS2BN(signature, signLen, BN_Sign); -// SDRM_PrintBN("Generated Sign : ", BN_Sign); + // SDRM_PrintBN("Generated Sign : ", BN_Sign); //RSA Verification by modular exponent #ifndef _OP64_NOTSUPPORTED - retVal = SDRM_BN_ModExp2(BN_dMsg, BN_Sign, crt->ctx->rsactx->e, crt->ctx->rsactx->n); + retVal = SDRM_BN_ModExp2(BN_dMsg, BN_Sign, crt->ctx->rsactx->e, + crt->ctx->rsactx->n); #else - retVal = SDRM_BN_ModExp(BN_dMsg, BN_Sign, crt->ctx->rsactx->e, crt->ctx->rsactx->n); -#endif //_OP64_NOTSUPPORTED + retVal = SDRM_BN_ModExp(BN_dMsg, BN_Sign, crt->ctx->rsactx->e, + crt->ctx->rsactx->n); +#endif //_OP64_NOTSUPPORTED - if (retVal != CRYPTO_SUCCESS) - { + if (retVal != CRYPTO_SUCCESS) { free(pbBuf); return retVal; } @@ -1618,35 +1497,33 @@ int SDRM_RSA_verify(CryptoCoreContainer *crt, cc_u8 *hash, cc_u32 hashLen, cc_u8 SDRM_I2OSP(BN_dMsg, RSA_KeyByteLen, pbBuf); //Msg Depadding - switch(SDRM_LOW_HALF(crt->ctx->rsactx->pm)) - { - case ID_RSASSA_PKCS15 : - *result = SDRM_Depad_Rsassa_pkcs15(pbBuf, RSA_KeyByteLen, hash, hashLen, SDRM_HIGH_HALF(crt->ctx->rsactx->pm)); - break; - case ID_RSASSA_PSS : - SDRM_BN_GETBITLEN(crt->ctx->rsactx->n, nBits); - *result = SDRM_Depad_Rsassa_pss(pbBuf, nBits, hash, hashLen, RSA_KeyByteLen, SDRM_HIGH_HALF(crt->ctx->rsactx->pm)); - break; - case ID_NO_PADDING : - for (i = 0; i < (RSA_KeyByteLen - hashLen); i++) - { - if (pbBuf[i] != 0) - { - *result = CRYPTO_INVALID_SIGN; - } - } + switch (SDRM_LOW_HALF(crt->ctx->rsactx->pm)) { + case ID_RSASSA_PKCS15: + *result = SDRM_Depad_Rsassa_pkcs15(pbBuf, RSA_KeyByteLen, hash, hashLen, + SDRM_HIGH_HALF(crt->ctx->rsactx->pm)); + break; - if ((i == (RSA_KeyByteLen - hashLen)) && (memcmp(pbBuf + i, hash, hashLen) == 0)) - { - *result = CRYPTO_VALID_SIGN; - } - else - { + case ID_RSASSA_PSS: + SDRM_BN_GETBITLEN(crt->ctx->rsactx->n, nBits); + *result = SDRM_Depad_Rsassa_pss(pbBuf, nBits, hash, hashLen, RSA_KeyByteLen, + SDRM_HIGH_HALF(crt->ctx->rsactx->pm)); + break; + + case ID_NO_PADDING: + for (i = 0; i < (RSA_KeyByteLen - hashLen); i++) { + if (pbBuf[i] != 0) *result = CRYPTO_INVALID_SIGN; - } + } - default : - break; + if ((i == (RSA_KeyByteLen - hashLen)) && + (memcmp(pbBuf + i, hash, hashLen) == 0)) + *result = CRYPTO_VALID_SIGN; + + else + *result = CRYPTO_INVALID_SIGN; + + default: + break; } memset(pbBuf, 0x00, SDRM_RSA_ALLOC_SIZE * 2 + RSA_KeyByteLen); @@ -1656,42 +1533,50 @@ int SDRM_RSA_verify(CryptoCoreContainer *crt, cc_u8 *hash, cc_u32 hashLen, cc_u8 } /***************************** End of File *****************************/ -int SDRM_Extended_GCD(SDRM_BIG_NUM* BN_v, SDRM_BIG_NUM* BN_a, SDRM_BIG_NUM* BN_b, SDRM_BIG_NUM* BN_x, SDRM_BIG_NUM* BN_y) +int SDRM_Extended_GCD(SDRM_BIG_NUM *BN_v, SDRM_BIG_NUM *BN_a, + SDRM_BIG_NUM *BN_b, SDRM_BIG_NUM *BN_x, SDRM_BIG_NUM *BN_y) { - SDRM_BIG_NUM* BN_g; - SDRM_BIG_NUM* BN_u; - SDRM_BIG_NUM* BN_A; - SDRM_BIG_NUM* BN_B; - SDRM_BIG_NUM* BN_C; - SDRM_BIG_NUM* BN_D; - SDRM_BIG_NUM* BN_tmp; - SDRM_BIG_NUM* BN_xx; - SDRM_BIG_NUM* BN_yy; - cc_u8* pbBuf = NULL; + SDRM_BIG_NUM *BN_g; + SDRM_BIG_NUM *BN_u; + SDRM_BIG_NUM *BN_A; + SDRM_BIG_NUM *BN_B; + SDRM_BIG_NUM *BN_C; + SDRM_BIG_NUM *BN_D; + SDRM_BIG_NUM *BN_tmp; + SDRM_BIG_NUM *BN_xx; + SDRM_BIG_NUM *BN_yy; + cc_u8 *pbBuf = NULL; cc_u32 RSA_KeyByteLen = 128; - pbBuf = (cc_u8*)malloc(SDRM_RSA_ALLOC_SIZE * 9); + pbBuf = (cc_u8 *)malloc(SDRM_RSA_ALLOC_SIZE * 9); + if (pbBuf == NULL) - { return CRYPTO_MEMORY_ALLOC_FAIL; - } - BN_g = SDRM_BN_Alloc((cc_u8*)pbBuf, SDRM_RSA_BN_BUFSIZE); - BN_u = SDRM_BN_Alloc((cc_u8*)BN_g + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE); - BN_A = SDRM_BN_Alloc((cc_u8*)BN_u + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE); - BN_B = SDRM_BN_Alloc((cc_u8*)BN_A + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE); - BN_C = SDRM_BN_Alloc((cc_u8*)BN_B + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE); - BN_D = SDRM_BN_Alloc((cc_u8*)BN_C + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE); - BN_tmp = SDRM_BN_Alloc((cc_u8*)BN_D + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE); - BN_xx = SDRM_BN_Alloc((cc_u8*)BN_tmp + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE); - BN_yy = SDRM_BN_Alloc((cc_u8*)BN_xx + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE); + BN_g = SDRM_BN_Alloc((cc_u8 *)pbBuf, + SDRM_RSA_BN_BUFSIZE); + BN_u = SDRM_BN_Alloc((cc_u8 *)BN_g + SDRM_RSA_ALLOC_SIZE, + SDRM_RSA_BN_BUFSIZE); + BN_A = SDRM_BN_Alloc((cc_u8 *)BN_u + SDRM_RSA_ALLOC_SIZE, + SDRM_RSA_BN_BUFSIZE); + BN_B = SDRM_BN_Alloc((cc_u8 *)BN_A + SDRM_RSA_ALLOC_SIZE, + SDRM_RSA_BN_BUFSIZE); + BN_C = SDRM_BN_Alloc((cc_u8 *)BN_B + SDRM_RSA_ALLOC_SIZE, + SDRM_RSA_BN_BUFSIZE); + BN_D = SDRM_BN_Alloc((cc_u8 *)BN_C + SDRM_RSA_ALLOC_SIZE, + SDRM_RSA_BN_BUFSIZE); + BN_tmp = SDRM_BN_Alloc((cc_u8 *)BN_D + SDRM_RSA_ALLOC_SIZE, + SDRM_RSA_BN_BUFSIZE); + BN_xx = SDRM_BN_Alloc((cc_u8 *)BN_tmp + SDRM_RSA_ALLOC_SIZE, + SDRM_RSA_BN_BUFSIZE); + BN_yy = SDRM_BN_Alloc((cc_u8 *)BN_xx + SDRM_RSA_ALLOC_SIZE, + SDRM_RSA_BN_BUFSIZE); SDRM_BN_Copy(BN_g, BN_One); SDRM_BN_Copy(BN_xx, BN_x); SDRM_BN_Copy(BN_yy, BN_y); - while(!SDRM_BN_IS_ODD(BN_xx) && !SDRM_BN_IS_ODD(BN_yy)) - { + while (!SDRM_BN_IS_ODD(BN_xx) && !SDRM_BN_IS_ODD(BN_yy)) { SDRM_BN_SHR(BN_xx, BN_xx, 1); SDRM_BN_SHR(BN_yy, BN_yy, 1); SDRM_BN_SHL(BN_g, BN_g, 1); @@ -1705,18 +1590,14 @@ int SDRM_Extended_GCD(SDRM_BIG_NUM* BN_v, SDRM_BIG_NUM* BN_a, SDRM_BIG_NUM* BN_b SDRM_BN_Copy(BN_C, BN_Zero); SDRM_BN_Copy(BN_D, BN_One); - while(1) - { - while(!SDRM_BN_IS_ODD(BN_u)) - { + while (1) { + while (!SDRM_BN_IS_ODD(BN_u)) { SDRM_BN_SHR(BN_u, BN_u, 1); - if (!SDRM_BN_IS_ODD(BN_A) && !SDRM_BN_IS_ODD(BN_B)) - { + + if (!SDRM_BN_IS_ODD(BN_A) && !SDRM_BN_IS_ODD(BN_B)) { SDRM_BN_SHR(BN_A, BN_A, 1); SDRM_BN_SHR(BN_B, BN_B, 1); - } - else - { + } else { SDRM_BN_Add(BN_A, BN_A, BN_yy); SDRM_BN_SHR(BN_A, BN_A, 1); @@ -1725,16 +1606,13 @@ int SDRM_Extended_GCD(SDRM_BIG_NUM* BN_v, SDRM_BIG_NUM* BN_a, SDRM_BIG_NUM* BN_b } } - while(!SDRM_BN_IS_ODD(BN_v)) - { + while (!SDRM_BN_IS_ODD(BN_v)) { SDRM_BN_SHR(BN_v, BN_v, 1); - if (!SDRM_BN_IS_ODD(BN_C) && !SDRM_BN_IS_ODD(BN_D)) - { + + if (!SDRM_BN_IS_ODD(BN_C) && !SDRM_BN_IS_ODD(BN_D)) { SDRM_BN_SHR(BN_C, BN_C, 1); SDRM_BN_SHR(BN_D, BN_D, 1); - } - else - { + } else { SDRM_BN_Add(BN_C, BN_C, BN_yy); SDRM_BN_SHR(BN_C, BN_C, 1); @@ -1743,8 +1621,7 @@ int SDRM_Extended_GCD(SDRM_BIG_NUM* BN_v, SDRM_BIG_NUM* BN_a, SDRM_BIG_NUM* BN_b } } - if (SDRM_BN_Cmp(BN_u, BN_v) >= 0) - { + if (SDRM_BN_Cmp(BN_u, BN_v) >= 0) { SDRM_BN_Sub(BN_tmp, BN_u, BN_v); SDRM_BN_Copy(BN_u, BN_tmp); @@ -1753,9 +1630,7 @@ int SDRM_Extended_GCD(SDRM_BIG_NUM* BN_v, SDRM_BIG_NUM* BN_a, SDRM_BIG_NUM* BN_b SDRM_BN_Sub(BN_tmp, BN_B, BN_D); SDRM_BN_Copy(BN_B, BN_tmp); - } - else - { + } else { SDRM_BN_Sub(BN_tmp, BN_v, BN_u); SDRM_BN_Copy(BN_v, BN_tmp); @@ -1766,8 +1641,7 @@ int SDRM_Extended_GCD(SDRM_BIG_NUM* BN_v, SDRM_BIG_NUM* BN_a, SDRM_BIG_NUM* BN_b SDRM_BN_Copy(BN_D, BN_tmp); } - if (SDRM_BN_Cmp(BN_u, BN_Zero) == 0) - { + if (SDRM_BN_Cmp(BN_u, BN_Zero) == 0) { SDRM_BN_Copy(BN_a, BN_C); SDRM_BN_Copy(BN_b, BN_D); SDRM_BN_Mul(BN_tmp, BN_g, BN_v); @@ -1783,25 +1657,27 @@ int SDRM_Extended_GCD(SDRM_BIG_NUM* BN_v, SDRM_BIG_NUM* BN_a, SDRM_BIG_NUM* BN_b } -int SDRM_CheckRSAKey(SDRM_BIG_NUM* BN_n, SDRM_BIG_NUM* BN_e, SDRM_BIG_NUM* BN_d) +int SDRM_CheckRSAKey(SDRM_BIG_NUM *BN_n, SDRM_BIG_NUM *BN_e, SDRM_BIG_NUM *BN_d) { - SDRM_BIG_NUM* BN_m; - SDRM_BIG_NUM* BN_c; - SDRM_BIG_NUM* BN_m1; - cc_u8* pbBuf = NULL; + SDRM_BIG_NUM *BN_m; + SDRM_BIG_NUM *BN_c; + SDRM_BIG_NUM *BN_m1; + cc_u8 *pbBuf = NULL; cc_u32 RSA_KeyByteLen = 128; int retVal; - pbBuf = (cc_u8*)malloc(SDRM_RSA_ALLOC_SIZE * 3); + pbBuf = (cc_u8 *)malloc(SDRM_RSA_ALLOC_SIZE * 3); + if (pbBuf == NULL) - { return CRYPTO_MEMORY_ALLOC_FAIL; - } - BN_m = SDRM_BN_Alloc((cc_u8*)pbBuf, SDRM_RSA_BN_BUFSIZE); - BN_c = SDRM_BN_Alloc((cc_u8*)BN_m + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE); - BN_m1 = SDRM_BN_Alloc((cc_u8*)BN_c + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE); + BN_m = SDRM_BN_Alloc((cc_u8 *)pbBuf, + SDRM_RSA_BN_BUFSIZE); + BN_c = SDRM_BN_Alloc((cc_u8 *)BN_m + SDRM_RSA_ALLOC_SIZE, + SDRM_RSA_BN_BUFSIZE); + BN_m1 = SDRM_BN_Alloc((cc_u8 *)BN_c + SDRM_RSA_ALLOC_SIZE, + SDRM_RSA_BN_BUFSIZE); SDRM_BN_Rand(BN_m, 1020); @@ -1809,13 +1685,10 @@ int SDRM_CheckRSAKey(SDRM_BIG_NUM* BN_n, SDRM_BIG_NUM* BN_e, SDRM_BIG_NUM* BN_d) SDRM_BN_ModExp(BN_m1, BN_c, BN_d, BN_n); if (SDRM_BN_Cmp(BN_m, BN_m1) == 0) - { retVal = CRYPTO_SUCCESS; - } + else - { retVal = CRYPTO_ERROR; - } free(pbBuf); @@ -1824,44 +1697,57 @@ int SDRM_CheckRSAKey(SDRM_BIG_NUM* BN_n, SDRM_BIG_NUM* BN_e, SDRM_BIG_NUM* BN_d) int SDRM_RSA_ConvertCRT2PrivateExp(cc_u8 *p320byteCRTParam, cc_u8 *PrivateExp) { - SDRM_BIG_NUM* BN_g; - SDRM_BIG_NUM* BN_v; - SDRM_BIG_NUM* BN_diff; - SDRM_BIG_NUM* BN_k; - SDRM_BIG_NUM* BN_r; - SDRM_BIG_NUM* BN_l; - SDRM_BIG_NUM* BN_u; - SDRM_BIG_NUM* BN_n; - SDRM_BIG_NUM* BN_e; - SDRM_BIG_NUM* BN_d; - SDRM_BIG_NUM* BN_p; - SDRM_BIG_NUM* BN_q; - SDRM_BIG_NUM* BN_dp; - SDRM_BIG_NUM* BN_dq; - - cc_u8* pbBuf = NULL; + SDRM_BIG_NUM *BN_g; + SDRM_BIG_NUM *BN_v; + SDRM_BIG_NUM *BN_diff; + SDRM_BIG_NUM *BN_k; + SDRM_BIG_NUM *BN_r; + SDRM_BIG_NUM *BN_l; + SDRM_BIG_NUM *BN_u; + SDRM_BIG_NUM *BN_n; + SDRM_BIG_NUM *BN_e; + SDRM_BIG_NUM *BN_d; + SDRM_BIG_NUM *BN_p; + SDRM_BIG_NUM *BN_q; + SDRM_BIG_NUM *BN_dp; + SDRM_BIG_NUM *BN_dq; + + cc_u8 *pbBuf = NULL; cc_u32 RSA_KeyByteLen = 128; - pbBuf = (cc_u8*)malloc(SDRM_RSA_ALLOC_SIZE * 14); + pbBuf = (cc_u8 *)malloc(SDRM_RSA_ALLOC_SIZE * 14); + if (pbBuf == NULL) - { return CRYPTO_MEMORY_ALLOC_FAIL; - } - BN_g = SDRM_BN_Alloc((cc_u8*)pbBuf, SDRM_RSA_BN_BUFSIZE); - BN_v = SDRM_BN_Alloc((cc_u8*)BN_g + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE); - BN_diff = SDRM_BN_Alloc((cc_u8*)BN_v + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE); - BN_k = SDRM_BN_Alloc((cc_u8*)BN_diff + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE); - BN_r = SDRM_BN_Alloc((cc_u8*)BN_k + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE); - BN_l = SDRM_BN_Alloc((cc_u8*)BN_r + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE); - BN_u = SDRM_BN_Alloc((cc_u8*)BN_l + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE); - BN_n = SDRM_BN_Alloc((cc_u8*)BN_u + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE); - BN_e = SDRM_BN_Alloc((cc_u8*)BN_n + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE); - BN_d = SDRM_BN_Alloc((cc_u8*)BN_e + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE); - BN_p = SDRM_BN_Alloc((cc_u8*)BN_d + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE); - BN_q = SDRM_BN_Alloc((cc_u8*)BN_p + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE); - BN_dp = SDRM_BN_Alloc((cc_u8*)BN_q + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE); - BN_dq = SDRM_BN_Alloc((cc_u8*)BN_dp + SDRM_RSA_ALLOC_SIZE, SDRM_RSA_BN_BUFSIZE); + BN_g = SDRM_BN_Alloc((cc_u8 *)pbBuf, + SDRM_RSA_BN_BUFSIZE); + BN_v = SDRM_BN_Alloc((cc_u8 *)BN_g + SDRM_RSA_ALLOC_SIZE, + SDRM_RSA_BN_BUFSIZE); + BN_diff = SDRM_BN_Alloc((cc_u8 *)BN_v + SDRM_RSA_ALLOC_SIZE, + SDRM_RSA_BN_BUFSIZE); + BN_k = SDRM_BN_Alloc((cc_u8 *)BN_diff + SDRM_RSA_ALLOC_SIZE, + SDRM_RSA_BN_BUFSIZE); + BN_r = SDRM_BN_Alloc((cc_u8 *)BN_k + SDRM_RSA_ALLOC_SIZE, + SDRM_RSA_BN_BUFSIZE); + BN_l = SDRM_BN_Alloc((cc_u8 *)BN_r + SDRM_RSA_ALLOC_SIZE, + SDRM_RSA_BN_BUFSIZE); + BN_u = SDRM_BN_Alloc((cc_u8 *)BN_l + SDRM_RSA_ALLOC_SIZE, + SDRM_RSA_BN_BUFSIZE); + BN_n = SDRM_BN_Alloc((cc_u8 *)BN_u + SDRM_RSA_ALLOC_SIZE, + SDRM_RSA_BN_BUFSIZE); + BN_e = SDRM_BN_Alloc((cc_u8 *)BN_n + SDRM_RSA_ALLOC_SIZE, + SDRM_RSA_BN_BUFSIZE); + BN_d = SDRM_BN_Alloc((cc_u8 *)BN_e + SDRM_RSA_ALLOC_SIZE, + SDRM_RSA_BN_BUFSIZE); + BN_p = SDRM_BN_Alloc((cc_u8 *)BN_d + SDRM_RSA_ALLOC_SIZE, + SDRM_RSA_BN_BUFSIZE); + BN_q = SDRM_BN_Alloc((cc_u8 *)BN_p + SDRM_RSA_ALLOC_SIZE, + SDRM_RSA_BN_BUFSIZE); + BN_dp = SDRM_BN_Alloc((cc_u8 *)BN_q + SDRM_RSA_ALLOC_SIZE, + SDRM_RSA_BN_BUFSIZE); + BN_dq = SDRM_BN_Alloc((cc_u8 *)BN_dp + SDRM_RSA_ALLOC_SIZE, + SDRM_RSA_BN_BUFSIZE); SDRM_OS2BN(p320byteCRTParam, 64, BN_p); SDRM_OS2BN(p320byteCRTParam + 64, 64, BN_q); @@ -1870,9 +1756,8 @@ int SDRM_RSA_ConvertCRT2PrivateExp(cc_u8 *p320byteCRTParam, cc_u8 *PrivateExp) SDRM_BN_Mul(BN_n, BN_p, BN_q); - if (SDRM_BN_Cmp(BN_dp, BN_dq) < 0) - { - SDRM_BIG_NUM* tmp; + if (SDRM_BN_Cmp(BN_dp, BN_dq) < 0) { + SDRM_BIG_NUM *tmp; tmp = BN_p; BN_p = BN_q; BN_q = tmp; @@ -1899,30 +1784,23 @@ int SDRM_RSA_ConvertCRT2PrivateExp(cc_u8 *p320byteCRTParam, cc_u8 *PrivateExp) SDRM_BN_ModRed(BN_r, BN_d, BN_l); if ((SDRM_BN_Cmp(BN_r, BN_Zero) != 0) && SDRM_IS_BN_NEGATIVE(BN_r)) - { SDRM_BN_Add(BN_d, BN_l, BN_r); - } + else - { SDRM_BN_Copy(BN_d, BN_r); - } SDRM_BN_ModInv(BN_e, BN_d, BN_l); SDRM_BN_ModInv(BN_d, BN_e, BN_k); - if ((SDRM_BN_Cmp(BN_d, BN_Zero) != 0) && !SDRM_IS_BN_NEGATIVE(BN_d)) - { + if ((SDRM_BN_Cmp(BN_d, BN_Zero) != 0) && !SDRM_IS_BN_NEGATIVE(BN_d)) { SDRM_BN_ModRed(BN_r, BN_d, BN_p); SDRM_PrintBN("n", BN_n); SDRM_PrintBN("e", BN_e); SDRM_PrintBN("d", BN_d); - if (SDRM_BN_Cmp(BN_r, BN_dp) == 0) - { + if (SDRM_BN_Cmp(BN_r, BN_dp) == 0) { if (SDRM_CheckRSAKey(BN_n, BN_e, BN_d) == CRYPTO_SUCCESS) - { SDRM_BN2OS(BN_d, 128, PrivateExp); - } } } diff --git a/ssflib/dep/cryptocore/source/middle/cc_symmetric.c b/ssflib/dep/cryptocore/source/middle/cc_symmetric.c index b313fe4..06309a3 100644 --- a/ssflib/dep/cryptocore/source/middle/cc_symmetric.c +++ b/ssflib/dep/cryptocore/source/middle/cc_symmetric.c @@ -33,156 +33,158 @@ // Functions //////////////////////////////////////////////////////////////////////////// /* - * @fn SDRM_getEncRoundKey - * @brief get scheduled key for encryption + * @fn SDRM_getEncRoundKey + * @brief get scheduled key for encryption * - * @param Algorithm [in]cipher algorithm - * @param UserKey [in]user key - * @param RoundKey [out]round key + * @param Algorithm [in]cipher algorithm + * @param UserKey [in]user key + * @param RoundKey [out]round key * - * @return CRYPTO_SUCCESS if success - * \n CRYPTO_NULL_POINTER if given argument is a null pointer - * \n CRYPTO_INVALID_ARGUMENT if given argument is invalid + * @return CRYPTO_SUCCESS if success + * \n CRYPTO_NULL_POINTER if given argument is a null pointer + * \n CRYPTO_INVALID_ARGUMENT if given argument is invalid */ -int SDRM_getEncRoundKey(int Algorithm, cc_u8* UserKey, cc_u8* RoundKey) +int SDRM_getEncRoundKey(int Algorithm, cc_u8 *UserKey, cc_u8 *RoundKey) { if ((UserKey == NULL) || (RoundKey == NULL)) - { return CRYPTO_NULL_POINTER; - } - switch (Algorithm) - { - case ID_AES128 : - SDRM_rijndaelKeySetupEnc((cc_u32*)(void*)RoundKey, UserKey, 128); - return CRYPTO_SUCCESS; - case ID_AES192 : - SDRM_rijndaelKeySetupEnc((cc_u32*)(void*)RoundKey, UserKey, 192); - return CRYPTO_SUCCESS; - case ID_AES256 : - SDRM_rijndaelKeySetupEnc((cc_u32*)(void*)RoundKey, UserKey, 256); - return CRYPTO_SUCCESS; - case ID_DES : - SDRM_DES_KeySched(RoundKey, UserKey, 0, 1); - return CRYPTO_SUCCESS; - case ID_TDES_EDE2 : - SDRM_TDES_KeySched(RoundKey, UserKey, 16, 1); - return CRYPTO_SUCCESS; - case ID_TDES_EDE3 : - SDRM_TDES_KeySched(RoundKey, UserKey, 24, 1); - return CRYPTO_SUCCESS; - default : - break; + switch (Algorithm) { + case ID_AES128: + SDRM_rijndaelKeySetupEnc((cc_u32 *)(void *)RoundKey, UserKey, 128); + return CRYPTO_SUCCESS; + + case ID_AES192: + SDRM_rijndaelKeySetupEnc((cc_u32 *)(void *)RoundKey, UserKey, 192); + return CRYPTO_SUCCESS; + + case ID_AES256: + SDRM_rijndaelKeySetupEnc((cc_u32 *)(void *)RoundKey, UserKey, 256); + return CRYPTO_SUCCESS; + + case ID_DES: + SDRM_DES_KeySched(RoundKey, UserKey, 0, 1); + return CRYPTO_SUCCESS; + + case ID_TDES_EDE2: + SDRM_TDES_KeySched(RoundKey, UserKey, 16, 1); + return CRYPTO_SUCCESS; + + case ID_TDES_EDE3: + SDRM_TDES_KeySched(RoundKey, UserKey, 24, 1); + return CRYPTO_SUCCESS; + + default: + break; } return CRYPTO_INVALID_ARGUMENT; } /* - * @fn SDRM_getDecRoundKey - * @brief get scheduled key for decryption + * @fn SDRM_getDecRoundKey + * @brief get scheduled key for decryption * - * @param Algorithm [in]cipher algorithm - * @param UserKey [in]user key - * @param RoundKey [out]round key + * @param Algorithm [in]cipher algorithm + * @param UserKey [in]user key + * @param RoundKey [out]round key * - * @return CRYPTO_SUCCESS if success - * \n CRYPTO_NULL_POINTER if given argument is a null pointer - * \n CRYPTO_INVALID_ARGUMENT if given argument is invalid + * @return CRYPTO_SUCCESS if success + * \n CRYPTO_NULL_POINTER if given argument is a null pointer + * \n CRYPTO_INVALID_ARGUMENT if given argument is invalid */ -int SDRM_getDecRoundKey(int Algorithm, cc_u8* UserKey, cc_u8* RoundKey) +int SDRM_getDecRoundKey(int Algorithm, cc_u8 *UserKey, cc_u8 *RoundKey) { if ((UserKey == NULL) || (RoundKey == NULL)) - { return CRYPTO_NULL_POINTER; - } - switch (Algorithm) - { - case ID_AES128 : - SDRM_rijndaelKeySetupDec((cc_u32*)(void*)RoundKey, UserKey, 128); - return CRYPTO_SUCCESS; - case ID_AES192 : - SDRM_rijndaelKeySetupDec((cc_u32*)(void*)RoundKey, UserKey, 192); - return CRYPTO_SUCCESS; - case ID_AES256 : - SDRM_rijndaelKeySetupDec((cc_u32*)(void*)RoundKey, UserKey, 256); - return CRYPTO_SUCCESS; - case ID_DES : - SDRM_DES_KeySched(RoundKey, UserKey, 15, (cc_u32)-1); - return CRYPTO_SUCCESS; - case ID_TDES_EDE2 : - SDRM_TDES_KeySched(RoundKey, UserKey, 16, (cc_u32)-1); - return CRYPTO_SUCCESS; - case ID_TDES_EDE3 : - SDRM_TDES_KeySched(RoundKey, UserKey, 24, (cc_u32)-1); - return CRYPTO_SUCCESS; - default : - break; + switch (Algorithm) { + case ID_AES128: + SDRM_rijndaelKeySetupDec((cc_u32 *)(void *)RoundKey, UserKey, 128); + return CRYPTO_SUCCESS; + + case ID_AES192: + SDRM_rijndaelKeySetupDec((cc_u32 *)(void *)RoundKey, UserKey, 192); + return CRYPTO_SUCCESS; + + case ID_AES256: + SDRM_rijndaelKeySetupDec((cc_u32 *)(void *)RoundKey, UserKey, 256); + return CRYPTO_SUCCESS; + + case ID_DES: + SDRM_DES_KeySched(RoundKey, UserKey, 15, (cc_u32) - 1); + return CRYPTO_SUCCESS; + + case ID_TDES_EDE2: + SDRM_TDES_KeySched(RoundKey, UserKey, 16, (cc_u32) - 1); + return CRYPTO_SUCCESS; + + case ID_TDES_EDE3: + SDRM_TDES_KeySched(RoundKey, UserKey, 24, (cc_u32) - 1); + return CRYPTO_SUCCESS; + + default: + break; } return CRYPTO_INVALID_ARGUMENT; } /* - * @fn SDRM_AES_init - * @brief intialize crypt context for aes + * @fn SDRM_AES_init + * @brief intialize crypt context for aes * - * @param crt [out]crypto env structure - * @param mode [in]encryption|decryption and mode of operation - * @param PADDING [in]padding method - * @param key [in]user key - * @param keysize [in]byte-length of key - * @param IV [in]initial vector + * @param crt [out]crypto env structure + * @param mode [in]encryption|decryption and mode of operation + * @param PADDING [in]padding method + * @param key [in]user key + * @param keysize [in]byte-length of key + * @param IV [in]initial vector * - * @return CRYPTO_SUCCESS if success - * \n CRYPTO_NULL_POINTER if given argument is a null pointer - * \n CRYPTO_INVALID_ARGUMENT if given argument is invalid + * @return CRYPTO_SUCCESS if success + * \n CRYPTO_NULL_POINTER if given argument is a null pointer + * \n CRYPTO_INVALID_ARGUMENT if given argument is invalid */ -int SDRM_AES_init(CryptoCoreContainer *crt, cc_u32 mode, cc_u32 PADDING, cc_u8 *key, cc_u32 keysize, cc_u8 *IV) +int SDRM_AES_init(CryptoCoreContainer *crt, cc_u32 mode, cc_u32 PADDING, + cc_u8 *key, cc_u32 keysize, cc_u8 *IV) { - if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->aesctx == NULL) || (key == NULL)) - { + if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->aesctx == NULL) || + (key == NULL)) return CRYPTO_NULL_POINTER; - } - if (!(((mode >= 1111) && (mode <= 1115)) || ((mode >= 1121) && (mode <= 1125)))) - { + + if (!(((mode >= 1111) && (mode <= 1115)) || ((mode >= 1121) && + (mode <= 1125)))) return CRYPTO_INVALID_ARGUMENT; - } + if (!((crt->alg == ID_AES128) && (keysize == 16)) && - !((crt->alg == ID_AES192) && (keysize == 24)) && - !((crt->alg == ID_AES256) && (keysize == 32))) - { + !((crt->alg == ID_AES192) && (keysize == 24)) && + !((crt->alg == ID_AES256) && (keysize == 32))) return CRYPTO_INVALID_ARGUMENT; - } - if ((crt->alg != ID_AES128) && (crt->alg != ID_AES192) && (crt->alg != ID_AES256)) - { + + if ((crt->alg != ID_AES128) && (crt->alg != ID_AES192) && + (crt->alg != ID_AES256)) return CRYPTO_INVALID_ARGUMENT; - } - if ((PADDING != 0) && (PADDING != ID_PKCS5) && (PADDING != ID_SSL_PADDING) && (PADDING != ID_ZERO_PADDING) && (PADDING != ID_NO_PADDING)) - { + if ((PADDING != 0) && (PADDING != ID_PKCS5) && (PADDING != ID_SSL_PADDING) && + (PADDING != ID_ZERO_PADDING) && (PADDING != ID_NO_PADDING)) return CRYPTO_INVALID_ARGUMENT; - } + crt->ctx->aesctx->moo = mode; crt->ctx->aesctx->padding = PADDING; + if (mode != ID_DEC_ECB && mode != ID_DEC_CBC) - { SDRM_getEncRoundKey(crt->alg, key, crt->ctx->aesctx->RoundKey); - } + else - { SDRM_getDecRoundKey(crt->alg, key, crt->ctx->aesctx->RoundKey); - } + if (IV) - { memcpy(crt->ctx->aesctx->IV, IV, SDRM_AES_BLOCK_SIZ); - } + else - { memset(crt->ctx->aesctx->IV, 0x00, SDRM_AES_BLOCK_SIZ); - } + crt->ctx->aesctx->BlockLen = 0; GET_UINT32(crt->ctx->aesctx->CTR_Count, crt->ctx->aesctx->IV + 12, 0); @@ -191,20 +193,21 @@ int SDRM_AES_init(CryptoCoreContainer *crt, cc_u32 mode, cc_u32 PADDING, cc_u8 * } /* - * @fn SDRM_AES_process - * @brief process message block + * @fn SDRM_AES_process + * @brief process message block * - * @param crt [in]crypto env structure - * @param Text [in]message block - * @param TextLen [in]byte-length of Text - * @param output [out]proecessed message - * @param outputLen [out]byte-length of output + * @param crt [in]crypto env structure + * @param Text [in]message block + * @param TextLen [in]byte-length of Text + * @param output [out]proecessed message + * @param outputLen [out]byte-length of output * - * @return CRYPTO_SUCCESS if success - * \n CRYPTO_NULL_POINTER if given argument is a null pointer - * \n CRYPTO_INVALID_ARGUMENT if given argument is invalid + * @return CRYPTO_SUCCESS if success + * \n CRYPTO_NULL_POINTER if given argument is a null pointer + * \n CRYPTO_INVALID_ARGUMENT if given argument is invalid */ -int SDRM_AES_process(CryptoCoreContainer *crt, cc_u8 *Text, cc_u32 TextLen, cc_u8 *output, cc_u32 *outputLen) +int SDRM_AES_process(CryptoCoreContainer *crt, cc_u8 *Text, cc_u32 TextLen, + cc_u8 *output, cc_u32 *outputLen) { int i, Temp; int retVal, BlockLen; @@ -212,163 +215,192 @@ int SDRM_AES_process(CryptoCoreContainer *crt, cc_u8 *Text, cc_u32 TextLen, cc_u cc_u32 tempLen = 0; if (outputLen != NULL) - { *outputLen = 0; - } if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->aesctx == NULL)) - { return CRYPTO_NULL_POINTER; - } Block = crt->ctx->aesctx->Block; BlockLen = crt->ctx->aesctx->BlockLen; - if ((TextLen + BlockLen) < SDRM_AES_BLOCK_SIZ) - { + if ((TextLen + BlockLen) < SDRM_AES_BLOCK_SIZ) { memcpy(Block + BlockLen, Text, TextLen); crt->ctx->aesctx->BlockLen += TextLen; return CRYPTO_SUCCESS; } - if (BlockLen) - { + if (BlockLen) { memcpy(Block + BlockLen, Text, SDRM_AES_BLOCK_SIZ - BlockLen); - switch(crt->ctx->aesctx->moo) - { - case ID_ENC_ECB : - retVal = SDRM_ECB_Enc(crt->alg, output, Block, crt->ctx->aesctx->RoundKey); - tempLen += SDRM_AES_BLOCK_SIZ; - break; - case ID_ENC_CBC : - retVal = SDRM_CBC_Enc(crt->alg, output, Block, crt->ctx->aesctx->RoundKey, crt->ctx->aesctx->IV); - tempLen += SDRM_AES_BLOCK_SIZ; - break; - case ID_ENC_CFB : - retVal = SDRM_CFB_Enc(crt->alg, output, Block, crt->ctx->aesctx->RoundKey, crt->ctx->aesctx->IV); - tempLen += SDRM_AES_BLOCK_SIZ; - break; - case ID_ENC_OFB : - retVal = SDRM_OFB_Enc(crt->alg, output, Block, crt->ctx->aesctx->RoundKey, crt->ctx->aesctx->IV); - tempLen += SDRM_AES_BLOCK_SIZ; - break; - case ID_ENC_CTR : - retVal = SDRM_CTR_Enc(crt->alg, output, Block, crt->ctx->aesctx->RoundKey, crt->ctx->aesctx->IV, crt->ctx->aesctx->CTR_Count++); - tempLen += SDRM_AES_BLOCK_SIZ; - break; - case ID_DEC_ECB : - retVal = SDRM_ECB_Dec(crt->alg, output, Block, crt->ctx->aesctx->RoundKey); - tempLen += SDRM_AES_BLOCK_SIZ; - break; - case ID_DEC_CBC : - retVal = SDRM_CBC_Dec(crt->alg, output, Block, crt->ctx->aesctx->RoundKey, crt->ctx->aesctx->IV); - tempLen += SDRM_AES_BLOCK_SIZ; - break; - case ID_DEC_CFB : - retVal = SDRM_CFB_Dec(crt->alg, output, Block, crt->ctx->aesctx->RoundKey, crt->ctx->aesctx->IV); - tempLen += SDRM_AES_BLOCK_SIZ; - break; - case ID_DEC_OFB : - retVal = SDRM_OFB_Dec(crt->alg, output, Block, crt->ctx->aesctx->RoundKey, crt->ctx->aesctx->IV); - tempLen += SDRM_AES_BLOCK_SIZ; - break; - case ID_DEC_CTR : - retVal = SDRM_CTR_Dec(crt->alg, output, Block, crt->ctx->aesctx->RoundKey, crt->ctx->aesctx->IV, crt->ctx->aesctx->CTR_Count++); - tempLen += SDRM_AES_BLOCK_SIZ; - break; - default : - return CRYPTO_INVALID_ARGUMENT; + switch (crt->ctx->aesctx->moo) { + case ID_ENC_ECB: + retVal = SDRM_ECB_Enc(crt->alg, output, Block, crt->ctx->aesctx->RoundKey); + tempLen += SDRM_AES_BLOCK_SIZ; + break; + + case ID_ENC_CBC: + retVal = SDRM_CBC_Enc(crt->alg, output, Block, crt->ctx->aesctx->RoundKey, + crt->ctx->aesctx->IV); + tempLen += SDRM_AES_BLOCK_SIZ; + break; + + case ID_ENC_CFB: + retVal = SDRM_CFB_Enc(crt->alg, output, Block, crt->ctx->aesctx->RoundKey, + crt->ctx->aesctx->IV); + tempLen += SDRM_AES_BLOCK_SIZ; + break; + + case ID_ENC_OFB: + retVal = SDRM_OFB_Enc(crt->alg, output, Block, crt->ctx->aesctx->RoundKey, + crt->ctx->aesctx->IV); + tempLen += SDRM_AES_BLOCK_SIZ; + break; + + case ID_ENC_CTR: + retVal = SDRM_CTR_Enc(crt->alg, output, Block, crt->ctx->aesctx->RoundKey, + crt->ctx->aesctx->IV, crt->ctx->aesctx->CTR_Count++); + tempLen += SDRM_AES_BLOCK_SIZ; + break; + + case ID_DEC_ECB: + retVal = SDRM_ECB_Dec(crt->alg, output, Block, crt->ctx->aesctx->RoundKey); + tempLen += SDRM_AES_BLOCK_SIZ; + break; + + case ID_DEC_CBC: + retVal = SDRM_CBC_Dec(crt->alg, output, Block, crt->ctx->aesctx->RoundKey, + crt->ctx->aesctx->IV); + tempLen += SDRM_AES_BLOCK_SIZ; + break; + + case ID_DEC_CFB: + retVal = SDRM_CFB_Dec(crt->alg, output, Block, crt->ctx->aesctx->RoundKey, + crt->ctx->aesctx->IV); + tempLen += SDRM_AES_BLOCK_SIZ; + break; + + case ID_DEC_OFB: + retVal = SDRM_OFB_Dec(crt->alg, output, Block, crt->ctx->aesctx->RoundKey, + crt->ctx->aesctx->IV); + tempLen += SDRM_AES_BLOCK_SIZ; + break; + + case ID_DEC_CTR: + retVal = SDRM_CTR_Dec(crt->alg, output, Block, crt->ctx->aesctx->RoundKey, + crt->ctx->aesctx->IV, crt->ctx->aesctx->CTR_Count++); + tempLen += SDRM_AES_BLOCK_SIZ; + break; + + default: + return CRYPTO_INVALID_ARGUMENT; } if (retVal != CRYPTO_SUCCESS) - { return retVal; - } } Temp = TextLen - SDRM_AES_BLOCK_SIZ + 1; - for (i = (SDRM_AES_BLOCK_SIZ - BlockLen) & 0x0f; i < Temp; i += SDRM_AES_BLOCK_SIZ) - { - switch(crt->ctx->aesctx->moo) - { - case ID_ENC_ECB : - retVal = SDRM_ECB_Enc(crt->alg, output + tempLen, Text + i, crt->ctx->aesctx->RoundKey); - tempLen += SDRM_AES_BLOCK_SIZ; - break; - case ID_ENC_CBC : - retVal = SDRM_CBC_Enc(crt->alg, output + tempLen, Text + i, crt->ctx->aesctx->RoundKey, crt->ctx->aesctx->IV); - tempLen += SDRM_AES_BLOCK_SIZ; - break; - case ID_ENC_CFB : - retVal = SDRM_CFB_Enc(crt->alg, output + tempLen, Text + i, crt->ctx->aesctx->RoundKey, crt->ctx->aesctx->IV); - tempLen += SDRM_AES_BLOCK_SIZ; - break; - case ID_ENC_OFB : - retVal = SDRM_OFB_Enc(crt->alg, output + tempLen, Text + i, crt->ctx->aesctx->RoundKey, crt->ctx->aesctx->IV); - tempLen += SDRM_AES_BLOCK_SIZ; - break; - case ID_ENC_CTR : - retVal = SDRM_CTR_Enc(crt->alg, output + tempLen, Text + i, crt->ctx->aesctx->RoundKey, crt->ctx->aesctx->IV, crt->ctx->aesctx->CTR_Count++); - tempLen += SDRM_AES_BLOCK_SIZ; - break; - case ID_DEC_ECB : - retVal = SDRM_ECB_Dec(crt->alg, output + tempLen, Text + i, crt->ctx->aesctx->RoundKey); - tempLen += SDRM_AES_BLOCK_SIZ; - break; - case ID_DEC_CBC : - retVal = SDRM_CBC_Dec(crt->alg, output + tempLen, Text + i, crt->ctx->aesctx->RoundKey, crt->ctx->aesctx->IV); - tempLen += SDRM_AES_BLOCK_SIZ; - break; - case ID_DEC_CFB : - retVal = SDRM_CFB_Dec(crt->alg, output + tempLen, Text + i, crt->ctx->aesctx->RoundKey, crt->ctx->aesctx->IV); - tempLen += SDRM_AES_BLOCK_SIZ; - break; - case ID_DEC_OFB : - retVal = SDRM_OFB_Dec(crt->alg, output + tempLen, Text + i, crt->ctx->aesctx->RoundKey, crt->ctx->aesctx->IV); - tempLen += SDRM_AES_BLOCK_SIZ; - break; - case ID_DEC_CTR : - retVal = SDRM_CTR_Dec(crt->alg, output + tempLen, Text + i, crt->ctx->aesctx->RoundKey, crt->ctx->aesctx->IV, crt->ctx->aesctx->CTR_Count++); - tempLen += SDRM_AES_BLOCK_SIZ; - break; - default : - return CRYPTO_INVALID_ARGUMENT; + + for (i = (SDRM_AES_BLOCK_SIZ - BlockLen) & 0x0f; i < Temp; + i += SDRM_AES_BLOCK_SIZ) { + switch (crt->ctx->aesctx->moo) { + case ID_ENC_ECB: + retVal = SDRM_ECB_Enc(crt->alg, output + tempLen, Text + i, + crt->ctx->aesctx->RoundKey); + tempLen += SDRM_AES_BLOCK_SIZ; + break; + + case ID_ENC_CBC: + retVal = SDRM_CBC_Enc(crt->alg, output + tempLen, Text + i, + crt->ctx->aesctx->RoundKey, crt->ctx->aesctx->IV); + tempLen += SDRM_AES_BLOCK_SIZ; + break; + + case ID_ENC_CFB: + retVal = SDRM_CFB_Enc(crt->alg, output + tempLen, Text + i, + crt->ctx->aesctx->RoundKey, crt->ctx->aesctx->IV); + tempLen += SDRM_AES_BLOCK_SIZ; + break; + + case ID_ENC_OFB: + retVal = SDRM_OFB_Enc(crt->alg, output + tempLen, Text + i, + crt->ctx->aesctx->RoundKey, crt->ctx->aesctx->IV); + tempLen += SDRM_AES_BLOCK_SIZ; + break; + + case ID_ENC_CTR: + retVal = SDRM_CTR_Enc(crt->alg, output + tempLen, Text + i, + crt->ctx->aesctx->RoundKey, crt->ctx->aesctx->IV, + crt->ctx->aesctx->CTR_Count++); + tempLen += SDRM_AES_BLOCK_SIZ; + break; + + case ID_DEC_ECB: + retVal = SDRM_ECB_Dec(crt->alg, output + tempLen, Text + i, + crt->ctx->aesctx->RoundKey); + tempLen += SDRM_AES_BLOCK_SIZ; + break; + + case ID_DEC_CBC: + retVal = SDRM_CBC_Dec(crt->alg, output + tempLen, Text + i, + crt->ctx->aesctx->RoundKey, crt->ctx->aesctx->IV); + tempLen += SDRM_AES_BLOCK_SIZ; + break; + + case ID_DEC_CFB: + retVal = SDRM_CFB_Dec(crt->alg, output + tempLen, Text + i, + crt->ctx->aesctx->RoundKey, crt->ctx->aesctx->IV); + tempLen += SDRM_AES_BLOCK_SIZ; + break; + + case ID_DEC_OFB: + retVal = SDRM_OFB_Dec(crt->alg, output + tempLen, Text + i, + crt->ctx->aesctx->RoundKey, crt->ctx->aesctx->IV); + tempLen += SDRM_AES_BLOCK_SIZ; + break; + + case ID_DEC_CTR: + retVal = SDRM_CTR_Dec(crt->alg, output + tempLen, Text + i, + crt->ctx->aesctx->RoundKey, crt->ctx->aesctx->IV, + crt->ctx->aesctx->CTR_Count++); + tempLen += SDRM_AES_BLOCK_SIZ; + break; + + default: + return CRYPTO_INVALID_ARGUMENT; } if (retVal != CRYPTO_SUCCESS) - { return retVal; - } } crt->ctx->aesctx->BlockLen = (SDRM_AES_BLOCK_SIZ + TextLen - i) & 0x0f; - memcpy(Block, Text + TextLen - crt->ctx->aesctx->BlockLen, crt->ctx->aesctx->BlockLen); + memcpy(Block, Text + TextLen - crt->ctx->aesctx->BlockLen, + crt->ctx->aesctx->BlockLen); if (outputLen != 0) - { *outputLen = tempLen; - } return CRYPTO_SUCCESS; } /* - * @fn SDRM_AES_final - * @brief process final block and padding + * @fn SDRM_AES_final + * @brief process final block and padding * - * @param crt [in]crypto env structure - * @param input [in]message block - * @param inputLen [in]byte-length of Text - * @param output [out]processed message - * @param outputLen [out]byte-length of output + * @param crt [in]crypto env structure + * @param input [in]message block + * @param inputLen [in]byte-length of Text + * @param output [out]processed message + * @param outputLen [out]byte-length of output * - * @return CRYPTO_SUCCESS if success - * \n CRYPTO_NULL_POINTER if given argument is a null pointer - * \n CRYPTO_INVALID_ARGUMENT if given argument is invalid + * @return CRYPTO_SUCCESS if success + * \n CRYPTO_NULL_POINTER if given argument is a null pointer + * \n CRYPTO_INVALID_ARGUMENT if given argument is invalid */ -int SDRM_AES_final(CryptoCoreContainer *crt, cc_u8 *input, cc_u32 inputLen, cc_u8 *output, cc_u32 *outputLen) +int SDRM_AES_final(CryptoCoreContainer *crt, cc_u8 *input, cc_u32 inputLen, + cc_u8 *output, cc_u32 *outputLen) { int retVal = CRYPTO_SUCCESS; cc_u8 *Block, PADDING[16]; @@ -376,268 +408,256 @@ int SDRM_AES_final(CryptoCoreContainer *crt, cc_u8 *input, cc_u32 inputLen, cc_u cc_u8 t; if (outputLen != NULL) - { *outputLen = 0; - } if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->aesctx == NULL)) - { return CRYPTO_NULL_POINTER; - } Block = crt->ctx->aesctx->Block; BlockLen = crt->ctx->aesctx->BlockLen; if (crt->ctx->aesctx->moo >= ID_DEC_ECB) - { goto DECRYPTION; - } -//ENCRYPTION: - if (inputLen != 0) - { + //ENCRYPTION: + if (inputLen != 0) { unsigned int temp; retVal = SDRM_AES_process(crt, input, inputLen, output, &temp); if (retVal != CRYPTO_SUCCESS) - { return retVal; - } retVal = SDRM_AES_final(crt, NULL, 0, output + temp, outputLen); if (outputLen) - { *outputLen += temp; - } return retVal; } if (outputLen != NULL) - { *outputLen = SDRM_AES_BLOCK_SIZ; - } //padding - switch(crt->ctx->aesctx->padding) - { - case 0 : - case ID_PKCS5 : - memset(Block + BlockLen, SDRM_AES_BLOCK_SIZ - BlockLen, SDRM_AES_BLOCK_SIZ - BlockLen); - break; - case ID_SSL_PADDING : - memset(Block + BlockLen, SDRM_AES_BLOCK_SIZ - BlockLen - 1, SDRM_AES_BLOCK_SIZ - BlockLen); - break; - case ID_ZERO_PADDING : - memset(Block + BlockLen, 0x00, SDRM_AES_BLOCK_SIZ - BlockLen); - break; - case ID_NO_PADDING : - if (BlockLen == 0) - { - if (outputLen) - { - *outputLen = 0; - } - return CRYPTO_SUCCESS; - } - break; - default : - return CRYPTO_INVALID_ARGUMENT; + switch (crt->ctx->aesctx->padding) { + case 0: + case ID_PKCS5: + memset(Block + BlockLen, SDRM_AES_BLOCK_SIZ - BlockLen, + SDRM_AES_BLOCK_SIZ - BlockLen); + break; + + case ID_SSL_PADDING: + memset(Block + BlockLen, SDRM_AES_BLOCK_SIZ - BlockLen - 1, + SDRM_AES_BLOCK_SIZ - BlockLen); + break; + + case ID_ZERO_PADDING: + memset(Block + BlockLen, 0x00, SDRM_AES_BLOCK_SIZ - BlockLen); + break; + + case ID_NO_PADDING: + if (BlockLen == 0) { + if (outputLen) + *outputLen = 0; + + return CRYPTO_SUCCESS; + } + + break; + + default: + return CRYPTO_INVALID_ARGUMENT; } //encryption - switch(crt->ctx->aesctx->moo) - { - case ID_ENC_ECB : - retVal = SDRM_ECB_Enc(crt->alg, output, Block, crt->ctx->aesctx->RoundKey); - break; - case ID_ENC_CBC : - retVal = SDRM_CBC_Enc(crt->alg, output, Block, crt->ctx->aesctx->RoundKey, crt->ctx->aesctx->IV); - break; - case ID_ENC_CFB : - retVal = SDRM_CFB_Enc(crt->alg, output, Block, crt->ctx->aesctx->RoundKey, crt->ctx->aesctx->IV); - break; - case ID_ENC_OFB : - retVal = SDRM_OFB_Enc(crt->alg, output, Block, crt->ctx->aesctx->RoundKey, crt->ctx->aesctx->IV); - break; - case ID_ENC_CTR : - retVal = SDRM_CTR_Enc(crt->alg, Block, Block, crt->ctx->aesctx->RoundKey, crt->ctx->aesctx->IV, crt->ctx->aesctx->CTR_Count++); - if(crt->ctx->aesctx->padding != ID_NO_PADDING)// add by xugx to support padding - { - BlockLen = SDRM_AES_BLOCK_SIZ; - } - memcpy(output, Block, BlockLen); - if(outputLen != NULL) - { - *outputLen = BlockLen; - } - break; - default : - retVal = CRYPTO_INVALID_ARGUMENT; - break; + switch (crt->ctx->aesctx->moo) { + case ID_ENC_ECB: + retVal = SDRM_ECB_Enc(crt->alg, output, Block, crt->ctx->aesctx->RoundKey); + break; + + case ID_ENC_CBC: + retVal = SDRM_CBC_Enc(crt->alg, output, Block, crt->ctx->aesctx->RoundKey, + crt->ctx->aesctx->IV); + break; + + case ID_ENC_CFB: + retVal = SDRM_CFB_Enc(crt->alg, output, Block, crt->ctx->aesctx->RoundKey, + crt->ctx->aesctx->IV); + break; + + case ID_ENC_OFB: + retVal = SDRM_OFB_Enc(crt->alg, output, Block, crt->ctx->aesctx->RoundKey, + crt->ctx->aesctx->IV); + break; + + case ID_ENC_CTR: + retVal = SDRM_CTR_Enc(crt->alg, Block, Block, crt->ctx->aesctx->RoundKey, + crt->ctx->aesctx->IV, crt->ctx->aesctx->CTR_Count++); + + if (crt->ctx->aesctx->padding != + ID_NO_PADDING) // add by xugx to support padding + BlockLen = SDRM_AES_BLOCK_SIZ; + + memcpy(output, Block, BlockLen); + + if (outputLen != NULL) + *outputLen = BlockLen; + + break; + + default: + retVal = CRYPTO_INVALID_ARGUMENT; + break; } return retVal; DECRYPTION: + if (outputLen != NULL) - { *outputLen = 0; - } - if ((inputLen == 0) && (crt->ctx->aesctx->padding == ID_NO_PADDING) && (crt->ctx->aesctx->moo != ID_DEC_CTR)) - { + if ((inputLen == 0) && (crt->ctx->aesctx->padding == ID_NO_PADDING) && + (crt->ctx->aesctx->moo != ID_DEC_CTR)) return CRYPTO_SUCCESS; - } - if (((BlockLen + inputLen) != SDRM_AES_BLOCK_SIZ) && (crt->ctx->aesctx->moo != ID_DEC_CTR)) - { + if (((BlockLen + inputLen) != SDRM_AES_BLOCK_SIZ) && + (crt->ctx->aesctx->moo != ID_DEC_CTR)) return CRYPTO_INVALID_ARGUMENT; - } if (inputLen != 0) - { memcpy(Block + BlockLen, input, inputLen); - } - switch(crt->ctx->aesctx->moo) - { - case ID_DEC_ECB : - retVal = SDRM_ECB_Dec(crt->alg, Block, Block, crt->ctx->aesctx->RoundKey); - break; - case ID_DEC_CBC : - retVal = SDRM_CBC_Dec(crt->alg, Block, Block, crt->ctx->aesctx->RoundKey, crt->ctx->aesctx->IV); - break; - case ID_DEC_CFB : - retVal = SDRM_CFB_Dec(crt->alg, Block, Block, crt->ctx->aesctx->RoundKey, crt->ctx->aesctx->IV); - break; - case ID_DEC_OFB : - retVal = SDRM_OFB_Dec(crt->alg, Block, Block, crt->ctx->aesctx->RoundKey, crt->ctx->aesctx->IV); - break; - case ID_DEC_CTR : - retVal = SDRM_CTR_Dec(crt->alg, Block, Block, crt->ctx->aesctx->RoundKey, crt->ctx->aesctx->IV, crt->ctx->aesctx->CTR_Count++); - break; - default : - return CRYPTO_INVALID_ARGUMENT; + switch (crt->ctx->aesctx->moo) { + case ID_DEC_ECB: + retVal = SDRM_ECB_Dec(crt->alg, Block, Block, crt->ctx->aesctx->RoundKey); + break; + + case ID_DEC_CBC: + retVal = SDRM_CBC_Dec(crt->alg, Block, Block, crt->ctx->aesctx->RoundKey, + crt->ctx->aesctx->IV); + break; + + case ID_DEC_CFB: + retVal = SDRM_CFB_Dec(crt->alg, Block, Block, crt->ctx->aesctx->RoundKey, + crt->ctx->aesctx->IV); + break; + + case ID_DEC_OFB: + retVal = SDRM_OFB_Dec(crt->alg, Block, Block, crt->ctx->aesctx->RoundKey, + crt->ctx->aesctx->IV); + break; + + case ID_DEC_CTR: + retVal = SDRM_CTR_Dec(crt->alg, Block, Block, crt->ctx->aesctx->RoundKey, + crt->ctx->aesctx->IV, crt->ctx->aesctx->CTR_Count++); + break; + + default: + return CRYPTO_INVALID_ARGUMENT; } if (retVal != CRYPTO_SUCCESS) - { return retVal; - } //de-padding t = Block[SDRM_AES_BLOCK_SIZ - 1]; - switch(crt->ctx->aesctx->padding) - { - case 0 : - case ID_PKCS5 : - if ((t > SDRM_AES_BLOCK_SIZ) || (t < 1)) - { - return CRYPTO_INVALID_ARGUMENT; - } - memset(PADDING, t, t); - break; - case ID_SSL_PADDING : - ++t; - if ((t > SDRM_AES_BLOCK_SIZ) || (t < 1)) - { - return CRYPTO_INVALID_ARGUMENT; - } - memset(PADDING, t - 1, t); - break; - case ID_ZERO_PADDING : - { - cc_u32 tmpLen; - tmpLen = SDRM_AES_BLOCK_SIZ; - while((tmpLen != 0x00) && (Block[tmpLen - 1] == 0x00)) - { - tmpLen--; - } - - memcpy(output, Block, tmpLen); - - if (outputLen != NULL) - { - *outputLen = tmpLen; - } - } - return CRYPTO_SUCCESS; - case ID_NO_PADDING : - { - cc_u32 tmpLen; - tmpLen = SDRM_AES_BLOCK_SIZ; - - if (crt->ctx->aesctx->moo == ID_DEC_CTR) - { - tmpLen = BlockLen + inputLen; - } - else - { - tmpLen = SDRM_AES_BLOCK_SIZ; - } - - memcpy(output, Block, tmpLen); - - if (outputLen != NULL) - { - *outputLen = tmpLen; - } - } - return CRYPTO_SUCCESS; - default : - if (outputLen != NULL) - { - *outputLen = 0; - } + switch (crt->ctx->aesctx->padding) { + case 0: + case ID_PKCS5: + if ((t > SDRM_AES_BLOCK_SIZ) || (t < 1)) return CRYPTO_INVALID_ARGUMENT; + + memset(PADDING, t, t); + break; + + case ID_SSL_PADDING: + ++t; + + if ((t > SDRM_AES_BLOCK_SIZ) || (t < 1)) + return CRYPTO_INVALID_ARGUMENT; + + memset(PADDING, t - 1, t); + break; + + case ID_ZERO_PADDING: { + cc_u32 tmpLen; + tmpLen = SDRM_AES_BLOCK_SIZ; + + while ((tmpLen != 0x00) && (Block[tmpLen - 1] == 0x00)) + tmpLen--; + + memcpy(output, Block, tmpLen); + + if (outputLen != NULL) + *outputLen = tmpLen; } - if (memcmp(PADDING, Block + SDRM_AES_BLOCK_SIZ - t, t) != 0) - { + return CRYPTO_SUCCESS; + + case ID_NO_PADDING: { + cc_u32 tmpLen; + tmpLen = SDRM_AES_BLOCK_SIZ; + + if (crt->ctx->aesctx->moo == ID_DEC_CTR) + tmpLen = BlockLen + inputLen; + + else + tmpLen = SDRM_AES_BLOCK_SIZ; + + memcpy(output, Block, tmpLen); + + if (outputLen != NULL) + *outputLen = tmpLen; + } + + return CRYPTO_SUCCESS; + + default: + if (outputLen != NULL) + *outputLen = 0; + return CRYPTO_INVALID_ARGUMENT; } - memcpy(output, Block, SDRM_AES_BLOCK_SIZ -t); + if (memcmp(PADDING, Block + SDRM_AES_BLOCK_SIZ - t, t) != 0) + return CRYPTO_INVALID_ARGUMENT; + + memcpy(output, Block, SDRM_AES_BLOCK_SIZ - t); if (outputLen != NULL) - { *outputLen = SDRM_AES_BLOCK_SIZ - t; - } return CRYPTO_SUCCESS; } /* - * @fn SDRM_RC4_init - * @brief intialize crypt context for RC4 + * @fn SDRM_RC4_init + * @brief intialize crypt context for RC4 * - * @param crt [out]crypto env structure - * @param mode [in]encryption|decryption and mode of operation - * @param PADDING [in]padding method, not needed - * @param key [in]user key - * @param keysize [in]byte-length of key - * @param IV [in]initial vector, not needed + * @param crt [out]crypto env structure + * @param mode [in]encryption|decryption and mode of operation + * @param PADDING [in]padding method, not needed + * @param key [in]user key + * @param keysize [in]byte-length of key + * @param IV [in]initial vector, not needed * - * @return CRYPTO_SUCCESS if success - * \n CRYPTO_NULL_POINTER if given argument is a null pointer - * \n CRYPTO_INVALID_ARGUMENT if given argument is invalid + * @return CRYPTO_SUCCESS if success + * \n CRYPTO_NULL_POINTER if given argument is a null pointer + * \n CRYPTO_INVALID_ARGUMENT if given argument is invalid */ -int SDRM_RC4_init(CryptoCoreContainer *crt, cc_u32 mode, cc_u32 PADDING, cc_u8 *key, cc_u32 keysize, cc_u8 *IV) +int SDRM_RC4_init(CryptoCoreContainer *crt, cc_u32 mode, cc_u32 PADDING, + cc_u8 *key, cc_u32 keysize, cc_u8 *IV) { - if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->rc4ctx == NULL) || (key == NULL)) - { + if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->rc4ctx == NULL) || + (key == NULL)) return CRYPTO_NULL_POINTER; - } if (keysize > 32) - { return CRYPTO_INVALID_ARGUMENT; - } SDRM_RC4_Setup(crt->ctx->rc4ctx, key, keysize); @@ -645,64 +665,60 @@ int SDRM_RC4_init(CryptoCoreContainer *crt, cc_u32 mode, cc_u32 PADDING, cc_u8 * } /* - * @fn SDRM_RC4_process - * @brief process message block + * @fn SDRM_RC4_process + * @brief process message block * - * @param crt [in]crypto env structure - * @param in [in]message block - * @param inLen [in]byte-length of Text - * @param out [out]processed message - * @param outLen [out]byte-length of output + * @param crt [in]crypto env structure + * @param in [in]message block + * @param inLen [in]byte-length of Text + * @param out [out]processed message + * @param outLen [out]byte-length of output * - * @return CRYPTO_SUCCESS if success - * \n CRYPTO_NULL_POINTER if given argument is a null pointer - * \n CRYPTO_INVALID_ARGUMENT if given argument is invalid + * @return CRYPTO_SUCCESS if success + * \n CRYPTO_NULL_POINTER if given argument is a null pointer + * \n CRYPTO_INVALID_ARGUMENT if given argument is invalid */ -int SDRM_RC4_process(CryptoCoreContainer *crt, cc_u8 *in, cc_u32 inLen, cc_u8 *out, cc_u32 *outLen) +int SDRM_RC4_process(CryptoCoreContainer *crt, cc_u8 *in, cc_u32 inLen, + cc_u8 *out, cc_u32 *outLen) { - if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->rc4ctx == NULL) || (in == NULL) || (out == NULL)) - { + if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->rc4ctx == NULL) || + (in == NULL) || (out == NULL)) return CRYPTO_NULL_POINTER; - } SDRM_RC4_PRNG(crt->ctx->rc4ctx, in, inLen, out); if (outLen != NULL) - { *outLen = inLen; - } return CRYPTO_SUCCESS; } /* - * @fn SDRM_SNOW2_init - * @brief intialize crypt context for SNOW2 + * @fn SDRM_SNOW2_init + * @brief intialize crypt context for SNOW2 * - * @param crt [out]crypto env structure - * @param mode [in]encryption|decryption and mode of operation - * @param PADDING [in]padding method, not needed - * @param key [in]user key - * @param keysize [in]byte-length of key - * @param IV [in]initial vector + * @param crt [out]crypto env structure + * @param mode [in]encryption|decryption and mode of operation + * @param PADDING [in]padding method, not needed + * @param key [in]user key + * @param keysize [in]byte-length of key + * @param IV [in]initial vector * - * @return CRYPTO_SUCCESS if success - * \n CRYPTO_NULL_POINTER if given argument is a null pointer - * \n CRYPTO_INVALID_ARGUMENT if given argument is invalid + * @return CRYPTO_SUCCESS if success + * \n CRYPTO_NULL_POINTER if given argument is a null pointer + * \n CRYPTO_INVALID_ARGUMENT if given argument is invalid */ -int SDRM_SNOW2_init(CryptoCoreContainer *crt, cc_u32 mode, cc_u32 PADDING, cc_u8 *key, cc_u32 keysize, cc_u8 *IV) +int SDRM_SNOW2_init(CryptoCoreContainer *crt, cc_u32 mode, cc_u32 PADDING, + cc_u8 *key, cc_u32 keysize, cc_u8 *IV) { - if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->snow2ctx == NULL) || (key == NULL) || (IV == NULL)) - { + if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->snow2ctx == NULL) || + (key == NULL) || (IV == NULL)) return CRYPTO_NULL_POINTER; - } if ((keysize != 16) && (keysize != 32)) - { return CRYPTO_INVALID_ARGUMENT; - } SDRM_SNOW2_Setup(crt->ctx->snow2ctx, key, keysize, IV); @@ -710,62 +726,54 @@ int SDRM_SNOW2_init(CryptoCoreContainer *crt, cc_u32 mode, cc_u32 PADDING, cc_u8 } /* - * @fn SDRM_SNOW2_process - * @brief process message block + * @fn SDRM_SNOW2_process + * @brief process message block * - * @param crt [in]crypto env structure - * @param in [in]message block - * @param inLen [in]byte-length of Text - * @param out [out]processed message - * @param outLen [out]byte-length of output + * @param crt [in]crypto env structure + * @param in [in]message block + * @param inLen [in]byte-length of Text + * @param out [out]processed message + * @param outLen [out]byte-length of output * - * @return CRYPTO_SUCCESS if success - * \n CRYPTO_NULL_POINTER if given argument is a null pointer - * \n CRYPTO_INVALID_ARGUMENT if given argument is invalid + * @return CRYPTO_SUCCESS if success + * \n CRYPTO_NULL_POINTER if given argument is a null pointer + * \n CRYPTO_INVALID_ARGUMENT if given argument is invalid */ -int SDRM_SNOW2_process(CryptoCoreContainer *crt, cc_u8 *in, cc_u32 inLen, cc_u8 *out, cc_u32 *outLen) +int SDRM_SNOW2_process(CryptoCoreContainer *crt, cc_u8 *in, cc_u32 inLen, + cc_u8 *out, cc_u32 *outLen) { cc_u32 i, j, BlockLen, rpt, loc; cc_u32 keyStream64[16], keyStream; - if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->snow2ctx == NULL) || (in == NULL) || (out == NULL)) - { + if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->snow2ctx == NULL) || + (in == NULL) || (out == NULL)) return CRYPTO_NULL_POINTER; - } if ((inLen & 0x03) != 0) - { return CRYPTO_INVALID_ARGUMENT; - } BlockLen = inLen / 64; - if (crt->ctx->snow2ctx->endian == CRYPTO_LITTLE_ENDIAN) - { //little endian machine - for (i = 0; i < BlockLen; i++) - { + if (crt->ctx->snow2ctx->endian == CRYPTO_LITTLE_ENDIAN) { + //little endian machine + for (i = 0; i < BlockLen; i++) { SDRM_SNOW2_getKeyStream64(crt->ctx->snow2ctx, keyStream64); - for (j = 0; j < 16; j++) - { + for (j = 0; j < 16; j++) { loc = i * 64 + j * 4; - out[loc ] = (cc_u8)(in[loc ] ^ ((keyStream64[j] >> 24) & 0xff)); + out[loc] = (cc_u8)(in[loc] ^ ((keyStream64[j] >> 24) & 0xff)); out[loc + 1] = (cc_u8)(in[loc + 1] ^ ((keyStream64[j] >> 16) & 0xff)); out[loc + 2] = (cc_u8)(in[loc + 2] ^ ((keyStream64[j] >> 8) & 0xff)); - out[loc + 3] = (cc_u8)(in[loc + 3] ^ ((keyStream64[j] ) & 0xff)); + out[loc + 3] = (cc_u8)(in[loc + 3] ^ ((keyStream64[j]) & 0xff)); } } - } - else - { //big endian machine - for (i = 0; i < BlockLen; i++) - { + } else { + //big endian machine + for (i = 0; i < BlockLen; i++) { SDRM_SNOW2_getKeyStream64(crt->ctx->snow2ctx, keyStream64); for (j = 0; j < 16; j++) - { - ((cc_u32*)(void*)out)[j] = ((cc_u32*)(void*)in)[j] ^ keyStream64[j]; - } + ((cc_u32 *)(void *)out)[j] = ((cc_u32 *)(void *)in)[j] ^ keyStream64[j]; } } @@ -774,80 +782,70 @@ int SDRM_SNOW2_process(CryptoCoreContainer *crt, cc_u8 *in, cc_u32 inLen, cc_u8 rpt = (inLen - (BlockLen * 64)) / 4; - if (crt->ctx->snow2ctx->endian == CRYPTO_LITTLE_ENDIAN) - { //little endian machine - for (i = 0; i < rpt; i++) - { + if (crt->ctx->snow2ctx->endian == CRYPTO_LITTLE_ENDIAN) { + //little endian machine + for (i = 0; i < rpt; i++) { SDRM_SNOW2_getKeyStream(crt->ctx->snow2ctx, &keyStream); loc = i * 4; - out[loc ] = (cc_u8)(in[loc ] ^ ((keyStream >> 24) & 0xff)); + out[loc] = (cc_u8)(in[loc] ^ ((keyStream >> 24) & 0xff)); out[loc + 1] = (cc_u8)(in[loc + 1] ^ ((keyStream >> 16) & 0xff)); out[loc + 2] = (cc_u8)(in[loc + 2] ^ ((keyStream >> 8) & 0xff)); - out[loc + 3] = (cc_u8)(in[loc + 3] ^ ((keyStream ) & 0xff)); + out[loc + 3] = (cc_u8)(in[loc + 3] ^ ((keyStream) & 0xff)); } - } - else - { //big endian machine - for (i = 0; i < rpt; i++) - { + } else { + //big endian machine + for (i = 0; i < rpt; i++) { SDRM_SNOW2_getKeyStream(crt->ctx->snow2ctx, &keyStream); - ((cc_u32*)(void*)out)[i] = ((cc_u32*)(void*)in)[i] ^ keyStream; + ((cc_u32 *)(void *)out)[i] = ((cc_u32 *)(void *)in)[i] ^ keyStream; } } if (outLen != NULL) - { *outLen = inLen; - } return CRYPTO_SUCCESS; } /* - * @fn SDRM_DES_init - * @brief intialize crypt context for des + * @fn SDRM_DES_init + * @brief intialize crypt context for des * - * @param crt [out]crypto env structure - * @param mode [in]encryption|decryption and mode of operation - * @param PADDING [in]padding method - * @param key [in]user key - * @param keysize [in]byte-length of key - * @param IV [in]initial vector + * @param crt [out]crypto env structure + * @param mode [in]encryption|decryption and mode of operation + * @param PADDING [in]padding method + * @param key [in]user key + * @param keysize [in]byte-length of key + * @param IV [in]initial vector * - * @return CRYPTO_SUCCESS if success - * \n CRYPTO_NULL_POINTER if given argument is a null pointer - * \n CRYPTO_INVALID_ARGUMENT if given argument is invalid + * @return CRYPTO_SUCCESS if success + * \n CRYPTO_NULL_POINTER if given argument is a null pointer + * \n CRYPTO_INVALID_ARGUMENT if given argument is invalid */ -int SDRM_DES_init(CryptoCoreContainer *crt, cc_u32 mode, cc_u32 PADDING, cc_u8 *key, cc_u32 keysize, cc_u8 *IV) +int SDRM_DES_init(CryptoCoreContainer *crt, cc_u32 mode, cc_u32 PADDING, + cc_u8 *key, cc_u32 keysize, cc_u8 *IV) { - if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->desctx == NULL) || (key == NULL)) - { + if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->desctx == NULL) || + (key == NULL)) return CRYPTO_NULL_POINTER; - } - if ((keysize != 8) || !(((mode >= 1111) && (mode <= 1115)) || ((mode >= 1121) && (mode <= 1125)))) - { + if ((keysize != 8) || !(((mode >= 1111) && (mode <= 1115)) || ((mode >= 1121) && + (mode <= 1125)))) return CRYPTO_INVALID_ARGUMENT; - } crt->ctx->desctx->moo = mode; - if ((PADDING != 0) && (PADDING != ID_PKCS5) && (PADDING != ID_SSL_PADDING) && (PADDING != ID_ZERO_PADDING) && (PADDING != ID_NO_PADDING)) - { + if ((PADDING != 0) && (PADDING != ID_PKCS5) && (PADDING != ID_SSL_PADDING) && + (PADDING != ID_ZERO_PADDING) && (PADDING != ID_NO_PADDING)) return CRYPTO_INVALID_ARGUMENT; - } crt->ctx->desctx->padding = PADDING; if (mode != ID_DEC_ECB && mode != ID_DEC_CBC) - { - SDRM_getEncRoundKey(ID_DES, key, (cc_u8*)(crt->ctx->desctx->RoundKey)); - } + SDRM_getEncRoundKey(ID_DES, key, (cc_u8 *)(crt->ctx->desctx->RoundKey)); + else - { - SDRM_getDecRoundKey(ID_DES, key, (cc_u8*)(crt->ctx->desctx->RoundKey)); - } + SDRM_getDecRoundKey(ID_DES, key, (cc_u8 *)(crt->ctx->desctx->RoundKey)); crt->ctx->desctx->BlockLen = 0; crt->ctx->desctx->CTR_Count = 0; @@ -855,213 +853,241 @@ int SDRM_DES_init(CryptoCoreContainer *crt, cc_u32 mode, cc_u32 PADDING, cc_u8 * memcpy(crt->ctx->desctx->UserKey, key, SDRM_DES_BLOCK_SIZ); if (IV) - { memcpy(crt->ctx->desctx->IV, IV, SDRM_DES_BLOCK_SIZ); - } + else - { memset(crt->ctx->desctx->IV, 0x00, SDRM_DES_BLOCK_SIZ); - } return CRYPTO_SUCCESS; } /* - * @fn SDRM_DES_process - * @brief process message block + * @fn SDRM_DES_process + * @brief process message block * - * @param crt [in]crypto env structure - * @param Text [in]message block - * @param TextLen [in]byte-length of Text - * @param output [out]proecessed message - * @param outputLen [out]byte-length of output + * @param crt [in]crypto env structure + * @param Text [in]message block + * @param TextLen [in]byte-length of Text + * @param output [out]proecessed message + * @param outputLen [out]byte-length of output * - * @return CRYPTO_SUCCESS if success - * \n CRYPTO_NULL_POINTER if given argument is a null pointer - * \n CRYPTO_INVALID_ARGUMENT if given argument is invalid + * @return CRYPTO_SUCCESS if success + * \n CRYPTO_NULL_POINTER if given argument is a null pointer + * \n CRYPTO_INVALID_ARGUMENT if given argument is invalid */ -int SDRM_DES_process(CryptoCoreContainer *crt, cc_u8 *Text, cc_u32 TextLen, cc_u8 *output, cc_u32 *outputLen) +int SDRM_DES_process(CryptoCoreContainer *crt, cc_u8 *Text, cc_u32 TextLen, + cc_u8 *output, cc_u32 *outputLen) { - int i, Temp; - int retVal, BlockLen; - cc_u8 *Block; + int i, Temp; + int retVal, BlockLen; + cc_u8 *Block; if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->desctx == NULL)) - { return CRYPTO_NULL_POINTER; - } Block = crt->ctx->desctx->Block; BlockLen = crt->ctx->desctx->BlockLen; *outputLen = 0; - if ((TextLen + BlockLen) < SDRM_DES_BLOCK_SIZ) - { + if ((TextLen + BlockLen) < SDRM_DES_BLOCK_SIZ) { memcpy(Block + BlockLen, Text, TextLen); crt->ctx->desctx->BlockLen += TextLen; return CRYPTO_SUCCESS; } - if (BlockLen) - { + if (BlockLen) { memcpy(Block + BlockLen, Text, SDRM_DES_BLOCK_SIZ - BlockLen); - switch(crt->ctx->desctx->moo) - { - case ID_ENC_ECB : - retVal = SDRM_ECB_Enc(ID_DES, output, Block, (cc_u8*)crt->ctx->desctx->RoundKey); - *outputLen += SDRM_DES_BLOCK_SIZ; - break; - case ID_ENC_CBC : - retVal = SDRM_CBC_Enc(ID_DES, output, Block, (cc_u8*)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV); - *outputLen += SDRM_DES_BLOCK_SIZ; - break; - case ID_ENC_CFB : - retVal = SDRM_CFB_Enc(ID_DES, output, Block, (cc_u8*)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV); - *outputLen += SDRM_DES_BLOCK_SIZ; - break; - case ID_ENC_OFB : - retVal = SDRM_OFB_Enc(ID_DES, output, Block, (cc_u8*)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV); - *outputLen += SDRM_DES_BLOCK_SIZ; - break; - case ID_ENC_CTR : - retVal = SDRM_CTR_Enc(ID_DES, output, Block, (cc_u8*)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV, crt->ctx->desctx->CTR_Count++); - *outputLen += SDRM_DES_BLOCK_SIZ; - break; - case ID_DEC_ECB : - retVal = SDRM_ECB_Dec(ID_DES, output, Block, (cc_u8*)crt->ctx->desctx->RoundKey); - *outputLen += SDRM_DES_BLOCK_SIZ; - break; - case ID_DEC_CBC : - retVal = SDRM_CBC_Dec(ID_DES, output, Block, (cc_u8*)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV); - *outputLen += SDRM_DES_BLOCK_SIZ; - break; - case ID_DEC_CFB : - retVal = SDRM_CFB_Dec(ID_DES, output, Block, (cc_u8*)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV); - *outputLen += SDRM_DES_BLOCK_SIZ; - break; - case ID_DEC_OFB : - retVal = SDRM_OFB_Dec(ID_DES, output, Block, (cc_u8*)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV); - *outputLen += SDRM_DES_BLOCK_SIZ; - break; - case ID_DEC_CTR : - retVal = SDRM_CTR_Dec(ID_DES, output, Block, (cc_u8*)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV, crt->ctx->desctx->CTR_Count++); - *outputLen += SDRM_DES_BLOCK_SIZ; - break; - default : - return CRYPTO_INVALID_ARGUMENT; + switch (crt->ctx->desctx->moo) { + case ID_ENC_ECB: + retVal = SDRM_ECB_Enc(ID_DES, output, Block, + (cc_u8 *)crt->ctx->desctx->RoundKey); + *outputLen += SDRM_DES_BLOCK_SIZ; + break; + + case ID_ENC_CBC: + retVal = SDRM_CBC_Enc(ID_DES, output, Block, + (cc_u8 *)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV); + *outputLen += SDRM_DES_BLOCK_SIZ; + break; + + case ID_ENC_CFB: + retVal = SDRM_CFB_Enc(ID_DES, output, Block, + (cc_u8 *)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV); + *outputLen += SDRM_DES_BLOCK_SIZ; + break; + + case ID_ENC_OFB: + retVal = SDRM_OFB_Enc(ID_DES, output, Block, + (cc_u8 *)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV); + *outputLen += SDRM_DES_BLOCK_SIZ; + break; + + case ID_ENC_CTR: + retVal = SDRM_CTR_Enc(ID_DES, output, Block, + (cc_u8 *)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV, + crt->ctx->desctx->CTR_Count++); + *outputLen += SDRM_DES_BLOCK_SIZ; + break; + + case ID_DEC_ECB: + retVal = SDRM_ECB_Dec(ID_DES, output, Block, + (cc_u8 *)crt->ctx->desctx->RoundKey); + *outputLen += SDRM_DES_BLOCK_SIZ; + break; + + case ID_DEC_CBC: + retVal = SDRM_CBC_Dec(ID_DES, output, Block, + (cc_u8 *)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV); + *outputLen += SDRM_DES_BLOCK_SIZ; + break; + + case ID_DEC_CFB: + retVal = SDRM_CFB_Dec(ID_DES, output, Block, + (cc_u8 *)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV); + *outputLen += SDRM_DES_BLOCK_SIZ; + break; + + case ID_DEC_OFB: + retVal = SDRM_OFB_Dec(ID_DES, output, Block, + (cc_u8 *)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV); + *outputLen += SDRM_DES_BLOCK_SIZ; + break; + + case ID_DEC_CTR: + retVal = SDRM_CTR_Dec(ID_DES, output, Block, + (cc_u8 *)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV, + crt->ctx->desctx->CTR_Count++); + *outputLen += SDRM_DES_BLOCK_SIZ; + break; + + default: + return CRYPTO_INVALID_ARGUMENT; } if (retVal != CRYPTO_SUCCESS) - { return retVal; - } } Temp = TextLen + BlockLen - SDRM_DES_BLOCK_SIZ + 1; - for (i = (SDRM_DES_BLOCK_SIZ - BlockLen) & 0x07; i < Temp; i += SDRM_DES_BLOCK_SIZ) - { - switch(crt->ctx->desctx->moo) - { - case ID_ENC_ECB : - retVal = SDRM_ECB_Enc(ID_DES, output + *outputLen, Text + i, (cc_u8*)crt->ctx->desctx->RoundKey); - *outputLen += SDRM_DES_BLOCK_SIZ; - break; - case ID_ENC_CBC : - retVal = SDRM_CBC_Enc(ID_DES, output + *outputLen, Text + i, (cc_u8*)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV); - *outputLen += SDRM_DES_BLOCK_SIZ; - break; - case ID_ENC_CFB : - retVal = SDRM_CFB_Enc(ID_DES, output + *outputLen, Text + i, (cc_u8*)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV); - *outputLen += SDRM_DES_BLOCK_SIZ; - break; - case ID_ENC_OFB : - retVal = SDRM_OFB_Enc(ID_DES, output + *outputLen, Text + i, (cc_u8*)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV); - *outputLen += SDRM_DES_BLOCK_SIZ; - break; - case ID_ENC_CTR : - retVal = SDRM_CTR_Enc(ID_DES, output + *outputLen, Text + i, (cc_u8*)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV, crt->ctx->desctx->CTR_Count++); - *outputLen += SDRM_DES_BLOCK_SIZ; - break; - case ID_DEC_ECB : - retVal = SDRM_ECB_Dec(ID_DES, output + *outputLen, Text + i, (cc_u8*)crt->ctx->desctx->RoundKey); - *outputLen += SDRM_DES_BLOCK_SIZ; - break; - case ID_DEC_CBC : - retVal = SDRM_CBC_Dec(ID_DES, output + *outputLen, Text + i, (cc_u8*)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV); - *outputLen += SDRM_DES_BLOCK_SIZ; - break; - case ID_DEC_CFB : - retVal = SDRM_CFB_Dec(ID_DES, output + *outputLen, Text + i, (cc_u8*)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV); - *outputLen += SDRM_DES_BLOCK_SIZ; - break; - case ID_DEC_OFB : - retVal = SDRM_OFB_Dec(ID_DES, output + *outputLen, Text + i, (cc_u8*)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV); - *outputLen += SDRM_DES_BLOCK_SIZ; - break; - case ID_DEC_CTR : - retVal = SDRM_CTR_Dec(ID_DES, output + *outputLen, Text + i, (cc_u8*)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV, crt->ctx->desctx->CTR_Count++); - *outputLen += SDRM_DES_BLOCK_SIZ; - break; - default : - return CRYPTO_INVALID_ARGUMENT; + + for (i = (SDRM_DES_BLOCK_SIZ - BlockLen) & 0x07; i < Temp; + i += SDRM_DES_BLOCK_SIZ) { + switch (crt->ctx->desctx->moo) { + case ID_ENC_ECB: + retVal = SDRM_ECB_Enc(ID_DES, output + *outputLen, Text + i, + (cc_u8 *)crt->ctx->desctx->RoundKey); + *outputLen += SDRM_DES_BLOCK_SIZ; + break; + + case ID_ENC_CBC: + retVal = SDRM_CBC_Enc(ID_DES, output + *outputLen, Text + i, + (cc_u8 *)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV); + *outputLen += SDRM_DES_BLOCK_SIZ; + break; + + case ID_ENC_CFB: + retVal = SDRM_CFB_Enc(ID_DES, output + *outputLen, Text + i, + (cc_u8 *)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV); + *outputLen += SDRM_DES_BLOCK_SIZ; + break; + + case ID_ENC_OFB: + retVal = SDRM_OFB_Enc(ID_DES, output + *outputLen, Text + i, + (cc_u8 *)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV); + *outputLen += SDRM_DES_BLOCK_SIZ; + break; + + case ID_ENC_CTR: + retVal = SDRM_CTR_Enc(ID_DES, output + *outputLen, Text + i, + (cc_u8 *)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV, + crt->ctx->desctx->CTR_Count++); + *outputLen += SDRM_DES_BLOCK_SIZ; + break; + + case ID_DEC_ECB: + retVal = SDRM_ECB_Dec(ID_DES, output + *outputLen, Text + i, + (cc_u8 *)crt->ctx->desctx->RoundKey); + *outputLen += SDRM_DES_BLOCK_SIZ; + break; + + case ID_DEC_CBC: + retVal = SDRM_CBC_Dec(ID_DES, output + *outputLen, Text + i, + (cc_u8 *)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV); + *outputLen += SDRM_DES_BLOCK_SIZ; + break; + + case ID_DEC_CFB: + retVal = SDRM_CFB_Dec(ID_DES, output + *outputLen, Text + i, + (cc_u8 *)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV); + *outputLen += SDRM_DES_BLOCK_SIZ; + break; + + case ID_DEC_OFB: + retVal = SDRM_OFB_Dec(ID_DES, output + *outputLen, Text + i, + (cc_u8 *)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV); + *outputLen += SDRM_DES_BLOCK_SIZ; + break; + + case ID_DEC_CTR: + retVal = SDRM_CTR_Dec(ID_DES, output + *outputLen, Text + i, + (cc_u8 *)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV, + crt->ctx->desctx->CTR_Count++); + *outputLen += SDRM_DES_BLOCK_SIZ; + break; + + default: + return CRYPTO_INVALID_ARGUMENT; } if (retVal != CRYPTO_SUCCESS) - { return retVal; - } } crt->ctx->desctx->BlockLen = (SDRM_DES_BLOCK_SIZ + TextLen - i) & 0x07; - memcpy(Block, Text + TextLen - crt->ctx->desctx->BlockLen, crt->ctx->desctx->BlockLen); + memcpy(Block, Text + TextLen - crt->ctx->desctx->BlockLen, + crt->ctx->desctx->BlockLen); return CRYPTO_SUCCESS; } /* - * @fn SDRM_DES_final - * @brief process final block and padding + * @fn SDRM_DES_final + * @brief process final block and padding * - * @param crt [in]crypto env structure - * @param input [in]message block - * @param inputLen [in]byte-length of Text - * @param output [out]processed message - * @param outputLen [out]byte-length of output + * @param crt [in]crypto env structure + * @param input [in]message block + * @param inputLen [in]byte-length of Text + * @param output [out]processed message + * @param outputLen [out]byte-length of output * - * @return CRYPTO_SUCCESS if success - * \n CRYPTO_NULL_POINTER if given argument is a null pointer - * \n CRYPTO_INVALID_ARGUMENT if given argument is invalid + * @return CRYPTO_SUCCESS if success + * \n CRYPTO_NULL_POINTER if given argument is a null pointer + * \n CRYPTO_INVALID_ARGUMENT if given argument is invalid */ -int SDRM_DES_final(CryptoCoreContainer *crt, cc_u8 *input, cc_u32 inputLen, cc_u8 *output, cc_u32 *outputLen) +int SDRM_DES_final(CryptoCoreContainer *crt, cc_u8 *input, cc_u32 inputLen, + cc_u8 *output, cc_u32 *outputLen) { - int retVal = CRYPTO_SUCCESS; - cc_u8 *Block, PADDING[16]; - cc_u32 BlockLen, t; + int retVal = CRYPTO_SUCCESS; + cc_u8 *Block, PADDING[16]; + cc_u32 BlockLen, t; if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->desctx == NULL)) - { return CRYPTO_NULL_POINTER; - } Block = crt->ctx->desctx->Block; BlockLen = crt->ctx->desctx->BlockLen; if (crt->ctx->desctx->moo >= ID_DEC_ECB) - { goto DECRYPTION; - } -//ENCRYPTION: - if (inputLen != 0) - { + //ENCRYPTION: + if (inputLen != 0) { retVal = SDRM_DES_process(crt, input, inputLen, output, outputLen); if (retVal != CRYPTO_SUCCESS) - { return retVal; - } retVal = SDRM_DES_final(crt, NULL, 0, output + *outputLen, &t); *outputLen += t; @@ -1070,230 +1096,225 @@ int SDRM_DES_final(CryptoCoreContainer *crt, cc_u8 *input, cc_u32 inputLen, cc_u } if (outputLen != NULL) - { *outputLen = SDRM_DES_BLOCK_SIZ; - } //padding - switch(crt->ctx->desctx->padding) - { - case 0 : - case ID_PKCS5 : - memset(Block + BlockLen, SDRM_DES_BLOCK_SIZ - BlockLen, SDRM_DES_BLOCK_SIZ - BlockLen); - break; - case ID_SSL_PADDING : - memset(Block + BlockLen, SDRM_DES_BLOCK_SIZ - BlockLen - 1, SDRM_DES_BLOCK_SIZ - BlockLen); - break; - case ID_ZERO_PADDING : - memset(Block + BlockLen, 0x00, SDRM_DES_BLOCK_SIZ - BlockLen); - break; - case ID_NO_PADDING : - if (BlockLen == 0) - { - if (outputLen) - { - *outputLen = 0; - } - return CRYPTO_SUCCESS; - } - break; - default : - return CRYPTO_INVALID_ARGUMENT; + switch (crt->ctx->desctx->padding) { + case 0: + case ID_PKCS5: + memset(Block + BlockLen, SDRM_DES_BLOCK_SIZ - BlockLen, + SDRM_DES_BLOCK_SIZ - BlockLen); + break; + + case ID_SSL_PADDING: + memset(Block + BlockLen, SDRM_DES_BLOCK_SIZ - BlockLen - 1, + SDRM_DES_BLOCK_SIZ - BlockLen); + break; + + case ID_ZERO_PADDING: + memset(Block + BlockLen, 0x00, SDRM_DES_BLOCK_SIZ - BlockLen); + break; + + case ID_NO_PADDING: + if (BlockLen == 0) { + if (outputLen) + *outputLen = 0; + + return CRYPTO_SUCCESS; + } + + break; + + default: + return CRYPTO_INVALID_ARGUMENT; } //encryption - switch(crt->ctx->desctx->moo) - { - case ID_ENC_ECB : - retVal = SDRM_ECB_Enc(ID_DES, output, Block, (cc_u8*)crt->ctx->desctx->RoundKey); - break; - case ID_ENC_CBC : - retVal = SDRM_CBC_Enc(ID_DES, output, Block, (cc_u8*)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV); - break; - case ID_ENC_CFB : - retVal = SDRM_CFB_Enc(ID_DES, output, Block, (cc_u8*)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV); - break; - case ID_ENC_OFB : - retVal = SDRM_OFB_Enc(ID_DES, output, Block, (cc_u8*)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV); - break; - case ID_ENC_CTR : - retVal = SDRM_CTR_Enc(ID_DES, output, Block, (cc_u8*)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV, crt->ctx->desctx->CTR_Count++); - break; - default : - retVal = CRYPTO_INVALID_ARGUMENT; - break; + switch (crt->ctx->desctx->moo) { + case ID_ENC_ECB: + retVal = SDRM_ECB_Enc(ID_DES, output, Block, + (cc_u8 *)crt->ctx->desctx->RoundKey); + break; + + case ID_ENC_CBC: + retVal = SDRM_CBC_Enc(ID_DES, output, Block, + (cc_u8 *)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV); + break; + + case ID_ENC_CFB: + retVal = SDRM_CFB_Enc(ID_DES, output, Block, + (cc_u8 *)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV); + break; + + case ID_ENC_OFB: + retVal = SDRM_OFB_Enc(ID_DES, output, Block, + (cc_u8 *)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV); + break; + + case ID_ENC_CTR: + retVal = SDRM_CTR_Enc(ID_DES, output, Block, + (cc_u8 *)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV, + crt->ctx->desctx->CTR_Count++); + break; + + default: + retVal = CRYPTO_INVALID_ARGUMENT; + break; } return retVal; DECRYPTION: + if (outputLen != NULL) - { *outputLen = 0; - } if ((inputLen == 0) && (crt->ctx->desctx->padding == ID_NO_PADDING)) - { return CRYPTO_SUCCESS; - } if ((BlockLen + inputLen) != SDRM_DES_BLOCK_SIZ) - { return CRYPTO_INVALID_ARGUMENT; - } if (inputLen != 0) - { memcpy(Block + BlockLen, input, inputLen); - } - switch(crt->ctx->desctx->moo) - { - case ID_DEC_ECB : - retVal = SDRM_ECB_Dec(ID_DES, output, Block, (cc_u8*)crt->ctx->desctx->RoundKey); - break; - case ID_DEC_CBC : - retVal = SDRM_CBC_Dec(ID_DES, output, Block, (cc_u8*)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV); - break; - case ID_DEC_CFB : - retVal = SDRM_CFB_Dec(ID_DES, output, Block, (cc_u8*)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV); - break; - case ID_DEC_OFB : - retVal = SDRM_OFB_Dec(ID_DES, output, Block, (cc_u8*)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV); - break; - case ID_DEC_CTR : - retVal = SDRM_CTR_Dec(ID_DES, output, Block, (cc_u8*)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV, crt->ctx->desctx->CTR_Count++); - break; - default : - return CRYPTO_INVALID_ARGUMENT; + switch (crt->ctx->desctx->moo) { + case ID_DEC_ECB: + retVal = SDRM_ECB_Dec(ID_DES, output, Block, + (cc_u8 *)crt->ctx->desctx->RoundKey); + break; + + case ID_DEC_CBC: + retVal = SDRM_CBC_Dec(ID_DES, output, Block, + (cc_u8 *)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV); + break; + + case ID_DEC_CFB: + retVal = SDRM_CFB_Dec(ID_DES, output, Block, + (cc_u8 *)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV); + break; + + case ID_DEC_OFB: + retVal = SDRM_OFB_Dec(ID_DES, output, Block, + (cc_u8 *)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV); + break; + + case ID_DEC_CTR: + retVal = SDRM_CTR_Dec(ID_DES, output, Block, + (cc_u8 *)crt->ctx->desctx->RoundKey, crt->ctx->desctx->IV, + crt->ctx->desctx->CTR_Count++); + break; + + default: + return CRYPTO_INVALID_ARGUMENT; } if (retVal != CRYPTO_SUCCESS) - { return retVal; - } //de-padding t = output[SDRM_DES_BLOCK_SIZ - 1]; - switch(crt->ctx->desctx->padding) - { - case 0 : - case ID_PKCS5 : - if ((t > SDRM_DES_BLOCK_SIZ) || (t < 1)) - { - return CRYPTO_INVALID_ARGUMENT; - } - memset(PADDING, t, t); - break; - case ID_SSL_PADDING : - ++t; - if ((t > SDRM_DES_BLOCK_SIZ) || (t < 1)) - { - return CRYPTO_INVALID_ARGUMENT; - } - memset(PADDING, t - 1, t); - break; - case ID_ZERO_PADDING : - { - cc_u32 tmpLen; - tmpLen = SDRM_DES_BLOCK_SIZ; - while((tmpLen != 0x00) && (output[tmpLen - 1] == 0x00)) - { - tmpLen--; - } - - if (outputLen != NULL) - { - *outputLen = tmpLen; - } - } - return CRYPTO_SUCCESS; - case ID_NO_PADDING : - if (outputLen != NULL) - { - *outputLen = SDRM_DES_BLOCK_SIZ; - } - return CRYPTO_SUCCESS; - default : - if (outputLen != NULL) - { - *outputLen = 0; - } + switch (crt->ctx->desctx->padding) { + case 0: + case ID_PKCS5: + if ((t > SDRM_DES_BLOCK_SIZ) || (t < 1)) return CRYPTO_INVALID_ARGUMENT; + + memset(PADDING, t, t); + break; + + case ID_SSL_PADDING: + ++t; + + if ((t > SDRM_DES_BLOCK_SIZ) || (t < 1)) + return CRYPTO_INVALID_ARGUMENT; + + memset(PADDING, t - 1, t); + break; + + case ID_ZERO_PADDING: { + cc_u32 tmpLen; + tmpLen = SDRM_DES_BLOCK_SIZ; + + while ((tmpLen != 0x00) && (output[tmpLen - 1] == 0x00)) + tmpLen--; + + if (outputLen != NULL) + *outputLen = tmpLen; } - if (memcmp(PADDING, output + SDRM_DES_BLOCK_SIZ - t, t) != 0) - { + return CRYPTO_SUCCESS; + + case ID_NO_PADDING: + if (outputLen != NULL) + *outputLen = SDRM_DES_BLOCK_SIZ; + + return CRYPTO_SUCCESS; + + default: + if (outputLen != NULL) + *outputLen = 0; + return CRYPTO_INVALID_ARGUMENT; } + if (memcmp(PADDING, output + SDRM_DES_BLOCK_SIZ - t, t) != 0) + return CRYPTO_INVALID_ARGUMENT; + if (outputLen != NULL) - { *outputLen = SDRM_DES_BLOCK_SIZ - t; - } return CRYPTO_SUCCESS; } /* - * @fn SDRM_TDES_init - * @brief intialize crypt context for triple des + * @fn SDRM_TDES_init + * @brief intialize crypt context for triple des * - * @param crt [out]crypto env structure - * @param mode [in]encryption|decryption and mode of operation - * @param PADDING [in]padding method - * @param key [in]user key - * @param keysize [in]byte-length of key - * @param IV [in]initial vector + * @param crt [out]crypto env structure + * @param mode [in]encryption|decryption and mode of operation + * @param PADDING [in]padding method + * @param key [in]user key + * @param keysize [in]byte-length of key + * @param IV [in]initial vector * - * @return CRYPTO_SUCCESS if success - * \n CRYPTO_NULL_POINTER if given argument is a null pointer - * \n CRYPTO_INVALID_ARGUMENT if given argument is invalid + * @return CRYPTO_SUCCESS if success + * \n CRYPTO_NULL_POINTER if given argument is a null pointer + * \n CRYPTO_INVALID_ARGUMENT if given argument is invalid */ -int SDRM_TDES_init(CryptoCoreContainer *crt, cc_u32 mode, cc_u32 PADDING, cc_u8 *key, cc_u32 keysize, cc_u8 *IV) +int SDRM_TDES_init(CryptoCoreContainer *crt, cc_u32 mode, cc_u32 PADDING, + cc_u8 *key, cc_u32 keysize, cc_u8 *IV) { - if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->tdesctx == NULL) || (key == NULL)) - { + if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->tdesctx == NULL) || + (key == NULL)) return CRYPTO_NULL_POINTER; - } - if (((keysize != 16) && (keysize != 24)) || !(((mode >= 1111) && (mode <= 1115)) || ((mode >= 1121) && (mode <= 1125)))) - { + if (((keysize != 16) && (keysize != 24)) || !(((mode >= 1111) && + (mode <= 1115)) || ((mode >= 1121) && (mode <= 1125)))) return CRYPTO_INVALID_ARGUMENT; - } crt->ctx->tdesctx->moo = mode; - if ((PADDING != 0) && (PADDING != ID_PKCS5) && (PADDING != ID_SSL_PADDING) && (PADDING != ID_ZERO_PADDING) && (PADDING != ID_NO_PADDING)) - { + if ((PADDING != 0) && (PADDING != ID_PKCS5) && (PADDING != ID_SSL_PADDING) && + (PADDING != ID_ZERO_PADDING) && (PADDING != ID_NO_PADDING)) return CRYPTO_INVALID_ARGUMENT; - } crt->ctx->tdesctx->padding = PADDING; - if ((mode != ID_DEC_ECB) && (mode != ID_DEC_CBC)) - { + if ((mode != ID_DEC_ECB) && (mode != ID_DEC_CBC)) { if (keysize == 16) - { - SDRM_getEncRoundKey(ID_TDES_EDE2, key, (cc_u8*)(crt->ctx->tdesctx->RoundKey)); - } + SDRM_getEncRoundKey(ID_TDES_EDE2, key, (cc_u8 *)(crt->ctx->tdesctx->RoundKey)); + else - { - SDRM_getEncRoundKey(ID_TDES_EDE3, key, (cc_u8*)(crt->ctx->tdesctx->RoundKey)); - } - } - else - { + SDRM_getEncRoundKey(ID_TDES_EDE3, key, (cc_u8 *)(crt->ctx->tdesctx->RoundKey)); + } else { if (keysize == 16) - { - SDRM_getDecRoundKey(ID_TDES_EDE2, key, (cc_u8*)(crt->ctx->tdesctx->RoundKey)); - } + SDRM_getDecRoundKey(ID_TDES_EDE2, key, (cc_u8 *)(crt->ctx->tdesctx->RoundKey)); + else - { - SDRM_getDecRoundKey(ID_TDES_EDE3, key, (cc_u8*)(crt->ctx->tdesctx->RoundKey)); - } + SDRM_getDecRoundKey(ID_TDES_EDE3, key, (cc_u8 *)(crt->ctx->tdesctx->RoundKey)); } crt->ctx->tdesctx->BlockLen = 0; @@ -1302,215 +1323,243 @@ int SDRM_TDES_init(CryptoCoreContainer *crt, cc_u32 mode, cc_u32 PADDING, cc_u8 memcpy(crt->ctx->tdesctx->UserKey, key, SDRM_DES_BLOCK_SIZ); if (IV) - { memcpy(crt->ctx->tdesctx->IV, IV, SDRM_DES_BLOCK_SIZ); - } + else - { memset(crt->ctx->tdesctx->IV, 0x00, SDRM_DES_BLOCK_SIZ); - } return CRYPTO_SUCCESS; } /* - * @fn int SDRM_TDES_process(CryptoCoreContainer *crt, cc_u8 *Text, cc_u32 TextLen, cc_u8 *output, cc_u32 *outputLen) - * @brief process message block + * @fn int SDRM_TDES_process(CryptoCoreContainer *crt, cc_u8 *Text, cc_u32 TextLen, cc_u8 *output, cc_u32 *outputLen) + * @brief process message block * - * @param crt [in]crypto env structure - * @param Text [in]message block - * @param TextLen [in]byte-length of Text - * @param output [out]proecessed message - * @param outputLen [out]byte-length of output + * @param crt [in]crypto env structure + * @param Text [in]message block + * @param TextLen [in]byte-length of Text + * @param output [out]proecessed message + * @param outputLen [out]byte-length of output * - * @return CRYPTO_SUCCESS if success - * \n CRYPTO_NULL_POINTER if given argument is a null pointer - * \n CRYPTO_INVALID_ARGUMENT if given argument is invalid + * @return CRYPTO_SUCCESS if success + * \n CRYPTO_NULL_POINTER if given argument is a null pointer + * \n CRYPTO_INVALID_ARGUMENT if given argument is invalid */ -int SDRM_TDES_process(CryptoCoreContainer *crt, cc_u8 *Text, cc_u32 TextLen, cc_u8 *output, cc_u32 *outputLen) +int SDRM_TDES_process(CryptoCoreContainer *crt, cc_u8 *Text, cc_u32 TextLen, + cc_u8 *output, cc_u32 *outputLen) { - int i, Temp; - int retVal, BlockLen; - cc_u8 *Block; + int i, Temp; + int retVal, BlockLen; + cc_u8 *Block; if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->tdesctx == NULL)) - { return CRYPTO_NULL_POINTER; - } Block = crt->ctx->tdesctx->Block; BlockLen = crt->ctx->tdesctx->BlockLen; *outputLen = 0; - if ((TextLen + BlockLen) < SDRM_DES_BLOCK_SIZ) - { + if ((TextLen + BlockLen) < SDRM_DES_BLOCK_SIZ) { memcpy(Block + BlockLen, Text, TextLen); crt->ctx->tdesctx->BlockLen += TextLen; return CRYPTO_SUCCESS; } - if (BlockLen) - { + if (BlockLen) { memcpy(Block + BlockLen, Text, SDRM_DES_BLOCK_SIZ - BlockLen); - switch(crt->ctx->tdesctx->moo) - { - case ID_ENC_ECB : - retVal = SDRM_ECB_Enc(ID_TDES, output, Block, (cc_u8*)crt->ctx->tdesctx->RoundKey); - *outputLen += SDRM_DES_BLOCK_SIZ; - break; - case ID_ENC_CBC : - retVal = SDRM_CBC_Enc(ID_TDES, output, Block, (cc_u8*)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV); - *outputLen += SDRM_DES_BLOCK_SIZ; - break; - case ID_ENC_CFB : - retVal = SDRM_CFB_Enc(ID_TDES, output, Block, (cc_u8*)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV); - *outputLen += SDRM_DES_BLOCK_SIZ; - break; - case ID_ENC_OFB : - retVal = SDRM_OFB_Enc(ID_TDES, output, Block, (cc_u8*)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV); - *outputLen += SDRM_DES_BLOCK_SIZ; - break; - case ID_ENC_CTR : - retVal = SDRM_CTR_Enc(ID_TDES, output, Block, (cc_u8*)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV, crt->ctx->tdesctx->CTR_Count++); - *outputLen += SDRM_DES_BLOCK_SIZ; - break; - case ID_DEC_ECB : - retVal = SDRM_ECB_Dec(ID_TDES, output, Block, (cc_u8*)crt->ctx->tdesctx->RoundKey); - *outputLen += SDRM_DES_BLOCK_SIZ; - break; - case ID_DEC_CBC : - retVal = SDRM_CBC_Dec(ID_TDES, output, Block, (cc_u8*)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV); - *outputLen += SDRM_DES_BLOCK_SIZ; - break; - case ID_DEC_CFB : - retVal = SDRM_CFB_Dec(ID_TDES, output, Block, (cc_u8*)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV); - *outputLen += SDRM_DES_BLOCK_SIZ; - break; - case ID_DEC_OFB : - retVal = SDRM_OFB_Dec(ID_TDES, output, Block, (cc_u8*)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV); - *outputLen += SDRM_DES_BLOCK_SIZ; - break; - case ID_DEC_CTR : - retVal = SDRM_CTR_Dec(ID_TDES, output, Block, (cc_u8*)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV, crt->ctx->tdesctx->CTR_Count++); - *outputLen += SDRM_DES_BLOCK_SIZ; - break; - default : - return CRYPTO_INVALID_ARGUMENT; + switch (crt->ctx->tdesctx->moo) { + case ID_ENC_ECB: + retVal = SDRM_ECB_Enc(ID_TDES, output, Block, + (cc_u8 *)crt->ctx->tdesctx->RoundKey); + *outputLen += SDRM_DES_BLOCK_SIZ; + break; + + case ID_ENC_CBC: + retVal = SDRM_CBC_Enc(ID_TDES, output, Block, + (cc_u8 *)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV); + *outputLen += SDRM_DES_BLOCK_SIZ; + break; + + case ID_ENC_CFB: + retVal = SDRM_CFB_Enc(ID_TDES, output, Block, + (cc_u8 *)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV); + *outputLen += SDRM_DES_BLOCK_SIZ; + break; + + case ID_ENC_OFB: + retVal = SDRM_OFB_Enc(ID_TDES, output, Block, + (cc_u8 *)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV); + *outputLen += SDRM_DES_BLOCK_SIZ; + break; + + case ID_ENC_CTR: + retVal = SDRM_CTR_Enc(ID_TDES, output, Block, + (cc_u8 *)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV, + crt->ctx->tdesctx->CTR_Count++); + *outputLen += SDRM_DES_BLOCK_SIZ; + break; + + case ID_DEC_ECB: + retVal = SDRM_ECB_Dec(ID_TDES, output, Block, + (cc_u8 *)crt->ctx->tdesctx->RoundKey); + *outputLen += SDRM_DES_BLOCK_SIZ; + break; + + case ID_DEC_CBC: + retVal = SDRM_CBC_Dec(ID_TDES, output, Block, + (cc_u8 *)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV); + *outputLen += SDRM_DES_BLOCK_SIZ; + break; + + case ID_DEC_CFB: + retVal = SDRM_CFB_Dec(ID_TDES, output, Block, + (cc_u8 *)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV); + *outputLen += SDRM_DES_BLOCK_SIZ; + break; + + case ID_DEC_OFB: + retVal = SDRM_OFB_Dec(ID_TDES, output, Block, + (cc_u8 *)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV); + *outputLen += SDRM_DES_BLOCK_SIZ; + break; + + case ID_DEC_CTR: + retVal = SDRM_CTR_Dec(ID_TDES, output, Block, + (cc_u8 *)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV, + crt->ctx->tdesctx->CTR_Count++); + *outputLen += SDRM_DES_BLOCK_SIZ; + break; + + default: + return CRYPTO_INVALID_ARGUMENT; } if (retVal != CRYPTO_SUCCESS) - { return retVal; - } } Temp = TextLen + BlockLen - SDRM_DES_BLOCK_SIZ + 1; - for (i = (SDRM_DES_BLOCK_SIZ - BlockLen) & 0x07; i < Temp; i += SDRM_DES_BLOCK_SIZ) - { - switch(crt->ctx->tdesctx->moo) - { - case ID_ENC_ECB : - retVal = SDRM_ECB_Enc(ID_TDES, output + *outputLen, Text + i, (cc_u8*)crt->ctx->tdesctx->RoundKey); - *outputLen += SDRM_DES_BLOCK_SIZ; - break; - case ID_ENC_CBC : - retVal = SDRM_CBC_Enc(ID_TDES, output + *outputLen, Text + i, (cc_u8*)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV); - *outputLen += SDRM_DES_BLOCK_SIZ; - break; - case ID_ENC_CFB : - retVal = SDRM_CFB_Enc(ID_TDES, output + *outputLen, Text + i, (cc_u8*)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV); - *outputLen += SDRM_DES_BLOCK_SIZ; - break; - case ID_ENC_OFB : - retVal = SDRM_OFB_Enc(ID_TDES, output + *outputLen, Text + i, (cc_u8*)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV); - *outputLen += SDRM_DES_BLOCK_SIZ; - break; - case ID_ENC_CTR : - retVal = SDRM_CTR_Enc(ID_TDES, output + *outputLen, Text + i, (cc_u8*)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV, crt->ctx->tdesctx->CTR_Count++); - *outputLen += SDRM_DES_BLOCK_SIZ; - break; - case ID_DEC_ECB : - retVal = SDRM_ECB_Dec(ID_TDES, output + *outputLen, Text + i, (cc_u8*)crt->ctx->tdesctx->RoundKey); - *outputLen += SDRM_DES_BLOCK_SIZ; - break; - case ID_DEC_CBC : - retVal = SDRM_CBC_Dec(ID_TDES, output + *outputLen, Text + i, (cc_u8*)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV); - *outputLen += SDRM_DES_BLOCK_SIZ; - break; - case ID_DEC_CFB : - retVal = SDRM_CFB_Dec(ID_TDES, output + *outputLen, Text + i, (cc_u8*)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV); - *outputLen += SDRM_DES_BLOCK_SIZ; - break; - case ID_DEC_OFB : - retVal = SDRM_OFB_Dec(ID_TDES, output + *outputLen, Text + i, (cc_u8*)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV); - *outputLen += SDRM_DES_BLOCK_SIZ; - break; - case ID_DEC_CTR : - retVal = SDRM_CTR_Dec(ID_TDES, output + *outputLen, Text + i, (cc_u8*)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV, crt->ctx->tdesctx->CTR_Count++); - *outputLen += SDRM_DES_BLOCK_SIZ; - break; - default : - return CRYPTO_INVALID_ARGUMENT; + + for (i = (SDRM_DES_BLOCK_SIZ - BlockLen) & 0x07; i < Temp; + i += SDRM_DES_BLOCK_SIZ) { + switch (crt->ctx->tdesctx->moo) { + case ID_ENC_ECB: + retVal = SDRM_ECB_Enc(ID_TDES, output + *outputLen, Text + i, + (cc_u8 *)crt->ctx->tdesctx->RoundKey); + *outputLen += SDRM_DES_BLOCK_SIZ; + break; + + case ID_ENC_CBC: + retVal = SDRM_CBC_Enc(ID_TDES, output + *outputLen, Text + i, + (cc_u8 *)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV); + *outputLen += SDRM_DES_BLOCK_SIZ; + break; + + case ID_ENC_CFB: + retVal = SDRM_CFB_Enc(ID_TDES, output + *outputLen, Text + i, + (cc_u8 *)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV); + *outputLen += SDRM_DES_BLOCK_SIZ; + break; + + case ID_ENC_OFB: + retVal = SDRM_OFB_Enc(ID_TDES, output + *outputLen, Text + i, + (cc_u8 *)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV); + *outputLen += SDRM_DES_BLOCK_SIZ; + break; + + case ID_ENC_CTR: + retVal = SDRM_CTR_Enc(ID_TDES, output + *outputLen, Text + i, + (cc_u8 *)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV, + crt->ctx->tdesctx->CTR_Count++); + *outputLen += SDRM_DES_BLOCK_SIZ; + break; + + case ID_DEC_ECB: + retVal = SDRM_ECB_Dec(ID_TDES, output + *outputLen, Text + i, + (cc_u8 *)crt->ctx->tdesctx->RoundKey); + *outputLen += SDRM_DES_BLOCK_SIZ; + break; + + case ID_DEC_CBC: + retVal = SDRM_CBC_Dec(ID_TDES, output + *outputLen, Text + i, + (cc_u8 *)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV); + *outputLen += SDRM_DES_BLOCK_SIZ; + break; + + case ID_DEC_CFB: + retVal = SDRM_CFB_Dec(ID_TDES, output + *outputLen, Text + i, + (cc_u8 *)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV); + *outputLen += SDRM_DES_BLOCK_SIZ; + break; + + case ID_DEC_OFB: + retVal = SDRM_OFB_Dec(ID_TDES, output + *outputLen, Text + i, + (cc_u8 *)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV); + *outputLen += SDRM_DES_BLOCK_SIZ; + break; + + case ID_DEC_CTR: + retVal = SDRM_CTR_Dec(ID_TDES, output + *outputLen, Text + i, + (cc_u8 *)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV, + crt->ctx->tdesctx->CTR_Count++); + *outputLen += SDRM_DES_BLOCK_SIZ; + break; + + default: + return CRYPTO_INVALID_ARGUMENT; } if (retVal != CRYPTO_SUCCESS) - { return retVal; - } } crt->ctx->tdesctx->BlockLen = (SDRM_DES_BLOCK_SIZ + TextLen - i) & 0x07; - memcpy(Block, Text + TextLen - crt->ctx->tdesctx->BlockLen, crt->ctx->tdesctx->BlockLen); + memcpy(Block, Text + TextLen - crt->ctx->tdesctx->BlockLen, + crt->ctx->tdesctx->BlockLen); return CRYPTO_SUCCESS; } /* - * @fn int SDRM_TDES_final(CryptoCoreContainer *crt, cc_u8 *input, cc_u32 inputLen, cc_u8 *output, cc_u32 *outputLen) - * @brief process final block and padding + * @fn int SDRM_TDES_final(CryptoCoreContainer *crt, cc_u8 *input, cc_u32 inputLen, cc_u8 *output, cc_u32 *outputLen) + * @brief process final block and padding * - * @param crt [in]crypto env structure - * @param input [in]message block - * @param inputLen [in]byte-length of Text - * @param output [out]processed message - * @param outputLen [out]byte-length of output + * @param crt [in]crypto env structure + * @param input [in]message block + * @param inputLen [in]byte-length of Text + * @param output [out]processed message + * @param outputLen [out]byte-length of output * - * @return CRYPTO_SUCCESS if success - * \n CRYPTO_NULL_POINTER if given argument is a null pointer - * \n CRYPTO_INVALID_ARGUMENT if given argument is invalid + * @return CRYPTO_SUCCESS if success + * \n CRYPTO_NULL_POINTER if given argument is a null pointer + * \n CRYPTO_INVALID_ARGUMENT if given argument is invalid */ -int SDRM_TDES_final(CryptoCoreContainer *crt, cc_u8 *input, cc_u32 inputLen, cc_u8 *output, cc_u32 *outputLen) +int SDRM_TDES_final(CryptoCoreContainer *crt, cc_u8 *input, cc_u32 inputLen, + cc_u8 *output, cc_u32 *outputLen) { - int retVal = CRYPTO_SUCCESS; - cc_u8 *Block, PADDING[16]; - cc_u32 BlockLen, t; + int retVal = CRYPTO_SUCCESS; + cc_u8 *Block, PADDING[16]; + cc_u32 BlockLen, t; if ((crt == NULL) || (crt->ctx == NULL) || (crt->ctx->tdesctx == NULL)) - { return CRYPTO_NULL_POINTER; - } Block = crt->ctx->tdesctx->Block; BlockLen = crt->ctx->tdesctx->BlockLen; if (crt->ctx->tdesctx->moo >= ID_DEC_ECB) - { goto DECRYPTION; - } -//ENCRYPTION: - if (inputLen != 0) - { + //ENCRYPTION: + if (inputLen != 0) { retVal = SDRM_TDES_process(crt, input, inputLen, output, outputLen); if (retVal != CRYPTO_SUCCESS) - { return retVal; - } retVal = SDRM_TDES_final(crt, NULL, 0, output + *outputLen, &t); *outputLen += t; @@ -1519,171 +1568,177 @@ int SDRM_TDES_final(CryptoCoreContainer *crt, cc_u8 *input, cc_u32 inputLen, cc_ } if (outputLen != NULL) - { *outputLen = SDRM_DES_BLOCK_SIZ; - } //padding - switch(crt->ctx->tdesctx->padding) - { - case 0 : - case ID_PKCS5 : - memset(Block + BlockLen, SDRM_DES_BLOCK_SIZ - BlockLen, SDRM_DES_BLOCK_SIZ - BlockLen); - break; - case ID_SSL_PADDING : - memset(Block + BlockLen, SDRM_DES_BLOCK_SIZ - BlockLen - 1, SDRM_DES_BLOCK_SIZ - BlockLen); - break; - case ID_ZERO_PADDING : - memset(Block + BlockLen, 0x00, SDRM_DES_BLOCK_SIZ - BlockLen); - break; - case ID_NO_PADDING : - if (BlockLen == 0) - { - if (outputLen) - { - *outputLen = 0; - } - return CRYPTO_SUCCESS; - } - break; - default : - return CRYPTO_INVALID_ARGUMENT; + switch (crt->ctx->tdesctx->padding) { + case 0: + case ID_PKCS5: + memset(Block + BlockLen, SDRM_DES_BLOCK_SIZ - BlockLen, + SDRM_DES_BLOCK_SIZ - BlockLen); + break; + + case ID_SSL_PADDING: + memset(Block + BlockLen, SDRM_DES_BLOCK_SIZ - BlockLen - 1, + SDRM_DES_BLOCK_SIZ - BlockLen); + break; + + case ID_ZERO_PADDING: + memset(Block + BlockLen, 0x00, SDRM_DES_BLOCK_SIZ - BlockLen); + break; + + case ID_NO_PADDING: + if (BlockLen == 0) { + if (outputLen) + *outputLen = 0; + + return CRYPTO_SUCCESS; + } + + break; + + default: + return CRYPTO_INVALID_ARGUMENT; } //encryption - switch(crt->ctx->tdesctx->moo) - { - case ID_ENC_ECB : - retVal = SDRM_ECB_Enc(ID_TDES, output, Block, (cc_u8*)crt->ctx->tdesctx->RoundKey); - break; - case ID_ENC_CBC : - retVal = SDRM_CBC_Enc(ID_TDES, output, Block, (cc_u8*)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV); - break; - case ID_ENC_CFB : - retVal = SDRM_CFB_Enc(ID_TDES, output, Block, (cc_u8*)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV); - break; - case ID_ENC_OFB : - retVal = SDRM_OFB_Enc(ID_TDES, output, Block, (cc_u8*)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV); - break; - case ID_ENC_CTR : - retVal = SDRM_CTR_Enc(ID_TDES, output, Block, (cc_u8*)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV, crt->ctx->tdesctx->CTR_Count++); - break; - default : - retVal = CRYPTO_INVALID_ARGUMENT; - break; + switch (crt->ctx->tdesctx->moo) { + case ID_ENC_ECB: + retVal = SDRM_ECB_Enc(ID_TDES, output, Block, + (cc_u8 *)crt->ctx->tdesctx->RoundKey); + break; + + case ID_ENC_CBC: + retVal = SDRM_CBC_Enc(ID_TDES, output, Block, + (cc_u8 *)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV); + break; + + case ID_ENC_CFB: + retVal = SDRM_CFB_Enc(ID_TDES, output, Block, + (cc_u8 *)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV); + break; + + case ID_ENC_OFB: + retVal = SDRM_OFB_Enc(ID_TDES, output, Block, + (cc_u8 *)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV); + break; + + case ID_ENC_CTR: + retVal = SDRM_CTR_Enc(ID_TDES, output, Block, + (cc_u8 *)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV, + crt->ctx->tdesctx->CTR_Count++); + break; + + default: + retVal = CRYPTO_INVALID_ARGUMENT; + break; } return retVal; DECRYPTION: + if (outputLen != NULL) - { *outputLen = 0; - } if ((inputLen == 0) && (crt->ctx->tdesctx->padding == ID_NO_PADDING)) - { return CRYPTO_SUCCESS; - } if ((BlockLen + inputLen) != SDRM_DES_BLOCK_SIZ) - { return CRYPTO_INVALID_ARGUMENT; - } if (inputLen != 0) - { memcpy(Block + BlockLen, input, inputLen); - } - switch(crt->ctx->tdesctx->moo) - { - case ID_DEC_ECB : - retVal = SDRM_ECB_Dec(ID_TDES, output, Block, (cc_u8*)crt->ctx->tdesctx->RoundKey); - break; - case ID_DEC_CBC : - retVal = SDRM_CBC_Dec(ID_TDES, output, Block, (cc_u8*)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV); - break; - case ID_DEC_CFB : - retVal = SDRM_CFB_Dec(ID_TDES, output, Block, (cc_u8*)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV); - break; - case ID_DEC_OFB : - retVal = SDRM_OFB_Dec(ID_TDES, output, Block, (cc_u8*)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV); - break; - case ID_DEC_CTR : - retVal = SDRM_CTR_Dec(ID_TDES, output, Block, (cc_u8*)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV, crt->ctx->tdesctx->CTR_Count++); - break; - default : - return CRYPTO_INVALID_ARGUMENT; + switch (crt->ctx->tdesctx->moo) { + case ID_DEC_ECB: + retVal = SDRM_ECB_Dec(ID_TDES, output, Block, + (cc_u8 *)crt->ctx->tdesctx->RoundKey); + break; + + case ID_DEC_CBC: + retVal = SDRM_CBC_Dec(ID_TDES, output, Block, + (cc_u8 *)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV); + break; + + case ID_DEC_CFB: + retVal = SDRM_CFB_Dec(ID_TDES, output, Block, + (cc_u8 *)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV); + break; + + case ID_DEC_OFB: + retVal = SDRM_OFB_Dec(ID_TDES, output, Block, + (cc_u8 *)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV); + break; + + case ID_DEC_CTR: + retVal = SDRM_CTR_Dec(ID_TDES, output, Block, + (cc_u8 *)crt->ctx->tdesctx->RoundKey, crt->ctx->tdesctx->IV, + crt->ctx->tdesctx->CTR_Count++); + break; + + default: + return CRYPTO_INVALID_ARGUMENT; } if (retVal != CRYPTO_SUCCESS) - { return retVal; - } //de-padding t = output[SDRM_DES_BLOCK_SIZ - 1]; - switch(crt->ctx->tdesctx->padding) - { - case 0 : - case ID_PKCS5 : - if ((t > SDRM_DES_BLOCK_SIZ) || (t < 1)) - { - return CRYPTO_INVALID_ARGUMENT; - } - memset(PADDING, t, t); - break; - case ID_SSL_PADDING : - ++t; - if ((t > SDRM_DES_BLOCK_SIZ) || (t < 1)) - { - return CRYPTO_INVALID_ARGUMENT; - } - memset(PADDING, t - 1, t); - break; - case ID_ZERO_PADDING : - { - cc_u32 tmpLen; - tmpLen = SDRM_TDES_BLOCK_SIZ; - while((tmpLen != 0x00) && (output[tmpLen - 1] == 0x00)) - { - tmpLen--; - } - - if (outputLen != NULL) - { - *outputLen = tmpLen; - } - } - return CRYPTO_SUCCESS; - case ID_NO_PADDING : - if (outputLen != NULL) - { - *outputLen = SDRM_TDES_BLOCK_SIZ; - } - return CRYPTO_SUCCESS; - default : - if (outputLen != NULL) - { - *outputLen = 0; - } + switch (crt->ctx->tdesctx->padding) { + case 0: + case ID_PKCS5: + if ((t > SDRM_DES_BLOCK_SIZ) || (t < 1)) return CRYPTO_INVALID_ARGUMENT; + + memset(PADDING, t, t); + break; + + case ID_SSL_PADDING: + ++t; + + if ((t > SDRM_DES_BLOCK_SIZ) || (t < 1)) + return CRYPTO_INVALID_ARGUMENT; + + memset(PADDING, t - 1, t); + break; + + case ID_ZERO_PADDING: { + cc_u32 tmpLen; + tmpLen = SDRM_TDES_BLOCK_SIZ; + + while ((tmpLen != 0x00) && (output[tmpLen - 1] == 0x00)) + tmpLen--; + + if (outputLen != NULL) + *outputLen = tmpLen; } - if (memcmp(PADDING, output + SDRM_TDES_BLOCK_SIZ - t, t) != 0) - { + return CRYPTO_SUCCESS; + + case ID_NO_PADDING: + if (outputLen != NULL) + *outputLen = SDRM_TDES_BLOCK_SIZ; + + return CRYPTO_SUCCESS; + + default: + if (outputLen != NULL) + *outputLen = 0; + return CRYPTO_INVALID_ARGUMENT; } + if (memcmp(PADDING, output + SDRM_TDES_BLOCK_SIZ - t, t) != 0) + return CRYPTO_INVALID_ARGUMENT; + if (outputLen != NULL) - { *outputLen = SDRM_DES_BLOCK_SIZ - t; - } return CRYPTO_SUCCESS; } -/***************************** End of File *****************************/ \ No newline at end of file +/***************************** End of File *****************************/ diff --git a/ssflib/dep/cryptocore/source/middle/cc_tdes.c b/ssflib/dep/cryptocore/source/middle/cc_tdes.c index 1698bac..adcfc42 100644 --- a/ssflib/dep/cryptocore/source/middle/cc_tdes.c +++ b/ssflib/dep/cryptocore/source/middle/cc_tdes.c @@ -31,44 +31,41 @@ // Functions //////////////////////////////////////////////////////////////////////////// /* - * @fn SDRM_TDES_KeySched - * @brief Expand the cipher key into the encryption key schedule + * @fn SDRM_TDES_KeySched + * @brief Expand the cipher key into the encryption key schedule * - * @param RoundKey [out]generated round key - * @param UserKey [in]user key, 16 or 24 byte - * @param KeyLen [in]byte-length of UserKey - * @param RKStep [in]operation mode + * @param RoundKey [out]generated round key + * @param UserKey [in]user key, 16 or 24 byte + * @param KeyLen [in]byte-length of UserKey + * @param RKStep [in]operation mode * - * @return the number of rounds for the given cipher key size + * @return the number of rounds for the given cipher key size */ -int SDRM_TDES_KeySched(cc_u8 *RoundKey, cc_u8 *UserKey, cc_u32 KeyLen, cc_u32 RKStep) +int SDRM_TDES_KeySched(cc_u8 *RoundKey, cc_u8 *UserKey, cc_u32 KeyLen, + cc_u32 RKStep) { - if (RKStep == 1) - { + if (RKStep == 1) { SDRM_DES_KeySched(RoundKey, UserKey, 0, 1); - SDRM_DES_KeySched(RoundKey + 128, UserKey + 8, 15, (cc_u32)-1); + SDRM_DES_KeySched(RoundKey + 128, UserKey + 8, 15, (cc_u32) - 1); - if (KeyLen == 16) - { //2-key des + if (KeyLen == 16) { + //2-key des memcpy(RoundKey + 256, RoundKey, 128); - } - else - { //3-key des + } else { + //3-key des SDRM_DES_KeySched(RoundKey + 256, UserKey + 16, 0, 1); } - } - else { - SDRM_DES_KeySched(RoundKey + 256, UserKey, 15, (cc_u32)-1); + } else { + SDRM_DES_KeySched(RoundKey + 256, UserKey, 15, (cc_u32) - 1); SDRM_DES_KeySched(RoundKey + 128, UserKey + 8, 0, 1); - if (KeyLen == 16) - { //2-key des + if (KeyLen == 16) { + //2-key des memcpy(RoundKey, RoundKey + 256, 128); - } - else - { //3-key des - SDRM_DES_KeySched(RoundKey, UserKey + 16, 15, (cc_u32)-1); + } else { + //3-key des + SDRM_DES_KeySched(RoundKey, UserKey + 16, 15, (cc_u32) - 1); } } @@ -76,20 +73,20 @@ int SDRM_TDES_KeySched(cc_u8 *RoundKey, cc_u8 *UserKey, cc_u32 KeyLen, cc_u32 RK } /* - * @fn SDRM_TDES_Encryption - * @brief Triple DES processing for one block + * @fn SDRM_TDES_Encryption + * @brief Triple DES processing for one block * - * @param RoundKey [in]expanded round key - * @param msg [in]8 byte plaintext - * @param out [out]8 byte ciphertext + * @param RoundKey [in]expanded round key + * @param msg [in]8 byte plaintext + * @param out [out]8 byte ciphertext * - * @return CRYPTO_SUCCESS if no error is occured + * @return CRYPTO_SUCCESS if no error is occured */ int SDRM_TDES_Encryption(cc_u32 RoundKey[][2], cc_u8 *msg, cc_u8 *out) { cc_u8 buf[8]; - SDRM_DES_Encryption(RoundKey , msg, buf); + SDRM_DES_Encryption(RoundKey, msg, buf); SDRM_DES_Encryption(RoundKey + 16, buf, buf); SDRM_DES_Encryption(RoundKey + 32, buf, out); @@ -98,20 +95,20 @@ int SDRM_TDES_Encryption(cc_u32 RoundKey[][2], cc_u8 *msg, cc_u8 *out) /* - * @fn SDRM_TDES64_Encryption - * @brief one block Triple DES Encryption + * @fn SDRM_TDES64_Encryption + * @brief one block Triple DES Encryption * - * @param cipherText [out]encrypted text - * @param plainText [in]plain text - * @param UserKey [in]user key + * @param cipherText [out]encrypted text + * @param plainText [in]plain text + * @param UserKey [in]user key * - * @return CRYPTO_SUCCESS if success + * @return CRYPTO_SUCCESS if success */ int SDRM_TDES64_Encryption(cc_u8 *cipherText, cc_u8 *plainText, cc_u8 *UserKey) { cc_u32 RoundKey[48][2]; - SDRM_TDES_KeySched((cc_u8*)RoundKey, UserKey, 16, 1); + SDRM_TDES_KeySched((cc_u8 *)RoundKey, UserKey, 16, 1); SDRM_TDES_Encryption(RoundKey, plainText, cipherText); @@ -119,24 +116,24 @@ int SDRM_TDES64_Encryption(cc_u8 *cipherText, cc_u8 *plainText, cc_u8 *UserKey) } /* - * @fn SDRM_TDES64_Decryption - * @brief one block Triple DES Decryption + * @fn SDRM_TDES64_Decryption + * @brief one block Triple DES Decryption * - * @param plainText [out]decrypted text - * @param cipherText [in]cipher text - * @param UserKey [in]user key + * @param plainText [out]decrypted text + * @param cipherText [in]cipher text + * @param UserKey [in]user key * - * @return CRYPTO_SUCCESS if success + * @return CRYPTO_SUCCESS if success */ int SDRM_TDES64_Decryption(cc_u8 *plainText, cc_u8 *cipherText, cc_u8 *UserKey) { cc_u32 RoundKey[48][2]; - SDRM_TDES_KeySched((cc_u8*)RoundKey, UserKey, 16, (cc_u32)-1); + SDRM_TDES_KeySched((cc_u8 *)RoundKey, UserKey, 16, (cc_u32) - 1); SDRM_TDES_Encryption(RoundKey, cipherText, plainText); return CRYPTO_SUCCESS; } -/***************************** End of File *****************************/ \ No newline at end of file +/***************************** End of File *****************************/ diff --git a/ssflib/dep/uci/source/uci_aes_xcbc_mac.c b/ssflib/dep/uci/source/uci_aes_xcbc_mac.c index da5dbe7..e107f91 100644 --- a/ssflib/dep/uci/source/uci_aes_xcbc_mac.c +++ b/ssflib/dep/uci/source/uci_aes_xcbc_mac.c @@ -29,13 +29,17 @@ #include "cc_aes.h" static unsigned char k1[] = {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, - 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}; + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 + }; static unsigned char k2[] = {0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, - 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02}; + 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02 + }; static unsigned char k3[] = {0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, - 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03}; + 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03 + }; -int xcbc_init(aes_xcbc_state *xcbc, unsigned char *key, unsigned int keylen) { +int xcbc_init(aes_xcbc_state *xcbc, unsigned char *key, unsigned int keylen) +{ /* (1) Derive 3 128-bit keys (K1, K2 and K3) from the 128-bit secret @@ -44,15 +48,15 @@ int xcbc_init(aes_xcbc_state *xcbc, unsigned char *key, unsigned int keylen) { K2 = 0x02020202020202020202020202020202 encrypted with Key K K3 = 0x03030303030303030303030303030303 encrypted with Key K - (2) Define E[0](iv) = 0x00000000000000000000000000000000 + (2) Define E[0](iv) = 0x00000000000000000000000000000000 */ - if (keylen != 16) { + if (keylen != 16) return 0; - } - if (xcbc == NULL) { + + if (xcbc == NULL) return 0; - } + memcpy(xcbc->key, key, 16); SDRM_AES128_Encryption(xcbc->K[0], k1, xcbc->key); SDRM_AES128_Encryption(xcbc->K[1], k2, xcbc->key); @@ -64,7 +68,8 @@ int xcbc_init(aes_xcbc_state *xcbc, unsigned char *key, unsigned int keylen) { memset(xcbc->IV, 0, MAXBLOCKSIZE); return 1; } -int xcbc_process(aes_xcbc_state *xcbc, unsigned char *in, unsigned int inlen) { +int xcbc_process(aes_xcbc_state *xcbc, unsigned char *in, unsigned int inlen) +{ /* (3) For each block M[i], where i = 1 ... n-1: @@ -73,34 +78,40 @@ int xcbc_process(aes_xcbc_state *xcbc, unsigned char *in, unsigned int inlen) { */ unsigned int x; - if (xcbc == NULL) { + + if (xcbc == NULL) return 0; - } + if (xcbc->buflen == 0) { while (inlen > xcbc->blocksize) { - for (x = 0; x < xcbc->blocksize; x++) { + for (x = 0; x < xcbc->blocksize; x++) xcbc->IV[x] ^= in[x]; - } + SDRM_AES128_Encryption(xcbc->IV, xcbc->IV, xcbc->key); in += xcbc->blocksize; inlen -= xcbc->blocksize; } } + while (inlen) { if (xcbc->buflen == xcbc->blocksize) { SDRM_AES128_Encryption(xcbc->IV, xcbc->IV, xcbc->key); xcbc->buflen = 0; } + xcbc->IV[xcbc->buflen++] ^= *in++; --inlen; } + return 1; } -int xcbc_done(aes_xcbc_state *xcbc, unsigned char *out, unsigned int *outlen) { +int xcbc_done(aes_xcbc_state *xcbc, unsigned char *out, unsigned int *outlen) +{ unsigned int x; - if (xcbc == NULL || out == NULL) { + + if (xcbc == NULL || out == NULL) return 0; - } + /* (4) a) If the blocksize of M[n] is 128 bits: @@ -108,13 +119,12 @@ int xcbc_done(aes_xcbc_state *xcbc, unsigned char *out, unsigned int *outlen) { Key K1, yielding E[n]. */ if (xcbc->buflen == xcbc->blocksize) { - for (x = 0; x < xcbc->blocksize; x++) { + for (x = 0; x < xcbc->blocksize; x++) xcbc->IV[x] ^= xcbc->K[1][x]; - } } else { /* (4) - b) If the blocksize of M[n] is less than 128 bits: + b) If the blocksize of M[n] is less than 128 bits: i) Pad M[n] with a single "1" bit, followed by the number of "0" bits (possibly none) required to increase M[n]'s @@ -125,14 +135,16 @@ int xcbc_done(aes_xcbc_state *xcbc, unsigned char *out, unsigned int *outlen) { */ xcbc->IV[xcbc->buflen] ^= 0x80; - for (x = 0; x < xcbc->blocksize; x++) { + + for (x = 0; x < xcbc->blocksize; x++) xcbc->IV[x] ^= xcbc->K[2][x]; - } } + SDRM_AES128_Encryption(out, xcbc->IV, xcbc->key); - if (outlen != NULL) { + + if (outlen != NULL) *outlen = xcbc->blocksize; - } + return 1; } diff --git a/ssflib/dep/uci/source/uci_api.c b/ssflib/dep/uci/source/uci_api.c index 7989065..a21522c 100644 --- a/ssflib/dep/uci/source/uci_api.c +++ b/ssflib/dep/uci/source/uci_api.c @@ -34,382 +34,431 @@ #if 1 #define TC_PRINT(fmt...) \ - do { printf(fmt);}while(0) + do { printf(fmt); } while (0) #else #define TC_PRINT(fmt...)\ - do {;}while(0) + do {; } while (0) #endif -/*! \brief print out by byte unit */ +/*! \brief print out by byte unit */ #undef PrintBYTE #define g_bTAdbug 1 #define TZ_PRINT(fmt...) \ - do {if (g_bTAdbug) printf("[SSFLIB] ");printf(fmt);}while(0) + do {if (g_bTAdbug) printf("[SSFLIB] "); printf(fmt); } while (0) #define TZ_ERROR(fmt...) \ - do {if (g_bTAdbug) printf("[SSFLIB] ");printf(fmt);}while(0) -#define PrintBYTE(msg, Data, DataLen) { \ - int idx; \ - TC_PRINT("%10s =", msg); \ - for(idx=0; idx<(int)DataLen; idx++) { \ - if((idx!=0) && ((idx%16)==0)) TC_PRINT("\n"); \ - if((idx % 4) == 0) TC_PRINT(" 0x"); \ - TC_PRINT("%.2x", Data[idx]); \ - } \ - TC_PRINT("\n"); \ -} - -int uci_context_alloc(unsigned int algorithm, uci_engine_config_e config, UCI_HANDLE* context) { + do {if (g_bTAdbug) printf("[SSFLIB] "); printf(fmt); } while (0) +#define PrintBYTE(msg, Data, DataLen) { \ + int idx; \ + TC_PRINT("%10s =", msg); \ + for (idx = 0; idx < (int)DataLen; idx++) { \ + if ((idx != 0) && ((idx%16) == 0)) TC_PRINT("\n"); \ + if ((idx % 4) == 0) TC_PRINT(" 0x"); \ + TC_PRINT("%.2x", Data[idx]); \ + } \ + TC_PRINT("\n"); \ + } + +int uci_context_alloc(unsigned int algorithm, uci_engine_config_e config, + UCI_HANDLE *context) +{ unsigned int conf = SDRM_LOW_HALF(config); uci_context_s *ctx; - if (context == NULL) { + if (context == NULL) return UCI_ERROR; - } - if (algorithm < ID_UCI_X931 || algorithm > ID_UCI_AE_CCM) { + + if (algorithm < ID_UCI_X931 || algorithm > ID_UCI_AE_CCM) return UCI_ERROR; - } + #if 0 - if(algorithm == ID_UCI_AE_GCM) - { + + if (algorithm == ID_UCI_AE_GCM) { ctx = OsaMalloc(sizeof(uci_context_s)); ctx->imp = OsaMalloc(sizeof(gcm_context)); ctx->alg = ID_UCI_AE_GCM; return (int)ctx; } - if(algorithm == ID_UCI_AE_CCM) - { + + if (algorithm == ID_UCI_AE_CCM) { ctx = OsaMalloc(sizeof(uci_context_s)); ctx->imp = OsaMalloc(sizeof(aes_ccm_context)); ctx->alg = ID_UCI_AE_CCM; return (int)ctx; } + #endif + if (algorithm == ID_UCI_XCBCMAC) { - ctx = (uci_context_s*)OsaMalloc(sizeof(uci_context_s)); - if (ctx == NULL) { + ctx = (uci_context_s *)OsaMalloc(sizeof(uci_context_s)); + + if (ctx == NULL) return UCI_ERROR; - } + ctx->imp = (aes_xcbc_state *)OsaMalloc(sizeof(aes_xcbc_state)); ctx->alg = ID_UCI_XCBCMAC; *context = ctx; return UCI_SUCCESS; } - if (conf == UCI_SW_CRYPTOCORE) { + + if (conf == UCI_SW_CRYPTOCORE) return cryptocore_context_alloc(algorithm, context); - } - if (conf == UCI_HW) { + + if (conf == UCI_HW) return hwcrypto_context_alloc(algorithm, config, context); - } return UCI_ERROR; } -int uci_context_free(UCI_HANDLE oh) { - uci_context_s *pctx = (uci_context_s*)oh; +int uci_context_free(UCI_HANDLE oh) +{ + uci_context_s *pctx = (uci_context_s *)oh; unsigned int conf; - if (pctx == NULL) { + + if (pctx == NULL) return UCI_INVALID_HANDLE; - } + conf = SDRM_LOW_HALF(pctx->config); + if (pctx->alg == ID_UCI_AE_GCM || pctx->alg == ID_UCI_AE_CCM - || pctx->alg == ID_UCI_XCBCMAC) { + || pctx->alg == ID_UCI_XCBCMAC) { OsaFree(pctx->imp); OsaFree(pctx); return UCI_SUCCESS; } - if (conf == UCI_SW_CRYPTOCORE) { + + if (conf == UCI_SW_CRYPTOCORE) return cryptocore_context_free(oh); - } - if (conf == UCI_HW) { + + if (conf == UCI_HW) return hwcrypto_context_free(oh); - } + return UCI_ERROR; } -int uci_md_init(UCI_HANDLE oh) { +int uci_md_init(UCI_HANDLE oh) +{ return cryptocore_md_init(oh); } -int uci_md_update(UCI_HANDLE oh, unsigned char *msg, unsigned int msg_len) { +int uci_md_update(UCI_HANDLE oh, unsigned char *msg, unsigned int msg_len) +{ return cryptocore_md_update(oh, msg, msg_len); } -int uci_md_final(UCI_HANDLE oh, unsigned char *output) { +int uci_md_final(UCI_HANDLE oh, unsigned char *output) +{ return cryptocore_md_final(oh, output); } int uci_md_get_hash(UCI_HANDLE oh, unsigned char *msg, unsigned int msg_len, - unsigned char *output) { + unsigned char *output) +{ - if (output == NULL) { + if (output == NULL) return UCI_ERROR; - } + return cryptocore_md_get_hash(oh, msg, msg_len, output); } -int uci_mac_init(UCI_HANDLE oh, unsigned char *key, unsigned int key_len) { +int uci_mac_init(UCI_HANDLE oh, unsigned char *key, unsigned int key_len) +{ int ret = 0; - uci_context_s *pctx = (uci_context_s*)oh; + uci_context_s *pctx = (uci_context_s *)oh; + if (pctx->alg == ID_UCI_XCBCMAC) { ret = xcbc_init((aes_xcbc_state *)(pctx->imp), key, key_len); - if (ret != 1) { + + if (ret != 1) return UCI_ERROR; - } else { + + else return UCI_SUCCESS; - } } + return cryptocore_mac_init(oh, key, key_len); } -int uci_mac_update(UCI_HANDLE oh, unsigned char *msg, unsigned int msg_len) { +int uci_mac_update(UCI_HANDLE oh, unsigned char *msg, unsigned int msg_len) +{ int ret = 0; - uci_context_s *pctx = (uci_context_s*)oh; + uci_context_s *pctx = (uci_context_s *)oh; + if (pctx->alg == ID_UCI_XCBCMAC) { - ret = xcbc_process((aes_xcbc_state*)(pctx->imp), msg, msg_len); - if (ret != 1) { + ret = xcbc_process((aes_xcbc_state *)(pctx->imp), msg, msg_len); + + if (ret != 1) return UCI_ERROR; - } else { + + else return UCI_SUCCESS; - } } return cryptocore_mac_update(oh, msg, msg_len); } int uci_mac_final(UCI_HANDLE oh, unsigned char *output, - unsigned int *output_len) { + unsigned int *output_len) +{ int ret = 0; - uci_context_s *pctx = (uci_context_s*)oh; + uci_context_s *pctx = (uci_context_s *)oh; + if (pctx->alg == ID_UCI_XCBCMAC) { - ret = xcbc_done((aes_xcbc_state*)(pctx->imp), output, output_len); - if (ret != 1) { + ret = xcbc_done((aes_xcbc_state *)(pctx->imp), output, output_len); + + if (ret != 1) return UCI_ERROR; - } else { + + else return UCI_SUCCESS; - } } return cryptocore_mac_final(oh, output, output_len); } int uci_mac_get_mac(UCI_HANDLE oh, unsigned char *key, unsigned int key_len, - unsigned char *msg, unsigned int msg_len, unsigned char *output, - unsigned int *output_len) { + unsigned char *msg, unsigned int msg_len, unsigned char *output, + unsigned int *output_len) +{ //int ret = 0; - uci_context_s *pctx = (uci_context_s*)oh; + uci_context_s *pctx = (uci_context_s *)oh; + if (pctx->alg == ID_UCI_XCBCMAC) { - if (xcbc_init((aes_xcbc_state *)(pctx->imp), key, key_len) != 1) { + if (xcbc_init((aes_xcbc_state *)(pctx->imp), key, key_len) != 1) return UCI_ERROR; - } - if (xcbc_process((aes_xcbc_state*)(pctx->imp), msg, msg_len) != 1) { + if (xcbc_process((aes_xcbc_state *)(pctx->imp), msg, msg_len) != 1) return UCI_ERROR; - } - if (xcbc_done((aes_xcbc_state*)(pctx->imp), output, output_len) != 1) { + if (xcbc_done((aes_xcbc_state *)(pctx->imp), output, output_len) != 1) return UCI_ERROR; - } + return UCI_SUCCESS; } + return cryptocore_mac_getmac(oh, key, key_len, msg, msg_len, output, - output_len); + output_len); } int uci_se_init(UCI_HANDLE oh, unsigned int mode, unsigned padding, - unsigned char *key, unsigned int key_len, unsigned char *iv) { - uci_context_s *pctx = (uci_context_s*)oh; - if (pctx == NULL) { + unsigned char *key, unsigned int key_len, unsigned char *iv) +{ + uci_context_s *pctx = (uci_context_s *)oh; + + if (pctx == NULL) return UCI_INVALID_HANDLE; - } + unsigned conf = SDRM_LOW_HALF(pctx->config); - if (conf == UCI_SW_CRYPTOCORE) { + + if (conf == UCI_SW_CRYPTOCORE) return cryptocore_se_init(oh, mode, padding, key, key_len, iv); - } - if (conf == UCI_HW) { + + if (conf == UCI_HW) return hwcrypto_se_init(oh, mode, padding, key, key_len, iv); - } + return UCI_ERROR; } int uci_se_process(UCI_HANDLE oh, unsigned char *input, unsigned int input_len, - unsigned char *output, unsigned int *output_len) { + unsigned char *output, unsigned int *output_len) +{ + + uci_context_s *pctx = (uci_context_s *)oh; - uci_context_s *pctx = (uci_context_s*)oh; - if (pctx == NULL) { + if (pctx == NULL) return UCI_INVALID_HANDLE; - } + unsigned conf = SDRM_LOW_HALF(pctx->config); - if (input != NULL && output == NULL) { + + if (input != NULL && output == NULL) return UCI_ERROR; - } - if (conf == UCI_SW_CRYPTOCORE) { + + if (conf == UCI_SW_CRYPTOCORE) return cryptocore_se_process(oh, input, input_len, output, output_len); - } - if (conf == UCI_HW) { + + if (conf == UCI_HW) return hwcrypto_se_process(oh, input, input_len, output, output_len); - } + return UCI_ERROR; } int uci_se_final(UCI_HANDLE oh, unsigned char *input, unsigned int input_len, - unsigned char *output, unsigned int *output_len) { - uci_context_s *pctx = (uci_context_s*)oh; - if (pctx == NULL) { + unsigned char *output, unsigned int *output_len) +{ + uci_context_s *pctx = (uci_context_s *)oh; + + if (pctx == NULL) return UCI_INVALID_HANDLE; - } + unsigned conf = SDRM_LOW_HALF(pctx->config); + if (input != NULL && output == NULL) { TZ_ERROR("UCI_ERROR error line = %d,%s\n", __LINE__, __func__); return UCI_ERROR; } - if (conf == UCI_SW_CRYPTOCORE) { + + if (conf == UCI_SW_CRYPTOCORE) return cryptocore_se_final(oh, input, input_len, output, output_len); - } - if (conf == UCI_HW) { + + if (conf == UCI_HW) return hwcrypto_se_final(oh, input, input_len, output, output_len); - } + TZ_ERROR("UCI_ERROR error line = %d,%s\n", __LINE__, __func__); return UCI_ERROR; } int uci_se_encrypt_oneblock(UCI_HANDLE oh, unsigned char *cipher_text, - unsigned char *plain_text, unsigned char *user_key) { - if (cipher_text == NULL || plain_text == NULL || user_key == NULL) { + unsigned char *plain_text, unsigned char *user_key) +{ + if (cipher_text == NULL || plain_text == NULL || user_key == NULL) return UCI_ERROR; - } + return cryptocore_se_encrypt_oneblock(oh, cipher_text, plain_text, user_key); } int uci_se_decrypt_oneblock(UCI_HANDLE oh, unsigned char *plain_text, - unsigned char *cipher_text, unsigned char *user_key) { - if (cipher_text == NULL || plain_text == NULL || user_key == NULL) { + unsigned char *cipher_text, unsigned char *user_key) +{ + if (cipher_text == NULL || plain_text == NULL || user_key == NULL) return UCI_ERROR; - } + return cryptocore_se_decrypt_oneblock(oh, plain_text, cipher_text, user_key); } int uci_wbse_init(UCI_HANDLE oh, int flag, unsigned char *key, - char *table_filepath, void *pencoder1, void *pencoder2) { + char *table_filepath, void *pencoder1, void *pencoder2) +{ return UCI_ERROR; } int uci_wbse_final(UCI_HANDLE oh, unsigned char *input, unsigned int input_len, - unsigned char *output, unsigned int output_len) { + unsigned char *output, unsigned int output_len) +{ return UCI_ERROR; } -int uci_ae_gen_param(UCI_HANDLE oh, uci_param_s *param, unsigned int size) { +int uci_ae_gen_param(UCI_HANDLE oh, uci_param_s *param, unsigned int size) +{ return cryptocore_ae_gen_param(oh, param, size); } int uci_ae_gen_keypair(UCI_HANDLE oh, uci_key_s *keymaterial, - uci_param_s *param) { - if (keymaterial == NULL) { + uci_param_s *param) +{ + if (keymaterial == NULL) return UCI_ERROR; - } + return cryptocore_ae_gen_keypair(oh, keymaterial, param); } int uci_ae_set_keypair(UCI_HANDLE oh, uci_key_s *keymaterial, - uci_param_s *param) { - if (keymaterial == NULL) { + uci_param_s *param) +{ + if (keymaterial == NULL) return UCI_ERROR; - } + return cryptocore_ae_set_keypair(oh, keymaterial, param); } int uci_ae_encrypt(UCI_HANDLE oh, unsigned char *input, unsigned int input_len, - unsigned char *output, unsigned int *output_len) { + unsigned char *output, unsigned int *output_len) +{ return cryptocore_ae_encrypt(oh, input, input_len, output, output_len); } int uci_ae_decrypt(UCI_HANDLE oh, unsigned char *input, unsigned int input_len, - unsigned char *output, unsigned int *output_len) { + unsigned char *output, unsigned int *output_len) +{ return cryptocore_ae_decrypt(oh, input, input_len, output, output_len); } int uci_ae_decryptbycrt(UCI_HANDLE oh, unsigned char *input, - unsigned int input_len, unsigned char *output, unsigned int *output_len) { + unsigned int input_len, unsigned char *output, unsigned int *output_len) +{ return cryptocore_ae_decryptbycrt(oh, input, input_len, output, output_len); } int uci_wbae_encrypt(UCI_HANDLE oh, unsigned char *input, - unsigned int input_len, unsigned char *output, unsigned int *output_len) { + unsigned int input_len, unsigned char *output, unsigned int *output_len) +{ return UCI_ERROR; } int uci_wbae_decrypt(UCI_HANDLE oh, unsigned char *input, - unsigned int input_len, unsigned char *output, unsigned int *output_len) { + unsigned int input_len, unsigned char *output, unsigned int *output_len) +{ return UCI_ERROR; } int uci_ds_sign(UCI_HANDLE oh, unsigned char *hash, unsigned int hash_len, - unsigned char *signature, unsigned int *sign_len) { + unsigned char *signature, unsigned int *sign_len) +{ return cryptocore_ds_sign(oh, hash, hash_len, signature, sign_len); } int uci_ds_verify(UCI_HANDLE oh, unsigned char *hash, unsigned int hash_len, - unsigned char *signature, unsigned int sign_len, int *result) { + unsigned char *signature, unsigned int sign_len, int *result) +{ return cryptocore_ds_verify(oh, hash, hash_len, signature, sign_len, result); } int uci_dh_gen_phasekey(UCI_HANDLE oh, unsigned char *pch_xk, - unsigned char *pch_xv, uci_param_s *param) { + unsigned char *pch_xv, uci_param_s *param) +{ return cryptocore_dh_gen_dh1stphasekey(oh, pch_xk, pch_xv, param); } int uci_dh_gen_authkey(UCI_HANDLE oh, unsigned char *pch_xk, - unsigned char *pch_xv, unsigned char *pch_kauth) { + unsigned char *pch_xv, unsigned char *pch_kauth) +{ return cryptocore_dh_gen_dhkey(oh, pch_xk, pch_xv, pch_kauth); } -int uci_prng_seed(UCI_HANDLE oh, unsigned char * seed) { +int uci_prng_seed(UCI_HANDLE oh, unsigned char *seed) +{ return cryptocore_prng_seed(oh, seed); } -int uci_prng_get(UCI_HANDLE oh, unsigned int bit_len, unsigned char *data) { +int uci_prng_get(UCI_HANDLE oh, unsigned int bit_len, unsigned char *data) +{ return cryptocore_prng_get(oh, bit_len, data); } int uci_authcrypt_init(UCI_HANDLE oh, unsigned int mode, unsigned char *nonce, - unsigned int nonce_len, unsigned int tag_len, unsigned int aad_len, - unsigned int payload_len, unsigned char *key, unsigned int key_len) { + unsigned int nonce_len, unsigned int tag_len, unsigned int aad_len, + unsigned int payload_len, unsigned char *key, unsigned int key_len) +{ #if 0 - uci_context_s *pctx = (uci_context_s*)oh; + uci_context_s *pctx = (uci_context_s *)oh; gcm_context *gctx = NULL; aes_ccm_context *cctx = NULL; int ret; - if(pctx->alg == ID_UCI_AE_GCM) - { + + if (pctx->alg == ID_UCI_AE_GCM) { gctx = (gcm_context *)pctx->imp; - if(gcm_init(gctx, key, key_len) != 0) - { + + if (gcm_init(gctx, key, key_len) != 0) return UCI_ERROR; - } - if(gcm_starts(gctx, mode, nonce, nonce_len) != 0) - { + + if (gcm_starts(gctx, mode, nonce, nonce_len) != 0) return UCI_ERROR; - } + pctx->flag = tag_len; - } - else if(pctx->alg == ID_UCI_AE_CCM) - { + } else if (pctx->alg == ID_UCI_AE_CCM) { cctx = (aes_ccm_context *)pctx->imp; - ret = aes_ccm_init(cctx, mode, key, key_len, nonce, nonce_len, tag_len, aad_len, payload_len); - if(ret != 0) - { + ret = aes_ccm_init(cctx, mode, key, key_len, nonce, nonce_len, tag_len, aad_len, + payload_len); + + if (ret != 0) { printf("aes_ccm_init error. ret = %d\n ", ret); return UCI_ERROR; } - } - else - { + } else { printf("alg type erro \n"); return UCI_ERROR; @@ -420,272 +469,271 @@ int uci_authcrypt_init(UCI_HANDLE oh, unsigned int mode, unsigned char *nonce, return UCI_ERROR; } int uci_authcrypt_update_aad(UCI_HANDLE oh, unsigned char *aad, - unsigned int aad_len) { + unsigned int aad_len) +{ #if 0 - uci_context_s *pctx = (uci_context_s*)oh; + uci_context_s *pctx = (uci_context_s *)oh; gcm_context *gctx; aes_ccm_context *cctx; - if(pctx->alg == ID_UCI_AE_GCM) - { + + if (pctx->alg == ID_UCI_AE_GCM) { gctx = (gcm_context *)pctx->imp; - if(gcm_update_add(gctx, aad, aad_len) != 0) - { + + if (gcm_update_add(gctx, aad, aad_len) != 0) return UCI_ERROR; - } - } - else if(pctx->alg == ID_UCI_AE_CCM) - { + } else if (pctx->alg == ID_UCI_AE_CCM) { cctx = (aes_ccm_context *)pctx->imp; - if(aes_ccm_update_aad(cctx,aad,aad_len) != 0) - { + + if (aes_ccm_update_aad(cctx, aad, aad_len) != 0) return UCI_ERROR; - } - } - else - { + } else return UCI_ERROR; - } return UCI_SUCCESS; #endif return UCI_ERROR; } int uci_authcrypt_update(UCI_HANDLE oh, unsigned char *src, - unsigned int src_len, unsigned char *dest, unsigned int *dest_len) { + unsigned int src_len, unsigned char *dest, unsigned int *dest_len) +{ #if 0 - uci_context_s *pctx = (uci_context_s*)oh; + uci_context_s *pctx = (uci_context_s *)oh; gcm_context *gctx; aes_ccm_context *cctx; - if(pctx->alg == ID_UCI_AE_GCM) - { + if (pctx->alg == ID_UCI_AE_GCM) { gctx = (gcm_context *)pctx->imp; - if(gcm_update(gctx,src_len, src, dest) != 0) - { + + if (gcm_update(gctx, src_len, src, dest) != 0) return UCI_ERROR; - } + *dest_len = src_len; - } - else if(pctx->alg == ID_UCI_AE_CCM) - { - cctx = (aes_ccm_context*)pctx->imp; - if(aes_ccm_process(cctx,src,src_len,dest,dest_len,NULL,NULL,0) != 0) - { + } else if (pctx->alg == ID_UCI_AE_CCM) { + cctx = (aes_ccm_context *)pctx->imp; + + if (aes_ccm_process(cctx, src, src_len, dest, dest_len, NULL, NULL, 0) != 0) return UCI_ERROR; - } - } - else - { + } else return UCI_ERROR; - } return UCI_SUCCESS; #endif return UCI_ERROR; } int uci_authcrypt_encryptfinal(UCI_HANDLE oh, unsigned char *src, - unsigned int src_len, unsigned char *dest, unsigned int *dest_len, - unsigned char *tag, unsigned int *tag_len) { + unsigned int src_len, unsigned char *dest, unsigned int *dest_len, + unsigned char *tag, unsigned int *tag_len) +{ #if 0 - uci_context_s *pctx = (uci_context_s*)oh; + uci_context_s *pctx = (uci_context_s *)oh; gcm_context *gctx = NULL; aes_ccm_context *cctx = NULL; int ret; - if(pctx->alg == ID_UCI_AE_GCM) - { + + if (pctx->alg == ID_UCI_AE_GCM) { gctx = (gcm_context *)pctx->imp; - if(gcm_update(gctx,src_len, src, dest) != 0) - { + + if (gcm_update(gctx, src_len, src, dest) != 0) return UCI_ERROR; - } - if(dest_len != NULL) - { + + if (dest_len != NULL) *dest_len = src_len; - } - if((ret = gcm_finish(gctx, tag, pctx->flag)) != 0) - { - printf("ERROR %d\n",ret); + + if ((ret = gcm_finish(gctx, tag, pctx->flag)) != 0) { + printf("ERROR %d\n", ret); return UCI_ERROR; } - if(tag_len != NULL) - { - *tag_len= pctx->flag; - } - } - else if(pctx->alg == ID_UCI_AE_CCM) - { - cctx = (aes_ccm_context*)pctx->imp; - if(aes_ccm_process(cctx,src,src_len,dest,dest_len,tag,tag_len,1) != 0) - { + + if (tag_len != NULL) + *tag_len = pctx->flag; + } else if (pctx->alg == ID_UCI_AE_CCM) { + cctx = (aes_ccm_context *)pctx->imp; + + if (aes_ccm_process(cctx, src, src_len, dest, dest_len, tag, tag_len, 1) != 0) return UCI_ERROR; - } - } - else - { + } else return UCI_ERROR; - } + return UCI_SUCCESS; #endif return UCI_ERROR; } int uci_authcrypt_decryptfinal(UCI_HANDLE oh, unsigned char *src, - unsigned int src_len, unsigned char *dest, unsigned int *dest_len, - unsigned char *tag, unsigned int tag_len) { + unsigned int src_len, unsigned char *dest, unsigned int *dest_len, + unsigned char *tag, unsigned int tag_len) +{ #if 0 - uci_context_s *pctx = (uci_context_s*)oh; + uci_context_s *pctx = (uci_context_s *)oh; gcm_context *gctx; aes_ccm_context *cctx; - unsigned char tmp[16] = - { 0x0,}; + unsigned char tmp[16] = {0x0,}; unsigned int len = 0; len = tag_len; - if(pctx->alg == ID_UCI_AE_GCM) - { + + if (pctx->alg == ID_UCI_AE_GCM) { gctx = (gcm_context *)pctx->imp; - if(gcm_update(gctx,src_len, src, dest) != 0) - { + + if (gcm_update(gctx, src_len, src, dest) != 0) return UCI_ERROR; - } + *dest_len = src_len; - if(gcm_finish(gctx, tmp, pctx->flag) != 0) - { + + if (gcm_finish(gctx, tmp, pctx->flag) != 0) { printf("gcm_finish error \n"); return UCI_ERROR; } - if(memcmp(tmp, tag, tag_len) != 0) - { - PrintBYTE("tmp",tmp,tag_len); - PrintBYTE("tag",tag,tag_len); + + if (memcmp(tmp, tag, tag_len) != 0) { + PrintBYTE("tmp", tmp, tag_len); + PrintBYTE("tag", tag, tag_len); printf("tag not right \n"); return UCI_ERROR; } - } - else if(pctx->alg == ID_UCI_AE_CCM) - { - cctx = (aes_ccm_context*)pctx->imp; - if(aes_ccm_process(cctx,src,src_len,dest,dest_len,tag,&len,1) != 0) - { + } else if (pctx->alg == ID_UCI_AE_CCM) { + cctx = (aes_ccm_context *)pctx->imp; + + if (aes_ccm_process(cctx, src, src_len, dest, dest_len, tag, &len, 1) != 0) return UCI_ERROR; - } - } - else - { + } else return UCI_ERROR; - } + return UCI_SUCCESS; #endif return UCI_ERROR; } -int uci_dup_handle(UCI_HANDLE srcoh, UCI_HANDLE* destoh) { +int uci_dup_handle(UCI_HANDLE srcoh, UCI_HANDLE *destoh) +{ uci_context_s *srcctx = (uci_context_s *)srcoh; uci_context_s *destctx = NULL; - if (destoh == NULL) { + if (destoh == NULL) return UCI_ERROR; - } - int ret = uci_context_alloc(srcctx->alg, (uci_engine_config_e)srcctx->config, destoh); - if (ret != UCI_SUCCESS) { + int ret = uci_context_alloc(srcctx->alg, (uci_engine_config_e)srcctx->config, + destoh); + + if (ret != UCI_SUCCESS) return ret; - } destctx = (uci_context_s *)(*destoh); - if (destctx == NULL) { + + if (destctx == NULL) return UCI_ERROR; - } switch (srcctx->alg) { #if 0 - case ID_UCI_AE_GCM: + + case ID_UCI_AE_GCM: memcpy(destctx->imp, srcctx->imp, sizeof(gcm_context)); break; - case ID_UCI_AE_CCM: + + case ID_UCI_AE_CCM: memcpy(destctx->imp, srcctx->imp, sizeof(aes_ccm_context)); break; #endif - case ID_UCI_X931: - memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_X931Context)); - break; - case ID_UCI_MD5: - memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_MD5Context)); - break; - case ID_UCI_SHA1: - memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_SHA1Context)); - break; - case ID_UCI_SHA224: - memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_SHA224Context)); - break; - case ID_UCI_SHA256: - memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_SHA256Context)); - break; + + case ID_UCI_X931: + memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_X931Context)); + break; + + case ID_UCI_MD5: + memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_MD5Context)); + break; + + case ID_UCI_SHA1: + memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_SHA1Context)); + break; + + case ID_UCI_SHA224: + memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_SHA224Context)); + break; + + case ID_UCI_SHA256: + memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_SHA256Context)); + break; #ifndef _OP64_NOTSUPPORTED - case ID_UCI_SHA384: - memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_SHA384Context)); - break; - case ID_UCI_SHA512: - memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_SHA512Context)); - break; + + case ID_UCI_SHA384: + memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_SHA384Context)); + break; + + case ID_UCI_SHA512: + memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_SHA512Context)); + break; #endif - case ID_UCI_CMAC: - memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_CMACContext)); - break; - case ID_UCI_XCBCMAC: - memcpy(destctx->imp, srcctx->imp, sizeof(aes_xcbc_state)); - break; - case ID_UCI_HMD5: - case ID_UCI_HSHA1: - case ID_UCI_HSHA256: - case ID_UCI_HSHA224: + + case ID_UCI_CMAC: + memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_CMACContext)); + break; + + case ID_UCI_XCBCMAC: + memcpy(destctx->imp, srcctx->imp, sizeof(aes_xcbc_state)); + break; + + case ID_UCI_HMD5: + case ID_UCI_HSHA1: + case ID_UCI_HSHA256: + case ID_UCI_HSHA224: #ifndef _OP64_NOTSUPPORTED - case ID_UCI_HSHA384: - case ID_UCI_HSHA512: + case ID_UCI_HSHA384: + case ID_UCI_HSHA512: #endif //_OP64_NOTSUPPORTED - memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_HMACContext)); - break; - case ID_UCI_AES128: - case ID_UCI_AES192: - case ID_UCI_AES256: - memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_AESContext)); - break; - case ID_UCI_DES: - memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_DESContext)); - break; - case ID_UCI_TDES: - memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_TDESContext)); - break; - case ID_UCI_RC4: - memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_RC4Context)); - break; - case ID_UCI_SNOW2: - memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_SNOW2Context)); - break; - case ID_UCI_RSA512: - case ID_UCI_RSA: - case ID_UCI_RSA1024: - case ID_UCI_RSA2048: - case ID_UCI_RSA3072: - case ID_UCI_RSA4096: - memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_RSAContext)); - break; - case ID_UCI_DSA: - memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_DSAContext)); - break; - case ID_UCI_ECDSA: - memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_ECDSAContext)); - break; - case ID_UCI_ECDH: - memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_ECDHContext)); - break; - case ID_UCI_DH: - memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_DHContext)); - break; + memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_HMACContext)); + break; + + case ID_UCI_AES128: + case ID_UCI_AES192: + case ID_UCI_AES256: + memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_AESContext)); + break; + + case ID_UCI_DES: + memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_DESContext)); + break; + + case ID_UCI_TDES: + memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_TDESContext)); + break; + + case ID_UCI_RC4: + memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_RC4Context)); + break; + + case ID_UCI_SNOW2: + memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_SNOW2Context)); + break; + + case ID_UCI_RSA512: + case ID_UCI_RSA: + case ID_UCI_RSA1024: + case ID_UCI_RSA2048: + case ID_UCI_RSA3072: + case ID_UCI_RSA4096: + memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_RSAContext)); + break; + + case ID_UCI_DSA: + memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_DSAContext)); + break; + + case ID_UCI_ECDSA: + memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_ECDSAContext)); + break; + + case ID_UCI_ECDH: + memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_ECDHContext)); + break; + + case ID_UCI_DH: + memcpy(destctx->imp, srcctx->imp, sizeof(SDRM_DHContext)); + break; } return UCI_SUCCESS; diff --git a/ssflib/dep/uci/source/uci_cryptocore.c b/ssflib/dep/uci/source/uci_cryptocore.c index 2824ee7..6a87eb1 100644 --- a/ssflib/dep/uci/source/uci_cryptocore.c +++ b/ssflib/dep/uci/source/uci_cryptocore.c @@ -29,285 +29,302 @@ #include "uci_aes_xcbc_mac.h" #define g_bTAdbug 1 #define TZ_PRINT(fmt...) \ - do {if (g_bTAdbug) printf("[SSFLIB] ");printf(fmt);}while(0) + do {if (g_bTAdbug) printf("[SSFLIB] "); printf(fmt); } while (0) #define TZ_ERROR(fmt...) \ - do {if (g_bTAdbug) printf("[SSFLIB] ");printf(fmt);}while(0) + do {if (g_bTAdbug) printf("[SSFLIB] "); printf(fmt); } while (0) -int cryptocore_context_alloc(unsigned int algorithm, UCI_HANDLE* context) { - uci_context_s* ctx; +int cryptocore_context_alloc(unsigned int algorithm, UCI_HANDLE *context) +{ + uci_context_s *ctx; CryptoCoreContainer *crt; - if (context == NULL) { + if (context == NULL) return UCI_ERROR; - } - ctx = (uci_context_s*)OsaMalloc(sizeof(uci_context_s)); - if (ctx == NULL) { + ctx = (uci_context_s *)OsaMalloc(sizeof(uci_context_s)); + + if (ctx == NULL) return UCI_MEM_ALLOR_ERROR; - } crt = create_CryptoCoreContainer(algorithm); + if (crt == NULL) { OsaFree(ctx); return UCI_MEM_ALLOR_ERROR; } - ctx->imp = (void*)crt; + + ctx->imp = (void *)crt; ctx->config = UCI_SW; ctx->alg = algorithm; *context = ctx; return UCI_SUCCESS; } -int cryptocore_context_free(UCI_HANDLE oh) { - uci_context_s *pctx = (uci_context_s*)oh; - if (pctx == NULL) { +int cryptocore_context_free(UCI_HANDLE oh) +{ + uci_context_s *pctx = (uci_context_s *)oh; + + if (pctx == NULL) return UCI_INVALID_HANDLE; - } - destroy_CryptoCoreContainer((CryptoCoreContainer*)pctx->imp); + + destroy_CryptoCoreContainer((CryptoCoreContainer *)pctx->imp); OsaFree(pctx); pctx = NULL; return UCI_SUCCESS; } -int cryptocore_md_init(UCI_HANDLE oh) { +int cryptocore_md_init(UCI_HANDLE oh) +{ int ret; - uci_context_s *pctx = (uci_context_s*)oh; - if (pctx == NULL) { + uci_context_s *pctx = (uci_context_s *)oh; + + if (pctx == NULL) return UCI_INVALID_HANDLE; - } - if (pctx->config != UCI_SW) { + if (pctx->config != UCI_SW) return UCI_INVALID_HANDLE; - } ret = ((CryptoCoreContainer *)pctx->imp)->MD_init( - (CryptoCoreContainer*)(pctx->imp)); - if (ret != CRYPTO_SUCCESS) { + (CryptoCoreContainer *)(pctx->imp)); + + if (ret != CRYPTO_SUCCESS) return UCI_ERROR; - } + return UCI_SUCCESS; } int cryptocore_md_update(UCI_HANDLE oh, unsigned char *msg, - unsigned int msg_len) { + unsigned int msg_len) +{ int ret; - uci_context_s *pctx = (uci_context_s*)oh; - if (pctx == NULL) { + uci_context_s *pctx = (uci_context_s *)oh; + + if (pctx == NULL) return UCI_INVALID_HANDLE; - } - if (pctx->config != UCI_SW) { + if (pctx->config != UCI_SW) return UCI_INVALID_HANDLE; - } ret = ((CryptoCoreContainer *)pctx->imp)->MD_update( - ((CryptoCoreContainer*)pctx->imp), msg, msg_len); - if (ret != CRYPTO_SUCCESS) { + ((CryptoCoreContainer *)pctx->imp), msg, msg_len); + + if (ret != CRYPTO_SUCCESS) return UCI_ERROR; - } + return UCI_SUCCESS; } -int cryptocore_md_final(UCI_HANDLE oh, unsigned char *output) { +int cryptocore_md_final(UCI_HANDLE oh, unsigned char *output) +{ int ret; - uci_context_s *pctx = (uci_context_s*)oh; - if (pctx == NULL) { + uci_context_s *pctx = (uci_context_s *)oh; + + if (pctx == NULL) return UCI_INVALID_HANDLE; - } - if (pctx->config != UCI_SW) { + if (pctx->config != UCI_SW) return UCI_INVALID_HANDLE; - } ret = ((CryptoCoreContainer *)pctx->imp)->MD_final( - (CryptoCoreContainer*)pctx->imp, output); - if (ret != CRYPTO_SUCCESS) { + (CryptoCoreContainer *)pctx->imp, output); + + if (ret != CRYPTO_SUCCESS) return UCI_ERROR; - } + return UCI_SUCCESS; } int cryptocore_md_get_hash(UCI_HANDLE oh, unsigned char *msg, - unsigned int msg_len, unsigned char * output) { + unsigned int msg_len, unsigned char *output) +{ int ret; - uci_context_s *pctx = (uci_context_s*)oh; - if (pctx == NULL) { + uci_context_s *pctx = (uci_context_s *)oh; + + if (pctx == NULL) return UCI_INVALID_HANDLE; - } - if (pctx->config != UCI_SW) { + if (pctx->config != UCI_SW) return UCI_INVALID_HANDLE; - } ret = ((CryptoCoreContainer *)pctx->imp)->MD_getHASH( - (CryptoCoreContainer*)pctx->imp, msg, msg_len, output); - if (ret != CRYPTO_SUCCESS) { + (CryptoCoreContainer *)pctx->imp, msg, msg_len, output); + + if (ret != CRYPTO_SUCCESS) return UCI_ERROR; - } + return UCI_SUCCESS; } -int cryptocore_mac_init(UCI_HANDLE oh, unsigned char *key, unsigned int key_len) { +int cryptocore_mac_init(UCI_HANDLE oh, unsigned char *key, + unsigned int key_len) +{ int ret; - uci_context_s *pctx = (uci_context_s*)oh; - if (pctx == NULL) { + uci_context_s *pctx = (uci_context_s *)oh; + + if (pctx == NULL) return UCI_INVALID_HANDLE; - } - if (pctx->config != UCI_SW) { + if (pctx->config != UCI_SW) return UCI_INVALID_HANDLE; - } + ret = ((CryptoCoreContainer *)pctx->imp)->MAC_init( - (CryptoCoreContainer*)(pctx->imp), key, key_len); - if (ret != CRYPTO_SUCCESS) { + (CryptoCoreContainer *)(pctx->imp), key, key_len); + + if (ret != CRYPTO_SUCCESS) return UCI_ERROR; - } + return UCI_SUCCESS; } int cryptocore_mac_update(UCI_HANDLE oh, unsigned char *msg, - unsigned int msg_len) { + unsigned int msg_len) +{ int ret; - uci_context_s *pctx = (uci_context_s*)oh; - if (pctx == NULL) { + uci_context_s *pctx = (uci_context_s *)oh; + + if (pctx == NULL) return UCI_INVALID_HANDLE; - } - if (pctx->config != UCI_SW) { + if (pctx->config != UCI_SW) return UCI_INVALID_HANDLE; - } + ret = ((CryptoCoreContainer *)pctx->imp)->MAC_update( - (CryptoCoreContainer*)(pctx->imp), msg, msg_len); - if (ret != CRYPTO_SUCCESS) { + (CryptoCoreContainer *)(pctx->imp), msg, msg_len); + + if (ret != CRYPTO_SUCCESS) return UCI_ERROR; - } + return UCI_SUCCESS; } int cryptocore_mac_final(UCI_HANDLE oh, unsigned char *output, - unsigned int *output_len) { + unsigned int *output_len) +{ int ret; - uci_context_s *pctx = (uci_context_s*)oh; - if (pctx == NULL) { + uci_context_s *pctx = (uci_context_s *)oh; + + if (pctx == NULL) return UCI_INVALID_HANDLE; - } - if (pctx->config != UCI_SW) { + if (pctx->config != UCI_SW) return UCI_INVALID_HANDLE; - } ret = ((CryptoCoreContainer *)pctx->imp)->MAC_final( - (CryptoCoreContainer*)(pctx->imp), output, output_len); - if (ret != CRYPTO_SUCCESS) { + (CryptoCoreContainer *)(pctx->imp), output, output_len); + + if (ret != CRYPTO_SUCCESS) return UCI_ERROR; - } + return UCI_SUCCESS; } int cryptocore_mac_getmac(UCI_HANDLE oh, unsigned char *key, - unsigned int key_len, unsigned char *msg, unsigned int msg_len, - unsigned char *output, unsigned int *output_len) { + unsigned int key_len, unsigned char *msg, unsigned int msg_len, + unsigned char *output, unsigned int *output_len) +{ int ret; - uci_context_s *pctx = (uci_context_s*)oh; - if (pctx == NULL) { + uci_context_s *pctx = (uci_context_s *)oh; + + if (pctx == NULL) return UCI_INVALID_HANDLE; - } - if (pctx->config != UCI_SW) { + if (pctx->config != UCI_SW) return UCI_INVALID_HANDLE; - } ret = ((CryptoCoreContainer *)pctx->imp)->MAC_getMAC( - (CryptoCoreContainer*)(pctx->imp), key, key_len, msg, msg_len, output, - output_len); - if (ret != CRYPTO_SUCCESS) { + (CryptoCoreContainer *)(pctx->imp), key, key_len, msg, msg_len, output, + output_len); + + if (ret != CRYPTO_SUCCESS) return UCI_ERROR; - } + return UCI_SUCCESS; } int cryptocore_se_init(UCI_HANDLE oh, unsigned int mode, unsigned padding, - unsigned char *key, unsigned int key_len, unsigned char *iv) { + unsigned char *key, unsigned int key_len, unsigned char *iv) +{ int ret; - uci_context_s *pctx = (uci_context_s*)oh; - if (pctx == NULL) { + uci_context_s *pctx = (uci_context_s *)oh; + + if (pctx == NULL) return UCI_INVALID_HANDLE; - } - if (pctx->config != UCI_SW) { + if (pctx->config != UCI_SW) return UCI_INVALID_HANDLE; - } + //deal with CTS base CBC if (mode == ID_UCI_ENC_CTS || mode == ID_UCI_DEC_CTS) { pctx->flag = mode; + if (mode == ID_UCI_ENC_CTS) { ret = ((CryptoCoreContainer *)pctx->imp)->SE_init( - (CryptoCoreContainer*)(pctx->imp), ID_UCI_ENC_CBC, - ID_UCI_ZERO_PADDING, key, key_len, iv); + (CryptoCoreContainer *)(pctx->imp), ID_UCI_ENC_CBC, + ID_UCI_ZERO_PADDING, key, key_len, iv); } else { ret = ((CryptoCoreContainer *)pctx->imp)->SE_init( - (CryptoCoreContainer*)(pctx->imp), ID_UCI_DEC_CBC, - ID_UCI_ZERO_PADDING, key, key_len, iv); + (CryptoCoreContainer *)(pctx->imp), ID_UCI_DEC_CBC, + ID_UCI_ZERO_PADDING, key, key_len, iv); } } else { ret = ((CryptoCoreContainer *)pctx->imp)->SE_init( - (CryptoCoreContainer*)(pctx->imp), mode, padding, key, key_len, iv); + (CryptoCoreContainer *)(pctx->imp), mode, padding, key, key_len, iv); } - if (ret == CRYPTO_INVALID_ARGUMENT) { + + if (ret == CRYPTO_INVALID_ARGUMENT) return UCI_INVALID_ARGUMENT; - } - if (ret != CRYPTO_SUCCESS) { + + if (ret != CRYPTO_SUCCESS) return UCI_ERROR; - } return UCI_SUCCESS; } int cryptocore_se_process(UCI_HANDLE oh, unsigned char *input, - unsigned int input_len, unsigned char *output, unsigned int *output_len) { + unsigned int input_len, unsigned char *output, unsigned int *output_len) +{ int ret; - uci_context_s *pctx = (uci_context_s*)oh; - if (pctx == NULL) { + uci_context_s *pctx = (uci_context_s *)oh; + + if (pctx == NULL) return UCI_INVALID_HANDLE; - } - if (pctx->config != UCI_SW) { + if (pctx->config != UCI_SW) return UCI_INVALID_HANDLE; - } ret = ((CryptoCoreContainer *)pctx->imp)->SE_process( - (CryptoCoreContainer*)(pctx->imp), input, input_len, output, output_len); - if (ret != CRYPTO_SUCCESS) { + (CryptoCoreContainer *)(pctx->imp), input, input_len, output, output_len); + + if (ret != CRYPTO_SUCCESS) return UCI_ERROR; - } + return UCI_SUCCESS; } int cryptocore_se_final(UCI_HANDLE oh, unsigned char *input, - unsigned int input_len, unsigned char *output, unsigned int *output_len) { + unsigned int input_len, unsigned char *output, unsigned int *output_len) +{ int ret; unsigned int lastblocksize = 0; unsigned char lastblock[SDRM_AES_BLOCK_SIZ]; unsigned char secondlastblock[SDRM_AES_BLOCK_SIZ]; unsigned char aIV[SDRM_AES_BLOCK_SIZ] = {0x0, }; - uci_context_s *pctx = (uci_context_s*)oh; + uci_context_s *pctx = (uci_context_s *)oh; unsigned char *psecondlastblk = NULL; //point to second last block unsigned char *plastblk = NULL; //point to last block CryptoCoreContainer *crt = NULL; - if (pctx == NULL) { + + if (pctx == NULL) return UCI_INVALID_HANDLE; - } - if (pctx->config != UCI_SW) { + if (pctx->config != UCI_SW) return UCI_INVALID_HANDLE; - } - crt = (CryptoCoreContainer*)(pctx->imp); + crt = (CryptoCoreContainer *)(pctx->imp); - if (input_len <= SDRM_AES_BLOCK_SIZ) { + if (input_len <= SDRM_AES_BLOCK_SIZ) goto final; - } plastblk = input + SDRM_AES_BLOCK_SIZ; psecondlastblk = input; @@ -317,35 +334,38 @@ int cryptocore_se_final(UCI_HANDLE oh, unsigned char *input, #if 0 lastblocksize = input_len % SDRM_AES_BLOCK_SIZ; - if(lastblocksize == 0) - { + + if (lastblocksize == 0) lastblocksize = 16; - } - ret = crt->SE_process(crt, psecondlastblk, SDRM_AES_BLOCK_SIZ, lastblock, output_len); - if(ret!=CRYPTO_SUCCESS) - { + ret = crt->SE_process(crt, psecondlastblk, SDRM_AES_BLOCK_SIZ, lastblock, + output_len); + + if (ret != CRYPTO_SUCCESS) return UCI_ERROR; - } + #endif - if (input_len % SDRM_AES_BLOCK_SIZ == 0) { + + if (input_len % SDRM_AES_BLOCK_SIZ == 0) crt->ctx->aesctx->padding = ID_NO_PADDING; - } + ret = crt->SE_final(crt, input, input_len, output, output_len); - if (ret != CRYPTO_SUCCESS) { + + if (ret != CRYPTO_SUCCESS) return UCI_ERROR; - } + //swap last block memcpy(lastblock, output + *output_len - 2 * SDRM_AES_BLOCK_SIZ, - SDRM_AES_BLOCK_SIZ); + SDRM_AES_BLOCK_SIZ); memcpy(output + *output_len - 2 * SDRM_AES_BLOCK_SIZ, - output + *output_len - SDRM_AES_BLOCK_SIZ, - SDRM_AES_BLOCK_SIZ); + output + *output_len - SDRM_AES_BLOCK_SIZ, + SDRM_AES_BLOCK_SIZ); memcpy(output + *output_len - SDRM_AES_BLOCK_SIZ, lastblock, - SDRM_AES_BLOCK_SIZ); + SDRM_AES_BLOCK_SIZ); return UCI_SUCCESS; } + //cts decrypt if (pctx->flag == ID_UCI_DEC_CTS) { lastblocksize = input_len % SDRM_AES_BLOCK_SIZ; @@ -360,7 +380,8 @@ int cryptocore_se_final(UCI_HANDLE oh, unsigned char *input, crt->ctx->aesctx->BlockLen = 0; ret = crt->SE_process(crt, psecondlastblk, - SDRM_AES_BLOCK_SIZ, secondlastblock, output_len); + SDRM_AES_BLOCK_SIZ, secondlastblock, output_len); + if (ret != CRYPTO_SUCCESS) { TZ_ERROR("UCI_ERROR error line = %d,%s,ret=%d\n", __LINE__, __func__, ret); return UCI_ERROR; @@ -369,610 +390,659 @@ int cryptocore_se_final(UCI_HANDLE oh, unsigned char *input, // 2. Cn = Cn || Tail (Dn, B-M). Pad the ciphertext to the nearest multiple of the block size using the last B-M bits of block cipher decryption of the second-to-last ciphertext block. memcpy(lastblock, plastblk, lastblocksize); memcpy(lastblock + lastblocksize, secondlastblock + lastblocksize, - SDRM_AES_BLOCK_SIZ - lastblocksize); + SDRM_AES_BLOCK_SIZ - lastblocksize); memcpy(crt->ctx->aesctx->IV, aIV, SDRM_AES_BLOCK_SIZ); // 3. Swap the last two ciphertext blocks. // 4. Decrypt the (modified) ciphertext using the standard CBC mode up to the last block. ret = crt->SE_process(crt, lastblock, - SDRM_AES_BLOCK_SIZ, output, output_len); + SDRM_AES_BLOCK_SIZ, output, output_len); + if (ret != CRYPTO_SUCCESS) { TZ_ERROR("UCI_ERROR error line = %d,%s,ret=%d\n", __LINE__, __func__, ret); return UCI_ERROR; } + ret = crt->SE_process(crt, psecondlastblk, - SDRM_AES_BLOCK_SIZ, lastblock, output_len); + SDRM_AES_BLOCK_SIZ, lastblock, output_len); + if (ret != CRYPTO_SUCCESS) { TZ_ERROR("UCI_ERROR error line = %d,%s,ret=%d\n", __LINE__, __func__, ret); return UCI_ERROR; } + memcpy(output + SDRM_AES_BLOCK_SIZ, lastblock, lastblocksize); *output_len = input_len; return UCI_SUCCESS; } + //swap last two block and decrypto if (input_len == 2 * SDRM_AES_BLOCK_SIZ) { ret = crt->SE_process(crt, input + SDRM_AES_BLOCK_SIZ, - SDRM_AES_BLOCK_SIZ, output, output_len); + SDRM_AES_BLOCK_SIZ, output, output_len); + if (ret != CRYPTO_SUCCESS) { TZ_ERROR("UCI_ERROR error line = %d,%s,ret=%d\n", __LINE__, __func__, ret); return UCI_ERROR; } + crt->ctx->aesctx->padding = ID_NO_PADDING; ret = crt->SE_final(crt, input, - SDRM_AES_BLOCK_SIZ, output + SDRM_AES_BLOCK_SIZ, output_len); + SDRM_AES_BLOCK_SIZ, output + SDRM_AES_BLOCK_SIZ, output_len); + if (ret != CRYPTO_SUCCESS) { TZ_ERROR("UCI_ERROR error line = %d,%s,ret=%d\n", __LINE__, __func__, ret); return UCI_ERROR; } + *output_len = 2 * SDRM_AES_BLOCK_SIZ; return UCI_SUCCESS; } } -// deal with other mode except cts - final: ret = crt->SE_final(crt, input, input_len, output, output_len); - if (ret != CRYPTO_SUCCESS) { - TZ_ERROR("UCI_ERROR error line = %d,%s,ret=%d\n", __LINE__, __func__, ret); + + // deal with other mode except cts +final: + ret = crt->SE_final(crt, input, input_len, output, output_len); + + if (ret != CRYPTO_SUCCESS) { + TZ_ERROR("UCI_ERROR error line = %d,%s,ret=%d\n", __LINE__, __func__, ret); return UCI_ERROR; } + return UCI_SUCCESS; } int cryptocore_se_encrypt_oneblock(UCI_HANDLE oh, unsigned char *cipher_text, - unsigned char * plain_text, unsigned char *user_key) { + unsigned char *plain_text, unsigned char *user_key) +{ int ret; - uci_context_s *pctx = (uci_context_s*)oh; - if (pctx == NULL) { + uci_context_s *pctx = (uci_context_s *)oh; + + if (pctx == NULL) return UCI_INVALID_HANDLE; - } - if (pctx->config != UCI_SW) { + if (pctx->config != UCI_SW) return UCI_INVALID_HANDLE; - } ret = ((CryptoCoreContainer *)pctx->imp)->SE_EncryptOneBlock(cipher_text, - plain_text, user_key); - if (ret != CRYPTO_SUCCESS) { + plain_text, user_key); + + if (ret != CRYPTO_SUCCESS) return UCI_ERROR; - } + return UCI_SUCCESS; } int cryptocore_se_decrypt_oneblock(UCI_HANDLE oh, unsigned char *plain_text, - unsigned char *cipher_text, unsigned char *user_key) { + unsigned char *cipher_text, unsigned char *user_key) +{ int ret; - uci_context_s *pctx = (uci_context_s*)oh; - if (pctx == NULL) { + uci_context_s *pctx = (uci_context_s *)oh; + + if (pctx == NULL) return UCI_INVALID_HANDLE; - } - if (pctx->config != UCI_SW) { + if (pctx->config != UCI_SW) return UCI_INVALID_HANDLE; - } ret = ((CryptoCoreContainer *)pctx->imp)->SE_DecryptOneBlock(cipher_text, - plain_text, user_key); - if (ret != CRYPTO_SUCCESS) { + plain_text, user_key); + + if (ret != CRYPTO_SUCCESS) return UCI_ERROR; - } + return UCI_SUCCESS; } int cryptocore_ae_gen_param(UCI_HANDLE oh, uci_param_s *param, - unsigned int size) { + unsigned int size) +{ int ret; unsigned int alg; //uci_param_imp_u *uciparm = NULL; CryptoCoreContainer *crt = NULL; - uci_context_s *pctx = (uci_context_s*)oh; - if (param == NULL) { + uci_context_s *pctx = (uci_context_s *)oh; + + if (param == NULL) return UCI_INVALID_ARGUMENT; - } alg = pctx->alg; switch (alg) { - case ID_UCI_DH: - crt = create_CryptoCoreContainer(ID_DH); - if (crt == NULL) { - return UCI_ERROR; - } - if (crt->DH_GenerateParam(crt, param->uparam.udhp.prime, size, - param->uparam.udhp.generator) != CRYPTO_SUCCESS) { - destroy_CryptoCoreContainer(crt); - return UCI_ERROR; - } - param->uparam.udhp.len = size; + case ID_UCI_DH: + crt = create_CryptoCoreContainer(ID_DH); + + if (crt == NULL) + return UCI_ERROR; + + if (crt->DH_GenerateParam(crt, param->uparam.udhp.prime, size, + param->uparam.udhp.generator) != CRYPTO_SUCCESS) { destroy_CryptoCoreContainer(crt); - break; - case ID_UCI_DSA: - crt = create_CryptoCoreContainer(ID_DSA); - if (crt == NULL) { - return UCI_ERROR; - } + return UCI_ERROR; + } - ret = crt->DSA_genParam(crt, size, param->uparam.udp.dsa_p_data, - &(param->uparam.udp.dsa_p_len), param->uparam.udp.dsa_q_data, - &(param->uparam.udp.dsa_q_len), param->uparam.udp.dsa_g_data, - &(param->uparam.udp.dsa_g_len)); + param->uparam.udhp.len = size; + destroy_CryptoCoreContainer(crt); + break; - if (ret != UCI_SUCCESS) { - destroy_CryptoCoreContainer(crt); - return UCI_ERROR; - } + case ID_UCI_DSA: + crt = create_CryptoCoreContainer(ID_DSA); + + if (crt == NULL) + return UCI_ERROR; + + ret = crt->DSA_genParam(crt, size, param->uparam.udp.dsa_p_data, + &(param->uparam.udp.dsa_p_len), param->uparam.udp.dsa_q_data, + &(param->uparam.udp.dsa_q_len), param->uparam.udp.dsa_g_data, + &(param->uparam.udp.dsa_g_len)); + + if (ret != UCI_SUCCESS) { destroy_CryptoCoreContainer(crt); - break; - default: - return UCI_INVALID_HANDLE; + return UCI_ERROR; + } + + destroy_CryptoCoreContainer(crt); + break; + + default: + return UCI_INVALID_HANDLE; } + return UCI_SUCCESS; } int cryptocore_ae_gen_keypair(UCI_HANDLE oh, uci_key_s *keymaterial, - uci_param_s *param) { - uci_context_s *pctx = (uci_context_s*)oh; + uci_param_s *param) +{ + uci_context_s *pctx = (uci_context_s *)oh; unsigned int alg; unsigned int pad; uci_key_s *ucikey = keymaterial; int ret = UCI_ERROR; uci_param_imp_u *uciparm = NULL; - if (param == NULL) { + + if (param == NULL) return UCI_INVALID_ARGUMENT; - } + uciparm = ¶m->uparam; - if (pctx->config != UCI_SW_CRYPTOCORE) { + if (pctx->config != UCI_SW_CRYPTOCORE) return UCI_INVALID_HANDLE; - } + alg = pctx->alg; + switch (alg) { - case ID_UCI_RSA512: - case ID_UCI_RSA: - case ID_UCI_RSA1024: - case ID_UCI_RSA2048: - case ID_UCI_RSA3072: - case ID_UCI_RSA4096: - pad = SDRM_LOW_HALF(uciparm->urp.padding); - if (pad != ID_UCI_RSAES_PKCS15 && pad != ID_UCI_RSAES_OAEP - && pad != ID_UCI_NO_PADDING && pad != ID_UCI_RSASSA_PKCS15 - && pad != ID_UCI_RSASSA_PSS) { - return UCI_INVALID_ARGUMENT; - } - if (uciparm->urp.flag == RSA_GENKEYWITHNON) { - ret = ((CryptoCoreContainer *)pctx->imp)->RSA_genKeypair( - (CryptoCoreContainer *)pctx->imp, uciparm->urp.padding, - ucikey->imp.rkey.n, &(ucikey->imp.rkey.n_len), ucikey->imp.rkey.e, - &(ucikey->imp.rkey.e_len), ucikey->imp.rkey.d, - &(ucikey->imp.rkey.d_len)); - break; - } - if (uciparm->urp.flag == RSA_GENKEYWITHE) { - ret = ((CryptoCoreContainer *)pctx->imp)->RSA_genKeypairWithE( - (CryptoCoreContainer *)pctx->imp, uciparm->urp.padding, - uciparm->urp.e, uciparm->urp.e_len, ucikey->imp.rkey.n, - &(ucikey->imp.rkey.n_len), ucikey->imp.rkey.d, - &(ucikey->imp.rkey.d_len)); - break; - } - if (uciparm->urp.flag == RSA_GENKEYWITHPQE) { - ret = ((CryptoCoreContainer *)pctx->imp)->RSA_genKeyDWithPQE( - (CryptoCoreContainer *)pctx->imp, uciparm->urp.padding, - uciparm->urp.e, uciparm->urp.e_len, uciparm->urp.p, - uciparm->urp.p_len, uciparm->urp.q, uciparm->urp.q_len, - ucikey->imp.rkey.n, &(ucikey->imp.rkey.n_len), ucikey->imp.rkey.d, - &(ucikey->imp.rkey.d_len)); - break; - } - if (uciparm->urp.flag == RSA_KEYFORCRT) { - ret = ((CryptoCoreContainer *)pctx->imp)->RSA_genKeypairForCRT( - (CryptoCoreContainer *)pctx->imp, uciparm->urp.padding, - ucikey->imp.rkey.n, &(ucikey->imp.rkey.n_len), ucikey->imp.rkey.e, - &(ucikey->imp.rkey.e_len), ucikey->imp.rkey.d, - &(ucikey->imp.rkey.d_len), uciparm->urp.p, &(uciparm->urp.p_len), - uciparm->urp.q, &(uciparm->urp.q_len), uciparm->urp.dmodp1, - &(uciparm->urp.dmodp1_len), uciparm->urp.dmodq1, - &(uciparm->urp.dmodq1_len), uciparm->urp.iqp, - &(uciparm->urp.iqp_len)); - } + case ID_UCI_RSA512: + case ID_UCI_RSA: + case ID_UCI_RSA1024: + case ID_UCI_RSA2048: + case ID_UCI_RSA3072: + case ID_UCI_RSA4096: + pad = SDRM_LOW_HALF(uciparm->urp.padding); + + if (pad != ID_UCI_RSAES_PKCS15 && pad != ID_UCI_RSAES_OAEP + && pad != ID_UCI_NO_PADDING && pad != ID_UCI_RSASSA_PKCS15 + && pad != ID_UCI_RSASSA_PSS) + return UCI_INVALID_ARGUMENT; + + if (uciparm->urp.flag == RSA_GENKEYWITHNON) { + ret = ((CryptoCoreContainer *)pctx->imp)->RSA_genKeypair( + (CryptoCoreContainer *)pctx->imp, uciparm->urp.padding, + ucikey->imp.rkey.n, &(ucikey->imp.rkey.n_len), ucikey->imp.rkey.e, + &(ucikey->imp.rkey.e_len), ucikey->imp.rkey.d, + &(ucikey->imp.rkey.d_len)); break; - case ID_UCI_DSA: - ret = ((CryptoCoreContainer *)pctx->imp)->DSA_setParam( - (CryptoCoreContainer *)pctx->imp, uciparm->udp.dsa_p_data, - uciparm->udp.dsa_p_len, uciparm->udp.dsa_q_data, - uciparm->udp.dsa_q_len, uciparm->udp.dsa_g_data, - uciparm->udp.dsa_g_len); - if (ret != CRYPTO_SUCCESS) { - return UCI_ERROR; - } + } - ret = ((CryptoCoreContainer *)pctx->imp)->DSA_genKeypair( - (CryptoCoreContainer *)pctx->imp, ucikey->imp.dkey.ydata, - &(ucikey->imp.dkey.ydata_len), ucikey->imp.dkey.xdata, - &(ucikey->imp.dkey.xdata_len)); + if (uciparm->urp.flag == RSA_GENKEYWITHE) { + ret = ((CryptoCoreContainer *)pctx->imp)->RSA_genKeypairWithE( + (CryptoCoreContainer *)pctx->imp, uciparm->urp.padding, + uciparm->urp.e, uciparm->urp.e_len, ucikey->imp.rkey.n, + &(ucikey->imp.rkey.n_len), ucikey->imp.rkey.d, + &(ucikey->imp.rkey.d_len)); break; - case ID_UCI_ECDSA: - case ID_UCI_ECDH: - //set curver parameter - ret = ((CryptoCoreContainer *)pctx->imp)->EC_setCurve( - (CryptoCoreContainer *)pctx->imp, uciparm->uep.dimension, - uciparm->uep.ecc_p_data, uciparm->uep.ecc_p_len, - uciparm->uep.ecc_a_data, uciparm->uep.ecc_a_len, - uciparm->uep.ecc_b_data, uciparm->uep.ecc_b_len, - uciparm->uep.ecc_g_x_data, uciparm->uep.ecc_g_x_len, - uciparm->uep.ecc_g_y_data, uciparm->uep.ecc_g_y_len, - uciparm->uep.ecc_r_data, uciparm->uep.ecc_r_len); - if (ret != CRYPTO_SUCCESS) { - break; - } + } - ret = ((CryptoCoreContainer *)pctx->imp)->EC_genKeypair( - (CryptoCoreContainer *)pctx->imp, ucikey->imp.ekey.privatekey, - &(ucikey->imp.ekey.privatekey_len), ucikey->imp.ekey.publickey_x, - &(ucikey->imp.ekey.publickey_x_len), ucikey->imp.ekey.publickey_y, - &(ucikey->imp.ekey.publickey_y_len)); + if (uciparm->urp.flag == RSA_GENKEYWITHPQE) { + ret = ((CryptoCoreContainer *)pctx->imp)->RSA_genKeyDWithPQE( + (CryptoCoreContainer *)pctx->imp, uciparm->urp.padding, + uciparm->urp.e, uciparm->urp.e_len, uciparm->urp.p, + uciparm->urp.p_len, uciparm->urp.q, uciparm->urp.q_len, + ucikey->imp.rkey.n, &(ucikey->imp.rkey.n_len), ucikey->imp.rkey.d, + &(ucikey->imp.rkey.d_len)); break; - default: + } + + if (uciparm->urp.flag == RSA_KEYFORCRT) { + ret = ((CryptoCoreContainer *)pctx->imp)->RSA_genKeypairForCRT( + (CryptoCoreContainer *)pctx->imp, uciparm->urp.padding, + ucikey->imp.rkey.n, &(ucikey->imp.rkey.n_len), ucikey->imp.rkey.e, + &(ucikey->imp.rkey.e_len), ucikey->imp.rkey.d, + &(ucikey->imp.rkey.d_len), uciparm->urp.p, &(uciparm->urp.p_len), + uciparm->urp.q, &(uciparm->urp.q_len), uciparm->urp.dmodp1, + &(uciparm->urp.dmodp1_len), uciparm->urp.dmodq1, + &(uciparm->urp.dmodq1_len), uciparm->urp.iqp, + &(uciparm->urp.iqp_len)); + } + + break; + + case ID_UCI_DSA: + ret = ((CryptoCoreContainer *)pctx->imp)->DSA_setParam( + (CryptoCoreContainer *)pctx->imp, uciparm->udp.dsa_p_data, + uciparm->udp.dsa_p_len, uciparm->udp.dsa_q_data, + uciparm->udp.dsa_q_len, uciparm->udp.dsa_g_data, + uciparm->udp.dsa_g_len); + + if (ret != CRYPTO_SUCCESS) return UCI_ERROR; + + ret = ((CryptoCoreContainer *)pctx->imp)->DSA_genKeypair( + (CryptoCoreContainer *)pctx->imp, ucikey->imp.dkey.ydata, + &(ucikey->imp.dkey.ydata_len), ucikey->imp.dkey.xdata, + &(ucikey->imp.dkey.xdata_len)); + break; + + case ID_UCI_ECDSA: + case ID_UCI_ECDH: + //set curver parameter + ret = ((CryptoCoreContainer *)pctx->imp)->EC_setCurve( + (CryptoCoreContainer *)pctx->imp, uciparm->uep.dimension, + uciparm->uep.ecc_p_data, uciparm->uep.ecc_p_len, + uciparm->uep.ecc_a_data, uciparm->uep.ecc_a_len, + uciparm->uep.ecc_b_data, uciparm->uep.ecc_b_len, + uciparm->uep.ecc_g_x_data, uciparm->uep.ecc_g_x_len, + uciparm->uep.ecc_g_y_data, uciparm->uep.ecc_g_y_len, + uciparm->uep.ecc_r_data, uciparm->uep.ecc_r_len); + + if (ret != CRYPTO_SUCCESS) + break; + + ret = ((CryptoCoreContainer *)pctx->imp)->EC_genKeypair( + (CryptoCoreContainer *)pctx->imp, ucikey->imp.ekey.privatekey, + &(ucikey->imp.ekey.privatekey_len), ucikey->imp.ekey.publickey_x, + &(ucikey->imp.ekey.publickey_x_len), ucikey->imp.ekey.publickey_y, + &(ucikey->imp.ekey.publickey_y_len)); + break; + + default: + return UCI_ERROR; } - if (ret == CRYPTO_INVALID_ARGUMENT) { + + if (ret == CRYPTO_INVALID_ARGUMENT) return UCI_INVALID_ARGUMENT; - } - if (ret != CRYPTO_SUCCESS) { + + if (ret != CRYPTO_SUCCESS) return UCI_ERROR; - } + return UCI_SUCCESS; } int cryptocore_ae_set_keypair(UCI_HANDLE oh, uci_key_s *keymaterial, - uci_param_s *param) { - uci_context_s *pctx = (uci_context_s*)oh; + uci_param_s *param) +{ + uci_context_s *pctx = (uci_context_s *)oh; uci_key_s *ucikey = keymaterial; uci_param_imp_u *uciparm = ¶m->uparam; int ret; unsigned int pad; - if (pctx == NULL) { + if (pctx == NULL) return UCI_INVALID_HANDLE; - } + int alg = pctx->alg; - if (pctx->config != UCI_SW) { + + if (pctx->config != UCI_SW) return UCI_INVALID_HANDLE; - } switch (alg) { - case ID_UCI_RSA512: - case ID_UCI_RSA: - case ID_UCI_RSA1024: - case ID_UCI_RSA2048: - case ID_UCI_RSA3072: - case ID_UCI_RSA4096: - pad = SDRM_LOW_HALF(uciparm->urp.padding); - - if (pad != ID_UCI_RSAES_PKCS15 && pad != ID_UCI_RSAES_OAEP - && pad != ID_UCI_NO_PADDING && pad != ID_UCI_RSASSA_PKCS15 - && pad != ID_UCI_RSASSA_PSS) { - return UCI_INVALID_ARGUMENT; - } + case ID_UCI_RSA512: + case ID_UCI_RSA: + case ID_UCI_RSA1024: + case ID_UCI_RSA2048: + case ID_UCI_RSA3072: + case ID_UCI_RSA4096: + pad = SDRM_LOW_HALF(uciparm->urp.padding); + + if (pad != ID_UCI_RSAES_PKCS15 && pad != ID_UCI_RSAES_OAEP + && pad != ID_UCI_NO_PADDING && pad != ID_UCI_RSASSA_PKCS15 + && pad != ID_UCI_RSASSA_PSS) + return UCI_INVALID_ARGUMENT; + + if (uciparm->urp.flag == RSA_KEYFORCRT) { + ret = ((CryptoCoreContainer *)pctx->imp)->RSA_setKeypairForCRT( + (CryptoCoreContainer *)pctx->imp, uciparm->urp.padding, + ucikey->imp.rkey.n, ucikey->imp.rkey.n_len, ucikey->imp.rkey.e, + ucikey->imp.rkey.e_len, ucikey->imp.rkey.d, ucikey->imp.rkey.d_len, + uciparm->urp.p, uciparm->urp.p_len, uciparm->urp.q, + uciparm->urp.q_len, uciparm->urp.dmodp1, uciparm->urp.dmodp1_len, + uciparm->urp.dmodq1, uciparm->urp.dmodq1_len, uciparm->urp.iqp, + uciparm->urp.iqp_len); + } else { + ret = ((CryptoCoreContainer *)pctx->imp)->RSA_setKeypair( + (CryptoCoreContainer *)pctx->imp, uciparm->urp.padding, + ucikey->imp.rkey.n, ucikey->imp.rkey.n_len, ucikey->imp.rkey.e, + ucikey->imp.rkey.e_len, ucikey->imp.rkey.d, ucikey->imp.rkey.d_len); + } - if (uciparm->urp.flag == RSA_KEYFORCRT) { - ret = ((CryptoCoreContainer *)pctx->imp)->RSA_setKeypairForCRT( - (CryptoCoreContainer *)pctx->imp, uciparm->urp.padding, - ucikey->imp.rkey.n, ucikey->imp.rkey.n_len, ucikey->imp.rkey.e, - ucikey->imp.rkey.e_len, ucikey->imp.rkey.d, ucikey->imp.rkey.d_len, - uciparm->urp.p, uciparm->urp.p_len, uciparm->urp.q, - uciparm->urp.q_len, uciparm->urp.dmodp1, uciparm->urp.dmodp1_len, - uciparm->urp.dmodq1, uciparm->urp.dmodq1_len, uciparm->urp.iqp, - uciparm->urp.iqp_len); - } else { - ret = ((CryptoCoreContainer *)pctx->imp)->RSA_setKeypair( - (CryptoCoreContainer *)pctx->imp, uciparm->urp.padding, - ucikey->imp.rkey.n, ucikey->imp.rkey.n_len, ucikey->imp.rkey.e, - ucikey->imp.rkey.e_len, ucikey->imp.rkey.d, ucikey->imp.rkey.d_len); - } - break; - case ID_UCI_DSA: - ret = ((CryptoCoreContainer *)pctx->imp)->DSA_setParam( - (CryptoCoreContainer *)pctx->imp, uciparm->udp.dsa_p_data, - uciparm->udp.dsa_p_len, uciparm->udp.dsa_q_data, - uciparm->udp.dsa_q_len, uciparm->udp.dsa_g_data, - uciparm->udp.dsa_g_len); - if (ret != CRYPTO_SUCCESS) { - return UCI_ERROR; - } - ret = ((CryptoCoreContainer *)pctx->imp)->DSA_setKeyPair( - (CryptoCoreContainer *)pctx->imp, ucikey->imp.dkey.ydata, - (ucikey->imp.dkey.ydata_len), ucikey->imp.dkey.xdata, - (ucikey->imp.dkey.xdata_len)); - break; - case ID_UCI_ECDSA: - ret = ((CryptoCoreContainer *)pctx->imp)->EC_setCurve( - (CryptoCoreContainer *)pctx->imp, uciparm->uep.dimension, - uciparm->uep.ecc_p_data, uciparm->uep.ecc_p_len, - uciparm->uep.ecc_a_data, uciparm->uep.ecc_a_len, - uciparm->uep.ecc_b_data, uciparm->uep.ecc_b_len, - uciparm->uep.ecc_g_x_data, uciparm->uep.ecc_g_x_len, - uciparm->uep.ecc_g_y_data, uciparm->uep.ecc_g_y_len, - uciparm->uep.ecc_r_data, uciparm->uep.ecc_r_len); - - ret = ((CryptoCoreContainer *)pctx->imp)->EC_setKeypair( - (CryptoCoreContainer *)pctx->imp, ucikey->imp.ekey.privatekey, - (ucikey->imp.ekey.privatekey_len), ucikey->imp.ekey.publickey_x, - (ucikey->imp.ekey.publickey_x_len), ucikey->imp.ekey.publickey_y, - (ucikey->imp.ekey.publickey_y_len)); - break; - case ID_UCI_DH: - ret = ((CryptoCoreContainer *)pctx->imp)->DH_SetParam( - (CryptoCoreContainer *)pctx->imp, uciparm->udhp.prime, - uciparm->udhp.len, uciparm->udhp.generator, uciparm->udhp.len); - break; - default: + break; + + case ID_UCI_DSA: + ret = ((CryptoCoreContainer *)pctx->imp)->DSA_setParam( + (CryptoCoreContainer *)pctx->imp, uciparm->udp.dsa_p_data, + uciparm->udp.dsa_p_len, uciparm->udp.dsa_q_data, + uciparm->udp.dsa_q_len, uciparm->udp.dsa_g_data, + uciparm->udp.dsa_g_len); + + if (ret != CRYPTO_SUCCESS) return UCI_ERROR; - } - if (ret != CRYPTO_SUCCESS) { + + ret = ((CryptoCoreContainer *)pctx->imp)->DSA_setKeyPair( + (CryptoCoreContainer *)pctx->imp, ucikey->imp.dkey.ydata, + (ucikey->imp.dkey.ydata_len), ucikey->imp.dkey.xdata, + (ucikey->imp.dkey.xdata_len)); + break; + + case ID_UCI_ECDSA: + ret = ((CryptoCoreContainer *)pctx->imp)->EC_setCurve( + (CryptoCoreContainer *)pctx->imp, uciparm->uep.dimension, + uciparm->uep.ecc_p_data, uciparm->uep.ecc_p_len, + uciparm->uep.ecc_a_data, uciparm->uep.ecc_a_len, + uciparm->uep.ecc_b_data, uciparm->uep.ecc_b_len, + uciparm->uep.ecc_g_x_data, uciparm->uep.ecc_g_x_len, + uciparm->uep.ecc_g_y_data, uciparm->uep.ecc_g_y_len, + uciparm->uep.ecc_r_data, uciparm->uep.ecc_r_len); + + ret = ((CryptoCoreContainer *)pctx->imp)->EC_setKeypair( + (CryptoCoreContainer *)pctx->imp, ucikey->imp.ekey.privatekey, + (ucikey->imp.ekey.privatekey_len), ucikey->imp.ekey.publickey_x, + (ucikey->imp.ekey.publickey_x_len), ucikey->imp.ekey.publickey_y, + (ucikey->imp.ekey.publickey_y_len)); + break; + + case ID_UCI_DH: + ret = ((CryptoCoreContainer *)pctx->imp)->DH_SetParam( + (CryptoCoreContainer *)pctx->imp, uciparm->udhp.prime, + uciparm->udhp.len, uciparm->udhp.generator, uciparm->udhp.len); + break; + + default: return UCI_ERROR; } + + if (ret != CRYPTO_SUCCESS) + return UCI_ERROR; + return UCI_SUCCESS; } int cryptocore_ae_encrypt(UCI_HANDLE oh, unsigned char *input, - unsigned int input_len, unsigned char *output, unsigned int *output_len) { + unsigned int input_len, unsigned char *output, unsigned int *output_len) +{ int ret; - uci_context_s *pctx = (uci_context_s*)oh; + uci_context_s *pctx = (uci_context_s *)oh; - if (pctx == NULL) { + if (pctx == NULL) return UCI_INVALID_HANDLE; - } - if (pctx->config != UCI_SW) { + if (pctx->config != UCI_SW) return UCI_INVALID_HANDLE; - } - if (pctx->alg < ID_UCI_RSA || pctx->alg > ID_UCI_RSA512) { + if (pctx->alg < ID_UCI_RSA || pctx->alg > ID_UCI_RSA512) return UCI_INVALID_HANDLE; - } + ret = ((CryptoCoreContainer *)pctx->imp)->AE_encrypt( - ((CryptoCoreContainer*)pctx->imp), input, input_len, output, output_len); - if (ret == CRYPTO_MSG_TOO_LONG) { + ((CryptoCoreContainer *)pctx->imp), input, input_len, output, output_len); + + if (ret == CRYPTO_MSG_TOO_LONG) return UCI_MSG_TOO_LONG; - } - if (ret != CRYPTO_SUCCESS) { + + if (ret != CRYPTO_SUCCESS) return UCI_ERROR; - } + return UCI_SUCCESS; } int cryptocore_ae_decrypt(UCI_HANDLE oh, unsigned char *input, - unsigned int input_len, unsigned char *output, unsigned int *output_len) { + unsigned int input_len, unsigned char *output, unsigned int *output_len) +{ int ret; - uci_context_s *pctx = (uci_context_s*)oh; + uci_context_s *pctx = (uci_context_s *)oh; - if (pctx == NULL) { + if (pctx == NULL) return UCI_INVALID_HANDLE; - } - if (pctx->config != UCI_SW) { + if (pctx->config != UCI_SW) return UCI_INVALID_HANDLE; - } - if (pctx->alg < ID_UCI_RSA || pctx->alg > ID_UCI_RSA512) { + if (pctx->alg < ID_UCI_RSA || pctx->alg > ID_UCI_RSA512) return UCI_INVALID_HANDLE; - } + ret = ((CryptoCoreContainer *)pctx->imp)->AE_decrypt( - ((CryptoCoreContainer*)pctx->imp), input, input_len, output, output_len); - if (ret == CRYPTO_MSG_TOO_LONG) { + ((CryptoCoreContainer *)pctx->imp), input, input_len, output, output_len); + + if (ret == CRYPTO_MSG_TOO_LONG) return UCI_MSG_TOO_LONG; - } - if (ret != CRYPTO_SUCCESS) { + + if (ret != CRYPTO_SUCCESS) return UCI_ERROR; - } + return UCI_SUCCESS; } int cryptocore_ae_decryptbycrt(UCI_HANDLE oh, unsigned char *input, - unsigned int input_len, unsigned char *output, unsigned int *output_len) { + unsigned int input_len, unsigned char *output, unsigned int *output_len) +{ int ret; - uci_context_s *pctx = (uci_context_s*)oh; + uci_context_s *pctx = (uci_context_s *)oh; - if (pctx == NULL) { + if (pctx == NULL) return UCI_INVALID_HANDLE; - } - if (pctx->config != UCI_SW) { + if (pctx->config != UCI_SW) return UCI_INVALID_HANDLE; - } // ctr=(CryptoCoreContainer *)(pctx->imp); // ctr->MD_update(ctr,msg,msg_len); ret = ((CryptoCoreContainer *)pctx->imp)->AE_decryptByCRT( - ((CryptoCoreContainer*)pctx->imp), input, input_len, output, output_len); - if (ret == CRYPTO_MSG_TOO_LONG) { + ((CryptoCoreContainer *)pctx->imp), input, input_len, output, output_len); + + if (ret == CRYPTO_MSG_TOO_LONG) return UCI_MSG_TOO_LONG; - } - if (ret != CRYPTO_SUCCESS) { + + if (ret != CRYPTO_SUCCESS) return UCI_ERROR; - } + return UCI_SUCCESS; } int cryptocore_ds_sign(UCI_HANDLE oh, unsigned char *hash, - unsigned int hash_len, unsigned char *signature, unsigned int *sign_len) { + unsigned int hash_len, unsigned char *signature, unsigned int *sign_len) +{ int ret; - uci_context_s *pctx = (uci_context_s*)oh; + uci_context_s *pctx = (uci_context_s *)oh; - if (pctx == NULL) { + if (pctx == NULL) return UCI_INVALID_HANDLE; - } - if (pctx->config != UCI_SW) { + if (pctx->config != UCI_SW) return UCI_INVALID_HANDLE; - } ret = ((CryptoCoreContainer *)pctx->imp)->DS_sign( - ((CryptoCoreContainer*)pctx->imp), hash, hash_len, signature, sign_len); - if (ret == CRYPTO_MSG_TOO_LONG) { + ((CryptoCoreContainer *)pctx->imp), hash, hash_len, signature, sign_len); + + if (ret == CRYPTO_MSG_TOO_LONG) return UCI_MSG_TOO_LONG; - } - if (ret != CRYPTO_SUCCESS) { + + if (ret != CRYPTO_SUCCESS) return UCI_ERROR; - } + return UCI_SUCCESS; } int cryptocore_ds_verify(UCI_HANDLE oh, unsigned char *hash, - unsigned int hash_len, unsigned char *signature, unsigned int sign_len, - int *result) { + unsigned int hash_len, unsigned char *signature, unsigned int sign_len, + int *result) +{ int ret; - uci_context_s *pctx = (uci_context_s*)oh; + uci_context_s *pctx = (uci_context_s *)oh; - if (pctx == NULL) { + if (pctx == NULL) return UCI_INVALID_HANDLE; - } - if (pctx->config != UCI_SW) { + if (pctx->config != UCI_SW) return UCI_INVALID_HANDLE; - } ret = ((CryptoCoreContainer *)pctx->imp)->DS_verify( - (CryptoCoreContainer*)pctx->imp, hash, hash_len, signature, sign_len, - result); - if (ret == CRYPTO_MSG_TOO_LONG) { + (CryptoCoreContainer *)pctx->imp, hash, hash_len, signature, sign_len, + result); + + if (ret == CRYPTO_MSG_TOO_LONG) return UCI_MSG_TOO_LONG; - } - if (ret != CRYPTO_SUCCESS) { + + if (ret != CRYPTO_SUCCESS) return UCI_ERROR; - } + return UCI_SUCCESS; } int cryptocore_dh_gen_dh1stphasekey(UCI_HANDLE oh, unsigned char *pch_xk, - unsigned char *pch_xv, uci_param_s *param) { + unsigned char *pch_xv, uci_param_s *param) +{ int ret; uci_param_imp_u *uciparam = ¶m->uparam; - uci_context_s *pctx = (uci_context_s*)oh; + uci_context_s *pctx = (uci_context_s *)oh; unsigned int alg; - if (pctx == NULL) { + if (pctx == NULL) return UCI_INVALID_HANDLE; - } - if (pctx->config != UCI_SW) { + if (pctx->config != UCI_SW) return UCI_INVALID_HANDLE; - } alg = pctx->alg; + if (alg == ID_UCI_ECDH) { ret = ((CryptoCoreContainer *)pctx->imp)->EC_setCurve( - (CryptoCoreContainer *)pctx->imp, uciparam->uep.dimension, - uciparam->uep.ecc_p_data, uciparam->uep.ecc_p_len, - uciparam->uep.ecc_a_data, uciparam->uep.ecc_a_len, - uciparam->uep.ecc_b_data, uciparam->uep.ecc_b_len, - uciparam->uep.ecc_g_x_data, uciparam->uep.ecc_g_x_len, - uciparam->uep.ecc_g_y_data, uciparam->uep.ecc_g_y_len, - uciparam->uep.ecc_r_data, uciparam->uep.ecc_r_len); - - if (ret != CRYPTO_SUCCESS) { + (CryptoCoreContainer *)pctx->imp, uciparam->uep.dimension, + uciparam->uep.ecc_p_data, uciparam->uep.ecc_p_len, + uciparam->uep.ecc_a_data, uciparam->uep.ecc_a_len, + uciparam->uep.ecc_b_data, uciparam->uep.ecc_b_len, + uciparam->uep.ecc_g_x_data, uciparam->uep.ecc_g_x_len, + uciparam->uep.ecc_g_y_data, uciparam->uep.ecc_g_y_len, + uciparam->uep.ecc_r_data, uciparam->uep.ecc_r_len); + + if (ret != CRYPTO_SUCCESS) return UCI_ERROR; - } + ret = ((CryptoCoreContainer *)pctx->imp)->ECDH_Gen1stPhaseKey( - (CryptoCoreContainer*)pctx->imp, pch_xk, pch_xv); - if (ret != CRYPTO_SUCCESS) { + (CryptoCoreContainer *)pctx->imp, pch_xk, pch_xv); + + if (ret != CRYPTO_SUCCESS) return UCI_ERROR; - } else { + + else return UCI_SUCCESS; - } } + if (alg == ID_UCI_DH) { ret = ((CryptoCoreContainer *)pctx->imp)->DH_SetParam( - (CryptoCoreContainer *)pctx->imp, uciparam->udhp.prime, - uciparam->udhp.len, uciparam->udhp.generator, uciparam->udhp.len); + (CryptoCoreContainer *)pctx->imp, uciparam->udhp.prime, + uciparam->udhp.len, uciparam->udhp.generator, uciparam->udhp.len); - if (ret != CRYPTO_SUCCESS) { + if (ret != CRYPTO_SUCCESS) return UCI_ERROR; - } + ret = ((CryptoCoreContainer *)pctx->imp)->DH_Gen1stPhaseKey( - (CryptoCoreContainer*)pctx->imp, pch_xk, pch_xv); - if (ret != CRYPTO_SUCCESS) { + (CryptoCoreContainer *)pctx->imp, pch_xk, pch_xv); + + if (ret != CRYPTO_SUCCESS) return UCI_ERROR; - } else { + + else return UCI_SUCCESS; - } } + return UCI_ERROR; } int cryptocore_dh_gen_dhkey(UCI_HANDLE oh, unsigned char *pch_xk, - unsigned char *pch_xv, unsigned char *pch_kauth) { + unsigned char *pch_xv, unsigned char *pch_kauth) +{ int ret; - uci_context_s *pctx = (uci_context_s*)oh; + uci_context_s *pctx = (uci_context_s *)oh; unsigned int alg; - if (pctx->config != UCI_SW_CRYPTOCORE) { + if (pctx->config != UCI_SW_CRYPTOCORE) return UCI_INVALID_HANDLE; - } + alg = pctx->alg; if (alg == ID_UCI_ECDH) { ret = ((CryptoCoreContainer *)pctx->imp)->ECDH_GenAuthKey( - (CryptoCoreContainer*)pctx->imp, pch_xk, pch_xv, pch_kauth); + (CryptoCoreContainer *)pctx->imp, pch_xk, pch_xv, pch_kauth); } else if (alg == ID_UCI_DH) { ret = ((CryptoCoreContainer *)pctx->imp)->DH_GenAuthKey( - (CryptoCoreContainer*)pctx->imp, pch_xk, pch_xv, pch_kauth); - } else { + (CryptoCoreContainer *)pctx->imp, pch_xk, pch_xv, pch_kauth); + } else return UCI_INVALID_HANDLE; - } - if (ret != CRYPTO_SUCCESS) { + + if (ret != CRYPTO_SUCCESS) return UCI_ERROR; - } + return CRYPTO_SUCCESS; } -int cryptocore_prng_seed(UCI_HANDLE oh, unsigned char *seed) { +int cryptocore_prng_seed(UCI_HANDLE oh, unsigned char *seed) +{ int ret; - uci_context_s *pctx = (uci_context_s*)oh; + uci_context_s *pctx = (uci_context_s *)oh; - if (pctx == NULL) { + if (pctx == NULL) return UCI_INVALID_HANDLE; - } - if (pctx->config != UCI_SW) { + if (pctx->config != UCI_SW) return UCI_INVALID_HANDLE; - } - if (pctx->alg != ID_UCI_X931) { + + if (pctx->alg != ID_UCI_X931) return UCI_INVALID_HANDLE; - } + ret = ((CryptoCoreContainer *)pctx->imp)->PRNG_seed( - (CryptoCoreContainer*)(pctx->imp), seed); - if (ret != CRYPTO_SUCCESS) { + (CryptoCoreContainer *)(pctx->imp), seed); + + if (ret != CRYPTO_SUCCESS) return UCI_ERROR; - } + return UCI_SUCCESS; } int cryptocore_prng_get(UCI_HANDLE oh, unsigned int bit_len, - unsigned char *data) { + unsigned char *data) +{ int ret; - uci_context_s *pctx = (uci_context_s*)oh; - if (pctx == NULL) { + uci_context_s *pctx = (uci_context_s *)oh; + + if (pctx == NULL) return UCI_INVALID_HANDLE; - } - if (pctx->config != UCI_SW) { + if (pctx->config != UCI_SW) return UCI_INVALID_HANDLE; - } - if (pctx->alg != ID_UCI_X931) { + + if (pctx->alg != ID_UCI_X931) return UCI_INVALID_HANDLE; - } + ret = ((CryptoCoreContainer *)pctx->imp)->PRNG_get( - (CryptoCoreContainer*)(pctx->imp), bit_len, data); - if (ret != CRYPTO_SUCCESS) { + (CryptoCoreContainer *)(pctx->imp), bit_len, data); + + if (ret != CRYPTO_SUCCESS) return UCI_ERROR; - } + return UCI_SUCCESS; } diff --git a/ssflib/dep/uci/source/uci_hwcrypto.c b/ssflib/dep/uci/source/uci_hwcrypto.c index 19d0ef6..adcf86e 100644 --- a/ssflib/dep/uci/source/uci_hwcrypto.c +++ b/ssflib/dep/uci/source/uci_hwcrypto.c @@ -37,49 +37,47 @@ #include -/*! \brief print out by byte unit */ +/*! \brief print out by byte unit */ #undef PrintBYTE -#define PrintBYTE(msg, Data, DataLen) { \ - int idx; \ - printf("%10s =", msg); \ - for( idx=0; idx<(int)DataLen; idx++) { \ - if( (idx!=0) && ((idx%16)==0) ) printf("\n"); \ - if((idx % 4) == 0) printf(" 0x"); \ - printf("%.2x", Data[idx]); \ - } \ - printf("\n"); \ -} +#define PrintBYTE(msg, Data, DataLen) { \ + int idx; \ + printf("%10s =", msg); \ + for (idx = 0; idx < (int)DataLen; idx++) { \ + if ((idx != 0) && ((idx%16) == 0)) printf("\n"); \ + if ((idx % 4) == 0) printf(" 0x"); \ + printf("%.2x", Data[idx]); \ + } \ + printf("\n"); \ + } #define g_bTAdbug 0 #define TA_PRINT(fmt...) \ - do {if (g_bTAdbug) printf(fmt);}while(0) + do {if (g_bTAdbug) printf(fmt); } while (0) #define TA_ERROR(fmt...) \ - do {if (g_bTAdbug) printf(fmt);}while(0) + do {if (g_bTAdbug) printf(fmt); } while (0) /*! \brief convert 32-bit unit to 4 byte */ #undef GET_UINT32 -#define GET_UINT32(n,b,i) \ - { \ - (n) = ((unsigned int)((b)[(i) ]) << 24 ) \ - | ((unsigned int)((b)[(i) + 1]) << 16 ) \ - | ((unsigned int)((b)[(i) + 2]) << 8 ) \ - | ((unsigned int)((b)[(i) + 3]) ); \ - } +#define GET_UINT32(n, b, i) \ + { \ + (n) = ((unsigned int)((b)[(i)]) << 24) \ + | ((unsigned int)((b)[(i) + 1]) << 16) \ + | ((unsigned int)((b)[(i) + 2]) << 8) \ + | ((unsigned int)((b)[(i) + 3])); \ + } -int hwcrypto_context_alloc(unsigned int algorithm, uci_engine_config_e config, UCI_HANDLE* context) +int hwcrypto_context_alloc(unsigned int algorithm, uci_engine_config_e config, + UCI_HANDLE *context) { - uci_context_s* ctx; + uci_context_s *ctx; if (context == NULL) - { return UCI_ERROR; - } - ctx = (uci_context_s*)malloc(sizeof(uci_context_s)); - if(ctx == NULL) - { + ctx = (uci_context_s *)malloc(sizeof(uci_context_s)); + + if (ctx == NULL) return UCI_MEM_ALLOR_ERROR; - } ctx->config = config; ctx->alg = algorithm; @@ -88,8 +86,8 @@ int hwcrypto_context_alloc(unsigned int algorithm, uci_engine_config_e config, U #else ctx->imp = create_CryptoCoreContainer(algorithm); #endif - if(ctx->imp == NULL) - { + + if (ctx->imp == NULL) { free(ctx); return UCI_MEM_ALLOR_ERROR; } @@ -98,485 +96,456 @@ int hwcrypto_context_alloc(unsigned int algorithm, uci_engine_config_e config, U return UCI_SUCCESS; } -int hwcrypto_context_free( UCI_HANDLE oh ) +int hwcrypto_context_free(UCI_HANDLE oh) { - uci_context_s *pctx = (uci_context_s*)oh; - if(pctx == NULL) - { + uci_context_s *pctx = (uci_context_s *)oh; + + if (pctx == NULL) return UCI_INVALID_HANDLE; - } - if(pctx->imp != NULL) - { + + if (pctx->imp != NULL) { #ifndef PC_I586 free(pctx->imp); #else - destroy_CryptoCoreContainer((CryptoCoreContainer*)pctx->imp); + destroy_CryptoCoreContainer((CryptoCoreContainer *)pctx->imp); #endif pctx->imp = NULL; } + #ifndef PC_I586 + /*close crypto handle*/ - if(pctx->handle >= 0) - { + if (pctx->handle >= 0) close(pctx->handle); - } + #endif free(pctx); pctx = NULL; return UCI_SUCCESS; } -int hwcrypto_se_init(UCI_HANDLE oh, unsigned int mode, unsigned int padding, unsigned char *key, unsigned int key_len, unsigned char *iv) +int hwcrypto_se_init(UCI_HANDLE oh, unsigned int mode, unsigned int padding, + unsigned char *key, unsigned int key_len, unsigned char *iv) { #ifndef PC_I586 uci_context_s *pctx; struct crypt_info *info; unsigned int keytype; int ret = 0; - pctx = (uci_context_s*)oh; - if(pctx == NULL) - { + pctx = (uci_context_s *)oh; + + if (pctx == NULL) return UCI_INVALID_HANDLE; - } - info = (struct crypt_info*)pctx->imp; + + info = (struct crypt_info *)pctx->imp; keytype = SDRM_HIGH_HALF(pctx->config); - if(keytype == UCI_USER_KEY && key == NULL) - { + if (keytype == UCI_USER_KEY && key == NULL) return UCI_ERROR; - } - switch(pctx->alg) - { - case ID_UCI_AES128: - switch(mode) - { - case ID_UCI_ENC_CBC: - if(padding == ID_UCI_PKCS5) - { - info->mode = MI_AES_CBC_PAD; - } - else if(padding == ID_UCI_NO_PADDING) - { - info->mode = MI_AES_CBC; - } - else - { - return UCI_INVALID_ARGUMENT; - } - pctx->mode = ID_UCI_ENC_CBC; - break; - case ID_UCI_ENC_CTR: - if(padding == ID_UCI_PKCS5) - { - info->mode = MI_AES_CTR_PAD; - } - else if(padding == ID_UCI_NO_PADDING) - { - info->mode = MI_AES_CTR; - } - else - { - return UCI_INVALID_ARGUMENT; - } - pctx->mode = ID_UCI_ENC_CTR; - break; - case ID_UCI_ENC_ECB: - if(padding == ID_UCI_PKCS5) - { - info->mode = MI_AES_ECB_PAD; - } - else if(padding == ID_UCI_NO_PADDING) - { - info->mode = MI_AES_ECB; - } - else - { - return UCI_INVALID_ARGUMENT; - } - pctx->mode = ID_UCI_ENC_ECB; - break; - case ID_UCI_DEC_CBC: - if(padding == ID_UCI_PKCS5) - { - info->mode = MI_AES_CBC_PAD | _MODE_DEC_; - } - else if(padding == ID_UCI_NO_PADDING) - { - info->mode = MI_AES_CBC | _MODE_DEC_; - } - else - { - return UCI_INVALID_ARGUMENT; - } - pctx->mode = ID_UCI_DEC_CBC; - break; - case ID_UCI_DEC_CTR: - if(padding == ID_UCI_PKCS5) - { - info->mode = MI_AES_CTR_PAD | _MODE_DEC_; - } - else if(padding == ID_UCI_NO_PADDING) - { - info->mode = MI_AES_CTR | _MODE_DEC_; - } - else - { - return UCI_INVALID_ARGUMENT; - } - pctx->mode = ID_UCI_DEC_CTR; - break; - case ID_UCI_DEC_ECB: - if(padding == ID_UCI_PKCS5) - { - info->mode = MI_AES_ECB_PAD | _MODE_DEC_; - } - else if(padding == ID_UCI_NO_PADDING) - { - info->mode = MI_AES_ECB | _MODE_DEC_; - } - else - { - return UCI_INVALID_ARGUMENT; - } - pctx->mode = ID_UCI_DEC_ECB; - break; - default: - return UCI_INVALID_ARGUMENT; - } - info->keylen = 16; - info->ivlen = 16; + switch (pctx->alg) { + case ID_UCI_AES128: + switch (mode) { + case ID_UCI_ENC_CBC: + if (padding == ID_UCI_PKCS5) + info->mode = MI_AES_CBC_PAD; + + else if (padding == ID_UCI_NO_PADDING) + info->mode = MI_AES_CBC; + + else + return UCI_INVALID_ARGUMENT; + + pctx->mode = ID_UCI_ENC_CBC; break; - case ID_UCI_AES256:/*now only support ecb and ctr*/ - switch(mode) - { - case ID_UCI_ENC_ECB: - if(padding == ID_UCI_PKCS5) - { - info->mode = MI_AES_ECB_PAD; - } - else if(padding == ID_UCI_NO_PADDING) - { - info->mode = MI_AES_ECB; - } - else - { - return UCI_INVALID_ARGUMENT; - } - pctx->mode = ID_UCI_ENC_ECB; - break; - case ID_UCI_DEC_ECB: - if(padding == ID_UCI_PKCS5) - { - info->mode = MI_AES_ECB_PAD | _MODE_DEC_; - } - else if(padding == ID_UCI_NO_PADDING) - { - info->mode = MI_AES_ECB | _MODE_DEC_; - } - else - { - return UCI_INVALID_ARGUMENT; - } - pctx->mode = ID_UCI_DEC_ECB; - break; - case ID_UCI_ENC_CTR: - if(padding == ID_UCI_PKCS5) - { - info->mode = MI_AES_CTR_PAD; - } - else if(padding == ID_UCI_NO_PADDING) - { - info->mode = MI_AES_CTR; - } - else - { - return UCI_INVALID_ARGUMENT; - } - pctx->mode = ID_UCI_ENC_CTR; - break; - case ID_UCI_DEC_CTR: - if(padding == ID_UCI_PKCS5) - { - info->mode = MI_AES_CTR_PAD | _MODE_DEC_; - } - else if(padding == ID_UCI_NO_PADDING) - { - info->mode = MI_AES_CTR | _MODE_DEC_; - } - else - { - return UCI_INVALID_ARGUMENT; - } - pctx->mode = ID_UCI_DEC_CTR; - break; - default: - return UCI_INVALID_ARGUMENT; - } - info->keylen = 32; - info->ivlen = 16; + + case ID_UCI_ENC_CTR: + if (padding == ID_UCI_PKCS5) + info->mode = MI_AES_CTR_PAD; + + else if (padding == ID_UCI_NO_PADDING) + info->mode = MI_AES_CTR; + + else + return UCI_INVALID_ARGUMENT; + + pctx->mode = ID_UCI_ENC_CTR; break; - case ID_UCI_DES: - switch(mode) - { - case ID_UCI_ENC_CBC: - if(padding == ID_UCI_PKCS5) - { - info->mode = MI_DES_CBC_PAD; - } - else if(padding == ID_UCI_NO_PADDING) - { - info->mode = MI_DES_CBC; - } - else - { - return UCI_INVALID_ARGUMENT; - } - pctx->mode = ID_UCI_ENC_CBC; - break; - case ID_UCI_ENC_ECB: - if(padding == ID_UCI_PKCS5) - { - info->mode = MI_DES_ECB_PAD; - } - else if(padding == ID_UCI_NO_PADDING) - { - info->mode = MI_DES_ECB; - } - else - { - return UCI_INVALID_ARGUMENT; - } - pctx->mode = ID_UCI_ENC_ECB; - break; - case ID_UCI_DEC_CBC: - if(padding == ID_UCI_PKCS5) - { - info->mode = MI_DES_CBC_PAD | _MODE_DEC_; - } - else if(padding == ID_UCI_NO_PADDING) - { - info->mode = MI_DES_CBC | _MODE_DEC_; - } - else - { - return UCI_INVALID_ARGUMENT; - } - pctx->mode = ID_UCI_DEC_CBC; - break; - case ID_UCI_DEC_ECB: - if(padding == ID_UCI_PKCS5) - { - info->mode = MI_DES_ECB_PAD | _MODE_DEC_; - } - else if(padding == ID_UCI_NO_PADDING) - { - info->mode = MI_DES_ECB | _MODE_DEC_; - } - else - { - return UCI_INVALID_ARGUMENT; - } - pctx->mode = ID_UCI_DEC_ECB; - break; - default: - return UCI_INVALID_ARGUMENT; - } - info->keylen = 8; - info->ivlen = 8; + case ID_UCI_ENC_ECB: + if (padding == ID_UCI_PKCS5) + info->mode = MI_AES_ECB_PAD; + + else if (padding == ID_UCI_NO_PADDING) + info->mode = MI_AES_ECB; + + else + return UCI_INVALID_ARGUMENT; + + pctx->mode = ID_UCI_ENC_ECB; break; - case ID_UCI_TDES: - switch(mode) - { - case ID_UCI_ENC_CBC: - if(padding == ID_UCI_PKCS5) - { - info->mode = MI_TDES_CBC_PAD; - } - else if(padding == ID_UCI_NO_PADDING) - { - info->mode = MI_TDES_CBC; - } - else - { - return UCI_INVALID_ARGUMENT; - } - pctx->mode = ID_UCI_ENC_CBC; - break; - case ID_UCI_ENC_ECB: - if(padding == ID_UCI_PKCS5) - { - info->mode = MI_TDES_ECB_PAD; - } - else if(padding == ID_UCI_NO_PADDING) - { - info->mode = MI_TDES_ECB; - } - else - { - return UCI_INVALID_ARGUMENT; - } - pctx->mode = ID_UCI_ENC_ECB; - break; - case ID_UCI_DEC_CBC: - if(padding == ID_UCI_PKCS5) - { - info->mode = MI_TDES_CBC_PAD | _MODE_DEC_; - } - else if(padding == ID_UCI_NO_PADDING) - { - info->mode = MI_TDES_CBC | _MODE_DEC_; - } - else - { - return UCI_INVALID_ARGUMENT; - } - pctx->mode = ID_UCI_ENC_ECB; - break; - case ID_UCI_DEC_ECB: - if(padding == ID_UCI_PKCS5) - { - info->mode = MI_TDES_ECB_PAD | _MODE_DEC_; - } - else if(padding == ID_UCI_NO_PADDING) - { - info->mode = MI_TDES_ECB | _MODE_DEC_; - } - else - { - return UCI_INVALID_ARGUMENT; - } - pctx->mode = ID_UCI_DEC_ECB; - break; - default: - return UCI_INVALID_ARGUMENT; - } - info->keylen = 24; - info->ivlen = 8; + case ID_UCI_DEC_CBC: + if (padding == ID_UCI_PKCS5) + info->mode = MI_AES_CBC_PAD | _MODE_DEC_; + + else if (padding == ID_UCI_NO_PADDING) + info->mode = MI_AES_CBC | _MODE_DEC_; + + else + return UCI_INVALID_ARGUMENT; + + pctx->mode = ID_UCI_DEC_CBC; break; + + case ID_UCI_DEC_CTR: + if (padding == ID_UCI_PKCS5) + info->mode = MI_AES_CTR_PAD | _MODE_DEC_; + + else if (padding == ID_UCI_NO_PADDING) + info->mode = MI_AES_CTR | _MODE_DEC_; + + else + return UCI_INVALID_ARGUMENT; + + pctx->mode = ID_UCI_DEC_CTR; + break; + + case ID_UCI_DEC_ECB: + if (padding == ID_UCI_PKCS5) + info->mode = MI_AES_ECB_PAD | _MODE_DEC_; + + else if (padding == ID_UCI_NO_PADDING) + info->mode = MI_AES_ECB | _MODE_DEC_; + + else + return UCI_INVALID_ARGUMENT; + + pctx->mode = ID_UCI_DEC_ECB; + break; + default: - return UCI_INVALID_HANDLE; + return UCI_INVALID_ARGUMENT; + } - } + info->keylen = 16; + info->ivlen = 16; + break; - /*set info key*/ + case ID_UCI_AES256:/*now only support ecb and ctr*/ + switch (mode) { + case ID_UCI_ENC_ECB: + if (padding == ID_UCI_PKCS5) + info->mode = MI_AES_ECB_PAD; + + else if (padding == ID_UCI_NO_PADDING) + info->mode = MI_AES_ECB; - switch(keytype) - { - case UCI_USER_KEY: - info->keytype = KEYID_USER_KEY; - if(key_len != 8 && key_len != 16 && key_len != 24 && key_len != 32) - { + else return UCI_INVALID_ARGUMENT; - } - memcpy(info->key, key, key_len); + + pctx->mode = ID_UCI_ENC_ECB; break; - case UCI_SECRET_KEY: - info->keytype = KEYID_SECURE_KEY; + + case ID_UCI_DEC_ECB: + if (padding == ID_UCI_PKCS5) + info->mode = MI_AES_ECB_PAD | _MODE_DEC_; + + else if (padding == ID_UCI_NO_PADDING) + info->mode = MI_AES_ECB | _MODE_DEC_; + + else + return UCI_INVALID_ARGUMENT; + + pctx->mode = ID_UCI_DEC_ECB; break; - case UCI_MASTER_KEY: - info->keytype = KEYID_MASTER_KEY; + + case ID_UCI_ENC_CTR: + if (padding == ID_UCI_PKCS5) + info->mode = MI_AES_CTR_PAD; + + else if (padding == ID_UCI_NO_PADDING) + info->mode = MI_AES_CTR; + + else + return UCI_INVALID_ARGUMENT; + + pctx->mode = ID_UCI_ENC_CTR; + break; + + case ID_UCI_DEC_CTR: + if (padding == ID_UCI_PKCS5) + info->mode = MI_AES_CTR_PAD | _MODE_DEC_; + + else if (padding == ID_UCI_NO_PADDING) + info->mode = MI_AES_CTR | _MODE_DEC_; + + else + return UCI_INVALID_ARGUMENT; + + pctx->mode = ID_UCI_DEC_CTR; break; - default : + + default: return UCI_INVALID_ARGUMENT; + } + + info->keylen = 32; + info->ivlen = 16; + break; + + case ID_UCI_DES: + switch (mode) { + case ID_UCI_ENC_CBC: + if (padding == ID_UCI_PKCS5) + info->mode = MI_DES_CBC_PAD; + + else if (padding == ID_UCI_NO_PADDING) + info->mode = MI_DES_CBC; + + else + return UCI_INVALID_ARGUMENT; + + pctx->mode = ID_UCI_ENC_CBC; + break; + + case ID_UCI_ENC_ECB: + if (padding == ID_UCI_PKCS5) + info->mode = MI_DES_ECB_PAD; + + else if (padding == ID_UCI_NO_PADDING) + info->mode = MI_DES_ECB; + + else + return UCI_INVALID_ARGUMENT; + + pctx->mode = ID_UCI_ENC_ECB; + break; + + case ID_UCI_DEC_CBC: + if (padding == ID_UCI_PKCS5) + info->mode = MI_DES_CBC_PAD | _MODE_DEC_; + + else if (padding == ID_UCI_NO_PADDING) + info->mode = MI_DES_CBC | _MODE_DEC_; + + else + return UCI_INVALID_ARGUMENT; + + pctx->mode = ID_UCI_DEC_CBC; + break; + + case ID_UCI_DEC_ECB: + if (padding == ID_UCI_PKCS5) + info->mode = MI_DES_ECB_PAD | _MODE_DEC_; + + else if (padding == ID_UCI_NO_PADDING) + info->mode = MI_DES_ECB | _MODE_DEC_; + + else + return UCI_INVALID_ARGUMENT; + + pctx->mode = ID_UCI_DEC_ECB; + break; + + default: + return UCI_INVALID_ARGUMENT; + + } + + info->keylen = 8; + info->ivlen = 8; + break; + + case ID_UCI_TDES: + switch (mode) { + case ID_UCI_ENC_CBC: + if (padding == ID_UCI_PKCS5) + info->mode = MI_TDES_CBC_PAD; + + else if (padding == ID_UCI_NO_PADDING) + info->mode = MI_TDES_CBC; + + else + return UCI_INVALID_ARGUMENT; + + pctx->mode = ID_UCI_ENC_CBC; + break; + + case ID_UCI_ENC_ECB: + if (padding == ID_UCI_PKCS5) + info->mode = MI_TDES_ECB_PAD; + + else if (padding == ID_UCI_NO_PADDING) + info->mode = MI_TDES_ECB; + + else + return UCI_INVALID_ARGUMENT; + + pctx->mode = ID_UCI_ENC_ECB; + break; + + case ID_UCI_DEC_CBC: + if (padding == ID_UCI_PKCS5) + info->mode = MI_TDES_CBC_PAD | _MODE_DEC_; + + else if (padding == ID_UCI_NO_PADDING) + info->mode = MI_TDES_CBC | _MODE_DEC_; + + else + return UCI_INVALID_ARGUMENT; + + pctx->mode = ID_UCI_ENC_ECB; + break; + + case ID_UCI_DEC_ECB: + if (padding == ID_UCI_PKCS5) + info->mode = MI_TDES_ECB_PAD | _MODE_DEC_; + + else if (padding == ID_UCI_NO_PADDING) + info->mode = MI_TDES_ECB | _MODE_DEC_; + + else + return UCI_INVALID_ARGUMENT; + + pctx->mode = ID_UCI_DEC_ECB; + break; + + default: + return UCI_INVALID_ARGUMENT; + + } + + info->keylen = 24; + info->ivlen = 8; + break; + + default: + return UCI_INVALID_HANDLE; + } + + /*set info key*/ + + switch (keytype) { + case UCI_USER_KEY: + info->keytype = KEYID_USER_KEY; + + if (key_len != 8 && key_len != 16 && key_len != 24 && key_len != 32) + return UCI_INVALID_ARGUMENT; + + memcpy(info->key, key, key_len); + break; + + case UCI_SECRET_KEY: + info->keytype = KEYID_SECURE_KEY; + break; + + case UCI_MASTER_KEY: + info->keytype = KEYID_MASTER_KEY; + break; + + default: + return UCI_INVALID_ARGUMENT; + } + /*setiv*/ - if(iv) - { + if (iv) memcpy(info->iv, iv, info->ivlen); - } + else - { memset(info->iv, 0x0, info->ivlen); - } - pctx->handle = open("/dev/crypto", 0, 0 ); //return hndl; + + pctx->handle = open("/dev/crypto", 0, 0); //return hndl; + //TA_PRINT("hand = %d \n",pctx->handle); - if(pctx->handle < 0) - { + if (pctx->handle < 0) return UCI_ERROR; - } - if (ret = ioctl(pctx->handle, IOCTL_CRYPTO_INIT, info)) - { - TA_PRINT("error:ioctl(hndl, IOCTL_CRYPTO_INIT, info) returned %d\n",ret); + + if (ret = ioctl(pctx->handle, IOCTL_CRYPTO_INIT, info)) { + TA_PRINT("error:ioctl(hndl, IOCTL_CRYPTO_INIT, info) returned %d\n", ret); return UCI_ERROR; } + return UCI_SUCCESS; #else int ret = UCI_ERROR; - uci_context_s *pctx = (uci_context_s*)oh; + uci_context_s *pctx = (uci_context_s *)oh; unsigned int keytype; unsigned int alg; - //!AS current hw is not ready, so using SW pseduo way temproray. - unsigned char hwkey_master[32]={ 0xAB, 0x12, 0x45, 0x67, 0x3F, 0x80, 0x98, 0x35, - 0x06, 0x4F, 0x33, 0x39, 0x72, 0x1C, 0xDF, 0x23, - 0xAB, 0x12, 0x45, 0x67, 0x3F, 0x80, 0x98, 0x35, - 0x06, 0x4F, 0x33, 0x39, 0x72, 0x1C, 0xDF, 0x23}; - unsigned char hwiv_master[16] ={ 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, - 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF}; - unsigned char hwkey_unique[32]={ 0xF0, 0x22, 0x34, 0x67, 0x66, 0x88, 0xAB, 0xCD, - 0x12, 0x67, 0x89, 0x54, 0x32, 0x10, 0xCC, 0xFE, - 0xAB, 0x12, 0x45, 0x67, 0x3F, 0x80, 0x98, 0x35, - 0x06, 0x4F, 0x33, 0x39, 0x72, 0x1C, 0xDF, 0x23}; - unsigned char hwiv_unique[16] ={ 0xFF, 0xEE, 0xDD, 0xCC, 0xBB, 0xAA, 0x99, 0x88, - 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11, 0x00}; - if(pctx == NULL) - { + //!AS current hw is not ready, so using SW pseduo way temproray. + unsigned char hwkey_master[32] = { 0xAB, 0x12, 0x45, 0x67, 0x3F, 0x80, 0x98, 0x35, + 0x06, 0x4F, 0x33, 0x39, 0x72, 0x1C, 0xDF, 0x23, + 0xAB, 0x12, 0x45, 0x67, 0x3F, 0x80, 0x98, 0x35, + 0x06, 0x4F, 0x33, 0x39, 0x72, 0x1C, 0xDF, 0x23 + }; + unsigned char hwiv_master[16] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, + 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF + }; + unsigned char hwkey_unique[32] = { 0xF0, 0x22, 0x34, 0x67, 0x66, 0x88, 0xAB, 0xCD, + 0x12, 0x67, 0x89, 0x54, 0x32, 0x10, 0xCC, 0xFE, + 0xAB, 0x12, 0x45, 0x67, 0x3F, 0x80, 0x98, 0x35, + 0x06, 0x4F, 0x33, 0x39, 0x72, 0x1C, 0xDF, 0x23 + }; + unsigned char hwiv_unique[16] = { 0xFF, 0xEE, 0xDD, 0xCC, 0xBB, 0xAA, 0x99, 0x88, + 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11, 0x00 + }; + + if (pctx == NULL) return UCI_INVALID_HANDLE; - } + alg = pctx->alg; - switch(alg) - { - case ID_UCI_AES128: - key_len = 16; - break; - case ID_UCI_AES256: - key_len = 32; - break; - case ID_UCI_DES: - key_len = 8; - break; - case ID_UCI_TDES: - key_len = 24; + + switch (alg) { + case ID_UCI_AES128: + key_len = 16; + break; + + case ID_UCI_AES256: + key_len = 32; + break; + + case ID_UCI_DES: + key_len = 8; + break; + + case ID_UCI_TDES: + key_len = 24; } + keytype = SDRM_HIGH_HALF(pctx->config); - if (keytype != UCI_USER_KEY) - { - if(keytype == UCI_MASTER_KEY) - { - ret = ((CryptoCoreContainer *)pctx->imp)->SE_init((CryptoCoreContainer *)pctx->imp, mode, padding, hwkey_master,key_len,hwiv_master); + if (keytype != UCI_USER_KEY) { + if (keytype == UCI_MASTER_KEY) { + ret = ((CryptoCoreContainer *)pctx->imp)->SE_init((CryptoCoreContainer *) + pctx->imp, mode, padding, hwkey_master, key_len, hwiv_master); + + } else if (keytype == UCI_SECRET_KEY) { + ret = ((CryptoCoreContainer *)pctx->imp)->SE_init((CryptoCoreContainer *) + pctx->imp, mode, padding, hwkey_unique, key_len, hwiv_unique); } - else if(keytype == UCI_SECRET_KEY) - { - ret =((CryptoCoreContainer *)pctx->imp)->SE_init((CryptoCoreContainer *)pctx->imp, mode, padding, hwkey_unique,key_len,hwiv_unique); - } - } - else - { - ret = ((CryptoCoreContainer *)pctx->imp)->SE_init((CryptoCoreContainer *)pctx->imp, mode, padding, key,key_len, iv); + } else { + ret = ((CryptoCoreContainer *)pctx->imp)->SE_init((CryptoCoreContainer *) + pctx->imp, mode, padding, key, key_len, iv); } - if(ret == CRYPTO_INVALID_ARGUMENT) - { + if (ret == CRYPTO_INVALID_ARGUMENT) return UCI_INVALID_ARGUMENT; - } - if(ret != CRYPTO_SUCCESS) - { + + if (ret != CRYPTO_SUCCESS) return UCI_ERROR; - } return UCI_SUCCESS; #endif } -int hwcrypto_se_process(UCI_HANDLE oh, unsigned char *input, unsigned int input_len, unsigned char *output, unsigned int *output_len) +int hwcrypto_se_process(UCI_HANDLE oh, unsigned char *input, + unsigned int input_len, unsigned char *output, unsigned int *output_len) { #ifndef PC_I586 uci_context_s *pctx = NULL; @@ -588,37 +557,34 @@ int hwcrypto_se_process(UCI_HANDLE oh, unsigned char *input, unsigned int input_ memset(&oper, 0, sizeof(struct crypt_oper)); - pctx = (uci_context_s*)oh; - if(pctx == NULL) - { + pctx = (uci_context_s *)oh; + + if (pctx == NULL) return UCI_INVALID_HANDLE; - } - if(pctx->handle < 0) - { + + if (pctx->handle < 0) { TA_PRINT("Handle error \n"); return UCI_ERROR; } - if(output_len != NULL) - { + + if (output_len != NULL) *output_len = 0; - } - if(alg == ID_UCI_AES128 || alg == ID_UCI_AES256) - { - if((input_len % 16) != 0) - { + if (alg == ID_UCI_AES128 || alg == ID_UCI_AES256) { + if ((input_len % 16) != 0) { TA_PRINT("input_len error\n"); return UCI_ERROR; } + blocksize = 16; } - if(alg == ID_UCI_DES || alg == ID_UCI_TDES) - { - if((input_len % 8) != 0) - { + + if (alg == ID_UCI_DES || alg == ID_UCI_TDES) { + if ((input_len % 8) != 0) { TA_PRINT("input_len error\n"); return UCI_ERROR; } + blocksize = 8; } @@ -626,9 +592,9 @@ int hwcrypto_se_process(UCI_HANDLE oh, unsigned char *input, unsigned int input_ oper.src_len = input_len; oper.dst_addr = output; oper.dst_len = output_len; - if (ret = ioctl(pctx->handle, IOCTL_CRYPTO_CRYPT, &oper)) - { - TA_PRINT("error:ioctl(pctx->handle , 1, &oper) returned %d\n",ret); + + if (ret = ioctl(pctx->handle, IOCTL_CRYPTO_CRYPT, &oper)) { + TA_PRINT("error:ioctl(pctx->handle , 1, &oper) returned %d\n", ret); return UCI_ERROR; } @@ -636,21 +602,23 @@ int hwcrypto_se_process(UCI_HANDLE oh, unsigned char *input, unsigned int input_ #else int ret; uci_context_s *ucictx = (uci_context_s *)oh; - if(ucictx == NULL) - { + + if (ucictx == NULL) return UCI_INVALID_HANDLE; - } - ret = ((CryptoCoreContainer *)ucictx->imp)->SE_process((CryptoCoreContainer*)(ucictx->imp), input, input_len, output, output_len); - if(ret != CRYPTO_SUCCESS) - { + + ret = ((CryptoCoreContainer *)ucictx->imp)->SE_process((CryptoCoreContainer *)( + ucictx->imp), input, input_len, output, output_len); + + if (ret != CRYPTO_SUCCESS) return UCI_ERROR; - } + return UCI_SUCCESS; #endif } -int hwcrypto_se_final(UCI_HANDLE oh, unsigned char *input, unsigned int input_len, unsigned char *output, unsigned int *output_len) +int hwcrypto_se_final(UCI_HANDLE oh, unsigned char *input, + unsigned int input_len, unsigned char *output, unsigned int *output_len) { #ifndef PC_I586 uci_context_s *pctx = NULL; @@ -665,59 +633,55 @@ int hwcrypto_se_final(UCI_HANDLE oh, unsigned char *input, unsigned int input_l unsigned int lastlen = 0; unsigned char padding[32] = {0x0}; - memset(padding, 0, sizeof(padding)/sizeof(padding[0])); + memset(padding, 0, sizeof(padding) / sizeof(padding[0])); memset(&oper, 0, sizeof(struct crypt_oper)); - pctx = (uci_context_s*)oh; - if(pctx == NULL) - { + pctx = (uci_context_s *)oh; + + if (pctx == NULL) + return UCI_INVALID_HANDLE; + + info = (struct crypt_info *) pctx->imp; + + if (info == NULL) return UCI_INVALID_HANDLE; - } - info = (struct crypt_info*) pctx->imp; - if(info == NULL) - { - return UCI_INVALID_HANDLE; - } hndl = pctx->handle; alg = pctx->alg; - if(hndl < 0) - { + + if (hndl < 0) return UCI_INVALID_HANDLE; - } - if(alg == ID_UCI_AES128 || alg == ID_UCI_AES256) - { + + if (alg == ID_UCI_AES128 || alg == ID_UCI_AES256) blocksize = 16; - } - else if(alg == ID_UCI_DES|| alg == ID_UCI_TDES) - { + + else if (alg == ID_UCI_DES || alg == ID_UCI_TDES) blocksize = 8; - } - else - { + + else { return -UCI_INVALID_HANDLE; + UCI_INVALID_HANDLE; } - if(pctx->mode == ID_UCI_ENC_CBC || pctx->mode == ID_UCI_ENC_CTR || pctx->mode == ID_UCI_ENC_ECB)/*encrypt*/ - { + if (pctx->mode == ID_UCI_ENC_CBC || pctx->mode == ID_UCI_ENC_CTR || + pctx->mode == ID_UCI_ENC_ECB) { /*encrypt*/ lastlen = input_len % blocksize; - if(input_len > lastlen ) /* last blocksize is bigger than blocksize or equal to blocksize*/ - { - len = input_len -lastlen; - ret = hwcrypto_se_process(oh,input,len,output,output_len); - if(ret != UCI_SUCCESS) - { + + if (input_len > + lastlen) { /* last blocksize is bigger than blocksize or equal to blocksize*/ + len = input_len - lastlen; + ret = hwcrypto_se_process(oh, input, len, output, output_len); + + if (ret != UCI_SUCCESS) { TA_PRINT("hwcrypto_se_process error \n"); return ret; } } - if(MI_GET_PADDING(info->mode) == _PAD_PKCS7_)/*do padding*/ - { - if(lastlen >0) - { + if (MI_GET_PADDING(info->mode) == _PAD_PKCS7_) { /*do padding*/ + + if (lastlen > 0) memcpy(padding, input + len, lastlen); - } + memset(padding + lastlen, blocksize - lastlen, blocksize - lastlen); oper.src_addr = padding; oper.src_len = blocksize; @@ -725,74 +689,68 @@ UCI_INVALID_HANDLE; oper.dst_len = output_len; //oper.final = 1; - if(ret = ioctl(hndl, IOCTL_CRYPTO_CRYPT, &oper)) - { - TA_PRINT("error:ioctl(hndl, 1, &oper) returned %d\n",ret); + if (ret = ioctl(hndl, IOCTL_CRYPTO_CRYPT, &oper)) { + TA_PRINT("error:ioctl(hndl, 1, &oper) returned %d\n", ret); return UCI_ERROR; } *output_len = input_len - lastlen + blocksize; } - if(MI_GET_PADDING(info->mode) == _PAD_NO_)/*do padding*/ - { - if(lastlen >0) - { + + if (MI_GET_PADDING(info->mode) == _PAD_NO_) { /*do padding*/ + if (lastlen > 0) memcpy(output + len, output + len, lastlen); - } + *output_len = input_len ; } - } - else/*decrypt*/ - { + } else { /*decrypt*/ lastlen = input_len % blocksize; - if(input_len > lastlen) - { - len = input_len -lastlen; - } - if(len > 0) - { + + if (input_len > lastlen) + len = input_len - lastlen; + + if (len > 0) { oper.src_addr = (char *)input; oper.src_len = len; oper.dst_addr = (char *)output; oper.dst_len = output_len; + //oper.final = 1; - if (ret = ioctl(hndl, IOCTL_CRYPTO_CRYPT, &oper)) - { - TA_PRINT("error:ioctl(hndl, 1, &oper) returned %d\n",ret); + if (ret = ioctl(hndl, IOCTL_CRYPTO_CRYPT, &oper)) { + TA_PRINT("error:ioctl(hndl, 1, &oper) returned %d\n", ret); return UCI_ERROR; } } - if(MI_GET_PADDING(info->mode) == _PAD_NO_)/*do padding*/ - { - if(lastlen >0) - { + + if (MI_GET_PADDING(info->mode) == _PAD_NO_) { /*do padding*/ + if (lastlen > 0) memcpy(output + len, input + len, lastlen); - } + *output_len = input_len ; } - if(MI_GET_PADDING(info->mode) == _PAD_PKCS7_)/*de padding*/ - { - if(lastlen >0) - { - TA_PRINT("psrc_len is not aligen to %d\n",blocksize); + + if (MI_GET_PADDING(info->mode) == _PAD_PKCS7_) { /*de padding*/ + if (lastlen > 0) { + TA_PRINT("psrc_len is not aligen to %d\n", blocksize); return UCI_ERROR; } - padlen = output[input_len -1]; + padlen = output[input_len - 1]; + //PrintBYTE("padding",output,input_len); //PrintBYTE("input",input,input_len); - if(padlen < 1 || padlen > 16) - { + if (padlen < 1 || padlen > 16) { *output_len = 0; - TA_PRINT("padding size{%d} is incorretc ",padlen); + TA_PRINT("padding size{%d} is incorretc ", padlen); return UCI_ERROR; } + memset(padding, padlen, blocksize); - if(memcmp(output + input_len - padlen ,padding, padlen) != 0) - { + + if (memcmp(output + input_len - padlen, padding, padlen) != 0) { *output_len = 0; - TA_PRINT("padding size{%d} is incorretc ",padlen); + TA_PRINT("padding size{%d} is incorretc ", padlen); return UCI_ERROR; } @@ -805,15 +763,16 @@ UCI_INVALID_HANDLE; #else int ret; uci_context_s *ucictx = (uci_context_s *)oh; - if(ucictx==NULL) - { + + if (ucictx == NULL) return UCI_INVALID_HANDLE; - } - ret = ((CryptoCoreContainer *)ucictx->imp)->SE_final((CryptoCoreContainer*)(ucictx->imp), input, input_len, output, output_len); - if(ret!=CRYPTO_SUCCESS) - { + + ret = ((CryptoCoreContainer *)ucictx->imp)->SE_final((CryptoCoreContainer *)( + ucictx->imp), input, input_len, output, output_len); + + if (ret != CRYPTO_SUCCESS) return UCI_ERROR; - } + return UCI_SUCCESS; #endif -- 2.7.4 From 1712f0ba24e0bca9e40f09b0518da630ceb177be Mon Sep 17 00:00:00 2001 From: Uladzislau Harbuz Date: Tue, 24 Oct 2017 17:40:16 +0200 Subject: [PATCH 11/16] Remove dead code related to downloadable TA Change-Id: Ieff27f29b34432c0d572b85a652c05246541a9eb --- packaging/tef-simulator.spec | 1 - simulatordaemon/CMakeLists.txt | 1 - simulatordaemon/inc/SecurityContext.h | 12 ---- simulatordaemon/inc/TAFactory.h | 2 +- simulatordaemon/src/SecurityContext.cpp | 79 ---------------------- .../src/TABinaryManager/TABinaryManager.cpp | 18 ++--- .../src/TABinaryManager/TABinaryManager.h | 4 +- simulatordaemon/src/TAFactory.cpp | 16 ++--- 8 files changed, 17 insertions(+), 116 deletions(-) diff --git a/packaging/tef-simulator.spec b/packaging/tef-simulator.spec index 99f1571..e9696c5 100644 --- a/packaging/tef-simulator.spec +++ b/packaging/tef-simulator.spec @@ -13,7 +13,6 @@ BuildRequires: pkgconfig(openssl) BuildRequires: pkgconfig(cynara-client) BuildRequires: pkgconfig(cynara-session) BuildRequires: pkgconfig(cynara-creds-socket) -BuildRequires: pkgconfig(libtzplatform-config) BuildRequires: pkgconfig(security-manager) BuildRequires: pkgconfig(libsystemd-daemon) BuildRequires: pkgconfig(tef-libteec) diff --git a/simulatordaemon/CMakeLists.txt b/simulatordaemon/CMakeLists.txt index 033b098..21b789b 100644 --- a/simulatordaemon/CMakeLists.txt +++ b/simulatordaemon/CMakeLists.txt @@ -23,7 +23,6 @@ PKG_CHECK_MODULES(DAEMON_DEPS REQUIRED cynara-creds-socket security-manager libsystemd-daemon - libtzplatform-config dlog ) diff --git a/simulatordaemon/inc/SecurityContext.h b/simulatordaemon/inc/SecurityContext.h index 2ebf6b6..6dddfef 100644 --- a/simulatordaemon/inc/SecurityContext.h +++ b/simulatordaemon/inc/SecurityContext.h @@ -52,18 +52,6 @@ public: ~SecurityContext(); /** - * This function tries to find TA by name in directories, - * which are allowed for CA according to security policies - * - * @param taName Name of ta to connect for. - * @param allowedPath Out parameter, if function returns true, it - * contains found path to TA with given name, - * otherwise it's empty. - * @return true if TA was found, otherwise false. - */ - bool findRequestedTa(const std::string &taName, std::string& allowedPath); - -/** * Check if client has Tizen permission for use TEE. * * @param privilege Privilege to be checked for client. diff --git a/simulatordaemon/inc/TAFactory.h b/simulatordaemon/inc/TAFactory.h index 9063fed..3f7b092 100644 --- a/simulatordaemon/inc/TAFactory.h +++ b/simulatordaemon/inc/TAFactory.h @@ -55,7 +55,7 @@ private: TAFactory(); bool checkIfTARunning(string TAUUID); TAInstancePtr createUninitalizedTAInstance(string TAUUID, ISession* session); - bool launchTA(string TAPath, string TAUUID, std::stringstream& str, bool debug, pid_t& pid); + bool launchTA(string TAUUID, std::stringstream& str, bool debug, pid_t& pid); static void* waitForChild(void *pid); void cleanupTAInstance(pid_t PID); ~TAFactory(); diff --git a/simulatordaemon/src/SecurityContext.cpp b/simulatordaemon/src/SecurityContext.cpp index 3cd4e1a..109170f 100644 --- a/simulatordaemon/src/SecurityContext.cpp +++ b/simulatordaemon/src/SecurityContext.cpp @@ -24,22 +24,16 @@ #include "SecurityContext.h" #include #include -#include #include #include "log.h" -#include #include #include -#include #include #include #include using p_char = std::unique_ptr>; using p_cynara_conf = std::unique_ptr>; -using p_tzplatform_context = std::unique_ptr>; - -namespace fs = boost::filesystem; constexpr const char* SecurityContext::sysTaPaths[]; @@ -47,83 +41,10 @@ cynara* SecurityContext::_cynara = SecurityContext::initCynara(); pthread_mutex_t cynara_mutex = PTHREAD_MUTEX_INITIALIZER; -#define FILE_WAS_FOUND 1 -#define MAX_OPENED_FD 5 -#define MAX_PATH_LENGTH 100 #define BOOST_FILESYSTEM_VERSION 3 #define RETURN_UNLOCK(ret, mtx) {pthread_mutex_unlock(&mtx); return ret;} -std::string SecurityContext::getCaFullPathFromPkgId(char* pkgid) { - std::string path; - - tzplatform_variable ids[3] = {TZ_USER_APP, TZ_SYS_RW_APP, TZ_SYS_RO_APP}; - tzplatform_context *ctx; - if (tzplatform_context_create(&ctx) != 0) { - LOGE(SIM_DAEMON, "Can't create tizen context"); - return ""; - } - - p_tzplatform_context p_ctx(ctx, &tzplatform_context_destroy); - - socklen_t len = (socklen_t) sizeof(struct ucred); - struct ucred ucred; - - if (getsockopt(connFd, SOL_SOCKET, SO_PEERCRED, &ucred, &len) == -1) { - LOGE(SIM_DAEMON, "Can't get uid of client"); - return ""; - } - - auto clientUID = ucred.uid; - - if (tzplatform_context_set_user(p_ctx.get(), clientUID) != 0) { - LOGE(SIM_DAEMON, "Can not set user for context"); - return ""; - } - - for (auto &id : ids) { - path = std::move(tzplatform_context_getenv(p_ctx.get(), id)); - LOGD(SIM_DAEMON, "Path is : %s", path.c_str()); - if (!path.empty()) break; - } - - if (!fs::exists(path)) { - LOGE(SIM_DAEMON, "Path doesn't exist: %s", path.c_str()); - return ""; - } - if (fs::is_symlink(path)) { - path = fs::read_symlink(path).string(); - } - if (path.empty()) { - LOGE(SIM_DAEMON, "Bad CA path. Does this directory exist: %s ?", path.c_str()); - } - - path += "/" + std::string(pkgid) + TA_LOCAL_PATH; - LOGD(SIM_DAEMON, "Path: %s", path.c_str()); - if (!fs::exists(path)) { - LOGE(SIM_DAEMON, "Path: %s not found", path.c_str()); - return ""; - } - - return path; -} - - -bool SecurityContext::findRequestedTa(const std::string &ta_name, std::string &allowed_path) { - LOGD(SIM_DAEMON, "Entry"); - std::string ta_full_path; - /* Check if any of system ta directories contains our ta */ - for (const std::string& path : sysTaPaths) { - ta_full_path = path + ta_name; - if (fs::exists(ta_full_path)){ - allowed_path = path; - return true; - } - } - return false; -} - - bool SecurityContext::clientHasCynaraPermission(const std::string &privelege) { int ret = -1; diff --git a/simulatordaemon/src/TABinaryManager/TABinaryManager.cpp b/simulatordaemon/src/TABinaryManager/TABinaryManager.cpp index f0cae26..4561b2e 100644 --- a/simulatordaemon/src/TABinaryManager/TABinaryManager.cpp +++ b/simulatordaemon/src/TABinaryManager/TABinaryManager.cpp @@ -25,6 +25,7 @@ * Include files *-----------------------------------------------------------------------------*/ #include "TABinaryManager.h" +#include "Config.h" #include #include #include @@ -155,10 +156,10 @@ TABinaryManager* TABinaryManager::getInstance() { } /** - * This function add TA at given path to BinaryManager if it exists. + * This function add TA to BinaryManager if it exists. * @return On successful completion of above operations returns true else false. */ -bool TABinaryManager::initTAatPath(const string &path, const string &uuid) { +bool TABinaryManager::initTA(const string &uuid) { LOGD(SIM_DAEMON, "Entry"); pthread_rwlock_wrlock(&binaryMapLock); @@ -166,10 +167,10 @@ bool TABinaryManager::initTAatPath(const string &path, const string &uuid) { bool res = false; StructBinaryInfo info; - if (boost::filesystem::exists(path + uuid)) { + if (boost::filesystem::exists(TA_STORE_PATH + uuid)) { pthread_mutex_lock(&taLock); try { - if (unpackBinary(uuid, path, info)) { + if (unpackBinary(uuid, info)) { binaryMap[uuid] = info; res = true; } @@ -244,15 +245,14 @@ void TABinaryManager::decryptImage(StructBinaryInfo& info) { * It is very important to check for return value from this function. */ -bool TABinaryManager::unpackBinary(const string &uuid, const string &path, StructBinaryInfo& info) { +bool TABinaryManager::unpackBinary(const string &uuid, StructBinaryInfo& info) { TAUnpack* unpacker = TAUnpack::getInstance(); bool ret = false; - LOGD(SIM_DAEMON, "Unpacking TA %s in %s", uuid.c_str(), path.c_str()); - if (0 == unpacker->unpackTA(path, uuid)) { + if (0 == unpacker->unpackTA(string(TA_STORE_PATH), uuid)) { LOGD(SIM_DAEMON, "Unpacked, filling info"); // 1. Set binary info - info.path = path + uuid; - info.extractpath = path + uuid + "-ext/"; + info.path = string(TA_STORE_PATH)+ uuid; + info.extractpath = string(TA_STORE_PATH) + uuid + "-ext/"; info.imagePath = info.extractpath + uuid + ".image"; info.manifestPath = info.extractpath + uuid + ".manifest"; // 2. Parse manifest and store results diff --git a/simulatordaemon/src/TABinaryManager/TABinaryManager.h b/simulatordaemon/src/TABinaryManager/TABinaryManager.h index bf7366e..108fff0 100644 --- a/simulatordaemon/src/TABinaryManager/TABinaryManager.h +++ b/simulatordaemon/src/TABinaryManager/TABinaryManager.h @@ -64,7 +64,7 @@ private: // map < string uuid, StructBinaryInfo> map binaryMap; TABinaryManager(); - bool unpackBinary(const string &uuid, const string &path, StructBinaryInfo& info); + bool unpackBinary(const string &uuid, StructBinaryInfo& info); template std::string IntToHex(T i, int width = sizeof(T) * 2) { std::stringstream stream; @@ -85,7 +85,7 @@ public: */ pthread_mutex_t taLock; static TABinaryManager* getInstance(); - bool initTAatPath(const string &path, const string &uuid); + bool initTA (const string &uuid); /* * Query functions on Binary Manager diff --git a/simulatordaemon/src/TAFactory.cpp b/simulatordaemon/src/TAFactory.cpp index f55e11d..4c5bcbe 100644 --- a/simulatordaemon/src/TAFactory.cpp +++ b/simulatordaemon/src/TAFactory.cpp @@ -194,12 +194,7 @@ TAInstancePtr TAFactory::createUninitalizedTAInstance(string TAUUID, str << TAUUID << "-"; str << InstID; - string allowedTAPath; - if(!session->getSecurityContext().findRequestedTa(TAUUID, allowedTAPath)) { - LOGE(SIM_DAEMON, "Access for TA %s forbidden", TAUUID.c_str()); - return TAInstancePtr(); - } - if (launchTA(allowedTAPath, TAUUID, str, debug, pid)) { + if (launchTA(TAUUID, str, debug, pid)) { // TA is launched successfully, Create a new instance of TAInstance class /* Check if TA is to be keep alive and accordingly set TAInstance's @@ -347,22 +342,21 @@ void TAFactory::cleanupTAInstance(pid_t PID) { * @param debug debug flag * @param pid PID to be update for launched TA */ -bool TAFactory::launchTA(string path, string TAUUID, std::stringstream& str, bool debug, +bool TAFactory::launchTA(string TAUUID, std::stringstream& str, bool debug, pid_t& pid) { int32_t result = -1; pthread_t thread; LOGD(SIM_DAEMON, "Entry"); - LOGD(SIM_DAEMON, "Path: %s", path.c_str()); // Get TABinaryManager instance TABinaryManager *TABin = TABinaryManager::getInstance(); // Get TA Image path for launching string argvPath = ""; - if (TABin->initTAatPath(path, TAUUID)) { - argvPath = TABin->getImagePath(TAUUID); - LOGD(SIM_DAEMON, "argvPath: ", argvPath.c_str()); + if (TABin->initTA(TAUUID)) { + argvPath = TABin->getImagePath(TAUUID); } + if ("" == argvPath) { LOGE(SIM_DAEMON, "Trusted Application does not exist"); return false; -- 2.7.4 From a41ff8624122b876d3d775192c567d7885aa4ece Mon Sep 17 00:00:00 2001 From: akoszewski Date: Mon, 6 Nov 2017 15:25:13 +0100 Subject: [PATCH 12/16] Fix smack labels on tef-simulator files Change-Id: I7964f1b1545c9961e499755562c827eb6e9d3992 --- packaging/tef-simulator.spec | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packaging/tef-simulator.spec b/packaging/tef-simulator.spec index 99f1571..d318b5a 100644 --- a/packaging/tef-simulator.spec +++ b/packaging/tef-simulator.spec @@ -5,6 +5,7 @@ Release: 1 Group: Security License: Apache-2.0 Source0: %{name}-%{version}.tar.gz +Source1: %{name}.manifest ExcludeArch: armv6l armv7hl armv7l aarch64 BuildRequires: cmake BuildRequires: boost-devel @@ -68,6 +69,7 @@ with TEF Simulator. %prep %setup -q +cp %{SOURCE1} . %build # cannot call cmake rpmbuild macro because of scripts removing libTEEStub.a, which is a part of devkit @@ -106,6 +108,7 @@ if [ $1 = 0 ] ; then fi %files -n %{name} +%manifest tef-simulator.manifest %attr(111,security_fw,security_fw) %{bin_dir}/tef-simulator-daemon %{lib_dir}/libtef-simulator-ssflib.so %attr(770,root,security_fw) %{tastore_dir} @@ -113,7 +116,6 @@ fi %attr(444,security_fw,security_fw) %{_unitdir}/tef-simulator.socket %attr(755,security_fw,security_fw) %{lib_dir}/tef/simulator/libteec.so - %files -n %{name}-devkit %{bin_dir}/TA_PackageBuilder.sh %{bin_dir}/TAPackageMaker -- 2.7.4 From 5c88eac4359c8e3f77957ce5d8e8186c6be0b705 Mon Sep 17 00:00:00 2001 From: akoszewski Date: Fri, 27 Oct 2017 15:25:02 +0200 Subject: [PATCH 13/16] Fix TEEC operation preprocessing TEEC Operation arguments are now parsed properly. Change-Id: I91a811158b118066a2377a1d47eec36b9e8e03ac --- TEECLib/src/teec_api.c | 120 +++++++++++++++++++++++-------------------------- 1 file changed, 57 insertions(+), 63 deletions(-) diff --git a/TEECLib/src/teec_api.c b/TEECLib/src/teec_api.c index a3c0c71..d1f68c8 100644 --- a/TEECLib/src/teec_api.c +++ b/TEECLib/src/teec_api.c @@ -267,6 +267,42 @@ static uint32_t checkContext(TEEC_Context *context) return found; } +TEEC_Result tempSharedMemAllocate(TEEC_SharedMemory** tmpSharedMem, uint32_t type, + TEEC_Context* context, TEEC_Parameter param) +{ + TEEC_Result result; + *tmpSharedMem = (TEEC_SharedMemory *)OsaMalloc(sizeof(TEEC_SharedMemory)); + (*tmpSharedMem)->size = param.tmpref.size; + (*tmpSharedMem)->buffer = param.tmpref.buffer; + + (*tmpSharedMem)->flags = type & (TEEC_MEM_INPUT | TEEC_MEM_OUTPUT); + + result = TEEC_RegisterSharedMemory(context, *tmpSharedMem); + + if (result != TEEC_SUCCESS) { + if (*tmpSharedMem) { + OsaFree(*tmpSharedMem); + *tmpSharedMem = NULL; + } + return result; + } + + if (type & TEEC_MEMREF_TEMP_INPUT) { + memcpy(((TEEC_SharedMemoryImp *)(*tmpSharedMem)->imp)->allocPtr, + (*tmpSharedMem)->buffer, (*tmpSharedMem)->size); + } + return result; +} + +void tempSharedMemoryDeallocate(TEEC_SharedMemory** tmpSharedMem) +{ + if (*tmpSharedMem) { + TEEC_ReleaseSharedMemory(*tmpSharedMem); + OsaFree(*tmpSharedMem); + *tmpSharedMem = NULL; + } +} + /* * === FUNCTION ====================================================================== * Name: preProcessOperation @@ -339,38 +375,10 @@ static TEEC_Result preProcessOperation(TEEC_Session *session, op->paramTypes |= type << (8 * i); if (!tmpSharedMem[i]) { - tmpSharedMem[i] = (TEEC_SharedMemory *)OsaMalloc( - sizeof(TEEC_SharedMemory)); - tmpSharedMem[i]->size = operation->params[i].tmpref.size; - tmpSharedMem[i]->buffer = operation->params[i].tmpref.buffer; - - if (type == TEEC_MEMREF_TEMP_INPUT) - tmpSharedMem[i]->flags = TEEC_MEM_INPUT; - - else if (type == TEEC_MEMREF_TEMP_OUTPUT) - tmpSharedMem[i]->flags = TEEC_MEM_OUTPUT; - - else if (type == TEEC_MEMREF_TEMP_INOUT) - tmpSharedMem[i]->flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT; - - result = TEEC_RegisterSharedMemory( - ((TEEC_SessionImp *)session->imp)->context, tmpSharedMem[i]); - - if (result != TEEC_SUCCESS) { - for (i = 0; i < 4; i++) { - if (tmpSharedMem[i]) { - OsaFree(tmpSharedMem[i]); - tmpSharedMem[i] = NULL; - } - } - - return result; - } - - if (type & TEEC_MEMREF_TEMP_INPUT) { - memcpy(((TEEC_SharedMemoryImp *)tmpSharedMem[i]->imp)->allocPtr, - tmpSharedMem[i]->buffer, tmpSharedMem[i]->size); - } + result = tempSharedMemAllocate(&tmpSharedMem[i], type, + ((TEEC_SessionImp *)session->imp)->context, operation->params[i]); + if (result != TEEC_SUCCESS) + goto cleanup; } op->params[i].mem.size = tmpSharedMem[i]->size; @@ -380,25 +388,19 @@ static TEEC_Result preProcessOperation(TEEC_Session *session, break; case TEEC_MEMREF_WHOLE: - op->paramTypes |= TEE_PARAM_TYPE_MEMREF_INOUT << (8 * i); memref = &operation->params[i].memref; if ((NULL == memref) || (NULL == memref->parent) || (((TEEC_SharedMemoryImp *)memref->parent->imp)->context->imp != context->imp)) { - for (i = 0; i < 4; i++) { - if (tmpSharedMem[i]) { - TEEC_ReleaseSharedMemory(tmpSharedMem[i]); - OsaFree(tmpSharedMem[i]); - tmpSharedMem[i] = NULL; - } - } - LOGE(TEEC_LIB, "Bad parameters"); - return TEEC_ERROR_BAD_PARAMETERS; + result = TEEC_ERROR_BAD_PARAMETERS; + goto cleanup; } memref_imp = (TEEC_SharedMemoryImp *)memref->parent->imp; + op->paramTypes |= (operation->params[i].memref.parent->flags | + (TEE_PARAM_TYPE_MEMREF_INPUT & TEE_PARAM_TYPE_MEMREF_OUTPUT)) << (8 * i); op->params[i].mem.offset = 0; op->params[i].mem.size = memref->parent->size; op->params[i].mem.shmKey = memref_imp->shmKey; @@ -411,23 +413,16 @@ static TEEC_Result preProcessOperation(TEEC_Session *session, case TEEC_MEMREF_PARTIAL_INPUT: case TEEC_MEMREF_PARTIAL_OUTPUT: case TEEC_MEMREF_PARTIAL_INOUT: - op->paramTypes |= (type + TEE_PARAM_TYPE_MEMREF_INPUT - - TEEC_MEMREF_PARTIAL_INPUT) << (8 * i); + op->paramTypes |= ((operation->params[i].memref.parent->flags & type) | + (TEE_PARAM_TYPE_MEMREF_INPUT & TEE_PARAM_TYPE_MEMREF_OUTPUT)) << (8 * i); memref = &operation->params[i].memref; if ((NULL == memref) || (NULL == memref->parent) || (((TEEC_SharedMemoryImp *)memref->parent->imp)->context->imp != context->imp)) { - for (i = 0; i < 4; i++) { - if (tmpSharedMem[i]) { - TEEC_ReleaseSharedMemory(tmpSharedMem[i]); - OsaFree(tmpSharedMem[i]); - tmpSharedMem[i] = NULL; - } - } - LOGE(TEEC_LIB, "Bad parameters"); - return TEEC_ERROR_BAD_PARAMETERS; + result = TEEC_ERROR_BAD_PARAMETERS; + goto cleanup; } memref_imp = (TEEC_SharedMemoryImp *)memref->parent->imp; @@ -446,19 +441,18 @@ static TEEC_Result preProcessOperation(TEEC_Session *session, break; default: - for (i = 0; i < 4; i++) { - if (tmpSharedMem[i]) { - TEEC_ReleaseSharedMemory(tmpSharedMem[i]); - OsaFree(tmpSharedMem[i]); - tmpSharedMem[i] = NULL; - } - } - - return TEEC_ERROR_BAD_PARAMETERS; + result = TEEC_ERROR_BAD_PARAMETERS; + goto cleanup; } } - return TEEC_SUCCESS; + return result; + +cleanup: + for (i = 0; i < 4; i++) { + tempSharedMemoryDeallocate(&tmpSharedMem[i]); + } + return result; } /* -- 2.7.4 From 00a43a81817133b64e314cfafd297f5d7e9ac861 Mon Sep 17 00:00:00 2001 From: akoszewski Date: Fri, 10 Nov 2017 12:43:23 +0100 Subject: [PATCH 14/16] Add stub PrintLog function Add stub PrintLog function in release tef-simulator to fix error with loading debug TA by release tef simulator Change-Id: Ia673fbb615baaff9834c339ce965baad5317a444 --- log/log.c | 15 +++++++++++++++ log/log.h | 13 ------------- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/log/log.c b/log/log.c index cfc55c6..afe0f81 100644 --- a/log/log.c +++ b/log/log.c @@ -150,6 +150,9 @@ const char *GetModuleLevel(IN int32_t module_level) * * @return void */ + +#ifdef _LOGGING + __attribute__((visibility("default"))) void PrintLog(IN const char *function_name, IN const int32_t line_no, IN int32_t module_level, IN int32_t debug_level, IN const char *message, @@ -236,3 +239,15 @@ void PrintLog(IN const char *function_name, IN const int32_t line_no, va_end(variable_list); return; } + +#else // ifdef _LOGGING + +__attribute__((visibility("default"))) +void PrintLog(IN const char *function_name, IN const int32_t line_no, + IN int32_t module_level, IN int32_t debug_level, IN const char *message, + ...) +{ + // stub function +} + +#endif // ifdef _LOGGING diff --git a/log/log.h b/log/log.h index 11b2fd4..8c31f2d 100644 --- a/log/log.h +++ b/log/log.h @@ -102,8 +102,6 @@ typedef enum { #endif #endif // __TIZEN__ -#ifdef _LOGGING - #define _LOG(module_level, debug_level, ...) PrintLog(__FUNCTION__, __LINE__, module_level, debug_level, ##__VA_ARGS__) #define LOGE(module_level, ...) PrintLog(__FUNCTION__, __LINE__, module_level, ERROR_LEVEL_LOG, ##__VA_ARGS__) @@ -113,17 +111,6 @@ typedef enum { #define LOGS(module_level, ...) PrintLog(__FUNCTION__, __LINE__, module_level, INFO_LEVEL_LOG, ##__VA_ARGS__) #define LOGP(module_level, ...) PrintLog(__FUNCTION__, __LINE__, module_level, PACKET_LEVEL_LOG, ##__VA_ARGS__) -#else //ifdef _LOGGING - -#define LOGE(module_level, ...) -#define LOGV(module_level, ...) -#define LOGD(module_level, ...) -#define LOGI(module_level, ...) -#define LOGS(module_level, ...) -#define LOGP(module_level, ...) - -#endif //ifdef _LOGGING - #if defined(__cplusplus) extern "C" { #endif -- 2.7.4 From 36e40dddbda41db1f8cad529664dbae3d1bc664f Mon Sep 17 00:00:00 2001 From: Krzysztof Dynowski Date: Tue, 14 Nov 2017 15:27:02 +0100 Subject: [PATCH 15/16] Send (correct) response to client when cynara danied access Change-Id: I53b11b0149725dc88679febb6706af42b8d37cb3 --- simulatordaemon/inc/TEEContext.h | 1 + simulatordaemon/src/ConnectionSession.cpp | 17 +++++++++-------- simulatordaemon/src/TEEContext.cpp | 14 ++++++++++++++ 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/simulatordaemon/inc/TEEContext.h b/simulatordaemon/inc/TEEContext.h index 4db8506..ab68665 100644 --- a/simulatordaemon/inc/TEEContext.h +++ b/simulatordaemon/inc/TEEContext.h @@ -61,6 +61,7 @@ public: uint32_t mContextID; /* Security context wich stores info about low-level connection data*/ SecurityContext secContext; + bool cynara_check_result; /* For TA internal APIs support, dummy Context is created and for recognizing * the context as dummy isInternal member variable is used diff --git a/simulatordaemon/src/ConnectionSession.cpp b/simulatordaemon/src/ConnectionSession.cpp index 31fb181..8fadf2f 100644 --- a/simulatordaemon/src/ConnectionSession.cpp +++ b/simulatordaemon/src/ConnectionSession.cpp @@ -48,22 +48,23 @@ void ConnectionSession::start() { // init SecurityContext of current session after initializing socket this->secContext = SecurityContext(clientSocket.native()); + // Create a new Context + pthread_rwlock_wrlock(&ctxIDLock); + TEECtx = new TEEContext(ctxID, this); + // Increment the Context ID to be assigned to next Context + ctxID++; + if (ctxID == 0) ctxID++; + pthread_rwlock_unlock(&ctxIDLock); + #ifdef _CYNARA_INTEGRATION /* Check if client has cynara permission */ const string privilege("http://tizen.org/privilege/tee.client"); if (!secContext.clientHasCynaraPermission(privilege)) { LOGE(SIM_DAEMON, "Client has no permission to use TEE"); - return; + TEECtx->cynara_check_result = false; } #endif /* _CYNARA_INTEGRATION */ - // Create a new Context - pthread_rwlock_wrlock(&ctxIDLock); - TEECtx = new TEEContext(ctxID, this); - // Increment the Context ID to be assigned to next Context - ctxID++; - if (ctxID == 0) ctxID++; - pthread_rwlock_unlock(&ctxIDLock); currentState = CMD_READ; // read exactly 1 byte to identify the command and execute callback when diff --git a/simulatordaemon/src/TEEContext.cpp b/simulatordaemon/src/TEEContext.cpp index f85729c..189f918 100644 --- a/simulatordaemon/src/TEEContext.cpp +++ b/simulatordaemon/src/TEEContext.cpp @@ -57,6 +57,8 @@ TEEContext::TEEContext(uint32_t contextID, IConnectionSession* connSession): /* Clear the shared memory list (mShmList) and Session map (mSessionMap) */ mSessionMap.clear(); mShmList.clear(); + + cynara_check_result = true; } /** @@ -75,6 +77,17 @@ TEEC_Result TEEContext::initContext(InitContextData* data) { */ isInternal = false; + if (!cynara_check_result) { + result = TEEC_ERROR_ACCESS_DENIED; + data->returnValue = result; + result = mConnSess->write(INITIALIZE_CONTEXT, (char*)data, + sizeof(InitContextData)); + if (result != TEEC_SUCCESS) { + LOGE(SIM_DAEMON, "Initialize Context response write to CA FAILED"); + } + return result; + } + /* Check if the TEEName is proper or not */ if (data->nameLength != 0) { string TName(data->TEEName); @@ -167,6 +180,7 @@ TEEC_Result TEEContext::openSession(OpenSessionData data) { LOGD(SIM_DAEMON, "Entry"); data.returnOrigin = TEEC_ORIGIN_TEE; data.returnValue = TEEC_ERROR_GENERIC; + pthread_rwlock_wrlock(&sessIDLock); data.sessionID = sessID; sessID++; -- 2.7.4 From 4bf6c192dc128b4407372f96450b7fa181a60f81 Mon Sep 17 00:00:00 2001 From: Uladzislau Harbuz Date: Fri, 10 Nov 2017 18:23:01 +0100 Subject: [PATCH 16/16] Fix C++Test static analysis violations Change-Id: Ia458bb472af6f3cd18cc9dd1ccaacb9bc7558805 --- TEEStub/PropertyAccess/Property.h | 6 +- TEEStub/PropertyAccess/PropertyApi.cpp | 2 +- TEEStub/PropertyAccess/PropertyUtility.cpp | 1 - TEEStub/TACommands/CommandCloseSession.cpp | 6 +- TEEStub/TACommands/CommandCloseSession.h | 2 +- TEEStub/TACommands/CommandCreateEntryPoint.cpp | 12 +- TEEStub/TACommands/CommandDestroyEntryPoint.cpp | 12 +- include/include/tee_command.h | 1 - log/log.c | 3 + log/log.h | 2 +- osal/CMakeLists.txt | 4 + osal/OsaCommon.c | 44 ++----- osal/OsaIpc.c | 41 ++++--- osal/OsaQueue.c | 58 +++------ osal/OsaSem.c | 65 ++++------- osal/OsaSignal.c | 9 +- osal/OsaTask.c | 27 ++--- simulatordaemon/inc/SecurityContext.h | 2 +- simulatordaemon/src/ClientCommands/MakeCommand.cpp | 4 - simulatordaemon/src/SecurityContext.cpp | 8 +- simulatordaemon/src/TABinaryManager/TAManifest.cpp | 5 - simulatordaemon/src/TABinaryManager/TAUnpack.cpp | 2 - ssflib/dep/cryptocore/source/base/cc_bignum.c | 129 +-------------------- ssflib/dep/cryptocore/source/base/cc_ecc.c | 2 - ssflib/dep/cryptocore/source/base/cc_md5.c | 7 -- ssflib/dep/cryptocore/source/base/cc_pkcs1_v21.c | 1 - ssflib/dep/cryptocore/source/base/cc_rc4.c | 2 - ssflib/dep/cryptocore/source/base/cc_sha1.c | 81 +------------ ssflib/dep/cryptocore/source/middle/cc_cmac.c | 5 +- ssflib/dep/cryptocore/source/middle/cc_dsa.c | 3 +- ssflib/dep/cryptocore/source/middle/cc_ecdsa.c | 20 +--- ssflib/dep/cryptocore/source/middle/cc_rsa.c | 21 +--- ssflib/dep/swdss/source/file_op.cpp | 3 - ssflib/dep/swdss/source/secure_file.cpp | 43 ++----- ssflib/dep/swdss/source/ss_crypto.cpp | 2 - ssflib/dep/uci/source/uci_api.c | 1 - ssflib/dep/uci/source/uci_cryptocore.c | 2 - ssflib/dep/uci/source/uci_hwcrypto.c | 31 ++--- ssflib/inc/app_debug.h | 1 - ssflib/src/app_debug.cpp | 1 - ssflib/src/ssf_client.cpp | 1 - ssflib/src/ssf_crypto.cpp | 43 ++----- ssflib/src/ssf_storage.cpp | 21 ---- 43 files changed, 172 insertions(+), 564 deletions(-) diff --git a/TEEStub/PropertyAccess/Property.h b/TEEStub/PropertyAccess/Property.h index 80ecb4c..23c81bc 100644 --- a/TEEStub/PropertyAccess/Property.h +++ b/TEEStub/PropertyAccess/Property.h @@ -52,15 +52,15 @@ public: virtual void reset() = 0; virtual bool getPropertyValue(PropertyValue&) = 0; void setPropSet(uintptr_t propset) { - this->propset = propset; + this->m_propset = propset; }; - Property() : propset(0) { + Property() : m_propset(0) { } ; virtual ~Property() { } ; - uintptr_t propset; + uintptr_t m_propset; }; #endif /* PROPERTYACCESS_PROPERTY_H_ */ diff --git a/TEEStub/PropertyAccess/PropertyApi.cpp b/TEEStub/PropertyAccess/PropertyApi.cpp index 2a52fdf..50e3a99 100644 --- a/TEEStub/PropertyAccess/PropertyApi.cpp +++ b/TEEStub/PropertyAccess/PropertyApi.cpp @@ -413,7 +413,7 @@ uintptr_t _GetTargetPropsetType(TEE_PropSetHandle propsetOrEnumerator) { (PropertyEnumHandle*)propsetOrEnumerator; if (enumHandle && enumHandle->property) { targetProperty = enumHandle->property; - return targetProperty->propset; + return targetProperty->m_propset; } } return 0; diff --git a/TEEStub/PropertyAccess/PropertyUtility.cpp b/TEEStub/PropertyAccess/PropertyUtility.cpp index 930f5d3..d7dfec9 100644 --- a/TEEStub/PropertyAccess/PropertyUtility.cpp +++ b/TEEStub/PropertyAccess/PropertyUtility.cpp @@ -127,7 +127,6 @@ TEE_Result PropertyUtility::convertToUUID(const PropertyValue& in, uint64_t clockSeq; string clockSeqStr = tokensString[4] + tokensString[5] + tokensString[6] + tokensString[7]; - //TEST CODE: string clockSeqStr("0123456789ABCDEF"); clockSeq = std::stoll(clockSeqStr); memcpy(uuid.clockSeqAndNode, &clockSeq, sizeof(uint64_t)); // Change endian-ness diff --git a/TEEStub/TACommands/CommandCloseSession.cpp b/TEEStub/TACommands/CommandCloseSession.cpp index 74433bc..17db1d3 100644 --- a/TEEStub/TACommands/CommandCloseSession.cpp +++ b/TEEStub/TACommands/CommandCloseSession.cpp @@ -37,7 +37,7 @@ */ CommandCloseSession::CommandCloseSession(CloseTASessionData data) : CommandBase(CLOSESESSION) { - this->data = data; + this->m_data = data; sessionID = data.sessionID; } @@ -61,7 +61,7 @@ TEE_Result CommandCloseSession::execute() { */ std::string CommandCloseSession::getCommandUID() const { std::stringstream ss; - ss << data.sessionID; + ss << m_data.sessionID; ss << ":"; ss << "0"; return ss.str(); @@ -81,6 +81,6 @@ CommandCloseSession::~CommandCloseSession() { void CommandCloseSession::getSerializedData(unsigned char *data, unsigned int &size) { data[0] = command; - memcpy(&data[1], (unsigned char*)&this->data, sizeof(CloseTASessionData)); + memcpy(&data[1], (unsigned char*)&this->m_data, sizeof(CloseTASessionData)); size = sizeof(CloseTASessionData); } diff --git a/TEEStub/TACommands/CommandCloseSession.h b/TEEStub/TACommands/CommandCloseSession.h index 67b38d3..fb37242 100644 --- a/TEEStub/TACommands/CommandCloseSession.h +++ b/TEEStub/TACommands/CommandCloseSession.h @@ -39,7 +39,7 @@ class CommandCloseSession: public CommandBase { public: - CloseTASessionData data; + CloseTASessionData m_data; void getSerializedData(unsigned char *data, unsigned int &size); CommandCloseSession(CloseTASessionData data); TEE_Result execute(); diff --git a/TEEStub/TACommands/CommandCreateEntryPoint.cpp b/TEEStub/TACommands/CommandCreateEntryPoint.cpp index 15c0df9..bb32028 100644 --- a/TEEStub/TACommands/CommandCreateEntryPoint.cpp +++ b/TEEStub/TACommands/CommandCreateEntryPoint.cpp @@ -31,10 +31,10 @@ /*----------------------------------------------------------------------------- * Member functions *-----------------------------------------------------------------------------*/ -CommandCreateEntryPoint::CommandCreateEntryPoint(CreateTAEntryPointData data) : +CommandCreateEntryPoint::CommandCreateEntryPoint(CreateTAEntryPointData _data) : CommandBase(CREATE) { - this->data = data; - sessionID = data.sessionID; + this->data = _data; + sessionID = _data.sessionID; } /** @@ -69,9 +69,9 @@ CommandCreateEntryPoint::~CommandCreateEntryPoint() { * @param data[out] serialized object data * @param size[out] size in bytes */ -void CommandCreateEntryPoint::getSerializedData(unsigned char *data, +void CommandCreateEntryPoint::getSerializedData(unsigned char *_data, unsigned int &size) { - data[0] = command; - memcpy(&data[1], (unsigned char*)&this->data, sizeof(CreateTAEntryPointData)); + _data[0] = command; + memcpy(&_data[1], (unsigned char*)&this->data, sizeof(CreateTAEntryPointData)); size = sizeof(CreateTAEntryPointData); } diff --git a/TEEStub/TACommands/CommandDestroyEntryPoint.cpp b/TEEStub/TACommands/CommandDestroyEntryPoint.cpp index 84af8c9..616b12f 100644 --- a/TEEStub/TACommands/CommandDestroyEntryPoint.cpp +++ b/TEEStub/TACommands/CommandDestroyEntryPoint.cpp @@ -31,10 +31,10 @@ /*----------------------------------------------------------------------------- * Member functions *-----------------------------------------------------------------------------*/ -CommandDestroyEntryPoint::CommandDestroyEntryPoint(DestroyTAEntryPointData data) : +CommandDestroyEntryPoint::CommandDestroyEntryPoint(DestroyTAEntryPointData _data) : CommandBase(DESTROY) { - this->data = data; - sessionID = data.sessionID; + this->data = _data; + sessionID = _data.sessionID; } //TODO: Handle exit of TA instance in a clean way @@ -71,10 +71,10 @@ CommandDestroyEntryPoint::~CommandDestroyEntryPoint() { * @param data[out] serialized object data * @param size[out] size in bytes */ -void CommandDestroyEntryPoint::getSerializedData(unsigned char *data, +void CommandDestroyEntryPoint::getSerializedData(unsigned char *_data, unsigned int &size) { - data[0] = command; - memcpy(&data[1], (unsigned char*)&this->data, + _data[0] = command; + memcpy(&_data[1], (unsigned char*)&this->data, sizeof(DestroyTAEntryPointData)); size = sizeof(DestroyTAEntryPointData); } diff --git a/include/include/tee_command.h b/include/include/tee_command.h index c3f2c4a..f2197cd 100644 --- a/include/include/tee_command.h +++ b/include/include/tee_command.h @@ -36,7 +36,6 @@ typedef enum { OPEN_TA_SESSION, INVOKE_TA_COMMAND, CLOSE_TA_SESSION, - CHECK_MEMORY, PANIC } TEE_CMD; #endif /* __TEE_COMMAND_H__ */ diff --git a/log/log.c b/log/log.c index afe0f81..4b1e3d6 100644 --- a/log/log.c +++ b/log/log.c @@ -125,6 +125,9 @@ const char *GetModuleLevel(IN int32_t module_level) case SSF_LIB: return "SSF_LIB"; + case OSA_LIB: + return "OSA_LIB"; + default: return "TA_SDK"; } diff --git a/log/log.h b/log/log.h index 8c31f2d..2658c0c 100644 --- a/log/log.h +++ b/log/log.h @@ -36,7 +36,6 @@ #define INOUT #define OUT -//#define _LOGGING #ifdef _WIN typedef int timer_t; @@ -77,6 +76,7 @@ typedef enum { TEE_STUB = 0x08, TEST = 0x10, SSF_LIB = 0x11, + OSA_LIB = 0x12, ALL_MODULES = 0xFFFFFFF, } ModuleLevel; diff --git a/osal/CMakeLists.txt b/osal/CMakeLists.txt index 755cd04..82f6c8d 100644 --- a/osal/CMakeLists.txt +++ b/osal/CMakeLists.txt @@ -26,6 +26,10 @@ SET(OSAL_SOURCES ${OSAL_PATH}/OsaTask.c ) +INCLUDE_DIRECTORIES( + ${LOG_PATH} +) + ADD_LIBRARY(${TARGET_TEF_SIMULATOR_OSAL} ${OSAL_SOURCES}) INSTALL(TARGETS ${TARGET_TEF_SIMULATOR_OSAL} DESTINATION ${LIB_DIR}) diff --git a/osal/OsaCommon.c b/osal/OsaCommon.c index 3caabef..a2c652b 100644 --- a/osal/OsaCommon.c +++ b/osal/OsaCommon.c @@ -24,6 +24,7 @@ * Include files *-----------------------------------------------------------------------------*/ #include "OsaLinuxUser.h" +#include /*----------------------------------------------------------------------------- * Globals @@ -148,7 +149,7 @@ int OsaTimerCreate(int *pTimerId, int periodic, int s32Time, * Add SIGALRM in the list */ if (sigaddset(&(Action_t.sa_mask), SIGALRM) < 0) { - //PrintError("In OsaTimerCreate() : Could Not Stop \n"); + LOGE(OSA_LIB, "In OsaTimerCreate() : Could Not Stop \n"); return OSAL_ERROR; } @@ -156,7 +157,7 @@ int OsaTimerCreate(int *pTimerId, int periodic, int s32Time, * Unblock the SIGALRM,if it is blocked */ if (sigprocmask(SIG_UNBLOCK, &(Action_t.sa_mask), NULL) < 0) { - //PrintError("In OsaTimerCreate() : Could not mask the Signal \n"); + LOGE(OSA_LIB, "In OsaTimerCreate() : Could not mask the Signal \n"); return OSAL_ERROR; } @@ -197,7 +198,7 @@ int OsaTimerStart(int iTimerId) /* The timer is started */ if (Timer_data_t.start_timer != TRUE) { if (setitimer(ITIMER_REAL, &Timer_data_t.iTval_t, NULL) < 0) { - //PrintError("In OsaTimerStart() : OsaTimerStart failed \n "); + LOGE(OSA_LIB, "In OsaTimerStart() : OsaTimerStart failed \n "); return OSAL_ERROR; } @@ -221,44 +222,19 @@ int OsaTimerStart(int iTimerId) */ int OsaTimerStop(int iTimerId) { - //struct sigaction Action_t; - //Action_t.sa_flags = 0; struct itimerval trivial_it; /* OSAL_080918_1 */ if (Timer_data_t.start_timer == FALSE) { - //PrintError("In OsaTimerStop() : Timer not yet started \n"); - return OSAL_ERROR; - } - - /* OSAL_080920 : Don't block the alarm signal */ -#if 0 - /* OSAL_080918_2 : init signal set variable */ - sigemptyset(&(Action_t.sa_mask)); - - /* - * Add SIGALRM in the signal List - */ - if (sigaddset(&(Action_t.sa_mask), SIGALRM) < 0) { - PrintError("In OsaTimerStop() : Could Not Stop \n"); - return OSAL_ERROR; - } - - /* - * Block SIGALRM - */ - if (sigprocmask(SIG_BLOCK, &(Action_t.sa_mask), NULL) < 0) { - PrintError("In OsaTimerStop() : Could not mask the Signal \n"); return OSAL_ERROR; } -#endif /* OSAL_080918_1 : stop interval timer after alarm signal blocked */ trivial_it.it_value.tv_sec = 0; trivial_it.it_value.tv_usec = 0; if (setitimer(ITIMER_REAL, &trivial_it, NULL) == -1) { - //PrintError("OsaTimerStop failed\n "); + LOGE(OSA_LIB, "OsaTimerStop failed\n "); return OSAL_ERROR; } @@ -285,7 +261,7 @@ int OsaTimerDelete(int iTimerId) //Action_t.sa_flags = 0; if (Timer_data_t.start_timer == FALSE) { - //PrintError("In OsaTimerDelete() : No timer present to be deleted \n"); + LOGE(OSA_LIB, "In OsaTimerDelete() : No timer present to be deleted \n"); return OSAL_ERROR; } @@ -297,7 +273,7 @@ int OsaTimerDelete(int iTimerId) /* The Timer is deleted */ if (setitimer(ITIMER_REAL, &iTmpval_t, NULL) < 0) { - //PrintError("In OsaTimerDelete() : pOsaTimerDelete failed \n"); + LOGE(OSA_LIB, "In OsaTimerDelete() : pOsaTimerDelete failed \n"); return -1; } @@ -323,14 +299,14 @@ int OsaTimerRestart(int iTimerId) struct sigaction Action_t; if (Timer_data_t.stop_timer == TRUE) { - //PrintError("In OsaTimerRestart() : Has been stopped forever \n"); + LOGE(OSA_LIB, "In OsaTimerRestart() : Has been stopped forever \n"); return OSAL_ERROR; } /* OSAL_080918_1 : reset it_value to keep the first expiration after restart */ if (setitimer(ITIMER_REAL, &Timer_data_t.iTval_t, NULL) == -1) { - //PrintError("OsaTimerRestart failed\n "); + LOGE(OSA_LIB, "OsaTimerRestart failed\n "); return OSAL_ERROR; } @@ -341,7 +317,7 @@ int OsaTimerRestart(int iTimerId) Action_t.sa_flags = 0; if (sigprocmask(SIG_UNBLOCK, &(Action_t.sa_mask), NULL) < 0) { - //PrintError("In OsaTimerRestart() : Could Not Start Again \n"); + LOGE(OSA_LIB, "In OsaTimerRestart() : Could Not Start Again \n"); return OSAL_ERROR; } diff --git a/osal/OsaIpc.c b/osal/OsaIpc.c index 07d2f89..89b5190 100644 --- a/osal/OsaIpc.c +++ b/osal/OsaIpc.c @@ -25,6 +25,7 @@ * Include files *-----------------------------------------------------------------------------*/ #include "OsaLinuxUser.h" +#include key_t OsaGetKey(const char pcName[10]) { @@ -36,7 +37,7 @@ key_t OsaGetKey(const char pcName[10]) memset(aName, 0x00, 10); memcpy(aName, pcName, 9); - uid = 1; //getuid(); + uid = 1; // getuid was here acc = 0; len = strlen(aName); @@ -103,7 +104,7 @@ int OsaShmDetach(const void *pShmAddr) return OSAL_ERROR; if (shmdt(pShmAddr) == -1) { - //PrintError("Error in Detaching"); + LOGE(OSA_LIB, "Error in Detaching"); return OSAL_ERROR; } @@ -147,7 +148,7 @@ static int UlOsaNamedSemCreate(const char pcName[10], int iCount, sem = (UlOsaSem_t *)malloc(sizeof(*sem)); if (!sem) { - //PrintError ("UlOsaSemCreate, Out of memory!\n"); + LOGE(OSA_LIB, "UlOsaSemCreate, Out of memory!\n"); return OSAL_ERROR; } @@ -163,7 +164,7 @@ static int UlOsaNamedSemCreate(const char pcName[10], int iCount, if (semctl(sem->iSemId, 0, SETVAL, semUnion) == -1) { semctl(sem->iSemId, 0, IPC_RMID, NULL); free(sem); - //PrintError ("UlOsaSemCreate, semctl Failed!\n"); + LOGE(OSA_LIB, "UlOsaSemCreate, semctl Failed!\n"); return OSAL_ERROR; } } else { @@ -172,7 +173,7 @@ static int UlOsaNamedSemCreate(const char pcName[10], int iCount, iRetVal = OSAL_EXIST; } else { free(sem); - //PrintError ("UlOsaSemCreate, semget Failed!\n"); + LOGE(OSA_LIB, "UlOsaSemCreate, semget Failed!\n"); return OSAL_ERROR; } } @@ -226,7 +227,7 @@ static int UlOsaNamedSemGet(void *uiSmid, int iFlags, int iTimeout) ret = semop(sem->iSemId, &semBuf, 1); } else if (iTimeout == 0) { /* wait _inifinite_ */ - //PrintDbg ("UlOsaSemGet-infinite(%s).\n", sem->bName); + LOGD(OSA_LIB, "UlOsaSemGet-infinite(%s).\n", sem->bName); semBuf.sem_num = 0; semBuf.sem_op = -1; semBuf.sem_flg = SEM_UNDO; @@ -234,9 +235,9 @@ static int UlOsaNamedSemGet(void *uiSmid, int iFlags, int iTimeout) ret = OSAL_FAILURE_RETRY(semop(sem->iSemId, &semBuf, 1)); } else { /* with _timeout_ */ - //PrintDbg ("UlOsaSemGet-timeout(%s).\n", sem->bName); + LOGD(OSA_LIB, "UlOsaSemGet-timeout(%s).\n", sem->bName); if (iTimeout < 0) { - //PrintError ("UlOsaSemGet-timeout: invalid arg!\n"); + LOGE(OSA_LIB, "UlOsaSemGet-timeout: invalid arg!\n"); return OSAL_ERROR; } @@ -257,17 +258,17 @@ static int UlOsaNamedSemGet(void *uiSmid, int iFlags, int iTimeout) /* result */ if (ret == 0) { - //PrintDbg ("UlOsaSemGet(%s) success.\n", sem->bName); + LOGD(OSA_LIB, "UlOsaSemGet(%s) success.\n", sem->bName); return OSAL_OK; } else { if (iFlags == OSAL_SEM_NOWAIT && errno == EAGAIN) { - //PrintError ("UlOsaSemGet-nowait: now locked, failed to get.\n"); + LOGE(OSA_LIB, "UlOsaSemGet-nowait: now locked, failed to get.\n"); return OSAL_ERROR; } else if (iTimeout > 0 && errno == EAGAIN) { - //PrintError ("UlOsaSemGet-timeout(%s): time-out\n", sem->bName); + LOGE(OSA_LIB, "UlOsaSemGet-timeout(%s): time-out\n", sem->bName); return OSAL_ERR_TIMEOUT; } else { - //PrintError ("UlOsaSemGet error, errno=%d\n", errno); + LOGE(OSA_LIB, "UlOsaSemGet error, errno=%d\n", errno); return OSAL_ERROR; } } @@ -281,13 +282,13 @@ static int UlOsaNamedSemRelease(void *uiSmid) if (!sem) return OSAL_ERROR; - //PrintDbg ("UlOsaSemRelease(%s)\n", sem->bName); + LOGD(OSA_LIB, "UlOsaSemRelease(%s)\n", sem->bName); semBuf.sem_num = 0; semBuf.sem_op = 1; semBuf.sem_flg = SEM_UNDO; if (semop(sem->iSemId, &semBuf, 1) == -1) { - //PrintError ("UlOsaSemRelease(%s) error! errno=%d.\n", sem->bName, errno); + LOGE(OSA_LIB, "UlOsaSemRelease(%s) error! errno=%d.\n", sem->bName, errno); return OSAL_ERROR; } else return OSAL_OK; @@ -301,11 +302,11 @@ static int UlOsaNamedSemReset(void *uiSmid) if (!sem) return OSAL_ERROR; - //PrintDbg ("UlOsaSemReset(%s).\n", sem->bName); + LOGD(OSA_LIB, "UlOsaSemReset(%s).\n", sem->bName); semUnion.val = sem->iCount; if (semctl(sem->iSemId, 0, SETVAL, semUnion) == -1) { - //PrintError ("UlOsaSemReset, semctl Failed!\n"); + LOGE(OSA_LIB, "UlOsaSemReset, semctl Failed!\n"); return OSAL_ERROR; } @@ -323,10 +324,10 @@ static int UlOsaNamedSemGetval(void *uiSmid) n = semctl(sem->iSemId, 0, GETVAL, NULL); if (n == -1) { - //PrintError ("UlOsaSemGetval, semctl Failed!\n"); + LOGE(OSA_LIB, "UlOsaSemGetval, semctl Failed!\n"); return OSAL_ERROR; } else { - //PrintDbg ("UlOsaSemGetval(%s): now %d\n", sem->bName, n); + LOGD(OSA_LIB, "UlOsaSemGetval(%s): now %d\n", sem->bName, n); return (int)n; } } @@ -347,10 +348,8 @@ int OsaNamedSemRelease(void *uiSmid) return UlOsaNamedSemRelease(uiSmid); } -int OsaNamedSemDelete(void - *uiSmid) // Deleting Semaphore : Never USE!!! - junhyeong.kim, sukki.min 12.04.20 +int OsaNamedSemDelete(void *uiSmid) { - //return UlOsaNamedSemDelete (uiSmid); return -1; } diff --git a/osal/OsaQueue.c b/osal/OsaQueue.c index ac5567b..75d39c0 100644 --- a/osal/OsaQueue.c +++ b/osal/OsaQueue.c @@ -24,6 +24,7 @@ * Include files *-----------------------------------------------------------------------------*/ #include "OsaLinuxUser.h" +#include /*----------------------------------------------------------------------------- * Globals @@ -59,7 +60,7 @@ int OsaQueueCreate(const char bName[10], unsigned int uiFlags, mqd_t QuId; if (puiQid == NULL) { - PrintDbg("Null Argument(s) \n"); + LOGD(OSA_LIB, "Null Argument(s) \n"); return OSAL_ERROR; } @@ -88,14 +89,14 @@ int OsaQueueCreate(const char bName[10], unsigned int uiFlags, if (((int)*puiQid) == -1) { //IPC_CREATE perror("In OsaQueueCreate() : msgget: msgget failed"); - //PrintError("In OsaQueueCreate() : Error no. : %d\n",errno); + LOGE(OSA_LIB, "In OsaQueueCreate() : Error no. : %d\n", errno); return ((int)errno); } /* Get the current value from the structure for the message queue and copy it in buf_t*/ if (msgctl((int)(*puiQid), IPC_STAT, &tSetMqAttr) == -1) { perror("In OsaQueueCreate() : msgctl: msgctl failed"); - //PrintError("In OsaQueueCreate() : Error no. : %d\n",errno); + LOGE(OSA_LIB, "In OsaQueueCreate() : Error no. : %d\n", errno); return ((int)errno); } @@ -104,7 +105,7 @@ int OsaQueueCreate(const char bName[10], unsigned int uiFlags, if (msgctl((int)(*puiQid), IPC_SET, &tSetMqAttr) == -1) { perror("In OsaQueueCreate() : msgctl: msgctl failed"); - //PrintError("In OsaQueueCreate() : Error no. : %d\n",errno); + LOGE(OSA_LIB, "In OsaQueueCreate() : Error no. : %d\n", errno); return ((int)errno); } @@ -168,7 +169,7 @@ int OsaQueueDelete(const char *bName, unsigned int uiQid) if (msgctl((int)uiQid, IPC_RMID, NULL) == -1) { perror("In OsaQueueDelete(): msgctl: msgctl failed"); - //PrintError("In OsaQueueDelete(): Error no. : %d\n",errno); + LOGE(OSA_LIB, "In OsaQueueDelete(): Error no. : %d\n", errno); return ((int)errno); } @@ -204,27 +205,22 @@ int OsaQueueSend(unsigned int uiQid, unsigned int uiFlags, void *pvMsg_buf, tMqAttr.mq_flags = O_NONBLOCK; if ((err_no = mq_setattr((mqd_t)uiQid, &tMqAttr, (struct mq_attr *)NULL)) < 0) { - ////PrintError("mq_setattr(): mq_setattr() Failed errno=%d\n",err_no,0,0,0,0,0); //COMMON_071024_1 + LOGE(OSA_LIB, "mq_setattr(): mq_setattr() Failed errno=%d\n", err_no, 0, 0, 0, 0, 0); return (OSAL_ERROR); } } - if ((err_no = mq_send((mqd_t)uiQid, (const char *)pvMsg_buf, uiMsgLen, - uiPriority)) < 0) { - ////PrintError("mq_send():Failed errno=%d qid=%x flag=%d\n",err_no,uiQid,uiFlags,0,0,0); //COMMON_071024_1 + if ((err_no = mq_send((mqd_t)uiQid, (const char *)pvMsg_buf, uiMsgLen, uiPriority)) < 0) { + LOGE(OSA_LIB, "mq_send():Failed errno=%d qid=%x flag=%d\n", err_no, uiQid, uiFlags, 0, 0, 0); return (OSAL_ERROR); } #else /*SYS5 MSG QUEUE*/ - /* - uiFlags : IPC_NOWAIT , ZERO - pMsgLen: len - sizeof(int); - */ osaMsgBuf_t osaMsg; int ret; if (uiMsgLen > MAXML) { - ////PrintError("Message length exceeds max limit of %d\n",MAXML); //COMMON_071024_1 + LOGE(OSA_LIB, "Message length exceeds max limit of %d\n", MAXML); return OSAL_ERROR; } @@ -238,7 +234,7 @@ int OsaQueueSend(unsigned int uiQid, unsigned int uiFlags, void *pvMsg_buf, if (ret != 0) { //perror("In OsaQueueSend () : msgsnd failed"); //COMMON_071024_1 - ////PrintError("In OsaQueueSend() : Error no. : %d\n",errno); //COMMON_071024_1 + LOGE(OSA_LIB, "In OsaQueueSend() : Error no. : %d\n", errno); return ((int)errno); } @@ -267,12 +263,6 @@ int OsaQueueReceive(unsigned int uiQid, unsigned int uiFlags, void *pvMsgBuf, { #ifndef __NO_OS__ #ifdef POSIX_QUEUE - /* - uiFlags : IPC_NOWAIT , ZERO - */ -#if 0 - struct timespec absTimeOut; -#endif struct mq_attr tMqAttr; int uiMsgLen = 0; @@ -282,18 +272,6 @@ int OsaQueueReceive(unsigned int uiQid, unsigned int uiFlags, void *pvMsgBuf, else tMqAttr.mq_flags = 0; -#if 0 - - if (ptTimeOut && (!(uiFlags & OSAL_Q_NOWAIT))) { - absTimeOut.tv_sec = ptTimeOut->Sec; - absTimeOut.tv_nsec = ptTimeOut->NanoSec; - } else { - absTimeOut.tv_sec = 0; - absTimeOut.tv_nsec = 0; - } - -#endif - if (mq_setattr((mqd_t)uiQid, &tMqAttr, (struct mq_attr *)NULL) < 0) { perror("OsaQueueSend(): mq_setattr() Failed \n"); return ((int)errno); @@ -308,10 +286,6 @@ int OsaQueueReceive(unsigned int uiQid, unsigned int uiFlags, void *pvMsgBuf, // Update the received message length *pMsgLen = uiMsgLen; #else /*SYS5 MSG QUEUE*/ - /* - uiFlags : IPC_NOWAIT, MSG_NOERROR - buf_len : len - sizeof(int); - */ int iRet = 0; osaMsgBuf_t osaMsg; @@ -329,7 +303,7 @@ int OsaQueueReceive(unsigned int uiQid, unsigned int uiFlags, void *pvMsgBuf, if (errno != ENOMSG) { perror("In OsaQueueReceive() : msgrcv failed"); - //PrintError("In OsaQueueReceive() : Msg id %d, Error no. : %d\n", uiQid, errno); + LOGE(OSA_LIB, "In OsaQueueReceive() : Msg id %d, Error no. : %d\n", uiQid, errno); } return ((int)errno); @@ -363,7 +337,7 @@ int OsaQueueGetinfo(unsigned int uiQid, void *pvBuf) struct mq_attr tMqAttr; if (pvBuf == NULL) { - //PrintError("Null Argument(s) \n"); + LOGE(OSA_LIB, "Null Argument(s) \n"); return OSAL_ERROR; } @@ -387,7 +361,7 @@ int OsaQueueGetinfo(unsigned int uiQid, void *pvBuf) if (msgctl((int)uiQid, IPC_STAT, &buf) < 0) { perror("In OsaQueueGetinfo() : msgctl: msgctl failed"); - //PrintError("In OsaQueueGetinfo() : Error no. : %d\n",errno); + LOGE(OSA_LIB, "In OsaQueueGetinfo() : Error no. : %d\n", errno); return ((int)errno); } @@ -426,7 +400,7 @@ int OsaQueueSetinfo(unsigned int uiQid, void *pvBuf) struct mq_attr tMqAttr; if (pvBuf == NULL) { - //PrintError("Null Argument(s) \n"); + LOGE(OSA_LIB, "Null Argument(s) \n"); return OSAL_ERROR; } @@ -455,7 +429,7 @@ int OsaQueueSetinfo(unsigned int uiQid, void *pvBuf) if (msgctl((int)uiQid, IPC_SET, &buf) < 0) { perror("In OsaQueueGetinfo() : msgctl: msgctl failed"); - //PrintError("In OsaQueueGetinfo() : Error no. : %d\n",errno); + LOGE(OSA_LIB, "In OsaQueueGetinfo() : Error no. : %d\n", errno); return ((int)errno); } diff --git a/osal/OsaSem.c b/osal/OsaSem.c index 710a009..82af922 100644 --- a/osal/OsaSem.c +++ b/osal/OsaSem.c @@ -24,6 +24,7 @@ * Include files *-----------------------------------------------------------------------------*/ #include "OsaLinuxUser.h" +#include /*----------------------------------------------------------------------------- * Globals @@ -49,12 +50,12 @@ static int UlOsaSemCreate(const char bName[10], int iCount, int iAttribute, sem = (UlOsaSem_t *)malloc(sizeof(*sem)); if (!sem) { - //PrintError ("UlOsaSemCreate, Out of memory!\n"); + LOGE(OSA_LIB, "UlOsaSemCreate, Out of memory!\n"); return OSAL_ERROR; } if (sem_init(&sem->sem, 1, (unsigned int)iCount) < 0) { - //PrintError ("UlOsaSemCreate, sem_init Failed!\n"); + LOGE(OSA_LIB, "UlOsaSemCreate, sem_init Failed!\n"); free(sem); return OSAL_ERROR; } @@ -93,38 +94,19 @@ static int UlOsaSemGet(void *uiSmid, int iFlags, int iTimeout) if (iFlags == OSAL_SEM_NOWAIT) { /* no wait */ - //PrintDbg ("UlOsaSemGet-nowait(%s).\n", sem->bName); + LOGD(OSA_LIB, "UlOsaSemGet-nowait(%s).\n", sem->bName); ret = sem_trywait(&sem->sem); } else if (iTimeout == 0) { /* wait _inifinite_ */ - //PrintDbg ("UlOsaSemGet-infinite(%s).\n", sem->bName); + LOGD(OSA_LIB, "UlOsaSemGet-infinite(%s).\n", sem->bName); ret = OSAL_FAILURE_RETRY(sem_wait(&sem->sem)); } else { /* with _timeout_ */ - //PrintDbg ("UlOsaSemGet-timeout(%s).\n", sem->bName); + LOGD(OSA_LIB, "UlOsaSemGet-timeout(%s).\n", sem->bName); if (iTimeout < 0) { - //PrintError ("UlOsaSemGet-timeout: invalid arg!\n"); + LOGE(OSA_LIB, "UlOsaSemGet-timeout: invalid arg!\n"); return OSAL_ERROR; } - -#if 0 - struct timeval tv; - - gettimeofday(&tv, NULL); - tv.tv_sec += iTimeout / 1000000; - tv.tv_usec += iTimeout % 1000000; - - if (tv.tv_usec >= 1000000) { - tv.tv_sec += tv.tv_usec / 1000000; - tv.tv_usec %= 1000000; - } - - ts.tv_sec = tv.tv_sec; - ts.tv_nsec = tv.tv_usec * 1000; - - ret = OSAL_FAILURE_RETRY(sem_timedwait(&sem->sem, &ts)); -#endif - do { // SoC_D00003324 ret = sem_trywait(&sem->sem); @@ -138,18 +120,17 @@ static int UlOsaSemGet(void *uiSmid, int iFlags, int iTimeout) /* result */ if (ret == 0) { - //PrintDbg ("UlOsaSemGet(%s) success.\n", sem->bName); + LOGD(OSA_LIB, "UlOsaSemGet(%s) success.\n", sem->bName); return OSAL_OK; } else { if (iFlags == OSAL_SEM_NOWAIT && errno == EAGAIN) { - // PrintError ("UlOsaSemGet-nowait: now locked, failed to get.\n"); + // LOGE(OSA_LIB, "UlOsaSemGet-nowait: now locked, failed to get.\n"); return OSAL_ERROR; - } else if (iFlags == OSAL_SEM_WAIT && - iTimeout <= 0) {// Before : cond - (iTimeout > 0 && errno == ETIMEDOUT) - //PrintError ("UlOsaSemGet-timeout(%s): time-out\n",sem->bName); + } else if (iFlags == OSAL_SEM_WAIT && iTimeout <= 0) { + LOGE(OSA_LIB, "UlOsaSemGet-timeout(%s): time-out\n", sem->bName); return OSAL_ERR_TIMEOUT; } else { - //PrintError ("UlOsaSemGet error, errno=%d\n", errno); + LOGE(OSA_LIB, "UlOsaSemGet error, errno=%d\n", errno); return OSAL_ERROR; } } @@ -162,9 +143,9 @@ static int UlOsaSemRelease(void *uiSmid) if (!sem) return OSAL_ERROR; - //PrintDbg ("UlOsaSemRelease(%s)\n", sem->bName); + LOGD(OSA_LIB, "UlOsaSemRelease(%s)\n", sem->bName); if (sem_post(&sem->sem) != 0) { - //PrintError ("UlOsaSemRelease(%s) error! errno=%d.\n", sem->bName, errno); + LOGE(OSA_LIB, "UlOsaSemRelease(%s) error! errno=%d.\n", sem->bName, errno); return OSAL_ERROR; } else return OSAL_OK; @@ -177,12 +158,12 @@ static int UlOsaSemReset(void *uiSmid) if (!sem) return OSAL_ERROR; - //PrintDbg ("UlOsaSemReset(%s).\n", sem->bName); + LOGD(OSA_LIB, "UlOsaSemReset(%s).\n", sem->bName); /* For threads currently blocked, the effect of destroying is not defined in POSIX. Currently, this will not release any blocked threads. */ if (sem_destroy(&sem->sem) < 0) { - //PrintError ("UlOsaSemReset, sem_destroy errno=%d\n", errno); + LOGE(OSA_LIB, "UlOsaSemReset, sem_destroy errno=%d\n", errno); return OSAL_ERROR; } @@ -201,10 +182,10 @@ static int UlOsaSemGetval(void *uiSmid) return OSAL_ERROR; if (sem_getvalue(&sem->sem, &n) != 0) { - //PrintError ("UlOsaSemGetval(%s), sem_getvalue errno=%d\n", sem->bName, errno); + LOGE(OSA_LIB, "UlOsaSemGetval(%s), sem_getvalue errno=%d\n", sem->bName, errno); return OSAL_ERROR; } else { - //PrintDbg ("UlOsaSemGetval(%s): now %d\n", sem->bName, n); + LOGD(OSA_LIB, "UlOsaSemGetval(%s): now %d\n", sem->bName, n); return (int)n; } } @@ -324,7 +305,7 @@ int OsaMutCreate(const char bName[10], int iAttributes, void **puiMutid) pthread_mutex_t *pmutex_t; if (puiMutid == NULL) { - //PrintError("In OsaMutCreate() : NULL PTR ERROR"); + LOGE(OSA_LIB, "In OsaMutCreate() : NULL PTR ERROR"); return OSAL_ERROR; } @@ -353,7 +334,7 @@ int OsaMutCreate(const char bName[10], int iAttributes, void **puiMutid) pthread_mutexattr_destroy(&attr_t); } else { - //PrintError("In OsaMutCreate() : No memory"); + LOGE(OSA_LIB, "In OsaMutCreate() : No memory"); return OSAL_ERROR; } @@ -389,7 +370,7 @@ int OsaMutDelete(void *uiMutid) if (iRet < 0) { perror("In OsaMutDelete() : failed "); - //PrintError("Error no. : %d\n",errno); + LOGE(OSA_LIB, "Error no. : %d\n", errno); return ((int)errno); } @@ -419,7 +400,7 @@ int OsaMutRelease(void *uiMutid) if (iRet < 0) { perror("In OsaMutRelease() : failed "); - //PrintError("Error no. : %d\n",errno); + LOGE(OSA_LIB, "Error no. : %d\n", errno); return ((int)errno); } @@ -446,7 +427,7 @@ int OsaMutGet(void *uiMutid, int iFlags, int iTimeout) if (iRet < 0) { perror("In OsaMutGet() : failed "); - //PrintError("Error no. : %d\n",errno); + LOGE(OSA_LIB, "Error no. : %d\n", errno); return ((int)errno); } diff --git a/osal/OsaSignal.c b/osal/OsaSignal.c index 14edc56..bf09d3b 100644 --- a/osal/OsaSignal.c +++ b/osal/OsaSignal.c @@ -24,6 +24,7 @@ * Include files *-----------------------------------------------------------------------------*/ #include "OsaLinuxUser.h" +#include /*----------------------------------------------------------------------------- * Functions @@ -50,7 +51,7 @@ int OsaSigProcmask(int iMode, const unsigned int *puiNewmask, if (iRet) { perror("SigProcMask: SigProcMask Failed "); - //PrintError("Error No. : %d\n",errno); + LOGE(OSA_LIB, "Error No. : %d\n", errno); return ((int)errno); } @@ -77,7 +78,7 @@ int OsaSigSuspend(unsigned int *puiPending) if (iRet) { perror("SigSuspend: SigSuspend INTR "); - //PrintError("Error No. : %d\n",errno); + LOGE(OSA_LIB, "Error No. : %d\n", errno); return ((int)errno); } @@ -105,7 +106,7 @@ int OsaSigTimedwait(void) if (iRet) { perror("TimeWait: TimeWait INTR "); - //PrintError("Error No. : %d\n",errno); + LOGE(OSA_LIB, "Error No. : %d\n", errno); return ((int)errno); } @@ -133,7 +134,7 @@ int OsaSigSetmask(int iSigno) if (sigaddset(&(Action_t.sa_mask), iSigno) < 0) { perror("sigaddset: sigaddset Failed "); - //PrintError("Error No. : %d\n",errno); + LOGE(OSA_LIB, "Error No. : %d\n", errno); return ((int)errno); } diff --git a/osal/OsaTask.c b/osal/OsaTask.c index afcc382..3c719bf 100644 --- a/osal/OsaTask.c +++ b/osal/OsaTask.c @@ -24,6 +24,7 @@ * Include files *-----------------------------------------------------------------------------*/ #include "OsaLinuxUser.h" +#include /*----------------------------------------------------------------------------- * MACROS @@ -59,7 +60,7 @@ static void *_thread_start_handler(void *pArg) if (iRet) { perror("In OsaTaskSpawn() : prctl() Failed\n "); - //PrintError("In OsaTaskSpawn() : prctl() error no. : %d\n", iRet); + LOGE(OSA_LIB, "In OsaTaskSpawn() : prctl() error no. : %d\n", iRet); } (*sThreadParam.pEntryFunc)(sThreadParam.pArg); @@ -132,7 +133,7 @@ int OsaTaskSpawn(const char *pName, void **puiTid, int iPriority, *puiTid = NULL; free(pThreadParam); perror("In OsaTaskSpawn() : pthread create Failed\n "); - //PrintError("In OsaTaskSpawn() : error no. : %d\n", iRet); + LOGE(OSA_LIB, "In OsaTaskSpawn() : error no. : %d\n", iRet); return ((int)iRet); } } @@ -144,7 +145,7 @@ int OsaTaskSpawn(const char *pName, void **puiTid, int iPriority, *puiTid = NULL; free(pThreadParam); perror("In OsaTaskSpawn() : pthread attr init Failed\n "); - //PrintError("In OsaTaskSpawn() : error no. : %d\n", iRet); + LOGE(OSA_LIB, "In OsaTaskSpawn() : error no. : %d\n", iRet); return ((int)iRet); } @@ -154,7 +155,7 @@ int OsaTaskSpawn(const char *pName, void **puiTid, int iPriority, *puiTid = NULL; free(pThreadParam); perror("In OsaTaskSpawn() : pthread attr setstacksize Failed\n "); - //PrintError("In OsaTaskSpawn() : error no. : %d\n", iRet); + LOGE(OSA_LIB, "In OsaTaskSpawn() : error no. : %d\n", iRet); pthread_attr_destroy(&tattr_t); return ((int)iRet); } @@ -166,7 +167,7 @@ int OsaTaskSpawn(const char *pName, void **puiTid, int iPriority, *puiTid = NULL; free(pThreadParam); perror("In OsaTaskSpawn() : pthread create Failed\n "); - //PrintError("In OsaTaskSpawn() : error no. : %d\n", iRet); + LOGE(OSA_LIB, "In OsaTaskSpawn() : error no. : %d\n", iRet); pthread_attr_destroy(&tattr_t); return ((int)iRet); } @@ -182,7 +183,7 @@ int OsaTaskSpawn(const char *pName, void **puiTid, int iPriority, if (iRet) { *puiTid = NULL; perror("In OsaTaskSpawn() : pthread setschedparam Failed\n "); - //PrintError("In OsaTaskSpawn() : error no. : %d\n", iRet); + LOGE(OSA_LIB, "In OsaTaskSpawn() : error no. : %d\n", iRet); pthread_kill(createThread, 0); return ((int)iRet); } @@ -193,14 +194,14 @@ int OsaTaskSpawn(const char *pName, void **puiTid, int iPriority, if (iRet) { *puiTid = NULL; perror("In OsaTaskSpawn() : pthread_detach Failed\n "); - //PrintError("In OsaTaskSpawn() : detach error no. : %d\n", iRet); + LOGE(OSA_LIB, "In OsaTaskSpawn() : detach error no. : %d\n", iRet); pthread_kill(createThread, 0); return ((int)iRet); } *puiTid = (void *)createThread; - //PrintDbg("%s thread created policy: %d, priority %d\n", pName, createThreadPolicy, iPriority); + LOGD(OSA_LIB, "%s thread created policy: %d, priority %d\n", pName, createThreadPolicy, iPriority); return OSAL_OK; } @@ -241,7 +242,7 @@ int OsaTaskDelete(void *uiTid) if (iRet) { perror("In OsaTaskDelete() : TaskDelete Failed "); - //PrintError("In OsaTaskDelete() : error no. : %d\n",errno); + LOGE(OSA_LIB, "In OsaTaskDelete() : error no. : %d\n",errno); return ((int)errno); } @@ -273,7 +274,7 @@ int OsaTaskSetPriority(unsigned int uiTid, int iNewpriority) if (iRet) { perror("In OsaTaskSetPriority() : TaskSetPriority set Failed "); - //PrintError("In OsaTaskSetPriority() : error no. : %d\n",errno); + LOGE(OSA_LIB, "In OsaTaskSetPriority() : error no. : %d\n",errno); return ((int)errno); } @@ -303,7 +304,7 @@ int OsaTaskGetPriority(unsigned int uiTid, int *piPriority) if (iRet) { piPriority = NULL; perror("In OsaTaskGetPriority() : TaskGetPriority Failed "); - //PrintError("In OsaTaskGetPriority() : error no. : %d\n",errno); + LOGE(OSA_LIB, "In OsaTaskGetPriority() : error no. : %d\n",errno); return ((int)errno); } @@ -341,7 +342,7 @@ int OsaTaskNanosleep(int iNanosec) if (iRetval) { perror("TaskNanoSleep: TaskNanoSleep Failed "); - //PrintError("Error No. : %d\n",errno); + LOGE(OSA_LIB, "Error No. : %d\n",errno); return ((int)errno); } @@ -395,7 +396,7 @@ int OsaTaskDelaymsecs(unsigned int uiMsec) if (iRetval) { perror("In OsaTaskDelaymsecs() : TaskNanoSleep Failed "); - //PrintError("In OsaTaskDelaymsecs() : error no. : %d\n",errno); + LOGE(OSA_LIB, "In OsaTaskDelaymsecs() : error no. : %d\n",errno); return ((int)errno); } diff --git a/simulatordaemon/inc/SecurityContext.h b/simulatordaemon/inc/SecurityContext.h index 6dddfef..2008a57 100644 --- a/simulatordaemon/inc/SecurityContext.h +++ b/simulatordaemon/inc/SecurityContext.h @@ -33,7 +33,7 @@ class SecurityContext { private: - int connFd; + int m_connFd; static constexpr const size_t CYNARA_CACHE_SIZE = 100U; diff --git a/simulatordaemon/src/ClientCommands/MakeCommand.cpp b/simulatordaemon/src/ClientCommands/MakeCommand.cpp index 6ea78cb..a41f178 100644 --- a/simulatordaemon/src/ClientCommands/MakeCommand.cpp +++ b/simulatordaemon/src/ClientCommands/MakeCommand.cpp @@ -179,10 +179,6 @@ uint32_t MakeCommand::getDataSize(TEE_CMD command) { size = sizeof(IntTACloseSessionData); LOGD(SIM_DAEMON, "[TEEC] IntTACloseSessionData Size: %d", size); break; - case CHECK_MEMORY: - // size = sizeof(CheckMemoryData); - LOGD(SIM_DAEMON, "[TEEC] CheckMemoryData Size: %d", size); - break; case PANIC: size = sizeof(IntTAPanicData); LOGD(SIM_DAEMON, "[TEEC] PanicData Size: %d", size); diff --git a/simulatordaemon/src/SecurityContext.cpp b/simulatordaemon/src/SecurityContext.cpp index 109170f..70b867f 100644 --- a/simulatordaemon/src/SecurityContext.cpp +++ b/simulatordaemon/src/SecurityContext.cpp @@ -51,7 +51,7 @@ bool SecurityContext::clientHasCynaraPermission(const std::string &privelege) { pthread_mutex_lock(&cynara_mutex); char *label = nullptr; - ret = cynara_creds_socket_get_client(connFd, CLIENT_METHOD_SMACK, &label); + ret = cynara_creds_socket_get_client(m_connFd, CLIENT_METHOD_SMACK, &label); if (ret != CYNARA_API_SUCCESS) { LOGE(SIM_DAEMON, "Couldn't get smack label of the client. Error code: %d", ret); RETURN_UNLOCK(false, cynara_mutex); @@ -59,7 +59,7 @@ bool SecurityContext::clientHasCynaraPermission(const std::string &privelege) { p_char p_label(label, &free); pid_t ca_pid = -1; - ret = cynara_creds_socket_get_pid(connFd, &ca_pid); + ret = cynara_creds_socket_get_pid(m_connFd, &ca_pid); if (ret != CYNARA_API_SUCCESS) { LOGE(SIM_DAEMON, "Couldn't get pid of the client. Error code: %d", ret); RETURN_UNLOCK(false, cynara_mutex); @@ -74,7 +74,7 @@ bool SecurityContext::clientHasCynaraPermission(const std::string &privelege) { p_char p_session(session, &free); char *user = nullptr; - ret = cynara_creds_socket_get_user(connFd, USER_METHOD_DEFAULT, &user); + ret = cynara_creds_socket_get_user(m_connFd, USER_METHOD_DEFAULT, &user); if (ret != CYNARA_API_SUCCESS) { LOGE(SIM_DAEMON, "Couldn't get user. Error code: %d", ret); RETURN_UNLOCK(false, cynara_mutex); @@ -131,7 +131,7 @@ SecurityContext::SecurityContext(): SecurityContext::SecurityContext(int connFd): - connFd(connFd) { + m_connFd(connFd) { if (_cynara == nullptr) throw std::runtime_error("Cynara is not initialized"); } diff --git a/simulatordaemon/src/TABinaryManager/TAManifest.cpp b/simulatordaemon/src/TABinaryManager/TAManifest.cpp index 90eb3bb..1f9c397 100644 --- a/simulatordaemon/src/TABinaryManager/TAManifest.cpp +++ b/simulatordaemon/src/TABinaryManager/TAManifest.cpp @@ -86,13 +86,8 @@ bool TAManifest::processXML(const string &xmlManifestPath) { properties.extension.appName = string(propertiesExtension->first_attribute("appName")->value()); properties.extension.appVersion = string(propertiesExtension->first_attribute("appVersion")->value()); - /*properties.extension.type = string(propertiesExtension->first_attribute("type")->value()); - properties.extension.zone = string(propertiesExtension->first_attribute("zone")->value());*/ properties.extension.sdkVersion = string(propertiesExtension->first_attribute("sdkVersion")->value()); - // Removed, taEncrypion flag used now - //properties.extension.secret = string(propertiesExtension->first_attribute("secret")->value()); - properties.extension.launchMode = string(propertiesExtension->first_attribute("launchMode")->value()); } } diff --git a/simulatordaemon/src/TABinaryManager/TAUnpack.cpp b/simulatordaemon/src/TABinaryManager/TAUnpack.cpp index e2aa854..01a4a32 100644 --- a/simulatordaemon/src/TABinaryManager/TAUnpack.cpp +++ b/simulatordaemon/src/TABinaryManager/TAUnpack.cpp @@ -86,7 +86,6 @@ int TAUnpack::unpackTA(string path, string uuid) { LOGE(SIM_DAEMON, "Read failed"); return -1; } - // fixHeaderEndianness(&packageHeader); // 2. Verify header if (SECURITY_HEADER_MAGIC1 == packageHeader.magic1 && SECURITY_HEADER_MAGIC2 == packageHeader.magic2) { @@ -120,7 +119,6 @@ int TAUnpack::unpackTA(string path, string uuid) { LOGE(SIM_DAEMON, "Read failed"); return -1; } - //manifest.write(manifestdump, sizeWithoutPadding(manifestdump, packageHeader.manifest_size)); manifest.write(manifestdump, packageHeader.manifest_size); manifest.flush(); delete[] manifestdump; diff --git a/ssflib/dep/cryptocore/source/base/cc_bignum.c b/ssflib/dep/cryptocore/source/base/cc_bignum.c index 2119c79..d6c96d7 100644 --- a/ssflib/dep/cryptocore/source/base/cc_bignum.c +++ b/ssflib/dep/cryptocore/source/base/cc_bignum.c @@ -424,18 +424,7 @@ static cc_u32 SDRM_DWD_MulAdd(cc_u32 *pdDest, cc_u32 dDstLen, cc_u32 *pdSrc, pdDest[i] = (cc_u32)pdDigit2; dTemp = pdDigit2 >> 32; - /*SDRM_DIGIT_Mul(pdDigit, dMultiplier, pdSrc[i]); - if ((dTemp += pdDigit[0]) < pdDigit[0]) - { - pdDigit[1]++; - } - - if ((pdDest[i] += dTemp) < dTemp) - { - pdDigit[1]++; - } - dTemp = pdDigit[1];*/ } if (i == dDstLen) @@ -536,18 +525,7 @@ static void SDRM_DWD_Mul(cc_u32 *pdDest, cc_u32 *pdSrc1, cc_u32 dSrcLen1, dTemp = pdDigit2 >> 32; - /*SDRM_DIGIT_Mul(pdDigit, pdSrc1[i], pdSrc2[j]); - if ((dTemp += pdDigit[0]) < pdDigit[0]) - { - pdDigit[1]++; - } - if ((pdDest[i + j] += dTemp) < dTemp) - { - pdDigit[1]++; - } - - dTemp = pdDigit[1];*/ } pdDest[i + j] = dTemp; @@ -1436,8 +1414,6 @@ int SDRM_BN_Div(SDRM_BIG_NUM *BN_Quotient, SDRM_BIG_NUM *BN_Remainder, if (SDRM_BN_Cmp(temp_Dividend, temp_Divisor) < 0) { if (BN_Remainder != NULL) { SDRM_BN_Copy(BN_Remainder, temp_Dividend); - //free(pbBuf); - //return CRYPTO_SUCCESS; modify by Chalyi Aleksandr: it is not correct } if (BN_Quotient != NULL) @@ -1911,10 +1887,8 @@ int SDRM_MONT_Rzn2zn(SDRM_BIG_NUM *BN_Dst, SDRM_BIG_NUM *BN_Src1, SDRM_BN_OPTIMIZE_LENGTH(Src1); SDRM_BN_SHR(BN_Dst, Src1, (Mod_Len) * 32); - //BN_Dst->Length = Src1->Length - ri; - BN_Dst->Length = Src1->Length - ri - 1; //Added by yhhwang + BN_Dst->Length = Src1->Length - ri - 1;//Added by yhhwang - //if (SDRM_BN_Cmp(BN_Dst, Mont->Mod) >= 0) while (SDRM_BN_Cmp(BN_Dst, Mont->Mod) >= 0) SDRM_BN_Sub(BN_Dst, BN_Dst, Mont->Mod); @@ -1939,45 +1913,6 @@ int SDRM_MONT_Mul(SDRM_BIG_NUM *BN_Dst, SDRM_BIG_NUM *BN_Src1, { int ret; - /* Begin - Add to test input range by Yong Ho Hwang (20120809) */ - /* - if (SDRM_BN_Cmp(BN_Src1, Mont->Mod) >= 0) - { - ret = SDRM_BN_ModRed(BN_Src1, BN_Src1, Mont->Mod); - if (ret != CRYPTO_SUCCESS) - { - return ret; - } - } else if ( BN_Src1->sign == 1) - { - printf("Minus Value\n"); - ret = SDRM_BN_Add(BN_Src1, BN_Src1, Mont->Mod); - if (BN_Src1->sign == 1) - { - printf("Value Fail.\n"); - return CRYPTO_ERROR; - } - } - - if (SDRM_BN_Cmp(BN_Src2, Mont->Mod) >= 0) - { - ret = SDRM_BN_ModRed(BN_Src2, BN_Src2, Mont->Mod); - if (ret != CRYPTO_SUCCESS) - { - return ret; - } - } else if ( BN_Src2->sign == 1) - { - printf("Minus Value\n"); - ret = SDRM_BN_Add(BN_Src2, BN_Src2, Mont->Mod); - if (BN_Src2->sign == 1) - { - printf("Value Fail.\n"); - return CRYPTO_ERROR; - } - } - */ - /* End - Add to test input range by Yong Ho Hwang (20120809) */ ret = SDRM_BN_Mul(BN_Dst, BN_Src1, BN_Src2); @@ -1986,17 +1921,6 @@ int SDRM_MONT_Mul(SDRM_BIG_NUM *BN_Dst, SDRM_BIG_NUM *BN_Src1, ret = SDRM_MONT_Rzn2zn(BN_Dst, BN_Dst, Mont); - /* Begin - Add to test input range by Yong Ho Hwang (20120809) */ - /* - if (SDRM_BN_Cmp(BN_Dst, Mont->Mod) >= 0) - { - printf("Output is bigger than Mod\n"); - } else if ( BN_Dst->sign == 1) - { - printf("Minus Value\n"); - } - */ - /* End - Add to test input range by Yong Ho Hwang (20120809) */ return ret; } @@ -2049,39 +1973,7 @@ int SDRM_MONT_Set(SDRM_BIG_MONT *Mont, SDRM_BIG_NUM *BN_Modulus) R = SDRM_BN_Alloc((cc_u8 *)Ri + dAllocSize, dSize); temp = SDRM_BN_Alloc((cc_u8 *)R + dAllocSize, dSize); - //++ 2012.08.20 - modified by yhhwang to apply R=2^(160+32) - /* == DELETED == - SDRM_BN_Copy(Mont->Mod, BN_Modulus); - - Mont->ri = (SDRM_BN_num_bits(BN_Modulus) + (SDRM_BitsInDWORD - 1)) / SDRM_BitsInDWORD * SDRM_BitsInDWORD; - - SDRM_BN_SHL(R, BN_One, SDRM_BitsInDWORD); - - buf[0] = BN_Modulus->pData[0]; - buf[1] = 0; - temp->pData[0] = buf[0]; - temp->Length = 1; - temp->sign = BN_Modulus->sign; - - SDRM_BN_ModInv(Ri, R, temp); - if (Ri == NULL) - { - free(pbBuf); - - return CRYPTO_INVERSE_NOT_EXIST; - } - - SDRM_BN_SHL(Ri, Ri, SDRM_BitsInDWORD); - SDRM_BN_Sub(Ri, Ri, BN_One); - SDRM_BN_Div(Ri, NULL, Ri, temp); - SDRM_BN_Copy(Mont->Inv_Mod, Ri); - Mont->N0 = Ri->pData[0]; - - SDRM_BN_SHL(Mont->R, BN_One, 2 * (32 + Mont->ri)); - SDRM_BN_ModRed(Mont->R, Mont->R, Mont->Mod); - */ - // == NEW CODE == SDRM_BN_Copy(Mont->Mod, BN_Modulus); Mont->Mod->pData[Mont->Mod->Length] = 0; @@ -2113,7 +2005,6 @@ int SDRM_MONT_Set(SDRM_BIG_MONT *Mont, SDRM_BIG_NUM *BN_Modulus) // Compute R and R^2 mod M SDRM_BN_SHL(Rsquare, BN_One, r2Size); SDRM_BN_ModRed(Mont->R, Rsquare, BN_Modulus); - //-- 2012.08.20 - modified by yhhwang free(pbBuf); free(Rsquare); @@ -2595,16 +2486,9 @@ int SDRM_HEX2BN(cc_u8 *pbSrc, SDRM_BIG_NUM *BN_Dst) //normalize length if (n % SDRM_SIZE_BLOCK != 0) - BN_Dst->Length += 1; - -#if 0 //fix prevent problem by guoxing.xu 20140826. move to before - - if (!BN_Dst) - BN_Dst = SDRM_BN_Init(BN_Dst->Length * SDRM_SIZE_OF_DWORD * 8); - -#endif + BN_Dst->Length += 1; - for (i = 0; i < BN_Dst->Length ; i++) + for (i = 0; i < BN_Dst->Length; i++) BN_Dst->pData[i] = 0; //full string: bufferHex mod Length = 0 @@ -2800,10 +2684,8 @@ cc_u8 *SDRM_BN2STRFOUR(cc_u32 *numberBits, SDRM_BIG_NUM *BN_Src) while (!SDRM_BN_isZero(num)) { SDRM_BN_Div(num, tempREM, num, d); - //itoa(tempREM->pData[0], (char *)tempChar, 10); - //sprintf((char*)tempChar, "%d", tempREM->pData[0]); snprintf((char *)tempChar, sizeof(tempChar), "%d", - tempREM->pData[0]); // fix prevnet 60199 by guoxing.xu + tempREM->pData[0]); strDestTemp[(*numberBits)] = tempChar[0]; (*numberBits)++; @@ -2864,14 +2746,11 @@ SDRM_BIG_NUM **SDRM_BN_MassInit(cc_u32 dBufSize, cc_u32 count) ptr = (cc_u8 *)BN_Buf + sizeof(SDRM_BIG_NUM *) * count; for (i = 0; i < count; i++) { - //add by guoxing.xu to avoid warning. 2/15/2014 tmp = ptr; BN_Buf[i] = (SDRM_BIG_NUM *)tmp; - //BN_Buf[i] = (SDRM_BIG_NUM*)ptr; BN_Buf[i]->Size = dBufSize; tmp = (ptr + sizeof(SDRM_BIG_NUM)); BN_Buf[i]->pData = (cc_u32 *)tmp; - //BN_Buf[i]->pData = (cc_u32*)(ptr + sizeof(SDRM_BIG_NUM)); ptr += bnsiz; } diff --git a/ssflib/dep/cryptocore/source/base/cc_ecc.c b/ssflib/dep/cryptocore/source/base/cc_ecc.c index 9908edd..8d52715 100644 --- a/ssflib/dep/cryptocore/source/base/cc_ecc.c +++ b/ssflib/dep/cryptocore/source/base/cc_ecc.c @@ -1040,8 +1040,6 @@ int SDRM_CTX_EC_2kP(SDRM_ECC_CTX *ctx, SDRM_EC_POINT *EC_Dst, SDRM_BIG_NUM *k1, // Precomputation data for (i = 0; i < 2; i++) { - // Pw[i] = (SDRM_EC_POINT **)malloc(sizeof(SDRM_EC_POINT *) * w2); - // if (!Pw[i]) return CRYPTO_MEMORY_ALLOC_FAIL; for (j = 0; j < 9; j++) Pw[i][j] = SDRM_ECC_Init(); } diff --git a/ssflib/dep/cryptocore/source/base/cc_md5.c b/ssflib/dep/cryptocore/source/base/cc_md5.c index cbb667f..6cef2f0 100644 --- a/ssflib/dep/cryptocore/source/base/cc_md5.c +++ b/ssflib/dep/cryptocore/source/base/cc_md5.c @@ -36,13 +36,6 @@ static void SDRM_Decode(cc_u32 *, const unsigned char *, cc_u32); static unsigned char PADDING[64] = {0x80, 0,}; -/* F, G, H and I are basic MD5 functions. - */ -//#define F(x, y, z) (((x) & (y)) | ((~x) & (z))) -//#define G(x, y, z) (((x) & (z)) | ((y) & (~z))) -//#define H(x, y, z) ((x) ^ (y) ^ (z)) -//#define I(x, y, z) ((y) ^ ((x) | (~z))) - #define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z)))) #define G(x, y, z) F(z, x, y) #define H(x, y, z) ((x) ^ (y) ^ (z)) diff --git a/ssflib/dep/cryptocore/source/base/cc_pkcs1_v21.c b/ssflib/dep/cryptocore/source/base/cc_pkcs1_v21.c index 74f7eb5..dfd2171 100644 --- a/ssflib/dep/cryptocore/source/base/cc_pkcs1_v21.c +++ b/ssflib/dep/cryptocore/source/base/cc_pkcs1_v21.c @@ -755,7 +755,6 @@ int SDRM_Enpad_Rsassa_pss(cc_u8 *EM, cc_u32 nBits, cc_u8 *h, cc_u32 hLen, EM[emLen - sLen - hLen - 2] ^= 0x01; - //memset(EM, 0x00, emLen - sLen - hLen - 2); for (i = 0; i < sLen; i++) EM[emLen - sLen - hLen - 1 + i] ^= salt[i]; diff --git a/ssflib/dep/cryptocore/source/base/cc_rc4.c b/ssflib/dep/cryptocore/source/base/cc_rc4.c index ec769e6..245f2e6 100644 --- a/ssflib/dep/cryptocore/source/base/cc_rc4.c +++ b/ssflib/dep/cryptocore/source/base/cc_rc4.c @@ -87,10 +87,8 @@ int SDRM_RC4_Setup(SDRM_RC4Context *ctx, cc_u8 *UserKey, cc_u32 keyLen) i = 0xff; if (((cc_u8 *)&i)[0] == 0xff) { - // LOG4DRM_INFO(&CryptoLogCTX), "is Little Endian machine\n"); memcpy(ctx->s, RC4_S_VALUE_LITTLE, 256); } else { - // LOG4DRM_INFO(&CryptoLogCTX), "is Big Endian machine\n"); memcpy(ctx->s, RC4_S_VALUE_BIG, 256); } diff --git a/ssflib/dep/cryptocore/source/base/cc_sha1.c b/ssflib/dep/cryptocore/source/base/cc_sha1.c index 8f3de28..c64dcbc 100644 --- a/ssflib/dep/cryptocore/source/base/cc_sha1.c +++ b/ssflib/dep/cryptocore/source/base/cc_sha1.c @@ -75,12 +75,10 @@ static void SDRM_SHAtoByte(unsigned char *output, unsigned int *input, save one boolean operation each - thanks to Rich Schroeppel, rcs@cs.arizona.edu for discovering this */ -/*#define SDRM_SHA1_f1(x,y,z) ((x & y) | (~x & z)) // Rounds 0-19 */ -#define SDRM_SHA1_f1(x, y, z) ((z) ^ ((x) & ((y) ^ (z)))) /* Rounds 0-19 */ -#define SDRM_SHA1_f2(x, y, z) ((x) ^ (y) ^ (z)) /* Rounds 20-39 */ -/*#define SDRM_SHA1_f3(x,y,z) ((x & y) | (x & z) | (y & z)) // Rounds 40-59 */ -#define SDRM_SHA1_f3(x, y, z) (((x) & (y)) | ((z) & ((x) | (y)))) /* Rounds 40-59 */ -#define SDRM_SHA1_f4(x, y, z) ((x) ^ (y) ^ (z)) /* Rounds 60-79 */ +#define SDRM_SHA1_f1(x, y, z) ((z) ^ ((x) & ((y) ^ (z)))) /* Rounds 0-19 */ +#define SDRM_SHA1_f2(x, y, z) ((x) ^ (y) ^ (z)) /* Rounds 20-39 */ +#define SDRM_SHA1_f3(x, y, z) (((x) & (y)) | ((z) & ((x) | (y)))) /* Rounds 40-59 */ +#define SDRM_SHA1_f4(x, y, z) ((x) ^ (y) ^ (z)) /* Rounds 60-79 */ /* The SHS Mysterious Constants */ @@ -464,77 +462,6 @@ static void SDRM_SHAtoByte(unsigned char *output, unsigned int *input, } -//unsigned char digest[20]; -//unsigned char message[3] = {'a', 'b', 'c' }; -//unsigned char *mess56 = -// "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"; - -/* Correct solutions from FIPS PUB 180-1 */ -//char *dig1 = "A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D"; -//char *dig2 = "84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1"; -//char *dig3 = "34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F"; - -/* Output should look like:- - a9993e36 4706816a ba3e2571 7850c26c 9cd0d89d - A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D <= correct - 84983e44 1c3bd26e baae4aa1 f95129e5 e54670f1 - 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1 <= correct - 34aa973c d4c4daa4 f61eeb2b dbad2731 6534016f - 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F <= correct -*/ - -//main() -//{ -// SHA_CTX sha; -// int i; -// BYTE big[1000]; -// -// SHAInit(&sha); -// SHAUpdate(&sha, message, 3); -// SHAFinal(digest, &sha); -// -// for (i = 0; i < 20; i++) -// { -// if ((i % 4) == 0) printf(" "); -// printf("%02x", digest[i]); -// } -// printf("\n"); -// printf(" %s <= correct\n", dig1); -// -// SHAInit(&sha); -// SHAUpdate(&sha, mess56, 56); -// SHAFinal(digest, &sha); -// -// for (i = 0; i < 20; i++) -// { -// if ((i % 4) == 0) printf(" "); -// printf("%02x", digest[i]); -// } -// printf("\n"); -// printf(" %s <= correct\n", dig2); -// -// /* Fill up big array */ -// for (i = 0; i < 1000; i++) -// big[i] = 'a'; -// -// SHAInit(&sha); -// /* Digest 1 million x 'a' */ -// for (i = 0; i < 1000; i++) -// SHAUpdate(&sha, big, 1000); -// SHAFinal(digest, &sha); -// -// for (i = 0; i < 20; i++) -// { -// if ((i % 4) == 0) printf(" "); -// printf("%02x", digest[i]); -// } -// printf("\n"); -// printf(" %s <= correct\n", dig3); -// -// return 0; -//} - -/* endian.c */ void SDRM_endianTest(int *endian_ness) { diff --git a/ssflib/dep/cryptocore/source/middle/cc_cmac.c b/ssflib/dep/cryptocore/source/middle/cc_cmac.c index 1e33d93..0394bf6 100644 --- a/ssflib/dep/cryptocore/source/middle/cc_cmac.c +++ b/ssflib/dep/cryptocore/source/middle/cc_cmac.c @@ -107,8 +107,7 @@ int SDRM_CMAC_init(CryptoCoreContainer *crt, cc_u8 *Key, cc_u32 KeyLen) BlockXor(K2, K2, R_b); } - // LOG4DRM_BUFFER(&CryptoLogCTX), LOG_DEBUG, "K1", K1, 16); - // LOG4DRM_BUFFER(&CryptoLogCTX), LOG_DEBUG, "K2", K2, 16); + return CRYPTO_SUCCESS; } @@ -161,7 +160,7 @@ int SDRM_CMAC_update(CryptoCoreContainer *crt, cc_u8 *msg, cc_u32 msgLen) ptr += SDRM_AES_BLOCK_SIZ; } - // LOG4DRM_BUFFER(&CryptoLogCTX), LOG_DEBUG, "Block", crt->ctx->cmacctx->IV, 16); + memcpy(crt->ctx->cmacctx->Block, ptr, crt->ctx->cmacctx->BlockLen); diff --git a/ssflib/dep/cryptocore/source/middle/cc_dsa.c b/ssflib/dep/cryptocore/source/middle/cc_dsa.c index da53040..4ab673b 100644 --- a/ssflib/dep/cryptocore/source/middle/cc_dsa.c +++ b/ssflib/dep/cryptocore/source/middle/cc_dsa.c @@ -570,8 +570,7 @@ int SDRM_DSA_verify(CryptoCoreContainer *crt, cc_u8 *hash, cc_u32 hashLen, SDRM_BN_ModRed(v, temp3, crt->ctx->dsactx->q); //v = (alpha^u1 x y^u2 mod p) mod q - // SDRM_PrintBN("v : ", v); - // SDRM_PrintBN("Hash : ", BNH_m); + if (SDRM_BN_Cmp(v, BN_r) == 0) *result = CRYPTO_VALID_SIGN; diff --git a/ssflib/dep/cryptocore/source/middle/cc_ecdsa.c b/ssflib/dep/cryptocore/source/middle/cc_ecdsa.c index 1725ead..141797f 100644 --- a/ssflib/dep/cryptocore/source/middle/cc_ecdsa.c +++ b/ssflib/dep/cryptocore/source/middle/cc_ecdsa.c @@ -180,7 +180,6 @@ int SDRM_CTX_ECDSA_SIG_GEN(SDRM_ECC_CTX *ctx, cc_u8 *sig, cc_u8 *hash, return res; } - //SDRM_PrintBN("kP->x", kP->x); SDRM_BN_ModRed(BN_r, kP->x, ctx->ECC_n); if (BN_r->Length > 0) // r = 0 �̸� k �ٽ� ���� @@ -190,9 +189,6 @@ int SDRM_CTX_ECDSA_SIG_GEN(SDRM_ECC_CTX *ctx, cc_u8 *sig, cc_u8 *hash, // 3. k^{-1} mod n ���. SDRM_BN_ModInv(BN_Tmp1, BN_k, ctx->ECC_n); - //SDRM_PrintBN("BN_k", BN_k); - //SDRM_PrintBN("ctx->ECC_n", ctx->ECC_n); - //SDRM_PrintBN("BN_Tmp1 = k^{-1} mod n", BN_Tmp1); // 4. s = k^{-1}(hash + dr) mod n ��� (d = private key). s = 0 �̸� 1������. // BN_Tmp2 = dr @@ -206,9 +202,7 @@ int SDRM_CTX_ECDSA_SIG_GEN(SDRM_ECC_CTX *ctx, cc_u8 *sig, cc_u8 *hash, break; } - // (r, s) �������� ���. - //SDRM_PrintBN("BN_r", BN_r); - //SDRM_PrintBN("BN_s", BN_s); +// (r, s) �������� ���. SDRM_BN2OS(BN_r, ctx->uDimension / 8, sig); SDRM_BN2OS(BN_s, ctx->uDimension / 8, sig + ctx->uDimension / 8); @@ -286,8 +280,6 @@ int SDRM_CTX_ECDSA_SIG_VERIFY(SDRM_ECC_CTX *ctx, cc_u8 *sig, int signLen, SDRM_OS2BN(sig, ctx->uDimension / 8, pBN_r); SDRM_OS2BN(sig + ctx->uDimension / 8, ctx->uDimension / 8, pBN_s); - //SDRM_PrintBN("BN_r", pBN_r); - //SDRM_PrintBN("BN_s", pBN_s); // 1. r�� s�� ���� ���� SDRM_BN_Sub(BN_tmp, ctx->ECC_n, BN_One); @@ -311,7 +303,6 @@ int SDRM_CTX_ECDSA_SIG_VERIFY(SDRM_ECC_CTX *ctx, cc_u8 *sig, int signLen, // 2. w = s^(-1) mod n, BN_hash ��� SDRM_OS2BN(hash, hashLen, BN_hash); res = SDRM_BN_ModInv(BN_w, pBN_s, ctx->ECC_n); - //SDRM_PrintBN("BN_w", BN_w); if (res != CRYPTO_SUCCESS) { free(pbBuf); @@ -324,8 +315,7 @@ int SDRM_CTX_ECDSA_SIG_VERIFY(SDRM_ECC_CTX *ctx, cc_u8 *sig, int signLen, // 3. u1 = BN_hash *w mod n, u2 = rw mod n SDRM_BN_ModMul(BN_u1, BN_hash, BN_w, ctx->ECC_n); SDRM_BN_ModMul(BN_u2, pBN_r, BN_w, ctx->ECC_n); - //SDRM_PrintBN("BN_u1", BN_u1); - //SDRM_PrintBN("BN_u2", BN_u2); + // 4. (x0, y0) = u1P + u2Q, V = x0 mod n res = SDRM_CTX_EC_2kP(ctx, EC_temp1, BN_u1, ctx->ECC_G, BN_u2, ctx->PUBLIC_KEY); @@ -345,12 +335,10 @@ int SDRM_CTX_ECDSA_SIG_VERIFY(SDRM_ECC_CTX *ctx, cc_u8 *sig, int signLen, return res; } - // SDRM_PrintBN("EC_temp1->x", EC_temp1->x); - // SDRM_PrintBN("ctx->ECC_n", ctx->ECC_n); + SDRM_BN_ModRed(BN_tmp, EC_temp1->x, ctx->ECC_n); - // SDRM_PrintBN("BN_tmp", BN_tmp); - // SDRM_PrintBN("pBN_r", pBN_r); + // 5. V = r�� ��� ���� ok res = SDRM_BN_Cmp_sign(BN_tmp, pBN_r); diff --git a/ssflib/dep/cryptocore/source/middle/cc_rsa.c b/ssflib/dep/cryptocore/source/middle/cc_rsa.c index 8aad8ad..9f24538 100644 --- a/ssflib/dep/cryptocore/source/middle/cc_rsa.c +++ b/ssflib/dep/cryptocore/source/middle/cc_rsa.c @@ -311,8 +311,6 @@ GEN_RND: q->pData[q->Length - 1] |= (1L << t1); } while (SDRM_BN_MILLER_RABIN(q, sp) != CRYPTO_ISPRIME); - // SDRM_PrintBN("p", p); - // SDRM_PrintBN("q", q); //temp1 = (p - 1), temp2 = (q - 1) SDRM_BN_Sub(temp1, p, BN_One); @@ -878,8 +876,6 @@ GEN_RND: q->pData[q->Length - 1] |= (1L << t1); } while (SDRM_BN_MILLER_RABIN(q, sp) != CRYPTO_ISPRIME); - // SDRM_PrintBN("p", p); - // SDRM_PrintBN("q", q); //temp1 = (p - 1), temp2 = (q - 1) @@ -1037,7 +1033,6 @@ int SDRM_RSA_encrypt(CryptoCoreContainer *crt, cc_u8 *in, cc_u32 inLen, return CRYPTO_INVALID_ARGUMENT; } - // SDRM_PrintBN("ENPADDED Text : ", BN_pMsg); if (retVal != CRYPTO_SUCCESS) { free(pbBuf); @@ -1293,7 +1288,6 @@ int SDRM_RSA_decryptByCRT(CryptoCoreContainer *crt, cc_u8 *in, cc_u32 inLen, return CRYPTO_INVALID_ARGUMENT; } - // SDRM_PrintBN("OAEP Text : ", BN_dMsg); SDRM_I2OSP(BN_dMsg, RSA_KeyByteLen, pbBuf); @@ -1377,24 +1371,17 @@ int SDRM_RSA_sign(CryptoCoreContainer *crt, cc_u8 *hash, cc_u32 hashLen, //Msg Padding switch (SDRM_LOW_HALF(crt->ctx->rsactx->pm)) { case ID_RSASSA_PKCS15: - retVal = SDRM_Enpad_Rsassa_pkcs15(pbBuf, RSA_KeyByteLen, hash, hashLen, - SDRM_HIGH_HALF(crt->ctx->rsactx->pm)); + retVal = SDRM_Enpad_Rsassa_pkcs15(pbBuf, RSA_KeyByteLen, hash, hashLen, SDRM_HIGH_HALF(crt->ctx->rsactx->pm)); break; - case ID_RSASSA_PSS: SDRM_BN_GETBITLEN(crt->ctx->rsactx->n, nBits); - retVal = SDRM_Enpad_Rsassa_pss(pbBuf, nBits, hash, hashLen, RSA_KeyByteLen, - SDRM_HIGH_HALF(crt->ctx->rsactx->pm)); + retVal = SDRM_Enpad_Rsassa_pss(pbBuf, nBits, hash, hashLen, RSA_KeyByteLen, SDRM_HIGH_HALF(crt->ctx->rsactx->pm)); break; - case ID_NO_PADDING: memset(pbBuf, 0x00, RSA_KeyByteLen - hashLen); - //memcpy(pbBuf + hashLen, hash, RSA_KeyByteLen); - memcpy(pbBuf + RSA_KeyByteLen - hashLen, hash, - hashLen);// fixed by guoxing.xu 20140919 + memcpy(pbBuf + RSA_KeyByteLen - hashLen, hash, hashLen);// fixed by guoxing.xu 20140919 retVal = CRYPTO_SUCCESS; break; - default: free(pbBuf); return CRYPTO_INVALID_ARGUMENT; @@ -1405,7 +1392,6 @@ int SDRM_RSA_sign(CryptoCoreContainer *crt, cc_u8 *hash, cc_u32 hashLen, return retVal; } - // SDRM_PrintBN("ENPADDED Msg : ", BN_pMsg); SDRM_OS2BN(pbBuf, RSA_KeyByteLen, BN_pMsg); @@ -1478,7 +1464,6 @@ int SDRM_RSA_verify(CryptoCoreContainer *crt, cc_u8 *hash, cc_u32 hashLen, SDRM_RSA_BN_BUFSIZE); SDRM_OS2BN(signature, signLen, BN_Sign); - // SDRM_PrintBN("Generated Sign : ", BN_Sign); //RSA Verification by modular exponent #ifndef _OP64_NOTSUPPORTED diff --git a/ssflib/dep/swdss/source/file_op.cpp b/ssflib/dep/swdss/source/file_op.cpp index c731bd0..9e1350b 100644 --- a/ssflib/dep/swdss/source/file_op.cpp +++ b/ssflib/dep/swdss/source/file_op.cpp @@ -74,8 +74,6 @@ int file_op::write_file(const char* filename, unsigned char* buffer, fflush(file); - //sync(fileno(file)); // sync blocked - fclose(file); return SS_RET_SUCCESS; @@ -198,7 +196,6 @@ int file_op::create_folder(const char* folder) { result = SS_RET_FAIL; SLOGE("Failed to create folder %s.", folder); } - //sync(); } else if (!(S_ISDIR(st.st_mode))) { result = SS_RET_FAIL; } diff --git a/ssflib/dep/swdss/source/secure_file.cpp b/ssflib/dep/swdss/source/secure_file.cpp index 9a828f1..8a0700a 100644 --- a/ssflib/dep/swdss/source/secure_file.cpp +++ b/ssflib/dep/swdss/source/secure_file.cpp @@ -24,7 +24,6 @@ #ifdef _SECOS_SIM_ #include "file_op.h" -//#define SWD_SS_ROOT "/opt/usr/apps/tz_simulator/data/swdss/" #define SWD_SS_ROOT "/tmp/tastore2/" #endif @@ -320,12 +319,6 @@ int is_valid_credential(const ss_credential_s& cred) { return SS_RET_INVALID_CREDENTIAL; } - // checking specific values - //if (tmp_uuid[14] != '4' || (tmp_uuid[19] != '8' && tmp_uuid[19] != '9' && tmp_uuid[19] != 'a' && tmp_uuid[19] != 'b')) - //{ - // return SS_RET_INVALID_CREDENTIAL; - //} - // checking that string contains only hexidecimal values if (-1 == is_hex(tmp_uuid, '-')) { return SS_RET_INVALID_CREDENTIAL; } @@ -488,9 +481,16 @@ int secure_file::derive_file_path() { return SS_RET_SUCCESS; } + int remaining = SS_FULL_DATA_NAME_LEN; + memcpy(m_full_path, m_cred.uuid, SS_MAX_UUID_LEN); - strcat(m_full_path, "/"); - strcat(m_full_path, m_data_name); + m_full_path[SS_MAX_UUID_LEN] = '\0'; + remaining -= SS_MAX_UUID_LEN; + + strncat(m_full_path, "/", 1); + remaining -= 1; + + strncat(m_full_path, m_data_name, remaining - 1); m_file_path_ready = true; return SS_RET_SUCCESS; #else @@ -520,10 +520,8 @@ int secure_file::derive_file_path() { // obtain first part of our string byte_to_hex(pFolderName, pHash, CCryptoEngine::Hash_Size); memcpy(sTemp, pFolderName, 2 * CCryptoEngine::Hash_Size); - //sTemp.assign((char*)pFolderName, 2*CCryptoEngine::Hash_Size); // concatenate with UUID and Name - //sTemp += sUUID_and_Name; memcpy(sTemp + 2 * CCryptoEngine::Hash_Size, sUUID_and_Name, strlen(sUUID_and_Name)); @@ -532,14 +530,6 @@ int secure_file::derive_file_path() { CCryptoEngine::Hash(pHash, (CBT_OCTET*)sTemp, strlen(sTemp)); // we will use first 4 bytes of hash value - // convert them into hex format - //byte_to_hex(pFolderName, pHash, 4); - - // set folder name - //sfolder.assign((char*)pFolderName, 8); - //sfolder += "/"; - - //m_full_path = sfolder; memset(m_full_path, 0, SS_FULL_DATA_NAME_LEN); memcpy(m_full_path, pHash, 4); @@ -547,8 +537,6 @@ int secure_file::derive_file_path() { byte_to_hex(dir, pHash, 4); SLOGI("Dir is %s.", (char*)dir); - //m_full_path[8] = '/'; - if (0 != strlen(m_data_name)) { // computing file name uint64_t data_id = transform_id_to_name(transform_name_to_id(m_data_name)); @@ -809,7 +797,6 @@ int secure_file::parse_file_content(unsigned char* buffer, switch (FileStructureType(ptr)) { case 0: { - // [header][hash][data][key] m_file_content.m_pFileHeader = ptr; ptr += HEADER_SIZE; m_file_content.m_pHashMaterial = ptr; @@ -821,7 +808,6 @@ int secure_file::parse_file_content(unsigned char* buffer, break; } case 1: { - // [header][hash][key][data] m_file_content.m_pFileHeader = ptr; ptr += HEADER_SIZE; m_file_content.m_pHashMaterial = ptr; @@ -833,7 +819,6 @@ int secure_file::parse_file_content(unsigned char* buffer, break; } case 2: { - // [header][data][key][hash] m_file_content.m_pFileHeader = ptr; ptr += HEADER_SIZE; m_file_content.m_pFileContent = ptr; @@ -845,7 +830,6 @@ int secure_file::parse_file_content(unsigned char* buffer, break; } case 3: { - // [header][key][data][hash] m_file_content.m_pFileHeader = ptr; ptr += HEADER_SIZE; m_file_content.m_pKeyMaterial = ptr; @@ -857,7 +841,6 @@ int secure_file::parse_file_content(unsigned char* buffer, break; } case 4: { - // [header][key][hash][data] m_file_content.m_pFileHeader = ptr; ptr += HEADER_SIZE; m_file_content.m_pKeyMaterial = ptr; @@ -869,7 +852,6 @@ int secure_file::parse_file_content(unsigned char* buffer, break; } case 5: { - // [header][data][hash][key] m_file_content.m_pFileHeader = ptr; ptr += HEADER_SIZE; m_file_content.m_pFileContent = ptr; @@ -944,7 +926,6 @@ int secure_file::serialize_data(unsigned char** buffer, #ifdef _SECOS_SIM_ *buffer = (unsigned char*)OsaMalloc(m_write_data_size); if (NULL == *buffer) { - //SLOGE("fail to alloc memory for data."); return SS_RET_MALLOC_FAILED; } @@ -975,7 +956,6 @@ int secure_file::serialize_data(unsigned char** buffer, unsigned char* ptr = data; switch (FileStructureType(m_file_content.m_pFileHeader)) { case 0: { - // [header][hash][data][key] memcpy(ptr, m_file_content.m_pFileHeader, HEADER_SIZE); ptr += HEADER_SIZE; memcpy(ptr, m_file_content.m_pHashMaterial, HASH_SIZE); @@ -989,7 +969,6 @@ int secure_file::serialize_data(unsigned char** buffer, break; } case 1: { - // [header][hash][key][data] memcpy(ptr, m_file_content.m_pFileHeader, HEADER_SIZE); ptr += HEADER_SIZE; memcpy(ptr, m_file_content.m_pHashMaterial, HASH_SIZE); @@ -1003,7 +982,6 @@ int secure_file::serialize_data(unsigned char** buffer, break; } case 2: { - // [header][data][key][hash] memcpy(ptr, m_file_content.m_pFileHeader, HEADER_SIZE); ptr += HEADER_SIZE; memcpy(ptr, @@ -1017,7 +995,6 @@ int secure_file::serialize_data(unsigned char** buffer, break; } case 3: { - // [header][key][data][hash] memcpy(ptr, m_file_content.m_pFileHeader, HEADER_SIZE); ptr += HEADER_SIZE; memcpy(ptr, m_file_content.m_pKeyMaterial, KEY_MAT_SIZE); @@ -1031,7 +1008,6 @@ int secure_file::serialize_data(unsigned char** buffer, break; } case 4: { - // [header][key][hash][data] memcpy(ptr, m_file_content.m_pFileHeader, HEADER_SIZE); ptr += HEADER_SIZE; memcpy(ptr, m_file_content.m_pKeyMaterial, KEY_MAT_SIZE); @@ -1045,7 +1021,6 @@ int secure_file::serialize_data(unsigned char** buffer, break; } case 5: { - // [header][data][hash][key] memcpy(ptr, m_file_content.m_pFileHeader, HEADER_SIZE); ptr += HEADER_SIZE; memcpy(ptr, diff --git a/ssflib/dep/swdss/source/ss_crypto.cpp b/ssflib/dep/swdss/source/ss_crypto.cpp index d4c5e99..3b8bb97 100644 --- a/ssflib/dep/swdss/source/ss_crypto.cpp +++ b/ssflib/dep/swdss/source/ss_crypto.cpp @@ -40,8 +40,6 @@ void gen_rand_vec(CBT_OCTET* vec, CBT_UINT32 size) { */ int CCryptoEngine::Encrypt(uint8_t* dest, uint8_t* src, unsigned long data_len, const uint8_t* key, unsigned long key_type) { - //memcpy(dest,src,data_len); - //return data_len; unsigned int cipherTextLen, t; CryptoCoreContainer *crt = create_CryptoCoreContainer(ID_AES); diff --git a/ssflib/dep/uci/source/uci_api.c b/ssflib/dep/uci/source/uci_api.c index a21522c..2873e7d 100644 --- a/ssflib/dep/uci/source/uci_api.c +++ b/ssflib/dep/uci/source/uci_api.c @@ -28,7 +28,6 @@ #include "uci_cryptocore.h" #include "uci_internal.h" #include "uci_hwcrypto.h" -//#include "ae.h" #include "CC_Context.h" #include "uci_aes_xcbc_mac.h" diff --git a/ssflib/dep/uci/source/uci_cryptocore.c b/ssflib/dep/uci/source/uci_cryptocore.c index 6a87eb1..7aed80a 100644 --- a/ssflib/dep/uci/source/uci_cryptocore.c +++ b/ssflib/dep/uci/source/uci_cryptocore.c @@ -844,8 +844,6 @@ int cryptocore_ae_decryptbycrt(UCI_HANDLE oh, unsigned char *input, if (pctx->config != UCI_SW) return UCI_INVALID_HANDLE; - // ctr=(CryptoCoreContainer *)(pctx->imp); - // ctr->MD_update(ctr,msg,msg_len); ret = ((CryptoCoreContainer *)pctx->imp)->AE_decryptByCRT( ((CryptoCoreContainer *)pctx->imp), input, input_len, output, output_len); diff --git a/ssflib/dep/uci/source/uci_hwcrypto.c b/ssflib/dep/uci/source/uci_hwcrypto.c index adcf86e..7642d97 100644 --- a/ssflib/dep/uci/source/uci_hwcrypto.c +++ b/ssflib/dep/uci/source/uci_hwcrypto.c @@ -457,9 +457,7 @@ int hwcrypto_se_init(UCI_HANDLE oh, unsigned int mode, unsigned int padding, else memset(info->iv, 0x0, info->ivlen); - pctx->handle = open("/dev/crypto", 0, 0); //return hndl; - - //TA_PRINT("hand = %d \n",pctx->handle); + pctx->handle = open("/dev/crypto", 0, 0); if (pctx->handle < 0) return UCI_ERROR; @@ -476,22 +474,22 @@ int hwcrypto_se_init(UCI_HANDLE oh, unsigned int mode, unsigned int padding, uci_context_s *pctx = (uci_context_s *)oh; unsigned int keytype; unsigned int alg; - //!AS current hw is not ready, so using SW pseduo way temproray. + unsigned char hwkey_master[32] = { 0xAB, 0x12, 0x45, 0x67, 0x3F, 0x80, 0x98, 0x35, - 0x06, 0x4F, 0x33, 0x39, 0x72, 0x1C, 0xDF, 0x23, - 0xAB, 0x12, 0x45, 0x67, 0x3F, 0x80, 0x98, 0x35, - 0x06, 0x4F, 0x33, 0x39, 0x72, 0x1C, 0xDF, 0x23 - }; + 0x06, 0x4F, 0x33, 0x39, 0x72, 0x1C, 0xDF, 0x23, + 0xAB, 0x12, 0x45, 0x67, 0x3F, 0x80, 0x98, 0x35, + 0x06, 0x4F, 0x33, 0x39, 0x72, 0x1C, 0xDF, 0x23 + }; unsigned char hwiv_master[16] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, - 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF + 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF }; unsigned char hwkey_unique[32] = { 0xF0, 0x22, 0x34, 0x67, 0x66, 0x88, 0xAB, 0xCD, - 0x12, 0x67, 0x89, 0x54, 0x32, 0x10, 0xCC, 0xFE, - 0xAB, 0x12, 0x45, 0x67, 0x3F, 0x80, 0x98, 0x35, - 0x06, 0x4F, 0x33, 0x39, 0x72, 0x1C, 0xDF, 0x23 - }; + 0x12, 0x67, 0x89, 0x54, 0x32, 0x10, 0xCC, 0xFE, + 0xAB, 0x12, 0x45, 0x67, 0x3F, 0x80, 0x98, 0x35, + 0x06, 0x4F, 0x33, 0x39, 0x72, 0x1C, 0xDF, 0x23 + }; unsigned char hwiv_unique[16] = { 0xFF, 0xEE, 0xDD, 0xCC, 0xBB, 0xAA, 0x99, 0x88, - 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11, 0x00 + 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11, 0x00 }; if (pctx == NULL) @@ -715,8 +713,6 @@ int hwcrypto_se_final(UCI_HANDLE oh, unsigned char *input, oper.src_len = len; oper.dst_addr = (char *)output; oper.dst_len = output_len; - - //oper.final = 1; if (ret = ioctl(hndl, IOCTL_CRYPTO_CRYPT, &oper)) { TA_PRINT("error:ioctl(hndl, 1, &oper) returned %d\n", ret); return UCI_ERROR; @@ -737,9 +733,6 @@ int hwcrypto_se_final(UCI_HANDLE oh, unsigned char *input, } padlen = output[input_len - 1]; - - //PrintBYTE("padding",output,input_len); - //PrintBYTE("input",input,input_len); if (padlen < 1 || padlen > 16) { *output_len = 0; TA_PRINT("padding size{%d} is incorretc ", padlen); diff --git a/ssflib/inc/app_debug.h b/ssflib/inc/app_debug.h index f363ba3..266991c 100644 --- a/ssflib/inc/app_debug.h +++ b/ssflib/inc/app_debug.h @@ -67,7 +67,6 @@ unsigned char one_time_print_buffer_test[10240]; #define TURST_APP_WRN(fmt, ...) if (g_app_svc_dbglvl >= TRUSTAPP_DEBUG_LEVEL_WRN) {APP_SVC_WRN(APP_MODULE_NAME, fmt, ##__VA_ARGS__)} #define TURST_APP_DBG(fmt, ...) if (g_app_svc_dbglvl >= TRUSTAPP_DEBUG_LEVEL_DBG) {APP_SVC_DBG(APP_MODULE_NAME, fmt, ##__VA_ARGS__)} #define TURST_APP_LOG(fmt, ...) if (g_app_svc_dbglvl >= TRUSTAPP_DEBUG_LEVEL_LOG) {APP_SVC_LOG(APP_MODULE_NAME, fmt, ##__VA_ARGS__)} -//#define TURST_APP_LOG(fmt, ...) TURST_APP_LOG_TEST(fmt,##__VA_ARGS__) diff --git a/ssflib/src/app_debug.cpp b/ssflib/src/app_debug.cpp index d169936..525accd 100644 --- a/ssflib/src/app_debug.cpp +++ b/ssflib/src/app_debug.cpp @@ -24,7 +24,6 @@ #include #include -//#define PRINT_LOG_TO_CONSOLE #ifdef PRINT_LOG_TO_CONSOLE #include #define portname "/dev/ttyS0" diff --git a/ssflib/src/ssf_client.cpp b/ssflib/src/ssf_client.cpp index 00b07e4..2c89207 100644 --- a/ssflib/src/ssf_client.cpp +++ b/ssflib/src/ssf_client.cpp @@ -36,7 +36,6 @@ *-----------------------------------------------------------------------------*/ #define SOCKPATH "/tmp/simdaemon" //path to be updated -//#define TEST /*----------------------------------------------------------------------------- * local functions diff --git a/ssflib/src/ssf_crypto.cpp b/ssflib/src/ssf_crypto.cpp index 4020cdf..21ddf54 100644 --- a/ssflib/src/ssf_crypto.cpp +++ b/ssflib/src/ssf_crypto.cpp @@ -31,6 +31,8 @@ #include #include #include +#include +#include #include "CC_API.h" #include "ssf_crypto_openssl.h" @@ -471,12 +473,6 @@ static int sw_crypto_ioctl_init(crypto_internal_operation *operation, crypto_int key->rsa_exponent2.buffer, &key->rsa_exponent2.size, key->rsa_coefficient.buffer, &key->rsa_coefficient.size); - /*if(rc == (-ETIMEDOUT)) - { - LOGE(SSF_LIB, "Algorithm - %X : TIMEOUT \n", operation->info.algorithm); - rc = TEE_ERROR_TIMEOUT; - }*/ - memcpy(key->rsa_public.buffer, E, ELen); key->rsa_public.size = ELen; } @@ -2250,35 +2246,20 @@ TEE_Result TEE_AEInit(TEE_OperationHandle operation, void* nonce, size_t nonceLe CRYPTO_PANIC; } // tagLen check + std::array values_GCM = {128, 120, 112, 104, 96}; + std::array values_CCM = {128, 112, 96, 64, 48, 32}; switch (op->info.algorithm) { case TEE_ALG_AES_GCM: { - switch (tagLen) { - case 128: - case 120: - case 112: - case 104: - case 96: - break; - default: - LOGE(SSF_LIB, "Incorrect tag length %u", tagLen); - return TEE_ERROR_NOT_SUPPORTED; - }; - break; + if (std::find(values_GCM.begin(), values_GCM.end(), tagLen) == values_GCM.end()) { + LOGE(SSF_LIB, "Incorrect tag length %u", tagLen); + return TEE_ERROR_NOT_SUPPORTED; + } } case TEE_ALG_AES_CCM: { - switch (tagLen) { - case 128: - case 112: - case 96: - case 64: - case 48: - case 32: - break; - default: - LOGE(SSF_LIB, "Incorrect tag length %u", tagLen); - return TEE_ERROR_NOT_SUPPORTED; - }; - break; + if (std::find(values_CCM.begin(), values_CCM.end(), tagLen) == values_CCM.end()) { + LOGE(SSF_LIB, "Incorrect tag length %u", tagLen); + return TEE_ERROR_NOT_SUPPORTED; + } } default: { LOGE(SSF_LIB, "Incorrect algorithm %x", op->info.algorithm); diff --git a/ssflib/src/ssf_storage.cpp b/ssflib/src/ssf_storage.cpp index 1b71c8e..1c92a21 100644 --- a/ssflib/src/ssf_storage.cpp +++ b/ssflib/src/ssf_storage.cpp @@ -151,67 +151,52 @@ TEE_Result allocate_transient_object(TransientObject* tr, uint32_t objectType, //tr->attr.buf_len = (maxObjectSize + 7)>>3; break; case TEE_TYPE_DES: - //if (maxObjectSize != 64) { - // return TEE_ERROR_NOT_SUPPORTED; - //} - //tr->attr.buf_len = (maxObjectSize + 7)>>3; break; case TEE_TYPE_DES3: if (maxObjectSize != 128 && maxObjectSize != 192) return TEE_ERROR_NOT_SUPPORTED; - //tr->attr.buf_len = (maxObjectSize + 7)>>3; break; case TEE_TYPE_HMAC_MD5: if (maxObjectSize < 64 || maxObjectSize > 512 || maxObjectSize % 8) return TEE_ERROR_NOT_SUPPORTED; - //tr->attr.buf_len = (maxObjectSize + 7)>>3; break; case TEE_TYPE_HMAC_SHA1: if (maxObjectSize < 80 || maxObjectSize > 512 || maxObjectSize % 8) return TEE_ERROR_NOT_SUPPORTED; - //tr->attr.buf_len = (maxObjectSize + 7)>>3; break; case TEE_TYPE_HMAC_SHA224: if (maxObjectSize < 112 || maxObjectSize > 512 || maxObjectSize % 8) return TEE_ERROR_NOT_SUPPORTED; - //tr->attr.buf_len = (maxObjectSize + 7)>>3; break; case TEE_TYPE_HMAC_SHA256: if (maxObjectSize < 192 || maxObjectSize > 1024 || maxObjectSize % 8) return TEE_ERROR_NOT_SUPPORTED; - //tr->attr.buf_len = (maxObjectSize + 7)>>3; break; case TEE_TYPE_HMAC_SHA384: if (maxObjectSize < 256 || maxObjectSize > 1024 || maxObjectSize % 8) return TEE_ERROR_NOT_SUPPORTED; - //tr->attr.buf_len = (maxObjectSize + 7)>>3; break; case TEE_TYPE_HMAC_SHA512: if (maxObjectSize < 256 || maxObjectSize > 1024 || maxObjectSize % 8) return TEE_ERROR_NOT_SUPPORTED; - //tr->attr.buf_len = (maxObjectSize + 7)>>3; break; case TEE_TYPE_RSA_PUBLIC_KEY: case TEE_TYPE_RSA_KEYPAIR: if (maxObjectSize < 256 || maxObjectSize > 4096 || maxObjectSize % 64) return TEE_ERROR_NOT_SUPPORTED; - //tr->attr.buf_len = sizeof(rsa_context); break; case TEE_TYPE_DSA_PUBLIC_KEY: case TEE_TYPE_DSA_KEYPAIR: if (maxObjectSize < 512 || maxObjectSize > 1024 || maxObjectSize % 64) return TEE_ERROR_NOT_SUPPORTED; - //tr->attr.buf_len = sizeof(dsa_context); break; case TEE_TYPE_DH_KEYPAIR: if (maxObjectSize < 256 || maxObjectSize > 2048) return TEE_ERROR_NOT_SUPPORTED; - //tr->attr.buf_len = sizeof(dh_context); break; case TEE_TYPE_GENERIC_SECRET: if (maxObjectSize > 4096 || maxObjectSize % 8) return TEE_ERROR_NOT_SUPPORTED; - //tr->attr.buf_len = (maxObjectSize + 7)>>3; break; default: return TEE_ERROR_NOT_SUPPORTED; @@ -221,9 +206,6 @@ TEE_Result allocate_transient_object(TransientObject* tr, uint32_t objectType, tr->info.objectType = objectType; tr->info.objectSize = 0; tr->info.maxObjectSize = maxObjectSize; - //tr->info.dataSize = 0; - //tr->info.dataPosition = 0; - //tr->info.handleFlags = 0; tr->info.objectUsage = 0xffffffff; return TEE_SUCCESS; } @@ -1245,7 +1227,6 @@ void add_to_po_list(persistent_object* po) { g_po_list.next = &po->po_list; } MSG("=====PO %s added=====", po->po_file.file_name); - //debug_list(); } void rem_from_po_list(persistent_object* po) { @@ -1253,7 +1234,6 @@ void rem_from_po_list(persistent_object* po) { return; } MSG("=====To remove PO %s=====", po->po_file.file_name); - //debug_list(); if (po->po_list.prev) { po->po_list.prev->next = po->po_list.next; } @@ -1261,7 +1241,6 @@ void rem_from_po_list(persistent_object* po) { po->po_list.next->prev = po->po_list.prev; } MSG("======PO removed====="); - //debug_list(); } po_user* get_po_user_from_po_list(uint32_t storageID, const void* objectID, -- 2.7.4