14#define DISABLE_DEBUGLOG
17#include "cryptkeyrsa_p.h"
18#include <gwenhywfar/misc.h>
19#include <gwenhywfar/debug.h>
20#include <gwenhywfar/text.h>
31static void dumpKeyData(gcry_ac_data_t data)
36 l=gcry_ac_data_length(data);
44 gcry_ac_data_get_index(data, 0, i, &dname, &mpi);
45 fprintf(stderr,
"%3d: [%s]\n", i, dname);
48 err=gcry_mpi_aprint(GCRYMPI_FMT_USG, &buf, &nbytes, mpi);
68 list=gcry_sexp_find_token(pkey, name, 0);
74 mpi=gcry_sexp_nth_mpi(list, 1, GCRYMPI_FMT_USG);
77 gcry_sexp_release(list);
82 gcry_sexp_release(list);
94# define PRI_SIZET PRIu64
96# define PRI_SIZET PRIu32
99# define PRI_SIZET "zd"
107 gcry_sexp_t keyparm, key;
118 snprintf(numbuf,
sizeof(numbuf)-1,
"%d", nbits);
120 snprintf(buffer,
sizeof(buffer)-1,
124 " (rsa-use-e 5:65537)\n"
130 snprintf(buffer,
sizeof(buffer)-1,
138 buffer[
sizeof(buffer)-1]=0;
142 rc=gcry_sexp_new(&keyparm, buffer, 0, 1);
145 "Error creating S-expression: %s", gpg_strerror(rc));
149 rc=gcry_pk_genkey(&key, keyparm);
150 gcry_sexp_release(keyparm);
156 pkey=gcry_sexp_find_token(key,
"public-key", 0);
159 gcry_sexp_release(key);
167 GWEN_CRYPT_KEY_RSA *xk;
173 gcry_sexp_release(key);
181 gcry_sexp_release(key);
193 xk->modulus=gcry_mpi_copy(n);
194 xk->pubExponent=gcry_mpi_copy(e);
203 pkey=gcry_sexp_find_token(key,
"private-key", 0);
206 gcry_sexp_release(key);
215 GWEN_CRYPT_KEY_RSA *xk;
221 gcry_sexp_release(key);
229 gcry_sexp_release(key);
238 gcry_sexp_release(key);
250 xk->modulus=gcry_mpi_copy(n);
251 xk->pubExponent=gcry_mpi_copy(e);
252 xk->privExponent=gcry_mpi_copy(d);
261 gcry_sexp_release(key);
264 *pSecretKey=secretKey;
271#ifndef NO_DEPRECATED_SYMBOLS
278 "nbits is required to be a multiple of 8 (%d)", nbits);
289 const uint8_t *pInData,
291 uint8_t *pSignatureData,
292 uint32_t *pSignatureLen)
294 GWEN_CRYPT_KEY_RSA *xk;
297 gcry_mpi_t mpi_in=
NULL;
298 gcry_mpi_t mpi_sigout1;
299 gcry_mpi_t mpi_sigout2=
NULL;
306 if (xk->modulus==
NULL) {
311 if (xk->privExponent==
NULL) {
317 err=gcry_mpi_scan(&mpi_in, GCRYMPI_FMT_USG, pInData, inLen, &nscanned);
320 gcry_mpi_release(mpi_in);
326 gcry_mpi_powm(mpi_sigout1, mpi_in, xk->privExponent, xk->modulus);
331 gcry_mpi_sub(mpi_sigout2, xk->modulus, mpi_sigout1);
333 if (gcry_mpi_cmp(mpi_sigout2, mpi_sigout1)<0) {
335 gcry_mpi_set(mpi_sigout1, mpi_sigout2);
340 gcry_mpi_release(mpi_sigout2);
341 gcry_mpi_release(mpi_in);
344 err=gcry_mpi_print(GCRYMPI_FMT_USG,
345 pSignatureData, *pSignatureLen,
346 &nwritten, mpi_sigout1);
347 gcry_mpi_release(mpi_sigout1);
352 *pSignatureLen=nwritten;
360 const uint8_t *pInData,
362 const uint8_t *pSignatureData,
363 uint32_t signatureLen)
365 GWEN_CRYPT_KEY_RSA *xk;
368 gcry_mpi_t mpi_in=
NULL;
369 gcry_mpi_t mpi_sigin1=
NULL;
370 gcry_mpi_t mpi_sigout;
376 if (xk->modulus==
NULL) {
381 if (xk->pubExponent==
NULL) {
388 err=gcry_mpi_scan(&mpi_in, GCRYMPI_FMT_USG, pInData, inLen, &nscanned);
391 gcry_mpi_release(mpi_in);
396 err=gcry_mpi_scan(&mpi_sigin1, GCRYMPI_FMT_USG,
397 pSignatureData, signatureLen,
401 gcry_mpi_release(mpi_sigin1);
402 gcry_mpi_release(mpi_in);
408 gcry_mpi_powm(mpi_sigout, mpi_sigin1, xk->pubExponent, xk->modulus);
410 if (gcry_mpi_cmp(mpi_sigout, mpi_in)) {
411 gcry_mpi_t mpi_sigin2;
416 gcry_mpi_sub(mpi_sigin2, xk->modulus, mpi_sigin1);
417 gcry_mpi_powm(mpi_sigout, mpi_sigin2, xk->pubExponent, xk->modulus);
418 if (gcry_mpi_cmp(mpi_sigout, mpi_in)) {
420 gcry_mpi_release(mpi_sigin2);
421 gcry_mpi_release(mpi_sigout);
422 gcry_mpi_release(mpi_sigin1);
423 gcry_mpi_release(mpi_in);
426 gcry_mpi_release(mpi_sigin2);
429 gcry_mpi_release(mpi_sigout);
430 gcry_mpi_release(mpi_sigin1);
431 gcry_mpi_release(mpi_in);
439 const uint8_t *pInData,
444 GWEN_CRYPT_KEY_RSA *xk;
447 gcry_mpi_t mpi_in=
NULL;
455 if (xk->modulus==
NULL) {
460 if (xk->pubExponent==
NULL) {
467 err=gcry_mpi_scan(&mpi_in, GCRYMPI_FMT_USG, pInData, inLen, &nscanned);
470 gcry_mpi_release(mpi_in);
476 gcry_mpi_powm(mpi_out, mpi_in, xk->pubExponent, xk->modulus);
479 gcry_mpi_release(mpi_in);
482 err=gcry_mpi_print(GCRYMPI_FMT_USG,
485 gcry_mpi_release(mpi_out);
498 const uint8_t *pInData,
503 GWEN_CRYPT_KEY_RSA *xk;
506 gcry_mpi_t mpi_in=
NULL;
514 if (xk->modulus==
NULL) {
519 if (xk->privExponent==
NULL) {
526 err=gcry_mpi_scan(&mpi_in, GCRYMPI_FMT_USG, pInData, inLen, &nscanned);
529 gcry_mpi_release(mpi_in);
535 gcry_mpi_powm(mpi_out, mpi_in, xk->privExponent, xk->modulus);
538 gcry_mpi_release(mpi_in);
541 err=gcry_mpi_print(GCRYMPI_FMT_USG,
544 gcry_mpi_release(mpi_out);
566 if (p==
NULL || len<1) {
571 err=gcry_mpi_scan(&mpi, GCRYMPI_FMT_USG, p, len, &nscanned);
574 gcry_mpi_release(mpi);
598 err=gcry_mpi_aprint(GCRYMPI_FMT_USG, &buf, &nbytes, mpi);
619 err=gcry_mpi_print(GCRYMPI_FMT_USG, buf, nbytes, &nwritten, mpi);
633 GWEN_CRYPT_KEY_RSA *xk;
635 xk=(GWEN_CRYPT_KEY_RSA *) p;
637 gcry_mpi_release(xk->modulus);
639 gcry_mpi_release(xk->pubExponent);
640 if (xk->privExponent)
641 gcry_mpi_release(xk->privExponent);
653 GWEN_CRYPT_KEY_RSA *xk;
718 GWEN_CRYPT_KEY_RSA *xk;
726 if (xk->pub && !pub) {
732 DBG_ERROR(0,
"toDb (%s):", pub?
"public":
"private");
782 GWEN_CRYPT_KEY_RSA *xk;
805 GWEN_CRYPT_KEY_RSA *xk;
828 GWEN_CRYPT_KEY_RSA *xk;
850 const uint8_t *pModulus,
852 const uint8_t *pExponent,
883 pExponent, lExponent);
889 "Internal error: Bad RSA key group");
902 const uint8_t *pModulus,
904 const uint8_t *pExponent,
906 const uint8_t *pPrivExponent,
907 uint32_t lPrivExponent)
918 assert(pPrivExponent);
919 assert(lPrivExponent);
939 pExponent, lExponent);
942 pPrivExponent, lPrivExponent);
948 "Internal error: Bad RSA key group");
962 GWEN_CRYPT_KEY_RSA *xk;
994 GWEN_CRYPT_KEY_RSA *xk;
1007 GWEN_CRYPT_KEY_RSA *xk;
1020 GWEN_CRYPT_KEY_RSA *xk;
1033 GWEN_CRYPT_KEY_RSA *xk;
const char * GWEN_Crypt_CryptAlgoId_toString(GWEN_CRYPT_CRYPTALGOID a)
GWEN_CRYPT_KEY * GWEN_Crypt_Key_new(GWEN_CRYPT_CRYPTALGOID cryptAlgoId, int keySize)
void GWEN_Crypt_Key_free(GWEN_CRYPT_KEY *k)
GWEN_CRYPT_KEY * GWEN_Crypt_Key_fromDb(GWEN_DB_NODE *db)
GWEN_CRYPT_CRYPTALGOID GWEN_Crypt_Key_GetCryptAlgoId(const GWEN_CRYPT_KEY *k)
int GWEN_Crypt_Key_GetKeySize(const GWEN_CRYPT_KEY *k)
GWEN_CRYPT_KEY_SIGN_FN GWEN_Crypt_Key_SetSignFn(GWEN_CRYPT_KEY *k, GWEN_CRYPT_KEY_SIGN_FN f)
GWEN_CRYPT_KEY_VERIFY_FN GWEN_Crypt_Key_SetVerifyFn(GWEN_CRYPT_KEY *k, GWEN_CRYPT_KEY_VERIFY_FN f)
GWEN_CRYPT_KEY_ENCIPHER_FN GWEN_Crypt_Key_SetEncipherFn(GWEN_CRYPT_KEY *k, GWEN_CRYPT_KEY_ENCIPHER_FN f)
int GWEN_Crypt_Key_toDb(const GWEN_CRYPT_KEY *k, GWEN_DB_NODE *db)
GWEN_CRYPT_KEY_DECIPHER_FN GWEN_Crypt_Key_SetDecipherFn(GWEN_CRYPT_KEY *k, GWEN_CRYPT_KEY_DECIPHER_FN f)
struct GWEN_CRYPT_KEY GWEN_CRYPT_KEY
void GWEN_Crypt_KeyRsa_SubFlags(GWEN_CRYPT_KEY *k, uint32_t fl)
static int GWEN_Crypt_KeyRsa__MpiToBuffer(const gcry_mpi_t mpi, unsigned char *buf, size_t nbytes)
static int GWEN_Crypt_KeyRsa__getNamedElement(gcry_sexp_t pkey, const char *name, gcry_mpi_t *pMpi)
GWEN_CRYPT_KEY * GWEN_Crypt_KeyRsa_dup(const GWEN_CRYPT_KEY *k)
int GWEN_Crypt_KeyRsa_GeneratePair2(unsigned int nbits, int use65537e, GWEN_CRYPT_KEY **pPubKey, GWEN_CRYPT_KEY **pSecretKey)
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)
GWEN_CRYPT_KEY * GWEN_Crypt_KeyRsa_fromDb(GWEN_DB_NODE *db)
int GWEN_Crypt_KeyRsa_GeneratePair(unsigned int nbytes, int use65537e, GWEN_CRYPT_KEY **pPubKey, GWEN_CRYPT_KEY **pSecretKey)
GWEN_CRYPT_KEY * GWEN_Crypt_KeyRsa_fromModExp(unsigned int nbytes, const uint8_t *pModulus, uint32_t lModulus, const uint8_t *pExponent, uint32_t lExponent)
int GWEN_Crypt_KeyRsa_toDb(const GWEN_CRYPT_KEY *k, GWEN_DB_NODE *db, int pub)
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)
int GWEN_Crypt_KeyRsa_GetExponent(const GWEN_CRYPT_KEY *k, uint8_t *buffer, uint32_t *pBufLen)
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)
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)
int GWEN_Crypt_KeyRsa_GetSecretExponent(const GWEN_CRYPT_KEY *k, uint8_t *buffer, uint32_t *pBufLen)
void GWEN_Crypt_KeyRsa_AddFlags(GWEN_CRYPT_KEY *k, uint32_t fl)
void GWEN_Crypt_KeyRsa_SetFlags(GWEN_CRYPT_KEY *k, uint32_t fl)
GWENHYWFAR_CB int GWEN_Crypt_KeyRsa_Sign(GWEN_CRYPT_KEY *k, const uint8_t *pInData, uint32_t inLen, uint8_t *pSignatureData, uint32_t *pSignatureLen)
static int GWEN_Crypt_KeyRsa__WriteMpi(GWEN_DB_NODE *db, const char *dbName, const gcry_mpi_t mpi)
int GWEN_Crypt_KeyRsa_GetModulus(const GWEN_CRYPT_KEY *k, uint8_t *buffer, uint32_t *pBufLen)
static int GWEN_Crypt_KeyRsa__ReadMpi(GWEN_DB_NODE *db, const char *dbName, gcry_mpi_t *pMpi)
uint32_t GWEN_Crypt_KeyRsa_GetFlags(const GWEN_CRYPT_KEY *k)
#define GWEN_CRYPT_KEYRSA_FLAGS_DIRECTSIGN
GWEN_DB_NODE * GWEN_DB_Group_new(const char *name)
int GWEN_DB_SetIntValue(GWEN_DB_NODE *n, uint32_t flags, const char *path, int val)
GWEN_DB_NODE * GWEN_DB_GetGroup(GWEN_DB_NODE *n, uint32_t flags, const char *path)
int GWEN_DB_SetCharValue(GWEN_DB_NODE *n, uint32_t flags, const char *path, const char *val)
int GWEN_DB_SetBinValue(GWEN_DB_NODE *n, uint32_t flags, const char *path, const void *val, unsigned int valSize)
const void * GWEN_DB_GetBinValue(GWEN_DB_NODE *n, const char *path, int idx, const void *defVal, unsigned int defValSize, unsigned int *returnValueSize)
void GWEN_DB_Dump(GWEN_DB_NODE *n, int insert)
int GWEN_DB_GetIntValue(GWEN_DB_NODE *n, const char *path, int idx, int defVal)
void GWEN_DB_Group_free(GWEN_DB_NODE *n)
#define GWEN_DB_FLAGS_OVERWRITE_GROUPS
#define GWEN_DB_FLAGS_OVERWRITE_VARS
struct GWEN_DB_NODE GWEN_DB_NODE
#define DBG_INFO(dbg_logger, format,...)
#define DBG_ERROR(dbg_logger, format,...)
#define DBG_DEBUG(dbg_logger, format,...)
#define GWEN_ERROR_VERIFY
#define GWEN_ERROR_INVALID
#define GWEN_ERROR_BAD_DATA
#define GWEN_ERROR_GENERIC
#define GWEN_ERROR_NO_DATA
#define GWEN_INHERIT_SETDATA(bt, t, element, data, fn)
#define GWEN_INHERIT(bt, t)
#define GWEN_INHERIT_GETDATA(bt, t, element)
#define GWEN_FREE_OBJECT(varname)
#define GWEN_NEW_OBJECT(typ, varname)
#define GWEN_PATH_FLAGS_NAMEMUSTEXIST
void GWEN_Text_DumpString(const char *s, unsigned int l, unsigned int insert)