Skip to content

Commit

Permalink
Initial commit of the actual worker.
Browse files Browse the repository at this point in the history
  • Loading branch information
jrhames committed Mar 4, 2012
1 parent 569b081 commit 25e123b
Show file tree
Hide file tree
Showing 3 changed files with 279 additions and 0 deletions.
129 changes: 129 additions & 0 deletions safr.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
// Copyright (c) 2012 FTSY
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#include "safr.h"

void SafrInstance::HandleMessage(const pp::Var& var_message) {
// TODO(sdk_user): 1. Make this function handle the incoming message.
if (var_message == "register") {
if (register_algs()) {
PostMessage("OK");
} else {
PostMessage("FAIL");
}
} else if (var_message == "test") {
if (test()) {
PostMessage("OK");
} else {
PostMessage("FAIL");
}
} else {
std::string str = var_message.AsString();
const char* tmp = str.c_str();
PostMessage(tmp);
sha1sum(tmp);
md5sum(tmp);
}
}

bool SafrInstance::register_algs() {
if (register_cipher(&aes_desc) == -1) return false;
if (register_hash(&sha1_desc) == -1) return false;
if (register_hash(&sha256_desc) == -1) return false;
if (register_hash(&md5_desc) == -1) return false;

aes_idx = find_cipher("aes");
sha1_idx = find_hash("sha1");
sha256_idx = find_hash("sha256");
md5_idx = find_hash("md5");

return true;
}

bool SafrInstance::test() {
if (aes_idx == -1) return false;
if (sha1_idx == -1) return false;
if (sha256_idx == -1) return false;
if (md5_idx == -1) return false;
return true;
}

void SafrInstance::crypt (const char* salt, const char* text) {
if (aes_idx == -1) return;

ivsize = cipher_descriptor[aes_idx].block_length;
ks = hash_descriptor[sha256_idx].hashsize;
if (cipher_descriptor[aes_idx].keysize(&ks) != CRYPT_OK) {
return;
}
}

void SafrInstance::decrypt (const char* salt, const char* str) {
}

void SafrInstance::sha1sum (const char* text) {
if (sha1_idx == -1) return;

unsigned char tmp[16];
hash_descriptor[sha1_idx].init(&md);
hash_descriptor[sha1_idx].process(&md, (unsigned char *)text,strlen(text));
hash_descriptor[sha1_idx].done(&md, tmp);

char buffer[32] = "";
char str[2];
for (x = 0; x < (int)hash_descriptor[sha1_idx].hashsize; x++) {
sprintf(str, "%02x",tmp[x]);
strcat(buffer, str);
}
PostMessage(buffer);
}

void SafrInstance::md5sum (const char* text) {
if (md5_idx == -1) return;

unsigned char tmp[16];
hash_descriptor[md5_idx].init(&md);
hash_descriptor[md5_idx].process(&md, (unsigned char *)text,strlen(text));
hash_descriptor[md5_idx].done(&md, tmp);

char buffer[32] = "";
char str[2];
for (x = 0; x < (int)hash_descriptor[md5_idx].hashsize; x++) {
sprintf(str, "%02x",tmp[x]);
strcat(buffer, str);
}
PostMessage(buffer);
}

class SafrModule : public pp::Module {
public:
SafrModule() : pp::Module() {}
virtual ~SafrModule() {}

virtual pp::Instance* CreateInstance(PP_Instance instance) {
return new SafrInstance(instance);
}
};

namespace pp {
Module* CreateModule() {
return new SafrModule();
}
}
55 changes: 55 additions & 0 deletions safr.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright (c) 2012 FTSY
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#ifndef SAFR_H_
#define SAFR_H_

#include <cstdio>
#include <string>
#include <tomcrypt.h>
#include "ppapi/cpp/instance.h"
#include "ppapi/cpp/module.h"
#include "ppapi/cpp/var.h"

class SafrInstance : public pp::Instance {
public:
explicit SafrInstance(PP_Instance instance) : pp::Instance(instance) {};
virtual ~SafrInstance() {};
void HandleMessage(const pp::Var& var_message);

private:
unsigned char plaintext[512],ciphertext[512];
unsigned char tmpkey[512], key[MAXBLOCKSIZE], IV[MAXBLOCKSIZE];
unsigned char inbuf[512]; /* i/o block size */
unsigned long outlen, ivsize, x, y, z;
symmetric_CTR ctr;
int sha1_idx, sha256_idx, md5_idx, aes_idx, ks;
unsigned long w;
hash_state md;

bool register_algs();
bool test();
void crypt (const char* salt, const char* text);
void decrypt (const char* salt, const char* str);
void sha1sum (const char* text);
void md5sum (const char* text);
};

#endif // SAFR_H_
95 changes: 95 additions & 0 deletions safr.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<!DOCTYPE html>
<html>
<head>
<title>Safr!</title>

<script type="text/javascript">
SafrModule = null; // Global application object.
statusText = 'NO-STATUS';

// Indicate load success.
function moduleDidLoad() {
SafrModule = document.getElementById('safr');
updateStatus('SUCCESS');
}

// The 'message' event handler. This handler is fired when the NaCl module
// posts a message to the browser by calling PPB_Messaging.PostMessage()
// (in C) or pp::Instance.PostMessage() (in C++). This implementation
// simply displays the content of the message in an alert panel.
function handleMessage(message_event) {
console.log(message_event.data);
}

// If the page loads before the Native Client module loads, then set the
// status message indicating that the module is still loading. Otherwise,
// do not change the status message.
function pageDidLoad() {
if (SafrModule == null) {
updateStatus('LOADING...');
} else {
// It's possible that the Native Client module onload event fired
// before the page's onload event. In this case, the status message
// will reflect 'SUCCESS', but won't be displayed. This call will
// display the current message.
updateStatus();
}
}

// Set the global status message. If the element with id 'statusField'
// exists, then set its HTML to the status message as well.
// opt_message The message test. If this is null or undefined, then
// attempt to set the element with id 'statusField' to the value of
// |statusText|.
function updateStatus(opt_message) {
if (opt_message)
statusText = opt_message;
var statusField = document.getElementById('status_field');
if (statusField) {
statusField.innerHTML = statusText;
}
}
</script>
</head>
<body onload="pageDidLoad()">

<h1>Native Client Module Safr</h1>
<p>
<!-- Load the published .nexe. This includes the 'nacl' attribute which
shows how to load multi-architecture modules. Each entry in the "nexes"
object in the .nmf manifest file is a key-value pair: the key is the
instruction set architecture ('x86-32', 'x86-64', etc.); the value is a URL
for the desired NaCl module.
To load the debug versions of your .nexes, set the 'nacl' attribute to the
_dbg.nmf version of the manifest file.
Note: Since this NaCl module does not use any real-estate in the browser,
it's width and height are set to 0.
Note: The <EMBED> element is wrapped inside a <DIV>, which has both a 'load'
and a 'message' event listener attached. This wrapping method is used
instead of attaching the event listeners directly to the <EMBED> element to
ensure that the listeners are active before the NaCl module 'load' event
fires. This also allows you to use PPB_Messaging.PostMessage() (in C) or
pp::Instance.PostMessage() (in C++) from within the initialization code in
your NaCl module.
-->
<div id="listener">
<script type="text/javascript">
var listener = document.getElementById('listener');
listener.addEventListener('load', moduleDidLoad, true);
listener.addEventListener('message', handleMessage, true);
</script>

<embed name="nacl_module"
id="safr"
width=0 height=0
src="safr.nmf"
type="application/x-nacl" />
</div>
</p>

<h2>Status</h2>
<div id="status_field">NO-STATUS</div>
</body>
</html>

0 comments on commit 25e123b

Please sign in to comment.