<p>This is the API documentation of libpcp, the library behind <ahref="/PrettyCurvedPrivacy">Pretty Curved Privacy (pcp)</a>. The library can be used independently of pcp to manage keys and to encrypt or sign files or buffers.</p>
<p>For most actual crypto related things, libpcp uses <ahref="https://github.com/jedisct1/libsodium">libsodium, the portable NaCL library</a>.</p>
<divclass="line"><aclass="code"href="struct__pcp__pubkey__t.html"title="PCP public key structure.">pcp_pubkey_t</a> *alicepub, *bobpub, *pubhash;</div>
<divclass="line"><aclass="code"href="struct__pcp__stream__t.html"title="An I/O wrapper object backed by a file or a buffer.">Pcpstream</a> *clear_in, *crypt_out, *clear_out;</div>
<divclass="line"><spanclass="comment">/* generate the keypairs for both */</span></div>
<divclass="line"> alice = <aclass="code"href="group__KEYS.html#gaecf6a1829b4133350a7cb694c3eca8ad"title="Generate a new key structure.">pcpkey_new</a>();</div>
<divclass="line"> bob = <aclass="code"href="group__KEYS.html#gaecf6a1829b4133350a7cb694c3eca8ad"title="Generate a new key structure.">pcpkey_new</a>();</div>
<divclass="line"></div>
<divclass="line"><spanclass="comment">/* get the public parts of them */</span></div>
<divclass="line"> alicepub = <aclass="code"href="group__KEYS.html#ga22c965c007208e860deaa4c145046283"title="Generate a public key structure from a given secret key structure.">pcpkey_pub_from_secret</a>(alice);</div>
<divclass="line"> bobpub = <aclass="code"href="group__KEYS.html#ga22c965c007208e860deaa4c145046283"title="Generate a public key structure from a given secret key structure.">pcpkey_pub_from_secret</a>(bob);</div>
<divclass="line"></div>
<divclass="line"><spanclass="comment">/* put the clear text message into the stream */</span></div>
<divclass="line"> inbuf = <aclass="code"href="group__Buffer.html#ga55e468b36750f28308b7fa5dfea5b564"title="Create a new buffer.">buffer_new</a>(32, <spanclass="stringliteral">"a"</span>);</div>
<divclass="line"><aclass="code"href="group__Buffer.html#ga5b99c0ae55b2e279339c745b6fd7bb21"title="Add a formated string to the buffer.">buffer_add_str</a>(inbuf, message);</div>
<divclass="line"> clear_in = <aclass="code"href="group__Pcpstream.html#ga1b50c92fdad290e59317af2404460662"title="Create a new input stream, backed with filled a buffer.">ps_new_inbuffer</a>(inbuf);</div>
<divclass="line"></div>
<divclass="line"><spanclass="comment">/* create the output stream as buffer */</span></div>
<divclass="line"> crypt_out = <aclass="code"href="group__Pcpstream.html#ga13ec0245b579949e6d586e1817615d2e"title="Create a new output stream, backed with a buffer.">ps_new_outbuffer</a>();</div>
<divclass="line"></div>
<divclass="line"><spanclass="comment">/* prepare the pubkey recipient list (only 1 recipient: Bob) */</span></div>
<divclass="line"> pubhash = NULL;</div>
<divclass="line"> strncpy(bobpub-><aclass="code"href="struct__pcp__pubkey__t.html#ad10eadcb9325538e71c9fd8b140dbdd2"title="key-id, used internally only, jenhash of public keys">id</a>, <aclass="code"href="group__KEYS.html#gab16e85cdb769465c8ce8808b581c7a3d"title="Calculate a key-id from public key fields.">pcp_getpubkeyid</a>(bobpub), 17);</div>
<divclass="line"><spanclass="comment">/* actually encrypt the message, don't sign it</span></div>
<divclass="line"><spanclass="comment"> Alice is the sender, Bob is the recipient */</span></div>
<divclass="line"><aclass="code"href="group__CRYPTO.html#gadbdb839246cac65ff20ef63902041597"title="Asymmetrically encrypt a file or a buffer stream.">pcp_encrypt_stream</a>(clear_in, crypt_out, alice, pubhash, 0);</div>
<divclass="line"></div>
<divclass="line"><spanclass="comment">/* now, print the encrypted result */</span></div>
<divclass="line"> fprintf(stderr, <spanclass="stringliteral">"Alice encrypted %ld bytes for Bob:\n"</span>, strlen(message));</div>
<divclass="line"><aclass="code"href="group__Buffer.html#ga0e0f433cbd077d3048ebdda35aa9ccb6"title="Dump the Buffer contents to stderr in hex form.">buffer_dump</a>(<aclass="code"href="group__Pcpstream.html#ga64bc34dfbc1b3951c3d62e82a1ae8c34"title="Access the Buffer backend pointer.">ps_buffer</a>(crypt_out));</div>
<divclass="line"></div>
<divclass="line"><spanclass="comment">/* ---- encryption don, now decrypt ---- */</span></div>
<divclass="line"></div>
<divclass="line"><spanclass="comment">/* prepare the output buffer stream */</span></div>
<divclass="line"> clear_out = <aclass="code"href="group__Pcpstream.html#ga13ec0245b579949e6d586e1817615d2e"title="Create a new output stream, backed with a buffer.">ps_new_outbuffer</a>();</div>
<divclass="line"></div>
<divclass="line"><spanclass="comment">/* in order for the decryptor find the senders public key,</span></div>
<divclass="line"><spanclass="comment"> we need to put it into the global hash. this step can be</span></div>
<divclass="line"><spanclass="comment"> omitted when using a Vault. */</span></div>
<divclass="line"><aclass="code"href="group__KEYHASH.html#ga2953fd446079220a948693dfab5d6055"title="Global hash for public keys.">pcppubkey_hash</a> = NULL;</div>
<divclass="line"> HASH_ADD_STR( <aclass="code"href="group__KEYHASH.html#ga2953fd446079220a948693dfab5d6055"title="Global hash for public keys.">pcppubkey_hash</a> , <spanclass="keywordtype">id</span>, alicepub);</div>
<divclass="line"></div>
<divclass="line"><spanclass="comment">/* try to decrypt the message */</span></div>
<divclass="line"><spanclass="keywordflow">if</span>(<aclass="code"href="group__CRYPTO.html#gaeb1196ed4a3edc911e77d1d15612c539"title="Asymmetrically decrypt a file or a buffer stream.">pcp_decrypt_stream</a>(crypt_out, clear_out, bob, NULL, 0) == 0)</div>
<divclass="line"><aclass="code"href="group__FATALS.html#ga0ea01da59af8a06c55258d78da6cb0cb"title="Prints error messages to STDERR, if there are some.">fatals_ifany</a>();</div>
<divclass="line"><spanclass="comment">/* and finally print out the decrypted message */</span></div>
<divclass="line"> fprintf(stderr, <spanclass="stringliteral">"Bob decrypted %ld bytes from Alice:\n"</span>, <aclass="code"href="group__Buffer.html#ga714b0a831d0f31a33e66e8ef76a26e46"title="Tell how much data there is in the buffer available.">buffer_size</a>(<aclass="code"href="group__Pcpstream.html#ga64bc34dfbc1b3951c3d62e82a1ae8c34"title="Access the Buffer backend pointer.">ps_buffer</a>(crypt_out)));</div>
<divclass="line"> printf(<spanclass="stringliteral">"Decrypted message: %s\n"</span>, <aclass="code"href="group__Buffer.html#ga8c89e4ab5d03dff079b7b861a1e04a7f"title="Read the whole Buffer content as string.">buffer_get_str</a>(<aclass="code"href="group__Pcpstream.html#ga64bc34dfbc1b3951c3d62e82a1ae8c34"title="Access the Buffer backend pointer.">ps_buffer</a>(clear_out)));</div>
<divclass="line"> }</div>
<divclass="line"></div>
<divclass="line"><aclass="code"href="group__Pcpstream.html#ga4a68da22eca6e9bd854d54467a071d0a"title="Close the stream and frees allocated memory.">ps_close</a>(clear_in);</div>
<divclass="line"><aclass="code"href="group__Pcpstream.html#ga4a68da22eca6e9bd854d54467a071d0a"title="Close the stream and frees allocated memory.">ps_close</a>(crypt_out);</div>
<divclass="line"><aclass="code"href="group__Pcpstream.html#ga4a68da22eca6e9bd854d54467a071d0a"title="Close the stream and frees allocated memory.">ps_close</a>(clear_out);</div>