Browse Source

Fix CryptoCore signatures, add SHA1 context.

Fix hash size in SHA256 signature
Fix source parameter in hash context update function to be const.
Add SHA1 hash context.
Fabio Alessandrelli 6 years ago
parent
commit
5cb41faece
2 changed files with 46 additions and 6 deletions
  1. 29 3
      core/math/crypto_core.cpp
  2. 17 3
      core/math/crypto_core.h

+ 29 - 3
core/math/crypto_core.cpp

@@ -52,7 +52,7 @@ Error CryptoCore::MD5Context::start() {
 	return ret ? FAILED : OK;
 }
 
-Error CryptoCore::MD5Context::update(uint8_t *p_src, size_t p_len) {
+Error CryptoCore::MD5Context::update(const uint8_t *p_src, size_t p_len) {
 	int ret = mbedtls_md5_update_ret((mbedtls_md5_context *)ctx, p_src, p_len);
 	return ret ? FAILED : OK;
 }
@@ -62,6 +62,32 @@ Error CryptoCore::MD5Context::finish(unsigned char r_hash[16]) {
 	return ret ? FAILED : OK;
 }
 
+// SHA1
+CryptoCore::SHA1Context::SHA1Context() {
+	ctx = memalloc(sizeof(mbedtls_sha1_context));
+	mbedtls_sha1_init((mbedtls_sha1_context *)ctx);
+}
+
+CryptoCore::SHA1Context::~SHA1Context() {
+	mbedtls_sha1_free((mbedtls_sha1_context *)ctx);
+	memfree((mbedtls_sha1_context *)ctx);
+}
+
+Error CryptoCore::SHA1Context::start() {
+	int ret = mbedtls_sha1_starts_ret((mbedtls_sha1_context *)ctx);
+	return ret ? FAILED : OK;
+}
+
+Error CryptoCore::SHA1Context::update(const uint8_t *p_src, size_t p_len) {
+	int ret = mbedtls_sha1_update_ret((mbedtls_sha1_context *)ctx, p_src, p_len);
+	return ret ? FAILED : OK;
+}
+
+Error CryptoCore::SHA1Context::finish(unsigned char r_hash[20]) {
+	int ret = mbedtls_sha1_finish_ret((mbedtls_sha1_context *)ctx, r_hash);
+	return ret ? FAILED : OK;
+}
+
 // SHA256
 CryptoCore::SHA256Context::SHA256Context() {
 	ctx = memalloc(sizeof(mbedtls_sha256_context));
@@ -78,12 +104,12 @@ Error CryptoCore::SHA256Context::start() {
 	return ret ? FAILED : OK;
 }
 
-Error CryptoCore::SHA256Context::update(uint8_t *p_src, size_t p_len) {
+Error CryptoCore::SHA256Context::update(const uint8_t *p_src, size_t p_len) {
 	int ret = mbedtls_sha256_update_ret((mbedtls_sha256_context *)ctx, p_src, p_len);
 	return ret ? FAILED : OK;
 }
 
-Error CryptoCore::SHA256Context::finish(unsigned char r_hash[16]) {
+Error CryptoCore::SHA256Context::finish(unsigned char r_hash[32]) {
 	int ret = mbedtls_sha256_finish_ret((mbedtls_sha256_context *)ctx, r_hash);
 	return ret ? FAILED : OK;
 }

+ 17 - 3
core/math/crypto_core.h

@@ -46,10 +46,24 @@ public:
 		~MD5Context();
 
 		Error start();
-		Error update(uint8_t *p_src, size_t p_len);
+		Error update(const uint8_t *p_src, size_t p_len);
 		Error finish(unsigned char r_hash[16]);
 	};
 
+	class SHA1Context {
+
+	private:
+		void *ctx; // To include, or not to include...
+
+	public:
+		SHA1Context();
+		~SHA1Context();
+
+		Error start();
+		Error update(const uint8_t *p_src, size_t p_len);
+		Error finish(unsigned char r_hash[20]);
+	};
+
 	class SHA256Context {
 
 	private:
@@ -60,8 +74,8 @@ public:
 		~SHA256Context();
 
 		Error start();
-		Error update(uint8_t *p_src, size_t p_len);
-		Error finish(unsigned char r_hash[16]);
+		Error update(const uint8_t *p_src, size_t p_len);
+		Error finish(unsigned char r_hash[32]);
 	};
 
 	class AESContext {