Hash :
b2a551e0
Author :
Date :
2005-10-08T09:26:59
2005-10-08 Simon Josefsson <jas@extundo.com> * gc.h: Add gc_hash and gc_hash_buffer. * gc-gnulib.c (gc_hash_buffer): Add. Reorder #include's. * gc-libgcrypt.c (gc_hash_buffer): Add.
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
/* gc-gl-common.c --- Common gnulib internal crypto interface functions
* Copyright (C) 2002, 2003, 2004, 2005 Simon Josefsson
*
* This file is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published
* by the Free Software Foundation; either version 2, or (at your
* option) any later version.
*
* This file 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 a copy of the GNU General Public License
* along with this file; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*
*/
/* Note: This file is only built if GC uses internal functions. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
/* Get prototype. */
#include <gc.h>
#include <stdlib.h>
#include <string.h>
/* For randomize. */
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include "md5.h"
#include "hmac.h"
int
gc_init (void)
{
return 0;
}
void
gc_done (void)
{
return;
}
/* Randomness. */
static int
randomize (int level, char *data, size_t datalen)
{
int fd;
const char *device;
size_t len = 0;
int rc;
switch (level)
{
case 0:
device = NAME_OF_NONCE_DEVICE;
break;
case 1:
device = NAME_OF_PSEUDO_RANDOM_DEVICE;
break;
default:
device = NAME_OF_RANDOM_DEVICE;
break;
}
fd = open (device, O_RDONLY);
if (fd < 0)
return GC_RANDOM_ERROR;
do
{
ssize_t tmp;
tmp = read (fd, data, datalen);
if (tmp < 0)
{
int save_errno = errno;
close (fd);
errno = save_errno;
return GC_RANDOM_ERROR;
}
len += tmp;
}
while (len < datalen);
rc = close (fd);
if (rc < 0)
return GC_RANDOM_ERROR;
return GC_OK;
}
int
gc_nonce (char *data, size_t datalen)
{
return randomize (0, data, datalen);
}
int
gc_pseudo_random (char *data, size_t datalen)
{
return randomize (1, data, datalen);
}
int
gc_random (char *data, size_t datalen)
{
return randomize (2, data, datalen);
}
/* Memory allocation. */
void
gc_set_allocators (gc_malloc_t func_malloc,
gc_malloc_t secure_malloc,
gc_secure_check_t secure_check,
gc_realloc_t func_realloc, gc_free_t func_free)
{
return;
}
/* Hashes. */
int
gc_hash_buffer (int hash, const void *in, size_t inlen, char *resbuf)
{
switch (hash)
{
case GC_MD5:
md5_buffer (in, inlen, resbuf);
break;
default:
return GC_INVALID_HASH;
}
return GC_OK;
}
int
gc_md5 (const void *in, size_t inlen, void *resbuf)
{
md5_buffer (in, inlen, resbuf);
return 0;
}
int
gc_hmac_md5 (const void *key, size_t keylen,
const void *in, size_t inlen, char *resbuf)
{
hmac_md5 (key, keylen, in, inlen, resbuf);
return 0;
}