diff --git a/CODEOWNERS b/CODEOWNERS index 96aa46df949..7233623d452 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -16,7 +16,7 @@ /java/ql/test-kotlin2/ @github/codeql-kotlin # Experimental CodeQL cryptography -**/experimental/quantum/ @github/ps-codeql +**/experimental/**/quantum/ @github/ps-codeql /shared/quantum/ @github/ps-codeql # CodeQL tools and associated docs diff --git a/cpp/ql/lib/experimental/quantum/OpenSSL/CtxFlow.qll b/cpp/ql/lib/experimental/quantum/OpenSSL/CtxFlow.qll index cbce19fb5df..38b49b8d901 100644 --- a/cpp/ql/lib/experimental/quantum/OpenSSL/CtxFlow.qll +++ b/cpp/ql/lib/experimental/quantum/OpenSSL/CtxFlow.qll @@ -29,7 +29,19 @@ import semmle.code.cpp.dataflow.new.DataFlow * - EVP_PKEY_CTX */ private class CtxType extends Type { - CtxType() { this.getUnspecifiedType().stripType().getName().matches("evp_%ctx_%st") } + CtxType() { + // It is possible for users to use the underlying type of the CTX variables + // these have a name matching 'evp_%ctx_%st + this.getUnspecifiedType().stripType().getName().matches("evp_%ctx_%st") + or + // In principal the above check should be sufficient, but in case of build mode none issues + // i.e., if a typedef cannot be resolved, + // or issues with properly stubbing test cases, we also explicitly check for the wrapping type defs + // i.e., patterns matching 'EVP_%_CTX' + exists(Type base | base = this or base = this.(DerivedType).getBaseType() | + base.getName().matches("EVP_%_CTX") + ) + } } /** diff --git a/cpp/ql/lib/experimental/quantum/OpenSSL/Operations/EVPCipherOperation.qll b/cpp/ql/lib/experimental/quantum/OpenSSL/Operations/EVPCipherOperation.qll index bb884f6db53..233bfd50433 100644 --- a/cpp/ql/lib/experimental/quantum/OpenSSL/Operations/EVPCipherOperation.qll +++ b/cpp/ql/lib/experimental/quantum/OpenSSL/Operations/EVPCipherOperation.qll @@ -10,7 +10,7 @@ private module AlgGetterToAlgConsumerConfig implements DataFlow::ConfigSig { } predicate isSink(DataFlow::Node sink) { - exists(EVP_Cipher_Operation c | c.getInitCall().getAlgorithmArg() = sink.asExpr()) + exists(EVP_Cipher_Operation c | c.getAlgorithmArg() = sink.asExpr()) } } @@ -32,6 +32,8 @@ private module AlgGetterToAlgConsumerFlow = DataFlow::Global 0) { + // Success + plaintext_len += len; + return plaintext_len; + } else { + // Verification failed + return -1; + } +} + +// Function to calculate SHA-256 hash +int calculate_sha256(const unsigned char *message, size_t message_len, + unsigned char *digest) { + EVP_MD_CTX *mdctx; + unsigned int digest_len; + + // Create and initialize the context + if(!(mdctx = EVP_MD_CTX_new())) + return 0; + + // Initialize the hash operation + if(1 != EVP_DigestInit_ex(mdctx, EVP_sha256(), NULL)) + return 0; + + // Provide the message to be hashed + if(1 != EVP_DigestUpdate(mdctx, message, message_len)) + return 0; + + // Finalize the hash + if(1 != EVP_DigestFinal_ex(mdctx, digest, &digest_len)) + return 0; + + // Clean up + EVP_MD_CTX_free(mdctx); + + return 1; +} + +// Function to generate random bytes +int generate_random_bytes(unsigned char *buffer, size_t length) { + return RAND_bytes(buffer, length); +} + +// Function using direct EVP_Digest function (one-shot hash) +int calculate_md5_oneshot(const unsigned char *message, size_t message_len, + unsigned char *digest) { + unsigned int digest_len; + + // Calculate MD5 in a single call + if(1 != EVP_Digest(message, message_len, digest, &digest_len, EVP_md5(), NULL)) + return 0; + + return 1; +} + +// Function using HMAC +int calculate_hmac_sha256(const unsigned char *key, size_t key_len, + const unsigned char *message, size_t message_len, + unsigned char *mac) { + EVP_MD_CTX *ctx = EVP_MD_CTX_new(); + EVP_PKEY *pkey = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, key, key_len); + + if (!ctx || !pkey) + return 0; + + if (EVP_DigestSignInit(ctx, NULL, EVP_sha256(), NULL, pkey) != 1) + return 0; + + if (EVP_DigestSignUpdate(ctx, message, message_len) != 1) + return 0; + + size_t mac_len = 32; // SHA-256 output size + if (EVP_DigestSignFinal(ctx, mac, &mac_len) != 1) + return 0; + + EVP_MD_CTX_free(ctx); + EVP_PKEY_free(pkey); + + return 1; +} + +// Test function +int test_main() { + // Test encryption and decryption + unsigned char *key = (unsigned char *)"01234567890123456789012345678901"; // 32 bytes + unsigned char *iv = (unsigned char *)"0123456789012345"; // 16 bytes + unsigned char *plaintext = (unsigned char *)"This is a test message for encryption"; + unsigned char ciphertext[1024]; + unsigned char tag[16]; + unsigned char decrypted[1024]; + int plaintext_len = strlen((char *)plaintext); + int ciphertext_len; + int decrypted_len; + + // Test SHA-256 hash + unsigned char hash[32]; + + // Test random generation + unsigned char random_bytes[32]; + + // // Initialize OpenSSL + // ERR_load_crypto_strings(); + + // Encrypt data + ciphertext_len = encrypt_aes_gcm(plaintext, plaintext_len, key, iv, 16, ciphertext, tag); + + // Decrypt data + decrypted_len = decrypt_aes_gcm(ciphertext, ciphertext_len, tag, key, iv, 16, decrypted); + + //printf("decrypted: %s\n", decrypted); + + // Calculate hash + calculate_sha256(plaintext, plaintext_len, hash); + + // Generate random bytes + generate_random_bytes(random_bytes, 32); + + // Calculate one-shot MD5 + unsigned char md5_hash[16]; + calculate_md5_oneshot(plaintext, plaintext_len, md5_hash); + + // Calculate HMAC + unsigned char hmac[32]; + calculate_hmac_sha256(key, 32, plaintext, plaintext_len, hmac); + + return 0; +} \ No newline at end of file