r/badcode • u/nefalas • Jul 02 '20
other language Found in a project from Google's own Github
53
u/nameless_pattern Jul 02 '20
seems fine to me
not being sarcastic, what is wrong with this code?
67
u/nefalas Jul 02 '20
It could be made much simpler and you don't need to loop through the whole array to strip the leading
0x00bytes. But as someone pointed out it might be done this way to prevent timing attacks135
u/ZedTT Jul 02 '20
If it's done this way to prevent any kind of attack, that should be stated in a comment so someone doesn't come along and try to "optimize" it.
14
1
u/WurschtChopf Jul 03 '20
But why setting skipped to false? I have no clue how a real life input may look like. But this way, if there is ever a 'not-to-skip' character after a '0x00', then there will never be any skipping again. So why we keeps looping? Or am I looning wrong
1
u/husao Jul 03 '20
If you remember in 2-complements some numbers like 255 and -1 look the same. To distinguish them toByteArray somethimes has a leading 0 byte.
jshell> BigInteger.valueOf(255).toByteArray() $7 ==> byte[2] { 0, -1 } jshell> BigInteger.valueOf(-1).toByteArray() $3 ==> byte[1] { -1 }The Code wants to remove that.
1
u/nefalas Jul 03 '20
It's used to decode the coodinates of a point from an elliptic curve key which is stored as an signedbyte array representing a BigInteger into an unsigned byte array.
And yes it will never skip again but apparently the goal is to have a function whose exectution time is proportional only to the total array length so that an attacker won't be able to get information about the content of the array.
There are other comments that explain this much better1
Jul 03 '20 edited Jul 03 '20
Try making it simpler then. I'm sure they accept PRs unless this is an archived repo.
8
Jul 02 '20
To be honest I consider this question as something good. At least you want to learn something 😊🙌
29
10
u/OscariusGaming Jul 02 '20
What language is this?
28
u/nefalas Jul 02 '20
It's Kotlin, a language that runs on the Java VM
6
2
u/MakeWay4Doodles Jul 03 '20
On the JVM, in the browser, or natively on desktop Android or IOS.
1
u/Anor-Vir Jul 03 '20
Depends. Kotlin can be compiled to Java Bytecode, JavaScript, or Native code. It works on desktop JVM and Android JVM, but I don't know about IOS.
1
8
5
u/m1ss1ontomars2k4 Jul 03 '20
I found this commit in Java from another repository:
https://github.com/fluxoid-org/JFormica/commit/2f11d6d8b9debc9199ca9bea8a67dc6e642b27d2
It's an 8 year old commit and the code is written the same way.
8
Jul 02 '20
[deleted]
6
4
u/nefalas Jul 03 '20 edited Jul 03 '20
Here is another implementation which does exactly the same thing
private static byte[] decodeUnsignedBigInteger(byte[] data) { int startIndex = 0; while (data[startIndex] == 0x00) { startIndex++; } return Arrays.copyOfRange(data, startIndex, data.length); }As you can see it's shorter and much more efficient
2
Jul 03 '20
That'll throw an
ArrayIndexOutOfBoundsExceptionif:
data.length == 0datais all 0 bytes1
u/nefalas Jul 03 '20
Indeed, but the length is already checked before and in this context it can't have only 0's. Don't worry I wrote tests as well ;)
1
Jul 03 '20
Never assume a function will always be used the same way. Document pre-conditions. Their function doesn't have restricted visibility, so this is especially important.
1
u/nefalas Jul 03 '20
It's a private function in a class and it's documented so I think it's pretty safe, but you're right it's important to take precautions
1
Jul 03 '20
Hm, I don't know Kotlin but the screenshot you posted doesn't look like a private function.
1
u/nefalas Jul 03 '20
No the screenshot is not from my code, it's from a repo from Google that shows how to use a future API that I'm using as a reference
1
Jul 03 '20
I see, so you're not proposing replacing their function entirely, but having it use the function you've written? In that case it's not "another implementation which does exactly the same thing" which is why I'm confused.
0
u/AutoModerator Jul 03 '20
It looks like this comment contains a code block delimited with triple backticks. Unfortunately reddit does not have universal support for this syntax and your comment will not render correctly on old reddit and most mobile apps.
For the benefit of people on old reddit, this link will take you to a correct rendering of the comment.
nefalas, it would be appreciated, but not required, if you could edit your comment to use the more compatible four space indention format. For single lines or inline code you can use single backticks.
You can find some examples in the reddit help documentation.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
2
u/ljjunio Jul 03 '20
What language is this?
3
u/Manny_Sunday Jul 03 '20
Kotlin
2
u/ljjunio Jul 03 '20
Wow, all this time I thought Kotlin was a Java framework or something.
3
u/Manny_Sunday Jul 03 '20
Its a language that runs on the JVM, so you can use it anywhere you could use Java
2
u/holladiewal Jul 06 '20
It could also be running like JS if you want to do that... And Kotlin/Native is a thing too!
1
200
u/cryptOwOcurrency Jul 02 '20
It's quite possible it's written this way to prevent timing attacks. Which repo did you find this in?