1 /*
2  * This file is part of the Sofia-SIP package
3  *
4  * Copyright (C) 2005 Nokia Corporation.
5  *
6  * Contact: Pekka Pessi <pekka.pessi@nokia.com>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation; either version 2.1 of
11  * the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21  * 02110-1301 USA
22  *
23  */
24 
25 #ifndef SU_MD5_H /**  Defined when su_md5.h has been included. */
26 #define SU_MD5_H
27 
28 /**@defgroup su_md5 MD5 Digest
29  *
30  * The MD5 message digest algorithm is described in RFC 1321 by R. Rivest,
31  * 1992.  The algorithm takes as input a octet string of arbitrary length
32  * and generates a 128-bit hash value, "message digest" from the message
33  * contents.
34  *
35  * While some message collisions (different messages with same MD5 digest)
36  * has been generated, using collisions in an actual attack is much harder
37  * and MD5 can be considered as cryptographically strong.
38  */
39 
40 /** @file sofia-sip/su_md5.h MD5 digest interface.
41  *
42  * @author <Pekka.Pessi@nokia.com>
43  */
44 
45 #ifndef SU_TYPES_H
46 #include "sofia-sip/su_types.h"
47 #endif
48 
49 SOFIA_BEGIN_DECLS
50 
51 /** MD5 context. */
52 typedef struct su_md5_t {
53   uint32_t buf[4];
54   uint32_t bits[2];
55   uint8_t  in[64];
56 } su_md5_t;
57 
58 #define SU_MD5_DIGEST_SIZE 16
59 
60 SOFIAPUBFUN void su_md5_init(su_md5_t *context);
61 SOFIAPUBFUN void su_md5_deinit(su_md5_t *context);
62 SOFIAPUBFUN void su_md5_update(su_md5_t *context,
63 			       void const *buf, usize_t len);
64 SOFIAPUBFUN void su_md5_strupdate(su_md5_t *ctx, char const *s);
65 SOFIAPUBFUN void su_md5_str0update(su_md5_t *ctx, char const *s);
66 
67 SOFIAPUBFUN void su_md5_iupdate(su_md5_t *context,
68 				void const *buf, usize_t len);
69 SOFIAPUBFUN void su_md5_striupdate(su_md5_t *ctx, char const *s);
70 SOFIAPUBFUN void su_md5_stri0update(su_md5_t *ctx, char const *s);
71 
72 SOFIAPUBFUN void su_md5_digest(su_md5_t const *ctx,
73 			       uint8_t digest[SU_MD5_DIGEST_SIZE]);
74 SOFIAPUBFUN void su_md5_hexdigest(su_md5_t const *ctx,
75 				  char digest[2 * SU_MD5_DIGEST_SIZE + 1]);
76 
77 #define SU_MD5_STRUPDATE(ctx, s) \
78  ((s) ? su_md5_update(ctx, (s), strlen(s)) : (void)0)
79 #define SU_MD5_STR0UPDATE(ctx, s) \
80  su_md5_update(ctx, (s) ? (s) : "", (s) ? strlen(s) + 1 : 1)
81 #define SU_MD5_STRIUPDATE(ctx, s) \
82   ((s) ? su_md5_iupdate(ctx, (s), strlen(s)) : (void)0)
83 #define SU_MD5_STRI0UPDATE(ctx, s) \
84   su_md5_iupdate(ctx, (s) ? (s) : "", (s) ? strlen(s) : 1)
85 
86 SOFIA_END_DECLS
87 
88 #endif /* !defined(MD5_H) */
89