xref: /openbmc/qemu/crypto/rsakey.h (revision 93e0932b)
1 /*
2  * QEMU Crypto RSA key parser
3  *
4  * Copyright (c) 2022 Bytedance
5  * Author: lei he <helei.sig11@bytedance.com>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef QCRYPTO_RSAKEY_H
23 #define QCRYPTO_RSAKEY_H
24 
25 #include "qemu/host-utils.h"
26 #include "crypto/akcipher.h"
27 
28 typedef struct QCryptoAkCipherRSAKey QCryptoAkCipherRSAKey;
29 typedef struct QCryptoAkCipherMPI QCryptoAkCipherMPI;
30 
31 /**
32  * Multiple precious integer, encoded as two' complement,
33  * copied directly from DER encoded ASN.1 structures.
34  */
35 struct QCryptoAkCipherMPI {
36     uint8_t *data;
37     size_t len;
38 };
39 
40 /* See rfc2437: https://datatracker.ietf.org/doc/html/rfc2437 */
41 struct QCryptoAkCipherRSAKey {
42     /* The modulus */
43     QCryptoAkCipherMPI n;
44     /* The public exponent */
45     QCryptoAkCipherMPI e;
46     /* The private exponent */
47     QCryptoAkCipherMPI d;
48     /* The first factor */
49     QCryptoAkCipherMPI p;
50     /* The second factor */
51     QCryptoAkCipherMPI q;
52     /* The first factor's exponent */
53     QCryptoAkCipherMPI dp;
54     /* The second factor's exponent */
55     QCryptoAkCipherMPI dq;
56     /* The CRT coefficient */
57     QCryptoAkCipherMPI u;
58 };
59 
60 /**
61  * Parse DER encoded ASN.1 RSA keys, expected ASN.1 schemas:
62  *        RsaPrivKey ::= SEQUENCE {
63  *             version     INTEGER
64  *             n           INTEGER
65  *             e           INTEGER
66  *             d           INTEGER
67  *             p           INTEGER
68  *             q           INTEGER
69  *             dp          INTEGER
70  *             dq          INTEGER
71  *             u           INTEGER
72  *       otherPrimeInfos   OtherPrimeInfos OPTIONAL
73  *         }
74  *
75  *        RsaPubKey ::= SEQUENCE {
76  *             n           INTEGER
77  *             e           INTEGER
78  *         }
79  *
80  * Returns: On success QCryptoAkCipherRSAKey is returned, otherwise returns NULL
81  */
82 QCryptoAkCipherRSAKey *qcrypto_akcipher_rsakey_parse(
83     QCryptoAkCipherKeyType type,
84     const uint8_t *key, size_t keylen, Error **errp);
85 
86 /**
87  * qcrypto_akcipher_rsakey_export_as_p8info:
88  *
89  * Export RSA private key to PKCS#8 private key info.
90  */
91 void qcrypto_akcipher_rsakey_export_p8info(const uint8_t *key,
92                                            size_t keylen,
93                                            uint8_t **dst,
94                                            size_t *dlen);
95 
96 void qcrypto_akcipher_rsakey_free(QCryptoAkCipherRSAKey *key);
97 
98 G_DEFINE_AUTOPTR_CLEANUP_FUNC(QCryptoAkCipherRSAKey,
99                               qcrypto_akcipher_rsakey_free);
100 
101 #endif
102