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 30 * 31 * public_key_length bytes of public key 32 * 33 * 4-byte 32-bit integer big endian for private_key_length 34 * 35 * private_key_length bytes of private key 36 */ 37 static const uint8_t SOFT_KEY_MAGIC[] = { 'P', 'K', '#', '8' }; 38 39 size_t get_softkey_header_size() { 40 return sizeof(SOFT_KEY_MAGIC); 41 } 42 43 uint8_t* add_softkey_header(uint8_t* key_blob, size_t key_blob_length) { 44 if (key_blob_length < sizeof(SOFT_KEY_MAGIC)) { 45 return NULL; 46 } 47 48 memcpy(key_blob, SOFT_KEY_MAGIC, sizeof(SOFT_KEY_MAGIC)); 49 50 return key_blob + sizeof(SOFT_KEY_MAGIC); 51 } 52 53 bool is_softkey(const uint8_t* key_blob, const size_t key_blob_length) { 54 if (key_blob_length < sizeof(SOFT_KEY_MAGIC)) { 55 return false; 56 } 57 58 return !memcmp(key_blob, SOFT_KEY_MAGIC, sizeof(SOFT_KEY_MAGIC)); 59 } 60