Back to home page

LXR

 
 

    


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

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
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 COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0016  * AND 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 COPYRIGHT OWNER OR CONTRIBUTORS BE
0019  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0020  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0021  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0022  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0023  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0024  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0025  * POSSIBILITY OF SUCH DAMAGE.
0026  */
0027 
0028 #ifdef HAVE_CONFIG_H
0029 #include "config.h"
0030 #endif
0031 
0032 #include <sha224.h>
0033 #include <sha256.h>
0034 #include <sha384.h>
0035 #include <sha512.h>
0036 #include <sha512t.h>
0037 #include <stdio.h>
0038 #include <string.h>
0039 
0040 #include "tmacros.h"
0041 
0042 #include <rtems/stackchk.h>
0043 
0044 const char rtems_test_name[] = "SHA";
0045 
0046 static const char *const test_vectors[] = {
0047   "abc",
0048   "",
0049   "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
0050   "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"
0051     "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"
0052 };
0053 
0054 static const unsigned char
0055 test_sha224_results[RTEMS_ARRAY_SIZE(test_vectors)][SHA224_DIGEST_LENGTH] = {
0056   {
0057     0x23, 0x09, 0x7d, 0x22, 0x34, 0x05, 0xd8, 0x22,
0058     0x86, 0x42, 0xa4, 0x77, 0xbd, 0xa2, 0x55, 0xb3,
0059     0x2a, 0xad, 0xbc, 0xe4, 0xbd, 0xa0, 0xb3, 0xf7,
0060     0xe3, 0x6c, 0x9d, 0xa7
0061   }, {
0062     0xd1, 0x4a, 0x02, 0x8c, 0x2a, 0x3a, 0x2b, 0xc9,
0063     0x47, 0x61, 0x02, 0xbb, 0x28, 0x82, 0x34, 0xc4,
0064     0x15, 0xa2, 0xb0, 0x1f, 0x82, 0x8e, 0xa6, 0x2a,
0065     0xc5, 0xb3, 0xe4, 0x2f
0066   }, {
0067     0x75, 0x38, 0x8b, 0x16, 0x51, 0x27, 0x76, 0xcc,
0068     0x5d, 0xba, 0x5d, 0xa1, 0xfd, 0x89, 0x01, 0x50,
0069     0xb0, 0xc6, 0x45, 0x5c, 0xb4, 0xf5, 0x8b, 0x19,
0070     0x52, 0x52, 0x25, 0x25
0071   }, {
0072     0xc9, 0x7c, 0xa9, 0xa5, 0x59, 0x85, 0x0c, 0xe9,
0073     0x7a, 0x04, 0xa9, 0x6d, 0xef, 0x6d, 0x99, 0xa9,
0074     0xe0, 0xe0, 0xe2, 0xab, 0x14, 0xe6, 0xb8, 0xdf,
0075     0x26, 0x5f, 0xc0, 0xb3
0076   }
0077 };
0078 
0079 static const unsigned char
0080 test_sha256_results[RTEMS_ARRAY_SIZE(test_vectors)][SHA256_DIGEST_LENGTH] = {
0081   {
0082     0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea,
0083     0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
0084     0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
0085     0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad
0086   }, {
0087     0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14,
0088     0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,
0089     0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
0090     0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55
0091   }, {
0092     0x24, 0x8d, 0x6a, 0x61, 0xd2, 0x06, 0x38, 0xb8,
0093     0xe5, 0xc0, 0x26, 0x93, 0x0c, 0x3e, 0x60, 0x39,
0094     0xa3, 0x3c, 0xe4, 0x59, 0x64, 0xff, 0x21, 0x67,
0095     0xf6, 0xec, 0xed, 0xd4, 0x19, 0xdb, 0x06, 0xc1
0096   }, {
0097     0xcf, 0x5b, 0x16, 0xa7, 0x78, 0xaf, 0x83, 0x80,
0098     0x03, 0x6c, 0xe5, 0x9e, 0x7b, 0x04, 0x92, 0x37,
0099     0x0b, 0x24, 0x9b, 0x11, 0xe8, 0xf0, 0x7a, 0x51,
0100     0xaf, 0xac, 0x45, 0x03, 0x7a, 0xfe, 0xe9, 0xd1
0101   }
0102 };
0103 
0104 static const unsigned char
0105 test_sha384_results[RTEMS_ARRAY_SIZE(test_vectors)][SHA384_DIGEST_LENGTH] = {
0106   {
0107     0xcb, 0x00, 0x75, 0x3f, 0x45, 0xa3, 0x5e, 0x8b,
0108     0xb5, 0xa0, 0x3d, 0x69, 0x9a, 0xc6, 0x50, 0x07,
0109     0x27, 0x2c, 0x32, 0xab, 0x0e, 0xde, 0xd1, 0x63,
0110     0x1a, 0x8b, 0x60, 0x5a, 0x43, 0xff, 0x5b, 0xed,
0111     0x80, 0x86, 0x07, 0x2b, 0xa1, 0xe7, 0xcc, 0x23,
0112     0x58, 0xba, 0xec, 0xa1, 0x34, 0xc8, 0x25, 0xa7
0113   }, {
0114     0x38, 0xb0, 0x60, 0xa7, 0x51, 0xac, 0x96, 0x38,
0115     0x4c, 0xd9, 0x32, 0x7e, 0xb1, 0xb1, 0xe3, 0x6a,
0116     0x21, 0xfd, 0xb7, 0x11, 0x14, 0xbe, 0x07, 0x43,
0117     0x4c, 0x0c, 0xc7, 0xbf, 0x63, 0xf6, 0xe1, 0xda,
0118     0x27, 0x4e, 0xde, 0xbf, 0xe7, 0x6f, 0x65, 0xfb,
0119     0xd5, 0x1a, 0xd2, 0xf1, 0x48, 0x98, 0xb9, 0x5b
0120   }, {
0121     0x33, 0x91, 0xfd, 0xdd, 0xfc, 0x8d, 0xc7, 0x39,
0122     0x37, 0x07, 0xa6, 0x5b, 0x1b, 0x47, 0x09, 0x39,
0123     0x7c, 0xf8, 0xb1, 0xd1, 0x62, 0xaf, 0x05, 0xab,
0124     0xfe, 0x8f, 0x45, 0x0d, 0xe5, 0xf3, 0x6b, 0xc6,
0125     0xb0, 0x45, 0x5a, 0x85, 0x20, 0xbc, 0x4e, 0x6f,
0126     0x5f, 0xe9, 0x5b, 0x1f, 0xe3, 0xc8, 0x45, 0x2b
0127   }, {
0128     0x09, 0x33, 0x0c, 0x33, 0xf7, 0x11, 0x47, 0xe8,
0129     0x3d, 0x19, 0x2f, 0xc7, 0x82, 0xcd, 0x1b, 0x47,
0130     0x53, 0x11, 0x1b, 0x17, 0x3b, 0x3b, 0x05, 0xd2,
0131     0x2f, 0xa0, 0x80, 0x86, 0xe3, 0xb0, 0xf7, 0x12,
0132     0xfc, 0xc7, 0xc7, 0x1a, 0x55, 0x7e, 0x2d, 0xb9,
0133     0x66, 0xc3, 0xe9, 0xfa, 0x91, 0x74, 0x60, 0x39
0134   }
0135 };
0136 
0137 static const unsigned char
0138 test_sha512_results[RTEMS_ARRAY_SIZE(test_vectors)][SHA512_DIGEST_LENGTH] = {
0139   {
0140     0xdd, 0xaf, 0x35, 0xa1, 0x93, 0x61, 0x7a, 0xba,
0141     0xcc, 0x41, 0x73, 0x49, 0xae, 0x20, 0x41, 0x31,
0142     0x12, 0xe6, 0xfa, 0x4e, 0x89, 0xa9, 0x7e, 0xa2,
0143     0x0a, 0x9e, 0xee, 0xe6, 0x4b, 0x55, 0xd3, 0x9a,
0144     0x21, 0x92, 0x99, 0x2a, 0x27, 0x4f, 0xc1, 0xa8,
0145     0x36, 0xba, 0x3c, 0x23, 0xa3, 0xfe, 0xeb, 0xbd,
0146     0x45, 0x4d, 0x44, 0x23, 0x64, 0x3c, 0xe8, 0x0e,
0147     0x2a, 0x9a, 0xc9, 0x4f, 0xa5, 0x4c, 0xa4, 0x9f
0148   }, {
0149     0xcf, 0x83, 0xe1, 0x35, 0x7e, 0xef, 0xb8, 0xbd,
0150     0xf1, 0x54, 0x28, 0x50, 0xd6, 0x6d, 0x80, 0x07,
0151     0xd6, 0x20, 0xe4, 0x05, 0x0b, 0x57, 0x15, 0xdc,
0152     0x83, 0xf4, 0xa9, 0x21, 0xd3, 0x6c, 0xe9, 0xce,
0153     0x47, 0xd0, 0xd1, 0x3c, 0x5d, 0x85, 0xf2, 0xb0,
0154     0xff, 0x83, 0x18, 0xd2, 0x87, 0x7e, 0xec, 0x2f,
0155     0x63, 0xb9, 0x31, 0xbd, 0x47, 0x41, 0x7a, 0x81,
0156     0xa5, 0x38, 0x32, 0x7a, 0xf9, 0x27, 0xda, 0x3e
0157   }, {
0158     0x20, 0x4a, 0x8f, 0xc6, 0xdd, 0xa8, 0x2f, 0x0a,
0159     0x0c, 0xed, 0x7b, 0xeb, 0x8e, 0x08, 0xa4, 0x16,
0160     0x57, 0xc1, 0x6e, 0xf4, 0x68, 0xb2, 0x28, 0xa8,
0161     0x27, 0x9b, 0xe3, 0x31, 0xa7, 0x03, 0xc3, 0x35,
0162     0x96, 0xfd, 0x15, 0xc1, 0x3b, 0x1b, 0x07, 0xf9,
0163     0xaa, 0x1d, 0x3b, 0xea, 0x57, 0x78, 0x9c, 0xa0,
0164     0x31, 0xad, 0x85, 0xc7, 0xa7, 0x1d, 0xd7, 0x03,
0165     0x54, 0xec, 0x63, 0x12, 0x38, 0xca, 0x34, 0x45
0166   }, {
0167     0x8e, 0x95, 0x9b, 0x75, 0xda, 0xe3, 0x13, 0xda,
0168     0x8c, 0xf4, 0xf7, 0x28, 0x14, 0xfc, 0x14, 0x3f,
0169     0x8f, 0x77, 0x79, 0xc6, 0xeb, 0x9f, 0x7f, 0xa1,
0170     0x72, 0x99, 0xae, 0xad, 0xb6, 0x88, 0x90, 0x18,
0171     0x50, 0x1d, 0x28, 0x9e, 0x49, 0x00, 0xf7, 0xe4,
0172     0x33, 0x1b, 0x99, 0xde, 0xc4, 0xb5, 0x43, 0x3a,
0173     0xc7, 0xd3, 0x29, 0xee, 0xb6, 0xdd, 0x26, 0x54,
0174     0x5e, 0x96, 0xe5, 0x5b, 0x87, 0x4b, 0xe9, 0x09
0175   }
0176 };
0177 
0178 static const unsigned char test_sha512_224_results
0179 [RTEMS_ARRAY_SIZE(test_vectors)][SHA512_224_DIGEST_LENGTH] = {
0180   {
0181     0x46, 0x34, 0x27, 0x0f, 0x70, 0x7b, 0x6a, 0x54,
0182     0xda, 0xae, 0x75, 0x30, 0x46, 0x08, 0x42, 0xe2,
0183     0x0e, 0x37, 0xed, 0x26, 0x5c, 0xee, 0xe9, 0xa4,
0184     0x3e, 0x89, 0x24, 0xaa
0185   }, {
0186     0x6e, 0xd0, 0xdd, 0x02, 0x80, 0x6f, 0xa8, 0x9e,
0187     0x25, 0xde, 0x06, 0x0c, 0x19, 0xd3, 0xac, 0x86,
0188     0xca, 0xbb, 0x87, 0xd6, 0xa0, 0xdd, 0xd0, 0x5c,
0189     0x33, 0x3b, 0x84, 0xf4
0190   }, {
0191     0xe5, 0x30, 0x2d, 0x6d, 0x54, 0xbb, 0x24, 0x22,
0192     0x75, 0xd1, 0xe7, 0x62, 0x2d, 0x68, 0xdf, 0x6e,
0193     0xb0, 0x2d, 0xed, 0xd1, 0x3f, 0x56, 0x4c, 0x13,
0194     0xdb, 0xda, 0x21, 0x74
0195   }, {
0196     0x23, 0xfe, 0xc5, 0xbb, 0x94, 0xd6, 0x0b, 0x23,
0197     0x30, 0x81, 0x92, 0x64, 0x0b, 0x0c, 0x45, 0x33,
0198     0x35, 0xd6, 0x64, 0x73, 0x4f, 0xe4, 0x0e, 0x72,
0199     0x68, 0x67, 0x4a, 0xf9
0200   }
0201 };
0202 
0203 static const unsigned char test_sha512_256_results
0204 [RTEMS_ARRAY_SIZE(test_vectors)][SHA512_256_DIGEST_LENGTH] = {
0205   {
0206     0x53, 0x04, 0x8e, 0x26, 0x81, 0x94, 0x1e, 0xf9,
0207     0x9b, 0x2e, 0x29, 0xb7, 0x6b, 0x4c, 0x7d, 0xab,
0208     0xe4, 0xc2, 0xd0, 0xc6, 0x34, 0xfc, 0x6d, 0x46,
0209     0xe0, 0xe2, 0xf1, 0x31, 0x07, 0xe7, 0xaf, 0x23
0210   }, {
0211     0xc6, 0x72, 0xb8, 0xd1, 0xef, 0x56, 0xed, 0x28,
0212     0xab, 0x87, 0xc3, 0x62, 0x2c, 0x51, 0x14, 0x06,
0213     0x9b, 0xdd, 0x3a, 0xd7, 0xb8, 0xf9, 0x73, 0x74,
0214     0x98, 0xd0, 0xc0, 0x1e, 0xce, 0xf0, 0x96, 0x7a
0215   }, {
0216     0xbd, 0xe8, 0xe1, 0xf9, 0xf1, 0x9b, 0xb9, 0xfd,
0217     0x34, 0x06, 0xc9, 0x0e, 0xc6, 0xbc, 0x47, 0xbd,
0218     0x36, 0xd8, 0xad, 0xa9, 0xf1, 0x18, 0x80, 0xdb,
0219     0xc8, 0xa2, 0x2a, 0x70, 0x78, 0xb6, 0xa4, 0x61
0220   }, {
0221     0x39, 0x28, 0xe1, 0x84, 0xfb, 0x86, 0x90, 0xf8,
0222     0x40, 0xda, 0x39, 0x88, 0x12, 0x1d, 0x31, 0xbe,
0223     0x65, 0xcb, 0x9d, 0x3e, 0xf8, 0x3e, 0xe6, 0x14,
0224     0x6f, 0xea, 0xc8, 0x61, 0xe1, 0x9b, 0x56, 0x3a
0225   }
0226 };
0227 
0228 static void print_result(const unsigned char *r, size_t n)
0229 {
0230   size_t i;
0231 
0232   for (i = 0; i < n; ++i) {
0233     if (i == 0) {
0234       /* Do nothing */
0235     } else if (i % 32 == 0) {
0236       printf("\n");
0237     } else if (i % 8 == 0) {
0238       printf(" ");
0239     }
0240 
0241     printf("%02x", r[i]);
0242   }
0243 
0244   printf("\n");
0245 }
0246 
0247 static void test_sha224(void)
0248 {
0249   size_t i;
0250 
0251   printf("test SHA224\n");
0252 
0253   for (i = 0; i < RTEMS_ARRAY_SIZE(test_vectors); ++i) {
0254     SHA224_CTX ctx;
0255     unsigned char r[SHA224_DIGEST_LENGTH];
0256     const char *s = test_vectors[i];
0257 
0258     SHA224_Init(&ctx);
0259     SHA224_Update(&ctx, s, strlen(s));
0260     SHA224_Final(r, &ctx);
0261 
0262     print_result(&r[0], sizeof(r));
0263 
0264     rtems_test_assert(
0265       memcmp(&r[0], &test_sha224_results[i][0], sizeof(r)) == 0
0266     );
0267   }
0268 }
0269 
0270 static void test_sha256(void)
0271 {
0272   size_t i;
0273 
0274   printf("test SHA256\n");
0275 
0276   for (i = 0; i < RTEMS_ARRAY_SIZE(test_vectors); ++i) {
0277     SHA256_CTX ctx;
0278     unsigned char r[SHA256_DIGEST_LENGTH];
0279     const char *s = test_vectors[i];
0280 
0281     SHA256_Init(&ctx);
0282     SHA256_Update(&ctx, s, strlen(s));
0283     SHA256_Final(r, &ctx);
0284 
0285     print_result(&r[0], sizeof(r));
0286 
0287     rtems_test_assert(
0288       memcmp(&r[0], &test_sha256_results[i][0], sizeof(r)) == 0
0289     );
0290   }
0291 }
0292 
0293 static void test_sha384(void)
0294 {
0295   size_t i;
0296 
0297   printf("test SHA384\n");
0298 
0299   for (i = 0; i < RTEMS_ARRAY_SIZE(test_vectors); ++i) {
0300     SHA384_CTX ctx;
0301     unsigned char r[SHA384_DIGEST_LENGTH];
0302     const char *s = test_vectors[i];
0303 
0304     SHA384_Init(&ctx);
0305     SHA384_Update(&ctx, s, strlen(s));
0306     SHA384_Final(r, &ctx);
0307 
0308     print_result(&r[0], sizeof(r));
0309 
0310     rtems_test_assert(
0311       memcmp(&r[0], &test_sha384_results[i][0], sizeof(r)) == 0
0312     );
0313   }
0314 }
0315 
0316 static void test_sha512(void)
0317 {
0318   size_t i;
0319 
0320   printf("test SHA512\n");
0321 
0322   for (i = 0; i < RTEMS_ARRAY_SIZE(test_vectors); ++i) {
0323     SHA512_CTX ctx;
0324     unsigned char r[SHA512_DIGEST_LENGTH];
0325     const char *s = test_vectors[i];
0326 
0327     SHA512_Init(&ctx);
0328     SHA512_Update(&ctx, s, strlen(s));
0329     SHA512_Final(r, &ctx);
0330 
0331     print_result(&r[0], sizeof(r));
0332 
0333     rtems_test_assert(
0334       memcmp(&r[0], &test_sha512_results[i][0], sizeof(r)) == 0
0335     );
0336   }
0337 }
0338 
0339 static void test_sha512_224(void)
0340 {
0341   size_t i;
0342 
0343   printf("test SHA512-224\n");
0344 
0345   for (i = 0; i < RTEMS_ARRAY_SIZE(test_vectors); ++i) {
0346     SHA512_CTX ctx;
0347     unsigned char r[SHA512_224_DIGEST_LENGTH];
0348     const char *s = test_vectors[i];
0349 
0350     SHA512_224_Init(&ctx);
0351     SHA512_224_Update(&ctx, s, strlen(s));
0352     SHA512_224_Final(r, &ctx);
0353 
0354     print_result(&r[0], sizeof(r));
0355 
0356     rtems_test_assert(
0357       memcmp(&r[0], &test_sha512_224_results[i][0], sizeof(r)) == 0
0358     );
0359   }
0360 }
0361 
0362 static void test_sha512_256(void)
0363 {
0364   size_t i;
0365 
0366   printf("test SHA512-256\n");
0367 
0368   for (i = 0; i < RTEMS_ARRAY_SIZE(test_vectors); ++i) {
0369     SHA512_CTX ctx;
0370     unsigned char r[SHA512_256_DIGEST_LENGTH];
0371     const char *s = test_vectors[i];
0372 
0373     SHA512_256_Init(&ctx);
0374     SHA512_256_Update(&ctx, s, strlen(s));
0375     SHA512_256_Final(r, &ctx);
0376 
0377     print_result(&r[0], sizeof(r));
0378 
0379     rtems_test_assert(
0380       memcmp(&r[0], &test_sha512_256_results[i][0], sizeof(r)) == 0
0381     );
0382   }
0383 }
0384 
0385 static void Init(rtems_task_argument arg)
0386 {
0387   TEST_BEGIN();
0388 
0389   test_sha224();
0390   test_sha256();
0391   test_sha384();
0392   test_sha512();
0393   test_sha512_224();
0394   test_sha512_256();
0395   rtems_stack_checker_report_usage();
0396 
0397   TEST_END();
0398   rtems_test_exit(0);
0399 }
0400 
0401 #define CONFIGURE_APPLICATION_DOES_NOT_NEED_CLOCK_DRIVER
0402 #define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER
0403 
0404 #define CONFIGURE_MAXIMUM_TASKS 1
0405 
0406 #define CONFIGURE_STACK_CHECKER_ENABLED
0407 
0408 #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
0409 
0410 #define CONFIGURE_RTEMS_INIT_TASKS_TABLE
0411 
0412 /*
0413  * The SHA512_Update() function may need a lot of stack space if the compiler
0414  * optimization is disabled.
0415  */
0416 #define CONFIGURE_INIT_TASK_STACK_SIZE (32 * 1024)
0417 
0418 #define CONFIGURE_INIT
0419 
0420 #include <rtems/confdefs.h>