summaryrefslogtreecommitdiffstats
path: root/tests/symm_ciphers.c
blob: ccf6fe525807ec7e9091a21b5acb2771f43a6b79 (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
/* ncr_symm_cipher_* tests.

Copyright 2010 Red Hat, Inc.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
EVENT SHALL CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.

Red Hat author: Miloslav Trmač <mitr@redhat.com> */

#include <assert.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

#include <glib.h>
#include <ncrypto/ncrypto.h>

struct tv
{
  CK_MECHANISM_TYPE mech;
  CK_KEY_TYPE key_type;
  const uint8_t *key;
  size_t key_size;
  const uint8_t *input;
  size_t input_size;
  const uint8_t *output;
  size_t output_size;
};

/* FIXME: Test non-ECB modes as well. */
static const struct tv tvs[] =
  {
#define TV(M, K, KEY, IN, OUT)						\
    {									\
      (M), (K), (const uint8_t *)(KEY), sizeof (KEY) - 1,		\
      (const uint8_t *)(IN), sizeof (IN) - 1, (const uint8_t *)(OUT),	\
      sizeof (OUT) - 1							\
    }
    TV (CKM_AES_ECB, CKK_AES,
	"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F",
	"\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA\xBB\xCC\xDD\xEE\xFF",
	"\x69\xC4\xE0\xD8\x6A\x7B\x04\x30\xD8\xCD\xB7\x80\x70\xB4\xC5\x5A"),
    TV (CKM_AES_ECB, CKK_AES,
	"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17",
	"\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA\xBB\xCC\xDD\xEE\xFF",
	"\xDD\xA9\x7C\xA4\x86\x4C\xDF\xE0\x6E\xAF\x70\xA0\xEC\x0D\x71\x91"),
    TV (CKM_AES_ECB, CKK_AES,
	"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F",
	"\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA\xBB\xCC\xDD\xEE\xFF",
	"\x8E\xA2\xB7\xCA\x51\x67\x45\xBF\xEA\xFC\x49\x90\x4B\x49\x60\x89"),
#undef TV
  };

int
main (void)
{
  struct ncr_symm_cipher_session *sess;
  struct ncr_symm_key *key;
  uint8_t dest[256];
  size_t i, j, dest_size;
  CK_RV res;

  for (i = 0; i < G_N_ELEMENTS (tvs); i++)
    {
      res = ncr_symm_cipher_alloc (&sess, tvs[i].mech);
      assert (res == CKR_OK);

      res = ncr_symm_key_create (&key, tvs[i].key_type, tvs[i].key,
				 tvs[i].key_size);
      assert (res == CKR_OK);

      for (j = 0; j < 2; j++)
	{
	  res = ncr_symm_cipher_encrypt_init (sess, key, NULL, 0);
	  assert (res == CKR_OK);

	  dest_size = sizeof (dest);
	  res = ncr_symm_cipher_encrypt_update (sess, dest, &dest_size,
						tvs[i].input,
						tvs[i].input_size);
	  assert (res == CKR_OK);
	  assert (dest_size == tvs[i].output_size);
	  assert (memcmp (dest, tvs[i].output, dest_size) == 0);

	  dest_size = sizeof (dest);
	  res = ncr_symm_cipher_encrypt_final (sess, dest, &dest_size,
					       NULL, 0);
	  assert (res == CKR_OK);
	  assert (dest_size == 0);

	  res = ncr_symm_cipher_decrypt_init (sess, key, NULL, 0);
	  assert (res == CKR_OK);

	  dest_size = sizeof (dest);
	  res = ncr_symm_cipher_decrypt_update (sess, dest, &dest_size,
						tvs[i].output,
						tvs[i].output_size);
	  assert (res == CKR_OK);
	  assert (dest_size == tvs[i].input_size);
	  assert (memcmp (dest, tvs[i].input, dest_size) == 0);

	  dest_size = sizeof (dest);
	  res = ncr_symm_cipher_decrypt_final (sess, dest, &dest_size,
					       NULL, 0);
	  assert (res == CKR_OK);
	  assert (dest_size == 0);
	}

      res = ncr_symm_key_destroy (key);
      assert (res == CKR_OK);

      res = ncr_symm_cipher_free (sess);
      assert (res == CKR_OK);
    }

  for (i = 0; i < G_N_ELEMENTS (tvs); i++)
    {
      res = ncr_symm_cipher_alloc (&sess, tvs[i].mech);
      assert (res == CKR_OK);

      res = ncr_symm_key_create (&key, tvs[i].key_type, tvs[i].key,
				 tvs[i].key_size);
      assert (res == CKR_OK);

      for (j = 0; j < 2; j++)
	{
	  res = ncr_symm_cipher_encrypt_init (sess, key, NULL, 0);
	  assert (res == CKR_OK);

	  dest_size = sizeof (dest);
	  res = ncr_symm_cipher_encrypt (sess, dest, &dest_size, tvs[i].input,
					 tvs[i].input_size);
	  assert (res == CKR_OK);
	  assert (dest_size == tvs[i].output_size);
	  assert (memcmp (dest, tvs[i].output, dest_size) == 0);

	  res = ncr_symm_cipher_decrypt_init (sess, key, NULL, 0);
	  assert (res == CKR_OK);

	  dest_size = sizeof (dest);
	  res = ncr_symm_cipher_decrypt (sess, dest, &dest_size, tvs[i].output,
					 tvs[i].output_size);
	  assert (res == CKR_OK);
	  assert (dest_size == tvs[i].input_size);
	  assert (memcmp (dest, tvs[i].input, dest_size) == 0);
	}

      res = ncr_symm_key_destroy (key);
      assert (res == CKR_OK);

      res = ncr_symm_cipher_free (sess);
      assert (res == CKR_OK);
    }

  return EXIT_SUCCESS;
}