1+ #include <openssl/evp.h>
2+ #include <openssl/rand.h>
3+ #include <openssl/err.h>
4+ #include <string.h>
5+ #include <stdlib.h>
6+
7+ // Sample OpenSSL code that demonstrates various cryptographic operations
8+ // that can be detected by the quantum model
9+
10+ // Function to perform AES-256-GCM encryption
11+ int encrypt_aes_gcm (const unsigned char * plaintext , int plaintext_len ,
12+ const unsigned char * key , const unsigned char * iv , int iv_len ,
13+ unsigned char * ciphertext , unsigned char * tag ) {
14+ EVP_CIPHER_CTX * ctx ;
15+ int len ;
16+ int ciphertext_len ;
17+
18+ // Create and initialize the context
19+ if (!(ctx = EVP_CIPHER_CTX_new ()))
20+ return -1 ;
21+
22+ // Initialize the encryption operation
23+ if (1 != EVP_EncryptInit_ex (ctx , EVP_aes_256_gcm (), NULL , NULL , NULL ))
24+ return -1 ;
25+
26+ // Set IV length (for GCM mode)
27+ if (1 != EVP_CIPHER_CTX_ctrl (ctx , EVP_CTRL_GCM_SET_IVLEN , iv_len , NULL ))
28+ return -1 ;
29+
30+ // Initialize key and IV
31+ if (1 != EVP_EncryptInit_ex (ctx , NULL , NULL , key , iv ))
32+ return -1 ;
33+
34+ // Provide the plaintext to be encrypted
35+ if (1 != EVP_EncryptUpdate (ctx , ciphertext , & len , plaintext , plaintext_len ))
36+ return -1 ;
37+ ciphertext_len = len ;
38+
39+ // Finalize the encryption
40+ if (1 != EVP_EncryptFinal_ex (ctx , ciphertext + len , & len ))
41+ return -1 ;
42+ ciphertext_len += len ;
43+
44+ // Get the tag
45+ if (1 != EVP_CIPHER_CTX_ctrl (ctx , EVP_CTRL_GCM_GET_TAG , 16 , tag ))
46+ return -1 ;
47+
48+ // Clean up
49+ EVP_CIPHER_CTX_free (ctx );
50+
51+ return ciphertext_len ;
52+ }
53+
54+ // Function to perform AES-256-GCM decryption
55+ int decrypt_aes_gcm (const unsigned char * ciphertext , int ciphertext_len ,
56+ const unsigned char * tag , const unsigned char * key ,
57+ const unsigned char * iv , int iv_len ,
58+ unsigned char * plaintext ) {
59+ EVP_CIPHER_CTX * ctx ;
60+ int len ;
61+ int plaintext_len ;
62+ int ret ;
63+
64+ // Create and initialize the context
65+ if (!(ctx = EVP_CIPHER_CTX_new ()))
66+ return -1 ;
67+
68+ // Initialize the decryption operation
69+ if (!EVP_DecryptInit_ex (ctx , EVP_aes_256_gcm (), NULL , NULL , NULL ))
70+ return -1 ;
71+
72+ // Set IV length
73+ if (!EVP_CIPHER_CTX_ctrl (ctx , EVP_CTRL_GCM_SET_IVLEN , iv_len , NULL ))
74+ return -1 ;
75+
76+ // Initialize key and IV
77+ if (!EVP_DecryptInit_ex (ctx , NULL , NULL , key , iv ))
78+ return -1 ;
79+
80+ // Provide the ciphertext to be decrypted
81+ if (!EVP_DecryptUpdate (ctx , plaintext , & len , ciphertext , ciphertext_len ))
82+ return -1 ;
83+ plaintext_len = len ;
84+
85+ // Set expected tag value
86+ if (!EVP_CIPHER_CTX_ctrl (ctx , EVP_CTRL_GCM_SET_TAG , 16 , (void * )tag ))
87+ return -1 ;
88+
89+ // Finalize the decryption
90+ ret = EVP_DecryptFinal_ex (ctx , plaintext + len , & len );
91+
92+ // Clean up
93+ EVP_CIPHER_CTX_free (ctx );
94+
95+ if (ret > 0 ) {
96+ // Success
97+ plaintext_len += len ;
98+ return plaintext_len ;
99+ } else {
100+ // Verification failed
101+ return -1 ;
102+ }
103+ }
104+
105+ // Function to calculate SHA-256 hash
106+ int calculate_sha256 (const unsigned char * message , size_t message_len ,
107+ unsigned char * digest ) {
108+ EVP_MD_CTX * mdctx ;
109+ unsigned int digest_len ;
110+
111+ // Create and initialize the context
112+ if (!(mdctx = EVP_MD_CTX_new ()))
113+ return 0 ;
114+
115+ // Initialize the hash operation
116+ if (1 != EVP_DigestInit_ex (mdctx , EVP_sha256 (), NULL ))
117+ return 0 ;
118+
119+ // Provide the message to be hashed
120+ if (1 != EVP_DigestUpdate (mdctx , message , message_len ))
121+ return 0 ;
122+
123+ // Finalize the hash
124+ if (1 != EVP_DigestFinal_ex (mdctx , digest , & digest_len ))
125+ return 0 ;
126+
127+ // Clean up
128+ EVP_MD_CTX_free (mdctx );
129+
130+ return 1 ;
131+ }
132+
133+ // Function to generate random bytes
134+ int generate_random_bytes (unsigned char * buffer , size_t length ) {
135+ return RAND_bytes (buffer , length );
136+ }
137+
138+ // Function using direct EVP_Digest function (one-shot hash)
139+ int calculate_md5_oneshot (const unsigned char * message , size_t message_len ,
140+ unsigned char * digest ) {
141+ unsigned int digest_len ;
142+
143+ // Calculate MD5 in a single call
144+ if (1 != EVP_Digest (message , message_len , digest , & digest_len , EVP_md5 (), NULL ))
145+ return 0 ;
146+
147+ return 1 ;
148+ }
149+
150+ // Function using HMAC
151+ int calculate_hmac_sha256 (const unsigned char * key , size_t key_len ,
152+ const unsigned char * message , size_t message_len ,
153+ unsigned char * mac ) {
154+ EVP_MD_CTX * ctx = EVP_MD_CTX_new ();
155+ EVP_PKEY * pkey = EVP_PKEY_new_mac_key (EVP_PKEY_HMAC , NULL , key , key_len );
156+
157+ if (!ctx || !pkey )
158+ return 0 ;
159+
160+ if (EVP_DigestSignInit (ctx , NULL , EVP_sha256 (), NULL , pkey ) != 1 )
161+ return 0 ;
162+
163+ if (EVP_DigestSignUpdate (ctx , message , message_len ) != 1 )
164+ return 0 ;
165+
166+ size_t mac_len = 32 ; // SHA-256 output size
167+ if (EVP_DigestSignFinal (ctx , mac , & mac_len ) != 1 )
168+ return 0 ;
169+
170+ EVP_MD_CTX_free (ctx );
171+ EVP_PKEY_free (pkey );
172+
173+ return 1 ;
174+ }
175+
176+ // Test function
177+ int main () {
178+ // Test encryption and decryption
179+ unsigned char * key = (unsigned char * )"01234567890123456789012345678901" ; // 32 bytes
180+ unsigned char * iv = (unsigned char * )"0123456789012345" ; // 16 bytes
181+ unsigned char * plaintext = (unsigned char * )"This is a test message for encryption" ;
182+ unsigned char ciphertext [1024 ];
183+ unsigned char tag [16 ];
184+ unsigned char decrypted [1024 ];
185+ int plaintext_len = strlen ((char * )plaintext );
186+ int ciphertext_len ;
187+ int decrypted_len ;
188+
189+ // Test SHA-256 hash
190+ unsigned char hash [32 ];
191+
192+ // Test random generation
193+ unsigned char random_bytes [32 ];
194+
195+ // Initialize OpenSSL
196+ ERR_load_crypto_strings ();
197+
198+ // Encrypt data
199+ ciphertext_len = encrypt_aes_gcm (plaintext , plaintext_len , key , iv , 16 , ciphertext , tag );
200+
201+ // Decrypt data
202+ decrypted_len = decrypt_aes_gcm (ciphertext , ciphertext_len , tag , key , iv , 16 , decrypted );
203+
204+ printf ("decrypted: %s\n" , decrypted );
205+
206+ // Calculate hash
207+ calculate_sha256 (plaintext , plaintext_len , hash );
208+
209+ // Generate random bytes
210+ generate_random_bytes (random_bytes , 32 );
211+
212+ // Calculate one-shot MD5
213+ unsigned char md5_hash [16 ];
214+ calculate_md5_oneshot (plaintext , plaintext_len , md5_hash );
215+
216+ // Calculate HMAC
217+ unsigned char hmac [32 ];
218+ calculate_hmac_sha256 (key , 32 , plaintext , plaintext_len , hmac );
219+
220+ return 0 ;
221+ }
0 commit comments