Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Wim Lewis
nettle
Commits
5046486e
Commit
5046486e
authored
Aug 26, 2003
by
Niels Möller
Browse files
Reorderd functions, putting md5_final at the end.
Rev: src/nettle/md5.c:1.8
parent
ae200b5e
Changes
1
Hide whitespace changes
Inline
Side-by-side
md5.c
View file @
5046486e
...
...
@@ -46,6 +46,9 @@ md5_transform(uint32_t *digest, const uint32_t *data);
static
void
md5_block
(
struct
md5_ctx
*
ctx
,
const
uint8_t
*
block
);
static
void
md5_final
(
struct
md5_ctx
*
ctx
);
void
md5_init
(
struct
md5_ctx
*
ctx
)
{
...
...
@@ -92,53 +95,6 @@ md5_update(struct md5_ctx *ctx,
memcpy
(
ctx
->
block
,
data
,
length
);
}
/* Final wrapup - pad to MD5_DATA_SIZE-byte boundary with the bit
* pattern 1 0* (64-bit count of bits processed, LSB-first) */
static
void
md5_final
(
struct
md5_ctx
*
ctx
)
{
uint32_t
data
[
MD5_DATA_LENGTH
];
unsigned
i
;
unsigned
words
;
i
=
ctx
->
index
;
/* Set the first char of padding to 0x80. This is safe since there
* is always at least one byte free */
assert
(
i
<
MD5_DATA_SIZE
);
ctx
->
block
[
i
++
]
=
0x80
;
/* Fill rest of word */
for
(
;
i
&
3
;
i
++
)
ctx
->
block
[
i
]
=
0
;
/* i is now a multiple of the word size 4 */
words
=
i
>>
2
;
for
(
i
=
0
;
i
<
words
;
i
++
)
data
[
i
]
=
LE_READ_UINT32
(
ctx
->
block
+
4
*
i
);
if
(
words
>
(
MD5_DATA_LENGTH
-
2
))
{
/* No room for length in this block. Process it and
* pad with another one */
for
(
i
=
words
;
i
<
MD5_DATA_LENGTH
;
i
++
)
data
[
i
]
=
0
;
md5_transform
(
ctx
->
digest
,
data
);
for
(
i
=
0
;
i
<
(
MD5_DATA_LENGTH
-
2
);
i
++
)
data
[
i
]
=
0
;
}
else
for
(
i
=
words
;
i
<
MD5_DATA_LENGTH
-
2
;
i
++
)
data
[
i
]
=
0
;
/* There are 512 = 2^9 bits in one block
* Little-endian order => Least significant word first */
data
[
MD5_DATA_LENGTH
-
1
]
=
(
ctx
->
count_h
<<
9
)
|
(
ctx
->
count_l
>>
23
);
data
[
MD5_DATA_LENGTH
-
2
]
=
(
ctx
->
count_l
<<
9
)
|
(
ctx
->
index
<<
3
);
md5_transform
(
ctx
->
digest
,
data
);
}
void
md5_digest
(
struct
md5_ctx
*
ctx
,
unsigned
length
,
...
...
@@ -175,10 +131,10 @@ md5_digest(struct md5_ctx *ctx,
}
/* MD5 functions */
#define F1(x, y, z) (
z
^ (
x
& (
y ^ z
)))
#define F2(x, y, z) F1(
z, x
, y)
#define F3(x, y, z) (
x ^ y
^ z)
#define F4(x, y, z) (
y
^ (x | ~
z
))
#define F1(x, y, z) (
(z)
^ (
(x)
& (
(y) ^ (z)
)))
#define F2(x, y, z) F1(
(z), (x)
,
(
y)
)
#define F3(x, y, z) (
(x) ^ (y)
^
(
z)
)
#define F4(x, y, z) (
(y)
^
(
(x
)
| ~
(z)
))
#define ROUND(f, w, x, y, z, data, s) \
( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
...
...
@@ -288,3 +244,50 @@ md5_block(struct md5_ctx *ctx, const uint8_t *block)
md5_transform
(
ctx
->
digest
,
data
);
}
/* Final wrapup - pad to MD5_DATA_SIZE-byte boundary with the bit
* pattern 1 0* (64-bit count of bits processed, LSB-first) */
static
void
md5_final
(
struct
md5_ctx
*
ctx
)
{
uint32_t
data
[
MD5_DATA_LENGTH
];
unsigned
i
;
unsigned
words
;
i
=
ctx
->
index
;
/* Set the first char of padding to 0x80. This is safe since there
* is always at least one byte free */
assert
(
i
<
MD5_DATA_SIZE
);
ctx
->
block
[
i
++
]
=
0x80
;
/* Fill rest of word */
for
(
;
i
&
3
;
i
++
)
ctx
->
block
[
i
]
=
0
;
/* i is now a multiple of the word size 4 */
words
=
i
>>
2
;
for
(
i
=
0
;
i
<
words
;
i
++
)
data
[
i
]
=
LE_READ_UINT32
(
ctx
->
block
+
4
*
i
);
if
(
words
>
(
MD5_DATA_LENGTH
-
2
))
{
/* No room for length in this block. Process it and
* pad with another one */
for
(
i
=
words
;
i
<
MD5_DATA_LENGTH
;
i
++
)
data
[
i
]
=
0
;
md5_transform
(
ctx
->
digest
,
data
);
for
(
i
=
0
;
i
<
(
MD5_DATA_LENGTH
-
2
);
i
++
)
data
[
i
]
=
0
;
}
else
for
(
i
=
words
;
i
<
MD5_DATA_LENGTH
-
2
;
i
++
)
data
[
i
]
=
0
;
/* There are 512 = 2^9 bits in one block
* Little-endian order => Least significant word first */
data
[
MD5_DATA_LENGTH
-
1
]
=
(
ctx
->
count_h
<<
9
)
|
(
ctx
->
count_l
>>
23
);
data
[
MD5_DATA_LENGTH
-
2
]
=
(
ctx
->
count_l
<<
9
)
|
(
ctx
->
index
<<
3
);
md5_transform
(
ctx
->
digest
,
data
);
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment