├── CMakeLists.txt ├── cpace.h ├── test.c ├── cpace.c ├── README.md └── LICENSE /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.9) 2 | project(cPace LANGUAGES C DESCRIPTION 3 | "An C implementation of cPace that uses OpenSSL as the crypto library" 4 | ) 5 | 6 | find_package(OpenSSL 1.1.0 REQUIRED COMPONENTS Crypto) 7 | 8 | add_library(cpace STATIC cpace.c cpace.h) 9 | target_link_libraries(cpace PRIVATE OpenSSL::Crypto) 10 | 11 | add_executable(test test.c cpace.h) 12 | target_link_libraries(test cpace) 13 | -------------------------------------------------------------------------------- /cpace.h: -------------------------------------------------------------------------------- 1 | #ifndef CPACE_H 2 | #define CPACE_H 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | #include 9 | 10 | #define CPACE_PUBKEY_SIZE 32 11 | #define CPACE_ISK_SIZE 64 12 | 13 | int cpace_is_initialized(); 14 | int cpace_init(); 15 | void cpace_clean(); 16 | 17 | int cpace_elligator25519(unsigned char *point, 18 | const unsigned char *u, int u_size); 19 | 20 | typedef struct cpace_challenge_data_ cpace_challenge_data; 21 | void cpace_challenge_data_free(cpace_challenge_data *challenge); 22 | 23 | int cpace_challenge_start(unsigned char *ya, cpace_challenge_data **challenge, 24 | const char *prs, size_t prs_size, 25 | const unsigned char *sid, size_t sid_size, 26 | const char *ci, size_t ci_size); 27 | 28 | int cpace_respond(unsigned char *isk, 29 | unsigned char *yb, const unsigned char *ya, 30 | const char *prs, size_t prs_size, 31 | const unsigned char *sid, size_t sid_size, 32 | const char *ci, size_t ci_size); 33 | 34 | int cpace_challenge_finish(unsigned char *isk, cpace_challenge_data *challenge, 35 | const unsigned char *yb); 36 | 37 | int cpace_random_sid(unsigned char *sid, size_t sid_size); 38 | void cpace_cleanse(void *ptr, size_t size); 39 | 40 | #ifdef __cplusplus 41 | } 42 | #endif 43 | 44 | #endif // CPACE_H 45 | -------------------------------------------------------------------------------- /test.c: -------------------------------------------------------------------------------- 1 | #include "cpace.h" 2 | 3 | #include 4 | #include 5 | 6 | #define STRING2(X) #X 7 | #define STRING(X) STRING2(X) 8 | 9 | #define MAX_PASSPHRASE_SIZE 64 10 | #define SID_SIZE 16 11 | #define BYTES_PER_LINE 16 12 | 13 | #define IDENTITY_A "PartyA" 14 | #define IDENTITY_B "PartyB" 15 | #define IDENTITY_AB "Test" 16 | #define CI (IDENTITY_A IDENTITY_B IDENTITY_AB) 17 | 18 | static void print_bytes(unsigned char *buffer, size_t size, size_t breaks) { 19 | for (size_t i = 0; i < size; ++i) { 20 | if (i == 0) 21 | printf("%02X", buffer[i]); 22 | else if (breaks > 0 && i % breaks == 0) 23 | printf(":\n%02X", buffer[i]); 24 | else 25 | printf(":%02X", buffer[i]); 26 | } 27 | } 28 | 29 | #define E(X) do { if ((X) <= 0) { error_line = __LINE__; goto error; } } while (0) 30 | 31 | int main(int argc, char *argv[]) { 32 | char prs[MAX_PASSPHRASE_SIZE + 1]; 33 | size_t prs_size; 34 | unsigned char sid[SID_SIZE]; 35 | unsigned char ya[CPACE_PUBKEY_SIZE]; 36 | unsigned char yb[CPACE_PUBKEY_SIZE]; 37 | unsigned char a_isk[CPACE_ISK_SIZE]; 38 | unsigned char b_isk[CPACE_ISK_SIZE]; 39 | cpace_challenge_data *challenge = NULL; 40 | int match, error_line; 41 | 42 | printf("Enter a passphrase: "); 43 | fflush(stdout); 44 | E(scanf("%" STRING(MAX_PASSPHRASE_SIZE) "s", prs)); 45 | prs_size = strlen(prs); 46 | E(cpace_random_sid(sid, SID_SIZE)); 47 | 48 | printf("\npassphrase: %s\n", prs); 49 | printf("sid: "); 50 | print_bytes(sid, SID_SIZE, 0); 51 | printf("\nci: %s\n", CI); 52 | 53 | // Initialize the cPace constants 54 | E(cpace_init()); 55 | 56 | // Challenger initializes the protocol 57 | E(cpace_challenge_start(ya, &challenge, prs, prs_size, 58 | sid, SID_SIZE, CI, sizeof(CI) - 1)); 59 | printf("\nChallenger's public share:\n"); 60 | print_bytes(ya, CPACE_PUBKEY_SIZE, BYTES_PER_LINE); 61 | printf("\n"); 62 | 63 | // Responder responds to the challenge 64 | E(cpace_respond(b_isk, yb, ya, prs, prs_size, 65 | sid, SID_SIZE, CI, sizeof(CI) - 1)); 66 | printf("Responder's public share:\n"); 67 | print_bytes(yb, CPACE_PUBKEY_SIZE, BYTES_PER_LINE); 68 | printf("\n"); 69 | 70 | // Challenger finishes the protocol with the response data 71 | E(cpace_challenge_finish(a_isk, challenge, yb)); 72 | 73 | // Cleanup Challenger state data 74 | cpace_challenge_data_free(challenge); 75 | 76 | // Cleanup the cPace constants 77 | cpace_clean(); 78 | 79 | printf("\nChallenger's ISK: \n"); 80 | print_bytes(a_isk, CPACE_ISK_SIZE, BYTES_PER_LINE); 81 | printf("\nResponder's ISK: \n"); 82 | print_bytes(b_isk, CPACE_ISK_SIZE, BYTES_PER_LINE); 83 | match = memcmp(a_isk, b_isk, CPACE_ISK_SIZE) == 0; 84 | printf("\nISK's match: %s\n", match ? "TRUE" : "FALSE"); 85 | 86 | // Cleanup the ISK for security (done for demonstration) 87 | cpace_cleanse(a_isk, CPACE_ISK_SIZE); 88 | cpace_cleanse(b_isk, CPACE_ISK_SIZE); 89 | 90 | return 0; 91 | error: 92 | printf("\nERROR performing the cPace procedure on line %d\n", error_line); 93 | cpace_challenge_data_free(challenge); 94 | // Cleanup the ISK for security (done for demonstration) 95 | cpace_cleanse(a_isk, CPACE_ISK_SIZE); 96 | cpace_cleanse(b_isk, CPACE_ISK_SIZE); 97 | cpace_clean(); 98 | return 1; 99 | } 100 | -------------------------------------------------------------------------------- /cpace.c: -------------------------------------------------------------------------------- 1 | #include "cpace.h" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include 9 | 10 | static EVP_PKEY_CTX *keygen_ctx = NULL; 11 | static BIGNUM *c_p = NULL, *c_r = NULL; 12 | static BIGNUM *c_j = NULL, *c_n = NULL; 13 | 14 | #define E(X) do { if((X) <= 0) goto clean; } while(0) 15 | 16 | int cpace_is_initialized() { 17 | return keygen_ctx != NULL; 18 | } 19 | 20 | int cpace_init() { 21 | int status = 0; 22 | 23 | if (cpace_is_initialized()) return 1; 24 | 25 | E(keygen_ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_X25519, NULL)); 26 | E(EVP_PKEY_keygen_init(keygen_ctx)); 27 | 28 | E(c_p = BN_new()); 29 | E(c_r = BN_new()); 30 | E(c_j = BN_new()); 31 | E(c_n = BN_new()); 32 | 33 | E(BN_lshift(c_p, BN_value_one(), 255)); 34 | E(BN_sub_word(c_p, (BN_ULONG) 19)); 35 | E(BN_rshift1(c_r, c_p)); 36 | BN_set_flags(c_r, BN_FLG_CONSTTIME); 37 | E(BN_set_word(c_j, (BN_ULONG) 486662)); 38 | E(BN_sub(c_n, c_p, c_j)); 39 | status = 1; 40 | 41 | clean: 42 | if (status <= 0) cpace_clean(); 43 | return status; 44 | } 45 | 46 | void cpace_clean() { 47 | BN_free(c_n); c_n = NULL; 48 | BN_free(c_j); c_j = NULL; 49 | BN_free(c_r); c_r = NULL; 50 | BN_free(c_p); c_p = NULL; 51 | EVP_PKEY_CTX_free(keygen_ctx); keygen_ctx = NULL; 52 | } 53 | 54 | int cpace_elligator25519(unsigned char *point, 55 | const unsigned char *u, int u_size) { 56 | int status = 0; 57 | BN_CTX *ctx = NULL; 58 | BIGNUM *x1 = NULL, *x2 = NULL, *yy = NULL, *p = NULL; 59 | 60 | E(ctx = BN_CTX_new()); 61 | E(x2 = BN_new()); 62 | E(yy = BN_new()); 63 | // Load u into x1 64 | E(x1 = BN_lebin2bn(u, u_size, NULL)); 65 | 66 | // Take x1 = u (mod 2^255 - 19) 67 | E(BN_mod(x1, x1, c_p, ctx)); 68 | // x1 = -J / (1 + Z * u^2) 69 | E(BN_mod_sqr(x1, x1, c_p, ctx)); 70 | E(BN_mod_add(x1, x1, x1, c_p, ctx)); 71 | E(BN_add(x1, x1, BN_value_one())); 72 | E(BN_mod_inverse(x1, x1, c_p, ctx)); 73 | E(BN_mod_mul(x1, x1, c_n, c_p, ctx)); 74 | // yy = y^2 = x1^3 + J * x1^2 + x1 (x2 is temp) 75 | E(BN_mod_sqr(yy, x1, c_p, ctx)); 76 | E(BN_mod_mul(x2, yy, x1, c_p, ctx)); 77 | E(BN_mod_add(x2, x2, x1, c_p, ctx)); 78 | E(BN_mod_mul(yy, yy, c_j, c_p, ctx)); 79 | E(BN_mod_add(yy, yy, x2, c_p, ctx)); 80 | // Set yy to Euler's criterion to test for y's existance 81 | E(BN_mod_exp(yy, yy, c_r, c_p, ctx)); 82 | // x2 = -J - x1 83 | E(BN_mod_sub(x2, c_n, x1, c_p, ctx)); 84 | // return x1 if it's valid, otherwise return x2 85 | p = BN_is_bit_set(yy, 2) ? x2 : x1; 86 | E(BN_bn2lebinpad(p, point, CPACE_PUBKEY_SIZE)); 87 | status = 1; 88 | 89 | clean: 90 | BN_clear_free(x1); 91 | BN_clear_free(yy); 92 | BN_clear_free(x2); 93 | BN_CTX_free(ctx); 94 | return status; 95 | } 96 | 97 | static int derive(unsigned char *out, 98 | const unsigned char *pub, EVP_PKEY *priv) { 99 | int status = 0; 100 | EVP_PKEY_CTX *ctx = NULL; 101 | EVP_PKEY *pub_key = NULL; 102 | size_t retsize = CPACE_PUBKEY_SIZE; 103 | 104 | E(pub_key = EVP_PKEY_new_raw_public_key(EVP_PKEY_X25519, NULL, 105 | pub, CPACE_PUBKEY_SIZE)); 106 | E(ctx = EVP_PKEY_CTX_new(priv, NULL)); 107 | E(EVP_PKEY_derive_init(ctx)); 108 | E(EVP_PKEY_derive_set_peer(ctx, pub_key)); 109 | E(EVP_PKEY_derive(ctx, out, &retsize)); 110 | status = (retsize == CPACE_PUBKEY_SIZE); 111 | 112 | clean: 113 | EVP_PKEY_CTX_free(ctx); 114 | return status; 115 | } 116 | //iferr(EVP_PKEY_keygen(pctx, &x), "keygen x"); 117 | 118 | #define H_block_SHA512 128 119 | #define DSI1 "CPace25519-1" 120 | #define DSI1_size (sizeof(DSI1) - 1) 121 | #define PRS_pad (H_block_SHA512 - DSI1_size) 122 | static const unsigned char zpad[PRS_pad]; 123 | 124 | static int map_to_group(unsigned char *point, 125 | const char *prs, size_t prs_size, 126 | const unsigned char *sid, size_t sid_size, 127 | const char *ci, size_t ci_size) { 128 | unsigned char md[64]; 129 | int status = 0; 130 | EVP_MD_CTX *ctx = NULL; 131 | const EVP_MD *md_alg = EVP_sha512(); 132 | 133 | if (EVP_MD_size(md_alg) != sizeof(md)) return 0; 134 | 135 | E(ctx = EVP_MD_CTX_new()); 136 | E(EVP_DigestInit_ex(ctx, md_alg, NULL)); 137 | E(EVP_DigestUpdate(ctx, DSI1, DSI1_size)); 138 | E(EVP_DigestUpdate(ctx, prs, prs_size)); 139 | if (prs_size < PRS_pad) 140 | E(EVP_DigestUpdate(ctx, zpad, PRS_pad - prs_size)); 141 | E(EVP_DigestUpdate(ctx, sid, sid_size)); 142 | E(EVP_DigestUpdate(ctx, ci, ci_size)); 143 | E(EVP_DigestFinal_ex(ctx, md, NULL)); 144 | 145 | E(cpace_elligator25519(point, md, sizeof(md))); 146 | status = 1; 147 | 148 | clean: 149 | EVP_MD_CTX_free(ctx); 150 | return status; 151 | } 152 | 153 | #define DSI2 "CPace25519-2" 154 | #define DSI2_size (sizeof(DSI2) - 1) 155 | 156 | static int final_keying(unsigned char *isk, const unsigned char *k, 157 | const unsigned char *ya, const unsigned char *yb, 158 | const unsigned char *sid, size_t sid_size) { 159 | int status = 0; 160 | EVP_MD_CTX *ctx = NULL; 161 | const EVP_MD *md_alg = EVP_sha512(); 162 | 163 | if (EVP_MD_size(md_alg) != CPACE_ISK_SIZE) return 0; 164 | 165 | E(ctx = EVP_MD_CTX_new()); 166 | E(EVP_DigestInit_ex(ctx, EVP_sha512(), NULL)); 167 | E(EVP_DigestUpdate(ctx, DSI2, DSI2_size)); 168 | E(EVP_DigestUpdate(ctx, sid, sid_size)); 169 | E(EVP_DigestUpdate(ctx, k, CPACE_PUBKEY_SIZE)); 170 | E(EVP_DigestUpdate(ctx, ya, CPACE_PUBKEY_SIZE)); 171 | E(EVP_DigestUpdate(ctx, yb, CPACE_PUBKEY_SIZE)); 172 | E(EVP_DigestFinal_ex(ctx, isk, NULL)); 173 | status = 1; 174 | 175 | clean: 176 | EVP_MD_CTX_free(ctx); 177 | return status; 178 | } 179 | 180 | struct cpace_challenge_data_ { 181 | EVP_PKEY *pkey; 182 | unsigned char *sid; 183 | size_t sid_size; 184 | unsigned char ya[CPACE_PUBKEY_SIZE]; 185 | }; 186 | 187 | void cpace_challenge_data_free(cpace_challenge_data *challenge) { 188 | if (challenge) { 189 | EVP_PKEY_free(challenge->pkey); 190 | OPENSSL_free(challenge->sid); 191 | } 192 | OPENSSL_free(challenge); 193 | } 194 | 195 | int cpace_challenge_start(unsigned char *ya, cpace_challenge_data **challenge, 196 | const char *prs, size_t prs_size, 197 | const unsigned char *sid, size_t sid_size, 198 | const char *ci, size_t ci_size) { 199 | unsigned char g[CPACE_PUBKEY_SIZE]; 200 | int status = 0; 201 | cpace_challenge_data *data = NULL; 202 | 203 | E(data = OPENSSL_malloc(sizeof(cpace_challenge_data))); 204 | data->pkey = NULL; 205 | E(data->sid = OPENSSL_malloc(sid_size)); 206 | data->sid_size = sid_size; 207 | 208 | E(map_to_group(g, prs, prs_size, sid, sid_size, ci, ci_size)); 209 | E(EVP_PKEY_keygen(keygen_ctx, &data->pkey)); 210 | E(derive(data->ya, g, data->pkey)); 211 | 212 | memcpy(data->sid, sid, sid_size); 213 | memcpy(ya, data->ya, CPACE_PUBKEY_SIZE); 214 | *challenge = data; 215 | status = 1; 216 | 217 | clean: 218 | if (status <= 0) cpace_challenge_data_free(data); 219 | OPENSSL_cleanse(g, CPACE_PUBKEY_SIZE); 220 | return status; 221 | } 222 | 223 | int cpace_respond(unsigned char *isk, 224 | unsigned char *yb, const unsigned char *ya, 225 | const char *prs, size_t prs_size, 226 | const unsigned char *sid, size_t sid_size, 227 | const char *ci, size_t ci_size) { 228 | unsigned char g[CPACE_PUBKEY_SIZE]; 229 | unsigned char k[CPACE_PUBKEY_SIZE]; 230 | int status = 0; 231 | EVP_PKEY *pkey = NULL; 232 | 233 | E(map_to_group(g, prs, prs_size, sid, sid_size, ci, ci_size)); 234 | E(EVP_PKEY_keygen(keygen_ctx, &pkey)); 235 | E(derive(yb, g, pkey)); 236 | 237 | E(derive(k, ya, pkey)); 238 | E(final_keying(isk, k, ya, yb, sid, sid_size)); 239 | status = 1; 240 | 241 | clean: 242 | EVP_PKEY_free(pkey); 243 | OPENSSL_cleanse(g, CPACE_PUBKEY_SIZE); 244 | OPENSSL_cleanse(k, CPACE_PUBKEY_SIZE); 245 | return status; 246 | } 247 | 248 | int cpace_challenge_finish(unsigned char *isk, cpace_challenge_data *challenge, 249 | const unsigned char *yb) { 250 | unsigned char k[CPACE_PUBKEY_SIZE]; 251 | int status = 0; 252 | 253 | E(derive(k, yb, challenge->pkey)); 254 | E(final_keying(isk, k, challenge->ya, yb, 255 | challenge->sid, challenge->sid_size)); 256 | status = 1; 257 | 258 | clean: 259 | OPENSSL_cleanse(k, CPACE_PUBKEY_SIZE); 260 | return status; 261 | } 262 | 263 | int cpace_random_sid(unsigned char *sid, size_t sid_size) { 264 | return RAND_bytes(sid, sid_size); 265 | } 266 | 267 | void cpace_cleanse(void *ptr, size_t size) { 268 | OPENSSL_cleanse(ptr, size); 269 | } 270 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # cPace-OpenSSL 2 | A C implementation of cPace that uses OpenSSL as the crypto library. 3 | 4 | This library implements the **CPACE-X25519-ELLIGATOR2_SHA512-SHA512** ciphersuite for cPace using OpenSSL for its implementation of SHA512, X25519, and BIGNUM. 5 | 6 | The cPace specification refers to the two parties as Party A and Party B. However, these names can make following the protocol a little more difficult. The API uses a different naming scheme for the parties of the protocol. Party A, who initiates the protocol, is known in the API as the Challenger, and Party B, who waits for the other party's public share, is known as the Responder. Their actions in the API are called "challenge" and "response" respectively. 7 | 8 | This implementation **SHOULD NOT** be used in any security-critical scenarios. The implementation of cPace here, particularly the implementation of Elligator 2, is not well protected against side-channel attacks when using OpenSSL. This is primarily due to problems with the implementation of BIGNUM in OpenSSL, namely [openssl/openssl#6078](https://github.com/openssl/openssl/issues/6078) and [openssl/openssl#6640](https://github.com/openssl/openssl/issues/6640). Using BoringSSL improves timing attack resilience, but it's still not recommended to use this library when security against side-channel attacks are a priority. 9 | 10 | There are other implementations of cPace out there, such as [jedisct1/cpace](https://github.com/jedisct1/cpace), but those implementations don't use either of the draft specification's ciphersuites and instead implement their own using Ristretto255. This implementation follows the draft specification's definition for the X25519 ciphersuite. 11 | 12 | The specification version of cPace implemented is [draft-irtf-cfrg-cpace-00](https://tools.ietf.org/html/draft-irtf-cfrg-cpace-00) 13 | 14 | ## Example 15 | 16 | [test.c](https://github.com/LRFLEW/cPace-OpenSSL/blob/main/test.c) includes a short command-line program to demonstrate how the API can be used. Below is a sample output of the program: 17 | 18 | ``` 19 | Enter a passphrase: hunter2 20 | 21 | passphrase: hunter2 22 | sid: EB:08:F6:30:19:C1:08:F0:89:02:17:82:EC:86:9C:33 23 | ci: PartyAPartyBTest 24 | 25 | Challenger's public share: 26 | 0D:0E:1E:35:A1:F3:28:49:72:34:A3:1F:CE:DE:C0:68: 27 | FA:D6:44:54:81:FC:51:D0:42:B2:F6:EC:9C:64:AF:5E 28 | Responder's public share: 29 | 49:98:D7:39:AC:9F:EC:54:1D:92:23:8C:5A:C9:D3:34: 30 | 8F:75:1B:1B:8B:31:31:B0:11:72:84:E6:F1:DF:67:3D 31 | 32 | Challenger's ISK: 33 | 93:F4:0F:5C:E9:F3:22:11:EB:4C:AE:05:85:64:A2:2D: 34 | 98:74:AE:B3:A8:C9:81:31:F2:77:75:8D:E6:13:B6:24: 35 | F3:EF:DC:A3:B6:24:30:07:FF:F2:FF:EA:FF:89:4C:00: 36 | EE:93:AD:0E:79:33:52:B2:FA:26:07:74:4D:83:8A:18 37 | Responder's ISK: 38 | 93:F4:0F:5C:E9:F3:22:11:EB:4C:AE:05:85:64:A2:2D: 39 | 98:74:AE:B3:A8:C9:81:31:F2:77:75:8D:E6:13:B6:24: 40 | F3:EF:DC:A3:B6:24:30:07:FF:F2:FF:EA:FF:89:4C:00: 41 | EE:93:AD:0E:79:33:52:B2:FA:26:07:74:4D:83:8A:18 42 | ISK's match: TRUE 43 | ``` 44 | 45 | ## API 46 | 47 | The API header is [cpace.h](https://github.com/LRFLEW/cPace-OpenSSL/blob/main/cpace.h). All the return values in the API follow OpenSSL's return code system, where `1` indicates success and `0` indicates an error occurred. 48 | 49 | ### `#define CPACE_PUBKEY_SIZE 32` 50 | ### `#define CPACE_ISK_SIZE 64` 51 | 52 | Macros for the size in bytes required for Ya/Yb and the ISK respectively. Any buffers used for these variables should be at least as big as these values specify. 53 | 54 | ### `int cpace_init()` 55 | 56 | Initializes global constants utilized by Elligator 2 and private key generation. This function **must** be called before calling any other functions in this API unless otherwise specified, and should be called once at the start of the program. 57 | 58 | ### `void cpace_clean()` 59 | 60 | Cleans the global constants that are initialized by `cpace_init()`. It is safe to call `cpace_init()` when it's already initialized and to call `cpace_clean()` when it's uninitialized or already cleaned. However, there is no reference counting in the API, so calling `cpace_clean()` will clean up the global constants independent to the number of calls made to `cpace_init()`. Because of this, it's recommended to only call these functions once each in the lifetime of your program. 61 | 62 | ### `int cpace_is_initialized()` 63 | 64 | Returns `1` if the global constants are initialized and `0` if it is uninitialized or cleaned. This function may be called at any time. 65 | 66 | ### `int cpace_elligator25519();` 67 | 68 | **Args:** 69 | 70 | `unsigned char *point` **Return Arg**: The u-coordinate of the point generated by Elligator 2 71 | 72 | `const unsigned char *u`: The input value for Elligator 2 in little-endian 73 | 74 | `int u_size`: The size in bytes of `u` 75 | 76 | Receives a little-endian value `u` with a size of `u_size` bytes, takes the value `u (mod 2^255 - 19)`, and returns the u-coordinate of Elligator 2's output in the buffer `point`. `point` must be at least `CPACE_PUBKEY_SIZE` bytes in size. 77 | 78 | ### `typedef struct cpace_challenge_data_ cpace_challenge_data;` 79 | 80 | An opaque structure used to store data while Party A (the Challenger) is awaiting a response from Party B (the Responder). 81 | 82 | ### `void cpace_challenge_data_free()` 83 | 84 | **Args:** 85 | 86 | `cpace_challenge_data *challenge`: The pointer the challenge data to free 87 | 88 | Deallocates the challenge data created by `cpace_challenge_start()`. This should be called when the challenge data is no longer required, either after a successful or failed call to `cpace_challenge_finish()` or after a timeout occurs waiting for a response. Setting `challenge` to `NULL` results in no operation being performed. 89 | 90 | ### `int cpace_challenge_start()` 91 | 92 | **Args:** 93 | 94 | `unsigned char *ya` **Return Arg**: The Challenger's public share 95 | 96 | `cpace_challenge_data **challenge` **Return Arg**: The challenge data required by `cpace_challenge_finish()` 97 | 98 | `const char *prs`: The Password Related String (password/passphrase) 99 | 100 | `size_t prs_size`: The size of the PRS in bytes 101 | 102 | `const unsigned char *sid`: The Session ID 103 | 104 | `size_t sid_size`: The size of the SID in bytes 105 | 106 | `const char *ci`: The CI string, formed as the concatenation of identities and optional additional data 107 | 108 | `size_t ci_size`: The size of the CI string in bytes 109 | 110 | Initializes the protocol as Party A (the Challenger) with the provided data. The value `ya` shall be sent to other party, while the challenge data `*challenge` is kept until a response is received. If an error occurs and the function returns `0`, then `*challenge` will be uninitialized and does not require freeing. 111 | 112 | ### `int cpace_respond()` 113 | 114 | **Args:** 115 | 116 | `unsigned char *isk` **Return Arg**: The Intermediate Session Key (final output of cPace) 117 | 118 | `unsigned char *yb` **Return Arg**: The Responder's public share 119 | 120 | `const unsigned char *ya`: The Challenger's public share 121 | 122 | `const char *prs`: The Password Related String (password/passphrase) 123 | 124 | `size_t prs_size`: The size of the PRS in bytes 125 | 126 | `const unsigned char *sid`: The Session ID 127 | 128 | `size_t sid_size`: The size of the SID in bytes 129 | 130 | `const char *ci`: The CI string, formed as the concatenation of identities and optional additional data 131 | 132 | `size_t ci_size`: The size of the CI string in bytes 133 | 134 | Performs the protocol as Party B (the Challenger) with the other party's public share and the provided data. The value `yb` shall be sent to the other party, while the value `isk` is kept for encryption/authentication with the other party. It is recommended to call `cpace_cleanse()` with `isk` when you are done with its value. 135 | 136 | ### `int cpace_challenge_finish()` 137 | 138 | **Args:** 139 | 140 | `unsigned char *isk` **Return Arg**: The Intermediate Session Key (final output of cPace) 141 | 142 | `cpace_challenge_data *challenge`: The challenge data from by `cpace_challenge_start()` 143 | 144 | `const unsigned char *yb`: The Responder's public share 145 | 146 | Performs the final steps as Party A (the Challenger) to get the final value `isk`. The value is used for encryption/authentication with the other party. It is recommended to call `cpace_cleanse()` with `isk` when you are done with its value. 147 | 148 | ### `int cpace_random_sid();` 149 | 150 | **Args:** 151 | 152 | `unsigned char *sid` **Return Arg**: The randomly generated SID 153 | 154 | `size_t sid_size`: The size of the SID value in bytes to generate 155 | 156 | A simple wrapper for OpenSSL's `RAND_bytes` provided as an option for generating a SID. The SID does *not* need to be generated this way. The standard specifies that "sid is typically pre-established by a higher-level protocol invoking CPace." This method may be used of no such higher-level session ID is available. This function may be called at any time. 157 | 158 | ### `void cpace_cleanse()` 159 | 160 | **Args:** 161 | 162 | `void *ptr`: The buffer to cleanse 163 | 164 | `size_t size`: The size of the buffer in bytes 165 | 166 | A simple wrapper for OpenSSL's `OPENSSL_cleanse`, which writes zero bytes to the provided buffer. This prevents the zero-fill from being optimized out (which can happen if directly calling `memset`). This function may be called at any time. 167 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | --------------------------------------------------------------------------------