LCOV - code coverage report
Current view: top level - src/utils/unittests - challenge.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 73 81 90.1 %
Date: 2025-01-18 12:13:41 Functions: 9 9 100.0 %

          Line data    Source code
       1             : /* FreeTDS - Library of routines accessing Sybase and Microsoft databases
       2             :  * Copyright (C) 2011  Frediano Ziglio
       3             :  *
       4             :  * This library is free software; you can redistribute it and/or
       5             :  * modify it under the terms of the GNU Library General Public
       6             :  * License as published by the Free Software Foundation; either
       7             :  * version 2 of the License, or (at your option) any later version.
       8             :  *
       9             :  * This library is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      12             :  * Library General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU Library General Public
      15             :  * License along with this library; if not, write to the
      16             :  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
      17             :  * Boston, MA 02111-1307, USA.
      18             :  */
      19             : 
      20             : /* 
      21             :  * Purpose: test challenge code.
      22             :  */
      23             : #undef NDEBUG
      24             : #include <config.h>
      25             : 
      26             : #include <stdio.h>
      27             : #include <assert.h>
      28             : 
      29             : #if HAVE_STDLIB_H
      30             : #include <stdlib.h>
      31             : #endif
      32             : 
      33             : #ifdef HAVE_STRING_H
      34             : #include <string.h>
      35             : #endif
      36             : 
      37             : #include "tds_sysdep_public.h"
      38             : #include <freetds/utils/md4.h>
      39             : #include <freetds/utils/md5.h>
      40             : #include <freetds/utils/hmac_md5.h>
      41             : #include <freetds/utils/des.h>
      42             : 
      43             : static char long_string[512];
      44             : 
      45             : static char *
      46             : bin2ascii(char *dest, const void *data, size_t len)
      47             : {
      48          84 :         char *s = dest;
      49          84 :         const unsigned char *src = (const unsigned char *) data;
      50        2368 :         for (; len > 0; --len, s += 2)
      51        2368 :                 sprintf(s, "%02x", *src++);
      52         144 :         *s = 0;
      53             :         return dest;
      54             : }
      55             : 
      56             : static void
      57          64 : md4(const char *src, const char *out)
      58             : {
      59             :         MD4_CTX ctx;
      60             :         unsigned char digest[16];
      61             :         char s_digest[34];
      62             : 
      63          64 :         assert(strlen(out) == 32);
      64          64 :         MD4Init(&ctx);
      65          64 :         if (strlen(src) > 12) {
      66          40 :                 MD4Update(&ctx, (const unsigned char *) src, 5);
      67          40 :                 MD4Update(&ctx, (const unsigned char *) src+5, strlen(src) - 5);
      68             :         } else {
      69          24 :                 MD4Update(&ctx, (const unsigned char *) src, strlen(src));
      70             :         }
      71          32 :         MD4Final(&ctx, digest);
      72          64 :         if (strcasecmp(bin2ascii(s_digest, digest, 16), out) != 0) {
      73           0 :                 fprintf(stderr, "Wrong md4(%s) -> %s expected %s\n", src, s_digest, out);
      74           0 :                 exit(1);
      75             :         }
      76          64 : }
      77             : 
      78             : static void
      79           8 : md4tests(void)
      80             : {
      81           8 :         md4("", "31d6cfe0d16ae931b73c59d7e0c089c0");
      82           8 :         md4("a", "bde52cb31de33e46245e05fbdbd6fb24");
      83           8 :         md4("abc", "a448017aaf21d8525fc10ae87aa6729d");
      84           8 :         md4("message digest", "d9130a8164549fe818874806e1c7014b");
      85           8 :         md4("abcdefghijklmnopqrstuvwxyz", "d79e1c308aa5bbcdeea8ed63df412da9");
      86           8 :         md4("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", "043f8582f241db351ce627e153e7f0e4");
      87           8 :         md4("12345678901234567890123456789012345678901234567890123456789012345678901234567890", "e33b4ddc9c38f2199c3e7b164fcc0536");
      88           8 :         md4(long_string, "9a827c99bab54f09cd2dbd80246f5fb6");
      89           8 : }
      90             : 
      91             : static void
      92          32 : md5(const char *src, const char *out)
      93             : {
      94             :         MD5_CTX ctx;
      95             :         unsigned char digest[16];
      96             :         char s_digest[34];
      97             : 
      98          32 :         assert(strlen(out) == 32);
      99          32 :         MD5Init(&ctx);
     100          32 :         if (strlen(src) > 12) {
     101          24 :                 MD5Update(&ctx, (const unsigned char *) src, 5);
     102          24 :                 MD5Update(&ctx, (const unsigned char *) src+5, strlen(src) - 5);
     103             :         } else {
     104           8 :                 MD5Update(&ctx, (const unsigned char *) src, strlen(src));
     105             :         }
     106          16 :         MD5Final(&ctx, digest);
     107          32 :         if (strcasecmp(bin2ascii(s_digest, digest, 16), out) != 0) {
     108           0 :                 fprintf(stderr, "Wrong md5(%s) -> %s expected %s\n", src, s_digest, out);
     109           0 :                 exit(1);
     110             :         }
     111          32 : }
     112             : 
     113             : static void
     114           8 : md5tests(void)
     115             : {
     116           8 :         md5("", "d41d8cd98f00b204e9800998ecf8427e");
     117           8 :         md5("The quick brown fox jumps over the lazy dog", "9e107d9d372bb6826bd81d3542a419d6");
     118           8 :         md5("The quick brown fox jumps over the lazy dog.", "e4d909c290d0fb1ca068ffaddf22cbd0");
     119           8 :         md5(long_string, "8b9fc2ac1113974b82ef43ff890efdc6");
     120           8 : }
     121             : 
     122             : static const char *hmac5_key = NULL;
     123             : 
     124             : static void
     125          24 : hmac5(const char *src, const char *out)
     126             : {
     127             :         unsigned char digest[16];
     128             :         char s_digest[34];
     129             : 
     130          24 :         assert(strlen(out) == 32 && hmac5_key);
     131          24 :         hmac_md5((const unsigned char*) hmac5_key, (const unsigned char*) src, strlen(src), digest);
     132          24 :         if (strcasecmp(bin2ascii(s_digest, digest, 16), out) != 0) {
     133           0 :                 fprintf(stderr, "Wrong hman md5(%s) -> %s expected %s\n", src, s_digest, out);
     134           0 :                 exit(1);
     135             :         }
     136          24 : }
     137             : 
     138             : static void
     139           8 : hmac5tests(void)
     140             : {
     141           8 :         hmac5_key = "01234567890123456";
     142           8 :         hmac5("", "c26c57d7ff1236db11d7419a490ad84d");
     143           8 :         hmac5("message digest", "e4f89edbda3d3b6b7328a0a90f87ece8");
     144           8 :         hmac5("abcdefghijklmnopqrstuvwxyz", "2c4eac474ec340df63ae93b8ffc33571");
     145           8 : }
     146             : 
     147             : static void
     148          24 : des(const char *src, const char *out)
     149             : {
     150             :         static const des_cblock key = { 0x4B, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25 };
     151             :         DES_KEY ks;
     152             :         unsigned char digest[128];
     153             :         char s_digest[256];
     154             :         size_t out_len;
     155             : 
     156          24 :         memset(digest, 0, sizeof(digest));
     157          36 :         tds_des_set_key(&ks, key, sizeof(key));
     158          24 :         tds_des_ecb_encrypt(src, strlen(src), &ks, digest);
     159             : 
     160          24 :         out_len = strlen(src) & ~7u;
     161          24 :         if (strcasecmp(bin2ascii(s_digest, digest, out_len), out) != 0) {
     162           0 :                 fprintf(stderr, "Wrong des(%s) -> %s expected %s\n", src, s_digest, out);
     163           0 :                 exit(1);
     164             :         }
     165          24 : }
     166             : 
     167             : static void
     168           8 : destests(void)
     169             : {
     170           8 :         des("", "");
     171           8 :         des("The quick brown fox jumps over the lazy dog",
     172             :             "51551eab3ebab959553caaed64a3dd9c49f595a630c45cb7317332f8ade70308c4e97aeabbdc7f19");
     173           8 :         des("test des encryption",
     174             :             "7ced9849bed3f7efc1686c89759bafa8");
     175           8 : }
     176             : 
     177             : 
     178             : int
     179           8 : main(void)
     180             : {
     181             :         int i;
     182             : 
     183           8 :         long_string[0] = 0;
     184         152 :         for (i = 0; i < 18; ++i)
     185         144 :                 strcat(long_string, "test md4 12345");
     186             : 
     187           8 :         md4tests();
     188           8 :         md5tests();
     189           8 :         hmac5tests();
     190           8 :         destests();
     191           8 :         printf("All tests passed\n");
     192             :         return 0;
     193             : }
     194             : 

Generated by: LCOV version 1.13