diff --git a/include/dap_enc_bliss.h b/include/dap_enc_bliss.h index 8b602bc66e387ba8cc9661ed1edab7c6a452be56..8e716d90b8231a990ddacb673885cf3b1222395c 100755 --- a/include/dap_enc_bliss.h +++ b/include/dap_enc_bliss.h @@ -28,10 +28,10 @@ int dap_enc_sig_bliss_key_pub_output(struct dap_enc_key *l_key, void * l_output) uint8_t* dap_enc_sig_bliss_write_signature(bliss_signature_t* a_sign, size_t *a_sign_out); -bliss_signature_t* dap_enc_sig_bliss_read_signature(uint8_t *a_buf, size_t a_buflen); +bliss_signature_t* dap_enc_sig_bliss_read_signature(const byte_t *a_buf, size_t a_buflen); uint8_t* dap_enc_sig_bliss_write_private_key(const bliss_private_key_t* a_private_key, size_t *a_buflen_out); uint8_t* dap_enc_sig_bliss_write_public_key(const bliss_public_key_t* a_public_key, size_t *a_buflen_out); -bliss_private_key_t* dap_enc_sig_bliss_read_private_key(uint8_t *a_buf, size_t a_buflen); +bliss_private_key_t* dap_enc_sig_bliss_read_private_key(const uint8_t *a_buf, size_t a_buflen); bliss_public_key_t* dap_enc_sig_bliss_read_public_key(const uint8_t *a_buf, size_t a_buflen); diff --git a/include/dap_enc_key.h b/include/dap_enc_key.h index 064896d3cfadd7109fa932de816eb596686b19dd..6699bb5f890c99a9feae86c07e83341f8155dcf7 100755 --- a/include/dap_enc_key.h +++ b/include/dap_enc_key.h @@ -29,7 +29,7 @@ #include <stddef.h> #include <time.h> #include <stdint.h> - +#include "dap_common.h" typedef enum dap_enc_data_type{DAP_ENC_DATA_TYPE_RAW, @@ -229,8 +229,8 @@ uint8_t* dap_enc_key_serealize_sign(dap_enc_key_type_t a_key_type, uint8_t *a_si uint8_t* dap_enc_key_deserealize_sign(dap_enc_key_type_t a_key_type, uint8_t *a_sign, size_t *a_sign_len); uint8_t* dap_enc_key_serealize_priv_key(dap_enc_key_t *a_key, size_t *a_buflen_out); uint8_t* dap_enc_key_serealize_pub_key(dap_enc_key_t *a_key, size_t *a_buflen_out); -int dap_enc_key_deserealize_priv_key(dap_enc_key_t *a_key, uint8_t *a_buf, size_t a_buflen); -int dap_enc_key_deserealize_pub_key(dap_enc_key_t *a_key, const uint8_t *a_buf, size_t a_buflen); +int dap_enc_key_deserealize_priv_key(dap_enc_key_t *a_key, const byte_t *a_buf, size_t a_buflen); +int dap_enc_key_deserealize_pub_key(dap_enc_key_t *a_key, const byte_t *a_buf, size_t a_buflen); dap_enc_key_serealize_t* dap_enc_key_serealize(dap_enc_key_t * key); dap_enc_key_t* dap_enc_key_deserealize(const void *buf, size_t buf_size); diff --git a/src/dap_enc.c b/src/dap_enc.c index 8ad781c0b7904a93a9a88e79186d0d01e0e47699..28f0f753f051cea01042033d824497201265f22a 100755 --- a/src/dap_enc.c +++ b/src/dap_enc.c @@ -1,185 +1,185 @@ -/* - Copyright (c) 2017-2018 (c) Project "DeM Labs Inc" https://github.com/demlabsinc - All rights reserved. - - This file is part of DAP (Deus Applications Prototypes) the open source project - - DAP (Deus Applicaions Prototypes) is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - DAP is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public License - along with any DAP based project. If not, see <http://www.gnu.org/licenses/>. -*/ - -#ifndef _WIN32 -#include <arpa/inet.h> -#endif -#include <time.h> -#include <stdlib.h> -#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" -#include "dap_common.h" - -#define LOG_TAG "dap_enc" - -/** - * @brief enc_init - * @return - */ -int dap_enc_init() -{ - srand(time(NULL)); - return 0; -} - -/** - * @brief dap_enc_deinit - */ -void dap_enc_deinit() -{ - -} - -/** - * @brief dap_enc_code_out_size - * @param a_key - * @param a_buf_in_size - * @return min buffer size for input in encode function - */ -size_t dap_enc_code_out_size(dap_enc_key_t* a_key, const size_t a_buf_in_size, dap_enc_data_type_t type) -{ - size_t raw_encode_data_size = dap_enc_key_get_enc_size(a_key, a_buf_in_size); - if(raw_encode_data_size == 0) { - log_it(L_ERROR, "dap_enc_key_get_enc_size return 0"); - return 0; - } - if(type != DAP_ENC_DATA_TYPE_RAW) { - return (size_t)DAP_ENC_BASE64_ENCODE_SIZE(raw_encode_data_size); - } - return raw_encode_data_size; -} - -/** - * @brief dap_enc_decode_out_size - * @param a_key - * @param a_buf_in_size - * @return min buffer size for input in decode function - */ -size_t dap_enc_decode_out_size(dap_enc_key_t* a_key, const size_t a_buf_in_size, dap_enc_data_type_t type) -{ - size_t raw_decode_data_size = dap_enc_key_get_dec_size(a_key, a_buf_in_size); - if(raw_decode_data_size == 0) { - log_it(L_ERROR, "dap_enc_key_get_enc_size return 0"); - return 0; - } - - if(type != DAP_ENC_DATA_TYPE_RAW) { - return (size_t)DAP_ENC_BASE64_ENCODE_SIZE(raw_decode_data_size); - } - return raw_decode_data_size; -} - - - -/** - * @brief dap_enc_code Encode data with key - * @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 * 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(!a_key) { - log_it(L_ERROR, "key in dap_enc_code() is NULL"); - return 0; - } - 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 { - DAP_DELETE(l_proc_buf); - log_it(L_ERROR, "a_buf_out_size_max less than result size"); - return 0; - } - } else { - log_it(L_ERROR, "Unknown dap_enc_data_type"); - DAP_DELETE(l_proc_buf); - return 0; - } - } - } else { - log_it(L_ERROR, "key->enc_na is NULL"); - return 0; - } -} - -/** - * @brief dap_enc_decode Decode data with key - * @param key_public Public key - * @param buf Input buffer - * @param buf_size Input buffer size - * @param buf_out Output buffer - * @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 * 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 *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: - 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:{ - l_proc_buf_const=a_buf_in; - l_proc_buf_size=a_buf_in_size; - }break; - } - - 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 = a_key->dec_na(a_key,l_proc_buf_const,l_proc_buf_size, a_buf_out,a_buf_out_size_max); - - 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_na is NULL"); - if(l_proc_buf_size) - free(l_proc_buf); - return 0; - } -} +/* + Copyright (c) 2017-2018 (c) Project "DeM Labs Inc" https://github.com/demlabsinc + All rights reserved. + + This file is part of DAP (Deus Applications Prototypes) the open source project + + DAP (Deus Applicaions Prototypes) is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + DAP is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with any DAP based project. If not, see <http://www.gnu.org/licenses/>. +*/ + +#ifndef _WIN32 +#include <arpa/inet.h> +#endif +#include <time.h> +#include <stdlib.h> +#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" +#include "dap_common.h" + +#define LOG_TAG "dap_enc" + +/** + * @brief enc_init + * @return + */ +int dap_enc_init() +{ + srand(time(NULL)); + return 0; +} + +/** + * @brief dap_enc_deinit + */ +void dap_enc_deinit() +{ + +} + +/** + * @brief dap_enc_code_out_size + * @param a_key + * @param a_buf_in_size + * @return min buffer size for input in encode function + */ +size_t dap_enc_code_out_size(dap_enc_key_t* a_key, const size_t a_buf_in_size, dap_enc_data_type_t type) +{ + size_t raw_encode_data_size = dap_enc_key_get_enc_size(a_key, a_buf_in_size); + if(raw_encode_data_size == 0) { + log_it(L_ERROR, "dap_enc_key_get_enc_size return 0"); + return 0; + } + if(type != DAP_ENC_DATA_TYPE_RAW) { + return (size_t)DAP_ENC_BASE64_ENCODE_SIZE(raw_encode_data_size); + } + return raw_encode_data_size; +} + +/** + * @brief dap_enc_decode_out_size + * @param a_key + * @param a_buf_in_size + * @return min buffer size for input in decode function + */ +size_t dap_enc_decode_out_size(dap_enc_key_t* a_key, const size_t a_buf_in_size, dap_enc_data_type_t type) +{ + size_t raw_decode_data_size = dap_enc_key_get_dec_size(a_key, a_buf_in_size); + if(raw_decode_data_size == 0) { + log_it(L_ERROR, "dap_enc_key_get_enc_size return 0"); + return 0; + } + + if(type != DAP_ENC_DATA_TYPE_RAW) { + return (size_t)DAP_ENC_BASE64_ENCODE_SIZE(raw_decode_data_size); + } + return raw_decode_data_size; +} + + + +/** + * @brief dap_enc_code Encode data with key + * @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 * 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(!a_key) { + log_it(L_ERROR, "key in dap_enc_code() is NULL"); + return 0; + } + 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 { + DAP_DELETE(l_proc_buf); + log_it(L_ERROR, "a_buf_out_size_max less than result size"); + return 0; + } + } else { + log_it(L_ERROR, "Unknown dap_enc_data_type"); + DAP_DELETE(l_proc_buf); + return 0; + } + } + } else { + log_it(L_ERROR, "key->enc_na is NULL"); + return 0; + } +} + +/** + * @brief dap_enc_decode Decode data with key + * @param key_public Public key + * @param buf Input buffer + * @param buf_size Input buffer size + * @param buf_out Output buffer + * @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 * 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 *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: + 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:{ + l_proc_buf_const=a_buf_in; + l_proc_buf_size=a_buf_in_size; + }break; + } + + 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 = a_key->dec_na(a_key,l_proc_buf_const,l_proc_buf_size, a_buf_out,a_buf_out_size_max); + + 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_na is NULL"); + if(l_proc_buf_size) + free(l_proc_buf); + return 0; + } +} diff --git a/src/dap_enc_base58.c b/src/dap_enc_base58.c index eba1c466bdbbed7d24aa1bb804cb7e01e0e21448..8549457a15969a8f279de36c18475ef5fd31efb1 100755 --- a/src/dap_enc_base58.c +++ b/src/dap_enc_base58.c @@ -1,197 +1,197 @@ -/* - * Authors: - * Dmitriy A. Gearasimov <kahovski@gmail.com> - * DeM Labs Inc. https://demlabs.net - * Kelvin Blockchain community https://github.com/kelvinblockchain - * Copyright (c) 2017-2019 - * All rights reserved. - - This file is part of DAP (Deus Applications Prototypes) the open source project - - DAP (Deus Applicaions Prototypes) is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - DAP is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with any DAP based project. If not, see <http://www.gnu.org/licenses/>. -*/ - -#include <stdbool.h> -#include <stddef.h> -#include <stdint.h> -#include <string.h> -#include "dap_common.h" -#include "dap_enc_base58.h" - -#define LOG_TAG "dap_enc_base58" - -const char c_b58digits_ordered[] = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; -const int8_t c_b58digits_map[] = { - -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, - -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, - -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, - -1, 0, 1, 2, 3, 4, 5, 6, 7, 8,-1,-1,-1,-1,-1,-1, - -1, 9,10,11,12,13,14,15,16,-1,17,18,19,20,21,-1, - 22,23,24,25,26,27,28,29,30,31,32,-1,-1,-1,-1,-1, - -1,33,34,35,36,37,38,39,40,41,42,43,-1,44,45,46, - 47,48,49,50,51,52,53,54,55,56,57,-1,-1,-1,-1,-1, -}; - -/** - * @brief dap_enc_base58_decode - * @param a_in - * @param a_out - * @return - */ -size_t dap_enc_base58_decode(const char * a_in, void * a_out) -{ - size_t l_out_size_max = DAP_ENC_BASE58_DECODE_SIZE(strlen(a_in )); - size_t l_out_size = l_out_size_max; - - const unsigned char *l_in_u8 = (const unsigned char*)a_in; - size_t l_outi_size = (l_out_size_max + 3) / 4; - - uint32_t l_outi[l_outi_size]; - memset(l_outi, 0, l_outi_size*sizeof(uint32_t)); - uint64_t t; - uint32_t c; - size_t i, j; - uint8_t bytesleft = l_out_size_max % 4; - uint32_t zeromask = bytesleft ? (0xffffffff << (bytesleft * 8)) : 0; - unsigned zerocount = 0; - size_t l_in_len = strlen(a_in); - - - // Leading zeros, just count - for (i = 0; i < l_in_len && l_in_u8[i] == '1'; ++i) - ++zerocount; - - for ( ; i < l_in_len; ++i) - { - if (l_in_u8[i] & 0x80) - // High-bit set on invalid digit - return 0; - if (c_b58digits_map[l_in_u8[i]] == -1) - // Invalid base58 digit - return 0; - c = (unsigned)c_b58digits_map[l_in_u8[i]]; - for (j = l_outi_size; j--; ) - { - t = ((uint64_t)l_outi[j]) * 58 + c; - c = (t & 0x3f00000000) >> 32; - l_outi[j] = t & 0xffffffff; - } - if (c) - // Output number too big (carry to the next int32) - return 0; - if (l_outi[0] & zeromask) - // Output number too big (last int32 filled too far) - return 0; - } - - unsigned char l_out_u80[l_out_size_max]; - unsigned char *l_out_u8 = l_out_u80; - j = 0; - switch (bytesleft) { - case 3: - *(l_out_u8++) = (l_outi[0] & 0xff0000) >> 16; - //-fallthrough - case 2: - *(l_out_u8++) = (l_outi[0] & 0xff00) >> 8; - //-fallthrough - case 1: - *(l_out_u8++) = (l_outi[0] & 0xff); - ++j; - //-fallthrough - default: - break; - } - - for (; j < l_outi_size; ++j) - { - *(l_out_u8++) = (l_outi[j] >> 0x18) & 0xff; - *(l_out_u8++) = (l_outi[j] >> 0x10) & 0xff; - *(l_out_u8++) = (l_outi[j] >> 8) & 0xff; - *(l_out_u8++) = (l_outi[j] >> 0) & 0xff; - } - - // Count canonical base58 byte count - l_out_u8 = l_out_u80; - for (i = 0; i < l_out_size_max; ++i) - { - if (l_out_u8[i]) { - if (zerocount > i) { - /* result too large */ - return 0; - } - break; - } - --l_out_size; - } - - - unsigned char *l_out = a_out; - memset(l_out, 0, zerocount); - // shift result to beginning of the string - for (j = 0; j < l_out_size; j++){ - l_out[j+zerocount] = l_out_u8[j+i]; - } - l_out[j+zerocount] = 0; - l_out_size += zerocount; - - return l_out_size; -} - - - -//bool b58enc(char *a_out, size_t *l_out_size, const void *a_in, size_t a_in_size) -size_t dap_enc_base58_encode(const void * a_in, size_t a_in_size, char * a_out) -{ - const uint8_t *l_in_u8 = a_in; - int carry; - ssize_t i, j, high, zcount = 0; - size_t size; - size_t l_out_size = DAP_ENC_BASE58_ENCODE_SIZE (a_in_size); - while (zcount < (ssize_t)a_in_size && !l_in_u8[zcount]) - ++zcount; - - size = (a_in_size - zcount) * 138 / 100 + 1; - uint8_t buf[size]; - memset(buf, 0, size); - - for (i = zcount, high = size - 1; i < (ssize_t)a_in_size; ++i, high = j) - { - for (carry = l_in_u8[i], j = size - 1; (j > high) || carry; --j) - { - carry += 256 * buf[j]; - buf[j] = carry % 58; - carry /= 58; - } - } - - for (j = 0; j < (ssize_t)size && !buf[j]; ++j); - - if (l_out_size <= ( zcount + size - j) ){ - l_out_size = ( zcount + size - j + 1 ); - return l_out_size; - } - - if (zcount) - memset(a_out, '1', zcount); - for (i = zcount; j < (ssize_t)size; ++i, ++j) - a_out[i] = c_b58digits_ordered[buf[j]]; - a_out[i] = '\0'; - l_out_size = i; - - return l_out_size; -} - - - - +/* + * Authors: + * Dmitriy A. Gearasimov <kahovski@gmail.com> + * DeM Labs Inc. https://demlabs.net + * Kelvin Blockchain community https://github.com/kelvinblockchain + * Copyright (c) 2017-2019 + * All rights reserved. + + This file is part of DAP (Deus Applications Prototypes) the open source project + + DAP (Deus Applicaions Prototypes) is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + DAP is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with any DAP based project. If not, see <http://www.gnu.org/licenses/>. +*/ + +#include <stdbool.h> +#include <stddef.h> +#include <stdint.h> +#include <string.h> +#include "dap_common.h" +#include "dap_enc_base58.h" + +#define LOG_TAG "dap_enc_base58" + +const char c_b58digits_ordered[] = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; +const int8_t c_b58digits_map[] = { + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, + -1, 0, 1, 2, 3, 4, 5, 6, 7, 8,-1,-1,-1,-1,-1,-1, + -1, 9,10,11,12,13,14,15,16,-1,17,18,19,20,21,-1, + 22,23,24,25,26,27,28,29,30,31,32,-1,-1,-1,-1,-1, + -1,33,34,35,36,37,38,39,40,41,42,43,-1,44,45,46, + 47,48,49,50,51,52,53,54,55,56,57,-1,-1,-1,-1,-1, +}; + +/** + * @brief dap_enc_base58_decode + * @param a_in + * @param a_out + * @return + */ +size_t dap_enc_base58_decode(const char * a_in, void * a_out) +{ + size_t l_out_size_max = DAP_ENC_BASE58_DECODE_SIZE(strlen(a_in )); + size_t l_out_size = l_out_size_max; + + const unsigned char *l_in_u8 = (const unsigned char*)a_in; + size_t l_outi_size = (l_out_size_max + 3) / 4; + + uint32_t l_outi[l_outi_size]; + memset(l_outi, 0, l_outi_size*sizeof(uint32_t)); + uint64_t t; + uint32_t c; + size_t i, j; + uint8_t bytesleft = l_out_size_max % 4; + uint32_t zeromask = bytesleft ? (0xffffffff << (bytesleft * 8)) : 0; + unsigned zerocount = 0; + size_t l_in_len = strlen(a_in); + + + // Leading zeros, just count + for (i = 0; i < l_in_len && l_in_u8[i] == '1'; ++i) + ++zerocount; + + for ( ; i < l_in_len; ++i) + { + if (l_in_u8[i] & 0x80) + // High-bit set on invalid digit + return 0; + if (c_b58digits_map[l_in_u8[i]] == -1) + // Invalid base58 digit + return 0; + c = (unsigned)c_b58digits_map[l_in_u8[i]]; + for (j = l_outi_size; j--; ) + { + t = ((uint64_t)l_outi[j]) * 58 + c; + c = (t & 0x3f00000000) >> 32; + l_outi[j] = t & 0xffffffff; + } + if (c) + // Output number too big (carry to the next int32) + return 0; + if (l_outi[0] & zeromask) + // Output number too big (last int32 filled too far) + return 0; + } + + unsigned char l_out_u80[l_out_size_max]; + unsigned char *l_out_u8 = l_out_u80; + j = 0; + switch (bytesleft) { + case 3: + *(l_out_u8++) = (l_outi[0] & 0xff0000) >> 16; + //-fallthrough + case 2: + *(l_out_u8++) = (l_outi[0] & 0xff00) >> 8; + //-fallthrough + case 1: + *(l_out_u8++) = (l_outi[0] & 0xff); + ++j; + //-fallthrough + default: + break; + } + + for (; j < l_outi_size; ++j) + { + *(l_out_u8++) = (l_outi[j] >> 0x18) & 0xff; + *(l_out_u8++) = (l_outi[j] >> 0x10) & 0xff; + *(l_out_u8++) = (l_outi[j] >> 8) & 0xff; + *(l_out_u8++) = (l_outi[j] >> 0) & 0xff; + } + + // Count canonical base58 byte count + l_out_u8 = l_out_u80; + for (i = 0; i < l_out_size_max; ++i) + { + if (l_out_u8[i]) { + if (zerocount > i) { + /* result too large */ + return 0; + } + break; + } + --l_out_size; + } + + + unsigned char *l_out = a_out; + memset(l_out, 0, zerocount); + // shift result to beginning of the string + for (j = 0; j < l_out_size; j++){ + l_out[j+zerocount] = l_out_u8[j+i]; + } + l_out[j+zerocount] = 0; + l_out_size += zerocount; + + return l_out_size; +} + + + +//bool b58enc(char *a_out, size_t *l_out_size, const void *a_in, size_t a_in_size) +size_t dap_enc_base58_encode(const void * a_in, size_t a_in_size, char * a_out) +{ + const uint8_t *l_in_u8 = a_in; + int carry; + ssize_t i, j, high, zcount = 0; + size_t size; + size_t l_out_size = DAP_ENC_BASE58_ENCODE_SIZE (a_in_size); + while (zcount < (ssize_t)a_in_size && !l_in_u8[zcount]) + ++zcount; + + size = (a_in_size - zcount) * 138 / 100 + 1; + uint8_t buf[size]; + memset(buf, 0, size); + + for (i = zcount, high = size - 1; i < (ssize_t)a_in_size; ++i, high = j) + { + for (carry = l_in_u8[i], j = size - 1; (j > high) || carry; --j) + { + carry += 256 * buf[j]; + buf[j] = carry % 58; + carry /= 58; + } + } + + for (j = 0; j < (ssize_t)size && !buf[j]; ++j); + + if (l_out_size <= ( zcount + size - j) ){ + l_out_size = ( zcount + size - j + 1 ); + return l_out_size; + } + + if (zcount) + memset(a_out, '1', zcount); + for (i = zcount; j < (ssize_t)size; ++i, ++j) + a_out[i] = c_b58digits_ordered[buf[j]]; + a_out[i] = '\0'; + l_out_size = i; + + return l_out_size; +} + + + + diff --git a/src/dap_enc_base64.c b/src/dap_enc_base64.c index a8e3cebe7c264fe568e852f3399a1a37381a3319..4501e7d3ab6705efae9c6332a03ae38d533459f9 100755 --- a/src/dap_enc_base64.c +++ b/src/dap_enc_base64.c @@ -1,412 +1,412 @@ -#include <math.h> -#include <stdio.h> -#include <stdint.h> -#include <ctype.h> -#include <stdlib.h> -#include <string.h> - -#include "dap_enc_base64.h" - -typedef unsigned char byte; - -// get the size of the result buffer required for Base-64 -// encoding/decoding. -// sz - size of original buffer to be encoded/decoded -// isEncoded - true (1) when encoding the original buffer; -// false (0) when decoding the original buffer. -int B64_GetSize( int sz, int isEncode ); - -// Base-64 encode the given byte array -// outChars - buffer of length returned by GetSize(), filled upon return -void B64_Encode( const byte* srcBytes, int srcLen, char* outChars ); - -// Base-64 decode the given string -// srcChars - characters to be decoded -// outBytes - buffer of length returned by GetSize(), filled upon return -void B64_Decode( const char* srcChars, int srcLen, byte* outBytes ); - -// return the Base-64 encoded char for the given source byte -char B64_EncodeByte( byte b ); - -// return the Base-64 decoded byte for the given source char -// <returns></returns> -byte B64_DecodeByte( byte b ); - -#ifndef b64_malloc -# define b64_malloc(ptr) malloc(ptr) -#endif -#ifndef b64_realloc -# define b64_realloc(ptr, size) realloc(ptr, size) -#endif - -/** - * @breif Base64 index table. - */ - -static const char b64_standart_table[] = { - 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', - 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', - 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', - 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', - 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', - 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', - 'w', 'x', 'y', 'z', '0', '1', '2', '3', - '4', '5', '6', '7', '8', '9', '+', '/' -}; - -/** - * @breif Base64 url safe index table. - */ -static const char b64_table_url_safe[] = { - 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', - 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', - 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', - 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', - 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', - 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', - 'w', 'x', 'y', 'z', '0', '1', '2', '3', - '4', '5', '6', '7', '8', '9', '-', '_' -}; - -/** - * Encode `unsigned char *' source with `size_t' size. - * Returns a `char *' base64 encoded string. - */ - -char * -b64_encode (const unsigned char *, size_t); - -/** - * Dencode `char *' source with `size_t' size. - * Returns a `unsigned char *' base64 decoded string. - */ -unsigned char * -b64_decode (const char *, size_t); - -/** - * Dencode `char *' source with `size_t' size. - * Returns a `unsigned char *' base64 decoded string + size of decoded string. - */ -unsigned char * -b64_decode_ex (const char *, size_t, size_t *); - -/** - * @brief b64_table_by_standard The function returns the corresponding table of indices - * @param[in] standard Base64 or Base64 URLSAFE encoding - * @return index table - */ -static const char* b64_table_by_standard(dap_enc_data_type_t standard) -{ - switch (standard) { - case DAP_ENC_DATA_TYPE_B64: - return b64_standart_table; - case DAP_ENC_DATA_TYPE_B64_URLSAFE: - return b64_table_url_safe; - default: - perror("Unknown base64 standard"); - abort(); - } - return NULL; -} - -/** - * @brief dap_enc_base64_decode Function of reverse transformation of base64 algorithm - * @param[in] in Pointer to an array with incoming data - * @param[in] in_size Size of the array with outgoing data - * @param[out] out Pointer to an array with outgoing data - * @return Size of the array with outgoing data - */ -size_t dap_enc_base64_decode(const char * in, size_t in_size,void * out, dap_enc_data_type_t standard) -{ - uint8_t * out_bytes = (uint8_t*) out; - - int j = 0; - int8_t l = 0, i = 0; - size_t l_size = 0; - - unsigned char buf[3] = {0}; - unsigned char tmp[4] = {0}; - - const char* b64_table = b64_table_by_standard(standard); - - if (NULL == out) { return 0; } - - // parse until end of source - while (in_size--) { - // break if char is `=' or not base64 char - if ('=' == in[j]) { break; } - - if (!(isalnum(in[j]) || in[j] == b64_table[62] || in[j] == b64_table[63])) - break; - - // read up to 4 bytes at a time into `tmp' - tmp[i++] = in[j++]; - - // if 4 bytes read then decode into `buf' - if (4 == i) { - // translate values in `tmp' from table - for (i = 0; i < 4; ++i) { - // find translation char in `b64_table' - for (l = 0; l < 64; ++l) { - if (tmp[i] == b64_table[l]) { - tmp[i] = l; - break; - } - } - } - - // decode - buf[0] = (tmp[0] << 2) + ((tmp[1] & 0x30) >> 4); - buf[1] = ((tmp[1] & 0xf) << 4) + ((tmp[2] & 0x3c) >> 2); - buf[2] = ((tmp[2] & 0x3) << 6) + tmp[3]; - - // write decoded buffer to `dec' - for (i = 0; i < 3; ++i) { - out_bytes[l_size++] = buf[i]; - } - - // reset - i = 0; - } - } - - // remainder - if (i > 0) { - // fill `tmp' with `\0' at most 4 times - for (j = i; j < 4; ++j) { - tmp[j] = '\0'; - } - - // translate remainder - for (j = 0; j < 4; ++j) { - // find translation char in `b64_table' - for (l = 0; l < 64; ++l) { - if (tmp[j] == b64_table[l]) { - tmp[j] = l; - break; - } - } - } - - // decode remainder - buf[0] = (tmp[0] << 2) + ((tmp[1] & 0x30) >> 4); - buf[1] = ((tmp[1] & 0xf) << 4) + ((tmp[2] & 0x3c) >> 2); - buf[2] = ((tmp[2] & 0x3) << 6) + tmp[3]; - - // write remainer decoded buffer to `dec' - for (j = 0; (j < i - 1); ++j) { - out_bytes[l_size++] = buf[j]; - } - - } - - return l_size; -} - -/** - * @brief dap_enc_base64_encode The function encodes the array according to the base64 algorithm - * @param[in] a_in Array with incoming data - * @param[in] a_in_size The size of the deviance array in the a_in parameter - * @param[out] a_out A pointer to an array in which the data will be after encoding - * @return Size of the array with outgoing data - */ -size_t dap_enc_base64_encode(const void * a_in, size_t a_in_size, char * a_out, dap_enc_data_type_t standard) -{ - uint8_t i = 0; - int j = 0; - size_t size = 0; - unsigned char buf[4]; - unsigned char tmp[3]; - const unsigned char * l_in_bytes = (const unsigned char*) a_in; - - const char* b64_table = b64_table_by_standard(standard); - - if (NULL == a_out) { return 0; } - - // parse until end of source - while (a_in_size--) { - // read up to 3 bytes at a time into `tmp' - tmp[i++] = *( l_in_bytes++); - - // if 3 bytes read then encode into `buf' - if (3 == i) { - buf[0] = (tmp[0] & 0xfc) >> 2; - buf[1] = ((tmp[0] & 0x03) << 4) + ((tmp[1] & 0xf0) >> 4); - buf[2] = ((tmp[1] & 0x0f) << 2) + ((tmp[2] & 0xc0) >> 6); - buf[3] = tmp[2] & 0x3f; - - for (i = 0; i < 4; ++i) { - a_out[size++] = b64_table[buf[i]]; - } - - // reset index - i = 0; - } - } - - // remainder - if (i > 0) { - // fill `tmp' with `\0' at most 3 times - for (j = i; j < 3; ++j) { - tmp[j] = '\0'; - } - - // perform same codec as above - buf[0] = (tmp[0] & 0xfc) >> 2; - buf[1] = ((tmp[0] & 0x03) << 4) + ((tmp[1] & 0xf0) >> 4); - buf[2] = ((tmp[1] & 0x0f) << 2) + ((tmp[2] & 0xc0) >> 6); - buf[3] = tmp[2] & 0x3f; - - // perform same write to `enc` with new allocation - for (j = 0; (j < i + 1); ++j) { - a_out[size++] = b64_table[buf[j]]; - } - - // while there is still a remainder - // append `=' to `enc' - while ((i++ < 3)) { - a_out[size++] = '='; - } - } - return size; -} - - -// get the size of the result buffer required for Base-64 -// encoding/decoding. -// sz - size of original buffer to be encoded/decoded -// isEncoded - true (1) when encoding the original buffer; -// false (0) when decoding the original buffer. -int B64_GetSize( int sz, int isEncode ) -{ - int n = 0; - - if( isEncode ) { - n = ceil ( ((double) sz) / 3.0 ) * 4.0; - switch( sz % 3 ) { - case 0: break; - case 1: n += 2; break; - case 2: n += 3; break; - } - } - else { - n = ceil ( ((double) sz) / 4.0 ) * 3.0; - switch( sz % 4 ) { - case 0: break; - case 1: break; - case 2: n += 1; break; - case 3: n += 2; break; - } - } - return n; -} - - -// Base-64 encode the given byte array -// outChars - buffer of length returned by GetSize(), filled upon return -void B64_Encode( const byte* srcBytes, int srcLen, char* outChars ) -{ - byte b1, b2, b3; - byte* destBytes = (byte*)outChars; - - // walk through the source, taking 3 bytes at a time - int srcNdx = 0; - int destNdx = 0; - int remaining = srcLen; - for( ; remaining > 2; remaining -= 3 ) { - b1 = srcBytes[ srcNdx++ ]; - b2 = srcBytes[ srcNdx++ ]; - b3 = srcBytes[ srcNdx++ ]; - destBytes[destNdx++] = B64_EncodeByte( (byte)( b1 >> 2 ) ); - destBytes[destNdx++] = B64_EncodeByte( (byte)( ( b1 << 4 ) | ( b2 >> 4 ) ) ); - destBytes[destNdx++] = B64_EncodeByte( (byte)( ( b2 << 2 ) | ( b3 >> 6 ) ) ); - destBytes[destNdx++] = B64_EncodeByte( (byte)b3 ); - } - - // process the remaining bytes - b2 = 0; - if( remaining > 0 ) { - b1 = srcBytes[srcNdx++]; - if( remaining == 2 ) - b2 = srcBytes[srcNdx++]; - - destBytes[destNdx++] = B64_EncodeByte( (byte)( b1 >> 2 ) ); - destBytes[destNdx++] = B64_EncodeByte( (byte)( ( b1 << 4 ) | ( b2 >> 4 ) ) ); - if( remaining == 2 ) - destBytes[destNdx++] = B64_EncodeByte( (byte)( b2 << 2 ) ); - } -} - - -// Base-64 decode the given string -// srcChars - characters to be decoded -// outBytes - buffer of length returned by GetSize(), filled upon return -void B64_Decode( const char* srcChars, int srcLen, byte* outBytes ) -{ - byte b1, b2, b3, b4; - const byte* srcBytes = (byte*)srcChars; - byte* destBytes = outBytes; - - // walk through the source, taking 4 bytes at a time - int srcNdx = 0; - int destNdx = 0; - int remaining = srcLen; - for( ; remaining > 3; remaining -= 4 ) { - b1 = B64_DecodeByte( srcBytes[srcNdx++] ); - b2 = B64_DecodeByte( srcBytes[srcNdx++] ); - b3 = B64_DecodeByte( srcBytes[srcNdx++] ); - b4 = B64_DecodeByte( srcBytes[srcNdx++] ); - - destBytes[destNdx++] = (byte)( ( b1 << 2 ) | ( b2 >> 4 ) ); - destBytes[destNdx++] = (byte)( ( b2 << 4 ) | ( b3 >> 2 ) ); - destBytes[destNdx++] = (byte)( ( b3 << 6 ) | b4 ); - } - - // process the remaining bytes - b2 = b3 = 0; - if( remaining > 0 ) { - b1 = B64_DecodeByte( srcBytes[srcNdx++] ); - if( remaining > 1 ) - b2 = B64_DecodeByte( srcBytes[srcNdx++] ); - if( remaining == 3 ) - b3 = B64_DecodeByte( srcBytes[srcNdx++] ); - - destBytes[destNdx++] = (byte)( ( b1 << 2 ) | ( b2 >> 4 ) ); - if( remaining == 3 ) - destBytes[destNdx++] = (byte)( ( b2 << 4 ) | ( b3 >> 2 ) ); - } -} - - -// return the Base-64 encoded char for the given source byte -char B64_EncodeByte( byte b ) -{ - b &= 0x3f; - if( b <= 25 ) - return (byte)( b +'A' ); - if( b <= 51 ) - return (byte)( b - 26 + 'a' ); - if( b <= 61 ) - return (byte)( b - 52 + '0' ); - if( b == 62 ) - return (byte)'-'; - - return (byte)'_'; -} - - -// return the Base-64 decoded byte for the given source char -// <returns></returns> -byte B64_DecodeByte( byte b ) -{ - if (( b == '+' ) || (b =='-') ) - return 62; - if( (b == '/' ) || (b == '_') ) - return 63; - if( b <= '9' ) - return (byte)( b - '0' + 52 ); - if( b <= 'Z' ) - return (byte)( b - 'A' ); - return (byte)( b - 'a' + 26 ); -} - +#include <math.h> +#include <stdio.h> +#include <stdint.h> +#include <ctype.h> +#include <stdlib.h> +#include <string.h> + +#include "dap_enc_base64.h" + +typedef unsigned char byte; + +// get the size of the result buffer required for Base-64 +// encoding/decoding. +// sz - size of original buffer to be encoded/decoded +// isEncoded - true (1) when encoding the original buffer; +// false (0) when decoding the original buffer. +int B64_GetSize( int sz, int isEncode ); + +// Base-64 encode the given byte array +// outChars - buffer of length returned by GetSize(), filled upon return +void B64_Encode( const byte* srcBytes, int srcLen, char* outChars ); + +// Base-64 decode the given string +// srcChars - characters to be decoded +// outBytes - buffer of length returned by GetSize(), filled upon return +void B64_Decode( const char* srcChars, int srcLen, byte* outBytes ); + +// return the Base-64 encoded char for the given source byte +char B64_EncodeByte( byte b ); + +// return the Base-64 decoded byte for the given source char +// <returns></returns> +byte B64_DecodeByte( byte b ); + +#ifndef b64_malloc +# define b64_malloc(ptr) malloc(ptr) +#endif +#ifndef b64_realloc +# define b64_realloc(ptr, size) realloc(ptr, size) +#endif + +/** + * @breif Base64 index table. + */ + +static const char b64_standart_table[] = { + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', + 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', + 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', + 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', + 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', + 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', + 'w', 'x', 'y', 'z', '0', '1', '2', '3', + '4', '5', '6', '7', '8', '9', '+', '/' +}; + +/** + * @breif Base64 url safe index table. + */ +static const char b64_table_url_safe[] = { + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', + 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', + 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', + 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', + 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', + 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', + 'w', 'x', 'y', 'z', '0', '1', '2', '3', + '4', '5', '6', '7', '8', '9', '-', '_' +}; + +/** + * Encode `unsigned char *' source with `size_t' size. + * Returns a `char *' base64 encoded string. + */ + +char * +b64_encode (const unsigned char *, size_t); + +/** + * Dencode `char *' source with `size_t' size. + * Returns a `unsigned char *' base64 decoded string. + */ +unsigned char * +b64_decode (const char *, size_t); + +/** + * Dencode `char *' source with `size_t' size. + * Returns a `unsigned char *' base64 decoded string + size of decoded string. + */ +unsigned char * +b64_decode_ex (const char *, size_t, size_t *); + +/** + * @brief b64_table_by_standard The function returns the corresponding table of indices + * @param[in] standard Base64 or Base64 URLSAFE encoding + * @return index table + */ +static const char* b64_table_by_standard(dap_enc_data_type_t standard) +{ + switch (standard) { + case DAP_ENC_DATA_TYPE_B64: + return b64_standart_table; + case DAP_ENC_DATA_TYPE_B64_URLSAFE: + return b64_table_url_safe; + default: + perror("Unknown base64 standard"); + abort(); + } + return NULL; +} + +/** + * @brief dap_enc_base64_decode Function of reverse transformation of base64 algorithm + * @param[in] in Pointer to an array with incoming data + * @param[in] in_size Size of the array with outgoing data + * @param[out] out Pointer to an array with outgoing data + * @return Size of the array with outgoing data + */ +size_t dap_enc_base64_decode(const char * in, size_t in_size,void * out, dap_enc_data_type_t standard) +{ + uint8_t * out_bytes = (uint8_t*) out; + + int j = 0; + int8_t l = 0, i = 0; + size_t l_size = 0; + + unsigned char buf[3] = {0}; + unsigned char tmp[4] = {0}; + + const char* b64_table = b64_table_by_standard(standard); + + if (NULL == out) { return 0; } + + // parse until end of source + while (in_size--) { + // break if char is `=' or not base64 char + if ('=' == in[j]) { break; } + + if (!(isalnum(in[j]) || in[j] == b64_table[62] || in[j] == b64_table[63])) + break; + + // read up to 4 bytes at a time into `tmp' + tmp[i++] = in[j++]; + + // if 4 bytes read then decode into `buf' + if (4 == i) { + // translate values in `tmp' from table + for (i = 0; i < 4; ++i) { + // find translation char in `b64_table' + for (l = 0; l < 64; ++l) { + if (tmp[i] == b64_table[l]) { + tmp[i] = l; + break; + } + } + } + + // decode + buf[0] = (tmp[0] << 2) + ((tmp[1] & 0x30) >> 4); + buf[1] = ((tmp[1] & 0xf) << 4) + ((tmp[2] & 0x3c) >> 2); + buf[2] = ((tmp[2] & 0x3) << 6) + tmp[3]; + + // write decoded buffer to `dec' + for (i = 0; i < 3; ++i) { + out_bytes[l_size++] = buf[i]; + } + + // reset + i = 0; + } + } + + // remainder + if (i > 0) { + // fill `tmp' with `\0' at most 4 times + for (j = i; j < 4; ++j) { + tmp[j] = '\0'; + } + + // translate remainder + for (j = 0; j < 4; ++j) { + // find translation char in `b64_table' + for (l = 0; l < 64; ++l) { + if (tmp[j] == b64_table[l]) { + tmp[j] = l; + break; + } + } + } + + // decode remainder + buf[0] = (tmp[0] << 2) + ((tmp[1] & 0x30) >> 4); + buf[1] = ((tmp[1] & 0xf) << 4) + ((tmp[2] & 0x3c) >> 2); + buf[2] = ((tmp[2] & 0x3) << 6) + tmp[3]; + + // write remainer decoded buffer to `dec' + for (j = 0; (j < i - 1); ++j) { + out_bytes[l_size++] = buf[j]; + } + + } + + return l_size; +} + +/** + * @brief dap_enc_base64_encode The function encodes the array according to the base64 algorithm + * @param[in] a_in Array with incoming data + * @param[in] a_in_size The size of the deviance array in the a_in parameter + * @param[out] a_out A pointer to an array in which the data will be after encoding + * @return Size of the array with outgoing data + */ +size_t dap_enc_base64_encode(const void * a_in, size_t a_in_size, char * a_out, dap_enc_data_type_t standard) +{ + uint8_t i = 0; + int j = 0; + size_t size = 0; + unsigned char buf[4]; + unsigned char tmp[3]; + const unsigned char * l_in_bytes = (const unsigned char*) a_in; + + const char* b64_table = b64_table_by_standard(standard); + + if (NULL == a_out) { return 0; } + + // parse until end of source + while (a_in_size--) { + // read up to 3 bytes at a time into `tmp' + tmp[i++] = *( l_in_bytes++); + + // if 3 bytes read then encode into `buf' + if (3 == i) { + buf[0] = (tmp[0] & 0xfc) >> 2; + buf[1] = ((tmp[0] & 0x03) << 4) + ((tmp[1] & 0xf0) >> 4); + buf[2] = ((tmp[1] & 0x0f) << 2) + ((tmp[2] & 0xc0) >> 6); + buf[3] = tmp[2] & 0x3f; + + for (i = 0; i < 4; ++i) { + a_out[size++] = b64_table[buf[i]]; + } + + // reset index + i = 0; + } + } + + // remainder + if (i > 0) { + // fill `tmp' with `\0' at most 3 times + for (j = i; j < 3; ++j) { + tmp[j] = '\0'; + } + + // perform same codec as above + buf[0] = (tmp[0] & 0xfc) >> 2; + buf[1] = ((tmp[0] & 0x03) << 4) + ((tmp[1] & 0xf0) >> 4); + buf[2] = ((tmp[1] & 0x0f) << 2) + ((tmp[2] & 0xc0) >> 6); + buf[3] = tmp[2] & 0x3f; + + // perform same write to `enc` with new allocation + for (j = 0; (j < i + 1); ++j) { + a_out[size++] = b64_table[buf[j]]; + } + + // while there is still a remainder + // append `=' to `enc' + while ((i++ < 3)) { + a_out[size++] = '='; + } + } + return size; +} + + +// get the size of the result buffer required for Base-64 +// encoding/decoding. +// sz - size of original buffer to be encoded/decoded +// isEncoded - true (1) when encoding the original buffer; +// false (0) when decoding the original buffer. +int B64_GetSize( int sz, int isEncode ) +{ + int n = 0; + + if( isEncode ) { + n = ceil ( ((double) sz) / 3.0 ) * 4.0; + switch( sz % 3 ) { + case 0: break; + case 1: n += 2; break; + case 2: n += 3; break; + } + } + else { + n = ceil ( ((double) sz) / 4.0 ) * 3.0; + switch( sz % 4 ) { + case 0: break; + case 1: break; + case 2: n += 1; break; + case 3: n += 2; break; + } + } + return n; +} + + +// Base-64 encode the given byte array +// outChars - buffer of length returned by GetSize(), filled upon return +void B64_Encode( const byte* srcBytes, int srcLen, char* outChars ) +{ + byte b1, b2, b3; + byte* destBytes = (byte*)outChars; + + // walk through the source, taking 3 bytes at a time + int srcNdx = 0; + int destNdx = 0; + int remaining = srcLen; + for( ; remaining > 2; remaining -= 3 ) { + b1 = srcBytes[ srcNdx++ ]; + b2 = srcBytes[ srcNdx++ ]; + b3 = srcBytes[ srcNdx++ ]; + destBytes[destNdx++] = B64_EncodeByte( (byte)( b1 >> 2 ) ); + destBytes[destNdx++] = B64_EncodeByte( (byte)( ( b1 << 4 ) | ( b2 >> 4 ) ) ); + destBytes[destNdx++] = B64_EncodeByte( (byte)( ( b2 << 2 ) | ( b3 >> 6 ) ) ); + destBytes[destNdx++] = B64_EncodeByte( (byte)b3 ); + } + + // process the remaining bytes + b2 = 0; + if( remaining > 0 ) { + b1 = srcBytes[srcNdx++]; + if( remaining == 2 ) + b2 = srcBytes[srcNdx++]; + + destBytes[destNdx++] = B64_EncodeByte( (byte)( b1 >> 2 ) ); + destBytes[destNdx++] = B64_EncodeByte( (byte)( ( b1 << 4 ) | ( b2 >> 4 ) ) ); + if( remaining == 2 ) + destBytes[destNdx++] = B64_EncodeByte( (byte)( b2 << 2 ) ); + } +} + + +// Base-64 decode the given string +// srcChars - characters to be decoded +// outBytes - buffer of length returned by GetSize(), filled upon return +void B64_Decode( const char* srcChars, int srcLen, byte* outBytes ) +{ + byte b1, b2, b3, b4; + const byte* srcBytes = (byte*)srcChars; + byte* destBytes = outBytes; + + // walk through the source, taking 4 bytes at a time + int srcNdx = 0; + int destNdx = 0; + int remaining = srcLen; + for( ; remaining > 3; remaining -= 4 ) { + b1 = B64_DecodeByte( srcBytes[srcNdx++] ); + b2 = B64_DecodeByte( srcBytes[srcNdx++] ); + b3 = B64_DecodeByte( srcBytes[srcNdx++] ); + b4 = B64_DecodeByte( srcBytes[srcNdx++] ); + + destBytes[destNdx++] = (byte)( ( b1 << 2 ) | ( b2 >> 4 ) ); + destBytes[destNdx++] = (byte)( ( b2 << 4 ) | ( b3 >> 2 ) ); + destBytes[destNdx++] = (byte)( ( b3 << 6 ) | b4 ); + } + + // process the remaining bytes + b2 = b3 = 0; + if( remaining > 0 ) { + b1 = B64_DecodeByte( srcBytes[srcNdx++] ); + if( remaining > 1 ) + b2 = B64_DecodeByte( srcBytes[srcNdx++] ); + if( remaining == 3 ) + b3 = B64_DecodeByte( srcBytes[srcNdx++] ); + + destBytes[destNdx++] = (byte)( ( b1 << 2 ) | ( b2 >> 4 ) ); + if( remaining == 3 ) + destBytes[destNdx++] = (byte)( ( b2 << 4 ) | ( b3 >> 2 ) ); + } +} + + +// return the Base-64 encoded char for the given source byte +char B64_EncodeByte( byte b ) +{ + b &= 0x3f; + if( b <= 25 ) + return (byte)( b +'A' ); + if( b <= 51 ) + return (byte)( b - 26 + 'a' ); + if( b <= 61 ) + return (byte)( b - 52 + '0' ); + if( b == 62 ) + return (byte)'-'; + + return (byte)'_'; +} + + +// return the Base-64 decoded byte for the given source char +// <returns></returns> +byte B64_DecodeByte( byte b ) +{ + if (( b == '+' ) || (b =='-') ) + return 62; + if( (b == '/' ) || (b == '_') ) + return 63; + if( b <= '9' ) + return (byte)( b - '0' + 52 ); + if( b <= 'Z' ) + return (byte)( b - 'A' ); + return (byte)( b - 'a' + 26 ); +} + diff --git a/src/dap_enc_bliss.c b/src/dap_enc_bliss.c index 255a29a3efcfb41e950845bb1f736afb6aca4937..371f69929f89b6126fdb27351ce0751f95c3b716 100755 --- a/src/dap_enc_bliss.c +++ b/src/dap_enc_bliss.c @@ -1,311 +1,312 @@ -#include <stdint.h> -#include <assert.h> -#include <inttypes.h> -#include <string.h> - -#include "dap_enc_bliss.h" -#include "dap_common.h" -#include "dap_rand.h" -#include "SimpleFIPS202.h" - -#define LOG_TAG "dap_enc_sig_bliss" - -static enum DAP_BLISS_SIGN_SECURITY _bliss_type = MAX_SECURITY; // by default - -void dap_enc_sig_bliss_set_type(enum DAP_BLISS_SIGN_SECURITY type) -{ - _bliss_type = type; -} - -void dap_enc_sig_bliss_key_new(struct dap_enc_key *key) { - - key->type = DAP_ENC_KEY_TYPE_SIG_BLISS; - key->enc = NULL; - key->gen_bob_shared_key = (dap_enc_gen_bob_shared_key) dap_enc_sig_bliss_get_sign; - key->gen_alice_shared_key = (dap_enc_gen_alice_shared_key) dap_enc_sig_bliss_verify_sign; -} - -/** - * @brief dap_enc_sig_bliss_key_pub_output_size - * @param l_key - * @return - */ -size_t dap_enc_sig_bliss_key_pub_output_size(struct dap_enc_key *l_key) -{ - (void) l_key; - return sizeof(bliss_public_key_t); // Always same, right? -} - -/** - * @brief dap_enc_sig_bliss_key_pub_output - * @param l_key - * @param l_output - * @return - */ -int dap_enc_sig_bliss_key_pub_output(struct dap_enc_key *l_key, void * l_output) -{ - int32_t retcode; - - retcode = bliss_b_public_key_extract((bliss_public_key_t *) l_output, - (const bliss_private_key_t *) l_key->priv_key_data); - if(retcode != BLISS_B_NO_ERROR) { - log_it(L_CRITICAL, "Can't extract public key from the private one"); - return -1; - } - return 0; -} - -// generation key pair for sign Alice -// OUTPUT: -// a_key->data --- Alice's public key -// alice_priv --- Alice's private key -// alice_msg_len --- Alice's private key length -void dap_enc_sig_bliss_key_new_generate(struct dap_enc_key * a_key, const void *kex_buf, - size_t kex_size, const void * seed, size_t seed_size, - size_t key_size) -{ - (void) kex_buf; - (void) kex_size; - (void) seed; - (void) seed_size; - (void) key_size; - - int32_t l_retcode; - - dap_enc_sig_bliss_key_new(a_key); - - uint8_t seed_tmp[SHA3_512_DIGEST_LENGTH]; - entropy_t entropy; - if(seed && seed_size>0){ - assert(SHA3_512_DIGEST_LENGTH==64); - SHA3_512((unsigned char *)seed_tmp, (const unsigned char *)seed, seed_size); - } - else - randombytes(&seed_tmp, 64); - entropy_init(&entropy, seed_tmp); - - /* type is a param of sign-security - * type = 0 - "toy" version (< 60 bits) - * type = 1 - max speed (128 bits) - * type = 2 - min size (128 bits) - * type = 3 - good speed and good security (160 bits) - * type = 4 - max securiry (192 bits) - */ - //int32_t type = 4; - a_key->priv_key_data_size = sizeof(bliss_private_key_t); - a_key->priv_key_data = DAP_NEW_SIZE(void, a_key->priv_key_data_size); - l_retcode = bliss_b_private_key_gen((bliss_private_key_t *) a_key->priv_key_data, _bliss_type, &entropy); - if(l_retcode != BLISS_B_NO_ERROR) { - bliss_b_private_key_delete(a_key->priv_key_data); - a_key->priv_key_data = NULL; - a_key->priv_key_data_size = 0; - log_it(L_CRITICAL, "Error"); - return; - } - - a_key->pub_key_data_size = sizeof(bliss_public_key_t); - a_key->pub_key_data = DAP_NEW_SIZE(void, a_key->pub_key_data_size); - l_retcode = bliss_b_public_key_extract((bliss_public_key_t *) a_key->pub_key_data, - (const bliss_private_key_t *) a_key->priv_key_data); - if(l_retcode != BLISS_B_NO_ERROR) { - bliss_b_private_key_delete(a_key->priv_key_data); - bliss_b_public_key_delete(a_key->pub_key_data); - log_it(L_CRITICAL, "Error"); - return; - } -} - -int dap_enc_sig_bliss_get_sign(struct dap_enc_key * key, const void * msg, - const size_t msg_size, void * signature, const size_t signature_size) -{ - if(signature_size < sizeof(bliss_signature_t)) { - log_it(L_ERROR, "bad signature size"); - return 0; - } - uint8_t seed_tmp[SHA3_512_DIGEST_LENGTH]; - entropy_t entropy; - randombytes(&seed_tmp, 64); - entropy_init(&entropy, seed_tmp); - - return bliss_b_sign((bliss_signature_t *) signature, - (const bliss_private_key_t *) key->priv_key_data, - (const uint8_t *) msg, - msg_size, - &entropy); -} - -int dap_enc_sig_bliss_verify_sign(struct dap_enc_key * key, const void * msg, - const size_t msg_size, void * signature, const size_t signature_size) -{ - if(signature_size < sizeof(bliss_signature_t)) { - log_it(L_ERROR, "bad signature size"); - return -1; - } - return bliss_b_verify(signature, key->pub_key_data, msg, msg_size); -} - -void dap_enc_sig_bliss_key_delete(struct dap_enc_key *key) -{ - if(key->priv_key_data) - bliss_b_private_key_delete(key->priv_key_data); - if(key->pub_key_data) - bliss_b_public_key_delete(key->pub_key_data); -} - -/* Serialize a signature */ -uint8_t* dap_enc_sig_bliss_write_signature(bliss_signature_t* a_sign, size_t *a_sign_out) -{ - bliss_param_t p; - if(!bliss_params_init(&p, a_sign->kind)) { - return NULL ; - } - size_t l_shift_mem = 0; - size_t l_buflen = sizeof(size_t) + sizeof(bliss_kind_t) + p.n * 2 * sizeof(int32_t) + p.kappa * sizeof(int32_t); - - uint8_t *l_buf = DAP_NEW_SIZE(uint8_t, l_buflen); - memcpy(l_buf, &l_buflen, sizeof(size_t)); - l_shift_mem += sizeof(size_t); - memcpy(l_buf + l_shift_mem, &a_sign->kind, sizeof(bliss_kind_t)); - l_shift_mem += sizeof(bliss_kind_t); - memcpy(l_buf + l_shift_mem, a_sign->z1, p.n * sizeof(int32_t)); - l_shift_mem += p.n * sizeof(int32_t); - memcpy(l_buf + l_shift_mem, a_sign->z2, p.n * sizeof(int32_t)); - l_shift_mem += p.n * sizeof(int32_t); - memcpy(l_buf + l_shift_mem, a_sign->c, p.kappa * sizeof(int32_t)); - l_shift_mem += p.kappa * sizeof(int32_t); - - if(a_sign_out) - *a_sign_out = l_buflen; - return l_buf; -} - -/* Deserialize a signature */ -bliss_signature_t* dap_enc_sig_bliss_read_signature(uint8_t *a_buf, size_t a_buflen) -{ - if(!a_buf || a_buflen < (sizeof(size_t) + sizeof(bliss_kind_t))) - return NULL ; - bliss_kind_t kind; - size_t l_buflen = 0; - memcpy(&l_buflen, a_buf, sizeof(size_t)); - memcpy(&kind, a_buf + sizeof(size_t), sizeof(bliss_kind_t)); - if(l_buflen != a_buflen) - return NULL ; - bliss_param_t p; - if(!bliss_params_init(&p, kind)) - return NULL ; - - bliss_signature_t* l_sign = DAP_NEW(bliss_signature_t); - l_sign->kind = kind; - l_sign->z1 = DAP_NEW_SIZE(int32_t, p.n * sizeof(int32_t)); - l_sign->z2 = DAP_NEW_SIZE(int32_t, p.n * sizeof(int32_t)); - l_sign->c = DAP_NEW_SIZE(uint32_t, p.kappa * sizeof(int32_t)); - size_t l_shift_mem = sizeof(size_t) + sizeof(bliss_kind_t); - memcpy(l_sign->z1, a_buf + l_shift_mem, p.n * sizeof(int32_t)); - l_shift_mem += p.n * sizeof(int32_t); - memcpy(l_sign->z2, a_buf + l_shift_mem, p.n * sizeof(int32_t)); - l_shift_mem += p.n * sizeof(int32_t); - memcpy(l_sign->c, a_buf + l_shift_mem, p.kappa * sizeof(int32_t)); - l_shift_mem += p.kappa * sizeof(int32_t); - return l_sign; -} - -/* Serialize a private key. */ -uint8_t* dap_enc_sig_bliss_write_private_key(const bliss_private_key_t* a_private_key, size_t *a_buflen_out) -{ - bliss_param_t p; - if(!bliss_params_init(&p, a_private_key->kind)) { - return NULL; - } - size_t l_shift_mem = 0; - size_t l_buflen = sizeof(size_t) + sizeof(bliss_kind_t) + 3 * p.n * sizeof(int32_t); - uint8_t *l_buf = DAP_NEW_SIZE(uint8_t, l_buflen); - memcpy(l_buf, &l_buflen, sizeof(size_t)); - l_shift_mem += sizeof(size_t); - memcpy(l_buf + l_shift_mem, &a_private_key->kind, sizeof(bliss_kind_t)); - l_shift_mem += sizeof(bliss_kind_t); - memcpy(l_buf + l_shift_mem, a_private_key->s1, p.n * sizeof(int32_t)); - l_shift_mem += p.n * sizeof(int32_t); - memcpy(l_buf + l_shift_mem, a_private_key->s2, p.n * sizeof(int32_t)); - l_shift_mem += p.n * sizeof(int32_t); - memcpy(l_buf + l_shift_mem, a_private_key->a, p.n * sizeof(int32_t)); - l_shift_mem += p.n * sizeof(int32_t); - if(a_buflen_out) - *a_buflen_out = l_buflen; - return l_buf; -} - -/* Serialize a public key. */ -uint8_t* dap_enc_sig_bliss_write_public_key(const bliss_public_key_t* a_public_key, size_t *a_buflen_out) -{ - bliss_param_t p; - - if(!bliss_params_init(&p, a_public_key->kind)) { - return NULL; - } - - size_t l_shift_mem = 0; - size_t l_buflen = sizeof(size_t) + sizeof(bliss_kind_t) + p.n * sizeof(int32_t); - uint8_t *l_buf = DAP_NEW_SIZE(uint8_t, l_buflen); - memcpy(l_buf, &l_buflen, sizeof(size_t)); - l_shift_mem += sizeof(size_t); - memcpy(l_buf + l_shift_mem, &a_public_key->kind, sizeof(bliss_kind_t)); - l_shift_mem += sizeof(bliss_kind_t); - memcpy(l_buf + l_shift_mem, a_public_key->a, p.n * sizeof(int32_t)); - l_shift_mem += p.n * sizeof(int32_t); - if(a_buflen_out) - *a_buflen_out = l_buflen; - return l_buf; -} - -/* Deserialize a private key. */ -bliss_private_key_t* dap_enc_sig_bliss_read_private_key(uint8_t *a_buf, size_t a_buflen) -{ - if(!a_buf || a_buflen < (sizeof(size_t) + sizeof(bliss_kind_t))) - return NULL; - bliss_kind_t kind; - size_t l_buflen = 0; - memcpy(&l_buflen, a_buf, sizeof(size_t)); - memcpy(&kind, a_buf + sizeof(size_t), sizeof(bliss_kind_t)); - if(l_buflen != a_buflen) - return NULL; - bliss_param_t p; - if(!bliss_params_init(&p, kind)) - return NULL; - - bliss_private_key_t* l_private_key = DAP_NEW(bliss_private_key_t); - l_private_key->kind = kind; - - l_private_key->s1 = DAP_NEW_SIZE(int32_t, p.n * sizeof(int32_t)); - l_private_key->s2 = DAP_NEW_SIZE(int32_t, p.n * sizeof(int32_t)); - l_private_key->a = DAP_NEW_SIZE(int32_t, p.n * sizeof(int32_t)); - size_t l_shift_mem = sizeof(size_t) + sizeof(bliss_kind_t); - memcpy(l_private_key->s1, a_buf + l_shift_mem, p.n * sizeof(int32_t)); - l_shift_mem += p.n * sizeof(int32_t); - memcpy(l_private_key->s2, a_buf + l_shift_mem, p.n * sizeof(int32_t)); - l_shift_mem += p.n * sizeof(int32_t); - memcpy(l_private_key->a, a_buf + l_shift_mem, p.n * sizeof(int32_t)); - l_shift_mem += p.n * sizeof(int32_t); - return l_private_key; -} - -/* Deserialize a public key. */ -bliss_public_key_t* dap_enc_sig_bliss_read_public_key(const uint8_t *a_buf, size_t a_buflen) -{ - if(!a_buf || a_buflen < (sizeof(size_t) + sizeof(bliss_kind_t))) - return NULL; - bliss_kind_t kind; - size_t l_buflen = 0; - memcpy(&l_buflen, a_buf, sizeof(size_t)); - memcpy(&kind, a_buf + sizeof(size_t), sizeof(bliss_kind_t)); - if(l_buflen != a_buflen) - return NULL; - bliss_param_t p; - if(!bliss_params_init(&p, kind)) - return NULL; - bliss_public_key_t* l_public_key = DAP_NEW(bliss_public_key_t); - l_public_key->kind = kind; - - l_public_key->a = DAP_NEW_SIZE(int32_t, p.n * sizeof(int32_t)); - memcpy(l_public_key->a, a_buf + sizeof(size_t) + sizeof(bliss_kind_t), p.n * sizeof(int32_t)); - return l_public_key; -} +#include <stdint.h> +#include <assert.h> +#include <inttypes.h> +#include <string.h> + +#include "dap_enc_bliss.h" +#include "dap_common.h" +#include "dap_rand.h" +#include "SimpleFIPS202.h" + +#define LOG_TAG "dap_enc_sig_bliss" + +static enum DAP_BLISS_SIGN_SECURITY _bliss_type = MAX_SECURITY; // by default +bliss_kind_t _bliss_kind = BLISS_B_4; // same as previous as I expect + +void dap_enc_sig_bliss_set_type(enum DAP_BLISS_SIGN_SECURITY type) +{ + _bliss_type = type; +} + +void dap_enc_sig_bliss_key_new(struct dap_enc_key *key) { + + key->type = DAP_ENC_KEY_TYPE_SIG_BLISS; + key->enc = NULL; + key->gen_bob_shared_key = (dap_enc_gen_bob_shared_key) dap_enc_sig_bliss_get_sign; + key->gen_alice_shared_key = (dap_enc_gen_alice_shared_key) dap_enc_sig_bliss_verify_sign; +} + +/** + * @brief dap_enc_sig_bliss_key_pub_output_size + * @param l_key + * @return + */ +size_t dap_enc_sig_bliss_key_pub_output_size(struct dap_enc_key *l_key) +{ + (void) l_key; + return sizeof(bliss_public_key_t); // Always same, right? +} + +/** + * @brief dap_enc_sig_bliss_key_pub_output + * @param l_key + * @param l_output + * @return + */ +int dap_enc_sig_bliss_key_pub_output(struct dap_enc_key *l_key, void * l_output) +{ + int32_t retcode; + + retcode = bliss_b_public_key_extract((bliss_public_key_t *) l_output, + (const bliss_private_key_t *) l_key->priv_key_data); + if(retcode != BLISS_B_NO_ERROR) { + log_it(L_CRITICAL, "Can't extract public key from the private one"); + return -1; + } + return 0; +} + +// generation key pair for sign Alice +// OUTPUT: +// a_key->data --- Alice's public key +// alice_priv --- Alice's private key +// alice_msg_len --- Alice's private key length +void dap_enc_sig_bliss_key_new_generate(struct dap_enc_key * a_key, const void *kex_buf, + size_t kex_size, const void * seed, size_t seed_size, + size_t key_size) +{ + (void) kex_buf; + (void) kex_size; + (void) seed; + (void) seed_size; + (void) key_size; + + int32_t l_retcode; + + dap_enc_sig_bliss_key_new(a_key); + + uint8_t seed_tmp[SHA3_512_DIGEST_LENGTH]; + entropy_t entropy; + if(seed && seed_size>0){ + assert(SHA3_512_DIGEST_LENGTH==64); + SHA3_512((unsigned char *)seed_tmp, (const unsigned char *)seed, seed_size); + } + else + randombytes(&seed_tmp, 64); + entropy_init(&entropy, seed_tmp); + + /* type is a param of sign-security + * type = 0 - "toy" version (< 60 bits) + * type = 1 - max speed (128 bits) + * type = 2 - min size (128 bits) + * type = 3 - good speed and good security (160 bits) + * type = 4 - max securiry (192 bits) + */ + //int32_t type = 4; + a_key->priv_key_data_size = sizeof(bliss_private_key_t); + a_key->priv_key_data = DAP_NEW_SIZE(void, a_key->priv_key_data_size); + l_retcode = bliss_b_private_key_gen((bliss_private_key_t *) a_key->priv_key_data, _bliss_kind, &entropy); + if(l_retcode != BLISS_B_NO_ERROR) { + bliss_b_private_key_delete(a_key->priv_key_data); + a_key->priv_key_data = NULL; + a_key->priv_key_data_size = 0; + log_it(L_CRITICAL, "Error"); + return; + } + + a_key->pub_key_data_size = sizeof(bliss_public_key_t); + a_key->pub_key_data = DAP_NEW_SIZE(void, a_key->pub_key_data_size); + l_retcode = bliss_b_public_key_extract((bliss_public_key_t *) a_key->pub_key_data, + (const bliss_private_key_t *) a_key->priv_key_data); + if(l_retcode != BLISS_B_NO_ERROR) { + bliss_b_private_key_delete(a_key->priv_key_data); + bliss_b_public_key_delete(a_key->pub_key_data); + log_it(L_CRITICAL, "Error"); + return; + } +} + +int dap_enc_sig_bliss_get_sign(struct dap_enc_key * key, const void * msg, + const size_t msg_size, void * signature, const size_t signature_size) +{ + if(signature_size < sizeof(bliss_signature_t)) { + log_it(L_ERROR, "bad signature size"); + return 0; + } + uint8_t seed_tmp[SHA3_512_DIGEST_LENGTH]; + entropy_t entropy; + randombytes(&seed_tmp, 64); + entropy_init(&entropy, seed_tmp); + + return bliss_b_sign((bliss_signature_t *) signature, + (const bliss_private_key_t *) key->priv_key_data, + (const uint8_t *) msg, + msg_size, + &entropy); +} + +int dap_enc_sig_bliss_verify_sign(struct dap_enc_key * key, const void * msg, + const size_t msg_size, void * signature, const size_t signature_size) +{ + if(signature_size < sizeof(bliss_signature_t)) { + log_it(L_ERROR, "bad signature size"); + return -1; + } + return bliss_b_verify(signature, key->pub_key_data, msg, msg_size); +} + +void dap_enc_sig_bliss_key_delete(struct dap_enc_key *key) +{ + if(key->priv_key_data) + bliss_b_private_key_delete(key->priv_key_data); + if(key->pub_key_data) + bliss_b_public_key_delete(key->pub_key_data); +} + +/* Serialize a signature */ +uint8_t* dap_enc_sig_bliss_write_signature(bliss_signature_t* a_sign, size_t *a_sign_out) +{ + bliss_param_t p; + if(!bliss_params_init(&p, a_sign->kind)) { + return NULL ; + } + size_t l_shift_mem = 0; + size_t l_buflen = sizeof(size_t) + sizeof(bliss_kind_t) + p.n * 2 * sizeof(int32_t) + p.kappa * sizeof(int32_t); + + uint8_t *l_buf = DAP_NEW_SIZE(uint8_t, l_buflen); + memcpy(l_buf, &l_buflen, sizeof(size_t)); + l_shift_mem += sizeof(size_t); + memcpy(l_buf + l_shift_mem, &a_sign->kind, sizeof(bliss_kind_t)); + l_shift_mem += sizeof(bliss_kind_t); + memcpy(l_buf + l_shift_mem, a_sign->z1, p.n * sizeof(int32_t)); + l_shift_mem += p.n * sizeof(int32_t); + memcpy(l_buf + l_shift_mem, a_sign->z2, p.n * sizeof(int32_t)); + l_shift_mem += p.n * sizeof(int32_t); + memcpy(l_buf + l_shift_mem, a_sign->c, p.kappa * sizeof(int32_t)); + l_shift_mem += p.kappa * sizeof(int32_t); + + if(a_sign_out) + *a_sign_out = l_buflen; + return l_buf; +} + +/* Deserialize a signature */ +bliss_signature_t* dap_enc_sig_bliss_read_signature(const byte_t *a_buf, size_t a_buflen) +{ + if(!a_buf || a_buflen < (sizeof(size_t) + sizeof(bliss_kind_t))) + return NULL ; + bliss_kind_t kind; + size_t l_buflen = 0; + memcpy(&l_buflen, a_buf, sizeof(size_t)); + memcpy(&kind, a_buf + sizeof(size_t), sizeof(bliss_kind_t)); + if(l_buflen != a_buflen) + return NULL ; + bliss_param_t p; + if(!bliss_params_init(&p, kind)) + return NULL ; + + bliss_signature_t* l_sign = DAP_NEW(bliss_signature_t); + l_sign->kind = kind; + l_sign->z1 = DAP_NEW_SIZE(int32_t, p.n * sizeof(int32_t)); + l_sign->z2 = DAP_NEW_SIZE(int32_t, p.n * sizeof(int32_t)); + l_sign->c = DAP_NEW_SIZE(uint32_t, p.kappa * sizeof(int32_t)); + size_t l_shift_mem = sizeof(size_t) + sizeof(bliss_kind_t); + memcpy(l_sign->z1, a_buf + l_shift_mem, p.n * sizeof(int32_t)); + l_shift_mem += p.n * sizeof(int32_t); + memcpy(l_sign->z2, a_buf + l_shift_mem, p.n * sizeof(int32_t)); + l_shift_mem += p.n * sizeof(int32_t); + memcpy(l_sign->c, a_buf + l_shift_mem, p.kappa * sizeof(int32_t)); + l_shift_mem += p.kappa * sizeof(int32_t); + return l_sign; +} + +/* Serialize a private key. */ +uint8_t* dap_enc_sig_bliss_write_private_key(const bliss_private_key_t* a_private_key, size_t *a_buflen_out) +{ + bliss_param_t p; + if(!bliss_params_init(&p, a_private_key->kind)) { + return NULL; + } + size_t l_shift_mem = 0; + size_t l_buflen = sizeof(size_t) + sizeof(bliss_kind_t) + 3 * p.n * sizeof(int32_t); + uint8_t *l_buf = DAP_NEW_SIZE(uint8_t, l_buflen); + memcpy(l_buf, &l_buflen, sizeof(size_t)); + l_shift_mem += sizeof(size_t); + memcpy(l_buf + l_shift_mem, &a_private_key->kind, sizeof(bliss_kind_t)); + l_shift_mem += sizeof(bliss_kind_t); + memcpy(l_buf + l_shift_mem, a_private_key->s1, p.n * sizeof(int32_t)); + l_shift_mem += p.n * sizeof(int32_t); + memcpy(l_buf + l_shift_mem, a_private_key->s2, p.n * sizeof(int32_t)); + l_shift_mem += p.n * sizeof(int32_t); + memcpy(l_buf + l_shift_mem, a_private_key->a, p.n * sizeof(int32_t)); + l_shift_mem += p.n * sizeof(int32_t); + if(a_buflen_out) + *a_buflen_out = l_buflen; + return l_buf; +} + +/* Serialize a public key. */ +uint8_t* dap_enc_sig_bliss_write_public_key(const bliss_public_key_t* a_public_key, size_t *a_buflen_out) +{ + bliss_param_t p; + + if(!bliss_params_init(&p, a_public_key->kind)) { + return NULL; + } + + size_t l_shift_mem = 0; + size_t l_buflen = sizeof(size_t) + sizeof(bliss_kind_t) + p.n * sizeof(int32_t); + uint8_t *l_buf = DAP_NEW_SIZE(uint8_t, l_buflen); + memcpy(l_buf, &l_buflen, sizeof(size_t)); + l_shift_mem += sizeof(size_t); + memcpy(l_buf + l_shift_mem, &a_public_key->kind, sizeof(bliss_kind_t)); + l_shift_mem += sizeof(bliss_kind_t); + memcpy(l_buf + l_shift_mem, a_public_key->a, p.n * sizeof(int32_t)); + l_shift_mem += p.n * sizeof(int32_t); + if(a_buflen_out) + *a_buflen_out = l_buflen; + return l_buf; +} + +/* Deserialize a private key. */ +bliss_private_key_t* dap_enc_sig_bliss_read_private_key(const uint8_t *a_buf, size_t a_buflen) +{ + if(!a_buf || a_buflen < (sizeof(size_t) + sizeof(bliss_kind_t))) + return NULL; + bliss_kind_t kind; + size_t l_buflen = 0; + memcpy(&l_buflen, a_buf, sizeof(size_t)); + memcpy(&kind, a_buf + sizeof(size_t), sizeof(bliss_kind_t)); + if(l_buflen != a_buflen) + return NULL; + bliss_param_t p; + if(!bliss_params_init(&p, kind)) + return NULL; + + bliss_private_key_t* l_private_key = DAP_NEW(bliss_private_key_t); + l_private_key->kind = kind; + + l_private_key->s1 = DAP_NEW_SIZE(int32_t, p.n * sizeof(int32_t)); + l_private_key->s2 = DAP_NEW_SIZE(int32_t, p.n * sizeof(int32_t)); + l_private_key->a = DAP_NEW_SIZE(int32_t, p.n * sizeof(int32_t)); + size_t l_shift_mem = sizeof(size_t) + sizeof(bliss_kind_t); + memcpy(l_private_key->s1, a_buf + l_shift_mem, p.n * sizeof(int32_t)); + l_shift_mem += p.n * sizeof(int32_t); + memcpy(l_private_key->s2, a_buf + l_shift_mem, p.n * sizeof(int32_t)); + l_shift_mem += p.n * sizeof(int32_t); + memcpy(l_private_key->a, a_buf + l_shift_mem, p.n * sizeof(int32_t)); + l_shift_mem += p.n * sizeof(int32_t); + return l_private_key; +} + +/* Deserialize a public key. */ +bliss_public_key_t* dap_enc_sig_bliss_read_public_key(const uint8_t *a_buf, size_t a_buflen) +{ + if(!a_buf || a_buflen < (sizeof(size_t) + sizeof(bliss_kind_t))) + return NULL; + bliss_kind_t kind; + size_t l_buflen = 0; + memcpy(&l_buflen, a_buf, sizeof(size_t)); + memcpy(&kind, a_buf + sizeof(size_t), sizeof(bliss_kind_t)); + if(l_buflen != a_buflen) + return NULL; + bliss_param_t p; + if(!bliss_params_init(&p, kind)) + return NULL; + bliss_public_key_t* l_public_key = DAP_NEW(bliss_public_key_t); + l_public_key->kind = kind; + + l_public_key->a = DAP_NEW_SIZE(int32_t, p.n * sizeof(int32_t)); + memcpy(l_public_key->a, a_buf + sizeof(size_t) + sizeof(bliss_kind_t), p.n * sizeof(int32_t)); + return l_public_key; +} diff --git a/src/dap_enc_defeo.c b/src/dap_enc_defeo.c index 8b4e171a9f950f1a9e12f6169358159f656b771a..c7e9f1f7a5e66df3df8bf264f053763bcb4ffc5c 100755 --- a/src/dap_enc_defeo.c +++ b/src/dap_enc_defeo.c @@ -1,152 +1,152 @@ - -#include <stdlib.h> -#include <stdio.h> -#include <stddef.h> -#include <stdint.h> -#include <string.h> -#include <stdbool.h> - -#include "dap_enc_defeo.h" -#include "dap_enc_key.h" - -#include "dap_common.h" -#include "defeo_config.h" -#include "defeo_scheme/defeo_P768_internal.h" - - -void dap_enc_defeo_key_new(struct dap_enc_key *a_key) { - - a_key->type = DAP_ENC_KEY_TYPE_DEFEO; - a_key->enc = NULL; - a_key->gen_bob_shared_key = (dap_enc_gen_bob_shared_key) dap_enc_defeo_gen_bob_shared_key; - a_key->gen_alice_shared_key = (dap_enc_gen_alice_shared_key) dap_enc_defeo_gen_alice_shared_key; -} - -// key pair generation of Alice -// OUTPUT: -// a_key->data --- Alice's public key -// alice_priv --- Alice's private key -// alice_msg_len --- Alice's private key length -void dap_enc_defeo_key_new_generate(struct dap_enc_key * a_key, const void *kex_buf, - size_t kex_size, const void * seed, size_t seed_size, - size_t key_size) -{ - (void) kex_buf; (void) kex_size; - (void) seed; (void) seed_size; - (void)key_size; - - dap_enc_defeo_key_new(a_key); - - a_key->pub_key_data = malloc(DEFEO_PUBLICK_KEY_LEN); - a_key->pub_key_data_size = DEFEO_PUBLICK_KEY_LEN; - if(a_key->pub_key_data == NULL) { - log_it(L_CRITICAL, "Error malloc"); - return; - } - - a_key->priv_key_data = malloc(DEFEO_SECRET_KEY_LEN); - a_key->priv_key_data_size = DEFEO_SECRET_KEY_LEN; - - // generate A key pair - random_mod_order_A((unsigned char *) a_key->priv_key_data); - if(EphemeralKeyGeneration_A((unsigned char *) a_key->priv_key_data, (unsigned char *) a_key->pub_key_data) != 0) { - log_it(L_CRITICAL, "Error malloc"); - } - -} - -void dap_enc_defeo_key_delete(struct dap_enc_key *a_key) -{ - (void)a_key; -} - - -// key pair generation and generation of shared key at Bob's side -// INPUT: -// a_pub --- Alice's public key -// a_pub_size --- Alice's public key length -// OUTPUT: -// b_pub --- Bob's public key -// b_key->data --- shared key -// a_pub_size --- shared key length -size_t dap_enc_defeo_gen_bob_shared_key(struct dap_enc_key *b_key, const void *a_pub, - size_t a_pub_size, void ** b_pub) -{ - *b_pub = NULL; - - if(a_pub_size != DEFEO_PUBLICK_KEY_LEN) { - return 1; - } - - *b_pub = malloc(DEFEO_PUBLICK_KEY_LEN); - if(b_pub == NULL) { - log_it(L_CRITICAL, "Error malloc"); - return 2; - } - - b_key->priv_key_data = malloc(DEFEO_SHARED_KEY_LEN); - if(b_key->priv_key_data == NULL) { - log_it(L_CRITICAL, "Error malloc"); - return 3; - } - - uint8_t *bob_priv = malloc(DEFEO_SECRET_KEY_LEN); - - // generate Bob's key pair - random_mod_order_B((unsigned char *)bob_priv); - if(EphemeralKeyGeneration_B((unsigned char *) bob_priv, (unsigned char *) b_key->pub_key_data) != 0) { - log_it(L_CRITICAL, "Error malloc"); - return 1; - } - - // compute Bob's shared secret - if(EphemeralSecretAgreement_B((unsigned char *) bob_priv, (unsigned char *) a_pub, - (unsigned char *) b_key->priv_key_data) != 0) { - log_it(L_CRITICAL, "Error malloc"); - return 2; - } - - free(bob_priv); - b_key->priv_key_data_size = DEFEO_SHARED_KEY_LEN; - b_key->pub_key_data_size = DEFEO_PUBLICK_KEY_LEN; - - return 0; -} - - -// generation of shared key at Alice's side -// INPUT: -// a_priv --- Alice's private key -// b_pub --- Bob's public key -// OUTPUT: -// a_key->priv_key_data --- shared key -// a_key_len --- shared key length -size_t dap_enc_defeo_gen_alice_shared_key(struct dap_enc_key *a_key, const void *a_priv, size_t b_pub_size, unsigned char *b_pub) -{ - size_t ret_val = 0; - if(b_pub_size != DEFEO_PUBLICK_KEY_LEN) { - log_it(L_ERROR, "public key size not equal DEFEO_PUBLICKEYBYTES"); - return 1; - } - void *oldkey = NULL; - if(a_key->priv_key_data && a_key->priv_key_data_size > 0) - oldkey = a_key->priv_key_data; - a_key->priv_key_data = malloc(DEFEO_SHARED_KEY_LEN); - - if(a_key->priv_key_data == NULL) { - log_it(L_CRITICAL, "Error malloc"); - ret_val = 2; - } - - if(EphemeralSecretAgreement_A((unsigned char *) a_priv, (unsigned char *) b_pub, - (unsigned char *) a_key->priv_key_data) != 0) { - log_it(L_ERROR, "Error EphemeralSecretAgreement_A"); - ret_val = 3; - } - - if(oldkey) - free(oldkey); - if(!ret_val) - a_key->priv_key_data_size = DEFEO_SHARED_KEY_LEN; - return ret_val; -} + +#include <stdlib.h> +#include <stdio.h> +#include <stddef.h> +#include <stdint.h> +#include <string.h> +#include <stdbool.h> + +#include "dap_enc_defeo.h" +#include "dap_enc_key.h" + +#include "dap_common.h" +#include "defeo_config.h" +#include "defeo_scheme/defeo_P768_internal.h" + + +void dap_enc_defeo_key_new(struct dap_enc_key *a_key) { + + a_key->type = DAP_ENC_KEY_TYPE_DEFEO; + a_key->enc = NULL; + a_key->gen_bob_shared_key = (dap_enc_gen_bob_shared_key) dap_enc_defeo_gen_bob_shared_key; + a_key->gen_alice_shared_key = (dap_enc_gen_alice_shared_key) dap_enc_defeo_gen_alice_shared_key; +} + +// key pair generation of Alice +// OUTPUT: +// a_key->data --- Alice's public key +// alice_priv --- Alice's private key +// alice_msg_len --- Alice's private key length +void dap_enc_defeo_key_new_generate(struct dap_enc_key * a_key, const void *kex_buf, + size_t kex_size, const void * seed, size_t seed_size, + size_t key_size) +{ + (void) kex_buf; (void) kex_size; + (void) seed; (void) seed_size; + (void)key_size; + + dap_enc_defeo_key_new(a_key); + + a_key->pub_key_data = malloc(DEFEO_PUBLICK_KEY_LEN); + a_key->pub_key_data_size = DEFEO_PUBLICK_KEY_LEN; + if(a_key->pub_key_data == NULL) { + log_it(L_CRITICAL, "Error malloc"); + return; + } + + a_key->priv_key_data = malloc(DEFEO_SECRET_KEY_LEN); + a_key->priv_key_data_size = DEFEO_SECRET_KEY_LEN; + + // generate A key pair + random_mod_order_A((unsigned char *) a_key->priv_key_data); + if(EphemeralKeyGeneration_A((unsigned char *) a_key->priv_key_data, (unsigned char *) a_key->pub_key_data) != 0) { + log_it(L_CRITICAL, "Error malloc"); + } + +} + +void dap_enc_defeo_key_delete(struct dap_enc_key *a_key) +{ + (void)a_key; +} + + +// key pair generation and generation of shared key at Bob's side +// INPUT: +// a_pub --- Alice's public key +// a_pub_size --- Alice's public key length +// OUTPUT: +// b_pub --- Bob's public key +// b_key->data --- shared key +// a_pub_size --- shared key length +size_t dap_enc_defeo_gen_bob_shared_key(struct dap_enc_key *b_key, const void *a_pub, + size_t a_pub_size, void ** b_pub) +{ + *b_pub = NULL; + + if(a_pub_size != DEFEO_PUBLICK_KEY_LEN) { + return 1; + } + + *b_pub = malloc(DEFEO_PUBLICK_KEY_LEN); + if(b_pub == NULL) { + log_it(L_CRITICAL, "Error malloc"); + return 2; + } + + b_key->priv_key_data = malloc(DEFEO_SHARED_KEY_LEN); + if(b_key->priv_key_data == NULL) { + log_it(L_CRITICAL, "Error malloc"); + return 3; + } + + uint8_t *bob_priv = malloc(DEFEO_SECRET_KEY_LEN); + + // generate Bob's key pair + random_mod_order_B((unsigned char *)bob_priv); + if(EphemeralKeyGeneration_B((unsigned char *) bob_priv, (unsigned char *) b_key->pub_key_data) != 0) { + log_it(L_CRITICAL, "Error malloc"); + return 1; + } + + // compute Bob's shared secret + if(EphemeralSecretAgreement_B((unsigned char *) bob_priv, (unsigned char *) a_pub, + (unsigned char *) b_key->priv_key_data) != 0) { + log_it(L_CRITICAL, "Error malloc"); + return 2; + } + + free(bob_priv); + b_key->priv_key_data_size = DEFEO_SHARED_KEY_LEN; + b_key->pub_key_data_size = DEFEO_PUBLICK_KEY_LEN; + + return 0; +} + + +// generation of shared key at Alice's side +// INPUT: +// a_priv --- Alice's private key +// b_pub --- Bob's public key +// OUTPUT: +// a_key->priv_key_data --- shared key +// a_key_len --- shared key length +size_t dap_enc_defeo_gen_alice_shared_key(struct dap_enc_key *a_key, const void *a_priv, size_t b_pub_size, unsigned char *b_pub) +{ + size_t ret_val = 0; + if(b_pub_size != DEFEO_PUBLICK_KEY_LEN) { + log_it(L_ERROR, "public key size not equal DEFEO_PUBLICKEYBYTES"); + return 1; + } + void *oldkey = NULL; + if(a_key->priv_key_data && a_key->priv_key_data_size > 0) + oldkey = a_key->priv_key_data; + a_key->priv_key_data = malloc(DEFEO_SHARED_KEY_LEN); + + if(a_key->priv_key_data == NULL) { + log_it(L_CRITICAL, "Error malloc"); + ret_val = 2; + } + + if(EphemeralSecretAgreement_A((unsigned char *) a_priv, (unsigned char *) b_pub, + (unsigned char *) a_key->priv_key_data) != 0) { + log_it(L_ERROR, "Error EphemeralSecretAgreement_A"); + ret_val = 3; + } + + if(oldkey) + free(oldkey); + if(!ret_val) + a_key->priv_key_data_size = DEFEO_SHARED_KEY_LEN; + return ret_val; +} diff --git a/src/dap_enc_dilithium.c b/src/dap_enc_dilithium.c index c530ef8c49ab01e83c78fee69004461822984ed9..265299e8d4ab042dcd123804d7c7ffe59edfd0c2 100755 --- a/src/dap_enc_dilithium.c +++ b/src/dap_enc_dilithium.c @@ -1,235 +1,235 @@ -#include <assert.h> -#include <inttypes.h> -#include <string.h> - -#include "dap_enc_dilithium.h" -#include "dap_common.h" -#include "dap_rand.h" - -#define LOG_TAG "dap_enc_sig_dilithium" - -static enum DAP_DILITHIUM_SIGN_SECURITY _dilithium_type = DILITHIUM_MIN_SIZE; // by default - -void dap_enc_sig_dilithium_set_type(enum DAP_DILITHIUM_SIGN_SECURITY type) -{ - _dilithium_type = type; -} - -void dap_enc_sig_dilithium_key_new(struct dap_enc_key *key) { - - key->type = DAP_ENC_KEY_TYPE_SIG_DILITHIUM; - key->enc = NULL; - key->enc_na = (dap_enc_callback_dataop_na_t) dap_enc_sig_dilithium_get_sign; - key->dec_na = (dap_enc_callback_dataop_na_t) dap_enc_sig_dilithium_verify_sign; -// key->gen_bob_shared_key = (dap_enc_gen_bob_shared_key) dap_enc_sig_dilithium_get_sign; -// key->gen_alice_shared_key = (dap_enc_gen_alice_shared_key) dap_enc_sig_dilithium_verify_sign; -} - -// generation key pair for sign Alice -// OUTPUT: -// a_key->data --- Alice's public key -// alice_priv --- Alice's private key -// alice_msg_len --- Alice's private key length -void dap_enc_sig_dilithium_key_new_generate(struct dap_enc_key * key, const void *kex_buf, - size_t kex_size, const void * seed, size_t seed_size, - size_t key_size) -{ - (void) kex_buf; - (void) kex_size; - (void) key_size; - - int32_t retcode; - - int dilithium_type = (seed && seed_size >= sizeof(uint8_t)) ? ((uint8_t*)seed)[0] % (DILITHIUM_MAX_SECURITY + 1) : - DILITHIUM_MIN_SIZE; - dap_enc_sig_dilithium_set_type(dilithium_type); - - - //int32_t type = 2; - key->priv_key_data_size = sizeof(dilithium_private_key_t); - key->pub_key_data_size = sizeof(dilithium_public_key_t); - key->priv_key_data = malloc(key->priv_key_data_size); - key->pub_key_data = malloc(key->pub_key_data_size); - - retcode = dilithium_crypto_sign_keypair((dilithium_public_key_t *) key->pub_key_data, - (dilithium_private_key_t *) key->priv_key_data, _dilithium_type, seed, seed_size); - if(retcode != 0) { - dilithium_private_and_public_keys_delete((dilithium_private_key_t *) key->pub_key_data, - (dilithium_public_key_t *) key->pub_key_data); - log_it(L_CRITICAL, "Error"); - return; - } -} - -size_t dap_enc_sig_dilithium_get_sign(struct dap_enc_key * key, const void * msg, - const size_t msg_size, void * signature, const size_t signature_size) -{ - if(signature_size < sizeof(dilithium_signature_t)) { - log_it(L_ERROR, "bad signature size"); - return 0; - } - - if(!dilithium_crypto_sign((dilithium_signature_t *) signature, (const unsigned char *) msg, msg_size, key->priv_key_data)) - return signature_size; - else - return 0; -} - -size_t dap_enc_sig_dilithium_verify_sign(struct dap_enc_key * key, const void * msg, - const size_t msg_size, void * signature, const size_t signature_size) -{ - if(signature_size < sizeof(dilithium_signature_t)) { - log_it(L_ERROR, "bad signature size"); - return 0; - } - - return (dilithium_crypto_sign_open( (unsigned char *) msg, msg_size, (dilithium_signature_t *) signature, key->pub_key_data)); -} - -void dap_enc_sig_dilithium_key_delete(struct dap_enc_key * key) -{ - if( key->priv_key_data && key->pub_key_data) - dilithium_private_and_public_keys_delete((dilithium_private_key_t *) key->priv_key_data, - (dilithium_public_key_t *) key->pub_key_data); - else if ( key->pub_key_data ) - dilithium_public_key_delete((dilithium_public_key_t *) key->pub_key_data); - else if ( key->priv_key_data ) - dilithium_public_key_delete((dilithium_public_key_t *) key->priv_key_data); - -} - -size_t dap_enc_dilithium_calc_signature_unserialized_size(void) -{ - return sizeof(dilithium_signature_t); -} - - - - -/* Serialize a signature */ -uint8_t* dap_enc_dilithium_write_signature(dilithium_signature_t* a_sign, size_t *a_sign_out) -{ - if(!a_sign ) { - return NULL ; - } - size_t l_shift_mem = 0; - size_t l_buflen = dap_enc_dilithium_calc_signagture_size(a_sign); - - uint8_t *l_buf = DAP_NEW_SIZE(uint8_t, l_buflen); - memcpy(l_buf, &l_buflen, sizeof(size_t)); - l_shift_mem += sizeof(size_t); - memcpy(l_buf + l_shift_mem, &a_sign->kind, sizeof(dilithium_kind_t)); - l_shift_mem += sizeof(dilithium_kind_t); - memcpy(l_buf + l_shift_mem, &a_sign->sig_len, sizeof(unsigned long long)); - l_shift_mem += sizeof(unsigned long long); - memcpy(l_buf + l_shift_mem, a_sign->sig_data, a_sign->sig_len ); - l_shift_mem += a_sign->sig_len ; - - if(a_sign_out) - *a_sign_out = l_buflen; - return l_buf; -} - -/* Deserialize a signature */ -dilithium_signature_t* dap_enc_dilithium_read_signature(uint8_t *a_buf, size_t a_buflen) -{ - if(!a_buf || a_buflen < (sizeof(size_t) + sizeof(dilithium_kind_t))) - return NULL ; - dilithium_kind_t kind; - size_t l_buflen = 0; - memcpy(&l_buflen, a_buf, sizeof(size_t)); - memcpy(&kind, a_buf + sizeof(size_t), sizeof(dilithium_kind_t)); - if(l_buflen != a_buflen) - return NULL ; - dilithium_param_t p; - if(!dilithium_params_init(&p, kind)) - return NULL ; - - dilithium_signature_t* l_sign = DAP_NEW(dilithium_signature_t); - l_sign->kind = kind; - size_t l_shift_mem = sizeof(size_t) + sizeof(dilithium_kind_t); - memcpy(&l_sign->sig_len, a_buf + l_shift_mem, sizeof(unsigned long long)); - l_shift_mem += sizeof(unsigned long long); - l_sign->sig_data = DAP_NEW_SIZE(unsigned char, l_sign->sig_len); - memcpy(l_sign->sig_data, a_buf + l_shift_mem, l_sign->sig_len); - l_shift_mem += l_sign->sig_len; - return l_sign; -} - -/* Serialize a private key. */ -uint8_t* dap_enc_dilithium_write_private_key(const dilithium_private_key_t* a_private_key, size_t *a_buflen_out) -{ - dilithium_param_t p;// = malloc(sizeof(dilithium_param_t)); - if(!dilithium_params_init(&p, a_private_key->kind)) - return NULL; - - size_t l_buflen = sizeof(size_t) + sizeof(dilithium_kind_t) + p.CRYPTO_SECRETKEYBYTES; //CRYPTO_PUBLICKEYBYTES; - uint8_t *l_buf = DAP_NEW_SIZE(uint8_t, l_buflen); - memcpy(l_buf, &l_buflen, sizeof(size_t)); - memcpy(l_buf + sizeof(size_t), &a_private_key->kind, sizeof(dilithium_kind_t)); - memcpy(l_buf + sizeof(size_t) + sizeof(dilithium_kind_t), a_private_key->data, p.CRYPTO_SECRETKEYBYTES); - if(a_buflen_out) - *a_buflen_out = l_buflen; - return l_buf; -} - -/* Serialize a public key. */ -uint8_t* dap_enc_dilithium_write_public_key(const dilithium_public_key_t* a_public_key, size_t *a_buflen_out) -{ - dilithium_param_t p; - if(!dilithium_params_init(&p, a_public_key->kind)) - return NULL; - - size_t l_buflen = sizeof(size_t) + sizeof(dilithium_kind_t) + p.CRYPTO_PUBLICKEYBYTES; - uint8_t *l_buf = DAP_NEW_SIZE(uint8_t, l_buflen); - memcpy(l_buf, &l_buflen, sizeof(size_t)); - memcpy(l_buf + sizeof(size_t), &a_public_key->kind, sizeof(dilithium_kind_t)); - memcpy(l_buf + sizeof(size_t) + sizeof(dilithium_kind_t), a_public_key->data, p.CRYPTO_PUBLICKEYBYTES); - if(a_buflen_out) - *a_buflen_out = l_buflen; - return l_buf; -} - -/* Deserialize a private key. */ -dilithium_private_key_t* dap_enc_dilithium_read_private_key(const uint8_t *a_buf, size_t a_buflen) -{ - if(!a_buf || a_buflen < (sizeof(size_t) + sizeof(dilithium_kind_t))) - return NULL; - dilithium_kind_t kind; - size_t l_buflen = 0; - memcpy(&l_buflen, a_buf, sizeof(size_t)); - memcpy(&kind, a_buf + sizeof(size_t), sizeof(dilithium_kind_t)); - if(l_buflen != a_buflen) - return NULL; - dilithium_param_t p; - if(!dilithium_params_init(&p, kind)) - return NULL; - dilithium_private_key_t* l_private_key = DAP_NEW(dilithium_private_key_t); - l_private_key->kind = kind; - - l_private_key->data = DAP_NEW_SIZE(unsigned char, p.CRYPTO_SECRETKEYBYTES); - memcpy(l_private_key->data, a_buf + sizeof(size_t) + sizeof(dilithium_kind_t), p.CRYPTO_SECRETKEYBYTES); - return l_private_key; -} - -/* Deserialize a public key. */ -dilithium_public_key_t* dap_enc_dilithium_read_public_key(const uint8_t *a_buf, size_t a_buflen) -{ - if(!a_buf || a_buflen < (sizeof(size_t) + sizeof(dilithium_kind_t))) - return NULL; - dilithium_kind_t kind; - size_t l_buflen = 0; - memcpy(&l_buflen, a_buf, sizeof(size_t)); - memcpy(&kind, a_buf + sizeof(size_t), sizeof(dilithium_kind_t)); - if(l_buflen != a_buflen) - return NULL; - dilithium_param_t p; - if(!dilithium_params_init(&p, kind)) - return NULL; - dilithium_public_key_t* l_public_key = DAP_NEW_Z(dilithium_public_key_t); - l_public_key->kind = kind; - - l_public_key->data = DAP_NEW_Z_SIZE(unsigned char, p.CRYPTO_PUBLICKEYBYTES); - memcpy(l_public_key->data, a_buf + sizeof(size_t) + sizeof(dilithium_kind_t), p.CRYPTO_PUBLICKEYBYTES); - return l_public_key; -} +#include <assert.h> +#include <inttypes.h> +#include <string.h> + +#include "dap_enc_dilithium.h" +#include "dap_common.h" +#include "dap_rand.h" + +#define LOG_TAG "dap_enc_sig_dilithium" + +static enum DAP_DILITHIUM_SIGN_SECURITY _dilithium_type = DILITHIUM_MIN_SIZE; // by default + +void dap_enc_sig_dilithium_set_type(enum DAP_DILITHIUM_SIGN_SECURITY type) +{ + _dilithium_type = type; +} + +void dap_enc_sig_dilithium_key_new(struct dap_enc_key *key) { + + key->type = DAP_ENC_KEY_TYPE_SIG_DILITHIUM; + key->enc = NULL; + key->enc_na = (dap_enc_callback_dataop_na_t) dap_enc_sig_dilithium_get_sign; + key->dec_na = (dap_enc_callback_dataop_na_t) dap_enc_sig_dilithium_verify_sign; +// key->gen_bob_shared_key = (dap_enc_gen_bob_shared_key) dap_enc_sig_dilithium_get_sign; +// key->gen_alice_shared_key = (dap_enc_gen_alice_shared_key) dap_enc_sig_dilithium_verify_sign; +} + +// generation key pair for sign Alice +// OUTPUT: +// a_key->data --- Alice's public key +// alice_priv --- Alice's private key +// alice_msg_len --- Alice's private key length +void dap_enc_sig_dilithium_key_new_generate(struct dap_enc_key * key, const void *kex_buf, + size_t kex_size, const void * seed, size_t seed_size, + size_t key_size) +{ + (void) kex_buf; + (void) kex_size; + (void) key_size; + + int32_t retcode; + + int dilithium_type = (seed && seed_size >= sizeof(uint8_t)) ? ((uint8_t*)seed)[0] % (DILITHIUM_MAX_SECURITY + 1) : + DILITHIUM_MIN_SIZE; + dap_enc_sig_dilithium_set_type(dilithium_type); + + + //int32_t type = 2; + key->priv_key_data_size = sizeof(dilithium_private_key_t); + key->pub_key_data_size = sizeof(dilithium_public_key_t); + key->priv_key_data = malloc(key->priv_key_data_size); + key->pub_key_data = malloc(key->pub_key_data_size); + + retcode = dilithium_crypto_sign_keypair((dilithium_public_key_t *) key->pub_key_data, + (dilithium_private_key_t *) key->priv_key_data, _dilithium_type, seed, seed_size); + if(retcode != 0) { + dilithium_private_and_public_keys_delete((dilithium_private_key_t *) key->pub_key_data, + (dilithium_public_key_t *) key->pub_key_data); + log_it(L_CRITICAL, "Error"); + return; + } +} + +size_t dap_enc_sig_dilithium_get_sign(struct dap_enc_key * key, const void * msg, + const size_t msg_size, void * signature, const size_t signature_size) +{ + if(signature_size < sizeof(dilithium_signature_t)) { + log_it(L_ERROR, "bad signature size"); + return 0; + } + + if(!dilithium_crypto_sign((dilithium_signature_t *) signature, (const unsigned char *) msg, msg_size, key->priv_key_data)) + return signature_size; + else + return 0; +} + +size_t dap_enc_sig_dilithium_verify_sign(struct dap_enc_key * key, const void * msg, + const size_t msg_size, void * signature, const size_t signature_size) +{ + if(signature_size < sizeof(dilithium_signature_t)) { + log_it(L_ERROR, "bad signature size"); + return 0; + } + + return (dilithium_crypto_sign_open( (unsigned char *) msg, msg_size, (dilithium_signature_t *) signature, key->pub_key_data)); +} + +void dap_enc_sig_dilithium_key_delete(struct dap_enc_key * key) +{ + if( key->priv_key_data && key->pub_key_data) + dilithium_private_and_public_keys_delete((dilithium_private_key_t *) key->priv_key_data, + (dilithium_public_key_t *) key->pub_key_data); + else if ( key->pub_key_data ) + dilithium_public_key_delete((dilithium_public_key_t *) key->pub_key_data); + else if ( key->priv_key_data ) + dilithium_public_key_delete((dilithium_public_key_t *) key->priv_key_data); + +} + +size_t dap_enc_dilithium_calc_signature_unserialized_size(void) +{ + return sizeof(dilithium_signature_t); +} + + + + +/* Serialize a signature */ +uint8_t* dap_enc_dilithium_write_signature(dilithium_signature_t* a_sign, size_t *a_sign_out) +{ + if(!a_sign ) { + return NULL ; + } + size_t l_shift_mem = 0; + size_t l_buflen = dap_enc_dilithium_calc_signagture_size(a_sign); + + uint8_t *l_buf = DAP_NEW_SIZE(uint8_t, l_buflen); + memcpy(l_buf, &l_buflen, sizeof(size_t)); + l_shift_mem += sizeof(size_t); + memcpy(l_buf + l_shift_mem, &a_sign->kind, sizeof(dilithium_kind_t)); + l_shift_mem += sizeof(dilithium_kind_t); + memcpy(l_buf + l_shift_mem, &a_sign->sig_len, sizeof(unsigned long long)); + l_shift_mem += sizeof(unsigned long long); + memcpy(l_buf + l_shift_mem, a_sign->sig_data, a_sign->sig_len ); + l_shift_mem += a_sign->sig_len ; + + if(a_sign_out) + *a_sign_out = l_buflen; + return l_buf; +} + +/* Deserialize a signature */ +dilithium_signature_t* dap_enc_dilithium_read_signature(uint8_t *a_buf, size_t a_buflen) +{ + if(!a_buf || a_buflen < (sizeof(size_t) + sizeof(dilithium_kind_t))) + return NULL ; + dilithium_kind_t kind; + size_t l_buflen = 0; + memcpy(&l_buflen, a_buf, sizeof(size_t)); + memcpy(&kind, a_buf + sizeof(size_t), sizeof(dilithium_kind_t)); + if(l_buflen != a_buflen) + return NULL ; + dilithium_param_t p; + if(!dilithium_params_init(&p, kind)) + return NULL ; + + dilithium_signature_t* l_sign = DAP_NEW(dilithium_signature_t); + l_sign->kind = kind; + size_t l_shift_mem = sizeof(size_t) + sizeof(dilithium_kind_t); + memcpy(&l_sign->sig_len, a_buf + l_shift_mem, sizeof(unsigned long long)); + l_shift_mem += sizeof(unsigned long long); + l_sign->sig_data = DAP_NEW_SIZE(unsigned char, l_sign->sig_len); + memcpy(l_sign->sig_data, a_buf + l_shift_mem, l_sign->sig_len); + l_shift_mem += l_sign->sig_len; + return l_sign; +} + +/* Serialize a private key. */ +uint8_t* dap_enc_dilithium_write_private_key(const dilithium_private_key_t* a_private_key, size_t *a_buflen_out) +{ + dilithium_param_t p;// = malloc(sizeof(dilithium_param_t)); + if(!dilithium_params_init(&p, a_private_key->kind)) + return NULL; + + size_t l_buflen = sizeof(size_t) + sizeof(dilithium_kind_t) + p.CRYPTO_SECRETKEYBYTES; //CRYPTO_PUBLICKEYBYTES; + uint8_t *l_buf = DAP_NEW_SIZE(uint8_t, l_buflen); + memcpy(l_buf, &l_buflen, sizeof(size_t)); + memcpy(l_buf + sizeof(size_t), &a_private_key->kind, sizeof(dilithium_kind_t)); + memcpy(l_buf + sizeof(size_t) + sizeof(dilithium_kind_t), a_private_key->data, p.CRYPTO_SECRETKEYBYTES); + if(a_buflen_out) + *a_buflen_out = l_buflen; + return l_buf; +} + +/* Serialize a public key. */ +uint8_t* dap_enc_dilithium_write_public_key(const dilithium_public_key_t* a_public_key, size_t *a_buflen_out) +{ + dilithium_param_t p; + if(!dilithium_params_init(&p, a_public_key->kind)) + return NULL; + + size_t l_buflen = sizeof(size_t) + sizeof(dilithium_kind_t) + p.CRYPTO_PUBLICKEYBYTES; + uint8_t *l_buf = DAP_NEW_SIZE(uint8_t, l_buflen); + memcpy(l_buf, &l_buflen, sizeof(size_t)); + memcpy(l_buf + sizeof(size_t), &a_public_key->kind, sizeof(dilithium_kind_t)); + memcpy(l_buf + sizeof(size_t) + sizeof(dilithium_kind_t), a_public_key->data, p.CRYPTO_PUBLICKEYBYTES); + if(a_buflen_out) + *a_buflen_out = l_buflen; + return l_buf; +} + +/* Deserialize a private key. */ +dilithium_private_key_t* dap_enc_dilithium_read_private_key(const uint8_t *a_buf, size_t a_buflen) +{ + if(!a_buf || a_buflen < (sizeof(size_t) + sizeof(dilithium_kind_t))) + return NULL; + dilithium_kind_t kind; + size_t l_buflen = 0; + memcpy(&l_buflen, a_buf, sizeof(size_t)); + memcpy(&kind, a_buf + sizeof(size_t), sizeof(dilithium_kind_t)); + if(l_buflen != a_buflen) + return NULL; + dilithium_param_t p; + if(!dilithium_params_init(&p, kind)) + return NULL; + dilithium_private_key_t* l_private_key = DAP_NEW(dilithium_private_key_t); + l_private_key->kind = kind; + + l_private_key->data = DAP_NEW_SIZE(unsigned char, p.CRYPTO_SECRETKEYBYTES); + memcpy(l_private_key->data, a_buf + sizeof(size_t) + sizeof(dilithium_kind_t), p.CRYPTO_SECRETKEYBYTES); + return l_private_key; +} + +/* Deserialize a public key. */ +dilithium_public_key_t* dap_enc_dilithium_read_public_key(const uint8_t *a_buf, size_t a_buflen) +{ + if(!a_buf || a_buflen < (sizeof(size_t) + sizeof(dilithium_kind_t))) + return NULL; + dilithium_kind_t kind; + size_t l_buflen = 0; + memcpy(&l_buflen, a_buf, sizeof(size_t)); + memcpy(&kind, a_buf + sizeof(size_t), sizeof(dilithium_kind_t)); + if(l_buflen != a_buflen) + return NULL; + dilithium_param_t p; + if(!dilithium_params_init(&p, kind)) + return NULL; + dilithium_public_key_t* l_public_key = DAP_NEW_Z(dilithium_public_key_t); + l_public_key->kind = kind; + + l_public_key->data = DAP_NEW_Z_SIZE(unsigned char, p.CRYPTO_PUBLICKEYBYTES); + memcpy(l_public_key->data, a_buf + sizeof(size_t) + sizeof(dilithium_kind_t), p.CRYPTO_PUBLICKEYBYTES); + return l_public_key; +} diff --git a/src/dap_enc_iaes.c b/src/dap_enc_iaes.c index a3636d40cc614b977715f17bc7518b8398b6e730..f55d3bb30c959e435aa064b857e231dff9d33982 100755 --- a/src/dap_enc_iaes.c +++ b/src/dap_enc_iaes.c @@ -1,151 +1,151 @@ -#include <stdlib.h> -#include <stdio.h> -#include <string.h> -#include "dap_enc_key.h" -#include "dap_enc_iaes.h" -#include "sha3/fips202.h" - -//#include "KeccakHash.h" -//#include "SimpleFIPS202.h" - -#include "dap_common.h" - - -#define LOG_TAG "dap_enc_aes" - -typedef struct dap_enc_aes_key { - unsigned char ivec[IAES_BLOCK_SIZE]; -} dap_enc_aes_key_t; - -#define DAP_ENC_AES_KEY(a) ((dap_enc_aes_key_t *)((a)->_inheritor) ) - -void dap_enc_aes_key_delete(struct dap_enc_key *a_key) -{ - free(a_key->_inheritor); - //No need any specific actions -} - -/** - * @brief dap_enc_aes_key_new_generate - * @param a_key - * @param a_size - */ -void dap_enc_aes_key_new(struct dap_enc_key * a_key) -{ - a_key->_inheritor = DAP_NEW_Z(dap_enc_aes_key_t); - a_key->_inheritor_size = sizeof (dap_enc_aes_key_t); - a_key->type = DAP_ENC_KEY_TYPE_IAES; - a_key->enc = dap_enc_iaes256_cbc_encrypt; - a_key->dec = dap_enc_iaes256_cbc_decrypt; - a_key->enc_na = dap_enc_iaes256_cbc_encrypt_fast; - a_key->dec_na = dap_enc_iaes256_cbc_decrypt_fast; - //a_key->delete_callback = dap_enc_aes_key_delete; - - a_key->priv_key_data = (uint8_t *)malloc(IAES_KEYSIZE); - a_key->priv_key_data_size = IAES_KEYSIZE; -} - -void dap_enc_aes_key_generate(struct dap_enc_key * a_key, const void *kex_buf, - size_t kex_size, const void * seed, size_t seed_size, - size_t key_size) -{ - (void)key_size; - a_key->last_used_timestamp = time(NULL); - - uint8_t * id_concat_kex = (uint8_t *) malloc(kex_size + seed_size); - - memcpy(id_concat_kex,seed, seed_size); - memcpy(id_concat_kex + seed_size, kex_buf, kex_size); - //SHAKE256(a_key->priv_key_data, IAES_KEYSIZE, id_concat_kex, (kex_size + seed_size)); - //SHAKE128(DAP_ENC_AES_KEY(a_key)->ivec, IAES_BLOCK_SIZE, seed, seed_size); - shake256(a_key->priv_key_data, IAES_KEYSIZE, id_concat_kex, (kex_size + seed_size)); - shake128(DAP_ENC_AES_KEY(a_key)->ivec, IAES_BLOCK_SIZE, seed, seed_size); - - free(id_concat_kex); -} - - -/** - * @brief ap_enc_aes256_cbc_decrypt - * @param a_key - * @param a_in - * @param a_in_size - * @param a_out - * @return - */ - -size_t dap_enc_iaes256_cbc_decrypt(struct dap_enc_key * a_key, const void * a_in, size_t a_in_size, void ** a_out) -{ - if (a_in_size % 16) { - log_it(L_ERROR, "Bad in data size"); - return 0; - } - - *a_out = (uint8_t *) malloc(a_in_size); - - return IAES_256_CBC_decrypt(a_in, *a_out, DAP_ENC_AES_KEY(a_key)->ivec, a_in_size, a_key->priv_key_data); -} - -size_t dap_enc_iaes256_cbc_decrypt_fast(struct dap_enc_key * a_key, const void * a_in, - size_t a_in_size, void * buf_out, size_t buf_out_size) -{ - if (a_in_size % 16) { - log_it(L_ERROR, "Bad in size"); - return 0; - } else if(buf_out_size < a_in_size) { - log_it(L_ERROR, "buf_out_size < a_in_size"); - return 0; - } - - return IAES_256_CBC_decrypt(a_in, buf_out, DAP_ENC_AES_KEY(a_key)->ivec, - a_in_size, a_key->priv_key_data); -} - -size_t dap_enc_iaes256_cbc_encrypt(struct dap_enc_key * a_key, const void * a_in, size_t a_in_size, void ** a_out) -{ - size_t length_data_new; - uint8_t *data_new; - - length_data_new = iaes_block128_padding(a_in, &data_new, a_in_size); - *a_out = (uint8_t *)malloc(length_data_new); - - IAES_256_CBC_encrypt(data_new, *a_out, DAP_ENC_AES_KEY(a_key)->ivec, length_data_new, a_key->priv_key_data); - - free(data_new); - return length_data_new; -} - -size_t dap_enc_iaes256_calc_encode_size(const size_t size_in) -{ - return iaes_calc_block128_size(size_in); -} - -size_t dap_enc_iaes256_calc_decode_size(const size_t size_in) -{ - return size_in; -} - -size_t dap_enc_iaes256_cbc_encrypt_fast(struct dap_enc_key * a_key, const void * a_in, - size_t a_in_size, void * buf_out, size_t buf_out_size) -{ - size_t out_size = iaes_calc_block128_size(a_in_size); - - if((a_in_size % IAES_BLOCK_SIZE) == 0) { - IAES_256_CBC_encrypt(a_in, buf_out, DAP_ENC_AES_KEY(a_key)->ivec, out_size, a_key->priv_key_data); - return out_size; - } - - if(buf_out_size < out_size) { - log_it(L_ERROR, "buf_out_size less than expected encrypt out size data"); - return 0; - } - uint8_t* data_in_new; - iaes_block128_padding(a_in, &data_in_new, a_in_size); - - IAES_256_CBC_encrypt(data_in_new, buf_out, DAP_ENC_AES_KEY(a_key)->ivec, - out_size, a_key->priv_key_data); - - free(data_in_new); - - return out_size; -} +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include "dap_enc_key.h" +#include "dap_enc_iaes.h" +#include "sha3/fips202.h" + +//#include "KeccakHash.h" +//#include "SimpleFIPS202.h" + +#include "dap_common.h" + + +#define LOG_TAG "dap_enc_aes" + +typedef struct dap_enc_aes_key { + unsigned char ivec[IAES_BLOCK_SIZE]; +} dap_enc_aes_key_t; + +#define DAP_ENC_AES_KEY(a) ((dap_enc_aes_key_t *)((a)->_inheritor) ) + +void dap_enc_aes_key_delete(struct dap_enc_key *a_key) +{ + free(a_key->_inheritor); + //No need any specific actions +} + +/** + * @brief dap_enc_aes_key_new_generate + * @param a_key + * @param a_size + */ +void dap_enc_aes_key_new(struct dap_enc_key * a_key) +{ + a_key->_inheritor = DAP_NEW_Z(dap_enc_aes_key_t); + a_key->_inheritor_size = sizeof (dap_enc_aes_key_t); + a_key->type = DAP_ENC_KEY_TYPE_IAES; + a_key->enc = dap_enc_iaes256_cbc_encrypt; + a_key->dec = dap_enc_iaes256_cbc_decrypt; + a_key->enc_na = dap_enc_iaes256_cbc_encrypt_fast; + a_key->dec_na = dap_enc_iaes256_cbc_decrypt_fast; + //a_key->delete_callback = dap_enc_aes_key_delete; + + a_key->priv_key_data = (uint8_t *)malloc(IAES_KEYSIZE); + a_key->priv_key_data_size = IAES_KEYSIZE; +} + +void dap_enc_aes_key_generate(struct dap_enc_key * a_key, const void *kex_buf, + size_t kex_size, const void * seed, size_t seed_size, + size_t key_size) +{ + (void)key_size; + a_key->last_used_timestamp = time(NULL); + + uint8_t * id_concat_kex = (uint8_t *) malloc(kex_size + seed_size); + + memcpy(id_concat_kex,seed, seed_size); + memcpy(id_concat_kex + seed_size, kex_buf, kex_size); + //SHAKE256(a_key->priv_key_data, IAES_KEYSIZE, id_concat_kex, (kex_size + seed_size)); + //SHAKE128(DAP_ENC_AES_KEY(a_key)->ivec, IAES_BLOCK_SIZE, seed, seed_size); + shake256(a_key->priv_key_data, IAES_KEYSIZE, id_concat_kex, (kex_size + seed_size)); + shake128(DAP_ENC_AES_KEY(a_key)->ivec, IAES_BLOCK_SIZE, seed, seed_size); + + free(id_concat_kex); +} + + +/** + * @brief ap_enc_aes256_cbc_decrypt + * @param a_key + * @param a_in + * @param a_in_size + * @param a_out + * @return + */ + +size_t dap_enc_iaes256_cbc_decrypt(struct dap_enc_key * a_key, const void * a_in, size_t a_in_size, void ** a_out) +{ + if (a_in_size % 16) { + log_it(L_ERROR, "Bad in data size"); + return 0; + } + + *a_out = (uint8_t *) malloc(a_in_size); + + return IAES_256_CBC_decrypt(a_in, *a_out, DAP_ENC_AES_KEY(a_key)->ivec, a_in_size, a_key->priv_key_data); +} + +size_t dap_enc_iaes256_cbc_decrypt_fast(struct dap_enc_key * a_key, const void * a_in, + size_t a_in_size, void * buf_out, size_t buf_out_size) +{ + if (a_in_size % 16) { + log_it(L_ERROR, "Bad in size"); + return 0; + } else if(buf_out_size < a_in_size) { + log_it(L_ERROR, "buf_out_size < a_in_size"); + return 0; + } + + return IAES_256_CBC_decrypt(a_in, buf_out, DAP_ENC_AES_KEY(a_key)->ivec, + a_in_size, a_key->priv_key_data); +} + +size_t dap_enc_iaes256_cbc_encrypt(struct dap_enc_key * a_key, const void * a_in, size_t a_in_size, void ** a_out) +{ + size_t length_data_new; + uint8_t *data_new; + + length_data_new = iaes_block128_padding(a_in, &data_new, a_in_size); + *a_out = (uint8_t *)malloc(length_data_new); + + IAES_256_CBC_encrypt(data_new, *a_out, DAP_ENC_AES_KEY(a_key)->ivec, length_data_new, a_key->priv_key_data); + + free(data_new); + return length_data_new; +} + +size_t dap_enc_iaes256_calc_encode_size(const size_t size_in) +{ + return iaes_calc_block128_size(size_in); +} + +size_t dap_enc_iaes256_calc_decode_size(const size_t size_in) +{ + return size_in; +} + +size_t dap_enc_iaes256_cbc_encrypt_fast(struct dap_enc_key * a_key, const void * a_in, + size_t a_in_size, void * buf_out, size_t buf_out_size) +{ + size_t out_size = iaes_calc_block128_size(a_in_size); + + if((a_in_size % IAES_BLOCK_SIZE) == 0) { + IAES_256_CBC_encrypt(a_in, buf_out, DAP_ENC_AES_KEY(a_key)->ivec, out_size, a_key->priv_key_data); + return out_size; + } + + if(buf_out_size < out_size) { + log_it(L_ERROR, "buf_out_size less than expected encrypt out size data"); + return 0; + } + uint8_t* data_in_new; + iaes_block128_padding(a_in, &data_in_new, a_in_size); + + IAES_256_CBC_encrypt(data_in_new, buf_out, DAP_ENC_AES_KEY(a_key)->ivec, + out_size, a_key->priv_key_data); + + free(data_in_new); + + return out_size; +} diff --git a/src/dap_enc_key.c b/src/dap_enc_key.c index 38eee1a20e407631b35a9c95cacee64548b57c00..3f9b912971637d07a90282e1386598adb3b1590d 100755 --- a/src/dap_enc_key.c +++ b/src/dap_enc_key.c @@ -1,658 +1,658 @@ -/* - Copyright (c) 2017-2018 (c) Project "DeM Labs Inc" https://github.com/demlabsinc - All rights reserved. - - This file is part of DAP (Deus Applications Prototypes) the open source project - - DAP (Deus Applicaions Prototypes) is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - DAP is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public License - along with any DAP based project. If not, see <http://www.gnu.org/licenses/>. -*/ - - -#include <stdlib.h> -#include <string.h> -#include "dap_common.h" - -#include "dap_enc_iaes.h" -#include "dap_enc_oaes.h" -#include "dap_enc_msrln.h" -#include "dap_enc_defeo.h" -#include "dap_enc_picnic.h" -#include "dap_enc_bliss.h" -#include "dap_enc_tesla.h" -#include "dap_enc_dilithium.h" - - -#include "dap_enc_key.h" - -#undef LOG_TAG -#define LOG_TAG "dap_enc_key" - -struct dap_enc_key_callbacks{ - const char * name; - dap_enc_callback_dataop_t enc; - dap_enc_callback_dataop_t dec; - dap_enc_callback_dataop_na_t enc_na; - dap_enc_callback_dataop_na_t dec_na; - - dap_enc_callback_sign_op_t sign_get; - dap_enc_callback_sign_op_t sign_verify; - - dap_enc_callback_gen_key_public_t gen_key_public; - dap_enc_callback_key_size_t gen_key_public_size; - - dap_enc_callback_calc_out_size enc_out_size; - dap_enc_callback_calc_out_size dec_out_size; - - dap_enc_gen_bob_shared_key gen_bob_shared_key; - dap_enc_gen_alice_shared_key gen_alice_shared_key; - - dap_enc_callback_new new_callback; - dap_enc_callback_data_t new_from_data_public_callback; - dap_enc_callback_new_generate new_generate_callback; - dap_enc_callback_delete delete_callback; -} s_callbacks[]={ - // AES - [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, - .gen_key_public = NULL, - .gen_key_public_size = NULL, - .enc_out_size = dap_enc_iaes256_calc_encode_size, - .dec_out_size = dap_enc_iaes256_calc_decode_size, - .sign_get = NULL, - .sign_verify = NULL - }, - // OAES - [DAP_ENC_KEY_TYPE_OAES]={ - .name = "OAES", - .enc = dap_enc_oaes_encrypt, - .enc_na = dap_enc_oaes_encrypt_fast , - .dec = dap_enc_oaes_decrypt, - .dec_na = dap_enc_oaes_decrypt_fast , - .new_callback = dap_enc_oaes_key_new, - .delete_callback = dap_enc_oaes_key_delete, - .new_generate_callback = dap_enc_oaes_key_generate, - .gen_key_public = NULL, - .gen_key_public_size = NULL, - .enc_out_size = dap_enc_oaes_calc_encode_size, - .dec_out_size = dap_enc_oaes_calc_decode_size, - .sign_get = NULL, - .sign_verify = NULL - }, - [DAP_ENC_KEY_TYPE_MSRLN] = { - .name = "MSRLN", - .enc = NULL, - .dec = NULL, - .new_callback = dap_enc_msrln_key_new, - .delete_callback = dap_enc_msrln_key_delete, - .new_generate_callback = dap_enc_msrln_key_generate, - .gen_bob_shared_key = dap_enc_msrln_gen_bob_shared_key, - .gen_alice_shared_key = dap_enc_msrln_gen_alice_shared_key, - .gen_key_public = NULL, - .gen_key_public_size = NULL, - .new_from_data_public_callback = dap_enc_msrln_key_new_from_data_public, - .enc_out_size = NULL, - .dec_out_size = NULL, - .sign_get = NULL, - .sign_verify = NULL - }, - [DAP_ENC_KEY_TYPE_DEFEO]={ - .name = "DEFEO", - .enc = NULL, - .dec = NULL, - .gen_key_public = NULL, - .gen_key_public_size = NULL, - .gen_bob_shared_key = dap_enc_defeo_gen_bob_shared_key, - .gen_alice_shared_key = dap_enc_defeo_gen_alice_shared_key, - .new_callback = dap_enc_defeo_key_new, - .delete_callback = dap_enc_defeo_key_delete, - .new_generate_callback = dap_enc_defeo_key_new_generate, - .enc_out_size = NULL, - .dec_out_size = NULL, - .sign_get = NULL, - .sign_verify = NULL - }, - [DAP_ENC_KEY_TYPE_SIG_PICNIC]={ - .name = "PICNIC", - .enc = NULL, - .dec = NULL, - .enc_na = dap_enc_sig_picnic_get_sign, // dap_enc_picnic_enc_na - .dec_na = dap_enc_sig_picnic_verify_sign,// dap_enc_picnic_dec_na - .gen_bob_shared_key = NULL, - .gen_alice_shared_key = NULL, - .new_callback = dap_enc_sig_picnic_key_new, - .gen_key_public = NULL, - .gen_key_public_size = dap_enc_picnic_calc_signature_size, - .delete_callback = dap_enc_sig_picnic_key_delete, - .new_generate_callback = dap_enc_sig_picnic_key_new_generate, - .enc_out_size = NULL, - .dec_out_size = NULL, - .sign_get = NULL, - .sign_verify = NULL - }, - [DAP_ENC_KEY_TYPE_SIG_BLISS]={ - .name = "SIG_BLISS", - .enc = NULL, - .dec = NULL, - .enc_na = NULL, - .dec_na = NULL, - .sign_get = dap_enc_sig_bliss_get_sign, - .sign_verify = dap_enc_sig_bliss_verify_sign, - .gen_bob_shared_key = NULL, - .gen_alice_shared_key = NULL, - .new_callback = dap_enc_sig_bliss_key_new, - .delete_callback = dap_enc_sig_bliss_key_delete, - .new_generate_callback = dap_enc_sig_bliss_key_new_generate, - .gen_key_public = dap_enc_sig_bliss_key_pub_output, - .gen_key_public_size = dap_enc_sig_bliss_key_pub_output_size, - - .enc_out_size = NULL, - .dec_out_size = NULL - }, - [DAP_ENC_KEY_TYPE_SIG_TESLA]={ - .name = "SIG_TESLA", - .enc = NULL, - .dec = NULL, - .enc_na = dap_enc_sig_tesla_get_sign, - .dec_na = dap_enc_sig_tesla_verify_sign, - .gen_key_public = NULL, - .gen_key_public_size = NULL, - .gen_bob_shared_key = NULL, - .gen_alice_shared_key = NULL, - .new_callback = dap_enc_sig_tesla_key_new, - .delete_callback = dap_enc_sig_tesla_key_delete, - .new_generate_callback = dap_enc_sig_tesla_key_new_generate, - .enc_out_size = NULL, - .dec_out_size = NULL, - .sign_get = NULL, - .sign_verify = NULL - }, - [DAP_ENC_KEY_TYPE_SIG_DILITHIUM]={ - .name = "SIG_DILITHIUM", - .enc = NULL, - .dec = NULL, - .enc_na = dap_enc_sig_dilithium_get_sign, - .dec_na = dap_enc_sig_dilithium_verify_sign, - .gen_key_public = NULL, - .gen_key_public_size = NULL, - .gen_bob_shared_key = NULL, - .gen_alice_shared_key = NULL, - .new_callback = dap_enc_sig_dilithium_key_new, - .delete_callback = dap_enc_sig_dilithium_key_delete, - .new_generate_callback = dap_enc_sig_dilithium_key_new_generate, - .enc_out_size = NULL, - .dec_out_size = NULL, - .sign_get = NULL, - .sign_verify = NULL - } -}; - -const size_t c_callbacks_size = sizeof(s_callbacks) / sizeof(s_callbacks[0]); - -/** - * @brief dap_enc_key_init - * @return - */ -int dap_enc_key_init() -{ - return 0; -} - -/** - * @brief dap_enc_key_deinit - */ -void dap_enc_key_deinit() -{ - -} - -/** - * @brief dap_enc_key_serealize_sign - * - * @param a_key_type - * @param a_sign - * @param a_sign_len [in/out] - * @return allocates memory with private key - */ -uint8_t* dap_enc_key_serealize_sign(dap_enc_key_type_t a_key_type, uint8_t *a_sign, size_t *a_sign_len) -{ - uint8_t *data = NULL; - switch (a_key_type) { - case DAP_ENC_KEY_TYPE_SIG_BLISS: - data = dap_enc_sig_bliss_write_signature((bliss_signature_t*)a_sign, a_sign_len); - break; - case DAP_ENC_KEY_TYPE_SIG_TESLA: - data = dap_enc_tesla_write_signature((tesla_signature_t*)a_sign, a_sign_len); - break; - case DAP_ENC_KEY_TYPE_SIG_DILITHIUM: - data = dap_enc_dilithium_write_signature((dilithium_signature_t*)a_sign, a_sign_len); - break; - default: - data = DAP_NEW_Z_SIZE(uint8_t, *a_sign_len); - memcpy(data, a_sign, *a_sign_len); - } - return data; -} - -/** - * @brief dap_enc_key_serealize_sign - * - * @param a_key_type - * @param a_sign - * @param a_sign_len [in/out] - * @return allocates memory with private key - */ -uint8_t* dap_enc_key_deserealize_sign(dap_enc_key_type_t a_key_type, uint8_t *a_sign, size_t *a_sign_len) -{ - uint8_t *data = NULL; - switch (a_key_type) { - case DAP_ENC_KEY_TYPE_SIG_BLISS: - data = (uint8_t*)dap_enc_sig_bliss_read_signature(a_sign, *a_sign_len); - *a_sign_len = sizeof(bliss_signature_t); - break; - case DAP_ENC_KEY_TYPE_SIG_TESLA: - data = (uint8_t*)dap_enc_tesla_read_signature(a_sign, *a_sign_len); - *a_sign_len = sizeof(tesla_signature_t); - break; - case DAP_ENC_KEY_TYPE_SIG_DILITHIUM: - data = (uint8_t*)dap_enc_dilithium_read_signature(a_sign, *a_sign_len); - *a_sign_len = sizeof(dilithium_signature_t); - break; - default: - data = DAP_NEW_Z_SIZE(uint8_t, *a_sign_len); - memcpy(data, a_sign, *a_sign_len); - } - return data; -} - - -/** - * @brief dap_enc_key_serealize_priv_key - * - * @param a_key - * @param a_buflen_out - * @return allocates memory with private key - */ -uint8_t* dap_enc_key_serealize_priv_key(dap_enc_key_t *a_key, size_t *a_buflen_out) -{ - uint8_t *data = NULL; - switch (a_key->type) { - case DAP_ENC_KEY_TYPE_SIG_BLISS: - data = dap_enc_sig_bliss_write_private_key(a_key->priv_key_data, a_buflen_out); - break; - case DAP_ENC_KEY_TYPE_SIG_TESLA: - data = dap_enc_tesla_write_private_key(a_key->priv_key_data, a_buflen_out); - break; - case DAP_ENC_KEY_TYPE_SIG_DILITHIUM: - data = dap_enc_dilithium_write_private_key(a_key->priv_key_data, a_buflen_out); - break; - default: - data = DAP_NEW_Z_SIZE(uint8_t, a_key->priv_key_data_size); - memcpy(data, a_key->priv_key_data, a_key->priv_key_data_size); - if(a_buflen_out) - *a_buflen_out = a_key->priv_key_data_size; - } - return data; -} - -/** - * @brief dap_enc_key_serealize_pub_key - * - * @param a_key - * @param a_buflen_out - * @return allocates memory with private key - */ -uint8_t* dap_enc_key_serealize_pub_key(dap_enc_key_t *a_key, size_t *a_buflen_out) -{ - uint8_t *data = NULL; - switch (a_key->type) { - case DAP_ENC_KEY_TYPE_SIG_BLISS: - data = dap_enc_sig_bliss_write_public_key(a_key->pub_key_data, a_buflen_out); - break; - case DAP_ENC_KEY_TYPE_SIG_TESLA: - data = dap_enc_tesla_write_public_key(a_key->pub_key_data, a_buflen_out); - break; - case DAP_ENC_KEY_TYPE_SIG_DILITHIUM: - data = dap_enc_dilithium_write_public_key(a_key->pub_key_data, a_buflen_out); - break; - default: - data = DAP_NEW_Z_SIZE(uint8_t, a_key->pub_key_data_size); - memcpy(data, a_key->pub_key_data, a_key->pub_key_data_size); - if(a_buflen_out) - *a_buflen_out = a_key->pub_key_data_size; - } - return data; -} -/** - * @brief dap_enc_key_deserealize_priv_key - * - * @param a_key - * @param a_buf - * @param a_buflen_out - * @return 0 Ok, -1 error - */ -int dap_enc_key_deserealize_priv_key(dap_enc_key_t *a_key, uint8_t *a_buf, size_t a_buflen) -{ - if(!a_key || !a_buf) - return -1; - switch (a_key->type) { - case DAP_ENC_KEY_TYPE_SIG_BLISS: - if((a_key->priv_key_data)) { - bliss_b_private_key_delete((bliss_private_key_t *) a_key->priv_key_data); - DAP_DELETE(a_key->pub_key_data); - } - a_key->priv_key_data = (uint8_t*) dap_enc_sig_bliss_read_private_key(a_buf, a_buflen); - if(!a_key->priv_key_data) - { - a_key->priv_key_data_size = 0; - return -1; - } - a_key->priv_key_data_size = sizeof(bliss_private_key_t); - break; - case DAP_ENC_KEY_TYPE_SIG_TESLA: - tesla_private_key_delete((tesla_private_key_t *) a_key->priv_key_data); - a_key->priv_key_data = (uint8_t*) dap_enc_tesla_read_private_key(a_buf, a_buflen); - if(!a_key->priv_key_data) - { - a_key->priv_key_data_size = 0; - return -1; - } - a_key->priv_key_data_size = sizeof(tesla_private_key_t); - break; - case DAP_ENC_KEY_TYPE_SIG_PICNIC: - DAP_DELETE(a_key->priv_key_data); - a_key->priv_key_data_size = a_buflen; - a_key->priv_key_data = DAP_NEW_Z_SIZE(uint8_t, a_key->priv_key_data_size); - memcpy(a_key->priv_key_data, a_buf, a_key->priv_key_data_size); - dap_enc_sig_picnic_update(a_key); - break; - case DAP_ENC_KEY_TYPE_SIG_DILITHIUM: - dilithium_private_key_delete((dilithium_private_key_t *) a_key->priv_key_data); - a_key->priv_key_data = (uint8_t*) dap_enc_dilithium_read_private_key(a_buf, a_buflen); - if(!a_key->priv_key_data) - { - a_key->priv_key_data_size = 0; - return -1; - } - a_key->priv_key_data_size = sizeof(dilithium_private_key_t); - break; - default: - DAP_DELETE(a_key->priv_key_data); - a_key->priv_key_data_size = a_buflen; - a_key->priv_key_data = DAP_NEW_Z_SIZE(uint8_t, a_key->priv_key_data_size); - memcpy(a_key->priv_key_data, a_buf, a_key->priv_key_data_size); - } - return 0; -} - -/** - * @brief dap_enc_key_deserealize_pub_key - * - * @param a_key - * @param a_buf - * @param a_buflen_out - * @return 0 Ok, -1 error - */ -int dap_enc_key_deserealize_pub_key(dap_enc_key_t *a_key, const uint8_t *a_buf, size_t a_buflen) -{ - if(!a_key || !a_buf) - return -1; - switch (a_key->type) { - case DAP_ENC_KEY_TYPE_SIG_BLISS: - if((a_key->pub_key_data)) { - bliss_b_public_key_delete((bliss_public_key_t *) a_key->pub_key_data); - DAP_DELETE(a_key->pub_key_data); - } - a_key->pub_key_data = (uint8_t*) dap_enc_sig_bliss_read_public_key(a_buf, a_buflen); - if(!a_key->pub_key_data) - { - a_key->pub_key_data_size = 0; - return -1; - } - a_key->pub_key_data_size = sizeof(bliss_public_key_t); - break; - case DAP_ENC_KEY_TYPE_SIG_TESLA: - tesla_public_key_delete((tesla_public_key_t *) a_key->pub_key_data); - a_key->pub_key_data = (uint8_t*) dap_enc_tesla_read_public_key(a_buf, a_buflen); - if(!a_key->pub_key_data) - { - a_key->pub_key_data_size = 0; - return -1; - } - a_key->pub_key_data_size = sizeof(tesla_public_key_t); - break; - case DAP_ENC_KEY_TYPE_SIG_PICNIC: - DAP_DELETE(a_key->pub_key_data); - a_key->pub_key_data_size = a_buflen; - a_key->pub_key_data = DAP_NEW_Z_SIZE(uint8_t, a_key->pub_key_data_size); - memcpy(a_key->pub_key_data, a_buf, a_key->pub_key_data_size); - dap_enc_sig_picnic_update(a_key); - break; - case DAP_ENC_KEY_TYPE_SIG_DILITHIUM: - if ( a_key->pub_key_data ) - dilithium_public_key_delete((dilithium_public_key_t *) a_key->pub_key_data); - a_key->pub_key_data = (uint8_t*) dap_enc_dilithium_read_public_key(a_buf, a_buflen); - if(!a_key->pub_key_data) - { - a_key->pub_key_data_size = 0; - return -1; - } - a_key->pub_key_data_size = sizeof(dilithium_public_key_t); - break; - default: - DAP_DELETE(a_key->pub_key_data); - a_key->pub_key_data_size = a_buflen; - a_key->pub_key_data = DAP_NEW_Z_SIZE(uint8_t, a_key->pub_key_data_size); - memcpy(a_key->pub_key_data, a_buf, a_key->pub_key_data_size); - } - return 0; -} - -/** - * @brief dap_enc_key_serealize - * @param key - * @return allocates dap_enc_key_serealize_t* dont remember use free() - */ -dap_enc_key_serealize_t* dap_enc_key_serealize(dap_enc_key_t * key) -{ - dap_enc_key_serealize_t *result = DAP_NEW_Z(dap_enc_key_serealize_t); - result->priv_key_data_size = key->priv_key_data_size; - result->pub_key_data_size = key->pub_key_data_size; - result->last_used_timestamp = key->last_used_timestamp; - result->inheritor_size = key->_inheritor_size; - result->type = key->type; - memcpy(result->priv_key_data, key->priv_key_data, key->priv_key_data_size); - memcpy(result->pub_key_data, key->pub_key_data, key->pub_key_data_size); - memcpy(result->inheritor, key->_inheritor, key->_inheritor_size); - return result; -} - -/** - * @brief dap_enc_key_deserealize - * @param buf - * @param buf_size - * @return allocates dap_enc_key_t*. Use dap_enc_key_delete for free memory - */ -dap_enc_key_t* dap_enc_key_deserealize(const void *buf, size_t buf_size) -{ - if(buf_size != sizeof (dap_enc_key_serealize_t)) { - log_it(L_ERROR, "Key can't be deserealize. buf_size(%d) != sizeof (dap_enc_key_serealize_t)(%d)",buf_size,sizeof (dap_enc_key_serealize_t)); - return NULL; - } - const dap_enc_key_serealize_t *in_key = (const dap_enc_key_serealize_t *)buf; - dap_enc_key_t *result = dap_enc_key_new(in_key->type); - result->last_used_timestamp = in_key->last_used_timestamp; - result->priv_key_data_size = in_key->priv_key_data_size; - result->pub_key_data_size = in_key->pub_key_data_size; - result->_inheritor_size = in_key->inheritor_size; - memcpy(result->priv_key_data, in_key->priv_key_data, result->priv_key_data_size); - memcpy(result->pub_key_data, in_key->pub_key_data, result->pub_key_data_size); - - if(in_key->inheritor_size) - memcpy(result->_inheritor, in_key->inheritor, in_key->inheritor_size); - else - result->_inheritor = NULL; - - return result; -} - -/** - * @brief dap_enc_key_new - * @param a_key_type - * @return - */ -dap_enc_key_t *dap_enc_key_new(dap_enc_key_type_t a_key_type) -{ - dap_enc_key_t * ret = NULL; - if(a_key_type < c_callbacks_size ){ - ret = DAP_NEW_Z(dap_enc_key_t); - if(s_callbacks[a_key_type].new_callback){ - s_callbacks[a_key_type].new_callback(ret); - } - } - ret->type = a_key_type; - return ret; -} - -/** - * @brief dap_enc_key_new_generate - * @param a_key_type - * @param kex_buf - * @param kex_size - * @param seed - * @param seed_size - * @param key_size - can be NULL ( generate size by default ) - * @return - */ -dap_enc_key_t *dap_enc_key_new_generate(dap_enc_key_type_t a_key_type, const void *kex_buf, - size_t kex_size, const void* seed, - size_t seed_size, size_t key_size) -{ - dap_enc_key_t * ret = NULL; - if(a_key_type< c_callbacks_size ) { - ret = dap_enc_key_new(a_key_type); - if(s_callbacks[a_key_type].new_generate_callback) { - s_callbacks[a_key_type].new_generate_callback( ret, kex_buf, kex_size, seed, seed_size, key_size); - } - } - return ret; -} - -/** - * @brief dap_enc_key_update - * @param a_key_type - * @return - */ -void dap_enc_key_update(dap_enc_key_t *a_key) -{ - if(a_key) - switch (a_key->type) { - case DAP_ENC_KEY_TYPE_SIG_TESLA: - break; - case DAP_ENC_KEY_TYPE_SIG_PICNIC: - dap_enc_sig_picnic_update(a_key); - break; - case DAP_ENC_KEY_TYPE_SIG_BLISS: - break; - case DAP_ENC_KEY_TYPE_SIG_DILITHIUM: - break; - default: - break; - } -} - -size_t dap_enc_gen_key_public_size (dap_enc_key_t *a_key) -{ - if(s_callbacks[a_key->type].gen_key_public_size) { - return s_callbacks[a_key->type].gen_key_public_size(a_key); - } else { - log_it(L_ERROR, "No callback for key public size calculate"); - return 0; - } -} - -int dap_enc_gen_key_public (dap_enc_key_t *a_key, void * a_output) -{ - if(s_callbacks[a_key->type].gen_key_public) { - return s_callbacks[a_key->type].gen_key_public(a_key,a_output); - } else { - log_it(L_ERROR, "No callback for key public generate action"); - } - return -1; -} - -/** - * @brief dap_enc_key_delete - * @param a_key - */ -void dap_enc_key_signature_delete(dap_enc_key_type_t a_key_type, uint8_t *a_sig_buf) -{ - switch (a_key_type) { - case DAP_ENC_KEY_TYPE_SIG_BLISS: - bliss_signature_delete((bliss_signature_t*)a_sig_buf); - break; - case DAP_ENC_KEY_TYPE_SIG_TESLA: - tesla_signature_delete((tesla_signature_t*)a_sig_buf); - break; - case DAP_ENC_KEY_TYPE_SIG_DILITHIUM: - dilithium_signature_delete((dilithium_signature_t*)a_sig_buf); - break; - default: - break; - } - DAP_DELETE(a_sig_buf); -} - -/** - * @brief dap_enc_key_delete - * @param a_key - */ -void dap_enc_key_delete(dap_enc_key_t * a_key) -{ - if(s_callbacks[a_key->type].delete_callback) { - s_callbacks[a_key->type].delete_callback(a_key); - } else { - log_it(L_ERROR, "delete callback is null. Can be leak memory!"); - } - /* a_key->_inheritor must be cleaned in delete_callback func */ - if ( a_key->pub_key_data) - DAP_DELETE(a_key->pub_key_data); - if (a_key->priv_key_data ) - DAP_DELETE(a_key->priv_key_data); - DAP_DELETE(a_key); -} - -size_t dap_enc_key_get_enc_size(dap_enc_key_t * a_key, const size_t buf_in_size) -{ - if(s_callbacks[a_key->type].enc_out_size) { - return s_callbacks[a_key->type].enc_out_size(buf_in_size); - } - log_it(L_ERROR, "enc_out_size not realize for current key type"); - return 0; -} - -size_t dap_enc_key_get_dec_size(dap_enc_key_t * a_key, const size_t buf_in_size) -{ - if(s_callbacks[a_key->type].dec_out_size) { - return s_callbacks[a_key->type].dec_out_size(buf_in_size); - } - log_it(L_ERROR, "dec_out_size not realize for current key type"); - return 0; -} +/* + Copyright (c) 2017-2018 (c) Project "DeM Labs Inc" https://github.com/demlabsinc + All rights reserved. + + This file is part of DAP (Deus Applications Prototypes) the open source project + + DAP (Deus Applicaions Prototypes) is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + DAP is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with any DAP based project. If not, see <http://www.gnu.org/licenses/>. +*/ + + +#include <stdlib.h> +#include <string.h> +#include "dap_common.h" + +#include "dap_enc_iaes.h" +#include "dap_enc_oaes.h" +#include "dap_enc_msrln.h" +#include "dap_enc_defeo.h" +#include "dap_enc_picnic.h" +#include "dap_enc_bliss.h" +#include "dap_enc_tesla.h" +#include "dap_enc_dilithium.h" + + +#include "dap_enc_key.h" + +#undef LOG_TAG +#define LOG_TAG "dap_enc_key" + +struct dap_enc_key_callbacks{ + const char * name; + dap_enc_callback_dataop_t enc; + dap_enc_callback_dataop_t dec; + dap_enc_callback_dataop_na_t enc_na; + dap_enc_callback_dataop_na_t dec_na; + + dap_enc_callback_sign_op_t sign_get; + dap_enc_callback_sign_op_t sign_verify; + + dap_enc_callback_gen_key_public_t gen_key_public; + dap_enc_callback_key_size_t gen_key_public_size; + + dap_enc_callback_calc_out_size enc_out_size; + dap_enc_callback_calc_out_size dec_out_size; + + dap_enc_gen_bob_shared_key gen_bob_shared_key; + dap_enc_gen_alice_shared_key gen_alice_shared_key; + + dap_enc_callback_new new_callback; + dap_enc_callback_data_t new_from_data_public_callback; + dap_enc_callback_new_generate new_generate_callback; + dap_enc_callback_delete delete_callback; +} s_callbacks[]={ + // AES + [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, + .gen_key_public = NULL, + .gen_key_public_size = NULL, + .enc_out_size = dap_enc_iaes256_calc_encode_size, + .dec_out_size = dap_enc_iaes256_calc_decode_size, + .sign_get = NULL, + .sign_verify = NULL + }, + // OAES + [DAP_ENC_KEY_TYPE_OAES]={ + .name = "OAES", + .enc = dap_enc_oaes_encrypt, + .enc_na = dap_enc_oaes_encrypt_fast , + .dec = dap_enc_oaes_decrypt, + .dec_na = dap_enc_oaes_decrypt_fast , + .new_callback = dap_enc_oaes_key_new, + .delete_callback = dap_enc_oaes_key_delete, + .new_generate_callback = dap_enc_oaes_key_generate, + .gen_key_public = NULL, + .gen_key_public_size = NULL, + .enc_out_size = dap_enc_oaes_calc_encode_size, + .dec_out_size = dap_enc_oaes_calc_decode_size, + .sign_get = NULL, + .sign_verify = NULL + }, + [DAP_ENC_KEY_TYPE_MSRLN] = { + .name = "MSRLN", + .enc = NULL, + .dec = NULL, + .new_callback = dap_enc_msrln_key_new, + .delete_callback = dap_enc_msrln_key_delete, + .new_generate_callback = dap_enc_msrln_key_generate, + .gen_bob_shared_key = dap_enc_msrln_gen_bob_shared_key, + .gen_alice_shared_key = dap_enc_msrln_gen_alice_shared_key, + .gen_key_public = NULL, + .gen_key_public_size = NULL, + .new_from_data_public_callback = dap_enc_msrln_key_new_from_data_public, + .enc_out_size = NULL, + .dec_out_size = NULL, + .sign_get = NULL, + .sign_verify = NULL + }, + [DAP_ENC_KEY_TYPE_DEFEO]={ + .name = "DEFEO", + .enc = NULL, + .dec = NULL, + .gen_key_public = NULL, + .gen_key_public_size = NULL, + .gen_bob_shared_key = dap_enc_defeo_gen_bob_shared_key, + .gen_alice_shared_key = dap_enc_defeo_gen_alice_shared_key, + .new_callback = dap_enc_defeo_key_new, + .delete_callback = dap_enc_defeo_key_delete, + .new_generate_callback = dap_enc_defeo_key_new_generate, + .enc_out_size = NULL, + .dec_out_size = NULL, + .sign_get = NULL, + .sign_verify = NULL + }, + [DAP_ENC_KEY_TYPE_SIG_PICNIC]={ + .name = "PICNIC", + .enc = NULL, + .dec = NULL, + .enc_na = dap_enc_sig_picnic_get_sign, // dap_enc_picnic_enc_na + .dec_na = dap_enc_sig_picnic_verify_sign,// dap_enc_picnic_dec_na + .gen_bob_shared_key = NULL, + .gen_alice_shared_key = NULL, + .new_callback = dap_enc_sig_picnic_key_new, + .gen_key_public = NULL, + .gen_key_public_size = dap_enc_picnic_calc_signature_size, + .delete_callback = dap_enc_sig_picnic_key_delete, + .new_generate_callback = dap_enc_sig_picnic_key_new_generate, + .enc_out_size = NULL, + .dec_out_size = NULL, + .sign_get = NULL, + .sign_verify = NULL + }, + [DAP_ENC_KEY_TYPE_SIG_BLISS]={ + .name = "SIG_BLISS", + .enc = NULL, + .dec = NULL, + .enc_na = NULL, + .dec_na = NULL, + .sign_get = dap_enc_sig_bliss_get_sign, + .sign_verify = dap_enc_sig_bliss_verify_sign, + .gen_bob_shared_key = NULL, + .gen_alice_shared_key = NULL, + .new_callback = dap_enc_sig_bliss_key_new, + .delete_callback = dap_enc_sig_bliss_key_delete, + .new_generate_callback = dap_enc_sig_bliss_key_new_generate, + .gen_key_public = dap_enc_sig_bliss_key_pub_output, + .gen_key_public_size = dap_enc_sig_bliss_key_pub_output_size, + + .enc_out_size = NULL, + .dec_out_size = NULL + }, + [DAP_ENC_KEY_TYPE_SIG_TESLA]={ + .name = "SIG_TESLA", + .enc = NULL, + .dec = NULL, + .enc_na = dap_enc_sig_tesla_get_sign, + .dec_na = dap_enc_sig_tesla_verify_sign, + .gen_key_public = NULL, + .gen_key_public_size = NULL, + .gen_bob_shared_key = NULL, + .gen_alice_shared_key = NULL, + .new_callback = dap_enc_sig_tesla_key_new, + .delete_callback = dap_enc_sig_tesla_key_delete, + .new_generate_callback = dap_enc_sig_tesla_key_new_generate, + .enc_out_size = NULL, + .dec_out_size = NULL, + .sign_get = NULL, + .sign_verify = NULL + }, + [DAP_ENC_KEY_TYPE_SIG_DILITHIUM]={ + .name = "SIG_DILITHIUM", + .enc = NULL, + .dec = NULL, + .enc_na = dap_enc_sig_dilithium_get_sign, + .dec_na = dap_enc_sig_dilithium_verify_sign, + .gen_key_public = NULL, + .gen_key_public_size = NULL, + .gen_bob_shared_key = NULL, + .gen_alice_shared_key = NULL, + .new_callback = dap_enc_sig_dilithium_key_new, + .delete_callback = dap_enc_sig_dilithium_key_delete, + .new_generate_callback = dap_enc_sig_dilithium_key_new_generate, + .enc_out_size = NULL, + .dec_out_size = NULL, + .sign_get = NULL, + .sign_verify = NULL + } +}; + +const size_t c_callbacks_size = sizeof(s_callbacks) / sizeof(s_callbacks[0]); + +/** + * @brief dap_enc_key_init + * @return + */ +int dap_enc_key_init() +{ + return 0; +} + +/** + * @brief dap_enc_key_deinit + */ +void dap_enc_key_deinit() +{ + +} + +/** + * @brief dap_enc_key_serealize_sign + * + * @param a_key_type + * @param a_sign + * @param a_sign_len [in/out] + * @return allocates memory with private key + */ +uint8_t* dap_enc_key_serealize_sign(dap_enc_key_type_t a_key_type, uint8_t *a_sign, size_t *a_sign_len) +{ + uint8_t *data = NULL; + switch (a_key_type) { + case DAP_ENC_KEY_TYPE_SIG_BLISS: + data = dap_enc_sig_bliss_write_signature((bliss_signature_t*)a_sign, a_sign_len); + break; + case DAP_ENC_KEY_TYPE_SIG_TESLA: + data = dap_enc_tesla_write_signature((tesla_signature_t*)a_sign, a_sign_len); + break; + case DAP_ENC_KEY_TYPE_SIG_DILITHIUM: + data = dap_enc_dilithium_write_signature((dilithium_signature_t*)a_sign, a_sign_len); + break; + default: + data = DAP_NEW_Z_SIZE(uint8_t, *a_sign_len); + memcpy(data, a_sign, *a_sign_len); + } + return data; +} + +/** + * @brief dap_enc_key_serealize_sign + * + * @param a_key_type + * @param a_sign + * @param a_sign_len [in/out] + * @return allocates memory with private key + */ +uint8_t* dap_enc_key_deserealize_sign(dap_enc_key_type_t a_key_type, uint8_t *a_sign, size_t *a_sign_len) +{ + uint8_t *data = NULL; + switch (a_key_type) { + case DAP_ENC_KEY_TYPE_SIG_BLISS: + data = (uint8_t*)dap_enc_sig_bliss_read_signature(a_sign, *a_sign_len); + *a_sign_len = sizeof(bliss_signature_t); + break; + case DAP_ENC_KEY_TYPE_SIG_TESLA: + data = (uint8_t*)dap_enc_tesla_read_signature(a_sign, *a_sign_len); + *a_sign_len = sizeof(tesla_signature_t); + break; + case DAP_ENC_KEY_TYPE_SIG_DILITHIUM: + data = (uint8_t*)dap_enc_dilithium_read_signature(a_sign, *a_sign_len); + *a_sign_len = sizeof(dilithium_signature_t); + break; + default: + data = DAP_NEW_Z_SIZE(uint8_t, *a_sign_len); + memcpy(data, a_sign, *a_sign_len); + } + return data; +} + + +/** + * @brief dap_enc_key_serealize_priv_key + * + * @param a_key + * @param a_buflen_out + * @return allocates memory with private key + */ +uint8_t* dap_enc_key_serealize_priv_key(dap_enc_key_t *a_key, size_t *a_buflen_out) +{ + uint8_t *data = NULL; + switch (a_key->type) { + case DAP_ENC_KEY_TYPE_SIG_BLISS: + data = dap_enc_sig_bliss_write_private_key(a_key->priv_key_data, a_buflen_out); + break; + case DAP_ENC_KEY_TYPE_SIG_TESLA: + data = dap_enc_tesla_write_private_key(a_key->priv_key_data, a_buflen_out); + break; + case DAP_ENC_KEY_TYPE_SIG_DILITHIUM: + data = dap_enc_dilithium_write_private_key(a_key->priv_key_data, a_buflen_out); + break; + default: + data = DAP_NEW_Z_SIZE(uint8_t, a_key->priv_key_data_size); + memcpy(data, a_key->priv_key_data, a_key->priv_key_data_size); + if(a_buflen_out) + *a_buflen_out = a_key->priv_key_data_size; + } + return data; +} + +/** + * @brief dap_enc_key_serealize_pub_key + * + * @param a_key + * @param a_buflen_out + * @return allocates memory with private key + */ +uint8_t* dap_enc_key_serealize_pub_key(dap_enc_key_t *a_key, size_t *a_buflen_out) +{ + uint8_t *data = NULL; + switch (a_key->type) { + case DAP_ENC_KEY_TYPE_SIG_BLISS: + data = dap_enc_sig_bliss_write_public_key(a_key->pub_key_data, a_buflen_out); + break; + case DAP_ENC_KEY_TYPE_SIG_TESLA: + data = dap_enc_tesla_write_public_key(a_key->pub_key_data, a_buflen_out); + break; + case DAP_ENC_KEY_TYPE_SIG_DILITHIUM: + data = dap_enc_dilithium_write_public_key(a_key->pub_key_data, a_buflen_out); + break; + default: + data = DAP_NEW_Z_SIZE(uint8_t, a_key->pub_key_data_size); + memcpy(data, a_key->pub_key_data, a_key->pub_key_data_size); + if(a_buflen_out) + *a_buflen_out = a_key->pub_key_data_size; + } + return data; +} +/** + * @brief dap_enc_key_deserealize_priv_key + * + * @param a_key + * @param a_buf + * @param a_buflen_out + * @return 0 Ok, -1 error + */ +int dap_enc_key_deserealize_priv_key(dap_enc_key_t *a_key, const byte_t *a_buf, size_t a_buflen) +{ + if(!a_key || !a_buf) + return -1; + switch (a_key->type) { + case DAP_ENC_KEY_TYPE_SIG_BLISS: + if((a_key->priv_key_data)) { + bliss_b_private_key_delete((bliss_private_key_t *) a_key->priv_key_data); + DAP_DELETE(a_key->pub_key_data); + } + a_key->priv_key_data = (uint8_t*) dap_enc_sig_bliss_read_private_key(a_buf, a_buflen); + if(!a_key->priv_key_data) + { + a_key->priv_key_data_size = 0; + return -1; + } + a_key->priv_key_data_size = sizeof(bliss_private_key_t); + break; + case DAP_ENC_KEY_TYPE_SIG_TESLA: + tesla_private_key_delete((tesla_private_key_t *) a_key->priv_key_data); + a_key->priv_key_data = (uint8_t*) dap_enc_tesla_read_private_key(a_buf, a_buflen); + if(!a_key->priv_key_data) + { + a_key->priv_key_data_size = 0; + return -1; + } + a_key->priv_key_data_size = sizeof(tesla_private_key_t); + break; + case DAP_ENC_KEY_TYPE_SIG_PICNIC: + DAP_DELETE(a_key->priv_key_data); + a_key->priv_key_data_size = a_buflen; + a_key->priv_key_data = DAP_NEW_Z_SIZE(uint8_t, a_key->priv_key_data_size); + memcpy(a_key->priv_key_data, a_buf, a_key->priv_key_data_size); + dap_enc_sig_picnic_update(a_key); + break; + case DAP_ENC_KEY_TYPE_SIG_DILITHIUM: + dilithium_private_key_delete((dilithium_private_key_t *) a_key->priv_key_data); + a_key->priv_key_data = (uint8_t*) dap_enc_dilithium_read_private_key(a_buf, a_buflen); + if(!a_key->priv_key_data) + { + a_key->priv_key_data_size = 0; + return -1; + } + a_key->priv_key_data_size = sizeof(dilithium_private_key_t); + break; + default: + DAP_DELETE(a_key->priv_key_data); + a_key->priv_key_data_size = a_buflen; + a_key->priv_key_data = DAP_NEW_Z_SIZE(uint8_t, a_key->priv_key_data_size); + memcpy(a_key->priv_key_data, a_buf, a_key->priv_key_data_size); + } + return 0; +} + +/** + * @brief dap_enc_key_deserealize_pub_key + * + * @param a_key + * @param a_buf + * @param a_buflen_out + * @return 0 Ok, -1 error + */ +int dap_enc_key_deserealize_pub_key(dap_enc_key_t *a_key, const byte_t *a_buf, size_t a_buflen) +{ + if(!a_key || !a_buf) + return -1; + switch (a_key->type) { + case DAP_ENC_KEY_TYPE_SIG_BLISS: + if((a_key->pub_key_data)) { + bliss_b_public_key_delete((bliss_public_key_t *) a_key->pub_key_data); + DAP_DELETE(a_key->pub_key_data); + } + a_key->pub_key_data = (uint8_t*) dap_enc_sig_bliss_read_public_key(a_buf, a_buflen); + if(!a_key->pub_key_data) + { + a_key->pub_key_data_size = 0; + return -1; + } + a_key->pub_key_data_size = sizeof(bliss_public_key_t); + break; + case DAP_ENC_KEY_TYPE_SIG_TESLA: + tesla_public_key_delete((tesla_public_key_t *) a_key->pub_key_data); + a_key->pub_key_data = (uint8_t*) dap_enc_tesla_read_public_key(a_buf, a_buflen); + if(!a_key->pub_key_data) + { + a_key->pub_key_data_size = 0; + return -1; + } + a_key->pub_key_data_size = sizeof(tesla_public_key_t); + break; + case DAP_ENC_KEY_TYPE_SIG_PICNIC: + DAP_DELETE(a_key->pub_key_data); + a_key->pub_key_data_size = a_buflen; + a_key->pub_key_data = DAP_NEW_Z_SIZE(uint8_t, a_key->pub_key_data_size); + memcpy(a_key->pub_key_data, a_buf, a_key->pub_key_data_size); + dap_enc_sig_picnic_update(a_key); + break; + case DAP_ENC_KEY_TYPE_SIG_DILITHIUM: + if ( a_key->pub_key_data ) + dilithium_public_key_delete((dilithium_public_key_t *) a_key->pub_key_data); + a_key->pub_key_data = (uint8_t*) dap_enc_dilithium_read_public_key(a_buf, a_buflen); + if(!a_key->pub_key_data) + { + a_key->pub_key_data_size = 0; + return -1; + } + a_key->pub_key_data_size = sizeof(dilithium_public_key_t); + break; + default: + DAP_DELETE(a_key->pub_key_data); + a_key->pub_key_data_size = a_buflen; + a_key->pub_key_data = DAP_NEW_Z_SIZE(uint8_t, a_key->pub_key_data_size); + memcpy(a_key->pub_key_data, a_buf, a_key->pub_key_data_size); + } + return 0; +} + +/** + * @brief dap_enc_key_serealize + * @param key + * @return allocates dap_enc_key_serealize_t* dont remember use free() + */ +dap_enc_key_serealize_t* dap_enc_key_serealize(dap_enc_key_t * key) +{ + dap_enc_key_serealize_t *result = DAP_NEW_Z(dap_enc_key_serealize_t); + result->priv_key_data_size = key->priv_key_data_size; + result->pub_key_data_size = key->pub_key_data_size; + result->last_used_timestamp = key->last_used_timestamp; + result->inheritor_size = key->_inheritor_size; + result->type = key->type; + memcpy(result->priv_key_data, key->priv_key_data, key->priv_key_data_size); + memcpy(result->pub_key_data, key->pub_key_data, key->pub_key_data_size); + memcpy(result->inheritor, key->_inheritor, key->_inheritor_size); + return result; +} + +/** + * @brief dap_enc_key_deserealize + * @param buf + * @param buf_size + * @return allocates dap_enc_key_t*. Use dap_enc_key_delete for free memory + */ +dap_enc_key_t* dap_enc_key_deserealize(const void *buf, size_t buf_size) +{ + if(buf_size != sizeof (dap_enc_key_serealize_t)) { + log_it(L_ERROR, "Key can't be deserealize. buf_size(%d) != sizeof (dap_enc_key_serealize_t)(%d)",buf_size,sizeof (dap_enc_key_serealize_t)); + return NULL; + } + const dap_enc_key_serealize_t *in_key = (const dap_enc_key_serealize_t *)buf; + dap_enc_key_t *result = dap_enc_key_new(in_key->type); + result->last_used_timestamp = in_key->last_used_timestamp; + result->priv_key_data_size = in_key->priv_key_data_size; + result->pub_key_data_size = in_key->pub_key_data_size; + result->_inheritor_size = in_key->inheritor_size; + memcpy(result->priv_key_data, in_key->priv_key_data, result->priv_key_data_size); + memcpy(result->pub_key_data, in_key->pub_key_data, result->pub_key_data_size); + + if(in_key->inheritor_size) + memcpy(result->_inheritor, in_key->inheritor, in_key->inheritor_size); + else + result->_inheritor = NULL; + + return result; +} + +/** + * @brief dap_enc_key_new + * @param a_key_type + * @return + */ +dap_enc_key_t *dap_enc_key_new(dap_enc_key_type_t a_key_type) +{ + dap_enc_key_t * ret = NULL; + if(a_key_type < c_callbacks_size ){ + ret = DAP_NEW_Z(dap_enc_key_t); + if(s_callbacks[a_key_type].new_callback){ + s_callbacks[a_key_type].new_callback(ret); + } + } + ret->type = a_key_type; + return ret; +} + +/** + * @brief dap_enc_key_new_generate + * @param a_key_type + * @param kex_buf + * @param kex_size + * @param seed + * @param seed_size + * @param key_size - can be NULL ( generate size by default ) + * @return + */ +dap_enc_key_t *dap_enc_key_new_generate(dap_enc_key_type_t a_key_type, const void *kex_buf, + size_t kex_size, const void* seed, + size_t seed_size, size_t key_size) +{ + dap_enc_key_t * ret = NULL; + if(a_key_type< c_callbacks_size ) { + ret = dap_enc_key_new(a_key_type); + if(s_callbacks[a_key_type].new_generate_callback) { + s_callbacks[a_key_type].new_generate_callback( ret, kex_buf, kex_size, seed, seed_size, key_size); + } + } + return ret; +} + +/** + * @brief dap_enc_key_update + * @param a_key_type + * @return + */ +void dap_enc_key_update(dap_enc_key_t *a_key) +{ + if(a_key) + switch (a_key->type) { + case DAP_ENC_KEY_TYPE_SIG_TESLA: + break; + case DAP_ENC_KEY_TYPE_SIG_PICNIC: + dap_enc_sig_picnic_update(a_key); + break; + case DAP_ENC_KEY_TYPE_SIG_BLISS: + break; + case DAP_ENC_KEY_TYPE_SIG_DILITHIUM: + break; + default: + break; + } +} + +size_t dap_enc_gen_key_public_size (dap_enc_key_t *a_key) +{ + if(s_callbacks[a_key->type].gen_key_public_size) { + return s_callbacks[a_key->type].gen_key_public_size(a_key); + } else { + log_it(L_ERROR, "No callback for key public size calculate"); + return 0; + } +} + +int dap_enc_gen_key_public (dap_enc_key_t *a_key, void * a_output) +{ + if(s_callbacks[a_key->type].gen_key_public) { + return s_callbacks[a_key->type].gen_key_public(a_key,a_output); + } else { + log_it(L_ERROR, "No callback for key public generate action"); + } + return -1; +} + +/** + * @brief dap_enc_key_delete + * @param a_key + */ +void dap_enc_key_signature_delete(dap_enc_key_type_t a_key_type, uint8_t *a_sig_buf) +{ + switch (a_key_type) { + case DAP_ENC_KEY_TYPE_SIG_BLISS: + bliss_signature_delete((bliss_signature_t*)a_sig_buf); + break; + case DAP_ENC_KEY_TYPE_SIG_TESLA: + tesla_signature_delete((tesla_signature_t*)a_sig_buf); + break; + case DAP_ENC_KEY_TYPE_SIG_DILITHIUM: + dilithium_signature_delete((dilithium_signature_t*)a_sig_buf); + break; + default: + break; + } + DAP_DELETE(a_sig_buf); +} + +/** + * @brief dap_enc_key_delete + * @param a_key + */ +void dap_enc_key_delete(dap_enc_key_t * a_key) +{ + if(s_callbacks[a_key->type].delete_callback) { + s_callbacks[a_key->type].delete_callback(a_key); + } else { + log_it(L_ERROR, "delete callback is null. Can be leak memory!"); + } + /* a_key->_inheritor must be cleaned in delete_callback func */ + if ( a_key->pub_key_data) + DAP_DELETE(a_key->pub_key_data); + if (a_key->priv_key_data ) + DAP_DELETE(a_key->priv_key_data); + DAP_DELETE(a_key); +} + +size_t dap_enc_key_get_enc_size(dap_enc_key_t * a_key, const size_t buf_in_size) +{ + if(s_callbacks[a_key->type].enc_out_size) { + return s_callbacks[a_key->type].enc_out_size(buf_in_size); + } + log_it(L_ERROR, "enc_out_size not realize for current key type"); + return 0; +} + +size_t dap_enc_key_get_dec_size(dap_enc_key_t * a_key, const size_t buf_in_size) +{ + if(s_callbacks[a_key->type].dec_out_size) { + return s_callbacks[a_key->type].dec_out_size(buf_in_size); + } + log_it(L_ERROR, "dec_out_size not realize for current key type"); + return 0; +} diff --git a/src/dap_enc_msrln.c b/src/dap_enc_msrln.c index 87bca724c6bcbd94e04a0d5aae844766e43bcb71..80e7ba83896a2ec42bd2e135de5371c4b53e816d 100755 --- a/src/dap_enc_msrln.c +++ b/src/dap_enc_msrln.c @@ -1,245 +1,245 @@ -#include <string.h> -#include "dap_common.h" -#include "dap_enc_msrln.h" -#include "msrln/msrln.h" - - -#define LOG_TAG "dap_enc_msrln" - -void dap_enc_msrln_key_new(struct dap_enc_key* a_key) -{ - a_key->type = DAP_ENC_KEY_TYPE_MSRLN; - a_key->dec = NULL; - a_key->enc = NULL; - a_key->gen_bob_shared_key = (dap_enc_gen_bob_shared_key)dap_enc_msrln_gen_bob_shared_key; - a_key->gen_alice_shared_key = (dap_enc_gen_alice_shared_key)dap_enc_msrln_gen_alice_shared_key; - a_key->priv_key_data_size = 0; - a_key->pub_key_data_size = 0; -} - -///** -// * @brief dap_enc_msrln_key_new_generate -// * @param a_key Struct for new key -// * @param a_size Not used -// */ -//void dap_enc_msrln_key_new_generate(struct dap_enc_key* a_key, size_t a_size) -//{ -// (void)a_size; -// a_key = DAP_NEW(dap_enc_key_t); -// if(a_key == NULL) { -// log_it(L_ERROR, "Can't allocate memory for key"); -// return; -// } - -// a_key->type = DAP_ENC_KEY_TYPE_MSRLN; -// a_key->dec = dap_enc_msrln_decode; -// a_key->enc = dap_enc_msrln_encode; -// a_key->_inheritor = DAP_NEW_Z(dap_enc_msrln_key_t); -// //a_key->delete_callback = dap_enc_msrln_key_delete; -//} - -/** - * @brief dap_enc_msrln_key_generate - * @param a_key - * @param kex_buf - * @param kex_size - * @param seed - * @param seed_size - * @param key_size - * @details allocate memory and generate private and public key - */ -void dap_enc_msrln_key_generate(struct dap_enc_key * a_key, const void *kex_buf, - size_t kex_size, const void * seed, size_t seed_size, - size_t key_size) -{ - (void)kex_buf; (void)kex_size; - (void)seed; (void)seed_size; (void)key_size; - - /* alice_msg is alice's public key */ - a_key->pub_key_data = NULL; - a_key->pub_key_data = malloc(MSRLN_PKA_BYTES); - a_key->pub_key_data_size = MSRLN_PKA_BYTES; - if(a_key->pub_key_data == NULL) { - abort(); - } - - a_key->priv_key_data = malloc(MSRLN_PKA_BYTES * sizeof(uint32_t)); - - PLatticeCryptoStruct PLCS = LatticeCrypto_allocate(); - LatticeCrypto_initialize(PLCS, (RandomBytes)randombytes, MSRLN_generate_a, MSRLN_get_error); - - if (MSRLN_KeyGeneration_A((int32_t *) a_key->priv_key_data, - (unsigned char *) a_key->pub_key_data, PLCS) != CRYPTO_MSRLN_SUCCESS) { - abort(); - } - free(PLCS); - a_key->priv_key_data_size = MSRLN_SHAREDKEY_BYTES; - - return; -} - - -/** - * @brief dap_enc_msrln16_encode - * @param k - * @param alice_priv - * @param alice_msg - * @param alice_msg_len - * @return - */ -size_t dap_enc_msrln_gen_bob_shared_key(struct dap_enc_key* b_key, const void* a_pub, size_t a_pub_size, void ** b_pub) -{ - size_t ret; - - uint8_t *bob_tmp_pub = NULL; - - *b_pub = NULL; - if(b_key->priv_key_data_size == 0) { // need allocate mamory for priv key - b_key->priv_key_data = malloc(MSRLN_SHAREDKEY_BYTES); - b_key->priv_key_data_size = MSRLN_SHAREDKEY_BYTES; - } - // b_key->priv_key_data = NULL; - - if(a_pub_size != MSRLN_PKA_BYTES) { - ret = 0; - DAP_DELETE(b_pub); - b_pub = NULL; - DAP_DELETE(b_key->priv_key_data); - b_key->priv_key_data = NULL; - return ret; - } - - *b_pub = malloc(MSRLN_PKB_BYTES); - if(b_pub == NULL) { - ret = 0; - DAP_DELETE(b_pub); - b_pub = NULL; - DAP_DELETE(b_key->priv_key_data); - b_key->priv_key_data = NULL; - return ret; - } - bob_tmp_pub = *b_pub; - -// b_key->priv_key_data = malloc(MSRLN_SHAREDKEY_BYTES); - if(b_key->priv_key_data == NULL) { - ret = 0; - DAP_DELETE(b_pub); - b_pub = NULL; - DAP_DELETE(b_key->priv_key_data); - b_key->priv_key_data = NULL; - return ret; - } - - PLatticeCryptoStruct PLCS = LatticeCrypto_allocate(); - LatticeCrypto_initialize(PLCS, (RandomBytes)randombytes, MSRLN_generate_a, MSRLN_get_error); - if (MSRLN_SecretAgreement_B((unsigned char *) a_pub, (unsigned char *) b_key->priv_key_data, (unsigned char *) bob_tmp_pub, PLCS) != CRYPTO_MSRLN_SUCCESS) { - ret = 0; - DAP_DELETE(b_pub); - b_pub = NULL; - DAP_DELETE(b_key->priv_key_data); - b_key->priv_key_data = NULL; - return ret; - } - free(PLCS); - - b_key->priv_key_data_size = MSRLN_SHAREDKEY_BYTES; - b_key->pub_key_data_size = MSRLN_PKB_BYTES; - // *a_pub_size = MSRLN_PKB_BYTES; - - ret = 1; - return ret; -} - -/** - * @brief dap_enc_msrln_decode - * @param k - * @param alice_msg - * @param alice_msg_len - * @param bob_msg - * @param bob_msg_len - * @param key - * @param key_len - * @return - */ -size_t dap_enc_msrln_gen_alice_shared_key(struct dap_enc_key* a_key, const void* a_priv, const size_t b_key_len, unsigned char * b_pub) -{ - size_t ret = 1; - - if(a_key->priv_key_data_size == 0) { // need allocate mamory for priv key - a_key->priv_key_data = malloc(MSRLN_SHAREDKEY_BYTES); - a_key->priv_key_data_size = MSRLN_SHAREDKEY_BYTES; - } - - - if(a_key->priv_key_data == NULL || b_key_len != MSRLN_PKB_BYTES) { - ret = 0; - DAP_DELETE(b_pub); - b_pub = NULL; - a_priv = NULL; - DAP_DELETE(a_key->priv_key_data); - a_key->priv_key_data = NULL; - } - - if (MSRLN_SecretAgreement_A((unsigned char *) b_pub, (int32_t *) a_priv, (unsigned char *) a_key->priv_key_data) != CRYPTO_MSRLN_SUCCESS) { - ret = 0; - DAP_DELETE(b_pub); - b_pub = NULL; - a_priv = NULL; - DAP_DELETE(a_key->priv_key_data); - a_key->priv_key_data = NULL; - } - - a_key->priv_key_data_size = MSRLN_SHAREDKEY_BYTES; - - return ret; -} - -/** - * @brief dap_enc_msrln_key_new_from_data_public - * @param a_key - * @param a_in - * @param a_in_size - */ -void dap_enc_msrln_key_new_from_data_public(dap_enc_key_t * a_key, const void * a_in, size_t a_in_size) -{ - (void)a_key; - (void)a_in; - (void)a_in_size; -} - -/** - * @brief dap_enc_msrln_key_delete - * @param a_key - */ -void dap_enc_msrln_key_delete(struct dap_enc_key* a_key) -{ - (void) a_key; - if(!a_key){ - return; - } -// DAP_DELETE(a_key); -} - -/** - * @brief dap_enc_msrln_key_public_base64 - * @param a_key - * @return - */ -char* dap_enc_msrln_key_public_base64(dap_enc_key_t *a_key) -{ - (void)a_key; - return NULL; -} - -/** - * @brief dap_enc_msrln_key_public_raw - * @param a_key - * @param a_key_public - * @return - */ -size_t dap_enc_msrln_key_public_raw(dap_enc_key_t *a_key, void ** a_key_public) -{ - (void)a_key; - (void)a_key_public; - return 0; -} +#include <string.h> +#include "dap_common.h" +#include "dap_enc_msrln.h" +#include "msrln/msrln.h" + + +#define LOG_TAG "dap_enc_msrln" + +void dap_enc_msrln_key_new(struct dap_enc_key* a_key) +{ + a_key->type = DAP_ENC_KEY_TYPE_MSRLN; + a_key->dec = NULL; + a_key->enc = NULL; + a_key->gen_bob_shared_key = (dap_enc_gen_bob_shared_key)dap_enc_msrln_gen_bob_shared_key; + a_key->gen_alice_shared_key = (dap_enc_gen_alice_shared_key)dap_enc_msrln_gen_alice_shared_key; + a_key->priv_key_data_size = 0; + a_key->pub_key_data_size = 0; +} + +///** +// * @brief dap_enc_msrln_key_new_generate +// * @param a_key Struct for new key +// * @param a_size Not used +// */ +//void dap_enc_msrln_key_new_generate(struct dap_enc_key* a_key, size_t a_size) +//{ +// (void)a_size; +// a_key = DAP_NEW(dap_enc_key_t); +// if(a_key == NULL) { +// log_it(L_ERROR, "Can't allocate memory for key"); +// return; +// } + +// a_key->type = DAP_ENC_KEY_TYPE_MSRLN; +// a_key->dec = dap_enc_msrln_decode; +// a_key->enc = dap_enc_msrln_encode; +// a_key->_inheritor = DAP_NEW_Z(dap_enc_msrln_key_t); +// //a_key->delete_callback = dap_enc_msrln_key_delete; +//} + +/** + * @brief dap_enc_msrln_key_generate + * @param a_key + * @param kex_buf + * @param kex_size + * @param seed + * @param seed_size + * @param key_size + * @details allocate memory and generate private and public key + */ +void dap_enc_msrln_key_generate(struct dap_enc_key * a_key, const void *kex_buf, + size_t kex_size, const void * seed, size_t seed_size, + size_t key_size) +{ + (void)kex_buf; (void)kex_size; + (void)seed; (void)seed_size; (void)key_size; + + /* alice_msg is alice's public key */ + a_key->pub_key_data = NULL; + a_key->pub_key_data = malloc(MSRLN_PKA_BYTES); + a_key->pub_key_data_size = MSRLN_PKA_BYTES; + if(a_key->pub_key_data == NULL) { + abort(); + } + + a_key->priv_key_data = malloc(MSRLN_PKA_BYTES * sizeof(uint32_t)); + + PLatticeCryptoStruct PLCS = LatticeCrypto_allocate(); + LatticeCrypto_initialize(PLCS, (RandomBytes)randombytes, MSRLN_generate_a, MSRLN_get_error); + + if (MSRLN_KeyGeneration_A((int32_t *) a_key->priv_key_data, + (unsigned char *) a_key->pub_key_data, PLCS) != CRYPTO_MSRLN_SUCCESS) { + abort(); + } + free(PLCS); + a_key->priv_key_data_size = MSRLN_SHAREDKEY_BYTES; + + return; +} + + +/** + * @brief dap_enc_msrln16_encode + * @param k + * @param alice_priv + * @param alice_msg + * @param alice_msg_len + * @return + */ +size_t dap_enc_msrln_gen_bob_shared_key(struct dap_enc_key* b_key, const void* a_pub, size_t a_pub_size, void ** b_pub) +{ + size_t ret; + + uint8_t *bob_tmp_pub = NULL; + + *b_pub = NULL; + if(b_key->priv_key_data_size == 0) { // need allocate mamory for priv key + b_key->priv_key_data = malloc(MSRLN_SHAREDKEY_BYTES); + b_key->priv_key_data_size = MSRLN_SHAREDKEY_BYTES; + } + // b_key->priv_key_data = NULL; + + if(a_pub_size != MSRLN_PKA_BYTES) { + ret = 0; + DAP_DELETE(b_pub); + b_pub = NULL; + DAP_DELETE(b_key->priv_key_data); + b_key->priv_key_data = NULL; + return ret; + } + + *b_pub = malloc(MSRLN_PKB_BYTES); + if(b_pub == NULL) { + ret = 0; + DAP_DELETE(b_pub); + b_pub = NULL; + DAP_DELETE(b_key->priv_key_data); + b_key->priv_key_data = NULL; + return ret; + } + bob_tmp_pub = *b_pub; + +// b_key->priv_key_data = malloc(MSRLN_SHAREDKEY_BYTES); + if(b_key->priv_key_data == NULL) { + ret = 0; + DAP_DELETE(b_pub); + b_pub = NULL; + DAP_DELETE(b_key->priv_key_data); + b_key->priv_key_data = NULL; + return ret; + } + + PLatticeCryptoStruct PLCS = LatticeCrypto_allocate(); + LatticeCrypto_initialize(PLCS, (RandomBytes)randombytes, MSRLN_generate_a, MSRLN_get_error); + if (MSRLN_SecretAgreement_B((unsigned char *) a_pub, (unsigned char *) b_key->priv_key_data, (unsigned char *) bob_tmp_pub, PLCS) != CRYPTO_MSRLN_SUCCESS) { + ret = 0; + DAP_DELETE(b_pub); + b_pub = NULL; + DAP_DELETE(b_key->priv_key_data); + b_key->priv_key_data = NULL; + return ret; + } + free(PLCS); + + b_key->priv_key_data_size = MSRLN_SHAREDKEY_BYTES; + b_key->pub_key_data_size = MSRLN_PKB_BYTES; + // *a_pub_size = MSRLN_PKB_BYTES; + + ret = 1; + return ret; +} + +/** + * @brief dap_enc_msrln_decode + * @param k + * @param alice_msg + * @param alice_msg_len + * @param bob_msg + * @param bob_msg_len + * @param key + * @param key_len + * @return + */ +size_t dap_enc_msrln_gen_alice_shared_key(struct dap_enc_key* a_key, const void* a_priv, const size_t b_key_len, unsigned char * b_pub) +{ + size_t ret = 1; + + if(a_key->priv_key_data_size == 0) { // need allocate mamory for priv key + a_key->priv_key_data = malloc(MSRLN_SHAREDKEY_BYTES); + a_key->priv_key_data_size = MSRLN_SHAREDKEY_BYTES; + } + + + if(a_key->priv_key_data == NULL || b_key_len != MSRLN_PKB_BYTES) { + ret = 0; + DAP_DELETE(b_pub); + b_pub = NULL; + a_priv = NULL; + DAP_DELETE(a_key->priv_key_data); + a_key->priv_key_data = NULL; + } + + if (MSRLN_SecretAgreement_A((unsigned char *) b_pub, (int32_t *) a_priv, (unsigned char *) a_key->priv_key_data) != CRYPTO_MSRLN_SUCCESS) { + ret = 0; + DAP_DELETE(b_pub); + b_pub = NULL; + a_priv = NULL; + DAP_DELETE(a_key->priv_key_data); + a_key->priv_key_data = NULL; + } + + a_key->priv_key_data_size = MSRLN_SHAREDKEY_BYTES; + + return ret; +} + +/** + * @brief dap_enc_msrln_key_new_from_data_public + * @param a_key + * @param a_in + * @param a_in_size + */ +void dap_enc_msrln_key_new_from_data_public(dap_enc_key_t * a_key, const void * a_in, size_t a_in_size) +{ + (void)a_key; + (void)a_in; + (void)a_in_size; +} + +/** + * @brief dap_enc_msrln_key_delete + * @param a_key + */ +void dap_enc_msrln_key_delete(struct dap_enc_key* a_key) +{ + (void) a_key; + if(!a_key){ + return; + } +// DAP_DELETE(a_key); +} + +/** + * @brief dap_enc_msrln_key_public_base64 + * @param a_key + * @return + */ +char* dap_enc_msrln_key_public_base64(dap_enc_key_t *a_key) +{ + (void)a_key; + return NULL; +} + +/** + * @brief dap_enc_msrln_key_public_raw + * @param a_key + * @param a_key_public + * @return + */ +size_t dap_enc_msrln_key_public_raw(dap_enc_key_t *a_key, void ** a_key_public) +{ + (void)a_key; + (void)a_key_public; + return 0; +} diff --git a/src/dap_enc_oaes.c b/src/dap_enc_oaes.c index e90cd30fbff0ec6c74d40559ab08d1f45fead4f1..d0a37d66dae177859a4b13cc68be846ae0561da9 100755 --- a/src/dap_enc_oaes.c +++ b/src/dap_enc_oaes.c @@ -1,140 +1,140 @@ -#include <stdlib.h> -#include <stdio.h> -#include <stdint.h> -#include <stddef.h> -#include <string.h> - -#include "oaes_lib.h" -#include "dap_enc_oaes.h" -#include "dap_common.h" - -#define LOG_TAG "dap_enc_oaes" - -static OAES_CTX* get_oaes_ctx(struct dap_enc_key *a_key) -{ - OAES_CTX *ctx = NULL; - if(a_key && a_key->_inheritor && - a_key->_inheritor_size == sizeof(oaes_ctx)) { - ctx = a_key->_inheritor; - } - return ctx; -} - -void dap_enc_oaes_key_new(struct dap_enc_key * a_key) -{ - a_key->_inheritor = (uint8_t *) oaes_alloc(); - a_key->_inheritor_size = sizeof(oaes_ctx); - a_key->type = DAP_ENC_KEY_TYPE_OAES; - a_key->enc = dap_enc_oaes_encrypt; - a_key->dec = dap_enc_oaes_decrypt; - a_key->enc_na = dap_enc_oaes_encrypt_fast; - a_key->dec_na = dap_enc_oaes_decrypt_fast; -} - -void dap_enc_oaes_key_delete(struct dap_enc_key *a_key) -{ - - OAES_CTX *ctx = get_oaes_ctx(a_key); - if(ctx) { - oaes_free(&ctx); // free(a_key->_inheritor); - a_key->_inheritor_size = 0; - } -} - -void dap_enc_oaes_key_generate(struct dap_enc_key * a_key, const void *kex_buf, - size_t kex_size, const void * seed, size_t seed_size, size_t key_size) -{ - a_key->last_used_timestamp = time(NULL); - - oaes_ctx *ctx = get_oaes_ctx(a_key); - - if(kex_size < key_size) { - log_it(L_ERROR, "kex_size can't be less than key_size"); - return; - } - - OAES_RET r = oaes_key_import_data(ctx, kex_buf, key_size); - if(r != OAES_RET_SUCCESS) { - log_it(L_ERROR, "Error generate key"); - return; - } - - if(seed_size >= OAES_BLOCK_SIZE) - memcpy(ctx->iv, seed, OAES_BLOCK_SIZE); - else - memset(ctx->iv, 0, OAES_BLOCK_SIZE); -} - -size_t dap_enc_oaes_calc_encode_size(const size_t size_in) -{ - size_t a_out_size = 2 * OAES_BLOCK_SIZE + size_in - + (size_in % OAES_BLOCK_SIZE == 0 ? 0 : - OAES_BLOCK_SIZE - size_in % OAES_BLOCK_SIZE); - return a_out_size; -} - -size_t dap_enc_oaes_calc_decode_size(const size_t size_in) -{ - return size_in - 2 * OAES_BLOCK_SIZE; -} - -size_t dap_enc_oaes_decrypt(struct dap_enc_key *a_key, const void * a_in, - size_t a_in_size, void ** a_out) { - OAES_CTX *ctx = get_oaes_ctx(a_key); - if(!ctx) - return 0; - size_t a_out_size = dap_enc_oaes_calc_decode_size(a_in_size); - *a_out = calloc(a_out_size, 1); - OAES_RET ret = oaes_decrypt(ctx, a_in, a_in_size, *a_out, &a_out_size); - if(ret != OAES_RET_SUCCESS) { - a_out_size = 0; - free(*a_out); - } - return a_out_size; -} - -size_t dap_enc_oaes_encrypt(struct dap_enc_key * a_key, const void * a_in, size_t a_in_size, void ** a_out) -{ - OAES_CTX *ctx = get_oaes_ctx(a_key); - if(!ctx) - return 0; - size_t a_out_size = dap_enc_oaes_calc_encode_size(a_in_size); - *a_out = calloc(a_out_size, 1); - OAES_RET ret = oaes_encrypt(ctx, a_in, a_in_size, *a_out, &a_out_size); - if(ret != OAES_RET_SUCCESS) { - a_out_size = 0; - free(*a_out); - } - return a_out_size; -} - -// Writes result ( out ) in already allocated buffer -size_t dap_enc_oaes_decrypt_fast(struct dap_enc_key * a_key, const void * a_in, size_t a_in_size, - void * buf_out, size_t buf_out_size) -{ - OAES_CTX *ctx = get_oaes_ctx(a_key); - if(!ctx) - return 0; - - OAES_RET ret = oaes_decrypt(ctx, a_in, a_in_size, buf_out, &buf_out_size); - if(ret != OAES_RET_SUCCESS) { - buf_out_size = 0; - } - return buf_out_size; -} - -// Writes result ( out ) in already allocated buffer -size_t dap_enc_oaes_encrypt_fast(struct dap_enc_key * a_key, const void * a_in, - size_t a_in_size, void * buf_out, size_t buf_out_size) -{ - OAES_CTX *ctx = get_oaes_ctx(a_key); - if(!ctx) - return 0; - - OAES_RET ret = oaes_encrypt(ctx, a_in, a_in_size, buf_out, &buf_out_size); - if(ret != OAES_RET_SUCCESS) { - buf_out_size = 0; - } - return buf_out_size; -} - +#include <stdlib.h> +#include <stdio.h> +#include <stdint.h> +#include <stddef.h> +#include <string.h> + +#include "oaes_lib.h" +#include "dap_enc_oaes.h" +#include "dap_common.h" + +#define LOG_TAG "dap_enc_oaes" + +static OAES_CTX* get_oaes_ctx(struct dap_enc_key *a_key) +{ + OAES_CTX *ctx = NULL; + if(a_key && a_key->_inheritor && + a_key->_inheritor_size == sizeof(oaes_ctx)) { + ctx = a_key->_inheritor; + } + return ctx; +} + +void dap_enc_oaes_key_new(struct dap_enc_key * a_key) +{ + a_key->_inheritor = (uint8_t *) oaes_alloc(); + a_key->_inheritor_size = sizeof(oaes_ctx); + a_key->type = DAP_ENC_KEY_TYPE_OAES; + a_key->enc = dap_enc_oaes_encrypt; + a_key->dec = dap_enc_oaes_decrypt; + a_key->enc_na = dap_enc_oaes_encrypt_fast; + a_key->dec_na = dap_enc_oaes_decrypt_fast; +} + +void dap_enc_oaes_key_delete(struct dap_enc_key *a_key) +{ + + OAES_CTX *ctx = get_oaes_ctx(a_key); + if(ctx) { + oaes_free(&ctx); // free(a_key->_inheritor); + a_key->_inheritor_size = 0; + } +} + +void dap_enc_oaes_key_generate(struct dap_enc_key * a_key, const void *kex_buf, + size_t kex_size, const void * seed, size_t seed_size, size_t key_size) +{ + a_key->last_used_timestamp = time(NULL); + + oaes_ctx *ctx = get_oaes_ctx(a_key); + + if(kex_size < key_size) { + log_it(L_ERROR, "kex_size can't be less than key_size"); + return; + } + + OAES_RET r = oaes_key_import_data(ctx, kex_buf, key_size); + if(r != OAES_RET_SUCCESS) { + log_it(L_ERROR, "Error generate key"); + return; + } + + if(seed_size >= OAES_BLOCK_SIZE) + memcpy(ctx->iv, seed, OAES_BLOCK_SIZE); + else + memset(ctx->iv, 0, OAES_BLOCK_SIZE); +} + +size_t dap_enc_oaes_calc_encode_size(const size_t size_in) +{ + size_t a_out_size = 2 * OAES_BLOCK_SIZE + size_in + + (size_in % OAES_BLOCK_SIZE == 0 ? 0 : + OAES_BLOCK_SIZE - size_in % OAES_BLOCK_SIZE); + return a_out_size; +} + +size_t dap_enc_oaes_calc_decode_size(const size_t size_in) +{ + return size_in - 2 * OAES_BLOCK_SIZE; +} + +size_t dap_enc_oaes_decrypt(struct dap_enc_key *a_key, const void * a_in, + size_t a_in_size, void ** a_out) { + OAES_CTX *ctx = get_oaes_ctx(a_key); + if(!ctx) + return 0; + size_t a_out_size = dap_enc_oaes_calc_decode_size(a_in_size); + *a_out = calloc(a_out_size, 1); + OAES_RET ret = oaes_decrypt(ctx, a_in, a_in_size, *a_out, &a_out_size); + if(ret != OAES_RET_SUCCESS) { + a_out_size = 0; + free(*a_out); + } + return a_out_size; +} + +size_t dap_enc_oaes_encrypt(struct dap_enc_key * a_key, const void * a_in, size_t a_in_size, void ** a_out) +{ + OAES_CTX *ctx = get_oaes_ctx(a_key); + if(!ctx) + return 0; + size_t a_out_size = dap_enc_oaes_calc_encode_size(a_in_size); + *a_out = calloc(a_out_size, 1); + OAES_RET ret = oaes_encrypt(ctx, a_in, a_in_size, *a_out, &a_out_size); + if(ret != OAES_RET_SUCCESS) { + a_out_size = 0; + free(*a_out); + } + return a_out_size; +} + +// Writes result ( out ) in already allocated buffer +size_t dap_enc_oaes_decrypt_fast(struct dap_enc_key * a_key, const void * a_in, size_t a_in_size, + void * buf_out, size_t buf_out_size) +{ + OAES_CTX *ctx = get_oaes_ctx(a_key); + if(!ctx) + return 0; + + OAES_RET ret = oaes_decrypt(ctx, a_in, a_in_size, buf_out, &buf_out_size); + if(ret != OAES_RET_SUCCESS) { + buf_out_size = 0; + } + return buf_out_size; +} + +// Writes result ( out ) in already allocated buffer +size_t dap_enc_oaes_encrypt_fast(struct dap_enc_key * a_key, const void * a_in, + size_t a_in_size, void * buf_out, size_t buf_out_size) +{ + OAES_CTX *ctx = get_oaes_ctx(a_key); + if(!ctx) + return 0; + + OAES_RET ret = oaes_encrypt(ctx, a_in, a_in_size, buf_out, &buf_out_size); + if(ret != OAES_RET_SUCCESS) { + buf_out_size = 0; + } + return buf_out_size; +} + diff --git a/src/dap_enc_picnic.c b/src/dap_enc_picnic.c index 6c64c3d768eabf3612b137f40ed1e08a838e48c8..f45985de5af3b800d666dd86e868f9ebfba0265b 100755 --- a/src/dap_enc_picnic.c +++ b/src/dap_enc_picnic.c @@ -1,212 +1,212 @@ -#include "dap_common.h" -#include "dap_enc_picnic.h" -#include <stdio.h> -#include <stddef.h> -#include <stdbool.h> -#include <stdint.h> -#include <stdlib.h> -#include "picnic.h" -#include "picnic_impl.h" - -#define LOG_TAG "dap_enc_picnic_sig" - -/** - * Set the mark that valid keys are present - */ -static void set_picnic_params_t(struct dap_enc_key *key) -{ - picnic_params_t *param = (key) ? (picnic_params_t*) key->_inheritor : NULL; - if(param && key->_inheritor_size == sizeof(picnic_params_t)){ - if(key->priv_key_data) - *param = ((picnic_privatekey_t*) key->priv_key_data)->params; - else if(key->pub_key_data) - *param = ((picnic_publickey_t*) key->pub_key_data)->params; - } -} - -/** - * Check present of valid keys - */ -static bool check_picnic_params_t(struct dap_enc_key *key) -{ - picnic_params_t *param = (key) ? (picnic_params_t*) key->_inheritor : NULL; - if(param && *param > PARAMETER_SET_INVALID && *param < PARAMETER_SET_MAX_INDEX) - return true; - return false; -} - -size_t dap_enc_picnic_calc_signature_size(struct dap_enc_key *key) -{ - picnic_params_t *param = (picnic_params_t*) key->_inheritor; - size_t max_signature_size = picnic_signature_size(*param); - return max_signature_size; -} - -void dap_enc_sig_picnic_key_new(struct dap_enc_key *key) { - - key->type = DAP_ENC_KEY_TYPE_SIG_PICNIC; - key->_inheritor = calloc(sizeof(picnic_params_t), 1); - key->_inheritor_size = sizeof(picnic_params_t); - key->enc = NULL; - key->enc = NULL; - key->gen_bob_shared_key = NULL; //(dap_enc_gen_bob_shared_key) dap_enc_sig_picnic_get_sign; - key->gen_alice_shared_key = NULL; //(dap_enc_gen_alice_shared_key) dap_enc_sig_picnic_verify_sign; - key->enc_na = (dap_enc_callback_dataop_na_t) dap_enc_sig_picnic_get_sign; - key->dec_na = (dap_enc_callback_dataop_na_t) dap_enc_sig_picnic_verify_sign; - key->priv_key_data = NULL; - key->pub_key_data = NULL; -} - -void dap_enc_sig_picnic_key_delete(struct dap_enc_key *key) -{ - if(key->_inheritor_size > 0) - free(key->_inheritor); - key->_inheritor = NULL; - key->_inheritor_size = 0; - // free memory will be in dap_enc_key_delete() - //picnic_keypair_delete((picnic_privatekey_t*) key->priv_key_data, (picnic_publickey_t *) key->pub_key_data); - key->priv_key_data_size = 0; - key->pub_key_data_size = 0; -} - -void dap_enc_sig_picnic_update(struct dap_enc_key * a_key) -{ - if(a_key) { - if(!a_key->priv_key_data || - !picnic_validate_keypair((picnic_privatekey_t *) a_key->priv_key_data, (picnic_publickey_t *) a_key->pub_key_data)) - set_picnic_params_t(a_key); - } -} - -void dap_enc_sig_picnic_key_new_generate(struct dap_enc_key * key, const void *kex_buf, size_t kex_size, - const void * seed, size_t seed_size, size_t key_size) -{ - (void) kex_buf; - (void) kex_size; - (void) key_size; - picnic_params_t parameters; - // Parameter name from Picnic_L1_FS = 1 to PARAMETER_SET_MAX_INDEX - if(seed_size >= sizeof(unsigned char) && seed) - parameters = (((unsigned char*) seed)[0] % (PARAMETER_SET_MAX_INDEX - 1)) + 1; - else - parameters = DAP_PICNIC_SIGN_PARAMETR; - - key->priv_key_data_size = sizeof(picnic_privatekey_t); - key->pub_key_data_size = sizeof(picnic_publickey_t); - key->priv_key_data = calloc(1, key->priv_key_data_size); - key->pub_key_data = calloc(1, key->pub_key_data_size); - - picnic_keys_gen((picnic_privatekey_t *) key->priv_key_data, (picnic_publickey_t *) key->pub_key_data, parameters, seed, seed_size); - if(!picnic_validate_keypair((picnic_privatekey_t *) key->priv_key_data, (picnic_publickey_t *) key->pub_key_data)) - set_picnic_params_t(key); -} - -size_t dap_enc_sig_picnic_get_sign(struct dap_enc_key * key, const void* message, size_t message_len, - void* signature, size_t signature_len) -{ - int ret; - if(!check_picnic_params_t(key)) - return -1; - picnic_privatekey_t* sk = key->priv_key_data; - signature_t* sig = (signature_t*) malloc(sizeof(signature_t)); - paramset_t paramset; - - ret = get_param_set(sk->params, ¶mset); - if(ret != EXIT_SUCCESS) { - free(sig); - return -1; - } - - allocateSignature(sig, ¶mset); - if(sig == NULL) { - return -1; - } - - ret = sign((uint32_t*) sk->data, (uint32_t*) sk->pk.ciphertext, (uint32_t*) sk->pk.plaintext, (const uint8_t*)message, - message_len, sig, ¶mset); - if(ret != EXIT_SUCCESS) { - freeSignature(sig, ¶mset); - free(sig); - return -1; - } - ret = serializeSignature(sig, (uint8_t*)signature, signature_len, ¶mset); - if(ret == -1) { - freeSignature(sig, ¶mset); - free(sig); - return -1; - } -// *signature_len = ret; - freeSignature(sig, ¶mset); - free(sig); - return ret; -} - -size_t dap_enc_sig_picnic_verify_sign(struct dap_enc_key * key, const void* message, size_t message_len, - void* signature, size_t signature_len) -{ - int ret; - if(!check_picnic_params_t(key)) - return -1; - picnic_publickey_t* pk = key->pub_key_data; - paramset_t paramset; - - ret = get_param_set(pk->params, ¶mset); - if(ret != EXIT_SUCCESS) - return -1; - - signature_t* sig = (signature_t*) malloc(sizeof(signature_t)); - allocateSignature(sig, ¶mset); - if(sig == NULL) { - return -1; - } - - ret = deserializeSignature(sig, (const uint8_t*)signature, signature_len, ¶mset); - if(ret != EXIT_SUCCESS) { - freeSignature(sig, ¶mset); - free(sig); - return -1; - } - - ret = verify(sig, (uint32_t*) pk->ciphertext, - (uint32_t*) pk->plaintext, (const uint8_t*)message, message_len, ¶mset); - if(ret != EXIT_SUCCESS) { - /* Signature is invalid, or verify function failed */ - freeSignature(sig, ¶mset); - free(sig); - return -1; - } - - freeSignature(sig, ¶mset); - free(sig); - return 0; -} - -/* -uint8_t* dap_enc_sig_picnic_write_public_key(struct dap_enc_key * a_key, size_t *a_buflen_out) -{ - const picnic_publickey_t *l_key = a_key->pub_key_data; - size_t buflen = picnic_get_public_key_size(l_key); // Get public key size for serialize - uint8_t* l_buf = DAP_NEW_SIZE(uint8_t, buflen); - // Serialize public key - if(picnic_write_public_key(l_key, l_buf, buflen)>0){ - if(a_buflen_out) - *a_buflen_out = buflen; - return l_buf; - } - return NULL; -} - -uint8_t* dap_enc_sig_picnic_read_public_key(struct dap_enc_key * a_key, uint8_t a_buf, size_t *a_buflen) -{ - const picnic_publickey_t *l_key = a_key->pub_key_data; - size_t buflen = picnic_get_public_key_size(l_key); Get public key size for serialize - uint8_t* l_buf = DAP_NEW_SIZE(uint8_t, buflen); - // Deserialize public key - if(!picnic_read_public_key(l_key, a_l_buf, buflen)>0){ - if(a_buflen_out) - *a_buflen_out = buflen; - return l_buf; - } - return NULL; -}*/ - +#include "dap_common.h" +#include "dap_enc_picnic.h" +#include <stdio.h> +#include <stddef.h> +#include <stdbool.h> +#include <stdint.h> +#include <stdlib.h> +#include "picnic.h" +#include "picnic_impl.h" + +#define LOG_TAG "dap_enc_picnic_sig" + +/** + * Set the mark that valid keys are present + */ +static void set_picnic_params_t(struct dap_enc_key *key) +{ + picnic_params_t *param = (key) ? (picnic_params_t*) key->_inheritor : NULL; + if(param && key->_inheritor_size == sizeof(picnic_params_t)){ + if(key->priv_key_data) + *param = ((picnic_privatekey_t*) key->priv_key_data)->params; + else if(key->pub_key_data) + *param = ((picnic_publickey_t*) key->pub_key_data)->params; + } +} + +/** + * Check present of valid keys + */ +static bool check_picnic_params_t(struct dap_enc_key *key) +{ + picnic_params_t *param = (key) ? (picnic_params_t*) key->_inheritor : NULL; + if(param && *param > PARAMETER_SET_INVALID && *param < PARAMETER_SET_MAX_INDEX) + return true; + return false; +} + +size_t dap_enc_picnic_calc_signature_size(struct dap_enc_key *key) +{ + picnic_params_t *param = (picnic_params_t*) key->_inheritor; + size_t max_signature_size = picnic_signature_size(*param); + return max_signature_size; +} + +void dap_enc_sig_picnic_key_new(struct dap_enc_key *key) { + + key->type = DAP_ENC_KEY_TYPE_SIG_PICNIC; + key->_inheritor = calloc(sizeof(picnic_params_t), 1); + key->_inheritor_size = sizeof(picnic_params_t); + key->enc = NULL; + key->enc = NULL; + key->gen_bob_shared_key = NULL; //(dap_enc_gen_bob_shared_key) dap_enc_sig_picnic_get_sign; + key->gen_alice_shared_key = NULL; //(dap_enc_gen_alice_shared_key) dap_enc_sig_picnic_verify_sign; + key->enc_na = (dap_enc_callback_dataop_na_t) dap_enc_sig_picnic_get_sign; + key->dec_na = (dap_enc_callback_dataop_na_t) dap_enc_sig_picnic_verify_sign; + key->priv_key_data = NULL; + key->pub_key_data = NULL; +} + +void dap_enc_sig_picnic_key_delete(struct dap_enc_key *key) +{ + if(key->_inheritor_size > 0) + free(key->_inheritor); + key->_inheritor = NULL; + key->_inheritor_size = 0; + // free memory will be in dap_enc_key_delete() + //picnic_keypair_delete((picnic_privatekey_t*) key->priv_key_data, (picnic_publickey_t *) key->pub_key_data); + key->priv_key_data_size = 0; + key->pub_key_data_size = 0; +} + +void dap_enc_sig_picnic_update(struct dap_enc_key * a_key) +{ + if(a_key) { + if(!a_key->priv_key_data || + !picnic_validate_keypair((picnic_privatekey_t *) a_key->priv_key_data, (picnic_publickey_t *) a_key->pub_key_data)) + set_picnic_params_t(a_key); + } +} + +void dap_enc_sig_picnic_key_new_generate(struct dap_enc_key * key, const void *kex_buf, size_t kex_size, + const void * seed, size_t seed_size, size_t key_size) +{ + (void) kex_buf; + (void) kex_size; + (void) key_size; + picnic_params_t parameters; + // Parameter name from Picnic_L1_FS = 1 to PARAMETER_SET_MAX_INDEX + if(seed_size >= sizeof(unsigned char) && seed) + parameters = (((unsigned char*) seed)[0] % (PARAMETER_SET_MAX_INDEX - 1)) + 1; + else + parameters = DAP_PICNIC_SIGN_PARAMETR; + + key->priv_key_data_size = sizeof(picnic_privatekey_t); + key->pub_key_data_size = sizeof(picnic_publickey_t); + key->priv_key_data = calloc(1, key->priv_key_data_size); + key->pub_key_data = calloc(1, key->pub_key_data_size); + + picnic_keys_gen((picnic_privatekey_t *) key->priv_key_data, (picnic_publickey_t *) key->pub_key_data, parameters, seed, seed_size); + if(!picnic_validate_keypair((picnic_privatekey_t *) key->priv_key_data, (picnic_publickey_t *) key->pub_key_data)) + set_picnic_params_t(key); +} + +size_t dap_enc_sig_picnic_get_sign(struct dap_enc_key * key, const void* message, size_t message_len, + void* signature, size_t signature_len) +{ + int ret; + if(!check_picnic_params_t(key)) + return -1; + picnic_privatekey_t* sk = key->priv_key_data; + signature_t* sig = (signature_t*) malloc(sizeof(signature_t)); + paramset_t paramset; + + ret = get_param_set(sk->params, ¶mset); + if(ret != EXIT_SUCCESS) { + free(sig); + return -1; + } + + allocateSignature(sig, ¶mset); + if(sig == NULL) { + return -1; + } + + ret = sign((uint32_t*) sk->data, (uint32_t*) sk->pk.ciphertext, (uint32_t*) sk->pk.plaintext, (const uint8_t*)message, + message_len, sig, ¶mset); + if(ret != EXIT_SUCCESS) { + freeSignature(sig, ¶mset); + free(sig); + return -1; + } + ret = serializeSignature(sig, (uint8_t*)signature, signature_len, ¶mset); + if(ret == -1) { + freeSignature(sig, ¶mset); + free(sig); + return -1; + } +// *signature_len = ret; + freeSignature(sig, ¶mset); + free(sig); + return ret; +} + +size_t dap_enc_sig_picnic_verify_sign(struct dap_enc_key * key, const void* message, size_t message_len, + void* signature, size_t signature_len) +{ + int ret; + if(!check_picnic_params_t(key)) + return -1; + picnic_publickey_t* pk = key->pub_key_data; + paramset_t paramset; + + ret = get_param_set(pk->params, ¶mset); + if(ret != EXIT_SUCCESS) + return -1; + + signature_t* sig = (signature_t*) malloc(sizeof(signature_t)); + allocateSignature(sig, ¶mset); + if(sig == NULL) { + return -1; + } + + ret = deserializeSignature(sig, (const uint8_t*)signature, signature_len, ¶mset); + if(ret != EXIT_SUCCESS) { + freeSignature(sig, ¶mset); + free(sig); + return -1; + } + + ret = verify(sig, (uint32_t*) pk->ciphertext, + (uint32_t*) pk->plaintext, (const uint8_t*)message, message_len, ¶mset); + if(ret != EXIT_SUCCESS) { + /* Signature is invalid, or verify function failed */ + freeSignature(sig, ¶mset); + free(sig); + return -1; + } + + freeSignature(sig, ¶mset); + free(sig); + return 0; +} + +/* +uint8_t* dap_enc_sig_picnic_write_public_key(struct dap_enc_key * a_key, size_t *a_buflen_out) +{ + const picnic_publickey_t *l_key = a_key->pub_key_data; + size_t buflen = picnic_get_public_key_size(l_key); // Get public key size for serialize + uint8_t* l_buf = DAP_NEW_SIZE(uint8_t, buflen); + // Serialize public key + if(picnic_write_public_key(l_key, l_buf, buflen)>0){ + if(a_buflen_out) + *a_buflen_out = buflen; + return l_buf; + } + return NULL; +} + +uint8_t* dap_enc_sig_picnic_read_public_key(struct dap_enc_key * a_key, uint8_t a_buf, size_t *a_buflen) +{ + const picnic_publickey_t *l_key = a_key->pub_key_data; + size_t buflen = picnic_get_public_key_size(l_key); Get public key size for serialize + uint8_t* l_buf = DAP_NEW_SIZE(uint8_t, buflen); + // Deserialize public key + if(!picnic_read_public_key(l_key, a_l_buf, buflen)>0){ + if(a_buflen_out) + *a_buflen_out = buflen; + return l_buf; + } + return NULL; +}*/ + diff --git a/src/dap_enc_tesla.c b/src/dap_enc_tesla.c index 4da96a8ddafc58f0dd0b0f1a1ebaba374b77c1f4..ca979cb6c949872c072695d4f06988f5f4b8cc81 100755 --- a/src/dap_enc_tesla.c +++ b/src/dap_enc_tesla.c @@ -1,237 +1,237 @@ -#include <assert.h> -#include <inttypes.h> -#include <string.h> - -#include "dap_enc_tesla.h" -#include "dap_common.h" -#include "dap_rand.h" - -#define LOG_TAG "dap_enc_sig_tesla" - -static enum DAP_TESLA_SIGN_SECURITY _tesla_type = HEURISTIC_MAX_SECURITY_AND_MAX_SPEED; // by default - -void dap_enc_sig_tesla_set_type(enum DAP_TESLA_SIGN_SECURITY type) -{ - _tesla_type = type; -} - -void dap_enc_sig_tesla_key_new(struct dap_enc_key *key) { - - key->type = DAP_ENC_KEY_TYPE_SIG_TESLA; - key->enc = NULL; - key->enc_na = (dap_enc_callback_dataop_na_t) dap_enc_sig_tesla_get_sign; - key->dec_na = (dap_enc_callback_dataop_na_t) dap_enc_sig_tesla_verify_sign; -// key->gen_bob_shared_key = (dap_enc_gen_bob_shared_key) dap_enc_sig_tesla_get_sign; -// key->gen_alice_shared_key = (dap_enc_gen_alice_shared_key) dap_enc_sig_tesla_verify_sign; -} - -// generation key pair for sign Alice -// OUTPUT: -// a_key->data --- Alice's public key -// alice_priv --- Alice's private key -// alice_msg_len --- Alice's private key length -void dap_enc_sig_tesla_key_new_generate(struct dap_enc_key * key, const void *kex_buf, - size_t kex_size, const void * seed, size_t seed_size, - size_t key_size) -{ - (void) kex_buf; - (void) kex_size; - (void) key_size; - - int32_t retcode; - - int tesla_type = (seed && seed_size >= sizeof(uint8_t)) ? ((uint8_t*)seed)[0] % (PROVABLY_MAX_SECURITY + 1) : - HEURISTIC_MAX_SECURITY_AND_MAX_SPEED; - dap_enc_sig_tesla_set_type(tesla_type); - - /* type is a param of sign-security - * type = 0 - Heuristic qTESLA, NIST's security category 1 - * type = 1 - Heuristic qTESLA, NIST's security category 3 (option for size) - * type = 2 - Heuristic qTESLA, NIST's security category 3 (option for speed) - * type = 3 - Provably-secure qTESLA, NIST's security category 1 - * type = 4 - Provably-secure qTESLA, NIST's security category 3 (max security) - */ - //int32_t type = 2; - key->priv_key_data_size = sizeof(tesla_private_key_t); - key->pub_key_data_size = sizeof(tesla_public_key_t); - key->priv_key_data = malloc(key->priv_key_data_size); - key->pub_key_data = malloc(key->pub_key_data_size); - - retcode = tesla_crypto_sign_keypair((tesla_public_key_t *) key->pub_key_data, - (tesla_private_key_t *) key->priv_key_data, _tesla_type, seed, seed_size); - if(retcode != 0) { - tesla_private_and_public_keys_delete((tesla_private_key_t *) key->pub_key_data, - (tesla_public_key_t *) key->pub_key_data); - log_it(L_CRITICAL, "Error"); - return; - } -} - -size_t dap_enc_sig_tesla_get_sign(struct dap_enc_key * key, const void * msg, - const size_t msg_size, void * signature, const size_t signature_size) -{ - if(signature_size < sizeof(tesla_signature_t)) { - log_it(L_ERROR, "bad signature size"); - return 0; - } - - if(!tesla_crypto_sign((tesla_signature_t *) signature, (const unsigned char *) msg, msg_size, key->priv_key_data)) - return signature_size; - else - return 0; -} - -size_t dap_enc_sig_tesla_verify_sign(struct dap_enc_key * key, const void * msg, - const size_t msg_size, void * signature, const size_t signature_size) -{ - if(signature_size < sizeof(tesla_signature_t)) { - log_it(L_ERROR, "bad signature size"); - return 0; - } - - return (tesla_crypto_sign_open((tesla_signature_t *) signature, (unsigned char *) msg, msg_size, key->pub_key_data)); -} - -void dap_enc_sig_tesla_key_delete(struct dap_enc_key * key) -{ - tesla_private_and_public_keys_delete((tesla_private_key_t *) key->priv_key_data, - (tesla_public_key_t *) key->pub_key_data); -} - -size_t dap_enc_tesla_calc_signature_size(void) -{ - return sizeof(tesla_signature_t); -} - -size_t dap_enc_tesla_calc_signature_serialized_size(tesla_signature_t* a_sign) -{ - return sizeof(size_t) + sizeof(tesla_kind_t) + a_sign->sig_len + sizeof(unsigned long long); -} - -/* Serialize a signature */ -uint8_t* dap_enc_tesla_write_signature(tesla_signature_t* a_sign, size_t *a_sign_out) -{ - if(!a_sign || *a_sign_out!=sizeof(tesla_signature_t)) { - return NULL ; - } - size_t l_shift_mem = 0; - size_t l_buflen = dap_enc_tesla_calc_signature_serialized_size(a_sign); - - uint8_t *l_buf = DAP_NEW_SIZE(uint8_t, l_buflen); - memcpy(l_buf, &l_buflen, sizeof(size_t)); - l_shift_mem += sizeof(size_t); - memcpy(l_buf + l_shift_mem, &a_sign->kind, sizeof(tesla_kind_t)); - l_shift_mem += sizeof(tesla_kind_t); - memcpy(l_buf + l_shift_mem, &a_sign->sig_len, sizeof(unsigned long long)); - l_shift_mem += sizeof(unsigned long long); - memcpy(l_buf + l_shift_mem, a_sign->sig_data, a_sign->sig_len ); - l_shift_mem += a_sign->sig_len ; - - if(a_sign_out) - *a_sign_out = l_buflen; - return l_buf; -} - -/* Deserialize a signature */ -tesla_signature_t* dap_enc_tesla_read_signature(uint8_t *a_buf, size_t a_buflen) -{ - if(!a_buf || a_buflen < (sizeof(size_t) + sizeof(tesla_kind_t))) - return NULL ; - tesla_kind_t kind; - size_t l_buflen = 0; - memcpy(&l_buflen, a_buf, sizeof(size_t)); - memcpy(&kind, a_buf + sizeof(size_t), sizeof(tesla_kind_t)); - if(l_buflen != a_buflen) - return NULL ; - tesla_param_t p; - if(!tesla_params_init(&p, kind)) - return NULL ; - - tesla_signature_t* l_sign = DAP_NEW(tesla_signature_t); - l_sign->kind = kind; - size_t l_shift_mem = sizeof(size_t) + sizeof(tesla_kind_t); - memcpy(&l_sign->sig_len, a_buf + l_shift_mem, sizeof(unsigned long long)); - l_shift_mem += sizeof(unsigned long long); - l_sign->sig_data = DAP_NEW_SIZE(unsigned char, l_sign->sig_len); - memcpy(l_sign->sig_data, a_buf + l_shift_mem, l_sign->sig_len); - l_shift_mem += l_sign->sig_len; - return l_sign; -} - -/* Serialize a private key. */ -uint8_t* dap_enc_tesla_write_private_key(const tesla_private_key_t* a_private_key, size_t *a_buflen_out) -{ - tesla_param_t p;// = malloc(sizeof(tesla_param_t)); - if(!tesla_params_init(&p, a_private_key->kind)) - return NULL; - - size_t l_buflen = sizeof(size_t) + sizeof(tesla_kind_t) + p.CRYPTO_SECRETKEYBYTES; //CRYPTO_PUBLICKEYBYTES; - uint8_t *l_buf = DAP_NEW_SIZE(uint8_t, l_buflen); - memcpy(l_buf, &l_buflen, sizeof(size_t)); - memcpy(l_buf + sizeof(size_t), &a_private_key->kind, sizeof(tesla_kind_t)); - memcpy(l_buf + sizeof(size_t) + sizeof(tesla_kind_t), a_private_key->data, p.CRYPTO_SECRETKEYBYTES); - if(a_buflen_out) - *a_buflen_out = l_buflen; - return l_buf; -} - -/* Serialize a public key. */ -uint8_t* dap_enc_tesla_write_public_key(const tesla_public_key_t* a_public_key, size_t *a_buflen_out) -{ - tesla_param_t p; - if(!tesla_params_init(&p, a_public_key->kind)) - return NULL; - - size_t l_buflen = sizeof(size_t) + sizeof(tesla_kind_t) + p.CRYPTO_PUBLICKEYBYTES; - uint8_t *l_buf = DAP_NEW_SIZE(uint8_t, l_buflen); - memcpy(l_buf, &l_buflen, sizeof(size_t)); - memcpy(l_buf + sizeof(size_t), &a_public_key->kind, sizeof(tesla_kind_t)); - memcpy(l_buf + sizeof(size_t) + sizeof(tesla_kind_t), a_public_key->data, p.CRYPTO_PUBLICKEYBYTES); - if(a_buflen_out) - *a_buflen_out = l_buflen; - return l_buf; -} - -/* Deserialize a private key. */ -tesla_private_key_t* dap_enc_tesla_read_private_key(const uint8_t *a_buf, size_t a_buflen) -{ - if(!a_buf || a_buflen < (sizeof(size_t) + sizeof(tesla_kind_t))) - return NULL; - tesla_kind_t kind; - size_t l_buflen = 0; - memcpy(&l_buflen, a_buf, sizeof(size_t)); - memcpy(&kind, a_buf + sizeof(size_t), sizeof(tesla_kind_t)); - if(l_buflen != a_buflen) - return NULL; - tesla_param_t p; - if(!tesla_params_init(&p, kind)) - return NULL; - tesla_private_key_t* l_private_key = DAP_NEW(tesla_private_key_t); - l_private_key->kind = kind; - - l_private_key->data = DAP_NEW_SIZE(unsigned char, p.CRYPTO_SECRETKEYBYTES); - memcpy(l_private_key->data, a_buf + sizeof(size_t) + sizeof(tesla_kind_t), p.CRYPTO_SECRETKEYBYTES); - return l_private_key; -} - -/* Deserialize a public key. */ -tesla_public_key_t* dap_enc_tesla_read_public_key(const uint8_t *a_buf, size_t a_buflen) -{ - if(!a_buf || a_buflen < (sizeof(size_t) + sizeof(tesla_kind_t))) - return NULL; - tesla_kind_t kind; - size_t l_buflen = 0; - memcpy(&l_buflen, a_buf, sizeof(size_t)); - memcpy(&kind, a_buf + sizeof(size_t), sizeof(tesla_kind_t)); - if(l_buflen != a_buflen) - return NULL; - tesla_param_t p; - if(!tesla_params_init(&p, kind)) - return NULL; - tesla_public_key_t* l_public_key = DAP_NEW(tesla_public_key_t); - l_public_key->kind = kind; - - l_public_key->data = DAP_NEW_SIZE(unsigned char, p.CRYPTO_PUBLICKEYBYTES); - memcpy(l_public_key->data, a_buf + sizeof(size_t) + sizeof(tesla_kind_t), p.CRYPTO_PUBLICKEYBYTES); - return l_public_key; -} +#include <assert.h> +#include <inttypes.h> +#include <string.h> + +#include "dap_enc_tesla.h" +#include "dap_common.h" +#include "dap_rand.h" + +#define LOG_TAG "dap_enc_sig_tesla" + +static enum DAP_TESLA_SIGN_SECURITY _tesla_type = HEURISTIC_MAX_SECURITY_AND_MAX_SPEED; // by default + +void dap_enc_sig_tesla_set_type(enum DAP_TESLA_SIGN_SECURITY type) +{ + _tesla_type = type; +} + +void dap_enc_sig_tesla_key_new(struct dap_enc_key *key) { + + key->type = DAP_ENC_KEY_TYPE_SIG_TESLA; + key->enc = NULL; + key->enc_na = (dap_enc_callback_dataop_na_t) dap_enc_sig_tesla_get_sign; + key->dec_na = (dap_enc_callback_dataop_na_t) dap_enc_sig_tesla_verify_sign; +// key->gen_bob_shared_key = (dap_enc_gen_bob_shared_key) dap_enc_sig_tesla_get_sign; +// key->gen_alice_shared_key = (dap_enc_gen_alice_shared_key) dap_enc_sig_tesla_verify_sign; +} + +// generation key pair for sign Alice +// OUTPUT: +// a_key->data --- Alice's public key +// alice_priv --- Alice's private key +// alice_msg_len --- Alice's private key length +void dap_enc_sig_tesla_key_new_generate(struct dap_enc_key * key, const void *kex_buf, + size_t kex_size, const void * seed, size_t seed_size, + size_t key_size) +{ + (void) kex_buf; + (void) kex_size; + (void) key_size; + + int32_t retcode; + + int tesla_type = (seed && seed_size >= sizeof(uint8_t)) ? ((uint8_t*)seed)[0] % (PROVABLY_MAX_SECURITY + 1) : + HEURISTIC_MAX_SECURITY_AND_MAX_SPEED; + dap_enc_sig_tesla_set_type(tesla_type); + + /* type is a param of sign-security + * type = 0 - Heuristic qTESLA, NIST's security category 1 + * type = 1 - Heuristic qTESLA, NIST's security category 3 (option for size) + * type = 2 - Heuristic qTESLA, NIST's security category 3 (option for speed) + * type = 3 - Provably-secure qTESLA, NIST's security category 1 + * type = 4 - Provably-secure qTESLA, NIST's security category 3 (max security) + */ + //int32_t type = 2; + key->priv_key_data_size = sizeof(tesla_private_key_t); + key->pub_key_data_size = sizeof(tesla_public_key_t); + key->priv_key_data = malloc(key->priv_key_data_size); + key->pub_key_data = malloc(key->pub_key_data_size); + + retcode = tesla_crypto_sign_keypair((tesla_public_key_t *) key->pub_key_data, + (tesla_private_key_t *) key->priv_key_data, _tesla_type, seed, seed_size); + if(retcode != 0) { + tesla_private_and_public_keys_delete((tesla_private_key_t *) key->pub_key_data, + (tesla_public_key_t *) key->pub_key_data); + log_it(L_CRITICAL, "Error"); + return; + } +} + +size_t dap_enc_sig_tesla_get_sign(struct dap_enc_key * key, const void * msg, + const size_t msg_size, void * signature, const size_t signature_size) +{ + if(signature_size < sizeof(tesla_signature_t)) { + log_it(L_ERROR, "bad signature size"); + return 0; + } + + if(!tesla_crypto_sign((tesla_signature_t *) signature, (const unsigned char *) msg, msg_size, key->priv_key_data)) + return signature_size; + else + return 0; +} + +size_t dap_enc_sig_tesla_verify_sign(struct dap_enc_key * key, const void * msg, + const size_t msg_size, void * signature, const size_t signature_size) +{ + if(signature_size < sizeof(tesla_signature_t)) { + log_it(L_ERROR, "bad signature size"); + return 0; + } + + return (tesla_crypto_sign_open((tesla_signature_t *) signature, (unsigned char *) msg, msg_size, key->pub_key_data)); +} + +void dap_enc_sig_tesla_key_delete(struct dap_enc_key * key) +{ + tesla_private_and_public_keys_delete((tesla_private_key_t *) key->priv_key_data, + (tesla_public_key_t *) key->pub_key_data); +} + +size_t dap_enc_tesla_calc_signature_size(void) +{ + return sizeof(tesla_signature_t); +} + +size_t dap_enc_tesla_calc_signature_serialized_size(tesla_signature_t* a_sign) +{ + return sizeof(size_t) + sizeof(tesla_kind_t) + a_sign->sig_len + sizeof(unsigned long long); +} + +/* Serialize a signature */ +uint8_t* dap_enc_tesla_write_signature(tesla_signature_t* a_sign, size_t *a_sign_out) +{ + if(!a_sign || *a_sign_out!=sizeof(tesla_signature_t)) { + return NULL ; + } + size_t l_shift_mem = 0; + size_t l_buflen = dap_enc_tesla_calc_signature_serialized_size(a_sign); + + uint8_t *l_buf = DAP_NEW_SIZE(uint8_t, l_buflen); + memcpy(l_buf, &l_buflen, sizeof(size_t)); + l_shift_mem += sizeof(size_t); + memcpy(l_buf + l_shift_mem, &a_sign->kind, sizeof(tesla_kind_t)); + l_shift_mem += sizeof(tesla_kind_t); + memcpy(l_buf + l_shift_mem, &a_sign->sig_len, sizeof(unsigned long long)); + l_shift_mem += sizeof(unsigned long long); + memcpy(l_buf + l_shift_mem, a_sign->sig_data, a_sign->sig_len ); + l_shift_mem += a_sign->sig_len ; + + if(a_sign_out) + *a_sign_out = l_buflen; + return l_buf; +} + +/* Deserialize a signature */ +tesla_signature_t* dap_enc_tesla_read_signature(uint8_t *a_buf, size_t a_buflen) +{ + if(!a_buf || a_buflen < (sizeof(size_t) + sizeof(tesla_kind_t))) + return NULL ; + tesla_kind_t kind; + size_t l_buflen = 0; + memcpy(&l_buflen, a_buf, sizeof(size_t)); + memcpy(&kind, a_buf + sizeof(size_t), sizeof(tesla_kind_t)); + if(l_buflen != a_buflen) + return NULL ; + tesla_param_t p; + if(!tesla_params_init(&p, kind)) + return NULL ; + + tesla_signature_t* l_sign = DAP_NEW(tesla_signature_t); + l_sign->kind = kind; + size_t l_shift_mem = sizeof(size_t) + sizeof(tesla_kind_t); + memcpy(&l_sign->sig_len, a_buf + l_shift_mem, sizeof(unsigned long long)); + l_shift_mem += sizeof(unsigned long long); + l_sign->sig_data = DAP_NEW_SIZE(unsigned char, l_sign->sig_len); + memcpy(l_sign->sig_data, a_buf + l_shift_mem, l_sign->sig_len); + l_shift_mem += l_sign->sig_len; + return l_sign; +} + +/* Serialize a private key. */ +uint8_t* dap_enc_tesla_write_private_key(const tesla_private_key_t* a_private_key, size_t *a_buflen_out) +{ + tesla_param_t p;// = malloc(sizeof(tesla_param_t)); + if(!tesla_params_init(&p, a_private_key->kind)) + return NULL; + + size_t l_buflen = sizeof(size_t) + sizeof(tesla_kind_t) + p.CRYPTO_SECRETKEYBYTES; //CRYPTO_PUBLICKEYBYTES; + uint8_t *l_buf = DAP_NEW_SIZE(uint8_t, l_buflen); + memcpy(l_buf, &l_buflen, sizeof(size_t)); + memcpy(l_buf + sizeof(size_t), &a_private_key->kind, sizeof(tesla_kind_t)); + memcpy(l_buf + sizeof(size_t) + sizeof(tesla_kind_t), a_private_key->data, p.CRYPTO_SECRETKEYBYTES); + if(a_buflen_out) + *a_buflen_out = l_buflen; + return l_buf; +} + +/* Serialize a public key. */ +uint8_t* dap_enc_tesla_write_public_key(const tesla_public_key_t* a_public_key, size_t *a_buflen_out) +{ + tesla_param_t p; + if(!tesla_params_init(&p, a_public_key->kind)) + return NULL; + + size_t l_buflen = sizeof(size_t) + sizeof(tesla_kind_t) + p.CRYPTO_PUBLICKEYBYTES; + uint8_t *l_buf = DAP_NEW_SIZE(uint8_t, l_buflen); + memcpy(l_buf, &l_buflen, sizeof(size_t)); + memcpy(l_buf + sizeof(size_t), &a_public_key->kind, sizeof(tesla_kind_t)); + memcpy(l_buf + sizeof(size_t) + sizeof(tesla_kind_t), a_public_key->data, p.CRYPTO_PUBLICKEYBYTES); + if(a_buflen_out) + *a_buflen_out = l_buflen; + return l_buf; +} + +/* Deserialize a private key. */ +tesla_private_key_t* dap_enc_tesla_read_private_key(const uint8_t *a_buf, size_t a_buflen) +{ + if(!a_buf || a_buflen < (sizeof(size_t) + sizeof(tesla_kind_t))) + return NULL; + tesla_kind_t kind; + size_t l_buflen = 0; + memcpy(&l_buflen, a_buf, sizeof(size_t)); + memcpy(&kind, a_buf + sizeof(size_t), sizeof(tesla_kind_t)); + if(l_buflen != a_buflen) + return NULL; + tesla_param_t p; + if(!tesla_params_init(&p, kind)) + return NULL; + tesla_private_key_t* l_private_key = DAP_NEW(tesla_private_key_t); + l_private_key->kind = kind; + + l_private_key->data = DAP_NEW_SIZE(unsigned char, p.CRYPTO_SECRETKEYBYTES); + memcpy(l_private_key->data, a_buf + sizeof(size_t) + sizeof(tesla_kind_t), p.CRYPTO_SECRETKEYBYTES); + return l_private_key; +} + +/* Deserialize a public key. */ +tesla_public_key_t* dap_enc_tesla_read_public_key(const uint8_t *a_buf, size_t a_buflen) +{ + if(!a_buf || a_buflen < (sizeof(size_t) + sizeof(tesla_kind_t))) + return NULL; + tesla_kind_t kind; + size_t l_buflen = 0; + memcpy(&l_buflen, a_buf, sizeof(size_t)); + memcpy(&kind, a_buf + sizeof(size_t), sizeof(tesla_kind_t)); + if(l_buflen != a_buflen) + return NULL; + tesla_param_t p; + if(!tesla_params_init(&p, kind)) + return NULL; + tesla_public_key_t* l_public_key = DAP_NEW(tesla_public_key_t); + l_public_key->kind = kind; + + l_public_key->data = DAP_NEW_SIZE(unsigned char, p.CRYPTO_PUBLICKEYBYTES); + memcpy(l_public_key->data, a_buf + sizeof(size_t) + sizeof(tesla_kind_t), p.CRYPTO_PUBLICKEYBYTES); + return l_public_key; +}