2012-05-10 1 views
3

非常に基本的なバージョンのSecKeySign()を動作させるために問題が発生しました(動作中のOSX SecSignTransformCreate()/ SecTransformSetAttribute()/ SecTransformExecute iOSへ):SecKeyRawSignは、Appleの例であっても-50(errSecParam)エラーを返します。

コードはhttp://developer.apple.com/library/ios/#samplecode/CryptoExercise/Listings/Classes_SecKeyWrapper_m.htmlとほぼ同じですが、さらに単純化されています。

上記のリンクのように、最初にアップを設定します。変更はありません。

const char someData[] = "No one loves pain itself, but those who seek..."; 

NSData * blob = [NSData dataWithBytes:someData length:sizeof(someData)]; 
assert(blob); 

SecKeyRef publicKeyRef, privateKeyRef; 
int keySize = 2048; 

OSStatus sanityCheck = noErr; 
NSMutableDictionary * privateKeyAttr = [[NSMutableDictionary alloc] init]; 
NSMutableDictionary * publicKeyAttr = [[NSMutableDictionary alloc] init]; 
NSMutableDictionary * keyPairAttr = [[NSMutableDictionary alloc] init]; 

// attribute dictionaries for 2048 bit RSA key pair. 
// 
[keyPairAttr setObject:(__bridge id)kSecAttrKeyTypeRSA forKey:(__bridge id)kSecAttrKeyType]; 
[keyPairAttr setObject:[NSNumber numberWithUnsignedInteger:keySize] forKey:(__bridge id)kSecAttrKeySizeInBits]; 
[privateKeyAttr setObject:[NSNumber numberWithBool:YES] forKey:(__bridge id)kSecAttrIsPermanent]; 
[publicKeyAttr setObject:[NSNumber numberWithBool:YES] forKey:(__bridge id)kSecAttrIsPermanent]; 
[keyPairAttr setObject:privateKeyAttr forKey:(__bridge id)kSecPrivateKeyAttrs]; 
[keyPairAttr setObject:publicKeyAttr forKey:(__bridge id)kSecPublicKeyAttrs]; 

そして、実際の作業は、鍵ペアの生成で始まる:

sanityCheck = SecKeyGeneratePair((__bridge CFDictionaryRef)keyPairAttr, &publicKeyRef, &privateKeyRef); 
assert(sanityCheck == noErr); 

NSLog(@"Pub/Priv: %@/%@", publicKeyRef, privateKeyRef); 

私が見ることができるものであれば見事に動作します。

問題は、それらを使用して署名することです。あるいは、むしろ署名:

// Zero-ed Buffer for the signature. 
// 
size_t signatureBytesSize = SecKeyGetBlockSize(privateKeyRef); 
assert(signatureBytesSize == keySize/8); 

uint8_t * signatureBytes = malloc(signatureBytesSize * sizeof(uint8_t)); 
memset((void *)signatureBytes, 0x0, signatureBytesSize); 

// Sign the binary blob; with type 1 padding. 
// 
sanityCheck = SecKeyRawSign(privateKeyRef, 
          kSecPaddingPKCS1, 
          (const uint8_t *)[blob bytes], [blob length], 
          (uint8_t *)signatureBytes, &signatureBytesSize 
          ); 
assert(sanityCheck == noErr); 

常に-50/errSecParamを返す(関数に渡さつまたは複数のパラメータが有効ではありませんでした。)。

提案がありますか?これは実際のiPhoneにありますか?ありがとうございます。

Dw。

答えて

0

私が試すことができる限り、SecKeyRawSign()の入力はSHA_DIGEST_LENのデータブロックだけです。それ以外は拒否されます。

SHA1ハッシュはこの関数によって計算されるため、必要な領域を渡すだけで済みます。

SHA2や他のハッシュを指定する方法がまだ見つかりませんでした。

dw

+0

ハッシュアルゴリズムを渡していないと意味があります。しかし、私が第2引数としてPKCS1SHA1を指定すると、ドキュメントは最初にハッシュし(SHA_DIGEST_LENのデータブロックを生成する)、次に署名を行うことを提案します。だから私は自分自身を最初にハッシュしなければならないか、私のためにそれを行うために関数に頼るべきかどうかは分かりません。 –

+0

更新された回答。 –

関連する問題