blob: 2c9680fb579f895f170676b55304c908000b124a [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 Willden4200f212014-12-02 07:01:21 -070033 RsaOperation(keymaster_purpose_t purpose, const Logger& logger, keymaster_padding_t padding,
34 RSA* key)
35 : Operation(purpose, logger), rsa_key_(key), padding_(padding) {}
Shawn Willden0a4df7e2014-08-28 16:09:05 -060036 ~RsaOperation();
37
Shawn Willden111edb32015-02-05 22:44:24 -070038 virtual keymaster_error_t Begin(const AuthorizationSet& /* input_params */,
39 AuthorizationSet* /* output_params */) {
40 return KM_ERROR_OK;
41 }
Shawn Willden6bfbff02015-02-06 19:48:24 -070042 virtual keymaster_error_t Update(const AuthorizationSet& additional_params, const Buffer& input,
43 Buffer* output, size_t* input_consumed);
Shawn Willden0a4df7e2014-08-28 16:09:05 -060044 virtual keymaster_error_t Abort() { return KM_ERROR_OK; }
45
46 protected:
Shawn Willdenb7361132014-12-08 08:15:14 -070047 keymaster_error_t StoreData(const Buffer& input, size_t* input_consumed);
Shawn Willden0a4df7e2014-08-28 16:09:05 -060048
49 RSA* rsa_key_;
Shawn Willden0a4df7e2014-08-28 16:09:05 -060050 keymaster_padding_t padding_;
51 Buffer data_;
52};
53
54class RsaSignOperation : public RsaOperation {
55 public:
Shawn Willden4200f212014-12-02 07:01:21 -070056 RsaSignOperation(const Logger& logger, keymaster_digest_t digest, keymaster_padding_t padding,
57 RSA* key)
58 : RsaOperation(KM_PURPOSE_SIGN, logger, padding, key), digest_(digest) {}
Shawn Willden6bfbff02015-02-06 19:48:24 -070059 virtual keymaster_error_t Finish(const AuthorizationSet& additional_params,
60 const Buffer& signature, Buffer* output);
Shawn Willden4200f212014-12-02 07:01:21 -070061
62 private:
63 keymaster_digest_t digest_;
Shawn Willden0a4df7e2014-08-28 16:09:05 -060064};
65
66class RsaVerifyOperation : public RsaOperation {
67 public:
Shawn Willden4200f212014-12-02 07:01:21 -070068 RsaVerifyOperation(const Logger& logger, keymaster_digest_t digest, keymaster_padding_t padding,
69 RSA* key)
70 : RsaOperation(KM_PURPOSE_VERIFY, logger, padding, key), digest_(digest) {}
Shawn Willden6bfbff02015-02-06 19:48:24 -070071 virtual keymaster_error_t Finish(const AuthorizationSet& additional_params,
72 const Buffer& signature, Buffer* output);
Shawn Willden4200f212014-12-02 07:01:21 -070073
74 private:
75 keymaster_digest_t digest_;
76};
77
78class RsaEncryptOperation : public RsaOperation {
79 public:
80 RsaEncryptOperation(const Logger& logger, keymaster_padding_t padding, RSA* key)
81 : RsaOperation(KM_PURPOSE_ENCRYPT, logger, padding, key) {}
Shawn Willden6bfbff02015-02-06 19:48:24 -070082 virtual keymaster_error_t Finish(const AuthorizationSet& additional_params,
83 const Buffer& signature, Buffer* output);
Shawn Willden4200f212014-12-02 07:01:21 -070084};
85
86class RsaDecryptOperation : public RsaOperation {
87 public:
88 RsaDecryptOperation(const Logger& logger, keymaster_padding_t padding, RSA* key)
89 : RsaOperation(KM_PURPOSE_DECRYPT, logger, padding, key) {}
Shawn Willden6bfbff02015-02-06 19:48:24 -070090 virtual keymaster_error_t Finish(const AuthorizationSet& additional_params,
91 const Buffer& signature, Buffer* output);
Shawn Willden0a4df7e2014-08-28 16:09:05 -060092};
93
94} // namespace keymaster
95
96#endif // SYSTEM_KEYMASTER_RSA_OPERATION_H_