Assembly

Course
Time
13 hours 15 minutes
Difficulty
Beginner
CEU/CPE
14

Video Transcription

00:03
Hello. This is Dr Miller, and this is Episode 15.3 of Assembly.
00:08
Today we're gonna talk about cross platform es and then the A s interest, intrinsic six and their implementation.
00:15
And then we'll disassemble these intrinsic stand look at them.
00:19
So cross platform A s.
00:23
So if you go out there and google a tiny A s library or one that is very small, you can find a cross platform see version that somebody has written.
00:34
It's called Tiny A S and C, and I put a get her blink here to this person's code. And so one of the things that they do is they implement a lot of the different a AES encryption libraries, and it's it's pretty easy to go through them and sort of read
00:49
how they work. And we've got a couple different ones here. So, for example, there's functions to initialize the context of the A s library.
00:58
Um, and then there is a couple different ways of encrypting. So we have Elektronik code book or E C B. And then we have cipher block Cheney or CBC.
01:07
And if you go and read on those, you'll learn about why, um Elektronik code book is useful and when you would want to use it and then when cipher block chaining is useful And when you want to use that,
01:17
um and so we have both the encrypt and the decrypt functions that we can use inside of there.
01:23
And so one of the things that you'll see and if you go in Google about a yes, so a s uses the Rindel soft cipher defines this s box. And so that s boxes a bunch of fights
01:36
that are used for substitution ins that we can have when we have before word one and we ever in verse one.
01:42
So when you go through and look at the A s code, you'll end up seeing all of these bites. So zero x 63 So this is defining what all those s boxes are. And so, in order to use it,
01:53
the programmer has to define all of these bites so that the encryption when they implement it, is going to work properly.
02:01
And so, for example, we'll see here. This is another function. Mixed columns, right, which we've talked about before, a little bit, but what it's going to do is it's going to mix these columns based on how a yes says that they're supposed to
02:14
be mixed, right? And so we've got a bunch of different indexes. Um, and you got to make sure that you get that right for the different columns inside of the state matrix.
02:23
Another example is the shift rose again in a s dot C and you can go and look at the source code. But this is shifting those rose in two different locations
02:34
and we can see that,
02:36
um, again, it involves quite a bit of code, right in comparison to what we'll look at in the next one.
02:43
Um, the one benefit of the of the previous one is that it's cross platform. So if you're architecture doesn't support these A s and trick six, then you can use it.
02:53
Um, but if you do have hardware support, then you can use the A yes, intrinsic implementation, and there are a couple of different versions of it. So this is just one of the implementations that I found
03:04
again. There's some code on get hub, and it supports again the initialization of the or key expansion a minute sports. The in Krypton decrypt. And this is just an 128 bit A s implementation.
03:17
Again, there's additional ones that can be implemented, and there's are implemented in that cross platform one.
03:25
So a couple of things we see the W mm intrinsic. So that's going to define a lot of different memory access operations.
03:34
Um, that we might want to use a swell as some other raw assembly instructions.
03:39
And so when you're looking in here and you see a type that is not your normal type, that's because they're using the W. M. M
03:46
um extension in order to implement those a yes intrinsic.
03:52
So here again, we're going to see these are basically raw assembly command. So it's gonna take Z code. It's going to generate as close to assembly as it can. It takes arguments. And so then it implements that very directly into our assembly code, and we'll see how that 11 correspondents works.
04:13
But we here have some macro. So this is the key round, and this is for encryption.
04:17
And so we can see the key round is actually this pound of fine here on what pound to find means is that it's basically going to replace, um, this code with all of this code. And so, for every key round, we end up taking the key at I and we have our round. And then we do that
04:35
assist. And then there's some shuffle, some X sores,
04:40
Um, and then where we do one more X or at the end, right. And so, for each one of these rounds, it's going to expand to one of these.
04:46
And then this down here is for the decryption, so it actually sets up both of them at the same time.
04:51
So when we get to the encrypt function again, this is in C. But this is the M. M. A s
04:59
encrypt. Right we see are we see a couple loads and an explorer, and then we have our encrypts for all of the rounds. And then we have the A s encrypt last at the very end, right? And so this is the very raw implementation of that. Using those a yes intrinsic sis, if we notice we didn't, we're not going to see
05:17
that s box, right? So that that is sort of built in to the processor.
05:21
So it knows what that substitution boxes when it's gonna go on, Do encryption.
05:26
Um, and it's just implemented. Is one instruction instead of the whole bunch of instructions that we saw inside of the sea version,
05:32
I'm decryption is gonna be similar, right? So, again, this is where it sets up the inverse key for decryption. So in this person's implementation, where we have it all at once, So we generate the, um,
05:46
key expansion, right? And then we do the inverse, right? So that's that inverse mixed columns I am. See, that's in there. So that's gonna raw get created for us directly an assembly. And then it's able to use the decrypt with the key at these different positions. Again, we can see where those get loaded,
06:04
um, and then the encrypt or decrypt last uses K zero so that
06:09
initial one that gets set is used on the last one. And so we have again The intrinsic six were able to use those in order to very directly access the hardware in order to do these operations, and you'll see that a lot with
06:24
very specific extensions that people want to use inside of sea in order to speed up their code.
06:31
And so now we can take that a yes. Library and weaken basically disassemble it so we can see what the assembly looks like.
06:39
So, for example, we can see each one of these operations ends up basically a 1 to 1 correspondence between,
06:46
For example, a S key. Gen assist the number one right. That is this substitution right here. And it's got our round, which are round is one right, two for eight.
06:58
We can all see those on there, and then we can see that we have the shuffle operation and then we can see we have the X or operations and there's a couple loads inside of there. And so again, it's a very direct correspondence. If you would were to take
07:13
the sea version and decrypted, clearly, it's not gonna be able to use the A s libraries, and additionally, it's going to do all of that math for encryption inside of the hardware. And so,
07:25
at the end of the day, it's going to take up more memory and it's gonna be slower.
07:30
And here we can see the encryption. So again, we've got some load and X or and then we've got our encrypt. We've gotta invert columns in there, so we got a couple extra operations, but it's directly corresponding. We got our A s. Encrypt is going to directly correspond to that,
07:47
and we can see our XMM registers in there and the compilers managing all of those forests.
07:54
But if you're disassembling some code and you see the V A s encrypt or V A s decrypt right, you know that they're going to use a yes for their encryption mechanism.
08:05
So today we talked about a cross platform A s, and then sort of compared that with the implementation that can leverage the A s and trimmed six. And then we disassemble it to sort of see that there was a direct correspondence between the intrinsic still finding that W mm library and
08:22
the assembly code that we will get generated for that.
08:28
So what are the benefits of using A S and I
08:33
So we have reduced code size and then we'll have faster execution.
08:37
If you have questions, you can email me Miller. I'm Jay. You and Kate. I e d. And you can find me on Twitter at Milhouse 30

Up Next

Assembly

This course will provide background and information related to programming in assembly. Assembly is the lowest level programming language which is useful in reverse engineering and malware analysis.

Instructed By

Instructor Profile Image
Matthew Miller
Assistant Professor at the University of Nebraska at Kearney
Instructor