md5.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /* md5.c */
  2. /*
  3. This file is part of the ARM-Crypto-Lib.
  4. Copyright (C) 2006-2010 Daniel Otte (daniel.otte@rub.de)
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. /*
  17. * \file md5.c
  18. * \author Daniel Otte
  19. * \date 2006-07-31
  20. * \license GPLv3 or later
  21. * \brief Implementation of the MD5 hash algorithm as described in RFC 1321
  22. *
  23. */
  24. #include <crypto/md5.h>
  25. #include <crypto/md5_sbox.h>
  26. #include "cli.h"
  27. #include <stdint.h>
  28. #include <string.h>
  29. #undef DEBUG
  30. void md5_init(md5_ctx_t *s){
  31. s->counter = 0;
  32. s->a[0] = 0x67452301;
  33. s->a[1] = 0xefcdab89;
  34. s->a[2] = 0x98badcfe;
  35. s->a[3] = 0x10325476;
  36. }
  37. static
  38. uint32_t md5_F(uint32_t x, uint32_t y, uint32_t z){
  39. return ((x&y)|((~x)&z));
  40. }
  41. static
  42. uint32_t md5_G(uint32_t x, uint32_t y, uint32_t z){
  43. return ((x&z)|((~z)&y));
  44. }
  45. static
  46. uint32_t md5_H(uint32_t x, uint32_t y, uint32_t z){
  47. return (x^y^z);
  48. }
  49. static
  50. uint32_t md5_I(uint32_t x, uint32_t y, uint32_t z){
  51. return (y ^ (x | (~z)));
  52. }
  53. typedef uint32_t md5_func_t(uint32_t, uint32_t, uint32_t);
  54. #define ROTL32(x,n) (((x)<<(n)) | ((x)>>(32-(n))))
  55. static
  56. void md5_core(uint32_t* a, void* block, uint8_t as, uint8_t s, uint8_t i, uint8_t fi){
  57. uint32_t t;
  58. md5_func_t* funcs[]={md5_F, md5_G, md5_H, md5_I};
  59. as &= 0x3;
  60. /* a = b + ((a + F(b,c,d) + X[k] + T[i]) <<< s). */
  61. #ifdef DEBUG
  62. char funcc[]={'*', '-', '+', '~'};
  63. cli_putstr("\r\n DBG: md5_core [");
  64. cli_putc(funcc[fi]);
  65. cli_hexdump(&as, 1); cli_putc(' ');
  66. cli_hexdump(&k, 1); cli_putc(' ');
  67. cli_hexdump(&s, 1); cli_putc(' ');
  68. cli_hexdump(&i, 1); cli_putc(']');
  69. #endif
  70. t = a[as] + funcs[fi](a[(as+1)&3], a[(as+2)&3], a[(as+3)&3])
  71. + *((uint32_t*)block) + md5_T[i] ;
  72. a[as]=a[(as+1)&3] + ROTL32(t, s);
  73. }
  74. void md5_nextBlock(md5_ctx_t *state, const void* block){
  75. uint32_t a[4];
  76. uint8_t m,n,i=0;
  77. /* this requires other mixed sboxes */
  78. #ifdef DEBUG
  79. cli_putstr("\r\n DBG: md5_nextBlock: block:\r\n");
  80. cli_hexdump(block, 16); cli_putstr("\r\n");
  81. cli_hexdump(block+16, 16); cli_putstr("\r\n");
  82. cli_hexdump(block+32, 16); cli_putstr("\r\n");
  83. cli_hexdump(block+48, 16); cli_putstr("\r\n");
  84. #endif
  85. a[0]=state->a[0];
  86. a[1]=state->a[1];
  87. a[2]=state->a[2];
  88. a[3]=state->a[3];
  89. /* round 1 */
  90. uint8_t s1t[]={7,12,17,22}; // 1,-1 1,4 2,-1 3,-2
  91. for(m=0;m<4;++m){
  92. for(n=0;n<4;++n){
  93. md5_core(a, &(((uint32_t*)block)[m*4+n]), 4-n, s1t[n],i++,0);
  94. }
  95. }
  96. /* round 2 */
  97. uint8_t s2t[]={5,9,14,20}; // 1,-3 1,1 2,-2 2,4
  98. for(m=0;m<4;++m){
  99. for(n=0;n<4;++n){
  100. md5_core(a, &(((uint32_t*)block)[(1+m*4+n*5)&0xf]), 4-n, s2t[n],i++,1);
  101. }
  102. }
  103. /* round 3 */
  104. uint8_t s3t[]={4,11,16,23}; // 0,4 1,3 2,0 3,-1
  105. for(m=0;m<4;++m){
  106. for(n=0;n<4;++n){
  107. md5_core(a, &(((uint32_t*)block)[(5-m*4+n*3)&0xf]), 4-n, s3t[n],i++,2);
  108. }
  109. }
  110. /* round 4 */
  111. uint8_t s4t[]={6,10,15,21}; // 1,-2 1,2 2,-1 3,-3
  112. for(m=0;m<4;++m){
  113. for(n=0;n<4;++n){
  114. md5_core(a, &(((uint32_t*)block)[(0-m*4+n*7)&0xf]), 4-n, s4t[n],i++,3);
  115. }
  116. }
  117. state->a[0] += a[0];
  118. state->a[1] += a[1];
  119. state->a[2] += a[2];
  120. state->a[3] += a[3];
  121. state->counter++;
  122. }
  123. void md5_lastBlock(md5_ctx_t *state, const void* block, uint16_t length_b){
  124. uint16_t l;
  125. union {
  126. uint8_t v8[64];
  127. uint64_t v64[ 8];
  128. } buffer;
  129. while (length_b >= 512){
  130. md5_nextBlock(state, block);
  131. length_b -= 512;
  132. block = ((uint8_t*)block) + 512/8;
  133. }
  134. memset(buffer.v8, 0, 64);
  135. memcpy(buffer.v8, block, length_b/8);
  136. /* insert padding one */
  137. l=length_b/8;
  138. if(length_b%8){
  139. uint8_t t;
  140. t = ((uint8_t*)block)[l];
  141. t |= (0x80>>(length_b%8));
  142. buffer.v8[l]=t;
  143. }else{
  144. buffer.v8[l]=0x80;
  145. }
  146. /* insert length value */
  147. if(l+sizeof(uint64_t) >= 512/8){
  148. md5_nextBlock(state, buffer.v8);
  149. state->counter--;
  150. memset(buffer.v8, 0, 64-8);
  151. }
  152. buffer.v64[7] = (state->counter * 512) + length_b;
  153. md5_nextBlock(state, buffer.v8);
  154. }
  155. void md5_ctx2hash(md5_hash_t* dest, const md5_ctx_t* state){
  156. memcpy(dest, state->a, MD5_HASH_BYTES);
  157. }
  158. void md5(md5_hash_t* dest, const void* msg, uint32_t length_b){
  159. md5_ctx_t ctx;
  160. md5_init(&ctx);
  161. while(length_b>=MD5_BLOCK_BITS){
  162. md5_nextBlock(&ctx, msg);
  163. msg = (uint8_t*)msg + MD5_BLOCK_BYTES;
  164. length_b -= MD5_BLOCK_BITS;
  165. }
  166. md5_lastBlock(&ctx, msg, length_b);
  167. md5_ctx2hash(dest, &ctx);
  168. }