summaryrefslogtreecommitdiff
path: root/fat-arm.c
blob: 8133ca695418e4834b4dc3ca2a4b7a1be3ac929d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/* fat-arm.c

   Copyright (C) 2015 Niels Möller

   This file is part of GNU Nettle.

   GNU Nettle is free software: you can redistribute it and/or
   modify it under the terms of either:

     * the GNU Lesser General Public License as published by the Free
       Software Foundation; either version 3 of the License, or (at your
       option) any later version.

   or

     * the GNU General Public License as published by the Free
       Software Foundation; either version 2 of the License, or (at your
       option) any later version.

   or both in parallel, as here.

   GNU Nettle is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received copies of the GNU General Public License and
   the GNU Lesser General Public License along with this program.  If
   not, see http://www.gnu.org/licenses/.
*/

#define _GNU_SOURCE

#if HAVE_CONFIG_H
# include "config.h"
#endif

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "nettle-types.h"

#include "aes-internal.h"
#include "chacha-internal.h"
#include "salsa20-internal.h"
#include "fat-setup.h"

struct arm_features
{
  /* /proc/cpuinfo "CPU Architecture" doesn't correspond exactly to
     ARM architecture version, but it's good enough for our purposes.
     Will be set to 5, 6, 7 or 8. */
  unsigned arch_version;
  int have_neon;
};

#define SKIP(s, slen, literal, llen)				\
  (((slen) >= (llen) && memcmp ((s), (literal), llen) == 0)	\
   ? ((slen) -= (llen), (s) += (llen), 1) : 0)
#define MATCH(s, slen, literal, llen)				\
  ((slen) == (llen) && memcmp ((s), (literal), llen) == 0)

static void
get_arm_features (struct arm_features *features)
{
  const char *s;
  features->arch_version = 5;
  features->have_neon = 0;

  s = secure_getenv (ENV_OVERRIDE);
  if (s)
    for (;;)
      {
	const char *sep = strchr (s, ',');
	size_t length = sep ? (size_t) (sep - s) : strlen(s);

	if (SKIP (s, length, "arch:", 5))
	  {
	    if (length == 1 && *s >= '0' && *s <= '9')
	      features->arch_version = *s - '0';
	  }
	else if (MATCH (s, length, "neon", 4))
	  features->have_neon = 1;
	if (!sep)
	  break;
	s = sep + 1;
      }
  else
    {
      FILE *f;
      char line[200];
      int seen_arch = 0;
      int seen_features = 0;

      f = fopen ("/proc/cpuinfo", "r");
      if (!f)
	return;
      while (seen_features + seen_arch < 2
	     && fgets (line, sizeof(line), f))
	{
	  char *sep;
	  char *p;
	  sep = strchr (line, ':');
	  if (!sep)
	    continue;
	  for (p = sep; p - line > 0 && p[-1] == '\t'; p--)
	    ;

	  *p = '\0';
	  p = sep+1;

	  if (strcmp (line, "Features") == 0)
	    {
	      features->have_neon = (strstr (p, " neon ") != NULL);
	      seen_features = 1;
	    }
	  else if (strcmp (line, "CPU architecture") == 0)
	    {
	      /* Don't use strtol, since it's locale dependent. */
	      while (p[0] == ' ')
		p++;
	      if (p[0] > '5' && p[0] <= '9')
		features->arch_version = p[0] - '0';
	      else if (strcmp (p, "AArch64") == 0)
		features->arch_version = 8;
	      seen_arch = 1;
	    }
	}
      if (features->arch_version >= 8)
	{
	  /* Neon is not required, and maybe not listed in feature flags */
	  features->have_neon = 1;
	}
      fclose (f);
    }
}

DECLARE_FAT_FUNC(_nettle_aes_encrypt, aes_crypt_internal_func)
DECLARE_FAT_FUNC_VAR(aes_encrypt, aes_crypt_internal_func, arm)
DECLARE_FAT_FUNC_VAR(aes_encrypt, aes_crypt_internal_func, armv6)

