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 | |
Shawn Willden | 6190236 | 2014-12-18 10:33:24 -0700 | [diff] [blame] | 31 | /** |
| 32 | * Base class for all RSA operations. |
| 33 | * |
| 34 | * This class provides RSA key management, plus buffering of data for non-digesting modes. |
| 35 | */ |
Shawn Willden | 0a4df7e | 2014-08-28 16:09:05 -0600 | [diff] [blame] | 36 | class RsaOperation : public Operation { |
| 37 | public: |
Shawn Willden | 567a4a0 | 2014-12-31 12:14:46 -0700 | [diff] [blame] | 38 | RsaOperation(keymaster_purpose_t purpose, keymaster_padding_t padding, RSA* key) |
| 39 | : Operation(purpose), rsa_key_(key), padding_(padding) {} |
Shawn Willden | 0a4df7e | 2014-08-28 16:09:05 -0600 | [diff] [blame] | 40 | ~RsaOperation(); |
| 41 | |
Shawn Willden | 111edb3 | 2015-02-05 22:44:24 -0700 | [diff] [blame] | 42 | virtual keymaster_error_t Begin(const AuthorizationSet& /* input_params */, |
| 43 | AuthorizationSet* /* output_params */) { |
| 44 | return KM_ERROR_OK; |
| 45 | } |
Shawn Willden | 6bfbff0 | 2015-02-06 19:48:24 -0700 | [diff] [blame] | 46 | virtual keymaster_error_t Update(const AuthorizationSet& additional_params, const Buffer& input, |
| 47 | Buffer* output, size_t* input_consumed); |
Shawn Willden | 0a4df7e | 2014-08-28 16:09:05 -0600 | [diff] [blame] | 48 | virtual keymaster_error_t Abort() { return KM_ERROR_OK; } |
| 49 | |
| 50 | protected: |
Shawn Willden | b736113 | 2014-12-08 08:15:14 -0700 | [diff] [blame] | 51 | keymaster_error_t StoreData(const Buffer& input, size_t* input_consumed); |
Shawn Willden | 0a4df7e | 2014-08-28 16:09:05 -0600 | [diff] [blame] | 52 | |
| 53 | RSA* rsa_key_; |
Shawn Willden | 0a4df7e | 2014-08-28 16:09:05 -0600 | [diff] [blame] | 54 | keymaster_padding_t padding_; |
| 55 | Buffer data_; |
| 56 | }; |
| 57 | |
Shawn Willden | 6190236 | 2014-12-18 10:33:24 -0700 | [diff] [blame] | 58 | /** |
| 59 | * Base class for all RSA operations. |
| 60 | * |
| 61 | * This class adds digesting support, for digesting modes. For non-digesting modes, it falls back |
| 62 | * on the RsaOperation input buffering. |
| 63 | */ |
| 64 | class RsaDigestingOperation : public RsaOperation { |
| 65 | public: |
| 66 | RsaDigestingOperation(keymaster_purpose_t purpose, keymaster_digest_t digest, |
| 67 | keymaster_padding_t padding, RSA* key); |
| 68 | ~RsaDigestingOperation(); |
| 69 | |
| 70 | virtual keymaster_error_t Begin(const AuthorizationSet& input_params, |
| 71 | AuthorizationSet* output_params); |
| 72 | virtual keymaster_error_t Update(const AuthorizationSet& additional_params, const Buffer& input, |
| 73 | Buffer* output, size_t* input_consumed); |
| 74 | |
| 75 | protected: |
Shawn Willden | f90f235 | 2014-12-18 23:01:15 -0700 | [diff] [blame] | 76 | bool require_digest() const { return padding_ == KM_PAD_RSA_PSS; } |
| 77 | keymaster_error_t InitDigest(); |
| 78 | keymaster_error_t UpdateDigest(const Buffer& input, size_t* input_consumed); |
| 79 | keymaster_error_t FinishDigest(unsigned* digest_size); |
Shawn Willden | 6190236 | 2014-12-18 10:33:24 -0700 | [diff] [blame] | 80 | |
| 81 | const keymaster_digest_t digest_; |
| 82 | const EVP_MD* digest_algorithm_; |
| 83 | EVP_MD_CTX digest_ctx_; |
Shawn Willden | f90f235 | 2014-12-18 23:01:15 -0700 | [diff] [blame] | 84 | uint8_t digest_buf_[EVP_MAX_MD_SIZE]; |
Shawn Willden | 6190236 | 2014-12-18 10:33:24 -0700 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | /** |
| 88 | * RSA private key signing operation. |
| 89 | */ |
| 90 | class RsaSignOperation : public RsaDigestingOperation { |
Shawn Willden | 0a4df7e | 2014-08-28 16:09:05 -0600 | [diff] [blame] | 91 | public: |
Shawn Willden | 567a4a0 | 2014-12-31 12:14:46 -0700 | [diff] [blame] | 92 | RsaSignOperation(keymaster_digest_t digest, keymaster_padding_t padding, RSA* key) |
Shawn Willden | 6190236 | 2014-12-18 10:33:24 -0700 | [diff] [blame] | 93 | : RsaDigestingOperation(KM_PURPOSE_SIGN, digest, padding, key) {} |
Shawn Willden | 6bfbff0 | 2015-02-06 19:48:24 -0700 | [diff] [blame] | 94 | virtual keymaster_error_t Finish(const AuthorizationSet& additional_params, |
| 95 | const Buffer& signature, Buffer* output); |
Shawn Willden | 4200f21 | 2014-12-02 07:01:21 -0700 | [diff] [blame] | 96 | |
| 97 | private: |
Shawn Willden | f90f235 | 2014-12-18 23:01:15 -0700 | [diff] [blame] | 98 | keymaster_error_t SignUndigested(Buffer* output); |
| 99 | keymaster_error_t SignDigested(Buffer* output); |
| 100 | keymaster_error_t PrivateEncrypt(uint8_t* to_encrypt, size_t len, int openssl_padding, |
| 101 | Buffer* output); |
| 102 | keymaster_error_t PssPadDigest(UniquePtr<uint8_t[]>* padded_digest); |
Shawn Willden | 0a4df7e | 2014-08-28 16:09:05 -0600 | [diff] [blame] | 103 | }; |
| 104 | |
Shawn Willden | 6190236 | 2014-12-18 10:33:24 -0700 | [diff] [blame] | 105 | /** |
| 106 | * RSA public key verification operation. |
| 107 | */ |
| 108 | class RsaVerifyOperation : public RsaDigestingOperation { |
Shawn Willden | 0a4df7e | 2014-08-28 16:09:05 -0600 | [diff] [blame] | 109 | public: |
Shawn Willden | 567a4a0 | 2014-12-31 12:14:46 -0700 | [diff] [blame] | 110 | RsaVerifyOperation(keymaster_digest_t digest, keymaster_padding_t padding, RSA* key) |
Shawn Willden | 6190236 | 2014-12-18 10:33:24 -0700 | [diff] [blame] | 111 | : RsaDigestingOperation(KM_PURPOSE_VERIFY, digest, padding, key) {} |
Shawn Willden | 6bfbff0 | 2015-02-06 19:48:24 -0700 | [diff] [blame] | 112 | virtual keymaster_error_t Finish(const AuthorizationSet& additional_params, |
| 113 | const Buffer& signature, Buffer* output); |
Shawn Willden | 4200f21 | 2014-12-02 07:01:21 -0700 | [diff] [blame] | 114 | |
| 115 | private: |
Shawn Willden | f90f235 | 2014-12-18 23:01:15 -0700 | [diff] [blame] | 116 | keymaster_error_t VerifyUndigested(const Buffer& signature); |
| 117 | keymaster_error_t VerifyDigested(const Buffer& signature); |
| 118 | keymaster_error_t DecryptAndMatch(const Buffer& signature, const uint8_t* to_match, size_t len); |
Shawn Willden | 4200f21 | 2014-12-02 07:01:21 -0700 | [diff] [blame] | 119 | }; |
| 120 | |
Shawn Willden | 6190236 | 2014-12-18 10:33:24 -0700 | [diff] [blame] | 121 | /** |
| 122 | * RSA public key encryption operation. |
| 123 | */ |
Shawn Willden | 4200f21 | 2014-12-02 07:01:21 -0700 | [diff] [blame] | 124 | class RsaEncryptOperation : public RsaOperation { |
| 125 | public: |
Shawn Willden | 567a4a0 | 2014-12-31 12:14:46 -0700 | [diff] [blame] | 126 | RsaEncryptOperation(keymaster_padding_t padding, RSA* key) |
| 127 | : RsaOperation(KM_PURPOSE_ENCRYPT, padding, key) {} |
Shawn Willden | 6bfbff0 | 2015-02-06 19:48:24 -0700 | [diff] [blame] | 128 | virtual keymaster_error_t Finish(const AuthorizationSet& additional_params, |
| 129 | const Buffer& signature, Buffer* output); |
Shawn Willden | 4200f21 | 2014-12-02 07:01:21 -0700 | [diff] [blame] | 130 | }; |
| 131 | |
Shawn Willden | 6190236 | 2014-12-18 10:33:24 -0700 | [diff] [blame] | 132 | /** |
| 133 | * RSA private key decryption operation. |
| 134 | */ |
Shawn Willden | 4200f21 | 2014-12-02 07:01:21 -0700 | [diff] [blame] | 135 | class RsaDecryptOperation : public RsaOperation { |
| 136 | public: |
Shawn Willden | 567a4a0 | 2014-12-31 12:14:46 -0700 | [diff] [blame] | 137 | RsaDecryptOperation(keymaster_padding_t padding, RSA* key) |
| 138 | : RsaOperation(KM_PURPOSE_DECRYPT, padding, key) {} |
Shawn Willden | 6bfbff0 | 2015-02-06 19:48:24 -0700 | [diff] [blame] | 139 | virtual keymaster_error_t Finish(const AuthorizationSet& additional_params, |
| 140 | const Buffer& signature, Buffer* output); |
Shawn Willden | 0a4df7e | 2014-08-28 16:09:05 -0600 | [diff] [blame] | 141 | }; |
| 142 | |
| 143 | } // namespace keymaster |
| 144 | |
| 145 | #endif // SYSTEM_KEYMASTER_RSA_OPERATION_H_ |