summaryrefslogtreecommitdiffstats
path: root/tests/symm_keys.c
blob: aee5396ee9e9e0961aaae14edc6e54cb8f46ad0e (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
/* ncr_symm_key_* 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 <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

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

static void
log_silent (const gchar *log_domain, GLogLevelFlags log_level,
	    const gchar *message, gpointer user_data)
{
  (void)log_domain;
  (void)log_level;
  (void)message;
  (void)user_data;
}

static void
check_set_sentitive_failure (struct ncr_symm_key *key)
{
  uint8_t dest[256];
  size_t dest_size;
  CK_RV res;

  /* Extraction of a sensitive value is a programming error, so we complain to
     stderr.  Hide this in the test output. */

  g_log_set_default_handler (log_silent, NULL);

  dest_size = sizeof (dest);
  res = ncr_symm_key_export (key, dest, &dest_size);
  assert (res == CKR_ATTRIBUTE_SENSITIVE);

  g_log_set_default_handler (g_log_default_handler, NULL);
}

int
main (void)
{
  static const uint8_t input[32]
    = "\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";

  uint8_t dest[256];
  size_t dest_size;
  struct ncr_symm_key *key;
  CK_RV res;

  res = ncr_symm_key_create (&key, CKK_AES, false, input, sizeof (input));
  assert (res == CKR_OK);

  dest_size = sizeof (dest);
  res = ncr_symm_key_export (key, dest, &dest_size);
  assert (res == CKR_OK);
  assert (dest_size == sizeof (input));
  assert (memcmp (dest, input, dest_size) == 0);

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

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

  check_set_sentitive_failure (key);

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


  res = ncr_symm_key_create (&key, CKK_AES, true, input, sizeof (input));
  assert (res == CKR_OK);

  check_set_sentitive_failure (key);

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


  res = ncr_symm_key_generate (&key, CKM_AES_KEY_GEN, false, sizeof (input));
  assert (res == CKR_OK);

  dest_size = sizeof (dest);
  res = ncr_symm_key_export (key, dest, &dest_size);
  assert (res == CKR_OK);
  assert (dest_size == sizeof (input));

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

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

  check_set_sentitive_failure (key);

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


  res = ncr_symm_key_generate (&key, CKM_AES_KEY_GEN, true, sizeof (input));
  assert (res == CKR_OK);

  check_set_sentitive_failure (key);

  res = ncr_symm_key_destroy (key);
  assert (res == CKR_OK);
  return EXIT_SUCCESS;
}