gcm.c 7.05 KB
Newer Older
Nikos Mavrogiannopoulos's avatar
Nikos Mavrogiannopoulos committed
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
/* gcm.h
 *
 * Galois counter mode, specified by NIST,
 * http://csrc.nist.gov/publications/nistpubs/800-38D/SP-800-38D.pdf
 *
 */

/* NOTE: Tentative interface, subject to change. No effort will be
   made to avoid incompatible changes. */

/* nettle, low-level cryptographics library
 *
 * Copyright (C) 2011 Niels Möller
 * Copyright (C) 2011 Katholieke Universiteit Leuven
 * 
 * Contributed by Nikos Mavrogiannopoulos
 *
 * A few functions copied from Tom S. Dennis' libtomcrypt, which is in
 * the public domain.
 *
 * The nettle library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or (at your
 * option) any later version.
 * 
 * The nettle library 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 Lesser General Public
 * License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with the nettle library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 * MA 02111-1307, USA.
 */

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

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

#include "gcm.h"

#include "memxor.h"
#include "nettle-internal.h"
#include "macros.h"

51 52
/* Big-endian shift right. The argument must be properly aligned for
   word accesses. */
Nikos Mavrogiannopoulos's avatar
Nikos Mavrogiannopoulos committed
53
static void
54
gcm_rightshift (uint8_t *a)
Nikos Mavrogiannopoulos's avatar
Nikos Mavrogiannopoulos committed
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
  unsigned long *w = (unsigned long *) a;

  /* Shift uses big-endian representation. */
#if WORDS_BIGENDIAN
# if SIZEOF_LONG == 4
  w[3] = (w[3] >> 1) | ((w[2] & 1) << 31);
  w[2] = (w[2] >> 1) | ((w[1] & 1) << 31);
  w[1] = (w[1] >> 1) | ((w[0] & 1) << 31);
  w[0] = (w[0] >> 1);
# elif SIZEOF_LONG == 8
  w[1] = (w[1] >> 1) | ((w[0] & 1) << 63);
  w[0] = (w[0] >> 1);
# else
#  error Unsupported word size. */
#endif
#else /* ! WORDS_BIGENDIAN */
# if SIZEOF_LONG == 4
#define RSHIFT_WORD(x) \
  ((((x) & 0xfefefefeUL) >> 1) \
   | (((x) & 0x01010101) << 15))
  w[3] = RSHIFT_WORD(w[3]) | ((w[2] >> 17) & 0x80);
  w[2] = RSHIFT_WORD(w[2]) | ((w[1] >> 17) & 0x80);
  w[1] = RSHIFT_WORD(w[1]) | ((w[0] >> 17) & 0x80);
  w[0] = RSHIFT_WORD(w[0]);
# elif SIZEOF_LONG == 8
#define RSHIFT_WORD(x) \
  ((((x) & 0xfefefefefefefefeUL) >> 1) \
   | (((x) & 0x0101010101010101UL) << 15))
  w[1] = RSHIFT_WORD(w[1]) | ((w[0] >> 49) & 0x80);
  w[0] = RSHIFT_WORD(w[0]);
# else
#  error Unsupported word size. */
# endif
#endif /* ! WORDS_BIGENDIAN */
Nikos Mavrogiannopoulos's avatar
Nikos Mavrogiannopoulos committed
90
}
91 92 93 94
/* The function gcm_gf_mul was copied from
 * libtomcrypt, which is under public domain. 
 * Written by Tom S. Dennis.
 */
Nikos Mavrogiannopoulos's avatar
Nikos Mavrogiannopoulos committed
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

/**
  GCM GF multiplier (internal use only) bitserial
  @param a   First value (and destination)
  @param b   Second value
 */
static void
gcm_gf_mul (uint8_t * a, const uint8_t * b)
{
  static const uint8_t mask[] =
    { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 };
  static const uint8_t poly[] = { 0x00, 0xE1 };

  uint8_t Z[16], V[16];
  unsigned x, z;

  memset (Z, 0, 16);
  memcpy (V, a, 16);
  for (x = 0; x < 128; x++)
    {
      if (b[x >> 3] & mask[x & 7])
        {
          memxor (Z, V, 16);
        }
      z = V[15] & 0x01;
      gcm_rightshift (V);
      V[0] ^= poly[z];
    }
  memcpy (a, Z, 16);
}

/* Increment the rightmost 32 bits. */
#define INC32(block) INCREMENT(4, (block) + GCM_BLOCK_SIZE - 4)

/* Initialization of GCM.
 * @ctx: The context of GCM
 * @cipher: The context of the underlying block cipher
 * @f: The underlying cipher encryption function
 */
void
gcm_set_key(struct gcm_ctx *ctx,
	    void *cipher, nettle_crypt_func f)
{
  memset (ctx->h, 0, sizeof (ctx->h));
  f (cipher, GCM_BLOCK_SIZE, ctx->h, ctx->h);  /* H */
#if GCM_TABLE_BITS
  /* FIXME: Expand hash subkey */
  abort();
#endif
}

