Coverage Report

Created: 2025-03-06 07:58

/src/gnutls/lib/nettle/gost/acpkm.c
Line
Count
Source (jump to first uncovered line)
1
/* acpkm.c
2
3
   The R 1323565.1.017-2018 cipher function. See draft-irtf-cfrg-re-keying.
4
5
   Copyright (C) 2018 Dmitry Eremin-Solenikov
6
7
   This file is part of GNU Nettle.
8
9
   GNU Nettle is free software: you can redistribute it and/or
10
   modify it under the terms of either:
11
12
     * the GNU Lesser General Public License as published by the Free
13
       Software Foundation; either version 3 of the License, or (at your
14
       option) any later version.
15
16
   or
17
18
     * the GNU General Public License as published by the Free
19
       Software Foundation; either version 2 of the License, or (at your
20
       option) any later version.
21
22
   or both in parallel, as here.
23
24
   GNU Nettle is distributed in the hope that it will be useful,
25
   but WITHOUT ANY WARRANTY; without even the implied warranty of
26
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
27
   General Public License for more details.
28
29
   You should have received copies of the GNU General Public License and
30
   the GNU Lesser General Public License along with this program.  If
31
   not, see https://www.gnu.org/licenses/.
32
*/
33
34
#if HAVE_CONFIG_H
35
#include "config.h"
36
#endif
37
38
#include "acpkm.h"
39
40
static uint8_t acpkm_mesh_data[ACPKM_KEY_SIZE] = {
41
  0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a,
42
  0x8b, 0x8c, 0x8d, 0x8e, 0x8f, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95,
43
  0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
44
};
45
46
void acpkm_crypt(struct acpkm_ctx *ctx, void *cipher,
47
     nettle_cipher_func *encrypt, nettle_set_key_func *set_key,
48
     size_t length, uint8_t *dst, const uint8_t *src)
49
0
{
50
0
  size_t N = ctx->N;
51
0
  size_t part;
52
0
  uint8_t new_key[ACPKM_KEY_SIZE];
53
54
  /* Less than a block, no rekeying */
55
0
  if (ctx->pos + length < N) {
56
0
    encrypt(cipher, length, dst, src);
57
0
    ctx->pos += length;
58
0
    return;
59
0
  }
60
61
0
  for (part = N - ctx->pos; length >= part; part = N) {
62
0
    encrypt(cipher, part, dst, src);
63
0
    src += part;
64
0
    dst += part;
65
0
    length -= part;
66
67
    /* Rekey */
68
0
    encrypt(cipher, ACPKM_KEY_SIZE, new_key, acpkm_mesh_data);
69
0
    set_key(cipher, new_key);
70
0
  }
71
72
0
  if (length != 0)
73
0
    encrypt(cipher, length, dst, src);
74
75
0
  ctx->pos = length;
76
0
}