09. Signing the transaction log
March 13, 2019 ยท View on GitHub
Solution to problem 08
// sign.js
var sodium = require('sodium-native')
var publicKey = Buffer.alloc(sodium.crypto_sign_PUBLICKEYBYTES)
var secretKey = Buffer.alloc(sodium.crypto_sign_SECRETKEYBYTES)
sodium.crypto_sign_keypair(publicKey, secretKey)
var message = Buffer.from('Hello world!')
var signature = Buffer.alloc(sodium.crypto_sign_BYTES)
sodium.crypto_sign_detached(signature, message, secretKey)
console.log('Public key: ' + publicKey.toString('hex'))
console.log('Message: ' + message.toString())
console.log('Signature: ' + signature.toString('hex'))
// verify.js
Now that we have a basic understanding on how digital signatures work we can start securing the transaction log with this new primitive.
Remember, since we already have a hash chain of all transactions the only thing missing is making sure we, the bank, are the only ones that are generating the hash chain.
Problem
Using the signature APIs you learned about in the previous exercise, extend the bank to:
- Check if a existing key-pair is stored on disk, if so load it.
- If not, generate a new key-pair and store it.
- When you generate a new hash for a transaction, sign the hash using the secret key
and store the signature as a new property
signature, next to thehashandvalueproperties. - When loading the transaction log, extend verification to validate the signatures of the hashes in addition to the hashes themselves.
You might be thinking that storing the key-pair right next to the transaction log is not very safe. This is much more of an operational problem (which also has technical mitigations) but for the purpose of our workshop it will suffice. In a real bank, you might use a Hardware Secure Module (HSM), which is a logical separate computing unit in the server, from which the secret key never leaves. AWS has a CloudHSM Classic product which gives you a dedicated machine with it's own HSM. However, the upfront cost is $5,000 USD and there is a $1,500 USD monthly maintenance cost.
Testing
Make sure your bank works the same way as before. Then stop the bank and try tampering with the log. The bank should reject the bad transaction log, even if the hashes are correct.