From 883bbabf156932fd18cb7a2ad383cba454508703 Mon Sep 17 00:00:00 2001 From: Konrad Lipinski Date: Tue, 24 Sep 2019 16:36:36 +0200 Subject: [PATCH] Refactor RawBuffer hex dumps Change-Id: I2d52c63c908e3a69c8de5f20e275fecda0165a66 --- src/include/ckm/ckm-raw-buffer.h | 20 +++++++++++++++++++- src/manager/crypto/tz-backend/tz-context.cpp | 13 +------------ src/manager/dpl/db/src/sql_connection.cpp | 13 ++----------- tests/test_common.cpp | 14 ++------------ 4 files changed, 24 insertions(+), 36 deletions(-) diff --git a/src/include/ckm/ckm-raw-buffer.h b/src/include/ckm/ckm-raw-buffer.h index d9b41a7..c4c2a06 100644 --- a/src/include/ckm/ckm-raw-buffer.h +++ b/src/include/ckm/ckm-raw-buffer.h @@ -1,4 +1,4 @@ -/* Copyright (c) 2014 Samsung Electronics Co. +/* Copyright (c) 2014-2019 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. @@ -113,6 +113,24 @@ struct SafeBuffer { // used to pass password and raw key data typedef SafeBuffer::Type RawBuffer; +template +T hexDump(const RawBuffer &raw) { + T dump; + dump.reserve(2 * raw.size()); + constexpr char digit[2][16] = {{ + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', + 'a', 'b', 'c', 'd', 'e', 'f' + }, { + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', + 'A', 'B', 'C', 'D', 'E', 'F' + }}; + for (auto &e : raw) { + dump.push_back(digit[Uppercase][e / 16]); + dump.push_back(digit[Uppercase][e % 16]); + } + return dump; +} + } // namespace CKM #endif //_SAFE_BUFFER_H_ diff --git a/src/manager/crypto/tz-backend/tz-context.cpp b/src/manager/crypto/tz-backend/tz-context.cpp index 9dcd13f..df421f7 100644 --- a/src/manager/crypto/tz-backend/tz-context.cpp +++ b/src/manager/crypto/tz-backend/tz-context.cpp @@ -32,8 +32,6 @@ #include #include #include -#include -#include #include namespace CKM { @@ -56,16 +54,7 @@ const TEEC_UUID KEY_MANAGER_TA_UUID = KM_TA_UUID; //raw to hex string conversion to print persistent storage data ID static std::string rawToHexString(const RawBuffer &raw) { - std::string dump; - - for (auto &e : raw) { - char buf[3]; - snprintf(buf, sizeof(buf), "%02x", (e & 0xff)); - dump.push_back(buf[0]); - dump.push_back(buf[1]); - } - - return dump; + return hexDump(raw); } /* diff --git a/src/manager/dpl/db/src/sql_connection.cpp b/src/manager/dpl/db/src/sql_connection.cpp index 902c94d..15e3cc8 100644 --- a/src/manager/dpl/db/src/sql_connection.cpp +++ b/src/manager/dpl/db/src/sql_connection.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2014-2019 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. @@ -678,16 +678,7 @@ const std::size_t SQLCIPHER_RAW_DATA_SIZE = 32; RawBuffer rawToHexString(const RawBuffer &raw) { - RawBuffer output; - - for (auto &e : raw) { - char result[3]; - snprintf(result, sizeof(result), "%02X", (e & 0xff)); - output.push_back(static_cast(result[0])); - output.push_back(static_cast(result[1])); - } - - return output; + return hexDump(raw); } RawBuffer createHexPass(const RawBuffer &rawPass) diff --git a/tests/test_common.cpp b/tests/test_common.cpp index d01b92a..ab51cea 100644 --- a/tests/test_common.cpp +++ b/tests/test_common.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2016-2019 Samsung Electronics Co., Ltd. All rights reserved * * Contact: Kyungwook Tak * @@ -21,7 +21,6 @@ * @brief */ #include -#include #include #include @@ -61,14 +60,5 @@ RawBuffer createRandom(std::size_t size) //raw to hex string conversion from SqlConnection std::string rawToHexString(const RawBuffer &raw) { - std::string dump; - - for (auto &e : raw) { - char buf[3]; - snprintf(buf, sizeof(buf), "%02x", (e & 0xff)); - dump.push_back(buf[0]); - dump.push_back(buf[1]); - } - - return dump; + return hexDump(raw); } -- 2.7.4