crypto_mod.h 2.45 KB
Newer Older
1
/*
2
 * Copyright (c) 2015-2020, ARM Limited and Contributors. All rights reserved.
3
 *
dp-arm's avatar
dp-arm committed
4
 * SPDX-License-Identifier: BSD-3-Clause
5
6
 */

7
8
#ifndef CRYPTO_MOD_H
#define CRYPTO_MOD_H
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

/* Return values */
enum crypto_ret_value {
	CRYPTO_SUCCESS = 0,
	CRYPTO_ERR_INIT,
	CRYPTO_ERR_HASH,
	CRYPTO_ERR_SIGNATURE,
	CRYPTO_ERR_UNKNOWN
};

/*
 * Cryptographic library descriptor
 */
typedef struct crypto_lib_desc_s {
	const char *name;

	/* Initialize library. This function is not expected to fail. All errors
	 * must be handled inside the function, asserting or panicing in case of
	 * a non-recoverable error */
	void (*init)(void);

	/* Verify a digital signature. Return one of the
	 * 'enum crypto_ret_value' options */
	int (*verify_signature)(void *data_ptr, unsigned int data_len,
				void *sig_ptr, unsigned int sig_len,
				void *sig_alg, unsigned int sig_alg_len,
				void *pk_ptr, unsigned int pk_len);

	/* Verify a hash. Return one of the 'enum crypto_ret_value' options */
	int (*verify_hash)(void *data_ptr, unsigned int data_len,
			   void *digest_info_ptr, unsigned int digest_info_len);
40
41
42
43
44
45
46

#if MEASURED_BOOT
	/* Calculate a hash. Return hash value */
	int (*calc_hash)(unsigned int alg, void *data_ptr,
			 unsigned int data_len, unsigned char *output);
#endif /* MEASURED_BOOT */

47
48
49
50
51
52
} crypto_lib_desc_t;

/* Public functions */
void crypto_mod_init(void);
int crypto_mod_verify_signature(void *data_ptr, unsigned int data_len,
				void *sig_ptr, unsigned int sig_len,
Roberto Vargas's avatar
Roberto Vargas committed
53
				void *sig_alg_ptr, unsigned int sig_alg_len,
54
55
56
57
				void *pk_ptr, unsigned int pk_len);
int crypto_mod_verify_hash(void *data_ptr, unsigned int data_len,
			   void *digest_info_ptr, unsigned int digest_info_len);

58
59
60
61
#if MEASURED_BOOT
int crypto_mod_calc_hash(unsigned int alg, void *data_ptr,
			 unsigned int data_len, unsigned char *output);

62
/* Macro to register a cryptographic library */
63
64
65
66
67
68
69
70
71
72
#define REGISTER_CRYPTO_LIB(_name, _init, _verify_signature, _verify_hash, \
							     _calc_hash) \
	const crypto_lib_desc_t crypto_lib_desc = { \
		.name = _name, \
		.init = _init, \
		.verify_signature = _verify_signature, \
		.verify_hash = _verify_hash, \
		.calc_hash = _calc_hash \
	}
#else
73
74
75
76
77
78
79
#define REGISTER_CRYPTO_LIB(_name, _init, _verify_signature, _verify_hash) \
	const crypto_lib_desc_t crypto_lib_desc = { \
		.name = _name, \
		.init = _init, \
		.verify_signature = _verify_signature, \
		.verify_hash = _verify_hash \
	}
80
#endif	/* MEASURED_BOOT */
81

Roberto Vargas's avatar
Roberto Vargas committed
82
83
extern const crypto_lib_desc_t crypto_lib_desc;

84
#endif /* CRYPTO_MOD_H */