Subversion Repositories planix.SVN

Rev

Rev 2 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 - 1
.TH RSA 2
2
.SH NAME
3
asn1dump,
4
asn1toRSApub,
5
asn1toRSApriv,
6
decodePEM,
7
rsadecrypt,
8
rsaencrypt,
9
rsagen,
10
rsaprivalloc,
11
rsaprivfree,
12
rsaprivtopub,
13
rsapuballoc,
14
rsapubfree,
15
RSApubtoasn1,
16
X509toRSApub,
17
X509gen,
18
X509verify \- RSA encryption algorithm
19
.SH SYNOPSIS
20
.B #include <u.h>
21
.br
22
.B #include <libc.h>
23
.br
24
.B #include <mp.h>
25
.br
26
.B #include <libsec.h>
27
.PP
28
.ta +\w'\fLRSApriv* \fP'u
29
.B
30
RSApriv*	rsagen(int nlen, int elen, int nrep)
31
.PP
32
.B
33
mpint*	rsaencrypt(RSApub *k, mpint *in, mpint *out)
34
.PP
35
.B
36
mpint*	rsadecrypt(RSApriv *k, mpint *in, mpint *out)
37
.PP
38
.B
39
RSApub*	rsapuballoc(void)
40
.PP
41
.B
42
void	rsapubfree(RSApub*)
43
.PP
44
.B
45
RSApriv*	rsaprivalloc(void)
46
.PP
47
.B
48
void	rsaprivfree(RSApriv*)
49
.PP
50
.B
51
RSApub*	rsaprivtopub(RSApriv*)
52
.PP
53
.B
54
RSApub*	X509toRSApub(uchar *cert, int ncert, char *name, int nname)
55
.PP
56
.B
57
RSApub*	asn1toRSApub(uchar *pub, int npub)
58
.PP
59
.B
60
uchar* RSApubtoasn1(RSApub *pub, int *keylen)
61
.PP
62
.B
63
RSApriv*	asn1toRSApriv(uchar *priv, int npriv)
64
.PP
65
.B
66
void		asn1dump(uchar *der, int len)
67
.PP
68
.B
69
uchar*	decodePEM(char *s, char *type, int *len, char **new_s)
70
.PP
71
.B
72
uchar*	X509gen(RSApriv *priv, char *subj, ulong valid[2], int *certlen);
73
.PP
74
.B
75
uchar*	X509req(RSApriv *priv, char *subj, int *certlen);
76
.PP
77
.B
78
char*	X509verify(uchar *cert, int ncert, RSApub *pk)
79
.DT
80
.SH DESCRIPTION
81
RSA is a public key encryption algorithm.  The owner of a key publishes
82
the public part of the key:
83
.IP
84
.EX
85
struct RSApub
86
{
87
	mpint	*n;	/* modulus */
88
	mpint	*ek;	/* exp (encryption key) */
89
};
90
.EE
91
.LP
92
This part can be used for encrypting data (with
93
.IR rsaencrypt )
94
to be sent to the owner.
95
The owner decrypts (with
96
.IR rsadecrypt )
97
using his private key:
98
.IP
99
.EX
100
struct RSApriv
101
{
102
	RSApub	pub;
103
	mpint	*dk;	/* exp (decryption key) */
104
 
105
	/* precomputed crt values */
106
	mpint	*p;
107
	mpint	*q;
108
	mpint	*kp;	/* k mod p-1 */
109
	mpint	*kq;	/* k mod q-1 */
110
	mpint	*c2;	/* for converting residues to number */
111
};
112
.EE
113
.PP
114
Keys are generated using
115
.IR rsagen .
116
.I Rsagen
117
takes both bit length of the modulus, the bit length of the
118
public key exponent, and the number of repetitions of the Miller-Rabin
119
primality test to run.  If the latter is 0, it does the default number
120
of rounds.
121
.I Rsagen
122
returns a newly allocated structure containing both
123
public and private keys.
124
.I Rsaprivtopub
125
returns a newly allocated copy of the public key
126
corresponding to the private key.
127
.PP
128
The routines
129
.IR rsaalloc ,
130
.IR rsafree ,
131
.IR rsapuballoc ,
132
.IR rsapubfree ,
133
.IR rsaprivalloc ,
134
and
135
.I rsaprivfree
136
are provided to aid in user provided key I/O.
137
.PP
138
Given a binary X.509
139
.IR cert ,
140
the routine
141
.I X509toRSApub
142
returns the public key and, if
143
.I name
144
is not nil, the CN part of the Distinguished Name of the
145
certificate's Subject.
146
(This is conventionally a userid or a host DNS name.)
147
No verification is done of the certificate signature;  the
148
caller should check the fingerprint,
149
.IR sha1(cert) ,
150
against a table or check the certificate by other means.
151
X.509 certificates are often stored in PEM format; use
152
.I dec64
153
to convert to binary before computing the fingerprint or calling
154
.IR X509toRSApub .
155
For the special case of
156
certificates signed by a known trusted key
157
(in a single step, without certificate chains),
158
.I X509verify
159
checks the signature on
160
.IR cert .
161
It returns nil if successful, else an error string.
162
.PP
163
.I X509gen
164
creates a self-signed X.509 certificate, given an RSA keypair
165
.IR priv ,
166
a issuer/subject string
167
.IR subj ,
168
and the starting and ending validity dates,
169
.IR valid .
170
Length of the allocated binary certificate is stored in
171
.IR certlen .
172
The subject line is conventionally of the form
173
.IP
174
.EX
175
C=US ST=NJ L=07922 O=Lucent OU='Bell Labs' CN=Eric
176
.EE
177
.LP
178
using the quoting conventions of
179
.I tokenize
180
in
181
.IR getfields (2).
182
.PP
183
.I Asn1toRSApub
184
converts an ASN1-formatted RSA public key into the corresponding
185
.B RSApub
186
structure.
187
.PP
188
.I RSApubtoasn1
189
encodes an RSA public key using ASN.1 DER.  It returns a pointer
190
to the encoded key; if keylen is non-nil, it sets
191
.B *keylen
192
to the length of the encoded key.
193
.PP
194
.I Asn1toRSApriv
195
converts an ASN1 formatted RSA private key into the corresponding
196
.B RSApriv
197
structure.
198
.PP
199
.I Asn1dump
200
prints an ASN1 object to standard output.
201
.PP
202
.I DecodePEM
203
takes a zero terminated string,
204
.IR s ,
205
and decodes the PEM (privacy-enhanced mail) formatted section for
206
.I type
207
within it.
208
If successful, it returns
209
.IR malloc ed
210
storage containing the decoded section,
211
which the caller must free,
212
and sets
213
.BI * len
214
to its decoded length.
215
Otherwise
216
.B nil
217
is returned and
218
.BI * len
219
is undefined.
220
If not nil,
221
.I new_s
222
is set to the first character beyond the
223
.I type
224
section.
225
.SH SOURCE
226
.B /sys/src/libsec
227
.SH SEE ALSO
228
.IR mp (2),
229
.IR aes (2),
230
.IR blowfish (2),
231
.IR des (2),
232
.IR dsa (2),
233
.IR elgamal (2),
234
.IR rc4 (2),
235
.IR sechash (2),
236
.IR prime (2),
237
.IR rand (2),
238
.IR rsa (8)