How to require two PRIVATE keyholders to decrypt a document? - cryptography

Consider 3 entities A,B and C. I want A to be able to encrypt a document that can be decrypted only when keys of both B and C are combined. This question talks about a similar problem, but it uses AES.
In my usecase, I don't want the keys to be shared across the entities. There is one solution that was coming to my mind:
A encrypts the document (with RSA) first using public key of B and then public key of C. Then the document is decrypted using private key of C and then private key of B.
Is this approach feasible? Is there a better way?
Note: I tried doing this but got this error when I was doing encryption for the 2nd time: Data must not be longer than 117 bytes.

Related

Is there a one to one correlation between pub/priv key pairs and account IDs on the Hedera Network?

One public key usually corresponds to one or a specific number of addresses in other cryptocurrencies based on some algorithm.
https://testnet.dragonglass.me/hedera/transactions/0025330611632361982325526043
https://testnet.dragonglass.me/hedera/transactions/0025330611632362160372571164
In hedera, as the above two transactions have shown, using the same public key to send out two accountCreate transactions gives back two different accountId.
Is there an API whose params are a public key and who returns all the account Ids corresponding to the public key?
The mirror nodes support querying based on public key. However, Hedera's accounts are not 1:1 with a public key, you can't assume a public key is linked to a single account, in fact, the same public key can be used across multiple accounts meaning the same private key can sign transactions for all these accounts.
Furthermore, an account can be associated to a list of public keys meaning every key has to sign, or a threshold list which is a list where say 5 of 10 have to sign. All these can be nested too !
Finally, subject to having the private key to sign the transaction, you can update an account's public key. What you can't do is derive the account id from the public key. The account id comes from a sequence of numbers, each new entity (an account is an entity as are topics, tokens, etc...) is given the next id in the sequence.

Can we use the same pair of public and private keys for more than two communications at the same time?

Can we use same pair of public and private keys for more than two communications at the same time?
Or do we need to get a different pair of keys for different communications (at the same time)?
For example:
There are 3 persons: A B C.
Now should A share the same public key with both B and C or should it generate two pairs of public and private keys and give one public key to each?
The same key pair can be used for all communications but to make it secure we need the peers to derive the common key for each specific conversation. Example of such method is TLS protocol where the common key for each conversation is created using the same server peer key pair.

Mifare 1k : use on the same sector only key A for one block and only key B for another block

