Coverage Report

Created: 2024-06-28 06:39

/src/nettle-with-libgmp/camellia128-set-encrypt-key.c
Line
Count
Source
1
/* camellia128-set-encrypt-key.c
2
3
   Key setup for the camellia block cipher.
4
5
   Copyright (C) 2006,2007 NTT
6
   (Nippon Telegraph and Telephone Corporation).
7
8
   Copyright (C) 2010, 2013 Niels Möller
9
10
   This file is part of GNU Nettle.
11
12
   GNU Nettle is free software: you can redistribute it and/or
13
   modify it under the terms of either:
14
15
     * the GNU Lesser General Public License as published by the Free
16
       Software Foundation; either version 3 of the License, or (at your
17
       option) any later version.
18
19
   or
20
21
     * the GNU General Public License as published by the Free
22
       Software Foundation; either version 2 of the License, or (at your
23
       option) any later version.
24
25
   or both in parallel, as here.
26
27
   GNU Nettle is distributed in the hope that it will be useful,
28
   but WITHOUT ANY WARRANTY; without even the implied warranty of
29
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
30
   General Public License for more details.
31
32
   You should have received copies of the GNU General Public License and
33
   the GNU Lesser General Public License along with this program.  If
34
   not, see http://www.gnu.org/licenses/.
35
*/
36
37
/*
38
 * Algorithm Specification 
39
 *  http://info.isl.ntt.co.jp/crypt/eng/camellia/specifications.html
40
 */
41
42
/* Based on camellia.c ver 1.2.0, see
43
   http://info.isl.ntt.co.jp/crypt/eng/camellia/dl/camellia-LGPL-1.2.0.tar.gz.
44
 */
45
#if HAVE_CONFIG_H
46
# include "config.h"
47
#endif
48
49
#include <assert.h>
50
#include <limits.h>
51
52
#include "camellia-internal.h"
53
54
#include "macros.h"
55
56
void
57
camellia128_set_encrypt_key (struct camellia128_ctx *ctx,
58
           const uint8_t *key)
59
295
{
60
295
  uint64_t k0, k1;
61
62
295
  uint64_t subkey[_CAMELLIA128_NKEYS + 2];
63
295
  uint64_t w;
64
65
295
  k0 = READ_UINT64(key);
66
295
  k1 = READ_UINT64(key +  8);
67
  
68
  /**
69
   * generate KL dependent subkeys
70
   */
71
295
  subkey[0] = k0; subkey[1] = k1;
72
295
  ROTL128(15, k0, k1);
73
295
  subkey[4] = k0; subkey[5] = k1;
74
295
  ROTL128(30, k0, k1);
75
295
  subkey[10] = k0; subkey[11] = k1;
76
295
  ROTL128(15, k0, k1);
77
295
  subkey[13] = k1;
78
295
  ROTL128(17, k0, k1);
79
295
  subkey[16] = k0; subkey[17] = k1;
80
295
  ROTL128(17, k0, k1);
81
295
  subkey[18] = k0; subkey[19] = k1;
82
295
  ROTL128(17, k0, k1);
83
295
  subkey[22] = k0; subkey[23] = k1;
84
85
  /* generate KA. D1 is k0, d2 is k1. */
86
  /* FIXME: Make notation match the spec better. */
87
  /* For the 128-bit case, KR = 0, the construction of KA reduces to:
88
89
     D1 = KL >> 64;
90
     W = KL & MASK64;
91
     D2 = F(D1, Sigma1);
92
     W = D2 ^ W
93
     D1 = F(W, Sigma2)
94
     D2 = D2 ^ F(D1, Sigma3);
95
     D1 = D1 ^ F(D2, Sigma4);
96
     KA = (D1 << 64) | D2;
97
  */
98
295
  k0 = subkey[0]; w = subkey[1];
99
295
  CAMELLIA_F(k0, SIGMA1, k1);
100
295
  w ^= k1;
101
295
  CAMELLIA_F(w, SIGMA2, k0);
102
295
  CAMELLIA_F(k0, SIGMA3, w);
103
295
  k1 ^= w;
104
295
  CAMELLIA_F(k1, SIGMA4, w);
105
295
  k0 ^= w;
106
107
  /* generate KA dependent subkeys */
108
295
  subkey[2] = k0; subkey[3] = k1;
109
295
  ROTL128(15, k0, k1);
110
295
  subkey[6] = k0; subkey[7] = k1;
111
295
  ROTL128(15, k0, k1);
112
295
  subkey[8] = k0; subkey[9] = k1;
113
295
  ROTL128(15, k0, k1);
114
295
  subkey[12] = k0;
115
295
  ROTL128(15, k0, k1);
116
295
  subkey[14] = k0; subkey[15] = k1;
117
295
  ROTL128(34, k0, k1);
118
295
  subkey[20] = k0; subkey[21] = k1;
119
295
  ROTL128(17, k0, k1);
120
295
  subkey[24] = k0; subkey[25] = k1;
121
122
  /* Common final processing */
123
295
  _nettle_camellia_absorb (_CAMELLIA128_NKEYS, ctx->keys, subkey);
124
295
}