Skip to content

Commit aafdec3

Browse files
committed
fix: make rsa_pss int_to_bit_size more efficient
1 parent f9be075 commit aafdec3

File tree

1 file changed

+6
-11
lines changed

1 file changed

+6
-11
lines changed

src/rsa_pss.erl

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
-module(rsa_pss).
1313

1414
-include_lib("public_key/include/public_key.hrl").
15+
-include("include/hb.hrl").
1516

1617
%% API
1718
-export([sign/3]).
@@ -88,7 +89,7 @@ verify({digest, Digest}, DigestType, Signature, PublicKey=#'RSAPublicKey'{modulu
8889
PrivateByteSize = (PublicBitSize + 7) div 8,
8990
PublicByteSize = int_to_byte_size(N),
9091
SignatureSize = byte_size(Signature),
91-
case PublicByteSize =:= SignatureSize of
92+
Ret = case PublicByteSize =:= SignatureSize of
9293
true ->
9394
SignatureNumber = binary:decode_unsigned(Signature, big),
9495
case SignatureNumber >= 0 andalso SignatureNumber < N of
@@ -124,7 +125,8 @@ verify({digest, Digest}, DigestType, Signature, PublicKey=#'RSAPublicKey'{modulu
124125
end;
125126
false ->
126127
false
127-
end.
128+
end,
129+
Ret.
128130

129131
verify_legacy(Message, DigestType, Signature, PublicKey) when is_binary(Message) ->
130132
verify_legacy({digest, crypto:hash(DigestType, Message)}, DigestType, Signature, PublicKey);
@@ -185,16 +187,9 @@ ep(B, #'RSAPublicKey'{modulus=N, publicExponent=E}) ->
185187
crypto:mod_pow(B, E, N).
186188

187189
%% @private
188-
int_to_bit_size(I) ->
189-
int_to_bit_size(I, 0).
190+
int_to_bit_size(I) ->
191+
bit_size(binary:encode_unsigned(I)).
190192

191-
%% @private
192-
int_to_bit_size(0, B) ->
193-
B;
194-
int_to_bit_size(I, B) ->
195-
int_to_bit_size(I bsr 1, B + 1).
196-
197-
%% @private
198193
int_to_byte_size(I) ->
199194
int_to_byte_size(I, 0).
200195

0 commit comments

Comments
 (0)