root/openpgpsdk/trunk/tests/test_crypt_mpi.c

Revision 474 (checked in by rachel, 6 years ago)

use memcmp rather than strncmp for binary data. doh!
remove old commented out code

Line 
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     // Create temp directory
25     if (!mktmpdir())
26         return 1;
27
28     /*
29      * Create a RSA keypair with no passphrase
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     // Initialise OPS
47     ops_init();
48
49     // read keyrings
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     // Return success
61     return 0;
62     }
63
64 int clean_suite_crypt_mpi(void)
65     {
66     char cmd[MAXBUF+1];
67        
68     /* Close OPS */
69    
70     ops_keyring_free(&pub_keyring);
71     ops_keyring_free(&sec_keyring);
72     ops_finish();
73
74     /* Remove test dir and files */
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     // recreate what was encrypted
97     ops_create_m_buf(session_key, in);
98
99     //    CU_ASSERT(session_key);
100
101     // the encrypted_mpi is now in session_key->parameters.rsa.encrypted_m
102
103     // decrypt it
104     rtn=ops_decrypt_mpi(out,BSZ, session_key->parameters.rsa.encrypted_m, &seckey->key.skey);
105
106     // [0] is the symmetric algorithm
107     // [body] is the session key
108     // [last two] is the checksum
109
110     // is it the same?
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     // add tests to suite
123     
124     if (NULL == CU_add_test(suite, "encrypt_mpi, then decrypt_mpi", test_crypt_mpi))
125             return NULL;
126    
127     return suite;
128     }
Note: See TracBrowser for help on using the browser.