001/*
002 * jPOS Project [http://jpos.org]
003 * Copyright (C) 2000-2026 jPOS Software SRL
004 *
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Affero General Public License as
007 * published by the Free Software Foundation, either version 3 of the
008 * License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU Affero General Public License for more details.
014 *
015 * You should have received a copy of the GNU Affero General Public License
016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017 */
018
019package org.jpos.security;
020
021import org.bouncycastle.util.encoders.Base64;
022import org.jpos.iso.ISOUtil;
023
024import java.io.IOException;
025import java.io.InputStream;
026import java.nio.ByteBuffer;
027
028/**
029 * Provides deterministic byte sequences derived from a built-in seed table,
030 * used as a stable source of pseudo-random data when no live RNG is needed.
031 */
032@SuppressWarnings("unused")
033public class SystemSeed {
034    /** Default constructor; no instance state to initialise. */
035    public SystemSeed() {}
036    /**
037     * Returns {@code l} bytes from the seed table, starting at offset 0.
038     *
039     * @param l number of bytes to return
040     * @return the requested bytes
041     */
042    public static byte[] getSeed (int l) {
043        return getSeed(0, l);
044    }
045    /**
046     * Returns {@code l} bytes from the seed table, starting at the given offset.
047     *
048     * @param offset starting offset (wrapped modulo the seed table length)
049     * @param l number of bytes to return
050     * @return the requested bytes
051     */
052    public static byte[] getSeed (int offset, int l) {
053        ByteBuffer buf = ByteBuffer.allocate(l);
054        while (buf.hasRemaining()) {
055            offset = offset % seed.length;
056            int i = Math.min(l-buf.position(), seed.length - offset);
057            buf.put(seed, offset, i);
058            offset += i;
059        }
060        return buf.array();
061    }
062
063    private static final byte[] seed;
064    static {
065        try {
066            byte[] _s0 = get("/META-INF/q2/.seed");
067            byte[] _s1 = get("/META-INF/.seed");
068            seed = _s1 == null ? _s0 : ISOUtil.xor(_s0, _s1);
069            if (seed == null || seed.length < 16)
070                throw new IllegalArgumentException ("Invalid seed");
071        } catch (IOException e) {
072            throw new IllegalArgumentException("Invalid system configuration");
073        }
074    }
075
076    private static byte[] get (String path) throws IOException {
077        InputStream is = SystemSeed.class.getResourceAsStream(path);
078        if (is != null) {
079            try {
080                byte[] b = new byte[is.available()];
081                is.read(b);
082                return Base64.decode(b);
083            } finally {
084                is.close();
085            }
086        }
087        return null;
088    }
089}