Coverage Report

Created: 2026-03-31 06:37

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/nettle/arcfour.c
Line
Count
Source
1
/* arcfour.c
2
3
   The arcfour/rc4 stream cipher.
4
5
   Copyright (C) 2001, 2014 Niels Möller
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 http://www.gnu.org/licenses/.
32
*/
33
34
#if HAVE_CONFIG_H
35
# include "config.h"
36
#endif
37
38
#include <assert.h>
39
40
#include "arcfour.h"
41
42
259k
#define SWAP(a,b) do { int _t = a; a = b; b = _t; } while(0)
43
44
void
45
arcfour_set_key(struct arcfour_ctx *ctx,
46
    size_t length, const uint8_t *key)
47
1.01k
{
48
1.01k
  unsigned i, j, k;
49
  
50
1.01k
  assert(length >= ARCFOUR_MIN_KEY_SIZE);
51
1.01k
  assert(length <= ARCFOUR_MAX_KEY_SIZE);
52
53
  /* Initialize context */
54
260k
  for (i = 0; i<256; i++)
55
259k
    ctx->S[i] = i;
56
57
260k
  for (i = j = k = 0; i<256; i++)
58
259k
    {
59
259k
      j += ctx->S[i] + key[k]; j &= 0xff;
60
259k
      SWAP(ctx->S[i], ctx->S[j]);
61
      /* Repeat key as needed */
62
259k
      k = (k + 1) % length;
63
259k
    }
64
1.01k
  ctx->i = ctx->j = 0;
65
1.01k
}
66
67
void
68
arcfour128_set_key(struct arcfour_ctx *ctx, const uint8_t *key)
69
0
{
70
0
  arcfour_set_key (ctx, ARCFOUR128_KEY_SIZE, key);
71
0
}
72
73
void
74
arcfour_crypt(struct arcfour_ctx *ctx,
75
        size_t length, uint8_t *dst,
76
        const uint8_t *src)
77
1.01k
{
78
1.01k
  register uint8_t i, j;
79
1.01k
  register int si, sj;
80
81
1.01k
  i = ctx->i; j = ctx->j;
82
130k
  while(length--)
83
129k
    {
84
129k
      i++; i &= 0xff;
85
129k
      si = ctx->S[i];
86
129k
      j += si; j &= 0xff;
87
129k
      sj = ctx->S[i] = ctx->S[j];
88
129k
      ctx->S[j] = si;
89
129k
      *dst++ = *src++ ^ ctx->S[ (si + sj) & 0xff ];
90
129k
    }
91
1.01k
  ctx->i = i; ctx->j = j;
92
1.01k
}