jmd5.h
4.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/***************************************************************************
* Copyright (C) 2005 by Jeff Ferr *
* root@sat *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
a***************************************************************************/
#ifndef J_MD5_H
#define J_MD5_H
#include "jobject.h"
#include <string>
#include <stdint.h>
/* F, G, H and I are basic MD5 functions. */
#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
#define H(x, y, z) ((x) ^ (y) ^ (z))
#define I(x, y, z) ((y) ^ ((x) | (~z)))
/* ROTATE_LEFT rotates x left n bits. */
#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
Rotation is separate from addition to prevent recomputation. */
#define FF(a, b, c, d, x, s, ac) { \
(a) += F ((b), (c), (d)) + (x) + (uint32_t)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
#define GG(a, b, c, d, x, s, ac) { \
(a) += G ((b), (c), (d)) + (x) + (uint32_t)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
#define HH(a, b, c, d, x, s, ac) { \
(a) += H ((b), (c), (d)) + (x) + (uint32_t)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
#define II(a, b, c, d, x, s, ac) { \
(a) += I ((b), (c), (d)) + (x) + (uint32_t)(ac); \
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
#define MD5_LEN_BIN 16
#define MD5_LEN_STR 32
namespace jmath {
enum jmd5_rc_t {
MD5_RC_OK = 0,
MD5_RC_ARG = 1,
MD5_RC_MEM = 2
};
struct MD5_CTX {
uint32_t state[4]; // state (ABCD)
uint32_t count[2]; // number of bits, modulo 2^64 (lsb first)
uint8_t buffer[64]; // input buffer
};
/**
* \brief This is a RFC 1321 compliant Message Digest 5 (MD5) algorithm
* implementation. It is directly derived from the RSA code published in
* RFC 1321 with just the following functionality preserving changes:
* - converted function definitions from K&R to ANSI C
* - included contents of the "global.h" and "md5.h" headers
* - moved the SXX defines into the MD5Transform function
* - replaced MD5_memcpy() with memcpy(3) and MD5_memset() with memset(3)
* - renamed "index" variables to "idx" to avoid namespace conflicts
* - reformatted C style to conform with OSSP C style
* - added own OSSP style frontend API
*
* \author Jeff Ferr
*/
class MD5 : public virtual jcommon::Object{
private:
public:
/*
* \brief
*
*/
MD5();
/*
* \brief
*
*/
virtual ~MD5();
/*
* \brief MD5 initialization. Begins an MD5 operation, writing a new context.
*
*/
static void MD5Init(MD5_CTX *context);
/*
* \brief MD5 block update operation. Continues an MD5 message-digest
* operation, processing another message block, and updating the context.
*
*/
static void MD5Update(MD5_CTX *context, uint8_t *input, unsigned int inputLen);
/*
* \brief MD5 finalization. Ends an MD5 message-digest operation, writing the
* the message digest and zeroizing the context.
*
*/
static void MD5Final(MD5_CTX *context, uint8_t digest[]);
/*
* \brief MD5 basic transformation. Transforms state based on block.
*
*/
static void MD5Transform(uint32_t state[], uint8_t block[]);
/*
* \brief Encodes input (uint32_t) into output (uint8_t).
* Assumes len is a multiple of 4.
*
*/
static void Encode(uint8_t *output, uint32_t *input, unsigned int len);
/*
* \brief Decodes input (uint8_t) into output (uint32_t).
* Assumes len is a multiple of 4.
*
*/
static void Decode(uint32_t *output, uint8_t *input, unsigned int len);
/*
* \brief
*
*/
virtual std::string what();
};
}
#endif