14 #define DISABLE_DEBUGLOG
17 #include "cryptkeyrsa_p.h"
18 #include <gwenhywfar/misc.h>
19 #include <gwenhywfar/debug.h>
20 #include <gwenhywfar/text.h>
31 static void dumpKeyData(gcry_ac_data_t data) {
35 l=gcry_ac_data_length(data);
43 gcry_ac_data_get_index(data, 0, i, &dname, &mpi);
44 fprintf(stderr,
"%3d: [%s]\n", i, dname);
47 err=gcry_mpi_aprint(GCRYMPI_FMT_USG, &buf, &nbytes, mpi);
66 list=gcry_sexp_find_token(pkey, name, 0);
72 mpi=gcry_sexp_nth_mpi(list, 1, GCRYMPI_FMT_USG);
75 gcry_sexp_release(list);
80 gcry_sexp_release(list);
90 gcry_sexp_t keyparm, key;
102 snprintf(numbuf,
sizeof(numbuf)-1,
"%d", nbits);
104 snprintf(buffer,
sizeof(buffer)-1,
108 " (rsa-use-e 5:65537)\n"
114 snprintf(buffer,
sizeof(buffer)-1,
122 buffer[
sizeof(buffer)-1]=0;
126 rc=gcry_sexp_new(&keyparm, buffer, 0, 1);
129 "Error creating S-expression: %s", gpg_strerror (rc));
133 rc=gcry_pk_genkey(&key, keyparm);
134 gcry_sexp_release(keyparm);
140 pkey=gcry_sexp_find_token(key,
"public-key", 0);
143 gcry_sexp_release(key);
151 GWEN_CRYPT_KEY_RSA *xk;
157 gcry_sexp_release(key);
165 gcry_sexp_release(key);
177 xk->modulus=gcry_mpi_copy(n);
178 xk->pubExponent=gcry_mpi_copy(e);
187 pkey=gcry_sexp_find_token(key,
"private-key", 0);
190 gcry_sexp_release(key);
199 GWEN_CRYPT_KEY_RSA *xk;
205 gcry_sexp_release(key);
213 gcry_sexp_release(key);
222 gcry_sexp_release(key);
234 xk->modulus=gcry_mpi_copy(n);
235 xk->pubExponent=gcry_mpi_copy(e);
236 xk->privExponent=gcry_mpi_copy(d);
245 gcry_sexp_release(key);
248 *pSecretKey=secretKey;
265 const uint8_t *pInData,
267 uint8_t *pSignatureData,
268 uint32_t *pSignatureLen) {
269 GWEN_CRYPT_KEY_RSA *xk;
273 gcry_mpi_t mpi_sigout1;
274 gcry_mpi_t mpi_sigout2=
NULL;
281 if (xk->modulus==
NULL) {
286 if (xk->privExponent==
NULL) {
293 err=gcry_mpi_scan(&mpi_in, GCRYMPI_FMT_USG, pInData, inLen, &nscanned);
296 gcry_mpi_release(mpi_in);
302 gcry_mpi_powm(mpi_sigout1, mpi_in, xk->privExponent, xk->modulus);
307 gcry_mpi_sub(mpi_sigout2, xk->modulus, mpi_sigout1);
309 if (gcry_mpi_cmp(mpi_sigout2, mpi_sigout1)<0) {
311 gcry_mpi_set(mpi_sigout1, mpi_sigout2);
316 gcry_mpi_release(mpi_sigout2);
317 gcry_mpi_release(mpi_in);
320 err=gcry_mpi_print(GCRYMPI_FMT_USG,
321 pSignatureData, *pSignatureLen,
322 &nwritten, mpi_sigout1);
323 gcry_mpi_release(mpi_sigout1);
328 *pSignatureLen=nwritten;
336 const uint8_t *pInData,
338 const uint8_t *pSignatureData,
339 uint32_t signatureLen) {
340 GWEN_CRYPT_KEY_RSA *xk;
344 gcry_mpi_t mpi_sigin1;
345 gcry_mpi_t mpi_sigout;
351 if (xk->modulus==
NULL) {
356 if (xk->pubExponent==
NULL) {
364 err=gcry_mpi_scan(&mpi_in, GCRYMPI_FMT_USG, pInData, inLen, &nscanned);
367 gcry_mpi_release(mpi_in);
373 err=gcry_mpi_scan(&mpi_sigin1, GCRYMPI_FMT_USG,
374 pSignatureData, signatureLen,
378 gcry_mpi_release(mpi_sigin1);
379 gcry_mpi_release(mpi_in);
385 gcry_mpi_powm(mpi_sigout, mpi_sigin1, xk->pubExponent, xk->modulus);
387 if (gcry_mpi_cmp(mpi_sigout, mpi_in)) {
388 gcry_mpi_t mpi_sigin2;
393 gcry_mpi_sub(mpi_sigin2, xk->modulus, mpi_sigin1);
394 gcry_mpi_powm(mpi_sigout, mpi_sigin2, xk->pubExponent, xk->modulus);
395 if (gcry_mpi_cmp(mpi_sigout, mpi_in)) {
397 gcry_mpi_release(mpi_sigin2);
398 gcry_mpi_release(mpi_sigout);
399 gcry_mpi_release(mpi_sigin1);
400 gcry_mpi_release(mpi_in);
403 gcry_mpi_release(mpi_sigin2);
406 gcry_mpi_release(mpi_sigout);
407 gcry_mpi_release(mpi_sigin1);
408 gcry_mpi_release(mpi_in);
416 const uint8_t *pInData,
420 GWEN_CRYPT_KEY_RSA *xk;
431 if (xk->modulus==
NULL) {
436 if (xk->pubExponent==
NULL) {
444 err=gcry_mpi_scan(&mpi_in, GCRYMPI_FMT_USG, pInData, inLen, &nscanned);
447 gcry_mpi_release(mpi_in);
453 gcry_mpi_powm(mpi_out, mpi_in, xk->pubExponent, xk->modulus);
456 gcry_mpi_release(mpi_in);
459 err=gcry_mpi_print(GCRYMPI_FMT_USG,
462 gcry_mpi_release(mpi_out);
475 const uint8_t *pInData,
479 GWEN_CRYPT_KEY_RSA *xk;
490 if (xk->modulus==
NULL) {
495 if (xk->privExponent==
NULL) {
503 err=gcry_mpi_scan(&mpi_in, GCRYMPI_FMT_USG, pInData, inLen, &nscanned);
506 gcry_mpi_release(mpi_in);
512 gcry_mpi_powm(mpi_out, mpi_in, xk->privExponent, xk->modulus);
515 gcry_mpi_release(mpi_in);
518 err=gcry_mpi_print(GCRYMPI_FMT_USG,
521 gcry_mpi_release(mpi_out);
542 if (p==
NULL || len<1) {
547 err=gcry_mpi_scan(&mpi, GCRYMPI_FMT_USG, p, len, &nscanned);
551 gcry_mpi_release(mpi);
574 err=gcry_mpi_aprint(GCRYMPI_FMT_USG, &buf, &nbytes, mpi);
594 err=gcry_mpi_print(GCRYMPI_FMT_USG, buf, nbytes, &nwritten, mpi);
607 GWEN_CRYPT_KEY_RSA *xk;
609 xk=(GWEN_CRYPT_KEY_RSA*) p;
611 gcry_mpi_release(xk->modulus);
613 gcry_mpi_release(xk->pubExponent);
614 if (xk->privExponent)
615 gcry_mpi_release(xk->privExponent);
626 GWEN_CRYPT_KEY_RSA *xk;
690 GWEN_CRYPT_KEY_RSA *xk;
698 if (xk->pub && !pub) {
704 DBG_ERROR(0,
"toDb (%s):", pub?
"public":
"private");
753 GWEN_CRYPT_KEY_RSA *xk;
775 GWEN_CRYPT_KEY_RSA *xk;
797 GWEN_CRYPT_KEY_RSA *xk;
819 const uint8_t *pModulus,
821 const uint8_t *pExponent,
822 uint32_t lExponent) {
851 pExponent, lExponent);
857 "Internal error: Bad RSA key group");
870 const uint8_t *pModulus,
872 const uint8_t *pExponent,
874 const uint8_t *pPrivExponent,
875 uint32_t lPrivExponent) {
885 assert(pPrivExponent);
886 assert(lPrivExponent);
906 pExponent, lExponent);
909 pPrivExponent, lPrivExponent);
915 "Internal error: Bad RSA key group");
928 GWEN_CRYPT_KEY_RSA *xk;
959 GWEN_CRYPT_KEY_RSA *xk;
971 GWEN_CRYPT_KEY_RSA *xk;
983 GWEN_CRYPT_KEY_RSA *xk;
995 GWEN_CRYPT_KEY_RSA *xk;