Mailing List Archive

Constructing a public key (pair) from component parts
Given the components of an RSA key (n, e, p, q, d), is there any way to
construct a key that I can use for encryption/decryption/signing/verifying
ops withing gcrypt?

If so, can I have a hint?

Brad
Re: Constructing a public key (pair) from component parts [ In reply to ]
On Sun, 5 Jun 2005 11:13:45 +1000, Brad Hards said:

> Given the components of an RSA key (n, e, p, q, d), is there any way to
> construct a key that I can use for encryption/decryption/signing/verifying
> ops withing gcrypt?

Create an S-expression:

(private-key
(rsa
(n NNNNNNNNNNNNN)
(e EEEEEEEEEEEEE)
(d DDDDDDDDDDDDD)
(p PPPPPPPPPPPPP)
(q QQQQQQQQQQQQQ)
(u UUUUUUUUUUUUU)))


Here is a snippet to create a canonical encoded S-expression using
plain C. You may also use libgcrypt functions, which is easier.

sexp = p = xmalloc_secure (30
+ rsa_n_len + rsa_e_len + rsa_p_len + rsa_q_len
+ 4*sizeof (numbuf) + 25 + sizeof(numbuf) + 20);

p = stpcpy (p,"(11:private-key(3:rsa(1:n");
sprintf (numbuf, "%u:", rsa_n_len);
p = stpcpy (p, numbuf);
memcpy (p, rsa_n, rsa_n_len);
p += rsa_n_len;

sprintf (numbuf, ")(1:e%u:", rsa_e_len);
p = stpcpy (p, numbuf);
memcpy (p, rsa_e, rsa_e_len);
p += rsa_e_len;

sprintf (numbuf, ")(1:p%u:", rsa_p_len);
p = stpcpy (p, numbuf);
memcpy (p, rsa_p, rsa_p_len);
p += rsa_p_len;

sprintf (numbuf, ")(1:q%u:", rsa_q_len);
p = stpcpy (p, numbuf);
memcpy (p, rsa_q, rsa_q_len);
p += rsa_q_len;

p = stpcpy (p,"))(10:created-at");
sprintf (numbuf2, "%lu", (unsigned long)sk->timestamp);
sprintf (numbuf, "%d:", strlen (numbuf2));
p = stpcpy (stpcpy (stpcpy (p, numbuf), numbuf2), "))");

rc = agent_scd_writekey (keyno, sexp, p - sexp);

Change above to also include D an U. If you don't have them, you may
create them from P,Q,N,E. There is code in
gnupg-1.9/agent/protect-tool.c#rsa_key_check which shows how to do it.



Salam-Shalom,

Werner