Back to home page

EIC code displayed by LXR

 
 

    


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

0001 /* umac.h
0002 
0003    UMAC message authentication code (RFC-4418).
0004 
0005    Copyright (C) 2013 Niels Möller
0006 
0007    This file is part of GNU Nettle.
0008 
0009    GNU Nettle is free software: you can redistribute it and/or
0010    modify it under the terms of either:
0011 
0012      * the GNU Lesser General Public License as published by the Free
0013        Software Foundation; either version 3 of the License, or (at your
0014        option) any later version.
0015 
0016    or
0017 
0018      * the GNU General Public License as published by the Free
0019        Software Foundation; either version 2 of the License, or (at your
0020        option) any later version.
0021 
0022    or both in parallel, as here.
0023 
0024    GNU Nettle is distributed in the hope that it will be useful,
0025    but WITHOUT ANY WARRANTY; without even the implied warranty of
0026    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0027    General Public License for more details.
0028 
0029    You should have received copies of the GNU General Public License and
0030    the GNU Lesser General Public License along with this program.  If
0031    not, see http://www.gnu.org/licenses/.
0032 */
0033 
0034 #ifndef NETTLE_UMAC_H_INCLUDED
0035 #define NETTLE_UMAC_H_INCLUDED
0036 
0037 #ifdef __cplusplus
0038 extern "C" {
0039 #endif
0040 
0041 /* Namespace mangling */
0042 #define umac32_set_key  nettle_umac32_set_key
0043 #define umac64_set_key  nettle_umac64_set_key
0044 #define umac96_set_key  nettle_umac96_set_key
0045 #define umac128_set_key nettle_umac128_set_key
0046 #define umac32_set_nonce  nettle_umac32_set_nonce
0047 #define umac64_set_nonce  nettle_umac64_set_nonce
0048 #define umac96_set_nonce  nettle_umac96_set_nonce
0049 #define umac128_set_nonce nettle_umac128_set_nonce
0050 #define umac32_update  nettle_umac32_update
0051 #define umac64_update  nettle_umac64_update
0052 #define umac96_update  nettle_umac96_update
0053 #define umac128_update nettle_umac128_update
0054 #define umac32_digest  nettle_umac32_digest
0055 #define umac64_digest  nettle_umac64_digest
0056 #define umac96_digest  nettle_umac96_digest
0057 #define umac128_digest nettle_umac128_digest
0058 
0059 #include "nettle-types.h"
0060 #include "aes.h"
0061 
0062 #define UMAC_KEY_SIZE AES128_KEY_SIZE
0063 #define UMAC32_DIGEST_SIZE 4
0064 #define UMAC64_DIGEST_SIZE 8
0065 #define UMAC96_DIGEST_SIZE 12
0066 #define UMAC128_DIGEST_SIZE 16
0067 #define UMAC_BLOCK_SIZE 1024
0068 #define UMAC_MIN_NONCE_SIZE 1
0069 #define UMAC_MAX_NONCE_SIZE AES_BLOCK_SIZE
0070 /* For backwards compatibility */
0071 #define UMAC_DATA_SIZE UMAC_BLOCK_SIZE
0072 
0073 /* Subkeys and state for UMAC with tag size 32*n bits. */
0074 #define _UMAC_STATE(n)                  \
0075   uint32_t l1_key[UMAC_BLOCK_SIZE/4 + 4*((n)-1)];   \
0076   /* Keys in 32-bit pieces, high first */       \
0077   uint32_t l2_key[6*(n)];               \
0078   uint64_t l3_key1[8*(n)];              \
0079   uint32_t l3_key2[(n)];                \
0080   /* AES cipher for encrypting the nonce */     \
0081   struct aes128_ctx pdf_key;                \
0082   /* The l2_state consists of 2*n uint64_t, for poly64  \
0083      and poly128 hashing, followed by n additional  \
0084      uint64_t used as an input buffer. */       \
0085   uint64_t l2_state[3*(n)];             \
0086   /* Input to the pdf_key, zero-padded and low bits \
0087      cleared if appropriate. */             \
0088   uint8_t nonce[AES_BLOCK_SIZE];            \
0089   unsigned short nonce_length /* For incrementing */
0090 
0091   /* Buffering */ 
0092 #define _UMAC_BUFFER                    \
0093   unsigned index;                   \
0094   /* Complete blocks processed */           \
0095   uint64_t count;                   \
0096   uint8_t block[UMAC_BLOCK_SIZE]
0097   
0098 #define _UMAC_NONCE_CACHED 0x80
0099 
0100 struct umac32_ctx
0101 {
0102   _UMAC_STATE(1);
0103   /* Low bits and cache flag. */
0104   unsigned short nonce_low;
0105   /* Previous padding block */
0106   uint32_t pad_cache[AES_BLOCK_SIZE / 4];
0107   _UMAC_BUFFER;
0108 };
0109 
0110 struct umac64_ctx
0111 {
0112   _UMAC_STATE(2);
0113   /* Low bit and cache flag. */
0114   unsigned short nonce_low;
0115   /* Previous padding block */
0116   uint32_t pad_cache[AES_BLOCK_SIZE/4];
0117   _UMAC_BUFFER;
0118 };
0119 
0120 struct umac96_ctx
0121 {
0122   _UMAC_STATE(3);
0123   _UMAC_BUFFER;
0124 };
0125 
0126 struct umac128_ctx
0127 {
0128   _UMAC_STATE(4);
0129   _UMAC_BUFFER;
0130 };
0131 
0132 /* The _set_key function initialize the nonce to zero. */
0133 void
0134 umac32_set_key (struct umac32_ctx *ctx, const uint8_t *key);
0135 void
0136 umac64_set_key (struct umac64_ctx *ctx, const uint8_t *key);
0137 void
0138 umac96_set_key (struct umac96_ctx *ctx, const uint8_t *key);
0139 void
0140 umac128_set_key (struct umac128_ctx *ctx, const uint8_t *key);
0141 
0142 /* Optional, if not used, messages get incrementing nonces starting from zero. */
0143 void
0144 umac32_set_nonce (struct umac32_ctx *ctx,
0145           size_t nonce_length, const uint8_t *nonce);
0146 void
0147 umac64_set_nonce (struct umac64_ctx *ctx,
0148           size_t nonce_length, const uint8_t *nonce);
0149 void
0150 umac96_set_nonce (struct umac96_ctx *ctx,
0151           size_t nonce_length, const uint8_t *nonce);
0152 void
0153 umac128_set_nonce (struct umac128_ctx *ctx,
0154            size_t nonce_length, const uint8_t *nonce);
0155 
0156 void
0157 umac32_update (struct umac32_ctx *ctx,
0158            size_t length, const uint8_t *data);
0159 void
0160 umac64_update (struct umac64_ctx *ctx,
0161            size_t length, const uint8_t *data);
0162 void
0163 umac96_update (struct umac96_ctx *ctx,
0164            size_t length, const uint8_t *data);
0165 void
0166 umac128_update (struct umac128_ctx *ctx,
0167         size_t length, const uint8_t *data);
0168 
0169 /* The _digest functions increment the nonce */
0170 void
0171 umac32_digest (struct umac32_ctx *ctx,
0172            size_t length, uint8_t *digest);
0173 void
0174 umac64_digest (struct umac64_ctx *ctx,
0175            size_t length, uint8_t *digest);
0176 void
0177 umac96_digest (struct umac96_ctx *ctx,
0178            size_t length, uint8_t *digest);
0179 void
0180 umac128_digest (struct umac128_ctx *ctx,
0181         size_t length, uint8_t *digest);
0182 
0183 
0184 /* Internal functions */
0185 #define UMAC_POLY64_BLOCKS 16384
0186 
0187 #define UMAC_P64_OFFSET 59
0188 #define UMAC_P64 (- (uint64_t) UMAC_P64_OFFSET)
0189 
0190 #define UMAC_P128_OFFSET 159
0191 #define UMAC_P128_HI (~(uint64_t) 0)
0192 #define UMAC_P128_LO (-(uint64_t) UMAC_P128_OFFSET)
0193 
0194 #ifdef __cplusplus
0195 }
0196 #endif
0197 
0198 #endif /* NETTLE_UMAC_H_INCLUDED */