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 11:50:39 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/sysdep_private.h>
      39             : #include <freetds/utils/md4.h>
      40             : #include <freetds/utils/md5.h>
      41             : #include <freetds/utils/hmac_md5.h>
      42             : #include <freetds/utils/des.h>
      43             : 
      44             : static char long_string[512];
      45             : 
      46             : static char *
      47             : bin2ascii(char *dest, const void *data, size_t len)
      48             : {
      49          84 :         char *s = dest;
      50          84 :         const unsigned char *src = (const unsigned char *) data;
      51        2368 :         for (; len > 0; --len, s += 2)
      52        2368 :                 sprintf(s, "%02x", *src++);
      53         144 :         *s = 0;
      54             :         return dest;
      55             : }
      56             : 
      57             : static void
      58          64 : md4(const char *src, const char *out)
      59             : {
      60             :         MD4_CTX ctx;
      61             :         unsigned char digest[16];
      62             :         char s_digest[34];
      63             : 
      64          64 :         assert(strlen(out) == 32);
      65          64 :         MD4Init(&ctx);
      66          64 :         if (strlen(src) > 12) {
      67          40 :                 MD4Update(&ctx, (const unsigned char *) src, 5);
      68          40 :                 MD4Update(&ctx, (const unsigned char *) src+5, strlen(src) - 5);
      69             :         } else {
      70          24 :                 MD4Update(&ctx, (const unsigned char *) src, strlen(src));
      71             :         }
      72          32 :         MD4Final(&ctx, digest);
      73          64 :         if (strcasecmp(bin2ascii(s_digest, digest, 16), out) != 0) {
      74           0 :                 fprintf(stderr, "Wrong md4(%s) -> %s expected %s\n", src, s_digest, out);
      75           0 :                 exit(1);
      76             :         }
      77          64 : }
      78             : 
      79             : static void
      80           8 : md4tests(void)
      81             : {
      82           8 :         md4("", "31d6cfe0d16ae931b73c59d7e0c089c0");
      83           8 :         md4("a", "bde52cb31de33e46245e05fbdbd6fb24");
      84           8 :         md4("abc", "a448017aaf21d8525fc10ae87aa6729d");
      85           8 :         md4("message digest", "d9130a8164549fe818874806e1c7014b");
      86           8 :         md4("abcdefghijklmnopqrstuvwxyz", "d79e1c308aa5bbcdeea8ed63df412da9");
      87           8 :         md4("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", "043f8582f241db351ce627e153e7f0e4");
      88           8 :         md4("12345678901234567890123456789012345678901234567890123456789012345678901234567890", "e33b4ddc9c38f2199c3e7b164fcc0536");
      89           8 :         md4(long_string, "9a827c99bab54f09cd2dbd80246f5fb6");
      90           8 : }
      91             : 
      92             : static void
      93          32 : md5(const char *src, const char *out)
      94             : {
      95             :         MD5_CTX ctx;
      96             :         unsigned char digest[16];
      97             :         char s_digest[34];
      98             : 
      99          32 :         assert(strlen(out) == 32);
     100          32 :         MD5Init(&ctx);
     101          32 :         if (strlen(src) > 12) {
     102          24 :                 MD5Update(&ctx, (const unsigned char *) src, 5);
     103          24 :                 MD5Update(&ctx, (const unsigned char *) src+5, strlen(src) - 5);
     104             :         } else {
     105           8 :                 MD5Update(&ctx, (const unsigned char *) src, strlen(src));
     106             :         }
     107          16 :         MD5Final(&ctx, digest);
     108          32 :         if (strcasecmp(bin2ascii(s_digest, digest, 16), out) != 0) {
     109           0 :                 fprintf(stderr, "Wrong md5(%s) -> %s expected %s\n", src, s_digest, out);
     110           0 :                 exit(1);
     111             :         }
     112          32 : }
     113             : 
     114             : static void
     115           8 : md5tests(void)
     116             : {
     117           8 :         md5("", "d41d8cd98f00b204e9800998ecf8427e");
     118           8 :         md5("The quick brown fox jumps over the lazy dog", "9e107d9d372bb6826bd81d3542a419d6");
     119           8 :         md5("The quick brown fox jumps over the lazy dog.", "e4d909c290d0fb1ca068ffaddf22cbd0");
     120           8 :         md5(long_string, "8b9fc2ac1113974b82ef43ff890efdc6");
     121           8 : }
     122             : 
     123             : static const char *hmac5_key = NULL;
     124             : 
     125             : static void
     126          24 : hmac5(const char *src, const char *out)
     127             : {
     128             :         unsigned char digest[16];
     129             :         char s_digest[34];
     130             : 
     131          24 :         assert(strlen(out) == 32 && hmac5_key);
     132          24 :         hmac_md5((const unsigned char*) hmac5_key, (const unsigned char*) src, strlen(src), digest);
     133          24 :         if (strcasecmp(bin2ascii(s_digest, digest, 16), out) != 0) {
     134           0 :                 fprintf(stderr, "Wrong hman md5(%s) -> %s expected %s\n", src, s_digest, out);
     135           0 :                 exit(1);
     136             :         }
     137          24 : }
     138             : 
     139             : static void
     140           8 : hmac5tests(void)
     141             : {
     142           8 :         hmac5_key = "01234567890123456";
     143           8 :         hmac5("", "c26c57d7ff1236db11d7419a490ad84d");
     144           8 :         hmac5("message digest", "e4f89edbda3d3b6b7328a0a90f87ece8");
     145           8 :         hmac5("abcdefghijklmnopqrstuvwxyz", "2c4eac474ec340df63ae93b8ffc33571");
     146           8 : }
     147             : 
     148             : static void
     149          24 : des(const char *src, const char *out)
     150             : {
     151             :         static const des_cblock key = { 0x4B, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25 };
     152             :         DES_KEY ks;
     153             :         unsigned char digest[128];
     154             :         char s_digest[256];
     155             :         size_t out_len;
     156             : 
     157          24 :         memset(digest, 0, sizeof(digest));
     158          24 :         tds_des_set_key(&ks, key);
     159          24 :         tds_des_ecb_encrypt(src, strlen(src), &ks, digest);
     160             : 
     161          24 :         out_len = strlen(src) & ~7u;
     162          24 :         if (strcasecmp(bin2ascii(s_digest, digest, out_len), out) != 0) {
     163           0 :                 fprintf(stderr, "Wrong des(%s) -> %s expected %s\n", src, s_digest, out);
     164           0 :                 exit(1);
     165             :         }
     166          24 : }
     167             : 
     168             : static void
     169           8 : destests(void)
     170             : {
     171           8 :         des("", "");
     172           8 :         des("The quick brown fox jumps over the lazy dog",
     173             :             "51551eab3ebab959553caaed64a3dd9c49f595a630c45cb7317332f8ade70308c4e97aeabbdc7f19");
     174           8 :         des("test des encryption",
     175             :             "7ced9849bed3f7efc1686c89759bafa8");
     176           8 : }
     177             : 
     178             : 
     179             : int
     180           8 : main(void)
     181             : {
     182             :         int i;
     183             : 
     184           8 :         long_string[0] = 0;
     185         152 :         for (i = 0; i < 18; ++i)
     186         144 :                 strcat(long_string, "test md4 12345");
     187             : 
     188           8 :         md4tests();
     189           8 :         md5tests();
     190           8 :         hmac5tests();
     191           8 :         destests();
     192           8 :         printf("All tests passed\n");
     193             :         return 0;
     194             : }
     195             : 

Generated by: LCOV version 1.13