log: printf in one call, add extra info 26/141126/4
authorLukasz Kostyra <l.kostyra@samsung.com>
Tue, 6 Jun 2017 09:40:02 +0000 (11:40 +0200)
committerLukasz Kostyra <l.kostyra@samsung.com>
Thu, 26 Oct 2017 12:29:43 +0000 (14:29 +0200)
Change-Id: I1653832d0f408efbdfa6db628eba20ce10ec26b2

ta/crypto_hash.c [deleted file]
ta/include/log.h
ta/src/cmd_exec.c
ta/src/crypto_hash.c [new file with mode: 0644]
ta/src/internal.c
ta/src/log.c [new file with mode: 0644]
ta/src/ta_km.c

diff --git a/ta/crypto_hash.c b/ta/crypto_hash.c
deleted file mode 100644 (file)
index c9cc854..0000000
+++ /dev/null
@@ -1,39 +0,0 @@
-/*
- *  Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
- *
- *  Licensed under the Apache License, Version 2.0 (the "License");
- *  you may not use this file except in compliance with the License.
- *  You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS,
- *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License
- */
-/*
- * @file        crypto_hash.c
- * @author      Lukasz Kostyra (l.kostyra@samsung.com)
- * @version     1.0
- * @brief       Implementation of Global Platform Internal API usage (hashing)
- */
-
-#include <crypto_hash.h>
-#include <log.h>
-
-TEE_Result KM_Hash(TEE_OperationHandle hndl, void *iv, uint32_t iv_size,
-                                       void *data, uint32_t data_size, void *out, uint32_t *out_size)
-{
-       TEE_Result ret = TEE_SUCCESS;
-
-       TEE_MACInit(hndl, iv, iv_size);
-
-       ret = TEE_MACComputeFinal(hndl, data, data_size, out, out_size);
-       if (ret != TEE_SUCCESS) {
-               LOG("Failed to compute MAC: %x", ret);
-       }
-
-       return ret;
-}
index addb3e536b8ab516290841ee23b0d31981464077..51dab9e4ce0e0362f411eb48220124d8dffc88ec 100644 (file)
 #define __LOG_H__
 
 #include <stdio.h>
+#include <string.h>
 
-#define LOG(...) do { printf("%s:", __func__); printf(__VA_ARGS__); printf(" \n"); } while(0)
+void log_msg(const char* type, const char* location, int line, const char* func,
+                       const char* format, ...)
+__attribute__((format(printf, 5, 6)));
+
+#define LOG(...) do { \
+       log_msg(" MSG ", __FILE__, __LINE__, __func__, __VA_ARGS__); \
+} while(0)
+
+#ifdef DEBUG
+#define LOGD(...) do { \
+       log_msg("DEBUG", __FILE__, __LINE__, __func__, __VA_ARGS__); \
+} while(0)
+#else
+#define LOGD(...) do { } while(0)
+#endif
 
 #endif // __LOG_H__
index 21d32e408dd0b67e84e6de9ad9eff6ae585c2c53..39dbf34d32e8395c74e31904df80be194d152a17 100644 (file)
@@ -137,24 +137,27 @@ TEE_Result KM_ExecCmdGenerateKeyPwd(TEE_Param param[4])
 
        ret = TEE_AllocateOperation(&op, TEE_ALG_HMAC_SHA1, TEE_MODE_MAC, key_bits_size);
        if (TEE_SUCCESS != ret) {
+               LOG("Failed to allocate HMAC operation");
                goto clean;
        }
 
        ret = KM_Hash(op, iv_data->data, iv_data->data_size, input_data->data,
                                input_data->data_size, hashed, &hashed_size);
+       TEE_FreeOperation(op);
+
        if (TEE_SUCCESS != ret) {
-               TEE_FreeOperation(op);
+               LOG("Failed to hash key");
                goto clean;
        }
 
-       TEE_FreeOperation(op);
-
        ret = KM_CreateKey(type, key_bits_size, 0, 0, &hndl);
        if (TEE_SUCCESS != ret) {
+               LOG("Failed to create new key");
                goto clean;
        }
        ret = KM_SaveKey(hashed, hashed_size, hndl, objId, objId_size);
        if (TEE_SUCCESS != ret) {
+               LOG("Failed to save new key");
                goto clean;
        }
 
@@ -250,7 +253,7 @@ TEE_Result KM_ExecCmdSymmetric(uint32_t commandID, TEE_Param param[4])
        }
 
        out_size -= out_size_pad;
