diff options
author | Johan Hedberg <johan.hedberg@intel.com> | 2014-12-29 23:48:36 -0500 |
---|---|---|
committer | Johan Hedberg <johan.hedberg@intel.com> | 2014-12-30 02:46:57 -0500 |
commit | 0b6415b65234ff723d32fd7dcdd917ba7ad5fe86 (patch) | |
tree | 3b2c11f721f4ae9e7a270fd138aafc51d01a5005 | |
parent | ee485290c6af942f0371def632c32e747d110b1e (diff) |
Bluetooth: Add support for ECDH test cases
This patch adds the test cases for ECDH cryptographic functionality
used by Bluetooth Low Energy Secure Connections feature.
Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
Signed-off-by: Marcel Holtmann <marcel@holtmann.org>
-rw-r--r-- | net/bluetooth/Kconfig | 7 | ||||
-rw-r--r-- | net/bluetooth/selftest.c | 169 |
2 files changed, 175 insertions, 1 deletions
diff --git a/net/bluetooth/Kconfig b/net/bluetooth/Kconfig index 3296841531c9..8d3b607a72be 100644 --- a/net/bluetooth/Kconfig +++ b/net/bluetooth/Kconfig | |||
@@ -77,4 +77,11 @@ config BT_SELFTEST | |||
77 | subsystem is compiled into the kernel image, then the test cases | 77 | subsystem is compiled into the kernel image, then the test cases |
78 | are run late in the initcall hierarchy. | 78 | are run late in the initcall hierarchy. |
79 | 79 | ||
80 | config BT_SELFTEST_ECDH | ||
81 | bool "ECDH test cases" | ||
82 | depends on BT_LE && BT_SELFTEST | ||
83 | help | ||
84 | Run test cases for ECDH cryptographic functionality used by the | ||
85 | Bluetooth Low Energy Secure Connections feature. | ||
86 | |||
80 | source "drivers/bluetooth/Kconfig" | 87 | source "drivers/bluetooth/Kconfig" |
diff --git a/net/bluetooth/selftest.c b/net/bluetooth/selftest.c index 00cac4a5e229..b9cb33cd45d7 100644 --- a/net/bluetooth/selftest.c +++ b/net/bluetooth/selftest.c | |||
@@ -23,15 +23,182 @@ | |||
23 | 23 | ||
24 | #include <net/bluetooth/bluetooth.h> | 24 | #include <net/bluetooth/bluetooth.h> |
25 | 25 | ||
26 | #include "ecc.h" | ||
26 | #include "selftest.h" | 27 | #include "selftest.h" |
27 | 28 | ||
29 | #if IS_ENABLED(CONFIG_BT_SELFTEST_ECDH) | ||
30 | |||
31 | static const u8 priv_a_1[32] __initconst = { | ||
32 | 0xbd, 0x1a, 0x3c, 0xcd, 0xa6, 0xb8, 0x99, 0x58, | ||
33 | 0x99, 0xb7, 0x40, 0xeb, 0x7b, 0x60, 0xff, 0x4a, | ||
34 | 0x50, 0x3f, 0x10, 0xd2, 0xe3, 0xb3, 0xc9, 0x74, | ||
35 | 0x38, 0x5f, 0xc5, 0xa3, 0xd4, 0xf6, 0x49, 0x3f, | ||
36 | }; | ||
37 | static const u8 priv_b_1[32] __initconst = { | ||
38 | 0xfd, 0xc5, 0x7f, 0xf4, 0x49, 0xdd, 0x4f, 0x6b, | ||
39 | 0xfb, 0x7c, 0x9d, 0xf1, 0xc2, 0x9a, 0xcb, 0x59, | ||
40 | 0x2a, 0xe7, 0xd4, 0xee, 0xfb, 0xfc, 0x0a, 0x90, | ||
41 | 0x9a, 0xbb, 0xf6, 0x32, 0x3d, 0x8b, 0x18, 0x55, | ||
42 | }; | ||
43 | static const u8 pub_a_1[64] __initconst = { | ||
44 | 0xe6, 0x9d, 0x35, 0x0e, 0x48, 0x01, 0x03, 0xcc, | ||
45 | 0xdb, 0xfd, 0xf4, 0xac, 0x11, 0x91, 0xf4, 0xef, | ||
46 | 0xb9, 0xa5, 0xf9, 0xe9, 0xa7, 0x83, 0x2c, 0x5e, | ||
47 | 0x2c, 0xbe, 0x97, 0xf2, 0xd2, 0x03, 0xb0, 0x20, | ||
48 | |||
49 | 0x8b, 0xd2, 0x89, 0x15, 0xd0, 0x8e, 0x1c, 0x74, | ||
50 | 0x24, 0x30, 0xed, 0x8f, 0xc2, 0x45, 0x63, 0x76, | ||
51 | 0x5c, 0x15, 0x52, 0x5a, 0xbf, 0x9a, 0x32, 0x63, | ||
52 | 0x6d, 0xeb, 0x2a, 0x65, 0x49, 0x9c, 0x80, 0xdc, | ||
53 | }; | ||
54 | static const u8 pub_b_1[64] __initconst = { | ||
55 | 0x90, 0xa1, 0xaa, 0x2f, 0xb2, 0x77, 0x90, 0x55, | ||
56 | 0x9f, 0xa6, 0x15, 0x86, 0xfd, 0x8a, 0xb5, 0x47, | ||
57 | 0x00, 0x4c, 0x9e, 0xf1, 0x84, 0x22, 0x59, 0x09, | ||
58 | 0x96, 0x1d, 0xaf, 0x1f, 0xf0, 0xf0, 0xa1, 0x1e, | ||
59 | |||
60 | 0x4a, 0x21, 0xb1, 0x15, 0xf9, 0xaf, 0x89, 0x5f, | ||
61 | 0x76, 0x36, 0x8e, 0xe2, 0x30, 0x11, 0x2d, 0x47, | ||
62 | 0x60, 0x51, 0xb8, 0x9a, 0x3a, 0x70, 0x56, 0x73, | ||
63 | 0x37, 0xad, 0x9d, 0x42, 0x3e, 0xf3, 0x55, 0x4c, | ||
64 | }; | ||
65 | static const u8 dhkey_1[32] __initconst = { | ||
66 | 0x98, 0xa6, 0xbf, 0x73, 0xf3, 0x34, 0x8d, 0x86, | ||
67 | 0xf1, 0x66, 0xf8, 0xb4, 0x13, 0x6b, 0x79, 0x99, | ||
68 | 0x9b, 0x7d, 0x39, 0x0a, 0xa6, 0x10, 0x10, 0x34, | ||
69 | 0x05, 0xad, 0xc8, 0x57, 0xa3, 0x34, 0x02, 0xec, | ||
70 | }; | ||
71 | |||
72 | static const u8 priv_a_2[32] __initconst = { | ||
73 | 0x63, 0x76, 0x45, 0xd0, 0xf7, 0x73, 0xac, 0xb7, | ||
74 | 0xff, 0xdd, 0x03, 0x72, 0xb9, 0x72, 0x85, 0xb4, | ||
75 | 0x41, 0xb6, 0x5d, 0x0c, 0x5d, 0x54, 0x84, 0x60, | ||
76 | 0x1a, 0xa3, 0x9a, 0x3c, 0x69, 0x16, 0xa5, 0x06, | ||
77 | }; | ||
78 | static const u8 priv_b_2[32] __initconst = { | ||
79 | 0xba, 0x30, 0x55, 0x50, 0x19, 0xa2, 0xca, 0xa3, | ||
80 | 0xa5, 0x29, 0x08, 0xc6, 0xb5, 0x03, 0x88, 0x7e, | ||
81 | 0x03, 0x2b, 0x50, 0x73, 0xd4, 0x2e, 0x50, 0x97, | ||
82 | 0x64, 0xcd, 0x72, 0x0d, 0x67, 0xa0, 0x9a, 0x52, | ||
83 | }; | ||
84 | static const u8 pub_a_2[64] __initconst = { | ||
85 | 0xdd, 0x78, 0x5c, 0x74, 0x03, 0x9b, 0x7e, 0x98, | ||
86 | 0xcb, 0x94, 0x87, 0x4a, 0xad, 0xfa, 0xf8, 0xd5, | ||
87 | 0x43, 0x3e, 0x5c, 0xaf, 0xea, 0xb5, 0x4c, 0xf4, | ||
88 | 0x9e, 0x80, 0x79, 0x57, 0x7b, 0xa4, 0x31, 0x2c, | ||
89 | |||
90 | 0x4f, 0x5d, 0x71, 0x43, 0x77, 0x43, 0xf8, 0xea, | ||
91 | 0xd4, 0x3e, 0xbd, 0x17, 0x91, 0x10, 0x21, 0xd0, | ||
92 | 0x1f, 0x87, 0x43, 0x8e, 0x40, 0xe2, 0x52, 0xcd, | ||
93 | 0xbe, 0xdf, 0x98, 0x38, 0x18, 0x12, 0x95, 0x91, | ||
94 | }; | ||
95 | static const u8 pub_b_2[64] __initconst = { | ||
96 | 0xcc, 0x00, 0x65, 0xe1, 0xf5, 0x6c, 0x0d, 0xcf, | ||
97 | 0xec, 0x96, 0x47, 0x20, 0x66, 0xc9, 0xdb, 0x84, | ||
98 | 0x81, 0x75, 0xa8, 0x4d, 0xc0, 0xdf, 0xc7, 0x9d, | ||
99 | 0x1b, 0x3f, 0x3d, 0xf2, 0x3f, 0xe4, 0x65, 0xf4, | ||
100 | |||
101 | 0x79, 0xb2, 0xec, 0xd8, 0xca, 0x55, 0xa1, 0xa8, | ||
102 | 0x43, 0x4d, 0x6b, 0xca, 0x10, 0xb0, 0xc2, 0x01, | ||
103 | 0xc2, 0x33, 0x4e, 0x16, 0x24, 0xc4, 0xef, 0xee, | ||
104 | 0x99, 0xd8, 0xbb, 0xbc, 0x48, 0xd0, 0x01, 0x02, | ||
105 | }; | ||
106 | static const u8 dhkey_2[32] __initconst = { | ||
107 | 0x69, 0xeb, 0x21, 0x32, 0xf2, 0xc6, 0x05, 0x41, | ||
108 | 0x60, 0x19, 0xcd, 0x5e, 0x94, 0xe1, 0xe6, 0x5f, | ||
109 | 0x33, 0x07, 0xe3, 0x38, 0x4b, 0x68, 0xe5, 0x62, | ||
110 | 0x3f, 0x88, 0x6d, 0x2f, 0x3a, 0x84, 0x85, 0xab, | ||
111 | }; | ||
112 | |||
113 | static const u8 priv_a_3[32] __initconst = { | ||
114 | 0xbd, 0x1a, 0x3c, 0xcd, 0xa6, 0xb8, 0x99, 0x58, | ||
115 | 0x99, 0xb7, 0x40, 0xeb, 0x7b, 0x60, 0xff, 0x4a, | ||
116 | 0x50, 0x3f, 0x10, 0xd2, 0xe3, 0xb3, 0xc9, 0x74, | ||
117 | 0x38, 0x5f, 0xc5, 0xa3, 0xd4, 0xf6, 0x49, 0x3f, | ||
118 | }; | ||
119 | static const u8 pub_a_3[64] __initconst = { | ||
120 | 0xe6, 0x9d, 0x35, 0x0e, 0x48, 0x01, 0x03, 0xcc, | ||
121 | 0xdb, 0xfd, 0xf4, 0xac, 0x11, 0x91, 0xf4, 0xef, | ||
122 | 0xb9, 0xa5, 0xf9, 0xe9, 0xa7, 0x83, 0x2c, 0x5e, | ||
123 | 0x2c, 0xbe, 0x97, 0xf2, 0xd2, 0x03, 0xb0, 0x20, | ||
124 | |||
125 | 0x8b, 0xd2, 0x89, 0x15, 0xd0, 0x8e, 0x1c, 0x74, | ||
126 | 0x24, 0x30, 0xed, 0x8f, 0xc2, 0x45, 0x63, 0x76, | ||
127 | 0x5c, 0x15, 0x52, 0x5a, 0xbf, 0x9a, 0x32, 0x63, | ||
128 | 0x6d, 0xeb, 0x2a, 0x65, 0x49, 0x9c, 0x80, 0xdc, | ||
129 | }; | ||
130 | static const u8 dhkey_3[32] __initconst = { | ||
131 | 0x2d, 0xab, 0x00, 0x48, 0xcb, 0xb3, 0x7b, 0xda, | ||
132 | 0x55, 0x7b, 0x8b, 0x72, 0xa8, 0x57, 0x87, 0xc3, | ||
133 | 0x87, 0x27, 0x99, 0x32, 0xfc, 0x79, 0x5f, 0xae, | ||
134 | 0x7c, 0x1c, 0xf9, 0x49, 0xe6, 0xd7, 0xaa, 0x70, | ||
135 | }; | ||
136 | |||
137 | static int __init test_ecdh_sample(const u8 priv_a[32], const u8 priv_b[32], | ||
138 | const u8 pub_a[64], const u8 pub_b[64], | ||
139 | const u8 dhkey[32]) | ||
140 | { | ||
141 | u8 dhkey_a[32], dhkey_b[32]; | ||
142 | |||
143 | ecdh_shared_secret(pub_b, priv_a, dhkey_a); | ||
144 | ecdh_shared_secret(pub_a, priv_b, dhkey_b); | ||
145 | |||
146 | if (memcmp(dhkey_a, dhkey, 32)) | ||
147 | return -EINVAL; | ||
148 | |||
149 | if (memcmp(dhkey_b, dhkey, 32)) | ||
150 | return -EINVAL; | ||
151 | |||
152 | return 0; | ||
153 | } | ||
154 | |||
155 | static int __init test_ecdh(void) | ||
156 | { | ||
157 | int err; | ||
158 | |||
159 | err = test_ecdh_sample(priv_a_1, priv_b_1, pub_a_1, pub_b_1, dhkey_1); | ||
160 | if (err) { | ||
161 | BT_ERR("ECDH sample 1 failed"); | ||
162 | return err; | ||
163 | } | ||
164 | |||
165 | err = test_ecdh_sample(priv_a_2, priv_b_2, pub_a_2, pub_b_2, dhkey_2); | ||
166 | if (err) { | ||
167 | BT_ERR("ECDH sample 2 failed"); | ||
168 | return err; | ||
169 | } | ||
170 | |||
171 | err = test_ecdh_sample(priv_a_3, priv_a_3, pub_a_3, pub_a_3, dhkey_3); | ||
172 | if (err) { | ||
173 | BT_ERR("ECDH sample 3 failed"); | ||
174 | return err; | ||
175 | } | ||
176 | |||
177 | BT_INFO("ECDH test passed"); | ||
178 | |||
179 | return 0; | ||
180 | } | ||
181 | |||
182 | #else | ||
183 | |||
184 | static inline int test_ecdh(void) | ||
185 | { | ||
186 | return 0; | ||
187 | } | ||
188 | |||
189 | #endif | ||
190 | |||
28 | static int __init run_selftest(void) | 191 | static int __init run_selftest(void) |
29 | { | 192 | { |
193 | int err; | ||
194 | |||
30 | BT_INFO("Starting self testing"); | 195 | BT_INFO("Starting self testing"); |
31 | 196 | ||
197 | err = test_ecdh(); | ||
198 | |||
32 | BT_INFO("Finished self testing"); | 199 | BT_INFO("Finished self testing"); |
33 | 200 | ||
34 | return 0; | 201 | return err; |
35 | } | 202 | } |
36 | 203 | ||
37 | #if IS_MODULE(CONFIG_BT) | 204 | #if IS_MODULE(CONFIG_BT) |