Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:32

0001 /*
0002  * Copyright (c) 2011 The FreeBSD Project. All rights reserved.
0003  *
0004  * Copyright (c) 2014 embedded brains GmbH & Co. KG
0005  *
0006  * Redistribution and use in source and binary forms, with or without
0007  * modification, are permitted provided that the following conditions
0008  * are met:
0009  * 1. Redistributions of source code must retain the above copyright
0010  *    notice, this list of conditions and the following disclaimer.
0011  * 2. Redistributions in binary form must reproduce the above copyright
0012  *    notice, this list of conditions and the following disclaimer in the
0013  *    documentation and/or other materials provided with the distribution.
0014  *
0015  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
0016  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0017  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0018  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
0019  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
0020  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
0021  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
0022  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
0023  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
0024  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
0025  * SUCH DAMAGE.
0026  */
0027 
0028 /* Based on:
0029  * SHA256-based Unix crypt implementation. Released into the Public Domain by
0030  * Ulrich Drepper <drepper@redhat.com>. */
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>