refactor: cpp improvements (#114)

* build: use cpp standard if not set

* fix: remove more compiler warnigns

* chore: initial usage of shared pointers

* chore: remove dupplicate code

* chore: use cose_free

* chore: less warnings

* chore: less warnings

* chore: remove auto

* chore: only use context if needed
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 645e8e0..1b00bc5 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -98,6 +98,10 @@
   set(CMAKE_C_STANDARD 11)
 endif()
 
+if(NOT CMAKE_CXX_STANDARD)
+  set(CMAKE_CXX_STANDARD 11)
+endif()
+
 if(NOT CMAKE_BUILD_TYPE)
   if(COSE_C_OPTIMIZE)
     set(CMAKE_BUILD_TYPE MinSizeRel)
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index ee0444e..18788e6 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -39,6 +39,7 @@
     cose_crypto.h
     cose_int.h
     Cose.cpp
+    utils.hpp
     CoseKey.cpp
     CounterSign.cpp
     CounterSign0.cpp
diff --git a/src/Cose.cpp b/src/Cose.cpp
index c6bb91e..9260069 100644
--- a/src/Cose.cpp
+++ b/src/Cose.cpp
@@ -138,13 +138,12 @@
 	cn_cbor *pCounter =
 		cn_cbor_mapget_int(pobj->m_unprotectMap, COSE_Header_CounterSign);
 	if (pCounter != NULL) {
-		int i;
 		CHECK_CONDITION(
 			pCounter->type == CN_CBOR_ARRAY, COSE_ERR_INVALID_PARAMETER);
 		CHECK_CONDITION(pCounter->length > 0, COSE_ERR_INVALID_PARAMETER);
 		if (pCounter->first_child->type == CN_CBOR_ARRAY) {
 			cn_cbor *pSig = pCounter->first_child;
-			for (i = 0; i < pCounter->length; i++, pSig = pSig->next) {
+			for (size_t i = 0; i < pCounter->length; i++, pSig = pSig->next) {
 				COSE_CounterSign *cs = _COSE_CounterSign_Init_From_Object(
 					pSig, NULL, CBOR_CONTEXT_PARAM_COMMA perr);
 				cs->m_next = pobj->m_counterSigners;
@@ -552,7 +551,6 @@
 
 	newMsg->m_handleList = *rootNode;
 	*rootNode = newMsg;
-	return;
 }
 
 bool _COSE_IsInList(const COSE *const rootNode, const COSE *const thisMsg)
@@ -588,7 +586,6 @@
 			return;
 		}
 	}
-	return;
 }
 
 #ifndef NDEBUG
diff --git a/src/Recipient.cpp b/src/Recipient.cpp
index 4a4eca3..3714f32 100644
--- a/src/Recipient.cpp
+++ b/src/Recipient.cpp
@@ -159,8 +159,6 @@
 	}
 
 	COSE_FREE(pRecipient, &pRecipient->m_encrypt.m_message.m_allocContext);
-
-	return;
 }
 #endif
 
diff --git a/src/Sign1.cpp b/src/Sign1.cpp
index 2846291..87b8f75 100644
--- a/src/Sign1.cpp
+++ b/src/Sign1.cpp
@@ -363,7 +363,7 @@
 static bool CreateSign1AAD(COSE_Sign1Message *pMessage,
 	byte **ppbToSign,
 	size_t *pcbToSign,
-	char *szContext,
+	const char *szContext,
 	cose_errback *perr)
 {
 	cn_cbor *pArray = NULL;
diff --git a/src/cose_int.h b/src/cose_int.h
index fce2352..802705d 100644
--- a/src/cose_int.h
+++ b/src/cose_int.h
@@ -17,6 +17,7 @@
 typedef struct CounterSign1 COSE_CounterSign1;
 
 #define UNUSED(x) ((void)(x))
+#define COSE_MIN(A, B) ((A) < (B) ? (A) : (B))
 
 #ifndef _countof
 #define _countof(x) (sizeof(x) / sizeof(x[0]))
diff --git a/src/mbedtls.cpp b/src/mbedtls.cpp
index 6a3655b..bb87940 100644
--- a/src/mbedtls.cpp
+++ b/src/mbedtls.cpp
@@ -24,8 +24,6 @@
 
 static bool FUseCompressed = true;
 
-#define MIN(A, B) ((A) < (B) ? (A) : (B))
-
 #ifdef INCLUDE_AES_CCM
 bool AES_CCM_Decrypt(COSE_Enveloped *pcose,
 	int TSize,
@@ -41,7 +39,7 @@
 	mbedtls_ccm_context ctx;
 	int cbOut = 0;
 	byte *rgbOut = NULL;
-	int NSize = 15 - (LSize / 8);
+	size_t NSize = 15 - (LSize / 8);
 	byte rgbIV[15] = {0};
 	const cn_cbor *pIV = NULL;
 	mbedtls_cipher_id_t cipher;
@@ -103,7 +101,7 @@
 	mbedtls_ccm_context ctx;
 	int cbOut;
 	byte *rgbOut = NULL;
-	int NSize = 15 - (LSize / 8);
+	size_t NSize = 15 - (LSize / 8);
 	const cn_cbor *cbor_iv = NULL;
 	cn_cbor *cbor_iv_t = NULL;
 #ifdef USE_CBOR_CONTEXT
diff --git a/src/openssl.cpp b/src/openssl.cpp
index 9883908..129e9ec 100644
--- a/src/openssl.cpp
+++ b/src/openssl.cpp
@@ -24,8 +24,6 @@
 
 static bool FUseCompressed = true;
 
-#define MIN(A, B) ((A) < (B) ? (A) : (B))
-
 #if (OPENSSL_VERSION_NUMBER < 0x10100000)
 
 HMAC_CTX *HMAC_CTX_new()
@@ -77,7 +75,7 @@
 	EVP_CIPHER_CTX *ctx;
 	int cbOut;
 	byte *rgbOut = NULL;
-	int NSize = 15 - (LSize / 8);
+	size_t NSize = 15 - (LSize / 8);
 	int outl = 0;
 	byte rgbIV[15] = {0};
 	const cn_cbor *pIV = NULL;
@@ -179,7 +177,7 @@
 	EVP_CIPHER_CTX *ctx;
 	int cbOut;
 	byte *rgbOut = NULL;
-	int NSize = 15 - (LSize / 8);
+	size_t NSize = 15 - (LSize / 8);
 	int outl = 0;
 	const cn_cbor *cbor_iv = NULL;
 	cn_cbor *cbor_iv_t = NULL;
@@ -795,7 +793,7 @@
 			COSE_ERR_CRYPTO_FAIL);
 		for (ib2 = 0; ib2 < cbInfo; ib2 += 16) {
 			CHECK_CONDITION(EVP_EncryptUpdate(ctx, rgbOut, &cbOut, pbInfo + ib2,
-								(int)MIN(16, cbInfo - ib2)),
+								(int)COSE_MIN(16, cbInfo - ib2)),
 				COSE_ERR_CRYPTO_FAIL);
 		}
 		CHECK_CONDITION(EVP_EncryptUpdate(ctx, rgbOut, &cbOut, &bCount, 1),
@@ -807,7 +805,7 @@
 		}
 		memcpy(rgbDigest, rgbOut, cbOut);
 		cbDigest = cbOut;
-		memcpy(pbOutput + ib, rgbDigest, MIN(16, cbOutput - ib));
+		memcpy(pbOutput + ib, rgbDigest, COSE_MIN(16, cbOutput - ib));
 	}
 
 	EVP_CIPHER_CTX_free(ctx);
@@ -935,7 +933,7 @@
 		CHECK_CONDITION(
 			HMAC_Final(ctx, rgbDigest, &cbDigest), COSE_ERR_CRYPTO_FAIL);
 
-		memcpy(pbOutput + ib, rgbDigest, MIN(cbDigest, cbOutput - ib));
+		memcpy(pbOutput + ib, rgbDigest, COSE_MIN(cbDigest, cbOutput - ib));
 	}
 
 	HMAC_CTX_free(ctx);
@@ -1064,7 +1062,7 @@
 	cn_cbor *cn = _COSE_arrayget_int(&pcose->m_message, INDEX_MAC_TAG);
 	CHECK_CONDITION(cn != NULL, COSE_ERR_CBOR);
 
-	if (cn->length > (int)cbOut) {
+	if (cn->length > cbOut) {
 		return false;
 	}
 	for (unsigned int i = 0; i < (unsigned int)TSize / 8; i++) {
@@ -1139,7 +1137,7 @@
 	p = cn_cbor_mapget_int(pKey->m_cborKey, COSE_Key_EC_X);
 	CHECK_CONDITION(
 		(p != NULL) && (p->type == CN_CBOR_BYTES), COSE_ERR_INVALID_PARAMETER);
-	CHECK_CONDITION(p->length == *cbGroup, COSE_ERR_INVALID_PARAMETER);
+	CHECK_CONDITION(p->length == (size_t)*cbGroup, COSE_ERR_INVALID_PARAMETER);
 	memcpy(rgbKey + 1, p->v.str, p->length);
 
 	p = cn_cbor_mapget_int(pKey->m_cborKey, COSE_Key_EC_Y);
@@ -1147,7 +1145,7 @@
 	if (p->type == CN_CBOR_BYTES) {
 		rgbKey[0] = POINT_CONVERSION_UNCOMPRESSED;
 		cbKey = (*cbGroup * 2) + 1;
-		CHECK_CONDITION(p->length == *cbGroup, COSE_ERR_INVALID_PARAMETER);
+		CHECK_CONDITION(p->length == (size_t)*cbGroup, COSE_ERR_INVALID_PARAMETER);
 		memcpy(rgbKey + p->length + 1, p->v.str, p->length);
 	}
 	else if (p->type == CN_CBOR_TRUE) {
diff --git a/src/utils.hpp b/src/utils.hpp
new file mode 100644
index 0000000..aa80487
--- /dev/null
+++ b/src/utils.hpp
@@ -0,0 +1,25 @@
+#pragma once
+
+#include <memory>
+
+#include "cose/cose.h"
+
+namespace cose {
+
+template <typename T>
+inline std::shared_ptr<T> make_managed_array(size_t size)
+{
+	return std::shared_ptr<T>(new T[size], [](T* p) { delete[] p; });
+}
+
+#ifdef USE_CBOR_CONTEXT
+template <typename T>
+inline std::shared_ptr<T> make_managed_array(size_t size,
+	cn_cbor_context* context)
+{
+	return std::shared_ptr<T>(
+		COSE_CALLOC(1, size, context), [=](T* p) { COSE_FREE(p, context); });
+}
+#endif
+
+}  // namespace cose
\ No newline at end of file
diff --git a/test/Encrypt_test.cpp b/test/Encrypt_test.cpp
index c40fbfd..312a33e 100644
--- a/test/Encrypt_test.cpp
+++ b/test/Encrypt_test.cpp
@@ -1,5 +1,3 @@
-//  encrypt.c
-
 #define _CRT_SECURE_NO_WARNINGS
 
 #include <stdio.h>
@@ -16,6 +14,9 @@
 #include "test.h"
 #include "context.h"
 #include "cose_int.h"
+#include "utils.hpp"
+
+using namespace cose;
 
 #ifdef _MSC_VER
 #pragma warning(disable : 4127)
@@ -53,9 +54,9 @@
 			COSE_Recipient_Free(hRecip2);
 		}
 
-		return fRet;		
+		return fRet;
 	}
-	
+
 	hEnc = (HCOSE_ENVELOPED)COSE_Decode(pbEncoded, cbEncoded, &type,
 		COSE_enveloped_object, CBOR_CONTEXT_PARAM_COMMA & cose_err);
 	if (hEnc == NULL) {
@@ -610,13 +611,13 @@
 	}
 
 	size_t cb = COSE_Encode((HCOSE)hEncObj, NULL, 0, 0) + 1;
-	byte *rgb = (byte *)malloc(cb);
-	cb = COSE_Encode((HCOSE)hEncObj, rgb, 0, cb);
+	std::shared_ptr<byte> rgb = make_managed_array<byte>(cb);
+
+	cb = COSE_Encode((HCOSE)hEncObj, rgb.get(), 0, cb);
 
 	COSE_Enveloped_Free(hEncObj);
 
-	int f = _ValidateEnveloped(pControl, rgb, cb);
-	free(rgb);
+	int f = _ValidateEnveloped(pControl, rgb.get(), cb);
 	return f;
 }
 
@@ -756,7 +757,7 @@
 	const cn_cbor *pFail = NULL;
 	const cn_cbor *pEncrypt = NULL;
 	const cn_cbor *pRecipients = NULL;
-	cn_cbor *pkey = NULL;	
+	cn_cbor *pkey = NULL;
 	HCOSE_ENCRYPT hEnc = NULL;
 	int type;
 	bool fFail = false;
@@ -797,7 +798,7 @@
 		}
 
 		CFails += 1;
-		return 0;		
+		return 0;
 	}
 
 	pFail = cn_cbor_mapget_string(pControl, "fail");
@@ -1111,7 +1112,7 @@
 	if (pkey != NULL) {
 		CN_CBOR_FREE(pkey, context);
 	}
-	
+
 	free(rgb);
 	return f;
 }
@@ -1292,8 +1293,6 @@
 
 	COSE_Enveloped_Free(hEncrypt);
 	COSE_Recipient_Free(hRecipient);
-
-	return;
 }
 #endif
 
@@ -1398,8 +1397,6 @@
 	CHECK_FAILURE(COSE_Encrypt_encrypt(hEncrypt, rgb, sizeof(rgb), &cose_error),
 		COSE_ERR_UNKNOWN_ALGORITHM, CFails++);
 	COSE_Encrypt_Free(hEncrypt);
-
-	return;
 }
 #endif
 
diff --git a/test/Mac_test.cpp b/test/Mac_test.cpp
index 8cc265b..c6fdc57 100644
--- a/test/Mac_test.cpp
+++ b/test/Mac_test.cpp
@@ -510,7 +510,7 @@
 {
 	HCOSE_MAC hEncObj =
 		COSE_Mac_Init(COSE_INIT_FLAGS_NONE, CBOR_CONTEXT_PARAM_COMMA NULL);
-	char *sz = "This is the content to be used";
+	const char *sz = "This is the content to be used";
 	byte rgbSecret[256 / 8] = {'a', 'b', 'c'};
 	byte rgbKid[6] = {'a', 'b', 'c', 'd', 'e', 'f'};
 	int cbKid = 6;
@@ -518,7 +518,7 @@
 	byte *rgb = NULL;
 
 	if (hEncObj == NULL) {
-		errorReturn:
+	errorReturn:
 		if (hEncObj != NULL) {
 			COSE_Mac_Free(hEncObj);
 		}
@@ -626,7 +626,7 @@
 	const cn_cbor *pFail = NULL;
 	const cn_cbor *pMac = NULL;
 	const cn_cbor *pRecipients = NULL;
-	cn_cbor *pkey = NULL;	
+	cn_cbor *pkey = NULL;
 	HCOSE_MAC0 hMAC = NULL;
 	int type;
 	bool fFail = false;
@@ -652,9 +652,9 @@
 			COSE_Mac0_Free(hMAC);
 		}
 		CFails += 1;
-		return (fFail || fUnsuportedAlg) ? 0 : 1;		
+		return (fFail || fUnsuportedAlg) ? 0 : 1;
 	}
-	
+
 	pFail = cn_cbor_mapget_string(pControl, "fail");
 	if ((pFail != NULL) && (pFail->type == CN_CBOR_TRUE)) {
 		fFailBody = true;
diff --git a/test/Sign_test.cpp b/test/Sign_test.cpp
index 242c98e..72765d1 100644
--- a/test/Sign_test.cpp
+++ b/test/Sign_test.cpp
@@ -485,7 +485,7 @@
 {
 	HCOSE_SIGN hEncObj =
 		COSE_Sign_Init(COSE_INIT_FLAGS_NONE, CBOR_CONTEXT_PARAM_COMMA NULL);
-	char *sz = "This is the content to be used";
+	const char *sz = "This is the content to be used";
 	size_t cb;
 	byte *rgb;
 
@@ -1089,8 +1089,6 @@
 	else {
 		CFails++;
 	}
-
-	return;
 }
 #endif
 
diff --git a/test/context.cpp b/test/context.cpp
index 87a9b2a..a604612 100644
--- a/test/context.cpp
+++ b/test/context.cpp
@@ -140,8 +140,6 @@
 	}
 
 	free(myContext);
-
-	return;
 }
 
 int IsContextEmpty(cn_cbor_context *pContext)