Back to home page

LXR

 
 

    


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

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /*
0004  * Copyright (c) 2012 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 "tmacros.h"
0033 
0034 #include <md5.h>
0035 
0036 const char rtems_test_name[] = "MD5 1";
0037 
0038 #ifndef MD5_BLOCK_SIZE
0039   #define MD5_BLOCK_SIZE 64
0040 #endif
0041 
0042 #ifndef MD5_DIGEST_LENGTH
0043   #define MD5_DIGEST_LENGTH 16
0044 #endif
0045 
0046 /* Test cases according to RFC 2202 */
0047 
0048 static const uint8_t key_1[] = {
0049   0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
0050   0x0b, 0x0b, 0x0b
0051 };
0052 
0053 static const uint8_t data_1[] = {
0054   'H', 'i', ' ', 'T', 'h', 'e', 'r', 'e'
0055 };
0056 
0057 static const uint8_t digest_1[MD5_DIGEST_LENGTH] = {
0058   0x92, 0x94, 0x72, 0x7a, 0x36, 0x38, 0xbb, 0x1c, 0x13, 0xf4, 0x8e, 0xf8, 0x15,
0059   0x8b, 0xfc, 0x9d
0060 };
0061 
0062 static const uint8_t key_2[] = {
0063   'J', 'e', 'f', 'e'
0064 };
0065 
0066 static const uint8_t data_2[] = {
0067   'w', 'h', 'a', 't', ' ', 'd', 'o', ' ', 'y', 'a', ' ', 'w', 'a', 'n', 't',
0068   ' ', 'f', 'o', 'r', ' ', 'n', 'o', 't', 'h', 'i', 'n', 'g', '?'
0069 };
0070 
0071 static const uint8_t digest_2[MD5_DIGEST_LENGTH] = {
0072   0x75, 0x0c, 0x78, 0x3e, 0x6a, 0xb0, 0xb5, 0x03, 0xea, 0xa8, 0x6e, 0x31, 0x0a,
0073   0x5d, 0xb7, 0x38
0074 };
0075 
0076 static const uint8_t key_3[] = {
0077   0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
0078   0xaa, 0xaa, 0xaa
0079 };
0080 
0081 static const uint8_t data_3[] = {
0082   0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
0083   0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
0084   0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
0085   0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd
0086 };
0087 
0088 static const uint8_t digest_3[MD5_DIGEST_LENGTH] = {
0089   0x56, 0xbe, 0x34, 0x52, 0x1d, 0x14, 0x4c, 0x88, 0xdb, 0xb8, 0xc7, 0x33, 0xf0,
0090   0xe8, 0xb3, 0xf6
0091 };
0092 
0093 static const uint8_t key_4[] = {
0094   0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d,
0095   0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19
0096 };
0097 
0098 static const uint8_t data_4[] = {
0099   0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd,
0100   0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd,
0101   0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd,
0102   0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd
0103 };
0104 
0105 static const uint8_t digest_4[MD5_DIGEST_LENGTH] = {
0106   0x69, 0x7e, 0xaf, 0x0a, 0xca, 0x3a, 0x3a, 0xea, 0x3a, 0x75, 0x16, 0x47, 0x46,
0107   0xff, 0xaa, 0x79
0108 };
0109 
0110 static const uint8_t key_5[] = {
0111   0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,
0112   0x0c, 0x0c, 0x0c
0113 };
0114 
0115 static const uint8_t data_5[] = {
0116   'T', 'e', 's', 't', ' ', 'W', 'i', 't', 'h', ' ', 'T', 'r', 'u', 'n', 'c',
0117   'a', 't', 'i', 'o', 'n'
0118 };
0119 
0120 static const uint8_t digest_5[MD5_DIGEST_LENGTH] = {
0121   0x56, 0x46, 0x1e, 0xf2, 0x34, 0x2e, 0xdc, 0x00, 0xf9, 0xba, 0xb9, 0x95, 0x69,
0122   0x0e, 0xfd, 0x4c
0123 };
0124 
0125 static const uint8_t key_6[] = {
0126   0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
0127   0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
0128   0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
0129   0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
0130   0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
0131   0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
0132   0xaa, 0xaa
0133 };
0134 
0135 static const uint8_t data_6[] = {
0136   'T', 'e', 's', 't', ' ', 'U', 's', 'i', 'n', 'g', ' ', 'L', 'a', 'r', 'g',
0137   'e', 'r', ' ', 'T', 'h', 'a', 'n', ' ', 'B', 'l', 'o', 'c', 'k', '-', 'S',
0138   'i', 'z', 'e', ' ', 'K', 'e', 'y', ' ', '-', ' ', 'H', 'a', 's', 'h', ' ',
0139   'K', 'e', 'y', ' ', 'F', 'i', 'r', 's', 't'
0140 };
0141 
0142 static const uint8_t digest_6[MD5_DIGEST_LENGTH] = {
0143   0x6b, 0x1a, 0xb7, 0xfe, 0x4b, 0xd7, 0xbf, 0x8f, 0x0b, 0x62, 0xe6, 0xce, 0x61,
0144   0xb9, 0xd0, 0xcd
0145 };
0146 
0147 static const uint8_t key_7[] = {
0148   0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
0149   0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
0150   0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
0151   0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
0152   0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
0153   0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
0154   0xaa, 0xaa
0155 };
0156 
0157 static const uint8_t data_7[] = {
0158   'T', 'e', 's', 't', ' ', 'U', 's', 'i', 'n', 'g', ' ', 'L', 'a', 'r', 'g',
0159   'e', 'r', ' ', 'T', 'h', 'a', 'n', ' ', 'B', 'l', 'o', 'c', 'k', '-', 'S',
0160   'i', 'z', 'e', ' ', 'K', 'e', 'y', ' ', 'a', 'n', 'd', ' ', 'L', 'a', 'r',
0161   'g', 'e', 'r', ' ', 'T', 'h', 'a', 'n', ' ', 'O', 'n', 'e', ' ', 'B', 'l',
0162   'o', 'c', 'k', '-', 'S', 'i', 'z', 'e', ' ', 'D', 'a', 't', 'a'
0163 };
0164 
0165 static const uint8_t digest_7[MD5_DIGEST_LENGTH] = {
0166   0x6f, 0x63, 0x0f, 0xad, 0x67, 0xcd, 0xa0, 0xee, 0x1f, 0xb1, 0xf5, 0x62, 0xdb,
0167   0x3a, 0xa5, 0x3e
0168 };
0169 
0170 static void test_md5(
0171  const uint8_t *key,
0172  size_t key_size,
0173  const uint8_t *data,
0174  size_t data_size,
0175  const uint8_t expected_digest[MD5_DIGEST_LENGTH]
0176 )
0177 {
0178   MD5_CTX inner_ctx;
0179   MD5_CTX outer_ctx;
0180   uint8_t inner_digest[MD5_DIGEST_LENGTH];
0181   uint8_t outer_digest[MD5_DIGEST_LENGTH];
0182   uint8_t key_buf[MD5_DIGEST_LENGTH];
0183   uint8_t buf[MD5_BLOCK_SIZE];
0184   size_t i;
0185 
0186   if (key_size > MD5_BLOCK_SIZE) {
0187     MD5_CTX tmp_ctx;
0188 
0189     MD5Init(&tmp_ctx);
0190     MD5Update(&tmp_ctx, key, key_size);
0191     MD5Final(&key_buf[0], &tmp_ctx);
0192 
0193     key = &key_buf[0];
0194     key_size = MD5_DIGEST_LENGTH;
0195   }
0196 
0197   /* Inner digest */
0198 
0199   MD5Init(&inner_ctx);
0200 
0201   /* Pad the key for the inner digest */
0202   for (i = 0; i < key_size; ++i) {
0203     buf[i] = key[i] ^ 0x36;
0204   }
0205   for (i = key_size; i < MD5_BLOCK_SIZE; ++i) {
0206     buf[i] = 0x36;
0207   }
0208 
0209   MD5Update(&inner_ctx, &buf[0], MD5_BLOCK_SIZE);
0210   MD5Update(&inner_ctx, data, data_size);
0211 
0212   MD5Final(&inner_digest[0], &inner_ctx);
0213 
0214   /* Outer digest */
0215 
0216   MD5Init(&outer_ctx);
0217 
0218   /* Pad the key for outer digest */
0219   for (i = 0; i < key_size; ++i) {
0220     buf[i] = key[i] ^ 0x5c;
0221   }
0222   for (i = key_size; i < MD5_BLOCK_SIZE; ++i) {
0223     buf[i] = 0x5c;
0224   }
0225 
0226   MD5Update(&outer_ctx, &buf[0], MD5_BLOCK_SIZE);
0227   MD5Update(&outer_ctx, &inner_digest[0], MD5_DIGEST_LENGTH);
0228 
0229   MD5Final(&outer_digest[0], &outer_ctx);
0230 
0231   rtems_test_assert(
0232     memcmp(&outer_digest[0], expected_digest, sizeof(outer_digest)) == 0
0233   );
0234 }
0235 
0236 static void test(void)
0237 {
0238   test_md5(&key_1[0], sizeof(key_1), &data_1[0], sizeof(data_1), &digest_1[0]);
0239   test_md5(&key_2[0], sizeof(key_2), &data_2[0], sizeof(data_2), &digest_2[0]);
0240   test_md5(&key_3[0], sizeof(key_3), &data_3[0], sizeof(data_3), &digest_3[0]);
0241   test_md5(&key_4[0], sizeof(key_4), &data_4[0], sizeof(data_4), &digest_4[0]);
0242   test_md5(&key_5[0], sizeof(key_5), &data_5[0], sizeof(data_5), &digest_5[0]);
0243   test_md5(&key_6[0], sizeof(key_6), &data_6[0], sizeof(data_6), &digest_6[0]);
0244   test_md5(&key_7[0], sizeof(key_7), &data_7[0], sizeof(data_7), &digest_7[0]);
0245 }
0246 
0247 static void Init(rtems_task_argument arg)
0248 {
0249   TEST_BEGIN();
0250 
0251   test();
0252 
0253   TEST_END();
0254 
0255   rtems_test_exit(0);
0256 }
0257 
0258 #define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
0259 #define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER
0260 
0261 #define CONFIGURE_MAXIMUM_TASKS 1
0262 
0263 #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
0264 
0265 #define CONFIGURE_RTEMS_INIT_TASKS_TABLE
0266 
0267 #define CONFIGURE_INIT
0268 
0269 #include <rtems/confdefs.h>