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_AES_OPERATION_H_ 18 #define SYSTEM_KEYMASTER_AES_OPERATION_H_ 19 20 #include <openssl/evp.h> 21 22 #include "ocb_utils.h" 23 #include "operation.h" 24 25 namespace keymaster { 26 27 /** 28 * Abstract base for AES operation factories. This class does all of the work to create 29 * AES operations. 30 */ 31 class AesOperationFactory : public OperationFactory { 32 public: registry_key()33 KeyType registry_key() const override { return KeyType(KM_ALGORITHM_AES, purpose()); } 34 35 Operation* CreateOperation(const Key& key, const AuthorizationSet& begin_params, 36 keymaster_error_t* error) override; 37 const keymaster_block_mode_t* SupportedBlockModes(size_t* block_mode_count) const override; 38 const keymaster_padding_t* SupportedPaddingModes(size_t* padding_count) const override; 39 40 virtual keymaster_purpose_t purpose() const = 0; 41 }; 42 43 /** 44 * Concrete factory for AES encryption operations. 45 */ 46 class AesEncryptionOperationFactory : public AesOperationFactory { purpose()47 keymaster_purpose_t purpose() const override { return KM_PURPOSE_ENCRYPT; } 48 }; 49 50 /** 51 * Concrete factory for AES decryption operations. 52 */ 53 class AesDecryptionOperationFactory : public AesOperationFactory { purpose()54 keymaster_purpose_t purpose() const override { return KM_PURPOSE_DECRYPT; } 55 }; 56 57 static const size_t MAX_EVP_KEY_SIZE = 32; 58 59 class AesEvpOperation : public Operation { 60 public: 61 AesEvpOperation(keymaster_purpose_t purpose, keymaster_block_mode_t block_mode, 62 keymaster_padding_t padding, bool caller_iv, size_t tag_length, 63 const uint8_t* key, size_t key_size); 64 ~AesEvpOperation(); 65 66 keymaster_error_t Begin(const AuthorizationSet& input_params, 67 AuthorizationSet* output_params) override; 68 keymaster_error_t Update(const AuthorizationSet& additional_params, const Buffer& input, 69 AuthorizationSet* output_params, Buffer* output, 70 size_t* input_consumed) override; 71 keymaster_error_t Finish(const AuthorizationSet& additional_params, const Buffer& signature, 72 AuthorizationSet* output_params, Buffer* output) override; 73 keymaster_error_t Abort() override; 74 75 virtual int evp_encrypt_mode() = 0; 76 77 protected: 78 bool need_iv() const; 79 keymaster_error_t InitializeCipher(); 80 keymaster_error_t GetIv(const AuthorizationSet& input_params); 81 bool HandleAad(const AuthorizationSet& input_params, const Buffer& input, 82 keymaster_error_t* error); 83 bool ProcessAadBlocks(const uint8_t* data, size_t blocks, keymaster_error_t* error); 84 void FillBufferedAadBlock(keymaster_blob_t* aad); 85 bool ProcessBufferedAadBlock(keymaster_error_t* error); 86 bool InternalUpdate(const uint8_t* input, size_t input_length, Buffer* output, 87 keymaster_error_t* error); 88 89 const keymaster_block_mode_t block_mode_; 90 EVP_CIPHER_CTX ctx_; 91 UniquePtr<uint8_t[]> iv_; 92 size_t iv_length_; 93 const bool caller_iv_; 94 size_t tag_length_; 95 UniquePtr<uint8_t[]> aad_block_buf_; 96 size_t aad_block_buf_length_; 97 98 private: 99 bool data_started_; 100 const size_t key_size_; 101 const keymaster_padding_t padding_; 102 uint8_t key_[MAX_EVP_KEY_SIZE]; 103 }; 104 105 class AesEvpEncryptOperation : public AesEvpOperation { 106 public: AesEvpEncryptOperation(keymaster_block_mode_t block_mode,keymaster_padding_t padding,bool caller_iv,size_t tag_length,const uint8_t * key,size_t key_size)107 AesEvpEncryptOperation(keymaster_block_mode_t block_mode, keymaster_padding_t padding, 108 bool caller_iv, size_t tag_length, const uint8_t* key, size_t key_size) 109 : AesEvpOperation(KM_PURPOSE_ENCRYPT, block_mode, padding, caller_iv, tag_length, key, 110 key_size) {} 111 112 keymaster_error_t Begin(const AuthorizationSet& input_params, 113 AuthorizationSet* output_params) override; 114 keymaster_error_t Finish(const AuthorizationSet& additional_params, const Buffer& signature, 115 AuthorizationSet* output_params, Buffer* output) override; 116 evp_encrypt_mode()117 int evp_encrypt_mode() override { return 1; } 118 119 private: 120 keymaster_error_t GenerateIv(); 121 }; 122 123 class AesEvpDecryptOperation : public AesEvpOperation { 124 public: AesEvpDecryptOperation(keymaster_block_mode_t block_mode,keymaster_padding_t padding,size_t tag_length,const uint8_t * key,size_t key_size)125 AesEvpDecryptOperation(keymaster_block_mode_t block_mode, keymaster_padding_t padding, 126 size_t tag_length, const uint8_t* key, size_t key_size) 127 : AesEvpOperation(KM_PURPOSE_DECRYPT, block_mode, padding, 128 false /* caller_iv -- don't care */, tag_length, key, key_size) {} 129 130 keymaster_error_t Begin(const AuthorizationSet& input_params, 131 AuthorizationSet* output_params) override; 132 keymaster_error_t Update(const AuthorizationSet& additional_params, const Buffer& input, 133 AuthorizationSet* output_params, Buffer* output, 134 size_t* input_consumed) override; 135 keymaster_error_t Finish(const AuthorizationSet& additional_params, const Buffer& signature, 136 AuthorizationSet* output_params, Buffer* output) override; 137 evp_encrypt_mode()138 int evp_encrypt_mode() override { return 0; } 139 140 private: tag_buf_unused()141 size_t tag_buf_unused() { return tag_length_ - tag_buf_length_; } 142 143 keymaster_error_t ProcessAllButTagLengthBytes(const Buffer& input, Buffer* output); 144 bool ProcessTagBufContentsAsData(size_t to_process, Buffer* output, keymaster_error_t* error); 145 void BufferCandidateTagData(const uint8_t* data, size_t data_length); 146 147 UniquePtr<uint8_t[]> tag_buf_; 148 size_t tag_buf_length_; 149 }; 150 151 } // namespace keymaster 152 153 #endif // SYSTEM_KEYMASTER_AES_OPERATION_H_ 154