Blob


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