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;
101 snprintf(numbuf,
sizeof(numbuf)-1,
"%d", nbits);
103 snprintf(buffer,
sizeof(buffer)-1,
107 " (rsa-use-e 5:65537)\n"
113 snprintf(buffer,
sizeof(buffer)-1,
121 buffer[
sizeof(buffer)-1]=0;
125 rc=gcry_sexp_new(&keyparm, buffer, 0, 1);
128 "Error creating S-expression: %s", gpg_strerror (rc));
132 rc=gcry_pk_genkey(&key, keyparm);
133 gcry_sexp_release(keyparm);
139 pkey=gcry_sexp_find_token(key,
"public-key", 0);
142 gcry_sexp_release(key);
150 GWEN_CRYPT_KEY_RSA *xk;
156 gcry_sexp_release(key);
164 gcry_sexp_release(key);
176 xk->modulus=gcry_mpi_copy(n);
177 xk->pubExponent=gcry_mpi_copy(e);
186 pkey=gcry_sexp_find_token(key,
"private-key", 0);
189 gcry_sexp_release(key);
198 GWEN_CRYPT_KEY_RSA *xk;
204 gcry_sexp_release(key);
212 gcry_sexp_release(key);
221 gcry_sexp_release(key);
233 xk->modulus=gcry_mpi_copy(n);
234 xk->pubExponent=gcry_mpi_copy(e);
235 xk->privExponent=gcry_mpi_copy(d);
244 gcry_sexp_release(key);
247 *pSecretKey=secretKey;
254 #ifndef NO_DEPRECATED_SYMBOLS
260 "nbits is required to be a multiple of 8 (%d)", nbits);
265 #endif // ifndef NO_DEPRECATED_SYMBOLS
271 const uint8_t *pInData,
273 uint8_t *pSignatureData,
274 uint32_t *pSignatureLen) {
275 GWEN_CRYPT_KEY_RSA *xk;
278 gcry_mpi_t mpi_in=
NULL;
279 gcry_mpi_t mpi_sigout1;
280 gcry_mpi_t mpi_sigout2=
NULL;
287 if (xk->modulus==
NULL) {
292 if (xk->privExponent==
NULL) {
298 err=gcry_mpi_scan(&mpi_in, GCRYMPI_FMT_USG, pInData, inLen, &nscanned);
301 gcry_mpi_release(mpi_in);
307 gcry_mpi_powm(mpi_sigout1, mpi_in, xk->privExponent, xk->modulus);
312 gcry_mpi_sub(mpi_sigout2, xk->modulus, mpi_sigout1);
314 if (gcry_mpi_cmp(mpi_sigout2, mpi_sigout1)<0) {
316 gcry_mpi_set(mpi_sigout1, mpi_sigout2);
321 gcry_mpi_release(mpi_sigout2);
322 gcry_mpi_release(mpi_in);
325 err=gcry_mpi_print(GCRYMPI_FMT_USG,
326 pSignatureData, *pSignatureLen,
327 &nwritten, mpi_sigout1);
328 gcry_mpi_release(mpi_sigout1);
333 *pSignatureLen=nwritten;
341 const uint8_t *pInData,
343 const uint8_t *pSignatureData,
344 uint32_t signatureLen) {
345 GWEN_CRYPT_KEY_RSA *xk;
348 gcry_mpi_t mpi_in=
NULL;
349 gcry_mpi_t mpi_sigin1=
NULL;
350 gcry_mpi_t mpi_sigout;
356 if (xk->modulus==
NULL) {
361 if (xk->pubExponent==
NULL) {
368 err=gcry_mpi_scan(&mpi_in, GCRYMPI_FMT_USG, pInData, inLen, &nscanned);
371 gcry_mpi_release(mpi_in);
376 err=gcry_mpi_scan(&mpi_sigin1, GCRYMPI_FMT_USG,
377 pSignatureData, signatureLen,
381 gcry_mpi_release(mpi_sigin1);
382 gcry_mpi_release(mpi_in);
388 gcry_mpi_powm(mpi_sigout, mpi_sigin1, xk->pubExponent, xk->modulus);
390 if (gcry_mpi_cmp(mpi_sigout, mpi_in)) {
391 gcry_mpi_t mpi_sigin2;
396 gcry_mpi_sub(mpi_sigin2, xk->modulus, mpi_sigin1);
397 gcry_mpi_powm(mpi_sigout, mpi_sigin2, xk->pubExponent, xk->modulus);
398 if (gcry_mpi_cmp(mpi_sigout, mpi_in)) {
400 gcry_mpi_release(mpi_sigin2);
401 gcry_mpi_release(mpi_sigout);
402 gcry_mpi_release(mpi_sigin1);
403 gcry_mpi_release(mpi_in);
406 gcry_mpi_release(mpi_sigin2);
409 gcry_mpi_release(mpi_sigout);
410 gcry_mpi_release(mpi_sigin1);
411 gcry_mpi_release(mpi_in);
419 const uint8_t *pInData,
423 GWEN_CRYPT_KEY_RSA *xk;
426 gcry_mpi_t mpi_in=
NULL;
434 if (xk->modulus==
NULL) {
439 if (xk->pubExponent==
NULL) {
446 err=gcry_mpi_scan(&mpi_in, GCRYMPI_FMT_USG, pInData, inLen, &nscanned);
449 gcry_mpi_release(mpi_in);
455 gcry_mpi_powm(mpi_out, mpi_in, xk->pubExponent, xk->modulus);
458 gcry_mpi_release(mpi_in);
461 err=gcry_mpi_print(GCRYMPI_FMT_USG,
464 gcry_mpi_release(mpi_out);
477 const uint8_t *pInData,
481 GWEN_CRYPT_KEY_RSA *xk;
484 gcry_mpi_t mpi_in=
NULL;
492 if (xk->modulus==
NULL) {
497 if (xk->privExponent==
NULL) {
504 err=gcry_mpi_scan(&mpi_in, GCRYMPI_FMT_USG, pInData, inLen, &nscanned);
507 gcry_mpi_release(mpi_in);
513 gcry_mpi_powm(mpi_out, mpi_in, xk->privExponent, xk->modulus);
516 gcry_mpi_release(mpi_in);
519 err=gcry_mpi_print(GCRYMPI_FMT_USG,
522 gcry_mpi_release(mpi_out);
543 if (p==
NULL || len<1) {
548 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;
int GWEN_Crypt_KeyRsa_GeneratePair2(unsigned int nbits, int use65537e, GWEN_CRYPT_KEY **pPubKey, GWEN_CRYPT_KEY **pSecretKey)
#define GWEN_DB_FLAGS_OVERWRITE_VARS
void GWEN_DB_Dump(GWEN_DB_NODE *n, int insert)
GWEN_CRYPT_KEY * GWEN_Crypt_KeyRsa_fromModPrivExp(unsigned int nbytes, const uint8_t *pModulus, uint32_t lModulus, const uint8_t *pExponent, uint32_t lExponent, const uint8_t *pPrivExponent, uint32_t lPrivExponent)
static GWENHYWFAR_CB void GWEN_Crypt_KeyRsa_freeData(GWEN_UNUSED void *bp, void *p)
struct GWEN_DB_NODE GWEN_DB_NODE
void GWEN_DB_Group_free(GWEN_DB_NODE *n)
#define GWEN_ERROR_INVALID
int GWEN_Crypt_Key_GetKeySize(const GWEN_CRYPT_KEY *k)
GWEN_CRYPT_KEY_VERIFY_FN GWEN_Crypt_Key_SetVerifyFn(GWEN_CRYPT_KEY *k, GWEN_CRYPT_KEY_VERIFY_FN f)
void GWEN_Crypt_KeyRsa_SetFlags(GWEN_CRYPT_KEY *k, uint32_t fl)
void GWEN_Text_DumpString(const char *s, unsigned int l, unsigned int insert)
void GWEN_Crypt_Key_free(GWEN_CRYPT_KEY *k)
int GWEN_Crypt_KeyRsa_GeneratePair(unsigned int nbytes, int use65537e, GWEN_CRYPT_KEY **pPubKey, GWEN_CRYPT_KEY **pSecretKey)
#define GWEN_FREE_OBJECT(varname)
GWEN_CRYPT_KEY * GWEN_Crypt_KeyRsa_fromModExp(unsigned int nbytes, const uint8_t *pModulus, uint32_t lModulus, const uint8_t *pExponent, uint32_t lExponent)
GWENHYWFAR_CB int GWEN_Crypt_KeyRsa_Sign(GWEN_CRYPT_KEY *k, const uint8_t *pInData, uint32_t inLen, uint8_t *pSignatureData, uint32_t *pSignatureLen)
void GWEN_Crypt_KeyRsa_SubFlags(GWEN_CRYPT_KEY *k, uint32_t fl)
GWEN_CRYPT_CRYPTALGOID GWEN_Crypt_Key_GetCryptAlgoId(const GWEN_CRYPT_KEY *k)
static int GWEN_Crypt_KeyRsa__WriteMpi(GWEN_DB_NODE *db, const char *dbName, const gcry_mpi_t mpi)
void GWEN_Crypt_KeyRsa_AddFlags(GWEN_CRYPT_KEY *k, uint32_t fl)
static GWENHYWFAR_CB int GWEN_Crypt_KeyRsa_Decipher(GWEN_CRYPT_KEY *k, const uint8_t *pInData, uint32_t inLen, uint8_t *pOutData, uint32_t *pOutLen)
static int GWEN_Crypt_KeyRsa__getNamedElement(gcry_sexp_t pkey, const char *name, gcry_mpi_t *pMpi)
int GWEN_Crypt_Key_toDb(const GWEN_CRYPT_KEY *k, GWEN_DB_NODE *db)
#define GWEN_ERROR_BAD_DATA
int GWEN_DB_SetBinValue(GWEN_DB_NODE *n, uint32_t flags, const char *path, const void *val, unsigned int valSize)
#define GWEN_NEW_OBJECT(typ, varname)
GWEN_CRYPT_KEY * GWEN_Crypt_KeyRsa_fromDb(GWEN_DB_NODE *db)
GWEN_CRYPT_KEY_SIGN_FN GWEN_Crypt_Key_SetSignFn(GWEN_CRYPT_KEY *k, GWEN_CRYPT_KEY_SIGN_FN f)
#define DBG_DEBUG(dbg_logger, format, args...)
static GWENHYWFAR_CB int GWEN_Crypt_KeyRsa_Encipher(GWEN_CRYPT_KEY *k, const uint8_t *pInData, uint32_t inLen, uint8_t *pOutData, uint32_t *pOutLen)
const char * GWEN_Crypt_CryptAlgoId_toString(GWEN_CRYPT_CRYPTALGOID a)
#define GWEN_DB_FLAGS_OVERWRITE_GROUPS
const void * GWEN_DB_GetBinValue(GWEN_DB_NODE *n, const char *path, int idx, const void *defVal, unsigned int defValSize, unsigned int *returnValueSize)
#define GWEN_ERROR_GENERIC
struct GWEN_CRYPT_KEY GWEN_CRYPT_KEY
static int GWEN_Crypt_KeyRsa__ReadMpi(GWEN_DB_NODE *db, const char *dbName, gcry_mpi_t *pMpi)
GWEN_DB_NODE * GWEN_DB_GetGroup(GWEN_DB_NODE *n, uint32_t flags, const char *path)
#define GWEN_CRYPT_KEYRSA_FLAGS_DIRECTSIGN
GWEN_CRYPT_KEY_DECIPHER_FN GWEN_Crypt_Key_SetDecipherFn(GWEN_CRYPT_KEY *k, GWEN_CRYPT_KEY_DECIPHER_FN f)
#define DBG_ERROR(dbg_logger, format, args...)
int GWEN_DB_SetCharValue(GWEN_DB_NODE *n, uint32_t flags, const char *path, const char *val)
#define DBG_INFO(dbg_logger, format, args...)
int GWEN_Crypt_KeyRsa_GetSecretExponent(const GWEN_CRYPT_KEY *k, uint8_t *buffer, uint32_t *pBufLen)
int GWEN_DB_GetIntValue(GWEN_DB_NODE *n, const char *path, int idx, int defVal)
#define GWEN_ERROR_VERIFY
GWEN_DB_NODE * GWEN_DB_Group_new(const char *name)
uint32_t GWEN_Crypt_KeyRsa_GetFlags(const GWEN_CRYPT_KEY *k)
#define GWEN_INHERIT(bt, t)
GWEN_CRYPT_KEY_ENCIPHER_FN GWEN_Crypt_Key_SetEncipherFn(GWEN_CRYPT_KEY *k, GWEN_CRYPT_KEY_ENCIPHER_FN f)
int GWEN_DB_SetIntValue(GWEN_DB_NODE *n, uint32_t flags, const char *path, int val)
GWEN_CRYPT_KEY * GWEN_Crypt_Key_new(GWEN_CRYPT_CRYPTALGOID cryptAlgoId, int keySize)
#define GWEN_ERROR_NO_DATA
#define GWEN_INHERIT_SETDATA(bt, t, element, data, fn)
GWEN_CRYPT_KEY * GWEN_Crypt_KeyRsa_dup(const GWEN_CRYPT_KEY *k)
int GWEN_Crypt_KeyRsa_GetExponent(const GWEN_CRYPT_KEY *k, uint8_t *buffer, uint32_t *pBufLen)
#define GWEN_PATH_FLAGS_NAMEMUSTEXIST
int GWEN_Crypt_KeyRsa_toDb(const GWEN_CRYPT_KEY *k, GWEN_DB_NODE *db, int pub)
GWEN_CRYPT_KEY * GWEN_Crypt_Key_fromDb(GWEN_DB_NODE *db)
static int GWEN_Crypt_KeyRsa__MpiToBuffer(const gcry_mpi_t mpi, unsigned char *buf, size_t nbytes)
int GWEN_Crypt_KeyRsa_GetModulus(const GWEN_CRYPT_KEY *k, uint8_t *buffer, uint32_t *pBufLen)
#define GWEN_INHERIT_GETDATA(bt, t, element)
static GWENHYWFAR_CB int GWEN_Crypt_KeyRsa_Verify(GWEN_CRYPT_KEY *k, const uint8_t *pInData, uint32_t inLen, const uint8_t *pSignatureData, uint32_t signatureLen)