Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
N
nettle
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Labels
Merge Requests
5
Merge Requests
5
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
Nettle
nettle
Commits
eead7cfa
Commit
eead7cfa
authored
Jul 02, 2019
by
Dmitry Baryshkov
Committed by
Niels Möller
Jul 02, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Move MAC testing code to generic place from cmac-test
Signed-off-by:
Dmitry Eremin-Solenikov
<
dbaryshkov@gmail.com
>
parent
ba24450d
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
96 additions
and
74 deletions
+96
-74
testsuite/cmac-test.c
testsuite/cmac-test.c
+26
-74
testsuite/testutils.c
testsuite/testutils.c
+64
-0
testsuite/testutils.h
testsuite/testutils.h
+6
-0
No files found.
testsuite/cmac-test.c
View file @
eead7cfa
...
...
@@ -2,83 +2,35 @@
#include "nettle-internal.h"
#include "cmac.h"
const
struct
nettle_mac
nettle_cmac_aes128
=
{
"CMAC-AES128"
,
sizeof
(
struct
cmac_aes128_ctx
),
CMAC128_DIGEST_SIZE
,
AES128_KEY_SIZE
,
(
nettle_set_key_func
*
)
cmac_aes128_set_key
,
(
nettle_hash_update_func
*
)
cmac_aes128_update
,
(
nettle_hash_digest_func
*
)
cmac_aes128_digest
};
const
struct
nettle_mac
nettle_cmac_aes256
=
{
"CMAC-AES256"
,
sizeof
(
struct
cmac_aes256_ctx
),
CMAC128_DIGEST_SIZE
,
AES256_KEY_SIZE
,
(
nettle_set_key_func
*
)
cmac_aes256_set_key
,
(
nettle_hash_update_func
*
)
cmac_aes256_update
,
(
nettle_hash_digest_func
*
)
cmac_aes256_digest
};
#define test_cmac_aes128(key, msg, ref) \
test_cmac_hash ((nettle_set_key_func*) cmac_aes128_set_key, \
(nettle_hash_update_func*) cmac_aes128_update, \
(nettle_hash_digest_func*) cmac_aes128_digest, \
sizeof(struct cmac_aes128_ctx), \
key, msg, ref)
test_mac(&nettle_cmac_aes128, key, msg, ref)
#define test_cmac_aes256(key, msg, ref) \
test_cmac_hash ((nettle_set_key_func*) cmac_aes256_set_key, \
(nettle_hash_update_func*) cmac_aes256_update, \
(nettle_hash_digest_func*) cmac_aes256_digest, \
sizeof(struct cmac_aes256_ctx), \
key, msg, ref)
static
void
test_cmac_hash
(
nettle_set_key_func
*
set_key
,
nettle_hash_update_func
*
update
,
nettle_hash_digest_func
*
digest
,
size_t
ctx_size
,
const
struct
tstring
*
key
,
const
struct
tstring
*
msg
,
const
struct
tstring
*
ref
)
{
void
*
ctx
;
uint8_t
hash
[
16
];
unsigned
i
;
ctx
=
xalloc
(
ctx_size
);
ASSERT
(
ref
->
length
==
sizeof
(
hash
));
ASSERT
(
key
->
length
==
16
||
key
->
length
==
32
);
set_key
(
ctx
,
key
->
data
);
update
(
ctx
,
msg
->
length
,
msg
->
data
);
digest
(
ctx
,
sizeof
(
hash
),
hash
);
if
(
!
MEMEQ
(
ref
->
length
,
ref
->
data
,
hash
))
{
fprintf
(
stderr
,
"cmac_hash failed, msg: "
);
print_hex
(
msg
->
length
,
msg
->
data
);
fprintf
(
stderr
,
"Output:"
);
print_hex
(
16
,
hash
);
fprintf
(
stderr
,
"Expected:"
);
tstring_print_hex
(
ref
);
fprintf
(
stderr
,
"
\n
"
);
FAIL
();
}
/* attempt to re-use the structure */
update
(
ctx
,
msg
->
length
,
msg
->
data
);
digest
(
ctx
,
sizeof
(
hash
),
hash
);
if
(
!
MEMEQ
(
ref
->
length
,
ref
->
data
,
hash
))
{
fprintf
(
stderr
,
"cmac_hash failed on re-use, msg: "
);
print_hex
(
msg
->
length
,
msg
->
data
);
fprintf
(
stderr
,
"Output:"
);
print_hex
(
16
,
hash
);
fprintf
(
stderr
,
"Expected:"
);
tstring_print_hex
(
ref
);
fprintf
(
stderr
,
"
\n
"
);
FAIL
();
}
/* attempt byte-by-byte hashing */
set_key
(
ctx
,
key
->
data
);
for
(
i
=
0
;
i
<
msg
->
length
;
i
++
)
update
(
ctx
,
1
,
msg
->
data
+
i
);
digest
(
ctx
,
sizeof
(
hash
),
hash
);
if
(
!
MEMEQ
(
ref
->
length
,
ref
->
data
,
hash
))
{
fprintf
(
stderr
,
"cmac_hash failed on byte-by-byte, msg: "
);
print_hex
(
msg
->
length
,
msg
->
data
);
fprintf
(
stderr
,
"Output:"
);
print_hex
(
16
,
hash
);
fprintf
(
stderr
,
"Expected:"
);
tstring_print_hex
(
ref
);
fprintf
(
stderr
,
"
\n
"
);
FAIL
();
}
free
(
ctx
);
}
test_mac(&nettle_cmac_aes256, key, msg, ref)
void
test_main
(
void
)
...
...
testsuite/testutils.c
View file @
eead7cfa
...
...
@@ -924,6 +924,70 @@ test_hash_large(const struct nettle_hash *hash,
free
(
data
);
}
void
test_mac
(
const
struct
nettle_mac
*
mac
,
const
struct
tstring
*
key
,
const
struct
tstring
*
msg
,
const
struct
tstring
*
digest
)
{
void
*
ctx
=
xalloc
(
mac
->
context_size
);
uint8_t
*
hash
=
xalloc
(
mac
->
digest_size
);
unsigned
i
;
ASSERT
(
digest
->
length
==
mac
->
digest_size
);
ASSERT
(
key
->
length
==
mac
->
key_size
);
mac
->
set_key
(
ctx
,
key
->
data
);
mac
->
update
(
ctx
,
msg
->
length
,
msg
->
data
);
mac
->
digest
(
ctx
,
digest
->
length
,
hash
);
if
(
!
MEMEQ
(
digest
->
length
,
digest
->
data
,
hash
))
{
fprintf
(
stderr
,
"test_mac failed, msg: "
);
print_hex
(
msg
->
length
,
msg
->
data
);
fprintf
(
stderr
,
"Output:"
);
print_hex
(
mac
->
digest_size
,
hash
);
fprintf
(
stderr
,
"Expected:"
);
tstring_print_hex
(
digest
);
fprintf
(
stderr
,
"
\n
"
);
FAIL
();
}
/* attempt to re-use the structure */
mac
->
update
(
ctx
,
msg
->
length
,
msg
->
data
);
mac
->
digest
(
ctx
,
digest
->
length
,
hash
);
if
(
!
MEMEQ
(
digest
->
length
,
digest
->
data
,
hash
))
{
fprintf
(
stderr
,
"test_mac: failed on re-use, msg: "
);
print_hex
(
msg
->
length
,
msg
->
data
);
fprintf
(
stderr
,
"Output:"
);
print_hex
(
mac
->
digest_size
,
hash
);
fprintf
(
stderr
,
"Expected:"
);
tstring_print_hex
(
digest
);
fprintf
(
stderr
,
"
\n
"
);
FAIL
();
}
/* attempt byte-by-byte hashing */
mac
->
set_key
(
ctx
,
key
->
data
);
for
(
i
=
0
;
i
<
msg
->
length
;
i
++
)
mac
->
update
(
ctx
,
1
,
msg
->
data
+
i
);
mac
->
digest
(
ctx
,
digest
->
length
,
hash
);
if
(
!
MEMEQ
(
digest
->
length
,
digest
->
data
,
hash
))
{
fprintf
(
stderr
,
"cmac_hash failed on byte-by-byte, msg: "
);
print_hex
(
msg
->
length
,
msg
->
data
);
fprintf
(
stderr
,
"Output:"
);
print_hex
(
16
,
hash
);
fprintf
(
stderr
,
"Expected:"
);
tstring_print_hex
(
digest
);
fprintf
(
stderr
,
"
\n
"
);
FAIL
();
}
free
(
ctx
);
free
(
hash
);
}
void
test_armor
(
const
struct
nettle_armor
*
armor
,
size_t
data_length
,
...
...
testsuite/testutils.h
View file @
eead7cfa
...
...
@@ -170,6 +170,12 @@ test_hash_large(const struct nettle_hash *hash,
uint8_t
c
,
const
struct
tstring
*
digest
);
void
test_mac
(
const
struct
nettle_mac
*
mac
,
const
struct
tstring
*
key
,
const
struct
tstring
*
msg
,
const
struct
tstring
*
digest
);
void
test_armor
(
const
struct
nettle_armor
*
armor
,
size_t
data_length
,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a 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