From 89590ee17c41c4e790c1fbad471dfa9566e5a514 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Cbill=2Echan=E2=80=9D?= <“bill.chan@ums.ag”> Date: Mon, 13 Apr 2015 11:13:20 +0200 Subject: [PATCH] SHA512 signing support - (NSData *)signWithSHA512:(NSData *)message error:(NSError **)error implemented --- MIHCrypto/Core/MIHPrivateKey.h | 2 ++ MIHCrypto/RSA/MIHRSAPrivateKey.m | 31 ++++++++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) mode change 100644 => 100755 MIHCrypto/Core/MIHPrivateKey.h mode change 100644 => 100755 MIHCrypto/RSA/MIHRSAPrivateKey.m diff --git a/MIHCrypto/Core/MIHPrivateKey.h b/MIHCrypto/Core/MIHPrivateKey.h old mode 100644 new mode 100755 index 77f9446..f2f9f9a --- a/MIHCrypto/Core/MIHPrivateKey.h +++ b/MIHCrypto/Core/MIHPrivateKey.h @@ -55,4 +55,6 @@ */ - (NSData *)signWithSHA256:(NSData *)message error:(NSError **)error; +- (NSData *)signWithSHA512:(NSData *)message error:(NSError **)error; + @end \ No newline at end of file diff --git a/MIHCrypto/RSA/MIHRSAPrivateKey.m b/MIHCrypto/RSA/MIHRSAPrivateKey.m old mode 100644 new mode 100755 index 1b05cd2..db478f3 --- a/MIHCrypto/RSA/MIHRSAPrivateKey.m +++ b/MIHCrypto/RSA/MIHRSAPrivateKey.m @@ -157,7 +157,7 @@ - (NSData *)signWithSHA256:(NSData *)message error:(NSError **)error if (error) *error = [NSError errorFromOpenSSL]; return nil; } - + NSMutableData *signature = [NSMutableData dataWithLength:(NSUInteger) RSA_size(_rsa)]; unsigned int signatureLength = 0; if (RSA_sign(NID_sha256, messageDigest, SHA256_DIGEST_LENGTH, signature.mutableBytes, &signatureLength, _rsa) == 0) { @@ -166,7 +166,36 @@ - (NSData *)signWithSHA256:(NSData *)message error:(NSError **)error return nil; } [signature setLength:(NSUInteger) signatureLength]; + + return signature; +} +- (NSData *)signWithSHA512:(NSData *)message error:(NSError **)error +{ + SHA512_CTX sha512Ctx; + unsigned char messageDigest[SHA512_DIGEST_LENGTH]; + if (!SHA512_Init(&sha512Ctx)) { + if (error) *error = [NSError errorFromOpenSSL]; + return nil; + } + if (!SHA512_Update(&sha512Ctx, message.bytes, message.length)) { + if (error) *error = [NSError errorFromOpenSSL]; + return nil; + } + if (!SHA512_Final(messageDigest, &sha512Ctx)) { + if (error) *error = [NSError errorFromOpenSSL]; + return nil; + } + + NSMutableData *signature = [NSMutableData dataWithLength:(NSUInteger) RSA_size(_rsa)]; + unsigned int signatureLength = 0; + if (RSA_sign(NID_sha512, messageDigest, SHA512_DIGEST_LENGTH, signature.mutableBytes, &signatureLength, _rsa) == 0) { + if (error) + *error = [NSError errorFromOpenSSL]; + return nil; + } + [signature setLength:(NSUInteger) signatureLength]; + return signature; }