Hello Daiki and Stephan, thanks for your feedback!

I'll describe my context to explain my needs with ECDH.

I'm the author of a library called Rhonabwy [1], this library implements JOSE standards [2], in C, using GnuTLS to implement all cryptographic routines.

This library is used in my SSO Server glewlwyd [3] to implement signed and/or encrypted requests and response between parties.

In my prototype, the ECDH-ES key management works using _gnutls_ecdh_compute_key (I only need this function).

There are 2 feedbacks though:
1- I have a memory leak in the _gnutls_ecdh_compute_key function
I've attached a sample code to reproduce the problem and the valgrind output

2- The input paramters for the _gnutls_ecdh_compute_key functions are gnutls_datum_t of exported values from the ECC keys. I think a public function would rather have gnutls_privkey_t, gnutls_pubkey_t and the gnutls_datum_t *Z output

Le 2021-02-22 à 10 h 32, Stephan Mueller a écrit :

The impact on FIPS is as follows:

- If the newly conceived ECDH API only available in non-FIPS mode, we have no
impact.

- If the newly conceived ECDH API is only meant to be an "internal" API that
is not supposed to be used by normal users, we are fine.

- If the newly conceived API is to be used as a truly normal API that offers
generic (EC)DH operation, the following recently added checks must be invoked
by this API:

        * the received remote public key must be validated

        * during local key pair generation, the key pair must be validated

        * after generating the shared secret, it must be validated

In my opinion, I'd rather have an official API that offers (EC)DH operation as described in my point 2- above, with of course, added checks to make sure the call is valid

I'm very willing to help with that with test cases and help with the code if required.

/Nicolas

[1] https://github.com/babelouest/rhonabwy
[2] https://jose.readthedocs.io/en/latest/
[3] https://github.com/babelouest/glewlwyd
#include <stdio.h>
#include <gnutls/gnutls.h>

int _gnutls_ecdh_compute_key(gnutls_ecc_curve_t curve,
			   const gnutls_datum_t *x, const gnutls_datum_t *y,
			   const gnutls_datum_t *k,
			   const gnutls_datum_t *peer_x, const gnutls_datum_t *peer_y,
			   gnutls_datum_t *Z);

int main(void) {
  unsigned char c_x[] = {0x80, 0x8d, 0x06, 0x00, 0x82, 0xc1, 0x76, 0xee, 0xd3, 0xe7, 0x76, 0xa4, 0xac, 0x59, 0x8c, 0xc8, 0x67, 0x2c, 0x17, 0x79, 0xf9, 0x74, 0xee, 0xcc, 0x9b, 0x03, 0x41, 0x1c, 0xa5, 0xb9, 0x49, 0x5d},
                c_y[] = {0x48, 0xb5, 0xbf, 0xc5, 0x27, 0xdf, 0xce, 0x53, 0xd6, 0xac, 0x71, 0x15, 0x23, 0x7d, 0x03, 0x1c, 0xcf, 0xf8, 0x7a, 0x05, 0x70, 0xb7, 0x73, 0x50, 0xa9, 0xe5, 0x03, 0xee, 0x73, 0x05, 0xa6, 0x9b},
                c_k[] = {0xd3, 0xf3, 0x71, 0x69, 0x13, 0xd4, 0x31, 0x0a, 0x00, 0x26, 0xde, 0x74, 0x1b, 0x3f, 0x18, 0x89, 0x3a, 0xfc, 0x81, 0x14, 0xf0, 0xc8, 0x46, 0x82, 0xba, 0x67, 0x7e, 0x31, 0x3a, 0x13, 0x98, 0x8a},
                c_peer_x[] = {0xc1, 0xe3, 0x49, 0xcb, 0x61, 0xec, 0x70, 0x24, 0x8c, 0xe8, 0x01, 0x03, 0x4c, 0x38, 0x34, 0xe1, 0xb8, 0x8e, 0xbe, 0x11, 0x61, 0xcb, 0x25, 0xaf, 0x38, 0x74, 0x1f, 0x78, 0x5f, 0xcf, 0xc4, 0xc4},
                c_peer_y[] = {0x7b, 0xc9, 0x67, 0x08, 0xef, 0x80, 0x95, 0x2b, 0x53, 0xf8, 0xd2, 0x55, 0x5f, 0xe7, 0x2b, 0x84, 0x1e, 0xd0, 0x45, 0x88, 0x62, 0x8b, 0x1d, 0x37, 0x8a, 0x59, 0x49, 0x39, 0x50, 0x0e, 0xc9, 0xc9};
  gnutls_datum_t x = {c_x, sizeof(c_x)}, 
                 y = {c_y, sizeof(c_y)}, 
                 k = {c_k, sizeof(c_k)}, 
                 peer_x = {c_peer_x, sizeof(c_peer_x)}, 
                 peer_y = {c_peer_y, sizeof(c_peer_y)}, 
                 Z = {NULL, 0};
  gnutls_ecc_curve_t curve = GNUTLS_ECC_CURVE_SECP256R1;

  if (_gnutls_ecdh_compute_key(curve, &x, &y, &k, &peer_x, &peer_y, &Z)) {
    printf("_gnutls_ecdh_compute_key error\n");
  } else {
    printf("_gnutls_ecdh_compute_key success\n");
  }
  
  gnutls_free(Z.data);
  
  printf("End\n");
  
  return 0;
}
==9445== Memcheck, a memory error detector
==9445== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==9445== Using Valgrind-3.16.1 and LibVEX; rerun with -h for copyright info
==9445== Command: ./test_ecdh
==9445== 
==9445== 
==9445== HEAP SUMMARY:
==9445==     in use at exit: 240 bytes in 10 blocks
==9445==   total heap usage: 1,317 allocs, 1,307 frees, 108,091 bytes allocated
==9445== 
==9445== 32 bytes in 1 blocks are indirectly lost in loss record 1 of 10
==9445==    at 0x483877F: malloc (vg_replace_malloc.c:307)
==9445==    by 0x4FA99A9: __gmp_default_allocate (in 
/usr/lib/x86_64-linux-gnu/libgmp.so.10.4.1)
==9445==    by 0x4FC03D3: __gmpz_realloc (in 
/usr/lib/x86_64-linux-gnu/libgmp.so.10.4.1)
==9445==    by 0x4FB9A98: __gmpz_import (in 
/usr/lib/x86_64-linux-gnu/libgmp.so.10.4.1)
==9445==    by 0x4F5F929: nettle_mpz_set_str_256_u (in 
/usr/lib/x86_64-linux-gnu/libhogweed.so.6.1)
==9445==    by 0x499867A: wrap_nettle_mpi_scan (mpi.c:146)
==9445==    by 0x48AAB19: _gnutls_mpi_init_scan (mpi.c:122)
==9445==    by 0x48AAE6D: _gnutls_mpi_init_scan_nz (mpi.c:141)
==9445==    by 0x4998177: _gnutls_ecdh_compute_key (pk.c:1981)
==9445==    by 0x10933D: main (test_ecdh.c:24)
==9445== 
==9445== 32 bytes in 1 blocks are indirectly lost in loss record 2 of 10
==9445==    at 0x483877F: malloc (vg_replace_malloc.c:307)
==9445==    by 0x4FA99A9: __gmp_default_allocate (in 
/usr/lib/x86_64-linux-gnu/libgmp.so.10.4.1)
==9445==    by 0x4FC03D3: __gmpz_realloc (in 
/usr/lib/x86_64-linux-gnu/libgmp.so.10.4.1)
==9445==    by 0x4FB9A98: __gmpz_import (in 
/usr/lib/x86_64-linux-gnu/libgmp.so.10.4.1)
==9445==    by 0x4F5F929: nettle_mpz_set_str_256_u (in 
/usr/lib/x86_64-linux-gnu/libhogweed.so.6.1)
==9445==    by 0x499867A: wrap_nettle_mpi_scan (mpi.c:146)
==9445==    by 0x48AAB19: _gnutls_mpi_init_scan (mpi.c:122)
==9445==    by 0x48AAE6D: _gnutls_mpi_init_scan_nz (mpi.c:141)
==9445==    by 0x4998193: _gnutls_ecdh_compute_key (pk.c:1989)
==9445==    by 0x10933D: main (test_ecdh.c:24)
==9445== 
==9445== 32 bytes in 1 blocks are indirectly lost in loss record 3 of 10
==9445==    at 0x483877F: malloc (vg_replace_malloc.c:307)
==9445==    by 0x4FA99A9: __gmp_default_allocate (in 
/usr/lib/x86_64-linux-gnu/libgmp.so.10.4.1)
==9445==    by 0x4FC03D3: __gmpz_realloc (in 
/usr/lib/x86_64-linux-gnu/libgmp.so.10.4.1)
==9445==    by 0x4FB9A98: __gmpz_import (in 
/usr/lib/x86_64-linux-gnu/libgmp.so.10.4.1)
==9445==    by 0x4F5F929: nettle_mpz_set_str_256_u (in 
/usr/lib/x86_64-linux-gnu/libhogweed.so.6.1)
==9445==    by 0x499867A: wrap_nettle_mpi_scan (mpi.c:146)
==9445==    by 0x48AAB19: _gnutls_mpi_init_scan (mpi.c:122)
==9445==    by 0x48AAE6D: _gnutls_mpi_init_scan_nz (mpi.c:141)
==9445==    by 0x49981B9: _gnutls_ecdh_compute_key (pk.c:1999)
==9445==    by 0x10933D: main (test_ecdh.c:24)
==9445== 
==9445== 32 bytes in 1 blocks are indirectly lost in loss record 4 of 10
==9445==    at 0x483877F: malloc (vg_replace_malloc.c:307)
==9445==    by 0x4FA99A9: __gmp_default_allocate (in 
/usr/lib/x86_64-linux-gnu/libgmp.so.10.4.1)
==9445==    by 0x4FC03D3: __gmpz_realloc (in 
/usr/lib/x86_64-linux-gnu/libgmp.so.10.4.1)
==9445==    by 0x4FB9A98: __gmpz_import (in 
/usr/lib/x86_64-linux-gnu/libgmp.so.10.4.1)
==9445==    by 0x4F5F929: nettle_mpz_set_str_256_u (in 
/usr/lib/x86_64-linux-gnu/libhogweed.so.6.1)
==9445==    by 0x499867A: wrap_nettle_mpi_scan (mpi.c:146)
==9445==    by 0x48AAB19: _gnutls_mpi_init_scan (mpi.c:122)
==9445==    by 0x48AAE6D: _gnutls_mpi_init_scan_nz (mpi.c:141)
==9445==    by 0x49981D1: _gnutls_ecdh_compute_key (pk.c:2007)
==9445==    by 0x10933D: main (test_ecdh.c:24)
==9445== 
==9445== 32 bytes in 1 blocks are indirectly lost in loss record 5 of 10
==9445==    at 0x483877F: malloc (vg_replace_malloc.c:307)
==9445==    by 0x4FA99A9: __gmp_default_allocate (in 
/usr/lib/x86_64-linux-gnu/libgmp.so.10.4.1)
==9445==    by 0x4FC03D3: __gmpz_realloc (in 
/usr/lib/x86_64-linux-gnu/libgmp.so.10.4.1)
==9445==    by 0x4FB9A98: __gmpz_import (in 
/usr/lib/x86_64-linux-gnu/libgmp.so.10.4.1)
==9445==    by 0x4F5F929: nettle_mpz_set_str_256_u (in 
/usr/lib/x86_64-linux-gnu/libhogweed.so.6.1)
==9445==    by 0x499867A: wrap_nettle_mpi_scan (mpi.c:146)
==9445==    by 0x48AAB19: _gnutls_mpi_init_scan (mpi.c:122)
==9445==    by 0x48AAE6D: _gnutls_mpi_init_scan_nz (mpi.c:141)
==9445==    by 0x49981F1: _gnutls_ecdh_compute_key (pk.c:2015)
==9445==    by 0x10933D: main (test_ecdh.c:24)
==9445== 
==9445== 48 (16 direct, 32 indirect) bytes in 1 blocks are definitely lost in 
loss record 6 of 10
==9445==    at 0x483877F: malloc (vg_replace_malloc.c:307)
==9445==    by 0x4998754: wrap_nettle_mpi_init (mpi.c:79)
==9445==    by 0x48AAB02: _gnutls_mpi_init_scan (mpi.c:117)
==9445==    by 0x48AAE6D: _gnutls_mpi_init_scan_nz (mpi.c:141)
==9445==    by 0x4998177: _gnutls_ecdh_compute_key (pk.c:1981)
==9445==    by 0x10933D: main (test_ecdh.c:24)
==9445== 
==9445== 48 (16 direct, 32 indirect) bytes in 1 blocks are definitely lost in 
loss record 7 of 10
==9445==    at 0x483877F: malloc (vg_replace_malloc.c:307)
==9445==    by 0x4998754: wrap_nettle_mpi_init (mpi.c:79)
==9445==    by 0x48AAB02: _gnutls_mpi_init_scan (mpi.c:117)
==9445==    by 0x48AAE6D: _gnutls_mpi_init_scan_nz (mpi.c:141)
==9445==    by 0x4998193: _gnutls_ecdh_compute_key (pk.c:1989)
==9445==    by 0x10933D: main (test_ecdh.c:24)
==9445== 
==9445== 48 (16 direct, 32 indirect) bytes in 1 blocks are definitely lost in 
loss record 8 of 10
==9445==    at 0x483877F: malloc (vg_replace_malloc.c:307)
==9445==    by 0x4998754: wrap_nettle_mpi_init (mpi.c:79)
==9445==    by 0x48AAB02: _gnutls_mpi_init_scan (mpi.c:117)
==9445==    by 0x48AAE6D: _gnutls_mpi_init_scan_nz (mpi.c:141)
==9445==    by 0x49981B9: _gnutls_ecdh_compute_key (pk.c:1999)
==9445==    by 0x10933D: main (test_ecdh.c:24)
==9445== 
==9445== 48 (16 direct, 32 indirect) bytes in 1 blocks are definitely lost in 
loss record 9 of 10
==9445==    at 0x483877F: malloc (vg_replace_malloc.c:307)
==9445==    by 0x4998754: wrap_nettle_mpi_init (mpi.c:79)
==9445==    by 0x48AAB02: _gnutls_mpi_init_scan (mpi.c:117)
==9445==    by 0x48AAE6D: _gnutls_mpi_init_scan_nz (mpi.c:141)
==9445==    by 0x49981D1: _gnutls_ecdh_compute_key (pk.c:2007)
==9445==    by 0x10933D: main (test_ecdh.c:24)
==9445== 
==9445== 48 (16 direct, 32 indirect) bytes in 1 blocks are definitely lost in 
loss record 10 of 10
==9445==    at 0x483877F: malloc (vg_replace_malloc.c:307)
==9445==    by 0x4998754: wrap_nettle_mpi_init (mpi.c:79)
==9445==    by 0x48AAB02: _gnutls_mpi_init_scan (mpi.c:117)
==9445==    by 0x48AAE6D: _gnutls_mpi_init_scan_nz (mpi.c:141)
==9445==    by 0x49981F1: _gnutls_ecdh_compute_key (pk.c:2015)
==9445==    by 0x10933D: main (test_ecdh.c:24)
==9445== 
==9445== LEAK SUMMARY:
==9445==    definitely lost: 80 bytes in 5 blocks
==9445==    indirectly lost: 160 bytes in 5 blocks
==9445==      possibly lost: 0 bytes in 0 blocks
==9445==    still reachable: 0 bytes in 0 blocks
==9445==         suppressed: 0 bytes in 0 blocks
==9445== 
==9445== For lists of detected and suppressed errors, rerun with: -s
==9445== ERROR SUMMARY: 5 errors from 5 contexts (suppressed: 0 from 0)

Attachment: OpenPGP_0xFE82139440BD22B9.asc
Description: application/pgp-keys

Attachment: OpenPGP_signature
Description: OpenPGP digital signature

_______________________________________________
Gnutls-help mailing list
[email protected]
http://lists.gnupg.org/mailman/listinfo/gnutls-help

Reply via email to