2 * Copyright (C) 2018 Tobias Brunner
3 * HSR Hochschule fuer Technik Rapperswil
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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
24 #include "botan_ed_public_key.h"
25 #include "botan_util.h"
27 #include <botan/build.h>
29 #ifdef BOTAN_HAS_ED25519
31 #include <utils/debug.h>
33 typedef struct private_public_key_t private_public_key_t
;
38 struct private_public_key_t
{
46 * Botan public key object
56 METHOD(public_key_t
, get_type
, key_type_t
,
57 private_public_key_t
*this)
62 METHOD(public_key_t
, get_keysize
, int,
63 private_public_key_t
*this)
65 return ED25519_KEY_LEN
* 8;
68 METHOD(public_key_t
, verify
, bool,
69 private_public_key_t
*this, signature_scheme_t scheme
,
70 void *params
, chunk_t data
, chunk_t signature
)
75 return botan_verify_signature(this->key
, "Pure", data
, signature
);
77 DBG1(DBG_LIB
, "signature scheme %N not supported via botan",
78 signature_scheme_names
, scheme
);
83 METHOD(public_key_t
, encrypt
, bool,
84 private_public_key_t
*this, encryption_scheme_t scheme
,
85 void *params
, chunk_t crypto
, chunk_t
*plain
)
87 DBG1(DBG_LIB
, "EdDSA public key encryption not implemented");
91 METHOD(public_key_t
, get_fingerprint
, bool,
92 private_public_key_t
*this, cred_encoding_type_t type
,
95 return botan_get_fingerprint(this->key
, this, type
, fingerprint
);
98 METHOD(public_key_t
, get_encoding
, bool,
99 private_public_key_t
*this, cred_encoding_type_t type
,
102 return botan_get_encoding(this->key
, type
, encoding
);
105 METHOD(public_key_t
, get_ref
, public_key_t
*,
106 private_public_key_t
*this)
109 return &this->public;
112 METHOD(public_key_t
, destroy
, void,
113 private_public_key_t
*this)
115 if (ref_put(&this->ref
))
117 lib
->encoding
->clear_cache(lib
->encoding
, this);
118 botan_pubkey_destroy(this->key
);
124 * Internal generic constructor
126 static private_public_key_t
*create_empty()
128 private_public_key_t
*this;
132 .get_type
= _get_type
,
135 .get_keysize
= _get_keysize
,
136 .equals
= public_key_equals
,
137 .get_fingerprint
= _get_fingerprint
,
138 .has_fingerprint
= public_key_has_fingerprint
,
139 .get_encoding
= _get_encoding
,
150 * Described in header
152 public_key_t
*botan_ed_public_key_adopt(botan_pubkey_t key
)
154 private_public_key_t
*this;
156 this = create_empty();
159 return &this->public;
163 * Described in header
165 public_key_t
*botan_ed_public_key_load(key_type_t type
, va_list args
)
167 private_public_key_t
*this;
168 chunk_t key
= chunk_empty
;
172 switch (va_arg(args
, builder_part_t
))
174 case BUILD_EDDSA_PUB
:
175 key
= va_arg(args
, chunk_t
);
185 /* ASN.1-encoded keys are handled generically, so we only handle the
187 if (key
.len
!= ED25519_KEY_LEN
)
192 this = create_empty();
194 if (botan_pubkey_load_ed25519(&this->key
, key
.ptr
))
199 return &this->public;