jpsec

package
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 30, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package jpsec implements JPEG 2000 Part 8 (JPSEC) security features as specified in ISO/IEC 15444-8.

JPSEC provides tools for securing JPEG 2000 images including:

  • Security zones for defining protected regions
  • Encryption of codestream data
  • Authentication and integrity verification
  • Key management for encrypted content

Architecture

The JPSEC framework uses marker segments to describe security properties:

  • SEC: Security marker defining protected zones
  • Encryption markers for specifying encryption algorithms
  • Authentication markers for integrity verification
  • Key management markers for key distribution

Security Zones

A security zone defines a contiguous region of the codestream that is protected by a specific security tool (encryption, authentication, etc.). Zones can overlap and are identified by zone indices.

Encryption Interface

This package provides a decryption interface that allows external implementations to handle actual cryptographic operations. The package parses encryption markers and delegates decryption to user-provided implementations.

Usage

The JPSEC reader integrates with the main JPEG 2000 decoder. When security markers are detected, the reader extracts security metadata and, if a decryptor is provided, processes encrypted regions.

Security

All operations validate input bounds and use safe integer conversions from the internal/safeconv package. The parser enforces security limits defined in the security package to prevent denial-of-service attacks.

References

  • ISO/IEC 15444-8:2007 - JPEG 2000 image coding system: Secure JPEG 2000
  • ITU-T Rec. T.807 (2006)

Index

Constants

View Source
const (
	// MarkerSEC is the Security marker (0xFF65).
	// This marker introduces security zone definitions.
	MarkerSEC uint16 = 0xFF65

	// MarkerINS is the Insecurity marker (0xFF94).
	// Marks end of security zone or insecure region.
	MarkerINS uint16 = 0xFF94
)

JPSEC marker codes per ISO/IEC 15444-8.

View Source
const (
	// ToolEncryption identifies encryption security tools.
	ToolEncryption uint8 = 0x01

	// ToolAuthentication identifies authentication/integrity tools.
	ToolAuthentication uint8 = 0x02

	// ToolKeyManagement identifies key management tools.
	ToolKeyManagement uint8 = 0x03

	// ToolAccessControl identifies access control tools.
	ToolAccessControl uint8 = 0x04

	// ToolDigitalSignature identifies digital signature tools.
	ToolDigitalSignature uint8 = 0x05
)

Security tool identifiers per ISO/IEC 15444-8.

View Source
const (
	// AlgorithmAES128 is AES with 128-bit key.
	AlgorithmAES128 uint8 = 0x01

	// AlgorithmAES192 is AES with 192-bit key.
	AlgorithmAES192 uint8 = 0x02

	// AlgorithmAES256 is AES with 256-bit key.
	AlgorithmAES256 uint8 = 0x03

	// Algorithm3DES is Triple DES (168-bit effective key).
	Algorithm3DES uint8 = 0x04

	// AlgorithmJPSEC is JPSEC-specific scrambling algorithm.
	AlgorithmJPSEC uint8 = 0x10
)

Encryption algorithms supported by JPSEC.

View Source
const (
	// ModeCBC is Cipher Block Chaining mode.
	ModeCBC uint8 = 0x01

	// ModeCTR is Counter mode.
	ModeCTR uint8 = 0x02

	// ModeGCM is Galois/Counter Mode (authenticated encryption).
	ModeGCM uint8 = 0x03
)

Block cipher modes for encryption.

View Source
const (
	// MaxSecurityZones is the maximum number of security zones allowed.
	MaxSecurityZones = 256

	// MaxKeySize is the maximum key size in bytes.
	MaxKeySize = 64

	// MaxIVSize is the maximum initialization vector size in bytes.
	MaxIVSize = 32

	// MaxAuthTagSize is the maximum authentication tag size in bytes.
	MaxAuthTagSize = 32
)

Maximum limits for security validation.

Variables

