nagendra modadugu | 793cf59 | 2019-10-09 14:07:05 -0700 | [diff] [blame] | 1 | // Copyright 2016 Google Inc. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | #include "cryptoc/md5.h" |
| 15 | |
| 16 | #include <string.h> |
| 17 | #include <stdint.h> |
| 18 | |
| 19 | #define rol(bits, value) (((value) << (bits)) | ((value) >> (32 - (bits)))) |
| 20 | |
| 21 | static const char Kr[64] = |
| 22 | { |
| 23 | 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, |
| 24 | 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, |
| 25 | 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, |
| 26 | 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21 |
| 27 | }; |
| 28 | |
| 29 | static const uint32_t KK[64] = |
| 30 | { |
| 31 | 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, |
| 32 | 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501, |
| 33 | 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be, |
| 34 | 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821, |
| 35 | 0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa, |
| 36 | 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8, |
| 37 | 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed, |
| 38 | 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a, |
| 39 | 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c, |
| 40 | 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70, |
| 41 | 0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05, |
| 42 | 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665, |
| 43 | 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039, |
| 44 | 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1, |
| 45 | 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1, |
| 46 | 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391 |
| 47 | }; |
| 48 | |
| 49 | static void MD5_Transform(LITE_MD5_CTX* ctx) { |
| 50 | uint32_t W[64]; |
| 51 | uint32_t A, B, C, D; |
| 52 | uint8_t* p = ctx->buf; |
| 53 | int t; |
| 54 | |
| 55 | for(t = 0; t < 16; ++t) { |
| 56 | uint32_t tmp = *p++; |
| 57 | tmp |= (uint32_t)*p++ << 8u; |
| 58 | tmp |= (uint32_t)*p++ << 16u; |
| 59 | tmp |= (uint32_t)*p++ << 24u; |
| 60 | W[t] = tmp; |
| 61 | } |
| 62 | |
| 63 | A = ctx->state[0]; |
| 64 | B = ctx->state[1]; |
| 65 | C = ctx->state[2]; |
| 66 | D = ctx->state[3]; |
| 67 | |
| 68 | for(t = 0; t < 64; t++) { |
| 69 | uint32_t f, tmp; |
| 70 | int g; |
| 71 | |
| 72 | if (t < 16) { |
| 73 | f = (D^(B&(C^D))); |
| 74 | g = t; |
| 75 | } else if ( t < 32) { |
| 76 | f = (C^(D&(B^C))); |
| 77 | g = (5*t + 1) & 15; |
| 78 | } else if ( t < 48) { |
| 79 | f = (B^C^D); |
| 80 | g = (3*t + 5) & 15; |
| 81 | } else { |
| 82 | f = (C^(B|(~D))); |
| 83 | g = (7*t) & 15; |
| 84 | } |
| 85 | |
| 86 | tmp = D; |
| 87 | D = C; |
| 88 | C = B; |
| 89 | B = B + rol(Kr[t], (A+f+KK[t]+W[g])); |
| 90 | A = tmp; |
| 91 | } |
| 92 | |
| 93 | ctx->state[0] += A; |
| 94 | ctx->state[1] += B; |
| 95 | ctx->state[2] += C; |
| 96 | ctx->state[3] += D; |
| 97 | } |
| 98 | |
| 99 | static const HASH_VTAB MD5_VTAB = { |
| 100 | MD5_init, |
| 101 | MD5_update, |
| 102 | MD5_final, |
| 103 | MD5_hash, |
| 104 | MD5_DIGEST_SIZE, |
| 105 | #ifdef SHA512_SUPPORT |
| 106 | MD5_BLOCK_SIZE, |
| 107 | #endif |
| 108 | }; |
| 109 | |
| 110 | void MD5_init(LITE_MD5_CTX* ctx) { |
| 111 | ctx->f = &MD5_VTAB; |
| 112 | ctx->state[0] = 0x67452301; |
| 113 | ctx->state[1] = 0xEFCDAB89; |
| 114 | ctx->state[2] = 0x98BADCFE; |
| 115 | ctx->state[3] = 0x10325476; |
| 116 | ctx->count = 0; |
| 117 | } |
| 118 | |
| 119 | |
| 120 | void MD5_update(LITE_MD5_CTX* ctx, const void* data, size_t len) { |
| 121 | unsigned int i = (unsigned int)(ctx->count & 63); |
| 122 | const uint8_t* p = (const uint8_t*)data; |
| 123 | |
| 124 | ctx->count += len; |
| 125 | |
| 126 | while (len--) { |
| 127 | ctx->buf[i++] = *p++; |
| 128 | if (i == 64) { |
| 129 | MD5_Transform(ctx); |
| 130 | i = 0; |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | |
| 136 | const uint8_t* MD5_final(LITE_MD5_CTX* ctx) { |
| 137 | uint8_t* p = ctx->buf; |
| 138 | uint64_t cnt = ctx->count * 8; |
| 139 | int i; |
| 140 | |
| 141 | MD5_update(ctx, (uint8_t*)"\x80", 1); |
| 142 | while ((ctx->count & 63) != 56) { |
| 143 | MD5_update(ctx, (uint8_t*)"\0", 1); |
| 144 | } |
| 145 | for (i = 0; i < 4; ++i) { |
| 146 | uint8_t tmp = (uint8_t)(((uint32_t)cnt) >> (i * 8)); |
| 147 | MD5_update(ctx, &tmp, 1); |
| 148 | } |
| 149 | cnt >>= 32; |
| 150 | for (i = 0; i < 4; ++i) { |
| 151 | uint8_t tmp = (uint8_t)(((uint32_t)cnt) >> (i * 8)); |
| 152 | MD5_update(ctx, &tmp, 1); |
| 153 | } |
| 154 | |
| 155 | for (i = 0; i < 4; i++) { |
| 156 | uint32_t tmp = ctx->state[i]; |
| 157 | *p++ = tmp; |
| 158 | *p++ = tmp >> 8; |
| 159 | *p++ = tmp >> 16; |
| 160 | *p++ = tmp >> 24; |
| 161 | } |
| 162 | |
| 163 | return ctx->buf; |
| 164 | } |
| 165 | |
| 166 | |
| 167 | /* Convenience function */ |
| 168 | const uint8_t* MD5_hash(const void* data, size_t len, uint8_t* digest) { |
| 169 | LITE_MD5_CTX ctx; |
| 170 | MD5_init(&ctx); |
| 171 | MD5_update(&ctx, data, len); |
| 172 | memcpy(digest, MD5_final(&ctx), MD5_DIGEST_SIZE); |
| 173 | return digest; |
| 174 | } |