LCOV - code coverage report
Current view: top level - egg - test-dh.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 45 81 55.6 %
Date: 2024-02-08 14:44:34 Functions: 10 14 71.4 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 7 46 15.2 %

           Branch data     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                 :            :    see <http://www.gnu.org/licenses/>.
      19                 :            : 
      20                 :            :    Author: Stef Walter <stef@memberwebs.com>
      21                 :            : */
      22                 :            : 
      23                 :            : #include "config.h"
      24                 :            : 
      25                 :            : #undef G_DISABLE_ASSERT
      26                 :            : 
      27                 :            : #include "egg/egg-dh.h"
      28                 :            : #include "egg/egg-secure-memory.h"
      29                 :            : #include "egg/egg-testing.h"
      30                 :            : 
      31                 :            : #include <stdlib.h>
      32                 :            : #include <stdio.h>
      33                 :            : #include <string.h>
      34                 :            : 
      35                 :            : #include <glib.h>
      36                 :            : 
      37                 :          0 : EGG_SECURE_DEFINE_GLIB_GLOBALS ();
      38                 :            : 
      39                 :            : static void
      40                 :          0 : test_perform (void)
      41                 :            : {
      42                 :            :         egg_dh_params *params;
      43                 :            :         egg_dh_pubkey *y1;
      44                 :            :         egg_dh_privkey *x1;
      45                 :            :         egg_dh_pubkey *y2;
      46                 :            :         egg_dh_privkey *x2;
      47                 :            :         GBytes *k1, *k2;
      48                 :            :         gboolean ret;
      49                 :            : 
      50                 :            :         /* Load up the parameters */
      51                 :          0 :         params = egg_dh_default_params ("ietf-ike-grp-modp-768");
      52         [ #  # ]:          0 :         if (!params)
      53                 :          0 :                 g_assert_not_reached ();
      54                 :            : 
      55                 :            :         /* Generate secrets */
      56                 :          0 :         ret = egg_dh_gen_pair (params, 0, &y1, &x1);
      57         [ #  # ]:          0 :         g_assert_true (ret);
      58                 :          0 :         ret = egg_dh_gen_pair (params, 0, &y2, &x2);
      59         [ #  # ]:          0 :         g_assert_true (ret);
      60                 :            : 
      61                 :            :         /* Calculate keys */
      62                 :          0 :         k1 = egg_dh_gen_secret (y1, x2, params);
      63         [ #  # ]:          0 :         g_assert_nonnull (k1);
      64                 :          0 :         k2 = egg_dh_gen_secret (y2, x1, params);
      65         [ #  # ]:          0 :         g_assert_nonnull (k2);
      66                 :            : 
      67                 :            :         /* Keys must be the same */
      68   [ #  #  #  #  :          0 :         g_assert_cmpmem (g_bytes_get_data (k1, NULL), g_bytes_get_size (k1),
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
      69                 :            :                          g_bytes_get_data (k2, NULL), g_bytes_get_size (k2));
      70                 :            : 
      71                 :          0 :         egg_dh_params_free (params);
      72                 :          0 :         egg_dh_pubkey_free (y1);
      73                 :          0 :         egg_dh_privkey_free (x1);
      74                 :          0 :         egg_secure_free (k1);
      75                 :          0 :         egg_dh_pubkey_free (y2);
      76                 :          0 :         egg_dh_privkey_free (x2);
      77                 :          0 :         egg_secure_free (k2);
      78                 :          0 : }
      79                 :            : 
      80                 :            : static void
      81                 :          0 : test_short_pair (void)
      82                 :            : {
      83                 :            :         egg_dh_params *params;
      84                 :            :         egg_dh_pubkey *y1;
      85                 :            :         egg_dh_privkey *x1;
      86                 :            :         GBytes *bytes;
      87                 :            :         gboolean ret;
      88                 :            : 
      89                 :            :         /* Load up the parameters */
      90                 :          0 :         params = egg_dh_default_params ("ietf-ike-grp-modp-1024");
      91         [ #  # ]:          0 :         g_assert_nonnull (params);
      92                 :            : 
      93                 :            :         /* Generate secrets */
      94                 :          0 :         ret = egg_dh_gen_pair (params, 512, &y1, &x1);
      95         [ #  # ]:          0 :         g_assert_true (ret);
      96                 :          0 :         bytes = egg_dh_pubkey_export (y1);
      97         [ #  # ]:          0 :         g_assert_cmpuint (g_bytes_get_size (bytes), <=, 512);
      98                 :          0 :         g_bytes_unref (bytes);
      99                 :            : 
     100                 :          0 :         egg_dh_params_free (params);
     101                 :          0 :         egg_dh_pubkey_free (y1);
     102                 :          0 :         egg_dh_privkey_free (x1);
     103                 :          0 : }
     104                 :            : 
     105                 :            : static void
     106                 :          7 : check_dh_default (const gchar *name, guint bits)
     107                 :            : {
     108                 :            :         gboolean ret;
     109                 :            :         gconstpointer prime, base;
     110                 :            :         gsize n_prime, n_base;
     111                 :            : 
     112                 :          7 :         ret = egg_dh_default_params_raw (name, &prime, &n_prime, &base, &n_base);
     113         [ -  + ]:          7 :         g_assert_true (ret);
     114         [ -  + ]:          7 :         g_assert_nonnull (prime);
     115         [ -  + ]:          7 :         egg_assert_cmpsize (n_prime, >, 0);
     116         [ -  + ]:          7 :         g_assert_nonnull (base);
     117         [ -  + ]:          7 :         egg_assert_cmpsize (n_base, >, 0);
     118                 :          7 : }
     119                 :            : 
     120                 :            : static void
     121                 :          1 : test_default_768 (void)
     122                 :            : {
     123                 :          1 :         check_dh_default ("ietf-ike-grp-modp-768", 768);
     124                 :          1 : }
     125                 :            : 
     126                 :            : static void
     127                 :          1 : test_default_1024 (void)
     128                 :            : {
     129                 :          1 :         check_dh_default ("ietf-ike-grp-modp-1024", 1024);
     130                 :          1 : }
     131                 :            : 
     132                 :            : static void
     133                 :          1 : test_default_1536 (void)
     134                 :            : {
     135                 :          1 :         check_dh_default ("ietf-ike-grp-modp-1536", 1536);
     136                 :          1 : }
     137                 :            : 
     138                 :            : static void
     139                 :          1 : test_default_2048 (void)
     140                 :            : {
     141                 :          1 :         check_dh_default ("ietf-ike-grp-modp-2048", 2048);
     142                 :          1 : }
     143                 :            : 
     144                 :            : static void
     145                 :          1 : test_default_3072 (void)
     146                 :            : {
     147                 :          1 :         check_dh_default ("ietf-ike-grp-modp-3072", 3072);
     148                 :          1 : }
     149                 :            : 
     150                 :            : static void
     151                 :          1 : test_default_4096 (void)
     152                 :            : {
     153                 :          1 :         check_dh_default ("ietf-ike-grp-modp-4096", 4096);
     154                 :          1 : }
     155                 :            : 
     156                 :            : static void
     157                 :          1 : test_default_8192 (void)
     158                 :            : {
     159                 :          1 :         check_dh_default ("ietf-ike-grp-modp-8192", 8192);
     160                 :          1 : }
     161                 :            : 
     162                 :            : static void
     163                 :          1 : test_default_bad (void)
     164                 :            : {
     165                 :            :         egg_dh_params *params;
     166                 :            : 
     167                 :          1 :         params = egg_dh_default_params ("bad-name");
     168         [ -  + ]:          1 :         g_assert_null (params);
     169                 :          1 : }
     170                 :            : 
     171                 :            : int
     172                 :          1 : main (int argc, char **argv)
     173                 :            : {
     174                 :          1 :         g_test_init (&argc, &argv, NULL);
     175                 :            : 
     176         [ -  + ]:          1 :         if (!g_test_quick ()) {
     177                 :          0 :                 g_test_add_func ("/dh/perform", test_perform);
     178                 :          0 :                 g_test_add_func ("/dh/short_pair", test_short_pair);
     179                 :            :         }
     180                 :            : 
     181                 :          1 :         g_test_add_func ("/dh/default_768", test_default_768);
     182                 :          1 :         g_test_add_func ("/dh/default_1024", test_default_1024);
     183                 :          1 :         g_test_add_func ("/dh/default_1536", test_default_1536);
     184                 :          1 :         g_test_add_func ("/dh/default_2048", test_default_2048);
     185                 :          1 :         g_test_add_func ("/dh/default_3072", test_default_3072);
     186                 :          1 :         g_test_add_func ("/dh/default_4096", test_default_4096);
     187                 :          1 :         g_test_add_func ("/dh/default_8192", test_default_8192);
     188                 :          1 :         g_test_add_func ("/dh/default_bad", test_default_bad);
     189                 :            : 
     190                 :          1 :         return g_test_run ();
     191                 :            : }

Generated by: LCOV version 1.14