blob: 4005dbdc239d90b5c24fe9aad827917c6dac9aff [file] [log] [blame]
Shawn Willden0a4df7e2014-08-28 16:09:05 -06001/*
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 <UniquePtr.h>
21
Shawn Willden63ac0432014-12-29 14:07:08 -070022#include <openssl/evp.h>
23#include <openssl/rsa.h>
24
Shawn Willden0a4df7e2014-08-28 16:09:05 -060025#include "operation.h"
26
27namespace keymaster {
28
Shawn Willden61902362014-12-18 10:33:24 -070029/**
30 * Base class for all RSA operations.
31 *
32 * This class provides RSA key management, plus buffering of data for non-digesting modes.
33 */
Shawn Willden0a4df7e2014-08-28 16:09:05 -060034class RsaOperation : public Operation {
35 public:
Shawn Willden2bf4ad32015-06-01 07:33:51 -060036 RsaOperation(keymaster_purpose_t purpose, keymaster_padding_t padding, EVP_PKEY* key)
Shawn Willden567a4a02014-12-31 12:14:46 -070037 : Operation(purpose), rsa_key_(key), padding_(padding) {}
Shawn Willden0a4df7e2014-08-28 16:09:05 -060038 ~RsaOperation();
39
Shawn Willden06298102015-05-25 23:12:48 -060040 keymaster_error_t Begin(const AuthorizationSet& /* input_params */,
41 AuthorizationSet* /* output_params */) override {
Shawn Willden111edb32015-02-05 22:44:24 -070042 return KM_ERROR_OK;
43 }
Shawn Willden06298102015-05-25 23:12:48 -060044 keymaster_error_t Update(const AuthorizationSet& additional_params, const Buffer& input,
Shawn Willdended8e7d2015-06-01 15:29:12 -060045 AuthorizationSet* output_params, Buffer* output,
46 size_t* input_consumed) override;
Shawn Willden06298102015-05-25 23:12:48 -060047 keymaster_error_t Abort() override { return KM_ERROR_OK; }
Shawn Willden0a4df7e2014-08-28 16:09:05 -060048
49 protected:
Shawn Willden2bf4ad32015-06-01 07:33:51 -060050 virtual int GetOpensslPadding(keymaster_error_t* error) = 0;
Shawn Willden0a4df7e2014-08-28 16:09:05 -060051
Shawn Willden2bf4ad32015-06-01 07:33:51 -060052 keymaster_error_t StoreData(const Buffer& input, size_t* input_consumed);
53 keymaster_error_t SetRsaPaddingInEvpContext(EVP_PKEY_CTX* pkey_ctx);
54
55 EVP_PKEY* rsa_key_;
Shawn Willden0a4df7e2014-08-28 16:09:05 -060056 keymaster_padding_t padding_;
57 Buffer data_;
58};
59
Shawn Willden61902362014-12-18 10:33:24 -070060/**
Shawn Willden2bf4ad32015-06-01 07:33:51 -060061 * Base class for all digesting RSA operations.
Shawn Willden61902362014-12-18 10:33:24 -070062 *
63 * This class adds digesting support, for digesting modes. For non-digesting modes, it falls back
64 * on the RsaOperation input buffering.
65 */
66class RsaDigestingOperation : public RsaOperation {
67 public:
68 RsaDigestingOperation(keymaster_purpose_t purpose, keymaster_digest_t digest,
Shawn Willden2bf4ad32015-06-01 07:33:51 -060069 keymaster_padding_t padding, EVP_PKEY* key);
Shawn Willden61902362014-12-18 10:33:24 -070070 ~RsaDigestingOperation();
71
Shawn Willden61902362014-12-18 10:33:24 -070072 protected:
Shawn Willdenf90f2352014-12-18 23:01:15 -070073 keymaster_error_t InitDigest();
Shawn Willden2bf4ad32015-06-01 07:33:51 -060074 int GetOpensslPadding(keymaster_error_t* error) override;
75
76 bool require_digest() const { return padding_ == KM_PAD_RSA_PSS; }
Shawn Willden61902362014-12-18 10:33:24 -070077
78 const keymaster_digest_t digest_;
79 const EVP_MD* digest_algorithm_;
80 EVP_MD_CTX digest_ctx_;
81};
82
83/**
84 * RSA private key signing operation.
85 */
86class RsaSignOperation : public RsaDigestingOperation {
Shawn Willden0a4df7e2014-08-28 16:09:05 -060087 public:
Shawn Willden2bf4ad32015-06-01 07:33:51 -060088 RsaSignOperation(keymaster_digest_t digest, keymaster_padding_t padding, EVP_PKEY* key)
Shawn Willden61902362014-12-18 10:33:24 -070089 : RsaDigestingOperation(KM_PURPOSE_SIGN, digest, padding, key) {}
Shawn Willden2bf4ad32015-06-01 07:33:51 -060090
91 keymaster_error_t Begin(const AuthorizationSet& input_params,
92 AuthorizationSet* output_params) override;
93 keymaster_error_t Update(const AuthorizationSet& additional_params, const Buffer& input,
Shawn Willdended8e7d2015-06-01 15:29:12 -060094 AuthorizationSet* output_params, Buffer* output,
95 size_t* input_consumed) override;
Shawn Willden06298102015-05-25 23:12:48 -060096 keymaster_error_t Finish(const AuthorizationSet& additional_params, const Buffer& signature,
Shawn Willdended8e7d2015-06-01 15:29:12 -060097 AuthorizationSet* output_params, Buffer* output) override;
Shawn Willden4200f212014-12-02 07:01:21 -070098
99 private:
Shawn Willdenf90f2352014-12-18 23:01:15 -0700100 keymaster_error_t SignUndigested(Buffer* output);
101 keymaster_error_t SignDigested(Buffer* output);
Shawn Willden0a4df7e2014-08-28 16:09:05 -0600102};
103
Shawn Willden61902362014-12-18 10:33:24 -0700104/**
105 * RSA public key verification operation.
106 */
107class RsaVerifyOperation : public RsaDigestingOperation {
Shawn Willden0a4df7e2014-08-28 16:09:05 -0600108 public:
Shawn Willden2bf4ad32015-06-01 07:33:51 -0600109 RsaVerifyOperation(keymaster_digest_t digest, keymaster_padding_t padding, EVP_PKEY* key)
Shawn Willden61902362014-12-18 10:33:24 -0700110 : RsaDigestingOperation(KM_PURPOSE_VERIFY, digest, padding, key) {}
Shawn Willden2bf4ad32015-06-01 07:33:51 -0600111
112 keymaster_error_t Begin(const AuthorizationSet& input_params,
113 AuthorizationSet* output_params) override;
114 keymaster_error_t Update(const AuthorizationSet& additional_params, const Buffer& input,
Shawn Willdended8e7d2015-06-01 15:29:12 -0600115 AuthorizationSet* output_params, Buffer* output,
116 size_t* input_consumed) override;
Shawn Willden06298102015-05-25 23:12:48 -0600117 keymaster_error_t Finish(const AuthorizationSet& additional_params, const Buffer& signature,
Shawn Willdended8e7d2015-06-01 15:29:12 -0600118 AuthorizationSet* output_params, Buffer* output) override;
Shawn Willden4200f212014-12-02 07:01:21 -0700119
120 private:
Shawn Willdenf90f2352014-12-18 23:01:15 -0700121 keymaster_error_t VerifyUndigested(const Buffer& signature);
122 keymaster_error_t VerifyDigested(const Buffer& signature);
Shawn Willden2bf4ad32015-06-01 07:33:51 -0600123};
124
125/**
126 * Base class for RSA crypting operations.
127 */
128class RsaCryptOperation : public RsaOperation {
129 public:
130 RsaCryptOperation(keymaster_purpose_t, keymaster_padding_t padding, EVP_PKEY* key)
131 : RsaOperation(KM_PURPOSE_ENCRYPT, padding, key) {}
132
133 private:
134 int GetOpensslPadding(keymaster_error_t* error) override;
Shawn Willden4200f212014-12-02 07:01:21 -0700135};
136
Shawn Willden61902362014-12-18 10:33:24 -0700137/**
138 * RSA public key encryption operation.
139 */
Shawn Willden2bf4ad32015-06-01 07:33:51 -0600140class RsaEncryptOperation : public RsaCryptOperation {
Shawn Willden4200f212014-12-02 07:01:21 -0700141 public:
Shawn Willden2bf4ad32015-06-01 07:33:51 -0600142 RsaEncryptOperation(keymaster_padding_t padding, EVP_PKEY* key)
143 : RsaCryptOperation(KM_PURPOSE_ENCRYPT, padding, key) {}
Shawn Willden06298102015-05-25 23:12:48 -0600144 keymaster_error_t Finish(const AuthorizationSet& additional_params, const Buffer& signature,
Shawn Willdended8e7d2015-06-01 15:29:12 -0600145 AuthorizationSet* output_params, Buffer* output) override;
Shawn Willden4200f212014-12-02 07:01:21 -0700146};
147
Shawn Willden61902362014-12-18 10:33:24 -0700148/**
149 * RSA private key decryption operation.
150 */
Shawn Willden2bf4ad32015-06-01 07:33:51 -0600151class RsaDecryptOperation : public RsaCryptOperation {
Shawn Willden4200f212014-12-02 07:01:21 -0700152 public:
Shawn Willden2bf4ad32015-06-01 07:33:51 -0600153 RsaDecryptOperation(keymaster_padding_t padding, EVP_PKEY* key)
154 : RsaCryptOperation(KM_PURPOSE_DECRYPT, padding, key) {}
Shawn Willden06298102015-05-25 23:12:48 -0600155 keymaster_error_t Finish(const AuthorizationSet& additional_params, const Buffer& signature,
Shawn Willdended8e7d2015-06-01 15:29:12 -0600156 AuthorizationSet* output_params, Buffer* output) override;
Shawn Willden06298102015-05-25 23:12:48 -0600157};
158
159/**
160 * Abstract base for all RSA operation factories. This class exists mainly to centralize some code
161 * common to all RSA operation factories.
162 */
163class RsaOperationFactory : public OperationFactory {
164 public:
165 KeyType registry_key() const override { return KeyType(KM_ALGORITHM_RSA, purpose()); }
166 virtual keymaster_purpose_t purpose() const = 0;
167
168 protected:
Shawn Willden2bf4ad32015-06-01 07:33:51 -0600169 static EVP_PKEY* GetRsaKey(const Key& key, keymaster_error_t* error);
Shawn Willden06298102015-05-25 23:12:48 -0600170};
171
172/**
173 * Abstract base for RSA operations that digest their input (signing and verification). This class
174 * does most of the work of creation of RSA digesting operations, delegating only the actual
175 * operation instantiation.
176 */
177class RsaDigestingOperationFactory : public RsaOperationFactory {
178 public:
179 virtual Operation* CreateOperation(const Key& key, const AuthorizationSet& begin_params,
180 keymaster_error_t* error);
181 const keymaster_digest_t* SupportedDigests(size_t* digest_count) const override;
182 const keymaster_padding_t* SupportedPaddingModes(size_t* padding_mode_count) const override;
183
184 private:
185 virtual Operation* InstantiateOperation(keymaster_digest_t digest, keymaster_padding_t padding,
Shawn Willden2bf4ad32015-06-01 07:33:51 -0600186 EVP_PKEY* key) = 0;
Shawn Willden06298102015-05-25 23:12:48 -0600187};
188
189/**
190 * Abstract base for en/de-crypting RSA operation factories. This class does most of the work of
191 * creating such operations, delegating only the actual operation instantiation.
192 */
193class RsaCryptingOperationFactory : public RsaOperationFactory {
194 public:
195 virtual Operation* CreateOperation(const Key& key, const AuthorizationSet& begin_params,
196 keymaster_error_t* error);
197 const keymaster_padding_t* SupportedPaddingModes(size_t* padding_mode_count) const override;
198 const keymaster_digest_t* SupportedDigests(size_t* digest_count) const override;
199
200 private:
Shawn Willden2bf4ad32015-06-01 07:33:51 -0600201 virtual Operation* InstantiateOperation(keymaster_padding_t padding, EVP_PKEY* key) = 0;
Shawn Willden06298102015-05-25 23:12:48 -0600202};
203
204/**
205 * Concrete factory for RSA signing operations.
206 */
207class RsaSigningOperationFactory : public RsaDigestingOperationFactory {
208 public:
209 keymaster_purpose_t purpose() const override { return KM_PURPOSE_SIGN; }
210 Operation* InstantiateOperation(keymaster_digest_t digest, keymaster_padding_t padding,
Shawn Willden2bf4ad32015-06-01 07:33:51 -0600211 EVP_PKEY* key) override {
Shawn Willden06298102015-05-25 23:12:48 -0600212 return new RsaSignOperation(digest, padding, key);
213 }
214};
215
216/**
217 * Concrete factory for RSA signing operations.
218 */
219class RsaVerificationOperationFactory : public RsaDigestingOperationFactory {
220 keymaster_purpose_t purpose() const override { return KM_PURPOSE_VERIFY; }
221 Operation* InstantiateOperation(keymaster_digest_t digest, keymaster_padding_t padding,
Shawn Willden2bf4ad32015-06-01 07:33:51 -0600222 EVP_PKEY* key) override {
Shawn Willden06298102015-05-25 23:12:48 -0600223 return new RsaVerifyOperation(digest, padding, key);
224 }
225};
226
227/**
228 * Concrete factory for RSA signing operations.
229 */
230class RsaEncryptionOperationFactory : public RsaCryptingOperationFactory {
231 keymaster_purpose_t purpose() const override { return KM_PURPOSE_ENCRYPT; }
Shawn Willden2bf4ad32015-06-01 07:33:51 -0600232 Operation* InstantiateOperation(keymaster_padding_t padding, EVP_PKEY* key) override {
Shawn Willden06298102015-05-25 23:12:48 -0600233 return new RsaEncryptOperation(padding, key);
234 }
235};
236
237/**
238 * Concrete factory for RSA signing operations.
239 */
240class RsaDecryptionOperationFactory : public RsaCryptingOperationFactory {
241 keymaster_purpose_t purpose() const override { return KM_PURPOSE_DECRYPT; }
Shawn Willden2bf4ad32015-06-01 07:33:51 -0600242 Operation* InstantiateOperation(keymaster_padding_t padding, EVP_PKEY* key) override {
Shawn Willden06298102015-05-25 23:12:48 -0600243 return new RsaDecryptOperation(padding, key);
244 }
Shawn Willden0a4df7e2014-08-28 16:09:05 -0600245};
246
247} // namespace keymaster
248
249#endif // SYSTEM_KEYMASTER_RSA_OPERATION_H_