/* MUST BE THREAD-SAFE */ void calc_dh(struct pluto_crypto_req *r) { /* copy the request, since the reply will re-use the memory of the r->pcr_d.dhq */ struct pcr_skeyid_q dhq; memcpy(&dhq, &r->pcr_d.dhq, sizeof(r->pcr_d.dhq)); /* clear out the reply */ struct pcr_skeyid_r *skr = &r->pcr_d.dhr; zero(skr); /* ??? pointer fields might not be NULLed */ INIT_WIRE_ARENA(*skr); const struct oakley_group_desc *group = lookup_group(dhq.oakley_group); passert(group != NULL); SECKEYPrivateKey *ltsecret = dhq.secret; SECKEYPublicKey *pubk = dhq.pubk; /* now calculate the (g^x)(g^y) */ chunk_t g; setchunk_from_wire(g, &dhq, dhq.role == ORIGINAL_RESPONDER ? &dhq.gi : &dhq.gr); DBG(DBG_CRYPT, DBG_dump_chunk("peer's g: ", g)); const char *story; /* we ignore the value */ skr->shared = calc_dh_shared(g, ltsecret, group, pubk, &story); }
/* MUST BE THREAD-SAFE */ void calc_dh_v2(struct pluto_crypto_req *r, const char **story) { struct pcr_skeycalc_v2_r *const skr = &r->pcr_d.dhv2; /* copy the request, since the reply will re-use the memory of the r->pcr_d.dhq */ struct pcr_skeyid_q dhq; memcpy(&dhq, &r->pcr_d.dhq, sizeof(r->pcr_d.dhq)); /* clear out the reply (including pointers) */ static const struct pcr_skeycalc_v2_r zero_pcr_skeycalc_v2_r; *skr = zero_pcr_skeycalc_v2_r; INIT_WIRE_ARENA(*skr); const struct oakley_group_desc *group = lookup_group(dhq.oakley_group); passert(group != NULL); SECKEYPrivateKey *ltsecret = dhq.secret; SECKEYPublicKey *pubk = dhq.pubk; /* now calculate the (g^x)(g^y) --- need gi on responder, gr on initiator */ chunk_t g; setchunk_from_wire(g, &dhq, dhq.role == ORIGINAL_RESPONDER ? &dhq.gi : &dhq.gr); DBG(DBG_CRYPT, DBG_dump_chunk("peer's g: ", g)); skr->shared = calc_dh_shared(g, ltsecret, group, pubk, story); if (skr->shared != NULL) { /* okay, so now all the shared key material */ calc_skeyseed_v2(&dhq, /* input */ skr->shared, /* input */ dhq.key_size, /* input */ dhq.salt_size, /* input */ &skr->skeyseed, /* output */ &skr->skeyid_d, /* output */ &skr->skeyid_ai, /* output */ &skr->skeyid_ar, /* output */ &skr->skeyid_ei, /* output */ &skr->skeyid_er, /* output */ &skr->skeyid_pi, /* output */ &skr->skeyid_pr, /* output */ &skr->skey_initiator_salt, /* output */ &skr->skey_responder_salt, /* output */ &skr->skey_chunk_SK_pi, /* output */ &skr->skey_chunk_SK_pr); /* output */ } }