diff --git a/examples/MD5/MD5.ino b/examples/MD5/MD5.ino new file mode 100644 index 0000000..af650ca --- /dev/null +++ b/examples/MD5/MD5.ino @@ -0,0 +1,64 @@ +/* + ArduinoBearSSL MD5 + + This sketch demonstrates how to create a MD5 hash + for an input string. + + This example code is in the public domain. +*/ + +#include + +void setup() { + Serial.begin(9600); + while (!Serial); + + // expect 9e107d9d372bb6826bd81d3542a419d6 + printMD5("The quick brown fox jumps over the lazy dog"); + + // expect 80070713463e7749b90c2dc24911e275 + printHMACMD5("key", "The quick brown fox jumps over the lazy dog"); +} + +void loop() { +} + +void printMD5(const char* str) { + Serial.print("MD5 of '"); + Serial.print(str); + Serial.print("' is 0x"); + + MD5.beginHash(); + MD5.print(str); + MD5.endHash(); + + printResult(); +} + +void printHMACMD5(const char* secret, const char* str) { + Serial.print("HMAC-MD5 of '"); + Serial.print(str); + Serial.print("' with secret '"); + Serial.print(secret); + Serial.print("' is 0x"); + + MD5.beginHmac(secret); + MD5.print(str); + MD5.endHmac(); + + printResult(); +} + +void printResult() +{ + while (MD5.available()) { + byte b = MD5.read(); + + if (b < 16) { + Serial.print("0"); + } + + Serial.print(b, HEX); + } + Serial.println(); +} diff --git a/src/ArduinoBearSSL.h b/src/ArduinoBearSSL.h index c2f9640..5374331 100644 --- a/src/ArduinoBearSSL.h +++ b/src/ArduinoBearSSL.h @@ -28,6 +28,7 @@ #include "BearSSLClient.h" #include "SHA1.h" #include "SHA256.h" +#include "MD5.h" class ArduinoBearSSLClass { public: diff --git a/src/MD5.cpp b/src/MD5.cpp new file mode 100644 index 0000000..6a511c2 --- /dev/null +++ b/src/MD5.cpp @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2019 Arduino SA. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "MD5.h" + +MD5Class::MD5Class() : + SHAClass(MD5_BLOCK_SIZE, MD5_DIGEST_SIZE) +{ +} + +MD5Class::~MD5Class() +{ +} + +int MD5Class::begin() +{ + br_md5_init(&_ctx); + + return 1; +} + +int MD5Class::update(const uint8_t *buffer, size_t size) +{ + br_md5_update(&_ctx, buffer, size); + + return 1; +} + +int MD5Class::end(uint8_t *digest) +{ + br_md5_out(&_ctx, digest); + + return 1; +} + +MD5Class MD5; diff --git a/src/MD5.h b/src/MD5.h new file mode 100644 index 0000000..72ff544 --- /dev/null +++ b/src/MD5.h @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2019 Arduino SA. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#ifndef MD5_H +#define MD5_H + +#include + +#include "SHA.h" + +#define MD5_BLOCK_SIZE 64 +#define MD5_DIGEST_SIZE 16 + +class MD5Class: public SHAClass { + +public: + MD5Class(); + virtual ~MD5Class(); + +protected: + virtual int begin(); + virtual int update(const uint8_t *buffer, size_t size); + virtual int end(uint8_t *digest); + +private: + br_md5_context _ctx; +}; + +extern MD5Class MD5; + +#endif