Integrity

The data integrity security ensures the correctness or accuracy of data. The data is protected against unauthorized modification, deletion, creation, and replication and provides an indication of these unauthorized. Integrity implies that the data is an exact copy of some original version.

Data integrity is provided by hash functions like Message Digest Algorithm (MD5) or Secure Hash Algorithm (SHA). The following methods are provided by the Encryption libraries:

  • MD5 algorithm takes as input a message of arbitrary length and produces as output a 128-bit “fingerprint” or “message digest” of the input message. MD5 is currently a standard, Internet Engineering Task Force (IETF) Request for Comments (RFC) 1321. In comparison, MD5 is not quite as fast as the MD4 algorithm, but offers much more assurance of data security.

  • SHA is considered to be the successor to MD5. The Federal Information Processing Standard (FIPS 180-2) specifies four secure hash algorithms: SHA-1, SHA-256, SHA-384, and SHA-512. All algorithms are iterative, one-way hash functions that can process a message with a maximum length of 2⁶⁴ bits to 212⁸ bits to produce a 160-bit to 512-bit condensed representation called a message digest. The input message is processed in 512-bit to 1024-bit blocks. The four algorithms differ most significantly in the number of bits for the message digest length. Each SHA algorithm processes a message in two stages: preprocessing and hash computation. Preprocessing involves padding a message, parsing the padded message into 512-bit or 1024-bit blocks, and setting initialization values to be used in the hash computation. The hash computation generates a message schedule from the padded message and uses that schedule, along with functions, constants, and word operations to iteratively generate a series of hash values. The final hash value generated by the hash computation is used to determine the message digest.

Waspmote Libraries

Waspmote Hash Files

WaspHash.h is the header file of the class, and WaspHash.cpp is the class where the functions and variables are implemented.

It is mandatory to include the Hash library when using it. The following line must be introduced at the beginning of the code:

#include <WaspHash.h>

Constructor

To start using Waspmote Hash library, an object from class ‘WaspHash’ must be created. This object, called HASH, is created inside the Waspmote Hash library and it is public to all libraries. It is used through the guide to show how the Waspmote Hash library works.

When creating this constructor, no variables are initialized by default.

Pre-Defined Constants

There are some constants defined in ‘WaspHash.h’ related with the different encryption and padding modes that can be used to encrypt and decrypt messages.

Calculating Message Digest

This section will describe how calculate a message digest depends on the selected algorithm. The different algorithms provide different message digest size.

For example, the Message Digest 5 (MD5) hash is a mathematical algorithm which produces a unique 128 bit number (a hash) created from the data input. If even one bit of data changes, the hash value will change.

Calculating MD5 hash

Previously, a variable to store the message digest must be declared. This variable must be correctly dimensioned to contain the calculated hash message.

{
    uint8_t hash_message[16];
}

The features that must be met in order to calculate the MD5 algorithm are:

Algorithms

Output size (bits)

Internal state size (bits)

Block size (bits)

Max message size (bits)

Word size (bits)

MD5

128

128

512

2⁶⁴− 1

32

The next code shows how to calculate the message digest with HASH.md5(). The inputs expected are: the pointer to the buffer where the output is stored, the input message pointer and the length of the input message.

{
 char message[] = “Libelium”;
 HASH.md5(hash_message_md5, (uint8_t*)message, strlen(message)*8);
}

Calculating SHA hash

The features that must be met in order to calculate the SHA algorithm are:

Algorithms

Output size (bits)

Internal state size (bits)

Block size (bits)

Max message size (bits)

Word size (bits)

Rounds

SHA-1

160

160

512

2⁶⁴− 1

32

80

SHA-224

224

256

512

2⁶⁴− 1

32

64

SHA-256

256

256

512

2⁶⁴− 1

32

64

SHA-384

384

512

512

2128− 1

64

80

SHA-512

512

512

1024

2128− 1

64

80

Previously, a variable to store the message digest must be declared. This variable must be correctly dimensioned to contain the calculated hash message.

If SHA-1 is used, the variable that stores the hash message is defined:

{
    uint8_t hash_message[20];
}

If SHA-384 is used, the variable that stores the hash message is defined:

{
    uint8_t hash_message[48];
}

Next code shows how to calculate the message digest with HASH.sha(). The inputs expected are: the SHA algorithm, the pointer to the buffer where the output is stored, the input message pointer and the length of the input message.

{
 char message[] = “Libelium”;
 HASH.sha(SHA1, hash_message, (uint8_t*)message, strlen(message)*8);
}
{
 char message[] = “Libelium”;
 HASH.sha(SHA384, hash_message, (uint8_t*)message, strlen(message)*8);
}

Printing Message

Using the hash function HASH.printMessageDigest() the message digest is written via USB port.

{
 // Write message digest by USB port
 HASH.printMessageDigest("SHA-1:", hash_message, 20);
 HASH.printMessageDigest("MD5:", hash_message, 16);
}

Example of use may be found in:

https://development.libelium.com/hash-01-md5/

https://development.libelium.com/hash-02-sha-1/

Last updated