Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Issue #929: rewrote message digest support to use separate init/updat…
…e/final CommonCrypto functions.

	Change on 2018/01/11 by tball <tball@google.com>

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=181645247
  • Loading branch information
tomball committed Jan 11, 2018
1 parent c380e52 commit 1c596e4
Show file tree
Hide file tree
Showing 3 changed files with 269 additions and 83 deletions.
Expand Up @@ -17,7 +17,6 @@


package com.google.j2objc.security; package com.google.j2objc.security;


import java.io.ByteArrayOutputStream;
import java.security.MessageDigest; import java.security.MessageDigest;


/*-[ /*-[
Expand All @@ -31,38 +30,68 @@
*/ */
public class IosMD5MessageDigest extends MessageDigest implements Cloneable { public class IosMD5MessageDigest extends MessageDigest implements Cloneable {


private ByteArrayOutputStream buffer; // Malloc'd CommonCrypto context.
protected long ctx;


public IosMD5MessageDigest() { public IosMD5MessageDigest() {
super("MD5"); super("MD5");
buffer = new ByteArrayOutputStream(); allocContext();
} }


@Override @Override
protected void engineUpdate(byte input) { protected native int engineGetDigestLength() /*-[
buffer.write(input); return CC_MD5_DIGEST_LENGTH;
} ]-*/;

private native void allocContext() /*-[
self->ctx_ = (jlong)calloc(1, sizeof(CC_MD5_CTX));
[self engineReset];
]-*/;


@Override @Override
protected void engineUpdate(byte[] input, int offset, int len) { protected native void engineReset() /*-[
buffer.write(input, offset, len); CC_MD5_Init((CC_MD5_CTX *)ctx_);
} ]-*/;


@Override @Override
protected native byte[] engineDigest() /*-[ protected native void engineUpdate(byte input) /*-[
IOSByteArray *bytes = [buffer_ toByteArray]; CC_MD5_Update((CC_MD5_CTX *)ctx_, &input, 1);
unsigned char digest[CC_MD5_DIGEST_LENGTH];
CC_MD5(bytes->buffer_, (unsigned) bytes->size_, digest);
return [IOSByteArray arrayWithBytes:(jbyte *)digest count:CC_MD5_DIGEST_LENGTH];
]-*/; ]-*/;


@Override @Override
protected void engineReset() { protected native void engineUpdate(byte[] input, int offset, int len) /*-[
buffer.reset(); IOSArray_checkRange(input->size_, offset, len);
} CC_MD5_Update((CC_MD5_CTX *)ctx_, input->buffer_ + offset, len);
]-*/;


@Override @Override
protected native int engineGetDigestLength() /*-[ protected native byte[] engineDigest() /*-[
return CC_MD5_DIGEST_LENGTH; IOSByteArray *md = [IOSByteArray arrayWithLength:CC_MD5_DIGEST_LENGTH];
CC_MD5_Final((unsigned char *)md->buffer_, (CC_MD5_CTX *)ctx_);
[self engineReset];
return md;
]-*/;

public native Object clone() throws CloneNotSupportedException /*-[
ComGoogleJ2objcSecurityIosMD5MessageDigest *obj =
(ComGoogleJ2objcSecurityIosMD5MessageDigest *) [super java_clone];
ComGoogleJ2objcSecurityIosMD5MessageDigest_allocContext(obj);
if (ctx_ != 0LL) {
memcpy((void *)obj->ctx_, (const void *)ctx_, sizeof(CC_MD5_CTX));
}
return obj;
]-*/; ]-*/;

native void close() /*-[
if (ctx_ != 0LL) {
free((void *)ctx_);
ctx_ = 0LL;
}
]-*/;

@Override
protected void finalize() throws Throwable {
close();
super.finalize();
}
} }
226 changes: 168 additions & 58 deletions jre_emul/Classes/com/google/j2objc/security/IosSHAMessageDigest.java
Expand Up @@ -17,8 +17,6 @@


package com.google.j2objc.security; package com.google.j2objc.security;


import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.security.MessageDigest; import java.security.MessageDigest;


/*-[ /*-[
Expand All @@ -33,120 +31,232 @@
*/ */
public abstract class IosSHAMessageDigest extends MessageDigest implements Cloneable { public abstract class IosSHAMessageDigest extends MessageDigest implements Cloneable {


protected ByteArrayOutputStream buffer; // Malloc'd CommonCrypto context.
protected long shaCtx;


public IosSHAMessageDigest(String algorithm) { public IosSHAMessageDigest(String algorithm) {
super(algorithm); super(algorithm);
buffer = new ByteArrayOutputStream();
} }


@Override native void close() /*-[
protected void engineUpdate(byte input) { if (shaCtx_ != 0LL) {
buffer.write(input); free((void *)shaCtx_);
} shaCtx_ = 0LL;

}
@Override ]-*/;
protected void engineUpdate(byte[] input, int offset, int len) {
buffer.write(input, offset, len);
}


@Override @Override
protected void engineReset() { protected void finalize() throws Throwable {
buffer.reset(); close();
} super.finalize();

public Object clone() throws CloneNotSupportedException {
IosSHAMessageDigest obj = (IosSHAMessageDigest) super.clone();
// ByteArrayOutputStreams are not cloneable, so copy it.
obj.buffer = new ByteArrayOutputStream();
if (buffer.size() > 0) {
try {
obj.buffer.write(buffer.toByteArray());
} catch (IOException e) {
// Should never happen.
throw new AssertionError(e);
}
}
return obj;
} }


public static class SHA1 extends IosSHAMessageDigest { public static class SHA1 extends IosSHAMessageDigest {


public SHA1() { public SHA1() {
super("SHA-1"); super("SHA-1");
allocContext();
} }


@Override @Override
protected native byte[] engineDigest() /*-[ protected native int engineGetDigestLength() /*-[
IOSByteArray *bytes = [buffer_ toByteArray]; return CC_SHA1_DIGEST_LENGTH;
unsigned char digest[CC_SHA1_DIGEST_LENGTH]; ]-*/;
CC_SHA1(bytes->buffer_, bytes->size_, digest);
return [IOSByteArray arrayWithBytes:(jbyte *)digest count:CC_SHA1_DIGEST_LENGTH]; private native void allocContext() /*-[
self->shaCtx_ = (jlong)calloc(1, sizeof(CC_SHA1_CTX));
[self engineReset];
]-*/; ]-*/;


@Override @Override
protected native int engineGetDigestLength() /*-[ protected native void engineReset() /*-[
return CC_SHA1_DIGEST_LENGTH; CC_SHA1_Init((CC_SHA1_CTX *)shaCtx_);
]-*/;

@Override
protected native void engineUpdate(byte input) /*-[
CC_SHA1_Update((CC_SHA1_CTX *)shaCtx_, &input, 1);
]-*/;

@Override
protected native void engineUpdate(byte[] input, int offset, int len) /*-[
IOSArray_checkRange(input->size_, offset, len);
CC_SHA1_Update((CC_SHA1_CTX *)shaCtx_, input->buffer_ + offset, len);
]-*/;

@Override
protected native byte[] engineDigest() /*-[
IOSByteArray *md = [IOSByteArray arrayWithLength:CC_SHA1_DIGEST_LENGTH];
CC_SHA1_Final((unsigned char *)md->buffer_, (CC_SHA1_CTX *)shaCtx_);
[self engineReset];
return md;
]-*/;

public native Object clone() throws CloneNotSupportedException /*-[
ComGoogleJ2objcSecurityIosSHAMessageDigest_SHA1 *obj =
(ComGoogleJ2objcSecurityIosSHAMessageDigest_SHA1 *) [super java_clone];
ComGoogleJ2objcSecurityIosSHAMessageDigest_SHA1_allocContext(obj);
if (shaCtx_ != 0LL) {
memcpy((void *)obj->shaCtx_, (const void *)shaCtx_, sizeof(CC_SHA1_CTX));
}
return obj;
]-*/; ]-*/;
} }


public static class SHA256 extends IosSHAMessageDigest { public static class SHA256 extends IosSHAMessageDigest {


public SHA256() { public SHA256() {
super("SHA-256"); super("SHA-256");
allocContext();
} }


@Override @Override
protected native byte[] engineDigest() /*-[ protected native int engineGetDigestLength() /*-[
IOSByteArray *bytes = [buffer_ toByteArray]; return CC_SHA256_DIGEST_LENGTH;
unsigned char digest[CC_SHA256_DIGEST_LENGTH]; ]-*/;
CC_SHA256(bytes->buffer_, bytes->size_, digest);
return [IOSByteArray arrayWithBytes:(jbyte *)digest count:CC_SHA256_DIGEST_LENGTH]; private native void allocContext() /*-[
self->shaCtx_ = (jlong)calloc(1, sizeof(CC_SHA256_CTX));
[self engineReset];
]-*/; ]-*/;


@Override @Override
protected native int engineGetDigestLength() /*-[ protected native void engineReset() /*-[
return CC_SHA256_DIGEST_LENGTH; CC_SHA256_Init((CC_SHA256_CTX *)shaCtx_);
]-*/;

@Override
protected native void engineUpdate(byte input) /*-[
CC_SHA256_Update((CC_SHA256_CTX *)shaCtx_, &input, 1);
]-*/;

@Override
protected native void engineUpdate(byte[] input, int offset, int len) /*-[
IOSArray_checkRange(input->size_, offset, len);
CC_SHA256_Update((CC_SHA256_CTX *)shaCtx_, input->buffer_ + offset, len);
]-*/;

@Override
protected native byte[] engineDigest() /*-[
IOSByteArray *md = [IOSByteArray arrayWithLength:CC_SHA256_DIGEST_LENGTH];
CC_SHA256_Final((unsigned char *)md->buffer_, (CC_SHA256_CTX *)shaCtx_);
[self engineReset];
return md;
]-*/;

public native Object clone() throws CloneNotSupportedException /*-[
ComGoogleJ2objcSecurityIosSHAMessageDigest_SHA256 *obj =
(ComGoogleJ2objcSecurityIosSHAMessageDigest_SHA256 *) [super java_clone];
ComGoogleJ2objcSecurityIosSHAMessageDigest_SHA256_allocContext(obj);
if (shaCtx_ != 0LL) {
memcpy((void *)obj->shaCtx_, (const void *)shaCtx_, sizeof(CC_SHA256_CTX));
}
return obj;
]-*/; ]-*/;
} }


