Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Dmitry Baryshkov
nettle
Commits
27cb6438
Commit
27cb6438
authored
Jan 08, 2018
by
Niels Möller
Browse files
New helper function ctr_fill.
parent
54b2d297
Changes
2
Hide whitespace changes
Inline
Side-by-side
ChangeLog
View file @
27cb6438
...
...
@@ -9,6 +9,8 @@
2018-01-08 Niels Möller <nisse@lysator.liu.se>
* ctr.c (ctr_fill): New function. Use in ctr_crypt.
* ctr.c (ctr_crypt): For in-place operation, increase max buffer
size from 4 blocks to 512 bytes, similarly to CBC and CFB.
Improves in-place aes128 CTR performance by 25% on x86_64.
...
...
ctr.c
View file @
27cb6438
...
...
@@ -48,6 +48,20 @@
/* Don't allocate any more space than this on the stack */
#define CTR_BUFFER_LIMIT 512
#define MIN(a,b) (((a) < (b)) ? (a) : (b))
static
size_t
ctr_fill
(
size_t
block_size
,
uint8_t
*
ctr
,
size_t
length
,
uint8_t
*
buffer
)
{
size_t
i
;
for
(
i
=
0
;
i
+
block_size
<=
length
;
i
+=
block_size
)
{
memcpy
(
buffer
+
i
,
ctr
,
block_size
);
INCREMENT
(
block_size
,
ctr
);
}
return
i
;
}
void
ctr_crypt
(
const
void
*
ctx
,
nettle_cipher_func
*
f
,
size_t
block_size
,
uint8_t
*
ctr
,
...
...
@@ -64,28 +78,19 @@ ctr_crypt(const void *ctx, nettle_cipher_func *f,
}
else
{
size_t
left
;
uint8_t
*
p
;
size_t
filled
=
ctr_fill
(
block_size
,
ctr
,
length
,
dst
);
for
(
p
=
dst
,
left
=
length
;
left
>=
block_size
;
left
-=
block_size
,
p
+=
block_size
)
{
memcpy
(
p
,
ctr
,
block_size
);
INCREMENT
(
block_size
,
ctr
);
}
f
(
ctx
,
length
-
left
,
dst
,
dst
);
memxor
(
dst
,
src
,
length
-
left
);
f
(
ctx
,
filled
,
dst
,
dst
);
memxor
(
dst
,
src
,
filled
);
if
(
left
)
if
(
filled
<
length
)
{
TMP_DECL
(
buffer
,
uint8_t
,
NETTLE_MAX_CIPHER_BLOCK_SIZE
);
TMP_ALLOC
(
buffer
,
block_size
);
f
(
ctx
,
block_size
,
buffer
,
ctr
);
INCREMENT
(
block_size
,
ctr
);
memxor3
(
dst
+
length
-
left
,
src
+
length
-
left
,
buffer
,
left
);
memxor3
(
dst
+
filled
,
src
+
filled
,
buffer
,
length
-
filled
);
}
}
}
...
...
@@ -107,19 +112,12 @@ ctr_crypt(const void *ctx, nettle_cipher_func *f,
while
(
length
>=
block_size
)
{
size_t
i
;
for
(
i
=
0
;
i
+
block_size
<=
buffer_size
&&
i
+
block_size
<=
length
;
i
+=
block_size
)
{
memcpy
(
buffer
+
i
,
ctr
,
block_size
);
INCREMENT
(
block_size
,
ctr
);
}
assert
(
i
>
0
);
f
(
ctx
,
i
,
buffer
,
buffer
);
memxor
(
dst
,
buffer
,
i
);
length
-=
i
;
dst
+=
i
;
size_t
filled
=
ctr_fill
(
block_size
,
ctr
,
MIN
(
buffer_size
,
length
),
buffer
);
assert
(
filled
>
0
);
f
(
ctx
,
filled
,
buffer
,
buffer
);
memxor
(
dst
,
buffer
,
filled
);
length
-=
filled
;
dst
+=
filled
;
}
/* Final, possibly partial, block. */
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment