1%%
2%% %CopyrightBegin%
3%%
4%% Copyright Ericsson AB 2018-2018. All Rights Reserved.
5%%
6%% Licensed under the Apache License, Version 2.0 (the "License");
7%% you may not use this file except in compliance with the License.
8%% You may obtain a copy of the License at
9%%
10%%     http://www.apache.org/licenses/LICENSE-2.0
11%%
12%% Unless required by applicable law or agreed to in writing, software
13%% distributed under the License is distributed on an "AS IS" BASIS,
14%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15%% See the License for the specific language governing permissions and
16%% limitations under the License.
17%%
18%% %CopyrightEnd%
19%%
20
21%%
22-module(ssl_rfc_5869_SUITE).
23
24-behaviour(ct_suite).
25
26-include_lib("common_test/include/ct.hrl").
27
28%% Common test
29-export([all/0,
30         init_per_suite/1,
31         init_per_testcase/2,
32         end_per_suite/1,
33         end_per_testcase/2
34        ]).
35
36%% Test cases
37-export([sha_256_basic/0,
38         sha_256_basic/1,
39         sha_256_long/0,
40         sha_256_long/1,
41         sha_256_no_salt/0,
42         sha_256_no_salt/1,
43         sha_basic/0,
44         sha_basic/1,
45         sha_long/0,
46         sha_long/1,
47         sha_no_salt/0,
48         sha_no_salt/1,
49         sha_default_salt/0,
50         sha_default_salt/1
51        ]).
52
53%%--------------------------------------------------------------------
54%% Common Test interface functions -----------------------------------
55%%--------------------------------------------------------------------
56all() ->
57    [sha_256_basic,
58     sha_256_long,
59     sha_256_no_salt,
60     sha_basic,
61     sha_long,
62     sha_no_salt,
63     sha_default_salt
64    ].
65
66%%--------------------------------------------------------------------
67init_per_suite(Config) ->
68    catch crypto:stop(),
69    try crypto:start() of
70	ok ->
71            Config
72    catch _:_ ->
73	    {skip, "Crypto did not start"}
74    end.
75
76end_per_suite(_Config) ->
77    application:stop(crypto).
78
79%%--------------------------------------------------------------------
80init_per_testcase(_TestCase, Config) ->
81    ct:timetrap({seconds, 5}),
82    Config.
83
84end_per_testcase(_TestCase, Config) ->
85    Config.
86
87%%--------------------------------------------------------------------
88%% Test Cases --------------------------------------------------------
89%%--------------------------------------------------------------------
90
91sha_256_basic() ->
92    [{doc, "Basic test case with SHA-256"}].
93sha_256_basic(Config) when is_list(Config) ->
94    %% Hash = SHA-256
95    %% IKM  = 0x0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b (22 octets)
96    %% salt = 0x000102030405060708090a0b0c (13 octets)
97    %% info = 0xf0f1f2f3f4f5f6f7f8f9 (10 octets)
98    %% L    = 42
99    %% PRK  = 0x077709362c2e32df0ddc3f0dc47bba63
100    %%        90b6c73bb50f9c3122ec844ad7c2b3e5 (32 octets)
101    %% OKM  = 0x3cb25f25faacd57a90434f64d0362f2a
102    %%        2d2d0a90cf1a5a4c5db02d56ecc4c5bf
103    %%        34007208d5b887185865 (42 octets)
104    IKM  = hexstr2bin("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b"),
105    Salt = hexstr2bin("000102030405060708090a0b0c"),
106    Info = hexstr2bin("f0f1f2f3f4f5f6f7f8f9"),
107    PRK  = hexstr2bin("077709362c2e32df0ddc3f0dc47bba63"
108                      "90b6c73bb50f9c3122ec844ad7c2b3e5"),
109    OKM  = hexstr2bin("3cb25f25faacd57a90434f64d0362f2a"
110                      "2d2d0a90cf1a5a4c5db02d56ecc4c5bf"
111                      "34007208d5b887185865"),
112    hkdf_test(sha256, Salt, IKM, PRK, Info, 42, OKM).
113
114sha_256_long() ->
115    [{doc, "Test with SHA-256 and longer inputs/outputs"}].
116sha_256_long(Config) when is_list(Config) ->
117    %% Hash = SHA-256
118    %% IKM  = 0x000102030405060708090a0b0c0d0e0f
119    %%        101112131415161718191a1b1c1d1e1f
120    %%        202122232425262728292a2b2c2d2e2f
121    %%        303132333435363738393a3b3c3d3e3f
122    %%        404142434445464748494a4b4c4d4e4f (80 octets)
123    %% salt = 0x606162636465666768696a6b6c6d6e6f
124    %%        707172737475767778797a7b7c7d7e7f
125    %%        808182838485868788898a8b8c8d8e8f
126    %%        909192939495969798999a9b9c9d9e9f
127    %%        a0a1a2a3a4a5a6a7a8a9aaabacadaeaf (80 octets)
128    %% info = 0xb0b1b2b3b4b5b6b7b8b9babbbcbdbebf
129    %%        c0c1c2c3c4c5c6c7c8c9cacbcccdcecf
130    %%        d0d1d2d3d4d5d6d7d8d9dadbdcdddedf
131    %%        e0e1e2e3e4e5e6e7e8e9eaebecedeeef
132    %%        f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff (80 octets)
133    %% L    = 82
134
135    %% PRK  = 0x06a6b88c5853361a06104c9ceb35b45c
136    %%        ef760014904671014a193f40c15fc244 (32 octets)
137    %% OKM  = 0xb11e398dc80327a1c8e7f78c596a4934
138    %%        4f012eda2d4efad8a050cc4c19afa97c
139    %%        59045a99cac7827271cb41c65e590e09
140    %%        da3275600c2f09b8367793a9aca3db71
141    %%        cc30c58179ec3e87c14c01d5c1f3434f
142    %%        1d87 (82 octets)
143    IKM  = hexstr2bin("000102030405060708090a0b0c0d0e0f"
144                      "101112131415161718191a1b1c1d1e1f"
145                      "202122232425262728292a2b2c2d2e2f"
146                      "303132333435363738393a3b3c3d3e3f"
147                      "404142434445464748494a4b4c4d4e4f"
148                     ),
149    Salt = hexstr2bin("606162636465666768696a6b6c6d6e6f"
150                      "707172737475767778797a7b7c7d7e7f"
151                      "808182838485868788898a8b8c8d8e8f"
152                      "909192939495969798999a9b9c9d9e9f"
153                      "a0a1a2a3a4a5a6a7a8a9aaabacadaeaf"
154                     ),
155    Info = hexstr2bin("b0b1b2b3b4b5b6b7b8b9babbbcbdbebf"
156                      "c0c1c2c3c4c5c6c7c8c9cacbcccdcecf"
157                      "d0d1d2d3d4d5d6d7d8d9dadbdcdddedf"
158                      "e0e1e2e3e4e5e6e7e8e9eaebecedeeef"
159                      "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff"
160                     ),
161    PRK  = hexstr2bin("06a6b88c5853361a06104c9ceb35b45c"
162                      "ef760014904671014a193f40c15fc244"),
163    OKM  = hexstr2bin("b11e398dc80327a1c8e7f78c596a4934"
164                      "4f012eda2d4efad8a050cc4c19afa97c"
165                      "59045a99cac7827271cb41c65e590e09"
166                      "da3275600c2f09b8367793a9aca3db71"
167                      "cc30c58179ec3e87c14c01d5c1f3434f"
168                      "1d87"
169                     ),
170    hkdf_test(sha256, Salt, IKM, PRK, Info, 82, OKM).
171sha_256_no_salt() ->
172    [{doc, "Test with SHA-256 and zero-length salt/info"}].
173sha_256_no_salt(Config) when is_list(Config) ->
174    %% Hash = SHA-256
175    %% IKM  = 0x0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b (22 octets)
176    %% salt = (0 octets)
177    %% info = (0 octets)
178    %% L    = 42
179
180    %% PRK  = 0x19ef24a32c717b167f33a91d6f648bdf
181    %%        96596776afdb6377ac434c1c293ccb04 (32 octets)
182    %% OKM  = 0x8da4e775a563c18f715f802a063c5a31
183    %%        b8a11f5c5ee1879ec3454e5f3c738d2d
184    %%        9d201395faa4b61a96c8 (42 octets)
185    IKM  = hexstr2bin("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b"),
186    Salt = <<>>,
187    Info = <<>>,
188    PRK  = hexstr2bin("19ef24a32c717b167f33a91d6f648bdf"
189                      "96596776afdb6377ac434c1c293ccb04"),
190    OKM  = hexstr2bin("8da4e775a563c18f715f802a063c5a31"
191                      "b8a11f5c5ee1879ec3454e5f3c738d2d"
192                      "9d201395faa4b61a96c8"),
193    hkdf_test(sha256, Salt, IKM, PRK, Info, 42, OKM).
194
195sha_basic() ->
196    [{doc, " Basic test case with SHA-1"}].
197sha_basic(Config) when is_list(Config) ->
198    %% Hash = SHA-1
199    %% IKM  = 0x0b0b0b0b0b0b0b0b0b0b0b (11 octets)
200    %% salt = 0x000102030405060708090a0b0c (13 octets)
201    %% info = 0xf0f1f2f3f4f5f6f7f8f9 (10 octets)
202    %% L    = 42
203
204    %% PRK  = 0x9b6c18c432a7bf8f0e71c8eb88f4b30baa2ba243 (20 octets)
205    %% OKM  = 0x085a01ea1b10f36933068b56efa5ad81
206    %%        a4f14b822f5b091568a9cdd4f155fda2
207    %%        c22e422478d305f3f896 (42 octets)
208    IKM  = hexstr2bin("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b"),
209    Salt = hexstr2bin("000102030405060708090a0b0c"),
210    Info = hexstr2bin("f0f1f2f3f4f5f6f7f8f9"),
211    PRK  = hexstr2bin("077709362c2e32df0ddc3f0dc47bba63"
212                      "90b6c73bb50f9c3122ec844ad7c2b3e5"),
213    OKM  = hexstr2bin("3cb25f25faacd57a90434f64d0362f2a"
214                      "2d2d0a90cf1a5a4c5db02d56ecc4c5bf"
215                      "34007208d5b887185865"),
216    hkdf_test(sha256, Salt, IKM, PRK, Info, 42, OKM).
217
218sha_long() ->
219    [{doc, "Test with SHA-1 and longer inputs/outputs"}].
220sha_long(Config) when is_list(Config) ->
221    %% Hash = SHA-1
222    %% IKM  = 0x000102030405060708090a0b0c0d0e0f
223    %%        101112131415161718191a1b1c1d1e1f
224    %%        202122232425262728292a2b2c2d2e2f
225    %%        303132333435363738393a3b3c3d3e3f
226    %%        404142434445464748494a4b4c4d4e4f (80 octets)
227    %% salt = 0x606162636465666768696a6b6c6d6e6f
228    %%        707172737475767778797a7b7c7d7e7f
229    %%        808182838485868788898a8b8c8d8e8f
230    %%        909192939495969798999a9b9c9d9e9f
231    %%        a0a1a2a3a4a5a6a7a8a9aaabacadaeaf (80 octets)
232    %% info = 0xb0b1b2b3b4b5b6b7b8b9babbbcbdbebf
233    %%        c0c1c2c3c4c5c6c7c8c9cacbcccdcecf
234    %%        d0d1d2d3d4d5d6d7d8d9dadbdcdddedf
235    %%        e0e1e2e3e4e5e6e7e8e9eaebecedeeef
236    %%        f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff (80 octets)
237    %% L    = 82
238
239    %% PRK  = 0x8adae09a2a307059478d309b26c4115a224cfaf6 (20 octets)
240    %% OKM  = 0x0bd770a74d1160f7c9f12cd5912a06eb
241    %%        ff6adcae899d92191fe4305673ba2ffe
242    %%        8fa3f1a4e5ad79f3f334b3b202b2173c
243    %%        486ea37ce3d397ed034c7f9dfeb15c5e
244    %%        927336d0441f4c4300e2cff0d0900b52
245    %%        d3b4 (82 octets)
246    IKM  = hexstr2bin("000102030405060708090a0b0c0d0e0f"
247                      "101112131415161718191a1b1c1d1e1f"
248                      "202122232425262728292a2b2c2d2e2f"
249                      "303132333435363738393a3b3c3d3e3f"
250                      "404142434445464748494a4b4c4d4e4f"
251                     ),
252    Salt = hexstr2bin("606162636465666768696a6b6c6d6e6f"
253                      "707172737475767778797a7b7c7d7e7f"
254                      "808182838485868788898a8b8c8d8e8f"
255                      "909192939495969798999a9b9c9d9e9f"
256                      "a0a1a2a3a4a5a6a7a8a9aaabacadaeaf"
257                     ),
258    Info = hexstr2bin("b0b1b2b3b4b5b6b7b8b9babbbcbdbebf"
259                      "c0c1c2c3c4c5c6c7c8c9cacbcccdcecf"
260                      "d0d1d2d3d4d5d6d7d8d9dadbdcdddedf"
261                      "e0e1e2e3e4e5e6e7e8e9eaebecedeeef"
262                      "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff"
263                     ),
264    PRK  = hexstr2bin("8adae09a2a307059478d309b26c4115a224cfaf6"),
265    OKM  = hexstr2bin("0bd770a74d1160f7c9f12cd5912a06eb"
266                      "ff6adcae899d92191fe4305673ba2ffe"
267                      "8fa3f1a4e5ad79f3f334b3b202b2173c"
268                      "486ea37ce3d397ed034c7f9dfeb15c5e"
269                      "927336d0441f4c4300e2cff0d0900b52"
270                      "d3b4"
271                     ),
272    hkdf_test(sha, Salt, IKM, PRK, Info, 82, OKM).
273
274sha_no_salt() ->
275    [{doc, "Test with SHA-1 and zero-length salt/info"}].
276sha_no_salt(Config) when is_list(Config) ->
277    %%   Hash = SHA-1
278    %% IKM  = 0x0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b (22 octets)
279    %% salt = (0 octets)
280    %% info = (0 octets)
281    %% L    = 42
282
283    %% PRK  = 0xda8c8a73c7fa77288ec6f5e7c297786aa0d32d01 (20 octets)
284    %% OKM  = 0x0ac1af7002b3d761d1e55298da9d0506
285    %%        b9ae52057220a306e07b6b87e8df21d0
286    %%        ea00033de03984d34918 (42 octets)
287    IKM  = hexstr2bin("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b"),
288    Salt = <<>>,
289    Info = <<>>,
290    PRK  = hexstr2bin("da8c8a73c7fa77288ec6f5e7c297786aa0d32d01"),
291    OKM  = hexstr2bin("0ac1af7002b3d761d1e55298da9d0506"
292                      "b9ae52057220a306e07b6b87e8df21d0"
293                      "ea00033de03984d34918"),
294    hkdf_test(sha, Salt, IKM, PRK, Info, 42, OKM).
295
296
297sha_default_salt() ->
298    [{doc, "Test with SHA-1, salt not provided (defaults to HashLen zero octets),
299   zero-length info"}].
300sha_default_salt(Config) when is_list(Config) ->
301    %% Hash = SHA-1
302    %% IKM  = 0x0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c (22 octets)
303    %% salt = not provided (defaults to HashLen zero octets)
304    %% info = (0 octets)
305    %% L    = 42
306
307    %% PRK  = 0x2adccada18779e7c2077ad2eb19d3f3e731385dd (20 octets)
308    %% OKM  = 0x2c91117204d745f3500d636a62f64f0a
309    %%        b3bae548aa53d423b0d1f27ebba6f5e5
310    %%        673a081d70cce7acfc48 (42 octets)
311    IKM  = hexstr2bin("0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c"),
312    Salt = binary:copy(<<0>>, 20),
313    Info = <<>>,
314    PRK  = hexstr2bin("2adccada18779e7c2077ad2eb19d3f3e731385dd"),
315    OKM  = hexstr2bin("2c91117204d745f3500d636a62f64f0a"
316                      "b3bae548aa53d423b0d1f27ebba6f5e5"
317                      "673a081d70cce7acfc48"),
318    hkdf_test(sha, Salt, IKM, PRK, Info, 42, OKM).
319
320hkdf_test(HashAlg, Salt, KeyingMaterial, PsedoRandKey, ContextInfo, Length, Key) ->
321    PsedoRandKey = tls_v1:hkdf_extract(HashAlg, Salt, KeyingMaterial),
322    Key = tls_v1:hkdf_expand(PsedoRandKey, ContextInfo, Length, HashAlg).
323
324hexstr2bin(S) when is_binary(S) ->
325    list_to_binary(hexstr2list(binary_to_list(S)));
326hexstr2bin(S) ->
327    list_to_binary(hexstr2list(S)).
328
329hexstr2list([$ |T]) ->
330    hexstr2list(T);
331hexstr2list([X,Y|T]) ->
332    [mkint(X)*16 + mkint(Y) | hexstr2list(T)];
333hexstr2list([]) ->
334    [].
335mkint(C) when $0 =< C, C =< $9 ->
336    C - $0;
337mkint(C) when $A =< C, C =< $F ->
338    C - $A + 10;
339mkint(C) when $a =< C, C =< $f ->
340    C - $a + 10.
341