Blame view

RIOT/tests/unittests/tests-crypto/tests-crypto-3des.c 2.53 KB
fb11e647   vrobic   reseau statique a...
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
  /*
   * Copyright (C) 2015 Nico von Geyso
   *
   * This file is subject to the terms and conditions of the GNU Lesser
   * General Public License v2.1. See the file LICENSE in the top level
   * directory for more details.
   */
  
  #include <limits.h>
  
  #include "embUnit.h"
  #include "crypto/3des.h"
  #include "tests-crypto.h"
  
  static uint8_t TEST_0_KEY[] = {
      0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
      0x8, 0x9, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF
  };
  
  static uint8_t TEST_0_INP[] = {
      0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7
  };
  static uint8_t TEST_0_ENC[] = {
      0xDF, 0x0B, 0x6C, 0x9C, 0x31, 0xCD, 0x0C, 0xE4
  };
  
  static uint8_t TEST_1_KEY[] = {
      0x23, 0xA0, 0x18, 0x53, 0xFA, 0xB3, 0x89, 0x23,
      0x65, 0x89, 0x2A, 0xBC, 0x43, 0x99, 0xCC, 0x00
  };
  
  static uint8_t TEST_1_INP[] = {
      0x11, 0x53, 0x81, 0xE2, 0x5F, 0x33, 0xE7, 0x41
  };
  static uint8_t TEST_1_ENC[] = {
      0x6C, 0x71, 0x5C, 0xC2, 0x58, 0x13, 0xEC, 0x6E
  };
  
  static void test_crypto_tripledes_encrypt(void)
  {
      cipher_context_t ctx;
      int err;
      uint8_t data[THREEDES_BLOCK_SIZE];
  
      err = tripledes_init(&ctx, TEST_0_KEY, 16);
      TEST_ASSERT_EQUAL_INT(1, err);
  
      err = tripledes_encrypt(&ctx, TEST_0_INP, data);
      TEST_ASSERT_EQUAL_INT(1, err);
      TEST_ASSERT_MESSAGE(1 == compare(TEST_0_ENC, data, THREEDES_BLOCK_SIZE), "wrong ciphertext");
  
      err = tripledes_init(&ctx, TEST_1_KEY, 16);
      TEST_ASSERT_EQUAL_INT(1, err);
  
      err = tripledes_encrypt(&ctx, TEST_1_INP, data);
      TEST_ASSERT_EQUAL_INT(1, err);
      TEST_ASSERT_MESSAGE(1 == compare(TEST_1_ENC, data, THREEDES_BLOCK_SIZE), "wrong ciphertext");
  }
  
  static void test_crypto_tripledes_decrypt(void)
  {
  
      cipher_context_t ctx;
      int err;
      uint8_t data[THREEDES_BLOCK_SIZE];
  
      err = tripledes_init(&ctx, TEST_0_KEY, 16);
      TEST_ASSERT_EQUAL_INT(1, err);
  
      err = tripledes_decrypt(&ctx, TEST_0_ENC, data);
      TEST_ASSERT_EQUAL_INT(1, err);
      TEST_ASSERT_MESSAGE(1 == compare(TEST_0_INP, data, THREEDES_BLOCK_SIZE), "wrong plaintext");
  
      err = tripledes_init(&ctx, TEST_1_KEY, 16);
      TEST_ASSERT_EQUAL_INT(1, err);
  
      err = tripledes_decrypt(&ctx, TEST_1_ENC, data);
      TEST_ASSERT_EQUAL_INT(1, err);
      TEST_ASSERT_MESSAGE(1 == compare(TEST_1_INP, data, THREEDES_BLOCK_SIZE), "wrong plaintext");
  }
  
  Test* tests_crypto_3des_tests(void)
  {
      EMB_UNIT_TESTFIXTURES(fixtures) {
          new_TestFixture(test_crypto_tripledes_encrypt),
                          new_TestFixture(test_crypto_tripledes_decrypt),
      };
  
      EMB_UNIT_TESTCALLER(crypto_tripledes_tests, NULL, NULL, fixtures);
  
      return (Test*)&crypto_tripledes_tests;
  }