summaryrefslogtreecommitdiffstats
path: root/crypto/evp
diff options
context:
space:
mode:
authorDr. Stephen Henson <steve@openssl.org>2006-04-06 13:02:06 +0000
committerDr. Stephen Henson <steve@openssl.org>2006-04-06 13:02:06 +0000
commit0b6f3c66cddf047933b8ff77c4f8a3e706e93aa6 (patch)
tree072aa96c432ee3962c2f5bee5e93eb0d781c859e /crypto/evp
parenta01d9ac5582657cd2579153b8c5987b4201f1688 (diff)
Initial definitions and a few functions for EVP_PKEY_METHOD: an extension
of the EVP routines to public key algorithms.
Diffstat (limited to 'crypto/evp')
-rw-r--r--crypto/evp/Makefile4
-rw-r--r--crypto/evp/evp.h1
-rw-r--r--crypto/evp/evp_locl.h71
-rw-r--r--crypto/evp/pmeth_lib.c146
4 files changed, 220 insertions, 2 deletions
diff --git a/crypto/evp/Makefile b/crypto/evp/Makefile
index 310fff4257..f6476d88bc 100644
--- a/crypto/evp/Makefile
+++ b/crypto/evp/Makefile
@@ -28,7 +28,7 @@ LIBSRC= encode.c digest.c evp_enc.c evp_key.c evp_acnf.c \
bio_md.c bio_b64.c bio_enc.c evp_err.c e_null.c \
c_all.c c_allc.c c_alld.c evp_lib.c bio_ok.c \
evp_pkey.c evp_pbe.c p5_crpt.c p5_crpt2.c \
- e_old.c
+ e_old.c pmeth_lib.c
LIBOBJ= encode.o digest.o evp_enc.o evp_key.o evp_acnf.o \
e_des.o e_bf.o e_idea.o e_des3.o \
@@ -40,7 +40,7 @@ LIBOBJ= encode.o digest.o evp_enc.o evp_key.o evp_acnf.o \
bio_md.o bio_b64.o bio_enc.o evp_err.o e_null.o \
c_all.o c_allc.o c_alld.o evp_lib.o bio_ok.o \
evp_pkey.o evp_pbe.o p5_crpt.o p5_crpt2.o \
- e_old.o
+ e_old.o pmeth_lib.o
SRC= $(LIBSRC)
diff --git a/crypto/evp/evp.h b/crypto/evp/evp.h
index d06904b7ec..7d16da8f04 100644
--- a/crypto/evp/evp.h
+++ b/crypto/evp/evp.h
@@ -129,6 +129,7 @@ struct evp_pkey_st
int save_type;
int references;
const EVP_PKEY_ASN1_METHOD *ameth;
+ const EVP_PKEY_METHOD *pmeth;
union {
char *ptr;
#ifndef OPENSSL_NO_RSA
diff --git a/crypto/evp/evp_locl.h b/crypto/evp/evp_locl.h
index 2204e345ad..af02514252 100644
--- a/crypto/evp/evp_locl.h
+++ b/crypto/evp/evp_locl.h
@@ -234,3 +234,74 @@ const EVP_CIPHER *EVP_##cname##_ecb(void) { return &cname##_ecb; }
EVP_CIPHER_set_asn1_iv, \
EVP_CIPHER_get_asn1_iv, \
NULL)
+
+
+struct evp_pkey_ctx_st
+ {
+ /* Method associated with this operation */
+ const EVP_PKEY_METHOD *pmeth;
+ /* Key: may be NULL */
+ EVP_PKEY *pkey;
+ /* Actual operation */
+ int operation;
+ /* Algorithm specific data */
+ void *data;
+ } /* EVP_PKEY_CTX */;
+
+#define EVP_PKEY_OP_UNDEFINED 0
+#define EVP_PKEY_OP_PARAMGEN 1
+#define EVP_PKEY_OP_KEYGEN 2
+#define EVP_PKEY_OP_SIGN 3
+#define EVP_PKEY_OP_VERIFY 4
+#define EVP_PKEY_OP_VERIFYRECOVER 5
+#define EVP_PKEY_OP_SIGNCTX 6
+#define EVP_PKEY_OP_VERIFYCTX 7
+#define EVP_PKEY_OP_ENCRYPT 8
+#define EVP_PKEY_OP_DECRYPT 9
+
+struct evp_pkey_method_st
+ {
+ int pkey_id;
+ int (*paramgen_init)(EVP_PKEY_CTX *ctx);
+ int (*paramgen)(EVP_PKEY *key, EVP_PKEY_CTX *ctx);
+
+ int (*keygen_init)(EVP_PKEY_CTX *ctx);
+ int (*keygen)(EVP_PKEY *key, EVP_PKEY_CTX *ctx);
+
+ int (*sign_init)(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey);
+ int (*sign)(EVP_PKEY_CTX *ctx, unsigned char *sig, int *siglen,
+ unsigned char *tbs, int tbslen);
+
+ int (*verify_init)(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey);
+ int (*verify)(EVP_PKEY_CTX *ctx, unsigned char *sig, int siglen,
+ unsigned char *tbs, int tbslen);
+
+ int (*verify_recover_init)(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey);
+ int (*verify_recover)(EVP_PKEY_CTX *ctx,
+ unsigned char *rout, int *routlen,
+ unsigned char *sig, int siglen);
+
+ int (*signctx_init)(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx,
+ EVP_PKEY *pkey);
+ int (*signctx)(EVP_PKEY_CTX *ctx, unsigned char *sig, int *siglen,
+ EVP_MD_CTX *mctx);
+
+ int (*verifyctx_init)(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx,
+ EVP_PKEY *pkey);
+ int (*verifyctx)(EVP_PKEY_CTX *ctx, unsigned char *sig, int siglen,
+ EVP_MD_CTX *mctx);
+
+ int (*encrypt_init)(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey);
+ int (*encrypt)(EVP_PKEY_CTX *ctx, unsigned char *out, int *outlen,
+ unsigned char *in, int inlen);
+
+ int (*decrypt_init)(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey);
+ int (*decrypt)(EVP_PKEY_CTX *ctx, unsigned char *out, int *outlen,
+ unsigned char *in, int inlen);
+
+ int (*ctrl)(EVP_PKEY_CTX *ctx, int type, int p1, void *p2);
+ int (*ctrl_str)(EVP_PKEY_CTX *ctx, const char *type, const char *value);
+
+ void (*cleanup)(EVP_PKEY_CTX *ctx);
+
+ } /* EVP_PKEY_METHOD */;
diff --git a/crypto/evp/pmeth_lib.c b/crypto/evp/pmeth_lib.c
new file mode 100644
index 0000000000..1830ea1a6d
--- /dev/null
+++ b/crypto/evp/pmeth_lib.c
@@ -0,0 +1,146 @@
+/* pmeth_lib.c */
+/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
+ * project 2006.
+ */
+/* ====================================================================
+ * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * 3. All advertising materials mentioning features or use of this
+ * software must display the following acknowledgment:
+ * "This product includes software developed by the OpenSSL Project
+ * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
+ *
+ * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
+ * endorse or promote products derived from this software without
+ * prior written permission. For written permission, please contact
+ * licensing@OpenSSL.org.
+ *
+ * 5. Products derived from this software may not be called "OpenSSL"
+ * nor may "OpenSSL" appear in their names without prior written
+ * permission of the OpenSSL Project.
+ *
+ * 6. Redistributions of any form whatsoever must retain the following
+ * acknowledgment:
+ * "This product includes software developed by the OpenSSL Project
+ * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
+ * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
+ * ====================================================================
+ *
+ * This product includes cryptographic software written by Eric Young
+ * (eay@cryptsoft.com). This product includes software written by Tim
+ * Hudson (tjh@cryptsoft.com).
+ *
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <openssl/objects.h>
+#include "cryptlib.h"
+#include "evp_locl.h"
+
+STACK *app_pkey_methods = NULL;
+
+extern EVP_PKEY_METHOD rsa_pkey_meth;
+
+const EVP_PKEY_METHOD *standard_methods[] =
+ {
+ &rsa_pkey_meth
+ };
+
+static int pmeth_cmp(const EVP_PKEY_METHOD * const *a,
+ const EVP_PKEY_METHOD * const *b)
+ {
+ return ((*a)->pkey_id - (*b)->pkey_id);
+ }
+
+const EVP_PKEY_METHOD *EVP_PKEY_meth_find(int type, ENGINE *e)
+ {
+ EVP_PKEY_METHOD tmp, *t = &tmp, **ret;
+ tmp.pkey_id = type;
+ if (app_pkey_methods)
+ {
+ int idx;
+ idx = sk_find(app_pkey_methods, (char *)&tmp);
+ if (idx >= 0)
+ return (EVP_PKEY_METHOD *)
+ sk_value(app_pkey_methods, idx);
+ }
+ ret = (EVP_PKEY_METHOD **) OBJ_bsearch((char *)&t,
+ (char *)standard_methods,
+ sizeof(standard_methods)/sizeof(EVP_PKEY_METHOD *),
+ sizeof(EVP_PKEY_METHOD *),
+ (int (*)(const void *, const void *))pmeth_cmp);
+ if (!ret || !*ret)
+ return NULL;
+ return *ret;
+ }
+
+EVP_PKEY_CTX *EVP_PKEY_CTX_new(int ktype, ENGINE *e)
+ {
+ EVP_PKEY_CTX *ret;
+ const EVP_PKEY_METHOD *pmeth;
+ pmeth = EVP_PKEY_meth_find(ktype, e);
+ if (pmeth == NULL)
+ return NULL;
+ ret = OPENSSL_malloc(sizeof(EVP_PKEY_CTX));
+ ret->pmeth = pmeth;
+ ret->operation = EVP_PKEY_OP_UNDEFINED;
+ ret->pkey = NULL;
+ ret->data = NULL;
+
+ return ret;
+ }
+
+int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype,
+ int cmd, int p1, void *p2)
+ {
+ if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl)
+ return -2;
+ if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype))
+ return -1;
+
+ if (ctx->operation == EVP_PKEY_OP_UNDEFINED)
+ {
+ /* Not initialized */
+ return -1;
+ }
+
+ if ((optype != -1) && (ctx->operation != optype))
+ {
+ /* Invalid operation type */
+ return -1;
+ }
+
+ return ctx->pmeth->ctrl(ctx, cmd, p1, p2);
+
+ }
+
+
+
+
+
+