root/openpgpsdk/trunk/tests/test_crypt_mpi.c

Revision 538 (checked in by rachel, 5 years ago)

Refactored test code to produce high-level function ops_encrypt_file.
Created high-level function ops_sign_buf_as_cleartext for Ben@Nominet.
Change use of MAXBUF in snprintf to sizeof buf, as suggested by BenL.
Added in new tests to do.
Minor cosmetic changes.

Line 
1 #include "CUnit/Basic.h"
2
3 #include <openssl/cast.h>
4
5 #include "tests.h"
6 #include "openpgpsdk/types.h"
7 #include "openpgpsdk/keyring.h"
8 #include "../src/advanced/keyring_local.h"
9 #include "openpgpsdk/packet.h"
10 #include "openpgpsdk/create.h"
11
12 //static char secring[MAXBUF+1];
13 //static char pubring[MAXBUF+1];
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 #ifdef XXX
20     static char keydetails[MAXBUF+1];
21     int fd=0;
22     char cmd[MAXBUF+1];
23     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";
24    
25     // Create temp directory
26     if (!mktmpdir())
27         return 1;
28
29     /*
30      * Create a RSA keypair with no passphrase
31      */
32
33     snprintf(keydetails,sizeof keydetails,"%s/%s",dir,"keydetails.alpha");
34
35     if ((fd=open(keydetails,O_WRONLY | O_CREAT | O_EXCL | O_BINARY, 0600))<0)
36         {
37         fprintf(stderr,"Can't create key details\n");
38         return 1;
39         }
40
41     write(fd,rsa_nopass,strlen(rsa_nopass));
42     close(fd);
43
44     snprintf(cmd,sizeof cmd,"gpg --quiet --no-tty --homedir=%s --gen-key --expert --batch %s",dir,keydetails);
45     system(cmd);
46
47     // Initialise OPS
48     ops_init();
49
50     // read keyrings
51     snprintf(pubring,sizeof pubring,"%s/pubring.gpg", dir);
52     ops_keyring_read(&pub_keyring,pubring);
53
54     snprintf(secring,sizeof secring,"%s/secring.gpg", dir);
55     ops_keyring_read(&sec_keyring,secring);
56
57     char keyid[]="Alpha (RSA, no passphrase) <alpha@test.com>";
58 #endif
59     pubkey=ops_keyring_find_key_by_userid(&pub_keyring,alpha_user_id);
60     //    seckey=ops_keyring_find_key_by_userid(&sec_keyring,keyid);
61     seckey=ops_keyring_find_key_by_userid(&sec_keyring,alpha_user_id);
62
63     // Return success
64     return 0;
65     }
66
67 int clean_suite_crypt_mpi(void)
68     {
69        
70 #ifdef XXX
71     char cmd[MAXBUF+1];
72     /* Close OPS */
73    
74     ops_keyring_free(&pub_keyring);
75     ops_keyring_free(&sec_keyring);
76 #endif
77
78     ops_finish();
79
80 #ifdef XXX
81     /* Remove test dir and files */
82     snprintf(cmd,sizeof cmd,"rm -rf %s", dir);
83     if (system(cmd))
84         {
85         perror("Can't delete test directory ");
86         return 1;
87         }
88 #endif
89     
90     reset_vars();
91
92     return 0;
93     }
94
95 void test_crypt_mpi(void)
96     {
97     // hardcoded using CAST
98 #define BSZ (CAST_KEY_LENGTH+1+2)
99
100     unsigned char in[BSZ];
101     unsigned char out[BSZ];
102
103     ops_boolean_t rtn;
104    
105     ops_pk_session_key_t *encrypted_pk_session_key=NULL;
106
107     encrypted_pk_session_key=ops_create_pk_session_key(pubkey);
108
109     // recreate what was encrypted
110     //    ops_create_m_buf(session_key, in);
111
112     //    CU_ASSERT(session_key);
113
114     // the encrypted_mpi is now in session_key->parameters.rsa.encrypted_m
115
116     // decrypt it
117     rtn=ops_decrypt_and_unencode_mpi(out,BSZ, encrypted_pk_session_key->parameters.rsa.encrypted_m, &seckey->key.skey);
118
119     // [0] is the symmetric algorithm
120     // [body] is the session key
121     // [last two] is the checksum
122
123     // is it the same?
124     CU_ASSERT(memcmp((char *)in,(char *)out,sizeof(in))==0);
125     }
126
127 CU_pSuite suite_crypt_mpi()
128     {
129     CU_pSuite suite = NULL;
130
131     suite = CU_add_suite("Crypt MPI Suite", init_suite_crypt_mpi, clean_suite_crypt_mpi);
132     if (!suite)
133             return NULL;
134
135     // add tests to suite
136     
137     if (NULL == CU_add_test(suite, "encrypt_mpi, then decrypt_mpi", test_crypt_mpi))
138             return NULL;
139    
140     return suite;
141     }
Note: See TracBrowser for help on using the browser.