xref: /openbmc/qemu/crypto/rsakey.h (revision ec8c2936)
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/osdep.h"
26 #include "qemu/host-utils.h"
27 #include "crypto/akcipher.h"
28 
29 typedef struct QCryptoAkCipherRSAKey QCryptoAkCipherRSAKey;
30 typedef struct QCryptoAkCipherMPI QCryptoAkCipherMPI;
31 
32 /**
33  * Multiple precious integer, encoded as two' complement,
34  * copied directly from DER encoded ASN.1 structures.
35  */
36 struct QCryptoAkCipherMPI {
37     uint8_t *data;
38     size_t len;
39 };
40 
41 /* See rfc2437: https://datatracker.ietf.org/doc/html/rfc2437 */
42 struct QCryptoAkCipherRSAKey {
43     /* The modulus */
44     QCryptoAkCipherMPI n;
45     /* The public exponent */
46     QCryptoAkCipherMPI e;
47     /* The private exponent */
48     QCryptoAkCipherMPI d;
49     /* The first factor */
50     QCryptoAkCipherMPI p;
51     /* The second factor */
52     QCryptoAkCipherMPI q;
53     /* The first factor's exponent */
54     QCryptoAkCipherMPI dp;
55     /* The second factor's exponent */
56     QCryptoAkCipherMPI dq;
57     /* The CRT coefficient */
58     QCryptoAkCipherMPI u;
59 };
60 
61 /**
62  * Parse DER encoded ASN.1 RSA keys, expected ASN.1 schemas:
63  *        RsaPrivKey ::= SEQUENCE {
64  *             version     INTEGER
65  *             n           INTEGER
66  *             e           INTEGER
67  *             d           INTEGER
68  *             p           INTEGER
69  *             q           INTEGER
70  *             dp          INTEGER
71  *             dq          INTEGER
72  *             u           INTEGER
73  *       otherPrimeInfos   OtherPrimeInfos OPTIONAL
74  *         }
75  *
76  *        RsaPubKey ::= SEQUENCE {
77  *             n           INTEGER
78  *             e           INTEGER
79  *         }
80  *
81  * Returns: On success QCryptoAkCipherRSAKey is returned, otherwise returns NULL
82  */
83 QCryptoAkCipherRSAKey *qcrypto_akcipher_rsakey_parse(
84     QCryptoAkCipherKeyType type,
85     const uint8_t *key, size_t keylen, Error **errp);
86 
87 void qcrypto_akcipher_rsakey_free(QCryptoAkCipherRSAKey *key);
88 
89 G_DEFINE_AUTOPTR_CLEANUP_FUNC(QCryptoAkCipherRSAKey,
90                               qcrypto_akcipher_rsakey_free);
91 
92 #endif
93