chiark / gitweb /
extract-profile: Add manpage.
[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$.
139.el \{\
1401
141\(<=
142.I thresh
143\(<=
144.I count
145\(<=
146255.
147.\}
148.PP
149The first line contains secret-sharing parameters,
150which will be required in any recovery operation.
151These do not usually need to be kept secret
152(though they contain a cryptographic hash of the original input)
153but it's a good idea to ensure their integrity.
154This can be done by storing the parameters in a safe place,
155possibly protected by a digital signature,
156or by issuing each share holder with a copy of the parameters
157and checking that they match at recovery time.
158.PP
159The remaining lines contain share data, one per line, for
160.I count
161lines.
162Each share holder should be given one of these lines.
163.PP
164The following options are accepted.
165.TP
166.BI "\-H, \-\-hash-function=" hashfn
167The hash function to use when hashing the secret.
168This may be any hash function name known to OpenSSL.
169.PP
170Example:
171.VS
172\*(pr shamir issue 3/5 secret
173.ft R
174shamir-params:n=5;t=3;f=sha256;h=lbcQW/lmV4z0ZKORE5Y0/g+thgWFKr4Kv3i1vEfNkUQ=
175shamir-share:i=0;y=Ft3jhDQgTdMgEJ3Og3+OZ98NTSRGEWFjIN6mAQmzvHc=
176shamir-share:i=1;y=zHpbi0h+mFVVv0vAo0djMK296b5XRCAYRV6yMsy74Rs=
177shamir-share:i=2;y=FbSHVSqoXLVbxkuVZweLXacXmqAnWMEgLOP3qlSvfPc=
178shamir-share:i=3;y=Q0bUOCUfgSlZSFPOSwhr2YjCSfXKRjwRDmwK8PZPB48=
179shamir-share:i=4;y=mogI5kfJRclXMVObj0iDtIJoOuu6Wt0pZ9FPaG5bmmM=
180.VE
181.SS recover
182The
183.B recover
184command reads parameters and shares from standard input,
185computes the original secret,
186and writes it to standard output.
187.PP
188There are no command-line options.
189.PP
190The first line of standard input must contain the sharing parameters.
191Subsequent lines contain shares,
192one per line.
193The shares may be in any order.
194Exactly the correct
195.RI ( thresh )
196number of shares must be provided.
197The shares are checked for various kinds of errors
198(e.g., duplicate shares, out-of-range indices),
199and the secret is computed
200and checked against the hash stored with the parameters.
201If everything is correct,
202the secret is written to standard output;
203otherwise errors are reported to standard error,
204and the program exits with a nonzero status
205having written nothing to standard output.
206.PP
207Example:
208.VS
209\*(pr cat shares
210.ft R
211shamir-params:n=5;t=3;f=sha256;h=lbcQW/lmV4z0ZKORE5Y0/g+thgWFKr4Kv3i1vEfNkUQ=
212shamir-share:i=2;y=FbSHVSqoXLVbxkuVZweLXacXmqAnWMEgLOP3qlSvfPc=
213shamir-share:i=1;y=zHpbi0h+mFVVv0vAo0djMK296b5XRCAYRV6yMsy74Rs=
214shamir-share:i=4;y=mogI5kfJRclXMVObj0iDtIJoOuu6Wt0pZ9FPaG5bmmM=
215.ft B
216\*(pr shamir recover <shares >secret2
217\*(pr cmp secret secret2
218.VE
219.SH DATA FORMATS
220The
221.B shamir
222program applies a simple encoding
223to the various kinds of structured data it deals with,
224specifically to
225.IR secrets ,
226.IR parameters ,
227and
228.IR shares .
229.PP
230An encoded object has the form
231.IP
232.IB label : \c
233.IB slot = value \c
234.RB [ ; ...]
235.PP
236Encodings do not contain whitespace characters.
237.PP
238The
239.I label
240is a token which identifies the kind of object encoded.
241Each
242.I slot
243is named with a token
244(usually a single letter),
245and supplied with its
246.I value
247in a suitable encoding,
248as described below.
249.PP
250The syntax is very picky.
251The order of the slots is significant,
252and all slots must be defined.
253.SS Secets
254A
255.I secret
256object describes a secret and its sharing parameters.
257Secret objects are not input or output directly,
258but are hashed when constructing parameters objects (described below).
259.PP
260The label for a secret object is
261.BR shamir-secret .
262Its slots are as follows.
263.TP
264.B n
265The total number of shares issued,
266i.e., the
267.I count
268argument to
269.BR "shamir issue" ,
270as a decimal integer.
271.TP
272.B t
273The threshold required for recovery,
274i.e., the
275.I thresh
276argument to
277.BR "shamir issue" ,
278as a decimal integer.
279.TP
280.B s
281The secret itself, Base64-encoded,
282with no whitespace.
283.SS Parameters
284A
285.I parameters
286object describes how a secret has been split into shares
287and provides information about how it can be reassembled.
288.PP
289The label for a parameters object is
290.BR shamir-params .
291Its slots are as follows.
292.TP
293.B n
294The total number of shares issued,
295i.e., the
296.I count
297argument to
298.BR "shamir issue" ,
299as a decimal integer.
300.TP
301.B t
302The threshold required for recovery,
303i.e., the
304.I thresh
305argument to
306.BR "shamir issue" ,
307as a decimal integer.
308.TP
309.B f
310The name of the hash function
311used to compute the
312.B h
313slot.
314This may be any of the hash functions known to OpenSSL.
315.TP
316.B h
317The hash of a secret object,
318made using the hash function named by the
319.B f
320slot,
321and Base64 encoded.
322No trailing newline is included when hashing the secret object.
323.SS Shares
324A
325.I share
326object describes a single share.
327The label for a parameters object is
328.BR shamir-share .
329Its slots are as follows.
330.TP
331.B i
332The share's index,
333as a decimal integer;
334.ie t $0 <= i < "count"$.
335.el \{\
3360 \(<=
337.I i
338<
339.IR count .
340.\}
341.TP
342.B y
343The share data, Base64 encoded.
344.PP
345.SH MATHEMATICAL BACKGROUND
346(This section has lots of mathematical notation
347and doesn't look especially good in a terminal.)
348.PP
349Let
350.I k
351be a field.
352Suppose we are given a secret
353.ie t $s member k$
354.el \{\
355.I s
356\*(mo
357.I k
358.\}
359which we want to split into shares
360such that any $t$ of them can be used to recover
361.IR s .
362Choose coefficients
363.ie t $a sub i member k$
364.el \{\
365.IR a _ i
366\*(mo
367.I k
368.\}
369for
370.ie t $1 <= i < t$
371.el \{\
3721 \(<=
373.I i
374<
375.I t
376.\}
377at random.
378Let
379.ie t $p(x) = s + a sub 1 x + ... + a sub {t-1} x sup {t-1}$.
380.el \{\
381.IR p ( x )
382=
383.I s
384+
385.IR a _1
386.I x
387+ ... +
388.IR a _( t \-1)
389.IR x ^( t \-1).
390.\}
391Note that
392.ie t $p(0) = s$.
393.el \{\
394.IR p (0)
395=
396.IR s .
397.\}
398We can evaluate
399.I p
400to obtain shares
401.ie t $y sub i = p( x sub i )$
402.el \{\
403.IR y _ i
404=
405.IR p ( x _ i )
406.\}
407for various
408.ie t $x sub i member k - lbrace ~ 0 ~ rbrace$.
409.el \{\
410.IR x _ i
411\*(mo
412.I k
413\-
414{ 0 }.
415.\}
416.PP
417How do we recover the secret?
418Suppose we are given
419.ie t $y sub i = p( x sub i )$
420.el \{\
421.IR y _ i
422=
423.IR p ( x _ i )
424.\}
425for
426.ie t $0 <= i < t$,
427.el \{\
4280 \(<=
429.I i
430<
431.IR t .
432.\}
433where the
434.ie t $x sub i$
435.el .IR x _ i
436are distinct.
437Define
438.ie t \{\
439.EQ
440lambda sub i (x) =
441 sum from {pile {0 <= j < t above j != i}}
442 {x - x sub j} over {x sub i - x sub j}.
443.EN
444.\}
445.el \{\
446.IP
447.nf
448.\" --- x - x_j
449.\" L_i(x) = > ---------.
450.\" --- x_i - x_j
451.\" 0<j<t
452.\" j=i
453 --- \fIx\fP \- \fIx\fP_\fIj\fP
454\*(*l_\fIi\fP(\fIx\fP) = > ---------.
455 --- \fIx\fP_\fIi\fP - \fIx\fP_\fIj\fP
456 0\(<=\fIj\fP<\fIt\fP
457 \fIj\fP\(!=\fIi\fP
458.fi
459.PP
460.\}
461Then
462.ie t $lambda sub i$
463.el .RI \*(*l_ i
464is a
465.ie t degree-$(t - 1)$
466.el \{\
467.RI degree-( t
468\- 1)
469.\}
470polynomial
471such that
472.ie t $lambda sub i ( x sub i ) = 1$,
473.el \{\
474.RI \*(*l_ i ( x _ i )
475= 1,
476.\}
477and
478.ie t $lambda sub i ( x sub j ) = 0$
479.el \{\
480.RI \*(*l_ i ( x _ j )
481= 0
482.\}
483if
484.ie t $j != i$.
485.el \{\
486.I j
487\(!=
488.IR i .
489.\}
490Define
491.ie t \{\
492.EQ
493q(x) = sum from {0 <= i < t} lambda sub i (x) y sub i .
494.EN
495.\}
496.el \{\
497.IP
498.nf
499.\" ---
500.\" q(x) = > L_i(x) y_i.
501.\" ---
502.\" 0<i<t
503 ---
504\fIq\fP(\fIx\fP) = > \*(*l_\fIi\fP(\fIx\fP) \fIy\fP_\fIi\fP.
505 ---
506 0\(<=\fIi\fP<\fIt\fP
507.fi
508.PP
509.\}
510Note that
511.I q
512has degree
513.ie t $t - 1$,
514.el \{\
515.I t
516\- 1,
517.\}
518and
519.ie t $q( x sub i ) = y sub i = p( x sub i )$
520.el \{\
521.IR q ( x _ i )
522=
523.IR y _ i
524=
525.IR p ( x _ i )
526.\}
527for all
528.ie t $0 <= i < t$.
529.el \{\
5300 \(<=
531.I i
532<
533.IR t .
534.\}
535Hence
536.ie t $p - q$
537.el \{\
538.I p
539\-
540.I q
541.\}
542is a polynomial with degree at most
543.ie t $t - 1$
544.el \{\
545.I t
546\- 1
547.\}
548but with at least
549.I t
550distinct zeros;
551therefore
552.ie t $q - p$
553.el \{\
554.I p
555\-
556.I q
557.\}
558must be identically zero
559and hence
560.ie t $q == p$
561.el \{\
562.I q
563\(==
564.I p
565.\}
566and
567.ie t $s = q(0)$.
568.el \{\
569.I s
570=
571.IR q (0).
572.\}
573.PP
574Suppose we are given
575.ie t $t - 1$
576.el \{\
577.I t
578\- 1
579.\}
580shares.
581Then,
582for any putative secret
583.ie t $s'$,
584.el .IR s ',
585there exists a distinct possible value for each of the remaining shares
586which can be determined using the above interpolation,
587each of which is equally likely.
588.PP
589The polynomial interpolation technique is due to Lagrange;
590its use as a secret-sharing scheme is due to Adi Shamir.
591.PP
592In theory, then,
593this secret-sharing scheme is
594.IR "information-theoretically secure" ;
595in practice,
596there's a cryptographic hash of the secret in the sharing parameters,
597so security is limited to the computational difficulty
598of inverting this hash.
599Knowledge of fewer than
600.I t
601shares doesn't make this any easier;
602and there's a security benefit to
603knowing that a dishonest share holder
604can't introduce errors into the recovered secret.
605.PP
606All of the above works in any field
607.IR k .
608Computation is easiest in a small finite field
609(rather than, say, the rationals).
610This program uses the field
611.ie t $FIELD sub {2 sup 8}$
612.el GF(2^8)
613represented as the quotient ring
614.ie t $FIELD sub 2 [u]/( u sup 8 + u sup 4 + u sup 3 + u sup 2 + 1 )$.
615.el \{\
616.RI GF(2)[ u ]/( u ^8
617+
618.IR u ^4
619+
620.IR u ^3
621+
622.IR u ^2
623+
6241).
625.\}
626Hence, a field element fits exactly into a single byte:
627specifically, the field element
628.ie t $x = a sub 0 + a sub 1 u + ... + a sub 7 u sup 7$
629.el \{\
630.I x
631=
632.IR a _0
633+
634.IR a _1
635.I u
636+ ... +
637.IR a _7
638.IR u ^7
639.\}
640corresponds to the byte
641.ie t $B(x) = a sub 0 + 2 a sub 1 + ... + 2 sup 7 a sub 7$.
642.el \{\
643.IR B ( x )
644=
645.IR a _0
646+
6472
648.IR a _1
649+ ... +
6502^7
651.IR a _7.
652.\}
653Secrets longer than a single byte are shared bytewise independently,
654which is why the shares leak information about the secret size.
655.PP
656The program represents share indices as small integers
657.ie t $0 <= i < n$;
658.el \{\
6590 \(<=
660.I i
661<
662.IR n ;
663.\}
664specifically, the share with index
665.I i
666is generated by
667evaluating the polynomial
668.ie t $p(x)$
669.el .IR p ( x )
670where
671.ie t $i = B(x) - 1$.
672.el \{\
673.I i
674=
675.IR B ( x )
676\- 1.
677.\}
678.SH AUTHOR
679Mark Wooding, <mdw@distorted.org.uk>
680.SH SEE ALSO
681.BR distorted-keys (7),
682.BR dgst (1ssl).