+uint8_t pre_alloc_key_crt(void){
+ priv_key.n = 5;
+ priv_key.components = malloc(5 * sizeof(bigint_t));
+ if(!priv_key.components){
+ cli_putstr_P(PSTR("\r\nERROR: OOM!"));
+ return 2;
+ }
+ return 0;
+}
+
+void free_key(void){
+ uint8_t c;
+ free(pub_key.modulus.wordv);
+ free(pub_key.exponent.wordv);
+ for(c = 0; c < priv_key.n; ++c){
+ free(priv_key.components[c].wordv);
+ }
+ free(priv_key.components);
+ keys_allocated = 0;
+}
+
+uint8_t read_key_crt(void){
+ uint8_t r;
+ cli_putstr_P(PSTR("\r\n== reading key (crt) =="));
+ r = pre_alloc_key_crt();
+ if(r) return r;
+ r = read_bigint(&pub_key.modulus,"\r\n = module =");
+ if(r) return r;
+ memcpy(&priv_key.modulus, &pub_key.modulus, sizeof(bigint_t));
+ r = read_bigint(&pub_key.exponent,"\r\n = public exponent =");
+ if(r) return r;
+ r = read_bigint(&(priv_key.components[0]),"\r\n = p (first prime) =");
+ if(r) return r;
+ r = read_bigint(&(priv_key.components[1]),"\r\n = q (second prime) =");
+ if(r) return r;
+ r = read_bigint(&(priv_key.components[2]),"\r\n = dp (p's exponent) =");
+ if(r) return r;
+ r = read_bigint(&(priv_key.components[3]),"\r\n = dq (q's exponent) =");
+ if(r) return r;
+ r = read_bigint(&(priv_key.components[4]),"\r\n = qInv (q' coefficient) =");
+ return r;
+}
+
+uint8_t read_key_conv(void){
+ uint8_t r;
+ priv_key.components = malloc(sizeof(bigint_t));
+ if(!priv_key.components){
+ cli_putstr_P(PSTR("\r\nERROR: OOM!"));
+ return 2;
+ }
+ cli_putstr_P(PSTR("\r\n== reading key (conv) =="));
+ r = read_bigint(&pub_key.modulus,"\r\n = module =");
+ if(r) return r;
+ memcpy(&priv_key.modulus, &pub_key.modulus, sizeof(bigint_t));
+ priv_key.n = 1;
+ r = read_bigint(&pub_key.exponent,"\r\n = public exponent =");
+ if(r) return r;
+ r = read_bigint(priv_key.components,"\r\n = private exponent =");
+ return r;
+}
+
+uint8_t load_bigint_from_os(bigint_t *a, PGM_VOID_P os, uint16_t length_B){
+ a->length_W = BIGINT_CEIL(length_B) / sizeof(bigint_word_t);
+ a->wordv = malloc(BIGINT_CEIL(length_B));
+ if(!a->wordv){
+ cli_putstr_P(PSTR("\r\nOOM!\r\n"));
+ return 1;
+ }
+ memset(a->wordv, 0, sizeof(bigint_word_t));
+ memcpy_P((uint8_t*)a->wordv + BIGINT_OFF(length_B), os, length_B);
+ a->info = 0;
+ bigint_changeendianess(a);
+ bigint_adjust(a);
+ return 0;
+}
+
+void load_fix_rsa(void){
+ if(keys_allocated){
+ free_key();
+ }
+ keys_allocated = 1;
+
+ if(pre_alloc_key_crt()){
+ cli_putstr_P(PSTR("\r\nOOM!\r\n"));
+ return;
+ }
+
+ load_bigint_from_os(&pub_key.modulus, MODULUS, sizeof(MODULUS));
+ memcpy(&priv_key.modulus, &pub_key.modulus, sizeof(bigint_t));
+ load_bigint_from_os(&pub_key.exponent, PUB_EXPONENT, sizeof(PUB_EXPONENT));
+ priv_key.n = 5;
+ load_bigint_from_os(&(priv_key.components[0]), P, sizeof(P));
+ load_bigint_from_os(&(priv_key.components[1]), Q, sizeof(Q));
+ load_bigint_from_os(&(priv_key.components[2]), DP, sizeof(DP));
+ load_bigint_from_os(&(priv_key.components[3]), DQ, sizeof(DQ));
+ load_bigint_from_os(&(priv_key.components[4]), QINV, sizeof(QINV));
+}
+
+/*
+