Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prototype Interfaces for Cipher #26

Open
wants to merge 14 commits into
base: gsoc
Choose a base branch
from
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pkg
build.properties
build_lib/jruby.jar
build
lib/kryptcore.jar
coverage.ec
dist
doc
Expand Down
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
source :rubygems
source 'https://rubygems.org'

gem 'krypt', :path => File.expand_path('../krypt', File.dirname(__FILE__))
gem 'krypt-provider-jce', :path => File.expand_path('../krypt-provider-jce', File.dirname(__FILE__))
Expand Down
Binary file modified lib/kryptcore.jar
Binary file not shown.
20 changes: 20 additions & 0 deletions src/org/jruby/ext/krypt/Errors.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,30 @@ public static RaiseException newPEMError(Ruby rt, String message) {
return newError(rt, "Krypt::PEM::PEMError", message);
}

public static RaiseException newHexError(Ruby rt, String message) {
return newError(rt, "Krypt::Hex::HexError", message);
}

public static RaiseException newBase64Error(Ruby rt, String message) {
return newError(rt, "Krypt::Base64::Base64Error", message);
}

public static RaiseException newDigestError(Ruby rt, String message) {
return newError(rt, "Krypt::Digest::DigestError", message);
}

public static RaiseException newCipherError(Ruby rt, String message) {
return newError(rt, "Krypt::Cipher::CipherError", message);
}

public static RaiseException newSignatureError(Ruby rt, String message) {
return newError(rt, "Krypt::SignatureError::DSAError", message);
}

public static RaiseException newKeyError(Ruby rt, String message) {
return newError(rt, "Krypt::Key::KeyError", message);
}

public static RaiseException newError(Ruby rt, String path, String message) {
return new RaiseException(rt, getClassFromPath(rt, path), message, true);
}
Expand Down
5 changes: 3 additions & 2 deletions src/org/jruby/ext/krypt/Hex.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,10 @@ public static byte[] decode(byte[] bytes) {
if (bytes == null) throw new NullPointerException("bytes");
if (bytes.length % 2 != 0) throw new IllegalArgumentException("Hex data length must be a multiple of 2");

byte[] ret = new byte[bytes.length / 2];
int retlen = bytes.length / 2;
byte[] ret = new byte[retlen];

for (int i=0; i<bytes.length; i++) {
for (int i=0; i < retlen; i++) {
byte c = bytes[i*2];
byte d = bytes[i*2+1];
if (c < 0 || d < 0 ||
Expand Down
8 changes: 8 additions & 0 deletions src/org/jruby/ext/krypt/KryptCoreService.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@
import org.jruby.RubyModule;
import org.jruby.ext.krypt.asn1.RubyAsn1;
import org.jruby.ext.krypt.asn1.RubyPem;
import org.jruby.ext.krypt.codec.RubyBase64;
import org.jruby.ext.krypt.codec.RubyHex;
import org.jruby.ext.krypt.digest.RubyDigest;
import org.jruby.ext.krypt.signature.RubySignature;
import org.jruby.krypt.crypto.Cipher;

/**
*
Expand All @@ -48,6 +52,10 @@ public static void create(Ruby runtime) {
RubyClass kryptError = krypt.defineClassUnder("KryptError", standardError, standardError.getAllocator());
RubyAsn1.createAsn1(runtime, krypt, kryptError);
RubyPem.createPem(runtime, krypt, kryptError);
RubyHex.createHex(runtime, krypt, kryptError);
RubyBase64.createBase64(runtime, krypt, kryptError);
RubyDigest.createDigest(runtime, krypt, kryptError);
Cipher.createCipher(runtime, krypt);
RubySignature.createSignature(runtime, krypt, kryptError );
}
}
95 changes: 95 additions & 0 deletions src/org/jruby/ext/krypt/KryptImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package org.jruby.ext.krypt;

import java.security.MessageDigest;

/**
*
* @author Vipul A M <[email protected]>
*
* Credits jruby-ossl
*/

public class KryptImpl {

/**
* No instantiating this class...
*/
private KryptImpl() {}

public static interface KeyAndIv {
byte[] getKey();
byte[] getIv();
}

private static class KeyAndIvImpl implements KeyAndIv {
private final byte[] key;
private final byte[] iv;
public KeyAndIvImpl(byte[] key, byte[] iv) {
this.key = key;
this.iv = iv;
}
public byte[] getKey() {
return key;
}
public byte[] getIv() {
return iv;
}
}

public static KeyAndIv EVP_BytesToKey(int key_len, int iv_len, MessageDigest md, byte[] salt, byte[] data, int count) {
byte[] key = new byte[key_len];
byte[] iv = new byte[iv_len];
int key_ix = 0;
int iv_ix = 0;
byte[] md_buf = null;
int nkey = key_len;
int niv = iv_len;
int i = 0;
if(data == null) {
return new KeyAndIvImpl(key,iv);
}
int addmd = 0;
for(;;) {
md.reset();
if(addmd++ > 0) {
md.update(md_buf);
}
md.update(data);
if(null != salt) {
md.update(salt,0,8);
}
md_buf = md.digest();
for(i=1;i<count;i++) {
md.reset();
md.update(md_buf);
md_buf = md.digest();
}
i=0;
if(nkey > 0) {
for(;;) {
if(nkey == 0) break;
if(i == md_buf.length) break;
key[key_ix++] = md_buf[i];
nkey--;
i++;
}
}
if(niv > 0 && i != md_buf.length) {
for(;;) {
if(niv == 0) break;
if(i == md_buf.length) break;
iv[iv_ix++] = md_buf[i];
niv--;
i++;
}
}
if(nkey == 0 && niv == 0) {
break;
}
}
for(i=0;i<md_buf.length;i++) {
md_buf[i] = 0;
}
return new KeyAndIvImpl(key,iv);
}
}
57 changes: 57 additions & 0 deletions src/org/jruby/ext/krypt/SimpleSecretKey.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package org.jruby.ext.krypt;
/***** BEGIN LICENSE BLOCK *****
* Version: CPL 1.0/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Common Public
* License Version 1.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.eclipse.org/legal/cpl-v10.html
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* Copyright (C) 2006 Ola Bini <[email protected]>
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the CPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the CPL, the GPL or the LGPL.
***** END LICENSE BLOCK *****/

/*
* Huge credits to jruby-ossl
*/

import javax.crypto.SecretKey;

/**
* @author <a href="mailto:[email protected]">Ola Bini</a>
*/
public class SimpleSecretKey implements SecretKey {
private static final long serialVersionUID = 1L;

private final String algorithm;
private final byte[] value;
public SimpleSecretKey(String algorithm, byte[] value) {
this.algorithm = algorithm;
this.value = value;
}
public String getAlgorithm() {
return algorithm;
}
public byte[] getEncoded() {
return value;
}
public String getFormat() {
return "RAW";
}
}// SimpleSecretKey
100 changes: 100 additions & 0 deletions src/org/jruby/ext/krypt/codec/RubyBase64.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/***** BEGIN LICENSE BLOCK *****
* Version: CPL 1.0/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Common Public
* License Version 1.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.eclipse.org/legal/cpl-v10.html
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* Copyright (C) 2011
* Hiroshi Nakamura <[email protected]>
* Martin Bosslet <[email protected]>
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the CPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the CPL, the GPL or the LGPL.
*/
package org.jruby.ext.krypt.codec;

import java.io.IOException;
import org.jruby.Ruby;
import org.jruby.RubyClass;
import org.jruby.RubyModule;
import org.jruby.anno.JRubyMethod;
import org.jruby.ext.krypt.Base64;
import org.jruby.ext.krypt.Errors;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.util.ByteList;

/**
*
* @author <a href="mailto:[email protected]">Martin Bosslet</a>
*/
public class RubyBase64 {

private RubyBase64() {}

@JRubyMethod(meta = true)
public static IRubyObject encode(ThreadContext ctx, IRubyObject recv, IRubyObject data) {
try {
byte[] bytes = data.convertToString().getBytes();
byte[] encoded = Base64.encode(bytes, -1);
return ctx.getRuntime().newString(new ByteList(encoded, false));
} catch(IOException ex){
throw Errors.newHexError(ctx.getRuntime(), ex.getMessage());
}
catch (RuntimeException ex) {
throw Errors.newHexError(ctx.getRuntime(), ex.getMessage());
}
}

@JRubyMethod(meta = true)
public static IRubyObject encode(ThreadContext ctx, IRubyObject recv, IRubyObject data, IRubyObject cols) {
try {
int c= (int) cols.convertToInteger().getLongValue();
byte[] bytes = data.convertToString().getBytes();
byte[] encoded = Base64.encode(bytes, c);
return ctx.getRuntime().newString(new ByteList(encoded, false));
} catch(IOException ex){
throw Errors.newHexError(ctx.getRuntime(), ex.getMessage());
}
catch (RuntimeException ex) {
throw Errors.newHexError(ctx.getRuntime(), ex.getMessage());
}
}


@JRubyMethod(meta = true)
public static IRubyObject decode(ThreadContext ctx, IRubyObject recv, IRubyObject data) {
try {
byte[] bytes = data.convertToString().getBytes();
byte[] decoded = Base64.decode(bytes);
return ctx.getRuntime().newString(new ByteList(decoded, false));
} catch(IOException ex){
throw Errors.newHexError(ctx.getRuntime(), ex.getMessage());
} catch (RuntimeException ex) {
throw Errors.newHexError(ctx.getRuntime(), ex.getMessage());
}
}

public static void createBase64(Ruby runtime, RubyModule krypt, RubyClass kryptError) {
RubyModule mHex = runtime.defineModuleUnder("Base64", krypt);
mHex.defineClassUnder("Base64Error", kryptError, kryptError.getAllocator());
mHex.defineAnnotatedMethods(RubyBase64.class);
}

}
Loading