Skip to content

Commit

Permalink
Automatically calculate RSA a, b, c, see #11.
Browse files Browse the repository at this point in the history
  • Loading branch information
bungle committed May 29, 2017
1 parent 15ed9f5 commit dd41916
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib/resty/nettle/mpz.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ size_t __gmpz_sizeinbase(const mpz_t op, int base);
char * __gmpz_get_str(char *str, int base, const mpz_t op);
int __gmpz_set_str(mpz_t rop, const char *str, int base);
void __gmpz_set_ui(mpz_t, unsigned long int iv);
int __gmpz_invert(mpz_ptr, mpz_srcptr, mpz_srcptr);
void __gmpz_sub_ui(mpz_ptr, mpz_srcptr, unsigned long int);
void __gmpz_fdiv_r(mpz_ptr, mpz_srcptr, mpz_srcptr);
]]

local ctx = ffi_typeof "mpz_t"
Expand Down Expand Up @@ -60,4 +63,16 @@ function mpz.set(op, value, base)
return true
end

function mpz.invert(rop, op1, op2)
return gmp.__gmpz_invert(rop, op1, op2)
end

function mpz.sub(rop, op1, op2)
gmp.__gmpz_sub_ui(rop, op1, op2)
end

function mpz.div(rop, op1, op2)
gmp.__gmpz_fdiv_r(rop, op1, op2)
end

return mpz
20 changes: 20 additions & 0 deletions lib/resty/nettle/rsa.lua
Original file line number Diff line number Diff line change
Expand Up @@ -106,35 +106,55 @@ function private.new(d, p, q, a, b, c, base)
return nil, err
end
end
local p1
if p then
local ok, err = mpz.set(context[0].p, p, base)
if not ok then
return nil, err
end
if d and not a then
p1 = mpz.new()
mpz.sub(p1, context[0].p, 1)
end
end
local q1
if q then
local ok, err = mpz.set(context[0].q, q, base)
if not ok then
return nil, err
end
if d and not b then
q1 = mpz.new()
mpz.sub(q1, context[0].q, 1)
end
end
if a then
local ok, err = mpz.set(context[0].a, a, base)
if not ok then
return nil, err
end
elseif p1 then
mpz.div(context[0].a, context[0].d, p1)
end
if b then
local ok, err = mpz.set(context[0].b, b, base)
if not ok then
return nil, err
end
elseif q1 then
mpz.div(context[0].b, context[0].d, q1)
end

if c then
local ok, err = mpz.set(context[0].c, c, base)
if not ok then
return nil, err
end
elseif q and p then
local ret = mpz.invert(context[0].c, context[0].q, context[0].p)
if ret == 0 then
ret = mpz.invert(context[0].c, context[0].q, context[0].p)
end
end
if d or p or q or a or b or c then
if hogweed.nettle_rsa_private_key_prepare(context) ~= 1 then
Expand Down
1 change: 1 addition & 0 deletions lib/resty/nettle/types/mpz.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ typedef struct {
int _mp_size;
mp_limb_t *_mp_d;
} __mpz_struct;
typedef const __mpz_struct *mpz_srcptr;
typedef __mpz_struct mpz_t[1];
typedef __mpz_struct *mpz_ptr;
]]

0 comments on commit dd41916

Please sign in to comment.