2018-06-11 23:48:45 +02:00
# include "olm/crypto.h"
# include "olm/olm.h"
2021-07-15 15:06:14 +02:00
# include "olm/pk.h"
2018-06-11 23:48:45 +02:00
2021-12-22 19:45:33 +01:00
# include "testing.hh"
2021-07-15 15:06:14 +02:00
# include "utils.hh"
2018-06-11 23:48:45 +02:00
# include <iostream>
2019-04-22 16:12:42 +02:00
# include <vector>
2018-06-11 23:48:45 +02:00
2021-12-22 19:45:33 +01:00
/* Encryption Test Case 1 */
2018-06-11 23:48:45 +02:00
2021-12-22 19:45:33 +01:00
TEST_CASE ( " Public Key Encryption/Decryption Test Case 1 " ) {
2018-06-11 23:48:45 +02:00
2019-04-22 16:12:42 +02:00
std : : vector < std : : uint8_t > decryption_buffer ( olm_pk_decryption_size ( ) ) ;
OlmPkDecryption * decryption = olm_pk_decryption ( decryption_buffer . data ( ) ) ;
2018-06-11 23:48:45 +02:00
std : : uint8_t alice_private [ 32 ] = {
0x77 , 0x07 , 0x6D , 0x0A , 0x73 , 0x18 , 0xA5 , 0x7D ,
0x3C , 0x16 , 0xC1 , 0x72 , 0x51 , 0xB2 , 0x66 , 0x45 ,
0xDF , 0x4C , 0x2F , 0x87 , 0xEB , 0xC0 , 0x99 , 0x2A ,
0xB1 , 0x77 , 0xFB , 0xA5 , 0x1D , 0xB9 , 0x2C , 0x2A
} ;
2018-10-01 21:01:47 +02:00
const std : : uint8_t * alice_public = ( std : : uint8_t * ) " hSDwCYkwp1R0i33ctD73Wg2/Og0mOBr066SpjqqbTmo " ;
2018-06-11 23:48:45 +02:00
std : : uint8_t bob_private [ 32 ] = {
0x5D , 0xAB , 0x08 , 0x7E , 0x62 , 0x4A , 0x8A , 0x4B ,
0x79 , 0xE1 , 0x7F , 0x8B , 0x83 , 0x80 , 0x0E , 0xE6 ,
0x6F , 0x3B , 0xB1 , 0x29 , 0x26 , 0x18 , 0xB6 , 0xFD ,
0x1C , 0x2F , 0x8B , 0x27 , 0xFF , 0x88 , 0xE0 , 0xEB
} ;
2018-10-01 21:01:47 +02:00
const std : : uint8_t * bob_public = ( std : : uint8_t * ) " 3p7bfXt9wbTTW2HC7OQ1Nz+DQ8hbeGdNrfx+FG+IK08 " ;
2018-06-11 23:48:45 +02:00
2019-04-22 16:12:42 +02:00
std : : vector < std : : uint8_t > pubkey ( : : olm_pk_key_length ( ) ) ;
2018-06-11 23:48:45 +02:00
2018-10-02 13:02:56 +02:00
olm_pk_key_from_private (
2018-06-11 23:48:45 +02:00
decryption ,
2019-04-22 16:12:42 +02:00
pubkey . data ( ) , pubkey . size ( ) ,
2018-06-11 23:48:45 +02:00
alice_private , sizeof ( alice_private )
) ;
2021-12-22 19:45:33 +01:00
CHECK_EQ_SIZE ( alice_public , ( const std : : uint8_t * ) pubkey . data ( ) , olm_pk_key_length ( ) ) ;
2018-06-11 23:48:45 +02:00
2018-10-02 13:02:56 +02:00
uint8_t * alice_private_back_out = ( uint8_t * ) malloc ( olm_pk_private_key_length ( ) ) ;
olm_pk_get_private_key ( decryption , alice_private_back_out , olm_pk_private_key_length ( ) ) ;
2021-12-22 19:45:33 +01:00
CHECK_EQ_SIZE ( alice_private , alice_private_back_out , olm_pk_private_key_length ( ) ) ;
2018-10-02 13:02:56 +02:00
free ( alice_private_back_out ) ;
2019-04-22 16:12:42 +02:00
std : : vector < std : : uint8_t > encryption_buffer ( olm_pk_encryption_size ( ) ) ;
OlmPkEncryption * encryption = olm_pk_encryption ( encryption_buffer . data ( ) ) ;
2018-06-11 23:48:45 +02:00
2019-04-22 16:12:42 +02:00
olm_pk_encryption_set_recipient_key ( encryption , pubkey . data ( ) , pubkey . size ( ) ) ;
2018-06-11 23:48:45 +02:00
const size_t plaintext_length = 14 ;
const std : : uint8_t * plaintext = ( std : : uint8_t * ) " This is a test " ;
size_t ciphertext_length = olm_pk_ciphertext_length ( encryption , plaintext_length ) ;
std : : uint8_t * ciphertext_buffer = ( std : : uint8_t * ) malloc ( ciphertext_length ) ;
2019-04-22 16:12:42 +02:00
std : : vector < std : : uint8_t > output_buffer ( olm_pk_mac_length ( encryption ) ) ;
std : : vector < std : : uint8_t > ephemeral_key ( olm_pk_key_length ( ) ) ;
2018-06-11 23:48:45 +02:00
olm_pk_encrypt (
encryption ,
plaintext , plaintext_length ,
ciphertext_buffer , ciphertext_length ,
2019-04-22 16:12:42 +02:00
output_buffer . data ( ) , output_buffer . size ( ) ,
ephemeral_key . data ( ) , ephemeral_key . size ( ) ,
2018-06-11 23:48:45 +02:00
bob_private , sizeof ( bob_private )
) ;
2021-12-22 19:45:33 +01:00
CHECK_EQ_SIZE ( bob_public , ( const std : : uint8_t * ) ephemeral_key . data ( ) , olm_pk_key_length ( ) ) ;
2018-06-11 23:48:45 +02:00
size_t max_plaintext_length = olm_pk_max_plaintext_length ( decryption , ciphertext_length ) ;
std : : uint8_t * plaintext_buffer = ( std : : uint8_t * ) malloc ( max_plaintext_length ) ;
olm_pk_decrypt (
decryption ,
2019-04-22 16:12:42 +02:00
ephemeral_key . data ( ) , ephemeral_key . size ( ) ,
output_buffer . data ( ) , output_buffer . size ( ) ,
2018-06-11 23:48:45 +02:00
ciphertext_buffer , ciphertext_length ,
plaintext_buffer , max_plaintext_length
) ;
2021-12-22 19:45:33 +01:00
CHECK_EQ_SIZE ( plaintext , ( const std : : uint8_t * ) plaintext_buffer , plaintext_length ) ;
2018-06-11 23:48:45 +02:00
free ( ciphertext_buffer ) ;
free ( plaintext_buffer ) ;
}
2021-12-22 19:45:33 +01:00
/* Encryption Test Case 1 */
2018-06-28 23:10:36 +02:00
2021-12-22 19:45:33 +01:00
TEST_CASE ( " Public Key Decryption pickling " ) {
2018-06-28 23:10:36 +02:00
2019-04-22 16:12:42 +02:00
std : : vector < std : : uint8_t > decryption_buffer ( olm_pk_decryption_size ( ) ) ;
OlmPkDecryption * decryption = olm_pk_decryption ( decryption_buffer . data ( ) ) ;
2018-06-28 23:10:36 +02:00
std : : uint8_t alice_private [ 32 ] = {
0x77 , 0x07 , 0x6D , 0x0A , 0x73 , 0x18 , 0xA5 , 0x7D ,
0x3C , 0x16 , 0xC1 , 0x72 , 0x51 , 0xB2 , 0x66 , 0x45 ,
0xDF , 0x4C , 0x2F , 0x87 , 0xEB , 0xC0 , 0x99 , 0x2A ,
0xB1 , 0x77 , 0xFB , 0xA5 , 0x1D , 0xB9 , 0x2C , 0x2A
} ;
const std : : uint8_t * alice_public = ( std : : uint8_t * ) " hSDwCYkwp1R0i33ctD73Wg2/Og0mOBr066SpjqqbTmoK " ;
2019-04-22 16:12:42 +02:00
std : : vector < std : : uint8_t > pubkey ( olm_pk_key_length ( ) ) ;
2018-06-28 23:10:36 +02:00
2018-10-02 13:02:56 +02:00
olm_pk_key_from_private (
2018-06-28 23:10:36 +02:00
decryption ,
2019-04-22 16:12:42 +02:00
pubkey . data ( ) , pubkey . size ( ) ,
2018-06-28 23:10:36 +02:00
alice_private , sizeof ( alice_private )
) ;
const uint8_t * PICKLE_KEY = ( uint8_t * ) " secret_key " ;
2019-04-22 16:12:42 +02:00
std : : vector < std : : uint8_t > pickle_buffer ( olm_pickle_pk_decryption_length ( decryption ) ) ;
2018-06-28 23:10:36 +02:00
const uint8_t * expected_pickle = ( uint8_t * ) " qx37WTQrjZLz5tId/uBX9B3/okqAbV1ofl9UnHKno1eipByCpXleAAlAZoJgYnCDOQZDQWzo3luTSfkF9pU1mOILCbbouubs6TVeDyPfgGD9i86J8irHjA " ;
olm_pickle_pk_decryption (
decryption ,
PICKLE_KEY , strlen ( ( char * ) PICKLE_KEY ) ,
2019-04-22 16:12:42 +02:00
pickle_buffer . data ( ) , pickle_buffer . size ( )
2018-06-28 23:10:36 +02:00
) ;
2021-12-22 19:45:33 +01:00
CHECK_EQ_SIZE ( expected_pickle , ( const std : : uint8_t * ) pickle_buffer . data ( ) , olm_pickle_pk_decryption_length ( decryption ) ) ;
2018-06-28 23:10:36 +02:00
olm_clear_pk_decryption ( decryption ) ;
2019-04-22 16:12:42 +02:00
memset ( pubkey . data ( ) , 0 , olm_pk_key_length ( ) ) ;
2018-06-28 23:10:36 +02:00
olm_unpickle_pk_decryption (
decryption ,
PICKLE_KEY , strlen ( ( char * ) PICKLE_KEY ) ,
2019-04-22 16:12:42 +02:00
pickle_buffer . data ( ) , pickle_buffer . size ( ) ,
pubkey . data ( ) , pubkey . size ( )
2018-06-28 23:10:36 +02:00
) ;
2021-12-22 19:45:33 +01:00
CHECK_EQ_SIZE ( alice_public , ( const std : : uint8_t * ) pubkey . data ( ) , olm_pk_key_length ( ) ) ;
2018-06-28 23:10:36 +02:00
2021-07-15 15:06:14 +02:00
/* Deliberately corrupt the pickled session by supplying a junk suffix and
* ensure this is caught as an error . */
const size_t junk_length = 1 ;
std : : size_t pickle_length = olm_pickle_pk_decryption_length ( decryption ) ;
2021-11-17 20:18:03 +01:00
std : : vector < std : : uint8_t > junk_pickle ( pickle_length + _olm_enc_output_length ( junk_length ) ) ;
2021-07-15 15:06:14 +02:00
olm_pickle_pk_decryption (
decryption ,
PICKLE_KEY , strlen ( ( char * ) PICKLE_KEY ) ,
junk_pickle . data ( ) , pickle_length
) ;
const size_t junk_pickle_length = add_junk_suffix_to_pickle (
PICKLE_KEY , strlen ( ( char * ) PICKLE_KEY ) ,
junk_pickle . data ( ) ,
pickle_length ,
junk_length ) ;
2021-12-22 19:45:33 +01:00
CHECK_EQ ( std : : size_t ( - 1 ) ,
2021-07-15 15:06:14 +02:00
olm_unpickle_pk_decryption (
decryption ,
PICKLE_KEY , strlen ( ( char * ) PICKLE_KEY ) ,
junk_pickle . data ( ) , junk_pickle_length ,
pubkey . data ( ) , pubkey . size ( )
) ) ;
2021-12-22 19:45:33 +01:00
CHECK_EQ ( OLM_PICKLE_EXTRA_DATA , olm_pk_decryption_last_error_code ( decryption ) ) ;
2021-07-15 15:06:14 +02:00
/***/
2018-06-28 23:10:36 +02:00
char * ciphertext = strdup ( " ntk49j/KozVFtSqJXhCejg " ) ;
const char * mac = " zpzU6BkZcNI " ;
const char * ephemeral_key = " 3p7bfXt9wbTTW2HC7OQ1Nz+DQ8hbeGdNrfx+FG+IK08 " ;
size_t max_plaintext_length = olm_pk_max_plaintext_length ( decryption , strlen ( ciphertext ) ) ;
std : : uint8_t * plaintext_buffer = ( std : : uint8_t * ) malloc ( max_plaintext_length ) ;
olm_pk_decrypt (
decryption ,
ephemeral_key , strlen ( ephemeral_key ) ,
mac , strlen ( mac ) ,
ciphertext , strlen ( ciphertext ) ,
plaintext_buffer , max_plaintext_length
) ;
const std : : uint8_t * plaintext = ( std : : uint8_t * ) " This is a test " ;
2021-12-22 19:45:33 +01:00
CHECK_EQ_SIZE ( plaintext , ( const std : : uint8_t * ) plaintext_buffer , strlen ( ( const char * ) plaintext ) ) ;
2018-06-28 23:10:36 +02:00
free ( ciphertext ) ;
free ( plaintext_buffer ) ;
2019-01-29 21:47:41 +01:00
}
2021-12-22 19:45:33 +01:00
/* Signing Test Case 1 */
2019-01-29 21:47:41 +01:00
2021-12-22 19:45:33 +01:00
TEST_CASE ( " Public Key Signing " ) {
2019-01-29 21:47:41 +01:00
2019-04-22 16:12:42 +02:00
std : : vector < std : : uint8_t > signing_buffer ( olm_pk_signing_size ( ) ) ;
OlmPkSigning * signing = olm_pk_signing ( signing_buffer . data ( ) ) ;
2019-01-29 21:47:41 +01:00
std : : uint8_t seed [ 32 ] = {
0x77 , 0x07 , 0x6D , 0x0A , 0x73 , 0x18 , 0xA5 , 0x7D ,
0x3C , 0x16 , 0xC1 , 0x72 , 0x51 , 0xB2 , 0x66 , 0x45 ,
0xDF , 0x4C , 0x2F , 0x87 , 0xEB , 0xC0 , 0x99 , 0x2A ,
0xB1 , 0x77 , 0xFB , 0xA5 , 0x1D , 0xB9 , 0x2C , 0x2A
} ;
//const std::uint8_t *pub_key = (std::uint8_t *) "hSDwCYkwp1R0i33ctD73Wg2/Og0mOBr066SpjqqbTmoK";
2019-04-22 16:12:42 +02:00
std : : vector < char > pubkey ( olm_pk_signing_public_key_length ( ) + 1 ) ;
2019-01-29 21:47:41 +01:00
olm_pk_signing_key_from_seed (
signing ,
2019-04-22 16:12:42 +02:00
pubkey . data ( ) , pubkey . size ( ) - 1 ,
2019-01-29 21:47:41 +01:00
seed , sizeof ( seed )
) ;
char * message = strdup ( " We hold these truths to be self-evident, that all men are created equal, that they are endowed by their Creator with certain unalienable Rights, that among these are Life, Liberty and the pursuit of Happiness. " ) ;
std : : uint8_t * sig_buffer = ( std : : uint8_t * ) malloc ( olm_pk_signature_length ( ) + 1 ) ;
olm_pk_sign (
signing ,
( const uint8_t * ) message , strlen ( message ) ,
sig_buffer , olm_pk_signature_length ( )
) ;
void * utility_buffer = malloc ( : : olm_utility_size ( ) ) ;
: : OlmUtility * utility = : : olm_utility ( utility_buffer ) ;
size_t result ;
result = : : olm_ed25519_verify (
utility ,
2019-04-22 16:12:42 +02:00
pubkey . data ( ) , olm_pk_signing_public_key_length ( ) ,
2019-01-29 21:47:41 +01:00
message , strlen ( message ) ,
sig_buffer , olm_pk_signature_length ( )
) ;
2021-12-22 19:45:33 +01:00
CHECK_EQ ( ( size_t ) 0 , result ) ;
2019-01-29 21:47:41 +01:00
sig_buffer [ 5 ] = ' m ' ;
result = : : olm_ed25519_verify (
utility ,
2019-04-22 16:12:42 +02:00
pubkey . data ( ) , olm_pk_signing_public_key_length ( ) ,
2019-01-29 21:47:41 +01:00
message , strlen ( message ) ,
sig_buffer , olm_pk_signature_length ( )
) ;
2021-12-22 19:45:33 +01:00
CHECK_EQ ( ( size_t ) - 1 , result ) ;
2019-01-29 21:47:41 +01:00
2020-10-01 15:39:48 +02:00
olm_clear_utility ( utility ) ;
free ( utility_buffer ) ;
2019-01-29 21:47:41 +01:00
free ( message ) ;
free ( sig_buffer ) ;
olm_clear_pk_signing ( signing ) ;
2018-06-28 23:10:36 +02:00
}