public static class SHA384 extends IosSHAMessageDigest { public static class SHA384 extends IosSHAMessageDigest {


public SHA384() { public SHA384() {
super("SHA-384"); super("SHA-384");
allocContext();
} }


@Override @Override
protected native byte[] engineDigest() /*-[ protected native int engineGetDigestLength() /*-[
IOSByteArray *bytes = [buffer_ toByteArray]; return CC_SHA384_DIGEST_LENGTH;
unsigned char digest[CC_SHA384_DIGEST_LENGTH]; ]-*/;
CC_SHA384(bytes->buffer_, bytes->size_, digest);
return [IOSByteArray arrayWithBytes:(jbyte *)digest count:CC_SHA384_DIGEST_LENGTH]; private native void allocContext() /*-[
// SHA384 and SHA512 use the same context struct.
self->shaCtx_ = (jlong)calloc(1, sizeof(CC_SHA512_CTX));
[self engineReset];
]-*/; ]-*/;


@Override @Override
protected native int engineGetDigestLength() /*-[ protected native void engineReset() /*-[
return CC_SHA384_DIGEST_LENGTH; CC_SHA384_Init((CC_SHA512_CTX *)shaCtx_);
]-*/;

@Override
protected native void engineUpdate(byte input) /*-[
CC_SHA384_Update((CC_SHA512_CTX *)shaCtx_, &input, 1);
]-*/;

@Override
protected native void engineUpdate(byte[] input, int offset, int len) /*-[
IOSArray_checkRange(input->size_, offset, len);
CC_SHA384_Update((CC_SHA512_CTX *)shaCtx_, input->buffer_ + offset, len);
]-*/;

@Override
protected native byte[] engineDigest() /*-[
IOSByteArray *md = [IOSByteArray arrayWithLength:CC_SHA384_DIGEST_LENGTH];
CC_SHA384_Final((unsigned char *)md->buffer_, (CC_SHA512_CTX *)shaCtx_);
[self engineReset];
return md;
]-*/;

public native Object clone() throws CloneNotSupportedException /*-[
ComGoogleJ2objcSecurityIosSHAMessageDigest_SHA384 *obj =
(ComGoogleJ2objcSecurityIosSHAMessageDigest_SHA384 *) [super java_clone];
ComGoogleJ2objcSecurityIosSHAMessageDigest_SHA384_allocContext(obj);
if (shaCtx_ != 0LL) {
memcpy((void *)obj->shaCtx_, (const void *)shaCtx_, sizeof(CC_SHA512_CTX));
}
return obj;
]-*/; ]-*/;
} }


