mirror of
https://github.com/polhenarejos/pico-hsm.git
synced 2026-01-18 09:48:06 +00:00
Compare commits
1002 commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
16d4d0d26e | ||
|
|
380ff7afa4 | ||
|
|
60dafec2e8 | ||
|
|
3207fe3451 | ||
|
|
6914be4fea | ||
|
|
7d551f6fea | ||
|
|
d3a7ff425a | ||
|
|
97e7303505 | ||
|
|
e41f2ba712 | ||
|
|
710eb70af7 | ||
|
|
9dbd764c8c | ||
|
|
feec958d10 | ||
|
|
9720bcfd4b | ||
|
|
4bb81f5b25 | ||
|
|
c9926a71d1 | ||
|
|
10c25b6a3a | ||
|
|
629f14ab0d | ||
|
|
4d6f6e4635 | ||
|
|
8e35104695 | ||
|
|
82f4b2201c | ||
|
|
8978456524 | ||
|
|
a0faf5308e | ||
|
|
f4df9766b6 | ||
|
|
a1d64d2523 | ||
|
|
82c6badc48 | ||
|
|
2243629d58 | ||
|
|
b5b5ccd53c | ||
|
|
0df97c881f | ||
|
|
3c0706f7b2 | ||
|
|
0d1d77b3f6 | ||
|
|
25301f14e1 | ||
|
|
256e61d813 | ||
|
|
1fe771f074 | ||
|
|
0588dde490 | ||
|
|
1d6cc6ff89 | ||
|
|
8fde25eff2 | ||
|
|
634eac50b1 | ||
|
|
c4a1cf32e5 | ||
|
|
966b12ffad | ||
|
|
de33124d6b | ||
|
|
ae61150f5c | ||
|
|
605819f0a3 | ||
|
|
c9dc70b095 | ||
|
|
7060e4df7c | ||
|
|
28c99a21bd | ||
|
|
dba614ed36 | ||
|
|
2471c747d6 | ||
|
|
c7006b6319 | ||
|
|
5f5e010a19 | ||
|
|
ad5a503d36 | ||
|
|
5e4f2286e6 | ||
|
|
f0f8e4382a | ||
|
|
a0f6abf6a6 | ||
|
|
cbf9bb9281 | ||
|
|
bd0796b217 | ||
|
|
defa18a5ec | ||
|
|
bf06081b9c | ||
|
|
f1a8d8bc91 | ||
|
|
d6f15f8384 | ||
|
|
dce4e304bc | ||
|
|
f97aaadd17 | ||
|
|
611b762a1f | ||
|
|
7c1ef56799 | ||
|
|
4b59bd6481 | ||
|
|
9b1dceb8da | ||
|
|
c4bc346d98 | ||
|
|
3e1b29f72a | ||
|
|
bb27c2324b | ||
|
|
8c0a7c6681 | ||
|
|
2db265c9c8 | ||
|
|
c58823f9ef | ||
|
|
21b6a7782a | ||
|
|
26f9e0dd54 | ||
|
|
4053b22f2e | ||
|
|
ef71ec6a29 | ||
|
|
72f2e0a58b | ||
|
|
17ca65a41b | ||
|
|
92fe26c1e1 | ||
|
|
5c2dce9e52 | ||
|
|
1cbcfce23f | ||
|
|
1f3cecea8b | ||
|
|
ac8d05b9e9 | ||
|
|
e00e620264 | ||
|
|
64d8dafa2b | ||
|
|
0f3bb8242c | ||
|
|
747e5fbe86 | ||
|
|
8eb4669c0f | ||
|
|
4c636e0ce5 | ||
|
|
297f2e6228 | ||
|
|
12a26f8506 | ||
|
|
45a96f8742 | ||
|
|
ca2e04af53 | ||
|
|
f0a9d03ca8 | ||
|
|
2502f99aff | ||
|
|
71636f4d66 | ||
|
|
310bcb8092 | ||
|
|
6c5735fc4b | ||
|
|
3430655897 | ||
|
|
3d9e806409 | ||
|
|
ff8b343df4 | ||
|
|
0c6b0d295f | ||
|
|
c6b03e54ca | ||
|
|
991f5fc960 | ||
|
|
6d516b1b78 | ||
|
|
2a3ac550ec | ||
|
|
73232b6de4 | ||
|
|
d56b540324 | ||
|
|
74b635fa3c | ||
|
|
07d9e225d2 | ||
|
|
3d960b931a | ||
|
|
ff7ef56cda | ||
|
|
320455815f | ||
|
|
859dec7e4a | ||
|
|
053ccf4a89 | ||
|
|
f88aad1e2c | ||
|
|
866aac8fe3 | ||
|
|
a242a28394 | ||
|
|
c33ed9b968 | ||
|
|
65a4474dec | ||
|
|
e243def565 | ||
|
|
78bc34827e | ||
|
|
a69cb8a95a | ||
|
|
bb45c9b3a8 | ||
|
|
1ea0a91ba8 | ||
|
|
6f572954d5 | ||
|
|
99a9803328 | ||
|
|
5535f3ec10 | ||
|
|
f992a04142 | ||
|
|
d858a1e1d5 | ||
|
|
b7c6ca58d0 | ||
|
|
2f3d70fcac | ||
|
|
91c641033a | ||
|
|
f3bf3cd8f4 | ||
|
|
e4736cc448 | ||
|
|
bfa8891c8d | ||
|
|
618fdb4c1a | ||
|
|
9457433844 | ||
|
|
c239b4dd7c | ||
|
|
96359ebe03 | ||
|
|
f301b2a1b1 | ||
|
|
4ec1d4d891 | ||
|
|
f1410bbf04 | ||
|
|
9b9ea7cae5 | ||
|
|
f5b89aed69 | ||
|
|
f74a374c64 | ||
|
|
3c6684cdab | ||
|
|
20ef94c301 | ||
|
|
68360978d6 | ||
|
|
1933498a33 | ||
|
|
0ce89ae968 | ||
|
|
9fa3758dad | ||
|
|
d1ee43baab | ||
|
|
2856ec6917 | ||
|
|
ccec83dfb1 | ||
|
|
9de1b4ca5d | ||
|
|
cbd9a5d296 | ||
|
|
eaf3d051e3 | ||
|
|
8e3801e9a2 | ||
|
|
5388c11182 | ||
|
|
2b77519a3b | ||
|
|
812a737ff5 | ||
|
|
6163b870ed | ||
|
|
08d4dc58aa | ||
|
|
0193e55f7b | ||
|
|
9dc3ea3232 | ||
|
|
48d92f53a0 | ||
|
|
3b083f9bd7 | ||
|
|
9cd7bc7b3e | ||
|
|
4bea47faca | ||
|
|
954eb1d890 | ||
|
|
f7451f56ed | ||
|
|
1a8c8440cf | ||
|
|
9f1e5d0abd | ||
|
|
4b314470b4 | ||
|
|
d0461f81f4 | ||
|
|
4454c633e4 | ||
|
|
15664da61d | ||
|
|
1a2bff33a8 | ||
|
|
cf44794bb6 | ||
|
|
33a6a70457 | ||
|
|
c3ddfe9689 | ||
|
|
72eb5a2a69 | ||
|
|
95f3a464b1 | ||
|
|
bb37a3ddb8 | ||
|
|
6da49336c9 | ||
|
|
fc69f5e1b1 | ||
|
|
3602577b6e | ||
|
|
c6a72e4ffb | ||
|
|
c889cef488 | ||
|
|
d78e925d49 | ||
|
|
bb8fcbcd3f | ||
|
|
c433e131eb | ||
|
|
11d52495d7 | ||
|
|
f450279370 | ||
|
|
b571b0b945 | ||
|
|
5ae63959d5 | ||
|
|
8e8bd32b44 | ||
|
|
68d2909653 | ||
|
|
e64eb2157c | ||
|
|
af23b401cb | ||
|
|
1da29f22c1 | ||
|
|
f458750c91 | ||
|
|
c5143df212 | ||
|
|
60f471cdd7 | ||
|
|
5c3c45a189 | ||
|
|
3b92ce0597 | ||
|
|
56ab770a26 | ||
|
|
09f7ed6640 | ||
|
|
aaf1bc2bbd | ||
|
|
f7e8359835 | ||
|
|
1c45295d28 | ||
|
|
af099cd416 | ||
|
|
36d1011471 | ||
|
|
c5714a91ab | ||
|
|
b8002a1a08 | ||
|
|
604fc1aa45 | ||
|
|
39a5af8649 | ||
|
|
28c63a500c | ||
|
|
ef03ce4020 | ||
|
|
9335b088cf | ||
|
|
9cc934282c | ||
|
|
2ad67e5e17 | ||
|
|
7493f9eb3e | ||
|
|
d44b780970 | ||
|
|
e7080d6f82 | ||
|
|
9258c9ff70 | ||
|
|
ad18577e98 | ||
|
|
3cae928de8 | ||
|
|
b429616895 | ||
|
|
0c2e728c35 | ||
|
|
5630043a4d | ||
|
|
c1a47ed023 | ||
|
|
bf2f961b85 | ||
|
|
0811b8022e | ||
|
|
fd12758551 | ||
|
|
c65f921188 | ||
|
|
55cf9292d2 | ||
|
|
db5c7adef4 | ||
|
|
d3751e3918 | ||
|
|
d708158fea | ||
|
|
dc979194fa | ||
|
|
237e687c3b | ||
|
|
ed89175edc | ||
|
|
9a7be98e6e | ||
|
|
c4a08aff0f | ||
|
|
223fc117dd | ||
|
|
d1fb4d0c65 | ||
|
|
47acef71c8 | ||
|
|
bb641ee134 | ||
|
|
99c777c780 | ||
|
|
79f76a176d | ||
|
|
dd68554782 | ||
|
|
f20fdc9bda | ||
|
|
e7d4a5dd3d | ||
|
|
b4671c2ecb | ||
|
|
50a29519bd | ||
|
|
54cdbfc22c | ||
|
|
4a40cebe1f | ||
|
|
87ffd21543 | ||
|
|
7cb0bbf982 | ||
|
|
07079b6ba1 | ||
|
|
294ca81d7d | ||
|
|
218660e694 | ||
|
|
de89d61359 | ||
|
|
8e8192362c | ||
|
|
25c93c279f | ||
|
|
001d076fdf | ||
|
|
35d5d5e94e | ||
|
|
b034303193 | ||
|
|
623cf10dba | ||
|
|
fdc41e5856 | ||
|
|
5d0dc210da | ||
|
|
a674410826 | ||
|
|
920d22212a | ||
|
|
aeeb540a2f | ||
|
|
be071b0bc1 | ||
|
|
8bbbdb4dd8 | ||
|
|
45b633cc9d | ||
|
|
842919a26b | ||
|
|
3dbcefea85 | ||
|
|
60038f9345 | ||
|
|
d8c7fb0856 | ||
|
|
d6456b4dde | ||
|
|
ac781c1db6 | ||
|
|
c3b66773e8 | ||
|
|
3ca23b932c | ||
|
|
9fad920c3b | ||
|
|
38bef5b43f | ||
|
|
2034e436d1 | ||
|
|
215221b30e | ||
|
|
de4d95beb8 | ||
|
|
963b6f4f87 | ||
|
|
fa9b244c42 | ||
|
|
2b92d89ab7 | ||
|
|
a1d7733b95 | ||
|
|
39f1041dbb | ||
|
|
2e4fc568db | ||
|
|
4f4e6e09a2 | ||
|
|
08c0aaee6f | ||
|
|
de98e79c99 | ||
|
|
481cd5fd69 | ||
|
|
d9a8826a32 | ||
|
|
d82affa880 | ||
|
|
ab31a6615c | ||
|
|
677cceb130 | ||
|
|
29967c067a | ||
|
|
2974aa234a | ||
|
|
d90b296237 | ||
|
|
e98b26fee5 | ||
|
|
2086a68c53 | ||
|
|
74afa07512 | ||
|
|
e96e1d0097 | ||
|
|
4d47f0224e | ||
|
|
58692b2711 | ||
|
|
a5ab1cabc5 | ||
|
|
1c7cdc8564 | ||
|
|
d74b3418bc | ||
|
|
fb5be153ed | ||
|
|
0ac71f2fff | ||
|
|
6ec5235cc3 | ||
|
|
ff74d6306e | ||
|
|
ba562da00e | ||
|
|
98e9b72b42 | ||
|
|
1b0d23cf24 | ||
|
|
3ccff7881f | ||
|
|
a3bf2e9e14 | ||
|
|
88ff27f354 | ||
|
|
ed2925cfb6 | ||
|
|
cc19f8f061 | ||
|
|
b6831a4650 | ||
|
|
652a0f0d21 | ||
|
|
9dbf4b3172 | ||
|
|
76522829ef | ||
|
|
6b1eeb4004 | ||
|
|
2693ab4926 | ||
|
|
5d21e39aa6 | ||
|
|
1bf0d6337b | ||
|
|
1950b03d35 | ||
|
|
4be258f4c7 | ||
|
|
eddb1baf7b | ||
|
|
11bb00e186 | ||
|
|
aaed6bd7b6 | ||
|
|
2853b38b08 | ||
|
|
654cb1e4e0 | ||
|
|
185d19504f | ||
|
|
dbe0ef19de | ||
|
|
c3b57b229f | ||
|
|
43c46e9112 | ||
|
|
d5af9160c1 | ||
|
|
e27c8d4ff6 | ||
|
|
2ecfff0ebb | ||
|
|
3057aba041 | ||
|
|
011a594fe4 | ||
|
|
448d61dd2d | ||
|
|
fa821c43c8 | ||
|
|
9db3e78d32 | ||
|
|
b74c4070d1 | ||
|
|
a7770a7e57 | ||
|
|
78cbe59e64 | ||
|
|
42a9434c50 | ||
|
|
11a3472941 | ||
|
|
2f177eb8df | ||
|
|
c97680772d | ||
|
|
345ae2be69 | ||
|
|
ba036f4c0f | ||
|
|
4493afa84d | ||
|
|
d118b787a2 | ||
|
|
205356b848 | ||
|
|
c435764978 | ||
|
|
2e633abb2c | ||
|
|
3514c7ad82 | ||
|
|
4bba3c6f64 | ||
|
|
8321996a14 | ||
|
|
c3af303468 | ||
|
|
c44d7db4e1 | ||
|
|
cb1bba4f13 | ||
|
|
48301a5d71 | ||
|
|
1105a1263c | ||
|
|
37421d9aad | ||
|
|
d906df2809 | ||
|
|
08e7d74a81 | ||
|
|
433e509c94 | ||
|
|
9f08d612ae | ||
|
|
173ca7678d | ||
|
|
3d7f714936 | ||
|
|
f4a537f156 | ||
|
|
f09654ccf4 | ||
|
|
7506ccd263 | ||
|
|
ee442ac084 | ||
|
|
b94810d31d | ||
|
|
2c4be41ecf | ||
|
|
447d223de4 | ||
|
|
4f9f7f14fe | ||
|
|
6699913eff | ||
|
|
b0057bc3fe | ||
|
|
e3a773d145 | ||
|
|
10a47f9177 | ||
|
|
9f49a144ac | ||
|
|
e67a7091ff | ||
|
|
1d3232df36 | ||
|
|
7376817724 | ||
|
|
5e801fbae7 | ||
|
|
e18c413268 | ||
|
|
0e1c82196a | ||
|
|
c6a5272340 | ||
|
|
a6072b4ce8 | ||
|
|
5bdba8e1e8 | ||
|
|
5df4e62f81 | ||
|
|
685c7f25fc | ||
|
|
faef2dc278 | ||
|
|
22d8793683 | ||
|
|
32c8bc4a24 | ||
|
|
b5174d64af | ||
|
|
235bf342b3 | ||
|
|
57188ff877 | ||
|
|
f631e4a5f3 | ||
|
|
f880ee6c93 | ||
|
|
6af4cc7fec | ||
|
|
e3f1f45a9f | ||
|
|
fe315e7326 | ||
|
|
02903bbc65 | ||
|
|
ee3ee21e40 | ||
|
|
c6c00e7e43 | ||
|
|
29544a3f17 | ||
|
|
5135404083 | ||
|
|
55520ce184 | ||
|
|
525b05b7ef | ||
|
|
b61a34d415 | ||
|
|
0eea0fb096 | ||
|
|
82c03ee6c4 | ||
|
|
20dfbcaaed | ||
|
|
a9fb170c58 | ||
|
|
46d85c029e | ||
|
|
1c6f4cfd7f | ||
|
|
526869bc06 | ||
|
|
279df32acc | ||
|
|
c402b884cc | ||
|
|
8316196b42 | ||
|
|
7a6dafd7b6 | ||
|
|
6d3809a792 | ||
|
|
f22f58f983 | ||
|
|
405bf92e18 | ||
|
|
b5b6dcd2e6 | ||
|
|
d637dbec01 | ||
|
|
ebcac9dfdc | ||
|
|
510b148adf | ||
|
|
371ae93fcd | ||
|
|
f5e875a6b7 | ||
|
|
ad3304a384 | ||
|
|
4d647ba3c8 | ||
|
|
008db87fa7 | ||
|
|
1d80b45439 | ||
|
|
17560034ec | ||
|
|
8b0d85fbd4 | ||
|
|
3dcb2e9d70 | ||
|
|
0f12ff1c48 | ||
|
|
2a5fe1cc6d | ||
|
|
5e0f62265d | ||
|
|
0990805fb6 | ||
|
|
18bcf532e7 | ||
|
|
9f0316aedd | ||
|
|
088002863c | ||
|
|
7876f4b32a | ||
|
|
a5e3d4e0e5 | ||
|
|
0c842773d2 | ||
|
|
0aaf339353 | ||
|
|
efba39adc5 | ||
|
|
13755cb4d5 | ||
|
|
11a30863e8 | ||
|
|
a69d06b2d9 | ||
|
|
1c7bc18161 | ||
|
|
86ce01cac2 | ||
|
|
9cb60ba420 | ||
|
|
99f1620e7d | ||
|
|
755570e01a | ||
|
|
4b264597d6 | ||
|
|
67e3d908b3 | ||
|
|
7ae76354ef | ||
|
|
9291c2b4e3 | ||
|
|
657ee18730 | ||
|
|
132054c9b9 | ||
|
|
2a3b9b7474 | ||
|
|
4e73723747 | ||
|
|
09724d95db | ||
|
|
c1bfb597bc | ||
|
|
fd53b88397 | ||
|
|
11e5338736 | ||
|
|
3660120b11 | ||
|
|
75ca1b57d9 | ||
|
|
7af67713e7 | ||
|
|
63c465138b | ||
|
|
4d569df108 | ||
|
|
bee6a7bb92 | ||
|
|
e4583eb9c0 | ||
|
|
1c8582365d | ||
|
|
daf71678c5 | ||
|
|
26f0775772 | ||
|
|
ed674b379c | ||
|
|
6aa8d37d07 | ||
|
|
963456051e | ||
|
|
1e90699a3f | ||
|
|
f8110a9d75 | ||
|
|
d81b21695f | ||
|
|
51e9e6722e | ||
|
|
dba4630895 | ||
|
|
ee73c6e781 | ||
|
|
2f63966c60 | ||
|
|
e624f9ff72 | ||
|
|
fb4ff9424e | ||
|
|
63b245b858 | ||
|
|
cd525d91a3 | ||
|
|
8fd3b4d858 | ||
|
|
3c160f69c0 | ||
|
|
2c9fa8b19f | ||
|
|
ccfe7ca8a8 | ||
|
|
d2d038f14d | ||
|
|
1a05d7b51f | ||
|
|
fa60ed5049 | ||
|
|
35aec06391 | ||
|
|
61359c7ebd | ||
|
|
20c01eb08d | ||
|
|
0b71bf693d | ||
|
|
a1f478239d | ||
|
|
420e55901c | ||
|
|
414cab78e3 | ||
|
|
36e5282d19 | ||
|
|
367651fe75 | ||
|
|
7b62ca5327 | ||
|
|
9be176e523 | ||
|
|
b24e109a3c | ||
|
|
f61d0dd677 | ||
|
|
215fdca9f8 | ||
|
|
9279773073 | ||
|
|
d5b718ca58 | ||
|
|
cd6e280f4f | ||
|
|
e7495d11f2 | ||
|
|
daaa5bf402 | ||
|
|
cab72f200a | ||
|
|
380d47faa1 | ||
|
|
962d1c6916 | ||
|
|
d81d51d6fc | ||
|
|
319000df1a | ||
|
|
5508e531a0 | ||
|
|
1e846600dd | ||
|
|
43a49d2a14 | ||
|
|
3b9c2d159b | ||
|
|
14c7852143 | ||
|
|
407110def4 | ||
|
|
79d86a335c | ||
|
|
0685be5f1e | ||
|
|
8f6ae52c70 | ||
|
|
af16be64a2 | ||
|
|
68071825c2 | ||
|
|
fbabb81acd | ||
|
|
8bc4b133ca | ||
|
|
2ce458dad5 | ||
|
|
478b186094 | ||
|
|
ab71b3163b | ||
|
|
c4de88e6c6 | ||
|
|
3b0e34f465 | ||
|
|
9077e78315 | ||
|
|
2f5fcd1f14 | ||
|
|
24d430a3ac | ||
|
|
307eb63ee5 | ||
|
|
07f85dc4b8 | ||
|
|
a4026e0ca0 | ||
|
|
ebd8dc7bb8 | ||
|
|
bac15d5472 | ||
|
|
8540fdcf19 | ||
|
|
fc8d39882f | ||
|
|
ca093eb81e | ||
|
|
35c42bccb4 | ||
|
|
52114b2d85 | ||
|
|
8f593f6357 | ||
|
|
6f8769ee8d | ||
|
|
1af461c206 | ||
|
|
8976dc1f79 | ||
|
|
9d47e62041 | ||
|
|
0daddfd477 | ||
|
|
89d617110f | ||
|
|
17941397e9 | ||
|
|
f2aa4aae79 | ||
|
|
e29b3143ed | ||
|
|
5419ff74ee | ||
|
|
723011078c | ||
|
|
fe7c0333ab | ||
|
|
c01940b62b | ||
|
|
7195a8f3ec | ||
|
|
7764de4e10 | ||
|
|
e05c526b4d | ||
|
|
bc20f98b44 | ||
|
|
742c3f3e3c | ||
|
|
9793977ffc | ||
|
|
6c85772fff | ||
|
|
9ff3254a4c | ||
|
|
7d906851dd | ||
|
|
691aa3308c | ||
|
|
6a57e67629 | ||
|
|
202c8a4b22 | ||
|
|
f2874ae3ef | ||
|
|
9e9cf9b768 | ||
|
|
c95dee84f2 | ||
|
|
65cde9960f | ||
|
|
7ca96178fb | ||
|
|
020feaf325 | ||
|
|
e70461e551 | ||
|
|
0e918434a2 | ||
|
|
63c85000d0 | ||
|
|
4113f6a65d | ||
|
|
f98d744076 | ||
|
|
bb4c293736 | ||
|
|
aa8b1e6efe | ||
|
|
0cb2e8ec2e | ||
|
|
0e96753ccb | ||
|
|
2b2df22d75 | ||
|
|
f65167e3c7 | ||
|
|
8fe2677a56 | ||
|
|
d09a7cf9c8 | ||
|
|
6bf72e5a59 | ||
|
|
7c877ebea2 | ||
|
|
e1983f7bcc | ||
|
|
a5e025a4e5 | ||
|
|
a7682d2639 | ||
|
|
30301c68f1 | ||
|
|
abf980d84e | ||
|
|
8718f55df2 | ||
|
|
d1a3a24527 | ||
|
|
f363b77a07 | ||
|
|
d5899a90c1 | ||
|
|
f1058ea611 | ||
|
|
00279da8d5 | ||
|
|
eda8b53949 | ||
|
|
cfc0cc8f6e | ||
|
|
ab61b2a2d5 | ||
|
|
f79a6ed30a | ||
|
|
4313722b06 | ||
|
|
eec4612a6f | ||
|
|
b2ac893efc | ||
|
|
14e8d9cd04 | ||
|
|
1a6cfd17cb | ||
|
|
3835507e00 | ||
|
|
4536589e2c | ||
|
|
1e39558fbc | ||
|
|
a089cc279b | ||
|
|
84f646dbad | ||
|
|
b9ec473aaa | ||
|
|
b7eb0dff02 | ||
|
|
06f550cc04 | ||
|
|
1752b1aec7 | ||
|
|
67ea640a14 | ||
|
|
f593060007 | ||
|
|
8504eed35c | ||
|
|
8b29b137a8 | ||
|
|
08d3ca6d27 | ||
|
|
e6767f4c0a | ||
|
|
9e5d65706e | ||
|
|
61d7b6da38 | ||
|
|
6affe64ec5 | ||
|
|
ab75718927 | ||
|
|
9e1747e539 | ||
|
|
a7396dc698 | ||
|
|
67699bd24c | ||
|
|
62c72c48a5 | ||
|
|
3944c8437a | ||
|
|
b7d603342e | ||
|
|
6e5db3c292 | ||
|
|
4fb26559f4 | ||
|
|
c366c1d1a3 | ||
|
|
e8cc6a169e | ||
|
|
7d7b6b88ba | ||
|
|
138af5c113 | ||
|
|
b3bcad9ce6 | ||
|
|
cd6f898f8e | ||
|
|
9ef088971b | ||
|
|
e399b1c0b1 | ||
|
|
59bacaf5b4 | ||
|
|
d872a156c1 | ||
|
|
c5e4583762 | ||
|
|
38b9c06138 | ||
|
|
df18a1e917 | ||
|
|
2bc40771ca | ||
|
|
5696c7a5da | ||
|
|
c5f980fc98 | ||
|
|
aebb68724a | ||
|
|
1f2ccd8c1c | ||
|
|
874058d86a | ||
|
|
8fccb80295 | ||
|
|
c9c60575c7 | ||
|
|
7e6ed20b26 | ||
|
|
52c7be4e16 | ||
|
|
36d250fc2b | ||
|
|
84ba0e03de | ||
|
|
7d27c4b546 | ||
|
|
80b2bab0f8 | ||
|
|
79372ced2f | ||
|
|
6fc91962bd | ||
|
|
fb76c23694 | ||
|
|
fc6c852e09 | ||
|
|
82f61ff1d4 | ||
|
|
64052f4f70 | ||
|
|
36c8150f25 | ||
|
|
cb492728ec | ||
|
|
fec02ca733 | ||
|
|
4e01a78286 | ||
|
|
538b39386b | ||
|
|
977aced343 | ||
|
|
83b5753bb5 | ||
|
|
a57c3b691f | ||
|
|
648a374ebb | ||
|
|
c3568e1211 | ||
|
|
6a16d4d55c | ||
|
|
ab2e71cc40 | ||
|
|
f79fe9f7d0 | ||
|
|
6956587106 | ||
|
|
349df56b09 | ||
|
|
e6f082d512 | ||
|
|
87feed1222 | ||
|
|
55c8a66613 | ||
|
|
2e88422c86 | ||
|
|
da841b82d4 | ||
|
|
9256a72c3e | ||
|
|
69120cc961 | ||
|
|
06aaf58f0b | ||
|
|
12e5a586d2 | ||
|
|
0e76ed7077 | ||
|
|
be911a7aa7 | ||
|
|
0556a528f3 | ||
|
|
de789cef66 | ||
|
|
7208d01547 | ||
|
|
46cb0a455d | ||
|
|
300e19b612 | ||
|
|
2666573050 | ||
|
|
5506b46c9d | ||
|
|
7b27cb7a1c | ||
|
|
84a70a1de0 | ||
|
|
1756ec49ad | ||
|
|
7b286b04b1 | ||
|
|
a731e88c78 | ||
|
|
ffd31f2ea7 | ||
|
|
356eeea505 | ||
|
|
34d9469157 | ||
|
|
36b1bf9875 | ||
|
|
7badd19a07 | ||
|
|
f122a9ab28 | ||
|
|
14dbad4dd7 | ||
|
|
cdce9ab50b | ||
|
|
30d3270e1d | ||
|
|
157923decc | ||
|
|
7bbcbc57eb | ||
|
|
9074463f4e | ||
|
|
3ebf4fdff5 | ||
|
|
77e5fa2d2b | ||
|
|
6bd2e65459 | ||
|
|
3363e9ad0c | ||
|
|
d1f0f45525 | ||
|
|
efc1b4a4ae | ||
|
|
a45303d9e6 | ||
|
|
871ff69f56 | ||
|
|
d4b4289c0b | ||
|
|
32af000435 | ||
|
|
64178192ad | ||
|
|
598752956f | ||
|
|
4dce0e5958 | ||
|
|
9f02aef930 | ||
|
|
0c25b0968b | ||
|
|
ddc0bd7202 | ||
|
|
20727e1508 | ||
|
|
3afc1964dc | ||
|
|
914020fd36 | ||
|
|
168a8cd5a6 | ||
|
|
eb94ed7806 | ||
|
|
db6b3ec427 | ||
|
|
32d0cdcea7 | ||
|
|
332fe8c884 | ||
|
|
59f0cf7732 | ||
|
|
b803505287 | ||
|
|
3542062ecd | ||
|
|
824c327a2c | ||
|
|
76a41dffa1 | ||
|
|
65482cad9c | ||
|
|
a17a4c0a3c | ||
|
|
2437cf09d1 | ||
|
|
c4c394845d | ||
|
|
a4d4f9a944 | ||
|
|
5eb086935e | ||
|
|
83a583a33f | ||
|
|
143c2d279b | ||
|
|
08dd596883 | ||
|
|
a4ffcebb0f | ||
|
|
8a14c22056 | ||
|
|
39f7b5284a | ||
|
|
4f58cd255b | ||
|
|
d96d7a533e | ||
|
|
0e59166c64 | ||
|
|
6d8161de73 | ||
|
|
494df64674 | ||
|
|
d057729675 | ||
|
|
b14a323ef8 | ||
|
|
f2b66468ec | ||
|
|
a48dfbbaf4 | ||
|
|
61625c4c5e | ||
|
|
3124f5e565 | ||
|
|
48a05f9afc | ||
|
|
26fdf8b00c | ||
|
|
34cb360d62 | ||
|
|
4bf5a80a7a | ||
|
|
40efcd71c3 | ||
|
|
aca291da9e | ||
|
|
7f6bcdfb11 | ||
|
|
696110a5b0 | ||
|
|
73fb61070f | ||
|
|
23da8047bc | ||
|
|
bf70a08c9f | ||
|
|
ce410dae65 | ||
|
|
94a42c4267 | ||
|
|
f4cc1fed36 | ||
|
|
7c27be784b | ||
|
|
7d1b22c337 | ||
|
|
1e550c8866 | ||
|
|
2febbe2795 | ||
|
|
299516f576 | ||
|
|
6edeab6f85 | ||
|
|
7b79d7ffde | ||
|
|
c4f06ccead | ||
|
|
541d5b3c19 | ||
|
|
e0b9a68fad | ||
|
|
d0098015fe | ||
|
|
3660a35c2c | ||
|
|
9132dd16f0 | ||
|
|
652551269e | ||
|
|
81730f37a9 | ||
|
|
4b86e96660 | ||
|
|
271240f11c | ||
|
|
00e8596a0e | ||
|
|
39ab429c88 | ||
|
|
96175c9fd3 | ||
|
|
cee3e83077 | ||
|
|
4fa8d4ba64 | ||
|
|
1ac4402f99 | ||
|
|
8554262aaf | ||
|
|
d4d989e562 | ||
|
|
d2766b2225 | ||
|
|
f124ee52ce | ||
|
|
2167d28514 | ||
|
|
80792dc555 | ||
|
|
080337f847 | ||
|
|
5e20c830fd | ||
|
|
b754fdb449 | ||
|
|
a926239613 | ||
|
|
c80b723112 | ||
|
|
a062b92dad | ||
|
|
89d40b7c94 | ||
|
|
7b5cb48dcc | ||
|
|
7de0121db5 | ||
|
|
cb338af8fb | ||
|
|
89bb5d2815 | ||
|
|
fffe2fb451 | ||
|
|
373a3ce491 | ||
|
|
0a798b9f9a | ||
|
|
5f0b15b5e9 | ||
|
|
9a93c8afe0 | ||
|
|
fe990100d9 | ||
|
|
df15a27ceb | ||
|
|
5f4aafed37 | ||
|
|
86298f3421 | ||
|
|
77971ac7e6 | ||
|
|
302f287967 | ||
|
|
b9c08d72c4 | ||
|
|
522860f736 | ||
|
|
b09fc75913 | ||
|
|
1b010c8a68 | ||
|
|
e2f424d4ab | ||
|
|
b9fb224d62 | ||
|
|
69e869852e | ||
|
|
618966b742 | ||
|
|
b68920ff45 | ||
|
|
9dfe0ee7b3 | ||
|
|
da6c578973 | ||
|
|
49d9ec7cf9 | ||
|
|
af07f1d549 | ||
|
|
db5f5fd435 | ||
|
|
7232625bab | ||
|
|
1557a4a039 | ||
|
|
b61575bbc3 | ||
|
|
3781777138 | ||
|
|
2f1f8e0c90 | ||
|
|
c4c2bf86ba | ||
|
|
f26668b81d | ||
|
|
964af6a064 | ||
|
|
c3a93a46ba | ||
|
|
57d593561a | ||
|
|
c098d80524 | ||
|
|
6c892af9f1 | ||
|
|
b545a1618b | ||
|
|
dec3d54ddd | ||
|
|
ce4d0bf102 | ||
|
|
4e6bada892 | ||
|
|
98ad2e3d55 | ||
|
|
e686b42934 | ||
|
|
239e01c3f8 | ||
|
|
0d839c3136 | ||
|
|
4a57698173 | ||
|
|
cc3bfad00a | ||
|
|
468051288c | ||
|
|
565ea12d88 | ||
|
|
1c7ef50568 | ||
|
|
878eae9787 | ||
|
|
24b1d6807b | ||
|
|
6bc081a1e1 | ||
|
|
afb16fff65 | ||
|
|
cf81a82645 | ||
|
|
dc820a60ae | ||
|
|
c57cc139f6 | ||
|
|
79426f35cd | ||
|
|
502a7ba81c | ||
|
|
deef209687 | ||
|
|
bb09f212d2 | ||
|
|
1e6556ebdd | ||
|
|
cfd86df45e | ||
|
|
c16a7a3c5c | ||
|
|
7060d2d2ca | ||
|
|
532d79bcc5 | ||
|
|
770097d6ab | ||
|
|
ce2a1c21de | ||
|
|
d16c9b2324 | ||
|
|
6e1c47ddf4 | ||
|
|
f1630023c7 | ||
|
|
d49e7be972 | ||
|
|
13d17fc4f7 | ||
|
|
d41a488eda | ||
|
|
375a18ebac | ||
|
|
20216ac4ba | ||
|
|
d27d8b0c5b | ||
|
|
a619527482 | ||
|
|
85ff92c4de | ||
|
|
b1121718db | ||
|
|
2905dcc8c0 | ||
|
|
c9855f7214 | ||
|
|
853b8f29a2 | ||
|
|
d5378ffa41 | ||
|
|
4400eba974 | ||
|
|
0cc656c6c0 | ||
|
|
c9b32ab5d0 | ||
|
|
f9ffd39661 | ||
|
|
bfc12d6856 | ||
|
|
11874b52de | ||
|
|
b4e928588e | ||
|
|
33a2222cd8 | ||
|
|
923e05a36c | ||
|
|
b5cc4d6fd7 | ||
|
|
25291f978f | ||
|
|
ad66170379 | ||
|
|
86e38419ac | ||
|
|
1a5e6a7edc | ||
|
|
7cf166d615 | ||
|
|
413c3e0208 | ||
|
|
7410498df1 | ||
|
|
7aee18110e | ||
|
|
7aca7b323a | ||
|
|
4651a0e224 | ||
|
|
d018e3b9b9 | ||
|
|
1c272842a7 | ||
|
|
0141e0ab4e | ||
|
|
e7d8695394 | ||
|
|
6876edea5a | ||
|
|
2e655d6341 | ||
|
|
2f4cca19c4 | ||
|
|
5eb74d8ca3 | ||
|
|
7b0d5a6700 | ||
|
|
427260663f | ||
|
|
047a443536 | ||
|
|
7a9ee8145d | ||
|
|
2535d0e537 | ||
|
|
6fe7d7991b | ||
|
|
d061958f90 | ||
|
|
3112200eb6 | ||
|
|
69a406832d | ||
|
|
cd4ceb0a61 | ||
|
|
450ec5dec1 | ||
|
|
c7abd1a067 | ||
|
|
c6d87756ab | ||
|
|
0916489388 | ||
|
|
b1e83c92e9 | ||
|
|
d01e06aa11 | ||
|
|
464107b13f | ||
|
|
e431b25fc1 | ||
|
|
e4ed917c1c | ||
|
|
ade3e6d2fb | ||
|
|
d12d18261f | ||
|
|
525b4439c9 | ||
|
|
43ec92ddc5 | ||
|
|
74127a038f | ||
|
|
a01bd39f21 | ||
|
|
9c707df93b | ||
|
|
4bdb189f10 | ||
|
|
c2a474df98 | ||
|
|
483dc5e953 | ||
|
|
f490f073b0 | ||
|
|
2eab8eba09 | ||
|
|
783c901567 | ||
|
|
90d1fa0f9b | ||
|
|
96b791b3b9 |
167 changed files with 13606 additions and 17599 deletions
4
.github/FUNDING.yml
vendored
Normal file
4
.github/FUNDING.yml
vendored
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
# These are supported funding model platforms
|
||||
|
||||
github: polhenarejos
|
||||
custom: ["https://www.paypal.me/polhenarejos"]
|
||||
50
.github/PULL_REQUEST_TEMPLATE.md
vendored
Normal file
50
.github/PULL_REQUEST_TEMPLATE.md
vendored
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
## Summary
|
||||
|
||||
Describe in plain language what this PR does and why.
|
||||
|
||||
- What problem does it solve?
|
||||
- Is it a bug fix, a new feature, a cleanup/refactor…?
|
||||
|
||||
|
||||
## Details / Impact
|
||||
|
||||
Please include any relevant details:
|
||||
|
||||
- Hardware / board(s) tested:
|
||||
- Firmware / commit/base version:
|
||||
- Security impact (if any):
|
||||
- e.g. changes PIN handling, touches key storage, affects attestation, etc.
|
||||
- Behavior changes:
|
||||
- e.g. new command, new API surface, different defaults, etc.
|
||||
|
||||
|
||||
## Testing
|
||||
|
||||
How did you test this change?
|
||||
|
||||
- Steps to reproduce / validate:
|
||||
- Expected vs actual results:
|
||||
- Any logs / traces (please remove secrets):
|
||||
|
||||
|
||||
## Licensing confirmation (required)
|
||||
|
||||
By checking the box below, you confirm ALL of the following:
|
||||
|
||||
- You are the author of this contribution, or you have the right to contribute it.
|
||||
- You have read `CONTRIBUTING.md`.
|
||||
- You agree that this contribution may be merged, used, modified, and redistributed:
|
||||
- under the AGPLv3 Community Edition, **and**
|
||||
- under any proprietary / commercial / Enterprise editions of this project,
|
||||
now or in the future.
|
||||
- You understand that submitting this PR does not create any support obligation,
|
||||
SLA, or guarantee of merge.
|
||||
|
||||
**I confirm the above licensing terms:**
|
||||
|
||||
- [ ] Yes, I agree
|
||||
|
||||
|
||||
## Anything else?
|
||||
|
||||
Optional: mention known limitations, follow-ups, or if this is related to an existing Issue.
|
||||
74
.github/workflows/codeql.yml
vendored
Normal file
74
.github/workflows/codeql.yml
vendored
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
# For most projects, this workflow file will not need changing; you simply need
|
||||
# to commit it to your repository.
|
||||
#
|
||||
# You may wish to alter this file to override the set of languages analyzed,
|
||||
# or to provide custom queries or build logic.
|
||||
#
|
||||
# ******** NOTE ********
|
||||
# We have attempted to detect the languages in your repository. Please check
|
||||
# the `language` matrix defined below to confirm you have the correct set of
|
||||
# supported CodeQL languages.
|
||||
#
|
||||
name: "CodeQL"
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ "master", "development", "development-eddsa" ]
|
||||
pull_request:
|
||||
# The branches below must be a subset of the branches above
|
||||
branches: [ "master", "development", "development-eddsa" ]
|
||||
schedule:
|
||||
- cron: '23 5 * * 4'
|
||||
workflow_dispatch:
|
||||
|
||||
jobs:
|
||||
analyze:
|
||||
name: Analyze
|
||||
runs-on: ubuntu-latest
|
||||
permissions:
|
||||
actions: read
|
||||
contents: read
|
||||
security-events: write
|
||||
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
language: [ 'cpp', 'python' ]
|
||||
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ]
|
||||
# Learn more about CodeQL language support at https://aka.ms/codeql-docs/language-support
|
||||
mode: [ 'pico', 'local' ]
|
||||
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v3
|
||||
|
||||
# Initializes the CodeQL tools for scanning.
|
||||
- name: Initialize CodeQL
|
||||
uses: github/codeql-action/init@v3
|
||||
with:
|
||||
languages: ${{ matrix.language }}
|
||||
# If you wish to specify custom queries, you can do so here or in a config file.
|
||||
# By default, queries listed here will override any specified in a config file.
|
||||
# Prefix the list here with "+" to use these queries and those in the config file.
|
||||
|
||||
# Details on CodeQL's query packs refer to : https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs
|
||||
# queries: security-extended,security-and-quality
|
||||
|
||||
|
||||
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
|
||||
# If this step fails, then you should remove it and run the build manually (see below)
|
||||
# - name: Autobuild
|
||||
# uses: github/codeql-action/autobuild@v2
|
||||
|
||||
# ℹ️ Command-line programs to run using the OS shell.
|
||||
# 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun
|
||||
|
||||
# If the Autobuild fails above, remove it and uncomment the following three lines.
|
||||
# modify them (or add more) to build your code if your project, please refer to the EXAMPLE below for guidance.
|
||||
|
||||
- run: |
|
||||
echo "Run, Build Application using script"
|
||||
./workflows/autobuild.sh ${{ matrix.mode }}
|
||||
|
||||
- name: Perform CodeQL Analysis
|
||||
uses: github/codeql-action/analyze@v3
|
||||
42
.github/workflows/nightly.yml
vendored
Normal file
42
.github/workflows/nightly.yml
vendored
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
name: "Nightly deploy"
|
||||
|
||||
on:
|
||||
schedule:
|
||||
- cron: '0 2 * * *'
|
||||
workflow_dispatch:
|
||||
|
||||
jobs:
|
||||
nightly:
|
||||
name: Deploy nightly
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
refs: [master, development]
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
ref: ${{ matrix.refs }}
|
||||
submodules: 'recursive'
|
||||
- name: Restore private key
|
||||
run: |
|
||||
echo "${{ secrets.PRIVATE_KEY_B64 }}" | base64 -d > private.pem
|
||||
chmod 600 private.pem
|
||||
- name : Build
|
||||
env:
|
||||
PICO_SDK_PATH: ../pico-sdk
|
||||
SECURE_BOOT_PKEY: ../private.pem
|
||||
run: |
|
||||
./workflows/autobuild.sh pico
|
||||
./build_pico_hsm.sh --no-eddsa
|
||||
./workflows/autobuild.sh esp32
|
||||
- name: Delete private key
|
||||
run: rm private.pem
|
||||
- name: Update nightly release
|
||||
uses: pyTooling/Actions/releaser@v6.7.0
|
||||
with:
|
||||
tag: nightly-${{ matrix.refs }}
|
||||
rm: true
|
||||
token: ${{ secrets.GITHUB_TOKEN }}
|
||||
files: release/*.*
|
||||
66
.github/workflows/test.yml
vendored
Normal file
66
.github/workflows/test.yml
vendored
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
# For most projects, this workflow file will not need changing; you simply need
|
||||
# to commit it to your repository.
|
||||
#
|
||||
# You may wish to alter this file to override the set of languages analyzed,
|
||||
# or to provide custom queries or build logic.
|
||||
#
|
||||
# ******** NOTE ********
|
||||
# We have attempted to detect the languages in your repository. Please check
|
||||
# the `language` matrix defined below to confirm you have the correct set of
|
||||
# supported CodeQL languages.
|
||||
#
|
||||
name: "Emulation and test"
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ "master", "development", "development-eddsa" ]
|
||||
pull_request:
|
||||
# The branches below must be a subset of the branches above
|
||||
branches: [ "master", "development", "development-eddsa" ]
|
||||
schedule:
|
||||
- cron: '23 5 * * 4'
|
||||
workflow_dispatch:
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout repository and submodules
|
||||
uses: actions/checkout@v3
|
||||
with:
|
||||
submodules: recursive
|
||||
- name: Build in container
|
||||
run: ./tests/build-in-docker.sh
|
||||
- name: Export image
|
||||
run: |
|
||||
mkdir -p artifacts
|
||||
docker save pico-hsm-test:bullseye -o artifacts/docker-image.tar
|
||||
- name: Temporarily save image
|
||||
uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: docker-artifact
|
||||
path: artifacts
|
||||
retention-days: 1
|
||||
|
||||
test:
|
||||
runs-on: ubuntu-latest
|
||||
needs: build
|
||||
strategy:
|
||||
matrix:
|
||||
suite: ["pkcs11", "pytest", "sc-hsm-pkcs11"]
|
||||
steps:
|
||||
- name: Checkout repository and submodules
|
||||
uses: actions/checkout@v3
|
||||
with:
|
||||
submodules: recursive
|
||||
- name: Retrieve saved image
|
||||
uses: actions/download-artifact@v4
|
||||
with:
|
||||
name: docker-artifact
|
||||
path: artifacts
|
||||
- name: Load image
|
||||
run: |
|
||||
cd artifacts
|
||||
docker load -q -i docker-image.tar
|
||||
- name: Test ${{ matrix.suite }}
|
||||
run: ./tests/run-test-in-docker.sh ${{ matrix.suite }}
|
||||
9
.gitmodules
vendored
9
.gitmodules
vendored
|
|
@ -1,6 +1,3 @@
|
|||
[submodule "OpenSC"]
|
||||
path = OpenSC
|
||||
url = https://github.com/polhenarejos/OpenSC
|
||||
[submodule "mbedtls"]
|
||||
path = mbedtls
|
||||
url = https://github.com/ARMmbed/mbedtls
|
||||
[submodule "pico-keys-sdk"]
|
||||
path = pico-keys-sdk
|
||||
url = https://github.com/polhenarejos/pico-keys-sdk
|
||||
|
|
|
|||
317
.uncrustify.cfg
Normal file
317
.uncrustify.cfg
Normal file
|
|
@ -0,0 +1,317 @@
|
|||
#
|
||||
# Uncrustify Configuration File
|
||||
# File Created With UncrustifyX 0.4.3 (252)
|
||||
#
|
||||
|
||||
# Code-Modifying
|
||||
# --------------
|
||||
|
||||
## Braces
|
||||
|
||||
# Braces on single-line do statement
|
||||
mod_full_brace_do = add # string (add/force/ignore/remove)
|
||||
|
||||
# Braces on single-line else statement
|
||||
mod_full_brace_if = add # string (add/force/ignore/remove)
|
||||
|
||||
# Braces on single-line for statement
|
||||
mod_full_brace_for = add # string (add/force/ignore/remove)
|
||||
|
||||
# Braces on single-line while statement
|
||||
mod_full_brace_while = add # string (add/force/ignore/remove)
|
||||
|
||||
## Parentheses
|
||||
|
||||
# Remove unnecessary parentheses on return statement
|
||||
mod_paren_on_return = remove # string (add/force/ignore/remove)
|
||||
|
||||
# Comments
|
||||
# --------
|
||||
|
||||
## Other
|
||||
|
||||
# Remove leading spaces from multi-line comments
|
||||
cmt_multi_check_last = false # boolean (false/true)
|
||||
|
||||
# General
|
||||
# -------
|
||||
|
||||
## Other
|
||||
|
||||
# Input tab size
|
||||
input_tab_size = 4 # number
|
||||
|
||||
# Indentation
|
||||
# -----------
|
||||
|
||||
## Indentation Size
|
||||
|
||||
# Indentation column size
|
||||
indent_columns = 4 # number
|
||||
|
||||
# Indentation size between case and switch
|
||||
indent_switch_case = 4 # number
|
||||
|
||||
## Other
|
||||
|
||||
# Align strings broken by backslash
|
||||
indent_align_string = true # boolean (false/true)
|
||||
|
||||
# Indent with tabs
|
||||
indent_with_tabs = 0 # number
|
||||
|
||||
# Line-Splitting
|
||||
# --------------
|
||||
|
||||
## Splitting
|
||||
|
||||
# Code width
|
||||
code_width = 100 # number
|
||||
|
||||
# Split long for statements at semicolons
|
||||
ls_for_split_full = true # boolean (false/true)
|
||||
|
||||
# Split long function prototypes/calls at commas
|
||||
ls_func_split_full = true # boolean (false/true)
|
||||
|
||||
# Newlines
|
||||
# --------
|
||||
|
||||
## Newline Between
|
||||
|
||||
# Newline between assignment and open brace
|
||||
nl_assign_brace = remove # string (add/force/ignore/remove)
|
||||
|
||||
# Newline between close brace and else
|
||||
nl_brace_else = add # string (add/force/ignore/remove)
|
||||
|
||||
# Newline between close brace and while
|
||||
nl_brace_while = remove # string (add/force/ignore/remove)
|
||||
|
||||
# Newline between do and open brace
|
||||
nl_do_brace = remove # string (add/force/ignore/remove)
|
||||
|
||||
# Newline between else and open brace
|
||||
nl_else_brace = remove # string (add/force/ignore/remove)
|
||||
|
||||
# Newline between enum and open brace
|
||||
nl_enum_brace = remove # string (add/force/ignore/remove)
|
||||
|
||||
# Newline between for and open brace
|
||||
nl_for_brace = remove # string (add/force/ignore/remove)
|
||||
|
||||
# Newline between function call and open brace
|
||||
nl_fcall_brace = add # string (add/force/ignore/remove)
|
||||
|
||||
# Newline between function signature and open brace
|
||||
nl_fdef_brace = remove # string (add/force/ignore/remove)
|
||||
|
||||
# Newline between if and open brace
|
||||
nl_if_brace = remove # string (add/force/ignore/remove)
|
||||
|
||||
# Newline between struct and open brace
|
||||
nl_struct_brace = remove # string (add/force/ignore/remove)
|
||||
|
||||
# Newline between switch and open brace
|
||||
nl_switch_brace = remove # string (add/force/ignore/remove)
|
||||
|
||||
# Newline between union and open brace
|
||||
nl_union_brace = remove # string (add/force/ignore/remove)
|
||||
|
||||
# Newline between while and open brace
|
||||
nl_while_brace = remove # string (add/force/ignore/remove)
|
||||
|
||||
## Other
|
||||
|
||||
# Newline count at end of file
|
||||
nl_end_of_file_min = 1 # number
|
||||
|
||||
# Newlines at end of file
|
||||
nl_end_of_file = add # string (add/force/ignore/remove)
|
||||
|
||||
# Newlines at start of file
|
||||
nl_start_of_file = remove # string (add/force/ignore/remove)
|
||||
|
||||
# Spacing
|
||||
# -------
|
||||
|
||||
## Space After
|
||||
|
||||
# Space after address-of operator
|
||||
sp_addr = remove # string (add/force/ignore/remove)
|
||||
|
||||
# Space after cast
|
||||
sp_after_cast = add # string (add/force/ignore/remove)
|
||||
|
||||
# Space after comma
|
||||
sp_after_comma = add # string (add/force/ignore/remove)
|
||||
|
||||
# Space after dereference operator
|
||||
sp_deref = remove # string (add/force/ignore/remove)
|
||||
|
||||
# Space after final semicolon in empty for statement
|
||||
sp_after_semi_for_empty = remove # string (add/force/ignore/remove)
|
||||
|
||||
# Space after invert operator
|
||||
sp_inv = remove # string (add/force/ignore/remove)
|
||||
|
||||
# Space after not operator
|
||||
sp_not = remove # string (add/force/ignore/remove)
|
||||
|
||||
# Space after pointer star
|
||||
sp_after_ptr_star = remove # string (add/force/ignore/remove)
|
||||
|
||||
# Space after pointer star followed by function
|
||||
sp_after_ptr_star_func = remove # string (add/force/ignore/remove)
|
||||
|
||||
# Space after semicolon
|
||||
sp_after_semi = add # string (add/force/ignore/remove)
|
||||
|
||||
# Space after semicolon in non-empty for statements
|
||||
sp_after_semi_for = add # string (add/force/ignore/remove)
|
||||
|
||||
# Space after sign in assignment
|
||||
sp_sign = remove # string (add/force/ignore/remove)
|
||||
|
||||
# Space after type
|
||||
sp_after_type = add # string (add/force/ignore/remove)
|
||||
|
||||
## Space Around
|
||||
|
||||
# Space around arithmetic operators
|
||||
sp_arith = add # string (add/force/ignore/remove)
|
||||
|
||||
# Space around assignment operator
|
||||
sp_assign = add # string (add/force/ignore/remove)
|
||||
|
||||
# Space around boolean operators
|
||||
sp_bool = add # string (add/force/ignore/remove)
|
||||
|
||||
# Space around compare operators
|
||||
sp_compare = add # string (add/force/ignore/remove)
|
||||
|
||||
# Space around increment/decrement operators
|
||||
sp_incdec = remove # string (add/force/ignore/remove)
|
||||
|
||||
# Space around member operators
|
||||
sp_member = remove # string (add/force/ignore/remove)
|
||||
|
||||
# Space around preprocessor concatenation operator
|
||||
sp_pp_concat = ignore # string (add/force/ignore/remove)
|
||||
|
||||
# Space around ternary condition colon
|
||||
sp_cond_colon = add # string (add/force/ignore/remove)
|
||||
|
||||
# Space around ternary condition question mark
|
||||
sp_cond_question = add # string (add/force/ignore/remove)
|
||||
|
||||
## Space Before
|
||||
|
||||
# Space before backslash-newline at end of line
|
||||
sp_before_nl_cont = add # string (add/force/ignore/remove)
|
||||
|
||||
# Space before case colon
|
||||
sp_before_case_colon = remove # string (add/force/ignore/remove)
|
||||
|
||||
# Space before comma
|
||||
sp_before_comma = remove # string (add/force/ignore/remove)
|
||||
|
||||
# Space before if/for/switch/while open parenthesis
|
||||
sp_before_sparen = force # string (add/force/ignore/remove)
|
||||
|
||||
# Space before pointer star
|
||||
sp_before_ptr_star = add # string (add/force/ignore/remove)
|
||||
|
||||
# Space before semicolon
|
||||
sp_before_semi = remove # string (add/force/ignore/remove)
|
||||
|
||||
# Space before semicolon in empty for statement
|
||||
sp_before_semi_for_empty = remove # string (add/force/ignore/remove)
|
||||
|
||||
# Space before semicolon in for statements
|
||||
sp_before_semi_for = remove # string (add/force/ignore/remove)
|
||||
|
||||
## Space Between
|
||||
|
||||
# Space between __attribute__ and open parenthesis
|
||||
sp_attribute_paren = remove # string (add/force/ignore/remove)
|
||||
|
||||
# Space between close brace and else
|
||||
sp_brace_else = remove # string (add/force/ignore/remove)
|
||||
|
||||
# Space between close brace and typedef name
|
||||
sp_brace_typedef = force # string (add/force/ignore/remove)
|
||||
|
||||
# Space between closing parenthesis and open brace
|
||||
sp_fparen_brace = add # string (add/force/ignore/remove)
|
||||
|
||||
# Space between defined and open parenthesis
|
||||
sp_defined_paren = remove # string (add/force/ignore/remove)
|
||||
|
||||
# Space between else and open brace
|
||||
sp_else_brace = force # string (add/force/ignore/remove)
|
||||
|
||||
# Space between function name and open parenthesis
|
||||
sp_func_call_paren = remove # string (add/force/ignore/remove)
|
||||
|
||||
# Space between function name and open parenthesis in declaration
|
||||
sp_func_proto_paren = remove # string (add/force/ignore/remove)
|
||||
|
||||
# Space between function name and open parenthesis in function definition
|
||||
sp_func_def_paren = remove # string (add/force/ignore/remove)
|
||||
|
||||
# Space between if/for/switch/while close parenthesis and open brace
|
||||
sp_sparen_brace = force # string (add/force/ignore/remove)
|
||||
|
||||
# Space between macro and value
|
||||
sp_macro = add # string (add/force/ignore/remove)
|
||||
|
||||
# Space between macro function close parenthesis and value
|
||||
sp_macro_func = add # string (add/force/ignore/remove)
|
||||
|
||||
# Space between nested parentheses
|
||||
sp_paren_paren = remove # string (add/force/ignore/remove)
|
||||
|
||||
# Space between pointer stars
|
||||
sp_between_ptr_star = remove # string (add/force/ignore/remove)
|
||||
|
||||
# Space between preprocessor else and comment
|
||||
sp_endif_cmt = add # string (add/force/ignore/remove)
|
||||
|
||||
# Space between return type and function name
|
||||
sp_type_func = add # string (add/force/ignore/remove)
|
||||
|
||||
# Space between sizeof and open parenthesis
|
||||
sp_sizeof_paren = remove # string (add/force/ignore/remove)
|
||||
|
||||
## Space Inside
|
||||
|
||||
# Space inside braces
|
||||
sp_inside_braces = add # string (add/force/ignore/remove)
|
||||
|
||||
# Space inside cast parentheses
|
||||
sp_inside_paren_cast = remove # string (add/force/ignore/remove)
|
||||
|
||||
# Space inside empty function parentheses
|
||||
sp_inside_fparens = remove # string (add/force/ignore/remove)
|
||||
|
||||
# Space inside enum braces
|
||||
sp_inside_braces_enum = add # string (add/force/ignore/remove)
|
||||
|
||||
# Space inside function parentheses
|
||||
sp_inside_fparen = remove # string (add/force/ignore/remove)
|
||||
|
||||
# Space inside if-condition parentheses
|
||||
sp_inside_sparen = remove # string (add/force/ignore/remove)
|
||||
|
||||
# Space inside non-empty square brackets
|
||||
sp_inside_square = remove # string (add/force/ignore/remove)
|
||||
|
||||
# Space inside parentheses
|
||||
sp_inside_paren = remove # string (add/force/ignore/remove)
|
||||
|
||||
# Space inside parentheses in function type
|
||||
sp_inside_tparen = remove # string (add/force/ignore/remove)
|
||||
|
||||
# Space inside struct/union braces
|
||||
sp_inside_braces_struct = add # string (add/force/ignore/remove)
|
||||
231
CMakeLists.txt
231
CMakeLists.txt
|
|
@ -1,124 +1,131 @@
|
|||
#
|
||||
# This file is part of the Pico HSM distribution (https://github.com/polhenarejos/pico-hsm).
|
||||
# Copyright (c) 2022 Pol Henarejos.
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, version 3.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful, but
|
||||
# WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
# General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
|
||||
cmake_minimum_required(VERSION 3.13)
|
||||
|
||||
include(pico_sdk_import.cmake)
|
||||
if(ESP_PLATFORM)
|
||||
set(EXTRA_COMPONENT_DIRS src pico-keys-sdk/src)
|
||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||
else()
|
||||
if(NOT ENABLE_EMULATION)
|
||||
set(PICO_USE_FASTEST_SUPPORTED_CLOCK 1)
|
||||
include(pico_sdk_import.cmake)
|
||||
endif()
|
||||
|
||||
project(pico_hsm C CXX ASM)
|
||||
project(pico_hsm C CXX ASM)
|
||||
|
||||
set(CMAKE_C_STANDARD 11)
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
set(CMAKE_C_STANDARD 11)
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
|
||||
pico_sdk_init()
|
||||
if(NOT DEFINED __FOR_CI)
|
||||
set(__FOR_CI 0)
|
||||
endif()
|
||||
if(__FOR_CI)
|
||||
add_definitions(-D__FOR_CI)
|
||||
endif()
|
||||
|
||||
add_executable(pico_hsm)
|
||||
|
||||
if (NOT DEFINED USB_VID)
|
||||
set(USB_VID 0xFEFF)
|
||||
add_executable(pico_hsm)
|
||||
endif()
|
||||
add_definitions(-DUSB_VID=${USB_VID})
|
||||
if (NOT DEFINED USB_PID)
|
||||
set(USB_PID 0xFCFD)
|
||||
set(USB_ITF_CCID 1)
|
||||
set(USB_ITF_WCID 1)
|
||||
include(pico-keys-sdk/pico_keys_sdk_import.cmake)
|
||||
if(NOT ESP_PLATFORM)
|
||||
set(SOURCES ${PICO_KEYS_SOURCES})
|
||||
endif()
|
||||
add_definitions(-DUSB_PID=${USB_PID})
|
||||
set(SOURCES ${SOURCES}
|
||||
${CMAKE_CURRENT_LIST_DIR}/src/hsm/sc_hsm.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_select.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_list_keys.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_read_binary.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_verify.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_reset_retry.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_challenge.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_external_authenticate.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_mse.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_initialize.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_key_domain.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_key_wrap.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_keypair_gen.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_update_ef.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_delete_file.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_change_pin.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_key_gen.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_signature.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_key_unwrap.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_decrypt_asym.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_cipher_sym.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_derive_asym.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_extras.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_general_authenticate.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_session_pin.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_puk_auth.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_pso.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/src/hsm/cmd_bip_slip.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/src/hsm/cvc.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/src/hsm/files.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/src/hsm/kek.c
|
||||
)
|
||||
|
||||
set_source_files_properties(
|
||||
${CMAKE_CURRENT_LIST_DIR}/OpenSC/src/libopensc/ctx.c
|
||||
PROPERTIES COMPILE_DEFINITIONS "PACKAGE_VERSION=\"0.22.0\";OPENSC_CONF_PATH=\".\""
|
||||
SET_VERSION(ver_major ver_minor "${CMAKE_CURRENT_LIST_DIR}/src/hsm/version.h" 2)
|
||||
|
||||
if(ESP_PLATFORM)
|
||||
project(pico_hsm)
|
||||
endif()
|
||||
|
||||
set(INCLUDES ${INCLUDES}
|
||||
${CMAKE_CURRENT_LIST_DIR}/src/hsm
|
||||
)
|
||||
if(NOT ESP_PLATFORM)
|
||||
target_sources(pico_hsm PUBLIC ${SOURCES})
|
||||
target_include_directories(pico_hsm PUBLIC ${INCLUDES})
|
||||
|
||||
target_compile_options(pico_hsm PUBLIC
|
||||
-Wall
|
||||
)
|
||||
if(NOT MSVC)
|
||||
target_compile_options(pico_hsm PUBLIC
|
||||
-Werror
|
||||
)
|
||||
endif()
|
||||
|
||||
target_sources(pico_hsm PUBLIC
|
||||
${CMAKE_CURRENT_LIST_DIR}/src/hsm/hsm2040.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/src/hsm/sc_hsm.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/src/usb/usb_descriptors.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/src/fs/file.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/src/fs/flash.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/src/fs/low_flash.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/src/rng/random.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/src/rng/neug.c
|
||||
|
||||
${CMAKE_CURRENT_LIST_DIR}/mbedtls/library/sha256.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/mbedtls/library/aes.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/mbedtls/library/sha512.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/mbedtls/library/rsa.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/mbedtls/library/bignum.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/mbedtls/library/platform_util.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/mbedtls/library/md.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/mbedtls/library/oid.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/mbedtls/library/rsa_alt_helpers.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/mbedtls/library/constant_time.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/mbedtls/library/ecdsa.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/mbedtls/library/ecp.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/mbedtls/library/ecp_curves.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/mbedtls/library/asn1write.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/mbedtls/library/hmac_drbg.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/mbedtls/library/md5.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/mbedtls/library/ripemd160.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/mbedtls/library/sha1.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/mbedtls/library/ecdh.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/mbedtls/library/cmac.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/mbedtls/library/cipher.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/mbedtls/library/cipher_wrap.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/mbedtls/library/chachapoly.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/mbedtls/library/camellia.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/mbedtls/library/chacha20.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/mbedtls/library/aria.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/mbedtls/library/poly1305.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/mbedtls/library/gcm.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/mbedtls/library/ccm.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/mbedtls/library/des.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/mbedtls/library/nist_kw.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/mbedtls/library/hkdf.c
|
||||
|
||||
${CMAKE_CURRENT_LIST_DIR}/OpenSC/src/libopensc/pkcs15.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/OpenSC/src/libopensc/pkcs15-prkey.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/OpenSC/src/libopensc/pkcs15-algo.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/OpenSC/src/libopensc/pkcs15-cert.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/OpenSC/src/libopensc/pkcs15-data.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/OpenSC/src/libopensc/pkcs15-pin.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/OpenSC/src/libopensc/pkcs15-pubkey.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/OpenSC/src/libopensc/pkcs15-sec.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/OpenSC/src/libopensc/pkcs15-skey.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/OpenSC/src/libopensc/asn1.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/OpenSC/src/libopensc/log.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/OpenSC/src/libopensc/errors.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/OpenSC/src/libopensc/sc.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/OpenSC/src/libopensc/ctx.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/OpenSC/src/libopensc/pkcs15-sc-hsm.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/OpenSC/src/libopensc/padding.c
|
||||
)
|
||||
if(ENABLE_EMULATION)
|
||||
if(NOT MSVC)
|
||||
target_compile_options(pico_hsm PUBLIC
|
||||
-fdata-sections
|
||||
-ffunction-sections
|
||||
)
|
||||
endif()
|
||||
if(APPLE)
|
||||
target_link_options(pico_hsm PUBLIC
|
||||
-Wl,-dead_strip
|
||||
)
|
||||
elseif(MSVC)
|
||||
target_compile_options(pico_hsm PUBLIC
|
||||
-WX
|
||||
)
|
||||
|
||||
target_include_directories(pico_hsm PUBLIC
|
||||
${CMAKE_CURRENT_LIST_DIR}/src/fs
|
||||
${CMAKE_CURRENT_LIST_DIR}/src/hsm
|
||||
${CMAKE_CURRENT_LIST_DIR}/src/rng
|
||||
${CMAKE_CURRENT_LIST_DIR}/src/usb
|
||||
${CMAKE_CURRENT_LIST_DIR}/opensc/src
|
||||
${CMAKE_CURRENT_LIST_DIR}/mbedtls/include
|
||||
${CMAKE_CURRENT_LIST_DIR}/mbedtls/library
|
||||
)
|
||||
|
||||
pico_add_extra_outputs(pico_hsm)
|
||||
|
||||
#target_compile_definitions(pico_hsm PRIVATE MBEDTLS_ECDSA_DETERMINISTIC=1)
|
||||
|
||||
target_link_libraries(pico_hsm PRIVATE pico_stdlib tinyusb_device tinyusb_board pico_multicore hardware_flash hardware_sync hardware_adc pico_unique_id)
|
||||
|
||||
#
|
||||
#project(flash_nuke C CXX ASM)
|
||||
#add_executable(flash_nuke nuke.c)
|
||||
#target_link_libraries(flash_nuke
|
||||
# pico_stdlib
|
||||
# hardware_flash
|
||||
# )
|
||||
#pico_set_binary_type(flash_nuke no_flash)
|
||||
#
|
||||
#pico_add_extra_outputs(flash_nuke)
|
||||
|
||||
#project(memory C CXX ASM)
|
||||
#add_executable(memory memory.c)
|
||||
#target_link_libraries(memory
|
||||
# pico_stdlib
|
||||
# hardware_flash
|
||||
# )
|
||||
#pico_set_binary_type(memory no_flash)
|
||||
#
|
||||
#pico_add_extra_outputs(memory)
|
||||
target_link_libraries(pico_hsm PUBLIC wsock32 ws2_32 Bcrypt)
|
||||
else()
|
||||
target_link_options(pico_hsm PUBLIC
|
||||
-Wl,--gc-sections
|
||||
)
|
||||
endif(APPLE)
|
||||
target_link_libraries(pico_hsm PRIVATE pthread m)
|
||||
else()
|
||||
pico_add_extra_outputs(${CMAKE_PROJECT_NAME})
|
||||
endif()
|
||||
endif()
|
||||
|
|
|
|||
105
CONTRIBUTING.md
Normal file
105
CONTRIBUTING.md
Normal file
|
|
@ -0,0 +1,105 @@
|
|||
# Contributing
|
||||
|
||||
Thank you for your interest in contributing to this project.
|
||||
|
||||
This repository is published in two forms:
|
||||
- a Community Edition released under AGPLv3, and
|
||||
- a proprietary / commercial / Enterprise Edition offered to organizations.
|
||||
|
||||
To keep that model legally clean, we need to be explicit about how contributions can be used.
|
||||
|
||||
By opening a pull request, you agree to all of the following:
|
||||
|
||||
1. **You have the right to contribute this code.**
|
||||
You are either the original author of the contribution, or you have obtained the necessary rights/permissions to contribute it under these terms.
|
||||
|
||||
2. **Dual licensing permission.**
|
||||
You agree that your contribution may be:
|
||||
- merged into this repository, and
|
||||
- used, copied, modified, sublicensed, and redistributed
|
||||
- under the AGPLv3 Community Edition, and
|
||||
- under any proprietary / commercial / Enterprise editions of this project,
|
||||
now or in the future.
|
||||
|
||||
In other words: you are granting the project maintainer(s) the right to include
|
||||
your contribution in both the open-source (AGPLv3) codebase and in closed-source /
|
||||
commercially licensed builds, without any additional approval or payment.
|
||||
|
||||
3. **Attribution.**
|
||||
The maintainers may keep or add attribution lines such as
|
||||
`Copyright (c) <your name>` or an AUTHORS / CONTRIBUTORS list.
|
||||
The maintainers may also make changes for clarity, style, security, refactoring,
|
||||
or integration reasons.
|
||||
|
||||
4. **No automatic SLA.**
|
||||
Submitting a pull request does *not* create any support obligation,
|
||||
service-level agreement, warranty, or guarantee that the contribution
|
||||
will be reviewed, merged, or maintained.
|
||||
|
||||
5. **Potential rejection for business reasons.**
|
||||
Features that fall under "Enterprise / Commercial" functionality
|
||||
(e.g. multi-tenant provisioning at scale, centralized audit trails,
|
||||
corporate policy enforcement, attestation/branding flows, key escrow / dual-control,
|
||||
etc.) may be declined for the public AGPLv3 tree even if technically valid.
|
||||
That is normal: some functionality is intentionally offered only
|
||||
under commercial terms.
|
||||
|
||||
If you are not comfortable with these terms, **do not open a pull request yet.**
|
||||
Instead, please open an Issue to start a discussion.
|
||||
|
||||
## How to contribute (technical side)
|
||||
|
||||
### 1. Bug reports / issues
|
||||
- Please include:
|
||||
- hardware / board revision
|
||||
- firmware / commit hash
|
||||
- exact steps to reproduce
|
||||
- expected vs actual behavior
|
||||
- logs / traces if available (strip secrets)
|
||||
|
||||
Security-sensitive findings: do **not** post publicly.
|
||||
Send a short report by email instead so it can be triaged responsibly.
|
||||
|
||||
### 2. Small fixes / minor improvements
|
||||
- You can open a PR directly for:
|
||||
- bug fixes
|
||||
- portability fixes / new board definitions
|
||||
- clarifications in code comments
|
||||
- build / tooling cleanup
|
||||
- documentation of existing behavior
|
||||
|
||||
Please keep PRs focused (one logical change per PR if possible).
|
||||
|
||||
### 3. Larger features / behavior changes
|
||||
- Please open an Issue first and describe:
|
||||
- what problem you're solving (not just "add feature X")
|
||||
- impact on existing flows / security model
|
||||
- any new dependencies
|
||||
|
||||
This helps avoid doing a bunch of work on something that won't be accepted
|
||||
in the Community Edition.
|
||||
|
||||
### 4. Coding style / security posture
|
||||
- Aim for clarity and small, auditable changes. This code runs in places
|
||||
where secrets live.
|
||||
- No debug backdoors, no "just for testing" shortcuts left enabled.
|
||||
- Keep external dependencies minimal and license-compatible
|
||||
(MIT / Apache 2.0 / similarly permissive is usually fine).
|
||||
|
||||
### 5. Commit / PR format
|
||||
- Use descriptive commit messages ("Fix PIN retry counter wrap" is better than "fix stuff").
|
||||
- In the PR description, please include a short summary of what was changed and why.
|
||||
- At the bottom of the PR description, **copy/paste and confirm the licensing line below**:
|
||||
|
||||
> I confirm that I have read `CONTRIBUTING.md` and I agree that this contribution may be used under both the AGPLv3 Community Edition and any proprietary / commercial / Enterprise editions of this project, now or in the future.
|
||||
|
||||
A PR without that confirmation may be delayed or closed without merge.
|
||||
|
||||
## Thank you
|
||||
|
||||
This project exists because people build on it, break it, fix it,
|
||||
and push it into places it wasn't originally designed to go.
|
||||
|
||||
Whether you are here for research, hacking on hardware,
|
||||
rolling out secure keys for a team, or building a commercial product:
|
||||
thank you for helping improve it.
|
||||
54
Dockerfile
Normal file
54
Dockerfile
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
FROM debian:bullseye
|
||||
ENV DEBIAN_FRONTEND noninteractive
|
||||
RUN apt-get update && apt-get install -y \
|
||||
build-essential \
|
||||
git \
|
||||
cmake \
|
||||
gcc-arm-none-eabi \
|
||||
libnewlib-arm-none-eabi \
|
||||
libstdc++-arm-none-eabi-newlib \
|
||||
python3 \
|
||||
python3-pip
|
||||
|
||||
RUN useradd -m builduser
|
||||
|
||||
USER builduser
|
||||
|
||||
WORKDIR /home/builduser
|
||||
|
||||
VOLUME /home/builduser/release
|
||||
|
||||
ARG VERSION_PICO_SDK 2.0.0
|
||||
|
||||
RUN mkdir -p /home/builduser/Devel/pico
|
||||
RUN cd /home/builduser/Devel/pico \
|
||||
&& git clone https://github.com/raspberrypi/pico-sdk.git \
|
||||
&& cd pico-sdk \
|
||||
&& git checkout $VERSION_PICO_SDK \
|
||||
&& git submodule update --init --recursive
|
||||
|
||||
RUN pip install cryptography
|
||||
|
||||
ARG VERSION_MAJOR 4
|
||||
ARG VERSION_MINOR 2
|
||||
|
||||
RUN cd /home/builduser \
|
||||
&& git clone https://github.com/polhenarejos/pico-hsm.git \
|
||||
&& cd pico-hsm \
|
||||
&& git checkout v${VERSION_MAJOR}.${VERSION_MINOR} \
|
||||
&& git submodule update --init --recursive \
|
||||
&& mkdir build_release
|
||||
|
||||
ENV PICO_SDK_PATH /home/builduser/Devel/pico/pico-sdk
|
||||
|
||||
|
||||
|
||||
ARG USB_VID 0xfeff
|
||||
ARG USB_PID 0xfcfd
|
||||
|
||||
ARG PICO_BOARD waveshare_rp2040_zero
|
||||
|
||||
RUN cd /home/builduser/pico-hsm \
|
||||
&& cd build_release \
|
||||
&& cmake .. -DPICO_BOARD=$PICO_BOARD -DUSB_VID=${USB_VID} -DUSB_PID=${USB_PID} \
|
||||
&& make -kj20
|
||||
116
ENTERPRISE.md
Normal file
116
ENTERPRISE.md
Normal file
|
|
@ -0,0 +1,116 @@
|
|||
# Enterprise / Commercial Edition
|
||||
|
||||
This project is offered under two editions:
|
||||
|
||||
## 1. Community Edition (FOSS)
|
||||
|
||||
The Community Edition is released under the GNU Affero General Public License v3 (AGPLv3).
|
||||
|
||||
Intended for:
|
||||
- individual users and researchers
|
||||
- evaluation / prototyping
|
||||
- internal lab / security testing
|
||||
|
||||
You are allowed to:
|
||||
- read and study the source code
|
||||
- modify it
|
||||
- run it internally
|
||||
|
||||
Obligations under AGPLv3:
|
||||
- If you distribute modified firmware/binaries/libraries to third parties, you must provide the corresponding source code of your modifications.
|
||||
- If you run a modified version of this project as a network-accessible service (internal or external), you must offer the source code of those modifications to the users of that service.
|
||||
- No warranty, no support, no SLA.
|
||||
- Enterprise features (bulk provisioning, multi-user policy enforcement, device inventory / revocation, corporate PIN rules, custom attestation/identity, etc.) are NOT included.
|
||||
|
||||
The Community Edition will continue to exist.
|
||||
|
||||
## 2. Enterprise / Commercial Edition
|
||||
|
||||
The Enterprise / Commercial Edition is a proprietary license for organizations that need to:
|
||||
|
||||
- deploy this in production at scale (multiple devices / multiple users / multiple teams)
|
||||
- integrate it into their own physical product or appliance
|
||||
- run it as an internal service (VM / container / private cloud "HSM / auth backend") for multiple internal teams or tenants
|
||||
- enforce internal security policy (admin vs user roles, mandatory PIN rules, secure offboarding / revocation)
|
||||
- avoid any AGPLv3 disclosure obligations for their own modifications and integration code
|
||||
|
||||
### What the Enterprise Edition provides
|
||||
|
||||
**Base license package (always included):**
|
||||
- **Commercial license (proprietary).**
|
||||
You may run and integrate the software/firmware in production — including virtualized / internal-cloud style deployments — without being required to disclose derivative source code under AGPLv3.
|
||||
- **Official signed builds.**
|
||||
You receive signed builds from the original developer so you can prove integrity and provenance.
|
||||
- **Onboarding call (up to 1 hour).**
|
||||
A live remote session to get you from "we have it" to "it’s actually running in our environment" with minimal guesswork.
|
||||
|
||||
**Optional enterprise components (available on demand, scoped and priced per customer):**
|
||||
- **Production / multi-user readiness.**
|
||||
Permission to operate the system with multiple users, multiple devices and multiple teams in real environments.
|
||||
- **Bulk / fleet provisioning.**
|
||||
Automated enrollment for many tokens/devices/users at once (CSV / directory import), scripted onboarding of new users, initial PIN assignment / reset workflows, and role-based access (admin vs user).
|
||||
- **Policy & lifecycle tooling.**
|
||||
Corporate PIN policy enforcement, per-user / per-team access control, device inventory / traceability, and secure revocation / retirement when someone leaves.
|
||||
- **Custom attestation / per-organization identity.**
|
||||
Per-company certificate chains and attestation keys so devices can prove "this token/HSM is officially ours," including anti-cloning / unique device identity for OEM and fleet use.
|
||||
- **Virtualization / internal cloud deployment support.**
|
||||
Guidance and components to run this as an internal service (VM, container, private-cloud HSM/auth backend) serving multiple internal teams or tenants under your brand.
|
||||
- **Post-quantum (PQC) key material handling.**
|
||||
Integration/roadmap support for PQC algorithms (auth / signing) and secure PQC key storage inside the device or service.
|
||||
- **Hierarchical deterministic key derivation (HD).**
|
||||
Wallet-style hierarchical key trees (BIP32-like concepts adapted to this platform) for issuing per-user / per-tenant / per-purpose subkeys without exporting the root secret — e.g. embedded wallet logic, tenant isolation, firmware signing trees, large fleets.
|
||||
- **Cryptographically signed audit trail / tamper-evident event logging.**
|
||||
High-assurance logging of sensitive actions (key use, provisioning, PIN resets, revocations) with integrity protection for forensic / compliance needs.
|
||||
- **Dual-control / two-person approval ("four-eyes").**
|
||||
Require multi-party authorization for high-risk actions such as firmware signing, key export, or critical configuration changes — standard in high-assurance / regulated environments.
|
||||
- **Secure key escrow / disaster recovery design.**
|
||||
Split-secret or escrowed backup strategies so you don’t lose critical signing keys if a single admin disappears or hardware is lost.
|
||||
- **Release-signing / supply-chain hardening pipeline.**
|
||||
Reference tooling and process so every production firmware/binary is signed with hardware-backed keys, proving origin and preventing tampering in transit or at manufacturing.
|
||||
- **Policy-locked hardened mode ("FIPS-style profile").**
|
||||
Restricted algorithms, debug disabled, no raw key export, tamper-evident configuration for regulated / high-assurance deployments.
|
||||
- **Priority support / security response SLA.**
|
||||
A direct line and guaranteed response window for production-impacting security issues.
|
||||
- **White-label demo / pre-sales bundle.**
|
||||
Branded demo firmware + safe onboarding script so you can show "your product" to your own customers without exposing real production secrets.
|
||||
|
||||
These components are NOT automatically bundled. They are available case-by-case depending on your use case and are priced separately.
|
||||
|
||||
### Licensing models
|
||||
|
||||
- **Internal Use License**
|
||||
Internal production use within one legal entity (your company), including internal private cloud / virtualized deployments for multiple internal teams.
|
||||
Optional enterprise components can be added as needed.
|
||||
|
||||
- **OEM / Redistribution / Service License**
|
||||
Integration into a product/appliance you ship to customers, OR operating this as a managed service / hosted feature for external clients or third parties.
|
||||
Optional enterprise components (attestation branding, PQC support, HD key derivation, multi-tenant service hardening, audit trail, etc.) can be added as required.
|
||||
|
||||
Pricing depends on scope, fleet size, number of users/tenants, regulatory requirements, and which optional components you select.
|
||||
|
||||
### Request a quote
|
||||
|
||||
Email: pol@henarejos.me
|
||||
Subject: `ENTERPRISE LICENSE <your company name>`
|
||||
|
||||
Please include:
|
||||
- Company name and country
|
||||
- Intended use:
|
||||
- Internal private deployment
|
||||
- OEM / external service to third parties
|
||||
- Approximate scale (number of devices/tokens, number of users/tenants)
|
||||
- Which optional components you are interested in (bulk provisioning, policy & lifecycle tooling, attestation branding / anti-cloning, virtualization/cloud, PQC, HD key derivation, audit trail, dual-control, key escrow, supply-chain signing, hardened mode, SLA, white-label demo)
|
||||
|
||||
You will receive:
|
||||
1. A short commercial license agreement naming your company.
|
||||
2. Access to the base package (and any optional components agreed).
|
||||
3. Scheduling of the onboarding call.
|
||||
|
||||
## Why Enterprise exists
|
||||
|
||||
- Companies often need hardware-backed security (HSM, FIDO2, OpenPGP, etc.) under their own control, but cannot or will not open-source their internal security workflows.
|
||||
- They also need multi-user / fleet-management features that hobby users do not.
|
||||
- The commercial license funds continued development, maintenance and new hardware support.
|
||||
|
||||
The Community Edition remains AGPLv3.
|
||||
The Enterprise Edition is for production, scale, and legal clarity.
|
||||
661
LICENSE
Normal file
661
LICENSE
Normal file
|
|
@ -0,0 +1,661 @@
|
|||
GNU AFFERO GENERAL PUBLIC LICENSE
|
||||
Version 3, 19 November 2007
|
||||
|
||||
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
||||
Everyone is permitted to copy and distribute verbatim copies
|
||||
of this license document, but changing it is not allowed.
|
||||
|
||||
Preamble
|
||||
|
||||
The GNU Affero General Public License is a free, copyleft license for
|
||||
software and other kinds of works, specifically designed to ensure
|
||||
cooperation with the community in the case of network server software.
|
||||
|
||||
The licenses for most software and other practical works are designed
|
||||
to take away your freedom to share and change the works. By contrast,
|
||||
our General Public Licenses are intended to guarantee your freedom to
|
||||
share and change all versions of a program--to make sure it remains free
|
||||
software for all its users.
|
||||
|
||||
When we speak of free software, we are referring to freedom, not
|
||||
price. Our General Public Licenses are designed to make sure that you
|
||||
have the freedom to distribute copies of free software (and charge for
|
||||
them if you wish), that you receive source code or can get it if you
|
||||
want it, that you can change the software or use pieces of it in new
|
||||
free programs, and that you know you can do these things.
|
||||
|
||||
Developers that use our General Public Licenses protect your rights
|
||||
with two steps: (1) assert copyright on the software, and (2) offer
|
||||
you this License which gives you legal permission to copy, distribute
|
||||
and/or modify the software.
|
||||
|
||||
A secondary benefit of defending all users' freedom is that
|
||||
improvements made in alternate versions of the program, if they
|
||||
receive widespread use, become available for other developers to
|
||||
incorporate. Many developers of free software are heartened and
|
||||
encouraged by the resulting cooperation. However, in the case of
|
||||
software used on network servers, this result may fail to come about.
|
||||
The GNU General Public License permits making a modified version and
|
||||
letting the public access it on a server without ever releasing its
|
||||
source code to the public.
|
||||
|
||||
The GNU Affero General Public License is designed specifically to
|
||||
ensure that, in such cases, the modified source code becomes available
|
||||
to the community. It requires the operator of a network server to
|
||||
provide the source code of the modified version running there to the
|
||||
users of that server. Therefore, public use of a modified version, on
|
||||
a publicly accessible server, gives the public access to the source
|
||||
code of the modified version.
|
||||
|
||||
An older license, called the Affero General Public License and
|
||||
published by Affero, was designed to accomplish similar goals. This is
|
||||
a different license, not a version of the Affero GPL, but Affero has
|
||||
released a new version of the Affero GPL which permits relicensing under
|
||||
this license.
|
||||
|
||||
The precise terms and conditions for copying, distribution and
|
||||
modification follow.
|
||||
|
||||
TERMS AND CONDITIONS
|
||||
|
||||
0. Definitions.
|
||||
|
||||
"This License" refers to version 3 of the GNU Affero General Public License.
|
||||
|
||||
"Copyright" also means copyright-like laws that apply to other kinds of
|
||||
works, such as semiconductor masks.
|
||||
|
||||
"The Program" refers to any copyrightable work licensed under this
|
||||
License. Each licensee is addressed as "you". "Licensees" and
|
||||
"recipients" may be individuals or organizations.
|
||||
|
||||
To "modify" a work means to copy from or adapt all or part of the work
|
||||
in a fashion requiring copyright permission, other than the making of an
|
||||
exact copy. The resulting work is called a "modified version" of the
|
||||
earlier work or a work "based on" the earlier work.
|
||||
|
||||
A "covered work" means either the unmodified Program or a work based
|
||||
on the Program.
|
||||
|
||||
To "propagate" a work means to do anything with it that, without
|
||||
permission, would make you directly or secondarily liable for
|
||||
infringement under applicable copyright law, except executing it on a
|
||||
computer or modifying a private copy. Propagation includes copying,
|
||||
distribution (with or without modification), making available to the
|
||||
public, and in some countries other activities as well.
|
||||
|
||||
To "convey" a work means any kind of propagation that enables other
|
||||
parties to make or receive copies. Mere interaction with a user through
|
||||
a computer network, with no transfer of a copy, is not conveying.
|
||||
|
||||
An interactive user interface displays "Appropriate Legal Notices"
|
||||
to the extent that it includes a convenient and prominently visible
|
||||
feature that (1) displays an appropriate copyright notice, and (2)
|
||||
tells the user that there is no warranty for the work (except to the
|
||||
extent that warranties are provided), that licensees may convey the
|
||||
work under this License, and how to view a copy of this License. If
|
||||
the interface presents a list of user commands or options, such as a
|
||||
menu, a prominent item in the list meets this criterion.
|
||||
|
||||
1. Source Code.
|
||||
|
||||
The "source code" for a work means the preferred form of the work
|
||||
for making modifications to it. "Object code" means any non-source
|
||||
form of a work.
|
||||
|
||||
A "Standard Interface" means an interface that either is an official
|
||||
standard defined by a recognized standards body, or, in the case of
|
||||
interfaces specified for a particular programming language, one that
|
||||
is widely used among developers working in that language.
|
||||
|
||||
The "System Libraries" of an executable work include anything, other
|
||||
than the work as a whole, that (a) is included in the normal form of
|
||||
packaging a Major Component, but which is not part of that Major
|
||||
Component, and (b) serves only to enable use of the work with that
|
||||
Major Component, or to implement a Standard Interface for which an
|
||||
implementation is available to the public in source code form. A
|
||||
"Major Component", in this context, means a major essential component
|
||||
(kernel, window system, and so on) of the specific operating system
|
||||
(if any) on which the executable work runs, or a compiler used to
|
||||
produce the work, or an object code interpreter used to run it.
|
||||
|
||||
The "Corresponding Source" for a work in object code form means all
|
||||
the source code needed to generate, install, and (for an executable
|
||||
work) run the object code and to modify the work, including scripts to
|
||||
control those activities. However, it does not include the work's
|
||||
System Libraries, or general-purpose tools or generally available free
|
||||
programs which are used unmodified in performing those activities but
|
||||
which are not part of the work. For example, Corresponding Source
|
||||
includes interface definition files associated with source files for
|
||||
the work, and the source code for shared libraries and dynamically
|
||||
linked subprograms that the work is specifically designed to require,
|
||||
such as by intimate data communication or control flow between those
|
||||
subprograms and other parts of the work.
|
||||
|
||||
The Corresponding Source need not include anything that users
|
||||
can regenerate automatically from other parts of the Corresponding
|
||||
Source.
|
||||
|
||||
The Corresponding Source for a work in source code form is that
|
||||
same work.
|
||||
|
||||
2. Basic Permissions.
|
||||
|
||||
All rights granted under this License are granted for the term of
|
||||
copyright on the Program, and are irrevocable provided the stated
|
||||
conditions are met. This License explicitly affirms your unlimited
|
||||
permission to run the unmodified Program. The output from running a
|
||||
covered work is covered by this License only if the output, given its
|
||||
content, constitutes a covered work. This License acknowledges your
|
||||
rights of fair use or other equivalent, as provided by copyright law.
|
||||
|
||||
You may make, run and propagate covered works that you do not
|
||||
convey, without conditions so long as your license otherwise remains
|
||||
in force. You may convey covered works to others for the sole purpose
|
||||
of having them make modifications exclusively for you, or provide you
|
||||
with facilities for running those works, provided that you comply with
|
||||
the terms of this License in conveying all material for which you do
|
||||
not control copyright. Those thus making or running the covered works
|
||||
for you must do so exclusively on your behalf, under your direction
|
||||
and control, on terms that prohibit them from making any copies of
|
||||
your copyrighted material outside their relationship with you.
|
||||
|
||||
Conveying under any other circumstances is permitted solely under
|
||||
the conditions stated below. Sublicensing is not allowed; section 10
|
||||
makes it unnecessary.
|
||||
|
||||
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
|
||||
|
||||
No covered work shall be deemed part of an effective technological
|
||||
measure under any applicable law fulfilling obligations under article
|
||||
11 of the WIPO copyright treaty adopted on 20 December 1996, or
|
||||
similar laws prohibiting or restricting circumvention of such
|
||||
measures.
|
||||
|
||||
When you convey a covered work, you waive any legal power to forbid
|
||||
circumvention of technological measures to the extent such circumvention
|
||||
is effected by exercising rights under this License with respect to
|
||||
the covered work, and you disclaim any intention to limit operation or
|
||||
modification of the work as a means of enforcing, against the work's
|
||||
users, your or third parties' legal rights to forbid circumvention of
|
||||
technological measures.
|
||||
|
||||
4. Conveying Verbatim Copies.
|
||||
|
||||
You may convey verbatim copies of the Program's source code as you
|
||||
receive it, in any medium, provided that you conspicuously and
|
||||
appropriately publish on each copy an appropriate copyright notice;
|
||||
keep intact all notices stating that this License and any
|
||||
non-permissive terms added in accord with section 7 apply to the code;
|
||||
keep intact all notices of the absence of any warranty; and give all
|
||||
recipients a copy of this License along with the Program.
|
||||
|
||||
You may charge any price or no price for each copy that you convey,
|
||||
and you may offer support or warranty protection for a fee.
|
||||
|
||||
5. Conveying Modified Source Versions.
|
||||
|
||||
You may convey a work based on the Program, or the modifications to
|
||||
produce it from the Program, in the form of source code under the
|
||||
terms of section 4, provided that you also meet all of these conditions:
|
||||
|
||||
a) The work must carry prominent notices stating that you modified
|
||||
it, and giving a relevant date.
|
||||
|
||||
b) The work must carry prominent notices stating that it is
|
||||
released under this License and any conditions added under section
|
||||
7. This requirement modifies the requirement in section 4 to
|
||||
"keep intact all notices".
|
||||
|
||||
c) You must license the entire work, as a whole, under this
|
||||
License to anyone who comes into possession of a copy. This
|
||||
License will therefore apply, along with any applicable section 7
|
||||
additional terms, to the whole of the work, and all its parts,
|
||||
regardless of how they are packaged. This License gives no
|
||||
permission to license the work in any other way, but it does not
|
||||
invalidate such permission if you have separately received it.
|
||||
|
||||
d) If the work has interactive user interfaces, each must display
|
||||
Appropriate Legal Notices; however, if the Program has interactive
|
||||
interfaces that do not display Appropriate Legal Notices, your
|
||||
work need not make them do so.
|
||||
|
||||
A compilation of a covered work with other separate and independent
|
||||
works, which are not by their nature extensions of the covered work,
|
||||
and which are not combined with it such as to form a larger program,
|
||||
in or on a volume of a storage or distribution medium, is called an
|
||||
"aggregate" if the compilation and its resulting copyright are not
|
||||
used to limit the access or legal rights of the compilation's users
|
||||
beyond what the individual works permit. Inclusion of a covered work
|
||||
in an aggregate does not cause this License to apply to the other
|
||||
parts of the aggregate.
|
||||
|
||||
6. Conveying Non-Source Forms.
|
||||
|
||||
You may convey a covered work in object code form under the terms
|
||||
of sections 4 and 5, provided that you also convey the
|
||||
machine-readable Corresponding Source under the terms of this License,
|
||||
in one of these ways:
|
||||
|
||||
a) Convey the object code in, or embodied in, a physical product
|
||||
(including a physical distribution medium), accompanied by the
|
||||
Corresponding Source fixed on a durable physical medium
|
||||
customarily used for software interchange.
|
||||
|
||||
b) Convey the object code in, or embodied in, a physical product
|
||||
(including a physical distribution medium), accompanied by a
|
||||
written offer, valid for at least three years and valid for as
|
||||
long as you offer spare parts or customer support for that product
|
||||
model, to give anyone who possesses the object code either (1) a
|
||||
copy of the Corresponding Source for all the software in the
|
||||
product that is covered by this License, on a durable physical
|
||||
medium customarily used for software interchange, for a price no
|
||||
more than your reasonable cost of physically performing this
|
||||
conveying of source, or (2) access to copy the
|
||||
Corresponding Source from a network server at no charge.
|
||||
|
||||
c) Convey individual copies of the object code with a copy of the
|
||||
written offer to provide the Corresponding Source. This
|
||||
alternative is allowed only occasionally and noncommercially, and
|
||||
only if you received the object code with such an offer, in accord
|
||||
with subsection 6b.
|
||||
|
||||
d) Convey the object code by offering access from a designated
|
||||
place (gratis or for a charge), and offer equivalent access to the
|
||||
Corresponding Source in the same way through the same place at no
|
||||
further charge. You need not require recipients to copy the
|
||||
Corresponding Source along with the object code. If the place to
|
||||
copy the object code is a network server, the Corresponding Source
|
||||
may be on a different server (operated by you or a third party)
|
||||
that supports equivalent copying facilities, provided you maintain
|
||||
clear directions next to the object code saying where to find the
|
||||
Corresponding Source. Regardless of what server hosts the
|
||||
Corresponding Source, you remain obligated to ensure that it is
|
||||
available for as long as needed to satisfy these requirements.
|
||||
|
||||
e) Convey the object code using peer-to-peer transmission, provided
|
||||
you inform other peers where the object code and Corresponding
|
||||
Source of the work are being offered to the general public at no
|
||||
charge under subsection 6d.
|
||||
|
||||
A separable portion of the object code, whose source code is excluded
|
||||
from the Corresponding Source as a System Library, need not be
|
||||
included in conveying the object code work.
|
||||
|
||||
A "User Product" is either (1) a "consumer product", which means any
|
||||
tangible personal property which is normally used for personal, family,
|
||||
or household purposes, or (2) anything designed or sold for incorporation
|
||||
into a dwelling. In determining whether a product is a consumer product,
|
||||
doubtful cases shall be resolved in favor of coverage. For a particular
|
||||
product received by a particular user, "normally used" refers to a
|
||||
typical or common use of that class of product, regardless of the status
|
||||
of the particular user or of the way in which the particular user
|
||||
actually uses, or expects or is expected to use, the product. A product
|
||||
is a consumer product regardless of whether the product has substantial
|
||||
commercial, industrial or non-consumer uses, unless such uses represent
|
||||
the only significant mode of use of the product.
|
||||
|
||||
"Installation Information" for a User Product means any methods,
|
||||
procedures, authorization keys, or other information required to install
|
||||
and execute modified versions of a covered work in that User Product from
|
||||
a modified version of its Corresponding Source. The information must
|
||||
suffice to ensure that the continued functioning of the modified object
|
||||
code is in no case prevented or interfered with solely because
|
||||
modification has been made.
|
||||
|
||||
If you convey an object code work under this section in, or with, or
|
||||
specifically for use in, a User Product, and the conveying occurs as
|
||||
part of a transaction in which the right of possession and use of the
|
||||
User Product is transferred to the recipient in perpetuity or for a
|
||||
fixed term (regardless of how the transaction is characterized), the
|
||||
Corresponding Source conveyed under this section must be accompanied
|
||||
by the Installation Information. But this requirement does not apply
|
||||
if neither you nor any third party retains the ability to install
|
||||
modified object code on the User Product (for example, the work has
|
||||
been installed in ROM).
|
||||
|
||||
The requirement to provide Installation Information does not include a
|
||||
requirement to continue to provide support service, warranty, or updates
|
||||
for a work that has been modified or installed by the recipient, or for
|
||||
the User Product in which it has been modified or installed. Access to a
|
||||
network may be denied when the modification itself materially and
|
||||
adversely affects the operation of the network or violates the rules and
|
||||
protocols for communication across the network.
|
||||
|
||||
Corresponding Source conveyed, and Installation Information provided,
|
||||
in accord with this section must be in a format that is publicly
|
||||
documented (and with an implementation available to the public in
|
||||
source code form), and must require no special password or key for
|
||||
unpacking, reading or copying.
|
||||
|
||||
7. Additional Terms.
|
||||
|
||||
"Additional permissions" are terms that supplement the terms of this
|
||||
License by making exceptions from one or more of its conditions.
|
||||
Additional permissions that are applicable to the entire Program shall
|
||||
be treated as though they were included in this License, to the extent
|
||||
that they are valid under applicable law. If additional permissions
|
||||
apply only to part of the Program, that part may be used separately
|
||||
under those permissions, but the entire Program remains governed by
|
||||
this License without regard to the additional permissions.
|
||||
|
||||
When you convey a copy of a covered work, you may at your option
|
||||
remove any additional permissions from that copy, or from any part of
|
||||
it. (Additional permissions may be written to require their own
|
||||
removal in certain cases when you modify the work.) You may place
|
||||
additional permissions on material, added by you to a covered work,
|
||||
for which you have or can give appropriate copyright permission.
|
||||
|
||||
Notwithstanding any other provision of this License, for material you
|
||||
add to a covered work, you may (if authorized by the copyright holders of
|
||||
that material) supplement the terms of this License with terms:
|
||||
|
||||
a) Disclaiming warranty or limiting liability differently from the
|
||||
terms of sections 15 and 16 of this License; or
|
||||
|
||||
b) Requiring preservation of specified reasonable legal notices or
|
||||
author attributions in that material or in the Appropriate Legal
|
||||
Notices displayed by works containing it; or
|
||||
|
||||
c) Prohibiting misrepresentation of the origin of that material, or
|
||||
requiring that modified versions of such material be marked in
|
||||
reasonable ways as different from the original version; or
|
||||
|
||||
d) Limiting the use for publicity purposes of names of licensors or
|
||||
authors of the material; or
|
||||
|
||||
e) Declining to grant rights under trademark law for use of some
|
||||
trade names, trademarks, or service marks; or
|
||||
|
||||
f) Requiring indemnification of licensors and authors of that
|
||||
material by anyone who conveys the material (or modified versions of
|
||||
it) with contractual assumptions of liability to the recipient, for
|
||||
any liability that these contractual assumptions directly impose on
|
||||
those licensors and authors.
|
||||
|
||||
All other non-permissive additional terms are considered "further
|
||||
restrictions" within the meaning of section 10. If the Program as you
|
||||
received it, or any part of it, contains a notice stating that it is
|
||||
governed by this License along with a term that is a further
|
||||
restriction, you may remove that term. If a license document contains
|
||||
a further restriction but permits relicensing or conveying under this
|
||||
License, you may add to a covered work material governed by the terms
|
||||
of that license document, provided that the further restriction does
|
||||
not survive such relicensing or conveying.
|
||||
|
||||
If you add terms to a covered work in accord with this section, you
|
||||
must place, in the relevant source files, a statement of the
|
||||
additional terms that apply to those files, or a notice indicating
|
||||
where to find the applicable terms.
|
||||
|
||||
Additional terms, permissive or non-permissive, may be stated in the
|
||||
form of a separately written license, or stated as exceptions;
|
||||
the above requirements apply either way.
|
||||
|
||||
8. Termination.
|
||||
|
||||
You may not propagate or modify a covered work except as expressly
|
||||
provided under this License. Any attempt otherwise to propagate or
|
||||
modify it is void, and will automatically terminate your rights under
|
||||
this License (including any patent licenses granted under the third
|
||||
paragraph of section 11).
|
||||
|
||||
However, if you cease all violation of this License, then your
|
||||
license from a particular copyright holder is reinstated (a)
|
||||
provisionally, unless and until the copyright holder explicitly and
|
||||
finally terminates your license, and (b) permanently, if the copyright
|
||||
holder fails to notify you of the violation by some reasonable means
|
||||
prior to 60 days after the cessation.
|
||||
|
||||
Moreover, your license from a particular copyright holder is
|
||||
reinstated permanently if the copyright holder notifies you of the
|
||||
violation by some reasonable means, this is the first time you have
|
||||
received notice of violation of this License (for any work) from that
|
||||
copyright holder, and you cure the violation prior to 30 days after
|
||||
your receipt of the notice.
|
||||
|
||||
Termination of your rights under this section does not terminate the
|
||||
licenses of parties who have received copies or rights from you under
|
||||
this License. If your rights have been terminated and not permanently
|
||||
reinstated, you do not qualify to receive new licenses for the same
|
||||
material under section 10.
|
||||
|
||||
9. Acceptance Not Required for Having Copies.
|
||||
|
||||
You are not required to accept this License in order to receive or
|
||||
run a copy of the Program. Ancillary propagation of a covered work
|
||||
occurring solely as a consequence of using peer-to-peer transmission
|
||||
to receive a copy likewise does not require acceptance. However,
|
||||
nothing other than this License grants you permission to propagate or
|
||||
modify any covered work. These actions infringe copyright if you do
|
||||
not accept this License. Therefore, by modifying or propagating a
|
||||
covered work, you indicate your acceptance of this License to do so.
|
||||
|
||||
10. Automatic Licensing of Downstream Recipients.
|
||||
|
||||
Each time you convey a covered work, the recipient automatically
|
||||
receives a license from the original licensors, to run, modify and
|
||||
propagate that work, subject to this License. You are not responsible
|
||||
for enforcing compliance by third parties with this License.
|
||||
|
||||
An "entity transaction" is a transaction transferring control of an
|
||||
organization, or substantially all assets of one, or subdividing an
|
||||
organization, or merging organizations. If propagation of a covered
|
||||
work results from an entity transaction, each party to that
|
||||
transaction who receives a copy of the work also receives whatever
|
||||
licenses to the work the party's predecessor in interest had or could
|
||||
give under the previous paragraph, plus a right to possession of the
|
||||
Corresponding Source of the work from the predecessor in interest, if
|
||||
the predecessor has it or can get it with reasonable efforts.
|
||||
|
||||
You may not impose any further restrictions on the exercise of the
|
||||
rights granted or affirmed under this License. For example, you may
|
||||
not impose a license fee, royalty, or other charge for exercise of
|
||||
rights granted under this License, and you may not initiate litigation
|
||||
(including a cross-claim or counterclaim in a lawsuit) alleging that
|
||||
any patent claim is infringed by making, using, selling, offering for
|
||||
sale, or importing the Program or any portion of it.
|
||||
|
||||
11. Patents.
|
||||
|
||||
A "contributor" is a copyright holder who authorizes use under this
|
||||
License of the Program or a work on which the Program is based. The
|
||||
work thus licensed is called the contributor's "contributor version".
|
||||
|
||||
A contributor's "essential patent claims" are all patent claims
|
||||
owned or controlled by the contributor, whether already acquired or
|
||||
hereafter acquired, that would be infringed by some manner, permitted
|
||||
by this License, of making, using, or selling its contributor version,
|
||||
but do not include claims that would be infringed only as a
|
||||
consequence of further modification of the contributor version. For
|
||||
purposes of this definition, "control" includes the right to grant
|
||||
patent sublicenses in a manner consistent with the requirements of
|
||||
this License.
|
||||
|
||||
Each contributor grants you a non-exclusive, worldwide, royalty-free
|
||||
patent license under the contributor's essential patent claims, to
|
||||
make, use, sell, offer for sale, import and otherwise run, modify and
|
||||
propagate the contents of its contributor version.
|
||||
|
||||
In the following three paragraphs, a "patent license" is any express
|
||||
agreement or commitment, however denominated, not to enforce a patent
|
||||
(such as an express permission to practice a patent or covenant not to
|
||||
sue for patent infringement). To "grant" such a patent license to a
|
||||
party means to make such an agreement or commitment not to enforce a
|
||||
patent against the party.
|
||||
|
||||
If you convey a covered work, knowingly relying on a patent license,
|
||||
and the Corresponding Source of the work is not available for anyone
|
||||
to copy, free of charge and under the terms of this License, through a
|
||||
publicly available network server or other readily accessible means,
|
||||
then you must either (1) cause the Corresponding Source to be so
|
||||
available, or (2) arrange to deprive yourself of the benefit of the
|
||||
patent license for this particular work, or (3) arrange, in a manner
|
||||
consistent with the requirements of this License, to extend the patent
|
||||
license to downstream recipients. "Knowingly relying" means you have
|
||||
actual knowledge that, but for the patent license, your conveying the
|
||||
covered work in a country, or your recipient's use of the covered work
|
||||
in a country, would infringe one or more identifiable patents in that
|
||||
country that you have reason to believe are valid.
|
||||
|
||||
If, pursuant to or in connection with a single transaction or
|
||||
arrangement, you convey, or propagate by procuring conveyance of, a
|
||||
covered work, and grant a patent license to some of the parties
|
||||
receiving the covered work authorizing them to use, propagate, modify
|
||||
or convey a specific copy of the covered work, then the patent license
|
||||
you grant is automatically extended to all recipients of the covered
|
||||
work and works based on it.
|
||||
|
||||
A patent license is "discriminatory" if it does not include within
|
||||
the scope of its coverage, prohibits the exercise of, or is
|
||||
conditioned on the non-exercise of one or more of the rights that are
|
||||
specifically granted under this License. You may not convey a covered
|
||||
work if you are a party to an arrangement with a third party that is
|
||||
in the business of distributing software, under which you make payment
|
||||
to the third party based on the extent of your activity of conveying
|
||||
the work, and under which the third party grants, to any of the
|
||||
parties who would receive the covered work from you, a discriminatory
|
||||
patent license (a) in connection with copies of the covered work
|
||||
conveyed by you (or copies made from those copies), or (b) primarily
|
||||
for and in connection with specific products or compilations that
|
||||
contain the covered work, unless you entered into that arrangement,
|
||||
or that patent license was granted, prior to 28 March 2007.
|
||||
|
||||
Nothing in this License shall be construed as excluding or limiting
|
||||
any implied license or other defenses to infringement that may
|
||||
otherwise be available to you under applicable patent law.
|
||||
|
||||
12. No Surrender of Others' Freedom.
|
||||
|
||||
If conditions are imposed on you (whether by court order, agreement or
|
||||
otherwise) that contradict the conditions of this License, they do not
|
||||
excuse you from the conditions of this License. If you cannot convey a
|
||||
covered work so as to satisfy simultaneously your obligations under this
|
||||
License and any other pertinent obligations, then as a consequence you may
|
||||
not convey it at all. For example, if you agree to terms that obligate you
|
||||
to collect a royalty for further conveying from those to whom you convey
|
||||
the Program, the only way you could satisfy both those terms and this
|
||||
License would be to refrain entirely from conveying the Program.
|
||||
|
||||
13. Remote Network Interaction; Use with the GNU General Public License.
|
||||
|
||||
Notwithstanding any other provision of this License, if you modify the
|
||||
Program, your modified version must prominently offer all users
|
||||
interacting with it remotely through a computer network (if your version
|
||||
supports such interaction) an opportunity to receive the Corresponding
|
||||
Source of your version by providing access to the Corresponding Source
|
||||
from a network server at no charge, through some standard or customary
|
||||
means of facilitating copying of software. This Corresponding Source
|
||||
shall include the Corresponding Source for any work covered by version 3
|
||||
of the GNU General Public License that is incorporated pursuant to the
|
||||
following paragraph.
|
||||
|
||||
Notwithstanding any other provision of this License, you have
|
||||
permission to link or combine any covered work with a work licensed
|
||||
under version 3 of the GNU General Public License into a single
|
||||
combined work, and to convey the resulting work. The terms of this
|
||||
License will continue to apply to the part which is the covered work,
|
||||
but the work with which it is combined will remain governed by version
|
||||
3 of the GNU General Public License.
|
||||
|
||||
14. Revised Versions of this License.
|
||||
|
||||
The Free Software Foundation may publish revised and/or new versions of
|
||||
the GNU Affero General Public License from time to time. Such new versions
|
||||
will be similar in spirit to the present version, but may differ in detail to
|
||||
address new problems or concerns.
|
||||
|
||||
Each version is given a distinguishing version number. If the
|
||||
Program specifies that a certain numbered version of the GNU Affero General
|
||||
Public License "or any later version" applies to it, you have the
|
||||
option of following the terms and conditions either of that numbered
|
||||
version or of any later version published by the Free Software
|
||||
Foundation. If the Program does not specify a version number of the
|
||||
GNU Affero General Public License, you may choose any version ever published
|
||||
by the Free Software Foundation.
|
||||
|
||||
If the Program specifies that a proxy can decide which future
|
||||
versions of the GNU Affero General Public License can be used, that proxy's
|
||||
public statement of acceptance of a version permanently authorizes you
|
||||
to choose that version for the Program.
|
||||
|
||||
Later license versions may give you additional or different
|
||||
permissions. However, no additional obligations are imposed on any
|
||||
author or copyright holder as a result of your choosing to follow a
|
||||
later version.
|
||||
|
||||
15. Disclaimer of Warranty.
|
||||
|
||||
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
|
||||
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
|
||||
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
|
||||
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
|
||||
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
|
||||
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
|
||||
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
|
||||
|
||||
16. Limitation of Liability.
|
||||
|
||||
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
||||
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
|
||||
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
|
||||
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
|
||||
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
|
||||
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
|
||||
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
|
||||
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
|
||||
SUCH DAMAGES.
|
||||
|
||||
17. Interpretation of Sections 15 and 16.
|
||||
|
||||
If the disclaimer of warranty and limitation of liability provided
|
||||
above cannot be given local legal effect according to their terms,
|
||||
reviewing courts shall apply local law that most closely approximates
|
||||
an absolute waiver of all civil liability in connection with the
|
||||
Program, unless a warranty or assumption of liability accompanies a
|
||||
copy of the Program in return for a fee.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
How to Apply These Terms to Your New Programs
|
||||
|
||||
If you develop a new program, and you want it to be of the greatest
|
||||
possible use to the public, the best way to achieve this is to make it
|
||||
free software which everyone can redistribute and change under these terms.
|
||||
|
||||
To do so, attach the following notices to the program. It is safest
|
||||
to attach them to the start of each source file to most effectively
|
||||
state the exclusion of warranty; and each file should have at least
|
||||
the "copyright" line and a pointer to where the full notice is found.
|
||||
|
||||
<one line to give the program's name and a brief idea of what it does.>
|
||||
Copyright (C) <year> <name of author>
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
Also add information on how to contact you by electronic and paper mail.
|
||||
|
||||
If your software can interact with users remotely through a computer
|
||||
network, you should also make sure that it provides a way for users to
|
||||
get its source. For example, if your program is a web application, its
|
||||
interface could display a "Source" link that leads users to an archive
|
||||
of the code. There are many ways you could offer source, and different
|
||||
solutions will be better for different programs; see section 13 for the
|
||||
specific requirements.
|
||||
|
||||
You should also get your employer (if you work as a programmer) or school,
|
||||
if any, to sign a "copyright disclaimer" for the program, if necessary.
|
||||
For more information on this, and how to apply and follow the GNU AGPL, see
|
||||
<https://www.gnu.org/licenses/>.
|
||||
1
OpenSC
1
OpenSC
|
|
@ -1 +0,0 @@
|
|||
Subproject commit 1320393a7ed2f9c9a9ed6aefbf262ca75b35b932
|
||||
385
README.md
385
README.md
|
|
@ -1,45 +1,201 @@
|
|||
# Raspberry Pico HSM
|
||||
This is a project to create a Hardware Security Module (HSM) with a Raspberry Pico. It converts your Pico board into a HSM which is able to generate and store private keys, encrypt or decrypt with AES or signing data without to disclose the private key. In detail, the private key never leaves the board and it cannot be retrieved as it is encrypted in the flash memory.
|
||||
# Pico HSM
|
||||
This project aims to transform a Raspberry Pi Pico or ESP32 microcontroller into a Hardware Security Module (HSM). The modified Pico or ESP32 board will be capable of generating and storing private keys, performing AES encryption or decryption, and signing data without exposing the private key. Specifically, the private key remains securely on the board and cannot be retrieved since it is encrypted within the flash memory.
|
||||
|
||||
## Capabilities
|
||||
- Key generation and protected storing.
|
||||
- RSA key generation from 1024 to 4096 bits.
|
||||
- ECDSA key generation from 192 to 521 bits.
|
||||
- ECC curves secp192r1, secp256r1, secp384r1, secp521r1, brainpoolP256r1, brainpoolP384r1, brainpoolP512r1, secp192k1 (insecure), secp256k1.
|
||||
- SHA1, SHA224, SHA256, SHA384, SHA256 digests.
|
||||
- RSA-PSS, RSA-PKCS and raw RSA signature.
|
||||
- ECDSA signature.
|
||||
- RSA-OEP and RSA-X-509 decryption.
|
||||
- AES key generation of 128, 192 and 256 bits.
|
||||
- AES-CBC encryption/decryption.
|
||||
- PIN authorization.
|
||||
- PKCS11 compliant interface.
|
||||
- HRNG (hardware random number generator).
|
||||
- Device Key Encryption Key (DKEK) shares.
|
||||
- DKEK n-of-m threshold scheme.
|
||||
- USB/CCID support with OpenSC, openssl, etc.
|
||||
- Extended APDU support.
|
||||
### > Key generation and encrypted storage
|
||||
Private and secret keys are secured using a master AES 256 key (MKEK). The MKEK is encrypted with a hashed and salted version of the PIN.
|
||||
**No private/secret keys, DKEK or PIN are stored in plain text ever. Never.**
|
||||
|
||||
### > RSA Key Generation (1024 to 4096 Bits)
|
||||
RSA key generation is supported for 1024, 2048, 3072, and 4096 bits. Private keys never leave the device.
|
||||
|
||||
### > ECDSA Key Generation (192 to 521 Bits)
|
||||
ECDSA key generation supports various curves from 192 to 521 bits.
|
||||
|
||||
### > ECC Curves
|
||||
Supported ECC curves include secp192r1, secp256r1, secp384r1, secp521r1, brainpoolP256r1, brainpoolP384r1, brainpoolP512r1, secp192k1 (insecure), secp256k1, Curve25519, and Curve448.
|
||||
|
||||
### > SHA Digests
|
||||
ECDSA and RSA signatures can be combined with SHA-1, SHA-224, SHA-256, SHA-384, and SHA-512 digests.
|
||||
|
||||
### > Multiple RSA Signature Algorithms
|
||||
Supported RSA signature algorithms include RSA-PSS, RSA-PKCS, and raw RSA signatures.
|
||||
|
||||
### > ECDSA Signatures
|
||||
ECDSA signatures can be raw or pre-hashed.
|
||||
|
||||
### > ECDH Key Derivation
|
||||
Supports the ECDH algorithm for calculating shared secrets.
|
||||
|
||||
### > EC Private Key Derivation
|
||||
Allows ECDSA key derivation.
|
||||
|
||||
### > RSA Decryption
|
||||
Supports RSA-OEP and RSA-X.509 decryption.
|
||||
|
||||
### > AES Key Generation
|
||||
Supports AES key generation with keys of 128, 192, and 256 bits.
|
||||
|
||||
### > AES-CBC Encryption/Decryption
|
||||
Performs AES-CBC encryption and decryption.
|
||||
|
||||
### > Advanced AES Modes
|
||||
Supports AES encryption and decryption in ECB, CBC, CFB, OFB, XTS, CTR, GCM, and CCM modes, with customizable IV/nonce and additional authenticated data (AAD).[^4]
|
||||
|
||||
### > AES Key Generation (128, 192, 256, 512 Bits)
|
||||
Supports AES key generation up to 512 bits, useful for AES XTS where two 256-bit keys are concatenated.
|
||||
|
||||
### > CMAC
|
||||
Supports AES-CMAC authentication.[^1]
|
||||
|
||||
### > AES Secret Key Derivation
|
||||
Supports AES secret key derivation.[^1]
|
||||
|
||||
### > PIN Authorization
|
||||
Private and secret keys require prior PIN authentication. Supports alphanumeric PINs.
|
||||
|
||||
### > PKCS11 Compliant Interface
|
||||
Interfacing with the PKCS11 standard is supported.
|
||||
|
||||
### > Hardware Random Number Generator (HRNG)
|
||||
Contains an HRNG designed for maximum entropy.
|
||||
|
||||
### > Device Key Encryption Key (DKEK) Shares
|
||||
Supports importing DKEK shares to wrap, unwrap, and encrypt keys.
|
||||
|
||||
### > DKEK n-of-m Threshold Scheme
|
||||
Supports an n-of-m threshold scheme to prevent outages when a DKEK custodian is unavailable.
|
||||
|
||||
### > USB/CCID Support
|
||||
Full USB CCID stack for communication with the host via OpenSC and PCSC, allowing the use of frontend applications like OpenSSL via the PKCS11 module.
|
||||
|
||||
### > Extended APDU Support
|
||||
Supports extended APDU packets, allowing up to 65535 bytes.
|
||||
|
||||
### > CV Certificates
|
||||
Handles CVC certificates and requests to minimize internal certificate storage.
|
||||
|
||||
### > Attestation
|
||||
Each generated key is attached to a certificate signed by an external PKI, ensuring the key was generated by the specific device.
|
||||
|
||||
### > Import External Keys and Certificates
|
||||
Allows importing private keys and certificates via WKY or PKCS#12 files.[^2][^3]
|
||||
|
||||
### > Transport PIN
|
||||
Allows a transport PIN for provisioning, ensuring the device has not been tampered with during transportation.[^2]
|
||||
|
||||
### > Press-to-Confirm Button
|
||||
Uses the BOOTSEL button to confirm operations with private/secret keys, providing a 15-second window to confirm the operation to protect against unauthorized use.
|
||||
|
||||
### > Store and Retrieve Binary Data
|
||||
Allows the storage of arbitrary binary data files.
|
||||
|
||||
### > Real-Time Clock (RTC)
|
||||
Includes an RTC with external date and time setting and retrieval.
|
||||
|
||||
### > Secure Messaging
|
||||
Supports secure channels to encrypt data packets between the host and device, preventing man-in-the-middle attacks.
|
||||
|
||||
### > Session PIN
|
||||
A specific session PIN can be set during session opening to avoid systematic PIN usage.
|
||||
|
||||
### > PKI CVCert Remote Issuing for Secure Messaging
|
||||
Secure channel messages are secured with a certificate issued by an external PKI.
|
||||
|
||||
### > Multiple Key Domains
|
||||
Supports separate key domains protected by independent DKEKs, allowing different keys in different domains.
|
||||
|
||||
### > Key Usage Counter
|
||||
Tracks and limits the usage of private/secret keys, disabling keys once their usage counter reaches zero.
|
||||
|
||||
### > Public Key Authentication (PKA)
|
||||
Supports PKA for enhanced security, requiring a secondary device for authentication using a challenge-response mechanism.
|
||||
|
||||
### > Secure Lock
|
||||
Adds an extra layer of security by locking the Pico HSM to a specific computer using a private key.
|
||||
|
||||
### > ChaCha20-Poly1305
|
||||
Supports the ChaCha20-Poly1305 encryption algorithm for secure data encryption.[^4]
|
||||
|
||||
### > X25519 and X448
|
||||
Supports DH X25519 and X448 for key agreement, though these cannot be used for signing.
|
||||
|
||||
### > Key Derivation Functions
|
||||
Supports HKDF, PBKDF2, and X963-KDF for symmetric key derivation.
|
||||
|
||||
### > HMAC
|
||||
Supports HMAC generation with SHA digest algorithms.
|
||||
|
||||
### > CMAC
|
||||
Supports CMAC with AES for keys of 128, 192, and 256 bits.
|
||||
|
||||
### > XKEK
|
||||
Supports an advanced key sharing scheme (XKEK) for securely wrapping and unwrapping keys within authorized domains.
|
||||
|
||||
### > Master Key Encryption Key (MKEK)
|
||||
Uses an MKEK to securely store all keys, encrypted with an ephemeral key derived from the hashed PIN.
|
||||
|
||||
### > Hierarchical Deterministic Key Generation
|
||||
Supports BIP32 for asymmetric key derivation and SLIP10 for symmetric key derivation, enabling crypto wallet deployment with infinite key generation. Supports NIST 256 and Koblitz 256 curves for master key generation.[^4]
|
||||
|
||||
### > One Time Programming (OTP) Storage
|
||||
The OTP securely stores the MKEK (Master Key Encryption Key) and Device Key permanently, making it inaccessible from external interfaces. This ensures that the key is protected against unauthorized access and tampering.
|
||||
|
||||
### > Secure Boot
|
||||
Secure Boot ensures only authenticated firmware can run on the device, verifying each firmware’s digital signature to block unauthorized code.
|
||||
|
||||
### > Secure Lock
|
||||
Secure Lock restricts the device to the manufacturer’s firmware only, locking out debug access and preventing any further boot key installations.
|
||||
|
||||
### > Rescue Interface
|
||||
A built-in rescue interface allows recovery of the device if it becomes unresponsive or undetectable. This feature provides a way to restore the device to operational status without compromising security.
|
||||
|
||||
### > LED Customization
|
||||
The LED can be customized to reflect device status and user preferences, offering flexible color and brightness options for an enhanced user experience.
|
||||
|
||||
[^1]: PKCS11 modules (`pkcs11-tool` and `sc-tool`) do not support CMAC and key derivation. It must be processed through raw APDU command (`opensc-tool -s`).
|
||||
[^2]: Available via SCS3 tool. See [SCS3](/doc/scs3.md "SCS3") for more information.
|
||||
[^3]: Imports are available only if the Pico HSM is previously initialized with a DKEK and DKEK shares are available during the import process.
|
||||
[^4]: Available by using PicoHSM python tool.
|
||||
|
||||
### > ESP32-S3 support
|
||||
Pico HSM also supports ESP32-S3 boards, which add secure storage, flash encryption and secure boot.
|
||||
|
||||
### > Dynamic VID/PID
|
||||
Supports setting VID & PID on-the-fly. U
|
||||
|
||||
### > Rescue Pico HSM
|
||||
Pico HSM Tool implements a new CCID stack to rescue the Pico HSM in case it has wrong VID/PID values and it is not recognized by the OS.
|
||||
|
||||
## Security considerations
|
||||
All secret keys (asymmetric and symmetric) are stored encrypted in the flash memory of the Raspberry Pico. DKEK is used as a 256 bit AES key to protect private and secret keys. Keys are never stored in RAM except for signature and decryption operations. All keys (including DKEK) are loaded and cleared every time to avoid potential flaws.
|
||||
All secret keys (both asymmetric and symmetric) are encrypted and stored in the flash memory. The MKEK, a 256-bit AES key, is used to protect these private and secret keys. Keys are held in RAM only during signature and decryption operations, and are loaded and cleared each time to avoid potential security vulnerabilities.
|
||||
|
||||
At the same time, DKEK is encrypted with doubled salted and hashed PIN. Also, the PIN is hashed in memory during the session. Hence, PIN is never stored in plain text neither in flash nor in memory. Note that PIN is conveyed from the host to the HSM in plain text if no secure channel is provided.
|
||||
The MKEK itself is encrypted using a doubly salted and hashed PIN, and the PIN is hashed in memory during sessions. This ensures that the PIN is never stored in plaintext, neither in flash memory nor in RAM. However, if no secure channel is used, the PIN may be transmitted in plaintext from the host to the HSM.
|
||||
|
||||
If the Pico is stolen the contents of private and secret keys cannot be read without the PIN, even if the flash memory is dumped.
|
||||
DKEKs are used during the export and import of private/secret keys and are part of a Key Domain. A Key Domain is a set of secret/private keys that share the same DKEK. These are also shared by the custodians and are not specific to Pico HSM. Therefore, if a key does not belong to a Key Domain (and thus lacks a DKEK), it cannot be exported.
|
||||
|
||||
In the event that the Pico is stolen, the private and secret key contents cannot be accessed without the PIN, even if the flash memory is dumped.
|
||||
|
||||
### RP2350 and ESP32-S3
|
||||
RP2350 and ESP32-S3 microcontrollers are equipped with advanced security features, including Secure Boot and Secure Lock, ensuring that firmware integrity and authenticity are tightly controlled. Both devices support the storage of the Master Key Encryption Key (MKEK) in an OTP (One-Time Programmable) memory region, making it permanently inaccessible for external access or tampering. This secure, non-volatile region guarantees that critical security keys are embedded into the hardware, preventing unauthorized access and supporting robust defenses against code injection or firmware modification. Together, Secure Boot and Secure Lock enforce firmware authentication, while the MKEK in OTP memory solidifies the foundation for secure operations.
|
||||
|
||||
## Download
|
||||
Please, go to the Release page and download the UF2 file for your board.
|
||||
**If you own an ESP32-S3 board, go to [ESP32 Flasher](https://www.picokeys.com/esp32-flasher/) for flashing your Pico HSM.**
|
||||
|
||||
Note that UF2 files are shiped with a dummy VID/PID to avoid license issues (FEFF:FCFD). If you are planning to use it with OpenSC or similar, you should modify Info.plist of CCID driver to add these VID/PID or use the VID/PID patcher as follows:
|
||||
`./patch_vidpid.sh VID:PID input_hsm_file.uf2 output_hsm_file.uf2`
|
||||
If you own a Raspberry Pico (RP2040 or RP2350), go to [Download page](https://www.picokeys.com/getting-started/). If your board is mounted with the RP2040, then select Pico. If your board is mounted with the RP2350 or RP2354, select Pico2.
|
||||
|
||||
Note that UF2 files are shiped with a dummy VID/PID to avoid license issues (FEFF:FCFD). If you plan to use it with OpenSC or similar tools, you should modify Info.plist of CCID driver to add these VID/PID or use the [PicoKey App](https://www.picokeys.com/picokeyapp/ "PicoKey App").
|
||||
|
||||
You can use whatever VID/PID (i.e., 234b:0000 from FISJ), but remember that you are not authorized to distribute the binary with a VID/PID that you do not own.
|
||||
|
||||
## Build
|
||||
Note that the [PicoKey App](https://www.picokeys.com/picokeyapp/ "PicoKey App") is the most recommended.
|
||||
|
||||
## Build for Raspberry Pico
|
||||
Before building, ensure you have installed the toolchain for the Pico and the Pico SDK is properly located in your drive.
|
||||
|
||||
```
|
||||
git clone https://github.com/polhenarejos/pico-hsm
|
||||
git submodule update --init --recursive
|
||||
cd pico-hsm
|
||||
mkdir build
|
||||
cd build
|
||||
|
|
@ -48,50 +204,185 @@ make
|
|||
```
|
||||
Note that `PICO_BOARD`, `USB_VID` and `USB_PID` are optional. If not provided, `pico` board and VID/PID `FEFF:FCFD` will be used.
|
||||
|
||||
After `make` ends, the binary file `pico_hsm.uf2` will be generated. Put your pico board into loading mode, by pushing `BOOTSEL` button while pluging on, and copy the UF2 to the new fresh usb mass storage Pico device. Once copied, the pico mass storage will be disconnected automatically and the pico board will reset with the new firmware. A blinking led will indicate the device is ready to work.
|
||||
Additionally, you can pass the `VIDPID=value` parameter to build the firmware with a known VID/PID. The supported values are:
|
||||
|
||||
- `NitroHSM`
|
||||
- `NitroFIDO2`
|
||||
- `NitroStart`
|
||||
- `NitroPro`
|
||||
- `Nitro3`
|
||||
- `Yubikey5`
|
||||
- `YubikeyNeo`
|
||||
- `YubiHSM`
|
||||
- `Gnuk`
|
||||
- `GnuPG`
|
||||
|
||||
After running `make`, the binary file `pico_hsm.uf2` will be generated. To load this onto your Pico board:
|
||||
|
||||
1. Put the Pico board into loading mode by holding the `BOOTSEL` button while plugging it in.
|
||||
2. Copy the `pico_hsm.uf2` file to the new USB mass storage device that appears.
|
||||
3. Once the file is copied, the Pico mass storage device will automatically disconnect, and the Pico board will reset with the new firmware.
|
||||
4. A blinking LED will indicate that the device is ready to work.
|
||||
|
||||
### Docker
|
||||
Independent from your Linux distribution or when using another OS that supports Docker, you could build a specific pico-hsm version in a Linux container.
|
||||
|
||||
```
|
||||
sudo docker build \
|
||||
--build-arg VERSION_PICO_SDK=2.0.0 \
|
||||
--build-arg VERSION_MAJOR=5 \
|
||||
--build-arg VERSION_MINOR=0 \
|
||||
--build-arg PICO_BOARD=waveshare_rp2040_zero \
|
||||
--build-arg USB_VID=0xfeff \
|
||||
--build-arg USB_PID=0xfcfd \
|
||||
-t pico-hsm-builder .
|
||||
|
||||
sudo docker run \
|
||||
--name mybuild \
|
||||
-it pico-hsm-builder \
|
||||
ls -l /home/builduser/pico-hsm/build_release/pico_hsm.uf2
|
||||
|
||||
sudo docker cp mybuild:/home/builduser/pico-hsm/build_release/pico_hsm.uf2 .
|
||||
|
||||
sudo docker rm mybuild
|
||||
```
|
||||
|
||||
## Usage
|
||||
The firmware uploaded to the Pico contains a reader and a virtual smart card. It is like having a physical reader with an inserted SIM card.
|
||||
We recommend the use of [OpenSC](http://github.com/opensc/opensc/ "OpenSC") to communicate with the reader. If it is not installed, you can download and build it or install the binaries for your system. The first command is to ensure that the Pico is detected as a HSM:
|
||||
```
|
||||
The firmware uploaded to the Pico contains a reader and a virtual smart card, similar to having a physical reader with an inserted SIM card. We recommend using [OpenSC](http://github.com/opensc/opensc/ "OpenSC") to communicate with the reader. If OpenSC is not installed, you can download and build it or install the binaries for your system.
|
||||
|
||||
To ensure that the Pico is detected as an HSM, use the following command:
|
||||
```sh
|
||||
opensc-tool -an
|
||||
````
|
||||
It should return a text like the following:
|
||||
```
|
||||
It should return a text similar to:
|
||||
```sh
|
||||
Using reader with a card: Free Software Initiative of Japan Gnuk
|
||||
3b:fe:18:00:00:81:31:fe:45:80:31:81:54:48:53:4d:31:73:80:21:40:81:07:fa
|
||||
SmartCard-HSM
|
||||
```
|
||||
The name of the reader may vary if you modified the VID/PID.
|
||||
|
||||
For initialization and asymmetric operations, check [doc/usage.md](/doc/usage.md).
|
||||
For further details and operations, refer to the following documentation:
|
||||
|
||||
For signing and verification operations, check [doc/sign-verify.md](/doc/sign-verify.md).
|
||||
- Initialization and Asymmetric Operations [doc/usage.md](/doc/usage.md)
|
||||
- Signing and Verification Operations [doc/sign-verify.md](/doc/sign-verify.md)
|
||||
- Asymmetric Encryption and Decryption [doc/asymmetric-ciphering.md](/doc/asymmetric-ciphering.md)
|
||||
- Backup, Restore, and DKEK Share Management [doc/backup-and-restore.md](/doc/backup-and-restore.md)
|
||||
- AES Key Generation, Encryption, and Decryption [doc/aes.md](/doc/aes.md)
|
||||
- 4096 Bits RSA Support [doc/scs3.md](/doc/scs3.md)
|
||||
- Storing and Retrieving Arbitrary Data [doc/store_data.md](/doc/store_data.md)
|
||||
- Extra Options (e.g., set/get real datetime, enable/disable press-to-confirm button [doc/extra_command.md](/doc/extra_command.md)
|
||||
- Public Key Authentication [doc/public_key_authentication.md](/doc/public_key_authentication.md)
|
||||
|
||||
For asymmetric encryption and decryption, check [doc/asymmetric-ciphering.md](/doc/asymmetric-ciphering.md).
|
||||
## Operation time
|
||||
### Keypair generation
|
||||
Generating EC keys is almost instant. RSA keypair generation takes some time, specially for `3072` and `4096` bits.
|
||||
|
||||
For backup, restore and DKEK share management, check [doc/backup-and-restore.md](/doc/backup-and-restore.md).
|
||||
| RSA key length (bits) | Average time (seconds) |
|
||||
| :---: | :---: |
|
||||
| 1024 | 16 |
|
||||
| 2048 | 124 |
|
||||
| 3072 | 600 |
|
||||
| 4096 | ~1000 |
|
||||
|
||||
For AES key generation, encryption and decryption, check [doc/aes.md](/doc/aes.md).
|
||||
### Signature and decrypt
|
||||
| RSA key length (bits) | Average time (seconds) |
|
||||
| :---: | :---: |
|
||||
| 1024 | 1 |
|
||||
| 2048 | 3 |
|
||||
| 3072 | 7 |
|
||||
| 4096 | 15 |
|
||||
|
||||
## Press-to-confirm button
|
||||
The Raspberry Pico includes a BOOTSEL button used for loading firmware initially. Once the Pico HSM firmware is running, this button can be repurposed for additional functionalities. Specifically, the Pico HSM utilizes this button to confirm private and secret operations, a feature that is optional but highly recommended for enhanced security.
|
||||
|
||||
When enabled, each time a private or secret key operation is initiated, the Pico HSM enters a waiting state where it awaits user confirmation by pressing the BOOTSEL button. During this waiting period, the Pico HSM's LED remains mostly illuminated but blinks off briefly every second, signaling to the user to press the button for confirmation. If no action is taken, the Pico HSM will continue to wait indefinitely. This operation mode includes periodic timeout commands sent to the host to prevent the session from timing out prematurely.
|
||||
|
||||
This feature adds an additional layer of security by requiring physical user intervention for sensitive operations such as signing or decrypting data. It mitigates risks associated with unauthorized applications or scripts using the Pico HSM without user awareness. However, it is not recommended for server environments or other automated settings where physical access to press the button may not be practical.
|
||||
|
||||
For more details on configuring and using this feature, refer to the [doc/extra_command.md](/doc/extra_command.md) document.
|
||||
|
||||
## Led blink
|
||||
Pico HSM uses the led to indicate the current status. Four states are available:
|
||||
|
||||
### Press to confirm
|
||||
The Led is almost on all the time. It goes off for 100 miliseconds every second.
|
||||
|
||||

|
||||
|
||||
### Idle mode
|
||||
In idle mode, the Pico HSM goes to sleep. It waits for a command and it is awaken by the driver. The Led is almost off all the time. It goes on for 500 milliseconds every second.
|
||||
|
||||

|
||||
|
||||
### Active mode
|
||||
In active mode, the Pico HSM is awaken and ready to receive a command. It blinks four times in a second.
|
||||
|
||||

|
||||
|
||||
### Processing
|
||||
While processing, the Pico HSM is busy and cannot receive additional commands until the current is processed. In this state, the Led blinks 20 times in a second.
|
||||
|
||||

|
||||
|
||||
## Driver
|
||||
|
||||
Pico HSM uses the `sc-hsm` driver provided by [OpenSC](https://github.com/OpenSC/OpenSC/ "OpenSC") or the `sc-hsm-embedded` driver provided by [CardContact](https://github.com/CardContact/sc-hsm-embedded "CardContact"). This driver utilizes the standardized PKCS#11 interface to communicate with the user and it can be used with many engines that accept PKCS#11 interface, such as OpenSSL, P11 library or pkcs11-tool.
|
||||
The Pico HSM uses either the `sc-hsm` driver from [OpenSC](https://github.com/OpenSC/OpenSC/) or the `sc-hsm-embedded` driver from [CardContact](https://github.com/CardContact/sc-hsm-embedded/) to interface with external applications. These drivers employ the standardized PKCS#11 interface, making it compatible with various cryptographic engines that support PKCS#11, such as OpenSSL, P11 library, or pkcs11-tool.
|
||||
|
||||
Pico HSM relies on PKCS#15 structure to store and manipulate the internal files (PINs, private keys, certificates, etc.) and directories. Therefore, it accepts the commands from `pkcs15-tool`. For instance, `pkcs15-tool -D` will list all elements stored in the Pico HSM.
|
||||
Internally, the Pico HSM organizes and manages its data using the PKCS#15 structure, which includes elements like PINs, private keys, and certificates. Commands can be issued to interact with these stored elements using tools such as `pkcs15-tool`. For example, `pkcs15-tool -D` lists all elements stored within the Pico HSM.
|
||||
|
||||
The way to communicate is exactly the same as with other cards, such as OpenPGP or similar.
|
||||
Communication with the Pico HSM follows the same protocols and methods used with other smart cards, such as OpenPGP cards or similar devices.
|
||||
|
||||
For an advanced usage, see the docs and examples.
|
||||
For advanced usage scenarios, refer to the documentation and examples provided. Additionally, the Pico HSM supports the SCS3 tool for more sophisticated operations and includes features like multiple key domains. For detailed information on SCS3 usage, refer to [SCS3 documentation](/doc/scs3.md).
|
||||
|
||||
### Important
|
||||
OpenSC relies on PCSC driver, which reads a list (`Info.plist`) that contains a pair of VID/PID of supported readers. In order to be detectable, you must patch the UF2 binary (if you just downloaded from the [Release section](https://github.com/polhenarejos/pico-hsm/releases "Release section")) or configure the project with the proper VID/PID with `USB_VID` and `USB_PID` parameters in `CMake` (see [Build section](#build "Build section")). Note that you cannot distribute the patched/compiled binary if you do not own the VID/PID.
|
||||
## License and Commercial Use
|
||||
|
||||
This project is available under two editions:
|
||||
|
||||
**Community Edition (FOSS)**
|
||||
- Released under the GNU Affero General Public License v3 (AGPLv3).
|
||||
- You are free to study, modify, and run the code, including for internal evaluation.
|
||||
- If you distribute modified binaries/firmware, OR if you run a modified version of this project as a network-accessible service, you must provide the corresponding source code to the users of that binary or service, as required by AGPLv3.
|
||||
- No warranty. No SLA. No guaranteed support.
|
||||
|
||||
**Enterprise / Commercial Edition**
|
||||
- Proprietary license for organizations that want to:
|
||||
- run this in production with multiple users/devices,
|
||||
- integrate it into their own product/appliance,
|
||||
- enforce corporate policies (PIN policy, admin/user roles, revocation),
|
||||
- deploy it as an internal virtualized / cloud-style service,
|
||||
- and *not* be required to publish derivative source code.
|
||||
- Base package includes:
|
||||
- commercial license (no AGPLv3 disclosure obligation for your modifications / integration)
|
||||
- onboarding call
|
||||
- access to officially signed builds
|
||||
- Optional / on-demand enterprise components that can be added case-by-case:
|
||||
- ability to operate in multi-user / multi-device environments
|
||||
- device inventory, traceability and secure revocation/offboarding
|
||||
- custom attestation, per-organization device identity / anti-cloning
|
||||
- virtualization / internal "HSM or auth backend" service for multiple teams or tenants
|
||||
- post-quantum (PQC) key material handling and secure PQC credential storage
|
||||
- hierarchical deterministic key derivation (HD wallet–style key trees for per-user / per-tenant keys, firmware signing trees, etc.)
|
||||
- cryptographically signed audit trail / tamper-evident logging
|
||||
- dual-control / two-person approval for high-risk operations
|
||||
- secure key escrow / disaster recovery strategy
|
||||
- release-signing / supply-chain hardening toolchain
|
||||
- policy-locked hardened mode ("FIPS-style profile")
|
||||
- priority security-response SLA
|
||||
- white-label demo / pre-sales bundle
|
||||
|
||||
Typical licensing models:
|
||||
- Internal use (single legal entity, including internal private cloud / virtualized deployments).
|
||||
- OEM / Redistribution / Service (ship in your product OR offer it as a service to third parties).
|
||||
|
||||
These options are scoped and priced individually depending on which components you actually need.
|
||||
|
||||
For commercial licensing and enterprise features, email pol@henarejos.me
|
||||
Subject: `ENTERPRISE LICENSE <your company name>`
|
||||
|
||||
See `ENTERPRISE.md` for details.
|
||||
|
||||
## Credits
|
||||
Pico HSM uses the following libraries or portion of code:
|
||||
- OpenSC for ASN1 manipulation.
|
||||
- mbedTLS for cryptographic operations.
|
||||
- gnuk for low level CCID procedures and OpenPGP support.
|
||||
- TinyUSB for low level USB procedures.
|
||||
|
||||
In the case of gnuk, it is intended to work with STM32 processor and its family. Part of the code of CCID procedures are ported and adapted to run with Pico.
|
||||
|
|
|
|||
25
build_pico_hsm.sh
Executable file
25
build_pico_hsm.sh
Executable file
|
|
@ -0,0 +1,25 @@
|
|||
#!/bin/bash
|
||||
|
||||
VERSION_MAJOR="6"
|
||||
VERSION_MINOR="2"
|
||||
SUFFIX="${VERSION_MAJOR}.${VERSION_MINOR}"
|
||||
#if ! [[ -z "${GITHUB_SHA}" ]]; then
|
||||
# SUFFIX="${SUFFIX}.${GITHUB_SHA}"
|
||||
#fi
|
||||
|
||||
mkdir -p build_release
|
||||
mkdir -p release
|
||||
rm -rf -- release/*
|
||||
cd build_release
|
||||
|
||||
PICO_SDK_PATH="${PICO_SDK_PATH:-../../pico-sdk}"
|
||||
SECURE_BOOT_PKEY="${SECURE_BOOT_PKEY:-../../ec_private_key.pem}"
|
||||
boards=("pico" "pico2")
|
||||
|
||||
for board_name in "${boards[@]}"
|
||||
do
|
||||
rm -rf -- ./*
|
||||
PICO_SDK_PATH="${PICO_SDK_PATH}" cmake .. -DPICO_BOARD=$board_name -DSECURE_BOOT_PKEY=${SECURE_BOOT_PKEY}
|
||||
make -j`nproc`
|
||||
mv pico_hsm.uf2 ../release/pico_hsm_$board_name-$SUFFIX.uf2
|
||||
done
|
||||
|
|
@ -40,7 +40,7 @@ Once a secret AES key is generated, a content can be encrypted and decrypted sym
|
|||
|
||||
```
|
||||
$ echo "This is a text." | sc-tool -l --pin 648219 --encrypt --id 12 --mechanism aes-cbc > crypted.aes
|
||||
````
|
||||
```
|
||||
|
||||
The file `crypted.aes` contains the ciphered string with the AES key generated previously.
|
||||
|
||||
|
|
@ -54,5 +54,4 @@ Using decrypt algorithm AES-CBC
|
|||
This is a text.
|
||||
```
|
||||
|
||||
AES-CBC it is a block operation and it requires an input size multiple of 16 bytes. Thus, for a trivial data, a padding operation has to be performed beforehand.
|
||||
|
||||
AES-CBC it is a block operation and it requires an input size multiple of 16 bytes. Thus, for a trivial data, a padding operation has to be performed beforehand.
|
||||
|
|
|
|||
|
|
@ -24,12 +24,12 @@ This algorithm uses the PKCSv1.5 padding. It is considered deprecated and insecu
|
|||
First, we encrypt the data with the public key:
|
||||
|
||||
```
|
||||
$ openssl rsautl -encrypt -inkey 1.pub -in data -pubin -out data.crypt
|
||||
$ openssl rsautl -encrypt -inkey 1.pub -in data -pubin -out data.crypt
|
||||
```
|
||||
|
||||
Then, we decrypt with the private key inside the Pico HSM:
|
||||
|
||||
```
|
||||
```
|
||||
$ pkcs11-tool --id 1 --pin 648219 --decrypt --mechanism RSA-PKCS -i data.crypt
|
||||
Using slot 0 with a present token (0x0)
|
||||
Using decrypt algorithm RSA-PKCS
|
||||
|
|
@ -54,7 +54,7 @@ $ openssl rsautl -encrypt -inkey 1.pub -in data_pad -pubin -out data.crypt -raw
|
|||
|
||||
Then, we decrypt with the private key inside the Pico HSM:
|
||||
```
|
||||
$ cat data.crypt|pkcs11-tool --id 4 --pin 648219 --decrypt --mechanism RSA-X-509
|
||||
$ cat data.crypt|pkcs11-tool --id 4 --pin 648219 --decrypt --mechanism RSA-X-509
|
||||
Using slot 0 with a present token (0x0)
|
||||
Using decrypt algorithm RSA-X-509
|
||||
This is a test string. Be safe, be secure.
|
||||
|
|
@ -78,7 +78,7 @@ This is a test string. Be safe, be secure.
|
|||
```
|
||||
|
||||
## ECDH-DERIVE
|
||||
ECC keys do not allow ciphering operations. Instead, the ECDH scheme provides a mechanism to exchange a shared symmetric key without transmitting it to the remote part. The shared key is composed by multiplying the local private key and the remote public key.
|
||||
ECC keys do not allow ciphering operations. Instead, the ECDH scheme provides a mechanism to exchange a shared symmetric key without transmitting it to the remote part. The shared key is composed by multiplying the local private key and the remote public key.
|
||||
|
||||
First, we create the remote part, Bob, by generating an ECC keypair and getting the public key:
|
||||
```
|
||||
|
|
@ -104,8 +104,8 @@ No output is displayed if both are equal.
|
|||
|
||||
You can also view the contents of both keys:
|
||||
```
|
||||
$ xxd -p bob-mine.der
|
||||
$ xxd -p bob-mine.der
|
||||
9874558aefa9d92cc051e5da6d1753987e5314925d6d78bf
|
||||
$ xxd -p mine-bob.der
|
||||
$ xxd -p mine-bob.der
|
||||
9874558aefa9d92cc051e5da6d1753987e5314925d6d78bf
|
||||
```
|
||||
|
|
|
|||
|
|
@ -29,15 +29,15 @@ symbols.
|
|||
Please keep the generated DKEK share file in a safe location. We also recommend to keep a
|
||||
paper printout, in case the electronic version becomes unavailable. A printable version
|
||||
of the file can be generated using "openssl base64 -in <filename>".
|
||||
Enter password to encrypt DKEK share :
|
||||
Enter password to encrypt DKEK share :
|
||||
|
||||
Please retype password to confirm :
|
||||
Please retype password to confirm :
|
||||
|
||||
Enciphering DKEK share, please wait...
|
||||
DKEK share created and saved to dkek.pbe
|
||||
```
|
||||
|
||||
The generated file `dkek.pbe` contains the DKEK. Technically, it contains a share. But if a device is initialized with one share, it is equivalent to contain the full DKEK.
|
||||
The generated file `dkek.pbe` contains the DKEK. Technically, it contains a share. But if a device is initialized with one share, it is equivalent to contain the full DKEK.
|
||||
|
||||
Keep these file in a safe place. If this file is lost, you can export the private keys but you will not be able to import into another device or in the same device if it is initialized again.
|
||||
|
||||
|
|
@ -52,7 +52,7 @@ At this moment, the Pico HSM expects the DKEK. It is loaded with the following c
|
|||
```
|
||||
$ sc-hsm-tool --import-dkek-share dkek.pbe
|
||||
Using reader with a card: Free Software Initiative of Japan Gnuk
|
||||
Enter password to decrypt DKEK share :
|
||||
Enter password to decrypt DKEK share :
|
||||
|
||||
Deciphering DKEK share, please wait...
|
||||
DKEK share imported
|
||||
|
|
@ -81,7 +81,7 @@ And finally, all are imported one after the other, without special order:
|
|||
```
|
||||
$ sc-hsm-tool --import-dkek-share dkek-share-1.pbe
|
||||
Using reader with a card: Free Software Initiative of Japan Gnuk
|
||||
Enter password to decrypt DKEK share :
|
||||
Enter password to decrypt DKEK share :
|
||||
|
||||
Deciphering DKEK share, please wait...
|
||||
DKEK share imported
|
||||
|
|
@ -90,7 +90,7 @@ DKEK import pending, 2 share(s) still missing
|
|||
|
||||
$ sc-hsm-tool --import-dkek-share dkek-share-2.pbe
|
||||
Using reader with a card: Free Software Initiative of Japan Gnuk
|
||||
Enter password to decrypt DKEK share :
|
||||
Enter password to decrypt DKEK share :
|
||||
|
||||
Deciphering DKEK share, please wait...
|
||||
DKEK share imported
|
||||
|
|
@ -99,7 +99,7 @@ DKEK import pending, 1 share(s) still missing
|
|||
|
||||
$ sc-hsm-tool --import-dkek-share dkek-share-1.pbe
|
||||
Using reader with a card: Free Software Initiative of Japan Gnuk
|
||||
Enter password to decrypt DKEK share :
|
||||
Enter password to decrypt DKEK share :
|
||||
|
||||
Deciphering DKEK share, please wait...
|
||||
DKEK share imported
|
||||
|
|
@ -110,7 +110,7 @@ DKEK key check value : 4B7DA256ACD4EF62
|
|||
### DKEK n-of-m threshold scheme
|
||||
This scheme provides an extra level of flexiblity, as not all custodians are necessary to import the DKEK share. For instance, with the previous schemes, if a custodian gets unavailable, the initialization will block until the missing custodian can got to finalize the initialization.
|
||||
|
||||
With n-of-m threshold scheme, it flexibilizes the number of required custodians to reduce failure points. If a share is lost, the DKEK can still be recovered without major implications.
|
||||
With n-of-m threshold scheme, it flexibilizes the number of required custodians to reduce failure points. If a share is lost, the DKEK can still be recovered without major implications.
|
||||
|
||||
This scheme is not a replacement of DKEK shares. Instead, it splits the DKEK share encryption password amongst the n-of-m threshold scheme. For instance, if you define 2 shares and a scheme of 3-of-5 threshold for each share, it will imply 10 different custodians, where 6 are necessary to load both shares. You can also mix one share with traditional passphrase and the other with the n-of-m threshold scheme.
|
||||
|
||||
|
|
@ -123,7 +123,7 @@ Using reader with a card:Free Software Initiative of Japan Gnuk
|
|||
The DKEK will be enciphered using a randomly generated 64 bit password.
|
||||
This password is split using a (3-of-5) threshold scheme.
|
||||
|
||||
Please keep the generated and encrypted DKEK file in a safe location. We also recommend
|
||||
Please keep the generated and encrypted DKEK file in a safe location. We also recommend
|
||||
to keep a paper printout, in case the electronic version becomes unavailable. A printable version
|
||||
of the file can be generated using "openssl base64 -in <filename>".
|
||||
|
||||
|
|
@ -191,7 +191,7 @@ Private RSA Key [Certificate]
|
|||
...
|
||||
```
|
||||
|
||||
Note that `Key ref` and `ID` may be different. Whilst different keys may share the same `ID` (highly discouraged), the `Key ref` is a value internally computed and unique.
|
||||
Note that `Key ref` and `ID` may be different. Whilst different keys may share the same `ID` (highly discouraged), the `Key ref` is a value internally computed and unique.
|
||||
|
||||
To export and wrap the private key:
|
||||
|
||||
|
|
@ -199,10 +199,10 @@ To export and wrap the private key:
|
|||
$ sc-hsm-tool --wrap-key wrap-key.bin --key-reference 1 --pin 648219
|
||||
```
|
||||
|
||||
A file named `wrap-key.bin` is created with the private key encrypted securely with the DKEK.
|
||||
A file named `wrap-key.bin` is created with the private key encrypted securely with the DKEK.
|
||||
|
||||
## Restore
|
||||
To restore the wraped key, a device initialized with the same DKEK is mandatory.
|
||||
To restore the wraped key, a device initialized with the same DKEK is mandatory.
|
||||
|
||||
To unwrap the key:
|
||||
|
||||
|
|
|
|||
115
doc/extra_command.md
Normal file
115
doc/extra_command.md
Normal file
|
|
@ -0,0 +1,115 @@
|
|||
# Extra command
|
||||
|
||||
Pico HSM supports a customized extra command to use with different options. Since the drivers in the market do not support the following features, a raw APDU command shall be sent.
|
||||
|
||||
To send a raw APDU command, `opensc-tool -s <APDU>` can be used. The `APDU` parameter is a string of hexadecimal numbers and it takes the following form:
|
||||
```
|
||||
8064XX00YYZZZZRR
|
||||
```
|
||||
|
||||
It composed by the following fields:
|
||||
- `80` to indicate that it is a custom vendor type command.
|
||||
- `64` is the `INS` custom command.
|
||||
- `XX` is the command to execute. It varies depending on the targeted command.
|
||||
- `00` is the parameter of the command. At this moment, no commands support parameters.
|
||||
- `YY` is the length of the data. If no data is provided, this field is absent.
|
||||
- `ZZZZ` is the data to be sent. Optional. The length is variable.
|
||||
- `RR` is the length of the expected response. If no response is expected, this field is absent.
|
||||
|
||||
## Real time clock and datetime
|
||||
Pico HSM has an internal real time clock (RTC) which can track precisely the date and the time. However, when it is reset or powered down, the Pico HSM is reset to the initial datetime: 2020 January 1, 00:00:00.
|
||||
|
||||
### Getting the datetime
|
||||
To obtain the current datetime (referenced to 00:00:00 2020/01/01), the `XX` parameter must be set to `0A`. There is no data and, thus, `YY` and `ZZZZ` are absent. The expected response is 8 bytes length.
|
||||
|
||||
For example, to obtain the current datetime:
|
||||
|
||||
```
|
||||
$ opensc-tool -s 80640A0008
|
||||
Using reader with a card: Free Software Initiative of Japan Gnuk
|
||||
Sending: 80 64 0A 00 08
|
||||
Received (SW1=0x90, SW2=0x00):
|
||||
07 E6 04 06 03 13 29 1E ......).
|
||||
```
|
||||
|
||||
The response is composed by 8 bytes:
|
||||
- The first two bytes are the current year, MSB first. Hence, `07E6h` equals to `2022`.
|
||||
- 1 byte for the current month, `01h` is January and `0Ch` is December.
|
||||
- 1 byte for the current day, from `01h` (1) to `1Fh` (31).
|
||||
- 1 byte for the day of the week, `00h` is Sunday, `01h` is Monday, etc.
|
||||
- 1 byte for the hours, from `00h` (0) to `17h` (23).
|
||||
- 1 byte for the minutes, from `00h` (0) to `3Bh` (59).
|
||||
- 1 byte for the seconds, from `00h` (0) to `3Bh` (59).
|
||||
|
||||
If the command is correctly received, `SW1=0x90` and `SW2=0x00`. Other values mean that an error has ocurred.
|
||||
|
||||
### Setting the datetime
|
||||
To set the reference datetime, a datetime string must be provided. For example:
|
||||
|
||||
```
|
||||
$ opensc-tool -s 80640A000807E6040603132917
|
||||
Using reader with a card: Free Software Initiative of Japan Gnuk
|
||||
Sending: 80 64 0A 00 08 07 E6 04 06 03 13 29 17
|
||||
Received (SW1=0x90, SW2=0x00)
|
||||
```
|
||||
|
||||
will set the reference datetime to `Wednesday, 2022 April 6th, 19:41:23`.
|
||||
|
||||
## Dynamic options
|
||||
Pico HSM support initialize options, such as setting Transport PIN or reset retry counter options. However, once it is initialized, these options cannot be modified anymore, without a new initialization (loosing all stored keys). Pico HSM offers the chance to define a set of dynamic options that can be enabled/disabled dynamically without initializing the device at every moment.
|
||||
|
||||
To specify a set of options, the `XX` parameter shall be set to `06`. The data parameter shall be 1 byte, where the options are combined with the or operand `|`. The length `YY` shall be set to `01`.
|
||||
|
||||
Available options (counting from LSB):
|
||||
- Bit `0`: enable/disable press-to-confirm button.
|
||||
- Bit `1`: enable/disable key usage counter for all keys.
|
||||
|
||||
### Press-to-confirm button
|
||||
Press-to-confirm button offers an extra security layer by requiring the user confirmation everytime that a private/secret key is loaded. This avoids ghost applications thay may perform hidden opperations without noticing the user, such as signing or decrypting. Pico HSM will inform the user that is awaiting for a confirmation by making almost a fixed Led blink.
|
||||
|
||||
This feature is disabled by default but can be enabled rapidly by setting the LSB bit to 1:
|
||||
|
||||
```
|
||||
$ opensc-tool -s 806406000101
|
||||
Using reader with a card: Free Software Initiative of Japan Gnuk
|
||||
Sending: 80 64 06 00 01 01
|
||||
Received (SW1=0x90, SW2=0x00)
|
||||
```
|
||||
|
||||
At this moment, when a private/secret key is loaded, the Pico HSM will wait for the pressed BOOTSEL button to confirm the operation.
|
||||
|
||||
To disable, the LSB bit must be set to 0:
|
||||
|
||||
```
|
||||
$ opensc-tool -s 806406000100
|
||||
Using reader with a card: Free Software Initiative of Japan Gnuk
|
||||
Sending: 80 64 06 00 01 00
|
||||
Received (SW1=0x90, SW2=0x00)
|
||||
```
|
||||
|
||||
### Key usage counter by default
|
||||
Pico HSM supports a key usage counter to audit the usage of a particular key. For every operation with the key, the counter is reduced by 1. When it reaches 0, the key is disabled and cannot be used.
|
||||
|
||||
This option is disabled by default. When enabled, each generated key in the device is attached to a counter, starting at `2^32-1` (`FFFFFFFEh`). Therefore, it allows to count how many times a key is used for signing or decryption.
|
||||
|
||||
The counter can be viewed by using the SCS3 tool. More info at [doc/scs3.md](/doc/scs3.md).
|
||||
|
||||
This feature is disabled by default but can be enabled rapidly by setting the 2nd LSB bit to 1:
|
||||
|
||||
```
|
||||
$ opensc-tool -s 806406000102
|
||||
Using reader with a card: Free Software Initiative of Japan Gnuk
|
||||
Sending: 80 64 06 00 01 01
|
||||
Received (SW1=0x90, SW2=0x00)
|
||||
```
|
||||
|
||||
At this moment, when a private/secret key is loaded, the Pico HSM will wait for the pressed BOOTSEL button to confirm the operation.
|
||||
|
||||
To disable, the LSB bit must be set to 0:
|
||||
|
||||
```
|
||||
$ opensc-tool -s 806406000100
|
||||
Using reader with a card: Free Software Initiative of Japan Gnuk
|
||||
Sending: 80 64 06 00 01 00
|
||||
Received (SW1=0x90, SW2=0x00)
|
||||
```
|
||||
97
doc/public_key_authentication.md
Normal file
97
doc/public_key_authentication.md
Normal file
|
|
@ -0,0 +1,97 @@
|
|||
# Public Key Authentication
|
||||
|
||||
Public Key Authentication (PKA) is a mechanism to authenticate a legit user without introducing any PIN (see Notes below). The authentication is performed by signing a challenge and checking the signature result.
|
||||
|
||||
1. A Pico HSM #A contains a private key, whose public key will be used for authentication.
|
||||
2. The public key of #A is registered into a second Pico HSM #B.
|
||||
3. When a user wants to login into #B, #B generates a challenge that is passed to #A for signature.
|
||||
4. #A signs the challenge and returns the signature.
|
||||
5. #B verifies the signature against the challenge with the public key of #A, previously registered.
|
||||
6. If the signature is valid, #B grants access to the user.
|
||||
|
||||
To enable PKA, the device must be initialized beforehand. In case the device has secret/private keys, all shall be exported and reimported when the set up is finished.
|
||||
|
||||
## Requirements
|
||||
|
||||
To take advantage of PKA, the following is required:
|
||||
|
||||
1. Two Pico HSM: one will be used only for authentication (it can be any device able to generate a private key and sign arbitrary data).
|
||||
2. [SCS3](/doc/scs3.md "SCS3") tool to authenticate the user. At this time, OpenSC does not support PKA, only initialization.
|
||||
3. A secret key of ECC 256 bits.
|
||||
|
||||
## Usage
|
||||
|
||||
Before using SCS3, it must be patched [scs3.patch.txt](https://github.com/polhenarejos/pico-hsm/files/8890050/scs3.patch.txt). See [SCS3](/doc/scs3.md "SCS3") for further details.
|
||||
|
||||
### Generate the authentication key
|
||||
|
||||
On a secondary device, generate a private key, on the ECC 256 bits (`brainpoolP256r1` or `secp192r1`). Label it with an easy name, such as "Authentication".
|
||||
|
||||
<img width="1037" src="https://user-images.githubusercontent.com/55573252/173353764-4620ece4-0d82-4a23-a153-99bf912621a7.png">
|
||||
|
||||
Once finished, export the public key.
|
||||
|
||||
<img width="350" src="https://user-images.githubusercontent.com/55573252/173353732-63f40572-a42f-4e5c-a9ab-6e52a083956b.png">
|
||||
|
||||
### Initialization
|
||||
|
||||
On the primary device, initialize it. When prompting for an authentication mechanism, select "Public Key Authentication".
|
||||
|
||||
<img width="412" src="https://user-images.githubusercontent.com/55573252/173353661-17caf6db-0c76-4903-9b70-5afa79f5ae54.png"><img width="1037" alt="Captura de Pantalla 2022-06-13 a les 12 14 48" src="https://user-images.githubusercontent.com/55573252/173353822-310219dc-7c7d-4ece-9fd9-c7835c2688df.png">
|
||||
|
||||
Once finished, register the exported public key. A message of `0 authenticated public key(s) in 1 of 1 scheme` will appear if it is properly registered.
|
||||
|
||||
<img width="342" src="https://user-images.githubusercontent.com/55573252/173353917-f3f99405-c7ff-43ce-8914-6f3b713df952.png"><img width="1037" alt="Captura de Pantalla 2022-06-13 a les 12 16 17" src="https://user-images.githubusercontent.com/55573252/173353946-ee7eacf9-cead-4804-ac7a-57848f7c822b.png">
|
||||
|
||||
### Authentication
|
||||
|
||||
Plug the secondary device that stores the private key (do not load the device in the SCS3 tool) and initiate the public key authentication.
|
||||
|
||||
<img width="321" src="https://user-images.githubusercontent.com/55573252/173353998-8f418ec6-d90d-4168-801f-51008c78824d.png">
|
||||
|
||||
Select the secondary card and the Authentication private key (or the name you labeled it).
|
||||
|
||||
<img width="435" src="https://user-images.githubusercontent.com/55573252/173354044-50163113-829e-4d80-bbda-7b589849af73.png">
|
||||
|
||||
Introduce the PIN of the secondary device.
|
||||
|
||||
If the private key matches with the registered public key, the primary device will grant access and it will display `User PIN authenticated (9000)` (despite no PIN is provided).
|
||||
|
||||
From now on, you have full access and can operate normally with the primary device.
|
||||
|
||||
## Notes on DKEK
|
||||
|
||||
Pico HSM uses the PIN to protect the DKEK, which is lately used to protect private/secret keys and wrap/unwrap. However, when PKA is enabled, the authentication is not performed by introducing any PIN.
|
||||
Authenticated privileges are granted when PKA succeeds, regardless of PIN, which is optional.
|
||||
|
||||
Nevertheless, **it is extremely recommended to combine PKA with PIN**. Note that when combined, only PKA grants authenticated privileges. Therefore, if both schemes are setup, it is necessary to unlock the DKEK with PIN verification.
|
||||
Otherwise, it will not be possible to operate with private/secret keys despite the user will be logged in.
|
||||
|
||||
With this scheme, multiple custodians may authenticate the device individually and remotely and, when fully authenticated, the master user can unlock the DKEK with the PIN.
|
||||
|
||||
Moreover, with this approach the device is kept safe and neither the DKEK nor the private/secret keys are stored in plain text in the device.
|
||||
Even though the flash memory is dumped by an attacker, it will not be possible to decipher any sensitive data or key.
|
||||
|
||||
Initialization of the device with PKA **and** PIN can be achieved with SCS3 or OpenSC:
|
||||
|
||||
**Note:** do not import any DKEK share or DKEK operation before PKA and PIN setup.
|
||||
|
||||
### With OpenSC
|
||||
|
||||
Use the following command (or similar), which accepts the use of PIN parameter **and** PKA configuration:
|
||||
|
||||
```
|
||||
sc-hsm-tool -X --so-pin 1234567890123456 --pin 648219 -K 1 -n 1 -s 1
|
||||
```
|
||||
|
||||
and PKA and PIN are enabled, jointly with DKEK protection.
|
||||
|
||||
### With SCS3
|
||||
|
||||
Unfortunately, SCS3 does not allow to initialize the device with PKA and PIN at the same time, though it can be achieved in separated steps:
|
||||
|
||||
1. Initialize the device with PKA. When done, the PIN will not be initialized but it will advice that 3 attemps can be performed.
|
||||
2. There is NO default PIN. So, DO NOT attempt to log in yet. A reset PIN shall be requested.
|
||||
3. Click on ``Reset User-PIN``, introduce the SO-PIN configured during the initialization and introduce the desired User-PIN.
|
||||
|
||||
When done, the device will be configured with PIN **and** PKA.
|
||||
72
doc/scs3.md
Normal file
72
doc/scs3.md
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
# SCS3 tool
|
||||
|
||||
SCS3 tool is a specific tool developed by CardContact to manage HSM. Thanks to its interface, Pico HSM can be enhanced with more advanced functionalities, not present in the PKCS11 module:
|
||||
|
||||
- Import PKCS12 private keys and certificates.
|
||||
- Import private keys and certificates from other Pico HSM devices in WKY format.
|
||||
-
|
||||
|
||||
Unfortunately, there is no pkcs11 tool or equivalent capable to perform the import. Since it uses the SC-HSM driver, it also supports the communication with the [SCS3 tool](https://www.openscdp.org/scsh3/ "SCS3 tool"). It can be downloaded from [here](https://www.openscdp.org/scsh3/download.html "here").
|
||||
|
||||
However, SCS3 only works with those HSM manufactured by CardContact. The check is performed by means of trust store against the manufacturing certificates. For obvious reasons, these certificates can only be signed with the private keys of the Certificate Authorities listed in the trust store.
|
||||
|
||||
Pico HSM is shipped with its own CA certificates. To load this certificate onto the trust store of SCS3, the following line has to be appended to `SmartCardHSM.rootCerts` variable, near line `235` in the file `scs3/scsh/sc-hsm/SmartCardHSM.js`.
|
||||
|
||||
```
|
||||
ESPICOHSMCA00001: new CVC(new ByteString("7F218201BA7F4E8201725F290100421045535049434F48534D434130303030317F4982011D060A04007F000702020202038120FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF8220FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC83205AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B8441046B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C2964FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F58520FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC6325518641046A82C0A4FEAF41D6A1336AE7E992D81AD4F827929145DD0D777E1AB63D7E3325C8F7DAC0F74B6EAE13A72F6366777EC133AC5C28F456868E5F2C315044EB54EF8701015F201045535049434F48534D434130303030317F4C12060904007F0007030102025305C0000000005F25060202000801085F24060203000801085F3740601E974F57DDE060875FE6121AEF5BC02E10FC655311C7A32CA822FD18E53A80298EDC56E0D5EBF38FB470DC12987B1600AE91A0ADB5B22C4D80080782E278AD", HEX)),
|
||||
ESPICOHSMCA00002: new CVC(new ByteString("7F218201BA7F4E8201725F290100421045535049434F48534D434130303030327F4982011D060A04007F000702020202038120FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F822000000000000000000000000000000000000000000000000000000000000000008320000000000000000000000000000000000000000000000000000000000000000784410479BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B88520FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141864104FD4DDCD165DD64494F5F1DB76CDAE4E6BD10DA59BF6D48B684E9D50BCC4C2114D2A8BC7C9C1ACAA24C982B1383D4D02E0F32323F657E146E8A051DEB7F6CD40C8701015F201045535049434F48534D434130303030327F4C12060904007F0007030102025305C0000000045F25060205000400065F24060308000302065F3740472AA7A7945F6352F8A65388C2810AEFB657356FFD31CBB2FCC516BC4ECC953D0BACA935B22D73F49802DD9E9DE6439FD8AEA18BD71E1439ABF458C085BD70C8", HEX))
|
||||
```
|
||||
|
||||
Therefore, the whole variable becomes:
|
||||
|
||||
```
|
||||
SmartCardHSM.rootCerts = {
|
||||
DESRCACC100001: new CVC(new ByteString("7F218201B47F4E82016C5F290100420E44455352434143433130303030317F4982011D060A04007F000702020202038120A9FB57DBA1EEA9BC3E660A909D838D726E3BF623D52620282013481D1F6E537782207D5A0975FC2C3057EEF67530417AFFE7FB8055C126DC5C6CE94A4B44F330B5D9832026DC5C6CE94A4B44F330B5D9BBD77CBF958416295CF7E1CE6BCCDC18FF8C07B68441048BD2AEB9CB7E57CB2C4B482FFC81B7AFB9DE27E1E3BD23C23A4453BD9ACE3262547EF835C3DAC4FD97F8461A14611DC9C27745132DED8E545C1D54C72F0469978520A9FB57DBA1EEA9BC3E660A909D838D718C397AA3B561A6F7901E0E82974856A78641046D025A8026CDBA245F10DF1B72E9880FFF746DAB40A43A3D5C6BEBF27707C30F6DEA72430EE3287B0665C1EAA6EAA4FA26C46303001983F82BD1AA31E03DA0628701015F200E44455352434143433130303030317F4C10060B2B0601040181C31F0301015301C05F25060102010100095F24060302010100085F37409DBB382B1711D2BAACB0C623D40C6267D0B52BA455C01F56333DC9554810B9B2878DAF9EC3ADA19C7B065D780D6C9C3C2ECEDFD78DEB18AF40778ADF89E861CA", HEX)),
|
||||
UTSRCACC100001: new CVC(new ByteString("7F218201B47F4E82016C5F290100420E55545352434143433130303030317F4982011D060A04007F000702020202038120A9FB57DBA1EEA9BC3E660A909D838D726E3BF623D52620282013481D1F6E537782207D5A0975FC2C3057EEF67530417AFFE7FB8055C126DC5C6CE94A4B44F330B5D9832026DC5C6CE94A4B44F330B5D9BBD77CBF958416295CF7E1CE6BCCDC18FF8C07B68441048BD2AEB9CB7E57CB2C4B482FFC81B7AFB9DE27E1E3BD23C23A4453BD9ACE3262547EF835C3DAC4FD97F8461A14611DC9C27745132DED8E545C1D54C72F0469978520A9FB57DBA1EEA9BC3E660A909D838D718C397AA3B561A6F7901E0E82974856A7864104A041FEB2FD116B2AD19CA6B7EACD71C9892F941BB88D67DCEEC92501F070011957E22122BA6C2CF5FF02936F482E35A6129CCBBA8E9383836D3106879C408EF08701015F200E55545352434143433130303030317F4C10060B2B0601040181C31F0301015301C05F25060102010100095F24060302010100085F3740914DD0FA00615C44048D1467435400423A4AD1BD37FD98D6DE84FD8037489582325C72956D4FDFABC6EDBA48184A754F37F1BE5142DD1C27D66569308CE19AAF", HEX)),
|
||||
ESPICOHSMCA00001: new CVC(new ByteString("7F218201BA7F4E8201725F290100421045535049434F48534D434130303030317F4982011D060A04007F000702020202038120FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF8220FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC83205AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B8441046B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C2964FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F58520FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC6325518641046A82C0A4FEAF41D6A1336AE7E992D81AD4F827929145DD0D777E1AB63D7E3325C8F7DAC0F74B6EAE13A72F6366777EC133AC5C28F456868E5F2C315044EB54EF8701015F201045535049434F48534D434130303030317F4C12060904007F0007030102025305C0000000005F25060202000801085F24060203000801085F3740601E974F57DDE060875FE6121AEF5BC02E10FC655311C7A32CA822FD18E53A80298EDC56E0D5EBF38FB470DC12987B1600AE91A0ADB5B22C4D80080782E278AD", HEX)),
|
||||
ESPICOHSMCA00002: new CVC(new ByteString("7F218201BA7F4E8201725F290100421045535049434F48534D434130303030327F4982011D060A04007F000702020202038120FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F822000000000000000000000000000000000000000000000000000000000000000008320000000000000000000000000000000000000000000000000000000000000000784410479BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B88520FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141864104FD4DDCD165DD64494F5F1DB76CDAE4E6BD10DA59BF6D48B684E9D50BCC4C2114D2A8BC7C9C1ACAA24C982B1383D4D02E0F32323F657E146E8A051DEB7F6CD40C8701015F201045535049434F48534D434130303030327F4C12060904007F0007030102025305C0000000045F25060205000400065F24060308000302065F3740472AA7A7945F6352F8A65388C2810AEFB657356FFD31CBB2FCC516BC4ECC953D0BACA935B22D73F49802DD9E9DE6439FD8AEA18BD71E1439ABF458C085BD70C8", HEX))
|
||||
}
|
||||
```
|
||||
|
||||
Similarly, replace the line `1531` in file `scs3/keymanager/keymanager.js` with:
|
||||
|
||||
```
|
||||
assert(devcert.verifyWith(this.crypto, dicacert.getPublicKey(SmartCardHSM.rootCerts.ESPICOHSMCA00001.getPublicKey()), dicacert.getPublicKeyOID()));
|
||||
```
|
||||
|
||||
|
||||
Alternatively, this patch [scs3.patch.txt](https://github.com/user-attachments/files/20274013/scs3.patch.txt) can be applied.
|
||||
|
||||
After this ammendment, the program can be started and the KeyManager can be invoked (CTRL+M) and it will output something similar to:
|
||||
```
|
||||
>load("keymanager/keymanager.js");
|
||||
|
||||
SmartCard-HSM Version 2.6 on JCOP Free memory 215512 byte
|
||||
Issuer Certificate : CVC id-AT DV (official domestic) CAR=ESPICOHSMCA00001 CHR=ESPICOHSMDV00001 CED=18 / d’agost / 2022 CXD=14 / de juny / 2023
|
||||
Device Certificate : CVC id-AT Terminal CAR=ESPICOHSMDV00001 CHR=ESPICOHSMTRYZRGW CED=22 / d’agost / 2022 CXD=22 / d’agost / 2023
|
||||
Default Key Domain : 223CD8D8F794889AC163305881BF8C04960BBB8658120491F1C0601F6BF97183
|
||||
-------------------------------------------------------------------
|
||||
Please right-click on nodes in the outline to see possible actions.
|
||||
For most operations you will need to authenticate first using a
|
||||
mechanism from the User PIN context menu.
|
||||
>
|
||||
```
|
||||
|
||||
The SCS3 tool is ready to import private keys and certificates, wraped in WKY files or in PKCS#12 format. Also, all stored keys can be exported, combined with their respective certificates. Note that the user has to be previously logged in.
|
||||
|
||||
## macOS users
|
||||
In macOS, the PCSC must be explicitly specified. Otherwise, the reader will not be found.
|
||||
|
||||
It can be executed in a Terminal via
|
||||
```
|
||||
java -Dsun.security.smartcardio.library=/System/Library/Frameworks/PCSC.framework/Versions/Current/PCSC -Dorg.bouncycastle.asn1.allow_unsafe_integer=true -Djava.library.path=./lib -classpath 'lib/*' de.cardcontact.scdp.scsh3.GUIShell
|
||||
```
|
||||
## DKEK requirement
|
||||
|
||||
In order to perform the import, private keys must be wrapped with the same DKEK present in the Pico HSM. Thus, the Pico HSM must be previously initialized with at minimum of 1 DKEK share. This share will be used to wrap the private key before import.
|
||||
|
||||
Note that the DKEK share shall be available before the import. In this way, all custodians must be present during the import process, since they will have to introduce their respective DKEK.
|
||||
|
||||
## RSA 4096 support
|
||||
|
||||
Generating 4096 bits key in the Pico HSM is highly expensive. It may take minutes or hours to finish the generation. Therefore, it is extremely recommendable to generate the key in the host and import it into the Pico HSM.
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
# Sign and verify
|
||||
|
||||
Pico HSM supports in place signature of arbitrary data. It supports the following algorithms:
|
||||
* RSA-PKCS
|
||||
* RSA-PKCS
|
||||
* RSA-X-509
|
||||
* SHA1-RSA-PKCS
|
||||
* SHA256-RSA-PKCS
|
||||
|
|
@ -32,7 +32,7 @@ $ openssl rsa -inform DER -outform PEM -in 1.der -pubin > 1.pub
|
|||
At this moment, you are able to verify with the public key in `1.pub`. The signature is computed inside the Pico HSM with the private key. It never leaves the device.
|
||||
|
||||
## RSA-PKCS
|
||||
This algorithm is used to sign raw data.
|
||||
This algorithm is used to sign raw data.
|
||||
|
||||
To sign the data:
|
||||
```
|
||||
|
|
@ -91,7 +91,7 @@ This algorithm uses the RSA-PKCS with PSS salt to randomize the signature. Pico
|
|||
To sign the data:
|
||||
```
|
||||
$ pkcs11-tool --id 1 --sign --pin 648219 --mechanism RSA-PKCS-PSS -i data.sha1 -o data.sig
|
||||
```
|
||||
```
|
||||
|
||||
To verify the signature:
|
||||
```
|
||||
|
|
@ -105,7 +105,7 @@ This algorithm takes the file as the input and sends its hash for signing with t
|
|||
To sign the data:
|
||||
```
|
||||
$ pkcs11-tool --id 1 --sign --pin 648219 --mechanism SHA1-RSA-PKCS-PSS -i data -o data.sig
|
||||
```
|
||||
```
|
||||
|
||||
To verify the signature:
|
||||
```
|
||||
|
|
@ -118,14 +118,14 @@ This is a raw ECDSA signature, which is usually used to sign a hashed message. `
|
|||
|
||||
To sign the data:
|
||||
```
|
||||
$ pkcs11-tool --id 11 --sign --pin 648219 --mechanism ECDSA -i data.sha1 -o data.sig --signature-format openssl
|
||||
$ pkcs11-tool --id 11 --sign --pin 648219 --mechanism ECDSA -i data.sha1 -o data.sig --signature-format openssl
|
||||
Using slot 0 with a present token (0x0)
|
||||
Using signature algorithm ECDSA
|
||||
```
|
||||
|
||||
To verify the signature:
|
||||
```
|
||||
$ openssl pkeyutl -verify -pubin -inkey 11.pub -in data.sha1 -sigfile data.sig
|
||||
$ openssl pkeyutl -verify -pubin -inkey 11.pub -in data.sha1 -sigfile data.sig
|
||||
Signature Verified Successfully
|
||||
```
|
||||
|
||||
|
|
@ -143,11 +143,6 @@ Using signature algorithm ECDSA-SHA256
|
|||
|
||||
The signature is verified with the hash:
|
||||
```
|
||||
$ openssl pkeyutl -verify -pubin -inkey 11.pub -in data.sha1 -sigfile data.sig
|
||||
$ openssl pkeyutl -verify -pubin -inkey 11.pub -in data.sha1 -sigfile data.sig
|
||||
Signature Verified Successfully
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
117
doc/store_data.md
Normal file
117
doc/store_data.md
Normal file
|
|
@ -0,0 +1,117 @@
|
|||
# Store binary data
|
||||
Pico HSM has a internal flash which can store binary data. With this approach, you can save different files, encrypt into the Pico HSM and retrieve them after.
|
||||
|
||||
## Maximum size
|
||||
Due to internal constraints with the flash components, the maximum file size is `4096` bytes. This mechanism is mainly used to store small files, such as keys in plain text, certificates, credentials, etc.
|
||||
|
||||
## Store a file
|
||||
Before writting a file into the Pico HSM, we generate the data file with the following text:
|
||||
|
||||
```
|
||||
$ echo 'Pico HSM is awesome!' > test
|
||||
```
|
||||
|
||||
Then, we can store the data file with the following command:
|
||||
|
||||
```
|
||||
$ pkcs11-tool --pin 648219 --write-object test --type data --id 1 --label 'test1'
|
||||
Using slot 0 with a present token (0x0)
|
||||
Created Data Object:
|
||||
Data object 1236368320
|
||||
label: 'test1'
|
||||
application: 'test1'
|
||||
app_id: <empty>
|
||||
flags: modifiable
|
||||
```
|
||||
|
||||
This file can also be protected with the PIN. In this case, use the previous command with the `--private` flag:
|
||||
|
||||
```
|
||||
$ pkcs11-tool --pin 648219 --write-object test --type data --id 2 --label 'test2' --private
|
||||
Using slot 0 with a present token (0x0)
|
||||
Created Data Object:
|
||||
Data object 1329612320
|
||||
label: 'test2'
|
||||
application: 'test2'
|
||||
app_id: <empty>
|
||||
flags: modifiable private
|
||||
```
|
||||
|
||||
Always provide a unique `--label`, as it will be used to index and reference the file for retrieving.
|
||||
|
||||
## Retrieve a file
|
||||
To view the stored file, we can use the following command with the same label we employed:
|
||||
|
||||
```
|
||||
$ pkcs11-tool --read-object --type data --label 'test1'
|
||||
Using slot 0 with a present token (0x0)
|
||||
Pico HSM is awesome!
|
||||
```
|
||||
|
||||
Note that if the `--private` flag is not provided during the writting stage, the file can be accessed without the PIN.
|
||||
|
||||
To retrieve a private file with the PIN:
|
||||
|
||||
```
|
||||
$ pkcs11-tool --read-object --type data --label 'test2' --pin 648219
|
||||
Using slot 0 with a present token (0x0)
|
||||
Pico HSM is awesome!
|
||||
```
|
||||
|
||||
## Using `pkcs15-tool`
|
||||
PKCS15 tool can be used to list the stored files. For instance:
|
||||
|
||||
```
|
||||
$ pkcs15-tool -D
|
||||
Using reader with a card: Free Software Initiative of Japan Gnuk
|
||||
PKCS#15 Card [Pico-HSM]:
|
||||
Version : 1
|
||||
Serial number : ESTERMHSM
|
||||
Manufacturer ID: Pol Henarejos
|
||||
Flags : PRN generation, EID compliant
|
||||
|
||||
|
||||
PIN [UserPIN]
|
||||
Object Flags : [0x03], private, modifiable
|
||||
Auth ID : 02
|
||||
ID : 01
|
||||
Flags : [0x812], local, initialized, exchangeRefData
|
||||
Length : min_len:6, max_len:15, stored_len:0
|
||||
Pad char : 0x00
|
||||
Reference : 129 (0x81)
|
||||
Type : ascii-numeric
|
||||
Path : e82b0601040181c31f0201::
|
||||
Tries left : 3
|
||||
|
||||
PIN [SOPIN]
|
||||
Object Flags : [0x01], private
|
||||
ID : 02
|
||||
Flags : [0x9A], local, unblock-disabled, initialized, soPin
|
||||
Length : min_len:16, max_len:16, stored_len:0
|
||||
Pad char : 0x00
|
||||
Reference : 136 (0x88)
|
||||
Type : bcd
|
||||
Path : e82b0601040181c31f0201::
|
||||
Tries left : 15
|
||||
|
||||
Data object 'test1'
|
||||
applicationName: test1
|
||||
Path: e82b0601040181c31f0201::cf00
|
||||
Data (21 bytes): 5069636F2048534D20697320617765736F6D65210A
|
||||
|
||||
Data object 'test2'
|
||||
applicationName: test2
|
||||
Path: e82b0601040181c31f0201::cd01
|
||||
Auth ID: 01
|
||||
```
|
||||
|
||||
As expected, the public file is displayed (in hexadecimal string). The private file contains the `Auth ID` flag and it is not displayed.
|
||||
|
||||
## Delete a file
|
||||
A stored file can be deleted with the following command:
|
||||
|
||||
```
|
||||
$ pkcs11-tool --login --pin 648219 --delete-object --type data --application-label test1
|
||||
```
|
||||
|
||||
|
||||
22
doc/usage.md
22
doc/usage.md
|
|
@ -1,7 +1,7 @@
|
|||
# Usage
|
||||
|
||||
## Tools
|
||||
We use multiple tools and PKCS#11 drivers and modules, depending on the purpose.
|
||||
We use multiple tools and PKCS#11 drivers and modules, depending on the purpose.
|
||||
* **pkcs11-tool**: from OpenSC. It interfaces with the HSM via PKCS#11 interface. It supports different drivers and modules.
|
||||
* **sc-tool**: an alias of pkcs11-tool with the sc-hsm-embedded module. It is mainly used for AES management and it is defined as:
|
||||
```
|
||||
|
|
@ -22,20 +22,20 @@ init=0
|
|||
PIN=648219
|
||||
```
|
||||
`opensc-pkcs11.so` can be replaced by `libsc-hsm-pkcs11.so` if desired.
|
||||
* **sc-hsm-tool**: from OpenSC. Used to initialize the device.
|
||||
* **pico-hsm-tool**: Used to initialize the device.
|
||||
* **opensc-tool**: from OpenSC. Used to list and detect the reader with the HSM.
|
||||
|
||||
[^1]: `openssl version -a` will return the `OPENSSLDIR`, which contains `openssl.cnf` file and `ENGINESDIR`, which contains the p11 engine.
|
||||
|
||||
## Initialization
|
||||
The first step is to initialize the HSM:
|
||||
The first step is to initialize the HSM. To do so, use:
|
||||
```
|
||||
$ sc-hsm-tool --initialize --so-pin 3537363231383830 --pin 648219
|
||||
```
|
||||
The PIN number is used to manage all private keys in the device. It supports three attemps. After the third PIN failure, it gets blocked.
|
||||
The PIN number is used to manage all private keys in the device. It supports three attemps. After the third PIN failure, it gets blocked.
|
||||
The PIN accepts from 6 to 16 characters.
|
||||
|
||||
The SO-PIN is used to unblock the PIN. It accepts 15 attemps. After 15 failed attempts, the device will be completely blocked and will be necessary to initialize again, erasing all private keys and losing the access. Therefore, keep the SO-PIN in a safe place.
|
||||
The SO-PIN is used to unblock the PIN. It accepts 15 attemps. After 15 failed attempts, the device will be completely blocked and will be necessary to initialize again, erasing all private keys and losing the access. Therefore, keep the SO-PIN in a safe place.
|
||||
The SO-PIN is always 16 hexadecimal characters.
|
||||
|
||||
## PIN and SO-PIN management
|
||||
|
|
@ -51,7 +51,7 @@ $ pkcs11-tool --login --pin 648219 --change-pin --new-pin 123456
|
|||
|
||||
To unblock the PIN:
|
||||
```
|
||||
$ pkcs11-tool --login --login-type so --so-pin=3537363231383830 --init-pin --new-pin=648219
|
||||
$ pkcs11-tool --login --login-type so --so-pin 3537363231383830 --init-pin --new-pin 648219
|
||||
```
|
||||
|
||||
## Keypair generation
|
||||
|
|
@ -62,7 +62,7 @@ To generate a RSA 2048 bits, use the following command:
|
|||
$ pkcs11-tool -l --pin 648219 --keypairgen --key-type rsa:2048 --id 1 --label "RSA2K"
|
||||
Using slot 0 with a present token (0x0)
|
||||
Key pair generated:
|
||||
Private Key Object; RSA
|
||||
Private Key Object; RSA
|
||||
label: RSA2K
|
||||
ID: 1
|
||||
Usage: decrypt, sign
|
||||
|
|
@ -77,7 +77,7 @@ The ID parameter is an internal hexadecimal number for easy identification. The
|
|||
|
||||
Pico HSM accepts RSA of 1024 (`rsa:1024`), 2048 (`rsa:2048`) and 4096 bits (`rsa:4096`).
|
||||
|
||||
**Caution**: RSA 2048 bits may take more than 20 seconds. RSA 4096 bits may take more than 20 minutes. The Pico HSM will work as normally and neither the HSM nor the host will block. But, in the meantime, the Pico HSM will not accept any command.
|
||||
**Caution**: RSA 2048 bits may take more than 20 seconds. RSA 4096 bits may take more than 20 minutes. The Pico HSM will work as normally and neither the HSM nor the host will block. But, in the meantime, the Pico HSM will not accept any command.
|
||||
An alternative is to generate the private key locally and import it to the HSM. This approach, however, is less secure as it does not use a True RNG or HRNG like Pico HSM. Use this approach if you have plugged a TRNG or you are not worried about obtaining the highest entropy.
|
||||
|
||||
Pico HSM also accepts ECDSA keypairs:
|
||||
|
|
@ -157,9 +157,9 @@ Certificate:
|
|||
a0:30:b2:ec:d3:d6:0d:58:f3
|
||||
Exponent: 65537 (0x10001)
|
||||
X509v3 extensions:
|
||||
X509v3 Subject Key Identifier:
|
||||
X509v3 Subject Key Identifier:
|
||||
98:07:DA:13:B0:8E:A0:5C:97:83:68:FE:4A:25:8D:50:C4:DC:16:FA
|
||||
X509v3 Authority Key Identifier:
|
||||
X509v3 Authority Key Identifier:
|
||||
keyid:98:07:DA:13:B0:8E:A0:5C:97:83:68:FE:4A:25:8D:50:C4:DC:16:FA
|
||||
|
||||
X509v3 Basic Constraints: critical
|
||||
|
|
@ -173,7 +173,7 @@ Certificate:
|
|||
99:2b:b2:82:66:c1:06:a7:2c:62:af:e2:e4:93:42:36:66:8d:
|
||||
c5:3f:e1:ec:5f:9a:f8:5f:b3:6a:8f:0e:12:5d:c9:46:38:ea:
|
||||
0b:08
|
||||
```
|
||||
```
|
||||
|
||||
The resulting file `cert.pem` contains the signed certificate in PEM format. Convert it into DER format and load it into the Pico HSM:
|
||||
|
||||
|
|
|
|||
1
mbedtls
1
mbedtls
|
|
@ -1 +0,0 @@
|
|||
Subproject commit d65aeb37349ad1a50e0f6c9b694d4b5290d60e49
|
||||
|
|
@ -1,77 +0,0 @@
|
|||
#!/bin/bash
|
||||
|
||||
echo "----------------------------"
|
||||
echo "VID/PID patcher for Pico HSM"
|
||||
echo "----------------------------"
|
||||
echo ""
|
||||
|
||||
if [ "$#" -le 0 ]; then
|
||||
echo "Usage: $0 VID:PID [input_uf2_file] [output_uf2_file]"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
IFS=':' read -r -a ARR <<< "$1"
|
||||
|
||||
if [ ${#ARR[@]} -ne 2 ]; then
|
||||
echo "ERROR: Specify vendor and product ids as VID:PID (e.g., $0 CAFE:1234)"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
VID=${ARR[0]}
|
||||
PID=${ARR[1]}
|
||||
|
||||
if [ ${#VID} -ne 4 ]; then
|
||||
echo "ERROR: VID length must be 4 hexadecimal characters"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ ${#PID} -ne 4 ]; then
|
||||
echo "ERROR: PID length must be 4 hexadecimal characters"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if ! [[ $VID =~ ^[0-9A-Fa-f]{1,}$ ]] ; then
|
||||
echo "ERROR: VID must contain hexadecimal characters"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if ! [[ $PID =~ ^[0-9A-Fa-f]{1,}$ ]] ; then
|
||||
echo "ERROR: PID must contain hexadecimal characters"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
UF2_FILE_IF="hsm2040.uf2"
|
||||
UF2_FILE_OF="$UF2_FILE_IF"
|
||||
|
||||
if [ "$#" -ge 2 ]; then
|
||||
UF2_FILE_IF="$2"
|
||||
UF2_FILE_OF="$UF2_FILE_IF"
|
||||
fi
|
||||
|
||||
if [ "$#" -ge 3 ]; then
|
||||
UF2_FILE_OF="$3"
|
||||
fi
|
||||
|
||||
|
||||
echo -n "Patching ${UF2_FILE_IF}... "
|
||||
|
||||
if [[ ! -f "$UF2_FILE_IF" ]]; then
|
||||
echo "ERROR: UF2 file ${UF2_FILE_IF} does not exist"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ "$UF2_FILE_IF" != "$UF2_FILE_OF" ]; then
|
||||
cp -R $UF2_FILE_IF $UF2_FILE_OF
|
||||
fi
|
||||
|
||||
BASE_ADDRESS=`xxd "$UF2_FILE_IF" | grep "fffe fdfc 0103 0102 0301" | cut -d " " -f1`
|
||||
ADDRESS="0x${BASE_ADDRESS%?}"
|
||||
|
||||
LITTLE_VID="\x${VID:2:2}\x${VID:0:2}"
|
||||
LITTLE_PID="\x${PID:2:2}\x${PID:0:2}"
|
||||
|
||||
printf "$LITTLE_VID$LITTLE_PID" | dd of="$UF2_FILE_OF" bs=1 seek=$(($ADDRESS)) conv=notrunc 2> /dev/null
|
||||
|
||||
echo "Done!"
|
||||
echo ""
|
||||
echo "Patched file was saved in ${UF2_FILE_OF}"
|
||||
1
pico-keys-sdk
Submodule
1
pico-keys-sdk
Submodule
|
|
@ -0,0 +1 @@
|
|||
Subproject commit 263e554cc6c59a5f168f8589c4bdabe6e1e64c25
|
||||
|
|
@ -18,9 +18,20 @@ if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT_PATH} AND (NOT PICO_SDK_FETCH_FROM_GIT_P
|
|||
message("Using PICO_SDK_FETCH_FROM_GIT_PATH from environment ('${PICO_SDK_FETCH_FROM_GIT_PATH}')")
|
||||
endif ()
|
||||
|
||||
if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT_TAG} AND (NOT PICO_SDK_FETCH_FROM_GIT_TAG))
|
||||
set(PICO_SDK_FETCH_FROM_GIT_TAG $ENV{PICO_SDK_FETCH_FROM_GIT_TAG})
|
||||
message("Using PICO_SDK_FETCH_FROM_GIT_TAG from environment ('${PICO_SDK_FETCH_FROM_GIT_TAG}')")
|
||||
endif ()
|
||||
|
||||
if (PICO_SDK_FETCH_FROM_GIT AND NOT PICO_SDK_FETCH_FROM_GIT_TAG)
|
||||
set(PICO_SDK_FETCH_FROM_GIT_TAG "master")
|
||||
message("Using master as default value for PICO_SDK_FETCH_FROM_GIT_TAG")
|
||||
endif()
|
||||
|
||||
set(PICO_SDK_PATH "${PICO_SDK_PATH}" CACHE PATH "Path to the Raspberry Pi Pico SDK")
|
||||
set(PICO_SDK_FETCH_FROM_GIT "${PICO_SDK_FETCH_FROM_GIT}" CACHE BOOL "Set to ON to fetch copy of SDK from git if not otherwise locatable")
|
||||
set(PICO_SDK_FETCH_FROM_GIT_PATH "${PICO_SDK_FETCH_FROM_GIT_PATH}" CACHE FILEPATH "location to download SDK")
|
||||
set(PICO_SDK_FETCH_FROM_GIT_TAG "${PICO_SDK_FETCH_FROM_GIT_TAG}" CACHE FILEPATH "release tag for SDK")
|
||||
|
||||
if (NOT PICO_SDK_PATH)
|
||||
if (PICO_SDK_FETCH_FROM_GIT)
|
||||
|
|
@ -29,11 +40,22 @@ if (NOT PICO_SDK_PATH)
|
|||
if (PICO_SDK_FETCH_FROM_GIT_PATH)
|
||||
get_filename_component(FETCHCONTENT_BASE_DIR "${PICO_SDK_FETCH_FROM_GIT_PATH}" REALPATH BASE_DIR "${CMAKE_SOURCE_DIR}")
|
||||
endif ()
|
||||
FetchContent_Declare(
|
||||
pico_sdk
|
||||
GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk
|
||||
GIT_TAG master
|
||||
)
|
||||
# GIT_SUBMODULES_RECURSE was added in 3.17
|
||||
if (${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.17.0")
|
||||
FetchContent_Declare(
|
||||
pico_sdk
|
||||
GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk
|
||||
GIT_TAG ${PICO_SDK_FETCH_FROM_GIT_TAG}
|
||||
GIT_SUBMODULES_RECURSE FALSE
|
||||
)
|
||||
else ()
|
||||
FetchContent_Declare(
|
||||
pico_sdk
|
||||
GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk
|
||||
GIT_TAG ${PICO_SDK_FETCH_FROM_GIT_TAG}
|
||||
)
|
||||
endif ()
|
||||
|
||||
if (NOT pico_sdk)
|
||||
message("Downloading Raspberry Pi Pico SDK")
|
||||
FetchContent_Populate(pico_sdk)
|
||||
|
|
|
|||
57
sdkconfig.defaults
Executable file
57
sdkconfig.defaults
Executable file
|
|
@ -0,0 +1,57 @@
|
|||
# This file was generated using idf.py save-defconfig. It can be edited manually.
|
||||
# Espressif IoT Development Framework (ESP-IDF) Project Minimal Configuration
|
||||
#
|
||||
IGNORE_UNKNOWN_FILES_FOR_MANAGED_COMPONENTS=y
|
||||
|
||||
CONFIG_TINYUSB=y
|
||||
CONFIG_TINYUSB_TASK_STACK_SIZE=16384
|
||||
|
||||
CONFIG_PARTITION_TABLE_CUSTOM=y
|
||||
CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="pico-keys-sdk/config/esp32/partitions.csv"
|
||||
CONFIG_PARTITION_TABLE_FILENAME="pico-keys-sdk/config/esp32/partitions.csv"
|
||||
CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y
|
||||
CONFIG_ESPTOOLPY_FLASHMODE_QIO=y
|
||||
CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ_240=y
|
||||
CONFIG_WL_SECTOR_SIZE_512=y
|
||||
CONFIG_WL_SECTOR_MODE_PERF=y
|
||||
COMPILER_OPTIMIZATION="Performance"
|
||||
|
||||
CONFIG_MBEDTLS_CMAC_C=y
|
||||
CONFIG_MBEDTLS_CHACHA20_C=y
|
||||
CONFIG_MBEDTLS_POLY1305_C=y
|
||||
CONFIG_MBEDTLS_CHACHAPOLY_C=y
|
||||
CONFIG_MBEDTLS_HKDF_C=y
|
||||
CONFIG_MBEDTLS_HARDWARE_ECC=y
|
||||
CONFIG_MBEDTLS_HARDWARE_GCM=y
|
||||
# CONFIG_MBEDTLS_HARDWARE_MPI is not set
|
||||
CONFIG_MBEDTLS_HARDWARE_SHA=y
|
||||
CONFIG_MBEDTLS_HARDWARE_AES=y
|
||||
# CONFIG_MBEDTLS_ROM_MD5 is not set
|
||||
CONFIG_MBEDTLS_SHA512_C=y
|
||||
CONFIG_MBEDTLS_TLS_DISABLED=y
|
||||
# CONFIG_MBEDTLS_TLS_ENABLED is not set
|
||||
# CONFIG_ESP_TLS_USE_DS_PERIPHERAL is not set
|
||||
# CONFIG_ESP_WIFI_ENABLED is not set
|
||||
# CONFIG_ESP_WIFI_MBEDTLS_CRYPTO is not set
|
||||
# CONFIG_ESP_WIFI_MBEDTLS_TLS_CLIENT is not set
|
||||
# CONFIG_ESP_WIFI_MBEDTLS_CRYPTO is not set
|
||||
# CONFIG_MBEDTLS_PSK_MODES is not set
|
||||
# CONFIG_MBEDTLS_KEY_EXCHANGE_RSA is not set
|
||||
# CONFIG_MBEDTLS_KEY_EXCHANGE_ELLIPTIC_CURVE is not set
|
||||
# CONFIG_MBEDTLS_KEY_EXCHANGE_ECDHE_RSA is not set
|
||||
# CONFIG_MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA is not set
|
||||
# CONFIG_MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA is not set
|
||||
# CONFIG_MBEDTLS_KEY_EXCHANGE_ECDH_RSA is not set
|
||||
# CONFIG_MBEDTLS_SSL_RENEGOTIATION is not set
|
||||
# CONFIG_MBEDTLS_SSL_PROTO_TLS1_2 is not set
|
||||
# CONFIG_MBEDTLS_SSL_PROTO_GMTSSL1_1 is not set
|
||||
# CONFIG_MBEDTLS_SSL_PROTO_DTLS is not set
|
||||
# CONFIG_MBEDTLS_SSL_ALPN is not set
|
||||
# CONFIG_MBEDTLS_CLIENT_SSL_SESSION_TICKETS is not set
|
||||
# CONFIG_MBEDTLS_SERVER_SSL_SESSION_TICKETS is not set
|
||||
# CONFIG_ESP_WIFI_ENABLE_WPA3_SAE is not set
|
||||
# CONFIG_ESP_WIFI_ENABLE_WPA3_OWE_STA is not set
|
||||
# CONFIG_ESP_WIFI_ENABLE_WPA3_SAE is not set
|
||||
# CONFIG_ESP_WIFI_ENABLE_WPA3_OWE_STA is not set
|
||||
|
||||
CONFIG_ESP_COREDUMP_ENABLE_TO_UART=y
|
||||
449
src/fs/file.c
449
src/fs/file.c
|
|
@ -1,449 +0,0 @@
|
|||
#include "file.h"
|
||||
#include "tusb.h"
|
||||
#include "hsm2040.h"
|
||||
#include "sc_hsm.h"
|
||||
#include "libopensc/card-sc-hsm.h"
|
||||
#include <string.h>
|
||||
|
||||
extern const uintptr_t end_data_pool;
|
||||
extern const uintptr_t start_data_pool;
|
||||
extern int flash_write_data_to_file(file_t *file, const uint8_t *data, uint16_t len);
|
||||
extern int flash_program_halfword (uintptr_t addr, uint16_t data);
|
||||
extern int flash_program_word (uintptr_t addr, uint32_t data);
|
||||
extern int flash_program_uintptr (uintptr_t addr, uintptr_t data);
|
||||
extern int flash_program_block(uintptr_t addr, const uint8_t *data, size_t len);
|
||||
extern uintptr_t flash_read_uintptr(uintptr_t addr);
|
||||
extern uint16_t flash_read_uint16(uintptr_t addr);
|
||||
extern uint8_t flash_read_uint8(uintptr_t addr);
|
||||
extern uint8_t *flash_read(uintptr_t addr);
|
||||
extern void low_flash_available();
|
||||
|
||||
//puts FCI in the RAPDU
|
||||
void process_fci(const file_t *pe) {
|
||||
uint8_t *p = res_APDU;
|
||||
uint8_t buf[64];
|
||||
res_APDU_size = 0;
|
||||
res_APDU[res_APDU_size++] = 0x6f;
|
||||
res_APDU[res_APDU_size++] = 0x00; //computed later
|
||||
|
||||
res_APDU[res_APDU_size++] = 0x81;
|
||||
res_APDU[res_APDU_size++] = 2;
|
||||
if (pe->data) {
|
||||
if ((pe->type & FILE_DATA_FUNC) == FILE_DATA_FUNC) {
|
||||
uint16_t len = ((int (*)(const file_t *, int))(pe->data))(pe, 0);
|
||||
res_APDU[res_APDU_size++] = (len >> 8) & 0xff;
|
||||
res_APDU[res_APDU_size++] = len & 0xff;
|
||||
}
|
||||
else {
|
||||
res_APDU[res_APDU_size++] = pe->data[1];
|
||||
res_APDU[res_APDU_size++] = pe->data[0];
|
||||
}
|
||||
}
|
||||
else {
|
||||
memset(res_APDU+res_APDU_size, 0, 2);
|
||||
res_APDU_size += 2;
|
||||
}
|
||||
|
||||
res_APDU[res_APDU_size++] = 0x82;
|
||||
res_APDU[res_APDU_size++] = 1;
|
||||
res_APDU[res_APDU_size] = 0;
|
||||
if (pe->type == FILE_TYPE_INTERNAL_EF)
|
||||
res_APDU[res_APDU_size++] |= 0x08;
|
||||
else if (pe->type == FILE_TYPE_WORKING_EF)
|
||||
res_APDU[res_APDU_size++] |= pe->ef_structure & 0x7;
|
||||
else if (pe->type == FILE_TYPE_DF)
|
||||
res_APDU[res_APDU_size++] |= 0x38;
|
||||
|
||||
res_APDU[res_APDU_size++] = 0x83;
|
||||
res_APDU[res_APDU_size++] = 2;
|
||||
put_uint16_t(pe->fid, res_APDU+res_APDU_size);
|
||||
res_APDU_size += 2;
|
||||
res_APDU[1] = res_APDU_size-2;
|
||||
}
|
||||
|
||||
const uint8_t cvca[] = {
|
||||
0x6A, 0x01,
|
||||
0x7f, 0x21, 0x82, 0x01, 0x65, 0x7f, 0x4e, 0x82, 0x01, 0x2d, 0x5f,
|
||||
0x29, 0x01, 0x00, 0x42, 0x0e, 0x45, 0x53, 0x48, 0x53, 0x4d, 0x43,
|
||||
0x56, 0x43, 0x41, 0x32, 0x30, 0x34, 0x30, 0x31, 0x7f, 0x49, 0x81,
|
||||
0xdd, 0x06, 0x0a, 0x04, 0x00, 0x7f, 0x00, 0x07, 0x02, 0x02, 0x02,
|
||||
0x02, 0x03, 0x81, 0x18, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x82, 0x18, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x83,
|
||||
0x18, 0x64, 0x21, 0x05, 0x19, 0xe5, 0x9c, 0x80, 0xe7, 0x0f, 0xa7,
|
||||
0xe9, 0xab, 0x72, 0x24, 0x30, 0x49, 0xfe, 0xb8, 0xde, 0xec, 0xc1,
|
||||
0x46, 0xb9, 0xb1, 0x84, 0x31, 0x04, 0x18, 0x8d, 0xa8, 0x0e, 0xb0,
|
||||
0x30, 0x90, 0xf6, 0x7c, 0xbf, 0x20, 0xeb, 0x43, 0xa1, 0x88, 0x00,
|
||||
0xf4, 0xff, 0x0a, 0xfd, 0x82, 0xff, 0x10, 0x12, 0x07, 0x19, 0x2b,
|
||||
0x95, 0xff, 0xc8, 0xda, 0x78, 0x63, 0x10, 0x11, 0xed, 0x6b, 0x24,
|
||||
0xcd, 0xd5, 0x73, 0xf9, 0x77, 0xa1, 0x1e, 0x79, 0x48, 0x11, 0x85,
|
||||
0x18, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0x99, 0xde, 0xf8, 0x36, 0x14, 0x6b, 0xc9, 0xb1, 0xb4,
|
||||
0xd2, 0x28, 0x31, 0x86, 0x31, 0x04, 0x4d, 0x28, 0x34, 0x67, 0xb5,
|
||||
0x43, 0xfd, 0x84, 0x22, 0x09, 0xbd, 0xd2, 0xd6, 0x26, 0x27, 0x2d,
|
||||
0x53, 0xa7, 0xdf, 0x52, 0x8f, 0xc2, 0xde, 0x7c, 0x9a, 0xcd, 0x1f,
|
||||
0xf2, 0x10, 0x42, 0x7c, 0x13, 0x44, 0x03, 0xb0, 0xa5, 0xdf, 0x8a,
|
||||
0xd4, 0x59, 0xd1, 0x86, 0x4b, 0xde, 0x33, 0xb1, 0x60, 0x17, 0x87,
|
||||
0x01, 0x01, 0x5f, 0x20, 0x0e, 0x45, 0x53, 0x48, 0x53, 0x4d, 0x43,
|
||||
0x56, 0x43, 0x41, 0x32, 0x30, 0x34, 0x30, 0x31, 0x7f, 0x4c, 0x12,
|
||||
0x06, 0x09, 0x04, 0x00, 0x7f, 0x00, 0x07, 0x03, 0x01, 0x02, 0x02,
|
||||
0x53, 0x05, 0xc0, 0x00, 0x00, 0x00, 0x04, 0x5f, 0x25, 0x06, 0x02,
|
||||
0x02, 0x00, 0x02, 0x01, 0x09, 0x5f, 0x24, 0x06, 0x03, 0x00, 0x01,
|
||||
0x02, 0x03, 0x01, 0x5f, 0x37, 0x30, 0x26, 0x2d, 0x6f, 0xa6, 0xd0,
|
||||
0x52, 0x01, 0xf1, 0x41, 0x1e, 0xe9, 0x33, 0x29, 0x19, 0x42, 0x42,
|
||||
0x9b, 0xb0, 0xeb, 0xf7, 0x46, 0x20, 0xcb, 0x81, 0xfe, 0xda, 0xd7,
|
||||
0xab, 0x2b, 0xdc, 0xa7, 0x38, 0xf4, 0xc8, 0xec, 0x4c, 0x66, 0xb4,
|
||||
0x0a, 0x2d, 0x16, 0xfb, 0xf3, 0x79, 0xe9, 0x93, 0xc8, 0x25
|
||||
};
|
||||
const uint8_t token_info[] = {
|
||||
0x28, 0x00, //litle endian
|
||||
0x30, 0x26, 0x2, 0x1, 0x1, 0x4, 0x4, 0xd, 0x0, 0x0, 0x0, 0xc, 0xd, 0x50, 0x6f, 0x6c, 0x20, 0x48, 0x65, 0x6e, 0x61, 0x72, 0x65, 0x6a, 0x6f, 0x73, 0x80, 0x8, 0x48, 0x53, 0x4d, 0x20, 0x32, 0x30, 0x34, 0x30, 0x3, 0x2, 0x4, 0xf0
|
||||
};
|
||||
|
||||
extern const uint8_t sc_hsm_aid[];
|
||||
extern int parse_token_info(const file_t *f, int mode);
|
||||
|
||||
file_t file_entries[] = {
|
||||
/* 0 */ { .fid = 0x3f00 , .parent = 0xff, .name = NULL, .type = FILE_TYPE_DF, .data = NULL, .ef_structure = 0, .acl = {0} }, // MF
|
||||
/* 1 */ { .fid = 0x2f00 , .parent = 0, .name = NULL, .type = FILE_TYPE_WORKING_EF, .data = NULL, .ef_structure = FILE_EF_TRANSPARENT, .acl = {0} }, //EF.DIR
|
||||
/* 2 */ { .fid = 0x2f01 , .parent = 0, .name = NULL, .type = FILE_TYPE_WORKING_EF, .data = NULL, .ef_structure = FILE_EF_TRANSPARENT, .acl = {0} }, //EF.ATR
|
||||
/* 3 */ { .fid = 0x2f02 , .parent = 0, .name = NULL, .type = FILE_TYPE_WORKING_EF,.data = (uint8_t *)cvca, .ef_structure = FILE_EF_TRANSPARENT, .acl = {0} }, //EF.GDO
|
||||
/* 4 */ { .fid = 0x2f03 , .parent = 5, .name = NULL, .type = FILE_TYPE_WORKING_EF | FILE_DATA_FUNC,.data = (uint8_t *)parse_token_info, .ef_structure = FILE_EF_TRANSPARENT, .acl = {0} }, //EF.TokenInfo
|
||||
/* 5 */ { .fid = 0x5015 , .parent = 0, .name = NULL, .type = FILE_TYPE_DF, .data = NULL, .ef_structure = 0, .acl = {0} }, //DF.PKCS15
|
||||
/* 6 */ { .fid = 0x5031 , .parent = 5, .name = NULL, .type = FILE_TYPE_WORKING_EF, .data = NULL, .ef_structure = FILE_EF_TRANSPARENT, .acl = {0} }, //EF.ODF
|
||||
/* 7 */ { .fid = 0x5032 , .parent = 5, .name = NULL, .type = FILE_TYPE_WORKING_EF, .data = NULL, .ef_structure = FILE_EF_TRANSPARENT, .acl = {0} }, //EF.TokenInfo
|
||||
/* 8 */ { .fid = 0x5033 , .parent = 0, .name = NULL, .type = FILE_TYPE_WORKING_EF, .data = NULL, .ef_structure = FILE_EF_TRANSPARENT, .acl = {0} }, //EF.UnusedSpace
|
||||
/* 9 */ { .fid = 0x1081 , .parent = 5, .name = NULL, .type = FILE_TYPE_INTERNAL_EF | FILE_DATA_FLASH, .data = NULL, .ef_structure = FILE_EF_TRANSPARENT, .acl = {0xff} }, //PIN (PIN1)
|
||||
/* 10 */ { .fid = 0x1082 , .parent = 5, .name = NULL, .type = FILE_TYPE_INTERNAL_EF | FILE_DATA_FLASH, .data = NULL, .ef_structure = FILE_EF_TRANSPARENT, .acl = {0xff} }, //max retries PIN (PIN1)
|
||||
/* 11 */ { .fid = 0x1083 , .parent = 5, .name = NULL, .type = FILE_TYPE_INTERNAL_EF | FILE_DATA_FLASH, .data = NULL, .ef_structure = FILE_EF_TRANSPARENT, .acl = {0xff} }, //retries PIN (PIN1)
|
||||
/* 12 */ { .fid = 0x1088 , .parent = 5, .name = NULL, .type = FILE_TYPE_INTERNAL_EF | FILE_DATA_FLASH, .data = NULL, .ef_structure = FILE_EF_TRANSPARENT, .acl = {0xff} }, //PIN (SOPIN)
|
||||
/* 13 */ { .fid = 0x1089 , .parent = 5, .name = NULL, .type = FILE_TYPE_INTERNAL_EF | FILE_DATA_FLASH, .data = NULL, .ef_structure = FILE_EF_TRANSPARENT, .acl = {0xff} }, //max retries PIN (SOPIN)
|
||||
/* 14 */ { .fid = 0x108A , .parent = 5, .name = NULL, .type = FILE_TYPE_INTERNAL_EF | FILE_DATA_FLASH, .data = NULL, .ef_structure = FILE_EF_TRANSPARENT, .acl = {0xff} }, //retries PIN (SOPIN)
|
||||
/* 15 */ { .fid = EF_DKEK , .parent = 5, .name = NULL, .type = FILE_TYPE_INTERNAL_EF | FILE_DATA_FLASH, .data = NULL, .ef_structure = FILE_EF_TRANSPARENT, .acl = {0xff} }, //DKEK
|
||||
/* 16 */ { .fid = EF_PRKDFS , .parent = 5, .name = NULL, .type = FILE_TYPE_WORKING_EF, .data = NULL, .ef_structure = FILE_EF_TRANSPARENT, .acl = {0} }, //EF.PrKDFs
|
||||
/* 17 */ { .fid = EF_PUKDFS , .parent = 5, .name = NULL, .type = FILE_TYPE_WORKING_EF, .data = NULL, .ef_structure = FILE_EF_TRANSPARENT, .acl = {0} }, //EF.PuKDFs
|
||||
/* 18 */ { .fid = EF_CDFS , .parent = 5, .name = NULL, .type = FILE_TYPE_WORKING_EF, .data = NULL, .ef_structure = FILE_EF_TRANSPARENT, .acl = {0} }, //EF.CDFs
|
||||
/* 19 */ { .fid = EF_AODFS , .parent = 5, .name = NULL, .type = FILE_TYPE_WORKING_EF, .data = NULL, .ef_structure = FILE_EF_TRANSPARENT, .acl = {0} }, //EF.AODFs
|
||||
/* 20 */ { .fid = EF_DODFS , .parent = 5, .name = NULL, .type = FILE_TYPE_WORKING_EF, .data = NULL, .ef_structure = FILE_EF_TRANSPARENT, .acl = {0} }, //EF.DODFs
|
||||
/* 21 */ { .fid = EF_SKDFS , .parent = 5, .name = NULL, .type = FILE_TYPE_WORKING_EF, .data = NULL, .ef_structure = FILE_EF_TRANSPARENT, .acl = {0} }, //EF.SKDFs
|
||||
///* 22 */ { .fid = 0x0000, .parent = 0, .name = openpgpcard_aid, .type = FILE_TYPE_WORKING_EF, .data = NULL, .ef_structure = FILE_EF_TRANSPARENT, .acl = {0} },
|
||||
/* 23 */ { .fid = 0x0000, .parent = 5, .name = sc_hsm_aid, .type = FILE_TYPE_WORKING_EF, .data = NULL, .ef_structure = FILE_EF_TRANSPARENT, .acl = {0} },
|
||||
/* 24 */ { .fid = 0x0000, .parent = 0xff, .name = NULL, .type = FILE_TYPE_UNKNOWN, .data = NULL, .ef_structure = 0, .acl = {0} } //end
|
||||
};
|
||||
|
||||
const file_t *MF = &file_entries[0];
|
||||
const file_t *file_last = &file_entries[sizeof(file_entries)/sizeof(file_t)-1];
|
||||
const file_t *file_openpgp = &file_entries[sizeof(file_entries)/sizeof(file_t)-3];
|
||||
const file_t *file_sc_hsm = &file_entries[sizeof(file_entries)/sizeof(file_t)-2];
|
||||
file_t *file_pin1 = NULL;
|
||||
file_t *file_retries_pin1 = NULL;
|
||||
file_t *file_sopin = NULL;
|
||||
file_t *file_retries_sopin = NULL;
|
||||
|
||||
#define MAX_DYNAMIC_FILES 64
|
||||
uint16_t dynamic_files = 0;
|
||||
file_t dynamic_file[MAX_DYNAMIC_FILES];
|
||||
|
||||
bool card_terminated = false;
|
||||
|
||||
bool is_parent(const file_t *child, const file_t *parent) {
|
||||
if (child == parent)
|
||||
return true;
|
||||
if (child == MF)
|
||||
return false;
|
||||
return is_parent(&file_entries[child->parent], parent);
|
||||
}
|
||||
|
||||
file_t *get_parent(file_t *f) {
|
||||
return &file_entries[f->parent];
|
||||
}
|
||||
|
||||
file_t *search_by_name(uint8_t *name, uint16_t namelen) {
|
||||
for (file_t *p = file_entries; p != file_last; p++) {
|
||||
if (p->name && *p->name == apdu.cmd_apdu_data_len && memcmp(p->name+1, name, namelen) == 0) {
|
||||
return p;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
file_t *search_by_fid(const uint16_t fid, const file_t *parent, const uint8_t sp) {
|
||||
|
||||
for (file_t *p = file_entries; p != file_last; p++) {
|
||||
if (p->fid != 0x0000 && p->fid == fid) {
|
||||
if (!parent || (parent && is_parent(p, parent))) {
|
||||
if (!sp || sp == SPECIFY_ANY || (((sp & SPECIFY_EF) && (p->type & FILE_TYPE_INTERNAL_EF)) || ((sp & SPECIFY_DF) && p->type == FILE_TYPE_DF)))
|
||||
return p;
|
||||
}
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
uint8_t make_path_buf(const file_t *pe, uint8_t *buf, uint8_t buflen, const file_t *top) {
|
||||
if (!buflen)
|
||||
return 0;
|
||||
if (pe == top) //MF or relative DF
|
||||
return 0;
|
||||
put_uint16_t(pe->fid, buf);
|
||||
return make_path_buf(&file_entries[pe->parent], buf+2, buflen-2, top)+2;
|
||||
}
|
||||
|
||||
uint8_t make_path(const file_t *pe, const file_t *top, uint8_t *path) {
|
||||
uint8_t buf[MAX_DEPTH*2], *p = path;
|
||||
put_uint16_t(pe->fid, buf);
|
||||
uint8_t depth = make_path_buf(&file_entries[pe->parent], buf+2, sizeof(buf)-2, top)+2;
|
||||
for (int d = depth-2; d >= 0; d -= 2) {
|
||||
memcpy(p, buf+d, 2);
|
||||
p += 2;
|
||||
}
|
||||
return depth;
|
||||
}
|
||||
|
||||
file_t *search_by_path(const uint8_t *pe_path, uint8_t pathlen, const file_t *parent) {
|
||||
uint8_t path[MAX_DEPTH*2];
|
||||
if (pathlen > sizeof(path)) {
|
||||
return NULL;
|
||||
}
|
||||
for (file_t *p = file_entries; p != file_last; p++) {
|
||||
uint8_t depth = make_path(p, parent, path);
|
||||
if (pathlen == depth && memcmp(path, pe_path, depth) == 0)
|
||||
return p;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
file_t *currentEF = NULL;
|
||||
file_t *currentDF = NULL;
|
||||
const file_t *selected_applet = NULL;
|
||||
bool isUserAuthenticated = false;
|
||||
|
||||
bool authenticate_action(const file_t *ef, uint8_t op) {
|
||||
uint8_t acl = ef->acl[op];
|
||||
if (acl == 0x0)
|
||||
return true;
|
||||
else if (acl == 0xff)
|
||||
return false;
|
||||
else if (acl == 0x90 || acl & 0x9F == 0x10) {
|
||||
// PIN required.
|
||||
if(isUserAuthenticated) {
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
#include "libopensc/pkcs15.h"
|
||||
|
||||
void initialize_chain(file_chain_t **chain) {
|
||||
file_chain_t *next;
|
||||
for (file_chain_t *f = *chain; f; f = next) {
|
||||
next = f->next;
|
||||
free(f);
|
||||
}
|
||||
*chain = NULL;
|
||||
}
|
||||
|
||||
void initialize_flash(bool hard) {
|
||||
if (hard) {
|
||||
const uint8_t empty[8] = { 0 };
|
||||
flash_program_block(end_data_pool, empty, sizeof(empty));
|
||||
low_flash_available();
|
||||
}
|
||||
for (file_t *f = file_entries; f != file_last; f++) {
|
||||
if ((f->type & FILE_DATA_FLASH) == FILE_DATA_FLASH)
|
||||
f->data = NULL;
|
||||
}
|
||||
dynamic_files = 0;
|
||||
}
|
||||
|
||||
void scan_flash() {
|
||||
initialize_flash(false); //soft initialization
|
||||
if (*(uintptr_t *)end_data_pool == 0xffffffff && *(uintptr_t *)(end_data_pool+sizeof(uintptr_t)) == 0xffffffff)
|
||||
{
|
||||
printf("First initialization (or corrupted!)\r\n");
|
||||
const uint8_t empty[8] = { 0 };
|
||||
flash_program_block(end_data_pool, empty, sizeof(empty));
|
||||
//low_flash_available();
|
||||
//wait_flash_finish();
|
||||
}
|
||||
printf("SCAN\r\n");
|
||||
|
||||
uintptr_t base = flash_read_uintptr(end_data_pool);
|
||||
for (uintptr_t base = flash_read_uintptr(end_data_pool); base >= start_data_pool; base = flash_read_uintptr(base)) {
|
||||
if (base == 0x0) //all is empty
|
||||
break;
|
||||
|
||||
uint16_t fid = flash_read_uint16(base+sizeof(uintptr_t)+sizeof(uintptr_t));
|
||||
printf("[%x] scan fid %x, len %d\r\n",base,fid,flash_read_uint16(base+sizeof(uintptr_t)+sizeof(uintptr_t)+sizeof(uint16_t)));
|
||||
file_t *file = (file_t *)search_by_fid(fid, NULL, SPECIFY_EF);
|
||||
if (!file) {
|
||||
file = file_new(fid);
|
||||
if ((fid & 0xff00) == (KEY_PREFIX << 8)) {
|
||||
//add_file_to_chain(file, &ef_kf);
|
||||
}
|
||||
else if ((fid & 0xff00) == (PRKD_PREFIX << 8)) {
|
||||
//add_file_to_chain(file, &ef_prkdf);
|
||||
}
|
||||
else if ((fid & 0xff00) == (CD_PREFIX << 8)) {
|
||||
//add_file_to_chain(file, &ef_cdf);
|
||||
}
|
||||
else if ((fid & 0xff00) == (EE_CERTIFICATE_PREFIX << 8)) {
|
||||
//add_file_to_chain(file, &ef_pukdf);
|
||||
}
|
||||
else {
|
||||
TU_LOG1("SCAN FOUND ORPHAN FILE: %x\r\n",fid);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
file->data = (uint8_t *)(base+sizeof(uintptr_t)+sizeof(uintptr_t)+sizeof(uint16_t));
|
||||
if (flash_read_uintptr(base) == 0x0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
file_pin1 = search_by_fid(0x1081, NULL, SPECIFY_EF);
|
||||
if (file_pin1) {
|
||||
if (!file_pin1->data) {
|
||||
TU_LOG1("PIN1 is empty. Initializing with default password\r\n");
|
||||
const uint8_t empty[33] = { 0 };
|
||||
flash_write_data_to_file(file_pin1, empty, sizeof(empty));
|
||||
}
|
||||
}
|
||||
else {
|
||||
TU_LOG1("FATAL ERROR: PIN1 not found in memory!\r\n");
|
||||
}
|
||||
file_sopin = search_by_fid(0x1088, NULL, SPECIFY_EF);
|
||||
if (file_sopin) {
|
||||
if (!file_sopin->data) {
|
||||
TU_LOG1("SOPIN is empty. Initializing with default password\r\n");
|
||||
const uint8_t empty[33] = { 0 };
|
||||
flash_write_data_to_file(file_sopin, empty, sizeof(empty));
|
||||
}
|
||||
}
|
||||
else {
|
||||
TU_LOG1("FATAL ERROR: SOPIN not found in memory!\r\n");
|
||||
}
|
||||
file_retries_pin1 = search_by_fid(0x1083, NULL, SPECIFY_EF);
|
||||
if (file_retries_pin1) {
|
||||
if (!file_retries_pin1->data) {
|
||||
TU_LOG1("Retries PIN1 is empty. Initializing with default retriesr\n");
|
||||
const uint8_t retries = 3;
|
||||
flash_write_data_to_file(file_retries_pin1, &retries, sizeof(uint8_t));
|
||||
}
|
||||
}
|
||||
else {
|
||||
TU_LOG1("FATAL ERROR: Retries PIN1 not found in memory!\r\n");
|
||||
}
|
||||
file_retries_sopin = search_by_fid(0x108A, NULL, SPECIFY_EF);
|
||||
if (file_retries_sopin) {
|
||||
if (!file_retries_sopin->data) {
|
||||
TU_LOG1("Retries SOPIN is empty. Initializing with default retries\r\n");
|
||||
const uint8_t retries = 15;
|
||||
flash_write_data_to_file(file_retries_sopin, &retries, sizeof(uint8_t));
|
||||
}
|
||||
}
|
||||
else {
|
||||
TU_LOG1("FATAL ERROR: Retries SOPIN not found in memory!\r\n");
|
||||
}
|
||||
file_t *tf = NULL;
|
||||
|
||||
tf = search_by_fid(0x1082, NULL, SPECIFY_EF);
|
||||
if (tf) {
|
||||
if (!tf->data) {
|
||||
TU_LOG1("Max retries PIN1 is empty. Initializing with default max retriesr\n");
|
||||
const uint8_t retries = 3;
|
||||
flash_write_data_to_file(tf, &retries, sizeof(uint8_t));
|
||||
}
|
||||
}
|
||||
else {
|
||||
TU_LOG1("FATAL ERROR: Max Retries PIN1 not found in memory!\r\n");
|
||||
}
|
||||
tf = search_by_fid(0x1089, NULL, SPECIFY_EF);
|
||||
if (tf) {
|
||||
if (!tf->data) {
|
||||
TU_LOG1("Max Retries SOPIN is empty. Initializing with default max retries\r\n");
|
||||
const uint8_t retries = 15;
|
||||
flash_write_data_to_file(tf, &retries, sizeof(uint8_t));
|
||||
}
|
||||
}
|
||||
else {
|
||||
TU_LOG1("FATAL ERROR: Retries SOPIN not found in memory!\r\n");
|
||||
}
|
||||
low_flash_available();
|
||||
}
|
||||
|
||||
uint8_t *file_read(const uint8_t *addr) {
|
||||
return flash_read((uintptr_t)addr);
|
||||
}
|
||||
uint16_t file_read_uint16(const uint8_t *addr) {
|
||||
return flash_read_uint16((uintptr_t)addr);
|
||||
}
|
||||
uint8_t file_read_uint8(const uint8_t *addr) {
|
||||
return flash_read_uint8((uintptr_t)addr);
|
||||
}
|
||||
|
||||
file_t *search_dynamic_file(uint16_t fid) {
|
||||
for (int i = 0; i < dynamic_files; i++) {
|
||||
if (dynamic_file[i].fid == fid)
|
||||
return &dynamic_file[i];
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int delete_dynamic_file(file_t *f) {
|
||||
for (int i = 0; i < dynamic_files; i++) {
|
||||
if (dynamic_file[i].fid == f->fid) {
|
||||
for (int j = i+1; j < dynamic_files; j++)
|
||||
memcpy(&dynamic_file[j-1], &dynamic_file[j], sizeof(file_t));
|
||||
dynamic_files--;
|
||||
return HSM_OK;
|
||||
}
|
||||
}
|
||||
return HSM_ERR_FILE_NOT_FOUND;
|
||||
}
|
||||
|
||||
file_t *file_new(uint16_t fid) {
|
||||
file_t *f;
|
||||
if ((f = search_dynamic_file(fid)))
|
||||
return f;
|
||||
if (dynamic_files == MAX_DYNAMIC_FILES)
|
||||
return NULL;
|
||||
f = &dynamic_file[dynamic_files];
|
||||
dynamic_files++;
|
||||
file_t file = {
|
||||
.fid = fid,
|
||||
.parent = 5,
|
||||
.name = NULL,
|
||||
.type = FILE_TYPE_WORKING_EF,
|
||||
.ef_structure = FILE_EF_TRANSPARENT,
|
||||
.data = NULL,
|
||||
.acl = {0}
|
||||
};
|
||||
memcpy(f, &file, sizeof(file_t));
|
||||
//memset((uint8_t *)f->acl, 0x90, sizeof(f->acl));
|
||||
return f;
|
||||
}
|
||||
|
||||
file_chain_t *add_file_to_chain(file_t *file, file_chain_t **chain) {
|
||||
if (search_file_chain(file->fid, *chain))
|
||||
return NULL;
|
||||
file_chain_t *fc = (file_chain_t *)malloc(sizeof(file_chain_t));
|
||||
fc->file = file;
|
||||
fc->next = *chain;
|
||||
*chain = fc;
|
||||
return fc;
|
||||
}
|
||||
|
||||
file_t *search_file_chain(uint16_t fid, file_chain_t *chain) {
|
||||
for (file_chain_t *fc = chain; fc; fc = fc->next) {
|
||||
if (fid == fc->file->fid) {
|
||||
return fc->file;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
105
src/fs/file.h
105
src/fs/file.h
|
|
@ -1,105 +0,0 @@
|
|||
#ifndef _FILE_H_
|
||||
#define _FILE_H_
|
||||
|
||||
#include <stdlib.h>
|
||||
#include "pico/stdlib.h"
|
||||
|
||||
#define FILE_TYPE_UNKNOWN 0x00
|
||||
#define FILE_TYPE_DF 0x04
|
||||
#define FILE_TYPE_INTERNAL_EF 0x03
|
||||
#define FILE_TYPE_WORKING_EF 0x01
|
||||
#define FILE_TYPE_BSO 0x10
|
||||
#define FILE_PERSISTENT 0x20
|
||||
#define FILE_DATA_FLASH 0x40
|
||||
#define FILE_DATA_FUNC 0x80
|
||||
|
||||
/* EF structures */
|
||||
#define FILE_EF_UNKNOWN 0x00
|
||||
#define FILE_EF_TRANSPARENT 0x01
|
||||
#define FILE_EF_LINEAR_FIXED 0x02
|
||||
#define FILE_EF_LINEAR_FIXED_TLV 0x03
|
||||
#define FILE_EF_LINEAR_VARIABLE 0x04
|
||||
#define FILE_EF_LINEAR_VARIABLE_TLV 0x05
|
||||
#define FILE_EF_CYCLIC 0x06
|
||||
#define FILE_EF_CYCLIC_TLV 0x07
|
||||
|
||||
#define ACL_OP_DELETE_SELF 0x00
|
||||
#define ACL_OP_CREATE_DF 0x01
|
||||
#define ACL_OP_CREATE_EF 0x02
|
||||
#define ACL_OP_DELETE_CHILD 0x03
|
||||
#define ACL_OP_WRITE 0x04
|
||||
#define ACL_OP_UPDATE_ERASE 0x05
|
||||
#define ACL_OP_READ_SEARCH 0x06
|
||||
|
||||
#define SPECIFY_EF 0x1
|
||||
#define SPECIFY_DF 0x2
|
||||
#define SPECIFY_ANY 0x3
|
||||
|
||||
#define EF_DKEK 0x108F
|
||||
#define EF_PRKDFS 0x6040
|
||||
#define EF_PUKDFS 0x6041
|
||||
#define EF_CDFS 0x6042
|
||||
#define EF_AODFS 0x6043
|
||||
#define EF_DODFS 0x6044
|
||||
#define EF_SKDFS 0x6045
|
||||
|
||||
#define MAX_DEPTH 4
|
||||
|
||||
typedef struct file
|
||||
{
|
||||
const uint16_t fid;
|
||||
const uint8_t parent; //entry number in the whole table!!
|
||||
const uint8_t *name;
|
||||
const uint8_t type;
|
||||
const uint8_t ef_structure;
|
||||
uint8_t *data; //should include 2 bytes len at begining
|
||||
const uint8_t acl[7];
|
||||
} __attribute__((packed)) file_t;
|
||||
|
||||
typedef struct file_chain
|
||||
{
|
||||
file_t *file;
|
||||
struct file_chain *next;
|
||||
} file_chain_t;
|
||||
|
||||
extern file_t *currentEF;
|
||||
extern file_t *currentDF;
|
||||
extern const file_t *selected_applet;
|
||||
|
||||
extern const file_t *MF;
|
||||
extern const file_t *file_last;
|
||||
extern const file_t *file_openpgp;
|
||||
extern const file_t *file_sc_hsm;
|
||||
extern bool card_terminated;
|
||||
extern file_t *file_pin1;
|
||||
extern file_t *file_retries_pin1;
|
||||
extern file_t *file_sopin;
|
||||
extern file_t *file_retries_sopin;
|
||||
|
||||
extern file_t *search_by_fid(const uint16_t fid, const file_t *parent, const uint8_t sp);
|
||||
extern file_t *search_by_name(uint8_t *name, uint16_t namelen);
|
||||
extern file_t *search_by_path(const uint8_t *pe_path, uint8_t pathlen, const file_t *parent);
|
||||
extern bool authenticate_action(const file_t *ef, uint8_t op);
|
||||
extern void process_fci(const file_t *pe);
|
||||
extern void scan_flash();
|
||||
extern void initialize_flash(bool);
|
||||
|
||||
extern file_t file_entries[];
|
||||
|
||||
extern uint8_t *file_read(const uint8_t *addr);
|
||||
extern uint16_t file_read_uint16(const uint8_t *addr);
|
||||
extern uint8_t file_read_uint8(const uint8_t *addr);
|
||||
extern file_t *file_new(uint16_t);
|
||||
file_t *get_parent(file_t *f);
|
||||
|
||||
extern uint16_t dynamic_files;
|
||||
extern file_t dynamic_file[];
|
||||
extern file_t *search_dynamic_file(uint16_t);
|
||||
extern int delete_dynamic_file(file_t *f);
|
||||
|
||||
extern file_chain_t *add_file_to_chain(file_t *file, file_chain_t **chain);
|
||||
extern file_t *search_file_chain(uint16_t fid, file_chain_t *chain);
|
||||
extern bool isUserAuthenticated;
|
||||
|
||||
#endif
|
||||
|
||||
115
src/fs/flash.c
115
src/fs/flash.c
|
|
@ -1,115 +0,0 @@
|
|||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "pico/stdlib.h"
|
||||
#include "hardware/flash.h"
|
||||
#include "hsm2040.h"
|
||||
#include "tusb.h"
|
||||
#include "file.h"
|
||||
#include "sc_hsm.h"
|
||||
|
||||
/*
|
||||
* ------------------------------------------------------
|
||||
* | |
|
||||
* | next_addr | prev_addr | fid | data (len + payload) |
|
||||
* | |
|
||||
* ------------------------------------------------------
|
||||
*/
|
||||
#define FLASH_TARGET_OFFSET (PICO_FLASH_SIZE_BYTES >> 1) // DATA starts at the mid of flash
|
||||
#define FLASH_DATA_HEADER_SIZE (sizeof(uintptr_t)+sizeof(uint32_t))
|
||||
|
||||
//To avoid possible future allocations, data region starts at the end of flash and goes upwards to the center region
|
||||
|
||||
const uintptr_t start_data_pool = (XIP_BASE + FLASH_TARGET_OFFSET);
|
||||
const uintptr_t end_data_pool = (XIP_BASE + PICO_FLASH_SIZE_BYTES)-FLASH_DATA_HEADER_SIZE; //This is a fixed value. DO NOT CHANGE
|
||||
#define FLASH_ADDR_DATA_STORAGE_START start_data_pool
|
||||
|
||||
extern int flash_program_block(uintptr_t addr, const uint8_t *data, size_t len);
|
||||
extern int flash_program_halfword (uintptr_t addr, uint16_t data);
|
||||
extern int flash_program_uintptr(uintptr_t, uintptr_t);
|
||||
extern uintptr_t flash_read_uintptr(uintptr_t addr);
|
||||
extern uint16_t flash_read_uint16(uintptr_t addr);
|
||||
|
||||
extern void low_flash_available();
|
||||
|
||||
uintptr_t allocate_free_addr(uint16_t size) {
|
||||
if (size > FLASH_SECTOR_SIZE)
|
||||
return 0x0; //ERROR
|
||||
size_t real_size = size+sizeof(uint16_t)+sizeof(uintptr_t)+sizeof(uint16_t)+sizeof(uintptr_t); //len+len size+next address+fid+prev_addr size
|
||||
uintptr_t next_base = 0x0;
|
||||
for (uintptr_t base = end_data_pool; base >= start_data_pool; base = next_base) {
|
||||
uintptr_t addr_alg = base & -FLASH_SECTOR_SIZE; //start address of sector
|
||||
uintptr_t potential_addr = base-real_size;
|
||||
next_base = flash_read_uintptr(base);
|
||||
//printf("nb %x %x %x %x\r\n",base,next_base,addr_alg,potential_addr);
|
||||
//printf("fid %x\r\n",flash_read_uint16(next_base+sizeof(uintptr_t)));
|
||||
if (next_base == 0x0) { //we are at the end
|
||||
//now we check if we fit in the current sector
|
||||
if (addr_alg <= potential_addr) //it fits in the current sector
|
||||
{
|
||||
flash_program_uintptr(potential_addr, 0x0);
|
||||
flash_program_uintptr(potential_addr+sizeof(uintptr_t), base);
|
||||
flash_program_uintptr(base, potential_addr);
|
||||
return potential_addr;
|
||||
}
|
||||
else if (addr_alg-FLASH_SECTOR_SIZE >= start_data_pool) { //check whether it fits in the next sector, so we take addr_aligned as the base
|
||||
potential_addr = addr_alg-real_size;
|
||||
flash_program_uintptr(potential_addr, 0x0);
|
||||
flash_program_uintptr(potential_addr+sizeof(uintptr_t), base);
|
||||
flash_program_uintptr(base, potential_addr);
|
||||
return potential_addr;
|
||||
}
|
||||
return 0x0;
|
||||
}
|
||||
//we check if |base-(next_addr+size_next_addr)| > |base-potential_addr| only if fid != 1xxx (not size blocked)
|
||||
else if (addr_alg <= potential_addr && base-(next_base+flash_read_uint16(next_base+sizeof(uintptr_t)+sizeof(uintptr_t)+sizeof(uint16_t))+2*sizeof(uint16_t)+2*sizeof(uintptr_t)) > base-potential_addr && flash_read_uint16(next_base+sizeof(uintptr_t)) & 0x1000 != 0x1000) {
|
||||
flash_program_uintptr(potential_addr, next_base);
|
||||
flash_program_uintptr(potential_addr+sizeof(uintptr_t), base);
|
||||
flash_program_uintptr(base, potential_addr);
|
||||
return potential_addr;
|
||||
}
|
||||
}
|
||||
return 0x0; //probably never reached
|
||||
}
|
||||
|
||||
int flash_clear_file(file_t *file) {
|
||||
uintptr_t base_addr = (uintptr_t)(file->data-sizeof(uintptr_t)-sizeof(uint16_t)-sizeof(uintptr_t));
|
||||
uintptr_t prev_addr = flash_read_uintptr(base_addr+sizeof(uintptr_t));
|
||||
uintptr_t next_addr = flash_read_uintptr(base_addr);
|
||||
//printf("nc %x->%x %x->%x\r\n",prev_addr,flash_read_uintptr(prev_addr),base_addr,next_addr);
|
||||
flash_program_uintptr(prev_addr, next_addr);
|
||||
flash_program_halfword((uintptr_t)file->data, 0);
|
||||
if (next_addr > 0)
|
||||
flash_program_uintptr(next_addr+sizeof(uintptr_t), prev_addr);
|
||||
//printf("na %x->%x\r\n",prev_addr,flash_read_uintptr(prev_addr));
|
||||
return HSM_OK;
|
||||
}
|
||||
|
||||
int flash_write_data_to_file(file_t *file, const uint8_t *data, uint16_t len) {
|
||||
if (!file)
|
||||
return HSM_ERR_NULL_PARAM;
|
||||
if (len > FLASH_SECTOR_SIZE)
|
||||
return HSM_ERR_NO_MEMORY;
|
||||
if (file->data) { //already in flash
|
||||
uint16_t size_file_flash = flash_read_uint16((uintptr_t)file->data);
|
||||
if (len <= size_file_flash) { //it fits, no need to move it
|
||||
flash_program_halfword((uintptr_t)file->data, len);
|
||||
if (data)
|
||||
flash_program_block((uintptr_t)file->data+sizeof(uint16_t), data, len);
|
||||
return HSM_OK;
|
||||
}
|
||||
else { //we clear the old file
|
||||
flash_clear_file(file);
|
||||
}
|
||||
}
|
||||
uintptr_t new_addr = allocate_free_addr(len);
|
||||
//printf("na %x\r\n",new_addr);
|
||||
if (new_addr == 0x0)
|
||||
return HSM_ERR_NO_MEMORY;
|
||||
file->data = (uint8_t *)new_addr+sizeof(uintptr_t)+sizeof(uint16_t)+sizeof(uintptr_t); //next addr+fid+prev addr
|
||||
flash_program_halfword(new_addr+sizeof(uintptr_t)+sizeof(uintptr_t), file->fid);
|
||||
flash_program_halfword((uintptr_t)file->data, len);
|
||||
if (data)
|
||||
flash_program_block((uintptr_t)file->data+sizeof(uint16_t), data, len);
|
||||
return HSM_OK;
|
||||
}
|
||||
|
|
@ -1,242 +0,0 @@
|
|||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "pico/stdlib.h"
|
||||
#include "hardware/flash.h"
|
||||
#include "hardware/sync.h"
|
||||
#include "pico/mutex.h"
|
||||
#include "pico/sem.h"
|
||||
#include "pico/multicore.h"
|
||||
#include "hsm2040.h"
|
||||
#include "sc_hsm.h"
|
||||
#include <string.h>
|
||||
|
||||
#define TOTAL_FLASH_PAGES 4
|
||||
|
||||
typedef struct page_flash {
|
||||
uint8_t page[FLASH_SECTOR_SIZE];
|
||||
uintptr_t address;
|
||||
bool ready;
|
||||
bool erase;
|
||||
size_t page_size; //this param is for easy erase. It allows to erase with a single call. IT DOES NOT APPLY TO WRITE
|
||||
} page_flash_t;
|
||||
|
||||
static page_flash_t flash_pages[TOTAL_FLASH_PAGES];
|
||||
|
||||
static mutex_t mtx_flash;
|
||||
static semaphore_t sem_wait;
|
||||
|
||||
static uint8_t ready_pages = 0;
|
||||
|
||||
bool flash_available = false;
|
||||
static bool locked_out = false;
|
||||
|
||||
|
||||
//this function has to be called from the core 0
|
||||
void do_flash()
|
||||
{
|
||||
if (mutex_try_enter(&mtx_flash, NULL) == true)
|
||||
{
|
||||
if (locked_out == true && flash_available == true && ready_pages > 0)
|
||||
{
|
||||
//printf(" DO_FLASH AVAILABLE\r\n");
|
||||
for (int r = 0; r < TOTAL_FLASH_PAGES; r++)
|
||||
{
|
||||
if (flash_pages[r].ready == true)
|
||||
{
|
||||
//printf("WRITTING %X\r\n",flash_pages[r].address-XIP_BASE);
|
||||
while (multicore_lockout_start_timeout_us(1000) == false);
|
||||
//printf("WRITTING %X\r\n",flash_pages[r].address-XIP_BASE);
|
||||
uint32_t ints = save_and_disable_interrupts();
|
||||
flash_range_erase(flash_pages[r].address-XIP_BASE, FLASH_SECTOR_SIZE);
|
||||
flash_range_program(flash_pages[r].address-XIP_BASE, flash_pages[r].page, FLASH_SECTOR_SIZE);
|
||||
restore_interrupts (ints);
|
||||
while (multicore_lockout_end_timeout_us(1000) == false);
|
||||
//printf("WRITEN %X !\r\n",flash_pages[r].address);
|
||||
|
||||
flash_pages[r].ready = false;
|
||||
ready_pages--;
|
||||
}
|
||||
else if (flash_pages[r].erase == true)
|
||||
{
|
||||
while (multicore_lockout_start_timeout_us(1000) == false);
|
||||
//printf("WRITTING\r\n");
|
||||
flash_range_erase(flash_pages[r].address-XIP_BASE, flash_pages[r].page_size ? ((int)(flash_pages[r].page_size/FLASH_SECTOR_SIZE))*FLASH_SECTOR_SIZE : FLASH_SECTOR_SIZE);
|
||||
while (multicore_lockout_end_timeout_us(1000) == false);
|
||||
flash_pages[r].erase = false;
|
||||
ready_pages--;
|
||||
}
|
||||
}
|
||||
flash_available = false;
|
||||
if (ready_pages != 0) {
|
||||
DEBUG_INFO("ERROR: DO FLASH DOES NOT HAVE ZERO PAGES");
|
||||
}
|
||||
}
|
||||
mutex_exit(&mtx_flash);
|
||||
}
|
||||
sem_release(&sem_wait);
|
||||
}
|
||||
|
||||
//this function has to be called from the core 0
|
||||
void low_flash_init()
|
||||
{
|
||||
mutex_init(&mtx_flash);
|
||||
sem_init(&sem_wait, 0, 1);
|
||||
memset(flash_pages, 0, sizeof(page_flash_t)*TOTAL_FLASH_PAGES);
|
||||
}
|
||||
|
||||
void low_flash_init_core1() {
|
||||
mutex_enter_blocking(&mtx_flash);
|
||||
multicore_lockout_victim_init();
|
||||
locked_out = true;
|
||||
mutex_exit(&mtx_flash);
|
||||
}
|
||||
|
||||
void wait_flash_finish() {
|
||||
sem_acquire_blocking(&sem_wait); //blocks until released
|
||||
//wake up
|
||||
sem_acquire_blocking(&sem_wait); //decrease permits
|
||||
}
|
||||
|
||||
void low_flash_available()
|
||||
{
|
||||
mutex_enter_blocking(&mtx_flash);
|
||||
flash_available = true;
|
||||
mutex_exit(&mtx_flash);
|
||||
}
|
||||
|
||||
page_flash_t *find_free_page(uintptr_t addr) {
|
||||
uintptr_t addr_alg = addr & -FLASH_SECTOR_SIZE;
|
||||
page_flash_t *p = NULL;
|
||||
for (int r = 0; r < TOTAL_FLASH_PAGES; r++)
|
||||
{
|
||||
if ((!flash_pages[r].ready && !flash_pages[r].erase) || flash_pages[r].address == addr_alg) //first available
|
||||
{
|
||||
p = &flash_pages[r];
|
||||
if (!flash_pages[r].ready && !flash_pages[r].erase)
|
||||
{
|
||||
memcpy(p->page, (uint8_t *)addr_alg, FLASH_SECTOR_SIZE);
|
||||
ready_pages++;
|
||||
p->address = addr_alg;
|
||||
p->ready = true;
|
||||
}
|
||||
return p;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int flash_program_block(uintptr_t addr, const uint8_t *data, size_t len) {
|
||||
uintptr_t addr_alg = addr & -FLASH_SECTOR_SIZE;
|
||||
page_flash_t *p = NULL;
|
||||
|
||||
if (!data || len == 0)
|
||||
return HSM_ERR_NULL_PARAM;
|
||||
|
||||
mutex_enter_blocking(&mtx_flash);
|
||||
if (ready_pages == TOTAL_FLASH_PAGES) {
|
||||
mutex_exit(&mtx_flash);
|
||||
DEBUG_INFO("ERROR: ALL FLASH PAGES CACHED\r\n");
|
||||
return HSM_ERR_NO_MEMORY;
|
||||
}
|
||||
if (!(p = find_free_page(addr)))
|
||||
{
|
||||
DEBUG_INFO("ERROR: FLASH CANNOT FIND A PAGE (rare error)\r\n");
|
||||
mutex_exit(&mtx_flash);
|
||||
return HSM_ERR_MEMORY_FATAL;
|
||||
}
|
||||
memcpy(&p->page[addr&(FLASH_SECTOR_SIZE-1)], data, len);
|
||||
//printf("Flash: modified page %X with data %x at [%x] (top page %X)\r\n",addr_alg,data,addr&(FLASH_SECTOR_SIZE-1),addr);
|
||||
mutex_exit(&mtx_flash);
|
||||
return HSM_OK;
|
||||
}
|
||||
|
||||
int flash_program_halfword (uintptr_t addr, uint16_t data)
|
||||
{
|
||||
return flash_program_block(addr, (const uint8_t *)&data, sizeof(uint16_t));
|
||||
}
|
||||
|
||||
int flash_program_word (uintptr_t addr, uint32_t data)
|
||||
{
|
||||
return flash_program_block(addr, (const uint8_t *)&data, sizeof(uint32_t));
|
||||
}
|
||||
|
||||
int flash_program_uintptr (uintptr_t addr, uintptr_t data)
|
||||
{
|
||||
return flash_program_block(addr, (const uint8_t *)&data, sizeof(uintptr_t));
|
||||
}
|
||||
|
||||
uint8_t *flash_read(uintptr_t addr) {
|
||||
uintptr_t addr_alg = addr & -FLASH_SECTOR_SIZE;
|
||||
//mutex_enter_blocking(&mtx_flash);
|
||||
if (ready_pages > 0) {
|
||||
for (int r = 0; r < TOTAL_FLASH_PAGES; r++)
|
||||
{
|
||||
if (flash_pages[r].ready && flash_pages[r].address == addr_alg) {
|
||||
uint8_t *v = &flash_pages[r].page[addr&(FLASH_SECTOR_SIZE-1)];
|
||||
//mutex_exit(&mtx_flash);
|
||||
return v;
|
||||
}
|
||||
}
|
||||
}
|
||||
uint8_t *v = (uint8_t *)addr;
|
||||
//mutex_exit(&mtx_flash);
|
||||
return v;
|
||||
}
|
||||
|
||||
uintptr_t flash_read_uintptr(uintptr_t addr) {
|
||||
uint8_t *p = flash_read(addr);
|
||||
uintptr_t v = 0x0;
|
||||
for (int i = 0; i < sizeof(uintptr_t); i++) {
|
||||
v |= (uintptr_t)p[i]<<(8*i);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
uint16_t flash_read_uint16(uintptr_t addr) {
|
||||
uint8_t *p = flash_read(addr);
|
||||
uint16_t v = 0x0;
|
||||
for (int i = 0; i < sizeof(uint16_t); i++) {
|
||||
v |= p[i]<<(8*i);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
uint8_t flash_read_uint8(uintptr_t addr) {
|
||||
return *flash_read(addr);
|
||||
}
|
||||
|
||||
int flash_erase_page (uintptr_t addr, size_t page_size)
|
||||
{
|
||||
uintptr_t addr_alg = addr & -FLASH_SECTOR_SIZE;
|
||||
page_flash_t *p = NULL;
|
||||
|
||||
mutex_enter_blocking(&mtx_flash);
|
||||
if (ready_pages == TOTAL_FLASH_PAGES) {
|
||||
mutex_exit(&mtx_flash);
|
||||
DEBUG_INFO("ERROR: ALL FLASH PAGES CACHED\r\n");
|
||||
return HSM_ERR_NO_MEMORY;
|
||||
}
|
||||
if (!(p = find_free_page(addr)))
|
||||
{
|
||||
DEBUG_INFO("ERROR: FLASH CANNOT FIND A PAGE (rare error)\r\n");
|
||||
mutex_exit(&mtx_flash);
|
||||
return HSM_ERR_MEMORY_FATAL;
|
||||
}
|
||||
p->erase = true;
|
||||
p->ready = false;
|
||||
p->page_size = page_size;
|
||||
mutex_exit(&mtx_flash);
|
||||
|
||||
return HSM_OK;
|
||||
}
|
||||
|
||||
bool flash_check_blank (const uint8_t *p_start, size_t size)
|
||||
{
|
||||
const uint8_t *p;
|
||||
|
||||
for (p = p_start; p < p_start + size; p++)
|
||||
if (*p != 0xff)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
6
src/hsm/CMakeLists.txt
Normal file
6
src/hsm/CMakeLists.txt
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
idf_component_register(
|
||||
SRCS ${SOURCES}
|
||||
INCLUDE_DIRS . ../../pico-keys-sdk/src ../../pico-keys-sdk/src/fs ../../pico-keys-sdk/src/rng ../../pico-keys-sdk/src/usb
|
||||
REQUIRES mbedtls efuse
|
||||
)
|
||||
idf_component_set_property(${COMPONENT_NAME} WHOLE_ARCHIVE ON)
|
||||
326
src/hsm/cmd_bip_slip.c
Normal file
326
src/hsm/cmd_bip_slip.c
Normal file
|
|
@ -0,0 +1,326 @@
|
|||
/*
|
||||
* This file is part of the Pico HSM distribution (https://github.com/polhenarejos/pico-hsm).
|
||||
* Copyright (c) 2022 Pol Henarejos.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "sc_hsm.h"
|
||||
#include "files.h"
|
||||
#include "random.h"
|
||||
#include "kek.h"
|
||||
#include "asn1.h"
|
||||
|
||||
const uint8_t *k1_seed = (const uint8_t *) "Bitcoin seed";
|
||||
const uint8_t *p1_seed = (const uint8_t *) "Nist256p1 seed";
|
||||
const uint8_t *sym_seed = (const uint8_t *) "Symmetric key seed";
|
||||
mbedtls_ecp_keypair hd_context = { 0 };
|
||||
uint8_t hd_keytype = 0;
|
||||
|
||||
int node_derive_bip_child(const mbedtls_ecp_keypair *parent,
|
||||
const uint8_t cpar[32],
|
||||
const uint8_t *i,
|
||||
mbedtls_ecp_keypair *child,
|
||||
uint8_t cchild[32]) {
|
||||
uint8_t data[1 + 32 + 4], I[64], *iL = I, *iR = I + 32;
|
||||
mbedtls_mpi il, kchild;
|
||||
mbedtls_mpi_init(&il);
|
||||
mbedtls_mpi_init(&kchild);
|
||||
if (i[0] >= 0x80) {
|
||||
if (mbedtls_mpi_cmp_int(&parent->d, 0) == 0) {
|
||||
return PICOKEY_ERR_NULL_PARAM;
|
||||
}
|
||||
data[0] = 0x00;
|
||||
mbedtls_mpi_write_binary(&parent->d, data + 1, 32);
|
||||
}
|
||||
else {
|
||||
size_t olen = 0;
|
||||
mbedtls_ecp_point_write_binary(&parent->grp,
|
||||
&parent->Q,
|
||||
MBEDTLS_ECP_PF_COMPRESSED,
|
||||
&olen,
|
||||
data,
|
||||
33);
|
||||
}
|
||||
do {
|
||||
memcpy(data + 33, i, 4);
|
||||
mbedtls_md_hmac(mbedtls_md_info_from_type(MBEDTLS_MD_SHA512),
|
||||
cpar,
|
||||
32,
|
||||
data,
|
||||
sizeof(data),
|
||||
I);
|
||||
mbedtls_mpi_read_binary(&il, iL, 32);
|
||||
mbedtls_mpi_add_mpi(&kchild, &il, &parent->d);
|
||||
mbedtls_mpi_mod_mpi(&kchild, &kchild, &parent->grp.N);
|
||||
data[0] = 0x01;
|
||||
memcpy(data + 1, iR, 32);
|
||||
} while (mbedtls_mpi_cmp_mpi(&il,
|
||||
&parent->grp.N) != -1 || mbedtls_mpi_cmp_int(&kchild, 0) == 0);
|
||||
mbedtls_mpi_copy(&child->d, &kchild);
|
||||
mbedtls_ecp_mul(&child->grp, &child->Q, &child->d, &child->grp.G, random_gen, NULL);
|
||||
memcpy(cchild, iR, 32);
|
||||
mbedtls_mpi_free(&il);
|
||||
mbedtls_mpi_free(&kchild);
|
||||
return PICOKEY_OK;
|
||||
}
|
||||
|
||||
int sha256_ripemd160(const uint8_t *buffer, size_t buffer_len, uint8_t *output) {
|
||||
mbedtls_md(mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), buffer, buffer_len, output);
|
||||
mbedtls_md(mbedtls_md_info_from_type(MBEDTLS_MD_RIPEMD160), output, 32, output);
|
||||
return PICOKEY_OK;
|
||||
}
|
||||
|
||||
int sha256_sha256(const uint8_t *buffer, size_t buffer_len, uint8_t *output) {
|
||||
mbedtls_md(mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), buffer, buffer_len, output);
|
||||
mbedtls_md(mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), output, 32, output);
|
||||
return PICOKEY_OK;
|
||||
}
|
||||
|
||||
int node_fingerprint_bip(mbedtls_ecp_keypair *ctx, uint8_t fingerprint[4]) {
|
||||
size_t olen = 0;
|
||||
uint8_t buffer[33];
|
||||
mbedtls_ecp_point_write_binary(&ctx->grp,
|
||||
&ctx->Q,
|
||||
MBEDTLS_ECP_PF_COMPRESSED,
|
||||
&olen,
|
||||
buffer,
|
||||
sizeof(buffer));
|
||||
sha256_ripemd160(buffer, sizeof(buffer), buffer);
|
||||
memcpy(fingerprint, buffer, 4);
|
||||
return PICOKEY_OK;
|
||||
}
|
||||
|
||||
int node_fingerprint_slip(mbedtls_ecp_keypair *ctx, uint8_t fingerprint[4]) {
|
||||
uint8_t buffer[32];
|
||||
mbedtls_mpi_write_binary(&ctx->d, buffer, sizeof(buffer));
|
||||
sha256_ripemd160(buffer, sizeof(buffer), buffer);
|
||||
memcpy(fingerprint, buffer, 4);
|
||||
return PICOKEY_OK;
|
||||
}
|
||||
|
||||
int load_master_bip(uint16_t mid, mbedtls_ecp_keypair *ctx, uint8_t chain[32],
|
||||
uint8_t key_type[1]) {
|
||||
uint8_t mkey[65];
|
||||
mbedtls_ecp_keypair_init(ctx);
|
||||
file_t *ef = search_file(EF_MASTER_SEED | mid);
|
||||
if (!file_has_data(ef)) {
|
||||
return PICOKEY_ERR_FILE_NOT_FOUND;
|
||||
}
|
||||
memcpy(mkey, file_get_data(ef), sizeof(mkey));
|
||||
int r = mkek_decrypt(mkey + 1,
|
||||
sizeof(mkey) - 1);
|
||||
if (r != PICOKEY_OK) {
|
||||
return PICOKEY_EXEC_ERROR;
|
||||
}
|
||||
if (mkey[0] == 0x1 || mkey[0] == 0x2) {
|
||||
if (mkey[0] == 0x1) {
|
||||
mbedtls_ecp_group_load(&ctx->grp, MBEDTLS_ECP_DP_SECP256K1);
|
||||
}
|
||||
else if (mkey[0] == 0x2) {
|
||||
mbedtls_ecp_group_load(&ctx->grp, MBEDTLS_ECP_DP_SECP256R1);
|
||||
}
|
||||
else {
|
||||
return PICOKEY_WRONG_DATA;
|
||||
}
|
||||
|
||||
mbedtls_mpi_read_binary(&ctx->d, mkey + 1, 32);
|
||||
memcpy(chain, mkey + 33, 32);
|
||||
mbedtls_ecp_mul(&ctx->grp, &ctx->Q, &ctx->d, &ctx->grp.G, random_gen, NULL);
|
||||
}
|
||||
else if (mkey[0] == 0x3) {
|
||||
mbedtls_mpi_read_binary(&ctx->d, mkey + 33, 32);
|
||||
memcpy(chain, mkey + 1, 32);
|
||||
}
|
||||
key_type[0] = mkey[0];
|
||||
return PICOKEY_OK;
|
||||
}
|
||||
|
||||
int node_derive_path(const uint8_t *path,
|
||||
uint16_t path_len,
|
||||
mbedtls_ecp_keypair *ctx,
|
||||
uint8_t chain[32],
|
||||
uint8_t fingerprint[4],
|
||||
uint8_t *nodes,
|
||||
uint8_t last_node[4],
|
||||
uint8_t key_type[1]) {
|
||||
uint8_t *tag_data = NULL, *p = NULL;
|
||||
uint16_t tag_len = 0, tag = 0x0;
|
||||
uint8_t node = 0, N[64] = { 0 };
|
||||
int r = 0;
|
||||
memset(last_node, 0, 4);
|
||||
memset(fingerprint, 0, 4);
|
||||
|
||||
asn1_ctx_t ctxi;
|
||||
asn1_ctx_init((uint8_t *)path, path_len, &ctxi);
|
||||
for (; walk_tlv(&ctxi, &p, &tag, &tag_len, &tag_data); node++) {
|
||||
if (tag == 0x02) {
|
||||
if ((node == 0 && tag_len != 1) || (node != 0 && tag_len != 4)) {
|
||||
return PICOKEY_WRONG_DATA;
|
||||
}
|
||||
if (node == 0) {
|
||||
if ((r = load_master_bip(tag_data[0], ctx, chain, key_type)) != PICOKEY_OK) {
|
||||
return r;
|
||||
}
|
||||
}
|
||||
else if (node > 0) {
|
||||
node_fingerprint_bip(ctx, fingerprint);
|
||||
if ((r = node_derive_bip_child(ctx, chain, tag_data, ctx, chain)) != PICOKEY_OK) {
|
||||
return r;
|
||||
}
|
||||
memcpy(last_node, tag_data, 4);
|
||||
}
|
||||
}
|
||||
else if (tag == 0x04) {
|
||||
if (node == 0) {
|
||||
return PICOKEY_WRONG_DATA;
|
||||
}
|
||||
else if (node > 0) {
|
||||
node_fingerprint_slip(ctx, fingerprint);
|
||||
*(tag_data - 1) = 0;
|
||||
mbedtls_md_hmac(mbedtls_md_info_from_type(MBEDTLS_MD_SHA512),
|
||||
chain,
|
||||
32,
|
||||
tag_data - 1,
|
||||
tag_len + 1,
|
||||
N);
|
||||
memcpy(chain, N, 32);
|
||||
mbedtls_mpi_read_binary(&ctx->d, N + 32, 32);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (nodes) {
|
||||
*nodes = node;
|
||||
}
|
||||
return PICOKEY_OK;
|
||||
}
|
||||
|
||||
int cmd_bip_slip() {
|
||||
uint8_t p1 = P1(apdu), p2 = P2(apdu);
|
||||
if (p1 == 0x1 || p1 == 0x2 || p1 == 0x3) { // Master generation (K1 and P1)
|
||||
if (p2 >= 10) {
|
||||
return SW_INCORRECT_P1P2();
|
||||
}
|
||||
uint8_t mkey[65], *seed = mkey + 1, seed_len = 64;
|
||||
const uint8_t *key_seed = NULL;
|
||||
mbedtls_mpi il;
|
||||
mbedtls_mpi_init(&il);
|
||||
mbedtls_ecp_group grp;
|
||||
mbedtls_ecp_group_init(&grp);
|
||||
if (p1 == 0x1) {
|
||||
mbedtls_ecp_group_load(&grp, MBEDTLS_ECP_DP_SECP256K1);
|
||||
key_seed = k1_seed;
|
||||
}
|
||||
else if (p1 == 0x2) {
|
||||
mbedtls_ecp_group_load(&grp, MBEDTLS_ECP_DP_SECP256R1);
|
||||
key_seed = p1_seed;
|
||||
}
|
||||
else if (p1 == 0x3) {
|
||||
key_seed = sym_seed;
|
||||
}
|
||||
if (apdu.nc == 0) {
|
||||
seed_len = 64;
|
||||
random_gen(NULL, seed, seed_len);
|
||||
}
|
||||
else {
|
||||
seed_len = MIN((uint8_t)apdu.nc, 64);
|
||||
memcpy(seed, apdu.data, seed_len);
|
||||
}
|
||||
if (p1 == 0x1 || p1 == 0x2) {
|
||||
do {
|
||||
mbedtls_md_hmac(mbedtls_md_info_from_type(MBEDTLS_MD_SHA512), key_seed,
|
||||
strlen((char *) key_seed), seed, seed_len, seed);
|
||||
mbedtls_mpi_read_binary(&il, seed, 32);
|
||||
seed_len = 64;
|
||||
} while (mbedtls_mpi_cmp_int(&il, 0) == 0 || mbedtls_mpi_cmp_mpi(&il, &grp.N) != -1);
|
||||
mbedtls_ecp_group_free(&grp);
|
||||
mbedtls_mpi_free(&il);
|
||||
}
|
||||
else if (p1 == 0x3) {
|
||||
mbedtls_md_hmac(mbedtls_md_info_from_type(MBEDTLS_MD_SHA512), key_seed,
|
||||
strlen((char *) key_seed), seed, seed_len, seed);
|
||||
}
|
||||
mkey[0] = p1;
|
||||
file_t *ef = file_new(EF_MASTER_SEED | p2);
|
||||
int r = mkek_encrypt(mkey + 1, sizeof(mkey) - 1);
|
||||
if (r != PICOKEY_OK) {
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
r = file_put_data(ef, mkey, sizeof(mkey));
|
||||
if (r != PICOKEY_OK) {
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
low_flash_available();
|
||||
}
|
||||
else if (p1 == 0xA) {
|
||||
if (apdu.nc == 0) {
|
||||
return SW_WRONG_LENGTH();
|
||||
}
|
||||
mbedtls_ecp_keypair ctx;
|
||||
uint8_t chain[32] = { 0 }, fgpt[4] = { 0 }, last_node[4] = { 0 }, key_type = 0, nodes = 0;
|
||||
size_t olen = 0;
|
||||
int r =
|
||||
node_derive_path(apdu.data, (uint16_t)apdu.nc, &ctx, chain, fgpt, &nodes, last_node, &key_type);
|
||||
if (r != PICOKEY_OK) {
|
||||
mbedtls_ecp_keypair_free(&ctx);
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
uint8_t pubkey[33];
|
||||
res_APDU_size = 0;
|
||||
memcpy(res_APDU, "\x04\x88\xB2\x1E", 4);
|
||||
res_APDU_size += 4;
|
||||
res_APDU[res_APDU_size++] = nodes - 1;
|
||||
memcpy(res_APDU + res_APDU_size, fgpt, 4);
|
||||
res_APDU_size += 4;
|
||||
memcpy(res_APDU + res_APDU_size, last_node, 4);
|
||||
res_APDU_size += 4;
|
||||
if (key_type == 0x1 || key_type == 0x2) {
|
||||
memcpy(res_APDU + res_APDU_size, chain, 32);
|
||||
res_APDU_size += 32;
|
||||
mbedtls_ecp_point_write_binary(&ctx.grp,
|
||||
&ctx.Q,
|
||||
MBEDTLS_ECP_PF_COMPRESSED,
|
||||
&olen,
|
||||
pubkey,
|
||||
sizeof(pubkey));
|
||||
memcpy(res_APDU + res_APDU_size, pubkey, olen);
|
||||
res_APDU_size += (uint16_t)olen;
|
||||
}
|
||||
else if (key_type == 0x3) {
|
||||
sha256_sha256(chain, 32, chain);
|
||||
memcpy(res_APDU + res_APDU_size, chain, 32);
|
||||
res_APDU_size += 32;
|
||||
mbedtls_mpi_write_binary(&ctx.d, pubkey, 32);
|
||||
sha256_sha256(pubkey, 32, pubkey);
|
||||
memcpy(res_APDU + res_APDU_size, pubkey, 32);
|
||||
res_APDU_size += 32;
|
||||
}
|
||||
mbedtls_ecp_keypair_free(&ctx);
|
||||
}
|
||||
else if (p1 == 0x10) {
|
||||
uint8_t chain[32] = { 0 }, fgpt[4] = { 0 }, last_node[4] = { 0 }, nodes = 0;
|
||||
int r = node_derive_path(apdu.data,
|
||||
(uint16_t)apdu.nc,
|
||||
&hd_context,
|
||||
chain,
|
||||
fgpt,
|
||||
&nodes,
|
||||
last_node,
|
||||
&hd_keytype);
|
||||
if (r != PICOKEY_OK) {
|
||||
mbedtls_ecp_keypair_free(&hd_context);
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
}
|
||||
return SW_OK();
|
||||
}
|
||||
34
src/hsm/cmd_challenge.c
Normal file
34
src/hsm/cmd_challenge.c
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
/*
|
||||
* This file is part of the Pico HSM distribution (https://github.com/polhenarejos/pico-hsm).
|
||||
* Copyright (c) 2022 Pol Henarejos.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "random.h"
|
||||
#include "sc_hsm.h"
|
||||
|
||||
uint8_t challenge[256];
|
||||
uint8_t challenge_len = 0;
|
||||
|
||||
int cmd_challenge() {
|
||||
uint8_t *rb = (uint8_t *) random_bytes_get(apdu.ne);
|
||||
if (!rb) {
|
||||
return SW_WRONG_LENGTH();
|
||||
}
|
||||
memcpy(res_APDU, rb, apdu.ne);
|
||||
challenge_len = (uint8_t)MIN(apdu.ne, sizeof(challenge));
|
||||
memcpy(challenge, rb, challenge_len);
|
||||
res_APDU_size = (uint16_t)apdu.ne;
|
||||
return SW_OK();
|
||||
}
|
||||
72
src/hsm/cmd_change_pin.c
Normal file
72
src/hsm/cmd_change_pin.c
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
/*
|
||||
* This file is part of the Pico HSM distribution (https://github.com/polhenarejos/pico-hsm).
|
||||
* Copyright (c) 2022 Pol Henarejos.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "crypto_utils.h"
|
||||
#include "sc_hsm.h"
|
||||
#include "kek.h"
|
||||
|
||||
int cmd_change_pin() {
|
||||
if (P1(apdu) == 0x0) {
|
||||
if (P2(apdu) == 0x81 || P2(apdu) == 0x88) {
|
||||
file_t *file_pin = NULL;
|
||||
if (P2(apdu) == 0x81) {
|
||||
file_pin = file_pin1;
|
||||
}
|
||||
else if (P2(apdu) == 0x88) {
|
||||
file_pin = file_sopin;
|
||||
}
|
||||
if (!file_pin) {
|
||||
return SW_FILE_NOT_FOUND();
|
||||
}
|
||||
if (!file_has_data(file_pin)) {
|
||||
return SW_REFERENCE_NOT_FOUND();
|
||||
}
|
||||
uint8_t pin_len = file_read_uint8(file_pin);
|
||||
int r = check_pin(file_pin, apdu.data, pin_len);
|
||||
if (r != 0x9000) {
|
||||
return r;
|
||||
}
|
||||
uint8_t mkek[MKEK_SIZE];
|
||||
r = load_mkek(mkek); //loads the MKEK with old pin
|
||||
if (r != PICOKEY_OK) {
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
//encrypt MKEK with new pin
|
||||
|
||||
if (P2(apdu) == 0x81) {
|
||||
hash_multi(apdu.data + pin_len, (uint16_t)(apdu.nc - pin_len), session_pin);
|
||||
has_session_pin = true;
|
||||
}
|
||||
else if (P2(apdu) == 0x88) {
|
||||
hash_multi(apdu.data + pin_len, (uint16_t)(apdu.nc - pin_len), session_sopin);
|
||||
has_session_sopin = true;
|
||||
}
|
||||
r = store_mkek(mkek);
|
||||
release_mkek(mkek);
|
||||
if (r != PICOKEY_OK) {
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
uint8_t dhash[33];
|
||||
dhash[0] = (uint8_t)apdu.nc - pin_len;
|
||||
double_hash_pin(apdu.data + pin_len, (uint16_t)(apdu.nc - pin_len), dhash + 1);
|
||||
file_put_data(file_pin, dhash, sizeof(dhash));
|
||||
low_flash_available();
|
||||
return SW_OK();
|
||||
}
|
||||
}
|
||||
return SW_WRONG_P1P2();
|
||||
}
|
||||
704
src/hsm/cmd_cipher_sym.c
Normal file
704
src/hsm/cmd_cipher_sym.c
Normal file
|
|
@ -0,0 +1,704 @@
|
|||
/*
|
||||
* This file is part of the Pico HSM distribution (https://github.com/polhenarejos/pico-hsm).
|
||||
* Copyright (c) 2022 Pol Henarejos.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "sc_hsm.h"
|
||||
#include "mbedtls/aes.h"
|
||||
#include "mbedtls/cmac.h"
|
||||
#include "mbedtls/hkdf.h"
|
||||
#include "mbedtls/chachapoly.h"
|
||||
#include "mbedtls/gcm.h"
|
||||
//#include "mbedtls/md_wrap.h"
|
||||
#include "mbedtls/md.h"
|
||||
#include "crypto_utils.h"
|
||||
#include "kek.h"
|
||||
#include "asn1.h"
|
||||
#include "oid.h"
|
||||
#include "mbedtls/pkcs5.h"
|
||||
#include "mbedtls/error.h"
|
||||
#include "mbedtls/asn1.h"
|
||||
#include "mbedtls/cipher.h"
|
||||
#include "mbedtls/oid.h"
|
||||
#include "mbedtls/ccm.h"
|
||||
|
||||
extern mbedtls_ecp_keypair hd_context;
|
||||
extern uint8_t hd_keytype;
|
||||
|
||||
/* This is copied from pkcs5.c Mbedtls */
|
||||
/** Unfortunately it is declared as static, so I cannot call it. **/
|
||||
|
||||
static int pkcs5_parse_pbkdf2_params(const mbedtls_asn1_buf *params,
|
||||
mbedtls_asn1_buf *salt, int *iterations,
|
||||
uint16_t *keylen, mbedtls_md_type_t *md_type) {
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
mbedtls_asn1_buf prf_alg_oid;
|
||||
unsigned char *p = params->p;
|
||||
const unsigned char *end = params->p + params->len;
|
||||
|
||||
if (params->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
|
||||
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS5_INVALID_FORMAT,
|
||||
MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
|
||||
}
|
||||
/*
|
||||
* PBKDF2-params ::= SEQUENCE {
|
||||
* salt OCTET STRING,
|
||||
* iterationCount INTEGER,
|
||||
* keyLength INTEGER OPTIONAL
|
||||
* prf AlgorithmIdentifier DEFAULT algid-hmacWithSHA1
|
||||
* }
|
||||
*
|
||||
*/
|
||||
if ((ret = mbedtls_asn1_get_tag(&p, end, &salt->len,
|
||||
MBEDTLS_ASN1_OCTET_STRING)) != 0) {
|
||||
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS5_INVALID_FORMAT, ret);
|
||||
}
|
||||
|
||||
salt->p = p;
|
||||
p += salt->len;
|
||||
|
||||
if ((ret = mbedtls_asn1_get_int(&p, end, iterations)) != 0) {
|
||||
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS5_INVALID_FORMAT, ret);
|
||||
}
|
||||
|
||||
if (p == end) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if ((ret = mbedtls_asn1_get_int(&p, end, (int *)keylen)) != 0) {
|
||||
if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
|
||||
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS5_INVALID_FORMAT, ret);
|
||||
}
|
||||
}
|
||||
|
||||
if (p == end) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if ((ret = mbedtls_asn1_get_alg_null(&p, end, &prf_alg_oid)) != 0) {
|
||||
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS5_INVALID_FORMAT, ret);
|
||||
}
|
||||
|
||||
if (mbedtls_oid_get_md_hmac(&prf_alg_oid, md_type) != 0) {
|
||||
return MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE;
|
||||
}
|
||||
|
||||
if (p != end) {
|
||||
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS5_INVALID_FORMAT,
|
||||
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Taken from https://github.com/Mbed-TLS/mbedtls/issues/2335 */
|
||||
int mbedtls_ansi_x963_kdf(mbedtls_md_type_t md_type,
|
||||
uint16_t input_len,
|
||||
uint8_t *input,
|
||||
uint16_t shared_info_len,
|
||||
uint8_t *shared_info,
|
||||
uint16_t output_len,
|
||||
uint8_t *output) {
|
||||
mbedtls_md_context_t md_ctx;
|
||||
const mbedtls_md_info_t *md_info = NULL;
|
||||
int hashlen = 0, exit_code = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
|
||||
uint8_t counter_buf[4], tmp_output[64]; //worst case
|
||||
|
||||
mbedtls_md_init(&md_ctx);
|
||||
|
||||
md_info = mbedtls_md_info_from_type(md_type);
|
||||
|
||||
if (md_info == NULL) {
|
||||
return exit_code;
|
||||
}
|
||||
|
||||
if (mbedtls_md_setup(&md_ctx, md_info, 0)) {
|
||||
return exit_code;
|
||||
}
|
||||
|
||||
if (input_len + shared_info_len + 4 >= (1ULL << 61) - 1) {
|
||||
return exit_code;
|
||||
}
|
||||
|
||||
// keydatalen equals output_len
|
||||
hashlen = mbedtls_md_get_size(md_info);
|
||||
if (output_len >= hashlen * ((1ULL << 32) - 1)) {
|
||||
return exit_code;
|
||||
}
|
||||
|
||||
for (int i = 0, counter = 1; i < output_len; counter++) {
|
||||
mbedtls_md_starts(&md_ctx);
|
||||
mbedtls_md_update(&md_ctx, input, input_len);
|
||||
|
||||
//TODO: be careful with architecture little vs. big
|
||||
put_uint32_t_be(counter, counter_buf);
|
||||
|
||||
mbedtls_md_update(&md_ctx, counter_buf, 4);
|
||||
|
||||
if (shared_info_len > 0 && shared_info != NULL) {
|
||||
mbedtls_md_update(&md_ctx, shared_info, shared_info_len);
|
||||
}
|
||||
mbedtls_md_finish(&md_ctx, tmp_output);
|
||||
memcpy(&output[i], tmp_output, (output_len - i < hashlen) ? output_len - i : hashlen);
|
||||
i += hashlen;
|
||||
}
|
||||
mbedtls_md_free(&md_ctx);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int cmd_cipher_sym() {
|
||||
uint8_t key_id = P1(apdu), algo = P2(apdu);
|
||||
if (!isUserAuthenticated) {
|
||||
return SW_SECURITY_STATUS_NOT_SATISFIED();
|
||||
}
|
||||
if (wait_button_pressed() == true) { // timeout
|
||||
return SW_SECURE_MESSAGE_EXEC_ERROR();
|
||||
}
|
||||
file_t *ef = search_file((KEY_PREFIX << 8) | key_id);
|
||||
if (hd_keytype == 0) {
|
||||
if (!ef) {
|
||||
return SW_FILE_NOT_FOUND();
|
||||
}
|
||||
if (key_has_purpose(ef, algo) == false) {
|
||||
return SW_CONDITIONS_NOT_SATISFIED();
|
||||
}
|
||||
}
|
||||
uint16_t key_size = file_get_size(ef);
|
||||
uint8_t kdata[64]; //maximum AES key size
|
||||
memcpy(kdata, file_get_data(ef), key_size);
|
||||
if (hd_keytype == 0 && mkek_decrypt(kdata, key_size) != 0) {
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
if (algo == ALGO_AES_CBC_ENCRYPT || algo == ALGO_AES_CBC_DECRYPT) {
|
||||
if ((apdu.nc % 16) != 0) {
|
||||
return SW_WRONG_LENGTH();
|
||||
}
|
||||
mbedtls_aes_context aes;
|
||||
mbedtls_aes_init(&aes);
|
||||
uint8_t tmp_iv[IV_SIZE];
|
||||
memset(tmp_iv, 0, sizeof(tmp_iv));
|
||||
if (algo == ALGO_AES_CBC_ENCRYPT) {
|
||||
int r = mbedtls_aes_setkey_enc(&aes, kdata, key_size * 8);
|
||||
mbedtls_platform_zeroize(kdata, sizeof(kdata));
|
||||
if (r != 0) {
|
||||
mbedtls_aes_free(&aes);
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
r = mbedtls_aes_crypt_cbc(&aes,
|
||||
MBEDTLS_AES_ENCRYPT,
|
||||
apdu.nc,
|
||||
tmp_iv,
|
||||
apdu.data,
|
||||
res_APDU);
|
||||
mbedtls_aes_free(&aes);
|
||||
if (r != 0) {
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
}
|
||||
else if (algo == ALGO_AES_CBC_DECRYPT) {
|
||||
int r = mbedtls_aes_setkey_dec(&aes, kdata, key_size * 8);
|
||||
if (r != 0) {
|
||||
mbedtls_aes_free(&aes);
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
r = mbedtls_aes_crypt_cbc(&aes,
|
||||
MBEDTLS_AES_DECRYPT,
|
||||
apdu.nc,
|
||||
tmp_iv,
|
||||
apdu.data,
|
||||
res_APDU);
|
||||
mbedtls_aes_free(&aes);
|
||||
if (r != 0) {
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
}
|
||||
res_APDU_size = (uint16_t)apdu.nc;
|
||||
}
|
||||
else if (algo == ALGO_AES_CMAC) {
|
||||
const mbedtls_cipher_info_t *cipher_info;
|
||||
if (key_size == 16) {
|
||||
cipher_info = mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_128_ECB);
|
||||
}
|
||||
else if (key_size == 24) {
|
||||
cipher_info = mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_192_ECB);
|
||||
}
|
||||
else if (key_size == 32) {
|
||||
cipher_info = mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_256_ECB);
|
||||
}
|
||||
else {
|
||||
mbedtls_platform_zeroize(kdata, sizeof(kdata));
|
||||
return SW_WRONG_DATA();
|
||||
}
|
||||
int r = mbedtls_cipher_cmac(cipher_info, kdata, key_size * 8, apdu.data, apdu.nc, res_APDU);
|
||||
mbedtls_platform_zeroize(kdata, sizeof(kdata));
|
||||
if (r != 0) {
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
res_APDU_size = 16;
|
||||
}
|
||||
else if (algo == ALGO_AES_DERIVE) {
|
||||
int r = mbedtls_hkdf(mbedtls_md_info_from_type(MBEDTLS_MD_SHA256),
|
||||
NULL,
|
||||
0,
|
||||
kdata,
|
||||
key_size,
|
||||
apdu.data,
|
||||
apdu.nc,
|
||||
res_APDU,
|
||||
apdu.nc);
|
||||
mbedtls_platform_zeroize(kdata, sizeof(kdata));
|
||||
if (r != 0) {
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
res_APDU_size = (uint16_t)apdu.nc;
|
||||
}
|
||||
else if (algo == ALGO_EXT_CIPHER_ENCRYPT || algo == ALGO_EXT_CIPHER_DECRYPT) {
|
||||
asn1_ctx_t ctxi, oid = {0}, enc = {0}, iv = {0}, aad = {0};
|
||||
asn1_ctx_init(apdu.data, (uint16_t)apdu.nc, &ctxi);
|
||||
if (!asn1_find_tag(&ctxi, 0x6, &oid) || asn1_len(&oid) == 0) {
|
||||
mbedtls_platform_zeroize(kdata, sizeof(kdata));
|
||||
return SW_WRONG_DATA();
|
||||
}
|
||||
asn1_find_tag(&ctxi, 0x81, &enc);
|
||||
asn1_find_tag(&ctxi, 0x82, &iv);
|
||||
asn1_find_tag(&ctxi, 0x83, &aad);
|
||||
uint8_t tmp_iv[16];
|
||||
memset(tmp_iv, 0, sizeof(tmp_iv));
|
||||
if (memcmp(oid.data, OID_CHACHA20_POLY1305, oid.len) == 0) {
|
||||
if (algo == ALGO_EXT_CIPHER_DECRYPT && enc.len < 16) {
|
||||
mbedtls_platform_zeroize(kdata, sizeof(kdata));
|
||||
return SW_WRONG_DATA();
|
||||
}
|
||||
int r = 0;
|
||||
mbedtls_chachapoly_context ctx;
|
||||
mbedtls_chachapoly_init(&ctx);
|
||||
mbedtls_chachapoly_setkey(&ctx, kdata);
|
||||
if (algo == ALGO_EXT_CIPHER_ENCRYPT) {
|
||||
r = mbedtls_chachapoly_encrypt_and_tag(&ctx,
|
||||
enc.len,
|
||||
asn1_len(&iv) > 0 ? iv.data : tmp_iv,
|
||||
aad.data,
|
||||
aad.len,
|
||||
enc.data,
|
||||
res_APDU,
|
||||
res_APDU + enc.len);
|
||||
}
|
||||
else if (algo == ALGO_EXT_CIPHER_DECRYPT) {
|
||||
r = mbedtls_chachapoly_auth_decrypt(&ctx,
|
||||
enc.len - 16,
|
||||
asn1_len(&iv) > 0 ? iv.data : tmp_iv,
|
||||
aad.data,
|
||||
aad.len,
|
||||
enc.data + enc.len - 16,
|
||||
enc.data,
|
||||
res_APDU);
|
||||
}
|
||||
mbedtls_platform_zeroize(kdata, sizeof(kdata));
|
||||
mbedtls_chachapoly_free(&ctx);
|
||||
if (r != 0) {
|
||||
if (r == MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED) {
|
||||
return SW_WRONG_DATA();
|
||||
}
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
if (algo == ALGO_EXT_CIPHER_ENCRYPT) {
|
||||
res_APDU_size = enc.len + 16;
|
||||
}
|
||||
else if (algo == ALGO_EXT_CIPHER_DECRYPT) {
|
||||
res_APDU_size = enc.len - 16;
|
||||
}
|
||||
}
|
||||
else if (memcmp(oid.data, OID_DIGEST, 7) == 0) {
|
||||
const mbedtls_md_info_t *md_info = NULL;
|
||||
if (memcmp(oid.data, OID_HMAC_SHA1, oid.len) == 0) {
|
||||
md_info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA1);
|
||||
}
|
||||
else if (memcmp(oid.data, OID_HMAC_SHA224, oid.len) == 0) {
|
||||
md_info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA224);
|
||||
}
|
||||
else if (memcmp(oid.data, OID_HMAC_SHA256, oid.len) == 0) {
|
||||
md_info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA256);
|
||||
}
|
||||
else if (memcmp(oid.data, OID_HMAC_SHA384, oid.len) == 0) {
|
||||
md_info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA384);
|
||||
}
|
||||
else if (memcmp(oid.data, OID_HMAC_SHA512, oid.len) == 0) {
|
||||
md_info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA512);
|
||||
}
|
||||
if (md_info == NULL) {
|
||||
return SW_WRONG_DATA();
|
||||
}
|
||||
int r = mbedtls_md_hmac(md_info, kdata, key_size, enc.data, enc.len, res_APDU);
|
||||
mbedtls_platform_zeroize(kdata, sizeof(kdata));
|
||||
if (r != 0) {
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
res_APDU_size = mbedtls_md_get_size(md_info);
|
||||
}
|
||||
else if (memcmp(oid.data, OID_HKDF_SHA256,
|
||||
oid.len) == 0 ||
|
||||
memcmp(oid.data, OID_HKDF_SHA384,
|
||||
oid.len) == 0 || memcmp(oid.data, OID_HKDF_SHA512, oid.len) == 0) {
|
||||
const mbedtls_md_info_t *md_info = NULL;
|
||||
if (memcmp(oid.data, OID_HKDF_SHA256, oid.len) == 0) {
|
||||
md_info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA256);
|
||||
}
|
||||
else if (memcmp(oid.data, OID_HKDF_SHA384, oid.len) == 0) {
|
||||
md_info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA384);
|
||||
}
|
||||
else if (memcmp(oid.data, OID_HKDF_SHA512, oid.len) == 0) {
|
||||
md_info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA512);
|
||||
}
|
||||
int r = mbedtls_hkdf(md_info,
|
||||
iv.data,
|
||||
iv.len,
|
||||
kdata,
|
||||
key_size,
|
||||
enc.data,
|
||||
enc.len,
|
||||
res_APDU,
|
||||
apdu.ne > 0 &&
|
||||
apdu.ne < 65536 ? apdu.ne : mbedtls_md_get_size(md_info));
|
||||
mbedtls_platform_zeroize(kdata, sizeof(kdata));
|
||||
if (r != 0) {
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
res_APDU_size = apdu.ne > 0 && apdu.ne < 65536 ? (uint16_t)apdu.ne : (uint16_t)mbedtls_md_get_size(md_info);
|
||||
}
|
||||
else if (memcmp(oid.data, OID_PKCS5_PBKDF2, oid.len) == 0) {
|
||||
int iterations = 0;
|
||||
uint16_t keylen = 0;
|
||||
mbedtls_asn1_buf salt,
|
||||
params =
|
||||
{ .p = enc.data, .len = enc.len, .tag = (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE) };
|
||||
mbedtls_md_type_t md_type = MBEDTLS_MD_SHA1;
|
||||
|
||||
int r = pkcs5_parse_pbkdf2_params(¶ms, &salt, &iterations, &keylen, &md_type);
|
||||
if (r != 0) {
|
||||
mbedtls_platform_zeroize(kdata, sizeof(kdata));
|
||||
return SW_WRONG_DATA();
|
||||
}
|
||||
|
||||
r = mbedtls_pkcs5_pbkdf2_hmac_ext(md_type,
|
||||
kdata,
|
||||
key_size,
|
||||
salt.p,
|
||||
salt.len,
|
||||
iterations,
|
||||
keylen ? keylen : (apdu.ne > 0 &&
|
||||
apdu.ne < 65536 ? apdu.ne : 32),
|
||||
res_APDU);
|
||||
mbedtls_platform_zeroize(kdata, sizeof(kdata));
|
||||
if (r != 0) {
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
res_APDU_size = keylen ? keylen : (apdu.ne > 0 && apdu.ne < 65536 ? (uint16_t)apdu.ne : 32);
|
||||
}
|
||||
else if (memcmp(oid.data, OID_PKCS5_PBES2, oid.len) == 0) {
|
||||
size_t olen = 0;
|
||||
mbedtls_asn1_buf params =
|
||||
{.p = aad.data, .len = aad.len, .tag = (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)};
|
||||
int r = mbedtls_pkcs5_pbes2_ext(¶ms, algo == ALGO_EXT_CIPHER_ENCRYPT ? MBEDTLS_PKCS5_ENCRYPT : MBEDTLS_PKCS5_DECRYPT, kdata, key_size, enc.data, enc.len, res_APDU, MAX_APDU_DATA, &olen);
|
||||
mbedtls_platform_zeroize(kdata, sizeof(kdata));
|
||||
if (r != 0) {
|
||||
return SW_WRONG_DATA();
|
||||
}
|
||||
res_APDU_size = (uint16_t)olen;
|
||||
}
|
||||
else if (memcmp(oid.data, OID_KDF_X963, oid.len) == 0) {
|
||||
mbedtls_md_type_t md_type = MBEDTLS_MD_SHA1;
|
||||
if (memcmp(enc.data, OID_HMAC_SHA1, enc.len) == 0) {
|
||||
md_type = MBEDTLS_MD_SHA1;
|
||||
}
|
||||
else if (memcmp(enc.data, OID_HMAC_SHA224, enc.len) == 0) {
|
||||
md_type = MBEDTLS_MD_SHA224;
|
||||
}
|
||||
else if (memcmp(enc.data, OID_HMAC_SHA256, enc.len) == 0) {
|
||||
md_type = MBEDTLS_MD_SHA256;
|
||||
}
|
||||
else if (memcmp(enc.data, OID_HMAC_SHA384, enc.len) == 0) {
|
||||
md_type = MBEDTLS_MD_SHA384;
|
||||
}
|
||||
else if (memcmp(enc.data, OID_HMAC_SHA512, enc.len) == 0) {
|
||||
md_type = MBEDTLS_MD_SHA512;
|
||||
}
|
||||
int r = mbedtls_ansi_x963_kdf(md_type,
|
||||
key_size,
|
||||
kdata,
|
||||
aad.len,
|
||||
aad.data,
|
||||
apdu.ne > 0 && apdu.ne < 65536 ? (uint16_t)apdu.ne : 32,
|
||||
res_APDU);
|
||||
mbedtls_platform_zeroize(kdata, sizeof(kdata));
|
||||
if (r != 0) {
|
||||
return SW_WRONG_DATA();
|
||||
}
|
||||
res_APDU_size = apdu.ne > 0 && apdu.ne < 65536 ? (uint16_t)apdu.ne : 32;
|
||||
}
|
||||
else if (memcmp(oid.data, OID_NIST_AES, 8) == 0) {
|
||||
if (oid.len != 9) {
|
||||
return SW_WRONG_DATA();
|
||||
}
|
||||
uint8_t aes_algo = oid.data[8],
|
||||
mode =
|
||||
(algo == ALGO_EXT_CIPHER_ENCRYPT ? MBEDTLS_AES_ENCRYPT : MBEDTLS_AES_DECRYPT);
|
||||
if ((aes_algo >= 0x01 && aes_algo <= 0x09 && key_size != 16) ||
|
||||
(aes_algo >= 0x15 && aes_algo <= 0x1D && key_size != 24) ||
|
||||
(aes_algo >= 0x29 && aes_algo <= 0x31 && key_size != 32)) {
|
||||
return SW_WRONG_DATA();
|
||||
}
|
||||
mbedtls_aes_context ctx;
|
||||
int r = 0;
|
||||
mbedtls_aes_init(&ctx);
|
||||
if (asn1_len(&iv) == 0) {
|
||||
iv.data = tmp_iv;
|
||||
iv.len = sizeof(tmp_iv);
|
||||
}
|
||||
if (aes_algo == 0x01 || aes_algo == 0x15 || aes_algo == 0x29) { /* ECB */
|
||||
if (algo == ALGO_EXT_CIPHER_ENCRYPT) {
|
||||
r = mbedtls_aes_setkey_enc(&ctx, kdata, key_size * 8);
|
||||
}
|
||||
else if (algo == ALGO_EXT_CIPHER_DECRYPT) {
|
||||
r = mbedtls_aes_setkey_dec(&ctx, kdata, key_size * 8);
|
||||
}
|
||||
mbedtls_platform_zeroize(kdata, sizeof(kdata));
|
||||
r = mbedtls_aes_crypt_ecb(&ctx, mode, enc.data, res_APDU);
|
||||
mbedtls_aes_free(&ctx);
|
||||
if (r != 0) {
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
res_APDU_size = MIN(enc.len, 16); // ECB operates with 16-byte blocks
|
||||
}
|
||||
else if (aes_algo == 0x02 || aes_algo == 0x16 || aes_algo == 0x2A) { /* CBC */
|
||||
if (algo == ALGO_EXT_CIPHER_ENCRYPT) {
|
||||
r = mbedtls_aes_setkey_enc(&ctx, kdata, key_size * 8);
|
||||
}
|
||||
else if (algo == ALGO_EXT_CIPHER_DECRYPT) {
|
||||
r = mbedtls_aes_setkey_dec(&ctx, kdata, key_size * 8);
|
||||
}
|
||||
if (r != 0) {
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
mbedtls_platform_zeroize(kdata, sizeof(kdata));
|
||||
r = mbedtls_aes_crypt_cbc(&ctx, mode, enc.len, iv.data, enc.data, res_APDU);
|
||||
mbedtls_aes_free(&ctx);
|
||||
if (r != 0) {
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
res_APDU_size = enc.len;
|
||||
}
|
||||
else if (aes_algo == 0x03 || aes_algo == 0x17 || aes_algo == 0x2B) { /* OFB */
|
||||
size_t iv_off = 0;
|
||||
r = mbedtls_aes_setkey_enc(&ctx, kdata, key_size * 8);
|
||||
mbedtls_platform_zeroize(kdata, sizeof(kdata));
|
||||
r = mbedtls_aes_crypt_ofb(&ctx, enc.len, &iv_off, iv.data, enc.data, res_APDU);
|
||||
mbedtls_aes_free(&ctx);
|
||||
if (r != 0) {
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
res_APDU_size = enc.len;
|
||||
}
|
||||
else if (aes_algo == 0x04 || aes_algo == 0x18 || aes_algo == 0x2C) { /* CFB */
|
||||
size_t iv_off = 0;
|
||||
r = mbedtls_aes_setkey_enc(&ctx, kdata, key_size * 8);
|
||||
mbedtls_platform_zeroize(kdata, sizeof(kdata));
|
||||
r = mbedtls_aes_crypt_cfb128(&ctx, mode, enc.len, &iv_off, iv.data, enc.data, res_APDU);
|
||||
mbedtls_aes_free(&ctx);
|
||||
if (r != 0) {
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
res_APDU_size = enc.len;
|
||||
}
|
||||
else if (aes_algo == 0x06 || aes_algo == 0x1A || aes_algo == 0x2E) { /* GCM */
|
||||
mbedtls_aes_free(&ctx); // No AES ctx used
|
||||
mbedtls_gcm_context gctx;
|
||||
mbedtls_gcm_init(&gctx);
|
||||
r = mbedtls_gcm_setkey(&gctx, MBEDTLS_CIPHER_ID_AES, kdata, key_size * 8);
|
||||
mbedtls_platform_zeroize(kdata, sizeof(kdata));
|
||||
if (algo == ALGO_EXT_CIPHER_ENCRYPT) {
|
||||
r = mbedtls_gcm_crypt_and_tag(&gctx,
|
||||
MBEDTLS_GCM_ENCRYPT,
|
||||
enc.len,
|
||||
iv.data,
|
||||
iv.len,
|
||||
aad.data,
|
||||
aad.len,
|
||||
enc.data,
|
||||
res_APDU,
|
||||
16,
|
||||
res_APDU + enc.len);
|
||||
res_APDU_size = enc.len + 16;
|
||||
}
|
||||
else if (algo == ALGO_EXT_CIPHER_DECRYPT) {
|
||||
r = mbedtls_gcm_auth_decrypt(&gctx,
|
||||
enc.len - 16,
|
||||
iv.data,
|
||||
iv.len,
|
||||
aad.data,
|
||||
aad.len,
|
||||
enc.data + enc.len - 16,
|
||||
16,
|
||||
enc.data,
|
||||
res_APDU);
|
||||
res_APDU_size = enc.len - 16;
|
||||
}
|
||||
mbedtls_gcm_free(&gctx);
|
||||
if (r != 0) {
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
}
|
||||
else if (aes_algo == 0x09 || aes_algo == 0x1D || aes_algo == 0x31) { /* CTR */
|
||||
size_t iv_off = 0;
|
||||
uint8_t stream_block[16];
|
||||
r = mbedtls_aes_setkey_enc(&ctx, kdata, key_size * 8);
|
||||
mbedtls_platform_zeroize(kdata, sizeof(kdata));
|
||||
r = mbedtls_aes_crypt_ctr(&ctx, enc.len, &iv_off, iv.data, stream_block, enc.data, res_APDU);
|
||||
mbedtls_aes_free(&ctx);
|
||||
if (r != 0) {
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
res_APDU_size = enc.len;
|
||||
}
|
||||
else if (aes_algo == 0x07 || aes_algo == 0x1B || aes_algo == 0x2F) { /* CCM */
|
||||
mbedtls_aes_free(&ctx); // No AES ctx used
|
||||
mbedtls_ccm_context gctx;
|
||||
mbedtls_ccm_init(&gctx);
|
||||
r = mbedtls_ccm_setkey(&gctx, MBEDTLS_CIPHER_ID_AES, kdata, key_size * 8);
|
||||
if (r != 0) {
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
if (iv.len == 16) {
|
||||
iv.len = 12;
|
||||
}
|
||||
mbedtls_platform_zeroize(kdata, sizeof(kdata));
|
||||
if (algo == ALGO_EXT_CIPHER_ENCRYPT) {
|
||||
r = mbedtls_ccm_encrypt_and_tag(&gctx,
|
||||
enc.len,
|
||||
iv.data,
|
||||
iv.len,
|
||||
aad.data,
|
||||
aad.len,
|
||||
enc.data,
|
||||
res_APDU,
|
||||
res_APDU + enc.len,
|
||||
16);
|
||||
res_APDU_size = enc.len + 16;
|
||||
}
|
||||
else if (algo == ALGO_EXT_CIPHER_DECRYPT) {
|
||||
r = mbedtls_ccm_auth_decrypt(&gctx,
|
||||
enc.len - 16,
|
||||
iv.data,
|
||||
iv.len,
|
||||
aad.data,
|
||||
aad.len,
|
||||
enc.data,
|
||||
res_APDU,
|
||||
enc.data + enc.len - 16,
|
||||
16);
|
||||
res_APDU_size = enc.len - 16;
|
||||
}
|
||||
mbedtls_ccm_free(&gctx);
|
||||
if (r != 0) {
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (memcmp(oid.data, OID_IEEE_ALG, 8) == 0) {
|
||||
if (oid.len != 9) {
|
||||
return SW_WRONG_DATA();
|
||||
}
|
||||
uint8_t aes_algo = oid.data[8],
|
||||
mode =
|
||||
(algo == ALGO_EXT_CIPHER_ENCRYPT ? MBEDTLS_AES_ENCRYPT : MBEDTLS_AES_DECRYPT);
|
||||
int r = 0;
|
||||
memset(tmp_iv, 0, sizeof(tmp_iv));
|
||||
if (asn1_len(&iv) == 0) {
|
||||
iv.data = tmp_iv;
|
||||
iv.len = sizeof(tmp_iv);
|
||||
}
|
||||
if ((aes_algo == 0x01 && key_size != 32) || (aes_algo == 0x02 && key_size != 64)) {
|
||||
return SW_WRONG_DATA();
|
||||
}
|
||||
mbedtls_aes_xts_context ctx;
|
||||
mbedtls_aes_xts_init(&ctx);
|
||||
if (algo == ALGO_EXT_CIPHER_ENCRYPT) {
|
||||
r = mbedtls_aes_xts_setkey_enc(&ctx, kdata, key_size * 8);
|
||||
}
|
||||
else if (algo == ALGO_EXT_CIPHER_DECRYPT) {
|
||||
r = mbedtls_aes_xts_setkey_dec(&ctx, kdata, key_size * 8);
|
||||
}
|
||||
mbedtls_platform_zeroize(kdata, sizeof(kdata));
|
||||
r = mbedtls_aes_crypt_xts(&ctx, mode, enc.len, iv.data, enc.data, res_APDU);
|
||||
mbedtls_aes_xts_free(&ctx);
|
||||
if (r != 0) {
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
res_APDU_size = enc.len;
|
||||
}
|
||||
else if (memcmp(oid.data, OID_HD, 11) == 0) {
|
||||
mbedtls_aes_context ctx;
|
||||
int r = 0;
|
||||
uint8_t mode =
|
||||
(algo == ALGO_EXT_CIPHER_ENCRYPT ? MBEDTLS_AES_ENCRYPT : MBEDTLS_AES_DECRYPT),
|
||||
secret[64] = { 0 };
|
||||
mbedtls_aes_init(&ctx);
|
||||
if (hd_keytype != 0x3) {
|
||||
mbedtls_ecp_keypair_free(&hd_context);
|
||||
return SW_INCORRECT_PARAMS();
|
||||
}
|
||||
key_size = 32;
|
||||
mbedtls_mpi_write_binary(&hd_context.d, kdata, key_size);
|
||||
r = mbedtls_md_hmac(mbedtls_md_info_from_type(MBEDTLS_MD_SHA512),
|
||||
kdata,
|
||||
key_size,
|
||||
aad.data,
|
||||
aad.len,
|
||||
secret);
|
||||
mbedtls_platform_zeroize(kdata, sizeof(kdata));
|
||||
if (r != 0) {
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
if (iv.data == tmp_iv || iv.len == 0) {
|
||||
iv.data = secret + 32;
|
||||
iv.len = 16;
|
||||
}
|
||||
if (algo == ALGO_EXT_CIPHER_ENCRYPT) {
|
||||
r = mbedtls_aes_setkey_enc(&ctx, secret, key_size * 8);
|
||||
}
|
||||
else if (algo == ALGO_EXT_CIPHER_DECRYPT) {
|
||||
r = mbedtls_aes_setkey_dec(&ctx, secret, key_size * 8);
|
||||
}
|
||||
if (r != 0) {
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
r = mbedtls_aes_crypt_cbc(&ctx, mode, enc.len, iv.data, enc.data, res_APDU);
|
||||
mbedtls_aes_free(&ctx);
|
||||
if (r != 0) {
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
res_APDU_size = enc.len;
|
||||
mbedtls_ecp_keypair_free(&hd_context);
|
||||
hd_keytype = 0;
|
||||
}
|
||||
else {
|
||||
return SW_WRONG_DATA();
|
||||
}
|
||||
}
|
||||
else {
|
||||
mbedtls_platform_zeroize(kdata, sizeof(kdata));
|
||||
return SW_WRONG_P1P2();
|
||||
}
|
||||
return SW_OK();
|
||||
}
|
||||
197
src/hsm/cmd_decrypt_asym.c
Normal file
197
src/hsm/cmd_decrypt_asym.c
Normal file
|
|
@ -0,0 +1,197 @@
|
|||
/*
|
||||
* This file is part of the Pico HSM distribution (https://github.com/polhenarejos/pico-hsm).
|
||||
* Copyright (c) 2022 Pol Henarejos.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "sc_hsm.h"
|
||||
#include "mbedtls/ecdh.h"
|
||||
#include "crypto_utils.h"
|
||||
#include "kek.h"
|
||||
#include "files.h"
|
||||
#include "asn1.h"
|
||||
#include "cvc.h"
|
||||
#include "random.h"
|
||||
#include "oid.h"
|
||||
|
||||
int cmd_decrypt_asym() {
|
||||
uint8_t key_id = P1(apdu);
|
||||
uint8_t p2 = P2(apdu);
|
||||
if (!isUserAuthenticated) {
|
||||
return SW_SECURITY_STATUS_NOT_SATISFIED();
|
||||
}
|
||||
file_t *ef = search_file((KEY_PREFIX << 8) | key_id);
|
||||
if (!ef) {
|
||||
return SW_FILE_NOT_FOUND();
|
||||
}
|
||||
if (get_key_counter(ef) == 0) {
|
||||
return SW_FILE_FULL();
|
||||
}
|
||||
if (key_has_purpose(ef, p2) == false) {
|
||||
return SW_CONDITIONS_NOT_SATISFIED();
|
||||
}
|
||||
if (p2 >= ALGO_RSA_DECRYPT && p2 <= ALGO_RSA_DECRYPT_OEP) {
|
||||
mbedtls_rsa_context ctx;
|
||||
mbedtls_rsa_init(&ctx);
|
||||
if (p2 == ALGO_RSA_DECRYPT_OEP) {
|
||||
mbedtls_rsa_set_padding(&ctx, MBEDTLS_RSA_PKCS_V21, MBEDTLS_MD_SHA256);
|
||||
}
|
||||
int r = load_private_key_rsa(&ctx, ef);
|
||||
if (r != PICOKEY_OK) {
|
||||
mbedtls_rsa_free(&ctx);
|
||||
if (r == PICOKEY_VERIFICATION_FAILED) {
|
||||
return SW_SECURE_MESSAGE_EXEC_ERROR();
|
||||
}
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
uint16_t key_size = file_get_size(ef);
|
||||
if (apdu.nc < key_size) { //needs padding
|
||||
memset(apdu.data + apdu.nc, 0, key_size - apdu.nc);
|
||||
}
|
||||
if (p2 == ALGO_RSA_DECRYPT_PKCS1 || p2 == ALGO_RSA_DECRYPT_OEP) {
|
||||
size_t olen = apdu.nc;
|
||||
r = mbedtls_rsa_pkcs1_decrypt(&ctx, random_gen, NULL, &olen, apdu.data, res_APDU, 512);
|
||||
if (r == 0) {
|
||||
res_APDU_size = (uint16_t)olen;
|
||||
}
|
||||
}
|
||||
else {
|
||||
r = mbedtls_rsa_private(&ctx, random_gen, NULL, apdu.data, res_APDU);
|
||||
if (r == 0) {
|
||||
res_APDU_size = key_size;
|
||||
}
|
||||
}
|
||||
if (r != 0) {
|
||||
mbedtls_rsa_free(&ctx);
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
mbedtls_rsa_free(&ctx);
|
||||
}
|
||||
else if (p2 == ALGO_EC_DH || p2 == ALGO_EC_DH_XKEK) {
|
||||
mbedtls_ecdh_context ctx;
|
||||
if (wait_button_pressed() == true) { //timeout
|
||||
return SW_SECURE_MESSAGE_EXEC_ERROR();
|
||||
}
|
||||
uint16_t key_size = file_get_size(ef);
|
||||
uint8_t *kdata = (uint8_t *) calloc(1, key_size);
|
||||
memcpy(kdata, file_get_data(ef), key_size);
|
||||
if (mkek_decrypt(kdata, key_size) != 0) {
|
||||
mbedtls_platform_zeroize(kdata, key_size);
|
||||
free(kdata);
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
mbedtls_ecdh_init(&ctx);
|
||||
mbedtls_ecp_group_id gid = kdata[0];
|
||||
int r = 0;
|
||||
r = mbedtls_ecdh_setup(&ctx, gid);
|
||||
if (r != 0) {
|
||||
mbedtls_platform_zeroize(kdata, key_size);
|
||||
mbedtls_ecdh_free(&ctx);
|
||||
free(kdata);
|
||||
return SW_DATA_INVALID();
|
||||
}
|
||||
r = mbedtls_ecp_read_key(gid, (mbedtls_ecdsa_context *)&ctx.ctx.mbed_ecdh, kdata + 1, key_size - 1);
|
||||
mbedtls_platform_zeroize(kdata, key_size);
|
||||
free(kdata);
|
||||
if (r != 0) {
|
||||
mbedtls_ecdh_free(&ctx);
|
||||
return SW_DATA_INVALID();
|
||||
}
|
||||
r = -1;
|
||||
if (p2 == ALGO_EC_DH) {
|
||||
*(apdu.data - 1) = (uint8_t)apdu.nc;
|
||||
r = mbedtls_ecdh_read_public(&ctx, apdu.data - 1, apdu.nc + 1);
|
||||
}
|
||||
else if (p2 == ALGO_EC_DH_XKEK) {
|
||||
uint16_t pub_len = 0;
|
||||
const uint8_t *pub = cvc_get_pub(apdu.data, (uint16_t)apdu.nc, &pub_len);
|
||||
if (pub) {
|
||||
uint16_t t86_len = 0;
|
||||
const uint8_t *t86 = cvc_get_field(pub, pub_len, &t86_len, 0x86);
|
||||
uint8_t *t86w = (uint8_t *)t86;
|
||||
if (t86) {
|
||||
*(t86w - 1) = (uint8_t)t86_len;
|
||||
r = mbedtls_ecdh_read_public(&ctx, t86 - 1, t86_len + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (r != 0) {
|
||||
mbedtls_ecdh_free(&ctx);
|
||||
return SW_DATA_INVALID();
|
||||
}
|
||||
size_t olen = 0;
|
||||
// The SmartCard-HSM returns the point result of the DH operation
|
||||
// with a leading '04'
|
||||
res_APDU[0] = 0x04;
|
||||
r =
|
||||
mbedtls_ecdh_calc_secret(&ctx, &olen, res_APDU + 1, MBEDTLS_ECP_MAX_BYTES, random_gen,
|
||||
NULL);
|
||||
mbedtls_ecdh_free(&ctx);
|
||||
if (r != 0) {
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
if (p2 == ALGO_EC_DH) {
|
||||
res_APDU_size = (uint16_t)(olen + 1);
|
||||
}
|
||||
else {
|
||||
res_APDU_size = 0;
|
||||
uint16_t ext_len = 0;
|
||||
const uint8_t *ext = NULL;
|
||||
if ((ext = cvc_get_ext(apdu.data, (uint16_t)apdu.nc, &ext_len)) == NULL) {
|
||||
return SW_WRONG_DATA();
|
||||
}
|
||||
uint8_t *p = NULL;
|
||||
uint16_t tag = 0;
|
||||
asn1_ctx_t ctxi, ctxo = { 0 }, kdom_uid = { 0 };
|
||||
asn1_ctx_init((uint8_t *)ext, ext_len, &ctxi);
|
||||
while (walk_tlv(&ctxi, &p, &tag, &ctxo.len, &ctxo.data)) {
|
||||
if (tag == 0x73) {
|
||||
asn1_ctx_t oid = {0};
|
||||
if (asn1_find_tag(&ctxo, 0x6, &oid) == true &&
|
||||
oid.len == strlen(OID_ID_KEY_DOMAIN_UID) &&
|
||||
memcmp(oid.data, OID_ID_KEY_DOMAIN_UID,
|
||||
strlen(OID_ID_KEY_DOMAIN_UID)) == 0) {
|
||||
if (asn1_find_tag(&ctxo, 0x80, &kdom_uid) == false) {
|
||||
return SW_WRONG_DATA();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (asn1_len(&kdom_uid) == 0) {
|
||||
return SW_WRONG_DATA();
|
||||
}
|
||||
for (uint8_t n = 0; n < MAX_KEY_DOMAINS; n++) {
|
||||
file_t *tf = search_file(EF_XKEK + n);
|
||||
if (tf) {
|
||||
if (file_get_size(tf) == kdom_uid.len &&
|
||||
memcmp(file_get_data(tf), kdom_uid.data, kdom_uid.len) == 0) {
|
||||
file_new(EF_DKEK + n);
|
||||
if (store_dkek_key(n, res_APDU + 1) != PICOKEY_OK) {
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
mbedtls_platform_zeroize(res_APDU, 32);
|
||||
return SW_OK();
|
||||
}
|
||||
}
|
||||
}
|
||||
return SW_REFERENCE_NOT_FOUND();
|
||||
}
|
||||
}
|
||||
else {
|
||||
return SW_WRONG_P1P2();
|
||||
}
|
||||
decrement_key_counter(ef);
|
||||
return SW_OK();
|
||||
}
|
||||
45
src/hsm/cmd_delete_file.c
Normal file
45
src/hsm/cmd_delete_file.c
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
/*
|
||||
* This file is part of the Pico HSM distribution (https://github.com/polhenarejos/pico-hsm).
|
||||
* Copyright (c) 2022 Pol Henarejos.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "sc_hsm.h"
|
||||
|
||||
int cmd_delete_file() {
|
||||
file_t *ef = NULL;
|
||||
if (!isUserAuthenticated) {
|
||||
return SW_SECURITY_STATUS_NOT_SATISFIED();
|
||||
}
|
||||
|
||||
if (apdu.nc == 0) {
|
||||
ef = currentEF;
|
||||
if (!(ef = search_file(ef->fid))) {
|
||||
return SW_FILE_NOT_FOUND();
|
||||
}
|
||||
}
|
||||
else {
|
||||
uint16_t fid = get_uint16_t_be(apdu.data);
|
||||
if (!(ef = search_file(fid))) {
|
||||
return SW_FILE_NOT_FOUND();
|
||||
}
|
||||
}
|
||||
if (!authenticate_action(ef, ACL_OP_DELETE_SELF)) {
|
||||
return SW_SECURITY_STATUS_NOT_SATISFIED();
|
||||
}
|
||||
if (delete_file(ef) != PICOKEY_OK) {
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
return SW_OK();
|
||||
}
|
||||
101
src/hsm/cmd_derive_asym.c
Normal file
101
src/hsm/cmd_derive_asym.c
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
/*
|
||||
* This file is part of the Pico HSM distribution (https://github.com/polhenarejos/pico-hsm).
|
||||
* Copyright (c) 2022 Pol Henarejos.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "sc_hsm.h"
|
||||
#include "mbedtls/ecdsa.h"
|
||||
#include "crypto_utils.h"
|
||||
#include "cvc.h"
|
||||
|
||||
#define MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED -0x006E
|
||||
#define MOD_ADD(N) \
|
||||
while (mbedtls_mpi_cmp_mpi(&(N), &grp->P) >= 0) \
|
||||
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_abs(&(N), &(N), &grp->P))
|
||||
static inline int mbedtls_mpi_add_mod(const mbedtls_ecp_group *grp,
|
||||
mbedtls_mpi *X,
|
||||
const mbedtls_mpi *A,
|
||||
const mbedtls_mpi *B) {
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(X, A, B));
|
||||
MOD_ADD(*X);
|
||||
cleanup:
|
||||
return ret;
|
||||
}
|
||||
|
||||
int cmd_derive_asym() {
|
||||
uint8_t key_id = P1(apdu);
|
||||
uint8_t dest_id = P2(apdu);
|
||||
file_t *fkey;
|
||||
if (!isUserAuthenticated) {
|
||||
return SW_SECURITY_STATUS_NOT_SATISFIED();
|
||||
}
|
||||
if (!(fkey = search_file((KEY_PREFIX << 8) | key_id)) || !file_has_data(fkey)) {
|
||||
return SW_FILE_NOT_FOUND();
|
||||
}
|
||||
if (key_has_purpose(fkey, ALGO_EC_DERIVE) == false) {
|
||||
return SW_CONDITIONS_NOT_SATISFIED();
|
||||
}
|
||||
if (apdu.nc == 0) {
|
||||
return SW_WRONG_LENGTH();
|
||||
}
|
||||
if (apdu.data[0] == ALGO_EC_DERIVE) {
|
||||
mbedtls_ecp_keypair ctx;
|
||||
mbedtls_ecp_keypair_init(&ctx);
|
||||
|
||||
int r;
|
||||
r = load_private_key_ec(&ctx, fkey);
|
||||
if (r != PICOKEY_OK) {
|
||||
mbedtls_ecp_keypair_free(&ctx);
|
||||
if (r == PICOKEY_VERIFICATION_FAILED) {
|
||||
return SW_SECURE_MESSAGE_EXEC_ERROR();
|
||||
}
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
mbedtls_mpi a, nd;
|
||||
mbedtls_mpi_init(&a);
|
||||
mbedtls_mpi_init(&nd);
|
||||
r = mbedtls_mpi_read_binary(&a, apdu.data + 1, apdu.nc - 1);
|
||||
if (r != 0) {
|
||||
mbedtls_ecp_keypair_free(&ctx);
|
||||
mbedtls_mpi_free(&a);
|
||||
mbedtls_mpi_free(&nd);
|
||||
return SW_DATA_INVALID();
|
||||
}
|
||||
r = mbedtls_mpi_add_mod(&ctx.grp, &nd, &ctx.d, &a);
|
||||
mbedtls_mpi_free(&a);
|
||||
if (r != 0) {
|
||||
mbedtls_ecp_keypair_free(&ctx);
|
||||
mbedtls_mpi_free(&nd);
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
r = mbedtls_mpi_copy(&ctx.d, &nd);
|
||||
mbedtls_mpi_free(&nd);
|
||||
if (r != 0) {
|
||||
mbedtls_ecp_keypair_free(&ctx);
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
r = store_keys(&ctx, PICO_KEYS_KEY_EC, dest_id);
|
||||
if (r != PICOKEY_OK) {
|
||||
mbedtls_ecp_keypair_free(&ctx);
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
mbedtls_ecp_keypair_free(&ctx);
|
||||
}
|
||||
else {
|
||||
return SW_WRONG_DATA();
|
||||
}
|
||||
return SW_OK();
|
||||
}
|
||||
67
src/hsm/cmd_external_authenticate.c
Normal file
67
src/hsm/cmd_external_authenticate.c
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
/*
|
||||
* This file is part of the Pico HSM distribution (https://github.com/polhenarejos/pico-hsm).
|
||||
* Copyright (c) 2022 Pol Henarejos.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "crypto_utils.h"
|
||||
#include "sc_hsm.h"
|
||||
#include "cvc.h"
|
||||
#include "files.h"
|
||||
|
||||
extern file_t *ef_puk_aut;
|
||||
extern uint8_t challenge[256];
|
||||
extern uint8_t challenge_len;
|
||||
|
||||
int cmd_external_authenticate() {
|
||||
if (P1(apdu) != 0x0 || P2(apdu) != 0x0) {
|
||||
return SW_INCORRECT_P1P2();
|
||||
}
|
||||
if (ef_puk_aut == NULL) {
|
||||
return SW_REFERENCE_NOT_FOUND();
|
||||
}
|
||||
if (apdu.nc == 0) {
|
||||
return SW_WRONG_LENGTH();
|
||||
}
|
||||
file_t *ef_puk = search_file(EF_PUKAUT);
|
||||
if (!file_has_data(ef_puk)) {
|
||||
return SW_FILE_NOT_FOUND();
|
||||
}
|
||||
puk_status[ef_puk_aut->fid & (MAX_PUK - 1)] = 0;
|
||||
uint8_t *puk_data = file_get_data(ef_puk);
|
||||
uint8_t *input = (uint8_t *) calloc(dev_name_len + challenge_len, sizeof(uint8_t)), hash[32];
|
||||
memcpy(input, dev_name, dev_name_len);
|
||||
memcpy(input + dev_name_len, challenge, challenge_len);
|
||||
hash256(input, dev_name_len + challenge_len, hash);
|
||||
int r =
|
||||
puk_verify(apdu.data,
|
||||
(uint16_t)apdu.nc,
|
||||
hash,
|
||||
32,
|
||||
file_get_data(ef_puk_aut),
|
||||
file_get_size(ef_puk_aut));
|
||||
free(input);
|
||||
if (r != 0) {
|
||||
return SW_CONDITIONS_NOT_SATISFIED();
|
||||
}
|
||||
puk_status[ef_puk_aut->fid & (MAX_PUK - 1)] = 1;
|
||||
uint8_t auts = 0;
|
||||
for (int i = 0; i < puk_data[0]; i++) {
|
||||
auts += puk_status[i];
|
||||
}
|
||||
if (auts >= puk_data[2]) {
|
||||
isUserAuthenticated = true;
|
||||
}
|
||||
return SW_OK();
|
||||
}
|
||||
308
src/hsm/cmd_extras.c
Normal file
308
src/hsm/cmd_extras.c
Normal file
|
|
@ -0,0 +1,308 @@
|
|||
/*
|
||||
* This file is part of the Pico HSM distribution (https://github.com/polhenarejos/pico-hsm).
|
||||
* Copyright (c) 2022 Pol Henarejos.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "sc_hsm.h"
|
||||
#include "mbedtls/ecdh.h"
|
||||
#ifdef PICO_PLATFORM
|
||||
#include "pico/aon_timer.h"
|
||||
#include "hardware/watchdog.h"
|
||||
#else
|
||||
#include <sys/time.h>
|
||||
#include <time.h>
|
||||
#endif
|
||||
#include "files.h"
|
||||
#include "random.h"
|
||||
#include "kek.h"
|
||||
#include "mbedtls/hkdf.h"
|
||||
#include "mbedtls/chachapoly.h"
|
||||
#ifdef PICO_RP2350
|
||||
#include "otp.h"
|
||||
#endif
|
||||
|
||||
#define CMD_DATETIME 0xA
|
||||
#define CMD_DYNOPS 0x6
|
||||
#define CMD_SECURE_LOCK 0x3A
|
||||
#define CMD_REBOOT 0xFB
|
||||
#define SECURE_LOCK_KEY_AGREEMENT 0x1
|
||||
#define SECURE_LOCK_ENABLE 0x2
|
||||
#define SECURE_LOCK_MASK 0x3
|
||||
#define SECURE_LOCK_DISABLE 0x4
|
||||
#define CMD_PHY 0x1B
|
||||
#define CMD_OTP 0x4C
|
||||
#define CMD_MEMORY 0x5
|
||||
|
||||
int cmd_extras() {
|
||||
int cmd = P1(apdu);
|
||||
#ifndef ENABLE_EMULATION
|
||||
// Only allow change PHY without PIN
|
||||
if (!isUserAuthenticated && cmd != CMD_PHY && cmd != CMD_MEMORY) {
|
||||
return SW_SECURITY_STATUS_NOT_SATISFIED();
|
||||
}
|
||||
#endif
|
||||
//check button (if enabled)
|
||||
if (wait_button_pressed() == true) {
|
||||
return SW_SECURE_MESSAGE_EXEC_ERROR();
|
||||
}
|
||||
if (cmd == CMD_DATETIME) { //datetime operations
|
||||
if (P2(apdu) != 0x0) {
|
||||
return SW_INCORRECT_P1P2();
|
||||
}
|
||||
if (apdu.nc == 0) {
|
||||
#ifdef PICO_PLATFORM
|
||||
struct timespec tv;
|
||||
aon_timer_get_time(&tv);
|
||||
#else
|
||||
struct timeval tv;
|
||||
gettimeofday(&tv, NULL);
|
||||
#endif
|
||||
struct tm *tm = localtime(&tv.tv_sec);
|
||||
res_APDU_size += put_uint16_t_be(tm->tm_year + 1900, res_APDU);
|
||||
res_APDU[res_APDU_size++] = tm->tm_mon;
|
||||
res_APDU[res_APDU_size++] = tm->tm_mday;
|
||||
res_APDU[res_APDU_size++] = tm->tm_wday;
|
||||
res_APDU[res_APDU_size++] = tm->tm_hour;
|
||||
res_APDU[res_APDU_size++] = tm->tm_min;
|
||||
res_APDU[res_APDU_size++] = tm->tm_sec;
|
||||
}
|
||||
else {
|
||||
if (apdu.nc != 8) {
|
||||
return SW_WRONG_LENGTH();
|
||||
}
|
||||
struct tm tm;
|
||||
tm.tm_year = get_uint16_t_be(apdu.data) - 1900;
|
||||
tm.tm_mon = apdu.data[2];
|
||||
tm.tm_mday = apdu.data[3];
|
||||
tm.tm_wday = apdu.data[4];
|
||||
tm.tm_hour = apdu.data[5];
|
||||
tm.tm_min = apdu.data[6];
|
||||
tm.tm_sec = apdu.data[7];
|
||||
time_t tv_sec = mktime(&tm);
|
||||
#ifdef PICO_PLATFORM
|
||||
struct timespec tv = {.tv_sec = tv_sec, .tv_nsec = 0};
|
||||
aon_timer_set_time(&tv);
|
||||
#else
|
||||
struct timeval tv = {.tv_sec = tv_sec, .tv_usec = 0};
|
||||
settimeofday(&tv, NULL);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
else if (cmd == CMD_DYNOPS) { //dynamic options
|
||||
if (P2(apdu) != 0x0) {
|
||||
return SW_INCORRECT_P1P2();
|
||||
}
|
||||
if (apdu.nc > sizeof(uint8_t)) {
|
||||
return SW_WRONG_LENGTH();
|
||||
}
|
||||
uint16_t opts = get_device_options();
|
||||
if (apdu.nc == 0) {
|
||||
res_APDU_size += put_uint16_t_be(opts, res_APDU);
|
||||
}
|
||||
else {
|
||||
uint8_t newopts[] = { apdu.data[0], (opts & 0xff) };
|
||||
file_t *tf = search_file(EF_DEVOPS);
|
||||
file_put_data(tf, newopts, sizeof(newopts));
|
||||
low_flash_available();
|
||||
}
|
||||
}
|
||||
else if (cmd == CMD_SECURE_LOCK) { // secure lock
|
||||
if (apdu.nc == 0) {
|
||||
return SW_WRONG_LENGTH();
|
||||
}
|
||||
if (P2(apdu) == SECURE_LOCK_KEY_AGREEMENT) { // Key Agreement
|
||||
mbedtls_ecdh_context hkey;
|
||||
mbedtls_ecdh_init(&hkey);
|
||||
mbedtls_ecdh_setup(&hkey, MBEDTLS_ECP_DP_SECP256R1);
|
||||
int ret = mbedtls_ecdh_gen_public(&hkey.ctx.mbed_ecdh.grp, &hkey.ctx.mbed_ecdh.d, &hkey.ctx.mbed_ecdh.Q, random_gen, NULL);
|
||||
mbedtls_mpi_lset(&hkey.ctx.mbed_ecdh.Qp.Z, 1);
|
||||
ret = mbedtls_ecp_point_read_binary(&hkey.ctx.mbed_ecdh.grp, &hkey.ctx.mbed_ecdh.Qp, apdu.data, apdu.nc);
|
||||
if (ret != 0) {
|
||||
mbedtls_ecdh_free(&hkey);
|
||||
return SW_WRONG_DATA();
|
||||
}
|
||||
memcpy(mse.Qpt, apdu.data, sizeof(mse.Qpt));
|
||||
|
||||
uint8_t buf[MBEDTLS_ECP_MAX_BYTES];
|
||||
size_t olen = 0;
|
||||
ret = mbedtls_ecdh_calc_secret(&hkey, &olen, buf, MBEDTLS_ECP_MAX_BYTES, random_gen, NULL);
|
||||
if (ret != 0) {
|
||||
mbedtls_ecdh_free(&hkey);
|
||||
mbedtls_platform_zeroize(buf, sizeof(buf));
|
||||
return SW_WRONG_DATA();
|
||||
}
|
||||
ret = mbedtls_hkdf(mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), NULL, 0, buf, olen, mse.Qpt, sizeof(mse.Qpt), mse.key_enc, sizeof(mse.key_enc));
|
||||
mbedtls_platform_zeroize(buf, sizeof(buf));
|
||||
if (ret != 0) {
|
||||
mbedtls_ecdh_free(&hkey);
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
|
||||
ret = mbedtls_ecp_point_write_binary(&hkey.ctx.mbed_ecdh.grp, &hkey.ctx.mbed_ecdh.Q, MBEDTLS_ECP_PF_UNCOMPRESSED, &olen, res_APDU, MAX_APDU_DATA);
|
||||
mbedtls_ecdh_free(&hkey);
|
||||
if (ret != 0) {
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
mse.init = true;
|
||||
res_APDU_size = (uint16_t)olen;
|
||||
}
|
||||
else if (P2(apdu) == SECURE_LOCK_ENABLE || P2(apdu) == SECURE_LOCK_MASK || P2(apdu) == SECURE_LOCK_DISABLE) {
|
||||
if (mse.init == false) {
|
||||
return SW_COMMAND_NOT_ALLOWED();
|
||||
}
|
||||
uint16_t opts = get_device_options();
|
||||
int ret = mse_decrypt_ct(apdu.data, apdu.nc);
|
||||
if (ret != 0) {
|
||||
return SW_WRONG_DATA();
|
||||
}
|
||||
if (P2(apdu) == SECURE_LOCK_ENABLE || P2(apdu) == SECURE_LOCK_DISABLE) { // Enable
|
||||
uint8_t newopts[] = { opts >> 8, (opts & 0xff) };
|
||||
if ((P2(apdu) == SECURE_LOCK_ENABLE && !(opts & HSM_OPT_SECURE_LOCK)) ||
|
||||
(P2(apdu) == SECURE_LOCK_DISABLE && (opts & HSM_OPT_SECURE_LOCK))) {
|
||||
uint16_t tfids[] = { EF_MKEK, EF_MKEK_SO };
|
||||
for (int t = 0; t < sizeof(tfids) / sizeof(uint16_t); t++) {
|
||||
file_t *tf = search_file(tfids[t]);
|
||||
if (tf) {
|
||||
uint8_t *tmp = (uint8_t *) calloc(1, file_get_size(tf));
|
||||
memcpy(tmp, file_get_data(tf), file_get_size(tf));
|
||||
for (int i = 0; i < MKEK_KEY_SIZE; i++) {
|
||||
MKEK_KEY(tmp)[i] ^= apdu.data[i];
|
||||
}
|
||||
file_put_data(tf, tmp, file_get_size(tf));
|
||||
free(tmp);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (P2(apdu) == SECURE_LOCK_ENABLE) {
|
||||
newopts[0] |= HSM_OPT_SECURE_LOCK >> 8;
|
||||
}
|
||||
else if (P2(apdu) == SECURE_LOCK_DISABLE) {
|
||||
newopts[0] &= ~HSM_OPT_SECURE_LOCK >> 8;
|
||||
}
|
||||
file_t *tf = search_file(EF_DEVOPS);
|
||||
file_put_data(tf, newopts, sizeof(newopts));
|
||||
low_flash_available();
|
||||
}
|
||||
else if (P2(apdu) == SECURE_LOCK_MASK && (opts & HSM_OPT_SECURE_LOCK)) {
|
||||
memcpy(mkek_mask, apdu.data, MKEK_KEY_SIZE);
|
||||
has_mkek_mask = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
#ifndef ENABLE_EMULATION
|
||||
else if (cmd == CMD_PHY) { // Set PHY
|
||||
if (apdu.nc == 0) {
|
||||
if (file_has_data(ef_phy)) {
|
||||
res_APDU_size = file_get_size(ef_phy);
|
||||
memcpy(res_APDU, file_get_data(ef_phy), res_APDU_size);
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (P2(apdu) == PHY_VIDPID) { // VIDPID
|
||||
if (apdu.nc != 4) {
|
||||
return SW_WRONG_LENGTH();
|
||||
}
|
||||
phy_data.vid = get_uint16_t_be(apdu.data);
|
||||
phy_data.pid = get_uint16_t_be(apdu.data + 2);
|
||||
phy_data.vidpid_present = true;
|
||||
}
|
||||
else if (P2(apdu) == PHY_LED_GPIO) {
|
||||
phy_data.led_gpio = apdu.data[0];
|
||||
phy_data.led_gpio_present = true;
|
||||
}
|
||||
else if (P2(apdu) == PHY_LED_BTNESS) {
|
||||
phy_data.led_brightness = apdu.data[0];
|
||||
phy_data.led_brightness_present = true;
|
||||
}
|
||||
else if (P2(apdu) == PHY_OPTS) {
|
||||
if (apdu.nc != 2) {
|
||||
return SW_WRONG_LENGTH();
|
||||
}
|
||||
phy_data.opts = get_uint16_t_be(apdu.data);
|
||||
}
|
||||
else {
|
||||
return SW_INCORRECT_P1P2();
|
||||
}
|
||||
if (phy_save() != PICOKEY_OK) {
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#if PICO_RP2350
|
||||
else if (cmd == CMD_OTP) {
|
||||
if (apdu.nc < 2) {
|
||||
return SW_WRONG_LENGTH();
|
||||
}
|
||||
uint16_t row = get_uint16_t_be(apdu.data);
|
||||
bool israw = P2(apdu) == 0x1;
|
||||
if (apdu.nc == 2) {
|
||||
if (row > 0xbf && row < 0xf48) {
|
||||
return SW_WRONG_DATA();
|
||||
}
|
||||
if (israw) {
|
||||
memcpy(res_APDU, otp_buffer_raw(row), apdu.ne);
|
||||
}
|
||||
else {
|
||||
memcpy(res_APDU, otp_buffer(row), apdu.ne);
|
||||
}
|
||||
res_APDU_size = apdu.ne;
|
||||
}
|
||||
else {
|
||||
apdu.nc -= 2;
|
||||
apdu.data += 2;
|
||||
if (apdu.nc > 1024) {
|
||||
return SW_WRONG_LENGTH();
|
||||
}
|
||||
if (apdu.nc % (israw ? 4 : 2)) {
|
||||
return SW_WRONG_DATA();
|
||||
}
|
||||
uint8_t adata[1024] __attribute__((aligned(4)));
|
||||
memcpy(adata, apdu.data, apdu.nc);
|
||||
int ret = 0;
|
||||
if (israw) {
|
||||
ret = otp_write_data_raw(row, adata, apdu.nc);
|
||||
}
|
||||
else {
|
||||
ret = otp_write_data(row, adata, apdu.nc);
|
||||
}
|
||||
if (ret != 0) {
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#ifdef PICO_PLATFORM
|
||||
else if (cmd == CMD_REBOOT) {
|
||||
if (apdu.nc != 0) {
|
||||
return SW_WRONG_LENGTH();
|
||||
}
|
||||
watchdog_reboot(0, 0, 100);
|
||||
}
|
||||
#endif
|
||||
else if (cmd == CMD_MEMORY) {
|
||||
res_APDU_size = 0;
|
||||
uint32_t free = flash_free_space(), total = flash_total_space(), used = flash_used_space(), nfiles = flash_num_files(), size = flash_size();
|
||||
res_APDU_size += put_uint32_t_be(free, res_APDU + res_APDU_size);
|
||||
res_APDU_size += put_uint32_t_be(used, res_APDU + res_APDU_size);
|
||||
res_APDU_size += put_uint32_t_be(total, res_APDU + res_APDU_size);
|
||||
res_APDU_size += put_uint32_t_be(nfiles, res_APDU + res_APDU_size);
|
||||
res_APDU_size += put_uint32_t_be(size, res_APDU + res_APDU_size);
|
||||
}
|
||||
else {
|
||||
return SW_INCORRECT_P1P2();
|
||||
}
|
||||
return SW_OK();
|
||||
}
|
||||
120
src/hsm/cmd_general_authenticate.c
Normal file
120
src/hsm/cmd_general_authenticate.c
Normal file
|
|
@ -0,0 +1,120 @@
|
|||
/*
|
||||
* This file is part of the Pico HSM distribution (https://github.com/polhenarejos/pico-hsm).
|
||||
* Copyright (c) 2022 Pol Henarejos.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "sc_hsm.h"
|
||||
#include "mbedtls/ecdh.h"
|
||||
#include "asn1.h"
|
||||
#include "random.h"
|
||||
#include "oid.h"
|
||||
#include "eac.h"
|
||||
#include "files.h"
|
||||
#include "otp.h"
|
||||
|
||||
int cmd_general_authenticate() {
|
||||
if (P1(apdu) == 0x0 && P2(apdu) == 0x0) {
|
||||
if (apdu.data[0] == 0x7C) {
|
||||
int r = 0;
|
||||
uint16_t pubkey_len = 0;
|
||||
const uint8_t *pubkey = NULL;
|
||||
uint16_t tag = 0x0;
|
||||
uint8_t *tag_data = NULL, *p = NULL;
|
||||
uint16_t tag_len = 0;
|
||||
asn1_ctx_t ctxi;
|
||||
asn1_ctx_init(apdu.data + 2, (uint16_t)(apdu.nc - 2), &ctxi);
|
||||
while (walk_tlv(&ctxi, &p, &tag, &tag_len, &tag_data)) {
|
||||
if (tag == 0x80) {
|
||||
pubkey = tag_data - 1; //mbedtls ecdh starts reading one pos before
|
||||
pubkey_len = tag_len + 1;
|
||||
}
|
||||
}
|
||||
file_t *fkey = search_file(EF_KEY_DEV);
|
||||
if (!fkey) {
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
mbedtls_ecp_keypair ectx;
|
||||
mbedtls_ecp_keypair_init(&ectx);
|
||||
r = load_private_key_ecdh(&ectx, fkey);
|
||||
if (r != PICOKEY_OK) {
|
||||
mbedtls_ecp_keypair_free(&ectx);
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
mbedtls_ecdh_context ctx;
|
||||
mbedtls_ecdh_init(&ctx);
|
||||
mbedtls_ecp_group_id gid = MBEDTLS_ECP_DP_SECP256R1;
|
||||
if (otp_key_2) {
|
||||
gid = MBEDTLS_ECP_DP_SECP256K1;
|
||||
}
|
||||
r = mbedtls_ecdh_setup(&ctx, gid);
|
||||
if (r != 0) {
|
||||
mbedtls_ecp_keypair_free(&ectx);
|
||||
mbedtls_ecdh_free(&ctx);
|
||||
return SW_DATA_INVALID();
|
||||
}
|
||||
r = mbedtls_mpi_copy(&ctx.ctx.mbed_ecdh.d, &ectx.d);
|
||||
mbedtls_ecp_keypair_free(&ectx);
|
||||
if (r != 0) {
|
||||
mbedtls_ecdh_free(&ctx);
|
||||
return SW_DATA_INVALID();
|
||||
}
|
||||
r = mbedtls_ecdh_read_public(&ctx, pubkey, pubkey_len);
|
||||
if (r != 0) {
|
||||
mbedtls_ecdh_free(&ctx);
|
||||
return SW_DATA_INVALID();
|
||||
}
|
||||
size_t olen = 0;
|
||||
uint8_t derived[MBEDTLS_ECP_MAX_BYTES];
|
||||
r = mbedtls_ecdh_calc_secret(&ctx,
|
||||
&olen,
|
||||
derived,
|
||||
MBEDTLS_ECP_MAX_BYTES,
|
||||
random_gen,
|
||||
NULL);
|
||||
mbedtls_ecdh_free(&ctx);
|
||||
if (r != 0) {
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
|
||||
sm_derive_all_keys(derived, olen);
|
||||
|
||||
uint8_t *t = (uint8_t *) calloc(1, pubkey_len + 16);
|
||||
memcpy(t, "\x7F\x49\x4F\x06\x0A", 5);
|
||||
if (sm_get_protocol() == MSE_AES) {
|
||||
memcpy(t + 5, OID_ID_CA_ECDH_AES_CBC_CMAC_128, 10);
|
||||
}
|
||||
t[15] = 0x86;
|
||||
memcpy(t + 16, pubkey, pubkey_len);
|
||||
|
||||
res_APDU[res_APDU_size++] = 0x7C;
|
||||
res_APDU[res_APDU_size++] = 20;
|
||||
res_APDU[res_APDU_size++] = 0x81;
|
||||
res_APDU[res_APDU_size++] = 8;
|
||||
memcpy(res_APDU + res_APDU_size, sm_get_nonce(), 8);
|
||||
res_APDU_size += 8;
|
||||
res_APDU[res_APDU_size++] = 0x82;
|
||||
res_APDU[res_APDU_size++] = 8;
|
||||
|
||||
r = sm_sign(t, pubkey_len + 16, res_APDU + res_APDU_size);
|
||||
|
||||
free(t);
|
||||
if (r != PICOKEY_OK) {
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
res_APDU_size += 8;
|
||||
}
|
||||
}
|
||||
return SW_OK();
|
||||
}
|
||||
257
src/hsm/cmd_initialize.c
Normal file
257
src/hsm/cmd_initialize.c
Normal file
|
|
@ -0,0 +1,257 @@
|
|||
/*
|
||||
* This file is part of the Pico HSM distribution (https://github.com/polhenarejos/pico-hsm).
|
||||
* Copyright (c) 2022 Pol Henarejos.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "sc_hsm.h"
|
||||
#include "crypto_utils.h"
|
||||
#include "files.h"
|
||||
#include "random.h"
|
||||
#include "kek.h"
|
||||
#include "version.h"
|
||||
#include "asn1.h"
|
||||
#include "cvc.h"
|
||||
#include "otp.h"
|
||||
|
||||
extern void scan_all();
|
||||
|
||||
extern char __StackLimit;
|
||||
int heapLeft() {
|
||||
#if !defined(ENABLE_EMULATION) && !defined(ESP_PLATFORM)
|
||||
char *p = malloc(256); // try to avoid undue fragmentation
|
||||
int left = &__StackLimit - p;
|
||||
free(p);
|
||||
#else
|
||||
int left = 1024 * 1024;
|
||||
#endif
|
||||
return left;
|
||||
}
|
||||
|
||||
extern void reset_puk_store();
|
||||
int cmd_initialize() {
|
||||
if (apdu.nc > 0) {
|
||||
uint8_t mkek[MKEK_SIZE];
|
||||
uint16_t opts = get_device_options();
|
||||
if (opts & HSM_OPT_SECURE_LOCK && !has_mkek_mask) {
|
||||
return SW_SECURITY_STATUS_NOT_SATISFIED();
|
||||
}
|
||||
int ret_mkek = load_mkek(mkek); //Try loading MKEK with previous session
|
||||
initialize_flash(true);
|
||||
scan_all();
|
||||
has_session_pin = has_session_sopin = has_mkek_mask = false;
|
||||
uint16_t tag = 0x0;
|
||||
uint8_t *tag_data = NULL, *p = NULL, *kds = NULL, *dkeks = NULL;
|
||||
uint16_t tag_len = 0;
|
||||
asn1_ctx_t ctxi;
|
||||
asn1_ctx_init(apdu.data, (uint16_t)apdu.nc, &ctxi);
|
||||
while (walk_tlv(&ctxi, &p, &tag, &tag_len, &tag_data)) {
|
||||
if (tag == 0x80) { //options
|
||||
file_t *tf = search_file(EF_DEVOPS);
|
||||
file_put_data(tf, tag_data, tag_len);
|
||||
}
|
||||
else if (tag == 0x81) { //user pin
|
||||
if (file_pin1 && file_pin1->data) {
|
||||
uint8_t dhash[33];
|
||||
dhash[0] = (uint8_t)tag_len;
|
||||
double_hash_pin(tag_data, tag_len, dhash + 1);
|
||||
file_put_data(file_pin1, dhash, sizeof(dhash));
|
||||
hash_multi(tag_data, tag_len, session_pin);
|
||||
has_session_pin = true;
|
||||
}
|
||||
}
|
||||
else if (tag == 0x82) { //sopin pin
|
||||
if (file_sopin && file_sopin->data) {
|
||||
uint8_t dhash[33];
|
||||
dhash[0] = (uint8_t)tag_len;
|
||||
double_hash_pin(tag_data, tag_len, dhash + 1);
|
||||
file_put_data(file_sopin, dhash, sizeof(dhash));
|
||||
hash_multi(tag_data, tag_len, session_sopin);
|
||||
has_session_sopin = true;
|
||||
}
|
||||
}
|
||||
else if (tag == 0x91) { //retries user pin
|
||||
file_t *tf = search_file(EF_PIN1_MAX_RETRIES);
|
||||
if (tf && tf->data) {
|
||||
file_put_data(tf, tag_data, tag_len);
|
||||
}
|
||||
if (file_retries_pin1 && file_retries_pin1->data) {
|
||||
file_put_data(file_retries_pin1, tag_data, tag_len);
|
||||
}
|
||||
}
|
||||
else if (tag == 0x92) {
|
||||
dkeks = tag_data;
|
||||
file_t *tf = file_new(EF_DKEK);
|
||||
if (!tf) {
|
||||
release_mkek(mkek);
|
||||
return SW_MEMORY_FAILURE();
|
||||
}
|
||||
file_put_data(tf, NULL, 0);
|
||||
}
|
||||
else if (tag == 0x93) {
|
||||
file_t *ef_puk = search_file(EF_PUKAUT);
|
||||
if (!ef_puk) {
|
||||
release_mkek(mkek);
|
||||
return SW_MEMORY_FAILURE();
|
||||
}
|
||||
uint8_t pk_status[4], puks = MIN(tag_data[0], MAX_PUK);
|
||||
memset(pk_status, 0, sizeof(pk_status));
|
||||
pk_status[0] = puks;
|
||||
pk_status[1] = puks;
|
||||
pk_status[2] = tag_data[1];
|
||||
file_put_data(ef_puk, pk_status, sizeof(pk_status));
|
||||
for (uint8_t i = 0; i < puks; i++) {
|
||||
file_t *tf = file_new(EF_PUK + i);
|
||||
if (!tf) {
|
||||
release_mkek(mkek);
|
||||
return SW_MEMORY_FAILURE();
|
||||
}
|
||||
file_put_data(tf, NULL, 0);
|
||||
}
|
||||
}
|
||||
else if (tag == 0x97) {
|
||||
kds = tag_data;
|
||||
/*
|
||||
for (int i = 0; i < MIN(*kds,MAX_KEY_DOMAINS); i++) {
|
||||
file_t *tf = file_new(EF_DKEK+i);
|
||||
if (!tf)
|
||||
return SW_MEMORY_FAILURE();
|
||||
file_put_data(tf, NULL, 0);
|
||||
}
|
||||
*/
|
||||
}
|
||||
}
|
||||
file_t *tf_kd = search_file(EF_KEY_DOMAIN);
|
||||
if (!tf_kd) {
|
||||
release_mkek(mkek);
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
if (ret_mkek != PICOKEY_OK) {
|
||||
ret_mkek = load_mkek(mkek); //Try again with new PIN/SO-PIN just in case some is the same
|
||||
}
|
||||
if (store_mkek(ret_mkek == PICOKEY_OK ? mkek : NULL) != PICOKEY_OK) {
|
||||
release_mkek(mkek);
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
release_mkek(mkek);
|
||||
if (dkeks) {
|
||||
if (*dkeks > 0) {
|
||||
uint16_t d = *dkeks;
|
||||
if (file_put_data(tf_kd, (const uint8_t *) &d, sizeof(d)) != PICOKEY_OK) {
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
}
|
||||
else {
|
||||
int r = save_dkek_key(0, random_bytes_get(32));
|
||||
if (r != PICOKEY_OK) {
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
uint16_t d = 0x0101;
|
||||
if (file_put_data(tf_kd, (const uint8_t *) &d, sizeof(d)) != PICOKEY_OK) {
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
uint16_t d = 0x0000;
|
||||
if (file_put_data(tf_kd, (const uint8_t *) &d, sizeof(d)) != PICOKEY_OK) {
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
}
|
||||
if (kds) {
|
||||
uint8_t t[MAX_KEY_DOMAINS * 2], k = MIN(*kds, MAX_KEY_DOMAINS);
|
||||
memset(t, 0xff, 2 * k);
|
||||
if (file_put_data(tf_kd, t, 2 * k) != PICOKEY_OK) {
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
}
|
||||
/* When initialized, it has all credentials */
|
||||
isUserAuthenticated = true;
|
||||
/* Create terminal private key */
|
||||
file_t *fdkey = search_file(EF_KEY_DEV);
|
||||
if (!fdkey) {
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
int ret = 0;
|
||||
if (ret_mkek != PICOKEY_OK || !file_has_data(fdkey)) {
|
||||
mbedtls_ecdsa_context ecdsa;
|
||||
mbedtls_ecdsa_init(&ecdsa);
|
||||
mbedtls_ecp_group_id ec_id = MBEDTLS_ECP_DP_SECP256R1;
|
||||
uint8_t key_id = 0;
|
||||
if (otp_key_2) {
|
||||
ret = mbedtls_ecp_read_key(MBEDTLS_ECP_DP_SECP256K1, &ecdsa, otp_key_2, 32);
|
||||
if (ret != 0) {
|
||||
mbedtls_ecdsa_free(&ecdsa);
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
ret = mbedtls_ecp_mul(&ecdsa.grp, &ecdsa.Q, &ecdsa.d, &ecdsa.grp.G, random_gen, NULL);
|
||||
}
|
||||
else {
|
||||
ret = mbedtls_ecdsa_genkey(&ecdsa, ec_id, random_gen, NULL);
|
||||
}
|
||||
if (ret != 0) {
|
||||
mbedtls_ecdsa_free(&ecdsa);
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
ret = store_keys(&ecdsa, PICO_KEYS_KEY_EC, key_id);
|
||||
if (ret != PICOKEY_OK) {
|
||||
mbedtls_ecdsa_free(&ecdsa);
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
uint16_t ee_len = 0, term_len = 0;
|
||||
if ((ee_len = asn1_cvc_aut(&ecdsa, PICO_KEYS_KEY_EC, res_APDU, MAX_APDU_DATA, NULL, 0)) == 0) {
|
||||
mbedtls_ecdsa_free(&ecdsa);
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
|
||||
file_t *fpk = search_file(EF_EE_DEV);
|
||||
ret = file_put_data(fpk, res_APDU, ee_len);
|
||||
if (ret != 0) {
|
||||
mbedtls_ecdsa_free(&ecdsa);
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
|
||||
if ((term_len = asn1_cvc_cert(&ecdsa, PICO_KEYS_KEY_EC, res_APDU + ee_len, MAX_APDU_DATA - ee_len, NULL, 0, true)) == 0) {
|
||||
mbedtls_ecdsa_free(&ecdsa);
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
mbedtls_ecdsa_free(&ecdsa);
|
||||
fpk = search_file(EF_TERMCA);
|
||||
ret = file_put_data(fpk, res_APDU, ee_len + term_len);
|
||||
if (ret != 0) {
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
|
||||
const uint8_t *keyid = (const uint8_t *) "\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0",
|
||||
*label = (const uint8_t *) "ESPICOHSMTR";
|
||||
uint16_t prkd_len = asn1_build_prkd_ecc(label, (uint16_t)strlen((const char *) label), keyid, 20, 256, res_APDU, MAX_APDU_DATA);
|
||||
fpk = search_file(EF_PRKD_DEV);
|
||||
ret = file_put_data(fpk, res_APDU, prkd_len);
|
||||
}
|
||||
if (ret != 0) {
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
low_flash_available();
|
||||
reset_puk_store();
|
||||
}
|
||||
else { //free memory bytes request
|
||||
int heap_left = heapLeft();
|
||||
res_APDU_size += put_uint32_t_be(heap_left, res_APDU);
|
||||
res_APDU[4] = 0;
|
||||
res_APDU[5] = HSM_VERSION_MAJOR;
|
||||
res_APDU[6] = HSM_VERSION_MINOR;
|
||||
res_APDU_size = 7;
|
||||
}
|
||||
return SW_OK();
|
||||
}
|
||||
212
src/hsm/cmd_key_domain.c
Normal file
212
src/hsm/cmd_key_domain.c
Normal file
|
|
@ -0,0 +1,212 @@
|
|||
/*
|
||||
* This file is part of the Pico HSM distribution (https://github.com/polhenarejos/pico-hsm).
|
||||
* Copyright (c) 2022 Pol Henarejos.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "crypto_utils.h"
|
||||
#include "sc_hsm.h"
|
||||
#include "cvc.h"
|
||||
#include "kek.h"
|
||||
#include "files.h"
|
||||
|
||||
uint8_t get_key_domain(file_t *fkey) {
|
||||
uint16_t tag_len = 0;
|
||||
if (!file_has_data(fkey)) {
|
||||
return 0xff;
|
||||
}
|
||||
const uint8_t *meta_tag = get_meta_tag(fkey, 0x92, &tag_len);
|
||||
if (meta_tag) {
|
||||
return *meta_tag;
|
||||
}
|
||||
return 0x0;
|
||||
}
|
||||
|
||||
int cmd_key_domain() {
|
||||
//if (dkeks == 0)
|
||||
// return SW_COMMAND_NOT_ALLOWED();
|
||||
uint8_t p1 = P1(apdu), p2 = P2(apdu);
|
||||
if ((has_session_pin == false || isUserAuthenticated == false) && apdu.nc > 0 &&
|
||||
!(p1 == 0x0 && p2 == 0x0)) {
|
||||
return SW_CONDITIONS_NOT_SATISFIED();
|
||||
}
|
||||
if (p2 >= MAX_KEY_DOMAINS) {
|
||||
return SW_WRONG_P1P2();
|
||||
}
|
||||
file_t *tf_kd = search_file(EF_KEY_DOMAIN);
|
||||
if (!tf_kd) {
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
uint16_t tf_kd_size = file_get_size(tf_kd);
|
||||
if (tf_kd_size == 0) {
|
||||
return SW_WRONG_P1P2();
|
||||
}
|
||||
if (2 * p2 >= tf_kd_size) {
|
||||
return SW_INCORRECT_P1P2();
|
||||
}
|
||||
uint8_t *kdata = file_get_data(tf_kd), dkeks = kdata ? kdata[2 * p2] : 0,
|
||||
current_dkeks = kdata ? kdata[2 * p2 + 1] : 0;
|
||||
if (p1 == 0x0) { //dkek import
|
||||
if (apdu.nc > 0) {
|
||||
file_t *tf = file_new(EF_DKEK + p2);
|
||||
if (!tf) {
|
||||
return SW_MEMORY_FAILURE();
|
||||
}
|
||||
if (apdu.nc < 32) {
|
||||
return SW_WRONG_LENGTH();
|
||||
}
|
||||
if (current_dkeks == dkeks) {
|
||||
return SW_COMMAND_NOT_ALLOWED();
|
||||
}
|
||||
import_dkek_share(p2, apdu.data);
|
||||
if (++current_dkeks >= dkeks) {
|
||||
int r = save_dkek_key(p2, NULL);
|
||||
if (r != PICOKEY_OK) {
|
||||
if (r == PICOKEY_NO_LOGIN) {
|
||||
pending_save_dkek = p2;
|
||||
}
|
||||
else {
|
||||
/* On fail, it will return to previous dkek state. */
|
||||
import_dkek_share(p2, apdu.data);
|
||||
return SW_FILE_NOT_FOUND();
|
||||
}
|
||||
}
|
||||
}
|
||||
uint8_t t[MAX_KEY_DOMAINS * 2];
|
||||
memcpy(t, kdata, tf_kd_size);
|
||||
t[2 * p2 + 1] = current_dkeks;
|
||||
if (file_put_data(tf_kd, t, tf_kd_size) != PICOKEY_OK) {
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
low_flash_available();
|
||||
}
|
||||
else {
|
||||
file_t *tf = search_file(EF_XKEK + p2);
|
||||
if (current_dkeks == 0xff && !file_has_data(tf)) { //XKEK have always 0xff
|
||||
return SW_REFERENCE_NOT_FOUND();
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (p1 == 0x1 || p1 == 0x3 || p1 == 0x4) { //key domain setup
|
||||
if (p1 == 0x1 && apdu.nc != 1) {
|
||||
return SW_WRONG_LENGTH();
|
||||
}
|
||||
if (p1 == 0x3) { //if key domain is not empty, command is denied
|
||||
for (uint16_t i = 1; i < 256; i++) {
|
||||
file_t *fkey = search_file(KEY_PREFIX << 8 | (uint8_t)i);
|
||||
if (get_key_domain(fkey) == p2) {
|
||||
return SW_FILE_EXISTS();
|
||||
}
|
||||
}
|
||||
}
|
||||
uint8_t t[MAX_KEY_DOMAINS * 2];
|
||||
memcpy(t, kdata, tf_kd_size);
|
||||
if (p1 == 0x1) {
|
||||
if (t[2 * p2] != 0xff || t[2 * p2 + 1] != 0xff) {
|
||||
return SW_INCORRECT_P1P2();
|
||||
}
|
||||
t[2 * p2] = dkeks = apdu.data[0];
|
||||
t[2 * p2 + 1] = current_dkeks = 0;
|
||||
}
|
||||
else if (p1 == 0x3) {
|
||||
if (t[2 * p2] == 0xff && t[2 * p2 + 1] == 0xff) {
|
||||
return SW_INCORRECT_P1P2();
|
||||
}
|
||||
t[2 * p2] = dkeks = 0xff;
|
||||
t[2 * p2 + 1] = 0xff;
|
||||
}
|
||||
else if (p1 == 0x4) {
|
||||
t[2 * p2 + 1] = current_dkeks = 0;
|
||||
}
|
||||
if (file_put_data(tf_kd, t, tf_kd_size) != PICOKEY_OK) {
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
file_t *tf = NULL;
|
||||
if ((tf = search_file(EF_DKEK + p2))) {
|
||||
if (delete_file(tf) != PICOKEY_OK) {
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
}
|
||||
if (p1 == 0x3 && (tf = search_file(EF_XKEK + p2))) {
|
||||
if (delete_file(tf) != PICOKEY_OK) {
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
}
|
||||
low_flash_available();
|
||||
if (p1 == 0x3) {
|
||||
return SW_REFERENCE_NOT_FOUND();
|
||||
}
|
||||
}
|
||||
else if (p1 == 0x2) { //XKEK Key Domain creation
|
||||
if (apdu.nc > 0) {
|
||||
uint16_t pub_len = 0;
|
||||
file_t *fterm = search_file(EF_TERMCA);
|
||||
if (!fterm) {
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
const uint8_t *pub = cvc_get_pub(file_get_data(fterm), file_get_size(fterm), &pub_len);
|
||||
if (!pub) {
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
uint16_t t86_len = 0;
|
||||
const uint8_t *t86 = cvc_get_field(pub, pub_len, &t86_len, 0x86);
|
||||
if (!t86 || t86[0] != 0x4) {
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
uint16_t t54_len = 0;
|
||||
const uint8_t *t54 = cvc_get_field(apdu.data, (uint16_t)apdu.nc, &t54_len, 0x54);
|
||||
if (!t54) {
|
||||
return SW_WRONG_DATA();
|
||||
}
|
||||
uint8_t hash[32], *input = (uint8_t *) calloc(1, (t86_len - 1) / 2 + 1);
|
||||
input[0] = 0x54;
|
||||
memcpy(input + 1, t86 + 1, (t86_len - 1) / 2);
|
||||
hash256(input, (t86_len - 1) / 2 + 1, hash);
|
||||
free(input);
|
||||
int r = puk_verify(t54, t54_len, hash, 32, apdu.data, (uint16_t)apdu.nc);
|
||||
if (r != 0) {
|
||||
return SW_CONDITIONS_NOT_SATISFIED();
|
||||
}
|
||||
file_t *tf = file_new(EF_XKEK + p2);
|
||||
if (!tf) {
|
||||
return SW_MEMORY_FAILURE();
|
||||
}
|
||||
|
||||
//All checks done. Get Key Domain UID
|
||||
pub = cvc_get_pub(apdu.data, (uint16_t)apdu.nc, &pub_len);
|
||||
if (pub) {
|
||||
t86_len = 0;
|
||||
t86 = cvc_get_field(pub, pub_len, &t86_len, 0x86);
|
||||
if (t86) {
|
||||
file_put_data(tf, t86 + 1, (uint16_t)t86_len - 1);
|
||||
low_flash_available();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
return SW_INCORRECT_P1P2();
|
||||
}
|
||||
memset(res_APDU, 0, 10);
|
||||
res_APDU[0] = dkeks;
|
||||
res_APDU[1] = dkeks > current_dkeks ? dkeks - current_dkeks : 0;
|
||||
dkek_kcv(p2, res_APDU + 2);
|
||||
res_APDU_size = 2 + 8;
|
||||
file_t *tf = search_file(EF_XKEK + p2);
|
||||
if (file_has_data(tf)) {
|
||||
memcpy(res_APDU + 10, file_get_data(tf), file_get_size(tf));
|
||||
res_APDU_size += file_get_size(tf);
|
||||
}
|
||||
return SW_OK();
|
||||
}
|
||||
67
src/hsm/cmd_key_gen.c
Normal file
67
src/hsm/cmd_key_gen.c
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
/*
|
||||
* This file is part of the Pico HSM distribution (https://github.com/polhenarejos/pico-hsm).
|
||||
* Copyright (c) 2022 Pol Henarejos.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "crypto_utils.h"
|
||||
#include "sc_hsm.h"
|
||||
#include "random.h"
|
||||
|
||||
int cmd_key_gen() {
|
||||
uint8_t key_id = P1(apdu);
|
||||
uint8_t p2 = P2(apdu);
|
||||
uint8_t key_size = 32;
|
||||
int r;
|
||||
if (!isUserAuthenticated) {
|
||||
return SW_SECURITY_STATUS_NOT_SATISFIED();
|
||||
}
|
||||
if (p2 == 0xB3) {
|
||||
key_size = 64;
|
||||
}
|
||||
else if (p2 == 0xB2) {
|
||||
key_size = 32;
|
||||
}
|
||||
else if (p2 == 0xB1) {
|
||||
key_size = 24;
|
||||
}
|
||||
else if (p2 == 0xB0) {
|
||||
key_size = 16;
|
||||
}
|
||||
//at this moment, we do not use the template, as only CBC is supported by the driver (encrypt, decrypt and CMAC)
|
||||
uint8_t aes_key[64]; //maximum AES key size
|
||||
memcpy(aes_key, random_bytes_get(key_size), key_size);
|
||||
int aes_type = 0x0;
|
||||
if (key_size == 16) {
|
||||
aes_type = PICO_KEYS_KEY_AES_128;
|
||||
}
|
||||
else if (key_size == 24) {
|
||||
aes_type = PICO_KEYS_KEY_AES_192;
|
||||
}
|
||||
else if (key_size == 32) {
|
||||
aes_type = PICO_KEYS_KEY_AES_256;
|
||||
}
|
||||
else if (key_size == 64) {
|
||||
aes_type = PICO_KEYS_KEY_AES_512;
|
||||
}
|
||||
r = store_keys(aes_key, aes_type, key_id);
|
||||
if (r != PICOKEY_OK) {
|
||||
return SW_MEMORY_FAILURE();
|
||||
}
|
||||
if (find_and_store_meta_key(key_id) != PICOKEY_OK) {
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
low_flash_available();
|
||||
return SW_OK();
|
||||
}
|
||||
153
src/hsm/cmd_key_unwrap.c
Normal file
153
src/hsm/cmd_key_unwrap.c
Normal file
|
|
@ -0,0 +1,153 @@
|
|||
/*
|
||||
* This file is part of the Pico HSM distribution (https://github.com/polhenarejos/pico-hsm).
|
||||
* Copyright (c) 2022 Pol Henarejos.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "sc_hsm.h"
|
||||
#include "crypto_utils.h"
|
||||
#include "kek.h"
|
||||
#include "cvc.h"
|
||||
|
||||
int cmd_key_unwrap() {
|
||||
uint8_t key_id = P1(apdu);
|
||||
int r = 0;
|
||||
if (P2(apdu) != 0x93) {
|
||||
return SW_WRONG_P1P2();
|
||||
}
|
||||
if (!isUserAuthenticated) {
|
||||
return SW_SECURITY_STATUS_NOT_SATISFIED();
|
||||
}
|
||||
uint8_t *data = apdu.data;
|
||||
uint16_t data_len = apdu.nc;
|
||||
if (data_len == 0) { // New style
|
||||
file_t *tef = search_file(0x2F10);
|
||||
if (!file_has_data(tef)) {
|
||||
return SW_FILE_NOT_FOUND();
|
||||
}
|
||||
data = file_get_data(tef);
|
||||
data_len = file_get_size(tef);
|
||||
}
|
||||
int key_type = dkek_type_key(data);
|
||||
uint8_t *allowed = NULL;
|
||||
int16_t kdom = -1;
|
||||
uint16_t allowed_len = 0;
|
||||
if (key_type == 0x0) {
|
||||
return SW_DATA_INVALID();
|
||||
}
|
||||
if (key_type & PICO_KEYS_KEY_RSA) {
|
||||
mbedtls_rsa_context ctx;
|
||||
mbedtls_rsa_init(&ctx);
|
||||
do {
|
||||
r = dkek_decode_key((uint8_t)++kdom, &ctx, data, data_len, NULL, &allowed, &allowed_len);
|
||||
} while ((r == PICOKEY_ERR_FILE_NOT_FOUND || r == PICOKEY_WRONG_DKEK) && kdom < MAX_KEY_DOMAINS);
|
||||
if (r != PICOKEY_OK) {
|
||||
mbedtls_rsa_free(&ctx);
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
r = store_keys(&ctx, PICO_KEYS_KEY_RSA, key_id);
|
||||
if ((res_APDU_size = (uint16_t)asn1_cvc_aut(&ctx, PICO_KEYS_KEY_RSA, res_APDU, MAX_APDU_DATA, NULL, 0)) == 0) {
|
||||
mbedtls_rsa_free(&ctx);
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
mbedtls_rsa_free(&ctx);
|
||||
if (r != PICOKEY_OK) {
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
}
|
||||
else if (key_type & PICO_KEYS_KEY_EC) {
|
||||
mbedtls_ecp_keypair ctx;
|
||||
mbedtls_ecp_keypair_init(&ctx);
|
||||
do {
|
||||
r = dkek_decode_key((uint8_t)++kdom, &ctx, data, data_len, NULL, &allowed, &allowed_len);
|
||||
} while ((r == PICOKEY_ERR_FILE_NOT_FOUND || r == PICOKEY_WRONG_DKEK) && kdom < MAX_KEY_DOMAINS);
|
||||
if (r != PICOKEY_OK) {
|
||||
mbedtls_ecp_keypair_free(&ctx);
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
r = store_keys(&ctx, PICO_KEYS_KEY_EC, key_id);
|
||||
if ((res_APDU_size = (uint16_t)asn1_cvc_aut(&ctx, PICO_KEYS_KEY_EC, res_APDU, MAX_APDU_DATA, NULL, 0)) == 0) {
|
||||
mbedtls_ecp_keypair_free(&ctx);
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
mbedtls_ecp_keypair_free(&ctx);
|
||||
if (r != PICOKEY_OK) {
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
}
|
||||
else if (key_type & PICO_KEYS_KEY_AES) {
|
||||
uint8_t aes_key[64];
|
||||
int key_size = 0, aes_type = 0;
|
||||
do {
|
||||
r = dkek_decode_key((uint8_t)++kdom,
|
||||
aes_key,
|
||||
data,
|
||||
data_len,
|
||||
&key_size,
|
||||
&allowed,
|
||||
&allowed_len);
|
||||
} while ((r == PICOKEY_ERR_FILE_NOT_FOUND || r == PICOKEY_WRONG_DKEK) && kdom < MAX_KEY_DOMAINS);
|
||||
if (r != PICOKEY_OK) {
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
if (key_size == 64) {
|
||||
aes_type = PICO_KEYS_KEY_AES_512;
|
||||
}
|
||||
else if (key_size == 32) {
|
||||
aes_type = PICO_KEYS_KEY_AES_256;
|
||||
}
|
||||
else if (key_size == 24) {
|
||||
aes_type = PICO_KEYS_KEY_AES_192;
|
||||
}
|
||||
else if (key_size == 16) {
|
||||
aes_type = PICO_KEYS_KEY_AES_128;
|
||||
}
|
||||
else {
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
r = store_keys(aes_key, aes_type, key_id);
|
||||
if (r != PICOKEY_OK) {
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
}
|
||||
if ((allowed != NULL && allowed_len > 0) || kdom >= 0) {
|
||||
uint16_t meta_len = (allowed_len > 0 ? 2 + allowed_len : 0) + (kdom >= 0 ? 3 : 0);
|
||||
uint8_t *meta = (uint8_t *) calloc(1, meta_len), *m = meta;
|
||||
if (allowed_len > 0) {
|
||||
*m++ = 0x91;
|
||||
*m++ = (uint8_t)allowed_len;
|
||||
memcpy(m, allowed, allowed_len); m += allowed_len;
|
||||
}
|
||||
if (kdom >= 0) {
|
||||
*m++ = 0x92;
|
||||
*m++ = 1;
|
||||
*m++ = (uint8_t)kdom;
|
||||
}
|
||||
r = meta_add((KEY_PREFIX << 8) | key_id, meta, meta_len);
|
||||
free(meta);
|
||||
if (r != PICOKEY_OK) {
|
||||
return r;
|
||||
}
|
||||
}
|
||||
if (res_APDU_size > 0) {
|
||||
file_t *fpk = file_new((EE_CERTIFICATE_PREFIX << 8) | key_id);
|
||||
r = file_put_data(fpk, res_APDU, res_APDU_size);
|
||||
if (r != 0) {
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
res_APDU_size = 0;
|
||||
}
|
||||
low_flash_available();
|
||||
return SW_OK();
|
||||
}
|
||||
118
src/hsm/cmd_key_wrap.c
Normal file
118
src/hsm/cmd_key_wrap.c
Normal file
|
|
@ -0,0 +1,118 @@
|
|||
/*
|
||||
* This file is part of the Pico HSM distribution (https://github.com/polhenarejos/pico-hsm).
|
||||
* Copyright (c) 2022 Pol Henarejos.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "crypto_utils.h"
|
||||
#include "sc_hsm.h"
|
||||
#include "asn1.h"
|
||||
#include "kek.h"
|
||||
#include "files.h"
|
||||
|
||||
extern uint8_t get_key_domain(file_t *fkey);
|
||||
|
||||
int cmd_key_wrap() {
|
||||
int r = 0;
|
||||
uint8_t key_id = P1(apdu);
|
||||
if (P2(apdu) != 0x92) {
|
||||
return SW_WRONG_P1P2();
|
||||
}
|
||||
if (!isUserAuthenticated) {
|
||||
return SW_SECURITY_STATUS_NOT_SATISFIED();
|
||||
}
|
||||
file_t *ef = search_file((KEY_PREFIX << 8) | key_id);
|
||||
if (!ef) {
|
||||
return SW_FILE_NOT_FOUND();
|
||||
}
|
||||
uint8_t kdom = get_key_domain(ef);
|
||||
if (kdom == 0xff) {
|
||||
return SW_REFERENCE_NOT_FOUND();
|
||||
}
|
||||
file_t *tf_kd = search_file(EF_KEY_DOMAIN);
|
||||
uint8_t *kdata = file_get_data(tf_kd), dkeks = kdata ? kdata[2 * kdom] : 0,
|
||||
current_dkeks = kdata ? kdata[2 * kdom + 1] : 0;
|
||||
if (dkeks != current_dkeks || dkeks == 0 || dkeks == 0xff) {
|
||||
return SW_REFERENCE_NOT_FOUND();
|
||||
}
|
||||
if (key_has_purpose(ef, ALGO_WRAP) == false) {
|
||||
return SW_CONDITIONS_NOT_SATISFIED();
|
||||
}
|
||||
file_t *prkd = search_file((PRKD_PREFIX << 8) | key_id);
|
||||
if (!prkd) {
|
||||
return SW_FILE_NOT_FOUND();
|
||||
}
|
||||
const uint8_t *dprkd = file_get_data(prkd);
|
||||
uint16_t wrap_len = MAX_DKEK_ENCODE_KEY_BUFFER, tag_len = 0;
|
||||
const uint8_t *meta_tag = get_meta_tag(ef, 0x91, &tag_len);
|
||||
if (*dprkd == P15_KEYTYPE_RSA) {
|
||||
mbedtls_rsa_context ctx;
|
||||
mbedtls_rsa_init(&ctx);
|
||||
r = load_private_key_rsa(&ctx, ef);
|
||||
if (r != PICOKEY_OK) {
|
||||
mbedtls_rsa_free(&ctx);
|
||||
if (r == PICOKEY_VERIFICATION_FAILED) {
|
||||
return SW_SECURE_MESSAGE_EXEC_ERROR();
|
||||
}
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
r = dkek_encode_key(kdom, &ctx, PICO_KEYS_KEY_RSA, res_APDU, &wrap_len, meta_tag, tag_len);
|
||||
mbedtls_rsa_free(&ctx);
|
||||
}
|
||||
else if (*dprkd == P15_KEYTYPE_ECC) {
|
||||
mbedtls_ecp_keypair ctx;
|
||||
mbedtls_ecp_keypair_init(&ctx);
|
||||
r = load_private_key_ec(&ctx, ef);
|
||||
if (r != PICOKEY_OK) {
|
||||
mbedtls_ecp_keypair_free(&ctx);
|
||||
if (r == PICOKEY_VERIFICATION_FAILED) {
|
||||
return SW_SECURE_MESSAGE_EXEC_ERROR();
|
||||
}
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
r = dkek_encode_key(kdom, &ctx, PICO_KEYS_KEY_EC, res_APDU, &wrap_len, meta_tag, tag_len);
|
||||
mbedtls_ecp_keypair_free(&ctx);
|
||||
}
|
||||
else if (*dprkd == P15_KEYTYPE_AES) {
|
||||
uint8_t kdata_aes[64]; //maximum AES key size
|
||||
if (wait_button_pressed() == true) { //timeout
|
||||
return SW_SECURE_MESSAGE_EXEC_ERROR();
|
||||
}
|
||||
|
||||
uint16_t key_size = file_get_size(ef), aes_type = PICO_KEYS_KEY_AES;
|
||||
memcpy(kdata_aes, file_get_data(ef), key_size);
|
||||
if (mkek_decrypt(kdata_aes, key_size) != 0) {
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
if (key_size == 64) {
|
||||
aes_type = PICO_KEYS_KEY_AES_512;
|
||||
}
|
||||
else if (key_size == 32) {
|
||||
aes_type = PICO_KEYS_KEY_AES_256;
|
||||
}
|
||||
else if (key_size == 24) {
|
||||
aes_type = PICO_KEYS_KEY_AES_192;
|
||||
}
|
||||
else if (key_size == 16) {
|
||||
aes_type = PICO_KEYS_KEY_AES_128;
|
||||
}
|
||||
r = dkek_encode_key(kdom, kdata_aes, aes_type, res_APDU, &wrap_len, meta_tag, tag_len);
|
||||
mbedtls_platform_zeroize(kdata_aes, sizeof(kdata_aes));
|
||||
}
|
||||
if (r != PICOKEY_OK) {
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
res_APDU_size = wrap_len;
|
||||
return SW_OK();
|
||||
}
|
||||
165
src/hsm/cmd_keypair_gen.c
Normal file
165
src/hsm/cmd_keypair_gen.c
Normal file
|
|
@ -0,0 +1,165 @@
|
|||
/*
|
||||
* This file is part of the Pico HSM distribution (https://github.com/polhenarejos/pico-hsm).
|
||||
* Copyright (c) 2022 Pol Henarejos.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "crypto_utils.h"
|
||||
#include "sc_hsm.h"
|
||||
#include "files.h"
|
||||
#include "asn1.h"
|
||||
#include "cvc.h"
|
||||
#include "oid.h"
|
||||
#include "random.h"
|
||||
#include "kek.h"
|
||||
|
||||
int cmd_keypair_gen() {
|
||||
uint8_t key_id = P1(apdu);
|
||||
if (!isUserAuthenticated) {
|
||||
return SW_SECURITY_STATUS_NOT_SATISFIED();
|
||||
}
|
||||
int ret = 0;
|
||||
|
||||
//sc_asn1_print_tags(apdu.data, apdu.nc);
|
||||
//DEBUG_DATA(apdu.data,apdu.nc);
|
||||
asn1_ctx_t ctxi, ctxo = { 0 };
|
||||
asn1_ctx_init(apdu.data, (uint16_t)apdu.nc, &ctxi);
|
||||
if (asn1_find_tag(&ctxi, 0x7f49, &ctxo) && asn1_len(&ctxo) > 0) {
|
||||
asn1_ctx_t oid = { 0 };
|
||||
if (asn1_find_tag(&ctxo, 0x6, &oid) && asn1_len(&oid) > 0) {
|
||||
if (memcmp(oid.data, OID_ID_TA_RSA_V1_5_SHA_256, oid.len) == 0) { //RSA
|
||||
asn1_ctx_t ex = { 0 }, ks = { 0 };
|
||||
uint32_t exponent = 65537, key_size = 2048;
|
||||
if (asn1_find_tag(&ctxo, 0x82, &ex) && asn1_len(&ex) > 0) {
|
||||
exponent = asn1_get_uint(&ex);
|
||||
}
|
||||
if (asn1_find_tag(&ctxo, 0x2, &ks) && asn1_len(&ks) > 0) {
|
||||
key_size = asn1_get_uint(&ks);
|
||||
}
|
||||
mbedtls_rsa_context rsa;
|
||||
mbedtls_rsa_init(&rsa);
|
||||
uint8_t index = 0;
|
||||
ret = mbedtls_rsa_gen_key(&rsa, random_gen, &index, key_size, exponent);
|
||||
if (ret != 0) {
|
||||
mbedtls_rsa_free(&rsa);
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
if ((res_APDU_size = (uint16_t)asn1_cvc_aut(&rsa, PICO_KEYS_KEY_RSA, res_APDU, MAX_APDU_DATA, NULL, 0)) == 0) {
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
ret = store_keys(&rsa, PICO_KEYS_KEY_RSA, key_id);
|
||||
if (ret != PICOKEY_OK) {
|
||||
mbedtls_rsa_free(&rsa);
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
mbedtls_rsa_free(&rsa);
|
||||
}
|
||||
else if (memcmp(oid.data, OID_ID_TA_ECDSA_SHA_256, MIN(oid.len, 10)) == 0) { //ECC
|
||||
asn1_ctx_t prime = { 0 };
|
||||
if (asn1_find_tag(&ctxo, 0x81, &prime) != true) {
|
||||
return SW_WRONG_DATA();
|
||||
}
|
||||
mbedtls_ecp_group_id ec_id = ec_get_curve_from_prime(prime.data, prime.len);
|
||||
if (ec_id == MBEDTLS_ECP_DP_NONE) {
|
||||
return SW_FUNC_NOT_SUPPORTED();
|
||||
}
|
||||
if (ec_id == MBEDTLS_ECP_DP_CURVE25519 || ec_id == MBEDTLS_ECP_DP_CURVE448) {
|
||||
asn1_ctx_t g = { 0 };
|
||||
if (asn1_find_tag(&ctxo, 0x83, &g) != true) {
|
||||
return SW_WRONG_DATA();
|
||||
}
|
||||
#ifdef MBEDTLS_EDDSA_C
|
||||
if (ec_id == MBEDTLS_ECP_DP_CURVE25519 && (g.data[0] != 9)) {
|
||||
ec_id = MBEDTLS_ECP_DP_ED25519;
|
||||
}
|
||||
else if (ec_id == MBEDTLS_ECP_DP_CURVE448 && (g.len != 56 || g.data[0] != 5)) {
|
||||
ec_id = MBEDTLS_ECP_DP_ED448;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
mbedtls_ecdsa_context ecdsa;
|
||||
mbedtls_ecdsa_init(&ecdsa);
|
||||
uint8_t index = 0;
|
||||
ret = mbedtls_ecdsa_genkey(&ecdsa, ec_id, random_gen, &index);
|
||||
if (ret != 0) {
|
||||
mbedtls_ecdsa_free(&ecdsa);
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
asn1_ctx_t a91 = { 0 }, ext = { 0 };
|
||||
if (asn1_find_tag(&ctxi, 0x91, &a91) && asn1_len(&a91) > 0) {
|
||||
for (size_t n = 0; n < a91.len; n++) {
|
||||
if (a91.data[n] == ALGO_EC_DH_XKEK) {
|
||||
asn1_ctx_t a92 = {0};
|
||||
if (!asn1_find_tag(&ctxi, 0x92, &a92) || asn1_len(&a92) == 0) {
|
||||
return SW_WRONG_DATA();
|
||||
}
|
||||
if (a92.data[0] > MAX_KEY_DOMAINS) {
|
||||
return SW_WRONG_DATA();
|
||||
}
|
||||
file_t *tf_xkek = search_file(EF_XKEK + a92.data[0]);
|
||||
if (!tf_xkek) {
|
||||
return SW_WRONG_DATA();
|
||||
}
|
||||
ext.len = 2 + 2 + (uint16_t)strlen(OID_ID_KEY_DOMAIN_UID) + 2 + file_get_size(
|
||||
tf_xkek);
|
||||
ext.data = (uint8_t *) calloc(1, ext.len);
|
||||
uint8_t *pe = ext.data;
|
||||
*pe++ = 0x73;
|
||||
*pe++ = (uint8_t)ext.len - 2;
|
||||
*pe++ = 0x6;
|
||||
*pe++ = (uint8_t)strlen(OID_ID_KEY_DOMAIN_UID);
|
||||
memcpy(pe, OID_ID_KEY_DOMAIN_UID, strlen(OID_ID_KEY_DOMAIN_UID));
|
||||
pe += strlen(OID_ID_KEY_DOMAIN_UID);
|
||||
*pe++ = 0x80;
|
||||
*pe++ = (uint8_t)file_get_size(tf_xkek);
|
||||
memcpy(pe, file_get_data(tf_xkek), file_get_size(tf_xkek));
|
||||
}
|
||||
}
|
||||
}
|
||||
if ((res_APDU_size = (uint16_t)asn1_cvc_aut(&ecdsa, PICO_KEYS_KEY_EC, res_APDU, MAX_APDU_DATA, ext.data, ext.len)) == 0) {
|
||||
if (ext.data) {
|
||||
free(ext.data);
|
||||
}
|
||||
mbedtls_ecdsa_free(&ecdsa);
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
if (ext.data) {
|
||||
free(ext.data);
|
||||
}
|
||||
ret = store_keys(&ecdsa, PICO_KEYS_KEY_EC, key_id);
|
||||
mbedtls_ecdsa_free(&ecdsa);
|
||||
if (ret != PICOKEY_OK) {
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
else {
|
||||
return SW_WRONG_DATA();
|
||||
}
|
||||
if (find_and_store_meta_key(key_id) != PICOKEY_OK) {
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
file_t *fpk = file_new((EE_CERTIFICATE_PREFIX << 8) | key_id);
|
||||
ret = file_put_data(fpk, res_APDU, res_APDU_size);
|
||||
if (ret != 0) {
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
if (apdu.ne == 0) {
|
||||
apdu.ne = res_APDU_size;
|
||||
}
|
||||
low_flash_available();
|
||||
return SW_OK();
|
||||
}
|
||||
68
src/hsm/cmd_list_keys.c
Normal file
68
src/hsm/cmd_list_keys.c
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
/*
|
||||
* This file is part of the Pico HSM distribution (https://github.com/polhenarejos/pico-hsm).
|
||||
* Copyright (c) 2022 Pol Henarejos.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "sc_hsm.h"
|
||||
#include "files.h"
|
||||
|
||||
int cmd_list_keys() {
|
||||
/* First we send DEV private key */
|
||||
/* Both below conditions should be always TRUE */
|
||||
if (search_file(EF_PRKD_DEV)) {
|
||||
res_APDU_size += put_uint16_t_be(EF_PRKD_DEV, res_APDU + res_APDU_size);
|
||||
}
|
||||
if (search_file(EF_KEY_DEV)) {
|
||||
res_APDU_size += put_uint16_t_be(EF_KEY_DEV, res_APDU + res_APDU_size);
|
||||
}
|
||||
//first CC
|
||||
for (int i = 0; i < dynamic_files; i++) {
|
||||
file_t *f = &dynamic_file[i];
|
||||
if ((f->fid & 0xff00) == (KEY_PREFIX << 8)) {
|
||||
res_APDU[res_APDU_size++] = KEY_PREFIX;
|
||||
res_APDU[res_APDU_size++] = f->fid & 0xff;
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < dynamic_files; i++) {
|
||||
file_t *f = &dynamic_file[i];
|
||||
if ((f->fid & 0xff00) == (PRKD_PREFIX << 8)) {
|
||||
res_APDU[res_APDU_size++] = PRKD_PREFIX;
|
||||
res_APDU[res_APDU_size++] = f->fid & 0xff;
|
||||
}
|
||||
}
|
||||
//second CD
|
||||
for (int i = 0; i < dynamic_files; i++) {
|
||||
file_t *f = &dynamic_file[i];
|
||||
if ((f->fid & 0xff00) == (CD_PREFIX << 8)) {
|
||||
res_APDU[res_APDU_size++] = CD_PREFIX;
|
||||
res_APDU[res_APDU_size++] = f->fid & 0xff;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < dynamic_files; i++) {
|
||||
file_t *f = &dynamic_file[i];
|
||||
if ((f->fid & 0xff00) == (DCOD_PREFIX << 8)) {
|
||||
res_APDU[res_APDU_size++] = DCOD_PREFIX;
|
||||
res_APDU[res_APDU_size++] = f->fid & 0xff;
|
||||
}
|
||||
}
|
||||
#if !defined(ENABLE_EMULATION) && !defined(ESP_PLATFORM)
|
||||
if ((apdu.rlen + 2 + 10) % 64 == 0) { // FIX for strange behaviour with PSCS and multiple of 64
|
||||
res_APDU[res_APDU_size++] = 0;
|
||||
res_APDU[res_APDU_size++] = 0;
|
||||
}
|
||||
#endif
|
||||
return SW_OK();
|
||||
}
|
||||
89
src/hsm/cmd_mse.c
Normal file
89
src/hsm/cmd_mse.c
Normal file
|
|
@ -0,0 +1,89 @@
|
|||
/*
|
||||
* This file is part of the Pico HSM distribution (https://github.com/polhenarejos/pico-hsm).
|
||||
* Copyright (c) 2022 Pol Henarejos.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "sc_hsm.h"
|
||||
#include "asn1.h"
|
||||
#include "oid.h"
|
||||
#include "eac.h"
|
||||
#include "files.h"
|
||||
#include "cvc.h"
|
||||
|
||||
file_t *ef_puk_aut = NULL;
|
||||
|
||||
int cmd_mse() {
|
||||
int p1 = P1(apdu);
|
||||
int p2 = P2(apdu);
|
||||
if (p2 != 0xA4 && p2 != 0xA6 && p2 != 0xAA && p2 != 0xB4 && p2 != 0xB6 && p2 != 0xB8) {
|
||||
return SW_INCORRECT_P1P2();
|
||||
}
|
||||
if (p1 & 0x1) { //SET
|
||||
uint16_t tag = 0x0;
|
||||
uint8_t *tag_data = NULL, *p = NULL;
|
||||
uint16_t tag_len = 0;
|
||||
asn1_ctx_t ctxi;
|
||||
asn1_ctx_init(apdu.data, (uint16_t)apdu.nc, &ctxi);
|
||||
while (walk_tlv(&ctxi, &p, &tag, &tag_len, &tag_data)) {
|
||||
if (tag == 0x80) {
|
||||
if (p2 == 0xA4) {
|
||||
if (tag_len == 10 &&
|
||||
memcmp(tag_data, OID_ID_CA_ECDH_AES_CBC_CMAC_128, tag_len) == 0) {
|
||||
sm_set_protocol(MSE_AES);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (tag == 0x83) {
|
||||
if (tag_len == 1) {
|
||||
|
||||
}
|
||||
else {
|
||||
if (p2 == 0xB6) {
|
||||
if (puk_store_select_chr(tag_data) == PICOKEY_OK) {
|
||||
return SW_OK();
|
||||
}
|
||||
}
|
||||
else if (p2 == 0xA4) { /* Aut */
|
||||
for (uint8_t i = 0; i < MAX_PUK; i++) {
|
||||
file_t *ef = search_file(EF_PUK + i);
|
||||
if (!ef) {
|
||||
break;
|
||||
}
|
||||
if (!file_has_data(ef)) {
|
||||
break;
|
||||
}
|
||||
uint16_t chr_len = 0;
|
||||
const uint8_t *chr = cvc_get_chr(file_get_data(ef),
|
||||
file_get_size(ef),
|
||||
&chr_len);
|
||||
if (memcmp(chr, tag_data, chr_len) == 0) {
|
||||
ef_puk_aut = ef;
|
||||
if (puk_status[i] == 1) {
|
||||
return SW_CONDITIONS_NOT_SATISFIED(); // It is correct
|
||||
}
|
||||
return SW_OK();
|
||||
}
|
||||
}
|
||||
}
|
||||
return SW_REFERENCE_NOT_FOUND();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
return SW_INCORRECT_P1P2();
|
||||
}
|
||||
return SW_OK();
|
||||
}
|
||||
161
src/hsm/cmd_pso.c
Normal file
161
src/hsm/cmd_pso.c
Normal file
|
|
@ -0,0 +1,161 @@
|
|||
/*
|
||||
* This file is part of the Pico HSM distribution (https://github.com/polhenarejos/pico-hsm).
|
||||
* Copyright (c) 2022 Pol Henarejos.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "sc_hsm.h"
|
||||
#include "oid.h"
|
||||
#include "asn1.h"
|
||||
#include "cvc.h"
|
||||
|
||||
extern int add_cert_puk_store(const uint8_t *data, uint16_t data_len, bool copy);
|
||||
extern PUK *current_puk;
|
||||
|
||||
int cmd_pso() {
|
||||
uint8_t p1 = P1(apdu), p2 = P2(apdu);
|
||||
if (p1 == 0x0 && (p2 == 0x92 || p2 == 0xAE || p2 == 0xBE)) { /* Verify certificate */
|
||||
if (apdu.nc == 0) {
|
||||
return SW_WRONG_LENGTH();
|
||||
}
|
||||
if (current_puk == NULL) {
|
||||
return SW_REFERENCE_NOT_FOUND();
|
||||
}
|
||||
if (apdu.data[0] != 0x7F || apdu.data[1] != 0x21) {
|
||||
uint8_t tlv_len = 2 + format_tlv_len((uint16_t)apdu.nc, NULL);
|
||||
memmove(apdu.data + tlv_len, apdu.data, apdu.nc);
|
||||
memcpy(apdu.data, "\x7F\x21", 2);
|
||||
format_tlv_len((uint16_t)apdu.nc, apdu.data + 2);
|
||||
apdu.nc += tlv_len;
|
||||
}
|
||||
int r = cvc_verify(apdu.data, (uint16_t)apdu.nc, current_puk->cvcert, current_puk->cvcert_len);
|
||||
if (r != PICOKEY_OK) {
|
||||
if (r == PICOKEY_WRONG_DATA) {
|
||||
return SW_DATA_INVALID();
|
||||
}
|
||||
else if (r == PICOKEY_WRONG_SIGNATURE) {
|
||||
return SW_CONDITIONS_NOT_SATISFIED();
|
||||
}
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
for (uint8_t i = 0; i < 0xfe; i++) {
|
||||
uint16_t fid = (CA_CERTIFICATE_PREFIX << 8) | i;
|
||||
file_t *ca_ef = search_file(fid);
|
||||
if (!ca_ef) {
|
||||
ca_ef = file_new(fid);
|
||||
file_put_data(ca_ef, apdu.data, (uint16_t)apdu.nc);
|
||||
if (add_cert_puk_store(file_get_data(ca_ef), file_get_size(ca_ef),
|
||||
false) != PICOKEY_OK) {
|
||||
return SW_FILE_FULL();
|
||||
}
|
||||
|
||||
uint16_t chr_len = 0;
|
||||
const uint8_t *chr = cvc_get_chr(apdu.data, (uint16_t)apdu.nc, &chr_len);
|
||||
if (chr == NULL) {
|
||||
return SW_WRONG_DATA();
|
||||
}
|
||||
uint16_t puk_len = 0, puk_bin_len = 0;
|
||||
const uint8_t *puk = cvc_get_pub(apdu.data, (uint16_t)apdu.nc, &puk_len), *puk_bin = NULL;
|
||||
if (puk == NULL) {
|
||||
return SW_WRONG_DATA();
|
||||
}
|
||||
uint16_t oid_len = 0;
|
||||
const uint8_t *oid = cvc_get_field(puk, puk_len, &oid_len, 0x6);
|
||||
if (oid == NULL) {
|
||||
return SW_WRONG_DATA();
|
||||
}
|
||||
if (memcmp(oid, OID_ID_TA_RSA, 9) == 0) { //RSA
|
||||
puk_bin = cvc_get_field(puk, puk_len, &puk_bin_len, 0x81);
|
||||
if (!puk_bin) {
|
||||
return SW_WRONG_DATA();
|
||||
}
|
||||
}
|
||||
else if (memcmp(oid, OID_ID_TA_ECDSA, 9) == 0) { //ECC
|
||||
mbedtls_ecp_group_id ec_id = cvc_inherite_ec_group(apdu.data, (uint16_t)apdu.nc);
|
||||
mbedtls_ecp_group grp;
|
||||
mbedtls_ecp_group_init(&grp);
|
||||
if (mbedtls_ecp_group_load(&grp, ec_id) != 0) {
|
||||
mbedtls_ecp_group_free(&grp);
|
||||
return SW_WRONG_DATA();
|
||||
}
|
||||
uint16_t plen = (uint16_t)mbedtls_mpi_size(&grp.P);
|
||||
uint16_t t86_len = 0;
|
||||
const uint8_t *t86 = cvc_get_field(puk, puk_len, &t86_len, 0x86);
|
||||
if (mbedtls_ecp_get_type(&grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) {
|
||||
if (plen != t86_len) {
|
||||
mbedtls_ecp_group_free(&grp);
|
||||
return SW_WRONG_DATA();
|
||||
}
|
||||
puk_bin = t86;
|
||||
puk_bin_len = t86_len;
|
||||
}
|
||||
else if (mbedtls_ecp_get_type(&grp) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) {
|
||||
if (t86[0] == 0x2 || t86[0] == 0x3) {
|
||||
if (t86_len != plen + 1) {
|
||||
mbedtls_ecp_group_free(&grp);
|
||||
return SW_WRONG_DATA();
|
||||
}
|
||||
}
|
||||
else if (t86[0] == 0x4) {
|
||||
if (t86_len != 2 * plen + 1) {
|
||||
mbedtls_ecp_group_free(&grp);
|
||||
return SW_WRONG_DATA();
|
||||
}
|
||||
}
|
||||
else {
|
||||
mbedtls_ecp_group_free(&grp);
|
||||
return SW_WRONG_DATA();
|
||||
}
|
||||
puk_bin = t86 + 1;
|
||||
puk_bin_len = plen;
|
||||
}
|
||||
mbedtls_ecp_group_free(&grp);
|
||||
if (!puk_bin) {
|
||||
return SW_WRONG_DATA();
|
||||
}
|
||||
}
|
||||
file_t *cd_ef = file_new((CD_PREFIX << 8) | i);
|
||||
uint16_t cd_len = (uint16_t)asn1_build_cert_description(chr,
|
||||
chr_len,
|
||||
puk_bin,
|
||||
puk_bin_len,
|
||||
fid,
|
||||
NULL,
|
||||
0);
|
||||
if (cd_len == 0) {
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
uint8_t *buf = (uint8_t *) calloc(cd_len, sizeof(uint8_t));
|
||||
r = (int)asn1_build_cert_description(chr,
|
||||
chr_len,
|
||||
puk_bin,
|
||||
puk_bin_len,
|
||||
fid,
|
||||
buf,
|
||||
cd_len);
|
||||
file_put_data(cd_ef, buf, cd_len);
|
||||
free(buf);
|
||||
if (r == 0) {
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
low_flash_available();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
return SW_INCORRECT_P1P2();
|
||||
}
|
||||
return SW_OK();
|
||||
}
|
||||
99
src/hsm/cmd_puk_auth.c
Normal file
99
src/hsm/cmd_puk_auth.c
Normal file
|
|
@ -0,0 +1,99 @@
|
|||
/*
|
||||
* This file is part of the Pico HSM distribution (https://github.com/polhenarejos/pico-hsm).
|
||||
* Copyright (c) 2022 Pol Henarejos.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "sc_hsm.h"
|
||||
#include "files.h"
|
||||
#include "cvc.h"
|
||||
|
||||
int cmd_puk_auth() {
|
||||
uint8_t p1 = P1(apdu), p2 = P2(apdu);
|
||||
file_t *ef_puk = search_file(EF_PUKAUT);
|
||||
if (!file_has_data(ef_puk)) {
|
||||
if (apdu.nc > 0) {
|
||||
return SW_FILE_NOT_FOUND();
|
||||
}
|
||||
return SW_INCORRECT_P1P2();
|
||||
}
|
||||
uint8_t *puk_data = file_get_data(ef_puk);
|
||||
if (apdu.nc > 0) {
|
||||
if (p1 == 0x0 || p1 == 0x1) {
|
||||
file_t *ef = NULL;
|
||||
if (p1 == 0x0) { /* Add */
|
||||
if (p2 != 0x0) {
|
||||
return SW_INCORRECT_P1P2();
|
||||
}
|
||||
for (uint8_t i = 0; i < puk_data[0]; i++) {
|
||||
ef = search_file(EF_PUK + i);
|
||||
if (!ef) { /* Never should not happen */
|
||||
return SW_MEMORY_FAILURE();
|
||||
}
|
||||
if (!file_has_data(ef)) { /* found first empty slot */
|
||||
break;
|
||||
}
|
||||
}
|
||||
uint8_t *tmp = (uint8_t *) calloc(file_get_size(ef_puk), sizeof(uint8_t));
|
||||
memcpy(tmp, puk_data, file_get_size(ef_puk));
|
||||
tmp[1] = puk_data[1] - 1;
|
||||
file_put_data(ef_puk, tmp, file_get_size(ef_puk));
|
||||
puk_data = file_get_data(ef_puk);
|
||||
free(tmp);
|
||||
}
|
||||
else if (p1 == 0x1) { /* Replace */
|
||||
if (p2 >= puk_data[0]) {
|
||||
return SW_INCORRECT_P1P2();
|
||||
}
|
||||
ef = search_file(EF_PUK + p2);
|
||||
if (!ef) { /* Never should not happen */
|
||||
return SW_MEMORY_FAILURE();
|
||||
}
|
||||
}
|
||||
file_put_data(ef, apdu.data, (uint16_t)apdu.nc);
|
||||
low_flash_available();
|
||||
}
|
||||
else {
|
||||
return SW_INCORRECT_P1P2();
|
||||
}
|
||||
}
|
||||
if (p1 == 0x2) {
|
||||
if (p2 >= puk_data[0]) {
|
||||
return SW_INCORRECT_P1P2();
|
||||
}
|
||||
file_t *ef = search_file(EF_PUK + p2);
|
||||
if (!ef) {
|
||||
return SW_INCORRECT_P1P2();
|
||||
}
|
||||
if (!file_has_data(ef)) {
|
||||
return SW_REFERENCE_NOT_FOUND();
|
||||
}
|
||||
uint16_t chr_len = 0;
|
||||
const uint8_t *chr = cvc_get_chr(file_get_data(ef), file_get_size(ef), &chr_len);
|
||||
if (chr) {
|
||||
memcpy(res_APDU, chr, chr_len);
|
||||
res_APDU_size = chr_len;
|
||||
}
|
||||
return set_res_sw(0x90, puk_status[p2]);
|
||||
}
|
||||
else {
|
||||
memcpy(res_APDU, puk_data, 3);
|
||||
res_APDU[3] = 0;
|
||||
for (int i = 0; i < puk_data[0]; i++) {
|
||||
res_APDU[3] += puk_status[i];
|
||||
}
|
||||
res_APDU_size = 4;
|
||||
}
|
||||
return SW_OK();
|
||||
}
|
||||
98
src/hsm/cmd_read_binary.c
Normal file
98
src/hsm/cmd_read_binary.c
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
/*
|
||||
* This file is part of the Pico HSM distribution (https://github.com/polhenarejos/pico-hsm).
|
||||
* Copyright (c) 2022 Pol Henarejos.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "sc_hsm.h"
|
||||
|
||||
int cmd_read_binary() {
|
||||
uint16_t offset = 0;
|
||||
uint8_t ins = INS(apdu), p1 = P1(apdu), p2 = P2(apdu);
|
||||
const file_t *ef = NULL;
|
||||
|
||||
if ((ins & 0x1) == 0) {
|
||||
if ((p1 & 0x80) != 0) {
|
||||
if (!(ef = search_file(p1 & 0x1f))) {
|
||||
return SW_FILE_NOT_FOUND();
|
||||
}
|
||||
offset = p2;
|
||||
}
|
||||
else {
|
||||
offset = make_uint16_t_be(p1, p2) & 0x7fff;
|
||||
ef = currentEF;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (p1 == 0 && (p2 & 0xE0) == 0 && (p2 & 0x1f) != 0 && (p2 & 0x1f) != 0x1f) {
|
||||
if (!(ef = search_file(p2 & 0x1f))) {
|
||||
return SW_FILE_NOT_FOUND();
|
||||
}
|
||||
}
|
||||
else {
|
||||
uint16_t file_id = make_uint16_t_be(p1, p2); // & 0x7fff;
|
||||
if (file_id == 0x0) {
|
||||
ef = currentEF;
|
||||
}
|
||||
else if (!(ef = search_file(file_id))) {
|
||||
return SW_FILE_NOT_FOUND();
|
||||
}
|
||||
|
||||
if (apdu.data[0] != 0x54) {
|
||||
return SW_WRONG_DATA();
|
||||
}
|
||||
|
||||
offset = 0;
|
||||
for (int d = 0; d < apdu.data[1]; d++) {
|
||||
offset |= apdu.data[2 + d] << (apdu.data[1] - 1 - d) * 8;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ((ef->fid >> 8) == KEY_PREFIX || !authenticate_action(ef, ACL_OP_READ_SEARCH)) {
|
||||
return SW_SECURITY_STATUS_NOT_SATISFIED();
|
||||
}
|
||||
if (ef->data) {
|
||||
if ((ef->type & FILE_DATA_FUNC) == FILE_DATA_FUNC) {
|
||||
uint16_t data_len = (uint16_t)((int (*)(const file_t *, int))(ef->data))((const file_t *) ef, 1); //already copies content to res_APDU
|
||||
if (offset > data_len) {
|
||||
return SW_WRONG_P1P2();
|
||||
}
|
||||
uint16_t maxle = data_len - offset;
|
||||
if (apdu.ne > maxle) {
|
||||
apdu.ne = maxle;
|
||||
}
|
||||
if (offset) {
|
||||
memmove(res_APDU, res_APDU + offset, res_APDU_size - offset);
|
||||
//res_APDU += offset;
|
||||
res_APDU_size -= offset;
|
||||
}
|
||||
}
|
||||
else {
|
||||
uint16_t data_len = file_get_size(ef);
|
||||
if (offset > data_len) {
|
||||
return SW_WARNING_EOF();
|
||||
}
|
||||
|
||||
//uint16_t maxle = data_len - offset;
|
||||
//if (apdu.ne > maxle) {
|
||||
// apdu.ne = maxle;
|
||||
//}
|
||||
memcpy(res_APDU, file_get_data(ef) + offset, data_len - offset);
|
||||
res_APDU_size = data_len - offset;
|
||||
}
|
||||
}
|
||||
|
||||
return SW_OK();
|
||||
}
|
||||
108
src/hsm/cmd_reset_retry.c
Normal file
108
src/hsm/cmd_reset_retry.c
Normal file
|
|
@ -0,0 +1,108 @@
|
|||
/*
|
||||
* This file is part of the Pico HSM distribution (https://github.com/polhenarejos/pico-hsm).
|
||||
* Copyright (c) 2022 Pol Henarejos.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "crypto_utils.h"
|
||||
#include "sc_hsm.h"
|
||||
#include "kek.h"
|
||||
|
||||
int cmd_reset_retry() {
|
||||
if (P2(apdu) != 0x81) {
|
||||
return SW_REFERENCE_NOT_FOUND();
|
||||
}
|
||||
if (!file_sopin || !file_pin1) {
|
||||
return SW_FILE_NOT_FOUND();
|
||||
}
|
||||
if (!file_has_data(file_sopin)) {
|
||||
return SW_REFERENCE_NOT_FOUND();
|
||||
}
|
||||
uint16_t opts = get_device_options();
|
||||
if (!(opts & HSM_OPT_RRC)) {
|
||||
return SW_COMMAND_NOT_ALLOWED();
|
||||
}
|
||||
if (P1(apdu) == 0x0 || P1(apdu) == 0x2) {
|
||||
uint8_t newpin_len = 0;
|
||||
if (P1(apdu) == 0x0) {
|
||||
uint8_t so_pin_len = file_read_uint8(file_sopin);
|
||||
if ((uint16_t)apdu.nc <= so_pin_len + 1) {
|
||||
return SW_WRONG_LENGTH();
|
||||
}
|
||||
uint16_t r = check_pin(file_sopin, apdu.data, so_pin_len);
|
||||
if (r != 0x9000) {
|
||||
return r;
|
||||
}
|
||||
newpin_len = (uint8_t)apdu.nc - so_pin_len;
|
||||
}
|
||||
else if (P1(apdu) == 0x2) {
|
||||
if (!has_session_sopin) {
|
||||
return SW_CONDITIONS_NOT_SATISFIED();
|
||||
}
|
||||
if (apdu.nc > 16) {
|
||||
return SW_WRONG_LENGTH();
|
||||
}
|
||||
newpin_len = (uint8_t)apdu.nc;
|
||||
}
|
||||
uint8_t dhash[33];
|
||||
dhash[0] = newpin_len;
|
||||
double_hash_pin(apdu.data + (apdu.nc - newpin_len), newpin_len, dhash + 1);
|
||||
file_put_data(file_pin1, dhash, sizeof(dhash));
|
||||
if (pin_reset_retries(file_pin1, true) != PICOKEY_OK) {
|
||||
return SW_MEMORY_FAILURE();
|
||||
}
|
||||
uint8_t mkek[MKEK_SIZE];
|
||||
int r = load_mkek(mkek); //loads the MKEK with SO pin
|
||||
if (r != PICOKEY_OK) {
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
hash_multi(apdu.data + (apdu.nc - newpin_len), newpin_len, session_pin);
|
||||
has_session_pin = true;
|
||||
r = store_mkek(mkek);
|
||||
release_mkek(mkek);
|
||||
if (r != PICOKEY_OK) {
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
low_flash_available();
|
||||
return SW_OK();
|
||||
}
|
||||
else if (P1(apdu) == 0x1 || P1(apdu) == 0x3) {
|
||||
if (!(opts & HSM_OPT_RRC_RESET_ONLY)) {
|
||||
return SW_COMMAND_NOT_ALLOWED();
|
||||
}
|
||||
if (P1(apdu) == 0x1) {
|
||||
uint8_t so_pin_len = file_read_uint8(file_sopin);
|
||||
if (apdu.nc != so_pin_len) {
|
||||
return SW_WRONG_LENGTH();
|
||||
}
|
||||
uint16_t r = check_pin(file_sopin, apdu.data, so_pin_len);
|
||||
if (r != 0x9000) {
|
||||
return r;
|
||||
}
|
||||
}
|
||||
else if (P1(apdu) == 0x3) {
|
||||
if (!has_session_sopin) {
|
||||
return SW_CONDITIONS_NOT_SATISFIED();
|
||||
}
|
||||
if (apdu.nc != 0) {
|
||||
return SW_WRONG_LENGTH();
|
||||
}
|
||||
}
|
||||
if (pin_reset_retries(file_pin1, true) != PICOKEY_OK) {
|
||||
return SW_MEMORY_FAILURE();
|
||||
}
|
||||
return SW_OK();
|
||||
}
|
||||
return SW_INCORRECT_P1P2();
|
||||
}
|
||||
134
src/hsm/cmd_select.c
Normal file
134
src/hsm/cmd_select.c
Normal file
|
|
@ -0,0 +1,134 @@
|
|||
/*
|
||||
* This file is part of the Pico HSM distribution (https://github.com/polhenarejos/pico-hsm).
|
||||
* Copyright (c) 2022 Pol Henarejos.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "sc_hsm.h"
|
||||
#include "version.h"
|
||||
|
||||
void select_file(file_t *pe) {
|
||||
if (!pe) {
|
||||
currentDF = (file_t *) MF;
|
||||
currentEF = NULL;
|
||||
}
|
||||
else if (pe->type & (FILE_TYPE_INTERNAL_EF|FILE_TYPE_WORKING_EF)) {
|
||||
currentEF = pe;
|
||||
currentDF = &file_entries[pe->parent];
|
||||
}
|
||||
else {
|
||||
currentDF = pe;
|
||||
}
|
||||
if (currentEF == file_openpgp || currentEF == file_sc_hsm) {
|
||||
selected_applet = currentEF;
|
||||
//sc_hsm_unload(); //reset auth status
|
||||
}
|
||||
}
|
||||
|
||||
int cmd_select() {
|
||||
uint8_t p1 = P1(apdu);
|
||||
uint8_t p2 = P2(apdu);
|
||||
file_t *pe = NULL;
|
||||
uint16_t fid = 0x0;
|
||||
|
||||
// Only "first or only occurence" supported
|
||||
//if ((p2 & 0xF3) != 0x00) {
|
||||
// return SW_INCORRECT_P1P2();
|
||||
//}
|
||||
|
||||
if (apdu.nc == 2) {
|
||||
fid = get_uint16_t_be(apdu.data);
|
||||
}
|
||||
|
||||
//if ((fid & 0xff00) == (KEY_PREFIX << 8))
|
||||
// fid = (PRKD_PREFIX << 8) | (fid & 0xff);
|
||||
|
||||
/*uint8_t pfx = fid >> 8;*/
|
||||
/*if (pfx == PRKD_PREFIX ||
|
||||
pfx == CD_PREFIX ||
|
||||
pfx == CA_CERTIFICATE_PREFIX ||
|
||||
pfx == KEY_PREFIX ||
|
||||
pfx == EE_CERTIFICATE_PREFIX ||
|
||||
pfx == DCOD_PREFIX ||
|
||||
pfx == DATA_PREFIX ||
|
||||
pfx == PROT_DATA_PREFIX) {*/
|
||||
if (fid != 0x0 && !(pe = search_file(fid))) {
|
||||
return SW_FILE_NOT_FOUND();
|
||||
}
|
||||
/*}*/
|
||||
if (!pe) {
|
||||
if (p1 == 0x0) { //Select MF, DF or EF - File identifier or absent
|
||||
if (apdu.nc == 0) {
|
||||
pe = (file_t *) MF;
|
||||
//ac_fini();
|
||||
}
|
||||
else if (apdu.nc == 2) {
|
||||
if (!(pe = search_by_fid(fid, NULL, SPECIFY_ANY))) {
|
||||
return SW_FILE_NOT_FOUND();
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (p1 == 0x01) { //Select child DF - DF identifier
|
||||
if (!(pe = search_by_fid(fid, currentDF, SPECIFY_DF))) {
|
||||
return SW_FILE_NOT_FOUND();
|
||||
}
|
||||
}
|
||||
else if (p1 == 0x02) { //Select EF under the current DF - EF identifier
|
||||
if (!(pe = search_by_fid(fid, currentDF, SPECIFY_EF))) {
|
||||
return SW_FILE_NOT_FOUND();
|
||||
}
|
||||
}
|
||||
else if (p1 == 0x03) { //Select parent DF of the current DF - Absent
|
||||
if (apdu.nc != 0) {
|
||||
return SW_FILE_NOT_FOUND();
|
||||
}
|
||||
}
|
||||
else if (p1 == 0x04) { //Select by DF name - e.g., [truncated] application identifier
|
||||
if (!(pe = search_by_name(apdu.data, (uint16_t)apdu.nc))) {
|
||||
return SW_FILE_NOT_FOUND();
|
||||
}
|
||||
if (card_terminated) {
|
||||
return set_res_sw(0x62, 0x85);
|
||||
}
|
||||
}
|
||||
else if (p1 == 0x08) { //Select from the MF - Path without the MF identifier
|
||||
if (!(pe = search_by_path(apdu.data, (uint8_t)apdu.nc, MF))) {
|
||||
return SW_FILE_NOT_FOUND();
|
||||
}
|
||||
}
|
||||
else if (p1 == 0x09) { //Select from the current DF - Path without the current DF identifier
|
||||
if (!(pe = search_by_path(apdu.data, (uint8_t)apdu.nc, currentDF))) {
|
||||
return SW_FILE_NOT_FOUND();
|
||||
}
|
||||
}
|
||||
}
|
||||
if ((p2 & 0xfc) == 0x00 || (p2 & 0xfc) == 0x04) {
|
||||
process_fci(pe, 0);
|
||||
if (pe == file_sc_hsm) {
|
||||
res_APDU[res_APDU_size++] = 0x85;
|
||||
res_APDU[res_APDU_size++] = 5;
|
||||
uint16_t opts = get_device_options();
|
||||
res_APDU_size += put_uint16_t_be(opts, res_APDU + res_APDU_size);
|
||||
res_APDU[res_APDU_size++] = 0xFF;
|
||||
res_APDU[res_APDU_size++] = HSM_VERSION_MAJOR;
|
||||
res_APDU[res_APDU_size++] = HSM_VERSION_MINOR;
|
||||
res_APDU[1] = (uint8_t)res_APDU_size - 2;
|
||||
}
|
||||
}
|
||||
else {
|
||||
return SW_INCORRECT_P1P2();
|
||||
}
|
||||
select_file(pe);
|
||||
return SW_OK();
|
||||
}
|
||||
35
src/hsm/cmd_session_pin.c
Normal file
35
src/hsm/cmd_session_pin.c
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
/*
|
||||
* This file is part of the Pico HSM distribution (https://github.com/polhenarejos/pico-hsm).
|
||||
* Copyright (c) 2022 Pol Henarejos.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "sc_hsm.h"
|
||||
#include "random.h"
|
||||
#include "eac.h"
|
||||
|
||||
int cmd_session_pin() {
|
||||
if (P1(apdu) == 0x01 && P2(apdu) == 0x81) {
|
||||
memcpy(sm_session_pin, random_bytes_get(8), 8);
|
||||
sm_session_pin_len = 8;
|
||||
|
||||
memcpy(res_APDU, sm_session_pin, sm_session_pin_len);
|
||||
res_APDU_size = sm_session_pin_len;
|
||||
apdu.ne = sm_session_pin_len;
|
||||
}
|
||||
else {
|
||||
return SW_INCORRECT_P1P2();
|
||||
}
|
||||
return SW_OK();
|
||||
}
|
||||
325
src/hsm/cmd_signature.c
Normal file
325
src/hsm/cmd_signature.c
Normal file
|
|
@ -0,0 +1,325 @@
|
|||
/*
|
||||
* This file is part of the Pico HSM distribution (https://github.com/polhenarejos/pico-hsm).
|
||||
* Copyright (c) 2022 Pol Henarejos.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include "sc_hsm.h"
|
||||
#include "crypto_utils.h"
|
||||
#include "sc_hsm.h"
|
||||
#include "asn1.h"
|
||||
#include "mbedtls/oid.h"
|
||||
#include "random.h"
|
||||
#ifdef MBEDTLS_EDDSA_C
|
||||
#include "mbedtls/eddsa.h"
|
||||
#endif
|
||||
|
||||
extern mbedtls_ecp_keypair hd_context;
|
||||
extern uint8_t hd_keytype;
|
||||
|
||||
//-----
|
||||
/* From OpenSC */
|
||||
static const uint8_t hdr_md5[] = {
|
||||
0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48, 0x86, 0xf7,
|
||||
0x0d, 0x02, 0x05, 0x05, 0x00, 0x04, 0x10
|
||||
};
|
||||
static const uint8_t hdr_sha1[] = {
|
||||
0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a,
|
||||
0x05, 0x00, 0x04, 0x14
|
||||
};
|
||||
static const uint8_t hdr_sha256[] = {
|
||||
0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65,
|
||||
0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20
|
||||
};
|
||||
static const uint8_t hdr_sha384[] = {
|
||||
0x30, 0x41, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65,
|
||||
0x03, 0x04, 0x02, 0x02, 0x05, 0x00, 0x04, 0x30
|
||||
};
|
||||
static const uint8_t hdr_sha512[] = {
|
||||
0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65,
|
||||
0x03, 0x04, 0x02, 0x03, 0x05, 0x00, 0x04, 0x40
|
||||
};
|
||||
static const uint8_t hdr_sha224[] = {
|
||||
0x30, 0x2d, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65,
|
||||
0x03, 0x04, 0x02, 0x04, 0x05, 0x00, 0x04, 0x1c
|
||||
};
|
||||
static const uint8_t hdr_ripemd160[] = {
|
||||
0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x24, 0x03, 0x02, 0x01,
|
||||
0x05, 0x00, 0x04, 0x14
|
||||
};
|
||||
static const struct digest_info_prefix {
|
||||
mbedtls_md_type_t algorithm;
|
||||
const uint8_t *hdr;
|
||||
uint16_t hdr_len;
|
||||
uint16_t hash_len;
|
||||
} digest_info_prefix[] = {
|
||||
{ MBEDTLS_MD_MD5, hdr_md5, sizeof(hdr_md5), 16 },
|
||||
{ MBEDTLS_MD_SHA1, hdr_sha1, sizeof(hdr_sha1), 20 },
|
||||
{ MBEDTLS_MD_SHA256, hdr_sha256, sizeof(hdr_sha256), 32 },
|
||||
{ MBEDTLS_MD_SHA384, hdr_sha384, sizeof(hdr_sha384), 48 },
|
||||
{ MBEDTLS_MD_SHA512, hdr_sha512, sizeof(hdr_sha512), 64 },
|
||||
{ MBEDTLS_MD_SHA224, hdr_sha224, sizeof(hdr_sha224), 28 },
|
||||
{ MBEDTLS_MD_RIPEMD160, hdr_ripemd160, sizeof(hdr_ripemd160), 20 },
|
||||
{ 0, NULL, 0, 0 }
|
||||
};
|
||||
int pkcs1_strip_digest_info_prefix(mbedtls_md_type_t *algorithm,
|
||||
const uint8_t *in_dat,
|
||||
uint16_t in_len,
|
||||
uint8_t *out_dat,
|
||||
uint16_t *out_len) {
|
||||
for (int i = 0; digest_info_prefix[i].algorithm != 0; i++) {
|
||||
uint16_t hdr_len = digest_info_prefix[i].hdr_len, hash_len = digest_info_prefix[i].hash_len;
|
||||
const uint8_t *hdr = digest_info_prefix[i].hdr;
|
||||
if (in_len == (hdr_len + hash_len) && !memcmp(in_dat, hdr, hdr_len)) {
|
||||
if (algorithm) {
|
||||
*algorithm = digest_info_prefix[i].algorithm;
|
||||
}
|
||||
if (out_dat == NULL) {
|
||||
return PICOKEY_OK;
|
||||
}
|
||||
if (*out_len < hash_len) {
|
||||
return PICOKEY_WRONG_DATA;
|
||||
}
|
||||
memmove(out_dat, in_dat + hdr_len, hash_len);
|
||||
*out_len = hash_len;
|
||||
return PICOKEY_OK;
|
||||
}
|
||||
}
|
||||
return PICOKEY_EXEC_ERROR;
|
||||
}
|
||||
//-----
|
||||
|
||||
int cmd_signature() {
|
||||
uint8_t key_id = P1(apdu);
|
||||
uint8_t p2 = P2(apdu);
|
||||
mbedtls_md_type_t md = MBEDTLS_MD_NONE;
|
||||
file_t *fkey;
|
||||
if (!isUserAuthenticated) {
|
||||
return SW_SECURITY_STATUS_NOT_SATISFIED();
|
||||
}
|
||||
if (!(fkey = search_file((KEY_PREFIX << 8) | key_id)) || !file_has_data(fkey)) {
|
||||
return SW_FILE_NOT_FOUND();
|
||||
}
|
||||
if (get_key_counter(fkey) == 0) {
|
||||
return SW_FILE_FULL();
|
||||
}
|
||||
if (key_has_purpose(fkey, p2) == false) {
|
||||
return SW_CONDITIONS_NOT_SATISFIED();
|
||||
}
|
||||
uint16_t key_size = file_get_size(fkey);
|
||||
if (p2 == ALGO_RSA_PKCS1_SHA1 || p2 == ALGO_RSA_PSS_SHA1 || p2 == ALGO_EC_SHA1) {
|
||||
md = MBEDTLS_MD_SHA1;
|
||||
}
|
||||
else if (p2 == ALGO_RSA_PKCS1_SHA256 || p2 == ALGO_RSA_PSS_SHA256 || p2 == ALGO_EC_SHA256) {
|
||||
md = MBEDTLS_MD_SHA256;
|
||||
}
|
||||
else if (p2 == ALGO_EC_SHA224 || p2 == ALGO_RSA_PKCS1_SHA224 || p2 == ALGO_RSA_PSS_SHA224) {
|
||||
md = MBEDTLS_MD_SHA224;
|
||||
}
|
||||
else if (p2 == ALGO_EC_SHA384 || p2 == ALGO_RSA_PKCS1_SHA384 || p2 == ALGO_RSA_PSS_SHA384) {
|
||||
md = MBEDTLS_MD_SHA384;
|
||||
}
|
||||
else if (p2 == ALGO_EC_SHA512 || p2 == ALGO_RSA_PKCS1_SHA512 || p2 == ALGO_RSA_PSS_SHA512) {
|
||||
md = MBEDTLS_MD_SHA512;
|
||||
}
|
||||
if (p2 == ALGO_RSA_PKCS1_SHA1 || p2 == ALGO_RSA_PSS_SHA1 || p2 == ALGO_EC_SHA1 ||
|
||||
p2 == ALGO_RSA_PKCS1_SHA256 || p2 == ALGO_RSA_PSS_SHA256 || p2 == ALGO_EC_SHA256 ||
|
||||
p2 == ALGO_EC_SHA224 || p2 == ALGO_EC_SHA384 || p2 == ALGO_EC_SHA512 ||
|
||||
p2 == ALGO_RSA_PKCS1_SHA224 || p2 == ALGO_RSA_PKCS1_SHA384 || p2 == ALGO_RSA_PKCS1_SHA512 ||
|
||||
p2 == ALGO_RSA_PSS_SHA224 || p2 == ALGO_RSA_PSS_SHA384 || p2 == ALGO_RSA_PSS_SHA512) {
|
||||
generic_hash(md, apdu.data, apdu.nc, apdu.data);
|
||||
apdu.nc = mbedtls_md_get_size(mbedtls_md_info_from_type(md));
|
||||
}
|
||||
if (p2 >= ALGO_RSA_RAW && p2 <= ALGO_RSA_PSS_SHA512) {
|
||||
mbedtls_rsa_context ctx;
|
||||
mbedtls_rsa_init(&ctx);
|
||||
|
||||
int r = load_private_key_rsa(&ctx, fkey);
|
||||
if (r != PICOKEY_OK) {
|
||||
mbedtls_rsa_free(&ctx);
|
||||
if (r == PICOKEY_VERIFICATION_FAILED) {
|
||||
return SW_SECURE_MESSAGE_EXEC_ERROR();
|
||||
}
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
asn1_ctx_t hash = {.len = (uint16_t)apdu.nc, .data = apdu.data};
|
||||
if (p2 == ALGO_RSA_PKCS1) { //DigestInfo attached
|
||||
uint16_t nc = (uint16_t)apdu.nc;
|
||||
if (pkcs1_strip_digest_info_prefix(&md, apdu.data, (uint16_t)apdu.nc, apdu.data,
|
||||
&nc) != PICOKEY_OK) { //gets the MD algo id and strips it off
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
apdu.nc = nc;
|
||||
}
|
||||
else {
|
||||
//sc_asn1_print_tags(apdu.data, apdu.nc);
|
||||
asn1_ctx_t ctxi, ctxo = { 0 }, oid = { 0 };
|
||||
asn1_ctx_init(apdu.data, (uint16_t)apdu.nc, &ctxi);
|
||||
if (asn1_find_tag(&ctxi, 0x30, &ctxo) && asn1_len(&ctxo) > 0) {
|
||||
asn1_ctx_t a30 = { 0 };
|
||||
if (asn1_find_tag(&ctxo, 0x30, &a30) && asn1_len(&a30) > 0) {
|
||||
asn1_find_tag(&a30, 0x6, &oid);
|
||||
}
|
||||
asn1_find_tag(&ctxo, 0x4, &hash);
|
||||
}
|
||||
if (asn1_len(&oid)) {
|
||||
if (memcmp(oid.data, MBEDTLS_OID_DIGEST_ALG_SHA1, oid.len) == 0) {
|
||||
md = MBEDTLS_MD_SHA1;
|
||||
}
|
||||
else if (memcmp(oid.data, MBEDTLS_OID_DIGEST_ALG_SHA224, oid.len) == 0) {
|
||||
md = MBEDTLS_MD_SHA224;
|
||||
}
|
||||
else if (memcmp(oid.data, MBEDTLS_OID_DIGEST_ALG_SHA256, oid.len) == 0) {
|
||||
md = MBEDTLS_MD_SHA256;
|
||||
}
|
||||
else if (memcmp(oid.data, MBEDTLS_OID_DIGEST_ALG_SHA384, oid.len) == 0) {
|
||||
md = MBEDTLS_MD_SHA384;
|
||||
}
|
||||
else if (memcmp(oid.data, MBEDTLS_OID_DIGEST_ALG_SHA512, oid.len) == 0) {
|
||||
md = MBEDTLS_MD_SHA512;
|
||||
}
|
||||
}
|
||||
if (p2 >= ALGO_RSA_PSS && p2 <= ALGO_RSA_PSS_SHA512) {
|
||||
if (p2 == ALGO_RSA_PSS && asn1_len(&oid) == 0) {
|
||||
if (apdu.nc == 20) { //default is sha1
|
||||
md = MBEDTLS_MD_SHA1;
|
||||
}
|
||||
else if (apdu.nc == 28) {
|
||||
md = MBEDTLS_MD_SHA224;
|
||||
}
|
||||
else if (apdu.nc == 32) {
|
||||
md = MBEDTLS_MD_SHA256;
|
||||
}
|
||||
else if (apdu.nc == 48) {
|
||||
md = MBEDTLS_MD_SHA384;
|
||||
}
|
||||
else if (apdu.nc == 64) {
|
||||
md = MBEDTLS_MD_SHA512;
|
||||
}
|
||||
}
|
||||
mbedtls_rsa_set_padding(&ctx, MBEDTLS_RSA_PKCS_V21, md);
|
||||
}
|
||||
}
|
||||
if (md == MBEDTLS_MD_NONE) {
|
||||
if (apdu.nc < key_size) { //needs padding
|
||||
memset(apdu.data + apdu.nc, 0, key_size - apdu.nc);
|
||||
}
|
||||
r = mbedtls_rsa_private(&ctx, random_gen, NULL, apdu.data, res_APDU);
|
||||
}
|
||||
else {
|
||||
uint8_t *signature = (uint8_t *) calloc(key_size, sizeof(uint8_t));
|
||||
r = mbedtls_rsa_pkcs1_sign(&ctx, random_gen, NULL, md, hash.len, hash.data, signature);
|
||||
memcpy(res_APDU, signature, key_size);
|
||||
free(signature);
|
||||
}
|
||||
if (r != 0) {
|
||||
mbedtls_rsa_free(&ctx);
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
res_APDU_size = key_size;
|
||||
apdu.ne = key_size;
|
||||
mbedtls_rsa_free(&ctx);
|
||||
}
|
||||
else if (p2 >= ALGO_EC_RAW && p2 <= ALGO_EC_SHA512) {
|
||||
mbedtls_ecp_keypair ctx;
|
||||
mbedtls_ecp_keypair_init(&ctx);
|
||||
md = MBEDTLS_MD_SHA256;
|
||||
if (p2 == ALGO_EC_RAW) {
|
||||
if (apdu.nc == 32) {
|
||||
md = MBEDTLS_MD_SHA256;
|
||||
}
|
||||
else if (apdu.nc == 20) {
|
||||
md = MBEDTLS_MD_SHA1;
|
||||
}
|
||||
else if (apdu.nc == 28) {
|
||||
md = MBEDTLS_MD_SHA224;
|
||||
}
|
||||
else if (apdu.nc == 48) {
|
||||
md = MBEDTLS_MD_SHA384;
|
||||
}
|
||||
else if (apdu.nc == 64) {
|
||||
md = MBEDTLS_MD_SHA512;
|
||||
}
|
||||
}
|
||||
if (p2 == ALGO_EC_SHA1) {
|
||||
md = MBEDTLS_MD_SHA1;
|
||||
}
|
||||
else if (p2 == ALGO_EC_SHA224) {
|
||||
md = MBEDTLS_MD_SHA224;
|
||||
}
|
||||
else if (p2 == ALGO_EC_SHA256) {
|
||||
md = MBEDTLS_MD_SHA256;
|
||||
}
|
||||
else if (p2 == ALGO_EC_SHA384) {
|
||||
md = MBEDTLS_MD_SHA384;
|
||||
}
|
||||
else if (p2 == ALGO_EC_SHA512) {
|
||||
md = MBEDTLS_MD_SHA512;
|
||||
}
|
||||
int r = load_private_key_ec(&ctx, fkey);
|
||||
if (r != PICOKEY_OK) {
|
||||
mbedtls_ecp_keypair_free(&ctx);
|
||||
if (r == PICOKEY_VERIFICATION_FAILED) {
|
||||
return SW_SECURE_MESSAGE_EXEC_ERROR();
|
||||
}
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
size_t olen = 0;
|
||||
uint8_t buf[MBEDTLS_ECDSA_MAX_LEN];
|
||||
#ifdef MBEDTLS_EDDSA_C
|
||||
if (ctx.grp.id == MBEDTLS_ECP_DP_ED25519 || ctx.grp.id == MBEDTLS_ECP_DP_ED448) {
|
||||
r = mbedtls_eddsa_write_signature(&ctx, apdu.data, apdu.nc, buf, sizeof(buf), &olen, MBEDTLS_EDDSA_PURE, NULL, 0, random_gen, NULL);
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
r = mbedtls_ecdsa_write_signature(&ctx, md, apdu.data, apdu.nc, buf, MBEDTLS_ECDSA_MAX_LEN,
|
||||
&olen, random_gen, NULL);
|
||||
}
|
||||
if (r != 0) {
|
||||
mbedtls_ecp_keypair_free(&ctx);
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
memcpy(res_APDU, buf, olen);
|
||||
res_APDU_size = (uint16_t)olen;
|
||||
mbedtls_ecp_keypair_free(&ctx);
|
||||
}
|
||||
else if (p2 == ALGO_HD) {
|
||||
size_t olen = 0;
|
||||
uint8_t buf[MBEDTLS_ECDSA_MAX_LEN] = {0};
|
||||
if (hd_context.grp.id == MBEDTLS_ECP_DP_NONE) {
|
||||
mbedtls_ecp_keypair_free(&hd_context);
|
||||
return SW_CONDITIONS_NOT_SATISFIED();
|
||||
}
|
||||
if (hd_keytype != 0x1 && hd_keytype != 0x2) {
|
||||
mbedtls_ecp_keypair_free(&hd_context);
|
||||
return SW_INCORRECT_PARAMS();
|
||||
}
|
||||
md = MBEDTLS_MD_SHA256;
|
||||
if (mbedtls_ecdsa_write_signature(&hd_context, md, apdu.data, apdu.nc, buf,
|
||||
MBEDTLS_ECDSA_MAX_LEN,
|
||||
&olen, random_gen, NULL) != 0) {
|
||||
mbedtls_ecp_keypair_free(&hd_context);
|
||||
return SW_EXEC_ERROR();
|
||||
}
|
||||
memcpy(res_APDU, buf, olen);
|
||||
res_APDU_size = (uint16_t)olen;
|
||||
mbedtls_ecp_keypair_free(&hd_context);
|
||||
hd_keytype = 0;
|
||||
}
|
||||
else {
|
||||
return SW_INCORRECT_P1P2();
|
||||
}
|
||||
decrement_key_counter(fkey);
|
||||
return SW_OK();
|
||||
}
|
||||
102
src/hsm/cmd_update_ef.c
Normal file
102
src/hsm/cmd_update_ef.c
Normal file
|
|
@ -0,0 +1,102 @@
|
|||
/*
|
||||
* This file is part of the Pico HSM distribution (https://github.com/polhenarejos/pico-hsm).
|
||||
* Copyright (c) 2022 Pol Henarejos.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "sc_hsm.h"
|
||||
#include "asn1.h"
|
||||
|
||||
extern void select_file(file_t *pe);
|
||||
|
||||
int cmd_update_ef() {
|
||||
uint8_t p1 = P1(apdu), p2 = P2(apdu);
|
||||
uint16_t fid = (p1 << 8) | p2;
|
||||
uint8_t *data = NULL;
|
||||
uint16_t offset = 0;
|
||||
uint16_t data_len = 0;
|
||||
file_t *ef = NULL;
|
||||
if (!isUserAuthenticated) {
|
||||
return SW_SECURITY_STATUS_NOT_SATISFIED();
|
||||
}
|
||||
if (fid == 0x0) {
|
||||
ef = currentEF;
|
||||
}
|
||||
/*
|
||||
// This should not happen
|
||||
else if (p1 != EE_CERTIFICATE_PREFIX && p1 != PRKD_PREFIX && p1 != CA_CERTIFICATE_PREFIX &&
|
||||
p1 != CD_PREFIX && p1 != DATA_PREFIX && p1 != DCOD_PREFIX &&
|
||||
p1 != PROT_DATA_PREFIX) {
|
||||
return SW_INCORRECT_P1P2();
|
||||
}
|
||||
*/
|
||||
|
||||
if (ef && !authenticate_action(ef, ACL_OP_UPDATE_ERASE)) {
|
||||
return SW_SECURITY_STATUS_NOT_SATISFIED();
|
||||
}
|
||||
|
||||
uint16_t tag = 0x0;
|
||||
uint8_t *tag_data = NULL, *p = NULL;
|
||||
uint16_t tag_len = 0;
|
||||
asn1_ctx_t ctxi;
|
||||
asn1_ctx_init(apdu.data, (uint16_t)apdu.nc, &ctxi);
|
||||
while (walk_tlv(&ctxi, &p, &tag, &tag_len, &tag_data)) {
|
||||
if (tag == 0x54) { //ofset tag
|
||||
for (size_t i = 1; i <= tag_len; i++) {
|
||||
offset |= (*tag_data++ << (8 * (tag_len - i)));
|
||||
}
|
||||
}
|
||||
else if (tag == 0x53) { //data
|
||||
data_len = (uint16_t)tag_len;
|
||||
data = tag_data;
|
||||
}
|
||||
}
|
||||
if (data_len == 0 && offset == 0) { //new file
|
||||
ef = file_new(fid);
|
||||
//if ((fid & 0xff00) == (EE_CERTIFICATE_PREFIX << 8))
|
||||
// add_file_to_chain(ef, &ef_pukdf);
|
||||
select_file(ef);
|
||||
}
|
||||
else {
|
||||
if (fid == 0x0 && !ef) {
|
||||
return SW_FILE_NOT_FOUND();
|
||||
}
|
||||
else if (fid != 0x0 && !(ef = search_file(fid))) { //if does not exist, create it
|
||||
//return SW_FILE_NOT_FOUND();
|
||||
ef = file_new(fid);
|
||||
}
|
||||
if (offset == 0) {
|
||||
int r = file_put_data(ef, data, data_len);
|
||||
if (r != PICOKEY_OK) {
|
||||
return SW_MEMORY_FAILURE();
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (!file_has_data(ef)) {
|
||||
return SW_DATA_INVALID();
|
||||
}
|
||||
|
||||
uint8_t *data_merge = (uint8_t *) calloc(1, offset + data_len);
|
||||
memcpy(data_merge, file_get_data(ef), offset);
|
||||
memcpy(data_merge + offset, data, data_len);
|
||||
int r = file_put_data(ef, data_merge, offset + data_len);
|
||||
free(data_merge);
|
||||
if (r != PICOKEY_OK) {
|
||||
return SW_MEMORY_FAILURE();
|
||||
}
|
||||
}
|
||||
low_flash_available();
|
||||
}
|
||||
return SW_OK();
|
||||
}
|
||||
63
src/hsm/cmd_verify.c
Normal file
63
src/hsm/cmd_verify.c
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
/*
|
||||
* This file is part of the Pico HSM distribution (https://github.com/polhenarejos/pico-hsm).
|
||||
* Copyright (c) 2022 Pol Henarejos.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "sc_hsm.h"
|
||||
|
||||
int cmd_verify() {
|
||||
uint8_t p1 = P1(apdu);
|
||||
uint8_t p2 = P2(apdu);
|
||||
|
||||
if (p1 != 0x0 || (p2 & 0x60) != 0x0) {
|
||||
return SW_WRONG_P1P2();
|
||||
}
|
||||
|
||||
if (p2 == 0x81) { //UserPin
|
||||
uint16_t opts = get_device_options();
|
||||
if (opts & HSM_OPT_TRANSPORT_PIN) {
|
||||
return SW_DATA_INVALID();
|
||||
}
|
||||
if (has_session_pin && apdu.nc == 0) {
|
||||
return SW_OK();
|
||||
}
|
||||
if (*file_get_data(file_pin1) == 0 && pka_enabled() == false) { //not initialized
|
||||
return SW_REFERENCE_NOT_FOUND();
|
||||
}
|
||||
if (apdu.nc > 0) {
|
||||
return check_pin(file_pin1, apdu.data, (uint16_t)apdu.nc);
|
||||
}
|
||||
if (file_read_uint8(file_retries_pin1) == 0) {
|
||||
return SW_PIN_BLOCKED();
|
||||
}
|
||||
return set_res_sw(0x63, 0xc0 | file_read_uint8(file_retries_pin1));
|
||||
}
|
||||
else if (p2 == 0x88) { //SOPin
|
||||
if (file_read_uint8(file_sopin) == 0) { //not initialized
|
||||
return SW_REFERENCE_NOT_FOUND();
|
||||
}
|
||||
if (apdu.nc > 0) {
|
||||
return check_pin(file_sopin, apdu.data, (uint16_t)apdu.nc);
|
||||
}
|
||||
if (file_read_uint8(file_retries_sopin) == 0) {
|
||||
return SW_PIN_BLOCKED();
|
||||
}
|
||||
if (has_session_sopin) {
|
||||
return SW_OK();
|
||||
}
|
||||
return set_res_sw(0x63, 0xc0 | file_read_uint8(file_retries_sopin));
|
||||
}
|
||||
return SW_REFERENCE_NOT_FOUND();
|
||||
}
|
||||
935
src/hsm/cvc.c
Normal file
935
src/hsm/cvc.c
Normal file
|
|
@ -0,0 +1,935 @@
|
|||
/*
|
||||
* This file is part of the Pico HSM distribution (https://github.com/polhenarejos/pico-hsm).
|
||||
* Copyright (c) 2022 Pol Henarejos.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "sc_hsm.h"
|
||||
#include "cvc.h"
|
||||
#include "mbedtls/rsa.h"
|
||||
#include "mbedtls/ecdsa.h"
|
||||
#include <string.h>
|
||||
#include "asn1.h"
|
||||
#include "crypto_utils.h"
|
||||
#include "random.h"
|
||||
#include "oid.h"
|
||||
#include "mbedtls/md.h"
|
||||
#include "files.h"
|
||||
#ifdef MBEDTLS_EDDSA_C
|
||||
#include "mbedtls/eddsa.h"
|
||||
#endif
|
||||
|
||||
extern const uint8_t *dev_name;
|
||||
extern uint16_t dev_name_len;
|
||||
|
||||
uint16_t asn1_cvc_public_key_rsa(mbedtls_rsa_context *rsa, uint8_t *buf, uint16_t buf_len) {
|
||||
const uint8_t oid_rsa[] = { 0x04, 0x00, 0x7F, 0x00, 0x07, 0x02, 0x02, 0x02, 0x01, 0x02 };
|
||||
uint16_t n_size = (uint16_t)mbedtls_mpi_size(&rsa->N), e_size = (uint16_t)mbedtls_mpi_size(&rsa->E);
|
||||
uint16_t ntot_size = asn1_len_tag(0x81, n_size), etot_size = asn1_len_tag(0x82, e_size);
|
||||
uint16_t oid_len = asn1_len_tag(0x6, sizeof(oid_rsa));
|
||||
uint16_t tot_len = asn1_len_tag(0x7f49, oid_len + ntot_size + etot_size);
|
||||
if (buf == NULL || buf_len == 0) {
|
||||
return tot_len;
|
||||
}
|
||||
if (buf_len < tot_len) {
|
||||
return 0;
|
||||
}
|
||||
uint8_t *p = buf;
|
||||
memcpy(p, "\x7F\x49", 2); p += 2;
|
||||
p += format_tlv_len(oid_len + ntot_size + etot_size, p);
|
||||
//oid
|
||||
*p++ = 0x6; p += format_tlv_len(sizeof(oid_rsa), p); memcpy(p, oid_rsa, sizeof(oid_rsa));
|
||||
p += sizeof(oid_rsa);
|
||||
//n
|
||||
*p++ = 0x81; p += format_tlv_len(n_size, p); mbedtls_mpi_write_binary(&rsa->N, p, n_size);
|
||||
p += n_size;
|
||||
//n
|
||||
*p++ = 0x82; p += format_tlv_len(e_size, p); mbedtls_mpi_write_binary(&rsa->E, p, e_size);
|
||||
p += e_size;
|
||||
return tot_len;
|
||||
}
|
||||
|
||||
const uint8_t *pointA[] = {
|
||||
NULL,
|
||||
(uint8_t *)
|
||||
"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFE\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFC",
|
||||
(uint8_t *)
|
||||
"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFE\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFE",
|
||||
(uint8_t *)
|
||||
"\xFF\xFF\xFF\xFF\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFC",
|
||||
(uint8_t *)
|
||||
"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFE\xFF\xFF\xFF\xFF\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF\xFF\xFC",
|
||||
(uint8_t *)
|
||||
"\x01\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFC",
|
||||
};
|
||||
|
||||
uint16_t asn1_cvc_public_key_ecdsa(mbedtls_ecp_keypair *ecdsa, uint8_t *buf, uint16_t buf_len) {
|
||||
uint8_t Y_buf[MBEDTLS_ECP_MAX_PT_LEN], G_buf[MBEDTLS_ECP_MAX_PT_LEN];
|
||||
const uint8_t oid_ecdsa[] = { 0x04, 0x00, 0x7F, 0x00, 0x07, 0x02, 0x02, 0x02, 0x02, 0x03 };
|
||||
const uint8_t oid_ri[] = { 0x04, 0x00, 0x7F, 0x00, 0x07, 0x02, 0x02, 0x05, 0x02, 0x03 };
|
||||
const uint8_t *oid = oid_ecdsa;
|
||||
size_t p_size = mbedtls_mpi_size(&ecdsa->grp.P), a_size = mbedtls_mpi_size(&ecdsa->grp.A);
|
||||
size_t b_size = mbedtls_mpi_size(&ecdsa->grp.B), g_size = 0;
|
||||
size_t o_size = mbedtls_mpi_size(&ecdsa->grp.N), y_size = 0;
|
||||
mbedtls_ecp_point_write_binary(&ecdsa->grp, &ecdsa->grp.G, MBEDTLS_ECP_PF_UNCOMPRESSED, &g_size, G_buf, sizeof(G_buf));
|
||||
mbedtls_ecp_point_write_binary(&ecdsa->grp, &ecdsa->Q, MBEDTLS_ECP_PF_UNCOMPRESSED, &y_size, Y_buf, sizeof(Y_buf));
|
||||
uint16_t c_size = 1;
|
||||
uint16_t ptot_size = asn1_len_tag(0x81, (uint16_t)p_size), atot_size = asn1_len_tag(0x82, a_size ? (uint16_t)a_size : (pointA[ecdsa->grp.id] && ecdsa->grp.id < 6 ? (uint16_t)p_size : 1));
|
||||
uint16_t btot_size = asn1_len_tag(0x83, (uint16_t)b_size), gtot_size = asn1_len_tag(0x84, (uint16_t)g_size);
|
||||
uint16_t otot_size = asn1_len_tag(0x85, (uint16_t)o_size), ytot_size = asn1_len_tag(0x86, (uint16_t)y_size);
|
||||
uint16_t ctot_size = asn1_len_tag(0x87, (uint16_t)c_size);
|
||||
uint16_t oid_len = asn1_len_tag(0x6, sizeof(oid_ecdsa));
|
||||
uint16_t tot_len = 0, tot_data_len = 0;
|
||||
if (mbedtls_ecp_get_type(&ecdsa->grp) == MBEDTLS_ECP_TYPE_MONTGOMERY
|
||||
#ifdef MBEDTLS_EDDSA_C
|
||||
|| mbedtls_ecp_get_type(&ecdsa->grp) == MBEDTLS_ECP_TYPE_EDWARDS
|
||||
#endif
|
||||
) {
|
||||
tot_data_len = oid_len + ptot_size + otot_size + gtot_size + ytot_size;
|
||||
oid = oid_ri;
|
||||
}
|
||||
else {
|
||||
tot_data_len = oid_len + ptot_size + atot_size + btot_size + gtot_size + otot_size + ytot_size +
|
||||
ctot_size;
|
||||
}
|
||||
tot_len = asn1_len_tag(0x7f49, tot_data_len);
|
||||
if (buf == NULL || buf_len == 0) {
|
||||
return tot_len;
|
||||
}
|
||||
if (buf_len < tot_len) {
|
||||
return 0;
|
||||
}
|
||||
uint8_t *p = buf;
|
||||
memcpy(p, "\x7F\x49", 2); p += 2;
|
||||
p += format_tlv_len(tot_data_len, p);
|
||||
//oid
|
||||
*p++ = 0x6; p += format_tlv_len(sizeof(oid_ecdsa), p); memcpy(p, oid, sizeof(oid_ecdsa));
|
||||
p += sizeof(oid_ecdsa);
|
||||
if (mbedtls_ecp_get_type(&ecdsa->grp) == MBEDTLS_ECP_TYPE_MONTGOMERY
|
||||
#ifdef MBEDTLS_EDDSA_C
|
||||
|| mbedtls_ecp_get_type(&ecdsa->grp) == MBEDTLS_ECP_TYPE_EDWARDS
|
||||
#endif
|
||||
) {
|
||||
//p
|
||||
*p++ = 0x81; p += format_tlv_len((uint16_t)p_size, p); mbedtls_mpi_write_binary(&ecdsa->grp.P, p, p_size);
|
||||
p += p_size;
|
||||
//order
|
||||
*p++ = 0x82; p += format_tlv_len((uint16_t)o_size, p); mbedtls_mpi_write_binary(&ecdsa->grp.N, p, o_size);
|
||||
p += o_size;
|
||||
//G
|
||||
*p++ = 0x83; p += format_tlv_len((uint16_t)g_size, p); memcpy(p, G_buf, g_size); p += g_size;
|
||||
//Y
|
||||
*p++ = 0x84; p += format_tlv_len((uint16_t)y_size, p); memcpy(p, Y_buf, y_size); p += y_size;
|
||||
}
|
||||
else {
|
||||
//p
|
||||
*p++ = 0x81; p += format_tlv_len((uint16_t)p_size, p); mbedtls_mpi_write_binary(&ecdsa->grp.P, p, p_size);
|
||||
p += p_size;
|
||||
//A
|
||||
if (a_size) {
|
||||
*p++ = 0x82; p += format_tlv_len((uint16_t)a_size, p); mbedtls_mpi_write_binary(&ecdsa->grp.A, p, a_size); p += a_size;
|
||||
}
|
||||
else { //mbedtls does not set point A for some curves
|
||||
if (pointA[ecdsa->grp.id] && ecdsa->grp.id < 6) {
|
||||
*p++ = 0x82; p += format_tlv_len((uint16_t)p_size, p); memcpy(p, pointA[ecdsa->grp.id], p_size);
|
||||
p += p_size;
|
||||
}
|
||||
else {
|
||||
*p++ = 0x82; p += format_tlv_len(1, p);
|
||||
*p++ = 0x0;
|
||||
}
|
||||
}
|
||||
//B
|
||||
*p++ = 0x83; p += format_tlv_len((uint16_t)b_size, p); mbedtls_mpi_write_binary(&ecdsa->grp.B, p, b_size);
|
||||
p += b_size;
|
||||
//G
|
||||
*p++ = 0x84; p += format_tlv_len((uint16_t)g_size, p); memcpy(p, G_buf, g_size); p += g_size;
|
||||
//order
|
||||
*p++ = 0x85; p += format_tlv_len((uint16_t)o_size, p); mbedtls_mpi_write_binary(&ecdsa->grp.N, p, o_size);
|
||||
p += o_size;
|
||||
//Y
|
||||
*p++ = 0x86; p += format_tlv_len((uint16_t)y_size, p); memcpy(p, Y_buf, y_size); p += y_size;
|
||||
//cofactor
|
||||
*p++ = 0x87; p += format_tlv_len(c_size, p);
|
||||
*p++ = 1;
|
||||
}
|
||||
return tot_len;
|
||||
}
|
||||
|
||||
uint16_t asn1_cvc_cert_body(void *rsa_ecdsa,
|
||||
uint8_t key_type,
|
||||
uint8_t *buf,
|
||||
uint16_t buf_len,
|
||||
const uint8_t *ext,
|
||||
uint16_t ext_len,
|
||||
bool full) {
|
||||
uint16_t pubkey_size = 0;
|
||||
if (key_type & PICO_KEYS_KEY_RSA) {
|
||||
pubkey_size = asn1_cvc_public_key_rsa(rsa_ecdsa, NULL, 0);
|
||||
}
|
||||
else if (key_type & PICO_KEYS_KEY_EC) {
|
||||
pubkey_size = asn1_cvc_public_key_ecdsa(rsa_ecdsa, NULL, 0);
|
||||
}
|
||||
uint16_t cpi_size = 4, ext_size = 0, role_size = 0, valid_size = 0;
|
||||
if (ext && ext_len > 0) {
|
||||
ext_size = asn1_len_tag(0x65, ext_len);
|
||||
}
|
||||
const uint8_t *role = (const uint8_t *)"\x06\x09\x04\x00\x7F\x00\x07\x03\x01\x02\x02\x53\x01\x00";
|
||||
uint16_t rolelen = 14;
|
||||
if (full) {
|
||||
role_size = asn1_len_tag(0x7f4c, rolelen);
|
||||
valid_size = asn1_len_tag(0x5f24, 6) + asn1_len_tag(0x5f25, 6);
|
||||
}
|
||||
|
||||
asn1_ctx_t ctxi, car = {0}, chr = {0};
|
||||
asn1_ctx_init(apdu.data, (uint16_t)apdu.nc, &ctxi);
|
||||
if (asn1_find_tag(&ctxi, 0x42, &car) == false || asn1_len(&car) == 0) {
|
||||
car.data = (uint8_t *) dev_name;
|
||||
car.len = dev_name_len;
|
||||
if (dev_name == NULL) {
|
||||
car.data = (uint8_t *)"ESPICOHSMTR00001";
|
||||
car.len = (uint16_t)strlen((const char *)car.data);
|
||||
}
|
||||
}
|
||||
if (asn1_find_tag(&ctxi, 0x5f20, &chr) == false || asn1_len(&chr) == 0) {
|
||||
chr.data = (uint8_t *) dev_name;
|
||||
chr.len = dev_name_len;
|
||||
if (chr.data == NULL) {
|
||||
chr.data = car.data;
|
||||
chr.len = car.len;
|
||||
}
|
||||
}
|
||||
uint16_t car_size = asn1_len_tag(0x42, car.len), chr_size = asn1_len_tag(0x5f20, chr.len);
|
||||
|
||||
uint16_t tot_len = asn1_len_tag(0x7f4e, cpi_size + car_size + pubkey_size + chr_size + ext_size + role_size + valid_size);
|
||||
|
||||
if (buf_len == 0 || buf == NULL) {
|
||||
return tot_len;
|
||||
}
|
||||
if (buf_len < tot_len) {
|
||||
return 0;
|
||||
}
|
||||
uint8_t *p = buf;
|
||||
memcpy(p, "\x7F\x4E", 2); p += 2;
|
||||
p += format_tlv_len(cpi_size + car_size + pubkey_size + chr_size + role_size + valid_size + ext_size, p);
|
||||
//cpi
|
||||
*p++ = 0x5f; *p++ = 0x29; *p++ = 1; *p++ = 0;
|
||||
//car
|
||||
*p++ = 0x42; p += format_tlv_len(car.len, p); memcpy(p, car.data, car.len); p += car.len;
|
||||
//pubkey
|
||||
if (key_type & PICO_KEYS_KEY_RSA) {
|
||||
p += asn1_cvc_public_key_rsa(rsa_ecdsa, p, pubkey_size);
|
||||
}
|
||||
else if (key_type & PICO_KEYS_KEY_EC) {
|
||||
p += asn1_cvc_public_key_ecdsa(rsa_ecdsa, p, pubkey_size);
|
||||
}
|
||||
//chr
|
||||
*p++ = 0x5f; *p++ = 0x20; p += format_tlv_len(chr.len, p); memcpy(p, chr.data, chr.len); p += chr.len;
|
||||
if (full) {
|
||||
*p++ = 0x7f;
|
||||
*p++ = 0x4c;
|
||||
p += format_tlv_len(rolelen, p);
|
||||
memcpy(p, role, rolelen);
|
||||
p += rolelen;
|
||||
|
||||
*p++ = 0x5f;
|
||||
*p++ = 0x25;
|
||||
p += format_tlv_len(6, p);
|
||||
memcpy(p, "\x02\x03\x00\x03\x02\x01", 6);
|
||||
p += 6;
|
||||
|
||||
*p++ = 0x5f;
|
||||
*p++ = 0x24;
|
||||
p += format_tlv_len(6, p);
|
||||
memcpy(p, "\x07\x00\x01\x02\x03\x01", 6);
|
||||
p += 6;
|
||||
}
|
||||
if (ext && ext_len > 0) {
|
||||
*p++ = 0x65;
|
||||
p += format_tlv_len(ext_len, p);
|
||||
memcpy(p, ext, ext_len);
|
||||
p += ext_len;
|
||||
}
|
||||
return tot_len;
|
||||
}
|
||||
|
||||
uint16_t asn1_cvc_cert(void *rsa_ecdsa,
|
||||
uint8_t key_type,
|
||||
uint8_t *buf,
|
||||
uint16_t buf_len,
|
||||
const uint8_t *ext,
|
||||
uint16_t ext_len,
|
||||
bool full) {
|
||||
uint16_t key_size = 0;
|
||||
if (key_type & PICO_KEYS_KEY_RSA) {
|
||||
key_size = (uint16_t)mbedtls_mpi_size(&((mbedtls_rsa_context *) rsa_ecdsa)->N);
|
||||
}
|
||||
else if (key_type & PICO_KEYS_KEY_EC) {
|
||||
key_size = 2 * (int)((mbedtls_ecp_curve_info_from_grp_id(((mbedtls_ecdsa_context *) rsa_ecdsa)->grp.id)->bit_size + 7) / 8);
|
||||
}
|
||||
uint16_t body_size = asn1_cvc_cert_body(rsa_ecdsa, key_type, NULL, 0, ext, ext_len, full), sig_size = asn1_len_tag(0x5f37, key_size);
|
||||
uint16_t tot_len = asn1_len_tag(0x7f21, body_size + sig_size);
|
||||
if (buf_len == 0 || buf == NULL) {
|
||||
return tot_len;
|
||||
}
|
||||
if (buf_len < tot_len) {
|
||||
return 0;
|
||||
}
|
||||
uint8_t *p = buf, *body = NULL;
|
||||
memcpy(p, "\x7F\x21", 2); p += 2;
|
||||
p += format_tlv_len(body_size + sig_size, p);
|
||||
body = p;
|
||||
p += asn1_cvc_cert_body(rsa_ecdsa, key_type, p, body_size, ext, ext_len, full);
|
||||
uint8_t hsh[32];
|
||||
hash256(body, body_size, hsh);
|
||||
memcpy(p, "\x5F\x37", 2); p += 2;
|
||||
p += format_tlv_len(key_size, p);
|
||||
if (key_type & PICO_KEYS_KEY_RSA) {
|
||||
if (mbedtls_rsa_rsassa_pkcs1_v15_sign(rsa_ecdsa, random_gen, NULL, MBEDTLS_MD_SHA256, 32, hsh, p) != 0) {
|
||||
memset(p, 0, key_size);
|
||||
}
|
||||
p += key_size;
|
||||
}
|
||||
else if (key_type & PICO_KEYS_KEY_EC) {
|
||||
mbedtls_mpi r, s;
|
||||
int ret = 0;
|
||||
mbedtls_ecp_keypair *ecdsa = (mbedtls_ecp_keypair *) rsa_ecdsa;
|
||||
mbedtls_mpi_init(&r);
|
||||
mbedtls_mpi_init(&s);
|
||||
#ifdef MBEDTLS_EDDSA_C
|
||||
if (ecdsa->grp.id == MBEDTLS_ECP_DP_ED25519 || ecdsa->grp.id == MBEDTLS_ECP_DP_ED448) {
|
||||
ret = mbedtls_eddsa_sign(&ecdsa->grp, &r, &s, &ecdsa->d, body, body_size, MBEDTLS_EDDSA_PURE, NULL, 0, random_gen, NULL);
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
ret = mbedtls_ecdsa_sign(&ecdsa->grp, &r, &s, &ecdsa->d, hsh, sizeof(hsh), random_gen, NULL);
|
||||
}
|
||||
if (ret == 0) {
|
||||
mbedtls_mpi_write_binary(&r, p, key_size / 2); p += key_size / 2;
|
||||
mbedtls_mpi_write_binary(&s, p, key_size / 2); p += key_size / 2;
|
||||
}
|
||||
else {
|
||||
memset(p, 0, key_size);
|
||||
p += key_size;
|
||||
}
|
||||
mbedtls_mpi_free(&r);
|
||||
mbedtls_mpi_free(&s);
|
||||
}
|
||||
return (uint16_t)(p - buf);
|
||||
}
|
||||
|
||||
uint16_t asn1_cvc_aut(void *rsa_ecdsa,
|
||||
uint8_t key_type,
|
||||
uint8_t *buf,
|
||||
uint16_t buf_len,
|
||||
const uint8_t *ext,
|
||||
uint16_t ext_len) {
|
||||
uint16_t cvcert_size = asn1_cvc_cert(rsa_ecdsa, key_type, NULL, 0, ext, ext_len, false);
|
||||
uint16_t outcar_len = dev_name_len;
|
||||
const uint8_t *outcar = dev_name;
|
||||
uint16_t outcar_size = asn1_len_tag(0x42, outcar_len);
|
||||
file_t *fkey = search_file(EF_KEY_DEV);
|
||||
if (!fkey) {
|
||||
return 0;
|
||||
}
|
||||
mbedtls_ecp_keypair ectx;
|
||||
mbedtls_ecp_keypair_init(&ectx);
|
||||
if (load_private_key_ec(&ectx, fkey) != PICOKEY_OK) {
|
||||
mbedtls_ecp_keypair_free(&ectx);
|
||||
return 0;
|
||||
}
|
||||
int ret = 0;
|
||||
uint16_t key_size = 2 * (uint16_t)mbedtls_mpi_size(&ectx.d);
|
||||
uint16_t outsig_size = asn1_len_tag(0x5f37, key_size), tot_len = asn1_len_tag(0x67, cvcert_size + outcar_size + outsig_size);
|
||||
if (buf_len == 0 || buf == NULL) {
|
||||
return tot_len;
|
||||
}
|
||||
if (buf_len < tot_len) {
|
||||
return 0;
|
||||
}
|
||||
uint8_t *p = buf;
|
||||
*p++ = 0x67;
|
||||
p += format_tlv_len(cvcert_size + outcar_size + outsig_size, p);
|
||||
uint8_t *body = p;
|
||||
//cvcert
|
||||
p += asn1_cvc_cert(rsa_ecdsa, key_type, p, cvcert_size, ext, ext_len, false);
|
||||
//outcar
|
||||
*p++ = 0x42; p += format_tlv_len(outcar_len, p); memcpy(p, outcar, outcar_len); p += outcar_len;
|
||||
memcpy(p, "\x5f\x37", 2); p += 2;
|
||||
p += format_tlv_len(key_size, p);
|
||||
mbedtls_mpi r, s;
|
||||
mbedtls_mpi_init(&r);
|
||||
mbedtls_mpi_init(&s);
|
||||
#ifdef MBEDTLS_EDDSA_C
|
||||
if (ectx.grp.id == MBEDTLS_ECP_DP_ED25519 || ectx.grp.id == MBEDTLS_ECP_DP_ED448) {
|
||||
ret = mbedtls_eddsa_sign(&ectx.grp, &r, &s, &ectx.d, body, cvcert_size + outcar_size, MBEDTLS_EDDSA_PURE, NULL, 0, random_gen, NULL);
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
uint8_t hsh[32];
|
||||
hash256(body, cvcert_size + outcar_size, hsh);
|
||||
ret = mbedtls_ecdsa_sign(&ectx.grp, &r, &s, &ectx.d, hsh, sizeof(hsh), random_gen, NULL);
|
||||
}
|
||||
mbedtls_ecp_keypair_free(&ectx);
|
||||
if (ret != 0) {
|
||||
mbedtls_mpi_free(&r);
|
||||
mbedtls_mpi_free(&s);
|
||||
return 0;
|
||||
}
|
||||
mbedtls_mpi_write_binary(&r, p, key_size / 2); p += key_size / 2;
|
||||
mbedtls_mpi_write_binary(&s, p, key_size / 2); p += key_size / 2;
|
||||
mbedtls_mpi_free(&r);
|
||||
mbedtls_mpi_free(&s);
|
||||
return (uint16_t)(p - buf);
|
||||
}
|
||||
|
||||
uint16_t asn1_build_cert_description(const uint8_t *label,
|
||||
uint16_t label_len,
|
||||
const uint8_t *puk,
|
||||
uint16_t puk_len,
|
||||
uint16_t fid,
|
||||
uint8_t *buf,
|
||||
uint16_t buf_len) {
|
||||
uint16_t opt_len = 2;
|
||||
uint16_t seq1_size =
|
||||
asn1_len_tag(0x30, asn1_len_tag(0xC, label_len) + asn1_len_tag(0x3, opt_len));
|
||||
uint16_t seq2_size = asn1_len_tag(0x30, asn1_len_tag(0x4, 20)); /* SHA1 is 20 bytes length */
|
||||
uint16_t seq3_size =
|
||||
asn1_len_tag(0xA1,
|
||||
asn1_len_tag(0x30, asn1_len_tag(0x30, asn1_len_tag(0x4, sizeof(uint16_t)))));
|
||||
uint16_t tot_len = asn1_len_tag(0x30, seq1_size + seq2_size + seq3_size);
|
||||
if (buf_len == 0 || buf == NULL) {
|
||||
return tot_len;
|
||||
}
|
||||
if (buf_len < tot_len) {
|
||||
return 0;
|
||||
}
|
||||
uint8_t *p = buf;
|
||||
*p++ = 0x30;
|
||||
p += format_tlv_len(seq1_size + seq2_size + seq3_size, p);
|
||||
//Seq 1
|
||||
*p++ = 0x30;
|
||||
p += format_tlv_len(asn1_len_tag(0xC, label_len) + asn1_len_tag(0x3, opt_len), p);
|
||||
*p++ = 0xC;
|
||||
p += format_tlv_len(label_len, p);
|
||||
memcpy(p, label, label_len); p += label_len;
|
||||
*p++ = 0x3;
|
||||
p += format_tlv_len(opt_len, p);
|
||||
memcpy(p, "\x06\x40", 2); p += 2;
|
||||
|
||||
//Seq 2
|
||||
*p++ = 0x30;
|
||||
p += format_tlv_len(asn1_len_tag(0x4, 20), p);
|
||||
*p++ = 0x4;
|
||||
p += format_tlv_len(20, p);
|
||||
mbedtls_md(mbedtls_md_info_from_type(MBEDTLS_MD_SHA1), puk, puk_len, p); p += 20;
|
||||
|
||||
//Seq 3
|
||||
*p++ = 0xA1;
|
||||
p += format_tlv_len(asn1_len_tag(0x30, asn1_len_tag(0x30, asn1_len_tag(0x4, sizeof(uint16_t)))),
|
||||
p);
|
||||
*p++ = 0x30;
|
||||
p += format_tlv_len(asn1_len_tag(0x30, asn1_len_tag(0x4, sizeof(uint16_t))), p);
|
||||
*p++ = 0x30;
|
||||
p += format_tlv_len(asn1_len_tag(0x4, sizeof(uint16_t)), p);
|
||||
*p++ = 0x4;
|
||||
p += format_tlv_len(sizeof(uint16_t), p);
|
||||
put_uint16_t_be(fid, p); p += sizeof(uint16_t);
|
||||
return (uint16_t)(p - buf);
|
||||
}
|
||||
|
||||
uint16_t asn1_build_prkd_generic(const uint8_t *label,
|
||||
uint16_t label_len,
|
||||
const uint8_t *keyid,
|
||||
uint16_t keyid_len,
|
||||
uint16_t keysize,
|
||||
int key_type,
|
||||
uint8_t *buf,
|
||||
uint16_t buf_len) {
|
||||
uint16_t seq_len = 0;
|
||||
const uint8_t *seq = NULL;
|
||||
uint8_t first_tag = 0x0;
|
||||
if (key_type & PICO_KEYS_KEY_EC) {
|
||||
seq = (const uint8_t *)"\x07\x20\x80";
|
||||
seq_len = 3;
|
||||
first_tag = 0xA0;
|
||||
}
|
||||
else if (key_type & PICO_KEYS_KEY_RSA) {
|
||||
seq = (const uint8_t *)"\x02\x74";
|
||||
seq_len = 2;
|
||||
first_tag = 0x30;
|
||||
}
|
||||
else if (key_type & PICO_KEYS_KEY_AES) {
|
||||
seq = (const uint8_t *)"\x07\xC0\x10";
|
||||
seq_len = 3;
|
||||
first_tag = 0xA8;
|
||||
}
|
||||
uint16_t seq1_size = asn1_len_tag(0x30, asn1_len_tag(0xC, label_len));
|
||||
uint16_t seq2_size =
|
||||
asn1_len_tag(0x30, asn1_len_tag(0x4, keyid_len) + asn1_len_tag(0x3, seq_len));
|
||||
uint16_t seq3_size = 0, seq4_size = 0;
|
||||
if (key_type & PICO_KEYS_KEY_EC || key_type & PICO_KEYS_KEY_RSA) {
|
||||
seq4_size = asn1_len_tag(0xA1, asn1_len_tag(0x30, asn1_len_tag(0x30, asn1_len_tag(0x4, 0)) + asn1_len_tag(0x2, 2)));
|
||||
}
|
||||
else if (key_type & PICO_KEYS_KEY_AES) {
|
||||
seq3_size = asn1_len_tag(0xA0, asn1_len_tag(0x30, asn1_len_tag(0x2, 2)));
|
||||
seq4_size = asn1_len_tag(0xA1, asn1_len_tag(0x30, asn1_len_tag(0x30, asn1_len_tag(0x4, 0))));
|
||||
}
|
||||
uint16_t tot_len = asn1_len_tag(first_tag, seq1_size + seq2_size + seq4_size);
|
||||
if (buf_len == 0 || buf == NULL) {
|
||||
return tot_len;
|
||||
}
|
||||
if (buf_len < tot_len) {
|
||||
return 0;
|
||||
}
|
||||
uint8_t *p = buf;
|
||||
*p++ = first_tag;
|
||||
p += format_tlv_len(seq1_size + seq2_size + seq3_size + seq4_size, p);
|
||||
//Seq 1
|
||||
*p++ = 0x30;
|
||||
p += format_tlv_len(asn1_len_tag(0xC, label_len), p);
|
||||
*p++ = 0xC;
|
||||
p += format_tlv_len(label_len, p);
|
||||
memcpy(p, label, label_len); p += label_len;
|
||||
|
||||
//Seq 2
|
||||
*p++ = 0x30;
|
||||
p += format_tlv_len(asn1_len_tag(0x4, keyid_len) + asn1_len_tag(0x3, seq_len), p);
|
||||
*p++ = 0x4;
|
||||
p += format_tlv_len(keyid_len, p);
|
||||
memcpy(p, keyid, keyid_len); p += keyid_len;
|
||||
*p++ = 0x3;
|
||||
p += format_tlv_len(seq_len, p);
|
||||
memcpy(p, seq, seq_len); p += seq_len;
|
||||
|
||||
//Seq 3
|
||||
if (key_type & PICO_KEYS_KEY_AES) {
|
||||
*p++ = 0xA0;
|
||||
p += format_tlv_len(asn1_len_tag(0x30, asn1_len_tag(0x2, 2)), p);
|
||||
*p++ = 0x30;
|
||||
p += format_tlv_len(asn1_len_tag(0x2, 2), p);
|
||||
*p++ = 0x2;
|
||||
p += format_tlv_len(2, p);
|
||||
p += put_uint16_t_be(keysize, p);
|
||||
}
|
||||
|
||||
//Seq 4
|
||||
*p++ = 0xA1;
|
||||
uint16_t inseq4_len = asn1_len_tag(0x30, asn1_len_tag(0x4, 0));
|
||||
if (key_type & PICO_KEYS_KEY_EC || key_type & PICO_KEYS_KEY_RSA) {
|
||||
inseq4_len += asn1_len_tag(0x2, 2);
|
||||
}
|
||||
p += format_tlv_len(asn1_len_tag(0x30, inseq4_len), p);
|
||||
*p++ = 0x30;
|
||||
p += format_tlv_len(inseq4_len, p);
|
||||
*p++ = 0x30;
|
||||
p += format_tlv_len(asn1_len_tag(0x4, 0), p);
|
||||
*p++ = 0x4;
|
||||
p += format_tlv_len(0, p);
|
||||
if (key_type & PICO_KEYS_KEY_EC || key_type & PICO_KEYS_KEY_RSA) {
|
||||
*p++ = 0x2;
|
||||
p += format_tlv_len(2, p);
|
||||
p += put_uint16_t_be(keysize, p);
|
||||
}
|
||||
return (uint16_t)(p - buf);
|
||||
}
|
||||
|
||||
uint16_t asn1_build_prkd_ecc(const uint8_t *label,
|
||||
uint16_t label_len,
|
||||
const uint8_t *keyid,
|
||||
uint16_t keyid_len,
|
||||
uint16_t keysize,
|
||||
uint8_t *buf,
|
||||
uint16_t buf_len) {
|
||||
return asn1_build_prkd_generic(label,
|
||||
label_len,
|
||||
keyid,
|
||||
keyid_len,
|
||||
keysize,
|
||||
PICO_KEYS_KEY_EC,
|
||||
buf,
|
||||
buf_len);
|
||||
}
|
||||
|
||||
uint16_t asn1_build_prkd_rsa(const uint8_t *label,
|
||||
uint16_t label_len,
|
||||
const uint8_t *keyid,
|
||||
uint16_t keyid_len,
|
||||
uint16_t keysize,
|
||||
uint8_t *buf,
|
||||
uint16_t buf_len) {
|
||||
return asn1_build_prkd_generic(label,
|
||||
label_len,
|
||||
keyid,
|
||||
keyid_len,
|
||||
keysize,
|
||||
PICO_KEYS_KEY_RSA,
|
||||
buf,
|
||||
buf_len);
|
||||
}
|
||||
|
||||
uint16_t asn1_build_prkd_aes(const uint8_t *label,
|
||||
uint16_t label_len,
|
||||
const uint8_t *keyid,
|
||||
uint16_t keyid_len,
|
||||
uint16_t keysize,
|
||||
uint8_t *buf,
|
||||
uint16_t buf_len) {
|
||||
return asn1_build_prkd_generic(label,
|
||||
label_len,
|
||||
keyid,
|
||||
keyid_len,
|
||||
keysize,
|
||||
PICO_KEYS_KEY_AES,
|
||||
buf,
|
||||
buf_len);
|
||||
}
|
||||
|
||||
const uint8_t *cvc_get_field(const uint8_t *data, uint16_t len, uint16_t *olen, uint16_t tag) {
|
||||
asn1_ctx_t ctxi, ctxo = { 0 };
|
||||
asn1_ctx_init((uint8_t *)data, len, &ctxi);
|
||||
if (asn1_len(&ctxi) == 0) {
|
||||
return NULL;
|
||||
}
|
||||
if (asn1_find_tag(&ctxi, tag, &ctxo) == false) {
|
||||
return NULL;
|
||||
}
|
||||
*olen = ctxo.len;
|
||||
return ctxo.data;
|
||||
}
|
||||
|
||||
const uint8_t *cvc_get_body(const uint8_t *data, uint16_t len, uint16_t *olen) {
|
||||
const uint8_t *bkdata = data;
|
||||
if ((data = cvc_get_field(data, len, olen, 0x67)) == NULL) { /* Check for CSR */
|
||||
data = bkdata;
|
||||
}
|
||||
if ((data = cvc_get_field(data, len, olen, 0x7F21)) != NULL) {
|
||||
return cvc_get_field(data, len, olen, 0x7F4E);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const uint8_t *cvc_get_sig(const uint8_t *data, uint16_t len, uint16_t *olen) {
|
||||
const uint8_t *bkdata = data;
|
||||
if ((data = cvc_get_field(data, len, olen, 0x67)) == NULL) { /* Check for CSR */
|
||||
data = bkdata;
|
||||
}
|
||||
if ((data = cvc_get_field(data, len, olen, 0x7F21)) != NULL) {
|
||||
return cvc_get_field(data, len, olen, 0x5F37);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const uint8_t *cvc_get_car(const uint8_t *data, uint16_t len, uint16_t *olen) {
|
||||
if ((data = cvc_get_body(data, len, olen)) != NULL) {
|
||||
return cvc_get_field(data, len, olen, 0x42);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const uint8_t *cvc_get_chr(const uint8_t *data, uint16_t len, uint16_t *olen) {
|
||||
if ((data = cvc_get_body(data, len, olen)) != NULL) {
|
||||
return cvc_get_field(data, len, olen, 0x5F20);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const uint8_t *cvc_get_pub(const uint8_t *data, uint16_t len, uint16_t *olen) {
|
||||
if ((data = cvc_get_body(data, len, olen)) != NULL) {
|
||||
return cvc_get_field(data, len, olen, 0x7F49);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const uint8_t *cvc_get_ext(const uint8_t *data, uint16_t len, uint16_t *olen) {
|
||||
if ((data = cvc_get_body(data, len, olen)) != NULL) {
|
||||
return cvc_get_field(data, len, olen, 0x65);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
extern PUK puk_store[MAX_PUK_STORE_ENTRIES];
|
||||
extern int puk_store_entries;
|
||||
|
||||
int puk_store_index(const uint8_t *chr, uint16_t chr_len) {
|
||||
for (int i = 0; i < puk_store_entries; i++) {
|
||||
if (memcmp(puk_store[i].chr, chr, chr_len) == 0) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
mbedtls_ecp_group_id cvc_inherite_ec_group(const uint8_t *ca, uint16_t ca_len) {
|
||||
uint16_t chr_len = 0, car_len = 0;
|
||||
const uint8_t *chr = NULL, *car = NULL;
|
||||
int eq = -1;
|
||||
do {
|
||||
chr = cvc_get_chr(ca, ca_len, &chr_len);
|
||||
car = cvc_get_car(ca, ca_len, &car_len);
|
||||
eq = memcmp(car, chr, MAX(car_len, chr_len));
|
||||
if (car && eq != 0) {
|
||||
int idx = puk_store_index(car, car_len);
|
||||
if (idx != -1) {
|
||||
ca = puk_store[idx].cvcert;
|
||||
ca_len = puk_store[idx].cvcert_len;
|
||||
}
|
||||
else {
|
||||
ca = NULL;
|
||||
}
|
||||
}
|
||||
} while (car && chr && eq != 0);
|
||||
uint16_t ca_puk_len = 0;
|
||||
const uint8_t *ca_puk = cvc_get_pub(ca, ca_len, &ca_puk_len);
|
||||
if (!ca_puk) {
|
||||
return MBEDTLS_ECP_DP_NONE;
|
||||
}
|
||||
uint16_t t81_len = 0;
|
||||
const uint8_t *t81 = cvc_get_field(ca_puk, ca_puk_len, &t81_len, 0x81);
|
||||
if (!t81) {
|
||||
return MBEDTLS_ECP_DP_NONE;
|
||||
}
|
||||
|
||||
return ec_get_curve_from_prime(t81, t81_len);
|
||||
}
|
||||
|
||||
int puk_verify(const uint8_t *sig,
|
||||
uint16_t sig_len,
|
||||
const uint8_t *hash,
|
||||
uint16_t hash_len,
|
||||
const uint8_t *ca,
|
||||
uint16_t ca_len) {
|
||||
uint16_t puk_len = 0;
|
||||
const uint8_t *puk = cvc_get_pub(ca, ca_len, &puk_len);
|
||||
if (!puk) {
|
||||
return PICOKEY_WRONG_DATA;
|
||||
}
|
||||
uint16_t oid_len = 0;
|
||||
const uint8_t *oid = cvc_get_field(puk, puk_len, &oid_len, 0x6);
|
||||
if (!oid) {
|
||||
return PICOKEY_WRONG_DATA;
|
||||
}
|
||||
if (memcmp(oid, OID_ID_TA_RSA, 9) == 0) { //RSA
|
||||
uint16_t t81_len = 0, t82_len = 0;
|
||||
const uint8_t *t81 = cvc_get_field(puk, puk_len, &t81_len, 0x81), *t82 = cvc_get_field(puk,
|
||||
puk_len,
|
||||
&t81_len,
|
||||
0x82);
|
||||
if (!t81 || !t82) {
|
||||
return PICOKEY_WRONG_DATA;
|
||||
}
|
||||
mbedtls_rsa_context rsa;
|
||||
mbedtls_rsa_init(&rsa);
|
||||
mbedtls_md_type_t md = MBEDTLS_MD_NONE;
|
||||
if (memcmp(oid, OID_ID_TA_RSA_V1_5_SHA_1, oid_len) == 0) {
|
||||
md = MBEDTLS_MD_SHA1;
|
||||
}
|
||||
else if (memcmp(oid, OID_ID_TA_RSA_V1_5_SHA_256, oid_len) == 0) {
|
||||
md = MBEDTLS_MD_SHA256;
|
||||
}
|
||||
else if (memcmp(oid, OID_ID_TA_RSA_V1_5_SHA_512, oid_len) == 0) {
|
||||
md = MBEDTLS_MD_SHA512;
|
||||
}
|
||||
else if (memcmp(oid, OID_ID_TA_RSA_PSS_SHA_1, oid_len) == 0) {
|
||||
md = MBEDTLS_MD_SHA1;
|
||||
mbedtls_rsa_set_padding(&rsa, MBEDTLS_RSA_PKCS_V21, md);
|
||||
}
|
||||
else if (memcmp(oid, OID_ID_TA_RSA_PSS_SHA_256, oid_len) == 0) {
|
||||
md = MBEDTLS_MD_SHA256;
|
||||
mbedtls_rsa_set_padding(&rsa, MBEDTLS_RSA_PKCS_V21, md);
|
||||
}
|
||||
else if (memcmp(oid, OID_ID_TA_RSA_PSS_SHA_512, oid_len) == 0) {
|
||||
md = MBEDTLS_MD_SHA512;
|
||||
mbedtls_rsa_set_padding(&rsa, MBEDTLS_RSA_PKCS_V21, md);
|
||||
}
|
||||
if (md == MBEDTLS_MD_NONE) {
|
||||
mbedtls_rsa_free(&rsa);
|
||||
return PICOKEY_WRONG_DATA;
|
||||
}
|
||||
int r = mbedtls_mpi_read_binary(&rsa.N, t81, t81_len);
|
||||
if (r != 0) {
|
||||
mbedtls_rsa_free(&rsa);
|
||||
return PICOKEY_EXEC_ERROR;
|
||||
}
|
||||
r = mbedtls_mpi_read_binary(&rsa.E, t82, t82_len);
|
||||
if (r != 0) {
|
||||
mbedtls_rsa_free(&rsa);
|
||||
return PICOKEY_EXEC_ERROR;
|
||||
}
|
||||
r = mbedtls_rsa_complete(&rsa);
|
||||
if (r != 0) {
|
||||
mbedtls_rsa_free(&rsa);
|
||||
return PICOKEY_EXEC_ERROR;
|
||||
}
|
||||
r = mbedtls_rsa_check_pubkey(&rsa);
|
||||
if (r != 0) {
|
||||
mbedtls_rsa_free(&rsa);
|
||||
return PICOKEY_EXEC_ERROR;
|
||||
}
|
||||
r = mbedtls_rsa_pkcs1_verify(&rsa, md, (unsigned int)hash_len, hash, sig);
|
||||
mbedtls_rsa_free(&rsa);
|
||||
if (r != 0) {
|
||||
return PICOKEY_WRONG_SIGNATURE;
|
||||
}
|
||||
}
|
||||
else if (memcmp(oid, OID_ID_TA_ECDSA, 9) == 0) { //ECC
|
||||
mbedtls_md_type_t md = MBEDTLS_MD_NONE;
|
||||
if (memcmp(oid, OID_ID_TA_ECDSA_SHA_1, oid_len) == 0) {
|
||||
md = MBEDTLS_MD_SHA1;
|
||||
}
|
||||
else if (memcmp(oid, OID_ID_TA_ECDSA_SHA_224, oid_len) == 0) {
|
||||
md = MBEDTLS_MD_SHA224;
|
||||
}
|
||||
else if (memcmp(oid, OID_ID_TA_ECDSA_SHA_256, oid_len) == 0) {
|
||||
md = MBEDTLS_MD_SHA256;
|
||||
}
|
||||
else if (memcmp(oid, OID_ID_TA_ECDSA_SHA_384, oid_len) == 0) {
|
||||
md = MBEDTLS_MD_SHA384;
|
||||
}
|
||||
else if (memcmp(oid, OID_ID_TA_ECDSA_SHA_512, oid_len) == 0) {
|
||||
md = MBEDTLS_MD_SHA512;
|
||||
}
|
||||
if (md == MBEDTLS_MD_NONE) {
|
||||
return PICOKEY_WRONG_DATA;
|
||||
}
|
||||
|
||||
uint16_t t86_len = 0;
|
||||
const uint8_t *t86 = cvc_get_field(puk, puk_len, &t86_len, 0x86);
|
||||
if (!t86) {
|
||||
return PICOKEY_WRONG_DATA;
|
||||
}
|
||||
mbedtls_ecp_group_id ec_id = cvc_inherite_ec_group(ca, ca_len);
|
||||
if (ec_id == MBEDTLS_ECP_DP_NONE) {
|
||||
return PICOKEY_WRONG_DATA;
|
||||
}
|
||||
mbedtls_ecdsa_context ecdsa;
|
||||
mbedtls_ecdsa_init(&ecdsa);
|
||||
int ret = mbedtls_ecp_group_load(&ecdsa.grp, ec_id);
|
||||
if (ret != 0) {
|
||||
mbedtls_ecdsa_free(&ecdsa);
|
||||
return PICOKEY_WRONG_DATA;
|
||||
}
|
||||
ret = mbedtls_ecp_point_read_binary(&ecdsa.grp, &ecdsa.Q, t86, t86_len);
|
||||
if (ret != 0) {
|
||||
mbedtls_ecdsa_free(&ecdsa);
|
||||
return PICOKEY_EXEC_ERROR;
|
||||
}
|
||||
ret = mbedtls_ecp_check_pubkey(&ecdsa.grp, &ecdsa.Q);
|
||||
if (ret != 0) {
|
||||
mbedtls_ecdsa_free(&ecdsa);
|
||||
return PICOKEY_EXEC_ERROR;
|
||||
}
|
||||
mbedtls_mpi r, s;
|
||||
mbedtls_mpi_init(&r);
|
||||
mbedtls_mpi_init(&s);
|
||||
ret = mbedtls_mpi_read_binary(&r, sig, sig_len / 2);
|
||||
if (ret != 0) {
|
||||
mbedtls_mpi_free(&r);
|
||||
mbedtls_mpi_free(&s);
|
||||
mbedtls_ecdsa_free(&ecdsa);
|
||||
return PICOKEY_EXEC_ERROR;
|
||||
}
|
||||
ret = mbedtls_mpi_read_binary(&s, sig + sig_len / 2, sig_len / 2);
|
||||
if (ret != 0) {
|
||||
mbedtls_mpi_free(&r);
|
||||
mbedtls_mpi_free(&s);
|
||||
mbedtls_ecdsa_free(&ecdsa);
|
||||
return PICOKEY_EXEC_ERROR;
|
||||
}
|
||||
ret = mbedtls_ecdsa_verify(&ecdsa.grp, hash, hash_len, &ecdsa.Q, &r, &s);
|
||||
mbedtls_mpi_free(&r);
|
||||
mbedtls_mpi_free(&s);
|
||||
mbedtls_ecdsa_free(&ecdsa);
|
||||
if (ret != 0) {
|
||||
return PICOKEY_WRONG_SIGNATURE;
|
||||
}
|
||||
}
|
||||
return PICOKEY_OK;
|
||||
}
|
||||
|
||||
int cvc_verify(const uint8_t *cert, uint16_t cert_len, const uint8_t *ca, uint16_t ca_len) {
|
||||
uint16_t puk_len = 0;
|
||||
const uint8_t *puk = cvc_get_pub(ca, ca_len, &puk_len);
|
||||
if (!puk) {
|
||||
return PICOKEY_WRONG_DATA;
|
||||
}
|
||||
uint16_t oid_len = 0, cv_body_len = 0, sig_len = 0;
|
||||
const uint8_t *oid = cvc_get_field(puk, puk_len, &oid_len, 0x6);
|
||||
const uint8_t *cv_body = cvc_get_body(cert, cert_len, &cv_body_len);
|
||||
const uint8_t *sig = cvc_get_sig(cert, cert_len, &sig_len);
|
||||
if (!sig) {
|
||||
return PICOKEY_WRONG_DATA;
|
||||
}
|
||||
if (!cv_body) {
|
||||
return PICOKEY_WRONG_DATA;
|
||||
}
|
||||
if (!oid) {
|
||||
return PICOKEY_WRONG_DATA;
|
||||
}
|
||||
mbedtls_md_type_t md = MBEDTLS_MD_NONE;
|
||||
if (memcmp(oid, OID_ID_TA_RSA, 9) == 0) { //RSA
|
||||
if (memcmp(oid, OID_ID_TA_RSA_V1_5_SHA_1, oid_len) == 0) {
|
||||
md = MBEDTLS_MD_SHA1;
|
||||
}
|
||||
else if (memcmp(oid, OID_ID_TA_RSA_V1_5_SHA_256, oid_len) == 0) {
|
||||
md = MBEDTLS_MD_SHA256;
|
||||
}
|
||||
else if (memcmp(oid, OID_ID_TA_RSA_V1_5_SHA_512, oid_len) == 0) {
|
||||
md = MBEDTLS_MD_SHA512;
|
||||
}
|
||||
else if (memcmp(oid, OID_ID_TA_RSA_PSS_SHA_1, oid_len) == 0) {
|
||||
md = MBEDTLS_MD_SHA1;
|
||||
}
|
||||
else if (memcmp(oid, OID_ID_TA_RSA_PSS_SHA_256, oid_len) == 0) {
|
||||
md = MBEDTLS_MD_SHA256;
|
||||
}
|
||||
else if (memcmp(oid, OID_ID_TA_RSA_PSS_SHA_512, oid_len) == 0) {
|
||||
md = MBEDTLS_MD_SHA512;
|
||||
}
|
||||
}
|
||||
else if (memcmp(oid, OID_ID_TA_ECDSA, 9) == 0) { //ECC
|
||||
if (memcmp(oid, OID_ID_TA_ECDSA_SHA_1, oid_len) == 0) {
|
||||
md = MBEDTLS_MD_SHA1;
|
||||
}
|
||||
else if (memcmp(oid, OID_ID_TA_ECDSA_SHA_224, oid_len) == 0) {
|
||||
md = MBEDTLS_MD_SHA224;
|
||||
}
|
||||
else if (memcmp(oid, OID_ID_TA_ECDSA_SHA_256, oid_len) == 0) {
|
||||
md = MBEDTLS_MD_SHA256;
|
||||
}
|
||||
else if (memcmp(oid, OID_ID_TA_ECDSA_SHA_384, oid_len) == 0) {
|
||||
md = MBEDTLS_MD_SHA384;
|
||||
}
|
||||
else if (memcmp(oid, OID_ID_TA_ECDSA_SHA_512, oid_len) == 0) {
|
||||
md = MBEDTLS_MD_SHA512;
|
||||
}
|
||||
}
|
||||
if (md == MBEDTLS_MD_NONE) {
|
||||
return PICOKEY_WRONG_DATA;
|
||||
}
|
||||
const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md);
|
||||
uint8_t hash[64], hash_len = mbedtls_md_get_size(md_info);
|
||||
uint8_t tlv_body = 2 + format_tlv_len(cv_body_len, NULL);
|
||||
int r = mbedtls_md(md_info, cv_body - tlv_body, cv_body_len + tlv_body, hash);
|
||||
if (r != 0) {
|
||||
return PICOKEY_EXEC_ERROR;
|
||||
}
|
||||
r = puk_verify(sig, sig_len, hash, hash_len, ca, ca_len);
|
||||
if (r != 0) {
|
||||
return PICOKEY_WRONG_SIGNATURE;
|
||||
}
|
||||
return PICOKEY_OK;
|
||||
}
|
||||
105
src/hsm/cvc.h
Normal file
105
src/hsm/cvc.h
Normal file
|
|
@ -0,0 +1,105 @@
|
|||
/*
|
||||
* This file is part of the Pico HSM distribution (https://github.com/polhenarejos/pico-hsm).
|
||||
* Copyright (c) 2022 Pol Henarejos.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef _CVC_H_
|
||||
#define _CVC_H_
|
||||
|
||||
#include <stdlib.h>
|
||||
#if !defined(ENABLE_EMULATION) && !defined(ESP_PLATFORM)
|
||||
#include "pico/stdlib.h"
|
||||
#else
|
||||
#include <stdbool.h>
|
||||
#endif
|
||||
#include "mbedtls/ecp.h"
|
||||
|
||||
typedef struct PUK {
|
||||
const uint8_t *puk;
|
||||
uint16_t puk_len;
|
||||
const uint8_t *car;
|
||||
uint16_t car_len;
|
||||
const uint8_t *chr;
|
||||
uint16_t chr_len;
|
||||
const uint8_t *cvcert;
|
||||
uint16_t cvcert_len;
|
||||
bool copied;
|
||||
} PUK;
|
||||
|
||||
#define MAX_PUK_STORE_ENTRIES 4
|
||||
|
||||
extern uint16_t asn1_cvc_cert(void *rsa_ecdsa,
|
||||
uint8_t key_type,
|
||||
uint8_t *buf,
|
||||
uint16_t buf_len,
|
||||
const uint8_t *ext,
|
||||
uint16_t ext_len,
|
||||
bool full);
|
||||
extern uint16_t asn1_cvc_aut(void *rsa_ecdsa,
|
||||
uint8_t key_type,
|
||||
uint8_t *buf,
|
||||
uint16_t buf_len,
|
||||
const uint8_t *ext,
|
||||
uint16_t ext_len);
|
||||
extern uint16_t asn1_build_cert_description(const uint8_t *label,
|
||||
uint16_t label_len,
|
||||
const uint8_t *puk,
|
||||
uint16_t puk_len,
|
||||
uint16_t fid,
|
||||
uint8_t *buf,
|
||||
uint16_t buf_len);
|
||||
extern const uint8_t *cvc_get_field(const uint8_t *data, uint16_t len, uint16_t *olen, uint16_t tag);
|
||||
extern const uint8_t *cvc_get_car(const uint8_t *data, uint16_t len, uint16_t *olen);
|
||||
extern const uint8_t *cvc_get_chr(const uint8_t *data, uint16_t len, uint16_t *olen);
|
||||
extern const uint8_t *cvc_get_pub(const uint8_t *data, uint16_t len, uint16_t *olen);
|
||||
extern const uint8_t *cvc_get_ext(const uint8_t *data, uint16_t len, uint16_t *olen);
|
||||
extern int cvc_verify(const uint8_t *cert, uint16_t cert_len, const uint8_t *ca, uint16_t ca_len);
|
||||
extern mbedtls_ecp_group_id cvc_inherite_ec_group(const uint8_t *ca, uint16_t ca_len);
|
||||
extern int puk_verify(const uint8_t *sig,
|
||||
uint16_t sig_len,
|
||||
const uint8_t *hash,
|
||||
uint16_t hash_len,
|
||||
const uint8_t *ca,
|
||||
uint16_t ca_len);
|
||||
extern uint16_t asn1_build_prkd_ecc(const uint8_t *label,
|
||||
uint16_t label_len,
|
||||
const uint8_t *keyid,
|
||||
uint16_t keyid_len,
|
||||
uint16_t keysize,
|
||||
uint8_t *buf,
|
||||
uint16_t buf_len);
|
||||
extern uint16_t asn1_build_prkd_rsa(const uint8_t *label,
|
||||
uint16_t label_len,
|
||||
const uint8_t *keyid,
|
||||
uint16_t keyid_len,
|
||||
uint16_t keysize,
|
||||
uint8_t *buf,
|
||||
uint16_t buf_len);
|
||||
extern uint16_t asn1_build_prkd_aes(const uint8_t *label,
|
||||
uint16_t label_len,
|
||||
const uint8_t *keyid,
|
||||
uint16_t keyid_len,
|
||||
uint16_t keysize,
|
||||
uint8_t *buf,
|
||||
uint16_t buf_len);
|
||||
extern uint16_t asn1_build_prkd_generic(const uint8_t *label,
|
||||
uint16_t label_len,
|
||||
const uint8_t *keyid,
|
||||
uint16_t keyid_len,
|
||||
uint16_t keysize,
|
||||
int key_tpe,
|
||||
uint8_t *buf,
|
||||
uint16_t buf_len);
|
||||
#endif
|
||||
116
src/hsm/files.c
Normal file
116
src/hsm/files.c
Normal file
|
|
@ -0,0 +1,116 @@
|
|||
/*
|
||||
* This file is part of the Pico HSM distribution (https://github.com/polhenarejos/pico-hsm).
|
||||
* Copyright (c) 2022 Pol Henarejos.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "files.h"
|
||||
|
||||
extern const uint8_t sc_hsm_aid[];
|
||||
extern int parse_token_info(const file_t *f, int mode);
|
||||
extern int parse_ef_dir(const file_t *f, int mode);
|
||||
|
||||
file_t file_entries[] = {
|
||||
/* 0 */ { .fid = 0x3f00, .parent = 0xff, .name = NULL, .type = FILE_TYPE_DF, .data = NULL,
|
||||
.ef_structure = 0, .acl = { 0 } }, // MF
|
||||
/* 1 */ { .fid = 0x2f00, .parent = 0, .name = NULL, .type = FILE_TYPE_WORKING_EF | FILE_DATA_FUNC, .data = (uint8_t *) parse_ef_dir,
|
||||
.ef_structure = FILE_EF_TRANSPARENT, .acl = { 0 } }, //EF.DIR
|
||||
/* 2 */ { .fid = 0x2f01, .parent = 0, .name = NULL, .type = FILE_TYPE_WORKING_EF, .data = NULL,
|
||||
.ef_structure = FILE_EF_TRANSPARENT, .acl = { 0 } }, //EF.ATR
|
||||
/* 3 */ { .fid = EF_TERMCA, .parent = 0, .name = NULL,
|
||||
.type = FILE_TYPE_WORKING_EF | FILE_DATA_FLASH | FILE_PERSISTENT, .data = NULL,
|
||||
.ef_structure = FILE_EF_TRANSPARENT, .acl = { 0 } }, //EF.GDO
|
||||
/* 4 */ { .fid = 0x2f03, .parent = 5, .name = NULL,
|
||||
.type = FILE_TYPE_WORKING_EF | FILE_DATA_FUNC, .data = (uint8_t *) parse_token_info,
|
||||
.ef_structure = FILE_EF_TRANSPARENT, .acl = { 0 } }, //EF.TokenInfo
|
||||
/* 5 */ { .fid = 0x5015, .parent = 0, .name = NULL, .type = FILE_TYPE_DF, .data = NULL,
|
||||
.ef_structure = 0, .acl = { 0 } }, //DF.PKCS15
|
||||
/* 6 */ { .fid = 0x5031, .parent = 5, .name = NULL, .type = FILE_TYPE_WORKING_EF, .data = NULL,
|
||||
.ef_structure = FILE_EF_TRANSPARENT, .acl = { 0 } }, //EF.ODF
|
||||
/* 7 */ { .fid = 0x5032, .parent = 5, .name = NULL, .type = FILE_TYPE_WORKING_EF, .data = NULL,
|
||||
.ef_structure = FILE_EF_TRANSPARENT, .acl = { 0 } }, //EF.TokenInfo
|
||||
/* 8 */ { .fid = 0x5033, .parent = 0, .name = NULL, .type = FILE_TYPE_WORKING_EF, .data = NULL,
|
||||
.ef_structure = FILE_EF_TRANSPARENT, .acl = { 0 } }, //EF.UnusedSpace
|
||||
/* 9 */ { .fid = EF_PIN1, .parent = 5, .name = NULL,
|
||||
.type = FILE_TYPE_INTERNAL_EF | FILE_DATA_FLASH, .data = NULL,
|
||||
.ef_structure = FILE_EF_TRANSPARENT, .acl = { 0xff } }, //PIN (PIN1)
|
||||
/* 10 */ { .fid = EF_PIN1_MAX_RETRIES, .parent = 5, .name = NULL,
|
||||
.type = FILE_TYPE_INTERNAL_EF | FILE_DATA_FLASH, .data = NULL,
|
||||
.ef_structure = FILE_EF_TRANSPARENT, .acl = { 0xff } }, //max retries PIN (PIN1)
|
||||
/* 11 */ { .fid = EF_PIN1_RETRIES, .parent = 5, .name = NULL,
|
||||
.type = FILE_TYPE_INTERNAL_EF | FILE_DATA_FLASH, .data = NULL,
|
||||
.ef_structure = FILE_EF_TRANSPARENT, .acl = { 0xff } }, //retries PIN (PIN1)
|
||||
/* 12 */ { .fid = EF_SOPIN, .parent = 5, .name = NULL,
|
||||
.type = FILE_TYPE_INTERNAL_EF | FILE_DATA_FLASH, .data = NULL,
|
||||
.ef_structure = FILE_EF_TRANSPARENT, .acl = { 0xff } }, //PIN (SOPIN)
|
||||
/* 13 */ { .fid = EF_SOPIN_MAX_RETRIES, .parent = 5, .name = NULL,
|
||||
.type = FILE_TYPE_INTERNAL_EF | FILE_DATA_FLASH, .data = NULL,
|
||||
.ef_structure = FILE_EF_TRANSPARENT, .acl = { 0xff } }, //max retries PIN (SOPIN)
|
||||
/* 14 */ { .fid = EF_SOPIN_RETRIES, .parent = 5, .name = NULL,
|
||||
.type = FILE_TYPE_INTERNAL_EF | FILE_DATA_FLASH, .data = NULL,
|
||||
.ef_structure = FILE_EF_TRANSPARENT, .acl = { 0xff } }, //retries PIN (SOPIN)
|
||||
/* 15 */ { .fid = EF_DEVOPS, .parent = 5, .name = NULL,
|
||||
.type = FILE_TYPE_INTERNAL_EF | FILE_DATA_FLASH | FILE_PERSISTENT, .data = NULL,
|
||||
.ef_structure = FILE_EF_TRANSPARENT, .acl = { 0xff } }, //Device options
|
||||
/* 16 */ { .fid = EF_PRKDFS, .parent = 5, .name = NULL, .type = FILE_TYPE_WORKING_EF,
|
||||
.data = NULL, .ef_structure = FILE_EF_TRANSPARENT, .acl = { 0 } }, //EF.PrKDFs
|
||||
/* 17 */ { .fid = EF_PUKDFS, .parent = 5, .name = NULL, .type = FILE_TYPE_WORKING_EF,
|
||||
.data = NULL, .ef_structure = FILE_EF_TRANSPARENT, .acl = { 0 } }, //EF.PuKDFs
|
||||
/* 18 */ { .fid = EF_CDFS, .parent = 5, .name = NULL, .type = FILE_TYPE_WORKING_EF,
|
||||
.data = NULL, .ef_structure = FILE_EF_TRANSPARENT, .acl = { 0 } }, //EF.CDFs
|
||||
/* 19 */ { .fid = EF_AODFS, .parent = 5, .name = NULL, .type = FILE_TYPE_WORKING_EF,
|
||||
.data = NULL, .ef_structure = FILE_EF_TRANSPARENT, .acl = { 0 } }, //EF.AODFs
|
||||
/* 20 */ { .fid = EF_DODFS, .parent = 5, .name = NULL, .type = FILE_TYPE_WORKING_EF,
|
||||
.data = NULL, .ef_structure = FILE_EF_TRANSPARENT, .acl = { 0 } }, //EF.DODFs
|
||||
/* 21 */ { .fid = EF_SKDFS, .parent = 5, .name = NULL, .type = FILE_TYPE_WORKING_EF,
|
||||
.data = NULL, .ef_structure = FILE_EF_TRANSPARENT, .acl = { 0 } }, //EF.SKDFs
|
||||
/* 22 */ { .fid = EF_KEY_DOMAIN, .parent = 5, .name = NULL,
|
||||
.type = FILE_TYPE_INTERNAL_EF | FILE_DATA_FLASH, .data = NULL,
|
||||
.ef_structure = FILE_EF_TRANSPARENT, .acl = { 0xff } }, //Key domain options
|
||||
/* 23 */ { .fid = EF_META, .parent = 5, .name = NULL,
|
||||
.type = FILE_TYPE_INTERNAL_EF | FILE_DATA_FLASH, .data = NULL,
|
||||
.ef_structure = FILE_EF_TRANSPARENT, .acl = { 0xff } }, //EF.CDFs
|
||||
/* 24 */ { .fid = EF_PUKAUT, .parent = 5, .name = NULL,
|
||||
.type = FILE_TYPE_INTERNAL_EF | FILE_DATA_FLASH, .data = NULL,
|
||||
.ef_structure = FILE_EF_TRANSPARENT, .acl = { 0xff } }, //Public Key Authentication
|
||||
/* 25 */ { .fid = EF_KEY_DEV, .parent = 5, .name = NULL,
|
||||
.type = FILE_TYPE_INTERNAL_EF | FILE_DATA_FLASH | FILE_PERSISTENT, .data = NULL,
|
||||
.ef_structure = FILE_EF_TRANSPARENT, .acl = { 0xff } }, //Device Key
|
||||
/* 26 */ { .fid = EF_PRKD_DEV, .parent = 5, .name = NULL,
|
||||
.type = FILE_TYPE_INTERNAL_EF | FILE_DATA_FLASH | FILE_PERSISTENT, .data = NULL,
|
||||
.ef_structure = FILE_EF_TRANSPARENT, .acl = { 0xff } }, //PrKD Device
|
||||
/* 27 */ { .fid = EF_EE_DEV, .parent = 5, .name = NULL,
|
||||
.type = FILE_TYPE_INTERNAL_EF | FILE_DATA_FLASH | FILE_PERSISTENT, .data = NULL,
|
||||
.ef_structure = FILE_EF_TRANSPARENT, .acl = { 0xff } }, //End Entity Certificate Device
|
||||
/* 28 */ { .fid = EF_MKEK, .parent = 5, .name = NULL,
|
||||
.type = FILE_TYPE_INTERNAL_EF | FILE_DATA_FLASH | FILE_PERSISTENT, .data = NULL,
|
||||
.ef_structure = FILE_EF_TRANSPARENT, .acl = { 0xff } }, //MKEK
|
||||
/* 29 */ { .fid = EF_MKEK_SO, .parent = 5, .name = NULL,
|
||||
.type = FILE_TYPE_INTERNAL_EF | FILE_DATA_FLASH | FILE_PERSISTENT, .data = NULL,
|
||||
.ef_structure = FILE_EF_TRANSPARENT, .acl = { 0xff } }, //MKEK with SO-PIN
|
||||
///* 30 */ { .fid = 0x0000, .parent = 0, .name = openpgpcard_aid, .type = FILE_TYPE_WORKING_EF, .data = NULL, .ef_structure = FILE_EF_TRANSPARENT, .acl = {0} },
|
||||
/* 31 */ { .fid = 0x0000, .parent = 5, .name = sc_hsm_aid, .type = FILE_TYPE_WORKING_EF,
|
||||
.data = NULL, .ef_structure = FILE_EF_TRANSPARENT, .acl = { 0 } },
|
||||
/* 32 */ { .fid = 0x0000, .parent = 0xff, .name = NULL, .type = FILE_TYPE_NOT_KNOWN, .data = NULL,
|
||||
.ef_structure = 0, .acl = { 0 } } //end
|
||||
};
|
||||
|
||||
const file_t *MF = &file_entries[0];
|
||||
const file_t *file_last = &file_entries[sizeof(file_entries) / sizeof(file_t) - 1];
|
||||
const file_t *file_openpgp = &file_entries[sizeof(file_entries) / sizeof(file_t) - 3];
|
||||
const file_t *file_sc_hsm = &file_entries[sizeof(file_entries) / sizeof(file_t) - 2];
|
||||
file_t *file_pin1 = NULL;
|
||||
file_t *file_retries_pin1 = NULL;
|
||||
file_t *file_sopin = NULL;
|
||||
file_t *file_retries_sopin = NULL;
|
||||
59
src/hsm/files.h
Normal file
59
src/hsm/files.h
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
/*
|
||||
* This file is part of the Pico HSM distribution (https://github.com/polhenarejos/pico-hsm).
|
||||
* Copyright (c) 2022 Pol Henarejos.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
#ifndef _FILES_H_
|
||||
#define _FILES_H_
|
||||
|
||||
#include "file.h"
|
||||
|
||||
#define EF_DEVOPS 0x100E
|
||||
#define EF_MKEK 0x100A
|
||||
#define EF_MKEK_SO 0x100B
|
||||
#define EF_XKEK 0x1070
|
||||
#define EF_PIN1 0x1081
|
||||
#define EF_PIN1_MAX_RETRIES 0x1082
|
||||
#define EF_PIN1_RETRIES 0x1083
|
||||
#define EF_SOPIN 0x1088
|
||||
#define EF_SOPIN_MAX_RETRIES 0x1089
|
||||
#define EF_SOPIN_RETRIES 0x108A
|
||||
#define EF_DKEK 0x1090
|
||||
#define EF_KEY_DOMAIN 0x10A0
|
||||
#define EF_PUKAUT 0x10C0
|
||||
#define EF_PUK 0x10D0
|
||||
#define EF_MASTER_SEED 0x1110
|
||||
#define EF_PRKDFS 0x6040
|
||||
#define EF_PUKDFS 0x6041
|
||||
#define EF_CDFS 0x6042
|
||||
#define EF_AODFS 0x6043
|
||||
#define EF_DODFS 0x6044
|
||||
#define EF_SKDFS 0x6045
|
||||
|
||||
#define EF_KEY_DEV 0xCC00
|
||||
#define EF_PRKD_DEV 0xC400
|
||||
#define EF_EE_DEV 0xCE00
|
||||
|
||||
#define EF_TERMCA 0x2F02
|
||||
#define EF_TOKENINFO 0x2F03
|
||||
#define EF_STATICTOKEN 0xCB00
|
||||
|
||||
extern file_t *file_pin1;
|
||||
extern file_t *file_retries_pin1;
|
||||
extern file_t *file_sopin;
|
||||
extern file_t *file_retries_sopin;
|
||||
|
||||
#endif
|
||||
1629
src/hsm/hsm2040.c
1629
src/hsm/hsm2040.c
File diff suppressed because it is too large
Load diff
|
|
@ -1,149 +0,0 @@
|
|||
/**
|
||||
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
#ifndef _HSM2040_H_
|
||||
#define _HSM2040_H_
|
||||
|
||||
#include "ccid.h"
|
||||
#include "tusb.h"
|
||||
#include "file.h"
|
||||
#include "pico/unique_id.h"
|
||||
|
||||
#define USB_REQ_CCID 0xA1
|
||||
|
||||
typedef struct app {
|
||||
const uint8_t *aid;
|
||||
int (*process_apdu)();
|
||||
struct app* (*select_aid)();
|
||||
int (*unload)();
|
||||
} app_t;
|
||||
|
||||
extern int register_app(app_t * (*)());
|
||||
|
||||
extern const uint8_t historical_bytes[];
|
||||
|
||||
#define DEBUG_PAYLOAD(p,s) { \
|
||||
printf("Payload %s (%d bytes):\r\n", #p,s);\
|
||||
for (int i = 0; i < s; i += 16) {\
|
||||
printf("%07Xh : ",i+p);\
|
||||
for (int j = 0; j < 16; j++) {\
|
||||
if (j < s-i) printf("%02X ",(p)[i+j]);\
|
||||
else printf(" ");\
|
||||
if (j == 7) printf(" ");\
|
||||
} printf(": "); \
|
||||
for (int j = 0; j < MIN(16,s-i); j++) {\
|
||||
printf("%c",(p)[i+j] == 0x0a || (p)[i+j] == 0x0d ? '\\' : (p)[i+j]);\
|
||||
if (j == 7) printf(" ");\
|
||||
}\
|
||||
printf("\r\n");\
|
||||
} printf("\r\n"); \
|
||||
}
|
||||
|
||||
struct apdu {
|
||||
uint8_t seq;
|
||||
|
||||
/* command APDU */
|
||||
uint8_t *cmd_apdu_head; /* CLS INS P1 P2 [ internal Lc ] */
|
||||
uint8_t *cmd_apdu_data;
|
||||
size_t cmd_apdu_data_len; /* Nc, calculated by Lc field */
|
||||
size_t expected_res_size; /* Ne, calculated by Le field */
|
||||
|
||||
/* response APDU */
|
||||
uint16_t sw;
|
||||
uint16_t res_apdu_data_len;
|
||||
uint8_t *res_apdu_data;
|
||||
};
|
||||
|
||||
#define MAX_CMD_APDU_DATA_SIZE (24+4+512*4)
|
||||
#define MAX_RES_APDU_DATA_SIZE (5+9+512*4)
|
||||
#define CCID_MSG_HEADER_SIZE 10
|
||||
#define USB_LL_BUF_SIZE 64
|
||||
|
||||
/* CCID thread */
|
||||
#define EV_CARD_CHANGE 1
|
||||
#define EV_TX_FINISHED 2 /* CCID Tx finished */
|
||||
#define EV_EXEC_ACK_REQUIRED 4 /* OpenPGPcard Execution ACK required */
|
||||
#define EV_EXEC_FINISHED 8 /* OpenPGPcard Execution finished */
|
||||
#define EV_RX_DATA_READY 16 /* USB Rx data available */
|
||||
|
||||
/* OpenPGPcard thread */
|
||||
#define EV_MODIFY_CMD_AVAILABLE 1
|
||||
#define EV_VERIFY_CMD_AVAILABLE 2
|
||||
#define EV_CMD_AVAILABLE 4
|
||||
#define EV_EXIT 8
|
||||
#define EV_PINPAD_INPUT_DONE 16
|
||||
|
||||
enum ccid_state {
|
||||
CCID_STATE_NOCARD, /* No card available */
|
||||
CCID_STATE_START, /* Initial */
|
||||
CCID_STATE_WAIT, /* Waiting APDU */
|
||||
|
||||
CCID_STATE_EXECUTE, /* Executing command */
|
||||
CCID_STATE_ACK_REQUIRED_0, /* Ack required (executing)*/
|
||||
CCID_STATE_ACK_REQUIRED_1, /* Waiting user's ACK (execution finished) */
|
||||
|
||||
CCID_STATE_EXITED, /* CCID Thread Terminated */
|
||||
CCID_STATE_EXEC_REQUESTED, /* Exec requested */
|
||||
};
|
||||
|
||||
#define CLS(a) a.cmd_apdu_head[0]
|
||||
#define INS(a) a.cmd_apdu_head[1]
|
||||
#define P1(a) a.cmd_apdu_head[2]
|
||||
#define P2(a) a.cmd_apdu_head[3]
|
||||
|
||||
#define res_APDU apdu.res_apdu_data
|
||||
#define res_APDU_size apdu.res_apdu_data_len
|
||||
|
||||
extern struct apdu apdu;
|
||||
|
||||
uint16_t set_res_sw (uint8_t sw1, uint8_t sw2);
|
||||
|
||||
|
||||
static inline const uint16_t make_uint16_t(uint8_t b1, uint8_t b2) {
|
||||
return (b1 << 8) | b2;
|
||||
}
|
||||
static inline const uint16_t get_uint16_t(const uint8_t *b, uint16_t offset) {
|
||||
return make_uint16_t(b[offset], b[offset+1]);
|
||||
}
|
||||
static inline const void put_uint16_t(uint16_t n, uint8_t *b) {
|
||||
*b++ = (n >> 8) & 0xff;
|
||||
*b = n & 0xff;
|
||||
}
|
||||
|
||||
|
||||
#ifdef DEBUG
|
||||
void stdout_init (void);
|
||||
#define DEBUG_MORE 1
|
||||
/*
|
||||
* Debug functions in debug.c
|
||||
*/
|
||||
void put_byte (uint8_t b);
|
||||
void put_byte_with_no_nl (uint8_t b);
|
||||
void put_short (uint16_t x);
|
||||
void put_word (uint32_t x);
|
||||
void put_int (uint32_t x);
|
||||
void put_string (const char *s);
|
||||
void put_binary (const char *s, int len);
|
||||
|
||||
#define DEBUG_INFO(msg) put_string (msg)
|
||||
#define DEBUG_WORD(w) put_word (w)
|
||||
#define DEBUG_SHORT(h) put_short (h)
|
||||
#define DEBUG_BYTE(b) put_byte (b)
|
||||
#define DEBUG_BINARY(s,len) put_binary ((const char *)s,len)
|
||||
#else
|
||||
#define DEBUG_INFO(msg)
|
||||
#define DEBUG_WORD(w)
|
||||
#define DEBUG_SHORT(h)
|
||||
#define DEBUG_BYTE(b)
|
||||
#define DEBUG_BINARY(s,len)
|
||||
#endif
|
||||
|
||||
extern int flash_write_data_to_file(file_t *file, const uint8_t *data, uint16_t len);
|
||||
extern void low_flash_available();
|
||||
extern int flash_clear_file(file_t *file);
|
||||
|
||||
extern pico_unique_board_id_t unique_id;
|
||||
#endif
|
||||
714
src/hsm/kek.c
Normal file
714
src/hsm/kek.c
Normal file
|
|
@ -0,0 +1,714 @@
|
|||
/*
|
||||
* This file is part of the Pico HSM distribution (https://github.com/polhenarejos/pico-hsm).
|
||||
* Copyright (c) 2022 Pol Henarejos.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "sc_hsm.h"
|
||||
#include "stdlib.h"
|
||||
#if !defined(ENABLE_EMULATION) && !defined(ESP_PLATFORM)
|
||||
#include "pico/stdlib.h"
|
||||
#endif
|
||||
#include "kek.h"
|
||||
#include "crypto_utils.h"
|
||||
#include "random.h"
|
||||
#include "mbedtls/md.h"
|
||||
#include "mbedtls/cmac.h"
|
||||
#include "mbedtls/rsa.h"
|
||||
#include "mbedtls/ecdsa.h"
|
||||
#include "mbedtls/chachapoly.h"
|
||||
#include "files.h"
|
||||
#include "otp.h"
|
||||
|
||||
extern bool has_session_pin, has_session_sopin;
|
||||
extern uint8_t session_pin[32], session_sopin[32];
|
||||
uint8_t mkek_mask[MKEK_KEY_SIZE];
|
||||
bool has_mkek_mask = false;
|
||||
uint8_t pending_save_dkek = 0xff;
|
||||
|
||||
#define POLY 0xedb88320
|
||||
|
||||
uint32_t crc32c(const uint8_t *buf, size_t len) {
|
||||
uint32_t crc = 0xffffffff;
|
||||
while (len--) {
|
||||
crc ^= *buf++;
|
||||
for (int k = 0; k < 8; k++) {
|
||||
crc = (crc >> 1) ^ (POLY & (0 - (crc & 1)));
|
||||
}
|
||||
}
|
||||
return ~crc;
|
||||
}
|
||||
|
||||
void mkek_masked(uint8_t *mkek, const uint8_t *mask) {
|
||||
if (mask) {
|
||||
for (int i = 0; i < MKEK_KEY_SIZE; i++) {
|
||||
MKEK_KEY(mkek)[i] ^= mask[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int load_mkek(uint8_t *mkek) {
|
||||
if (has_session_pin == false && has_session_sopin == false) {
|
||||
return PICOKEY_NO_LOGIN;
|
||||
}
|
||||
const uint8_t *pin = NULL;
|
||||
if (pin == NULL && has_session_pin == true) {
|
||||
file_t *tf = search_file(EF_MKEK);
|
||||
if (file_has_data(tf)) {
|
||||
memcpy(mkek, file_get_data(tf), MKEK_SIZE);
|
||||
pin = session_pin;
|
||||
}
|
||||
}
|
||||
if (pin == NULL && has_session_sopin == true) {
|
||||
file_t *tf = search_file(EF_MKEK_SO);
|
||||
if (file_has_data(tf)) {
|
||||
memcpy(mkek, file_get_data(tf), MKEK_SIZE);
|
||||
pin = session_sopin;
|
||||
}
|
||||
}
|
||||
if (pin == NULL) { //Should never happen
|
||||
return PICOKEY_EXEC_ERROR;
|
||||
}
|
||||
|
||||
if (has_mkek_mask) {
|
||||
mkek_masked(mkek, mkek_mask);
|
||||
}
|
||||
|
||||
int ret = aes_decrypt_cfb_256(pin, MKEK_IV(mkek), MKEK_KEY(mkek), MKEK_KEY_SIZE + MKEK_KEY_CS_SIZE);
|
||||
if (ret != 0) {
|
||||
return PICOKEY_EXEC_ERROR;
|
||||
}
|
||||
if (crc32c(MKEK_KEY(mkek), MKEK_KEY_SIZE) != *(uint32_t *) MKEK_CHECKSUM(mkek)) {
|
||||
return PICOKEY_WRONG_DKEK;
|
||||
}
|
||||
if (otp_key_1) {
|
||||
mkek_masked(mkek, otp_key_1);
|
||||
}
|
||||
return PICOKEY_OK;
|
||||
}
|
||||
|
||||
mse_t mse = { .init = false };
|
||||
|
||||
int mse_decrypt_ct(uint8_t *data, size_t len) {
|
||||
mbedtls_chachapoly_context chatx;
|
||||
mbedtls_chachapoly_init(&chatx);
|
||||
mbedtls_chachapoly_setkey(&chatx, mse.key_enc + 12);
|
||||
int ret = mbedtls_chachapoly_auth_decrypt(&chatx, len - 16, mse.key_enc, mse.Qpt, 65, data + len - 16, data, data);
|
||||
mbedtls_chachapoly_free(&chatx);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int load_dkek(uint8_t id, uint8_t *dkek) {
|
||||
file_t *tf = search_file(EF_DKEK + id);
|
||||
if (!file_has_data(tf)) {
|
||||
return PICOKEY_ERR_FILE_NOT_FOUND;
|
||||
}
|
||||
memcpy(dkek, file_get_data(tf), DKEK_KEY_SIZE);
|
||||
return mkek_decrypt(dkek, DKEK_KEY_SIZE);
|
||||
}
|
||||
|
||||
void release_mkek(uint8_t *mkek) {
|
||||
mbedtls_platform_zeroize(mkek, MKEK_SIZE);
|
||||
}
|
||||
|
||||
int store_mkek(const uint8_t *mkek) {
|
||||
if (has_session_pin == false && has_session_sopin == false) {
|
||||
return PICOKEY_NO_LOGIN;
|
||||
}
|
||||
uint8_t tmp_mkek[MKEK_SIZE];
|
||||
if (mkek == NULL) {
|
||||
const uint8_t *rd = random_bytes_get(MKEK_IV_SIZE + MKEK_KEY_SIZE);
|
||||
memcpy(tmp_mkek, rd, MKEK_IV_SIZE + MKEK_KEY_SIZE);
|
||||
}
|
||||
else {
|
||||
memcpy(tmp_mkek, mkek, MKEK_SIZE);
|
||||
}
|
||||
if (otp_key_1) {
|
||||
mkek_masked(tmp_mkek, otp_key_1);
|
||||
}
|
||||
*(uint32_t *) MKEK_CHECKSUM(tmp_mkek) = crc32c(MKEK_KEY(tmp_mkek), MKEK_KEY_SIZE);
|
||||
if (has_session_pin) {
|
||||
uint8_t tmp_mkek_pin[MKEK_SIZE];
|
||||
memcpy(tmp_mkek_pin, tmp_mkek, MKEK_SIZE);
|
||||
file_t *tf = search_file(EF_MKEK);
|
||||
if (!tf) {
|
||||
release_mkek(tmp_mkek);
|
||||
release_mkek(tmp_mkek_pin);
|
||||
return PICOKEY_ERR_FILE_NOT_FOUND;
|
||||
}
|
||||
aes_encrypt_cfb_256(session_pin, MKEK_IV(tmp_mkek_pin), MKEK_KEY(tmp_mkek_pin), MKEK_KEY_SIZE + MKEK_KEY_CS_SIZE);
|
||||
file_put_data(tf, tmp_mkek_pin, MKEK_SIZE);
|
||||
release_mkek(tmp_mkek_pin);
|
||||
}
|
||||
if (has_session_sopin) {
|
||||
uint8_t tmp_mkek_sopin[MKEK_SIZE];
|
||||
memcpy(tmp_mkek_sopin, tmp_mkek, MKEK_SIZE);
|
||||
file_t *tf = search_file(EF_MKEK_SO);
|
||||
if (!tf) {
|
||||
release_mkek(tmp_mkek);
|
||||
release_mkek(tmp_mkek_sopin);
|
||||
return PICOKEY_ERR_FILE_NOT_FOUND;
|
||||
}
|
||||
aes_encrypt_cfb_256(session_sopin, MKEK_IV(tmp_mkek_sopin), MKEK_KEY(tmp_mkek_sopin), MKEK_KEY_SIZE + MKEK_KEY_CS_SIZE);
|
||||
file_put_data(tf, tmp_mkek_sopin, MKEK_SIZE);
|
||||
release_mkek(tmp_mkek_sopin);
|
||||
}
|
||||
low_flash_available();
|
||||
release_mkek(tmp_mkek);
|
||||
return PICOKEY_OK;
|
||||
}
|
||||
|
||||
int store_dkek_key(uint8_t id, uint8_t *dkek) {
|
||||
file_t *tf = search_file(EF_DKEK + id);
|
||||
if (!tf) {
|
||||
return PICOKEY_ERR_FILE_NOT_FOUND;
|
||||
}
|
||||
int r = mkek_encrypt(dkek, DKEK_KEY_SIZE);
|
||||
if (r != PICOKEY_OK) {
|
||||
return r;
|
||||
}
|
||||
file_put_data(tf, dkek, DKEK_KEY_SIZE);
|
||||
low_flash_available();
|
||||
return PICOKEY_OK;
|
||||
}
|
||||
|
||||
int save_dkek_key(uint8_t id, const uint8_t *key) {
|
||||
uint8_t dkek[DKEK_KEY_SIZE];
|
||||
if (!key) {
|
||||
file_t *tf = search_file(EF_DKEK + id);
|
||||
if (!tf) {
|
||||
return PICOKEY_ERR_FILE_NOT_FOUND;
|
||||
}
|
||||
memcpy(dkek, file_get_data(tf), DKEK_KEY_SIZE);
|
||||
}
|
||||
else {
|
||||
memcpy(dkek, key, DKEK_KEY_SIZE);
|
||||
}
|
||||
return store_dkek_key(id, dkek);
|
||||
}
|
||||
|
||||
int import_dkek_share(uint8_t id, const uint8_t *share) {
|
||||
uint8_t tmp_dkek[DKEK_KEY_SIZE];
|
||||
file_t *tf = search_file(EF_DKEK + id);
|
||||
if (!tf) {
|
||||
return PICOKEY_ERR_FILE_NOT_FOUND;
|
||||
}
|
||||
memset(tmp_dkek, 0, sizeof(tmp_dkek));
|
||||
if (file_get_size(tf) == DKEK_KEY_SIZE) {
|
||||
memcpy(tmp_dkek, file_get_data(tf), DKEK_KEY_SIZE);
|
||||
}
|
||||
for (int i = 0; i < DKEK_KEY_SIZE; i++) {
|
||||
tmp_dkek[i] ^= share[i];
|
||||
}
|
||||
file_put_data(tf, tmp_dkek, DKEK_KEY_SIZE);
|
||||
low_flash_available();
|
||||
return PICOKEY_OK;
|
||||
}
|
||||
|
||||
int dkek_kcv(uint8_t id, uint8_t *kcv) { //kcv 8 bytes
|
||||
uint8_t hsh[32], dkek[DKEK_KEY_SIZE];
|
||||
memset(kcv, 0, 8);
|
||||
memset(hsh, 0, sizeof(hsh));
|
||||
int r = load_dkek(id, dkek);
|
||||
if (r != PICOKEY_OK) {
|
||||
return r;
|
||||
}
|
||||
hash256(dkek, DKEK_KEY_SIZE, hsh);
|
||||
mbedtls_platform_zeroize(dkek, sizeof(dkek));
|
||||
memcpy(kcv, hsh, 8);
|
||||
return PICOKEY_OK;
|
||||
}
|
||||
|
||||
int dkek_kenc(uint8_t id, uint8_t *kenc) { //kenc 32 bytes
|
||||
uint8_t dkek[DKEK_KEY_SIZE + 4];
|
||||
memset(kenc, 0, 32);
|
||||
int r = load_dkek(id, dkek);
|
||||
if (r != PICOKEY_OK) {
|
||||
return r;
|
||||
}
|
||||
memcpy(dkek + DKEK_KEY_SIZE, "\x0\x0\x0\x1", 4);
|
||||
hash256(dkek, sizeof(dkek), kenc);
|
||||
mbedtls_platform_zeroize(dkek, sizeof(dkek));
|
||||
return PICOKEY_OK;
|
||||
}
|
||||
|
||||
int dkek_kmac(uint8_t id, uint8_t *kmac) { //kmac 32 bytes
|
||||
uint8_t dkek[DKEK_KEY_SIZE + 4];
|
||||
memset(kmac, 0, 32);
|
||||
int r = load_dkek(id, dkek);
|
||||
if (r != PICOKEY_OK) {
|
||||
return r;
|
||||
}
|
||||
memcpy(dkek + DKEK_KEY_SIZE, "\x0\x0\x0\x2", 4);
|
||||
hash256(dkek, DKEK_KEY_SIZE + 4, kmac);
|
||||
mbedtls_platform_zeroize(dkek, sizeof(dkek));
|
||||
return PICOKEY_OK;
|
||||
}
|
||||
|
||||
int mkek_encrypt(uint8_t *data, uint16_t len) {
|
||||
int r;
|
||||
uint8_t mkek[MKEK_SIZE + 4];
|
||||
if ((r = load_mkek(mkek)) != PICOKEY_OK) {
|
||||
return r;
|
||||
}
|
||||
r = aes_encrypt_cfb_256(MKEK_KEY(mkek), MKEK_IV(mkek), data, len);
|
||||
release_mkek(mkek);
|
||||
return r;
|
||||
}
|
||||
|
||||
int mkek_decrypt(uint8_t *data, uint16_t len) {
|
||||
int r;
|
||||
uint8_t mkek[MKEK_SIZE + 4];
|
||||
if ((r = load_mkek(mkek)) != PICOKEY_OK) {
|
||||
return r;
|
||||
}
|
||||
r = aes_decrypt_cfb_256(MKEK_KEY(mkek), MKEK_IV(mkek), data, len);
|
||||
release_mkek(mkek);
|
||||
return r;
|
||||
}
|
||||
|
||||
int dkek_encode_key(uint8_t id, void *key_ctx, int key_type, uint8_t *out, uint16_t *out_len, const uint8_t *allowed, uint16_t allowed_len) {
|
||||
if (!(key_type & PICO_KEYS_KEY_RSA) && !(key_type & PICO_KEYS_KEY_EC) && !(key_type & PICO_KEYS_KEY_AES)) {
|
||||
return PICOKEY_WRONG_DATA;
|
||||
}
|
||||
|
||||
uint8_t kb[8 + 2 * 4 + 2 * 4096 / 8 + 3 + 13]; //worst case: RSA-4096 (plus, 13 bytes padding)
|
||||
memset(kb, 0, sizeof(kb));
|
||||
uint16_t kb_len = 0;
|
||||
int r = 0;
|
||||
uint8_t *algo = NULL;
|
||||
uint8_t algo_len = 0;
|
||||
uint8_t kenc[32];
|
||||
memset(kenc, 0, sizeof(kenc));
|
||||
r = dkek_kenc(id, kenc);
|
||||
if (r != PICOKEY_OK) {
|
||||
return r;
|
||||
}
|
||||
|
||||
uint8_t kcv[8];
|
||||
memset(kcv, 0, sizeof(kcv));
|
||||
r = dkek_kcv(id, kcv);
|
||||
if (r != PICOKEY_OK) {
|
||||
return r;
|
||||
}
|
||||
|
||||
uint8_t kmac[32];
|
||||
memset(kmac, 0, sizeof(kmac));
|
||||
r = dkek_kmac(id, kmac);
|
||||
if (r != PICOKEY_OK) {
|
||||
return r;
|
||||
}
|
||||
|
||||
if (key_type & PICO_KEYS_KEY_AES) {
|
||||
if (key_type & PICO_KEYS_KEY_AES_128) {
|
||||
kb_len = 16;
|
||||
}
|
||||
else if (key_type & PICO_KEYS_KEY_AES_192) {
|
||||
kb_len = 24;
|
||||
}
|
||||
else if (key_type & PICO_KEYS_KEY_AES_256) {
|
||||
kb_len = 32;
|
||||
}
|
||||
else if (key_type & PICO_KEYS_KEY_AES_512) {
|
||||
kb_len = 64;
|
||||
}
|
||||
|
||||
if (kb_len != 16 && kb_len != 24 && kb_len != 32 && kb_len != 64) {
|
||||
return PICOKEY_WRONG_DATA;
|
||||
}
|
||||
if (*out_len < 8 + 1 + 10 + 6 + (2 + 64 + 14) + 16) { // 14 bytes padding
|
||||
return PICOKEY_WRONG_LENGTH;
|
||||
}
|
||||
|
||||
put_uint16_t_be(kb_len, kb + 8);
|
||||
memcpy(kb + 10, key_ctx, kb_len);
|
||||
kb_len += 2;
|
||||
|
||||
algo = (uint8_t *) "\x00\x08\x60\x86\x48\x01\x65\x03\x04\x01"; //2.16.840.1.101.3.4.1 (2+8)
|
||||
algo_len = 10;
|
||||
}
|
||||
else if (key_type & PICO_KEYS_KEY_RSA) {
|
||||
if (*out_len < 8 + 1 + 12 + 6 + (8 + 2 * 4 + 2 * 4096 / 8 + 3 + 13) + 16) { //13 bytes pading
|
||||
return PICOKEY_WRONG_LENGTH;
|
||||
}
|
||||
mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) key_ctx;
|
||||
kb_len = 0;
|
||||
kb_len += put_uint16_t_be((uint16_t)mbedtls_rsa_get_len(rsa) * 8, kb + 8 + kb_len);
|
||||
|
||||
kb_len += put_uint16_t_be((uint16_t)mbedtls_mpi_size(&rsa->D), kb + 8 + kb_len);
|
||||
mbedtls_mpi_write_binary(&rsa->D, kb + 8 + kb_len, mbedtls_mpi_size(&rsa->D));
|
||||
kb_len += (uint16_t)mbedtls_mpi_size(&rsa->D);
|
||||
kb_len += put_uint16_t_be((uint16_t)mbedtls_mpi_size(&rsa->N), kb + 8 + kb_len);
|
||||
mbedtls_mpi_write_binary(&rsa->N, kb + 8 + kb_len, mbedtls_mpi_size(&rsa->N));
|
||||
kb_len += (uint16_t)mbedtls_mpi_size(&rsa->N);
|
||||
kb_len += put_uint16_t_be((uint16_t)mbedtls_mpi_size(&rsa->E), kb + 8 + kb_len);
|
||||
mbedtls_mpi_write_binary(&rsa->E, kb + 8 + kb_len, mbedtls_mpi_size(&rsa->E));
|
||||
kb_len += (uint16_t)mbedtls_mpi_size(&rsa->E);
|
||||
|
||||
algo = (uint8_t *) "\x00\x0A\x04\x00\x7F\x00\x07\x02\x02\x02\x01\x02";
|
||||
algo_len = 12;
|
||||
}
|
||||
else if (key_type & PICO_KEYS_KEY_EC) {
|
||||
if (*out_len < 8 + 1 + 12 + 6 + (8 + 2 * 8 + 9 * 66 + 2 + 4) + 16) { //4 bytes pading
|
||||
return PICOKEY_WRONG_LENGTH;
|
||||
}
|
||||
mbedtls_ecdsa_context *ecdsa = (mbedtls_ecdsa_context *) key_ctx;
|
||||
kb_len = 0;
|
||||
kb_len += put_uint16_t_be((uint16_t)mbedtls_mpi_size(&ecdsa->grp.P) * 8, kb + 8 + kb_len);
|
||||
kb_len += put_uint16_t_be((uint16_t)mbedtls_mpi_size(&ecdsa->grp.A), kb + 8 + kb_len);
|
||||
mbedtls_mpi_write_binary(&ecdsa->grp.A, kb + 8 + kb_len, mbedtls_mpi_size(&ecdsa->grp.A));
|
||||
kb_len += (uint16_t)mbedtls_mpi_size(&ecdsa->grp.A);
|
||||
kb_len += put_uint16_t_be((uint16_t)mbedtls_mpi_size(&ecdsa->grp.B), kb + 8 + kb_len);
|
||||
mbedtls_mpi_write_binary(&ecdsa->grp.B, kb + 8 + kb_len, mbedtls_mpi_size(&ecdsa->grp.B));
|
||||
kb_len += (uint16_t)mbedtls_mpi_size(&ecdsa->grp.B);
|
||||
kb_len += put_uint16_t_be((uint16_t)mbedtls_mpi_size(&ecdsa->grp.P), kb + 8 + kb_len);
|
||||
mbedtls_mpi_write_binary(&ecdsa->grp.P, kb + 8 + kb_len, mbedtls_mpi_size(&ecdsa->grp.P));
|
||||
kb_len += (uint16_t)mbedtls_mpi_size(&ecdsa->grp.P);
|
||||
kb_len += put_uint16_t_be((uint16_t)mbedtls_mpi_size(&ecdsa->grp.N), kb + 8 + kb_len);
|
||||
mbedtls_mpi_write_binary(&ecdsa->grp.N, kb + 8 + kb_len, mbedtls_mpi_size(&ecdsa->grp.N));
|
||||
kb_len += (uint16_t)mbedtls_mpi_size(&ecdsa->grp.N);
|
||||
|
||||
size_t olen = 0;
|
||||
mbedtls_ecp_point_write_binary(&ecdsa->grp, &ecdsa->grp.G, MBEDTLS_ECP_PF_UNCOMPRESSED, &olen, kb + 8 + kb_len + 2, sizeof(kb) - 8 - kb_len - 2);
|
||||
kb_len += put_uint16_t_be((uint16_t)olen, kb + 8 + kb_len);
|
||||
kb_len += (uint16_t)olen;
|
||||
|
||||
kb_len += put_uint16_t_be((uint16_t)mbedtls_mpi_size(&ecdsa->d), kb + 8 + kb_len);
|
||||
mbedtls_mpi_write_binary(&ecdsa->d, kb + 8 + kb_len, mbedtls_mpi_size(&ecdsa->d));
|
||||
kb_len += (uint16_t)mbedtls_mpi_size(&ecdsa->d);
|
||||
|
||||
mbedtls_ecp_point_write_binary(&ecdsa->grp, &ecdsa->Q, MBEDTLS_ECP_PF_UNCOMPRESSED, &olen, kb + 8 + kb_len + 2, sizeof(kb) - 8 - kb_len - 2);
|
||||
kb_len += put_uint16_t_be((uint16_t)olen, kb + 8 + kb_len);
|
||||
kb_len += (uint16_t)olen;
|
||||
|
||||
algo = (uint8_t *) "\x00\x0A\x04\x00\x7F\x00\x07\x02\x02\x02\x02\x03";
|
||||
algo_len = 12;
|
||||
}
|
||||
memset(out, 0, *out_len);
|
||||
*out_len = 0;
|
||||
|
||||
memcpy(out + *out_len, kcv, 8);
|
||||
*out_len += 8;
|
||||
|
||||
if (key_type & PICO_KEYS_KEY_AES) {
|
||||
out[*out_len] = 15;
|
||||
}
|
||||
else if (key_type & PICO_KEYS_KEY_RSA) {
|
||||
out[*out_len] = 5;
|
||||
}
|
||||
else if (key_type & PICO_KEYS_KEY_EC) {
|
||||
out[*out_len] = 12;
|
||||
}
|
||||
*out_len += 1;
|
||||
|
||||
if (algo) {
|
||||
memcpy(out + *out_len, algo, algo_len);
|
||||
*out_len += algo_len;
|
||||
}
|
||||
else {
|
||||
*out_len += 2;
|
||||
}
|
||||
|
||||
if (allowed && allowed_len > 0) {
|
||||
*out_len += put_uint16_t_be(allowed_len, out + *out_len);
|
||||
memcpy(out + *out_len, allowed, allowed_len);
|
||||
*out_len += allowed_len;
|
||||
}
|
||||
else {
|
||||
*out_len += 2;
|
||||
}
|
||||
//add 4 zeros
|
||||
*out_len += 4;
|
||||
|
||||
memcpy(kb, random_bytes_get(8), 8);
|
||||
kb_len += 8; //8 random bytes
|
||||
uint16_t kb_len_pad = ((uint16_t) (kb_len / 16)) * 16;
|
||||
if (kb_len % 16 > 0) {
|
||||
kb_len_pad = ((int) (kb_len / 16) + 1) * 16;
|
||||
}
|
||||
//key already copied at kb+10
|
||||
if (kb_len < kb_len_pad) {
|
||||
kb[kb_len] = 0x80;
|
||||
}
|
||||
r = aes_encrypt(kenc, NULL, 256, PICO_KEYS_AES_MODE_CBC, kb, kb_len_pad);
|
||||
if (r != PICOKEY_OK) {
|
||||
return r;
|
||||
}
|
||||
|
||||
memcpy(out + *out_len, kb, kb_len_pad);
|
||||
*out_len += kb_len_pad;
|
||||
|
||||
r = mbedtls_cipher_cmac(mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_256_ECB), kmac, 256, out, *out_len, out + *out_len);
|
||||
|
||||
*out_len += 16;
|
||||
if (r != 0) {
|
||||
return r;
|
||||
}
|
||||
return PICOKEY_OK;
|
||||
}
|
||||
|
||||
int dkek_type_key(const uint8_t *in) {
|
||||
if (in[8] == 5 || in[8] == 6) {
|
||||
return PICO_KEYS_KEY_RSA;
|
||||
}
|
||||
else if (in[8] == 12) {
|
||||
return PICO_KEYS_KEY_EC;
|
||||
}
|
||||
else if (in[8] == 15) {
|
||||
return PICO_KEYS_KEY_AES;
|
||||
}
|
||||
return 0x0;
|
||||
}
|
||||
|
||||
int dkek_decode_key(uint8_t id, void *key_ctx, const uint8_t *in, uint16_t in_len, int *key_size_out, uint8_t **allowed, uint16_t *allowed_len) {
|
||||
uint8_t kcv[8];
|
||||
int r = 0;
|
||||
memset(kcv, 0, sizeof(kcv));
|
||||
r = dkek_kcv(id, kcv);
|
||||
if (r != PICOKEY_OK) {
|
||||
return r;
|
||||
}
|
||||
|
||||
uint8_t kmac[32];
|
||||
memset(kmac, 0, sizeof(kmac));
|
||||
r = dkek_kmac(id, kmac);
|
||||
if (r != PICOKEY_OK) {
|
||||
return r;
|
||||
}
|
||||
|
||||
uint8_t kenc[32];
|
||||
memset(kenc, 0, sizeof(kenc));
|
||||
r = dkek_kenc(id, kenc);
|
||||
if (r != PICOKEY_OK) {
|
||||
return r;
|
||||
}
|
||||
|
||||
if (memcmp(kcv, in, 8) != 0) {
|
||||
return PICOKEY_WRONG_DKEK;
|
||||
}
|
||||
|
||||
uint8_t signature[16];
|
||||
r = mbedtls_cipher_cmac(mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_256_ECB), kmac, 256, in, in_len - 16, signature);
|
||||
if (r != 0) {
|
||||
return PICOKEY_WRONG_SIGNATURE;
|
||||
}
|
||||
if (memcmp(signature, in + in_len - 16, 16) != 0) {
|
||||
return PICOKEY_WRONG_SIGNATURE;
|
||||
}
|
||||
|
||||
int key_type = in[8];
|
||||
if (key_type != 5 && key_type != 6 && key_type != 12 && key_type != 15) {
|
||||
return PICOKEY_WRONG_DATA;
|
||||
}
|
||||
|
||||
if ((key_type == 5 || key_type == 6) &&
|
||||
memcmp(in + 9, "\x00\x0A\x04\x00\x7F\x00\x07\x02\x02\x02\x01\x02", 12) != 0) {
|
||||
return PICOKEY_WRONG_DATA;
|
||||
}
|
||||
|
||||
if (key_type == 12 &&
|
||||
memcmp(in + 9, "\x00\x0A\x04\x00\x7F\x00\x07\x02\x02\x02\x02\x03", 12) != 0) {
|
||||
return PICOKEY_WRONG_DATA;
|
||||
}
|
||||
|
||||
if (key_type == 15 && memcmp(in + 9, "\x00\x08\x60\x86\x48\x01\x65\x03\x04\x01", 10) != 0) {
|
||||
return PICOKEY_WRONG_DATA;
|
||||
}
|
||||
|
||||
uint16_t ofs = 9;
|
||||
|
||||
//OID
|
||||
uint16_t len = get_uint16_t_be(in + ofs);
|
||||
ofs += len + 2;
|
||||
|
||||
//Allowed algorithms
|
||||
len = get_uint16_t_be(in + ofs);
|
||||
*allowed = (uint8_t *) (in + ofs + 2);
|
||||
*allowed_len = len;
|
||||
ofs += len + 2;
|
||||
|
||||
//Access conditions
|
||||
len = get_uint16_t_be(in + ofs);
|
||||
ofs += len + 2;
|
||||
|
||||
//Key OID
|
||||
len = get_uint16_t_be(in + ofs);
|
||||
ofs += len + 2;
|
||||
|
||||
if ((in_len - 16 - ofs) % 16 != 0) {
|
||||
return PICOKEY_WRONG_PADDING;
|
||||
}
|
||||
uint8_t kb[8 + 2 * 4 + 2 * 4096 / 8 + 3 + 13]; //worst case: RSA-4096 (plus, 13 bytes padding)
|
||||
memset(kb, 0, sizeof(kb));
|
||||
memcpy(kb, in + ofs, in_len - 16 - ofs);
|
||||
r = aes_decrypt(kenc, NULL, 256, PICO_KEYS_AES_MODE_CBC, kb, in_len - 16 - ofs);
|
||||
if (r != PICOKEY_OK) {
|
||||
return r;
|
||||
}
|
||||
|
||||
int key_size = get_uint16_t_be(kb + 8);
|
||||
if (key_size_out) {
|
||||
*key_size_out = key_size;
|
||||
}
|
||||
ofs = 10;
|
||||
if (key_type == 5 || key_type == 6) {
|
||||
mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) key_ctx;
|
||||
mbedtls_rsa_init(rsa);
|
||||
if (key_type == 5) {
|
||||
len = get_uint16_t_be(kb + ofs); ofs += 2;
|
||||
r = mbedtls_mpi_read_binary(&rsa->D, kb + ofs, len); ofs += len;
|
||||
if (r != 0) {
|
||||
mbedtls_rsa_free(rsa);
|
||||
return PICOKEY_WRONG_DATA;
|
||||
}
|
||||
|
||||
len = get_uint16_t_be(kb + ofs); ofs += 2;
|
||||
r = mbedtls_mpi_read_binary(&rsa->N, kb + ofs, len); ofs += len;
|
||||
if (r != 0) {
|
||||
mbedtls_rsa_free(rsa);
|
||||
return PICOKEY_WRONG_DATA;
|
||||
}
|
||||
}
|
||||
else if (key_type == 6) {
|
||||
//DP-1
|
||||
len = get_uint16_t_be(kb + ofs); ofs += len + 2;
|
||||
|
||||
//DQ-1
|
||||
len = get_uint16_t_be(kb + ofs); ofs += len + 2;
|
||||
|
||||
len = get_uint16_t_be(kb + ofs); ofs += 2;
|
||||
r = mbedtls_mpi_read_binary(&rsa->P, kb + ofs, len); ofs += len;
|
||||
if (r != 0) {
|
||||
mbedtls_rsa_free(rsa);
|
||||
return PICOKEY_WRONG_DATA;
|
||||
}
|
||||
|
||||
//PQ
|
||||
len = get_uint16_t_be(kb + ofs); ofs += len + 2;
|
||||
|
||||
len = get_uint16_t_be(kb + ofs); ofs += 2;
|
||||
r = mbedtls_mpi_read_binary(&rsa->Q, kb + ofs, len); ofs += len;
|
||||
if (r != 0) {
|
||||
mbedtls_rsa_free(rsa);
|
||||
return PICOKEY_WRONG_DATA;
|
||||
}
|
||||
//N
|
||||
len = get_uint16_t_be(kb + ofs); ofs += len + 2;
|
||||
}
|
||||
|
||||
len = get_uint16_t_be(kb + ofs); ofs += 2;
|
||||
r = mbedtls_mpi_read_binary(&rsa->E, kb + ofs, len); ofs += len;
|
||||
if (r != 0) {
|
||||
mbedtls_rsa_free(rsa);
|
||||
return PICOKEY_WRONG_DATA;
|
||||
}
|
||||
|
||||
if (key_type == 5) {
|
||||
r = mbedtls_rsa_import(rsa, &rsa->N, NULL, NULL, &rsa->D, &rsa->E);
|
||||
if (r != 0) {
|
||||
mbedtls_rsa_free(rsa);
|
||||
return PICOKEY_EXEC_ERROR;
|
||||
}
|
||||
}
|
||||
else if (key_type == 6) {
|
||||
r = mbedtls_rsa_import(rsa, NULL, &rsa->P, &rsa->Q, NULL, &rsa->E);
|
||||
if (r != 0) {
|
||||
mbedtls_rsa_free(rsa);
|
||||
return PICOKEY_EXEC_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
r = mbedtls_rsa_complete(rsa);
|
||||
if (r != 0) {
|
||||
mbedtls_rsa_free(rsa);
|
||||
return PICOKEY_EXEC_ERROR;
|
||||
}
|
||||
r = mbedtls_rsa_check_privkey(rsa);
|
||||
if (r != 0) {
|
||||
mbedtls_rsa_free(rsa);
|
||||
return PICOKEY_EXEC_ERROR;
|
||||
}
|
||||
}
|
||||
else if (key_type == 12) {
|
||||
mbedtls_ecdsa_context *ecdsa = (mbedtls_ecdsa_context *) key_ctx;
|
||||
mbedtls_ecdsa_init(ecdsa);
|
||||
|
||||
//A
|
||||
len = get_uint16_t_be(kb + ofs); ofs += len + 2;
|
||||
|
||||
//B
|
||||
len = get_uint16_t_be(kb + ofs); ofs += len + 2;
|
||||
|
||||
//P
|
||||
len = get_uint16_t_be(kb + ofs); ofs += 2;
|
||||
mbedtls_ecp_group_id ec_id = ec_get_curve_from_prime(kb + ofs, len);
|
||||
if (ec_id == MBEDTLS_ECP_DP_NONE) {
|
||||
mbedtls_ecdsa_free(ecdsa);
|
||||
return PICOKEY_WRONG_DATA;
|
||||
}
|
||||
ofs += len;
|
||||
|
||||
//N
|
||||
len = get_uint16_t_be(kb + ofs); ofs += len + 2;
|
||||
|
||||
//G
|
||||
len = get_uint16_t_be(kb + ofs);
|
||||
#ifdef MBEDTLS_EDDSA_C
|
||||
if (ec_id == MBEDTLS_ECP_DP_CURVE25519 && kb[ofs + 2] != 0x09) {
|
||||
ec_id = MBEDTLS_ECP_DP_ED25519;
|
||||
}
|
||||
else if (ec_id == MBEDTLS_ECP_DP_CURVE448 && (len != 56 || kb[ofs + 2] != 0x05)) {
|
||||
ec_id = MBEDTLS_ECP_DP_ED448;
|
||||
}
|
||||
#endif
|
||||
ofs += len + 2;
|
||||
|
||||
//d
|
||||
len = get_uint16_t_be(kb + ofs); ofs += 2;
|
||||
r = mbedtls_ecp_read_key(ec_id, ecdsa, kb + ofs, len);
|
||||
if (r != 0) {
|
||||
mbedtls_ecdsa_free(ecdsa);
|
||||
return PICOKEY_EXEC_ERROR;
|
||||
}
|
||||
ofs += len;
|
||||
|
||||
//Q
|
||||
len = get_uint16_t_be(kb + ofs); ofs += 2;
|
||||
r = mbedtls_ecp_point_read_binary(&ecdsa->grp, &ecdsa->Q, kb + ofs, len);
|
||||
if (r != 0) {
|
||||
#ifdef MBEDTLS_EDDSA_C
|
||||
if (mbedtls_ecp_get_type(&ecdsa->grp) == MBEDTLS_ECP_TYPE_EDWARDS) {
|
||||
r = mbedtls_ecp_point_edwards(&ecdsa->grp, &ecdsa->Q, &ecdsa->d, random_gen, NULL);
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
r = mbedtls_ecp_mul(&ecdsa->grp, &ecdsa->Q, &ecdsa->d, &ecdsa->grp.G, random_gen, NULL);
|
||||
}
|
||||
if (r != 0) {
|
||||
mbedtls_ecdsa_free(ecdsa);
|
||||
return PICOKEY_EXEC_ERROR;
|
||||
}
|
||||
}
|
||||
r = mbedtls_ecp_check_pub_priv(ecdsa, ecdsa, random_gen, NULL);
|
||||
if (r != 0) {
|
||||
mbedtls_ecdsa_free(ecdsa);
|
||||
return PICOKEY_EXEC_ERROR;
|
||||
}
|
||||
}
|
||||
else if (key_type == 15) {
|
||||
memcpy(key_ctx, kb + ofs, key_size);
|
||||
}
|
||||
return PICOKEY_OK;
|
||||
}
|
||||
80
src/hsm/kek.h
Normal file
80
src/hsm/kek.h
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
/*
|
||||
* This file is part of the Pico HSM distribution (https://github.com/polhenarejos/pico-hsm).
|
||||
* Copyright (c) 2022 Pol Henarejos.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef _DKEK_H_
|
||||
#define _DKEK_H_
|
||||
|
||||
#include "crypto_utils.h"
|
||||
#if defined(ENABLE_EMULATION) || defined(ESP_PLATFORM)
|
||||
#include <stdbool.h>
|
||||
#endif
|
||||
|
||||
|
||||
extern int load_mkek(uint8_t *);
|
||||
extern int store_mkek(const uint8_t *);
|
||||
extern int save_dkek_key(uint8_t, const uint8_t *key);
|
||||
extern int store_dkek_key(uint8_t, uint8_t *);
|
||||
extern void init_mkek();
|
||||
extern void release_mkek(uint8_t *);
|
||||
extern int import_dkek_share(uint8_t, const uint8_t *share);
|
||||
extern int dkek_kcv(uint8_t, uint8_t *kcv);
|
||||
extern int mkek_encrypt(uint8_t *data, uint16_t len);
|
||||
extern int mkek_decrypt(uint8_t *data, uint16_t len);
|
||||
extern int dkek_encode_key(uint8_t,
|
||||
void *key_ctx,
|
||||
int key_type,
|
||||
uint8_t *out,
|
||||
uint16_t *out_len,
|
||||
const uint8_t *,
|
||||
uint16_t);
|
||||
extern int dkek_type_key(const uint8_t *in);
|
||||
extern int dkek_decode_key(uint8_t,
|
||||
void *key_ctx,
|
||||
const uint8_t *in,
|
||||
uint16_t in_len,
|
||||
int *key_size_out,
|
||||
uint8_t **,
|
||||
uint16_t *);
|
||||
|
||||
#define MAX_DKEK_ENCODE_KEY_BUFFER (8 + 1 + 12 + 6 + (8 + 2 * 4 + 2 * 4096 / 8 + 3 + 13) + 16)
|
||||
|
||||
#define MAX_KEY_DOMAINS 16
|
||||
|
||||
#define MKEK_IV_SIZE (IV_SIZE)
|
||||
#define MKEK_KEY_SIZE (32)
|
||||
#define MKEK_KEY_CS_SIZE (4)
|
||||
#define MKEK_SIZE (MKEK_IV_SIZE + MKEK_KEY_SIZE + MKEK_KEY_CS_SIZE)
|
||||
#define MKEK_IV(p) (p)
|
||||
#define MKEK_KEY(p) (MKEK_IV(p) + MKEK_IV_SIZE)
|
||||
#define MKEK_CHECKSUM(p) (MKEK_KEY(p) + MKEK_KEY_SIZE)
|
||||
#define DKEK_KEY_SIZE (32)
|
||||
|
||||
extern uint8_t mkek_mask[MKEK_KEY_SIZE];
|
||||
extern bool has_mkek_mask;
|
||||
|
||||
typedef struct mse {
|
||||
uint8_t Qpt[65];
|
||||
uint8_t key_enc[12 + 32];
|
||||
bool init;
|
||||
} mse_t;
|
||||
extern mse_t mse;
|
||||
|
||||
extern int mse_decrypt_ct(uint8_t *, size_t);
|
||||
|
||||
extern uint8_t pending_save_dkek;
|
||||
|
||||
#endif
|
||||
18
src/hsm/oid.c
Normal file
18
src/hsm/oid.c
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
/*
|
||||
* This file is part of the Pico HSM distribution (https://github.com/polhenarejos/pico-hsm).
|
||||
* Copyright (c) 2022 Pol Henarejos.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "oid.h"
|
||||
177
src/hsm/oid.h
Normal file
177
src/hsm/oid.h
Normal file
|
|
@ -0,0 +1,177 @@
|
|||
/*
|
||||
* This file is part of the Pico HSM distribution (https://github.com/polhenarejos/pico-hsm).
|
||||
* Copyright (c) 2022 Pol Henarejos.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef _OID_H_
|
||||
#define _OID_H_
|
||||
|
||||
#define OID_BSI_DE "\x04\x00\x7F\x00\x07"
|
||||
|
||||
#define OID_ECKA OID_BSI_DE "\x01\x01\x05"
|
||||
#define OID_ECKA_EG OID_ECKA "\x01"
|
||||
#define OID_ECKA_EG_X963KDF OID_ECKA_EG "\x01"
|
||||
#define OID_ECKA_EG_X963KDF_SHA1 OID_ECKA_EG_X963KDF "\x01"
|
||||
#define OID_ECKA_EG_X963KDF_SHA224 OID_ECKA_EG_X963KDF "\x02"
|
||||
#define OID_ECKA_EG_X963KDF_SHA256 OID_ECKA_EG_X963KDF "\x03"
|
||||
#define OID_ECKA_EG_X963KDF_SHA384 OID_ECKA_EG_X963KDF "\x04"
|
||||
#define OID_ECKA_EG_X963KDF_SHA512 OID_ECKA_EG_X963KDF "\x05"
|
||||
#define OID_ECKA_DH OID_ECKA "\x02"
|
||||
#define OID_ECKA_DH_X963KDF OID_ECKA_DH "\x01"
|
||||
#define OID_ECKA_DH_X963KDF_SHA1 OID_ECKA_DH_X963KDF "\x01"
|
||||
#define OID_ECKA_DH_X963KDF_SHA224 OID_ECKA_DH_X963KDF "\x02"
|
||||
#define OID_ECKA_DH_X963KDF_SHA256 OID_ECKA_DH_X963KDF "\x03"
|
||||
#define OID_ECKA_DH_X963KDF_SHA384 OID_ECKA_DH_X963KDF "\x04"
|
||||
#define OID_ECKA_DH_X963KDF_SHA512 OID_ECKA_DH_X963KDF "\x05"
|
||||
|
||||
#define OID_ID_PK OID_BSI_DE "\x02\x02\x01"
|
||||
#define OID_ID_PK_DH OID_ID_PK "\x01"
|
||||
#define OID_ID_PK_ECDH OID_ID_PK "\x02"
|
||||
|
||||
#define OID_ID_TA OID_BSI_DE "\x02\x02\x02"
|
||||
|
||||
#define OID_ID_TA_RSA OID_ID_TA "\x01"
|
||||
|
||||
#define OID_ID_TA_RSA_V1_5_SHA_1 OID_ID_TA_RSA "\x01"
|
||||
#define OID_ID_TA_RSA_V1_5_SHA_256 OID_ID_TA_RSA "\x02"
|
||||
#define OID_ID_TA_RSA_PSS_SHA_1 OID_ID_TA_RSA "\x03"
|
||||
#define OID_ID_TA_RSA_PSS_SHA_256 OID_ID_TA_RSA "\x04"
|
||||
#define OID_ID_TA_RSA_V1_5_SHA_512 OID_ID_TA_RSA "\x05"
|
||||
#define OID_ID_TA_RSA_PSS_SHA_512 OID_ID_TA_RSA "\x06"
|
||||
|
||||
#define OID_ID_TA_ECDSA OID_ID_TA "\x02"
|
||||
|
||||
#define OID_ID_TA_ECDSA_SHA_1 OID_ID_TA_ECDSA "\x01"
|
||||
#define OID_ID_TA_ECDSA_SHA_224 OID_ID_TA_ECDSA "\x02"
|
||||
#define OID_ID_TA_ECDSA_SHA_256 OID_ID_TA_ECDSA "\x03"
|
||||
#define OID_ID_TA_ECDSA_SHA_384 OID_ID_TA_ECDSA "\x04"
|
||||
#define OID_ID_TA_ECDSA_SHA_512 OID_ID_TA_ECDSA "\x05"
|
||||
|
||||
#define OID_ID_CA OID_BSI_DE "\x02\x02\x03"
|
||||
|
||||
#define OID_ID_CA_DH OID_ID_CA "\x01"
|
||||
#define OID_ID_CA_DH_3DES_CBC_CBC OID_ID_CA_DH "\x01"
|
||||
#define OID_ID_CA_DH_AES_CBC_CMAC_128 OID_ID_CA_DH "\x02"
|
||||
#define OID_ID_CA_DH_AES_CBC_CMAC_192 OID_ID_CA_DH "\x03"
|
||||
#define OID_ID_CA_DH_AES_CBC_CMAC_256 OID_ID_CA_DH "\x04"
|
||||
|
||||
#define OID_ID_CA_ECDH OID_ID_CA "\x02"
|
||||
#define OID_ID_CA_ECDH_3DES_CBC_CBC OID_ID_CA_ECDH "\x01"
|
||||
#define OID_ID_CA_ECDH_AES_CBC_CMAC_128 OID_ID_CA_ECDH "\x02"
|
||||
#define OID_ID_CA_ECDH_AES_CBC_CMAC_192 OID_ID_CA_ECDH "\x03"
|
||||
#define OID_ID_CA_ECDH_AES_CBC_CMAC_256 OID_ID_CA_ECDH "\x04"
|
||||
|
||||
#define OID_ID_RI OID_BSI_DE "\x02\x02\x05"
|
||||
|
||||
#define OID_ID_RI_DH OID_ID_RI "\x01"
|
||||
|
||||
#define OID_ID_RI_DH_SHA_1 OID_ID_RI_DH "\x01"
|
||||
#define OID_ID_RI_DH_SHA_224 OID_ID_RI_DH "\x02"
|
||||
#define OID_ID_RI_DH_SHA_256 OID_ID_RI_DH "\x03"
|
||||
|
||||
#define OID_ID_RI_ECDH OID_ID_RI "\x02"
|
||||
|
||||
#define OID_ID_RI_ECDH_SHA_1 OID_ID_RI_ECDH "\x01"
|
||||
#define OID_ID_RI_ECDH_SHA_224 OID_ID_RI_ECDH "\x02"
|
||||
#define OID_ID_RI_ECDH_SHA_256 OID_ID_RI_ECDH "\x03"
|
||||
|
||||
#define OID_ID_CI OID_BSI_DE "\x02\x02\x06"
|
||||
|
||||
#define OID_CARDCONTACT "\x2B\x06\x01\x04\x01\x81\xC3\x1F"
|
||||
|
||||
#define OID_OPENSCDP OID_CARDCONTACT "\x01"
|
||||
#define OID_CC_ISO7816 OID_CARDCONTACT "\x02"
|
||||
#define OID_CC_PKI OID_CARDCONTACT "\x03"
|
||||
#define OID_CC_FORMAT OID_CARDCONTACT "\x04"
|
||||
#define OID_CC_GP_PROFILES OID_CARDCONTACT "\x10"
|
||||
|
||||
#define OID_SCSH3 OID_OPENSCDP "\x01"
|
||||
#define OID_SCSH3GUI OID_OPENSCDP "\x02"
|
||||
|
||||
#define OID_SMARCARD_HSM OID_CC_ISO7816 "\x01"
|
||||
#define OID_CC_APDUTEST OID_CC_ISO7816 "\x02"
|
||||
#define OID_CC_PACKAGES OID_CC_ISO7816 "\x7F"
|
||||
|
||||
#define OID_CC_ROLES OID_CC_PKI "\x01"
|
||||
#define OID_CC_ROLE_SC_HSM OID_CC_ROLES "\x01"
|
||||
|
||||
#define OID_CC_EXTENSIONS OID_CC_PKI "\x02"
|
||||
#define OID_ID_IMPU OID_CC_EXTENSIONS "\x01"
|
||||
#define OID_ID_KEY_DOMAIN_UID OID_CC_EXTENSIONS "\x02"
|
||||
|
||||
#define OID_CC_FF_DEVICEID OID_CC_FORMAT "\x01"
|
||||
#define OID_CC_FF_KDM OID_CC_FORMAT "\x02"
|
||||
#define OID_CC_FF_PKA OID_CC_FORMAT "\x03"
|
||||
#define OID_CC_FF_KDA OID_CC_FORMAT "\x04"
|
||||
|
||||
#define OID_RSADSI "\x2A\x86\x48\x86\xF7\x0D"
|
||||
|
||||
#define OID_PKCS OID_RSADSI "\x01"
|
||||
|
||||
#define OID_PKCS_5 OID_PKCS "\x05"
|
||||
#define OID_PKCS5_PBKDF2 OID_PKCS_5 "\x0C"
|
||||
#define OID_PKCS5_PBES2 OID_PKCS_5 "\x0D"
|
||||
|
||||
#define OID_PKCS_9 OID_PKCS "\x09"
|
||||
|
||||
#define OID_PKCS9_SMIME_ALG OID_PKCS_9 "\x10\x03"
|
||||
|
||||
#define OID_CHACHA20_POLY1305 OID_PKCS9_SMIME_ALG "\x12"
|
||||
#define OID_HKDF_SHA256 OID_PKCS9_SMIME_ALG "\x1D"
|
||||
#define OID_HKDF_SHA384 OID_PKCS9_SMIME_ALG "\x1E"
|
||||
#define OID_HKDF_SHA512 OID_PKCS9_SMIME_ALG "\x1F"
|
||||
|
||||
|
||||
#define OID_DIGEST OID_RSADSI "\x02"
|
||||
|
||||
#define OID_HMAC_SHA1 OID_DIGEST "\x07"
|
||||
#define OID_HMAC_SHA224 OID_DIGEST "\x08"
|
||||
#define OID_HMAC_SHA256 OID_DIGEST "\x09"
|
||||
#define OID_HMAC_SHA384 OID_DIGEST "\x0A"
|
||||
#define OID_HMAC_SHA512 OID_DIGEST "\x0B"
|
||||
|
||||
#define OID_KDF_X963 "\x2B\x81\x05\x10\x86\x48\x3F"
|
||||
|
||||
#define OID_NIST_ALG "\x60\x86\x48\x01\x65\x03\x04"
|
||||
#define OID_NIST_AES OID_NIST_ALG "\x01"
|
||||
#define OID_AES128_ECB OID_NIST_AES "\x01"
|
||||
#define OID_AES128_CBC OID_NIST_AES "\x02"
|
||||
#define OID_AES128_OFB OID_NIST_AES "\x03"
|
||||
#define OID_AES128_CFB OID_NIST_AES "\x04"
|
||||
#define OID_AES128_GCM OID_NIST_AES "\x06"
|
||||
#define OID_AES128_CCM OID_NIST_AES "\x07"
|
||||
#define OID_AES128_CTR OID_NIST_AES "\x09" // Not existing
|
||||
#define OID_AES192_ECB OID_NIST_AES "\x15"
|
||||
#define OID_AES192_CBC OID_NIST_AES "\x16"
|
||||
#define OID_AES192_OFB OID_NIST_AES "\x17"
|
||||
#define OID_AES192_CFB OID_NIST_AES "\x18"
|
||||
#define OID_AES192_GCM OID_NIST_AES "\x1A"
|
||||
#define OID_AES192_CCM OID_NIST_AES "\x1B"
|
||||
#define OID_AES192_CTR OID_NIST_AES "\x1D" // Not existing
|
||||
#define OID_AES256_ECB OID_NIST_AES "\x29"
|
||||
#define OID_AES256_CBC OID_NIST_AES "\x2A"
|
||||
#define OID_AES256_OFB OID_NIST_AES "\x2B"
|
||||
#define OID_AES256_CFB OID_NIST_AES "\x2C"
|
||||
#define OID_AES256_GCM OID_NIST_AES "\x2E"
|
||||
#define OID_AES256_CCM OID_NIST_AES "\x2F"
|
||||
#define OID_AES256_CTR OID_NIST_AES "\x31" // Not existing
|
||||
|
||||
#define OID_IEEE_ALG "\x2B\x6F\x02\x8C\x53\x00\x00\x01"
|
||||
#define OID_AES128_XTS OID_IEEE_ALG "\x01"
|
||||
#define OID_AES256_XTS OID_IEEE_ALG "\x02"
|
||||
|
||||
#define OID_HD "\x2B\x06\x01\x04\x01\x83\xA8\x78\x05\x8D\x6B"
|
||||
|
||||
#endif
|
||||
2156
src/hsm/sc_hsm.c
2156
src/hsm/sc_hsm.c
File diff suppressed because it is too large
Load diff
175
src/hsm/sc_hsm.h
175
src/hsm/sc_hsm.h
|
|
@ -1,81 +1,134 @@
|
|||
/*
|
||||
* This file is part of the Pico HSM distribution (https://github.com/polhenarejos/pico-hsm).
|
||||
* Copyright (c) 2022 Pol Henarejos.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef _SC_HSM_H_
|
||||
#define _SC_HSM_H_
|
||||
|
||||
#include <stdlib.h>
|
||||
#ifndef ESP_PLATFORM
|
||||
#include "common.h"
|
||||
#else
|
||||
#define MBEDTLS_ALLOW_PRIVATE_ACCESS
|
||||
#endif
|
||||
#include "mbedtls/rsa.h"
|
||||
#include "mbedtls/ecdsa.h"
|
||||
#if !defined(ENABLE_EMULATION) && !defined(ESP_PLATFORM)
|
||||
#include "pico/stdlib.h"
|
||||
#include "hsm2040.h"
|
||||
#endif
|
||||
#include "file.h"
|
||||
#include "apdu.h"
|
||||
#include "pico_keys.h"
|
||||
#include "usb.h"
|
||||
|
||||
#define MAX_APDU_DATA (USB_BUFFER_SIZE - 20)
|
||||
|
||||
extern const uint8_t sc_hsm_aid[];
|
||||
|
||||
#define SW_BYTES_REMAINING_00() set_res_sw (0x61, 0x00)
|
||||
#define SW_WARNING_STATE_UNCHANGED() set_res_sw (0x62, 0x00)
|
||||
#define SW_PIN_BLOCKED() set_res_sw (0x63, 0x00)
|
||||
#define SW_EXEC_ERROR() set_res_sw (0x64, 0x00)
|
||||
#define SW_MEMORY_FAILURE() set_res_sw (0x65, 0x81)
|
||||
#define SW_WRONG_LENGTH() set_res_sw (0x67, 0x00)
|
||||
#define SW_WRONG_DATA() set_res_sw (0x67, 0x00)
|
||||
#define SW_LOGICAL_CHANNEL_NOT_SUPPORTED() set_res_sw (0x68, 0x81)
|
||||
#define SW_SECURE_MESSAGING_NOT_SUPPORTED() set_res_sw (0x68, 0x82)
|
||||
#define SW_SECURITY_STATUS_NOT_SATISFIED() set_res_sw (0x69, 0x82)
|
||||
#define SW_FILE_INVALID() set_res_sw (0x69, 0x83)
|
||||
#define SW_DATA_INVALID() set_res_sw (0x69, 0x84)
|
||||
#define SW_CONDITIONS_NOT_SATISFIED() set_res_sw (0x69, 0x85)
|
||||
#define SW_COMMAND_NOT_ALLOWED() set_res_sw (0x69, 0x86)
|
||||
#define SW_APPLET_SELECT_FAILED() set_res_sw (0x69, 0x99)
|
||||
#define SW_FUNC_NOT_SUPPORTED() set_res_sw (0x6A, 0x81)
|
||||
#define SW_FILE_NOT_FOUND() set_res_sw (0x6A, 0x82)
|
||||
#define SW_RECORD_NOT_FOUND() set_res_sw (0x6A, 0x83)
|
||||
#define SW_FILE_FULL() set_res_sw (0x6A, 0x84)
|
||||
#define SW_INCORRECT_P1P2() set_res_sw (0x6A, 0x86)
|
||||
#define SW_REFERENCE_NOT_FOUND() set_res_sw (0x6A, 0x88)
|
||||
#define SW_WRONG_P1P2() set_res_sw (0x6B, 0x00)
|
||||
#define SW_CORRECT_LENGTH_00() set_res_sw (0x6C, 0x00)
|
||||
#define SW_INS_NOT_SUPPORTED() set_res_sw (0x6D, 0x00)
|
||||
#define SW_CLA_NOT_SUPPORTED() set_res_sw (0x6E, 0x00)
|
||||
#define SW_UNKNOWN() set_res_sw (0x6F, 0x00)
|
||||
#define SW_OK() set_res_sw (0x90, 0x00)
|
||||
#define ALGO_RSA_RAW 0x20 /* RSA signature with external padding */
|
||||
#define ALGO_RSA_DECRYPT 0x21 /* RSA raw decrypt */
|
||||
#define ALGO_RSA_DECRYPT_PKCS1 0x22
|
||||
#define ALGO_RSA_DECRYPT_OEP 0x23
|
||||
#define ALGO_RSA_PKCS1 0x30 /* RSA signature with DigestInfo input and PKCS#1 V1.5 padding */
|
||||
#define ALGO_RSA_PKCS1_SHA1 0x31 /* RSA signature with SHA-1 hash and PKCS#1 V1.5 padding */
|
||||
#define ALGO_RSA_PKCS1_SHA224 0x32
|
||||
#define ALGO_RSA_PKCS1_SHA256 0x33 /* RSA signature with SHA-256 hash and PKCS#1 V1.5 padding */
|
||||
#define ALGO_RSA_PKCS1_SHA384 0x34
|
||||
#define ALGO_RSA_PKCS1_SHA512 0x35
|
||||
|
||||
#define HSM_OK 0
|
||||
#define HSM_ERR_NO_MEMORY -1000
|
||||
#define HSM_ERR_MEMORY_FATAL -1001
|
||||
#define HSM_ERR_NULL_PARAM -1002
|
||||
#define HSM_ERR_FILE_NOT_FOUND -1003
|
||||
#define HSM_ERR_BLOCKED -1004
|
||||
#define HSM_NO_LOGIN -1005
|
||||
#define HSM_EXEC_ERROR -1006
|
||||
#define ALGO_RSA_PSS 0x40 /* RSA signature with external hash and PKCS#1 PSS padding*/
|
||||
#define ALGO_RSA_PSS_SHA1 0x41 /* RSA signature with SHA-1 hash and PKCS#1 PSS padding */
|
||||
#define ALGO_RSA_PSS_SHA224 0x42
|
||||
#define ALGO_RSA_PSS_SHA256 0x43 /* RSA signature with SHA-256 hash and PKCS#1 PSS padding */
|
||||
#define ALGO_RSA_PSS_SHA384 0x44
|
||||
#define ALGO_RSA_PSS_SHA512 0x45
|
||||
|
||||
#define ALGO_RSA_RAW 0x20 /* RSA signature with external padding */
|
||||
#define ALGO_RSA_DECRYPT 0x21 /* RSA decrypt */
|
||||
#define ALGO_RSA_PKCS1 0x30 /* RSA signature with DigestInfo input and PKCS#1 V1.5 padding */
|
||||
#define ALGO_RSA_PKCS1_SHA1 0x31 /* RSA signature with SHA-1 hash and PKCS#1 V1.5 padding */
|
||||
#define ALGO_RSA_PKCS1_SHA256 0x33 /* RSA signature with SHA-256 hash and PKCS#1 V1.5 padding */
|
||||
#define ALGO_EC_RAW 0x70 /* ECDSA signature with hash input */
|
||||
#define ALGO_EC_SHA1 0x71 /* ECDSA signature with SHA-1 hash */
|
||||
#define ALGO_EC_SHA224 0x72 /* ECDSA signature with SHA-224 hash */
|
||||
#define ALGO_EC_SHA256 0x73 /* ECDSA signature with SHA-256 hash */
|
||||
#define ALGO_EC_SHA384 0x74
|
||||
#define ALGO_EC_SHA512 0x75
|
||||
#define ALGO_EC_DH 0x80 /* ECDH key derivation */
|
||||
#define ALGO_EC_DH_AUTPUK 0x83
|
||||
#define ALGO_EC_DH_XKEK 0x84
|
||||
#define ALGO_HD 0xA0
|
||||
|
||||
#define ALGO_RSA_PSS 0x40 /* RSA signature with external hash and PKCS#1 PSS padding*/
|
||||
#define ALGO_RSA_PSS_SHA1 0x41 /* RSA signature with SHA-1 hash and PKCS#1 PSS padding */
|
||||
#define ALGO_RSA_PSS_SHA256 0x43 /* RSA signature with SHA-256 hash and PKCS#1 PSS padding */
|
||||
#define ALGO_WRAP 0x92
|
||||
#define ALGO_UNWRAP 0x93
|
||||
#define ALGO_REPLACE 0x94
|
||||
|
||||
#define ALGO_EC_RAW 0x70 /* ECDSA signature with hash input */
|
||||
#define ALGO_EC_SHA1 0x71 /* ECDSA signature with SHA-1 hash */
|
||||
#define ALGO_EC_SHA224 0x72 /* ECDSA signature with SHA-224 hash */
|
||||
#define ALGO_EC_SHA256 0x73 /* ECDSA signature with SHA-256 hash */
|
||||
#define ALGO_EC_DH 0x80 /* ECDH key derivation */
|
||||
#define ALGO_EC_DERIVE 0x98 /* Derive EC key from EC key */
|
||||
|
||||
#define ALGO_EC_DERIVE 0x98 /* Derive EC key from EC key */
|
||||
#define ALGO_AES_CBC_ENCRYPT 0x10
|
||||
#define ALGO_AES_CBC_DECRYPT 0x11
|
||||
#define ALGO_AES_CMAC 0x18
|
||||
#define ALGO_EXT_CIPHER_ENCRYPT 0x51 /* Extended ciphering Encrypt */
|
||||
#define ALGO_EXT_CIPHER_DECRYPT 0x52 /* Extended ciphering Decrypt */
|
||||
#define ALGO_AES_DERIVE 0x99
|
||||
|
||||
#define ALGO_AES_CBC_ENCRYPT 0x10
|
||||
#define ALGO_AES_CBC_DECRYPT 0x11
|
||||
#define ALGO_AES_CMAC 0x18
|
||||
#define ALGO_AES_DERIVE 0x99
|
||||
#define HSM_OPT_RRC 0x0001
|
||||
#define HSM_OPT_TRANSPORT_PIN 0x0002
|
||||
#define HSM_OPT_SESSION_PIN 0x0004
|
||||
#define HSM_OPT_SESSION_PIN_EXPL 0x000C
|
||||
#define HSM_OPT_REPLACE_PKA 0x0008
|
||||
#define HSM_OPT_COMBINED_AUTH 0x0010
|
||||
#define HSM_OPT_RRC_RESET_ONLY 0x0020
|
||||
#define HSM_OPT_BOOTSEL_BUTTON 0x0100
|
||||
#define HSM_OPT_KEY_COUNTER_ALL 0x0200
|
||||
#define HSM_OPT_SECURE_LOCK 0x0400
|
||||
|
||||
#define PRKD_PREFIX 0xC4 /* Hi byte in file identifier for PKCS#15 PRKD objects */
|
||||
#define CD_PREFIX 0xC8 /* Hi byte in file identifier for PKCS#15 CD objects */
|
||||
#define DCOD_PREFIX 0xC9 /* Hi byte in file identifier for PKCS#15 DCOD objects */
|
||||
#define CA_CERTIFICATE_PREFIX 0xCA /* Hi byte in file identifier for CA certificates */
|
||||
#define KEY_PREFIX 0xCC /* Hi byte in file identifier for key objects */
|
||||
#define PROT_DATA_PREFIX 0xCD /* Hi byte in file identifier for PIN protected data objects */
|
||||
#define EE_CERTIFICATE_PREFIX 0xCE /* Hi byte in file identifier for EE certificates */
|
||||
#define DATA_PREFIX 0xCF /* Hi byte in file identifier for readable data objects */
|
||||
|
||||
#define P15_KEYTYPE_RSA 0x30
|
||||
#define P15_KEYTYPE_ECC 0xA0
|
||||
#define P15_KEYTYPE_AES 0xA8
|
||||
|
||||
#define MAX_PUK 8
|
||||
|
||||
extern int pin_reset_retries(const file_t *pin, bool);
|
||||
extern int pin_wrong_retry(const file_t *pin);
|
||||
|
||||
extern void hash(const uint8_t *input, size_t len, uint8_t output[32]);
|
||||
extern void hash_multi(const uint8_t *input, size_t len, uint8_t output[32]);
|
||||
extern void double_hash_pin(const uint8_t *pin, size_t len, uint8_t output[32]);
|
||||
|
||||
extern void hash(const uint8_t *input, uint16_t len, uint8_t output[32]);
|
||||
extern uint16_t get_device_options();
|
||||
extern bool has_session_pin, has_session_sopin;
|
||||
extern uint8_t session_pin[32], session_sopin[32];
|
||||
extern uint16_t check_pin(const file_t *pin, const uint8_t *data, uint16_t len);
|
||||
extern bool pka_enabled();
|
||||
extern const uint8_t *dev_name;
|
||||
extern uint16_t dev_name_len;
|
||||
extern uint8_t puk_status[MAX_PUK];
|
||||
extern int puk_store_select_chr(const uint8_t *chr);
|
||||
extern int delete_file(file_t *ef);
|
||||
extern const uint8_t *get_meta_tag(file_t *ef, uint16_t meta_tag, uint16_t *tag_len);
|
||||
extern bool key_has_purpose(file_t *ef, uint8_t purpose);
|
||||
extern int load_private_key_rsa(mbedtls_rsa_context *ctx, file_t *fkey);
|
||||
extern int load_private_key_ec(mbedtls_ecp_keypair *ctx, file_t *fkey);
|
||||
extern int load_private_key_ecdh(mbedtls_ecp_keypair *ctx, file_t *fkey);
|
||||
extern bool wait_button_pressed();
|
||||
extern int store_keys(void *key_ctx, int type, uint8_t key_id);
|
||||
extern int find_and_store_meta_key(uint8_t key_id);
|
||||
extern uint32_t get_key_counter(file_t *fkey);
|
||||
extern uint32_t decrement_key_counter(file_t *fkey);
|
||||
|
||||
#define IV_SIZE 16
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -1,10 +1,26 @@
|
|||
/*
|
||||
* This file is part of the Pico HSM distribution (https://github.com/polhenarejos/pico-hsm).
|
||||
* Copyright (c) 2022 Pol Henarejos.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef __VERSION_H_
|
||||
#define __VERSION_H_
|
||||
|
||||
#define HSM_VERSION 0x0104
|
||||
#define HSM_VERSION 0x0602
|
||||
|
||||
#define HSM_VERSION_MAJOR ((HSM_VERSION >> 8) & 0xff)
|
||||
#define HSM_VERSION_MINOR (HSM_VERSION & 0xff)
|
||||
|
||||
#endif
|
||||
|
||||
|
|
|
|||
302
src/openpgp/ac.c
302
src/openpgp/ac.c
|
|
@ -1,302 +0,0 @@
|
|||
/*
|
||||
* ac.c -- Check access condition
|
||||
*
|
||||
* Copyright (C) 2010, 2012, 2013, 2017 Free Software Initiative of Japan
|
||||
* Author: NIIBE Yutaka <gniibe@fsij.org>
|
||||
*
|
||||
* This file is a part of Gnuk, a GnuPG USB Token implementation.
|
||||
*
|
||||
* Gnuk is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Gnuk is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
|
||||
* License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include "gnuk.h"
|
||||
#include "mbedtls/sha256.h"
|
||||
#include "random.h"
|
||||
#include "hsm2040.h"
|
||||
|
||||
uint8_t volatile auth_status; /* Initialized to AC_NONE_AUTHORIZED */
|
||||
|
||||
int
|
||||
ac_check_status (uint8_t ac_flag)
|
||||
{
|
||||
if (ac_flag == AC_ALWAYS)
|
||||
return 1;
|
||||
else if (ac_flag == AC_NEVER)
|
||||
return 0;
|
||||
else
|
||||
return (ac_flag & auth_status)? 1 : 0;
|
||||
}
|
||||
|
||||
void
|
||||
ac_reset_pso_cds (void)
|
||||
{
|
||||
gpg_do_clear_prvkey (GPG_KEY_FOR_SIGNING);
|
||||
auth_status &= ~AC_PSO_CDS_AUTHORIZED;
|
||||
}
|
||||
|
||||
void
|
||||
ac_reset_other (void)
|
||||
{
|
||||
gpg_do_clear_prvkey (GPG_KEY_FOR_DECRYPTION);
|
||||
gpg_do_clear_prvkey (GPG_KEY_FOR_AUTHENTICATION);
|
||||
auth_status &= ~AC_OTHER_AUTHORIZED;
|
||||
}
|
||||
|
||||
int
|
||||
verify_user_0 (uint8_t access, const uint8_t *pw, int buf_len, int pw_len_known,
|
||||
const uint8_t *ks_pw1, int save_ks)
|
||||
{
|
||||
int pw_len;
|
||||
int r;
|
||||
uint8_t keystring[KEYSTRING_MD_SIZE];
|
||||
const uint8_t *salt;
|
||||
int salt_len;
|
||||
|
||||
if (gpg_pw_locked (PW_ERR_PW1))
|
||||
return 0;
|
||||
|
||||
if (ks_pw1 == NULL)
|
||||
{
|
||||
const uint8_t *initial_pw;
|
||||
|
||||
salt = NULL;
|
||||
salt_len = 0;
|
||||
gpg_do_get_initial_pw_setting (0, &pw_len, &initial_pw);
|
||||
if ((pw_len_known >= 0 && pw_len_known != pw_len)
|
||||
|| buf_len < pw_len
|
||||
|| memcmp (pw, initial_pw, pw_len))
|
||||
goto failure;
|
||||
}
|
||||
else
|
||||
{
|
||||
pw_len = ks_pw1[0] & PW_LEN_MASK;
|
||||
salt = KS_GET_SALT (ks_pw1);
|
||||
salt_len = SALT_SIZE;
|
||||
|
||||
if ((pw_len_known >= 0 && pw_len_known != pw_len)
|
||||
|| buf_len < pw_len)
|
||||
goto failure;
|
||||
}
|
||||
|
||||
s2k (salt, salt_len, pw, pw_len, keystring);
|
||||
if (save_ks)
|
||||
memcpy (keystring_md_pw3, keystring, KEYSTRING_MD_SIZE);
|
||||
|
||||
if (access == AC_PSO_CDS_AUTHORIZED)
|
||||
r = gpg_do_load_prvkey (GPG_KEY_FOR_SIGNING, BY_USER, keystring);
|
||||
else
|
||||
{
|
||||
int r1, r2;
|
||||
|
||||
r1 = gpg_do_load_prvkey (GPG_KEY_FOR_DECRYPTION, BY_USER, keystring);
|
||||
r2 = gpg_do_load_prvkey (GPG_KEY_FOR_AUTHENTICATION, BY_USER, keystring);
|
||||
|
||||
if (r1 < 0 || r2 < 0)
|
||||
r = -1;
|
||||
else if (r1 == 0)
|
||||
{
|
||||
if (r2 == 0)
|
||||
/* No encryption/authentication keys, then, check signing key. */
|
||||
r = gpg_do_load_prvkey (GPG_KEY_FOR_SIGNING, BY_USER, keystring);
|
||||
else
|
||||
r = r2;
|
||||
}
|
||||
else if (r2 == 0)
|
||||
r = r1;
|
||||
else
|
||||
r = 1;
|
||||
}
|
||||
|
||||
if (r < 0)
|
||||
{
|
||||
failure:
|
||||
gpg_pw_increment_err_counter (PW_ERR_PW1);
|
||||
return -1;
|
||||
}
|
||||
|
||||
gpg_pw_reset_err_counter (PW_ERR_PW1);
|
||||
return pw_len;
|
||||
}
|
||||
|
||||
/*
|
||||
* Verify for "Perform Security Operation : Compute Digital Signature"
|
||||
*/
|
||||
int
|
||||
verify_pso_cds (const uint8_t *pw, int pw_len)
|
||||
{
|
||||
const uint8_t *ks_pw1 = gpg_do_read_simple (NR_DO_KEYSTRING_PW1);
|
||||
int r;
|
||||
|
||||
DEBUG_INFO ("verify_pso_cds\r\n");
|
||||
DEBUG_BYTE (pw_len);
|
||||
|
||||
r = verify_user_0 (AC_PSO_CDS_AUTHORIZED, pw, pw_len, pw_len, ks_pw1, 0);
|
||||
if (r > 0)
|
||||
auth_status |= AC_PSO_CDS_AUTHORIZED;
|
||||
return r;
|
||||
}
|
||||
|
||||
int
|
||||
verify_other (const uint8_t *pw, int pw_len)
|
||||
{
|
||||
const uint8_t *ks_pw1 = gpg_do_read_simple (NR_DO_KEYSTRING_PW1);
|
||||
int r;
|
||||
|
||||
DEBUG_INFO ("verify_other\r\n");
|
||||
DEBUG_BYTE (pw_len);
|
||||
|
||||
r = verify_user_0 (AC_OTHER_AUTHORIZED, pw, pw_len, pw_len, ks_pw1, 0);
|
||||
if (r > 0)
|
||||
auth_status |= AC_OTHER_AUTHORIZED;
|
||||
return r;
|
||||
}
|
||||
|
||||
|
||||
static int
|
||||
verify_admin_00 (const uint8_t *pw, int buf_len, int pw_len_known,
|
||||
const uint8_t *ks, int save_ks)
|
||||
{
|
||||
int pw_len;
|
||||
int r;
|
||||
uint8_t keystring[KEYSTRING_MD_SIZE];
|
||||
const uint8_t *salt;
|
||||
int salt_len;
|
||||
|
||||
pw_len = ks[0] & PW_LEN_MASK;
|
||||
salt = KS_GET_SALT (ks);
|
||||
salt_len = SALT_SIZE;
|
||||
|
||||
if ((pw_len_known >= 0 && pw_len_known != pw_len) || buf_len < pw_len)
|
||||
return -1;
|
||||
|
||||
s2k (salt, salt_len, pw, pw_len, keystring);
|
||||
if (save_ks)
|
||||
memcpy (keystring_md_pw3, keystring, KEYSTRING_MD_SIZE);
|
||||
|
||||
r = gpg_do_load_prvkey (GPG_KEY_FOR_SIGNING, BY_ADMIN, keystring);
|
||||
|
||||
if (r < 0)
|
||||
return -1;
|
||||
else if (r == 0)
|
||||
if ((ks[0] & PW_LEN_KEYSTRING_BIT) == 0
|
||||
|| memcmp (KS_GET_KEYSTRING (ks), keystring, KEYSTRING_MD_SIZE) != 0)
|
||||
return -1;
|
||||
|
||||
return pw_len;
|
||||
}
|
||||
|
||||
uint8_t keystring_md_pw3[KEYSTRING_MD_SIZE];
|
||||
uint8_t admin_authorized;
|
||||
|
||||
int
|
||||
verify_admin_0 (const uint8_t *pw, int buf_len, int pw_len_known,
|
||||
const uint8_t *pw3_keystring, int save_ks)
|
||||
{
|
||||
int pw_len;
|
||||
|
||||
if (pw3_keystring != NULL)
|
||||
{
|
||||
if (gpg_pw_locked (PW_ERR_PW3))
|
||||
return 0;
|
||||
|
||||
pw_len = verify_admin_00 (pw, buf_len, pw_len_known, pw3_keystring,
|
||||
save_ks);
|
||||
if (pw_len < 0)
|
||||
{
|
||||
failure:
|
||||
gpg_pw_increment_err_counter (PW_ERR_PW3);
|
||||
return -1;
|
||||
}
|
||||
|
||||
admin_authorized = BY_ADMIN;
|
||||
success: /* OK, the admin is now authenticated. */
|
||||
gpg_pw_reset_err_counter (PW_ERR_PW3);
|
||||
return pw_len;
|
||||
}
|
||||
else
|
||||
{
|
||||
const uint8_t *initial_pw;
|
||||
const uint8_t *ks_pw1 = gpg_do_read_simple (NR_DO_KEYSTRING_PW1);
|
||||
|
||||
if (ks_pw1 != NULL)
|
||||
{ /* empty PW3, but PW1 exists */
|
||||
int r = verify_user_0 (AC_PSO_CDS_AUTHORIZED,
|
||||
pw, buf_len, pw_len_known, ks_pw1, save_ks);
|
||||
|
||||
if (r > 0)
|
||||
admin_authorized = BY_USER;
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
if (gpg_pw_locked (PW_ERR_PW3))
|
||||
return 0;
|
||||
|
||||
/*
|
||||
* For the case of empty PW3 (with empty PW1), passphrase is
|
||||
* OPENPGP_CARD_INITIAL_PW3, or defined by KDF DO.
|
||||
*/
|
||||
gpg_do_get_initial_pw_setting (1, &pw_len, &initial_pw);
|
||||
if ((pw_len_known >=0 && pw_len_known != pw_len)
|
||||
|| buf_len < pw_len
|
||||
|| memcmp (pw, initial_pw, pw_len))
|
||||
goto failure;
|
||||
|
||||
admin_authorized = BY_ADMIN;
|
||||
if (save_ks)
|
||||
s2k (NULL, 0, pw, pw_len, keystring_md_pw3);
|
||||
goto success;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
verify_admin (const uint8_t *pw, int pw_len)
|
||||
{
|
||||
int r;
|
||||
const uint8_t *pw3_keystring;
|
||||
|
||||
pw3_keystring = gpg_do_read_simple (NR_DO_KEYSTRING_PW3);
|
||||
r = verify_admin_0 (pw, pw_len, pw_len, pw3_keystring, 1);
|
||||
if (r <= 0)
|
||||
return r;
|
||||
|
||||
auth_status |= AC_ADMIN_AUTHORIZED;
|
||||
return 1;
|
||||
}
|
||||
|
||||
void
|
||||
ac_reset_admin (void)
|
||||
{
|
||||
memset (keystring_md_pw3, 0, KEYSTRING_MD_SIZE);
|
||||
auth_status &= ~AC_ADMIN_AUTHORIZED;
|
||||
admin_authorized = 0;
|
||||
}
|
||||
|
||||
void
|
||||
ac_fini (void)
|
||||
{
|
||||
memset (keystring_md_pw3, 0, KEYSTRING_MD_SIZE);
|
||||
gpg_do_clear_prvkey (GPG_KEY_FOR_SIGNING);
|
||||
gpg_do_clear_prvkey (GPG_KEY_FOR_DECRYPTION);
|
||||
gpg_do_clear_prvkey (GPG_KEY_FOR_AUTHENTICATION);
|
||||
auth_status = AC_NONE_AUTHORIZED;
|
||||
admin_authorized = 0;
|
||||
}
|
||||
|
|
@ -1,8 +0,0 @@
|
|||
/**
|
||||
* @brief Affine coordinates
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
bn256 x[1];
|
||||
bn256 y[1];
|
||||
} ac;
|
||||
427
src/openpgp/bn.c
427
src/openpgp/bn.c
|
|
@ -1,427 +0,0 @@
|
|||
/*
|
||||
* bn.c -- 256-bit (and 512-bit) bignum calculation
|
||||
*
|
||||
* Copyright (C) 2011, 2013, 2014, 2019
|
||||
* Free Software Initiative of Japan
|
||||
* Author: NIIBE Yutaka <gniibe@fsij.org>
|
||||
*
|
||||
* This file is a part of Gnuk, a GnuPG USB Token implementation.
|
||||
*
|
||||
* Gnuk is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Gnuk is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
|
||||
* License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#ifndef BN256_NO_RANDOM
|
||||
#include "random.h"
|
||||
#endif
|
||||
#include "bn.h"
|
||||
|
||||
uint32_t
|
||||
bn256_add (bn256 *X, const bn256 *A, const bn256 *B)
|
||||
{
|
||||
int i;
|
||||
uint32_t v;
|
||||
uint32_t carry = 0;
|
||||
uint32_t *px;
|
||||
const uint32_t *pa, *pb;
|
||||
|
||||
px = X->word;
|
||||
pa = A->word;
|
||||
pb = B->word;
|
||||
|
||||
for (i = 0; i < BN256_WORDS; i++)
|
||||
{
|
||||
v = *pb;
|
||||
*px = *pa + carry;
|
||||
carry = (*px < carry);
|
||||
*px += v;
|
||||
carry += (*px < v);
|
||||
px++;
|
||||
pa++;
|
||||
pb++;
|
||||
}
|
||||
|
||||
return carry;
|
||||
}
|
||||
|
||||
uint32_t
|
||||
bn256_sub (bn256 *X, const bn256 *A, const bn256 *B)
|
||||
{
|
||||
int i;
|
||||
uint32_t v;
|
||||
uint32_t borrow = 0;
|
||||
uint32_t *px;
|
||||
const uint32_t *pa, *pb;
|
||||
|
||||
px = X->word;
|
||||
pa = A->word;
|
||||
pb = B->word;
|
||||
|
||||
for (i = 0; i < BN256_WORDS; i++)
|
||||
{
|
||||
uint32_t borrow0 = (*pa < borrow);
|
||||
|
||||
v = *pb;
|
||||
*px = *pa - borrow;
|
||||
borrow = (*px < v) + borrow0;
|
||||
*px -= v;
|
||||
px++;
|
||||
pa++;
|
||||
pb++;
|
||||
}
|
||||
|
||||
return borrow;
|
||||
}
|
||||
|
||||
uint32_t
|
||||
bn256_add_uint (bn256 *X, const bn256 *A, uint32_t w)
|
||||
{
|
||||
int i;
|
||||
uint32_t carry = w;
|
||||
uint32_t *px;
|
||||
const uint32_t *pa;
|
||||
|
||||
px = X->word;
|
||||
pa = A->word;
|
||||
|
||||
for (i = 0; i < BN256_WORDS; i++)
|
||||
{
|
||||
*px = *pa + carry;
|
||||
carry = (*px < carry);
|
||||
px++;
|
||||
pa++;
|
||||
}
|
||||
|
||||
return carry;
|
||||
}
|
||||
|
||||
uint32_t
|
||||
bn256_sub_uint (bn256 *X, const bn256 *A, uint32_t w)
|
||||
{
|
||||
int i;
|
||||
uint32_t borrow = w;
|
||||
uint32_t *px;
|
||||
const uint32_t *pa;
|
||||
|
||||
px = X->word;
|
||||
pa = A->word;
|
||||
|
||||
for (i = 0; i < BN256_WORDS; i++)
|
||||
{
|
||||
uint32_t borrow0 = (*pa < borrow);
|
||||
|
||||
*px = *pa - borrow;
|
||||
borrow = borrow0;
|
||||
px++;
|
||||
pa++;
|
||||
}
|
||||
|
||||
return borrow;
|
||||
}
|
||||
|
||||
#ifndef BN256_C_IMPLEMENTATION
|
||||
#define ASM_IMPLEMENTATION 0
|
||||
#endif
|
||||
void
|
||||
bn256_mul (bn512 *X, const bn256 *A, const bn256 *B)
|
||||
{
|
||||
#if ASM_IMPLEMENTATION
|
||||
#include "muladd_256.h"
|
||||
const uint32_t *s;
|
||||
uint32_t *d;
|
||||
uint32_t w;
|
||||
uint32_t c;
|
||||
|
||||
memset (X->word, 0, sizeof (uint32_t)*BN256_WORDS*2);
|
||||
|
||||
s = A->word; d = &X->word[0]; w = B->word[0]; MULADD_256 (s, d, w, c);
|
||||
s = A->word; d = &X->word[1]; w = B->word[1]; MULADD_256 (s, d, w, c);
|
||||
s = A->word; d = &X->word[2]; w = B->word[2]; MULADD_256 (s, d, w, c);
|
||||
s = A->word; d = &X->word[3]; w = B->word[3]; MULADD_256 (s, d, w, c);
|
||||
s = A->word; d = &X->word[4]; w = B->word[4]; MULADD_256 (s, d, w, c);
|
||||
s = A->word; d = &X->word[5]; w = B->word[5]; MULADD_256 (s, d, w, c);
|
||||
s = A->word; d = &X->word[6]; w = B->word[6]; MULADD_256 (s, d, w, c);
|
||||
s = A->word; d = &X->word[7]; w = B->word[7]; MULADD_256 (s, d, w, c);
|
||||
#else
|
||||
int i, j, k;
|
||||
int i_beg, i_end;
|
||||
uint32_t r0, r1, r2;
|
||||
|
||||
r0 = r1 = r2 = 0;
|
||||
for (k = 0; k <= (BN256_WORDS - 1)*2; k++)
|
||||
{
|
||||
if (k < BN256_WORDS)
|
||||
{
|
||||
i_beg = 0;
|
||||
i_end = k;
|
||||
}
|
||||
else
|
||||
{
|
||||
i_beg = k - BN256_WORDS + 1;
|
||||
i_end = BN256_WORDS - 1;
|
||||
}
|
||||
|
||||
for (i = i_beg; i <= i_end; i++)
|
||||
{
|
||||
uint64_t uv;
|
||||
uint32_t u, v;
|
||||
uint32_t carry;
|
||||
|
||||
j = k - i;
|
||||
|
||||
uv = ((uint64_t )A->word[i])*((uint64_t )B->word[j]);
|
||||
v = uv;
|
||||
u = (uv >> 32);
|
||||
r0 += v;
|
||||
carry = (r0 < v);
|
||||
r1 += carry;
|
||||
carry = (r1 < carry);
|
||||
r1 += u;
|
||||
carry += (r1 < u);
|
||||
r2 += carry;
|
||||
}
|
||||
|
||||
X->word[k] = r0;
|
||||
r0 = r1;
|
||||
r1 = r2;
|
||||
r2 = 0;
|
||||
}
|
||||
|
||||
X->word[k] = r0;
|
||||
#endif
|
||||
}
|
||||
|
||||
void
|
||||
bn256_sqr (bn512 *X, const bn256 *A)
|
||||
{
|
||||
#if ASM_IMPLEMENTATION
|
||||
int i;
|
||||
|
||||
memset (X->word, 0, sizeof (bn512));
|
||||
for (i = 0; i < BN256_WORDS; i++)
|
||||
{
|
||||
uint32_t *wij = &X->word[i*2];
|
||||
const uint32_t *xj = &A->word[i];
|
||||
uint32_t x_i = *xj++;
|
||||
uint32_t c;
|
||||
|
||||
asm (/* (C,R4,R5) := w_i_i + x_i*x_i; w_i_i := R5; */
|
||||
"mov %[c], #0\n\t"
|
||||
"ldr r5, [%[wij]]\n\t" /* R5 := w_i_i; */
|
||||
"mov r4, %[c]\n\t"
|
||||
"umlal r5, r4, %[x_i], %[x_i]\n\t"
|
||||
"str r5, [%[wij]], #4\n\t"
|
||||
"cmp %[xj], %[x_max1]\n\t"
|
||||
"bhi 0f\n\t"
|
||||
"mov r9, %[c]\n\t" /* R9 := 0, the constant ZERO from here. */
|
||||
"beq 1f\n"
|
||||
"2:\n\t"
|
||||
"ldmia %[xj]!, { r7, r8 }\n\t"
|
||||
"ldmia %[wij], { r5, r6 }\n\t"
|
||||
/* (C,R4,R5) := (C,R4) + w_i_j + 2*x_i*x_j; */
|
||||
"umull r7, r12, %[x_i], r7\n\t"
|
||||
"adds r5, r5, r4\n\t"
|
||||
"adc r4, %[c], r9\n\t"
|
||||
"adds r5, r5, r7\n\t"
|
||||
"adcs r4, r4, r12\n\t"
|
||||
"adc %[c], r9, r9\n\t"
|
||||
"adds r5, r5, r7\n\t"
|
||||
"adcs r4, r4, r12\n\t"
|
||||
"adc %[c], %[c], r9\n\t"
|
||||
/* (C,R4,R6) := (C,R4) + w_i_j + 2*x_i*x_j; */
|
||||
"adds r6, r6, r4\n\t"
|
||||
"adc r4, %[c], r9\n\t"
|
||||
"umull r7, r12, %[x_i], r8\n\t"
|
||||
"adds r6, r6, r7\n\t"
|
||||
"adcs r4, r4, r12\n\t"
|
||||
"adc %[c], r9, r9\n\t"
|
||||
"adds r6, r6, r7\n\t"
|
||||
"adcs r4, r4, r12\n\t"
|
||||
"adc %[c], %[c], r9\n\t"
|
||||
/**/
|
||||
"stmia %[wij]!, { r5, r6 }\n\t"
|
||||
"cmp %[xj], %[x_max1]\n\t"
|
||||
"bcc 2b\n\t"
|
||||
"bne 0f\n"
|
||||
"1:\n\t"
|
||||
/* (C,R4,R5) := (C,R4) + w_i_j + 2*x_i*x_j; */
|
||||
"ldr r5, [%[wij]]\n\t"
|
||||
"ldr r6, [%[xj]], #4\n\t"
|
||||
"adds r5, r5, r4\n\t"
|
||||
"adc r4, %[c], r9\n\t"
|
||||
"umull r7, r12, %[x_i], r6\n\t"
|
||||
"adds r5, r5, r7\n\t"
|
||||
"adcs r4, r4, r12\n\t"
|
||||
"adc %[c], r9, r9\n\t"
|
||||
"adds r5, r5, r7\n\t"
|
||||
"adcs r4, r4, r12\n\t"
|
||||
"adc %[c], %[c], r9\n\t"
|
||||
"str r5, [%[wij]], #4\n"
|
||||
"0:\n\t"
|
||||
"ldr r5, [%[wij]]\n\t"
|
||||
"adds r4, r4, r5\n\t"
|
||||
"adc %[c], %[c], #0\n\t"
|
||||
"str r4, [%[wij]], #4"
|
||||
: [c] "=&r" (c), [wij] "=r" (wij), [xj] "=r" (xj)
|
||||
: [x_i] "r" (x_i), [x_max1] "r" (&A->word[BN256_WORDS-1]),
|
||||
"[wij]" (wij), "[xj]" (xj)
|
||||
: "r4", "r5", "r6", "r7", "r8", "r9", "r12", "memory", "cc");
|
||||
|
||||
if (i < BN256_WORDS - 1)
|
||||
*wij = c;
|
||||
}
|
||||
#else
|
||||
int i, j, k;
|
||||
int i_beg, i_end;
|
||||
uint32_t r0, r1, r2;
|
||||
|
||||
r0 = r1 = r2 = 0;
|
||||
for (k = 0; k <= (BN256_WORDS - 1)*2; k++)
|
||||
{
|
||||
if (k < BN256_WORDS)
|
||||
{
|
||||
i_beg = 0;
|
||||
i_end = k/2;
|
||||
}
|
||||
else
|
||||
{
|
||||
i_beg = k - BN256_WORDS + 1;
|
||||
i_end = k/2;
|
||||
}
|
||||
|
||||
for (i = i_beg; i <= i_end; i++)
|
||||
{
|
||||
uint64_t uv;
|
||||
uint32_t u, v;
|
||||
uint32_t carry;
|
||||
|
||||
j = k - i;
|
||||
|
||||
uv = ((uint64_t )A->word[i])*((uint64_t )A->word[j]);
|
||||
if (i < j)
|
||||
{
|
||||
r2 += ((uv >> 63) != 0);
|
||||
uv <<= 1;
|
||||
}
|
||||
v = uv;
|
||||
u = (uv >> 32);
|
||||
r0 += v;
|
||||
carry = (r0 < v);
|
||||
r1 += carry;
|
||||
carry = (r1 < carry);
|
||||
r1 += u;
|
||||
carry += (r1 < u);
|
||||
r2 += carry;
|
||||
}
|
||||
|
||||
X->word[k] = r0;
|
||||
r0 = r1;
|
||||
r1 = r2;
|
||||
r2 = 0;
|
||||
}
|
||||
|
||||
X->word[k] = r0;
|
||||
#endif
|
||||
}
|
||||
|
||||
uint32_t
|
||||
bn256_shift (bn256 *X, const bn256 *A, int shift)
|
||||
{
|
||||
int i;
|
||||
uint32_t carry = 0, next_carry;
|
||||
|
||||
if (shift > 0)
|
||||
{
|
||||
for (i = 0; i < BN256_WORDS; i++)
|
||||
{
|
||||
next_carry = A->word[i] >> (32 - shift);
|
||||
X->word[i] = (A->word[i] << shift) | carry;
|
||||
carry = next_carry;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
shift = -shift;
|
||||
|
||||
for (i = BN256_WORDS - 1; i >= 0; i--)
|
||||
{
|
||||
next_carry = A->word[i] & ((1 << shift) - 1);
|
||||
X->word[i] = (A->word[i] >> shift) | (carry << (32 - shift));
|
||||
carry = next_carry;
|
||||
}
|
||||
}
|
||||
|
||||
return carry;
|
||||
}
|
||||
|
||||
int
|
||||
bn256_is_zero (const bn256 *X)
|
||||
{
|
||||
int i;
|
||||
int r = 1;
|
||||
|
||||
for (i = 0; i < BN256_WORDS; i++)
|
||||
r &= (X->word[i] == 0);
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
int
|
||||
bn256_is_even (const bn256 *X)
|
||||
{
|
||||
return !(X->word[0] & 1);
|
||||
}
|
||||
|
||||
int
|
||||
bn256_is_ge (const bn256 *A, const bn256 *B)
|
||||
{
|
||||
uint32_t borrow;
|
||||
bn256 tmp[1];
|
||||
|
||||
borrow = bn256_sub (tmp, A, B);
|
||||
return borrow == 0;
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
bn256_cmp (const bn256 *A, const bn256 *B)
|
||||
{
|
||||
uint32_t borrow;
|
||||
int is_zero;
|
||||
bn256 tmp[1];
|
||||
|
||||
borrow = bn256_sub (tmp, A, B);
|
||||
is_zero = bn256_is_zero (tmp);
|
||||
return is_zero ? 0 : (borrow ? -1 : 1);
|
||||
}
|
||||
|
||||
|
||||
#ifndef BN256_NO_RANDOM
|
||||
void
|
||||
bn256_random (bn256 *X)
|
||||
{
|
||||
int i, j;
|
||||
const uint8_t *rand;
|
||||
|
||||
for (i = 0; i < 256/256; i++)
|
||||
{
|
||||
rand = random_bytes_get (32);
|
||||
for (j = 0; j < BN256_WORDS; j++)
|
||||
X->word[i*BN256_WORDS+j] = ((uint32_t *)rand)[j];
|
||||
random_bytes_free (rand);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
|
@ -1,23 +0,0 @@
|
|||
#define BN256_WORDS 8
|
||||
typedef struct bn256 {
|
||||
uint32_t word[ BN256_WORDS ]; /* Little endian */
|
||||
} bn256;
|
||||
|
||||
#define BN512_WORDS 16
|
||||
typedef struct bn512 {
|
||||
uint32_t word[ BN512_WORDS ]; /* Little endian */
|
||||
} bn512;
|
||||
|
||||
uint32_t bn256_add (bn256 *X, const bn256 *A, const bn256 *B);
|
||||
uint32_t bn256_sub (bn256 *X, const bn256 *A, const bn256 *B);
|
||||
uint32_t bn256_add_uint (bn256 *X, const bn256 *A, uint32_t w);
|
||||
uint32_t bn256_sub_uint (bn256 *X, const bn256 *A, uint32_t w);
|
||||
|
||||
void bn256_mul (bn512 *X, const bn256 *A, const bn256 *B);
|
||||
void bn256_sqr (bn512 *X, const bn256 *A);
|
||||
uint32_t bn256_shift (bn256 *X, const bn256 *A, int shift);
|
||||
int bn256_is_zero (const bn256 *X);
|
||||
int bn256_is_even (const bn256 *X);
|
||||
int bn256_is_ge (const bn256 *A, const bn256 *B);
|
||||
int bn256_cmp (const bn256 *A, const bn256 *B);
|
||||
void bn256_random (bn256 *X);
|
||||
|
|
@ -1,136 +0,0 @@
|
|||
/*
|
||||
* call-ec.c - interface between Gnuk and Elliptic curve over GF(prime)
|
||||
*
|
||||
* Copyright (C) 2013, 2014, 2017 Free Software Initiative of Japan
|
||||
* Author: NIIBE Yutaka <gniibe@fsij.org>
|
||||
*
|
||||
* This file is a part of Gnuk, a GnuPG USB Token implementation.
|
||||
*
|
||||
* Gnuk is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Gnuk is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
|
||||
* License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "field-group-select.h"
|
||||
|
||||
/* We are little-endian in the computation, but the protocol is big-endian. */
|
||||
|
||||
#define ECDSA_BYTE_SIZE 32
|
||||
#define ECDH_BYTE_SIZE 32
|
||||
|
||||
int
|
||||
FUNC(ecdsa_sign) (const uint8_t *hash, uint8_t *output,
|
||||
const uint8_t *key_data)
|
||||
{
|
||||
int i;
|
||||
bn256 r[1], s[1], z[1], d[1];
|
||||
uint8_t *p;
|
||||
|
||||
p = (uint8_t *)d;
|
||||
for (i = 0; i < ECDSA_BYTE_SIZE; i++)
|
||||
p[ECDSA_BYTE_SIZE - i - 1] = key_data[i];
|
||||
|
||||
p = (uint8_t *)z;
|
||||
for (i = 0; i < ECDSA_BYTE_SIZE; i++)
|
||||
p[ECDSA_BYTE_SIZE - i - 1] = hash[i];
|
||||
|
||||
FUNC(ecdsa) (r, s, z, d);
|
||||
p = (uint8_t *)r;
|
||||
for (i = 0; i < ECDSA_BYTE_SIZE; i++)
|
||||
*output++ = p[ECDSA_BYTE_SIZE - i - 1];
|
||||
p = (uint8_t *)s;
|
||||
for (i = 0; i < ECDSA_BYTE_SIZE; i++)
|
||||
*output++ = p[ECDSA_BYTE_SIZE - i - 1];
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
FUNC(ecc_compute_public) (const uint8_t *key_data, uint8_t *pubkey)
|
||||
{
|
||||
uint8_t *p, *p1;
|
||||
ac q[1];
|
||||
bn256 k[1];
|
||||
int i;
|
||||
|
||||
p = (uint8_t *)k;
|
||||
for (i = 0; i < ECDSA_BYTE_SIZE; i++)
|
||||
p[ECDSA_BYTE_SIZE - i - 1] = key_data[i];
|
||||
if (FUNC(compute_kG) (q, k) < 0)
|
||||
return -1;
|
||||
|
||||
p = pubkey;
|
||||
p1 = (uint8_t *)q->x;
|
||||
for (i = 0; i < ECDSA_BYTE_SIZE; i++)
|
||||
*p++ = p1[ECDSA_BYTE_SIZE - i - 1];
|
||||
p1 = (uint8_t *)q->y;
|
||||
for (i = 0; i < ECDSA_BYTE_SIZE; i++)
|
||||
*p++ = p1[ECDSA_BYTE_SIZE - i - 1];
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
FUNC(ecdh_decrypt) (const uint8_t *input, uint8_t *output,
|
||||
const uint8_t *key_data)
|
||||
{
|
||||
bn256 k[1];
|
||||
ac X[1], P[1];
|
||||
int i;
|
||||
uint8_t *p0;
|
||||
const uint8_t *p1;
|
||||
int r;
|
||||
|
||||
p0 = (uint8_t *)k;
|
||||
for (i = 0; i < ECDH_BYTE_SIZE; i++)
|
||||
p0[ECDH_BYTE_SIZE - i - 1] = key_data[i];
|
||||
|
||||
p1 = input+1; /* skip '04' */
|
||||
p0 = (uint8_t *)P->x;
|
||||
for (i = 0; i < ECDH_BYTE_SIZE; i++)
|
||||
p0[ECDH_BYTE_SIZE - i - 1] = *p1++;
|
||||
p0 = (uint8_t *)P->y;
|
||||
for (i = 0; i < ECDH_BYTE_SIZE; i++)
|
||||
p0[ECDH_BYTE_SIZE - i - 1] = *p1++;
|
||||
|
||||
r = FUNC(compute_kP) (X, k, P);
|
||||
if (r == 0)
|
||||
{
|
||||
p0 = output;
|
||||
p1 = (const uint8_t *)X->x;
|
||||
*p0++ = 4;
|
||||
for (i = 0; i < ECDH_BYTE_SIZE; i++)
|
||||
*p0++ = p1[ECDH_BYTE_SIZE - i - 1];
|
||||
p1 = (const uint8_t *)X->y;
|
||||
for (i = 0; i < ECDH_BYTE_SIZE; i++)
|
||||
*p0++ = p1[ECDH_BYTE_SIZE - i - 1];
|
||||
}
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Check if a secret d0 is valid or not
|
||||
*
|
||||
* @param D0 scalar D0: secret
|
||||
* @param D1 scalar D1: secret candidate N-D0
|
||||
*
|
||||
* Return 0 on error.
|
||||
* Return -1 when D1 should be used as the secret
|
||||
* Return 1 when D0 should be used as the secret
|
||||
*/
|
||||
int
|
||||
FUNC(ecc_check_secret) (const uint8_t *d0, uint8_t *d1)
|
||||
{
|
||||
return FUNC(check_secret) ((const bn256 *)d0, (bn256 *)d1);
|
||||
}
|
||||
|
|
@ -1,34 +0,0 @@
|
|||
/*
|
||||
* call-ec_p256k1.c - interface between Gnuk and Elliptic curve over
|
||||
* GF(p256k1)
|
||||
*
|
||||
* Copyright (C) 2014, 2017 Free Software Initiative of Japan
|
||||
* Author: NIIBE Yutaka <gniibe@fsij.org>
|
||||
*
|
||||
* This file is a part of Gnuk, a GnuPG USB Token implementation.
|
||||
*
|
||||
* Gnuk is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Gnuk is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
|
||||
* License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include "bn.h"
|
||||
#include "affine.h"
|
||||
#include "jpc-ac_p256k1.h"
|
||||
#include "ec_p256k1.h"
|
||||
|
||||
#define FIELD p256k1
|
||||
|
||||
#include "call-ec.c"
|
||||
|
|
@ -1,291 +0,0 @@
|
|||
/*
|
||||
* call-rsa.c -- Glue code between RSA computation and OpenPGP card protocol
|
||||
*
|
||||
* Copyright (C) 2010, 2011, 2012, 2013, 2014, 2015, 2017
|
||||
* Free Software Initiative of Japan
|
||||
* Author: NIIBE Yutaka <gniibe@fsij.org>
|
||||
*
|
||||
* This file is a part of Gnuk, a GnuPG USB Token implementation.
|
||||
*
|
||||
* Gnuk is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Gnuk is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
|
||||
* License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "common.h"
|
||||
|
||||
//#include <chopstx.h>
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include "gnuk.h"
|
||||
#include "status-code.h"
|
||||
#include "random.h"
|
||||
//#include "polarssl/config.h"
|
||||
#include "mbedtls/rsa.h"
|
||||
#include "hsm2040.h"
|
||||
|
||||
static mbedtls_rsa_context rsa_ctx;
|
||||
//static struct chx_cleanup clp;
|
||||
|
||||
static void
|
||||
rsa_cleanup (void *arg)
|
||||
{
|
||||
(void)arg;
|
||||
mbedtls_rsa_free (&rsa_ctx);
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
rsa_sign (const uint8_t *raw_message, uint8_t *output, int msg_len,
|
||||
struct key_data *kd, int pubkey_len)
|
||||
{
|
||||
mbedtls_mpi P1, Q1, H;
|
||||
int ret = 0;
|
||||
unsigned char temp[pubkey_len];
|
||||
|
||||
mbedtls_rsa_init (&rsa_ctx);
|
||||
|
||||
mbedtls_mpi_init (&P1); mbedtls_mpi_init (&Q1); mbedtls_mpi_init (&H);
|
||||
|
||||
rsa_ctx.len = pubkey_len;
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_lset (&rsa_ctx.E, 0x10001) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary (&rsa_ctx.P, &kd->data[0], pubkey_len / 2) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary (&rsa_ctx.Q, &kd->data[pubkey_len / 2],
|
||||
pubkey_len / 2) );
|
||||
#if 0
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi (&rsa_ctx.N, &rsa_ctx.P, &rsa_ctx.Q) );
|
||||
#endif
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int (&P1, &rsa_ctx.P, 1) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int (&Q1, &rsa_ctx.Q, 1) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi (&H, &P1, &Q1) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod (&rsa_ctx.D , &rsa_ctx.E, &H) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi (&rsa_ctx.DP, &rsa_ctx.D, &P1) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi (&rsa_ctx.DQ, &rsa_ctx.D, &Q1) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod (&rsa_ctx.QP, &rsa_ctx.Q, &rsa_ctx.P) );
|
||||
cleanup:
|
||||
mbedtls_mpi_free (&P1); mbedtls_mpi_free (&Q1); mbedtls_mpi_free (&H);
|
||||
if (ret == 0)
|
||||
{
|
||||
int cs;
|
||||
|
||||
DEBUG_INFO ("RSA sign...");
|
||||
//clp.next = NULL;
|
||||
//clp.routine = rsa_cleanup;
|
||||
//clp.arg = NULL;
|
||||
//chopstx_cleanup_push (&clp);
|
||||
//cs = chopstx_setcancelstate (0); /* Allow cancellation. */
|
||||
ret = mbedtls_rsa_rsassa_pkcs1_v15_sign (&rsa_ctx, NULL, NULL,
|
||||
MBEDTLS_MD_NONE,
|
||||
msg_len, raw_message, temp);
|
||||
memcpy (output, temp, pubkey_len);
|
||||
rsa_cleanup(NULL);
|
||||
//chopstx_setcancelstate (cs);
|
||||
//chopstx_cleanup_pop (0);
|
||||
}
|
||||
|
||||
mbedtls_rsa_free (&rsa_ctx);
|
||||
if (ret != 0)
|
||||
{
|
||||
DEBUG_INFO ("fail:");
|
||||
DEBUG_SHORT (ret);
|
||||
return -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
DEBUG_INFO ("done.\r\n");
|
||||
GPG_SUCCESS ();
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* LEN: length in byte
|
||||
*/
|
||||
int
|
||||
modulus_calc (const uint8_t *p, int len, uint8_t *pubkey)
|
||||
{
|
||||
mbedtls_mpi P, Q, N;
|
||||
int ret;
|
||||
|
||||
mbedtls_mpi_init (&P); mbedtls_mpi_init (&Q); mbedtls_mpi_init (&N);
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary (&P, p, len / 2) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary (&Q, p + len / 2, len / 2) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi (&N, &P, &Q) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary (&N, pubkey, len) );
|
||||
cleanup:
|
||||
mbedtls_mpi_free (&P); mbedtls_mpi_free (&Q); mbedtls_mpi_free (&N);
|
||||
if (ret != 0)
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
rsa_decrypt (const uint8_t *input, uint8_t *output, int msg_len,
|
||||
struct key_data *kd, unsigned int *output_len_p)
|
||||
{
|
||||
mbedtls_mpi P1, Q1, H;
|
||||
int ret;
|
||||
|
||||
DEBUG_INFO ("RSA decrypt:");
|
||||
DEBUG_WORD ((uint32_t)&ret);
|
||||
|
||||
mbedtls_rsa_init (&rsa_ctx);
|
||||
mbedtls_mpi_init (&P1); mbedtls_mpi_init (&Q1); mbedtls_mpi_init (&H);
|
||||
|
||||
rsa_ctx.len = msg_len;
|
||||
DEBUG_WORD (msg_len);
|
||||
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_lset (&rsa_ctx.E, 0x10001) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary (&rsa_ctx.P, &kd->data[0], msg_len / 2) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary (&rsa_ctx.Q, &kd->data[msg_len / 2], msg_len / 2) );
|
||||
#if 0
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi (&rsa_ctx.N, &rsa_ctx.P, &rsa_ctx.Q) );
|
||||
#endif
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int (&P1, &rsa_ctx.P, 1) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int (&Q1, &rsa_ctx.Q, 1) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi (&H, &P1, &Q1) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod (&rsa_ctx.D , &rsa_ctx.E, &H) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi (&rsa_ctx.DP, &rsa_ctx.D, &P1) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi (&rsa_ctx.DQ, &rsa_ctx.D, &Q1) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod (&rsa_ctx.QP, &rsa_ctx.Q, &rsa_ctx.P) );
|
||||
cleanup:
|
||||
mbedtls_mpi_free (&P1); mbedtls_mpi_free (&Q1); mbedtls_mpi_free (&H);
|
||||
if (ret == 0)
|
||||
{
|
||||
int cs;
|
||||
|
||||
DEBUG_INFO ("RSA decrypt ...");
|
||||
//clp.next = NULL;
|
||||
//clp.routine = rsa_cleanup;
|
||||
//clp.arg = NULL;
|
||||
//chopstx_cleanup_push (&clp);
|
||||
//cs = chopstx_setcancelstate (0); /* Allow cancellation. */
|
||||
ret = mbedtls_rsa_rsaes_pkcs1_v15_decrypt (&rsa_ctx, NULL, NULL,
|
||||
output_len_p, input,
|
||||
output, MAX_RES_APDU_DATA_SIZE);
|
||||
rsa_cleanup(NULL);
|
||||
//chopstx_setcancelstate (cs);
|
||||
//chopstx_cleanup_pop (0);
|
||||
}
|
||||
|
||||
mbedtls_rsa_free (&rsa_ctx);
|
||||
if (ret != 0)
|
||||
{
|
||||
DEBUG_INFO ("fail:");
|
||||
DEBUG_SHORT (ret);
|
||||
return -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
DEBUG_INFO ("done.\r\n");
|
||||
GPG_SUCCESS ();
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
rsa_verify (const uint8_t *pubkey, int pubkey_len,
|
||||
const uint8_t *hash, const uint8_t *sig)
|
||||
{
|
||||
int ret;
|
||||
|
||||
mbedtls_rsa_init (&rsa_ctx);
|
||||
rsa_ctx.len = pubkey_len;
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_lset (&rsa_ctx.E, 0x10001) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary (&rsa_ctx.N, pubkey, pubkey_len) );
|
||||
|
||||
DEBUG_INFO ("RSA verify...");
|
||||
|
||||
MBEDTLS_MPI_CHK( mbedtls_rsa_rsassa_pkcs1_v15_verify (&rsa_ctx,
|
||||
MBEDTLS_MD_SHA256, 32,
|
||||
hash, sig) );
|
||||
cleanup:
|
||||
mbedtls_rsa_free (&rsa_ctx);
|
||||
if (ret != 0)
|
||||
{
|
||||
DEBUG_INFO ("fail:");
|
||||
|
||||
DEBUG_SHORT (ret);
|
||||
return -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
DEBUG_INFO ("verified.\r\n");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
#define RSA_EXPONENT 0x10001
|
||||
|
||||
struct jkiss_state { uint32_t x, y, z, c; };
|
||||
static struct jkiss_state jkiss_state_v;
|
||||
|
||||
|
||||
int prng_seed (int (*f_rng)(void *, unsigned char *, size_t),
|
||||
void *p_rng)
|
||||
{
|
||||
int ret;
|
||||
|
||||
struct jkiss_state *s = &jkiss_state_v;
|
||||
|
||||
MBEDTLS_MPI_CHK ( f_rng (p_rng, (unsigned char *)s, sizeof (struct jkiss_state)) );
|
||||
while (s->y == 0)
|
||||
MBEDTLS_MPI_CHK ( f_rng (p_rng, (unsigned char *)&s->y, sizeof (uint32_t)) );
|
||||
s->z |= 1; /* avoiding z=c=0 */
|
||||
|
||||
cleanup:
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
rsa_genkey (int pubkey_len, uint8_t *pubkey, uint8_t *p_q)
|
||||
{
|
||||
int ret;
|
||||
uint8_t index = 0;
|
||||
uint8_t *p = p_q;
|
||||
uint8_t *q = p_q + pubkey_len / 2;
|
||||
int cs;
|
||||
|
||||
extern void neug_flush (void);
|
||||
|
||||
neug_flush ();
|
||||
prng_seed (random_gen, &index);
|
||||
mbedtls_rsa_init (&rsa_ctx);
|
||||
|
||||
//clp.next = NULL;
|
||||
//clp.routine = rsa_cleanup;
|
||||
//clp.arg = NULL;
|
||||
//chopstx_cleanup_push (&clp);
|
||||
//cs = chopstx_setcancelstate (0); /* Allow cancellation. */
|
||||
MBEDTLS_MPI_CHK( mbedtls_rsa_gen_key (&rsa_ctx, random_gen, &index, pubkey_len * 8,
|
||||
RSA_EXPONENT) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary (&rsa_ctx.P, p, pubkey_len / 2) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary (&rsa_ctx.Q, q, pubkey_len / 2) );
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary (&rsa_ctx.N, pubkey, pubkey_len) );
|
||||
|
||||
cleanup:
|
||||
//chopstx_setcancelstate (cs);
|
||||
//chopstx_cleanup_pop (1);
|
||||
rsa_cleanup(NULL);
|
||||
if (ret != 0)
|
||||
return -1;
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -1,17 +0,0 @@
|
|||
#define DEBUG
|
||||
#ifdef DEBUG
|
||||
#define ENABLE_VIRTUAL_COM_PORT 1
|
||||
#endif
|
||||
#undef DFU_SUPPORT
|
||||
#define ORIGIN 0x08000000
|
||||
#define ORIGIN_REAL 0x08000000
|
||||
#undef PINPAD_SUPPORT
|
||||
|
||||
#define CERTDO_SUPPORT 1
|
||||
#undef HID_CARD_CHANGE_SUPPORT
|
||||
#define LIFE_CYCLE_MANAGEMENT_SUPPORT 1
|
||||
#undef ACKBTN_SUPPORT
|
||||
#define SERIALNO_STR_LEN 12
|
||||
#undef KDF_DO_REQUIRED
|
||||
|
||||
#define MHZ 133
|
||||
|
|
@ -1,136 +0,0 @@
|
|||
/*
|
||||
* debug.c -- Debuging with virtual COM port
|
||||
*
|
||||
* Copyright (C) 2010 Free Software Initiative of Japan
|
||||
* Author: NIIBE Yutaka <gniibe@fsij.org>
|
||||
*
|
||||
* This file is a part of Gnuk, a GnuPG USB Token implementation.
|
||||
*
|
||||
* Gnuk is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Gnuk is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
|
||||
* License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include "tusb.h"
|
||||
#include "config.h"
|
||||
|
||||
void my_write (const char *s, int len)
|
||||
{
|
||||
if (len == 0)
|
||||
return;
|
||||
|
||||
TU_LOG1(s);
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
put_hex (uint8_t nibble)
|
||||
{
|
||||
uint8_t c;
|
||||
|
||||
if (nibble < 0x0a)
|
||||
c = '0' + nibble;
|
||||
else
|
||||
c = 'a' + nibble - 0x0a;
|
||||
|
||||
//my_write ((const char *)&c, 1);
|
||||
printf("%X",nibble);
|
||||
}
|
||||
|
||||
void
|
||||
put_byte (uint8_t b)
|
||||
{
|
||||
put_hex (b >> 4);
|
||||
put_hex (b &0x0f);
|
||||
my_write ("\r\n", 2);
|
||||
}
|
||||
|
||||
void
|
||||
put_byte_with_no_nl (uint8_t b)
|
||||
{
|
||||
my_write (" ", 1);
|
||||
put_hex (b >> 4);
|
||||
put_hex (b &0x0f);
|
||||
}
|
||||
|
||||
void
|
||||
put_short (uint16_t x)
|
||||
{
|
||||
put_hex (x >> 12);
|
||||
put_hex ((x >> 8)&0x0f);
|
||||
put_hex ((x >> 4)&0x0f);
|
||||
put_hex (x & 0x0f);
|
||||
my_write ("\r\n", 2);
|
||||
}
|
||||
|
||||
void
|
||||
put_word (uint32_t x)
|
||||
{
|
||||
put_hex (x >> 28);
|
||||
put_hex ((x >> 24)&0x0f);
|
||||
put_hex ((x >> 20)&0x0f);
|
||||
put_hex ((x >> 16)&0x0f);
|
||||
put_hex ((x >> 12)&0x0f);
|
||||
put_hex ((x >> 8)&0x0f);
|
||||
put_hex ((x >> 4)&0x0f);
|
||||
put_hex (x & 0x0f);
|
||||
my_write ("\r\n", 2);
|
||||
}
|
||||
|
||||
void
|
||||
put_int (uint32_t x)
|
||||
{
|
||||
char s[10];
|
||||
int i;
|
||||
|
||||
for (i = 0; i < 10; i++)
|
||||
{
|
||||
s[i] = '0' + (x % 10);
|
||||
x /= 10;
|
||||
if (x == 0)
|
||||
break;
|
||||
}
|
||||
|
||||
while (i)
|
||||
{
|
||||
my_write (s+i, 1);
|
||||
i--;
|
||||
}
|
||||
|
||||
my_write (s, 1);
|
||||
my_write ("\r\n", 2);
|
||||
}
|
||||
|
||||
void
|
||||
put_binary (const char *s, int len)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < len; i++)
|
||||
{
|
||||
put_byte_with_no_nl (s[i]);
|
||||
if ((i & 0x0f) == 0x0f)
|
||||
my_write ("\r\n", 2);
|
||||
}
|
||||
my_write ("\r\n", 2);
|
||||
}
|
||||
|
||||
void
|
||||
put_string (const char *s)
|
||||
{
|
||||
my_write (s, strlen (s));
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -1,233 +0,0 @@
|
|||
/* -*- coding: utf-8 -*-
|
||||
* ec_p256k1.c - Elliptic curve over GF(p256k1)
|
||||
*
|
||||
* Copyright (C) 2014 Free Software Initiative of Japan
|
||||
* Author: NIIBE Yutaka <gniibe@fsij.org>
|
||||
*
|
||||
* This file is a part of Gnuk, a GnuPG USB Token implementation.
|
||||
*
|
||||
* Gnuk is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Gnuk is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
|
||||
* License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* Note: we don't take advantage of the specific feature of this curve,
|
||||
* but use same method of computation as NIST P-256 curve. That's due
|
||||
* to some software patent(s).
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include "bn.h"
|
||||
#include "modp256k1.h"
|
||||
#include "affine.h"
|
||||
#include "jpc-ac_p256k1.h"
|
||||
#include "mod.h"
|
||||
#include "ec_p256k1.h"
|
||||
|
||||
#define FIELD p256k1
|
||||
#define COEFFICIENT_A_IS_ZERO 1
|
||||
|
||||
/*
|
||||
* a = 0, b = 7
|
||||
*/
|
||||
#if 0
|
||||
static const bn256 coefficient_a[1] = {
|
||||
{{ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 }}
|
||||
};
|
||||
#endif
|
||||
|
||||
static const bn256 coefficient_b[1] = {
|
||||
{{ 0x7, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 }}
|
||||
};
|
||||
|
||||
|
||||
static const ac precomputed_KG[15] = {
|
||||
{
|
||||
{{{ 0x16f81798, 0x59f2815b, 0x2dce28d9, 0x029bfcdb,
|
||||
0xce870b07, 0x55a06295, 0xf9dcbbac, 0x79be667e }}},
|
||||
{{{ 0xfb10d4b8, 0x9c47d08f, 0xa6855419, 0xfd17b448,
|
||||
0x0e1108a8, 0x5da4fbfc, 0x26a3c465, 0x483ada77 }}}
|
||||
}, {
|
||||
{{{ 0x42d0e6bd, 0x13b7e0e7, 0xdb0f5e53, 0xf774d163,
|
||||
0x104d6ecb, 0x82a2147c, 0x243c4e25, 0x3322d401 }}},
|
||||
{{{ 0x6c28b2a0, 0x24f3a2e9, 0xa2873af6, 0x2805f63e,
|
||||
0x4ddaf9b7, 0xbfb019bc, 0xe9664ef5, 0x56e70797 }}}
|
||||
}, {
|
||||
{{{ 0x829d122a, 0xdca81127, 0x67e99549, 0x8f17f314,
|
||||
0x6a8a9e73, 0x9b889085, 0x846dd99d, 0x583fdfd9 }}},
|
||||
{{{ 0x63c4eac4, 0xf3c7719e, 0xb734b37a, 0xb44685a3,
|
||||
0x572a47a6, 0x9f92d2d6, 0x2ff57d81, 0xabc6232f }}}
|
||||
}, {
|
||||
{{{ 0x9ec4c0da, 0x1b7b444c, 0x723ea335, 0xe88c5678,
|
||||
0x981f162e, 0x9239c1ad, 0xf63b5f33, 0x8f68b9d2 }}},
|
||||
{{{ 0x501fff82, 0xf23cbf79, 0x95510bfd, 0xbbea2cfe,
|
||||
0xb6be215d, 0xde1d90c2, 0xba063986, 0x662a9f2d }}}
|
||||
}, {
|
||||
{{{ 0x114cbf09, 0x63c5e885, 0x7be77e3e, 0x2f27ce93,
|
||||
0xf54a3e33, 0xdaa6d12d, 0x3eff872c, 0x8b300e51 }}},
|
||||
{{{ 0xb3b10a39, 0x26c6ff28, 0x9aaf7169, 0x08f6a7aa,
|
||||
0x6b8238ea, 0x446f0d46, 0x7f43c0cc, 0x1cec3067 }}}
|
||||
}, {
|
||||
{{{ 0x075e9070, 0xba16ce6a, 0x9b5cfe37, 0xbc26893d,
|
||||
0x9c510774, 0xe1ddadfe, 0xfe3ae2f4, 0x90922d88 }}},
|
||||
{{{ 0x5c08824a, 0x653943cc, 0xfce8f4bc, 0x06d74475,
|
||||
0x533c615d, 0x8d101fa7, 0x742108a9, 0x7b1903f6 }}}
|
||||
}, {
|
||||
{{{ 0x6ebdc96c, 0x1bcfa45c, 0x1c7584ba, 0xe400bc04,
|
||||
0x74cf531f, 0x6395e20e, 0xc5131b30, 0x1edd0bb1 }}},
|
||||
{{{ 0xe358cf9e, 0xa117161b, 0x2724d11c, 0xe490d6f0,
|
||||
0xee6dd8c9, 0xf75062f6, 0xfba373e4, 0x31e03b2b }}}
|
||||
}, {
|
||||
{{{ 0x2120e2b3, 0x7f3b58fa, 0x7f47f9aa, 0x7a58fdce,
|
||||
0x4ce6e521, 0xe7be4ae3, 0x1f51bdba, 0xeaa649f2 }}},
|
||||
{{{ 0xba5ad93d, 0xd47a5305, 0xf13f7e59, 0x01a6b965,
|
||||
0x9879aa5a, 0xc69a80f8, 0x5bbbb03a, 0xbe3279ed }}}
|
||||
}, {
|
||||
{{{ 0x27bb4d71, 0xcf291a33, 0x33524832, 0x6caf7d6b,
|
||||
0x766584ee, 0x6e0ee131, 0xd064c589, 0x160cb0f6 }}},
|
||||
{{{ 0x17136e8d, 0x9d5de554, 0x1aab720e, 0xe3f2d468,
|
||||
0xccf75cc2, 0xd1378b49, 0xc4ff16e1, 0x6920c375 }}}
|
||||
}, {
|
||||
{{{ 0x1a9ee611, 0x3eef9e96, 0x9cc37faf, 0xfe4d7bf3,
|
||||
0xb321d965, 0x462aa9b3, 0x208736c5, 0x1702da3e }}},
|
||||
{{{ 0x3a545ceb, 0xfba57bbf, 0x7ea858f5, 0x6dbcd766,
|
||||
0x680d92f1, 0x088e897c, 0xbc626c80, 0x468c1fd8 }}}
|
||||
}, {
|
||||
{{{ 0xb188660a, 0xb40f85c7, 0x99bc3c36, 0xc5873c19,
|
||||
0x7f33b54c, 0x3c7b4541, 0x1f8c9bf8, 0x4cd3a93c }}},
|
||||
{{{ 0x33099cb0, 0xf8dce380, 0x2edd2f33, 0x7a167dd6,
|
||||
0x0ffe35b7, 0x576d8987, 0xc68ace5c, 0xd2de0386 }}}
|
||||
}, {
|
||||
{{{ 0x6658bb08, 0x9a9e0a72, 0xc589607b, 0xe23c5f2a,
|
||||
0xf2bfb4c8, 0xa048ca14, 0xc62c2291, 0x4d9a0f89 }}},
|
||||
{{{ 0x0f827294, 0x427b5f31, 0x9f2c35cd, 0x1ea7a8b5,
|
||||
0x85a3c00f, 0x95442e56, 0x9b57975a, 0x8cb83121 }}}
|
||||
}, {
|
||||
{{{ 0x51f5cf67, 0x4333f0da, 0xf4f0d3cb, 0x6d3ea47c,
|
||||
0xa05a831f, 0x442fda14, 0x016d3e81, 0x6a496013 }}},
|
||||
{{{ 0xe52e0f48, 0xf647318c, 0x4a0d5ff1, 0x5ff3a66e,
|
||||
0x61199ba8, 0x046ed81a, 0x3e79c23a, 0x578edf08 }}}
|
||||
}, {
|
||||
{{{ 0x3ea01ea7, 0xb8f996f8, 0x7497bb15, 0xc0045d33,
|
||||
0x6205647c, 0xc4749dc9, 0x0efd22c9, 0xd8946054 }}},
|
||||
{{{ 0x12774ad5, 0x062dcb09, 0x8be06e3a, 0xcb13f310,
|
||||
0x235de1a9, 0xca281d35, 0x69c3645c, 0xaf8a7412 }}}
|
||||
}, {
|
||||
{{{ 0xbeb8b1e2, 0x8808ca5f, 0xea0dda76, 0x0262b204,
|
||||
0xddeb356b, 0xb6fffffc, 0xfbb83870, 0x52de253a }}},
|
||||
{{{ 0x8f8d21ea, 0x961f40c0, 0x002f03ed, 0x89686278,
|
||||
0x38e421ea, 0x0ff834d7, 0xd36fb8db, 0x3a270d6f }}}
|
||||
}
|
||||
};
|
||||
|
||||
static const ac precomputed_2E_KG[15] = {
|
||||
{
|
||||
{{{ 0x39a48db0, 0xefd7835b, 0x9b3c03bf, 0x9f1215a2,
|
||||
0x9b7bde45, 0x2791d0a0, 0x696e7167, 0x100f44da }}},
|
||||
{{{ 0x2bc65a09, 0x0fbd5cd6, 0xff5195ac, 0xb7ff4a18,
|
||||
0x0c090666, 0x2ec8f330, 0x92a00b77, 0xcdd9e131 }}}
|
||||
}, {
|
||||
{{{ 0x40fb27b6, 0x32427e28, 0xbe430576, 0xc76e3db2,
|
||||
0x61686aa5, 0x10f238ad, 0xbe778b1b, 0xfea74e3d }}},
|
||||
{{{ 0xf23cb96f, 0x701d3db7, 0x973f7b77, 0x126b596b,
|
||||
0xccb6af93, 0x7cf674de, 0x9b0b1329, 0x6e0568db }}}
|
||||
}, {
|
||||
{{{ 0x2c8118bc, 0x6cac5154, 0x399ddd98, 0x19bd4b34,
|
||||
0x2e9c8949, 0x47248a8d, 0x2cefa3b1, 0x734cb6a8 }}},
|
||||
{{{ 0x1e410fd5, 0xf1b340ad, 0xc4873539, 0xa2982bee,
|
||||
0xd4de4530, 0x7b5a3ea4, 0x42202574, 0xae46e10e }}}
|
||||
}, {
|
||||
{{{ 0xac1f98cd, 0xcbfc99c8, 0x4d7f0308, 0x52348905,
|
||||
0x1cc66021, 0xfaed8a9c, 0x4a474870, 0x9c3919a8 }}},
|
||||
{{{ 0xd4fc599d, 0xbe7e5e03, 0x6c64c8e6, 0x905326f7,
|
||||
0xf260e641, 0x584f044b, 0x4a4ddd57, 0xddb84f0f }}}
|
||||
}, {
|
||||
{{{ 0xed7cebed, 0xc4aacaa8, 0x4fae424e, 0xb75d2dce,
|
||||
0xba20735e, 0xa01585a2, 0xba122399, 0x3d75f24b }}},
|
||||
{{{ 0xd5570dce, 0xcbe4606f, 0x2da192c2, 0x9d00bfd7,
|
||||
0xa57b7265, 0x9c3ce86b, 0xec4edf5e, 0x987a22f1 }}}
|
||||
}, {
|
||||
{{{ 0x73ea0665, 0x211b9715, 0xf3a1abbb, 0x86f485d4,
|
||||
0xcd076f0e, 0xabd242d8, 0x0ba5dc88, 0x862332ab }}},
|
||||
{{{ 0x7b784911, 0x09af505c, 0xcaf4fae7, 0xc89544e8,
|
||||
0xae9a32eb, 0x256625f6, 0x606d1a3f, 0xe2532b72 }}}
|
||||
}, {
|
||||
{{{ 0x0deaf885, 0x79e9f313, 0x46df21c9, 0x938ff76e,
|
||||
0xa953bb2c, 0x1968f5fb, 0x29155f27, 0xdff538bf }}},
|
||||
{{{ 0x31d5d020, 0xf7bae0b1, 0x1a676a8d, 0x5afdc787,
|
||||
0xfa9d53ff, 0x11b4f032, 0xc5959167, 0x86ba433e }}}
|
||||
}, {
|
||||
{{{ 0x9475b7ba, 0x884fdff0, 0xe4918b3d, 0xe039e730,
|
||||
0xf5018cdb, 0x3d3e57ed, 0x1943785c, 0x95939698 }}},
|
||||
{{{ 0x7524f2fd, 0xe9b8abf8, 0xc8709385, 0x9c653f64,
|
||||
0x4b9cd684, 0x8ba0386a, 0x88c331dd, 0x2e7e5528 }}}
|
||||
}, {
|
||||
{{{ 0xeefe79e5, 0x940bef53, 0xbe9b87f3, 0xc518d286,
|
||||
0x7833042c, 0x9e0c7c76, 0x11fbe152, 0x104e2cb5 }}},
|
||||
{{{ 0x50bbec83, 0xc0d35e0f, 0x4acd0fcc, 0xee4879be,
|
||||
0x006085ee, 0xc8d80f5d, 0x72fe1ac1, 0x3c51bc1c }}}
|
||||
}, {
|
||||
{{{ 0xb2de976e, 0x06187f61, 0xf5e4b4b6, 0x52869e18,
|
||||
0x38d332ca, 0x74d4facd, 0xb3a2f8d9, 0x5c1c90b4 }}},
|
||||
{{{ 0xdaa37893, 0x98644d09, 0xabe39818, 0x682435a8,
|
||||
0x469c53a0, 0x17e46617, 0x77dc2e64, 0x642f9632 }}}
|
||||
}, {
|
||||
{{{ 0x222f6c54, 0xad2101c5, 0xfa74785e, 0xb05c7a58,
|
||||
0x489bcdaf, 0xce55fa79, 0xffe88d54, 0xc1f920fd }}},
|
||||
{{{ 0x9065e490, 0x32553ab0, 0x35329f74, 0x7611b9af,
|
||||
0xab7b24c0, 0x57df19ef, 0x6181c447, 0xb9a78749 }}}
|
||||
}, {
|
||||
{{{ 0xa80b7ea8, 0x392f156f, 0x8ae4a8bf, 0x57ab7ca0,
|
||||
0x50c4b178, 0xac320747, 0x0e781feb, 0x146041b9 }}},
|
||||
{{{ 0x845279b2, 0xd343f075, 0x7387afa5, 0x2d4fe757,
|
||||
0xa72f3c39, 0x151e0948, 0x550da168, 0x41a6d54e }}}
|
||||
}, {
|
||||
{{{ 0x075a0010, 0xb3134ed3, 0x7ae93e23, 0x9fa76f4b,
|
||||
0x7bb4daaa, 0xc0db256f, 0x464dd8a3, 0x7668dc27 }}},
|
||||
{{{ 0x9f5da977, 0x150063f5, 0x05efce00, 0x3acac5c8,
|
||||
0x884493fe, 0xc8e12ffc, 0x88f06bd2, 0x4ab936d8 }}}
|
||||
}, {
|
||||
{{{ 0x5d09ea98, 0x996fde77, 0x4145da58, 0x16ddf512,
|
||||
0xdc2fb225, 0xa97a6ca8, 0xfbdcdf5a, 0xc7331f30 }}},
|
||||
{{{ 0x86a86e52, 0x838f99e0, 0x77795edd, 0x68d39b29,
|
||||
0x9f412aaa, 0xe4e4f97e, 0x30d25352, 0xe5cc2c0a }}}
|
||||
}, {
|
||||
{{{ 0x9c21ff71, 0xb3d68650, 0xddbe3884, 0x11e7589d,
|
||||
0x423bac67, 0x7efd4055, 0x46957425, 0x587a7293 }}},
|
||||
{{{ 0x8f5a8fc6, 0x360adc2e, 0xbd69f12e, 0x6f8bbafb,
|
||||
0x0a3f3b4d, 0xf671f423, 0x59942dc3, 0xb49acb47 }}}
|
||||
}
|
||||
};
|
||||
|
||||
/*
|
||||
* N: order of G
|
||||
* 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141
|
||||
*/
|
||||
static const bn256 N[1] = {
|
||||
{{ 0xd0364141, 0xbfd25e8c, 0xaf48a03b, 0xbaaedce6,
|
||||
0xfffffffe, 0xffffffff, 0xffffffff, 0xffffffff }}
|
||||
};
|
||||
|
||||
/*
|
||||
* MU = 2^512 / N
|
||||
* MU = ( (1 << 256) | MU_lower )
|
||||
*/
|
||||
static const bn256 MU_lower[1] = {
|
||||
{{ 0x2fc9bec0, 0x402da173, 0x50b75fc4, 0x45512319,
|
||||
0x1, 0x0, 0x0, 0x0 }}
|
||||
};
|
||||
|
||||
|
||||
#include "ecc.c"
|
||||
|
|
@ -1,4 +0,0 @@
|
|||
int compute_kP_p256k1 (ac *X, const bn256 *K, const ac *P);
|
||||
int compute_kG_p256k1 (ac *X, const bn256 *K);
|
||||
void ecdsa_p256k1 (bn256 *r, bn256 *s, const bn256 *z, const bn256 *d);
|
||||
int check_secret_p256k1 (const bn256 *q, bn256 *d1);
|
||||
|
|
@ -1,955 +0,0 @@
|
|||
/* -*- coding: utf-8 -*-
|
||||
* ecc-ed25519.c - Elliptic curve computation for
|
||||
* the twisted Edwards curve: -x^2 + y^2 = 1 + d*x^2*y^2
|
||||
* d = -121665/121666
|
||||
*
|
||||
* Copyright (C) 2014, 2017 Free Software Initiative of Japan
|
||||
* Author: NIIBE Yutaka <gniibe@fsij.org>
|
||||
*
|
||||
* This file is a part of Gnuk, a GnuPG USB Token implementation.
|
||||
*
|
||||
* Gnuk is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Gnuk is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
|
||||
* License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "bn.h"
|
||||
#include "mod.h"
|
||||
#include "mod25638.h"
|
||||
#include "mbedtls/sha512.h"
|
||||
|
||||
/*
|
||||
* References:
|
||||
*
|
||||
* [1] Daniel J. Bernstein, Niels Duif, Tanja Lange, Peter Schwabe, Bo-Yin Yang.
|
||||
* High-speed high-security signatures.
|
||||
* Journal of Cryptographic Engineering 2 (2012), 77--89.
|
||||
* http://cr.yp.to/papers.html#ed25519
|
||||
*
|
||||
* [2] Daniel J. Bernstein, Peter Birkner, Marc Joye, Tanja Lange,
|
||||
* Christiane Peters.
|
||||
* Twisted Edwards curves.
|
||||
* Pages 389--405 in Progress in cryptology---AFRICACRYPT 2008.
|
||||
* http://cr.yp.to/papers.html#twisted
|
||||
*/
|
||||
|
||||
/*
|
||||
* IMPLEMENTATION NOTE
|
||||
*
|
||||
* (0) We assume that the processor has no cache, nor branch target
|
||||
* prediction. Thus, we don't avoid indexing by secret value.
|
||||
* We don't avoid conditional jump if both cases have same timing,
|
||||
* either.
|
||||
*
|
||||
* (1) We use Radix-32 field arithmetic. It's a representation like
|
||||
* 2^256-38, but it's more redundant. For example, "1" can be
|
||||
* represented in three ways in 256-bit: 1, 2^255-18, and
|
||||
* 2^256-37.
|
||||
*
|
||||
* (2) We use fixed base comb multiplication. Scalar is 252-bit.
|
||||
* There are various possible choices for 252 = 2 * 2 * 3 * 3 * 7.
|
||||
* Current choice of total size is 3KB. We use three tables, and
|
||||
* a table has 16 points (3 * 1KB).
|
||||
*
|
||||
* Window size W = 4-bit, E = 21.
|
||||
* <--21-bit-
|
||||
* <---42-bit----------
|
||||
* [ ][########][////////][ ][########][////////]
|
||||
* <-------63-bit----------------
|
||||
* <-----------84-bit----------------------
|
||||
* <--------------105-bit----------------------------
|
||||
*
|
||||
* [ ][########][////////][ ][########][////////]
|
||||
* <-126-bit-
|
||||
* <-147-bit-
|
||||
* <----168-bit--------
|
||||
*
|
||||
* <-------189-bit---------------
|
||||
* <----------210-bit----------------------
|
||||
* <-------------231-bit-----------------------------
|
||||
*/
|
||||
|
||||
/*
|
||||
* Identity element: (0,1)
|
||||
* Negation: -(x,y) = (-x,y)
|
||||
*
|
||||
* d: -0x2DFC9311D490018C7338BF8688861767FF8FF5B2BEBE27548A14B235ECA6874A
|
||||
* order:
|
||||
* 0x1000000000000000000000000000000014DEF9DEA2F79CD65812631A5CF5D3ED
|
||||
* Gx: 0x216936D3CD6E53FEC0A4E231FDD6DC5C692CC7609525A7B2C9562D608F25D51A
|
||||
* Gy: 0x6666666666666666666666666666666666666666666666666666666666666658
|
||||
*/
|
||||
|
||||
/* d + 2^255 - 19 */
|
||||
static const bn256 coefficient_d[1] = {
|
||||
{{ 0x135978a3, 0x75eb4dca, 0x4141d8ab, 0x00700a4d,
|
||||
0x7779e898, 0x8cc74079, 0x2b6ffe73, 0x52036cee }} };
|
||||
|
||||
|
||||
/**
|
||||
* @brief Projective Twisted Coordinates
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
bn256 x[1];
|
||||
bn256 y[1];
|
||||
bn256 z[1];
|
||||
} ptc;
|
||||
|
||||
#include "affine.h"
|
||||
|
||||
|
||||
static int
|
||||
mod25519_is_neg (const bn256 *a)
|
||||
{
|
||||
return (a->word[0] & 1);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief X = 2 * A
|
||||
*
|
||||
* Compute (X3 : Y3 : Z3) = 2 * (X1 : Y1 : Z1)
|
||||
*/
|
||||
static void
|
||||
point_double (ptc *X, const ptc *A)
|
||||
{
|
||||
bn256 b[1], d[1], e[1];
|
||||
|
||||
/* Compute: B = (X1 + Y1)^2 */
|
||||
mod25638_add (b, A->x, A->y);
|
||||
mod25638_sqr (b, b);
|
||||
|
||||
/* Compute: C = X1^2 : E */
|
||||
mod25638_sqr (e, A->x);
|
||||
|
||||
/* Compute: D = Y1^2 */
|
||||
mod25638_sqr (d, A->y);
|
||||
|
||||
/* E = aC; where a = -1 */
|
||||
/* Compute: D - E = D + C : Y3_tmp */
|
||||
mod25638_add (X->y, e, d);
|
||||
|
||||
/* Compute: -F = -(E + D) = C - D; where a = -1 : E */
|
||||
mod25638_sub (e, e, d);
|
||||
|
||||
/* Compute: H = Z1^2 : D */
|
||||
mod25638_sqr (d, A->z);
|
||||
|
||||
/* Compute: -J = 2*H - F : D */
|
||||
mod25638_add (d, d, d);
|
||||
mod25638_add (d, d, e);
|
||||
|
||||
/* Compute: X3 = (B-C-D)*J = -J*(C+D-B) = -J*(Y3_tmp-B) */
|
||||
mod25638_sub (X->x, X->y, b);
|
||||
mod25638_mul (X->x, X->x, d);
|
||||
|
||||
/* Compute: Y3 = -F*(D-E) = -F*Y3_tmp */
|
||||
mod25638_mul (X->y, X->y, e);
|
||||
|
||||
/* Z3 = -F*-J */
|
||||
mod25638_mul (X->z, e, d);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief X = A + B
|
||||
*
|
||||
* @param X Destination PTC
|
||||
* @param A PTC
|
||||
* @param B AC
|
||||
*
|
||||
* Compute: (X3 : Y3 : Z3) = (X1 : Y1 : Z1) + (X2 : Y2 : 1)
|
||||
*/
|
||||
static void
|
||||
point_add (ptc *X, const ptc *A, const ac *B)
|
||||
{
|
||||
bn256 c[1], d[1], e[1], tmp[1];
|
||||
|
||||
/* Compute: C = X1 * X2 */
|
||||
mod25638_mul (c, A->x, B->x);
|
||||
|
||||
/* Compute: D = Y1 * Y2 */
|
||||
mod25638_mul (d, A->y, B->y);
|
||||
|
||||
/* Compute: E = d * C * D */
|
||||
mod25638_mul (e, c, d);
|
||||
mod25638_mul (e, coefficient_d, e);
|
||||
|
||||
/* Compute: C_1 = C + D */
|
||||
mod25638_add (c, c, d);
|
||||
|
||||
/* Compute: D_1 = Z1^2 : B */
|
||||
mod25638_sqr (d, A->z);
|
||||
|
||||
/* tmp = D_1 - E : F */
|
||||
mod25638_sub (tmp, d, e);
|
||||
|
||||
/* D_2 = D_1 + E : G */
|
||||
mod25638_add (d, d, e);
|
||||
|
||||
/* X3_final = Z1 * tmp * ((X1 + Y1) * (X2 + Y2) - C_1) */
|
||||
mod25638_add (X->x, A->x, A->y);
|
||||
mod25638_add (e, B->x, B->y);
|
||||
mod25638_mul (e, X->x, e);
|
||||
mod25638_sub (e, e, c);
|
||||
mod25638_mul (e, tmp, e);
|
||||
mod25638_mul (X->x, A->z, e);
|
||||
|
||||
/* Y3_final = Z1 * D_2 * C_1 */
|
||||
mod25638_mul (c, d, c);
|
||||
mod25638_mul (X->y, A->z, c);
|
||||
|
||||
/* Z3_final = tmp * D_2 */
|
||||
mod25638_mul (X->z, tmp, d);
|
||||
|
||||
/* A = Z1 */
|
||||
/* B = A^2 */
|
||||
/* C = X1 * X2 */
|
||||
/* D = Y1 * Y2 */
|
||||
/* E = d * C * D */
|
||||
/* F = B - E */
|
||||
/* G = B + E */
|
||||
/* X3 = A * F * ((X1 + Y1) * (X2 + Y2) - C - D) */
|
||||
/* Y3 = A * G * (D - aC); where a = -1 */
|
||||
/* Z3 = F * G */
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief X = convert A
|
||||
*
|
||||
* @param X Destination AC
|
||||
* @param A PTC
|
||||
*
|
||||
* (X1:Y1:Z1) represents the affine point (x=X1/Z1, y=Y1/Z1)
|
||||
*/
|
||||
static void
|
||||
point_ptc_to_ac (ac *X, const ptc *A)
|
||||
{
|
||||
bn256 z_inv[1];
|
||||
|
||||
/*
|
||||
* A->z may be bigger than p25519, or two times bigger than p25519.
|
||||
* But this is no problem for computation of mod_inv.
|
||||
*/
|
||||
mod_inv (z_inv, A->z, p25519);
|
||||
|
||||
mod25638_mul (X->x, A->x, z_inv);
|
||||
mod25519_reduce (X->x);
|
||||
mod25638_mul (X->y, A->y, z_inv);
|
||||
mod25519_reduce (X->y);
|
||||
}
|
||||
|
||||
|
||||
static const ac precomputed_KG[16] = {
|
||||
{ {{{ 0, 0, 0, 0, 0, 0, 0, 0 }}},
|
||||
{{{ 1, 0, 0, 0, 0, 0, 0, 0 }}} },
|
||||
{ {{{ 0x8f25d51a, 0xc9562d60, 0x9525a7b2, 0x692cc760,
|
||||
0xfdd6dc5c, 0xc0a4e231, 0xcd6e53fe, 0x216936d3 }}},
|
||||
{{{ 0x66666658, 0x66666666, 0x66666666, 0x66666666,
|
||||
0x66666666, 0x66666666, 0x66666666, 0x66666666 }}} },
|
||||
{ {{{ 0x3713af22, 0xac7137bd, 0xac634604, 0x25ed77a4,
|
||||
0xa815e038, 0xce0d0064, 0xbca90151, 0x041c030f }}},
|
||||
{{{ 0x0780f989, 0xe9b33fcf, 0x3d4445e7, 0xe4e97c2a,
|
||||
0x655e5c16, 0xc67dc71c, 0xee43fb7a, 0x72467625 }}} },
|
||||
{ {{{ 0x3ee99893, 0x76a19171, 0x7ba9b065, 0xe647edd9,
|
||||
0x6aeae260, 0x31f39299, 0x5f4a9bb2, 0x6d9e4545 }}},
|
||||
{{{ 0x94cae280, 0xc41433da, 0x79061211, 0x8e842de8,
|
||||
0xa259dc8a, 0xaab95e0b, 0x99013cd0, 0x28bd5fc3 }}} },
|
||||
{ {{{ 0x7d23ea24, 0x59e22c56, 0x0460850e, 0x1e745a88,
|
||||
0xda13ef4b, 0x4583ff4c, 0x95083f85, 0x1f13202c }}},
|
||||
{{{ 0x90275f48, 0xad42025c, 0xb55c4778, 0x0085087e,
|
||||
0xfdfd7ffa, 0xf21109e7, 0x6c381b7e, 0x66336d35 }}} },
|
||||
{ {{{ 0xd00851f2, 0xaa9476ab, 0x4a61600b, 0xe7838534,
|
||||
0x1a52df87, 0x0de65625, 0xbd675870, 0x5f0dd494 }}},
|
||||
{{{ 0xe23493ba, 0xf20aec1b, 0x3414b0a8, 0x8f7f2741,
|
||||
0xa80e1eb6, 0x497e74bd, 0xe9365b15, 0x1648eaac }}} },
|
||||
{ {{{ 0x04ac2b69, 0x5b78dcec, 0x32001a73, 0xecdb66ce,
|
||||
0xb34cf697, 0xb75832f4, 0x3a2bce94, 0x7aaf57c5 }}},
|
||||
{{{ 0x60fdfc6f, 0xb32ed2ce, 0x757924c6, 0x77bf20be,
|
||||
0x48742dd1, 0xaebd15dd, 0x55d38439, 0x6311bb16 }}} },
|
||||
{ {{{ 0x42ff5c97, 0x139cdd73, 0xdbd82964, 0xee4c359e,
|
||||
0x70611a3f, 0x91c1cd94, 0x8075dbcb, 0x1d0c34f6 }}},
|
||||
{{{ 0x5f931219, 0x43eaa549, 0xa23d35a6, 0x3737aba7,
|
||||
0x46f167bb, 0x54b1992f, 0xb74a9944, 0x01a11f3c }}} },
|
||||
{ {{{ 0xba46b161, 0x67a5310e, 0xd9d67f6c, 0x790f8527,
|
||||
0x2f6cc814, 0x359c5b5f, 0x7786383d, 0x7b6a5565 }}},
|
||||
{{{ 0x663ab0d3, 0xf1431b60, 0x09995826, 0x14a32d8f,
|
||||
0xeddb8571, 0x61d526f6, 0x0eac739a, 0x0cb7acea }}} },
|
||||
{ {{{ 0x4a2d009f, 0x5eb1a697, 0xd8df987a, 0xdacb43b4,
|
||||
0x8397f958, 0x4870f214, 0x8a175fbb, 0x5aa0c67c }}},
|
||||
{{{ 0x78887db3, 0x27dbbd4c, 0x64e322ab, 0xe327b707,
|
||||
0x7cbe4e3b, 0x87e293fa, 0xbda72395, 0x17040799 }}} },
|
||||
{ {{{ 0x99d1e696, 0xc833a5a2, 0x2d9d5877, 0x969bff8e,
|
||||
0x2216fa67, 0x383a533a, 0x684d3925, 0x338bbe0a }}},
|
||||
{{{ 0xd6cfb491, 0x35b5aae8, 0xaa12f3f8, 0x4a588279,
|
||||
0x2e30380e, 0xa7c2e708, 0x9e4b3d62, 0x69f13e09 }}} },
|
||||
{ {{{ 0x27f1cd56, 0xec0dc2ef, 0xdb11cc97, 0x1af11548,
|
||||
0x9ebc7613, 0xb642f86a, 0xcb77c3b9, 0x5ce45e73 }}},
|
||||
{{{ 0x3eddd6de, 0x5d128786, 0x4859eab7, 0x16f9a6b4,
|
||||
0xd8782345, 0x55c53916, 0xdb7b202a, 0x6b1dfa87 }}} },
|
||||
{ {{{ 0x19e30528, 0x2461a8ed, 0x665cfb1c, 0xaf756bf9,
|
||||
0x3a6e8673, 0x0fcafd1d, 0x45d10f48, 0x0d264435 }}},
|
||||
{{{ 0x5431db67, 0x543fd4c6, 0x60932432, 0xc153a5b3,
|
||||
0xd2119aa4, 0x41d5b8eb, 0x8b09b6a5, 0x36bd9ab4 }}} },
|
||||
{ {{{ 0x21e06738, 0x6d39f935, 0x3765dd86, 0x4e6a7c59,
|
||||
0xa4730880, 0xefc0dd80, 0x4079fe2f, 0x40617e56 }}},
|
||||
{{{ 0x921439b9, 0xbc83cdff, 0x98833c09, 0xd5cccc06,
|
||||
0xda13cdcb, 0xe315c425, 0x67ff5370, 0x37bc6e84 }}} },
|
||||
{ {{{ 0xf643b5f5, 0x65e7f028, 0x0ffbf5a8, 0x5b0d4831,
|
||||
0xf4085f62, 0x0f540498, 0x0db7bd1b, 0x6f0bb035 }}},
|
||||
{{{ 0x9733742c, 0x51f65571, 0xf513409f, 0x2fc047a0,
|
||||
0x355facf6, 0x07f45010, 0x3a989a9c, 0x5cd416a9 }}} },
|
||||
{ {{{ 0x748f2a67, 0x0bdd7208, 0x415b7f7f, 0x0cf0b80b,
|
||||
0x57aa0119, 0x44afdd5f, 0x430dc946, 0x05d68802 }}},
|
||||
{{{ 0x1a60eeb2, 0x420c46e5, 0x665024f5, 0xc60a9b33,
|
||||
0x48c51347, 0x37520265, 0x00a21bfb, 0x6f4be0af }}} }
|
||||
};
|
||||
|
||||
static const ac precomputed_2E_KG[16] = {
|
||||
{ {{{ 0, 0, 0, 0, 0, 0, 0, 0 }}},
|
||||
{{{ 1, 0, 0, 0, 0, 0, 0, 0 }}} },
|
||||
{ {{{ 0x199c4f7d, 0xec314ac0, 0xb2ebaaf9, 0x66a39c16,
|
||||
0xedd4d15f, 0xab1c92b8, 0x57d9eada, 0x482a4cdf }}},
|
||||
{{{ 0x6e4eb04b, 0xbd513b11, 0x25e4fd6a, 0x3f115fa5,
|
||||
0x14519298, 0x0b3c5fc6, 0x81c2f7a8, 0x7391de43 }}} },
|
||||
{ {{{ 0x1254fe02, 0xa57dca18, 0x6da34368, 0xa56a2a14,
|
||||
0x63e7328e, 0x44c6e34f, 0xca63ab3e, 0x3f748617 }}},
|
||||
{{{ 0x7dc1641e, 0x5a13dc52, 0xee4e9ca1, 0x4cbb2899,
|
||||
0x1ba9acee, 0x3938a289, 0x420fc47b, 0x0fed89e6 }}} },
|
||||
{ {{{ 0x49cbad08, 0x3c193f32, 0x15e80ef5, 0xdda71ef1,
|
||||
0x9d128c33, 0xda44186c, 0xbf98c24f, 0x54183ede }}},
|
||||
{{{ 0x93d165c1, 0x2cb483f7, 0x177f44aa, 0x51762ace,
|
||||
0xb4ab035d, 0xb3fe651b, 0xa0b0d4e5, 0x426c99c3 }}} },
|
||||
{ {{{ 0xef3f3fb1, 0xb3fcf4d8, 0x065060a0, 0x7052292b,
|
||||
0x24240b15, 0x18795ff8, 0x9989ffcc, 0x13aea184 }}},
|
||||
{{{ 0xc2b81f44, 0x1930c101, 0x10600555, 0x672d6ca4,
|
||||
0x1b25e570, 0xfbddbff2, 0x8ca12b70, 0x0884949c }}} },
|
||||
{ {{{ 0x00564bbf, 0x9983a033, 0xde61b72d, 0x95587d25,
|
||||
0xeb17ad71, 0xb6719dfb, 0xc0bc3517, 0x46871ad0 }}},
|
||||
{{{ 0xe95a6693, 0xb034fb61, 0x76eabad9, 0x5b0d8d18,
|
||||
0x884785dc, 0xad295dd0, 0x74a1276a, 0x359debad }}} },
|
||||
{ {{{ 0xe89fb5ca, 0x2e5a2686, 0x5656c6c5, 0xd3d200ba,
|
||||
0x9c969001, 0xef4c051e, 0x02cb45f4, 0x0d4ea946 }}},
|
||||
{{{ 0x76d6e506, 0xa6f8a422, 0x63209e23, 0x454c768f,
|
||||
0x2b372386, 0x5c12fd04, 0xdbfee11f, 0x1aedbd3e }}} },
|
||||
{ {{{ 0x00dbf569, 0x700ab50f, 0xd335b313, 0x9553643c,
|
||||
0xa17dc97e, 0xeea9bddf, 0x3350a2bd, 0x0d12fe3d }}},
|
||||
{{{ 0xa16a3dee, 0xe5ac35fe, 0xf81950c3, 0x4ae4664a,
|
||||
0x3dbbf921, 0x75c63df4, 0x2958a5a6, 0x545b109c }}} },
|
||||
{ {{{ 0x0a61b29c, 0xd7a52a98, 0x65aca9ee, 0xe21e0acb,
|
||||
0x5985dcbe, 0x57a69c0f, 0xeb87a534, 0x3c0c1e7b }}},
|
||||
{{{ 0x6384bd2f, 0xf0a0b50d, 0xc6939e4b, 0xff349a34,
|
||||
0x6e2f1973, 0x922c4554, 0xf1347631, 0x74e826b2 }}} },
|
||||
{ {{{ 0xa655803c, 0xd7eaa066, 0x38292c5c, 0x09504e76,
|
||||
0x2c874953, 0xe298a02e, 0x8932b73f, 0x225093ed }}},
|
||||
{{{ 0xe69c3efd, 0xf93e2b4d, 0x8a87c799, 0xa2cbd5fc,
|
||||
0x85dba986, 0xdf41da94, 0xccee8edc, 0x36fe85e7 }}} },
|
||||
{ {{{ 0x7d742813, 0x78df7dc5, 0x4a193e64, 0x333bcc6d,
|
||||
0x6a966d2d, 0x8242aa25, 0x4cd36d32, 0x03500a94 }}},
|
||||
{{{ 0x580505d7, 0xd5d110fc, 0xfa11e1e9, 0xb2f47e16,
|
||||
0x06eab6b4, 0xd0030f92, 0x62c91d46, 0x2dc80d5f }}} },
|
||||
{ {{{ 0x2a75e492, 0x5788b01a, 0xbae31352, 0x992acf54,
|
||||
0x8159db27, 0x4591b980, 0xd3d84740, 0x36c6533c }}},
|
||||
{{{ 0x103883b5, 0xc44c7c00, 0x515d0820, 0x10329423,
|
||||
0x71b9dc16, 0xbd306903, 0xf88f8d32, 0x7edd5a95 }}} },
|
||||
{ {{{ 0x005523d7, 0xfd63b1ac, 0xad70dd21, 0x74482e0d,
|
||||
0x02b56105, 0x67c9d9d0, 0x5971b456, 0x4d318012 }}},
|
||||
{{{ 0x841106df, 0xdc9a6f6d, 0xa326987f, 0x7c52ed9d,
|
||||
0x00607ea0, 0x4dbeaa6f, 0x6959e688, 0x115c221d }}} },
|
||||
{ {{{ 0xc80f7c16, 0xf8718464, 0xe9930634, 0x05dc8f40,
|
||||
0xc2e9d5f4, 0xefa699bb, 0x021da209, 0x2469e813 }}},
|
||||
{{{ 0xc602a3c4, 0x75c02845, 0x0a200f9d, 0x49d1b2ce,
|
||||
0x2fb3ec8f, 0xd21b75e4, 0xd72a7545, 0x10dd726a }}} },
|
||||
{ {{{ 0x63ef1a6c, 0xeda58527, 0x051705e0, 0xb3fc0e72,
|
||||
0x44f1161f, 0xbda6f3ee, 0xf339efe5, 0x7680aebf }}},
|
||||
{{{ 0xb1b070a7, 0xe8d3fd01, 0xdbfbaaa0, 0xc3ff7dbf,
|
||||
0xa320c916, 0xd81ef6f2, 0x62a3b54d, 0x3e22a1fb }}} },
|
||||
{ {{{ 0xb1fa18c8, 0xcdbb9187, 0xcb483a17, 0x8ddb5f6b,
|
||||
0xea49af98, 0xc0a880b9, 0xf2dfddd0, 0x53bf600b }}},
|
||||
{{{ 0x9e25b164, 0x4217404c, 0xafb74aa7, 0xfabf06ee,
|
||||
0x2b9f233c, 0xb17712ae, 0xd0eb909e, 0x71f0b344 }}} }
|
||||
};
|
||||
|
||||
static const ac precomputed_4E_KG[16] = {
|
||||
{ {{{ 0, 0, 0, 0, 0, 0, 0, 0 }}},
|
||||
{{{ 1, 0, 0, 0, 0, 0, 0, 0 }}} },
|
||||
{ {{{ 0xe388a820, 0xbb6ec091, 0x5182278a, 0xa928b283,
|
||||
0xa9a6eb83, 0x2259174d, 0x45500054, 0x184b48cb }}},
|
||||
{{{ 0x26e77c33, 0xfe324dba, 0x83faf453, 0x6679a5e3,
|
||||
0x2380ef73, 0xdd60c268, 0x03dc33a9, 0x3ee0e07a }}} },
|
||||
{ {{{ 0xce974493, 0x403aff28, 0x9bf6f5c4, 0x84076bf4,
|
||||
0xecd898fb, 0xec57038c, 0xb663ed49, 0x2898ffaa }}},
|
||||
{{{ 0xf335163d, 0xf4b3bc46, 0xfa4fb6c6, 0xe613a0f4,
|
||||
0xb9934557, 0xe759d6bc, 0xab6c9477, 0x094f3b96 }}} },
|
||||
{ {{{ 0x6afffe9e, 0x168bb5a0, 0xee748c29, 0x950f7ad7,
|
||||
0xda17203d, 0xa4850a2b, 0x77289e0f, 0x0062f7a7 }}},
|
||||
{{{ 0x4b3829fa, 0x6265d4e9, 0xbdfcd386, 0x4f155ada,
|
||||
0x475795f6, 0x9f38bda4, 0xdece4a4c, 0x560ed4b3 }}} },
|
||||
{ {{{ 0x141e648a, 0xdad4570a, 0x019b965c, 0x8bbf674c,
|
||||
0xdb08fe30, 0xd7a8d50d, 0xa2851109, 0x7efb45d3 }}},
|
||||
{{{ 0xd0c28cda, 0x52e818ac, 0xa321d436, 0x792257dd,
|
||||
0x9d71f8b7, 0x867091c6, 0x11a1bf56, 0x0fe1198b }}} },
|
||||
{ {{{ 0x06137ab1, 0x4e848339, 0x3e6674cc, 0x5673e864,
|
||||
0x0140502b, 0xad882043, 0x6ea1e46a, 0x34b5c0cb }}},
|
||||
{{{ 0x1d70aa7c, 0x29786814, 0x8cdbb8aa, 0x840ae3f9,
|
||||
0xbd4801fb, 0x78b4d622, 0xcf18ae9a, 0x6cf4e146 }}} },
|
||||
{ {{{ 0x36297168, 0x95c270ad, 0x942e7812, 0x2303ce80,
|
||||
0x0205cf0e, 0x71908cc2, 0x32bcd754, 0x0cc15edd }}},
|
||||
{{{ 0x2c7ded86, 0x1db94364, 0xf141b22c, 0xc694e39b,
|
||||
0x5e5a9312, 0xf22f64ef, 0x3c5e6155, 0x649b8859 }}} },
|
||||
{ {{{ 0xb6417945, 0x0d5611c6, 0xac306c97, 0x9643fdbf,
|
||||
0x0df500ff, 0xe81faaa4, 0x6f50e615, 0x0792c79b }}},
|
||||
{{{ 0xd2af8c8d, 0xb45bbc49, 0x84f51bfe, 0x16c615ab,
|
||||
0xc1d02d32, 0xdc57c526, 0x3c8aaa55, 0x5fb9a9a6 }}} },
|
||||
{ {{{ 0xdee40b98, 0x82faa8db, 0x6d520674, 0xff8a5208,
|
||||
0x446ac562, 0x1f8c510f, 0x2cc6b66e, 0x4676d381 }}},
|
||||
{{{ 0x2e7429f4, 0x8f1aa780, 0x8ed6bdf6, 0x2a95c1bf,
|
||||
0x457fa0eb, 0x051450a0, 0x744c57b1, 0x7d89e2b7 }}} },
|
||||
{ {{{ 0x3f95ea15, 0xb6bdacd2, 0x2f1a5d69, 0xc9a9d1b1,
|
||||
0xf4d22d72, 0xd4c2f1a9, 0x4dc516b5, 0x73ecfdf1 }}},
|
||||
{{{ 0x05391e08, 0xa1ce93cd, 0x7b8aac17, 0x98f1e99e,
|
||||
0xa098cbb3, 0x9ba84f2e, 0xf9bdd37a, 0x1425aa8b }}} },
|
||||
{ {{{ 0x966abfc0, 0x8a385bf4, 0xf081a640, 0x55e5e8bc,
|
||||
0xee26f5ff, 0x835dff85, 0xe509e1ea, 0x4927e622 }}},
|
||||
{{{ 0x352334b0, 0x164c8dbc, 0xa3fea31f, 0xcac1ad63,
|
||||
0x682fd457, 0x9b87a676, 0x1a53145f, 0x75f382ff }}} },
|
||||
{ {{{ 0xc3efcb46, 0x16b944f5, 0x68cb184c, 0x1fb55714,
|
||||
0x9ccf2dc8, 0xf1c2b116, 0x808283d8, 0x7417e00f }}},
|
||||
{{{ 0x930199ba, 0x1ea67a22, 0x718990d8, 0x9fbaf765,
|
||||
0x8f3d5d57, 0x231fc664, 0xe5853194, 0x38141a19 }}} },
|
||||
{ {{{ 0x2f81290d, 0xb9f00390, 0x04a9ca6c, 0x44877827,
|
||||
0xe1dbdd65, 0x65d7f9b9, 0xf7c6698a, 0x7133424c }}},
|
||||
{{{ 0xa7cd250f, 0x604cfb3c, 0x5acc18f3, 0x460c3c4b,
|
||||
0xb518e3eb, 0xa53e50e0, 0x98a40196, 0x2b4b9267 }}} },
|
||||
{ {{{ 0xc5dbd06c, 0x591b0672, 0xaa1eeb65, 0x10d43dca,
|
||||
0xcd2517af, 0x420cdef8, 0x0b695a8a, 0x513a307e }}},
|
||||
{{{ 0x66503215, 0xee9d6a7b, 0x088fd9a4, 0xdea58720,
|
||||
0x973afe12, 0x8f3cbbea, 0x872f2538, 0x005c2350 }}} },
|
||||
{ {{{ 0x35af3291, 0xe5024b70, 0x4f5e669a, 0x1d3eec2d,
|
||||
0x6e79d539, 0xc1f6d766, 0x795b5248, 0x34ec043f }}},
|
||||
{{{ 0x400960b6, 0xb2763511, 0x29e57df0, 0xff7a3d84,
|
||||
0x1666c1f1, 0xaeac7792, 0x66084bc0, 0x72426e97 }}} },
|
||||
{ {{{ 0x44f826ca, 0x5b1c3199, 0x790aa408, 0x68b00b73,
|
||||
0x69e9b92b, 0xaf0984b4, 0x3ffe9093, 0x5fe6736f }}},
|
||||
{{{ 0xffd49312, 0xd67f2889, 0x5cb9ed21, 0x3520d747,
|
||||
0x3c65a606, 0x94f893b1, 0x2d65496f, 0x2fee5e8c }}} }
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief X = k * G
|
||||
*
|
||||
* @param K scalar k
|
||||
*
|
||||
* Return -1 on error.
|
||||
* Return 0 on success.
|
||||
*/
|
||||
static void
|
||||
compute_kG_25519 (ac *X, const bn256 *K)
|
||||
{
|
||||
ptc Q[1];
|
||||
int i;
|
||||
|
||||
/* identity element */
|
||||
memset (Q, 0, sizeof (ptc));
|
||||
Q->y->word[0] = 1;
|
||||
Q->z->word[0] = 1;
|
||||
|
||||
for (i = 20; i >= 0; i--)
|
||||
{
|
||||
int k0, k1, k2;
|
||||
|
||||
k0 = ((K->word[0] >> i) & 1)
|
||||
| (i < 1 ? ((K->word[1] >> 30) & 2)
|
||||
: (((K->word[2] >> (i-1)) & 1) << 1))
|
||||
| (i < 2 ? ((K->word[3] >> (i+28)) & 4)
|
||||
: (((K->word[4] >> (i-2)) & 1) << 2))
|
||||
| (i < 3 ? ((K->word[5] >> (i+26)) & 8)
|
||||
: (((K->word[6] >> (i-3)) & 1) << 3));
|
||||
|
||||
k1 = (i < 11 ? ((K->word[0] >> (i+21)) & 1)
|
||||
: ((K->word[1] >> (i-11)) & 1))
|
||||
| (i < 12 ? ((K->word[2] >> (i+19)) & 2)
|
||||
: (((K->word[3] >> (i-12)) & 1) << 1))
|
||||
| (i < 13 ? ((K->word[4] >> (i+17)) & 4)
|
||||
: (((K->word[5] >> (i-13)) & 1) << 2))
|
||||
| (i < 14 ? ((K->word[6] >> (i+15)) & 8)
|
||||
: (((K->word[7] >> (i-14)) & 1) << 3));
|
||||
|
||||
k2 = ((K->word[1] >> (i+10)) & 1)
|
||||
| ((K->word[3] >> (i+8)) & 2)
|
||||
| ((K->word[5] >> (i+6)) & 4)
|
||||
| ((K->word[7] >> (i+4)) & 8);
|
||||
|
||||
point_double (Q, Q);
|
||||
point_add (Q, Q, &precomputed_KG[k0]);
|
||||
point_add (Q, Q, &precomputed_2E_KG[k1]);
|
||||
point_add (Q, Q, &precomputed_4E_KG[k2]);
|
||||
}
|
||||
|
||||
point_ptc_to_ac (X, Q);
|
||||
}
|
||||
|
||||
|
||||
#define BN416_WORDS 13
|
||||
#define BN128_WORDS 4
|
||||
|
||||
/* M: The order of the generator G. */
|
||||
static const bn256 M[1] = {
|
||||
{{ 0x5CF5D3ED, 0x5812631A, 0xA2F79CD6, 0x14DEF9DE,
|
||||
0x00000000, 0x00000000, 0x00000000, 0x10000000 }}
|
||||
};
|
||||
|
||||
#define C ((const uint32_t *)M)
|
||||
|
||||
static void
|
||||
bnX_mul_C (uint32_t *r, const uint32_t *q, int q_size)
|
||||
{
|
||||
int i, j, k;
|
||||
int i_beg, i_end;
|
||||
uint32_t r0, r1, r2;
|
||||
|
||||
r0 = r1 = r2 = 0;
|
||||
for (k = 0; k <= q_size + BN128_WORDS - 2; k++)
|
||||
{
|
||||
if (q_size < BN128_WORDS)
|
||||
if (k < q_size)
|
||||
{
|
||||
i_beg = 0;
|
||||
i_end = k;
|
||||
}
|
||||
else
|
||||
{
|
||||
i_beg = k - q_size + 1;
|
||||
i_end = k;
|
||||
if (i_end > BN128_WORDS - 1)
|
||||
i_end = BN128_WORDS - 1;
|
||||
}
|
||||
else
|
||||
if (k < BN128_WORDS)
|
||||
{
|
||||
i_beg = 0;
|
||||
i_end = k;
|
||||
}
|
||||
else
|
||||
{
|
||||
i_beg = k - BN128_WORDS + 1;
|
||||
i_end = k;
|
||||
if (i_end > q_size - 1)
|
||||
i_end = q_size - 1;
|
||||
}
|
||||
|
||||
for (i = i_beg; i <= i_end; i++)
|
||||
{
|
||||
uint64_t uv;
|
||||
uint32_t u, v;
|
||||
uint32_t carry;
|
||||
|
||||
j = k - i;
|
||||
if (q_size < BN128_WORDS)
|
||||
uv = ((uint64_t )q[j])*((uint64_t )C[i]);
|
||||
else
|
||||
uv = ((uint64_t )q[i])*((uint64_t )C[j]);
|
||||
v = uv;
|
||||
u = (uv >> 32);
|
||||
r0 += v;
|
||||
carry = (r0 < v);
|
||||
r1 += carry;
|
||||
carry = (r1 < carry);
|
||||
r1 += u;
|
||||
carry += (r1 < u);
|
||||
r2 += carry;
|
||||
}
|
||||
|
||||
r[k] = r0;
|
||||
r0 = r1;
|
||||
r1 = r2;
|
||||
r2 = 0;
|
||||
}
|
||||
|
||||
r[k] = r0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief R = A mod M (using M=2^252+C) (Barret reduction)
|
||||
*
|
||||
* See HAC 14.47 and 14.52.
|
||||
*/
|
||||
static void
|
||||
mod_reduce_M (bn256 *R, const bn512 *A)
|
||||
{
|
||||
uint32_t q[BN256_WORDS+1];
|
||||
uint32_t tmp[BN416_WORDS];
|
||||
bn256 r[1];
|
||||
uint32_t carry, next_carry;
|
||||
int i;
|
||||
#define borrow carry
|
||||
|
||||
q[8] = A->word[15]>>28;
|
||||
carry = A->word[15] & 0x0fffffff;
|
||||
for (i = BN256_WORDS - 1; i >= 0; i--)
|
||||
{
|
||||
next_carry = A->word[i+7] & 0x0fffffff;
|
||||
q[i] = (A->word[i+7] >> 28) | (carry << 4);
|
||||
carry = next_carry;
|
||||
}
|
||||
memcpy (R, A, sizeof (bn256));
|
||||
R->word[7] &= 0x0fffffff;
|
||||
|
||||
/* Q_size: 9 */
|
||||
bnX_mul_C (tmp, q, 9); /* TMP = Q*C */
|
||||
/* Q = tmp / 2^252 */
|
||||
carry = tmp[12] & 0x0fffffff;
|
||||
for (i = 4; i >= 0; i--)
|
||||
{
|
||||
next_carry = tmp[i+7] & 0x0fffffff;
|
||||
q[i] = (tmp[i+7] >> 28) | (carry << 4);
|
||||
carry = next_carry;
|
||||
}
|
||||
/* R' = tmp % 2^252 */
|
||||
memcpy (r, tmp, sizeof (bn256));
|
||||
r->word[7] &= 0x0fffffff;
|
||||
/* R -= R' */
|
||||
borrow = bn256_sub (R, R, r);
|
||||
if (borrow)
|
||||
bn256_add (R, R, M);
|
||||
else
|
||||
bn256_add ((bn256 *)tmp, R, M);
|
||||
|
||||
/* Q_size: 5 */
|
||||
bnX_mul_C (tmp, q, 5); /* TMP = Q*C */
|
||||
carry = tmp[8] & 0x0fffffff;
|
||||
q[0] = (tmp[7] >> 28) | (carry << 4);
|
||||
/* R' = tmp % 2^252 */
|
||||
memcpy (r, tmp, sizeof (bn256));
|
||||
r->word[7] &= 0x0fffffff;
|
||||
/* R += R' */
|
||||
bn256_add (R, R, r);
|
||||
borrow = bn256_sub (R, R, M);
|
||||
if (borrow)
|
||||
bn256_add (R, R, M);
|
||||
else
|
||||
bn256_add ((bn256 *)tmp, R, M);
|
||||
|
||||
/* Q_size: 1 */
|
||||
bnX_mul_C (tmp, q, 1); /* TMP = Q*C */
|
||||
/* R' = tmp % 2^252 */
|
||||
memset (((uint8_t *)r)+(sizeof (uint32_t)*5), 0, sizeof (uint32_t)*3);
|
||||
memcpy (r, tmp, sizeof (uint32_t)*5);
|
||||
/* R -= R' */
|
||||
borrow = bn256_sub (R, R, r);
|
||||
if (borrow)
|
||||
bn256_add (R, R, M);
|
||||
else
|
||||
bn256_add ((bn256 *)tmp, R, M);
|
||||
#undef borrow
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
eddsa_sign_25519 (const uint8_t *input, size_t ilen, uint32_t *out,
|
||||
const bn256 *a, const uint8_t *seed, const bn256 *pk)
|
||||
{
|
||||
bn256 *r, *s;
|
||||
mbedtls_sha512_context ctx;
|
||||
mbedtls_sha512_init(&ctx);
|
||||
uint8_t hash[64];
|
||||
bn256 tmp[1];
|
||||
ac R[1];
|
||||
uint32_t carry, borrow;
|
||||
|
||||
r = (bn256 *)out;
|
||||
s = (bn256 *)(out+(32/4));
|
||||
|
||||
mbedtls_sha512_starts (&ctx, 0);
|
||||
mbedtls_sha512_update (&ctx, seed, sizeof (bn256)); /* It's upper half of the hash */
|
||||
mbedtls_sha512_update (&ctx, input, ilen);
|
||||
mbedtls_sha512_finish (&ctx, hash);
|
||||
|
||||
mod_reduce_M (r, (bn512 *)hash);
|
||||
compute_kG_25519 (R, r);
|
||||
|
||||
/* EdDSA encoding. */
|
||||
memcpy (tmp, R->y, sizeof (bn256));
|
||||
tmp->word[7] ^= mod25519_is_neg (R->x) * 0x80000000;
|
||||
|
||||
mbedtls_sha512_starts (&ctx, 0);
|
||||
mbedtls_sha512_update (&ctx, (uint8_t *)tmp, sizeof (bn256));
|
||||
mbedtls_sha512_update (&ctx, (uint8_t *)pk, sizeof (bn256));
|
||||
mbedtls_sha512_update (&ctx, input, ilen);
|
||||
mbedtls_sha512_finish (&ctx, (uint8_t *)hash);
|
||||
|
||||
mod_reduce_M (s, (bn512 *)hash);
|
||||
bn256_mul ((bn512 *)hash, s, a);
|
||||
mod_reduce_M (s, (bn512 *)hash);
|
||||
carry = bn256_add (s, s, r);
|
||||
borrow = bn256_sub (s, s, M);
|
||||
|
||||
memcpy (r, tmp, sizeof (bn256));
|
||||
|
||||
if ((borrow && !carry))
|
||||
bn256_add (s, s, M);
|
||||
else
|
||||
bn256_add (tmp, s, M);
|
||||
|
||||
mbedtls_sha512_free (&ctx);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void
|
||||
eddsa_public_key_25519 (bn256 *pk, const bn256 *a)
|
||||
{
|
||||
ac R[1];
|
||||
ptc X[1];
|
||||
bn256 a0[1];
|
||||
|
||||
bn256_shift (a0, a, -3);
|
||||
compute_kG_25519 (R, a0);
|
||||
memcpy (X, R, sizeof (ac));
|
||||
memset (X->z, 0, sizeof (bn256));
|
||||
X->z->word[0] = 1;
|
||||
point_double (X, X);
|
||||
point_double (X, X);
|
||||
point_double (X, X);
|
||||
point_ptc_to_ac (R, X);
|
||||
/* EdDSA encoding. */
|
||||
memcpy (pk, R->y, sizeof (bn256));
|
||||
pk->word[7] ^= mod25519_is_neg (R->x) * 0x80000000;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
eddsa_compute_public_25519 (const uint8_t *kd, uint8_t *pubkey)
|
||||
{
|
||||
eddsa_public_key_25519 ((bn256 *)pubkey, (const bn256 *)kd);
|
||||
}
|
||||
|
||||
|
||||
#if 0
|
||||
/**
|
||||
* check if P is on the curve.
|
||||
*
|
||||
* Return -1 on error.
|
||||
* Return 0 on success.
|
||||
*/
|
||||
static int
|
||||
point_is_on_the_curve (const ac *P)
|
||||
{
|
||||
bn256 s[1], t[1];
|
||||
|
||||
/* Twisted Edwards curve: a*x^2 + y^2 = 1 + d*x^2*y^2 */
|
||||
}
|
||||
|
||||
int
|
||||
compute_kP_25519 (ac *X, const bn256 *K, const ac *P);
|
||||
#endif
|
||||
|
||||
#ifdef PRINT_OUT_TABLE
|
||||
static const ptc G[1] = {{
|
||||
{{{ 0x8f25d51a, 0xc9562d60, 0x9525a7b2, 0x692cc760,
|
||||
0xfdd6dc5c, 0xc0a4e231, 0xcd6e53fe, 0x216936d3 }}},
|
||||
{{{ 0x66666658, 0x66666666, 0x66666666, 0x66666666,
|
||||
0x66666666, 0x66666666, 0x66666666, 0x66666666 }}},
|
||||
{{{ 1, 0, 0, 0, 0, 0, 0, 0 }}},
|
||||
}};
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#ifdef TESTING_EDDSA
|
||||
static void
|
||||
print_bn256 (const bn256 *X)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 7; i >= 0; i--)
|
||||
printf ("%08x", X->word[i]);
|
||||
puts ("");
|
||||
}
|
||||
#endif
|
||||
|
||||
#if 0
|
||||
static void
|
||||
print_point (const ac *X)
|
||||
{
|
||||
int i;
|
||||
|
||||
#ifdef PRINT_OUT_TABLE_AS_C
|
||||
fputs (" { {{{ ", stdout);
|
||||
for (i = 0; i < 4; i++)
|
||||
printf ("0x%08x, ", X->x->word[i]);
|
||||
fputs ("\n ", stdout);
|
||||
for (; i < 7; i++)
|
||||
printf ("0x%08x, ", X->x->word[i]);
|
||||
printf ("0x%08x }}},\n", X->x->word[i]);
|
||||
fputs (" {{{ ", stdout);
|
||||
for (i = 0; i < 4; i++)
|
||||
printf ("0x%08x, ", X->y->word[i]);
|
||||
fputs ("\n ", stdout);
|
||||
for (; i < 7; i++)
|
||||
printf ("0x%08x, ", X->y->word[i]);
|
||||
printf ("0x%08x }}} },\n", X->y->word[i]);
|
||||
#else
|
||||
puts ("--");
|
||||
for (i = 7; i >= 0; i--)
|
||||
printf ("%08x", X->x->word[i]);
|
||||
puts ("");
|
||||
for (i = 7; i >= 0; i--)
|
||||
printf ("%08x", X->y->word[i]);
|
||||
puts ("");
|
||||
puts ("--");
|
||||
#endif
|
||||
}
|
||||
|
||||
static void
|
||||
print_point_ptc (const ptc *X)
|
||||
{
|
||||
int i;
|
||||
|
||||
puts ("---");
|
||||
for (i = 7; i >= 0; i--)
|
||||
printf ("%08x", X->x->word[i]);
|
||||
puts ("");
|
||||
for (i = 7; i >= 0; i--)
|
||||
printf ("%08x", X->y->word[i]);
|
||||
puts ("");
|
||||
for (i = 7; i >= 0; i--)
|
||||
printf ("%08x", X->z->word[i]);
|
||||
puts ("");
|
||||
puts ("---");
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#ifndef TESTING_EDDSA
|
||||
static void power_2 (ac *A, ptc *a, int N)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < N; i++)
|
||||
ed_double_25638 (a, a);
|
||||
ptc_to_ac_25519 (A, a);
|
||||
}
|
||||
|
||||
static void print_table (ac *a0001, ac *a0010, ac *a0100, ac *a1000)
|
||||
{
|
||||
int i;
|
||||
ptc a[1];
|
||||
ac x[1];
|
||||
|
||||
for (i = 1; i < 16; i++)
|
||||
{
|
||||
/* A := Identity Element */
|
||||
memset (a, 0, sizeof (ptc));
|
||||
a->y->word[0] = 1;
|
||||
a->z->word[0] = 1;
|
||||
|
||||
if ((i & 1))
|
||||
ed_add_25638 (a, a, a0001);
|
||||
if ((i & 2))
|
||||
ed_add_25638 (a, a, a0010);
|
||||
if ((i & 4))
|
||||
ed_add_25638 (a, a, a0100);
|
||||
if ((i & 8))
|
||||
ed_add_25638 (a, a, a1000);
|
||||
|
||||
ptc_to_ac_25519 (x, a);
|
||||
print_point (x);
|
||||
}
|
||||
|
||||
fputs ("\n", stdout);
|
||||
}
|
||||
|
||||
static void compute_and_print_table (ac *a0001, ac *a0010, ac *a0100, ac *a1000)
|
||||
{
|
||||
ptc a[1];
|
||||
|
||||
memcpy (a, a0001, sizeof (ac));
|
||||
memset (a->z, 0, sizeof (bn256));
|
||||
a->z->word[0] = 1;
|
||||
power_2 (a0010, a, 63);
|
||||
power_2 (a0100, a, 63);
|
||||
power_2 (a1000, a, 63);
|
||||
print_table (a0001, a0010, a0100, a1000);
|
||||
}
|
||||
#endif
|
||||
|
||||
int
|
||||
main (int argc, char *argv[])
|
||||
{
|
||||
#ifdef TESTING_EDDSA
|
||||
uint8_t hash[64];
|
||||
bn256 a[1];
|
||||
uint8_t r_s[64];
|
||||
bn256 pk[1];
|
||||
bn256 *r, *s;
|
||||
|
||||
const bn256 sk[1] = {
|
||||
{{ 0x9db1619d, 0x605afdef, 0xf44a84ba, 0xc42cec92,
|
||||
0x69c54944, 0x1969327b, 0x03ac3b70, 0x607fae1c }} };
|
||||
|
||||
const bn256 r_expected[1] = {
|
||||
{{ 0x004356e5, 0x72ac60c3, 0xcce28690, 0x8a826e80,
|
||||
0x1e7f8784, 0x74d9e5b8, 0x65e073d8, 0x55014922 }} };
|
||||
|
||||
const bn256 s_expected[1] = {
|
||||
{{ 0x1582b85f, 0xac3ba390, 0x70391ec6, 0x6bb4f91c,
|
||||
0xf0f55bd2, 0x24be5b59, 0x43415165, 0x0b107a8e }} };
|
||||
|
||||
r = (bn256 *)r_s;
|
||||
s = (bn256 *)(r_s+32);
|
||||
|
||||
sha512 ((uint8_t *)sk, sizeof (bn256), hash);
|
||||
hash[0] &= 248;
|
||||
hash[31] &= 127;
|
||||
hash[31] |= 64;
|
||||
memcpy (a, hash, sizeof (bn256));
|
||||
|
||||
eddsa_public_key_25519 (pk, a);
|
||||
eddsa_sign_25519 ((const uint8_t *)"", 0, r_s, a, hash+32, pk);
|
||||
|
||||
if (memcmp (r, r_expected, sizeof (bn256)) != 0
|
||||
|| memcmp (s, s_expected, sizeof (bn256)) != 0)
|
||||
{
|
||||
print_bn256 (r);
|
||||
print_bn256 (s);
|
||||
return 1;
|
||||
}
|
||||
#else
|
||||
ac a0001[1], a0010[1], a0100[1], a1000[1];
|
||||
ptc a[1];
|
||||
|
||||
memcpy (a, G, sizeof (ptc));
|
||||
ptc_to_ac_25519 (a0001, a);
|
||||
compute_and_print_table (a0001, a0010, a0100, a1000);
|
||||
|
||||
memcpy (a, a0001, sizeof (ac));
|
||||
memset (a->z, 0, sizeof (bn256));
|
||||
a->z->word[0] = 1;
|
||||
power_2 (a0001, a, 21);
|
||||
compute_and_print_table (a0001, a0010, a0100, a1000);
|
||||
|
||||
memcpy (a, a0001, sizeof (ac));
|
||||
memset (a->z, 0, sizeof (bn256));
|
||||
a->z->word[0] = 1;
|
||||
power_2 (a0001, a, 21);
|
||||
compute_and_print_table (a0001, a0010, a0100, a1000);
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
|
@ -1,824 +0,0 @@
|
|||
/* -*- coding: utf-8 -*-
|
||||
* ecc-ed448.c - Elliptic curve computation for
|
||||
* the twisted Edwards curve: -x^2 + y^2 = 1 + d*x^2*y^2
|
||||
* d = -39081
|
||||
*
|
||||
* Copyright (C) 2021 Free Software Initiative of Japan
|
||||
* Author: NIIBE Yutaka <gniibe@fsij.org>
|
||||
*
|
||||
* This file is a part of Gnuk, a GnuPG USB Token implementation.
|
||||
*
|
||||
* Gnuk is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Gnuk is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
|
||||
* License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* IMPLEMENTATION NOTE
|
||||
*
|
||||
* (0) We assume that the processor has no cache, nor branch target
|
||||
* prediction. Thus, we don't avoid indexing by secret value.
|
||||
* We don't avoid conditional jump if both cases have same timing,
|
||||
* either.
|
||||
*
|
||||
* (1) We use fixed base comb multiplication. Scalar is 448-bit.
|
||||
* We use two tables, and a table has 16 points.
|
||||
* Window size W = 4-bit, E = 56.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "p448.h"
|
||||
#include "shake256.h"
|
||||
|
||||
|
||||
#define C_WORDS 7
|
||||
#define BN448_WORDS 14
|
||||
#define BN690_WORDS 22
|
||||
#define BN896_WORDS 28
|
||||
#define BN912_WORDS 29 /* 28.5 */
|
||||
|
||||
typedef struct bn448 {
|
||||
uint32_t word[ BN448_WORDS ]; /* Little endian */
|
||||
} bn448;
|
||||
|
||||
typedef struct bn896 {
|
||||
uint32_t word[ BN896_WORDS ]; /* Little endian */
|
||||
} bn896;
|
||||
|
||||
typedef struct bn912 {
|
||||
uint32_t word[ BN912_WORDS ]; /* Little endian */
|
||||
} bn912;
|
||||
|
||||
static const bn448 M[1] = {{{
|
||||
0xab5844f3, 0x2378c292, 0x8dc58f55, 0x216cc272,
|
||||
0xaed63690, 0xc44edb49, 0x7cca23e9, 0xffffffff,
|
||||
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
|
||||
0xffffffff, 0x3fffffff
|
||||
}}};
|
||||
|
||||
static const uint32_t C[C_WORDS] = {
|
||||
0x54a7bb0d, 0xdc873d6d, 0x723a70aa, 0xde933d8d,
|
||||
0x5129c96f, 0x3bb124b6, 0x8335dc16
|
||||
};
|
||||
|
||||
|
||||
static uint32_t
|
||||
bn448_add (bn448 *X, const bn448 *A, const bn448 *B)
|
||||
{
|
||||
int i;
|
||||
uint32_t v;
|
||||
uint32_t carry = 0;
|
||||
uint32_t *px;
|
||||
const uint32_t *pa, *pb;
|
||||
|
||||
px = X->word;
|
||||
pa = A->word;
|
||||
pb = B->word;
|
||||
|
||||
for (i = 0; i < BN448_WORDS; i++)
|
||||
{
|
||||
v = *pb;
|
||||
*px = *pa + carry;
|
||||
carry = (*px < carry);
|
||||
*px += v;
|
||||
carry += (*px < v);
|
||||
px++;
|
||||
pa++;
|
||||
pb++;
|
||||
}
|
||||
|
||||
return carry;
|
||||
}
|
||||
|
||||
static uint32_t
|
||||
bn448_sub (bn448 *X, const bn448 *A, const bn448 *B)
|
||||
{
|
||||
int i;
|
||||
uint32_t v;
|
||||
uint32_t borrow = 0;
|
||||
uint32_t *px;
|
||||
const uint32_t *pa, *pb;
|
||||
|
||||
px = X->word;
|
||||
pa = A->word;
|
||||
pb = B->word;
|
||||
|
||||
for (i = 0; i < BN448_WORDS; i++)
|
||||
{
|
||||
uint32_t borrow0 = (*pa < borrow);
|
||||
|
||||
v = *pb;
|
||||
*px = *pa - borrow;
|
||||
borrow = (*px < v) + borrow0;
|
||||
*px -= v;
|
||||
px++;
|
||||
pa++;
|
||||
pb++;
|
||||
}
|
||||
|
||||
return borrow;
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
bnX_mul_C (uint32_t *r, const uint32_t *q, int q_size)
|
||||
{
|
||||
int i, j, k;
|
||||
int i_beg, i_end;
|
||||
uint32_t r0, r1, r2;
|
||||
|
||||
r0 = r1 = r2 = 0;
|
||||
for (k = 0; k <= q_size + C_WORDS - 2; k++)
|
||||
{
|
||||
if (q_size < C_WORDS)
|
||||
if (k < q_size)
|
||||
{
|
||||
i_beg = 0;
|
||||
i_end = k;
|
||||
}
|
||||
else
|
||||
{
|
||||
i_beg = k - q_size + 1;
|
||||
i_end = k;
|
||||
if (i_end > C_WORDS - 1)
|
||||
i_end = C_WORDS - 1;
|
||||
}
|
||||
else
|
||||
if (k < C_WORDS)
|
||||
{
|
||||
i_beg = 0;
|
||||
i_end = k;
|
||||
}
|
||||
else
|
||||
{
|
||||
i_beg = k - C_WORDS + 1;
|
||||
i_end = k;
|
||||
if (i_end > q_size - 1)
|
||||
i_end = q_size - 1;
|
||||
}
|
||||
|
||||
for (i = i_beg; i <= i_end; i++)
|
||||
{
|
||||
uint64_t uv;
|
||||
uint32_t u, v;
|
||||
uint32_t carry;
|
||||
|
||||
j = k - i;
|
||||
if (q_size < C_WORDS)
|
||||
uv = ((uint64_t)q[j])*((uint64_t)C[i]);
|
||||
else
|
||||
uv = ((uint64_t)q[i])*((uint64_t)C[j]);
|
||||
v = uv;
|
||||
u = (uv >> 32);
|
||||
r0 += v;
|
||||
carry = (r0 < v);
|
||||
r1 += carry;
|
||||
carry = (r1 < carry);
|
||||
r1 += u;
|
||||
carry += (r1 < u);
|
||||
r2 += carry;
|
||||
}
|
||||
|
||||
r[k] = r0;
|
||||
r0 = r1;
|
||||
r1 = r2;
|
||||
r2 = 0;
|
||||
}
|
||||
|
||||
r[k] = r0;
|
||||
}
|
||||
|
||||
/* X <= X + A when COND!=0 */
|
||||
/* X <= X when COND==0 */
|
||||
static void
|
||||
bn448_add_cond (bn448 *X, const bn448 *A, int cond)
|
||||
{
|
||||
int i;
|
||||
uint32_t v;
|
||||
uint32_t carry = 0;
|
||||
uint32_t *px;
|
||||
const uint32_t *pa;
|
||||
uint32_t mask = -(!!cond);
|
||||
|
||||
px = X->word;
|
||||
pa = A->word;
|
||||
|
||||
for (i = 0; i < BN448_WORDS; i++)
|
||||
{
|
||||
v = *px;
|
||||
*px = (*pa & mask) + carry;
|
||||
carry = (*px < carry);
|
||||
*px += v;
|
||||
carry += (*px < v);
|
||||
px++;
|
||||
pa++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* X <= X + A mod M */
|
||||
static void
|
||||
bn448_addm (bn448 *X, const bn448 *A)
|
||||
{
|
||||
uint32_t borrow;
|
||||
|
||||
bn448_add (X, X, A);
|
||||
borrow = bn448_sub (X, X, M);
|
||||
bn448_add_cond (X, M, borrow);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief R = A mod M (using M=2^446-C) (Barret reduction)
|
||||
*
|
||||
* See HAC 14.47.
|
||||
*/
|
||||
void
|
||||
mod_reduce_M (bn448 *R, const bn912 *A)
|
||||
{
|
||||
uint32_t q[BN448_WORDS+1];
|
||||
uint32_t tmp[BN690_WORDS];
|
||||
bn448 r[1];
|
||||
uint32_t carry, next_carry;
|
||||
int i;
|
||||
|
||||
/* Q = A / 2^446 *//* 466-bit */
|
||||
/* Upper half of A->word[28] must be zero. */
|
||||
q[14] = (A->word[28] << 2) | (A->word[27] >> 30);
|
||||
carry = A->word[27] & 0x3fffffff;
|
||||
for (i = BN448_WORDS - 1; i >= 0; i--)
|
||||
{
|
||||
next_carry = A->word[i+13] & 0x3fffffff;
|
||||
q[i] = (A->word[i+13] >> 30) | (carry << 2);
|
||||
carry = next_carry;
|
||||
}
|
||||
memcpy (R, A, sizeof (bn448));
|
||||
R->word[13] &= 0x3fffffff;
|
||||
|
||||
/* Q_size: 15 *//* 466-bit */
|
||||
bnX_mul_C (tmp, q, 15); /* TMP = Q*C *//* 690-bit */
|
||||
/* Q = tmp / 2^446 *//* 244-bit */
|
||||
carry = tmp[21];
|
||||
for (i = 7; i >= 0; i--)
|
||||
{
|
||||
next_carry = tmp[i+13] & 0x3fffffff;
|
||||
q[i] = (tmp[i+13] >> 30) | (carry << 2);
|
||||
carry = next_carry;
|
||||
}
|
||||
/* R' = tmp % 2^446 */
|
||||
memcpy (r, tmp, sizeof (bn448));
|
||||
r->word[13] &= 0x3fffffff;
|
||||
/* R += R' */
|
||||
bn448_addm (R, r);
|
||||
|
||||
/* Q_size: 8 *//* 244-bit */
|
||||
bnX_mul_C (tmp, q, 8); /* TMP = Q*C *//* 468-bit */
|
||||
/* Q = tmp / 2^446 *//* 22-bit */
|
||||
carry = tmp[14];
|
||||
q[0] = (tmp[13] >> 30) | (carry << 2);
|
||||
/* R' = tmp % 2^446 */
|
||||
memcpy (r, tmp, sizeof (bn448));
|
||||
r->word[13] &= 0x3fffffff;
|
||||
/* R += R' */
|
||||
bn448_addm (R, r);
|
||||
|
||||
/* Q_size: 1 */
|
||||
bnX_mul_C (tmp, q, 1); /* TMP = Q*C *//* 246-bit */
|
||||
/* R' = tmp % 2^446 */
|
||||
memset (((uint8_t *)r)+(sizeof (uint32_t)*8), 0, sizeof (uint32_t)*6);
|
||||
memcpy (r, tmp, sizeof (uint32_t)*8);
|
||||
/* R += R' */
|
||||
bn448_addm (R, r);
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
bn448_mul (bn896 *X, const bn448 *A, const bn448 *B)
|
||||
{
|
||||
int i, j, k;
|
||||
int i_beg, i_end;
|
||||
uint32_t r0, r1, r2;
|
||||
|
||||
r0 = r1 = r2 = 0;
|
||||
for (k = 0; k <= (BN448_WORDS - 1)*2; k++)
|
||||
{
|
||||
if (k < BN448_WORDS)
|
||||
{
|
||||
i_beg = 0;
|
||||
i_end = k;
|
||||
}
|
||||
else
|
||||
{
|
||||
i_beg = k - BN448_WORDS + 1;
|
||||
i_end = BN448_WORDS - 1;
|
||||
}
|
||||
|
||||
for (i = i_beg; i <= i_end; i++)
|
||||
{
|
||||
uint64_t uv;
|
||||
uint32_t u, v;
|
||||
uint32_t carry;
|
||||
|
||||
j = k - i;
|
||||
|
||||
uv = ((uint64_t )A->word[i])*((uint64_t )B->word[j]);
|
||||
v = uv;
|
||||
u = (uv >> 32);
|
||||
r0 += v;
|
||||
carry = (r0 < v);
|
||||
r1 += carry;
|
||||
carry = (r1 < carry);
|
||||
r1 += u;
|
||||
carry += (r1 < u);
|
||||
r2 += carry;
|
||||
}
|
||||
|
||||
X->word[k] = r0;
|
||||
r0 = r1;
|
||||
r1 = r2;
|
||||
r2 = 0;
|
||||
}
|
||||
|
||||
X->word[k] = r0;
|
||||
}
|
||||
|
||||
static const p448_t nGx0[16] = {
|
||||
{ { 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00000000, 0x00000000, 0x00000000 } },
|
||||
{ { 0x070cc05e, 0x026a82bc, 0x00938e26, 0x080e18b0,
|
||||
0x0511433b, 0x0f72ab66, 0x0412ae1a, 0x0a3d3a46,
|
||||
0x0a6de324, 0x00f1767e, 0x04657047, 0x036da9e1,
|
||||
0x05a622bf, 0x0ed221d1, 0x066bed0d, 0x04f1970c } },
|
||||
{ { 0x0464238e, 0x00079817, 0x00d381ca, 0x02110302,
|
||||
0x0d9f01b5, 0x01cc4c6e, 0x05a131b1, 0x05e35dc5,
|
||||
0x006944eb, 0x0b61848d, 0x029631a3, 0x083792a0,
|
||||
0x0afca0dd, 0x0be1017f, 0x0782fcbb, 0x070aaa01 } },
|
||||
{ { 0x0e7661f9, 0x0b2f9f62, 0x009fae89, 0x03b99803,
|
||||
0x066014d2, 0x067900ef, 0x06556c10, 0x0c8eacf3,
|
||||
0x0ad4a82e, 0x020a44d0, 0x00572f1c, 0x0e7819e7,
|
||||
0x0fd08cdf, 0x0c0ed140, 0x09aee1da, 0x0a16934a } },
|
||||
{ { 0x091780c7, 0x0a7ea989, 0x0d2476b6, 0x004e4ecc,
|
||||
0x0c494b68, 0x00af9f58, 0x0dee64fd, 0x0e0f269f,
|
||||
0x0021bd26, 0x085a61f6, 0x0b5d284b, 0x0c265c35,
|
||||
0x03775afd, 0x058755ea, 0x02ecf2c6, 0x0617f174 } },
|
||||
{ { 0x067f4947, 0x0dbf4eb6, 0x0b8716d9, 0x02206a2a,
|
||||
0x0e7cad5a, 0x04a148b0, 0x0e483133, 0x0fbf12cd,
|
||||
0x0c6458f7, 0x0e022d5a, 0x01b7e39d, 0x0a60afe6,
|
||||
0x05a5208c, 0x0c62f458, 0x03311553, 0x0a08a4c3 } },
|
||||
{ { 0x0054a90d, 0x0ad5dc54, 0x00ac9fd6, 0x097f2af4,
|
||||
0x0f4ddbc7, 0x01b0f7b3, 0x0324ce0b, 0x01d5d092,
|
||||
0x0cd2798f, 0x08cb96e2, 0x0957bc39, 0x0bd045b5,
|
||||
0x0f76fbfb, 0x046308a9, 0x0ef679ce, 0x0c86d628 } },
|
||||
{ { 0x0d5d9262, 0x0f251539, 0x0711a956, 0x0240708f,
|
||||
0x04a0b0bc, 0x07f7e4dd, 0x055b70a8, 0x065dd24f,
|
||||
0x07ef8979, 0x0e83cec7, 0x09589db8, 0x0f1db2d1,
|
||||
0x09d93037, 0x0fcc7e8a, 0x04e0b8f4, 0x0cb99f0b } },
|
||||
{ { 0x04acea57, 0x06f24100, 0x0da68597, 0x0dace1c6,
|
||||
0x050ce77f, 0x0ea7dd41, 0x01585884, 0x01aecb84,
|
||||
0x0ea4a85c, 0x092ff208, 0x088eebd2, 0x0de9433c,
|
||||
0x03f4d289, 0x053cd318, 0x026539af, 0x03970858 } },
|
||||
{ { 0x0d229665, 0x06e9fd2b, 0x0878dd51, 0x049345aa,
|
||||
0x0f45bacf, 0x0ccde72a, 0x0be16b6f, 0x0bc249d1,
|
||||
0x0448a61d, 0x0a25bae9, 0x0d773878, 0x0c93b6ea,
|
||||
0x02cda508, 0x055f708a, 0x08cf49e6, 0x0fa56852 } },
|
||||
{ { 0x093bfef9, 0x07bec8db, 0x0fafda3d, 0x0ce4dcdc,
|
||||
0x06f62ed7, 0x0a75c872, 0x07b3dadd, 0x0c39ac92,
|
||||
0x0f926d90, 0x0ae1b8d1, 0x048da0a9, 0x0d7dbeca,
|
||||
0x02a52b3b, 0x0ec13f74, 0x0d4c5ce2, 0x02071cee } },
|
||||
{ { 0x05a644a6, 0x0e56b0a9, 0x0be6360b, 0x01ecf90e,
|
||||
0x023b73a8, 0x0c3bbcf7, 0x0292054b, 0x05417d25,
|
||||
0x07b91b46, 0x0ca1ea05, 0x07ea6c44, 0x01560b21,
|
||||
0x04f12989, 0x0463cd2a, 0x03d7e086, 0x0092781c } },
|
||||
{ { 0x0d59796d, 0x0ce08d7e, 0x055bc822, 0x0e464443,
|
||||
0x0d243cc4, 0x0542002f, 0x098259b3, 0x044fc576,
|
||||
0x012781de, 0x08650550, 0x0055e6b4, 0x0137f762,
|
||||
0x0fbf007e, 0x0a391ccc, 0x039fe6f6, 0x0a9c9ad3 } },
|
||||
{ { 0x01ca2765, 0x0ccddbb0, 0x0563b46c, 0x05d18f4c,
|
||||
0x0462647e, 0x02ff700d, 0x0822dc83, 0x0670b143,
|
||||
0x00013963, 0x01627d78, 0x055dbfb9, 0x0435f413,
|
||||
0x063d41e8, 0x066c95cd, 0x0c797bba, 0x08e27dfb } },
|
||||
{ { 0x03da4531, 0x01ff4dd6, 0x0cd39a3c, 0x02d0de4c,
|
||||
0x0bc9da8d, 0x0003561e, 0x033e1e9a, 0x001eea00,
|
||||
0x078bf710, 0x05458c53, 0x0f56338e, 0x069043ab,
|
||||
0x061ffba0, 0x0637cf41, 0x039fb551, 0x0fc09757 } },
|
||||
{ { 0x0256141f, 0x0f1e0e38, 0x00ab2673, 0x0efd5f47,
|
||||
0x0af4a4af, 0x0b749116, 0x0ac6540b, 0x04242f82,
|
||||
0x0abaf195, 0x0b26730c, 0x0d06842d, 0x076fbe60,
|
||||
0x0580cad8, 0x02613d91, 0x0b568ae0, 0x0c2e5b1d } }
|
||||
};
|
||||
|
||||
static const p448_t nGy0[16] = {
|
||||
{ { 0x00000001, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00000000, 0x00000000, 0x00000000 } },
|
||||
{ { 0x0230fa14, 0x008795bf, 0x07c8ad98, 0x0132c4ed,
|
||||
0x09c4fdbd, 0x01ce67c3, 0x073ad3ff, 0x005a0c2d,
|
||||
0x07789c1e, 0x0a398408, 0x0a73736c, 0x0c7624be,
|
||||
0x003756c9, 0x02488762, 0x016eb6bc, 0x0693f467 } },
|
||||
{ { 0x099945e7, 0x0c63b7a0, 0x0c4486c1, 0x0e9164ec,
|
||||
0x0885f2c1, 0x0b133e35, 0x0c99ae02, 0x0186f0d3,
|
||||
0x02bf53e6, 0x02fca492, 0x048a02bc, 0x0f922aa2,
|
||||
0x00dd3dca, 0x04fe6490, 0x0f6a8207, 0x0e8c313f } },
|
||||
{ { 0x0579a4e2, 0x0a1ffe8b, 0x0ce472b4, 0x01d006b3,
|
||||
0x089def96, 0x07c8f689, 0x0a32ae93, 0x079d7bd1,
|
||||
0x03a02760, 0x0ebb4776, 0x05b4c55e, 0x019b3c6c,
|
||||
0x07da436f, 0x066ff782, 0x0659536d, 0x0ee40076 } },
|
||||
{ { 0x05ec556a, 0x050109e2, 0x0fd57e39, 0x0235366b,
|
||||
0x044b6b2e, 0x07b3c976, 0x0b2b7b9c, 0x0f7f9e82,
|
||||
0x00ec6409, 0x0b6196ab, 0x00a20d9e, 0x088f1d16,
|
||||
0x0586f761, 0x0e3be3b4, 0x0e26395d, 0x09983c26 } },
|
||||
{ { 0x0fab8e56, 0x0ded288e, 0x057277e6, 0x0a4e6f4e,
|
||||
0x0e949681, 0x0a2a4c4f, 0x0721fdb3, 0x0508a46c,
|
||||
0x0fb44de2, 0x0f98049e, 0x02fb0f31, 0x071f3724,
|
||||
0x09067763, 0x0d3fbbb3, 0x0a83faaa, 0x0696ec4a } },
|
||||
{ { 0x07a04bb0, 0x0f52ae70, 0x0ae14cdb, 0x0784d14b,
|
||||
0x034acc37, 0x09aa3869, 0x09703f7b, 0x08f79c87,
|
||||
0x0264026c, 0x0859cde5, 0x0486b035, 0x0b2a45f7,
|
||||
0x03d5144b, 0x0809740f, 0x0416dc87, 0x0dcf324d } },
|
||||
{ { 0x0a0c8bc7, 0x04125cec, 0x0eac3f20, 0x0d30ff7e,
|
||||
0x029ad678, 0x06901f05, 0x04805ff1, 0x033c307d,
|
||||
0x049d6a79, 0x080f0710, 0x02dece6c, 0x0d1ba22b,
|
||||
0x0778cccb, 0x01692a0b, 0x02df78fb, 0x0f8c02d3 } },
|
||||
{ { 0x0b827d87, 0x04b57599, 0x03d77638, 0x0dc82ac0,
|
||||
0x052f6e61, 0x06943366, 0x0ad5e8a6, 0x0b8fc4b0,
|
||||
0x0f388642, 0x01b6f7dc, 0x0a74dd57, 0x06f24533,
|
||||
0x041750cf, 0x0c669378, 0x028a37af, 0x006757eb } },
|
||||
{ { 0x080128d5, 0x0ef186a8, 0x04a54843, 0x01ceb43b,
|
||||
0x045be148, 0x0c112a42, 0x01ac9412, 0x0621b93a,
|
||||
0x05e16552, 0x0a2ca24f, 0x086301c0, 0x0cf3fecf,
|
||||
0x05c2e2e0, 0x05108805, 0x09e9d8ab, 0x0d2ba341 } },
|
||||
{ { 0x02138911, 0x0f0d3e4c, 0x0c1a371b, 0x062382ce,
|
||||
0x05b3a392, 0x09d954e7, 0x0517d2a1, 0x0047d71a,
|
||||
0x07f70073, 0x09cd1733, 0x0efc3aea, 0x0549d0d1,
|
||||
0x0df78457, 0x0666e074, 0x0a48e084, 0x0f67e924 } },
|
||||
{ { 0x0b3114fe, 0x073bec50, 0x0e8b6172, 0x01c5e7b6,
|
||||
0x0e896bcc, 0x0a1c3ae1, 0x0bcd8cab, 0x0bb3f870,
|
||||
0x07e9fa9d, 0x0eea8546, 0x0042e2cf, 0x056431f0,
|
||||
0x0469e8d2, 0x08eb9b9c, 0x0a9adf2c, 0x06856458 } },
|
||||
{ { 0x07b2cfdd, 0x01855530, 0x073bd43a, 0x01816246,
|
||||
0x08897062, 0x02f82d12, 0x03563816, 0x06517857,
|
||||
0x0394a8c7, 0x0529bf2e, 0x075a3141, 0x0660c4f2,
|
||||
0x018e5a16, 0x0787c8ad, 0x045b679e, 0x0abaec01 } },
|
||||
{ { 0x06d87d9e, 0x07c9fabb, 0x03b2a99d, 0x0673b28a,
|
||||
0x068816ee, 0x0efb205e, 0x0dd5e3d5, 0x03d21920,
|
||||
0x07544f4d, 0x085f40c2, 0x06fb538d, 0x057d045b,
|
||||
0x05470e4e, 0x028a93c3, 0x063adfd4, 0x0d1cf7a5 } },
|
||||
{ { 0x06699694, 0x0c83c837, 0x0386dade, 0x0621103f,
|
||||
0x0f247dc3, 0x06058f43, 0x0aec07c3, 0x0b1ac29a,
|
||||
0x0bde5d50, 0x06e35e33, 0x078fd31c, 0x0516263c,
|
||||
0x00a9d127, 0x04a13379, 0x078bec6e, 0x0f39316a } },
|
||||
{ { 0x0e26ea19, 0x05ecf40e, 0x03bdf1b5, 0x07c284a0,
|
||||
0x06f461fa, 0x08393462, 0x064a69aa, 0x07d4f6a5,
|
||||
0x06e88ea4, 0x023059e9, 0x0f92bd0b, 0x0c4a8035,
|
||||
0x0c5c44a2, 0x0fccec22, 0x07f57ea1, 0x0598207c } }
|
||||
};
|
||||
|
||||
static const p448_t nGx1[16] = {
|
||||
{ { 0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00000000, 0x00000000, 0x00000000 } },
|
||||
{ { 0x0528af6f, 0x078c6f13, 0x094b74d9, 0x00001fe2,
|
||||
0x001aab44, 0x0ae77425, 0x0ef0039c, 0x07cbe937,
|
||||
0x00fa2a67, 0x0af3e4f0, 0x0da1378e, 0x0e28175f,
|
||||
0x08ccd90e, 0x072adeed, 0x000af22f, 0x016a8ce1 } },
|
||||
{ { 0x0fa0459e, 0x0f31f53f, 0x0315cd6b, 0x0f8742a1,
|
||||
0x0ae64e97, 0x0abe2f50, 0x09b9da48, 0x0bd78741,
|
||||
0x051e526e, 0x04521a33, 0x0e10ba45, 0x0fa05935,
|
||||
0x0e8f903c, 0x05c947e1, 0x05a754ee, 0x00aa47d1 } },
|
||||
{ { 0x00d9a33b, 0x0284f76f, 0x0e4d41e7, 0x09461141,
|
||||
0x0cc79344, 0x015371b9, 0x03dd8bdd, 0x0173f667,
|
||||
0x053f866b, 0x0c0d0f83, 0x030b45ea, 0x08b7d59b,
|
||||
0x0044dc82, 0x02b4cdec, 0x094fa772, 0x0e245b21 } },
|
||||
{ { 0x04ddc8a8, 0x02fe182d, 0x0ac056bf, 0x088d6e79,
|
||||
0x00e41e4e, 0x0c3ff2d1, 0x02c3679f, 0x032ec7f9,
|
||||
0x04e61051, 0x03561f09, 0x06c6250a, 0x04553f5a,
|
||||
0x0dd25c5b, 0x02b765ef, 0x06a1cd7f, 0x0e3a40a2 } },
|
||||
{ { 0x05e1f4b2, 0x0e9485c4, 0x070a1e6b, 0x01d85e53,
|
||||
0x077730a7, 0x0db61fa9, 0x050d418e, 0x0201a6bd,
|
||||
0x02774433, 0x0e78a475, 0x0622ea3a, 0x016424e5,
|
||||
0x0d5b9631, 0x01c7734d, 0x0f5064f2, 0x0c7586d3 } },
|
||||
{ { 0x0af6151d, 0x0c3ed603, 0x0aa19b93, 0x05a5e4a6,
|
||||
0x0536ff03, 0x07e465ce, 0x0b0be710, 0x0bbb36bf,
|
||||
0x09249bff, 0x0d15454d, 0x03736654, 0x0ba934d9,
|
||||
0x0370dc86, 0x0675c04e, 0x0d86eb3b, 0x06cd21cb } },
|
||||
{ { 0x030c7ce7, 0x04217221, 0x0e9dba4d, 0x0ec314cd,
|
||||
0x05439062, 0x0d7196cd, 0x0dd96166, 0x0b8295cd,
|
||||
0x0c15796f, 0x0c767da7, 0x00ab2036, 0x059120e7,
|
||||
0x0b7d07ec, 0x0e1562a9, 0x0231cdd9, 0x07d5c89f } },
|
||||
{ { 0x01a82a12, 0x091a5884, 0x080f3a62, 0x0a754175,
|
||||
0x0f73417a, 0x0399009f, 0x00a8c5cd, 0x02db1fb9,
|
||||
0x0c046d51, 0x082c8912, 0x08f18274, 0x00a3f577,
|
||||
0x026ccae2, 0x02ad0ede, 0x08a4e9c2, 0x07d6bd8b } },
|
||||
{ { 0x0afd28b4, 0x02b7b7be, 0x0298d67e, 0x0e834401,
|
||||
0x04b11493, 0x0e070d60, 0x063ce6fb, 0x04b67725,
|
||||
0x0a0cfb04, 0x0d3a0f67, 0x0f08f1b2, 0x0debe82e,
|
||||
0x0b402b9e, 0x07114482, 0x0b307043, 0x0af532e6 } },
|
||||
{ { 0x049ab457, 0x0f6483c2, 0x0818ac81, 0x05aced0a,
|
||||
0x0a900e3a, 0x080916bc, 0x02948675, 0x0145adb9,
|
||||
0x0d8b7821, 0x04fe2b0e, 0x0b1a62cc, 0x0a9e1bce,
|
||||
0x096c2408, 0x048f1f80, 0x0ac552fe, 0x0d17e7a0 } },
|
||||
{ { 0x08ce3344, 0x0ea48915, 0x0434ae70, 0x0c6cf019,
|
||||
0x0c48f5d2, 0x089d3c0f, 0x0ca7aa7e, 0x0c550a00,
|
||||
0x017fb3ab, 0x09f8b49f, 0x024844a0, 0x0366a6d5,
|
||||
0x0ceb4a83, 0x0f1f5bf4, 0x03b782f0, 0x099fd2f7 } },
|
||||
{ { 0x052daf76, 0x038fbbd7, 0x0bced01d, 0x0ffb0a8b,
|
||||
0x07c6bd6c, 0x0dc3b0ff, 0x041d595c, 0x03814ee7,
|
||||
0x01941d44, 0x0e1f8343, 0x0f89b18d, 0x0c083601,
|
||||
0x0e52ec62, 0x0fc338ff, 0x0e971788, 0x04601008 } },
|
||||
{ { 0x0add862e, 0x0e8c3a8e, 0x033cea23, 0x06d00cf1,
|
||||
0x0cdc039a, 0x0d7bda40, 0x0e0a2ac3, 0x04750dcb,
|
||||
0x0bec4388, 0x0a1bb0bc, 0x0d20c0f9, 0x077a4a7b,
|
||||
0x0b9e1f0b, 0x02ff072d, 0x07bd3e06, 0x0bd796d7 } },
|
||||
{ { 0x08e321b4, 0x08757de1, 0x0151699c, 0x06ba6bd4,
|
||||
0x0a156df0, 0x02ec93a1, 0x0dad4f9e, 0x04e547c5,
|
||||
0x0ee9310d, 0x01dcc8bf, 0x0f7b5016, 0x0355f710,
|
||||
0x0ce8f36d, 0x0389d7a9, 0x02b8056d, 0x0ff83804 } },
|
||||
{ { 0x060f6dcf, 0x0dcaa234, 0x0285b23d, 0x0ec8d56f,
|
||||
0x083dac2b, 0x01042255, 0x08e1bed7, 0x0c3fe788,
|
||||
0x0832c0af, 0x07258b0e, 0x02b2affc, 0x0a901bdb,
|
||||
0x0038f36e, 0x01a28d5f, 0x0dbb618d, 0x080838af } }
|
||||
};
|
||||
|
||||
static const p448_t nGy1[16] = {
|
||||
{ { 0x00000001, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
||||
0x00000000, 0x00000000, 0x00000000, 0x00000000 } },
|
||||
{ { 0x0cbf63dd, 0x069fae17, 0x09e39e26, 0x06786172,
|
||||
0x0f827a18, 0x0e92b3d5, 0x08403682, 0x04d75e41,
|
||||
0x09056a79, 0x001a4fd9, 0x020008f5, 0x089efb2d,
|
||||
0x0b78ff15, 0x0a2f6918, 0x0a3437f5, 0x0f41c870 } },
|
||||
{ { 0x0d814825, 0x0b2849ef, 0x05c9968d, 0x09c2a5d2,
|
||||
0x004e634c, 0x024dbb26, 0x0db38194, 0x033f3a4c,
|
||||
0x0c8a2b6b, 0x0e04f609, 0x0abbbfdb, 0x0caefd8e,
|
||||
0x0404498b, 0x0683119a, 0x08b21cbd, 0x024ab7a9 } },
|
||||
{ { 0x0ede77b3, 0x0043b728, 0x0a043f1d, 0x003cf736,
|
||||
0x0ab4e700, 0x0d95a612, 0x0c8fe17c, 0x05ccaac2,
|
||||
0x0177bd28, 0x0dc3bd14, 0x05360c86, 0x0b3d5c96,
|
||||
0x04ec7e48, 0x01880c26, 0x04bb47c6, 0x0fd5dba8 } },
|
||||
{ { 0x05d821dd, 0x0b27309b, 0x0c2c17ca, 0x0950fb8d,
|
||||
0x08fb0d4c, 0x0feed015, 0x0f550179, 0x0762c479,
|
||||
0x0e095840, 0x0306cf44, 0x0d379e66, 0x084b413a,
|
||||
0x0bb2e4f1, 0x0d6e5d5a, 0x094b085d, 0x08bc12b7 } },
|
||||
{ { 0x0b8a16f6, 0x0b4dacd9, 0x003afc96, 0x0000b9b9,
|
||||
0x03f19cbf, 0x0ab930b8, 0x0b077171, 0x0541f92e,
|
||||
0x019baa42, 0x08758d9c, 0x0fea31a2, 0x0299b935,
|
||||
0x081d9e24, 0x03bc7232, 0x09d91676, 0x0fc081c2 } },
|
||||
{ { 0x02f05282, 0x04ca6fb6, 0x02e9801e, 0x051928b6,
|
||||
0x0b609dcb, 0x0c6f37b6, 0x06e32803, 0x06617fd7,
|
||||
0x0166f0bb, 0x07d1bffb, 0x0ac137d4, 0x0bfdebdd,
|
||||
0x0df8f3cb, 0x0d558ac9, 0x08fabbb4, 0x00217c7c } },
|
||||
{ { 0x0f5d72ad, 0x04c71050, 0x008880dd, 0x093209a0,
|
||||
0x07c3fef0, 0x0e1857c5, 0x022b21d2, 0x07584709,
|
||||
0x0e52fe8a, 0x039aeffa, 0x0a384e66, 0x0bd7c58b,
|
||||
0x0bfbbfe2, 0x022fc035, 0x0506e447, 0x0bc96411 } },
|
||||
{ { 0x04b3de44, 0x0aa0d797, 0x096ac9bb, 0x0f8658b9,
|
||||
0x05f6c334, 0x031e7be2, 0x04df12c9, 0x023836ce,
|
||||
0x059eb5c9, 0x0029027b, 0x05b8649d, 0x02f22531,
|
||||
0x0d907162, 0x0a0fdf03, 0x09e80226, 0x0101d9df } },
|
||||
{ { 0x05237b19, 0x00d0c997, 0x04a2bcdb, 0x0692bae3,
|
||||
0x0805b9e0, 0x0a0d3a98, 0x08c7dd07, 0x0a253f11,
|
||||
0x0e19738e, 0x0c0794d0, 0x019812a1, 0x041a8569,
|
||||
0x025d360c, 0x078e4ebd, 0x07ee8567, 0x0f02e9d6 } },
|
||||
{ { 0x00548584, 0x0bb1ee61, 0x0549030f, 0x0026e17a,
|
||||
0x0b4c52fb, 0x0a4e4e61, 0x0a1ca8f9, 0x0339754c,
|
||||
0x0ee8806f, 0x03d2a45e, 0x0e2028fa, 0x03c44782,
|
||||
0x0072e42b, 0x03328ae4, 0x0d21c91f, 0x07e98738 } },
|
||||
{ { 0x0b9618ad, 0x07f781fa, 0x09cf7662, 0x0855bfab,
|
||||
0x0c316a14, 0x0d98f9ff, 0x07b3046a, 0x0109f273,
|
||||
0x042cecfe, 0x0cc21cdc, 0x05be5a36, 0x05236b10,
|
||||
0x058a0700, 0x0ff2cf95, 0x005ad57d, 0x09cbf152 } },
|
||||
{ { 0x0ebe90d2, 0x049f0de4, 0x02243779, 0x0221424d,
|
||||
0x09051808, 0x0b52f44b, 0x0bb9c3fb, 0x0a5d64e3,
|
||||
0x07690354, 0x0d8bf65d, 0x0bc06e3f, 0x05d039f6,
|
||||
0x033a3443, 0x04e11c79, 0x04147a83, 0x06a7e42c } },
|
||||
{ { 0x082e4773, 0x00d276be, 0x0e1b9057, 0x0e9dd324,
|
||||
0x0369bc97, 0x0b3181ef, 0x002f04fa, 0x01d08726,
|
||||
0x07c2c5d3, 0x0bf49cbf, 0x09ecb59b, 0x098eae7e,
|
||||
0x02e09293, 0x052e08b6, 0x0c40f3e6, 0x04096c37 } },
|
||||
{ { 0x06074e1f, 0x07bc94ed, 0x0790175a, 0x040b2a81,
|
||||
0x0e307782, 0x0b7958e8, 0x089ff273, 0x07ed27c6,
|
||||
0x026db869, 0x0b6a32f8, 0x03d2e15c, 0x00446ef9,
|
||||
0x0777e1ac, 0x0492d2de, 0x01b69b63, 0x06b8dbab } },
|
||||
{ { 0x07e98bea, 0x0e7c9e7a, 0x02e17335, 0x09302c64,
|
||||
0x0acc1e93, 0x05dcdcd8, 0x04d90baa, 0x05982bae,
|
||||
0x0c686ed6, 0x07c08c6c, 0x0fce2c72, 0x04dd3cce,
|
||||
0x01dc8f12, 0x029ca465, 0x0161cbd7, 0x09324c0a } }
|
||||
};
|
||||
|
||||
static void
|
||||
compute_kG_448 (uint8_t *out, const uint32_t k[16])
|
||||
{
|
||||
int i;
|
||||
p448_t x0[1], y0[1], z0[1]; /* P0 */
|
||||
p448_t tmp0[1], tmp1[1];
|
||||
|
||||
/* P0 <= O */
|
||||
memset (x0, 0, sizeof (p448_t));
|
||||
memset (y0, 0, sizeof (p448_t));
|
||||
memset (z0, 0, sizeof (p448_t));
|
||||
y0->limb[0] = 1;
|
||||
z0->limb[0] = 1;
|
||||
|
||||
for (i = 0; i < 56; i++)
|
||||
{
|
||||
p448_t b[1], c[1], d[1];
|
||||
p448_t e[1], f[1], g[1], h[1];
|
||||
int index0, index1;
|
||||
|
||||
if (i < 28)
|
||||
{
|
||||
int i0 = 28 - i - 1;
|
||||
|
||||
index0 = ((k[1] >> i0) & 1) | (((k[5] >> i0) & 1)<<1)
|
||||
| (((k[ 9] >> i0) & 1)<<2) | (((k[13] >> i0) & 1)<<3);
|
||||
index1 = ((k[3] >> i0) & 1) | (((k[7] >> i0) & 1)<<1)
|
||||
| (((k[11] >> i0) & 1)<<2) | (((k[15] >> i0) & 1)<<3);
|
||||
}
|
||||
else
|
||||
{
|
||||
int i0 = 56 - i - 1;
|
||||
|
||||
index0 = ((k[0] >> i0) & 1) | (((k[4] >> i0) & 1)<<1)
|
||||
| (((k[ 8] >> i0) & 1)<<2) | (((k[12] >> i0) & 1)<<3);
|
||||
index1 = ((k[2] >> i0) & 1) | (((k[6] >> i0) & 1)<<1)
|
||||
| (((k[10] >> i0) & 1)<<2) | (((k[14] >> i0) & 1)<<3);
|
||||
}
|
||||
|
||||
/* Point double P0' <= P0 + P0 */
|
||||
p448_add (tmp0, x0, y0);
|
||||
p448_sqr (b, tmp0);
|
||||
p448_sqr (c, x0);
|
||||
p448_sqr (d, y0);
|
||||
p448_add (e, c, d);
|
||||
p448_sqr (h, z0);
|
||||
p448_add (tmp0, h, h);
|
||||
p448_sub (tmp1, e, tmp0);
|
||||
p448_sub (tmp0, b, e);
|
||||
p448_mul (x0, tmp0, tmp1);
|
||||
p448_sub (tmp0, c, d);
|
||||
p448_mul (y0, e, tmp0);
|
||||
p448_mul (z0, e, tmp1);
|
||||
/*
|
||||
B = (X1+Y1)^2
|
||||
C = X1^2
|
||||
D = Y1^2
|
||||
E = C+D
|
||||
H = Z1^2
|
||||
J = E-2*H
|
||||
X3 = (B-E)*J
|
||||
Y3 = E*(C-D)
|
||||
Z3 = E*J
|
||||
*/
|
||||
|
||||
/* Point addition P0' <= P0 + [v0(index0)]G */
|
||||
p448_sqr (b, z0);
|
||||
p448_mul (c, x0, &nGx0[index0]);
|
||||
p448_mul (d, y0, &nGy0[index0]);
|
||||
p448_mul (tmp0, c, d);
|
||||
p448_mul_39081 (e, tmp0);
|
||||
p448_add (f, b, e);
|
||||
p448_sub (g, b, e);
|
||||
p448_add (tmp0, x0, y0);
|
||||
p448_add (tmp1, &nGx0[index0], &nGy0[index0]);
|
||||
p448_mul (h, tmp0, tmp1);
|
||||
p448_sub (tmp0, h, c);
|
||||
p448_sub (tmp1, tmp0, d);
|
||||
p448_mul (tmp0, f, tmp1);
|
||||
p448_mul (x0, z0, tmp0);
|
||||
p448_sub (tmp0, d, c);
|
||||
p448_mul (tmp1, g, tmp0);
|
||||
p448_mul (y0, z0, tmp1);
|
||||
p448_mul (z0, f, g);
|
||||
/*
|
||||
A = Z1*Z2
|
||||
B = A^2
|
||||
C = X1*X2
|
||||
D = Y1*Y2
|
||||
E = d*C*D
|
||||
F = B-E
|
||||
G = B+E
|
||||
H = (X1+Y1)*(X2+Y2)
|
||||
X3 = A*F*(H-C-D)
|
||||
Y3 = A*G*(D-C)
|
||||
Z3 = F*G
|
||||
*/
|
||||
/* Point addition P0' <= P0 + [v1(index1)]G */
|
||||
p448_sqr (b, z0);
|
||||
p448_mul (c, x0, &nGx1[index1]);
|
||||
p448_mul (d, y0, &nGy1[index1]);
|
||||
p448_mul (tmp0, c, d);
|
||||
p448_mul_39081 (e, tmp0);
|
||||
p448_add (f, b, e);
|
||||
p448_sub (g, b, e);
|
||||
p448_add (tmp0, x0, y0);
|
||||
p448_add (tmp1, &nGx1[index1], &nGy1[index1]);
|
||||
p448_mul (h, tmp0, tmp1);
|
||||
p448_sub (tmp0, h, c);
|
||||
p448_sub (tmp1, tmp0, d);
|
||||
p448_mul (tmp0, f, tmp1);
|
||||
p448_mul (x0, z0, tmp0);
|
||||
p448_sub (tmp0, d, c);
|
||||
p448_mul (tmp1, g, tmp0);
|
||||
p448_mul (y0, z0, tmp1);
|
||||
p448_mul (z0, f, g);
|
||||
}
|
||||
|
||||
/* Convert to affine coordinate. */
|
||||
p448_inv (tmp0, z0);
|
||||
p448_mul (tmp1, x0, tmp0);
|
||||
p448_serialize (out, tmp1);
|
||||
/* EdDSA encoding. */
|
||||
out[56] = (out[0] & 1) << 7;
|
||||
p448_mul (tmp1, y0, tmp0);
|
||||
p448_serialize (out, tmp1);
|
||||
}
|
||||
|
||||
|
||||
#define SEED_SIZE 57
|
||||
|
||||
#define DOM448 (const uint8_t *)"SigEd448"
|
||||
#define DOM448_LEN 8
|
||||
|
||||
int
|
||||
ed448_sign (uint8_t *out, const uint8_t *input, unsigned int ilen,
|
||||
const uint8_t *a_in, const uint8_t *seed, const uint8_t *pk)
|
||||
{
|
||||
bn448 a[1], k[1], s[1];
|
||||
shake_context ctx;
|
||||
const unsigned char x_olen[2] = { 0, 0 };
|
||||
uint32_t hash[BN912_WORDS];
|
||||
uint8_t r[57];
|
||||
uint32_t carry, borrow;
|
||||
p448_t k_redundant[1];
|
||||
|
||||
memset (hash, 0, sizeof (hash));
|
||||
|
||||
memcpy (a, a_in, sizeof (bn448));
|
||||
a->word[13] |= 0x80000000;
|
||||
a->word[0] &= ~3;
|
||||
|
||||
shake256_start (&ctx);
|
||||
shake256_update (&ctx, DOM448, DOM448_LEN);
|
||||
shake256_update (&ctx, x_olen, 2);
|
||||
shake256_update (&ctx, seed, 57);
|
||||
shake256_update (&ctx, input, ilen);
|
||||
shake256_finish (&ctx, (uint8_t *)hash, 2*57);
|
||||
|
||||
mod_reduce_M (k, (const bn912 *)hash);
|
||||
p448_deserialize (k_redundant, (uint8_t *)k);
|
||||
compute_kG_448 (r, (uint32_t *)k_redundant);
|
||||
|
||||
shake256_start (&ctx);
|
||||
shake256_update (&ctx, DOM448, DOM448_LEN);
|
||||
shake256_update (&ctx, x_olen, 2);
|
||||
shake256_update (&ctx, r, 57);
|
||||
shake256_update (&ctx, pk, 57);
|
||||
shake256_update (&ctx, input, ilen);
|
||||
shake256_finish (&ctx, (uint8_t *)hash, 2*57);
|
||||
|
||||
mod_reduce_M (s, (const bn912 *)hash);
|
||||
|
||||
memset (hash, 0, sizeof (hash));
|
||||
bn448_mul ((bn896 *)hash, s, a);
|
||||
mod_reduce_M (s, (const bn912 *)hash);
|
||||
|
||||
carry = bn448_add (s, s, k);
|
||||
borrow = bn448_sub (s, s, M);
|
||||
bn448_add_cond (s, M, (borrow && !carry));
|
||||
|
||||
memcpy (out, r, 57);
|
||||
memcpy (out+57, s, 56);
|
||||
out[114-1] = 0;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
ed448_compute_public (uint8_t *pk, const uint8_t *a_in)
|
||||
{
|
||||
p448_t a[1];
|
||||
|
||||
p448_deserialize (a, a_in);
|
||||
a->limb[15] |= 0x08000000;
|
||||
a->limb[0] &= ~3;
|
||||
|
||||
compute_kG_448 (pk, (uint32_t *)a);
|
||||
}
|
||||
|
|
@ -1,226 +0,0 @@
|
|||
/* -*- coding: utf-8 -*-
|
||||
* ecc-mont.c - Elliptic curve computation for
|
||||
* the Montgomery curve: y^2 = x^3 + 486662*x^2 + x.
|
||||
*
|
||||
* Copyright (C) 2014, 2015, 2017 Free Software Initiative of Japan
|
||||
* Author: NIIBE Yutaka <gniibe@fsij.org>
|
||||
*
|
||||
* This file is a part of Gnuk, a GnuPG USB Token implementation.
|
||||
*
|
||||
* Gnuk is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Gnuk is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
|
||||
* License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include "bn.h"
|
||||
#include "mod25638.h"
|
||||
#include "mod.h"
|
||||
|
||||
/*
|
||||
* References:
|
||||
*
|
||||
* [1] D. J. Bernstein. Curve25519: new Diffie-Hellman speed records.
|
||||
* Proceedings of PKC 2006, to appear.
|
||||
* http://cr.yp.to/papers.html#curve25519. Date: 2006.02.09.
|
||||
*
|
||||
* [2] D. J. Bernstein. Can we avoid tests for zero in fast
|
||||
* elliptic-curve arithmetic?
|
||||
* http://cr.yp.to/papers.html#curvezero. Date: 2006.07.26.
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* IMPLEMENTATION NOTE
|
||||
*
|
||||
* (0) We assume that the processor has no cache, nor branch target
|
||||
* prediction. Thus, we don't avoid indexing by secret value.
|
||||
* We don't avoid conditional jump if both cases have same timing,
|
||||
* either.
|
||||
*
|
||||
* (1) We use Radix-32 field arithmetic. It's a representation like
|
||||
* 2^256-38, but it's more redundant. For example, "1" can be
|
||||
* represented in three ways in 256-bit: 1, 2^255-18, and
|
||||
* 2^256-37.
|
||||
*
|
||||
* (2) We use Montgomery double-and-add.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef BN256_C_IMPLEMENTATION
|
||||
#define ASM_IMPLEMENTATION 0
|
||||
#endif
|
||||
/*
|
||||
*
|
||||
* 121665 = 0x1db41
|
||||
* 1 1101 1011 0100 0001
|
||||
*/
|
||||
static void
|
||||
mod25638_mul_121665 (bn256 *x, const bn256 *a)
|
||||
{
|
||||
#if ASM_IMPLEMENTATION
|
||||
#include "muladd_256.h"
|
||||
const uint32_t *s;
|
||||
uint32_t *d;
|
||||
uint32_t w;
|
||||
uint32_t c;
|
||||
|
||||
s = a->word;
|
||||
d = x->word;
|
||||
memset (d, 0, sizeof (bn256));
|
||||
w = 121665;
|
||||
MULADD_256_ASM (s, d, w, c);
|
||||
#else
|
||||
uint32_t c, c1;
|
||||
bn256 m[1];
|
||||
|
||||
c = c1 = bn256_shift (m, a, 6); c += bn256_add (x, a, m);
|
||||
c1 <<= 2; c1 |= bn256_shift (m, m, 2); c = c + c1 + bn256_add (x, x, m);
|
||||
c1 <<= 1; c1 |= bn256_shift (m, m, 1); c = c + c1 + bn256_add (x, x, m);
|
||||
c1 <<= 2; c1 |= bn256_shift (m, m, 2); c = c + c1 + bn256_add (x, x, m);
|
||||
c1 <<= 1; c1 |= bn256_shift (m, m, 1); c = c + c1 + bn256_add (x, x, m);
|
||||
c1 <<= 2; c1 |= bn256_shift (m, m, 2); c = c + c1 + bn256_add (x, x, m);
|
||||
c1 <<= 1; c1 |= bn256_shift (m, m, 1); c = c + c1 + bn256_add (x, x, m);
|
||||
c1 <<= 1; c1 |= bn256_shift (m, m, 1); c = c + c1 + bn256_add (x, x, m);
|
||||
#endif
|
||||
c = bn256_add_uint (x, x, c*38);
|
||||
x->word[0] += c * 38;
|
||||
}
|
||||
|
||||
|
||||
typedef struct
|
||||
{
|
||||
bn256 x[1];
|
||||
bn256 z[1];
|
||||
} pt;
|
||||
|
||||
|
||||
/**
|
||||
* @brief Process Montgomery double-and-add
|
||||
*
|
||||
* With Q0, Q1, DIF (= Q0 - Q1), compute PRD = 2Q0, SUM = Q0 + Q1
|
||||
* Q0 and Q1 are clobbered.
|
||||
*
|
||||
*/
|
||||
static void
|
||||
mont_d_and_a (pt *prd, pt *sum, pt *q0, pt *q1, const bn256 *dif_x)
|
||||
{
|
||||
mod25638_add (sum->x, q1->x, q1->z);
|
||||
mod25638_sub (q1->z, q1->x, q1->z);
|
||||
mod25638_add (prd->x, q0->x, q0->z);
|
||||
mod25638_sub (q0->z, q0->x, q0->z);
|
||||
mod25638_mul (q1->x, q0->z, sum->x);
|
||||
mod25638_mul (q1->z, prd->x, q1->z);
|
||||
mod25638_sqr (q0->x, prd->x);
|
||||
mod25638_sqr (q0->z, q0->z);
|
||||
mod25638_add (sum->x, q1->x, q1->z);
|
||||
mod25638_sub (q1->z, q1->x, q1->z);
|
||||
mod25638_mul (prd->x, q0->x, q0->z);
|
||||
mod25638_sub (q0->z, q0->x, q0->z);
|
||||
mod25638_sqr (sum->x, sum->x);
|
||||
mod25638_sqr (sum->z, q1->z);
|
||||
mod25638_mul_121665 (prd->z, q0->z);
|
||||
mod25638_mul (sum->z, sum->z, dif_x);
|
||||
mod25638_add (prd->z, q0->x, prd->z);
|
||||
mod25638_mul (prd->z, prd->z, q0->z);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief RES = x-coordinate of [n]Q
|
||||
*
|
||||
* @param N Scalar N (three least significant bits are 000)
|
||||
* @param Q_X x-coordinate of Q
|
||||
*
|
||||
*/
|
||||
static void
|
||||
compute_nQ (bn256 *res, const bn256 *n, const bn256 *q_x)
|
||||
{
|
||||
int i, j;
|
||||
pt p0[1], p1[1], p0_[1], p1_[1];
|
||||
|
||||
/* P0 = O = (1:0) */
|
||||
memset (p0->x, 0, sizeof (bn256));
|
||||
p0->x->word[0] = 1;
|
||||
memset (p0->z, 0, sizeof (bn256));
|
||||
|
||||
/* P1 = (X:1) */
|
||||
memcpy (p1->x, q_x, sizeof (bn256));
|
||||
memset (p1->z, 0, sizeof (bn256));
|
||||
p1->z->word[0] = 1;
|
||||
|
||||
for (i = 0; i < 8; i++)
|
||||
{
|
||||
uint32_t u = n->word[7-i];
|
||||
|
||||
for (j = 0; j < 16; j++)
|
||||
{
|
||||
pt *q0, *q1;
|
||||
pt *sum_n, *prd_n;
|
||||
|
||||
if ((u & 0x80000000))
|
||||
q0 = p1, q1 = p0, sum_n = p0_, prd_n = p1_;
|
||||
else
|
||||
q0 = p0, q1 = p1, sum_n = p1_, prd_n = p0_;
|
||||
mont_d_and_a (prd_n, sum_n, q0, q1, q_x);
|
||||
|
||||
if ((u & 0x40000000))
|
||||
q0 = p1_, q1 = p0_, sum_n = p0, prd_n = p1;
|
||||
else
|
||||
q0 = p0_, q1 = p1_, sum_n = p1, prd_n = p0;
|
||||
mont_d_and_a (prd_n, sum_n, q0, q1, q_x);
|
||||
|
||||
u <<= 2;
|
||||
}
|
||||
}
|
||||
|
||||
/* We know the LSB of N is always 0. Thus, result is always in P0. */
|
||||
/*
|
||||
* p0->z may be zero here, but our mod_inv doesn't raise error for 0,
|
||||
* but returns 0 (like the implementation of z^(p-2)), thus, RES will
|
||||
* be 0 in that case, which is correct value.
|
||||
*/
|
||||
mod_inv (res, p0->z, p25519);
|
||||
mod25638_mul (res, res, p0->x);
|
||||
mod25519_reduce (res);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
ecdh_compute_public_25519 (const uint8_t *key_data, uint8_t *pubkey)
|
||||
{
|
||||
bn256 gx[1];
|
||||
bn256 k[1];
|
||||
|
||||
memset (gx, 0, sizeof (bn256));
|
||||
gx[0].word[0] = 9; /* Gx = 9 */
|
||||
memcpy (k, key_data, sizeof (bn256));
|
||||
|
||||
compute_nQ ((bn256 *)pubkey, k, gx);
|
||||
}
|
||||
|
||||
int
|
||||
ecdh_decrypt_curve25519 (const uint8_t *input, uint8_t *output,
|
||||
const uint8_t *key_data)
|
||||
{
|
||||
bn256 q_x[1];
|
||||
bn256 k[1];
|
||||
bn256 shared[1];
|
||||
|
||||
memcpy (q_x, input, sizeof (bn256));
|
||||
memcpy (k, key_data, sizeof (bn256));
|
||||
compute_nQ (shared, k, q_x);
|
||||
memcpy (output, shared, sizeof (bn256));
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -1,177 +0,0 @@
|
|||
/* -*- coding: utf-8 -*-
|
||||
* ecc-x448.c - Elliptic curve computation for
|
||||
* the Montgomery curve: y^2 = x^3 + 156326*x^2 + x
|
||||
*
|
||||
* Copyright (C) 2021 Free Software Initiative of Japan
|
||||
* Author: NIIBE Yutaka <gniibe@fsij.org>
|
||||
*
|
||||
* This file is a part of Gnuk, a GnuPG USB Token implementation.
|
||||
*
|
||||
* Gnuk is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Gnuk is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
|
||||
* License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* IMPLEMENTATION NOTE
|
||||
*
|
||||
* (0) We assume that the processor has no cache, nor branch target
|
||||
* prediction.
|
||||
* We don't avoid conditional jump if both cases have same timing,
|
||||
* either.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "p448.h"
|
||||
|
||||
#define N_LIMBS 14
|
||||
|
||||
/**
|
||||
* @brief Process Montgomery double-and-add
|
||||
*
|
||||
* With Q0, Q1, DIF (= Q0 - Q1), compute PRD = 2Q0 into Q0,
|
||||
* and computute SUM = Q0 + Q1 into Q1
|
||||
*
|
||||
*/
|
||||
static void
|
||||
mont_d_and_a (p448_t q0_x[1], p448_t q0_z[1], p448_t q1_x[1], p448_t q1_z[1],
|
||||
const p448_t dif_x[1])
|
||||
{
|
||||
p448_t reg0[1], reg1[1];
|
||||
#define c reg0
|
||||
#define d reg1
|
||||
#define a q1_x
|
||||
#define b q1_z
|
||||
#define cb q0_x
|
||||
#define da reg0
|
||||
#define aa reg1
|
||||
#define bb q0_z
|
||||
#define da_plus_cb q1_z
|
||||
#define da_minus_cb q1_x
|
||||
#define e reg0
|
||||
#define dacb_2 q0_z
|
||||
#define a24_e q1_x
|
||||
#define aa_ aa /* override is allowed by p448_add */
|
||||
|
||||
p448_add (c, q1_x, q1_z);
|
||||
p448_sub (d, q1_x, q1_z);
|
||||
p448_add (a, q0_x, q0_z);
|
||||
p448_sub (b, q0_x, q0_z);
|
||||
p448_mul (cb, c, b);
|
||||
p448_mul (da, d, a);
|
||||
p448_sqr (aa, a);
|
||||
p448_sqr (bb, b);
|
||||
p448_add (da_plus_cb, da, cb);
|
||||
p448_sub (da_minus_cb, da, cb);
|
||||
p448_mul (q0_x, aa, bb);
|
||||
p448_sub (e, aa, bb);
|
||||
p448_sqr (dacb_2, da_minus_cb);
|
||||
p448_mul_39081 (a24_e, e);
|
||||
p448_add (aa_, aa, a24_e);
|
||||
p448_sqr (q1_x, da_plus_cb);
|
||||
p448_mul (q1_z, dacb_2, dif_x);
|
||||
p448_mul (q0_z, e, aa_);
|
||||
}
|
||||
|
||||
|
||||
typedef struct
|
||||
{
|
||||
p448_t x[1];
|
||||
p448_t z[1];
|
||||
} pt;
|
||||
|
||||
|
||||
/**
|
||||
* @brief RES = x-coordinate of [n]Q
|
||||
*
|
||||
* @param N Scalar N (three least significant bits are 00)
|
||||
* @param Q_X x-coordinate of Q
|
||||
*
|
||||
*/
|
||||
static void
|
||||
compute_nQ (uint8_t *res, const uint32_t n[N_LIMBS], const p448_t q_x[1])
|
||||
{
|
||||
int i, j;
|
||||
pt p0[1], p1[1];
|
||||
#define tmp0 p0->z
|
||||
#define tmp1 p1->z
|
||||
|
||||
/* P0 = O = (1:0) */
|
||||
memset (p0->x, 0, sizeof (p0->x));
|
||||
p0->x->limb[0] = 1;
|
||||
memset (p0->z, 0, sizeof (p0->z));
|
||||
|
||||
/* P1 = (X:1) */
|
||||
memcpy (p1->x, q_x, N_REDUNDANT_LIMBS*4);
|
||||
memset (p1->z, 0, sizeof (p1->z));
|
||||
p1->z->limb[0] = 1;
|
||||
|
||||
for (i = 0; i < N_LIMBS; i++)
|
||||
{
|
||||
uint32_t u = n[N_LIMBS-i-1];
|
||||
|
||||
for (j = 0; j < 32; j++)
|
||||
{
|
||||
p448_t *q0_x, *q0_z, *q1_x, *q1_z;
|
||||
|
||||
if ((u & 0x80000000))
|
||||
q0_x = p1->x, q0_z = p1->z, q1_x = p0->x, q1_z = p0->z;
|
||||
else
|
||||
q0_x = p0->x, q0_z = p0->z, q1_x = p1->x, q1_z = p1->z;
|
||||
mont_d_and_a (q0_x, q0_z, q1_x, q1_z, q_x);
|
||||
|
||||
u <<= 1;
|
||||
}
|
||||
}
|
||||
|
||||
/* We know the LSB of N is always 0. Thus, result is always in P0. */
|
||||
/*
|
||||
* p0->z may be zero here, but our inverse function doesn't raise
|
||||
* error for 0, but returns 0, thus, RES will be 0 in that case,
|
||||
* which is correct value.
|
||||
*/
|
||||
p448_inv (tmp1, p0->z);
|
||||
p448_mul (tmp0, tmp1, p0->x);
|
||||
p448_serialize (res, tmp0);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
ecdh_compute_public_x448 (uint8_t *pubkey, const uint8_t *key_data)
|
||||
{
|
||||
const p448_t gx[1] = { { { 5, 0, }, } };
|
||||
uint32_t k[N_LIMBS];
|
||||
|
||||
memcpy (k, key_data, N_LIMBS*4);
|
||||
k[0] &= ~3;
|
||||
k[N_LIMBS-1] |= 0x80000000;
|
||||
compute_nQ (pubkey, k, gx);
|
||||
}
|
||||
|
||||
int
|
||||
ecdh_decrypt_x448 (uint8_t *output, const uint8_t *input,
|
||||
const uint8_t *key_data)
|
||||
{
|
||||
p448_t q_x[1];
|
||||
uint32_t k[N_LIMBS];
|
||||
|
||||
p448_deserialize (q_x, input);
|
||||
memcpy (k, key_data, N_LIMBS*4);
|
||||
k[0] &= ~3;
|
||||
k[N_LIMBS-1] |= 0x80000000;
|
||||
compute_nQ (output, k, q_x);
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -1,398 +0,0 @@
|
|||
/* -*- coding: utf-8 -*-
|
||||
* ecc.c - Elliptic curve over GF(prime)
|
||||
*
|
||||
* Copyright (C) 2011, 2013, 2014, 2015
|
||||
* Free Software Initiative of Japan
|
||||
* Author: NIIBE Yutaka <gniibe@fsij.org>
|
||||
*
|
||||
* This file is a part of Gnuk, a GnuPG USB Token implementation.
|
||||
*
|
||||
* Gnuk is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Gnuk is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
|
||||
* License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* References:
|
||||
*
|
||||
* [1] Suite B Implementer's Guide to FIPS 186-3 (ECDSA), February 3, 2010.
|
||||
*
|
||||
* [2] Michael Brown, Darrel Hankerson, Julio López, and Alfred Menezes,
|
||||
* Software Implementation of the NIST Elliptic Curves Over Prime Fields,
|
||||
* Proceedings of the 2001 Conference on Topics in Cryptology: The
|
||||
* Cryptographer's Track at RSA
|
||||
* Pages 250-265, Springer-Verlag London, UK, 2001
|
||||
* ISBN:3-540-41898-9
|
||||
*
|
||||
* [3] Mustapha Hedabou, Pierre Pinel, Lucien Bénéteau,
|
||||
* A comb method to render ECC resistant against Side Channel Attacks,
|
||||
* 2004
|
||||
*/
|
||||
|
||||
#include "field-group-select.h"
|
||||
|
||||
/*
|
||||
* Coefficients
|
||||
*/
|
||||
/*
|
||||
* static const bn256 *coefficient_a;
|
||||
* static const bn256 *coefficient_b;
|
||||
*/
|
||||
/*
|
||||
* N: order of G
|
||||
*/
|
||||
/*
|
||||
* static const bn256 N[1];
|
||||
*/
|
||||
/*
|
||||
* MU = 2^512 / N
|
||||
* MU = ( (1 << 256) | MU_lower )
|
||||
*/
|
||||
/*
|
||||
* static const bn256 MU_lower[1];
|
||||
*/
|
||||
|
||||
/*
|
||||
* w = 4
|
||||
* m = 256
|
||||
* d = 64
|
||||
* e = 32
|
||||
*/
|
||||
|
||||
/*
|
||||
* static const ac precomputed_KG[15];
|
||||
* static const ac precomputed_2E_KG[15];
|
||||
*/
|
||||
|
||||
#if TEST
|
||||
/*
|
||||
* Generator of Elliptic curve over GF(p256)
|
||||
*/
|
||||
const ac *G = &precomputed_KG[0];
|
||||
#endif
|
||||
|
||||
|
||||
static int
|
||||
get_vk (const bn256 *K, int i)
|
||||
{
|
||||
uint32_t w0, w1, w2, w3;
|
||||
|
||||
if (i < 32)
|
||||
{
|
||||
w3 = K->word[6]; w2 = K->word[4]; w1 = K->word[2]; w0 = K->word[0];
|
||||
}
|
||||
else
|
||||
{
|
||||
w3 = K->word[7]; w2 = K->word[5]; w1 = K->word[3]; w0 = K->word[1];
|
||||
i -= 32;
|
||||
}
|
||||
|
||||
w3 >>= i; w2 >>= i; w1 >>= i; w0 >>= i;
|
||||
return ((w3 & 1) << 3) | ((w2 & 1) << 2) | ((w1 & 1) << 1) | (w0 & 1);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief X = k * G
|
||||
*
|
||||
* @param K scalar k
|
||||
*
|
||||
* Return -1 on error.
|
||||
* Return 0 on success.
|
||||
*/
|
||||
int
|
||||
FUNC(compute_kG) (ac *X, const bn256 *K)
|
||||
{
|
||||
uint8_t index[64]; /* Lower 4-bit for index absolute value, msb is
|
||||
for sign (encoded as: 0 means 1, 1 means -1). */
|
||||
bn256 K_dash[1];
|
||||
jpc Q[1], tmp[1], *dst;
|
||||
int i;
|
||||
int vk;
|
||||
uint32_t k_is_even = bn256_is_even (K);
|
||||
|
||||
bn256_sub_uint (K_dash, K, k_is_even);
|
||||
/* It keeps the condition: 1 <= K' <= N - 2, and K' is odd. */
|
||||
|
||||
/* Fill index. */
|
||||
vk = get_vk (K_dash, 0);
|
||||
for (i = 1; i < 64; i++)
|
||||
{
|
||||
int vk_next, is_zero;
|
||||
|
||||
vk_next = get_vk (K_dash, i);
|
||||
is_zero = (vk_next == 0);
|
||||
index[i-1] = (vk - 1) | (is_zero << 7);
|
||||
vk = (is_zero ? vk : vk_next);
|
||||
}
|
||||
index[63] = vk - 1;
|
||||
|
||||
memset (Q->z, 0, sizeof (bn256)); /* infinity */
|
||||
for (i = 31; i >= 0; i--)
|
||||
{
|
||||
FUNC(jpc_double) (Q, Q);
|
||||
FUNC(jpc_add_ac_signed) (Q, Q, &precomputed_2E_KG[index[i+32]&0x0f],
|
||||
index[i+32] >> 7);
|
||||
FUNC(jpc_add_ac_signed) (Q, Q, &precomputed_KG[index[i]&0x0f],
|
||||
index[i] >> 7);
|
||||
}
|
||||
|
||||
dst = k_is_even ? Q : tmp;
|
||||
FUNC(jpc_add_ac) (dst, Q, &precomputed_KG[0]);
|
||||
|
||||
return FUNC(jpc_to_ac) (X, Q);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* check if P is on the curve.
|
||||
*
|
||||
* Return -1 on error.
|
||||
* Return 0 on success.
|
||||
*/
|
||||
static int
|
||||
point_is_on_the_curve (const ac *P)
|
||||
{
|
||||
bn256 s[1], t[1];
|
||||
|
||||
/* Elliptic curve: y^2 = x^3 + a*x + b */
|
||||
MFNC(sqr) (s, P->x);
|
||||
MFNC(mul) (s, s, P->x);
|
||||
|
||||
#ifndef COEFFICIENT_A_IS_ZERO
|
||||
MFNC(mul) (t, coefficient_a, P->x);
|
||||
MFNC(add) (s, s, t);
|
||||
#endif
|
||||
MFNC(add) (s, s, coefficient_b);
|
||||
|
||||
MFNC(sqr) (t, P->y);
|
||||
if (bn256_cmp (s, t) == 0)
|
||||
return 0;
|
||||
else
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
static int
|
||||
get_vk_kP (const bn256 *K, int i)
|
||||
{
|
||||
uint32_t w;
|
||||
uint8_t blk = i/32;
|
||||
uint8_t pos = i%32;
|
||||
uint8_t col = 3*(pos % 11) + (pos >= 11) + (pos >= 22);
|
||||
uint8_t word_index = (blk * 3) + (pos / 11);
|
||||
|
||||
w = ((K->word[word_index] >> col) & 7);
|
||||
if (word_index < 7 && (pos == 10 || pos == 21))
|
||||
{
|
||||
uint8_t mask;
|
||||
uint8_t shift;
|
||||
|
||||
word_index++;
|
||||
if (pos == 10)
|
||||
{
|
||||
shift = 2;
|
||||
mask = 4;
|
||||
}
|
||||
else
|
||||
{
|
||||
shift = 1;
|
||||
mask = 6;
|
||||
}
|
||||
|
||||
w |= ((K->word[word_index] << shift) & mask);
|
||||
}
|
||||
|
||||
return w;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief X = k * P
|
||||
*
|
||||
* @param K scalar k
|
||||
* @param P P in affine coordiate
|
||||
*
|
||||
* Return -1 on error.
|
||||
* Return 0 on success.
|
||||
*
|
||||
* For the curve (cofactor is 1 and n is prime), possible error cases are:
|
||||
*
|
||||
* P is not on the curve.
|
||||
* P = G, k = n
|
||||
* Something wrong in the code.
|
||||
*
|
||||
* Mathmatically, k=1 and P=O is another possible case, but O cannot be
|
||||
* represented by affine coordinate.
|
||||
*/
|
||||
int
|
||||
FUNC(compute_kP) (ac *X, const bn256 *K, const ac *P)
|
||||
{
|
||||
uint8_t index[86]; /* Lower 2-bit for index absolute value, msb is
|
||||
for sign (encoded as: 0 means 1, 1 means -1). */
|
||||
bn256 K_dash[1];
|
||||
uint32_t k_is_even = bn256_is_even (K);
|
||||
jpc Q[1], tmp[1], *dst;
|
||||
int i;
|
||||
int vk;
|
||||
ac P3[1], P5[1], P7[1];
|
||||
const ac *p_Pi[4];
|
||||
|
||||
if (point_is_on_the_curve (P) < 0)
|
||||
return -1;
|
||||
|
||||
if (bn256_sub (K_dash, K, N) == 0) /* >= N, it's too big. */
|
||||
return -1;
|
||||
|
||||
bn256_sub_uint (K_dash, K, k_is_even);
|
||||
/* It keeps the condition: 1 <= K' <= N - 2, and K' is odd. */
|
||||
|
||||
p_Pi[0] = P;
|
||||
p_Pi[1] = P3;
|
||||
p_Pi[2] = P5;
|
||||
p_Pi[3] = P7;
|
||||
|
||||
{
|
||||
jpc Q1[1];
|
||||
|
||||
memcpy (Q->x, P->x, sizeof (bn256));
|
||||
memcpy (Q->y, P->y, sizeof (bn256));
|
||||
memset (Q->z, 0, sizeof (bn256));
|
||||
Q->z->word[0] = 1;
|
||||
|
||||
FUNC(jpc_double) (Q, Q);
|
||||
FUNC(jpc_add_ac) (Q1, Q, P);
|
||||
if (FUNC(jpc_to_ac) (P3, Q1) < 0) /* Never occurs, except coding errors. */
|
||||
return -1;
|
||||
FUNC(jpc_double) (Q, Q);
|
||||
FUNC(jpc_add_ac) (Q1, Q, P);
|
||||
if (FUNC(jpc_to_ac) (P5, Q1) < 0) /* Never occurs, except coding errors. */
|
||||
return -1;
|
||||
|
||||
memcpy (Q->x, P3->x, sizeof (bn256));
|
||||
memcpy (Q->y, P3->y, sizeof (bn256));
|
||||
memset (Q->z, 0, sizeof (bn256));
|
||||
Q->z->word[0] = 1;
|
||||
FUNC(jpc_double) (Q, Q);
|
||||
FUNC(jpc_add_ac) (Q1, Q, P);
|
||||
if (FUNC(jpc_to_ac) (P7, Q1) < 0) /* Never occurs, except coding errors. */
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Fill index. */
|
||||
vk = get_vk_kP (K_dash, 0);
|
||||
for (i = 1; i < 86; i++)
|
||||
{
|
||||
int vk_next, is_even;
|
||||
|
||||
vk_next = get_vk_kP (K_dash, i);
|
||||
is_even = ((vk_next & 1) == 0);
|
||||
index[i-1] = (is_even << 7) | ((is_even?7-vk:vk-1) >> 1);
|
||||
vk = vk_next + is_even;
|
||||
}
|
||||
index[85] = ((vk - 1) >> 1);
|
||||
|
||||
memset (Q->z, 0, sizeof (bn256)); /* infinity */
|
||||
for (i = 85; i >= 0; i--)
|
||||
{
|
||||
FUNC(jpc_double) (Q, Q);
|
||||
FUNC(jpc_double) (Q, Q);
|
||||
FUNC(jpc_double) (Q, Q);
|
||||
FUNC(jpc_add_ac_signed) (Q, Q, p_Pi[index[i]&0x03], index[i] >> 7);
|
||||
}
|
||||
|
||||
dst = k_is_even ? Q : tmp;
|
||||
FUNC(jpc_add_ac) (dst, Q, P);
|
||||
|
||||
return FUNC(jpc_to_ac) (X, Q);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Compute signature (r,s) of hash string z with secret key d
|
||||
*/
|
||||
void
|
||||
FUNC(ecdsa) (bn256 *r, bn256 *s, const bn256 *z, const bn256 *d)
|
||||
{
|
||||
bn256 k[1];
|
||||
ac KG[1];
|
||||
bn512 tmp[1];
|
||||
bn256 k_inv[1];
|
||||
uint32_t carry;
|
||||
#define borrow carry
|
||||
#define tmp_k k_inv
|
||||
|
||||
do
|
||||
{
|
||||
do
|
||||
{
|
||||
bn256_random (k);
|
||||
if (bn256_add_uint (k, k, 1))
|
||||
continue;
|
||||
if (bn256_sub (tmp_k, k, N) == 0) /* >= N, it's too big. */
|
||||
continue;
|
||||
/* 1 <= k <= N - 1 */
|
||||
FUNC(compute_kG) (KG, k);
|
||||
borrow = bn256_sub (r, KG->x, N);
|
||||
if (borrow)
|
||||
memcpy (r, KG->x, sizeof (bn256));
|
||||
else
|
||||
memcpy (KG->x, r, sizeof (bn256));
|
||||
}
|
||||
while (bn256_is_zero (r));
|
||||
|
||||
mod_inv (k_inv, k, N);
|
||||
bn256_mul (tmp, r, d);
|
||||
mod_reduce (s, tmp, N, MU_lower);
|
||||
carry = bn256_add (s, s, z);
|
||||
if (carry)
|
||||
bn256_sub (s, s, N);
|
||||
else
|
||||
bn256_sub ((bn256 *)tmp, s, N);
|
||||
bn256_mul (tmp, s, k_inv);
|
||||
mod_reduce (s, tmp, N, MU_lower);
|
||||
}
|
||||
while (bn256_is_zero (s));
|
||||
|
||||
#undef tmp_k
|
||||
#undef borrow
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Check if a secret d0 is valid or not
|
||||
*
|
||||
* @param D0 scalar D0: secret
|
||||
* @param D1 scalar D1: secret candidate N-D0
|
||||
*
|
||||
* Return 0 on error.
|
||||
* Return -1 when D1 should be used as the secret
|
||||
* Return 1 when D0 should be used as the secret
|
||||
*/
|
||||
int
|
||||
FUNC(check_secret) (const bn256 *d0, bn256 *d1)
|
||||
{
|
||||
ac Q0[1], Q1[1];
|
||||
|
||||
if (bn256_is_zero (d0) || bn256_sub (d1, N, d0) != 0)
|
||||
/* == 0 or >= N, it's not valid. */
|
||||
return 0;
|
||||
|
||||
FUNC(compute_kG) (Q0, d0);
|
||||
FUNC(compute_kG) (Q1, d1);
|
||||
|
||||
/*
|
||||
* Jivsov compliant key check
|
||||
*/
|
||||
return bn256_cmp (Q1[0].y, Q0[0].y);
|
||||
}
|
||||
|
|
@ -1,7 +0,0 @@
|
|||
#define CONCAT0(a,b) a##b
|
||||
#define CONCAT1(a,b) CONCAT0(a,b)
|
||||
#define CONCAT2(a,b,c) CONCAT1(a,b##c)
|
||||
#define CONCAT3(a,b,c) CONCAT2(a,b,c)
|
||||
|
||||
#define FUNC(func) CONCAT1(func##_,FIELD)
|
||||
#define MFNC(func) CONCAT3(mod,FIELD,_##func)
|
||||
|
|
@ -1,763 +0,0 @@
|
|||
/*
|
||||
* flash.c -- Data Objects (DO) and GPG Key handling on Flash ROM
|
||||
*
|
||||
* Copyright (C) 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018
|
||||
* Free Software Initiative of Japan
|
||||
* Author: NIIBE Yutaka <gniibe@fsij.org>
|
||||
*
|
||||
* This file is a part of Gnuk, a GnuPG USB Token implementation.
|
||||
*
|
||||
* Gnuk is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Gnuk is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
|
||||
* License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* We assume single DO size is less than 256.
|
||||
*
|
||||
* NOTE: "Card holder certificate" (which size is larger than 256) is
|
||||
* not put into data pool, but is implemented by its own flash
|
||||
* page(s).
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include "sys.h"
|
||||
#include "gnuk.h"
|
||||
|
||||
#include "pico/stdlib.h"
|
||||
#include "hardware/flash.h"
|
||||
#include "hsm2040.h"
|
||||
#include "tusb.h"
|
||||
|
||||
extern void low_flash_available();
|
||||
|
||||
/*
|
||||
* Flash memory map
|
||||
*
|
||||
* _text
|
||||
* .text
|
||||
* .ctors
|
||||
* .dtors
|
||||
* _etext
|
||||
* .data
|
||||
* _bss_start
|
||||
* .bss
|
||||
* _end
|
||||
* <alignment to page>
|
||||
* ch_certificate_startp
|
||||
* <2048 bytes>
|
||||
* _keystore_pool
|
||||
* Three flash pages for keystore
|
||||
* a page contains a key data of:
|
||||
* For RSA-2048: 512-byte (p, q and N)
|
||||
* For RSA-4096: 1024-byte (p, q and N)
|
||||
* For ECDSA/ECDH and EdDSA, there are padding after public key
|
||||
* _data_pool
|
||||
* <two pages>
|
||||
*/
|
||||
|
||||
#define FLASH_DATA_POOL_HEADER_SIZE 2
|
||||
#define FLASH_DATA_POOL_SIZE (2048*1024)
|
||||
|
||||
static uint16_t flash_page_size;
|
||||
static const uint8_t *data_pool;
|
||||
static uint8_t *last_p;
|
||||
|
||||
/* The first halfword is generation for the data page (little endian) */
|
||||
const uint8_t flash_data[4] __attribute__ ((section (".gnuk_data"))) = {
|
||||
0x00, 0x00, 0xff, 0xff
|
||||
};
|
||||
|
||||
#define FLASH_TARGET_OFFSET (PICO_FLASH_SIZE_BYTES >> 1) // DATA starts at the mid of flash
|
||||
|
||||
|
||||
const uint8_t *flash_addr_key_storage_start = (const uint8_t *) (XIP_BASE + FLASH_TARGET_OFFSET);
|
||||
const uint8_t *flash_addr_data_storage_start = (const uint8_t *) (XIP_BASE + FLASH_TARGET_OFFSET + 2048 * 1024); // 2 MB
|
||||
const uint8_t *ch_certificate_start = (const uint8_t *) (XIP_BASE + FLASH_TARGET_OFFSET - FLASH_SECTOR_SIZE);
|
||||
#define FLASH_ADDR_KEY_STORAGE_START flash_addr_key_storage_start
|
||||
#define FLASH_ADDR_DATA_STORAGE_START flash_addr_data_storage_start
|
||||
|
||||
extern int flash_erase_page (uintptr_t addr);
|
||||
extern int flash_program_halfword (uintptr_t addr, uint16_t data);
|
||||
extern int flash_check_blank (const uint8_t *p_start, size_t size);
|
||||
extern int flash_write (uintptr_t dst_addr, const uint8_t *src, size_t len);
|
||||
|
||||
static int key_available_at (const uint8_t *k, int key_size)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < key_size; i++)
|
||||
if (k[i])
|
||||
break;
|
||||
if (i == key_size) /* It's ZERO. Released key. */
|
||||
return 0;
|
||||
|
||||
for (i = 0; i < key_size; i++)
|
||||
if (k[i] != 0xff)
|
||||
break;
|
||||
if (i == key_size) /* It's FULL. Unused key. */
|
||||
return 0;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void
|
||||
flash_do_storage_init (const uint8_t **p_do_start, const uint8_t **p_do_end)
|
||||
{
|
||||
uint16_t gen0, gen1;
|
||||
uint16_t *gen0_p = (uint16_t *)FLASH_ADDR_DATA_STORAGE_START;
|
||||
uint16_t *gen1_p;
|
||||
|
||||
flash_page_size = FLASH_SECTOR_SIZE * 8; // 32 KB
|
||||
|
||||
gen1_p = (uint16_t *)(FLASH_ADDR_DATA_STORAGE_START + flash_page_size);
|
||||
data_pool = FLASH_ADDR_DATA_STORAGE_START;
|
||||
|
||||
/* Check data pool generation and choose the page */
|
||||
gen0 = *gen0_p;
|
||||
gen1 = *gen1_p;
|
||||
|
||||
if (gen0 == 0xffff && gen1 == 0xffff)
|
||||
{
|
||||
gen0 = 0x0000;
|
||||
*gen0_p = gen0;
|
||||
}
|
||||
|
||||
if (gen0 == 0xffff)
|
||||
/* Use another page if a page is erased. */
|
||||
data_pool = FLASH_ADDR_DATA_STORAGE_START + flash_page_size;
|
||||
else if (gen1 == 0xffff)
|
||||
/* Or use different page if another page is erased. */
|
||||
data_pool = FLASH_ADDR_DATA_STORAGE_START;
|
||||
else if ((gen0 == 0xfffe && gen1 == 0) || gen1 > gen0)
|
||||
/* When both pages have valid header, use newer page. */
|
||||
data_pool = FLASH_ADDR_DATA_STORAGE_START + flash_page_size;
|
||||
|
||||
*p_do_start = data_pool + FLASH_DATA_POOL_HEADER_SIZE;
|
||||
*p_do_end = data_pool + flash_page_size;
|
||||
}
|
||||
|
||||
static uint8_t *flash_key_getpage (enum kind_of_key kk);
|
||||
|
||||
void
|
||||
flash_terminate (void)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < 3; i++)
|
||||
flash_erase_page ((uintptr_t)flash_key_getpage (i));
|
||||
flash_erase_page ((uintptr_t)FLASH_ADDR_DATA_STORAGE_START);
|
||||
flash_erase_page ((uintptr_t)(FLASH_ADDR_DATA_STORAGE_START + flash_page_size));
|
||||
data_pool = FLASH_ADDR_DATA_STORAGE_START;
|
||||
last_p = (uint8_t *)FLASH_ADDR_DATA_STORAGE_START + FLASH_DATA_POOL_HEADER_SIZE;
|
||||
#if defined(CERTDO_SUPPORT)
|
||||
flash_erase_page ((uintptr_t)ch_certificate_start);
|
||||
if (FLASH_CH_CERTIFICATE_SIZE > flash_page_size)
|
||||
flash_erase_page ((uintptr_t)(ch_certificate_start + flash_page_size));
|
||||
#endif
|
||||
}
|
||||
|
||||
void
|
||||
flash_activate (void)
|
||||
{
|
||||
flash_program_halfword ((uintptr_t)FLASH_ADDR_DATA_STORAGE_START, 0);
|
||||
low_flash_available();
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
flash_key_storage_init (void)
|
||||
{
|
||||
const uint8_t *p;
|
||||
int i;
|
||||
|
||||
/* For each key, find its address. */
|
||||
p = FLASH_ADDR_KEY_STORAGE_START;
|
||||
for (i = 0; i < 3; i++)
|
||||
{
|
||||
const uint8_t *k;
|
||||
int key_size = gpg_get_algo_attr_key_size (i, GPG_KEY_STORAGE);
|
||||
|
||||
kd[i].pubkey = NULL;
|
||||
for (k = p; k < p + flash_page_size; k += key_size)
|
||||
if (key_available_at (k, key_size))
|
||||
{
|
||||
int prv_len = gpg_get_algo_attr_key_size (i, GPG_KEY_PRIVATE);
|
||||
|
||||
kd[i].pubkey = k + prv_len;
|
||||
break;
|
||||
}
|
||||
|
||||
p += flash_page_size;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Flash data pool managenent
|
||||
*
|
||||
* Flash data pool consists of two parts:
|
||||
* 2-byte header
|
||||
* contents
|
||||
*
|
||||
* Flash data pool objects:
|
||||
* Data Object (DO) (of smart card)
|
||||
* Internal objects:
|
||||
* NONE (0x0000)
|
||||
* 123-counter
|
||||
* 14-bit counter
|
||||
* bool object
|
||||
* small enum
|
||||
*
|
||||
* Format of a Data Object:
|
||||
* NR: 8-bit tag_number
|
||||
* LEN: 8-bit length
|
||||
* DATA: data * LEN
|
||||
* PAD: optional byte for 16-bit alignment
|
||||
*/
|
||||
|
||||
void
|
||||
flash_set_data_pool_last (const uint8_t *p)
|
||||
{
|
||||
last_p = (uint8_t *)p;
|
||||
}
|
||||
|
||||
/*
|
||||
* We use two pages
|
||||
*/
|
||||
static int
|
||||
flash_copying_gc (void)
|
||||
{
|
||||
uint8_t *src, *dst;
|
||||
uint16_t generation;
|
||||
|
||||
if (data_pool == FLASH_ADDR_DATA_STORAGE_START)
|
||||
{
|
||||
src = (uint8_t *)FLASH_ADDR_DATA_STORAGE_START;
|
||||
dst = (uint8_t *)FLASH_ADDR_DATA_STORAGE_START + flash_page_size;
|
||||
}
|
||||
else
|
||||
{
|
||||
src = (uint8_t *)FLASH_ADDR_DATA_STORAGE_START + flash_page_size;
|
||||
dst = (uint8_t *)FLASH_ADDR_DATA_STORAGE_START;
|
||||
}
|
||||
|
||||
generation = *(uint16_t *)src;
|
||||
data_pool = dst;
|
||||
gpg_data_copy (data_pool + FLASH_DATA_POOL_HEADER_SIZE);
|
||||
if (generation == 0xfffe)
|
||||
generation = 0;
|
||||
else
|
||||
generation++;
|
||||
flash_program_halfword ((uintptr_t)dst, generation);
|
||||
flash_erase_page ((uintptr_t)src);
|
||||
low_flash_available();
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
is_data_pool_full (size_t size)
|
||||
{
|
||||
return last_p + size > data_pool + flash_page_size;
|
||||
}
|
||||
|
||||
static uint8_t *
|
||||
flash_data_pool_allocate (size_t size)
|
||||
{
|
||||
uint8_t *p;
|
||||
|
||||
size = (size + 1) & ~1; /* allocation unit is 1-halfword (2-byte) */
|
||||
|
||||
if (is_data_pool_full (size))
|
||||
if (flash_copying_gc () < 0 || /*still*/ is_data_pool_full (size))
|
||||
TU_LOG1 ("!!!! FATAL: %d\r\n",FATAL_FLASH);
|
||||
|
||||
p = last_p;
|
||||
last_p += size;
|
||||
return p;
|
||||
}
|
||||
|
||||
void
|
||||
flash_do_write_internal (const uint8_t *p, int nr, const uint8_t *data, int len)
|
||||
{
|
||||
uint16_t hw;
|
||||
uintptr_t addr;
|
||||
int i;
|
||||
|
||||
addr = (uintptr_t)p;
|
||||
hw = nr | (len << 8);
|
||||
if (flash_program_halfword (addr, hw) != 0)
|
||||
flash_warning ("DO WRITE ERROR");
|
||||
addr += 2;
|
||||
|
||||
for (i = 0; i < len/2; i++)
|
||||
{
|
||||
hw = data[i*2] | (data[i*2+1]<<8);
|
||||
if (flash_program_halfword (addr, hw) != 0)
|
||||
flash_warning ("DO WRITE ERROR");
|
||||
addr += 2;
|
||||
}
|
||||
|
||||
if ((len & 1))
|
||||
{
|
||||
hw = data[i*2] | 0xff00;
|
||||
if (flash_program_halfword (addr, hw) != 0)
|
||||
flash_warning ("DO WRITE ERROR");
|
||||
}
|
||||
|
||||
low_flash_available();
|
||||
}
|
||||
|
||||
const uint8_t *
|
||||
flash_do_write (uint8_t nr, const uint8_t *data, int len)
|
||||
{
|
||||
const uint8_t *p;
|
||||
|
||||
DEBUG_INFO ("flash DO\r\n");
|
||||
|
||||
p = flash_data_pool_allocate (2 + len);
|
||||
if (p == NULL)
|
||||
{
|
||||
DEBUG_INFO ("flash data pool allocation failure.\r\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
flash_do_write_internal (p, nr, data, len);
|
||||
DEBUG_INFO ("flash DO...done\r\n");
|
||||
return p + 1;
|
||||
}
|
||||
|
||||
void
|
||||
flash_warning (const char *msg)
|
||||
{
|
||||
(void)msg;
|
||||
DEBUG_INFO ("FLASH: ");
|
||||
DEBUG_INFO (msg);
|
||||
DEBUG_INFO ("\r\n");
|
||||
}
|
||||
|
||||
void
|
||||
flash_do_release (const uint8_t *do_data)
|
||||
{
|
||||
uintptr_t addr = (uintptr_t)do_data - 1;
|
||||
uintptr_t addr_tag = addr;
|
||||
int i;
|
||||
int len = do_data[0];
|
||||
|
||||
/* Don't filling zero for data in code (such as ds_count_initial_value) */
|
||||
if (do_data < FLASH_ADDR_DATA_STORAGE_START
|
||||
|| do_data > FLASH_ADDR_DATA_STORAGE_START + FLASH_DATA_POOL_SIZE)
|
||||
return;
|
||||
|
||||
addr += 2;
|
||||
|
||||
/* Fill zero for content and pad */
|
||||
for (i = 0; i < len/2; i ++)
|
||||
{
|
||||
if (flash_program_halfword (addr, 0) != 0)
|
||||
flash_warning ("fill-zero failure");
|
||||
addr += 2;
|
||||
}
|
||||
|
||||
if ((len & 1))
|
||||
{
|
||||
if (flash_program_halfword (addr, 0) != 0)
|
||||
flash_warning ("fill-zero pad failure");
|
||||
}
|
||||
|
||||
/* Fill 0x0000 for "tag_number and length" word */
|
||||
if (flash_program_halfword (addr_tag, 0) != 0)
|
||||
flash_warning ("fill-zero tag_nr failure");
|
||||
|
||||
//CAUTION: flash_do_release is followed by a flash_write. Thus, we can avoid a single write
|
||||
//low_flash_available();
|
||||
}
|
||||
|
||||
|
||||
static uint8_t *
|
||||
flash_key_getpage (enum kind_of_key kk)
|
||||
{
|
||||
/* There is a page for each KK. */
|
||||
return (uint8_t *)FLASH_ADDR_KEY_STORAGE_START + (flash_page_size * kk);
|
||||
}
|
||||
|
||||
uint8_t *
|
||||
flash_key_alloc (enum kind_of_key kk)
|
||||
{
|
||||
uint8_t *k, *k0 = flash_key_getpage (kk);
|
||||
int i;
|
||||
int key_size = gpg_get_algo_attr_key_size (kk, GPG_KEY_STORAGE);
|
||||
|
||||
/* Seek free space in the page. */
|
||||
for (k = k0; k < k0 + flash_page_size; k += key_size)
|
||||
{
|
||||
const uint32_t *p = (const uint32_t *)k;
|
||||
|
||||
for (i = 0; i < key_size/4; i++)
|
||||
if (p[i] != 0xffffffff)
|
||||
break;
|
||||
|
||||
if (i == key_size/4) /* Yes, it's empty. */
|
||||
return k;
|
||||
}
|
||||
|
||||
/* Should not happen as we have enough free space all time, but just
|
||||
in case. */
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int
|
||||
flash_key_write (uint8_t *key_addr,
|
||||
const uint8_t *key_data, int key_data_len,
|
||||
const uint8_t *pubkey, int pubkey_len)
|
||||
{
|
||||
uint16_t hw;
|
||||
uintptr_t addr;
|
||||
int i;
|
||||
|
||||
addr = (uintptr_t)key_addr;
|
||||
for (i = 0; i < key_data_len/2; i ++)
|
||||
{
|
||||
hw = key_data[i*2] | (key_data[i*2+1]<<8);
|
||||
if (flash_program_halfword (addr, hw) != 0)
|
||||
return -1;
|
||||
addr += 2;
|
||||
}
|
||||
|
||||
for (i = 0; i < pubkey_len/2; i ++)
|
||||
{
|
||||
hw = pubkey[i*2] | (pubkey[i*2+1]<<8);
|
||||
if (flash_program_halfword (addr, hw) != 0)
|
||||
return -1;
|
||||
addr += 2;
|
||||
}
|
||||
|
||||
low_flash_available();
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
flash_check_all_other_keys_released (const uint8_t *key_addr, int key_size)
|
||||
{
|
||||
uintptr_t start = (uintptr_t)key_addr & ~(flash_page_size - 1);
|
||||
const uint32_t *p = (const uint32_t *)start;
|
||||
|
||||
while (p < (const uint32_t *)(start + flash_page_size))
|
||||
if (p == (const uint32_t *)key_addr)
|
||||
p += key_size/4;
|
||||
else
|
||||
if (*p)
|
||||
return 0;
|
||||
else
|
||||
p++;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void
|
||||
flash_key_fill_zero_as_released (uint8_t *key_addr, int key_size)
|
||||
{
|
||||
int i;
|
||||
uintptr_t addr = (uintptr_t)key_addr;
|
||||
|
||||
for (i = 0; i < key_size/2; i++)
|
||||
flash_program_halfword (addr + i*2, 0);
|
||||
|
||||
low_flash_available();
|
||||
}
|
||||
|
||||
void
|
||||
flash_key_release (uint8_t *key_addr, int key_size)
|
||||
{
|
||||
if (flash_check_all_other_keys_released (key_addr, key_size))
|
||||
flash_erase_page (((uintptr_t)key_addr & ~(flash_page_size - 1)));
|
||||
else
|
||||
flash_key_fill_zero_as_released (key_addr, key_size);
|
||||
}
|
||||
|
||||
void
|
||||
flash_key_release_page (enum kind_of_key kk)
|
||||
{
|
||||
flash_erase_page ((uintptr_t)flash_key_getpage (kk));
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
flash_clear_halfword (uintptr_t addr)
|
||||
{
|
||||
flash_program_halfword (addr, 0);
|
||||
low_flash_available();
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
flash_put_data_internal (const uint8_t *p, uint16_t hw)
|
||||
{
|
||||
flash_program_halfword ((uintptr_t)p, hw);
|
||||
low_flash_available();
|
||||
}
|
||||
|
||||
void
|
||||
flash_put_data (uint16_t hw)
|
||||
{
|
||||
uint8_t *p;
|
||||
|
||||
p = flash_data_pool_allocate (2);
|
||||
if (p == NULL)
|
||||
{
|
||||
DEBUG_INFO ("data allocation failure.\r\n");
|
||||
}
|
||||
|
||||
flash_program_halfword ((uintptr_t)p, hw);
|
||||
low_flash_available();
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
flash_bool_clear (const uint8_t **addr_p)
|
||||
{
|
||||
const uint8_t *p;
|
||||
|
||||
if ((p = *addr_p) == NULL)
|
||||
return;
|
||||
|
||||
flash_program_halfword ((uintptr_t)p, 0);
|
||||
*addr_p = NULL;
|
||||
low_flash_available();
|
||||
}
|
||||
|
||||
void
|
||||
flash_bool_write_internal (const uint8_t *p, int nr)
|
||||
{
|
||||
flash_program_halfword ((uintptr_t)p, nr);
|
||||
low_flash_available();
|
||||
}
|
||||
|
||||
const uint8_t *
|
||||
flash_bool_write (uint8_t nr)
|
||||
{
|
||||
uint8_t *p;
|
||||
uint16_t hw = nr;
|
||||
|
||||
p = flash_data_pool_allocate (2);
|
||||
if (p == NULL)
|
||||
{
|
||||
DEBUG_INFO ("bool allocation failure.\r\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
flash_program_halfword ((uintptr_t)p, hw);
|
||||
low_flash_available();
|
||||
return p;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
flash_enum_clear (const uint8_t **addr_p)
|
||||
{
|
||||
flash_bool_clear (addr_p);
|
||||
}
|
||||
|
||||
void
|
||||
flash_enum_write_internal (const uint8_t *p, int nr, uint8_t v)
|
||||
{
|
||||
uint16_t hw = nr | (v << 8);
|
||||
|
||||
flash_program_halfword ((uintptr_t)p, hw);
|
||||
low_flash_available();
|
||||
}
|
||||
|
||||
const uint8_t *
|
||||
flash_enum_write (uint8_t nr, uint8_t v)
|
||||
{
|
||||
uint8_t *p;
|
||||
uint16_t hw = nr | (v << 8);
|
||||
|
||||
p = flash_data_pool_allocate (2);
|
||||
if (p == NULL)
|
||||
{
|
||||
DEBUG_INFO ("enum allocation failure.\r\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
flash_program_halfword ((uintptr_t)p, hw);
|
||||
low_flash_available();
|
||||
return p;
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
flash_cnt123_get_value (const uint8_t *p)
|
||||
{
|
||||
if (p == NULL)
|
||||
return 0;
|
||||
else
|
||||
{
|
||||
uint8_t v = *p;
|
||||
|
||||
/*
|
||||
* After erase, a halfword in flash memory becomes 0xffff.
|
||||
* The halfword can be programmed to any value.
|
||||
* Then, the halfword can be programmed to zero.
|
||||
*
|
||||
* Thus, we can represent value 1, 2, and 3.
|
||||
*/
|
||||
if (v == 0xff)
|
||||
return 1;
|
||||
else if (v == 0x00)
|
||||
return 3;
|
||||
else
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
flash_cnt123_write_internal (const uint8_t *p, int which, int v)
|
||||
{
|
||||
uint16_t hw;
|
||||
|
||||
hw = NR_COUNTER_123 | (which << 8);
|
||||
flash_program_halfword ((uintptr_t)p, hw);
|
||||
|
||||
if (v == 1)
|
||||
return;
|
||||
else if (v == 2)
|
||||
flash_program_halfword ((uintptr_t)p+2, 0xc3c3);
|
||||
else /* v == 3 */
|
||||
flash_program_halfword ((uintptr_t)p+2, 0);
|
||||
low_flash_available();
|
||||
}
|
||||
|
||||
void
|
||||
flash_cnt123_increment (uint8_t which, const uint8_t **addr_p)
|
||||
{
|
||||
const uint8_t *p;
|
||||
uint16_t hw;
|
||||
|
||||
if ((p = *addr_p) == NULL)
|
||||
{
|
||||
p = flash_data_pool_allocate (4);
|
||||
if (p == NULL)
|
||||
{
|
||||
DEBUG_INFO ("cnt123 allocation failure.\r\n");
|
||||
return;
|
||||
}
|
||||
hw = NR_COUNTER_123 | (which << 8);
|
||||
flash_program_halfword ((uintptr_t)p, hw);
|
||||
*addr_p = p + 2;
|
||||
}
|
||||
else
|
||||
{
|
||||
uint8_t v = *p;
|
||||
|
||||
if (v == 0)
|
||||
return;
|
||||
|
||||
if (v == 0xff)
|
||||
hw = 0xc3c3;
|
||||
else
|
||||
hw = 0;
|
||||
|
||||
flash_program_halfword ((uintptr_t)p, hw);
|
||||
}
|
||||
low_flash_available();
|
||||
}
|
||||
|
||||
void
|
||||
flash_cnt123_clear (const uint8_t **addr_p)
|
||||
{
|
||||
const uint8_t *p;
|
||||
|
||||
if ((p = *addr_p) == NULL)
|
||||
return;
|
||||
|
||||
flash_program_halfword ((uintptr_t)p, 0);
|
||||
p -= 2;
|
||||
flash_program_halfword ((uintptr_t)p, 0);
|
||||
*addr_p = NULL;
|
||||
low_flash_available();
|
||||
}
|
||||
|
||||
|
||||
#if defined(CERTDO_SUPPORT)
|
||||
int
|
||||
flash_erase_binary (uint8_t file_id)
|
||||
{
|
||||
if (file_id == FILEID_CH_CERTIFICATE)
|
||||
{
|
||||
const uint8_t *p = ch_certificate_start;
|
||||
if (flash_check_blank (p, FLASH_CH_CERTIFICATE_SIZE) == 0)
|
||||
{
|
||||
flash_erase_page ((uintptr_t)p);
|
||||
if (FLASH_CH_CERTIFICATE_SIZE > flash_page_size)
|
||||
flash_erase_page ((uintptr_t)p + flash_page_size);
|
||||
}
|
||||
low_flash_available();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
int
|
||||
flash_write_binary (uint8_t file_id, const uint8_t *data,
|
||||
uint16_t len, uint16_t offset)
|
||||
{
|
||||
uint16_t maxsize;
|
||||
const uint8_t *p;
|
||||
|
||||
if (file_id == FILEID_SERIAL_NO)
|
||||
{
|
||||
maxsize = 6;
|
||||
p = &openpgpcard_aid[8];
|
||||
}
|
||||
#if defined(CERTDO_SUPPORT)
|
||||
else if (file_id == FILEID_CH_CERTIFICATE)
|
||||
{
|
||||
maxsize = FLASH_CH_CERTIFICATE_SIZE;
|
||||
p = ch_certificate_start;
|
||||
}
|
||||
#endif
|
||||
else
|
||||
return -1;
|
||||
|
||||
if (offset + len > maxsize || (offset&1) || (len&1))
|
||||
return -1;
|
||||
else
|
||||
{
|
||||
uint16_t hw;
|
||||
uintptr_t addr;
|
||||
int i;
|
||||
|
||||
if (flash_check_blank (p + offset, len) == 0)
|
||||
return -1;
|
||||
|
||||
addr = (uintptr_t)p + offset;
|
||||
for (i = 0; i < len/2; i++)
|
||||
{
|
||||
hw = data[i*2] | (data[i*2+1]<<8);
|
||||
if (flash_program_halfword (addr, hw) != 0)
|
||||
flash_warning ("DO WRITE ERROR");
|
||||
addr += 2;
|
||||
}
|
||||
|
||||
low_flash_available();
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,465 +0,0 @@
|
|||
#ifndef _GNUK_H_
|
||||
#define _GNUK_H_
|
||||
|
||||
#include "config.h"
|
||||
/*
|
||||
* Application layer <-> CCID layer data structure
|
||||
*/
|
||||
|
||||
|
||||
#define CARD_CHANGE_INSERT 0
|
||||
#define CARD_CHANGE_REMOVE 1
|
||||
#define CARD_CHANGE_TOGGLE 2
|
||||
void ccid_card_change_signal (int how);
|
||||
|
||||
/* CCID thread */
|
||||
#define EV_CARD_CHANGE 1
|
||||
#define EV_TX_FINISHED 2 /* CCID Tx finished */
|
||||
#define EV_EXEC_ACK_REQUIRED 4 /* OpenPGPcard Execution ACK required */
|
||||
#define EV_EXEC_FINISHED 8 /* OpenPGPcard Execution finished */
|
||||
#define EV_RX_DATA_READY 16 /* USB Rx data available */
|
||||
|
||||
/* OpenPGPcard thread */
|
||||
#define EV_MODIFY_CMD_AVAILABLE 1
|
||||
#define EV_VERIFY_CMD_AVAILABLE 2
|
||||
#define EV_CMD_AVAILABLE 4
|
||||
#define EV_EXIT 8
|
||||
#define EV_PINPAD_INPUT_DONE 16
|
||||
|
||||
/* Maximum cmd apdu data is key import 24+4+256+256 (proc_key_import) */
|
||||
#define MAX_CMD_APDU_DATA_SIZE (24+4+256+256) /* without header */
|
||||
/* Maximum res apdu data is public key 5+9+512 (gpg_do_public_key) */
|
||||
#define MAX_RES_APDU_DATA_SIZE (5+9+512) /* without trailer */
|
||||
|
||||
#define CCID_MSG_HEADER_SIZE 10
|
||||
|
||||
|
||||
|
||||
/* USB buffer size of LL (Low-level): size of single Bulk transaction */
|
||||
#define USB_LL_BUF_SIZE 64
|
||||
|
||||
enum ccid_state {
|
||||
CCID_STATE_NOCARD, /* No card available */
|
||||
CCID_STATE_START, /* Initial */
|
||||
CCID_STATE_WAIT, /* Waiting APDU */
|
||||
|
||||
CCID_STATE_EXECUTE, /* Executing command */
|
||||
CCID_STATE_ACK_REQUIRED_0, /* Ack required (executing)*/
|
||||
CCID_STATE_ACK_REQUIRED_1, /* Waiting user's ACK (execution finished) */
|
||||
|
||||
CCID_STATE_EXITED, /* CCID Thread Terminated */
|
||||
CCID_STATE_EXEC_REQUESTED, /* Exec requested */
|
||||
};
|
||||
|
||||
|
||||
enum ccid_state ccid_get_ccid_state (void);
|
||||
|
||||
extern volatile uint8_t auth_status;
|
||||
#define AC_NONE_AUTHORIZED 0x00
|
||||
#define AC_PSO_CDS_AUTHORIZED 0x01 /* PW1 with 0x81 verified */
|
||||
#define AC_OTHER_AUTHORIZED 0x02 /* PW1 with 0x82 verified */
|
||||
#define AC_ADMIN_AUTHORIZED 0x04 /* PW3 verified */
|
||||
#define AC_NEVER 0x80
|
||||
#define AC_ALWAYS 0xFF
|
||||
|
||||
#define PW_ERR_PW1 0
|
||||
#define PW_ERR_RC 1
|
||||
#define PW_ERR_PW3 2
|
||||
int gpg_pw_get_retry_counter (int who);
|
||||
int gpg_pw_locked (uint8_t which);
|
||||
void gpg_pw_reset_err_counter (uint8_t which);
|
||||
void gpg_pw_increment_err_counter (uint8_t which);
|
||||
|
||||
int ac_check_status (uint8_t ac_flag);
|
||||
int verify_pso_cds (const uint8_t *pw, int pw_len);
|
||||
int verify_other (const uint8_t *pw, int pw_len);
|
||||
int verify_user_0 (uint8_t access, const uint8_t *pw, int buf_len,
|
||||
int pw_len_known, const uint8_t *ks_pw1, int saveks);
|
||||
int verify_admin (const uint8_t *pw, int pw_len);
|
||||
int verify_admin_0 (const uint8_t *pw, int buf_len, int pw_len_known,
|
||||
const uint8_t *ks_pw3, int saveks);
|
||||
|
||||
void ac_reset_pso_cds (void);
|
||||
void ac_reset_other (void);
|
||||
void ac_reset_admin (void);
|
||||
void ac_fini (void);
|
||||
|
||||
|
||||
extern uint8_t file_selection;
|
||||
extern const uint8_t historical_bytes[];
|
||||
extern uint16_t data_objects_number_of_bytes;
|
||||
|
||||
#define CHALLENGE_LEN 32
|
||||
|
||||
void gpg_data_scan (const uint8_t *start, const uint8_t *end);
|
||||
void gpg_data_copy (const uint8_t *p);
|
||||
void gpg_do_terminate (void);
|
||||
void gpg_do_get_data (uint16_t tag, int with_tag);
|
||||
void gpg_do_put_data (uint16_t tag, const uint8_t *data, int len);
|
||||
void gpg_do_public_key (uint8_t kk_byte);
|
||||
void gpg_do_keygen (uint8_t *buf);
|
||||
|
||||
const uint8_t *gpg_get_firmware_update_key (uint8_t keyno);
|
||||
|
||||
/* Constants: algo+size */
|
||||
#define ALGO_RSA4K 0
|
||||
/* #define ALGO_NISTP256R1 1 */
|
||||
#define ALGO_SECP256K1 2
|
||||
#define ALGO_ED25519 3
|
||||
#define ALGO_CURVE25519 4
|
||||
#define ALGO_X448 5
|
||||
#define ALGO_ED448 6
|
||||
#define ALGO_RSA2K 255
|
||||
|
||||
enum kind_of_key {
|
||||
GPG_KEY_FOR_SIGNING = 0,
|
||||
GPG_KEY_FOR_DECRYPTION = 1,
|
||||
GPG_KEY_FOR_AUTHENTICATION = 2,
|
||||
};
|
||||
|
||||
enum size_of_key {
|
||||
GPG_KEY_STORAGE = 0, /* PUBKEY + PRVKEY rounded to 2^N */
|
||||
GPG_KEY_PUBLIC,
|
||||
GPG_KEY_PRIVATE,
|
||||
};
|
||||
|
||||
int gpg_get_algo_attr (enum kind_of_key kk);
|
||||
int gpg_get_algo_attr_key_size (enum kind_of_key kk, enum size_of_key s);
|
||||
|
||||
void flash_do_storage_init (const uint8_t **, const uint8_t **);
|
||||
void flash_terminate (void);
|
||||
void flash_activate (void);
|
||||
void flash_key_storage_init (void);
|
||||
void flash_do_release (const uint8_t *);
|
||||
const uint8_t *flash_do_write (uint8_t nr, const uint8_t *data, int len);
|
||||
uint8_t *flash_key_alloc (enum kind_of_key);
|
||||
void flash_key_release (uint8_t *, int);
|
||||
void flash_key_release_page (enum kind_of_key);
|
||||
int flash_key_write (uint8_t *key_addr,
|
||||
const uint8_t *key_data, int key_data_len,
|
||||
const uint8_t *pubkey, int pubkey_len);
|
||||
void flash_set_data_pool_last (const uint8_t *p);
|
||||
void flash_clear_halfword (uintptr_t addr);
|
||||
void flash_increment_counter (uint8_t counter_tag_nr);
|
||||
void flash_reset_counter (uint8_t counter_tag_nr);
|
||||
|
||||
#define FILEID_SERIAL_NO 0
|
||||
#define FILEID_UPDATE_KEY_0 1
|
||||
#define FILEID_UPDATE_KEY_1 2
|
||||
#define FILEID_UPDATE_KEY_2 3
|
||||
#define FILEID_UPDATE_KEY_3 4
|
||||
#define FILEID_CH_CERTIFICATE 5
|
||||
int flash_erase_binary (uint8_t file_id);
|
||||
int flash_write_binary (uint8_t file_id, const uint8_t *data,
|
||||
uint16_t len, uint16_t offset);
|
||||
|
||||
#define FLASH_CH_CERTIFICATE_SIZE 2048
|
||||
|
||||
extern const uint8_t *ch_certificate_start;
|
||||
|
||||
#define FIRMWARE_UPDATE_KEY_CONTENT_LEN 256 /* RSA-2048 (p and q) */
|
||||
|
||||
#define INITIAL_VECTOR_SIZE 16
|
||||
#define DATA_ENCRYPTION_KEY_SIZE 16
|
||||
|
||||
#define MAX_PRVKEY_LEN 512 /* Maximum is the case for RSA 4096-bit. */
|
||||
|
||||
struct key_data {
|
||||
const uint8_t *pubkey; /* Pointer to public key */
|
||||
uint8_t data[MAX_PRVKEY_LEN]; /* decrypted private key data content */
|
||||
};
|
||||
|
||||
struct prvkey_data {
|
||||
/*
|
||||
* IV: Initial Vector
|
||||
*/
|
||||
uint8_t iv[INITIAL_VECTOR_SIZE];
|
||||
/*
|
||||
* Checksum
|
||||
*/
|
||||
uint8_t checksum_encrypted[DATA_ENCRYPTION_KEY_SIZE];
|
||||
/*
|
||||
* DEK (Data Encryption Key) encrypted
|
||||
*/
|
||||
uint8_t dek_encrypted_1[DATA_ENCRYPTION_KEY_SIZE]; /* For user */
|
||||
uint8_t dek_encrypted_2[DATA_ENCRYPTION_KEY_SIZE]; /* For resetcode */
|
||||
uint8_t dek_encrypted_3[DATA_ENCRYPTION_KEY_SIZE]; /* For admin */
|
||||
};
|
||||
|
||||
#define BY_USER 1
|
||||
#define BY_RESETCODE 2
|
||||
#define BY_ADMIN 3
|
||||
|
||||
/*
|
||||
* Maximum length of pass phrase is 127.
|
||||
* We use the top bit (0x80) to encode if keystring is available within DO.
|
||||
*/
|
||||
#define PW_LEN_MAX 127
|
||||
#define PW_LEN_MASK 0x7f
|
||||
#define PW_LEN_KEYSTRING_BIT 0x80
|
||||
|
||||
#define SALT_SIZE 8
|
||||
|
||||
void s2k (const unsigned char *salt, size_t slen,
|
||||
const unsigned char *input, size_t ilen, unsigned char output[32]);
|
||||
|
||||
#define KEYSTRING_PASSLEN_SIZE 1
|
||||
#define KEYSTRING_SALT_SIZE SALT_SIZE
|
||||
#define KEYSTRING_MD_SIZE 32
|
||||
#define KEYSTRING_SIZE (KEYSTRING_PASSLEN_SIZE + KEYSTRING_SALT_SIZE \
|
||||
+ KEYSTRING_MD_SIZE)
|
||||
#define KS_META_SIZE (KEYSTRING_PASSLEN_SIZE + KEYSTRING_SALT_SIZE)
|
||||
#define KS_GET_SALT(ks) (ks + KEYSTRING_PASSLEN_SIZE)
|
||||
#define KS_GET_KEYSTRING(ks) (ks + KS_META_SIZE)
|
||||
|
||||
void gpg_do_clear_prvkey (enum kind_of_key kk);
|
||||
int gpg_do_load_prvkey (enum kind_of_key kk, int who, const uint8_t *keystring);
|
||||
int gpg_do_chks_prvkey (enum kind_of_key kk,
|
||||
int who_old, const uint8_t *old_ks,
|
||||
int who_new, const uint8_t *new_ks);
|
||||
|
||||
int gpg_change_keystring (int who_old, const uint8_t *old_ks,
|
||||
int who_new, const uint8_t *new_ks);
|
||||
|
||||
extern struct key_data kd[3];
|
||||
|
||||
|
||||
int rsa_sign (const uint8_t *, uint8_t *, int, struct key_data *, int);
|
||||
int modulus_calc (const uint8_t *, int, uint8_t *);
|
||||
int rsa_decrypt (const uint8_t *, uint8_t *, int, struct key_data *,
|
||||
unsigned int *);
|
||||
int rsa_verify (const uint8_t *, int, const uint8_t *, const uint8_t *);
|
||||
int rsa_genkey (int, uint8_t *, uint8_t *);
|
||||
|
||||
int ecdsa_sign_p256k1 (const uint8_t *hash, uint8_t *output,
|
||||
const uint8_t *key_data);
|
||||
int ecc_compute_public_p256k1 (const uint8_t *key_data, uint8_t *);
|
||||
int ecc_check_secret_p256k1 (const uint8_t *d0, uint8_t *d1);
|
||||
int ecdh_decrypt_p256k1 (const uint8_t *input, uint8_t *output,
|
||||
const uint8_t *key_data);
|
||||
|
||||
int eddsa_sign_25519 (const uint8_t *input, size_t ilen, uint32_t *output,
|
||||
const uint8_t *sk_a, const uint8_t *seed,
|
||||
const uint8_t *pk);
|
||||
void eddsa_compute_public_25519 (const uint8_t *a, uint8_t *);
|
||||
void ecdh_compute_public_25519 (const uint8_t *a, uint8_t *);
|
||||
int ecdh_decrypt_curve25519 (const uint8_t *input, uint8_t *output,
|
||||
const uint8_t *key_data);
|
||||
|
||||
void ecdh_compute_public_x448 (uint8_t *pubkey, const uint8_t *key_data);
|
||||
int ecdh_decrypt_x448 (uint8_t *output, const uint8_t *input,
|
||||
const uint8_t *key_data);
|
||||
|
||||
int ed448_sign (uint8_t *out, const uint8_t *input, unsigned int ilen,
|
||||
const uint8_t *a_in, const uint8_t *seed, const uint8_t *pk);
|
||||
void ed448_compute_public (uint8_t *pk, const uint8_t *a_in);
|
||||
|
||||
|
||||
const uint8_t *gpg_do_read_simple (uint8_t);
|
||||
void gpg_do_write_simple (uint8_t, const uint8_t *, int);
|
||||
void gpg_increment_digital_signature_counter (void);
|
||||
void gpg_do_get_initial_pw_setting (int is_pw3, int *r_len,
|
||||
const uint8_t **r_p);
|
||||
int gpg_do_kdf_check (int len, int how_many);
|
||||
int gpg_do_get_uif (enum kind_of_key kk);
|
||||
|
||||
|
||||
void fatal (uint8_t code) __attribute__ ((noreturn));
|
||||
#define FATAL_FLASH 1
|
||||
#define FATAL_RANDOM 2
|
||||
#define FATAL_HEAP 3
|
||||
|
||||
extern uint8_t keystring_md_pw3[KEYSTRING_MD_SIZE];
|
||||
extern uint8_t admin_authorized;
|
||||
|
||||
/*** Flash memory tag values ***/
|
||||
/* Data objects */
|
||||
/*
|
||||
* Representation of data object:
|
||||
*
|
||||
* <-1 halfword-> <--len/2 halfwords->
|
||||
* <-tag-><-len-> <---data content--->
|
||||
*/
|
||||
#define NR_DO_SEX 0x00
|
||||
#define NR_DO_FP_SIG 0x01
|
||||
#define NR_DO_FP_DEC 0x02
|
||||
#define NR_DO_FP_AUT 0x03
|
||||
#define NR_DO_CAFP_1 0x04
|
||||
#define NR_DO_CAFP_2 0x05
|
||||
#define NR_DO_CAFP_3 0x06
|
||||
#define NR_DO_KGTIME_SIG 0x07
|
||||
#define NR_DO_KGTIME_DEC 0x08
|
||||
#define NR_DO_KGTIME_AUT 0x09
|
||||
#define NR_DO_LOGIN_DATA 0x0a
|
||||
#define NR_DO_URL 0x0b
|
||||
#define NR_DO_NAME 0x0c
|
||||
#define NR_DO_LANGUAGE 0x0d
|
||||
#define NR_DO_PRVKEY_SIG 0x0e
|
||||
#define NR_DO_PRVKEY_DEC 0x0f
|
||||
#define NR_DO_PRVKEY_AUT 0x10
|
||||
#define NR_DO_KEYSTRING_PW1 0x11
|
||||
#define NR_DO_KEYSTRING_RC 0x12
|
||||
#define NR_DO_KEYSTRING_PW3 0x13
|
||||
#define NR_DO_KDF 0x14
|
||||
#define NR_DO__LAST__ 21 /* == 0x15 */
|
||||
/* 14-bit counter for DS: Recorded in flash memory by 1-halfword (2-byte). */
|
||||
/*
|
||||
* Representation of 14-bit counter:
|
||||
* 0: 0x8000
|
||||
* 1: 0x8001
|
||||
* ...
|
||||
* 16383: 0xbfff
|
||||
*/
|
||||
#define NR_COUNTER_DS 0x80 /* ..0xbf */
|
||||
/* 10-bit counter for DS: Recorded in flash memory by 1-halfword (2-byte). */
|
||||
/*
|
||||
* Representation of 10-bit counter:
|
||||
* 0: 0xc000
|
||||
* 1: 0xc001
|
||||
* ...
|
||||
* 1023: 0xc3ff
|
||||
*/
|
||||
#define NR_COUNTER_DS_LSB 0xc0 /* ..0xc3 */
|
||||
/*
|
||||
* Boolean object, small enum, or 8-bit integer:
|
||||
* Recorded in flash memory by 1-halfword (2-byte)
|
||||
*/
|
||||
/*
|
||||
* Representation of Boolean object:
|
||||
* 0: No record in flash memory
|
||||
* 1: 0xf000
|
||||
*/
|
||||
#define NR_BOOL_PW1_LIFETIME 0xf0
|
||||
/*
|
||||
* Representation of algorithm attribute object:
|
||||
* RSA-2048: No record in flash memory
|
||||
* RSA-4096: 0xf?00
|
||||
* ECC p256r1: 0xf?01
|
||||
* ECC p256k1: 0xf?02
|
||||
* ECC Ed25519: 0xf?03
|
||||
* ECC Curve25519: 0xf?04
|
||||
* where <?> == 1 (signature), 2 (decryption) or 3 (authentication)
|
||||
*/
|
||||
#define NR_KEY_ALGO_ATTR_SIG 0xf1
|
||||
#define NR_KEY_ALGO_ATTR_DEC 0xf2
|
||||
#define NR_KEY_ALGO_ATTR_AUT 0xf3
|
||||
/*
|
||||
* Representation of User Interaction Flag:
|
||||
* 0 (UIF disabled): 0xf?00 or No record in flash memory
|
||||
* 1 (UIF enabled): 0xf?01
|
||||
* 2 (UIF permanently enabled): 0xf?02
|
||||
*
|
||||
*/
|
||||
#define NR_DO_UIF_SIG 0xf6
|
||||
#define NR_DO_UIF_DEC 0xf7
|
||||
#define NR_DO_UIF_AUT 0xf8
|
||||
/*
|
||||
* NR_UINT_SOMETHING could be here... Use 0xf[459abcd]
|
||||
*/
|
||||
/* 123-counters: Recorded in flash memory by 2-halfword (4-byte). */
|
||||
/*
|
||||
* Representation of 123-counters:
|
||||
* 0: No record in flash memory
|
||||
* 1: 0xfe?? 0xffff
|
||||
* 2: 0xfe?? 0xc3c3
|
||||
* 3: 0xfe?? 0x0000
|
||||
* where <counter_id> is placed at second byte <??>
|
||||
*/
|
||||
#define NR_COUNTER_123 0xfe
|
||||
#define NR_EMPTY 0xff
|
||||
|
||||
#define SIZE_PW_STATUS_BYTES 7
|
||||
|
||||
|
||||
#define NUM_ALL_PRV_KEYS 3 /* SIG, DEC and AUT */
|
||||
|
||||
#if !defined(OPENPGP_CARD_INITIAL_PW1)
|
||||
#define OPENPGP_CARD_INITIAL_PW1 "123456"
|
||||
#endif
|
||||
|
||||
#if !defined(OPENPGP_CARD_INITIAL_PW3)
|
||||
#define OPENPGP_CARD_INITIAL_PW3 "12345678"
|
||||
#endif
|
||||
|
||||
extern const uint8_t openpgpcard_aid[];
|
||||
|
||||
void flash_bool_clear (const uint8_t **addr_p);
|
||||
const uint8_t *flash_bool_write (uint8_t nr);
|
||||
void flash_enum_clear (const uint8_t **addr_p);
|
||||
const uint8_t *flash_enum_write (uint8_t nr, uint8_t v);
|
||||
int flash_cnt123_get_value (const uint8_t *p);
|
||||
void flash_cnt123_increment (uint8_t which, const uint8_t **addr_p);
|
||||
void flash_cnt123_clear (const uint8_t **addr_p);
|
||||
void flash_put_data (uint16_t hw);
|
||||
void flash_warning (const char *msg);
|
||||
|
||||
void flash_put_data_internal (const uint8_t *p, uint16_t hw);
|
||||
void flash_bool_write_internal (const uint8_t *p, int nr);
|
||||
void flash_enum_write_internal (const uint8_t *p, int nr, uint8_t v);
|
||||
void flash_cnt123_write_internal (const uint8_t *p, int which, int v);
|
||||
void flash_do_write_internal (const uint8_t *p, int nr,
|
||||
const uint8_t *data, int len);
|
||||
|
||||
extern const uint8_t gnuk_string_serial[];
|
||||
|
||||
#define LED_ONESHOT 1
|
||||
#define LED_TWOSHOTS 2
|
||||
#define LED_SHOW_STATUS 4
|
||||
#define LED_FATAL 8
|
||||
#define LED_SYNC 16
|
||||
#define LED_GNUK_EXEC 32
|
||||
#define LED_START_COMMAND 64
|
||||
#define LED_FINISH_COMMAND 128
|
||||
#define LED_WAIT_FOR_BUTTON 256
|
||||
#define LED_OFF LED_FINISH_COMMAND
|
||||
void led_blink (int spec);
|
||||
|
||||
#if defined(PINPAD_SUPPORT)
|
||||
# if defined(PINPAD_CIR_SUPPORT)
|
||||
void cir_init (void);
|
||||
# elif defined(PINPAD_DIAL_SUPPORT)
|
||||
void dial_sw_disable (void);
|
||||
void dial_sw_enable (void);
|
||||
# elif defined(PINPAD_DND_SUPPORT)
|
||||
void msc_init (void);
|
||||
void msc_media_insert_change (int available);
|
||||
int msc_scsi_write (uint32_t lba, const uint8_t *buf, size_t size);
|
||||
int msc_scsi_read (uint32_t lba, const uint8_t **sector_p);
|
||||
void msc_scsi_stop (uint8_t code);
|
||||
# endif
|
||||
#define PIN_INPUT_CURRENT 1
|
||||
#define PIN_INPUT_NEW 2
|
||||
#define PIN_INPUT_CONFIRM 3
|
||||
#define MAX_PIN_CHARS 32
|
||||
extern uint8_t pin_input_buffer[MAX_PIN_CHARS];
|
||||
extern uint8_t pin_input_len;
|
||||
|
||||
int pinpad_getline (int msg_code, uint32_t timeout_usec);
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
extern uint8_t _regnual_start, __heap_end__[];
|
||||
|
||||
uint8_t * sram_address (uint32_t offset);
|
||||
|
||||
static inline const uint8_t *
|
||||
unique_device_id (void)
|
||||
{
|
||||
/*
|
||||
* STM32F103 has 96-bit unique device identifier.
|
||||
* This routine mimics that.
|
||||
*/
|
||||
|
||||
static const uint8_t id[] = { /* My RSA fingerprint */
|
||||
0x12, 0x41, 0x24, 0xBD, 0x3B, 0x48, 0x62, 0xAF,
|
||||
0x7A, 0x0A, 0x42, 0xF1, 0x00, 0xB4, 0x5E, 0xBD,
|
||||
0x4C, 0xA7, 0xBA, 0xBE
|
||||
};
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
|
|
@ -1,14 +0,0 @@
|
|||
/**
|
||||
* @brief Jacobian projective coordinates
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
bn256 x[1];
|
||||
bn256 y[1];
|
||||
bn256 z[1];
|
||||
} jpc;
|
||||
|
||||
void jpc_double_p256k1 (jpc *X, const jpc *A);
|
||||
void jpc_add_ac_p256k1 (jpc *X, const jpc *A, const ac *B);
|
||||
void jpc_add_ac_signed_p256k1 (jpc *X, const jpc *A, const ac *B, int minus);
|
||||
int jpc_to_ac_p256k1 (ac *X, const jpc *A);
|
||||
|
|
@ -1,199 +0,0 @@
|
|||
/*
|
||||
* jpc.c -- arithmetic on Jacobian projective coordinates.
|
||||
*
|
||||
* Copyright (C) 2011, 2013 Free Software Initiative of Japan
|
||||
* Author: NIIBE Yutaka <gniibe@fsij.org>
|
||||
*
|
||||
* This file is a part of Gnuk, a GnuPG USB Token implementation.
|
||||
*
|
||||
* Gnuk is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Gnuk is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
|
||||
* License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "field-group-select.h"
|
||||
|
||||
/**
|
||||
* @brief X = 2 * A
|
||||
*
|
||||
* @param X Destination JPC
|
||||
* @param A JPC
|
||||
*/
|
||||
void
|
||||
FUNC(jpc_double) (jpc *X, const jpc *A)
|
||||
{
|
||||
bn256 a[1], b[1], c[1], tmp0[1];
|
||||
bn256 *d;
|
||||
|
||||
if (bn256_is_zero (A->z)) /* A is infinite */
|
||||
return;
|
||||
|
||||
d = X->x;
|
||||
MFNC(sqr) (a, A->y);
|
||||
memcpy (b, a, sizeof (bn256));
|
||||
MFNC(mul) (a, a, A->x);
|
||||
MFNC(shift) (a, a, 2);
|
||||
|
||||
MFNC(sqr) (b, b);
|
||||
MFNC(shift) (b, b, 3);
|
||||
|
||||
#if defined(COEFFICIENT_A_IS_MINUS_3)
|
||||
MFNC(sqr) (tmp0, A->z);
|
||||
MFNC(sub) (c, A->x, tmp0);
|
||||
MFNC(add) (tmp0, tmp0, A->x);
|
||||
MFNC(mul) (tmp0, tmp0, c);
|
||||
MFNC(shift) (c, tmp0, 1);
|
||||
MFNC(add) (c, c, tmp0);
|
||||
#elif defined (COEFFICIENT_A_IS_ZERO)
|
||||
MFNC(sqr) (tmp0, A->x);
|
||||
MFNC(shift) (c, tmp0, 1);
|
||||
MFNC(add) (c, c, tmp0);
|
||||
#else
|
||||
#error "not supported."
|
||||
#endif
|
||||
|
||||
MFNC(sqr) (d, c);
|
||||
MFNC(shift) (tmp0, a, 1);
|
||||
MFNC(sub) (d, d, tmp0);
|
||||
|
||||
MFNC(mul) (X->z, A->y, A->z);
|
||||
MFNC(shift) (X->z, X->z, 1);
|
||||
|
||||
MFNC(sub) (tmp0, a, d);
|
||||
MFNC(mul) (tmp0, c, tmp0);
|
||||
MFNC(sub) (X->y, tmp0, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief X = A + B
|
||||
*
|
||||
* @param X Destination JPC
|
||||
* @param A JPC
|
||||
* @param B AC
|
||||
* @param MINUS if 1 subtraction, addition otherwise.
|
||||
*/
|
||||
void
|
||||
FUNC(jpc_add_ac_signed) (jpc *X, const jpc *A, const ac *B, int minus)
|
||||
{
|
||||
bn256 a[1], b[1], c[1], d[1], tmp[1];
|
||||
#define minus_B_y c
|
||||
#define c_sqr a
|
||||
#define c_cube b
|
||||
#define x1_c_sqr c
|
||||
#define x1_c_sqr_2 c
|
||||
#define c_cube_plus_x1_c_sqr_2 c
|
||||
#define x1_c_sqr_copy a
|
||||
#define y3_tmp c
|
||||
#define y1_c_cube a
|
||||
|
||||
if (bn256_is_zero (A->z)) /* A is infinite */
|
||||
{
|
||||
memcpy (X->x, B->x, sizeof (bn256));
|
||||
if (minus)
|
||||
{
|
||||
memcpy (tmp, B->y, sizeof (bn256));
|
||||
bn256_sub (X->y, CONST_P256, B->y);
|
||||
}
|
||||
else
|
||||
{
|
||||
memcpy (X->y, B->y, sizeof (bn256));
|
||||
bn256_sub (tmp, CONST_P256, B->y);
|
||||
}
|
||||
memset (X->z, 0, sizeof (bn256));
|
||||
X->z->word[0] = 1;
|
||||
return;
|
||||
}
|
||||
|
||||
MFNC(sqr) (a, A->z);
|
||||
memcpy (b, a, sizeof (bn256));
|
||||
MFNC(mul) (a, a, B->x);
|
||||
|
||||
MFNC(mul) (b, b, A->z);
|
||||
if (minus)
|
||||
{
|
||||
bn256_sub (minus_B_y, CONST_P256, B->y);
|
||||
MFNC(mul) (b, b, minus_B_y);
|
||||
}
|
||||
else
|
||||
{
|
||||
bn256_sub (tmp, CONST_P256, B->y);
|
||||
MFNC(mul) (b, b, B->y);
|
||||
}
|
||||
|
||||
if (bn256_cmp (A->x, a) == 0 && bn256_cmp (A->y, b) == 0)
|
||||
{
|
||||
FUNC(jpc_double) (X, A);
|
||||
return;
|
||||
}
|
||||
|
||||
MFNC(sub) (c, a, A->x);
|
||||
MFNC(sub) (d, b, A->y);
|
||||
|
||||
MFNC(mul) (X->z, A->z, c);
|
||||
|
||||
MFNC(sqr) (c_sqr, c);
|
||||
MFNC(mul) (c_cube, c_sqr, c);
|
||||
|
||||
MFNC(mul) (x1_c_sqr, A->x, c_sqr);
|
||||
|
||||
MFNC(sqr) (X->x, d);
|
||||
memcpy (x1_c_sqr_copy, x1_c_sqr, sizeof (bn256));
|
||||
MFNC(shift) (x1_c_sqr_2, x1_c_sqr, 1);
|
||||
MFNC(add) (c_cube_plus_x1_c_sqr_2, x1_c_sqr_2, c_cube);
|
||||
MFNC(sub) (X->x, X->x, c_cube_plus_x1_c_sqr_2);
|
||||
|
||||
MFNC(sub) (y3_tmp, x1_c_sqr_copy, X->x);
|
||||
MFNC(mul) (y3_tmp, y3_tmp, d);
|
||||
MFNC(mul) (y1_c_cube, A->y, c_cube);
|
||||
MFNC(sub) (X->y, y3_tmp, y1_c_cube);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief X = A + B
|
||||
*
|
||||
* @param X Destination JPC
|
||||
* @param A JPC
|
||||
* @param B AC
|
||||
*/
|
||||
void
|
||||
FUNC(jpc_add_ac) (jpc *X, const jpc *A, const ac *B)
|
||||
{
|
||||
FUNC(jpc_add_ac_signed) (X, A, B, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief X = convert A
|
||||
*
|
||||
* @param X Destination AC
|
||||
* @param A JPC
|
||||
*
|
||||
* Return -1 on error (infinite).
|
||||
* Return 0 on success.
|
||||
*/
|
||||
int
|
||||
FUNC(jpc_to_ac) (ac *X, const jpc *A)
|
||||
{
|
||||
bn256 z_inv[1], z_inv_sqr[1];
|
||||
|
||||
if (bn256_is_zero (A->z))
|
||||
return -1;
|
||||
|
||||
mod_inv (z_inv, A->z, CONST_P256);
|
||||
|
||||
MFNC(sqr) (z_inv_sqr, z_inv);
|
||||
MFNC(mul) (z_inv, z_inv, z_inv_sqr);
|
||||
|
||||
MFNC(mul) (X->x, A->x, z_inv_sqr);
|
||||
MFNC(mul) (X->y, A->y, z_inv);
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -1,36 +0,0 @@
|
|||
/*
|
||||
* jpc_p256k1.c -- arithmetic on Jacobian projective coordinates for p256k1.
|
||||
*
|
||||
* Copyright (C) 2014 Free Software Initiative of Japan
|
||||
* Author: NIIBE Yutaka <gniibe@fsij.org>
|
||||
*
|
||||
* This file is a part of Gnuk, a GnuPG USB Token implementation.
|
||||
*
|
||||
* Gnuk is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Gnuk is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
|
||||
* License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include "bn.h"
|
||||
#include "mod.h"
|
||||
#include "modp256k1.h"
|
||||
#include "affine.h"
|
||||
#include "jpc-ac_p256k1.h"
|
||||
|
||||
#define FIELD p256k1
|
||||
#define CONST_P256 P256K1
|
||||
#define COEFFICIENT_A_IS_ZERO 1
|
||||
|
||||
#include "jpc.c"
|
||||
|
|
@ -1,352 +0,0 @@
|
|||
/*
|
||||
* mod.c -- modulo arithmetic
|
||||
*
|
||||
* Copyright (C) 2011, 2014 Free Software Initiative of Japan
|
||||
* Author: NIIBE Yutaka <gniibe@fsij.org>
|
||||
*
|
||||
* This file is a part of Gnuk, a GnuPG USB Token implementation.
|
||||
*
|
||||
* Gnuk is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Gnuk is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
|
||||
* License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include "bn.h"
|
||||
|
||||
/**
|
||||
* @brief X = A mod B (using MU=(1<<(256)+MU_lower)) (Barret reduction)
|
||||
*
|
||||
*/
|
||||
void
|
||||
mod_reduce (bn256 *X, const bn512 *A, const bn256 *B, const bn256 *MU_lower)
|
||||
{
|
||||
bn256 q[1];
|
||||
bn512 q_big[1], tmp[1];
|
||||
uint32_t carry;
|
||||
#define borrow carry
|
||||
|
||||
memset (q, 0, sizeof (bn256));
|
||||
q->word[0] = A->word[15];
|
||||
bn256_mul (tmp, q, MU_lower);
|
||||
tmp->word[8] += A->word[15];
|
||||
carry = (tmp->word[8] < A->word[15]);
|
||||
tmp->word[9] += carry;
|
||||
|
||||
q->word[7] = A->word[14];
|
||||
q->word[6] = A->word[13];
|
||||
q->word[5] = A->word[12];
|
||||
q->word[4] = A->word[11];
|
||||
q->word[3] = A->word[10];
|
||||
q->word[2] = A->word[9];
|
||||
q->word[1] = A->word[8];
|
||||
q->word[0] = A->word[7];
|
||||
bn256_mul (q_big, q, MU_lower);
|
||||
bn256_add ((bn256 *)&q_big->word[8], (bn256 *)&q_big->word[8], q);
|
||||
|
||||
q->word[0] = q_big->word[9] + tmp->word[1];
|
||||
carry = (q->word[0] < tmp->word[1]);
|
||||
|
||||
q->word[1] = q_big->word[10] + carry;
|
||||
carry = (q->word[1] < carry);
|
||||
q->word[1] += tmp->word[2];
|
||||
carry += (q->word[1] < tmp->word[2]);
|
||||
|
||||
q->word[2] = q_big->word[11] + carry;
|
||||
carry = (q->word[2] < carry);
|
||||
q->word[2] += tmp->word[3];
|
||||
carry += (q->word[2] < tmp->word[3]);
|
||||
|
||||
q->word[3] = q_big->word[12] + carry;
|
||||
carry = (q->word[3] < carry);
|
||||
q->word[3] += tmp->word[4];
|
||||
carry += (q->word[3] < tmp->word[4]);
|
||||
|
||||
q->word[4] = q_big->word[13] + carry;
|
||||
carry = (q->word[4] < carry);
|
||||
q->word[4] += tmp->word[5];
|
||||
carry += (q->word[4] < tmp->word[5]);
|
||||
|
||||
q->word[5] = q_big->word[14] + carry;
|
||||
carry = (q->word[5] < carry);
|
||||
q->word[5] += tmp->word[6];
|
||||
carry += (q->word[5] < tmp->word[6]);
|
||||
|
||||
q->word[6] = q_big->word[15] + carry;
|
||||
carry = (q->word[6] < carry);
|
||||
q->word[6] += tmp->word[7];
|
||||
carry += (q->word[6] < tmp->word[7]);
|
||||
|
||||
q->word[7] = carry;
|
||||
q->word[7] += tmp->word[8];
|
||||
carry = (q->word[7] < tmp->word[8]);
|
||||
|
||||
memset (q_big, 0, sizeof (bn512));
|
||||
q_big->word[8] = A->word[8];
|
||||
q_big->word[7] = A->word[7];
|
||||
q_big->word[6] = A->word[6];
|
||||
q_big->word[5] = A->word[5];
|
||||
q_big->word[4] = A->word[4];
|
||||
q_big->word[3] = A->word[3];
|
||||
q_big->word[2] = A->word[2];
|
||||
q_big->word[1] = A->word[1];
|
||||
q_big->word[0] = A->word[0];
|
||||
|
||||
bn256_mul (tmp, q, B);
|
||||
tmp->word[8] += carry * B->word[0];
|
||||
tmp->word[15] = tmp->word[14] = tmp->word[13] = tmp->word[12]
|
||||
= tmp->word[11] = tmp->word[10] = tmp->word[9] = 0;
|
||||
|
||||
borrow = bn256_sub (X, (bn256 *)&q_big->word[0], (bn256 *)&tmp->word[0]);
|
||||
q_big->word[8] -= borrow;
|
||||
q_big->word[8] -= tmp->word[8];
|
||||
|
||||
carry = q_big->word[8];
|
||||
if (carry)
|
||||
carry -= bn256_sub (X, X, B);
|
||||
else
|
||||
bn256_sub (q, X, B);
|
||||
|
||||
if (carry)
|
||||
bn256_sub (X, X, B);
|
||||
else
|
||||
bn256_sub (q, X, B);
|
||||
|
||||
borrow = bn256_sub (q, X, B);
|
||||
if (borrow)
|
||||
memcpy (q, X, sizeof (bn256));
|
||||
else
|
||||
memcpy (X, q, sizeof (bn256));
|
||||
#undef borrow
|
||||
}
|
||||
|
||||
/*
|
||||
* Reference:
|
||||
* Donald E. Knuth, The Art of Computer Programming, Vol. 2:
|
||||
* Seminumerical Algorithms, 3rd ed. Reading, MA: Addison-Wesley, 1998
|
||||
*
|
||||
* Max loop: X=0x8000...0000 and N=0xffff...ffff
|
||||
*/
|
||||
#define MAX_GCD_STEPS_BN256 (3*256-2)
|
||||
|
||||
/**
|
||||
* @brief C = X^(-1) mod N
|
||||
*
|
||||
* Assume X and N are co-prime (or N is prime).
|
||||
* NOTE: If X==0, it return 0.
|
||||
*
|
||||
*/
|
||||
void
|
||||
mod_inv (bn256 *C, const bn256 *X, const bn256 *N)
|
||||
{
|
||||
bn256 u[1], v[1], tmp[1];
|
||||
bn256 A[1] = { { { 1, 0, 0, 0, 0, 0, 0, 0 } } };
|
||||
uint32_t carry;
|
||||
#define borrow carry
|
||||
int n = MAX_GCD_STEPS_BN256;
|
||||
|
||||
memset (tmp, 0, sizeof (bn256));
|
||||
memset (C, 0, sizeof (bn256));
|
||||
memcpy (u, X, sizeof (bn256));
|
||||
memcpy (v, N, sizeof (bn256));
|
||||
|
||||
while (n--)
|
||||
{
|
||||
int c = (bn256_is_even (u) << 1) + bn256_is_even (v);
|
||||
|
||||
switch (c)
|
||||
{
|
||||
case 3:
|
||||
bn256_shift (u, u, -1);
|
||||
if (bn256_is_even (A))
|
||||
{
|
||||
bn256_add (tmp, A, N);
|
||||
carry = 0;
|
||||
}
|
||||
else
|
||||
carry = bn256_add (A, A, N);
|
||||
|
||||
bn256_shift (A, A, -1);
|
||||
A->word[7] |= carry * 0x80000000;
|
||||
|
||||
bn256_shift (v, v, -1);
|
||||
if (bn256_is_even (C))
|
||||
{
|
||||
bn256_add (tmp, C, N);
|
||||
carry = 0;
|
||||
}
|
||||
else
|
||||
carry = bn256_add (C, C, N);
|
||||
|
||||
bn256_shift (C, C, -1);
|
||||
C->word[7] |= carry * 0x80000000;
|
||||
|
||||
if (bn256_is_ge (tmp, tmp))
|
||||
{
|
||||
bn256_sub (tmp, tmp, tmp);
|
||||
borrow = bn256_sub (tmp, tmp, tmp);
|
||||
if (borrow)
|
||||
bn256_add (tmp, tmp, tmp);
|
||||
else
|
||||
bn256_add (tmp, A, N);
|
||||
}
|
||||
else
|
||||
{
|
||||
bn256_sub (tmp, tmp, tmp);
|
||||
borrow = bn256_sub (tmp, tmp, tmp);
|
||||
if (borrow)
|
||||
bn256_add (tmp, tmp, tmp);
|
||||
else
|
||||
bn256_add (tmp, tmp, N);
|
||||
}
|
||||
break;
|
||||
|
||||
case 1:
|
||||
bn256_shift (tmp, tmp, -1);
|
||||
if (bn256_is_even (tmp))
|
||||
{
|
||||
bn256_add (tmp, tmp, N);
|
||||
carry = 0;
|
||||
}
|
||||
else
|
||||
carry = bn256_add (tmp, tmp, N);
|
||||
|
||||
bn256_shift (tmp, tmp, -1);
|
||||
tmp->word[7] |= carry * 0x80000000;
|
||||
|
||||
bn256_shift (v, v, -1);
|
||||
if (bn256_is_even (C))
|
||||
{
|
||||
bn256_add (tmp, C, N);
|
||||
carry = 0;
|
||||
}
|
||||
else
|
||||
carry = bn256_add (C, C, N);
|
||||
|
||||
bn256_shift (C, C, -1);
|
||||
C->word[7] |= carry * 0x80000000;
|
||||
|
||||
if (bn256_is_ge (tmp, tmp))
|
||||
{
|
||||
bn256_sub (tmp, tmp, tmp);
|
||||
borrow = bn256_sub (tmp, tmp, tmp);
|
||||
if (borrow)
|
||||
bn256_add (tmp, tmp, tmp);
|
||||
else
|
||||
bn256_add (tmp, A, N);
|
||||
}
|
||||
else
|
||||
{
|
||||
bn256_sub (tmp, tmp, tmp);
|
||||
borrow = bn256_sub (tmp, tmp, tmp);
|
||||
if (borrow)
|
||||
bn256_add (tmp, tmp, tmp);
|
||||
else
|
||||
bn256_add (tmp, tmp, N);
|
||||
}
|
||||
break;
|
||||
|
||||
case 2:
|
||||
bn256_shift (u, u, -1);
|
||||
if (bn256_is_even (A))
|
||||
{
|
||||
bn256_add (tmp, A, N);
|
||||
carry = 0;
|
||||
}
|
||||
else
|
||||
carry = bn256_add (A, A, N);
|
||||
|
||||
bn256_shift (A, A, -1);
|
||||
A->word[7] |= carry * 0x80000000;
|
||||
|
||||
bn256_shift (tmp, tmp, -1);
|
||||
if (bn256_is_even (tmp))
|
||||
{
|
||||
bn256_add (tmp, tmp, N);
|
||||
carry = 0;
|
||||
}
|
||||
else
|
||||
carry = bn256_add (tmp, tmp, N);
|
||||
|
||||
bn256_shift (tmp, tmp, -1);
|
||||
tmp->word[7] |= carry * 0x80000000;
|
||||
|
||||
if (bn256_is_ge (tmp, tmp))
|
||||
{
|
||||
bn256_sub (tmp, tmp, tmp);
|
||||
borrow = bn256_sub (tmp, tmp, tmp);
|
||||
if (borrow)
|
||||
bn256_add (tmp, tmp, tmp);
|
||||
else
|
||||
bn256_add (tmp, A, N);
|
||||
}
|
||||
else
|
||||
{
|
||||
bn256_sub (tmp, tmp, tmp);
|
||||
borrow = bn256_sub (tmp, tmp, tmp);
|
||||
if (borrow)
|
||||
bn256_add (tmp, tmp, tmp);
|
||||
else
|
||||
bn256_add (tmp, tmp, N);
|
||||
}
|
||||
break;
|
||||
|
||||
case 0:
|
||||
bn256_shift (tmp, tmp, -1);
|
||||
if (bn256_is_even (tmp))
|
||||
{
|
||||
bn256_add (tmp, tmp, N);
|
||||
carry = 0;
|
||||
}
|
||||
else
|
||||
carry = bn256_add (tmp, tmp, N);
|
||||
|
||||
bn256_shift (tmp, tmp, -1);
|
||||
tmp->word[7] |= carry * 0x80000000;
|
||||
|
||||
bn256_shift (tmp, tmp, -1);
|
||||
if (bn256_is_even (tmp))
|
||||
{
|
||||
bn256_add (tmp, tmp, N);
|
||||
carry = 0;
|
||||
}
|
||||
else
|
||||
carry = bn256_add (tmp, tmp, N);
|
||||
|
||||
bn256_shift (tmp, tmp, -1);
|
||||
tmp->word[7] |= carry * 0x80000000;
|
||||
|
||||
if (bn256_is_ge (u, v))
|
||||
{
|
||||
bn256_sub (u, u, v);
|
||||
borrow = bn256_sub (A, A, C);
|
||||
if (borrow)
|
||||
bn256_add (A, A, N);
|
||||
else
|
||||
bn256_add (tmp, A, N);
|
||||
}
|
||||
else
|
||||
{
|
||||
bn256_sub (v, v, u);
|
||||
borrow = bn256_sub (C, C, A);
|
||||
if (borrow)
|
||||
bn256_add (C, C, N);
|
||||
else
|
||||
bn256_add (tmp, C, N);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
#undef borrow
|
||||
}
|
||||
|
|
@ -1,3 +0,0 @@
|
|||
void mod_reduce (bn256 *X, const bn512 *A, const bn256 *B,
|
||||
const bn256 *MU_lower);
|
||||
void mod_inv (bn256 *X, const bn256 *A, const bn256 *N);
|
||||
|
|
@ -1,287 +0,0 @@
|
|||
/*
|
||||
* mod25638.c -- modulo arithmetic of 2^256-38 for 2^255-19 field
|
||||
*
|
||||
* Copyright (C) 2014 Free Software Initiative of Japan
|
||||
* Author: NIIBE Yutaka <gniibe@fsij.org>
|
||||
*
|
||||
* This file is a part of Gnuk, a GnuPG USB Token implementation.
|
||||
*
|
||||
* Gnuk is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Gnuk is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
|
||||
* License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* The field is \Z/(2^255-19)
|
||||
*
|
||||
* We use radix-32. During computation, it's not reduced to 2^255-19,
|
||||
* but it is represented in 256-bit (it is redundant representation),
|
||||
* that is, something like 2^256-38.
|
||||
*
|
||||
* The idea is, keeping within 256-bit until it will be converted to
|
||||
* affine coordinates.
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "bn.h"
|
||||
#include "mod25638.h"
|
||||
|
||||
#ifndef BN256_C_IMPLEMENTATION
|
||||
#define ASM_IMPLEMENTATION 0
|
||||
#endif
|
||||
|
||||
#if ASM_IMPLEMENTATION
|
||||
#include "muladd_256.h"
|
||||
#define ADDWORD_256(d_,s_,w_,c_) \
|
||||
asm ( "ldmia %[s]!, { r4, r5, r6, r7 } \n\t" \
|
||||
"adds r4, r4, %[w] \n\t" \
|
||||
"adcs r5, r5, #0 \n\t" \
|
||||
"adcs r6, r6, #0 \n\t" \
|
||||
"adcs r7, r7, #0 \n\t" \
|
||||
"stmia %[d]!, { r4, r5, r6, r7 }\n\t" \
|
||||
"ldmia %[s]!, { r4, r5, r6, r7 } \n\t" \
|
||||
"adcs r4, r4, #0 \n\t" \
|
||||
"adcs r5, r5, #0 \n\t" \
|
||||
"adcs r6, r6, #0 \n\t" \
|
||||
"adcs r7, r7, #0 \n\t" \
|
||||
"stmia %[d]!, { r4, r5, r6, r7 }\n\t" \
|
||||
"mov %[c], #0 \n\t" \
|
||||
"adc %[c], %[c], #0" \
|
||||
: [s] "=&r" (s_), [d] "=&r" (d_), [c] "=&r" (c_) \
|
||||
: "[s]" (s_), "[d]" (d_), [w] "r" (w_) \
|
||||
: "r4", "r5", "r6", "r7", "memory", "cc" )
|
||||
#endif
|
||||
|
||||
/*
|
||||
256 224 192 160 128 96 64 32 0
|
||||
2^256
|
||||
1 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
|
||||
2^256 - 16
|
||||
0 ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff fffffff0
|
||||
2^256 - 16 - 2
|
||||
0 ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffee
|
||||
2^256 - 16 - 2 - 1
|
||||
0 ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffed
|
||||
*/
|
||||
const bn256 p25519[1] = {
|
||||
{{ 0xffffffed, 0xffffffff, 0xffffffff, 0xffffffff,
|
||||
0xffffffff, 0xffffffff, 0xffffffff, 0x7fffffff }} };
|
||||
|
||||
|
||||
/*
|
||||
* Implementation Note.
|
||||
*
|
||||
* It's not always modulo n25638. The representation is redundant
|
||||
* during computation. For example, when we add the number - 1 and 1,
|
||||
* it won't overflow to 2^256, and the result is represented within
|
||||
* 256-bit.
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @brief X = (A + B) mod 2^256-38
|
||||
*/
|
||||
void
|
||||
mod25638_add (bn256 *X, const bn256 *A, const bn256 *B)
|
||||
{
|
||||
uint32_t carry;
|
||||
|
||||
carry = bn256_add (X, A, B);
|
||||
carry = bn256_add_uint (X, X, carry*38);
|
||||
X->word[0] += carry * 38;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief X = (A - B) mod 2^256-38
|
||||
*/
|
||||
void
|
||||
mod25638_sub (bn256 *X, const bn256 *A, const bn256 *B)
|
||||
{
|
||||
uint32_t borrow;
|
||||
|
||||
borrow = bn256_sub (X, A, B);
|
||||
borrow = bn256_sub_uint (X, X, borrow*38);
|
||||
X->word[0] -= borrow * 38;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief X = A mod 2^256-38
|
||||
*
|
||||
* Note that the second argument is not "const bn512 *".
|
||||
* A is modified during the computation of modulo.
|
||||
*
|
||||
* It's not precisely modulo 2^256-38 for all cases,
|
||||
* but result may be redundant.
|
||||
*/
|
||||
static void
|
||||
mod25638_reduce (bn256 *X, bn512 *A)
|
||||
{
|
||||
const uint32_t *s;
|
||||
uint32_t *d;
|
||||
uint32_t w;
|
||||
|
||||
#if ASM_IMPLEMENTATION
|
||||
uint32_t c, c0;
|
||||
|
||||
s = &A->word[8]; d = &A->word[0]; w = 38; MULADD_256 (s, d, w, c);
|
||||
c0 = A->word[8] * 38;
|
||||
d = &X->word[0];
|
||||
s = &A->word[0];
|
||||
ADDWORD_256 (d, s, c0, c);
|
||||
X->word[0] += c * 38;
|
||||
#else
|
||||
s = &A->word[8]; d = &A->word[0]; w = 38;
|
||||
{
|
||||
int i;
|
||||
uint64_t r;
|
||||
uint32_t carry;
|
||||
|
||||
r = 0;
|
||||
for (i = 0; i < BN256_WORDS; i++)
|
||||
{
|
||||
uint64_t uv;
|
||||
|
||||
r += d[i];
|
||||
carry = (r < d[i]);
|
||||
|
||||
uv = ((uint64_t)s[i])*w;
|
||||
r += uv;
|
||||
carry += (r < uv);
|
||||
|
||||
d[i] = (uint32_t)r;
|
||||
r = ((r >> 32) | ((uint64_t)carry << 32));
|
||||
}
|
||||
|
||||
carry = bn256_add_uint (X, (bn256 *)A, r * 38);
|
||||
X->word[0] += carry * 38;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief X = (A * B) mod 2^256-38
|
||||
*/
|
||||
void
|
||||
mod25638_mul (bn256 *X, const bn256 *A, const bn256 *B)
|
||||
{
|
||||
bn512 tmp[1];
|
||||
|
||||
bn256_mul (tmp, A, B);
|
||||
mod25638_reduce (X, tmp);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief X = A * A mod 2^256-38
|
||||
*/
|
||||
void
|
||||
mod25638_sqr (bn256 *X, const bn256 *A)
|
||||
{
|
||||
bn512 tmp[1];
|
||||
|
||||
bn256_sqr (tmp, A);
|
||||
mod25638_reduce (X, tmp);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief X = (A << shift) mod 2^256-38
|
||||
* @note shift < 32
|
||||
*/
|
||||
void
|
||||
mod25638_shift (bn256 *X, const bn256 *A, int shift)
|
||||
{
|
||||
uint32_t carry;
|
||||
bn256 tmp[1];
|
||||
|
||||
carry = bn256_shift (X, A, shift);
|
||||
if (shift < 0)
|
||||
return;
|
||||
|
||||
memset (tmp, 0, sizeof (bn256));
|
||||
tmp->word[0] = (carry << 1);
|
||||
/* tmp->word[1] = (carry >> 31); always zero. */
|
||||
tmp->word[0] = tmp->word[0] + (carry << 2);
|
||||
tmp->word[1] = (tmp->word[0] < (carry << 2)) + (carry >> 30);
|
||||
tmp->word[0] = tmp->word[0] + (carry << 5);
|
||||
tmp->word[1] = tmp->word[1] + (tmp->word[0] < (carry << 5)) + (carry >> 27);
|
||||
|
||||
mod25638_add (X, X, tmp);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* @brief X = A mod 2^255-19
|
||||
*
|
||||
* It's precisely modulo 2^255-19 (unlike mod25638_reduce).
|
||||
*/
|
||||
void
|
||||
mod25519_reduce (bn256 *X)
|
||||
{
|
||||
uint32_t q;
|
||||
bn256 r0[1], r1[1];
|
||||
int flag;
|
||||
|
||||
memcpy (r0, X, sizeof (bn256));
|
||||
q = (r0->word[7] >> 31);
|
||||
r0->word[7] &= 0x7fffffff;
|
||||
if (q)
|
||||
{
|
||||
bn256_add_uint (r0, r0, 19);
|
||||
q = (r0->word[7] >> 31);
|
||||
r0->word[7] &= 0x7fffffff;
|
||||
if (q)
|
||||
{
|
||||
bn256_add_uint (r1, r0, 19);
|
||||
q = (r1->word[7] >> 31);
|
||||
r1->word[7] &= 0x7fffffff;
|
||||
flag = 0;
|
||||
}
|
||||
else
|
||||
flag = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
bn256_add_uint (r1, r0, 19);
|
||||
q = (r1->word[7] >> 31); /* dummy */
|
||||
r1->word[7] &= 0x7fffffff; /* dummy */
|
||||
if (q)
|
||||
flag = 2;
|
||||
else
|
||||
flag = 3;
|
||||
}
|
||||
|
||||
if (flag)
|
||||
{
|
||||
bn256_add_uint (r1, r0, 19);
|
||||
q = (r1->word[7] >> 31);
|
||||
r1->word[7] &= 0x7fffffff;
|
||||
if (q)
|
||||
memcpy (X, r1, sizeof (bn256));
|
||||
else
|
||||
memcpy (X, r0, sizeof (bn256));
|
||||
}
|
||||
else
|
||||
{
|
||||
if (q)
|
||||
{
|
||||
asm volatile ("" : : "r" (q) : "memory");
|
||||
memcpy (X, r1, sizeof (bn256));
|
||||
asm volatile ("" : : "r" (q) : "memory");
|
||||
}
|
||||
else
|
||||
memcpy (X, r1, sizeof (bn256));
|
||||
}
|
||||
}
|
||||
|
|
@ -1,7 +0,0 @@
|
|||
extern const bn256 p25519[1];
|
||||
|
||||
void mod25638_add (bn256 *X, const bn256 *A, const bn256 *B);
|
||||
void mod25638_sub (bn256 *X, const bn256 *A, const bn256 *B);
|
||||
void mod25638_mul (bn256 *X, const bn256 *A, const bn256 *B);
|
||||
void mod25638_sqr (bn256 *X, const bn256 *A);
|
||||
void mod25519_reduce (bn256 *X);
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Reference in a new issue