Click
Loading...
Searching...
No Matches
md5.h
1/* -*- related-file-name: "../../lib/md5.cc" -*-
2 Copyright (c) 2006-2007 Regents of the University of California
3 Altered for Click by Eddie Kohler. */
4/*
5 Copyright (C) 1999, 2002 Aladdin Enterprises. All rights reserved.
6
7 This software is provided 'as-is', without any express or implied
8 warranty. In no event will the authors be held liable for any damages
9 arising from the use of this software.
10
11 Permission is granted to anyone to use this software for any purpose,
12 including commercial applications, and to alter it and redistribute it
13 freely, subject to the following restrictions:
14
15 1. The origin of this software must not be misrepresented; you must not
16 claim that you wrote the original software. If you use this software
17 in a product, an acknowledgment in the product documentation would be
18 appreciated but is not required.
19 2. Altered source versions must be plainly marked as such, and must not be
20 misrepresented as being the original software.
21 3. This notice may not be removed or altered from any source distribution.
22
23 L. Peter Deutsch
24 ghost@aladdin.com
25
26 */
27/*
28 Independent implementation of MD5 (RFC 1321).
29
30 This code implements the MD5 Algorithm defined in RFC 1321, whose
31 text is available at
32 http://www.ietf.org/rfc/rfc1321.txt
33 The code is derived from the text of the RFC, including the test suite
34 (section A.5) but excluding the rest of Appendix A. It does not include
35 any code or documentation that is identified in the RFC as being
36 copyrighted.
37
38 The original and principal author of md5.h is L. Peter Deutsch
39 <ghost@aladdin.com>. Other authors are noted in the change history
40 that follows (in reverse chronological order):
41
42 2002-04-13 lpd Removed support for non-ANSI compilers; removed
43 references to Ghostscript; clarified derivation from RFC 1321;
44 now handles byte order either statically or dynamically.
45 1999-11-04 lpd Edited comments slightly for automatic TOC extraction.
46 1999-10-18 lpd Fixed typo in header comment (ansi2knr rather than md5);
47 added conditionalization for C++ compilation from Martin
48 Purschke <purschke@bnl.gov>.
49 1999-05-03 lpd Original version.
50 */
51
52#ifndef CLICK_MD5_H
53#define CLICK_MD5_H
54#if CLICK_LINUXMODULE
55# include <click/cxxprotect.h>
56CLICK_CXX_PROTECT
57# include <linux/err.h>
58# include <linux/crypto.h>
59# if HAVE_LINUX_ASM_SCATTERLIST_H
60# include <asm/scatterlist.h>
61# endif
62# include <linux/scatterlist.h>
63CLICK_CXX_UNPROTECT
64# include <click/cxxunprotect.h>
65
66#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,19)
67typedef struct hash_desc md5_state_t;
68#else
69typedef struct crypto_tfm *md5_state_t;
70#endif
71
72static inline int md5_init(md5_state_t *pms) {
73#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,19)
74 pms->tfm = crypto_alloc_hash("md5", 0, CRYPTO_ALG_ASYNC);
75 if (IS_ERR(pms->tfm))
76 return PTR_ERR(pms->tfm);
77 pms->flags = 0;
78 crypto_hash_init(pms);
79 return 0;
80#else
81 *pms = crypto_alloc_tfm("md5", 0);
82 if (IS_ERR(*pms))
83 return PTR_ERR(*pms);
84 crypto_digest_init(*pms);
85 return 0;
86#endif
87}
88
89static inline void md5_reinit(md5_state_t *pms) {
90#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,19)
91 crypto_hash_init(pms);
92#else
93 crypto_digest_init(*pms);
94#endif
95}
96
97static inline void md5_append(md5_state_t *pms, const unsigned char *data, int nbytes) {
98 struct scatterlist sg;
99 if (likely(virt_addr_valid((unsigned long)data))) {
100 sg_init_one(&sg, const_cast<uint8_t *>(data), nbytes);
101#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,19)
102 crypto_hash_update(pms, &sg, nbytes);
103#else
104 crypto_digest_update(*pms, &sg, 1);
105#endif
106 } else {
107 while (nbytes > 0) {
108 int len = nbytes;
109 int off = offset_in_page(data);
110 if (off + len > (int)PAGE_SIZE)
111 len = PAGE_SIZE - off;
112 sg_init_table(&sg, 1);
113 sg_set_page(&sg, vmalloc_to_page(const_cast<uint8_t *>(data)), len, off);
114#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,19)
115 crypto_hash_update(pms, &sg, len);
116#else
117 crypto_digest_update(*pms, &sg, 1);
118#endif
119 data += len;
120 nbytes -= len;
121 }
122 }
123}
124
125static inline void md5_finish(md5_state_t *pms, unsigned char digest[16]) {
126#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,19)
127 crypto_hash_final(pms, digest);
128#else
129 crypto_digest_final(*pms, digest);
130#endif
131}
132
133static inline void md5_free(md5_state_t *pms) {
134#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,19)
135 crypto_free_hash(pms->tfm);
136#else
137 crypto_free_tfm(*pms);
138#endif
139}
140
141#else
142
143/*
144 * This package supports both compile-time and run-time determination of CPU
145 * byte order. If ARCH_IS_BIG_ENDIAN is defined as 0, the code will be
146 * compiled to run only on little-endian CPUs; if ARCH_IS_BIG_ENDIAN is
147 * defined as non-zero, the code will be compiled to run only on big-endian
148 * CPUs; if ARCH_IS_BIG_ENDIAN is not defined, the code will be compiled to
149 * run on either big- or little-endian CPUs, but will run slightly less
150 * efficiently on either one than if ARCH_IS_BIG_ENDIAN is defined.
151 */
152
153typedef unsigned char md5_byte_t; /* 8-bit byte */
154typedef uint32_t md5_word_t; /* 32-bit word */
155
156/* Define the state of the MD5 Algorithm. */
157typedef struct md5_state_s {
158 md5_word_t count[2]; /* message length in bits, lsw first */
159 md5_word_t abcd[4]; /* digest buffer */
160 md5_byte_t buf[64]; /* accumulate block */
161} md5_state_t;
162
163#ifdef __cplusplus
164extern "C"
165{
166#endif
167
168/* Initialize the algorithm. */
169int md5_init(md5_state_t *pms);
170
171/* Re-initialize the algorithm after a prior md5_init(). */
172static inline void md5_reinit(md5_state_t *pms) {
173 md5_init(pms);
174}
175
176/* Append a string to the message. */
177void md5_append(md5_state_t *pms, const md5_byte_t *data, int nbytes);
178
179/* Finish the message and return the digest. */
180#define MD5_DIGEST_SIZE 16
181void md5_finish(md5_state_t *pms, md5_byte_t digest[16]);
182
183/* Finish the message and return the digest in ASCII. DOES NOT write a
184 terminating NUL character.
185 If 'allow_at == 0', the digest uses characters [A-Za-z0-9_], and has
186 length between MD5_TEXT_DIGEST_SIZE and MD5_TEXT_DIGEST_MAX_SIZE.
187 If 'allow_at != 0', the digest uses characters [A-Za-z0-9_@], and has
188 length of exactly MD5_TEXT_DIGEST_SIZE.
189 Returns the number of characters written. Again, this will NOT include
190 a terminating NUL. */
191#define MD5_TEXT_DIGEST_SIZE 22 /* min len */
192#define MD5_TEXT_DIGEST_MAX_SIZE 26 /* max len if !allow_at */
193int md5_finish_text(md5_state_t *pms, char *text_digest, int allow_at);
194
195#define md5_free(pms) /* do nothing */
196
197#ifdef __cplusplus
198}
199#endif
200
201#endif
202#endif
203