Created
April 12, 2024 19:00
-
-
Save valtzu/8fd2c3dda2219e5bd5d375832d3ac958 to your computer and use it in GitHub Desktop.
How to load authorized ssh public key from Raspberry Pi 4B bootloader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Include this in the image at /usr/lib/systemd/system-generators/ | |
key=/sys/bus/nvmem/devices/rmem1/nvmem | |
normal_dir=$1 | |
[ -e $key ] || exit 0 | |
n_reverse=$(od -An -N256 -vtx1 -w1 $key | tr -d '\n ') | |
for (( i=0; i<512; i=i+2 )) ; do n="${n_reverse:$i:2}$n" ; done | |
e=$(od -An -j256 -N8 -vtx8 --endian=little $key | tr -d '\n ' | sed ':a;s/^00//;ta') | |
ssh_hex() { | |
for str in "$@" ; do | |
[ $(( 0x${str:0:1} & 0x8 )) == 0 ] || str="00$str" | |
printf "%08X%s" $(( ${#str}/2 )) "${str^^}" | |
done | |
} | |
mkdir -p $normal_dir/systemd-tmpfiles-setup.service.d | |
printf '[Service]\nSetCredential=ssh.authorized_keys.root:ssh-rsa %s\\n\n' $(ssh_hex "7373682D727361" "$e" "$n" | basenc --base16 -i -d | basenc --base64 -w0) > $normal_dir/systemd-tmpfiles-setup.service.d/authorized-keys-from-eeprom.conf |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment