From b2bc9b5c707bd7d932d60cd4e8c1cb580b36b5b4 Mon Sep 17 00:00:00 2001 From: bdemsky Date: Sun, 11 Mar 2018 05:32:46 -0700 Subject: [PATCH] Bug fixes + tabbing --- version2/src/C/ArbitrationRound.cc | 8 +- version2/src/C/ByteBuffer.cc | 36 +- version2/src/C/CloudComm.cc | 276 +++++----- version2/src/C/CloudComm.h | 9 +- version2/src/C/Crypto.cc | 13 +- version2/src/C/Crypto.h | 9 +- version2/src/C/Error.h | 4 +- version2/src/C/IoTString.h | 14 +- version2/src/C/Mac.cc | 5 +- version2/src/C/Mac.h | 2 +- version2/src/C/Makefile | 2 +- version2/src/C/SecureRandom.h | 4 +- version2/src/C/SlotBuffer.cc | 2 +- version2/src/C/Table.cc | 8 +- version2/src/C/Table.h | 6 +- version2/src/C/aes.cc | 106 ++-- version2/src/C/aes.h | 132 ++--- version2/src/C/pbkdf2-sha256.cc | 779 +++++++++++++++-------------- version2/src/C/pbkdf2-sha256.h | 1 + version2/src/C/vector.h | 2 +- 20 files changed, 717 insertions(+), 701 deletions(-) diff --git a/version2/src/C/ArbitrationRound.cc b/version2/src/C/ArbitrationRound.cc index f038fa2..f5a1521 100644 --- a/version2/src/C/ArbitrationRound.cc +++ b/version2/src/C/ArbitrationRound.cc @@ -27,14 +27,14 @@ void ArbitrationRound::generateParts() { return; } parts = new Vector(); - SetIterator * abit = abortsBefore->iterator(); - while(abit->hasNext()) + SetIterator *abit = abortsBefore->iterator(); + while (abit->hasNext()) parts->add((Entry *)abit->next()); delete abit; if (commit != NULL) { - Vector * cParts = commit->getParts(); + Vector *cParts = commit->getParts(); uint cPartsSize = cParts->size(); - for(uint i=0; i < cPartsSize; i++) { + for (uint i = 0; i < cPartsSize; i++) { parts->add((Entry *)cParts->get(i)); } } diff --git a/version2/src/C/ByteBuffer.cc b/version2/src/C/ByteBuffer.cc index e1f6d4b..22e28cd 100644 --- a/version2/src/C/ByteBuffer.cc +++ b/version2/src/C/ByteBuffer.cc @@ -7,7 +7,7 @@ ByteBuffer::ByteBuffer(Array *array) : } void ByteBuffer::put(char c) { - buffer->set(offset++ , c); + buffer->set(offset++, c); } void ByteBuffer::putInt(int32_t l) { @@ -30,29 +30,29 @@ void ByteBuffer::putLong(int64_t l) { void ByteBuffer::put(Array *array) { memcpy(&buffer->internalArray()[offset], array->internalArray(), array->length()); - offset+=array->length(); + offset += array->length(); } int64_t ByteBuffer::getLong() { - char * array = &buffer->internalArray()[offset]; - offset+=8; - return (((int64_t)array[0]) << 56) | - (((int64_t)array[1]) << 48) | - (((int64_t)array[2]) << 40) | - (((int64_t)array[3]) << 32) | - (((int64_t)array[4]) << 24) | - (((int64_t)array[5]) << 16) | - (((int64_t)array[6]) << 8) | - (((int64_t)array[7])); + char *array = &buffer->internalArray()[offset]; + offset += 8; + return (((int64_t)(unsigned char)array[0]) << 56) | + (((int64_t)(unsigned char)array[1]) << 48) | + (((int64_t)(unsigned char)array[2]) << 40) | + (((int64_t)(unsigned char)array[3]) << 32) | + (((int64_t)(unsigned char)array[4]) << 24) | + (((int64_t)(unsigned char)array[5]) << 16) | + (((int64_t)(unsigned char)array[6]) << 8) | + (((int64_t)(unsigned char)array[7])); } int32_t ByteBuffer::getInt() { - char * array = &buffer->internalArray()[offset]; - offset+=4; - return (((int32_t)array[0]) << 24) | - (((int32_t)array[1]) << 16) | - (((int32_t)array[2]) << 8) | - (((int32_t)array[3])); + char *array = &buffer->internalArray()[offset]; + offset += 4; + return (((int32_t)(unsigned char)array[0]) << 24) | + (((int32_t)(unsigned char)array[1]) << 16) | + (((int32_t)(unsigned char)array[2]) << 8) | + (((int32_t)(unsigned char)array[3])); } char ByteBuffer::get() { diff --git a/version2/src/C/CloudComm.cc b/version2/src/C/CloudComm.cc index 9149f2d..c20488d 100644 --- a/version2/src/C/CloudComm.cc +++ b/version2/src/C/CloudComm.cc @@ -49,7 +49,7 @@ CloudComm::CloudComm(Table *_table, IoTString *_baseurl, IoTString *_password, baseurl(_baseurl), key(NULL), mac(NULL), - password(_password), + password(new IoTString(_password)), random(new SecureRandom()), salt(NULL), table(_table), @@ -107,6 +107,7 @@ void CloudComm::initCrypt() { } try { key = initKey(); + delete password; password = NULL;// drop password mac = new Mac(); mac->init(key); @@ -130,10 +131,10 @@ IoTString *CloudComm::buildRequest(bool isput, int64_t sequencenumber, int64_t m return urlstr; } -void loopWrite(int fd, char * array, int bytestowrite) { +void loopWrite(int fd, char *array, int bytestowrite) { int byteswritten = 0; while (bytestowrite) { - int bytes = write(fd, & array[byteswritten], bytestowrite); + int bytes = write(fd, &array[byteswritten], bytestowrite); if (bytes >= 0) { byteswritten += bytes; bytestowrite -= bytes; @@ -144,10 +145,10 @@ void loopWrite(int fd, char * array, int bytestowrite) { } } -void loopRead(int fd, char * array, int bytestoread) { +void loopRead(int fd, char *array, int bytestoread) { int bytesread = 0; while (bytestoread) { - int bytes = read(fd, & array[bytesread], bytestoread); + int bytes = read(fd, &array[bytesread], bytestoread); if (bytes >= 0) { bytesread += bytes; bytestoread -= bytes; @@ -158,13 +159,13 @@ void loopRead(int fd, char * array, int bytestoread) { } } -int openURL(IoTString *url) { +WebConnection openURL(IoTString *url) { if (url->length() < 7 || memcmp(url->internalBytes()->internalArray(), "http://", 7)) { printf("BOGUS URL\n"); exit(-1); } int i = 7; - for(; i < url->length(); i++) + for (; i < url->length(); i++) if (url->get(i) == '/') break; @@ -172,34 +173,34 @@ int openURL(IoTString *url) { printf("ERROR in openURL\n"); exit(-1); } - - char * host = (char *) malloc(i - 6); - memcpy(host, &url->internalBytes()->internalArray()[7], i-7); - host[i-7] = 0; + + char *host = (char *) malloc(i - 6); + memcpy(host, &url->internalBytes()->internalArray()[7], i - 7); + host[i - 7] = 0; printf("%s\n", host); - char * message = (char *)malloc(sizeof("POST HTTP/1.1\r\n") + sizeof("Host: \r\n") + 2*url->length()); - + char *message = (char *)malloc(sizeof("POST HTTP/1.1\r\n") + sizeof("Host: \r\n") + 2 * url->length()); + /* fill in the parameters */ int post = sprintf(message,"POST "); /* copy data */ - memcpy(&message[post], &url->internalBytes()->internalArray()[i], url->length()-i); - int endpost = sprintf(&message[post+url->length()-i], " HTTP/1.1\r\n"); + memcpy(&message[post], &url->internalBytes()->internalArray()[i], url->length() - i); + int endpost = sprintf(&message[post + url->length() - i], " HTTP/1.1\r\n"); + + int hostlen = sprintf(&message[endpost + post + url->length() - i], "Host: "); + memcpy(&message[endpost + post + url->length() + hostlen - i], host, i - 7); + sprintf(&message[endpost + post + url->length() + hostlen - 7], "\r\n"); - int hostlen = sprintf(&message[endpost + post + url->length()-i], "Host: "); - memcpy(&message[endpost + post + url->length()+hostlen-i], host, i-7); - sprintf(&message[endpost + post + url->length()+hostlen-7], "\r\n"); - /* create the socket */ int sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) {printf("ERROR opening socket\n"); exit(-1);} - + /* lookup the ip address */ struct hostent *server = gethostbyname(host); free(host); - + if (server == NULL) {printf("ERROR, no such host"); exit(-1);} - + /* fill in the structure */ struct sockaddr_in serv_addr; @@ -207,7 +208,7 @@ int openURL(IoTString *url) { serv_addr.sin_family = AF_INET; serv_addr.sin_port = htons(80); memcpy(&serv_addr.sin_addr.s_addr,server->h_addr,server->h_length); - + /* connect the socket */ if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0) { printf("ERROR connecting"); @@ -217,26 +218,26 @@ int openURL(IoTString *url) { /* send the request */ int total = strlen(message); loopWrite(sockfd, message, total); - return sockfd; + return (WebConnection) {sockfd, -1}; } int createSocket(IoTString *name, int port) { - char * host = (char *) malloc(name->length()+1); + char *host = (char *) malloc(name->length() + 1); memcpy(host, name->internalBytes()->internalArray(), name->length()); host[name->length()] = 0; printf("%s\n", host); /* How big is the message? */ - + /* create the socket */ int sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) {printf("ERROR opening socket\n"); exit(-1);} - + /* lookup the ip address */ struct hostent *server = gethostbyname(host); free(host); - + if (server == NULL) {printf("ERROR, no such host"); exit(-1);} - + /* fill in the structure */ struct sockaddr_in serv_addr; @@ -244,13 +245,13 @@ int createSocket(IoTString *name, int port) { serv_addr.sin_family = AF_INET; serv_addr.sin_port = htons(port); memcpy(&serv_addr.sin_addr.s_addr,server->h_addr,server->h_length); - + /* connect the socket */ if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0) { printf("ERROR connecting"); exit(-1); } - + return sockfd; } @@ -258,31 +259,31 @@ int createSocket(int port) { int fd; struct sockaddr_in sin; - bzero(&sin, sizeof(sin)); - sin.sin_family = AF_INET; - sin.sin_port = htons(port); - sin.sin_addr.s_addr = htonl(INADDR_ANY); - fd=socket(AF_INET, SOCK_STREAM, 0); + bzero(&sin, sizeof(sin)); + sin.sin_family = AF_INET; + sin.sin_port = htons(port); + sin.sin_addr.s_addr = htonl(INADDR_ANY); + fd = socket(AF_INET, SOCK_STREAM, 0); int n = 1; if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&n, sizeof (n)) < 0) { - close(fd); + close(fd); printf("Create Socket Error\n"); exit(-1); } - if (bind(fd, (struct sockaddr *) &sin, sizeof(sin))<0) { - close(fd); + if (bind(fd, (struct sockaddr *) &sin, sizeof(sin)) < 0) { + close(fd); exit(-1); } - if (listen(fd, 5)<0) { - close(fd); + if (listen(fd, 5) < 0) { + close(fd); exit(-1); } return fd; } int acceptSocket(int socket) { - struct sockaddr_in sin; - unsigned int sinlen=sizeof(sin); + struct sockaddr_in sin; + unsigned int sinlen = sizeof(sin); int newfd = accept(socket, (struct sockaddr *)&sin, &sinlen); int flag = 1; setsockopt(newfd, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(flag)); @@ -302,54 +303,53 @@ void writeSocketInt(int fd, int32_t value) { array[0] = value >> 24; array[1] = (value >> 16) & 0xff; array[2] = (value >> 8) & 0xff; - array[3] = (value >> 8) & 0xff; + array[3] = value & 0xff; loopWrite(fd, array, 4); } int readSocketInt(int fd) { char array[4]; loopRead(fd, array, 4); - return (((int32_t) array[0]) << 24) | - (((int32_t) array[1]) << 16) | - (((int32_t) array[2]) << 8) | - ((int32_t) array[3]); + return (((int32_t)(unsigned char) array[0]) << 24) | + (((int32_t)(unsigned char) array[1]) << 16) | + (((int32_t)(unsigned char) array[2]) << 8) | + ((int32_t)(unsigned char) array[3]); } void readSocketData(int fd, Array *data) { loopRead(fd, data->internalArray(), data->length()); } -void writeURLDataAndClose(int fd, Array *data) { - dprintf(fd, "Content-Length: %d\r\n\r\n", data->length()); - loopWrite(fd, data->internalArray(), data->length()); +void writeURLDataAndClose(WebConnection *wc, Array *data) { + dprintf(wc->fd, "Content-Length: %d\r\n\r\n", data->length()); + loopWrite(wc->fd, data->internalArray(), data->length()); } -void closeURLReq(int fd) { - dprintf(fd, "\r\n"); +void closeURLReq(WebConnection *wc) { + dprintf(wc->fd, "\r\n"); } -void readURLData(int fd, Array *output) { - loopRead(fd, output->internalArray(), output->length()); +void readURLData(WebConnection *wc, Array *output) { + loopRead(wc->fd, output->internalArray(), output->length()); } -int readURLInt(int fd) { +int readURLInt(WebConnection *wc) { char array[4]; - loopRead(fd, array, 4); - return (((int32_t) array[0]) << 24) | - (((int32_t) array[1]) << 16) | - (((int32_t) array[2]) << 8) | - ((int32_t) array[3]); + loopRead(wc->fd, array, 4); + return (((int32_t)(unsigned char) array[0]) << 24) | + (((int32_t)(unsigned char) array[1]) << 16) | + (((int32_t)(unsigned char) array[2]) << 8) | + ((int32_t)(unsigned char) array[3]); } -int getResponseCode(int fd) { - char response[600]; +void readLine(WebConnection *wc, char *response, int numBytes) { int offset = 0; char newchar; - while(true) { - int bytes = read(fd, &newchar, 1); + while (true) { + int bytes = read(wc->fd, &newchar, 1); if (bytes <= 0) break; - if (offset == (sizeof(response) - 1)) { + if (offset == (numBytes - 1)) { printf("Response too long"); exit(-1); } @@ -358,49 +358,29 @@ int getResponseCode(int fd) { break; } response[offset] = 0; +} + +int getResponseCode(WebConnection *wc) { + char response[600]; + readLine(wc, response, sizeof(response)); int ver1 = 0, ver2 = 0, respcode = 0; sscanf(response, "HTTP/%d.%d %d", &ver1, &ver2, &respcode); printf("Response code %d\n", respcode); return respcode; } -void readHeaders(int fd) { - int state = 2; - char newchar; +void readHeaders(WebConnection *wc) { + char response[600]; + int numBytes; - while(true) { - int bytes = read(fd, &newchar, 1); - if (bytes <= 0) - throw new Error("Headers malformed!"); - switch (state) { - case 0: - if (newchar == '\r') - state = 1; - break; - case 1: - if (newchar == '\n') - state = 2; - else - state = 0; - break; - case 2: - if (newchar == '\r') - state = 3; - else - state = 0; - break; - case 3: - if (newchar == '\n') - state = 4; - else - state = 0; - break; - default: - printf("ERROR in readHeaders\n"); - exit(-1); - } - if (state == 4) + while (true) { + readLine(wc, response, sizeof(response)); + if (response[0] == '\r') return; + else if (memcmp(response, "Content-Length:", sizeof("Content-Length:") - 1) == 0) { + sscanf(response, "Content-Length: %d", &numBytes); + wc->numBytes = numBytes; + } } } @@ -410,7 +390,7 @@ void CloudComm::setSalt() { return; } - int fd = -1; + WebConnection wc = {-1, -1}; try { Array *saltTmp = new Array(CloudComm_SALT_SIZE); random->nextBytes(saltTmp); @@ -423,15 +403,15 @@ void CloudComm::setSalt() { free(buffer); timer->startTime(); - fd = openURL(urlstr); - writeURLDataAndClose(fd, saltTmp); + wc = openURL(urlstr); + writeURLDataAndClose(&wc, saltTmp); - int responsecode = getResponseCode(fd); + int responsecode = getResponseCode(&wc); if (responsecode != HttpURLConnection_HTTP_OK) { throw new Error("Invalid response"); } - close(fd); - + close(wc.fd); + timer->endTime(); salt = saltTmp; } catch (Exception *e) { @@ -441,7 +421,7 @@ void CloudComm::setSalt() { } bool CloudComm::getSalt() { - int fd = -1; + WebConnection wc = {-1, -1}; IoTString *urlstr = NULL; try { @@ -456,8 +436,8 @@ bool CloudComm::getSalt() { } try { timer->startTime(); - fd = openURL(urlstr); - closeURLReq(fd); + wc = openURL(urlstr); + closeURLReq(&wc); timer->endTime(); } catch (SocketTimeoutException *e) { timer->endTime(); @@ -468,15 +448,22 @@ bool CloudComm::getSalt() { try { timer->startTime(); - int responsecode = getResponseCode(fd); - readHeaders(fd); + int responsecode = getResponseCode(&wc); + readHeaders(&wc); if (responsecode != HttpURLConnection_HTTP_OK) { throw new Error("Invalid response"); } - int salt_length = readURLInt(fd); + if (wc.numBytes == 0) { + timer->endTime(); + close(wc.fd); + return false; + } + + + int salt_length = readURLInt(&wc); Array *tmp = new Array(salt_length); - readURLData(fd, tmp); - close(fd); + readURLData(&wc, tmp); + close(wc.fd); salt = tmp; timer->endTime(); @@ -498,20 +485,23 @@ Array *CloudComm::createIV(int64_t machineId, int64_t localSequenceNumber) } Array *AESEncrypt(Array *ivBytes, AESKey *key, Array *data) { - Array * output=new Array(data->length()); - aes_encrypt_ctr((BYTE *)data->internalArray(), data->length(), (BYTE *) output->internalArray(), (WORD *)key->getKey()->internalArray(), key->getKey()->length()/(sizeof(WORD)/sizeof(BYTE)), (BYTE *)ivBytes->internalArray()); + Array *output = new Array(data->length()); + aes_encrypt_ctr((BYTE *)data->internalArray(), data->length(), (BYTE *) output->internalArray(), (WORD *)key->getKeySchedule(), key->getKey()->length() * 8, (BYTE *)ivBytes->internalArray()); return output; } Array *AESDecrypt(Array *ivBytes, AESKey *key, Array *data) { - Array * output=new Array(data->length()); - aes_decrypt_ctr((BYTE *)data->internalArray(), data->length(), (BYTE *)output->internalArray(), (WORD *)key->getKey()->internalArray(), key->getKey()->length()/(sizeof(WORD)/sizeof(BYTE)), (BYTE *)ivBytes->internalArray()); + Array *output = new Array(data->length()); + aes_decrypt_ctr((BYTE *)data->internalArray(), data->length(), (BYTE *)output->internalArray(), (WORD *)key->getKeySchedule(), key->getKey()->length() * 8, (BYTE *)ivBytes->internalArray()); return output; } Array *CloudComm::encryptSlotAndPrependIV(Array *rawData, Array *ivBytes) { try { Array *encryptedBytes = AESEncrypt(ivBytes, key, rawData); + Array *origBytes = AESDecrypt(ivBytes, key, encryptedBytes); + if (!rawData->equals(origBytes)) + throw new Error("BAD"); Array *chars = new Array(encryptedBytes->length() + CloudComm_IV_SIZE); System_arraycopy(ivBytes, 0, chars, 0, ivBytes->length()); System_arraycopy(encryptedBytes, 0, chars, CloudComm_IV_SIZE, encryptedBytes->length()); @@ -540,7 +530,7 @@ Array *CloudComm::stripIVAndDecryptSlot(Array *rawData) { * numbers. */ Array *CloudComm::putSlot(Slot *slot, int max) { - int fd = -1; + WebConnection wc = {-1, -1}; try { if (salt == NULL) { if (!getSalt()) { @@ -554,8 +544,8 @@ Array *CloudComm::putSlot(Slot *slot, int max) { Array *chars = encryptSlotAndPrependIV(slotBytes, slot->getSlotCryptIV()); IoTString *url = buildRequest(true, sequencenumber, max); timer->startTime(); - fd = openURL(url); - writeURLDataAndClose(fd, chars); + wc = openURL(url); + writeURLDataAndClose(&wc, chars); timer->endTime(); } catch (ServerException *e) { timer->endTime(); @@ -568,27 +558,27 @@ Array *CloudComm::putSlot(Slot *slot, int max) { } try { - int respcode = getResponseCode(fd); - readHeaders(fd); + int respcode = getResponseCode(&wc); + readHeaders(&wc); timer->startTime(); Array *resptype = new Array(7); - readURLData(fd, resptype); + readURLData(&wc, resptype); timer->endTime(); if (resptype->equals(getslot)) { - Array * tmp =processSlots(fd); - close(fd); + Array *tmp = processSlots(&wc); + close(wc.fd); return tmp; } else if (resptype->equals(putslot)) { - close(fd); + close(wc.fd); return NULL; } else { - close(fd); + close(wc.fd); throw new Error("Bad response to putslot"); } } catch (SocketTimeoutException *e) { timer->endTime(); - close(fd); + close(wc.fd); throw new ServerException("putSlot failed", ServerException_TypeInputTimeout); } catch (Exception *e) { throw new Error("putSlot failed"); @@ -600,7 +590,7 @@ Array *CloudComm::putSlot(Slot *slot, int max) { * sequencenumber or newer-> */ Array *CloudComm::getSlots(int64_t sequencenumber) { - int fd = -1; + WebConnection wc = {-1, -1}; try { if (salt == NULL) { if (!getSalt()) { @@ -611,8 +601,8 @@ Array *CloudComm::getSlots(int64_t sequencenumber) { IoTString *url = buildRequest(false, sequencenumber, 0); timer->startTime(); - fd = openURL(url); - closeURLReq(fd); + wc = openURL(url); + closeURLReq(&wc); timer->endTime(); } catch (SocketTimeoutException *e) { timer->endTime(); @@ -627,20 +617,20 @@ Array *CloudComm::getSlots(int64_t sequencenumber) { try { timer->startTime(); - int responsecode = getResponseCode(fd); - readHeaders(fd); + int responsecode = getResponseCode(&wc); + readHeaders(&wc); Array *resptype = new Array(7); - readURLData(fd, resptype); + readURLData(&wc, resptype); timer->endTime(); if (!resptype->equals(getslot)) throw new Error("Bad Response: "); - Array * tmp=processSlots(fd); - close(fd); + Array *tmp = processSlots(&wc); + close(wc.fd); return tmp; } catch (SocketTimeoutException *e) { timer->endTime(); - close(fd); + close(wc.fd); throw new ServerException("getSlots failed", ServerException_TypeInputTimeout); } catch (Exception *e) { throw new Error("getSlots failed"); @@ -651,16 +641,16 @@ Array *CloudComm::getSlots(int64_t sequencenumber) { * Method that actually handles building Slot objects from the * server response. Shared by both putSlot and getSlots. */ -Array *CloudComm::processSlots(int fd) { - int numberofslots = readURLInt(fd); +Array *CloudComm::processSlots(WebConnection *wc) { + int numberofslots = readURLInt(wc); Array *sizesofslots = new Array(numberofslots); Array *slots = new Array(numberofslots); for (int i = 0; i < numberofslots; i++) - sizesofslots->set(i, readURLInt(fd)); + sizesofslots->set(i, readURLInt(wc)); for (int i = 0; i < numberofslots; i++) { Array *rawData = new Array(sizesofslots->get(i)); - readURLData(fd, rawData); + readURLData(wc, rawData); Array *data = stripIVAndDecryptSlot(rawData); slots->set(i, Slot_decode(table, data, mac)); } diff --git a/version2/src/C/CloudComm.h b/version2/src/C/CloudComm.h index daa7989..ce7cfa0 100644 --- a/version2/src/C/CloudComm.h +++ b/version2/src/C/CloudComm.h @@ -18,6 +18,11 @@ #define CloudComm_HMAC_SIZE 32 #define HttpURLConnection_HTTP_OK 200 +typedef struct { + int fd; + int numBytes; +} WebConnection; + class CloudComm { private: @@ -54,7 +59,7 @@ private: Array *createIV(int64_t machineId, int64_t localSequenceNumber); Array *encryptSlotAndPrependIV(Array *rawData, Array *ivBytes); Array *stripIVAndDecryptSlot(Array *rawData); - Array *processSlots(int fd); + Array *processSlots(WebConnection *wc); public: @@ -68,7 +73,7 @@ public: */ CloudComm(Table *_table, IoTString *_baseurl, IoTString *_password, int _listeningPort); ~CloudComm(); - + /** * Inits all the security stuff */ diff --git a/version2/src/C/Crypto.cc b/version2/src/C/Crypto.cc index 9b1da86..9fe1154 100644 --- a/version2/src/C/Crypto.cc +++ b/version2/src/C/Crypto.cc @@ -2,16 +2,23 @@ #include "pbkdf2-sha256.h" AESKey::AESKey(Array *password, Array *salt, int iterationCount, int keyLength) { - key = new Array(keyLength); + key = new Array(keyLength / 8); PKCS5_PBKDF2_HMAC((unsigned char *) password->internalArray(), password->length(), (unsigned char *) salt->internalArray(), salt->length(), - iterationCount, keyLength, (unsigned char *) key->internalArray()); + iterationCount, keyLength / 8, (unsigned char *) key->internalArray()); + aes_key_setup((BYTE *)key->internalArray(), key_schedule, keyLength); } AESKey::~AESKey() { + bzero(key->internalArray(), key->length()); delete key; + bzero(key_schedule, sizeof(key_schedule)); } -Array * AESKey::getKey() { +WORD *AESKey::getKeySchedule() { + return (WORD *) &key_schedule; +} + +Array *AESKey::getKey() { return key; } diff --git a/version2/src/C/Crypto.h b/version2/src/C/Crypto.h index 8f64573..5334335 100644 --- a/version2/src/C/Crypto.h +++ b/version2/src/C/Crypto.h @@ -1,15 +1,18 @@ #ifndef CRYPTO_H #define CRYPTO_H #include "common.h" +#include "aes.h" class AESKey { public: AESKey(Array *password, Array *salt, int iterationCount, int keyLength); ~AESKey(); - Array * getKey(); - + Array *getKey(); + WORD *getKeySchedule(); + private: - Array * key; + Array *key; + WORD key_schedule[60]; }; #endif diff --git a/version2/src/C/Error.h b/version2/src/C/Error.h index 44e2845..106ae8e 100644 --- a/version2/src/C/Error.h +++ b/version2/src/C/Error.h @@ -21,8 +21,8 @@ public: class ServerException { char type; - public: - ServerException(const char *msg, char _type) : type(_type) {} +public: + ServerException(const char *msg, char _type) : type(_type) {} char getType() {return type;} }; #endif diff --git a/version2/src/C/IoTString.h b/version2/src/C/IoTString.h index cb2d68b..1fb527f 100644 --- a/version2/src/C/IoTString.h +++ b/version2/src/C/IoTString.h @@ -9,9 +9,9 @@ * @version 1.0 */ -inline int hashCharArray(Array *array) { +inline unsigned int hashCharArray(Array *array) { uint len = array->length(); - int hash = 0; + unsigned int hash = 0; for (uint i = 0; i < len; i++) { hash = 31 * hash + array->get(i); } @@ -22,7 +22,7 @@ class IoTString { private: Array *array; IoTString() {} - int hashvalue; + unsigned int hashvalue; /** * Builds an IoTString object around the char array. This * constructor makes a copy, so the caller is free to modify the char array. @@ -37,7 +37,7 @@ public: IoTString(const char *_array) { int32_t len = strlen(_array); array = new Array(len); - strcpy(array->internalArray(), _array); + memcpy(array->internalArray(), _array, len); hashvalue = hashCharArray(array); } @@ -51,7 +51,7 @@ public: } char get(uint i) {return array->get(i);} - + /** * Internal method to grab a reference to our char array. Caller * must not modify it. @@ -79,7 +79,7 @@ public: return result == 0; } - int hashValue() { return hashvalue;} + unsigned int hashValue() { return hashvalue;} int length() { return array->length(); } friend IoTString *IoTString_shallow(Array *_array); }; @@ -90,7 +90,7 @@ inline IoTString *IoTString_shallow(Array *_array) { return str; } -inline int hashString(IoTString *a) { +inline unsigned int hashString(IoTString *a) { return a->hashValue(); } diff --git a/version2/src/C/Mac.cc b/version2/src/C/Mac.cc index 65c2c25..aec7455 100644 --- a/version2/src/C/Mac.cc +++ b/version2/src/C/Mac.cc @@ -8,9 +8,10 @@ void Mac::update(Array *array, int32_t offset, int32_t len) { sha2_hmac_update(&ctx, (const unsigned char *) &array->internalArray()[offset], len); } -Array * Mac::doFinal() { - Array * hmac = new Array(32); +Array *Mac::doFinal() { + Array *hmac = new Array(32); sha2_hmac_finish(&ctx, (unsigned char *) hmac->internalArray()); + sha2_hmac_reset(&ctx); return hmac; } diff --git a/version2/src/C/Mac.h b/version2/src/C/Mac.h index cb6193b..eebb115 100644 --- a/version2/src/C/Mac.h +++ b/version2/src/C/Mac.h @@ -9,7 +9,7 @@ public: void update(Array *array, int32_t offset, int32_t len); Array *doFinal(); void init(AESKey *key); - private: +private: sha2_context ctx; }; #endif diff --git a/version2/src/C/Makefile b/version2/src/C/Makefile index 11ac1cb..66f289f 100644 --- a/version2/src/C/Makefile +++ b/version2/src/C/Makefile @@ -28,7 +28,7 @@ all: directories ${OBJ_DIR}/$(LIB_SO) test directories: ${OBJ_DIR} test: bin/lib_iotcloud.so - g++ Test.C -L./bin/ -l_iotcloud -lpthread -lbsd -o bin/Test + g++ -g -O0 Test.C -L./bin/ -l_iotcloud -lpthread -lbsd -o bin/Test ${OBJ_DIR}: ${MKDIR_P} ${OBJ_DIR} diff --git a/version2/src/C/SecureRandom.h b/version2/src/C/SecureRandom.h index 50ed6f8..f5d1af0 100644 --- a/version2/src/C/SecureRandom.h +++ b/version2/src/C/SecureRandom.h @@ -3,10 +3,10 @@ #include "common.h" class SecureRandom { - public: +public: SecureRandom(); void nextBytes(Array *array); int32_t nextInt(int32_t val); - private: +private: }; #endif diff --git a/version2/src/C/SlotBuffer.cc b/version2/src/C/SlotBuffer.cc index 2ce4a66..aa217ac 100644 --- a/version2/src/C/SlotBuffer.cc +++ b/version2/src/C/SlotBuffer.cc @@ -36,7 +36,7 @@ void SlotBuffer::resize(int newsize) { int index = tail; for (int i = 0; i < currsize; i++) { newarray->set(i, array->get(index)); - if (((uint32_t)++index) == array->length()) + if (((uint32_t)++ index) == array->length()) index = 0; } array = newarray; diff --git a/version2/src/C/Table.cc b/version2/src/C/Table.cc index df6099a..152b377 100644 --- a/version2/src/C/Table.cc +++ b/version2/src/C/Table.cc @@ -46,6 +46,7 @@ Table::Table(IoTString *baseurl, IoTString *password, int64_t _localMachineId, i oldestLiveSlotSequenceNumver(1), localMachineId(_localMachineId), sequenceNumber(0), + localSequenceNumber(0), localTransactionSequenceNumber(0), lastTransactionSequenceNumberSpeculatedOn(0), oldestTransactionSequenceNumberSpeculatedOn(0), @@ -108,6 +109,7 @@ Table::Table(CloudComm *_cloud, int64_t _localMachineId) : oldestLiveSlotSequenceNumver(1), localMachineId(_localMachineId), sequenceNumber(0), + localSequenceNumber(0), localTransactionSequenceNumber(0), lastTransactionSequenceNumberSpeculatedOn(0), oldestTransactionSequenceNumberSpeculatedOn(0), @@ -205,7 +207,7 @@ void Table::init() { liveNewKeyTable = new Hashtable(); lastMessageTable = new Hashtable * >(); rejectedMessageWatchVectorTable = new Hashtable * >(); - arbitratorTable = new Hashtable(); + arbitratorTable = new Hashtable(); liveAbortTable = new Hashtable *, Abort *, uintptr_t, 0, pairHashFunction, pairEquals>(); newTransactionParts = new Hashtable *, TransactionPart *, uintptr_t, 0, pairHashFunction, pairEquals> *>(); newCommitParts = new Hashtable *, CommitPart *, uintptr_t, 0, pairHashFunction, pairEquals> *>(); @@ -384,7 +386,7 @@ bool Table::update() { bool Table::createNewKey(IoTString *keyName, int64_t machineId) { while (true) { - if (!arbitratorTable->contains(keyName)) { + if (arbitratorTable->contains(keyName)) { // There is already an arbitrator return false; } @@ -1157,7 +1159,7 @@ ThreeTuple *> Table::sendSlotsToServer(Slot *slot, int Array *array = cloud->putSlot(slot, newSize); if (array == NULL) { - array = new Array(); + array = new Array(1); array->set(0, slot); rejectedSlotVector->clear(); inserted = true; diff --git a/version2/src/C/Table.h b/version2/src/C/Table.h index d3d22d7..a97d124 100644 --- a/version2/src/C/Table.h +++ b/version2/src/C/Table.h @@ -3,6 +3,8 @@ #include "common.h" #include "Pair.h" #include "ThreeTuple.h" +#include "IoTString.h" + /** * IoTTable data structure. Provides client interface. * @author Brian Demsky @@ -64,7 +66,7 @@ private: Hashtable *liveNewKeyTable; // Table of live new keys Hashtable *> *lastMessageTable; // Last message sent by a client machine id -> (Seq Num, Slot or LastMessage); Hashtable *> *rejectedMessageWatchVectorTable; // Table of machine Ids and the set of rejected messages they have not seen yet - Hashtable *arbitratorTable;// Table of keys and their arbitrators + Hashtable *arbitratorTable;// Table of keys and their arbitrators Hashtable *, Abort *, uintptr_t, 0, pairHashFunction, pairEquals> *liveAbortTable;// Table live abort messages Hashtable *, TransactionPart *, uintptr_t, 0, pairHashFunction, pairEquals> *> *newTransactionParts; // transaction parts that are seen in this latest round of slots from the server Hashtable *, CommitPart *, uintptr_t, 0, pairHashFunction, pairEquals> *> *newCommitParts; // commit parts that are seen in this latest round of slots from the server @@ -240,7 +242,7 @@ public: Table(IoTString *baseurl, IoTString *password, int64_t _localMachineId, int listeningPort); Table(CloudComm *_cloud, int64_t _localMachineId); ~Table(); - + /** * Initialize the table by inserting a table status as the first entry into the table status * also initialize the crypto stuff. diff --git a/version2/src/C/aes.cc b/version2/src/C/aes.cc index 800395b..a917c7f 100644 --- a/version2/src/C/aes.cc +++ b/version2/src/C/aes.cc @@ -7,12 +7,12 @@ the CTR, CBC, and CCM modes of operation it can be used in. AES is, specified by the NIST in in publication FIPS PUB 197, availible at: - * http://csrc.nist.gov/publications/fips/fips197/fips-197.pdf . +* http://csrc.nist.gov/publications/fips/fips197/fips-197.pdf . The CBC and CTR modes of operation are specified by NIST SP 800-38 A, available at: - * http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf . +* http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf . The CCM mode of operation is specified by NIST SP80-38 C, available at: - * http://csrc.nist.gov/publications/nistpubs/800-38C/SP800-38C_updated-July20_2007.pdf +* http://csrc.nist.gov/publications/nistpubs/800-38C/SP800-38C_updated-July20_2007.pdf *********************************************************************/ /*************************** HEADER FILES ***************************/ @@ -274,7 +274,7 @@ int aes_encrypt_cbc_mac(const BYTE in[], size_t in_len, BYTE out[], const WORD k // Do not output all encrypted blocks. } - memcpy(out, buf_out, AES_BLOCK_SIZE); // Only output the last block. + memcpy(out, buf_out, AES_BLOCK_SIZE); // Only output the last block. return(TRUE); } @@ -339,7 +339,7 @@ void aes_encrypt_ctr(const BYTE in[], size_t in_len, BYTE out[], const WORD key[ } aes_encrypt(iv_buf, out_buf, key, keysize); - xor_buf(out_buf, &out[idx], in_len - idx); // Use the Most Significant bytes. + xor_buf(out_buf, &out[idx], in_len - idx); // Use the Most Significant bytes. } void aes_decrypt_ctr(const BYTE in[], size_t in_len, BYTE out[], const WORD key[], int keysize, const BYTE iv[]) @@ -353,25 +353,25 @@ void aes_decrypt_ctr(const BYTE in[], size_t in_len, BYTE out[], const WORD key[ *******************/ // out_len = payload_len + assoc_len int aes_encrypt_ccm(const BYTE payload[], WORD payload_len, const BYTE assoc[], unsigned short assoc_len, - const BYTE nonce[], unsigned short nonce_len, BYTE out[], WORD *out_len, - WORD mac_len, const BYTE key_str[], int keysize) + const BYTE nonce[], unsigned short nonce_len, BYTE out[], WORD *out_len, + WORD mac_len, const BYTE key_str[], int keysize) { BYTE temp_iv[AES_BLOCK_SIZE], counter[AES_BLOCK_SIZE], mac[16], *buf; int end_of_buf, payload_len_store_size; WORD key[60]; if (mac_len != 4 && mac_len != 6 && mac_len != 8 && mac_len != 10 && - mac_len != 12 && mac_len != 14 && mac_len != 16) + mac_len != 12 && mac_len != 14 && mac_len != 16) return(FALSE); if (nonce_len < 7 || nonce_len > 13) return(FALSE); - if (assoc_len > 32768 /* = 2^15 */) + if (assoc_len > 32768 /* = 2^15 */) return(FALSE); - buf = (BYTE*)malloc(payload_len + assoc_len + 48 /*Round both payload and associated data up a block size and add an extra block.*/); - if (! buf) + buf = (BYTE *)malloc(payload_len + assoc_len + 48 /*Round both payload and associated data up a block size and add an extra block.*/); + if (!buf) return(FALSE); // Prepare the key for usage. @@ -401,7 +401,7 @@ int aes_encrypt_ccm(const BYTE payload[], WORD payload_len, const BYTE assoc[], // Encrypt the Payload with CTR mode with a counter starting at 1. memcpy(temp_iv, counter, AES_BLOCK_SIZE); - increment_iv(temp_iv, AES_BLOCK_SIZE - 1 - mac_len); // Last argument is the byte size of the counting portion of the counter block. /*BUG?*/ + increment_iv(temp_iv, AES_BLOCK_SIZE - 1 - mac_len); // Last argument is the byte size of the counting portion of the counter block. /*BUG?*/ aes_encrypt_ctr(out, payload_len, out, key, keysize, temp_iv); // Encrypt the MAC with CTR mode with a counter starting at 0. @@ -416,8 +416,8 @@ int aes_encrypt_ccm(const BYTE payload[], WORD payload_len, const BYTE assoc[], // plaintext_len = ciphertext_len - mac_len // Needs a flag for whether the MAC matches. int aes_decrypt_ccm(const BYTE ciphertext[], WORD ciphertext_len, const BYTE assoc[], unsigned short assoc_len, - const BYTE nonce[], unsigned short nonce_len, BYTE plaintext[], WORD *plaintext_len, - WORD mac_len, int *mac_auth, const BYTE key_str[], int keysize) + const BYTE nonce[], unsigned short nonce_len, BYTE plaintext[], WORD *plaintext_len, + WORD mac_len, int *mac_auth, const BYTE key_str[], int keysize) { BYTE temp_iv[AES_BLOCK_SIZE], counter[AES_BLOCK_SIZE], mac[16], mac_buf[16], *buf; int end_of_buf, plaintext_len_store_size; @@ -426,8 +426,8 @@ int aes_decrypt_ccm(const BYTE ciphertext[], WORD ciphertext_len, const BYTE ass if (ciphertext_len <= mac_len) return(FALSE); - buf = (BYTE*)malloc(assoc_len + ciphertext_len /*ciphertext_len = plaintext_len + mac_len*/ + 48); - if (! buf) + buf = (BYTE *)malloc(assoc_len + ciphertext_len /*ciphertext_len = plaintext_len + mac_len*/ + 48); + if (!buf) return(FALSE); // Prepare the key for usage. @@ -444,7 +444,7 @@ int aes_decrypt_ccm(const BYTE ciphertext[], WORD ciphertext_len, const BYTE ass // Decrypt the Payload with CTR mode with a counter starting at 1. memcpy(temp_iv, counter, AES_BLOCK_SIZE); - increment_iv(temp_iv, AES_BLOCK_SIZE - 1 - mac_len); // (AES_BLOCK_SIZE - 1 - mac_len) is the byte size of the counting portion of the counter block. + increment_iv(temp_iv, AES_BLOCK_SIZE - 1 - mac_len); // (AES_BLOCK_SIZE - 1 - mac_len) is the byte size of the counting portion of the counter block. aes_decrypt_ctr(plaintext, *plaintext_len, plaintext, key, keysize, temp_iv); // Setting mac_auth to NULL disables the authentication check. @@ -468,7 +468,7 @@ int aes_decrypt_ccm(const BYTE ciphertext[], WORD ciphertext_len, const BYTE ass aes_encrypt_cbc_mac(buf, end_of_buf, mac_buf, key, keysize, temp_iv); // Compare the calculated MAC against the MAC embedded in the ciphertext to see if they are the same. - if (! memcmp(mac, mac_buf, mac_len)) { + if (!memcmp(mac, mac_buf, mac_len)) { *mac_auth = TRUE; } else { @@ -512,7 +512,7 @@ void ccm_format_assoc_data(BYTE buf[], int *end_of_buf, const BYTE assoc[], int *end_of_buf += 2; memcpy(&buf[*end_of_buf], assoc, assoc_len); *end_of_buf += assoc_len; - pad = AES_BLOCK_SIZE - (*end_of_buf % AES_BLOCK_SIZE); /*BUG?*/ + pad = AES_BLOCK_SIZE - (*end_of_buf % AES_BLOCK_SIZE);/*BUG?*/ memset(&buf[*end_of_buf], 0, pad); *end_of_buf += pad; } @@ -554,30 +554,30 @@ WORD SubWord(WORD word) // "keysize" is the length in bits of "key", must be 128, 192, or 256. void aes_key_setup(const BYTE key[], WORD w[], int keysize) { - int Nb=4,Nr,Nk,idx; - WORD temp,Rcon[]={0x01000000,0x02000000,0x04000000,0x08000000,0x10000000,0x20000000, - 0x40000000,0x80000000,0x1b000000,0x36000000,0x6c000000,0xd8000000, - 0xab000000,0x4d000000,0x9a000000}; + int Nb = 4,Nr,Nk,idx; + WORD temp,Rcon[] = {0x01000000,0x02000000,0x04000000,0x08000000,0x10000000,0x20000000, + 0x40000000,0x80000000,0x1b000000,0x36000000,0x6c000000,0xd8000000, + 0xab000000,0x4d000000,0x9a000000}; switch (keysize) { - case 128: Nr = 10; Nk = 4; break; - case 192: Nr = 12; Nk = 6; break; - case 256: Nr = 14; Nk = 8; break; - default: return; + case 128: Nr = 10; Nk = 4; break; + case 192: Nr = 12; Nk = 6; break; + case 256: Nr = 14; Nk = 8; break; + default: return; } - for (idx=0; idx < Nk; ++idx) { + for (idx = 0; idx < Nk; ++idx) { w[idx] = ((key[4 * idx]) << 24) | ((key[4 * idx + 1]) << 16) | - ((key[4 * idx + 2]) << 8) | ((key[4 * idx + 3])); + ((key[4 * idx + 2]) << 8) | ((key[4 * idx + 3])); } - for (idx = Nk; idx < Nb * (Nr+1); ++idx) { + for (idx = Nk; idx < Nb * (Nr + 1); ++idx) { temp = w[idx - 1]; if ((idx % Nk) == 0) - temp = SubWord(KE_ROTWORD(temp)) ^ Rcon[(idx-1)/Nk]; + temp = SubWord(KE_ROTWORD(temp)) ^ Rcon[(idx - 1) / Nk]; else if (Nk > 6 && (idx % Nk) == 4) temp = SubWord(temp); - w[idx] = w[idx-Nk] ^ temp; + w[idx] = w[idx - Nk] ^ temp; } } @@ -1072,24 +1072,24 @@ void aes_decrypt(const BYTE in[], BYTE out[], const WORD key[], int keysize) ** AES DEBUGGING FUNCTIONS *******************/ /* -// This prints the "state" grid as a linear hex string. -void print_state(BYTE state[][4]) -{ - int idx,idx2; - - for (idx=0; idx < 4; idx++) - for (idx2=0; idx2 < 4; idx2++) - printf("%02x",state[idx2][idx]); - printf("\n"); -} - -// This prints the key (4 consecutive ints) used for a given round as a linear hex string. -void print_rnd_key(WORD key[]) -{ - int idx; - - for (idx=0; idx < 4; idx++) - printf("%08x",key[idx]); - printf("\n"); -} -*/ + // This prints the "state" grid as a linear hex string. + void print_state(BYTE state[][4]) + { + int idx,idx2; + + for (idx=0; idx < 4; idx++) + for (idx2=0; idx2 < 4; idx2++) + printf("%02x",state[idx2][idx]); + printf("\n"); + } + + // This prints the key (4 consecutive ints) used for a given round as a linear hex string. + void print_rnd_key(WORD key[]) + { + int idx; + + for (idx=0; idx < 4; idx++) + printf("%08x",key[idx]); + printf("\n"); + } + */ diff --git a/version2/src/C/aes.h b/version2/src/C/aes.h index 25721c8..b4a1440 100644 --- a/version2/src/C/aes.h +++ b/version2/src/C/aes.h @@ -13,84 +13,84 @@ #include /****************************** MACROS ******************************/ -#define AES_BLOCK_SIZE 16 // AES operates on 16 bytes at a time +#define AES_BLOCK_SIZE 16 // AES operates on 16 bytes at a time /**************************** DATA TYPES ****************************/ -typedef unsigned char BYTE; // 8-bit byte -typedef unsigned int WORD; // 32-bit word, change to "long" for 16-bit machines +typedef unsigned char BYTE; // 8-bit byte +typedef unsigned int WORD; // 32-bit word, change to "long" for 16-bit machines /*********************** FUNCTION DECLARATIONS **********************/ /////////////////// // AES /////////////////// // Key setup must be done before any AES en/de-cryption functions can be used. -void aes_key_setup(const BYTE key[], // The key, must be 128, 192, or 256 bits - WORD w[], // Output key schedule to be used later - int keysize); // Bit length of the key, 128, 192, or 256 +void aes_key_setup(const BYTE key[], // The key, must be 128, 192, or 256 bits + WORD w[], // Output key schedule to be used later + int keysize); // Bit length of the key, 128, 192, or 256 -void aes_encrypt(const BYTE in[], // 16 bytes of plaintext - BYTE out[], // 16 bytes of ciphertext - const WORD key[], // From the key setup - int keysize); // Bit length of the key, 128, 192, or 256 +void aes_encrypt(const BYTE in[], // 16 bytes of plaintext + BYTE out[], // 16 bytes of ciphertext + const WORD key[], // From the key setup + int keysize); // Bit length of the key, 128, 192, or 256 -void aes_decrypt(const BYTE in[], // 16 bytes of ciphertext - BYTE out[], // 16 bytes of plaintext - const WORD key[], // From the key setup - int keysize); // Bit length of the key, 128, 192, or 256 +void aes_decrypt(const BYTE in[], // 16 bytes of ciphertext + BYTE out[], // 16 bytes of plaintext + const WORD key[], // From the key setup + int keysize); // Bit length of the key, 128, 192, or 256 /////////////////// // AES - CBC /////////////////// -int aes_encrypt_cbc(const BYTE in[], // Plaintext - size_t in_len, // Must be a multiple of AES_BLOCK_SIZE - BYTE out[], // Ciphertext, same length as plaintext - const WORD key[], // From the key setup - int keysize, // Bit length of the key, 128, 192, or 256 - const BYTE iv[]); // IV, must be AES_BLOCK_SIZE bytes long +int aes_encrypt_cbc(const BYTE in[], // Plaintext + size_t in_len, // Must be a multiple of AES_BLOCK_SIZE + BYTE out[], // Ciphertext, same length as plaintext + const WORD key[], // From the key setup + int keysize, // Bit length of the key, 128, 192, or 256 + const BYTE iv[]); // IV, must be AES_BLOCK_SIZE bytes long // Only output the CBC-MAC of the input. -int aes_encrypt_cbc_mac(const BYTE in[], // plaintext - size_t in_len, // Must be a multiple of AES_BLOCK_SIZE - BYTE out[], // Output MAC - const WORD key[], // From the key setup - int keysize, // Bit length of the key, 128, 192, or 256 - const BYTE iv[]); // IV, must be AES_BLOCK_SIZE bytes long +int aes_encrypt_cbc_mac(const BYTE in[], // plaintext + size_t in_len, // Must be a multiple of AES_BLOCK_SIZE + BYTE out[], // Output MAC + const WORD key[], // From the key setup + int keysize, // Bit length of the key, 128, 192, or 256 + const BYTE iv[]); // IV, must be AES_BLOCK_SIZE bytes long /////////////////// // AES - CTR /////////////////// -void increment_iv(BYTE iv[], // Must be a multiple of AES_BLOCK_SIZE - int counter_size); // Bytes of the IV used for counting (low end) - -void aes_encrypt_ctr(const BYTE in[], // Plaintext - size_t in_len, // Any byte length - BYTE out[], // Ciphertext, same length as plaintext - const WORD key[], // From the key setup - int keysize, // Bit length of the key, 128, 192, or 256 - const BYTE iv[]); // IV, must be AES_BLOCK_SIZE bytes long - -void aes_decrypt_ctr(const BYTE in[], // Ciphertext - size_t in_len, // Any byte length - BYTE out[], // Plaintext, same length as ciphertext - const WORD key[], // From the key setup - int keysize, // Bit length of the key, 128, 192, or 256 - const BYTE iv[]); // IV, must be AES_BLOCK_SIZE bytes long +void increment_iv(BYTE iv[], // Must be a multiple of AES_BLOCK_SIZE + int counter_size); // Bytes of the IV used for counting (low end) + +void aes_encrypt_ctr(const BYTE in[], // Plaintext + size_t in_len, // Any byte length + BYTE out[], // Ciphertext, same length as plaintext + const WORD key[], // From the key setup + int keysize, // Bit length of the key, 128, 192, or 256 + const BYTE iv[]); // IV, must be AES_BLOCK_SIZE bytes long + +void aes_decrypt_ctr(const BYTE in[], // Ciphertext + size_t in_len, // Any byte length + BYTE out[], // Plaintext, same length as ciphertext + const WORD key[], // From the key setup + int keysize, // Bit length of the key, 128, 192, or 256 + const BYTE iv[]); // IV, must be AES_BLOCK_SIZE bytes long /////////////////// // AES - CCM /////////////////// // Returns True if the input parameters do not violate any constraint. -int aes_encrypt_ccm(const BYTE plaintext[], // IN - Plaintext. - WORD plaintext_len, // IN - Plaintext length. - const BYTE associated_data[], // IN - Associated Data included in authentication, but not encryption. - unsigned short associated_data_len, // IN - Associated Data length in bytes. - const BYTE nonce[], // IN - The Nonce to be used for encryption. - unsigned short nonce_len, // IN - Nonce length in bytes. - BYTE ciphertext[], // OUT - Ciphertext, a concatination of the plaintext and the MAC. - WORD *ciphertext_len, // OUT - The length of the ciphertext, always plaintext_len + mac_len. - WORD mac_len, // IN - The desired length of the MAC, must be 4, 6, 8, 10, 12, 14, or 16. - const BYTE key[], // IN - The AES key for encryption. - int keysize); // IN - The length of the key in bits. Valid values are 128, 192, 256. +int aes_encrypt_ccm(const BYTE plaintext[], // IN - Plaintext. + WORD plaintext_len, // IN - Plaintext length. + const BYTE associated_data[], // IN - Associated Data included in authentication, but not encryption. + unsigned short associated_data_len, // IN - Associated Data length in bytes. + const BYTE nonce[], // IN - The Nonce to be used for encryption. + unsigned short nonce_len, // IN - Nonce length in bytes. + BYTE ciphertext[], // OUT - Ciphertext, a concatination of the plaintext and the MAC. + WORD *ciphertext_len, // OUT - The length of the ciphertext, always plaintext_len + mac_len. + WORD mac_len, // IN - The desired length of the MAC, must be 4, 6, 8, 10, 12, 14, or 16. + const BYTE key[], // IN - The AES key for encryption. + int keysize); // IN - The length of the key in bits. Valid values are 128, 192, 256. // Returns True if the input parameters do not violate any constraint. // Use mac_auth to ensure decryption/validation was preformed correctly. @@ -98,18 +98,18 @@ int aes_encrypt_ccm(const BYTE plaintext[], // IN - Plaintext. // this, call with mac_auth = NULL. The proper proceedure is to decrypt with // authentication enabled (mac_auth != NULL) and make a second call to that // ignores authentication explicitly if the first call failes. -int aes_decrypt_ccm(const BYTE ciphertext[], // IN - Ciphertext, the concatination of encrypted plaintext and MAC. - WORD ciphertext_len, // IN - Ciphertext length in bytes. - const BYTE assoc[], // IN - The Associated Data, required for authentication. - unsigned short assoc_len, // IN - Associated Data length in bytes. - const BYTE nonce[], // IN - The Nonce to use for decryption, same one as for encryption. - unsigned short nonce_len, // IN - Nonce length in bytes. - BYTE plaintext[], // OUT - The plaintext that was decrypted. Will need to be large enough to hold ciphertext_len - mac_len. - WORD *plaintext_len, // OUT - Length in bytes of the output plaintext, always ciphertext_len - mac_len . - WORD mac_len, // IN - The length of the MAC that was calculated. - int *mac_auth, // OUT - TRUE if authentication succeeded, FALSE if it did not. NULL pointer will ignore the authentication. - const BYTE key[], // IN - The AES key for decryption. - int keysize); // IN - The length of the key in BITS. Valid values are 128, 192, 256. +int aes_decrypt_ccm(const BYTE ciphertext[], // IN - Ciphertext, the concatination of encrypted plaintext and MAC. + WORD ciphertext_len, // IN - Ciphertext length in bytes. + const BYTE assoc[], // IN - The Associated Data, required for authentication. + unsigned short assoc_len, // IN - Associated Data length in bytes. + const BYTE nonce[], // IN - The Nonce to use for decryption, same one as for encryption. + unsigned short nonce_len, // IN - Nonce length in bytes. + BYTE plaintext[], // OUT - The plaintext that was decrypted. Will need to be large enough to hold ciphertext_len - mac_len. + WORD *plaintext_len, // OUT - Length in bytes of the output plaintext, always ciphertext_len - mac_len . + WORD mac_len, // IN - The length of the MAC that was calculated. + int *mac_auth, // OUT - TRUE if authentication succeeded, FALSE if it did not. NULL pointer will ignore the authentication. + const BYTE key[], // IN - The AES key for decryption. + int keysize); // IN - The length of the key in BITS. Valid values are 128, 192, 256. /////////////////// // Test functions @@ -120,4 +120,4 @@ int aes_cbc_test(); int aes_ctr_test(); int aes_ccm_test(); -#endif // AES_H +#endif // AES_H diff --git a/version2/src/C/pbkdf2-sha256.cc b/version2/src/C/pbkdf2-sha256.cc index cd8abeb..2a78fa0 100644 --- a/version2/src/C/pbkdf2-sha256.cc +++ b/version2/src/C/pbkdf2-sha256.cc @@ -38,22 +38,22 @@ */ #ifndef GET_ULONG_BE #define GET_ULONG_BE(n,b,i) \ -{ \ - (n) = ( (unsigned long) (b)[(i) ] << 24 ) \ - | ( (unsigned long) (b)[(i) + 1] << 16 ) \ - | ( (unsigned long) (b)[(i) + 2] << 8 ) \ - | ( (unsigned long) (b)[(i) + 3] ); \ -} + { \ + (n) = ( (unsigned long) (b)[(i) ] << 24) \ + | ( (unsigned long) (b)[(i) + 1] << 16) \ + | ( (unsigned long) (b)[(i) + 2] << 8) \ + | ( (unsigned long) (b)[(i) + 3]); \ + } #endif #ifndef PUT_ULONG_BE #define PUT_ULONG_BE(n,b,i) \ -{ \ - (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \ - (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \ - (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \ - (b)[(i) + 3] = (unsigned char) ( (n) ); \ -} + { \ + (b)[(i) ] = (unsigned char) ( (n) >> 24); \ + (b)[(i) + 1] = (unsigned char) ( (n) >> 16); \ + (b)[(i) + 2] = (unsigned char) ( (n) >> 8); \ + (b)[(i) + 3] = (unsigned char) ( (n) ); \ + } #endif /* @@ -61,58 +61,58 @@ */ void sha2_starts( sha2_context *ctx, int is224 ) { - ctx->total[0] = 0; - ctx->total[1] = 0; - - if( is224 == 0 ) - { - /* SHA-256 */ - ctx->state[0] = 0x6A09E667; - ctx->state[1] = 0xBB67AE85; - ctx->state[2] = 0x3C6EF372; - ctx->state[3] = 0xA54FF53A; - ctx->state[4] = 0x510E527F; - ctx->state[5] = 0x9B05688C; - ctx->state[6] = 0x1F83D9AB; - ctx->state[7] = 0x5BE0CD19; - } - else - { - /* SHA-224 */ - ctx->state[0] = 0xC1059ED8; - ctx->state[1] = 0x367CD507; - ctx->state[2] = 0x3070DD17; - ctx->state[3] = 0xF70E5939; - ctx->state[4] = 0xFFC00B31; - ctx->state[5] = 0x68581511; - ctx->state[6] = 0x64F98FA7; - ctx->state[7] = 0xBEFA4FA4; - } - - ctx->is224 = is224; + ctx->total[0] = 0; + ctx->total[1] = 0; + + if ( is224 == 0 ) + { + /* SHA-256 */ + ctx->state[0] = 0x6A09E667; + ctx->state[1] = 0xBB67AE85; + ctx->state[2] = 0x3C6EF372; + ctx->state[3] = 0xA54FF53A; + ctx->state[4] = 0x510E527F; + ctx->state[5] = 0x9B05688C; + ctx->state[6] = 0x1F83D9AB; + ctx->state[7] = 0x5BE0CD19; + } + else + { + /* SHA-224 */ + ctx->state[0] = 0xC1059ED8; + ctx->state[1] = 0x367CD507; + ctx->state[2] = 0x3070DD17; + ctx->state[3] = 0xF70E5939; + ctx->state[4] = 0xFFC00B31; + ctx->state[5] = 0x68581511; + ctx->state[6] = 0x64F98FA7; + ctx->state[7] = 0xBEFA4FA4; + } + + ctx->is224 = is224; } static void sha2_process( sha2_context *ctx, const unsigned char data[64] ) { - unsigned long temp1, temp2, W[64]; - unsigned long A, B, C, D, E, F, G, H; - - GET_ULONG_BE( W[ 0], data, 0 ); - GET_ULONG_BE( W[ 1], data, 4 ); - GET_ULONG_BE( W[ 2], data, 8 ); - GET_ULONG_BE( W[ 3], data, 12 ); - GET_ULONG_BE( W[ 4], data, 16 ); - GET_ULONG_BE( W[ 5], data, 20 ); - GET_ULONG_BE( W[ 6], data, 24 ); - GET_ULONG_BE( W[ 7], data, 28 ); - GET_ULONG_BE( W[ 8], data, 32 ); - GET_ULONG_BE( W[ 9], data, 36 ); - GET_ULONG_BE( W[10], data, 40 ); - GET_ULONG_BE( W[11], data, 44 ); - GET_ULONG_BE( W[12], data, 48 ); - GET_ULONG_BE( W[13], data, 52 ); - GET_ULONG_BE( W[14], data, 56 ); - GET_ULONG_BE( W[15], data, 60 ); + unsigned long temp1, temp2, W[64]; + unsigned long A, B, C, D, E, F, G, H; + + GET_ULONG_BE( W[ 0], data, 0 ); + GET_ULONG_BE( W[ 1], data, 4 ); + GET_ULONG_BE( W[ 2], data, 8 ); + GET_ULONG_BE( W[ 3], data, 12 ); + GET_ULONG_BE( W[ 4], data, 16 ); + GET_ULONG_BE( W[ 5], data, 20 ); + GET_ULONG_BE( W[ 6], data, 24 ); + GET_ULONG_BE( W[ 7], data, 28 ); + GET_ULONG_BE( W[ 8], data, 32 ); + GET_ULONG_BE( W[ 9], data, 36 ); + GET_ULONG_BE( W[10], data, 40 ); + GET_ULONG_BE( W[11], data, 44 ); + GET_ULONG_BE( W[12], data, 48 ); + GET_ULONG_BE( W[13], data, 52 ); + GET_ULONG_BE( W[14], data, 56 ); + GET_ULONG_BE( W[15], data, 60 ); #define SHR(x,n) ((x & 0xFFFFFFFF) >> n) #define ROTR(x,n) (SHR(x,n) | (x << (32 - n))) @@ -127,17 +127,17 @@ static void sha2_process( sha2_context *ctx, const unsigned char data[64] ) #define F1(x,y,z) (z ^ (x & (y ^ z))) #define R(t) \ -( \ - W[t] = S1(W[t - 2]) + W[t - 7] + \ - S0(W[t - 15]) + W[t - 16] \ -) + ( \ + W[t] = S1(W[t - 2]) + W[t - 7] + \ + S0(W[t - 15]) + W[t - 16] \ + ) #define P(a,b,c,d,e,f,g,h,x,K) \ -{ \ - temp1 = h + S3(e) + F1(e,f,g) + K + x; \ - temp2 = S2(a) + F0(a,b,c); \ - d += temp1; h = temp1 + temp2; \ -} + { \ + temp1 = h + S3(e) + F1(e,f,g) + K + x; \ + temp2 = S2(a) + F0(a,b,c); \ + d += temp1; h = temp1 + temp2; \ + } A = ctx->state[0]; B = ctx->state[1]; @@ -148,79 +148,79 @@ static void sha2_process( sha2_context *ctx, const unsigned char data[64] ) G = ctx->state[6]; H = ctx->state[7]; - P( A, B, C, D, E, F, G, H, W[ 0], 0x428A2F98 ); - P( H, A, B, C, D, E, F, G, W[ 1], 0x71374491 ); - P( G, H, A, B, C, D, E, F, W[ 2], 0xB5C0FBCF ); - P( F, G, H, A, B, C, D, E, W[ 3], 0xE9B5DBA5 ); - P( E, F, G, H, A, B, C, D, W[ 4], 0x3956C25B ); - P( D, E, F, G, H, A, B, C, W[ 5], 0x59F111F1 ); - P( C, D, E, F, G, H, A, B, W[ 6], 0x923F82A4 ); - P( B, C, D, E, F, G, H, A, W[ 7], 0xAB1C5ED5 ); - P( A, B, C, D, E, F, G, H, W[ 8], 0xD807AA98 ); - P( H, A, B, C, D, E, F, G, W[ 9], 0x12835B01 ); - P( G, H, A, B, C, D, E, F, W[10], 0x243185BE ); - P( F, G, H, A, B, C, D, E, W[11], 0x550C7DC3 ); - P( E, F, G, H, A, B, C, D, W[12], 0x72BE5D74 ); - P( D, E, F, G, H, A, B, C, W[13], 0x80DEB1FE ); - P( C, D, E, F, G, H, A, B, W[14], 0x9BDC06A7 ); - P( B, C, D, E, F, G, H, A, W[15], 0xC19BF174 ); - P( A, B, C, D, E, F, G, H, R(16), 0xE49B69C1 ); - P( H, A, B, C, D, E, F, G, R(17), 0xEFBE4786 ); - P( G, H, A, B, C, D, E, F, R(18), 0x0FC19DC6 ); - P( F, G, H, A, B, C, D, E, R(19), 0x240CA1CC ); - P( E, F, G, H, A, B, C, D, R(20), 0x2DE92C6F ); - P( D, E, F, G, H, A, B, C, R(21), 0x4A7484AA ); - P( C, D, E, F, G, H, A, B, R(22), 0x5CB0A9DC ); - P( B, C, D, E, F, G, H, A, R(23), 0x76F988DA ); - P( A, B, C, D, E, F, G, H, R(24), 0x983E5152 ); - P( H, A, B, C, D, E, F, G, R(25), 0xA831C66D ); - P( G, H, A, B, C, D, E, F, R(26), 0xB00327C8 ); - P( F, G, H, A, B, C, D, E, R(27), 0xBF597FC7 ); - P( E, F, G, H, A, B, C, D, R(28), 0xC6E00BF3 ); - P( D, E, F, G, H, A, B, C, R(29), 0xD5A79147 ); - P( C, D, E, F, G, H, A, B, R(30), 0x06CA6351 ); - P( B, C, D, E, F, G, H, A, R(31), 0x14292967 ); - P( A, B, C, D, E, F, G, H, R(32), 0x27B70A85 ); - P( H, A, B, C, D, E, F, G, R(33), 0x2E1B2138 ); - P( G, H, A, B, C, D, E, F, R(34), 0x4D2C6DFC ); - P( F, G, H, A, B, C, D, E, R(35), 0x53380D13 ); - P( E, F, G, H, A, B, C, D, R(36), 0x650A7354 ); - P( D, E, F, G, H, A, B, C, R(37), 0x766A0ABB ); - P( C, D, E, F, G, H, A, B, R(38), 0x81C2C92E ); - P( B, C, D, E, F, G, H, A, R(39), 0x92722C85 ); - P( A, B, C, D, E, F, G, H, R(40), 0xA2BFE8A1 ); - P( H, A, B, C, D, E, F, G, R(41), 0xA81A664B ); - P( G, H, A, B, C, D, E, F, R(42), 0xC24B8B70 ); - P( F, G, H, A, B, C, D, E, R(43), 0xC76C51A3 ); - P( E, F, G, H, A, B, C, D, R(44), 0xD192E819 ); - P( D, E, F, G, H, A, B, C, R(45), 0xD6990624 ); - P( C, D, E, F, G, H, A, B, R(46), 0xF40E3585 ); - P( B, C, D, E, F, G, H, A, R(47), 0x106AA070 ); - P( A, B, C, D, E, F, G, H, R(48), 0x19A4C116 ); - P( H, A, B, C, D, E, F, G, R(49), 0x1E376C08 ); - P( G, H, A, B, C, D, E, F, R(50), 0x2748774C ); - P( F, G, H, A, B, C, D, E, R(51), 0x34B0BCB5 ); - P( E, F, G, H, A, B, C, D, R(52), 0x391C0CB3 ); - P( D, E, F, G, H, A, B, C, R(53), 0x4ED8AA4A ); - P( C, D, E, F, G, H, A, B, R(54), 0x5B9CCA4F ); - P( B, C, D, E, F, G, H, A, R(55), 0x682E6FF3 ); - P( A, B, C, D, E, F, G, H, R(56), 0x748F82EE ); - P( H, A, B, C, D, E, F, G, R(57), 0x78A5636F ); - P( G, H, A, B, C, D, E, F, R(58), 0x84C87814 ); - P( F, G, H, A, B, C, D, E, R(59), 0x8CC70208 ); - P( E, F, G, H, A, B, C, D, R(60), 0x90BEFFFA ); - P( D, E, F, G, H, A, B, C, R(61), 0xA4506CEB ); - P( C, D, E, F, G, H, A, B, R(62), 0xBEF9A3F7 ); - P( B, C, D, E, F, G, H, A, R(63), 0xC67178F2 ); - - ctx->state[0] += A; - ctx->state[1] += B; - ctx->state[2] += C; - ctx->state[3] += D; - ctx->state[4] += E; - ctx->state[5] += F; - ctx->state[6] += G; - ctx->state[7] += H; + P( A, B, C, D, E, F, G, H, W[ 0], 0x428A2F98 ); + P( H, A, B, C, D, E, F, G, W[ 1], 0x71374491 ); + P( G, H, A, B, C, D, E, F, W[ 2], 0xB5C0FBCF ); + P( F, G, H, A, B, C, D, E, W[ 3], 0xE9B5DBA5 ); + P( E, F, G, H, A, B, C, D, W[ 4], 0x3956C25B ); + P( D, E, F, G, H, A, B, C, W[ 5], 0x59F111F1 ); + P( C, D, E, F, G, H, A, B, W[ 6], 0x923F82A4 ); + P( B, C, D, E, F, G, H, A, W[ 7], 0xAB1C5ED5 ); + P( A, B, C, D, E, F, G, H, W[ 8], 0xD807AA98 ); + P( H, A, B, C, D, E, F, G, W[ 9], 0x12835B01 ); + P( G, H, A, B, C, D, E, F, W[10], 0x243185BE ); + P( F, G, H, A, B, C, D, E, W[11], 0x550C7DC3 ); + P( E, F, G, H, A, B, C, D, W[12], 0x72BE5D74 ); + P( D, E, F, G, H, A, B, C, W[13], 0x80DEB1FE ); + P( C, D, E, F, G, H, A, B, W[14], 0x9BDC06A7 ); + P( B, C, D, E, F, G, H, A, W[15], 0xC19BF174 ); + P( A, B, C, D, E, F, G, H, R(16), 0xE49B69C1 ); + P( H, A, B, C, D, E, F, G, R(17), 0xEFBE4786 ); + P( G, H, A, B, C, D, E, F, R(18), 0x0FC19DC6 ); + P( F, G, H, A, B, C, D, E, R(19), 0x240CA1CC ); + P( E, F, G, H, A, B, C, D, R(20), 0x2DE92C6F ); + P( D, E, F, G, H, A, B, C, R(21), 0x4A7484AA ); + P( C, D, E, F, G, H, A, B, R(22), 0x5CB0A9DC ); + P( B, C, D, E, F, G, H, A, R(23), 0x76F988DA ); + P( A, B, C, D, E, F, G, H, R(24), 0x983E5152 ); + P( H, A, B, C, D, E, F, G, R(25), 0xA831C66D ); + P( G, H, A, B, C, D, E, F, R(26), 0xB00327C8 ); + P( F, G, H, A, B, C, D, E, R(27), 0xBF597FC7 ); + P( E, F, G, H, A, B, C, D, R(28), 0xC6E00BF3 ); + P( D, E, F, G, H, A, B, C, R(29), 0xD5A79147 ); + P( C, D, E, F, G, H, A, B, R(30), 0x06CA6351 ); + P( B, C, D, E, F, G, H, A, R(31), 0x14292967 ); + P( A, B, C, D, E, F, G, H, R(32), 0x27B70A85 ); + P( H, A, B, C, D, E, F, G, R(33), 0x2E1B2138 ); + P( G, H, A, B, C, D, E, F, R(34), 0x4D2C6DFC ); + P( F, G, H, A, B, C, D, E, R(35), 0x53380D13 ); + P( E, F, G, H, A, B, C, D, R(36), 0x650A7354 ); + P( D, E, F, G, H, A, B, C, R(37), 0x766A0ABB ); + P( C, D, E, F, G, H, A, B, R(38), 0x81C2C92E ); + P( B, C, D, E, F, G, H, A, R(39), 0x92722C85 ); + P( A, B, C, D, E, F, G, H, R(40), 0xA2BFE8A1 ); + P( H, A, B, C, D, E, F, G, R(41), 0xA81A664B ); + P( G, H, A, B, C, D, E, F, R(42), 0xC24B8B70 ); + P( F, G, H, A, B, C, D, E, R(43), 0xC76C51A3 ); + P( E, F, G, H, A, B, C, D, R(44), 0xD192E819 ); + P( D, E, F, G, H, A, B, C, R(45), 0xD6990624 ); + P( C, D, E, F, G, H, A, B, R(46), 0xF40E3585 ); + P( B, C, D, E, F, G, H, A, R(47), 0x106AA070 ); + P( A, B, C, D, E, F, G, H, R(48), 0x19A4C116 ); + P( H, A, B, C, D, E, F, G, R(49), 0x1E376C08 ); + P( G, H, A, B, C, D, E, F, R(50), 0x2748774C ); + P( F, G, H, A, B, C, D, E, R(51), 0x34B0BCB5 ); + P( E, F, G, H, A, B, C, D, R(52), 0x391C0CB3 ); + P( D, E, F, G, H, A, B, C, R(53), 0x4ED8AA4A ); + P( C, D, E, F, G, H, A, B, R(54), 0x5B9CCA4F ); + P( B, C, D, E, F, G, H, A, R(55), 0x682E6FF3 ); + P( A, B, C, D, E, F, G, H, R(56), 0x748F82EE ); + P( H, A, B, C, D, E, F, G, R(57), 0x78A5636F ); + P( G, H, A, B, C, D, E, F, R(58), 0x84C87814 ); + P( F, G, H, A, B, C, D, E, R(59), 0x8CC70208 ); + P( E, F, G, H, A, B, C, D, R(60), 0x90BEFFFA ); + P( D, E, F, G, H, A, B, C, R(61), 0xA4506CEB ); + P( C, D, E, F, G, H, A, B, R(62), 0xBEF9A3F7 ); + P( B, C, D, E, F, G, H, A, R(63), 0xC67178F2 ); + + ctx->state[0] += A; + ctx->state[1] += B; + ctx->state[2] += C; + ctx->state[3] += D; + ctx->state[4] += E; + ctx->state[5] += F; + ctx->state[6] += G; + ctx->state[7] += H; } /* @@ -228,51 +228,51 @@ static void sha2_process( sha2_context *ctx, const unsigned char data[64] ) */ void sha2_update( sha2_context *ctx, const unsigned char *input, size_t ilen ) { - size_t fill; - unsigned long left; - - if( ilen <= 0 ) - return; - - left = ctx->total[0] & 0x3F; - fill = 64 - left; - - ctx->total[0] += (unsigned long) ilen; - ctx->total[0] &= 0xFFFFFFFF; - - if( ctx->total[0] < (unsigned long) ilen ) - ctx->total[1]++; - - if( left && ilen >= fill ) - { - memcpy( (void *) (ctx->buffer + left), - (void *) input, fill ); - sha2_process( ctx, ctx->buffer ); - input += fill; - ilen -= fill; - left = 0; - } - - while( ilen >= 64 ) - { - sha2_process( ctx, input ); - input += 64; - ilen -= 64; - } - - if( ilen > 0 ) - { - memcpy( (void *) (ctx->buffer + left), - (void *) input, ilen ); - } + size_t fill; + unsigned long left; + + if ( ilen <= 0 ) + return; + + left = ctx->total[0] & 0x3F; + fill = 64 - left; + + ctx->total[0] += (unsigned long) ilen; + ctx->total[0] &= 0xFFFFFFFF; + + if ( ctx->total[0] < (unsigned long) ilen ) + ctx->total[1]++; + + if ( left && ilen >= fill ) + { + memcpy( (void *) (ctx->buffer + left), + (void *) input, fill ); + sha2_process( ctx, ctx->buffer ); + input += fill; + ilen -= fill; + left = 0; + } + + while ( ilen >= 64 ) + { + sha2_process( ctx, input ); + input += 64; + ilen -= 64; + } + + if ( ilen > 0 ) + { + memcpy( (void *) (ctx->buffer + left), + (void *) input, ilen ); + } } static const unsigned char sha2_padding[64] = { - 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 + 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; /* @@ -280,79 +280,79 @@ static const unsigned char sha2_padding[64] = */ void sha2_finish( sha2_context *ctx, unsigned char output[32] ) { - unsigned long last, padn; - unsigned long high, low; - unsigned char msglen[8]; + unsigned long last, padn; + unsigned long high, low; + unsigned char msglen[8]; - high = ( ctx->total[0] >> 29 ) - | ( ctx->total[1] << 3 ); - low = ( ctx->total[0] << 3 ); + high = (ctx->total[0] >> 29) + | (ctx->total[1] << 3); + low = (ctx->total[0] << 3); - PUT_ULONG_BE( high, msglen, 0 ); - PUT_ULONG_BE( low, msglen, 4 ); + PUT_ULONG_BE( high, msglen, 0 ); + PUT_ULONG_BE( low, msglen, 4 ); - last = ctx->total[0] & 0x3F; - padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last ); + last = ctx->total[0] & 0x3F; + padn = (last < 56) ? (56 - last) : (120 - last); - sha2_update( ctx, (unsigned char *) sha2_padding, padn ); - sha2_update( ctx, msglen, 8 ); + sha2_update( ctx, (unsigned char *) sha2_padding, padn ); + sha2_update( ctx, msglen, 8 ); - PUT_ULONG_BE( ctx->state[0], output, 0 ); - PUT_ULONG_BE( ctx->state[1], output, 4 ); - PUT_ULONG_BE( ctx->state[2], output, 8 ); - PUT_ULONG_BE( ctx->state[3], output, 12 ); - PUT_ULONG_BE( ctx->state[4], output, 16 ); - PUT_ULONG_BE( ctx->state[5], output, 20 ); - PUT_ULONG_BE( ctx->state[6], output, 24 ); + PUT_ULONG_BE( ctx->state[0], output, 0 ); + PUT_ULONG_BE( ctx->state[1], output, 4 ); + PUT_ULONG_BE( ctx->state[2], output, 8 ); + PUT_ULONG_BE( ctx->state[3], output, 12 ); + PUT_ULONG_BE( ctx->state[4], output, 16 ); + PUT_ULONG_BE( ctx->state[5], output, 20 ); + PUT_ULONG_BE( ctx->state[6], output, 24 ); - if( ctx->is224 == 0 ) - PUT_ULONG_BE( ctx->state[7], output, 28 ); + if ( ctx->is224 == 0 ) + PUT_ULONG_BE( ctx->state[7], output, 28 ); } /* * output = SHA-256( input buffer ) */ void sha2( const unsigned char *input, size_t ilen, - unsigned char output[32], int is224 ) + unsigned char output[32], int is224 ) { - sha2_context ctx; + sha2_context ctx; - sha2_starts( &ctx, is224 ); - sha2_update( &ctx, input, ilen ); - sha2_finish( &ctx, output ); + sha2_starts( &ctx, is224 ); + sha2_update( &ctx, input, ilen ); + sha2_finish( &ctx, output ); - memset( &ctx, 0, sizeof( sha2_context ) ); + memset( &ctx, 0, sizeof(sha2_context) ); } /* * SHA-256 HMAC context setup */ void sha2_hmac_starts( sha2_context *ctx, const unsigned char *key, size_t keylen, - int is224 ) + int is224 ) { - size_t i; - unsigned char sum[32]; - - if( keylen > 64 ) - { - sha2( key, keylen, sum, is224 ); - keylen = ( is224 ) ? 28 : 32; - key = sum; - } + size_t i; + unsigned char sum[32]; + + if ( keylen > 64 ) + { + sha2( key, keylen, sum, is224 ); + keylen = (is224) ? 28 : 32; + key = sum; + } - memset( ctx->ipad, 0x36, 64 ); - memset( ctx->opad, 0x5C, 64 ); + memset( ctx->ipad, 0x36, 64 ); + memset( ctx->opad, 0x5C, 64 ); - for( i = 0; i < keylen; i++ ) - { - ctx->ipad[i] = (unsigned char)( ctx->ipad[i] ^ key[i] ); - ctx->opad[i] = (unsigned char)( ctx->opad[i] ^ key[i] ); - } + for ( i = 0; i < keylen; i++ ) + { + ctx->ipad[i] = (unsigned char)(ctx->ipad[i] ^ key[i]); + ctx->opad[i] = (unsigned char)(ctx->opad[i] ^ key[i]); + } - sha2_starts( ctx, is224 ); - sha2_update( ctx, ctx->ipad, 64 ); + sha2_starts( ctx, is224 ); + sha2_update( ctx, ctx->ipad, 64 ); - memset( sum, 0, sizeof( sum ) ); + memset( sum, 0, sizeof(sum) ); } /* @@ -360,7 +360,7 @@ void sha2_hmac_starts( sha2_context *ctx, const unsigned char *key, size_t keyle */ void sha2_hmac_update( sha2_context *ctx, const unsigned char *input, size_t ilen ) { - sha2_update( ctx, input, ilen ); + sha2_update( ctx, input, ilen ); } /* @@ -368,19 +368,19 @@ void sha2_hmac_update( sha2_context *ctx, const unsigned char *input, size_t ile */ void sha2_hmac_finish( sha2_context *ctx, unsigned char output[32] ) { - int is224, hlen; - unsigned char tmpbuf[32]; + int is224, hlen; + unsigned char tmpbuf[32]; - is224 = ctx->is224; - hlen = ( is224 == 0 ) ? 32 : 28; + is224 = ctx->is224; + hlen = (is224 == 0) ? 32 : 28; - sha2_finish( ctx, tmpbuf ); - sha2_starts( ctx, is224 ); - sha2_update( ctx, ctx->opad, 64 ); - sha2_update( ctx, tmpbuf, hlen ); - sha2_finish( ctx, output ); + sha2_finish( ctx, tmpbuf ); + sha2_starts( ctx, is224 ); + sha2_update( ctx, ctx->opad, 64 ); + sha2_update( ctx, tmpbuf, hlen ); + sha2_finish( ctx, output ); - memset( tmpbuf, 0, sizeof( tmpbuf ) ); + memset( tmpbuf, 0, sizeof(tmpbuf) ); } /* @@ -388,24 +388,24 @@ void sha2_hmac_finish( sha2_context *ctx, unsigned char output[32] ) */ void sha2_hmac_reset( sha2_context *ctx ) { - sha2_starts( ctx, ctx->is224 ); - sha2_update( ctx, ctx->ipad, 64 ); + sha2_starts( ctx, ctx->is224 ); + sha2_update( ctx, ctx->ipad, 64 ); } /* * output = HMAC-SHA-256( hmac key, input buffer ) */ void sha2_hmac( const unsigned char *key, size_t keylen, - const unsigned char *input, size_t ilen, - unsigned char output[32], int is224 ) + const unsigned char *input, size_t ilen, + unsigned char output[32], int is224 ) { - sha2_context ctx; + sha2_context ctx; - sha2_hmac_starts( &ctx, key, keylen, is224 ); - sha2_hmac_update( &ctx, input, ilen ); - sha2_hmac_finish( &ctx, output ); + sha2_hmac_starts( &ctx, key, keylen, is224 ); + sha2_hmac_update( &ctx, input, ilen ); + sha2_hmac_finish( &ctx, output ); - memset( &ctx, 0, sizeof( sha2_context ) ); + memset( &ctx, 0, sizeof(sha2_context) ); } @@ -417,9 +417,9 @@ void sha2_hmac( const unsigned char *key, size_t keylen, #endif void PKCS5_PBKDF2_HMAC(unsigned char *password, size_t plen, - unsigned char *salt, size_t slen, - const unsigned long iteration_count, const unsigned long key_length, - unsigned char *output) + unsigned char *salt, size_t slen, + const unsigned long iteration_count, const unsigned long key_length, + unsigned char *output) { sha2_context ctx; sha2_starts(&ctx, 0); @@ -461,7 +461,7 @@ void PKCS5_PBKDF2_HMAC(unsigned char *password, size_t plen, // Copy the generated bytes to the key unsigned long bytes_to_write = - min((key_length - generated_key_length), md_size); + min((key_length - generated_key_length), md_size); memcpy(output + generated_key_length, work, bytes_to_write); generated_key_length += bytes_to_write; ++counter; @@ -480,51 +480,51 @@ void PKCS5_PBKDF2_HMAC(unsigned char *password, size_t plen, /* * FIPS-180-2 test vectors */ -static unsigned char sha2_test_buf[3][57] = +static unsigned char sha2_test_buf[3][57] = { - { "abc" }, - { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" }, - { "" } + { "abc" }, + { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" }, + { "" } }; static const int sha2_test_buflen[3] = { - 3, 56, 1000 + 3, 56, 1000 }; static const unsigned char sha2_test_sum[6][32] = { - /* - * SHA-224 test vectors - */ - { 0x23, 0x09, 0x7D, 0x22, 0x34, 0x05, 0xD8, 0x22, - 0x86, 0x42, 0xA4, 0x77, 0xBD, 0xA2, 0x55, 0xB3, - 0x2A, 0xAD, 0xBC, 0xE4, 0xBD, 0xA0, 0xB3, 0xF7, - 0xE3, 0x6C, 0x9D, 0xA7 }, - { 0x75, 0x38, 0x8B, 0x16, 0x51, 0x27, 0x76, 0xCC, - 0x5D, 0xBA, 0x5D, 0xA1, 0xFD, 0x89, 0x01, 0x50, - 0xB0, 0xC6, 0x45, 0x5C, 0xB4, 0xF5, 0x8B, 0x19, - 0x52, 0x52, 0x25, 0x25 }, - { 0x20, 0x79, 0x46, 0x55, 0x98, 0x0C, 0x91, 0xD8, - 0xBB, 0xB4, 0xC1, 0xEA, 0x97, 0x61, 0x8A, 0x4B, - 0xF0, 0x3F, 0x42, 0x58, 0x19, 0x48, 0xB2, 0xEE, - 0x4E, 0xE7, 0xAD, 0x67 }, - - /* - * SHA-256 test vectors - */ - { 0xBA, 0x78, 0x16, 0xBF, 0x8F, 0x01, 0xCF, 0xEA, - 0x41, 0x41, 0x40, 0xDE, 0x5D, 0xAE, 0x22, 0x23, - 0xB0, 0x03, 0x61, 0xA3, 0x96, 0x17, 0x7A, 0x9C, - 0xB4, 0x10, 0xFF, 0x61, 0xF2, 0x00, 0x15, 0xAD }, - { 0x24, 0x8D, 0x6A, 0x61, 0xD2, 0x06, 0x38, 0xB8, - 0xE5, 0xC0, 0x26, 0x93, 0x0C, 0x3E, 0x60, 0x39, - 0xA3, 0x3C, 0xE4, 0x59, 0x64, 0xFF, 0x21, 0x67, - 0xF6, 0xEC, 0xED, 0xD4, 0x19, 0xDB, 0x06, 0xC1 }, - { 0xCD, 0xC7, 0x6E, 0x5C, 0x99, 0x14, 0xFB, 0x92, - 0x81, 0xA1, 0xC7, 0xE2, 0x84, 0xD7, 0x3E, 0x67, - 0xF1, 0x80, 0x9A, 0x48, 0xA4, 0x97, 0x20, 0x0E, - 0x04, 0x6D, 0x39, 0xCC, 0xC7, 0x11, 0x2C, 0xD0 } + /* + * SHA-224 test vectors + */ + { 0x23, 0x09, 0x7D, 0x22, 0x34, 0x05, 0xD8, 0x22, + 0x86, 0x42, 0xA4, 0x77, 0xBD, 0xA2, 0x55, 0xB3, + 0x2A, 0xAD, 0xBC, 0xE4, 0xBD, 0xA0, 0xB3, 0xF7, + 0xE3, 0x6C, 0x9D, 0xA7 }, + { 0x75, 0x38, 0x8B, 0x16, 0x51, 0x27, 0x76, 0xCC, + 0x5D, 0xBA, 0x5D, 0xA1, 0xFD, 0x89, 0x01, 0x50, + 0xB0, 0xC6, 0x45, 0x5C, 0xB4, 0xF5, 0x8B, 0x19, + 0x52, 0x52, 0x25, 0x25 }, + { 0x20, 0x79, 0x46, 0x55, 0x98, 0x0C, 0x91, 0xD8, + 0xBB, 0xB4, 0xC1, 0xEA, 0x97, 0x61, 0x8A, 0x4B, + 0xF0, 0x3F, 0x42, 0x58, 0x19, 0x48, 0xB2, 0xEE, + 0x4E, 0xE7, 0xAD, 0x67 }, + + /* + * SHA-256 test vectors + */ + { 0xBA, 0x78, 0x16, 0xBF, 0x8F, 0x01, 0xCF, 0xEA, + 0x41, 0x41, 0x40, 0xDE, 0x5D, 0xAE, 0x22, 0x23, + 0xB0, 0x03, 0x61, 0xA3, 0x96, 0x17, 0x7A, 0x9C, + 0xB4, 0x10, 0xFF, 0x61, 0xF2, 0x00, 0x15, 0xAD }, + { 0x24, 0x8D, 0x6A, 0x61, 0xD2, 0x06, 0x38, 0xB8, + 0xE5, 0xC0, 0x26, 0x93, 0x0C, 0x3E, 0x60, 0x39, + 0xA3, 0x3C, 0xE4, 0x59, 0x64, 0xFF, 0x21, 0x67, + 0xF6, 0xEC, 0xED, 0xD4, 0x19, 0xDB, 0x06, 0xC1 }, + { 0xCD, 0xC7, 0x6E, 0x5C, 0x99, 0x14, 0xFB, 0x92, + 0x81, 0xA1, 0xC7, 0xE2, 0x84, 0xD7, 0x3E, 0x67, + 0xF1, 0x80, 0x9A, 0x48, 0xA4, 0x97, 0x20, 0x0E, + 0x04, 0x6D, 0x39, 0xCC, 0xC7, 0x11, 0x2C, 0xD0 } }; /* @@ -532,14 +532,14 @@ static const unsigned char sha2_test_sum[6][32] = */ static unsigned char sha2_hmac_test_key[7][26] = { {"\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B" - "\x0B\x0B\x0B\x0B"}, + "\x0B\x0B\x0B\x0B"}, {"Jefe"}, {"\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA" - "\xAA\xAA\xAA\xAA"}, + "\xAA\xAA\xAA\xAA"}, {"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10" - "\x11\x12\x13\x14\x15\x16\x17\x18\x19"}, + "\x11\x12\x13\x14\x15\x16\x17\x18\x19"}, {"\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C" - "\x0C\x0C\x0C\x0C"}, + "\x0C\x0C\x0C\x0C"}, {""}, /* 0xAA 131 times */ {""} }; @@ -550,91 +550,91 @@ static const int sha2_hmac_test_keylen[7] = { static unsigned char sha2_hmac_test_buf[7][153] = { - { "Hi There" }, - { "what do ya want for nothing?" }, - { "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD" - "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD" - "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD" - "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD" - "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD" }, - { "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD" - "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD" - "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD" - "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD" - "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD" }, - { "Test With Truncation" }, - { "Test Using Larger Than Block-Size Key - Hash Key First" }, - { "This is a test using a larger than block-size key " - "and a larger than block-size data. The key needs to " - "be hashed before being used by the HMAC algorithm." } + { "Hi There" }, + { "what do ya want for nothing?" }, + { "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD" + "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD" + "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD" + "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD" + "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD" }, + { "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD" + "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD" + "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD" + "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD" + "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD" }, + { "Test With Truncation" }, + { "Test Using Larger Than Block-Size Key - Hash Key First" }, + { "This is a test using a larger than block-size key " + "and a larger than block-size data. The key needs to " + "be hashed before being used by the HMAC algorithm." } }; static const int sha2_hmac_test_buflen[7] = { - 8, 28, 50, 50, 20, 54, 152 + 8, 28, 50, 50, 20, 54, 152 }; static const unsigned char sha2_hmac_test_sum[14][32] = { - /* - * HMAC-SHA-224 test vectors - */ - { 0x89, 0x6F, 0xB1, 0x12, 0x8A, 0xBB, 0xDF, 0x19, - 0x68, 0x32, 0x10, 0x7C, 0xD4, 0x9D, 0xF3, 0x3F, - 0x47, 0xB4, 0xB1, 0x16, 0x99, 0x12, 0xBA, 0x4F, - 0x53, 0x68, 0x4B, 0x22 }, - { 0xA3, 0x0E, 0x01, 0x09, 0x8B, 0xC6, 0xDB, 0xBF, - 0x45, 0x69, 0x0F, 0x3A, 0x7E, 0x9E, 0x6D, 0x0F, - 0x8B, 0xBE, 0xA2, 0xA3, 0x9E, 0x61, 0x48, 0x00, - 0x8F, 0xD0, 0x5E, 0x44 }, - { 0x7F, 0xB3, 0xCB, 0x35, 0x88, 0xC6, 0xC1, 0xF6, - 0xFF, 0xA9, 0x69, 0x4D, 0x7D, 0x6A, 0xD2, 0x64, - 0x93, 0x65, 0xB0, 0xC1, 0xF6, 0x5D, 0x69, 0xD1, - 0xEC, 0x83, 0x33, 0xEA }, - { 0x6C, 0x11, 0x50, 0x68, 0x74, 0x01, 0x3C, 0xAC, - 0x6A, 0x2A, 0xBC, 0x1B, 0xB3, 0x82, 0x62, 0x7C, - 0xEC, 0x6A, 0x90, 0xD8, 0x6E, 0xFC, 0x01, 0x2D, - 0xE7, 0xAF, 0xEC, 0x5A }, - { 0x0E, 0x2A, 0xEA, 0x68, 0xA9, 0x0C, 0x8D, 0x37, - 0xC9, 0x88, 0xBC, 0xDB, 0x9F, 0xCA, 0x6F, 0xA8 }, - { 0x95, 0xE9, 0xA0, 0xDB, 0x96, 0x20, 0x95, 0xAD, - 0xAE, 0xBE, 0x9B, 0x2D, 0x6F, 0x0D, 0xBC, 0xE2, - 0xD4, 0x99, 0xF1, 0x12, 0xF2, 0xD2, 0xB7, 0x27, - 0x3F, 0xA6, 0x87, 0x0E }, - { 0x3A, 0x85, 0x41, 0x66, 0xAC, 0x5D, 0x9F, 0x02, - 0x3F, 0x54, 0xD5, 0x17, 0xD0, 0xB3, 0x9D, 0xBD, - 0x94, 0x67, 0x70, 0xDB, 0x9C, 0x2B, 0x95, 0xC9, - 0xF6, 0xF5, 0x65, 0xD1 }, - - /* - * HMAC-SHA-256 test vectors - */ - { 0xB0, 0x34, 0x4C, 0x61, 0xD8, 0xDB, 0x38, 0x53, - 0x5C, 0xA8, 0xAF, 0xCE, 0xAF, 0x0B, 0xF1, 0x2B, - 0x88, 0x1D, 0xC2, 0x00, 0xC9, 0x83, 0x3D, 0xA7, - 0x26, 0xE9, 0x37, 0x6C, 0x2E, 0x32, 0xCF, 0xF7 }, - { 0x5B, 0xDC, 0xC1, 0x46, 0xBF, 0x60, 0x75, 0x4E, - 0x6A, 0x04, 0x24, 0x26, 0x08, 0x95, 0x75, 0xC7, - 0x5A, 0x00, 0x3F, 0x08, 0x9D, 0x27, 0x39, 0x83, - 0x9D, 0xEC, 0x58, 0xB9, 0x64, 0xEC, 0x38, 0x43 }, - { 0x77, 0x3E, 0xA9, 0x1E, 0x36, 0x80, 0x0E, 0x46, - 0x85, 0x4D, 0xB8, 0xEB, 0xD0, 0x91, 0x81, 0xA7, - 0x29, 0x59, 0x09, 0x8B, 0x3E, 0xF8, 0xC1, 0x22, - 0xD9, 0x63, 0x55, 0x14, 0xCE, 0xD5, 0x65, 0xFE }, - { 0x82, 0x55, 0x8A, 0x38, 0x9A, 0x44, 0x3C, 0x0E, - 0xA4, 0xCC, 0x81, 0x98, 0x99, 0xF2, 0x08, 0x3A, - 0x85, 0xF0, 0xFA, 0xA3, 0xE5, 0x78, 0xF8, 0x07, - 0x7A, 0x2E, 0x3F, 0xF4, 0x67, 0x29, 0x66, 0x5B }, - { 0xA3, 0xB6, 0x16, 0x74, 0x73, 0x10, 0x0E, 0xE0, - 0x6E, 0x0C, 0x79, 0x6C, 0x29, 0x55, 0x55, 0x2B }, - { 0x60, 0xE4, 0x31, 0x59, 0x1E, 0xE0, 0xB6, 0x7F, - 0x0D, 0x8A, 0x26, 0xAA, 0xCB, 0xF5, 0xB7, 0x7F, - 0x8E, 0x0B, 0xC6, 0x21, 0x37, 0x28, 0xC5, 0x14, - 0x05, 0x46, 0x04, 0x0F, 0x0E, 0xE3, 0x7F, 0x54 }, - { 0x9B, 0x09, 0xFF, 0xA7, 0x1B, 0x94, 0x2F, 0xCB, - 0x27, 0x63, 0x5F, 0xBC, 0xD5, 0xB0, 0xE9, 0x44, - 0xBF, 0xDC, 0x63, 0x64, 0x4F, 0x07, 0x13, 0x93, - 0x8A, 0x7F, 0x51, 0x53, 0x5C, 0x3A, 0x35, 0xE2 } + /* + * HMAC-SHA-224 test vectors + */ + { 0x89, 0x6F, 0xB1, 0x12, 0x8A, 0xBB, 0xDF, 0x19, + 0x68, 0x32, 0x10, 0x7C, 0xD4, 0x9D, 0xF3, 0x3F, + 0x47, 0xB4, 0xB1, 0x16, 0x99, 0x12, 0xBA, 0x4F, + 0x53, 0x68, 0x4B, 0x22 }, + { 0xA3, 0x0E, 0x01, 0x09, 0x8B, 0xC6, 0xDB, 0xBF, + 0x45, 0x69, 0x0F, 0x3A, 0x7E, 0x9E, 0x6D, 0x0F, + 0x8B, 0xBE, 0xA2, 0xA3, 0x9E, 0x61, 0x48, 0x00, + 0x8F, 0xD0, 0x5E, 0x44 }, + { 0x7F, 0xB3, 0xCB, 0x35, 0x88, 0xC6, 0xC1, 0xF6, + 0xFF, 0xA9, 0x69, 0x4D, 0x7D, 0x6A, 0xD2, 0x64, + 0x93, 0x65, 0xB0, 0xC1, 0xF6, 0x5D, 0x69, 0xD1, + 0xEC, 0x83, 0x33, 0xEA }, + { 0x6C, 0x11, 0x50, 0x68, 0x74, 0x01, 0x3C, 0xAC, + 0x6A, 0x2A, 0xBC, 0x1B, 0xB3, 0x82, 0x62, 0x7C, + 0xEC, 0x6A, 0x90, 0xD8, 0x6E, 0xFC, 0x01, 0x2D, + 0xE7, 0xAF, 0xEC, 0x5A }, + { 0x0E, 0x2A, 0xEA, 0x68, 0xA9, 0x0C, 0x8D, 0x37, + 0xC9, 0x88, 0xBC, 0xDB, 0x9F, 0xCA, 0x6F, 0xA8 }, + { 0x95, 0xE9, 0xA0, 0xDB, 0x96, 0x20, 0x95, 0xAD, + 0xAE, 0xBE, 0x9B, 0x2D, 0x6F, 0x0D, 0xBC, 0xE2, + 0xD4, 0x99, 0xF1, 0x12, 0xF2, 0xD2, 0xB7, 0x27, + 0x3F, 0xA6, 0x87, 0x0E }, + { 0x3A, 0x85, 0x41, 0x66, 0xAC, 0x5D, 0x9F, 0x02, + 0x3F, 0x54, 0xD5, 0x17, 0xD0, 0xB3, 0x9D, 0xBD, + 0x94, 0x67, 0x70, 0xDB, 0x9C, 0x2B, 0x95, 0xC9, + 0xF6, 0xF5, 0x65, 0xD1 }, + + /* + * HMAC-SHA-256 test vectors + */ + { 0xB0, 0x34, 0x4C, 0x61, 0xD8, 0xDB, 0x38, 0x53, + 0x5C, 0xA8, 0xAF, 0xCE, 0xAF, 0x0B, 0xF1, 0x2B, + 0x88, 0x1D, 0xC2, 0x00, 0xC9, 0x83, 0x3D, 0xA7, + 0x26, 0xE9, 0x37, 0x6C, 0x2E, 0x32, 0xCF, 0xF7 }, + { 0x5B, 0xDC, 0xC1, 0x46, 0xBF, 0x60, 0x75, 0x4E, + 0x6A, 0x04, 0x24, 0x26, 0x08, 0x95, 0x75, 0xC7, + 0x5A, 0x00, 0x3F, 0x08, 0x9D, 0x27, 0x39, 0x83, + 0x9D, 0xEC, 0x58, 0xB9, 0x64, 0xEC, 0x38, 0x43 }, + { 0x77, 0x3E, 0xA9, 0x1E, 0x36, 0x80, 0x0E, 0x46, + 0x85, 0x4D, 0xB8, 0xEB, 0xD0, 0x91, 0x81, 0xA7, + 0x29, 0x59, 0x09, 0x8B, 0x3E, 0xF8, 0xC1, 0x22, + 0xD9, 0x63, 0x55, 0x14, 0xCE, 0xD5, 0x65, 0xFE }, + { 0x82, 0x55, 0x8A, 0x38, 0x9A, 0x44, 0x3C, 0x0E, + 0xA4, 0xCC, 0x81, 0x98, 0x99, 0xF2, 0x08, 0x3A, + 0x85, 0xF0, 0xFA, 0xA3, 0xE5, 0x78, 0xF8, 0x07, + 0x7A, 0x2E, 0x3F, 0xF4, 0x67, 0x29, 0x66, 0x5B }, + { 0xA3, 0xB6, 0x16, 0x74, 0x73, 0x10, 0x0E, 0xE0, + 0x6E, 0x0C, 0x79, 0x6C, 0x29, 0x55, 0x55, 0x2B }, + { 0x60, 0xE4, 0x31, 0x59, 0x1E, 0xE0, 0xB6, 0x7F, + 0x0D, 0x8A, 0x26, 0xAA, 0xCB, 0xF5, 0xB7, 0x7F, + 0x8E, 0x0B, 0xC6, 0x21, 0x37, 0x28, 0xC5, 0x14, + 0x05, 0x46, 0x04, 0x0F, 0x0E, 0xE3, 0x7F, 0x54 }, + { 0x9B, 0x09, 0xFF, 0xA7, 0x1B, 0x94, 0x2F, 0xCB, + 0x27, 0x63, 0x5F, 0xBC, 0xD5, 0xB0, 0xE9, 0x44, + 0xBF, 0xDC, 0x63, 0x64, 0x4F, 0x07, 0x13, 0x93, + 0x8A, 0x7F, 0x51, 0x53, 0x5C, 0x3A, 0x35, 0xE2 } }; typedef struct { char *t; @@ -647,7 +647,7 @@ typedef struct { char dk[1024]; // Remember to set this to max dkLen } testvector; -int do_test(testvector * tv) +int do_test(testvector *tv) { printf("Started %s\n", tv->t); fflush(stdout); @@ -656,9 +656,9 @@ int do_test(testvector * tv) return -1; } - PKCS5_PBKDF2_HMAC((unsigned char*)tv->p, tv->plen, - (unsigned char*)tv->s, tv->slen, tv->c, - tv->dkLen, (unsigned char*)key); + PKCS5_PBKDF2_HMAC((unsigned char *)tv->p, tv->plen, + (unsigned char *)tv->s, tv->slen, tv->c, + tv->dkLen, (unsigned char *)key); if (memcmp(tv->dk, key, tv->dkLen) != 0) { // Failed @@ -695,7 +695,7 @@ int main() sha2_update(&ctx, buf, buflen); } else sha2_update(&ctx, sha2_test_buf[j], - sha2_test_buflen[j]); + sha2_test_buflen[j]); sha2_finish(&ctx, sha2sum); @@ -719,17 +719,17 @@ int main() if (verbose != 0) printf(" HMAC-SHA-%d test #%d: ", 256 - k * 32, - j + 1); + j + 1); if (j == 5 || j == 6) { memset(buf, '\xAA', buflen = 131); sha2_hmac_starts(&ctx, buf, buflen, k); } else sha2_hmac_starts(&ctx, sha2_hmac_test_key[j], - sha2_hmac_test_keylen[j], k); + sha2_hmac_test_keylen[j], k); sha2_hmac_update(&ctx, sha2_hmac_test_buf[j], - sha2_hmac_test_buflen[j]); + sha2_hmac_test_buflen[j]); sha2_hmac_finish(&ctx, sha2sum); @@ -756,9 +756,9 @@ int main() "Test 1", "password", 8, "salt", 4, 1, 32, .dk = { 0x12, 0x0f, 0xb6, 0xcf, 0xfc, 0xf8, 0xb3, 0x2c, - 0x43, 0xe7, 0x22, 0x52, 0x56, 0xc4, 0xf8, 0x37, - 0xa8, 0x65, 0x48, 0xc9, 0x2c, 0xcc, 0x35, 0x48, - 0x08, 0x05, 0x98, 0x7c, 0xb7, 0x0b, 0xe1, 0x7b } + 0x43, 0xe7, 0x22, 0x52, 0x56, 0xc4, 0xf8, 0x37, + 0xa8, 0x65, 0x48, 0xc9, 0x2c, 0xcc, 0x35, 0x48, + 0x08, 0x05, 0x98, 0x7c, 0xb7, 0x0b, 0xe1, 0x7b } }; tv = &t1; @@ -774,7 +774,8 @@ int main() 0xae, 0x4d, 0x0c, 0x95, 0xaf, 0x6b, 0x46, 0xd3, 0x2d, 0x0a, 0xdf, 0xf9, 0x28, 0xf0, 0x6d, 0xd0, 0x2a, 0x30, 0x3f, 0x8e, 0xf3, 0xc2, 0x51, 0xdf, - 0xd6, 0xe2, 0xd8, 0x5a, 0x95, 0x47, 0x4c, 0x43 } + 0xd6, 0xe2, 0xd8, 0x5a, 0x95, 0x47, 0x4c, 0x43 + } }; tv = &t2; @@ -790,7 +791,8 @@ int main() 0xc5, 0xe4, 0x78, 0xd5, 0x92, 0x88, 0xc8, 0x41, 0xaa, 0x53, 0x0d, 0xb6, 0x84, 0x5c, 0x4c, 0x8d, 0x96, 0x28, 0x93, 0xa0, 0x01, 0xce, 0x4e, 0x11, - 0xa4, 0x96, 0x38, 0x73, 0xaa, 0x98, 0x13, 0x4a } + 0xa4, 0x96, 0x38, 0x73, 0xaa, 0x98, 0x13, 0x4a + } }; tv = &t3; @@ -806,7 +808,8 @@ int main() 0xcf, 0x81, 0xc6, 0x6f, 0xe8, 0xcf, 0xc0, 0x4d, 0x1f, 0x31, 0xec, 0xb6, 0x5d, 0xab, 0x40, 0x89, 0xf7, 0xf1, 0x79, 0xe8, 0x9b, 0x3b, 0x0b, 0xcb, - 0x17, 0xad, 0x10, 0xe3, 0xac, 0x6e, 0xba, 0x46 } + 0x17, 0xad, 0x10, 0xe3, 0xac, 0x6e, 0xba, 0x46 + } }; tv = &t4; @@ -824,7 +827,8 @@ int main() 0x32, 0xd8, 0x14, 0xb8, 0x11, 0x6e, 0x84, 0xcf, 0x2b, 0x17, 0x34, 0x7e, 0xbc, 0x18, 0x00, 0x18, 0x1c, 0x4e, 0x2a, 0x1f, 0xb8, 0xdd, 0x53, 0xe1, - 0xc6, 0x35, 0x51, 0x8c, 0x7d, 0xac, 0x47, 0xe9 } + 0xc6, 0x35, 0x51, 0x8c, 0x7d, 0xac, 0x47, 0xe9 + } }; tv = &t5; @@ -838,7 +842,8 @@ int main() "Test 6", "pass\0word", 9, "sa\0lt", 5, 4096, 16, { 0x89, 0xb6, 0x9d, 0x05, 0x16, 0xf8, 0x29, 0x89, - 0x3c, 0x69, 0x62, 0x26, 0x65, 0x0a, 0x86, 0x87 } + 0x3c, 0x69, 0x62, 0x26, 0x65, 0x0a, 0x86, 0x87 + } }; tv = &t6; diff --git a/version2/src/C/pbkdf2-sha256.h b/version2/src/C/pbkdf2-sha256.h index 183ac25..54a0bdf 100644 --- a/version2/src/C/pbkdf2-sha256.h +++ b/version2/src/C/pbkdf2-sha256.h @@ -18,6 +18,7 @@ void sha2_finish( sha2_context *ctx, unsigned char output[32] ); void sha2_hmac_starts( sha2_context *ctx, const unsigned char *key, size_t keylen, int is224 ); void sha2_hmac_update( sha2_context *ctx, const unsigned char *input, size_t ilen ); void sha2_hmac_finish( sha2_context *ctx, unsigned char output[32] ); +void sha2_hmac_reset( sha2_context *ctx ); void PKCS5_PBKDF2_HMAC(unsigned char *password, size_t plen, diff --git a/version2/src/C/vector.h b/version2/src/C/vector.h index 4c4a7e5..e1378c9 100644 --- a/version2/src/C/vector.h +++ b/version2/src/C/vector.h @@ -73,7 +73,7 @@ public: void removeAll(Vector *v) { uint vsize = v->size(); - for(uint i = 0; i < vsize; i++) + for (uint i = 0; i < vsize; i++) remove(v->get(i)); } -- 2.34.1