public static class SHA512 extends IosSHAMessageDigest { public static class SHA512 extends IosSHAMessageDigest {


public SHA512() { public SHA512() {
super("SHA-512"); super("SHA-512");
allocContext();
} }


@Override @Override
protected native byte[] engineDigest() /*-[ protected native int engineGetDigestLength() /*-[
IOSByteArray *bytes = [buffer_ toByteArray]; return CC_SHA512_DIGEST_LENGTH;
unsigned char digest[CC_SHA512_DIGEST_LENGTH]; ]-*/;
CC_SHA512(bytes->buffer_, bytes->size_, digest);
return [IOSByteArray arrayWithBytes:(jbyte *)digest count:CC_SHA512_DIGEST_LENGTH]; private native void allocContext() /*-[
self->shaCtx_ = (jlong)calloc(1, sizeof(CC_SHA512_CTX));
[self engineReset];
]-*/; ]-*/;


@Override @Override
protected native int engineGetDigestLength() /*-[ protected native void engineReset() /*-[
return CC_SHA512_DIGEST_LENGTH; CC_SHA512_Init((CC_SHA512_CTX *)shaCtx_);
]-*/;

@Override
protected native void engineUpdate(byte input) /*-[
CC_SHA512_Update((CC_SHA512_CTX *)shaCtx_, &input, 1);
]-*/;

@Override
protected native void engineUpdate(byte[] input, int offset, int len) /*-[
IOSArray_checkRange(input->size_, offset, len);
CC_SHA512_Update((CC_SHA512_CTX *)shaCtx_, input->buffer_ + offset, len);
]-*/;

@Override
protected native byte[] engineDigest() /*-[
IOSByteArray *md = [IOSByteArray arrayWithLength:CC_SHA512_DIGEST_LENGTH];
CC_SHA512_Final((unsigned char *)md->buffer_, (CC_SHA512_CTX *)shaCtx_);
[self engineReset];
return md;
]-*/;

public native Object clone() throws CloneNotSupportedException /*-[
ComGoogleJ2objcSecurityIosSHAMessageDigest_SHA512 *obj =
(ComGoogleJ2objcSecurityIosSHAMessageDigest_SHA512 *) [super java_clone];
ComGoogleJ2objcSecurityIosSHAMessageDigest_SHA512_allocContext(obj);
if (shaCtx_ != 0LL) {
memcpy((void *)obj->shaCtx_, (const void *)shaCtx_, sizeof(CC_SHA512_CTX));
}
return obj;
]-*/; ]-*/;
} }
} }

0 comments on commit 1c596e4

Please sign in to comment.