| 1 |
#include "CUnit/Basic.h" |
|---|
| 2 |
|
|---|
| 3 |
#include "tests.h" |
|---|
| 4 |
#include "openpgpsdk/types.h" |
|---|
| 5 |
#include "openpgpsdk/keyring.h" |
|---|
| 6 |
#include "../src/advanced/keyring_local.h" |
|---|
| 7 |
#include "openpgpsdk/packet.h" |
|---|
| 8 |
#include "openpgpsdk/create.h" |
|---|
| 9 |
|
|---|
| 10 |
static char secring[MAXBUF+1]; |
|---|
| 11 |
static char pubring[MAXBUF+1]; |
|---|
| 12 |
static ops_keyring_t pub_keyring; |
|---|
| 13 |
static ops_keyring_t sec_keyring; |
|---|
| 14 |
static const ops_key_data_t *pubkey; |
|---|
| 15 |
static const ops_key_data_t *seckey; |
|---|
| 16 |
|
|---|
| 17 |
int init_suite_crypt_mpi(void) |
|---|
| 18 |
{ |
|---|
| 19 |
static char keydetails[MAXBUF+1]; |
|---|
| 20 |
int fd=0; |
|---|
| 21 |
char cmd[MAXBUF+1]; |
|---|
| 22 |
char *rsa_nopass="Key-Type: RSA\nKey-Usage: encrypt, sign\nName-Real: Alpha\nName-Comment: RSA, no passphrase\nName-Email: alpha@test.com\nKey-Length: 1024\n"; |
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
if (!mktmpdir()) |
|---|
| 26 |
return 1; |
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
snprintf(keydetails,MAXBUF,"%s/%s",dir,"keydetails.alpha"); |
|---|
| 33 |
|
|---|
| 34 |
if ((fd=open(keydetails,O_WRONLY | O_CREAT | O_EXCL, 0600))<0) |
|---|
| 35 |
{ |
|---|
| 36 |
fprintf(stderr,"Can't create key details\n"); |
|---|
| 37 |
return 1; |
|---|
| 38 |
} |
|---|
| 39 |
|
|---|
| 40 |
write(fd,rsa_nopass,strlen(rsa_nopass)); |
|---|
| 41 |
close(fd); |
|---|
| 42 |
|
|---|
| 43 |
snprintf(cmd,MAXBUF,"gpg --quiet --gen-key --expert --homedir=%s --batch %s",dir,keydetails); |
|---|
| 44 |
system(cmd); |
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
ops_init(); |
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
snprintf(pubring,MAXBUF,"%s/pubring.gpg", dir); |
|---|
| 51 |
ops_keyring_read(&pub_keyring,pubring); |
|---|
| 52 |
|
|---|
| 53 |
snprintf(secring,MAXBUF,"%s/secring.gpg", dir); |
|---|
| 54 |
ops_keyring_read(&sec_keyring,secring); |
|---|
| 55 |
|
|---|
| 56 |
char keyid[]="Alpha (RSA, no passphrase) <alpha@test.com>"; |
|---|
| 57 |
pubkey=ops_keyring_find_key_by_userid(&pub_keyring,keyid); |
|---|
| 58 |
seckey=ops_keyring_find_key_by_userid(&sec_keyring,keyid); |
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 |
return 0; |
|---|
| 62 |
} |
|---|
| 63 |
|
|---|
| 64 |
int clean_suite_crypt_mpi(void) |
|---|
| 65 |
{ |
|---|
| 66 |
char cmd[MAXBUF+1]; |
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 |
ops_keyring_free(&pub_keyring); |
|---|
| 71 |
ops_keyring_free(&sec_keyring); |
|---|
| 72 |
ops_finish(); |
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 |
snprintf(cmd,MAXBUF,"rm -rf %s", dir); |
|---|
| 76 |
if (system(cmd)) |
|---|
| 77 |
{ |
|---|
| 78 |
perror("Can't delete test directory "); |
|---|
| 79 |
return 1; |
|---|
| 80 |
} |
|---|
| 81 |
|
|---|
| 82 |
return 0; |
|---|
| 83 |
} |
|---|
| 84 |
|
|---|
| 85 |
void test_crypt_mpi(void) |
|---|
| 86 |
{ |
|---|
| 87 |
#define BSZ (256/8+1+2) |
|---|
| 88 |
|
|---|
| 89 |
unsigned char in[BSZ]; |
|---|
| 90 |
unsigned char out[BSZ]; |
|---|
| 91 |
|
|---|
| 92 |
ops_boolean_t rtn; |
|---|
| 93 |
|
|---|
| 94 |
ops_pk_session_key_t *session_key=ops_create_pk_session_key(pubkey); |
|---|
| 95 |
|
|---|
| 96 |
|
|---|
| 97 |
ops_create_m_buf(session_key, in); |
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| 102 |
|
|---|
| 103 |
|
|---|
| 104 |
rtn=ops_decrypt_mpi(out,BSZ, session_key->parameters.rsa.encrypted_m, &seckey->key.skey); |
|---|
| 105 |
|
|---|
| 106 |
|
|---|
| 107 |
|
|---|
| 108 |
|
|---|
| 109 |
|
|---|
| 110 |
|
|---|
| 111 |
CU_ASSERT(memcmp((char *)in,(char *)out,sizeof(in))==0); |
|---|
| 112 |
} |
|---|
| 113 |
|
|---|
| 114 |
CU_pSuite suite_crypt_mpi() |
|---|
| 115 |
{ |
|---|
| 116 |
CU_pSuite suite = NULL; |
|---|
| 117 |
|
|---|
| 118 |
suite = CU_add_suite("Crypt MPI Suite", init_suite_crypt_mpi, clean_suite_crypt_mpi); |
|---|
| 119 |
if (!suite) |
|---|
| 120 |
return NULL; |
|---|
| 121 |
|
|---|
| 122 |
|
|---|
| 123 |
|
|---|
| 124 |
if (NULL == CU_add_test(suite, "encrypt_mpi, then decrypt_mpi", test_crypt_mpi)) |
|---|
| 125 |
return NULL; |
|---|
| 126 |
|
|---|
| 127 |
return suite; |
|---|
| 128 |
} |
|---|