From 0bfb0d15f3eeba2d7f974a6913a45035ad5d1f31 Mon Sep 17 00:00:00 2001
From: "Dmitriy A. Gerasimov" <dmitriy.gerasimov@demlabs.net>
Date: Wed, 28 Nov 2018 13:32:08 +0700
Subject: [PATCH] [+] na - no allocation enc/dec functions [*] Returned back
 old enc/dec signatures

---
 crypto/dap_enc.c        | 117 ++++++++++++++++++++++++++--------------
 crypto/dap_enc.h        |  12 +++--
 crypto/dap_enc_base64.c |   2 +-
 crypto/dap_enc_key.c    |   2 +
 crypto/dap_enc_key.h    |   4 +-
 test/libdap-test        |   2 +-
 6 files changed, 90 insertions(+), 49 deletions(-)

diff --git a/crypto/dap_enc.c b/crypto/dap_enc.c
index 1639cd4..5845267 100644
--- a/crypto/dap_enc.c
+++ b/crypto/dap_enc.c
@@ -25,6 +25,7 @@
 #include <stdint.h>
 #include <string.h>
 #include <time.h>
+#include <assert.h>
 #include "dap_enc.h"
 #include "dap_enc_base64.h"
 #include "dap_enc_key.h"
@@ -50,36 +51,70 @@ void dap_enc_deinit()
 
 }
 
+/**
+ * @brief dap_enc_code_out_size
+ * @param a_key
+ * @param a_buf_in_size
+ * @return
+ */
+size_t dap_enc_code_out_size(dap_enc_key_t a_key, const size_t a_buf_in_size)
+{
+    (void) a_key;
+    (void) a_buf_in_size;
+    assert(0); // TODO make out buf size calculations
+    return 0;
+}
+
+/**
+ * @brief dap_enc_decode_out_size
+ * @param a_key
+ * @param a_buf_in_size
+ * @return
+ */
+size_t dap_enc_decode_out_size(dap_enc_key_t a_key, const size_t a_buf_in_size)
+{
+    (void) a_key;
+    (void) a_buf_in_size;
+    assert(0); // TODO make out buf size calculations
+    return 0;
+}
+
+
 
 /**
  * @brief dap_enc_code Encode data with key
- * @param key_private Private key
- * @param buf  Input buffer
- * @param buf_size Input buffer size
- * @param buf_out Output buffer
+ * @param a_key Private key
+ * @param a_buf  Input buffer
+ * @param a_buf_size Input buffer size
+ * @param a_buf_out Output buffer
+ * @param a_buf_out_size_max
  * @return bytes actualy written in the output buffer
  */