DECLARE_FAT_FUNC(_nettle_aes_decrypt, aes_crypt_internal_func)
DECLARE_FAT_FUNC_VAR(aes_decrypt, aes_crypt_internal_func, arm)
DECLARE_FAT_FUNC_VAR(aes_decrypt, aes_crypt_internal_func, armv6)

DECLARE_FAT_FUNC(_nettle_salsa20_crypt, salsa20_crypt_func)
DECLARE_FAT_FUNC_VAR(salsa20_crypt, salsa20_crypt_func, 1core)
DECLARE_FAT_FUNC_VAR(salsa20_crypt, salsa20_crypt_func, 2core)

DECLARE_FAT_FUNC(nettle_sha1_compress, sha1_compress_func)
DECLARE_FAT_FUNC_VAR(sha1_compress, sha1_compress_func, c)
DECLARE_FAT_FUNC_VAR(sha1_compress, sha1_compress_func, armv6)

DECLARE_FAT_FUNC(_nettle_sha256_compress_n, sha256_compress_n_func)
DECLARE_FAT_FUNC_VAR(sha256_compress_n, sha256_compress_n_func, c)
DECLARE_FAT_FUNC_VAR(sha256_compress_n, sha256_compress_n_func, armv6)

DECLARE_FAT_FUNC(_nettle_sha512_compress, sha512_compress_func)
DECLARE_FAT_FUNC_VAR(sha512_compress, sha512_compress_func, c)
DECLARE_FAT_FUNC_VAR(sha512_compress, sha512_compress_func, neon)

DECLARE_FAT_FUNC(nettle_sha3_permute, sha3_permute_func)
DECLARE_FAT_FUNC_VAR(sha3_permute, sha3_permute_func, c)
DECLARE_FAT_FUNC_VAR(sha3_permute, sha3_permute_func, neon)

DECLARE_FAT_FUNC(_nettle_umac_nh, umac_nh_func)
DECLARE_FAT_FUNC_VAR(umac_nh, umac_nh_func, c);
DECLARE_FAT_FUNC_VAR(umac_nh, umac_nh_func, neon);

DECLARE_FAT_FUNC(_nettle_umac_nh_n, umac_nh_n_func)
DECLARE_FAT_FUNC_VAR(umac_nh_n, umac_nh_n_func, c);
DECLARE_FAT_FUNC_VAR(umac_nh_n, umac_nh_n_func, neon);

DECLARE_FAT_FUNC(nettle_chacha_crypt, chacha_crypt_func)
DECLARE_FAT_FUNC_VAR(chacha_crypt, chacha_crypt_func, 1core)
DECLARE_FAT_FUNC_VAR(chacha_crypt, chacha_crypt_func, 3core)

DECLARE_FAT_FUNC(nettle_chacha_crypt32, chacha_crypt_func)
DECLARE_FAT_FUNC_VAR(chacha_crypt32, chacha_crypt_func, 1core)
DECLARE_FAT_FUNC_VAR(chacha_crypt32, chacha_crypt_func, 3core)

