1 /*
2  * Copyright 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef SYSTEM_KEYMASTER_RSA_OPERATION_H_
18 #define SYSTEM_KEYMASTER_RSA_OPERATION_H_
19 
20 #include <keymaster/UniquePtr.h>
21 
22 #include <openssl/evp.h>
23 #include <openssl/rsa.h>
24 
25 #include <keymaster/operation.h>
26 
27 namespace keymaster {
28 
29 /**
30  * Base class for all RSA operations.
31  *
32  * This class provides RSA key management, plus buffering of data for non-digesting modes.
33  */
34 class RsaOperation : public Operation {
35   public:
RsaOperation(AuthorizationSet && hw_enforced,AuthorizationSet && sw_enforced,keymaster_purpose_t purpose,keymaster_digest_t digest,keymaster_padding_t padding,EVP_PKEY * key)36     RsaOperation(AuthorizationSet&& hw_enforced, AuthorizationSet&& sw_enforced,
37                  keymaster_purpose_t purpose, keymaster_digest_t digest,
38                  keymaster_padding_t padding, EVP_PKEY* key)
39         : Operation(purpose, move(hw_enforced), move(sw_enforced)), rsa_key_(key),
40           padding_(padding), digest_(digest), digest_algorithm_(nullptr) {}
41     ~RsaOperation();
42 
43     keymaster_error_t Begin(const AuthorizationSet& input_params,
44                             AuthorizationSet* output_params) override;
45     keymaster_error_t Update(const AuthorizationSet& additional_params, const Buffer& input,
46                              AuthorizationSet* output_params, Buffer* output,
47                              size_t* input_consumed) override;
Abort()48     keymaster_error_t Abort() override { return KM_ERROR_OK; }
49 
padding()50     keymaster_padding_t padding() const { return padding_; }
digest()51     keymaster_digest_t digest() const { return digest_; }
52 
53   protected:
54     virtual int GetOpensslPadding(keymaster_error_t* error) = 0;
55     virtual bool require_digest() const = 0;
56 
57     keymaster_error_t StoreData(const Buffer& input, size_t* input_consumed);
58     keymaster_error_t SetRsaPaddingInEvpContext(EVP_PKEY_CTX* pkey_ctx, bool signing);
59     keymaster_error_t InitDigest();
60 
61     EVP_PKEY* rsa_key_;
62     const keymaster_padding_t padding_;
63     Buffer data_;
64     const keymaster_digest_t digest_;
65     const EVP_MD* digest_algorithm_;
66 };
67 
68 /**
69  * Base class for all digesting RSA operations.
70  *
71  * This class adds digesting support, for digesting modes.  For non-digesting modes, it falls back
72  * on the RsaOperation input buffering.
73  */
74 class RsaDigestingOperation : public RsaOperation {
75   public:
76     RsaDigestingOperation(AuthorizationSet&& hw_enforced, AuthorizationSet&& sw_enforced,
77                           keymaster_purpose_t purpose, keymaster_digest_t digest,
78                           keymaster_padding_t padding, EVP_PKEY* key);
79     ~RsaDigestingOperation();
80 
81   protected:
82     int GetOpensslPadding(keymaster_error_t* error) override;
require_digest()83     bool require_digest() const override { return padding_ == KM_PAD_RSA_PSS; }
84     EVP_MD_CTX digest_ctx_;
85 };
86 
87 /**
88  * RSA private key signing operation.
89  */
90 class RsaSignOperation : public RsaDigestingOperation {
91   public:
RsaSignOperation(AuthorizationSet && hw_enforced,AuthorizationSet && sw_enforced,keymaster_digest_t digest,keymaster_padding_t padding,EVP_PKEY * key)92     RsaSignOperation(AuthorizationSet&& hw_enforced, AuthorizationSet&& sw_enforced,
93                      keymaster_digest_t digest, keymaster_padding_t padding, EVP_PKEY* key)
94         : RsaDigestingOperation(move(hw_enforced), move(sw_enforced), KM_PURPOSE_SIGN, digest,
95                                 padding, key) {}
96 
97     keymaster_error_t Begin(const AuthorizationSet& input_params,
98                             AuthorizationSet* output_params) override;
99     keymaster_error_t Update(const AuthorizationSet& additional_params, const Buffer& input,
100                              AuthorizationSet* output_params, Buffer* output,
101                              size_t* input_consumed) override;
102     keymaster_error_t Finish(const AuthorizationSet& additional_params, const Buffer& input,
103                              const Buffer& signature, AuthorizationSet* output_params,
104                              Buffer* output) override;
105 
106   private:
107     keymaster_error_t SignUndigested(Buffer* output);
108     keymaster_error_t SignDigested(Buffer* output);
109 };
110 
111 /**
112  * RSA public key verification operation.
113  */
114 class RsaVerifyOperation : public RsaDigestingOperation {
115   public:
RsaVerifyOperation(AuthorizationSet && hw_enforced,AuthorizationSet && sw_enforced,keymaster_digest_t digest,keymaster_padding_t padding,EVP_PKEY * key)116     RsaVerifyOperation(AuthorizationSet&& hw_enforced, AuthorizationSet&& sw_enforced,
117                        keymaster_digest_t digest, keymaster_padding_t padding, EVP_PKEY* key)
118         : RsaDigestingOperation(move(hw_enforced), move(sw_enforced), KM_PURPOSE_VERIFY, digest,
119                                 padding, key) {}
120 
121     keymaster_error_t Begin(const AuthorizationSet& input_params,
122                             AuthorizationSet* output_params) override;
123     keymaster_error_t Update(const AuthorizationSet& additional_params, const Buffer& input,
124                              AuthorizationSet* output_params, Buffer* output,
125                              size_t* input_consumed) override;
126     keymaster_error_t Finish(const AuthorizationSet& additional_params, const Buffer& input,
127                              const Buffer& signature, AuthorizationSet* output_params,
128                              Buffer* output) override;
129 
130   private:
131     keymaster_error_t VerifyUndigested(const Buffer& signature);
132     keymaster_error_t VerifyDigested(const Buffer& signature);
133 };
134 
135 /**
136  * Base class for RSA crypting operations.
137  */
138 class RsaCryptOperation : public RsaOperation {
139   public:
RsaCryptOperation(AuthorizationSet && hw_enforced,AuthorizationSet && sw_enforced,keymaster_purpose_t purpose,keymaster_digest_t digest,keymaster_padding_t padding,EVP_PKEY * key)140     RsaCryptOperation(AuthorizationSet&& hw_enforced, AuthorizationSet&& sw_enforced,
141                       keymaster_purpose_t purpose, keymaster_digest_t digest,
142                       keymaster_padding_t padding, EVP_PKEY* key)
143         : RsaOperation(move(hw_enforced), move(sw_enforced), purpose, digest, padding, key) {}
144 
145   protected:
146     keymaster_error_t SetOaepDigestIfRequired(EVP_PKEY_CTX* pkey_ctx);
147 
148   private:
149     int GetOpensslPadding(keymaster_error_t* error) override;
require_digest()150     bool require_digest() const override { return padding_ == KM_PAD_RSA_OAEP; }
151 };
152 
153 /**
154  * RSA public key encryption operation.
155  */
156 class RsaEncryptOperation : public RsaCryptOperation {
157   public:
RsaEncryptOperation(AuthorizationSet && hw_enforced,AuthorizationSet && sw_enforced,keymaster_digest_t digest,keymaster_padding_t padding,EVP_PKEY * key)158     RsaEncryptOperation(AuthorizationSet&& hw_enforced, AuthorizationSet&& sw_enforced,
159                         keymaster_digest_t digest, keymaster_padding_t padding, EVP_PKEY* key)
160         : RsaCryptOperation(move(hw_enforced), move(sw_enforced), KM_PURPOSE_ENCRYPT, digest,
161                             padding, key) {}
162     keymaster_error_t Finish(const AuthorizationSet& additional_params, const Buffer& input,
163                              const Buffer& signature, AuthorizationSet* output_params,
164                              Buffer* output) override;
165 };
166 
167 /**
168  * RSA private key decryption operation.
169  */
170 class RsaDecryptOperation : public RsaCryptOperation {
171   public:
RsaDecryptOperation(AuthorizationSet && hw_enforced,AuthorizationSet && sw_enforced,keymaster_digest_t digest,keymaster_padding_t padding,EVP_PKEY * key)172     RsaDecryptOperation(AuthorizationSet&& hw_enforced, AuthorizationSet&& sw_enforced,
173                         keymaster_digest_t digest, keymaster_padding_t padding, EVP_PKEY* key)
174         : RsaCryptOperation(move(hw_enforced), move(sw_enforced), KM_PURPOSE_DECRYPT, digest,
175                             padding, key) {}
176     keymaster_error_t Finish(const AuthorizationSet& additional_params, const Buffer& input,
177                              const Buffer& signature, AuthorizationSet* output_params,
178                              Buffer* output) override;
179 };
180 
181 /**
182  * Abstract base for all RSA operation factories.  This class exists mainly to centralize some code
183  * common to all RSA operation factories.
184  */
185 class RsaOperationFactory : public OperationFactory {
186   public:
registry_key()187     KeyType registry_key() const override { return KeyType(KM_ALGORITHM_RSA, purpose()); }
188     virtual keymaster_purpose_t purpose() const = 0;
189 
CreateOperation(Key && key,const AuthorizationSet & begin_params,keymaster_error_t * error)190     OperationPtr CreateOperation(Key&& key, const AuthorizationSet& begin_params,
191                                  keymaster_error_t* error) override {
192         return OperationPtr(CreateRsaOperation(move(key), begin_params, error));
193     }
194     const keymaster_digest_t* SupportedDigests(size_t* digest_count) const override;
195 
196   protected:
197     static EVP_PKEY* GetRsaKey(Key&& key, keymaster_error_t* error);
198     virtual RsaOperation* CreateRsaOperation(Key&& key, const AuthorizationSet& begin_params,
199                                              keymaster_error_t* error);
200 
201   private:
202     virtual RsaOperation* InstantiateOperation(AuthorizationSet&& hw_enforced,
203                                                AuthorizationSet&& sw_enforced,
204                                                keymaster_digest_t digest,
205                                                keymaster_padding_t padding, EVP_PKEY* key) = 0;
206 };
207 
208 /**
209  * Abstract base for RSA operations that digest their input (signing and verification).
210  */
211 class RsaDigestingOperationFactory : public RsaOperationFactory {
212   public:
213     const keymaster_padding_t* SupportedPaddingModes(size_t* padding_mode_count) const override;
214 };
215 
216 /**
217  * Abstract base for en/de-crypting RSA operation factories.  This class does most of the work of
218  * creating such operations, delegating only the actual operation instantiation.
219  */
220 class RsaCryptingOperationFactory : public RsaOperationFactory {
221   public:
222     RsaOperation* CreateRsaOperation(Key&& key, const AuthorizationSet& begin_params,
223                                      keymaster_error_t* error) override;
224     const keymaster_padding_t* SupportedPaddingModes(size_t* padding_mode_count) const override;
225 };
226 
227 /**
228  * Concrete factory for RSA signing operations.
229  */
230 class RsaSigningOperationFactory : public RsaDigestingOperationFactory {
231   public:
purpose()232     keymaster_purpose_t purpose() const override { return KM_PURPOSE_SIGN; }
InstantiateOperation(AuthorizationSet && hw_enforced,AuthorizationSet && sw_enforced,keymaster_digest_t digest,keymaster_padding_t padding,EVP_PKEY * key)233     RsaOperation* InstantiateOperation(AuthorizationSet&& hw_enforced,
234                                        AuthorizationSet&& sw_enforced, keymaster_digest_t digest,
235                                        keymaster_padding_t padding, EVP_PKEY* key) override {
236         return new (std::nothrow)
237             RsaSignOperation(move(hw_enforced), move(sw_enforced), digest, padding, key);
238     }
239 };
240 
241 /**
242  * Concrete factory for RSA signing operations.
243  */
244 class RsaVerificationOperationFactory : public RsaDigestingOperationFactory {
purpose()245     keymaster_purpose_t purpose() const override { return KM_PURPOSE_VERIFY; }
InstantiateOperation(AuthorizationSet && hw_enforced,AuthorizationSet && sw_enforced,keymaster_digest_t digest,keymaster_padding_t padding,EVP_PKEY * key)246     RsaOperation* InstantiateOperation(AuthorizationSet&& hw_enforced,
247                                        AuthorizationSet&& sw_enforced, keymaster_digest_t digest,
248                                        keymaster_padding_t padding, EVP_PKEY* key) override {
249         return new (std::nothrow)
250             RsaVerifyOperation(move(hw_enforced), move(sw_enforced), digest, padding, key);
251     }
252 };
253 
254 /**
255  * Concrete factory for RSA signing operations.
256  */
257 class RsaEncryptionOperationFactory : public RsaCryptingOperationFactory {
purpose()258     keymaster_purpose_t purpose() const override { return KM_PURPOSE_ENCRYPT; }
InstantiateOperation(AuthorizationSet && hw_enforced,AuthorizationSet && sw_enforced,keymaster_digest_t digest,keymaster_padding_t padding,EVP_PKEY * key)259     RsaOperation* InstantiateOperation(AuthorizationSet&& hw_enforced,
260                                        AuthorizationSet&& sw_enforced, keymaster_digest_t digest,
261                                        keymaster_padding_t padding, EVP_PKEY* key) override {
262         return new (std::nothrow)
263             RsaEncryptOperation(move(hw_enforced), move(sw_enforced), digest, padding, key);
264     }
265 };
266 
267 /**
268  * Concrete factory for RSA signing operations.
269  */
270 class RsaDecryptionOperationFactory : public RsaCryptingOperationFactory {
purpose()271     keymaster_purpose_t purpose() const override { return KM_PURPOSE_DECRYPT; }
InstantiateOperation(AuthorizationSet && hw_enforced,AuthorizationSet && sw_enforced,keymaster_digest_t digest,keymaster_padding_t padding,EVP_PKEY * key)272     RsaOperation* InstantiateOperation(AuthorizationSet&& hw_enforced,
273                                        AuthorizationSet&& sw_enforced, keymaster_digest_t digest,
274                                        keymaster_padding_t padding, EVP_PKEY* key) override {
275         return new (std::nothrow)
276             RsaDecryptOperation(move(hw_enforced), move(sw_enforced), digest, padding, key);
277     }
278 };
279 
280 }  // namespace keymaster
281 
282 #endif  // SYSTEM_KEYMASTER_RSA_OPERATION_H_
283