summaryrefslogtreecommitdiff
path: root/cesar/cp/secu/test/src/test-nmk.c
blob: 644047f99b5c2ae7d0ffbe594f54b1ba32799a3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/* Cesar project {{{
 *
 * Copyright (C) 2008 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    src/test-nmk.c
 * \brief   Test the generation of the NMK
 * \ingroup cp_secu
 *
 * Test the generation of the NMK with the data provided in section 13.5 table
 * 13-342.
 * Entry buffer "HomePlugAV0123"
 * NMK 0xb59319d7e8157ba001b018669cce
 */
#include "common/std.h"
#include "string.h"
#include "stdio.h"
#include "lib/test.h"

#include "cp/secu/secu.h"
#include "cp/secu/defs.h"
#include "cp/secu/sha256.h"

#include "cp/secu/inc/pbkdf1.h"

uint req [] = {0x03020100, 0x07060504, 0x0b0a0908, 0x0f0e0d0c,
    0x13121110, 0x17161514, 0x1b1a1918, 0x1f1e1d1c, 0x23222120,
    0x27262524, 0x2b2a2928, 0x2f2e2d2c, 0x33323130, 0x37363534,
    0x3b3a3938, 0x3f3e3d3c, 0x43424140, 0x47464544, 0x4b4a4948,
    0x4f4e4d4c, 0x53525150, 0x57565554, 0x5b5a5958, 0x5f5e5d5c,
    0x63626160, 0x67666564, 0x6b6a6968, 0x6f6e6d6c, 0x73727170,
    0x77767574, 0x7b7a7978, 0x7f7e7d7c, 0x83828180, 0x87868584,
    0x8b8a8988, 0x8f8e8d8c, 0x93929190, 0x97969594, 0x9b9a9998,
    0x9f9e9d9c, 0xa3a2a1a0, 0xa7a6a5a4, 0xabaaa9a8, 0xafaeadac,
    0xb3b2b1b0, 0xb7b6b5b4, 0xbbbab9b8, 0xbfbebdbc, 0xc3c2c1c0,
    0xc7c6c5c4, 0xcbcac9c8, 0xcfcecdcc, 0xd3d2d1d0, 0xd7d6d5d4,
    0xdbdad9d8, 0xdfdedddc, 0xe3e2e1e0, 0xe7e6e5e4, 0xebeae9e8,
    0xefeeedec, 0xf3f2f1f0, 0xf7f6f5f4, 0xfbfaf9f8, 0xfffefdfc,
    0x03020100, 0x07060504, 0x0b0a0908, 0x0f0e0d0c, 0x13121110,
    0x17161514, 0x1b1a1918, 0x1f1e1d1c, 0x23222120, 0x27262524,
    0x2b2a2928, 0x2f2e2d2c, 0x33323130, 0x37363534, 0x3b3a3938,
    0x3f3e3d3c, 0x43424140, 0x47464544, 0x4b4a4948, 0x4f4e4d4c,
    0x53525150, 0x57565554, 0x5b5a5958, 0x5f5e5d5c, 0x63626160,
    0x67666564, 0x6b6a6968, 0x6f6e6d6c, 0x73727170, 0x77767574,
    0x7b7a7978, 0x7f7e7d7c};

uint cnf [] = {0xfcfdfeff, 0xf8f9fafb, 0xf4f5f6f7, 0xf0f1f2f3,
    0xecedeeef, 0xe8e9eaeb, 0xe4e5e6e7, 0xe0e1e2e3, 0xdcdddedf,
    0xd8d9dadb, 0xd4d5d6d7, 0xd0d1d2d3, 0xcccdcecf, 0xc8c9cacb,
    0xc4c5c6c7, 0xc0c1c2c3, 0xbcbdbebf, 0xb8b9babb, 0xb4b5b6b7,
    0xb0b1b2b3, 0xacadaeaf, 0xa8a9aaab, 0xa4a5a6a7, 0xa0a1a2a3,
    0x9c9d9e9f, 0x98999a9b, 0x94959697, 0x90919293, 0x8c8d8e8f,
    0x88898a8b, 0x84858687, 0x80818283, 0x7c7d7e7f, 0x78797a7b,
    0x74757677, 0x70717273, 0x6c6d6e6f, 0x68696a6b, 0x64656667,
    0x60616263, 0x5c5d5e5f, 0x58595a5b, 0x54555657, 0x50515253,
    0x4c4d4e4f, 0x48494a4b, 0x44454647, 0x40414243, 0x3c3d3e3f,
    0x38393a3b, 0x34353637, 0x30313233, 0x2c2d2e2f, 0x28292a2b,
    0x24252627, 0x20212223, 0x1c1d1e1f, 0x18191a1b, 0x14151617,
    0x10111213, 0x0c0d0e0f, 0x08090a0b, 0x04050607, 0x00010203,
    0xfcfdfeff, 0xf8f9fafb, 0xf4f5f6f7, 0xf0f1f2f3, 0xecedeeef,
    0xe8e9eaeb, 0xe4e5e6e7, 0xe0e1e2e3, 0xdcdddedf, 0xd8d9dadb,
    0xd4d5d6d7, 0xd0d1d2d3, 0xcccdcecf, 0xc8c9cacb, 0xc4c5c6c7,
    0xc0c1c2c3, 0xbcbdbebf, 0xb8b9babb, 0xb4b5b6b7, 0xb0b1b2b3,
    0xacadaeaf, 0xa8a9aaab, 0xa4a5a6a7, 0xa0a1a2a3, 0x9c9d9e9f,
    0x98999a9b, 0x94959697, 0x90919293, 0x8c8d8e8f, 0x88898a8b,
    0x84858687, 0x80818283};


