Debian Codegraph @

src/lib/crypto/OSSLECPublicKey.cpp

raw
1 /*
2 * Copyright (c) 2010 SURFnet bv
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 /*****************************************************************************
28 OSSLECPublicKey.cpp
29
30 OpenSSL Elliptic Curve public key class
31 *****************************************************************************/
32
33 #include "config.h"
34 #ifdef WITH_ECC
35 #include "log.h"
36 #include "OSSLECPublicKey.h"
37 #include "OSSLUtil.h"
38 #include <openssl/bn.h>
39 #include <string.h>
40
41 // Constructors
42 OSSLECPublicKey::OSSLECPublicKey()
43 {
44 eckey = EC_KEY_new();
45 }
46
47 OSSLECPublicKey::OSSLECPublicKey(const EC_KEY* inECKEY)
48 {
49 eckey = EC_KEY_new();
50
51 setFromOSSL(inECKEY);
52 }
53
54 // Destructor
55 OSSLECPublicKey::~OSSLECPublicKey()
56 {
57 EC_KEY_free(eckey);
58 }
59
60 // The type
61 /*static*/ const char* OSSLECPublicKey::type = "OpenSSL EC Public Key";
62
63 // Get the base point order length
64 unsigned long OSSLECPublicKey::getOrderLength() const
65 {
66 const EC_GROUP* grp = EC_KEY_get0_group(eckey);
67 if (grp != NULL)
68 {
69 BIGNUM* order = BN_new();
70 if (order == NULL)
71 return 0;
72 if (!EC_GROUP_get_order(grp, order, NULL))
73 {
74 BN_clear_free(order);
75 return 0;
76 }
77 unsigned long len = BN_num_bytes(order);
78 BN_clear_free(order);
79 return len;
80 }
81 return 0;
82 }
83
84 // Set from OpenSSL representation
85 void OSSLECPublicKey::setFromOSSL(const EC_KEY* inECKEY)
86 {
87 const EC_GROUP* grp = EC_KEY_get0_group(inECKEY);
88 if (grp != NULL)
89 {
90 ByteString inEC = OSSL::grp2ByteString(grp);
91 setEC(inEC);
92 }
93 const EC_POINT* pub = EC_KEY_get0_public_key(inECKEY);
94 if (pub != NULL && grp != NULL)
95 {
96 ByteString inQ = OSSL::pt2ByteString(pub, grp);
97 setQ(inQ);
98 }
99 }
100
101 // Check if the key is of the given type
102 bool OSSLECPublicKey::isOfType(const char* inType)
103 {
104 return !strcmp(type, inType);
105 }
106
107 // Setters for the EC public key components
108 void OSSLECPublicKey::setEC(const ByteString& inEC)
109 {
110 ECPublicKey::setEC(inEC);
111
112 EC_GROUP* grp = OSSL::byteString2grp(inEC);
113 EC_KEY_set_group(eckey, grp);
114 EC_GROUP_free(grp);
115 }
116
117 void OSSLECPublicKey::setQ(const ByteString& inQ)
118 {
119 ECPublicKey::setQ(inQ);
120
121 EC_POINT* pub = OSSL::byteString2pt(inQ, EC_KEY_get0_group(eckey));
122 EC_KEY_set_public_key(eckey, pub);
123 EC_POINT_free(pub);
124 }
125
126 // Retrieve the OpenSSL representation of the key
127 EC_KEY* OSSLECPublicKey::getOSSLKey()
128 {
129 return eckey;
130 }
131 #endif