<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html lang="en" style='--code-editor-font: var(--default-mono-font, "GitLab Mono"), JetBrains Mono, Menlo, DejaVu Sans Mono, Liberation Mono, Consolas, Ubuntu Mono, Courier New, andale mono, lucida console, monospace;'>
<head>
<meta content="text/html; charset=US-ASCII" http-equiv="Content-Type">
<title>
GitLab
</title>

<style data-premailer="ignore" type="text/css">
a { color: #1068bf; }
</style>

<style>img {
max-width: 100%; height: auto;
}
body {
font-size: 0.875rem;
}
body {
-webkit-text-shadow: rgba(255,255,255,0.01) 0 0 1px;
}
body {
font-family: var(--default-regular-font, "GitLab Sans"),-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,"Noto Sans",Ubuntu,Cantarell,"Helvetica Neue",sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol","Noto Color Emoji"; font-size: inherit;
}
</style>
</head>
<body style='font-size: inherit; -webkit-text-shadow: rgba(255,255,255,0.01) 0 0 1px; font-family: var(--default-regular-font, "GitLab Sans"),-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,"Noto Sans",Ubuntu,Cantarell,"Helvetica Neue",sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol","Noto Color Emoji";'>
<div class="content">

<p style="color: #777777;">
<a href="https://gitlab.com/vivien_">Vivien Kraus Would Rather Not Be On Gitlab.com</a>
commented on a
<a href="https://gitlab.com/gnutls/gnutls/-/issues/1494#note_1475467677">discussion</a>:
</p>
<div class="md" style="color: #333238; word-wrap: break-word;">
<p dir="auto" style="color: #333238; margin: 0 0 16px;" align="initial"><a href="https://gitlab.com/jas" data-reference-type="user" data-user="47250" data-container="body" data-placement="top" class="gfm gfm-project_member js-user-link" title="Simon Josefsson" style="background-color: #cbe2f9; border-radius: 4px; color: #0b5cad; margin-top: 0; padding: 0 2px;">@jas</a> can you try this with gnutls:</p>
<div class="gl-relative markdown-code-block js-markdown-code" style="margin-bottom: 0;">
<pre data-canonical-lang="c" class="code highlight js-syntax-highlight language-c" lang="c" v-pre="true" style='display: block; font-size: 14px; color: #333238; line-height: 1.6em; overflow-x: auto; border-radius: 4px; position: relative; font-family: var(--default-mono-font, "GitLab Mono"),"JetBrains Mono","Menlo","DejaVu Sans Mono","Liberation Mono","Consolas","Ubuntu Mono","Courier New","andale mono","lucida console",monospace; word-break: break-all; word-wrap: break-word; background-color: #fbfafd; margin: 0 0 16px; padding: 12px; border: 1px solid #dcdcde;'><code style='font-size: 0.875rem; color: inherit; word-wrap: normal; word-break: keep-all; background-color: inherit; border-radius: 4px; white-space: pre; margin-top: 0; font-family: var(--default-mono-font, "GitLab Mono"),"JetBrains Mono","Menlo","DejaVu Sans Mono","Liberation Mono","Consolas","Ubuntu Mono","Courier New","andale mono","lucida console",monospace; overflow-wrap: normal; padding: unset;'><span id="LC1" class="line" lang="c" style="margin-top: 0;"><span class="cp" style="margin-top: 0;">#include</span> <span class="cpf"><gnutls/crypto.h></span></span>
<span id="LC2" class="line" lang="c"><span class="cp" style="margin-top: 0;">#include</span> <span class="cpf"><stdlib.h></span></span>
<span id="LC3" class="line" lang="c"><span class="cp" style="margin-top: 0;">#include</span> <span class="cpf"><stdio.h></span></span>
<span id="LC4" class="line" lang="c"><span class="cp" style="margin-top: 0;">#include</span> <span class="cpf"><string.h></span></span>
<span id="LC5" class="line" lang="c"></span>
<span id="LC6" class="line" lang="c"><span class="kt" style="margin-top: 0;">int</span></span>
<span id="LC7" class="line" lang="c"><span class="nf" style="margin-top: 0;">main</span> <span class="p">()</span></span>
<span id="LC8" class="line" lang="c"><span class="p" style="margin-top: 0;">{</span></span>
<span id="LC9" class="line" lang="c">  <span class="k" style="margin-top: 0;">static</span> <span class="k">const</span> <span class="n">gnutls_cipher_algorithm_t</span> <span class="n">algo</span> <span class="o">=</span> <span class="n">GNUTLS_CIPHER_AES_256_GCM</span><span class="p">;</span></span>
<span id="LC10" class="line" lang="c">  <span class="k" style="margin-top: 0;">static</span> <span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">key</span> <span class="o">=</span> <span class="s">"the secret key is 32 bytes long."</span><span class="p">;</span></span>
<span id="LC11" class="line" lang="c">  <span class="n" style="margin-top: 0;">gnutls_datum_t</span> <span class="n">datum_key</span><span class="p">;</span></span>
<span id="LC12" class="line" lang="c">  <span class="n" style="margin-top: 0;">datum_key</span><span class="p">.</span><span class="n">data</span> <span class="o">=</span> <span class="p">(</span><span class="kt">unsigned</span> <span class="kt">char</span> <span class="o">*</span><span class="p">)</span> <span class="n">key</span><span class="p">;</span></span>
<span id="LC13" class="line" lang="c">  <span class="n" style="margin-top: 0;">datum_key</span><span class="p">.</span><span class="n">size</span> <span class="o">=</span> <span class="n">strlen</span> <span class="p">(</span><span class="n">key</span><span class="p">);</span></span>
<span id="LC14" class="line" lang="c">  <span class="n" style="margin-top: 0;">gnutls_aead_cipher_hd_t</span> <span class="n">handle</span><span class="p">;</span></span>
<span id="LC15" class="line" lang="c">  <span class="k" style="margin-top: 0;">if</span> <span class="p">(</span><span class="n">gnutls_aead_cipher_init</span> <span class="p">(</span><span class="o">&</span><span class="n">handle</span><span class="p">,</span> <span class="n">algo</span><span class="p">,</span> <span class="o">&</span><span class="n">datum_key</span><span class="p">))</span></span>
<span id="LC16" class="line" lang="c">    <span class="p" style="margin-top: 0;">{</span></span>
<span id="LC17" class="line" lang="c">      <span class="k" style="margin-top: 0;">return</span> <span class="n">EXIT_FAILURE</span><span class="p">;</span></span>
<span id="LC18" class="line" lang="c">    <span class="p" style="margin-top: 0;">}</span></span>
<span id="LC19" class="line" lang="c">  <span class="k" style="margin-top: 0;">static</span> <span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">nonce</span> <span class="o">=</span> <span class="s">"Never encrypt more data with this nonce"</span><span class="p">;</span></span>
<span id="LC20" class="line" lang="c">  <span class="k" style="margin-top: 0;">static</span> <span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">auth</span> <span class="o">=</span> <span class="s">"Additional secret data"</span><span class="p">;</span></span>
<span id="LC21" class="line" lang="c">  <span class="k" style="margin-top: 0;">static</span> <span class="k">const</span> <span class="kt">int</span> <span class="n">tag_size</span> <span class="o">=</span> <span class="mi">16</span><span class="p">;</span></span>
<span id="LC22" class="line" lang="c">  <span class="k" style="margin-top: 0;">static</span> <span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">data</span> <span class="o">=</span> <span class="s">"Confidential data."</span><span class="p">;</span></span>
<span id="LC23" class="line" lang="c">  <span class="kt" style="margin-top: 0;">size_t</span> <span class="n">used_size</span> <span class="o">=</span> <span class="n">tag_size</span> <span class="o">+</span> <span class="n">strlen</span> <span class="p">(</span><span class="n">data</span><span class="p">);</span></span>
<span id="LC24" class="line" lang="c">  <span class="kt" style="margin-top: 0;">char</span> <span class="n">output</span><span class="p">[</span><span class="n">used_size</span><span class="p">];</span></span>
<span id="LC25" class="line" lang="c">  <span class="k" style="margin-top: 0;">if</span> <span class="p">(</span><span class="n">gnutls_aead_cipher_encrypt</span> <span class="p">(</span><span class="n">handle</span><span class="p">,</span></span>
<span id="LC26" class="line" lang="c">                                  <span class="n" style="margin-top: 0;">nonce</span><span class="p">,</span> <span class="n">strlen</span> <span class="p">(</span><span class="n">nonce</span><span class="p">),</span></span>
<span id="LC27" class="line" lang="c">                                  <span class="n" style="margin-top: 0;">auth</span><span class="p">,</span> <span class="n">strlen</span> <span class="p">(</span><span class="n">auth</span><span class="p">),</span></span>
<span id="LC28" class="line" lang="c">                                  <span class="n" style="margin-top: 0;">tag_size</span><span class="p">,</span></span>
<span id="LC29" class="line" lang="c">                                  <span class="n" style="margin-top: 0;">data</span><span class="p">,</span> <span class="n">strlen</span> <span class="p">(</span><span class="n">data</span><span class="p">),</span></span>
<span id="LC30" class="line" lang="c">                                  <span class="n" style="margin-top: 0;">output</span><span class="p">,</span> <span class="o">&</span><span class="n">used_size</span><span class="p">))</span></span>
<span id="LC31" class="line" lang="c">    <span class="p" style="margin-top: 0;">{</span></span>
<span id="LC32" class="line" lang="c">      <span class="k" style="margin-top: 0;">return</span> <span class="n">EXIT_FAILURE</span><span class="p">;</span></span>
<span id="LC33" class="line" lang="c">    <span class="p" style="margin-top: 0;">}</span></span>
<span id="LC34" class="line" lang="c">  <span class="k" style="margin-top: 0;">if</span> <span class="p">(</span><span class="n">used_size</span> <span class="o">!=</span> <span class="mi">34</span><span class="p">)</span></span>
<span id="LC35" class="line" lang="c">    <span class="p" style="margin-top: 0;">{</span></span>
<span id="LC36" class="line" lang="c">      <span class="k" style="margin-top: 0;">return</span> <span class="n">EXIT_FAILURE</span><span class="p">;</span></span>
<span id="LC37" class="line" lang="c">    <span class="p" style="margin-top: 0;">}</span></span>
<span id="LC38" class="line" lang="c">  <span class="n" style="margin-top: 0;">gnutls_aead_cipher_deinit</span> <span class="p">(</span><span class="n">handle</span><span class="p">);</span></span>
<span id="LC39" class="line" lang="c">  <span class="cm" style="margin-top: 0;">/* Decryption */</span></span>
<span id="LC40" class="line" lang="c">  <span class="kt" style="margin-top: 0;">size_t</span> <span class="n">decryption_used_size</span> <span class="o">=</span> <span class="n">strlen</span> <span class="p">(</span><span class="n">data</span><span class="p">);</span></span>
<span id="LC41" class="line" lang="c">  <span class="kt" style="margin-top: 0;">char</span> <span class="n">decrypted_output</span><span class="p">[</span><span class="n">decryption_used_size</span><span class="p">];</span></span>
<span id="LC42" class="line" lang="c">  <span class="k" style="margin-top: 0;">if</span> <span class="p">(</span><span class="n">gnutls_aead_cipher_init</span> <span class="p">(</span><span class="o">&</span><span class="n">handle</span><span class="p">,</span> <span class="n">algo</span><span class="p">,</span> <span class="o">&</span><span class="n">datum_key</span><span class="p">))</span></span>
<span id="LC43" class="line" lang="c">    <span class="p" style="margin-top: 0;">{</span></span>
<span id="LC44" class="line" lang="c">      <span class="k" style="margin-top: 0;">return</span> <span class="n">EXIT_FAILURE</span><span class="p">;</span></span>
<span id="LC45" class="line" lang="c">    <span class="p" style="margin-top: 0;">}</span></span>
<span id="LC46" class="line" lang="c">  <span class="k" style="margin-top: 0;">if</span> <span class="p">(</span><span class="n">gnutls_aead_cipher_decrypt</span> <span class="p">(</span><span class="n">handle</span><span class="p">,</span></span>
<span id="LC47" class="line" lang="c">                                  <span class="n" style="margin-top: 0;">nonce</span><span class="p">,</span> <span class="n">strlen</span> <span class="p">(</span><span class="n">nonce</span><span class="p">),</span></span>
<span id="LC48" class="line" lang="c">                                  <span class="n" style="margin-top: 0;">auth</span><span class="p">,</span> <span class="n">strlen</span> <span class="p">(</span><span class="n">auth</span><span class="p">),</span></span>
<span id="LC49" class="line" lang="c">                                  <span class="n" style="margin-top: 0;">tag_size</span><span class="p">,</span></span>
<span id="LC50" class="line" lang="c">                                  <span class="n" style="margin-top: 0;">output</span><span class="p">,</span> <span class="n">used_size</span><span class="p">,</span></span>
<span id="LC51" class="line" lang="c">                                  <span class="n" style="margin-top: 0;">decrypted_output</span><span class="p">,</span> <span class="o">&</span><span class="n">decryption_used_size</span><span class="p">))</span></span>
<span id="LC52" class="line" lang="c">    <span class="p" style="margin-top: 0;">{</span></span>
<span id="LC53" class="line" lang="c">      <span class="n" style="margin-top: 0;">fprintf</span> <span class="p">(</span><span class="n">stderr</span><span class="p">,</span> <span class="s">"Gnutls cannot decrypt, even if we have the exact same nonce.</span><span class="se">\n</span><span class="s">"</span><span class="p">);</span></span>
<span id="LC54" class="line" lang="c">      <span class="k" style="margin-top: 0;">return</span> <span class="n">EXIT_FAILURE</span><span class="p">;</span></span>
<span id="LC55" class="line" lang="c">    <span class="p" style="margin-top: 0;">}</span></span>
<span id="LC56" class="line" lang="c">  <span class="n" style="margin-top: 0;">fprintf</span> <span class="p">(</span><span class="n">stderr</span><span class="p">,</span> <span class="s">"Gnutls can decrypt if we have the same nonce.</span><span class="se">\n</span><span class="s">"</span><span class="p">);</span></span>
<span id="LC57" class="line" lang="c">  <span class="k" style="margin-top: 0;">return</span> <span class="n">EXIT_SUCCESS</span><span class="p">;</span></span>
<span id="LC58" class="line" lang="c"><span class="p" style="margin-top: 0;">}</span></span></code></pre>
<copy-code></copy-code>
</div>
</div>


</div>
<div class="footer" style="margin-top: 10px;">
<p style="font-size: small; color: #737278;">

<br>
Reply to this email directly or <a href="https://gitlab.com/gnutls/gnutls/-/issues/1494#note_1475467677">view it on GitLab</a>.
<br>
You're receiving this email because of your account on <a target="_blank" rel="noopener noreferrer" href="https://gitlab.com">gitlab.com</a>. <a href="https://gitlab.com/-/sent_notifications/c7e2105c36355803a61fd704c777ff12/unsubscribe" target="_blank" rel="noopener noreferrer">Unsubscribe</a> from this thread · <a href="https://gitlab.com/-/profile/notifications" target="_blank" rel="noopener noreferrer" class="mng-notif-link">Manage all notifications</a> · <a href="https://gitlab.com/help" target="_blank" rel="noopener noreferrer" class="help-link">Help</a>
<script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","action":{"@type":"ViewAction","name":"View Issue","url":"https://gitlab.com/gnutls/gnutls/-/issues/1494#note_1475467677"}}</script>


</p>
</div>
</body>
</html>