blob: 40ecfe6ef4ceafbffa1d1fd3f05590d2537cb957 [file] [log] [blame]
Shawn Willden0d560bf2014-12-15 17:44:02 -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
17#include "hmac_key.h"
18
19#include <openssl/err.h>
20#include <openssl/rand.h>
21
22#include "hmac_operation.h"
23
24namespace keymaster {
25
26Operation* HmacKey::CreateOperation(keymaster_purpose_t purpose, keymaster_error_t* error) {
27 *error = KM_ERROR_OK;
28
29 uint32_t tag_length;
30 if (!authorizations().GetTagValue(TAG_MAC_LENGTH, &tag_length))
31 *error = KM_ERROR_UNSUPPORTED_MAC_LENGTH;
32
33 keymaster_digest_t digest;
34 if (!authorizations().GetTagValue(TAG_DIGEST, &digest) || digest != KM_DIGEST_SHA_2_256)
35 *error = KM_ERROR_UNSUPPORTED_DIGEST;
36
37 if (*error != KM_ERROR_OK)
38 return NULL;
39
40 UniquePtr<Operation> op;
41 switch (purpose) {
42 case KM_PURPOSE_SIGN:
43 case KM_PURPOSE_VERIFY:
44 op.reset(
45 new HmacOperation(purpose, logger_, key_data(), key_data_size(), digest, tag_length));
46 break;
47 default:
48 *error = KM_ERROR_UNSUPPORTED_PURPOSE;
49 return NULL;
50 }
51
52 if (!op.get())
53 *error = KM_ERROR_MEMORY_ALLOCATION_FAILED;
54
55 return op.release();
56}
57
58} // namespace keymaster