1 #ifndef _IPXE_RSA_H
2 #define _IPXE_RSA_H
3 
4 /** @file
5  *
6  * RSA public-key cryptography
7  */
8 
9 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
10 
11 #include <stdarg.h>
12 #include <ipxe/crypto.h>
13 #include <ipxe/bigint.h>
14 #include <ipxe/asn1.h>
15 #include <ipxe/tables.h>
16 
17 /** RSA digestAlgorithm sequence contents */
18 #define RSA_DIGESTALGORITHM_CONTENTS( ... )				\
19 	ASN1_OID, VA_ARG_COUNT ( __VA_ARGS__ ), __VA_ARGS__,		\
20 	ASN1_NULL, 0x00
21 
22 /** RSA digestAlgorithm sequence */
23 #define RSA_DIGESTALGORITHM( ... )					\
24 	ASN1_SEQUENCE,							\
25 	VA_ARG_COUNT ( RSA_DIGESTALGORITHM_CONTENTS ( __VA_ARGS__ ) ),	\
26 	RSA_DIGESTALGORITHM_CONTENTS ( __VA_ARGS__ )
27 
28 /** RSA digest prefix */
29 #define RSA_DIGEST_PREFIX( digest_size )				\
30 	ASN1_OCTET_STRING, digest_size
31 
32 /** RSA digestInfo prefix */
33 #define RSA_DIGESTINFO_PREFIX( digest_size, ... )			\
34 	ASN1_SEQUENCE,							\
35 	( VA_ARG_COUNT ( RSA_DIGESTALGORITHM ( __VA_ARGS__ ) ) +	\
36 	  VA_ARG_COUNT ( RSA_DIGEST_PREFIX ( digest_size ) ) +		\
37 	  digest_size ),						\
38 	RSA_DIGESTALGORITHM ( __VA_ARGS__ ),				\
39 	RSA_DIGEST_PREFIX ( digest_size )
40 
41 /** An RSA digestInfo prefix */
42 struct rsa_digestinfo_prefix {
43 	/** Digest algorithm */
44 	struct digest_algorithm *digest;
45 	/** Prefix */
46 	const void *data;
47 	/** Length of prefix */
48 	size_t len;
49 };
50 
51 /** RSA digestInfo prefix table */
52 #define RSA_DIGESTINFO_PREFIXES \
53 	__table ( struct rsa_digestinfo_prefix, "rsa_digestinfo_prefixes" )
54 
55 /** Declare an RSA digestInfo prefix */
56 #define __rsa_digestinfo_prefix __table_entry ( RSA_DIGESTINFO_PREFIXES, 01 )
57 
58 /** An RSA context */
59 struct rsa_context {
60 	/** Allocated memory */
61 	void *dynamic;
62 	/** Modulus */
63 	bigint_element_t *modulus0;
64 	/** Modulus size */
65 	unsigned int size;
66 	/** Modulus length */
67 	size_t max_len;
68 	/** Exponent */
69 	bigint_element_t *exponent0;
70 	/** Exponent size */
71 	unsigned int exponent_size;
72 	/** Input buffer */
73 	bigint_element_t *input0;
74 	/** Output buffer */
75 	bigint_element_t *output0;
76 	/** Temporary working space for modular exponentiation */
77 	void *tmp;
78 };
79 
80 /** RSA context size */
81 #define RSA_CTX_SIZE sizeof ( struct rsa_context )
82 
83 extern struct pubkey_algorithm rsa_algorithm;
84 
85 #endif /* _IPXE_RSA_H */
86