/* Generated automatically, do not edit. */ #define M_2048_INIT \ {0x002291d8, 0xcdc31041, 0x1e7ec273, 0x78a661c9, \ 0x35187c07, 0xe4d5636e, 0x9bc3c400, 0xb27244b8, \ 0xcd3a97f1, 0x1ae65107, 0x0506a68a, 0x02f0e161, \ 0xaf37f86c, 0xb9078738, 0xc370f07e, 0x8d3b583b, \ 0xad38c275, 0xf34aed05, 0x6ad6ea8e, 0xeca4192f, \ 0xa1feb9dc, 0x4b1ebe55, 0xe5b8f9b6, 0x80eff76c, \ 0x81d4e9ab, 0x304d4896, 0xf9e17fd8, 0xf0816496, \ 0xda087a3e, 0xbecc676a, 0xaa2c5d8c, 0xe1b3c6ac, \ 0xbc5f1670, 0xa9821bc7, 0x2985d764, 0x5e7dbb07, \ 0x780b4eb4, 0xd9fb9d97, 0x9464a52b, 0x2b803afb, \ 0x03c5338a, 0xebdc8c3b, 0x678358f3, 0xd8935a75, \ 0xe844a88c, 0x9bf5ba01, 0x62c8dbd2, 0xf4e2f0bd, \ 0x83cf2184, 0xc78f346d, 0xf30e7bde, 0x5d918d33, \ 0xf081697c, 0xd05b6a58, 0x00898a9f, 0xc99c5475, \ 0x9907cd3a, 0xa22d8c95, 0x2edc17cc, 0x8dccd9d1, \ 0xee4108d7, 0xf1ac1215, 0xde047303, 0xc1c1473f} #define N_2048_INIT \ {0xd82cbb4a, 0x7b6b057d, 0x93bc6e12, 0x1615956b, \ 0xd0b7aba7, 0x9e5ba014, 0x1cc0fc63, 0x48dd42ed, \ 0x3dc4904e, 0x66c6605f, 0x751bfced, 0x135c6703, \ 0x46c37da7, 0xc39440aa, 0xb20b6995, 0x802a2cb1, \ 0xf531a4b4, 0xe702a248, 0x5c9109d3, 0x9c2e41d7, \ 0x28832979, 0x04c89899, 0x691c4bcc, 0xf10f44a3, \ 0x5e35a701, 0x75d2ff00, 0x835c1945, 0x2d3ce223, \ 0xb894d90e, 0xebacebcc, 0x08951d2a, 0xa08d18ce, \ 0x24147c14, 0xfc985600, 0xf2f00a86, 0x3df554a7, \ 0xd0f76f0d, 0x24a27f30, 0x70ca2377, 0xc97292aa, \ 0xd80a02b1, 0xd3b098d2, 0xbb8e6755, 0x1bf2533b, \ 0xcda2b067, 0x8d0dad30, 0xf7090c5c, 0x196254e1, \ 0x955a8527, 0xbd29c63b, 0x10020dd5, 0x97260509, \ 0x0a2dd5e6, 0x4aed6113, 0x4f636d78, 0xe3c192d8, \ 0xe1280048, 0x3a0eb61e, 0x6a830b2d, 0xfd4e69da, \ 0xc850335d, 0x04886009, 0x0b1534d8, 0xf2d026d3} #define N_FACTOR_2048_INIT \ {0x814227f1, 0x17ae17f4, 0x26e65c79, 0x1abb99dc, \ 0x93491025, 0x8202e00c, 0x76ecd5e2, 0xd82cdc62, \ 0x59884542, 0x316a0981, 0x9270495d, 0xfdfdad32, \ 0x5dd0428e, 0x669cfd54, 0x1caf2c67, 0x85810894, \ 0x7ed108cb, 0x41352317, 0x0a4cb7b1, 0x0c025a58, \ 0x862995aa, 0xcbcb6ceb, 0x44453aed, 0x6c8aea02, \ 0x204b54b2, 0x3b2d5edb, 0x6a3aa436, 0x69a11f19, \ 0xbdcf892e, 0x20b839aa, 0x357c3932, 0x6c743160, \ 0xbdc410e1, 0x3eb178cb, 0x6b1b30a1, 0x87d4fa2a, \ 0x10ddb3b8, 0xb91b183e, 0x549ff466, 0x7f179e76, \ 0x0525602c, 0xbd93586f, 0x28e4c46b, 0x6729c6fa, \ 0x58aedc1e, 0x399382e4, 0x141c3490, 0x141d5169, \ 0xe93fd2b1, 0x1f4a30b7, 0x2377dba3, 0xde705b6a, \ 0x432efe3d, 0x62509e40, 0x718984a3, 0xdb252caa, \ 0x35f135f6, 0xd9bcdf56, 0x48fb1cb9, 0x9509d41a, \ 0xffa19a1c, 0xb4529d64, 0xfee48017, 0x20b51222} #define N_COEFF_2048_INIT \ {0x0000230e, 0x4f7c9cc3, 0xa30db1d3, 0x96866233, \ 0xb2c2c944, 0xd21e7b6b, 0x26270e66, 0x34e92a94, \ 0x6b048552, 0xb03af0f0, 0xbad6a264, 0xddd9190e, \ 0x2484414c, 0x18c5957f, 0x0666b1f1, 0x465142b8, \ 0x9bfe0bc7, 0x6aeeccde, 0x89c96f7a, 0x31f0ff9a, \ 0x63037214, 0x96edc3ea, 0xbd82295f, 0x9f6c0cb5, \ 0xc1348f9d, 0x16fd36c3, 0x70192867, 0x50ba4eeb, \ 0xc11124a8, 0xa7195773, 0xb72605f4, 0x28958340, \ 0xd0db455d, 0x3cd40c85, 0xff2a7256, 0xc96f0706, \ 0xe9c7b93f, 0x57836dbb, 0x1a2893cc, 0xf23a49ac, \ 0x6296dc27, 0xace1a4ee, 0x5ed0c317, 0x35c59908, \ 0x44753f60, 0x3d8f25c8, 0xcb62c5d7, 0xe95a72c9, \ 0x566a98f4, 0x81fa5df5, 0xc5742a96, 0x7c0fe404, \ 0x976646fb, 0xf6ffe021, 0x3add83ac, 0xce8c7a02, \ 0x27ced42c, 0x0e099d02, 0x583c94a8, 0x59ce866c, \ 0x45000e17, 0x700d8af5, 0xbabd9dd6, 0x9b6d6280, \ 0x05a4dea5} #define X_2048_INIT \ {0x00f4dcf2, 0xd90e1715, 0x5cd52bbc, 0xcfabda4e, \ 0x409b369b, 0x0994ae28, 0xff6ea364, 0xcdb9dcfe, \ 0x82f35f8b, 0xef718044, 0xe609de07, 0x5d77ee51, \ 0xe8616ce4, 0xe2862a8f, 0x2d3c3b06, 0x2d532c22, \ 0x82825cff, 0x83ac8f2e, 0xfee472cb, 0x6abc86e8, \ 0xe8c35dca, 0x975a5cfb, 0xdbf67229, 0xf4c166b7, \ 0xbd76a787, 0x3f7d47ec, 0x7f8083d4, 0xcb5aa9e2, \ 0x74e6e776, 0x5991b9eb, 0x8eb9747c, 0xa838f053, \ 0xd0b3d52a, 0xe0e89d44, 0xc5e97a4f, 0x4df5ccb4, \ 0xd4818f84, 0x81a69d96, 0x684fbb35, 0x7d835def, \ 0xaf9fe113, 0xc8d257b9, 0x02e8d030, 0xffbe1b0f, \ 0x93a70c45, 0x973aaee0, 0xea1bc185, 0x22da443e, \ 0xd335f1e1, 0x0f6ce5b7, 0xc2080e5c, 0x5c2c3fac, \ 0x06151df4, 0x11060aba, 0xeb055f41, 0x20d0ef28, \ 0xbc2f85b1, 0x0062960b, 0xcbfd3f26, 0xf8090158, \ 0xf09da0be, 0xbf1c4956, 0x7d074e72, 0x8dc49abd} #define Y_2048_INIT \ {0x0976f869, 0x79d2709f, 0x2bd01d0b, 0x42d1a017, \ 0x77584369, 0x63a84f5b, 0xe5a424fc, 0xf2496f1c, \ 0x03e1b01c, 0xe17822f3, 0x8489af36, 0x01b85c64, \ 0xc1d956a2, 0xdb4caf37, 0x7ce1bb31, 0x7d51e611, \ 0xfd9d0fcb, 0x93b23236, 0xce46c707, 0x6e1b794b, \ 0x3034317a, 0x64c24fe7, 0xae18f26b, 0x2b9e6888, \ 0x167e4b9b, 0x79f8b5f8, 0x2916f577, 0x1e47373b, \ 0xaf37ea80, 0x4fe10e7e, 0x12e37006, 0x71193164, \ 0xe0c57fed, 0x2c994650, 0xead7cfe5, 0xdf33ac9d, \ 0x9afa0038, 0x4520d430, 0x6950d11f, 0xa30fa3e3, \ 0xf7e7228d, 0xb04a0047, 0x784ebb6b, 0x17a95bf3, \ 0x222f6eab, 0x536e1add, 0x88063bf2, 0x2c9dc31e, \ 0xa040946d, 0xd20d52a1, 0xe8f0a185, 0x369f9d5f, \ 0x47c6b7ea, 0x3c6cc5bc, 0x479bdab5, 0xf4e0cfe0, \ 0x8fee2667, 0x5b4d6230, 0xb330ef9d, 0x7a0226dd, \ 0x860c71db, 0x00a02a8c, 0x01012bbf, 0xf46afc19} #define P_2048_INIT \ {0xf66e84bb, 0x03961240, 0x504cb7ab, 0x43537a51, \ 0xa5cca453, 0x3e6f6ca3, 0x88229106, 0x808dac6e, \ 0x77d38e83, 0x9e03a391, 0x34851c73, 0xc477d84e, \ 0x317e6eca, 0xc9951868, 0xe9687b04, 0x1767f8da, \ 0x4c9bd43f, 0x03138fec, 0xc09be37a, 0xd8728306, \ 0x37607aa5, 0x33b86dd7, 0xb7cda3f0, 0x8e6eb799, \ 0x536eb211, 0x4397a9cd, 0x979048b9, 0x3154815f, \ 0xa71652f8, 0xf76820d1, 0x8a67872d, 0x42d2dbff} #define Q_2048_INIT \ {0xe0917700, 0x4d8dcdbf, 0x8193dbe7, 0xf660571a, \ 0xb831f6bc, 0xbcc05683, 0xb2f698ce, 0x71d440f5, \ 0x36183800, 0x6cc7b568, 0xaee8f151, 0x9d05c38c, \ 0xf3a45bd4, 0xb8de68ca, 0x817ef26e, 0xf7196058, \ 0xbd56f7ff, 0x8706478f, 0x2afbb654, 0xd11d76bc, \ 0x995af309, 0xa60f1cd2, 0x100f8306, 0x2020a024, \ 0xab3f63ae, 0xbc087521, 0xd56160cb, 0xa23ba129, \ 0x77adb7de, 0xfa683a0c, 0xdf759183, 0xa88c852d} #define P_FACTOR_2048_INIT \ {0x1c54bc5a, 0xe6b9eff3, 0x564ba141, 0xa632f9fe, \ 0x229e3363, 0x37d517e9, 0xe3a6a808, 0x4060b34f, \ 0x6ed77bce, 0x1528c4a9, 0x25aada03, 0x7f57f409, \ 0xccab79ac, 0xa1c400f5, 0xc7ff33ba, 0x055a978e, \ 0x375ebf5c, 0xe7243913, 0x5c020cc8, 0x207d9afd, \ 0xaf6d61ec, 0xd923676c
#!/usr/bin/env python

