Joel Galenson | ca0efb1 | 2020-10-01 14:32:30 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 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 | #ifndef __CRYPTO_H__ |
| 18 | #define __CRYPTO_H__ |
| 19 | |
| 20 | #include <stdbool.h> |
| 21 | #include <stdint.h> |
| 22 | #include <stddef.h> |
| 23 | |
| 24 | extern "C" { |
David Drysdale | c97eb9e | 2022-01-26 13:03:48 -0800 | [diff] [blame^] | 25 | bool hmacSha256(const uint8_t* key, size_t key_size, const uint8_t* msg, size_t msg_size, |
| 26 | uint8_t* out, size_t out_size); |
Janis Danisevskis | 9d90b81 | 2020-11-25 21:02:11 -0800 | [diff] [blame] | 27 | bool randomBytes(uint8_t* out, size_t len); |
Joel Galenson | ca0efb1 | 2020-10-01 14:32:30 -0700 | [diff] [blame] | 28 | bool AES_gcm_encrypt(const uint8_t* in, uint8_t* out, size_t len, |
| 29 | const uint8_t* key, size_t key_size, const uint8_t* iv, uint8_t* tag); |
| 30 | bool AES_gcm_decrypt(const uint8_t* in, uint8_t* out, size_t len, |
| 31 | const uint8_t* key, size_t key_size, const uint8_t* iv, |
| 32 | const uint8_t* tag); |
| 33 | |
| 34 | // Copied from system/security/keystore/keymaster_enforcement.h. |
| 35 | typedef uint64_t km_id_t; |
| 36 | |
| 37 | bool CreateKeyId(const uint8_t* key_blob, size_t len, km_id_t* out_id); |
| 38 | |
| 39 | void generateKeyFromPassword(uint8_t* key, size_t key_len, const char* pw, |
Janis Danisevskis | 9d90b81 | 2020-11-25 21:02:11 -0800 | [diff] [blame] | 40 | size_t pw_len, const uint8_t* salt); |
Joel Galenson | 0591458 | 2021-01-08 09:30:41 -0800 | [diff] [blame] | 41 | |
| 42 | #include "openssl/digest.h" |
| 43 | #include "openssl/ec_key.h" |
| 44 | |
| 45 | bool HKDFExtract(uint8_t *out_key, size_t *out_len, |
| 46 | const uint8_t *secret, size_t secret_len, |
| 47 | const uint8_t *salt, size_t salt_len); |
| 48 | |
| 49 | bool HKDFExpand(uint8_t *out_key, size_t out_len, |
| 50 | const uint8_t *prk, size_t prk_len, |
| 51 | const uint8_t *info, size_t info_len); |
| 52 | |
| 53 | // We define this as field_elem_size. |
| 54 | static const size_t EC_MAX_BYTES = 32; |
| 55 | |
| 56 | int ECDHComputeKey(void *out, const EC_POINT *pub_key, const EC_KEY *priv_key); |
| 57 | |
| 58 | EC_KEY* ECKEYGenerateKey(); |
| 59 | |
Paul Crowley | 7bb5edd | 2021-03-20 20:26:43 -0700 | [diff] [blame] | 60 | size_t ECKEYMarshalPrivateKey(const EC_KEY *priv_key, uint8_t *buf, size_t len); |
| 61 | |
| 62 | EC_KEY* ECKEYParsePrivateKey(const uint8_t *buf, size_t len); |
Joel Galenson | 0591458 | 2021-01-08 09:30:41 -0800 | [diff] [blame] | 63 | |
| 64 | size_t ECPOINTPoint2Oct(const EC_POINT *point, uint8_t *buf, size_t len); |
| 65 | |
| 66 | EC_POINT* ECPOINTOct2Point(const uint8_t *buf, size_t len); |
Shawn Willden | 8fde4c2 | 2021-02-14 13:58:22 -0700 | [diff] [blame] | 67 | |
Joel Galenson | ca0efb1 | 2020-10-01 14:32:30 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Shawn Willden | 8fde4c2 | 2021-02-14 13:58:22 -0700 | [diff] [blame] | 70 | // Parse a DER-encoded X.509 certificate contained in cert_buf, with length |
| 71 | // cert_len, extract the subject, DER-encode it and write the result to |
| 72 | // subject_buf, which has subject_buf_len capacity. |
| 73 | // |
Shawn Willden | 3412087 | 2021-02-24 21:56:30 -0700 | [diff] [blame] | 74 | // Because the length of the subject is unknown, and because we'd like to (a) be |
Shawn Willden | 8fde4c2 | 2021-02-14 13:58:22 -0700 | [diff] [blame] | 75 | // able to handle subjects of any size and (b) avoid parsing the certificate |
| 76 | // twice most of the time, once to discover the length and once to parse it, the |
| 77 | // return value is overloaded. |
| 78 | // |
| 79 | // If the return value > 0 it specifies the number of bytes written into |
| 80 | // subject_buf; the operation was successful. |
| 81 | // |
| 82 | // If the return value == 0, certificate parsing failed unrecoverably. The |
| 83 | // reason will be logged. |
| 84 | // |
| 85 | // If the return value < 0, the operation failed because the subject size > |
| 86 | // subject_buf_len. The return value is -(subject_size), where subject_size is |
| 87 | // the size of the extracted DER-encoded subject field. Call |
| 88 | // extractSubjectFromCertificate again with a sufficiently-large buffer. |
| 89 | int extractSubjectFromCertificate(const uint8_t* cert_buf, size_t cert_len, |
| 90 | uint8_t* subject_buf, size_t subject_buf_len); |
| 91 | |
Joel Galenson | ca0efb1 | 2020-10-01 14:32:30 -0700 | [diff] [blame] | 92 | #endif // __CRYPTO_H__ |