blob: ea8d81d6a5a29ab54d9642a99f750f84f082a16d [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:
Shawn Willdenf90f2352014-12-18 23:01:15 -070076 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 Willden61902362014-12-18 10:33:24 -070080
81 const keymaster_digest_t digest_;
82 const EVP_MD* digest_algorithm_;
83 EVP_MD_CTX digest_ctx_;
Shawn Willdenf90f2352014-12-18 23:01:15 -070084 uint8_t digest_buf_[EVP_MAX_MD_SIZE];
Shawn Willden61902362014-12-18 10:33:24 -070085};
86
87/**
88 * RSA private key signing operation.
89 */
90class RsaSignOperation : public RsaDigestingOperation {
Shawn Willden0a4df7e2014-08-28 16:09:05 -060091 public:
Shawn Willden567a4a02014-12-31 12:14:46 -070092 RsaSignOperation(keymaster_digest_t digest, keymaster_padding_t padding, RSA* key)
Shawn Willden61902362014-12-18 10:33:24 -070093 : RsaDigestingOperation(KM_PURPOSE_SIGN, digest, padding, key) {}
Shawn Willden6bfbff02015-02-06 19:48:24 -070094 virtual keymaster_error_t Finish(const AuthorizationSet& additional_params,
95 const Buffer& signature, Buffer* output);
Shawn Willden4200f212014-12-02 07:01:21 -070096
97 private:
Shawn Willdenf90f2352014-12-18 23:01:15 -070098 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 Willden0a4df7e2014-08-28 16:09:05 -0600103};
104
Shawn Willden61902362014-12-18 10:33:24 -0700105/**
106 * RSA public key verification operation.
107 */
108class RsaVerifyOperation : public RsaDigestingOperation {
Shawn Willden0a4df7e2014-08-28 16:09:05 -0600109 public:
Shawn Willden567a4a02014-12-31 12:14:46 -0700110 RsaVerifyOperation(keymaster_digest_t digest, keymaster_padding_t padding, RSA* key)
Shawn Willden61902362014-12-18 10:33:24 -0700111 : RsaDigestingOperation(KM_PURPOSE_VERIFY, digest, padding, key) {}
Shawn Willden6bfbff02015-02-06 19:48:24 -0700112 virtual keymaster_error_t Finish(const AuthorizationSet& additional_params,
113 const Buffer& signature, Buffer* output);
Shawn Willden4200f212014-12-02 07:01:21 -0700114
115 private:
Shawn Willdenf90f2352014-12-18 23:01:15 -0700116 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 Willden4200f212014-12-02 07:01:21 -0700119};
120
Shawn Willden61902362014-12-18 10:33:24 -0700121/**
122 * RSA public key encryption operation.
123 */
Shawn Willden4200f212014-12-02 07:01:21 -0700124class RsaEncryptOperation : public RsaOperation {
125 public:
Shawn Willden567a4a02014-12-31 12:14:46 -0700126 RsaEncryptOperation(keymaster_padding_t padding, RSA* key)
127 : RsaOperation(KM_PURPOSE_ENCRYPT, padding, key) {}
Shawn Willden6bfbff02015-02-06 19:48:24 -0700128 virtual keymaster_error_t Finish(const AuthorizationSet& additional_params,
129 const Buffer& signature, Buffer* output);
Shawn Willden4200f212014-12-02 07:01:21 -0700130};
131
Shawn Willden61902362014-12-18 10:33:24 -0700132/**
133 * RSA private key decryption operation.
134 */
Shawn Willden4200f212014-12-02 07:01:21 -0700135class RsaDecryptOperation : public RsaOperation {
136 public:
Shawn Willden567a4a02014-12-31 12:14:46 -0700137 RsaDecryptOperation(keymaster_padding_t padding, RSA* key)
138 : RsaOperation(KM_PURPOSE_DECRYPT, padding, key) {}
Shawn Willden6bfbff02015-02-06 19:48:24 -0700139 virtual keymaster_error_t Finish(const AuthorizationSet& additional_params,
140 const Buffer& signature, Buffer* output);
Shawn Willden0a4df7e2014-08-28 16:09:05 -0600141};
142
143} // namespace keymaster
144
145#endif // SYSTEM_KEYMASTER_RSA_OPERATION_H_