Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • car/libdap-crypto
1 result
Show changes
Showing
with 5219 additions and 3015 deletions
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <stddef.h>
#include <string.h>
#include "dap_enc_GOST.h"
#include "dap_common.h"
#include "rand/dap_rand.h"
#include "sha3/KeccakHash.h"
#define LOG_TAG "dap_enc_gost"
#define DAP_DEL_Z DAP_DELETE
void dap_enc_gost_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)
{
if(key_size < 32)
{
log_it(L_ERROR, "GOST key cannot be less than 32 bytes.");
}
a_key->last_used_timestamp = time(NULL);
a_key->priv_key_data_size = 32;
a_key->priv_key_data = DAP_NEW_SIZE(uint8_t, key_size);
Keccak_HashInstance Keccak_ctx;
Keccak_HashInitialize(&Keccak_ctx, 1088, 512, 32*8, 0x06);
Keccak_HashUpdate(&Keccak_ctx, kex_buf, kex_size*8);
if(seed_size)
Keccak_HashUpdate(&Keccak_ctx, seed, seed_size*8);
Keccak_HashFinal(&Keccak_ctx, a_key->priv_key_data);
}
void dap_enc_gost_key_delete(struct dap_enc_key *a_key)
{
if(a_key->priv_key_data != NULL)
{
randombytes(a_key->priv_key_data,a_key->priv_key_data_size);
DAP_DELETE(a_key->priv_key_data);
}
a_key->priv_key_data_size = 0;
}
//------GOST_OFB-----------
void dap_enc_gost_ofb_key_new(struct dap_enc_key * a_key)
{
a_key->_inheritor = NULL;
a_key->_inheritor_size = 0;
a_key->type = DAP_ENC_KEY_TYPE_GOST_OFB;
a_key->enc = dap_enc_gost_ofb_encrypt;
a_key->dec = dap_enc_gost_ofb_decrypt;
a_key->enc_na = dap_enc_gost_ofb_encrypt_fast;
a_key->dec_na = dap_enc_gost_ofb_decrypt_fast;
}
size_t dap_enc_gost_ofb_decrypt(struct dap_enc_key *a_key, const void * a_in, size_t a_in_size, void ** a_out)
{
size_t a_out_size = a_in_size - kBlockLen89;
if(a_out_size <= 0) {
log_it(L_ERROR, "gost_ofb decryption ct with iv must be more than kBlockLen89 bytes");
return 0;
}
*a_out = DAP_NEW_SIZE(uint8_t, a_in_size - kBlockLen89);
a_out_size = dap_enc_gost_ofb_decrypt_fast(a_key, a_in, a_in_size, *a_out, a_out_size);
if(a_out_size == 0)
DAP_DEL_Z(*a_out);
return a_out_size;
}
size_t dap_enc_gost_ofb_encrypt(struct dap_enc_key * a_key, const void * a_in, size_t a_in_size, void ** a_out)
{
if(a_in_size <= 0) {
log_it(L_ERROR, "gost ofb encryption pt cannot be 0 bytes");
return 0;
}
size_t a_out_size = a_in_size + kBlockLen89;
*a_out = DAP_NEW_SIZE(uint8_t, a_out_size);
a_out_size = dap_enc_gost_ofb_encrypt_fast(a_key, a_in, a_in_size, *a_out, a_out_size);
if(a_out_size == 0)
DAP_DEL_Z(*a_out);
return a_out_size;
}
size_t dap_enc_gost_ofb_calc_encode_size(const size_t size_in)
{
return size_in + kBlockLen89;
}
size_t dap_enc_gost_ofb_calc_decode_size(const size_t size_in)
{
if(size_in <= kBlockLen89) {
log_it(L_ERROR, "gost_ofb decryption size_in ct with iv must be more than kBlockLen89 bytes");
return 0;
}
return size_in - kBlockLen89;
}
size_t dap_enc_gost_ofb_decrypt_fast(struct dap_enc_key *a_key, const void * a_in,
size_t a_in_size, void * a_out, size_t buf_out_size) {
size_t a_out_size = a_in_size - kBlockLen89;
if(a_out_size > buf_out_size) {
log_it(L_ERROR, "gost_ofb fast_decryption too small buf_out_size");
return 0;
}
uint8_t iv[kBlockLen89];
memcpy(iv, a_in, kBlockLen89);
unsigned char ctx[kOfb89ContextLen];
if(init_ofb_89(a_key->priv_key_data, ctx, kBlockLen89, iv, kBlockLen89,NULL, NULL))//, print_array, print_uint_array))
{
return 0;
}
if(crypt_ofb(ctx, a_in + kBlockLen89, a_out, a_in_size - kBlockLen89))
{
return 0;
}
free_ofb(ctx);
return a_out_size;
}
size_t dap_enc_gost_ofb_encrypt_fast(struct dap_enc_key * a_key, const void * a_in, size_t a_in_size, void * a_out,size_t buf_out_size)
{
//generate iv and put it in *a_out first bytes
size_t a_out_size = a_in_size + kBlockLen89;
if(a_out_size > buf_out_size) {
log_it(L_ERROR, "gost_ofb fast_encryption too small buf_out_size");
return 0;
}
uint8_t iv[kBlockLen89];
if(randombytes(iv, kBlockLen89) == 1)
{
log_it(L_ERROR, "failed to get kBlockLen89 bytes iv gost ofb");
return 0;
}
memcpy(a_out, iv, kBlockLen89);
unsigned char ctx[kOfb89ContextLen];
if(init_ofb_89(a_key->priv_key_data, ctx, kBlockLen89, iv, kBlockLen89,NULL, NULL))//, print_array, print_uint_array))
{
return 0;
}
if(crypt_ofb(ctx, a_in, a_out + kBlockLen89, a_in_size))
{
return 0;
}
free_ofb(ctx);
return a_out_size;
}
//------KUZN_OFB-----------
void dap_enc_kuzn_ofb_key_new(struct dap_enc_key * a_key)
{
a_key->_inheritor = NULL;
a_key->_inheritor_size = 0;
a_key->type = DAP_ENC_KEY_TYPE_GOST_OFB;
a_key->enc = dap_enc_kuzn_ofb_encrypt;
a_key->dec = dap_enc_kuzn_ofb_decrypt;
a_key->enc_na = dap_enc_kuzn_ofb_encrypt_fast;
a_key->dec_na = dap_enc_kuzn_ofb_decrypt_fast;
}
size_t dap_enc_kuzn_ofb_calc_encode_size(const size_t size_in)
{
return size_in + kBlockLen14;
}
size_t dap_enc_kuzn_ofb_calc_decode_size(const size_t size_in)
{
if(size_in <= kBlockLen14) {
log_it(L_ERROR, "gost_ofb decryption size_in ct with iv must be more than kBlockLen14 bytes");
return 0;
}
return size_in - kBlockLen14;
}
size_t dap_enc_kuzn_ofb_encrypt_fast(struct dap_enc_key * a_key, const void * a_in, size_t a_in_size, void * a_out,size_t buf_out_size)
{
//generate iv and put it in *a_out first bytes
size_t a_out_size = a_in_size + kBlockLen14;
if(a_in_size <= 0) {
log_it(L_ERROR, "kuzn_ofb fast_encryption too small a_in_size");
return 0;
}
if(a_out_size > buf_out_size) {
log_it(L_ERROR, "kuzn_ofb fast_encryption too small buf_out_size");
return 0;
}
if(randombytes(a_out, kBlockLen14) == 1)//iv
{
log_it(L_ERROR, "failed to get kBlockLen14 bytes iv gost ofb");
return 0;
}
unsigned char ctx[kOfb14ContextLen];
if(init_ofb_14(a_key->priv_key_data, ctx, kBlockLen14, a_out, kBlockLen14, NULL,NULL))
return -1;
if(crypt_ofb(ctx, a_in, a_out + kBlockLen14, a_in_size))
return -1;
free_ofb(ctx);
return a_out_size;
}
size_t dap_enc_kuzn_ofb_decrypt_fast(struct dap_enc_key *a_key, const void * a_in,
size_t a_in_size, void * a_out, size_t buf_out_size)
{
size_t a_out_size = a_in_size - kBlockLen14;
if(a_out_size <= 0) {
log_it(L_ERROR, "kuzn_ofb fast_decryption too small a_in_size");
return 0;
}
if(a_out_size > buf_out_size) {
log_it(L_ERROR, "kuzn_ofb fast_decryption too small buf_out_size");
return 0;
}
unsigned char ctx[kOfb14ContextLen];
//iv first kBlockLen14 a_in bytes
if(init_ofb_14(a_key->priv_key_data, ctx, kBlockLen14, a_in, kBlockLen14, NULL, NULL))
return -1;
if(decrypt_ofb(ctx, a_in + kBlockLen14, a_out, a_out_size))
return -1;
free_ofb(ctx);
return a_out_size;
}
size_t dap_enc_kuzn_ofb_decrypt(struct dap_enc_key *a_key, const void * a_in,
size_t a_in_size, void ** a_out) {
size_t a_out_size = a_in_size - kBlockLen14;
if(a_out_size <= 0) {
log_it(L_ERROR, "kuzn_ofb decryption too small a_in_size");
return 0;
}
*a_out = DAP_NEW_SIZE(uint8_t, a_out_size);
a_out_size = dap_enc_kuzn_ofb_decrypt_fast(a_key, a_in, a_in_size, *a_out, a_out_size);
if(!a_out_size)
DAP_DEL_Z(*a_out);
return a_out_size;
}
size_t dap_enc_kuzn_ofb_encrypt(struct dap_enc_key * a_key, const void * a_in, size_t a_in_size, void ** a_out)
{
//generate iv and put it in *a_out first bytes
if(a_in_size <= 0) {
log_it(L_ERROR, "kuzn fast_encryption too small a_in_size");
return 0;
}
size_t a_out_size = a_in_size + kBlockLen14;
*a_out = DAP_NEW_SIZE(uint8_t, a_out_size);
a_out_size = dap_enc_kuzn_ofb_encrypt_fast(a_key, a_in, a_in_size, *a_out, a_out_size);
if(!a_out_size)
DAP_DEL_Z(*a_out);
return a_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;
}
/*
* 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;
}
#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 );
}
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
......@@ -42,12 +42,14 @@ int dap_chain_str_to_hash_fast( const char * a_hash_str, dap_chain_hash_fast_t *
const size_t c_hash_str_size = sizeof(*a_hash) * 2 + 1 /*trailing zero*/+ 2 /* heading 0x */;
size_t l_hash_str_len = strlen( a_hash_str);
if ( l_hash_str_len + 1 == c_hash_str_size ){
for (size_t l_offset = 2; l_offset < l_hash_str_len; l_offset+=2 ){
if ( ( sscanf(a_hash_str+l_offset,"%02hhx",a_hash->raw+l_offset/2-1) != 1) ||
( sscanf(a_hash_str+l_offset,"%02hhX",a_hash->raw+l_offset/2-1) != 1)
)
log_it(L_ERROR,"dap_chain_str_to_hash_fast parse error: offset=%u, hash_str_len=%u, str=\"%2s\"",l_offset, l_hash_str_len, a_hash_str+l_offset);
return -10* ((int) l_offset); // Wrong char
for(size_t l_offset = 2; l_offset < l_hash_str_len; l_offset += 2) {
if(sscanf(a_hash_str + l_offset, "%02hhx", a_hash->raw + l_offset / 2 - 1) != 1) {
if(sscanf(a_hash_str + l_offset, "%02hhX", a_hash->raw + l_offset / 2 - 1) != 1) {
log_it(L_ERROR, "dap_chain_str_to_hash_fast parse error: offset=%u, hash_str_len=%u, str=\"%2s\"",
l_offset, l_hash_str_len, a_hash_str + l_offset);
return -10 * ((int) l_offset); // Wrong char
}
}
}
return 0;
}else // Wromg string len
......
This diff is collapsed.
......@@ -6,6 +6,10 @@
#define IAES_BLOCK_SIZE 16
#define IAES_KEYSIZE 32
void AES256_enc_cernelT(uint32_t * in, uint32_t * out, uint32_t * masterkey);
void AES256_dec_cernelT(uint32_t * in, uint32_t * out, uint32_t * decr_key);
void swap_endian(uint32_t *buff, unsigned long len);
void Key_Shedule_for_decrypT(uint32_t * key, uint32_t * rounds_keys);
void IAES_256_CBC_encrypt(const uint8_t *data, uint8_t *output, uint8_t * ivec, unsigned long length, uint8_t *masterkey);
size_t IAES_256_CBC_decrypt(const uint8_t *cdata, uint8_t *output, uint8_t * ivec, unsigned long length, uint8_t *key);
......
......@@ -7,26 +7,12 @@
size_t iaes_calc_block128_size(size_t length_data)
{
if(length_data < IAES_BLOCK_SIZE) {
return IAES_BLOCK_SIZE;
} else if((length_data % IAES_BLOCK_SIZE) == 0) {
return length_data;
}
size_t padding = 16 - length_data % 16;
return length_data + padding;
size_t new_length_data = length_data + 1;
size_t padding = IAES_BLOCK_SIZE - new_length_data % IAES_BLOCK_SIZE;
new_length_data += padding;
return new_length_data;
}
size_t iaes_block128_padding(const void *data, uint8_t **data_new, size_t length_data)
{
size_t length_data_new = iaes_calc_block128_size(length_data);
*data_new = (uint8_t *) malloc(length_data_new);
memcpy(*data_new, data, length_data);
memset(*data_new + length_data, 0x0, length_data_new - length_data);
return length_data_new;
}
void swap_endian(uint32_t *buff, unsigned long len)
{
......@@ -214,7 +200,7 @@ void IAES_256_CBC_encrypt(const uint8_t *data, uint8_t *cdata, uint8_t *ivec, un
AES256_enc_cernelT(((uint32_t *)cdata + count_block * block_in32_size), feedback, (uint32_t *)masterkey);
memcpy ((uint32_t *)cdata + count_block * block_in32_size, &feedback[0], IAES_BLOCK_SIZE);
}
}
swap_endian((uint32_t *)masterkey,IAES_KEYSIZE/sizeof(uint32_t));
}
......@@ -487,19 +473,7 @@ size_t IAES_256_CBC_decrypt(const uint8_t *cdata, uint8_t *data, uint8_t *ivec,
memcpy(&feedback[0], (uint32_t *)cdata + count_block*block_in32_size, IAES_BLOCK_SIZE);
}
swap_endian((uint32_t *)masterkey, IAES_KEYSIZE/sizeof(uint32_t));
size_t i, padding = 0;
size_t end = length > 16 ? length - 16 : 0;
if(length>0)
for(i = length-1; i >= end; i--)
{
if(data[i] == 0)
padding++;
else
break;
}
return length - padding;
return length;
}
This diff is collapsed.