Tizen 2.0 Release
[external/nettle.git] / md5.c
1 /* md5.c
2  *
3  * The MD5 hash function, described in RFC 1321.
4  */
5
6 /* nettle, low-level cryptographics library
7  *
8  * Copyright (C) 2001 Niels Möller
9  *  
10  * The nettle library is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 of the License, or (at your
13  * option) any later version.
14  * 
15  * The nettle library is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
18  * License for more details.
19  * 
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with the nettle library; see the file COPYING.LIB.  If not, write to
22  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
23  * MA 02111-1307, USA.
24  */
25
26 /* Based on public domain code hacked by Colin Plumb, Andrew Kuchling, and
27  * Niels Möller. */
28
29 #if HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <assert.h>
34 #include <string.h>
35
36 #include "md5.h"
37
38 #include "macros.h"
39
40 static void
41 md5_final(struct md5_ctx *ctx);
42
43 void
44 md5_init(struct md5_ctx *ctx)
45 {
46   ctx->digest[0] = 0x67452301;
47   ctx->digest[1] = 0xefcdab89;
48   ctx->digest[2] = 0x98badcfe;
49   ctx->digest[3] = 0x10325476;
50   
51   ctx->count_l = ctx->count_h = 0;
52   ctx->index = 0;
53 }
54
55 #define MD5_INCR(ctx) ((ctx)->count_h += !++(ctx)->count_l)
56
57 void
58 md5_update(struct md5_ctx *ctx,
59            unsigned length,
60            const uint8_t *data)
61 {
62   if (ctx->index)
63     {
64       /* Try to fill partial block */
65       unsigned left = MD5_DATA_SIZE - ctx->index;
66       if (length < left)
67         {
68           memcpy(ctx->block + ctx->index, data, length);
69           ctx->index += length;
70           return; /* Finished */
71         }
72       else
73         {
74           memcpy(ctx->block + ctx->index, data, left);
75
76           _nettle_md5_compress(ctx->digest, ctx->block);
77           MD5_INCR(ctx);
78           
79           data += left;
80           length -= left;
81         }
82     }
83   while (length >= MD5_DATA_SIZE)
84     {
85       _nettle_md5_compress(ctx->digest, data);
86       MD5_INCR(ctx);
87
88       data += MD5_DATA_SIZE;
89       length -= MD5_DATA_SIZE;
90     }
91   if ((ctx->index = length))     /* This assignment is intended */
92     /* Buffer leftovers */
93     memcpy(ctx->block, data, length);
94 }
95
96 void
97 md5_digest(struct md5_ctx *ctx,
98            unsigned length,
99            uint8_t *digest)
100 {
101   unsigned i;
102   unsigned words;
103   unsigned leftover;
104   
105   assert(length <= MD5_DIGEST_SIZE);
106
107   md5_final(ctx);
108   
109   words = length / 4;
110   leftover = length % 4;
111   
112   /* Little endian order */
113   for (i = 0; i < words; i++, digest += 4)
114     LE_WRITE_UINT32(digest, ctx->digest[i]);
115
116   if (leftover)
117     {
118       uint32_t word;
119       unsigned j;
120
121       assert(i < _MD5_DIGEST_LENGTH);
122       
123       /* Still least significant byte first. */
124       for (word = ctx->digest[i], j = 0; j < leftover;
125            j++, word >>= 8)
126         digest[j] = word & 0xff;
127     }
128   md5_init(ctx);
129 }
130
131 /* Final wrapup - pad to MD5_DATA_SIZE-byte boundary with the bit
132  * pattern 1 0* (64-bit count of bits processed, LSB-first) */
133
134 static void
135 md5_final(struct md5_ctx *ctx)
136 {
137   uint32_t bitcount_high;
138   uint32_t bitcount_low;
139   unsigned i;
140   
141   i = ctx->index;
142
143   /* Set the first char of padding to 0x80. This is safe since there
144    * is always at least one byte free */
145   assert(i < MD5_DATA_SIZE);
146   ctx->block[i++] = 0x80;
147
148   if (i > (MD5_DATA_SIZE - 8))
149     {
150       /* No room for length in this block. Process it and
151          pad with another one */
152       memset(ctx->block + i, 0, MD5_DATA_SIZE - i);
153       
154       _nettle_md5_compress(ctx->digest, ctx->block);
155       i = 0;
156     }
157   if (i < (MD5_DATA_SIZE - 8))
158     memset(ctx->block + i, 0, (MD5_DATA_SIZE - 8) - i);
159     
160   /* There are 512 = 2^9 bits in one block 
161    * Little-endian order => Least significant word first */
162
163   bitcount_low = (ctx->count_l << 9) | (ctx->index << 3);
164   bitcount_high = (ctx->count_h << 9) | (ctx->count_l >> 23);
165   LE_WRITE_UINT32(ctx->block + (MD5_DATA_SIZE - 8), bitcount_low);
166   LE_WRITE_UINT32(ctx->block + (MD5_DATA_SIZE - 4), bitcount_high);
167   
168   _nettle_md5_compress(ctx->digest, ctx->block);
169 }