Signal-iOS/SignalServiceKit/tests/Devices/OWSProvisioningCipherTest.m
Evan Hahn 370ff654e7
Change license to AGPL
Change license to AGPL

This commit:

- Updates the `LICENSE` file

- Start every file with something like:

      // Copyright YEAR_FIRST_PUBLISHED Signal Messenger, LLC
      // SPDX-License-Identifier: AGPL-3.0-only

---

First, I removed existing license headers with this Ruby 3.1.2 script:

    require 'set'

    EXTENSIONS_TO_CHECK = Set['.h', '.hpp', '.cpp', '.m', '.mm', '.pch', '.swift']

    same = 0
    different = 0

    all_files = `git ls-files`.lines.map { |line| line.strip }
    all_files.each do |relative_path|
      if relative_path == 'Pods'
        next
      end

      unless EXTENSIONS_TO_CHECK.include? File.extname(relative_path)
        next
      end

      path = File.expand_path(relative_path)

      contents = File.read(path)
      new_contents = contents.sub(/\/\/\n\/\/  Copyright .*\n\/\/\n\n/, '')

      if contents == new_contents
        same += 1
      else
        different += 1
      end

      File.write(path, new_contents)
    end

    puts "updated #{different} file(s), left #{same} untouched"

I'm sure this script could be improved, but it worked well enough.

Then, I created `Scripts/lint/lint-license-headers` and ran it to auto-
fix a lot of files. This changed the mode of some files, but I think
that's actually desirable. For example,
`SignalServiceKit/src/Util/AppContext.m` previously had a mode of
`0755/-rwxr-xr-x`, and it's now `0644/-rw-r--r--`.

Then I fixed some stragglers and updated the precommit script.

See [a similar change in the Desktop app][0].

[0]: 8bfaf598af
2022-10-13 08:25:37 -05:00

157 lines
4.8 KiB
Objective-C

