File indexing completed on 2025-05-11 08:24:32
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032 #ifdef HAVE_CONFIG_H
0033 #include "config.h"
0034 #endif
0035
0036 #include <crypt.h>
0037 #include <string.h>
0038
0039 #include <rtems/stackchk.h>
0040
0041 #include "tmacros.h"
0042
0043 const char rtems_test_name[] = "CRYPT 1";
0044
0045 typedef struct {
0046 const char *salt;
0047 const char *input;
0048 const char *expected;
0049 } test_case;
0050
0051 static void test_formats(void)
0052 {
0053 struct crypt_data data;
0054 char *s;
0055
0056 s = crypt_r("abc", "def", &data);
0057 rtems_test_assert(strcmp(s, "abc") == 0);
0058
0059 crypt_add_format(&crypt_md5_format);
0060 crypt_add_format(&crypt_sha256_format);
0061 crypt_add_format(&crypt_sha512_format);
0062 }
0063
0064 static void test_md5(void)
0065 {
0066 const char key[] = "0.s0.l33t";
0067 const char salt_0[] = "$1$deadbeef$0Huu6KHrKLVWfqa4WljDE0";
0068 const char salt_1[] = "$1$cafebabe$0Huu6KHrKLVWfqa4WljDE0";
0069 struct crypt_data data;
0070 char *s;
0071
0072 printf("test crypt_md5_r()\n");
0073
0074 s = crypt_md5_r(&key[0], &salt_0[0], &data);
0075 rtems_test_assert(strcmp(s, &salt_0[0]) == 0);
0076
0077 s = crypt_md5_r(&key[0], &salt_1[0], &data);
0078 rtems_test_assert(strcmp(s, &salt_1[0]) != 0);
0079 }
0080
0081 static const test_case test_cases_sha256[] = {
0082 {
0083 "$5$saltstring", "Hello world!",
0084 "$5$saltstring$5B8vYYiY.CVt1RlTTf8KbXBH3hsxY/GNooZaBBGWEc5"
0085 }, {
0086 "$5$rounds=10000$saltstringsaltstring", "Hello world!",
0087 "$5$rounds=10000$saltstringsaltst$3xv.VbSHBb41AL9AvLeujZkZRBAwqFMz2."
0088 "opqey6IcA"
0089 }, {
0090 "$5$rounds=5000$toolongsaltstring", "This is just a test",
0091 "$5$rounds=5000$toolongsaltstrin$Un/5jzAHMgOGZ5.mWJpuVolil07guHPvOW8"
0092 "mGRcvxa5"
0093 }, {
0094 "$5$rounds=1400$anotherlongsaltstring",
0095 "a very much longer text to encrypt. This one even stretches over more"
0096 "than one line.",
0097 "$5$rounds=1400$anotherlongsalts$Rx.j8H.h8HjEDGomFU8bDkXm3XIUnzyxf12"
0098 "oP84Bnq1"
0099 }, {
0100 "$5$rounds=77777$short",
0101 "we have a short salt string but not a short password",
0102 "$5$rounds=77777$short$JiO1O3ZpDAxGJeaDIuqCoEFysAe1mZNJRs3pw0KQRd/"
0103 }, {
0104 "$5$rounds=123456$asaltof16chars..", "a short string",
0105 "$5$rounds=123456$asaltof16chars..$gP3VQ/6X7UUEW3HkBn2w1/Ptq2jxPyzV/"
0106 "cZKmF/wJvD"
0107 }, {
0108 "$5$rounds=10$roundstoolow", "the minimum number is still observed",
0109 "$5$rounds=1000$roundstoolow$yfvwcWrQ8l/K0DAWyuPMDNHpIVlTQebY9l/gL97"
0110 "2bIC"
0111 }
0112 };
0113
0114 static void print_test_case(const test_case *c, const char *s)
0115 {
0116 printf(
0117 "input: %s\nsalt: %s\nexpected: %s\nactual: %s\n",
0118 c->input,
0119 c->salt,
0120 c->expected,
0121 s
0122 );
0123 }
0124
0125 static void test_sha256(void)
0126 {
0127 size_t i;
0128
0129 printf("test crypt_sha256_r()\n");
0130
0131 for (i = 0; i < RTEMS_ARRAY_SIZE(test_cases_sha256); ++i) {
0132 const test_case *c = &test_cases_sha256[i];
0133 struct crypt_data data;
0134 char *s;
0135
0136 s = crypt_sha256_r(c->input, c->salt, &data);
0137 print_test_case(c, s);
0138 rtems_test_assert(strcmp(s, c->expected) == 0);
0139 }
0140 }
0141
0142 static const test_case test_cases_sha512[] = {
0143 {
0144 "$6$saltstring", "Hello world!",
0145 "$6$saltstring$svn8UoSVapNtMuq1ukKS4tPQd8iKwSMHWjl/O817G3uBnIFNjnQJu"
0146 "esI68u4OTLiBFdcbYEdFCoEOfaS35inz1"
0147 }, {
0148 "$6$rounds=10000$saltstringsaltstring", "Hello world!",
0149 "$6$rounds=10000$saltstringsaltst$OW1/O6BYHV6BcXZu8QVeXbDWra3Oeqh0sb"
0150 "HbbMCVNSnCM/UrjmM0Dp8vOuZeHBy/YTBmSK6H9qs/y3RnOaw5v."
0151 }, {
0152 "$6$rounds=5000$toolongsaltstring", "This is just a test",
0153 "$6$rounds=5000$toolongsaltstrin$lQ8jolhgVRVhY4b5pZKaysCLi0QBxGoNeKQ"
0154 "zQ3glMhwllF7oGDZxUhx1yxdYcz/e1JSbq3y6JMxxl8audkUEm0"
0155 }, {
0156 "$6$rounds=1400$anotherlongsaltstring",
0157 "a very much longer text to encrypt. This one even stretches over more"
0158 "than one line.",
0159 "$6$rounds=1400$anotherlongsalts$POfYwTEok97VWcjxIiSOjiykti.o/pQs.wP"
0160 "vMxQ6Fm7I6IoYN3CmLs66x9t0oSwbtEW7o7UmJEiDwGqd8p4ur1"
0161 }, {
0162 "$6$rounds=77777$short",
0163 "we have a short salt string but not a short password",
0164 "$6$rounds=77777$short$WuQyW2YR.hBNpjjRhpYD/ifIw05xdfeEyQoMxIXbkvr0g"
0165 "ge1a1x3yRULJ5CCaUeOxFmtlcGZelFl5CxtgfiAc0"
0166 }, {
0167 "$6$rounds=123456$asaltof16chars..", "a short string",
0168 "$6$rounds=123456$asaltof16chars..$BtCwjqMJGx5hrJhZywWvt0RLE8uZ4oPwc"
0169 "elCjmw2kSYu.Ec6ycULevoBK25fs2xXgMNrCzIMVcgEJAstJeonj1"
0170 }, {
0171 "$6$rounds=10$roundstoolow", "the minimum number is still observed",
0172 "$6$rounds=1000$roundstoolow$kUMsbe306n21p9R.FRkW3IGn.S9NPN0x50YhH1x"
0173 "hLsPuWGsUSklZt58jaTfF4ZEQpyUNGc0dqbpBYYBaHHrsX."
0174 }
0175 };
0176
0177 static void test_sha512(void)
0178 {
0179 size_t i;
0180
0181 printf("test crypt_sha512_r()\n");
0182
0183 for (i = 0; i < RTEMS_ARRAY_SIZE(test_cases_sha512); ++i) {
0184 const test_case *c = &test_cases_sha512[i];
0185 struct crypt_data data;
0186 char *s;
0187
0188 s = crypt_sha512_r(c->input, c->salt, &data);
0189 print_test_case(c, s);
0190 rtems_test_assert(strcmp(s, c->expected) == 0);
0191 }
0192 }
0193
0194 static const test_case test_cases_generic[] = {
0195 {
0196 "saltstring", "Hello world!",
0197 "$6$saltstring$svn8UoSVapNtMuq1ukKS4tPQd8iKwSMHWjl/O817G3uBnIFNjnQJu"
0198 "esI68u4OTLiBFdcbYEdFCoEOfaS35inz1"
0199 }, {
0200 "$1$deadbeef", "0.s0.l33t",
0201 "$1$deadbeef$0Huu6KHrKLVWfqa4WljDE0"
0202 }, {
0203 "$5$saltstring", "Hello world!",
0204 "$5$saltstring$5B8vYYiY.CVt1RlTTf8KbXBH3hsxY/GNooZaBBGWEc5"
0205 }, {
0206 "$6$saltstring", "Hello world!",
0207 "$6$saltstring$svn8UoSVapNtMuq1ukKS4tPQd8iKwSMHWjl/O817G3uBnIFNjnQJu"
0208 "esI68u4OTLiBFdcbYEdFCoEOfaS35inz1"
0209 }
0210 };
0211
0212 static void test_generic(void)
0213 {
0214 size_t i;
0215
0216 printf("test crypt_r()\n");
0217
0218 for (i = 0; i < RTEMS_ARRAY_SIZE(test_cases_generic); ++i) {
0219 const test_case *c = &test_cases_generic[i];
0220 struct crypt_data data;
0221 char *s;
0222
0223 s = crypt_r(c->input, c->salt, &data);
0224 print_test_case(c, s);
0225 rtems_test_assert(strcmp(s, c->expected) == 0);
0226 }
0227 }
0228
0229 static void Init(rtems_task_argument arg)
0230 {
0231 TEST_BEGIN();
0232
0233 test_formats();
0234 test_md5();
0235 test_sha256();
0236 test_sha512();
0237 test_generic();
0238
0239 rtems_test_assert(!rtems_stack_checker_is_blown());
0240 TEST_END();
0241 rtems_test_exit(0);
0242 }
0243
0244 #define CONFIGURE_APPLICATION_DOES_NOT_NEED_CLOCK_DRIVER
0245 #define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER
0246 #define CONFIGURE_STACK_CHECKER_ENABLED
0247
0248 #define CONFIGURE_MAXIMUM_TASKS 1
0249
0250 #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
0251
0252 #define CONFIGURE_INIT_TASK_STACK_SIZE (8 * RTEMS_MINIMUM_STACK_SIZE)
0253
0254 #define CONFIGURE_RTEMS_INIT_TASKS_TABLE
0255
0256 #define CONFIGURE_INIT_TASK_ATTRIBUTES RTEMS_FLOATING_POINT
0257
0258 #define CONFIGURE_INIT
0259
0260 #include <rtems/confdefs.h>