#include <openssl/evp.h>
#include <openssl/sha.h>
+#include <pakfire/ctx.h>
#include <pakfire/digest.h>
#include <pakfire/logging.h>
#include <pakfire/pakfire.h>
memset(digests->sha2_256, 0, sizeof(digests->sha2_256));
}
-static int pakfire_digests_check_length(struct pakfire* pakfire,
+static int pakfire_digests_check_length(struct pakfire_ctx* ctx,
const enum pakfire_digest_types type, const size_t length) {
const size_t l = pakfire_digest_length(type);
return 0;
// Otherwise set an error
- ERROR(pakfire, "Digest is of an unexpected length\n");
+ CTX_ERROR(ctx, "Digest is of an unexpected length\n");
+
return -ENOMSG;
}
-static EVP_MD_CTX* __pakfire_digest_setup(struct pakfire* pakfire, const EVP_MD* md) {
- EVP_MD_CTX* ctx = NULL;
+static EVP_MD_CTX* __pakfire_digest_setup(struct pakfire_ctx* ctx, const EVP_MD* md) {
+ EVP_MD_CTX* evp_ctx = NULL;
int r;
// Setup a new context
- ctx = EVP_MD_CTX_new();
- if (!ctx) {
- ERROR(pakfire, "Could not initialize OpenSSL context: %s\n",
+ evp_ctx = EVP_MD_CTX_new();
+ if (!evp_ctx) {
+ CTX_ERROR(ctx, "Could not initialize OpenSSL context: %s\n",
ERR_error_string(ERR_get_error(), NULL));
r = 1;
goto ERROR;
}
// Setup digest
- r = EVP_DigestInit_ex(ctx, md, NULL);
+ r = EVP_DigestInit_ex(evp_ctx, md, NULL);
if (r != 1) {
- ERROR(pakfire, "Could not setup digest: %s\n",
+ CTX_ERROR(ctx, "Could not setup digest: %s\n",
ERR_error_string(ERR_get_error(), NULL));
r = 1;
goto ERROR;
}
- return ctx;
+ return evp_ctx;
ERROR:
- if (ctx)
- EVP_MD_CTX_free(ctx);
+ if (evp_ctx)
+ EVP_MD_CTX_free(evp_ctx);
return NULL;
}
-static int __pakfire_digest_update(struct pakfire* pakfire, EVP_MD_CTX* ctx,
+static int __pakfire_digest_update(struct pakfire_ctx* ctx, EVP_MD_CTX* evp_ctx,
const char* buffer, const size_t length) {
int r;
// Nothing to do if digest not initialized
- if (!ctx)
+ if (!evp_ctx)
return 0;
// Update digest
- r = EVP_DigestUpdate(ctx, buffer, length);
+ r = EVP_DigestUpdate(evp_ctx, buffer, length);
if (r != 1) {
- ERROR(pakfire, "EVP_Digest_Update() failed: %s\n",
+ CTX_ERROR(ctx, "EVP_Digest_Update() failed: %s\n",
ERR_error_string(ERR_get_error(), NULL));
return 1;
}
return 0;
}
-static int __pakfire_digest_finalize(struct pakfire* pakfire,
- EVP_MD_CTX* ctx, unsigned char* digest) {
+static int __pakfire_digest_finalize(struct pakfire_ctx* ctx,
+ EVP_MD_CTX* evp_ctx, unsigned char* digest) {
int r;
// Nothing to do if digest not initialized
- if (!ctx)
+ if (!evp_ctx)
return 0;
// Finalize digest
- r = EVP_DigestFinal_ex(ctx, digest, NULL);
+ r = EVP_DigestFinal_ex(evp_ctx, digest, NULL);
if (r != 1) {
- ERROR(pakfire, "EVP_DigestFinal_ex() failed: %s\n",
+ CTX_ERROR(ctx, "EVP_DigestFinal_ex() failed: %s\n",
ERR_error_string(ERR_get_error(), NULL));
return 1;
}
return 0;
}
-int pakfire_digests_compute_from_file(struct pakfire* pakfire,
+int pakfire_digests_compute_from_file(struct pakfire_ctx* ctx,
struct pakfire_digests* digests, int types, FILE* f) {
EVP_MD_CTX* sha3_512_ctx = NULL;
EVP_MD_CTX* sha3_256_ctx = NULL;
// Initialize context for SHA-3-512
if (types & PAKFIRE_DIGEST_SHA3_512) {
- sha3_512_ctx = __pakfire_digest_setup(pakfire, EVP_sha3_512());
+ sha3_512_ctx = __pakfire_digest_setup(ctx, EVP_sha3_512());
if (!sha3_512_ctx)
goto ERROR;
}
// Initialize context for SHA-3-256
if (types & PAKFIRE_DIGEST_SHA3_256) {
- sha3_256_ctx = __pakfire_digest_setup(pakfire, EVP_sha3_256());
+ sha3_256_ctx = __pakfire_digest_setup(ctx, EVP_sha3_256());
if (!sha3_256_ctx)
goto ERROR;
}
// Initialize context for BLAKE2B512
if (types & PAKFIRE_DIGEST_BLAKE2B512) {
- blake2b512_ctx = __pakfire_digest_setup(pakfire, EVP_blake2b512());
+ blake2b512_ctx = __pakfire_digest_setup(ctx, EVP_blake2b512());
if (!blake2b512_ctx)
goto ERROR;
}
// Initialize context for BLAKE2S256
if (types & PAKFIRE_DIGEST_BLAKE2S256) {
- blake2s256_ctx = __pakfire_digest_setup(pakfire, EVP_blake2s256());
+ blake2s256_ctx = __pakfire_digest_setup(ctx, EVP_blake2s256());
if (!blake2s256_ctx)
goto ERROR;
}
// Initialize context for SHA-2-512
if (types & PAKFIRE_DIGEST_SHA2_512) {
- sha2_512_ctx = __pakfire_digest_setup(pakfire, EVP_sha512());
+ sha2_512_ctx = __pakfire_digest_setup(ctx, EVP_sha512());
if (!sha2_512_ctx)
goto ERROR;
}
// Initialize context for SHA-2-256
if (types & PAKFIRE_DIGEST_SHA2_256) {
- sha2_256_ctx = __pakfire_digest_setup(pakfire, EVP_sha256());
+ sha2_256_ctx = __pakfire_digest_setup(ctx, EVP_sha256());
if (!sha2_256_ctx)
goto ERROR;
}
}
// SHA-3-512
- r = __pakfire_digest_update(pakfire, sha3_512_ctx, buffer, bytes_read);
+ r = __pakfire_digest_update(ctx, sha3_512_ctx, buffer, bytes_read);
if (r)
goto ERROR;
// SHA-3-256
- r = __pakfire_digest_update(pakfire, sha3_256_ctx, buffer, bytes_read);
+ r = __pakfire_digest_update(ctx, sha3_256_ctx, buffer, bytes_read);
if (r)
goto ERROR;
// BLAKE2B512
- r = __pakfire_digest_update(pakfire, blake2b512_ctx, buffer, bytes_read);
+ r = __pakfire_digest_update(ctx, blake2b512_ctx, buffer, bytes_read);
if (r)
goto ERROR;
// BLAKE2S256
- r = __pakfire_digest_update(pakfire, blake2s256_ctx, buffer, bytes_read);
+ r = __pakfire_digest_update(ctx, blake2s256_ctx, buffer, bytes_read);
if (r)
goto ERROR;
// SHA-2-512
- r = __pakfire_digest_update(pakfire, sha2_512_ctx, buffer, bytes_read);
+ r = __pakfire_digest_update(ctx, sha2_512_ctx, buffer, bytes_read);
if (r)
goto ERROR;
// SHA-2-256
- r = __pakfire_digest_update(pakfire, sha2_256_ctx, buffer, bytes_read);
+ r = __pakfire_digest_update(ctx, sha2_256_ctx, buffer, bytes_read);
if (r)
goto ERROR;
}
// Finalize SHA-3-512
- r = __pakfire_digest_finalize(pakfire, sha3_512_ctx, digests->sha3_512);
+ r = __pakfire_digest_finalize(ctx, sha3_512_ctx, digests->sha3_512);
if (r)
goto ERROR;
// Finalize SHA-3-256
- r = __pakfire_digest_finalize(pakfire, sha3_256_ctx, digests->sha3_256);
+ r = __pakfire_digest_finalize(ctx, sha3_256_ctx, digests->sha3_256);
if (r)
goto ERROR;
// Finalize BLAKE2b512
- r = __pakfire_digest_finalize(pakfire, blake2b512_ctx, digests->blake2b512);
+ r = __pakfire_digest_finalize(ctx, blake2b512_ctx, digests->blake2b512);
if (r)
goto ERROR;
// Finalize BLAKE2s256
- r = __pakfire_digest_finalize(pakfire, blake2s256_ctx, digests->blake2s256);
+ r = __pakfire_digest_finalize(ctx, blake2s256_ctx, digests->blake2s256);
if (r)
goto ERROR;
// Finalize SHA-2-512
- r = __pakfire_digest_finalize(pakfire, sha2_512_ctx, digests->sha2_512);
+ r = __pakfire_digest_finalize(ctx, sha2_512_ctx, digests->sha2_512);
if (r)
goto ERROR;
// Finalize SHA-2-256
- r = __pakfire_digest_finalize(pakfire, sha2_256_ctx, digests->sha2_256);
+ r = __pakfire_digest_finalize(ctx, sha2_256_ctx, digests->sha2_256);
if (r)
goto ERROR;
return r;
}
-static void pakfire_digests_compare_mismatch(struct pakfire* pakfire, const char* what,
+static void pakfire_digests_compare_mismatch(struct pakfire_ctx* ctx, const char* what,
const unsigned char* digest1, const unsigned char* digest2, const size_t length) {
char* hexdigest1 = __pakfire_hexlify(digest1, length);
char* hexdigest2 = __pakfire_hexlify(digest2, length);
- DEBUG(pakfire, "%s digest does not match:\n", what);
+ CTX_DEBUG(ctx, "%s digest does not match:\n", what);
if (hexdigest1)
- DEBUG(pakfire, " Digest 1: %s\n", hexdigest1);
+ CTX_DEBUG(ctx, " Digest 1: %s\n", hexdigest1);
if (hexdigest2)
- DEBUG(pakfire, " Digest 2: %s\n", hexdigest2);
+ CTX_DEBUG(ctx, " Digest 2: %s\n", hexdigest2);
if (hexdigest1)
free(hexdigest1);
free(hexdigest2);
}
-int pakfire_digests_compare(struct pakfire* pakfire, const struct pakfire_digests* digests1,
+int pakfire_digests_compare(struct pakfire_ctx* ctx, const struct pakfire_digests* digests1,
const struct pakfire_digests* digests2, const int types) {
int r;
if (types & PAKFIRE_DIGEST_SHA3_512) {
r = CRYPTO_memcmp(digests1->sha3_512, digests2->sha3_512, sizeof(digests1->sha3_512));
if (r) {
- pakfire_digests_compare_mismatch(pakfire, "SHA-3-512",
+ pakfire_digests_compare_mismatch(ctx, "SHA-3-512",
digests1->sha3_512, digests2->sha3_512, sizeof(digests1->sha3_512));
return 1;
}
if (types & PAKFIRE_DIGEST_SHA3_256) {
r = CRYPTO_memcmp(digests1->sha3_256, digests2->sha3_256, sizeof(digests1->sha3_256));
if (r) {
- pakfire_digests_compare_mismatch(pakfire, "SHA-3-256",
+ pakfire_digests_compare_mismatch(ctx, "SHA-3-256",
digests1->sha3_256, digests2->sha3_256, sizeof(digests1->sha3_256));
return 1;
}
if (types & PAKFIRE_DIGEST_BLAKE2B512) {
r = CRYPTO_memcmp(digests1->blake2b512, digests2->blake2b512, sizeof(digests1->blake2b512));
if (r) {
- pakfire_digests_compare_mismatch(pakfire, "BLAKE2b512",
+ pakfire_digests_compare_mismatch(ctx, "BLAKE2b512",
digests1->blake2b512, digests2->blake2b512, sizeof(digests1->blake2b512));
return 1;
}
if (types & PAKFIRE_DIGEST_BLAKE2S256) {
r = CRYPTO_memcmp(digests1->blake2s256, digests2->blake2s256, sizeof(digests1->blake2s256));
if (r) {
- pakfire_digests_compare_mismatch(pakfire, "BLAKE2s256",
+ pakfire_digests_compare_mismatch(ctx, "BLAKE2s256",
digests1->blake2s256, digests2->blake2s256, sizeof(digests1->blake2s256));
return 1;
}
if (types & PAKFIRE_DIGEST_SHA2_512) {
r = CRYPTO_memcmp(digests1->sha2_512, digests2->sha2_512, sizeof(digests1->sha2_512));
if (r) {
- pakfire_digests_compare_mismatch(pakfire, "SHA-2-512",
+ pakfire_digests_compare_mismatch(ctx, "SHA-2-512",
digests1->sha2_512, digests2->sha2_512, sizeof(digests1->sha2_512));
return 1;
}
if (types & PAKFIRE_DIGEST_SHA2_256) {
r = CRYPTO_memcmp(digests1->sha2_256, digests2->sha2_256, sizeof(digests1->sha2_256));
if (r) {
- pakfire_digests_compare_mismatch(pakfire, "SHA-2-256",
+ pakfire_digests_compare_mismatch(ctx, "SHA-2-256",
digests1->sha2_256, digests2->sha2_256, sizeof(digests1->sha2_256));
return 1;
}
return 0;
}
-int pakfire_digests_compare_one(struct pakfire* pakfire, struct pakfire_digests* digests1,
+int pakfire_digests_compare_one(struct pakfire_ctx* ctx, struct pakfire_digests* digests1,
const enum pakfire_digest_types type, const unsigned char* digest, const size_t length) {
struct pakfire_digests digests2;
int r;
// Check for valid inputs
- r = pakfire_digests_check_length(pakfire, type, length);
+ r = pakfire_digests_check_length(ctx, type, length);
if (r)
return r;
break;
}
- return pakfire_digests_compare(pakfire, digests1, &digests2, type);
+ return pakfire_digests_compare(ctx, digests1, &digests2, type);
}