1: %%
    2: %% %CopyrightBegin%
    3: %%
    4: %% Copyright Ericsson AB 2009-2016. 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(blowfish_SUITE).
   23: 
   24: %% Note: This directive should only be used in test suites.
   25: -compile(export_all).
   26: 
   27: -include_lib("common_test/include/ct.hrl").
   28: 
   29: -define(TIMEOUT, 120000). % 2 min
   30: 
   31: -define(KEY, to_bin("0123456789ABCDEFF0E1D2C3B4A59687")).
   32: -define(IVEC, to_bin("FEDCBA9876543210")).
   33: %% "7654321 Now is the time for " (includes trailing '\0')
   34: -define(DATA, to_bin("37363534333231204E6F77206973207468652074696D6520666F722000")).
   35: -define(DATA_PADDED, to_bin("37363534333231204E6F77206973207468652074696D6520666F722000000000")).
   36: 
   37: %% Test server callback functions
   38: %%--------------------------------------------------------------------
   39: %% Function: init_per_suite(Config) -> Config
   40: %% Config - [tuple()]
   41: %%   A list of key/value pairs, holding the test case configuration.
   42: %% Description: Initialization before the whole suite
   43: %%
   44: %% Note: This function is free to add any key/value pairs to the Config
   45: %% variable, but should NOT alter/remove any existing entries.
   46: %%--------------------------------------------------------------------
   47: init_per_suite(Config) ->
   48:     case catch crypto:start() of
   49: 	ok ->
   50: 	    Config;
   51: 	_Else ->
   52: 	    {skip,"Could not start crypto!"}
   53:     end.
   54: 
   55: %%--------------------------------------------------------------------
   56: %% Function: end_per_suite(Config) -> _
   57: %% Config - [tuple()]
   58: %%   A list of key/value pairs, holding the test case configuration.
   59: %% Description: Cleanup after the whole suite
   60: %%--------------------------------------------------------------------
   61: end_per_suite(_Config) ->
   62:     crypto:stop().
   63: 
   64: %%--------------------------------------------------------------------
   65: %% Function: init_per_testcase(TestCase, Config) -> Config
   66: %% Case - atom()
   67: %%   Name of the test case that is about to be run.
   68: %% Config - [tuple()]
   69: %%   A list of key/value pairs, holding the test case configuration.
   70: %%
   71: %% Description: Initialization before each test case
   72: %%
   73: %% Note: This function is free to add any key/value pairs to the Config
   74: %% variable, but should NOT alter/remove any existing entries.
   75: %% Description: Initialization before each test case
   76: %%--------------------------------------------------------------------
   77: init_per_testcase(_TestCase, Config0) ->
   78:     Config = lists:keydelete(watchdog, 1, Config0),
   79:     Dog = test_server:timetrap(?TIMEOUT),
   80:     [{watchdog, Dog} | Config].
   81: 
   82: %%--------------------------------------------------------------------
   83: %% Function: end_per_testcase(TestCase, Config) -> _
   84: %% Case - atom()
   85: %%   Name of the test case that is about to be run.
   86: %% Config - [tuple()]
   87: %%   A list of key/value pairs, holding the test case configuration.
   88: %% Description: Cleanup after each test case
   89: %%--------------------------------------------------------------------
   90: end_per_testcase(_TestCase, Config) ->
   91:     Dog = ?config(watchdog, Config),
   92:     case Dog of
   93: 	undefined ->
   94: 	    ok;
   95: 	_ ->
   96: 	    test_server:timetrap_cancel(Dog)
   97:     end.
   98: 
   99: %%--------------------------------------------------------------------
  100: %% Function: all(Clause) -> TestCases
  101: %% Clause - atom() - suite | doc
  102: %% TestCases - [Case]
  103: %% Case - atom()
  104: %%   Name of a test case.
  105: %% Description: Returns a list of all test cases in this test suite
  106: %%--------------------------------------------------------------------
  107: suite() -> [{ct_hooks,[ts_install_cth]}].
  108: 
  109: all() -> 
  110: [ecb, cbc, cfb64, ofb64].
  111: 
  112: groups() -> 
  113:     [].
  114: 
  115: init_per_group(_GroupName, Config) ->
  116: 	Config.
  117: 
  118: end_per_group(_GroupName, Config) ->
  119: 	Config.
  120: 
  121: 
  122: %% Test cases start here.
  123: %%--------------------------------------------------------------------
  124: 
  125: ecb_test(KeyBytes, ClearBytes, CipherBytes) ->
  126:     {Key, Clear, Cipher} =
  127: 	{to_bin(KeyBytes), to_bin(ClearBytes), to_bin(CipherBytes)},
  128:     ?line m(crypto:blowfish_ecb_encrypt(Key, Clear), Cipher),
  129:     true.
  130: 
  131: ecb(doc) ->
  132:     "Test that ECB mode is OK";
  133: ecb(suite) ->
  134:     [];
  135: ecb(Config) when is_list(Config) ->
  136: 	true = ecb_test("0000000000000000", "0000000000000000", "4EF997456198DD78"),
  137: 	true = ecb_test("FFFFFFFFFFFFFFFF", "FFFFFFFFFFFFFFFF", "51866FD5B85ECB8A"),
  138: 	true = ecb_test("3000000000000000", "1000000000000001", "7D856F9A613063F2"),
  139: 	true = ecb_test("1111111111111111", "1111111111111111", "2466DD878B963C9D"),
  140: 	true = ecb_test("0123456789ABCDEF", "1111111111111111", "61F9C3802281B096"),
  141: 	true = ecb_test("1111111111111111", "0123456789ABCDEF", "7D0CC630AFDA1EC7"),
  142: 	true = ecb_test("0000000000000000", "0000000000000000", "4EF997456198DD78"),
  143: 	true = ecb_test("FEDCBA9876543210", "0123456789ABCDEF", "0ACEAB0FC6A0A28D"),
  144: 	true = ecb_test("7CA110454A1A6E57", "01A1D6D039776742", "59C68245EB05282B"),
  145: 	true = ecb_test("0131D9619DC1376E", "5CD54CA83DEF57DA", "B1B8CC0B250F09A0"),
  146: 	true = ecb_test("07A1133E4A0B2686", "0248D43806F67172", "1730E5778BEA1DA4"),
  147: 	true = ecb_test("3849674C2602319E", "51454B582DDF440A", "A25E7856CF2651EB"),
  148: 	true = ecb_test("04B915BA43FEB5B6", "42FD443059577FA2", "353882B109CE8F1A"),
  149: 	true = ecb_test("0113B970FD34F2CE", "059B5E0851CF143A", "48F4D0884C379918"),
  150: 	true = ecb_test("0170F175468FB5E6", "0756D8E0774761D2", "432193B78951FC98"),
  151: 	true = ecb_test("43297FAD38E373FE", "762514B829BF486A", "13F04154D69D1AE5"),
  152: 	true = ecb_test("07A7137045DA2A16", "3BDD119049372802", "2EEDDA93FFD39C79"),
  153: 	true = ecb_test("04689104C2FD3B2F", "26955F6835AF609A", "D887E0393C2DA6E3"),
  154: 	true = ecb_test("37D06BB516CB7546", "164D5E404F275232", "5F99D04F5B163969"),
  155: 	true = ecb_test("1F08260D1AC2465E", "6B056E18759F5CCA", "4A057A3B24D3977B"),
  156: 	true = ecb_test("584023641ABA6176", "004BD6EF09176062", "452031C1E4FADA8E"),
  157: 	true = ecb_test("025816164629B007", "480D39006EE762F2", "7555AE39F59B87BD"),
  158: 	true = ecb_test("49793EBC79B3258F", "437540C8698F3CFA", "53C55F9CB49FC019"),
  159: 	true = ecb_test("4FB05E1515AB73A7", "072D43A077075292", "7A8E7BFA937E89A3"),
  160: 	true = ecb_test("49E95D6D4CA229BF", "02FE55778117F12A", "CF9C5D7A4986ADB5"),
  161: 	true = ecb_test("018310DC409B26D6", "1D9D5C5018F728C2", "D1ABB290658BC778"),
  162: 	true = ecb_test("1C587F1C13924FEF", "305532286D6F295A", "55CB3774D13EF201"),
  163: 	true = ecb_test("0101010101010101", "0123456789ABCDEF", "FA34EC4847B268B2"),
  164: 	true = ecb_test("1F1F1F1F0E0E0E0E", "0123456789ABCDEF", "A790795108EA3CAE"),
  165: 	true = ecb_test("E0FEE0FEF1FEF1FE", "0123456789ABCDEF", "C39E072D9FAC631D"),
  166: 	true = ecb_test("0000000000000000", "FFFFFFFFFFFFFFFF", "014933E0CDAFF6E4"),
  167: 	true = ecb_test("FFFFFFFFFFFFFFFF", "0000000000000000", "F21E9A77B71C49BC"),
  168: 	true = ecb_test("0123456789ABCDEF", "0000000000000000", "245946885754369A"),
  169: 	true = ecb_test("FEDCBA9876543210", "FFFFFFFFFFFFFFFF", "6B5C5A9C5D9E0A5A"),
  170: 	ok.
  171: 
  172: cbc(doc) ->
  173:     "Test that CBC mode is OK";
  174: cbc(suite) ->
  175:     [];
  176: cbc(Config) when is_list(Config) ->
  177: 	true = crypto:blowfish_cbc_encrypt(?KEY, ?IVEC, ?DATA_PADDED) =:=
  178: 		to_bin("6B77B4D63006DEE605B156E27403979358DEB9E7154616D959F1652BD5FF92CC"),
  179: 	ok.
  180: 
  181: cfb64(doc) ->
  182:     "Test that CFB64 mode is OK";
  183: cfb64(suite) ->
  184:     [];
  185: cfb64(Config) when is_list(Config) ->
  186: 	true = crypto:blowfish_cfb64_encrypt(?KEY, ?IVEC, ?DATA) =:=
  187: 		to_bin("E73214A2822139CAF26ECF6D2EB9E76E3DA3DE04D1517200519D57A6C3"),
  188: 	ok.
  189: 
  190: ofb64(doc) ->
  191:     "Test that OFB64 mode is OK";
  192: ofb64(suite) ->
  193:     [];
  194: ofb64(Config) when is_list(Config) ->
  195: 	true = crypto:blowfish_ofb64_encrypt(?KEY, ?IVEC, ?DATA) =:=
  196: 		to_bin("E73214A2822139CA62B343CC5B65587310DD908D0C241B2263C2CF80DA"),
  197: 	ok.
  198: 
  199: %% Helper functions
  200: 
  201: %% Convert a hexadecimal string to a binary.
  202: -spec(to_bin(L::string()) -> binary()).
  203: to_bin(L) ->
  204:     to_bin(L, []).
  205: 
  206: %% @spec dehex(char()) -> integer()
  207: %% @doc Convert a hex digit to its integer value.
  208: -spec(dehex(char()) -> integer()).
  209: dehex(C) when C >= $0, C =< $9 ->
  210:     C - $0;
  211: dehex(C) when C >= $a, C =< $f ->
  212:     C - $a + 10;
  213: dehex(C) when C >= $A, C =< $F ->
  214:     C - $A + 10.
  215: 
  216: -spec(to_bin(L::string(), list()) -> binary()).
  217: to_bin([], Acc) ->
  218:     iolist_to_binary(lists:reverse(Acc));
  219: to_bin([C1, C2 | Rest], Acc) ->
  220:     to_bin(Rest, [(dehex(C1) bsl 4) bor dehex(C2) | Acc]).
  221: 
  222: m(X,X) -> ok.