When the plain text is 128 bytes, encrypt/decrypt failed.

Charles Wang charlesw123456 at gmail.com
Wed Jan 21 14:09:10 CET 2009


Hi:

I'm using libgcrypt-1.4.3. When I try to use RSA to encrypt and decrypt my
data, libgcrypt failed in case. I'm very confused. So I write the following
code:

#include  <stdio.h>
#include  <stdlib.h>
#include  <gcrypt.h>

static void random_set(unsigned char * buf, size_t szbuf)
{
    long rval;
    unsigned char * cur, * last = buf + szbuf;
    for (cur = buf; cur + sizeof(rval) <= last; cur += sizeof(rval)) {
	rval = random();
	memcpy(cur, &rval, sizeof(rval));
    }
    if (cur < last) {
	rval = random();
	memcpy(cur, &rval, last - cur);
    }
}

static void show_sexp(const char * prompt, gcry_sexp_t sexp)
{
    char dumpbuf[4096];
    gcry_sexp_sprint(sexp, GCRYSEXP_FMT_ADVANCED, dumpbuf, sizeof(dumpbuf));
    printf("%s: %s\n", prompt, dumpbuf);
}
static void show_mpi(const char * prompt, gcry_mpi_t mpi)
{
    char dumpbuf[4096];
    gcry_mpi_print(GCRYMPI_FMT_HEX, dumpbuf, sizeof(dumpbuf), NULL, mpi);
    printf("%s: %s\n", prompt, dumpbuf);
}

int main(void)
{
    int count;
    int nbits_data;
    gcry_sexp_t key_spec, key, pubkey, privkey;
    gcry_sexp_t plain0, cipher, plain1;
    gcry_mpi_t x0, x1;
    unsigned char xbuf0[128], xbuf1[128];
    size_t nwritten;

    gcry_control(GCRYCTL_DISABLE_SECMEM, 0);
    if (!gcry_check_version(GCRYPT_VERSION)) {
	fprintf(stderr, "version check failed.\n");
	exit(-1);
    }
    gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0);
    gcry_control(GCRYCTL_ENABLE_QUICK_RANDOM, 0);

    gcry_sexp_new(&key_spec, "(genkey (rsa (nbits 4:1024)))", 0, 1);
    gcry_pk_genkey(&key, key_spec);
    gcry_sexp_release(key_spec);

    pubkey = gcry_sexp_find_token(key, "public-key", 0);
    privkey = gcry_sexp_find_token(key, "private-key", 0);
    gcry_sexp_release(key);
    show_sexp("public-key", pubkey);
    show_sexp("private-key", privkey);

    nbits_data = 1016;

    for (count = 0; count < 8192; ++count) {
	random_set(xbuf0, sizeof(xbuf0));
	gcry_mpi_scan(&x0, GCRYMPI_FMT_USG, xbuf0, nbits_data / 8, NULL);
	/*gcry_mpi_randomize(x0, nbits_data, GCRY_WEAK_RANDOM);*/
	gcry_sexp_build(&plain0, NULL, "(data (flags raw) (value %m))", x0);
	show_sexp("plain0", plain0);

	gcry_pk_encrypt(&cipher, plain0, pubkey);
	show_sexp("cipher", cipher);
	gcry_pk_decrypt(&plain1, cipher, privkey);
	show_sexp("plain1", plain1);

	x1 = gcry_sexp_nth_mpi(plain1, 0, GCRYMPI_FMT_USG);
	show_mpi("x0", x0);
	show_mpi("x1", x1);
	gcry_mpi_print(GCRYMPI_FMT_USG, xbuf1, sizeof(xbuf1), &nwritten, x1);

	printf("%03d: cmp = %d, cmp = %d\n", count, gcry_mpi_cmp(x0, x1),
	       memcmp(xbuf0, xbuf1, nwritten));

	gcry_mpi_release(x1);
	gcry_mpi_release(x0);
	gcry_sexp_release(plain1);
	gcry_sexp_release(cipher);
	gcry_sexp_release(plain0);
    }

    gcry_sexp_release(pubkey);
    gcry_sexp_release(privkey);
    return 0;
}

When nbits_data > 1016 ( 127 bytes ), libgcrypt failed by chance. But if
nbits_data <= 1016, libgcrypt always success! Is this is a correct way or my
code is wrong? I have noticed that 'pubkey.c' in libgcrypt-1.4.3/tests use
800 for nbits_data.

Thanks greatly.

The previous file is provided as an attachment too.
http://www.nabble.com/file/p21582842/tryrsa.c tryrsa.c 

Charles Wang
-- 
View this message in context: http://www.nabble.com/When-the-plain-text-is-128-bytes%2C-encrypt-decrypt-failed.-tp21582842p21582842.html
Sent from the GnuPG - User mailing list archive at Nabble.com.




More information about the Gnupg-users mailing list