entropy: a simple way to add and save random entropy

This commit is contained in:
Lukas Rusak 2016-02-19 09:49:37 +01:00 committed by chewitt
parent 06cfa30851
commit 52b1ba1991
5 changed files with 101 additions and 0 deletions

View File

@ -0,0 +1,50 @@
################################################################################
# This file is part of OpenELEC - http://www.openelec.tv
# Copyright (C) 2009-2016 Stephan Raue (stephan@openelec.tv)
#
# OpenELEC is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# OpenELEC is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with OpenELEC. If not, see <http://www.gnu.org/licenses/>.
################################################################################
PKG_NAME="entropy"
PKG_VERSION="0"
PKG_REV="1"
PKG_ARCH="any"
PKG_LICENSE="GPL"
PKG_SITE=""
PKG_URL=""
PKG_DEPENDS_TARGET="toolchain"
PKG_PRIORITY="optional"
PKG_SECTION="system"
PKG_SHORTDESC="A simple way to add entropy at boot"
PKG_LONGDESC="A simple way to add entropy at boot"
PKG_IS_ADDON="no"
PKG_AUTORECONF="no"
make_target(){
:
}
makeinstall_target() {
mkdir -p $INSTALL/usr/lib/entropy
cp add-entropy $INSTALL/usr/lib/entropy
cp add-random-at-shutdown $INSTALL/usr/lib/entropy
chmod +x $INSTALL/usr/lib/entropy/*
}
post_install() {
enable_service add-entropy.service
enable_service add-random-at-shutdown.service
}

View File

@ -0,0 +1,24 @@
#!/usr/bin/python
import os
import struct
RNDADDENTROPY = 0x40085203
import fcntl
def add_entropy(fd, data):
add = struct.pack('ii', len(data)*8, len(data)) + data
fcntl.ioctl(fd, RNDADDENTROPY, add)
if not os.path.isfile("/storage/.cache/random.data"):
os.system("dd if=/dev/urandom of=/storage/.cache/random.data count=4 >/dev/null")
cache=os.open("/storage/.cache/random.data", os.O_RDONLY)
rnd=os.open("/dev/random", os.O_RDWR)
while True:
data=os.read(cache, 512)
if len(data) == 0:
break
add_entropy(rnd, data)

View File

@ -0,0 +1,2 @@
#!/bin/sh
dd if=/dev/urandom of=/storage/.cache/random.data count=4

View File

@ -0,0 +1,13 @@
[Unit]
Description=Add random entropy from file
DefaultDependencies=no
After=systemd-tmpfiles-setup.service
Before=systemd-udevd.service
[Service]
Type=oneshot
ExecStart=/usr/lib/entropy/add-entropy
RemainAfterExit=yes
[Install]
WantedBy=basic.target

View File

@ -0,0 +1,12 @@
[Unit]
Description=Save random entropy at shutdown
DefaultDependencies=no
Before=systemd-poweroff.service systemd-reboot.service systemd-halt.service
[Service]
Type=oneshot
ExecStart=/usr/lib/entropy/add-random-at-shutdown
RemainAfterExit=yes
[Install]
WantedBy=poweroff.target reboot.target halt.target