/*
 * @length: The size of the iv (fixed for now to GCM_NONCE_SIZE)
 * @iv: The iv
 */
void
gcm_set_iv(struct gcm_ctx *ctx, unsigned length, const uint8_t* iv)
{
  /* FIXME: remove the iv size limitation */
  assert (length == GCM_IV_SIZE);

  memcpy (ctx->iv, iv, GCM_BLOCK_SIZE - 4);
  ctx->iv[GCM_BLOCK_SIZE - 4] = 0;
  ctx->iv[GCM_BLOCK_SIZE - 3] = 0;
  ctx->iv[GCM_BLOCK_SIZE - 2] = 0;
  ctx->iv[GCM_BLOCK_SIZE - 1] = 1;

  memcpy (ctx->ctr, ctx->iv, GCM_BLOCK_SIZE);
  INC32 (ctx->ctr);

  /* Reset the rest of the message-dependent state. */
  memset(ctx->x, 0, sizeof(ctx->x));
  ctx->auth_size = ctx->data_size = 0;
}

static void
gcm_hash(struct gcm_ctx *ctx, unsigned length, const uint8_t *data)
{
  for (; length >= GCM_BLOCK_SIZE;
       length -= GCM_BLOCK_SIZE, data += GCM_BLOCK_SIZE)
    {
      memxor (ctx->x, data, GCM_BLOCK_SIZE);
      gcm_gf_mul (ctx->x, ctx->h);
    }
  if (length > 0)
    {
      memxor (ctx->x, data, length);
      gcm_gf_mul (ctx->x, ctx->h);
    }
}

void
gcm_auth(struct gcm_ctx *ctx,
	 unsigned length, const uint8_t *data)
{
  assert(ctx->auth_size % GCM_BLOCK_SIZE == 0);
  assert(ctx->data_size % GCM_BLOCK_SIZE == 0);

  gcm_hash(ctx, length, data);

  ctx->auth_size += length;
}

static void
gcm_crypt(struct gcm_ctx *ctx, void *cipher, nettle_crypt_func *f,
	  unsigned length,
	   uint8_t *dst, const uint8_t *src)
{
  uint8_t buffer[GCM_BLOCK_SIZE];

  if (src != dst)
    {
      for (; length >= GCM_BLOCK_SIZE;
           (length -= GCM_BLOCK_SIZE,
	    src += GCM_BLOCK_SIZE, dst += GCM_BLOCK_SIZE))
        {
          f (cipher, GCM_BLOCK_SIZE, dst, ctx->ctr);
          memxor (dst, src, GCM_BLOCK_SIZE);
          INC32 (ctx->ctr);
        }
    }
  else
    {
      for (; length >= GCM_BLOCK_SIZE;
           (length -= GCM_BLOCK_SIZE,
	    src += GCM_BLOCK_SIZE, dst += GCM_BLOCK_SIZE))
        {
          f (cipher, GCM_BLOCK_SIZE, buffer, ctx->ctr);
          memxor3 (dst, src, buffer, GCM_BLOCK_SIZE);
          INC32 (ctx->ctr);
        }
    }
  if (length > 0)
    {
      /* A final partial block */
      f (cipher, GCM_BLOCK_SIZE, buffer, ctx->ctr);
      memxor3 (dst, src, buffer, length);
      INC32 (ctx->ctr);
    }
}

void
gcm_encrypt (struct gcm_ctx *ctx, void *cipher, nettle_crypt_func *f,
	     unsigned length,
             uint8_t *dst, const uint8_t *src)
{
  assert(ctx->data_size % GCM_BLOCK_SIZE == 0);

  gcm_crypt(ctx, cipher, f, length, dst, src);
  gcm_hash(ctx, length, dst);

  ctx->data_size += length;
}

void
gcm_decrypt(struct gcm_ctx *ctx, void *cipher, nettle_crypt_func *f,
	    unsigned length, uint8_t *dst, const uint8_t *src)
{
  assert(ctx->data_size % GCM_BLOCK_SIZE == 0);

  gcm_hash(ctx, length, src);
  gcm_crypt(ctx, cipher, f, length, dst, src);

  ctx->data_size += length;
}

void
gcm_digest(struct gcm_ctx *ctx, void *cipher, nettle_crypt_func *f,
	   unsigned length, uint8_t *digest)
{
  uint8_t buffer[GCM_BLOCK_SIZE];

  assert (length <= GCM_BLOCK_SIZE);

  ctx->data_size *= 8;
  ctx->auth_size *= 8;

  WRITE_UINT64 (buffer, ctx->auth_size);
  WRITE_UINT64 (buffer + 8, ctx->data_size);

  gcm_hash(ctx, GCM_BLOCK_SIZE, buffer);

  f (cipher, GCM_BLOCK_SIZE, buffer, ctx->iv);
  memxor3 (digest, ctx->x, buffer, length);

  return;
}