Shawn Willden | 907c301 | 2014-12-08 15:51:55 -0700 | [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 | |
Shawn Willden | 6dde87c | 2014-12-11 14:08:48 -0700 | [diff] [blame^] | 17 | #include <stdio.h> |
| 18 | |
Shawn Willden | 907c301 | 2014-12-08 15:51:55 -0700 | [diff] [blame] | 19 | #include <openssl/aes.h> |
| 20 | #include <openssl/rand.h> |
| 21 | |
| 22 | #include "aes_operation.h" |
| 23 | |
| 24 | namespace keymaster { |
| 25 | |
Shawn Willden | 6dde87c | 2014-12-11 14:08:48 -0700 | [diff] [blame^] | 26 | keymaster_error_t AesOcbOperation::Initialize(uint8_t* key, size_t key_size, size_t nonce_length, |
| 27 | size_t tag_length) { |
| 28 | if (tag_length > MAX_TAG_LENGTH ||nonce_length > MAX_NONCE_LENGTH) |
| 29 | return KM_ERROR_INVALID_KEY_BLOB; |
Shawn Willden | 907c301 | 2014-12-08 15:51:55 -0700 | [diff] [blame] | 30 | |
Shawn Willden | 6dde87c | 2014-12-11 14:08:48 -0700 | [diff] [blame^] | 31 | if (ae_init(ctx(), key, key_size, nonce_length, tag_length) != AE_SUCCESS) { |
| 32 | memset_s(ctx(), 0, ae_ctx_sizeof()); |
Shawn Willden | 907c301 | 2014-12-08 15:51:55 -0700 | [diff] [blame] | 33 | return KM_ERROR_UNKNOWN_ERROR; |
| 34 | } |
Shawn Willden | 907c301 | 2014-12-08 15:51:55 -0700 | [diff] [blame] | 35 | return KM_ERROR_OK; |
| 36 | } |
| 37 | |
Shawn Willden | 6dde87c | 2014-12-11 14:08:48 -0700 | [diff] [blame^] | 38 | keymaster_error_t AesOcbOperation::EncryptChunk(const uint8_t* nonce, size_t /* nonce_length */, |
| 39 | size_t tag_length, |
| 40 | const keymaster_blob_t additional_data, |
| 41 | uint8_t* chunk, size_t chunk_size, Buffer* output) { |
| 42 | if (!ctx()) |
| 43 | return KM_ERROR_UNKNOWN_ERROR; |
| 44 | uint8_t __attribute__((aligned(16))) tag[MAX_TAG_LENGTH]; |
Shawn Willden | 907c301 | 2014-12-08 15:51:55 -0700 | [diff] [blame] | 45 | |
Shawn Willden | 6dde87c | 2014-12-11 14:08:48 -0700 | [diff] [blame^] | 46 | // Encrypt chunk in place. |
| 47 | int ae_err = ae_encrypt(ctx(), nonce, chunk, chunk_size, additional_data.data, |
| 48 | additional_data.data_length, chunk, tag, AE_FINALIZE); |
Shawn Willden | 907c301 | 2014-12-08 15:51:55 -0700 | [diff] [blame] | 49 | |
Shawn Willden | 907c301 | 2014-12-08 15:51:55 -0700 | [diff] [blame] | 50 | if (ae_err < 0) |
| 51 | return KM_ERROR_UNKNOWN_ERROR; |
Shawn Willden | 6dde87c | 2014-12-11 14:08:48 -0700 | [diff] [blame^] | 52 | assert(ae_err == (int)buffered_data_length()); |
Shawn Willden | 907c301 | 2014-12-08 15:51:55 -0700 | [diff] [blame] | 53 | |
Shawn Willden | 6dde87c | 2014-12-11 14:08:48 -0700 | [diff] [blame^] | 54 | output->write(chunk, buffered_data_length()); |
| 55 | output->write(tag, tag_length); |
Shawn Willden | 907c301 | 2014-12-08 15:51:55 -0700 | [diff] [blame] | 56 | |
| 57 | return KM_ERROR_OK; |
| 58 | } |
| 59 | |
Shawn Willden | 6dde87c | 2014-12-11 14:08:48 -0700 | [diff] [blame^] | 60 | keymaster_error_t AesOcbOperation::DecryptChunk(const uint8_t* nonce, size_t /* nonce_length */, |
| 61 | const uint8_t* tag, size_t /* tag_length */, |
| 62 | const keymaster_blob_t additional_data, |
| 63 | uint8_t* chunk, size_t chunk_size, Buffer* output) { |
| 64 | if (!ctx()) |
| 65 | return KM_ERROR_UNKNOWN_ERROR; |
| 66 | |
| 67 | // Decrypt chunk in place |
| 68 | int ae_err = ae_decrypt(ctx(), nonce, chunk, chunk_size, additional_data.data, |
| 69 | additional_data.data_length, chunk, tag, AE_FINALIZE); |
| 70 | if (ae_err == AE_INVALID) |
| 71 | return KM_ERROR_VERIFICATION_FAILED; |
| 72 | else if (ae_err < 0) |
| 73 | return KM_ERROR_UNKNOWN_ERROR; |
| 74 | assert(ae_err == (int)buffered_data_length()); |
| 75 | output->write(chunk, chunk_size); |
| 76 | |
| 77 | return KM_ERROR_OK; |
Shawn Willden | 907c301 | 2014-12-08 15:51:55 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | } // namespace keymaster |