-
Notifications
You must be signed in to change notification settings - Fork 1
/
rsa2rsapubickey_test.cpp
171 lines (136 loc) · 3.68 KB
/
rsa2rsapubickey_test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include <stdio.h>
#include "rsa/rsa.h"
#include "rsa/x509.h"
#include "mincrypt/rsa.h"
extern int RSA_to_RSAPublicKey(rsa_context *rsa, RSAPublicKey *pkey);
//32 bit limb
uint32_t mpi_get_word(mpi * x){
// 4 bytes
if (mpi_size(x) > 4)
return 0xffffffff;
else
return x->p[0];
//return 0;
}
/* Convert OpenSSL RSA public key to android pre-computed RSAPublicKey format */
int RSA_to_RSAPublicKey(rsa_context *rsa, RSAPublicKey *pkey)
{
int ret = 1;
unsigned int i;
mpi r32;
mpi rr;
mpi r;
mpi rem;
mpi n;
mpi n0inv;
mpi divid;
mpi_init(&r32);
mpi_init(&rr);
mpi_init(&r);
mpi_init(&rem);
mpi_init(&n);
mpi_init(&n0inv);
if(rsa->len != RSANUMBYTES){
ret = 0;
goto out;
}
mpi_set_bit(&r32, 32, 1);
mpi_copy(&n, &rsa->N);
//mpi_lset(&r, 1);
//mpi_shift_l(&r, rsa->N.n *32);
mpi_set_bit(&r, RSANUMWORDS * 32, 1);
mpi_lset(&rr, 1 );
mpi_shift_l(&rr, rsa->N.n * 2 * 32);
mpi_mod_mpi(&rr, &rr, &rsa->N);
mpi_div_mpi(NULL, &rem ,&n, &r32);
mpi_inv_mod(&n0inv, &rem, &r32);
pkey->len = RSANUMWORDS;
pkey->n0inv = 0 - mpi_get_word(&n0inv);
for (i = 0; i < RSANUMWORDS; i++) {
mpi_div_mpi(&divid, &rem, &rr, &r32);
pkey->rr[i] = mpi_get_word(&rem);
mpi_copy(&rr, &divid);
mpi_div_mpi(&divid, &rem, &n, &r32);
pkey->n[i] = mpi_get_word(&rem);
mpi_copy(&n, &divid);
}
pkey->exponent = mpi_get_word(&rsa->E);
out:
mpi_free(&r32);
mpi_free(&rr);
mpi_free(&r);
mpi_free(&rem);
mpi_free(&n);
mpi_free(&n0inv);
return ret;
}
static void dumpKey(RSAPublicKey *pkey){
int version=1;
if(pkey->exponent == 3){
version = 1;
}else if(pkey->exponent == 65537){
version = 2;
}else{
printf("unknown public key exponent\n");
return;
}
if(pkey->len != RSANUMWORDS){
printf("unknown key length\n");
return;
}
if(version > 1){
printf("v%d ",version);
}
printf("{%d,", RSANUMWORDS);
printf("0x%x,{", pkey->n0inv);
for(int i = 0; i < RSANUMWORDS; i++){
printf("%u", pkey->n[i]);
if(i < RSANUMWORDS - 1)
printf(",");
}
printf("},{");
for(int i = 0; i < RSANUMWORDS; i++){
printf("%u", pkey->rr[i]);
if(i < RSANUMWORDS - 1)
printf(",");
}
printf("}}\n");
}
int main(int argc, char **argv){
if(argc != 2){
printf("Usage: rsapublickey_test pem_public_key_file");
return -1;
}
FILE *file = fopen(argv[1], "r");
if(file == NULL){
printf("cannot open %s\n", argv[1]);
return -1;
}
char envval[1024]={0};
int envvallen=sizeof(envval);
int readed = 0;
RSAPublicKey pubkey;
rsa_context rsa;
fseek(file, 0, SEEK_END);
int size = ftell(file);
fseek(file, 0, SEEK_SET);
if(size > envvallen){
printf("file size too large\n");
goto END;
}
readed = fread(envval, 1, size, file);
if(readed != size){
printf("read error %d, size = %d\n", readed, size);
goto END;
}
rsa_init(&rsa, RSA_PKCS_V15, 0);
if(x509parse_public_key(&rsa, (unsigned char*)envval, envvallen) != 0){
rsa_free(&rsa);
return NULL;
}
RSA_to_RSAPublicKey(&rsa, &pubkey);
rsa_free(&rsa);
dumpKey(&pubkey);
END:
fclose(file);
}