commit4b8626e92171f969fcdb4453a819211da2f66bfeparent2c92e04ef06e2f88d3ad988748c691e042128475Author:oscarbenedito <oscar@obenedito.org>Date:Sun, 23 Feb 2020 23:39:28 +0100 New entry: sharing a secret and MathJax addedDiffstat:

A | .gitmodules | | | 3 | +++ |

A | content/blog/2020-02-23-sharing-a-secret.pdc | | | 42 | ++++++++++++++++++++++++++++++++++++++++++ |

M | content/jsweblabels.html | | | 5 | +++++ |

M | layouts/partials/scripts.html | | | 5 | ++++- |

A | static/js/mathjax | | | 1 | + |

5 files changed, 55 insertions(+), 1 deletion(-)diff --git a/.gitmodules b/.gitmodules@@ -0,0 +1,3 @@ +[submodule "mathjax"] + path = static/js/mathjax + url = https://git.oscarbenedito.com/mathjax-bindiff --git a/content/blog/2020-02-23-sharing-a-secret.pdc b/content/blog/2020-02-23-sharing-a-secret.pdc@@ -0,0 +1,42 @@ +--- +title: "Sharing a secret" +categories: technology +tags: ["Backup", "Cryptography", "Encryption", "Privacy", "Security"] +--- +Making a backup of a secret can be tricky. For instance: I have a lot of passwords stored in an encrypted file, which I can edit through my password manager. The data in that file is both very sensitive and crucial. I currently have some offline backups (which are updated every once in a while) in different locations and one online backup in case I lose access to my passwords and I am not able to go to one of the locations where other backups are kept. + +The problem with having an online backup is that such sensitive data must be kept away from untrusted third parties and, so far, there's no third party I would trust with all those passwords. My solution is to distribute the trust. The encrypted file is encrypted again multiple times with very long random passwords. Those passwords are distributed across different services, and the file in another one, so no one service has access to the encrypted file. + +This seems like a reasonably secure system (considering the diversity of parties that should agree to attack me/get hacked). However, a couple of days ago, I was introduced to a much simpler and convenient method to "distribute" a secret: [Shamir's Secret Sharing](https://en.wikipedia.org/wiki/Shamir%27s_Secret_Sharing). + +## Shamir's Secret Sharing + +Shamir's Secret Sharing was created by [Adi Shamir](https://en.wikipedia.org/wiki/Adi_Shamir), a cryptographer who is also the co-inventor of the—probably more widely known—[RSA algorithm](https://en.wikipedia.org/wiki/RSA_(cryptosystem)) (yes, that S stands for Shamir!). Here, I'll try to briefly explain how it works. + +Given a secret $S$ (coded as a number), we want to distribute it among $n$ parties (giving each party a "share" of the secret) in such a way that only $k \leq n$ shares are needed to retrieve the secret, but that $k-1$ shares don't grant any kind of knowledge on $S$. + +Shamir's method is based on the fact that given $n + 1$ pairs $(x_i, y_i)$ such that $i \neq j \implies x_i \neq x_j$, then there exists a unique polynomial $p$ of degree $n$ or less that satisfies that $p(x_i) = y_i$, $\forall i \in \{1, \dots, n\}$ (and we have an efficient method to find $p$ given $n$ points). + +Let's put it into practice. Given a secret $S$, to be shared with $n$ parties in a way that any $k$ parties can retrieve it, we'll build the following polynomial: + +$$p(x) = S + a_1 x + a_2 x^2 + ... + a_{k-1} x^{k-1},$$ + +where $a_i$ are random numbers, $\forall i \in \{1, \dots, k-1\}$. Now we'll evaluate our polynomial on $n$ different points (and different from 0) to obtain $n$ pairs of the form $(x_i, p(x_i))$. This will be the shares of the secret. Each party will get one share. We know that $k$ shares define a unique polynomial $p$ of degree $k-1$, (if we find it, we'll find $S$). On the other hand, there are an infinite amount of polynomials of degree $k-1$ that interpolate $k-1$ points, so the secret cannot be easily obtained by gaining access to $k-1$ shares[^integers]. + +[^integers]: This is not completely true when working with positive integers, but it can be solved by working with finite fields. + +If we want to recover the secret from $k$ shares, we can interpolate the $k$ points $(x_i, p(x_i))$ using [Lagrange's form for the interpolation polynomial](https://en.wikipedia.org/wiki/Lagrange_polynomial)[^proof]: + +[^proof]: Let's quickly proof that the $p$ defined in Lagrange's form ($\bar{p}$ from now on) is the same as the previously defined $p$. $\bar{p}$ is clearly a polynomial of degree (at most) $k-1$, since it is the sum of polynomials of degree $k-1$, so we only need to proof that it interpolates the points given (we'll asume that the fact that there is only one polynomial of degree at most $k-1$ that interpolates $k$ points is true). That is easy to proof since $i \neq j \implies l_i(x_j) = 0$ and $l_i(x_i) = 1$, therefore having $\bar{p}(x_i) = p(x_i) l_i(x_i) = p(x_i)$. + +$$p(x) = \sum_{i=1}^{k} p(x_i) l_i(x),$$ + +where + +$$l_i(x) = \prod_{\begin{smallmatrix}1\leq m\leq k\\ m\neq i\end{smallmatrix}} \frac{x-x_m}{x_i-x_m}.$$ + +Now, evaluating on $x = 0$ we'll find the secret (because $p(0) = S$). + +## Final notes + +Now we have a method to share our secret between multiple parties and being able to retrieve it with only some of them. This method is not too hard to code yourself, however, there are implementations online if you would rather not do that (make sure you are running trusted code!).diff --git a/content/jsweblabels.html b/content/jsweblabels.html@@ -26,6 +26,11 @@ type: page <td><a href="http://www.gnu.org/licenses/agpl-3.0.html">GNU-AGPL-3.0-or-later</a></td> <td><a href="https://gitlab.com/oscarbenedito/obenedito.org/raw/master/assets/js/composer.js">composer.js</a></td> </tr> + <tr> + <td><a href="/mathjax/tex-chtml.js">tex-chtml.js</a></td> + <td><a href="http://www.apache.org/licenses/LICENSE-2.0">Apache-2.0-only</a></td> + <td><a href="https://github.com/mathjax/MathJax-src/">MathJax source code</a></td> + </tr> </tbody> </table> </div>diff --git a/layouts/partials/scripts.html b/layouts/partials/scripts.html@@ -1,2 +1,5 @@ -{{ $js := resources.Get "js/main.js" | resources.Minify }} +{{ $js := resources.Get "js/main.js" | resources.Minify -}} <script type="text/javascript" src="{{ $js.RelPermalink }}"></script> +{{- if eq .File.Ext "pdc" }} +<script id="MathJax-script" async src="/js/mathjax/tex-chtml.js"></script> +{{- end }}diff --git a/static/js/mathjax b/static/js/mathjax@@ -0,0 +1 @@ +Subproject commit cc6681f8c80d1af9bf3d258cc760e7dc31892e9b