| 1 |
/* cipher-siv.c - SIV implementation (RFC 5297) |
| 2 |
* Copyright (C) 2021 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 |
#include <errno.h> |
| 25 |
|
| 26 |
#include "g10lib.h" |
| 27 |
#include "cipher.h" |
| 28 |
#include "bufhelp.h" |
| 29 |
#include "./cipher-internal.h" |
| 30 |
|
| 31 |
|
| 32 |
static inline void |
| 33 |
s2v_double (unsigned char *d) |
| 34 |
{ |
| 35 |
u64 hi, lo, mask; |
| 36 |
|
| 37 |
hi = buf_get_be64(d + 0); |
| 38 |
lo = buf_get_be64(d + 8); |
| 39 |
|
| 40 |
mask = -(hi >> 63); |
| 41 |
hi = (hi << 1) ^ (lo >> 63); |
| 42 |
lo = (lo << 1) ^ (mask & 0x87); |
| 43 |
|
| 44 |
buf_put_be64(d + 0, hi); |
| 45 |
buf_put_be64(d + 8, lo); |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
static void |
| 50 |
s2v_pad (unsigned char *out, const byte *in, size_t inlen) |
| 51 |
{ |
| 52 |
static const unsigned char padding[GCRY_SIV_BLOCK_LEN] = { 0x80 }; |
| 53 |
|
| 54 |
gcry_assert(inlen < GCRY_SIV_BLOCK_LEN); |
| 55 |
|
| 56 |
buf_cpy (out, in, inlen); |
| 57 |
buf_cpy (out + inlen, padding, GCRY_SIV_BLOCK_LEN - inlen); |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
gcry_err_code_t |
| 62 |
_gcry_cipher_siv_setkey (gcry_cipher_hd_t c, |
| 63 |
const unsigned char *ctrkey, size_t ctrkeylen) |
| 64 |
{ |
| 65 |
static const unsigned char zero[GCRY_SIV_BLOCK_LEN] = { 0 }; |
| 66 |
gcry_err_code_t err; |
| 67 |
|
| 68 |
if (c->spec->blocksize != GCRY_SIV_BLOCK_LEN) |
| 69 |
return GPG_ERR_CIPHER_ALGO; |
| 70 |
|
| 71 |
c->u_mode.siv.aad_count = 0; |
| 72 |
c->u_mode.siv.dec_tag_set = 0; |
| 73 |
c->marks.tag = 0; |
| 74 |
c->marks.iv = 0; |
| 75 |
|
| 76 |
/* Set CTR mode key. */ |
| 77 |
err = c->spec->setkey (c->u_mode.siv.ctr_context, ctrkey, ctrkeylen, |
| 78 |
&c->bulk); |
| 79 |
if (err != 0) |
| 80 |
return err; |
| 81 |
|
| 82 |
/* Initialize S2V. */ |
| 83 |
memset (&c->u_mode.siv.s2v_cmac, 0, sizeof(c->u_mode.siv.s2v_cmac)); |
| 84 |
err = _gcry_cmac_generate_subkeys (c, &c->u_mode.siv.s2v_cmac); |
| 85 |
if (err != 0) |
| 86 |
return err; |
| 87 |
|
| 88 |
err = _gcry_cmac_write (c, &c->u_mode.siv.s2v_cmac, zero, GCRY_SIV_BLOCK_LEN); |
| 89 |
if (err != 0) |
| 90 |
return err; |
| 91 |
|
| 92 |
err = _gcry_cmac_final (c, &c->u_mode.siv.s2v_cmac); |
| 93 |
if (err != 0) |
| 94 |
return err; |
| 95 |
|
| 96 |
memcpy (c->u_mode.siv.s2v_zero_block, c->u_mode.siv.s2v_cmac.u_iv.iv, |
| 97 |
GCRY_SIV_BLOCK_LEN); |
| 98 |
memcpy (c->u_mode.siv.s2v_d, c->u_mode.siv.s2v_zero_block, |
| 99 |
GCRY_SIV_BLOCK_LEN); |
| 100 |
|
| 101 |
return 0; |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
gcry_err_code_t |
| 106 |
_gcry_cipher_siv_authenticate (gcry_cipher_hd_t c, |
| 107 |
const byte *aadbuf, size_t aadbuflen) |
| 108 |
{ |
| 109 |
gcry_err_code_t err; |
| 110 |
|
| 111 |
if (c->spec->blocksize != GCRY_SIV_BLOCK_LEN) |
| 112 |
return GPG_ERR_CIPHER_ALGO; |
| 113 |
if (c->marks.tag) |
| 114 |
return GPG_ERR_INV_STATE; |
| 115 |
if (c->marks.iv) |
| 116 |
return GPG_ERR_INV_STATE; |
| 117 |
|
| 118 |
if (c->u_mode.siv.aad_count >= 126) |
| 119 |
return GPG_ERR_INV_STATE; /* Too many AD vector components. */ |
| 120 |
|
| 121 |
c->u_mode.siv.aad_count++; |
| 122 |
|
| 123 |
_gcry_cmac_reset (&c->u_mode.siv.s2v_cmac); |
| 124 |
|
| 125 |
err = _gcry_cmac_write (c, &c->u_mode.siv.s2v_cmac, aadbuf, aadbuflen); |
| 126 |
if (err != 0) |
| 127 |
return err; |
| 128 |
|
| 129 |
err = _gcry_cmac_final (c, &c->u_mode.siv.s2v_cmac); |
| 130 |
if (err != 0) |
| 131 |
return err; |
| 132 |
|
| 133 |
s2v_double (c->u_mode.siv.s2v_d); |
| 134 |
cipher_block_xor_1 (c->u_mode.siv.s2v_d, c->u_mode.siv.s2v_cmac.u_iv.iv, |
| 135 |
GCRY_SIV_BLOCK_LEN); |
| 136 |
|
| 137 |
return 0; |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
gcry_err_code_t |
| 142 |
_gcry_cipher_siv_set_nonce (gcry_cipher_hd_t c, const byte *nonce, |
| 143 |
size_t noncelen) |
| 144 |
{ |
| 145 |
gcry_err_code_t err; |
| 146 |
|
| 147 |
err = _gcry_cipher_siv_authenticate (c, nonce, noncelen); |
| 148 |
if (err) |
| 149 |
return err; |
| 150 |
|
| 151 |
/* Nonce is the last AD before plaintext. */ |
| 152 |
c->marks.iv = 1; |
| 153 |
|
| 154 |
return 0; |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
static gcry_err_code_t |
| 159 |
s2v_plaintext (gcry_cipher_hd_t c, const byte *plain, size_t plainlen) |
| 160 |
{ |
| 161 |
gcry_err_code_t err; |
| 162 |
|
| 163 |
if (c->u_mode.siv.aad_count >= 127) |
| 164 |
return GPG_ERR_INV_STATE; /* Too many AD vector components. */ |
| 165 |
|
| 166 |
_gcry_cmac_reset (&c->u_mode.siv.s2v_cmac); |
| 167 |
|
| 168 |
if (plainlen >= GCRY_SIV_BLOCK_LEN) |
| 169 |
{ |
| 170 |
err = _gcry_cmac_write (c, &c->u_mode.siv.s2v_cmac, plain, |
| 171 |
plainlen - GCRY_SIV_BLOCK_LEN); |
| 172 |
if (err) |
| 173 |
return err; |
| 174 |
|
| 175 |
cipher_block_xor_1 (c->u_mode.siv.s2v_d, |
| 176 |
plain + plainlen - GCRY_SIV_BLOCK_LEN, |
| 177 |
GCRY_SIV_BLOCK_LEN); |
| 178 |
|
| 179 |
err = _gcry_cmac_write (c, &c->u_mode.siv.s2v_cmac, c->u_mode.siv.s2v_d, |
| 180 |
GCRY_SIV_BLOCK_LEN); |
| 181 |
if (err) |
| 182 |
return err; |
| 183 |
} |
| 184 |
else |
| 185 |
{ |
| 186 |
unsigned char pad_sn[GCRY_SIV_BLOCK_LEN]; |
| 187 |
|
| 188 |
s2v_double (c->u_mode.siv.s2v_d); |
| 189 |
s2v_pad (pad_sn, plain, plainlen); |
| 190 |
cipher_block_xor_1 (pad_sn, c->u_mode.siv.s2v_d, GCRY_SIV_BLOCK_LEN); |
| 191 |
|
| 192 |
err = _gcry_cmac_write (c, &c->u_mode.siv.s2v_cmac, pad_sn, |
| 193 |
GCRY_SIV_BLOCK_LEN); |
| 194 |
wipememory (pad_sn, sizeof(pad_sn)); |
| 195 |
if (err) |
| 196 |
return err; |
| 197 |
} |
| 198 |
|
| 199 |
c->u_mode.siv.aad_count++; |
| 200 |
|
| 201 |
return _gcry_cmac_final (c, &c->u_mode.siv.s2v_cmac); |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
gcry_err_code_t |
| 206 |
_gcry_cipher_siv_encrypt (gcry_cipher_hd_t c, |
| 207 |
byte *outbuf, size_t outbuflen, |
| 208 |
const byte *inbuf, size_t inbuflen) |
| 209 |
{ |
| 210 |
gcry_err_code_t err; |
| 211 |
u64 q_lo; |
| 212 |
|
| 213 |
if (c->spec->blocksize != GCRY_SIV_BLOCK_LEN) |
| 214 |
return GPG_ERR_CIPHER_ALGO; |
| 215 |
if (outbuflen < inbuflen) |
| 216 |
return GPG_ERR_BUFFER_TOO_SHORT; |
| 217 |
if (c->marks.tag) |
| 218 |
return GPG_ERR_INV_STATE; |
| 219 |
if (c->u_mode.siv.dec_tag_set) |
| 220 |
return GPG_ERR_INV_STATE; |
| 221 |
|
| 222 |
/* Pass plaintext to S2V. */ |
| 223 |
err = s2v_plaintext (c, inbuf, inbuflen); |
| 224 |
if (err != 0) |
| 225 |
return err; |
| 226 |
|
| 227 |
/* Clear 31th and 63th bits. */ |
| 228 |
memcpy (c->u_ctr.ctr, c->u_mode.siv.s2v_cmac.u_iv.iv, GCRY_SIV_BLOCK_LEN); |
| 229 |
q_lo = buf_get_be64(c->u_ctr.ctr + 8); |
| 230 |
q_lo &= ~((u64)1 << 31); |
| 231 |
q_lo &= ~((u64)1 << 63); |
| 232 |
buf_put_be64(c->u_ctr.ctr + 8, q_lo); |
| 233 |
|
| 234 |
/* Encrypt plaintext. */ |
| 235 |
err = _gcry_cipher_ctr_encrypt_ctx(c, outbuf, outbuflen, inbuf, inbuflen, |
| 236 |
c->u_mode.siv.ctr_context); |
| 237 |
if (err != 0) |
| 238 |
return err; |
| 239 |
|
| 240 |
c->marks.tag = 1; |
| 241 |
|
| 242 |
return 0; |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
gcry_err_code_t |
| 247 |
_gcry_cipher_siv_set_decryption_tag (gcry_cipher_hd_t c, |
| 248 |
const byte *tag, size_t taglen) |
| 249 |
{ |
| 250 |
if (taglen != GCRY_SIV_BLOCK_LEN) |
| 251 |
return GPG_ERR_INV_ARG; |
| 252 |
if (c->spec->blocksize != GCRY_SIV_BLOCK_LEN) |
| 253 |
return GPG_ERR_CIPHER_ALGO; |
| 254 |
if (c->marks.tag) |
| 255 |
return GPG_ERR_INV_STATE; |
| 256 |
|
| 257 |
memcpy (&c->u_mode.siv.dec_tag, tag, GCRY_SIV_BLOCK_LEN); |
| 258 |
c->u_mode.siv.dec_tag_set = 1; |
| 259 |
|
| 260 |
return 0; |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
gcry_err_code_t |
| 265 |
_gcry_cipher_siv_decrypt (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 |
u64 q_lo; |
| 271 |
|
| 272 |
if (c->spec->blocksize != GCRY_SIV_BLOCK_LEN) |
| 273 |
return GPG_ERR_CIPHER_ALGO; |
| 274 |
if (outbuflen < inbuflen) |
| 275 |
return GPG_ERR_BUFFER_TOO_SHORT; |
| 276 |
if (c->marks.tag) |
| 277 |
return GPG_ERR_INV_STATE; |
| 278 |
if (!c->u_mode.siv.dec_tag_set) |
| 279 |
return GPG_ERR_INV_STATE; |
| 280 |
|
| 281 |
/* Clear 31th and 63th bits. */ |
| 282 |
memcpy (c->u_ctr.ctr, c->u_mode.siv.dec_tag, GCRY_SIV_BLOCK_LEN); |
| 283 |
q_lo = buf_get_be64(c->u_ctr.ctr + 8); |
| 284 |
q_lo &= ~((u64)1 << 31); |
| 285 |
q_lo &= ~((u64)1 << 63); |
| 286 |
buf_put_be64(c->u_ctr.ctr + 8, q_lo); |
| 287 |
|
| 288 |
/* Decrypt ciphertext. */ |
| 289 |
err = _gcry_cipher_ctr_encrypt_ctx(c, outbuf, outbuflen, inbuf, inbuflen, |
| 290 |
c->u_mode.siv.ctr_context); |
| 291 |
if (err != 0) |
| 292 |
return err; |
| 293 |
|
| 294 |
/* Pass plaintext to S2V. */ |
| 295 |
err = s2v_plaintext (c, outbuf, inbuflen); |
| 296 |
if (err != 0) |
| 297 |
return err; |
| 298 |
|
| 299 |
c->marks.tag = 1; |
| 300 |
|
| 301 |
if (!buf_eq_const(c->u_mode.siv.s2v_cmac.u_iv.iv, c->u_mode.siv.dec_tag, |
| 302 |
GCRY_SIV_BLOCK_LEN)) |
| 303 |
{ |
| 304 |
wipememory (outbuf, inbuflen); |
| 305 |
return GPG_ERR_CHECKSUM; |
| 306 |
} |
| 307 |
|
| 308 |
return 0; |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
gcry_err_code_t |
| 313 |
_gcry_cipher_siv_get_tag (gcry_cipher_hd_t c, unsigned char *outbuf, |
| 314 |
size_t outbuflen) |
| 315 |
{ |
| 316 |
gcry_err_code_t err; |
| 317 |
|
| 318 |
if (c->spec->blocksize != GCRY_SIV_BLOCK_LEN) |
| 319 |
return GPG_ERR_CIPHER_ALGO; |
| 320 |
if (c->u_mode.siv.dec_tag_set) |
| 321 |
return GPG_ERR_INV_STATE; |
| 322 |
|
| 323 |
if (!c->marks.tag) |
| 324 |
{ |
| 325 |
/* Finalize SIV with zero-length plaintext. */ |
| 326 |
err = s2v_plaintext (c, NULL, 0); |
| 327 |
if (err != 0) |
| 328 |
return err; |
| 329 |
|
| 330 |
c->marks.tag = 1; |
| 331 |
} |
| 332 |
|
| 333 |
if (outbuflen > GCRY_SIV_BLOCK_LEN) |
| 334 |
outbuflen = GCRY_SIV_BLOCK_LEN; |
| 335 |
|
| 336 |
/* We already checked that OUTBUF is large enough to hold |
| 337 |
* the result or has valid truncated length. */ |
| 338 |
memcpy (outbuf, c->u_mode.siv.s2v_cmac.u_iv.iv, outbuflen); |
| 339 |
|
| 340 |
return 0; |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
gcry_err_code_t |
| 345 |
_gcry_cipher_siv_check_tag (gcry_cipher_hd_t c, const unsigned char *intag, |
| 346 |
size_t taglen) |
| 347 |
{ |
| 348 |
gcry_err_code_t err; |
| 349 |
size_t n; |
| 350 |
|
| 351 |
if (c->spec->blocksize != GCRY_SIV_BLOCK_LEN) |
| 352 |
return GPG_ERR_CIPHER_ALGO; |
| 353 |
|
| 354 |
if (!c->marks.tag) |
| 355 |
{ |
| 356 |
/* Finalize SIV with zero-length plaintext. */ |
| 357 |
err = s2v_plaintext (c, NULL, 0); |
| 358 |
if (err != 0) |
| 359 |
return err; |
| 360 |
|
| 361 |
c->marks.tag = 1; |
| 362 |
} |
| 363 |
|
| 364 |
n = GCRY_SIV_BLOCK_LEN; |
| 365 |
if (taglen < n) |
| 366 |
n = taglen; |
| 367 |
|
| 368 |
if (!buf_eq_const(c->u_mode.siv.s2v_cmac.u_iv.iv, intag, n) |
| 369 |
|| GCRY_SIV_BLOCK_LEN != taglen) |
| 370 |
{ |
| 371 |
return GPG_ERR_CHECKSUM; |
| 372 |
} |
| 373 |
|
| 374 |
return 0; |
| 375 |
} |