LCOV - code coverage report
Current view: top level - egg - test-dh.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 61.5 % 96 59
Test Date: 2024-05-07 18:02:03 Functions: 71.4 % 14 10

            Line data    Source code
       1              : /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
       2              : /* test-dh.c: Test egg-dh.c
       3              : 
       4              :    Copyright (C) 2009 Stefan Walter
       5              : 
       6              :    The Gnome Keyring Library is free software; you can redistribute it and/or
       7              :    modify it under the terms of the GNU Library General Public License as
       8              :    published by the Free Software Foundation; either version 2 of the
       9              :    License, or (at your option) any later version.
      10              : 
      11              :    The Gnome Keyring Library is distributed in the hope that it will be useful,
      12              :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      13              :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      14              :    Library General Public License for more details.
      15              : 
      16              :    You should have received a copy of the GNU Library General Public
      17              :    License along with the Gnome Library; see the file COPYING.LIB.  If not,
      18              :    <http://www.gnu.org/licenses/>.
      19              : 
      20              :    Author: Stef Walter <stef@memberwebs.com>
      21              : */
      22              : 
      23              : #include "config.h"
      24              : 
      25              : #include "egg/egg-dh.h"
      26              : #include "egg/egg-secure-memory.h"
      27              : #include "egg/egg-testing.h"
      28              : 
      29              : #include <stdlib.h>
      30              : #include <stdio.h>
      31              : #include <string.h>
      32              : 
      33              : #include <glib.h>
      34              : #include <gcrypt.h>
      35              : 
      36            0 : EGG_SECURE_DEFINE_GLIB_GLOBALS ();
      37              : 
      38              : static void
      39            0 : test_perform (void)
      40              : {
      41              :         gcry_mpi_t p, g;
      42              :         gcry_mpi_t x1, X1;
      43              :         gcry_mpi_t x2, X2;
      44              :         gpointer k1, k2;
      45              :         gboolean ret;
      46              :         gsize n1, n2;
      47              : 
      48              :         /* Load up the parameters */
      49            0 :         if (!egg_dh_default_params ("ietf-ike-grp-modp-768", &p, &g))
      50            0 :                 g_assert_not_reached ();
      51              : 
      52              :         /* Generate secrets */
      53            0 :         ret = egg_dh_gen_pair (p, g, 0, &X1, &x1);
      54            0 :         g_assert (ret);
      55            0 :         ret = egg_dh_gen_pair (p, g, 0, &X2, &x2);
      56            0 :         g_assert (ret);
      57              : 
      58              :         /* Calculate keys */
      59            0 :         k1 = egg_dh_gen_secret (X2, x1, p, &n1);
      60            0 :         g_assert (k1);
      61            0 :         k2 = egg_dh_gen_secret (X1, x2, p, &n2);
      62            0 :         g_assert (k2);
      63              : 
      64              :         /* Keys must be the same */
      65            0 :         egg_assert_cmpsize (n1, ==, n2);
      66            0 :         g_assert (memcmp (k1, k2, n1) == 0);
      67              : 
      68            0 :         gcry_mpi_release (p);
      69            0 :         gcry_mpi_release (g);
      70            0 :         gcry_mpi_release (x1);
      71            0 :         gcry_mpi_release (X1);
      72            0 :         egg_secure_free (k1);
      73            0 :         gcry_mpi_release (x2);
      74            0 :         gcry_mpi_release (X2);
      75            0 :         egg_secure_free (k2);
      76            0 : }
      77              : 
      78              : static void
      79            0 : test_short_pair (void)
      80              : {
      81              :         gcry_mpi_t p, g;
      82              :         gcry_mpi_t x1, X1;
      83              :         gboolean ret;
      84              : 
      85              :         /* Load up the parameters */
      86            0 :         ret = egg_dh_default_params ("ietf-ike-grp-modp-1024", &p, &g);
      87            0 :         g_assert (ret);
      88            0 :         g_assert_cmpuint (gcry_mpi_get_nbits (p), ==, 1024);
      89              : 
      90              :         /* Generate secrets */
      91            0 :         ret = egg_dh_gen_pair (p, g, 512, &X1, &x1);
      92            0 :         g_assert (ret);
      93            0 :         g_assert_cmpuint (gcry_mpi_get_nbits (x1), <=, 512);
      94              : 
      95            0 :         gcry_mpi_release (p);
      96            0 :         gcry_mpi_release (g);
      97            0 :         gcry_mpi_release (x1);
      98            0 :         gcry_mpi_release (X1);
      99            0 : }
     100              : 
     101              : static void
     102            7 : check_dh_default (const gchar *name, guint bits)
     103              : {
     104              :         gboolean ret;
     105              :         gcry_mpi_t p, g, check;
     106              :         gconstpointer prime, base;
     107              :         gsize n_prime, n_base;
     108              :         gcry_error_t gcry;
     109              : 
     110            7 :         ret = egg_dh_default_params (name, &p, &g);
     111            7 :         g_assert (ret);
     112            7 :         g_assert_cmpint (gcry_mpi_get_nbits (p), ==, bits);
     113            7 :         g_assert_cmpint (gcry_mpi_get_nbits (g), <, gcry_mpi_get_nbits (p));
     114              : 
     115            7 :         ret = egg_dh_default_params_raw (name, &prime, &n_prime, &base, &n_base);
     116            7 :         g_assert (ret);
     117            7 :         g_assert (prime != NULL);
     118            7 :         egg_assert_cmpsize (n_prime, >, 0);
     119            7 :         g_assert (base != NULL);
     120            7 :         egg_assert_cmpsize (n_base, >, 0);
     121              : 
     122            7 :         gcry = gcry_mpi_scan (&check, GCRYMPI_FMT_USG, prime, n_prime, NULL);
     123            7 :         g_assert (gcry == 0);
     124            7 :         g_assert (gcry_mpi_cmp (check, p) == 0);
     125            7 :         gcry_mpi_release (check);
     126              : 
     127            7 :         gcry = gcry_mpi_scan (&check, GCRYMPI_FMT_USG, base, n_base, NULL);
     128            7 :         g_assert (gcry == 0);
     129            7 :         g_assert (gcry_mpi_cmp (check, g) == 0);
     130            7 :         gcry_mpi_release (check);
     131              : 
     132            7 :         gcry_mpi_release (p);
     133            7 :         gcry_mpi_release (g);
     134            7 : }
     135              : 
     136              : static void
     137            1 : test_default_768 (void)
     138              : {
     139            1 :         check_dh_default ("ietf-ike-grp-modp-768", 768);
     140            1 : }
     141              : 
     142              : static void
     143            1 : test_default_1024 (void)
     144              : {
     145            1 :         check_dh_default ("ietf-ike-grp-modp-1024", 1024);
     146            1 : }
     147              : 
     148              : static void
     149            1 : test_default_1536 (void)
     150              : {
     151            1 :         check_dh_default ("ietf-ike-grp-modp-1536", 1536);
     152            1 : }
     153              : 
     154              : static void
     155            1 : test_default_2048 (void)
     156              : {
     157            1 :         check_dh_default ("ietf-ike-grp-modp-2048", 2048);
     158            1 : }
     159              : 
     160              : static void
     161            1 : test_default_3072 (void)
     162              : {
     163            1 :         check_dh_default ("ietf-ike-grp-modp-3072", 3072);
     164            1 : }
     165              : 
     166              : static void
     167            1 : test_default_4096 (void)
     168              : {
     169            1 :         check_dh_default ("ietf-ike-grp-modp-4096", 4096);
     170            1 : }
     171              : 
     172              : static void
     173            1 : test_default_8192 (void)
     174              : {
     175            1 :         check_dh_default ("ietf-ike-grp-modp-8192", 8192);
     176            1 : }
     177              : 
     178              : static void
     179            1 : test_default_bad (void)
     180              : {
     181              :         gboolean ret;
     182              :         gcry_mpi_t p, g;
     183              : 
     184            1 :         ret = egg_dh_default_params ("bad-name", &p, &g);
     185            1 :         g_assert (!ret);
     186            1 : }
     187              : 
     188              : int
     189            1 : main (int argc, char **argv)
     190              : {
     191            1 :         g_test_init (&argc, &argv, NULL);
     192              : 
     193            1 :         if (!g_test_quick ()) {
     194            0 :                 g_test_add_func ("/dh/perform", test_perform);
     195            0 :                 g_test_add_func ("/dh/short_pair", test_short_pair);
     196              :         }
     197              : 
     198            1 :         g_test_add_func ("/dh/default_768", test_default_768);
     199            1 :         g_test_add_func ("/dh/default_1024", test_default_1024);
     200            1 :         g_test_add_func ("/dh/default_1536", test_default_1536);
     201            1 :         g_test_add_func ("/dh/default_2048", test_default_2048);
     202            1 :         g_test_add_func ("/dh/default_3072", test_default_3072);
     203            1 :         g_test_add_func ("/dh/default_4096", test_default_4096);
     204            1 :         g_test_add_func ("/dh/default_8192", test_default_8192);
     205            1 :         g_test_add_func ("/dh/default_bad", test_default_bad);
     206              : 
     207            1 :         return g_test_run ();
     208              : }
        

Generated by: LCOV version 2.0-1