2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A#include "libsoftcrypto.h"
2N/A
2N/A#define MAXSTRLEN 1024
2N/A
2N/Atypedef struct {
2N/A int num;
2N/A const char *str;
2N/A} mechstr_t;
2N/A
2N/A/*
2N/A * If a new enum value is added to ucrypto_mech_t AND support for that new
2N/A * mechanism is implemented by this library, it must also be inserted here
2N/A * in mechstrlist in the same order it appears in the ucrypto_mech_t enum.
2N/A * ucrypto_id2mech() uses bsearch() to traverse the list, thus order matters.
2N/A *
2N/A * JDK hardwired to the ucrypto_mech_t enum values. Do not change existing
2N/A * enum values. Do not add an entry here if only the enum value is defined,
2N/A * but there is no actual support implemented for the mechanism. See also
2N/A * the comment for ucrypto_mech_t.
2N/A */
2N/Astatic const mechstr_t mechstrlist[] = {
2N/A {CRYPTO_AES_ECB, "CRYPTO_AES_ECB"},
2N/A {CRYPTO_AES_CBC, "CRYPTO_AES_CBC"},
2N/A {CRYPTO_AES_CBC_PAD, "CRYPTO_AES_CBC_PAD"},
2N/A {CRYPTO_AES_CTR, "CRYPTO_AES_CTR"},
2N/A {CRYPTO_AES_CCM, "CRYPTO_AES_CCM"},
2N/A {CRYPTO_AES_GCM, "CRYPTO_AES_GCM"},
2N/A {CRYPTO_AES_CFB128, "CRYPTO_AES_CFB128"},
2N/A {CRYPTO_AES_XTS, "CRYPTO_AES_XTS"},
2N/A {CRYPTO_AES_XCBC_MAC, "CRYPTO_AES_XCBC_MAC"},
2N/A {CRYPTO_RSA_PKCS, "CRYPTO_RSA_PKCS"},
2N/A {CRYPTO_RSA_X_509, "CRYPTO_RSA_X_509"},
2N/A {CRYPTO_MD5_RSA_PKCS, "CRYPTO_MD5_RSA_PKCS"},
2N/A {CRYPTO_SHA1_RSA_PKCS, "CRYPTO_SHA1_RSA_PKCS"},
2N/A {CRYPTO_SHA256_RSA_PKCS, "CRYPTO_SHA256_RSA_PKCS"},
2N/A {CRYPTO_SHA384_RSA_PKCS, "CRYPTO_SHA384_RSA_PKCS"},
2N/A {CRYPTO_SHA512_RSA_PKCS, "CRYPTO_SHA512_RSA_PKCS"},
2N/A {CRYPTO_SHA224_RSA_PKCS, "CRYPTO_SHA224_RSA_PKCS"}
2N/A};
2N/A
2N/A/* bsearch compare function */
2N/Astatic int
2N/Amech_comp(const void *mech1, const void *mech2) {
2N/A return (((mechstr_t *)mech1)->num - ((mechstr_t *)mech2)->num);
2N/A}
2N/A
2N/A/* Returns the string value of the mechanism for a given mechanism number */
2N/Aconst char *
2N/Aucrypto_id2mech(ucrypto_mech_t mech_type)
2N/A{
2N/A mechstr_t target;
2N/A mechstr_t *result = NULL;
2N/A
2N/A target.num = mech_type;
2N/A target.str = NULL;
2N/A
2N/A result = (mechstr_t *)bsearch((void *)&target, (void *)mechstrlist,
2N/A (sizeof (mechstrlist) / sizeof (mechstr_t)), sizeof (mechstr_t),
2N/A mech_comp);
2N/A if (result != NULL)
2N/A return (result->str);
2N/A
2N/A return (NULL);
2N/A}
2N/A
2N/A/*
2N/A * Return a delimited string of supported mechanisms with their
2N/A * value number specified in ucrypto_mech_t. See libsoftcrypto.h for
2N/A * details on format
2N/A *
2N/A */
2N/Aint
2N/Aucrypto_get_mechlist(char *s)
2N/A{
2N/A int i = 0, pos;
2N/A static const int max = sizeof (mechstrlist) / sizeof (mechstr_t);
2N/A char str[MAXSTRLEN];
2N/A
2N/A if (s == NULL)
2N/A s = &(str[0]);
2N/A
2N/A pos = snprintf(s, MAXSTRLEN, "%d:", max);
2N/A for (i = 0; i < max; i++)
2N/A pos += snprintf(s + pos, MAXSTRLEN - pos, "%s,%d;",
2N/A mechstrlist[i].str, mechstrlist[i].num);
2N/A
2N/A (void) snprintf(s + pos, MAXSTRLEN - pos, "\0");
2N/A pos++; /* Need to count the NULL */
2N/A
2N/A return (pos);
2N/A}
2N/A
2N/A/*
2N/A * With a given mechanism string value, match it with the supported
2N/A * mechanism and return the mechanism number, if found; otherwise, it
2N/A * returns an undefined mech number.
2N/A */
2N/Aucrypto_mech_t
2N/Aucrypto_mech2id(const char *str)
2N/A{
2N/A static const int max = sizeof (mechstrlist) / sizeof (mechstr_t);
2N/A int i;
2N/A
2N/A if (str == NULL)
2N/A return (0);
2N/A
2N/A for (i = 0; i < max; i++) {
2N/A if (strcmp(mechstrlist[i].str, str) == 0)
2N/A return (mechstrlist[i].num);
2N/A }
2N/A
2N/A return (0);
2N/A}