• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: MIT
2 /*
3  * Test libfsverity_sign_digest().
4  *
5  * Copyright 2020 Google LLC
6  *
7  * Use of this source code is governed by an MIT-style
8  * license that can be found in the LICENSE file or at
9  * https://opensource.org/licenses/MIT.
10  */
11 
12 #include "utils.h"
13 
14 #include <fcntl.h>
15 
16 #define SHA256_DIGEST_SIZE 32
17 
main(void)18 int main(void)
19 {
20 	struct libfsverity_digest *d = xzalloc(sizeof(*d) + SHA256_DIGEST_SIZE);
21 	const struct libfsverity_signature_params params = {
22 		.keyfile = "testdata/key.pem",
23 		.certfile = "testdata/cert.pem",
24 	};
25 	u8 *sig;
26 	size_t sig_size;
27 	struct filedes file;
28 	u8 *expected_sig;
29 	u64 expected_sig_size;
30 	int err;
31 
32 	install_libfsverity_error_handler();
33 
34 	d->digest_algorithm = FS_VERITY_HASH_ALG_SHA256;
35 	d->digest_size = SHA256_DIGEST_SIZE;
36 	memcpy(d->digest,
37 	       "\x91\x79\x00\xb0\xd2\x99\x45\x4a\xa3\x04\xd5\xde\xbc\x6f\x39"
38 	       "\xe4\xaf\x7b\x5a\xbe\x33\xbd\xbc\x56\x8d\x5d\x8f\x1e\x5c\x4d"
39 	       "\x86\x52", SHA256_DIGEST_SIZE);
40 
41 	err = libfsverity_sign_digest(d, &params, &sig, &sig_size);
42 	ASSERT(err == 0);
43 
44 	ASSERT(open_file(&file, "testdata/file.sig", O_RDONLY, 0));
45 	ASSERT(get_file_size(&file, &expected_sig_size));
46 	ASSERT(sig_size == expected_sig_size);
47 	expected_sig = xmalloc(sig_size);
48 	ASSERT(full_read(&file, expected_sig, sig_size));
49 	ASSERT(!memcmp(sig, expected_sig, sig_size));
50 
51 	free(d);
52 	free(sig);
53 	free(expected_sig);
54 	filedes_close(&file);
55 	printf("test_sign_digest passed\n");
56 	return 0;
57 }
58