/*
* Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
*
- * Licensed under the OpenSSL license (the "License"). You may not use
+ * Licensed under the Apache License 2.0 (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
* in the file LICENSE in the source distribution or at
* https://www.openssl.org/source/license.html
#include <openssl/evp.h>
#include <openssl/objects.h>
#include "internal/evp_int.h"
+#include "internal/provider.h"
#include "evp_locl.h"
int EVP_CIPHER_param_to_asn1(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
int EVP_CIPHER_key_length(const EVP_CIPHER *cipher)
{
+ if (cipher->prov != NULL) {
+ if (cipher->key_length != NULL)
+ return (int)cipher->key_length();
+ return -1;
+ }
+
return cipher->key_len;
}
int EVP_CIPHER_CTX_key_length(const EVP_CIPHER_CTX *ctx)
{
+ /*
+ * TODO(3.0): This may need to change if/when we introduce variable length
+ * key ciphers into the providers.
+ */
+ if (ctx->cipher != NULL && ctx->cipher->prov != NULL)
+ return EVP_CIPHER_key_length(ctx->cipher);
return ctx->key_len;
}
int EVP_MD_block_size(const EVP_MD *md)
{
+ if (md == NULL) {
+ EVPerr(EVP_F_EVP_MD_BLOCK_SIZE, EVP_R_MESSAGE_DIGEST_IS_NULL);
+ return -1;
+ }
+
+ if (md->prov != NULL && md->dblock_size != NULL)
+ return (int)md->dblock_size();
+
return md->block_size;
}
EVPerr(EVP_F_EVP_MD_SIZE, EVP_R_MESSAGE_DIGEST_IS_NULL);
return -1;
}
+
+ if (md->prov != NULL && md->size != NULL)
+ return (int)md->size();
+
return md->md_size;
}
if (md != NULL) {
md->type = md_type;
md->pkey_type = pkey_type;
+ md->lock = CRYPTO_THREAD_lock_new();
+ if (md->lock == NULL) {
+ OPENSSL_free(md);
+ return NULL;
+ }
+ md->refcnt = 1;
}
return md;
}
+
EVP_MD *EVP_MD_meth_dup(const EVP_MD *md)
{
EVP_MD *to = EVP_MD_meth_new(md->type, md->pkey_type);
- if (to != NULL)
+ if (to != NULL) {
+ CRYPTO_RWLOCK *lock = to->lock;
memcpy(to, md, sizeof(*to));
+ to->lock = lock;
+ }
return to;
}
+
+int EVP_MD_upref(EVP_MD *md)
+{
+ int ref = 0;
+
+ CRYPTO_UP_REF(&md->refcnt, &ref, md->lock);
+ return 1;
+}
+
void EVP_MD_meth_free(EVP_MD *md)
{
- OPENSSL_free(md);
+ if (md != NULL) {
+ int i;
+
+ CRYPTO_DOWN_REF(&md->refcnt, &i, md->lock);
+ if (i > 0)
+ return;
+ ossl_provider_free(md->prov);
+ CRYPTO_THREAD_lock_free(md->lock);
+ OPENSSL_free(md);
+ }
}
int EVP_MD_meth_set_input_blocksize(EVP_MD *md, int blocksize)
{
const EVP_MD *EVP_MD_CTX_md(const EVP_MD_CTX *ctx)
{
- if (!ctx)
+ if (ctx == NULL)
return NULL;
- return ctx->digest;
+ return ctx->reqdigest;
}
EVP_PKEY_CTX *EVP_MD_CTX_pkey_ctx(const EVP_MD_CTX *ctx)
{
return (ctx->flags & flags);
}
+
+int EVP_str2ctrl(int (*cb)(void *ctx, int cmd, void *buf, size_t buflen),
+ void *ctx, int cmd, const char *value)
+{
+ size_t len;
+
+ len = strlen(value);
+ if (len > INT_MAX)
+ return -1;
+ return cb(ctx, cmd, (void *)value, len);
+}
+
+int EVP_hex2ctrl(int (*cb)(void *ctx, int cmd, void *buf, size_t buflen),
+ void *ctx, int cmd, const char *hex)
+{
+ unsigned char *bin;
+ long binlen;
+ int rv = -1;
+
+ bin = OPENSSL_hexstr2buf(hex, &binlen);
+ if (bin == NULL)
+ return 0;
+ if (binlen <= INT_MAX)
+ rv = cb(ctx, cmd, bin, binlen);
+ OPENSSL_free(bin);
+ return rv;
+}