More tests (3).
[gnuk/gnuk.git] / tests / test_005_keygen.py
1 """
2 test_005_keygen.py - test key generation
3
4 Copyright (C) 2018  g10 Code GmbH
5 Author: NIIBE Yutaka <gniibe@fsij.org>
6
7 This file is a part of Gnuk, a GnuPG USB Token implementation.
8
9 Gnuk is free software: you can redistribute it and/or modify it
10 under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 Gnuk is distributed in the hope that it will be useful, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
17 License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 """
22
23 from binascii import hexlify
24 import rsa_keys
25 from card_const import *
26
27 def test_keygen_1(card):
28     pk = card.cmd_genkey(1)
29     fpr_date = rsa_keys.calc_fpr(pk[0], pk[1])
30     r = card.cmd_put_data(0x00, 0xc7, fpr_date[0])
31     if r:
32         r = card.cmd_put_data(0x00, 0xce, fpr_date[1])
33     assert r
34
35 def test_keygen_2(card):
36     pk = card.cmd_genkey(2)
37     fpr_date = rsa_keys.calc_fpr(pk[0], pk[1])
38     r = card.cmd_put_data(0x00, 0xc8, fpr_date[0])
39     if r:
40         r = card.cmd_put_data(0x00, 0xcf, fpr_date[1])
41     assert r
42
43 def test_keygen_3(card):
44     pk = card.cmd_genkey(3)
45     fpr_date = rsa_keys.calc_fpr(pk[0], pk[1])
46     r = card.cmd_put_data(0x00, 0xc9, fpr_date[0])
47     if r:
48         r = card.cmd_put_data(0x00, 0xd0, fpr_date[1])
49     assert r
50
51 def test_verify_pw1(card):
52     v = card.cmd_verify(1, FACTORY_PASSPHRASE_PW1)
53     assert v
54
55 def test_signature_sigkey(card):
56     msg = b"Sign me please"
57     pk = card.cmd_get_public_key(1)
58     pk_info = (pk[9:9+256], pk[9+256+2:9+256+2+3])
59     digest = rsa_keys.compute_digestinfo(msg)
60     sig = int(hexlify(card.cmd_pso(0x9e, 0x9a, digest)),16)
61     r = rsa_keys.verify_signature(pk_info, digest, sig)
62     assert r
63
64 def test_verify_pw1_2(card):
65     v = card.cmd_verify(2, FACTORY_PASSPHRASE_PW1)
66     assert v
67
68 def test_decryption(card):
69     msg = b"encrypt me please"
70     pk = card.cmd_get_public_key(2)
71     pk_info = (pk[9:9+256], pk[9+256+2:9+256+2+3])
72     ciphertext = rsa_keys.encrypt_with_pubkey(pk_info, msg)
73     r = card.cmd_pso(0x80, 0x86, ciphertext)
74     assert r == msg
75
76 def test_signature_authkey(card):
77     msg = b"Sign me please to authenticate"
78     pk = card.cmd_get_public_key(3)
79     pk_info = (pk[9:9+256], pk[9+256+2:9+256+2+3])
80     digest = rsa_keys.compute_digestinfo(msg)
81     sig = int(hexlify(card.cmd_internal_authenticate(digest)),16)
82     r = rsa_keys.verify_signature(pk_info, digest, sig)
83     assert r