Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:02:15

0001 /* cmac.h
0002 
0003    CMAC mode, as specified in RFC4493
0004 
0005    Copyright (C) 2017 Red Hat, Inc.
0006 
0007    Contributed by Nikos Mavrogiannopoulos
0008 
0009    This file is part of GNU Nettle.
0010 
0011    GNU Nettle is free software: you can redistribute it and/or
0012    modify it under the terms of either:
0013 
0014      * the GNU Lesser General Public License as published by the Free
0015        Software Foundation; either version 3 of the License, or (at your
0016        option) any later version.
0017 
0018    or
0019 
0020      * the GNU General Public License as published by the Free
0021        Software Foundation; either version 2 of the License, or (at your
0022        option) any later version.
0023 
0024    or both in parallel, as here.
0025 
0026    GNU Nettle is distributed in the hope that it will be useful,
0027    but WITHOUT ANY WARRANTY; without even the implied warranty of
0028    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0029    General Public License for more details.
0030 
0031    You should have received copies of the GNU General Public License and
0032    the GNU Lesser General Public License along with this program.  If
0033    not, see http://www.gnu.org/licenses/.
0034 */
0035 
0036 #ifndef NETTLE_CMAC_H_INCLUDED
0037 #define NETTLE_CMAC_H_INCLUDED
0038 
0039 #include "aes.h"
0040 #include "des.h"
0041 #include "nettle-types.h"
0042 
0043 #ifdef __cplusplus
0044 extern "C" {
0045 #endif
0046 
0047 #define CMAC128_DIGEST_SIZE 16
0048 #define CMAC64_DIGEST_SIZE 8
0049 
0050 #define cmac128_set_key nettle_cmac128_set_key
0051 #define cmac128_init nettle_cmac128_init
0052 #define cmac128_update nettle_cmac128_update
0053 #define cmac128_digest nettle_cmac128_digest
0054 #define cmac_aes128_set_key nettle_cmac_aes128_set_key
0055 #define cmac_aes128_update nettle_cmac_aes128_update
0056 #define cmac_aes128_digest nettle_cmac_aes128_digest
0057 #define cmac_aes256_set_key nettle_cmac_aes256_set_key
0058 #define cmac_aes256_update nettle_cmac_aes256_update
0059 #define cmac_aes256_digest nettle_cmac_aes256_digest
0060 
0061 #define cmac64_set_key nettle_cmac64_set_key
0062 #define cmac64_init nettle_cmac64_init
0063 #define cmac64_update nettle_cmac64_update
0064 #define cmac64_digest nettle_cmac64_digest
0065 #define cmac_des3_set_key nettle_cmac_des3_set_key
0066 #define cmac_des3_update nettle_cmac_des3_update
0067 #define cmac_des3_digest nettle_cmac_des3_digest
0068 
0069 struct cmac128_key
0070 {
0071   union nettle_block16 K1;
0072   union nettle_block16 K2;
0073 };
0074 
0075 struct cmac128_ctx
0076 {
0077   /* MAC state */
0078   union nettle_block16 X;
0079 
0080   /* Block buffer */
0081   union nettle_block16 block;
0082   size_t index;
0083 };
0084 
0085 struct cmac64_key
0086 {
0087   union nettle_block8 K1;
0088   union nettle_block8 K2;
0089 };
0090 
0091 struct cmac64_ctx
0092 {
0093   /* MAC state */
0094   union nettle_block8 X;
0095 
0096   /* Block buffer */
0097   union nettle_block8 block;
0098   size_t index;
0099 };
0100 
0101 void
0102 cmac128_set_key(struct cmac128_key *key, const void *cipher,
0103         nettle_cipher_func *encrypt);
0104 
0105 void
0106 cmac128_init(struct cmac128_ctx *ctx);
0107 
0108 void
0109 cmac128_update(struct cmac128_ctx *ctx, const void *cipher,
0110            nettle_cipher_func *encrypt,
0111            size_t msg_len, const uint8_t *msg);
0112 void
0113 cmac128_digest(struct cmac128_ctx *ctx, const struct cmac128_key *key,
0114            const void *cipher, nettle_cipher_func *encrypt,
0115            unsigned length, uint8_t *digest);
0116 
0117 
0118 #define CMAC128_CTX(type) \
0119   { struct cmac128_key key; struct cmac128_ctx ctx; type cipher; }
0120 
0121 /* NOTE: Avoid using NULL, as we don't include anything defining it. */
0122 #define CMAC128_SET_KEY(self, set_key, encrypt, cmac_key)   \
0123   do {                              \
0124     (set_key)(&(self)->cipher, (cmac_key));         \
0125     if (0) (encrypt)(&(self)->cipher, ~(size_t) 0,      \
0126              (uint8_t *) 0, (const uint8_t *) 0);   \
0127     cmac128_set_key(&(self)->key, &(self)->cipher,      \
0128             (nettle_cipher_func *) (encrypt));      \
0129     cmac128_init(&(self)->ctx);                 \
0130   } while (0)
0131 
0132 #define CMAC128_UPDATE(self, encrypt, length, src)      \
0133   (0 ? (encrypt)(&(self)->cipher, ~(size_t) 0,          \
0134          (uint8_t *) 0, (const uint8_t *) 0)        \
0135      : cmac128_update(&(self)->ctx, &(self)->cipher,        \
0136               (nettle_cipher_func *)encrypt,        \
0137               (length), (src)))
0138 
0139 #define CMAC128_DIGEST(self, encrypt, length, digest)       \
0140   (0 ? (encrypt)(&(self)->cipher, ~(size_t) 0,          \
0141          (uint8_t *) 0, (const uint8_t *) 0)        \
0142      : cmac128_digest(&(self)->ctx, &(self)->key,       \
0143               &(self)->cipher,              \
0144               (nettle_cipher_func *) (encrypt),     \
0145               (length), (digest)))
0146 
0147 void
0148 cmac64_set_key(struct cmac64_key *key, const void *cipher,
0149         nettle_cipher_func *encrypt);
0150 
0151 void
0152 cmac64_init(struct cmac64_ctx *ctx);
0153 
0154 void
0155 cmac64_update(struct cmac64_ctx *ctx, const void *cipher,
0156            nettle_cipher_func *encrypt,
0157            size_t msg_len, const uint8_t *msg);
0158 
0159 void
0160 cmac64_digest(struct cmac64_ctx *ctx, const struct cmac64_key *key,
0161            const void *cipher, nettle_cipher_func *encrypt,
0162            unsigned length, uint8_t *digest);
0163 
0164 
0165 #define CMAC64_CTX(type) \
0166   { struct cmac64_key key; struct cmac64_ctx ctx; type cipher; }
0167 
0168 /* NOTE: Avoid using NULL, as we don't include anything defining it. */
0169 #define CMAC64_SET_KEY(self, set_key, encrypt, cmac_key)    \
0170   do {                              \
0171     (set_key)(&(self)->cipher, (cmac_key));         \
0172     if (0) (encrypt)(&(self)->cipher, ~(size_t) 0,      \
0173              (uint8_t *) 0, (const uint8_t *) 0);   \
0174     cmac64_set_key(&(self)->key, &(self)->cipher,       \
0175             (nettle_cipher_func *) (encrypt));      \
0176     cmac64_init(&(self)->ctx);                  \
0177   } while (0)
0178 
0179 #define CMAC64_UPDATE(self, encrypt, length, src)       \
0180   (0 ? (encrypt)(&(self)->cipher, ~(size_t) 0,          \
0181          (uint8_t *) 0, (const uint8_t *) 0)        \
0182      : cmac64_update(&(self)->ctx, &(self)->cipher,     \
0183               (nettle_cipher_func *)encrypt,        \
0184               (length), (src)))
0185 
0186 #define CMAC64_DIGEST(self, encrypt, length, digest)        \
0187   (0 ? (encrypt)(&(self)->cipher, ~(size_t) 0,          \
0188          (uint8_t *) 0, (const uint8_t *) 0)        \
0189      : cmac64_digest(&(self)->ctx, &(self)->key,        \
0190               &(self)->cipher,              \
0191               (nettle_cipher_func *) (encrypt),     \
0192               (length), (digest)))
0193 
0194 struct cmac_aes128_ctx CMAC128_CTX(struct aes128_ctx);
0195 
0196 void
0197 cmac_aes128_set_key(struct cmac_aes128_ctx *ctx, const uint8_t *key);
0198 
0199 void
0200 cmac_aes128_update(struct cmac_aes128_ctx *ctx,
0201            size_t length, const uint8_t *data);
0202 
0203 void
0204 cmac_aes128_digest(struct cmac_aes128_ctx *ctx,
0205            size_t length, uint8_t *digest);
0206 
0207 struct cmac_aes256_ctx CMAC128_CTX(struct aes256_ctx);
0208 
0209 void
0210 cmac_aes256_set_key(struct cmac_aes256_ctx *ctx, const uint8_t *key);
0211 
0212 void
0213 cmac_aes256_update(struct cmac_aes256_ctx *ctx,
0214            size_t length, const uint8_t *data);
0215 
0216 void
0217 cmac_aes256_digest(struct cmac_aes256_ctx *ctx,
0218            size_t length, uint8_t *digest);
0219 
0220 struct cmac_des3_ctx CMAC64_CTX(struct des3_ctx);
0221 
0222 void
0223 cmac_des3_set_key(struct cmac_des3_ctx *ctx, const uint8_t *key);
0224 
0225 void
0226 cmac_des3_update(struct cmac_des3_ctx *ctx,
0227          size_t length, const uint8_t *data);
0228 
0229 void
0230 cmac_des3_digest(struct cmac_des3_ctx *ctx,
0231          size_t length, uint8_t *digest);
0232 
0233 #ifdef __cplusplus
0234 }
0235 #endif
0236 
0237 #endif /* CMAC_H_INCLUDED */