void DH_generate_key(DH_CTX *dh_ctx) { BI_CTX *bi_ctx = bi_initialize(); int len = dh_ctx->len; bigint *p = bi_import(bi_ctx, dh_ctx->p, len); //p modulus bigint *g = bi_import(bi_ctx, dh_ctx->g, dh_ctx->glen); //generator bigint *x, *gx; bi_permanent(g); //generate private key X get_random_NZ(len, dh_ctx->x); x = bi_import(bi_ctx, dh_ctx->x, len); bi_permanent(x); //calculate public key gx = g^x mod p bi_set_mod(bi_ctx, p, BIGINT_M_OFFSET); bi_ctx->mod_offset = BIGINT_M_OFFSET; gx = bi_mod_power(bi_ctx, g, x); bi_permanent(gx); bi_export(bi_ctx, x, dh_ctx->x, len); bi_export(bi_ctx, gx, dh_ctx->gx, len); bi_depermanent(g); bi_depermanent(x); bi_depermanent(gx); bi_free(bi_ctx, g); bi_free(bi_ctx, x); bi_free(bi_ctx, gx); bi_free_mod(bi_ctx, BIGINT_M_OFFSET); bi_terminate(bi_ctx); }
/** * Free up any RSA context resources. */ void RSA_free(RSA_CTX *rsa_ctx) { BI_CTX *bi_ctx; if (rsa_ctx == NULL) /* deal with ptrs that are null */ return; bi_ctx = rsa_ctx->bi_ctx; bi_depermanent(rsa_ctx->e); bi_free(bi_ctx, rsa_ctx->e); bi_free_mod(rsa_ctx->bi_ctx, BIGINT_M_OFFSET); if (rsa_ctx->d) { bi_depermanent(rsa_ctx->d); bi_free(bi_ctx, rsa_ctx->d); bi_depermanent(rsa_ctx->dP); bi_depermanent(rsa_ctx->dQ); bi_depermanent(rsa_ctx->qInv); bi_free(bi_ctx, rsa_ctx->dP); bi_free(bi_ctx, rsa_ctx->dQ); bi_free(bi_ctx, rsa_ctx->qInv); bi_free_mod(rsa_ctx->bi_ctx, BIGINT_P_OFFSET); bi_free_mod(rsa_ctx->bi_ctx, BIGINT_Q_OFFSET); } bi_terminate(bi_ctx); free(rsa_ctx); }
void DH_compute_key(DH_CTX *dh_ctx) { BI_CTX *bi_ctx = bi_initialize(); int len = dh_ctx->len; bigint *p = bi_import(bi_ctx, dh_ctx->p, len); //p modulus bigint *x = bi_import(bi_ctx, dh_ctx->x, len); //private key bigint *gy = bi_import(bi_ctx, dh_ctx->gy, len); //public key(peer) bigint *k; //negotiated(session) key bi_permanent(x); bi_permanent(gy); //calculate session key k = gy^x mod p bi_set_mod(bi_ctx, p, BIGINT_M_OFFSET); bi_ctx->mod_offset = BIGINT_M_OFFSET; k = bi_mod_power(bi_ctx, gy, x); bi_permanent(k); bi_export(bi_ctx, k, dh_ctx->k, len); bi_depermanent(x); bi_depermanent(gy); bi_depermanent(k); bi_free(bi_ctx, x); bi_free(bi_ctx, gy); bi_free(bi_ctx, k); bi_free_mod(bi_ctx, BIGINT_M_OFFSET); bi_terminate(bi_ctx); }
/** * Free up any RSA context resources. */ void ICACHE_FLASH_ATTR RSA_free(RSA_CTX *rsa_ctx) { BI_CTX *bi_ctx; if (rsa_ctx == NULL) /* deal with ptrs that are null */ return; bi_ctx = rsa_ctx->bi_ctx; bi_depermanent(rsa_ctx->e); bi_free(bi_ctx, rsa_ctx->e); bi_free_mod(rsa_ctx->bi_ctx, BIGINT_M_OFFSET); if (rsa_ctx->d) { bi_depermanent(rsa_ctx->d); bi_free(bi_ctx, rsa_ctx->d); #ifdef CONFIG_BIGINT_CRT bi_depermanent(rsa_ctx->dP); bi_depermanent(rsa_ctx->dQ); bi_depermanent(rsa_ctx->qInv); bi_free(bi_ctx, rsa_ctx->dP); bi_free(bi_ctx, rsa_ctx->dQ); bi_free(bi_ctx, rsa_ctx->qInv); bi_free_mod(rsa_ctx->bi_ctx, BIGINT_P_OFFSET); bi_free_mod(rsa_ctx->bi_ctx, BIGINT_Q_OFFSET); #endif } bi_terminate(bi_ctx); os_free(rsa_ctx); }
/** * @brief Used when cleaning various bigints at the end of a session. * @param ctx [in] The bigint session context. * @param mod_offset [in] The offset to use. * @see bi_set_mod(). */ void ICACHE_FLASH_ATTR bi_free_mod(BI_CTX *ctx, int mod_offset) { bi_depermanent(ctx->bi_mod[mod_offset]); bi_free(ctx, ctx->bi_mod[mod_offset]); #if defined (CONFIG_BIGINT_MONTGOMERY) bi_depermanent(ctx->bi_RR_mod_m[mod_offset]); bi_depermanent(ctx->bi_R_mod_m[mod_offset]); bi_free(ctx, ctx->bi_RR_mod_m[mod_offset]); bi_free(ctx, ctx->bi_R_mod_m[mod_offset]); #elif defined(CONFIG_BIGINT_BARRETT) bi_depermanent(ctx->bi_mu[mod_offset]); bi_free(ctx, ctx->bi_mu[mod_offset]); #endif bi_depermanent(ctx->bi_normalised_mod[mod_offset]); bi_free(ctx, ctx->bi_normalised_mod[mod_offset]); }
/** * @brief Close the bigint context and free any resources. * * Free up any used memory - a check is done if all objects were not * properly freed. * @param ctx [in] The bigint session context. */ void ICACHE_FLASH_ATTR bi_terminate(BI_CTX *ctx) { bi_depermanent(ctx->bi_radix); bi_free(ctx, ctx->bi_radix); if (ctx->active_count != 0) { #ifdef CONFIG_SSL_FULL_MODE ssl_printf("bi_terminate: there were %d un-freed bigints\n", ctx->active_count); #endif return; /* wujg : org ---> abort(); */ } bi_clear_cache(ctx); os_free(ctx); }
/** * @brief Close the bigint context and free any resources. * * Free up any used memory - a check is done if all objects were not * properly freed. * @param ctx [in] The bigint session context. */ void bi_terminate(BI_CTX *ctx) { bi_depermanent(ctx->bi_radix); bi_free(ctx, ctx->bi_radix); if (ctx->active_count != 0) { #ifdef CONFIG_SSL_FULL_MODE printf("bi_terminate: there were %d un-freed bigints\n", ctx->active_count); #endif abort(); } bi_clear_cache(ctx); free(ctx); }
/** * @brief Perform a modular exponentiation. * * This function requires bi_set_mod() to have been called previously. This is * one of the optimisations used for performance. * @param ctx [in] The bigint session context. * @param bi [in] The bigint on which to perform the mod power operation. * @param biexp [in] The bigint exponent. * @return The result of the mod exponentiation operation * @see bi_set_mod(). */ bigint * ICACHE_FLASH_ATTR bi_mod_power(BI_CTX *ctx, bigint *bi, bigint *biexp) { int i = find_max_exp_index(biexp), j, window_size = 1; bigint *biR = int_to_bi(ctx, 1); #if defined(CONFIG_BIGINT_MONTGOMERY) uint8_t mod_offset = ctx->mod_offset; if (!ctx->use_classical) { /* preconvert */ bi = bi_mont(ctx, bi_multiply(ctx, bi, ctx->bi_RR_mod_m[mod_offset])); /* x' */ bi_free(ctx, biR); biR = ctx->bi_R_mod_m[mod_offset]; /* A */ } #endif check(bi); check(biexp); #ifdef CONFIG_BIGINT_SLIDING_WINDOW for (j = i; j > 32; j /= 5) /* work out an optimum size */ window_size++; /* work out the slide constants */ precompute_slide_window(ctx, window_size, bi); #else /* just one constant */ ctx->g = (bigint **)SSL_MALLOC(sizeof(bigint *)); ctx->g[0] = bi_clone(ctx, bi); ctx->window = 1; bi_permanent(ctx->g[0]); #endif /* if sliding-window is off, then only one bit will be done at a time and * will reduce to standard left-to-right exponentiation */ do { if (exp_bit_is_one(biexp, i)) { int l = i-window_size+1; int part_exp = 0; if (l < 0) /* LSB of exponent will always be 1 */ l = 0; else { while (exp_bit_is_one(biexp, l) == 0) l++; /* go back up */ } /* build up the section of the exponent */ for (j = i; j >= l; j--) { biR = bi_residue(ctx, bi_square(ctx, biR)); if (exp_bit_is_one(biexp, j)) part_exp++; if (j != l) part_exp <<= 1; } part_exp = (part_exp-1)/2; /* adjust for array */ biR = bi_residue(ctx, bi_multiply(ctx, biR, ctx->g[part_exp])); i = l-1; } else /* square it */ { biR = bi_residue(ctx, bi_square(ctx, biR)); i--; } } while (i >= 0); /* cleanup */ for (i = 0; i < ctx->window; i++) { bi_depermanent(ctx->g[i]); bi_free(ctx, ctx->g[i]); } SSL_FREE(ctx->g); bi_free(ctx, bi); bi_free(ctx, biexp); #if defined CONFIG_BIGINT_MONTGOMERY return ctx->use_classical ? biR : bi_mont(ctx, biR); /* convert back */ #else /* CONFIG_BIGINT_CLASSICAL or CONFIG_BIGINT_BARRETT */ return biR; #endif }