| 1 |
/* Copyright (C) 2018 Björn Esser <[email protected]> |
| 2 |
* |
| 3 |
* Redistribution and use in source and binary forms, with or without |
| 4 |
* modification, are permitted. |
| 5 |
* |
| 6 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 7 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 8 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 9 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 10 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 11 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 12 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 13 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 14 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 15 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 16 |
* SUCH DAMAGE. |
| 17 |
*/ |
| 18 |
|
| 19 |
#include "crypt-port.h" |
| 20 |
#include <stdio.h> |
| 21 |
|
| 22 |
#define PASSPHRASE "Ob-La-Di, Ob-La-Da" |
| 23 |
|
| 24 |
int |
| 25 |
main (void) |
| 26 |
{ |
| 27 |
const char *pm = crypt_preferred_method(); |
| 28 |
int retval = 0; |
| 29 |
|
| 30 |
#if defined HASH_ALGORITHM_DEFAULT |
| 31 |
if (pm == NULL) |
| 32 |
{ |
| 33 |
printf ("FAIL: crypt_preferred_method returned NULL.\n"); |
| 34 |
retval = 1; |
| 35 |
} |
| 36 |
else |
| 37 |
{ |
| 38 |
printf ("PASS: crypt_preferred_method returned \"%s\".\n", pm); |
| 39 |
|
| 40 |
char gs[CRYPT_GENSALT_OUTPUT_SIZE]; |
| 41 |
struct crypt_data cd; |
| 42 |
|
| 43 |
crypt_gensalt_rn (NULL, 0, NULL, 0, gs, sizeof (gs)); |
| 44 |
|
| 45 |
if (strncmp (gs, pm, strlen (pm))) |
| 46 |
{ |
| 47 |
printf ("FAIL: crypt_preferred_method: \"%s\" ", pm); |
| 48 |
printf ("differs from default prefix.\n"); |
| 49 |
printf ("crypt_gensalt returned: \"%s\".\n", gs); |
| 50 |
retval = 1; |
| 51 |
} |
| 52 |
else |
| 53 |
{ |
| 54 |
printf ("PASS: crypt_preferred_method: \"%s\" ", pm); |
| 55 |
printf ("is the same as default prefix used by "); |
| 56 |
printf ("crypt_gensalt.\n"); |
| 57 |
} |
| 58 |
|
| 59 |
crypt_gensalt_rn (pm, 0, NULL, 0, gs, sizeof (gs)); |
| 60 |
|
| 61 |
if (gs[0] == '*') |
| 62 |
{ |
| 63 |
printf ("FAIL: crypt_preferred_method: \"%s\" ", pm); |
| 64 |
printf ("is not a valid prefix for crypt_gensalt.\n"); |
| 65 |
printf ("crypt_gensalt returned: \"%s\".\n", gs); |
| 66 |
retval = 1; |
| 67 |
} |
| 68 |
else |
| 69 |
{ |
| 70 |
printf ("PASS: crypt_preferred_method: \"%s\" ", pm); |
| 71 |
printf ("is a valid prefix for crypt_gensalt.\n"); |
| 72 |
} |
| 73 |
|
| 74 |
if (strncmp (gs, pm, strlen (pm))) |
| 75 |
{ |
| 76 |
printf ("FAIL: crypt_preferred_method: \"%s\" ", pm); |
| 77 |
printf ("does not generate a setting for "); |
| 78 |
printf ("the intended method.\n"); |
| 79 |
printf ("crypt_gensalt returned: \"%s\".\n", gs); |
| 80 |
retval = 1; |
| 81 |
} |
| 82 |
else |
| 83 |
{ |
| 84 |
printf ("PASS: crypt_preferred_method: \"%s\" ", pm); |
| 85 |
printf ("does generate a setting for "); |
| 86 |
printf ("the intended method.\n"); |
| 87 |
} |
| 88 |
|
| 89 |
crypt_r (PASSPHRASE, gs, &cd); |
| 90 |
|
| 91 |
if (cd.output[0] == '*') |
| 92 |
{ |
| 93 |
printf ("FAIL: crypt_preferred_method: \"%s\" ", pm); |
| 94 |
printf ("is not a valid prefix for crypt.\n"); |
| 95 |
printf ("crypt returned: \"%s\".\n", gs); |
| 96 |
retval = 1; |
| 97 |
} |
| 98 |
else |
| 99 |
{ |
| 100 |
printf ("PASS: crypt_preferred_method: \"%s\" ", pm); |
| 101 |
printf ("is a valid prefix for crypt.\n"); |
| 102 |
} |
| 103 |
|
| 104 |
if (strncmp (cd.output, pm, strlen (pm))) |
| 105 |
{ |
| 106 |
printf ("FAIL: crypt_preferred_method: \"%s\" ", pm); |
| 107 |
printf ("does not generate a hash with "); |
| 108 |
printf ("the intended method.\n"); |
| 109 |
printf ("crypt returned: \"%s\".\n", gs); |
| 110 |
retval = 1; |
| 111 |
} |
| 112 |
else |
| 113 |
{ |
| 114 |
printf ("PASS: crypt_preferred_method: \"%s\" ", pm); |
| 115 |
printf ("does generate a hash with "); |
| 116 |
printf ("the intended method.\n"); |
| 117 |
} |
| 118 |
} |
| 119 |
#else |
| 120 |
if (pm != NULL) |
| 121 |
{ |
| 122 |
printf ("FAIL: crypt_preferred_method returned: \"%s\" ", pm); |
| 123 |
printf ("instead of NULL.\n"); |
| 124 |
retval = 1; |
| 125 |
} |
| 126 |
else |
| 127 |
{ |
| 128 |
printf ("PASS: crypt_preferred_method returned NULL."); |
| 129 |
} |
| 130 |
#endif |
| 131 |
|
| 132 |
return retval; |
| 133 |
} |