blob: 6c2fac90480a9a96ebc1792399547b44736f90b8 [file] [log] [blame]
Kenny Root822c3a92012-03-23 16:34:39 -07001/*
2 * Copyright (C) 2012 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 Willden80843db2015-02-24 09:31:25 -070017#include <stdint.h>
Elliott Hughes13574072015-01-28 11:48:09 -080018#include <string.h>
Kenny Root822c3a92012-03-23 16:34:39 -070019#include <sys/types.h>
20#include <unistd.h>
21
Shawn Willden80843db2015-02-24 09:31:25 -070022#include <keystore/keystore.h>
23
Kenny Root822c3a92012-03-23 16:34:39 -070024/**
25 * When a key is being migrated from a software keymaster implementation
26 * to a hardware keymaster implementation, the first 4 bytes of the key_blob
27 * given to the hardware implementation will be equal to SOFT_KEY_MAGIC.
28 * The hardware implementation should import these PKCS#8 format keys which
29 * are encoded like this:
30 *
31 * 4-byte SOFT_KEY_MAGIC
32 *
Kenny Root17208e02013-09-04 13:56:03 -070033 * 4-byte 32-bit integer big endian for public_key_length. This may be zero
34 * length which indicates the public key should be derived from the
35 * private key.
Kenny Root822c3a92012-03-23 16:34:39 -070036 *
Kenny Root17208e02013-09-04 13:56:03 -070037 * public_key_length bytes of public key (may be empty)
Kenny Root822c3a92012-03-23 16:34:39 -070038 *
39 * 4-byte 32-bit integer big endian for private_key_length
40 *
41 * private_key_length bytes of private key
42 */
43static const uint8_t SOFT_KEY_MAGIC[] = { 'P', 'K', '#', '8' };
44
45size_t get_softkey_header_size() {
46 return sizeof(SOFT_KEY_MAGIC);
47}
48
49uint8_t* add_softkey_header(uint8_t* key_blob, size_t key_blob_length) {
50 if (key_blob_length < sizeof(SOFT_KEY_MAGIC)) {
Yi Konge353f252018-07-30 01:38:39 -070051 return nullptr;
Kenny Root822c3a92012-03-23 16:34:39 -070052 }
53
54 memcpy(key_blob, SOFT_KEY_MAGIC, sizeof(SOFT_KEY_MAGIC));
55
56 return key_blob + sizeof(SOFT_KEY_MAGIC);
57}
58
59bool is_softkey(const uint8_t* key_blob, const size_t key_blob_length) {
60 if (key_blob_length < sizeof(SOFT_KEY_MAGIC)) {
61 return false;
62 }
63
64 return !memcmp(key_blob, SOFT_KEY_MAGIC, sizeof(SOFT_KEY_MAGIC));
65}