2 * Copyright (C) 2014-2015 Andreas Steffen
3 * HSR Hochschule fuer Technik Rapperswil
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 #include "test_suite.h"
18 #include <bliss_private_key.h>
19 #include <bliss_public_key.h>
21 static u_int key_type
[] = { 1, 3, 4 };
22 static u_int key_strength
[] = { 128, 160, 192 };
24 START_TEST(test_bliss_sign_all
)
26 signature_scheme_t signature_scheme
;
27 private_key_t
*privkey
, *privkey1
;
28 public_key_t
*pubkey
, *pubkey1
;
29 chunk_t msg
, signature
, privkey_blob
, pubkey_blob
, pubkey_fp
, privkey_fp
;
32 for (k
= 0; k
< 4; k
++)
34 int verify_count
= 1000;
39 signature_scheme
= SIGN_BLISS_WITH_SHA2_256
;
42 signature_scheme
= SIGN_BLISS_WITH_SHA2_384
;
45 signature_scheme
= SIGN_BLISS_WITH_SHA2_512
;
48 /* enforce BLISS-B key for k = 2, 3 */
49 lib
->settings
->set_bool(lib
->settings
,
50 "%s.plugins.bliss.use_bliss_b", k
>= 2, lib
->ns
);
52 msg
= chunk_from_str("Hello Dolly!");
54 /* generate private key */
55 privkey
= lib
->creds
->create(lib
->creds
, CRED_PRIVATE_KEY
, KEY_BLISS
,
56 BUILD_KEY_SIZE
, key_type
[_i
], BUILD_END
);
59 /* generate ASN.1 DER and PEM encoding of private key */
60 ck_assert(privkey
->get_encoding(privkey
, (k
% 2) ?
61 PRIVKEY_ASN1_DER
: PRIVKEY_PEM
, &privkey_blob
));
63 /* extract public key from private key */
64 pubkey
= privkey
->get_public_key(privkey
);
67 /* generate ASN.1 DER and PEM encodings of public key */
68 ck_assert(pubkey
->get_encoding(pubkey
, (k
% 2) ?
69 PUBKEY_SPKI_ASN1_DER
: PUBKEY_PEM
, &pubkey_blob
));
71 /* compare fingerprints of public and private key */
72 ck_assert(pubkey
->get_fingerprint(pubkey
, (k
% 2) ?
73 KEYID_PUBKEY_INFO_SHA1
: KEYID_PUBKEY_SHA1
, &pubkey_fp
));
74 ck_assert(privkey
->get_fingerprint(privkey
, (k
% 2) ?
75 KEYID_PUBKEY_INFO_SHA1
: KEYID_PUBKEY_SHA1
, &privkey_fp
));
76 ck_assert(chunk_equals(pubkey_fp
, privkey_fp
));
78 /* retrieve fingerprints of public and private key from cache */
79 ck_assert(pubkey
->get_fingerprint(pubkey
, (k
% 2) ?
80 KEYID_PUBKEY_INFO_SHA1
: KEYID_PUBKEY_SHA1
, &pubkey_fp
));
81 ck_assert(privkey
->get_fingerprint(privkey
, (k
% 2) ?
82 KEYID_PUBKEY_INFO_SHA1
: KEYID_PUBKEY_SHA1
, &privkey_fp
));
84 /* get a reference of the private key and destroy both instances */
85 privkey1
= privkey
->get_ref(privkey
);
87 ck_assert(privkey1
== privkey
);
88 privkey
->destroy(privkey
);
89 privkey1
->destroy(privkey1
);
91 /* get a reference of the public key and destroy both instances */
92 pubkey1
= pubkey
->get_ref(pubkey
);
94 ck_assert(pubkey1
== pubkey
);
95 pubkey
->destroy(pubkey
);
96 pubkey1
->destroy(pubkey1
);
98 /* enforce BLISS-B key for k = 1, 3 */
99 lib
->settings
->set_bool(lib
->settings
,
100 "%s.plugins.bliss.use_bliss_b", k
% 2, lib
->ns
);
102 /* load private key from ASN.1 blob */
103 privkey
= lib
->creds
->create(lib
->creds
, CRED_PRIVATE_KEY
, KEY_BLISS
,
104 BUILD_BLOB
, privkey_blob
, BUILD_END
);
106 ck_assert(privkey
->get_type(privkey
) == KEY_BLISS
);
107 ck_assert(privkey
->get_keysize(privkey
) == key_strength
[_i
]);
108 chunk_free(&privkey_blob
);
110 /* load public key from ASN.1 blob */
111 pubkey
= lib
->creds
->create(lib
->creds
, CRED_PUBLIC_KEY
, KEY_ANY
,
112 BUILD_BLOB
, pubkey_blob
, BUILD_END
);
114 ck_assert(pubkey
->get_type(pubkey
) == KEY_BLISS
);
115 ck_assert(pubkey
->get_keysize(pubkey
) == key_strength
[_i
]);
116 chunk_free(&pubkey_blob
);
118 /* generate and verify 1000 BLISS signatures */
119 while (verify_count
--)
121 ck_assert(privkey
->sign(privkey
, signature_scheme
, msg
,
123 ck_assert(pubkey
->verify(pubkey
, signature_scheme
, msg
,
127 privkey
->destroy(privkey
);
128 pubkey
->destroy(pubkey
);
133 START_TEST(test_bliss_sign_fail
)
135 private_key_t
*privkey
;
136 public_key_t
*pubkey
;
137 chunk_t msg
, signature
, encoding
, fp
;
139 /* generate non-supported BLISS-II private key */
140 privkey
= lib
->creds
->create(lib
->creds
, CRED_PRIVATE_KEY
, KEY_BLISS
,
141 BUILD_KEY_SIZE
, BLISS_II
, BUILD_END
);
144 /* generate non-supported BLISS-B-II private key */
145 privkey
= lib
->creds
->create(lib
->creds
, CRED_PRIVATE_KEY
, KEY_BLISS
,
146 BUILD_KEY_SIZE
, BLISS_B_II
, BUILD_END
);
149 /* generate supported BLISS-B-I private key */
150 privkey
= lib
->creds
->create(lib
->creds
, CRED_PRIVATE_KEY
, KEY_BLISS
,
151 BUILD_KEY_SIZE
, BLISS_B_I
, BUILD_END
);
154 /* wrong private key encoding format */
155 ck_assert(!privkey
->get_encoding(privkey
, PUBKEY_PEM
, &encoding
));
157 /* wrong fingerprint encoding format */
158 ck_assert(!privkey
->get_fingerprint(privkey
, KEYID_PGPV4
, &fp
));
160 /* extract public key */
161 pubkey
= privkey
->get_public_key(privkey
);
164 /* wrong private key encoding format */
165 ck_assert(!pubkey
->get_encoding(pubkey
, PRIVKEY_PEM
, &encoding
));
167 /* wrong fingerprint encoding format */
168 ck_assert(!pubkey
->get_fingerprint(pubkey
, KEYID_PGPV4
, &fp
));
170 /* encryption / decryption operation is not defined for BLISS */
171 ck_assert(!pubkey
->encrypt(pubkey
, ENCRYPT_UNKNOWN
, chunk_empty
, NULL
));
172 ck_assert(!privkey
->decrypt(privkey
, ENCRYPT_UNKNOWN
, chunk_empty
, NULL
));
174 /* sign with invalid signature scheme */
175 ck_assert(!privkey
->sign(privkey
, SIGN_UNKNOWN
, msg
, &signature
));
177 /* generate valid signature */
178 msg
= chunk_from_str("Hello Dolly!");
179 ck_assert(privkey
->sign(privkey
, SIGN_BLISS_WITH_SHA2_512
, msg
, &signature
));
181 /* verify with invalid signature scheme */
182 ck_assert(!pubkey
->verify(pubkey
, SIGN_UNKNOWN
, msg
, signature
));
184 /* corrupt signature */
185 signature
.ptr
[signature
.len
- 1] ^= 0x80;
186 ck_assert(!pubkey
->verify(pubkey
, SIGN_BLISS_WITH_SHA2_512
, msg
, signature
));
189 privkey
->destroy(privkey
);
190 pubkey
->destroy(pubkey
);
194 Suite
*bliss_sign_suite_create()
199 s
= suite_create("bliss_sign");
201 tc
= tcase_create("sign_all");
202 test_case_set_timeout(tc
, 30);
203 tcase_add_loop_test(tc
, test_bliss_sign_all
, 0, countof(key_type
));
204 suite_add_tcase(s
, tc
);
206 tc
= tcase_create("sign_fail");
207 tcase_add_test(tc
, test_bliss_sign_fail
);
208 suite_add_tcase(s
, tc
);