Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
24 changes: 11 additions & 13 deletions include/cryptlite/base64.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,22 +190,20 @@ class base64 : public boost::noncopyable {
}

private:
static const char enctable[65];
static const char dectable[128];
static constexpr char enctable[65] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static constexpr char dectable[128] {
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
-1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1
};

}; // end of class

const char base64::enctable[65] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const char base64::dectable[128] = {
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
-1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1
};

} // end of namespace

Expand Down
25 changes: 21 additions & 4 deletions include/cryptlite/hmac.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ THE SOFTWARE.
#include <cstring>
#include <cassert>
#include <iomanip>
#include <cryptlite/base64.h>
#include <boost/cstdint.hpp>

namespace cryptlite {
Expand Down Expand Up @@ -95,6 +96,26 @@ class hmac {
return oss.str();
}

inline static std::string calc_base64(
const std::string& text,
const std::string& key ) {
return calc_base64(reinterpret_cast<const boost::uint8_t*>(text.c_str()), text.size(),
reinterpret_cast<const boost::uint8_t*>(key.c_str()), key.size());
}

static std::string calc_base64(
const boost::uint8_t* text, int text_len,
const boost::uint8_t* key, int key_len ) {
int i;
boost::uint8_t digest[HASH_SIZE];
assert(key);
assert(text);
hmac<T> ctx(key, key_len);
ctx.input(text, text_len);
ctx.result(digest);
return base64::encode_from_array(digest, HASH_SIZE);
}

hmac(const boost::uint8_t* key, int key_len) : hasher_(T()) {
assert(key);
reset(key, key_len);
Expand Down Expand Up @@ -149,10 +170,6 @@ class hmac {
hasher_.input(text, text_len);
}

void final_bits(const boost::uint8_t bits, unsigned int bitcount) {
hasher_.final_bits(bits, bitcount);
}

void result(boost::uint8_t digest[HASH_SIZE]) {
assert(digest);
hasher_.result(digest);
Expand Down
29 changes: 0 additions & 29 deletions include/cryptlite/sha1.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,35 +130,6 @@ class sha1 {
}
}

void final_bits(const boost::uint8_t message_bits, unsigned int length)
{
boost::uint8_t masks[8] = {
/* 0 0b00000000 */ 0x00, /* 1 0b10000000 */ 0x80,
/* 2 0b11000000 */ 0xC0, /* 3 0b11100000 */ 0xE0,
/* 4 0b11110000 */ 0xF0, /* 5 0b11111000 */ 0xF8,
/* 6 0b11111100 */ 0xFC, /* 7 0b11111110 */ 0xFE
};
boost::uint8_t markbit[8] = {
/* 0 0b10000000 */ 0x80, /* 1 0b01000000 */ 0x40,
/* 2 0b00100000 */ 0x20, /* 3 0b00010000 */ 0x10,
/* 4 0b00001000 */ 0x08, /* 5 0b00000100 */ 0x04,
/* 6 0b00000010 */ 0x02, /* 7 0b00000001 */ 0x01
};

if (!length)
return;

if (computed_ || (length >= 8) || (length == 0))
corrupted_ = true;

if (corrupted_)
return;

boost::uint32_t temp;
SHA1_ADD_LENGTH(this, &temp, length);
finalize((boost::uint8_t)((message_bits & masks[length])|(markbit[length])));
}

void result(boost::uint8_t digest[HASH_SIZE])
{
assert(digest);
Expand Down
36 changes: 1 addition & 35 deletions include/cryptlite/sha256.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ THE SOFTWARE.
namespace cryptlite {

#define SHA256_SHR(bits,word) ((word) >> (bits))
#define SHA256_ROTL(bits,word) \
(((word) << (bits)) | ((word) >> (32-(bits))))
#define SHA256_ROTR(bits,word) \
(((word) >> (bits)) | ((word) << (32-(bits))))

Expand All @@ -55,7 +53,6 @@ namespace cryptlite {

#define SHA256_CH(x, y, z) (((x) & ((y) ^ (z))) ^ (z))
#define SHA256_MAJ(x, y, z) (((x) & ((y) | (z))) | ((y) & (z)))
#define SHA256_PARITY(x, y, z) ((x) ^ (y) ^ (z))

class sha256 {

Expand Down Expand Up @@ -146,36 +143,6 @@ class sha256 {
}
}

void final_bits(const boost::uint8_t message_bits, unsigned int length)
{
boost::uint8_t masks[8] = {
/* 0 0b00000000 */ 0x00, /* 1 0b10000000 */ 0x80,
/* 2 0b11000000 */ 0xC0, /* 3 0b11100000 */ 0xE0,
/* 4 0b11110000 */ 0xF0, /* 5 0b11111000 */ 0xF8,
/* 6 0b11111100 */ 0xFC, /* 7 0b11111110 */ 0xFE
};

boost::uint8_t markbit[8] = {
/* 0 0b10000000 */ 0x80, /* 1 0b01000000 */ 0x40,
/* 2 0b00100000 */ 0x20, /* 3 0b00010000 */ 0x10,
/* 4 0b00001000 */ 0x08, /* 5 0b00000100 */ 0x04,
/* 6 0b00000010 */ 0x02, /* 7 0b00000001 */ 0x01
};

if (!length)
return;

if (computed_ || (length >= 8) || (length == 0))
corrupted_ = true;

if (corrupted_)
return;

boost::uint32_t temp;
SHA256_ADD_LENGTH(this, &temp, length);
finalize((boost::uint8_t)((message_bits & masks[length]) | markbit[length]));
}

void result(boost::uint8_t digest[HASH_SIZE])
{
assert(digest);
Expand Down Expand Up @@ -257,8 +224,7 @@ class sha256 {
{
int i;
pad_message(pad_byte);
for (i = 0; i < BLOCK_SIZE; ++i)
message_block_[i] = 0;
memset(message_block_, 0, sizeof(message_block_));
length_low_ = 0;
length_high_ = 0;
computed_ = true;
Expand Down
Loading