summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNiels Möller <nisse@lysator.liu.se>2002-01-11 14:33:53 +0100
committerNiels Möller <nisse@lysator.liu.se>2002-01-11 14:33:53 +0100
commit7699f2d5609387e16461b46ad7687685e0d9a43b (patch)
tree8fe48a574156f3bc37d4bbee42716ead38208edb
parentcf5ba848d9c08b1d2725f0ee32da778766ec69bd (diff)
downloadnettle-7699f2d5609387e16461b46ad7687685e0d9a43b.tar.gz
New files.
Rev: src/nettle/hmac-md5.c:1.1 Rev: src/nettle/hmac.c:1.1 Rev: src/nettle/hmac.h:1.1 Rev: src/nettle/md5-meta.c:1.1 Rev: src/nettle/nettle-meta.h:1.1 Rev: src/nettle/sha1-meta.c:1.1 Rev: src/nettle/sha256-meta.c:1.1
-rw-r--r--hmac-md5.c47
-rw-r--r--hmac.c90
-rw-r--r--hmac.h177
-rw-r--r--md5-meta.c28
-rw-r--r--nettle-meta.h134
-rw-r--r--sha1-meta.c28
-rw-r--r--sha256-meta.c28
7 files changed, 532 insertions, 0 deletions
diff --git a/hmac-md5.c b/hmac-md5.c
new file mode 100644
index 00000000..76c502b3
--- /dev/null
+++ b/hmac-md5.c
@@ -0,0 +1,47 @@
+/* hmac.c
+ *
+ * HMAC message authentication code.
+ */
+
+/* nettle, low-level cryptographics library
+ *
+ * Copyright (C) 2001 Niels Möller
+ *
+ * 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.
+ */
+
+#include "hmac-md5.h"
+
+void
+hmac_md5_set_key(struct hmac_md5_ctx *ctx,
+ unsigned key_length, const uint8_t *key)
+{
+ HMAC_INIT(&hmac_md5_info, ctx, key_length, key);
+}
+
+void
+hmac_md5_update(struct hmac_md5_ctx *ctx,
+ unsigned length, const uint8_t *data)
+{
+ md5_update(&ctx->state, length, data);
+}
+
+void
+hmac_md5_digest(struct hmac_md5_ctx *ctx,
+ unsigned length, uint8_t *digest)
+{
+ HMAC_DIGEST(&hmac_md5_info, ctx, length, digest);
+}
diff --git a/hmac.c b/hmac.c
new file mode 100644
index 00000000..4ea273cd
--- /dev/null
+++ b/hmac.c
@@ -0,0 +1,90 @@
+/* hmac.c
+ *
+ * HMAC message authentication code.
+ */
+
+/* nettle, low-level cryptographics library
+ *
+ * Copyright (C) 2001 Niels Möller
+ *
+ * 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.
+ */
+
+#include "hmac.h"
+
+#include "memxor.h"
+
+#include <assert.h>
+
+#define IPAD 0x36
+#define OPAD 0x5c
+
+void
+hmac_init(void *outer, void *inner, void *state,
+ struct hmac_info *info,
+ unsigned key_length, const uint8_t *key)
+{
+ uint8_t pad = alloca(info->block_size);
+
+ info->init(outer);
+ info->init(inner);
+
+ if (length > info->block_size)
+ {
+ /* Reduce key to the algorithm's hash size. Use the area pointed
+ * to by state for the temporary state. */
+
+ uint8_t *digest = alloca(info->digest_size);
+
+ info->init(state);
+ info->update(state, key_length, key);
+ info->digest(state, info->digest_size, digest);
+
+ key = digest;
+ key_length = info->digest_size;
+ }
+
+ assert(key_size <= info->block_size);
+
+ memset(pad, OPAD, info->block_size);
+ memxor(pad, key, key_length);
+
+ info->update(outer, info->block_size, pad);
+
+ memset(pad, IPAD, info->block_size);
+ memxor(pad, key, key_length);
+
+ info->update(inner, info->block_size, pad);
+
+ memcpy(state, inner, info->ctx_size);
+}
+
+void
+hmac_digest(void *outer, void *inner, void *state
+ struct hmac_info *info,
+ unsigned length, uint8_t *dst)
+{
+ uint8_t *digest = alloca(info->digest_size);
+
+ info->digest(state, info->digest_size, digest);
+
+ memcpy(outer, state, info->ctx_size);
+
+ info->update(state, info->digest_size, digest);
+ info->digest(state, length, dst);
+
+ memcpy(state, inner, info->ctx_size);
+}
diff --git a/hmac.h b/hmac.h
new file mode 100644
index 00000000..03fb9103
--- /dev/null
+++ b/hmac.h
@@ -0,0 +1,177 @@
+/* hmac.h
+ *
+ * HMAC message authentication code.
+ */
+
+/* nettle, low-level cryptographics library
+ *
+ * Copyright (C) 2001 Niels Möller
+ *
+ * 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.
+ */
+
+#ifndef NETTLE_HMAC_H_INCLUDED
+#define NETTLE_HMAC_H_INCLUDED
+
+#include "nettle-meta.h"
+
+#include <inttypes.h>
+
+struct hmac_info
+{
+ /* Size of digests, both internal and the final MAC */
+ unsigned digest_size;
+
+ /* Internal block size */
+ unsigned block_size;
+
+ /* Size of the context struct for the underlying hash function. */
+ unsigned ctx_size;
+
+ /* Init function */
+ void (*init)(void *ctx);
+
+ /* Update function */
+ void (*update)(void *ctx,
+ unsigned length,
+ const uint8_t *data);
+
+ /* Digest extraction function */
+
+ void (*digest)(void *ctx,
+ unsigned length,
+ uint8_t *digest);
+};
+
+#define _HMAC_INFO(name, NAME)
+{
+ NAME##_DIGEST_SIZE,
+ NAME##_DATA_SIZE,
+ sizeof(struct name##_ctx);
+
+ (void (*)(void *ctx)) name##_init,
+ (void (*)(void *ctx,
+ unsigned length,
+ const uint8_t *data)) name##_update,
+ (void (*)(void *ctx,
+ unsigned length,
+ uint8_t *digest)) name##_digest
+}
+
+extern const struct hmac_info hmac_md5_info;
+extern const struct hmac_info hmac_sha1_info;
+
+#if 0
+void
+hmac_init(void *outer, void *inner, void *state,
+ struct hmac_info *info,
+ unsigned key_length, const uint8_t *key);
+
+void
+hmac_update(void *state, void (*update)(void *ctx,
+ unsigned length,
+ const uint8_t *data));
+void
+hmac_digest(void *outer, void *inner, void *state
+ struct hmac_info *info,
+ unsigned length, uint8_t *dst);
+#endif
+
+void
+hmac_set_key(void *outer, void *inner, void *state,
+ (void (*update)(void *ctx,
+ unsigned length,
+ const uint8_t *data))(update),
+ (void (*digest)(void *ctx,
+ unsigned length,
+ uint8_t *digest))(digest),
+ unsigned block_size, unsigned digest_size,
+ unsigned context_size,
+ unsigned key_length, const uint8_t *key);
+
+void
+hmac_digest(void *outer, void *inner, void *state,
+ (void (*update)(void *ctx,
+ unsigned length,
+ const uint8_t *data))(update),
+ (void (*digest)(void *ctx,
+ unsigned length,
+ uint8_t *digest))(digest),
+ unsigned digest_size,
+ unsigned context_size,
+ unsigned digest_length, uint8_t *digest);
+
+
+#define HMAC_CTX(type) \
+{ type outer; type inner; type state; }
+
+#define HMAC_INIT(ctx, init) \
+((init)((ctx)->outer), (init)((ctx)->inner), (init)((ctx)->state))
+
+#define HMAC_SET_KEY(ctx, update, digest, block_size, digest_size, length, key) \
+(0 ? ( (update)(ctx->outer, 0, NULL), (digest)(ctx->outer, 0, NULL)) \
+ : hmac_set_key( (void *) (ctx)->outer, \
+ (void *) (ctx)->inner, \
+ (void *) (ctx)->state, \
+ (void (*)(void *ctx, \
+ unsigned length, \
+ const uint8_t *data))(update), \
+ (void (*)(void *ctx, \
+ unsigned length, \
+ uint8_t *digest))(digest), \
+ (block_size), (digest_size), \
+ sizeof(*(ctx)->state), \
+ (length), (key)))
+
+#define HMAC_UPDATE(ctx, f, length, data) \
+((f)((ctx)->state, (length), (data)))
+
+#define HMAC_DIGEST(ctx, update, digest, digest_size, length, digest) \
+(0 ? ( (update)(ctx->outer, 0, NULL), (digest)(ctx->outer, 0, NULL)) \
+ : hmac_digest( (void *) (ctx)->outer, \
+ (void *) (ctx)->inner, \
+ (void *) (ctx)->state, \
+ (void (*)(void *ctx, \
+ unsigned length, \
+ const uint8_t *data))(update), \
+ (void (*)(void *ctx, \
+ unsigned length, \
+ uint8_t *digest))(digest), \
+ (digest_size), \
+ sizeof(*(ctx)->state), \
+ (length), (digest)))
+
+#if 0
+#define HMAC_INIT(info, ctx, length, key) \
+ (hmac_init( \
+ (void *) ((ctx)->outer), (void *) ((ctx)->inner), (void *) ((ctx)->state), \
+ (info), \
+ (length), (key)))
+
+#define HMAC_UPDATE(info, ctx, length, data) \
+ ((info)->update( \
+ (void *) ((ctx)->state), \
+ (length), (data)))
+
+#define HMAC_DIGEST(info, ctx, length, digest) \
+ (hmac_digest( \
+ (void *) ((ctx)->outer), (void *) ((ctx)->inner), (void *) ((ctx)->state), \
+ (info), \
+ (length), (digest)))
+
+#endif
+
+#endif /* NETTLE_HMAC_H_INCLUDED */
diff --git a/md5-meta.c b/md5-meta.c
new file mode 100644
index 00000000..f0af2379
--- /dev/null
+++ b/md5-meta.c
@@ -0,0 +1,28 @@
+/* md5-meta.c */
+
+/* nettle, low-level cryptographics library
+ *
+ * Copyright (C) 2002 Niels Möller
+ *
+ * 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.
+ */
+
+#include "nettle-meta.h"
+
+#include "md5.h"
+
+const struct nettle_hash nettle_md5
+= _NETTLE_HASH(md5, MD5);
diff --git a/nettle-meta.h b/nettle-meta.h
new file mode 100644
index 00000000..c717a118
--- /dev/null
+++ b/nettle-meta.h
@@ -0,0 +1,134 @@
+/* nettle-meta.h
+ *
+ * Information about algorithms.
+ */
+
+/* nettle, low-level cryptographics library
+ *
+ * Copyright (C) 2002 Niels Möller
+ *
+ * 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.
+ */
+
+#ifndef NETTLE_META_H_INCLUDED
+#define NETTLE_META_H_INCLUDED
+
+#include <inttypes.h>
+
+/* Ciphers */
+typedef void (*nettle_crypt_func)(void *ctx,
+ unsigned length, uint8_t *dst,
+ const uint8_t *src);
+
+typedef void (*nettle_setkey_func)(void *ctx,
+ unsigned length,
+ const uint8_t *key);
+
+
+struct nettle_cipher
+{
+ const char *name;
+
+ unsigned context_size;
+
+ /* Zero for stream ciphers */
+ unsigned block_size;
+
+ unsigned key_size;
+
+ nettle_setkey_func set_encrypt_key;
+ nettle_setkey_func set_decrypt_key;
+
+ nettle_crypt_func encrypt;
+ nettle_crypt_func decrypt;
+};
+
+#define _NETTLE_CIPHER(name, NAME, keysize) { \
+ #name ## #keysize, \
+ sizeof(struct name##_ctx), \
+ NAME##_BLOCK_SIZE, \
+ keysize, \
+ (nettle_set_key_func) name##_set_key, \
+ (nettle_set_key_func) name##_set_key, \
+ (nettle_crypt_func) name##_encrypt, \
+ (nettle_crypt_func) name##_decrypt, \
+}
+
+extern const struct nettle_cipher nettle_aes128;
+extern const struct nettle_cipher nettle_aes192;
+extern const struct nettle_cipher nettle_aes256;
+
+extern const struct nettle_cipher nettle_arcfour128;
+extern const struct nettle_cipher nettle_cast128;
+
+extern const struct nettle_cipher nettle_serpent128;
+extern const struct nettle_cipher nettle_serpent192;
+extern const struct nettle_cipher nettle_serpent256;
+
+extern const struct nettle_cipher nettle_twofish128;
+extern const struct nettle_cipher nettle_twofish192;
+extern const struct nettle_cipher nettle_twofish256;
+
+#if 0
+/* Doesn't quite fit, because of the weak keys and parity requirements. */
+extern const struct nettle_des;
+extern const struct nettle_des3;
+
+extern const struct nettle_blowfish128;
+#endif
+
+
+/* Hash algorithm */
+typedef void (*nettle_hash_init_func)(void *ctx);
+typedef void (*nettle_hash_update_func)(void *ctx,
+ unsigned length,
+ const uint8_t *src);
+typedef void (*nettle_hash_digest_func)(void *ctx,
+ unsigned length, uint8_t *dst);
+
+struct nettle_hash
+{
+ const char *name;
+
+ /* Size of the context struct */
+ unsigned ctx_size;
+
+ /* Size of digests */
+ unsigned digest_size;
+
+ /* Internal block size */
+ unsigned block_size;
+
+ nettle_hash_init_func init;
+ nettle_hash_update_func update;
+ nettle_hash_digest_func digest;
+};
+
+#define _NETTLE_HASH(name, NAME) { \
+ #name, \
+ sizeof(struct name##_ctx), \
+ NAME##_DIGEST_SIZE, \
+ NAME##_DATA_SIZE, \
+ (nettle_hash_init_func) name##_init, \
+ (nettle_hash_update_func) name##_update, \
+ (nettle_hash_digest_func) name##_digest \
+}
+
+extern const struct nettle_hash nettle_md5;
+extern const struct nettle_hash nettle_sha1;
+extern const struct nettle_hash nettle_sha256;
+
+#endif /* NETTLE_META_H_INCLUDED */
diff --git a/sha1-meta.c b/sha1-meta.c
new file mode 100644
index 00000000..c2e0ebb6
--- /dev/null
+++ b/sha1-meta.c
@@ -0,0 +1,28 @@
+/* sha1-meta.c */
+
+/* nettle, low-level cryptographics library
+ *
+ * Copyright (C) 2002 Niels Möller
+ *
+ * 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.
+ */
+
+#include "nettle-meta.h"
+
+#include "sha.h"
+
+const struct nettle_hash nettle_sha1
+= _NETTLE_HASH(sha1, SHA1);
diff --git a/sha256-meta.c b/sha256-meta.c
new file mode 100644
index 00000000..f60c9fc6
--- /dev/null
+++ b/sha256-meta.c
@@ -0,0 +1,28 @@
+/* sha256-meta.c */
+
+/* nettle, low-level cryptographics library
+ *
+ * Copyright (C) 2002 Niels Möller
+ *
+ * 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.
+ */
+
+#include "nettle-meta.h"
+
+#include "sha.h"
+
+const struct nettle_hash nettle_sha256
+= _NETTLE_HASH(sha256, SHA256);