+++ /dev/null
-/*
- * 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;
-}
#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__
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;
}
}
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)) {
--- /dev/null
+/*
+ * 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;
+}
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);
}
--- /dev/null
+#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
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 &&
default:
LOG("Unknown commandID=%d.", commandID);
ret = TEE_ERROR_BAD_PARAMETERS;
+ break;
}
return ret;