• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
17 #include <sys/types.h>
18 #include <unistd.h>
19 
20 /**
21  * When a key is being migrated from a software keymaster implementation
22  * to a hardware keymaster implementation, the first 4 bytes of the key_blob
23  * given to the hardware implementation will be equal to SOFT_KEY_MAGIC.
24  * The hardware implementation should import these PKCS#8 format keys which
25  * are encoded like this:
26  *
27  * 4-byte SOFT_KEY_MAGIC
28  *
29  * 4-byte 32-bit integer big endian for public_key_length. This may be zero
30  *     length which indicates the public key should be derived from the
31  *     private key.
32  *
33  * public_key_length bytes of public key (may be empty)
34  *
35  * 4-byte 32-bit integer big endian for private_key_length
36  *
37  * private_key_length bytes of private key
38  */
39 static const uint8_t SOFT_KEY_MAGIC[] = { 'P', 'K', '#', '8' };
40 
get_softkey_header_size()41 size_t get_softkey_header_size() {
42     return sizeof(SOFT_KEY_MAGIC);
43 }
44 
add_softkey_header(uint8_t * key_blob,size_t key_blob_length)45 uint8_t* add_softkey_header(uint8_t* key_blob, size_t key_blob_length) {
46     if (key_blob_length < sizeof(SOFT_KEY_MAGIC)) {
47         return NULL;
48     }
49 
50     memcpy(key_blob, SOFT_KEY_MAGIC, sizeof(SOFT_KEY_MAGIC));
51 
52     return key_blob + sizeof(SOFT_KEY_MAGIC);
53 }
54 
is_softkey(const uint8_t * key_blob,const size_t key_blob_length)55 bool is_softkey(const uint8_t* key_blob, const size_t key_blob_length) {
56     if (key_blob_length < sizeof(SOFT_KEY_MAGIC)) {
57         return false;
58     }
59 
60     return !memcmp(key_blob, SOFT_KEY_MAGIC, sizeof(SOFT_KEY_MAGIC));
61 }
62