blob: ee462831f7b8c9b6d0a7c8defecf84f6a57b4e96 [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 Willden567a4a02014-12-31 12:14:46 -070036 RsaOperation(keymaster_purpose_t purpose, keymaster_padding_t padding, RSA* key)
37 : 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,
45 Buffer* output, size_t* input_consumed) override;
46 keymaster_error_t Abort() override { return KM_ERROR_OK; }
Shawn Willden0a4df7e2014-08-28 16:09:05 -060047
48 protected:
Shawn Willdenb7361132014-12-08 08:15:14 -070049 keymaster_error_t StoreData(const Buffer& input, size_t* input_consumed);
Shawn Willden0a4df7e2014-08-28 16:09:05 -060050
51 RSA* rsa_key_;
Shawn Willden0a4df7e2014-08-28 16:09:05 -060052 keymaster_padding_t padding_;
53 Buffer data_;
54};
55
Shawn Willden61902362014-12-18 10:33:24 -070056/**
57 * Base class for all RSA operations.
58 *
59 * This class adds digesting support, for digesting modes. For non-digesting modes, it falls back
60 * on the RsaOperation input buffering.
61 */
62class RsaDigestingOperation : public RsaOperation {
63 public:
64 RsaDigestingOperation(keymaster_purpose_t purpose, keymaster_digest_t digest,
65 keymaster_padding_t padding, RSA* key);
66 ~RsaDigestingOperation();
67
Shawn Willden06298102015-05-25 23:12:48 -060068 keymaster_error_t Begin(const AuthorizationSet& input_params,
69 AuthorizationSet* output_params) override;
70 keymaster_error_t Update(const AuthorizationSet& additional_params, const Buffer& input,
71 Buffer* output, size_t* input_consumed) override;
Shawn Willden61902362014-12-18 10:33:24 -070072
73 protected:
Shawn Willdenf90f2352014-12-18 23:01:15 -070074 bool require_digest() const { return padding_ == KM_PAD_RSA_PSS; }
75 keymaster_error_t InitDigest();
76 keymaster_error_t UpdateDigest(const Buffer& input, size_t* input_consumed);
77 keymaster_error_t FinishDigest(unsigned* digest_size);
Shawn Willden61902362014-12-18 10:33:24 -070078
79 const keymaster_digest_t digest_;
80 const EVP_MD* digest_algorithm_;
81 EVP_MD_CTX digest_ctx_;
Shawn Willdenf90f2352014-12-18 23:01:15 -070082 uint8_t digest_buf_[EVP_MAX_MD_SIZE];
Shawn Willden61902362014-12-18 10:33:24 -070083};
84
85/**
86 * RSA private key signing operation.
87 */
88class RsaSignOperation : public RsaDigestingOperation {
Shawn Willden0a4df7e2014-08-28 16:09:05 -060089 public:
Shawn Willden567a4a02014-12-31 12:14:46 -070090 RsaSignOperation(keymaster_digest_t digest, keymaster_padding_t padding, RSA* key)
Shawn Willden61902362014-12-18 10:33:24 -070091 : RsaDigestingOperation(KM_PURPOSE_SIGN, digest, padding, key) {}
Shawn Willden06298102015-05-25 23:12:48 -060092 keymaster_error_t Finish(const AuthorizationSet& additional_params, const Buffer& signature,
93 Buffer* output) override;
Shawn Willden4200f212014-12-02 07:01:21 -070094
95 private:
Shawn Willdenf90f2352014-12-18 23:01:15 -070096 keymaster_error_t SignUndigested(Buffer* output);
97 keymaster_error_t SignDigested(Buffer* output);
98 keymaster_error_t PrivateEncrypt(uint8_t* to_encrypt, size_t len, int openssl_padding,
99 Buffer* output);
100 keymaster_error_t PssPadDigest(UniquePtr<uint8_t[]>* padded_digest);
Shawn Willden0a4df7e2014-08-28 16:09:05 -0600101};
102
Shawn Willden61902362014-12-18 10:33:24 -0700103/**
104 * RSA public key verification operation.
105 */
106class RsaVerifyOperation : public RsaDigestingOperation {
Shawn Willden0a4df7e2014-08-28 16:09:05 -0600107 public:
Shawn Willden567a4a02014-12-31 12:14:46 -0700108 RsaVerifyOperation(keymaster_digest_t digest, keymaster_padding_t padding, RSA* key)
Shawn Willden61902362014-12-18 10:33:24 -0700109 : RsaDigestingOperation(KM_PURPOSE_VERIFY, digest, padding, key) {}
Shawn Willden06298102015-05-25 23:12:48 -0600110 keymaster_error_t Finish(const AuthorizationSet& additional_params, const Buffer& signature,
111 Buffer* output) override;
Shawn Willden4200f212014-12-02 07:01:21 -0700112
113 private:
Shawn Willdenf90f2352014-12-18 23:01:15 -0700114 keymaster_error_t VerifyUndigested(const Buffer& signature);
115 keymaster_error_t VerifyDigested(const Buffer& signature);
116 keymaster_error_t DecryptAndMatch(const Buffer& signature, const uint8_t* to_match, size_t len);
Shawn Willden4200f212014-12-02 07:01:21 -0700117};
118
Shawn Willden61902362014-12-18 10:33:24 -0700119/**
120 * RSA public key encryption operation.
121 */
Shawn Willden4200f212014-12-02 07:01:21 -0700122class RsaEncryptOperation : public RsaOperation {
123 public:
Shawn Willden567a4a02014-12-31 12:14:46 -0700124 RsaEncryptOperation(keymaster_padding_t padding, RSA* key)
125 : RsaOperation(KM_PURPOSE_ENCRYPT, padding, key) {}
Shawn Willden06298102015-05-25 23:12:48 -0600126 keymaster_error_t Finish(const AuthorizationSet& additional_params, const Buffer& signature,
127 Buffer* output) override;
Shawn Willden4200f212014-12-02 07:01:21 -0700128};
129
Shawn Willden61902362014-12-18 10:33:24 -0700130/**
131 * RSA private key decryption operation.
132 */
Shawn Willden4200f212014-12-02 07:01:21 -0700133class RsaDecryptOperation : public RsaOperation {
134 public:
Shawn Willden567a4a02014-12-31 12:14:46 -0700135 RsaDecryptOperation(keymaster_padding_t padding, RSA* key)
136 : RsaOperation(KM_PURPOSE_DECRYPT, padding, key) {}
Shawn Willden06298102015-05-25 23:12:48 -0600137 keymaster_error_t Finish(const AuthorizationSet& additional_params, const Buffer& signature,
138 Buffer* output) override;
139};
140
141/**
142 * Abstract base for all RSA operation factories. This class exists mainly to centralize some code
143 * common to all RSA operation factories.
144 */
145class RsaOperationFactory : public OperationFactory {
146 public:
147 KeyType registry_key() const override { return KeyType(KM_ALGORITHM_RSA, purpose()); }
148 virtual keymaster_purpose_t purpose() const = 0;
149
150 protected:
151 bool GetAndValidatePadding(const AuthorizationSet& begin_params, const Key& key,
152 keymaster_padding_t* padding, keymaster_error_t* error) const;
153 bool GetAndValidateDigest(const AuthorizationSet& begin_params, const Key& key,
154 keymaster_digest_t* digest, keymaster_error_t* error) const;
155 static RSA* GetRsaKey(const Key& key, keymaster_error_t* error);
156};
157
158/**
159 * Abstract base for RSA operations that digest their input (signing and verification). This class
160 * does most of the work of creation of RSA digesting operations, delegating only the actual
161 * operation instantiation.
162 */
163class RsaDigestingOperationFactory : public RsaOperationFactory {
164 public:
165 virtual Operation* CreateOperation(const Key& key, const AuthorizationSet& begin_params,
166 keymaster_error_t* error);
167 const keymaster_digest_t* SupportedDigests(size_t* digest_count) const override;
168 const keymaster_padding_t* SupportedPaddingModes(size_t* padding_mode_count) const override;
169
170 private:
171 virtual Operation* InstantiateOperation(keymaster_digest_t digest, keymaster_padding_t padding,
172 RSA* key) = 0;
173};
174
175/**
176 * Abstract base for en/de-crypting RSA operation factories. This class does most of the work of
177 * creating such operations, delegating only the actual operation instantiation.
178 */
179class RsaCryptingOperationFactory : public RsaOperationFactory {
180 public:
181 virtual Operation* CreateOperation(const Key& key, const AuthorizationSet& begin_params,
182 keymaster_error_t* error);
183 const keymaster_padding_t* SupportedPaddingModes(size_t* padding_mode_count) const override;
184 const keymaster_digest_t* SupportedDigests(size_t* digest_count) const override;
185
186 private:
187 virtual Operation* InstantiateOperation(keymaster_padding_t padding, RSA* key) = 0;
188};
189
190/**
191 * Concrete factory for RSA signing operations.
192 */
193class RsaSigningOperationFactory : public RsaDigestingOperationFactory {
194 public:
195 keymaster_purpose_t purpose() const override { return KM_PURPOSE_SIGN; }
196 Operation* InstantiateOperation(keymaster_digest_t digest, keymaster_padding_t padding,
197 RSA* key) override {
198 return new RsaSignOperation(digest, padding, key);
199 }
200};
201
202/**
203 * Concrete factory for RSA signing operations.
204 */
205class RsaVerificationOperationFactory : public RsaDigestingOperationFactory {
206 keymaster_purpose_t purpose() const override { return KM_PURPOSE_VERIFY; }
207 Operation* InstantiateOperation(keymaster_digest_t digest, keymaster_padding_t padding,
208 RSA* key) override {
209 return new RsaVerifyOperation(digest, padding, key);
210 }
211};
212
213/**
214 * Concrete factory for RSA signing operations.
215 */
216class RsaEncryptionOperationFactory : public RsaCryptingOperationFactory {
217 keymaster_purpose_t purpose() const override { return KM_PURPOSE_ENCRYPT; }
218 Operation* InstantiateOperation(keymaster_padding_t padding, RSA* key) override {
219 return new RsaEncryptOperation(padding, key);
220 }
221};
222
223/**
224 * Concrete factory for RSA signing operations.
225 */
226class RsaDecryptionOperationFactory : public RsaCryptingOperationFactory {
227 keymaster_purpose_t purpose() const override { return KM_PURPOSE_DECRYPT; }
228 Operation* InstantiateOperation(keymaster_padding_t padding, RSA* key) override {
229 return new RsaDecryptOperation(padding, key);
230 }
Shawn Willden0a4df7e2014-08-28 16:09:05 -0600231};
232
233} // namespace keymaster
234
235#endif // SYSTEM_KEYMASTER_RSA_OPERATION_H_