//
// Copyright 2017 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
//
#import "SSKBaseTestObjC.h"
#import <Curve25519Kit/Curve25519.h>
#import <SignalCoreKit/Cryptography.h>
#import <SignalServiceKit/SignalServiceKit-Swift.h>
@interface OWSProvisioningCipher(Testing)
// Expose private method for testing.
- (instancetype)initWithTheirPublicKey:(NSData *)theirPublicKey
ourKeyPair:(ECKeyPair *)ourKeyPair
initializationVector:(NSData *)initializationVector;
@end
@interface OWSProvisioningCipherTest : SSKBaseTestObjC
@end
@implementation OWSProvisioningCipherTest
- (NSData *)knownInitializationVector
{
uint8_t initilizationVectorBytes[] = {
0xec, 0x67, 0x0b, 0xb7,
0x18, 0xe1, 0xe9, 0x0a,
0xcc, 0x5e, 0xcb, 0x37,
0xab, 0x79, 0xe0, 0x09
};
return [NSData dataWithBytes:initilizationVectorBytes length:16];
}
- (NSData *)knownPublicKey
{
uint8_t knownPublicKeyBytes[] = {
0x5e, 0x23, 0xe8, 0x49,
0xb2, 0x23, 0x21, 0xdb,
0x2e, 0x3a, 0x77, 0x74,
0x6f, 0x3b, 0x44, 0x18,
0xcc, 0x6c, 0x81, 0xce,
0xd5, 0xc2, 0x91, 0xaf,
0xed, 0xfb, 0x21, 0x4e,
0x59, 0xcc, 0x19, 0xa4
};
return [NSData dataWithBytes:knownPublicKeyBytes length: 32];
}
- (ECKeyPair *)knownKeyPair
{
uint8_t privateKeyBytes[] = {
0x60, 0xfd, 0xc1, 0xeb,
0x6a, 0x68, 0x3d, 0x2b,
0x51, 0x23, 0x1f, 0xea,
0x1a, 0x5e, 0x80, 0x88,
0x0c, 0x65, 0x2d, 0x3d,
0x47, 0x9e, 0x28, 0xc1,
0x9f, 0x48, 0x2c, 0x66,
0xde, 0x48, 0x5d, 0x57
};
uint8_t publicKeyBytes[] = {
0x02, 0x62, 0x7b, 0x5c,
0x21, 0x15, 0x59, 0x1b,
0x37, 0xd1, 0xfe, 0xeb,
0x15, 0x5d, 0xd2, 0x95,
0x0a, 0xce, 0xe8, 0xb2,
0x1e, 0x8e, 0xc8, 0xd6,
0x53, 0x4f, 0x1a, 0xcd,
0xf2, 0x00, 0x98, 0x32
};
// Righteous hack to build a deterministic ECKeyPair
// The publicKey/privateKey ivars are private but it's possible to `initWithCoder:` given the proper keys.
NSKeyedArchiver *archiver = [NSKeyedArchiver new];
[archiver encodeBytes:publicKeyBytes length:ECCKeyLength forKey:@"TSECKeyPairPublicKey"];
[archiver encodeBytes:privateKeyBytes length:ECCKeyLength forKey:@"TSECKeyPairPrivateKey"];
NSData *serialized = [archiver encodedData];
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:serialized];
return [[ECKeyPair alloc] initWithCoder:unarchiver];
}
- (NSData *)knownData
{
uint8_t knownBytes[] = {
0x19, 0x33, 0x78, 0x64,
0x96, 0x56, 0xa7, 0xd0,
0x6e, 0xff, 0x37, 0x1d
};
return [NSData dataWithBytes:knownBytes length:12];
}
- (void)testEncrypt
{
NSData *theirPublicKey = [self knownPublicKey];
ECKeyPair *ourKeyPair = [self knownKeyPair];
NSData *initializationVector = [self knownInitializationVector];
OWSProvisioningCipher *cipher = [[OWSProvisioningCipher alloc] initWithTheirPublicKey:theirPublicKey
ourKeyPair:ourKeyPair
initializationVector:initializationVector];
NSData *message = [self knownData];
NSData *actualOutput = [cipher encrypt:message];
uint8_t expectedBytes[] = {
0x01, 0xec, 0x67, 0x0b,
0xb7, 0x18, 0xe1, 0xe9,
0x0a, 0xcc, 0x5e, 0xcb,
0x37, 0xab, 0x79, 0xe0,
0x09, 0xf7, 0x2b, 0xf7,
0x14, 0x3d, 0x45, 0xd7,
0x45, 0x79, 0x1e, 0x4f,
0x9d, 0x34, 0x8a, 0x2d,
0x43, 0x64, 0xd4, 0x7d,
0x48, 0x9a, 0xdc, 0x5a,
0xc3, 0x72, 0xfa, 0x63,
0x41, 0x7a, 0xa8, 0x45,
0x36, 0xe9, 0xc5, 0xcb,
0xee, 0x9b, 0xc1, 0x1f,
0xec, 0x31, 0x1e, 0xc2,
0x33, 0x2d, 0x95, 0x54,
0xcc
};
NSData *expectedOutput = [NSData dataWithBytes:expectedBytes length:65];
XCTAssertEqualObjects(expectedOutput, actualOutput);
}
- (void)testPadding
{
NSUInteger kBlockSize = 16;
for (int i = 0; i <= kBlockSize; i++) {
NSData *message = [Cryptography generateRandomBytes:i];
NSData *theirPublicKey = [self knownPublicKey];
ECKeyPair *ourKeyPair = [self knownKeyPair];
NSData *initializationVector = [self knownInitializationVector];
OWSProvisioningCipher *cipher = [[OWSProvisioningCipher alloc] initWithTheirPublicKey:theirPublicKey
ourKeyPair:ourKeyPair
initializationVector:initializationVector];
NSData *actualOutput = [cipher encrypt:message];
XCTAssertNotNil(actualOutput, @"failed for message length: %d", i);
}
}
@end