Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Raspberry pi support #8

Open
wants to merge 34 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
3a02a06
added RPi support
spaniakos Dec 1, 2014
322e146
new readme
spaniakos Dec 1, 2014
0f59231
Update README.md
spaniakos Dec 1, 2014
7a86f62
Update README.md
spaniakos Dec 1, 2014
28f961e
Update Makefile
spaniakos Dec 1, 2014
47316ca
Update Makefile
spaniakos Dec 1, 2014
9ae9131
char >> const char
spaniakos Dec 15, 2014
33b717a
Config file + example Changes
spaniakos Dec 17, 2014
77a19ca
added time measurement
spaniakos Jan 12, 2015
809513e
RPi fixes
spaniakos Jan 12, 2015
1fbd58c
Merge remote-tracking branch 'upstream/master'
spaniakos Jan 13, 2015
e140f13
Added
spaniakos Jan 18, 2015
8aa8b22
added md5 and hmac
spaniakos Jan 18, 2015
8e1c6a6
Update MD5_config.h
spaniakos Jan 18, 2015
8c1dc7c
added md5 and hmac md5
spaniakos Jan 18, 2015
2437a0a
Update MD5_Hash.ino
spaniakos Jan 18, 2015
d7282d6
Update MD5_Hash.cpp
spaniakos Jan 18, 2015
c31d425
Update README.md
spaniakos Jan 18, 2015
b2bba9c
added md5 and hmac
spaniakos Jan 18, 2015
3c44a93
Update MD5.cpp
spaniakos Jan 18, 2015
f7ec99f
Update MD5.cpp
spaniakos Jan 18, 2015
59eec54
Hmac added and tested NO RPI YET
spaniakos Jan 20, 2015
4f186a9
examples hmac
spaniakos Jan 21, 2015
dcdc915
HMAC md5
spaniakos Jan 22, 2015
cdd4e30
better clean
spaniakos Jan 22, 2015
ddbcc1c
Hmac update
spaniakos Jan 22, 2015
34c0e78
Readme update
spaniakos Jan 22, 2015
449a729
changes for Documenation
spaniakos Jan 23, 2015
d0c08d0
added examples_Rpi dirR
spaniakos Jan 23, 2015
5e5b0c5
changes
spaniakos Jan 23, 2015
d1f3346
Update README.md
spaniakos Jan 26, 2015
e0380f8
Update HMAC_MD5.ino
jwd83 Jan 10, 2018
36ad8a2
Merge pull request #2 from jared0x90/patch-1
spaniakos Jan 11, 2018
1373f86
LICENSE.txt
spaniakos Apr 12, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2,372 changes: 2,372 additions & 0 deletions Doxyfile

Large diffs are not rendered by default.

661 changes: 661 additions & 0 deletions LICENSE.txt

Large diffs are not rendered by default.

102 changes: 99 additions & 3 deletions MD5.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,13 +278,109 @@ void MD5::MD5Final(unsigned char *result, void *ctxBuf)

memset(ctx, 0, sizeof(*ctx));
}
unsigned char* MD5::make_hash(char *arg)
unsigned char* MD5::make_hash(const void *arg)
{
MD5_CTX context;
unsigned char * hash = (unsigned char *) malloc(16);
unsigned char * hash = (unsigned char *) malloc(BLOCK_SIZE);
MD5Init(&context);
MD5Update(&context, arg, strlen(arg));
MD5Update(&context, arg, strlen((char*)arg));
MD5Final(hash, &context);
return hash;
}

char* MD5::md5(const void *arg){
return make_digest(make_hash(arg), BLOCK_SIZE);
}

void MD5::hmac_md5(const void *text, int text_len,void *key, int key_len, unsigned char *digest){
MD5_CTX context;
unsigned char k_ipad[65];
unsigned char k_opad[65];
unsigned char tk[BLOCK_SIZE];
int i;

if (key_len > 64){
MD5_CTX tctx;
MD5Init(&tctx);
MD5Update(&tctx, key, key_len);
MD5Final(tk,&tctx);

key = tk;
key_len= 16;
}

memset( k_ipad, 0, sizeof(k_ipad));
memset( k_opad, 0, sizeof(k_opad));
memcpy( k_ipad, key, key_len);
memcpy( k_opad, key, key_len);

for(i = 0; i < 64; i++){
k_ipad[i] ^= HMAC_IPAD;
k_opad[i] ^= HMAC_OPAD;
}

//inner
MD5Init(&context);
MD5Update(&context, k_ipad, 64);
MD5Update(&context, text, text_len);
MD5Final(digest, &context);

//outer
MD5Init(&context);
MD5Update(&context, k_opad, 64);
MD5Update(&context, digest, 16);
MD5Final(digest,&context);
}