-       LOG("out_size = %u", out_size);
+       LOGD("out_size = %u", out_size);
 
        if (0 != KM_ParamsSerializationInit(param[2].memref.buffer, param[2].memref.size, &input)
                || 0 != KM_ParamsSerializeOutData(input, out, out_size)) {
diff --git a/ta/src/crypto_hash.c b/ta/src/crypto_hash.c
new file mode 100644 (file)
index 0000000..c9cc854
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+ *  Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License
+ */
+/*
+ * @file        crypto_hash.c
+ * @author      Lukasz Kostyra (l.kostyra@samsung.com)
+ * @version     1.0
+ * @brief       Implementation of Global Platform Internal API usage (hashing)
+ */
+
+#include <crypto_hash.h>
+#include <log.h>
+
+TEE_Result KM_Hash(TEE_OperationHandle hndl, void *iv, uint32_t iv_size,
+                                       void *data, uint32_t data_size, void *out, uint32_t *out_size)
+{
+       TEE_Result ret = TEE_SUCCESS;
+
+       TEE_MACInit(hndl, iv, iv_size);
+
+       ret = TEE_MACComputeFinal(hndl, data, data_size, out, out_size);
+       if (ret != TEE_SUCCESS) {
+               LOG("Failed to compute MAC: %x", ret);
+       }
+
+       return ret;
+}
index 7ee90a6a0e228bad3d095847ee276e5796057259..7e1ccc962ba2993bf283c7e3d9d78737571701ae 100644 (file)
@@ -227,6 +227,6 @@ TEE_Result KM_ImportKey(TEE_ObjectHandle hndl, uint32_t tee_key_type, void *buff
 
 void KM_GenerateIV(void *iv, size_t iv_size)
 {
-       LOG("iv: %p iv_size: %u", iv, iv_size);
+       LOGD("iv: %p iv_size: %u", iv, iv_size);
        TEE_GenerateRandom(iv, iv_size);
 }
diff --git a/ta/src/log.c b/ta/src/log.c
new file mode 100644 (file)
index 0000000..4783078
--- /dev/null
@@ -0,0 +1,16 @@
+#include "log.h"
+#include <stdarg.h>
+
+void log_msg(const char* type, const char* location, int line, const char* func,
+                       const char* format, ...)
+{
+       va_list args;
+       const size_t logSize = 512; // increase this limit as needed
+       char logBuffer[logSize];
+
+       va_start(args, format);
+       vsnprintf(logBuffer, logSize, format, args);
+       va_end(args);
+
+       printf("[%s] %s @ %i (%s): %s \n\r", type, location, line, func, logBuffer);
+}
\ No newline at end of file
index 1a665aeccf1c4328cf0ec71c8e3fa5d200035a1a..d8e635a0e716aa210a921d85f034985f4ec5120c 100644 (file)
@@ -60,20 +60,20 @@ TEE_Result TA_InvokeCommandEntryPoint(void *sessionContext, uint32_t commandID,
 
        switch (commandID) {
        case CMD_GENERATE_KEY:
-               LOG("!!! Generate key !!!");
+               LOGD("!!! Generate key !!!");
                ret = KM_ExecCmdGenerateKey(param);
                break;
        case CMD_GENERATE_IV:
-               LOG("!!! Generate IV !!!");
+               LOGD("!!! Generate IV !!!");
                KM_GenerateIV(param[0].memref.buffer, param[0].memref.size);
                break;
        case CMD_GENERATE_KEY_PWD:
-               LOG("!!! Generate key from PWD !!!");
+               LOGD("!!! Generate key from PWD !!!");
                ret = KM_ExecCmdGenerateKeyPwd(param);
                break;
        case CMD_ENCRYPT:
        case CMD_DECRYPT:
-               LOG("!!! %scrypt !!!", (commandID == CMD_ENCRYPT) ? "En" : "De");
+               LOGD("!!! %scrypt !!!", (commandID == CMD_ENCRYPT) ? "En" : "De");
                if (ALGO_AES_CTR != param[0].value.a &&
                        ALGO_AES_CBC != param[0].value.a &&
                        ALGO_AES_GCM != param[0].value.a &&
@@ -92,6 +92,7 @@ TEE_Result TA_InvokeCommandEntryPoint(void *sessionContext, uint32_t commandID,
        default:
                LOG("Unknown commandID=%d.", commandID);
                ret = TEE_ERROR_BAD_PARAMETERS;
+               break;
        }
 
        return ret;