Shawn Willden | 0a4df7e | 2014-08-28 16:09:05 -0600 | [diff] [blame] | 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 <UniquePtr.h> |
| 21 | |
Shawn Willden | 63ac043 | 2014-12-29 14:07:08 -0700 | [diff] [blame] | 22 | #include <openssl/evp.h> |
| 23 | #include <openssl/rsa.h> |
| 24 | |
Shawn Willden | 0a4df7e | 2014-08-28 16:09:05 -0600 | [diff] [blame] | 25 | #include <keymaster/key_blob.h> |
| 26 | |
| 27 | #include "operation.h" |
| 28 | |
| 29 | namespace keymaster { |
| 30 | |
| 31 | class RsaOperation : public Operation { |
| 32 | public: |
Shawn Willden | 567a4a0 | 2014-12-31 12:14:46 -0700 | [diff] [blame^] | 33 | RsaOperation(keymaster_purpose_t purpose, keymaster_padding_t padding, RSA* key) |
| 34 | : Operation(purpose), rsa_key_(key), padding_(padding) {} |
Shawn Willden | 0a4df7e | 2014-08-28 16:09:05 -0600 | [diff] [blame] | 35 | ~RsaOperation(); |
| 36 | |
Shawn Willden | 111edb3 | 2015-02-05 22:44:24 -0700 | [diff] [blame] | 37 | virtual keymaster_error_t Begin(const AuthorizationSet& /* input_params */, |
| 38 | AuthorizationSet* /* output_params */) { |
| 39 | return KM_ERROR_OK; |
| 40 | } |
Shawn Willden | 6bfbff0 | 2015-02-06 19:48:24 -0700 | [diff] [blame] | 41 | virtual keymaster_error_t Update(const AuthorizationSet& additional_params, const Buffer& input, |
| 42 | Buffer* output, size_t* input_consumed); |
Shawn Willden | 0a4df7e | 2014-08-28 16:09:05 -0600 | [diff] [blame] | 43 | virtual keymaster_error_t Abort() { return KM_ERROR_OK; } |
| 44 | |
| 45 | protected: |
Shawn Willden | b736113 | 2014-12-08 08:15:14 -0700 | [diff] [blame] | 46 | keymaster_error_t StoreData(const Buffer& input, size_t* input_consumed); |
Shawn Willden | 0a4df7e | 2014-08-28 16:09:05 -0600 | [diff] [blame] | 47 | |
| 48 | RSA* rsa_key_; |
Shawn Willden | 0a4df7e | 2014-08-28 16:09:05 -0600 | [diff] [blame] | 49 | keymaster_padding_t padding_; |
| 50 | Buffer data_; |
| 51 | }; |
| 52 | |
| 53 | class RsaSignOperation : public RsaOperation { |
| 54 | public: |
Shawn Willden | 567a4a0 | 2014-12-31 12:14:46 -0700 | [diff] [blame^] | 55 | RsaSignOperation(keymaster_digest_t digest, keymaster_padding_t padding, RSA* key) |
| 56 | : RsaOperation(KM_PURPOSE_SIGN, padding, key), digest_(digest) {} |
Shawn Willden | 6bfbff0 | 2015-02-06 19:48:24 -0700 | [diff] [blame] | 57 | virtual keymaster_error_t Finish(const AuthorizationSet& additional_params, |
| 58 | const Buffer& signature, Buffer* output); |
Shawn Willden | 4200f21 | 2014-12-02 07:01:21 -0700 | [diff] [blame] | 59 | |
| 60 | private: |
| 61 | keymaster_digest_t digest_; |
Shawn Willden | 0a4df7e | 2014-08-28 16:09:05 -0600 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | class RsaVerifyOperation : public RsaOperation { |
| 65 | public: |
Shawn Willden | 567a4a0 | 2014-12-31 12:14:46 -0700 | [diff] [blame^] | 66 | RsaVerifyOperation(keymaster_digest_t digest, keymaster_padding_t padding, RSA* key) |
| 67 | : RsaOperation(KM_PURPOSE_VERIFY, padding, key), digest_(digest) {} |
Shawn Willden | 6bfbff0 | 2015-02-06 19:48:24 -0700 | [diff] [blame] | 68 | virtual keymaster_error_t Finish(const AuthorizationSet& additional_params, |
| 69 | const Buffer& signature, Buffer* output); |
Shawn Willden | 4200f21 | 2014-12-02 07:01:21 -0700 | [diff] [blame] | 70 | |
| 71 | private: |
| 72 | keymaster_digest_t digest_; |
| 73 | }; |
| 74 | |
| 75 | class RsaEncryptOperation : public RsaOperation { |
| 76 | public: |
Shawn Willden | 567a4a0 | 2014-12-31 12:14:46 -0700 | [diff] [blame^] | 77 | RsaEncryptOperation(keymaster_padding_t padding, RSA* key) |
| 78 | : RsaOperation(KM_PURPOSE_ENCRYPT, padding, key) {} |
Shawn Willden | 6bfbff0 | 2015-02-06 19:48:24 -0700 | [diff] [blame] | 79 | virtual keymaster_error_t Finish(const AuthorizationSet& additional_params, |
| 80 | const Buffer& signature, Buffer* output); |
Shawn Willden | 4200f21 | 2014-12-02 07:01:21 -0700 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | class RsaDecryptOperation : public RsaOperation { |
| 84 | public: |
Shawn Willden | 567a4a0 | 2014-12-31 12:14:46 -0700 | [diff] [blame^] | 85 | RsaDecryptOperation(keymaster_padding_t padding, RSA* key) |
| 86 | : RsaOperation(KM_PURPOSE_DECRYPT, padding, key) {} |
Shawn Willden | 6bfbff0 | 2015-02-06 19:48:24 -0700 | [diff] [blame] | 87 | virtual keymaster_error_t Finish(const AuthorizationSet& additional_params, |
| 88 | const Buffer& signature, Buffer* output); |
Shawn Willden | 0a4df7e | 2014-08-28 16:09:05 -0600 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | } // namespace keymaster |
| 92 | |
| 93 | #endif // SYSTEM_KEYMASTER_RSA_OPERATION_H_ |