Merge pull request #45 from jimsch/master

First pass at reducing memory usage
diff --git a/src/Cose.c b/src/Cose.c
index 930f42e..a30dc10 100644
--- a/src/Cose.c
+++ b/src/Cose.c
@@ -203,11 +203,10 @@
 	return NULL;
 }
 
-byte RgbDontUse[8 * 1024];   //  Remove this array when we can compute the size of a cbor serialization without this hack.
 
 size_t COSE_Encode(HCOSE msg, byte * rgb, size_t ib, size_t cb)
 {
-	if (rgb == NULL) return cn_cbor_encoder_write(RgbDontUse, 0, sizeof(RgbDontUse), ((COSE *)msg)->m_cbor) + ib;
+	if (rgb == NULL) return cn_cbor_encode_size(((COSE *)msg)->m_cbor) + ib;
 	return cn_cbor_encoder_write(rgb, ib, cb, ((COSE*)msg)->m_cbor);
 }
 
@@ -313,8 +312,6 @@
 	return f;
 }
 
-byte RgbDontUse3[1024];
-
 cn_cbor * _COSE_encode_protected(COSE * pMessage, cose_errback * perr)
 {
 	cn_cbor * pProtected;
@@ -332,7 +329,7 @@
 	}
 
 	if (pMessage->m_protectedMap->length > 0) {
-		cbProtected = cn_cbor_encoder_write(RgbDontUse3, 0, sizeof(RgbDontUse3), pMessage->m_protectedMap);
+		cbProtected = cn_cbor_encode_size(pMessage->m_protectedMap);
 		pbProtected = (byte *)COSE_CALLOC(cbProtected, 1, context);
 		CHECK_CONDITION(pbProtected != NULL, COSE_ERR_OUT_OF_MEMORY);
 
diff --git a/src/Encrypt.c b/src/Encrypt.c
index 408bdcc..089377a 100644
--- a/src/Encrypt.c
+++ b/src/Encrypt.c
@@ -14,8 +14,6 @@
 
 void _COSE_Enveloped_Release(COSE_Enveloped * p);
 
-byte RgbDontUse[8 * 1024];   //  Remove this array when we can compute the size of a cbor serialization without this hack.
-
 COSE * EnvelopedRoot = NULL;
 
 /*! \private
@@ -824,7 +822,7 @@
 	CHECK_CONDITION_CBOR(cn_cbor_array_append(pAuthData, ptmp, &cbor_error), cbor_error);
 	ptmp = NULL;
 
-	cbAuthData = cn_cbor_encoder_write(RgbDontUse, 0, sizeof(RgbDontUse), pAuthData);
+	cbAuthData = cn_cbor_encode_size(pAuthData);
 	pbAuthData = (byte *)COSE_CALLOC(cbAuthData, 1, context);
 	CHECK_CONDITION(pbAuthData != NULL, COSE_ERR_OUT_OF_MEMORY);
 	CHECK_CONDITION((size_t)cn_cbor_encoder_write(pbAuthData, 0, cbAuthData, pAuthData) == cbAuthData, COSE_ERR_CBOR);
diff --git a/src/Encrypt0.c b/src/Encrypt0.c
index 4103292..1a031d1 100644
--- a/src/Encrypt0.c
+++ b/src/Encrypt0.c
@@ -12,8 +12,6 @@
 #include "configure.h"
 #include "crypto.h"
 
-byte RgbDontUse[8 * 1024];   //  Remove this array when we can compute the size of a cbor serialization without this hack.
-
 void _COSE_Encrypt_Release(COSE_Encrypt * p);
 
 COSE * EncryptRoot = NULL;
diff --git a/src/MacMessage.c b/src/MacMessage.c
index 0c085c7..6fe434b 100644
--- a/src/MacMessage.c
+++ b/src/MacMessage.c
@@ -13,8 +13,6 @@
 #include "configure.h"
 #include "crypto.h"
 
-byte RgbDontUse2[8 * 1024];   //  Remove this array when we can compute the size of a cbor serialization without this hack.
-
 COSE * MacRoot = NULL;
 
 /*! \private
@@ -279,7 +277,7 @@
 	ptmp = NULL;
 
 	//  Turn it into bytes
-	cbAuthData = cn_cbor_encoder_write(RgbDontUse2, 0, sizeof(RgbDontUse2), pAuthData);
+	cbAuthData = cn_cbor_encode_size(pAuthData);
 	CHECK_CONDITION(cbAuthData > 0, COSE_ERR_CBOR);
 	pbAuthData = (byte *)COSE_CALLOC(cbAuthData, 1, context);
 	CHECK_CONDITION(pbAuthData != NULL, COSE_ERR_OUT_OF_MEMORY);
diff --git a/src/Recipient.c b/src/Recipient.c
index 1ba5f2c..92a7a08 100644
--- a/src/Recipient.c
+++ b/src/Recipient.c
@@ -1203,8 +1203,6 @@
 }
 
 
-byte RgbDontUse4[8 * 1024];
-
 bool BuildContextBytes(COSE * pcose, int algID, size_t cbitKey, byte ** ppbContext, size_t * pcbContext, CBOR_CONTEXT_COMMA cose_errback * perr)
 {
 	cn_cbor * pArray;
@@ -1335,7 +1333,7 @@
 		cnParam = NULL;
 	}
 
-	cbContext = cn_cbor_encoder_write(RgbDontUse4, 0, sizeof(RgbDontUse4), pArray);
+	cbContext = cn_cbor_encode_size(pArray);
 	CHECK_CONDITION(cbContext > 0, COSE_ERR_CBOR);
 	pbContext = (byte *)COSE_CALLOC(cbContext, 1, context);
 	CHECK_CONDITION(pbContext != NULL, COSE_ERR_OUT_OF_MEMORY);
diff --git a/src/Sign0.c b/src/Sign0.c
index b08ce5e..be725d5 100644
--- a/src/Sign0.c
+++ b/src/Sign0.c
@@ -246,9 +246,6 @@
 }
 
 
-byte RgbDontUse4[8 * 1024];
-byte RgbDontUseSign[8 * 1024];
-
 static bool CreateSign0AAD(COSE_Sign0Message * pMessage, byte ** ppbToSign, size_t * pcbToSign, char * szContext, cose_errback * perr)
 {
 	cn_cbor * pArray = NULL;
@@ -290,7 +287,7 @@
 	cn = NULL;
 
 
-	cbToSign = cn_cbor_encoder_write(RgbDontUse4, 0, sizeof(RgbDontUse4), pArray);
+	cbToSign = cn_cbor_encode_size(pArray);
 	CHECK_CONDITION(cbToSign > 0, COSE_ERR_CBOR);
 	pbToSign = (byte *)COSE_CALLOC(cbToSign, 1, context);
 	CHECK_CONDITION(pbToSign != NULL, COSE_ERR_OUT_OF_MEMORY);
diff --git a/src/SignerInfo.c b/src/SignerInfo.c
index 55dc346..f1a340e 100644
--- a/src/SignerInfo.c
+++ b/src/SignerInfo.c
@@ -106,8 +106,6 @@
 	return NULL;
 }
 
-byte RgbDontUse4[1024];
-
 bool BuildToBeSigned(byte ** ppbToSign, size_t * pcbToSign, const cn_cbor * pcborBody, const cn_cbor * pcborProtected, const cn_cbor * pcborProtectedSign, const byte * pbExternal, size_t cbExternal, CBOR_CONTEXT_COMMA cose_errback * perr)
 {
 	cn_cbor * pArray = NULL;
@@ -147,7 +145,7 @@
 	CHECK_CONDITION_CBOR(cn_cbor_array_append(pArray, cn, &cbor_error), cbor_error);
 	cn = NULL;
 
-	cbToSign = cn_cbor_encoder_write(RgbDontUse4, 0, sizeof(RgbDontUse4), pArray);
+	cbToSign = cn_cbor_encode_size(pArray);
 	CHECK_CONDITION(cbToSign > 0, COSE_ERR_CBOR);
 	pbToSign = (byte *)COSE_CALLOC(cbToSign, 1, context);
 	CHECK_CONDITION(pbToSign != NULL, COSE_ERR_OUT_OF_MEMORY);
diff --git a/src/cbor.c b/src/cbor.c
index a229b0f..b38a336 100644
--- a/src/cbor.c
+++ b/src/cbor.c
@@ -183,3 +183,11 @@
 	pcn->type = CN_CBOR_NULL;
 	return pcn;
 }
+
+
+unsigned char RgbDontUse4[8 * 1024];
+
+size_t cn_cbor_encode_size(cn_cbor * object)
+{
+	return cn_cbor_encoder_write(RgbDontUse4, 0, sizeof(RgbDontUse4), object);
+}
diff --git a/src/cose_int.h b/src/cose_int.h
index 8eeb483..e9149af 100644
--- a/src/cose_int.h
+++ b/src/cose_int.h
@@ -270,6 +270,7 @@
 bool cn_cbor_array_replace(cn_cbor * cb_array, cn_cbor * cb_value, int index, CBOR_CONTEXT_COMMA cn_cbor_errback *errp);
 cn_cbor * cn_cbor_bool_create(int boolValue, CBOR_CONTEXT_COMMA cn_cbor_errback * errp);
 
+extern size_t cn_cbor_encode_size(cn_cbor * object);
 
 enum {
 	COSE_Int_Alg_AES_CBC_MAC_256_64 = -22