"""
Somewhere, the HSM has to have a last-gasp default PIN, even if it's
only the null string, because there has to be **some** way to
initialize the poor thing.  Absent a better plan (feel free to
suggest one!), this last-gasp default is compiled in.

The normal value of this last-gasp PIN is deliberately chosen to be
annoying, so that people will change it, but since the derevation
requires running PBKDF2 and you might want a different default if
you're compiling this for yourself, we provide the script that
generates the default.
"""

# Author: Rob Austein
# Copyright (c) 2016, NORDUnet A/S
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
# - Redistributions of source code must retain the above copyright notice,
#   this list of conditions and the following disclaimer.
#
# - Redistributions in binary form must reproduce the above copyright
#   notice, this list of conditions and the following disclaimer in the
#   documentation and/or other materials provided with the distribution.
#
# - Neither the name of the NORDUnet nor the names of its contributors may
#   be used to endorse or promote products derived from this software
#   without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
# IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
# TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

from argparse                   import ArgumentParser, ArgumentDefaultsHelpFormatter
from os                         import urandom
from Crypto.Protocol.KDF        import PBKDF2
from Crypto.Hash                import SHA256, HMAC

parser = ArgumentParser(description = __doc__, formatter_class = ArgumentDefaultsHelpFormatter)
parser.add_argument("-p", "--pin",
                    default = "YouReallyNeedToChangeThisPINRightNowWeAreNotKidding",
                    help    = "PIN plaintext before PBKDF2 processing")
parser.add_argument("-i", "--iterations",
                    type    = int,
                    default = 100000,
                    help    = "PBKDF2 iteration count")
parser.add_argument("-d", "--derived-key-length",
                    type    = int,
                    default = 64,
                    help    = "length of PBKDF2 output (must match libhal)")
args = parser.parse_args()

def HMAC_SHA256(pin, salt):
    return HMAC.new(pin, salt, SHA256).digest()

def hexify(value):
    return ", ".join("0x%02x" % ord(v) for v in value)

salt = urandom(16)

pin  = PBKDF2(password = args.pin,
              salt     = salt,
              dkLen    = args.derived_key_length,
              count    = args.iterations,
              prf      = HMAC_SHA256)

print '''\
/*
 * Automatically generated by a script, do not edit.
 */

static const hal_ks_pin_t hal_last_gasp_pin = {{
  {iterations},
  {{{pin}}},
  {{{salt}}}
}};'''.format(iterations = args.iterations,
              pin        = hexify(pin),
              salt       = hexify(salt))