├── .github ├── 001-enable-pcscd.rules └── workflows │ └── oseid-github-ci-test.yaml ├── Licence.txt ├── README ├── doc ├── Changelog ├── T1_CRC.txt ├── build_pdf_doc.sh ├── dieharder_card.txt ├── dieharder_token.txt ├── doc.txt └── kicad │ └── OsEID_AVR128DA32 │ ├── OsEID-B.Cu.gbr │ ├── OsEID-B.Cu.svg │ ├── OsEID-cache.lib │ ├── OsEID.kicad_pcb │ ├── OsEID.net │ ├── OsEID.pro │ ├── OsEID.sch │ ├── OsEID_AVR_v1.0.lib │ ├── OsEID_footprints.pretty │ ├── ISO7816-3_pads_6.kicad_mod │ ├── PIN_ARRAY_3X1.kicad_mod │ └── TQFP-32_7x7mm_P0.8mm.kicad_mod │ ├── fp-lib-table │ └── sym-lib-table ├── download ├── OsEID.reg ├── OsEID_AVR128DA32.hex ├── OsEID_AVR128DA32_card_PCB_gerber.pho ├── OsEID_card.hex ├── OsEID_card_PCB.svg ├── OsEID_card_PCB_gerber.pho ├── OsEID_card_eeprom.hex ├── OsEID_token.hex ├── OsEID_token.ngc ├── oseid.profile ├── oseid_0.15.profile ├── oseid_0.16.profile ├── oseid_0.17.profile ├── oseid_0.18.profile ├── oseid_0.19.profile ├── oseid_0.20.profile ├── oseid_0.21.profile ├── oseid_0.22.profile ├── oseid_0.23.profile ├── oseid_0.24.profile └── oseid_0.25.profile ├── src ├── Makefile.AVR128DA ├── Makefile.STM32F10x ├── Makefile.atmega128 ├── Makefile.console ├── Makefile.simulavr ├── Makefile.simulavr1284 ├── Makefile.xmega128a4u ├── card_os │ ├── Makefile │ ├── T1_transport.c │ ├── aes.c │ ├── aes.h │ ├── bn_lib.h │ ├── card.c │ ├── card_io.h │ ├── constants.c │ ├── constants.h │ ├── debug.h │ ├── des.c │ ├── des.h │ ├── ec.c │ ├── ec.h │ ├── fs.c │ ├── fs.h │ ├── iso7816.c │ ├── iso7816.h │ ├── key.h │ ├── mem_device.h │ ├── myeid_emu.c │ ├── myeid_emu.h │ ├── rnd.h │ ├── rsa.c │ └── rsa.h ├── lib │ ├── ARM │ │ └── bn_lib_arm_cm3.c │ ├── avr │ │ ├── aes.S │ │ ├── bn_inv_mod.S │ │ ├── bn_lib.S │ │ ├── des.S │ │ ├── ec_fast_red.S │ │ ├── load_sp.h │ │ ├── mp.S │ │ ├── mp_mul_128.S │ │ ├── mp_mul_192.S │ │ ├── mp_mul_24.S │ │ ├── mp_mul_256.S │ │ ├── mp_mul_32.S │ │ ├── mp_mul_32_trunc.S │ │ ├── mp_mul_521.S │ │ ├── mp_square.S │ │ └── rsa.S │ └── generic │ │ └── bn_lib.c └── targets │ ├── AVR128DA │ ├── avr.S │ ├── card_io.S │ ├── card_io.c │ ├── constants.c │ ├── device-specs │ │ └── specs-avr128da32 │ ├── mem_device.S │ └── rnd.c │ ├── STM32F10x │ ├── LED.h │ ├── README │ ├── STM32F10X.ld │ ├── STM32F10x_dev_init.c │ ├── STM32F10x_flash.device.c │ ├── STM32F10x_flash.device.h │ ├── STM32F10x_init.S │ ├── ccid.c │ ├── ccid.h │ ├── constants.c │ ├── flash_cow_dev.c │ ├── flash_cow_dev.h │ ├── mem_device.c │ ├── os.h │ ├── rnd.c │ ├── serial_debug.c │ ├── serial_debug.h │ ├── usb.h │ └── usb_common.c │ ├── atmega128 │ ├── avr.c │ ├── card_io.S │ ├── constants.c │ ├── mem_device.S │ └── rnd.c │ ├── console │ ├── card_io.c │ ├── mem_device.c │ ├── pcscd │ │ ├── OsEIDsim │ │ │ ├── Makefile │ │ │ ├── hex2bytes.c │ │ │ ├── ifdhandler.c │ │ │ ├── ifdhandler.txt │ │ │ ├── reader.conf │ │ │ ├── run_pcscd.sh │ │ │ ├── serial.c │ │ │ └── serial.h │ │ └── Readme │ ├── restart.c │ ├── restart.h │ └── rnd.c │ ├── simulavr │ ├── Readme │ ├── avr.c │ ├── card_io.S │ ├── constants.c │ ├── gdb_macros │ ├── mem_device.S │ ├── rnd.c │ └── run_pcscd.sh │ └── xmega128a4u │ ├── LED.h │ ├── avr_os.c │ ├── avr_os.h │ ├── avrxmega7_OsEID.x │ ├── ccid.c │ ├── ccid.h │ ├── constants.c │ ├── mem_device.c │ ├── rnd.c │ ├── serial_debug.c │ ├── serial_debug.h │ ├── usb.c │ └── usb.h └── tools └── OsEID-tool /.github/001-enable-pcscd.rules: -------------------------------------------------------------------------------- 1 | polkit.addRule(function(action, subject) { 2 | if (action.id == "org.debian.pcsc-lite.access_card") { 3 | return polkit.Result.YES; 4 | } 5 | }); 6 | 7 | polkit.addRule(function(action, subject) { 8 | if (action.id == "org.debian.pcsc-lite.access_pcsc") { 9 | return polkit.Result.YES; 10 | } 11 | }); 12 | -------------------------------------------------------------------------------- /.github/workflows/oseid-github-ci-test.yaml: -------------------------------------------------------------------------------- 1 | name: GitHub Actions for OsEID 2 | on: [push] 3 | jobs: 4 | OsEID-GitHub-Actions: 5 | runs-on: ubuntu-latest 6 | steps: 7 | - run: echo "The job was automatically triggered by a ${{ github.event_name }} event." 8 | - run: echo "This job is now running on a ${{ runner.os }} server hosted by GitHub!" 9 | - run: echo "The name of your branch is ${{ github.ref }} and your repository is ${{ github.repository }}." 10 | - name: Check out repository code 11 | uses: actions/checkout@v3 12 | - run: echo "The ${{ github.repository }} repository has been cloned to the runner." 13 | - name: Install additional packages 14 | run: | 15 | sudo apt-get install libpcsclite-dev pcscd socat pcsc-tools gcc-avr avr-libc binutils-avr srecord 16 | sudo apt-get install gcc-arm-none-eabi binutils-arm-none-eabi 17 | sudo apt-get install opensc 18 | - name: OsEID - cross compilation for AVR devices 19 | run: | 20 | cd src 21 | make -f Makefile.AVR128DA 22 | make -f Makefile.xmega128a4u 23 | make -f Makefile.atmega128 24 | make -f Makefile.simulavr 25 | make -f Makefile.STM32F10x 26 | cd .. 27 | - name: Upload build artifacts 28 | uses: actions/upload-artifact@v4 29 | with: 30 | name: OsEID-AVR-build 31 | path: 32 | src/build/* 33 | - name: OsEID compile and run 34 | run: | 35 | sudo cp .github/001-enable-pcscd.rules /etc/polkit-1/rules.d/001-enable-pcscd.rules 36 | cd src 37 | make -f Makefile.console 38 | mkdir tmp; 39 | socat -d -d pty,link=tmp/OsEIDsim.socket,raw,echo=0 "exec:build/console/console ...,pty,raw,echo=0" & 40 | PID=$! 41 | sleep 1 42 | echo "# OsEIDsim" > tmp/reader.conf 43 | echo 'FRIENDLYNAME "OsEIDsim"' >> tmp/reader.conf 44 | echo "DEVICENAME ${{ github.workspace }}/src/tmp/OsEIDsim.socket" >> tmp/reader.conf 45 | echo "LIBPATH ${{ github.workspace }}/src/build/console/libOsEIDsim.so.0.0.1" >> tmp/reader.conf 46 | echo "CHANNELID 1" >> tmp/reader.conf 47 | sudo mv tmp/reader.conf /etc/reader.conf.d/reader.conf 48 | cat /etc/reader.conf.d/reader.conf 49 | sudo systemctl stop pcscd.service pcscd.socket 50 | sudo systemctl start pcscd.service pcscd.socket 51 | - name: compile OpenSC from git 52 | run: | 53 | sudo dpkg --purge opensc 54 | cd ${{ github.workspace }} 55 | git clone https://github.com/OpenSC/OpenSC 56 | set -ex; 57 | cd OpenSC/; 58 | ./bootstrap; 59 | ./configure; 60 | sudo make install; 61 | - name: OsEID test (OpenSC from git) 62 | run: | 63 | export LD_LIBRARY_PATH=/usr/local/lib; 64 | cd ${{ github.workspace }}/tools 65 | export TERM=xterm-256color 66 | ./OsEID-tool FULL-TEST 67 | - run: echo "This job's status is ${{ job.status }}." 68 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | Description 2 | 3 | AVR128DA32 microcontroller based open source EID smartcard with RSA 4 | (512-2048) and ECC - ECDSA and ECDH operations on prime192v1, prime256v1, 5 | secp384r1, secp256k1 and secp521r1. Compatible with MyEID card from Aventra. 6 | Supported in windows and linux by opensc package. Allow about 64KiB space 7 | for keys/certificates. PKCS#15 structure supported. 8 | 9 | USB token with CCID interface based on atmel xmega128a4u with same features as card. 10 | 11 | USB token based on ST LINK V2 (STM32F102CB/STM32F103CB/MH2103CB ARM processor). 12 | 13 | Online HTML doc https://popovec.github.io/OsEID/ 14 | 15 | PDF documentation available at https://oseid.sourceforge.io 16 | -------------------------------------------------------------------------------- /doc/Changelog: -------------------------------------------------------------------------------- 1 | OsEID 20210701 (released) 2 | Author: Peter Popovec 3 | Date: Thu Jul 1 10:39:27 CEST 2021 4 | 5 | * AES/DES fully compatible with MyEID 4.5.5 (AES-ECB/CBC/CBC-PAD) 6 | * key wrap/unwrap improved 7 | * modular multiplicative inversion - AVR-ASM code 8 | * T1 protocol for AVR128DA32 based device 9 | 10 | OsEID 20201130 (released) 11 | Author: Peter Popovec 12 | Date: Mon Nov 30 13:45:32 CET 2020 13 | 14 | * New hardware - AVR128DA32 15 | * card serial number derived from MCU serial number (AVR128DA, Xmega) 16 | * communication speed up to 312kbit 17 | * about 2x faster crypto function 18 | * class A,B,C support (1.8 to 5.5V) 19 | * ECC - fast reduction improved (moved from C to ASM) 20 | 21 | OsEID 20190830 (released) 22 | Author: Peter Popovec 23 | Date: Mon 30 Sep 2019 08:47:21 CEST 2019 24 | 25 | * RSA improved (speed up about 10%) 26 | * APDU chaining (for security operation decrypt/sign, key upload) 27 | * extended APDU support (partial, PSO and PUT DATA can handle Lc > 255) 28 | * Ne/Le handling in T1 protocol 29 | * support for 256 byte transfer in read binary (Case 2S from ISO7816-3) 30 | * RSA generate key fixed (P,Q too close) 31 | * AVR 256 bit multiplication, fixed error in carry propagation 32 | (swapped carry in bytes 57,56 of result) 33 | * DES 56 bits key support (56,64,128,192) 34 | * MyEID compatibility: security operation does not depend 35 | on current selected file. 36 | * unwrap generic secret or AES key by RSA key (experimental) 37 | 38 | OsEID 20190102 (relased) 39 | Author: Peter Popovec 40 | Date: Wed Jan 2 07:30:32 CET 2019 41 | 42 | OsEID token fix: response over 248 bytes was truncated 43 | ECC: secp521r1 curve added (ECDH, ECDSA) 44 | filesystem: subtree delete operation 45 | allow use of shareable file flag in file descriptor byte 46 | opensc 0.18, 0.19 profile file for OsEID card 47 | OsEID-tool: support for generating self signed certificate 48 | Security improvements: 49 | - RSA: Bellcore attack prevention 50 | - RSA: conditional addition in Garner's recombination 51 | replaced by bn_sub_mod 52 | 53 | OsEID 20180123 (released as 'branch errata_20171231' on github) 54 | Author: Peter Popovec 55 | Date: Tue Jan 23 10:22:15 2018 +0100 56 | Fixes (to version 20171231): 57 | - fixed ECC blinding - blinding was not used 58 | - fixed wrong calculation for secp384r1 (due wrong blinding code) 59 | 60 | OsEID 20171231 (released) 61 | Author: Peter Popovec 62 | Date: Sun Dec 31 10:42:01 CET 2017 63 | 64 | AES (128/192/256) support 65 | DES and 3DES support 66 | Gemalto USB reader for USB token (tested in W10) 67 | T1 protocol support (only in USB token) 68 | Security improvements: 69 | - RSA exponent blinding 70 | - ECC point multiplication blinding 71 | - random number generator improved 72 | RSA key generation 73 | More MyEID compatibility fixes: 74 | - Admin state 75 | - Global unblocker 76 | 77 | OsEID 20170630 (released) 78 | Author: Peter Popovec 79 | Date: Fri Jun 30 08:00:00 CEST 2017 80 | 81 | OsEID-tool 82 | ECDH support 83 | 84 | OsEID 20161231 (released) 85 | Author: Peter Popovec 86 | Date: Sat Dec 31 08:00:00 CET 2016 87 | 88 | Initial public release. 89 | RSA sign/decrypt 512-2048 bits 90 | ECC only ECDSA 91 | - prime192v1 92 | - prime256v1 93 | - secp384r1 94 | - secp256k1 95 | -------------------------------------------------------------------------------- /doc/build_pdf_doc.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | git clone https://github.com/popovec/popovec.github.io 4 | mv popovec.github.io/OsEID/images/ . 5 | rm -rf popovec.github.io 6 | a2x -vv -a revnumber="pdfdraft" -a revdate="`date`" -f pdf doc.txt 7 | -------------------------------------------------------------------------------- /doc/kicad/OsEID_AVR128DA32/OsEID-cache.lib: -------------------------------------------------------------------------------- 1 | EESchema-LIBRARY Version 2.4 2 | #encoding utf-8 3 | # 4 | # OsEID_AVR_v1.0_AVR128DA32-EPT 5 | # 6 | DEF OsEID_AVR_v1.0_AVR128DA32-EPT U 0 20 Y Y 1 F N 7 | F0 "U" -500 1750 50 H V L BNN 8 | F1 "OsEID_AVR_v1.0_AVR128DA32-EPT" 100 -1750 50 H V L TNN 9 | F2 "Package_QFP:TQFP-32_7x7mm_P0.8mm" 50 2200 50 H I C CIN 10 | F3 "" 0 0 50 H I C CNN 11 | $FPLIST 12 | TQFP*7x7mm*P0.8mm* 13 | $ENDFPLIST 14 | DRAW 15 | S -500 -1700 500 1700 0 1 10 f 16 | X PA3 1 600 1200 100 L 50 50 1 1 T 17 | X PD0 10 600 100 100 L 50 50 1 1 T 18 | X PC1 11 600 0 100 L 50 50 1 1 T 19 | X PD2 12 600 -100 100 L 50 50 1 1 T 20 | X PD3 13 600 -200 100 L 50 50 1 1 T 21 | X PD4 14 600 -300 100 L 50 50 1 1 T 22 | X PD5 15 600 -400 100 L 50 50 1 1 T 23 | X PD6 16 600 -500 100 L 50 50 1 1 T 24 | X PD7 17 600 -600 100 L 50 50 1 1 T 25 | X AVDD 18 100 1800 100 D 50 50 1 1 W 26 | X GND 19 -250 -1800 100 U 50 50 1 1 P 27 | X PA4 2 600 1100 100 L 50 50 1 1 T 28 | X PF0 20 600 -800 100 L 50 50 1 1 T 29 | X PF1 21 600 -900 100 L 50 50 1 1 T 30 | X PF2 22 600 -1000 100 L 50 50 1 1 T 31 | X PF3 23 600 -1100 100 L 50 50 1 1 T 32 | X PF4 24 600 -1200 100 L 50 50 1 1 T 33 | X PF5 25 600 -1300 100 L 50 50 1 1 T 34 | X ~RESET~/PF6 26 600 -1400 100 L 50 50 1 1 T 35 | X UPDI 27 -300 1800 100 D 50 50 1 1 P 36 | X VDD 28 -50 1800 100 D 50 50 1 1 W 37 | X GND 29 -50 -1800 100 U 50 50 1 1 P 38 | X PA5 3 600 1000 100 L 50 50 1 1 T 39 | X PA0 30 600 1500 100 L 50 50 1 1 T 40 | X PA1 31 600 1400 100 L 50 50 1 1 T 41 | X PA2 32 600 1300 100 L 50 50 1 1 T 42 | X PA6 4 600 900 100 L 50 50 1 1 T 43 | X PA7 5 600 800 100 L 50 50 1 1 T 44 | X PC0 6 600 600 100 L 50 50 1 1 T 45 | X PC1 7 600 500 100 L 50 50 1 1 T 46 | X PC2 8 600 400 100 L 50 50 1 1 T 47 | X PC3 9 600 300 100 L 50 50 1 1 T 48 | ENDDRAW 49 | ENDDEF 50 | # 51 | # OsEID_AVR_v1.0_CONN_3X2 52 | # 53 | DEF OsEID_AVR_v1.0_CONN_3X2 P 0 40 Y N 1 F N 54 | F0 "P" 0 250 50 H V C CNN 55 | F1 "OsEID_AVR_v1.0_CONN_3X2" 0 50 40 V V C CNN 56 | F2 "" 0 0 60 H V C CNN 57 | F3 "" 0 0 60 H V C CNN 58 | DRAW 59 | S -100 200 100 -100 0 1 0 N 60 | X 1 1 -400 150 300 R 60 60 1 1 P I 61 | X 2 2 400 150 300 L 60 60 1 1 P I 62 | X 3 3 -400 50 300 R 60 60 1 1 P I 63 | X 4 4 400 50 300 L 60 60 1 1 P I 64 | X 5 5 -400 -50 300 R 60 60 1 1 P I 65 | X 6 6 400 -50 300 L 60 60 1 1 P I 66 | ENDDRAW 67 | ENDDEF 68 | # 69 | # OsEID_AVR_v1.0_Conn_01x03-Connector_Generic 70 | # 71 | DEF OsEID_AVR_v1.0_Conn_01x03-Connector_Generic J 0 40 Y N 1 F N 72 | F0 "J" 0 200 50 H V C CNN 73 | F1 "OsEID_AVR_v1.0_Conn_01x03-Connector_Generic" 0 -200 50 H V C CNN 74 | F2 "" 0 0 50 H I C CNN 75 | F3 "" 0 0 50 H I C CNN 76 | $FPLIST 77 | Connector*:*_1x??_* 78 | $ENDFPLIST 79 | DRAW 80 | S -50 -95 0 -105 1 1 6 N 81 | S -50 5 0 -5 1 1 6 N 82 | S -50 105 0 95 1 1 6 N 83 | S -50 150 50 -150 1 1 10 f 84 | X Pin_1 1 -200 100 150 R 50 50 1 1 P 85 | X Pin_2 2 -200 0 150 R 50 50 1 1 P 86 | X Pin_3 3 -200 -100 150 R 50 50 1 1 P 87 | ENDDRAW 88 | ENDDEF 89 | # 90 | #End Library 91 | -------------------------------------------------------------------------------- /doc/kicad/OsEID_AVR128DA32/OsEID.pro: -------------------------------------------------------------------------------- 1 | update=Thu Oct 1 09:18:39 2020 2 | version=1 3 | last_client=kicad 4 | [cvpcb] 5 | version=1 6 | NetIExt=net 7 | [cvpcb/libraries] 8 | EquName1=devcms 9 | [general] 10 | version=1 11 | [pcbnew] 12 | version=1 13 | LastNetListRead= 14 | UseCmpFile=1 15 | PadDrill=0.600000000000 16 | PadDrillOvalY=0.600000000000 17 | PadSizeH=1.500000000000 18 | PadSizeV=1.500000000000 19 | PcbTextSizeV=1.500000000000 20 | PcbTextSizeH=1.500000000000 21 | PcbTextThickness=0.300000000000 22 | ModuleTextSizeV=1.000000000000 23 | ModuleTextSizeH=1.000000000000 24 | ModuleTextSizeThickness=0.150000000000 25 | SolderMaskClearance=0.000000000000 26 | SolderMaskMinWidth=0.000000000000 27 | DrawSegmentWidth=0.200000000000 28 | BoardOutlineThickness=0.100000000000 29 | ModuleOutlineThickness=0.150000000000 30 | [pcbnew/libraries] 31 | LibDir= 32 | LibName1=sockets 33 | LibName2=connect 34 | LibName3=discret 35 | LibName4=pin_array 36 | LibName5=divers 37 | LibName6=smd_capacitors 38 | LibName7=smd_resistors 39 | LibName8=smd_crystal_and_oscillator 40 | LibName9=smd_soic_packages 41 | LibName10=smd_transistors 42 | LibName11=libcms 43 | LibName12=display 44 | LibName13=led 45 | LibName14=dip_sockets 46 | LibName15=pga_sockets 47 | LibName16=valves 48 | [eeschema] 49 | version=1 50 | LibDir= 51 | [schematic_editor] 52 | version=1 53 | PageLayoutDescrFile= 54 | PlotDirectoryName= 55 | SubpartIdSeparator=0 56 | SubpartFirstId=65 57 | NetFmtName= 58 | SpiceAjustPassiveValues=0 59 | LabSize=50 60 | ERC_TestSimilarLabels=1 61 | -------------------------------------------------------------------------------- /doc/kicad/OsEID_AVR128DA32/OsEID.sch: -------------------------------------------------------------------------------- 1 | EESchema Schematic File Version 4 2 | LIBS:OsEID-cache 3 | EELAYER 26 0 4 | EELAYER END 5 | $Descr A4 11693 8268 6 | encoding utf-8 7 | Sheet 1 1 8 | Title "OsEID (AVR128DA32)" 9 | Date "2020-08-13" 10 | Rev "V1.0" 11 | Comp "" 12 | Comment1 "" 13 | Comment2 "" 14 | Comment3 "" 15 | Comment4 "" 16 | $EndDescr 17 | Text GLabel 7600 3700 0 60 Input ~ 0 18 | IO 19 | Text GLabel 8550 3700 2 60 Input ~ 0 20 | CLK 21 | Text GLabel 8550 3500 2 60 Input ~ 0 22 | Ucc 23 | Text GLabel 8800 3600 2 60 Input ~ 0 24 | Reset 25 | Text GLabel 7650 3500 0 60 Input ~ 0 26 | Gnd 27 | Text GLabel 7400 3600 0 60 Input ~ 0 28 | Vpp 29 | Wire Wire Line 30 | 7400 3600 7700 3600 31 | Wire Wire Line 32 | 8550 3500 8500 3500 33 | Wire Wire Line 34 | 8500 3600 8800 3600 35 | Wire Wire Line 36 | 8550 3700 8500 3700 37 | Text GLabel 4150 1900 0 60 Input ~ 0 38 | Vpp 39 | Text GLabel 4650 5850 0 60 Input ~ 0 40 | Gnd 41 | Text GLabel 5100 1600 2 60 Input ~ 0 42 | Ucc 43 | Text GLabel 6150 2750 2 60 Input ~ 0 44 | IO 45 | Text GLabel 6150 2650 2 60 Input ~ 0 46 | Reset 47 | Text GLabel 6150 2550 2 60 Input ~ 0 48 | CLK 49 | Wire Wire Line 50 | 7700 3500 7650 3500 51 | Wire Wire Line 52 | 7700 3700 7600 3700 53 | $Comp 54 | L OsEID_AVR_v1.0:CONN_3X2 P1 55 | U 1 1 5F343A5E 56 | P 8100 3650 57 | F 0 "P1" H 8100 4000 50 0000 C CNN 58 | F 1 "ISO7816-3_pads_6" H 8100 3916 40 0000 C CNN 59 | F 2 "OsEID_footprints:ISO7816-3_pads_6" H 8050 3450 60 0001 C CNN 60 | F 3 "" H 8100 3650 60 0000 C CNN 61 | 1 8100 3650 62 | 1 0 0 -1 63 | $EndComp 64 | Wire Wire Line 65 | 4150 1900 4750 1900 66 | Wire Wire Line 67 | 4750 1900 4750 2050 68 | Wire Wire Line 69 | 5100 1600 5000 1600 70 | Wire Wire Line 71 | 5000 1600 5000 2050 72 | Wire Wire Line 73 | 4800 5650 4800 5850 74 | Wire Wire Line 75 | 4800 5850 4650 5850 76 | NoConn ~ 5650 5250 77 | NoConn ~ 5650 5150 78 | NoConn ~ 5650 5050 79 | NoConn ~ 5650 4950 80 | NoConn ~ 5650 4850 81 | NoConn ~ 5650 4750 82 | NoConn ~ 5650 4650 83 | NoConn ~ 5650 3550 84 | NoConn ~ 5650 2350 85 | NoConn ~ 5650 2450 86 | NoConn ~ 5650 2850 87 | NoConn ~ 5650 3850 88 | NoConn ~ 5650 4450 89 | NoConn ~ 5650 4350 90 | NoConn ~ 5650 4250 91 | NoConn ~ 5650 4150 92 | NoConn ~ 5650 4050 93 | NoConn ~ 5650 3950 94 | NoConn ~ 5650 3750 95 | $Comp 96 | L OsEID_AVR_v1.0:AVR128DA32-EPT U1 97 | U 1 1 5F349ACB 98 | P 5050 3850 99 | F 0 "U1" H 4520 3896 50 0000 R CNN 100 | F 1 "AVR128DA32-EPT" H 4520 3805 50 0000 R CNN 101 | F 2 "OsEID_footprints:TQFP-32_7x7mm_P0.8mm" H 5100 6050 50 0001 C CIN 102 | F 3 "" H 5050 3850 50 0001 C CNN 103 | 1 5050 3850 104 | 1 0 0 -1 105 | $EndComp 106 | NoConn ~ 5650 3350 107 | NoConn ~ 5650 3450 108 | Wire Wire Line 109 | 5650 2750 6150 2750 110 | Wire Wire Line 111 | 6150 2650 5650 2650 112 | Wire Wire Line 113 | 5650 2550 6150 2550 114 | NoConn ~ 5650 2950 115 | NoConn ~ 5650 3050 116 | NoConn ~ 5650 3250 117 | $Comp 118 | L OsEID_AVR_v1.0:Conn_01x03-Connector_Generic J1 119 | U 1 1 5F3ED353 120 | P 5100 6250 121 | F 0 "J1" V 4973 6430 50 0000 L CNN 122 | F 1 "Conn_01x03-Connector_Generic" V 5064 6430 50 0000 L CNN 123 | F 2 "OsEID_footprints:PIN_ARRAY_3X1" H 5100 6250 50 0001 C CNN 124 | F 3 "" H 5100 6250 50 0001 C CNN 125 | 1 5100 6250 126 | 0 1 1 0 127 | $EndComp 128 | Wire Wire Line 129 | 5000 6050 5000 5650 130 | Text GLabel 5400 5850 2 60 Input ~ 0 131 | Ucc 132 | Text GLabel 5400 5950 2 60 Input ~ 0 133 | Vpp 134 | Wire Wire Line 135 | 5100 6050 5100 5850 136 | Wire Wire Line 137 | 5100 5850 5400 5850 138 | Wire Wire Line 139 | 5400 5950 5200 5950 140 | Wire Wire Line 141 | 5200 5950 5200 6050 142 | Text Notes 5275 1900 0 50 ~ 0 143 | AVDD and VDD are internally connected together \nsource: preliminary datasheet AVR128DA\nmicrochip doc, 40002183A.pdf, page 14 144 | $EndSCHEMATC 145 | -------------------------------------------------------------------------------- /doc/kicad/OsEID_AVR128DA32/OsEID_AVR_v1.0.lib: -------------------------------------------------------------------------------- 1 | EESchema-LIBRARY Version 2.4 2 | #encoding utf-8 3 | # 4 | # AVR128DA32-EPT 5 | # 6 | DEF AVR128DA32-EPT U 0 20 Y Y 1 F N 7 | F0 "U" -500 1750 50 H V L BNN 8 | F1 "AVR128DA32-EPT" 100 -1750 50 H V L TNN 9 | F2 "Package_QFP:TQFP-32_7x7mm_P0.8mm" 50 2200 50 H I C CIN 10 | F3 "" 0 0 50 H I C CNN 11 | $FPLIST 12 | TQFP*7x7mm*P0.8mm* 13 | $ENDFPLIST 14 | DRAW 15 | S -500 -1700 500 1700 0 1 10 f 16 | X PA3 1 600 1200 100 L 50 50 1 1 T 17 | X PD0 10 600 100 100 L 50 50 1 1 T 18 | X PC1 11 600 0 100 L 50 50 1 1 T 19 | X PD2 12 600 -100 100 L 50 50 1 1 T 20 | X PD3 13 600 -200 100 L 50 50 1 1 T 21 | X PD4 14 600 -300 100 L 50 50 1 1 T 22 | X PD5 15 600 -400 100 L 50 50 1 1 T 23 | X PD6 16 600 -500 100 L 50 50 1 1 T 24 | X PD7 17 600 -600 100 L 50 50 1 1 T 25 | X AVDD 18 100 1800 100 D 50 50 1 1 W 26 | X GND 19 -250 -1800 100 U 50 50 1 1 P 27 | X PA4 2 600 1100 100 L 50 50 1 1 T 28 | X PF0 20 600 -800 100 L 50 50 1 1 T 29 | X PF1 21 600 -900 100 L 50 50 1 1 T 30 | X PF2 22 600 -1000 100 L 50 50 1 1 T 31 | X PF3 23 600 -1100 100 L 50 50 1 1 T 32 | X PF4 24 600 -1200 100 L 50 50 1 1 T 33 | X PF5 25 600 -1300 100 L 50 50 1 1 T 34 | X ~RESET~/PF6 26 600 -1400 100 L 50 50 1 1 T 35 | X UPDI 27 -300 1800 100 D 50 50 1 1 P 36 | X VDD 28 -50 1800 100 D 50 50 1 1 W 37 | X GND 29 -50 -1800 100 U 50 50 1 1 P 38 | X PA5 3 600 1000 100 L 50 50 1 1 T 39 | X PA0 30 600 1500 100 L 50 50 1 1 T 40 | X PA1 31 600 1400 100 L 50 50 1 1 T 41 | X PA2 32 600 1300 100 L 50 50 1 1 T 42 | X PA6 4 600 900 100 L 50 50 1 1 T 43 | X PA7 5 600 800 100 L 50 50 1 1 T 44 | X PC0 6 600 600 100 L 50 50 1 1 T 45 | X PC1 7 600 500 100 L 50 50 1 1 T 46 | X PC2 8 600 400 100 L 50 50 1 1 T 47 | X PC3 9 600 300 100 L 50 50 1 1 T 48 | ENDDRAW 49 | ENDDEF 50 | # 51 | # CONN_3X2 52 | # 53 | DEF CONN_3X2 P 0 40 Y N 1 F N 54 | F0 "P" 0 250 50 H V C CNN 55 | F1 "CONN_3X2" 0 50 40 V V C CNN 56 | F2 "" 0 0 60 H V C CNN 57 | F3 "" 0 0 60 H V C CNN 58 | DRAW 59 | S -100 200 100 -100 0 1 0 N 60 | X 1 1 -400 150 300 R 60 60 1 1 P I 61 | X 2 2 400 150 300 L 60 60 1 1 P I 62 | X 3 3 -400 50 300 R 60 60 1 1 P I 63 | X 4 4 400 50 300 L 60 60 1 1 P I 64 | X 5 5 -400 -50 300 R 60 60 1 1 P I 65 | X 6 6 400 -50 300 L 60 60 1 1 P I 66 | ENDDRAW 67 | ENDDEF 68 | # 69 | # Conn_01x03-Connector_Generic 70 | # 71 | DEF Conn_01x03-Connector_Generic J 0 40 Y N 1 F N 72 | F0 "J" 0 200 50 H V C CNN 73 | F1 "Conn_01x03-Connector_Generic" 0 -200 50 H V C CNN 74 | F2 "" 0 0 50 H I C CNN 75 | F3 "" 0 0 50 H I C CNN 76 | $FPLIST 77 | Connector*:*_1x??_* 78 | $ENDFPLIST 79 | DRAW 80 | S -50 -95 0 -105 1 1 6 N 81 | S -50 5 0 -5 1 1 6 N 82 | S -50 105 0 95 1 1 6 N 83 | S -50 150 50 -150 1 1 10 f 84 | X Pin_1 1 -200 100 150 R 50 50 1 1 P 85 | X Pin_2 2 -200 0 150 R 50 50 1 1 P 86 | X Pin_3 3 -200 -100 150 R 50 50 1 1 P 87 | ENDDRAW 88 | ENDDEF 89 | # 90 | #End Library 91 | -------------------------------------------------------------------------------- /doc/kicad/OsEID_AVR128DA32/OsEID_footprints.pretty/ISO7816-3_pads_6.kicad_mod: -------------------------------------------------------------------------------- 1 | (module ISO7816-3_pads_6 (layer F.Cu) (tedit 5F378481) 2 | (descr "ISO7816 pads") 3 | (tags CONN) 4 | (fp_text reference P1 (at 0 3.81) (layer B.SilkS) hide 5 | (effects (font (size 1.016 1.016) (thickness 0.2032)) (justify mirror)) 6 | ) 7 | (fp_text value ISO7816-3_pads_6 (at -0.4788 9.0404) (layer B.SilkS) hide 8 | (effects (font (size 1.016 1.016) (thickness 0.2032)) (justify mirror)) 9 | ) 10 | (fp_line (start -4 -5.81) (end -4 5.81) (layer F.SilkS) (width 0.2032)) 11 | (fp_line (start 4 5.81) (end 4 -5.81) (layer F.SilkS) (width 0.2032)) 12 | (fp_line (start 4 5.81) (end -4 5.81) (layer F.SilkS) (width 0.2032)) 13 | (fp_line (start -4 -5.81) (end 4 -5.81) (layer F.SilkS) (width 0.2032)) 14 | (pad 6 smd rect (at 2.54 3.8) (size 2.1 3.5) (layers F.Cu F.Mask)) 15 | (pad 5 smd rect (at 2.54 -3.8) (size 2.1 3.5) (layers F.Cu F.Mask)) 16 | (pad 4 smd rect (at 0 3.8) (size 2.1 3.5) (layers F.Cu F.Mask)) 17 | (pad 3 smd rect (at 0 -3.8) (size 2.1 3.5) (layers F.Cu F.Mask)) 18 | (pad 2 smd rect (at -2.54 3.8) (size 2.1 3.5) (layers F.Cu F.Mask)) 19 | (pad 1 smd rect (at -2.54 -3.8) (size 2.1 3.5) (layers F.Cu F.Mask)) 20 | ) 21 | -------------------------------------------------------------------------------- /doc/kicad/OsEID_AVR128DA32/OsEID_footprints.pretty/PIN_ARRAY_3X1.kicad_mod: -------------------------------------------------------------------------------- 1 | (module PIN_ARRAY_3X1 (layer F.Cu) (tedit 5F3CE6B2) 2 | (descr "Connector 3 pins") 3 | (tags "CONN DEV") 4 | (attr virtual) 5 | (fp_text reference J1 (at 4.4196 1.6256) (layer F.SilkS) hide 6 | (effects (font (size 1.016 1.016) (thickness 0.1524))) 7 | ) 8 | (fp_text value Conn_01x03-Connector_Generic (at 1.7272 3.4036) (layer F.SilkS) hide 9 | (effects (font (size 1.016 1.016) (thickness 0.1524))) 10 | ) 11 | (pad 1 connect rect (at -2.54 0) (size 1.524 1.524) (layers B.Cu B.Mask)) 12 | (pad 2 connect circle (at 0 0) (size 1.524 1.524) (layers B.Cu B.Mask)) 13 | (pad 3 connect circle (at 2.54 0) (size 1.524 1.524) (layers B.Cu B.Mask)) 14 | (model pin_array/pins_array_3x1.wrl 15 | (at (xyz 0 0 0)) 16 | (scale (xyz 1 1 1)) 17 | (rotate (xyz 0 0 0)) 18 | ) 19 | ) 20 | -------------------------------------------------------------------------------- /doc/kicad/OsEID_AVR128DA32/OsEID_footprints.pretty/TQFP-32_7x7mm_P0.8mm.kicad_mod: -------------------------------------------------------------------------------- 1 | (module TQFP-32_7x7mm_P0.8mm (layer F.Cu) (tedit 5F45EF90) 2 | (descr "32-Lead Plastic Thin Quad Flatpack (PT) - 7x7x1.0 mm Body, 2.00 mm [TQFP] (see Microchip Packaging Specification 00000049BS.pdf)") 3 | (tags "QFP 0.8") 4 | (attr smd) 5 | (fp_text reference U1 (at -4.118 -6.0252) (layer F.SilkS) hide 6 | (effects (font (size 1 1) (thickness 0.15))) 7 | ) 8 | (fp_text value AVR128DA32-EPT (at 0 6.05) (layer F.Fab) 9 | (effects (font (size 1 1) (thickness 0.15))) 10 | ) 11 | (fp_line (start -4.6 -3.4) (end -4.05 -3.4) (layer F.SilkS) (width 0.15)) 12 | (fp_line (start -5.3 5.3) (end 5.3 5.3) (layer F.CrtYd) (width 0.05)) 13 | (fp_line (start -5.3 -5.3) (end 5.3 -5.3) (layer F.CrtYd) (width 0.05)) 14 | (fp_line (start 5.3 -5.3) (end 5.3 5.3) (layer F.CrtYd) (width 0.05)) 15 | (fp_line (start -5.3 -5.3) (end -5.3 5.3) (layer F.CrtYd) (width 0.05)) 16 | (fp_line (start -3.5 -2.5) (end -2.5 -3.5) (layer F.Fab) (width 0.15)) 17 | (fp_line (start -3.5 3.5) (end -3.5 -2.5) (layer F.Fab) (width 0.15)) 18 | (fp_line (start 3.5 3.5) (end -3.5 3.5) (layer F.Fab) (width 0.15)) 19 | (fp_line (start 3.5 -3.5) (end 3.5 3.5) (layer F.Fab) (width 0.15)) 20 | (fp_line (start -2.5 -3.5) (end 3.5 -3.5) (layer F.Fab) (width 0.15)) 21 | (fp_text user %R (at 0 0) (layer F.Fab) 22 | (effects (font (size 1 1) (thickness 0.15))) 23 | ) 24 | (pad 32 smd rect (at -2.8 -4.45 90) (size 1.2 0.55) (layers B.Cu B.Paste B.Mask)) 25 | (pad 31 smd rect (at -2 -4.45 90) (size 1.2 0.55) (layers B.Cu B.Paste B.Mask)) 26 | (pad 30 smd rect (at -1.2 -4.45 90) (size 1.2 0.55) (layers B.Cu B.Paste B.Mask)) 27 | (pad 29 smd rect (at -0.4 -4.45 90) (size 1.2 0.55) (layers B.Cu B.Paste B.Mask)) 28 | (pad 28 smd rect (at 0.4 -4.45 90) (size 1.2 0.55) (layers B.Cu B.Paste B.Mask)) 29 | (pad 27 smd rect (at 1.2 -4.45 90) (size 1.2 0.55) (layers B.Cu B.Paste B.Mask)) 30 | (pad 26 smd rect (at 2 -4.45 90) (size 1.2 0.55) (layers B.Cu B.Paste B.Mask)) 31 | (pad 25 smd rect (at 2.8 -4.45 90) (size 1.2 0.55) (layers B.Cu B.Paste B.Mask)) 32 | (pad 24 smd rect (at 4.45 -2.8) (size 1.2 0.55) (layers B.Cu B.Paste B.Mask)) 33 | (pad 23 smd rect (at 4.45 -2) (size 1.2 0.55) (layers B.Cu B.Paste B.Mask)) 34 | (pad 22 smd rect (at 4.45 -1.2) (size 1.2 0.55) (layers B.Cu B.Paste B.Mask)) 35 | (pad 21 smd rect (at 4.45 -0.4) (size 1.2 0.55) (layers B.Cu B.Paste B.Mask)) 36 | (pad 20 smd rect (at 4.45 0.4) (size 1.2 0.55) (layers B.Cu B.Paste B.Mask)) 37 | (pad 19 smd rect (at 4.45 1.2) (size 1.2 0.55) (layers B.Cu B.Paste B.Mask)) 38 | (pad 18 smd rect (at 4.45 2) (size 1.2 0.55) (layers B.Cu B.Paste B.Mask)) 39 | (pad 17 smd rect (at 4.45 2.8) (size 1.2 0.55) (layers B.Cu B.Paste B.Mask)) 40 | (pad 16 smd rect (at 2.8 4.45 90) (size 1.2 0.55) (layers B.Cu B.Paste B.Mask)) 41 | (pad 15 smd rect (at 2 4.45 90) (size 1.2 0.55) (layers B.Cu B.Paste B.Mask)) 42 | (pad 14 smd rect (at 1.2 4.45 90) (size 1.2 0.55) (layers B.Cu B.Paste B.Mask)) 43 | (pad 13 smd rect (at 0.4 4.45 90) (size 1.2 0.55) (layers B.Cu B.Paste B.Mask)) 44 | (pad 12 smd rect (at -0.4 4.45 90) (size 1.2 0.55) (layers B.Cu B.Paste B.Mask)) 45 | (pad 11 smd rect (at -1.2 4.45 90) (size 1.2 0.55) (layers B.Cu B.Paste B.Mask)) 46 | (pad 10 smd rect (at -2 4.45 90) (size 1.2 0.55) (layers B.Cu B.Paste B.Mask)) 47 | (pad 9 smd rect (at -2.8 4.45 90) (size 1.2 0.55) (layers B.Cu B.Paste B.Mask)) 48 | (pad 8 smd rect (at -4.45 2.8) (size 1.2 0.55) (layers B.Cu B.Paste B.Mask)) 49 | (pad 7 smd rect (at -4.45 2) (size 1.2 0.55) (layers B.Cu B.Paste B.Mask)) 50 | (pad 6 smd rect (at -4.45 1.2) (size 1.2 0.55) (layers B.Cu B.Paste B.Mask)) 51 | (pad 5 smd rect (at -4.45 0.4) (size 1.2 0.55) (layers B.Cu B.Paste B.Mask)) 52 | (pad 4 smd rect (at -4.45 -0.4) (size 1.2 0.55) (layers B.Cu B.Paste B.Mask)) 53 | (pad 3 smd rect (at -4.45 -1.2) (size 1.2 0.55) (layers B.Cu B.Paste B.Mask)) 54 | (pad 2 smd rect (at -4.45 -2) (size 1.2 0.55) (layers B.Cu B.Paste B.Mask)) 55 | (pad 1 smd rect (at -4.45 -2.8) (size 1.2 0.55) (layers B.Cu B.Paste B.Mask)) 56 | (model ${KISYS3DMOD}/Package_QFP.3dshapes/TQFP-32_7x7mm_P0.8mm.wrl 57 | (offset (xyz 0.03 -0.03 -1.2)) 58 | (scale (xyz 1 1 1)) 59 | (rotate (xyz 0 0 0)) 60 | ) 61 | ) 62 | -------------------------------------------------------------------------------- /doc/kicad/OsEID_AVR128DA32/fp-lib-table: -------------------------------------------------------------------------------- 1 | (fp_lib_table 2 | (lib (name OsEID_footprints)(type KiCad)(uri ${KIPRJMOD}/OsEID_footprints.pretty)(options "")(descr "")) 3 | ) 4 | -------------------------------------------------------------------------------- /doc/kicad/OsEID_AVR128DA32/sym-lib-table: -------------------------------------------------------------------------------- 1 | (sym_lib_table 2 | (lib (name OsEID_AVR_v1.0)(type Legacy)(uri ${KIPRJMOD}/OsEID_AVR_v1.0.lib)(options "")(descr "")) 3 | (lib (name OsEID-rescue)(type Legacy)(uri ${KIPRJMOD}/OsEID-rescue.lib)(options "")(descr "")) 4 | ) 5 | -------------------------------------------------------------------------------- /download/OsEID.reg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/popovec/oseid/392f82fb66547bfbc986b3fde14c70ad4d8b412b/download/OsEID.reg -------------------------------------------------------------------------------- /download/OsEID_card_eeprom.hex: -------------------------------------------------------------------------------- 1 | :100000002130FFFFFFFF0000000000000000FFFFA5 2 | :10001000FFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFF1 3 | :10002000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE0 4 | :10003000FFFF22307329C5CC6A19ECEC7AA7B048CF 5 | :10004000B20D1A58DF2D37F4814D63C7FFFFFFFF54 6 | :10005000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFB0 7 | :10006000FFFFFFFF2330FCFFFFFF00000000000048 8 | :100070000000FFFFFFFFFEFFFFFFFFFFFFFFFFFF8F 9 | :10008000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF80 10 | :10009000FFFFFFFFFFFF2430EF2AECD3EDC8852AD6 11 | :1000A0009DD12E8A8D3956C65A8713508F08140356 12 | :1000B000124181FE6E9C1D18192DF8E36B058E9878 13 | :1000C000E4E73EE2A72F31B32530B70A7672385EF7 14 | :1000D000543A6C2955BF5DF20255382A5482E041EA 15 | :1000E000F759989BA78B623B1D6E74AD20F31EC71A 16 | :1000F000B18E37058BBE22CA87AA26305F0EEA90E2 17 | :100100007C1D437A9D817E1DCEB1600AC0B8F0B5DA 18 | :100110001331DAE97C149A28BD1DF4F829DC929297 19 | :10012000BF989E5D6F2C26964ADE17361920FFFF7A 20 | :10013000FFFFFFFFFFFFFFFFFFFF000000000000C9 21 | :1001400000000000000001000000FFFFFFFF1A2078 22 | :10015000512563FCC2CAB9F3849E17A7ADFAE6BC69 23 | :10016000FFFFFFFFFFFFFFFF00000000FFFFFFFF9B 24 | :100170001B20FCFFFFFFFFFFFFFFFFFFFFFF000053 25 | :100180000000000000000000000001000000FFFF70 26 | :10019000FFFF1C204B60D2273E3CCE3BF6B053CC39 27 | :1001A000B0061D65BC86987655BDEBB3E7933AAAB9 28 | :1001B000D835C65A1D2096C298D84539A1F4A03327 29 | :1001C000EB2D817D0377F240A463E5E6BCF847425E 30 | :1001D0002CE1F2D1176B1E20F551BF376840B6CB2A 31 | :1001E000CE5E316B5733CE2B169E0F7C4AEBE78EDB 32 | :1001F0009B7F1AFEE242E34F1118FFFFFFFFFFFF54 33 | :10020000FFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFF 34 | :10021000FFFF12183128D2B4B1C96B1436F8DE9939 35 | :10022000FFFFFFFFFFFFFFFFFFFFFFFF1318FCFFB4 36 | :10023000FFFFFFFFFFFFFEFFFFFFFFFFFFFFFFFFCF 37 | :10024000FFFFFFFFFFFF1418B1B946C1ECDEB8FE97 38 | :1002500049302472ABE9A70FE7809CE519052164BA 39 | :1002600015181210FF82FD0AFFF40088A143EB204D 40 | :10027000BF7CF69030B00EA88D1816181148791E64 41 | :10028000A177F973D5CD246BED11106378DAC8FF2F 42 | :10029000952B190731202FFCFFFFFEFFFFFFFFFF0B 43 | :1002A000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5E 44 | :1002B000FFFFFFFFFFFF3220414136D08C5ED2BFEF 45 | :1002C0003BA048AFE6DCAEBAFEFFFFFFFFFFFFFF3B 46 | :1002D000FFFFFFFFFFFFFFFF3320000000000000D3 47 | :1002E000000000000000000000000000000000000E 48 | :1002F00000000000000000000000342007000000A3 49 | :1003000000000000000000000000000000000000ED 50 | :1003100000000000000000000000000035209817D9 51 | :10032000F8165B81F259D928CE2DDBFC9B02070B16 52 | :1003300087CE9562A055ACBBDCF97E66BE793620CF 53 | :10034000B8D410FB8FD0479C195485A648B417FD2C 54 | :10035000A808110EFCFBA45D65C4A32677DA3A4811 55 | :10036000710F3021300906052B0E03021A05000417 56 | :100370001401144F734549440405020000000000B5 57 | :1003800000000000010000020B01000F080000C087 58 | :100390000100018003807901F31160A31A068F36F2 59 | :1003A00036DF11D486510EC74FEC7DCEC0D13D78DB 60 | :1003B000E2DAE5B6BB35CB745CAC8BF18E63568E5E 61 | :1003C000E5185505EE86AD33B2984DB2C91A071B34 62 | :1003D000E915ADE0F6AE916E9970FD6AB194776B58 63 | :1003E0006550570FB1F982FCEF76F50E08A78FFE26 64 | :1003F00006A23C7C101C024CFA697D6BB57DD806C8 65 | :100400001212DFEF37B872BD206CBAB1620FE84B41 66 | :10041000F270F85FC8020F2A003FFF7F0000000063 67 | :1004200038113FFF0200E01550FF7F010000003847 68 | :10043000111FFF0200ECA000000063504B43532D3E 69 | :030440003135FF54 70 | :00000001FF 71 | -------------------------------------------------------------------------------- /download/oseid_0.15.profile: -------------------------------------------------------------------------------- 1 | # 2 | # PKCS15 r/w profile for OsEID cards 3 | # 4 | 5 | cardinfo { 6 | label = "OsEID"; 7 | manufacturer = "Atmel at/x mega"; 8 | min-pin-length = 4; 9 | max-pin-length = 8; 10 | pin-encoding = ascii-numeric; 11 | pin-pad-char = 0xFF; 12 | } 13 | 14 | # 15 | # The following controls some aspects of the PKCS15 we put onto 16 | # the card. 17 | # 18 | pkcs15 { 19 | # Put certificates into the CDF itself? 20 | direct-certificates = no; 21 | # Put the DF length into the ODF file? 22 | encode-df-length = no; 23 | # Have a lastUpdate field in the EF(TokenInfo)? 24 | do-last-update = no; 25 | } 26 | 27 | option default { 28 | macros { 29 | #protected = READ=NONE, UPDATE=CHV1, DELETE=CHV2; 30 | #unprotected = READ=NONE, UPDATE=CHV1, DELETE=CHV1; 31 | 32 | unusedspace-size = 504; 33 | odf-size = 252; 34 | aodf-size = 252; 35 | cdf-size = 1512; 36 | cdf-trusted-size = 756; 37 | prkdf-size = 1512; 38 | pukdf-size = 1512; 39 | dodf-size = 1512; 40 | } 41 | } 42 | 43 | # Define reasonable limits for PINs and PUK 44 | # Note that we do not set a file path or reference 45 | # here; that is done dynamically. 46 | PIN user-pin { 47 | reference = 1; 48 | min-length = 4; 49 | max-length = 8; 50 | attempts = 3; 51 | flags = initialized, needs-padding; 52 | } 53 | 54 | PIN user-puk { 55 | min-length = 4; 56 | max-length = 8; 57 | attempts = 10; 58 | flags = needs-padding; 59 | } 60 | 61 | PIN so-pin { 62 | reference = 3; 63 | auth-id = FF; 64 | min-length = 4; 65 | max-length = 8; 66 | attempts = 3; 67 | flags = initialized, soPin, needs-padding; 68 | } 69 | 70 | PIN so-puk { 71 | min-length = 4; 72 | max-length = 8; 73 | attempts = 10; 74 | flags = needs-padding; 75 | } 76 | 77 | # Additional filesystem info. 78 | # This is added to the file system info specified in the 79 | # main profile. 80 | filesystem { 81 | DF MF { 82 | path = 3F00; 83 | type = DF; 84 | acl = CREATE=$PIN, DELETE=$SOPIN; 85 | 86 | # This is the DIR file 87 | EF DIR { 88 | file-id = 2F00; 89 | structure = transparent; 90 | size = 128; 91 | acl = READ=NONE, UPDATE=$SOPIN, DELETE=$SOPIN; 92 | } 93 | DF PKCS15-AppDF { 94 | type = DF; 95 | file-id = 5015; 96 | acl = DELETE=$PIN, CREATE=$PIN; 97 | 98 | EF PKCS15-ODF { 99 | file-id = 5031; 100 | structure = transparent; 101 | size = $odf-size; 102 | acl = READ=NONE, UPDATE=$PIN, DELETE=$SOPIN; 103 | } 104 | 105 | EF PKCS15-TokenInfo { 106 | file-id = 5032; 107 | structure = transparent; 108 | acl = READ=NONE, UPDATE=$SOPIN, DELETE=$SOPIN; 109 | } 110 | 111 | EF PKCS15-UnusedSpace { 112 | file-id = 5033; 113 | structure = transparent; 114 | size = $unusedspace-size; 115 | acl = READ=NONE, UPDATE=$SOPIN, DELETE=$SOPIN; 116 | } 117 | 118 | EF PKCS15-AODF { 119 | file-id = 4401; 120 | structure = transparent; 121 | size = $aodf-size; 122 | acl = READ=NONE, UPDATE=$SOPIN, DELETE=$SOPIN; 123 | } 124 | 125 | EF PKCS15-PrKDF { 126 | file-id = 4402; 127 | structure = transparent; 128 | size = $prkdf-size; 129 | acl = *=NEVER, READ=NONE, UPDATE=$PIN, DELETE=$SOPIN; 130 | } 131 | 132 | EF PKCS15-PuKDF { 133 | file-id = 4404; 134 | structure = transparent; 135 | size = $pukdf-size; 136 | acl = *=NEVER, READ=NONE, UPDATE=$PIN, DELETE=$SOPIN; 137 | } 138 | 139 | EF PKCS15-CDF { 140 | file-id = 4403; 141 | structure = transparent; 142 | size = $cdf-size; 143 | acl = *=NEVER, READ=NONE, UPDATE=$PIN, DELETE=$SOPIN; 144 | } 145 | 146 | EF PKCS15-CDF-TRUSTED { 147 | file-id = 4405; 148 | structure = transparent; 149 | size = $cdf-trusted-size; 150 | acl = *=NEVER, READ=NONE, UPDATE=$PIN, DELETE=$SOPIN; 151 | } 152 | 153 | EF PKCS15-DODF { 154 | file-id = 4406; 155 | structure = transparent; 156 | size = $dodf-size; 157 | acl = *=NEVER, READ=NONE, UPDATE=$PIN, DELETE=$SOPIN; 158 | } 159 | 160 | EF template-private-key { 161 | type = internal-ef; 162 | file-id = 4B01; 163 | acl = CRYPTO=$PIN, UPDATE=$PIN, DELETE=$PIN, GENERATE=$PIN; 164 | } 165 | 166 | EF template-public-key { 167 | structure = transparent; 168 | file-id = 5501; 169 | acl = READ=NONE, UPDATE=$PIN, DELETE=$PIN, GENERATE=$PIN; 170 | } 171 | 172 | EF template-certificate { 173 | file-id = 4301; 174 | structure = transparent; 175 | acl = READ=NONE, UPDATE=$PIN, DELETE=$PIN; 176 | } 177 | 178 | template key-domain { 179 | # This is a dummy entry - pkcs15-init insists that 180 | # this is present 181 | EF private-key { 182 | file-id = 4B01; 183 | type = internal-ef; 184 | acl = READ=NONE, UPDATE=$PIN, DELETE=$PIN, GENERATE=$PIN; 185 | } 186 | EF public-key { 187 | file-id = 5501; 188 | structure = transparent; 189 | acl = READ=NONE, UPDATE=$PIN, DELETE=$PIN, GENERATE=$PIN; 190 | } 191 | 192 | # Certificate template 193 | EF certificate { 194 | file-id = 4301; 195 | structure = transparent; 196 | acl = READ=NONE, UPDATE=$PIN, DELETE=$PIN; 197 | } 198 | EF privdata { 199 | file-id = 4501; 200 | structure = transparent; 201 | acl = READ=$PIN, UPDATE=$PIN, DELETE=$PIN; 202 | } 203 | EF data { 204 | file-id = 4601; 205 | structure = transparent; 206 | acl = READ=NONE, UPDATE=$PIN, DELETE=$PIN; 207 | } 208 | } 209 | } 210 | } 211 | } 212 | -------------------------------------------------------------------------------- /download/oseid_0.16.profile: -------------------------------------------------------------------------------- 1 | # 2 | # PKCS15 r/w profile for OsEID cards 3 | # 4 | 5 | cardinfo { 6 | label = "OsEID"; 7 | manufacturer = "Atmel at/x mega"; 8 | min-pin-length = 4; 9 | max-pin-length = 8; 10 | pin-encoding = ascii-numeric; 11 | pin-pad-char = 0xFF; 12 | } 13 | 14 | # 15 | # The following controls some aspects of the PKCS15 we put onto 16 | # the card. 17 | # 18 | pkcs15 { 19 | # Put certificates into the CDF itself? 20 | direct-certificates = no; 21 | # Put the DF length into the ODF file? 22 | encode-df-length = no; 23 | # Have a lastUpdate field in the EF(TokenInfo)? 24 | do-last-update = no; 25 | } 26 | 27 | option default { 28 | macros { 29 | #protected = READ=NONE, UPDATE=CHV1, DELETE=CHV2; 30 | #unprotected = READ=NONE, UPDATE=CHV1, DELETE=CHV1; 31 | 32 | unusedspace-size = 504; 33 | odf-size = 252; 34 | aodf-size = 252; 35 | cdf-size = 1512; 36 | cdf-trusted-size = 756; 37 | prkdf-size = 1512; 38 | pukdf-size = 1512; 39 | dodf-size = 1512; 40 | } 41 | } 42 | 43 | # Define reasonable limits for PINs and PUK 44 | # Note that we do not set a file path or reference 45 | # here; that is done dynamically. 46 | PIN user-pin { 47 | reference = 1; 48 | min-length = 4; 49 | max-length = 8; 50 | attempts = 3; 51 | flags = initialized, needs-padding; 52 | } 53 | 54 | PIN user-puk { 55 | min-length = 4; 56 | max-length = 8; 57 | attempts = 10; 58 | flags = needs-padding; 59 | } 60 | 61 | PIN so-pin { 62 | reference = 3; 63 | auth-id = FF; 64 | min-length = 4; 65 | max-length = 8; 66 | attempts = 3; 67 | flags = initialized, soPin, needs-padding; 68 | } 69 | 70 | PIN so-puk { 71 | min-length = 4; 72 | max-length = 8; 73 | attempts = 10; 74 | flags = needs-padding; 75 | } 76 | 77 | # Additional filesystem info. 78 | # This is added to the file system info specified in the 79 | # main profile. 80 | filesystem { 81 | DF MF { 82 | path = 3F00; 83 | type = DF; 84 | acl = CREATE=$PIN, DELETE=$SOPIN; 85 | 86 | # This is the DIR file 87 | EF DIR { 88 | file-id = 2F00; 89 | structure = transparent; 90 | size = 128; 91 | acl = READ=NONE, UPDATE=$SOPIN, DELETE=$SOPIN; 92 | } 93 | DF PKCS15-AppDF { 94 | type = DF; 95 | file-id = 5015; 96 | acl = DELETE=$PIN, CREATE=$PIN; 97 | 98 | EF PKCS15-ODF { 99 | file-id = 5031; 100 | structure = transparent; 101 | size = $odf-size; 102 | acl = READ=NONE, UPDATE=$PIN, DELETE=$SOPIN; 103 | } 104 | 105 | EF PKCS15-TokenInfo { 106 | file-id = 5032; 107 | structure = transparent; 108 | acl = READ=NONE, UPDATE=$SOPIN, DELETE=$SOPIN; 109 | } 110 | 111 | EF PKCS15-UnusedSpace { 112 | file-id = 5033; 113 | structure = transparent; 114 | size = $unusedspace-size; 115 | acl = READ=NONE, UPDATE=$SOPIN, DELETE=$SOPIN; 116 | } 117 | 118 | EF PKCS15-AODF { 119 | file-id = 4401; 120 | structure = transparent; 121 | size = $aodf-size; 122 | acl = READ=NONE, UPDATE=$SOPIN, DELETE=$SOPIN; 123 | } 124 | 125 | EF PKCS15-PrKDF { 126 | file-id = 4402; 127 | structure = transparent; 128 | size = $prkdf-size; 129 | acl = *=NEVER, READ=NONE, UPDATE=$PIN, DELETE=$SOPIN; 130 | } 131 | 132 | EF PKCS15-PuKDF { 133 | file-id = 4404; 134 | structure = transparent; 135 | size = $pukdf-size; 136 | acl = *=NEVER, READ=NONE, UPDATE=$PIN, DELETE=$SOPIN; 137 | } 138 | 139 | EF PKCS15-CDF { 140 | file-id = 4403; 141 | structure = transparent; 142 | size = $cdf-size; 143 | acl = *=NEVER, READ=NONE, UPDATE=$PIN, DELETE=$SOPIN; 144 | } 145 | 146 | EF PKCS15-CDF-TRUSTED { 147 | file-id = 4405; 148 | structure = transparent; 149 | size = $cdf-trusted-size; 150 | acl = *=NEVER, READ=NONE, UPDATE=$PIN, DELETE=$SOPIN; 151 | } 152 | 153 | EF PKCS15-DODF { 154 | file-id = 4406; 155 | structure = transparent; 156 | size = $dodf-size; 157 | acl = *=NEVER, READ=NONE, UPDATE=$PIN, DELETE=$SOPIN; 158 | } 159 | 160 | EF template-private-key { 161 | type = internal-ef; 162 | file-id = 4B01; 163 | acl = CRYPTO=$PIN, UPDATE=$PIN, DELETE=$PIN, GENERATE=$PIN; 164 | } 165 | 166 | EF template-public-key { 167 | structure = transparent; 168 | file-id = 5501; 169 | acl = READ=NONE, UPDATE=$PIN, DELETE=$PIN, GENERATE=$PIN; 170 | } 171 | 172 | EF template-certificate { 173 | file-id = 4301; 174 | structure = transparent; 175 | acl = READ=NONE, UPDATE=$PIN, DELETE=$PIN; 176 | } 177 | 178 | template key-domain { 179 | # This is a dummy entry - pkcs15-init insists that 180 | # this is present 181 | EF private-key { 182 | file-id = 4B01; 183 | type = internal-ef; 184 | acl = READ=NONE, UPDATE=$PIN, DELETE=$PIN, GENERATE=$PIN; 185 | } 186 | EF public-key { 187 | file-id = 5501; 188 | structure = transparent; 189 | acl = READ=NONE, UPDATE=$PIN, DELETE=$PIN, GENERATE=$PIN; 190 | } 191 | 192 | # Certificate template 193 | EF certificate { 194 | file-id = 4301; 195 | structure = transparent; 196 | acl = READ=NONE, UPDATE=$PIN, DELETE=$PIN; 197 | } 198 | EF privdata { 199 | file-id = 4501; 200 | structure = transparent; 201 | acl = READ=$PIN, UPDATE=$PIN, DELETE=$PIN; 202 | } 203 | EF data { 204 | file-id = 4601; 205 | structure = transparent; 206 | acl = READ=NONE, UPDATE=$PIN, DELETE=$PIN; 207 | } 208 | } 209 | } 210 | } 211 | } 212 | -------------------------------------------------------------------------------- /src/Makefile.console: -------------------------------------------------------------------------------- 1 | TARGET= targets/console/ 2 | BUILD= build/console/ 3 | 4 | #normal debug 5 | #CFLAGS= -Wall -O2 -g -Wfatal-errors 6 | 7 | CFLAGS= -Wall -Wstrict-prototypes -Wfatal-errors 8 | CFLAGS+= -fstack-protector-strong -Wformat -Werror=format-security -Wextra 9 | CFLAGS+= -O2 -g 10 | CFLAGS+= -DRSA_BYTES=128 -DCARD_RESTART -I$(TARGET) 11 | 12 | # this is used to generate statistics for RSA keygen code (or enable this in card_os/debug.h) 13 | #CFLAGS+= -DRSA_GEN_DEBUG 14 | #grep -v close rsa_gen_debug.stat |grep -v 0x| awk '{g+=$2;r+=$4}END{print g/NR" "r/NR} 15 | #grep close rsa_gen_debug.stat |awk '{c+=$2;s+=$4}END{print s" "c}' 16 | # to test prime generator: 17 | #grep 0x rsa_gen_debug.stat|awk '{print "IsPrime("$1")"}' |genius|grep -v true 18 | 19 | CFLAGS += -DDEBUG_DEVEL 20 | 21 | CC= gcc 22 | ##CFLAGS+= -D_FORTIFY_SOURCE=2 23 | #CC= cgcc -Wsparse-all 24 | #CC=afl-clang-fast 25 | #ASAN_OPTIONS=symbolize=0 AFL_USE_ASAN=1 26 | 27 | all: $(BUILD)console $(BUILD)libOsEIDsim.so 28 | 29 | #Tested without NIST_ONLY, but not set as default 30 | #CFLAGS += -DNIST_ONLY 31 | 32 | # exponentation window 33 | CFLAGS += -DE_BITS=4 34 | 35 | # ECC size (in bytes 24,32,48,72) 36 | CFLAGS += -DMP_BYTES=72 37 | 38 | # precalculate inverse P and Q into key file 39 | CFLAGS += -DUSE_P_Q_INV 40 | 41 | # enable exponent blinding 42 | CFLAGS += -DRSA_EXP_BLINDING 43 | 44 | # enable protection for single error in CRT 45 | CFLAGS += -DPREVENT_CRT_SINGLE_ERROR 46 | 47 | # MyEID does not support 56 bit des version, OsEID allow this if needed 48 | #CFLAGS += -DENABLE_DES56 49 | 50 | CFLAGS += -DPROTOCOL_T0 51 | CFLAGS += -DPROTOCOL_T1 52 | CFLAGS += -DTRANSMISSION_PROTOCOL_MODE_NEGOTIABLE 53 | 54 | 55 | .PHONY: builddir all 56 | 57 | builddir: 58 | @rm -rf $(BUILD) 59 | @mkdir -p $(BUILD) 60 | 61 | #------------------------------------------------------------------- 62 | # target platform files 63 | #------------------------------------------------------------------- 64 | $(BUILD)card_io.o: $(TARGET)card_io.c 65 | $(CC) $(CFLAGS) -o $(BUILD)card_io.o -c $(TARGET)card_io.c -I$(TARGET) -Icard_os 66 | 67 | $(BUILD)mem_device.o: $(TARGET)mem_device.c 68 | $(CC) $(CFLAGS) -o $(BUILD)mem_device.o -c $(TARGET)mem_device.c -I$(TARGET) -Icard_os 69 | 70 | $(BUILD)rnd.o: $(TARGET)rnd.c 71 | $(CC) $(CFLAGS) -o $(BUILD)rnd.o -c $(TARGET)rnd.c -Icard_os 72 | 73 | #------------------------------------------------------------------- 74 | # Target specific files 75 | #------------------------------------------------------------------- 76 | 77 | TARGET_S = $(TARGET)pcscd/OsEIDsim/ 78 | 79 | sim_version="0.0.1" 80 | 81 | 82 | $(BUILD)libOsEIDsim.so: 83 | $(CC) -shared -O2 -g -Wall -fPIC -I. `pkg-config libpcsclite --cflags` -o $(BUILD)libOsEIDsim.so $(TARGET_S)ifdhandler.c $(TARGET_S)serial.c $(TARGET_S)hex2bytes.c 84 | chmod -x $(BUILD)libOsEIDsim.so 85 | (cd $(BUILD); ln -s libOsEIDsim.so libOsEIDsim.so.$(sim_version)) 86 | cp $(TARGET_S)run_pcscd.sh $(BUILD) 87 | 88 | 89 | .PHONY: all clean 90 | 91 | #------------------------------------------------------------------- 92 | # card_os files 93 | #------------------------------------------------------------------- 94 | 95 | include card_os/Makefile 96 | 97 | 98 | $(BUILD)console: builddir $(COMMON_TARGETS) $(BUILD)card_io.o $(BUILD)mem_device.o $(BUILD)rnd.o 99 | $(CC) $(CFLAGS) -o $(BUILD)console $(COMMON_TARGETS) $(BUILD)card_io.o $(BUILD)mem_device.o $(BUILD)rnd.o 100 | 101 | clean: 102 | rm -f *~ 103 | rm -f card_os/*~ 104 | rm -f $(TARGET)/*~ 105 | rm -f $(BUILD)/* 106 | 107 | -------------------------------------------------------------------------------- /src/card_os/Makefile: -------------------------------------------------------------------------------- 1 | #------------------------------------------------------------------- 2 | # card_os files 3 | #------------------------------------------------------------------- 4 | COMMON_TARGETS= $(BUILD)iso7816.o $(BUILD)myeid_emu.o $(BUILD)fs.o $(BUILD)ec.o $(BUILD)rsa.o $(BUILD)card.o $(BUILD)constants.o $(BUILD)aes.o $(BUILD)des.o $(BUILD)bn_lib.o 5 | 6 | $(BUILD)iso7816.o: card_os/iso7816.c 7 | $(CC) $(CFLAGS) $(HAVE) -o $(BUILD)iso7816.o -c card_os/iso7816.c -Icard_os 8 | 9 | $(BUILD)myeid_emu.o: card_os/myeid_emu.c 10 | $(CC) $(CFLAGS) $(HAVE) -o $(BUILD)myeid_emu.o -c card_os/myeid_emu.c -Icard_os 11 | 12 | $(BUILD)fs.o: card_os/fs.c 13 | $(CC) $(CFLAGS) $(HAVE) -o $(BUILD)fs.o -c card_os/fs.c -Icard_os 14 | 15 | $(BUILD)ec.o: card_os/ec.c 16 | $(CC) $(CFLAGS) $(HAVE) -o $(BUILD)ec.o -c card_os/ec.c -Icard_os 17 | 18 | $(BUILD)rsa.o: card_os/rsa.c card_os/rsa.h 19 | $(CC) $(CFLAGS) $(HAVE) -o $(BUILD)rsa.o -c card_os/rsa.c -Icard_os 20 | 21 | $(BUILD)aes.o: card_os/aes.c card_os/aes.h 22 | $(CC) $(CFLAGS) $(HAVE) -o $(BUILD)aes.o -c card_os/aes.c -Icard_os 23 | 24 | $(BUILD)des.o: card_os/des.c card_os/des.h 25 | $(CC) $(CFLAGS) $(HAVE) -o $(BUILD)des.o -c card_os/des.c -Icard_os 26 | 27 | $(BUILD)card.o: card_os/card.c 28 | $(CC) $(CFLAGS) -o $(BUILD)card.o -c card_os/card.c -Icard_os 29 | 30 | $(BUILD)constants.o: card_os/constants.h card_os/constants.c 31 | $(CC) $(CFLAGS) $(HAVE) -o $(BUILD)constants.o -c card_os/constants.c -Icard_os 32 | 33 | $(BUILD)bn_lib.o: card_os/bn_lib.h lib/generic/bn_lib.c 34 | $(CC) $(CFLAGS) $(HAVE) -o $(BUILD)bn_lib.o -c lib/generic/bn_lib.c -Icard_os 35 | 36 | -------------------------------------------------------------------------------- /src/card_os/aes.h: -------------------------------------------------------------------------------- 1 | /* 2 | aes.h 3 | 4 | This is part of OsEID (Open source Electronic ID) 5 | 6 | Copyright (C) 2017 Peter Popovec, popovec.peter@gmail.com 7 | 8 | This program is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | This program is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . 20 | 21 | AES(128,192,256) enc/dec routines (designed for 8 bit CPU) 22 | 23 | This version of AES is designed for minimal flash space. The design of the 24 | algorithm does not make the speed as important for this code. Key is 25 | expanded in RAM, SBOX and INV SBOX is calculated in RAM. 26 | */ 27 | 28 | void aes_run (uint8_t * data, uint8_t * key, uint8_t keysize, uint8_t mode); 29 | -------------------------------------------------------------------------------- /src/card_os/bn_lib.h: -------------------------------------------------------------------------------- 1 | /* 2 | bn_lib.h 3 | 4 | This is part of OsEID (Open source Electronic ID) 5 | 6 | Copyright (C) 2015-2023 Peter Popovec, popovec.peter@gmail.com 7 | 8 | This program is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | This program is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . 20 | 21 | big number arithmetic 22 | 23 | All functions are "generic", able to run on 8 bit CPU. It is assumed, 24 | all these functions are in ASM for target architecture, therefore all 25 | functions are weak. Use this code only for debug/test purposes, or if 26 | target architecture does not have optimized functions. 27 | 28 | lot of routines use "mod_len" variable) to get actual length of big number 29 | please use only 8 bytes steps in bn length (ASM optimized routines uses 30 | step in length 64 bits) 31 | 32 | please write this code for you platform in ASM/C to prevent side channel 33 | attack (timing.. ), this code must run in constant time 34 | 35 | 36 | */ 37 | #include "rsa.h" 38 | #ifndef __BN_LIB__ 39 | #define __BN_LIB__ 40 | 41 | // set arithmetic length (number of bits) 42 | uint8_t bn_set_bitlen(uint16_t blen); 43 | 44 | void bn_swap(void *a, void *b); 45 | uint8_t __attribute__((weak)) bn_is_zero(void *k); 46 | uint8_t __attribute__((weak)) bn_is_one(void *k); 47 | uint8_t __attribute__((weak)) bn_neg(void *a); 48 | 49 | uint8_t __attribute__((weak)) bn_add_v(void *r, void *a, uint8_t len, uint8_t carry); 50 | uint8_t __attribute__((weak)) bn_add(void *r, void *a); 51 | 52 | uint8_t __attribute__((weak)) bn_sub_v(void *r, void *a, void *b, uint8_t len); 53 | uint8_t __attribute__((weak)) bn_sub(void *r, void *a, void *b); 54 | uint8_t __attribute__((weak)) bn_sub_long(void *r, void *a, void *b); 55 | 56 | uint8_t __attribute__((weak)) bn_cmpGE(void *c, void *d); 57 | uint8_t __attribute__((weak)) bn_abs_sub(void *r, void *a, void *b); 58 | 59 | void __attribute__((weak)) bn_add_mod(void *r, void *a, void *mod); 60 | void __attribute__((weak)) bn_sub_mod(void *r, void *a, void *mod); 61 | 62 | uint8_t __attribute__((weak)) bn_shift_L_v(void *r, uint8_t len); 63 | uint8_t __attribute__((weak)) bn_shiftl(void *r); 64 | 65 | uint8_t __attribute__((weak)) bn_shift_R_v_c(void *r, uint8_t len, uint8_t carry); 66 | uint8_t __attribute__((weak)) bn_shiftr(void *r); 67 | uint8_t __attribute__((weak)) bn_shiftr_long(void *r); 68 | uint8_t __attribute__((weak)) bn_shiftr_c(void *r, uint8_t carry); 69 | uint8_t __attribute__((weak)) bn_shift_R_signed(void *r); 70 | 71 | void __attribute__((weak)) bn_mul_v(void *r, void *a, void *b, uint8_t len); 72 | 73 | void __attribute__((weak)) bn_mod(void *result, void *mod); 74 | void __attribute__((weak)) bn_mod_half(void *result, void *mod); 75 | 76 | uint8_t __attribute__((weak)) bn_inv_mod(void *r, void *c, void *p); 77 | 78 | #ifndef __BN_LIB_SELF__ 79 | extern uint8_t mod_len; 80 | extern uint16_t bn_real_bit_len; 81 | extern uint8_t bn_real_byte_len; 82 | #endif 83 | 84 | uint16_t __attribute__((weak)) bn_count_bits(void *n); 85 | uint8_t __attribute__((weak)) bn_shift_R_v_signed(void *r, uint8_t len); 86 | 87 | #endif 88 | -------------------------------------------------------------------------------- /src/card_os/card.c: -------------------------------------------------------------------------------- 1 | /* 2 | card.c 3 | 4 | This is part of OsEID (Open source Electronic ID) 5 | 6 | Copyright (C) 2015-2021 Peter Popovec, popovec.peter@gmail.com 7 | 8 | This program is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | This program is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . 20 | 21 | main 22 | 23 | */ 24 | 25 | #include 26 | #include 27 | #include "rnd.h" 28 | #include "card_io.h" 29 | #include "iso7816.h" 30 | #include "myeid_emu.h" 31 | #include "fs.h" 32 | 33 | #ifdef CARD_RESTART 34 | #include "restart.h" 35 | #endif 36 | 37 | int main(void) 38 | { 39 | #ifdef CARD_RESTART 40 | #include "restart.c" 41 | #endif 42 | 43 | //initialize filesystem 44 | fs_init(); 45 | //initialize random number generator 46 | rnd_init(); 47 | //initialize myeid emulation (not valid security env) 48 | security_env_set_reset(NULL, NULL); 49 | // initialize iso part of card 50 | response_clear(); 51 | // initialize card IO and send ATR 52 | card_io_init(); 53 | 54 | for (;;) 55 | card_poll(); 56 | return 0; 57 | } 58 | -------------------------------------------------------------------------------- /src/card_os/card_io.h: -------------------------------------------------------------------------------- 1 | /* 2 | card_io.h 3 | 4 | This is part of OsEID (Open source Electronic ID) 5 | 6 | Copyright (C) 2015-2023 Peter Popovec, popovec.peter@gmail.com 7 | 8 | This program is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | This program is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . 20 | 21 | 22 | input/output subsystem for OsEID 23 | 24 | 25 | Functions: 26 | 27 | void card_io_init(void) 28 | - I/O subsystem is initialized and ATR is sent to reader 29 | 30 | void card_io_tx (uint8_t * data, uint16_t len); 31 | - transmit data from buffer pointed by "data", length is 32 | defined in "len" variable. Minimal transmit size is 1 byte, 33 | if len==0 then 65536 bytes are transmitted. 34 | 35 | - for protocol T0, the error signal and character repetition is is used 36 | as specified in ISO7816-3:2006(E). The repetition of characters 37 | should be limited, if the error signal occurs more than (20?) times, 38 | TX is interrupted. 39 | 40 | uint16_t card_io_rx (uint8_t * data, uint16_t len); 41 | 42 | - pointer "data" must not be NULL and must point to at least 10 43 | free byte in RAM 44 | 45 | - read data from reader, store to buffer pointed by "data" 46 | maximal number of received bytes in "len". If reader transmit 47 | more data as expected in "len", rest of data are discarded. 48 | (transmit end is detected by timeout after latest received char) 49 | Discard is not signalized. 50 | 51 | - for protocol T0, the error signal and character repetition is is used 52 | as specified in ISO7816-3:2006(E). The repetition of characters 53 | should be limited, if the error signal occurs more than (20?) times, 54 | RX continues, but the error signal is no longer generated. Caller is 55 | informed about parity error. 56 | 57 | - if len == 0, whole frame is dropped, no byte is stored into buffer. 58 | 59 | - return value corresponds to number of received bytes. this function 60 | is waiting for data from card reader at least one byte is received. 61 | (even this byte is wrong i.e. withparity error..) 62 | 63 | - Any special case is signalized by return value 0, in this case, 64 | the 1st value in buffer is used as error/special code: 65 | 66 | Error codes: 67 | CARD_IO_PARITY_ERROR - at least one of character was received with 68 | parity error. 69 | 70 | Special codes: (only for negotiable mode): 71 | CARD_IO_PPS - response to PPS is in buffer (used to set T0/T1 protocol) 72 | CARD_IO_PPS_ERROR - incorrect PPS frame in buffer. 73 | 74 | PPS handling: 75 | ------------- 76 | 77 | IO layer is not responsible to handle 0xff in NAD field or in CLA field. 78 | 79 | If 1st received byte in buffer is 0xff and this is 1st frame after ATR, 80 | frame is checked, if this is valid PPS frame. 81 | 82 | PPS0 is checked for supported protocols, PPS1 is checked for valid F/D. 83 | If this frame is not correct PPS or or "PPS" contains values that 84 | cannot be used, 1st byte in buffer is set to "CARD_IO_PPS_ERROR" and 85 | returned length is 0. 86 | 87 | For valid PPS frame, the IO layer generates a response frame and this 88 | frame is returned to caller (1st byte in buffer = CARD_IO_PPS, 2nd 89 | byte in buffer is length of PPS response frame, returned length = 0). 90 | 91 | Example of data in buffer: 92 | 93 | 0xff 4 0xFF 0x11 0x96 0x78 94 | [CARD_IO_PPS] [LENGTH] [PPSS] [PPS0] [PPS1] optionally[PPS2,3] [PCK] 95 | 96 | Caller can use protocol value in PPS frame to select T0/1 protocol. 97 | Caller is responsible to transmit this frame back. IO layer then 98 | changes parameters of transmitions to negotiated values. 99 | 100 | void card_io_start_null (void); 101 | - setup I/O subsystem to transmit NULL bytes / WTX frames. 102 | 103 | void card_io_stop_null (void); (deprecated, should be handles in card_io_tx) 104 | - setup I/O subsystem to not transmit NULL bytes 105 | 106 | 107 | 108 | RST handling: 109 | 110 | Cart reset is asynchronous event, IO subsystem can handle this in different ways: 111 | 112 | 1. RST pad is connected to microcontroler RESET pin, then no other handling is needed 113 | 2. RST is monitored by interrupt system of microcontroler, microcontroler reset 114 | is generated if RST is detected 115 | 3. RST is monitored in software, if RST is detected to be active, signal handler 116 | is called and card is restarted from main (usually by setting setjmp and handler 117 | then call longjmp. card_os/card.c provide two places for include 118 | restart.h and restart.c to ensure this task. 119 | 120 | */ 121 | #define CARD_IO_PARITY_ERROR 2 122 | 123 | #ifdef TRANSMISSION_PROTOCOL_MODE_NEGOTIABLE 124 | #define CARD_IO_PPS_ERROR 0xfe 125 | #define CARD_IO_PPS 0xff 126 | #endif 127 | 128 | #if !defined (TRANSMISSION_PROTOCOL_MODE_NEGOTIABLE) && !defined(TRANSMISSION_PROTOCOL_MODE_SPECIFIC) 129 | #error TRANSMISSION_PROTOCOL_MODE_NEGOTIABLE or TRANSMISSION_PROTOCOL_MODE_SPECIFIC #define TRANSMISSION_PROTOCOL_MODE_SPECIFIC must be defined 130 | #endif 131 | 132 | #if defined (TRANSMISSION_PROTOCOL_MODE_NEGOTIABLE) && defined(TRANSMISSION_PROTOCOL_MODE_SPECIFIC) 133 | #error both TRANSMISSION_PROTOCOL_MODE_NEGOTIABLE and TRANSMISSION_PROTOCOL_MODE_SPECIFIC defined 134 | #endif 135 | 136 | #if defined (PROTOCOL_T0) && defined (PROTOCOL_T1) && defined(TRANSMISSION_PROTOCOL_MODE_SPECIFIC) 137 | #error SPECIFIC mode and both T0,T1 protocol defined.. 138 | #endif 139 | 140 | #if defined (T1_TRANSPORT) && !defined(PROTOCOL_T1) 141 | #error T1_TRANSPORT defined but not PROTOCOL_T1 142 | #endif 143 | 144 | #if !defined(__ASSEMBLER__) 145 | void card_io_init (void); 146 | uint16_t card_io_rx (uint8_t * data, uint16_t len); 147 | void card_io_tx (uint8_t * data, uint16_t len); 148 | uint8_t card_io_reset (void); 149 | void card_io_start_null (void); 150 | #endif 151 | -------------------------------------------------------------------------------- /src/card_os/constants.c: -------------------------------------------------------------------------------- 1 | /* 2 | constants.c 3 | 4 | This is part of OsEID (Open source Electronic ID) 5 | 6 | Copyright (C) 2015,2017 Peter Popovec, popovec.peter@gmail.com 7 | 8 | This program is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | This program is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . 20 | 21 | definition of constant values for eliptic curves, oids etc 22 | default helper 23 | */ 24 | #ifndef HAVE_GET_CONSTANTS 25 | #include 26 | #include 27 | #include "constants.h" 28 | 29 | /* *INDENT-OFF* */ 30 | uint8_t constants[] = { 31 | C_CONSTANTS 32 | //last position 33 | 0xff 34 | }; 35 | /* *INDENT-ON* */ 36 | 37 | uint8_t 38 | get_constant (void *here, uint8_t id) 39 | { 40 | uint8_t *t; 41 | uint8_t *s = constants; 42 | uint8_t size; 43 | 44 | t = (uint8_t *) here; 45 | for (;;) 46 | { 47 | if (*s == 0xff) 48 | { 49 | fprintf (stderr, "Unknown constant %d\n", id); 50 | return 0; 51 | } 52 | if (*s == id) 53 | { 54 | s++; 55 | size = *s; 56 | s++; 57 | while (size) 58 | { 59 | *t = *s; 60 | t++; 61 | s++; 62 | size--; 63 | } 64 | return 1; 65 | } 66 | s++; 67 | size = *s; 68 | s++; 69 | s += size; 70 | } 71 | } 72 | #endif 73 | -------------------------------------------------------------------------------- /src/card_os/debug.h: -------------------------------------------------------------------------------- 1 | /* 2 | debug.h 3 | 4 | This is part of OsEID (Open source Electronic ID) 5 | 6 | Copyright (C) 2019-2024 Peter Popovec, popovec.peter@gmail.com 7 | 8 | This program is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | This program is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . 20 | 21 | function and constants for debugging OsEID - (simulated in PC) 22 | 23 | */ 24 | 25 | 26 | // aditional debug 27 | //#define RSA_GEN_DEBUG 1 28 | 29 | /* 30 | 31 | env variable OsEID_DEBUG is used to controll debug messages 32 | 33 | variable is bit mapped, bits 0,1,2,3 are reserved for simulated reader and pcscd 34 | 35 | 1 - run pcscd with -d switch 36 | 2 - enable debug from OsEID simulated reader (ifHandler) 37 | 4 - RFU 38 | 8 - RFU 39 | 16 - low level input output - transport from reader to card and back, inclusive 40 | messages about NULL byte sending (to query more time for operation) 41 | 42 | below in #define codes for debug card internal functions 43 | 44 | */ 45 | 46 | 47 | #ifdef DEBUG_IFH 48 | #define DEBUG_V 2 49 | #endif 50 | 51 | #ifdef DEBUG_FS 52 | #define DEBUG_V 32 53 | #endif 54 | 55 | #ifdef DEBUG_MyEID_EMU 56 | #define DEBUG_V 256 57 | #endif 58 | 59 | #ifdef DEBUG_ISO7816 60 | #define DEBUG_V 128 61 | #endif 62 | 63 | #ifdef DEBUG_BN_MATH 64 | #define DEBUG_V 8192 65 | #endif 66 | 67 | #ifdef DEBUG_RSA 68 | #define DEBUG_V 16384 69 | #endif 70 | 71 | #ifdef DEBUG_ECC 72 | #define DEBUG_V 32768 73 | #endif 74 | 75 | 76 | #if defined(DEBUG_DEVEL) && defined (DEBUG_V) 77 | #include 78 | #include 79 | #define DPRINT(msg...) {char *env_atr = getenv ("OsEID_DEBUG"); if(env_atr){if(atoi(env_atr)& DEBUG_V){fprintf(stderr,msg);}}} 80 | 81 | #include 82 | // to print hexadecimal data 83 | static void __attribute__((unused)) HPRINT (char *m, void *d, int size) 84 | { 85 | uint8_t *data = (uint8_t *) d; 86 | 87 | char *env_atr = getenv ("OsEID_DEBUG"); 88 | if (env_atr) 89 | { 90 | if (atoi (env_atr) & DEBUG_V) 91 | { 92 | 93 | int j; 94 | fprintf (stderr, "%s", m); 95 | 96 | for (j = 0; j < size; j++) 97 | { 98 | if (j % 32 == 0 && j != 0) 99 | fprintf (stderr, "\n"); 100 | fprintf (stderr, "%02X ", *(data++)); 101 | } 102 | fprintf (stderr, "\n"); 103 | } 104 | } 105 | } 106 | // to print numbers (hexadecimal) 107 | static void __attribute__((unused)) NPRINT (char *m, void *d, int size) 108 | { 109 | uint8_t *data = (uint8_t *) d; 110 | 111 | char *env_atr = getenv ("OsEID_DEBUG"); 112 | if (env_atr) 113 | { 114 | if (atoi (env_atr) & DEBUG_V) 115 | { 116 | fprintf (stderr, "%s 0x", m); 117 | data +=size; 118 | while(size--) 119 | fprintf (stderr, "%02X", *(--data)); 120 | fprintf (stderr, "\n"); 121 | } 122 | } 123 | } 124 | #else 125 | #define DPRINT(msg...) 126 | #define HPRINT(msg, d, s) 127 | #define NPRINT(msg, d, s) 128 | #endif 129 | -------------------------------------------------------------------------------- /src/card_os/des.h: -------------------------------------------------------------------------------- 1 | /* 2 | des.h 3 | 4 | This is part of OsEID (Open source Electronic ID) 5 | 6 | Copyright (C) 2017,2019 Peter Popovec, popovec.peter@gmail.com 7 | 8 | This program is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | This program is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . 20 | 21 | DES cipher header file 22 | 23 | */ 24 | #ifndef _DES_H_ 25 | #define _DES_H_ 26 | 27 | #define DES_ENCRYPTION_MODE 0 28 | #define DES_DECRYPTION_MODE 3 29 | #define DES_3DES 0x0c 30 | 31 | void des_run (uint8_t * data, uint8_t *key, uint8_t mode); 32 | // transform 7 bytes of key to 8 bytes (with parity bits) 33 | void des_56to64 (uint8_t * key); 34 | #endif 35 | -------------------------------------------------------------------------------- /src/card_os/ec.h: -------------------------------------------------------------------------------- 1 | /* 2 | ec.h 3 | 4 | This is part of OsEID (Open source Electronic ID) 5 | 6 | Copyright (C) 2015-2018 Peter Popovec, popovec.peter@gmail.com 7 | 8 | This program is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | This program is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . 20 | 21 | ECC header file 22 | 23 | */ 24 | #ifndef CS_EC_H 25 | #define CS_EC_H 26 | #ifndef MP_BYTES 27 | #define MP_BYTES 48 28 | #endif 29 | #ifndef __ASSEMBLER__ 30 | 31 | #if MP_BYTES < 24 32 | #error minimal MP_BYTES must be set to 24 33 | #endif 34 | #include "constants.h" 35 | typedef struct 36 | { 37 | uint8_t value[MP_BYTES]; 38 | } bignum_t; 39 | 40 | typedef struct 41 | { 42 | bignum_t X; 43 | bignum_t Y; 44 | bignum_t Z; 45 | } ec_point_t; 46 | 47 | typedef struct 48 | { 49 | union { 50 | ec_point_t signature; 51 | struct{ 52 | bignum_t R; 53 | bignum_t S; 54 | }; 55 | }; 56 | bignum_t priv_key; 57 | 58 | } ecdsa_sig_t; 59 | 60 | // curve types, bit 7 and 6 used to encode special A value 61 | // (this is used to select optimized algo for calculation) 62 | // for A=0 set bit 7 63 | // for A=-3 set bit 6 64 | 65 | #define C_P192V1_MASK 0x40 66 | #define C_P256V1_MASK 0x40 67 | #define C_SECP384R1_MASK 0x40 68 | #define C_SECP521R1_MASK 0x40 69 | #define C_SECP256K1_MASK 0x80 70 | 71 | struct ec_param 72 | { 73 | bignum_t prime; 74 | bignum_t order; 75 | bignum_t a; 76 | bignum_t b; 77 | // do not change rest .. key generation depent on it 78 | uint8_t curve_type; // curve type (to select beeter algo and fast reduction algo) 79 | uint8_t mp_size; // arithmetics size (max 48 bytes) 80 | bignum_t working_key; 81 | }; 82 | 83 | 84 | #endif 85 | 86 | // return generated private key in ec->working_key, public key in pub_key 87 | uint8_t ec_key_gener (ec_point_t * pub_key, struct ec_param *ec); 88 | 89 | // sign HASH in message, return R,S in ecdsa_sig_t, use parameters from ec_param 90 | uint8_t ecdsa_sign (uint8_t *message, ecdsa_sig_t * ecsig, struct ec_param *ec); 91 | 92 | uint8_t ec_derive_key (ec_point_t * pub_key, struct ec_param *ec); 93 | #endif 94 | -------------------------------------------------------------------------------- /src/card_os/fs.h: -------------------------------------------------------------------------------- 1 | /* 2 | fs.h 3 | 4 | This is part of OsEID (Open source Electronic ID) 5 | 6 | Copyright (C) 2015-2019 Peter Popovec, popovec.peter@gmail.com 7 | 8 | This program is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | This program is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . 20 | 21 | filesystem function header file 22 | 23 | */ 24 | #ifndef CS_FS_H 25 | #define CS_FS_H 26 | 27 | /* fs.c */ 28 | uint8_t fs_return_pin_info (uint8_t pin, struct iso7816_response * r); 29 | uint8_t fs_change_pin (uint8_t * message); 30 | uint8_t fs_verify_pin (uint8_t * message); 31 | uint8_t fs_reset_retry_count (uint8_t * message); 32 | uint8_t fs_initialize_pin (uint8_t * message); 33 | void fs_deauth (uint8_t pin); 34 | 35 | uint16_t fs_get_access_condition (void); 36 | void fs_init (void); 37 | uint8_t fs_erase_card (uint8_t * acl); 38 | 39 | uint16_t fs_get_selected (void); 40 | 41 | uint16_t fs_get_selected_uuid (void); 42 | 43 | void fs_set_lifecycle(void); 44 | 45 | uint8_t fs_select_uuid (uint16_t uuid, struct iso7816_response * r); 46 | uint8_t fs_select_parent (struct iso7816_response *r); 47 | uint8_t fs_select_by_name (uint8_t * buffer, struct iso7816_response *r); 48 | uint8_t fs_select_0 (uint16_t id, struct iso7816_response *r); 49 | uint8_t fs_select_mf (struct iso7816_response *r); 50 | uint8_t fs_select_df (uint16_t id, struct iso7816_response *r); 51 | uint8_t fs_select_ef (uint16_t id, struct iso7816_response *r); 52 | uint8_t fs_select_by_path_from_mf (uint8_t * buffer, 53 | struct iso7816_response *r); 54 | uint8_t fs_select_by_path_from_df (uint8_t * buffer, 55 | struct iso7816_response *r); 56 | 57 | 58 | uint16_t fs_key_read_part (uint8_t * key, uint8_t type); 59 | 60 | // 1st byte = key type, 2nd key part size, rest key part 61 | uint8_t fs_key_write_part (uint8_t * key); 62 | 63 | uint8_t fs_read_binary (uint16_t offset, struct iso7816_response *r); 64 | uint8_t fs_update_binary (uint8_t * buffer, uint16_t offset); 65 | 66 | uint8_t fs_erase_binary(uint16_t offset); 67 | 68 | uint8_t fs_delete_file (void); 69 | 70 | uint8_t fs_create_file (uint8_t * buffer); 71 | uint8_t fs_list_files (uint8_t type, struct iso7816_response *r); 72 | 73 | uint16_t fs_get_file_size (void); 74 | uint8_t fs_get_file_type (void); 75 | // this function does not check if file is selected!, returned value 0xffff 76 | // is prop flag of file is not selected .. 77 | uint16_t fs_get_file_proflag (void); 78 | #ifndef NIST_ONLY 79 | // temp function to allow change file type for EC key to 0x23 80 | uint8_t fs_key_change_type (void); 81 | #endif 82 | #endif 83 | -------------------------------------------------------------------------------- /src/card_os/iso7816.h: -------------------------------------------------------------------------------- 1 | /* 2 | iso7816.h 3 | 4 | This is part of OsEID (Open source Electronic ID) 5 | 6 | Copyright (C) 2015-2021 Peter Popovec, popovec.peter@gmail.com 7 | 8 | This program is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | This program is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . 20 | 21 | header file for iso7816 functions 22 | 23 | */ 24 | #ifndef CS_ISO7816_H 25 | #define CS_ISO7816_H 26 | 27 | // RSA 2048 need 256 bytes of data + padding indicator -> 257 bytes data part of APDU 28 | // 5 bytes header, max 257 bytes data +2+2 (to support Case 3E, 4E ISO786-3) 29 | #ifndef APDU_CMD_LEN 30 | #define APDU_CMD_LEN 261+5 31 | #endif 32 | // 258 bytes (this buffer is used as case of APDU chaining 33 | // and for RSA operarion with padding indicator (P2 = 0x86) 34 | #ifndef APDU_RESP_LEN 35 | #define APDU_RESP_LEN 258 36 | #endif 37 | void card_poll (void); 38 | 39 | void response_clear (void); 40 | 41 | struct iso7816_response 42 | { 43 | uint8_t protocol; // 0 T0 1 T1 44 | uint16_t Nc; // 0, Lc not present, 1..65535 Lc 45 | uint16_t Ne; // 0, Le not present, 1..65535 Le (iso allow 65536 here, but for limited RAM in hardware this is not used) 46 | uint8_t chaining_ins; 47 | uint8_t chaining_state; 48 | uint16_t len16; 49 | uint16_t tmp_len; // length of chained APDU (except last APDU part) 50 | uint16_t chain_len; // length of chained APDU (whole collected data) 51 | uint8_t data[APDU_RESP_LEN]; 52 | uint8_t input[APDU_CMD_LEN]; 53 | }; 54 | 55 | #define RESP_READY(x) {r->len16 = (x); return S0x6100;} 56 | 57 | // 0x9000 58 | #define S_RET_OK 0 59 | // used by GET RESPONSE to signalize data must be returned 60 | #define S_RET_GET_RESPONSE 1 61 | // response length in low byte 62 | #define S0x6100 0x10 63 | 64 | #define S0x6281 0x21 65 | #define S0x6282 0x22 66 | 67 | #define S0x63c0 0x30 68 | /* 69 | #define S0x63c1 0x31 70 | #define S0x63c2 0x32 71 | #define S0x63c3 0x33 72 | #define S0x63c4 0x34 73 | #define S0x63c5 0x35 74 | #define S0x63c6 0x36 75 | #define S0x63c7 0x37 76 | #define S0x63c8 0x38 77 | #define S0x63c9 0x39 78 | #define S0x63ca 0x3a 79 | #define S0x63cb 0x3b 80 | #define S0x63cc 0x3c 81 | #define S0x63cd 0x3d 82 | #define S0x63ce 0x3e 83 | */ 84 | #define S0x63cf 0x3f 85 | 86 | #define S0x6581 0x51 87 | 88 | // fixed 6700 89 | #define S0x6700 0x70 90 | 91 | #define S0x6883 0x83 92 | #define S0x6884 0x84 93 | 94 | #define S0x6981 0x91 95 | #define S0x6982 0x92 96 | #define S0x6983 0x93 97 | #define S0x6984 0x94 98 | #define S0x6985 0x95 99 | #define S0x6986 0x96 100 | 101 | #define S0x6a80 0xa0 102 | #define S0x6a81 0xa1 103 | #define S0x6a82 0xa2 104 | #define S0x6a86 0xa6 105 | #define S0x6a87 0xa7 106 | #define S0x6a88 0xa8 107 | #define S0x6a89 0xa9 108 | 109 | // fixed 6b00 110 | #define S0x6b00 0xb0 111 | // wrong Le, low byte is used to signalize correct Le 112 | #define S0x6c00 0xc0 113 | // fixed 6d00 114 | #define S0x6d00 0xd0 115 | // fixed 6e00 116 | #define S0x6e00 0xe0 117 | // fixed 6f00 118 | #define S0x6f00 0xf0 119 | 120 | // APDU_CHAIN_RUNNING is mask for APDU_CHAIN_ACTIVE and APDU_CHAIN_START 121 | // there is test chaining_state <= APDU_CHAIN_START in code, do not change values! 122 | #define APDU_CHAIN_INACTIVE 0 123 | #define APDU_CHAIN_START 1 124 | #define APDU_CHAIN_ACTIVE 2 125 | #define APDU_CHAIN_RUNNING 3 126 | #define APDU_CHAIN_LAST 4 127 | #endif 128 | -------------------------------------------------------------------------------- /src/card_os/key.h: -------------------------------------------------------------------------------- 1 | /* 2 | key.h 3 | 4 | This is part of OsEID (Open source Electronic ID) 5 | 6 | Copyright (C) 2015-2021 Peter Popovec, popovec.peter@gmail.com 7 | 8 | This program is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | This program is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . 20 | 21 | key file constants definitions 22 | */ 23 | 24 | // types of KEY for fs_read_key/fs_write_key 25 | // (used low 5 bites, in upper bits reserved for flags) 26 | 27 | #define KEY_OID 1 28 | //EC key parts 29 | #define KEY_EC_PRIVATE 2 30 | #define KEY_EC_PUBLIC 3 31 | 32 | // RSA key parts 33 | // public exponent 34 | #define KEY_RSA_EXP_PUB 0x81 35 | 36 | // CRT components 37 | #define KEY_RSA_p 0x83 38 | #define KEY_RSA_q 0x84 39 | #ifdef USE_P_Q_INV 40 | // inverse of p and q 41 | #define KEY_RSA_p_ 0xb3 42 | #define KEY_RSA_q_ 0xb4 43 | #endif 44 | #define KEY_RSA_dP 0x85 45 | #define KEY_RSA_dQ 0x86 46 | #define KEY_RSA_qInv 0x87 47 | // modulus (for get data fcion.. not for CRT) 48 | #define KEY_RSA_MOD 0x80 49 | // parts for 2048 key 50 | #define KEY_RSA_MOD_p1 0x88 51 | #define KEY_RSA_MOD_p2 0x89 52 | 53 | // not CRT parts private exponent full 54 | #define KEY_RSA_EXP 0x82 55 | // parts for 2048 key 56 | #define KEY_RSA_EXP_p1 0x8a 57 | #define KEY_RSA_EXP_p2 0x8b 58 | 59 | 60 | //AES, DES - not used, 0xa0 comes from APDU directly 61 | #define KEY_AES_DES 0xa0 62 | 63 | // mask for key "generated" 64 | #define KEY_GENERATE 0x40 65 | 66 | #define KEY_FREE_SPACE 0xff 67 | 68 | #define RSA_KEY_EF 0x11 69 | #define EC1_KEY_EF 0x22 70 | #define EC2_KEY_EF 0x23 71 | #define DES_KEY_EF 0x19 72 | #define AES_KEY_EF 0x29 73 | 74 | 75 | uint8_t get_rsa_key_part (void *here, uint8_t id); 76 | -------------------------------------------------------------------------------- /src/card_os/mem_device.h: -------------------------------------------------------------------------------- 1 | /* 2 | mem_device.h 3 | 4 | This is part of OsEID (Open source Electronic ID) 5 | 6 | Copyright (C) 2015-2023 Peter Popovec, popovec.peter@gmail.com 7 | 8 | This program is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | This program is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . 20 | 21 | non volatile memory driver, header file 22 | 23 | */ 24 | 25 | /**************************************************** 26 | 27 | normal memory for files - data do not change often 28 | 29 | *****************************************************/ 30 | // for size = 0 size is interpreted as 256 31 | // on error 1 is returned, 0 = no error 32 | 33 | uint8_t device_read_block (void *buffer, uint16_t offset, uint8_t size); 34 | uint8_t device_write_block (void *buffer, uint16_t offset, uint8_t size); 35 | uint8_t device_write_ff (uint16_t offset, uint8_t size); 36 | 37 | // clear memory device 38 | uint8_t device_format(void); 39 | 40 | /**************************************************************** 41 | 42 | This memory is used for security data that may change more often 43 | (etc couter of unsucessful login ..) 44 | 45 | ****************************************************************/ 46 | // offset in range 0-1023 47 | // size 0 = 256 bytes 48 | uint8_t sec_device_read_block(void *buffer, uint16_t offset, uint8_t size); 49 | uint8_t sec_device_write_block(void *buffer, uint16_t offset, uint8_t size); 50 | // clear sec memory 51 | uint8_t sec_device_format(void); 52 | 53 | /**************************************************************** 54 | 55 | every change in the card's file system is tracked by a change counter 56 | This function is used to read the change counter value. 57 | 58 | ****************************************************************/ 59 | uint16_t device_get_change_counter(void); 60 | -------------------------------------------------------------------------------- /src/card_os/myeid_emu.h: -------------------------------------------------------------------------------- 1 | /* 2 | myeid_emu.h 3 | 4 | This is part of OsEID (Open source Electronic ID) 5 | 6 | Copyright (C) 2015-2023 Peter Popovec, popovec.peter@gmail.com 7 | 8 | This program is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | This program is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . 20 | 21 | myeid emulation header file 22 | 23 | */ 24 | #ifndef CS_MYEID_H 25 | #define CS_MYEID_H 26 | /* myeid_emu.c */ 27 | 28 | uint8_t security_env_set_reset(uint8_t * message, __attribute__((unused)) 29 | struct iso7816_response *r); 30 | 31 | uint8_t security_operation(uint8_t * message, struct iso7816_response *r); 32 | 33 | uint8_t myeid_generate_key(uint8_t * message, struct iso7816_response *r); 34 | 35 | uint8_t myeid_get_data(uint8_t * message, struct iso7816_response *r); 36 | 37 | uint8_t myeid_put_data(uint8_t * message, __attribute__((unused)) 38 | struct iso7816_response *r); 39 | 40 | uint8_t myeid_activate_applet( __attribute__((unused)) uint8_t * message, __attribute__((unused)) 41 | struct iso7816_response *r); 42 | 43 | uint8_t myeid_ecdh_derive(uint8_t * message, struct iso7816_response *r); 44 | 45 | #ifdef HW_SERIAL_NUMBER 46 | void get_HW_serial_number(uint8_t * s); 47 | #endif 48 | 49 | #endif 50 | -------------------------------------------------------------------------------- /src/card_os/rnd.h: -------------------------------------------------------------------------------- 1 | /* 2 | rnd.h 3 | 4 | This is part of OsEID (Open source Electronic ID) 5 | 6 | Copyright (C) 2015, 2017 Peter Popovec, popovec.peter@gmail.com 7 | 8 | This program is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | This program is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . 20 | 21 | Random number generatot header file 22 | 23 | */ 24 | #ifndef CS_RND_H 25 | #define CS_RND_H 26 | 27 | 28 | void rnd_init (void); 29 | // for size == 0 return 256 bytes 30 | void rnd_get (uint8_t *rnd, uint8_t size); 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /src/card_os/rsa.h: -------------------------------------------------------------------------------- 1 | /* 2 | rsa.h 3 | 4 | This is part of OsEID (Open source Electronic ID) 5 | 6 | Copyright (C) 2015-2023 Peter Popovec, popovec.peter@gmail.com 7 | 8 | This program is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | This program is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . 20 | 21 | RSA algo header file 22 | 23 | */ 24 | 25 | // RSA_BYTES - size of variable for RSA calculation. 26 | // 27 | // 128 bytes for 2048 RSA modulus (128*8=1024 bits, this is enough for CRT algo for 2048 RSA key) 28 | // 96 bytes for RSA 1536 29 | // 64 bytes for RSA 1024 30 | 31 | #ifndef RSA_BYTES 32 | #error RSA_BYTES undefined 33 | #endif 34 | 35 | #if RSA_BYTES != 64 && RSA_BYTES != 96 && RSA_BYTES != 128 36 | #error only fixed size RSA_BYTES are supported (64,96,128) 37 | #endif 38 | #ifndef __RSA_H__ 39 | #define __RSA_H__ 40 | 41 | #ifndef __ASSEMBLER__ 42 | typedef struct rsa_num 43 | { 44 | uint8_t value[RSA_BYTES]; //128bytes for 2048 RSA modulus + reserve for carry 45 | } rsa_num; 46 | 47 | typedef struct rsa_half_num 48 | { 49 | uint8_t value[RSA_BYTES/2]; 50 | } rsa_half_num; 51 | 52 | // allow use rsa_long_num as two rsa_num 53 | typedef struct rsa_long_num 54 | { 55 | union 56 | { 57 | uint8_t value[RSA_BYTES * 2]; //for multiplication result .. 58 | struct 59 | { 60 | rsa_num L; 61 | rsa_num H; 62 | }; 63 | }; 64 | } rsa_long_num; 65 | 66 | typedef struct rsa_exp_num 67 | { 68 | union 69 | { 70 | uint8_t value[RSA_BYTES + 8]; // always allow 8 bytes! big number arithmetic operates with number length in 64 bit steps 71 | rsa_num n; 72 | }; 73 | } rsa_exp_num; 74 | 75 | // structure is reused for intermediate results 76 | // in Chinese remainder algorithms 77 | /* 78 | struct rsa 79 | { 80 | rsa_num dP; // m1 here .. from c^dP mod p 81 | rsa_num dQ; // m2 here .. from c^dQ mod q 82 | rsa_num p; 83 | rsa_num q; 84 | rsa_num qInv; // h here .. from qInv.(m1 - m2) mod p 85 | // pointers for input and output, must address minimal 2*RSA_BYTES memory! 86 | // WARN, must point different places! 87 | rsa_long_num *data; // message/cyphertext 88 | rsa_long_num *result; // result after sign/decrypt 89 | uint8_t bytes; // size of rsa_num in bytes 90 | }; 91 | */ 92 | // 0 all ok, 1 error 93 | 94 | // this struct is used for RSA key generate function 95 | struct rsa_crt_key 96 | { 97 | union 98 | { 99 | rsa_long_num t[2]; 100 | struct 101 | { 102 | rsa_num dP; 103 | rsa_num dQ; 104 | rsa_num qInv; 105 | rsa_num d; // public exponent 106 | }; 107 | }; 108 | }; 109 | 110 | 111 | 112 | uint8_t rsa_calculate (uint8_t * data, uint8_t * result, uint16_t size); 113 | uint8_t rsa_keygen (uint8_t * message, uint8_t * r, struct rsa_crt_key *key, uint16_t size); 114 | uint8_t rsa_modulus(void *m); 115 | 116 | // return 0xffff for wrong padding (bit 15 is tested as error flag) 117 | // return value 0 .. 245 (up to 2048 bit RSA - 11 bytes for correct padding) 118 | // unpadded message is moved to buffer start 119 | // 120 | // caller is responsible to set input length in range 2..0x100 (bit 15 is 121 | // used to signalize error from decipher, this code masks this error and 122 | // performs fictitious depadding) 123 | // 124 | uint16_t remove_pkcs1_type_2_padding(uint8_t data[256], uint16_t len); 125 | 126 | #ifdef USE_P_Q_INV 127 | void rsa_inv_mod_N (rsa_half_num * n_, rsa_num * modulus); 128 | void rsa_mod (rsa_long_num * result, rsa_num * mod); 129 | void barrett_constant(rsa_num * Bc, rsa_num * modulus); 130 | #endif 131 | #endif 132 | 133 | // do not use different error codes for normal code 134 | // this save about 50 bytes of flash in AVR 135 | #if 0 136 | // error codes (for debug) 137 | #define Re_DATA_RESULT_SAME 230 138 | #define Re_P_GET_FAIL_1 231 139 | #define Re_P_EVEN_1 232 140 | #define Re_P_MOD_1 233 141 | #define Re_Q_GET_FAIL_1 234 142 | #define Re_Q_EVEN_1 235 143 | #define Re_Q_MOD_1 236 144 | #define Re_dP_1 237 145 | #define Re_dQ_1 238 146 | #define Re_P_GET_FAIL_2 239 147 | #define Re_P_GET_FAIL_3 240 148 | #define Re_P_GET_FAIL_4 241 149 | #define Re_qInv_GET_FAIL_1 242 150 | #define Re_Q_GET_FAIL_2 243 151 | #define Re_R_Single_Error 244 152 | #define Re_Q_Single_Error 245 153 | #else 154 | // error codes (normal) 155 | #define Re_DATA_RESULT_SAME 1 156 | #define Re_P_GET_FAIL_1 1 157 | #define Re_P_EVEN_1 1 158 | #define Re_P_MOD_1 1 159 | #define Re_Q_GET_FAIL_1 1 160 | #define Re_Q_EVEN_1 1 161 | #define Re_Q_MOD_1 1 162 | #define Re_dP_1 1 163 | #define Re_dQ_1 1 164 | #define Re_P_GET_FAIL_2 1 165 | #define Re_P_GET_FAIL_3 1 166 | #define Re_P_GET_FAIL_4 1 167 | #define Re_qInv_GET_FAIL_1 1 168 | #define Re_Q_GET_FAIL_2 1 169 | #define Re_R_Single_Error 1 170 | #define Re_Q_Single_Error 1 171 | #endif 172 | 173 | #endif 174 | 175 | -------------------------------------------------------------------------------- /src/lib/avr/bn_lib.S: -------------------------------------------------------------------------------- 1 | /* 2 | bn_lib.S 3 | 4 | This is part of OsEID (Open source Electronic ID) 5 | 6 | Copyright (C) 2015-2017 Peter Popovec, popovec.peter@gmail.com 7 | 8 | This program is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | This program is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . 20 | 21 | Atmega assembler, base routines for aritmetics for big number 22 | arithmetics 23 | 24 | This is blank for now, code is for now in old files 25 | */ 26 | 27 | -------------------------------------------------------------------------------- /src/lib/avr/load_sp.h: -------------------------------------------------------------------------------- 1 | /* 2 | load_sp.h 3 | 4 | This is part of OsEID (Open source Electronic ID) 5 | 6 | Copyright (C) 2015,2016,2018 Peter Popovec, popovec.peter@gmail.com 7 | 8 | This program is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | This program is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . 20 | 21 | Atmega/Xmega assembler routines - atomic SP load 22 | */ 23 | 24 | ///////////////////////////////////////////////////////////// 25 | 26 | /* 27 | There is a diference in atmega and xmega in I flag handling. 28 | atmega: 29 | ------- 30 | cli 31 | in r24, 0x3f 32 | ori r24, 0x80 33 | out 0x3f, r24 // irq not enabled 34 | nop // irq enabled 35 | 36 | xmega: 37 | ------ 38 | cli 39 | in r24, 0x3f 40 | ori r24, 0x80 41 | out 0x3f, r24 // irq enabled 42 | nop // irq enabled 43 | 44 | XMEGA does not need cli before changing SP, because writing 45 | to SPL automaticaly disables interrupts for 4 cycles or until the 46 | next IO memory write 47 | 48 | Update Wed Oct 3 12:37:28 CEST 2018 49 | 50 | There is new microchip docs available for example for atmega 328p with some 51 | changes in stack pointer registers description. Especialy, there is mention 52 | about related link to "Accessing 16-bit Timer/Counter Registers" in this 53 | section. 54 | 55 | This related link is not relevant for SPL/SPH update, SPH is updated 56 | directly, not over TEMP reg. 57 | 58 | 59 | */ 60 | 61 | // if ISR_ENABLE_FORCE is defined, interrupts are enabled, otherwise 62 | // previous state of I flag is restored 63 | // if ISR_DISABLED is defined, stack pointer is changed without CLI 64 | #ifdef ISR_DISABLED 65 | // it is assumed, interrupts are always disabled 66 | .macro LOAD_SP tmp RL RH 67 | out 0x3d, \RL 68 | out 0x3e, \RH 69 | .endm 70 | .macro LOAD_SP_SREG tmp RL RH 71 | out 0x3d, \RL 72 | out 0x3e, \RH 73 | .endm 74 | #else 75 | #ifndef ISR_ENABLE_FORCE 76 | #if __AVR_XMEGA__ == 1 77 | // reenable old state I flag 0 or 1 78 | // xmega code 79 | .macro LOAD_SP tmp RL RH 80 | out 0x3d, \RL 81 | out 0x3e, \RH 82 | .endm 83 | .macro LOAD_SP_SREG tmp RL RH 84 | out 0x3d, \RL 85 | out 0x3e, \RH 86 | .endm 87 | #else 88 | // atmega code 89 | .macro LOAD_SP tmp RL RH 90 | in \tmp,0x3f 91 | cli 92 | out 0x3d, \RL 93 | out 0x3f, \tmp 94 | out 0x3e, \RH 95 | .endm 96 | .macro LOAD_SP_SREG tmp RL RH 97 | cli 98 | out 0x3d, \RL 99 | out 0x3f, \tmp 100 | out 0x3e, \RH 101 | .endm 102 | #endif 103 | #else // ISR_ENABLE_FORCE 104 | #if __AVR_XMEGA__ == 1 105 | // xmega code ISR_ENABLE_FORCE 106 | #warning ISR_ENABLE_FORCE is not recomended for xmega! 107 | .macro LOAD_SP tmp RL RH 108 | out 0x3d, \RL 109 | out 0x3e, \RH 110 | sei 111 | .endm 112 | .macro LOAD_SP_SREG tmp RL RH 113 | out 0x3d, \RL 114 | out 0x3e, \RH 115 | sei 116 | .endm 117 | #else 118 | // atmega code ISR_ENABLE_FORCE 119 | .macro LOAD_SP tmp RL RH 120 | cli 121 | out 0x3d, \RL 122 | sei 123 | out 0x3e, \RH 124 | .endm 125 | .macro LOAD_SP_SREG tmp RL RH 126 | cli 127 | out 0x3d, \RL 128 | sei 129 | out 0x3e, \RH 130 | .endm 131 | #endif 132 | #endif // ISR_ENABLE_FORCE 133 | #endif // ISR_DISABLED 134 | -------------------------------------------------------------------------------- /src/lib/avr/mp_mul_521.S: -------------------------------------------------------------------------------- 1 | /* 2 | mp_mul_521.S 3 | 4 | This is part of OsEID (Open source Electronic ID) 5 | 6 | Copyright (C) 2018 Peter Popovec, popovec.peter@gmail.com 7 | 8 | This program is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | This program is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . 20 | 21 | Atmega assembler routines for 521 bit multiplications (for secp521r1) 22 | 23 | */ 24 | 25 | // enable this if some bytes of flash must be saved (22 bytes, code is 26 | // slower, for secp521r1 about 1.75 milions clock cycles) 27 | //#define SAVE_FLASH 28 | 29 | .global mp_square_521 30 | .type mp_square_521, @function 31 | 32 | .global mp_mul_521 33 | .type mp_mul_521, @function 34 | .section .text.mp_mul_521,"ax",@progbits 35 | 36 | mp_square_521: 37 | push r2 38 | push r3 39 | push r4 40 | push r5 41 | // save Y (ABI) 42 | push r28 43 | push r29 44 | // save operands.result positions 45 | movw r2,r24 46 | movw r4,r22 47 | movw r28,r22 48 | call rsa_square_512 49 | rjmp mp_mul_521_s 50 | 51 | mp_mul_521: 52 | push r2 53 | push r3 54 | push r4 55 | push r5 56 | // save Y (ABI) 57 | push r28 58 | push r29 59 | // save operands.result positions 60 | movw r2,r24 61 | movw r4,r22 62 | movw r28,r20 63 | call rsa_mul_512 64 | 65 | mp_mul_521_s: 66 | movw r26,r2 // result (now addressed by X) 67 | // low 64 bytes of result is already final 68 | subi r26,lo8(-64) 69 | sbci r27,hi8(-64) 70 | // operands A,B 71 | // movw r28, already in r28 72 | movw r30,r4 73 | // extract bits 520..512 from operands A, B 74 | adiw r30,63 75 | adiw r28,63 76 | ldd r18,Y+1 77 | ldd r20,Y+2 78 | ldd r19,Z+1 79 | ldd r21,Z+2 80 | neg r20 81 | neg r21 82 | sbiw r30,63 83 | sbiw r28,63 84 | 85 | // operands B[520..512]: r21,r19 A[520..512]:r20,r18 86 | // r21, r20 is 0 or ff! 87 | // pointer to B: r31,r30 (Z), pointer to A: r29,r28 (Y) 88 | 89 | // clear prev carry 90 | clr r3 91 | clr r4 92 | // zero reg 93 | clr r25 94 | #ifdef SAVE_FLASH 95 | ldi r24,66 //loop 96 | #else 97 | ldi r24,64 98 | #endif 99 | 1: 100 | ld r2,X 101 | ld r22,Z+ 102 | #ifdef SAVE_FLASH 103 | // do not use last bytes in result (over 128..) 104 | // do not duplicate multiplication for last bytes in operand 105 | cpi r24,3 106 | brcc 2f 107 | clr r2 108 | clr r22 109 | 2: 110 | #endif 111 | ld r23,Y+ 112 | // sumarize previous carry with partial result byte 113 | add r2,r3 114 | adc r4,r25 115 | mov r3,r4 116 | clr r4 117 | 118 | // do multiplication (8x8bit) 119 | mul r22,r18 //B A[519..512] 120 | add r2,r0 121 | adc r3,r1 122 | adc r4,r25 123 | 124 | mul r23,r19 //A B[B519..512] 125 | add r2,r0 126 | adc r3,r1 127 | adc r4,r25 128 | 129 | // do multiplication (1x8bit) 130 | and r22,r20 131 | add r3,r22 132 | adc r4,r25 133 | 134 | and r23,r21 135 | add r3,r23 136 | adc r4,r25 137 | 138 | st X+,r2 139 | dec r24 140 | brne 1b 141 | #ifdef SAVE_FLASH 142 | // carry byte into result (already 64+66 bytes = 130 bytes) 143 | // 521 bits*2= 1042 bits = 130.25 byte, 144 | st X+,r3 145 | #else 146 | mul r18,r19 147 | add r3,r0 148 | adc r4,r1 149 | adc r24,r25 150 | 151 | and r18,r21 152 | add r4,r18 153 | adc r24,r25 154 | 155 | and r19,r20 156 | add r4,r19 157 | adc r24,r25 158 | 159 | and r20,r21 160 | andi r20,1 161 | add r24,r20 162 | 163 | st X+,r3 164 | st X+,r4 165 | st X+,r24 166 | #endif 167 | // ABI 168 | clr r1 169 | // clean upper part (1042 bytes result = 131 bytes) 170 | ldi r24,MP_BYTES*2-131 171 | 1: 172 | st X+,r1 173 | dec r24 174 | brne 1b 175 | 176 | // restore Y (ABI) 177 | pop r29 178 | pop r28 179 | 180 | pop r5 181 | pop r4 182 | pop r3 183 | pop r2 184 | ret 185 | -------------------------------------------------------------------------------- /src/targets/AVR128DA/avr.S: -------------------------------------------------------------------------------- 1 | /* 2 | avr.S 3 | 4 | This is part of OsEID (Open source Electronic ID) 5 | 6 | Copyright (C) 2020 Peter Popovec, popovec.peter@gmail.com 7 | 8 | This program is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | This program is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . 20 | 21 | AVR128DA CPU initialization 22 | */ 23 | 24 | 25 | .section .vectors,"ax",@progbits 26 | .global __vectors 27 | .type __vectors, @function 28 | 29 | __vectors: 30 | jmp __init 31 | jmp 4 32 | jmp __vector_2 33 | jmp __vector_3 34 | 35 | 36 | .section .init0,"ax",@progbits 37 | 38 | __init: 39 | // switch CPU to 24MHz 40 | ldi r22,0xd8 41 | ldi r23,0x24 42 | out 0x34,r22 // unlock CCP 43 | sts 0x68,r23 // switch freq 44 | 45 | ldi r23,0x1f // tune oscilator at maximum 46 | sts 0x69,r23 47 | 48 | // do no tallow write to boot/application section, only application data section is allowed to be flashed 49 | ldi r23,3 // NVMCTRL.CTRLB, 3 50 | out 0x34,r22 // unlock CCP 51 | sts 0x1001,r23 52 | 53 | // compact vector table, vector in boot section 54 | ldi r23,0x60 // CPUINT.CTRLA,0x60 55 | out 0x34,r22 // unlock CCP 56 | sts 0x110,r23 57 | 58 | .section .init2,"ax",@progbits 59 | clr r1 60 | out 0x3f,r1 // clear SREG 61 | // set stack 62 | ldi r28,0xff 63 | ldi r29,0x7f 64 | out 0x3d,r28 65 | out 0x3e,r29 66 | 67 | // out 0x2b,r1 // clear RAMPZ 68 | 69 | // do copy data and clear bss is not needed 70 | .section .init9,"ax",@progbits 71 | 72 | // software reset is used (PF6 reset function is disabled by fuse) 73 | 74 | // wait for ISO7816 RST == 1 75 | 76 | 1: 77 | 78 | #if 0 79 | sbis (0x0+2),3 // VPORTA + 2 = IN from PORTA 80 | rjmp 1b 81 | #else 82 | in r24,0x02 // PORTA IN 83 | andi r24,0x08 // PF3 84 | breq 1b 85 | #endif 86 | // OK, RST is released 87 | 88 | // program high priority irq from pin PA3 (5 = level low) 89 | // PORTA.PIN3CTRL = 5; 90 | // 0x400 + 0x10 + 5 = PORTA.PIN3CTRL 91 | ldi r24,5 // level low interrupt 92 | sts 0x0413, r24 93 | // set CPUINT.LVL0PRI 94 | // for PORTA (vector 6, address 0x0c) 95 | ldi r22,0xd8 // KEY 96 | ldi r23,6 // vector number 97 | out 0x34,r22 // unlock CCP 98 | sts 0x113,r23 // CPUINT.LVL1VEC 99 | 100 | sei 101 | jmp main 102 | 103 | // Priority 1 level ISR (high) 104 | .global __vector_2 105 | .type __vector_2, @function 106 | // do soft reset 107 | __vector_2: 108 | ldi r22,0xd8 109 | ldi r23,1 110 | out 0x34,r22 // unlock CCP 111 | sts 0x41,r23 // run SW reset 112 | // reset seq. start immediately... ? 113 | rjmp 1b 114 | 115 | #ifdef HW_SERIAL_NUMBER 116 | .global get_HW_serial_number 117 | .type get_HW_serial_number, @function 118 | 119 | get_HW_serial_number: 120 | // void get_HW_serial_number (uint8_t *s); 121 | // AVR128DA - serial number address 0x1110 .. 0x111f 122 | // last four bytes = 0 123 | // we need 10 bytes 124 | ldi r31,0x11 125 | ldi r30,0x12 126 | movw r26,r24 127 | ldi r24,10 128 | 1: 129 | ld r0,Z+ 130 | st X+,r0 131 | dec r24 132 | brne 1b 133 | ret 134 | #endif 135 | 136 | .section .fuse 137 | __fuse: 138 | .byte 0 // WDTCFG 139 | .byte 0 // BODCFG 140 | .byte 0 // OSCCFG 141 | .byte 0 // reserved 142 | .byte 0 // reserved 143 | .byte 0xc0 // SYSCFG0 144 | .byte 0 // SYSCFG1 145 | .byte 0x80 // CODESIZE 146 | .byte 0x80 // BOOTSIZE 147 | 148 | 149 | .section .note.gnu.avr.deviceinfo, "", @note 150 | .long 4 // long of sring (AVR\0) 151 | .long 0x2d 152 | .long 1 // type 153 | .asciz "AVR" 154 | // flash start 155 | .long 0 156 | .long 0x20000 157 | 158 | // ram start/size 159 | .long 0x4000 160 | .long 0x4000 161 | // eeprom start ??? 0x1400 here ??? 162 | .long 0 163 | // eeprom size 164 | .long 512 165 | 166 | 167 | .L__stroffsettab_start: 168 | .long .L__stroffsettab_end - .L__stroffsettab_start 169 | .long .L__device_name_start - .L__strtab_start // offset of device name 170 | .L__stroffsettab_end: 171 | .L__strtab_start: 172 | .byte 0 173 | .L__device_name_start: 174 | .asciz "avr128da32" 175 | //.L__device_name_end: 176 | .byte 0 177 | .L__strtab_end: 178 | .balign 4 179 | -------------------------------------------------------------------------------- /src/targets/AVR128DA/constants.c: -------------------------------------------------------------------------------- 1 | /* 2 | constants.c 3 | 4 | This is part of OsEID (Open source Electronic ID) 5 | 6 | Copyright (C) 2015-2020 Peter Popovec, popovec.peter@gmail.com 7 | 8 | This program is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | This program is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . 20 | 21 | definition of constant values for eliptic curves, oids etc 22 | 23 | AVR128DA helper for read constants from flash. This file is based on 24 | old targets/atmega128/constants.c 25 | 26 | */ 27 | #ifdef HAVE_GET_CONSTANTS 28 | #ifndef __FLASH 29 | #error, Your compiler does not support Named Address Space 30 | #endif 31 | #include 32 | #include "constants.h" 33 | 34 | 35 | const uint8_t __flash constants[] = { 36 | C_CONSTANTS 37 | //last position 38 | 0xff 39 | }; 40 | 41 | // C code with named space 56 bytes 42 | // ASM code 40 bytes 43 | 44 | #if 0 45 | uint8_t 46 | get_constant (void *here, uint8_t id) 47 | { 48 | uint8_t *t; 49 | const __flash uint8_t *s = constants; 50 | uint8_t size, val; 51 | 52 | t = (uint8_t *) here; 53 | 54 | for (;;) 55 | { 56 | val = *s++; //pgm_read_byte (s); 57 | if (val == 0xff) 58 | return 0; 59 | size = *s++; //pgm_read_byte (s); 60 | if (val == id) 61 | { 62 | while (size) 63 | { 64 | *t = *s; //pgm_read_byte (s); 65 | t++; 66 | s++; 67 | size--; 68 | } 69 | return 1; 70 | } 71 | s += size; 72 | } 73 | } 74 | #else 75 | // constants are in 1st 64kB of Flash, no ELPM needed 76 | uint8_t 77 | get_constant (void *here, uint8_t id) 78 | { 79 | register uint8_t ret asm ("r24"); 80 | asm volatile ( // 81 | "1:\n" // 82 | "lpm r21,Z+\n" // load constant ID 83 | "cpi r21,0xff\n" // 84 | // test last constant ? 85 | "brne 2f\n" // 86 | "clr r24\n" // 87 | "ret \n" // 88 | "2:\n" // 89 | "lpm r20,Z+\n" // load constant size 90 | "cp r21,r22\n" // compare ID (r22 = id)? 91 | "breq 3f\n" // 92 | // jump to next constant 93 | "add r30,r20\n" // 94 | "adc r31,r1\n" // 95 | "rjmp 1b\n" // 96 | // ok, this constant to RAM 97 | "3:\n" // 98 | "lpm r0,Z+\n" // 99 | "st X+,r0\n" // 100 | "dec r20\n" // 101 | "brne 3b\n" // 102 | "ldi r24,1\n" // 103 | :"=r" (ret):"z" (constants), "x" (here):); 104 | return ret; 105 | } 106 | #endif // C/ASM 107 | 108 | #endif // have get constants 109 | -------------------------------------------------------------------------------- /src/targets/AVR128DA/device-specs/specs-avr128da32: -------------------------------------------------------------------------------- 1 | # This file may be replaced by original file from pack downloaded from 2 | # https://packs.download.microchip.com/ 3 | # 4 | # because here (OsEID project) no default libs are used, (there is no 5 | # need to use crtavr128da32.o, because we using compact vector ..) 6 | # avrlibc_startfile and avrlibc_devicelib must be commented out 7 | # 8 | # This file is public domain and is used in OsEID project 9 | # https://sourceforge.net/projects/oseid/ 10 | # 11 | # here only neccesary directives: 12 | 13 | 14 | *cc1_errata_skip: 15 | %{!mskip-bug: -mno-skip-bug} 16 | 17 | *asm_arch: 18 | -mmcu=avrxmega4 19 | 20 | *asm_errata_skip: 21 | %{!mskip-bug: -mno-skip-bug} 22 | 23 | *link_arch: 24 | %{mmcu=*:-m%*} 25 | 26 | *self_spec: 27 | %. 23 | 24 | AVR128DA random number generator (from temperature sensor) 25 | only bit 0 from ADC conversion is used 26 | 27 | */ 28 | #include 29 | #include 30 | 31 | #include "rnd.h" 32 | 33 | #define l_VREF_ADC0REF 0xa0 34 | #define l_ADC0 0x600 35 | void 36 | rnd_init (void) 37 | { 38 | // define register position, AVR128DA is new device, lot of people does not have gcc include file for this CPU 39 | volatile uint8_t *adc_base = (uint8_t *) 0x600; 40 | volatile uint8_t *vref_base = (uint8_t *) 0xa0; 41 | 42 | // use VDD - minimal VDD for band gap is 2.5V, but card is able to run at 1.8V 43 | vref_base[0] = 0x85; 44 | 45 | // single ended, right adjusted conversion, enable ADC /CTRLA/ 46 | adc_base[0] = 1; // 47 | // AVR is running at 24MHz internal oscilator, but tuned to max.. (about 27MHz) 48 | // clock divisor 24 (27) MHz/ 256 = about 93 (105) kHz /CTRLC/ 49 | adc_base[2] = 0x0d; 50 | // mux to temperature sensor /MUXPOS/ 51 | adc_base[8] = 0x42; 52 | } 53 | 54 | void 55 | rnd_get (uint8_t * r, uint8_t size) 56 | { 57 | uint8_t v; 58 | uint8_t pos = 0, b; 59 | 60 | volatile uint8_t *adc_base = (uint8_t *) 0x600; 61 | 62 | do 63 | { 64 | v = r[pos]; 65 | for (b = 0; b < 8; b++) 66 | { 67 | //start conversion (ADC0_COMMAND = 1) 68 | adc_base[10] = 1; 69 | // wait for conversion 70 | while (adc_base[10] & 1); 71 | #pragma GCC diagnostic ignored "-Wuninitialized" 72 | v <<= 1; 73 | v |= adc_base[16] & 1; // 1st read low part of result 74 | adc_base[17]; // read 2nd part 75 | } 76 | r[pos] ^= v; 77 | pos++; 78 | } 79 | while (--size); 80 | } 81 | -------------------------------------------------------------------------------- /src/targets/STM32F10x/LED.h: -------------------------------------------------------------------------------- 1 | /* 2 | LED.h 3 | 4 | This is part of OsEID (Open source Electronic ID) 5 | 6 | Copyright (C) 2015-2024 Peter Popovec, popovec.peter@gmail.com 7 | 8 | This program is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | This program is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . 20 | 21 | Macro definition for LEDs 22 | 23 | */ 24 | 25 | // signalize reader state 26 | // WAIT = reader wait for command from host 27 | // BUSY = reader toggles LED (waiting wor card, null byt sending) 28 | // RUN = command parsing/response generating 29 | 30 | #ifndef SERIAL_DEBUG 31 | void led1_on(void); 32 | void led1_off(void); 33 | void led1_toggle(void); 34 | void led2_on(void); 35 | void led2_off(void); 36 | 37 | // led is on on idle, of on run 38 | #define LED1_INIT() 39 | #define LED1_BUSY() led1_toggle() 40 | #define LED1_IDLE() led1_on() 41 | #define LED1_RUN() led1_off() 42 | #define LED1_OFF() led1_off() 43 | #else 44 | #define LED1_INIT() 45 | #define LED1_IDLE() 46 | #define LED1_BUSY() 47 | #define LED1_RUN() 48 | #define LED1_OFF() 49 | #endif 50 | 51 | // signalize suspend/online 52 | #ifndef SERIAL_DEBUG 53 | #define LED2_INIT() 54 | #define LED2_SUSPEND() led2_off() 55 | #define LED2_RUN() led2_on() 56 | #else 57 | #define LED2_INIT() 58 | #define LED2_SUSPEND() 59 | #define LED2_RUN() 60 | #endif 61 | -------------------------------------------------------------------------------- /src/targets/STM32F10x/README: -------------------------------------------------------------------------------- 1 | STlink schematics: 2 | https://stm32world.com/wiki/DIY_STM32_Programmer_%28ST-Link/V2-1%29 3 | 4 | PA9 LED .. 5 | 6 | 7 | security: 8 | 9 | "There is way to read locked MCU - flash data reads via the debug interface 10 | are blocked but the CPU's exception handling process is still able to read 11 | from flash memory via the ICode bus" ... 12 | 13 | https://blog.zapb.de/stm32f1-exceptional-failure/ 14 | 15 | https://gitlab.zapb.de/zapb/stm32f1-firmware-extractor 16 | -------------------------------------------------------------------------------- /src/targets/STM32F10x/STM32F10X.ld: -------------------------------------------------------------------------------- 1 | ENTRY(Reset_handler) 2 | 3 | /* flash size is specified in Makefile (depends on MCU) */ 4 | 5 | MEMORY 6 | { 7 | FLASH ( rx ) : ORIGIN = 0x08000000, LENGTH = CFG_CODE_SIZE 8 | RAM ( rxw ) : ORIGIN = 0x20000000, LENGTH = CFG_RAM_SIZE 9 | } 10 | 11 | _ram_end_ = ORIGIN(RAM) + LENGTH(RAM) -1 ; 12 | _estack = (_ram_end_ - 7); 13 | _estack &= 0xfffffff8; 14 | _user_stack = _estack; 15 | _user_stack -= 1024; 16 | 17 | SECTIONS 18 | { 19 | .vector_table : 20 | { 21 | . = ALIGN(4); 22 | KEEP (*(.vector_table)) 23 | . = ALIGN(4); 24 | } >FLASH 25 | 26 | .text : 27 | { 28 | . = ALIGN(4); 29 | *(.text) 30 | *(.text*) 31 | . = ALIGN(4); 32 | } >FLASH 33 | 34 | .ARM.extab : 35 | { 36 | *(.ARM.extab* .gnu.linkonce.armextab.*) 37 | } > FLASH 38 | exidx_start = .; 39 | 40 | .ARM.exidx : 41 | { 42 | *(.ARM.exidx* .gnu.linkonce.armexidx.*) 43 | } > FLASH 44 | __exidx_end = .; 45 | 46 | .rodata : 47 | { 48 | . = ALIGN(4); 49 | *(.rodata) 50 | *(.rodata*) 51 | . = ALIGN(4); 52 | } >FLASH 53 | 54 | _sidata = .; 55 | .data : AT(_sidata) 56 | { 57 | . = ALIGN(4); 58 | _sdata = .; 59 | *(.data) 60 | *(.data*) 61 | _edata = .; 62 | . = ALIGN(4); 63 | } >RAM 64 | 65 | .bss : 66 | { 67 | . = ALIGN(4); 68 | _sbss = .; 69 | *(.bss) 70 | *(.bss*) 71 | *(COMMON) 72 | . = ALIGN(4); 73 | _ebss = .; 74 | } >RAM 75 | 76 | end = . ; 77 | _end = . ; 78 | } 79 | -------------------------------------------------------------------------------- /src/targets/STM32F10x/STM32F10x_flash.device.c: -------------------------------------------------------------------------------- 1 | /* 2 | STM32F10x_flash.device.c 3 | 4 | This is part of OsEID (Open source Electronic ID) 5 | 6 | Copyright (C) 2024 Peter Popovec, popovec.peter@gmail.com 7 | 8 | This program is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | This program is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . 20 | 21 | Driver for FLASH memory in STM32F102CB 22 | 23 | No HAL is used, this is pure bare metal code! 24 | 25 | */ 26 | #include 27 | #include "STM32F10x_flash.device.h" 28 | 29 | #define FPEC_BASE 0x40022000 30 | #define FPEC_RDPRT_key 0x00A5 31 | #define FPEC_KEY1 0x45670123 32 | #define FPEC_KEY2 0xCDEF89AB 33 | 34 | #define FLASH_KEYR 1 35 | #define FLASH_CR (0x10/4) 36 | #define FLASH_SR (0xc/4) 37 | 38 | static void flash_unlock() 39 | { 40 | volatile uint32_t *fpec = (uint32_t *) (FPEC_BASE); 41 | 42 | fpec[FLASH_KEYR] = FPEC_KEY1; 43 | fpec[FLASH_KEYR] = FPEC_KEY2; 44 | } 45 | 46 | static void flash_lock() 47 | { 48 | volatile uint32_t *fpec = (uint32_t *) (FPEC_BASE); 49 | // disable wtite, page erase etc.. 50 | fpec[FLASH_CR] = 0; 51 | // lock 52 | fpec[FLASH_CR] |= (1 << 7); 53 | } 54 | 55 | int flash_hw_erase_page(void *address) 56 | { 57 | volatile uint32_t *fpec = (uint32_t *) (FPEC_BASE); 58 | 59 | uint32_t *a = (uint32_t *) address; 60 | int i = 256; 61 | uint32_t test = 0xffffffff; 62 | 63 | if ((uint8_t *) address < (uint8_t *) FLASH_MAP_BASE) 64 | return 1; 65 | if ((uint8_t *) address >= (uint8_t *) 0x08020000) 66 | return 1; 67 | //check if page must be erased... 68 | while (i--) 69 | test &= *(a++); 70 | if (test == 0xffffffff) 71 | return 0; 72 | a = (uint32_t *) address; 73 | flash_unlock(); 74 | // page erase.. wait if BUSY 75 | while (fpec[FLASH_SR] & 1) ; 76 | // set PER 77 | fpec[FLASH_CR] |= (1 << 1); 78 | 79 | // set address (address within page) 80 | fpec[0x14 / 4] = (uint32_t) a; 81 | // start; 82 | fpec[FLASH_CR] |= (1 << 6); 83 | // wait end 84 | while (fpec[FLASH_SR] & 1) ; 85 | flash_lock(); 86 | 87 | // recheck 88 | //check if page is erased... 89 | a = (uint32_t *) address; 90 | test = 0xffffffff; 91 | i = 256; 92 | while (i--) 93 | test &= *(a++); 94 | if (test != 0xffffffff) 95 | return 2; 96 | return 0; 97 | } 98 | 99 | int flash_hw_write_data(void *dst, void *src, uint16_t size) 100 | { 101 | volatile uint32_t *fpec = (uint32_t *) (FPEC_BASE); 102 | 103 | uint16_t *d = (uint16_t *) dst; 104 | uint16_t *s = (uint16_t *) src; 105 | uint8_t *test = (uint8_t *) dst; 106 | uint16_t i; 107 | 108 | if (test < (uint8_t *) FLASH_MAP_BASE) 109 | return 1; 110 | if (test + size > (uint8_t *) 0x08020000) 111 | return 1; 112 | flash_unlock(); 113 | //enable write 114 | fpec[FLASH_CR] |= 1; 115 | 116 | for (i = 0; i < size / 2; i++, d++, s++) { 117 | if (*s == 0xffff) 118 | continue; 119 | if (*s == *d) 120 | continue; 121 | *d = *s; 122 | while (fpec[FLASH_SR] & 1) ; 123 | } 124 | flash_lock(); 125 | 126 | d = (uint16_t *) dst; 127 | s = (uint16_t *) src; 128 | for (i = 0; i < size / 2; i++, d++, s++) 129 | if (*s != *d) 130 | return 2; 131 | return 0; 132 | } 133 | -------------------------------------------------------------------------------- /src/targets/STM32F10x/STM32F10x_flash.device.h: -------------------------------------------------------------------------------- 1 | /* 2 | STM32F10x_flash.device.h 3 | 4 | This is part of OsEID (Open source Electronic ID) 5 | 6 | Copyright (C) 2024 Peter Popovec, popovec.peter@gmail.com 7 | 8 | This program is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | This program is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . 20 | 21 | Driver for FLASH memory in STM32F102CB 22 | 23 | No HAL is used, this is pure bare metal code! 24 | 25 | */ 26 | 27 | // erase page at address "address" 28 | // return 0 if OK 29 | int __attribute__((warn_unused_result)) flash_hw_erase_page(void *address); 30 | // return 0 if OK 31 | // write data to flash 32 | int __attribute__((warn_unused_result)) flash_hw_write_data(void *dst, void *src, uint16_t size); 33 | -------------------------------------------------------------------------------- /src/targets/STM32F10x/STM32F10x_init.S: -------------------------------------------------------------------------------- 1 | /* 2 | STM32F10x_init.S 3 | 4 | This is part of OsEID (Open source Electronic ID) 5 | 6 | Copyright (C) 2024 Peter Popovec, popovec.peter@gmail.com 7 | 8 | This program is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | This program is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . 20 | 21 | STM32F102 reset handler + vector table 22 | 23 | */ 24 | 25 | .syntax unified 26 | .cpu cortex-m3 27 | .fpu softvfp 28 | .thumb 29 | 30 | .global vtable 31 | .type vtable, %object 32 | .section .vector_table,"a",%progbits 33 | vtable: 34 | // cortex-M3 35 | .word _estack 36 | .word Reset_handler 37 | .word default_interrupt_handler 38 | .word default_interrupt_handler 39 | // 0x10 40 | //MemManage BusFault UsageFault Reserved ... 41 | .word default_interrupt_handler, default_interrupt_handler, default_interrupt_handler, default_interrupt_handler 42 | // 0x20 43 | .word default_interrupt_handler, default_interrupt_handler, default_interrupt_handler 44 | // 0x2c SVCall 45 | .word default_interrupt_handler 46 | // 0x30 Debug Monitor 47 | .word default_interrupt_handler 48 | // 0x34 reserved 49 | .word default_interrupt_handler 50 | // 0x38 PendSV 51 | .word default_interrupt_handler 52 | // 0x3c SysTick 53 | .word default_interrupt_handler 54 | 55 | // STM32F102 specific 56 | // 0x40 WWDG 57 | .word default_interrupt_handler 58 | // PVD 59 | .word default_interrupt_handler 60 | // TAMPER 61 | .word default_interrupt_handler 62 | // RTC 63 | .word default_interrupt_handler 64 | // 0x50 FLASH 65 | .word default_interrupt_handler 66 | // RCC 67 | .word default_interrupt_handler 68 | // EXTI0 69 | .word default_interrupt_handler 70 | // EXTI1 71 | .word default_interrupt_handler 72 | // 0x60 EXTI2 73 | .word default_interrupt_handler 74 | // EXTI3 75 | .word default_interrupt_handler 76 | // EXTI4 77 | .word default_interrupt_handler 78 | // DMA1_Channel1 79 | .word default_interrupt_handler 80 | // 0x70 DMA1_Channel2 81 | .word default_interrupt_handler 82 | // DMA1_Channel3 83 | .word default_interrupt_handler 84 | // DMA1_Channel4 85 | .word default_interrupt_handler 86 | // DMA1_Channel5 87 | .word default_interrupt_handler 88 | // 0x80 DMA1_Channel6 89 | .word default_interrupt_handler 90 | // DMA1_Channel7 91 | .word default_interrupt_handler 92 | // ADC1_2 93 | .word default_interrupt_handler 94 | // USB_HP 95 | .word USB_HP_handler 96 | // 0x90 USB_LP 97 | .word USB_LP_handler 98 | .word default_interrupt_handler 99 | .word default_interrupt_handler 100 | .word default_interrupt_handler 101 | // 0xa0 102 | .word default_interrupt_handler 103 | .word default_interrupt_handler 104 | .word default_interrupt_handler 105 | .word default_interrupt_handler 106 | // 0xb0 107 | .word TIM2_handler 108 | .word default_interrupt_handler 109 | .word default_interrupt_handler 110 | .word default_interrupt_handler 111 | // 0xc0 112 | .word default_interrupt_handler 113 | .word default_interrupt_handler 114 | .word default_interrupt_handler 115 | .word default_interrupt_handler 116 | // 0xd0 117 | .word default_interrupt_handler 118 | .word default_interrupt_handler 119 | .word default_interrupt_handler 120 | .word default_interrupt_handler 121 | // 0xe0 122 | .word default_interrupt_handler 123 | .word default_interrupt_handler 124 | .word USBWakeup_handler 125 | .word default_interrupt_handler 126 | // 0xf0 127 | .word default_interrupt_handler 128 | .word default_interrupt_handler 129 | .word default_interrupt_handler 130 | .word default_interrupt_handler 131 | // 0x100 132 | .word default_interrupt_handler 133 | .word default_interrupt_handler 134 | .word default_interrupt_handler 135 | .word default_interrupt_handler 136 | // 0x110 137 | .word default_interrupt_handler 138 | .word default_interrupt_handler 139 | .word default_interrupt_handler 140 | .word default_interrupt_handler 141 | // 0x120 142 | .word default_interrupt_handler 143 | .word default_interrupt_handler 144 | .word default_interrupt_handler 145 | .word default_interrupt_handler 146 | 147 | // 0x130 .. free flash .. 148 | 149 | .weak USBWakeup_handler 150 | .thumb_set USBWakeup_handler,default_interrupt_handler 151 | 152 | .weak USB_HP_handler 153 | .thumb_set USB_HP_handler,default_interrupt_handler 154 | 155 | .weak USB_LP_handler 156 | .thumb_set USB_LP_handler,default_interrupt_handler 157 | 158 | .weak TIM2_handler 159 | .thumb_set TIM2_handler,default_interrupt_handler 160 | 161 | .section .text.default_interrupt_handler,"ax",%progbits 162 | .global default_interrupt_handler 163 | .type default_interrupt_handler, %function 164 | 165 | default_interrupt_handler: 166 | b . 167 | 168 | .size default_interrupt_handler, .-default_interrupt_handler 169 | 170 | 171 | 172 | .global Reset_handler 173 | .type Reset_handler, %function 174 | 175 | Reset_handler: 176 | // init stack pointer 177 | ldr r0, =_estack 178 | mov sp, r0 179 | 180 | // init RAM variables 181 | movs r0, #0 182 | ldr r1, =_sdata 183 | ldr r2, =_edata 184 | ldr r3, =_sidata 185 | b 2f 186 | 187 | 1: 188 | ldr r4, [r3, r0] 189 | str r4, [r1, r0] 190 | adds r0, r0, #4 191 | 192 | 2: 193 | adds r4, r0, r1 194 | cmp r4, r2 195 | bcc 1b 196 | 197 | // clear BSS 198 | movs r0, #0 199 | ldr r1, =_sbss 200 | ldr r2, =_ebss 201 | b 2f 202 | 203 | 1: 204 | str r0, [r1] 205 | adds r1, r1, #4 206 | 207 | 2: 208 | cmp r1, r2 209 | bcc 1b 210 | 211 | bl init_stm 212 | // do not call main, main is maintained in init_stm() as user task 213 | // bl main 214 | b . 215 | .align 4 216 | .size Reset_handler, .-Reset_handler 217 | -------------------------------------------------------------------------------- /src/targets/STM32F10x/ccid.h: -------------------------------------------------------------------------------- 1 | /* 2 | ccid.h 3 | 4 | This is part of OsEID (Open source Electronic ID) 5 | 6 | Copyright (C) 2015-2024 Peter Popovec, popovec.peter@gmail.com 7 | 8 | This program is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | This program is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . 20 | 21 | CCID subsystem, header file 22 | 23 | */ 24 | // shoud be called before USB initialization 25 | void CCID_Init (void); 26 | // should be called SetConfiguration and after resume 27 | void CCID_notify(void); 28 | 29 | // called if BULK endpoint receive some data from host 30 | void CCID_Handler_from_host(uint8_t * ccid_out, uint16_t cnt); 31 | 32 | -------------------------------------------------------------------------------- /src/targets/STM32F10x/constants.c: -------------------------------------------------------------------------------- 1 | /* 2 | constants.c 3 | 4 | This is part of OsEID (Open source Electronic ID) 5 | 6 | Copyright (C) 2015-2024 Peter Popovec, popovec.peter@gmail.com 7 | 8 | This program is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | This program is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . 20 | 21 | definition of constant values for eliptic curves, oids etc 22 | 23 | */ 24 | #ifndef HAVE_GET_CONSTANTS 25 | #include 26 | #include 27 | #include "constants.h" 28 | 29 | /* *INDENT-OFF* */ 30 | uint8_t const constants[] = { 31 | C_CONSTANTS 32 | //last position 33 | 0xff 34 | }; 35 | /* *INDENT-ON* */ 36 | 37 | uint8_t get_constant(void *here, uint8_t id) 38 | { 39 | uint8_t *t; 40 | uint8_t *s = (uint8_t *) constants; 41 | uint8_t size; 42 | 43 | t = (uint8_t *) here; 44 | for (;;) { 45 | if (*s == 0xff) { 46 | return 0; 47 | } 48 | if (*s == id) { 49 | s++; 50 | size = *s; 51 | s++; 52 | while (size) { 53 | *t = *s; 54 | t++; 55 | s++; 56 | size--; 57 | } 58 | return 1; 59 | } 60 | s++; 61 | size = *s; 62 | s++; 63 | s += size; 64 | } 65 | } 66 | #endif 67 | -------------------------------------------------------------------------------- /src/targets/STM32F10x/flash_cow_dev.h: -------------------------------------------------------------------------------- 1 | /* 2 | flash_cow_dev.h 3 | 4 | This is part of OsEID (Open source Electronic ID) 5 | 6 | Copyright (C) 2024 Peter Popovec, popovec.peter@gmail.com 7 | 8 | This program is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | This program is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . 20 | 21 | Driver for FLASH memory COW version 22 | 23 | */ 24 | 25 | uint8_t __attribute__((warn_unused_result)) flash_device_write_block(void *buffer, uint32_t offset, uint32_t size); 26 | uint8_t __attribute__((warn_unused_result)) flash_device_read_block(void *buffer, uint32_t offset, uint32_t size); 27 | uint16_t flash_device_get_changecounter(void); 28 | -------------------------------------------------------------------------------- /src/targets/STM32F10x/mem_device.c: -------------------------------------------------------------------------------- 1 | /* 2 | mem_device.c 3 | 4 | This is part of OsEID (Open source Electronic ID) 5 | 6 | Copyright (C) 2015-2024 Peter Popovec, popovec.peter@gmail.com 7 | 8 | This program is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | This program is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . 20 | 21 | mem device driver 22 | 23 | */ 24 | #include 25 | #include 26 | #include "mem_device.h" 27 | #include "flash_cow_dev.h" 28 | 29 | #if SEC_MEM_SIZE != 1024 30 | #error please recheck SEC_MEM_SIZE 31 | #endif 32 | 33 | #define DEV_SIZE ((FLASH_PAGE_COUNT - 2) * 1024) 34 | // size 0 is interpreted as 256! 35 | uint8_t sec_device_read_block(void *buffer, uint16_t offset, uint8_t size) 36 | { 37 | uint16_t overflow, s; 38 | 39 | s = size ? size : 256; 40 | overflow = offset + s - 1; 41 | if (overflow > SEC_MEM_SIZE) 42 | return 1; 43 | return flash_device_read_block(buffer, offset, s); 44 | } 45 | 46 | // size 0 is interpreted as 256! 47 | uint8_t sec_device_write_block(void *buffer, uint16_t offset, uint8_t size) 48 | { 49 | uint16_t overflow, s; 50 | 51 | s = size ? size : 256; 52 | overflow = offset + s - 1; 53 | if (overflow > SEC_MEM_SIZE) 54 | return 1; 55 | return flash_device_write_block(buffer, offset, s); 56 | } 57 | 58 | uint8_t sec_device_format() 59 | { 60 | uint8_t ffblock[1024]; 61 | memset(ffblock, 0xff, sizeof(ffblock)); 62 | 63 | return flash_device_write_block(ffblock, 0, 1024); 64 | } 65 | 66 | uint16_t device_get_change_counter() 67 | { 68 | return flash_device_get_changecounter(); 69 | } 70 | 71 | //size 0 is interpreted as 256 ! 72 | uint8_t device_read_block(void *buffer, uint16_t offset, uint8_t size) 73 | { 74 | uint32_t overflow, s; 75 | 76 | s = size ? size : 256; 77 | overflow = offset + s - 1; 78 | if (overflow > DEV_SIZE) 79 | return 1; 80 | return flash_device_read_block(buffer, offset + 1024, s); 81 | } 82 | 83 | //size 0 is interpreted as 256 ! 84 | uint8_t device_write_block(void *buffer, uint16_t offset, uint8_t size) 85 | { 86 | uint32_t overflow, s; 87 | s = size ? size : 256; 88 | 89 | overflow = offset + s - 1; 90 | if (overflow > 65536) 91 | return 1; 92 | return flash_device_write_block(buffer, offset + 1024, s); 93 | } 94 | 95 | //size 0 is interpreted as 256 ! 96 | uint8_t device_write_ff(uint16_t offset, uint8_t size) 97 | { 98 | uint8_t ffblock[256]; 99 | uint32_t overflow, s; 100 | 101 | s = size ? size : 256; 102 | overflow = offset + s - 1; 103 | if (overflow > DEV_SIZE) 104 | return 1; 105 | memset(ffblock, 0xff, sizeof(ffblock)); 106 | return flash_device_write_block(ffblock, offset + 1024, s); 107 | } 108 | 109 | uint8_t device_format() 110 | { 111 | uint8_t ffblock[1024]; 112 | 113 | memset(ffblock, 0xff, sizeof(ffblock)); 114 | for (int i = 0; i < (FLASH_PAGE_COUNT - 2); i++) 115 | if (flash_device_write_block(ffblock, 1024 + i * 1024, 1024)) 116 | return 1; 117 | return 0; 118 | } 119 | -------------------------------------------------------------------------------- /src/targets/STM32F10x/os.h: -------------------------------------------------------------------------------- 1 | /* 2 | os.h 3 | 4 | This is part of OsEID (Open source Electronic ID) 5 | 6 | Copyright (C) 2015-2024 Peter Popovec, popovec.peter@gmail.com 7 | 8 | This program is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | This program is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . 20 | 21 | Various functions related to power management, task management... 22 | 23 | */ 24 | 25 | // called from main task if waiting for data from interrupt 26 | void CPU_idle(void); 27 | 28 | // this is used by CCID layer to restart card task 29 | void CPU_do_restart_main(void); 30 | -------------------------------------------------------------------------------- /src/targets/STM32F10x/rnd.c: -------------------------------------------------------------------------------- 1 | /* 2 | rnd.c 3 | 4 | This is part of OsEID (Open source Electronic ID) 5 | 6 | Copyright (C) 2024 Peter Popovec, popovec.peter@gmail.com 7 | 8 | based on OsEID project, file targets/xmega128a4u/rnd.c 9 | Copyright (C) 2015-2019 Peter Popovec, popovec.peter@gmail.com 10 | 11 | This program is free software: you can redistribute it and/or modify 12 | it under the terms of the GNU General Public License as published by 13 | the Free Software Foundation, either version 3 of the License, or 14 | (at your option) any later version. 15 | 16 | This program is distributed in the hope that it will be useful, 17 | but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 | GNU General Public License for more details. 20 | 21 | You should have received a copy of the GNU General Public License 22 | along with this program. If not, see . 23 | 24 | STM32F102 random number generator (from temperature sensor) 25 | only bit 0 from ADC conversion is used 26 | 27 | */ 28 | #include 29 | #include "rnd.h" 30 | 31 | #define ADC1_BASE 0x40012400U 32 | void rnd_init(void) 33 | { 34 | volatile uint32_t *address = (uint32_t *) (ADC1_BASE); 35 | // 239.5 cycles (20uS at 12MHz ADC clock) 36 | address[0x0c / 4] = (7 << 18); 37 | // ADC channel TEMP 38 | address[2] = (1 << 23); 39 | // powert on 40 | address[2] |= 1; 41 | } 42 | 43 | static uint32_t entropy_crc(uint32_t crc, uint8_t n) 44 | { 45 | int j; 46 | uint32_t mask; 47 | 48 | crc = crc ^ n; 49 | for (j = 7; j >= 0; j--) { 50 | mask = -(crc & 1); 51 | crc = (crc >> 1) ^ (0xEDB88320 & mask); 52 | } 53 | return ~crc; 54 | } 55 | 56 | void rnd_get(uint8_t * r, uint8_t size) 57 | { 58 | static uint32_t __attribute__((section(".noinit"))) random; 59 | volatile uint32_t *address = (uint32_t *) (ADC1_BASE); 60 | uint32_t val; 61 | int c; 62 | uint8_t b = 0; 63 | 64 | while (size--) { 65 | for (c = 0; c < 8; c++) { 66 | // start 67 | address[2] |= 1; 68 | // wait conversion 69 | while ((address[0] & (1 << 1)) == 0) ; 70 | // lowest bit only 71 | val = address[0x4c / 4]; 72 | b <<= 1; 73 | b |= (val & 1); 74 | } 75 | random = entropy_crc(random, b); 76 | *(r++) = random & 0xff; 77 | random >>= 7; 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /src/targets/STM32F10x/serial_debug.c: -------------------------------------------------------------------------------- 1 | /* 2 | serial debug module 3 | 4 | This is part of OsEID (Open source Electronic ID) 5 | 6 | Copyright (C) 2017-2024 Peter Popovec, popovec.peter@gmail.com 7 | 8 | This program is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | This program is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . 20 | 21 | this module is used to dump USB traffic to serial port (for debug only) 22 | 23 | */ 24 | #ifdef SERIAL_DEBUG 25 | #include 26 | 27 | #include "serial_debug.h" 28 | 29 | #define USART1_BASE 0x40013800U 30 | #define GPIOA_BASE 0x40010800U 31 | #define RCC_BASE 0x40021000U 32 | 33 | void serial_init() 34 | { 35 | volatile uint32_t *gpio = (uint32_t *) USART1_BASE; 36 | volatile uint32_t *usart = (uint32_t *) USART1_BASE; 37 | volatile uint32_t *rcc = (uint32_t *) (RCC_BASE); 38 | 39 | // enable USART1 clock (APB2 bus) 40 | rcc[0x18 / 4] |= (1 << 14); 41 | 42 | // enable GPIOA clock (APB2 bus) 43 | rcc[0x18 / 4] |= 4; 44 | 45 | // enable usart, enable TX 46 | usart[3] = (1 << 13) | (1 << 3); // CTRL1 47 | 48 | // clock PCLK2 <- from APB2 prescaler[1] <- AHB prescaler [1] <- PLL [XX MHz] 49 | usart[2] = (CORE_FREQ * 2); // 500k 50 | 51 | // overwrite GPIO pin function to alternate fcion - USART 52 | gpio = (uint32_t *) GPIOA_BASE; 53 | gpio[4 / 4] &= 0xFFFFFF0F; 54 | // gpio[4 / 4] |= 0x0010; // PA9 push pull, 10MHz 55 | gpio[4 / 4] |= 0x00A0; // alternate .. USART 1 TX 56 | } 57 | 58 | void serial_putchar(uint8_t b) 59 | { 60 | volatile uint8_t *usart = (uint8_t *) USART1_BASE; 61 | uint16_t val; 62 | volatile uint16_t *usart16 = (uint16_t *) USART1_BASE; 63 | usart16 = (uint16_t *) USART1_BASE; 64 | 65 | for (;;) { 66 | val = *usart16; // USART_STS 67 | if (val & 0x80) // TDE (transmit register empty) 68 | break; 69 | } 70 | usart = (uint8_t *) USART1_BASE; 71 | usart[4] = b; 72 | } 73 | 74 | static void serial_print_nibble(uint8_t d) 75 | { 76 | d &= 15; 77 | if (d > 9) 78 | d += 'a' - 10; 79 | else 80 | d += '0'; 81 | serial_putchar(d); 82 | 83 | } 84 | 85 | void serial_print_hex(uint8_t data) 86 | { 87 | serial_putchar(' '); 88 | serial_print_nibble(data >> 4); 89 | serial_print_nibble(data); 90 | } 91 | 92 | void serial_print_hex16(uint16_t data) 93 | { 94 | serial_print_nibble(data >> 12); 95 | serial_print_nibble(data >> 8); 96 | serial_print_nibble(data >> 4); 97 | serial_print_nibble(data); 98 | } 99 | 100 | void serial_dump_block(uint8_t * data, uint16_t len) 101 | { 102 | uint8_t cr = 0; 103 | 104 | while (len--) { 105 | serial_print_hex(*data); 106 | data++; 107 | cr++; 108 | if (cr == 32) { 109 | serial_putchar(0x0a); 110 | cr = 0; 111 | } 112 | } 113 | serial_putchar(' '); 114 | serial_putchar(0x0a); 115 | } 116 | 117 | void serial_print_string(char *c) 118 | { 119 | 120 | while (*c) { 121 | serial_putchar((char)*c); 122 | c++; 123 | } 124 | } 125 | #endif //SERIAL_DEBUG 126 | -------------------------------------------------------------------------------- /src/targets/STM32F10x/serial_debug.h: -------------------------------------------------------------------------------- 1 | /* 2 | serial.h 3 | 4 | This is part of OsEID (Open source Electronic ID) 5 | 6 | Copyright (C) 2017-2024 Peter Popovec, popovec.peter@gmail.com 7 | 8 | This program is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | This program is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . 20 | 21 | this module is used to dump USB traffic to serial port (for debug only) 22 | header file 23 | 24 | Serial output is on PA9 (at the same pin LEDs are connected) 25 | 26 | */ 27 | #ifdef SERIAL_DEBUG 28 | 29 | void serial_init (); 30 | void serial_print_string(char *c); 31 | void serial_dump_block(uint8_t * data, uint16_t len); 32 | void serial_putchar(uint8_t c); 33 | void serial_print_hex(uint8_t data); 34 | void serial_print_hex16(uint16_t data); 35 | #define DEBUG_init() serial_init () 36 | #define DEBUG_print_string(c) serial_print_string(c) 37 | #define DEBUG_dump_block(d,l) serial_dump_block(d,l) 38 | #define DEBUG_putchar(c) serial_putchar(c) 39 | #define DEBUG_print_hex(d) serial_print_hex(d) 40 | #define DEBUG_print_hex16(d) serial_print_hex16(d) 41 | #else 42 | #define DEBUG_init() 43 | #define DEBUG_print_string(c) 44 | #define DEBUG_dump_block(d,l) 45 | #define DEBUG_putchar(c) 46 | #define DEBUG_print_hex(d) 47 | #define DEBUG_print_hex16(d) 48 | #endif 49 | 50 | -------------------------------------------------------------------------------- /src/targets/STM32F10x/usb.h: -------------------------------------------------------------------------------- 1 | /* 2 | usb.h 3 | 4 | This is part of OsEID (Open source Electronic ID) 5 | 6 | Copyright (C) 2024 Peter Popovec, popovec.peter@gmail.com 7 | 8 | This program is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | This program is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . 20 | 21 | USB subsystem for CCID layer - header file (STM32F102 target) 22 | 23 | */ 24 | 25 | // CCID layer uses this call to transmit data to host: 26 | // endpoint 3 - interrupt 27 | // endpoint 2 - CCID to host 28 | uint8_t USB_send_data_to_host(uint8_t ep, uint8_t * data, uint16_t length); 29 | 30 | void USBcommon_Reset(void); 31 | 32 | // This is called by HW driver to check if new address for device is available 33 | uint8_t USBcommon_GetAddress(void); 34 | 35 | // 0 - device unconfigures, any other value = configuration number 36 | // In this project only configuration 1 is used. 37 | uint8_t USBcommon_GetConfiguration(void); 38 | 39 | // This is called after USB hardware is reconfigured to new address 40 | void USBcommon_MarkDeviceAddressed(void); 41 | 42 | uint8_t USB_Get_Stall_state(uint8_t ep); 43 | 44 | void USB_Configure_ep(uint8_t ep); 45 | void USB_Stall_ep(uint8_t ep); 46 | void USB_Configure_CCID_ep(void); 47 | void USB_Deconfigure_CCID_ep(void); 48 | 49 | uint8_t USB_send_data_to_host(uint8_t ep, uint8_t * data, uint16_t length); 50 | // what to do after response: 51 | // OK - nothing 52 | // ZERO_FRAME - return blank frame (length=0) 53 | // RET_STALL - stall RX and TX 54 | #define RET_OK 0 55 | #define RET_ZERO_FRAME 20 56 | #define RET_STALL 100 57 | uint8_t USBcommon_ProcessSetupRequest(uint8_t * usb_buffer, uint8_t rec_len); 58 | -------------------------------------------------------------------------------- /src/targets/atmega128/avr.c: -------------------------------------------------------------------------------- 1 | /* 2 | avr.c 3 | 4 | This is part of OsEID (Open source Electronic ID) 5 | 6 | Copyright (C) 2015-2025 Peter Popovec, popovec.peter@gmail.com 7 | 8 | This program is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | This program is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . 20 | 21 | fuses setting for atmega128 22 | 23 | */ 24 | #include 25 | #include 26 | #include 27 | 28 | #define GCC_VERSION (__GNUC__ * 10000 \ 29 | + __GNUC_MINOR__ * 100 \ 30 | + __GNUC_PATCHLEVEL__) 31 | /* Test for GCC = 4.8.1 */ 32 | #if GCC_VERSION == 40801 33 | #define X_GCC_OK 34 | #warning Only code generated by avr-gcc 4.9.2 is tested on real card 35 | #endif 36 | #if GCC_VERSION == 40902 37 | #define X_GCC_OK 38 | #endif 39 | #if GCC_VERSION == 50400 40 | #define X_GCC_OK 41 | #endif 42 | 43 | #ifndef X_GCC_OK 44 | #warning only AVR GCC version 4.8.1 / 4.9.2 / 5.4.0 are tested to compile this code 45 | #endif 46 | 47 | 48 | FUSES = 49 | { // 50 | // 51 | // enable BOD(4V), SUT 00 = 0ms (6CK), clock source internal RC (8MHz) 52 | // .low = 0x04, // default 0xE1 53 | // enable BOD(4V), SUT 01 = 4.1ms (6CK), clock source internal RC (8MHz) 54 | .low = 0x14, 55 | // disable JTAG 56 | .high = 0xD9, // default 0x99 57 | // 58 | // no watchdogm no atmega103 comp. mode 59 | .extended = 0xFF, // default 0xFD 60 | }; 61 | 62 | //LOCKBITS = (LB_MODE_1 & BLB0_MODE_1 & BLB1_MODE_1); 63 | LOCKBITS = (LB_MODE_3); 64 | -------------------------------------------------------------------------------- /src/targets/atmega128/constants.c: -------------------------------------------------------------------------------- 1 | /* 2 | constants.c 3 | 4 | This is part of OsEID (Open source Electronic ID) 5 | 6 | Copyright (C) 2015-2019 Peter Popovec, popovec.peter@gmail.com 7 | 8 | This program is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | This program is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . 20 | 21 | definition of constant values for eliptic curves, oids etc 22 | 23 | atmega128/1284 helper for read constants from eeprom or flash 24 | now default is EEPROM for atmega 25 | 4kB eeprom, 0-0x7ff - constanst, 0x800-0xfff PINs (from mem_device.S) 26 | (xmega uses flash to store constants) 27 | 28 | */ 29 | #ifdef HAVE_GET_CONSTANTS 30 | #ifndef __FLASH 31 | #error, Your compiler does not support Named Address Space 32 | #endif 33 | #include 34 | #include "constants.h" 35 | 36 | #if 1 37 | const uint8_t __attribute__ ((section (".eeprom"))) eeprom_constants[] = { 38 | C_CONSTANTS 39 | 0xff 40 | }; 41 | 42 | uint8_t 43 | get_constant (void *here, uint8_t id) 44 | { 45 | register uint8_t ret asm ("r24"); 46 | asm volatile ( // 47 | "ldi r30,0\n" // 48 | "ldi r31,0\n" // 49 | "clr r24\n" // 50 | "1:\n" // 51 | "out 0x1f,r31\n" // set address 52 | "out 0x1e,r30\n" // 53 | "sbi 0x1c,0\n" // trigger read 54 | "adiw r30,1\n" // inc addr 55 | "in r25,0x1d\n" // 56 | // 57 | "out 0x1f,r31\n" // set address 58 | "out 0x1e,r30\n" // 59 | "sbi 0x1c,0\n" // trigger read 60 | "adiw r30,1\n" // inc addr 61 | "in r0,0x1d\n" // 62 | // 63 | "cp r25,r22\n" // match constant ID? 64 | "breq 1f\n" // ok found 65 | "cpi r25,0xff\n" 66 | "breq 3f\n" // fail, no constant found 67 | "in r0,0x1d\n" // 68 | "add r30,r0\n" // 69 | "adc r31,r1\n" // 70 | "rjmp 1b\n" // 71 | // copy 72 | // 73 | "1:\n" 74 | "out 0x1f,r31\n" // set address 75 | "out 0x1e,r30\n" // 76 | "sbi 0x1c,0\n" // trigger read 77 | "adiw r30,1\n" // inc addr 78 | "in r25,0x1d\n" // 79 | "st X+,r25\n" 80 | "dec r0\n" 81 | "brne 1b\n" 82 | // 83 | "2:\n" 84 | "ldi r24,1\n" // 85 | "3:\n" 86 | :"=r" (ret):"x" (here):); 87 | return ret; 88 | } 89 | 90 | 91 | 92 | 93 | #else 94 | 95 | const uint8_t __flash constants[] = { 96 | C_CONSTANTS 97 | //last position 98 | 0xff 99 | }; 100 | 101 | // C code with pgm_read_byte 64 bytes 102 | // C code with named space 56 bytes 103 | // ASM code 40 bytes 104 | 105 | #if 0 106 | uint8_t 107 | get_constant (void *here, uint8_t id) 108 | { 109 | uint8_t *t; 110 | const __flash uint8_t *s = constants; 111 | uint8_t size, val; 112 | 113 | t = (uint8_t *) here; 114 | 115 | for (;;) 116 | { 117 | val = *s++; //pgm_read_byte (s); 118 | if (val == 0xff) 119 | return 0; 120 | size = *s++; //pgm_read_byte (s); 121 | if (val == id) 122 | { 123 | while (size) 124 | { 125 | *t = *s; //pgm_read_byte (s); 126 | t++; 127 | s++; 128 | size--; 129 | } 130 | return 1; 131 | } 132 | s += size; 133 | } 134 | } 135 | #else 136 | uint8_t 137 | get_constant (void *here, uint8_t id) 138 | { 139 | register uint8_t ret asm ("r24"); 140 | asm volatile ( // 141 | "1:\n" // 142 | "lpm r21,Z+\n" // load constant ID 143 | "cpi r21,0xff\n" // 144 | // test last constant ? 145 | "brne 2f\n" // 146 | "clr r24\n" // 147 | "ret \n" // 148 | "2:\n" // 149 | "lpm r20,Z+\n" // load constant size 150 | "cp r21,r22\n" // compare ID (r22 = id)? 151 | "breq 3f\n" // 152 | // jump to next constant 153 | "add r30,r20\n" // 154 | "adc r31,r1\n" // 155 | "rjmp 1b\n" // 156 | // ok, this constant to RAM 157 | "3:\n" // 158 | "lpm r0,Z+\n" // 159 | "st X+,r0\n" // 160 | "dec r20\n" // 161 | "brne 3b\n" // 162 | "ldi r24,1\n" // 163 | :"=r" (ret):"z" (constants), "x" (here):); 164 | return ret; 165 | } 166 | #endif // C/ASM 167 | 168 | #endif // EEPROM/FLASH 169 | #endif // have get constants 170 | -------------------------------------------------------------------------------- /src/targets/atmega128/rnd.c: -------------------------------------------------------------------------------- 1 | /* 2 | rnd.c 3 | 4 | This is part of OsEID (Open source Electronic ID) 5 | 6 | Copyright (C) 2015, 2017-2019 Peter Popovec, popovec.peter@gmail.com 7 | 8 | This program is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | This program is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . 20 | 21 | Atmega128 random generator 22 | 23 | */ 24 | #include 25 | #include 26 | #include 27 | #include "des.h" 28 | #include "rnd.h" 29 | 30 | void 31 | rnd_init (void) 32 | { 33 | // ADC conversion channel: 34 | ADMUX = 0x5E; //reference AVcc, measuring internal 1.1V bandgap, ADLAR=0 35 | 36 | // Initialize ADC, clock /64 ( RC oscilator run at ~13.5 MHz, /64 = ~210kHz 37 | ADCSRA = (1 << ADEN) | (1 << ADSC) | (1 << ADFR) | (1 << ADPS2) | (1 << ADPS1); // | (1 << ADPS0); 38 | } 39 | 40 | static void 41 | rnd_get_16 (uint8_t * r) 42 | { 43 | uint8_t i, d, j; 44 | 45 | for (j = 0; j < 16; j++) 46 | { 47 | for (i = 0; i < 8; i++) 48 | { 49 | #pragma GCC diagnostic ignored "-Wuninitialized" 50 | #pragma GCC diagnostic ignored "-Wmaybe-uninitialized" 51 | d <<= 1; 52 | d |= ADCL & 1; 53 | ADCH; // ADC data register is not updated until ADCH is read! 54 | ADCSRA |= (1 << ADIF); // clear interrupt flag 55 | while (!(ADCSRA & (1 << ADIF))); // wait for conversion end 56 | } 57 | *r = d; 58 | r++; 59 | } 60 | } 61 | 62 | // size 0 -> 256 bytes 63 | void 64 | rnd_get (uint8_t * r, uint8_t size) 65 | { 66 | uint8_t data[16], *d; 67 | uint8_t k = 0; 68 | 69 | do 70 | { 71 | if (k == 0) 72 | { 73 | rnd_get_16 (data); 74 | des_run (data, data + 8, 0); 75 | d = data; 76 | k = 8; 77 | } 78 | *r = *d ^ *(d + 8); 79 | d++; 80 | r++; 81 | k--; 82 | } 83 | while (--size); 84 | } 85 | -------------------------------------------------------------------------------- /src/targets/console/card_io.c: -------------------------------------------------------------------------------- 1 | /* 2 | card_io.c 3 | 4 | This is part of OsEID (Open source Electronic ID) 5 | 6 | Copyright (C) 2015-2023 Peter Popovec, popovec.peter@gmail.com 7 | 8 | This program is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | This program is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . 20 | 21 | 22 | input/output subsystem for OsEID - debug console version 23 | 24 | */ 25 | 26 | #define DEBUG_IFH 27 | #include "debug.h" 28 | 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include "card_io.h" 38 | 39 | uint8_t pps; 40 | 41 | #if defined(T1_TRANSPORT) 42 | #error this code is not designed to use T1 transport 43 | #endif 44 | 45 | #if !defined(TRANSMISSION_PROTOCOL_MODE_NEGOTIABLE) 46 | #error no ATR for specific mode 47 | #endif 48 | 49 | void 50 | card_io_init (void) 51 | { 52 | #if defined(PROTOCOL_T0) && defined(PROTOCOL_T1) 53 | fprintf (stdout, "< 3b:f5:18:00:02:80:01:4f:73:45:49:44:1a\n"); 54 | DPRINT ("RESET, sending ATR, protocol reset to T0\n"); 55 | #elif defined(PROTOCOL_T0) 56 | fprintf (stdout, "< 3b:f5:18:00:02:00:4f:73:45:49:44\n"); 57 | DPRINT ("RESET, sending ATR, protocol reset to T0\n"); 58 | #else 59 | fprintf (stdout, "< 3b:d5:96:02:81:31:fe:65:4f:73:45:49:44:1e\n"); 60 | DPRINT ("RESET, sending ATR, protocol reset to T1\n"); 61 | #endif 62 | pps = 0; 63 | } 64 | 65 | uint16_t 66 | card_io_rx (uint8_t * data, uint16_t len) 67 | { 68 | ssize_t l; 69 | uint16_t xlen = len; 70 | char *line = NULL; 71 | size_t ilen = 0; 72 | char *endptr; 73 | long val; 74 | 75 | uint16_t count = 0; 76 | 77 | fflush (stdin); 78 | for (;;) 79 | { 80 | // printf ("> "); 81 | 82 | l = getline (&line, &ilen, stdin); 83 | if (line == NULL) 84 | continue; 85 | 86 | if (l == 4) 87 | { 88 | if (0 == strncmp ("quit", line, 4) 89 | || 0 == strncmp ("QUIT", line, 4)) 90 | exit (0); 91 | if (0 == strncmp ("> D", line, 3)) 92 | { 93 | DPRINT ("Power DOWN\n"); 94 | free (line); 95 | line = NULL; 96 | ilen = 0; 97 | continue; 98 | } 99 | if (0 == strncmp ("> P", line, 3)) 100 | { 101 | DPRINT ("Power UP\n"); 102 | free (line); 103 | line = NULL; 104 | ilen = 0; 105 | fflush (stdin); 106 | //free (line); 107 | raise (SIGINT); 108 | // wait for signal proccess 109 | for (;;); 110 | 111 | // continue; 112 | } 113 | if (0 == strncmp ("> R", line, 3)) 114 | { 115 | DPRINT ("RESET\n"); 116 | free (line); 117 | line = NULL; 118 | ilen = 0; 119 | fflush (stdin); 120 | //free (line); 121 | raise (SIGINT); 122 | // wait for signal proccess 123 | for (;;); 124 | 125 | // continue; 126 | } 127 | if (0 == strncmp ("> 0", line, 3)) 128 | { 129 | // TODO PTS allowed only after ATR 130 | DPRINT ("New protocol T0\n"); 131 | free (line); 132 | line = NULL; 133 | ilen = 0; 134 | // fprintf (stdout, "< 0\n"); 135 | // generate PPS frame 136 | data[0] = CARD_IO_PPS; 137 | data[1] = 3; 138 | data[2] = 0xff; 139 | data[3] = 0; 140 | data[4] = 0xff; 141 | pps = 1; 142 | return 0; // PPS frame 143 | } 144 | if (0 == strncmp ("> 1", line, 3)) 145 | { 146 | // TODO PTS allowed only after ATR 147 | DPRINT ("New protocol T1\n"); 148 | free (line); 149 | line = NULL; 150 | ilen = 0; 151 | // fprintf (stdout, "< 1\n"); 152 | // generate PPS frame 153 | data[0] = CARD_IO_PPS; 154 | data[1] = 3; 155 | data[2] = 0xff; 156 | data[3] = 1; 157 | data[4] = 0xfe; 158 | pps = 1; 159 | return 0; // PPS frame 160 | } 161 | } 162 | 163 | if (l >= 5) 164 | if (0 == strncmp ("reset", line, 5) 165 | || 0 == strncmp ("RESET", line, 5)) 166 | { 167 | DPRINT ("received reset from reader\n"); 168 | fflush (stdin); 169 | //free (line); 170 | raise (SIGINT); 171 | // wait for signal proccess 172 | for (;;); 173 | } 174 | if (l > 0) 175 | break; 176 | endptr = line; 177 | free (line); 178 | line = NULL; 179 | ilen = 0; 180 | } 181 | DPRINT ("parsing APDU hex string"); 182 | endptr = line + 1; 183 | for (; *endptr && xlen; xlen--) 184 | { 185 | val = strtol (endptr, &endptr, 16); 186 | val &= 0xff; 187 | data[count++] = (uint8_t) val; 188 | while (isspace (*endptr) && *endptr) 189 | endptr++; 190 | } 191 | DPRINT (" %d bytes\n", count); 192 | 193 | free (line); 194 | 195 | return count; 196 | } 197 | 198 | // for len = 0 transmit 65536 bytes 199 | void 200 | card_io_tx (uint8_t * data, uint16_t len) 201 | { 202 | printf ("< "); 203 | // check PPS 204 | if (pps) 205 | { 206 | pps = 0; 207 | printf ("%d\n", data[1]); 208 | return; 209 | } 210 | do 211 | { 212 | printf ("%02x ", *data++); 213 | } 214 | while (--len); 215 | printf ("\n"); 216 | 217 | return; 218 | } 219 | 220 | void 221 | card_io_start_null (void) 222 | { 223 | printf ("card_io_start_null\n"); 224 | 225 | } 226 | -------------------------------------------------------------------------------- /src/targets/console/mem_device.c: -------------------------------------------------------------------------------- 1 | /* 2 | mem_device.c 3 | 4 | This is part of OsEID (Open source Electronic ID) 5 | 6 | Copyright (C) 2015-2023 Peter Popovec, popovec.peter@gmail.com 7 | 8 | This program is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | This program is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . 20 | 21 | mem device driver for filesystem in disc file 22 | 23 | */ 24 | /* *INDENT-OFF* */ 25 | #include 26 | #include "mem_device.h" 27 | 28 | #include 29 | #define DPRINT(msg...) fprintf(stderr,msg) 30 | //#define DPRINT(msg...) 31 | 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include 38 | 39 | // to match simulavr for atmega128 use 64kiB - 256bytes 40 | #define MEMSIZE 65536-256 41 | #if MEMSIZE > 65536 42 | #error filesyste is designed to use max 65536 bytes! 43 | #endif 44 | 45 | static int initialized; 46 | static uint8_t mem[MEMSIZE]; 47 | 48 | #define SECSIZE 1024 49 | static uint8_t sd[SECSIZE]; 50 | 51 | uint8_t change_counter[2]; 52 | 53 | static uint8_t 54 | device_writeback (void) 55 | { 56 | int f; 57 | int size, xsize; 58 | 59 | f = open ("card_mem", O_WRONLY | O_CREAT, S_IWUSR | S_IRUSR); 60 | if (f < 0) 61 | return 1; 62 | 63 | size = MEMSIZE; 64 | while (size) 65 | { 66 | xsize = write (f, mem, size); 67 | if (xsize < 0) 68 | { 69 | close (f); 70 | return 1; 71 | } 72 | size -= xsize; 73 | } 74 | if (SECSIZE != write (f, sd, SECSIZE)) 75 | { 76 | close (f); 77 | return 1; 78 | } 79 | if (sizeof(change_counter) != write (f, &change_counter, sizeof(change_counter))) 80 | { 81 | close (f); 82 | return 1; 83 | } 84 | close (f); 85 | return 0; 86 | } 87 | 88 | static uint8_t 89 | device_init (void) 90 | { 91 | int f; 92 | int size, xsize; 93 | 94 | if (initialized) 95 | return 0; 96 | f = open ("card_mem", O_RDONLY); 97 | if (f < 0) 98 | { 99 | memset (mem, 0xff, MEMSIZE); 100 | memset (sd, 0xff, SECSIZE); 101 | change_counter[0] = 0; 102 | change_counter[1] = 0; 103 | if (device_writeback ()) 104 | return 1; 105 | initialized = 1; 106 | return 0; 107 | } 108 | size = MEMSIZE; 109 | while (size) 110 | { 111 | xsize = read (f, mem, size); 112 | if (xsize < 0) 113 | { 114 | close (f); 115 | return 1; 116 | } 117 | size -= xsize; 118 | } 119 | if (SECSIZE != read (f, sd, SECSIZE)) 120 | { 121 | close (f); 122 | return 1; 123 | } 124 | if (sizeof(change_counter) != read (f, change_counter, sizeof(change_counter))) 125 | { 126 | close (f); 127 | return 1; 128 | } 129 | initialized = 1; 130 | close (f); 131 | return 0; 132 | } 133 | 134 | static void update_change_counter(void){ 135 | int c; 136 | 137 | if(device_init ()) 138 | return; 139 | c = change_counter[0]; 140 | c |= change_counter[1] << 8; 141 | c++; 142 | change_counter[0] = c & 0xff; 143 | change_counter[1] = c >> 8; 144 | } 145 | 146 | uint16_t device_get_change_counter(){ 147 | return change_counter[0] | ((uint16_t)change_counter[1] << 8); 148 | } 149 | 150 | // size 0 is interpreted as 256! 151 | uint8_t 152 | sec_device_read_block (void *buffer, uint16_t offset, uint8_t size) 153 | { 154 | uint16_t overflow, s; 155 | 156 | s = size ? size : 256; 157 | overflow = offset + s - 1; 158 | 159 | if (overflow > SECSIZE) 160 | return 1; 161 | 162 | if (device_init ()) 163 | return 1; 164 | 165 | memcpy (buffer, sd + offset, s); 166 | return 0; 167 | } 168 | 169 | // size 0 is interpreted as 256! 170 | uint8_t 171 | sec_device_write_block (void *buffer, uint16_t offset, uint8_t size) 172 | { 173 | uint16_t overflow, s; 174 | 175 | s = size ? size : 256; 176 | overflow = offset + s - 1; 177 | 178 | if (overflow > SECSIZE) 179 | return 1; 180 | 181 | if (device_init ()) 182 | return 1; 183 | 184 | memcpy (sd + offset, buffer, s); 185 | 186 | if (device_writeback ()) 187 | return 1; 188 | 189 | return 0; 190 | } 191 | 192 | // size 0 is interpreted as 256! 193 | uint8_t 194 | device_read_block (void *buffer, uint16_t offset, uint8_t size) 195 | { 196 | uint32_t overflow, s; 197 | 198 | s = size ? size : 256; 199 | overflow = offset + s - 1; 200 | 201 | if (overflow > MEMSIZE) 202 | return 1; 203 | 204 | if (device_init ()) 205 | return 1; 206 | 207 | memcpy (buffer, mem + offset, s); 208 | 209 | return 0; 210 | } 211 | 212 | // size 0 is interpreted as 256! 213 | uint8_t 214 | device_write_block (void *buffer, uint16_t offset, uint8_t size) 215 | { 216 | uint32_t overflow, s; 217 | 218 | s = size ? size : 256; 219 | overflow = offset + s - 1; 220 | 221 | if (overflow > MEMSIZE) 222 | return 1; 223 | 224 | if (device_init ()) 225 | return 1; 226 | 227 | memcpy (mem + offset, buffer, s); 228 | update_change_counter(); 229 | if (device_writeback ()) 230 | return 1; 231 | 232 | return 0; 233 | } 234 | /* *INDENT-ON* */ 235 | 236 | // fill block at offset _offset_ with value 0xff 237 | uint8_t device_write_ff(uint16_t offset, uint8_t size) 238 | { 239 | uint32_t overflow, s; 240 | 241 | s = size ? size : 256; 242 | overflow = offset + s - 1; 243 | 244 | if (overflow > MEMSIZE) 245 | return 1; 246 | 247 | if (device_init()) 248 | return -1; 249 | 250 | memset(mem + offset, 0xff, s); 251 | update_change_counter(); 252 | 253 | if (device_writeback()) 254 | return -1; 255 | 256 | return 0; 257 | } 258 | 259 | uint8_t device_format() 260 | { 261 | 262 | if (device_init()) 263 | return -1; 264 | memset(mem, 0xff, MEMSIZE); 265 | if (device_writeback()) 266 | return -1; 267 | return 0; 268 | } 269 | 270 | uint8_t sec_device_format() 271 | { 272 | 273 | if (device_init()) 274 | return -1; 275 | 276 | memset(sd, 0xff, SECSIZE); 277 | 278 | if (device_writeback()) 279 | return -1; 280 | return 0; 281 | } 282 | -------------------------------------------------------------------------------- /src/targets/console/pcscd/OsEIDsim/Makefile: -------------------------------------------------------------------------------- 1 | # use a correct default CFLAGS 2 | ifeq ($(CFLAGS),) 3 | CFLAGS = -O2 -g -Wall -fPIC -I. `pkg-config libpcsclite --cflags` 4 | else 5 | CFLAGS += -Wall -fPIC -I. `pkg-config libpcsclite --cflags` 6 | endif 7 | 8 | version="0.0.1" 9 | 10 | targets = hex2bytes.c serial.c ifdhandler.c 11 | 12 | all: libOsEIDsim.so 13 | 14 | libOsEIDsim.so: $(targets) 15 | rm -f $@ 16 | $(CC) -shared $(CFLAGS) $(LDFLAGS) $(targets) -o $@ 17 | chmod -x $@ 18 | mv $@ $@.$(version) 19 | 20 | clean: 21 | rm -f $(targets) 22 | rm -f libOsEIDsim.so* 23 | rm -f Makefile.bak 24 | rm -f config.log 25 | rm -f *~ 26 | 27 | .PHONY: all clean 28 | -------------------------------------------------------------------------------- /src/targets/console/pcscd/OsEIDsim/hex2bytes.c: -------------------------------------------------------------------------------- 1 | /* 2 | hex2bytes.c 3 | 4 | This is part of OsEID (Open source Electronic ID) 5 | 6 | Copyright (C) 2019 Peter Popovec, popovec.peter@gmail.com 7 | 8 | This program is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | This program is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . 20 | 21 | very tolerant hex to binary converter (max 256 bytes) 22 | 23 | */ 24 | #include 25 | #include 26 | int 27 | hex2bytes (char *r_buffer, int already_read, uint8_t * buffer) 28 | { 29 | uint32_t length = 0, i; 30 | int byte = 0; 31 | int flag = 0; 32 | char c; 33 | 34 | for (i = 0;; i++) 35 | { 36 | // allow maximal size of extended APDU 37 | if (length == 5+2+65536+2) 38 | return length; 39 | 40 | if (i == already_read) 41 | goto end; 42 | 43 | c = r_buffer[i]; 44 | if (!isxdigit (c)) 45 | { 46 | end: 47 | if (flag) 48 | { 49 | // no hex digit but some hex digit already parsed 50 | // save byte 51 | buffer[length] = byte; 52 | byte = 0; 53 | flag = 0; 54 | length++; 55 | } 56 | if (i == already_read) 57 | return length; 58 | if (c == 0x0d) 59 | return length; 60 | if (c == 0x0a) 61 | return length; 62 | } 63 | else 64 | { 65 | flag++; 66 | if (flag == 3) 67 | { 68 | // save byte 69 | buffer[length] = byte; 70 | byte = 0; 71 | flag = 0; 72 | length++; 73 | } 74 | c -= '0'; 75 | if (c > 9) 76 | c -= 7; 77 | if (c > 16) 78 | c -= 32; 79 | byte *= 16; 80 | byte += c; 81 | } 82 | } 83 | return length; 84 | } 85 | -------------------------------------------------------------------------------- /src/targets/console/pcscd/OsEIDsim/reader.conf: -------------------------------------------------------------------------------- 1 | FRIENDLYNAME "OsEIDsim" 2 | DEVICENAME /dev/pts/15 3 | LIBPATH /usr/src/OsEID/build/console/OsEIDsim.so.0 4 | CHANNELID 1 5 | -------------------------------------------------------------------------------- /src/targets/console/pcscd/OsEIDsim/run_pcscd.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # 3 | # targets/console/pcscd/OsEIDsim/run_pcscd.sh 4 | # 5 | # This is part of OsEID (Open source Electronic ID) 6 | # 7 | # Copyright (C) 2015-2019 Peter Popovec, popovec.peter@gmail.com 8 | # 9 | # This program is free software: you can redistribute it and/or modify 10 | # it under the terms of the GNU General Public License as published by 11 | # the Free Software Foundation, either version 3 of the License, or 12 | # (at your option) any later version. 13 | # 14 | # This program is distributed in the hope that it will be useful, 15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | # GNU General Public License for more details. 18 | # 19 | # You should have received a copy of the GNU General Public License 20 | # along with this program. If not, see . 21 | # 22 | # connect pcscd daemon to simulavr with OsEID card (generic simualtion, 23 | # not in simulavr) 24 | # 25 | 26 | OsEID_DIR=`pwd` 27 | mkdir -p "${OsEID_DIR}/tmp" 28 | touch "${OsEID_DIR}/tmp/OsEIDsim.socket" 29 | DEV=$1 30 | if [ $? -lt 1 ]; then 31 | socat -d -d pty,link=${OsEID_DIR}/tmp/OsEIDsim.socket,raw,echo=0 "exec:${OsEID_DIR}/build/console/console ...,pty,raw,echo=0" & 32 | DEV="${OsEID_DIR}/tmp/OsEIDsim.socket" 33 | fi 34 | sleep 1 35 | echo 'FRIENDLYNAME "OsEIDsim"' > "${OsEID_DIR}/tmp/reader.conf" 36 | echo 'DEVICENAME '$DEV >> "${OsEID_DIR}/tmp/reader.conf" 37 | echo 'LIBPATH '${OsEID_DIR}/build/console/libOsEIDsim.so.0.0.1 >> "${OsEID_DIR}/tmp/reader.conf" 38 | echo 'CHANNELID 1' >> "${OsEID_DIR}/tmp/reader.conf" 39 | 40 | 41 | debug=0 42 | 43 | if [ x${OsEID_DEBUG} != x ]; then 44 | v=$[${OsEID_DEBUG} + 0 ] 45 | debug=$[$v % 2 ] 46 | fi 47 | 48 | if [ $debug -eq 1 ]; then 49 | /usr/sbin/pcscd -d -f -c "${OsEID_DIR}/tmp/reader.conf" 50 | else 51 | /usr/sbin/pcscd -f -c "${OsEID_DIR}/tmp/reader.conf" 52 | fi 53 | -------------------------------------------------------------------------------- /src/targets/console/pcscd/OsEIDsim/serial.h: -------------------------------------------------------------------------------- 1 | /* 2 | serial.h 3 | 4 | This is part of OsEID (Open source Electronic ID) 5 | 6 | Copyright (C) 2016 Peter Popovec, popovec.peter@gmail.com 7 | 8 | This program is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | This program is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . 20 | 21 | linux serial port I/O for OsEID simulator (header file) 22 | 23 | */ 24 | 25 | #define RET_OK 0 26 | #define RET_FAIL 1 27 | RESPONSECODE OpenGBP (DWORD lun, LPSTR dev_name); 28 | RESPONSECODE WritePort (DWORD lun, DWORD length, unsigned char *Buffer); 29 | RESPONSECODE ReadPort (DWORD lun, unsigned long *length, unsigned char *Buffer); 30 | RESPONSECODE CloseGBP (DWORD lun); 31 | RESPONSECODE OpenPortByName (DWORD lun, LPSTR dev_name); 32 | RESPONSECODE ClosePort (DWORD lun); 33 | RESPONSECODE OpenPort (DWORD lun, DWORD channel); 34 | -------------------------------------------------------------------------------- /src/targets/console/pcscd/Readme: -------------------------------------------------------------------------------- 1 | This target is for testing OsEID code on PC (as simulator) 2 | ========================================================== 3 | 4 | 5 | top level makefile can handle automatic pcscd run with this simulator, 6 | please use: build/console/run_pcscd.sh 7 | 8 | Read next, if you plan to use this system standalone. 9 | 10 | 11 | Compile simulator targed (make -f Makefile.console) 12 | 13 | Standalone simulator test: 14 | -------------------------- 15 | 16 | $ build/console/console 17 | ATR 18 | > 00 a4 00 00 00 19 | select 20 | fs_select_df 21 | fs_get_fci 22 | card_io_stop_null 23 | < 61 1e 24 | > 00 c0 00 00 1e 25 | iso7816_get_response 00 00 1E 26 | < c0 27 | < 6f 1c 81 02 00 40 82 01 38 83 02 3f 00 86 03 11 3f ff 85 02 00 00 8a 01 07 28 | 84 03 f0 4d 46 29 | card_io_stop_null 30 | < 90 00 31 | > 00 a4 00 00 02 32 | select 33 | < a4 34 | > 3f 00 35 | message ID 3f00 36 | fs_select_df 37 | fs_get_fci 38 | card_io_stop_null 39 | < 61 1e 40 | > 00 c0 00 00 1e 41 | iso7816_get_response 00 00 1E 42 | < c0 43 | < 6f 1c 81 02 00 40 82 01 38 83 02 3f 00 86 03 11 3f ff 85 02 00 00 8a 01 07 44 | 84 03 f0 4d 46 45 | card_io_stop_null 46 | < 90 00 47 | > reset 48 | CTRL-C, card reset (type 'quit' to exit) 49 | ATR 50 | > quit 51 | 52 | Connect simulator to serial line 53 | -------------------------------- 54 | socat -d -d /dev/ttyUSB1,b115200,cs8,parenb=0,cstopb=0,clocal=1,raw,echo=1 "exec:build/console/console ...,pty,raw,echo=0" 55 | 56 | 57 | 58 | Connect simulator to pcscd 59 | -------------------------- 60 | expected location of the source files is in the directory /usr/src/OsEID/ 61 | and working directory is set to /usr/src/OsEID/ too. 62 | 63 | 64 | connect virtual OsEID cadr (simultor) to pcscd: 65 | 66 | $ socat -d -d pty,raw,echo=0 "exec:build/console/console ...,pty,raw,echo=0" 67 | 2016/03/10 10:22:42 socat[8855] N PTY is /dev/pts/19 68 | 2016/03/10 10:22:42 socat[8855] N forking off child, using pty for reading and writing 69 | 2016/03/10 10:22:42 socat[8855] N forked off child process 8856 70 | 2016/03/10 10:22:42 socat[8855] N forked off child process 8856 71 | 2016/03/10 10:22:42 socat[8855] N starting data transfer loop with FDs [5,5] and [7,7] 72 | 2016/03/10 10:22:42 socat[8856] N execvp'ing "build/console/console" 73 | RESET 74 | 75 | 76 | notice that whitch device socat uses (/dev/pts/19) 77 | 78 | 79 | edit /usr/src/OsEID/build/console/reader.conf 80 | 81 | ---------------- cut --------------- 82 | FRIENDLYNAME "OsEIDsim" 83 | DEVICENAME /dev/pts/22 84 | LIBPATH /usr/src/OsEID/build/console/OsEIDsim.so.0 85 | CHANNELID 1 86 | ---------------- cut --------------- 87 | 88 | Start pcscd: 89 | 90 | pcscd -d -f -c /usr/src/OsEID/build/console/reader.conf 91 | 92 | 93 | 94 | 95 | Connect pcscd to minicom 96 | ------------------------ 97 | 98 | start socat: 99 | 100 | $ socat -d -d pty,raw,echo=0 pty,raw,echo=0 101 | 2016/03/01 14:17:34 socat[1100] N PTY is /dev/pts/22 102 | 2016/03/01 14:17:34 socat[1100] N PTY is /dev/pts/23 103 | 2016/03/01 14:17:34 socat[1100] N starting data transfer loop with FDs [5,5] 104 | and [7,7] 105 | 106 | notice that whitch devices socat uses (/dev/pts/22,/dev/pts/23) 107 | 108 | start minicom, configure serial port to /dev/pts/23, no soft/hard flow 109 | control. 110 | 111 | 112 | edit /usr/src/OsEID/build/console/reader.conf 113 | 114 | ---------------- cut --------------- 115 | FRIENDLYNAME "OsEIDsim" 116 | DEVICENAME /dev/pts/22 117 | LIBPATH /usr/src/OsEID/build/console/OsEIDsim.so.0 118 | CHANNELID 1 119 | ---------------- cut --------------- 120 | 121 | Start pcscd: 122 | 123 | pcscd -d -f -c /usr/src/OsEID/build/console/reader.conf 124 | 125 | 126 | 127 | -------------------------------------------------------------------------------- /src/targets/console/restart.c: -------------------------------------------------------------------------------- 1 | /* 2 | targets/console/restart.c 3 | 4 | This is part of OsEID (Open source Electronic ID) 5 | 6 | Copyright (C) 2015-2017 Peter Popovec, popovec.peter@gmail.com 7 | 8 | This program is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | This program is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . 20 | 21 | handler - restart of OsEID main 22 | 23 | */ 24 | while (sigsetjmp (JumpBuffer, 1)); 25 | signal (SIGINT, INThandler); 26 | -------------------------------------------------------------------------------- /src/targets/console/restart.h: -------------------------------------------------------------------------------- 1 | /* 2 | targets/console/restart.h 3 | 4 | This is part of OsEID (Open source Electronic ID) 5 | 6 | Copyright (C) 2015-2017 Peter Popovec, popovec.peter@gmail.com 7 | 8 | This program is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | This program is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . 20 | 21 | handler - restart of OsEID main 22 | 23 | */ 24 | #include 25 | #include 26 | #include 27 | 28 | sigjmp_buf JumpBuffer; 29 | void INThandler (int); 30 | 31 | void 32 | INThandler (int sig) 33 | { 34 | signal (sig, INThandler); 35 | printf ("CTRL-C, card reset (type 'quit' to exit)\n"); 36 | siglongjmp (JumpBuffer, 1); 37 | } 38 | -------------------------------------------------------------------------------- /src/targets/console/rnd.c: -------------------------------------------------------------------------------- 1 | /* 2 | rnd.c 3 | 4 | This is part of OsEID (Open source Electronic ID) 5 | 6 | Copyright (C) 2015-2019 Peter Popovec, popovec.peter@gmail.com 7 | 8 | This program is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | This program is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . 20 | 21 | // Emulator on linux, get random from /dev/urandom 22 | 23 | */ 24 | #include 25 | #include 26 | #include "rnd.h" 27 | 28 | void 29 | rnd_init (void) 30 | { 31 | } 32 | 33 | void 34 | rnd_get (uint8_t * rnd, uint8_t size) 35 | { 36 | FILE *f; 37 | uint16_t s; 38 | uint16_t xsize = size; 39 | 40 | if (size == 0) 41 | xsize = 256; 42 | 43 | for (;;) 44 | { 45 | f = fopen ("/dev/urandom", "r"); 46 | if (!f) 47 | continue; 48 | 49 | s = fread (rnd, sizeof (uint8_t), xsize, f); 50 | 51 | fclose (f); 52 | 53 | 54 | if (s == xsize) 55 | break; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/targets/simulavr/avr.c: -------------------------------------------------------------------------------- 1 | /* 2 | avr.c 3 | 4 | This is part of OsEID (Open source Electronic ID) 5 | 6 | Copyright (C) 2015-2025 Peter Popovec, popovec.peter@gmail.com 7 | 8 | This program is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | This program is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . 20 | 21 | simulavr target, this is used for debug only. RAM is filled with 22 | pattern to allow detection of untouched bytes (position of minimal SP) 23 | 24 | 25 | */ 26 | #include 27 | #include 28 | #include 29 | 30 | #define GCC_VERSION (__GNUC__ * 10000 \ 31 | + __GNUC_MINOR__ * 100 \ 32 | + __GNUC_PATCHLEVEL__) 33 | /* Test for GCC = 4.8.1 */ 34 | #if GCC_VERSION == 40801 35 | #define X_GCC_OK 36 | #endif 37 | #if GCC_VERSION == 40902 38 | #define X_GCC_OK 39 | #endif 40 | #if GCC_VERSION == 50400 41 | #define X_GCC_OK 42 | #endif 43 | 44 | #if GCC_VERSION == 50400 45 | #define X_GCC_OK 46 | #endif 47 | 48 | #ifndef X_GCC_OK 49 | #warning only AVR GCC version 4.8.1 / 4.9.2 / 5.4.0 are tested to compile this code 50 | #endif 51 | 52 | 53 | void fill_ram (void) __attribute__ ((naked)) 54 | __attribute__ ((section (".init1"))); 55 | void 56 | fill_ram (void) 57 | { 58 | uint8_t *address; 59 | for (address = (uint8_t *)0x100; address < (uint8_t *)(16384 + 0x100); address++) 60 | *address = (uint8_t) ((uint16_t) address & 0xff); 61 | } 62 | 63 | 64 | /* 65 | FUSES = 66 | { // 67 | // 68 | // enable BOD(4V), SUT 00 = 0ms (6CK), clock source internal RC (8MHz) 69 | // .low = 0x04, // default 0xE1 70 | // enable BOD(4V), SUT 01 = 4.1ms (6CK), clock source internal RC (8MHz) 71 | .low = 0x14, 72 | // disable JTAG 73 | .high = 0xD9, // default 0x99 74 | // 75 | // no watchdogm no atmega103 comp. mode 76 | .extended = 0xFF, // default 0xFD 77 | }; 78 | 79 | //LOCKBITS = (LB_MODE_1 & BLB0_MODE_1 & BLB1_MODE_1); 80 | LOCKBITS = (LB_MODE_3); 81 | */ 82 | -------------------------------------------------------------------------------- /src/targets/simulavr/card_io.S: -------------------------------------------------------------------------------- 1 | /* 2 | card_io.S - simulavr version 3 | 4 | This is part of OsEID (Open source Electronic ID) 5 | 6 | Copyright (C) 2015-2023 Peter Popovec, popovec.peter@gmail.com 7 | 8 | This program is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | This program is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . 20 | 21 | simulavr connector for OsEID 22 | 23 | */ 24 | #include "card_io.h" 25 | 26 | #if defined(T1_TRANSPORT) 27 | #error this code is not designed to use T1 transport 28 | #endif 29 | 30 | #if !defined(TRANSMISSION_PROTOCOL_MODE_NEGOTIABLE) 31 | #error simulavr allow only TRANSMISSION_PROTOCOL_MODE_NEGOTIABLE 32 | #endif 33 | 34 | #if !defined(PROTOCOL_T0) 35 | #error no protocol T0 36 | #endif 37 | 38 | #if !defined(PROTOCOL_T1) 39 | #error no protocol T1 40 | #endif 41 | 42 | 43 | .global card_io_tx 44 | .type card_io_tx, @function 45 | 46 | card_io_tx: 47 | movw r30,r24 48 | // test if this is PPS response 49 | lds r24,card_io_pps 50 | tst r24 51 | breq 1f 52 | // drop PPS response (negotiation is handles in patched simulavr) 53 | ret 54 | 1: 55 | movw r26,r22 56 | 57 | ldi r24,0 58 | sts 0xff,r24 //reset output fifo 59 | // insert data into buffer (up to 261 bytes..) 60 | card_io_tx_loop: 61 | ld r24,Z+ 62 | sts 0xfe,r24 63 | sbiw r26,1 64 | brne card_io_tx_loop 65 | 66 | ldi r24,1 67 | sts 0xff,r24 //print fifo 68 | ret 69 | 70 | .global card_io_rx 71 | .type card_io_rx, @function 72 | 73 | card_io_rx: 74 | movw r30,r24 75 | movw r26,r22 // length 76 | // clear counter 77 | clr r24 78 | clr r25 79 | 80 | // test, if PPS was generated in previous run 81 | lds r21,card_io_pps 82 | sts card_io_pps,r1 83 | tst r21 84 | brne 1f 85 | 86 | // yes, PPS was sent to card, there is message in buffer 87 | // skip buffer clear and read new message... 88 | ldi r22,2 89 | // next instruction waits until input is available 90 | sts 0xff,r22 // read line 91 | 1: 92 | 93 | // read protocol 94 | lds r23,0xff 95 | lds r21,card_io_proto 96 | cp r23,r21 97 | breq card_io_rx_loop 98 | // protocol change.. generate PPS 99 | sts card_io_proto,r23 100 | // set flag, PPS generated 101 | sts card_io_pps,r23 102 | // PPS is signalized by len = 0 and CARD_IO_PPS (0xff) at 1st position in buffer 103 | // 2nd position is length of PPS message 104 | ldi r24,0 105 | ldi r21,CARD_IO_PPS 106 | std Z+0,r21 107 | ldi r21,3 108 | std Z+1,r21 // length 109 | ldi r21,0xff 110 | std Z+2,r21 // PPSS 111 | andi r23,1 112 | std Z+3,r23 // PPS0 113 | eor r23,r21 114 | std Z+4,r23 // PCK 115 | ret 116 | 117 | card_io_rx_loop: 118 | lds r23,0xff 119 | tst r23 120 | breq 1f // no more characters, skip to end 121 | 122 | // in r23 0xF0 or 0xF1 signalize us protocol 123 | // read char 124 | lds r0,0xfe 125 | st z+,r0 126 | adiw r24,1 127 | sbiw r26,1 128 | brne card_io_rx_loop 129 | // buffer overrun 130 | 1: 131 | ret 132 | 133 | .global card_io_start_null 134 | .type card_io_start_null, @function 135 | 136 | card_io_start_null: 137 | ret 138 | 139 | .global card_io_init 140 | .type card_io_init, @function 141 | 142 | card_io_init: 143 | // ATR is managed in patched simulavr... 144 | sts card_io_proto,r1 145 | sts card_io_pps,r1 146 | 147 | ret 148 | 149 | .section .noinit,"aw",@nobits 150 | card_io_proto: 151 | .skip 1 152 | card_io_pps: 153 | .skip 1 154 | -------------------------------------------------------------------------------- /src/targets/simulavr/constants.c: -------------------------------------------------------------------------------- 1 | /* 2 | constants.c 3 | 4 | This is part of OsEID (Open source Electronic ID) 5 | 6 | Copyright (C) 2015-2019 Peter Popovec, popovec.peter@gmail.com 7 | 8 | This program is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | This program is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . 20 | 21 | definition of constant values for eliptic curves, oids etc 22 | 23 | atmega128/1284 helper for read constants from eeprom or flash 24 | now default is EEPROM for atmega 25 | 4kB eeprom, 0-0x7ff - constanst, 0x800-0xfff PINs (from mem_device.S) 26 | (xmega uses flash to store constants) 27 | 28 | */ 29 | #ifdef HAVE_GET_CONSTANTS 30 | #ifndef __FLASH 31 | #error, Your compiler does not support Named Address Space 32 | #endif 33 | #include 34 | #include "constants.h" 35 | 36 | #if 1 37 | const uint8_t __attribute__ ((section (".eeprom"))) eeprom_constants[] = { 38 | C_CONSTANTS 39 | 0xff, 40 | [2048 ... 4095] = 0xff 41 | }; 42 | 43 | uint8_t 44 | get_constant (void *here, uint8_t id) 45 | { 46 | register uint8_t ret asm ("r24"); 47 | asm volatile ( // 48 | "ldi r30,0\n" // 49 | "ldi r31,0\n" // 50 | "clr r24\n" // 51 | "1:\n" // 52 | "out 0x1f,r31\n" // set address 53 | "out 0x1e,r30\n" // 54 | "sbi 0x1c,0\n" // trigger read 55 | "adiw r30,1\n" // inc addr 56 | "in r25,0x1d\n" // 57 | // 58 | "out 0x1f,r31\n" // set address 59 | "out 0x1e,r30\n" // 60 | "sbi 0x1c,0\n" // trigger read 61 | "adiw r30,1\n" // inc addr 62 | "in r0,0x1d\n" // 63 | // 64 | "cp r25,r22\n" // match constant ID? 65 | "breq 1f\n" // ok found 66 | "cpi r25,0xff\n" 67 | "breq 3f\n" // fail, no constant found 68 | "in r0,0x1d\n" // 69 | "add r30,r0\n" // 70 | "adc r31,r1\n" // 71 | "rjmp 1b\n" // 72 | // copy 73 | // 74 | "1:\n" 75 | "out 0x1f,r31\n" // set address 76 | "out 0x1e,r30\n" // 77 | "sbi 0x1c,0\n" // trigger read 78 | "adiw r30,1\n" // inc addr 79 | "in r25,0x1d\n" // 80 | "st X+,r25\n" 81 | "dec r0\n" 82 | "brne 1b\n" 83 | // 84 | "2:\n" 85 | "ldi r24,1\n" // 86 | "3:\n" 87 | :"=r" (ret):"x" (here):); 88 | return ret; 89 | } 90 | 91 | 92 | 93 | 94 | #else 95 | 96 | const uint8_t __flash constants[] = { 97 | C_CONSTANTS 98 | //last position 99 | 0xff 100 | }; 101 | 102 | // C code with pgm_read_byte 64 bytes 103 | // C code with named space 56 bytes 104 | // ASM code 40 bytes 105 | 106 | #if 0 107 | uint8_t 108 | get_constant (void *here, uint8_t id) 109 | { 110 | uint8_t *t; 111 | const __flash uint8_t *s = constants; 112 | uint8_t size, val; 113 | 114 | t = (uint8_t *) here; 115 | 116 | for (;;) 117 | { 118 | val = *s++; //pgm_read_byte (s); 119 | if (val == 0xff) 120 | return 0; 121 | size = *s++; //pgm_read_byte (s); 122 | if (val == id) 123 | { 124 | while (size) 125 | { 126 | *t = *s; //pgm_read_byte (s); 127 | t++; 128 | s++; 129 | size--; 130 | } 131 | return 1; 132 | } 133 | s += size; 134 | } 135 | } 136 | #else 137 | uint8_t 138 | get_constant (void *here, uint8_t id) 139 | { 140 | register uint8_t ret asm ("r24"); 141 | asm volatile ( // 142 | "1:\n" // 143 | "lpm r21,Z+\n" // load constant ID 144 | "cpi r21,0xff\n" // 145 | // test last constant ? 146 | "brne 2f\n" // 147 | "clr r24\n" // 148 | "ret \n" // 149 | "2:\n" // 150 | "lpm r20,Z+\n" // load constant size 151 | "cp r21,r22\n" // compare ID (r22 = id)? 152 | "breq 3f\n" // 153 | // jump to next constant 154 | "add r30,r20\n" // 155 | "adc r31,r1\n" // 156 | "rjmp 1b\n" // 157 | // ok, this constant to RAM 158 | "3:\n" // 159 | "lpm r0,Z+\n" // 160 | "st X+,r0\n" // 161 | "dec r20\n" // 162 | "brne 3b\n" // 163 | "ldi r24,1\n" // 164 | :"=r" (ret):"z" (constants), "x" (here):); 165 | return ret; 166 | } 167 | #endif // C/ASM 168 | 169 | #endif // EEPROM/FLASH 170 | #endif // have get constants 171 | -------------------------------------------------------------------------------- /src/targets/simulavr/gdb_macros: -------------------------------------------------------------------------------- 1 | define avr_break 2 | break *(void(*)()) $arg0 3 | end 4 | 5 | define avr_diss 6 | disassemble (void(*)()) $arg0,(void(*)()) 0xffff 7 | end 8 | 9 | -------------------------------------------------------------------------------- /src/targets/simulavr/rnd.c: -------------------------------------------------------------------------------- 1 | /* 2 | rnd.c 3 | 4 | This is part of OsEID (Open source Electronic ID) 5 | 6 | Copyright (C) 2017-2019 Peter Popovec, popovec.peter@gmail.com 7 | 8 | This program is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | This program is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . 20 | 21 | simulavr random generator (need patched simulavr!) 22 | 23 | */ 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include "rnd.h" 29 | 30 | 31 | void 32 | rnd_init (void) 33 | { 34 | } 35 | 36 | static void 37 | rnd_setup () 38 | { 39 | asm volatile ( // 40 | "ldi r24,3\n" // RND function 41 | "sts 0xff,r24\n" // OsEID fifo function 42 | ); 43 | } 44 | 45 | static uint8_t 46 | get_rnd () 47 | { 48 | register uint8_t ret asm ("r24"); 49 | asm volatile ( // 50 | "lds r24,0xfe\n" // OsEID fifo 51 | :"=r" (ret)); 52 | return ret; 53 | 54 | } 55 | 56 | void 57 | rnd_get (uint8_t * r, uint8_t size) 58 | { 59 | while (size--) 60 | { 61 | rnd_setup (); 62 | *r = get_rnd (); 63 | r++; 64 | } 65 | } 66 | 67 | void 68 | rnd_stop () 69 | { 70 | } 71 | -------------------------------------------------------------------------------- /src/targets/simulavr/run_pcscd.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # 3 | # targets/simulavr/run_pcscd.sh 4 | # 5 | # This is part of OsEID (Open source Electronic ID) 6 | # 7 | # Copyright (C) 2015,2019 Peter Popovec, popovec.peter@gmail.com 8 | # 9 | # This program is free software: you can redistribute it and/or modify 10 | # it under the terms of the GNU General Public License as published by 11 | # the Free Software Foundation, either version 3 of the License, or 12 | # (at your option) any later version. 13 | # 14 | # This program is distributed in the hope that it will be useful, 15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | # GNU General Public License for more details. 18 | # 19 | # You should have received a copy of the GNU General Public License 20 | # along with this program. If not, see . 21 | # 22 | # connect pcscd daemon to simulavr with OsEID card 23 | # 24 | 25 | GDB=0 26 | 27 | echo $# 28 | if [ $# -ge 1 ]; then 29 | if [ $1 == "gdb" ]; then 30 | GDB=1 31 | fi 32 | fi 33 | 34 | if [ `id -u` -ne 0 ]; then 35 | echo "Sorry, this work only for root user" 36 | exit 1 37 | fi 38 | 39 | 40 | OsEID_DIR=`pwd` 41 | 42 | mkdir -p "${OsEID_DIR}/tmp" 43 | touch "${OsEID_DIR}/tmp/OsEIDsim.socket" 44 | DEV=$1 45 | if [ $? -lt 1 ]; then 46 | if [ -x "${OsEID_DIR}/targets/simulavr/simulavr-oseid" ]; then 47 | if [ $GDB -eq 0 ]; then 48 | socat -d -d pty,link=${OsEID_DIR}/tmp/OsEIDsim.socket,raw,echo=0 "exec:'${OsEID_DIR}/targets/simulavr/simulavr-oseid -d OsEID128 -e build/simulavr/card.eeprom.bin build/simulavr/card.bin',pty,raw,echo=0" & 49 | else 50 | socat -d -d pty,link=${OsEID_DIR}/tmp/OsEIDsim.socket,raw,echo=0 "exec:'${OsEID_DIR}/targets/simulavr/simulavr-oseid -g -d OsEID128',pty,raw,echo=0" & 51 | fi 52 | else 53 | which simulavr-oseid 54 | if [ $? -ne 0 ]; then 55 | echo "Unable to execute simulavr-oseid, please read targets/simulavr/Readme" 56 | exit 1 57 | fi 58 | if [ $GDB -eq 0 ]; then 59 | socat -d -d pty,link=${OsEID_DIR}/tmp/OsEIDsim.socket,raw,echo=0 "exec:'simulavr-oseid -d OsEID128 -e build/simulavr/card.eeprom.bin build/simulavr/card.bin',pty,raw,echo=0" & 60 | else 61 | socat -d -d pty,link=${OsEID_DIR}/tmp/OsEIDsim.socket,raw,echo=0 "exec:'simulavr-oseid -g -d OsEID128',pty,raw,echo=0" & 62 | fi 63 | fi 64 | DEV="${OsEID_DIR}/tmp/OsEIDsim.socket" 65 | fi 66 | sleep 1 67 | echo 'FRIENDLYNAME "OsEIDsim"' > "${OsEID_DIR}/tmp/reader.conf" 68 | echo 'DEVICENAME '$DEV >> "${OsEID_DIR}/tmp/reader.conf" 69 | echo 'LIBPATH '${OsEID_DIR}/build/console/libOsEIDsim.so.0.0.1 >> "${OsEID_DIR}/tmp/reader.conf" 70 | echo 'CHANNELID 1' >> "${OsEID_DIR}/tmp/reader.conf" 71 | 72 | /usr/sbin/pcscd -d -f -c "${OsEID_DIR}/tmp/reader.conf" 73 | -------------------------------------------------------------------------------- /src/targets/xmega128a4u/LED.h: -------------------------------------------------------------------------------- 1 | /* 2 | LED.h 3 | 4 | This is part of OsEID (Open source Electronic ID) 5 | 6 | Copyright (C) 2015-2017 Peter Popovec, popovec.peter@gmail.com 7 | 8 | This program is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | This program is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . 20 | 21 | Macro definition for LEDs 22 | 23 | */ 24 | 25 | // signalize reader state 26 | // WAIT = reader wait for command from host 27 | // BUSY = reader toggles LED (waiting wor card, null byt sending) 28 | // RUN = command parsing/response generating 29 | 30 | 31 | #if 1 32 | // led is on on idle, of on run 33 | #define LED1_INIT() PORTA.PIN6CTRL = PORT_OPC_TOTEM_gc; PORTA.DIRSET = PIN6_bm 34 | #define LED1_IDLE() PORTA.OUTSET = PIN6_bm 35 | #define LED1_BUSY() PORTA.OUTTGL = PIN6_bm 36 | #define LED1_RUN() PORTA.OUTCLR = PIN6_bm 37 | #define LED1_OFF() PORTA.OUTCLR = PIN6_bm 38 | #else 39 | #define LED1_INIT() 40 | #define LED1_IDLE() 41 | #define LED1_BUSY() 42 | #define LED1_RUN() 43 | #define LED1_OFF() 44 | #endif 45 | 46 | // signalize suspend/online 47 | #if 1 48 | #define LED2_INIT() PORTA.PIN5CTRL = PORT_OPC_TOTEM_gc; PORTA.DIRSET = PIN5_bm 49 | #define LED2_RUN() PORTA.OUTSET = PIN5_bm 50 | #define LED2_SUSPEND() PORTA.OUTCLR = PIN5_bm 51 | #else 52 | #define LED2_INIT() 53 | #define LED2_SUSPEND() 54 | #define LED2_RUN() 55 | #endif 56 | -------------------------------------------------------------------------------- /src/targets/xmega128a4u/avr_os.h: -------------------------------------------------------------------------------- 1 | /* 2 | avr_os.h 3 | 4 | This is part of OsEID (Open source Electronic ID) 5 | 6 | Copyright (C) 2015-2017 Peter Popovec, popovec.peter@gmail.com 7 | 8 | This program is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | This program is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . 20 | 21 | CPU initialization and fuses setting for xmega128a4u 22 | Special: this part of code is responsible for restart of main 23 | (main must be restarted if card is powered down or on USB suspend etc.) 24 | 25 | */ 26 | 27 | // this can be called from high/middle level irq or main 28 | void CPU_do_sleep (); 29 | void CPU_do_restart_main (); 30 | // turn CPU into IDLE sleep mode, interrupt is enabled 31 | void CPU_idle (); 32 | -------------------------------------------------------------------------------- /src/targets/xmega128a4u/ccid.h: -------------------------------------------------------------------------------- 1 | /* 2 | ccid.h 3 | 4 | This is part of OsEID (Open source Electronic ID) 5 | 6 | Copyright (C) 2015-2017 Peter Popovec, popovec.peter@gmail.com 7 | 8 | This program is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | This program is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . 20 | 21 | CCID subsystem, header file 22 | 23 | */ 24 | int8_t parse_command (uint8_t * command, uint16_t count, uint8_t * response); 25 | void CCID_Init (); 26 | -------------------------------------------------------------------------------- /src/targets/xmega128a4u/constants.c: -------------------------------------------------------------------------------- 1 | /* 2 | constants.c 3 | 4 | This is part of OsEID (Open source Electronic ID) 5 | 6 | Copyright (C) 2015 Peter Popovec, popovec.peter@gmail.com 7 | 8 | This program is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | This program is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . 20 | 21 | definition of constant values for eliptic curves, oids etc 22 | xmega helper 23 | 24 | */ 25 | #ifdef HAVE_GET_CONSTANTS 26 | #include 27 | #include 28 | #include "constants.h" 29 | 30 | /* *INDENT-OFF* */ 31 | const uint8_t constants[] PROGMEM = { 32 | C_CONSTANTS 33 | //last position 34 | 0xff 35 | }; 36 | /* *INDENT-ON* */ 37 | 38 | uint8_t 39 | get_constant (void *here, uint8_t id) 40 | { 41 | uint8_t *t; 42 | uint8_t *s = (uint8_t *) constants; 43 | uint8_t size, val; 44 | 45 | t = (uint8_t *) here; 46 | 47 | for (;;) 48 | { 49 | val = pgm_read_byte (s); 50 | if (val == 0xff) 51 | return 0; 52 | s++; 53 | size = pgm_read_byte (s); 54 | s++; 55 | if (val == id) 56 | { 57 | while (size) 58 | { 59 | *t = pgm_read_byte (s); 60 | t++; 61 | s++; 62 | size--; 63 | } 64 | return 1; 65 | } 66 | s += size; 67 | } 68 | } 69 | #endif -------------------------------------------------------------------------------- /src/targets/xmega128a4u/rnd.c: -------------------------------------------------------------------------------- 1 | /* 2 | rnd.c 3 | 4 | This is part of OsEID (Open source Electronic ID) 5 | 6 | Copyright (C) 2015-2019 Peter Popovec, popovec.peter@gmail.com 7 | 8 | This program is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | This program is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . 20 | 21 | xmega (TEMP code) 22 | 23 | */ 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include "rnd.h" 30 | //#include 31 | 32 | 33 | static uint8_t 34 | GetCalibration (uint8_t pos) 35 | { 36 | uint8_t ret; 37 | 38 | NVM_CMD = NVM_CMD_READ_CALIB_ROW_gc; 39 | ret = pgm_read_byte (pos); 40 | NVM_CMD = NVM_CMD_NO_OPERATION_gc; 41 | 42 | return ret; 43 | } 44 | 45 | 46 | 47 | 48 | void 49 | rnd_init (void) 50 | { 51 | ADCA.CTRLA = ADC_ENABLE_bm; 52 | ADCA.CALL = GetCalibration (offsetof (NVM_PROD_SIGNATURES_t, ADCACAL0)); 53 | ADCA.CALH = GetCalibration (offsetof (NVM_PROD_SIGNATURES_t, ADCACAL1)); 54 | 55 | ADCA.PRESCALER = ADC_PRESCALER_DIV32_gc; 56 | ADCA.REFCTRL = ADC_REFSEL_INT1V_gc | ADC_TEMPREF_bm; // ADC_REFSEL_INT1V_gc | ADC_BANDGAP_bm | ADC_TEMPREF_bm; 57 | ADCA.CTRLB = ADC_RESOLUTION_12BIT_gc; //ADC_CONVMODE_bm | ADC_FREERUN_bm 58 | ADCA.CH0.CTRL = ADC_CH_INPUTMODE_INTERNAL_gc; // | ADC_CH_GAIN_1X_gc; 59 | ADCA.CH0.MUXCTRL = ADC_CH_MUXINT_TEMP_gc;; 60 | } 61 | 62 | void 63 | rnd_get (uint8_t * r, uint8_t size) 64 | { 65 | uint8_t v; 66 | uint8_t pos = 0, b; 67 | 68 | do 69 | { 70 | v = r[pos]; 71 | for (b = 0; b < 8; b++) 72 | { 73 | //start conversion 74 | ADCA.CH0.CTRL |= ADC_CH_START_bm; 75 | // wait for conversion 76 | while ((ADCA.CH0.INTFLAGS & ADC_CH_CHIF_bm) == 0); 77 | ADCA.CH0.INTFLAGS = ADC_CH_CHIF_bm; 78 | #pragma GCC diagnostic ignored "-Wuninitialized" 79 | v <<= 1; 80 | v |= (ADCA.CH0.RESL & 1); 81 | } 82 | r[pos] ^= v; 83 | pos++; 84 | } 85 | while (--size); 86 | } 87 | -------------------------------------------------------------------------------- /src/targets/xmega128a4u/serial_debug.c: -------------------------------------------------------------------------------- 1 | /* 2 | serial debug module 3 | 4 | This is part of OsEID (Open source Electronic ID) 5 | 6 | Copyright (C) 2017 Peter Popovec, popovec.peter@gmail.com 7 | 8 | This program is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | This program is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . 20 | 21 | this module is used to dump USB trafic to serial port (for debug only) 22 | 23 | */ 24 | #ifdef SERIAL_DEBUG 25 | #define USART USARTC0 26 | #include 27 | #include 28 | 29 | 30 | #include "serial_debug.h" 31 | void 32 | serial_init () 33 | { 34 | #if 0 35 | uint8_t ok = 1; 36 | 37 | // test pin, set as input, test pull up/down, then enable output if all ok 38 | PORTC.DIRCLR = PIN3_bm; 39 | PORTC.PIN3CTRL = (3 << 3); // totem pole pull up 40 | if ((PORTC.IN & 0x08) != 0x08) 41 | ok = 0; 42 | PORTC.PIN3CTRL = (2 << 3); // totem pole pull down 43 | if ((PORTC.IN & 0x08) == 0x08) 44 | ok = 0; 45 | // if all ok, PC3 output 46 | if (ok) 47 | PORTC.DIRSET = PIN3_bm; 48 | #else 49 | // PC3 output 50 | PORTC.DIRSET = PIN3_bm; 51 | #endif 52 | // PC2 input 53 | PORTC.DIRCLR = PIN2_bm; 54 | 55 | USARTC0_CTRLA = 0; // no interrupts from usart 56 | // enable transmiter only.. 57 | USARTC0_CTRLB = USART_TXEN_bm; // | USART_RXEN_bm; 58 | USARTC0_CTRLC = USART_CHSIZE_8BIT_gc; // 8 bit no parity 1 stop bit 59 | #if 0 60 | USART.BAUDCTRLA = 33; 61 | USART.BAUDCTRLB = 0xf0; // 115200 (bsel=33, bscale=-1) 62 | #else 63 | // USART.BAUDCTRLA = 75; 64 | // USART.BAUDCTRLB = 0xa0; //921.6kbit 65 | USART.BAUDCTRLA = 107; //460.8 66 | USART.BAUDCTRLB = 0xb0; 67 | 68 | #endif 69 | } 70 | 71 | void 72 | serial_putchar (uint8_t c) 73 | { 74 | // while ((USART.STATUS & 0x20)!=0x20); 75 | while (!(USARTC0_STATUS & USART_DREIF_bm)); 76 | USART.DATA = c; 77 | } 78 | 79 | static void 80 | serial_print_nibble (uint8_t d) 81 | { 82 | d &= 15; 83 | if (d > 9) 84 | d += 'a' - 10; 85 | else 86 | d += '0'; 87 | serial_putchar (d); 88 | 89 | } 90 | 91 | void 92 | serial_print_hex (uint8_t data) 93 | { 94 | serial_putchar (' '); 95 | serial_print_nibble (data >> 4); 96 | serial_print_nibble (data); 97 | } 98 | 99 | void 100 | serial_dump_block (uint8_t * data, uint16_t len) 101 | { 102 | uint8_t cr = 0; 103 | 104 | while (len--) 105 | { 106 | serial_print_hex (*data); 107 | data++; 108 | cr++; 109 | if (cr == 32) 110 | { 111 | serial_putchar (0x0a); 112 | cr = 0; 113 | } 114 | } 115 | serial_putchar (' '); 116 | serial_putchar (0x0a); 117 | } 118 | 119 | void 120 | serial_print_string (char *c) 121 | { 122 | 123 | while (*c) 124 | { 125 | serial_putchar ((char) *c); 126 | c++; 127 | } 128 | } 129 | #endif //SERIAL_DEBUG 130 | -------------------------------------------------------------------------------- /src/targets/xmega128a4u/serial_debug.h: -------------------------------------------------------------------------------- 1 | /* 2 | serial.h 3 | 4 | This is part of OsEID (Open source Electronic ID) 5 | 6 | Copyright (C) 2017-2021 Peter Popovec, popovec.peter@gmail.com 7 | 8 | This program is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | This program is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . 20 | 21 | this module is used to dump USB trafic to serial port (for debug only) 22 | header file 23 | 24 | */ 25 | #ifdef SERIAL_DEBUG 26 | 27 | void serial_init (); 28 | void serial_print_string (char *c); 29 | void serial_dump_block (uint8_t * data, uint16_t len); 30 | void serial_putchar (uint8_t c); 31 | void serial_print_hex (uint8_t data); 32 | #define DEBUG_init() serial_init () 33 | #define DEBUG_print_string(c) serial_print_string (c) 34 | #define DEBUG_dump_block(d,l) serial_dump_block (d,l) 35 | #define DEBUG_putchar(c) serial_putchar (c) 36 | #define DEBUG_print_hex(d) serial_print_hex (d) 37 | #else 38 | #define DEBUG_init() 39 | #define DEBUG_print_string(c) 40 | #define DEBUG_dump_block(d,l) 41 | #define DEBUG_putchar(c) 42 | #define DEBUG_print_hex (d) 43 | #endif 44 | 45 | -------------------------------------------------------------------------------- /src/targets/xmega128a4u/usb.h: -------------------------------------------------------------------------------- 1 | /* 2 | usb.h 3 | 4 | This is part of OsEID (Open source Electronic ID) 5 | 6 | Copyright (C) 2015-2017 Peter Popovec, popovec.peter@gmail.com 7 | 8 | This program is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | This program is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . 20 | 21 | xmega USB subsystem for CCID layer - header file 22 | 23 | */ 24 | #include 25 | ////////////////////////////////////////////////////////////////////////// 26 | // CCID support 27 | 28 | // CCID part, CCID responses over BULK IN are handled by functions 29 | // isr_CCID_short_message_to_host() and CCID_response_to_host() 30 | // CCID layer normally running in ISR (transaction complete on BULK OUT 31 | // or CONTROL endpoint). If CCID layer can generate response inside this 32 | // ISR and response length is below 64 bytes, response is is synchronously 33 | // sended back by calling isr_CCID_short_message_to_host(). If CCID layer 34 | // receive PC_to_RDR_XfrBlock message: 35 | // 36 | // T0 protocol: message is transferred to card by CCID layer, 37 | // CCID layer then commit response to host, and card software is responsible 38 | // to call CCID_response_to_host() to return data to host. 39 | // CCID layer block any other PC_to_RDR_XfrBlock message to busy slot, 40 | // and slot is unblocked after response is generated. 41 | // 42 | // T1 protocol: message is send to T1 wrapper, T1 wrapper return R frame 43 | // (or any other response if needed). If T1 wrapper can construct full APDU 44 | // for card, CCID layer pass this APDU to card, and if card return response 45 | // T1 wrapper does call CCID_response_to_host() (for 1st 64 bytes of response). 46 | // Rest of response is sended back to host in isr. 47 | 48 | void isr_CCID_short_message_to_host (uint8_t * buffer, uint8_t length); 49 | void CCID_response_to_host (uint8_t * buffer, uint16_t length); 50 | // send CCID message to host with seq number "l_seq" to 51 | // request longer working time 52 | void CCID_start_null (uint8_t l_seq); 53 | //////////////////////////////////////////////////////////////////////////// 54 | 55 | // USB descriptors 56 | ////////////////// 57 | // String descriptor 58 | struct Descriptor_String_t 59 | { 60 | // USB_Descriptor_Header_t Header; 61 | uint8_t bLength; 62 | uint8_t bDescriptorType; 63 | wchar_t UnicodeString[]; 64 | // uint16_t UnicodeString[]; 65 | }; 66 | 67 | // Device descriptor 68 | struct Descriptor_Device_t 69 | { 70 | uint8_t Size; 71 | uint8_t Type; 72 | 73 | uint16_t USBSpecification; 74 | uint8_t Class; 75 | uint8_t SubClass; 76 | uint8_t Protocol; 77 | uint8_t Endpoint0Size; 78 | uint16_t VendorID; 79 | uint16_t ProductID; 80 | uint16_t ReleaseNumber; 81 | uint8_t ManufacturerStrIndex; 82 | uint8_t ProductStrIndex; 83 | uint8_t SerialNumStrIndex; 84 | uint8_t NumberOfConfigurations; 85 | }; 86 | 87 | struct Descriptor_Configuration_Header_t 88 | { 89 | uint8_t bLength; 90 | uint8_t bDescriptorType; 91 | uint16_t wTotalLength; 92 | uint8_t bNumInterfaces; 93 | uint8_t bConfigurationValue; 94 | uint8_t iConfiguration; 95 | uint8_t bmAttributes; 96 | uint8_t bMaxPower; 97 | }; 98 | 99 | struct Descriptor_Interface_t 100 | { 101 | uint8_t bLength; 102 | uint8_t bDescriptorType; 103 | uint8_t bInterfaceNumber; 104 | uint8_t bAlternateSetting; 105 | uint8_t bNumEndpoints; 106 | uint8_t bInterfaceClass; 107 | uint8_t bInterfaceSubClass; 108 | uint8_t bInterfaceProtocol; 109 | uint8_t iInterface; 110 | }; 111 | 112 | struct Descriptor_CCID_Interface_t 113 | { 114 | uint8_t bLength; // 0x36 115 | uint8_t bDescriptorType; // 0x21 116 | uint16_t bcdCCID; // 0x0110 117 | uint8_t bMaxSlotIndex; // 118 | uint8_t bVoltageSupport; 119 | uint32_t dwProtocols; 120 | uint32_t dwDefaultClock; 121 | uint32_t dwMaximumClock; 122 | uint8_t bNumClockSupported; 123 | uint32_t dwDataRate; 124 | uint32_t dwMaxDataRate; 125 | uint8_t bNumDataRatesSupported; 126 | uint32_t dwMaxIFSD; 127 | uint32_t dwSynchProtocols; 128 | uint32_t dwMechanical; 129 | uint32_t dwFeatures; 130 | uint32_t dwMaxCCIDMessageLength; 131 | uint8_t bClassGetResponse; 132 | uint8_t bClassEnvelope; 133 | uint16_t wLcdLayout; 134 | uint8_t bPINSupport; 135 | uint8_t bMaxCCIDBusySlots; 136 | }; 137 | 138 | struct Descriptor_Endpoint_t 139 | { 140 | uint8_t bLength; 141 | uint8_t bDescriptorType; 142 | uint8_t bEndpointAddress; 143 | uint8_t bmAttributes; 144 | uint16_t wMaxPacketSize; 145 | uint8_t bInterval; 146 | }; 147 | 148 | struct USB_Descriptor_Configuration_t 149 | { 150 | struct Descriptor_Configuration_Header_t Config; 151 | struct Descriptor_Interface_t Interface; 152 | struct Descriptor_CCID_Interface_t CCID_Interface; 153 | struct Descriptor_Endpoint_t CCID_bulk_out; 154 | struct Descriptor_Endpoint_t CCID_bulk_in; 155 | struct Descriptor_Endpoint_t CCID_interrupt; 156 | }; 157 | #define CCID_bulk_out_ID 1 158 | #define CCID_bulk_in_ID 2 159 | #define CCID_intr_ID 3 160 | 161 | #define ENDPOINT_DIR_OUT 0x00 162 | #define ENDPOINT_DIR_IN 0x80 163 | 164 | #define CCID_bulk_out_ADDR (ENDPOINT_DIR_OUT | CCID_bulk_out_ID) 165 | #define CCID_bulk_in_ADDR (ENDPOINT_DIR_IN | CCID_bulk_in_ID) 166 | #define CCID_intr_ADDR (ENDPOINT_DIR_IN | CCID_intr_ID) 167 | 168 | // device independent part 169 | 170 | struct USB_Setup_Packet 171 | { 172 | uint8_t bmRequestType; 173 | uint8_t bRequest; 174 | uint16_t wValue; 175 | uint16_t wIndex; 176 | uint16_t wLength; 177 | } __attribute__ ((packed)); 178 | 179 | 180 | void USB_Init (void); 181 | void USB_Reinit (void); 182 | uint16_t USB_Device_GetFrameNumber (void) 183 | __attribute__ ((warn_unused_result)); 184 | 185 | //////////////////////////////////////////////////// 186 | // enumeration and macros for accessing 187 | // device state variable 188 | 189 | // device powered only from bus, 190 | // initial state is then DEVICE_STATE_Powered 191 | // bus reset change state to DEVICE_STATE_Default 192 | // then address is set - DEVICE_STATE_Addressed 193 | // and then configured - DEVICE_STATE_Configured 194 | 195 | // If device is suspended (DEVICE_STATE_Configured), and then resumed, state 196 | // is derived from configuration number and address of device (powered, 197 | // addressed, configured) 198 | 199 | enum USB_Device_States_t 200 | { 201 | DEVICE_STATE_Powered = 0, 202 | DEVICE_STATE_Default, 203 | DEVICE_STATE_Addressed, 204 | DEVICE_STATE_Configured, 205 | DEVICE_STATE_Suspended 206 | }; 207 | extern volatile uint8_t var_DeviceState; 208 | #define V_SetDeviceState(val) var_DeviceState = val 209 | #define V_GetDeviceState() var_DeviceState 210 | //////////////////////////////////////////////////// 211 | 212 | struct __attribute__ ((packed)) USB_EndpointTable_t 213 | { 214 | struct 215 | { 216 | USB_EP_t OUT; 217 | USB_EP_t IN; 218 | } 219 | Endpoints[4]; 220 | uint16_t FrameNum; 221 | }; 222 | 223 | //__attribute__ ((packed)) USB_EndpointTable_t; 224 | --------------------------------------------------------------------------------