1
0

rsa 4.0 KB

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