-size_t dap_enc_code(struct dap_enc_key * key,const void * buf,const size_t buf_size,
-                    void ** buf_out, dap_enc_data_type_t data_type_out)
+size_t dap_enc_code(struct dap_enc_key * a_key,const void * a_buf_in,const size_t a_buf_size,
+                    void * a_buf_out, const size_t a_buf_out_size_max, dap_enc_data_type_t a_data_type_out)
 {
-    if(key->enc) {
-        if(data_type_out == DAP_ENC_DATA_TYPE_RAW) {
-            return key->enc(key, buf, buf_size, buf_out);
+    if(a_key->enc_na) {
+        if(a_data_type_out == DAP_ENC_DATA_TYPE_RAW) {
+            return a_key->enc_na(a_key, a_buf_in, a_buf_size, a_buf_out, a_buf_out_size_max);
+        }else{
+            void *l_proc_buf;
+            l_proc_buf  = DAP_NEW_SIZE (void, a_buf_out_size_max );
+            size_t l_proc_buf_size = a_key->enc_na(a_key, a_buf_in, a_buf_size, l_proc_buf,a_buf_out_size_max);
+            if(a_data_type_out == DAP_ENC_DATA_TYPE_B64 || a_data_type_out == DAP_ENC_DATA_TYPE_B64_URLSAFE) {
+                if( DAP_ENC_BASE64_ENCODE_SIZE ( l_proc_buf_size) < a_buf_out_size_max  ){
+                    size_t l_buf_out_size=dap_enc_base64_encode(l_proc_buf, l_proc_buf_size, a_buf_out, a_data_type_out);
+                    DAP_DELETE(l_proc_buf);
+                    return l_buf_out_size;
+                }
+            } else {
+                log_it(L_ERROR, "Unknown dap_enc_data_type");
+                DAP_DELETE(l_proc_buf);
+                return 0;
+            }
+            return l_proc_buf_size;
         }
-
-        void *proc_buf;
-        size_t ret = key->enc(key, buf, buf_size, &proc_buf);
-        if(data_type_out == DAP_ENC_DATA_TYPE_B64 || data_type_out == DAP_ENC_DATA_TYPE_B64_URLSAFE) {
-            *buf_out = malloc(DAP_ENC_BASE64_ENCODE_SIZE(ret));
-            ret=dap_enc_base64_encode(proc_buf, ret, *buf_out, data_type_out);
-            free(proc_buf);
-        } else {
-            log_it(L_ERROR, "Unknown dap_enc_data_type");
-            return 0;
-        }
-        return ret;
     } else {
-        log_it(L_ERROR, "key->enc is NULL");
+        log_it(L_ERROR, "key->enc_na is NULL");
         return 0;
     }
 }
@@ -93,39 +128,39 @@ size_t dap_enc_code(struct dap_enc_key * key,const void * buf,const size_t buf_s
  * @param buf_out_max Maximum size of output buffer
  * @return bytes actualy written in the output buffer
  */
-size_t dap_enc_decode(struct dap_enc_key * key,const void * buf, const size_t buf_size,
-                      void ** buf_out, dap_enc_data_type_t data_type_in)
+size_t dap_enc_decode(struct dap_enc_key * a_key,const void * a_buf_in, const size_t a_buf_in_size,
+                      void * a_buf_out, const size_t a_buf_out_size_max, dap_enc_data_type_t a_data_type_in)
 {
-    void *proc_buf = NULL;
-    const void *proc_buf_const = NULL;
-    size_t proc_buf_size = 0;
-    switch(data_type_in){
+    void *l_proc_buf = NULL;
+    const void *l_proc_buf_const = NULL;
+    size_t l_proc_buf_size = 0;
+    switch(a_data_type_in){
         case DAP_ENC_DATA_TYPE_B64:
         case DAP_ENC_DATA_TYPE_B64_URLSAFE:
-            proc_buf=calloc(1,DAP_ENC_BASE64_ENCODE_SIZE(buf_size));
-            proc_buf_size= dap_enc_base64_decode((const char*) buf,buf_size,proc_buf,data_type_in);
-            proc_buf_const=proc_buf;
+            l_proc_buf=DAP_NEW_SIZE(void,DAP_ENC_BASE64_ENCODE_SIZE(a_buf_in_size));
+            l_proc_buf_size= dap_enc_base64_decode((const char*) a_buf_in,a_buf_in_size,l_proc_buf,a_data_type_in);
+            l_proc_buf_const=l_proc_buf;
         break;
         case DAP_ENC_DATA_TYPE_RAW:{
-            proc_buf_const=buf;
-            proc_buf_size=buf_size;
+            l_proc_buf_const=a_buf_in;
+            l_proc_buf_size=a_buf_in_size;
         }break;
     }
 
-    if(key->dec) {
-        if(proc_buf_size == 0) {
+    if(a_key->dec_na) {
+        if(l_proc_buf_size == 0) {
             log_it(L_ERROR, "Buf is null. dap_enc_base64_decode is failed");
             return 0;
         }
-        size_t ret = key->dec(key,proc_buf_const,proc_buf_size, buf_out);
+        size_t ret = a_key->dec_na(a_key,l_proc_buf_const,l_proc_buf_size, a_buf_out,a_buf_out_size_max);
 
-        if(data_type_in==DAP_ENC_DATA_TYPE_B64 || data_type_in == DAP_ENC_DATA_TYPE_B64_URLSAFE)
-            free(proc_buf);
+        if(a_data_type_in==DAP_ENC_DATA_TYPE_B64 || a_data_type_in == DAP_ENC_DATA_TYPE_B64_URLSAFE)
+            free(l_proc_buf);
         return ret;
     } else {
-        log_it(L_WARNING, "key->dec is NULL");
-        if(proc_buf_size)
-            free(proc_buf);
+        log_it(L_WARNING, "key->dec_na is NULL");
+        if(l_proc_buf_size)
+            free(l_proc_buf);
         return 0;
     }
 }
diff --git a/crypto/dap_enc.h b/crypto/dap_enc.h
index 6449fac..0bdcaf6 100644
--- a/crypto/dap_enc.h
+++ b/crypto/dap_enc.h
@@ -32,10 +32,14 @@ extern "C" {
 int dap_enc_init(void);
 void dap_enc_deinit(void);
 
-size_t dap_enc_code(struct dap_enc_key * key, const void * buf, const size_t buf_size, void ** buf_out,
-                    dap_enc_data_type_t data_type_out);
-size_t dap_enc_decode(struct dap_enc_key * key, const void * buf, const size_t buf_size, void ** buf_out,
-                     dap_enc_data_type_t data_type_in);
+size_t dap_enc_code_out_size(dap_enc_key_t a_key, const size_t a_buf_in_size);
+size_t dap_enc_decode_out_size(dap_enc_key_t a_key, const size_t a_buf_in_size);
+
+size_t dap_enc_code(struct dap_enc_key * a_key, const void * a_buf_in, const size_t a_buf_in_size, void * a_buf_out, const size_t a_buf_out_size_max,
+                    dap_enc_data_type_t a_data_type_out);
+
+size_t dap_enc_decode(struct dap_enc_key * a_key, const void * a_buf_in, const size_t a_buf_in_size, void * a_buf_out, const size_t a_buf_out_size_max,
+                     dap_enc_data_type_t a_data_type_in);
 
 #ifdef __cplusplus
 }
diff --git a/crypto/dap_enc_base64.c b/crypto/dap_enc_base64.c
index f9c878d..c09048c 100755
--- a/crypto/dap_enc_base64.c
+++ b/crypto/dap_enc_base64.c
@@ -266,7 +266,7 @@ size_t dap_enc_base64_encode(const void * a_in, size_t a_in_size, char * a_out,
             a_out[size++] = '=';
         }
     }
-
+    a_out[size+1]='0';
     return size;
 }
 
diff --git a/crypto/dap_enc_key.c b/crypto/dap_enc_key.c
index b4abaaa..78d512c 100644
--- a/crypto/dap_enc_key.c
+++ b/crypto/dap_enc_key.c
@@ -54,7 +54,9 @@ struct dap_enc_key_callbacks{
     [DAP_ENC_KEY_TYPE_IAES]={
         .name = "IAES",
         .enc = dap_enc_iaes256_cbc_encrypt,
+        .enc_na = dap_enc_iaes256_cbc_encrypt_fast ,
         .dec = dap_enc_iaes256_cbc_decrypt,
+        .dec_na = dap_enc_iaes256_cbc_decrypt_fast ,
         .new_callback = dap_enc_aes_key_new,
         .delete_callback = dap_enc_aes_key_delete,
         .new_generate_callback = dap_enc_aes_key_generate,
diff --git a/crypto/dap_enc_key.h b/crypto/dap_enc_key.h
index a24d5a3..97680cb 100644
--- a/crypto/dap_enc_key.h
+++ b/crypto/dap_enc_key.h
@@ -166,8 +166,8 @@ typedef struct dap_enc_key {
     dap_enc_key_type_t type;
     dap_enc_callback_dataop_t enc;
     dap_enc_callback_dataop_t dec;
-    dap_enc_callback_dataop_t enc_na;
-    dap_enc_callback_dataop_t dec_na;
+    dap_enc_callback_dataop_na_t enc_na;
+    dap_enc_callback_dataop_na_t dec_na;
 
     dap_enc_gen_alice_shared_key gen_alice_shared_key;
     dap_enc_gen_bob_shared_key gen_bob_shared_key;
diff --git a/test/libdap-test b/test/libdap-test
index 982e8d9..269a48d 160000
--- a/test/libdap-test
+++ b/test/libdap-test
@@ -1 +1 @@
-Subproject commit 982e8d9e7726918ce622b42b5335f324e738c6dc
+Subproject commit 269a48d990a010dc778067fa88a7c2b793eec647
-- 
GitLab