rsa 4.3 KB

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