aboutsummaryrefslogblamecommitdiff
path: root/utils/last_gasp_default_pin
blob: 50d822fa8f33be2eb300447702188f9e852eb730 (plain) (tree)























































                                                                                               
                                    
                                                       


                                                 
                                                                            











                                                      
                                                 














                                                    
#!/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 = 10000,
                    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))
427' href='#n427'>427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715