Use non-swappable memory

This commit is contained in:
Jonathan Schleifer 2020-01-03 01:03:58 +01:00
parent 0321101564
commit d3f7b49a5c
No known key found for this signature in database
GPG key ID: 79D21189A2D4708D
7 changed files with 101 additions and 123 deletions

View file

@ -1,7 +1,7 @@
/* /*
* Copyright (c) 2016 - 2019 Jonathan Schleifer <js@heap.zone> * Copyright (c) 2016 - 2020 Jonathan Schleifer <js@nil.im>
* *
* https://heap.zone/git/cryptopassphrase.git * https://nil.im/git/cryptopassphrase.git
* *
* Permission to use, copy, modify, and/or distribute this software for any * Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above * purpose with or without fee is hereby granted, provided that the above

View file

@ -1,7 +1,7 @@
/* /*
* Copyright (c) 2016 - 2019 Jonathan Schleifer <js@heap.zone> * Copyright (c) 2016 - 2020 Jonathan Schleifer <js@nil.im>
* *
* https://heap.zone/git/cryptopassphrase.git * https://nil.im/git/cryptopassphrase.git
* *
* Permission to use, copy, modify, and/or distribute this software for any * Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above * purpose with or without fee is hereby granted, provided that the above
@ -64,7 +64,9 @@ showHelp(OFStream *output, bool verbose)
OFMutableData *keyFile = nil; OFMutableData *keyFile = nil;
OFString *prompt; OFString *prompt;
const char *promptCString; const char *promptCString;
char *passphrase; char *passphraseCString;
size_t passphraseLength;
OFSecureData *passphrase;
while ((option = [optionsParser nextOption]) != '\0') { while ((option = [optionsParser nextOption]) != '\0') {
switch (option) { switch (option) {
@ -143,56 +145,42 @@ showHelp(OFStream *output, bool verbose)
if (keyFilePath != nil) if (keyFilePath != nil)
keyFile = [OFMutableData dataWithContentsOfFile: keyFilePath]; keyFile = [OFMutableData dataWithContentsOfFile: keyFilePath];
passphrase = getpass(promptCString); passphraseCString = getpass(promptCString);
passphraseLength = strlen(passphraseCString);
@try { @try {
passphrase = [OFSecureData dataWithCount: passphraseLength + 1
allowsSwappableMemory: false];
memcpy(passphrase.mutableItems, passphraseCString,
passphraseLength + 1);
} @finally {
of_explicit_memset(passphraseCString, '\0', passphraseLength);
}
if (_repeat) { if (_repeat) {
char *passphraseCopy = of_strdup(passphrase); of_string_encoding_t encoding = [OFLocale encoding];
if (passphraseCopy == NULL)
@throw [OFOutOfMemoryException exception];
@try {
of_string_encoding_t encoding =
[OFLocale encoding];
prompt = [OFString stringWithFormat: prompt = [OFString stringWithFormat:
@"Repeat passphrase for site \"%@\": ", @"Repeat passphrase for site \"%@\": ", generator.site];
generator.site]; passphraseCString =
passphrase = getpass( getpass([prompt cStringWithEncoding: encoding]);
[prompt cStringWithEncoding: encoding]);
if (strcmp(passphrase, passphraseCopy) != 0) { if (strcmp(passphraseCString, passphrase.items) != 0) {
[of_stderr writeString: [of_stderr writeString: @"Passphrases do not match!\n"];
@"Passphrases do not match!\n"];
[OFApplication terminateWithStatus: 1]; [OFApplication terminateWithStatus: 1];
} }
} @finally {
of_explicit_memset(passphraseCopy, 0, of_explicit_memset(passphraseCString, '\0',
strlen(passphraseCopy)); strlen(passphraseCString));
free(passphraseCopy);
}
} }
generator.keyFile = keyFile; generator.keyFile = keyFile;
generator.passphrase = passphrase; generator.passphrase = passphrase;
[generator derivePassword]; [generator derivePassword];
@try { [of_stdout writeBuffer: generator.output.items
[of_stdout writeBuffer: generator.output
length: generator.length]; length: generator.length];
[of_stdout writeBuffer: "\n" [of_stdout writeBuffer: "\n"
length: 1]; length: 1];
} @finally {
of_explicit_memset(generator.output, 0,
generator.length);
}
} @finally {
of_explicit_memset(passphrase, 0, strlen(passphrase));
if (keyFile != nil)
of_explicit_memset(keyFile.mutableItems, 0,
keyFile.count);
}
[OFApplication terminate]; [OFApplication terminate];
} }

View file

@ -1,7 +1,7 @@
/* /*
* Copyright (c) 2016 - 2019 Jonathan Schleifer <js@heap.zone> * Copyright (c) 2016 - 2020 Jonathan Schleifer <js@nil.im>
* *
* https://heap.zone/git/cryptopassphrase.git * https://nil.im/git/cryptopassphrase.git
* *
* Permission to use, copy, modify, and/or distribute this software for any * Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above * purpose with or without fee is hereby granted, provided that the above
@ -27,7 +27,6 @@
size_t _length; size_t _length;
OFString *_site; OFString *_site;
OFData *_keyFile; OFData *_keyFile;
const char *_passphrase; OFSecureData *_passphrase, *_output;
unsigned char *_output;
} }
@end @end

View file

@ -1,7 +1,7 @@
/* /*
* Copyright (c) 2016 - 2019 Jonathan Schleifer <js@heap.zone> * Copyright (c) 2016 - 2020 Jonathan Schleifer <js@nil.im>
* *
* https://heap.zone/git/cryptopassphrase.git * https://nil.im/git/cryptopassphrase.git
* *
* Permission to use, copy, modify, and/or distribute this software for any * Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above * purpose with or without fee is hereby granted, provided that the above
@ -58,19 +58,19 @@
OFSHA256Hash *siteHash = [OFSHA256Hash OFSHA256Hash *siteHash = [OFSHA256Hash
cryptoHashWithAllowsSwappableMemory: true]; cryptoHashWithAllowsSwappableMemory: true];
size_t passphraseLength, combinedPassphraseLength; size_t passphraseLength, combinedPassphraseLength;
char *combinedPassphrase; OFSecureData *combinedPassphrase;
char *combinedPassphraseItems;
unsigned char *outputItems;
[siteHash updateWithBuffer: _site.UTF8String [siteHash updateWithBuffer: _site.UTF8String
length: _site.UTF8StringLength]; length: _site.UTF8StringLength];
if (_output != NULL) { [_output release];
of_explicit_memset(_output, 0, _length); _output = nil;
[self freeMemory: _output]; _output = [[OFSecureData alloc] initWithCount: _length + 1
} allowsSwappableMemory: false];
_output = [self allocMemoryWithSize: _length + 1]; passphraseLength = combinedPassphraseLength = _passphrase.count - 1;
passphraseLength = combinedPassphraseLength = strlen(_passphrase);
if (_keyFile != nil) { if (_keyFile != nil) {
if (SIZE_MAX - combinedPassphraseLength < _keyFile.count) if (SIZE_MAX - combinedPassphraseLength < _keyFile.count)
@throw [OFOutOfRangeException exception]; @throw [OFOutOfRangeException exception];
@ -78,36 +78,32 @@
combinedPassphraseLength += _keyFile.count; combinedPassphraseLength += _keyFile.count;
} }
if ((combinedPassphrase = malloc(combinedPassphraseLength)) == NULL) combinedPassphrase = [OFSecureData
@throw [OFOutOfMemoryException dataWithCount: combinedPassphraseLength
exceptionWithRequestedSize: combinedPassphraseLength]; allowsSwappableMemory: false];
@try { combinedPassphraseItems = combinedPassphrase.mutableItems;
memcpy(combinedPassphrase, _passphrase, passphraseLength); memcpy(combinedPassphraseItems, _passphrase.items, passphraseLength);
if (_keyFile != nil) if (_keyFile != nil)
memcpy(combinedPassphrase + passphraseLength, memcpy(combinedPassphraseItems + passphraseLength,
_keyFile.items, _keyFile.count); _keyFile.items, _keyFile.count);
of_scrypt(8, 524288, 2, siteHash.digest, outputItems = _output.mutableItems;
[siteHash.class digestSize], combinedPassphrase, of_scrypt(8, 524288, 2, siteHash.digest, [siteHash.class digestSize],
combinedPassphraseLength, _output, _length, true); combinedPassphraseItems, combinedPassphraseLength, outputItems,
} @finally { _length, true);
of_explicit_memset(combinedPassphrase, 0,
combinedPassphraseLength);
free(combinedPassphrase);
}
/* /*
* This has a bias, however, this is what scrypt-genpass does and the * This has a bias, however, this is what scrypt-genpass does and the
* legacy mode wants to be compatible to scrypt-genpass. * legacy mode wants to be compatible to scrypt-genpass.
*/ */
_output[0] = "abcdefghijklmnopqrstuvwxyz"[_output[0] % 26]; outputItems[0] = "abcdefghijklmnopqrstuvwxyz"[outputItems[0] % 26];
_output[1] = "0123456789"[_output[1] % 10]; outputItems[1] = "0123456789"[outputItems[1] % 10];
_output[2] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"[_output[2] % 26]; outputItems[2] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"[outputItems[2] % 26];
for (size_t i = 3; i < _length; i++) for (size_t i = 3; i < _length; i++)
_output[i] = "abcdefghijklmnopqrstuvwxyz" outputItems[i] = "abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"0123456789"[_output[i] % (26 + 26 + 10)]; "0123456789"[outputItems[i] % (26 + 26 + 10)];
} }
@end @end

