root/openpgpsdk/trunk/src/advanced/adv_crypto.c

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

RSA encryption produces packets which can be decrypted by GPG.
MDC error still to fix.
Some refactoring to re-use common code in tests.

Line 
1 #include <openpgpsdk/crypto.h>
2 #include <openpgpsdk/random.h>
3
4 #include <assert.h>
5 #include <string.h>
6
7 #include <openpgpsdk/final.h>
8
9 int ops_decrypt_and_unencode_mpi(unsigned char *buf,unsigned buflen,const BIGNUM *encmpi,
10                     const ops_secret_key_t *skey)
11     {
12     unsigned char encmpibuf[8192];
13     unsigned char mpibuf[8192];
14     unsigned mpisize;
15     int n;
16     int i;
17
18     mpisize=BN_num_bytes(encmpi);
19     /* MPI can't be more than 65,536 */
20     assert(mpisize <= sizeof encmpibuf);
21     BN_bn2bin(encmpi,encmpibuf);
22
23     assert(skey->public_key.algorithm == OPS_PKA_RSA);
24
25     /*
26     fprintf(stderr,"\nDECRYPTING\n");
27     fprintf(stderr,"encrypted data     : ");
28     for (i=0; i<16; i++)
29         fprintf(stderr,"%2x ", encmpibuf[i]);
30     fprintf(stderr,"\n");
31     */
32
33     n=ops_rsa_private_decrypt(mpibuf,encmpibuf,(BN_num_bits(encmpi)+7)/8,
34                               &skey->key.rsa,&skey->public_key.key.rsa);
35     assert(n!=-1);
36
37     /*
38     fprintf(stderr,"decrypted encoded m buf     : ");
39     for (i=0; i<16; i++)
40         fprintf(stderr,"%2x ", mpibuf[i]);
41     fprintf(stderr,"\n");
42     */
43
44     if(n <= 0)
45         return -1;
46
47     /*
48     printf(" decrypt=%d ",n);
49     hexdump(mpibuf,n);
50     printf("\n");
51     */
52
53     // Decode EME-PKCS1_V1_5 (RFC 2437).
54
55     if(mpibuf[0] != 0 || mpibuf[1] != 2)
56         return ops_false;
57
58     // Skip the random bytes.
59     for(i=2 ; i < n && mpibuf[i] ; ++i)
60         ;
61
62     if(i == n || i < 10)
63         return ops_false;
64
65     // Skip the zero
66     ++i;
67
68     // this is the unencoded m buf
69     if((unsigned)(n-i) <= buflen)
70         memcpy(buf,mpibuf+i,n-i);
71
72     /*
73     printf("unencoded m buf:\n");
74     int j;
75     for (j=0; j<n-i; j++)
76         printf("%2x ",buf[j]);
77     printf("\n");
78     */
79
80     return n-i;
81     }
82
83 ops_boolean_t ops_encrypt_mpi(const unsigned char *encoded_m_buf,
84                               const size_t sz_encoded_m_buf,
85                               const ops_public_key_t *pkey,
86                               ops_pk_session_key_parameters_t *skp)
87     {
88     assert(sz_encoded_m_buf==(size_t) BN_num_bytes(pkey->key.rsa.n));
89
90     unsigned char encmpibuf[8192];
91     int n=0;
92 #ifdef XXX
93     unsigned char EM[8192];
94     int k;
95     unsigned i;
96
97     // implementation of EME-PKCS1-v1_5-ENCODE, as defined in OpenPGP RFC
98     
99     assert(pkey->algorithm == OPS_PKA_RSA);
100
101     k=BN_num_bytes(pkey->key.rsa.n);
102     /*
103     printf("k=%d (length in octets of key modulus)\n",k);
104     printf("mLen=%d\n",mLen);
105     */
106     assert(mLen <= k-11);
107     if (mLen > k-11)
108         {
109         fprintf(stderr,"message too long\n");
110         return false;
111         }
112
113     // output will be written to ??
114
115     // these two bytes defined by RFC
116     EM[0]=0x00;
117     EM[1]=0x02;
118
119     // add non-zero random bytes of length k - mLen -3
120     for(i=2 ; i < k-mLen-1 ; ++i)
121         do
122             ops_random(EM+i, 1);
123         while(EM[i] == 0);
124
125     assert (i >= 8+2);
126
127     EM[i++]=0;
128
129     memcpy(EM+i, M, mLen);
130    
131     /*
132     int i=0;
133     fprintf(stderr,"Encoded Message: \n");
134     for (i=0; i<mLen; i++)
135         fprintf(stderr,"%2x ", EM[i]);
136     fprintf(stderr,"\n");
137     */
138
139 #endif
140     n=ops_rsa_public_encrypt(encmpibuf, encoded_m_buf, sz_encoded_m_buf, &pkey->key.rsa);
141     assert(n!=-1);
142
143     if(n <= 0)
144         return ops_false;
145
146     skp->rsa.encrypted_m=BN_bin2bn(encmpibuf, n, NULL);
147
148     /*
149     fprintf(stderr,"encrypted mpi buf     : ");
150     int i;
151     for (i=0; i<16; i++)
152         fprintf(stderr,"%2x ", encmpibuf[i]);
153     fprintf(stderr,"\n");
154     */
155
156     return ops_true;
157     }
Note: See TracBrowser for help on using the browser.