sechash 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. .TH SECHASH 2
  2. .SH NAME
  3. md4, md5, sha1, aes, hmac_md5, hmac_sha1, hmac_aes, md5pickle, md5unpickle, sha1pickle, sha1unpickle \- cryptographically secure hashes
  4. .SH SYNOPSIS
  5. .de Ti
  6. .in +0.5i
  7. .ti -0.5i
  8. ..
  9. .B #include <u.h>
  10. .br
  11. .B #include <libc.h>
  12. .br
  13. .B #include <mp.h>
  14. .br
  15. .B #include <libsec.h>
  16. .PP
  17. .Ti
  18. .B
  19. DigestState* md4(uchar *data, ulong dlen, uchar *digest, DigestState *state)
  20. .PP
  21. .Ti
  22. .B
  23. DigestState* md5(uchar *data, ulong dlen, uchar *digest, DigestState *state)
  24. .PP
  25. .B
  26. char* md5pickle(MD5state *state)
  27. .PP
  28. .B
  29. MD5state* md5unpickle(char *p);
  30. .PP
  31. .Ti
  32. .B
  33. DigestState* sha1(uchar *data, ulong dlen, uchar *digest, DigestState *state)
  34. .PP
  35. .B
  36. char* sha1pickle(MD5state *state)
  37. .PP
  38. .B
  39. MD5state* sha1unpickle(char *p);
  40. .PP
  41. .Ti
  42. .B
  43. DigestState* aes(uchar *data, ulong dlen, uchar *digest, DigestState *state)
  44. .PP
  45. .Ti
  46. .B
  47. DigestState* hmac_x(uchar *p, ulong len, uchar *key, ulong klen, uchar *digest, DigestState *s, DigestState*(*x)(uchar*, ulong, uchar*, DigestState*), int xlen)
  48. .PP
  49. .Ti
  50. .B
  51. DigestState* hmac_md5(uchar *data, ulong dlen, uchar *key, ulong klen, uchar *digest, DigestState *state)
  52. .PP
  53. .Ti
  54. .B
  55. DigestState* hmac_sha1(uchar *data, ulong dlen, uchar *key, ulong klen, uchar *digest, DigestState *state)
  56. .PP
  57. .Ti
  58. .B
  59. DigestState* hmac_aes(uchar *data, ulong dlen, uchar *key, ulong klen, uchar *digest, DigestState *state)
  60. .SH DESCRIPTION
  61. We support several secure hash functions. The output of a
  62. hash is called a
  63. .IR digest .
  64. A hash is secure if, given the hashed data and the digest,
  65. it is difficult to predict the change to the digest resulting
  66. from some change to the data without rehashing
  67. the whole data. Therefore, if a secret is part of the hashed
  68. data, the digest can be used as an integrity check of the data by anyone
  69. possessing the secret.
  70. .PP
  71. The routines
  72. .IR md4 ,
  73. .IR md5 ,
  74. .IR sha1 ,
  75. .IR aes ,
  76. .IR hmac_md5 ,
  77. .IR hmac_sha1 ,
  78. and
  79. .I hmac_aes
  80. differ only in the length of the resulting digest
  81. and in the security of the hash. Usage for each is the same.
  82. The first call to the routine should have
  83. .B nil
  84. as the
  85. .I state
  86. parameter. This call returns a state which can be used to chain
  87. subsequent calls.
  88. The last call should have digest
  89. .RL non- nil .
  90. .I Digest
  91. must point to a buffer of at least the size of the digest produced.
  92. This last call will free the state and copy the result into
  93. .IR digest .
  94. .PP
  95. The constants
  96. .IR MD4dlen ,
  97. .IR MD5dlen ,
  98. and
  99. .I SHA1dlen
  100. define the lengths of the digests.
  101. .PP
  102. .IR Hmac_md5 ,
  103. .IR hmac_sha1 .
  104. and
  105. .I hmac_aes
  106. are used slightly differently. These hash algorithms are keyed and require
  107. a key to be specified on every call.
  108. The digest lengths for these hashes are
  109. .IR MD5dlen ,
  110. .IR SHA1dlen ,
  111. and
  112. .I AESdlen
  113. respectively.
  114. These routines all call
  115. .I hmac_x
  116. internally, but
  117. .I hmac_x
  118. is not intended for general use.
  119. .PP
  120. The functions
  121. .I md5pickle
  122. and
  123. .I sha1pickle
  124. marshal the state of a digest for transmission.
  125. .I Md5unpickle
  126. and
  127. .I sha1unpickle
  128. unmarshal a pickled digest.
  129. All four routines return a pointer to a newly
  130. .IR malloc (2)'d
  131. object.
  132. .SH EXAMPLES
  133. To hash a single buffer using
  134. .IR md5 :
  135. .IP
  136. .EX
  137. uchar digest[MD5dlen];
  138. md5(data, len, digest, nil);
  139. .EE
  140. .PP
  141. To chain a number of buffers together,
  142. bounded on each end by some secret:
  143. .IP
  144. .EX
  145. char buf[256];
  146. uchar digest[MD5dlen];
  147. DigestState *s;
  148. s = md5("my password", 11, nil, nil);
  149. while((n = read(fd, buf, 256)) > 0)
  150. md5(buf, n, nil, s);
  151. md5("drowssap ym", 11, digest, s);
  152. .EE
  153. .SH SOURCE
  154. .B /sys/src/libsec
  155. .SH SEE ALSO
  156. .IR aes (2),
  157. .IR blowfish (2),
  158. .IR des (2),
  159. .IR elgamal (2),
  160. .IR rc4 (2),
  161. .IR rsa (2)