Re: [TLS] New Cached info draft

Simon Josefsson <simon@josefsson.org> Tue, 30 March 2010 08:41 UTC

Return-Path: <simon@josefsson.org>
X-Original-To: tls@core3.amsl.com
Delivered-To: tls@core3.amsl.com
Received: from localhost (localhost [127.0.0.1]) by core3.amsl.com (Postfix) with ESMTP id D97A13A698A for <tls@core3.amsl.com>; Tue, 30 Mar 2010 01:41:21 -0700 (PDT)
X-Virus-Scanned: amavisd-new at amsl.com
X-Spam-Flag: NO
X-Spam-Score: -0.608
X-Spam-Level:
X-Spam-Status: No, score=-0.608 tagged_above=-999 required=5 tests=[AWL=-1.739, BAYES_50=0.001, DNS_FROM_OPENWHOIS=1.13]
Received: from mail.ietf.org ([64.170.98.32]) by localhost (core3.amsl.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id nAgo3u2NgeS0 for <tls@core3.amsl.com>; Tue, 30 Mar 2010 01:41:20 -0700 (PDT)
Received: from yxa-v.extundo.com (yxa-v.extundo.com [83.241.177.39]) by core3.amsl.com (Postfix) with ESMTP id EC1FC3A6915 for <tls@ietf.org>; Tue, 30 Mar 2010 01:41:19 -0700 (PDT)
Received: from mocca (c80-216-24-99.bredband.comhem.se [80.216.24.99]) (authenticated bits=0) by yxa-v.extundo.com (8.14.3/8.14.3/Debian-5+lenny1) with ESMTP id o2U8fdHw028553 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES128-SHA bits=128 verify=NOT); Tue, 30 Mar 2010 10:41:41 +0200
From: Simon Josefsson <simon@josefsson.org>
To: Stefan Santesson <stefan@aaa-sec.com>
References: <4BB0CB91.9060306@extendedsubset.com> <C7D71EA5.9B22%stefan@aaa-sec.com>
OpenPGP: id=B565716F; url=http://josefsson.org/key.txt
X-Hashcash: 1:22:100330:stefan@aaa-sec.com::mS65hp5uhujdfB8B:6uaz
X-Hashcash: 1:22:100330:marsh@extendedsubset.com::425wsFPb5Cro1VQP:3C42
X-Hashcash: 1:22:100330:tls@ietf.org::7UnaUJEGnSrsbKwY:8kzk
X-Hashcash: 1:22:100330:agl@imperialviolet.org::kqEpmGX3KXAwBisZ:AStC
Date: Tue, 30 Mar 2010 10:41:39 +0200
In-Reply-To: <C7D71EA5.9B22%stefan@aaa-sec.com> (Stefan Santesson's message of "Tue, 30 Mar 2010 03:21:09 +0100")
Message-ID: <877houyzek.fsf@mocca.josefsson.org>
User-Agent: Gnus/5.110011 (No Gnus v0.11) Emacs/23.1 (gnu/linux)
MIME-Version: 1.0
Content-Type: text/plain; charset="us-ascii"
X-Virus-Scanned: clamav-milter 0.95.3 at yxa-v
X-Virus-Status: Clean
Cc: Adam Langley <agl@imperialviolet.org>, "tls@ietf.org" <tls@ietf.org>
Subject: Re: [TLS] New Cached info draft
X-BeenThere: tls@ietf.org
X-Mailman-Version: 2.1.9
Precedence: list
List-Id: "This is the mailing list for the Transport Layer Security working group of the IETF." <tls.ietf.org>
List-Unsubscribe: <https://www.ietf.org/mailman/listinfo/tls>, <mailto:tls-request@ietf.org?subject=unsubscribe>
List-Archive: <http://www.ietf.org/mail-archive/web/tls>
List-Post: <mailto:tls@ietf.org>
List-Help: <mailto:tls-request@ietf.org?subject=help>
List-Subscribe: <https://www.ietf.org/mailman/listinfo/tls>, <mailto:tls-request@ietf.org?subject=subscribe>
X-List-Received-Date: Tue, 30 Mar 2010 08:41:21 -0000

Stefan Santesson <stefan@aaa-sec.com> writes:

> Thanks Marsh,
>
> I implemented the pseudocode in Java and and obtained matching output.

I have implemented it in C independently from the pseudocode, and I also
got the same outputs.  My test code prints similar output like Marsh
code.

> I'm considering including this code sample in the draft.
> I would gratefully receive a matching C sample.

I believe it is important to have sample code and test vectors in the
document.  I'm donating the code below.

/Simon

fnv1a64.h:

#ifndef FNV1A64_H
# define FNV1A64_H

#include <string.h> /* For size_t */
#include <stdint.h> /* For uint64_t */

extern uint64_t fnv1a64 (const uint8_t *buffer, size_t len);

#endif

fnv1a64.c:

/* fnv1a.c -- Implementation of the FNV-1A non-cryptographic hash function.
 * Written by Simon Josefsson <simon@josefsson.org> on 2010-03-30.
 */

#include "fnv1a64.h"

#define FNV1A64_OFFSET_BASIS 14695981039346656037ULL
#define FNV1A64_PRIME 1099511628211ULL

uint64_t
fnv1a64 (const uint8_t *buffer, size_t len)
{
  uint64_t hash;
  size_t i;

  hash = FNV1A64_OFFSET_BASIS;
  for (i = 0; i < len; i++)
    {
      hash = hash ^ buffer[i];
      hash = hash * FNV1A64_PRIME;
    }

  return hash;
}

fnv1a64_test.c:

#include "fnv1a64.h"

#include <stdio.h> /* For printf */

static void
hash_buffer (const uint8_t *buffer, size_t len)
{
  uint64_t hash;
  size_t i;

  puts ("For input data:");
  for (i = 0; i < len; i++)
    {
      if ((i % 16) == 0)
	printf ("%04x  ", i);
      printf ("%02X ", buffer[i]);
      if ((i % 16) == 15)
	printf ("\n");
    }
  if (len > 0 && (i % 16) != 15)
    printf ("\n");
  printf ("%d bytes\n", len);

  hash = fnv1a64 (buffer, len);

  printf ("Digest is: ");
  for (i = 0; i < sizeof (hash); i++)
    printf ("%02X ", (int) (hash >> (8 * (sizeof (hash) - i -1))) & 0xFF);
  printf ("\n\n");
}

int
main (void)
{
  hash_buffer ((uint8_t *) "", 0);
  hash_buffer ((uint8_t *) "a", 1);
  hash_buffer ((uint8_t *) "b", 1);
  hash_buffer ((uint8_t *) "c", 1);
  hash_buffer ((uint8_t *) "d", 1);
  hash_buffer ((uint8_t *) "e", 1);
  hash_buffer ((uint8_t *) "f", 1);
  hash_buffer ((uint8_t *) "fo", 2);
  hash_buffer ((uint8_t *) "foo", 3);
  hash_buffer ((uint8_t *) "foob", 4);
  hash_buffer ((uint8_t *) "fooba", 5);
  hash_buffer ((uint8_t *) "foobar", 6);
  hash_buffer ((uint8_t *) "\xFF\x00\x00\x01", 4);
  hash_buffer ((uint8_t *) "http://en.wikipedia.org/wiki/"
	       "Fowler_Noll_Vo_hash_buffer", 48);

  return 0;
}