blob: 99530f838260dd1d379c87962802a7b2ea2808ff [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 <keymaster/key_blob.h>
26
27#include "operation.h"
28
29namespace keymaster {
30
31class RsaOperation : public Operation {
32 public:
Shawn Willden567a4a02014-12-31 12:14:46 -070033 RsaOperation(keymaster_purpose_t purpose, keymaster_padding_t padding, RSA* key)
34 : Operation(purpose), rsa_key_(key), padding_(padding) {}
Shawn Willden0a4df7e2014-08-28 16:09:05 -060035 ~RsaOperation();
36
Shawn Willden111edb32015-02-05 22:44:24 -070037 virtual keymaster_error_t Begin(const AuthorizationSet& /* input_params */,
38 AuthorizationSet* /* output_params */) {
39 return KM_ERROR_OK;
40 }
Shawn Willden6bfbff02015-02-06 19:48:24 -070041 virtual keymaster_error_t Update(const AuthorizationSet& additional_params, const Buffer& input,
42 Buffer* output, size_t* input_consumed);
Shawn Willden0a4df7e2014-08-28 16:09:05 -060043 virtual keymaster_error_t Abort() { return KM_ERROR_OK; }
44
45 protected:
Shawn Willdenb7361132014-12-08 08:15:14 -070046 keymaster_error_t StoreData(const Buffer& input, size_t* input_consumed);
Shawn Willden0a4df7e2014-08-28 16:09:05 -060047
48 RSA* rsa_key_;
Shawn Willden0a4df7e2014-08-28 16:09:05 -060049 keymaster_padding_t padding_;
50 Buffer data_;
51};
52
53class RsaSignOperation : public RsaOperation {
54 public:
Shawn Willden567a4a02014-12-31 12:14:46 -070055 RsaSignOperation(keymaster_digest_t digest, keymaster_padding_t padding, RSA* key)
56 : RsaOperation(KM_PURPOSE_SIGN, padding, key), digest_(digest) {}
Shawn Willden6bfbff02015-02-06 19:48:24 -070057 virtual keymaster_error_t Finish(const AuthorizationSet& additional_params,
58 const Buffer& signature, Buffer* output);
Shawn Willden4200f212014-12-02 07:01:21 -070059
60 private:
61 keymaster_digest_t digest_;
Shawn Willden0a4df7e2014-08-28 16:09:05 -060062};
63
64class RsaVerifyOperation : public RsaOperation {
65 public:
Shawn Willden567a4a02014-12-31 12:14:46 -070066 RsaVerifyOperation(keymaster_digest_t digest, keymaster_padding_t padding, RSA* key)
67 : RsaOperation(KM_PURPOSE_VERIFY, padding, key), digest_(digest) {}
Shawn Willden6bfbff02015-02-06 19:48:24 -070068 virtual keymaster_error_t Finish(const AuthorizationSet& additional_params,
69 const Buffer& signature, Buffer* output);
Shawn Willden4200f212014-12-02 07:01:21 -070070
71 private:
72 keymaster_digest_t digest_;
73};
74
75class RsaEncryptOperation : public RsaOperation {
76 public:
Shawn Willden567a4a02014-12-31 12:14:46 -070077 RsaEncryptOperation(keymaster_padding_t padding, RSA* key)
78 : RsaOperation(KM_PURPOSE_ENCRYPT, padding, key) {}
Shawn Willden6bfbff02015-02-06 19:48:24 -070079 virtual keymaster_error_t Finish(const AuthorizationSet& additional_params,
80 const Buffer& signature, Buffer* output);
Shawn Willden4200f212014-12-02 07:01:21 -070081};
82
83class RsaDecryptOperation : public RsaOperation {
84 public:
Shawn Willden567a4a02014-12-31 12:14:46 -070085 RsaDecryptOperation(keymaster_padding_t padding, RSA* key)
86 : RsaOperation(KM_PURPOSE_DECRYPT, padding, key) {}
Shawn Willden6bfbff02015-02-06 19:48:24 -070087 virtual keymaster_error_t Finish(const AuthorizationSet& additional_params,
88 const Buffer& signature, Buffer* output);
Shawn Willden0a4df7e2014-08-28 16:09:05 -060089};
90
91} // namespace keymaster
92
93#endif // SYSTEM_KEYMASTER_RSA_OPERATION_H_