View Source
var (
	// ErrInvalidSecurityZone indicates the security zone structure is malformed.
	ErrInvalidSecurityZone = errors.New("jpsec: invalid security zone")

	// ErrEncryptionParsing indicates an error parsing encryption markers.
	ErrEncryptionParsing = errors.New("jpsec: encryption parsing error")

	// ErrKeyManagement indicates an error in key management operations.
	ErrKeyManagement = errors.New("jpsec: key management error")

	// ErrAuthentication indicates an authentication or integrity error.
	ErrAuthentication = errors.New("jpsec: authentication error")

	// ErrTruncatedData indicates the security data is incomplete.
	ErrTruncatedData = errors.New("jpsec: truncated security data")

	// ErrInvalidMarker indicates an invalid JPSEC marker was encountered.
	ErrInvalidMarker = errors.New("jpsec: invalid marker")

	// ErrUnsupportedTool indicates an unsupported security tool was specified.
	ErrUnsupportedTool = errors.New("jpsec: unsupported security tool")

	// ErrUnsupportedAlgorithm indicates an unsupported encryption algorithm.
	ErrUnsupportedAlgorithm = errors.New("jpsec: unsupported encryption algorithm")

	// ErrDecryptionFailed indicates decryption of protected content failed.
	ErrDecryptionFailed = errors.New("jpsec: decryption failed")

	// ErrNoDecryptor indicates a decryptor is required but not provided.
	ErrNoDecryptor = errors.New("jpsec: decryptor not provided")

	// ErrZoneOverlap indicates security zones have invalid overlap.
	ErrZoneOverlap = errors.New("jpsec: invalid zone overlap")

	// ErrMaxZonesExceeded indicates too many security zones were defined.
	ErrMaxZonesExceeded = errors.New("jpsec: maximum zones exceeded")
)

JPSEC-specific errors for security parsing operations.

Functions

This section is empty.

Types

type AuthenticationParams

type AuthenticationParams struct {
	// Algorithm specifies the MAC/hash algorithm.
	Algorithm uint8

	// HashValue contains the computed hash/MAC value.
	HashValue []byte

	// CertificateID identifies the signing certificate.
	CertificateID []byte
}

AuthenticationParams contains parameters for authenticated zones.

type Decryptor

type Decryptor interface {
	// Decrypt decrypts data using the provided encryption parameters.
	// Returns the decrypted data or an error if decryption fails.
	Decrypt(data []byte, params *EncryptionParams) ([]byte, error)

	// SupportsAlgorithm returns true if the algorithm is supported.
	SupportsAlgorithm(algorithm, mode uint8) bool
}

Decryptor defines the interface for decrypting JPSEC protected content. Implementations should handle the actual cryptographic operations.

type EncryptionParams

type EncryptionParams struct {
	// Algorithm specifies the encryption algorithm.
	Algorithm uint8

	// Mode specifies the block cipher mode.
	Mode uint8

	// KeyID identifies the encryption key.
	KeyID []byte

	// IV is the initialization vector for block cipher modes.
	IV []byte

	// AuthTag is the authentication tag for authenticated encryption.
	AuthTag []byte
}

EncryptionParams contains parameters for encrypted zones.

type KeyManagementParams

type KeyManagementParams struct {
	// KeyID is the key identifier.
	KeyID []byte

	// EncryptedKey is the encrypted key data.
	EncryptedKey []byte

	// KeyWrapAlgorithm specifies how the key was encrypted.
	KeyWrapAlgorithm uint8

	// RecipientID identifies the intended key recipient.
	RecipientID []byte
}

KeyManagementParams contains key management information.

type Parser

type Parser struct {
	// contains filtered or unexported fields
}

Parser parses JPSEC security markers from JPEG 2000 codestreams.

func NewParser

func NewParser() *Parser

NewParser creates a new JPSEC parser.

func (*Parser) ParseAuthenticationMarker

func (p *Parser) ParseAuthenticationMarker(data []byte) (*AuthenticationParams, error)

ParseAuthenticationMarker parses authentication parameters from tool data.

func (*Parser) ParseEncryptionMarker

func (p *Parser) ParseEncryptionMarker(data []byte) (*EncryptionParams, error)

ParseEncryptionMarker parses encryption parameters from tool data.

func (*Parser) ParseKeyManagementMarker

func (p *Parser) ParseKeyManagementMarker(data []byte) (*KeyManagementParams, error)

ParseKeyManagementMarker parses key management parameters from tool data.

