PMTOTP - time-based one-time passwords for OS/2 Presentation Manager
====================================================================

PMTOTP generates the six to eight digit codes that web sites and
services ask for as a second factor (TOTP, RFC 6238) - the same codes
an authenticator app on a phone shows. It runs on OS/2 and eComStation,
keeps its entries in a plain text file, can copy a code to the
clipboard, and can encrypt the secrets in that file with a master
password.

Algorithms: SHA-1, SHA-256 and SHA-512. Digits: 6 to 8. Interval: 1 to
86400 seconds (usually 30).


Starting the program
--------------------

    pmtotp [file]

Without an argument the program reads TOTP.TXT in the current
directory. With an argument it uses that file instead.

The codes depend on the system time. Make sure the clock and the time
zone of your OS/2 system are correct (TZ setting), otherwise the codes
will not be accepted by the service.


Using the program
-----------------

The main window is a list of the entry names.

  * Double-click an entry, or select it and press Enter: a dialog shows
    the current code and the remaining time in seconds. The code is
    shown in red during the last sixth of its interval and changes by
    itself when the interval ends.
  * OK (or Enter) closes the dialog and copies the code to the
    clipboard as plain text, digits only. Cancel (or Esc) closes it
    without copying anything.
  * F5 reads the file again, for example after you changed it with a
    text editor. Errors, such as lines that could not be read, are
    shown in the title bar.


Menu and keys
-------------

  File
    New ...        Ins      create an entry
    Modify ...     F2       change the selected entry
    Delete         Del      delete the selected entry (asks first)
    Password ...            set or change the master password
    Quit                    end the program
  Help
    About ...               copyright notice

The entry dialog has one field each for name, secret, algorithm,
digits and interval. Tab and Shift+Tab move between the fields, Enter
means OK and Esc means Cancel.

  Name      letters (A-Z, a-z), digits, underscore, hyphen and dot,
            up to 47 characters. A name that is already in the file
            and is left unchanged is accepted as it is.
  Secret    the base32 key you get from the service. Lower case,
            blanks and dashes are tolerated; the program stores it
            as clean upper case.
  Algorithm SHA1, SHA256 or SHA512 (empty = SHA1).
  Digits    6, 7 or 8 (empty = 6).
  Interval  1 to 86400 seconds (empty = 30).

OK checks all fields and shows a message instead of saving if
something is wrong, so an invalid entry can not get into the file.


The file TOTP.TXT
-----------------

One entry per line:

    Name:SECRET
    Name:SECRET;ALGORITHM,DIGITS,PERIOD

The secret follows the LAST colon of the line, so an older file may
contain colons in a name. The part after the semicolon is positional;
an empty field keeps its default (SHA1, 6 digits, 30 seconds):

    # comment
    GitHub:JBSWY3DPEHPK3PXP
    Bank:gezd gnbv gy3t qojq;SHA256,8
    Token:JBSWY3DPEHPK3PXP;,,60
    Other:JBSWY3DPEHPK3PXP;SHA512,7,45

Empty lines and lines starting with # are ignored.

When you create, change or delete an entry in the program, only that
one line of the file is touched. Comments, blank lines and everything
else stay exactly as they are. The new file is written to a temporary
file with the extension .$$$ in the same directory, which then
replaces the original.

Every entry remembers a checksum of its line. If the file was changed
by another program after it was loaded, the program refuses to
overwrite it and asks you to press F5 first, so that a wrong line is
never replaced.


Master password and encrypted secrets
-------------------------------------

Choose File > Password, type a master password twice, and all entries
are encrypted in one step. From then on the program asks for the
password at startup. Cancel in that dialog ends the program. A new or
changed entry is encrypted automatically. Choosing File > Password
again changes the password.

The password can be up to 63 characters long. Deriving the key takes
a moment (the mouse pointer shows a clock).

What the file looks like afterwards:

    #KDF 100000 KFD5UMCMCIHNXSLPGBW2NN5PFE
    GitHub:$NIW6S5KF47UOXTW7AMYTLG7HI4GDHHLKEWLA
    Bank:$6TWU4P5DUGMPZ62YYQOC4AORKKNAF2EMTISQ;SHA256,8

  * The #KDF line holds the iteration count and the salt (base32).
    A file without this line is not encrypted and is read as usual.
  * Only the secret is encrypted. It is stored as $ followed by the
    base32 text of a 12 byte nonce and the encrypted key. Names,
    algorithm, digits and interval stay readable.
  * The key is derived from the password with PBKDF2-HMAC-SHA256
    (random salt, 100000 iterations for a new password). The secrets
    are encrypted with ChaCha20 (RFC 8439). Every time an entry is
    written it gets a new nonce.
  * Plain entries in an encrypted file are still accepted. They are
    encrypted when you change them or set the password again.
  * The iteration count of a new password is KDF_ITER in the source.
    On a slow machine you may want a smaller value; an existing file
    keeps the count stored in its #KDF line.

Please note:

  * THERE IS NO CHECK OF THE PASSWORD. This is deliberate: the file
    then gives nobody a way to test password guesses against it. The
    price is that a wrong password is not noticed, it just produces
    wrong codes. If the codes do not match the service, close the
    program and start it again with the right password. Do not
    create or change entries while the codes are wrong: they would be
    written under the wrong key.
  * If you forget the password, the secrets can not be recovered.
    Keep the recovery codes and the original secrets of your services
    in a safe place, and keep a backup copy of TOTP.TXT.
  * The encryption protects the file, for example a stolen copy or
    a backup. It does not protect against someone who can use the
    running program, against key loggers or other malware, or against
    reading the memory of the running program. The password is
    wiped from the program's own buffers after use, but the derived
    key and the decrypted secrets must stay in memory while the
    program runs.
  * ChaCha20 hides the secrets but does not detect changes. Someone
    who can write to the file can damage entries.
  * There is no menu item to remove the encryption again.
  * Try the encryption on a copy of your file first.


Limits
------

  * 100 entries. Further lines are neither shown nor encrypted.
  * Names up to 47 characters, secrets up to 64 bytes (103 base32
    characters), lines up to 511 characters.
  * The temporary file name is the file name with the extension .$$$,
    so that it works on FAT as well.


Building
--------

The program is one C source file, C90, no resource file needed.
With Open Watcom (tested with version 1.1):

    wcl386 -bt=os2 -l=os2v2_pm -w4 pmtotp.c


License and credits
-------------------

Copyright (c) 2026 Axel Meiss

Written by Claude (Anthropic). Compiled, tested and published by
Axel Meiss.

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
