# SIMD Encryption Example

Video Activity
Join over 3 million cybersecurity professionals advancing their career
or

Course
Time
13 hours 15 minutes
Difficulty
Beginner
CEU/CPE
14
Video Transcription
00:02
Hello. This is Dr Miller, and this is Episode 12.7 of Assembly.
00:07
Today. We're gonna given s I M D example.
00:10
So, yes, I am d example.
00:12
So here's our problem to be solved. So we're going to read a string from the user, and then we're going to read a key from the user, and then we're gonna encrypt that data.
00:24
All right,
00:26
So to start off, we're gonna have a couple different pieces of data here.
00:31
We're gonna have several different four minutes,
00:38
so I have a
00:40
string
00:48
that we're gonna have to formats. One is for reading in a string. Thea other one is for reading in a hex.
00:56
I need a name. These appropriately
01:07
citizens also a string.
01:08
We're also going to allocate some empty space in the BSS.
01:12
So this is blank space, but were allowed to read and write to it.
01:18
I want to find our array. It is just
01:21
space for 64 bites and I dont be used at the end.
01:26
We're also going to use a couple of functions. So, for example, renews can f in print off
01:42
here. We'll get into our main function.
01:47
All right,
01:49
so in order to get things set up. We're going to have to save our registers.
01:56
So we'll set up our stack.
01:59
You are.
02:05
And then we're gonna allocate quite a bit of space a sub s P s p
02:10
just to give ourselves some room to work with.
02:16
Okay, at the end. What we want to do is we want to fix our stacks or sub
02:24
from the frame point of the number four, cause we added to it,
02:29
and then pop
02:31
02:35
So now those were set back up.
02:38
All right,
02:39
So what we're gonna do now is we're going to create a pointer to our memory that we're going to use for the frame pointers so we'll go ahead and sub
02:49
We're saving toe are one will use the frame pointer and we'll subtract 180.
02:54
So this is some number less than this 200 But of course, greater than our four.
03:01
So that's gonna be our pointer. We're putting it in our one because
03:06
in our zero, we're going to have our format,
03:10
and then we'll call scan off.
03:14
So that will scan this string here,
03:16
and it'll store it on the stack, Um,
03:21
with the offset frame pointer.
03:23
Um, plus 180.
03:24
So down the stack. 180 fights.
03:29
So that should save our data there. And then
03:32
what we want to do is we want to figure out how long the string is, so you can go and look up documentation on a function called Starlin.
03:38
But we'll go ahead and use that.
03:42
Okay,
03:43
so
03:45
we need to
03:47
store into our zero our frame point or minus or plus 180
03:54
and then we'll call branch of link scan. If, er sorry, stir Lynn.
04:00
So that will tell us how long are string is? So it's going to give us a result inside of our zero, which is our return result.
04:08
And for now, I'm going to copy that into our nine because I may end up using our zero in the future.
04:17
All right, now I'm going to set up a loop. So,
04:19
um, with this percent x up here, I'm gonna have to read each one of these. And so these are gonna be my key, my encryption keys, But I want to read the same number of encryption keys as I have characters in my string, which is why I called the
04:32
sterling.
04:34
So what we can do here is we're going to set up a loop, so move our eight,
04:40
So our aid is gonna be our offset.
04:43
We'll give it the number zero, and we'll increment that each time.
04:46
And we'll just do a compare of our eight and are sorry or eight
04:53
and our nine and see if they're equal.
04:59
And we're gonna jump to this label down here that we're gonna make so
05:02
down here, we're gonna have a label called done reading.
05:08
And we'll do stuff after that
05:10
if we don't. Well, so this is the place where you come back up to so we'll call this top
05:15
and then at the bottom of our loop were going to say brands to top.
05:19
So always jump back up there and then do our comparison.
05:25
All right, so
05:26
we're done reading. But now we need to set up our pointers. We're going again. Call scan F
05:30
and so are one is gonna have to be our pointer to our memory. So we're going to
05:35
sub are one
05:39
frame pointer and here we're gonna copy this data into a different location.
05:43
So, listen, was that 1 80? I gave us
05:46
80 bites to go into her at 100.
05:51
All right,
05:53
and then so are offset is stored in our eight. So are a We're gonna increment each time so we could go ahead and to add or eight
06:01
our eight, the number one.
06:03
So our aid is being added each time, and it starts off zeros. We have offset zero.
06:09
But when I have to add that to our pointer in our one, So add are one
06:15
our eight.
06:16
06:18
to get the We've got our correct offset stored in our one. Now, we need our format in r zero,
06:31
okay? And then we can call scan. If
06:38
so, that should allow us to read in. Right. And each time we're gonna loop through because we do this at the bottom.
06:45
And so we'll go ahead and loop through in here and,
06:47
um, read that number of keys that we need for our data.
06:53
All right, so now we're done reading,
06:55
and then we're gonna want to do our exclusive or for our encryption, so we gotta load it into our registers. So
07:02
I'll start sub r zero frame pointer
07:06
So load in need the text key that they entered
07:11
and then we'll do V l D E r
07:15
64.
07:17
07:21
at our zero,
07:24
and then we're gonna use,
07:26
um, are one
07:29
for the next one.
07:30
So our encryption key is at 100
07:34
the l d r
07:36
64. Well, hadn't do D one,
07:41
and then we're going to get the data at
07:43
are one.
07:46
Okay, so now we can do our exclusive. Or so the EU or
07:53
D two d
07:55
Doesn't matter the order d one and d zero.
08:00
All right, so now we've encrypted it, but now we need to save it so that we can print it off.
08:05
That's what we have our array for.
08:11
So when do L d r r one
08:16
get the address of our array
08:20
and then
08:22
08:24
um, do RVs tr
08:28
We'll save d two at the location pointing to an r one slot Syria that we loaded in.
08:35
So I should save our data. We also will want to move a zero in there so
08:41
move or three
08:46
the number zero and then I'll do ah str
08:52
are three into.
08:54
We'll do our one. So we're just going to read in a maximum of eight bytes is
09:01
what we're doing just for this example, so that we can fit it in.
09:05
So we'll set by eight automatically to
09:09
a zero when we'll actually set a bunch of lights here. So we'll set three by four, bites to zero.
09:16
All right, so now we're ready to print. LD r r one
09:20
array LD R R zero
09:26
Our format that we want to use the print and then
09:31
print F
09:39
All right, so
09:43
couple of typos here. So I got my ask. Yes.
09:52
And then I think I forgot my colon after my label here.
10:01
All right,
10:03
so if I enter
10:05
Matt,
10:07
um,
10:13
I'll just do Matt to start, and then my encryption key is zero x won t rex three.
10:22
And you can type these one at a time. We'll do him here in a second,
10:28
so I got l B. P s.
10:31
So now if I run it again and type l B. P s
10:37
and then I enter my encryption key one
10:41
three
10:43
for
10:46
seven,
10:48
I'll get mad. So here I would. My encryption key is right here. And so I took Matt. I encrypted it. I got this value here
10:56
and then the only way to decrypt it is to know what the
11:00
key here is. And so I go ahead and
11:03
type that in again, and I get my original text back.
11:09
So today we used s I m d to do example of doing encryption. Using, exclusive or
11:18
looking forward, we'll talk about some mode and then conditional execution.
11:22
If you have questions, you can email me, Miller MJ at you and Kate I e to you, and you can find me on Twitter at Maha sturdy.
Up Next