forked from django-otp/yubiotp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
code-pico.py
155 lines (114 loc) · 3.13 KB
/
code-pico.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# YubiKey simulator YKSIM for RP2040 boards.
# Uses the local filesystem, an USR button, USB-HID keyboard and RGB NeoPixel LED.
#
# For 'Raspberry' Pico:
# - USR button is at GP15
# - LED is at GP25
import json
DEF = "default"
SFX = ".json"
# READ key+value data from JSON
def read_json_dict(filepath):
with open(filepath, 'r') as file:
data = json.load(file)
return data
# WRITE key+value data to JSON
def write_json_dict(filepath, object):
with open(filepath, 'w') as file:
json.dump(object, file)
#################
# Put "/default.json" into root directory
dfltfile = "/" + DEF + SFX
defaults = read_json_dict(dfltfile)
# Get location of config directory from "default.json": this is usually "/yk-ids".
DIR = defaults["directory"]
cfgfile = DIR + defaults["config"] + defaults["public"] + SFX
ykcfg = read_json_dict(cfgfile)
sesfile = DIR + ykcfg["session"] + ykcfg["public"] + SFX
sesctr = read_json_dict(sesfile)
#################
# These modules are in the "/yubiotp" directory.
from yubiotp.otp import OTP, encode_otp, decode_otp, YubiKey
from yubiotp.modhex import is_modhex, modhex, unmodhex
#################
def update_session(counter):
sesctr["counter"] = str(counter)
try:
write_json_dict(sesfile, sesctr)
except Exception as e:
#print(e)
pass
aeskey = ykcfg["aeskey"].encode('ascii')
private = ykcfg["private"].encode('ascii')
public = ykcfg["public"].encode('ascii')
session = int(sesctr["counter"]) + 1
update_session(session)
#################
usage = 0
YK1 = YubiKey(unmodhex(private), session, usage)
def gen_token():
global session
otp = YK1.generate()
#print(str(otp))
token = encode_otp(otp, unmodhex(aeskey), public)
#print(str(token))
if YK1.session != session:
session = YK1.session
update_session(session)
return token
#################
import usb_hid
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
# init Keyboard
kbd = Keyboard(usb_hid.devices)
keyboard = KeyboardLayoutUS(kbd)
#################
import time
import board
import neopixel
from digitalio import DigitalInOut, Direction, Pull
from adafruit_debouncer import Debouncer
#################
usrbtn = DigitalInOut(board.GP15)
usrbtn.direction = Direction.INPUT
usrbtn.pull = Pull.UP
switch = Debouncer(usrbtn)
def delay(count):
for i in range(count):
time.sleep(0.1)
switch.update()
#pixels = neopixel.NeoPixel(board.RGB, 1, pixel_order=neopixel.RGB)
RED = (99, 0, 0)
GREEN = (0, 99, 0)
BLUE = (0, 0, 99)
OFF = (0, 0, 0)
colors = [RED, GREEN, BLUE]
index = 0
WAIT = 30
LED = DigitalInOut(board.LED)
LED.direction = Direction.OUTPUT
def ledon():
LED.value = True
def ledoff():
LED.value = False
def rgbled(color):
pixels.fill(color)
def do_yksim():
#rgbled(GREEN)
yktok = gen_token()
keyboard.write(str(yktok, 'ascii') + "\n")
#rgbled(RED)
ledoff()
#################
#rgbled(BLUE)
ledon()
delay(WAIT)
while True:
switch.update()
if not switch.value:
#print("pressed")
do_yksim()
delay(WAIT)
#rgbled(BLUE)
ledon()