diff options
| author | William Ahern <william@25thandclement.com> | 2016-12-14 14:22:15 -0800 |
|---|---|---|
| committer | William Ahern <william@25thandclement.com> | 2016-12-14 14:22:15 -0800 |
| commit | ae16dd4dd147404fc73e34ab3263d50d93a57f0b (patch) | |
| tree | 2d25a428a5aa645f0f7d9557c094dbf3aa7f652b | |
| parent | 670a1123a04deb4195e137a8a504b34d18e2fff8 (diff) | |
| download | luaossl-ae16dd4dd147404fc73e34ab3263d50d93a57f0b.tar.gz luaossl-ae16dd4dd147404fc73e34ab3263d50d93a57f0b.tar.bz2 luaossl-ae16dd4dd147404fc73e34ab3263d50d93a57f0b.zip | |
for issue #82 split bn_prepops into explicit unary and binary operator modes (bn_prepuop and bn_prepbop), and make more generic by working at top of stack
| -rw-r--r-- | src/openssl.c | 62 |
1 files changed, 42 insertions, 20 deletions
diff --git a/src/openssl.c b/src/openssl.c index 112a4c8..fa7dd79 100644 --- a/src/openssl.c +++ b/src/openssl.c | |||
| @@ -2563,26 +2563,39 @@ static BIGNUM *(checkbig)(lua_State *L, int index, _Bool *lvalue) { | |||
| 2563 | } /* checkbig() */ | 2563 | } /* checkbig() */ |
| 2564 | 2564 | ||
| 2565 | 2565 | ||
| 2566 | static void bn_prepops(lua_State *L, BIGNUM **r, BIGNUM **a, BIGNUM **b, _Bool commute) { | 2566 | /* prepare number at top of stack for unary operation, and push result object onto stack */ |
| 2567 | static void bn_prepuop(lua_State *L, BIGNUM **r, BIGNUM **a, _Bool commute) { | ||
| 2567 | _Bool lvalue = 1; | 2568 | _Bool lvalue = 1; |
| 2568 | 2569 | ||
| 2569 | lua_settop(L, 2); /* a, b */ | 2570 | *a = checkbig(L, -1, &lvalue); |
| 2570 | 2571 | ||
| 2571 | *a = checkbig(L, 1, &lvalue); | 2572 | if (!lvalue && commute) { |
| 2573 | lua_pushvalue(L, -1); | ||
| 2574 | } else { | ||
| 2575 | bn_push(L); | ||
| 2576 | } | ||
| 2577 | |||
| 2578 | *r = *(BIGNUM **)lua_touserdata(L, -1); | ||
| 2579 | } /* bn_prepuop() */ | ||
| 2572 | 2580 | ||
| 2573 | if (!lvalue && commute) | ||
| 2574 | lua_pushvalue(L, 1); | ||
| 2575 | 2581 | ||
| 2576 | *b = checkbig(L, 2, &lvalue); | 2582 | /* prepare numbers at top of stack for binary operation, and push result object onto stack */ |
| 2583 | static void bn_prepbop(lua_State *L, BIGNUM **r, BIGNUM **a, BIGNUM **b, _Bool commute) { | ||
| 2584 | _Bool a_lvalue, b_lvalue; | ||
| 2577 | 2585 | ||
| 2578 | if (!lvalue && commute && lua_gettop(L) < 3) | 2586 | *a = checkbig(L, -2, &a_lvalue); |
| 2579 | lua_pushvalue(L, 2); | 2587 | *b = checkbig(L, -1, &b_lvalue); |
| 2580 | 2588 | ||
| 2581 | if (lua_gettop(L) < 3) | 2589 | if (commute && !a_lvalue) { |
| 2590 | lua_pushvalue(L, -2); | ||
| 2591 | } else if (commute && !b_lvalue) { | ||
| 2592 | lua_pushvalue(L, -1); | ||
| 2593 | } else { | ||
| 2582 | bn_push(L); | 2594 | bn_push(L); |
| 2595 | } | ||
| 2583 | 2596 | ||
| 2584 | *r = *(BIGNUM **)lua_touserdata(L, 3); | 2597 | *r = *(BIGNUM **)lua_touserdata(L, -1); |
| 2585 | } /* bn_prepops() */ | 2598 | } /* bn_prepbop() */ |
| 2586 | 2599 | ||
| 2587 | 2600 | ||
| 2588 | static int ctx__gc(lua_State *L) { | 2601 | static int ctx__gc(lua_State *L) { |
| @@ -2639,7 +2652,8 @@ static int bn_toBinary(lua_State *L) { | |||
| 2639 | static int bn__add(lua_State *L) { | 2652 | static int bn__add(lua_State *L) { |
| 2640 | BIGNUM *r, *a, *b; | 2653 | BIGNUM *r, *a, *b; |
| 2641 | 2654 | ||
| 2642 | bn_prepops(L, &r, &a, &b, 1); | 2655 | lua_settop(L, 2); |
| 2656 | bn_prepbop(L, &r, &a, &b, 1); | ||
| 2643 | 2657 | ||
| 2644 | if (!BN_add(r, a, b)) | 2658 | if (!BN_add(r, a, b)) |
| 2645 | return auxL_error(L, auxL_EOPENSSL, "bignum:__add"); | 2659 | return auxL_error(L, auxL_EOPENSSL, "bignum:__add"); |
| @@ -2651,7 +2665,8 @@ static int bn__add(lua_State *L) { | |||
| 2651 | static int bn__sub(lua_State *L) { | 2665 | static int bn__sub(lua_State *L) { |
| 2652 | BIGNUM *r, *a, *b; | 2666 | BIGNUM *r, *a, *b; |
| 2653 | 2667 | ||
| 2654 | bn_prepops(L, &r, &a, &b, 0); | 2668 | lua_settop(L, 2); |
| 2669 | bn_prepbop(L, &r, &a, &b, 0); | ||
| 2655 | 2670 | ||
| 2656 | if (!BN_sub(r, a, b)) | 2671 | if (!BN_sub(r, a, b)) |
| 2657 | return auxL_error(L, auxL_EOPENSSL, "bignum:__sub"); | 2672 | return auxL_error(L, auxL_EOPENSSL, "bignum:__sub"); |
| @@ -2663,7 +2678,8 @@ static int bn__sub(lua_State *L) { | |||
| 2663 | static int bn__mul(lua_State *L) { | 2678 | static int bn__mul(lua_State *L) { |
| 2664 | BIGNUM *r, *a, *b; | 2679 | BIGNUM *r, *a, *b; |
| 2665 | 2680 | ||
| 2666 | bn_prepops(L, &r, &a, &b, 1); | 2681 | lua_settop(L, 2); |
| 2682 | bn_prepbop(L, &r, &a, &b, 1); | ||
| 2667 | 2683 | ||
| 2668 | if (!BN_mul(r, a, b, getctx(L))) | 2684 | if (!BN_mul(r, a, b, getctx(L))) |
| 2669 | return auxL_error(L, auxL_EOPENSSL, "bignum:__mul"); | 2685 | return auxL_error(L, auxL_EOPENSSL, "bignum:__mul"); |
| @@ -2675,7 +2691,8 @@ static int bn__mul(lua_State *L) { | |||
| 2675 | static int bn_sqr(lua_State *L) { | 2691 | static int bn_sqr(lua_State *L) { |
| 2676 | BIGNUM *r, *a; | 2692 | BIGNUM *r, *a; |
| 2677 | 2693 | ||
| 2678 | bn_prepops(L, &r, &a, NULL, 1); | 2694 | lua_settop(L, 1); |
| 2695 | bn_prepuop(L, &r, &a, 1); | ||
| 2679 | 2696 | ||
| 2680 | if (!BN_sqr(r, a, getctx(L))) | 2697 | if (!BN_sqr(r, a, getctx(L))) |
| 2681 | return auxL_error(L, auxL_EOPENSSL, "bignum:sqr"); | 2698 | return auxL_error(L, auxL_EOPENSSL, "bignum:sqr"); |
| @@ -2687,7 +2704,8 @@ static int bn_sqr(lua_State *L) { | |||
| 2687 | static int bn__idiv(lua_State *L) { | 2704 | static int bn__idiv(lua_State *L) { |
| 2688 | BIGNUM *dv, *a, *b; | 2705 | BIGNUM *dv, *a, *b; |
| 2689 | 2706 | ||
| 2690 | bn_prepops(L, &dv, &a, &b, 0); | 2707 | lua_settop(L, 2); |
| 2708 | bn_prepbop(L, &dv, &a, &b, 0); | ||
| 2691 | 2709 | ||
| 2692 | if (!BN_div(dv, NULL, a, b, getctx(L))) | 2710 | if (!BN_div(dv, NULL, a, b, getctx(L))) |
| 2693 | return auxL_error(L, auxL_EOPENSSL, "bignum:__idiv"); | 2711 | return auxL_error(L, auxL_EOPENSSL, "bignum:__idiv"); |
| @@ -2699,7 +2717,8 @@ static int bn__idiv(lua_State *L) { | |||
| 2699 | static int bn__mod(lua_State *L) { | 2717 | static int bn__mod(lua_State *L) { |
| 2700 | BIGNUM *r, *a, *b; | 2718 | BIGNUM *r, *a, *b; |
| 2701 | 2719 | ||
| 2702 | bn_prepops(L, &r, &a, &b, 0); | 2720 | lua_settop(L, 2); |
| 2721 | bn_prepbop(L, &r, &a, &b, 0); | ||
| 2703 | 2722 | ||
| 2704 | if (!BN_mod(r, a, b, getctx(L))) | 2723 | if (!BN_mod(r, a, b, getctx(L))) |
| 2705 | return auxL_error(L, auxL_EOPENSSL, "bignum:__mod"); | 2724 | return auxL_error(L, auxL_EOPENSSL, "bignum:__mod"); |
| @@ -2717,7 +2736,8 @@ static int bn__mod(lua_State *L) { | |||
| 2717 | static int bn_nnmod(lua_State *L) { | 2736 | static int bn_nnmod(lua_State *L) { |
| 2718 | BIGNUM *r, *a, *b; | 2737 | BIGNUM *r, *a, *b; |
| 2719 | 2738 | ||
| 2720 | bn_prepops(L, &r, &a, &b, 0); | 2739 | lua_settop(L, 2); |
| 2740 | bn_prepbop(L, &r, &a, &b, 0); | ||
| 2721 | 2741 | ||
| 2722 | if (!BN_nnmod(r, a, b, getctx(L))) | 2742 | if (!BN_nnmod(r, a, b, getctx(L))) |
| 2723 | return auxL_error(L, auxL_EOPENSSL, "bignum:nnmod"); | 2743 | return auxL_error(L, auxL_EOPENSSL, "bignum:nnmod"); |
| @@ -2729,7 +2749,8 @@ static int bn_nnmod(lua_State *L) { | |||
| 2729 | static int bn__pow(lua_State *L) { | 2749 | static int bn__pow(lua_State *L) { |
| 2730 | BIGNUM *r, *a, *b; | 2750 | BIGNUM *r, *a, *b; |
| 2731 | 2751 | ||
| 2732 | bn_prepops(L, &r, &a, &b, 0); | 2752 | lua_settop(L, 2); |
| 2753 | bn_prepbop(L, &r, &a, &b, 0); | ||
| 2733 | 2754 | ||
| 2734 | if (!BN_exp(r, a, b, getctx(L))) | 2755 | if (!BN_exp(r, a, b, getctx(L))) |
| 2735 | return auxL_error(L, auxL_EOPENSSL, "bignum:__pow"); | 2756 | return auxL_error(L, auxL_EOPENSSL, "bignum:__pow"); |
| @@ -2741,7 +2762,8 @@ static int bn__pow(lua_State *L) { | |||
| 2741 | static int bn_gcd(lua_State *L) { | 2762 | static int bn_gcd(lua_State *L) { |
| 2742 | BIGNUM *r, *a, *b; | 2763 | BIGNUM *r, *a, *b; |
| 2743 | 2764 | ||
| 2744 | bn_prepops(L, &r, &a, &b, 1); | 2765 | lua_settop(L, 2); |
| 2766 | bn_prepbop(L, &r, &a, &b, 1); | ||
| 2745 | 2767 | ||
| 2746 | if (!BN_gcd(r, a, b, getctx(L))) | 2768 | if (!BN_gcd(r, a, b, getctx(L))) |
| 2747 | return auxL_error(L, auxL_EOPENSSL, "bignum:gcd"); | 2769 | return auxL_error(L, auxL_EOPENSSL, "bignum:gcd"); |
