tiny.reticulum.crypto.pkcs7
Defined in crypto.
API (8)
Actions
Public operations.
pad: Writes the padded data intooutand returns it, with between one and sixteen copies of the padding count appended, so a caller fills a plaintext out to whole blocks before the cipher runs, following Reticulum@1.5.0 RNS/Cryptography/PKCS7.py:32-39.paddedLengthunpad: Returns the data with as many trailing bytes removed as its final byte counts, so a caller cuts the padding back off a plaintext the cipher produced, following Reticulum@1.5.0 RNS/Cryptography/PKCS7.py:41-48.
Types and contracts
Public types and contracts.
Values and defaults
Public values and defaults.
Source
Source: lib/reticulum/src/crypto/pkcs7.zig
zig
const std = @import("std");pub const block_length: u8 = 16;pub const max_data_length: u16 = 65_519;pub const max_padded_length: u16 = 65_520;pub const PadError = error{ InputTooLong, OutputTooSmall,};pub const UnpadError = error{ EmptyInput, InvalidPadding,};pub fn paddedLength(data_length: u17) PadError!u16 { if (data_length > max_data_length) return error.InputTooLong; const remainder: u8 = @intCast(data_length % block_length); const padding: u8 = block_length - remainder; const result = data_length + padding; std.debug.assert(result <= max_padded_length); return @intCast(result);}/// Writes the padded data into `out` and returns it, with between one and/// sixteen copies of the padding count appended, so a caller fills a plaintext/// out to whole blocks before the cipher runs, following Reticulum@1.5.0/// RNS/Cryptography/PKCS7.py:32-39. Data already a whole number of blocks long/// gains a full block of padding, so the count is never zero. The call returns/// `error.InputTooLong` past 65,519 bytes and `error.OutputTooSmall` when `out`/// is shorter than the padded length.pub fn pad(data: []const u8, out: []u8) PadError![]u8 { if (data.len > max_data_length) return error.InputTooLong; const result_length = try paddedLength(@intCast(data.len)); const result_length_usize: usize = result_length; if (out.len < result_length_usize) return error.OutputTooSmall; const result = out[0..result_length_usize]; @memmove(result[0..data.len], data); const padding: u8 = @intCast(result.len - data.len); @memset(result[data.len..], padding); return result;}/// Returns the data with as many trailing bytes removed as its final byte/// counts, so a caller cuts the padding back off a plaintext the cipher/// produced, following Reticulum@1.5.0 RNS/Cryptography/PKCS7.py:41-48. A final/// byte counting more than sixteen returns `error.InvalidPadding`, and empty/// data returns `error.EmptyInput`. A final byte counting more than the data/// holds is handled the way the reference's Python slicing handles it: that/// slicing leaves twice the length less the count, or nothing at all once the/// count reaches twice the length. A final byte of zero removes nothing.pub fn unpad(data: []const u8) UnpadError![]const u8 { if (data.len == 0) return error.EmptyInput; const padding = data[data.len - 1]; if (padding > block_length) return error.InvalidPadding; const stop = pythonSliceStop(data.len, padding); return data[0..stop];}fn pythonSliceStop(length: usize, padding: u8) usize { if (padding <= length) return length - padding; std.debug.assert(length < block_length); const doubled = length * 2; if (padding >= doubled) return 0; return doubled - padding;}Source: lib/reticulum/src/crypto/root.zig:48
zig
pub const pkcs7 = @import("pkcs7.zig");Audit
| Definitions | 9 |
|---|---|
| Public names | 9 |
| Members | 4 |
| Version | 26.7.0 |
| Revision | daab053ee433 |