From: miha-q <> Date: Sat, 27 Jan 2024 03:53:38 +0000 (-0500) Subject: Fri Jan 26 10:53:38 PM EST 2024 X-Git-Url: http://www.foleosoft.com/?a=commitdiff_plain;h=154a3496c1154e312aeacc7d62ec7b5227bd9252;p=CryptoFoleo.git Fri Jan 26 10:53:38 PM EST 2024 --- diff --git a/bin/CryptoFoleo.h b/bin/CryptoFoleo.h index 68e54e6..d799ed0 100644 --- a/bin/CryptoFoleo.h +++ b/bin/CryptoFoleo.h @@ -53,4 +53,8 @@ uint8_t* foleo_hmac_prf(uint8_t, uint32_t, uint8_t*, uint32_t, uint8_t*, uint32_ uint8_t foleo_hash_size(uint8_t); void foleo_rand_mode(uint8_t, uint8_t*); + +uint8_t* foleo_base64_decode(uint8_t*, size_t*); +uint8_t* foleo_hex_decode(uint8_t*); + #endif diff --git a/bin/libCryptoFoleo.so b/bin/libCryptoFoleo.so index 3276ad2..e85e90c 100755 Binary files a/bin/libCryptoFoleo.so and b/bin/libCryptoFoleo.so differ diff --git a/bin/prv.key b/bin/prv.key deleted file mode 100644 index bffcf44..0000000 --- a/bin/prv.key +++ /dev/null @@ -1,28 +0,0 @@ -+-------[RSA Private Key]--------+ -|#.[/[@$%#,\~%=@<[<(^,(&$:~'}<}``| -|%$>(;&&%'($;')`#e~"},<<)@),^}%"<| -|.%(?`]._*?@!e/@[*:)]/ex*},>~[s/(| -|#;}#`*{=";[\,(=>.${x,:>!x(\"=]=&| -|"%){"&x#>~}e]{^~\s'`'}>"| -|$^]~%!`^~".\@<<{,@#<&;x")}:]{'^~| -|`!@).{%.{}>e!/s:>`_es^?=(/}<]_*=| -|]{@(s>.&\!`"$x@!{]>==s`.`$@{@`/<| -|^??^/}^*['_<:@(^?]/:`~[[={*[_@#]| -|::]}*@!)?}/`]=`/e#\';#(}[!,&:"`>!{'x!*./!,x;_%{^<]:<)$""^| -|;x[[^&!?e?=__']s~.,`,$&!?`:)/{\}!&!\{/| -|`#:`)x%)"e`?,[s.)e=>>^`(;,$}'(<:| -|%."{(=_,%^!ex,$,_x>*`%^,ex;]x_$;| -|!.!.#.x]e/}*~;<,s/{~?e`@}!~$)e_.| -|;,s),s@.{$ex{@=;];:{/(}$^.%(&;%\x!{}<,x!{| -|!<={'/}x?^'.s&;$(*~*")ses`},!:;*| -|e;s&~.:`;xx&_,}[};x@e=){]s&%'[_:| -|%!!\_%{(!`(;^/_:<^?@,=@%)>,/$)@,_| -|[.~!<\!<&.`%}^=s/?@_x=":'{~[_={)| -|\*$~~?.#};"`\[)[\[~;s | -+--------------------------------+ diff --git a/bin/pub.key b/bin/pub.key deleted file mode 100644 index 6b608b5..0000000 --- a/bin/pub.key +++ /dev/null @@ -1,15 +0,0 @@ -+--------[RSA Public Key]--------+ -|(^:/# $`,^_=^`s\e#^e,>.,`,$&!?`:| -|)/{\}!&!\{/`#:`)x%)"e`?,[s.)e=>>| -|^`(;,$}'(<:%."{(=_,%^!ex,$,_x>*`| -|%^,ex;]x_$;!.!.#.x]e/}*~;<,s/{~?| -|e`@}!~$)e_.;,s),s@.{$ex{@=;];:{/(}$^.%(&;| -|%\x!{}<,x!{!<={'/}x?^'.s&;$(*~*"| -|)ses`},!:;*e;s&~.:`;xx&_,}[};x@e| -|=){]s&%'[_:%!!\_%{(!`(;^/_:<^?@,| -|=@%)>,/$)@,_[.~!<\!<&.`%}^=s/?@_x| -|=":'{~[_={)\*$~~?.#};"`\[)[\[~;s| -+--------------------------------+ diff --git a/src/all.c b/src/all.c index 7085fac..0dd9c7d 100644 --- a/src/all.c +++ b/src/all.c @@ -7,3 +7,4 @@ #include "rsa.c" #include "sha256.c" #include "rand.c" +#include "encodings.c" \ No newline at end of file diff --git a/src/encodings.c b/src/encodings.c new file mode 100644 index 0000000..d6833e2 --- /dev/null +++ b/src/encodings.c @@ -0,0 +1,167 @@ +#ifndef __FOLEO_ENCODINGS__ +#define __FOLEO_ENCODINGS__ + +static uint8_t foleo_base64_decode1(uint8_t v) +{ + switch (v) + { + case 'A': return 0; + case 'B': return 1; + case 'C': return 2; + case 'D': return 3; + case 'E': return 4; + case 'F': return 5; + case 'G': return 6; + case 'H': return 7; + case 'I': return 8; + case 'J': return 9; + case 'K': return 10; + case 'L': return 11; + case 'M': return 12; + case 'N': return 13; + case 'O': return 14; + case 'P': return 15; + case 'Q': return 16; + case 'R': return 17; + case 'S': return 18; + case 'T': return 19; + case 'U': return 20; + case 'V': return 21; + case 'W': return 22; + case 'X': return 23; + case 'Y': return 24; + case 'Z': return 25; + case 'a': return 26; + case 'b': return 27; + case 'c': return 28; + case 'd': return 29; + case 'e': return 30; + case 'f': return 31; + case 'g': return 32; + case 'h': return 33; + case 'i': return 34; + case 'j': return 35; + case 'k': return 36; + case 'l': return 37; + case 'm': return 38; + case 'n': return 39; + case 'o': return 40; + case 'p': return 41; + case 'q': return 42; + case 'r': return 43; + case 's': return 44; + case 't': return 45; + case 'u': return 46; + case 'v': return 47; + case 'w': return 48; + case 'x': return 49; + case 'y': return 50; + case 'z': return 51; + case '0': return 52; + case '1': return 53; + case '2': return 54; + case '3': return 55; + case '4': return 56; + case '5': return 57; + case '6': return 58; + case '7': return 59; + case '8': return 60; + case '9': return 61; + case '+': return 62; + case '/': return 63; + } + return 255; +} + +uint8_t* foleo_base64_decode(uint8_t* b64, size_t *r) +{ + uint32_t size = strlen(b64); + uint8_t* ret = malloc(0); + uint32_t retS = 0; + uint16_t buffer = 0; + uint8_t bufferS = 0; + for (uint32_t i = 0; i < size; i++) + { + if (b64[i] == '=') { bufferS = 0; continue; }; + uint8_t val = foleo_base64_decode1(b64[i]); + if (val == 255) + { + *r = 0; + return NULL; + } + //printf("Pre : %04x %i\n", buffer, bufferS); + buffer = (buffer << 6) | val; + bufferS += 6; + if (bufferS >= 8) + { + uint8_t shift = 16 - bufferS; + buffer <<= shift; + //printf("Shift: %04x %i\n", buffer, bufferS); + ret = realloc(ret, ++retS); + ret[retS - 1] = buffer >> 8; + buffer = buffer & 0x00FF; + buffer >>= shift; + bufferS -= 8; + } + //printf("Post : %04x %i\n", buffer, bufferS); + //printf("--------------------------------\n"); + } + if (bufferS > 0) + { + buffer <<= 16 - bufferS; + ret = realloc(ret, ++retS); + ret[retS - 1] = buffer >> 8; + } + *r = retS; + return ret; +} + +static uint8_t foleo_hex_decode1(uint8_t v) +{ + switch (v) + { + case '0': return 0; + case '1': return 1; + case '2': return 2; + case '3': return 3; + case '4': return 4; + case '5': return 5; + case '6': return 6; + case '7': return 7; + case '8': return 8; + case '9': return 9; + case 'A': return 10; + case 'B': return 11; + case 'C': return 12; + case 'D': return 13; + case 'E': return 14; + case 'F': return 15; + case 'a': return 10; + case 'b': return 11; + case 'c': return 12; + case 'd': return 13; + case 'e': return 14; + case 'f': return 15; + } + return 255; +} + +uint8_t* foleo_hex_decode(uint8_t* hex) +{ + uint32_t size = strlen(hex); + uint8_t* ret = malloc(size / 2); + for (uint32_t i = 0; i < size; i += 2) + { + uint8_t upper = foleo_hex_decode1(hex[i]); + uint8_t lower = foleo_hex_decode1(hex[i + 1]); + if (upper == 255 || lower == 255) + { + free(ret); + return NULL; + } + ret[i / 2] = (upper << 4) | lower; + } + return ret; +} + +#endif \ No newline at end of file diff --git a/src/headers.h b/src/headers.h index 68e54e6..d799ed0 100644 --- a/src/headers.h +++ b/src/headers.h @@ -53,4 +53,8 @@ uint8_t* foleo_hmac_prf(uint8_t, uint32_t, uint8_t*, uint32_t, uint8_t*, uint32_ uint8_t foleo_hash_size(uint8_t); void foleo_rand_mode(uint8_t, uint8_t*); + +uint8_t* foleo_base64_decode(uint8_t*, size_t*); +uint8_t* foleo_hex_decode(uint8_t*); + #endif diff --git a/src/rsa.c b/src/rsa.c index e8e3154..187c44d 100644 --- a/src/rsa.c +++ b/src/rsa.c @@ -1,5 +1,7 @@ #ifndef __FOLEO_RSA__ #define __FOLEO_RSA__ +//#define __FOLEO_MAP_PEM__ +#define __FOLEO_MAP_PTY__ #include #include #include @@ -33,6 +35,7 @@ static uint8_t foleo_rsa_prettymap(uint8_t in, uint8_t dir) { switch (in) { + #ifdef __FOLEO_MAP_PTY__ case '_': return 0; case '!': return 1; case '@': return 2; @@ -65,6 +68,73 @@ static uint8_t foleo_rsa_prettymap(uint8_t in, uint8_t dir) case '<': return 29; case '>': return 30; case '?': return 31; + #endif + #ifdef __FOLEO_MAP_PEM__ + case 'A': return 0; + case 'B': return 1; + case 'C': return 2; + case 'D': return 3; + case 'E': return 4; + case 'F': return 5; + case 'G': return 6; + case 'H': return 7; + case 'I': return 8; + case 'J': return 9; + case 'K': return 10; + case 'L': return 11; + case 'M': return 12; + case 'N': return 13; + case 'O': return 14; + case 'P': return 15; + case 'Q': return 16; + case 'R': return 17; + case 'S': return 18; + case 'T': return 19; + case 'U': return 20; + case 'V': return 21; + case 'W': return 22; + case 'X': return 23; + case 'Y': return 24; + case 'Z': return 25; + case 'a': return 26; + case 'b': return 27; + case 'c': return 28; + case 'd': return 29; + case 'e': return 30; + case 'f': return 31; + case 'g': return 32; + case 'h': return 33; + case 'i': return 34; + case 'j': return 35; + case 'k': return 36; + case 'l': return 37; + case 'm': return 38; + case 'n': return 39; + case 'o': return 40; + case 'p': return 41; + case 'q': return 42; + case 'r': return 43; + case 's': return 44; + case 't': return 45; + case 'u': return 46; + case 'v': return 47; + case 'w': return 48; + case 'x': return 49; + case 'y': return 50; + case 'z': return 51; + case '0': return 52; + case '1': return 53; + case '2': return 54; + case '3': return 55; + case '4': return 56; + case '5': return 57; + case '6': return 58; + case '7': return 59; + case '8': return 60; + case '9': return 61; + case '+': return 62; + case '/': return 63; + #endif } return 0xFF; } @@ -72,6 +142,7 @@ static uint8_t foleo_rsa_prettymap(uint8_t in, uint8_t dir) { switch (in) { + #ifdef __FOLEO_MAP_PTY__ case 0: return '_'; case 1: return '!'; case 2: return '@'; @@ -104,6 +175,73 @@ static uint8_t foleo_rsa_prettymap(uint8_t in, uint8_t dir) case 29: return '<'; case 30: return '>'; case 31: return '?'; + #endif + #ifdef __FOLEO_MAP_PEM__ + case 0: return 'A'; + case 1: return 'B'; + case 2: return 'C'; + case 3: return 'D'; + case 4: return 'E'; + case 5: return 'F'; + case 6: return 'G'; + case 7: return 'H'; + case 8: return 'I'; + case 9: return 'J'; + case 10: return 'K'; + case 11: return 'L'; + case 12: return 'M'; + case 13: return 'N'; + case 14: return 'O'; + case 15: return 'P'; + case 16: return 'Q'; + case 17: return 'R'; + case 18: return 'S'; + case 19: return 'T'; + case 20: return 'U'; + case 21: return 'V'; + case 22: return 'W'; + case 23: return 'X'; + case 24: return 'Y'; + case 25: return 'Z'; + case 26: return 'a'; + case 27: return 'b'; + case 28: return 'c'; + case 29: return 'd'; + case 30: return 'e'; + case 31: return 'f'; + case 32: return 'g'; + case 33: return 'h'; + case 34: return 'i'; + case 35: return 'j'; + case 36: return 'k'; + case 37: return 'l'; + case 38: return 'm'; + case 39: return 'n'; + case 40: return 'o'; + case 41: return 'p'; + case 42: return 'q'; + case 43: return 'r'; + case 44: return 's'; + case 45: return 't'; + case 46: return 'u'; + case 47: return 'v'; + case 48: return 'w'; + case 49: return 'x'; + case 50: return 'y'; + case 51: return 'z'; + case 52: return '0'; + case 53: return '1'; + case 54: return '2'; + case 55: return '3'; + case 56: return '4'; + case 57: return '5'; + case 58: return '6'; + case 59: return '7'; + case 60: return '8'; + case 61: return '9'; + case 62: return '+'; + case 63: return '/'; + #endif } } } @@ -170,6 +308,7 @@ void foleo_rsa_import(rsakey_t* k, uint8_t* buf) uint8_t* foleo_rsa_export(rsakey_t* k) { + #ifdef __FOLEO_MAP_PTY__ const uint8_t width = 32; mpz_t n, t; mpz_init(n); @@ -321,6 +460,68 @@ uint8_t* foleo_rsa_export(rsakey_t* k) keyascii[keyascii_i++] = '\n'; keyascii[keyascii_i++] = 0; return keyascii; + #endif + + #ifdef __FOLEO_MAP_PEM__ + mpz_t n, t; + mpz_init(n); + mpz_init(t); + uint8_t* kn = malloc(0); + uint16_t knS = 0; + + mpz_set(n, k->k); + for (uint8_t i = 0; i < 4; i++) + { + mpz_mod_ui(t, n, 64); + kn = realloc(kn, knS + 1); + kn[knS++] = foleo_rsa_prettymap(mpz_get_ui(t), 0); + mpz_div_ui(n, n, 64); + } + mpz_set(n, k->n); + for (uint16_t i = 0; i < (k->bitWidth / 6); i++) + { + mpz_mod_ui(t, n, 64); + kn = realloc(kn, knS + 1); + kn[knS++] = foleo_rsa_prettymap(mpz_get_ui(t), 0); + mpz_div_ui(n, n, 64); + } + + uint8_t* ret = malloc(0); + uint16_t retS = 0; + for (uint8_t i = 0; i < 5; i++) + { ret = realloc(ret, retS + 1); ret[retS++] = '-'; } + ret = realloc(ret, retS + 1); ret[retS++] = 'B'; + ret = realloc(ret, retS + 1); ret[retS++] = 'E'; + ret = realloc(ret, retS + 1); ret[retS++] = 'G'; + ret = realloc(ret, retS + 1); ret[retS++] = 'I'; + ret = realloc(ret, retS + 1); ret[retS++] = 'N'; + ret = realloc(ret, retS + 1); ret[retS++] = ' '; + for (uint16_t i = 0; i < strlen(k->label); i++) + { ret = realloc(ret, retS + 1); ret[retS++] = k->label[i]; } + for (uint8_t i = 0; i < 5; i++) + { ret = realloc(ret, retS + 1); ret[retS++] = '-'; } + ret = realloc(ret, retS + 1); ret[retS++] = '\n'; + for (uint16_t i = 0; i < knS; i++) + { + if (i > 0 && i % 64 == 0) + { ret = realloc(ret, retS + 1); ret[retS++] = '\n'; } + ret = realloc(ret, retS + 1); ret[retS++] = kn[knS - i - 1]; + } + ret = realloc(ret, retS + 1); ret[retS++] = '\n'; + for (uint8_t i = 0; i < 5; i++) + { ret = realloc(ret, retS + 1); ret[retS++] = '-'; } + ret = realloc(ret, retS + 1); ret[retS++] = 'E'; + ret = realloc(ret, retS + 1); ret[retS++] = 'N'; + ret = realloc(ret, retS + 1); ret[retS++] = 'D'; + ret = realloc(ret, retS + 1); ret[retS++] = ' '; + for (uint16_t i = 0; i < strlen(k->label); i++) + { ret = realloc(ret, retS + 1); ret[retS++] = k->label[i]; } + for (uint8_t i = 0; i < 5; i++) + { ret = realloc(ret, retS + 1); ret[retS++] = '-'; } + ret = realloc(ret, retS + 1); ret[retS] = 0; + return ret; + + #endif } void foleo_rsa_keygen(uint16_t bitWidth, rsakey_t* public, rsakey_t* private) @@ -345,6 +546,7 @@ void foleo_rsa_keygen(uint16_t bitWidth, rsakey_t* public, rsakey_t* private) foleo_rsa_load(np, p, bitWidth / 16); foleo_rsa_load(nq, q, bitWidth / 16); foleo_rsa_load(public->k, e, 3); + mpz_set_ui(public->k, 65537); public->bitWidth = bitWidth; private->bitWidth = bitWidth; mpz_mul(public->n, np, nq); @@ -360,6 +562,7 @@ void foleo_rsa_keygen(uint16_t bitWidth, rsakey_t* public, rsakey_t* private) free(q); free(e); + #ifdef __FOLEO_MAP_PTY__ public->label = malloc(15); public->label[ 0] = 'R'; public->label[ 1] = 'S'; @@ -376,7 +579,23 @@ void foleo_rsa_keygen(uint16_t bitWidth, rsakey_t* public, rsakey_t* private) public->label[12] = 'e'; public->label[13] = 'y'; public->label[14] = 0; - + #endif + #ifdef __FOLEO_MAP_PEM__ + public->label = malloc(11); + public->label[ 0] = 'P'; + public->label[ 1] = 'U'; + public->label[ 2] = 'B'; + public->label[ 3] = 'L'; + public->label[ 4] = 'I'; + public->label[ 5] = 'C'; + public->label[ 6] = ' '; + public->label[ 7] = 'K'; + public->label[ 8] = 'E'; + public->label[ 9] = 'Y'; + public->label[10] = 0; + #endif + + #ifdef __FOLEO_MAP_PTY__ private->label = malloc(16); private->label[ 0] = 'R'; private->label[ 1] = 'S'; @@ -394,6 +613,22 @@ void foleo_rsa_keygen(uint16_t bitWidth, rsakey_t* public, rsakey_t* private) private->label[13] = 'e'; private->label[14] = 'y'; private->label[15] = 0; + #endif + #ifdef __FOLEO_MAP_PEM__ + private->label = malloc(12); + private->label[ 0] = 'P'; + private->label[ 1] = 'R'; + private->label[ 2] = 'I'; + private->label[ 3] = 'V'; + private->label[ 4] = 'A'; + private->label[ 5] = 'T'; + private->label[ 6] = 'E'; + private->label[ 7] = ' '; + private->label[ 8] = 'K'; + private->label[ 9] = 'E'; + private->label[10] = 'Y'; + private->label[11] = 0; + #endif }