blob: 0c7a86c9daf0be70a180448ceaf6a1485e018d89 [file] [log] [blame]
Shawn Willden907c3012014-12-08 15:51:55 -07001/*
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 Willden6dde87c2014-12-11 14:08:48 -070017#include <stdio.h>
18
Shawn Willden907c3012014-12-08 15:51:55 -070019#include <openssl/aes.h>
20#include <openssl/rand.h>
21
22#include "aes_operation.h"
23
24namespace keymaster {
25
Shawn Willden6dde87c2014-12-11 14:08:48 -070026keymaster_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 Willden907c3012014-12-08 15:51:55 -070030
Shawn Willden6dde87c2014-12-11 14:08:48 -070031 if (ae_init(ctx(), key, key_size, nonce_length, tag_length) != AE_SUCCESS) {
32 memset_s(ctx(), 0, ae_ctx_sizeof());
Shawn Willden907c3012014-12-08 15:51:55 -070033 return KM_ERROR_UNKNOWN_ERROR;
34 }
Shawn Willden907c3012014-12-08 15:51:55 -070035 return KM_ERROR_OK;
36}
37
Shawn Willden6dde87c2014-12-11 14:08:48 -070038keymaster_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 Willden907c3012014-12-08 15:51:55 -070045
Shawn Willden6dde87c2014-12-11 14:08:48 -070046 // 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 Willden907c3012014-12-08 15:51:55 -070049
Shawn Willden907c3012014-12-08 15:51:55 -070050 if (ae_err < 0)
51 return KM_ERROR_UNKNOWN_ERROR;
Shawn Willden6dde87c2014-12-11 14:08:48 -070052 assert(ae_err == (int)buffered_data_length());
Shawn Willden907c3012014-12-08 15:51:55 -070053
Shawn Willden6dde87c2014-12-11 14:08:48 -070054 output->write(chunk, buffered_data_length());
55 output->write(tag, tag_length);
Shawn Willden907c3012014-12-08 15:51:55 -070056
57 return KM_ERROR_OK;
58}
59
Shawn Willden6dde87c2014-12-11 14:08:48 -070060keymaster_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 Willden907c3012014-12-08 15:51:55 -070078}
79
80} // namespace keymaster