[136] | 1 | /*
|
---|
| 2 | ###############################################################################
|
---|
| 3 | #
|
---|
| 4 | # Temboo Arduino library
|
---|
| 5 | #
|
---|
| 6 | # Copyright 2014, Temboo Inc.
|
---|
| 7 | #
|
---|
| 8 | # Licensed under the Apache License, Version 2.0 (the "License");
|
---|
| 9 | # you may not use this file except in compliance with the License.
|
---|
| 10 | # You may obtain a copy of the License at
|
---|
| 11 | #
|
---|
| 12 | # http://www.apache.org/licenses/LICENSE-2.0
|
---|
| 13 | #
|
---|
| 14 | # Unless required by applicable law or agreed to in writing,
|
---|
| 15 | # software distributed under the License is distributed on an
|
---|
| 16 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
---|
| 17 | # either express or implied. See the License for the specific
|
---|
| 18 | # language governing permissions and limitations under the License.
|
---|
| 19 | #
|
---|
| 20 | ###############################################################################
|
---|
| 21 | */
|
---|
| 22 |
|
---|
| 23 | #ifndef TMBMD5_H_
|
---|
| 24 | #define TMBMD5_H_
|
---|
| 25 |
|
---|
| 26 | #include <stdint.h>
|
---|
| 27 | #include "TembooGlobal.h"
|
---|
| 28 |
|
---|
| 29 | #define MD5_HASH_SIZE_BITS (128)
|
---|
| 30 | #define MD5_HASH_SIZE_BYTES (MD5_HASH_SIZE_BITS/8)
|
---|
| 31 |
|
---|
| 32 | #define MD5_BLOCK_SIZE_BITS (512)
|
---|
| 33 | #define MD5_BLOCK_SIZE_BYTES (MD5_BLOCK_SIZE_BITS/8)
|
---|
| 34 |
|
---|
| 35 |
|
---|
| 36 | #define ROL(x, y) ( (((uint32_t)(x)<<(uint32_t)((y)&31)) | (((uint32_t)(x)&0xFFFFFFFFUL)>>(uint32_t)(32-((y)&31)))) & 0xFFFFFFFFUL)
|
---|
| 37 | class MD5 {
|
---|
| 38 |
|
---|
| 39 | public:
|
---|
| 40 | MD5();
|
---|
| 41 | void init();
|
---|
| 42 | int process(const uint8_t* in, uint32_t inlen);
|
---|
| 43 | int finish(uint8_t* hash);
|
---|
| 44 | enum {
|
---|
| 45 | MD5_OK = 0,
|
---|
| 46 | MD5_ERROR,
|
---|
| 47 | MD5_INVALID_ARG,
|
---|
| 48 | MD5_FAIL_TESTVECTOR
|
---|
| 49 | };
|
---|
| 50 |
|
---|
| 51 | private:
|
---|
| 52 | uint64_t m_msgLengthBits;
|
---|
| 53 | uint32_t m_state[4];
|
---|
| 54 | uint32_t m_bufLength;
|
---|
| 55 | uint8_t m_buf[64];
|
---|
| 56 |
|
---|
| 57 | int compress(const uint8_t* buf);
|
---|
| 58 |
|
---|
| 59 | inline uint32_t F_(uint32_t x, uint32_t y, uint32_t z) { return (z ^ (x & (y ^ z))); }
|
---|
| 60 | inline uint32_t G_(uint32_t x, uint32_t y, uint32_t z) { return (y ^ (z & (y ^ x))); }
|
---|
| 61 | inline uint32_t H_(uint32_t x, uint32_t y, uint32_t z) { return (x^y^z); }
|
---|
| 62 | inline uint32_t I_(uint32_t x, uint32_t y, uint32_t z) { return (y^(x|(~z))); }
|
---|
| 63 |
|
---|
| 64 |
|
---|
| 65 | inline void FF(uint32_t* a, uint32_t b, uint32_t c, uint32_t d, uint32_t M, uint32_t s, uint32_t t) { *a = (*a + F_(b,c,d) + M + t); *a = ROL(*a, s) + b; }
|
---|
| 66 | inline void GG(uint32_t* a, uint32_t b, uint32_t c, uint32_t d, uint32_t M, uint32_t s, uint32_t t) { *a = (*a + G_(b,c,d) + M + t); *a = ROL(*a, s) + b; }
|
---|
| 67 | inline void HH(uint32_t* a, uint32_t b, uint32_t c, uint32_t d, uint32_t M, uint32_t s, uint32_t t) { *a = (*a + H_(b,c,d) + M + t); *a = ROL(*a, s) + b; }
|
---|
| 68 | inline void II(uint32_t* a, uint32_t b, uint32_t c, uint32_t d, uint32_t M, uint32_t s, uint32_t t) { *a = (*a + I_(b,c,d) + M + t); *a = ROL(*a, s) + b; }
|
---|
| 69 |
|
---|
| 70 | };
|
---|
| 71 |
|
---|
| 72 | #endif
|
---|