chiark / gitweb /
keys.delete-keeper: Only do one pass through the file system.
[distorted-keys] / shamir.1
CommitLineData
3f98ee81
MW
1'\" e
2.\" -*-nroff-*-
3.ie t \{\
4. ds o \(bu
5. if \n(.g .fam P
6.\}
7.el \{\
8. ds o o
9. ie '\*(.T'utf8' .ds mo ∈
10. el .ds mo \fBin\fP
11. ie '\*(.T'utf8' .ds *l \(*l
12. el .ds *l L
13.\}
14.de hP
15.IP
16\h'-\w'\fB\\$1\ \fP'u'\fB\\$1\ \fP\c
17..
18.de VS
19.sp 1
20.RS
21.nf
22.ft B
23..
24.de VE
25.ft R
26.fi
27.RE
28.sp 1
29..
30.ds pr $
31.EQ
32delim $$
33tdefine member 'type "relation" \(mo'
34define lbrace 'roman "{"'
35define rbrace 'roman "}"'
36define FIELD 'bold F'
37.EN
38.TH shamir 1 "3 May 2015" "distorted.org.uk key management"
39.de EQ
40.PP
41.ce
42..
43.de EN
44.PP
45..
46.SH NAME
47shamir \- split a secret into shares, and recombine them
48.SH SYNOPSIS
49.B shamir
50.I command
51.PP
52where
53.I command
54is one of:
55.PP
56.B help
57.IB [ command\fR... ]
58.br
59.B issue
60.RB [ \-H
61.IR hashfn ]
62.IB thresh / count
63.RI [ secret-file ]
64.br
65.B recover
66.SH DESCRIPTION
67The
68.B shamir
69program implements a simple secret sharing scheme.
70Given a secret,
71the
72.B issue
73command will split it
74into a number of shares
75.IR n,
76any
77.ie t $t <= n$
78.el \{\
79.I t
80\(<=
81.I n
82.\}
83of which
84can be used to reconstruct it, but
85fewer than
86.I t
87shares provide no information at all
88about the secret
89(other than its length).
90.SH COMMAND-LINE OPTIONS
91The following options are recognized.
92.TP
93.B \-h, \-\-help
94Print an overview
95of the options and commands
96to standard output
97and exit successfully.
98.TP
99.B \-\-version
100Print the version number
101of the
102.B distorted-keys
103package
104to standard output
105and exit successfully.
106.SH COMMAND REFERENCE
107.SS help
108The
109.B help
110command prints information to standard output
111about the
112.B shamir
113program as a whole
114(similar to the
115.B \-\-help
116option),
117or about the named
118.IR command s.
119.SS issue
120The
121.B issue
122command reads a secret from
123.I secret-file
124(or from standard input,
125if
126.I secret-file
127is omitted or
128.RB ` \- '),
129and writes a number of lines to standard output.
130.PP
131The
132.I count
133is the total number of shares issued;
134any
135.I thresh
136of them can be used to reassemble the original secret.
137It must be the case that
138.ie t $1 <= "thresh" <= "count" <= 255$.
abcc04d6 139.el .RI "1\~\(<= " thresh "\~\(<= " count "\~\(<= 255."
3f98ee81
MW
140.PP
141The first line contains secret-sharing parameters,
142which will be required in any recovery operation.
143These do not usually need to be kept secret
144(though they contain a cryptographic hash of the original input)
145but it's a good idea to ensure their integrity.
146This can be done by storing the parameters in a safe place,
147possibly protected by a digital signature,
148or by issuing each share holder with a copy of the parameters
149and checking that they match at recovery time.
150.PP
151The remaining lines contain share data, one per line, for
152.I count
153lines.
154Each share holder should be given one of these lines.
155.PP
156The following options are accepted.
157.TP
158.BI "\-H, \-\-hash-function=" hashfn
159The hash function to use when hashing the secret.
160This may be any hash function name known to OpenSSL.
161.PP
162Example:
163.VS
164\*(pr shamir issue 3/5 secret
165.ft R
166shamir-params:n=5;t=3;f=sha256;h=lbcQW/lmV4z0ZKORE5Y0/g+thgWFKr4Kv3i1vEfNkUQ=
167shamir-share:i=0;y=Ft3jhDQgTdMgEJ3Og3+OZ98NTSRGEWFjIN6mAQmzvHc=
168shamir-share:i=1;y=zHpbi0h+mFVVv0vAo0djMK296b5XRCAYRV6yMsy74Rs=
169shamir-share:i=2;y=FbSHVSqoXLVbxkuVZweLXacXmqAnWMEgLOP3qlSvfPc=
170shamir-share:i=3;y=Q0bUOCUfgSlZSFPOSwhr2YjCSfXKRjwRDmwK8PZPB48=
171shamir-share:i=4;y=mogI5kfJRclXMVObj0iDtIJoOuu6Wt0pZ9FPaG5bmmM=
172.VE
173.SS recover
174The
175.B recover
176command reads parameters and shares from standard input,
177computes the original secret,
178and writes it to standard output.
179.PP
180There are no command-line options.
181.PP
182The first line of standard input must contain the sharing parameters.
183Subsequent lines contain shares,
184one per line.
185The shares may be in any order.
186Exactly the correct
187.RI ( thresh )
188number of shares must be provided.
189The shares are checked for various kinds of errors
190(e.g., duplicate shares, out-of-range indices),
191and the secret is computed
192and checked against the hash stored with the parameters.
193If everything is correct,
194the secret is written to standard output;
195otherwise errors are reported to standard error,
196and the program exits with a nonzero status
197having written nothing to standard output.
198.PP
199Example:
200.VS
201\*(pr cat shares
202.ft R
203shamir-params:n=5;t=3;f=sha256;h=lbcQW/lmV4z0ZKORE5Y0/g+thgWFKr4Kv3i1vEfNkUQ=
204shamir-share:i=2;y=FbSHVSqoXLVbxkuVZweLXacXmqAnWMEgLOP3qlSvfPc=
205shamir-share:i=1;y=zHpbi0h+mFVVv0vAo0djMK296b5XRCAYRV6yMsy74Rs=
206shamir-share:i=4;y=mogI5kfJRclXMVObj0iDtIJoOuu6Wt0pZ9FPaG5bmmM=
207.ft B
208\*(pr shamir recover <shares >secret2
209\*(pr cmp secret secret2
210.VE
211.SH DATA FORMATS
212The
213.B shamir
214program applies a simple encoding
215to the various kinds of structured data it deals with,
216specifically to
217.IR secrets ,
218.IR parameters ,
219and
220.IR shares .
221.PP
222An encoded object has the form
223.IP
224.IB label : \c
225.IB slot = value \c
226.RB [ ; ...]
227.PP
228Encodings do not contain whitespace characters.
229.PP
230The
231.I label
232is a token which identifies the kind of object encoded.
233Each
234.I slot
235is named with a token
236(usually a single letter),
237and supplied with its
238.I value
239in a suitable encoding,
240as described below.
241.PP
242The syntax is very picky.
243The order of the slots is significant,
244and all slots must be defined.
245.SS Secets
246A
247.I secret
248object describes a secret and its sharing parameters.
249Secret objects are not input or output directly,
250but are hashed when constructing parameters objects (described below).
251.PP
252The label for a secret object is
253.BR shamir-secret .
254Its slots are as follows.
255.TP
256.B n
257The total number of shares issued,
258i.e., the
259.I count
260argument to
261.BR "shamir issue" ,
262as a decimal integer.
263.TP
264.B t
265The threshold required for recovery,
266i.e., the
267.I thresh
268argument to
269.BR "shamir issue" ,
270as a decimal integer.
271.TP
272.B s
273The secret itself, Base64-encoded,
274with no whitespace.
275.SS Parameters
276A
277.I parameters
278object describes how a secret has been split into shares
279and provides information about how it can be reassembled.
280.PP
281The label for a parameters object is
282.BR shamir-params .
283Its slots are as follows.
284.TP
285.B n
286The total number of shares issued,
287i.e., the
288.I count
289argument to
290.BR "shamir issue" ,
291as a decimal integer.
292.TP
293.B t
294The threshold required for recovery,
295i.e., the
296.I thresh
297argument to
298.BR "shamir issue" ,
299as a decimal integer.
300.TP
301.B f
302The name of the hash function
303used to compute the
304.B h
305slot.
306This may be any of the hash functions known to OpenSSL.
307.TP
308.B h
309The hash of a secret object,
310made using the hash function named by the
311.B f
312slot,
313and Base64 encoded.
314No trailing newline is included when hashing the secret object.
315.SS Shares
316A
317.I share
318object describes a single share.
319The label for a parameters object is
320.BR shamir-share .
321Its slots are as follows.
322.TP
323.B i
324The share's index,
325as a decimal integer;
326.ie t $0 <= i < "count"$.
abcc04d6 327.el .RI "0\~\(<= " i "\~< " count .
3f98ee81
MW
328.TP
329.B y
330The share data, Base64 encoded.
331.PP
332.SH MATHEMATICAL BACKGROUND
333(This section has lots of mathematical notation
334and doesn't look especially good in a terminal.)
335.PP
336Let
337.I k
338be a field.
339Suppose we are given a secret
340.ie t $s member k$
abcc04d6 341.el .IR s \~\*(mo\~ k
3f98ee81
MW
342which we want to split into shares
343such that any $t$ of them can be used to recover
344.IR s .
345Choose coefficients
346.ie t $a sub i member k$
abcc04d6 347.el .IR a _ i \~\*(mo\~ k
3f98ee81
MW
348for
349.ie t $1 <= i < t$
abcc04d6 350.el .RI "1\~\(<= " i \~<\~ t
3f98ee81
MW
351at random.
352Let
353.ie t $p(x) = s + a sub 1 x + ... + a sub {t-1} x sup {t-1}$.
354.el \{\
abcc04d6
MW
355.IR p ( x ")\~= " s "\~+ " a "_1\~+ ...\~+"
356.IR a _( t \-1)\~ x ^( t \-1).
3f98ee81
MW
357.\}
358Note that
359.ie t $p(0) = s$.
abcc04d6 360.el .IR p "(0)\~= " s .
3f98ee81
MW
361We can evaluate
362.I p
363to obtain shares
364.ie t $y sub i = p( x sub i )$
abcc04d6 365.el .IR y _ i "\~= " p ( x _ i )
3f98ee81
MW
366for various
367.ie t $x sub i member k - lbrace ~ 0 ~ rbrace$.
abcc04d6 368.el .IR x _ i "\~\*(mo " k "\~\- {\~0\~}."
3f98ee81
MW
369.PP
370How do we recover the secret?
371Suppose we are given
372.ie t $y sub i = p( x sub i )$
abcc04d6 373.el .IR y _ i "\~= " p ( x _ i )
3f98ee81
MW
374for
375.ie t $0 <= i < t$,
abcc04d6 376.el .RI "0\~\(<= " i "\~< " t ,
3f98ee81
MW
377where the
378.ie t $x sub i$
379.el .IR x _ i
380are distinct.
381Define
382.ie t \{\
383.EQ
384lambda sub i (x) =
385 sum from {pile {0 <= j < t above j != i}}
386 {x - x sub j} over {x sub i - x sub j}.
387.EN
388.\}
389.el \{\
390.IP
391.nf
392.\" --- x - x_j
393.\" L_i(x) = > ---------.
394.\" --- x_i - x_j
395.\" 0<j<t
396.\" j=i
397 --- \fIx\fP \- \fIx\fP_\fIj\fP
398\*(*l_\fIi\fP(\fIx\fP) = > ---------.
399 --- \fIx\fP_\fIi\fP - \fIx\fP_\fIj\fP
400 0\(<=\fIj\fP<\fIt\fP
401 \fIj\fP\(!=\fIi\fP
402.fi
403.PP
404.\}
405Then
406.ie t $lambda sub i$
407.el .RI \*(*l_ i
408is a
409.ie t degree-$(t - 1)$
abcc04d6 410.el .RI degree-( t \~\-\~1)
3f98ee81
MW
411polynomial
412such that
413.ie t $lambda sub i ( x sub i ) = 1$,
abcc04d6 414.el .RI \*(*l_ i ( x _ i )\~=\~1,
3f98ee81
MW
415and
416.ie t $lambda sub i ( x sub j ) = 0$
abcc04d6 417.el .RI \*(*l_ i ( x _ j )\~=\~0
3f98ee81
MW
418if
419.ie t $j != i$.
abcc04d6 420.el .IR j \~\(!=\~ i .
3f98ee81
MW
421Define
422.ie t \{\
423.EQ
424q(x) = sum from {0 <= i < t} lambda sub i (x) y sub i .
425.EN
426.\}
427.el \{\
428.IP
429.nf
430.\" ---
431.\" q(x) = > L_i(x) y_i.
432.\" ---
433.\" 0<i<t
434 ---
435\fIq\fP(\fIx\fP) = > \*(*l_\fIi\fP(\fIx\fP) \fIy\fP_\fIi\fP.
436 ---
437 0\(<=\fIi\fP<\fIt\fP
438.fi
439.PP
440.\}
441Note that
442.I q
443has degree
444.ie t $t - 1$,
abcc04d6 445.el .IR t "\~\- 1"
3f98ee81
MW
446and
447.ie t $q( x sub i ) = y sub i = p( x sub i )$
abcc04d6 448.el .IR q ( x _ i ")\~= " y _ i "\~= " p ( x _ i )
3f98ee81
MW
449for all
450.ie t $0 <= i < t$.
abcc04d6 451.el .RI "0\~\(<= " i \~<\~ t .
3f98ee81
MW
452Hence
453.ie t $p - q$
abcc04d6 454.el .IR p \~\-\~ q
3f98ee81
MW
455is a polynomial with degree at most
456.ie t $t - 1$
abcc04d6 457.el .IR t \~\-\~1
3f98ee81
MW
458but with at least
459.I t
460distinct zeros;
461therefore
462.ie t $q - p$
abcc04d6 463.el .IR q \~\-\~ p
3f98ee81
MW
464must be identically zero
465and hence
466.ie t $q == p$
abcc04d6 467.el .IR q \~\(==\~ p
3f98ee81
MW
468and
469.ie t $s = q(0)$.
abcc04d6 470.el .IR s "\~= " q (0).
3f98ee81
MW
471.PP
472Suppose we are given
473.ie t $t - 1$
abcc04d6 474.el .IR t \~\-\~1
3f98ee81
MW
475shares.
476Then,
477for any putative secret
478.ie t $s'$,
479.el .IR s ',
480there exists a distinct possible value for each of the remaining shares
481which can be determined using the above interpolation,
482each of which is equally likely.
483.PP
484The polynomial interpolation technique is due to Lagrange;
485its use as a secret-sharing scheme is due to Adi Shamir.
486.PP
487In theory, then,
488this secret-sharing scheme is
489.IR "information-theoretically secure" ;
490in practice,
491there's a cryptographic hash of the secret in the sharing parameters,
492so security is limited to the computational difficulty
493of inverting this hash.
494Knowledge of fewer than
495.I t
496shares doesn't make this any easier;
497and there's a security benefit to
498knowing that a dishonest share holder
499can't introduce errors into the recovered secret.
500.PP
501All of the above works in any field
502.IR k .
503Computation is easiest in a small finite field
504(rather than, say, the rationals).
505This program uses the field
506.ie t $FIELD sub {2 sup 8}$
507.el GF(2^8)
508represented as the quotient ring
509.ie t $FIELD sub 2 [u]/( u sup 8 + u sup 4 + u sup 3 + u sup 2 + 1 )$.
510.el \{\
abcc04d6
MW
511.RI GF(2)[ u ]/( u ^8\~+
512.IR u ^4\~+
513.IR u ^3\~+
514.IR u ^2\~+\~1).
3f98ee81
MW
515.\}
516Hence, a field element fits exactly into a single byte:
517specifically, the field element
518.ie t $x = a sub 0 + a sub 1 u + ... + a sub 7 u sup 7$
519.el \{\
abcc04d6
MW
520.IR x \~=
521.IR a _0\~+
522.IR a _1\~ u \~+
523\&...\~+
524.IR a _7\~ u ^7
3f98ee81
MW
525.\}
526corresponds to the byte
527.ie t $B(x) = a sub 0 + 2 a sub 1 + ... + 2 sup 7 a sub 7$.
528.el \{\
abcc04d6
MW
529.IR B ( x )\~=
530.IR a _0\~+
531.RI 2\~ a _1\~+
532\&... +
533.RI 2^7\~ a _7.
3f98ee81
MW
534.\}
535Secrets longer than a single byte are shared bytewise independently,
536which is why the shares leak information about the secret size.
537.PP
538The program represents share indices as small integers
539.ie t $0 <= i < n$;
abcc04d6 540.el .RI "0\~\(<= " i \~<\~ n ;
3f98ee81
MW
541specifically, the share with index
542.I i
543is generated by
544evaluating the polynomial
545.ie t $p(x)$
546.el .IR p ( x )
547where
548.ie t $i = B(x) - 1$.
abcc04d6 549.el .IR i "\~= " B ( x )\~\-\~1.
3f98ee81
MW
550.SH AUTHOR
551Mark Wooding, <mdw@distorted.org.uk>
552.SH SEE ALSO
553.BR distorted-keys (7),
554.BR dgst (1ssl).