fat-x86_64.c 6.56 KB
Newer Older
1
/* fat-x86_64.c
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

   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/.
*/

32 33
#define _GNU_SOURCE

34 35 36 37 38 39 40
#if HAVE_CONFIG_H
# include "config.h"
#endif

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
41
#include <string.h>
42 43 44 45

#include "nettle-types.h"

#include "aes-internal.h"
46 47
#include "memxor.h"
#include "fat-setup.h"
48

49 50
void _nettle_cpuid (uint32_t input, uint32_t regs[4]);

51 52 53 54
struct x86_features
{
  enum x86_vendor { X86_OTHER, X86_INTEL, X86_AMD } vendor;
  int have_aesni;
55
  int have_sha_ni;
56 57 58 59 60 61 62 63 64 65 66 67 68 69
};

#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_x86_features (struct x86_features *features)
{
  const char *s;
  features->vendor = X86_OTHER;
  features->have_aesni = 0;
70
  features->have_sha_ni = 0;
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88

  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, "vendor:", 7))
	  {
	    if (MATCH(s, length, "intel", 5))
	      features->vendor = X86_INTEL;
	    else if (MATCH(s, length, "amd", 3))
	      features->vendor = X86_AMD;
	    
	  }
	else if (MATCH (s, length, "aesni", 5))
	  features->have_aesni = 1;
89 90
	else if (MATCH (s, length, "sha_ni", 6))
	  features->have_sha_ni = 1;
91 92
	if (!sep)
	  break;
93
	s = sep + 1;
94 95 96 97 98 99 100 101 102 103 104 105
      }
  else
    {
      uint32_t cpuid_data[4];
      _nettle_cpuid (0, cpuid_data);
      if (memcmp (cpuid_data + 1, "Genu" "ntel" "ineI", 12) == 0)
	features->vendor = X86_INTEL;
      else if (memcmp (cpuid_data + 1, "Auth" "cAMD" "enti", 12) == 0)
	features->vendor = X86_AMD;

      _nettle_cpuid (1, cpuid_data);
      if (cpuid_data[2] & 0x02000000)
106 107 108 109 110
       features->have_aesni = 1;

      _nettle_cpuid (7, cpuid_data);
      if (cpuid_data[1] & 0x20000000)
       features->have_sha_ni = 1;
111 112 113
    }
}

114 115 116
DECLARE_FAT_FUNC(_nettle_aes_encrypt, aes_crypt_internal_func)
DECLARE_FAT_FUNC_VAR(aes_encrypt, aes_crypt_internal_func, x86_64)
DECLARE_FAT_FUNC_VAR(aes_encrypt, aes_crypt_internal_func, aesni)
117

118 119 120
DECLARE_FAT_FUNC(_nettle_aes_decrypt, aes_crypt_internal_func)
DECLARE_FAT_FUNC_VAR(aes_decrypt, aes_crypt_internal_func, x86_64)
DECLARE_FAT_FUNC_VAR(aes_decrypt, aes_crypt_internal_func, aesni)
121

122 123 124
DECLARE_FAT_FUNC(nettle_memxor, memxor_func)
DECLARE_FAT_FUNC_VAR(memxor, memxor_func, x86_64)
DECLARE_FAT_FUNC_VAR(memxor, memxor_func, sse2)
125

126
DECLARE_FAT_FUNC(nettle_sha1_compress, sha1_compress_func)
127 128 129
DECLARE_FAT_FUNC_VAR(sha1_compress, sha1_compress_func, x86_64)
DECLARE_FAT_FUNC_VAR(sha1_compress, sha1_compress_func, sha_ni)

130 131 132 133
DECLARE_FAT_FUNC(_nettle_sha256_compress, sha256_compress_func)
DECLARE_FAT_FUNC_VAR(sha256_compress, sha256_compress_func, x86_64)
DECLARE_FAT_FUNC_VAR(sha256_compress, sha256_compress_func, sha_ni)

134 135 136 137
/* This function should usually be called only once, at startup. But
   it is idempotent, and on x86, pointer updates are atomic, so
   there's no danger if it is called simultaneously from multiple
   threads. */
138
static void CONSTRUCTOR
139 140
fat_init (void)
{
141
  struct x86_features features;
142 143 144
  int verbose;

  /* FIXME: Replace all getenv calls by getenv_secure? */
145
  verbose = getenv (ENV_VERBOSE) != NULL;
146 147 148
  if (verbose)
    fprintf (stderr, "libnettle: fat library initialization.\n");

149
  get_x86_features (&features);
150
  if (verbose)
151 152 153
    {
      const char * const vendor_names[3] =
	{ "other", "intel", "amd" };
154
      fprintf (stderr, "libnettle: cpu features: vendor:%s%s%s\n",
155
	       vendor_names[features.vendor],
156 157
	       features.have_aesni ? ",aesni" : "",
	       features.have_sha_ni ? ",sha_ni" : "");
158 159
    }
  if (features.have_aesni)
160 161
    {
      if (verbose)
162
	fprintf (stderr, "libnettle: using aes instructions.\n");
163 164
      _nettle_aes_encrypt_vec = _nettle_aes_encrypt_aesni;
      _nettle_aes_decrypt_vec = _nettle_aes_decrypt_aesni;
165 166 167 168
    }
  else
    {
      if (verbose)
169
	fprintf (stderr, "libnettle: not using aes instructions.\n");
170 171
      _nettle_aes_encrypt_vec = _nettle_aes_encrypt_x86_64;
      _nettle_aes_decrypt_vec = _nettle_aes_decrypt_x86_64;
172
    }
173

174 175 176 177
  if (features.have_sha_ni)
    {
      if (verbose)
	fprintf (stderr, "libnettle: using sha_ni instructions.\n");
178
      nettle_sha1_compress_vec = _nettle_sha1_compress_sha_ni;
179
      _nettle_sha256_compress_vec = _nettle_sha256_compress_sha_ni;
180 181 182 183 184
    }
  else
    {
      if (verbose)
	fprintf (stderr, "libnettle: not using sha_ni instructions.\n");
185
      nettle_sha1_compress_vec = _nettle_sha1_compress_x86_64;
186
      _nettle_sha256_compress_vec = _nettle_sha256_compress_x86_64;
187
    }
188
  if (features.vendor == X86_INTEL)
189 190
    {
      if (verbose)
191
	fprintf (stderr, "libnettle: intel SSE2 will be used for memxor.\n");
192
      nettle_memxor_vec = _nettle_memxor_sse2;
193 194 195 196
    }
  else
    {
      if (verbose)
197
	fprintf (stderr, "libnettle: intel SSE2 will not be used for memxor.\n");
198
      nettle_memxor_vec = _nettle_memxor_x86_64;
199
    }
200 201
}

202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
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_memxor, void *,
		(void *dst, const void *src, size_t n),
		(dst, src, n))
219

220
DEFINE_FAT_FUNC(nettle_sha1_compress, void,
221 222
		(uint32_t *state, const uint8_t *input),
		(state, input))
223 224 225 226

DEFINE_FAT_FUNC(_nettle_sha256_compress, void,
		(uint32_t *state, const uint8_t *input, const uint32_t *k),
		(state, input, k))