void
test_case_nmk_generation (test_t test)
{
    uint ctx;
    cp_key_t nmk;
    u8 buffer[64] __attribute__((aligned(64))) = "HomePlugAV0123\0";

    nmk = cp_secu_npw2nmk ((cp_t*) &ctx, buffer, 14);

    test_case_begin (test, "NMK generation");

    test_begin (test, "verify")
    {
        test_fail_if (nmk.key[0] != 0xd71993b5);
        test_fail_if (nmk.key[1] != 0xa07b15e8);
        test_fail_if (nmk.key[2] != 0x6618b001);
        test_fail_if (nmk.key[3] != 0x0de3ce9c);
    }
    test_end;
}

void
test_case_dak_generation (test_t test)
{
    uint ctx;
    cp_key_t dak;
    u8 buffer[64] __attribute__((aligned(64)));

    strcpy ((char *)buffer, "DAK_Password\0");

    dak = cp_secu_dak_gen ((cp_t*) &ctx, buffer, 12);

    test_case_begin (test, "DAK generation");

    test_begin (test, "verify")
    {
        test_fail_if (dak.key[0] != 0x88577fee);
        test_fail_if (dak.key[1] != 0xc921a0e2);
        test_fail_if (dak.key[2] != 0xc59a4699);
        test_fail_if (dak.key[3] != 0x060af32a);
    }
    test_end;
}

void
test_case_nid_generation (test_t test)
{
    cp_key_t nmk;
    u64 nid;
    uint ctx;

    nmk.key[0] = 0xd71993b5;
    nmk.key[1] = 0xa07b15e8;
    nmk.key[2] = 0x6618b001;
    nmk.key[3] = 0x0de3ce9c;

    nid = cp_secu_nmk2nid ((cp_t*) &ctx, nmk, 0x1);

    test_case_begin (test, "NID generation");

    test_begin (test, "verify")
    {
        test_fail_if (nid != 0x184e35a5cb6b02ull);
    }
    test_end;
}

void
test_case_tek_generation (test_t test)
{
    test_case_begin (test, "Tek generation");

    /* Using example section 13.6.2. */
    test_begin (test, "TEK")
    {
        uint i;
        u32 res[4] = {0x2d3b6a36, 0xddc60f8a, 0x56c5e8ca, 0xeb4b7d36};
        cp_key_t tek;

        cp_secu_tek_gen (req, cnf, &tek);

        for (i = 0; i < 4; i++)
            test_fail_unless (tek.key[i] == res[i]);

    }
    test_end;
}

void
test_case_nek_generation (test_t test)
{
    test_case_begin (test, "Generate NEK");

    test_begin (test, "nek")
    {
        cp_key_t output;
        uint ctx;

        cp_secu_generate_key ((cp_t*) &ctx, 0x12345678,
                             CP_SECU_PBKDF1_ITERATION_NEK, &output);

        test_fail_unless (output.key[0] == 0xcb268d65);
        test_fail_unless (output.key[1] == 0x5117d2d0);
        test_fail_unless (output.key[2] == 0xdf639617);
        test_fail_unless (output.key[3] == 0x130f3722);
    }
    test_end;
}

int
main (void)
{
    test_t test;

    test_init (test, 0, NULL);

    test_case_nmk_generation (test);
    test_case_dak_generation (test);
    test_case_nid_generation (test);
    test_case_tek_generation (test);
    test_case_nek_generation (test);

    test_result (test);
    return test_nb_failed (test) == 0 ? 0 : 1;
}