...
 
Commits (10)
2019-07-08 Niels Möller <nisse@lysator.liu.se>
* nettle-types.h (union nettle_block16): Mark w member as deprecated.
* eax.c (block16_xor): Use uint64_t member of nettle_block16.
* gcm.c (gcm_gf_add, gcm_gf_shift, gcm_gf_shift_8): Likewise.
2019-07-10 Niels Möller <nisse@lysator.liu.se>
From Dmitry Eremin-Solenikov:
* cmac64.c (_cmac64_block_mulx, cmac64_set_key, cmac64_init)
(cmac64_update, cmac64_digest): New file, new functions.
* cmac-des3.c (cmac_des3_set_key, cmac_des3_update)
(cmac_des3_digest): New file, new functions.
* cmac.h: Add cmac64 and cmac_des3 declarations.
* Makefile.in (nettle_SOURCES): Add cmac64.c and cmac-des3.c.
* testsuite/cmac-test.c (test_main): Add tests for cmac_des3.
2019-07-02 Niels Möller <nisse@lysator.liu.se>
From Dmitry Eremin-Solenikov:
......
......@@ -102,7 +102,7 @@ nettle_SOURCES = aes-decrypt-internal.c aes-decrypt.c \
gcm-aes256.c gcm-aes256-meta.c \
gcm-camellia128.c gcm-camellia128-meta.c \
gcm-camellia256.c gcm-camellia256-meta.c \
cmac.c cmac-aes128.c cmac-aes256.c \
cmac.c cmac64.c cmac-aes128.c cmac-aes256.c cmac-des3.c \
gosthash94.c gosthash94-meta.c \
hmac.c hmac-md5.c hmac-ripemd160.c hmac-sha1.c \
hmac-sha224.c hmac-sha256.c hmac-sha384.c hmac-sha512.c \
......
/* cmac-des3.c
CMAC using TripleDES as the underlying cipher.
Copyright (C) 2019 Dmitry Eremin-Solenikov
This file is part of GNU Nettle.
GNU Nettle is free software: you can redistribute it and/or
modify it under the terms of either:
* the GNU Lesser General Public License as published by the Free
Software Foundation; either version 3 of the License, or (at your
option) any later version.
or
* the GNU General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your
option) any later version.
or both in parallel, as here.
GNU Nettle is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received copies of the GNU General Public License and
the GNU Lesser General Public License along with this program. If
not, see http://www.gnu.org/licenses/.
*/
#if HAVE_CONFIG_H
# include "config.h"
#endif
#include <assert.h>
#include "cmac.h"
void
cmac_des3_set_key (struct cmac_des3_ctx *ctx, const uint8_t *key)
{
CMAC64_SET_KEY (ctx, des3_set_key, des3_encrypt, key);
}
void
cmac_des3_update (struct cmac_des3_ctx *ctx,
size_t length, const uint8_t *data)
{
CMAC64_UPDATE (ctx, des3_encrypt, length, data);
}
void
cmac_des3_digest (struct cmac_des3_ctx *ctx,
size_t length, uint8_t *digest)
{
CMAC64_DIGEST (ctx, des3_encrypt, length, digest);
}
......@@ -37,6 +37,7 @@
#define NETTLE_CMAC_H_INCLUDED
#include "aes.h"
#include "des.h"
#include "nettle-types.h"
#ifdef __cplusplus
......@@ -44,6 +45,7 @@ extern "C" {
#endif
#define CMAC128_DIGEST_SIZE 16
#define CMAC64_DIGEST_SIZE 8
#define cmac128_set_key nettle_cmac128_set_key
#define cmac128_init nettle_cmac128_init
......@@ -56,6 +58,14 @@ extern "C" {
#define cmac_aes256_update nettle_cmac_aes256_update
#define cmac_aes256_digest nettle_cmac_aes256_digest
#define cmac64_set_key nettle_cmac64_set_key
#define cmac64_init nettle_cmac64_init
#define cmac64_update nettle_cmac64_update
#define cmac64_digest nettle_cmac64_digest
#define cmac_des3_set_key nettle_cmac_des3_set_key
#define cmac_des3_update nettle_cmac_des3_update
#define cmac_des3_digest nettle_cmac_des3_digest
struct cmac128_key
{
union nettle_block16 K1;
......@@ -72,6 +82,22 @@ struct cmac128_ctx
size_t index;
};
struct cmac64_key
{
union nettle_block8 K1;
union nettle_block8 K2;
};
struct cmac64_ctx
{
/* MAC state */
union nettle_block8 X;
/* Block buffer */
union nettle_block8 block;
size_t index;
};
void
cmac128_set_key(struct cmac128_key *key, const void *cipher,
nettle_cipher_func *encrypt);
......@@ -118,6 +144,53 @@ cmac128_digest(struct cmac128_ctx *ctx, const struct cmac128_key *key,
(nettle_cipher_func *) (encrypt), \
(length), (digest)))
void
cmac64_set_key(struct cmac64_key *key, const void *cipher,
nettle_cipher_func *encrypt);
void
cmac64_init(struct cmac64_ctx *ctx);
void
cmac64_update(struct cmac64_ctx *ctx, const void *cipher,
nettle_cipher_func *encrypt,
size_t msg_len, const uint8_t *msg);
void
cmac64_digest(struct cmac64_ctx *ctx, const struct cmac64_key *key,
const void *cipher, nettle_cipher_func *encrypt,
unsigned length, uint8_t *digest);
#define CMAC64_CTX(type) \
{ struct cmac64_key key; struct cmac64_ctx ctx; type cipher; }
/* NOTE: Avoid using NULL, as we don't include anything defining it. */
#define CMAC64_SET_KEY(self, set_key, encrypt, cmac_key) \
do { \
(set_key)(&(self)->cipher, (cmac_key)); \
if (0) (encrypt)(&(self)->cipher, ~(size_t) 0, \
(uint8_t *) 0, (const uint8_t *) 0); \
cmac64_set_key(&(self)->key, &(self)->cipher, \
(nettle_cipher_func *) (encrypt)); \
cmac64_init(&(self)->ctx); \
} while (0)
#define CMAC64_UPDATE(self, encrypt, length, src) \
(0 ? (encrypt)(&(self)->cipher, ~(size_t) 0, \
(uint8_t *) 0, (const uint8_t *) 0) \
: cmac64_update(&(self)->ctx, &(self)->cipher, \
(nettle_cipher_func *)encrypt, \
(length), (src)))
#define CMAC64_DIGEST(self, encrypt, length, digest) \
(0 ? (encrypt)(&(self)->cipher, ~(size_t) 0, \
(uint8_t *) 0, (const uint8_t *) 0) \
: cmac64_digest(&(self)->ctx, &(self)->key, \
&(self)->cipher, \
(nettle_cipher_func *) (encrypt), \
(length), (digest)))
struct cmac_aes128_ctx CMAC128_CTX(struct aes128_ctx);
void
......@@ -144,6 +217,19 @@ void
cmac_aes256_digest(struct cmac_aes256_ctx *ctx,
size_t length, uint8_t *digest);
struct cmac_des3_ctx CMAC64_CTX(struct des3_ctx);
void
cmac_des3_set_key(struct cmac_des3_ctx *ctx, const uint8_t *key);
void
cmac_des3_update(struct cmac_des3_ctx *ctx,
size_t length, const uint8_t *data);
void
cmac_des3_digest(struct cmac_des3_ctx *ctx,
size_t length, uint8_t *digest);
#ifdef __cplusplus
}
#endif
......
/*
CMAC-64, NIST SP 800-38B
Copyright (C) Stefan Metzmacher 2012
Copyright (C) Jeremy Allison 2012
Copyright (C) Michael Adam 2012
Copyright (C) 2017, Red Hat Inc.
Copyright (C) 2019, Dmitry Eremin-Solenikov
This file is part of GNU Nettle.
GNU Nettle is free software: you can redistribute it and/or
modify it under the terms of either:
* the GNU Lesser General Public License as published by the Free
Software Foundation; either version 3 of the License, or (at your
option) any later version.
or
* the GNU General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your
option) any later version.
or both in parallel, as here.
GNU Nettle is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received copies of the GNU General Public License and
the GNU Lesser General Public License along with this program. If
not, see http://www.gnu.org/licenses/.
*/
#if HAVE_CONFIG_H
# include "config.h"
#endif
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "cmac.h"
#include "memxor.h"
#include "nettle-internal.h"
#include "macros.h"
/* shift one and XOR with 0x87. */
#if WORDS_BIGENDIAN
static void
_cmac64_block_mulx(union nettle_block8 *dst,
const union nettle_block8 *src)
{
uint64_t carry = src->u64 >> 63;
dst->u64 = (src->u64 << 1) ^ (0x1b & -carry);
}
#else /* !WORDS_BIGENDIAN */
#define LE_SHIFT(x) ((((x) & 0x7f7f7f7f7f7f7f7f) << 1) | \
(((x) & 0x8080808080808080) >> 15))
static void
_cmac64_block_mulx(union nettle_block8 *dst,
const union nettle_block8 *src)
{
uint64_t carry = (src->u64 & 0x80) >> 7;
dst->u64 = LE_SHIFT(src->u64) ^ (0x1b00000000000000 & -carry);
}
#endif /* !WORDS_BIGENDIAN */
void
cmac64_set_key(struct cmac64_key *key, const void *cipher,
nettle_cipher_func *encrypt)
{
static const union nettle_block8 zero_block;
union nettle_block8 L;
/* step 1 - generate subkeys k1 and k2 */
encrypt(cipher, 8, L.b, zero_block.b);
_cmac64_block_mulx(&key->K1, &L);
_cmac64_block_mulx(&key->K2, &key->K1);
}
void
cmac64_init(struct cmac64_ctx *ctx)
{
memset(&ctx->X, 0, sizeof(ctx->X));
ctx->index = 0;
}
#define MIN(x,y) ((x)<(y)?(x):(y))
void
cmac64_update(struct cmac64_ctx *ctx, const void *cipher,
nettle_cipher_func *encrypt,
size_t msg_len, const uint8_t *msg)
{
union nettle_block16 Y;
/*
* check if we expand the block
*/
if (ctx->index < 8)
{
size_t len = MIN(8 - ctx->index, msg_len);
memcpy(&ctx->block.b[ctx->index], msg, len);
msg += len;
msg_len -= len;
ctx->index += len;
}
if (msg_len == 0) {
/* if it is still the last block, we are done */
return;
}
/*
* now checksum everything but the last block
*/
memxor3(Y.b, ctx->X.b, ctx->block.b, 8);
encrypt(cipher, 8, ctx->X.b, Y.b);
while (msg_len > 8)
{
memxor3(Y.b, ctx->X.b, msg, 8);
encrypt(cipher, 8, ctx->X.b, Y.b);
msg += 8;
msg_len -= 8;
}
/*
* copy the last block, it will be processed in
* cmac64_digest().
*/
memcpy(ctx->block.b, msg, msg_len);
ctx->index = msg_len;
}
void
cmac64_digest(struct cmac64_ctx *ctx, const struct cmac64_key *key,
const void *cipher, nettle_cipher_func *encrypt,
unsigned length, uint8_t *dst)
{
union nettle_block8 Y;
memset(ctx->block.b+ctx->index, 0, sizeof(ctx->block.b)-ctx->index);
/* re-use ctx->block for memxor output */
if (ctx->index < 8)
{
ctx->block.b[ctx->index] = 0x80;
memxor(ctx->block.b, key->K2.b, 8);
}
else
{
memxor(ctx->block.b, key->K1.b, 8);
}
memxor3(Y.b, ctx->block.b, ctx->X.b, 8);
assert(length <= 8);
if (length == 8)
{
encrypt(cipher, 8, dst, Y.b);
}
else
{
encrypt(cipher, 8, ctx->block.b, Y.b);
memcpy(dst, ctx->block.b, length);
}
/* reset state for re-use */
memset(&ctx->X, 0, sizeof(ctx->X));
ctx->index = 0;
}
......@@ -54,12 +54,8 @@ omac_init (union nettle_block16 *state, unsigned t)
static void
block16_xor (union nettle_block16 *dst, const union nettle_block16 *src)
{
dst->w[0] ^= src->w[0];
dst->w[1] ^= src->w[1];
#if SIZEOF_LONG == 4
dst->w[2] ^= src->w[2];
dst->w[3] ^= src->w[3];
#endif
dst->u64[0] ^= src->u64[0];
dst->u64[1] ^= src->u64[1];
}
static void
......
......@@ -60,12 +60,8 @@ static void
gcm_gf_add (union nettle_block16 *r,
const union nettle_block16 *x, const union nettle_block16 *y)
{
r->w[0] = x->w[0] ^ y->w[0];
r->w[1] = x->w[1] ^ y->w[1];
#if SIZEOF_LONG == 4
r->w[2] = x->w[2] ^ y->w[2];
r->w[3] = x->w[3] ^ y->w[3];
#endif
r->u64[0] = x->u64[0] ^ y->u64[0];
r->u64[1] = x->u64[1] ^ y->u64[1];
}
/* Multiplication by 010...0; a big-endian shift right. If the bit
shifted out is one, the defining polynomial is added to cancel it
......@@ -73,43 +69,20 @@ gcm_gf_add (union nettle_block16 *r,
static void
gcm_gf_shift (union nettle_block16 *r, const union nettle_block16 *x)
{
long mask;
uint64_t mask;
/* Shift uses big-endian representation. */
#if WORDS_BIGENDIAN
# if SIZEOF_LONG == 4
mask = - (x->w[3] & 1);
r->w[3] = (x->w[3] >> 1) | ((x->w[2] & 1) << 31);
r->w[2] = (x->w[2] >> 1) | ((x->w[1] & 1) << 31);
r->w[1] = (x->w[1] >> 1) | ((x->w[0] & 1) << 31);
r->w[0] = (x->w[0] >> 1) ^ (mask & (GHASH_POLYNOMIAL << 24));
# elif SIZEOF_LONG == 8
mask = - (x->w[1] & 1);
r->w[1] = (x->w[1] >> 1) | ((x->w[0] & 1) << 63);
r->w[0] = (x->w[0] >> 1) ^ (mask & (GHASH_POLYNOMIAL << 56));
# else
# error Unsupported word size. */
#endif
mask = - (x->u64[1] & 1);
r->u64[1] = (x->u64[1] >> 1) | ((x->u64[0] & 1) << 63);
r->u64[0] = (x->u64[0] >> 1) ^ (mask & ((uint64_t) GHASH_POLYNOMIAL << 56));
#else /* ! WORDS_BIGENDIAN */
# if SIZEOF_LONG == 4
#define RSHIFT_WORD(x) \
((((x) & 0xfefefefeUL) >> 1) \
| (((x) & 0x00010101) << 15))
mask = - ((x->w[3] >> 24) & 1);
r->w[3] = RSHIFT_WORD(x->w[3]) | ((x->w[2] >> 17) & 0x80);
r->w[2] = RSHIFT_WORD(x->w[2]) | ((x->w[1] >> 17) & 0x80);
r->w[1] = RSHIFT_WORD(x->w[1]) | ((x->w[0] >> 17) & 0x80);
r->w[0] = RSHIFT_WORD(x->w[0]) ^ (mask & GHASH_POLYNOMIAL);
# elif SIZEOF_LONG == 8
#define RSHIFT_WORD(x) \
((((x) & 0xfefefefefefefefeUL) >> 1) \
| (((x) & 0x0001010101010101UL) << 15))
mask = - ((x->w[1] >> 56) & 1);
r->w[1] = RSHIFT_WORD(x->w[1]) | ((x->w[0] >> 49) & 0x80);
r->w[0] = RSHIFT_WORD(x->w[0]) ^ (mask & GHASH_POLYNOMIAL);
# else
# error Unsupported word size. */
# endif
mask = - ((x->u64[1] >> 56) & 1);
r->u64[1] = RSHIFT_WORD(x->u64[1]) | ((x->u64[0] >> 49) & 0x80);
r->u64[0] = RSHIFT_WORD(x->u64[0]) ^ (mask & GHASH_POLYNOMIAL);
# undef RSHIFT_WORD
#endif /* ! WORDS_BIGENDIAN */
}
......@@ -268,38 +241,17 @@ shift_table[0x100] = {
static void
gcm_gf_shift_8(union nettle_block16 *x)
{
unsigned long *w = x->w;
unsigned long reduce;
uint64_t reduce;
/* Shift uses big-endian representation. */
#if WORDS_BIGENDIAN
# if SIZEOF_LONG == 4
reduce = shift_table[w[3] & 0xff];
w[3] = (w[3] >> 8) | ((w[2] & 0xff) << 24);
w[2] = (w[2] >> 8) | ((w[1] & 0xff) << 24);
w[1] = (w[1] >> 8) | ((w[0] & 0xff) << 24);
w[0] = (w[0] >> 8) ^ (reduce << 16);
# elif SIZEOF_LONG == 8
reduce = shift_table[w[1] & 0xff];
w[1] = (w[1] >> 8) | ((w[0] & 0xff) << 56);
w[0] = (w[0] >> 8) ^ (reduce << 48);
# else
# error Unsupported word size. */
#endif
reduce = shift_table[x->u64[1] & 0xff];
x->u64[1] = (x->u64[1] >> 8) | ((x->u64[0] & 0xff) << 56);
x->u64[0] = (x->u64[0] >> 8) ^ (reduce << 48);
#else /* ! WORDS_BIGENDIAN */
# if SIZEOF_LONG == 4
reduce = shift_table[(w[3] >> 24) & 0xff];
w[3] = (w[3] << 8) | (w[2] >> 24);
w[2] = (w[2] << 8) | (w[1] >> 24);
w[1] = (w[1] << 8) | (w[0] >> 24);
w[0] = (w[0] << 8) ^ reduce;
# elif SIZEOF_LONG == 8
reduce = shift_table[(w[1] >> 56) & 0xff];
w[1] = (w[1] << 8) | (w[0] >> 56);
w[0] = (w[0] << 8) ^ reduce;
# else
# error Unsupported word size. */
# endif
reduce = shift_table[(x->u64[1] >> 56) & 0xff];
x->u64[1] = (x->u64[1] << 8) | (x->u64[0] >> 56);
x->u64[0] = (x->u64[0] << 8) ^ reduce;
#endif /* ! WORDS_BIGENDIAN */
}
......
......@@ -61,10 +61,16 @@ extern "C" {
union nettle_block16
{
uint8_t b[16];
unsigned long w[16 / sizeof(unsigned long)];
unsigned long w[16 / sizeof(unsigned long)] _NETTLE_ATTRIBUTE_DEPRECATED;
uint64_t u64[2];
};
union nettle_block8
{
uint8_t b[8];
uint64_t u64;
};
/* Randomness. Used by key generation and dsa signature creation. */
typedef void nettle_random_func(void *ctx,
size_t length, uint8_t *dst);
......
......@@ -3815,14 +3815,21 @@ as described above, the new value is used unless you call the
@node CMAC,, UMAC, Keyed hash functions
@subsection @acronym{CMAC}
@cindex CMAC
@cindex CMAC-128
@cindex CMAC-64
@acronym{CMAC} is a message authentication code based on CBC encryption
mode. It is suitable for systems where block ciphers are preferrable
and perform better than hash functions. @acronym{CMAC} is specified in
and perform better than hash functions. @acronym{CMAC-128} is specified in
@cite{RFC4493}. The block size is always 128 bits (16 octets).
@acronym{CMAC-64} is specified by
@uref{https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-38B.pdf,
NIST Special Publication 800-38B}. The block size is always 64 bits
(8 octets).
Nettle provides helper functions for @acronym{CMAC } with
the @acronym{AES} block cipher.
Nettle provides helper functions for @acronym{CMAC-128} with
the @acronym{AES} block cipher and for @acronym{CMAC-64} with
the @acronym{Tripple-DES} block cipher.
Nettle defines @acronym{CMAC} in @file{<nettle/cmac.h>}.
......@@ -3831,7 +3838,7 @@ Nettle defines @acronym{CMAC} in @file{<nettle/cmac.h>}.
@end deftp
@defvr Constant CMAC128_DIGEST_SIZE
The size of an CMAC digest, 16.
The size of an CMAC-128 digest, 16.
@end defvr
@deftypefun void cmac_aes128_set_key (struct cmac_aes128_ctx *@var{ctx}, const uint8_t *@var{key})
......@@ -3866,6 +3873,29 @@ provide a smaller value, only the first @var{length} octets of the
processing of a new message with the same key.
@end deftypefun
@deftp {Context struct} {struct cmac_des3_ctx}
@end deftp
@defvr Constant CMAC64_DIGEST_SIZE
The size of an CMAC-64 digest, 8.
@end defvr
@deftypefun void cmac_des3_set_key (struct cmac_des3_ctx *@var{ctx}, const uint8_t *@var{key})
This function initializes the @acronym{CMAC} context struct for @acronym{Tripple-DES}.
@end deftypefun
@deftypefun void cmac_des3_update (struct cmac_des3_ctx *@var{ctx},size_t @var{length}, const uint8_t *@var{data})
This function is called zero or more times to process the message.
@end deftypefun
@deftypefun void cmac_des3_digest (struct cmac_des3_ctx *@var{ctx}, size_t @var{length}, uint8_t *@var{digest})
Extracts the @acronym{MAC} of the message, writing it to @var{digest}.
@var{length} is usually equal to the specified output size, but if you
provide a smaller value, only the first @var{length} octets of the
@acronym{MAC} are written. This function resets the context for
processing of a new message with the same key.
@end deftypefun
@node Poly1305,, CMAC, Keyed hash functions
@comment node-name, next, previous, up
@subsection Poly1305
......
......@@ -26,12 +26,27 @@ const struct nettle_mac nettle_cmac_aes256 =
(nettle_hash_digest_func*) cmac_aes256_digest
};
const struct nettle_mac nettle_cmac_des3 =
{
"CMAC-3DES",
sizeof(struct cmac_des3_ctx),
CMAC64_DIGEST_SIZE,
DES3_KEY_SIZE,
(nettle_set_key_func*) cmac_des3_set_key,
(nettle_hash_update_func*) cmac_des3_update,
(nettle_hash_digest_func*) cmac_des3_digest
};
#define test_cmac_aes128(key, msg, ref) \
test_mac(&nettle_cmac_aes128, key, msg, ref)
#define test_cmac_aes256(key, msg, ref) \
test_mac(&nettle_cmac_aes256, key, msg, ref)
#define test_cmac_des3(key, msg, ref) \
test_mac(&nettle_cmac_des3, key, msg, ref)
void
test_main(void)
{
......@@ -96,4 +111,21 @@ test_main(void)
SHEX("6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710"),
SHEX("e1992190549f6ed5696a2c056c315410"));
/* CMAC-3DES vectors from NIST SP800-38B examples */
test_cmac_des3 (SHEX("0123456789abcdef23456789abcdef01456789abcdef0123"),
SDATA(""),
SHEX("7db0d37df936c550"));
test_cmac_des3 (SHEX("0123456789abcdef23456789abcdef01456789abcdef0123"),
SHEX("6bc1bee22e409f96e93d7e117393172a"),
SHEX("30239cf1f52e6609"));
test_cmac_des3 (SHEX("0123456789abcdef23456789abcdef01456789abcdef0123"),
SHEX("6bc1bee22e409f96e93d7e117393172aae2d8a57"),
SHEX("6c9f3ee4923f6be2"));
test_cmac_des3 (SHEX("0123456789abcdef23456789abcdef01456789abcdef0123"),
SHEX("6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e51"),
SHEX("99429bd0bf7904e5"));
}