Re: ECDH internal functions and FIPS140-2 mode

Nicolas Mora <[email protected]> Wed, 24 Feb 2021 12:03:17 -0500
Newsgroups gmane.network.gnutls.general
Message-ID <[email protected]>
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

_______________________________________________
Gnutls-help mailing list
[email protected]
http://lists.gnupg.org/mailman/listinfo/gnutls-help
test_ecdh.c (text/x-csrc, 1.9 KB)
#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;
}
val.txt (text/plain, 7 KB)
==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)
OpenPGP_0xFE82139440BD22B9.asc (application/pgp-keys, 3 KB)
-----BEGIN PGP PUBLIC KEY BLOCK-----

xsFNBFmJqr8BEADBhkCFzusIdcIn8V8+Maee1V+GhD/sNS/GuqDL5WwVlrdv6TDrEiiIGvX76fs+
F1/wP9z/8P2QVm6pxZG+MGpARmWyYkMyklMpqjuXN8JMutjAM9ymouEtVcb3CV20AgXU7Qe1M2Do
fmg4waRM5vHsLI0gvARgo5Rxxc+DoKS8GApE2nbXB8imFLJ48L1FnDVbQWpIW+mzO7dtMY6XQkpv
qtRkYrEfxvVDHD06fG4SIzVF8QL1iiRHncG+5u24AU1FxKxxFNYUTcQxCQZ5JNHsANmgsWCcheEL
15B0eDYrJ7jDPaGiN2Ullh4csO9zlYyfWA84I4CGi3En5C69M7uvOxvyg7LL9GsrAaH51ksR1ksD
H41OMSBVkeLSpU8RPudy8bpIsGXNtqpAOFjhGoJz6POggY/HmAJeqRDF1HfjPFFm3dZ7E0dLR0aP
vxTwuzIERRcjK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