mirror of
https://github.com/github/codeql.git
synced 2025-12-16 16:53:25 +01:00
Crypto: Adding initial openssl tests, fixing a bug in hash modeling found through tests, and updating CODEOWNERS for quantum tests
This commit is contained in:
committed by
Nicolas Will
parent
21cb8b2172
commit
0de6647927
@@ -16,7 +16,7 @@
|
|||||||
/java/ql/test-kotlin2/ @github/codeql-kotlin
|
/java/ql/test-kotlin2/ @github/codeql-kotlin
|
||||||
|
|
||||||
# Experimental CodeQL cryptography
|
# Experimental CodeQL cryptography
|
||||||
**/experimental/quantum/ @github/ps-codeql
|
**/experimental/**/quantum/ @github/ps-codeql
|
||||||
/shared/quantum/ @github/ps-codeql
|
/shared/quantum/ @github/ps-codeql
|
||||||
|
|
||||||
# CodeQL tools and associated docs
|
# CodeQL tools and associated docs
|
||||||
|
|||||||
@@ -29,7 +29,19 @@ import semmle.code.cpp.dataflow.new.DataFlow
|
|||||||
* - EVP_PKEY_CTX
|
* - EVP_PKEY_CTX
|
||||||
*/
|
*/
|
||||||
private class CtxType extends Type {
|
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")
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ private module AlgGetterToAlgConsumerConfig implements DataFlow::ConfigSig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
predicate isSink(DataFlow::Node sink) {
|
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<AlgGetterToAlgConsu
|
|||||||
abstract class EVP_Cipher_Operation extends OpenSSLOperation, Crypto::KeyOperationInstance {
|
abstract class EVP_Cipher_Operation extends OpenSSLOperation, Crypto::KeyOperationInstance {
|
||||||
Expr getContextArg() { result = this.(Call).getArgument(0) }
|
Expr getContextArg() { result = this.(Call).getArgument(0) }
|
||||||
|
|
||||||
|
Expr getAlgorithmArg() { this.getInitCall().getAlgorithmArg() = result }
|
||||||
|
|
||||||
override Expr getOutputArg() { result = this.(Call).getArgument(1) }
|
override Expr getOutputArg() { result = this.(Call).getArgument(1) }
|
||||||
|
|
||||||
override Crypto::KeyOperationSubtype getKeyOperationSubtype() {
|
override Crypto::KeyOperationSubtype getKeyOperationSubtype() {
|
||||||
|
|||||||
@@ -12,6 +12,8 @@ private import experimental.quantum.OpenSSL.AlgorithmValueConsumers.OpenSSLAlgor
|
|||||||
abstract class EVP_Hash_Operation extends OpenSSLOperation, Crypto::HashOperationInstance {
|
abstract class EVP_Hash_Operation extends OpenSSLOperation, Crypto::HashOperationInstance {
|
||||||
Expr getContextArg() { result = this.(Call).getArgument(0) }
|
Expr getContextArg() { result = this.(Call).getArgument(0) }
|
||||||
|
|
||||||
|
Expr getAlgorithmArg() { result = this.getInitCall().getAlgorithmArg() }
|
||||||
|
|
||||||
EVP_Hash_Initializer getInitCall() {
|
EVP_Hash_Initializer getInitCall() {
|
||||||
CTXFlow::ctxArgFlowsToCtxArg(result.getContextArg(), this.getContextArg())
|
CTXFlow::ctxArgFlowsToCtxArg(result.getContextArg(), this.getContextArg())
|
||||||
}
|
}
|
||||||
@@ -23,7 +25,7 @@ abstract class EVP_Hash_Operation extends OpenSSLOperation, Crypto::HashOperatio
|
|||||||
*/
|
*/
|
||||||
override Crypto::AlgorithmValueConsumer getAnAlgorithmValueConsumer() {
|
override Crypto::AlgorithmValueConsumer getAnAlgorithmValueConsumer() {
|
||||||
AlgGetterToAlgConsumerFlow::flow(result.(OpenSSLAlgorithmValueConsumer).getResultNode(),
|
AlgGetterToAlgConsumerFlow::flow(result.(OpenSSLAlgorithmValueConsumer).getResultNode(),
|
||||||
DataFlow::exprNode(this.getInitCall().getAlgorithmArg()))
|
DataFlow::exprNode(this.getAlgorithmArg()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -33,7 +35,7 @@ private module AlgGetterToAlgConsumerConfig implements DataFlow::ConfigSig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
predicate isSink(DataFlow::Node sink) {
|
predicate isSink(DataFlow::Node sink) {
|
||||||
exists(EVP_Hash_Operation c | c.getInitCall().getAlgorithmArg() = sink.asExpr())
|
exists(EVP_Hash_Operation c | c.getAlgorithmArg() = sink.asExpr())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -64,6 +66,8 @@ class EVP_Q_Digest_Operation extends EVP_Hash_Operation {
|
|||||||
// simply return 'this', see modeled hash algorithm consuers for EVP_Q_Digest
|
// simply return 'this', see modeled hash algorithm consuers for EVP_Q_Digest
|
||||||
this = result
|
this = result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override Expr getAlgorithmArg() { result = this.(Call).getArgument(1) }
|
||||||
}
|
}
|
||||||
|
|
||||||
class EVP_Digest_Operation extends EVP_Hash_Operation {
|
class EVP_Digest_Operation extends EVP_Hash_Operation {
|
||||||
@@ -72,17 +76,14 @@ class EVP_Digest_Operation extends EVP_Hash_Operation {
|
|||||||
// There is no context argument for this function
|
// There is no context argument for this function
|
||||||
override Expr getContextArg() { none() }
|
override Expr getContextArg() { none() }
|
||||||
|
|
||||||
override Crypto::AlgorithmValueConsumer getAnAlgorithmValueConsumer() {
|
|
||||||
AlgGetterToAlgConsumerFlow::flow(result.(OpenSSLAlgorithmValueConsumer).getResultNode(),
|
|
||||||
DataFlow::exprNode(this.(Call).getArgument(4)))
|
|
||||||
}
|
|
||||||
|
|
||||||
override EVP_Hash_Initializer getInitCall() {
|
override EVP_Hash_Initializer getInitCall() {
|
||||||
// This variant of digest does not use an init
|
// This variant of digest does not use an init
|
||||||
// and even if it were used, the init would be ignored/undefined
|
// and even if it were used, the init would be ignored/undefined
|
||||||
none()
|
none()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override Expr getAlgorithmArg() { result = this.(Call).getArgument(4) }
|
||||||
|
|
||||||
override Expr getOutputArg() { result = this.(Call).getArgument(2) }
|
override Expr getOutputArg() { result = this.(Call).getArgument(2) }
|
||||||
|
|
||||||
override Expr getInputArg() { result = this.(Call).getArgument(0) }
|
override Expr getInputArg() { result = this.(Call).getArgument(0) }
|
||||||
|
|||||||
@@ -0,0 +1,2 @@
|
|||||||
|
| openssl_basic.c:40:13:40:31 | EncryptOperation | openssl_basic.c:31:49:31:51 | Key | openssl_basic.c:179:43:179:76 | Constant |
|
||||||
|
| openssl_basic.c:90:11:90:29 | DecryptOperation | openssl_basic.c:77:45:77:47 | Key | openssl_basic.c:179:43:179:76 | Constant |
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
import cpp
|
||||||
|
import experimental.quantum.Language
|
||||||
|
|
||||||
|
from Crypto::CipherOperationNode op, Crypto::KeyArtifactNode k
|
||||||
|
where op.getAKey() = k
|
||||||
|
select op, k, k.getSourceNode()
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
| openssl_basic.c:40:13:40:31 | EncryptOperation | openssl_basic.c:31:54:31:55 | Nonce | openssl_basic.c:180:42:180:59 | Constant |
|
||||||
|
| openssl_basic.c:90:11:90:29 | DecryptOperation | openssl_basic.c:77:50:77:51 | Nonce | openssl_basic.c:180:42:180:59 | Constant |
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
import cpp
|
||||||
|
import experimental.quantum.Language
|
||||||
|
|
||||||
|
from Crypto::CipherOperationNode op, Crypto::NonceArtifactNode n
|
||||||
|
where op.getANonce() = n
|
||||||
|
select op, n, n.getSourceNode()
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
| openssl_basic.c:40:13:40:31 | EncryptOperation | openssl_basic.c:35:54:35:62 | Message | openssl_basic.c:40:13:40:31 | KeyOperationOutput | openssl_basic.c:23:62:23:65 | Key | openssl_basic.c:23:68:23:71 | Nonce | openssl_basic.c:23:37:23:51 | KeyOperationAlgorithm | Encrypt |
|
||||||
|
| openssl_basic.c:40:13:40:31 | EncryptOperation | openssl_basic.c:35:54:35:62 | Message | openssl_basic.c:40:13:40:31 | KeyOperationOutput | openssl_basic.c:23:62:23:65 | Key | openssl_basic.c:31:54:31:55 | Nonce | openssl_basic.c:23:37:23:51 | KeyOperationAlgorithm | Encrypt |
|
||||||
|
| openssl_basic.c:40:13:40:31 | EncryptOperation | openssl_basic.c:35:54:35:62 | Message | openssl_basic.c:40:13:40:31 | KeyOperationOutput | openssl_basic.c:31:49:31:51 | Key | openssl_basic.c:23:68:23:71 | Nonce | openssl_basic.c:23:37:23:51 | KeyOperationAlgorithm | Encrypt |
|
||||||
|
| openssl_basic.c:40:13:40:31 | EncryptOperation | openssl_basic.c:35:54:35:62 | Message | openssl_basic.c:40:13:40:31 | KeyOperationOutput | openssl_basic.c:31:49:31:51 | Key | openssl_basic.c:31:54:31:55 | Nonce | openssl_basic.c:23:37:23:51 | KeyOperationAlgorithm | Encrypt |
|
||||||
|
| openssl_basic.c:90:11:90:29 | DecryptOperation | openssl_basic.c:81:49:81:58 | Message | openssl_basic.c:90:11:90:29 | KeyOperationOutput | openssl_basic.c:69:58:69:61 | Key | openssl_basic.c:69:64:69:67 | Nonce | openssl_basic.c:69:33:69:47 | KeyOperationAlgorithm | Decrypt |
|
||||||
|
| openssl_basic.c:90:11:90:29 | DecryptOperation | openssl_basic.c:81:49:81:58 | Message | openssl_basic.c:90:11:90:29 | KeyOperationOutput | openssl_basic.c:69:58:69:61 | Key | openssl_basic.c:77:50:77:51 | Nonce | openssl_basic.c:69:33:69:47 | KeyOperationAlgorithm | Decrypt |
|
||||||
|
| openssl_basic.c:90:11:90:29 | DecryptOperation | openssl_basic.c:81:49:81:58 | Message | openssl_basic.c:90:11:90:29 | KeyOperationOutput | openssl_basic.c:77:45:77:47 | Key | openssl_basic.c:69:64:69:67 | Nonce | openssl_basic.c:69:33:69:47 | KeyOperationAlgorithm | Decrypt |
|
||||||
|
| openssl_basic.c:90:11:90:29 | DecryptOperation | openssl_basic.c:81:49:81:58 | Message | openssl_basic.c:90:11:90:29 | KeyOperationOutput | openssl_basic.c:77:45:77:47 | Key | openssl_basic.c:77:50:77:51 | Nonce | openssl_basic.c:69:33:69:47 | KeyOperationAlgorithm | Decrypt |
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
import cpp
|
||||||
|
import experimental.quantum.Language
|
||||||
|
|
||||||
|
from Crypto::CipherOperationNode n
|
||||||
|
select n, n.getAnInputArtifact(), n.getAnOutputArtifact(), n.getAKey(), n.getANonce(),
|
||||||
|
n.getAnAlgorithmOrGenericSource(), n.getKeyOperationSubtype()
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
| openssl_basic.c:40:13:40:31 | EncryptOperation | openssl_basic.c:35:54:35:62 | Message | openssl_basic.c:181:49:181:87 | Constant |
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
import cpp
|
||||||
|
import experimental.quantum.Language
|
||||||
|
|
||||||
|
from Crypto::CipherOperationNode n, Crypto::MessageArtifactNode m
|
||||||
|
where n.getAnInputArtifact() = m
|
||||||
|
select n, m, m.getSourceNode()
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
| openssl_basic.c:124:13:124:30 | HashOperation | openssl_basic.c:120:37:120:43 | Message | openssl_basic.c:181:49:181:87 | Constant |
|
||||||
|
| openssl_basic.c:144:13:144:22 | HashOperation | openssl_basic.c:144:24:144:30 | Message | openssl_basic.c:181:49:181:87 | Constant |
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
import cpp
|
||||||
|
import experimental.quantum.Language
|
||||||
|
|
||||||
|
from Crypto::HashOperationNode n, Crypto::MessageArtifactNode m
|
||||||
|
where n.getInputArtifact() = m
|
||||||
|
select n, m, m.getSourceNode()
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
| openssl_basic.c:124:13:124:30 | HashOperation | openssl_basic.c:124:13:124:30 | Digest | openssl_basic.c:116:38:116:47 | HashAlgorithm | openssl_basic.c:120:37:120:43 | Message |
|
||||||
|
| openssl_basic.c:144:13:144:22 | HashOperation | openssl_basic.c:144:13:144:22 | Digest | openssl_basic.c:144:67:144:73 | HashAlgorithm | openssl_basic.c:144:24:144:30 | Message |
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
import cpp
|
||||||
|
import experimental.quantum.Language
|
||||||
|
|
||||||
|
from Crypto::HashOperationNode n
|
||||||
|
select n, n.getDigest(), n.getAnAlgorithmOrGenericSource(), n.getInputArtifact()
|
||||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,3 @@
|
|||||||
|
int RAND_bytes(unsigned char *buf, int num);
|
||||||
|
|
||||||
|
int RAND_pseudo_bytes(unsigned char *buf, int num);
|
||||||
@@ -0,0 +1,221 @@
|
|||||||
|
#include "includes/evp_stubs.h"
|
||||||
|
#include "includes/alg_macro_stubs.h"
|
||||||
|
#include "includes/rand_stubs.h"
|
||||||
|
|
||||||
|
size_t strlen(const char* str);
|
||||||
|
|
||||||
|
// Sample OpenSSL code that demonstrates various cryptographic operations
|
||||||
|
// that can be detected by the quantum model
|
||||||
|
|
||||||
|
// Function to perform AES-256-GCM encryption
|
||||||
|
int encrypt_aes_gcm(const unsigned char *plaintext, int plaintext_len,
|
||||||
|
const unsigned char *key, const unsigned char *iv, int iv_len,
|
||||||
|
unsigned char *ciphertext, unsigned char *tag) {
|
||||||
|
EVP_CIPHER_CTX *ctx;
|
||||||
|
int len;
|
||||||
|
int ciphertext_len;
|
||||||
|
|
||||||
|
// Create and initialize the context
|
||||||
|
if(!(ctx = EVP_CIPHER_CTX_new()))
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
// Initialize the encryption operation
|
||||||
|
if(1 != EVP_EncryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL))
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
// Set IV length (for GCM mode)
|
||||||
|
if(1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, iv_len, NULL))
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
// Initialize key and IV
|
||||||
|
if(1 != EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv))
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
// Provide the plaintext to be encrypted
|
||||||
|
if(1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len))
|
||||||
|
return -1;
|
||||||
|
ciphertext_len = len;
|
||||||
|
|
||||||
|
// Finalize the encryption
|
||||||
|
if(1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len))
|
||||||
|
return -1;
|
||||||
|
ciphertext_len += len;
|
||||||
|
|
||||||
|
// Get the tag
|
||||||
|
if(1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, 16, tag))
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
// Clean up
|
||||||
|
EVP_CIPHER_CTX_free(ctx);
|
||||||
|
|
||||||
|
return ciphertext_len;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Function to perform AES-256-GCM decryption
|
||||||
|
int decrypt_aes_gcm(const unsigned char *ciphertext, int ciphertext_len,
|
||||||
|
const unsigned char *tag, const unsigned char *key,
|
||||||
|
const unsigned char *iv, int iv_len,
|
||||||
|
unsigned char *plaintext) {
|
||||||
|
EVP_CIPHER_CTX *ctx;
|
||||||
|
int len;
|
||||||
|
int plaintext_len;
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
// Create and initialize the context
|
||||||
|
if(!(ctx = EVP_CIPHER_CTX_new()))
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
// Initialize the decryption operation
|
||||||
|
if(!EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL))
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
// Set IV length
|
||||||
|
if(!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, iv_len, NULL))
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
// Initialize key and IV
|
||||||
|
if(!EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv))
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
// Provide the ciphertext to be decrypted
|
||||||
|
if(!EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len))
|
||||||
|
return -1;
|
||||||
|
plaintext_len = len;
|
||||||
|
|
||||||
|
// Set expected tag value
|
||||||
|
if(!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, 16, (void*)tag))
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
// Finalize the decryption
|
||||||
|
ret = EVP_DecryptFinal_ex(ctx, plaintext + len, &len);
|
||||||
|
|
||||||
|
// Clean up
|
||||||
|
EVP_CIPHER_CTX_free(ctx);
|
||||||
|
|
||||||
|
if(ret > 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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user