Nan Zhou | 7a33704 | 2021-07-26 21:05:21 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2021 Google LLC |
| 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 | */ |
Willy Tu | adb8ffe | 2023-07-17 13:44:22 -0700 | [diff] [blame] | 16 | |
William A. Kennington III | 5acaca2 | 2021-10-28 16:32:33 -0700 | [diff] [blame] | 17 | #include <libcr51sign/libcr51sign_support.h> |
William A. Kennington III | deb5501 | 2021-10-28 17:12:23 -0700 | [diff] [blame] | 18 | #include <openssl/err.h> |
Nan Zhou | 7a33704 | 2021-07-26 21:05:21 -0700 | [diff] [blame] | 19 | #include <openssl/evp.h> |
| 20 | #include <openssl/pem.h> |
| 21 | #include <openssl/rsa.h> |
| 22 | #include <stdio.h> |
Willy Tu | dca92e4 | 2023-11-16 23:22:07 -0800 | [diff] [blame] | 23 | #include <string.h> |
Nan Zhou | 7a33704 | 2021-07-26 21:05:21 -0700 | [diff] [blame] | 24 | |
| 25 | #ifdef __cplusplus |
| 26 | extern "C" |
| 27 | { |
| 28 | #endif |
| 29 | |
William A. Kennington III | deb5501 | 2021-10-28 17:12:23 -0700 | [diff] [blame] | 30 | #ifndef USER_PRINT |
| 31 | #define CPRINTS(ctx, ...) fprintf(stderr, __VA_ARGS__) |
| 32 | #endif |
| 33 | |
Patrick Williams | 6084957 | 2023-10-20 11:19:52 -0500 | [diff] [blame] | 34 | // @func hash_init get ready to compute a hash |
| 35 | // |
| 36 | // @param[in] ctx - context struct |
| 37 | // @param[in] hash_type - type of hash function to use |
| 38 | // |
| 39 | // @return nonzero on error, zero on success |
Nan Zhou | 7a33704 | 2021-07-26 21:05:21 -0700 | [diff] [blame] | 40 | |
Patrick Williams | 6084957 | 2023-10-20 11:19:52 -0500 | [diff] [blame] | 41 | int hash_init(const void* ctx, enum hash_type type) |
| 42 | { |
| 43 | struct libcr51sign_ctx* context = (struct libcr51sign_ctx*)ctx; |
| 44 | struct hash_ctx* hash_context = (struct hash_ctx*)context->priv; |
| 45 | hash_context->hash_type = type; |
| 46 | if (type == HASH_SHA2_256) // SHA256_Init returns 1 |
| 47 | SHA256_Init(&hash_context->sha256_ctx); |
| 48 | else if (type == HASH_SHA2_512) |
| 49 | SHA512_Init(&hash_context->sha512_ctx); |
| 50 | else |
| 51 | return LIBCR51SIGN_ERROR_INVALID_HASH_TYPE; |
Nan Zhou | 7a33704 | 2021-07-26 21:05:21 -0700 | [diff] [blame] | 52 | |
Patrick Williams | 6084957 | 2023-10-20 11:19:52 -0500 | [diff] [blame] | 53 | return LIBCR51SIGN_SUCCESS; |
| 54 | } |
| 55 | |
| 56 | // @func hash_update add data to the hash |
| 57 | // |
| 58 | // @param[in] ctx - context struct |
| 59 | // @param[in] buf - data to add to hash |
| 60 | // @param[in] count - number of bytes of data to add |
| 61 | // |
| 62 | // @return nonzero on error, zero on success |
| 63 | |
| 64 | int hash_update(void* ctx, const uint8_t* data, size_t size) |
| 65 | { |
| 66 | if (size == 0) |
Nan Zhou | 7a33704 | 2021-07-26 21:05:21 -0700 | [diff] [blame] | 67 | return LIBCR51SIGN_SUCCESS; |
Patrick Williams | 6084957 | 2023-10-20 11:19:52 -0500 | [diff] [blame] | 68 | struct libcr51sign_ctx* context = (struct libcr51sign_ctx*)ctx; |
| 69 | struct hash_ctx* hash_context = (struct hash_ctx*)context->priv; |
Nan Zhou | 7a33704 | 2021-07-26 21:05:21 -0700 | [diff] [blame] | 70 | |
Patrick Williams | 6084957 | 2023-10-20 11:19:52 -0500 | [diff] [blame] | 71 | if (hash_context->hash_type == HASH_SHA2_256) // SHA256_Update returns 1 |
| 72 | SHA256_Update(&hash_context->sha256_ctx, data, size); |
| 73 | else if (hash_context->hash_type == HASH_SHA2_512) |
| 74 | SHA512_Update(&hash_context->sha512_ctx, data, size); |
| 75 | else |
| 76 | return LIBCR51SIGN_ERROR_INVALID_HASH_TYPE; |
Nan Zhou | 7a33704 | 2021-07-26 21:05:21 -0700 | [diff] [blame] | 77 | |
Patrick Williams | 6084957 | 2023-10-20 11:19:52 -0500 | [diff] [blame] | 78 | return LIBCR51SIGN_SUCCESS; |
| 79 | } |
Nan Zhou | 7a33704 | 2021-07-26 21:05:21 -0700 | [diff] [blame] | 80 | |
Patrick Williams | 6084957 | 2023-10-20 11:19:52 -0500 | [diff] [blame] | 81 | // @func hash_final finish hash calculation |
| 82 | // |
| 83 | // @param[in] ctx - context struct |
| 84 | // @param[out] hash - buffer to write hash to (guaranteed to be big enough) |
| 85 | // |
| 86 | // @return nonzero on error, zero on success |
Nan Zhou | 7a33704 | 2021-07-26 21:05:21 -0700 | [diff] [blame] | 87 | |
Patrick Williams | 6084957 | 2023-10-20 11:19:52 -0500 | [diff] [blame] | 88 | int hash_final(void* ctx, uint8_t* hash) |
| 89 | { |
| 90 | int rv; |
| 91 | struct libcr51sign_ctx* context = (struct libcr51sign_ctx*)ctx; |
| 92 | struct hash_ctx* hash_context = (struct hash_ctx*)context->priv; |
| 93 | |
| 94 | if (hash_context->hash_type == HASH_SHA2_256) |
| 95 | rv = SHA256_Final(hash, &hash_context->sha256_ctx); |
| 96 | else if (hash_context->hash_type == HASH_SHA2_512) |
| 97 | rv = SHA512_Final(hash, &hash_context->sha512_ctx); |
| 98 | else |
| 99 | return LIBCR51SIGN_ERROR_INVALID_HASH_TYPE; |
| 100 | |
| 101 | if (rv) |
Nan Zhou | 7a33704 | 2021-07-26 21:05:21 -0700 | [diff] [blame] | 102 | return LIBCR51SIGN_SUCCESS; |
Patrick Williams | 6084957 | 2023-10-20 11:19:52 -0500 | [diff] [blame] | 103 | else |
| 104 | return LIBCR51SIGN_ERROR_RUNTIME_FAILURE; |
| 105 | } |
Nan Zhou | 7a33704 | 2021-07-26 21:05:21 -0700 | [diff] [blame] | 106 | |
Patrick Williams | 6084957 | 2023-10-20 11:19:52 -0500 | [diff] [blame] | 107 | // @func verify check that the signature is valid for given hashed data |
| 108 | // |
| 109 | // @param[in] ctx - context struct |
| 110 | // @param[in] scheme - type of signature, hash, etc. |
| 111 | // @param[in] sig - signature blob |
| 112 | // @param[in] sig_len - length of signature in bytes |
| 113 | // @param[in] data - pre-hashed data to verify |
| 114 | // @param[in] data_len - length of hashed data in bytes |
| 115 | // |
| 116 | // verify_signature expects RSA public key file path in ctx->key_ring |
| 117 | // @return nonzero on error, zero on success |
Nan Zhou | 7a33704 | 2021-07-26 21:05:21 -0700 | [diff] [blame] | 118 | |
Patrick Williams | 6084957 | 2023-10-20 11:19:52 -0500 | [diff] [blame] | 119 | int verify_signature(const void* ctx, enum signature_scheme sig_scheme, |
| 120 | const uint8_t* sig, size_t sig_len, const uint8_t* data, |
| 121 | size_t data_len) |
| 122 | { |
| 123 | // By default returns error. |
| 124 | int rv = LIBCR51SIGN_ERROR_INVALID_ARGUMENT; |
| 125 | |
| 126 | CPRINTS(ctx, "sig_len %zu sig: ", sig_len); |
| 127 | for (size_t i = 0; i < sig_len; i++) |
Nan Zhou | 7a33704 | 2021-07-26 21:05:21 -0700 | [diff] [blame] | 128 | { |
Patrick Williams | 6084957 | 2023-10-20 11:19:52 -0500 | [diff] [blame] | 129 | CPRINTS(ctx, "%x", sig[i]); |
Nan Zhou | 7a33704 | 2021-07-26 21:05:21 -0700 | [diff] [blame] | 130 | } |
Patrick Williams | 6084957 | 2023-10-20 11:19:52 -0500 | [diff] [blame] | 131 | CPRINTS(ctx, "\n"); |
Nan Zhou | 7a33704 | 2021-07-26 21:05:21 -0700 | [diff] [blame] | 132 | |
Patrick Williams | 6084957 | 2023-10-20 11:19:52 -0500 | [diff] [blame] | 133 | struct libcr51sign_ctx* lctx = (struct libcr51sign_ctx*)ctx; |
| 134 | FILE* fp = fopen(lctx->keyring, "r"); |
| 135 | RSA *rsa = NULL, *pub_rsa = NULL; |
| 136 | EVP_PKEY* pkey = NULL; |
| 137 | BIO* bio = BIO_new(BIO_s_mem()); |
| 138 | if (!fp) |
Nan Zhou | 7a33704 | 2021-07-26 21:05:21 -0700 | [diff] [blame] | 139 | { |
Patrick Williams | 6084957 | 2023-10-20 11:19:52 -0500 | [diff] [blame] | 140 | CPRINTS(ctx, "fopen failed\n"); |
| 141 | goto clean_up; |
Nan Zhou | 7a33704 | 2021-07-26 21:05:21 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Patrick Williams | 6084957 | 2023-10-20 11:19:52 -0500 | [diff] [blame] | 144 | pkey = PEM_read_PUBKEY(fp, 0, 0, 0); |
| 145 | if (!pkey) |
| 146 | { |
| 147 | CPRINTS(ctx, "Read public key failed\n"); |
| 148 | goto clean_up; |
| 149 | } |
| 150 | |
| 151 | rsa = EVP_PKEY_get1_RSA(pkey); |
| 152 | if (!rsa) |
| 153 | { |
| 154 | goto clean_up; |
| 155 | } |
| 156 | pub_rsa = RSAPublicKey_dup(rsa); |
| 157 | if (!RSA_print(bio, pub_rsa, 2)) |
| 158 | { |
| 159 | CPRINTS(ctx, "RSA print failed\n"); |
| 160 | } |
| 161 | if (!pub_rsa) |
| 162 | { |
| 163 | CPRINTS(ctx, "no pub RSA\n"); |
| 164 | goto clean_up; |
| 165 | } |
| 166 | CPRINTS(ctx, "public RSA\n"); |
Willy Tu | dca92e4 | 2023-11-16 23:22:07 -0800 | [diff] [blame] | 167 | char buffer[1024] = {}; |
Patrick Williams | 6084957 | 2023-10-20 11:19:52 -0500 | [diff] [blame] | 168 | while (BIO_read(bio, buffer, sizeof(buffer) - 1) > 0) |
| 169 | { |
| 170 | CPRINTS(ctx, " %s", buffer); |
| 171 | } |
| 172 | enum hash_type hash_type; |
| 173 | rv = get_hash_type_from_signature(sig_scheme, &hash_type); |
| 174 | if (rv != LIBCR51SIGN_SUCCESS) |
| 175 | { |
| 176 | CPRINTS(ctx, "Invalid hash_type!\n"); |
| 177 | goto clean_up; |
| 178 | } |
| 179 | int hash_nid = -1; |
| 180 | if (hash_type == HASH_SHA2_256) |
| 181 | { |
| 182 | hash_nid = NID_sha256; |
| 183 | } |
| 184 | else if (hash_type == HASH_SHA2_512) |
| 185 | { |
| 186 | hash_nid = NID_sha512; |
| 187 | } |
| 188 | else |
| 189 | { |
| 190 | rv = LIBCR51SIGN_ERROR_INVALID_HASH_TYPE; |
| 191 | goto clean_up; |
| 192 | } |
| 193 | |
| 194 | int ret = RSA_verify(hash_nid, data, data_len, sig, sig_len, pub_rsa); |
| 195 | // OpenSSL RSA_verify returns 1 on success and 0 on failure |
| 196 | if (!ret) |
| 197 | { |
| 198 | CPRINTS(ctx, "OPENSSL_ERROR: %s\n", |
| 199 | ERR_error_string(ERR_get_error(), NULL)); |
| 200 | rv = LIBCR51SIGN_ERROR_RUNTIME_FAILURE; |
| 201 | goto clean_up; |
| 202 | } |
| 203 | rv = LIBCR51SIGN_SUCCESS; |
| 204 | CPRINTS(ctx, "sig: "); |
| 205 | for (size_t i = 0; i < sig_len; i++) |
| 206 | { |
| 207 | CPRINTS(ctx, "%x", sig[i]); |
| 208 | } |
| 209 | CPRINTS(ctx, "\n"); |
| 210 | |
| 211 | CPRINTS(ctx, "data: "); |
| 212 | for (size_t i = 0; i < data_len; i++) |
| 213 | { |
| 214 | CPRINTS(ctx, "%x", data[i]); |
| 215 | } |
| 216 | CPRINTS(ctx, "\n"); |
| 217 | |
| 218 | const unsigned rsa_size = RSA_size(pub_rsa); |
| 219 | CPRINTS(ctx, "rsa size %d sig_len %d\n", rsa_size, (uint32_t)sig_len); |
| 220 | |
| 221 | clean_up: |
| 222 | if (fp) |
| 223 | { |
| 224 | fclose(fp); |
| 225 | } |
| 226 | EVP_PKEY_free(pkey); |
| 227 | RSA_free(rsa); |
| 228 | RSA_free(pub_rsa); |
| 229 | BIO_free(bio); |
| 230 | return rv; |
| 231 | } |
| 232 | |
Nan Zhou | 7a33704 | 2021-07-26 21:05:21 -0700 | [diff] [blame] | 233 | #ifdef __cplusplus |
| 234 | } // extern "C" |
| 235 | #endif |