ecfcf5f52a4bd4fe7568a49beeee08ace95acbaf
[gnuk/gnuk.git] / tests / card_test_keygen.py
1 """
2 card_test_keygen.py - test key generation
3
4 Copyright (C) 2018, 2019  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 class Test_Card_Keygen(object):
28     def test_keygen_1(self, card):
29         pk = card.cmd_genkey(1)
30         fpr_date = rsa_keys.calc_fpr(pk[0], pk[1])
31         r = card.cmd_put_data(0x00, 0xc7, fpr_date[0])
32         if r:
33             r = card.cmd_put_data(0x00, 0xce, fpr_date[1])
34         assert r
35
36     def test_keygen_2(self, card):
37         pk = card.cmd_genkey(2)
38         fpr_date = rsa_keys.calc_fpr(pk[0], pk[1])
39         r = card.cmd_put_data(0x00, 0xc8, fpr_date[0])
40         if r:
41             r = card.cmd_put_data(0x00, 0xcf, fpr_date[1])
42         assert r
43
44     def test_keygen_3(self, card):
45         pk = card.cmd_genkey(3)
46         fpr_date = rsa_keys.calc_fpr(pk[0], pk[1])
47         r = card.cmd_put_data(0x00, 0xc9, fpr_date[0])
48         if r:
49             r = card.cmd_put_data(0x00, 0xd0, fpr_date[1])
50         assert r
51
52     def test_verify_pw1(self, card):
53         v = card.cmd_verify(1, FACTORY_PASSPHRASE_PW1)
54         assert v
55
56     def test_signature_sigkey(self, card):
57         msg = b"Sign me please"
58         pk = card.cmd_get_public_key(1)
59         pk_info = (pk[9:9+256], pk[9+256+2:9+256+2+3])
60         digest = rsa_keys.compute_digestinfo(msg)
61         sig = int(hexlify(card.cmd_pso(0x9e, 0x9a, digest)),16)
62         r = rsa_keys.verify_signature(pk_info, digest, sig)
63         assert r
64
65     def test_verify_pw1_2(self, card):
66         v = card.cmd_verify(2, FACTORY_PASSPHRASE_PW1)
67         assert v
68
69     def test_decryption(self, card):
70         msg = b"encrypt me please"
71         pk = card.cmd_get_public_key(2)
72         pk_info = (pk[9:9+256], pk[9+256+2:9+256+2+3])
73         ciphertext = rsa_keys.encrypt_with_pubkey(pk_info, msg)
74         r = card.cmd_pso(0x80, 0x86, ciphertext)
75         assert r == msg
76
77     def test_signature_authkey(self, card):
78         msg = b"Sign me please to authenticate"
79         pk = card.cmd_get_public_key(3)
80         pk_info = (pk[9:9+256], pk[9+256+2:9+256+2+3])
81         digest = rsa_keys.compute_digestinfo(msg)
82         sig = int(hexlify(card.cmd_internal_authenticate(digest)),16)
83         r = rsa_keys.verify_signature(pk_info, digest, sig)
84         assert r