static void CONSTRUCTOR
fat_init (void)
{
  struct arm_features features;
  int verbose;

  get_arm_features (&features);

  verbose = getenv (ENV_VERBOSE) != NULL;
  if (verbose)
    fprintf (stderr, "libnettle: cpu features: arch:%d%s\n",
	     features.arch_version,
	     features.have_neon ? ",neon" : "");

  if (features.arch_version >= 6)
    {
      if (verbose)
	fprintf (stderr, "libnettle: enabling armv6 code.\n");
      _nettle_aes_encrypt_vec = _nettle_aes_encrypt_armv6;
      _nettle_aes_decrypt_vec = _nettle_aes_decrypt_armv6;
      nettle_sha1_compress_vec = _nettle_sha1_compress_armv6;
      _nettle_sha256_compress_n_vec = _nettle_sha256_compress_n_armv6;
    }
  else
    {
      if (verbose)
	fprintf (stderr, "libnettle: not enabling armv6 code.\n");
      _nettle_aes_encrypt_vec = _nettle_aes_encrypt_arm;
      _nettle_aes_decrypt_vec = _nettle_aes_decrypt_arm;
      nettle_sha1_compress_vec = _nettle_sha1_compress_c;
      _nettle_sha256_compress_n_vec = _nettle_sha256_compress_n_c;
    }
  if (features.have_neon)
    {
      if (verbose)
	fprintf (stderr, "libnettle: enabling neon code.\n");
      _nettle_salsa20_crypt_vec = _nettle_salsa20_crypt_2core;
      _nettle_sha512_compress_vec = _nettle_sha512_compress_neon;
      nettle_sha3_permute_vec = _nettle_sha3_permute_neon;
      _nettle_umac_nh_vec = _nettle_umac_nh_neon;
      _nettle_umac_nh_n_vec = _nettle_umac_nh_n_neon;
      nettle_chacha_crypt_vec = _nettle_chacha_crypt_3core;
      nettle_chacha_crypt32_vec = _nettle_chacha_crypt32_3core;
    }
  else
    {
      if (verbose)
	fprintf (stderr, "libnettle: not enabling neon code.\n");
      _nettle_salsa20_crypt_vec = _nettle_salsa20_crypt_1core;
      _nettle_sha512_compress_vec = _nettle_sha512_compress_c;
      nettle_sha3_permute_vec = _nettle_sha3_permute_c;
      _nettle_umac_nh_vec = _nettle_umac_nh_c;
      _nettle_umac_nh_n_vec = _nettle_umac_nh_n_c;
      nettle_chacha_crypt_vec = _nettle_chacha_crypt_1core;
      nettle_chacha_crypt32_vec = _nettle_chacha_crypt32_1core;
    }
}
  
DEFINE_FAT_FUNC(_nettle_aes_encrypt, void,
		(unsigned rounds, const uint32_t *keys,
		 const struct aes_table *T,
		 size_t length, uint8_t *dst,
		 const uint8_t *src),
		(rounds, keys, T, length, dst, src))

DEFINE_FAT_FUNC(_nettle_aes_decrypt, void,
		(unsigned rounds, const uint32_t *keys,
		 const struct aes_table *T,
		 size_t length, uint8_t *dst,
		 const uint8_t *src),
		(rounds, keys, T, length, dst, src))

DEFINE_FAT_FUNC(_nettle_salsa20_crypt, void,
		(struct salsa20_ctx *ctx, unsigned rounds,
		 size_t length, uint8_t *dst,
		 const uint8_t *src),
		(ctx, rounds, length, dst, src))

DEFINE_FAT_FUNC(nettle_sha1_compress, void,
		(uint32_t *state, const uint8_t *input),
		(state, input))

DEFINE_FAT_FUNC(_nettle_sha256_compress_n, const uint8_t *,
		(uint32_t *state, const uint32_t *k,
		 size_t blocks, const uint8_t *input),
		(state, k, blocks, input))

DEFINE_FAT_FUNC(_nettle_sha512_compress, void,
		(uint64_t *state, const uint8_t *input, const uint64_t *k),
		(state, input, k))

DEFINE_FAT_FUNC(nettle_sha3_permute, void,
		(struct sha3_state *state), (state))

DEFINE_FAT_FUNC(_nettle_umac_nh, uint64_t,
		(const uint32_t *key, unsigned length, const uint8_t *msg),
		(key, length, msg))

DEFINE_FAT_FUNC(_nettle_umac_nh_n, void,
		(uint64_t *out, unsigned n, const uint32_t *key,
		 unsigned length, const uint8_t *msg),
		(out, n, key, length, msg))

DEFINE_FAT_FUNC(nettle_chacha_crypt, void,
		(struct chacha_ctx *ctx,
		 size_t length,
		 uint8_t *dst,
		 const uint8_t *src),
		(ctx, length, dst, src))

DEFINE_FAT_FUNC(nettle_chacha_crypt32, void,
		(struct chacha_ctx *ctx,
		 size_t length,
		 uint8_t *dst,
		 const uint8_t *src),
		(ctx, length, dst, src))