3
* Miscellaneous functions used by the example programs.
6
/* nettle, low-level cryptographics library
8
* Copyright (C) 2002 Niels M�ller
10
* The nettle library is free software; you can redistribute it and/or modify
11
* it under the terms of the GNU Lesser General Public License as published by
12
* the Free Software Foundation; either version 2.1 of the License, or (at your
13
* option) any later version.
15
* The nettle library is distributed in the hope that it will be useful, but
16
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18
* License for more details.
20
* You should have received a copy of the GNU Lesser General Public License
21
* along with the nettle library; see the file COPYING.LIB. If not, write to
22
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
35
#define RANDOM_DEVICE "/dev/urandom"
43
void *p = malloc(size);
46
fprintf(stderr, "Virtual memory exhausted.\n");
54
werror(const char *format, ...)
59
va_start(args, format);
60
vfprintf(stderr, format, args);
65
#define MIN(a,b) (((a) < (b)) ? (a) : (b))
68
read_file(const char *name, unsigned max_size, char **contents)
75
f = fopen(name, "rb");
81
if (max_size && max_size < 100)
86
for (size = 100, done = 0;
87
(!max_size || done < max_size) && !feof(f);
92
if (max_size && size > max_size)
95
/* Space for terminating NUL */
96
p = realloc(buffer, size + 1);
108
done += fread(buffer + done, 1, size - done, f);
116
/* NUL-terminate the data. */
124
write_file(const char *name, unsigned size, const char *buffer)
126
FILE *f = fopen(name, "wb");
132
res = fwrite(buffer, 1, size, f);
137
return fclose(f) == 0 && res > 0;
141
write_string(FILE *f, unsigned size, const char *buffer)
143
size_t res = fwrite(buffer, 1, size, f);
149
simple_random(struct yarrow256_ctx *ctx, const char *name)
155
length = read_file(name, 0, &buffer);
157
length = read_file(RANDOM_DEVICE, 20, &buffer);
162
yarrow256_seed(ctx, length, buffer);
168
hash_file(const struct nettle_hash *hash, void *ctx, FILE *f)
172
char buffer[BUFSIZE];
173
size_t res = fread(buffer, 1, sizeof(buffer), f);
177
hash->update(ctx, res, buffer);
185
read_rsa_key(const char *name,
186
struct rsa_public_key *pub,
187
struct rsa_private_key *priv)
193
length = read_file(name, 0, &buffer);
197
res = rsa_keypair_from_sexp(pub, priv, 0, length, buffer);
202
#endif /* WITH_PUBLIC_KEY */