Quellcode durchsuchen

[crypto] Use linker tables for RSA digestInfo prefixes

Allow external code to specify RSA digestInfo prefixes for additional
digest algorithms.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown vor 12 Jahren
Ursprung
Commit
05c13716f9
2 geänderte Dateien mit 44 neuen und 37 gelöschten Zeilen
  1. 26
    37
      src/crypto/rsa.c
  2. 18
    0
      src/include/ipxe/rsa.h

+ 26
- 37
src/crypto/rsa.c Datei anzeigen

@@ -39,45 +39,37 @@ FILE_LICENCE ( GPL2_OR_LATER );
39 39
  * RSA is documented in RFC 3447.
40 40
  */
41 41
 
42
-/** An RSA digestInfo prefix */
43
-struct rsa_digestinfo_prefix {
44
-	/** Digest algorithm */
45
-	struct digest_algorithm *digest;
46
-	/** Prefix */
47
-	const void *data;
48
-	/** Length of prefix */
49
-	size_t len;
50
-};
51
-
52
-/** "id-md5" object identifier */
53
-static const uint8_t rsa_md5_prefix[] =
42
+/** MD5 digestInfo prefix */
43
+static const uint8_t rsa_md5_prefix_data[] =
54 44
 	{ RSA_DIGESTINFO_PREFIX ( MD5_DIGEST_SIZE, ASN1_OID_MD5 ) };
55 45
 
56
-/** "id-sha1" object identifier */
57
-static const uint8_t rsa_sha1_prefix[] =
46
+/** SHA-1 digestInfo prefix */
47
+static const uint8_t rsa_sha1_prefix_data[] =
58 48
 	{ RSA_DIGESTINFO_PREFIX ( SHA1_DIGEST_SIZE, ASN1_OID_SHA1 ) };
59 49
 
60
-/** "id-sha256" object identifier */
61
-static const uint8_t rsa_sha256_prefix[] =
50
+/** SHA-256 digestInfo prefix */
51
+static const uint8_t rsa_sha256_prefix_data[] =
62 52
 	{ RSA_DIGESTINFO_PREFIX ( SHA256_DIGEST_SIZE, ASN1_OID_SHA256 ) };
63 53
 
64
-/** RSA digestInfo prefixes */
65
-static struct rsa_digestinfo_prefix rsa_digestinfo_prefixes[] = {
66
-	{
67
-		.digest = &md5_algorithm,
68
-		.data = rsa_md5_prefix,
69
-		.len = sizeof ( rsa_md5_prefix ),
70
-	},
71
-	{
72
-		.digest = &sha1_algorithm,
73
-		.data = rsa_sha1_prefix,
74
-		.len = sizeof ( rsa_sha1_prefix ),
75
-	},
76
-	{
77
-		.digest = &sha256_algorithm,
78
-		.data = rsa_sha256_prefix,
79
-		.len = sizeof ( rsa_sha256_prefix ),
80
-	},
54
+/** MD5 digestInfo prefix */
55
+struct rsa_digestinfo_prefix rsa_md5_prefix __rsa_digestinfo_prefix = {
56
+	.digest = &md5_algorithm,
57
+	.data = rsa_md5_prefix_data,
58
+	.len = sizeof ( rsa_md5_prefix_data ),
59
+};
60
+
61
+/** SHA-1 digestInfo prefix */
62
+struct rsa_digestinfo_prefix rsa_sha1_prefix __rsa_digestinfo_prefix = {
63
+	.digest = &sha1_algorithm,
64
+	.data = rsa_sha1_prefix_data,
65
+	.len = sizeof ( rsa_sha1_prefix_data ),
66
+};
67
+
68
+/** SHA-256 digestInfo prefix */
69
+struct rsa_digestinfo_prefix rsa_sha256_prefix __rsa_digestinfo_prefix = {
70
+	.digest = &sha256_algorithm,
71
+	.data = rsa_sha256_prefix_data,
72
+	.len = sizeof ( rsa_sha256_prefix_data ),
81 73
 };
82 74
 
83 75
 /**
@@ -89,11 +81,8 @@ static struct rsa_digestinfo_prefix rsa_digestinfo_prefixes[] = {
89 81
 static struct rsa_digestinfo_prefix *
90 82
 rsa_find_prefix ( struct digest_algorithm *digest ) {
91 83
 	struct rsa_digestinfo_prefix *prefix;
92
-	unsigned int i;
93 84
 
94
-	for ( i = 0 ; i < ( sizeof ( rsa_digestinfo_prefixes ) /
95
-			    sizeof ( rsa_digestinfo_prefixes[0] ) ) ; i++ ) {
96
-		prefix = &rsa_digestinfo_prefixes[i];
85
+	for_each_table_entry ( prefix, RSA_DIGESTINFO_PREFIXES ) {
97 86
 		if ( prefix->digest == digest )
98 87
 			return prefix;
99 88
 	}

+ 18
- 0
src/include/ipxe/rsa.h Datei anzeigen

@@ -11,6 +11,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
11 11
 #include <ipxe/crypto.h>
12 12
 #include <ipxe/bigint.h>
13 13
 #include <ipxe/asn1.h>
14
+#include <ipxe/tables.h>
14 15
 
15 16
 /** ASN.1 OID for iso(1) member-body(2) us(840) */
16 17
 #define ASN1_OID_ISO_US ASN1_OID_ISO_MEMBERBODY, ASN1_OID_DOUBLE ( 840 )
@@ -111,6 +112,23 @@ FILE_LICENCE ( GPL2_OR_LATER );
111 112
 	RSA_DIGESTALGORITHM ( __VA_ARGS__ ),				\
112 113
 	RSA_DIGEST_PREFIX ( digest_size )
113 114
 
115
+/** An RSA digestInfo prefix */
116
+struct rsa_digestinfo_prefix {
117
+	/** Digest algorithm */
118
+	struct digest_algorithm *digest;
119
+	/** Prefix */
120
+	const void *data;
121
+	/** Length of prefix */
122
+	size_t len;
123
+};
124
+
125
+/** RSA digestInfo prefix table */
126
+#define RSA_DIGESTINFO_PREFIXES \
127
+	__table ( struct rsa_digestinfo_prefix, "rsa_digestinfo_prefixes" )
128
+
129
+/** Declare an RSA digestInfo prefix */
130
+#define __rsa_digestinfo_prefix __table_entry ( RSA_DIGESTINFO_PREFIXES, 01 )
131
+
114 132
 /** An RSA context */
115 133
 struct rsa_context {
116 134
 	/** Allocated memory */

Laden…
Abbrechen
Speichern