Mailing List Archive

gcry_cipher_algo_name before gcry_cipher_open makes gcry_cipher_open fail
Hello,

I've stumbled accross somethig I don't understand (libgcrypt 1.9.4): the following code

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

void main() {

gcry_cipher_hd_t handle;
int algo=GCRY_CIPHER_SERPENT128;
int mode=GCRY_CIPHER_MODE_CTR;


printf("Error from opening cipher: %i \n",gcry_cipher_open (&handle, algo, mode, 0));
printf("Selected cipher: %s \n",gcry_cipher_algo_name (algo));
}


produces the expected result:


Error from opening cipher: 0
Selected cipher: SERPENT128


However swap the two printf statements, i.e. call gcry_cipher_algo_name before gcry_cipher_open, and the output is


Selected cipher: ?
Error from opening cipher: 536870924


I'm struggling to grasp as to why opening a cipher should fail after doing a mere query.

The funny thing is that if I select int algo=GCRY_CIPHER_AES128 with query-before-open, everythig does work again:


Selected cipher: AES
Error from opening cipher: 0


Is gcry_cipher_openout failing for query-before-open for non-AES a bug or a feature?

Regards
Andreas



_______________________________________________
Gcrypt-devel mailing list
Gcrypt-devel@gnupg.org
http://lists.gnupg.org/mailman/listinfo/gcrypt-devel
Re: gcry_cipher_algo_name before gcry_cipher_open makes gcry_cipher_open fail [ In reply to ]
Hi!

On Wed, 8 Sep 2021 16:05, Andreas Mattheiss said:

> However swap the two printf statements, i.e. call
> gcry_cipher_algo_name before gcry_cipher_open, and the output is
>
>
> Selected cipher: ?
> Error from opening cipher: 536870924

Which translates to

$ gpg-error 536870924
536870924 = (32, 12) = (GPG_ERR_SOURCE_USER_1, GPG_ERR_CIPHER_ALGO)

I just tried this myself using my latest libgcrypt 1.9 version and don't
run into any problem. Please double check that the modified program is
okay:

--8<---------------cut here---------------start------------->8---
#include <gcrypt.h>
#include <stdio.h>

void main() {

gcry_cipher_hd_t handle;
int algo=GCRY_CIPHER_SERPENT128;
int mode=GCRY_CIPHER_MODE_CTR;


printf("Selected cipher: %s \n",gcry_cipher_algo_name (algo));
printf("Error from opening cipher: %i \n",gcry_cipher_open (&handle,
algo, mode, 0));

}
--8<---------------cut here---------------end--------------->8---

I used

$ gcc x.c -lgcrypt
$ ./a.out

with gcc (Debian 8.3.0-6) 8.3.0

and also tried runtime linked with different library version.


Shalom-Salam,

Werner


--
Die Gedanken sind frei. Ausnahmen regelt ein Bundesgesetz.
Re: gcry_cipher_algo_name before gcry_cipher_open makes gcry_cipher_open fail [ In reply to ]
Hello Werner,


Am Sun, Sep 12, 2021 at 10:03:40PM +0200 schrieb Werner Koch:
>
> I just tried this myself using my latest libgcrypt 1.9 version and don't
> run into any problem. Please double check that the modified program is
> okay:

mystery solved.

1.) I incorrectly reported that this happens under libgcrypt 1.9.4. I must have been in the wrong ptty. The problem actually happened under version 1.4.5, which is
ancient.

2.) I failed to notice that a call to gcry_check_version is needed to initialize the library prior to doing anything else. This is described on page 4 of the manual.
Doing so, even on 1.4.5, solves to problem. Apparently, on 1.9.4, the call to gcry_check_version, is not strictly required for the test program to work correctly.

Thanks
Andreas


_______________________________________________
Gcrypt-devel mailing list
Gcrypt-devel@gnupg.org
http://lists.gnupg.org/mailman/listinfo/gcrypt-devel