projects
/
bertos.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Fix conding stily.
[bertos.git]
/
bertos
/
sec
/
hash
/
md5.c
diff --git
a/bertos/sec/hash/md5.c
b/bertos/sec/hash/md5.c
index 54413267e29a0f561936cf97786943561114240d..e4d2c135be86e48bb07fd97cb6d59cbfa712fe00 100644
(file)
--- a/
bertos/sec/hash/md5.c
+++ b/
bertos/sec/hash/md5.c
@@
-37,7
+37,7
@@
static void byteReverse(uint32_t *buf, unsigned longs)
static void MD5_begin(Hash *h)
{
MD5_Context *ctx = (MD5_Context *)h;
static void MD5_begin(Hash *h)
{
MD5_Context *ctx = (MD5_Context *)h;
-
+
ctx->buf[0] = 0x67452301;
ctx->buf[1] = 0xefcdab89;
ctx->buf[2] = 0x98badcfe;
ctx->buf[0] = 0x67452301;
ctx->buf[1] = 0xefcdab89;
ctx->buf[2] = 0x98badcfe;
@@
-54,6
+54,11
@@
static void MD5_update(Hash *h, const void* vbuf, size_t len)
{
MD5_Context *ctx = (MD5_Context *)h;
const char *buf = (const char *)vbuf;
{
MD5_Context *ctx = (MD5_Context *)h;
const char *buf = (const char *)vbuf;
+ uint32_t *aligned_ptr = NULL;
+ if (is_aligned(ctx->in, sizeof(uint32_t)))
+ aligned_ptr = (uint32_t *)((size_t)ctx->in);
+ else
+ ASSERT2(0, "Unaligned memory");
uint32_t t;
/* Update bitcount */
uint32_t t;
/* Update bitcount */
@@
-62,26
+67,29
@@
static void MD5_update(Hash *h, const void* vbuf, size_t len)
/* Handle any leading odd-sized chunks */
/* Handle any leading odd-sized chunks */
- if (t) {
+ if (t)
+ {
uint8_t *p = (uint8_t*) ctx->in + t;
t = 64 - t;
uint8_t *p = (uint8_t*) ctx->in + t;
t = 64 - t;
- if (len < t) {
+ if (len < t)
+ {
memcpy(p, buf, len);
return;
}
memcpy(p, buf, t);
memcpy(p, buf, len);
return;
}
memcpy(p, buf, t);
-
byteReverse((uint32_t*)ctx->in
, 16);
-
MD5Transform(ctx->buf, (uint32_t*)ctx->in
);
+
byteReverse(aligned_ptr
, 16);
+
MD5Transform(ctx->buf, aligned_ptr
);
buf += t;
len -= t;
}
/* Process data in 64-byte chunks */
buf += t;
len -= t;
}
/* Process data in 64-byte chunks */
- while (len >= 64) {
+ while (len >= 64)
+ {
memcpy(ctx->in, buf, 64);
memcpy(ctx->in, buf, 64);
- byteReverse(
(uint32_t*)ctx->in
, 16);
- MD5Transform(ctx->buf,
(uint32_t*)ctx->in
);
+ byteReverse(
aligned_ptr
, 16);
+ MD5Transform(ctx->buf,
aligned_ptr
);
buf += 64;
len -= 64;
}
buf += 64;
len -= 64;
}
@@
-94,11
+102,16
@@
static void MD5_update(Hash *h, const void* vbuf, size_t len)
* Final wrapup - pad to 64-byte boundary with the bit pattern
* 1 0* (64-bit count of bits processed, MSB-first)
*/
* Final wrapup - pad to 64-byte boundary with the bit pattern
* 1 0* (64-bit count of bits processed, MSB-first)
*/
-uint8_t* MD5_final(struct Hash *h)
+
static
uint8_t* MD5_final(struct Hash *h)
{
MD5_Context *ctx = (MD5_Context *)h;
unsigned count;
unsigned char *p;
{
MD5_Context *ctx = (MD5_Context *)h;
unsigned count;
unsigned char *p;
+ uint32_t *aligned_ptr = NULL;
+ if (is_aligned(ctx->in, sizeof(uint32_t)))
+ aligned_ptr = (uint32_t *)((size_t)ctx->in);
+ else
+ ASSERT2(0, "Unaligned memory");
/* Compute number of bytes mod 64 */
count = (ctx->bits >> 3) & 0x3F;
/* Compute number of bytes mod 64 */
count = (ctx->bits >> 3) & 0x3F;
@@
-112,31
+125,35
@@
uint8_t* MD5_final(struct Hash *h)
count = 64 - 1 - count;
/* Pad out to 56 mod 64 */
count = 64 - 1 - count;
/* Pad out to 56 mod 64 */
- if (count < 8) {
+ if (count < 8)
+ {
/* Two lots of padding: Pad the first block to 64 bytes */
memset(p, 0, count);
/* Two lots of padding: Pad the first block to 64 bytes */
memset(p, 0, count);
- byteReverse(
(uint32_t*)ctx->in
, 16);
- MD5Transform(ctx->buf,
(uint32_t *) ctx->in
);
+ byteReverse(
aligned_ptr
, 16);
+ MD5Transform(ctx->buf,
aligned_ptr
);
/* Now fill the next block with 56 bytes */
memset(ctx->in, 0, 56);
/* Now fill the next block with 56 bytes */
memset(ctx->in, 0, 56);
- } else {
+ }
+ else
+ {
/* Pad block to 56 bytes */
memset(p, 0, count - 8);
}
/* Pad block to 56 bytes */
memset(p, 0, count - 8);
}
- byteReverse((uint32_t*)ctx->in, 14);
+
+ byteReverse(aligned_ptr, 14);
/* Append length in bits and transform */
/* Append length in bits and transform */
-
((uint32_t*) ctx->in)
[14] = (uint32_t)ctx->bits;
-
((uint32_t*) ctx->in)
[15] = (uint32_t)(ctx->bits >> 32);
+
aligned_ptr
[14] = (uint32_t)ctx->bits;
+
aligned_ptr
[15] = (uint32_t)(ctx->bits >> 32);
- MD5Transform(ctx->buf,
(uint32_t *) ctx->in
);
+ MD5Transform(ctx->buf,
aligned_ptr
);
byteReverse((uint32_t*)ctx->buf, 4);
PURGE(ctx->in);
PURGE(ctx->bits);
byteReverse((uint32_t*)ctx->buf, 4);
PURGE(ctx->in);
PURGE(ctx->bits);
- return ctx->buf;
+ return
(uint8_t *)
ctx->buf;
}
}
@@
-248,5
+265,5
@@
void MD5_init(MD5_Context *ctx)
ctx->h.update = MD5_update;
ctx->h.final = MD5_final;
ctx->h.digest_len = 16;
ctx->h.update = MD5_update;
ctx->h.final = MD5_final;
ctx->h.digest_len = 16;
- ctx->h.block_len =
16
;
+ ctx->h.block_len =
64
;
}
}