blob: 0c1396a891ba544b35a2077ef66468c4b69808b8 [file] [log] [blame]
rsccfa37a72004-04-10 18:53:55 +00001.TH RSA 3
2.SH NAME
3asn1dump,
4asn1toRSApriv,
5decodepem,
rsc058b0112005-01-03 06:40:20 +00006decodepemchain,
rsccfa37a72004-04-10 18:53:55 +00007rsadecrypt,
8rsaencrypt,
rsc058b0112005-01-03 06:40:20 +00009rsafill,,
rsccfa37a72004-04-10 18:53:55 +000010rsagen,
11rsaprivalloc,
12rsaprivfree,
13rsaprivtopub,
14rsapuballoc,
15rsapubfree,
16X509toRSApub,
rsc058b0112005-01-03 06:40:20 +000017X509dump,
rsccfa37a72004-04-10 18:53:55 +000018X509gen,
rsc058b0112005-01-03 06:40:20 +000019X509req,
rsccfa37a72004-04-10 18:53:55 +000020X509verify \- RSA encryption algorithm
21.SH SYNOPSIS
22.B #include <u.h>
23.br
24.B #include <libc.h>
25.br
26.B #include <mp.h>
27.br
28.B #include <libsec.h>
29.PP
30.B
rsc058b0112005-01-03 06:40:20 +000031.ta +\w'\fLPEMChain* 'u
rsccfa37a72004-04-10 18:53:55 +000032RSApriv* rsagen(int nlen, int elen, int nrep)
33.PP
34.B
rsc058b0112005-01-03 06:40:20 +000035RSApriv* rsafill(mpint *n, mpint *ek, mpint *dk, mpint *p, mpint *q)
36.PP
37.B
rsccfa37a72004-04-10 18:53:55 +000038mpint* rsaencrypt(RSApub *k, mpint *in, mpint *out)
39.PP
40.B
41mpint* rsadecrypt(RSApriv *k, mpint *in, mpint *out)
42.PP
43.B
44RSApub* rsapuballoc(void)
45.PP
46.B
47void rsapubfree(RSApub*)
48.PP
49.B
50RSApriv* rsaprivalloc(void)
51.PP
52.B
53void rsaprivfree(RSApriv*)
54.PP
55.B
56RSApub* rsaprivtopub(RSApriv*)
57.PP
58.B
59RSApub* X509toRSApub(uchar *cert, int ncert, char *name, int nname)
60.PP
61.B
62RSApriv* asn1toRSApriv(uchar *priv, int npriv)
63.PP
64.B
65void asn1dump(uchar *der, int len)
66.PP
67.B
68uchar* decodepem(char *s, char *type, int *len)
69.PP
70.B
rsc058b0112005-01-03 06:40:20 +000071PEMChain* decodepemchain(char *s, char *type)
72.PP
73.B
74void X509dump(uchar *cert, int ncert)
75.PP
76.B
rsccfa37a72004-04-10 18:53:55 +000077uchar* X509gen(RSApriv *priv, char *subj, ulong valid[2], int *certlen);
78.PP
79.B
80uchar* X509req(RSApriv *priv, char *subj, int *certlen);
81.PP
82.B
83char* X509verify(uchar *cert, int ncert, RSApub *pk)
84.SH DESCRIPTION
85.PP
86RSA is a public key encryption algorithm. The owner of a key publishes
87the public part of the key:
88.EX
89 struct RSApub
90 {
91 mpint *n; // modulus
92 mpint *ek; // exp (encryption key)
93 };
94.EE
95This part can be used for encrypting data (with
96.IR rsaencrypt )
97to be sent to the owner.
98The owner decrypts (with
99.IR rsadecrypt )
100using his private key:
101.EX
102 struct RSApriv
103 {
104 RSApub pub;
105 mpint *dk; // exp (decryption key)
106
107 // precomputed crt values
108 mpint *p;
109 mpint *q;
110 mpint *kp; // k mod p-1
111 mpint *kq; // k mod q-1
112 mpint *c2; // for converting residues to number
113 };
114.EE
115.PP
116Keys are generated using
117.IR rsagen .
118.I Rsagen
119takes both bit length of the modulus, the bit length of the
120public key exponent, and the number of repetitions of the Miller-Rabin
121primality test to run. If the latter is 0, it does the default number
122of rounds.
123.I Rsagen
124returns a newly allocated structure containing both
125public and private keys.
126.I Rsaprivtopub
127returns a newly allocated copy of the public key
128corresponding to the private key.
129.PP
rsc058b0112005-01-03 06:40:20 +0000130.I Rsafill
131takes as input the bare minimum pieces of an RSA private key
132and computes the rest
133.RB ( kp ,
134.BR kq ,
135and
136.BR c2 ).
137It returns a new private key.
138All the
139.BR mpint s
140in the key,
141even the ones that correspond directly to
142.IR rsafill 's
143input parameters,
144are freshly allocated,
145.PP
rsccfa37a72004-04-10 18:53:55 +0000146The routines
147.IR rsaalloc ,
148.IR rsafree ,
149.IR rsapuballoc ,
150.IR rsapubfree ,
151.IR rsaprivalloc ,
152and
153.I rsaprivfree
154are provided to aid in user provided key I/O.
155.PP
156Given a binary X.509
157.IR cert ,
158the routine
159.I X509toRSApub
160returns the public key and, if
161.I name
162is not nil, the CN part of the Distinguished Name of the
163certificate's Subject.
164(This is conventionally a userid or a host DNS name.)
165No verification is done of the certificate signature; the
166caller should check the fingerprint,
167.IR sha1(cert) ,
168against a table or check the certificate by other means.
169X.509 certificates are often stored in PEM format; use
170.I dec64
171to convert to binary before computing the fingerprint or calling
172.IR X509toRSApub .
173For the special case of
174certificates signed by a known trusted key
175(in a single step, without certificate chains)
176.I X509verify
177checks the signature on
178.IR cert .
179It returns nil if successful, else an error string.
180.PP
rsc058b0112005-01-03 06:40:20 +0000181.I X509dump
182prints an X.509 certificate to standard ouptut.
183.PP
rsccfa37a72004-04-10 18:53:55 +0000184.I X509gen
185creates a self-signed X.509 certificate, given an RSA keypair
186.IR priv ,
187a issuer/subject string
188.IR subj ,
189and the starting and ending validity dates,
190.IR valid .
191Length of the allocated binary certificate is stored in
192.IR certlen .
193The subject line is conventionally of the form
194.EX
195 "C=US ST=NJ L=07922 O=Lucent OU='Bell Labs' CN=Eric"
196.EE
197using the quoting conventions of
rsc058b0112005-01-03 06:40:20 +0000198.I tokenize
199(see
200.IR getfields (3)).
201.PP
202.I X509req
203creates an X.509 certification request.
rsccfa37a72004-04-10 18:53:55 +0000204.PP
205.I Asn1toRSApriv
206converts an ASN1 formatted RSA private key into the corresponding
207.B RSApriv
208structure.
209.PP
210.I Asn1dump
211prints an ASN1 object to standard output.
212.PP
213.I Decodepem
214takes a zero terminated string,
215.IR s ,
216and decodes the PEM (privacy-enhanced mail) formatted section for
217.I type
218within it.
219If successful, it returns the decoded section and sets
220.BI * len
221to its decoded length.
222If not, it returns
223.BR nil ,
224and
225.BI * len
226is undefined.
rsc058b0112005-01-03 06:40:20 +0000227.PP
228.I Decodepemchain
229is similar but expects a sequence of PEM-formatted sections
230and returns a linked list of the decodings:
231.IP
232.EX
233typedef struct PEMChain PEMChain
234struct PEMChain
235{
236 PEMChain *next;
237 uchar *pem;
238 int pemlen;
239};
240.EE
rsccfa37a72004-04-10 18:53:55 +0000241.SH SOURCE
rscc3674de2005-01-11 17:37:33 +0000242.B \*9/src/libsec
rsccfa37a72004-04-10 18:53:55 +0000243.SH SEE ALSO
rscbf8a59f2004-04-11 03:42:27 +0000244.IR mp (3),
245.IR aes (3),
246.IR blowfish (3),
247.IR des (3),
248.IR dsa (3),
249.IR elgamal (3),
250.IR rc4 (3),
251.IR sechash (3),
252.IR prime (3),
rsc058b0112005-01-03 06:40:20 +0000253.IR rand (3)
254.\" .IR pem (8)