Mailing List Archive

extracting mpi from a S-exp crypted with RSA
I haven't understood correctly the model for crypted keys:
in the documentation, i've found that,if i crypt with the rsa algorythm a
S-exp TEXT in the form:

(data
(flags raw)
(value MPI))

the crypted S-exp CRYPTED is represented as:

(enc-val
(rsa
(a A-MPI)))


Calling the function:
int legth=gcrypt_sexp_length(CRYPTED);
I get length=2.

i want to get the mpi (a A-MPI),(that i've understood to be the crypted text
in MPI format) but the function

gcry_mpi_t internal_mpi = gcry_sexp_nth_mpi(CRYPTED,1,GCRYMPI_FMT_USG);

fails.

for completeness,i post the output of a
writt= gcry_sexp_sprint ( CRYPTED , GCRYSEXP_FMT_DEFAULT , buf_test2 ,(size_t)
500);

(i do it only to show that it seems (at least to me) to be a good S-exp in the
format described in the documentation)

the results are: writt=166,buf_test2[500]="\000à\004\b@Ü\004\b(7:enc-val\n
(3:rsa\n (1:a128:
\003t«ö\215û\020+9DÎ\016\002\032\226¸\025OÃ4]²RU\025\003\001*\000Ô-o\210\211~\217i\020\f¯ÿ\225Üdæ\207ã\222)ðÕ¤Íd_Â/\036}ÒÎ\203ùu\r¿\fo\bC©Y\227lñP\026î\020\206¡\224àÄJ\"8æ¼\005NYUCNÇ\211Î.m\215.WðR¼Ð.
\212zýJ¦\222\f\037\2269\eÉϼ×d\025ª\234$)\n"

i'm not a good coder, neither a good english writer, so sorry if i haven't
been clear, or if i haven't understood well the documentation.

thanks in advance for any help.

Flebool

PS if you prefer i can post a source of an example program.
PPS:it may take some time before i can answer your replys
Re: extracting mpi from a S-exp crypted with RSA [ In reply to ]
On Tue, Apr 19, 2005 at 07:00:14PM +0000, flebool wrote:

Hello,

> the crypted S-exp CRYPTED is represented as:
>
> (enc-val
> (rsa
> (a A-MPI)))

Correct.

> Calling the function:
> int legth=gcrypt_sexp_length(CRYPTED);
> I get length=2.

Correct, you have two objects in that S-Expression:

1. "enc-val"
2. (rsa (a A-MPI))

> i want to get the mpi (a A-MPI),(that i've understood to be the crypted text
> in MPI format) but the function
>
> gcry_mpi_t internal_mpi = gcry_sexp_nth_mpi(CRYPTED,1,GCRYMPI_FMT_USG);
>
> fails.

You need to distringuish between the S-Expression containing the MPI
and it's label `(a A-MPI)' and the actual MPI value, which is `A-MPI'.

So, before you can use gcry_sexp_nth_mpi(), you need to extract the
sub-S-Expression, i.e.:

a = (enc-val (rsa (a A-MPI)));
b = sexp_nth (a, 1); // extract `(rsa (a A-MPI))'
mpi = sexp_mpi (b, 1; // extract `A-MPI'.

Alternatively, you can also use gcry_sexp_find_token().

> for completeness,i post the output of a
> writt= gcry_sexp_sprint ( CRYPTED , GCRYSEXP_FMT_DEFAULT , buf_test2 ,(size_t)
> 500);

By the way: GCRYSEXP_FMT_ADVANCED is much more appropriate for output
intended to be human-readable.

Thanks,
Moritz

--
Moritz Schulte
Re: extracting mpi from a S-exp crypted with RSA [ In reply to ]
thank you, you've been very clear:
using the function gcry_sexp_find_token() suggested, i've managed to extract
the MPI from the S-exp.

then i've tried to extract the crypted data from the MPI:

gcry_mpi_print(GCRYMPI_FMT_USG, buf_test, 4000, &written, mpi_extracted)

the function works well:

i've used as text to be crypted & decrypted: "clear text", with a pair of 1024
bit RSA keys(I've tested them with the function gcry_pk_testkey(),so they
should be right).there are two things i don't understand:

1)the value returned in written doesn't depend upon the legth of the
crypt/decrypt string:
i've increased the length of the crypt/decrypt string(up to 940 bytes), but
the value returned in written is always 128.

2)why the value returned in the variable written by the function is 128: i was
expecting it to be a multiple of the size of the keys(something like
1024,2048,3072,ecc)


i've thougth that this could depend on the fact that the MPI data type can
accept string up to 128 bytes length long: if so, I have to divide my strings
in blocks of 128bytes,but i haven't found anything like this in the manual.

thanks in advance for any help.