/*!
* \file sha1.hh
* \brief Header file for SHA-1 helper methods.
* \author ETSI STF637
* \copyright ETSI Copyright Notification
* No part may be reproduced except as authorized by written permission.
* The copyright and the foregoing restriction extend to reproduction in all media.
* All rights reserved.
* \version 0.1
*/
#pragma once
#include <openssl/objects.h>
#include <openssl/sha.h>
class OCTETSTRING; //! TITAN forward declaration
/*!
* \class sha1
* \brief This class provides description of SHA-1 helper methods
*/
class sha1 {
SHA_CTX _ctx; //! SHA context
public:
/*!
* \brief Default constructor
* Create a new instance of the sha1 class
*/
explicit sha1() : _ctx{} {};
/*!
* \brief Default destructor
*/
virtual ~sha1(){};
/*!
* \fn int generate(const OCTETSTRING& p_buffer, OCTETSTRING& p_hash);
* \brief Receive bytes formated data from the lower layers
* \param[in] p_buffer The data used to generate the SHA-1 hash
* \param[out] p_hash The SHA-1 hash value based of the provided data
* \return 0 on success, -1 otherwise
*/
int generate(const OCTETSTRING &p_buffer, OCTETSTRING &p_hash);
/*!
* \fn int generate(const unsigned char* p_buffer, OCTETSTRING& p_hash);
* \brief Receive bytes formated data from the lower layers
* \param[in] p_buffer The data used to generate the SHA-1 hash
* \param[in] The length of the data buffer
* \param[out] p_hash The SHA-1 hash value based of the provided data
* \return 0 on success, -1 otherwise
*/
int generate(const unsigned char *p_buffer, const size_t p_length, OCTETSTRING &p_hash);
/*!
* \fn const OCTETSTRING get_sha1_empty_string() const;
* \brief Return the SHA-1 of an empty string
* \return The SHA-1 of an empty string
*/
inline const OCTETSTRING get_sha1_empty_string() const {
static unsigned char sha1_empty_string[] = {
0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d, 0x32, 0x55, 0xbf, 0xef, 0x95, 0x60, 0x18, 0x90,
0xaf, 0xd8, 0x07, 0x09}; //! SHA-1 of an empty string
return OCTETSTRING(20, sha1_empty_string);
};
}; // End of class sha1
/*!
* \file sha1.hh
* \brief Header file for SHA-1 helper methods.
* \author ETSI STF637
* \copyright ETSI Copyright Notification
* No part may be reproduced except as authorized by written permission.
* The copyright and the foregoing restriction extend to reproduction in all media.
* All rights reserved.
* \version 0.1
*/
#pragma once
#include <openssl/objects.h>
#include <openssl/sha.h>
class OCTETSTRING; //! TITAN forward declaration
/*!
* \class sha1
* \brief This class provides description of SHA-1 helper methods
*/
class sha1 {
SHA_CTX _ctx; //! SHA context
public:
/*!
* \brief Default constructor
* Create a new instance of the sha1 class
*/
explicit sha1() : _ctx{} {};
/*!
* \brief Default destructor
*/
virtual ~sha1(){};
/*!
* \fn int generate(const OCTETSTRING& p_buffer, OCTETSTRING& p_hash);
* \brief Receive bytes formated data from the lower layers
* \param[in] p_buffer The data used to generate the SHA-1 hash
* \param[out] p_hash The SHA-1 hash value based of the provided data
* \return 0 on success, -1 otherwise
*/
int generate(const OCTETSTRING &p_buffer, OCTETSTRING &p_hash);
/*!
* \fn int generate(const unsigned char* p_buffer, OCTETSTRING& p_hash);
* \brief Receive bytes formated data from the lower layers
* \param[in] p_buffer The data used to generate the SHA-1 hash
* \param[in] The length of the data buffer
* \param[out] p_hash The SHA-1 hash value based of the provided data
* \return 0 on success, -1 otherwise
*/
int generate(const unsigned char *p_buffer, const size_t p_length, OCTETSTRING &p_hash);
/*!
* \fn const OCTETSTRING get_sha1_empty_string() const;
* \brief Return the SHA-1 of an empty string
* \return The SHA-1 of an empty string
*/
inline const OCTETSTRING get_sha1_empty_string() const {
static unsigned char sha1_empty_string[] = {
0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d, 0x32, 0x55, 0xbf, 0xef, 0x95, 0x60, 0x18, 0x90,
0xaf, 0xd8, 0x07, 0x09}; //! SHA-1 of an empty string
return OCTETSTRING(20, sha1_empty_string);
};
}; // End of class sha1
/*!
* \file sha256.hh
* \brief Header file for SHA-256 helper methods.
* \author ETSI STF637
* \copyright ETSI Copyright Notification
* No part may be reproduced except as authorized by written permission.
* The copyright and the foregoing restriction extend to reproduction in all media.
* All rights reserved.
* \version 0.1
*/
#pragma once
#include <openssl/objects.h>
#include <openssl/sha.h>
class OCTETSTRING; //! TITAN forward declaration
/*!
* \class sha256
* \brief This class provides description of SHA-256 helper methods
*/
class sha256 {
SHA256_CTX _ctx; //! SHA context
public:
/*!
* \brief Default constructor
* Create a new instance of the sha256 class
*/
explicit sha256() : _ctx{} {};
/*!
* \brief Default destructor
*/
virtual ~sha256(){};
/*!
* \fn int generate(const OCTETSTRING& p_buffer, OCTETSTRING& p_hash);
* \brief Receive bytes formated data from the lower layers
* \param[in] p_buffer The data used to generate the SHA-256 hash
* \param[out] p_hash The SHA-256 hash value based of the provided data
* \return 0 on success, -1 otherwise
*/
int generate(const OCTETSTRING &p_buffer, OCTETSTRING &p_hash);
/*!
* \fn int generate(const unsigned char* p_buffer, OCTETSTRING& p_hash);
* \brief Receive bytes formated data from the lower layers
* \param[in] p_buffer The data used to generate the SHA-256 hash
* \param[in] The length of the data buffer
* \param[out] p_hash The SHA-256 hash value based of the provided data
* \return 0 on success, -1 otherwise
*/
int generate(const unsigned char *p_buffer, const size_t p_length, OCTETSTRING &p_hash);
/*!
* \fn const OCTETSTRING get_sha256_empty_string() const;
* \brief Return the SHA-256 of an empty string
* \return The SHA-256 of an empty string
*/
inline const OCTETSTRING get_sha256_empty_string() const {
static unsigned char sha256_empty_string[] = {
0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,
0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55}; //! SHA-256 of an empty string
return OCTETSTRING(32, sha256_empty_string);
};
}; // End of class sha256
/*!
* \file sha256.hh
* \brief Header file for SHA-256 helper methods.
* \author ETSI STF637
* \copyright ETSI Copyright Notification
* No part may be reproduced except as authorized by written permission.
* The copyright and the foregoing restriction extend to reproduction in all media.
* All rights reserved.
* \version 0.1
*/
#pragma once
#include <openssl/objects.h>
#include <openssl/sha.h>
class OCTETSTRING; //! TITAN forward declaration
/*!
* \class sha256
* \brief This class provides description of SHA-256 helper methods
*/
class sha256 {
SHA256_CTX _ctx; //! SHA context
public:
/*!
* \brief Default constructor
* Create a new instance of the sha256 class
*/
explicit sha256() : _ctx{} {};
/*!
* \brief Default destructor
*/
virtual ~sha256(){};
/*!
* \fn int generate(const OCTETSTRING& p_buffer, OCTETSTRING& p_hash);
* \brief Receive bytes formated data from the lower layers
* \param[in] p_buffer The data used to generate the SHA-256 hash
* \param[out] p_hash The SHA-256 hash value based of the provided data
* \return 0 on success, -1 otherwise
*/
int generate(const OCTETSTRING &p_buffer, OCTETSTRING &p_hash);
/*!
* \fn int generate(const unsigned char* p_buffer, OCTETSTRING& p_hash);
* \brief Receive bytes formated data from the lower layers
* \param[in] p_buffer The data used to generate the SHA-256 hash
* \param[in] The length of the data buffer
* \param[out] p_hash The SHA-256 hash value based of the provided data
* \return 0 on success, -1 otherwise
*/
int generate(const unsigned char *p_buffer, const size_t p_length, OCTETSTRING &p_hash);
/*!
* \fn const OCTETSTRING get_sha256_empty_string() const;
* \brief Return the SHA-256 of an empty string
* \return The SHA-256 of an empty string
*/
inline const OCTETSTRING get_sha256_empty_string() const {
static unsigned char sha256_empty_string[] = {
0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,
0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55}; //! SHA-256 of an empty string
return OCTETSTRING(32, sha256_empty_string);
};
}; // End of class sha256
/*!
* \file sha384.hh
* \brief Header file for SHA-384 helper methods.
* \author ETSI STF637
* \copyright ETSI Copyright Notification
* No part may be reproduced except as authorized by written permission.
* The copyright and the foregoing restriction extend to reproduction in all media.
* All rights reserved.
* \version 0.1
*/
#pragma once
#include <openssl/objects.h>
#include <openssl/sha.h>
class OCTETSTRING; //! TITAN forward declaration
/*!
* \class sha384
* \brief This class provides description of SHA-384 helper methods
*/
class sha384 {
SHA512_CTX _ctx; //! SHA context
public: //! \publicsection
/*!
* \brief Default constructor
* Create a new instance of the sha384 class
*/
explicit sha384() : _ctx{} {};
/*!
* \brief Default destructor
*/
virtual ~sha384(){};
/*!
* \fn int generate(const OCTETSTRING& p_buffer, OCTETSTRING& p_hash);
* \brief Receive bytes formated data from the lower layers
* \param[in] p_buffer The data used to generate the SHA-384 hash
* \param[out] p_hash The SHA-384 hash value based of the provided data
* \return 0 on success, -1 otherwise
*/
int generate(const OCTETSTRING &p_buffer, OCTETSTRING &p_hash);
/*!
* \fn int generate(const OCTETSTRING p_buffer, OCTETSTRING& p_hash);
* \brief Receive bytes formated data from the lower layers
* \param[in] p_buffer The data used to generate the SHA-384 hash
* \param[in] The length of the data buffer
* \param[out] p_hash The SHA-384 hash value based of the provided data
* \return 0 on success, -1 otherwise
*/
int generate(const unsigned char *p_buffer, const size_t p_length, OCTETSTRING &p_hash);
/*!
* \fn const OCTETSTRING get_sha384_empty_string() const;
* \brief Return the SHA-384 of an empty string
* \return The SHA-384 of an empty string
*/
const OCTETSTRING get_sha384_empty_string() const;
}; // End of class sha384
/*!
* \file sha384.hh
* \brief Header file for SHA-384 helper methods.
* \author ETSI STF637
* \copyright ETSI Copyright Notification
* No part may be reproduced except as authorized by written permission.
* The copyright and the foregoing restriction extend to reproduction in all media.
* All rights reserved.
* \version 0.1
*/
#pragma once
#include <openssl/objects.h>
#include <openssl/sha.h>
class OCTETSTRING; //! TITAN forward declaration
/*!
* \class sha384
* \brief This class provides description of SHA-384 helper methods
*/
class sha384 {
SHA512_CTX _ctx; //! SHA context
public: //! \publicsection
/*!
* \brief Default constructor
* Create a new instance of the sha384 class
*/
explicit sha384() : _ctx{} {};
/*!
* \brief Default destructor
*/
virtual ~sha384(){};
/*!
* \fn int generate(const OCTETSTRING& p_buffer, OCTETSTRING& p_hash);
* \brief Receive bytes formated data from the lower layers
* \param[in] p_buffer The data used to generate the SHA-384 hash
* \param[out] p_hash The SHA-384 hash value based of the provided data
* \return 0 on success, -1 otherwise
*/
int generate(const OCTETSTRING &p_buffer, OCTETSTRING &p_hash);
/*!
* \fn int generate(const OCTETSTRING p_buffer, OCTETSTRING& p_hash);
* \brief Receive bytes formated data from the lower layers
* \param[in] p_buffer The data used to generate the SHA-384 hash
* \param[in] The length of the data buffer
* \param[out] p_hash The SHA-384 hash value based of the provided data
* \return 0 on success, -1 otherwise
*/
int generate(const unsigned char *p_buffer, const size_t p_length, OCTETSTRING &p_hash);
/*!
* \fn const OCTETSTRING get_sha384_empty_string() const;
* \brief Return the SHA-384 of an empty string
* \return The SHA-384 of an empty string
*/
const OCTETSTRING get_sha384_empty_string() const;
}; // End of class sha384
......@@ -9,5 +9,6 @@ sources := \
src/sha256.cc \
src/sha384.cc \
src/hmac.cc \
# src/sm3.cc \
includes := ./include
/*!
* \file sha1.cc
* \brief Source file for SHA-1 helper methods.
* \author ETSI STF637
* \copyright ETSI Copyright Notification
* No part may be reproduced except as authorized by written permission.
* The copyright and the foregoing restriction extend to reproduction in all media.
* All rights reserved.
* \version 0.1
*/
#include <TTCN3.hh>
#include "sha1.hh"
int sha1::generate(const OCTETSTRING& p_buffer, OCTETSTRING& p_hash) {
// Sanity check
if (p_buffer.lengthof() == 0) {
p_hash = get_sha1_empty_string();
return 0;
}
return generate(static_cast<const unsigned char*>(p_buffer), p_buffer.lengthof(), p_hash);
}
int sha1::generate(const unsigned char* p_buffer, const size_t p_length, OCTETSTRING& p_hash) {
// Sanity check
if ((p_buffer == nullptr) || (p_length == 0)) {
p_hash = get_sha1_empty_string();
return 0;
}
// Resize data buffer
p_hash = int2oct(0, SHA_DIGEST_LENGTH);
// Compute the hash value
::SHA1_Init(&_ctx);
::SHA1_Update(&_ctx, p_buffer, p_length);
::SHA1_Final((unsigned char*)static_cast<const unsigned char*>(p_hash), &_ctx);
return 0;
};
/*!
* \file sha1.cc
* \brief Source file for SHA-1 helper methods.
* \author ETSI STF637
* \copyright ETSI Copyright Notification
* No part may be reproduced except as authorized by written permission.
* The copyright and the foregoing restriction extend to reproduction in all media.
* All rights reserved.
* \version 0.1
*/
#include <TTCN3.hh>
#include "sha1.hh"
int sha1::generate(const OCTETSTRING& p_buffer, OCTETSTRING& p_hash) {
// Sanity check
if (p_buffer.lengthof() == 0) {
p_hash = get_sha1_empty_string();
return 0;
}
return generate(static_cast<const unsigned char*>(p_buffer), p_buffer.lengthof(), p_hash);
}
int sha1::generate(const unsigned char* p_buffer, const size_t p_length, OCTETSTRING& p_hash) {
// Sanity check
if ((p_buffer == nullptr) || (p_length == 0)) {
p_hash = get_sha1_empty_string();
return 0;
}
// Resize data buffer
p_hash = int2oct(0, SHA_DIGEST_LENGTH);
// Compute the hash value
::SHA1_Init(&_ctx);
::SHA1_Update(&_ctx, p_buffer, p_length);
::SHA1_Final((unsigned char*)static_cast<const unsigned char*>(p_hash), &_ctx);
return 0;
};
/*!
* \file sha256.cc
* \brief Source file for SHA-256 helper methods.
* \author ETSI STF637
* \copyright ETSI Copyright Notification
* No part may be reproduced except as authorized by written permission.
* The copyright and the foregoing restriction extend to reproduction in all media.
* All rights reserved.
* \version 0.1
*/
#include <TTCN3.hh>
#include "sha256.hh"
int sha256::generate(const OCTETSTRING& p_buffer, OCTETSTRING& p_hash) {
// Sanity check
if (p_buffer.lengthof() == 0) {
p_hash = get_sha256_empty_string();
return 0;
}
return generate(static_cast<const unsigned char*>(p_buffer), p_buffer.lengthof(), p_hash);
}
int sha256::generate(const unsigned char* p_buffer, const size_t p_length, OCTETSTRING& p_hash) {
// Sanity check
if ((p_buffer == nullptr) || (p_length == 0)) {
p_hash = get_sha256_empty_string();
return 0;
}
// Resize data buffer
p_hash = int2oct(0, SHA256_DIGEST_LENGTH);
// Compute the hash value
::SHA256_Init(&_ctx);
::SHA256_Update(&_ctx, p_buffer, p_length);
::SHA256_Final((unsigned char*)static_cast<const unsigned char*>(p_hash), &_ctx);
return 0;
};
/*!
* \file sha256.cc
* \brief Source file for SHA-256 helper methods.
* \author ETSI STF637
* \copyright ETSI Copyright Notification
* No part may be reproduced except as authorized by written permission.
* The copyright and the foregoing restriction extend to reproduction in all media.
* All rights reserved.
* \version 0.1
*/
#include <TTCN3.hh>
#include "sha256.hh"
int sha256::generate(const OCTETSTRING& p_buffer, OCTETSTRING& p_hash) {
// Sanity check
if (p_buffer.lengthof() == 0) {
p_hash = get_sha256_empty_string();
return 0;
}
return generate(static_cast<const unsigned char*>(p_buffer), p_buffer.lengthof(), p_hash);
}
int sha256::generate(const unsigned char* p_buffer, const size_t p_length, OCTETSTRING& p_hash) {
// Sanity check
if ((p_buffer == nullptr) || (p_length == 0)) {
p_hash = get_sha256_empty_string();
return 0;
}
// Resize data buffer
p_hash = int2oct(0, SHA256_DIGEST_LENGTH);
// Compute the hash value
::SHA256_Init(&_ctx);
::SHA256_Update(&_ctx, p_buffer, p_length);
::SHA256_Final((unsigned char*)static_cast<const unsigned char*>(p_hash), &_ctx);
return 0;
};
/*!
* \file sha384.cc
* \brief Sorce file for SHA-384 helper methods.
* \author ETSI STF637
* \copyright ETSI Copyright Notification
* No part may be reproduced except as authorized by written permission.
* The copyright and the foregoing restriction extend to reproduction in all media.
* All rights reserved.
* \version 0.1
*/
#include <TTCN3.hh>
#include "sha384.hh"
int sha384::generate(const OCTETSTRING &p_buffer, OCTETSTRING &p_hash) {
// Sanity check
if (p_buffer.lengthof() == 0) {
p_hash = get_sha384_empty_string();
return 0;
}
return generate(static_cast<const unsigned char *>(p_buffer), p_buffer.lengthof(), p_hash);
}
int sha384::generate(const unsigned char *p_buffer, const size_t p_length, OCTETSTRING &p_hash) {
// Sanity check
if ((p_buffer == nullptr) || (p_length == 0)) {
p_hash = get_sha384_empty_string();
return 0;
}
// Resize data buffer
p_hash = int2oct(0, SHA384_DIGEST_LENGTH);
// Compute the hash value
::SHA384_Init(&_ctx);
::SHA384_Update(&_ctx, p_buffer, p_length);
::SHA384_Final((unsigned char *)static_cast<const unsigned char *>(p_hash), &_ctx);
return 0;
}
const OCTETSTRING sha384::get_sha384_empty_string() const {
static unsigned char
sha384_empty_string[] = {0x38, 0xb0, 0x60, 0xa7, 0x51, 0xac, 0x96, 0x38, 0x4c, 0xd9, 0x32, 0x7e, 0xb1, 0xb1, 0xe3, 0x6a,
0x21, 0xfd, 0xb7, 0x11, 0x14, 0xbe, 0x07, 0x43, 0x4c, 0x0c, 0xc7, 0xbf, 0x63, 0xf6, 0xe1, 0xda,
0x27, 0x4e, 0xde, 0xbf, 0xe7, 0x6f, 0x65, 0xfb, 0xd5, 0x1a, 0xd2, 0xf1, 0x48, 0x98, 0xb9, 0x5b}; //! SHA-384 of an empty string
return OCTETSTRING(48, sha384_empty_string);
}
/*!
* \file sha384.cc
* \brief Sorce file for SHA-384 helper methods.
* \author ETSI STF637
* \copyright ETSI Copyright Notification
* No part may be reproduced except as authorized by written permission.
* The copyright and the foregoing restriction extend to reproduction in all media.
* All rights reserved.
* \version 0.1
*/
#include <TTCN3.hh>
#include "sha384.hh"
int sha384::generate(const OCTETSTRING &p_buffer, OCTETSTRING &p_hash) {
// Sanity check
if (p_buffer.lengthof() == 0) {
p_hash = get_sha384_empty_string();
return 0;
}
return generate(static_cast<const unsigned char *>(p_buffer), p_buffer.lengthof(), p_hash);
}
int sha384::generate(const unsigned char *p_buffer, const size_t p_length, OCTETSTRING &p_hash) {
// Sanity check
if ((p_buffer == nullptr) || (p_length == 0)) {
p_hash = get_sha384_empty_string();
return 0;
}
// Resize data buffer
p_hash = int2oct(0, SHA384_DIGEST_LENGTH);
// Compute the hash value
::SHA384_Init(&_ctx);
::SHA384_Update(&_ctx, p_buffer, p_length);
::SHA384_Final((unsigned char *)static_cast<const unsigned char *>(p_hash), &_ctx);
return 0;
}
const OCTETSTRING sha384::get_sha384_empty_string() const {
static unsigned char
sha384_empty_string[] = {0x38, 0xb0, 0x60, 0xa7, 0x51, 0xac, 0x96, 0x38, 0x4c, 0xd9, 0x32, 0x7e, 0xb1, 0xb1, 0xe3, 0x6a,
0x21, 0xfd, 0xb7, 0x11, 0x14, 0xbe, 0x07, 0x43, 0x4c, 0x0c, 0xc7, 0xbf, 0x63, 0xf6, 0xe1, 0xda,
0x27, 0x4e, 0xde, 0xbf, 0xe7, 0x6f, 0x65, 0xfb, 0xd5, 0x1a, 0xd2, 0xf1, 0x48, 0x98, 0xb9, 0x5b}; //! SHA-384 of an empty string
return OCTETSTRING(48, sha384_empty_string);
}