2 // KCAESGCMDuplexSession.m
7 #import <KeychainCircle/KCAESGCMDuplexSession.h>
8 #import <KeychainCircle/KCDer.h>
9 #import <KeychainCircle/KCError.h>
10 #import <NSError+KCCreationHelpers.h>
11 #import <NSData+SecRandom.h>
13 #include <corecrypto/ccaes.h>
14 #include <corecrypto/ccmode.h>
15 #include <corecrypto/cchkdf.h>
16 #include <corecrypto/ccsha2.h>
18 #include <corecrypto/ccder.h>
20 #include <libkern/OSByteOrder.h>
23 #define kdfInfoForwardString "send->recv"
24 #define kdfInfoBackwardString "recv->send"
25 static NSData* kdfInfoSendToReceive = nil;
26 static NSData* kdfInfoReceiveToSend = nil;
28 static const int kKCAESGCMTagSize = CCAES_KEY_SIZE_128;
29 static const int kKCAESGCMKeySize = CCAES_KEY_SIZE_128;
31 static bool derive_and_init(const struct ccmode_gcm *mode, ccgcm_ctx* ctx, NSData* sharedSecret, NSData* info) {
32 const struct ccdigest_info *di = ccsha256_di();
34 NSMutableData* space = [NSMutableData dataWithLength:di->output_size];
38 cc_status = cchkdf(di,
39 sharedSecret.length, sharedSecret.bytes,
41 info.length, info.bytes,
42 space.length, space.mutableBytes);
47 // We only use the first 16 bytes (128 bits) for the key.
48 cc_status = ccgcm_init(mode, ctx, kKCAESGCMKeySize, space.bytes);
49 cc_clear(space.length, space.mutableBytes);
51 return cc_status == 0;
54 @interface NSMutableData(KAESGCM)
55 - (void) replaceTrailingWith7LSB: (uint64_t) value;
58 @implementation NSMutableData(KAESGCM)
59 - (void) replaceTrailingWith7LSB: (uint64_t) value {
60 uint8_t bytes[sizeof(value)];
61 OSWriteBigInt64(bytes, 0, value);
63 [self replaceBytesInRange: NSMakeRange(self.length - 7, 7) withBytes: (bytes + 1)];
69 @interface KCAESGCMDuplexSession ()
70 @property (readwrite) bool asSender;
71 @property (readwrite) uint64_t context;
72 @property (readwrite) NSData* secret;
74 @property (readwrite) ccgcm_ctx * send;
75 @property (readwrite) ccgcm_ctx * receive;
79 @implementation KCAESGCMDuplexSession
81 + (nullable instancetype) sessionAsSender: (NSData*) sharedSecret
82 context: (uint64_t) context {
83 return [[KCAESGCMDuplexSession alloc] initAsSender:sharedSecret
87 + (nullable instancetype) sessionAsReceiver: (NSData*) sharedSecret
88 context: (uint64_t) context {
89 return [[KCAESGCMDuplexSession alloc] initAsReceiver:sharedSecret
94 static NSString* KCDSSender = @"asSender";
95 static NSString* KCDSSecret = @"secret";
96 static NSString* KCDSContext = @"context";
97 static NSString* KCDSPairingUUID = @"uuid";
98 static NSString* KCDSPiggybackingVersion = @"piggy";
99 static NSString* KCDSEpoch= @"epoch";
101 - (void)encodeWithCoder:(NSCoder *)aCoder {
102 [aCoder encodeBool: self.asSender forKey:KCDSSender];
103 [aCoder encodeObject: self.secret forKey:KCDSSecret];
104 [aCoder encodeInt64: self.context forKey:KCDSContext];
105 [aCoder encodeObject: self.pairingUUID forKey:KCDSPairingUUID];
106 [aCoder encodeInt64: self.piggybackingVersion forKey:KCDSPiggybackingVersion];
107 [aCoder encodeInt64:self.epoch forKey:KCDSEpoch];
110 - (nullable instancetype)initWithCoder:(NSCoder *)aDecoder {
111 bool asSender = [aDecoder decodeBoolForKey:KCDSSender];
112 NSData* secret = [aDecoder decodeObjectOfClass:[NSData class] forKey:KCDSSecret];
113 uint64_t context = [aDecoder decodeInt64ForKey:KCDSContext];
115 NSString* pairingUUID = [aDecoder decodeObjectOfClass:[NSString class] forKey:KCDSPairingUUID];
116 uint64_t piggybackingVersion = [aDecoder decodeInt64ForKey:KCDSPiggybackingVersion];
117 uint64_t epoch = [aDecoder decodeInt64ForKey:KCDSEpoch];
119 return [self initWithSecret:secret context:context as:asSender pairingUUID:pairingUUID piggybackingVersion:piggybackingVersion epoch:epoch];
122 + (BOOL)supportsSecureCoding {
128 - (nullable instancetype) initAsSender: (NSData*) sharedSecret context: (uint64_t) context {
129 return [self initWithSecret:sharedSecret context:context as:true];
132 - (nullable instancetype) initAsReceiver: (NSData*) sharedSecret context: (uint64_t) context {
133 return [self initWithSecret:sharedSecret context:context as:false];
136 - (nullable instancetype) initWithSecret: (NSData*) sharedSecret
137 context: (uint64_t) context
139 return [self initWithSecret:sharedSecret
143 piggybackingVersion:0
147 - (nullable instancetype)initWithSecret:(NSData*)sharedSecret
148 context:(uint64_t)context
150 pairingUUID:(NSString* _Nullable)pairingUUID
151 piggybackingVersion:(uint64_t)piggybackingVersion
152 epoch:(uint64_t)epoch
154 static dispatch_once_t onceToken;
155 dispatch_once(&onceToken, ^{
156 kdfInfoSendToReceive = [NSData dataWithBytesNoCopy: kdfInfoForwardString
157 length: strlen(kdfInfoForwardString)
158 freeWhenDone: false];
160 kdfInfoReceiveToSend = [NSData dataWithBytesNoCopy: kdfInfoBackwardString
161 length: strlen(kdfInfoBackwardString)
162 freeWhenDone: false];
165 if ((self = [super init])) {
166 self.asSender = sender;
167 self.secret = sharedSecret;
168 self.send = malloc(ccgcm_context_size(ccaes_gcm_encrypt_mode()));
169 self.receive = malloc(ccgcm_context_size(ccaes_gcm_decrypt_mode()));
170 self.context = context;
172 _pairingUUID = pairingUUID;
173 _piggybackingVersion = piggybackingVersion;
176 if (self.send == nil || self.receive == nil) {
180 derive_and_init(ccaes_gcm_encrypt_mode(),
181 self.send, self.secret,
182 sender ? kdfInfoSendToReceive : kdfInfoReceiveToSend);
183 derive_and_init(ccaes_gcm_decrypt_mode(),
184 self.receive, self.secret,
185 !sender ? kdfInfoSendToReceive : kdfInfoReceiveToSend);
190 - (size_t) encryptCapsuleSize: (NSData*) plaintext IV: (NSData*) iv {
191 size_t iv_size = kcder_sizeof_data(iv, nil);
195 size_t text_size = kcder_sizeof_data(plaintext, nil);
196 if (text_size == 0) {
199 size_t tag_size = kcder_sizeof_data([NSMutableData dataWithLength: kKCAESGCMTagSize], nil);
203 return ccder_sizeof(CCDER_CONSTRUCTED_SEQUENCE, iv_size + text_size + tag_size);
206 - (bool) GCM:(const struct ccmode_gcm*) mode
207 context:(ccgcm_ctx*) ctx
209 size:(size_t) data_size
210 data:(const uint8_t*) data
211 processed:(uint8_t*) result
212 tag:(uint8_t*) tagBuffer
213 error:(NSError**) error {
216 cc_status = ccgcm_reset(mode, ctx);
217 if (!CoreCryptoError(cc_status, error, @"ccgcm_reset failed: %d", cc_status))
220 cc_status = ccgcm_set_iv(mode, ctx, iv.length, iv.bytes);
221 if (!CoreCryptoError(cc_status, error, @"ccgcm_set_iv failed: %d", cc_status))
224 cc_status = ccgcm_update(mode, ctx, data_size, data, result);
225 if (!CoreCryptoError(cc_status, error, @"ccgcm_update failed: %d", cc_status))
228 cc_status = ccgcm_finalize(mode, ctx, kKCAESGCMTagSize, tagBuffer);
229 return CoreCryptoError(cc_status, error, @"ccgcm_finalize failed: %d", cc_status);
233 - (nullable NSData*) encrypt: (NSData*) data error: (NSError**) error {
234 static const int kIVSizeInBytes = 16;
236 NSMutableData* iv = [NSMutableData dataWithRandomBytes: kIVSizeInBytes];
238 NSMutableData* result = [NSMutableData dataWithLength: [self encryptCapsuleSize: data IV: iv]];
240 // Encode with all the space set up for the result:
242 uint8_t* der_end = result.mutableBytes + result.length;
243 const uint8_t* der = result.bytes;
246 uint8_t* encrypted = NULL;
248 der_end = ccder_encode_constructed_tl(CCDER_CONSTRUCTED_SEQUENCE, der_end, der,
249 kcder_encode_data(iv, error, der,
250 kcder_encode_raw_octet_space(data.length, &encrypted, der,
251 kcder_encode_raw_octet_space(kKCAESGCMTagSize, &tag, der, der_end))));
253 if (der_end != der) {
254 KCJoiningErrorCreate(kAllocationFailure, error, @"Failed to allocate space for der");
258 const struct ccmode_gcm * mode = ccaes_gcm_encrypt_mode();
260 return [self GCM:mode
267 error:error] ? result : nil;
270 - (nullable NSData*) decryptAndVerify: (NSData*) data error: (NSError**) error {
272 const uint8_t *der = data.bytes;
273 const uint8_t *der_end = der + data.length;
275 const uint8_t *sequence_end = 0;
276 der = ccder_decode_constructed_tl(CCDER_CONSTRUCTED_SEQUENCE, &sequence_end, der, der_end);
278 if (der == NULL || sequence_end != der_end) {
279 KCJoiningErrorCreate(kDERUnknownEncoding, error, @"decode failed");
283 const uint8_t *encrypted = 0;
284 size_t encrypted_len = 0;
285 const uint8_t *received_tag = 0;
289 der = kcder_decode_data(&iv, error, der, der_end);
290 if (der == NULL) return nil;
292 encrypted = ccder_decode_constructed_tl(CCDER_OCTET_STRING, &der, der, der_end);
293 encrypted_len = der - encrypted;
295 received_tag = ccder_decode_constructed_tl(CCDER_OCTET_STRING, &der, der, der_end);
298 KCJoiningErrorCreate(kDERUnknownEncoding, error, @"Decode failure");
302 if (der != der_end) {
303 KCJoiningErrorCreate(kDERUnknownEncoding, error, @"Extra space");
307 if (der - received_tag != kKCAESGCMTagSize) {
308 KCJoiningErrorCreate(kDERUnknownEncoding, error, @"Unexpected tag size: %ld", (long)(der - received_tag));
312 NSMutableData* decrypted = [NSMutableData dataWithLength: encrypted_len];
314 uint8_t tag[kKCAESGCMTagSize];
315 memcpy(tag, received_tag, sizeof(tag));
317 const struct ccmode_gcm * mode = ccaes_gcm_decrypt_mode();
319 return [self GCM:mode
324 processed:decrypted.mutableBytes
326 error:error] ? decrypted : nil;
329 #pragma clang diagnostic push
330 #pragma clang diagnostic ignored "-Wdeprecated-implementations"
333 ccgcm_ctx_clear(sizeof(*self.send), self.send);
337 ccgcm_ctx_clear(sizeof(*self.receive), self.receive);
342 #pragma clang diagnostic pop