View file

@ -1,7 +1,7 @@
/* /*
* Copyright (c) 2016 - 2019 Jonathan Schleifer <js@heap.zone> * Copyright (c) 2016 - 2020 Jonathan Schleifer <js@nil.im>
* *
* https://heap.zone/git/cryptopassphrase.git * https://nil.im/git/cryptopassphrase.git
* *
* Permission to use, copy, modify, and/or distribute this software for any * Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above * purpose with or without fee is hereby granted, provided that the above
@ -27,7 +27,6 @@
size_t _length; size_t _length;
OFString *_site; OFString *_site;
OFData *_keyFile; OFData *_keyFile;
const char *_passphrase; OFSecureData *_passphrase, *_output;
unsigned char *_output;
} }
@end @end

View file

@ -1,7 +1,7 @@
/* /*
* Copyright (c) 2016 - 2019 Jonathan Schleifer <js@heap.zone> * Copyright (c) 2016 - 2020 Jonathan Schleifer <js@nil.im>
* *
* https://heap.zone/git/cryptopassphrase.git * https://nil.im/git/cryptopassphrase.git
* *
* Permission to use, copy, modify, and/or distribute this software for any * Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above * purpose with or without fee is hereby granted, provided that the above
@ -45,19 +45,19 @@
OFSHA384Hash *siteHash = [OFSHA384Hash OFSHA384Hash *siteHash = [OFSHA384Hash
cryptoHashWithAllowsSwappableMemory: true]; cryptoHashWithAllowsSwappableMemory: true];
size_t passphraseLength, combinedPassphraseLength; size_t passphraseLength, combinedPassphraseLength;
char *combinedPassphrase; OFSecureData *combinedPassphrase;
char *combinedPassphraseItems;
unsigned char *outputItems;
[siteHash updateWithBuffer: _site.UTF8String [siteHash updateWithBuffer: _site.UTF8String
length: _site.UTF8StringLength]; length: _site.UTF8StringLength];
if (_output != NULL) { [_output release];
of_explicit_memset(_output, 0, _length); _output = nil;
[self freeMemory: _output]; _output = [[OFSecureData alloc] initWithCount: _length + 1
} allowsSwappableMemory: false];
_output = [self allocMemoryWithSize: _length + 1]; passphraseLength = combinedPassphraseLength = _passphrase.count - 1;
passphraseLength = combinedPassphraseLength = strlen(_passphrase);
if (_keyFile != nil) { if (_keyFile != nil) {
if (SIZE_MAX - combinedPassphraseLength < _keyFile.count) if (SIZE_MAX - combinedPassphraseLength < _keyFile.count)
@throw [OFOutOfRangeException exception]; @throw [OFOutOfRangeException exception];
@ -65,30 +65,26 @@
combinedPassphraseLength += _keyFile.count; combinedPassphraseLength += _keyFile.count;
} }
if ((combinedPassphrase = malloc(combinedPassphraseLength)) == NULL) combinedPassphrase = [OFSecureData
@throw [OFOutOfMemoryException dataWithCount: combinedPassphraseLength
exceptionWithRequestedSize: combinedPassphraseLength]; allowsSwappableMemory: false];
@try { combinedPassphraseItems = combinedPassphrase.mutableItems;
memcpy(combinedPassphrase, _passphrase, passphraseLength); memcpy(combinedPassphraseItems, _passphrase.items, passphraseLength);
if (_keyFile != nil) if (_keyFile != nil)
memcpy(combinedPassphrase + passphraseLength, memcpy(combinedPassphraseItems + passphraseLength,
_keyFile.items, _keyFile.count); _keyFile.items, _keyFile.count);
of_scrypt(8, 524288, 2, siteHash.digest, outputItems = _output.mutableItems;
[siteHash.class digestSize], combinedPassphrase, of_scrypt(8, 524288, 2, siteHash.digest, [siteHash.class digestSize],
combinedPassphraseLength, _output, _length, true); combinedPassphraseItems, combinedPassphraseLength, outputItems,
} @finally { _length, true);
of_explicit_memset(combinedPassphrase, 0,
combinedPassphraseLength);
free(combinedPassphrase);
}
for (size_t i = 0; i < _length; i++) for (size_t i = 0; i < _length; i++)
_output[i] = outputItems[i] =
"123456789" "123456789"
"abcdefghijkmnopqrstuvwxyz" "abcdefghijkmnopqrstuvwxyz"
"ABCDEFGHJKLMNPQRSTUVWXYZ" "ABCDEFGHJKLMNPQRSTUVWXYZ"
"#$%-=?"[_output[i] & 0x3F]; "#$%-=?"[outputItems[i] & 0x3F];
} }
@end @end

View file

@ -1,7 +1,7 @@
/* /*
* Copyright (c) 2016 - 2019 Jonathan Schleifer <js@heap.zone> * Copyright (c) 2016 - 2020 Jonathan Schleifer <js@nil.im>
* *
* https://heap.zone/git/cryptopassphrase.git * https://nil.im/git/cryptopassphrase.git
* *
* Permission to use, copy, modify, and/or distribute this software for any * Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above * purpose with or without fee is hereby granted, provided that the above
@ -26,8 +26,8 @@
@property (nonatomic) size_t length; @property (nonatomic) size_t length;
@property (copy, nonatomic) OFString *site; @property (copy, nonatomic) OFString *site;
@property (retain, nonatomic) OFData *keyFile; @property (retain, nonatomic) OFData *keyFile;
@property (nonatomic) const char *passphrase; @property (retain) OFSecureData *passphrase;
@property (readonly, nonatomic) unsigned char *output; @property (readonly, nonatomic) OFSecureData *output;
+ (instancetype)generator; + (instancetype)generator;
- (void)derivePassword; - (void)derivePassword;