Expose methods for capacity prediction
Encapsulate encrypt/decrypt buffer capacity calculation.
This commit is contained in:
parent
f6ee187db7
commit
dd02c92aa5
|
@ -52,7 +52,8 @@ NS_ASSUME_NONNULL_BEGIN
|
|||
@property (nonatomic, strong, readonly) NSData *_Nullable payload;
|
||||
@property (nonatomic, strong) NSDate *_Nullable sentDate;
|
||||
|
||||
- (NSInteger)serializeTo:(nullable uint8_t *)to;
|
||||
- (NSInteger)capacity;
|
||||
- (NSInteger)serializeTo:(uint8_t *)to;
|
||||
- (NSData *)serialized;
|
||||
|
||||
@end
|
||||
|
|
|
@ -95,9 +95,6 @@
|
|||
// Ruby: send_ctrl
|
||||
- (NSInteger)serializeTo:(uint8_t *)to
|
||||
{
|
||||
if (!to) {
|
||||
return [self capacity];
|
||||
}
|
||||
uint8_t *ptr = to;
|
||||
ptr += PacketHeaderSet(ptr, self.code, self.key, self.sessionId.bytes);
|
||||
if (self.ackIds.count > 0) {
|
||||
|
@ -124,7 +121,7 @@
|
|||
ptr += self.payload.length;
|
||||
}
|
||||
}
|
||||
return (int)(ptr - to);
|
||||
return ptr - to;
|
||||
}
|
||||
|
||||
- (NSData *)serialized
|
||||
|
|
|
@ -102,6 +102,11 @@ const NSInteger CryptoAEADTagLength = 16;
|
|||
return 0;
|
||||
}
|
||||
|
||||
- (NSInteger)encryptionCapacityWithLength:(NSInteger)length
|
||||
{
|
||||
return safe_crypto_capacity(length, self.overheadLength);
|
||||
}
|
||||
|
||||
#pragma mark Encrypter
|
||||
|
||||
- (void)configureEncryptionWithCipherKey:(ZeroingData *)cipherKey hmacKey:(ZeroingData *)hmacKey
|
||||
|
@ -234,11 +239,6 @@ const NSInteger CryptoAEADTagLength = 16;
|
|||
|
||||
#pragma mark DataPathChannel
|
||||
|
||||
- (int)overheadLength
|
||||
{
|
||||
return self.crypto.overheadLength;
|
||||
}
|
||||
|
||||
- (void)setPeerId:(uint32_t)peerId
|
||||
{
|
||||
peerId &= 0xffffff;
|
||||
|
@ -265,6 +265,11 @@ const NSInteger CryptoAEADTagLength = 16;
|
|||
}
|
||||
}
|
||||
|
||||
- (NSInteger)encryptionCapacityWithLength:(NSInteger)length
|
||||
{
|
||||
return [self.crypto encryptionCapacityWithLength:length];
|
||||
}
|
||||
|
||||
#pragma mark DataPathEncrypter
|
||||
|
||||
- (void)assembleDataPacketWithBlock:(DataPathAssembleBlock)block packetId:(uint32_t)packetId payload:(NSData *)payload into:(uint8_t *)packetBytes length:(NSInteger *)packetLength
|
||||
|
@ -282,7 +287,7 @@ const NSInteger CryptoAEADTagLength = 16;
|
|||
|
||||
- (NSData *)encryptedDataPacketWithKey:(uint8_t)key packetId:(uint32_t)packetId packetBytes:(const uint8_t *)packetBytes packetLength:(NSInteger)packetLength error:(NSError *__autoreleasing *)error
|
||||
{
|
||||
const int capacity = self.headerLength + PacketIdLength + (int)safe_crypto_capacity(packetLength, self.crypto.overheadLength);
|
||||
const int capacity = self.headerLength + PacketIdLength + (int)[self.crypto encryptionCapacityWithLength:packetLength];
|
||||
NSMutableData *encryptedPacket = [[NSMutableData alloc] initWithLength:capacity];
|
||||
uint8_t *ptr = encryptedPacket.mutableBytes;
|
||||
NSInteger encryptedPacketLength = INT_MAX;
|
||||
|
|
|
@ -120,6 +120,11 @@ const NSInteger CryptoCBCMaxHMACLength = 100;
|
|||
return 0;
|
||||
}
|
||||
|
||||
- (NSInteger)encryptionCapacityWithLength:(NSInteger)length
|
||||
{
|
||||
return safe_crypto_capacity(length, self.overheadLength);
|
||||
}
|
||||
|
||||
#pragma mark Encrypter
|
||||
|
||||
- (void)configureEncryptionWithCipherKey:(ZeroingData *)cipherKey hmacKey:(ZeroingData *)hmacKey
|
||||
|
@ -276,11 +281,6 @@ const NSInteger CryptoCBCMaxHMACLength = 100;
|
|||
|
||||
#pragma mark DataPathChannel
|
||||
|
||||
- (int)overheadLength
|
||||
{
|
||||
return self.crypto.overheadLength;
|
||||
}
|
||||
|
||||
- (void)setPeerId:(uint32_t)peerId
|
||||
{
|
||||
peerId &= 0xffffff;
|
||||
|
@ -303,6 +303,11 @@ const NSInteger CryptoCBCMaxHMACLength = 100;
|
|||
}
|
||||
}
|
||||
|
||||
- (NSInteger)encryptionCapacityWithLength:(NSInteger)length
|
||||
{
|
||||
return [self.crypto encryptionCapacityWithLength:length];
|
||||
}
|
||||
|
||||
#pragma mark DataPathEncrypter
|
||||
|
||||
- (void)assembleDataPacketWithBlock:(DataPathAssembleBlock)block packetId:(uint32_t)packetId payload:(NSData *)payload into:(uint8_t *)packetBytes length:(NSInteger *)packetLength
|
||||
|
@ -323,7 +328,7 @@ const NSInteger CryptoCBCMaxHMACLength = 100;
|
|||
|
||||
- (NSData *)encryptedDataPacketWithKey:(uint8_t)key packetId:(uint32_t)packetId packetBytes:(const uint8_t *)packetBytes packetLength:(NSInteger)packetLength error:(NSError *__autoreleasing *)error
|
||||
{
|
||||
const int capacity = self.headerLength + (int)safe_crypto_capacity(packetLength, self.crypto.overheadLength);
|
||||
const int capacity = self.headerLength + (int)[self.crypto encryptionCapacityWithLength:packetLength];
|
||||
NSMutableData *encryptedPacket = [[NSMutableData alloc] initWithLength:capacity];
|
||||
uint8_t *ptr = encryptedPacket.mutableBytes;
|
||||
NSInteger encryptedPacketLength = INT_MAX;
|
||||
|
|
|
@ -120,7 +120,7 @@
|
|||
|
||||
- (void)adjustEncBufferToPacketSize:(int)size
|
||||
{
|
||||
const int neededCapacity = DataPathByteAlignment + (int)safe_crypto_capacity(size, self.encrypter.overheadLength);
|
||||
const int neededCapacity = DataPathByteAlignment + (int)[self.encrypter encryptionCapacityWithLength:size];
|
||||
if (self.encBufferCapacity >= neededCapacity) {
|
||||
return;
|
||||
}
|
||||
|
@ -132,7 +132,7 @@
|
|||
|
||||
- (void)adjustDecBufferToPacketSize:(int)size
|
||||
{
|
||||
const int neededCapacity = DataPathByteAlignment + (int)safe_crypto_capacity(size, self.decrypter.overheadLength);
|
||||
const int neededCapacity = DataPathByteAlignment + (int)[self.decrypter encryptionCapacityWithLength:size];
|
||||
if (self.decBufferCapacity >= neededCapacity) {
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -43,8 +43,8 @@ typedef void (^DataPathParseBlock)(uint8_t *payload, NSInteger *payloadOffset, N
|
|||
|
||||
@protocol DataPathChannel
|
||||
|
||||
- (int)overheadLength;
|
||||
- (void)setPeerId:(uint32_t)peerId;
|
||||
- (NSInteger)encryptionCapacityWithLength:(NSInteger)length;
|
||||
|
||||
@end
|
||||
|
||||
|
|
|
@ -48,9 +48,8 @@ NS_ASSUME_NONNULL_BEGIN
|
|||
|
||||
- (void)configureEncryptionWithCipherKey:(nullable ZeroingData *)cipherKey hmacKey:(nullable ZeroingData *)hmacKey;
|
||||
- (int)digestLength;
|
||||
- (int)overheadLength;
|
||||
- (int)extraLength;
|
||||
|
||||
- (NSInteger)encryptionCapacityWithLength:(NSInteger)length;
|
||||
- (BOOL)encryptBytes:(const uint8_t *)bytes length:(NSInteger)length dest:(uint8_t *)dest destLength:(NSInteger *)destLength extra:(nullable const uint8_t *)extra error:(NSError **)error;
|
||||
|
||||
- (id<DataPathEncrypter>)dataPathEncrypter;
|
||||
|
@ -62,9 +61,8 @@ NS_ASSUME_NONNULL_BEGIN
|
|||
|
||||
- (void)configureDecryptionWithCipherKey:(nullable ZeroingData *)cipherKey hmacKey:(nullable ZeroingData *)hmacKey;
|
||||
- (int)digestLength;
|
||||
- (int)overheadLength;
|
||||
- (int)extraLength;
|
||||
|
||||
- (NSInteger)encryptionCapacityWithLength:(NSInteger)length;
|
||||
- (BOOL)decryptBytes:(const uint8_t *)bytes length:(NSInteger)length dest:(uint8_t *)dest destLength:(NSInteger *)destLength extra:(nullable const uint8_t *)extra error:(NSError **)error;
|
||||
- (BOOL)verifyBytes:(const uint8_t *)bytes length:(NSInteger)length extra:(nullable const uint8_t *)extra error:(NSError **)error;
|
||||
|
||||
|
|
Loading…
Reference in New Issue