| 1 |
/* cipher-aeswrap.c - Generic AESWRAP mode implementation |
| 2 |
* Copyright (C) 2009, 2011 Free Software Foundation, Inc. |
| 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 |
#include <errno.h> |
| 25 |
|
| 26 |
#include "g10lib.h" |
| 27 |
#include "cipher.h" |
| 28 |
#include "bufhelp.h" |
| 29 |
#include "./cipher-internal.h" |
| 30 |
|
| 31 |
/* Perform the wrap algorithm W as specified by NIST SP 800-38F. |
| 32 |
Cipher block size must be 128-bit. */ |
| 33 |
static gcry_err_code_t |
| 34 |
wrap (gcry_cipher_hd_t c, byte *outbuf, size_t inbuflen) |
| 35 |
{ |
| 36 |
int j, x; |
| 37 |
size_t n, i; |
| 38 |
unsigned char *r, *a, *b; |
| 39 |
unsigned char t[8]; |
| 40 |
unsigned int burn, nburn; |
| 41 |
|
| 42 |
#if MAX_BLOCKSIZE < 8 |
| 43 |
#error Invalid block size |
| 44 |
#endif |
| 45 |
/* We require a cipher with a 128 bit block length. */ |
| 46 |
if (c->spec->blocksize != 16) |
| 47 |
return GPG_ERR_INV_LENGTH; |
| 48 |
|
| 49 |
/* Input data must be multiple of 64 bits. */ |
| 50 |
if (inbuflen % 8) |
| 51 |
return GPG_ERR_INV_ARG; |
| 52 |
|
| 53 |
n = inbuflen / 8; |
| 54 |
|
| 55 |
/* We need at least three 64 bit blocks. */ |
| 56 |
if (n < 3) |
| 57 |
return GPG_ERR_INV_ARG; |
| 58 |
|
| 59 |
burn = 0; |
| 60 |
|
| 61 |
r = outbuf; |
| 62 |
a = outbuf; /* We store A directly in OUTBUF. */ |
| 63 |
b = c->u_ctr.ctr; /* B is also used to concatenate stuff. */ |
| 64 |
|
| 65 |
memset (t, 0, sizeof t); /* t := 0. */ |
| 66 |
|
| 67 |
for (j = 0; j <= 5; j++) |
| 68 |
{ |
| 69 |
for (i = 1; i < n; i++) |
| 70 |
{ |
| 71 |
/* B := CIPH_k( A | R[i] ) */ |
| 72 |
memcpy (b, a, 8); |
| 73 |
memcpy (b+8, r+i*8, 8); |
| 74 |
nburn = c->spec->encrypt (&c->context.c, b, b); |
| 75 |
burn = nburn > burn ? nburn : burn; |
| 76 |
/* t := t + 1 */ |
| 77 |
for (x = 7; x >= 0; x--) |
| 78 |
if (++t[x]) |
| 79 |
break; |
| 80 |
/* A := MSB_64(B) ^ t */ |
| 81 |
cipher_block_xor (a, b, t, 8); |
| 82 |
/* R[i] := LSB_64(B) */ |
| 83 |
memcpy (r+i*8, b+8, 8); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
if (burn > 0) |
| 88 |
_gcry_burn_stack (burn + 4 * sizeof(void *)); |
| 89 |
|
| 90 |
return 0; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
/* Perform the Key Wrap algorithm as specified by RFC3394. We |
| 95 |
implement this as a mode usable with any cipher algorithm of |
| 96 |
blocksize 128. */ |
| 97 |
gcry_err_code_t |
| 98 |
_gcry_cipher_keywrap_encrypt (gcry_cipher_hd_t c, |
| 99 |
byte *outbuf, size_t outbuflen, |
| 100 |
const byte *inbuf, size_t inbuflen) |
| 101 |
{ |
| 102 |
gcry_err_code_t err; |
| 103 |
unsigned char *r = outbuf; |
| 104 |
|
| 105 |
/* We require a cipher with a 128 bit block length. */ |
| 106 |
if (c->spec->blocksize != 16) |
| 107 |
return GPG_ERR_INV_LENGTH; |
| 108 |
|
| 109 |
/* The output buffer must be able to hold the input data plus one |
| 110 |
additional block. */ |
| 111 |
if (outbuflen < inbuflen + 8) |
| 112 |
return GPG_ERR_BUFFER_TOO_SHORT; |
| 113 |
/* Input data must be multiple of 64 bits. */ |
| 114 |
if (inbuflen % 8) |
| 115 |
return GPG_ERR_INV_ARG; |
| 116 |
|
| 117 |
/* We need at least two 64 bit blocks. */ |
| 118 |
if ((inbuflen / 8) < 2) |
| 119 |
return GPG_ERR_INV_ARG; |
| 120 |
|
| 121 |
/* Copy the inbuf to the outbuf. */ |
| 122 |
memmove (r+8, inbuf, inbuflen); |
| 123 |
|
| 124 |
/* If an IV has been set we use that IV as the Alternative Initial |
| 125 |
Value; if it has not been set we use the standard value. */ |
| 126 |
if (c->marks.iv) |
| 127 |
memcpy (r, c->u_iv.iv, 8); |
| 128 |
else |
| 129 |
memset (r, 0xa6, 8); |
| 130 |
|
| 131 |
err = wrap (c, r, inbuflen + 8); |
| 132 |
|
| 133 |
return err; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
static const unsigned char icv2[] = { 0xA6, 0x59, 0x59, 0xA6 }; |
| 138 |
|
| 139 |
/* Perform the Key Wrap algorithm as specified by RFC5649. */ |
| 140 |
gcry_err_code_t |
| 141 |
_gcry_cipher_keywrap_encrypt_padding (gcry_cipher_hd_t c, |
| 142 |
byte *outbuf, size_t outbuflen, |
| 143 |
const byte *inbuf, size_t inbuflen) |
| 144 |
{ |
| 145 |
gcry_err_code_t err; |
| 146 |
unsigned char *r = outbuf; |
| 147 |
unsigned int padlen; |
| 148 |
|
| 149 |
/* We require a cipher with a 128 bit block length. */ |
| 150 |
if (c->spec->blocksize != 16) |
| 151 |
return GPG_ERR_INV_LENGTH; |
| 152 |
|
| 153 |
/* The output buffer must be able to hold the input data plus one |
| 154 |
additional block and padding. */ |
| 155 |
if (outbuflen < ((inbuflen + 7)/8)*8 + 8) |
| 156 |
return GPG_ERR_BUFFER_TOO_SHORT; |
| 157 |
|
| 158 |
if (inbuflen % 8) |
| 159 |
padlen = 8 - (inbuflen % 8); |
| 160 |
else |
| 161 |
padlen = 0; |
| 162 |
|
| 163 |
memcpy (r, icv2, 4); |
| 164 |
r[4] = ((inbuflen >> 24) & 0xff); |
| 165 |
r[5] = ((inbuflen >> 16) & 0xff); |
| 166 |
r[6] = ((inbuflen >> 8) & 0xff); |
| 167 |
r[7] = (inbuflen & 0xff); |
| 168 |
memcpy (r+8, inbuf, inbuflen); |
| 169 |
if (padlen) |
| 170 |
memset (r+8+inbuflen, 0, padlen); |
| 171 |
|
| 172 |
if (inbuflen <= 8) |
| 173 |
{ |
| 174 |
unsigned int burn; |
| 175 |
|
| 176 |
burn = c->spec->encrypt (&c->context.c, r, r); |
| 177 |
if (burn > 0) |
| 178 |
_gcry_burn_stack (burn + 4 * sizeof(void *)); |
| 179 |
err = 0; |
| 180 |
} |
| 181 |
else |
| 182 |
err = wrap (c, r, ((inbuflen + 7)/8)*8 + 8); |
| 183 |
|
| 184 |
return err; |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
/* Perform the unwrap algorithm W^-1 as specified by NIST SP 800-38F. |
| 189 |
Cipher block size must be 128-bit. */ |
| 190 |
static gcry_err_code_t |
| 191 |
unwrap (gcry_cipher_hd_t c, byte *outbuf, const byte *inbuf, size_t inbuflen) |
| 192 |
{ |
| 193 |
int j, x; |
| 194 |
size_t n, i; |
| 195 |
unsigned char *r, *a, *b; |
| 196 |
unsigned char t[8]; |
| 197 |
unsigned int burn, nburn; |
| 198 |
|
| 199 |
#if MAX_BLOCKSIZE < 8 |
| 200 |
#error Invalid block size |
| 201 |
#endif |
| 202 |
/* We require a cipher with a 128 bit block length. */ |
| 203 |
if (c->spec->blocksize != 16) |
| 204 |
return GPG_ERR_INV_LENGTH; |
| 205 |
|
| 206 |
/* Input data must be multiple of 64 bits. */ |
| 207 |
if (inbuflen % 8) |
| 208 |
return GPG_ERR_INV_ARG; |
| 209 |
|
| 210 |
n = inbuflen / 8; |
| 211 |
|
| 212 |
/* We need at least three 64 bit blocks. */ |
| 213 |
if (n < 3) |
| 214 |
return GPG_ERR_INV_ARG; |
| 215 |
|
| 216 |
burn = 0; |
| 217 |
|
| 218 |
r = outbuf; |
| 219 |
a = c->lastiv; /* We use c->LASTIV as buffer for A. */ |
| 220 |
b = c->u_ctr.ctr; /* B is also used to concatenate stuff. */ |
| 221 |
|
| 222 |
/* Copy the inbuf to the outbuf and save A. */ |
| 223 |
memcpy (a, inbuf, 8); |
| 224 |
memmove (r, inbuf+8, inbuflen-8); |
| 225 |
n--; /* Reduce to actual number of data blocks. */ |
| 226 |
|
| 227 |
/* t := 6 * n */ |
| 228 |
i = n * 6; /* The range is valid because: n = inbuflen / 8 - 1. */ |
| 229 |
for (x=0; x < 8 && x < sizeof (i); x++) |
| 230 |
t[7-x] = i >> (8*x); |
| 231 |
for (; x < 8; x++) |
| 232 |
t[7-x] = 0; |
| 233 |
|
| 234 |
for (j = 5; j >= 0; j--) |
| 235 |
{ |
| 236 |
for (i = n; i >= 1; i--) |
| 237 |
{ |
| 238 |
/* B := CIPH_k^-1( (A ^ t)| R[i] ) */ |
| 239 |
cipher_block_xor (b, a, t, 8); |
| 240 |
memcpy (b+8, r+(i-1)*8, 8); |
| 241 |
nburn = c->spec->decrypt (&c->context.c, b, b); |
| 242 |
burn = nburn > burn ? nburn : burn; |
| 243 |
/* t := t - 1 */ |
| 244 |
for (x = 7; x >= 0; x--) |
| 245 |
if (--t[x] != 0xff) |
| 246 |
break; |
| 247 |
/* A := MSB_64(B) */ |
| 248 |
memcpy (a, b, 8); |
| 249 |
/* R[i] := LSB_64(B) */ |
| 250 |
memcpy (r+(i-1)*8, b+8, 8); |
| 251 |
} |
| 252 |
} |
| 253 |
wipememory (b, 16); /* Clear scratch area. */ |
| 254 |
|
| 255 |
if (burn > 0) |
| 256 |
_gcry_burn_stack (burn + 4 * sizeof(void *)); |
| 257 |
|
| 258 |
return 0; |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
/* Perform the Key Unwrap algorithm as specified by RFC3394 and |
| 263 |
RFC5649. */ |
| 264 |
gcry_err_code_t |
| 265 |
_gcry_cipher_keywrap_decrypt_auto (gcry_cipher_hd_t c, |
| 266 |
byte *outbuf, size_t outbuflen, |
| 267 |
const byte *inbuf, size_t inbuflen) |
| 268 |
{ |
| 269 |
gcry_err_code_t err; |
| 270 |
|
| 271 |
/* We require a cipher with a 128 bit block length. */ |
| 272 |
if (c->spec->blocksize != 16) |
| 273 |
return GPG_ERR_INV_LENGTH; |
| 274 |
|
| 275 |
/* The output buffer must be able to hold the input data minus one |
| 276 |
additional block. Fixme: The caller has more restrictive checks |
| 277 |
- we may want to fix them for this mode. */ |
| 278 |
if (outbuflen + 8 < inbuflen) |
| 279 |
return GPG_ERR_BUFFER_TOO_SHORT; |
| 280 |
/* Input data must be multiple of 64 bits. */ |
| 281 |
if (inbuflen % 8) |
| 282 |
return GPG_ERR_INV_ARG; |
| 283 |
|
| 284 |
if (inbuflen == 16) |
| 285 |
{ |
| 286 |
unsigned int burn; |
| 287 |
unsigned char t[16]; |
| 288 |
|
| 289 |
if (!(c->flags & GCRY_CIPHER_EXTENDED)) |
| 290 |
return GPG_ERR_BUFFER_TOO_SHORT; |
| 291 |
|
| 292 |
burn = c->spec->decrypt (&c->context.c, t, inbuf); |
| 293 |
if (burn > 0) |
| 294 |
_gcry_burn_stack (burn + 4 * sizeof(void *)); |
| 295 |
|
| 296 |
if (memcmp (t, icv2, 4)) |
| 297 |
err = GPG_ERR_CHECKSUM; |
| 298 |
else |
| 299 |
{ |
| 300 |
unsigned int plen = (t[4]<<24) | (t[5]<<16) | (t[6]<<8) | t[7]; |
| 301 |
|
| 302 |
err = 0; |
| 303 |
if (plen >= 8) |
| 304 |
err = GPG_ERR_CHECKSUM; |
| 305 |
else if (plen) |
| 306 |
{ |
| 307 |
int i; |
| 308 |
|
| 309 |
for (i = 0; i < 16 - (8+plen); i++) |
| 310 |
if (t[8+plen+i]) |
| 311 |
{ |
| 312 |
err = GPG_ERR_CHECKSUM; |
| 313 |
break; |
| 314 |
} |
| 315 |
if (!err) |
| 316 |
{ |
| 317 |
memcpy (outbuf, t+8, 8); |
| 318 |
memcpy (c->u_mode.wrap.plen, t+4, 4); |
| 319 |
} |
| 320 |
} |
| 321 |
} |
| 322 |
} |
| 323 |
else |
| 324 |
{ |
| 325 |
/* We need at least three 64 bit blocks. */ |
| 326 |
if ((inbuflen / 8) < 3) |
| 327 |
return GPG_ERR_INV_ARG; |
| 328 |
|
| 329 |
err = unwrap (c, outbuf, inbuf, inbuflen); |
| 330 |
if (!err) |
| 331 |
{ |
| 332 |
unsigned char *a; |
| 333 |
|
| 334 |
a = c->lastiv; /* We use c->LASTIV as buffer for A. */ |
| 335 |
|
| 336 |
/* If an IV has been set we compare against this Alternative Initial |
| 337 |
Value; if it has not been set we compare against the standard IV. */ |
| 338 |
if (c->marks.iv && !memcmp (a, c->u_iv.iv, 8)) |
| 339 |
memset (c->u_mode.wrap.plen, 0, 4); |
| 340 |
else if (!memcmp (a, icv2, 4)) /* It's a packet wrapped by KWP. */ |
| 341 |
{ |
| 342 |
unsigned int plen = (a[4]<<24) | (a[5]<<16) | (a[6]<<8) | a[7]; |
| 343 |
int padlen = inbuflen - 8 - plen; |
| 344 |
|
| 345 |
if (!(c->flags & GCRY_CIPHER_EXTENDED)) |
| 346 |
err = GPG_ERR_CHECKSUM; |
| 347 |
else if (padlen < 0 || padlen > 7) |
| 348 |
err = GPG_ERR_CHECKSUM; |
| 349 |
else if (padlen) |
| 350 |
{ |
| 351 |
int i; |
| 352 |
|
| 353 |
for (i = 0; i < padlen; i++) |
| 354 |
if (outbuf[plen+i]) |
| 355 |
{ |
| 356 |
err = GPG_ERR_CHECKSUM; |
| 357 |
break; |
| 358 |
} |
| 359 |
} |
| 360 |
if (!err) |
| 361 |
memcpy (c->u_mode.wrap.plen, a+4, 4); |
| 362 |
} |
| 363 |
else /* It's a packet wrapped by KW. */ |
| 364 |
{ |
| 365 |
int i; |
| 366 |
|
| 367 |
for (i = 0; i < 8; i++) |
| 368 |
if (a[i] != 0xa6) |
| 369 |
{ |
| 370 |
err = GPG_ERR_CHECKSUM; |
| 371 |
break; |
| 372 |
} |
| 373 |
if (!err) |
| 374 |
memset (c->u_mode.wrap.plen, 0, 4); |
| 375 |
} |
| 376 |
} |
| 377 |
} |
| 378 |
|
| 379 |
return err; |
| 380 |
} |