1 /* Software-based Trusted Platform Module (TPM) Emulator
2  * Copyright (C) 2004-2010 Mario Strasser <mast@gmx.net>
3  *
4  * This module is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published
6  * by the Free Software Foundation; either version 2 of the License,
7  * or (at your option) any later version.
8  *
9  * This module is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * $Id: rsa.h 364 2010-02-11 10:24:45Z mast $
15  */
16 
17 #ifndef _RSA_H_
18 #define _RSA_H_
19 
20 #include <stddef.h>
21 #include <stdint.h>
22 #include "bn.h"
23 
24 typedef struct {
25   tpm_bn_t n;
26   tpm_bn_t e;
27   tpm_bn_t d;
28   tpm_bn_t p;
29   tpm_bn_t q;
30   tpm_bn_t u;
31   uint16_t size;
32 } tpm_rsa_private_key_t;
33 
34 typedef struct {
35   tpm_bn_t n;
36   tpm_bn_t e;
37   uint16_t size;
38 } tpm_rsa_public_key_t;
39 
40 enum {
41   RSA_ES_PKCSV15,
42   RSA_ES_OAEP_SHA1,
43   RSA_ES_PLAIN,
44   RSA_SSA_PKCS1_SHA1,
45   RSA_SSA_PKCS1_SHA1_RAW,
46   RSA_SSA_PKCS1_DER
47 };
48 
49 enum {
50   RSA_LSB_FIRST = -1, RSA_MSB_FIRST = 1
51 };
52 
53 #define TPM_RSA_EXTRACT_PUBLIC_KEY(priv_key, pub_key) { \
54   tpm_bn_init_set(pub_key.n, priv_key.n); \
55   tpm_bn_init_set(pub_key.e, priv_key.e); \
56   pub_key.size = priv_key.size; }
57 
58 int tpm_rsa_import_key(tpm_rsa_private_key_t *key, int endian,
59                        const uint8_t *n, size_t n_len,
60                        const uint8_t *e, size_t e_len,
61                        const uint8_t *p, const uint8_t *q);
62 
63 void tpm_rsa_copy_key(tpm_rsa_private_key_t *dst, tpm_rsa_private_key_t *src);
64 
65 int tpm_rsa_import_public_key(tpm_rsa_public_key_t *key, int endian,
66                               const uint8_t *n, size_t n_len,
67                               const uint8_t *e, size_t e_len);
68 
69 int tpm_rsa_generate_key(tpm_rsa_private_key_t *key, uint16_t key_size);
70 
71 void tpm_rsa_release_private_key(tpm_rsa_private_key_t *key);
72 
73 void tpm_rsa_release_public_key(tpm_rsa_public_key_t *key);
74 
75 void tpm_rsa_export_modulus(tpm_rsa_private_key_t *key,
76                             uint8_t *modulus, size_t *length);
77 
78 void tpm_rsa_export_exponent(tpm_rsa_private_key_t *key,
79                              uint8_t *exponent, size_t *length);
80 
81 void tpm_rsa_export_prime1(tpm_rsa_private_key_t *key,
82                            uint8_t *prime, size_t *length);
83 
84 void tpm_rsa_export_prime2(tpm_rsa_private_key_t *key,
85                            uint8_t *prime, size_t *length);
86 
87 size_t tpm_rsa_modulus_length(tpm_rsa_private_key_t *key);
88 
89 size_t tpm_rsa_exponent_length(tpm_rsa_private_key_t *key);
90 
91 size_t tpm_rsa_prime1_length(tpm_rsa_private_key_t *key);
92 
93 size_t tpm_rsa_prime2_length(tpm_rsa_private_key_t *key);
94 
95 void tpm_rsa_mask_generation(const uint8_t *seed, size_t seed_len,
96                              uint8_t *data, size_t data_len);
97 
98 void tpm_rsa_export_public_modulus(tpm_rsa_public_key_t *key,
99                                    uint8_t *modulus, size_t *length);
100 
101 void tpm_rsa_export_public_exponent(tpm_rsa_public_key_t *key,
102                                     uint8_t *exponent, size_t *length);
103 
104 size_t tpm_rsa_public_modulus_length(tpm_rsa_public_key_t *key);
105 
106 size_t tpm_rsa_public_exponent_length(tpm_rsa_public_key_t *key);
107 
108 /* Note: Input and output areas MUST NOT overlap (i.e., one can't
109    use the same buffer for data and sig or in and out). */
110 
111 int tpm_rsa_sign(tpm_rsa_private_key_t *key, int type,
112                  const uint8_t *data, size_t data_len, uint8_t *sig);
113 
114 int tpm_rsa_verify(tpm_rsa_public_key_t *key, int type,
115                    const uint8_t *data, size_t data_len, uint8_t *sig);
116 
117 int tpm_rsa_decrypt(tpm_rsa_private_key_t *key, int type,
118                     const uint8_t *in, size_t in_len, uint8_t *out, size_t *out_len);
119 
120 int tpm_rsa_encrypt(tpm_rsa_public_key_t *key, int type,
121                     const uint8_t *in, size_t in_len, uint8_t *out, size_t *out_len);
122 
123 #endif /* _RSA_H_ */
124 
125