char* MD5::hmac_md5(const void *text, int text_len,void *key, int key_len){
unsigned char digest[17];
digest[16] = 0x00;
MD5_CTX context;
unsigned char k_ipad[65];
unsigned char k_opad[65];
unsigned char tk[BLOCK_SIZE];
int i;

if (key_len > 64){
MD5_CTX tctx;
MD5Init(&tctx);
MD5Update(&tctx, key, key_len);
MD5Final(tk,&tctx);

key = tk;
key_len= 16;
}

memset( k_ipad, 0, sizeof(k_ipad));
memset( k_opad, 0, sizeof(k_opad));
memcpy( k_ipad, key, key_len);
memcpy( k_opad, key, key_len);

for(i = 0; i < 64; i++){
k_ipad[i] ^= HMAC_IPAD;
k_opad[i] ^= HMAC_OPAD;
}

//inner
MD5Init(&context);
MD5Update(&context, k_ipad, 64);
MD5Update(&context, text, text_len);
MD5Final(digest, &context);

//outer
MD5Init(&context);
MD5Update(&context, k_opad, 64);
MD5Update(&context, digest, 16);
MD5Final(digest,&context);

return make_digest(digest,BLOCK_SIZE);
}

/******************************************************************************/
#if defined(MD5_LINUX)
double MD5::millis(){
gettimeofday(&tv, NULL);
return (tv.tv_sec + 0.000001 * tv.tv_usec);
}
#endif

229 changes: 222 additions & 7 deletions MD5.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
#ifndef MD5_h
#define MD5_h

#include "Arduino.h"
/**
* @file MD5.h
*
* Class declaration for MD5 and helper enums
*/

#include "MD5_config.h"
/*
* This is an OpenSSL-compatible implementation of the RSA Data Security,
* Inc. MD5 Message-Digest Algorithm (RFC 1321).
Expand All @@ -25,27 +30,237 @@
* <[email protected]>
*/

#include <string.h>

typedef unsigned long MD5_u32plus;

typedef struct {
MD5_u32plus lo, hi;
MD5_u32plus a, b, c, d;
unsigned char buffer[64];
MD5_u32plus block[16];
} MD5_CTX;
} MD5_CTX;/**< MD5 context */

class MD5
{
public:
/**
* class constructor.
* Does nothing.
*/
MD5();
static unsigned char* make_hash(char *arg);
static char* make_digest(const unsigned char *digest, int len);

/** Created the MD5 hash from a string of characters on hex encoding.
*
* It is one of the main function of this class.
* Gets an pointer to a string, and hash it to MD5.
*
* @param *arg pointer to the string or array of characters.
* @return a pointer containing the MD5digest
*
*/
unsigned char* make_hash(const void *arg);

/** Converts a digest to a string.
*
* In order for tedigest to be readable and printed easyly, we need to conver it.
*
* @param *digest pointer to the array that holds the digest
* @param len integer defining the lengs of the output, usually 16 for MD5
* @return poiner to the string that holds the String of the converted digest
*/
char* make_digest(const unsigned char *digest, int len);

/** Automation function.
* Gets a pointer to sequence of chars,
* Then Hashes it, and converts it to a readable form,
*
* @param *arg pointer to the string that will be hashed.
* @return pointer to the string that holds the string of the converted digest.
*/
char* md5(const void *arg);

/** Main function of the HMAC-MD5.
* gets the key and the text, and creates the HMAC-MD5 digest function.
* in order to be pronted, it is required for the make_digest function to be called.
* @code make_digest(digest,BLOCK_SIZE); @endcode
*
* @param *text pointer to the text that will be hashed.
* @param text_len integet value of the length of the text.
* @param *key pointer to the key that will be used in the HMAC process.
* @param key_len integer value of the key length.
* @param *digest pointer to the array that will hold the digest of this process
* @return the digest in the memory block that the *digest is pointing.
*/
void hmac_md5(const void *text, int text_len,void *key, int key_len, unsigned char *digest);

/** Main function of the HMAC-MD5.
* gets the key and the text, and creates the HMAC-MD5 digest function in a readable format.
*
* @param *text pointer to the text that will be hashed.
* @param text_len integet value of the length of the text.
* @param *key pointer to the key that will be used in the HMAC process.
* @param key_len integer value of the key length.
* @return pointer that points to the digest in a readable format.
*/
char* hmac_md5(const void *text, int text_len,void *key, int key_len);

/** This processes one or more 64-byte data blocks, but does NOT update the bit counters.
* There are no alignment requirements.
*
* @param *ctxBuf the ctx buffer that will be used
* @param *data pointer to the data that will be processed
* @param size size_t type, that hold the size
*/
static const void *body(void *ctxBuf, const void *data, size_t size);

/** Initialized the MD5 hashing process.
* this function must be called before MD5Update or MD5Final
*
* @param *ctxBuf the ctx buffer that will be used
*/
static void MD5Init(void *ctxBuf);


/** MD5Final finilized the Hashing process and creates the diggest.
* This function must be called after MD5Init and MD5Update
* @param *result pointer that will hold the digest.
* @param *ctxBuf the ctx buffer that will be used
* @return no return, the result is storesin the *result pointer
*/
static void MD5Final(unsigned char *result, void *ctxBuf);

/** MD5Update adds data in the buffers.
* This function can be used as many times as we want in the hashing process.
* Examples on hmac_md5 functions.
*
* @param *ctxBuf the ctx buffer that will be used
* @param *data the actual data that will be used in the hashing process.
* @param size size_t type, indicated the side of the data pointer.
*/
static void MD5Update(void *ctxBuf, const void *data, size_t size);
#if defined(MD5_LINUX)
/**
* used in linux in order to retrieve the time in milliseconds.
*
* @return returns the milliseconds in a double format.
*/
double millis();
#endif
private:
#if defined(MD5_LINUX)
timeval tv;/**< holds the time value on linux */
#endif
};
extern MD5 hashMD5;
#endif

