Annotation of embedaddon/strongswan/src/libstrongswan/plugins/botan/botan_x25519.c, revision 1.1

1.1     ! misho       1: /*
        !             2:  * Copyright (C) 2018 Tobias Brunner
        !             3:  * HSR Hochschule fuer Technik Rapperswil
        !             4:  *
        !             5:  * Permission is hereby granted, free of charge, to any person obtaining a copy
        !             6:  * of this software and associated documentation files (the "Software"), to deal
        !             7:  * in the Software without restriction, including without limitation the rights
        !             8:  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        !             9:  * copies of the Software, and to permit persons to whom the Software is
        !            10:  * furnished to do so, subject to the following conditions:
        !            11:  *
        !            12:  * The above copyright notice and this permission notice shall be included in
        !            13:  * all copies or substantial portions of the Software.
        !            14:  *
        !            15:  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        !            16:  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        !            17:  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        !            18:  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        !            19:  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        !            20:  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
        !            21:  * THE SOFTWARE.
        !            22:  */
        !            23: 
        !            24: #include "botan_x25519.h"
        !            25: 
        !            26: #include <botan/build.h>
        !            27: 
        !            28: #ifdef BOTAN_HAS_X25519
        !            29: 
        !            30: #include "botan_util.h"
        !            31: 
        !            32: #include <utils/debug.h>
        !            33: 
        !            34: #include <botan/ffi.h>
        !            35: 
        !            36: typedef struct private_diffie_hellman_t private_diffie_hellman_t;
        !            37: 
        !            38: /**
        !            39:  * Private data
        !            40:  */
        !            41: struct private_diffie_hellman_t {
        !            42: 
        !            43:        /**
        !            44:         * Public interface
        !            45:         */
        !            46:        diffie_hellman_t public;
        !            47: 
        !            48:        /**
        !            49:         * Private key
        !            50:         */
        !            51:        botan_privkey_t key;
        !            52: 
        !            53:        /**
        !            54:         * Shared secret
        !            55:         */
        !            56:        chunk_t shared_secret;
        !            57: };
        !            58: 
        !            59: METHOD(diffie_hellman_t, set_other_public_value, bool,
        !            60:        private_diffie_hellman_t *this, chunk_t value)
        !            61: {
        !            62:        if (!diffie_hellman_verify_value(CURVE_25519, value))
        !            63:        {
        !            64:                return FALSE;
        !            65:        }
        !            66: 
        !            67:        chunk_clear(&this->shared_secret);
        !            68: 
        !            69:        return botan_dh_key_derivation(this->key, value, &this->shared_secret);
        !            70: }
        !            71: 
        !            72: METHOD(diffie_hellman_t, get_my_public_value, bool,
        !            73:        private_diffie_hellman_t *this, chunk_t *value)
        !            74: {
        !            75:        value->len = 0;
        !            76:        if (botan_pk_op_key_agreement_export_public(this->key, NULL, &value->len)
        !            77:                != BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE)
        !            78:        {
        !            79:                return FALSE;
        !            80:        }
        !            81: 
        !            82:        *value = chunk_alloc(value->len);
        !            83:        if (botan_pk_op_key_agreement_export_public(this->key, value->ptr,
        !            84:                                                                                                &value->len))
        !            85:        {
        !            86:                chunk_free(value);
        !            87:                return FALSE;
        !            88:        }
        !            89:        return TRUE;
        !            90: }
        !            91: 
        !            92: METHOD(diffie_hellman_t, set_private_value, bool,
        !            93:        private_diffie_hellman_t *this, chunk_t value)
        !            94: {
        !            95:        if (value.len != 32)
        !            96:        {
        !            97:                return FALSE;
        !            98:        }
        !            99: 
        !           100:        chunk_clear(&this->shared_secret);
        !           101: 
        !           102:        if (botan_privkey_destroy(this->key))
        !           103:        {
        !           104:                return FALSE;
        !           105:        }
        !           106: 
        !           107:        if (botan_privkey_load_x25519(&this->key, value.ptr))
        !           108:        {
        !           109:                return FALSE;
        !           110:        }
        !           111:        return TRUE;
        !           112: }
        !           113: 
        !           114: METHOD(diffie_hellman_t, get_shared_secret, bool,
        !           115:        private_diffie_hellman_t *this, chunk_t *secret)
        !           116: {
        !           117:        if (!this->shared_secret.len)
        !           118:        {
        !           119:                return FALSE;
        !           120:        }
        !           121:        *secret = chunk_clone(this->shared_secret);
        !           122:        return TRUE;
        !           123: }
        !           124: 
        !           125: METHOD(diffie_hellman_t, get_dh_group, diffie_hellman_group_t,
        !           126:        private_diffie_hellman_t *this)
        !           127: {
        !           128:        return CURVE_25519;
        !           129: }
        !           130: 
        !           131: METHOD(diffie_hellman_t, destroy, void,
        !           132:        private_diffie_hellman_t *this)
        !           133: {
        !           134:        botan_privkey_destroy(this->key);
        !           135:        chunk_clear(&this->shared_secret);
        !           136:        free(this);
        !           137: }
        !           138: 
        !           139: /*
        !           140:  * Described in header
        !           141:  */
        !           142: diffie_hellman_t *botan_x25519_create(diffie_hellman_group_t group)
        !           143: {
        !           144:        private_diffie_hellman_t *this;
        !           145:        botan_rng_t rng;
        !           146: 
        !           147:        INIT(this,
        !           148:                .public = {
        !           149:                        .get_shared_secret = _get_shared_secret,
        !           150:                        .set_other_public_value = _set_other_public_value,
        !           151:                        .get_my_public_value = _get_my_public_value,
        !           152:                        .set_private_value = _set_private_value,
        !           153:                        .get_dh_group = _get_dh_group,
        !           154:                        .destroy = _destroy,
        !           155:                },
        !           156:        );
        !           157: 
        !           158:        if (botan_rng_init(&rng, "user"))
        !           159:        {
        !           160:                free(this);
        !           161:                return NULL;
        !           162:        }
        !           163: 
        !           164:        if (botan_privkey_create(&this->key, "Curve25519", "", rng))
        !           165:        {
        !           166:                DBG1(DBG_LIB, "x25519 private key generation failed");
        !           167:                botan_rng_destroy(rng);
        !           168:                free(this);
        !           169:                return NULL;
        !           170:        }
        !           171: 
        !           172:        botan_rng_destroy(rng);
        !           173:        return &this->public;
        !           174: }
        !           175: 
        !           176: #endif

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>