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
+++ /dev/null
-+-------[RSA Private Key]--------+
-|#.[/[@$%#,\~%=@<[<(^,(&$:~'}<}``|
-|%$>(;&&%'($;')`#e~"},<<)@),^}%"<|
-|.%(?`]._*?@!e/@[*:)]/ex*},>~[s/(|
-|#;}#`*{=";[\,(=>.${x,:>!x(\"=]=&|
-|"%){"&x#>~}e]{^~\s'`'}><xx];s!@}|
-|<;_#,xx#?@#(}@\#`#:$!e}(:\#~#@>"|
-|$^]~%!`^~".\@<<{,@#<&;x")}:]{'^~|
-|`!@).{%.{}>e!/s:>`_es^?=(/}<]_*=|
-|]{@(s>.&\!`"$x@!{]>==s`.`$@{@`/<|
-|^??^/}^*['_<:@(^?]/:`~[[={*[_@#]|
-|::]}*@!)?}/`]=`/e#\';#(}[!,&<!e_|
-|\}>:"`>!{'x!*./!,x;_%{^<]:<)$""^|
-|;x[[^&!?e?=__']s~<?x*\#)_s $`,^_|
-|=^`s\e#^e,>.,`,$&!?`:)/{\}!&!\{/|
-|`#:`)x%)"e`?,[s.)e=>>^`(;,$}'(<:|
-|%."{(=_,%^!ex,$,_x>*`%^,ex;]x_$;|
-|!.!.#.x]e/}*~;<,s/{~?e`@}!~$)e_.|
-|;,s),s@.{$ex{@=<s?\_/]/=*(\`'%=`|
-|!/~?][](.=x="'&^{s`(,?$}))&;)(x;|
-|{[(;`>;];:{/(}$^.%(&;%\x!{}<,x!{|
-|!<={'/}x?^'.s&;$(*~*")ses`},!:;*|
-|e;s&~.:`;xx&_,}[};x@e=){]s&%'[_:|
-|%!!\_%{(!`(;^/_:<^?@,=@%)>,/$)<s|
-|}`!#*~/{`'*"/[?$_]"?#?:s'}*=>@,_|
-|[.~!<\!<&.`%}^=s/?@_x=":'{~[_={)|
-|\*$~~?.#};"`\[)[\[~;s |
-+--------------------------------+
+++ /dev/null
-+--------[RSA Public Key]--------+
-|(^:/# $`,^_=^`s\e#^e,>.,`,$&!?`:|
-|)/{\}!&!\{/`#:`)x%)"e`?,[s.)e=>>|
-|^`(;,$}'(<:%."{(=_,%^!ex,$,_x>*`|
-|%^,ex;]x_$;!.!.#.x]e/}*~;<,s/{~?|
-|e`@}!~$)e_.;,s),s@.{$ex{@=<s?\_/|
-|]/=*(\`'%=`!/~?][](.=x="'&^{s`(,|
-|?$}))&;)(x;{[(;`>;];:{/(}$^.%(&;|
-|%\x!{}<,x!{!<={'/}x?^'.s&;$(*~*"|
-|)ses`},!:;*e;s&~.:`;xx&_,}[};x@e|
-|=){]s&%'[_:%!!\_%{(!`(;^/_:<^?@,|
-|=@%)>,/$)<s}`!#*~/{`'*"/[?$_]"?#|
-|?:s'}*=>@,_[.~!<\!<&.`%}^=s/?@_x|
-|=":'{~[_={)\*$~~?.#};"`\[)[\[~;s|
-+--------------------------------+
#include "rsa.c"
#include "sha256.c"
#include "rand.c"
+#include "encodings.c"
\ No newline at end of file
--- /dev/null
+#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
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
#ifndef __FOLEO_RSA__
#define __FOLEO_RSA__
+//#define __FOLEO_MAP_PEM__
+#define __FOLEO_MAP_PTY__
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
{
switch (in)
{
+ #ifdef __FOLEO_MAP_PTY__
case '_': return 0;
case '!': return 1;
case '@': return 2;
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;
}
{
switch (in)
{
+ #ifdef __FOLEO_MAP_PTY__
case 0: return '_';
case 1: return '!';
case 2: return '@';
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
}
}
}
uint8_t* foleo_rsa_export(rsakey_t* k)
{
+ #ifdef __FOLEO_MAP_PTY__
const uint8_t width = 32;
mpz_t n, t;
mpz_init(n);
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)
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);
free(q);
free(e);
+ #ifdef __FOLEO_MAP_PTY__
public->label = malloc(15);
public->label[ 0] = 'R';
public->label[ 1] = 'S';
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';
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
}