/**
* @example MD5_Hash.ino
* <b>For Arduino</b><br>
* <b>Updated: spaniakos 2015 </b><br>
*
* This is en example of how to use my MD5 library.<br />
* It provides two easy-to-use methods, one for generating the MD5 hash, and the second
* one to generate the hex encoding of the hash, which is frequently used.
*
* <b>UPDATE:<b> Now only md5 function is used that return only the hex encoding directlly.<br />
*/

/**
* @example MD5_Hash.cpp
* <b>For Rasberry Pi</b><br>
* <b>Updated: spaniakos 2015 </b><br>
*
* This is en example of how to use MD5 library. <br />
* It provides two easy-to-use methods, one for generating the MD5 hash, and the second
* one to generate the hex encoding of the hash, which is frequently used.
*
* <b>UPDATE:<b> Now only md5 function is used that return only the hex encoding directlly.<br />
*/

/**
* @example HMAC_MD5.ino
* <b>For Arduino</b><br>
* <b>Updated: spaniakos 2015 </b><br>
*
* This is an example of how to use HMAC of this MD5 library.<br />
* The text and keys can be either in HEX or String format.<br />
* The examples are from the RFC2202 Test Vectors
*/

/**
* @example HMAC_MD5.cpp
* <b>For Rasberry pi</b><br>
* <b>Updated: spaniakos 2015 </b><br>
*
* This is an example of how to use HMAC of this MD5 library.<br />
* The text and keys can be either in HEX or String format.<br />
* The examples are from the RFC2202 Test Vectors
*/

#endif
/**
* @mainpage MD5 and HMAC-MD5 library for Arduino and Raspberry pi.
*
* @section Goals Design Goals
*
* This library is designed to be...
* @li Fast and efficient
* @li Able to effectivelly hash any size of string
* @li Able to use any format of key for HMAC (hex or string)
* @li Easy for the user to use in his programms
* @li to hash using MD5
* @li to hash using HMAC-MD5
*
* @section Acknowlegments Acknowlegments
* This is an MD5 library for the Arduino, based on tzikis's MD5 library, which you can find <a href= "https://github.com/tzikis/arduino">here:</a>.<br />
* Tzikis library was based on scottmac`s library, which you can find <a href="https://github.com/scottmac/arduino">here:</a><br />
*
* @section Installation Installation
* <h3>Arduino</h3>
* Create a folder named _MD5_ in the _libraries_ folder inside your Arduino sketch folder. If the
* libraries folder doesn't exist, create it. Then copy everything inside. (re)launch the Arduino IDE.<br />
* You're done. Time for a mojito
*
* <h3>Raspberry pi</h3>
* <b>install</b><br /><br />
*
* sudo make install<br />
* cd examples_Rpi<br />
* make<br /><br />
*
* <b>What to do after changes to the library</b><br /><br />
* sudo make clean<br />
* sudo make install<br />
* cd examples_Rpi<br />
* make clean<br />
* make<br /><br />
* <b>What to do after changes to a sketch</b><br /><br />
* cd examples_Rpi<br />
* make <sketch><br /><br />
* or <br />
* make clean<br />
* make<br /><br /><br />
* <b>How to start a sketch</b><br /><br />
* cd examples_Rpi<br />
* sudo ./<sketch><br /><br />
*
* @section News News
*
* If issues are discovered with the documentation, please report them <a href="https://github.com/spaniakos/spaniakos.github.io/issues"> here</a>
* @section Useful Useful References
*
* Please refer to:
*
* @li <a href="http://spaniakos.github.io/ArduinoMD5/classMD5.html"><b>MD5</b> Class Documentation</a>
* @li <a href="https://github.com/spaniakos/ArduinoMD5/archive/master.zip"><b>Download</b></a>
* @li <a href="https://github.com/spaniakos/ArduinoMD5/"><b>Source Code</b></a>
* @li <a href="http://spaniakos.github.io/">All spaniakos Documentation Main Page</a>
*
* @section Board_Support Board Support
*
* Most standard Arduino based boards are supported:
* - Arduino
* - Intel Galileo support
* - Raspberry Pi Support
*
* - The library has not been tested to other boards, but it should suppport ATMega 328 based boards,Mega Boards,Arduino Due,ATTiny board
*/
23 changes: 23 additions & 0 deletions MD5_config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef MD5_config_h
#define MD5_config_h

#define HMAC_IPAD 0x36
#define HMAC_OPAD 0x5c
#define BLOCK_SIZE 16

#if (defined(__linux) || defined(linux)) && !defined(__ARDUINO_X86__)

#define MD5_LINUX

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <unistd.h>
#else
#include <Arduino.h>
#endif

#include <string.h>
#endif
Loading