blob: 586aaa1bee93abc9158ef001b9e5a07a2f2b733a [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 Willden111edb32015-02-05 22:44:24 -070040 virtual keymaster_error_t Begin(const AuthorizationSet& /* input_params */,
41 AuthorizationSet* /* output_params */) {
42 return KM_ERROR_OK;
43 }
Shawn Willden6bfbff02015-02-06 19:48:24 -070044 virtual keymaster_error_t Update(const AuthorizationSet& additional_params, const Buffer& input,
45 Buffer* output, size_t* input_consumed);
Shawn Willden0a4df7e2014-08-28 16:09:05 -060046 virtual keymaster_error_t Abort() { return KM_ERROR_OK; }
47
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
68 virtual keymaster_error_t Begin(const AuthorizationSet& input_params,
69 AuthorizationSet* output_params);
70 virtual keymaster_error_t Update(const AuthorizationSet& additional_params, const Buffer& input,
71 Buffer* output, size_t* input_consumed);
72
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 Willden6bfbff02015-02-06 19:48:24 -070092 virtual keymaster_error_t Finish(const AuthorizationSet& additional_params,
93 const Buffer& signature, Buffer* output);
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 Willden6bfbff02015-02-06 19:48:24 -0700110 virtual keymaster_error_t Finish(const AuthorizationSet& additional_params,
111 const Buffer& signature, Buffer* output);
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 Willden6bfbff02015-02-06 19:48:24 -0700126 virtual keymaster_error_t Finish(const AuthorizationSet& additional_params,
127 const Buffer& signature, Buffer* output);
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 Willden6bfbff02015-02-06 19:48:24 -0700137 virtual keymaster_error_t Finish(const AuthorizationSet& additional_params,
138 const Buffer& signature, Buffer* output);
Shawn Willden0a4df7e2014-08-28 16:09:05 -0600139};
140
141} // namespace keymaster
142
143#endif // SYSTEM_KEYMASTER_RSA_OPERATION_H_