func (*Parser) ParseSecurityZone

func (p *Parser) ParseSecurityZone(data []byte) (*SecurityZone, error)

ParseSecurityZone parses a SEC marker segment to extract security zone information.

func (*Parser) ProcessZone

func (p *Parser) ProcessZone(zone *SecurityZone, data []byte) ([]byte, error)

ProcessZone attempts to decrypt/verify a security zone. Returns the processed data or an error.

func (*Parser) SetDecryptor

func (p *Parser) SetDecryptor(d Decryptor)

SetDecryptor sets the decryptor for handling encrypted content.

type Reader

type Reader struct {
	// contains filtered or unexported fields
}

Reader reads JPSEC-protected JPEG 2000 codestreams.

func NewReader

func NewReader(data []byte) *Reader

NewReader creates a new JPSEC reader.

func (*Reader) GetContext

func (r *Reader) GetContext() *SecurityContext

GetContext returns the security context.

func (*Reader) HasSecurity

func (r *Reader) HasSecurity() bool

HasSecurity returns true if the codestream has security features.

func (*Reader) IsAuthenticated

func (r *Reader) IsAuthenticated() bool

IsAuthenticated returns true if the codestream has authentication.

func (*Reader) IsEncrypted

func (r *Reader) IsEncrypted() bool

IsEncrypted returns true if the codestream has encrypted zones.

func (*Reader) SetDecryptor

func (r *Reader) SetDecryptor(d Decryptor)

SetDecryptor sets the decryptor for handling encrypted content.

type SECMarker

type SECMarker struct {
	// Length is the marker segment length.
	Length uint16

	// ZoneIndex is the security zone index.
	ZoneIndex uint8

	// Flags contains security flags.
	Flags uint8

	// Tool identifies the security tool.
	Tool uint8

	// Offset specifies the zone start offset.
	Offset int64

	// Size specifies the zone size.
	Size int64

	// ToolParams contains tool-specific parameters.
	ToolParams []byte
}

SECMarker represents a parsed SEC (Security) marker segment.

type SecurityContext

type SecurityContext struct {
	// Zones contains all defined security zones.
	Zones []*SecurityZone

	// HasEncryption is true if any zone uses encryption.
	HasEncryption bool

	// HasAuthentication is true if any zone uses authentication.
	HasAuthentication bool

	// IsFullyProtected is true if the entire codestream is protected.
	IsFullyProtected bool
}

SecurityContext holds all security information for a codestream.

func NewSecurityContext

func NewSecurityContext() *SecurityContext

NewSecurityContext creates an empty security context.

func (*SecurityContext) AddZone

func (c *SecurityContext) AddZone(zone *SecurityZone) error

AddZone adds a security zone to the context.

func (*SecurityContext) GetZone

func (c *SecurityContext) GetZone(index uint8) *SecurityZone

GetZone returns the security zone with the given index.

func (*SecurityContext) GetZonesAtOffset

func (c *SecurityContext) GetZonesAtOffset(offset int64) []*SecurityZone

GetZonesAtOffset returns all zones that contain the given offset.

type SecurityZone

type SecurityZone struct {
	// Index is the zone identifier (0-255).
	Index uint8

	// StartOffset is the byte offset where the zone begins.
	StartOffset int64

	// EndOffset is the byte offset where the zone ends.
	EndOffset int64

	// Tool identifies the security tool applied to this zone.
	Tool uint8

	// ToolData contains tool-specific parameters.
	ToolData []byte

	// Encryption contains encryption parameters if Tool == ToolEncryption.
	Encryption *EncryptionParams

	// Authentication contains authentication parameters if Tool == ToolAuthentication.
	Authentication *AuthenticationParams

	// KeyManagement contains key info if Tool == ToolKeyManagement.
	KeyManagement *KeyManagementParams
}

SecurityZone represents a JPSEC security zone. A zone defines a contiguous region of the codestream protected by security tools.

func (*SecurityZone) Contains

func (z *SecurityZone) Contains(offset int64) bool

Contains returns true if the given offset falls within this zone.

func (*SecurityZone) Size

func (z *SecurityZone) Size() int64

Size returns the size of the security zone in bytes.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL