Change to CPP for all of the internal files (#113)

* REname everything and get it to compile

Need to check if this goes past gcc and clang, it does now pass VC.

* Fix round1 of errors

I have a different version of openssl
gcc is strictor on jump to label and initializations that don't matter

* next batch of compiler errors

* Next round of updates

* Next set

* Last one?

* Add back the initializer

* Turn off gcc std option

* Fix tidy errors

* Code review
diff --git a/.travis.yml b/.travis.yml
index 68cecb2..50620d5 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -37,7 +37,7 @@
   - cmake --version
   - git clone --depth 1 git://github.com/cose-wg/Examples Examples
   - mkdir build
-  - cd build && cmake -DCOSE_C_USE_CONTEXT=$USE_CONTEXT -DCOSE_C_USE_MBEDTLS=$USE_EMBEDTLS $CMAKE_OPTIONS $COVERALLS .. && make all test
+  - cd build && cmake -DCOSE_C_USE_CONTEXT=$USE_CONTEXT -DCOSE_C_USE_MBEDTLS=$USE_EMBEDTLS $CMAKE_OPTIONS $COVERALLS .. && make -k all test
 
 after_success:
   - make coveralls
diff --git a/include/cose/cose.h b/include/cose/cose.h
index 03a5870..57cb417 100644
--- a/include/cose/cose.h
+++ b/include/cose/cose.h
@@ -78,7 +78,7 @@
 	COSE_mac_object = 97,
 	COSE_mac0_object = 17,
 	COSE_recipient_object = -1,
-	COSE_countersign_object = -2,
+	COSE_countersign_object = -2
 } COSE_object_type;
 
 //  Generic functions for the COSE library
@@ -156,7 +156,7 @@
 
 	COSE_Algorithm_ECDSA_SHA_256 = -7,
 	COSE_Algorithm_ECDSA_SHA_384 = -35,
-	COSE_Algorithm_ECDSA_SHA_512 = -36,
+	COSE_Algorithm_ECDSA_SHA_512 = -36
 } COSE_Algorithms;
 
 typedef enum {
@@ -185,7 +185,7 @@
 	COSE_Header_ECDH_STATIC = -2,
 	COSE_Header_ECDH_EPK = -1,
 	COSE_Header_ECDH_SPK = -2,
-	COSE_Header_ECDH_SPK_KID = -3,
+	COSE_Header_ECDH_SPK_KID = -3
 
 } COSE_Header;
 
@@ -200,7 +200,7 @@
 	COSE_Key_EC2_X = -2,
 	COSE_Key_EC2_Y = -3,
 	COSE_Key_OPK_Curve = -1,
