rsa 4.2 KB

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