Blame hash_nss.c

Packit 96c956
/*
Packit 96c956
  chronyd/chronyc - Programs for keeping computer clocks accurate.
Packit 96c956
Packit 96c956
 **********************************************************************
Packit 96c956
 * Copyright (C) Miroslav Lichvar  2012
Packit 96c956
 * 
Packit 96c956
 * This program is free software; you can redistribute it and/or modify
Packit 96c956
 * it under the terms of version 2 of the GNU General Public License as
Packit 96c956
 * published by the Free Software Foundation.
Packit 96c956
 * 
Packit 96c956
 * This program is distributed in the hope that it will be useful, but
Packit 96c956
 * WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 96c956
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 96c956
 * General Public License for more details.
Packit 96c956
 * 
Packit 96c956
 * You should have received a copy of the GNU General Public License along
Packit 96c956
 * with this program; if not, write to the Free Software Foundation, Inc.,
Packit 96c956
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
Packit 96c956
 * 
Packit 96c956
 **********************************************************************
Packit 96c956
Packit 96c956
  =======================================================================
Packit 96c956
Packit 96c956
  Routines implementing crypto hashing using NSSLOWHASH API of the NSS library.
Packit 96c956
Packit 96c956
  */
Packit 96c956
Packit 96c956
#include "config.h"
Packit 96c956
Packit 96c956
#include <nss.h>
Packit 96c956
#include <hasht.h>
Packit 96c956
#include <nsslowhash.h>
Packit 96c956
Packit 96c956
#include "hash.h"
Packit 96c956
#include "util.h"
Packit 96c956
Packit 96c956
static NSSLOWInitContext *ictx;
Packit 96c956
Packit 96c956
struct hash {
Packit 96c956
  HASH_HashType type;
Packit 96c956
  const char *name;
Packit 96c956
  NSSLOWHASHContext *context;
Packit 96c956
};
Packit 96c956
Packit 96c956
static struct hash hashes[] = {
Packit 96c956
  { HASH_AlgMD5, "MD5", NULL },
Packit 96c956
  { HASH_AlgSHA1, "SHA1", NULL },
Packit 96c956
  { HASH_AlgSHA256, "SHA256", NULL },
Packit 96c956
  { HASH_AlgSHA384, "SHA384", NULL },
Packit 96c956
  { HASH_AlgSHA512, "SHA512", NULL },
Packit 96c956
  { 0, NULL, NULL }
Packit 96c956
};
Packit 96c956
Packit 96c956
int
Packit 96c956
HSH_GetHashId(const char *name)
Packit 96c956
{
Packit 96c956
  int i;
Packit 96c956
Packit 96c956
  for (i = 0; hashes[i].name; i++) {
Packit 96c956
    if (!strcmp(name, hashes[i].name))
Packit 96c956
      break;
Packit 96c956
  }
Packit 96c956
Packit 96c956
  if (!hashes[i].name)
Packit 96c956
    return -1; /* not found */
Packit 96c956
Packit 96c956
  if (!ictx && !(ictx = NSSLOW_Init()))
Packit 96c956
    return -1; /* couldn't init NSS */
Packit 96c956
Packit 96c956
  if (!hashes[i].context &&
Packit 96c956
      !(hashes[i].context = NSSLOWHASH_NewContext(ictx, hashes[i].type)))
Packit 96c956
    return -1; /* couldn't init hash */
Packit 96c956
Packit 96c956
  return i;
Packit 96c956
}
Packit 96c956
Packit 96c956
unsigned int
Packit 96c956
HSH_Hash(int id, const unsigned char *in1, unsigned int in1_len,
Packit 96c956
    const unsigned char *in2, unsigned int in2_len,
Packit 96c956
    unsigned char *out, unsigned int out_len)
Packit 96c956
{
Packit 96c956
  unsigned char buf[MAX_HASH_LENGTH];
Packit 96c956
  unsigned int ret = 0;
Packit 96c956
Packit 96c956
  NSSLOWHASH_Begin(hashes[id].context);
Packit 96c956
  NSSLOWHASH_Update(hashes[id].context, in1, in1_len);
Packit 96c956
  if (in2)
Packit 96c956
    NSSLOWHASH_Update(hashes[id].context, in2, in2_len);
Packit 96c956
  NSSLOWHASH_End(hashes[id].context, buf, &ret, sizeof (buf));
Packit 96c956
Packit 96c956
  ret = MIN(ret, out_len);
Packit 96c956
  memcpy(out, buf, ret);
Packit 96c956
Packit 96c956
  return ret;
Packit 96c956
}
Packit 96c956
Packit 96c956
void
Packit 96c956
HSH_Finalise(void)
Packit 96c956
{
Packit 96c956
  int i;
Packit 96c956
Packit 96c956
  for (i = 0; hashes[i].name; i++) {
Packit 96c956
    if (hashes[i].context)
Packit 96c956
      NSSLOWHASH_Destroy(hashes[i].context);
Packit 96c956
  }
Packit 96c956
Packit 96c956
  if (ictx)
Packit 96c956
    NSSLOW_Shutdown(ictx);
Packit 96c956
}