Make peerId stateless, imply from blocks

Will do the same with compressionFraming.
This commit is contained in:
Davide De Rosa 2018-08-30 12:55:02 +02:00
parent e57ef9fb86
commit 31924c6038
6 changed files with 16 additions and 18 deletions

View File

@ -51,7 +51,6 @@ NS_ASSUME_NONNULL_BEGIN
@interface DataPathCryptoAEAD : NSObject <DataPathEncrypter, DataPathDecrypter>
@property (nonatomic, assign) uint32_t peerId;
@property (nonatomic, assign) CompressionFraming compressionFraming;
- (instancetype)initWithCrypto:(nonnull CryptoAEAD *)crypto;

View File

@ -264,15 +264,16 @@ const NSInteger CryptoAEADTagLength = 16;
- (void)setPeerId:(uint32_t)peerId
{
_peerId = peerId & 0xffffff;
peerId &= 0xffffff;
if (_peerId == PacketPeerIdDisabled) {
if (peerId == PacketPeerIdDisabled) {
self.headerLength = 1;
self.crypto.extraLength = PacketIdLength;
self.crypto.extraPacketIdOffset = 0;
self.setDataHeader = ^(uint8_t *to, uint8_t key) {
PacketHeaderSet(to, PacketCodeDataV1, key);
};
self.checkPeerId = NULL;
}
else {
self.headerLength = 4;
@ -282,7 +283,7 @@ const NSInteger CryptoAEADTagLength = 16;
PacketHeaderSetDataV2(to, key, peerId);
};
self.checkPeerId = ^BOOL(const uint8_t *ptr) {
return (PacketHeaderGetDataV2PeerId(ptr) == self.peerId);
return (PacketHeaderGetDataV2PeerId(ptr) == peerId);
};
}
}
@ -328,7 +329,7 @@ const NSInteger CryptoAEADTagLength = 16;
*(uint32_t *)(ptr + self.headerLength) = htonl(packetId);
const uint8_t *extra = ptr; // AD = header + peer id + packet id
if (self.peerId == PacketPeerIdDisabled) {
if (!self.checkPeerId) {
extra += self.headerLength; // AD = packet id only
}
@ -354,7 +355,7 @@ const NSInteger CryptoAEADTagLength = 16;
- (BOOL)decryptDataPacket:(NSData *)packet into:(uint8_t *)dest length:(NSInteger *)length packetId:(uint32_t *)packetId error:(NSError *__autoreleasing *)error
{
const uint8_t *extra = packet.bytes; // AD = header + peer id + packet id
if (self.peerId == PacketPeerIdDisabled) {
if (!self.checkPeerId) {
extra += self.headerLength; // AD = packet id only
}

View File

@ -50,7 +50,6 @@ NS_ASSUME_NONNULL_BEGIN
@interface DataPathCryptoCBC : NSObject <DataPathEncrypter, DataPathDecrypter>
@property (nonatomic, assign) uint32_t peerId;
@property (nonatomic, assign) CompressionFraming compressionFraming;
- (instancetype)initWithCrypto:(nonnull CryptoCBC *)crypto;

View File

@ -266,13 +266,14 @@ const NSInteger CryptoCBCMaxHMACLength = 100;
- (void)setPeerId:(uint32_t)peerId
{
_peerId = peerId & 0xffffff;
peerId &= 0xffffff;
if (_peerId == PacketPeerIdDisabled) {
if (peerId == PacketPeerIdDisabled) {
self.headerLength = 1;
self.setDataHeader = ^(uint8_t *to, uint8_t key) {
PacketHeaderSet(to, PacketCodeDataV1, key);
};
self.checkPeerId = NULL;
}
else {
self.headerLength = 4;
@ -280,7 +281,7 @@ const NSInteger CryptoCBCMaxHMACLength = 100;
PacketHeaderSetDataV2(to, key, peerId);
};
self.checkPeerId = ^BOOL(const uint8_t *ptr) {
return (PacketHeaderGetDataV2PeerId(ptr) == self.peerId);
return (PacketHeaderGetDataV2PeerId(ptr) == peerId);
};
}
}

View File

@ -156,8 +156,8 @@
NSAssert(self.encrypter, @"Setting peer-id to nil encrypter");
NSAssert(self.decrypter, @"Setting peer-id to nil decrypter");
self.encrypter.peerId = peerId;
self.decrypter.peerId = peerId;
[self.encrypter setPeerId:peerId];
[self.decrypter setPeerId:peerId];
}
- (void)setCompressionFraming:(CompressionFraming)compressionFraming
@ -165,15 +165,15 @@
NSAssert(self.encrypter, @"Setting compressionFraming to nil encrypter");
NSAssert(self.decrypter, @"Setting compressionFraming to nil decrypter");
self.encrypter.compressionFraming = compressionFraming;
self.decrypter.compressionFraming = compressionFraming;
[self.encrypter setCompressionFraming:compressionFraming];
[self.decrypter setCompressionFraming:compressionFraming];
}
#pragma mark DataPath
- (NSArray<NSData *> *)encryptPackets:(NSArray<NSData *> *)packets key:(uint8_t)key error:(NSError *__autoreleasing *)error
{
NSAssert(self.encrypter.peerId == self.decrypter.peerId, @"Peer-id mismatch in DataPath encrypter/decrypter");
// NSAssert(self.encrypter.peerId == self.decrypter.peerId, @"Peer-id mismatch in DataPath encrypter/decrypter");
if (self.outPacketId > self.maxPacketId) {
if (error) {
@ -213,10 +213,9 @@
return self.outPackets;
}
//- (NSArray<NSData *> *)decryptPackets:(NSArray<NSData *> *)packets error:(NSError *__autoreleasing *)error
- (NSArray<NSData *> *)decryptPackets:(NSArray<NSData *> *)packets keepAlive:(bool *)keepAlive error:(NSError *__autoreleasing *)error
{
NSAssert(self.encrypter.peerId == self.decrypter.peerId, @"Peer-id mismatch in DataPath encrypter/decrypter");
// NSAssert(self.encrypter.peerId == self.decrypter.peerId, @"Peer-id mismatch in DataPath encrypter/decrypter");
[self.inPackets removeAllObjects];

View File

@ -39,7 +39,6 @@
@protocol DataPathChannel
- (int)overheadLength;
- (uint32_t)peerId;
- (void)setPeerId:(uint32_t)peerId;
- (CompressionFraming)compressionFraming;
- (void)setCompressionFraming:(CompressionFraming)compressionFraming;