opensslmd5_link.c revision a7d792804f564aa0889a94f64913b11bffd9738c
68a8db1efb0cd13e0e3d59362ab9db1a4a8bc496Andreas Gustafsson#if defined(OPENSSL)
68a8db1efb0cd13e0e3d59362ab9db1a4a8bc496Andreas Gustafsson
68a8db1efb0cd13e0e3d59362ab9db1a4a8bc496Andreas Gustafsson/*
68a8db1efb0cd13e0e3d59362ab9db1a4a8bc496Andreas Gustafsson * Portions Copyright (c) 1995-1998 by Network Associates, Inc.
68a8db1efb0cd13e0e3d59362ab9db1a4a8bc496Andreas Gustafsson *
68a8db1efb0cd13e0e3d59362ab9db1a4a8bc496Andreas Gustafsson * Permission to use, copy modify, and distribute this software for any
68a8db1efb0cd13e0e3d59362ab9db1a4a8bc496Andreas Gustafsson * purpose with or without fee is hereby granted, provided that the above
68a8db1efb0cd13e0e3d59362ab9db1a4a8bc496Andreas Gustafsson * copyright notice and this permission notice appear in all copies.
68a8db1efb0cd13e0e3d59362ab9db1a4a8bc496Andreas Gustafsson *
68a8db1efb0cd13e0e3d59362ab9db1a4a8bc496Andreas Gustafsson * THE SOFTWARE IS PROVIDED "AS IS" AND NETWORK ASSOCIATES
68a8db1efb0cd13e0e3d59362ab9db1a4a8bc496Andreas Gustafsson * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
68a8db1efb0cd13e0e3d59362ab9db1a4a8bc496Andreas Gustafsson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
68a8db1efb0cd13e0e3d59362ab9db1a4a8bc496Andreas Gustafsson * NETWORK ASSOCIATES BE LIABLE FOR ANY SPECIAL, DIRECT,
68a8db1efb0cd13e0e3d59362ab9db1a4a8bc496Andreas Gustafsson * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
68a8db1efb0cd13e0e3d59362ab9db1a4a8bc496Andreas Gustafsson * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
68a8db1efb0cd13e0e3d59362ab9db1a4a8bc496Andreas Gustafsson * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
68a8db1efb0cd13e0e3d59362ab9db1a4a8bc496Andreas Gustafsson * WITH THE USE OR PERFORMANCE OF THE SOFTWARE.
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson/*
bd3946db298667e769804b2e978df2d6dcd85e29Mark Andrews * Principal Author: Brian Wellington
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * $Id: opensslmd5_link.c,v 1.2 1999/10/20 22:14:15 bwelling Exp $
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson#include <config.h>
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson#include <stdio.h>
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson#include <unistd.h>
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson#include <stdlib.h>
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson#include <string.h>
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson#include <memory.h>
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson#include <isc/assertions.h>
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson#include <isc/buffer.h>
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson#include <isc/int.h>
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson#include <isc/region.h>
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson#include "dst_internal.h"
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson#include "dst_parse.h"
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson#include <openssl/crypto.h>
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson#include <openssl/bn.h>
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson#include <openssl/md5.h>
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson#define MD5Init MD5_Init
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson#define MD5Update MD5_Update
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson#define MD5Final MD5_Final
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson/*
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * dst_s_md5
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Call MD5 functions to digest a block of data.
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * There are three steps to signing, INIT (initialize structures),
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * UPDATE (hash (more) data), FINAL (generate a digest). This
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * routine performs one or more of these steps.
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Parameters
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * mode DST_SIGMODE_{INIT_UPDATE_FINAL|ALL}
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * context the context to use for this computation
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * data data to be signed
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * digest buffer to store digest
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * mctx memory context for temporary allocations
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Returns
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * ISC_R_SUCCESS Success
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * !ISC_R_SUCCESS Failure
8ac1acc30d0f405222ffa7b2b93131d9d4e18599Mark Andrews */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssondst_result_t
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssondst_s_md5(const unsigned int mode, void **context, isc_region_t *data,
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson isc_buffer_t *digest, isc_mem_t *mctx)
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson{
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson isc_region_t r;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson MD5_CTX *ctx = NULL;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (mode & DST_SIGMODE_INIT) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson ctx = (MD5_CTX *) isc_mem_get(mctx, sizeof(MD5_CTX));
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (ctx == NULL)
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return (ISC_R_NOMEMORY);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson else if (context != NULL)
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson ctx = (MD5_CTX *) *context;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson REQUIRE (ctx != NULL);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (mode & DST_SIGMODE_INIT)
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson MD5Init(ctx);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (mode & DST_SIGMODE_UPDATE)
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson MD5Update(ctx, data->base, data->length);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
e2cf0e8ff9ff37121518af5b34b9e4de7abbb47cMark Andrews if (mode & DST_SIGMODE_FINAL) {
e2cf0e8ff9ff37121518af5b34b9e4de7abbb47cMark Andrews isc_buffer_available(digest, &r);
e2cf0e8ff9ff37121518af5b34b9e4de7abbb47cMark Andrews if (r.length < MD5_DIGEST_LENGTH)
e2cf0e8ff9ff37121518af5b34b9e4de7abbb47cMark Andrews return (ISC_R_NOSPACE);
e2cf0e8ff9ff37121518af5b34b9e4de7abbb47cMark Andrews
e2cf0e8ff9ff37121518af5b34b9e4de7abbb47cMark Andrews MD5Final(r.base, ctx);
e2cf0e8ff9ff37121518af5b34b9e4de7abbb47cMark Andrews isc_buffer_add(digest, MD5_DIGEST_LENGTH);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson isc_mem_put(mctx, ctx, sizeof(MD5_CTX));
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson }
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson else
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson *context = ctx;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson return (ISC_R_SUCCESS);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson}
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson#endif
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson