| 1 |
/* cmac.c - CMAC, Cipher-based MAC. |
| 2 |
* Copyright (C) 2013,2018 Jussi Kivilinna <[email protected]> |
| 3 |
* |
| 4 |
* This file is part of Libgcrypt. |
| 5 |
* |
| 6 |
* Libgcrypt is free software; you can redistribute it and/or modify |
| 7 |
* it under the terms of the GNU Lesser General Public License as |
| 8 |
* published by the Free Software Foundation; either version 2.1 of |
| 9 |
* the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* Libgcrypt is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU Lesser General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU Lesser General Public |
| 17 |
* License along with this program; if not, see <http://www.gnu.org/licenses/>. |
| 18 |
*/ |
| 19 |
|
| 20 |
#include <config.h> |
| 21 |
#include <stdio.h> |
| 22 |
#include <stdlib.h> |
| 23 |
#include <string.h> |
| 24 |
|
| 25 |
#include "g10lib.h" |
| 26 |
#include "cipher.h" |
| 27 |
#include "cipher-internal.h" |
| 28 |
#include "bufhelp.h" |
| 29 |
|
| 30 |
|
| 31 |
#define set_burn(burn, nburn) do { \ |
| 32 |
unsigned int __nburn = (nburn); \ |
| 33 |
(burn) = (burn) > __nburn ? (burn) : __nburn; } while (0) |
| 34 |
|
| 35 |
|
| 36 |
gcry_err_code_t |
| 37 |
_gcry_cmac_write (gcry_cipher_hd_t c, gcry_cmac_context_t *ctx, |
| 38 |
const byte * inbuf, size_t inlen) |
| 39 |
{ |
| 40 |
gcry_cipher_encrypt_t enc_fn = c->spec->encrypt; |
| 41 |
size_t blocksize_shift = _gcry_blocksize_shift(c); |
| 42 |
size_t blocksize = 1 << blocksize_shift; |
| 43 |
byte outbuf[MAX_BLOCKSIZE]; |
| 44 |
unsigned int burn = 0; |
| 45 |
unsigned int nblocks; |
| 46 |
size_t n; |
| 47 |
|
| 48 |
if (ctx->tag) |
| 49 |
return GPG_ERR_INV_STATE; |
| 50 |
|
| 51 |
if (!inbuf) |
| 52 |
return GPG_ERR_INV_ARG; |
| 53 |
|
| 54 |
if (inlen == 0) |
| 55 |
return 0; |
| 56 |
|
| 57 |
/* Last block is needed for cmac_final. */ |
| 58 |
if (ctx->mac_unused + inlen <= blocksize) |
| 59 |
{ |
| 60 |
buf_cpy (&ctx->macbuf[ctx->mac_unused], inbuf, inlen); |
| 61 |
ctx->mac_unused += inlen; |
| 62 |
inbuf += inlen; |
| 63 |
inlen -= inlen; |
| 64 |
|
| 65 |
return 0; |
| 66 |
} |
| 67 |
|
| 68 |
if (ctx->mac_unused) |
| 69 |
{ |
| 70 |
n = inlen; |
| 71 |
if (n > blocksize - ctx->mac_unused) |
| 72 |
n = blocksize - ctx->mac_unused; |
| 73 |
|
| 74 |
buf_cpy (&ctx->macbuf[ctx->mac_unused], inbuf, n); |
| 75 |
ctx->mac_unused += n; |
| 76 |
inbuf += n; |
| 77 |
inlen -= n; |
| 78 |
|
| 79 |
cipher_block_xor (ctx->u_iv.iv, ctx->u_iv.iv, ctx->macbuf, blocksize); |
| 80 |
set_burn (burn, enc_fn (&c->context.c, ctx->u_iv.iv, ctx->u_iv.iv)); |
| 81 |
|
| 82 |
ctx->mac_unused = 0; |
| 83 |
} |
| 84 |
|
| 85 |
if (c->bulk.cbc_enc && inlen > blocksize) |
| 86 |
{ |
| 87 |
nblocks = inlen >> blocksize_shift; |
| 88 |
nblocks -= ((nblocks << blocksize_shift) == inlen); |
| 89 |
|
| 90 |
c->bulk.cbc_enc (&c->context.c, ctx->u_iv.iv, outbuf, inbuf, nblocks, 1); |
| 91 |
inbuf += nblocks << blocksize_shift; |
| 92 |
inlen -= nblocks << blocksize_shift; |
| 93 |
|
| 94 |
wipememory (outbuf, sizeof (outbuf)); |
| 95 |
} |
| 96 |
else |
| 97 |
while (inlen > blocksize) |
| 98 |
{ |
| 99 |
cipher_block_xor (ctx->u_iv.iv, ctx->u_iv.iv, inbuf, blocksize); |
| 100 |
set_burn (burn, enc_fn (&c->context.c, ctx->u_iv.iv, ctx->u_iv.iv)); |
| 101 |
inlen -= blocksize; |
| 102 |
inbuf += blocksize; |
| 103 |
} |
| 104 |
|
| 105 |
/* Make sure that last block is passed to cmac_final. */ |
| 106 |
if (inlen == 0) |
| 107 |
BUG (); |
| 108 |
|
| 109 |
n = inlen; |
| 110 |
if (n > blocksize - ctx->mac_unused) |
| 111 |
n = blocksize - ctx->mac_unused; |
| 112 |
|
| 113 |
buf_cpy (&ctx->macbuf[ctx->mac_unused], inbuf, n); |
| 114 |
ctx->mac_unused += n; |
| 115 |
inbuf += n; |
| 116 |
inlen -= n; |
| 117 |
|
| 118 |
if (burn) |
| 119 |
_gcry_burn_stack (burn + 4 * sizeof (void *)); |
| 120 |
|
| 121 |
return 0; |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
gcry_err_code_t |
| 126 |
_gcry_cmac_generate_subkeys (gcry_cipher_hd_t c, gcry_cmac_context_t *ctx) |
| 127 |
{ |
| 128 |
const unsigned int blocksize = c->spec->blocksize; |
| 129 |
byte rb, carry, t, bi; |
| 130 |
unsigned int burn; |
| 131 |
int i, j; |
| 132 |
union |
| 133 |
{ |
| 134 |
size_t _aligned; |
| 135 |
byte buf[MAX_BLOCKSIZE]; |
| 136 |
} u; |
| 137 |
|
| 138 |
/* Tell compiler that we require a cipher with a 64bit or 128 bit block |
| 139 |
* length, to allow better optimization of this function. */ |
| 140 |
if (blocksize > 16 || blocksize < 8 || blocksize & (8 - 1)) |
| 141 |
return GPG_ERR_INV_CIPHER_MODE; |
| 142 |
|
| 143 |
if (MAX_BLOCKSIZE < blocksize) |
| 144 |
BUG (); |
| 145 |
|
| 146 |
/* encrypt zero block */ |
| 147 |
memset (u.buf, 0, blocksize); |
| 148 |
burn = c->spec->encrypt (&c->context.c, u.buf, u.buf); |
| 149 |
|
| 150 |
/* Currently supported blocksizes are 16 and 8. */ |
| 151 |
rb = blocksize == 16 ? 0x87 : 0x1B /* blocksize == 8 */ ; |
| 152 |
|
| 153 |
for (j = 0; j < 2; j++) |
| 154 |
{ |
| 155 |
/* Generate subkeys K1 and K2 */ |
| 156 |
carry = 0; |
| 157 |
for (i = blocksize - 1; i >= 0; i--) |
| 158 |
{ |
| 159 |
bi = u.buf[i]; |
| 160 |
t = carry | (bi << 1); |
| 161 |
carry = bi >> 7; |
| 162 |
u.buf[i] = t & 0xff; |
| 163 |
ctx->subkeys[j][i] = u.buf[i]; |
| 164 |
} |
| 165 |
u.buf[blocksize - 1] ^= carry ? rb : 0; |
| 166 |
ctx->subkeys[j][blocksize - 1] = u.buf[blocksize - 1]; |
| 167 |
} |
| 168 |
|
| 169 |
wipememory (&u, sizeof (u)); |
| 170 |
if (burn) |
| 171 |
_gcry_burn_stack (burn + 4 * sizeof (void *)); |
| 172 |
|
| 173 |
return 0; |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
gcry_err_code_t |
| 178 |
_gcry_cmac_final (gcry_cipher_hd_t c, gcry_cmac_context_t *ctx) |
| 179 |
{ |
| 180 |
const unsigned int blocksize = c->spec->blocksize; |
| 181 |
unsigned int count = ctx->mac_unused; |
| 182 |
unsigned int burn; |
| 183 |
byte *subkey; |
| 184 |
|
| 185 |
/* Tell compiler that we require a cipher with a 64bit or 128 bit block |
| 186 |
* length, to allow better optimization of this function. */ |
| 187 |
if (blocksize > 16 || blocksize < 8 || blocksize & (8 - 1)) |
| 188 |
return GPG_ERR_INV_CIPHER_MODE; |
| 189 |
|
| 190 |
if (count == blocksize) |
| 191 |
subkey = ctx->subkeys[0]; /* K1 */ |
| 192 |
else |
| 193 |
{ |
| 194 |
subkey = ctx->subkeys[1]; /* K2 */ |
| 195 |
ctx->macbuf[count++] = 0x80; |
| 196 |
while (count < blocksize) |
| 197 |
ctx->macbuf[count++] = 0; |
| 198 |
} |
| 199 |
|
| 200 |
cipher_block_xor (ctx->macbuf, ctx->macbuf, subkey, blocksize); |
| 201 |
|
| 202 |
cipher_block_xor (ctx->u_iv.iv, ctx->u_iv.iv, ctx->macbuf, blocksize); |
| 203 |
burn = c->spec->encrypt (&c->context.c, ctx->u_iv.iv, ctx->u_iv.iv); |
| 204 |
if (burn) |
| 205 |
_gcry_burn_stack (burn + 4 * sizeof (void *)); |
| 206 |
|
| 207 |
ctx->mac_unused = 0; |
| 208 |
|
| 209 |
return 0; |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
static gcry_err_code_t |
| 214 |
cmac_tag (gcry_cipher_hd_t c, gcry_cmac_context_t *ctx, |
| 215 |
unsigned char *tag, size_t taglen, int check) |
| 216 |
{ |
| 217 |
gcry_err_code_t ret; |
| 218 |
|
| 219 |
if (!tag || taglen == 0 || taglen > c->spec->blocksize) |
| 220 |
return GPG_ERR_INV_ARG; |
| 221 |
|
| 222 |
if (!ctx->tag) |
| 223 |
{ |
| 224 |
ret = _gcry_cmac_final (c, ctx); |
| 225 |
if (ret != 0) |
| 226 |
return ret; |
| 227 |
|
| 228 |
ctx->tag = 1; |
| 229 |
} |
| 230 |
|
| 231 |
if (!check) |
| 232 |
{ |
| 233 |
memcpy (tag, ctx->u_iv.iv, taglen); |
| 234 |
return GPG_ERR_NO_ERROR; |
| 235 |
} |
| 236 |
else |
| 237 |
{ |
| 238 |
return buf_eq_const (tag, ctx->u_iv.iv, taglen) ? |
| 239 |
GPG_ERR_NO_ERROR : GPG_ERR_CHECKSUM; |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
void |
| 245 |
_gcry_cmac_reset (gcry_cmac_context_t *ctx) |
| 246 |
{ |
| 247 |
char tmp_buf[sizeof(ctx->subkeys)]; |
| 248 |
|
| 249 |
/* Only keep subkeys when reseting context. */ |
| 250 |
|
| 251 |
buf_cpy (tmp_buf, ctx->subkeys, sizeof(ctx->subkeys)); |
| 252 |
memset (ctx, 0, sizeof(*ctx)); |
| 253 |
buf_cpy (ctx->subkeys, tmp_buf, sizeof(ctx->subkeys)); |
| 254 |
wipememory (tmp_buf, sizeof(tmp_buf)); |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
gcry_err_code_t |
| 259 |
_gcry_cipher_cmac_authenticate (gcry_cipher_hd_t c, |
| 260 |
const unsigned char *abuf, size_t abuflen) |
| 261 |
{ |
| 262 |
if (abuflen > 0 && !abuf) |
| 263 |
return GPG_ERR_INV_ARG; |
| 264 |
/* To support new blocksize, update cmac_generate_subkeys() then add new |
| 265 |
blocksize here. */ |
| 266 |
if (c->spec->blocksize != 16 && c->spec->blocksize != 8) |
| 267 |
return GPG_ERR_INV_CIPHER_MODE; |
| 268 |
|
| 269 |
return _gcry_cmac_write (c, &c->u_mode.cmac, abuf, abuflen); |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
gcry_err_code_t |
| 274 |
_gcry_cipher_cmac_get_tag (gcry_cipher_hd_t c, |
| 275 |
unsigned char *outtag, size_t taglen) |
| 276 |
{ |
| 277 |
return cmac_tag (c, &c->u_mode.cmac, outtag, taglen, 0); |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
gcry_err_code_t |
| 282 |
_gcry_cipher_cmac_check_tag (gcry_cipher_hd_t c, |
| 283 |
const unsigned char *intag, size_t taglen) |
| 284 |
{ |
| 285 |
return cmac_tag (c, &c->u_mode.cmac, (unsigned char *) intag, taglen, 1); |
| 286 |
} |
| 287 |
|
| 288 |
gcry_err_code_t |
| 289 |
_gcry_cipher_cmac_set_subkeys (gcry_cipher_hd_t c) |
| 290 |
{ |
| 291 |
return _gcry_cmac_generate_subkeys (c, &c->u_mode.cmac); |
| 292 |
} |