I want on a Mifare 1K card make the data of the block 1 on the sector 0 only readable by the key A, and the data of the block 2 on the sector 0 only readable by the B key (For this problem i don't care about the writing right on those block)
Regarding the data block access bit rules from the data sheet for Mifare 1K
Table of access bit rules for data block
I have a doubt about one thing. You have 3 possibilities (Never, Key B, Key A|B). Key A|B means Key A or Key B.
Is that means that if your key B is not readable on the sector trailer (so active) you can do with the Key B every action that the Key A can do ?
So it's impossible on the same sector to have one block readable only by the key A and another block only readable by the Key B
Thank you for your answers
You are right. The whole sector is controlled by the last block of each sector in Mifare 1k.Therefore, it is impossible to have one block readable only by key A and another only by key B.
The only solution left is to use blocks from two different sectors to fulfill your requirement.
It's possible. Using correct access bits combination. So playing it you can set read block 0 with key A, write with key B, read/write block 1 by key B and so on.

Can we have multple private Keys with single public key?

I am encrypting data with public key and decrypting data with private key.
Is there a possibility of having multiple private keys with a single public key?
No you cant that is an essence of Public Private key encryption. Why do u want to have multiple private keys for a single public key? What if u want to sign an encrypted file?
If that would be possible I would be able to send encrypted file to your friends and sign it with my crafted private key?
I suppose in theory it depends on the algorithm.
But with the ones we generally use (RSA, ECC), there is a 1:1 correspondence, so: no.
Not having a 1:1 correspondence seems like a bad thing. After all, you want to know that only a specified person can be the sender/recipient.
If you want group conversations, this is usually done by encrypting the message with a symmetric session key (you'd do that anyway in all likelihood, because public-key-crypto does not scale well for large messages), and then encrypting the session key (separately) for multiple recipients.
Its not feasible to make multiple private keys from one public key. First a key is generated which will be your private key. The public key pair is generated from this private key. Only one public key can be generated from one key. Here is a simple command to generate public key using RSA algorithm
rsa -in <yourkeyname(private key)> -pubout -out <publickeyname>

What makes a keychain item unique (in iOS)?

My question concerns keychains in iOS (iPhone, iPad, ...). I think (but am not sure) that the implementation of keychains under Mac OS X raises the same question with the same answer.
iOS provides five types (classes) of keychain items. You must chose one of those five values for the key kSecClass to determine the type:
kSecClassGenericPassword used to store a generic password
kSecClassInternetPassword used to store an internet password
kSecClassCertificate used to store a certificate
kSecClassKey used to store a kryptographic key
kSecClassIdentity used to store an identity (certificate + private key)
After long time of reading apples documentation, blogs and forum-entries, I found out that a keychain item of type kSecClassGenericPassword gets its uniqueness from the attributes kSecAttrAccessGroup, kSecAttrAccount and kSecAttrService.
If those three attributes in request 1 are the same as in request 2, then you receive the same generic password keychain item, regardless of any other attributes. If one (or two or all) of this attributes changes its value, then you get different items.
But kSecAttrService is only available for items of type kSecClassGenericPassword, so it can't be part of the "unique key" of an item of any other type, and there seems to be no documentation that points out clearly which attributes uniquely determine a keychain item.
The sample code in the class "KeychainItemWrapper" of "GenericKeychain" uses the attribute kSecAttrGeneric to make an item unique, but this is a bug. The two entries in this example only are stored as two distinct entries, because their kSecAttrAccessGroup is different (one has the access group set, the other lets it free). If you try to add a 2nd password without an access group, using Apple's KeychainItemWrapper, you will fail.
So, please, answer my questions:
Is it true, that the combination of kSecAttrAccessGroup, kSecAttrAccount and kSecAttrService is the "unique key" of a keychain item whose kSecClass is kSecClassGenericPassword?
Which attributes makes a keychain item unique if its kSecClass is not kSecClassGenericPassword?
The primary keys are as follows (derived from open source files from Apple, see Schema.m4, KeySchema.m4 and SecItem.cpp):
For a keychain item of class kSecClassGenericPassword, the primary key is the combination of
kSecAttrAccount and kSecAttrService.
For a keychain item of class kSecClassInternetPassword, the primary key is the combination of kSecAttrAccount, kSecAttrSecurityDomain, kSecAttrServer, kSecAttrProtocol, kSecAttrAuthenticationType, kSecAttrPort and kSecAttrPath.
For a keychain item of class kSecClassCertificate, the primary key is the combination of kSecAttrCertificateType, kSecAttrIssuer and kSecAttrSerialNumber.
For a keychain item of class kSecClassKey, the primary key is the combination of kSecAttrApplicationLabel, kSecAttrApplicationTag, kSecAttrKeyType,
kSecAttrKeySizeInBits, kSecAttrEffectiveKeySize, and the creator, start date and end date which are not exposed by SecItem yet.
For a keychain item of class kSecClassIdentity I haven't found info on the primary key fields in the open source files, but as an identity is the combination of a private key and a certificate, I assume the primary key is the combination of the primary key fields for kSecClassKey and kSecClassCertificate.
As each keychain item belongs to a keychain access group, it feels like the keychain access group (field kSecAttrAccessGroup) is an added field to all these primary keys.
I was hitting a bug the other day (on iOS 7.1) that is related to this question. I was using SecItemCopyMatching to read a kSecClassGenericPassword item and it kept returning errSecItemNotFound (-25300) even though kSecAttrAccessGroup, kSecAttrAccount and kSecAttrService were all matching the item in the keychain.
Eventually I figured out that kSecAttrAccessible didn't match. The value in the keychain held pdmn = dk (kSecAttrAccessibleAlways), but I was using kSecAttrAccessibleWhenUnlocked.
Of course this value is not needed in the first place for SecItemCopyMatching, but the OSStatus was not errSecParam nor errSecBadReq but just errSecItemNotFound (-25300) which made it a bit tricky to find.
For SecItemUpdate I have experienced the same issue but in this method even using the same kSecAttrAccessible in the query parameter didn't work. Only completely removing this attribute fixed it.
I hope this comment will save few precious debugging moments for some of you.
Answer given by #Tammo Freese seems to be correct (but not mentioning all primary keys). I was searching for some proof in the documentation. Finally found:
Apple Documentation mentioning primary keys for each class of secret (quote below):
The system considers an item to be a duplicate for a given keychain when that keychain already has an item of the same class with the same set of composite primary keys. Each class of keychain item has a different set of primary keys, although a few attributes are used in common across all classes. In particular, where applicable, kSecAttrSynchronizable and kSecAttrAccessGroup are part of the set of primary keys. The additional per-class primary keys are listed below:
For generic passwords, the primary keys include kSecAttrAccount and
kSecAttrService.
For internet passwords, the primary keys include kSecAttrAccount,
kSecAttrSecurityDomain, kSecAttrServer, kSecAttrProtocol,
kSecAttrAuthenticationType, kSecAttrPort, and kSecAttrPath.
For certificates, the primary keys include kSecAttrCertificateType,
kSecAttrIssuer, and kSecAttrSerialNumber.
For key items, the primary keys include kSecAttrKeyClass,
kSecAttrKeyType, kSecAttrApplicationLabel, kSecAttrApplicationTag,
kSecAttrKeySizeInBits, and kSecAttrEffectiveKeySize.
For identity items, which are a certificate and a private key bundled
together, the primary keys are the same as for a certificate. Because
a private key may be certified more than once, the uniqueness of the
certificate determines that of the identity.
Here is another piece of useful information about uniqueness of a keychain item, found in the "Ensure Searchability" section of this Apple docs page.
To be able to find the item later, you’re going to use your knowledge of its attributes. In this example, the server and the account are the item’s distinguishing characteristics. For constant attributes (here, the server), use the same value during lookup. In contrast, the account attribute is dynamic, because it holds a value provided by the user at runtime. As long as your app never adds similar items with varying attributes (such as passwords for different accounts on the same server), you can omit these dynamic attributes as search parameters and instead retrieve them along with the item. As a result, when you look up the password, you also get the corresponding username.
If your app does add items with varying dynamic attributes, you’ll need a way to choose among them during retrieval. One option is to record information about the items in another way. For example, if you keep records of users in a Core Data model, you store the username there after using keychain services to store the password field. Later, you use the user name pulled from your data model to condition the search for the password.
In other cases, it may make sense to further characterize the item by adding more attributes. For example, you might include the kSecAttrLabel attribute in the original add query, providing a string that marks the item for the particular purpose. Then you’ll be able to use this attribute to narrow your search later.
Item of class kSecClassInternetPassword was used in the example, but there is a note that says:
Keychain services also offers the related kSecClassGenericPassword item class. Generic passwords are similar in most respects to Internet passwords, but they lack certain attributes specific to remote access (for example, they don’t have a kSecAttrServer attribute). When you don’t need these extra attributes, use a generic password instead.