-	COSE_Key_OPK_X = -2,
+	COSE_Key_OPK_X = -2
 } COSE_Constants;
 
 typedef enum {
@@ -210,7 +210,7 @@
 	COSE_Curve_X25519 = 4,
 	COSE_Curve_X448 = 5,
 	COSE_Curve_Ed25519 = 6,
-	COSE_Curve_Ed448 = 7,
+	COSE_Curve_Ed448 = 7
 } COSE_Curves;
 
 /*
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index f5e9194..ee0444e 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -1,6 +1,6 @@
 if(CMAKE_C_COMPILER_ID STREQUAL "GNU" OR CMAKE_C_COMPILER_ID MATCHES "Clang")
   message(STATUS "adding GCC/Clang options ")
-  add_definitions(-std=gnu99 -Wall -Wextra -pedantic)
+  add_definitions(-Wall -Wextra -pedantic)
   if(COSE_C_FATAL_WARNINGS)
     add_definitions(-Werror)
   endif()
@@ -28,9 +28,9 @@
                ${CMAKE_BINARY_DIR}/include/cose/cose_configure.h)
 
 if(COSE_C_USE_MBEDTLS)
-  set(cose_crypto mbedtls.c)
+  set(cose_crypto mbedtls.cpp)
 else()
-  set(cose_crypto openssl.c)
+  set(cose_crypto openssl.cpp)
 endif()
 
 set(cose_sources
@@ -38,19 +38,19 @@
     ${CMAKE_BINARY_DIR}/include/cose/cose_configure.h
     cose_crypto.h
     cose_int.h
-    Cose.c
-    CoseKey.c
-    CounterSign.c
-    CounterSign0.c
-    MacMessage.c
-    MacMessage0.c
-    Sign.c
-    Sign1.c
-    cbor.c
-    Encrypt.c
-    Encrypt0.c
-    Recipient.c
-    SignerInfo.c
+    Cose.cpp
+    CoseKey.cpp
+    CounterSign.cpp
+    CounterSign0.cpp
+    MacMessage.cpp
+    MacMessage0.cpp
+    Sign.cpp
+    Sign1.cpp
+    cbor.cpp
+    Encrypt.cpp
+    Encrypt0.cpp
+    Recipient.cpp
+    SignerInfo.cpp
     ${cose_crypto})
 
 target_sources(${PROJECT_NAME} PRIVATE ${cose_sources})
diff --git a/src/Cose.c b/src/Cose.cpp
similarity index 97%
rename from src/Cose.c
rename to src/Cose.cpp
index 413a099..c6bb91e 100644
--- a/src/Cose.c
+++ b/src/Cose.cpp
@@ -86,6 +86,11 @@
 	cn_cbor_errback errState;  // = { 0 };
 	cn_cbor_errback cbor_error;
 
+	if (false) {
+	errorReturn:
+		return false;	
+	}
+
 #ifdef USE_CBOR_CONTEXT
 	if (context != NULL) {
 		pobj->m_allocContext = *context;
@@ -159,8 +164,6 @@
 
 	return true;
 
-errorReturn:
-	return false;
 }
 
 void _COSE_Release(COSE *pcose)
@@ -207,6 +210,13 @@
 	cn_cbor_errback cbor_err;
 	HCOSE h;
 
+	if (false) {
+	errorReturn:
+		// M00TODO - break up the init and allocation below for memory tests.
+		CN_CBOR_FREE(cbor, context);
+		return NULL;	
+	}
+	
 	CHECK_CONDITION(
 		(rgbData != NULL) && (ptype != NULL), COSE_ERR_INVALID_PARAMETER);
 
@@ -220,7 +230,7 @@
 				COSE_ERR_INVALID_PARAMETER);
 		}
 		else {
-			struct_type = cbor->v.uint;
+			struct_type = (COSE_object_type) cbor->v.uint;
 		}
 		*ptype = struct_type;
 
@@ -318,10 +328,6 @@
 
 	return h;
 
-errorReturn:
-	// M00TODO - break up the init and allocation above for memory tests.
-	CN_CBOR_FREE(cbor, context);
-	return NULL;
 }
 
 size_t COSE_Encode(HCOSE msg, byte *rgb, size_t ib, size_t cb)
diff --git a/src/CoseKey.c b/src/CoseKey.cpp
similarity index 100%
rename from src/CoseKey.c
rename to src/CoseKey.cpp
diff --git a/src/CounterSign.c b/src/CounterSign.cpp
similarity index 97%
rename from src/CounterSign.c
rename to src/CounterSign.cpp
index 8d4b0e8..4698fad 100644
--- a/src/CounterSign.c
+++ b/src/CounterSign.cpp
@@ -38,7 +38,7 @@
 {
 	COSE_CounterSign* pobj = pIn;
 
-	cose_errback error = {0};
+	cose_errback error = {COSE_ERR_NONE};
 	if (perr == NULL) {
 		perr = &error;
 	}
@@ -252,6 +252,7 @@
 {
 	bool fRet = false;
 	COSE_CounterSign* pSigner = (COSE_CounterSign*)hSigner;
+	COSE_KEY* pKey = (COSE_KEY*)hKey;
 
 	CHECK_CONDITION(IsValidCounterSignHandle(hSigner), COSE_ERR_INVALID_HANDLE);
 	CHECK_CONDITION(IsValidKeyHandle(hKey), COSE_ERR_INVALID_HANDLE);
@@ -259,7 +260,6 @@
 	if (pSigner->m_signer.m_pkey != NULL) {
 		COSE_KEY_Free((HCOSE_KEY) pSigner->m_signer.m_pkey);
 	}
-	COSE_KEY* pKey = (COSE_KEY*)hKey;
 	
 	pSigner->m_signer.m_pkey = pKey;
 	if (hKey != NULL) {
@@ -275,11 +275,10 @@
 	int index,
 	cose_errback* perr)
 {
+	COSE_CounterSign* pCounterSign = pMessage->m_counterSigners;
 	CHECK_CONDITION(
 		pMessage->m_counterSigners != NULL, COSE_ERR_INVALID_PARAMETER);
 
-	COSE_CounterSign* pCounterSign = pMessage->m_counterSigners;
-
 	for (int i = 0; i < index; i++) {
 		pCounterSign = pCounterSign->m_next;
 		CHECK_CONDITION(pCounterSign != NULL, COSE_ERR_INVALID_PARAMETER);
@@ -462,6 +461,11 @@
 	HCOSE_COUNTERSIGN hCountersignature,
 	cose_errback* perr)
 {
+	if (false) {
+	errorReturn:
+		return false;	
+	}
+	
 	CHECK_CONDITION(IsValidSignerHandle(hSigner), COSE_ERR_INVALID_HANDLE);
 	CHECK_CONDITION(
 		IsValidCounterSignHandle(hCountersignature), COSE_ERR_INVALID_HANDLE);
@@ -484,8 +488,6 @@
 
 	return f;
 
-errorReturn:
-	return false;
 }
 
 /***************************************************************************************************
@@ -530,6 +532,11 @@
 	HCOSE_COUNTERSIGN hCountersignature,
 	cose_errback* perr)
 {
+	if (false) {
+	errorReturn:
+		return false;	
+	}
+	
 	CHECK_CONDITION(IsValidSignHandle(hSignMsg), COSE_ERR_INVALID_HANDLE);
 	CHECK_CONDITION(
 		IsValidCounterSignHandle(hCountersignature), COSE_ERR_INVALID_HANDLE);
@@ -552,8 +559,6 @@
 
 	return f;
 
-errorReturn:
-	return false;
 }
 #endif
 
@@ -600,6 +605,11 @@
 	HCOSE_COUNTERSIGN hCountersignature,
 	cose_errback* perr)
 {
+	if (false) {
+	errorReturn:
+		return false;		
+	}
+	
 	CHECK_CONDITION(IsValidSign1Handle(hSignMsg), COSE_ERR_INVALID_HANDLE);
 	CHECK_CONDITION(
 		IsValidCounterSignHandle(hCountersignature), COSE_ERR_INVALID_HANDLE);
@@ -621,9 +631,6 @@
 		cnProtected, "CounterSignature", perr);
 
 	return f;
-
-errorReturn:
-	return false;
 }
 #endif
 
@@ -670,6 +677,10 @@
 	HCOSE_COUNTERSIGN hCountersignature,
 	cose_errback* perr)
 {
+	if (false) {
+	errorReturn:
+		return false;		
+	}
 	CHECK_CONDITION(IsValidEnvelopedHandle(hSignMsg), COSE_ERR_INVALID_HANDLE);
 	CHECK_CONDITION(
 		IsValidCounterSignHandle(hCountersignature), COSE_ERR_INVALID_HANDLE);
@@ -691,9 +702,6 @@
 		cnProtected, "CounterSignature", perr);
 
 	return f;
-
-errorReturn:
-	return false;
 }
 #endif
 
@@ -741,6 +749,11 @@
 	HCOSE_COUNTERSIGN hCountersignature,
 	cose_errback* perr)
 {
+	if (false) {
+	errorReturn:
+		return false;		
+	}
+	
 	CHECK_CONDITION(IsValidRecipientHandle(hSignMsg), COSE_ERR_INVALID_HANDLE);
 	CHECK_CONDITION(
 		IsValidCounterSignHandle(hCountersignature), COSE_ERR_INVALID_HANDLE);
@@ -762,9 +775,6 @@
 		cnProtected, "CounterSignature", perr);
 
 	return f;
-
-errorReturn:
-	return false;
 }
 #endif
 
@@ -811,6 +821,11 @@
 	HCOSE_COUNTERSIGN hCountersignature,
 	cose_errback* perr)
 {
+	if (false) {
+	errorReturn:
+		return false;		
+	}
+	
 	CHECK_CONDITION(IsValidEncryptHandle(hSignMsg), COSE_ERR_INVALID_HANDLE);
 	CHECK_CONDITION(
 		IsValidCounterSignHandle(hCountersignature), COSE_ERR_INVALID_HANDLE);
@@ -832,9 +847,6 @@
 		cnProtected, "CounterSignature", perr);
 
 	return f;
-
-errorReturn:
-	return false;
 }
 #endif
 
@@ -881,6 +893,11 @@
 	HCOSE_COUNTERSIGN hCountersignature,
 	cose_errback* perr)
 {
+	if (false) {
+	errorReturn:
+		return false;		
+	}
+	
 	CHECK_CONDITION(IsValidMac0Handle(hSignMsg), COSE_ERR_INVALID_HANDLE);
 	CHECK_CONDITION(
 		IsValidCounterSignHandle(hCountersignature), COSE_ERR_INVALID_HANDLE);
@@ -902,9 +919,6 @@
 		cnProtected, "CounterSignature", perr);
 
 	return f;
-
-errorReturn:
-	return false;
 }
 #endif
 
@@ -951,6 +965,11 @@
 	HCOSE_COUNTERSIGN hCountersignature,
 	cose_errback* perr)
 {
+	if (false) {
+	errorReturn:
+		return false;		
+	}
+	
 	CHECK_CONDITION(IsValidMacHandle(hSignMsg), COSE_ERR_INVALID_HANDLE);
 	CHECK_CONDITION(
 		IsValidCounterSignHandle(hCountersignature), COSE_ERR_INVALID_HANDLE);
@@ -972,9 +991,6 @@
 		cnProtected, "CounterSignature", perr);
 
 	return f;
-
-errorReturn:
-	return false;
 }
 #endif
 
diff --git a/src/CounterSign0.c b/src/CounterSign0.cpp
similarity index 100%
rename from src/CounterSign0.c
rename to src/CounterSign0.cpp
diff --git a/src/Encrypt.c b/src/Encrypt.cpp
similarity index 97%
rename from src/Encrypt.c
rename to src/Encrypt.cpp
index 440fcc7..6cd360c 100644
--- a/src/Encrypt.c
+++ b/src/Encrypt.cpp
@@ -90,7 +90,7 @@
 {
 	COSE_Enveloped *pobj = pIn;
 	cn_cbor *pRecipients = NULL;
-	cose_errback error = {0};
+	cose_errback error = {COSE_ERR_NONE};
 	if (perr == NULL) {
 		perr = &error;
 	}
@@ -341,7 +341,7 @@
 		//  Allocate the key if we have not already done so
 
 		if (pbKeyNew == NULL) {
-			pbKeyNew = COSE_CALLOC(cbitKey / 8, 1, context);
+			pbKeyNew = static_cast<byte*>(COSE_CALLOC(cbitKey / 8, 1, context));
 			CHECK_CONDITION(pbKeyNew != NULL, COSE_ERR_OUT_OF_MEMORY);
 			pbKey = pbKeyNew;
 		}
@@ -350,8 +350,8 @@
 
 		if (pRecip != NULL) {
 			COSE_RecipientInfo *pRecipX = NULL;
-			cose_errback errorLocal = {0};
-			int errorFound = 0;
+			cose_errback errorLocal = {COSE_ERR_NONE};
+			cose_error errorFound = COSE_ERR_NONE;
 
 			for (pRecipX = pcose->m_recipientFirst; pRecipX != NULL;
 				 pRecipX = pRecipX->m_recipientNext) {
@@ -557,7 +557,15 @@
 	cn_Alg = _COSE_map_get_int(
 		&pcose->m_message, COSE_Header_Algorithm, COSE_BOTH, perr);
 	if (cn_Alg == NULL) {
-		goto errorReturn;
+	errorReturn:
+		if (pbAuthData != NULL) {
+			COSE_FREE(pbAuthData, context);
+		}
+		if (pbKeyNew != NULL) {
+			memset(pbKeyNew, 0, cbKey);
+			COSE_FREE(pbKeyNew, context);
+		}
+		return fRet;
 	}
 
 	CHECK_CONDITION((cn_Alg->type != CN_CBOR_TEXT), COSE_ERR_UNKNOWN_ALGORITHM);
@@ -821,16 +829,7 @@
 	//  Figure out the clean up
 
 	fRet = true;
-
-errorReturn:
-	if (pbAuthData != NULL) {
-		COSE_FREE(pbAuthData, context);
-	}
-	if (pbKeyNew != NULL) {
-		memset(pbKeyNew, 0, cbKey);
-		COSE_FREE(pbKeyNew, context);
-	}
-	return fRet;
+	goto errorReturn;
 }
 #endif
 
diff --git a/src/Encrypt0.c b/src/Encrypt0.cpp
similarity index 97%
rename from src/Encrypt0.c
rename to src/Encrypt0.cpp
index 805c0d1..1384243 100644
--- a/src/Encrypt0.c
+++ b/src/Encrypt0.cpp
@@ -46,6 +46,11 @@
 HCOSE_ENCRYPT COSE_Encrypt_Init(COSE_INIT_FLAGS flags,
 	CBOR_CONTEXT_COMMA cose_errback *perr)
 {
+	if (false) {
+	errorReturn:
+		return NULL;		
+	}
+	
 	CHECK_CONDITION(flags == COSE_INIT_FLAGS_NONE, COSE_ERR_INVALID_PARAMETER);
 	COSE_Encrypt *pobj =
 		(COSE_Encrypt *)COSE_CALLOC(1, sizeof(COSE_Encrypt), context);
@@ -61,9 +66,6 @@
 	_COSE_InsertInList(&EncryptRoot, &pobj->m_message);
 
 	return (HCOSE_ENCRYPT)pobj;
-
-errorReturn:
-	return NULL;
 }
 #endif
 
@@ -73,7 +75,7 @@
 {
 	COSE_Encrypt *pobj;
 
-	cose_errback error = {0};
+	cose_errback error = {COSE_ERR_NONE};
 	if (perr == NULL) {
 		perr = &error;
 	}
@@ -107,7 +109,7 @@
 {
 	COSE_Encrypt *pobj = pIn;
 	cn_cbor *pRecipients = NULL;
-	cose_errback error = {0};
+	cose_errback error = {COSE_ERR_NONE};
 	if (perr == NULL) {
 		perr = &error;
 	}
@@ -220,7 +222,7 @@
 		if (perror != NULL) {
 			perror->err = COSE_ERR_INVALID_PARAMETER;
 		}
-		return false;
+		return NULL;
 	}
 
 	*pcbContent = cose->cbContent;
diff --git a/src/MacMessage.c b/src/MacMessage.cpp
similarity index 98%
rename from src/MacMessage.c
rename to src/MacMessage.cpp
index 5bc59e7..7bff626 100644
--- a/src/MacMessage.c
+++ b/src/MacMessage.cpp
@@ -400,6 +400,18 @@
 	byte *pbKeyNew = NULL;
 	size_t cbKey = 0;
 
+	if (false) {
+	errorReturn:
+		if (pbKeyNew != NULL) {
+			memset(pbKeyNew, 0, cbKey);
+			COSE_FREE(pbKeyNew, context);
+		}
+		if (pbAuthData != NULL) {
+			COSE_FREE(pbAuthData, context);
+		}
+		return fRet;		
+	}
+	
 	cn_Alg = _COSE_map_get_int(
 		&pcose->m_message, COSE_Header_Algorithm, COSE_BOTH, perr);
 	if (cn_Alg == NULL) {
@@ -614,16 +626,7 @@
 	//  Figure out the clean up
 
 	fRet = true;
-
-errorReturn:
-	if (pbKeyNew != NULL) {
-		memset(pbKeyNew, 0, cbKey);
-		COSE_FREE(pbKeyNew, context);
-	}
-	if (pbAuthData != NULL) {
-		COSE_FREE(pbAuthData, context);
-	}
-	return fRet;
+	goto errorReturn;
 }
 #endif
 
@@ -750,7 +753,7 @@
 	}
 	else {
 		if (pbKeyNew == NULL) {
-			pbKeyNew = COSE_CALLOC(cbitKey / 8, 1, context);
+			pbKeyNew = static_cast<byte*>(COSE_CALLOC(cbitKey / 8, 1, context));
 			CHECK_CONDITION(pbKeyNew != NULL, COSE_ERR_OUT_OF_MEMORY);
 			pbKey = pbKeyNew;
 		}
diff --git a/src/MacMessage0.c b/src/MacMessage0.cpp
similarity index 100%
rename from src/MacMessage0.c
rename to src/MacMessage0.cpp
diff --git a/src/Recipient.c b/src/Recipient.cpp
similarity index 94%
rename from src/Recipient.c
rename to src/Recipient.cpp
index 7b98788..ff36224 100644
--- a/src/Recipient.c
+++ b/src/Recipient.cpp
@@ -46,23 +46,25 @@
 HCOSE_RECIPIENT COSE_Recipient_Init(COSE_INIT_FLAGS flags,
 	CBOR_CONTEXT_COMMA cose_errback *perr)
 {
+	if (false) {
+	errorReturn:
+		return NULL;		
+	}
+	
 	CHECK_CONDITION(flags == COSE_INIT_FLAGS_NONE, COSE_ERR_INVALID_PARAMETER);
 	COSE_RecipientInfo *pobj = (COSE_RecipientInfo *)COSE_CALLOC(
 		1, sizeof(COSE_RecipientInfo), context);
 	CHECK_CONDITION(pobj != NULL, COSE_ERR_OUT_OF_MEMORY);
 
-	if (!_COSE_Init(flags | COSE_INIT_FLAGS_NO_CBOR_TAG,
-			&pobj->m_encrypt.m_message, COSE_recipient_object,
-			CBOR_CONTEXT_PARAM_COMMA perr)) {
+	if (!_COSE_Init(static_cast<COSE_INIT_FLAGS>(COSE_INIT_FLAGS_NO_CBOR_TAG | flags),
+	                &pobj->m_encrypt.m_message, COSE_recipient_object,
+	                CBOR_CONTEXT_PARAM_COMMA perr)) {
 		_COSE_Recipient_Free(pobj);
 		return NULL;
 	}
 
 	_COSE_InsertInList(&RecipientRoot, &pobj->m_encrypt.m_message);
 	return (HCOSE_RECIPIENT)pobj;
-
-errorReturn:
-	return NULL;
 }
 
 bool COSE_Recipient_Free(HCOSE_RECIPIENT hRecipient)
@@ -92,7 +94,8 @@
 {
 	HCOSE_RECIPIENT hRecipient = NULL;
 
-	hRecipient = COSE_Recipient_Init(0, CBOR_CONTEXT_PARAM_COMMA perr);
+	hRecipient = COSE_Recipient_Init(
+		COSE_INIT_FLAGS_NONE, CBOR_CONTEXT_PARAM_COMMA perr);
 	if (hRecipient == NULL) {
 		goto errorReturn;
 	}
@@ -485,10 +488,10 @@
 	if (pcose->m_recipientFirst != NULL) {
 		//  If there is a recipient - ask it for the key
 		CHECK_CONDITION(cbitKeyX != 0, COSE_ERR_INVALID_PARAMETER);
-		pbKeyX = COSE_CALLOC(cbitKeyX / 8, 1, context);
+		pbKeyX = (byte*) COSE_CALLOC(cbitKeyX / 8, 1, context);
 		CHECK_CONDITION(pbKeyX != NULL, COSE_ERR_OUT_OF_MEMORY);
 		cose_errback error = {COSE_ERR_NONE};
-		int errorFound = false;
+		cose_error errorFound = COSE_ERR_NONE;
 
 		for (pRecip2 = pcose->m_recipientFirst; pRecip2 != NULL;
 			 pRecip2 = pRecip->m_recipientNext) {
@@ -501,7 +504,8 @@
 				errorFound = error.err;
 			}
 		}
-		if (errorFound) {
+		
+		if (errorFound != COSE_ERR_NONE) {
 			perr->err = errorFound;
 			goto errorReturn;
 		}
@@ -808,7 +812,25 @@
 	cn_Alg = _COSE_map_get_int(&pRecipient->m_encrypt.m_message,
 		COSE_Header_Algorithm, COSE_BOTH, perr);
 	if (cn_Alg == NULL) {
-		goto errorReturn;
+	errorReturn:
+		memset(rgbKey, 0, sizeof(rgbKey));
+		if (pbKey != NULL) {
+			memset(pbKey, 0, cbKey);
+			COSE_FREE(pbKey, context);
+		}
+		if (pbSecret != NULL) {
+			COSE_FREE(pbSecret, context);
+		}
+		if (pbContext != NULL) {
+			COSE_FREE(pbContext, context);
+		}
+		if (pbAuthData != NULL) {
+			COSE_FREE(pbAuthData, context);
+		}
+		if (ptmp != NULL) {
+			cn_cbor_free(ptmp CBOR_CONTEXT_PARAM);
+		}
+		return fRet;
 	}
 
 	CHECK_CONDITION(cn_Alg->type != CN_CBOR_TEXT, COSE_ERR_UNKNOWN_ALGORITHM);
@@ -1153,26 +1175,7 @@
 	//  Figure out the clean up
 
 	fRet = true;
-
-errorReturn:
-	memset(rgbKey, 0, sizeof(rgbKey));
-	if (pbKey != NULL) {
-		memset(pbKey, 0, cbKey);
-		COSE_FREE(pbKey, context);
-	}
-	if (pbSecret != NULL) {
-		COSE_FREE(pbSecret, context);
-	}
-	if (pbContext != NULL) {
-		COSE_FREE(pbContext, context);
-	}
-	if (pbAuthData != NULL) {
-		COSE_FREE(pbAuthData, context);
-	}
-	if (ptmp != NULL) {
-		cn_cbor_free(ptmp CBOR_CONTEXT_PARAM);
-	}
-	return fRet;
+	goto errorReturn;
 }
 
 byte *_COSE_RecipientInfo_generateKey(COSE_RecipientInfo *pRecipient,
@@ -1199,7 +1202,7 @@
 
 	_COSE_encode_protected(&pRecipient->m_encrypt.m_message, perr);
 
-	pb = COSE_CALLOC(cbitKeySize / 8, 1, context);
+	pb = (byte*) COSE_CALLOC(cbitKeySize / 8, 1, context);
 	CHECK_CONDITION(pb != NULL, COSE_ERR_OUT_OF_MEMORY);
 
 	switch (alg) {
@@ -1340,6 +1343,26 @@
 #endif
 	HCOSE_KEY hKey = NULL;
 
+	if (false) {
+	errorReturn:
+		if (hKey != NULL) {
+			COSE_KEY_Free(hKey);
+		}
+		if (cn_Temp != NULL) {
+			CN_CBOR_FREE(cn_Temp, context);
+		}
+		if (cnTemp != NULL) {
+			CN_CBOR_FREE(cnTemp, context);
+		}
+		if (pbTemp != NULL) {
+			COSE_FREE(pbTemp, context);
+		}
+		if (pbKey != NULL) {
+			COSE_FREE(pbKey, context);
+		}
+		return false;		
+	}
+	
 	CHECK_CONDITION(
 		IsValidRecipientHandle(hRecipient), COSE_ERR_INVALID_HANDLE);
 	CHECK_CONDITION(rgbKey != NULL, COSE_ERR_INVALID_PARAMETER);
@@ -1417,24 +1440,6 @@
 	cn_Temp = NULL;
 
 	return true;
-
-errorReturn:
-	if (hKey != NULL) {
-		COSE_KEY_Free(hKey);
-	}
-	if (cn_Temp != NULL) {		
-		CN_CBOR_FREE(cn_Temp, context);
-	}
-	if (cnTemp != NULL) {
-		CN_CBOR_FREE(cnTemp, context);
-	}
-	if (pbTemp != NULL) {
-		COSE_FREE(pbTemp, context);
-	}
-	if (pbKey != NULL) {
-		COSE_FREE(pbKey, context);
-	}
-	return false;
 }
 
 bool COSE_Recipient_SetKey(HCOSE_RECIPIENT h,
@@ -1442,6 +1447,15 @@
 	cose_errback *perr)
 {
 	HCOSE_KEY hkey = NULL;
+
+	if (false) {
+	errorReturn:
+		if (hkey != NULL) {
+			COSE_KEY_Free(hkey);
+		}
+		return false;		
+	}
+	
 	CHECK_CONDITION(IsValidRecipientHandle(h), COSE_ERR_INVALID_HANDLE);
 	CHECK_CONDITION(pKey != NULL, COSE_ERR_INVALID_PARAMETER);
 
@@ -1459,12 +1473,6 @@
 	}
 	COSE_KEY_Free(hkey);
 	return true;
-
-errorReturn:
-	if (hkey != NULL) {
-		COSE_KEY_Free(hkey);
-	}
-	return false;
 }
 
 bool COSE_Recipient_SetKey2(HCOSE_RECIPIENT h, HCOSE_KEY hKey, cose_errback * perr)
@@ -1513,6 +1521,14 @@
 	bool fRet = false;
 	HCOSE_KEY coseKey = NULL;
 
+	if (false) {
+	errorReturn:
+		if (coseKey != NULL) {
+			COSE_KEY_Free(coseKey);
+		}
+		return fRet;		
+	}
+	
 	CHECK_CONDITION(IsValidRecipientHandle(h), COSE_ERR_INVALID_HANDLE);
 	CHECK_CONDITION(pKey != NULL, COSE_ERR_INVALID_PARAMETER);
 
@@ -1524,12 +1540,7 @@
 	CHECK_CONDITION(coseKey != NULL, COSE_ERR_OUT_OF_MEMORY);
 
 	fRet = COSE_Recipient_SetSenderKey2(h, coseKey, destination, perr);
-
-errorReturn:
-	if (coseKey != NULL) {
-		COSE_KEY_Free(coseKey);
-	}
-	return fRet;
+	goto errorReturn;
 }
 
 bool COSE_Recipient_SetSenderKey2(HCOSE_RECIPIENT h,
@@ -1547,6 +1558,17 @@
 	cn_cbor_context *context = NULL;
 #endif
 
+	if (false) {
+	errorReturn:
+		if (cn2 != NULL) {
+			CN_CBOR_FREE(cn2, context);
+		}
+		if (cn3 != NULL) {
+			CN_CBOR_FREE(cn3, context);
+		}
+		return f;		
+	}
+	
 	CHECK_CONDITION(IsValidRecipientHandle(h), COSE_ERR_INVALID_HANDLE);
 	CHECK_CONDITION(IsValidKeyHandle(hKey), COSE_ERR_INVALID_PARAMETER);
 
@@ -1623,14 +1645,7 @@
 	pKey->m_refCount += 1;
 
 	f = true;
-errorReturn:
-	if (cn2 != NULL) {
-		CN_CBOR_FREE(cn2, context);
-	}
-	if (cn3 != NULL) {
-		CN_CBOR_FREE(cn3, context);
-	}
-	return f;
+	goto errorReturn;
 }
 
 /*!
@@ -1682,7 +1697,7 @@
 
 	if (key == COSE_Header_Algorithm) {
 		if (value->type == CN_CBOR_INT) {
-			switch (value->v.uint) {
+			switch (value->v.sint) {
 				case COSE_Algorithm_Direct:
 #ifdef USE_Direct_HKDF_AES_128
 				case COSE_Algorithm_Direct_HKDF_AES_128:
@@ -1708,17 +1723,28 @@
 #ifdef USE_ECDH_SS_HKDF_512
 				case COSE_Algorithm_ECDH_SS_HKDF_512:
 #endif
-					((COSE_RecipientInfo *)h)->m_encrypt.m_message.m_flags |= 1;
+					((COSE_RecipientInfo *)h)->m_encrypt.m_message.m_flags =
+						(cose_init_flags)(
+						((COSE_RecipientInfo *)h)
+							->m_encrypt.m_message
+							.m_flags | COSE_INIT_FLAGS_DETACHED_CONTENT);
 					break;
 
 				default:
-					((COSE_RecipientInfo *)h)->m_encrypt.m_message.m_flags &=
-						~1;
+					((COSE_RecipientInfo *)h)->m_encrypt.m_message.m_flags =
+						(cose_init_flags)(
+							((COSE_RecipientInfo *)h)
+								->m_encrypt.m_message
+								.m_flags &
+						~COSE_INIT_FLAGS_DETACHED_CONTENT);
 					break;
 			}
 		}
 		else {
-			((COSE_RecipientInfo *)h)->m_encrypt.m_message.m_flags &= ~1;
+			((COSE_RecipientInfo *)h)->m_encrypt.m_message.m_flags =
+				(cose_init_flags)(
+					((COSE_RecipientInfo *)h)->m_encrypt.m_message.m_flags &
+					~COSE_INIT_FLAGS_DETACHED_CONTENT);
 		}
 	}
 
@@ -1745,6 +1771,26 @@
 	cn_cbor *cnParam;
 	byte *pbContext = NULL;
 
+	if (false) {
+	errorReturn:
+		fReturn = false;
+
+	returnHere:
+		if (pbContext != NULL) {
+			COSE_FREE(pbContext, context);
+		}
+		if (pArray != NULL) {
+			CN_CBOR_FREE(pArray, context);
+		}
+		if (cnArrayT != NULL) {
+			CN_CBOR_FREE(cnArrayT, context);
+		}
+		if (cnT != NULL) {
+			CN_CBOR_FREE(cnT, context);
+		}
+		return fReturn;
+	}
+	
 	pArray = cn_cbor_array_create(CBOR_CONTEXT_PARAM_COMMA & cbor_error);
 	CHECK_CONDITION_CBOR(pArray != NULL, cbor_error);
 
@@ -1907,23 +1953,6 @@
 	pbContext = NULL;
 	fReturn = true;
 
-returnHere:
-	if (pbContext != NULL) {
-		COSE_FREE(pbContext, context);
-	}
-	if (pArray != NULL) {
-		CN_CBOR_FREE(pArray, context);
-	}
-	if (cnArrayT != NULL) {
-		CN_CBOR_FREE(cnArrayT, context);
-	}
-	if (cnT != NULL) {
-		CN_CBOR_FREE(cnT, context);
-	}
-	return fReturn;
-
-errorReturn:
-	fReturn = false;
 	goto returnHere;
 }
 #endif
diff --git a/src/Sign.c b/src/Sign.cpp
similarity index 98%
rename from src/Sign.c
rename to src/Sign.cpp
index 0bfe185..a66ff76 100644
--- a/src/Sign.c
+++ b/src/Sign.cpp
@@ -47,6 +47,10 @@
 HCOSE_SIGN COSE_Sign_Init(COSE_INIT_FLAGS flags,
 	CBOR_CONTEXT_COMMA cose_errback *perr)
 {
+	if (false) {
+	errorReturn:
+		return NULL;		
+	}
 	CHECK_CONDITION(flags == COSE_INIT_FLAGS_NONE, COSE_ERR_INVALID_PARAMETER);
 	COSE_SignMessage *pobj =
 		(COSE_SignMessage *)COSE_CALLOC(1, sizeof(COSE_SignMessage), context);
@@ -62,9 +66,6 @@
 	_COSE_InsertInList(&SignRoot, &pobj->m_message);
 
 	return (HCOSE_SIGN)pobj;
-
-errorReturn:
-	return NULL;
 }
 
 HCOSE_SIGN _COSE_Sign_Init_From_Object(cn_cbor *cbor,
@@ -74,7 +75,7 @@
 	COSE_SignMessage *pobj = pIn;
 	cn_cbor *pSigners = NULL;
 	// cn_cbor * tmp;
-	cose_errback error = {0};
+	cose_errback error = {COSE_ERR_NONE};
 	if (perr == NULL) {
 		perr = &error;
 	}
@@ -265,7 +266,7 @@
 
 errorReturn:
 	if (cbor2 != NULL) {
-		CN_CBOR_FREE((void *)cbor2, context);
+		CN_CBOR_FREE(cbor2, context);
 	}
 	if (hSigner != NULL) {
 		COSE_Signer_Free(hSigner);
diff --git a/src/Sign1.c b/src/Sign1.cpp
similarity index 97%
rename from src/Sign1.c
rename to src/Sign1.cpp
index fd217c2..2846291 100644
--- a/src/Sign1.c
+++ b/src/Sign1.cpp
@@ -48,6 +48,11 @@
 HCOSE_SIGN1 COSE_Sign1_Init(COSE_INIT_FLAGS flags,
 	CBOR_CONTEXT_COMMA cose_errback *perr)
 {
+	if (false) {
+	errorReturn:
+		return NULL;		
+	}
+	
 	CHECK_CONDITION(flags == COSE_INIT_FLAGS_NONE, COSE_ERR_INVALID_PARAMETER);
 	COSE_Sign1Message *pobj =
 		(COSE_Sign1Message *)COSE_CALLOC(1, sizeof(COSE_Sign1Message), context);
@@ -68,9 +73,6 @@
 	_COSE_InsertInList(&Sign1Root, &pobj->m_message);
 
 	return (HCOSE_SIGN1)pobj;
-
-errorReturn:
-	return NULL;
 }
 
 HCOSE_SIGN1 _COSE_Sign1_Init_From_Object(cn_cbor *cbor,
@@ -78,7 +80,7 @@
 	CBOR_CONTEXT_COMMA cose_errback *perr)
 {
 	COSE_Sign1Message *pobj = pIn;
-	cose_errback error = {0};
+	cose_errback error = {COSE_ERR_NONE};
 
 	if (perr == NULL) {
 		perr = &error;
@@ -219,6 +221,14 @@
 	HCOSE_KEY cose = NULL;
 	bool fRet = false;
 
+	if (false) {
+	errorReturn:
+		if (cose != NULL) {
+			COSE_KEY_Free(cose);
+		}
+		return fRet;		
+	}
+
 	CHECK_CONDITION(pKey != NULL, COSE_ERR_INVALID_PARAMETER);
 
 #ifdef USE_CBOR_CONTEXT
@@ -231,12 +241,7 @@
 	}
 
 	fRet = COSE_Sign1_Sign2(h, cose, perr);
-
-errorReturn:
-	if (cose != NULL) {
-		COSE_KEY_Free(cose);
-	}
-	return fRet;
+	goto errorReturn;
 }
 
 
@@ -290,6 +295,14 @@
 	const cn_cbor *cnProtected;
 	COSE_KEY *pcose = NULL;
 
+	if (false) {
+	errorReturn:
+		if (pcose != NULL) {
+			COSE_KEY_Free((HCOSE_KEY)pcose);
+		}
+		return false;		
+	}
+
 	CHECK_CONDITION(IsValidSign1Handle(hSign), COSE_ERR_INVALID_HANDLE);
 
 	pSign = (COSE_Sign1Message *)hSign;
@@ -313,12 +326,6 @@
 
 	COSE_KEY_Free((HCOSE_KEY)pcose);
 	return f;
-
-errorReturn:
-	if (pcose != NULL) {
-		COSE_KEY_Free((HCOSE_KEY)pcose);
-	}
-	return false;
 }
 
 cn_cbor *COSE_Sign1_map_get_int(HCOSE_SIGN1 h,
diff --git a/src/SignerInfo.c b/src/SignerInfo.cpp
similarity index 96%
rename from src/SignerInfo.c
rename to src/SignerInfo.cpp
index 2ffc110..292e21d 100644
--- a/src/SignerInfo.c
+++ b/src/SignerInfo.cpp
@@ -150,6 +150,19 @@
 	bool f = false;
 	cn_cbor *cn = NULL;
 
+	if (false) {
+	errorReturn:
+		if (cn != NULL) {
+			CN_CBOR_FREE(cn, context);
+		}
+		if (pArray != NULL) {
+			CN_CBOR_FREE(pArray, context);
+		}
+		if (pbToSign != NULL) {
+			COSE_FREE(pbToSign, context);
+		}
+		return f;		
+	}
 	pArray = cn_cbor_array_create(CBOR_CONTEXT_PARAM_COMMA & cbor_error);
 	CHECK_CONDITION_CBOR(pArray != NULL, cbor_error);
 
@@ -215,18 +228,7 @@
 	*pcbToSign = cbToSign;
 	pbToSign = NULL;
 	f = true;
-
-errorReturn:
-	if (cn != NULL) {
-		CN_CBOR_FREE(cn, context);
-	}
-	if (pArray != NULL) {
-		CN_CBOR_FREE(pArray, context);
-	}
-	if (pbToSign != NULL) {
-		COSE_FREE(pbToSign, context);
-	}
-	return f;
+	goto errorReturn;	
 }
 
 bool _COSE_Signer_sign(COSE_SignerInfo *pSigner,
@@ -344,8 +346,12 @@
 bool COSE_Signer_SetKey2(HCOSE_SIGNER h, HCOSE_KEY pKey, cose_errback *perr)
 {
 	COSE_SignerInfo *p;
-	bool fRet = false;
 
+	if (false) {
+	errorReturn:
+		return false;		
+	}
+	
 	CHECK_CONDITION(IsValidSignerHandle(h), COSE_ERR_INVALID_HANDLE);
 	CHECK_CONDITION(IsValidKeyHandle(pKey), COSE_ERR_INVALID_HANDLE);
 
@@ -359,9 +365,7 @@
 		pcose->m_refCount += 1;
 	}
 
-	fRet = true;
-errorReturn:
-	return fRet;
+	return true;
 }
 
 bool COSE_Signer_SetKey(HCOSE_SIGNER h, const cn_cbor *pKey, cose_errback *perr)
@@ -436,7 +440,12 @@
 	const cn_cbor *cn = _COSE_map_get_int(
 		&pSigner->m_message, COSE_Header_Algorithm, COSE_BOTH, perr);
 	if (cn == NULL) {
-		goto errorReturn;
+	errorReturn:
+		if (pbToBeSigned != NULL) {
+			COSE_FREE(pbToBeSigned, context);
+		}
+
+		return fRet;
 	}
 
 	if (cn->type == CN_CBOR_TEXT) {
@@ -514,13 +523,7 @@
 	}
 
 	fRet = true;
-
-errorReturn:
-	if (pbToBeSigned != NULL) {
-		COSE_FREE(pbToBeSigned, context);
-	}
-
-	return fRet;
+	goto errorReturn;
 }
 
 #if INCLUDE_SIGN
diff --git a/src/bcrypt.c b/src/bcrypt.cpp
similarity index 100%
rename from src/bcrypt.c
rename to src/bcrypt.cpp
diff --git a/src/cbor.c b/src/cbor.cpp
similarity index 93%
rename from src/cbor.c
rename to src/cbor.cpp
index 83f8e90..3bdce1a 100644
--- a/src/cbor.c
+++ b/src/cbor.cpp
@@ -1,4 +1,5 @@
 #include "cn-cbor/cn-cbor.h"
+#include <cose/cose.h>
 #include <stdlib.h>
 #ifndef __MBED__
 #include <memory.h>
@@ -8,7 +9,7 @@
 	if (errp) {                                    \
 		errp->err = CN_CBOR_NO_ERROR;              \
 	}                                              \
-	(v) = CN_CALLOC_CONTEXT();                     \
+	(v) = static_cast<cn_cbor *> (CN_CALLOC_CONTEXT());  \
 	if (!(v)) {                                    \
 		if (errp) {                                \
 			errp->err = CN_CBOR_ERR_OUT_OF_MEMORY; \
@@ -22,7 +23,7 @@
 #define CN_CALLOC(ctx)                                           \
 	((ctx) && (ctx)->calloc_func)                                \
 		? (ctx)->calloc_func(1, sizeof(cn_cbor), (ctx)->context) \
-		: calloc(1, sizeof(cn_cbor));
+		: calloc(1, sizeof(cn_cbor))
 
 #define CN_CALLOC_CONTEXT() CN_CALLOC(context)
 #define CN_CBOR_CALLOC(c, i, ctx)                                            \
@@ -30,9 +31,9 @@
 								  : calloc(c, i)
 #else
 #define CBOR_CONTEXT_PARAM
-#define CN_CALLOC(ctx) calloc(1, sizeof(cn_cbor));
+#define CN_CALLOC(ctx) calloc(1, sizeof(cn_cbor))
 #define CN_CALLOC_CONTEXT() CN_CALLOC(context)
-#define CN_CBOR_CALLOC(c, i, ctx) calloc(c, i);
+#define CN_CBOR_CALLOC(c, i, ctx) calloc(c, i)
 #endif
 
 /***
@@ -135,7 +136,7 @@
 
 	switch (pIn->type) {
 		case CN_CBOR_TEXT:
-			sz = CN_CBOR_CALLOC(pIn->length + 1, 1, context);
+			sz = (char*)( CN_CBOR_CALLOC(pIn->length + 1, 1, context));
 			if (sz == NULL) {
 				return NULL;
 			}
@@ -150,7 +151,8 @@
 			break;
 
 		case CN_CBOR_BYTES:
-			pb = CN_CBOR_CALLOC((int)pIn->length, 1, context);
+			pb = static_cast<unsigned char *> (CN_CBOR_CALLOC(
+				(int)pIn->length, 1, context));
 			if (pb == NULL) {
 				return NULL;
 			}
diff --git a/src/mbedtls.c b/src/mbedtls.cpp
similarity index 95%
rename from src/mbedtls.c
rename to src/mbedtls.cpp
index 9bf0e54..6a3655b 100644
--- a/src/mbedtls.c
+++ b/src/mbedtls.cpp
@@ -123,7 +123,7 @@
 	cbor_iv =
 		_COSE_map_get_int(&pcose->m_message, COSE_Header_IV, COSE_BOTH, perr);
 	if (cbor_iv == NULL) {
-		pbIV = COSE_CALLOC(NSize, 1, context);
+		pbIV = (byte *) COSE_CALLOC(NSize, 1, context);
 		CHECK_CONDITION(pbIV != NULL, COSE_ERR_OUT_OF_MEMORY);
 		rand_bytes(pbIV, NSize);
 		memcpy(rgbIV, pbIV, NSize);
@@ -303,6 +303,21 @@
 #endif
 	cn_cbor_errback cbor_error;
 
+	if (false) {
+	errorReturn:
+		if (pbIV != NULL) {
+			COSE_FREE(pbIV, context);
+		}
+		if (cbor_iv_t != NULL) {
+			COSE_FREE(cbor_iv_t, context);
+		}
+		if (rgbOut != NULL) {
+			COSE_FREE(rgbOut, context);
+		}
+		mbedtls_gcm_free(&ctx);
+		return false;		
+	}
+	
 	// Make it first so we can clean it up
 	mbedtls_gcm_init(&ctx);
 
@@ -311,7 +326,7 @@
 	cbor_iv =
 		_COSE_map_get_int(&pcose->m_message, COSE_Header_IV, COSE_BOTH, perr);
 	if (cbor_iv == NULL) {
-		pbIV = COSE_CALLOC(96, 1, context);
+		pbIV = (byte*) COSE_CALLOC(96, 1, context);
 		CHECK_CONDITION(pbIV != NULL, COSE_ERR_OUT_OF_MEMORY);
 		rand_bytes(pbIV, 96 / 8);
 		memcpy(rgbIV, pbIV, 96 / 8);
@@ -379,19 +394,6 @@
 		COSE_FREE(pbIV, context);
 	}
 	return true;
-
-errorReturn:
-	if (pbIV != NULL) {
-		COSE_FREE(pbIV, context);
-	}
-	if (cbor_iv_t != NULL) {
-		COSE_FREE(cbor_iv_t, context);
-	}
-	if (rgbOut != NULL) {
-		COSE_FREE(rgbOut, context);
-	}
-	mbedtls_gcm_free(&ctx);
-	return false;
 }
 #endif
 
@@ -450,7 +452,8 @@
 	}
 
 	CHECK_CONDITION0(
-		mbedtls_hkdf_extract(pmd, pbSalt, cbSalt, pbKey, cbKey, rgbDigest), 0);
+		mbedtls_hkdf_extract(pmd, pbSalt, cbSalt, pbKey, cbKey, rgbDigest),
+		COSE_ERR_CRYPTO_FAIL);
 
 	*pcbDigest = cbDigest;
 
@@ -560,7 +563,7 @@
 	info = mbedtls_md_info_from_string(md_name);
 	mbedtls_md_setup(&contx, info, 1);
 
-	rgbOut = COSE_CALLOC(mbedtls_md_get_size(info), 1, context);
+	rgbOut = (byte *) COSE_CALLOC(mbedtls_md_get_size(info), 1, context);
 	CHECK_CONDITION(rgbOut != NULL, COSE_ERR_OUT_OF_MEMORY);
 
 	CHECK_CONDITION(
@@ -603,6 +606,16 @@
 	cn_cbor_context *context = &pcose->m_message.m_allocContext;
 #endif
 
+	if (false) {
+	errorReturn:
+		if (rgbOut != NULL) {
+			COSE_FREE(rgbOut, context);
+		}
+		mbedtls_md_free(&contx);
+		return false;		
+	}
+
+
 	switch (HSize) {
 		case 256:
 			md_name = "SHA256";
@@ -623,7 +636,7 @@
 	mbedtls_md_setup(&contx, info, 1);
 
 	cbOut = mbedtls_md_get_size(info);
-	rgbOut = COSE_CALLOC(cbOut, 1, context);
+	rgbOut = (byte*) COSE_CALLOC(cbOut, 1, context);
 	CHECK_CONDITION(rgbOut != NULL, COSE_ERR_OUT_OF_MEMORY);
 
 	CHECK_CONDITION(
@@ -646,14 +659,6 @@
 	COSE_FREE(rgbOut, context);
 	mbedtls_md_free(&contx);
 	return !f;
-
-errorReturn:
-	if (rgbOut != NULL) {
-		COSE_FREE(rgbOut, context);
-	}
-	COSE_FREE(rgbOut, context);
-	mbedtls_md_free(&contx);
-	return false;
 }
 #endif
 
@@ -670,7 +675,7 @@
 	int cbKey = 0;
 	int cbGroup = 0;
 	const cn_cbor *p;
-	mbedtls_ecp_group_id groupId = 0;
+	mbedtls_ecp_group_id groupId;
 
 	p = cn_cbor_mapget_int(pKey->m_cborKey, COSE_Key_Type);
 	CHECK_CONDITION(p != NULL, COSE_ERR_INVALID_PARAMETER);
@@ -818,7 +823,7 @@
 
 	cbR = (keypair.grp.nbits + 7) / 8;
 
-	pbSig = COSE_CALLOC(cbR, 2, context);
+	pbSig = (byte *) COSE_CALLOC(cbR, 2, context);
 	CHECK_CONDITION(pbSig != NULL, COSE_ERR_OUT_OF_MEMORY);
 
 	CHECK_CONDITION(
@@ -1117,7 +1122,7 @@
 	int cbsecret = 0;
 	byte *pbsecret = NULL;
 	bool fRet = false;
-	mbedtls_ecp_group_id groupId = 0;
+	mbedtls_ecp_group_id groupId;
 	mbedtls_ecp_keypair keypair;
 	mbedtls_ecdh_context ctx;
 	mbedtls_mpi d;
@@ -1125,12 +1130,33 @@
 	mbedtls_mpi z;
 	cn_cbor *pkey = NULL;
 	int cose_group = 0;
+	mbedtls_ecp_group group = {MBEDTLS_ECP_DP_NONE};
 
 	mbedtls_mpi_init(&z);
 	mbedtls_ecdh_init(&ctx);
 	mbedtls_mpi_init(&d);
 	mbedtls_ecp_keypair_init(&keypair);
 
+	if (false) {
+	errorReturn:
+		if (pbsecret != NULL) {
+			COSE_FREE(pbsecret, context);
+		}
+		if (pkey != NULL) {
+			CN_CBOR_FREE(pkey, context);
+		}
+		if (p != NULL) {
+			CN_CBOR_FREE(p, context);
+		}
+
+		mbedtls_mpi_free(&d);
+		mbedtls_mpi_free(&z);
+		mbedtls_ecp_group_free(&group);
+		mbedtls_ecp_keypair_free(&keypair);
+		mbedtls_ecdh_free(&ctx);
+		return fRet;		
+	}
+
 	p = cn_cbor_mapget_int(pKeyPublic->m_cborKey, COSE_Key_EC_Curve);
 	CHECK_CONDITION(
 		(p != NULL) && (p->type == CN_CBOR_UINT), COSE_ERR_INVALID_PARAMETER);
@@ -1159,7 +1185,6 @@
 	}
 	p = NULL;
 
-	mbedtls_ecp_group group = {0};
 	CHECK_CONDITION0(
 		mbedtls_ecp_group_load(&group, groupId), COSE_ERR_INVALID_PARAMETER);
 
@@ -1198,7 +1223,7 @@
 			cbor_error);
 		p = NULL;
 
-		pbsecret = COSE_CALLOC(cbSize, 1, context);
+		pbsecret = (byte *) COSE_CALLOC(cbSize, 1, context);
 		CHECK_CONDITION(pbsecret != NULL, COSE_ERR_OUT_OF_MEMORY);
 		memcpy(pbsecret, buff + 1, cbSize);
 
@@ -1211,7 +1236,7 @@
 			cbor_error);
 		p = NULL;
 
-		pbsecret = COSE_CALLOC(cbSize, 1, context);
+		pbsecret = (byte *) COSE_CALLOC(cbSize, 1, context);
 		CHECK_CONDITION(pbsecret != NULL, COSE_ERR_OUT_OF_MEMORY);
 		memcpy(pbsecret, buff + 1 + cbSize, cbSize);
 
@@ -1255,7 +1280,7 @@
 		COSE_ERR_CRYPTO_FAIL);
 
 	cbsecret = cbGroup;
-	pbsecret = COSE_CALLOC(cbsecret, 1, context);
+	pbsecret = (byte *) COSE_CALLOC(cbsecret, 1, context);
 	CHECK_CONDITION(pbsecret != NULL, COSE_ERR_OUT_OF_MEMORY);
 
 	CHECK_CONDITION0(
@@ -1266,24 +1291,7 @@
 	pbsecret = NULL;
 
 	fRet = true;
-
-errorReturn:
-	if (pbsecret != NULL) {
-		COSE_FREE(pbsecret, context);
-	}
-	if (pkey != NULL) {
-		CN_CBOR_FREE(pkey, context);
-	}
-	if (p != NULL) {
-		CN_CBOR_FREE(p, context);
-	}
-
-	mbedtls_mpi_free(&d);
-	mbedtls_mpi_free(&z);
-	mbedtls_ecp_group_free(&group);
-	mbedtls_ecp_keypair_free(&keypair);
-	mbedtls_ecdh_free(&ctx);
-	return fRet;
+	goto errorReturn;
 }
 #endif	// USE_ECDH
 #endif	// COSE_C_USE_MBEDTLS
diff --git a/src/openssl.c b/src/openssl.cpp
similarity index 96%
rename from src/openssl.c
rename to src/openssl.cpp
index 55cf00a..dcd82b1 100644
--- a/src/openssl.c
+++ b/src/openssl.cpp
@@ -16,6 +16,7 @@
 #include <openssl/ecdsa.h>
 #include <openssl/ecdh.h>
 #include <openssl/rand.h>
+#include <openssl/bn.h>
 
 static bool FUseCompressed = true;
 
@@ -25,7 +26,7 @@
 
 HMAC_CTX *HMAC_CTX_new()
 {
-	HMAC_CTX *foo = malloc(sizeof(HMAC_CTX));
+	HMAC_CTX *foo = (HMAC_CTX *) malloc(sizeof(HMAC_CTX));
 	if (foo != NULL) {
 		HMAC_CTX_init(foo);
 	}
@@ -212,7 +213,7 @@
 	cbor_iv =
 		_COSE_map_get_int(&pcose->m_message, COSE_Header_IV, COSE_BOTH, perr);
 	if (cbor_iv == NULL) {
-		pbIV = COSE_CALLOC(NSize, 1, context);
+		pbIV = (byte *) COSE_CALLOC(NSize, 1, context);
 		CHECK_CONDITION(pbIV != NULL, COSE_ERR_OUT_OF_MEMORY);
 		rand_bytes(pbIV, NSize);
 		memcpy(rgbIV, pbIV, NSize);
@@ -434,6 +435,21 @@
 #endif
 	cn_cbor_errback cbor_error;
 
+	if (false) {
+	errorReturn:
+		if (pbIV != NULL) {
+			COSE_FREE(pbIV, context);
+		}
+		if (cbor_iv_t != NULL) {
+			COSE_FREE(cbor_iv_t, context);
+		}
+		if (rgbOut != NULL) {
+			COSE_FREE(rgbOut, context);
+		}
+		EVP_CIPHER_CTX_free(ctx);
+		return false;		
+	}
+	
 	// Make it first so we can clean it up
 	ctx = EVP_CIPHER_CTX_new();
 	CHECK_CONDITION(NULL != ctx, COSE_ERR_OUT_OF_MEMORY);
@@ -443,7 +459,7 @@
 	cbor_iv =
 		_COSE_map_get_int(&pcose->m_message, COSE_Header_IV, COSE_BOTH, perr);
 	if (cbor_iv == NULL) {
-		pbIV = COSE_CALLOC(96, 1, context);
+		pbIV = (byte *) COSE_CALLOC(96, 1, context);
 		CHECK_CONDITION(pbIV != NULL, COSE_ERR_OUT_OF_MEMORY);
 		rand_bytes(pbIV, 96 / 8);
 		memcpy(rgbIV, pbIV, 96 / 8);
@@ -523,19 +539,6 @@
 		COSE_FREE(pbIV, context);
 	}
 	return true;
-
-errorReturn:
-	if (pbIV != NULL) {
-		COSE_FREE(pbIV, context);
-	}
-	if (cbor_iv_t != NULL) {
-		COSE_FREE(cbor_iv_t, context);
-	}
-	if (rgbOut != NULL) {
-		COSE_FREE(rgbOut, context);
-	}
-	EVP_CIPHER_CTX_free(ctx);
-	return false;
 }
 
 bool AES_CBC_MAC_Create(COSE_MacMessage *pcose,
@@ -561,7 +564,7 @@
 	ctx = EVP_CIPHER_CTX_new();
 	CHECK_CONDITION(NULL != ctx, COSE_ERR_OUT_OF_MEMORY);
 
-	rgbOut = COSE_CALLOC(16, 1, context);
+	rgbOut = (byte *) COSE_CALLOC(16, 1, context);
 	CHECK_CONDITION(rgbOut != NULL, COSE_ERR_OUT_OF_MEMORY);
 
 	switch (cbKey * 8) {
@@ -635,6 +638,11 @@
 	bool f = false;
 	unsigned int i;
 
+	if (false) {
+	errorReturn:
+		EVP_CIPHER_CTX_free(ctx);
+		return false;		
+	}
 	switch (cbKey * 8) {
 		case 128:
 			pcipher = EVP_aes_128_cbc();
@@ -680,10 +688,6 @@
 
 	EVP_CIPHER_CTX_free(ctx);
 	return !f;
-
-errorReturn:
-	EVP_CIPHER_CTX_free(ctx);
-	return false;
 }
 
 #if 0
@@ -980,7 +984,7 @@
 			break;
 	}
 
-	rgbOut = COSE_CALLOC(EVP_MAX_MD_SIZE, 1, context);
+	rgbOut = (byte *) COSE_CALLOC(EVP_MAX_MD_SIZE, 1, context);
 	CHECK_CONDITION(rgbOut != NULL, COSE_ERR_OUT_OF_MEMORY);
 
 	CHECK_CONDITION(
@@ -1018,6 +1022,14 @@
 	cn_cbor_context *context = &pcose->m_message.m_allocContext;
 #endif
 
+	if (false) {
+	errorReturn:
+		if (rgbOut != NULL) {
+			COSE_FREE(rgbOut, context);
+		}
+		HMAC_CTX_free(ctx);
+		return false;		
+	}
 	ctx = HMAC_CTX_new();
 	CHECK_CONDITION(ctx != NULL, COSE_ERR_OUT_OF_MEMORY);
 
@@ -1036,7 +1048,7 @@
 			break;
 	}
 
-	rgbOut = COSE_CALLOC(EVP_MAX_MD_SIZE, 1, context);
+	rgbOut = (byte *) COSE_CALLOC(EVP_MAX_MD_SIZE, 1, context);
 	CHECK_CONDITION(rgbOut != NULL, COSE_ERR_OUT_OF_MEMORY);
 
 	CHECK_CONDITION(
@@ -1058,13 +1070,6 @@
 	COSE_FREE(rgbOut, context);
 	HMAC_CTX_free(ctx);
 	return !f;
-
-errorReturn:
-	if (rgbOut != NULL) {
-		COSE_FREE(rgbOut, context);
-	}
-	HMAC_CTX_free(ctx);
-	return false;
 }
 
 #define COSE_Key_EC_Curve -1
@@ -1075,6 +1080,15 @@
 EC_KEY *ECKey_From(COSE_KEY *pKey, int *cbGroup, cose_errback *perr)
 {
 	EC_KEY *pNewKey = NULL;
+
+	if (false) {
+	errorReturn:
+		if (pNewKey != NULL) {
+			EC_KEY_free(pNewKey);
+		}
+		return NULL;		
+	}
+
 	if (pKey->m_opensslKey != NULL) {
 		EC_KEY *pKeyNew = EVP_PKEY_get1_EC_KEY(pKey->m_opensslKey);
 		CHECK_CONDITION(pKeyNew != NULL, COSE_ERR_INVALID_PARAMETER);
@@ -1167,12 +1181,6 @@
 	CHECK_CONDITION(EVP_PKEY_set1_EC_KEY(pKey->m_opensslKey, pNewKey) == 1, COSE_ERR_CRYPTO_FAIL);
 
 	return pNewKey;
-
-errorReturn:
-	if (pNewKey != NULL) {
-		EC_KEY_free(pNewKey);
-	}
-	return NULL;
 }
 
 COSE_KEY *EC_FromKey(const EC_KEY *pKey, CBOR_CONTEXT_COMMA cose_errback *perr)
@@ -1223,7 +1231,7 @@
 		cbSize = EC_POINT_point2oct(
 			pgroup, pPoint, POINT_CONVERSION_COMPRESSED, NULL, 0, NULL);
 		CHECK_CONDITION(cbSize > 0, COSE_ERR_CRYPTO_FAIL);
-		pbPoint = COSE_CALLOC(cbSize, 1, context);
+		pbPoint = (byte *) COSE_CALLOC(cbSize, 1, context);
 		CHECK_CONDITION(pbPoint != NULL, COSE_ERR_OUT_OF_MEMORY);
 		CHECK_CONDITION(
 			EC_POINT_point2oct(pgroup, pPoint, POINT_CONVERSION_COMPRESSED,
@@ -1234,7 +1242,7 @@
 		cbSize = EC_POINT_point2oct(
 			pgroup, pPoint, POINT_CONVERSION_UNCOMPRESSED, NULL, 0, NULL);
 		CHECK_CONDITION(cbSize > 0, COSE_ERR_CRYPTO_FAIL);
-		pbPoint = COSE_CALLOC(cbSize, 1, context);
+		pbPoint = (byte *) COSE_CALLOC(cbSize, 1, context);
 		CHECK_CONDITION(pbPoint != NULL, COSE_ERR_OUT_OF_MEMORY);
 		CHECK_CONDITION(
 			EC_POINT_point2oct(pgroup, pPoint, POINT_CONVERSION_UNCOMPRESSED,
@@ -1242,7 +1250,7 @@
 			COSE_ERR_CRYPTO_FAIL);
 	}
 
-	pbOut = COSE_CALLOC((int)(cbSize / 2), 1, context);
+	pbOut = (byte *) COSE_CALLOC((int)(cbSize / 2), 1, context);
 	CHECK_CONDITION(pbOut != NULL, COSE_ERR_OUT_OF_MEMORY);
 	memcpy(pbOut, pbPoint + 1, (int)(cbSize / 2));
 	p = cn_cbor_data_create2(
@@ -1264,7 +1272,7 @@
 		p = NULL;
 	}
 	else {
-		pbOut = COSE_CALLOC((int)(cbSize / 2), 1, context);
+		pbOut = (byte *) COSE_CALLOC((int)(cbSize / 2), 1, context);
 		CHECK_CONDITION(pbOut != NULL, COSE_ERR_OUT_OF_MEMORY);
 		memcpy(pbOut, pbPoint + cbSize / 2 + 1, (int)(cbSize / 2));
 		p = cn_cbor_data_create2(pbOut, (int)(cbSize / 2), 0,
@@ -1379,7 +1387,7 @@
 	psig = ECDSA_do_sign(rgbDigest, cbDigest, eckey);
 	CHECK_CONDITION(psig != NULL, COSE_ERR_CRYPTO_FAIL);
 
-	pbSig = COSE_CALLOC(cbR, 2, context);
+	pbSig = (byte *) COSE_CALLOC(cbR, 2, context);
 	CHECK_CONDITION(pbSig != NULL, COSE_ERR_OUT_OF_MEMORY);
 
 	const BIGNUM *r;
@@ -1561,7 +1569,7 @@
 		COSE_ERR_CRYPTO_FAIL);
 	keyCtx = NULL;
 
-	pbSig = COSE_CALLOC(cbSig, 1, context);
+	pbSig = (byte *) COSE_CALLOC(cbSig, 1, context);
 	CHECK_CONDITION(pbSig != NULL, COSE_ERR_OUT_OF_MEMORY);
 
 	size_t cb2 = cbSig;
@@ -1702,7 +1710,7 @@
 #endif
 	cn_cbor *cnTmp = NULL;
 
-	pbOut = COSE_CALLOC(cbContent + 8, 1, context);
+	pbOut = (byte *) COSE_CALLOC(cbContent + 8, 1, context);
 	CHECK_CONDITION(pbOut != NULL, COSE_ERR_OUT_OF_MEMORY);
 
 	CHECK_CONDITION(
@@ -1794,7 +1802,7 @@
 		}
 	}
 
-	pbsecret = COSE_CALLOC(cbGroup, 1, context);
+	pbsecret = (byte *) COSE_CALLOC(cbGroup, 1, context);
 	CHECK_CONDITION(pbsecret != NULL, COSE_ERR_OUT_OF_MEMORY);
 
 	cbsecret = ECDH_compute_key(pbsecret, cbGroup,
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 18dd494..382ee14 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -1,6 +1,6 @@
 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${dist_dir}/test)
 
-add_executable(cose_test test.c json.c encrypt.c sign.c context.c mac_test.c CounterSign_test.c)
+add_executable(cose_test test.cpp json.cpp Encrypt_test.cpp Sign_test.cpp context.cpp Mac_test.cpp CounterSign_test.cpp)
 
 target_link_libraries(cose_test PRIVATE cose-c::cose-c cn-cbor::cn-cbor)
 
diff --git a/test/CounterSign_test.c b/test/CounterSign_test.cpp
similarity index 96%
rename from test/CounterSign_test.c
rename to test/CounterSign_test.cpp
index c1b02f5..c3dfb2b 100644
--- a/test/CounterSign_test.c
+++ b/test/CounterSign_test.cpp
@@ -27,8 +27,8 @@
 	hCounterSign = COSE_CounterSign_Init(CBOR_CONTEXT_PARAM_COMMA NULL);
 	hBadHandle = (HCOSE_COUNTERSIGN)COSE_CALLOC(1, sizeof(COSE), context);
 #if INCLUDE_SIGN1
-	HCOSE_SIGN1 hSign1 =
-		COSE_Sign1_Init(0, CBOR_CONTEXT_PARAM_COMMA & cose_error);
+	HCOSE_SIGN1 hSign1 = COSE_Sign1_Init(
+		COSE_INIT_FLAGS_NONE, CBOR_CONTEXT_PARAM_COMMA & cose_error);
 #endif
 	//  Look for invalid parameter
 	//		Null handle checks
diff --git a/test/encrypt.c b/test/Encrypt_test.cpp
similarity index 92%
rename from test/encrypt.c
rename to test/Encrypt_test.cpp
index 5ebe131..c40fbfd 100644
--- a/test/encrypt.c
+++ b/test/Encrypt_test.cpp
@@ -41,6 +41,21 @@
 	cn_cbor *pkey = NULL;
 	bool fNoSupport = false;
 
+	if (false) {
+	errorReturn:
+		if (hEnc != NULL) {
+			COSE_Enveloped_Free(hEnc);
+		}
+		if (hRecip1 != NULL) {
+			COSE_Recipient_Free(hRecip1);
+		}
+		if (hRecip2 != NULL) {
+			COSE_Recipient_Free(hRecip2);
+		}
+
+		return fRet;		
+	}
+	
 	hEnc = (HCOSE_ENVELOPED)COSE_Decode(pbEncoded, cbEncoded, &type,
 		COSE_enveloped_object, CBOR_CONTEXT_PARAM_COMMA & cose_err);
 	if (hEnc == NULL) {
@@ -314,19 +329,7 @@
 	if (!fRet && !fNoSupport) {
 		CFails++;
 	}
-
-errorReturn:
-	if (hEnc != NULL) {
-		COSE_Enveloped_Free(hEnc);
-	}
-	if (hRecip1 != NULL) {
-		COSE_Recipient_Free(hRecip1);
-	}
-	if (hRecip2 != NULL) {
-		COSE_Recipient_Free(hRecip2);
-	}
-
-	return fRet;
+	goto errorReturn;
 }
 
 int _ValidateEnveloped(const cn_cbor *pControl,
@@ -397,10 +400,12 @@
 
 HCOSE_RECIPIENT BuildRecipient(const cn_cbor *pRecipient)
 {
-	HCOSE_RECIPIENT hRecip =
-		COSE_Recipient_Init(0, CBOR_CONTEXT_PARAM_COMMA NULL);
+	HCOSE_RECIPIENT hRecip = COSE_Recipient_Init(
+		COSE_INIT_FLAGS_NONE, CBOR_CONTEXT_PARAM_COMMA NULL);
 	if (hRecip == NULL) {
-		goto returnError;
+	returnError:
+		COSE_Recipient_Free(hRecip);
+		return NULL;
 	}
 
 	if (!SetSendingAttributes(
@@ -485,10 +490,6 @@
 #endif
 
 	return hRecip;
-
-returnError:
-	COSE_Recipient_Free(hRecip);
-	return NULL;
 }
 
 int BuildEnvelopedMessage(const cn_cbor *pControl)
@@ -504,12 +505,18 @@
 		return 0;
 	}
 
-	HCOSE_ENVELOPED hEncObj =
-		COSE_Enveloped_Init(0, CBOR_CONTEXT_PARAM_COMMA NULL);
+	HCOSE_ENVELOPED hEncObj = COSE_Enveloped_Init(
+		COSE_INIT_FLAGS_NONE, CBOR_CONTEXT_PARAM_COMMA NULL);
 
 	const cn_cbor *pInputs = cn_cbor_mapget_string(pControl, "input");
 	if (pInputs == NULL) {
-		goto returnError;
+	returnError:
+		if (hEncObj != NULL) {
+			COSE_Enveloped_Free(hEncObj);
+		}
+
+		CFails += 1;
+		return 0;
 	}
 	const cn_cbor *pEnveloped = cn_cbor_mapget_string(pInputs, "enveloped");
 	if (pEnveloped == NULL) {
@@ -611,20 +618,12 @@
 	int f = _ValidateEnveloped(pControl, rgb, cb);
 	free(rgb);
 	return f;
-
-returnError:
-	if (hEncObj != NULL) {
-		COSE_Enveloped_Free(hEncObj);
-	}
-
-	CFails += 1;
-	return 0;
 }
 
 int EncryptMessage()
 {
-	HCOSE_ENVELOPED hEncObj =
-		COSE_Enveloped_Init(0, CBOR_CONTEXT_PARAM_COMMA NULL);
+	HCOSE_ENVELOPED hEncObj = COSE_Enveloped_Init(
+		COSE_INIT_FLAGS_NONE, CBOR_CONTEXT_PARAM_COMMA NULL);
 	byte rgbSecret[128 / 8] = {'a', 'b', 'c'};
 	int cbSecret = 128 / 8;
 	byte rgbKid[15] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'l',
@@ -632,11 +631,19 @@
 	int cbKid = 6;
 	size_t cb;
 	byte *rgb;
-	char *sz = "This is the content to be used";
+	const char *sz = "This is the content to be used";
 	HCOSE_RECIPIENT hRecip = NULL;
 
 	if (hEncObj == NULL) {
-		goto errorReturn;
+	errorReturn:
+		if (hEncObj != NULL) {
+			COSE_Enveloped_Free(hEncObj);
+		}
+		if (hRecip != NULL) {
+			COSE_Recipient_Free(hRecip);
+		}
+		CFails++;
+		return 0;
 	}
 	if (!COSE_Enveloped_map_put_int(hEncObj, COSE_Header_Algorithm,
 			cn_cbor_int_create(COSE_Algorithm_AES_CCM_16_64_128,
@@ -734,16 +741,6 @@
 
 	COSE_Enveloped_Free(hEncObj);
 	return 1;
-
-errorReturn:
-	if (hEncObj != NULL) {
-		COSE_Enveloped_Free(hEncObj);
-	}
-	if (hRecip != NULL) {
-		COSE_Recipient_Free(hRecip);
-	}
-	CFails++;
-	return 0;
 }
 #endif
 
@@ -766,6 +763,43 @@
 	bool fFailBody = false;
 	bool fAlgSupport = true;
 
+	if (false) {
+	exitHere:
+		if (hEnc != NULL) {
+			COSE_Encrypt_Free(hEnc);
+		}
+		if (pkey != NULL) {
+			CN_CBOR_FREE(pkey, context);
+		}
+
+		if (fAlgSupport) {
+			if (fFailBody) {
+				if (!fFail) {
+					fFail = true;
+				}
+				else {
+					fFail = false;
+				}
+			}
+		}
+		else {
+			fFail = false;
+		}
+
+		if (fFail) {
+			CFails += 1;
+		}
+		return fAlgSupport ? 1 : 0;
+
+	returnError:
+		if (hEnc != NULL) {
+			COSE_Encrypt_Free(hEnc);
+		}
+
+		CFails += 1;
+		return 0;		
+	}
+
 	pFail = cn_cbor_mapget_string(pControl, "fail");
 	if ((pFail != NULL) && (pFail->type == CN_CBOR_TRUE)) {
 		fFailBody = true;
@@ -933,40 +967,7 @@
 	}
 #endif
 
-exitHere:
-	if (hEnc != NULL) {
-		COSE_Encrypt_Free(hEnc);
-	}
-	if (pkey != NULL) {
-		CN_CBOR_FREE(pkey, context);
-	}
-
-	if (fAlgSupport) {
-		if (fFailBody) {
-			if (!fFail) {
-				fFail = true;
-			}
-			else {
-				fFail = false;
-			}
-		}
-	}
-	else {
-		fFail = false;
-	}
-
-	if (fFail) {
-		CFails += 1;
-	}
-	return fAlgSupport ? 1 : 0;
-
-returnError:
-	if (hEnc != NULL) {
-		COSE_Encrypt_Free(hEnc);
-	}
-
-	CFails += 1;
-	return 0;
+	goto exitHere;
 }
 
 int ValidateEncrypt(const cn_cbor *pControl)
@@ -1001,11 +1002,22 @@
 		return 0;
 	}
 
-	HCOSE_ENCRYPT hEncObj = COSE_Encrypt_Init(0, CBOR_CONTEXT_PARAM_COMMA NULL);
+	HCOSE_ENCRYPT hEncObj =
+		COSE_Encrypt_Init(COSE_INIT_FLAGS_NONE, CBOR_CONTEXT_PARAM_COMMA NULL);
 
 	const cn_cbor *pInputs = cn_cbor_mapget_string(pControl, "input");
 	if (pInputs == NULL) {
-		goto returnError;
+	returnError:
+		if (hEncObj != NULL) {
+			COSE_Encrypt_Free(hEncObj);
+		}
+
+		if (pkey != NULL) {
+			CN_CBOR_FREE(pkey, context);
+		}
+
+		CFails += 1;
+		return 1;
 	}
 	const cn_cbor *pEncrypt = cn_cbor_mapget_string(pInputs, "encrypted");
 	if (pEncrypt == NULL) {
@@ -1102,18 +1114,6 @@
 	
 	free(rgb);
 	return f;
-
-returnError:
-	if (hEncObj != NULL) {
-		COSE_Encrypt_Free(hEncObj);
-	}
-
-	if (pkey != NULL) {
-		CN_CBOR_FREE(pkey, context);
-	}
-
-	CFails += 1;
-	return 1;
 }
 #endif
 
@@ -1130,17 +1130,19 @@
 	cn_cbor *cn = cn_cbor_int_create(5, CBOR_CONTEXT_PARAM_COMMA NULL);
 	cose_errback cose_error;
 
-	hEncrypt = COSE_Enveloped_Init(0, CBOR_CONTEXT_PARAM_COMMA NULL);
+	hEncrypt = COSE_Enveloped_Init(
+		COSE_INIT_FLAGS_NONE, CBOR_CONTEXT_PARAM_COMMA NULL);
 #if INCLUDE_MAC
-	hEncryptBad =
-		(HCOSE_ENVELOPED)COSE_Mac_Init(0, CBOR_CONTEXT_PARAM_COMMA NULL);
+	hEncryptBad = (HCOSE_ENVELOPED)COSE_Mac_Init(
+		COSE_INIT_FLAGS_NONE, CBOR_CONTEXT_PARAM_COMMA NULL);
 #else
 	hEncryptBad = (HCOSE_ENVELOPED)COSE_CALLOC(1, sizeof(COSE), context);
 #endif
-	hRecipient = COSE_Recipient_Init(0, CBOR_CONTEXT_PARAM_COMMA NULL);
+	hRecipient = COSE_Recipient_Init(
+		COSE_INIT_FLAGS_NONE, CBOR_CONTEXT_PARAM_COMMA NULL);
 #if INCLUDE_MAC
-	hRecipientBad =
-		(HCOSE_RECIPIENT)COSE_Mac_Init(0, CBOR_CONTEXT_PARAM_COMMA NULL);
+	hRecipientBad = (HCOSE_RECIPIENT)COSE_Mac_Init(
+		COSE_INIT_FLAGS_NONE, CBOR_CONTEXT_PARAM_COMMA NULL);
 #else
 	hRecipientBad = (HCOSE_RECIPIENT)COSE_CALLOC(1, sizeof(COSE), context);
 #endif
@@ -1233,7 +1235,8 @@
 
 	//  Bad Int algorithm
 
-	hEncrypt = COSE_Enveloped_Init(0, CBOR_CONTEXT_PARAM_COMMA NULL);
+	hEncrypt = COSE_Enveloped_Init(
+		COSE_INIT_FLAGS_NONE, CBOR_CONTEXT_PARAM_COMMA NULL);
 	if (hEncrypt == NULL) {
 		CFails++;
 	}
@@ -1256,7 +1259,8 @@
 	COSE_Recipient_Free(hRecipient);
 	COSE_Enveloped_Free(hEncrypt);
 
-	hEncrypt = COSE_Enveloped_Init(0, CBOR_CONTEXT_PARAM_COMMA NULL);
+	hEncrypt = COSE_Enveloped_Init(
+		COSE_INIT_FLAGS_NONE, CBOR_CONTEXT_PARAM_COMMA NULL);
 	if (hEncrypt == NULL) {
 		CFails++;
 	}
@@ -1330,7 +1334,8 @@
 	//  Wrong type of handle checks
 
 #if INCLUDE_MAC
-	hEncrypt = (HCOSE_ENCRYPT)COSE_Mac_Init(0, CBOR_CONTEXT_PARAM_COMMA NULL);
+	hEncrypt = (HCOSE_ENCRYPT)COSE_Mac_Init(
+		COSE_INIT_FLAGS_NONE, CBOR_CONTEXT_PARAM_COMMA NULL);
 #else
 	hEncrypt = (HCOSE_ENCRYPT)COSE_CALLOC(1, sizeof(COSE), context);
 #endif
@@ -1360,7 +1365,8 @@
 	//
 	//  Unsupported algorithm
 
-	hEncrypt = COSE_Encrypt_Init(0, CBOR_CONTEXT_PARAM_COMMA NULL);
+	hEncrypt =
+		COSE_Encrypt_Init(COSE_INIT_FLAGS_NONE, CBOR_CONTEXT_PARAM_COMMA NULL);
 	if (hEncrypt == NULL) {
 		CFails++;
 	}
@@ -1376,7 +1382,8 @@
 		COSE_ERR_UNKNOWN_ALGORITHM, CFails++);
 	COSE_Encrypt_Free(hEncrypt);
 
-	hEncrypt = COSE_Encrypt_Init(0, CBOR_CONTEXT_PARAM_COMMA NULL);
+	hEncrypt =
+		COSE_Encrypt_Init(COSE_INIT_FLAGS_NONE, CBOR_CONTEXT_PARAM_COMMA NULL);
 	if (hEncrypt == NULL) {
 		CFails++;
 	}
@@ -1406,7 +1413,8 @@
 	byte rgb[10];
 	cn_cbor *cn = cn_cbor_int_create(1, CBOR_CONTEXT_PARAM_COMMA NULL);
 
-	hRecip = COSE_Recipient_Init(0, CBOR_CONTEXT_PARAM_COMMA & cose_error);
+	hRecip = COSE_Recipient_Init(
+		COSE_INIT_FLAGS_NONE, CBOR_CONTEXT_PARAM_COMMA & cose_error);
 #if INCLUDE_SIGN
 	hRecipBad = (HCOSE_RECIPIENT)COSE_Signer_Init(
 		CBOR_CONTEXT_PARAM_COMMA & cose_error);
@@ -1488,9 +1496,10 @@
 
 	//  Unknown algorithms
 #if INCLUDE_ENCRYPT
-	HCOSE_ENVELOPED hEnv =
-		COSE_Enveloped_Init(0, CBOR_CONTEXT_PARAM_COMMA NULL);
-	hRecip = COSE_Recipient_Init(0, CBOR_CONTEXT_PARAM_COMMA NULL);
+	HCOSE_ENVELOPED hEnv = COSE_Enveloped_Init(
+		COSE_INIT_FLAGS_NONE, CBOR_CONTEXT_PARAM_COMMA NULL);
+	hRecip = COSE_Recipient_Init(
+		COSE_INIT_FLAGS_NONE, CBOR_CONTEXT_PARAM_COMMA NULL);
 
 	CHECK_RETURN(COSE_Enveloped_map_put_int(hEnv, COSE_Header_Algorithm,
 					 cn_cbor_int_create(COSE_Algorithm_AES_GCM_128,
@@ -1512,8 +1521,10 @@
 	COSE_Enveloped_Free(hEnv);
 	COSE_Recipient_Free(hRecip);
 
-	hEnv = COSE_Enveloped_Init(0, CBOR_CONTEXT_PARAM_COMMA NULL);
-	hRecip = COSE_Recipient_Init(0, CBOR_CONTEXT_PARAM_COMMA NULL);
+	hEnv = COSE_Enveloped_Init(
+		COSE_INIT_FLAGS_NONE, CBOR_CONTEXT_PARAM_COMMA NULL);
+	hRecip = COSE_Recipient_Init(
+		COSE_INIT_FLAGS_NONE, CBOR_CONTEXT_PARAM_COMMA NULL);
 
 	CHECK_RETURN(COSE_Enveloped_map_put_int(hEnv, COSE_Header_Algorithm,
 					 cn_cbor_int_create(COSE_Algorithm_AES_GCM_128,
diff --git a/test/mac_test.c b/test/Mac_test.cpp
similarity index 93%
rename from test/mac_test.c
rename to test/Mac_test.cpp
index efaa0d9..8cc265b 100644
--- a/test/mac_test.c
+++ b/test/Mac_test.cpp
@@ -45,7 +45,10 @@
 		if (fFailBody) {
 			return 0;
 		}
-		goto failTest;
+
+	failTest:
+		CFails += 1;
+		return 0;
 	}
 
 	if ((pInput == NULL) || (pInput->type != CN_CBOR_MAP)) {
@@ -304,10 +307,6 @@
 		CFails += 1;
 	}
 	return returnCode;
-
-failTest:
-	CFails += 1;
-	return 0;
 }
 
 int ValidateMAC(const cn_cbor *pControl)
@@ -332,11 +331,20 @@
 		return 0;
 	}
 
-	HCOSE_MAC hMacObj = COSE_Mac_Init(0, CBOR_CONTEXT_PARAM_COMMA NULL);
+	HCOSE_MAC hMacObj =
+		COSE_Mac_Init(COSE_INIT_FLAGS_NONE, CBOR_CONTEXT_PARAM_COMMA NULL);
 
 	const cn_cbor *pInputs = cn_cbor_mapget_string(pControl, "input");
 	if (pInputs == NULL) {
-		goto returnError;
+	returnError:
+		if (hMacObj != NULL) {
+			COSE_Mac_Free(hMacObj);
+		}
+		if (hRecip != NULL) {
+			COSE_Recipient_Free(hRecip);
+		}
+		CFails += 1;
+		return 1;
 	}
 	const cn_cbor *pMac = cn_cbor_mapget_string(pInputs, "mac");
 	if (pMac == NULL) {
@@ -367,7 +375,8 @@
 			goto returnError;
 		}
 
-		hRecip = COSE_Recipient_Init(0, CBOR_CONTEXT_PARAM_COMMA NULL);
+		hRecip = COSE_Recipient_Init(
+			COSE_INIT_FLAGS_NONE, CBOR_CONTEXT_PARAM_COMMA NULL);
 		if (hRecip == NULL) {
 			goto returnError;
 		}
@@ -495,21 +504,12 @@
 
 	free(rgb);
 	return f;
-
-returnError:
-	if (hMacObj != NULL) {
-		COSE_Mac_Free(hMacObj);
-	}
-	if (hRecip != NULL) {
-		COSE_Recipient_Free(hRecip);
-	}
-	CFails += 1;
-	return 1;
 }
 
 int MacMessage()
 {
-	HCOSE_MAC hEncObj = COSE_Mac_Init(0, CBOR_CONTEXT_PARAM_COMMA NULL);
+	HCOSE_MAC hEncObj =
+		COSE_Mac_Init(COSE_INIT_FLAGS_NONE, CBOR_CONTEXT_PARAM_COMMA NULL);
 	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'};
@@ -518,7 +518,12 @@
 	byte *rgb = NULL;
 
 	if (hEncObj == NULL) {
-		goto errorReturn;
+		errorReturn:
+		if (hEncObj != NULL) {
+			COSE_Mac_Free(hEncObj);
+		}
+		CFails++;
+		return 0;
 	}
 
 	if (!COSE_Mac_map_put_int(hEncObj, COSE_Header_Algorithm,
@@ -609,10 +614,6 @@
 	COSE_Mac_Free(hEncObj);
 
 	return 1;
-
-errorReturn:
-	CFails++;
-	return 1;
 }
 #endif
 
@@ -632,6 +633,28 @@
 	bool fFailBody = false;
 	bool fUnsuportedAlg = false;
 
+	if (false) {
+	exitHere:
+		if (pkey != NULL) {
+			CN_CBOR_FREE(pkey, context);
+		}
+		if (hMAC != NULL) {
+			COSE_Mac0_Free(hMAC);
+		}
+
+		if (fFail) {
+			CFails += 1;
+		}
+		return fUnsuportedAlg ? 0 : 1;
+
+	errorReturn:
+		if (hMAC != NULL) {
+			COSE_Mac0_Free(hMAC);
+		}
+		CFails += 1;
+		return (fFail || fUnsuportedAlg) ? 0 : 1;		
+	}
+	
 	pFail = cn_cbor_mapget_string(pControl, "fail");
 	if ((pFail != NULL) && (pFail->type == CN_CBOR_TRUE)) {
 		fFailBody = true;
@@ -782,26 +805,7 @@
 			fFail = false;
 		}
 	}
-
-exitHere:
-	if (pkey != NULL) {
-		CN_CBOR_FREE(pkey, context);
-	}
-	if (hMAC != NULL) {
-		COSE_Mac0_Free(hMAC);
-	}
-
-	if (fFail) {
-		CFails += 1;
-	}
-	return fUnsuportedAlg ? 0 : 1;
-
-errorReturn:
-	if (hMAC != NULL) {
-		COSE_Mac0_Free(hMAC);
-	}
-	CFails += 1;
-	return (fFail || fUnsuportedAlg) ? 0 : 1;
+	goto exitHere;
 }
 
 int ValidateMac0(const cn_cbor *pControl)
@@ -825,11 +829,18 @@
 		return 0;
 	}
 
-	HCOSE_MAC0 hMacObj = COSE_Mac0_Init(0, CBOR_CONTEXT_PARAM_COMMA NULL);
+	HCOSE_MAC0 hMacObj =
+		COSE_Mac0_Init(COSE_INIT_FLAGS_NONE, CBOR_CONTEXT_PARAM_COMMA NULL);
 
 	const cn_cbor *pInputs = cn_cbor_mapget_string(pControl, "input");
 	if (pInputs == NULL) {
-		goto returnError;
+	returnError:
+		if (pkey != NULL) {
+			CN_CBOR_FREE(pkey, context);
+		}
+		COSE_Mac0_Free(hMacObj);
+		CFails += 1;
+		return 1;
 	}
 	const cn_cbor *pMac = cn_cbor_mapget_string(pInputs, "mac0");
 	if (pMac == NULL) {
@@ -918,14 +929,6 @@
 
 	free(rgb);
 	return f;
-
-returnError:
-	if (pkey != NULL) {
-		CN_CBOR_FREE(pkey, context);
-	}
-	COSE_Mac0_Free(hMacObj);
-	CFails += 1;
-	return 1;
 }
 #endif
 
@@ -943,7 +946,8 @@
 
 	//  Incorrect algorithm
 
-	hMAC = (HCOSE_MAC)COSE_Mac_Init(0, CBOR_CONTEXT_PARAM_COMMA NULL);
+	hMAC = (HCOSE_MAC)COSE_Mac_Init(
+		COSE_INIT_FLAGS_NONE, CBOR_CONTEXT_PARAM_COMMA NULL);
 
 	//  Invalid Handle checks
 
@@ -978,7 +982,8 @@
 	}
 
 #if INCLUDE_ENCRYPT0
-	hEncrypt = COSE_Encrypt_Init(0, CBOR_CONTEXT_PARAM_COMMA NULL);
+	hEncrypt =
+		COSE_Encrypt_Init(COSE_INIT_FLAGS_NONE, CBOR_CONTEXT_PARAM_COMMA NULL);
 #else
 	hEncrypt = (HCOSE_ENCRYPT)COSE_CALLOC(1, sizeof(COSE), context);
 #endif
@@ -1016,7 +1021,7 @@
 	//
 	//  Unsupported algorithm
 
-	hMAC = COSE_Mac_Init(0, CBOR_CONTEXT_PARAM_COMMA NULL);
+	hMAC = COSE_Mac_Init(COSE_INIT_FLAGS_NONE, CBOR_CONTEXT_PARAM_COMMA NULL);
 	if (hMAC == NULL) {
 		CFails++;
 	}
@@ -1041,7 +1046,7 @@
 	COSE_Mac_Free(hMAC);
 	COSE_Recipient_Free(hRecipient);
 
-	hMAC = COSE_Mac_Init(0, CBOR_CONTEXT_PARAM_COMMA NULL);
+	hMAC = COSE_Mac_Init(COSE_INIT_FLAGS_NONE, CBOR_CONTEXT_PARAM_COMMA NULL);
 	if (hMAC == NULL) {
 		CFails++;
 	}
@@ -1081,7 +1086,8 @@
 	cn_cbor *cn = cn_cbor_int_create(5, CBOR_CONTEXT_PARAM_COMMA NULL);
 	cose_errback cose_error;
 
-	hEncrypt = COSE_Encrypt_Init(0, CBOR_CONTEXT_PARAM_COMMA NULL);
+	hEncrypt =
+		COSE_Encrypt_Init(COSE_INIT_FLAGS_NONE, CBOR_CONTEXT_PARAM_COMMA NULL);
 
 	//  Missing case - addref then release on item
 
@@ -1110,7 +1116,8 @@
 		CFails++;
 	}
 
-	hEncrypt = COSE_Encrypt_Init(0, CBOR_CONTEXT_PARAM_COMMA NULL);
+	hEncrypt =
+		COSE_Encrypt_Init(COSE_INIT_FLAGS_NONE, CBOR_CONTEXT_PARAM_COMMA NULL);
 
 	if (COSE_Mac0_SetContent((HCOSE_MAC0)hEncrypt, rgb, 10, NULL)) {
 		CFails++;
@@ -1139,7 +1146,7 @@
 	//
 	//  Unsupported algorithm
 
-	hMAC = COSE_Mac0_Init(0, CBOR_CONTEXT_PARAM_COMMA NULL);
+	hMAC = COSE_Mac0_Init(COSE_INIT_FLAGS_NONE, CBOR_CONTEXT_PARAM_COMMA NULL);
 	if (hMAC == NULL) {
 		CFails++;
 	}
@@ -1155,7 +1162,7 @@
 		COSE_ERR_UNKNOWN_ALGORITHM, CFails++);
 	COSE_Mac0_Free(hMAC);
 
-	hMAC = COSE_Mac0_Init(0, CBOR_CONTEXT_PARAM_COMMA NULL);
+	hMAC = COSE_Mac0_Init(COSE_INIT_FLAGS_NONE, CBOR_CONTEXT_PARAM_COMMA NULL);
 	if (hMAC == NULL) {
 		CFails++;
 	}
diff --git a/test/sign.c b/test/Sign_test.cpp
similarity index 94%
rename from test/sign.c
rename to test/Sign_test.cpp
index eedb4e6..242c98e 100644
--- a/test/sign.c
+++ b/test/Sign_test.cpp
@@ -317,11 +317,21 @@
 		return 0;
 	}
 
-	HCOSE_SIGN hSignObj = COSE_Sign_Init(0, CBOR_CONTEXT_PARAM_COMMA NULL);
+	HCOSE_SIGN hSignObj =
+		COSE_Sign_Init(COSE_INIT_FLAGS_NONE, CBOR_CONTEXT_PARAM_COMMA NULL);
 
 	const cn_cbor *pInputs = cn_cbor_mapget_string(pControl, "input");
 	if (pInputs == NULL) {
-		goto returnError;
+	returnError:
+		if (hSignObj != NULL) {
+			COSE_Sign_Free(hSignObj);
+		}
+		if (hSigner != NULL) {
+			COSE_Signer_Free(hSigner);
+		}
+
+		CFails += 1;
+		return 1;
 	}
 	const cn_cbor *pSign = cn_cbor_mapget_string(pInputs, "sign");
 	if (pSign == NULL) {
@@ -469,22 +479,12 @@
 
 	free(rgb);
 	return f;
-
-returnError:
-	if (hSignObj != NULL) {
-		COSE_Sign_Free(hSignObj);
-	}
-	if (hSigner != NULL) {
-		COSE_Signer_Free(hSigner);
-	}
-
-	CFails += 1;
-	return 1;
 }
 
 int SignMessage()
 {
-	HCOSE_SIGN hEncObj = COSE_Sign_Init(0, CBOR_CONTEXT_PARAM_COMMA NULL);
+	HCOSE_SIGN hEncObj =
+		COSE_Sign_Init(COSE_INIT_FLAGS_NONE, CBOR_CONTEXT_PARAM_COMMA NULL);
 	char *sz = "This is the content to be used";
 	size_t cb;
 	byte *rgb;
@@ -594,6 +594,29 @@
 	bool fFailBody = false;
 	bool fNoAlgSupport = false;
 
+	if (false) {
+	exitHere:
+		if (hSig != NULL) {
+			COSE_Sign1_Free(hSig);
+		}
+
+		if (fFail) {
+			CFails += 1;
+		}
+		if (pkey != NULL) {
+			CN_CBOR_FREE(pkey, context);
+		}
+		return fNoAlgSupport ? 0 : 1;
+
+	returnError:
+		if (hSig != NULL) {
+			COSE_Sign1_Free(hSig);
+		}
+
+		CFails += 1;
+		return 0;		
+	}
+
 	pFail = cn_cbor_mapget_string(pControl, "fail");
 	if ((pFail != NULL) && (pFail->type == CN_CBOR_TRUE)) {
 		fFailBody = true;
@@ -735,27 +758,7 @@
 			fFail = false;
 		}
 	}
-
-exitHere:
-	if (hSig != NULL) {
-		COSE_Sign1_Free(hSig);
-	}
-
-	if (fFail) {
-		CFails += 1;
-	}
-	if (pkey != NULL) {
-		CN_CBOR_FREE(pkey, context);
-	}
-	return fNoAlgSupport ? 0 : 1;
-
-returnError:
-	if (hSig != NULL) {
-		COSE_Sign1_Free(hSig);
-	}
-
-	CFails += 1;
-	return 0;
+	goto exitHere;
 }
 
 int ValidateSign1(const cn_cbor *pControl)
@@ -778,11 +781,20 @@
 		return 0;
 	}
 
-	HCOSE_SIGN1 hSignObj = COSE_Sign1_Init(0, CBOR_CONTEXT_PARAM_COMMA NULL);
+	HCOSE_SIGN1 hSignObj =
+		COSE_Sign1_Init(COSE_INIT_FLAGS_NONE, CBOR_CONTEXT_PARAM_COMMA NULL);
 
 	const cn_cbor *pInputs = cn_cbor_mapget_string(pControl, "input");
 	if (pInputs == NULL) {
-		goto returnError;
+	returnError:
+		if (hSignObj != NULL) {
+			COSE_Sign1_Free(hSignObj);
+		}
+		if (pkey != NULL) {
+			CN_CBOR_FREE(pkey, context);
+		}
+		CFails += 1;
+		return 1;
 	}
 	const cn_cbor *pSign = cn_cbor_mapget_string(pInputs, "sign0");
 	if (pSign == NULL) {
@@ -865,16 +877,6 @@
 
 	free(rgb);
 	return f;
-
-returnError:
-	if (hSignObj != NULL) {
-		COSE_Sign1_Free(hSignObj);
-	}
-	if (pkey != NULL) {
-		CN_CBOR_FREE(pkey, context);
-	}
-	CFails += 1;
-	return 1;
 }
 #endif
 
@@ -891,17 +893,18 @@
 	cn_cbor *cn = cn_cbor_int_create(5, CBOR_CONTEXT_PARAM_COMMA NULL);
 	cose_errback cose_error;
 
-	hSign = COSE_Sign_Init(0, CBOR_CONTEXT_PARAM_COMMA NULL);
+	hSign = COSE_Sign_Init(COSE_INIT_FLAGS_NONE, CBOR_CONTEXT_PARAM_COMMA NULL);
 #if INCLUDE_SIGN1
-	hSignBad = (HCOSE_SIGN)COSE_Sign1_Init(0, CBOR_CONTEXT_PARAM_COMMA NULL);
+	hSignBad = (HCOSE_SIGN)COSE_Sign1_Init(
+		COSE_INIT_FLAGS_NONE, CBOR_CONTEXT_PARAM_COMMA NULL);
 #else
 	hSignBad = (HCOSE_SIGN)COSE_CALLOC(1, sizeof(COSE), context);
 #endif
 
 	hSigner = COSE_Signer_Init(CBOR_CONTEXT_PARAM_COMMA NULL);
 #if INCLUDE_ENCRYPT || INCLUDE_MAC
-	hSignerBad =
-		(HCOSE_SIGNER)COSE_Recipient_Init(0, CBOR_CONTEXT_PARAM_COMMA NULL);
+	hSignerBad = (HCOSE_SIGNER)COSE_Recipient_Init(
+		COSE_INIT_FLAGS_NONE, CBOR_CONTEXT_PARAM_COMMA NULL);
 #else
 	hSignerBad = (HCOSE_SIGNER)COSE_CALLOC(1, sizeof(COSE), context);
 #endif
@@ -1022,7 +1025,7 @@
 	//
 	//  Unsupported algorithm
 
-	hSign = COSE_Sign_Init(0, CBOR_CONTEXT_PARAM_COMMA NULL);
+	hSign = COSE_Sign_Init(COSE_INIT_FLAGS_NONE, CBOR_CONTEXT_PARAM_COMMA NULL);
 	if (hSign == NULL) {
 		CFails++;
 	}
@@ -1050,7 +1053,7 @@
 	COSE_Sign_Free(hSign);
 	COSE_Signer_Free(hSigner);
 
-	hSign = COSE_Sign_Init(0, CBOR_CONTEXT_PARAM_COMMA NULL);
+	hSign = COSE_Sign_Init(COSE_INIT_FLAGS_NONE, CBOR_CONTEXT_PARAM_COMMA NULL);
 	if (hSign == NULL) {
 		CFails++;
 	}
@@ -1102,9 +1105,11 @@
 	cn_cbor *cn = cn_cbor_int_create(5, CBOR_CONTEXT_PARAM_COMMA NULL);
 	cose_errback cose_error;
 
-	hSign = COSE_Sign1_Init(0, CBOR_CONTEXT_PARAM_COMMA NULL);
+	hSign =
+		COSE_Sign1_Init(COSE_INIT_FLAGS_NONE, CBOR_CONTEXT_PARAM_COMMA NULL);
 #if INCLUDE_SIGN
-	hSignBad = (HCOSE_SIGN1)COSE_Sign_Init(0, CBOR_CONTEXT_PARAM_COMMA NULL);
+	hSignBad = (HCOSE_SIGN1)COSE_Sign_Init(
+		COSE_INIT_FLAGS_NONE, CBOR_CONTEXT_PARAM_COMMA NULL);
 #else
 	hSignBad = (HCOSE_SIGN1)COSE_CALLOC(1, sizeof(COSE), context);
 #endif
@@ -1167,7 +1172,8 @@
 	//
 	//  Unsupported algorithm
 
-	hSign = COSE_Sign1_Init(0, CBOR_CONTEXT_PARAM_COMMA NULL);
+	hSign =
+		COSE_Sign1_Init(COSE_INIT_FLAGS_NONE, CBOR_CONTEXT_PARAM_COMMA NULL);
 	if (hSign == NULL) {
 		CFails++;
 	}
@@ -1185,7 +1191,8 @@
 		COSE_ERR_UNKNOWN_ALGORITHM, CFails++);
 	COSE_Sign1_Free(hSign);
 
-	hSign = COSE_Sign1_Init(0, CBOR_CONTEXT_PARAM_COMMA NULL);
+	hSign =
+		COSE_Sign1_Init(COSE_INIT_FLAGS_NONE, CBOR_CONTEXT_PARAM_COMMA NULL);
 	if (hSign == NULL) {
 		CFails++;
 	}
diff --git a/test/context.c b/test/context.cpp
similarity index 95%
rename from test/context.c
rename to test/context.cpp
index 136323e..87a9b2a 100644
--- a/test/context.c
+++ b/test/context.cpp
@@ -14,7 +14,7 @@
 typedef struct {
 	cn_cbor_context context;
 	byte *pFirst;
-	unsigned int iFailLeft;
+	int iFailLeft;
 	int allocCount;
 } MyContext;
 
@@ -112,9 +112,9 @@
 	memset(&pb->pad, 0xab, pb->size + 8);
 }
 
-cn_cbor_context *CreateContext(unsigned int iFailPoint)
+cn_cbor_context *CreateContext(int iFailPoint)
 {
-	MyContext *p = malloc(sizeof(MyContext));
+	MyContext *p = (MyContext*) malloc(sizeof(MyContext));
 
 	p->context.calloc_func = MyCalloc;
 	p->context.free_func = MyFree;
diff --git a/test/context.h b/test/context.h
index 601c66b..ff06772 100644
--- a/test/context.h
+++ b/test/context.h
@@ -2,7 +2,7 @@
 
 #ifdef USE_CBOR_CONTEXT
 
-cn_cbor_context* CreateContext();
+cn_cbor_context* CreateContext(int failNumber);
 void FreeContext(cn_cbor_context* pContext);
 int IsContextEmpty(cn_cbor_context* pContext);
 
diff --git a/test/json.c b/test/json.cpp
similarity index 94%
rename from test/json.c
rename to test/json.cpp
index 80dda58..d4ca613 100644
--- a/test/json.c
+++ b/test/json.cpp
@@ -152,7 +152,7 @@
 		return NULL;
 	}
 
-	rgch = malloc(8 * 1024);
+	rgch = (char*) malloc(8 * 1024);
 	cch = (int)fread(rgch, 1, 8 * 1024, fp);
 	fclose(fp);
 
@@ -175,7 +175,7 @@
 {
 	*output_length = 4 * ((input_length + 2) / 3);
 
-	char *encoded_data = malloc(*output_length);
+	char *encoded_data = (char*) malloc(*output_length);
 	if (encoded_data == NULL) {
 		return NULL;
 	}
@@ -212,7 +212,7 @@
 
 	if (input_length % 4 != 0) {
 		int c = 4 - (input_length % 4);
-		p = malloc(input_length + c);
+		p = (char*) malloc(input_length + c);
 		memcpy(p, data, input_length);
 		memcpy(p + input_length, "====", c);
 		input_length += c;
@@ -227,7 +227,7 @@
 		(*output_length)--;
 	}
 
-	unsigned char *decoded_data = malloc(*output_length);
+	unsigned char *decoded_data = (unsigned char *)malloc(*output_length);
 	if (decoded_data == NULL) {
 		if (p != NULL) {
 			free(p);
@@ -265,7 +265,7 @@
 
 static void build_decoding_table()
 {
-	decoding_table = malloc(256);
+	decoding_table = (unsigned char *) malloc(256);
 
 	for (int i = 0; i < 64; i++) {
 		decoding_table[(int)encoding_table[i]] = (unsigned char)i;
@@ -287,7 +287,7 @@
 
 	*output_length = input_length / 2;
 
-	unsigned char *decoded_data = malloc(*output_length);
+	unsigned char *decoded_data = (unsigned char *) malloc(*output_length);
 	if (decoded_data == NULL) {
 		return NULL;
 	}
diff --git a/test/test.c b/test/test.cpp
similarity index 97%
rename from test/test.c
rename to test/test.cpp
index bedc63e..c01e684 100644
--- a/test/test.c
+++ b/test/test.cpp
@@ -25,10 +25,14 @@
 #include "mbedtls/entropy.h"
 #endif
 
+#ifdef USE_CBOR_CONTEXT
+cn_cbor_context * context;
+#endif
+
 int CFails = 0;
 
 typedef struct _NameMap {
-	char* sz;
+	const char* sz;
 	int i;
 } NameMap;
 
@@ -122,7 +126,7 @@
 byte* FromHex(const char* rgch, int cch)
 {
 	// M00BUG - Why is this using malloc?  It does not get freed anyplace.
-	byte* pb = malloc(cch / 2);
+	byte* pb = (byte*) malloc(cch / 2);
 	const char* pb2 = rgch;
 
 	for (int i = 0; i < cch; i += 2) {
@@ -275,7 +279,7 @@
 	const cn_cbor* pOutputs = cn_cbor_mapget_string(pControl, "output");
 	byte* pb = NULL;
 	const byte* pb2;
-	int i;
+	size_t i;
 
 	if ((pOutputs == NULL) || (pOutputs->type != CN_CBOR_MAP)) {
 		fprintf(stderr, "Invalid output\n");
@@ -288,7 +292,7 @@
 		exit(1);
 	}
 
-	pb = malloc(pCBOR->length / 2);
+	pb = static_cast<byte*>(malloc(pCBOR->length / 2));
 	pb2 = pCBOR->v.bytes;
 
 	for (i = 0; i < pCBOR->length; i += 2) {
@@ -306,7 +310,7 @@
 #define OPERATION_HEX 4
 
 struct {
-	char* szKey;
+	const char* szKey;
 	int kty;
 	int operation;
 	int keyNew;
@@ -487,22 +491,22 @@
 
 	if (!SetAttributes(hMsg, cn_cbor_mapget_string(pIn, "protected"),
 			COSE_PROTECT_ONLY, base, true)) {
-		goto returnError;
+		return false;
 	}
 	if (!SetAttributes(hMsg, cn_cbor_mapget_string(pIn, "unprotected"),
 			COSE_UNPROTECT_ONLY, base, true)) {
-		goto returnError;
+		return false;
 	}
 	if (!SetAttributes(hMsg, cn_cbor_mapget_string(pIn, "unsent"),
 			COSE_DONT_SEND, base, false)) {
-		goto returnError;
+		return false;
 	}
 
 	cn_cbor* pExternal = cn_cbor_mapget_string(pIn, "external");
 	if (pExternal != NULL) {
 		cn_cbor* pcn = pExternal;
 		if (pcn == NULL) {
-			goto returnError;
+			return false;
 		}
 		switch (base) {
 #if INCLUDE_ENCRYPT0
@@ -510,7 +514,7 @@
 				if (!COSE_Encrypt_SetExternal((HCOSE_ENCRYPT)hMsg,
 						FromHex(pcn->v.str, (int)pcn->length), pcn->length / 2,
 						NULL)) {
-					goto returnError;
+					return false;
 				}
 				break;
 #endif
@@ -520,7 +524,7 @@
 				if (!COSE_Enveloped_SetExternal((HCOSE_ENVELOPED)hMsg,
 						FromHex(pcn->v.str, (int)pcn->length), pcn->length / 2,
 						NULL)) {
-					goto returnError;
+					return false;
 				}
 				break;
 #endif
@@ -530,7 +534,7 @@
 				if (!COSE_Mac_SetExternal((HCOSE_MAC)hMsg,
 						FromHex(pcn->v.str, (int)pcn->length), pcn->length / 2,
 						NULL)) {
-					goto returnError;
+					return false;
 				}
 				break;
 #endif
@@ -540,7 +544,7 @@
 				if (!COSE_Mac0_SetExternal((HCOSE_MAC0)hMsg,
 						FromHex(pcn->v.str, (int)pcn->length), pcn->length / 2,
 						NULL)) {
-					goto returnError;
+					return false;
 				}
 				break;
 #endif
@@ -550,7 +554,7 @@
 				if (!COSE_Signer_SetExternal((HCOSE_SIGNER)hMsg,
 						FromHex(pcn->v.str, (int)pcn->length), pcn->length / 2,
 						NULL)) {
-					goto returnError;
+					return false;
 				}
 				break;
 #endif
@@ -560,7 +564,7 @@
 				if (!COSE_Sign1_SetExternal((HCOSE_SIGN1)hMsg,
 						FromHex(pcn->v.str, (int)pcn->length), pcn->length / 2,
 						NULL)) {
-					goto returnError;
+					return false;
 				}
 				break;
 #endif
@@ -569,7 +573,7 @@
 				if (!COSE_CounterSign_SetExternal((HCOSE_COUNTERSIGN)hMsg,
 						FromHex(pcn->v.str, (int)pcn->length), pcn->length / 2,
 						NULL)) {
-					goto returnError;
+					return false;
 				}
 				break;
 #endif
@@ -579,9 +583,7 @@
 		}
 	}
 
-	f = true;
-returnError:
-	return f;
+	return true;
 }
 
 bool SetReceivingAttributes(HCOSE hMsg, const cn_cbor* pIn, int base)
@@ -590,14 +592,14 @@
 
 	if (!SetAttributes(hMsg, cn_cbor_mapget_string(pIn, "unsent"),
 			COSE_DONT_SEND, base, true)) {
-		goto returnError;
+		return false;
 	}
 
 	cn_cbor* pExternal = cn_cbor_mapget_string(pIn, "external");
 	if (pExternal != NULL) {
 		cn_cbor* pcn = pExternal;
 		if (pcn == NULL) {
-			goto returnError;
+			return false;
 		}
 		switch (base) {
 			
@@ -606,7 +608,7 @@
 				if (!COSE_Encrypt_SetExternal((HCOSE_ENCRYPT)hMsg,
 						FromHex(pcn->v.str, (int)pcn->length), pcn->length / 2,
 						NULL)) {
-					goto returnError;
+					return false;
 				}
 				break;
 #endif
@@ -616,7 +618,7 @@
 				if (!COSE_Enveloped_SetExternal((HCOSE_ENVELOPED)hMsg,
 						FromHex(pcn->v.str, (int)pcn->length), pcn->length / 2,
 						NULL)) {
-					goto returnError;
+					return false;
 				}
 				break;
 #endif
@@ -626,7 +628,7 @@
 				if (!COSE_Mac_SetExternal((HCOSE_MAC)hMsg,
 						FromHex(pcn->v.str, (int)pcn->length), pcn->length / 2,
 						NULL)) {
-					goto returnError;
+					return false;
 				}
 				break;
 #endif
@@ -636,7 +638,7 @@
 				if (!COSE_Mac0_SetExternal((HCOSE_MAC0)hMsg,
 						FromHex(pcn->v.str, (int)pcn->length), pcn->length / 2,
 						NULL)) {
-					goto returnError;
+					return false;
 				}
 				break;
 #endif
@@ -646,7 +648,7 @@
 				if (!COSE_Signer_SetExternal((HCOSE_SIGNER)hMsg,
 						FromHex(pcn->v.str, (int)pcn->length), pcn->length / 2,
 						NULL)) {
-					goto returnError;
+					return false;
 				}
 				break;
 #endif
@@ -656,7 +658,7 @@
 				if (!COSE_Sign1_SetExternal((HCOSE_SIGN1)hMsg,
 						FromHex(pcn->v.str, (int)pcn->length), pcn->length / 2,
 						NULL)) {
-					goto returnError;
+					return false;
 				}
 				break;
 #endif
@@ -665,16 +667,14 @@
 				if (!COSE_CounterSign_SetExternal((HCOSE_COUNTERSIGN)hMsg,
 						FromHex(pcn->v.str, (int)pcn->length), pcn->length / 2,
 						NULL)) {
-					goto returnError;
+					return false;
 				}
 				break;
 #endif
 		}
 	}
 
-	f = true;
-returnError:
-	return f;
+	return true;
 }
 
 cn_cbor* BuildKey(const cn_cbor* pKeyIn, bool fPublicKey)
@@ -875,7 +875,7 @@
 static void RunMemoryTest(const char* szFileName)
 {
 #ifdef USE_CBOR_CONTEXT
-	unsigned int iFail;
+	int iFail;
 	const cn_cbor* pControl = ParseJson(szFileName);
 
 	if (pControl == NULL) {
@@ -1306,10 +1306,12 @@
 
 		CFails = 0;
 		RunFileTest(rgchFullName);
-		if (CFails == 0)
+		if (CFails == 0) {
 			printf(" PASS\n");
-		else
+		}
+		else {
 			printf(" FAILED\n");
+		}
 		cFailTotal += CFails;
 	} while (FindNextFile(hFind, &FindFileData));
 
@@ -1423,7 +1425,7 @@
 	}
 	else {
 #ifdef USE_CBOR_CONTEXT
-		context = CreateContext((unsigned int)-1);
+		context = CreateContext(-1);
 #endif
 #if INCLUDE_MAC
 		MacMessage();
diff --git a/test/test.h b/test/test.h
index 300ee96..1da2d45 100644
--- a/test/test.h
+++ b/test/test.h
@@ -6,7 +6,7 @@
 #include "context.h"
 
 #ifdef USE_CBOR_CONTEXT
-cn_cbor_context* context;
+extern cn_cbor_context* context;
 #define CBOR_CONTEXT_PARAM , context
 #define CBOR_CONTEXT_PARAM_COMMA context,
 #else
@@ -47,7 +47,7 @@
 void MAC0_Corners();
 
 //  test.c
-enum {
+typedef enum {
 	Attributes_MAC_protected = 1,
 	Attributes_MAC0_protected,
 	Attributes_Recipient_protected,