blob: f890e2f907b46cfdb78ca43b14a9484ce314393b [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
Shawn Willden61902362014-12-18 10:33:24 -070031/**
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 Willden0a4df7e2014-08-28 16:09:05 -060036class RsaOperation : public Operation {
37 public:
Shawn Willden567a4a02014-12-31 12:14:46 -070038 RsaOperation(keymaster_purpose_t purpose, keymaster_padding_t padding, RSA* key)
39 : Operation(purpose), rsa_key_(key), padding_(padding) {}
Shawn Willden0a4df7e2014-08-28 16:09:05 -060040 ~RsaOperation();
41
Shawn Willden111edb32015-02-05 22:44:24 -070042 virtual keymaster_error_t Begin(const AuthorizationSet& /* input_params */,
43 AuthorizationSet* /* output_params */) {
44 return KM_ERROR_OK;
45 }
Shawn Willden6bfbff02015-02-06 19:48:24 -070046 virtual keymaster_error_t Update(const AuthorizationSet& additional_params, const Buffer& input,
47 Buffer* output, size_t* input_consumed);
Shawn Willden0a4df7e2014-08-28 16:09:05 -060048 virtual keymaster_error_t Abort() { return KM_ERROR_OK; }
49
50 protected:
Shawn Willdenb7361132014-12-08 08:15:14 -070051 keymaster_error_t StoreData(const Buffer& input, size_t* input_consumed);
Shawn Willden0a4df7e2014-08-28 16:09:05 -060052
53 RSA* rsa_key_;
Shawn Willden0a4df7e2014-08-28 16:09:05 -060054 keymaster_padding_t padding_;
55 Buffer data_;
56};
57
Shawn Willden61902362014-12-18 10:33:24 -070058/**
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 */
64class 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:
76 uint8_t* FinishDigest(unsigned* digest_size);
77
78 const keymaster_digest_t digest_;
79 const EVP_MD* digest_algorithm_;
80 EVP_MD_CTX digest_ctx_;
81};
82
83/**
84 * RSA private key signing operation.
85 */
86class RsaSignOperation : public RsaDigestingOperation {
Shawn Willden0a4df7e2014-08-28 16:09:05 -060087 public:
Shawn Willden567a4a02014-12-31 12:14:46 -070088 RsaSignOperation(keymaster_digest_t digest, keymaster_padding_t padding, RSA* key)
Shawn Willden61902362014-12-18 10:33:24 -070089 : RsaDigestingOperation(KM_PURPOSE_SIGN, digest, 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 Willden4200f212014-12-02 07:01:21 -070092
93 private:
Shawn Willden61902362014-12-18 10:33:24 -070094 int SignUndigested(Buffer* output);
95 int SignDigested(Buffer* output);
Shawn Willden0a4df7e2014-08-28 16:09:05 -060096};
97
Shawn Willden61902362014-12-18 10:33:24 -070098/**
99 * RSA public key verification operation.
100 */
101class RsaVerifyOperation : public RsaDigestingOperation {
Shawn Willden0a4df7e2014-08-28 16:09:05 -0600102 public:
Shawn Willden567a4a02014-12-31 12:14:46 -0700103 RsaVerifyOperation(keymaster_digest_t digest, keymaster_padding_t padding, RSA* key)
Shawn Willden61902362014-12-18 10:33:24 -0700104 : RsaDigestingOperation(KM_PURPOSE_VERIFY, digest, padding, key) {}
Shawn Willden6bfbff02015-02-06 19:48:24 -0700105 virtual keymaster_error_t Finish(const AuthorizationSet& additional_params,
106 const Buffer& signature, Buffer* output);
Shawn Willden4200f212014-12-02 07:01:21 -0700107
108 private:
Shawn Willden61902362014-12-18 10:33:24 -0700109 keymaster_error_t VerifyUndigested(uint8_t* decrypted_data);
110 keymaster_error_t VerifyDigested(uint8_t* decrypted_data);
Shawn Willden4200f212014-12-02 07:01:21 -0700111};
112
Shawn Willden61902362014-12-18 10:33:24 -0700113/**
114 * RSA public key encryption operation.
115 */
Shawn Willden4200f212014-12-02 07:01:21 -0700116class RsaEncryptOperation : public RsaOperation {
117 public:
Shawn Willden567a4a02014-12-31 12:14:46 -0700118 RsaEncryptOperation(keymaster_padding_t padding, RSA* key)
119 : RsaOperation(KM_PURPOSE_ENCRYPT, padding, key) {}
Shawn Willden6bfbff02015-02-06 19:48:24 -0700120 virtual keymaster_error_t Finish(const AuthorizationSet& additional_params,
121 const Buffer& signature, Buffer* output);
Shawn Willden4200f212014-12-02 07:01:21 -0700122};
123
Shawn Willden61902362014-12-18 10:33:24 -0700124/**
125 * RSA private key decryption operation.
126 */
Shawn Willden4200f212014-12-02 07:01:21 -0700127class RsaDecryptOperation : public RsaOperation {
128 public:
Shawn Willden567a4a02014-12-31 12:14:46 -0700129 RsaDecryptOperation(keymaster_padding_t padding, RSA* key)
130 : RsaOperation(KM_PURPOSE_DECRYPT, padding, key) {}
Shawn Willden6bfbff02015-02-06 19:48:24 -0700131 virtual keymaster_error_t Finish(const AuthorizationSet& additional_params,
132 const Buffer& signature, Buffer* output);
Shawn Willden0a4df7e2014-08-28 16:09:05 -0600133};
134
135} // namespace keymaster
136
137#endif // SYSTEM_KEYMASTER_RSA_OPERATION_H_