Skip to content

Commit

Permalink
Merge pull request #27 from ERussel/improve/key-compression
Browse files Browse the repository at this point in the history
add error handling for secp2561k1 public key uncompression
  • Loading branch information
ERussel authored Oct 13, 2021
2 parents b9df371 + 8158b32 commit 4033eb8
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
2 changes: 1 addition & 1 deletion IrohaCrypto.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'IrohaCrypto'
s.version = '0.8.2'
s.version = '0.9.0'
s.summary = 'Provides object oriented wrappers for C/C++ crypto functions used by blockchains.'

s.homepage = 'https://github.com/soramitsu'
Expand Down
2 changes: 1 addition & 1 deletion IrohaCrypto/Classes/secp256k1/SECPublicKey.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@

+ (NSUInteger)uncompressedLength;

- (nonnull NSData*)uncompressed;
- (nullable NSData*)uncompressed:(NSError*_Nullable*_Nullable)error;

@end
18 changes: 17 additions & 1 deletion IrohaCrypto/Classes/secp256k1/SECPublicKey.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#import "SECPublicKey.h"
#import <secp256k1/secp256k1.h>
#import "IRCryptoKey.h"

@interface SECPublicKey()

Expand Down Expand Up @@ -44,13 +45,26 @@ - (nullable instancetype)initWithRawData:(nonnull NSData*)data error:(NSError*_N
return self;
}

- (nonnull NSData*)uncompressed {
- (nullable NSData*)uncompressed:(NSError*_Nullable*_Nullable)error {
secp256k1_pubkey rawPublicKey;

secp256k1_context *context = secp256k1_context_create(SECP256K1_CONTEXT_NONE);

int status = secp256k1_ec_pubkey_parse(context, &rawPublicKey, _rawData.bytes, [_rawData length]);

if (status == 0) {
secp256k1_context_destroy(context);

if (error) {
NSString *message = [NSString stringWithFormat:@"Uncompressing unexpectedly failed"];
*error = [NSError errorWithDomain:NSStringFromClass([self class])
code:IRCryptoKeyErrorInvalidRawData
userInfo:@{NSLocalizedDescriptionKey: message}];
}

return nil;
}

size_t uncompressedLength = [SECPublicKey uncompressedLength];
unsigned char uncompresedPubkey[uncompressedLength];

Expand All @@ -60,6 +74,8 @@ - (nonnull NSData*)uncompressed {
&rawPublicKey,
SECP256K1_EC_UNCOMPRESSED);

secp256k1_context_destroy(context);

return [NSData dataWithBytes:uncompresedPubkey length:uncompressedLength];
}

Expand Down
8 changes: 7 additions & 1 deletion Tests/secp256k1/Secp256k1PublicKeyTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,13 @@ - (void)testPublicKeyUncompression {
return;
}

NSData *actualUncompressed = [publicKey uncompressed];
NSData *actualUncompressed = [publicKey uncompressed:&error];

if (error) {
XCTFail(@"Did receive error: %@", [error localizedDescription]);
return;
}

NSData *expectedUncompressed = [[NSData alloc] initWithHexString:UNCOMPRESSED[index] error:&error];

if (error) {
Expand Down

0 comments on commit 4033eb8

Please sign in to comment.