aboutsummaryrefslogtreecommitdiffstats
path: root/libparc/parc/security/parc_DiffieHellmanKeyShare.h
blob: 54e0f4588413380531ed9d912c918a8406349757 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
 * Copyright (c) 2017 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @file parc_DiffieHellmanKeyShare.h
 * @ingroup security
 * @brief A Diffie Hellman key share.
 *
 */
#ifndef libparc_parc_DiffieHellmanKeyShare_h
#define libparc_parc_DiffieHellmanKeyShare_h

#include <parc/security/parc_DiffieHellmanGroup.h>

struct parc_diffie_hellman_keyshare;
typedef struct parc_diffie_hellman_keyshare PARCDiffieHellmanKeyShare;

/**
 * Create a `PARCDiffieHellmanKeyShare` instance to hold one public and private
 * Diffie Hellman key share for the specified group.
 *
 * @param [in] groupType A type of PARCDiffieHellmanGroup
 *
 * @return NULL Memory could not be allocated.
 * @return non-NULL A pointer to a `PARCDiffieHellmanKeyShare` instance.
 *
 * Example:
 * @code
 * {
 *     PARCDiffieHellmanKeyShare *keyShare = parcDiffieHellmanKeyShare_Create(PARCDiffieHellmanGroup_Secp521r1);
 *
 *     parcDiffieHellmanKeyShare_Release(&keyShare);
 * }
 * @endcode
 */
PARCDiffieHellmanKeyShare *parcDiffieHellmanKeyShare_Create(PARCDiffieHellmanGroup groupType);

/**
 * Increase the number of references to an instance of this object.
 *
 * Note that new instance is not created, only that the given instance's reference count
 * is incremented. Discard the reference by invoking `parcDiffieHellman_Release()`.
 *
 * @param [in] keyShare A `PARCDiffieHellmanKeyShare` instance.
 *
 * @return The value of the input parameter @p instance.
 *
 * Example:
 * @code
 * {
 *     ...
 *
 *     PARCDiffieHellmanKeyShare *keyShare = parcDiffieHellmanKeyShare_Acquire(keyShareInstance);
 *
 *     parcDiffieHellmanKeyShare_Release(&keyShare);
 * }
 * @endcode
 *
 * @see parcDiffieHellmanKeyShare_Release
 */
PARCDiffieHellmanKeyShare *parcDiffieHellmanKeyShare_Acquire(const PARCDiffieHellmanKeyShare *keyShare);

/**
 * Release a previously acquired reference to the specified instance,
 * decrementing the reference count for the instance.
 *
 * The pointer to the instance is set to NULL as a side-effect of this function.
 *
 * If the invocation causes the last reference to the instance to be released,
 * the instance is deallocated and the instance's implementation will perform
 * additional cleanup and release other privately held references.
 *
 * @param [in,out] keyShareP A pointer to a pointer to the instance to release.
 *
 * Example:
 * @code
 * {
 *     ...
 *
 *     PARCDiffieHellmanKeyShare *keyShare = parcDiffieHellmanKeyShare_Acquire(keyShareInstance);
 *
 *     parcDiffieHellmanKeyShare_Release(&keyShare);
 * }
 * @endcode
 */
void parcDiffieHellmanKeyShare_Release(PARCDiffieHellmanKeyShare **keyShareP);

/**
 * Serialize the public key part of a `PARCDiffieHellmanKeyShare.`
 *
 * The public key is saved to a `PARCBuffer` and can be used for transport if needed.
 *
 * @param [in] keyShare A `PARCDiffieHellmanKeyShare` instance.
 *
 * @return A `PARCBuffer` containing the public key of this key share.
 *
 * Example:
 * @code
 * {
 *     PARCDiffieHellmanKeyShare *keyShare = parcDiffieHellmanKeyShare_Create(PARCDiffieHellmanGroup_Secp521r1);
 *
 *     PARCBuffer *publicKey = parcDiffieHellmanKeyShare_SerializePublicKey(keyShare);
 *     // use the public key
 *
 *     parcBuffer_Release(&publicKey);
 *     parcDiffieHellmanKeyShare_Release(&keyShare);
 * }
 * @endcode
 */
PARCBuffer *parcDiffieHellmanKeyShare_SerializePublicKey(PARCDiffieHellmanKeyShare *keyShare);

/**
 * Combine a `PARCDiffieHellmanKeyShare` with an encoded public key to create a shared secret.
 *
 * @param [in] keyShare A `PARCDiffieHellmanKeyShare` instance.
 * @param [in] publicShare The public key share to use to derive the shared secrect.
 *
 * @return A `PARCBuffer` containing the shared secret from the Diffie Hellman exchange.
 *
 * Example:
 * @code
 * {
 *     PARCDiffieHellmanKeyShare *keyShare = parcDiffieHellmanKeyShare_Create(PARCDiffieHellmanGroup_Secp521r1);
 *
 *     PARCBuffer *publicKey = parcDiffieHellmanKeyShare_SerializePublicKey(keyShare);
 *
 *     ...
 *
 *     PARCBuffer *sharedSecret = parcDiffieHellmanKeyShare_Combine(keyShare, publicKey);
 *     // use the shared secret to derive other cryptographic secrets.
 *
 *     parcBuffer_Release(&sharedSecret);
 *     parcBuffer_Release(&publicKey);
 *     parcDiffieHellmanKeyShare_Release(&keyShare);
 * }
 * @endcode
 */
PARCBuffer *parcDiffieHellmanKeyShare_Combine(PARCDiffieHellmanKeyShare *keyShare, PARCBuffer *publicShare);
#endif // libparc_parc_DiffieHellmanKeyShare_h